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.
6 import org.ibex.util.*;
7 import org.ibex.jetty.*;
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;
19 // restrict the maximum number of open connections from any given source IP (probably max 4)
22 public static final ThreadPool threadPool = new ThreadPool(10);
23 public static final Cron cron = new Cron(threadPool);
25 public static void main(String[] s) throws Exception {
27 File f = new File(Mailbox.STORAGE_ROOT + "/restart");
28 if (f.exists()) f.delete();
29 } catch (Exception e) { Log.error(Main.class, e); }
33 public void main() throws Exception {
34 if (System.getProperty("javax.net.ssl.keyStore")==null)
35 System.setProperty("javax.net.ssl.keyStore", Mailbox.STORAGE_ROOT+"/conf/ssl/ssl.keystore");
36 if (System.getProperty("javax.net.ssl.keyStorePassword")==null)
37 System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
38 SSLServerSocketFactory sslserversocketfactory =
39 (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
40 ServerSocket sslserversocket = (ServerSocket) sslserversocketfactory.createServerSocket(9999);
41 ServerSocket sock8025 = new ServerSocket(8025);
42 ServerSocket sock143 = new ServerSocket(143);
43 ServerSocket sock119 = new ServerSocket(119);
44 ServerSocket sock25 = new ServerSocket(25);
45 ServerSocket sock465 = (ServerSocket) sslserversocketfactory.createServerSocket(465);
46 ServerSocket sock993 = (ServerSocket) sslserversocketfactory.createServerSocket(993);
47 ServerSocket sock563 = (ServerSocket) sslserversocketfactory.createServerSocket(563);
48 //ServerSocket sock995 = new ServerSocket(995);
49 //ServerSocket sock113 = new ServerSocket(113);
51 cron.executeLater(500, new Runnable() {
54 File f = new File(Mailbox.STORAGE_ROOT + "/restart");
56 Log.error("RESTART", "restarting...");
59 } catch (Exception e) { Log.error(this, e); }
60 cron.executeLater(500, this);
63 new Acceptor(sock143).start();
64 new Acceptor(sock119).start();
65 new Acceptor(sock25).start();
66 new Acceptor(sock465).start();
67 new Acceptor(sock8025).start();
68 new Acceptor(sock993).start();
69 new Acceptor(sock563).start();
70 //new Acceptor(sock995).start();
71 //new Acceptor(sock113).start();
74 private class Acceptor extends Thread {
75 private ServerSocket ss;
76 public Acceptor(ServerSocket ss) { this.ss = ss; }
80 ss.setReuseAddress(true);
81 final Socket s = ss.accept();
82 threadPool.start(new Runnable() {
85 accept(new Connection(s, "megacz.com"));
86 } catch (Throwable t) { Log.error(Main.class, t); }
89 } catch (Throwable t) {
90 Log.error(Main.class, t);
97 public void accept(Connection conn) {
99 if (conn.getLocalPort() == 143) new IMAP.Listener(auth).handleRequest(conn);
100 else if (conn.getLocalPort() == 993) new IMAP.Listener(auth).handleRequest(conn);
101 else if (conn.getLocalPort() == 119) new NNTP.Listener(auth).handleRequest(conn);
102 else if (conn.getLocalPort() == 563) new NNTP.Listener(auth).handleRequest(conn);
103 else if (conn.getLocalPort() == 25) new SMTP.Server().handleRequest(conn);
104 else if (conn.getLocalPort() == 465) new SMTP.Server().handleRequest(conn);
105 else if (conn.getLocalPort() == 465) new SMTP.Server().handleRequest(conn);
106 else if (conn.getLocalPort() == 8025) SMTP.whitelist.handleRequest(conn);
107 //else if (conn.getLocalPort() == 110) new POP3.Listener(auth).handleRequest(conn);
108 //else if (conn.getLocalPort() == 995) new POP3.Listener(auth).handleRequest(conn);
109 //else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn);
110 } catch (Stream.Closed c) {
111 Log.error(this, "connection abruptly closed by client");
112 } catch (IOException e) {
119 static final Auth auth = new Auth();
120 static class Auth implements Login {
121 private KerberosAuth ka = new KerberosAuth("MEGACZ.COM", "gentzen.megacz.com");
122 public Account anonymous() {
124 final MailTree root =
125 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/list", false);
126 if (root==null) return null;
127 return new Account("anonymous", null, root) {
128 public MailTree getMailbox(Class protocol) {
129 return super.getMailbox(protocol);
132 } catch (Exception e) { throw new RuntimeException(e); }
134 public Object login(String user, String pass, Class protocol) { return login(user, pass); }
135 public Account login(String user, String pass) {
136 //if (!EtcPasswd.verify(user, pass)) return null;
137 //if (!CheckPassword.verify(user, pass)) return null;
138 if (!ka.auth(user, pass)) return null;
139 final MailTree root =
140 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true);
141 return new Account(user, null, root.slash(user, true)){
142 public MailTree getMailbox(Class protocol) {
143 return super.getMailbox(protocol);