added graylisting
[org.ibex.mail.git] / src / org / ibex / mail / protocol / SMTP.java
index 2ba9fdc..be67a0e 100644 (file)
@@ -31,6 +31,16 @@ public class SMTP {
 
     public static final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
     public static final int numOutgoingThreads = 5;
+
+    public static final int GRAYLIST_MINWAIT =  1000 * 60 * 60;           // one hour
+    public static final int GRAYLIST_MAXWAIT =  1000 * 60 * 60 * 24 * 5;  // five days
+
+    public static final Graylist graylist =
+        new Graylist(Mailbox.STORAGE_ROOT+"/graylist.sqlite");
+
+    public static final int MAX_MESSAGE_SIZE =
+        Integer.parseInt(System.getProperty("org.ibex.mail.smtp.maxMessageSize", "-1"));
+
     private static final Mailbox spool =
         FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT,false).slash("spool",true).slash("smtp",true);
 
@@ -119,7 +129,27 @@ public class SMTP {
                 } else if (c.startsWith("DATA")) {
                     //if (from == null) { conn.println("503 MAIL FROM command must precede DATA"); continue; }
                     if (to == null || to.size()==0) { conn.println("503 RCPT TO command must precede DATA"); continue; }
-                    conn.println("354 Enter message, ending with \".\" on a line by itself");
+                    if (!graylist.isWhitelisted(conn.getRemoteAddress()) && !conn.getRemoteAddress().isLoopbackAddress()) {
+                        long when = graylist.getGrayListTimestamp(conn.getRemoteAddress(), from+"", to+"");
+                        if (when == 0 || System.currentTimeMillis() - when > GRAYLIST_MAXWAIT) {
+                            graylist.setGrayListTimestamp(conn.getRemoteAddress(), from+"", to+"",  System.currentTimeMillis());
+                            conn.println("451 you are graylisted; please try back in one hour to be whitelisted");
+                            Log.warn(conn.getRemoteAddress().toString(), "451 you are graylisted; please try back in one hour to be whitelisted");
+                            conn.flush();
+                            continue;
+                        } else if (System.currentTimeMillis() - when > GRAYLIST_MINWAIT) {
+                            graylist.addWhitelist(conn.getRemoteAddress());
+                            conn.println("354 (you have been whitelisted) Enter message, ending with \".\" on a line by itself");
+                            Log.warn(conn.getRemoteAddress().toString(), "has been whitelisted");
+                        } else {
+                            conn.println("451 you are still graylisted (since "+new java.util.Date(when)+")");
+                            conn.flush();
+                            Log.warn(conn.getRemoteAddress().toString(), "451 you are still graylisted (since "+new java.util.Date(when)+")");
+                            continue;
+                        }
+                    } else {
+                        conn.println("354 Enter message, ending with \".\" on a line by itself");
+                    }
                     conn.flush();
                     try {
                         StringBuffer buf = new StringBuffer();