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.LogoutPage;
26 import org.cacert.gigi.pages.MainPage;
27 import org.cacert.gigi.pages.Page;
28 import org.cacert.gigi.pages.StaticPage;
29 import org.cacert.gigi.pages.TestSecure;
30 import org.cacert.gigi.pages.Verify;
31 import org.cacert.gigi.pages.account.CertificateAdd;
32 import org.cacert.gigi.pages.account.Certificates;
33 import org.cacert.gigi.pages.account.ChangePasswordPage;
34 import org.cacert.gigi.pages.account.DomainOverview;
35 import org.cacert.gigi.pages.account.MailOverview;
36 import org.cacert.gigi.pages.account.MyDetails;
37 import org.cacert.gigi.pages.error.PageNotFound;
38 import org.cacert.gigi.pages.main.RegisterPage;
39 import org.cacert.gigi.pages.wot.AssurePage;
40 import org.cacert.gigi.pages.wot.MyPoints;
41 import org.cacert.gigi.ping.PingerDaemon;
42 import org.cacert.gigi.util.ServerConstants;
44 public class Gigi extends HttpServlet {
46 private boolean firstInstanceInited = false;
48 public static final String LOGGEDIN = "loggedin";
50 public static final String USER = "user";
52 private static final long serialVersionUID = -6386785421902852904L;
54 private Template baseTemplate;
56 private LinkedList<Menu> categories = new LinkedList<Menu>();
58 private HashMap<String, Page> pages = new HashMap<String, Page>();
60 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
62 private Menu rootMenu;
64 private static Gigi instance;
66 private PingerDaemon pinger = new PingerDaemon();
68 public Gigi(Properties conf) {
69 if (instance != null) {
70 throw new IllegalStateException("Multiple Gigi instances!");
73 DatabaseConnection.init(conf);
78 public void init() throws ServletException {
79 if ( !firstInstanceInited) {
80 putPage("/error", new PageNotFound(), null);
81 putPage("/login", new LoginPage("CAcert - Login"), "Join CAcert.org");
82 putPage("/", new MainPage("CAcert - Home"), null);
83 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
84 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
85 putPage("/secure", new TestSecure(), null);
86 putPage(Verify.PATH, new Verify(), null);
87 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
88 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
89 putPage(MyDetails.PATH, new MyDetails(), "My Account");
90 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
91 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
92 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
93 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
94 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
95 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
96 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
97 rootMenu = new Menu("Main");
98 for (Menu menu : categories) {
100 rootMenu.addItem(menu);
103 firstInstanceInited = true;
108 private void putPage(String path, Page p, String category) {
110 reveresePages.put(p, path);
111 if (category == null) {
115 for (Menu menu : categories) {
116 if (menu.getMenuName().equals(category)) {
122 m = new Menu(category);
125 m.addItem(new PageMenuItem(p));
130 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
132 // if (req.getHeader("Origin") != null) {
133 // resp.getWriter().println("No cross domain access allowed.");
136 HttpSession hs = req.getSession();
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("/?\\*$", "");