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;
9 import java.util.Locale;
11 import java.util.Properties;
12 import java.util.regex.Pattern;
14 import javax.servlet.ServletException;
15 import javax.servlet.http.HttpServlet;
16 import javax.servlet.http.HttpServletRequest;
17 import javax.servlet.http.HttpServletResponse;
18 import javax.servlet.http.HttpSession;
20 import org.cacert.gigi.database.DatabaseConnection;
21 import org.cacert.gigi.dbObjects.User;
22 import org.cacert.gigi.localisation.Language;
23 import org.cacert.gigi.output.Form.CSRFException;
24 import org.cacert.gigi.output.Menu;
25 import org.cacert.gigi.output.Outputable;
26 import org.cacert.gigi.output.PageMenuItem;
27 import org.cacert.gigi.output.SimpleMenuItem;
28 import org.cacert.gigi.output.template.Template;
29 import org.cacert.gigi.pages.LoginPage;
30 import org.cacert.gigi.pages.LogoutPage;
31 import org.cacert.gigi.pages.MainPage;
32 import org.cacert.gigi.pages.Page;
33 import org.cacert.gigi.pages.PolicyIndex;
34 import org.cacert.gigi.pages.RootCertPage;
35 import org.cacert.gigi.pages.StaticPage;
36 import org.cacert.gigi.pages.TestSecure;
37 import org.cacert.gigi.pages.Verify;
38 import org.cacert.gigi.pages.account.ChangePasswordPage;
39 import org.cacert.gigi.pages.account.MyDetails;
40 import org.cacert.gigi.pages.account.certs.CertificateAdd;
41 import org.cacert.gigi.pages.account.certs.Certificates;
42 import org.cacert.gigi.pages.account.domain.DomainOverview;
43 import org.cacert.gigi.pages.account.mail.MailOverview;
44 import org.cacert.gigi.pages.admin.TTPAdminPage;
45 import org.cacert.gigi.pages.error.PageNotFound;
46 import org.cacert.gigi.pages.main.RegisterPage;
47 import org.cacert.gigi.pages.wot.AssurePage;
48 import org.cacert.gigi.pages.wot.MyPoints;
49 import org.cacert.gigi.pages.wot.RequestTTPPage;
50 import org.cacert.gigi.ping.PingerDaemon;
51 import org.cacert.gigi.util.ServerConstants;
53 public class Gigi extends HttpServlet {
55 private boolean firstInstanceInited = false;
57 public static final String LOGGEDIN = "loggedin";
59 public static final String USER = "user";
61 private static final long serialVersionUID = -6386785421902852904L;
63 private Template baseTemplate;
65 private LinkedList<Menu> categories = new LinkedList<Menu>();
67 private HashMap<String, Page> pages = new HashMap<String, Page>();
69 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
71 private Menu rootMenu;
73 private static Gigi instance;
75 private PingerDaemon pinger;
77 private KeyStore truststore;
79 public Gigi(Properties conf, KeyStore truststore) {
80 if (instance != null) {
81 throw new IllegalStateException("Multiple Gigi instances!");
84 DatabaseConnection.init(conf);
85 this.truststore = truststore;
86 pinger = new PingerDaemon(truststore);
91 public void init() throws ServletException {
92 if ( !firstInstanceInited) {
93 putPage("/error", new PageNotFound(), null);
94 putPage("/login", new LoginPage("CAcert - Login"), "CAcert.org");
95 putPage("/", new MainPage("CAcert - Home"), null);
96 putPage("/roots", new RootCertPage(truststore), "CAcert.org");
97 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
98 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
99 putPage("/secure", new TestSecure(), null);
100 putPage(Verify.PATH, new Verify(), null);
101 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
102 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
103 putPage(MyDetails.PATH, new MyDetails(), "My Account");
104 putPage(RegisterPage.PATH, new RegisterPage(), "CAcert.org");
105 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
106 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
107 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
108 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
109 putPage(RequestTTPPage.PATH, new RequestTTPPage(), "CAcert Web of Trust");
110 putPage(TTPAdminPage.PATH + "/*", new TTPAdminPage(), "Admin");
111 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
112 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
113 rootMenu = new Menu("Main");
114 Menu about = new Menu("About CAcert.org");
115 categories.add(about);
117 about.addItem(new SimpleMenuItem("//blog.cacert.org/", "CAcert News"));
118 about.addItem(new SimpleMenuItem("//wiki.cacert.org/", "Wiki Documentation"));
119 putPage(PolicyIndex.DEFAULT_PATH, new PolicyIndex(), "About CAcert.org");
120 about.addItem(new SimpleMenuItem("//wiki.cacert.org/FAQ/Privileges", "Point System"));
121 about.addItem(new SimpleMenuItem("//bugs.cacert.org/", "Bug Database"));
122 about.addItem(new SimpleMenuItem("//wiki.cacert.org/Board", "CAcert Board"));
123 about.addItem(new SimpleMenuItem("//lists.cacert.org/wws", "Mailing Lists"));
124 about.addItem(new SimpleMenuItem("//blog.CAcert.org/feed", "RSS News Feed"));
126 Menu languages = new Menu("Translations");
127 for (Locale l : Language.getSupportedLocales()) {
128 languages.addItem(new SimpleMenuItem("?lang=" + l.toString(), l.getDisplayName(l)));
130 categories.add(languages);
131 for (Menu menu : categories) {
133 rootMenu.addItem(menu);
137 firstInstanceInited = true;
142 private void putPage(String path, Page p, String category) {
144 reveresePages.put(p, path);
145 if (category == null) {
149 for (Menu menu : categories) {
150 if (menu.getMenuName().equals(category)) {
156 m = new Menu(category);
159 m.addItem(new PageMenuItem(p));
163 private static String staticTemplateVarHttp;
165 private static String staticTemplateVarHttps;
167 private static String getStaticTemplateVar(boolean https) {
169 if (staticTemplateVarHttps == null) {
170 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
172 return staticTemplateVarHttps;
174 if (staticTemplateVarHttp == null) {
175 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
177 return staticTemplateVarHttp;
182 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
183 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
184 addXSSHeaders(resp, isSecure);
185 // Firefox only sends this, if it's a cross domain access; safari sends
187 String originHeader = req.getHeader("Origin");
188 if (originHeader != null //
190 !(originHeader.matches("^" + Pattern.quote("https://" + ServerConstants.getWwwHostNamePortSecure()) + "(/.*|)") || //
191 originHeader.matches("^" + Pattern.quote("http://" + ServerConstants.getWwwHostNamePort()) + "(/.*|)") || //
192 originHeader.matches("^" + Pattern.quote("https://" + ServerConstants.getSecureHostNamePort()) + "(/.*|)"))) {
193 resp.setContentType("text/html; charset=utf-8");
194 resp.getWriter().println("<html><head><title>Alert</title></head><body>No cross domain access allowed.<br/><b>If you don't know why you're seeing this you may have been fished! Please change your password immediately!</b></body></html>");
197 HttpSession hs = req.getSession();
198 if (req.getParameter("lang") != null) {
199 Locale l = Language.getLocaleFromString(req.getParameter("lang"));
200 Language lu = Language.getInstance(l);
201 req.getSession().setAttribute(Language.SESSION_ATTRIB_NAME, lu.getLocale());
203 final Page p = getPage(req.getPathInfo());
206 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
207 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
210 User currentPageUser = LoginPage.getUser(req);
211 if ( !p.isPermitted(currentPageUser)) {
212 if (hs.getAttribute("loggedin") == null) {
213 String request = req.getPathInfo();
214 request = request.split("\\?")[0];
215 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
216 resp.sendRedirect("/login");
222 if (p.beforeTemplate(req, resp)) {
225 HashMap<String, Object> vars = new HashMap<String, Object>();
226 Outputable content = new Outputable() {
229 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
231 if (req.getMethod().equals("POST")) {
232 if (req.getQueryString() != null) {
239 } catch (CSRFException err) {
241 resp.sendError(500, "CSRF invalid");
242 } catch (IOException e) {
245 } catch (IOException e) {
251 vars.put(Menu.USER_VALUE, currentPageUser);
252 vars.put("menu", rootMenu);
253 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
254 vars.put("static", getStaticTemplateVar(isSecure));
255 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
256 vars.put("content", content);
257 resp.setContentType("text/html; charset=utf-8");
258 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
260 resp.sendError(404, "Page not found.");
265 private Page getPage(String pathInfo) {
266 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
267 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
269 Page page = pages.get(pathInfo);
273 page = pages.get(pathInfo + "/*");
277 int idx = pathInfo.lastIndexOf('/');
278 pathInfo = pathInfo.substring(0, idx);
280 page = pages.get(pathInfo + "/*");
288 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
289 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
290 hsr.addHeader("Access-Control-Max-Age", "60");
292 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
294 hsr.addHeader("Content-Security-Policy", getHttpCSP());
296 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
300 private static String httpsCSP = null;
302 private static String httpCSP = null;
304 private static String getHttpsCSP() {
305 if (httpsCSP == null) {
306 StringBuffer csp = new StringBuffer();
307 csp.append("default-src 'none'");
308 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
309 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
310 csp.append(";media-src 'none'; object-src 'none'");
311 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
312 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
313 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
314 csp.append(";report-url https://api.cacert.org/security/csp/report");
315 httpsCSP = csp.toString();
320 private static String getHttpCSP() {
321 if (httpCSP == null) {
322 StringBuffer csp = new StringBuffer();
323 csp.append("default-src 'none'");
324 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
325 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
326 csp.append(";media-src 'none'; object-src 'none'");
327 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
328 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
329 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
330 csp.append(";report-url http://api.cacert.org/security/csp/report");
331 httpCSP = csp.toString();
336 public static String getPathByPage(Page p) {
337 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
340 public static void notifyPinger() {
341 instance.pinger.interrupt();