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.Menu;
19 import org.cacert.gigi.output.MenuItem;
20 import org.cacert.gigi.output.Outputable;
21 import org.cacert.gigi.output.Form.CSRFException;
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.MailCertificateAdd;
30 import org.cacert.gigi.pages.account.MailCertificates;
31 import org.cacert.gigi.pages.account.MailOverview;
32 import org.cacert.gigi.pages.account.MyDetails;
33 import org.cacert.gigi.pages.error.PageNotFound;
34 import org.cacert.gigi.pages.main.RegisterPage;
35 import org.cacert.gigi.pages.wot.AssurePage;
36 import org.cacert.gigi.util.ServerConstants;
38 public class Gigi extends HttpServlet {
40 public static final String LOGGEDIN = "loggedin";
42 public static final String USER = "user";
44 private static final long serialVersionUID = -6386785421902852904L;
46 private Template baseTemplate;
48 private HashMap<String, Page> pages = new HashMap<String, Page>();
52 public Gigi(Properties conf) {
53 EmailProvider.init(conf);
54 DatabaseConnection.init(conf);
58 public void init() throws ServletException {
59 pages.put("/error", new PageNotFound());
60 pages.put("/login", new LoginPage("CACert - Login"));
61 pages.put("/", new MainPage("CACert - Home"));
62 pages.put("/secure", new TestSecure());
63 pages.put(Verify.PATH, new Verify());
64 pages.put(AssurePage.PATH + "/*", new AssurePage());
65 pages.put(MailCertificates.PATH + "/*", new MailCertificates());
66 pages.put(MyDetails.PATH, new MyDetails());
67 pages.put(ChangePasswordPage.PATH, new ChangePasswordPage());
68 pages.put(RegisterPage.PATH, new RegisterPage());
69 pages.put(MailCertificateAdd.PATH, new MailCertificateAdd());
70 pages.put(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"));
71 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
72 m = new Menu("Certificates", "cert", new MenuItem(MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("", "Client Certificates"), new MenuItem("", "Domains"), new MenuItem("", "Server Certificates"));
78 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
80 // if (req.getHeader("Origin") != null) {
81 // resp.getWriter().println("No cross domain access allowed.");
84 HttpSession hs = req.getSession();
85 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
87 hs.setAttribute(LOGGEDIN, null);
90 resp.sendRedirect("/");
94 final Page p = getPage(req.getPathInfo());
97 if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
98 String request = req.getPathInfo();
99 request = request.split("\\?")[0];
100 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
101 resp.sendRedirect("/login");
104 if (p.beforeTemplate(req, resp)) {
107 HashMap<String, Object> vars = new HashMap<String, Object>();
109 resp.setContentType("text/html; charset=utf-8");
110 Outputable content = new Outputable() {
113 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
115 if (req.getMethod().equals("POST")) {
116 if (req.getQueryString() != null) {
123 } catch (CSRFException err) {
125 resp.sendError(500, "CSRF invalid");
126 } catch (IOException e) {
129 } catch (IOException e) {
136 vars.put("title", p.getTitle());
137 vars.put("static", ServerConstants.getStaticHostNamePort());
138 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
139 vars.put("content", content);
140 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
142 resp.sendError(404, "Page not found.");
147 private Page getPage(String pathInfo) {
148 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
149 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
151 Page page = pages.get(pathInfo);
155 page = pages.get(pathInfo + "/*");
159 int idx = pathInfo.lastIndexOf('/');
160 pathInfo = pathInfo.substring(0, idx);
162 page = pages.get(pathInfo + "/*");
170 public static void addXSSHeaders(HttpServletResponse hsr) {
171 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://" + ServerConstants.getSecureHostNamePort());
172 hsr.addHeader("Access-Control-Max-Age", "60");
174 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
175 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
179 private static String defaultCSP = null;
181 private static String getDefaultCSP() {
182 if (defaultCSP == null) {
183 StringBuffer csp = new StringBuffer();
184 csp.append("default-src 'none';");
185 csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
186 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
187 csp.append(";media-src 'none'; object-src 'none';");
188 csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
189 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
190 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
191 csp.append("report-url https://api.cacert.org/security/csp/report");
192 defaultCSP = csp.toString();