X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2Fprotocol%2FSMTP.java;h=8ff30c520e94021051d3175ccb46dcff84191c47;hb=640852b87a1c5ccd4e4a9f2e99ced024bdf20fbc;hp=6cfd1163175dc287eb0cf5a0c668b5dc2811c64b;hpb=99f99e0f611f5774ea6d231ecf4c6c8b6632ed00;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/protocol/SMTP.java b/src/org/ibex/mail/protocol/SMTP.java index 6cfd116..8ff30c5 100644 --- a/src/org/ibex/mail/protocol/SMTP.java +++ b/src/org/ibex/mail/protocol/SMTP.java @@ -5,8 +5,8 @@ package org.ibex.mail.protocol; import org.ibex.mail.*; import org.ibex.mail.target.*; -import org.ibex.jinetd.Worker; import org.ibex.util.*; +import org.ibex.net.*; import org.ibex.io.*; import java.net.*; import java.io.*; @@ -15,28 +15,45 @@ import java.text.*; import javax.naming.*; import javax.naming.directory.*; -// FIXME: bounce messages (must go to return-path unless empty, in which case do not send -// FIXME: if more than 100 "Received" lines, must drop message +// FIXME: logging: current logging sucks +// FIXME: loop prevention + +// graylisting? + // FEATURE: infer messageid, date, if not present (?) +// FEATURE: exponential backoff on retry time? // FEATURE: RFC2822, section 4.5.1: special "postmaster" address // FEATURE: RFC2822, section 4.5.4.1: retry strategies // FEATURE: RFC2822, section 5, multiple MX records, preferences, ordering -// FEATURE: exponential backoff on retry time? // FEATURE: RFC2822, end of 4.1.2: backslashes in headers 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 Mailbox spool = FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT,false).slash("spool",true).slash("smtp",true); - static { new Thread() { public void run() { Outgoing.runq(); } }.start(); } + static { + for(int i=0; i= 100) - Log.warn(SMTP.Outgoing.class, "Message with " + m.traces.length + " trace hops; dropping\n" + m.summary()); - */ - else synchronized(Outgoing.class) { + String traces = m.headers.get("Received"); + if (traces!=null) { + int lines = 0; + for(int i=0; i 100) { // required by rfc + Log.warn(SMTP.Outgoing.class, "Message with " + lines + " trace hops; dropping\n" + m.summary()); + return; + } + } + synchronized(Outgoing.class) { spool.add(m); - Outgoing.class.notify(); + Outgoing.class.notifyAll(); } } - public static boolean attempt(Message m) throws IOException { + public static boolean attempt(Message m) throws IOException { return attempt(m, false); } + public static boolean attempt(Message m, boolean noBounces) throws IOException { if (m.envelopeTo == null) { Log.warn(SMTP.Outgoing.class, "aieeee, null envelopeTo: " + m.summary()); return false; } InetAddress[] mx = getMailExchangerIPs(m.envelopeTo.host); if (mx.length == 0) { - Log.warn(SMTP.Outgoing.class, "could not resolve " + m.envelopeTo.host + "; bouncing it\n" + m.summary()); - accept(m.bounce("could not resolve " + m.envelopeTo.host)); - return true; + if (!noBounces) { + accept(m.bounce("could not resolve " + m.envelopeTo.host)); + return true; + } else { + Log.warn(SMTP.Outgoing.class, "could not resolve " + m.envelopeTo.host); + return false; + } } if (new Date().getTime() - m.arrival.getTime() > 1000 * 60 * 60 * 24 * 5) { - Log.warn(SMTP.Outgoing.class, "could not send message after 5 days; bouncing it\n" + m.summary()); - accept(m.bounce("could not send for 5 days")); - return true; + if (!noBounces) { + accept(m.bounce("could not send for 5 days")); + return true; + } else { + Log.warn(SMTP.Outgoing.class, "could not send for 5 days: " + m.summary()); + return false; + } } for(int i=0; i 3 && s.charAt(3) == '-') s = conn.readln(); if (s.startsWith("4")||s.startsWith("5")) throw new SMTPException(s); } @@ -193,33 +235,57 @@ public class SMTP { check(conn.readln(), conn); } if (m.envelopeFrom==null) { + Log.warn("", "MAIL FROM:<>"); conn.println("MAIL FROM:<>"); check(conn.readln(), conn); } else { - conn.println("MAIL FROM:<" + m.envelopeFrom.user + "@" + m.envelopeFrom.host+">"); check(conn.readln(), conn); + Log.warn("", "MAIL FROM:<" + m.envelopeFrom.toString()+">"); + conn.println("MAIL FROM:<" + m.envelopeFrom.toString()+">"); check(conn.readln(), conn); } - conn.println("RCPT TO:<" + m.envelopeTo.user + "@" + m.envelopeTo.host+">"); check(conn.readln(), conn); + conn.println("RCPT TO:<" + m.envelopeTo.toString()+">"); check(conn.readln(), conn); conn.println("DATA"); check(conn.readln(), conn); - Stream stream = m.getStream(); - boolean inheaders = true; - while(true) { - String s = stream.readln(); - if (s == null) break; - if (s.length() == 0) inheaders = false; - // quash Return-Path; required by RFC2822 - if (inheaders && s.toLowerCase().startsWith("Return-Path:")) continue; + Headers head = m.headers; + head.remove("return-path"); + head.remove("bcc"); + Stream stream = head.getStream(); + for(String s = stream.readln(); s!=null; s=stream.readln()) { + if (s.startsWith(".")) conn.print("."); + conn.println(s); + } + conn.println(""); + stream = m.getBody().getStream(); + for(String s = stream.readln(); s!=null; s=stream.readln()) { if (s.startsWith(".")) conn.print("."); conn.println(s); } conn.println("."); - check(conn.readln(), conn); - Log.warn(SMTP.Outgoing.class, "success: " + mx + " accepted " + m.summary()); + String resp = conn.readln(); + if (resp == null) + throw new SMTPException("server " + mx + " closed connection without accepting message"); + check(resp, conn); + Log.warn(SMTP.Outgoing.class, "success: " + mx + " accepted " + m.summary() + "\n["+resp+"]"); accepted = true; conn.close(); + } catch (SMTPException 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 (e.code >= 500 && e.code <= 599) { + try { + attempt(m.bounce("unable to deliver: " + e), true); + } catch (Exception ex) { + Log.error(SMTP.Outgoing.class, "exception while trying to deliver bounce; giving up completely"); + Log.error(SMTP.Outgoing.class, ex); + } + 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()); return false; } finally { if (conn != null) conn.close(); @@ -227,33 +293,61 @@ public class SMTP { return accepted; } - static void runq() { + private static HashSet threads = new HashSet(); + private static int serials = 1; + private int serial = serials++; + private Mailbox.Iterator it; + + public Outgoing() { + synchronized(Outgoing.class) { + threads.add(this); + } + } + + public void wake() { + int count = spool.count(Query.all()); + Log.info(SMTP.Outgoing.class, "outgoing thread #"+serial+" woke up; " + count + " messages to send"); try { - Log.setThreadAnnotation("[outgoing smtp] "); - Log.info(SMTP.Outgoing.class, "outgoing thread started; " + spool.count(Query.all()) + " messages to send"); while(true) { - if (Thread.currentThread().isInterrupted()) throw new InterruptedException(); - for(Mailbox.Iterator it = spool.iterator(); it.next(); ) { - try { - if (Thread.currentThread().isInterrupted()) throw new InterruptedException(); - if (attempt(it.cur())) it.delete(); - } catch (Exception e) { - if (e instanceof InterruptedException) throw e; - Log.error(SMTP.Outgoing.class, e); - } - } + boolean good = false; synchronized(Outgoing.class) { - if (Thread.currentThread().isInterrupted()) throw new InterruptedException(); - Log.info(SMTP.Outgoing.class, "outgoing thread going to sleep"); - Outgoing.class.wait(5 * 60 * 1000); - deadHosts.clear(); - Log.info(SMTP.Outgoing.class,"outgoing thread woke up; "+spool.count(Query.all())+" messages in queue"); + it = spool.iterator(); + OUTER: for(; it.next(); ) { + for(Outgoing o : threads) + if (o!=this && o.it != null && o.it.uid()==it.uid()) + continue OUTER; + good = true; + break; + } } + if (!good) break; + try { + if (attempt(it.cur())) it.delete(); + } catch (Exception e) { + Log.error(SMTP.Outgoing.class, e); + } + Log.info(this, "sleeping for 3s..."); + Thread.sleep(3000); } } catch (Exception e) { - Log.error(SMTP.Outgoing.class, "outgoing thread killed by exception: " + e); + //if (e instanceof InterruptedException) throw e; Log.error(SMTP.Outgoing.class, e); } + Log.info(SMTP.Outgoing.class, "outgoing thread #"+serial+" going back to sleep"); + it = null; + } + + public void run() { + try { + while(true) { + Log.setThreadAnnotation("[outgoing #"+serial+"] "); + wake(); + Thread.sleep(1000); + synchronized(Outgoing.class) { + Outgoing.class.wait(5 * 60 * 1000); + } + } + } catch (InterruptedException e) { Log.warn(this, e); } } } @@ -277,12 +371,14 @@ public class SMTP { } else { ret = new InetAddress[attr.size()]; NamingEnumeration ne = attr.getAll(); - for(int i=0; ne.hasMore(); i++) { + 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); - ret[i] = InetAddress.getByName(mx); + InetAddress ia = InetAddress.getByName(mx); + if (ia.equals(IP.getIP(127,0,0,1))) continue; + ret[i++] = ia; } } } catch (Exception e) {