]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Launcher.java
Merge branch 'SMIME'
[gigi.git] / src / org / cacert / gigi / Launcher.java
1 package org.cacert.gigi;
2
3 import java.io.IOException;
4 import java.security.GeneralSecurityException;
5 import java.security.Key;
6 import java.security.KeyStore;
7 import java.security.KeyStoreException;
8 import java.security.NoSuchAlgorithmException;
9 import java.security.UnrecoverableKeyException;
10 import java.security.cert.Certificate;
11 import java.util.List;
12 import java.util.Properties;
13
14 import javax.net.ssl.ExtendedSSLSession;
15 import javax.net.ssl.SNIHostName;
16 import javax.net.ssl.SNIServerName;
17 import javax.net.ssl.SSLEngine;
18 import javax.net.ssl.SSLParameters;
19 import javax.net.ssl.SSLSession;
20
21 import org.cacert.gigi.api.GigiAPI;
22 import org.cacert.gigi.email.EmailProvider;
23 import org.cacert.gigi.natives.SetUID;
24 import org.cacert.gigi.util.CipherInfo;
25 import org.cacert.gigi.util.ServerConstants;
26 import org.eclipse.jetty.http.HttpVersion;
27 import org.eclipse.jetty.server.Connector;
28 import org.eclipse.jetty.server.Handler;
29 import org.eclipse.jetty.server.HttpConfiguration;
30 import org.eclipse.jetty.server.HttpConnectionFactory;
31 import org.eclipse.jetty.server.SecureRequestCustomizer;
32 import org.eclipse.jetty.server.Server;
33 import org.eclipse.jetty.server.ServerConnector;
34 import org.eclipse.jetty.server.SessionManager;
35 import org.eclipse.jetty.server.SslConnectionFactory;
36 import org.eclipse.jetty.server.handler.ContextHandler;
37 import org.eclipse.jetty.server.handler.HandlerList;
38 import org.eclipse.jetty.server.handler.HandlerWrapper;
39 import org.eclipse.jetty.server.handler.ResourceHandler;
40 import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
41 import org.eclipse.jetty.servlet.ServletContextHandler;
42 import org.eclipse.jetty.servlet.ServletHolder;
43 import org.eclipse.jetty.util.log.Log;
44 import org.eclipse.jetty.util.ssl.SslContextFactory;
45
46 public class Launcher {
47
48     public static void main(String[] args) throws Exception {
49         GigiConfig conf = GigiConfig.parse(System.in);
50         ServerConstants.init(conf.getMainProps());
51         initEmails(conf);
52
53         Server s = new Server();
54         // === SSL HTTP Configuration ===
55         HttpConfiguration https_config = new HttpConfiguration();
56         https_config.setSendServerVersion(false);
57         https_config.setSendXPoweredBy(false);
58
59         // for client-cert auth
60         https_config.addCustomizer(new SecureRequestCustomizer());
61
62         ServerConnector connector = new ServerConnector(s, createConnectionFactory(conf), new HttpConnectionFactory(https_config));
63         connector.setHost(conf.getMainProps().getProperty("host"));
64         connector.setPort(Integer.parseInt(conf.getMainProps().getProperty("port")));
65         s.setConnectors(new Connector[] {
66             connector
67         });
68
69         HandlerList hl = new HandlerList();
70         hl.setHandlers(new Handler[] {
71                 generateStaticContext(), generateGigiContexts(conf.getMainProps()), generateAPIContext()
72         });
73         s.setHandler(hl);
74         s.start();
75         if (connector.getPort() <= 1024 && !System.getProperty("os.name").toLowerCase().contains("win")) {
76             SetUID uid = new SetUID();
77             if ( !uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
78                 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
79             }
80         }
81     }
82
83     private static void initEmails(GigiConfig conf) throws GeneralSecurityException, IOException, KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException {
84         KeyStore privateStore = conf.getPrivateStore();
85         Certificate mail = privateStore.getCertificate("mail");
86         Key k = privateStore.getKey("mail", conf.getPrivateStorePw().toCharArray());
87         EmailProvider.initSystem(conf.getMainProps(), mail, k);
88     }
89
90     private static SslConnectionFactory createConnectionFactory(GigiConfig conf) throws GeneralSecurityException, IOException {
91         final SslContextFactory sslContextFactory = generateSSLContextFactory(conf, "www");
92         final SslContextFactory secureContextFactory = generateSSLContextFactory(conf, "secure");
93         secureContextFactory.setWantClientAuth(true);
94         secureContextFactory.setNeedClientAuth(false);
95         final SslContextFactory staticContextFactory = generateSSLContextFactory(conf, "static");
96         final SslContextFactory apiContextFactory = generateSSLContextFactory(conf, "api");
97         try {
98             secureContextFactory.start();
99             staticContextFactory.start();
100             apiContextFactory.start();
101         } catch (Exception e) {
102             e.printStackTrace();
103         }
104         return new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()) {
105
106             @Override
107             public boolean shouldRestartSSL() {
108                 return true;
109             }
110
111             @Override
112             public SSLEngine restartSSL(SSLSession sslSession) {
113                 SSLEngine e2 = null;
114                 if (sslSession instanceof ExtendedSSLSession) {
115                     ExtendedSSLSession es = (ExtendedSSLSession) sslSession;
116                     List<SNIServerName> names = es.getRequestedServerNames();
117                     for (SNIServerName sniServerName : names) {
118                         if (sniServerName instanceof SNIHostName) {
119                             SNIHostName host = (SNIHostName) sniServerName;
120                             String hostname = host.getAsciiName();
121                             if (hostname.equals(ServerConstants.getWwwHostName())) {
122                                 e2 = sslContextFactory.newSSLEngine();
123                             } else if (hostname.equals(ServerConstants.getStaticHostName())) {
124                                 e2 = staticContextFactory.newSSLEngine();
125                             } else if (hostname.equals(ServerConstants.getSecureHostName())) {
126                                 e2 = secureContextFactory.newSSLEngine();
127                             } else if (hostname.equals(ServerConstants.getApiHostName())) {
128                                 e2 = apiContextFactory.newSSLEngine();
129                             }
130                             break;
131                         }
132                     }
133                 }
134                 if (e2 == null) {
135                     e2 = sslContextFactory.newSSLEngine(sslSession.getPeerHost(), sslSession.getPeerPort());
136                 }
137                 e2.setUseClientMode(false);
138                 return e2;
139             }
140         };
141     }
142
143     private static Handler generateGigiContexts(Properties conf) {
144         ServletHolder webAppServlet = new ServletHolder(new Gigi(conf));
145
146         ContextHandler ch = generateGigiServletContext(webAppServlet);
147         ch.setVirtualHosts(new String[] {
148             ServerConstants.getWwwHostName()
149         });
150         ContextHandler chSecure = generateGigiServletContext(webAppServlet);
151         chSecure.setVirtualHosts(new String[] {
152             ServerConstants.getSecureHostName()
153         });
154
155         HandlerList hl = new HandlerList();
156         hl.setHandlers(new Handler[] {
157                 ch, chSecure
158         });
159         return hl;
160     }
161
162     private static ContextHandler generateGigiServletContext(ServletHolder webAppServlet) {
163         final ResourceHandler rh = new ResourceHandler();
164         rh.setResourceBase("static/www");
165
166         HandlerWrapper hw = new PolicyRedirector();
167         hw.setHandler(rh);
168
169         ServletContextHandler servlet = new ServletContextHandler(ServletContextHandler.SESSIONS);
170         servlet.setInitParameter(SessionManager.__SessionCookieProperty, "CACert-Session");
171         servlet.addServlet(webAppServlet, "/*");
172         ErrorPageErrorHandler epeh = new ErrorPageErrorHandler();
173         epeh.addErrorPage(404, "/error");
174         servlet.setErrorHandler(epeh);
175
176         HandlerList hl = new HandlerList();
177         hl.setHandlers(new Handler[] {
178                 hw, servlet
179         });
180
181         ContextHandler ch = new ContextHandler();
182         ch.setHandler(hl);
183         return ch;
184     }
185
186     private static Handler generateStaticContext() {
187         final ResourceHandler rh = new ResourceHandler();
188         rh.setResourceBase("static/static");
189
190         ContextHandler ch = new ContextHandler();
191         ch.setHandler(rh);
192         ch.setVirtualHosts(new String[] {
193             ServerConstants.getStaticHostName()
194         });
195
196         return ch;
197     }
198
199     private static Handler generateAPIContext() {
200         ServletContextHandler sch = new ServletContextHandler();
201
202         sch.addVirtualHosts(new String[] {
203             ServerConstants.getApiHostName()
204         });
205         sch.addServlet(new ServletHolder(new GigiAPI()), "/*");
206         return sch;
207     }
208
209     private static SslContextFactory generateSSLContextFactory(GigiConfig conf, String alias) throws GeneralSecurityException, IOException {
210         SslContextFactory scf = new SslContextFactory() {
211
212             String[] ciphers = null;
213
214             @Override
215             public void customize(SSLEngine sslEngine) {
216                 super.customize(sslEngine);
217
218                 SSLParameters ssl = sslEngine.getSSLParameters();
219                 ssl.setUseCipherSuitesOrder(true);
220                 if (ciphers == null) {
221                     ciphers = CipherInfo.filter(sslEngine.getSupportedCipherSuites());
222                 }
223
224                 ssl.setCipherSuites(ciphers);
225                 sslEngine.setSSLParameters(ssl);
226
227             }
228
229         };
230         scf.setRenegotiationAllowed(false);
231
232         scf.setProtocol("TLS");
233         scf.setTrustStore(conf.getTrustStore());
234         KeyStore privateStore = conf.getPrivateStore();
235         scf.setKeyStorePassword(conf.getPrivateStorePw());
236         scf.setKeyStore(privateStore);
237         scf.setCertAlias(alias);
238         return scf;
239     }
240 }