import java.io.IOException;
import java.io.PrintWriter;
+import java.io.UnsupportedEncodingException;
import java.security.KeyStore;
import java.security.cert.X509Certificate;
import java.util.Calendar;
+import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Locale;
public class Gigi extends HttpServlet {
- private boolean firstInstanceInited = false;
-
- public static final String LOGGEDIN = "loggedin";
-
- public static final String CERT_SERIAL = "org.cacert.gigi.serial";
-
- public static final String CERT_ISSUER = "org.cacert.gigi.issuer";
-
- public static final String USER = "user";
-
- public static final String LOGIN_METHOD = "org.cacert.gigi.loginMethod";
-
- private static final long serialVersionUID = -6386785421902852904L;
-
- private Template baseTemplate;
+ private class MenuBuilder {
- private LinkedList<Menu> categories = new LinkedList<Menu>();
+ private LinkedList<Menu> categories = new LinkedList<Menu>();
- private HashMap<String, Page> pages = new HashMap<String, Page>();
+ private HashMap<String, Page> pages = new HashMap<String, Page>();
- private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
+ private Menu rootMenu;
- private Menu rootMenu;
-
- private static Gigi instance;
-
- private PingerDaemon pinger;
+ public MenuBuilder() {}
- private KeyStore truststore;
+ private void putPage(String path, Page p, String category) {
+ pages.put(path, p);
+ if (category == null) {
+ return;
+ }
+ Menu m = getMenu(category);
+ m.addItem(new PageMenuItem(p, path.replaceFirst("/?\\*$", "")));
- private boolean testing;
+ }
- public Gigi(Properties conf, KeyStore truststore) {
- if (instance != null) {
- throw new IllegalStateException("Multiple Gigi instances!");
+ private Menu getMenu(String category) {
+ Menu m = null;
+ for (Menu menu : categories) {
+ if (menu.getMenuName().equals(category)) {
+ m = menu;
+ break;
+ }
+ }
+ if (m == null) {
+ m = new Menu(category);
+ categories.add(m);
+ }
+ return m;
}
- testing = conf.getProperty("testing") != null;
- instance = this;
- DatabaseConnection.init(conf);
- this.truststore = truststore;
- pinger = new PingerDaemon(truststore);
- pinger.start();
- }
- @Override
- public void init() throws ServletException {
- if ( !firstInstanceInited) {
+ public Menu generateMenu() throws ServletException {
putPage("/denied", new AccessDenied(), null);
putPage("/error", new PageNotFound(), null);
putPage("/login", new LoginPage("Password Login"), "CAcert.org");
}
}
- putPage("/wot/rules", new StaticPage("Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "Web of Trust");
+ try {
+ putPage("/wot/rules", new StaticPage("Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "Web of Trust");
+ } catch (UnsupportedEncodingException e) {
+ throw new ServletException(e);
+ }
baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
rootMenu = new Menu("Main");
Menu about = new Menu("About CAcert.org");
}
rootMenu.prepare();
- firstInstanceInited = true;
+ return rootMenu;
+ }
+
+ public Map<String, Page> getPages() {
+ return Collections.unmodifiableMap(pages);
}
- super.init();
}
- private void putPage(String path, Page p, String category) {
- pages.put(path, p);
- reveresePages.put(p, path);
- if (category == null) {
+ public static final String LOGGEDIN = "loggedin";
+
+ public static final String CERT_SERIAL = "org.cacert.gigi.serial";
+
+ public static final String CERT_ISSUER = "org.cacert.gigi.issuer";
+
+ public static final String USER = "user";
+
+ public static final String LOGIN_METHOD = "org.cacert.gigi.loginMethod";
+
+ private static final long serialVersionUID = -6386785421902852904L;
+
+ private static Gigi instance;
+
+ private Template baseTemplate;
+
+ private PingerDaemon pinger;
+
+ private KeyStore truststore;
+
+ private boolean testing;
+
+ private Menu rootMenu;
+
+ private Map<String, Page> pages;
+
+ private boolean firstInstanceInited = false;
+
+ public Gigi(Properties conf, KeyStore truststore) {
+ synchronized (Gigi.class) {
+ if (instance != null) {
+ throw new IllegalStateException("Multiple Gigi instances!");
+ }
+ testing = conf.getProperty("testing") != null;
+ instance = this;
+ DatabaseConnection.init(conf);
+ this.truststore = truststore;
+ pinger = new PingerDaemon(truststore);
+ pinger.start();
+ }
+ }
+
+ @Override
+ public synchronized void init() throws ServletException {
+ if (firstInstanceInited) {
+ super.init();
return;
}
- Menu m = getMenu(category);
- m.addItem(new PageMenuItem(p));
+ MenuBuilder mb = new MenuBuilder();
+ rootMenu = mb.generateMenu();
+ pages = mb.getPages();
+ firstInstanceInited = true;
+ super.init();
}
- private Menu getMenu(String category) {
- Menu m = null;
- for (Menu menu : categories) {
- if (menu.getMenuName().equals(category)) {
- m = menu;
- break;
- }
+ private Page getPage(String pathInfo) {
+ if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
+ pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
+ }
+ Page page = pages.get(pathInfo);
+ if (page != null) {
+ return page;
}
- if (m == null) {
- m = new Menu(category);
- categories.add(m);
+ page = pages.get(pathInfo + "/*");
+ if (page != null) {
+ return page;
}
- return m;
+ int idx = pathInfo.lastIndexOf('/');
+ pathInfo = pathInfo.substring(0, idx);
+
+ page = pages.get(pathInfo + "/*");
+ if (page != null) {
+ return page;
+ }
+ return null;
+
}
- private static String staticTemplateVarHttp;
+ private static String staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
- private static String staticTemplateVarHttps;
+ private static String staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
private static String getStaticTemplateVar(boolean https) {
if (https) {
- if (staticTemplateVarHttps == null) {
- staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
- }
return staticTemplateVarHttps;
} else {
- if (staticTemplateVarHttp == null) {
- staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
- }
return staticTemplateVarHttp;
}
}
}
- private Page getPage(String pathInfo) {
- if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
- pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
- }
- Page page = pages.get(pathInfo);
- if (page != null) {
- return page;
- }
- page = pages.get(pathInfo + "/*");
- if (page != null) {
- return page;
- }
- int idx = pathInfo.lastIndexOf('/');
- pathInfo = pathInfo.substring(0, idx);
-
- page = pages.get(pathInfo + "/*");
- if (page != null) {
- return page;
- }
- return null;
-
- }
-
public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
hsr.addHeader("Access-Control-Max-Age", "60");
if (doHttps) {
- hsr.addHeader("Content-Security-Policy", getHttpsCSP());
+ hsr.addHeader("Content-Security-Policy", httpsCSP);
} else {
- hsr.addHeader("Content-Security-Policy", getHttpCSP());
+ hsr.addHeader("Content-Security-Policy", httpCSP);
}
hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
}
- private static String httpsCSP = null;
-
- private static String httpCSP = null;
-
- private static String getHttpsCSP() {
- if (httpsCSP == null) {
- StringBuffer csp = new StringBuffer();
- csp.append("default-src 'none'");
- csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
- csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
- csp.append(";media-src 'none'; object-src 'none'");
- csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
- csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
- csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
- csp.append(";report-url https://api.cacert.org/security/csp/report");
- httpsCSP = csp.toString();
- }
- return httpsCSP;
- }
-
- private static String getHttpCSP() {
- if (httpCSP == null) {
- StringBuffer csp = new StringBuffer();
- csp.append("default-src 'none'");
- csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
- csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
- csp.append(";media-src 'none'; object-src 'none'");
- csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
- csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
- csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
- csp.append(";report-url http://api.cacert.org/security/csp/report");
- httpCSP = csp.toString();
- }
- return httpCSP;
+ private static String httpsCSP = genHttpsCSP();
+
+ private static String httpCSP = genHttpCSP();
+
+ private static String genHttpsCSP() {
+ StringBuffer csp = new StringBuffer();
+ csp.append("default-src 'none'");
+ csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
+ csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
+ csp.append(";media-src 'none'; object-src 'none'");
+ csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
+ csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
+ csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
+ csp.append(";report-url https://api.cacert.org/security/csp/report");
+ return csp.toString();
}
- public static String getPathByPage(Page p) {
- return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
+ private static String genHttpCSP() {
+ StringBuffer csp = new StringBuffer();
+ csp.append("default-src 'none'");
+ csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
+ csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
+ csp.append(";media-src 'none'; object-src 'none'");
+ csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
+ csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
+ csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
+ csp.append(";report-url http://api.cacert.org/security/csp/report");
+ return csp.toString();
}
/**