switch getMailExchangerIPs() to DNSUtil
[org.ibex.mail.git] / src / org / ibex / mail / SMTP.java
index bf162fb..db5da88 100644 (file)
@@ -11,8 +11,11 @@ import java.net.*;
 import java.io.*;
 import java.util.*;
 import java.text.*;
-import javax.naming.*;
-import javax.naming.directory.*;
+
+// FIXME: inbound throttling/ratelimiting
+
+// "Address enumeration detection" -- notice when it looks like somebody
+// is trying a raft of addresses.
 
 // RFC's implemented
 // RFC2554: SMTP Service Extension for Authentication
@@ -41,20 +44,31 @@ public class SMTP {
     public static final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
     public static final int numOutgoingThreads = 5;
 
+    private static final SqliteMailbox allmail =
+        (SqliteMailbox)FileBasedMailbox
+        .getFileBasedMailbox("/afs/megacz.com/mail/user/megacz/allmail", false);
+
     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 int RETRY_TIME = 1000 * 60 * 30;
 
-    public static final Whitelist whitelist =
-        new Whitelist(Mailbox.STORAGE_ROOT+"/db/whitelist.sqlite");
+    public static final Graylist graylist;
+    public static final Whitelist whitelist;
+    static {
+        try {
+            graylist =  new Graylist(Mailbox.STORAGE_ROOT+"/db/graylist.sqlite");
+            whitelist = new Whitelist(Mailbox.STORAGE_ROOT+"/db/whitelist.sqlite");
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
 
     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);
+        FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT,false).slash("spool",true).slash("smtp",true).getMailbox();
 
     static {
         for(int i=0; i<numOutgoingThreads; i++)
@@ -63,7 +77,16 @@ public class SMTP {
 
     public static void enqueue(Message m) throws IOException {
         if (!m.envelopeTo.isLocal()) Outgoing.enqueue(m);
-        else                         Target.root.accept(m);
+        else {
+            try {
+                allmail.accept(m);
+            } catch (Exception e) {
+                // FIXME incredibly gross hack
+                if (e.toString().indexOf("attempt to insert two messages with identical messageid")==-1)
+                    Log.error(SMTP.class, e);
+            }
+            Target.root.accept(m);
+        }
     }
 
     public static class SMTPException extends MailException {
@@ -94,6 +117,7 @@ public class SMTP {
             boolean ehlo = false;
             String remotehost = null;
             String authenticatedAs = null;
+            int failedRcptCount = 0;
             for(String command = conn.readln(); ; command = conn.readln()) try {
                 if (command == null) return;
                 Log.warn("**"+conn.getRemoteAddress()+"**", command);
@@ -173,21 +197,39 @@ public class SMTP {
                     command = command.substring(10).trim();
                     from = command.equals("<>") ? null : new Address(command);
                     conn.println("250 " + from + " is syntactically correct");
+                    // Don't perform SAV; discouraged here
+                    //   http://blog.fastmail.fm/2007/12/05/sending-email-servers-best-practice/
                 } else if (c.startsWith("RCPT TO:")) {
                     // some clients are broken and put RCPT first; we will tolerate this
                     command = command.substring(8).trim();
                     if(command.indexOf(' ') != -1) command = command.substring(0, command.indexOf(' '));
                     Address addr = new Address(command);
-                    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() || (from!=null&&from.toString().indexOf("johnw")!=-1)) {
+                    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);
+                        if (!whitelist.isWhitelisted(addr))
+                            whitelist.addWhitelist(addr);
+                    } else if (authenticatedAs!=null) {
+                        conn.println("250 you are authenticated as "+authenticatedAs+", so I will let you send");
+                        to.addElement(addr);
+                        if (!whitelist.isWhitelisted(addr))
+                            whitelist.addWhitelist(addr);
+                    } else if (addr.isLocal()) {
+                        if (to.size() > 3) {
+                            conn.println("536 sorry, limit on 3 RCPT TO's per DATA");
+                        } else {
+                            // 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 {
                         conn.println("535 sorry, " + addr + " is not on this machine, you are not connected from localhost, and I will not relay without SMTP AUTH");
                         Log.warn("","535 sorry, " + addr + " is not on this machine, you are not connected from localhost, and I will not relay without SMTP AUTH");
+                        failedRcptCount++;
+                        if (failedRcptCount > 3) {
+                            conn.close();
+                            return;
+                        }
                     }
                     conn.flush();
                 } else if (c.startsWith("DATA")) {
@@ -232,7 +274,7 @@ 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) {
+                            if (MAX_MESSAGE_SIZE != -1 && buf.length() > MAX_MESSAGE_SIZE && (from+"").indexOf("paperless")==-1) {
                                 Log.error("**"+conn.getRemoteAddress()+"**",
                                           "sorry, this mail server only accepts messages of less than " +
                                           ByteSize.toString(MAX_MESSAGE_SIZE));
@@ -240,12 +282,10 @@ public class SMTP {
                                                                   ByteSize.toString(MAX_MESSAGE_SIZE));
                             }
                         }
-                        String body = buf.toString();
+                        String message = buf.toString();
                         Message m = null;
-                        for(int i=0; i<to.size(); i++) {
-                           m = Message.newMessage(Fountain.Util.create(body), from, (Address)to.elementAt(i));
-                            enqueue(m);
-                       }
+                        for(int i=0; i<to.size(); i++)
+                            enqueue(m = Message.newMessage(Fountain.Util.create(message)).withEnvelope(from, (Address)to.elementAt(i)));
                         if (m != null) Log.info(SMTP.class, "accepted message: " + m.summary());
                         conn.println("250 message accepted");
                         conn.flush();
@@ -293,7 +333,7 @@ public class SMTP {
                 Log.warn(SMTP.Outgoing.class, "aieeee, null envelopeTo: " + m.summary());
                 return false;
             }
-            InetAddress[] mx = getMailExchangerIPs(m.envelopeTo.host);
+            InetAddress[] mx = DNSUtil.getMailExchangerIPs(m.envelopeTo.host);
             if (mx.length == 0) {
                if (!noBounces) {
                    enqueue(m.bounce("could not resolve " + m.envelopeTo.host));
@@ -314,7 +354,7 @@ public class SMTP {
             }
             for(int i=0; i<mx.length; i++) {
                 //if (deadHosts.contains(mx[i])) continue;
-                if (attempt(m, mx[i])) { return true; }
+                if (attempt(m, mx[i])) return true;
             }
             return false;
         }
@@ -322,35 +362,43 @@ public class SMTP {
         private static void check(String s, Connection conn) {
             if (s==null) return;
             while (s.length() > 3 && s.charAt(3) == '-') s = conn.readln();
-            if (s.startsWith("4")||s.startsWith("5")) throw new SMTPException(s);
+            //if (s.startsWith("4")||s.startsWith("5")) throw new SMTPException(s);
+            if (!s.startsWith("2")&&!s.startsWith("3")) throw new SMTPException(s);
         }
         private static boolean attempt(final Message m, final InetAddress mx) {
             boolean accepted = false;
             Connection conn = null;
             try {
                 conn = new Connection(new Socket(mx, 25), InetAddress.getLocalHost().getHostName());
+                InetAddress localAddress = conn.getSocket().getLocalAddress();
+                String reverse = DNSUtil.reverseLookup(localAddress);
+                Log.info(SMTP.Outgoing.class,
+                         "outbound connection to " + mx + " uses " + localAddress + " [reverse: " + reverse + "]");
+                InetAddress relookup = InetAddress.getByName(reverse);
+                if (!relookup.equals(localAddress))
+                    Log.error(SMTP.Outgoing.class,
+                              "Warning: local machine fails forward-confirmed-reverse; " +
+                              reverse + " resolves to " + localAddress);
                 conn.setNewline("\r\n");
                 conn.setTimeout(60 * 1000);
                 check(conn.readln(), conn);  // banner
                 try {
-                    conn.println("EHLO " + conn.vhost);
+                    conn.println("EHLO " + reverse);
                     check(conn.readln(), conn);
                 } catch (SMTPException smtpe) {
-                    conn.println("HELO " + conn.vhost);
+                    conn.println("HELO " + reverse);
                     check(conn.readln(), conn);
                 }
-                if (m.envelopeFrom==null) {
-                    Log.warn("", "MAIL FROM:<>");
-                    conn.println("MAIL FROM:<>");  check(conn.readln(), conn);
-                } else {
-                    Log.warn("", "MAIL FROM:<" + m.envelopeFrom.toString()+">");
-                    conn.println("MAIL FROM:<" + m.envelopeFrom.toString()+">");  check(conn.readln(), conn);
-                }
-                conn.println("RCPT TO:<"   + m.envelopeTo.toString()+">");      check(conn.readln(), conn);
-                conn.println("DATA");                          check(conn.readln(), conn);
-                Headers head = m.headers;
-                head = head.remove("return-path");
-                head = head.remove("bcc");
+                String envelopeFrom = m.envelopeFrom==null ? "" : m.envelopeFrom.toString();
+                conn.println("MAIL FROM:<" + envelopeFrom +">");            check(conn.readln(), conn);
+                conn.println("RCPT TO:<"   + m.envelopeTo.toString()+">");  check(conn.readln(), conn);
+                conn.println("DATA");                                       check(conn.readln(), conn);
+
+                Headers head = new Headers(m.headers,
+                                           new String[] {
+                                               "return-path", null,
+                                               "bcc", null
+                                           });
                 Stream stream = head.getStream();
                 for(String s = stream.readln(); s!=null; s=stream.readln()) {
                     if (s.startsWith(".")) conn.print(".");
@@ -375,6 +423,8 @@ public class SMTP {
                 Log.warn(SMTP.Outgoing.class, "    unable to send; error=" + e);
                 Log.warn(SMTP.Outgoing.class, "      message: " + m.summary());
                 Log.warn(SMTP.Outgoing.class, e);
+                /*
+                  // FIXME: we should not be bouncing here!
                 if (e.code >= 500 && e.code <= 599) {
                     try {
                         attempt(m.bounce("unable to deliver: " + e), true);
@@ -384,13 +434,14 @@ public class SMTP {
                     }
                     return true;
                 }
+                */
                 return false;
             } catch (Exception e) {
                 if (accepted) return true;
                 Log.warn(SMTP.Outgoing.class, "    unable to send; error=" + e);
                 Log.warn(SMTP.Outgoing.class, "      message: " + m.summary());
                 Log.warn(SMTP.Outgoing.class, e);
-                if (conn != null) Log.warn(SMTP.Outgoing.class, conn.dumpLog());
+                //if (conn != null) Log.warn(SMTP.Outgoing.class, conn.dumpLog());
                 return false;
             } finally {
                 if (conn != null) conn.close();
@@ -403,6 +454,8 @@ public class SMTP {
         private int serial = serials++;
         private Mailbox.Iterator it;
 
+        private static Map<String,Long> nextTry = Collections.synchronizedMap(new HashMap<String,Long>());
+
         public Outgoing() {
             synchronized(Outgoing.class) {
                 threads.add(this);
@@ -427,7 +480,12 @@ public class SMTP {
                     }
                     if (!good) break;
                    try {
-                       if (attempt(it.cur())) it.delete();
+                        String messageid = it.cur().messageid;
+                        if (nextTry.get(messageid) == null || System.currentTimeMillis() > nextTry.get(messageid)) {
+                            boolean ok = attempt(it.cur());
+                            if (ok) it.delete();
+                            else nextTry.put(messageid, System.currentTimeMillis() + RETRY_TIME);
+                        }
                    } catch (Exception e) {
                        Log.error(SMTP.Outgoing.class, e);
                    }
@@ -456,42 +514,4 @@ public class SMTP {
         }
     }
 
-    public static InetAddress[] getMailExchangerIPs(String hostName) {
-        InetAddress[] ret;
-        try {
-            Hashtable env = new Hashtable();
-            env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
-            DirContext ictx = new InitialDirContext(env);
-            Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
-            Attribute attr = attrs.get("MX");
-            if (attr == null) {
-                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);
-                    return new InetAddress[0];
-                }
-            } else {
-                ret = new InetAddress[attr.size()];
-                NamingEnumeration ne = attr.getAll();
-                for(int i=0; ne.hasMore();) {
-                    String mx = (String)ne.next();
-                    // FIXME we should be sorting here
-                    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)) || ia.isLoopbackAddress()) continue;
-                    ret[i++] = ia;
-                }
-            }
-        } catch (Exception e) {
-            Log.warn(SMTP.class, "couldn't find MX host for " + hostName + " due to");
-            Log.warn(SMTP.class, e);
-            return new InetAddress[0];
-        }
-        return ret;
-    }
 }