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;
7 import java.util.LinkedList;
9 import java.util.Properties;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.HttpSession;
17 import org.cacert.gigi.database.DatabaseConnection;
18 import org.cacert.gigi.localisation.Language;
19 import org.cacert.gigi.output.Form.CSRFException;
20 import org.cacert.gigi.output.Menu;
21 import org.cacert.gigi.output.Outputable;
22 import org.cacert.gigi.output.PageMenuItem;
23 import org.cacert.gigi.output.template.Template;
24 import org.cacert.gigi.pages.LoginPage;
25 import org.cacert.gigi.pages.MainPage;
26 import org.cacert.gigi.pages.Page;
27 import org.cacert.gigi.pages.TestSecure;
28 import org.cacert.gigi.pages.Verify;
29 import org.cacert.gigi.pages.account.CertificateAdd;
30 import org.cacert.gigi.pages.account.Certificates;
31 import org.cacert.gigi.pages.account.ChangePasswordPage;
32 import org.cacert.gigi.pages.account.DomainOverview;
33 import org.cacert.gigi.pages.account.MailOverview;
34 import org.cacert.gigi.pages.account.MyDetails;
35 import org.cacert.gigi.pages.error.PageNotFound;
36 import org.cacert.gigi.pages.main.RegisterPage;
37 import org.cacert.gigi.pages.wot.AssurePage;
38 import org.cacert.gigi.pages.wot.MyPoints;
39 import org.cacert.gigi.util.ServerConstants;
41 public class Gigi extends HttpServlet {
43 private boolean firstInstanceInited = false;
45 public static final String LOGGEDIN = "loggedin";
47 public static final String USER = "user";
49 private static final long serialVersionUID = -6386785421902852904L;
51 private Template baseTemplate;
53 private LinkedList<Menu> categories = new LinkedList<Menu>();
55 private HashMap<String, Page> pages = new HashMap<String, Page>();
57 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
59 private Menu rootMenu;
61 private static Gigi instance;
63 public Gigi(Properties conf) {
64 if (instance != null) {
65 new IllegalStateException("Multiple Gigi instances!");
68 DatabaseConnection.init(conf);
72 public void init() throws ServletException {
73 if ( !firstInstanceInited) {
74 putPage("/error", new PageNotFound(), null);
75 putPage("/login", new LoginPage("CACert - Login"), "Join CAcert.org");
76 putPage("/", new MainPage("CACert - Home"), null);
77 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
78 putPage("/secure", new TestSecure(), null);
79 putPage(Verify.PATH, new Verify(), null);
80 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
81 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
82 putPage(MyDetails.PATH, new MyDetails(), "My Account");
83 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
84 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
85 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
86 putPage(DomainOverview.PATH, new DomainOverview("Domains"), "Certificates");
87 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
88 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
89 rootMenu = new Menu("Main");
90 for (Menu menu : categories) {
92 rootMenu.addItem(menu);
95 firstInstanceInited = true;
100 private void putPage(String path, Page p, String category) {
102 reveresePages.put(p, path);
103 if (category == null) {
107 for (Menu menu : categories) {
108 if (menu.getMenuName().equals(category)) {
114 m = new Menu(category);
117 m.addItem(new PageMenuItem(p));
122 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
124 // if (req.getHeader("Origin") != null) {
125 // resp.getWriter().println("No cross domain access allowed.");
128 HttpSession hs = req.getSession();
129 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
131 hs.setAttribute(LOGGEDIN, null);
134 resp.sendRedirect("/");
138 final Page p = getPage(req.getPathInfo());
141 User currentPageUser = LoginPage.getUser(req);
142 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
143 String request = req.getPathInfo();
144 request = request.split("\\?")[0];
145 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
146 resp.sendRedirect("/login");
149 if (p.beforeTemplate(req, resp)) {
152 HashMap<String, Object> vars = new HashMap<String, Object>();
154 resp.setContentType("text/html; charset=utf-8");
155 Outputable content = new Outputable() {
158 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
160 if (req.getMethod().equals("POST")) {
161 if (req.getQueryString() != null) {
168 } catch (CSRFException err) {
170 resp.sendError(500, "CSRF invalid");
171 } catch (IOException e) {
174 } catch (IOException e) {
180 vars.put(Menu.USER_VALUE, currentPageUser);
181 vars.put("menu", rootMenu);
182 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
183 vars.put("static", ServerConstants.getStaticHostNamePort());
184 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
185 vars.put("content", content);
186 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
188 resp.sendError(404, "Page not found.");
193 private Page getPage(String pathInfo) {
194 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
195 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
197 Page page = pages.get(pathInfo);
201 page = pages.get(pathInfo + "/*");
205 int idx = pathInfo.lastIndexOf('/');
206 pathInfo = pathInfo.substring(0, idx);
208 page = pages.get(pathInfo + "/*");
216 public static void addXSSHeaders(HttpServletResponse hsr) {
217 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://" + ServerConstants.getSecureHostNamePort());
218 hsr.addHeader("Access-Control-Max-Age", "60");
220 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
221 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
225 private static String defaultCSP = null;
227 private static String getDefaultCSP() {
228 if (defaultCSP == null) {
229 StringBuffer csp = new StringBuffer();
230 csp.append("default-src 'none';");
231 csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
232 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
233 csp.append(";media-src 'none'; object-src 'none';");
234 csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
235 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
236 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
237 csp.append("report-url https://api.cacert.org/security/csp/report");
238 defaultCSP = csp.toString();
243 public static String getPathByPage(Page p) {
244 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");