rename MailboxTree -> MailTree
[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 // restrict the maximum number of open connections from any given source IP (probably max 4)
20 public class Main {
21
22     public static final ThreadPool threadPool = new ThreadPool(10);
23     public static final Cron       cron       = new Cron(threadPool);
24
25     public static void main(String[] s) throws Exception {
26         try {
27             File f = new File(Mailbox.STORAGE_ROOT + "/restart");
28             if (f.exists()) f.delete();
29         } catch (Exception e) { Log.error(Main.class, e); }
30         new Main().main();
31     }
32
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);
50
51         cron.executeLater(500, new Runnable() {
52             public void run() {
53                 try {
54                     File f = new File(Mailbox.STORAGE_ROOT + "/restart");
55                     if (f.exists()) {
56                         Log.error("RESTART", "restarting...");
57                         System.exit(0);
58                     }
59                 } catch (Exception e) { Log.error(this, e); }
60                 cron.executeLater(500, this);
61             } });
62             
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();
72     }
73
74     private class Acceptor extends Thread {
75         private ServerSocket ss;
76         public Acceptor(ServerSocket ss) { this.ss = ss; }
77         public void run() {
78             while(true) {
79                 try {
80                     ss.setReuseAddress(true);
81                     final Socket s = ss.accept();
82                     threadPool.start(new Runnable() {
83                         public void run() {
84                             try {
85                                 accept(new Connection(s, "megacz.com"));
86                             } catch (Throwable t) { Log.error(Main.class, t); }
87                         }
88                     });
89                 } catch (Throwable t) {
90                     Log.error(Main.class, t);
91                     Misc.sleep(1000);
92                 }
93             }
94         }
95     }
96
97     public void accept(Connection conn) {
98         try {
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) {
113             Log.error(this, e);
114         } finally {
115             conn.close();
116         }
117     }
118
119     static final Auth auth = new Auth();
120     static class Auth implements Login {
121         private KerberosAuth ka = new KerberosAuth("MEGACZ.COM", "chaitin.megacz.com");
122         public Account anonymous() {
123             try {
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);
130                     }
131                 };
132             } catch (Exception e) { throw new RuntimeException(e); }
133         }
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);
144                     }
145                 };
146         }
147     }
148
149 }