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.DomainPingConfiguration;
22 import org.cacert.gigi.dbObjects.User;
23 import org.cacert.gigi.localisation.Language;
24 import org.cacert.gigi.output.Form.CSRFException;
25 import org.cacert.gigi.output.Menu;
26 import org.cacert.gigi.output.Outputable;
27 import org.cacert.gigi.output.PageMenuItem;
28 import org.cacert.gigi.output.SimpleMenuItem;
29 import org.cacert.gigi.output.template.Template;
30 import org.cacert.gigi.pages.LoginPage;
31 import org.cacert.gigi.pages.LogoutPage;
32 import org.cacert.gigi.pages.MainPage;
33 import org.cacert.gigi.pages.Page;
34 import org.cacert.gigi.pages.PolicyIndex;
35 import org.cacert.gigi.pages.RootCertPage;
36 import org.cacert.gigi.pages.StaticPage;
37 import org.cacert.gigi.pages.TestSecure;
38 import org.cacert.gigi.pages.Verify;
39 import org.cacert.gigi.pages.account.CertificateAdd;
40 import org.cacert.gigi.pages.account.Certificates;
41 import org.cacert.gigi.pages.account.ChangePasswordPage;
42 import org.cacert.gigi.pages.account.DomainOverview;
43 import org.cacert.gigi.pages.account.MailOverview;
44 import org.cacert.gigi.pages.account.MyDetails;
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.ping.PingerDaemon;
50 import org.cacert.gigi.util.ServerConstants;
52 public class Gigi extends HttpServlet {
54 private boolean firstInstanceInited = false;
56 public static final String LOGGEDIN = "loggedin";
58 public static final String USER = "user";
60 private static final long serialVersionUID = -6386785421902852904L;
62 private Template baseTemplate;
64 private LinkedList<Menu> categories = new LinkedList<Menu>();
66 private HashMap<String, Page> pages = new HashMap<String, Page>();
68 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
70 private Menu rootMenu;
72 private static Gigi instance;
74 private PingerDaemon pinger;
76 private KeyStore truststore;
78 public Gigi(Properties conf, KeyStore truststore) {
79 if (instance != null) {
80 throw new IllegalStateException("Multiple Gigi instances!");
83 DatabaseConnection.init(conf);
84 this.truststore = truststore;
85 pinger = new PingerDaemon(truststore);
90 public void init() throws ServletException {
91 if ( !firstInstanceInited) {
92 putPage("/error", new PageNotFound(), null);
93 putPage("/login", new LoginPage("CAcert - Login"), "CAcert.org");
94 putPage("/", new MainPage("CAcert - Home"), null);
95 putPage("/roots", new RootCertPage(truststore), "CAcert.org");
96 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
97 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
98 putPage("/secure", new TestSecure(), null);
99 putPage(Verify.PATH, new Verify(), null);
100 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
101 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
102 putPage(MyDetails.PATH, new MyDetails(), "My Account");
103 putPage(RegisterPage.PATH, new RegisterPage(), "CAcert.org");
104 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
105 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
106 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
107 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
108 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
109 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
110 rootMenu = new Menu("Main");
111 Menu about = new Menu("About CAcert.org");
112 categories.add(about);
114 about.addItem(new SimpleMenuItem("//blog.cacert.org/", "CAcert News"));
115 about.addItem(new SimpleMenuItem("//wiki.cacert.org/", "Wiki Documentation"));
116 putPage(PolicyIndex.DEFAULT_PATH, new PolicyIndex(), "About CAcert.org");
117 about.addItem(new SimpleMenuItem("//wiki.cacert.org/FAQ/Privileges", "Point System"));
118 about.addItem(new SimpleMenuItem("//bugs.cacert.org/", "Bug Database"));
119 about.addItem(new SimpleMenuItem("//wiki.cacert.org/Board", "CAcert Board"));
120 about.addItem(new SimpleMenuItem("//lists.cacert.org/wws", "Mailing Lists"));
121 about.addItem(new SimpleMenuItem("//blog.CAcert.org/feed", "RSS News Feed"));
123 Menu languages = new Menu("Translations");
124 for (Locale l : Language.getSupportedLocales()) {
125 languages.addItem(new SimpleMenuItem("?lang=" + l.toString(), l.getDisplayName(l)));
127 categories.add(languages);
128 for (Menu menu : categories) {
130 rootMenu.addItem(menu);
134 firstInstanceInited = true;
139 private void putPage(String path, Page p, String category) {
141 reveresePages.put(p, path);
142 if (category == null) {
146 for (Menu menu : categories) {
147 if (menu.getMenuName().equals(category)) {
153 m = new Menu(category);
156 m.addItem(new PageMenuItem(p));
160 private static String staticTemplateVarHttp;
162 private static String staticTemplateVarHttps;
164 private static String getStaticTemplateVar(boolean https) {
166 if (staticTemplateVarHttps == null) {
167 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
169 return staticTemplateVarHttps;
171 if (staticTemplateVarHttp == null) {
172 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
174 return staticTemplateVarHttp;
179 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
180 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
181 addXSSHeaders(resp, isSecure);
182 // Firefox only sends this, if it's a cross domain access; safari sends
184 String originHeader = req.getHeader("Origin");
185 if (originHeader != null //
187 !(originHeader.matches("^" + Pattern.quote("https://" + ServerConstants.getWwwHostNamePortSecure()) + "(/.*|)") || //
188 originHeader.matches("^" + Pattern.quote("http://" + ServerConstants.getWwwHostNamePort()) + "(/.*|)") || //
189 originHeader.matches("^" + Pattern.quote("https://" + ServerConstants.getSecureHostNamePort()) + "(/.*|)"))) {
190 resp.setContentType("text/html; charset=utf-8");
191 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>");
194 HttpSession hs = req.getSession();
195 if (req.getParameter("lang") != null) {
196 Locale l = Language.getLocaleFromString(req.getParameter("lang"));
197 Language lu = Language.getInstance(l);
198 req.getSession().setAttribute(Language.SESSION_ATTRIB_NAME, lu.getLocale());
200 final Page p = getPage(req.getPathInfo());
203 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
204 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
207 User currentPageUser = LoginPage.getUser(req);
208 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
209 String request = req.getPathInfo();
210 request = request.split("\\?")[0];
211 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
212 resp.sendRedirect("/login");
215 if (p.beforeTemplate(req, resp)) {
218 HashMap<String, Object> vars = new HashMap<String, Object>();
219 Outputable content = new Outputable() {
222 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
224 if (req.getMethod().equals("POST")) {
225 if (req.getQueryString() != null) {
232 } catch (CSRFException err) {
234 resp.sendError(500, "CSRF invalid");
235 } catch (IOException e) {
238 } catch (IOException e) {
244 vars.put(Menu.USER_VALUE, currentPageUser);
245 vars.put("menu", rootMenu);
246 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
247 vars.put("static", getStaticTemplateVar(isSecure));
248 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
249 vars.put("content", content);
250 resp.setContentType("text/html; charset=utf-8");
251 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
253 resp.sendError(404, "Page not found.");
258 private Page getPage(String pathInfo) {
259 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
260 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
262 Page page = pages.get(pathInfo);
266 page = pages.get(pathInfo + "/*");
270 int idx = pathInfo.lastIndexOf('/');
271 pathInfo = pathInfo.substring(0, idx);
273 page = pages.get(pathInfo + "/*");
281 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
282 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
283 hsr.addHeader("Access-Control-Max-Age", "60");
285 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
287 hsr.addHeader("Content-Security-Policy", getHttpCSP());
289 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
293 private static String httpsCSP = null;
295 private static String httpCSP = null;
297 private static String getHttpsCSP() {
298 if (httpsCSP == null) {
299 StringBuffer csp = new StringBuffer();
300 csp.append("default-src 'none'");
301 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
302 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
303 csp.append(";media-src 'none'; object-src 'none'");
304 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
305 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
306 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
307 csp.append(";report-url https://api.cacert.org/security/csp/report");
308 httpsCSP = csp.toString();
313 private static String getHttpCSP() {
314 if (httpCSP == null) {
315 StringBuffer csp = new StringBuffer();
316 csp.append("default-src 'none'");
317 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
318 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
319 csp.append(";media-src 'none'; object-src 'none'");
320 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
321 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
322 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
323 csp.append(";report-url http://api.cacert.org/security/csp/report");
324 httpCSP = csp.toString();
329 public static String getPathByPage(Page p) {
330 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
333 public static void notifyPinger() {
334 instance.pinger.interrupt();
337 public static void requestReping(DomainPingConfiguration dpc) {
338 instance.pinger.requestReping(dpc);