X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=src%2Forg%2Fibex%2Fmail%2FMain.java;h=4d0659b59a9960dbb3d3a4c8bfe6ec0e0ed16ad5;hb=40e53cd3c7abe25902add4c97b8bc9202f3c8931;hp=8bd9da6c454af9d098fd33313d5a2b969ed43fa3;hpb=622e62c20bd8993133a0eba21cfb1bc7e253cf07;p=org.ibex.mail.git diff --git a/src/org/ibex/mail/Main.java b/src/org/ibex/mail/Main.java index 8bd9da6..4d0659b 100644 --- a/src/org/ibex/mail/Main.java +++ b/src/org/ibex/mail/Main.java @@ -1,84 +1,149 @@ +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + package org.ibex.mail; -import org.ibex.mail.target.*; -import org.ibex.mail.protocol.*; import org.ibex.util.*; +import org.ibex.jetty.*; +import org.ibex.io.*; +import org.ibex.net.*; import java.io.*; import java.net.*; import java.util.*; +import org.ibex.crypto.*; +import org.ibex.jetty.*; +import javax.net.ssl.SSLServerSocket; +import javax.net.ssl.SSLServerSocketFactory; +import javax.net.ssl.SSLSocket; +// restrict the maximum number of open connections from any given source IP (probably max 4) public class Main { - public static void main(String[] s) throws Exception { - - // set up logging - String logto = System.getProperty("ibex.mail.root", File.separatorChar + "var" + File.separatorChar + "org.ibex.mail"); - logto += File.separatorChar + "log"; + public static final ThreadPool threadPool = new ThreadPool(10); + public static final Cron cron = new Cron(threadPool); - //Log.file(logto); - Log.color = true; + public static void main(String[] s) throws Exception { + try { + File f = new File(Mailbox.STORAGE_ROOT + "/restart"); + if (f.exists()) f.delete(); + } catch (Exception e) { Log.error(Main.class, e); } + new Main().main(); + } - new IMAPThread().start(); - new SMTPThread().start(); + public void main() throws Exception { + if (System.getProperty("javax.net.ssl.keyStore")==null) + System.setProperty("javax.net.ssl.keyStore", Mailbox.STORAGE_ROOT+"/conf/ssl/ssl.keystore"); + if (System.getProperty("javax.net.ssl.keyStorePassword")==null) + System.setProperty("javax.net.ssl.keyStorePassword", "changeit"); + SSLServerSocketFactory sslserversocketfactory = + (SSLServerSocketFactory) SSLServerSocketFactory.getDefault(); + ServerSocket sslserversocket = (ServerSocket) sslserversocketfactory.createServerSocket(9999); + ServerSocket sock8025 = new ServerSocket(8025); + ServerSocket sock143 = new ServerSocket(143); + ServerSocket sock119 = new ServerSocket(119); + ServerSocket sock25 = new ServerSocket(25); + ServerSocket sock465 = (ServerSocket) sslserversocketfactory.createServerSocket(465); + ServerSocket sock993 = (ServerSocket) sslserversocketfactory.createServerSocket(993); + ServerSocket sock563 = (ServerSocket) sslserversocketfactory.createServerSocket(563); + //ServerSocket sock995 = new ServerSocket(995); + //ServerSocket sock113 = new ServerSocket(113); + cron.executeLater(500, new Runnable() { + public void run() { + try { + File f = new File(Mailbox.STORAGE_ROOT + "/restart"); + if (f.exists()) { + Log.error("RESTART", "restarting..."); + System.exit(0); + } + } catch (Exception e) { Log.error(this, e); } + cron.executeLater(500, this); + } }); + + new Acceptor(sock143).start(); + new Acceptor(sock119).start(); + new Acceptor(sock25).start(); + new Acceptor(sock465).start(); + new Acceptor(sock8025).start(); + new Acceptor(sock993).start(); + new Acceptor(sock563).start(); + //new Acceptor(sock995).start(); + //new Acceptor(sock113).start(); } - private static class BogusAuthenticator implements IMAP.Server.Authenticator { - final Mailbox root = - FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT, true).slash("user", true).slash("megacz", true); - public Mailbox authenticate(String u, String p) { - if (u.equals("megacz") && p.equals("pass")) return root; - return null; - } - } - - private static class IMAPThread extends Thread { - final int port = Integer.parseInt(System.getProperty("ibex.mail.imap.port", "143")); + private class Acceptor extends Thread { + private ServerSocket ss; + public Acceptor(ServerSocket ss) { this.ss = ss; } public void run() { - try { - Log.info(this, "binding to port " + port + "..."); - ServerSocket ss = new ServerSocket(port); - Log.info(this, "listening for connections..."); - for(;;) { - final Socket sock = ss.accept(); - new Thread() { + while(true) { + try { + ss.setReuseAddress(true); + final Socket s = ss.accept(); + threadPool.start(new Runnable() { public void run() { try { - new IMAP.Listener(sock, "megacz.com", new BogusAuthenticator()).handle(); - } catch (Exception e) { - Log.warn(this, e); - } + accept(new Connection(s, "megacz.com")); + } catch (Throwable t) { Log.error(Main.class, t); } } - }.start(); + }); + } catch (Throwable t) { + Log.error(Main.class, t); + Misc.sleep(1000); } - } catch (Exception e) { - Log.warn(this, e); } } } - private static class SMTPThread extends Thread { - final int port = Integer.parseInt(System.getProperty("ibex.mail.smtp.port", "25")); - public void run() { + public void accept(Connection conn) { + try { + if (conn.getLocalPort() == 143) new IMAP.Listener(auth).handleRequest(conn); + else if (conn.getLocalPort() == 993) new IMAP.Listener(auth).handleRequest(conn); + else if (conn.getLocalPort() == 119) new NNTP.Listener(auth).handleRequest(conn); + else if (conn.getLocalPort() == 563) new NNTP.Listener(auth).handleRequest(conn); + else if (conn.getLocalPort() == 25) new SMTP.Server().handleRequest(conn); + else if (conn.getLocalPort() == 465) new SMTP.Server().handleRequest(conn); + else if (conn.getLocalPort() == 465) new SMTP.Server().handleRequest(conn); + else if (conn.getLocalPort() == 8025) SMTP.whitelist.handleRequest(conn); + //else if (conn.getLocalPort() == 110) new POP3.Listener(auth).handleRequest(conn); + //else if (conn.getLocalPort() == 995) new POP3.Listener(auth).handleRequest(conn); + //else if (conn.getLocalPort() == 8099) GMail.handleRequest(conn); + } catch (Stream.Closed c) { + Log.error(this, "connection abruptly closed by client"); + } catch (IOException e) { + Log.error(this, e); + } finally { + conn.close(); + } + } + + static final Auth auth = new Auth(); + static class Auth implements Login { + private KerberosAuth ka = new KerberosAuth("MEGACZ.COM", "chaitin.megacz.com"); + public Account anonymous() { try { - Log.info(this, "binding to port " + port + "..."); - ServerSocket ss = new ServerSocket(port); - Log.info(this, "listening for connections..."); - while(true) { - final Socket sock = ss.accept(); - final SMTP.Server smtp = new SMTP.Server(sock, "megacz.com"); - new Thread() { - public void run() { - try { - smtp.handle(); - } catch (Exception e) { - Log.warn(this, e); - } - } - }.start(); - } - } catch (Exception e) { - Log.warn(this, e); - } + final MailTree root = + FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/list", false); + if (root==null) return null; + return new Account("anonymous", null, root) { + public MailTree getMailbox(Class protocol) { + return super.getMailbox(protocol); + } + }; + } catch (Exception e) { throw new RuntimeException(e); } + } + public Object login(String user, String pass, Class protocol) { return login(user, pass); } + public Account login(String user, String pass) { + //if (!EtcPasswd.verify(user, pass)) return null; + //if (!CheckPassword.verify(user, pass)) return null; + if (!ka.auth(user, pass)) return null; + final MailTree root = + FileBasedMailbox.getFileBasedMailbox(Mailbox.STORAGE_ROOT + "/user", true); + return new Account(user, null, root.slash(user, true)){ + public MailTree getMailbox(Class protocol) { + return super.getMailbox(protocol); + } + }; } } + }