use reverse DNS for EHLO/HELO
[org.ibex.mail.git] / src / org / ibex / mail / SMTP.java
index a426987..df480ac 100644 (file)
@@ -16,6 +16,9 @@ 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
 //     - did not implement section 5, though
@@ -43,9 +46,15 @@ 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 int RETRY_TIME = 1000 * 60 * 30;
+
     public static final Graylist graylist;
     public static final Whitelist whitelist;
     static {
@@ -70,7 +79,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 {
@@ -181,7 +199,8 @@ public class SMTP {
                     command = command.substring(10).trim();
                     from = command.equals("<>") ? null : new Address(command);
                     conn.println("250 " + from + " is syntactically correct");
-                    // FEATURE: perform SMTP validation on the address, reject if invalid
+                    // 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();
@@ -353,14 +372,23 @@ public class SMTP {
             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);
                 }
                 String envelopeFrom = m.envelopeFrom==null ? "" : m.envelopeFrom.toString();
@@ -428,6 +456,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);
@@ -452,7 +482,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);
                    }