it compiles
[org.ibex.mail.git] / src / org / ibex / mail / protocol / SMTP.java
1 package org.ibex.mail.protocol;
2 import org.ibex.mail.*;
3 import org.ibex.mail.target.*;
4 import org.ibex.util.*;
5 import java.net.*;
6 import java.io.*;
7 import java.util.*;
8 import java.text.*;
9 import javax.naming.*;
10 import javax.naming.directory.*;
11
12 public class SMTP extends MessageProtocol {
13
14     static { new Thread() { public void run() { Outgoing.runq(); } }.start(); }
15     public static String convdir = null;
16     public static void main(String[] s) throws Exception {
17         String logto = System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
18         logto += File.separatorChar + "log";
19         Log.file(logto);
20         convdir = System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail");
21         convdir += File.separatorChar + "conversation";
22         new File(convdir).mkdirs();
23         final SMTP smtp = new SMTP();
24         int port = Integer.parseInt(System.getProperty("ibex.mail.port", "25"));
25         Log.info(SMTP.class, "binding to port " + port + "...");
26         ServerSocket ss = new ServerSocket(port);
27         Log.info(SMTP.class, "listening for connections...");
28         while(true) {
29             final Socket sock = ss.accept();
30             new Thread() { public void run() { smtp.handle(sock); } }.start();
31         }
32     }
33
34     //public SMTP() { setProtocolName("SMTP"); }
35     //public ServerRequest createRequest(Connection conn) { return new Listener((TcpConnection)conn); }
36
37     public void handle(Socket s) { new Listener(s, "megacz.com").handleRequest(); }
38
39     //  FEATURE: exponential backoff on retry time?
40     public static class Outgoing {
41         private static final HashSet deadHosts = new HashSet();
42         private static final org.ibex.util.Queue queue = new org.ibex.util.Queue(100);
43
44         public static void send(Message m) throws IOException {
45             if (m.traces.length >= 100) {
46                 Log.warn(SMTP.Outgoing.class,
47                          "Message with " + m.traces.length + " trace hops; silently dropping\n" + m.summary());
48                 return;
49             }
50             synchronized(Outgoing.class) {
51                 FileSystem.root.slash("outgoing").add(m);
52                 queue.append(m);
53                 Outgoing.class.notify();
54             }
55         }
56
57         // FIXME!!! ignores more than one destination envelope!!!!
58         private static boolean attempt(Message m) throws IOException {
59             InetAddress[] mx = getMailExchangerIPs(m.envelopeTo[0].host);
60             if (mx.length == 0) {
61                 Log.warn(SMTP.Outgoing.class, "could not resolve " + m.envelopeTo[0].host + "; bouncing it\n" + m.summary());
62                 send(m.bounce("could not resolve " + m.envelopeTo[0].host));
63                 return true;
64             }
65             if (new Date().getTime() - m.arrival.getTime() > 1000 * 60 * 60 * 24 * 5) {
66                 Log.warn(SMTP.Outgoing.class, "could not send message after 5 days; bouncing it\n" + m.summary());
67                 send(m.bounce("could not send for 5 days"));
68                 return true;
69             }
70             for(int i=0; i<mx.length; i++) {
71                 if (deadHosts.contains(mx[i])) continue;
72                 if (attempt(m, mx[i])) { return true; }
73             }
74             return false;
75         }
76
77         private static void check(String s) throws IOException {
78             if (s.startsWith("4") || s.startsWith("5")) throw new IOException("SMTP Error: " + s); }
79         private static boolean attempt(Message m, InetAddress mx) {
80             try {
81                 String vhost = InetAddress.getLocalHost().getHostName();
82                 String cid = getConversation();
83                 PrintWriter logf = new PrintWriter(new OutputStreamWriter(new FileOutputStream(convdir+File.separatorChar+cid)));
84                 Log.setThreadAnnotation("[outgoing smtp: " + mx + " / " + cid + "] ");
85                 Log.info(SMTP.Outgoing.class, "connecting...");
86                 Socket s = new Socket(mx, 25);
87                 Log.info(SMTP.Outgoing.class, "connected");
88                 LineReader  r = new LoggedLineReader(new InputStreamReader(s.getInputStream()), logf);
89                 PrintWriter w = new LoggedPrintWriter(new OutputStreamWriter(s.getOutputStream()), logf);
90                                                                   check(r.readLine());  // banner
91                 w.print("HELO " + vhost + "\r\n");                check(r.readLine());
92                 w.print("MAIL FROM: " + m.envelopeFrom + "\r\n"); check(r.readLine());
93                 w.print("RCPT TO: " + m.envelopeTo + "\r\n");     check(r.readLine());
94                 w.print("DATA\r\n");                              check(r.readLine());
95                 w.print(m.body);
96                 w.print(".\r\n");
97                 check(r.readLine());
98                 Log.info(SMTP.Outgoing.class, "message accepted by " + mx);
99                 FileSystem.root.slash("outgoing").delete(m);
100                 s.close();
101                 return true;
102             } catch (Exception e) {
103                 Log.warn(SMTP.Outgoing.class, "unable to send; error=" + e);
104                 Log.warn(SMTP.Outgoing.class, e);
105                 return false;
106             } finally {
107                 Log.setThreadAnnotation("[outgoing smtp] ");
108             }
109         }
110
111         static void runq() {
112             try {
113                 Log.setThreadAnnotation("[outgoing smtp] ");
114                 int[] outgoing = FileSystem.root.slash("outgoing").list();
115                 Log.info(SMTP.Outgoing.class, "outgoing thread started; " + outgoing.length + " messages to send");
116                 for(int i=0; i<outgoing.length; i++) queue.append(FileSystem.root.slash("outgoing").get(outgoing[i]));
117                 while(true) {
118                     int num = queue.size();
119                     for(int i=0; i<num; i++) {
120                         Message next = (Message)queue.remove(true);
121                         boolean good = false;
122                         try {
123                             good = attempt(next);
124                         } catch (IOException e) {
125                             Log.error(SMTP.Outgoing.class, e);
126                         } finally {
127                             if (!good) queue.append(next);
128                         }
129                     }
130                     synchronized(Outgoing.class) {
131                         Log.info(SMTP.Outgoing.class, "outgoing thread going to sleep");
132                         Outgoing.class.wait(10 * 60 * 1000);
133                         deadHosts.clear();
134                         Log.info(SMTP.Outgoing.class, "outgoing thread woke up; " + queue.size() + " messages in queue");
135                     }
136                 }
137             } catch (Exception e) {
138                 Log.error(SMTP.Outgoing.class, e);
139             }
140         }
141     }
142
143     private static String lastTime = null;
144     private static int lastCounter = 0;
145
146     private class Listener extends Incoming /*implements ServerRequest*/ {
147         Socket conn;
148         String vhost;
149         public void init() { }
150         public Listener(Socket conn, String vhost) { this.vhost = vhost; this.conn = conn; }
151
152         //TcpConnection conn;
153         //public Listener(TcpConnection conn) { this.conn = conn; conn.getSocket().setSoTimeout(5 * 60 * 1000); }
154         public boolean handleRequest() {
155             try {
156                 conn.setSoTimeout(5 * 60 * 1000);
157                 StringBuffer logMessage = new StringBuffer();
158                 String conversationId = getConversation();
159                 Log.setThreadAnnotation("[conversation/" + conversationId + "] ");
160                 InetSocketAddress remote = (InetSocketAddress)conn.getRemoteSocketAddress();
161                 Log.info(this, "connection from " + remote.getHostName() + ":" + remote.getPort() +
162                          " (" + remote.getAddress() + ")");
163                 PrintWriter logf =
164                     new PrintWriter(new OutputStreamWriter(new FileOutputStream(convdir + File.separatorChar + conversationId)));
165                 try {
166                     return handleRequest(new LoggedLineReader(new InputStreamReader(conn.getInputStream()), logf),
167                                          new LoggedPrintWriter(new OutputStreamWriter(conn.getOutputStream()), logf));
168                 } catch(Throwable t) {
169                     Log.warn(this, t);
170                 } finally {
171                     logf.close();
172                     Log.setThreadAnnotation("");
173                 }
174             } catch (Exception e) {
175                 Log.error(this, e);
176             }
177             return false;
178         }
179         
180         public boolean handleRequest(LineReader rs, PrintWriter ws) throws IOException, MailException {
181             //ReadStream rs = conn.getReadStream();
182             //WriteStream ws = conn.getWriteStream();
183             //ws.setNewLineString("\r\n");
184             ws.println("220 " + vhost + " ESMTP " + this.getClass().getName());
185             Address from = null;
186             Vector to = new Vector();
187             while(true) {
188                 String command = rs.readLine();
189                 String c = command.toUpperCase();
190                 if (c.startsWith("HELO")) {
191                     ws.println("250 HELO " + vhost);
192                     from = null;
193                     to = new Vector();
194
195                 } else if (c.startsWith("EHLO")) {
196                     ws.println("250-" + vhost);
197                     ws.println("250-SIZE");
198                     ws.println("250 PIPELINING");
199                     from = null;
200                     to = new Vector();
201
202                 } else if (c.startsWith("RSET")) {
203                     from = null;
204                     to = new Vector();
205                     ws.println("250 reset ok");
206
207                 } else if (c.startsWith("MAIL FROM:")) {
208                     command = command.substring(10).trim();
209                     from = new Address(command);
210                     ws.println("250 " + from + " is syntactically correct");
211
212                 } else if (c.startsWith("RCPT TO:")) {
213                     if (from == null) {
214                         ws.println("503 MAIL FROM must precede RCPT TO");
215                         continue;
216                     }
217                     command = command.substring(9).trim();
218                     if(command.indexOf(' ') != -1) command = command.substring(0, command.indexOf(' '));
219                     Address addr = new Address(command);
220                     InetAddress[] mx = getMailExchangerIPs(addr.host);
221                     to.addElement(addr);
222                     if (((InetSocketAddress)conn.getRemoteSocketAddress()).getAddress().isLoopbackAddress()) {
223                         ws.println("250 you are connected locally, so I will let you send");
224                     } else {
225                         boolean good = false;
226                         for(int i=0; !good && i<mx.length; i++)
227                             if (NetworkInterface.getByInetAddress(mx[i]) != null)
228                                 good = true;
229                         if (!good) {
230                             ws.println("551 sorry, " + addr + " is not on this machine");
231                             return false;
232                         }
233                         ws.println("250 " + addr + " is on this machine; I will deliver it");
234                     }
235
236                 } else if (c.startsWith("DATA")) {
237                     if (from == null) { ws.println("503 MAIL FROM command must precede DATA"); continue; }
238                     if (to == null) { ws.println("503 RCPT TO command must precede DATA"); continue; }
239                     ws.println("354 Enter message, ending with \".\" on a line by itself");
240                     try {
241                         Address[] toArr = new Address[to.size()];
242                         to.copyInto(toArr);
243                         accept(new Message(from, toArr, new DotTerminatedLineReader(rs)));
244                         ws.println("250 message accepted");
245                     } catch (MailException.Malformed mfe) {   ws.println("501 " + mfe.toString());
246                     } catch (MailException.MailboxFull mbf) { ws.println("452 " + mbf);
247                     } catch (IOException ioe) {               ws.println("554 " + ioe.toString());
248                     }
249                     break;
250
251                 } else if (c.startsWith("HELP")) { ws.println("214 you are beyond help.  see a trained professional.");
252                 } else if (c.startsWith("VRFY")) { ws.println("252 We don't VRFY; proceed anyway");
253                 } else if (c.startsWith("EXPN")) { ws.println("550 EXPN not available");
254                 } else if (c.startsWith("NOOP")) { ws.println("250 OK");
255                 } else if (c.startsWith("QUIT")) { ws.println("221 " + vhost + " closing connection"); break;
256                 } else                           { ws.println("500 unrecognized command");
257                 }                    
258             
259             }
260             return false; // always tell resin to close the connection
261         }
262     }
263
264     private static class DotTerminatedLineReader extends LineReader {
265         private final LineReader r;
266         private boolean done = false;
267         public DotTerminatedLineReader(LineReader r) { super(null); this.r = r; }
268         public String readLine() throws IOException {
269             if (done) return null;
270             String s = r.readLine();
271             if (s.equals(".")) { done = true; return null; }
272             if (s.startsWith(".")) return s.substring(1);
273             return s;
274         }
275     }
276
277     public static InetAddress[] getMailExchangerIPs(String hostName) {
278         InetAddress[] ret;
279         try {
280             Hashtable env = new Hashtable();
281             env.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");
282             DirContext ictx = new InitialDirContext(env);
283             Attributes attrs = ictx.getAttributes(hostName, new String[] { "MX" });
284             Attribute attr = attrs.get("MX");
285             if (attr == null) {
286                 ret = new InetAddress[1];
287                 try {
288                     ret[0] = InetAddress.getByName(hostName);
289                     return ret;
290                 } catch (UnknownHostException uhe) {
291                     Log.warn(SMTP.class, "no MX hosts or A record for " + hostName);
292                     return new InetAddress[0];
293                 }
294             } else {
295                 ret = new InetAddress[attr.size()];
296                 NamingEnumeration ne = attr.getAll();
297                 for(int i=0; ne.hasMore(); i++) ret[i] = (InetAddress)ne.next();
298             }
299         } catch (Exception e) {
300             Log.warn(SMTP.class, "couldn't find MX host for " + hostName + " due to");
301             Log.warn(SMTP.class, e);
302             return new InetAddress[0];
303         }
304         return ret;
305     }
306
307     private static class LoggedLineReader extends LineReader {
308         PrintWriter log;
309         public LoggedLineReader(Reader r, PrintWriter log) { super(r); this.log = log; }
310         public String readLine() throws IOException {
311             String s = super.readLine();
312             if (s != null) { log.println("C: " + s); log.flush(); }
313             return s;
314         }
315     }
316
317     private static class LoggedPrintWriter extends PrintWriter {
318         PrintWriter log;
319         public LoggedPrintWriter(Writer w, PrintWriter log) { super(w); this.log = log; }
320         public void println(String s) {
321             log.println("S: " + s);
322             super.println(s);
323             flush();
324         }
325     }
326
327     static String getConversation() {
328         String time = new SimpleDateFormat("yy.MMM.dd-hh:mm:ss").format(new Date());
329         synchronized (SMTP.class) {
330             if (lastTime != null && lastTime.equals(time)) {
331                 time += "." + (++lastCounter);
332             } else {
333                 lastTime = time;
334             }
335         }
336         return time;
337     }
338
339 }