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.email.EmailProvider;
18 import org.cacert.gigi.output.Form.CSRFException;
19 import org.cacert.gigi.output.Menu;
20 import org.cacert.gigi.output.MenuItem;
21 import org.cacert.gigi.output.Outputable;
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.ChangePasswordPage;
29 import org.cacert.gigi.pages.account.DomainOverview;
30 import org.cacert.gigi.pages.account.CertificateAdd;
31 import org.cacert.gigi.pages.account.Certificates;
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>();
53 public Gigi(Properties conf) {
54 EmailProvider.init(conf);
55 DatabaseConnection.init(conf);
59 public void init() throws ServletException {
60 pages.put("/error", new PageNotFound());
61 pages.put("/login", new LoginPage("CACert - Login"));
62 pages.put("/", new MainPage("CACert - Home"));
63 pages.put("/secure", new TestSecure());
64 pages.put(Verify.PATH, new Verify());
65 pages.put(AssurePage.PATH + "/*", new AssurePage());
66 pages.put(Certificates.PATH + "/*", new Certificates());
67 pages.put(MyDetails.PATH, new MyDetails());
68 pages.put(ChangePasswordPage.PATH, new ChangePasswordPage());
69 pages.put(RegisterPage.PATH, new RegisterPage());
70 pages.put(CertificateAdd.PATH, new CertificateAdd());
71 pages.put(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"));
72 pages.put(DomainOverview.PATH, new DomainOverview("Domains"));
73 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
74 m = new Menu("Certificates", "cert", new MenuItem(MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("", "Client Certificates"), new MenuItem("", "Domains"), new MenuItem("", "Server Certificates"));
80 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
82 // if (req.getHeader("Origin") != null) {
83 // resp.getWriter().println("No cross domain access allowed.");
86 HttpSession hs = req.getSession();
87 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
89 hs.setAttribute(LOGGEDIN, null);
92 resp.sendRedirect("/");
96 final Page p = getPage(req.getPathInfo());
99 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
100 String request = req.getPathInfo();
101 request = request.split("\\?")[0];
102 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
103 resp.sendRedirect("/login");
106 if (p.beforeTemplate(req, resp)) {
109 HashMap<String, Object> vars = new HashMap<String, Object>();
111 resp.setContentType("text/html; charset=utf-8");
112 Outputable content = new Outputable() {
115 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
117 if (req.getMethod().equals("POST")) {
118 if (req.getQueryString() != null) {
125 } catch (CSRFException err) {
127 resp.sendError(500, "CSRF invalid");
128 } catch (IOException e) {
131 } catch (IOException e) {
138 vars.put("title", p.getTitle());
139 vars.put("static", ServerConstants.getStaticHostNamePort());
140 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
141 vars.put("content", content);
142 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
144 resp.sendError(404, "Page not found.");
149 private Page getPage(String pathInfo) {
150 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
151 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
153 Page page = pages.get(pathInfo);
157 page = pages.get(pathInfo + "/*");
161 int idx = pathInfo.lastIndexOf('/');
162 pathInfo = pathInfo.substring(0, idx);
164 page = pages.get(pathInfo + "/*");
172 public static void addXSSHeaders(HttpServletResponse hsr) {
173 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://" + ServerConstants.getSecureHostNamePort());
174 hsr.addHeader("Access-Control-Max-Age", "60");
176 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
177 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
181 private static String defaultCSP = null;
183 private static String getDefaultCSP() {
184 if (defaultCSP == null) {
185 StringBuffer csp = new StringBuffer();
186 csp.append("default-src 'none';");
187 csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
188 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
189 csp.append(";media-src 'none'; object-src 'none';");
190 csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
191 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
192 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
193 csp.append("report-url https://api.cacert.org/security/csp/report");
194 defaultCSP = csp.toString();