more robustness in Main
[org.ibex.mail.git] / src / org / ibex / mail / Main.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.mail;
6 import org.ibex.util.*;
7 import org.ibex.jetty.*;
8 import org.ibex.io.*;
9 import org.ibex.net.*;
10 import java.io.*;
11 import java.net.*;
12 import java.util.*;
13 import org.ibex.crypto.*;
14 import org.ibex.jetty.*;
15 import javax.net.ssl.SSLServerSocket;
16 import javax.net.ssl.SSLServerSocketFactory;
17 import javax.net.ssl.SSLSocket;
18
19 public class Main {
20
21     public static void main(String[] s) throws Exception {
22         try {
23             File f = new File(Mailbox.STORAGE_ROOT + "/restart");
24             if (f.exists()) f.delete();
25         } catch (Exception e) { Log.error(Main.class, e); }
26         new Main().main();
27     }
28
29     public void main() throws Exception {
30         if (System.getProperty("javax.net.ssl.keyStore")==null)
31             System.setProperty("javax.net.ssl.keyStore", Mailbox.STORAGE_ROOT+"/conf/ssl/ssl.keystore");
32         if (System.getProperty("javax.net.ssl.keyStorePassword")==null)
33             System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
34         SSLServerSocketFactory sslserversocketfactory =
35             (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
36         ServerSocket sslserversocket = (ServerSocket) sslserversocketfactory.createServerSocket(9999);
37         ServerSocket sock8025 = new ServerSocket(8025);
38         ServerSocket sock143  = new ServerSocket(143);
39         ServerSocket sock119  = new ServerSocket(119);
40         ServerSocket sock25   = new ServerSocket(25);
41         ServerSocket sock465  = (ServerSocket) sslserversocketfactory.createServerSocket(465);
42         ServerSocket sock993  = (ServerSocket) sslserversocketfactory.createServerSocket(993);
43         ServerSocket sock563  = (ServerSocket) sslserversocketfactory.createServerSocket(563);
44         //ServerSocket sock995  = new ServerSocket(995);
45         //ServerSocket sock113  = new ServerSocket(113);
46         new Thread() {
47             public void run() {
48                 while(true) {
49                     try { Thread.sleep(500); } catch (Exception e) { }
50                     try {
51                         File f = new File(Mailbox.STORAGE_ROOT + "/restart");
52                         if (f.exists()) {
53                             Log.error("RESTART", "restarting...");
54                             System.exit(0);
55                         }
56                     } catch (Exception e) { Log.error(this, e); }
57                 }
58             }
59         }.start();
60         new Acceptor(sock143).start();
61         new Acceptor(sock119).start();
62         new Acceptor(sock25).start();
63         new Acceptor(sock465).start();
64         new Acceptor(sock8025).start();
65         new Acceptor(sock993).start();
66         new Acceptor(sock563).start();
67         //new Acceptor(sock995).start();
68         //new Acceptor(sock113).start();
69     }
70
71     private class Acceptor extends Thread {
72         private ServerSocket ss;
73         public Acceptor(ServerSocket ss) { this.ss = ss; }
74         public void run() {
75             try {
76                 ss.setReuseAddress(true);
77
78                 while(true) {
79                     final Socket s = ss.accept();
80                     try {
81                         new Thread() {
82                             public void run() {
83                                 try {
84                                     accept(new Connection(s, "megacz.com"));
85                                 } catch (Exception e) { Log.error(Main.class, e); }
86                             }
87                         }.start();
88                     } catch (Exception e) { Log.error(Main.class, e); }
89                 }
90             } catch (Exception e) { Log.error(Main.class, e); }
91         }
92     }
93
94     public void accept(Connection conn) {
95         try {
96             if      (conn.getLocalPort() == 143)  new IMAP.Listener(auth).handleRequest(conn);
97             else if (conn.getLocalPort() == 993)  new IMAP.Listener(auth).handleRequest(conn);
98             else if (conn.getLocalPort() == 119)  new NNTP.Listener(auth).handleRequest(conn);
99             else if (conn.getLocalPort() == 563)  new NNTP.Listener(auth).handleRequest(conn);
100             else if (conn.getLocalPort() == 25)   new SMTP.Server().handleRequest(conn);
101             else if (conn.getLocalPort() == 465)  new SMTP.Server().handleRequest(conn);
102             else if (conn.getLocalPort() == 465)  new SMTP.Server().handleRequest(conn);
103             else if (conn.getLocalPort() == 8025) SMTP.whitelist.handleRequest(conn);
104             //else if (conn.getLocalPort() == 110)  new POP3.Listener(auth).handleRequest(conn);
105             //else if (conn.getLocalPort() == 995)  new POP3.Listener(auth).handleRequest(conn);
106             //else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn);
107         } catch (Stream.Closed c) {
108             Log.error(this, "connection abruptly closed by client");
109         } catch (IOException e) {
110             Log.error(this, e);
111         } finally {
112             conn.close();
113         }
114     }
115
116     private static final Auth auth = new Auth();
117     private static class Auth implements Login {
118         private KerberosAuth ka = new KerberosAuth("MEGACZ.COM", "godel.megacz.com");
119         public Account anonymous() {
120             try {
121                 final Mailbox root =
122                     FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/list", false);
123                 if (root==null) return null;
124                 return new Account("anonymous", null, root){
125                     public Mailbox getMailbox(Class protocol) {
126                         return super.getMailbox(protocol);
127                     }
128                 };
129             } catch (Exception e) { throw new RuntimeException(e); }
130         }
131         public Object login(String user, String pass, Class protocol) { return login(user, pass); }
132         public Account login(String user, String pass) {
133             if (!EtcPasswd.verify(user, pass)) return null;
134
135             // currently broken, but should be used
136             //if (!ka.auth(user, pass)) return null;
137
138             final Mailbox root =
139                 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true);
140             return new Account(user, null, root.slash(user, true)){
141                     public Mailbox getMailbox(Class protocol) {
142                         return super.getMailbox(protocol);
143                     }
144                 };
145         }
146     }
147
148 }