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;
13 import javax.servlet.ServletException;
14 import javax.servlet.http.HttpServlet;
15 import javax.servlet.http.HttpServletRequest;
16 import javax.servlet.http.HttpServletResponse;
17 import javax.servlet.http.HttpSession;
19 import org.cacert.gigi.database.DatabaseConnection;
20 import org.cacert.gigi.dbObjects.User;
21 import org.cacert.gigi.localisation.Language;
22 import org.cacert.gigi.output.Form.CSRFException;
23 import org.cacert.gigi.output.Menu;
24 import org.cacert.gigi.output.Outputable;
25 import org.cacert.gigi.output.PageMenuItem;
26 import org.cacert.gigi.output.SimpleMenuItem;
27 import org.cacert.gigi.output.template.Template;
28 import org.cacert.gigi.pages.LoginPage;
29 import org.cacert.gigi.pages.LogoutPage;
30 import org.cacert.gigi.pages.MainPage;
31 import org.cacert.gigi.pages.Page;
32 import org.cacert.gigi.pages.PolicyIndex;
33 import org.cacert.gigi.pages.RootCertPage;
34 import org.cacert.gigi.pages.StaticPage;
35 import org.cacert.gigi.pages.TestSecure;
36 import org.cacert.gigi.pages.Verify;
37 import org.cacert.gigi.pages.account.CertificateAdd;
38 import org.cacert.gigi.pages.account.Certificates;
39 import org.cacert.gigi.pages.account.ChangePasswordPage;
40 import org.cacert.gigi.pages.account.DomainOverview;
41 import org.cacert.gigi.pages.account.MailOverview;
42 import org.cacert.gigi.pages.account.MyDetails;
43 import org.cacert.gigi.pages.error.PageNotFound;
44 import org.cacert.gigi.pages.main.RegisterPage;
45 import org.cacert.gigi.pages.wot.AssurePage;
46 import org.cacert.gigi.pages.wot.MyPoints;
47 import org.cacert.gigi.ping.PingerDaemon;
48 import org.cacert.gigi.util.ServerConstants;
50 public class Gigi extends HttpServlet {
52 private boolean firstInstanceInited = false;
54 public static final String LOGGEDIN = "loggedin";
56 public static final String USER = "user";
58 private static final long serialVersionUID = -6386785421902852904L;
60 private Template baseTemplate;
62 private LinkedList<Menu> categories = new LinkedList<Menu>();
64 private HashMap<String, Page> pages = new HashMap<String, Page>();
66 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
68 private Menu rootMenu;
70 private static Gigi instance;
72 private PingerDaemon pinger;
74 private KeyStore truststore;
76 public Gigi(Properties conf, KeyStore truststore) {
77 if (instance != null) {
78 throw new IllegalStateException("Multiple Gigi instances!");
81 DatabaseConnection.init(conf);
82 this.truststore = truststore;
83 pinger = new PingerDaemon(truststore);
88 public void init() throws ServletException {
89 if ( !firstInstanceInited) {
90 putPage("/error", new PageNotFound(), null);
91 putPage("/login", new LoginPage("CAcert - Login"), "Join CAcert.org");
92 putPage("/", new MainPage("CAcert - Home"), null);
93 putPage("/roots", new RootCertPage(truststore), "Join CAcert.org");
94 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
95 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
96 putPage("/secure", new TestSecure(), null);
97 putPage(Verify.PATH, new Verify(), null);
98 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
99 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
100 putPage(MyDetails.PATH, new MyDetails(), "My Account");
101 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
102 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
103 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
104 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
105 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
106 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
107 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
108 rootMenu = new Menu("Main");
109 Menu about = new Menu("About CAcert.org");
110 categories.add(about);
112 about.addItem(new SimpleMenuItem("//blog.cacert.org/", "CAcert News"));
113 about.addItem(new SimpleMenuItem("//wiki.cacert.org/", "Wiki Documentation"));
114 putPage(PolicyIndex.DEFAULT_PATH, new PolicyIndex(), "About CAcert.org");
115 about.addItem(new SimpleMenuItem("//wiki.cacert.org/FAQ/Privileges", "Point System"));
116 about.addItem(new SimpleMenuItem("//bugs.cacert.org/", "Bug Database"));
117 about.addItem(new SimpleMenuItem("//wiki.cacert.org/Board", "CAcert Board"));
118 about.addItem(new SimpleMenuItem("//lists.cacert.org/wws", "Mailing Lists"));
119 about.addItem(new SimpleMenuItem("//blog.CAcert.org/feed", "RSS News Feed"));
121 Menu languages = new Menu("Translations");
122 for (Locale l : Language.getSupportedLocales()) {
123 languages.addItem(new SimpleMenuItem("?lang=" + l.toString(), l.getDisplayName(l)));
125 categories.add(languages);
126 for (Menu menu : categories) {
128 rootMenu.addItem(menu);
132 firstInstanceInited = true;
137 private void putPage(String path, Page p, String category) {
139 reveresePages.put(p, path);
140 if (category == null) {
144 for (Menu menu : categories) {
145 if (menu.getMenuName().equals(category)) {
151 m = new Menu(category);
154 m.addItem(new PageMenuItem(p));
158 private static String staticTemplateVarHttp;
160 private static String staticTemplateVarHttps;
162 private static String getStaticTemplateVar(boolean https) {
164 if (staticTemplateVarHttps == null) {
165 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
167 return staticTemplateVarHttps;
169 if (staticTemplateVarHttp == null) {
170 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
172 return staticTemplateVarHttp;
177 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
178 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
179 addXSSHeaders(resp, isSecure);
180 // if (req.getHeader("Origin") != null) {
181 // resp.getWriter().println("No cross domain access allowed.");
184 HttpSession hs = req.getSession();
185 if (req.getParameter("lang") != null) {
186 Locale l = Language.getLocaleFromString(req.getParameter("lang"));
187 Language lu = Language.getInstance(l);
188 req.getSession().setAttribute(Language.SESSION_ATTRIB_NAME, lu.getLocale());
190 final Page p = getPage(req.getPathInfo());
193 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
194 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
197 User currentPageUser = LoginPage.getUser(req);
198 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
199 String request = req.getPathInfo();
200 request = request.split("\\?")[0];
201 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
202 resp.sendRedirect("/login");
205 if (p.beforeTemplate(req, resp)) {
208 HashMap<String, Object> vars = new HashMap<String, Object>();
210 resp.setContentType("text/html; charset=utf-8");
211 Outputable content = new Outputable() {
214 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
216 if (req.getMethod().equals("POST")) {
217 if (req.getQueryString() != null) {
224 } catch (CSRFException err) {
226 resp.sendError(500, "CSRF invalid");
227 } catch (IOException e) {
230 } catch (IOException e) {
236 vars.put(Menu.USER_VALUE, currentPageUser);
237 vars.put("menu", rootMenu);
238 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
239 vars.put("static", getStaticTemplateVar(isSecure));
240 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
241 vars.put("content", content);
242 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
244 resp.sendError(404, "Page not found.");
249 private Page getPage(String pathInfo) {
250 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
251 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
253 Page page = pages.get(pathInfo);
257 page = pages.get(pathInfo + "/*");
261 int idx = pathInfo.lastIndexOf('/');
262 pathInfo = pathInfo.substring(0, idx);
264 page = pages.get(pathInfo + "/*");
272 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
273 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
274 hsr.addHeader("Access-Control-Max-Age", "60");
276 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
278 hsr.addHeader("Content-Security-Policy", getHttpCSP());
280 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
284 private static String httpsCSP = null;
286 private static String httpCSP = null;
288 private static String getHttpsCSP() {
289 if (httpsCSP == null) {
290 StringBuffer csp = new StringBuffer();
291 csp.append("default-src 'none'");
292 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
293 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
294 csp.append(";media-src 'none'; object-src 'none'");
295 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
296 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
297 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
298 csp.append(";report-url https://api.cacert.org/security/csp/report");
299 httpsCSP = csp.toString();
304 private static String getHttpCSP() {
305 if (httpCSP == null) {
306 StringBuffer csp = new StringBuffer();
307 csp.append("default-src 'none'");
308 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
309 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
310 csp.append(";media-src 'none'; object-src 'none'");
311 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
312 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
313 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
314 csp.append(";report-url http://api.cacert.org/security/csp/report");
315 httpCSP = csp.toString();
320 public static String getPathByPage(Page p) {
321 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
324 public static void notifyPinger() {
325 instance.pinger.interrupt();