8bfa8c1b059845b50d9c19c76d572300f6563c85
[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.mail.target.*;
7 import org.ibex.mail.protocol.*;
8 import org.ibex.util.*;
9 import org.ibex.jetty.*;
10 import org.ibex.io.*;
11 import java.io.*;
12 import java.net.*;
13 import java.util.*;
14 import org.ibex.crypto.*;
15 import org.ibex.jetty.*;
16 import javax.net.ssl.SSLServerSocket;
17 import javax.net.ssl.SSLServerSocketFactory;
18 import javax.net.ssl.SSLSocket;
19
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 =
38             (ServerSocket) sslserversocketfactory.createServerSocket(9999);
39         ServerSocket sock143  = new ServerSocket(143);
40         ServerSocket sock119  = new ServerSocket(119);
41         ServerSocket sock25   = new ServerSocket(25);
42         ServerSocket sock8025 = new ServerSocket(8025);
43         ServerSocket sock993  = new ServerSocket(993);
44         ServerSocket sock563  = new ServerSocket(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(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                 while(true) {
77                     final Socket s = ss.accept();
78                     try {
79                         new Thread() {
80                             public void run() {
81                                 try {
82                                     accept(new Connection(s, "megacz.com"));
83                                 } catch (Exception e) { Log.error(Main.class, e); }
84                             }
85                         }.start();
86                     } catch (Exception e) { Log.error(Main.class, e); }
87                 }
88             } catch (Exception e) { Log.error(Main.class, e); }
89         }
90     }
91
92     public void accept(Connection conn) {
93         try {
94             if      (conn.getLocalPort() == 143)  new IMAP.Listener(auth).handleRequest(conn);
95             else if (conn.getLocalPort() == 993)  new IMAP.Listener(auth).handleRequest(conn);
96             else if (conn.getLocalPort() == 119)  new NNTP.Listener(auth).handleRequest(conn);
97             else if (conn.getLocalPort() == 563)  new NNTP.Listener(auth).handleRequest(conn);
98             else if (conn.getLocalPort() == 25)   new SMTP.Server().handleRequest(conn);
99             else if (conn.getLocalPort() == 8025) SMTP.whitelist.handleRequest(conn);
100             //else if (conn.getLocalPort() == 110)  new POP3.Listener(auth).handleRequest(conn);
101             //else if (conn.getLocalPort() == 995)  new POP3.Listener(auth).handleRequest(conn);
102             //else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn);
103         } catch (Stream.Closed c) {
104             Log.error(this, "connection abruptly closed by client");
105         } catch (IOException e) {
106             Log.error(this, e);
107         } finally {
108             conn.close();
109         }
110     }
111
112     private static final Auth auth = new Auth();
113     private static class Auth implements Login {
114         private KerberosAuth ka = new KerberosAuth("MEGACZ.COM", "godel.megacz.com");
115         public Account anonymous() {
116             try {
117                 final Mailbox root =
118                     FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/list", false);
119                 if (root==null) return null;
120                 return new Account("anonymous", null, root){
121                     public Mailbox getMailbox(Class protocol) {
122                         return super.getMailbox(protocol);
123                     }
124                 };
125             } catch (Exception e) { throw new RuntimeException(e); }
126         }
127         public Object login(String user, String pass, Class protocol) { return login(user, pass); }
128         public Account login(String user, String pass) {
129             if (!EtcPasswd.verify(user, pass)) return null;
130
131             // currently broken, but should be used
132             //if (!ka.auth(user, pass)) return null;
133
134             final Mailbox root =
135                 FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true);
136             return new Account(user, null, root.slash(user, true)){
137                     public Mailbox getMailbox(Class protocol) {
138                         return super.getMailbox(protocol);
139                     }
140                 };
141         }
142     }
143
144 }