]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Launcher.java
Merge branch 'libs/jetty/upstream' into libs/jetty/local
[gigi.git] / src / org / cacert / gigi / Launcher.java
1 package org.cacert.gigi;
2 import java.io.IOException;
3 import java.security.GeneralSecurityException;
4 import java.security.KeyStore;
5 import java.util.Properties;
6
7 import javax.net.ssl.SSLEngine;
8 import javax.net.ssl.SSLParameters;
9 import javax.net.ssl.TrustManagerFactory;
10 import org.cacert.gigi.natives.SetUID;
11 import org.cacert.gigi.util.CipherInfo;
12 import org.eclipse.jetty.server.Connector;
13 import org.eclipse.jetty.server.Handler;
14 import org.eclipse.jetty.server.HttpConfiguration;
15 import org.eclipse.jetty.server.HttpConnectionFactory;
16 import org.eclipse.jetty.server.SecureRequestCustomizer;
17 import org.eclipse.jetty.server.Server;
18 import org.eclipse.jetty.server.ServerConnector;
19 import org.eclipse.jetty.server.SessionManager;
20 import org.eclipse.jetty.server.SslConnectionFactory;
21 import org.eclipse.jetty.server.handler.ContextHandler;
22 import org.eclipse.jetty.server.handler.HandlerList;
23 import org.eclipse.jetty.server.handler.HandlerWrapper;
24 import org.eclipse.jetty.server.handler.ResourceHandler;
25 import org.eclipse.jetty.servlet.ServletContextHandler;
26 import org.eclipse.jetty.servlet.ServletHolder;
27 import org.eclipse.jetty.util.log.Log;
28 import org.eclipse.jetty.util.ssl.SslContextFactory;
29
30 public class Launcher {
31         public static void main(String[] args) throws Exception {
32                 GigiConfig conf = GigiConfig.parse(System.in);
33
34                 Server s = new Server();
35                 // === SSL HTTP Configuration ===
36                 HttpConfiguration https_config = new HttpConfiguration();
37                 https_config.setSendServerVersion(false);
38                 https_config.setSendXPoweredBy(false);
39
40                 // for client-cert auth
41                 https_config.addCustomizer(new SecureRequestCustomizer());
42
43                 ServerConnector connector = new ServerConnector(s,
44                                 new SslConnectionFactory(generateSSLContextFactory(conf),
45                                                 "http/1.1"), new HttpConnectionFactory(https_config));
46                 connector.setHost(conf.getMainProps().getProperty("host"));
47                 connector.setPort(Integer.parseInt(conf.getMainProps().getProperty(
48                                 "port")));
49                 s.setConnectors(new Connector[]{connector});
50
51                 HandlerList hl = new HandlerList();
52                 hl.setHandlers(new Handler[]{generateStaticContext(),
53                                 generateGigiContext(conf.getMainProps())});
54                 s.setHandler(hl);
55                 s.start();
56                 if (connector.getPort() <= 1024
57                                 && !System.getProperty("os.name").toLowerCase().contains("win")) {
58                         SetUID uid = new SetUID();
59                         if (!uid.setUid(65536 - 2, 65536 - 2).getSuccess()) {
60                                 Log.getLogger(Launcher.class).warn("Couldn't set uid!");
61                         }
62                 }
63         }
64
65         private static ServletContextHandler generateGigiContext(Properties conf) {
66                 ServletContextHandler servlet = new ServletContextHandler(
67                                 ServletContextHandler.SESSIONS);
68                 servlet.setInitParameter(SessionManager.__SessionCookieProperty,
69                                 "CACert-Session");
70                 servlet.addServlet(new ServletHolder(new Gigi(conf)), "/*");
71                 return servlet;
72         }
73
74         private static Handler generateStaticContext() {
75                 final ResourceHandler rh = new ResourceHandler();
76                 rh.setResourceBase("static");
77                 HandlerWrapper hw = new PolicyRedirector();
78                 hw.setHandler(rh);
79
80                 ContextHandler ch = new ContextHandler();
81                 ch.setContextPath("/static");
82                 ch.setHandler(hw);
83
84                 return ch;
85         }
86
87         private static SslContextFactory generateSSLContextFactory(GigiConfig conf)
88                         throws GeneralSecurityException, IOException {
89                 TrustManagerFactory tmFactory = TrustManagerFactory.getInstance("PKIX");
90                 tmFactory.init((KeyStore) null);
91
92                 SslContextFactory scf = new SslContextFactory() {
93
94                         String[] ciphers = null;
95
96                         @Override
97                         public void customize(SSLEngine sslEngine) {
98                                 super.customize(sslEngine);
99
100                                 SSLParameters ssl = sslEngine.getSSLParameters();
101                                 ssl.setUseCipherSuitesOrder(true);
102                                 if (ciphers == null) {
103                                         ciphers = CipherInfo.filter(sslEngine
104                                                         .getSupportedCipherSuites());
105                                 }
106
107                                 ssl.setCipherSuites(ciphers);
108                                 sslEngine.setSSLParameters(ssl);
109
110                         }
111
112                 };
113                 scf.setRenegotiationAllowed(false);
114                 scf.setWantClientAuth(true);
115
116                 scf.setProtocol("TLS");
117                 scf.setTrustStore(conf.getTrustStore());
118                 scf.setKeyStore(conf.getPrivateStore());
119                 return scf;
120         }
121 }