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.error.PageNotFound;
45 import org.cacert.gigi.pages.main.RegisterPage;
46 import org.cacert.gigi.pages.wot.AssurePage;
47 import org.cacert.gigi.pages.wot.MyPoints;
48 import org.cacert.gigi.pages.wot.RequestTTPPage;
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(RequestTTPPage.PATH, new RequestTTPPage(), "CAcert Web of Trust");
109 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
110 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
111 rootMenu = new Menu("Main");
112 Menu about = new Menu("About CAcert.org");
113 categories.add(about);
115 about.addItem(new SimpleMenuItem("//blog.cacert.org/", "CAcert News"));
116 about.addItem(new SimpleMenuItem("//wiki.cacert.org/", "Wiki Documentation"));
117 putPage(PolicyIndex.DEFAULT_PATH, new PolicyIndex(), "About CAcert.org");
118 about.addItem(new SimpleMenuItem("//wiki.cacert.org/FAQ/Privileges", "Point System"));
119 about.addItem(new SimpleMenuItem("//bugs.cacert.org/", "Bug Database"));
120 about.addItem(new SimpleMenuItem("//wiki.cacert.org/Board", "CAcert Board"));
121 about.addItem(new SimpleMenuItem("//lists.cacert.org/wws", "Mailing Lists"));
122 about.addItem(new SimpleMenuItem("//blog.CAcert.org/feed", "RSS News Feed"));
124 Menu languages = new Menu("Translations");
125 for (Locale l : Language.getSupportedLocales()) {
126 languages.addItem(new SimpleMenuItem("?lang=" + l.toString(), l.getDisplayName(l)));
128 categories.add(languages);
129 for (Menu menu : categories) {
131 rootMenu.addItem(menu);
135 firstInstanceInited = true;
140 private void putPage(String path, Page p, String category) {
142 reveresePages.put(p, path);
143 if (category == null) {
147 for (Menu menu : categories) {
148 if (menu.getMenuName().equals(category)) {
154 m = new Menu(category);
157 m.addItem(new PageMenuItem(p));
161 private static String staticTemplateVarHttp;
163 private static String staticTemplateVarHttps;
165 private static String getStaticTemplateVar(boolean https) {
167 if (staticTemplateVarHttps == null) {
168 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
170 return staticTemplateVarHttps;
172 if (staticTemplateVarHttp == null) {
173 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
175 return staticTemplateVarHttp;
180 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
181 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
182 addXSSHeaders(resp, isSecure);
183 // Firefox only sends this, if it's a cross domain access; safari sends
185 String originHeader = req.getHeader("Origin");
186 if (originHeader != null //
188 !(originHeader.matches("^" + Pattern.quote("https://" + ServerConstants.getWwwHostNamePortSecure()) + "(/.*|)") || //
189 originHeader.matches("^" + Pattern.quote("http://" + ServerConstants.getWwwHostNamePort()) + "(/.*|)") || //
190 originHeader.matches("^" + Pattern.quote("https://" + ServerConstants.getSecureHostNamePort()) + "(/.*|)"))) {
191 resp.setContentType("text/html; charset=utf-8");
192 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>");
195 HttpSession hs = req.getSession();
196 if (req.getParameter("lang") != null) {
197 Locale l = Language.getLocaleFromString(req.getParameter("lang"));
198 Language lu = Language.getInstance(l);
199 req.getSession().setAttribute(Language.SESSION_ATTRIB_NAME, lu.getLocale());
201 final Page p = getPage(req.getPathInfo());
204 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
205 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
208 User currentPageUser = LoginPage.getUser(req);
209 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
210 String request = req.getPathInfo();
211 request = request.split("\\?")[0];
212 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
213 resp.sendRedirect("/login");
216 if (p.beforeTemplate(req, resp)) {
219 HashMap<String, Object> vars = new HashMap<String, Object>();
220 Outputable content = new Outputable() {
223 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
225 if (req.getMethod().equals("POST")) {
226 if (req.getQueryString() != null) {
233 } catch (CSRFException err) {
235 resp.sendError(500, "CSRF invalid");
236 } catch (IOException e) {
239 } catch (IOException e) {
245 vars.put(Menu.USER_VALUE, currentPageUser);
246 vars.put("menu", rootMenu);
247 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
248 vars.put("static", getStaticTemplateVar(isSecure));
249 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
250 vars.put("content", content);
251 resp.setContentType("text/html; charset=utf-8");
252 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
254 resp.sendError(404, "Page not found.");
259 private Page getPage(String pathInfo) {
260 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
261 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
263 Page page = pages.get(pathInfo);
267 page = pages.get(pathInfo + "/*");
271 int idx = pathInfo.lastIndexOf('/');
272 pathInfo = pathInfo.substring(0, idx);
274 page = pages.get(pathInfo + "/*");
282 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
283 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
284 hsr.addHeader("Access-Control-Max-Age", "60");
286 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
288 hsr.addHeader("Content-Security-Policy", getHttpCSP());
290 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
294 private static String httpsCSP = null;
296 private static String httpCSP = null;
298 private static String getHttpsCSP() {
299 if (httpsCSP == null) {
300 StringBuffer csp = new StringBuffer();
301 csp.append("default-src 'none'");
302 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
303 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
304 csp.append(";media-src 'none'; object-src 'none'");
305 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
306 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
307 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
308 csp.append(";report-url https://api.cacert.org/security/csp/report");
309 httpsCSP = csp.toString();
314 private static String getHttpCSP() {
315 if (httpCSP == null) {
316 StringBuffer csp = new StringBuffer();
317 csp.append("default-src 'none'");
318 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
319 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
320 csp.append(";media-src 'none'; object-src 'none'");
321 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
322 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
323 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
324 csp.append(";report-url http://api.cacert.org/security/csp/report");
325 httpCSP = csp.toString();
330 public static String getPathByPage(Page p) {
331 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
334 public static void notifyPinger() {
335 instance.pinger.interrupt();