1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.security.KeyStore;
6 import java.util.Calendar;
7 import java.util.HashMap;
8 import java.util.LinkedList;
10 import java.util.Properties;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpSession;
18 import org.cacert.gigi.database.DatabaseConnection;
19 import org.cacert.gigi.localisation.Language;
20 import org.cacert.gigi.output.Form.CSRFException;
21 import org.cacert.gigi.output.Menu;
22 import org.cacert.gigi.output.Outputable;
23 import org.cacert.gigi.output.PageMenuItem;
24 import org.cacert.gigi.output.template.Template;
25 import org.cacert.gigi.pages.LoginPage;
26 import org.cacert.gigi.pages.LogoutPage;
27 import org.cacert.gigi.pages.MainPage;
28 import org.cacert.gigi.pages.Page;
29 import org.cacert.gigi.pages.StaticPage;
30 import org.cacert.gigi.pages.TestSecure;
31 import org.cacert.gigi.pages.Verify;
32 import org.cacert.gigi.pages.account.CertificateAdd;
33 import org.cacert.gigi.pages.account.Certificates;
34 import org.cacert.gigi.pages.account.ChangePasswordPage;
35 import org.cacert.gigi.pages.account.DomainOverview;
36 import org.cacert.gigi.pages.account.MailOverview;
37 import org.cacert.gigi.pages.account.MyDetails;
38 import org.cacert.gigi.pages.error.PageNotFound;
39 import org.cacert.gigi.pages.main.RegisterPage;
40 import org.cacert.gigi.pages.wot.AssurePage;
41 import org.cacert.gigi.pages.wot.MyPoints;
42 import org.cacert.gigi.ping.PingerDaemon;
43 import org.cacert.gigi.util.ServerConstants;
45 public class Gigi extends HttpServlet {
47 private boolean firstInstanceInited = false;
49 public static final String LOGGEDIN = "loggedin";
51 public static final String USER = "user";
53 private static final long serialVersionUID = -6386785421902852904L;
55 private Template baseTemplate;
57 private LinkedList<Menu> categories = new LinkedList<Menu>();
59 private HashMap<String, Page> pages = new HashMap<String, Page>();
61 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
63 private Menu rootMenu;
65 private static Gigi instance;
67 private PingerDaemon pinger;
69 public Gigi(Properties conf, KeyStore truststore) {
70 if (instance != null) {
71 throw new IllegalStateException("Multiple Gigi instances!");
74 DatabaseConnection.init(conf);
75 pinger = new PingerDaemon(truststore);
80 public void init() throws ServletException {
81 if ( !firstInstanceInited) {
82 putPage("/error", new PageNotFound(), null);
83 putPage("/login", new LoginPage("CAcert - Login"), "Join CAcert.org");
84 putPage("/", new MainPage("CAcert - Home"), null);
85 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
86 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
87 putPage("/secure", new TestSecure(), null);
88 putPage(Verify.PATH, new Verify(), null);
89 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
90 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
91 putPage(MyDetails.PATH, new MyDetails(), "My Account");
92 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
93 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
94 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
95 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
96 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
97 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
98 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
99 rootMenu = new Menu("Main");
100 for (Menu menu : categories) {
102 rootMenu.addItem(menu);
105 firstInstanceInited = true;
110 private void putPage(String path, Page p, String category) {
112 reveresePages.put(p, path);
113 if (category == null) {
117 for (Menu menu : categories) {
118 if (menu.getMenuName().equals(category)) {
124 m = new Menu(category);
127 m.addItem(new PageMenuItem(p));
131 private static String staticTemplateVarHttp;
133 private static String staticTemplateVarHttps;
135 private static String getStaticTemplateVar(boolean https) {
137 if (staticTemplateVarHttps == null) {
138 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
140 return staticTemplateVarHttps;
142 if (staticTemplateVarHttp == null) {
143 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
145 return staticTemplateVarHttp;
150 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
151 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
152 addXSSHeaders(resp, isSecure);
153 // if (req.getHeader("Origin") != null) {
154 // resp.getWriter().println("No cross domain access allowed.");
157 HttpSession hs = req.getSession();
158 final Page p = getPage(req.getPathInfo());
161 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
162 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
165 User currentPageUser = LoginPage.getUser(req);
166 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
167 String request = req.getPathInfo();
168 request = request.split("\\?")[0];
169 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
170 resp.sendRedirect("/login");
173 if (p.beforeTemplate(req, resp)) {
176 HashMap<String, Object> vars = new HashMap<String, Object>();
178 resp.setContentType("text/html; charset=utf-8");
179 Outputable content = new Outputable() {
182 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
184 if (req.getMethod().equals("POST")) {
185 if (req.getQueryString() != null) {
192 } catch (CSRFException err) {
194 resp.sendError(500, "CSRF invalid");
195 } catch (IOException e) {
198 } catch (IOException e) {
204 vars.put(Menu.USER_VALUE, currentPageUser);
205 vars.put("menu", rootMenu);
206 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
207 vars.put("static", getStaticTemplateVar(isSecure));
208 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
209 vars.put("content", content);
210 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
212 resp.sendError(404, "Page not found.");
217 private Page getPage(String pathInfo) {
218 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
219 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
221 Page page = pages.get(pathInfo);
225 page = pages.get(pathInfo + "/*");
229 int idx = pathInfo.lastIndexOf('/');
230 pathInfo = pathInfo.substring(0, idx);
232 page = pages.get(pathInfo + "/*");
240 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
241 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
242 hsr.addHeader("Access-Control-Max-Age", "60");
244 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
246 hsr.addHeader("Content-Security-Policy", getHttpCSP());
248 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
252 private static String httpsCSP = null;
254 private static String httpCSP = null;
256 private static String getHttpsCSP() {
257 if (httpsCSP == null) {
258 StringBuffer csp = new StringBuffer();
259 csp.append("default-src 'none'");
260 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
261 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
262 csp.append(";media-src 'none'; object-src 'none'");
263 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
264 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
265 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
266 csp.append(";report-url https://api.cacert.org/security/csp/report");
267 httpsCSP = csp.toString();
272 private static String getHttpCSP() {
273 if (httpCSP == null) {
274 StringBuffer csp = new StringBuffer();
275 csp.append("default-src 'none'");
276 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
277 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
278 csp.append(";media-src 'none'; object-src 'none'");
279 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
280 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
281 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
282 csp.append(";report-url http://api.cacert.org/security/csp/report");
283 httpCSP = csp.toString();
288 public static String getPathByPage(Page p) {
289 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");