1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Calendar;
6 import java.util.HashMap;
8 import java.util.Properties;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.output.Form.CSRFException;
19 import org.cacert.gigi.output.Menu;
20 import org.cacert.gigi.output.Outputable;
21 import org.cacert.gigi.output.SimpleMenuItem;
22 import org.cacert.gigi.output.template.Template;
23 import org.cacert.gigi.pages.LoginPage;
24 import org.cacert.gigi.pages.MainPage;
25 import org.cacert.gigi.pages.Page;
26 import org.cacert.gigi.pages.TestSecure;
27 import org.cacert.gigi.pages.Verify;
28 import org.cacert.gigi.pages.account.CertificateAdd;
29 import org.cacert.gigi.pages.account.Certificates;
30 import org.cacert.gigi.pages.account.ChangePasswordPage;
31 import org.cacert.gigi.pages.account.DomainOverview;
32 import org.cacert.gigi.pages.account.MailOverview;
33 import org.cacert.gigi.pages.account.MyDetails;
34 import org.cacert.gigi.pages.error.PageNotFound;
35 import org.cacert.gigi.pages.main.RegisterPage;
36 import org.cacert.gigi.pages.wot.AssurePage;
37 import org.cacert.gigi.util.ServerConstants;
39 public class Gigi extends HttpServlet {
41 public static final String LOGGEDIN = "loggedin";
43 public static final String USER = "user";
45 private static final long serialVersionUID = -6386785421902852904L;
47 private Template baseTemplate;
49 private HashMap<String, Page> pages = new HashMap<String, Page>();
51 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
55 private static Gigi instance;
57 public Gigi(Properties conf) {
58 if (instance != null) {
59 new IllegalStateException("Multiple Gigi instances!");
61 DatabaseConnection.init(conf);
65 public void init() throws ServletException {
66 putPage("/error", new PageNotFound());
67 putPage("/login", new LoginPage("CACert - Login"));
68 putPage("/", new MainPage("CACert - Home"));
69 putPage("/secure", new TestSecure());
70 putPage(Verify.PATH, new Verify());
71 putPage(AssurePage.PATH + "/*", new AssurePage());
72 putPage(Certificates.PATH + "/*", new Certificates());
73 putPage(MyDetails.PATH, new MyDetails());
74 putPage(ChangePasswordPage.PATH, new ChangePasswordPage());
75 putPage(RegisterPage.PATH, new RegisterPage());
76 putPage(CertificateAdd.PATH, new CertificateAdd());
77 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"));
78 putPage(DomainOverview.PATH, new DomainOverview("Domains"));
79 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
80 m = new Menu("Certificates", "cert", new SimpleMenuItem(MailOverview.DEFAULT_PATH, "Emails"), new SimpleMenuItem("", "Client Certificates"), new SimpleMenuItem("", "Domains"), new SimpleMenuItem("", "Server Certificates"));
84 private void putPage(String path, Page p) {
86 reveresePages.put(p, path);
90 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
92 // if (req.getHeader("Origin") != null) {
93 // resp.getWriter().println("No cross domain access allowed.");
96 HttpSession hs = req.getSession();
97 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
99 hs.setAttribute(LOGGEDIN, null);
102 resp.sendRedirect("/");
106 final Page p = getPage(req.getPathInfo());
109 if ( !p.isPermitted(LoginPage.getUser(req)) && hs.getAttribute("loggedin") == null) {
110 String request = req.getPathInfo();
111 request = request.split("\\?")[0];
112 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
113 resp.sendRedirect("/login");
116 if (p.beforeTemplate(req, resp)) {
119 HashMap<String, Object> vars = new HashMap<String, Object>();
121 resp.setContentType("text/html; charset=utf-8");
122 Outputable content = new Outputable() {
125 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
127 if (req.getMethod().equals("POST")) {
128 if (req.getQueryString() != null) {
135 } catch (CSRFException err) {
137 resp.sendError(500, "CSRF invalid");
138 } catch (IOException e) {
141 } catch (IOException e) {
148 vars.put("title", p.getTitle());
149 vars.put("static", ServerConstants.getStaticHostNamePort());
150 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
151 vars.put("content", content);
152 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
154 resp.sendError(404, "Page not found.");
159 private Page getPage(String pathInfo) {
160 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
161 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
163 Page page = pages.get(pathInfo);
167 page = pages.get(pathInfo + "/*");
171 int idx = pathInfo.lastIndexOf('/');
172 pathInfo = pathInfo.substring(0, idx);
174 page = pages.get(pathInfo + "/*");
182 public static void addXSSHeaders(HttpServletResponse hsr) {
183 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://" + ServerConstants.getSecureHostNamePort());
184 hsr.addHeader("Access-Control-Max-Age", "60");
186 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
187 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
191 private static String defaultCSP = null;
193 private static String getDefaultCSP() {
194 if (defaultCSP == null) {
195 StringBuffer csp = new StringBuffer();
196 csp.append("default-src 'none';");
197 csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
198 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
199 csp.append(";media-src 'none'; object-src 'none';");
200 csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
201 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
202 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
203 csp.append("report-url https://api.cacert.org/security/csp/report");
204 defaultCSP = csp.toString();
209 public static String getPathByPage(Page p) {
210 return instance.reveresePages.get(p);