b11584c7c57274c10b30d983208fe1eb96b4b950
[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 void main(String[] s) throws Exception {
23         try {
24             File f = new File(Mailbox.STORAGE_ROOT + "/restart");
25             if (f.exists()) f.delete();
26         } catch (Exception e) { Log.error(Main.class, e); }
27         new Main().main();
28     }
29
30     public void main() throws Exception {
31         if (System.getProperty("javax.net.ssl.keyStore")==null)
32             System.setProperty("javax.net.ssl.keyStore", Mailbox.STORAGE_ROOT+"/conf/ssl/ssl.keystore");
33         if (System.getProperty("javax.net.ssl.keyStorePassword")==null)
34             System.setProperty("javax.net.ssl.keyStorePassword", "changeit");
35         SSLServerSocketFactory sslserversocketfactory =
36             (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
37         ServerSocket sslserversocket = (ServerSocket) sslserversocketfactory.createServerSocket(9999);
38         ServerSocket sock8025 = new ServerSocket(8025);
39         ServerSocket sock143  = new ServerSocket(143);
40         ServerSocket sock119  = new ServerSocket(119);
41         ServerSocket sock25   = new ServerSocket(25);
42         ServerSocket sock465  = (ServerSocket) sslserversocketfactory.createServerSocket(465);
43         ServerSocket sock993  = (ServerSocket) sslserversocketfactory.createServerSocket(993);
44         ServerSocket sock563  = (ServerSocket) sslserversocketfactory.createServerSocket(563);
45         //ServerSocket sock995  = new ServerSocket(995);
46         //ServerSocket sock113  = new ServerSocket(113);
47         new Thread() {
48             public void run() {
49                 while(true) {
50                     try { Thread.sleep(500); } catch (Exception e) { }
51                     try {
52                         File f = new File(Mailbox.STORAGE_ROOT + "/restart");
53                         if (f.exists()) {
54                             Log.error("RESTART", "restarting...");
55                             System.exit(0);
56                         }
57                     } catch (Exception e) { Log.error(this, e); }
58                 }
59             }
60         }.start();
61         new Acceptor(sock143).start();
62         new Acceptor(sock119).start();
63         new Acceptor(sock25).start();
64         new Acceptor(sock465).start();
65         new Acceptor(sock8025).start();
66         new Acceptor(sock993).start();
67         new Acceptor(sock563).start();
68         //new Acceptor(sock995).start();
69         //new Acceptor(sock113).start();
70     }
71
72     private class Acceptor extends Thread {
73         private ServerSocket ss;
74         public Acceptor(ServerSocket ss) { this.ss = ss; }
75         public void run() {
76             while(true) {
77                 try {
78                     ss.setReuseAddress(true);
79                     final Socket s = ss.accept();
80                     new Thread() {
81                         public void run() {
82                             try {
83                                 accept(new Connection(s, "megacz.com"));
84                             } catch (Throwable t) { Log.error(Main.class, t); }
85                         }
86                     }.start();
87                 } catch (Throwable t) {
88                     Log.error(Main.class, t);
89                     Misc.sleep(1000);
90                 }
91             }
92         }
93     }
94
95     public void accept(Connection conn) {
96         try {
97             if      (conn.getLocalPort() == 143)  new IMAP.Listener(auth).handleRequest(conn);
98             else if (conn.getLocalPort() == 993)  new IMAP.Listener(auth).handleRequest(conn);
99             else if (conn.getLocalPort() == 119)  new NNTP.Listener(auth).handleRequest(conn);
100             else if (conn.getLocalPort() == 563)  new NNTP.Listener(auth).handleRequest(conn);
101             else if (conn.getLocalPort() == 25)   new SMTP.Server().handleRequest(conn);
102             else if (conn.getLocalPort() == 465)  new SMTP.Server().handleRequest(conn);
103             else if (conn.getLocalPort() == 465)  new SMTP.Server().handleRequest(conn);
104             else if (conn.getLocalPort() == 8025) SMTP.whitelist.handleRequest(conn);
105             //else if (conn.getLocalPort() == 110)  new POP3.Listener(auth).handleRequest(conn);
106             //else if (conn.getLocalPort() == 995)  new POP3.Listener(auth).handleRequest(conn);
107             //else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn);
108         } catch (Stream.Closed c) {
109             Log.error(this, "connection abruptly closed by client");
110         } catch (IOException e) {
111             Log.error(this, e);
112         } finally {
113             conn.close();
114         }
115     }
116
117     static final Auth auth = new Auth();
118     static class Auth implements Login {
119         private KerberosAuth ka = new KerberosAuth("MEGACZ.COM", "chaitin.megacz.com");
120         public Account anonymous() {
121             try {
122                 final Mailbox root =
123                     FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/list", false);
124                 if (root==null) return null;
125                 return new Account("anonymous", null, root){
126                     public Mailbox getMailbox(Class protocol) {
127                         return super.getMailbox(protocol);
128                     }
129                 };
130             } catch (Exception e) { throw new RuntimeException(e); }
131         }
132         public Object login(String user, String pass, Class protocol) { return login(user, pass); }
133         public Account login(String user, String pass) {
134             //if (!EtcPasswd.verify(user, pass)) return null;
135             //if (!CheckPassword.verify(user, pass)) return null;
136             if (!ka.auth(user, pass)) return null;
137             final Mailbox root =
138                 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true);
139             return new Account(user, null, root.slash(user, true)){
140                     public Mailbox getMailbox(Class protocol) {
141                         return super.getMailbox(protocol);
142                     }
143                 };
144         }
145     }
146
147 }