major simplifications to Iterator api
[org.ibex.mail.git] / src / org / ibex / mail / protocol / SMTP.java
index 982d175..35855f1 100644 (file)
@@ -15,11 +15,9 @@ import java.text.*;
 import javax.naming.*;
 import javax.naming.directory.*;
 
-// FIXME: better delivery cycle attempt algorithm; current one sucks
 // FIXME: logging: current logging sucks
 // FIXME: loop prevention
-
-// graylisting?
+// FIXME: probably need some throttling on outbound mail
 
 // FEATURE: infer messageid, date, if not present (?)
 // FEATURE: exponential backoff on retry time?
@@ -31,6 +29,19 @@ 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+"/db/graylist.sqlite");
+
+    public static final Whitelist whitelist =
+        new Whitelist(Mailbox.STORAGE_ROOT+"/db/whitelist.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);
 
@@ -63,7 +74,7 @@ public class SMTP {
     // Server //////////////////////////////////////////////////////////////////////////////
 
     public static class Server {
-        public void handleRequest(Connection conn) {
+        public void handleRequest(Connection conn) throws IOException {
             conn.setTimeout(5 * 60 * 1000);
             conn.setNewline("\r\n");
             conn.println("220 " + conn.vhost + " SMTP " + this.getClass().getName());
@@ -73,7 +84,7 @@ public class SMTP {
             String remotehost = null;
             for(String command = conn.readln(); ; command = conn.readln()) try {
                 if (command == null) return;
-                Log.warn(conn.getRemoteAddress()+"", command);
+                //Log.warn("**"+conn.getRemoteAddress()+"**", command);
                 String c = command.toUpperCase();
                 if (c.startsWith("HELO"))        {
                     remotehost = c.substring(5).trim();
@@ -99,20 +110,48 @@ public class SMTP {
                     command = command.substring(8).trim();
                     if(command.indexOf(' ') != -1) command = command.substring(0, command.indexOf(' '));
                     Address addr = new Address(command);
+                    /*
+                    Log.warn("**"+conn.getRemoteAddress()+"**",
+                             "addr.isLocal(): " + addr.isLocal() + "\n" +
+                             "conn.getRemoteAddress().isLoopbackAddress(): " + conn.getRemoteAddress().isLoopbackAddress() + "\n" +
+                             "johnw: " + (from!=null&&from.toString().indexOf("johnw")!=-1) + "\n"
+                             );
+                    */
                     if (addr.isLocal()) {
                         // FEATURE: should check the address further and give 550 if undeliverable
                         conn.println("250 " + addr + " is on this machine; I will deliver it");
                         to.addElement(addr);
-                    } else if (conn.getRemoteAddress().isLoopbackAddress()) {
+                    } else if (conn.getRemoteAddress().isLoopbackAddress() || (from!=null&&from.toString().indexOf("johnw")!=-1)) {
                         conn.println("250 you are connected locally, so I will let you send");
                         to.addElement(addr);
                     } else {
                         conn.println("551 sorry, " + addr + " is not on this machine");
                     }
+                    conn.flush();
                 } 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();
@@ -128,6 +167,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;
@@ -142,14 +188,9 @@ 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) {               
-                        //conn.println("554 " + ioe.toString());
-                        Log.error(this, ioe);
-                        conn.close();
-                        return;
                     }
                 } else                    { conn.println("500 unrecognized command"); }                    
             } catch (Message.Malformed e) { conn.println("501 " + e.toString()); }
@@ -161,7 +202,7 @@ public class SMTP {
 
     public static class Outgoing extends Thread {
 
-        private static final HashSet deadHosts = new HashSet();
+        private static final HashMap deadHosts = new HashMap();
         public static void accept(Message m) throws IOException {
             if (m == null) { Log.warn(Outgoing.class, "attempted to accept(null)"); return; }
             String traces = m.headers.get("Received");
@@ -176,7 +217,7 @@ public class SMTP {
                 }
             }
             synchronized(Outgoing.class) {
-                spool.add(m);
+                spool.insert(m, Mailbox.Flag.defaultFlags);
                 Outgoing.class.notifyAll();
             }
         }
@@ -245,8 +286,8 @@ public class SMTP {
                 conn.println("RCPT TO:<"   + m.envelopeTo.toString()+">");      check(conn.readln(), conn);
                 conn.println("DATA");                          check(conn.readln(), conn);
                 Headers head = m.headers;
-                head.remove("return-path");
-                head.remove("bcc");
+                head = head.remove("return-path");
+                head = head.remove("bcc");
                 Stream stream = head.getStream();
                 for(String s = stream.readln(); s!=null; s=stream.readln()) {
                     if (s.startsWith(".")) conn.print(".");
@@ -364,6 +405,7 @@ public class SMTP {
                 ret = new InetAddress[1];
                 try {
                     ret[0] = InetAddress.getByName(hostName);
+                    if (ret[0].equals(IP.getIP(127,0,0,1)) || ret[0].isLoopbackAddress()) throw new UnknownHostException();
                     return ret;
                 } catch (UnknownHostException uhe) {
                     Log.warn(SMTP.class, "no MX hosts or A record for " + hostName);
@@ -378,7 +420,7 @@ public class SMTP {
                     mx = mx.substring(mx.indexOf(" ") + 1);
                     if (mx.charAt(mx.length() - 1) == '.') mx = mx.substring(0, mx.length() - 1);
                     InetAddress ia = InetAddress.getByName(mx);
-                    if (ia.equals(IP.getIP(127,0,0,1))) continue;
+                    if (ia.equals(IP.getIP(127,0,0,1)) || ia.isLoopbackAddress()) continue;
                     ret[i++] = ia;
                 }
             }