added max message size limit
[org.ibex.mail.git] / src / org / ibex / mail / protocol / SMTP.java
index 2ba9fdc..0fbc478 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();
@@ -135,6 +165,13 @@ public class SMTP {
                             if (s.equals(".")) break;
                             if (s.startsWith(".")) s = s.substring(1);
                             buf.append(s + "\r\n");
+                            if (MAX_MESSAGE_SIZE != -1 && buf.length() > MAX_MESSAGE_SIZE) {
+                                Log.error("**"+conn.getRemoteAddress()+"**",
+                                          "sorry, this mail server only accepts messages of less than " +
+                                          ByteSize.toString(MAX_MESSAGE_SIZE));
+                                throw new MailException.Malformed("sorry, this mail server only accepts messages of less than " +
+                                                                  ByteSize.toString(MAX_MESSAGE_SIZE));
+                            }
                         }
                         String body = buf.toString();
                         Message m = null;
@@ -149,7 +186,7 @@ public class SMTP {
                     } catch (Reject.RejectException re) {
                        Log.warn(SMTP.class, "rejecting message due to: " + re.reason + "\n   " + re.m.summary());
                        conn.println("501 " + re.reason);
-                    } catch (MailException.Malformed mfe) { conn.println("501 " + mfe.toString());
+                    } catch (MailException.Malformed mfe) {   conn.println("501 " + mfe.toString());
                     } catch (MailException.MailboxFull mbf) { conn.println("452 " + mbf);
                     } catch (Later.LaterException le) {       conn.println("453 try again later");
                     } catch (IOException ioe) {