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.localisation.Language;
21 import org.cacert.gigi.output.Form.CSRFException;
22 import org.cacert.gigi.output.Menu;
23 import org.cacert.gigi.output.Outputable;
24 import org.cacert.gigi.output.PageMenuItem;
25 import org.cacert.gigi.output.SimpleMenuItem;
26 import org.cacert.gigi.output.template.Template;
27 import org.cacert.gigi.pages.LoginPage;
28 import org.cacert.gigi.pages.LogoutPage;
29 import org.cacert.gigi.pages.MainPage;
30 import org.cacert.gigi.pages.Page;
31 import org.cacert.gigi.pages.PolicyIndex;
32 import org.cacert.gigi.pages.RootCertPage;
33 import org.cacert.gigi.pages.StaticPage;
34 import org.cacert.gigi.pages.TestSecure;
35 import org.cacert.gigi.pages.Verify;
36 import org.cacert.gigi.pages.account.CertificateAdd;
37 import org.cacert.gigi.pages.account.Certificates;
38 import org.cacert.gigi.pages.account.ChangePasswordPage;
39 import org.cacert.gigi.pages.account.DomainOverview;
40 import org.cacert.gigi.pages.account.MailOverview;
41 import org.cacert.gigi.pages.account.MyDetails;
42 import org.cacert.gigi.pages.error.PageNotFound;
43 import org.cacert.gigi.pages.main.RegisterPage;
44 import org.cacert.gigi.pages.wot.AssurePage;
45 import org.cacert.gigi.pages.wot.MyPoints;
46 import org.cacert.gigi.ping.PingerDaemon;
47 import org.cacert.gigi.util.ServerConstants;
49 public class Gigi extends HttpServlet {
51 private boolean firstInstanceInited = false;
53 public static final String LOGGEDIN = "loggedin";
55 public static final String USER = "user";
57 private static final long serialVersionUID = -6386785421902852904L;
59 private Template baseTemplate;
61 private LinkedList<Menu> categories = new LinkedList<Menu>();
63 private HashMap<String, Page> pages = new HashMap<String, Page>();
65 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
67 private Menu rootMenu;
69 private static Gigi instance;
71 private PingerDaemon pinger;
73 private KeyStore truststore;
75 public Gigi(Properties conf, KeyStore truststore) {
76 if (instance != null) {
77 throw new IllegalStateException("Multiple Gigi instances!");
80 DatabaseConnection.init(conf);
81 this.truststore = truststore;
82 pinger = new PingerDaemon(truststore);
87 public void init() throws ServletException {
88 if ( !firstInstanceInited) {
89 putPage("/error", new PageNotFound(), null);
90 putPage("/login", new LoginPage("CAcert - Login"), "Join CAcert.org");
91 putPage("/", new MainPage("CAcert - Home"), null);
92 putPage("/roots", new RootCertPage(truststore), "Join CAcert.org");
93 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
94 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
95 putPage("/secure", new TestSecure(), null);
96 putPage(Verify.PATH, new Verify(), null);
97 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
98 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
99 putPage(MyDetails.PATH, new MyDetails(), "My Account");
100 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
101 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
102 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
103 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
104 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
105 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
106 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
107 rootMenu = new Menu("Main");
108 Menu about = new Menu("About CAcert.org");
109 categories.add(about);
111 about.addItem(new SimpleMenuItem("//blog.cacert.org/", "CAcert News"));
112 about.addItem(new SimpleMenuItem("//wiki.cacert.org/", "Wiki Documentation"));
113 putPage(PolicyIndex.DEFAULT_PATH, new PolicyIndex(), "About CAcert.org");
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 Menu languages = new Menu("Translations");
121 for (Locale l : Language.getSupportedLocales()) {
122 languages.addItem(new SimpleMenuItem("?lang=" + l.toString(), l.getDisplayName(l)));
124 categories.add(languages);
125 for (Menu menu : categories) {
127 rootMenu.addItem(menu);
131 firstInstanceInited = true;
136 private void putPage(String path, Page p, String category) {
138 reveresePages.put(p, path);
139 if (category == null) {
143 for (Menu menu : categories) {
144 if (menu.getMenuName().equals(category)) {
150 m = new Menu(category);
153 m.addItem(new PageMenuItem(p));
157 private static String staticTemplateVarHttp;
159 private static String staticTemplateVarHttps;
161 private static String getStaticTemplateVar(boolean https) {
163 if (staticTemplateVarHttps == null) {
164 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
166 return staticTemplateVarHttps;
168 if (staticTemplateVarHttp == null) {
169 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
171 return staticTemplateVarHttp;
176 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
177 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
178 addXSSHeaders(resp, isSecure);
179 // if (req.getHeader("Origin") != null) {
180 // resp.getWriter().println("No cross domain access allowed.");
183 HttpSession hs = req.getSession();
184 if (req.getParameter("lang") != null) {
185 Locale l = Language.getLocaleFromString(req.getParameter("lang"));
186 Language lu = Language.getInstance(l);
187 req.getSession().setAttribute(Language.SESSION_ATTRIB_NAME, lu.getLocale());
189 final Page p = getPage(req.getPathInfo());
192 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
193 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
196 User currentPageUser = LoginPage.getUser(req);
197 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
198 String request = req.getPathInfo();
199 request = request.split("\\?")[0];
200 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
201 resp.sendRedirect("/login");
204 if (p.beforeTemplate(req, resp)) {
207 HashMap<String, Object> vars = new HashMap<String, Object>();
209 resp.setContentType("text/html; charset=utf-8");
210 Outputable content = new Outputable() {
213 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
215 if (req.getMethod().equals("POST")) {
216 if (req.getQueryString() != null) {
223 } catch (CSRFException err) {
225 resp.sendError(500, "CSRF invalid");
226 } catch (IOException e) {
229 } catch (IOException e) {
235 vars.put(Menu.USER_VALUE, currentPageUser);
236 vars.put("menu", rootMenu);
237 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
238 vars.put("static", getStaticTemplateVar(isSecure));
239 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
240 vars.put("content", content);
241 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
243 resp.sendError(404, "Page not found.");
248 private Page getPage(String pathInfo) {
249 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
250 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
252 Page page = pages.get(pathInfo);
256 page = pages.get(pathInfo + "/*");
260 int idx = pathInfo.lastIndexOf('/');
261 pathInfo = pathInfo.substring(0, idx);
263 page = pages.get(pathInfo + "/*");
271 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
272 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
273 hsr.addHeader("Access-Control-Max-Age", "60");
275 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
277 hsr.addHeader("Content-Security-Policy", getHttpCSP());
279 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
283 private static String httpsCSP = null;
285 private static String httpCSP = null;
287 private static String getHttpsCSP() {
288 if (httpsCSP == null) {
289 StringBuffer csp = new StringBuffer();
290 csp.append("default-src 'none'");
291 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
292 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
293 csp.append(";media-src 'none'; object-src 'none'");
294 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
295 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
296 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
297 csp.append(";report-url https://api.cacert.org/security/csp/report");
298 httpsCSP = csp.toString();
303 private static String getHttpCSP() {
304 if (httpCSP == null) {
305 StringBuffer csp = new StringBuffer();
306 csp.append("default-src 'none'");
307 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
308 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
309 csp.append(";media-src 'none'; object-src 'none'");
310 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
311 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
312 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
313 csp.append(";report-url http://api.cacert.org/security/csp/report");
314 httpCSP = csp.toString();
319 public static String getPathByPage(Page p) {
320 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
323 public static void notifyPinger() {
324 instance.pinger.interrupt();