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.util.ServerConstants;
40 public class Gigi extends HttpServlet {
42 private boolean firstInstanceInited = false;
44 public static final String LOGGEDIN = "loggedin";
46 public static final String USER = "user";
48 private static final long serialVersionUID = -6386785421902852904L;
50 private Template baseTemplate;
52 private LinkedList<Menu> categories = new LinkedList<Menu>();
54 private HashMap<String, Page> pages = new HashMap<String, Page>();
56 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
58 private Menu rootMenu;
60 private static Gigi instance;
62 public Gigi(Properties conf) {
63 if (instance != null) {
64 new IllegalStateException("Multiple Gigi instances!");
67 DatabaseConnection.init(conf);
71 public void init() throws ServletException {
72 if ( !firstInstanceInited) {
73 putPage("/error", new PageNotFound(), null);
74 putPage("/login", new LoginPage("CACert - Login"), "Join CAcert.org");
75 putPage("/", new MainPage("CACert - Home"), null);
76 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
77 putPage("/secure", new TestSecure(), null);
78 putPage(Verify.PATH, new Verify(), null);
79 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
80 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
81 putPage(MyDetails.PATH, new MyDetails(), "My Account");
82 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
83 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
84 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
85 putPage(DomainOverview.PATH, new DomainOverview("Domains"), "Certificates");
86 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
87 rootMenu = new Menu("Main");
88 for (Menu menu : categories) {
90 rootMenu.addItem(menu);
93 firstInstanceInited = true;
98 private void putPage(String path, Page p, String category) {
100 reveresePages.put(p, path);
101 if (category == null) {
105 for (Menu menu : categories) {
106 if (menu.getMenuName().equals(category)) {
112 m = new Menu(category);
115 m.addItem(new PageMenuItem(p));
120 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
122 // if (req.getHeader("Origin") != null) {
123 // resp.getWriter().println("No cross domain access allowed.");
126 HttpSession hs = req.getSession();
127 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
129 hs.setAttribute(LOGGEDIN, null);
132 resp.sendRedirect("/");
136 final Page p = getPage(req.getPathInfo());
139 User currentPageUser = LoginPage.getUser(req);
140 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
141 String request = req.getPathInfo();
142 request = request.split("\\?")[0];
143 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
144 resp.sendRedirect("/login");
147 if (p.beforeTemplate(req, resp)) {
150 HashMap<String, Object> vars = new HashMap<String, Object>();
152 resp.setContentType("text/html; charset=utf-8");
153 Outputable content = new Outputable() {
156 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
158 if (req.getMethod().equals("POST")) {
159 if (req.getQueryString() != null) {
166 } catch (CSRFException err) {
168 resp.sendError(500, "CSRF invalid");
169 } catch (IOException e) {
172 } catch (IOException e) {
178 vars.put(Menu.USER_VALUE, currentPageUser);
179 vars.put("menu", rootMenu);
180 vars.put("title", p.getTitle());
181 vars.put("static", ServerConstants.getStaticHostNamePort());
182 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
183 vars.put("content", content);
184 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
186 resp.sendError(404, "Page not found.");
191 private Page getPage(String pathInfo) {
192 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
193 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
195 Page page = pages.get(pathInfo);
199 page = pages.get(pathInfo + "/*");
203 int idx = pathInfo.lastIndexOf('/');
204 pathInfo = pathInfo.substring(0, idx);
206 page = pages.get(pathInfo + "/*");
214 public static void addXSSHeaders(HttpServletResponse hsr) {
215 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://" + ServerConstants.getSecureHostNamePort());
216 hsr.addHeader("Access-Control-Max-Age", "60");
218 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
219 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
223 private static String defaultCSP = null;
225 private static String getDefaultCSP() {
226 if (defaultCSP == null) {
227 StringBuffer csp = new StringBuffer();
228 csp.append("default-src 'none';");
229 csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
230 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
231 csp.append(";media-src 'none'; object-src 'none';");
232 csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
233 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
234 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
235 csp.append("report-url https://api.cacert.org/security/csp/report");
236 defaultCSP = csp.toString();
241 public static String getPathByPage(Page p) {
242 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");