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.Manager;
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.ChangePasswordPage;
40 import org.cacert.gigi.pages.account.MyDetails;
41 import org.cacert.gigi.pages.account.certs.CertificateAdd;
42 import org.cacert.gigi.pages.account.certs.Certificates;
43 import org.cacert.gigi.pages.account.domain.DomainOverview;
44 import org.cacert.gigi.pages.account.mail.MailOverview;
45 import org.cacert.gigi.pages.admin.TTPAdminPage;
46 import org.cacert.gigi.pages.error.PageNotFound;
47 import org.cacert.gigi.pages.main.RegisterPage;
48 import org.cacert.gigi.pages.orga.CreateOrgPage;
49 import org.cacert.gigi.pages.orga.ViewOrgPage;
50 import org.cacert.gigi.pages.wot.AssurePage;
51 import org.cacert.gigi.pages.wot.MyPoints;
52 import org.cacert.gigi.pages.wot.RequestTTPPage;
53 import org.cacert.gigi.ping.PingerDaemon;
54 import org.cacert.gigi.util.ServerConstants;
56 public class Gigi extends HttpServlet {
58 private boolean firstInstanceInited = false;
60 public static final String LOGGEDIN = "loggedin";
62 public static final String USER = "user";
64 private static final long serialVersionUID = -6386785421902852904L;
66 private Template baseTemplate;
68 private LinkedList<Menu> categories = new LinkedList<Menu>();
70 private HashMap<String, Page> pages = new HashMap<String, Page>();
72 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
74 private Menu rootMenu;
76 private static Gigi instance;
78 private PingerDaemon pinger;
80 private KeyStore truststore;
82 private boolean testing;
84 public Gigi(Properties conf, KeyStore truststore) {
85 if (instance != null) {
86 throw new IllegalStateException("Multiple Gigi instances!");
88 testing = conf.getProperty("testing") != null;
90 DatabaseConnection.init(conf);
91 this.truststore = truststore;
92 pinger = new PingerDaemon(truststore);
97 public void init() throws ServletException {
98 if ( !firstInstanceInited) {
99 putPage("/error", new PageNotFound(), null);
100 putPage("/login", new LoginPage("CAcert - Login"), "CAcert.org");
101 putPage("/", new MainPage("CAcert - Home"), null);
102 putPage("/roots", new RootCertPage(truststore), "CAcert.org");
103 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
104 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
105 putPage("/secure", new TestSecure(), null);
106 putPage(Verify.PATH, new Verify(), null);
107 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
108 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
109 putPage(MyDetails.PATH, new MyDetails(), "My Account");
110 putPage(RegisterPage.PATH, new RegisterPage(), "CAcert.org");
111 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
112 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
113 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
114 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
115 putPage(RequestTTPPage.PATH, new RequestTTPPage(), "CAcert Web of Trust");
116 putPage(TTPAdminPage.PATH + "/*", new TTPAdminPage(), "Admin");
117 putPage(CreateOrgPage.DEFAULT_PATH, new CreateOrgPage(), "Admin");
118 putPage(ViewOrgPage.DEFAULT_PATH + "/*", new ViewOrgPage(), "Admin");
120 putPage(Manager.PATH + "/*", Manager.getInstance(), "Admin");
123 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
124 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
125 rootMenu = new Menu("Main");
126 Menu about = new Menu("About CAcert.org");
127 categories.add(about);
129 about.addItem(new SimpleMenuItem("//blog.cacert.org/", "CAcert News"));
130 about.addItem(new SimpleMenuItem("//wiki.cacert.org/", "Wiki Documentation"));
131 putPage(PolicyIndex.DEFAULT_PATH, new PolicyIndex(), "About CAcert.org");
132 about.addItem(new SimpleMenuItem("//wiki.cacert.org/FAQ/Privileges", "Point System"));
133 about.addItem(new SimpleMenuItem("//bugs.cacert.org/", "Bug Database"));
134 about.addItem(new SimpleMenuItem("//wiki.cacert.org/Board", "CAcert Board"));
135 about.addItem(new SimpleMenuItem("//lists.cacert.org/wws", "Mailing Lists"));
136 about.addItem(new SimpleMenuItem("//blog.CAcert.org/feed", "RSS News Feed"));
138 Menu languages = new Menu("Translations");
139 for (Locale l : Language.getSupportedLocales()) {
140 languages.addItem(new SimpleMenuItem("?lang=" + l.toString(), l.getDisplayName(l)));
142 categories.add(languages);
143 for (Menu menu : categories) {
145 rootMenu.addItem(menu);
149 firstInstanceInited = true;
154 private void putPage(String path, Page p, String category) {
156 reveresePages.put(p, path);
157 if (category == null) {
161 for (Menu menu : categories) {
162 if (menu.getMenuName().equals(category)) {
168 m = new Menu(category);
171 m.addItem(new PageMenuItem(p));
175 private static String staticTemplateVarHttp;
177 private static String staticTemplateVarHttps;
179 private static String getStaticTemplateVar(boolean https) {
181 if (staticTemplateVarHttps == null) {
182 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
184 return staticTemplateVarHttps;
186 if (staticTemplateVarHttp == null) {
187 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
189 return staticTemplateVarHttp;
194 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
195 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
196 addXSSHeaders(resp, isSecure);
197 // Firefox only sends this, if it's a cross domain access; safari sends
199 String originHeader = req.getHeader("Origin");
200 if (originHeader != null //
202 !(originHeader.matches("^" + Pattern.quote("https://" + ServerConstants.getWwwHostNamePortSecure()) + "(/.*|)") || //
203 originHeader.matches("^" + Pattern.quote("http://" + ServerConstants.getWwwHostNamePort()) + "(/.*|)") || //
204 originHeader.matches("^" + Pattern.quote("https://" + ServerConstants.getSecureHostNamePort()) + "(/.*|)"))) {
205 resp.setContentType("text/html; charset=utf-8");
206 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>");
209 HttpSession hs = req.getSession();
210 if (req.getParameter("lang") != null) {
211 Locale l = Language.getLocaleFromString(req.getParameter("lang"));
212 Language lu = Language.getInstance(l);
213 req.getSession().setAttribute(Language.SESSION_ATTRIB_NAME, lu.getLocale());
215 final Page p = getPage(req.getPathInfo());
218 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
219 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
222 User currentPageUser = LoginPage.getUser(req);
223 if ( !p.isPermitted(currentPageUser)) {
224 if (hs.getAttribute("loggedin") == null) {
225 String request = req.getPathInfo();
226 request = request.split("\\?")[0];
227 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
228 resp.sendRedirect("/login");
234 if (p.beforeTemplate(req, resp)) {
237 HashMap<String, Object> vars = new HashMap<String, Object>();
238 Outputable content = new Outputable() {
241 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
243 if (req.getMethod().equals("POST")) {
244 if (req.getQueryString() != null) {
251 } catch (CSRFException err) {
253 resp.sendError(500, "CSRF invalid");
254 } catch (IOException e) {
257 } catch (IOException e) {
263 vars.put(Menu.USER_VALUE, currentPageUser);
264 vars.put("menu", rootMenu);
265 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
266 vars.put("static", getStaticTemplateVar(isSecure));
267 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
268 vars.put("content", content);
269 resp.setContentType("text/html; charset=utf-8");
270 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
272 resp.sendError(404, "Page not found.");
277 private Page getPage(String pathInfo) {
278 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
279 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
281 Page page = pages.get(pathInfo);
285 page = pages.get(pathInfo + "/*");
289 int idx = pathInfo.lastIndexOf('/');
290 pathInfo = pathInfo.substring(0, idx);
292 page = pages.get(pathInfo + "/*");
300 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
301 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
302 hsr.addHeader("Access-Control-Max-Age", "60");
304 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
306 hsr.addHeader("Content-Security-Policy", getHttpCSP());
308 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
312 private static String httpsCSP = null;
314 private static String httpCSP = null;
316 private static String getHttpsCSP() {
317 if (httpsCSP == null) {
318 StringBuffer csp = new StringBuffer();
319 csp.append("default-src 'none'");
320 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
321 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
322 csp.append(";media-src 'none'; object-src 'none'");
323 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
324 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
325 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
326 csp.append(";report-url https://api.cacert.org/security/csp/report");
327 httpsCSP = csp.toString();
332 private static String getHttpCSP() {
333 if (httpCSP == null) {
334 StringBuffer csp = new StringBuffer();
335 csp.append("default-src 'none'");
336 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
337 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
338 csp.append(";media-src 'none'; object-src 'none'");
339 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
340 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
341 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
342 csp.append(";report-url http://api.cacert.org/security/csp/report");
343 httpCSP = csp.toString();
348 public static String getPathByPage(Page p) {
349 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
352 public static void notifyPinger() {
353 instance.pinger.interrupt();