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.StaticPage;
28 import org.cacert.gigi.pages.TestSecure;
29 import org.cacert.gigi.pages.Verify;
30 import org.cacert.gigi.pages.account.CertificateAdd;
31 import org.cacert.gigi.pages.account.Certificates;
32 import org.cacert.gigi.pages.account.ChangePasswordPage;
33 import org.cacert.gigi.pages.account.DomainOverview;
34 import org.cacert.gigi.pages.account.MailOverview;
35 import org.cacert.gigi.pages.account.MyDetails;
36 import org.cacert.gigi.pages.error.PageNotFound;
37 import org.cacert.gigi.pages.main.RegisterPage;
38 import org.cacert.gigi.pages.wot.AssurePage;
39 import org.cacert.gigi.pages.wot.MyPoints;
40 import org.cacert.gigi.ping.PingerDaemon;
41 import org.cacert.gigi.util.ServerConstants;
43 public class Gigi extends HttpServlet {
45 private boolean firstInstanceInited = false;
47 public static final String LOGGEDIN = "loggedin";
49 public static final String USER = "user";
51 private static final long serialVersionUID = -6386785421902852904L;
53 private Template baseTemplate;
55 private LinkedList<Menu> categories = new LinkedList<Menu>();
57 private HashMap<String, Page> pages = new HashMap<String, Page>();
59 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
61 private Menu rootMenu;
63 private static Gigi instance;
65 private PingerDaemon pinger = new PingerDaemon();
67 public Gigi(Properties conf) {
68 if (instance != null) {
69 new IllegalStateException("Multiple Gigi instances!");
72 DatabaseConnection.init(conf);
77 public void init() throws ServletException {
78 if ( !firstInstanceInited) {
79 putPage("/error", new PageNotFound(), null);
80 putPage("/login", new LoginPage("CAcert - Login"), "Join CAcert.org");
81 putPage("/", new MainPage("CAcert - Home"), null);
82 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
83 putPage("/secure", new TestSecure(), null);
84 putPage(Verify.PATH, new Verify(), null);
85 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
86 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
87 putPage(MyDetails.PATH, new MyDetails(), "My Account");
88 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
89 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
90 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
91 putPage(DomainOverview.PATH, new DomainOverview("Domains"), "Certificates");
92 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
93 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
94 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
95 rootMenu = new Menu("Main");
96 for (Menu menu : categories) {
98 rootMenu.addItem(menu);
101 firstInstanceInited = true;
106 private void putPage(String path, Page p, String category) {
108 reveresePages.put(p, path);
109 if (category == null) {
113 for (Menu menu : categories) {
114 if (menu.getMenuName().equals(category)) {
120 m = new Menu(category);
123 m.addItem(new PageMenuItem(p));
128 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
130 // if (req.getHeader("Origin") != null) {
131 // resp.getWriter().println("No cross domain access allowed.");
134 HttpSession hs = req.getSession();
135 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
137 hs.setAttribute(LOGGEDIN, null);
140 resp.sendRedirect("/");
144 final Page p = getPage(req.getPathInfo());
147 User currentPageUser = LoginPage.getUser(req);
148 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
149 String request = req.getPathInfo();
150 request = request.split("\\?")[0];
151 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
152 resp.sendRedirect("/login");
155 if (p.beforeTemplate(req, resp)) {
158 HashMap<String, Object> vars = new HashMap<String, Object>();
160 resp.setContentType("text/html; charset=utf-8");
161 Outputable content = new Outputable() {
164 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
166 if (req.getMethod().equals("POST")) {
167 if (req.getQueryString() != null) {
174 } catch (CSRFException err) {
176 resp.sendError(500, "CSRF invalid");
177 } catch (IOException e) {
180 } catch (IOException e) {
186 vars.put(Menu.USER_VALUE, currentPageUser);
187 vars.put("menu", rootMenu);
188 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
189 vars.put("static", ServerConstants.getStaticHostNamePort());
190 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
191 vars.put("content", content);
192 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
194 resp.sendError(404, "Page not found.");
199 private Page getPage(String pathInfo) {
200 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
201 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
203 Page page = pages.get(pathInfo);
207 page = pages.get(pathInfo + "/*");
211 int idx = pathInfo.lastIndexOf('/');
212 pathInfo = pathInfo.substring(0, idx);
214 page = pages.get(pathInfo + "/*");
222 public static void addXSSHeaders(HttpServletResponse hsr) {
223 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://" + ServerConstants.getSecureHostNamePort());
224 hsr.addHeader("Access-Control-Max-Age", "60");
226 hsr.addHeader("Content-Security-Policy", getDefaultCSP());
227 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
231 private static String defaultCSP = null;
233 private static String getDefaultCSP() {
234 if (defaultCSP == null) {
235 StringBuffer csp = new StringBuffer();
236 csp.append("default-src 'none';");
237 csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
238 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
239 csp.append(";media-src 'none'; object-src 'none';");
240 csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
241 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
242 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
243 csp.append("report-url https://api.cacert.org/security/csp/report");
244 defaultCSP = csp.toString();
249 public static String getPathByPage(Page p) {
250 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");