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.RootCertPage;
31 import org.cacert.gigi.pages.StaticPage;
32 import org.cacert.gigi.pages.TestSecure;
33 import org.cacert.gigi.pages.Verify;
34 import org.cacert.gigi.pages.account.CertificateAdd;
35 import org.cacert.gigi.pages.account.Certificates;
36 import org.cacert.gigi.pages.account.ChangePasswordPage;
37 import org.cacert.gigi.pages.account.DomainOverview;
38 import org.cacert.gigi.pages.account.MailOverview;
39 import org.cacert.gigi.pages.account.MyDetails;
40 import org.cacert.gigi.pages.error.PageNotFound;
41 import org.cacert.gigi.pages.main.RegisterPage;
42 import org.cacert.gigi.pages.wot.AssurePage;
43 import org.cacert.gigi.pages.wot.MyPoints;
44 import org.cacert.gigi.ping.PingerDaemon;
45 import org.cacert.gigi.util.ServerConstants;
47 public class Gigi extends HttpServlet {
49 private boolean firstInstanceInited = false;
51 public static final String LOGGEDIN = "loggedin";
53 public static final String USER = "user";
55 private static final long serialVersionUID = -6386785421902852904L;
57 private Template baseTemplate;
59 private LinkedList<Menu> categories = new LinkedList<Menu>();
61 private HashMap<String, Page> pages = new HashMap<String, Page>();
63 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
65 private Menu rootMenu;
67 private static Gigi instance;
69 private PingerDaemon pinger;
71 private KeyStore truststore;
73 public Gigi(Properties conf, KeyStore truststore) {
74 if (instance != null) {
75 throw new IllegalStateException("Multiple Gigi instances!");
78 DatabaseConnection.init(conf);
79 this.truststore = truststore;
80 pinger = new PingerDaemon(truststore);
85 public void init() throws ServletException {
86 if ( !firstInstanceInited) {
87 putPage("/error", new PageNotFound(), null);
88 putPage("/login", new LoginPage("CAcert - Login"), "Join CAcert.org");
89 putPage("/", new MainPage("CAcert - Home"), null);
90 putPage("/roots", new RootCertPage(truststore), "Join CAcert.org");
91 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
92 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
93 putPage("/secure", new TestSecure(), null);
94 putPage(Verify.PATH, new Verify(), null);
95 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
96 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
97 putPage(MyDetails.PATH, new MyDetails(), "My Account");
98 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
99 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
100 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
101 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
102 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
103 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
104 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
105 rootMenu = new Menu("Main");
106 for (Menu menu : categories) {
108 rootMenu.addItem(menu);
110 Menu about = new Menu("About CAcert.org");
111 about.addItem(new SimpleMenuItem("//blog.cacert.org/", "CAcert News"));
112 about.addItem(new SimpleMenuItem("//wiki.cacert.org/", "Wiki Documentation"));
113 about.addItem(new SimpleMenuItem("/policy", "Policies"));
114 about.addItem(new SimpleMenuItem("//wiki.cacert.org/FAQ/Privileges", "Point System"));
115 about.addItem(new SimpleMenuItem("//bugs.cacert.org/", "Bug Database"));
116 about.addItem(new SimpleMenuItem("//wiki.cacert.org/Board", "CAcert Board"));
117 about.addItem(new SimpleMenuItem("//lists.cacert.org/wws", "Mailing Lists"));
118 about.addItem(new SimpleMenuItem("//blog.CAcert.org/feed", "RSS News Feed"));
120 rootMenu.addItem(about);
123 firstInstanceInited = true;
128 private void putPage(String path, Page p, String category) {
130 reveresePages.put(p, path);
131 if (category == null) {
135 for (Menu menu : categories) {
136 if (menu.getMenuName().equals(category)) {
142 m = new Menu(category);
145 m.addItem(new PageMenuItem(p));
149 private static String staticTemplateVarHttp;
151 private static String staticTemplateVarHttps;
153 private static String getStaticTemplateVar(boolean https) {
155 if (staticTemplateVarHttps == null) {
156 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
158 return staticTemplateVarHttps;
160 if (staticTemplateVarHttp == null) {
161 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
163 return staticTemplateVarHttp;
168 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
169 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
170 addXSSHeaders(resp, isSecure);
171 // if (req.getHeader("Origin") != null) {
172 // resp.getWriter().println("No cross domain access allowed.");
175 HttpSession hs = req.getSession();
176 final Page p = getPage(req.getPathInfo());
179 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
180 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
183 User currentPageUser = LoginPage.getUser(req);
184 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
185 String request = req.getPathInfo();
186 request = request.split("\\?")[0];
187 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
188 resp.sendRedirect("/login");
191 if (p.beforeTemplate(req, resp)) {
194 HashMap<String, Object> vars = new HashMap<String, Object>();
196 resp.setContentType("text/html; charset=utf-8");
197 Outputable content = new Outputable() {
200 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
202 if (req.getMethod().equals("POST")) {
203 if (req.getQueryString() != null) {
210 } catch (CSRFException err) {
212 resp.sendError(500, "CSRF invalid");
213 } catch (IOException e) {
216 } catch (IOException e) {
222 vars.put(Menu.USER_VALUE, currentPageUser);
223 vars.put("menu", rootMenu);
224 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
225 vars.put("static", getStaticTemplateVar(isSecure));
226 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
227 vars.put("content", content);
228 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
230 resp.sendError(404, "Page not found.");
235 private Page getPage(String pathInfo) {
236 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
237 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
239 Page page = pages.get(pathInfo);
243 page = pages.get(pathInfo + "/*");
247 int idx = pathInfo.lastIndexOf('/');
248 pathInfo = pathInfo.substring(0, idx);
250 page = pages.get(pathInfo + "/*");
258 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
259 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
260 hsr.addHeader("Access-Control-Max-Age", "60");
262 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
264 hsr.addHeader("Content-Security-Policy", getHttpCSP());
266 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
270 private static String httpsCSP = null;
272 private static String httpCSP = null;
274 private static String getHttpsCSP() {
275 if (httpsCSP == null) {
276 StringBuffer csp = new StringBuffer();
277 csp.append("default-src 'none'");
278 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
279 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
280 csp.append(";media-src 'none'; object-src 'none'");
281 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
282 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
283 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
284 csp.append(";report-url https://api.cacert.org/security/csp/report");
285 httpsCSP = csp.toString();
290 private static String getHttpCSP() {
291 if (httpCSP == null) {
292 StringBuffer csp = new StringBuffer();
293 csp.append("default-src 'none'");
294 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
295 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
296 csp.append(";media-src 'none'; object-src 'none'");
297 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
298 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
299 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
300 csp.append(";report-url http://api.cacert.org/security/csp/report");
301 httpCSP = csp.toString();
306 public static String getPathByPage(Page p) {
307 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
310 public static void notifyPinger() {
311 instance.pinger.interrupt();