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