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;
10 import java.util.Properties;
12 import javax.servlet.ServletException;
13 import javax.servlet.http.HttpServlet;
14 import javax.servlet.http.HttpServletRequest;
15 import javax.servlet.http.HttpServletResponse;
16 import javax.servlet.http.HttpSession;
18 import org.cacert.gigi.database.DatabaseConnection;
19 import org.cacert.gigi.localisation.Language;
20 import org.cacert.gigi.output.Form.CSRFException;
21 import org.cacert.gigi.output.Menu;
22 import org.cacert.gigi.output.Outputable;
23 import org.cacert.gigi.output.PageMenuItem;
24 import org.cacert.gigi.output.SimpleMenuItem;
25 import org.cacert.gigi.output.template.Template;
26 import org.cacert.gigi.pages.LoginPage;
27 import org.cacert.gigi.pages.LogoutPage;
28 import org.cacert.gigi.pages.MainPage;
29 import org.cacert.gigi.pages.Page;
30 import org.cacert.gigi.pages.PolicyIndex;
31 import org.cacert.gigi.pages.RootCertPage;
32 import org.cacert.gigi.pages.StaticPage;
33 import org.cacert.gigi.pages.TestSecure;
34 import org.cacert.gigi.pages.Verify;
35 import org.cacert.gigi.pages.account.CertificateAdd;
36 import org.cacert.gigi.pages.account.Certificates;
37 import org.cacert.gigi.pages.account.ChangePasswordPage;
38 import org.cacert.gigi.pages.account.DomainOverview;
39 import org.cacert.gigi.pages.account.MailOverview;
40 import org.cacert.gigi.pages.account.MyDetails;
41 import org.cacert.gigi.pages.error.PageNotFound;
42 import org.cacert.gigi.pages.main.RegisterPage;
43 import org.cacert.gigi.pages.wot.AssurePage;
44 import org.cacert.gigi.pages.wot.MyPoints;
45 import org.cacert.gigi.ping.PingerDaemon;
46 import org.cacert.gigi.util.ServerConstants;
48 public class Gigi extends HttpServlet {
50 private boolean firstInstanceInited = false;
52 public static final String LOGGEDIN = "loggedin";
54 public static final String USER = "user";
56 private static final long serialVersionUID = -6386785421902852904L;
58 private Template baseTemplate;
60 private LinkedList<Menu> categories = new LinkedList<Menu>();
62 private HashMap<String, Page> pages = new HashMap<String, Page>();
64 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
66 private Menu rootMenu;
68 private static Gigi instance;
70 private PingerDaemon pinger;
72 private KeyStore truststore;
74 public Gigi(Properties conf, KeyStore truststore) {
75 if (instance != null) {
76 throw new IllegalStateException("Multiple Gigi instances!");
79 DatabaseConnection.init(conf);
80 this.truststore = truststore;
81 pinger = new PingerDaemon(truststore);
86 public void init() throws ServletException {
87 if ( !firstInstanceInited) {
88 putPage("/error", new PageNotFound(), null);
89 putPage("/login", new LoginPage("CAcert - Login"), "Join CAcert.org");
90 putPage("/", new MainPage("CAcert - Home"), null);
91 putPage("/roots", new RootCertPage(truststore), "Join CAcert.org");
92 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
93 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
94 putPage("/secure", new TestSecure(), null);
95 putPage(Verify.PATH, new Verify(), null);
96 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
97 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
98 putPage(MyDetails.PATH, new MyDetails(), "My Account");
99 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
100 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
101 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
102 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
103 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
104 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
105 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
106 rootMenu = new Menu("Main");
107 Menu about = new Menu("About CAcert.org");
108 categories.add(about);
110 about.addItem(new SimpleMenuItem("//blog.cacert.org/", "CAcert News"));
111 about.addItem(new SimpleMenuItem("//wiki.cacert.org/", "Wiki Documentation"));
112 putPage(PolicyIndex.DEFAULT_PATH, new PolicyIndex(), "About CAcert.org");
113 about.addItem(new SimpleMenuItem("//wiki.cacert.org/FAQ/Privileges", "Point System"));
114 about.addItem(new SimpleMenuItem("//bugs.cacert.org/", "Bug Database"));
115 about.addItem(new SimpleMenuItem("//wiki.cacert.org/Board", "CAcert Board"));
116 about.addItem(new SimpleMenuItem("//lists.cacert.org/wws", "Mailing Lists"));
117 about.addItem(new SimpleMenuItem("//blog.CAcert.org/feed", "RSS News Feed"));
119 for (Menu menu : categories) {
121 rootMenu.addItem(menu);
125 firstInstanceInited = true;
130 private void putPage(String path, Page p, String category) {
132 reveresePages.put(p, path);
133 if (category == null) {
137 for (Menu menu : categories) {
138 if (menu.getMenuName().equals(category)) {
144 m = new Menu(category);
147 m.addItem(new PageMenuItem(p));
151 private static String staticTemplateVarHttp;
153 private static String staticTemplateVarHttps;
155 private static String getStaticTemplateVar(boolean https) {
157 if (staticTemplateVarHttps == null) {
158 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
160 return staticTemplateVarHttps;
162 if (staticTemplateVarHttp == null) {
163 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
165 return staticTemplateVarHttp;
170 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
171 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
172 addXSSHeaders(resp, isSecure);
173 // if (req.getHeader("Origin") != null) {
174 // resp.getWriter().println("No cross domain access allowed.");
177 HttpSession hs = req.getSession();
178 final Page p = getPage(req.getPathInfo());
181 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
182 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
185 User currentPageUser = LoginPage.getUser(req);
186 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
187 String request = req.getPathInfo();
188 request = request.split("\\?")[0];
189 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
190 resp.sendRedirect("/login");
193 if (p.beforeTemplate(req, resp)) {
196 HashMap<String, Object> vars = new HashMap<String, Object>();
198 resp.setContentType("text/html; charset=utf-8");
199 Outputable content = new Outputable() {
202 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
204 if (req.getMethod().equals("POST")) {
205 if (req.getQueryString() != null) {
212 } catch (CSRFException err) {
214 resp.sendError(500, "CSRF invalid");
215 } catch (IOException e) {
218 } catch (IOException e) {
224 vars.put(Menu.USER_VALUE, currentPageUser);
225 vars.put("menu", rootMenu);
226 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
227 vars.put("static", getStaticTemplateVar(isSecure));
228 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
229 vars.put("content", content);
230 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
232 resp.sendError(404, "Page not found.");
237 private Page getPage(String pathInfo) {
238 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
239 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
241 Page page = pages.get(pathInfo);
245 page = pages.get(pathInfo + "/*");
249 int idx = pathInfo.lastIndexOf('/');
250 pathInfo = pathInfo.substring(0, idx);
252 page = pages.get(pathInfo + "/*");
260 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
261 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
262 hsr.addHeader("Access-Control-Max-Age", "60");
264 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
266 hsr.addHeader("Content-Security-Policy", getHttpCSP());
268 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
272 private static String httpsCSP = null;
274 private static String httpCSP = null;
276 private static String getHttpsCSP() {
277 if (httpsCSP == null) {
278 StringBuffer csp = new StringBuffer();
279 csp.append("default-src 'none'");
280 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
281 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
282 csp.append(";media-src 'none'; object-src 'none'");
283 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
284 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
285 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
286 csp.append(";report-url https://api.cacert.org/security/csp/report");
287 httpsCSP = csp.toString();
292 private static String getHttpCSP() {
293 if (httpCSP == null) {
294 StringBuffer csp = new StringBuffer();
295 csp.append("default-src 'none'");
296 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
297 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
298 csp.append(";media-src 'none'; object-src 'none'");
299 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
300 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
301 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
302 csp.append(";report-url http://api.cacert.org/security/csp/report");
303 httpCSP = csp.toString();
308 public static String getPathByPage(Page p) {
309 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
312 public static void notifyPinger() {
313 instance.pinger.interrupt();