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.template.Template;
25 import org.cacert.gigi.pages.LoginPage;
26 import org.cacert.gigi.pages.LogoutPage;
27 import org.cacert.gigi.pages.MainPage;
28 import org.cacert.gigi.pages.Page;
29 import org.cacert.gigi.pages.RootCertPage;
30 import org.cacert.gigi.pages.StaticPage;
31 import org.cacert.gigi.pages.TestSecure;
32 import org.cacert.gigi.pages.Verify;
33 import org.cacert.gigi.pages.account.CertificateAdd;
34 import org.cacert.gigi.pages.account.Certificates;
35 import org.cacert.gigi.pages.account.ChangePasswordPage;
36 import org.cacert.gigi.pages.account.DomainOverview;
37 import org.cacert.gigi.pages.account.MailOverview;
38 import org.cacert.gigi.pages.account.MyDetails;
39 import org.cacert.gigi.pages.error.PageNotFound;
40 import org.cacert.gigi.pages.main.RegisterPage;
41 import org.cacert.gigi.pages.wot.AssurePage;
42 import org.cacert.gigi.pages.wot.MyPoints;
43 import org.cacert.gigi.ping.PingerDaemon;
44 import org.cacert.gigi.util.ServerConstants;
46 public class Gigi extends HttpServlet {
48 private boolean firstInstanceInited = false;
50 public static final String LOGGEDIN = "loggedin";
52 public static final String USER = "user";
54 private static final long serialVersionUID = -6386785421902852904L;
56 private Template baseTemplate;
58 private LinkedList<Menu> categories = new LinkedList<Menu>();
60 private HashMap<String, Page> pages = new HashMap<String, Page>();
62 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
64 private Menu rootMenu;
66 private static Gigi instance;
68 private PingerDaemon pinger;
70 private KeyStore truststore;
72 public Gigi(Properties conf, KeyStore truststore) {
73 if (instance != null) {
74 throw new IllegalStateException("Multiple Gigi instances!");
77 DatabaseConnection.init(conf);
78 this.truststore = truststore;
79 pinger = new PingerDaemon(truststore);
84 public void init() throws ServletException {
85 if ( !firstInstanceInited) {
86 putPage("/error", new PageNotFound(), null);
87 putPage("/login", new LoginPage("CAcert - Login"), "Join CAcert.org");
88 putPage("/", new MainPage("CAcert - Home"), null);
89 putPage("/roots", new RootCertPage(truststore), "Join CAcert.org");
90 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
91 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
92 putPage("/secure", new TestSecure(), null);
93 putPage(Verify.PATH, new Verify(), null);
94 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
95 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
96 putPage(MyDetails.PATH, new MyDetails(), "My Account");
97 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
98 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
99 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
100 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
101 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
102 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
103 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
104 rootMenu = new Menu("Main");
105 for (Menu menu : categories) {
107 rootMenu.addItem(menu);
110 firstInstanceInited = true;
115 private void putPage(String path, Page p, String category) {
117 reveresePages.put(p, path);
118 if (category == null) {
122 for (Menu menu : categories) {
123 if (menu.getMenuName().equals(category)) {
129 m = new Menu(category);
132 m.addItem(new PageMenuItem(p));
136 private static String staticTemplateVarHttp;
138 private static String staticTemplateVarHttps;
140 private static String getStaticTemplateVar(boolean https) {
142 if (staticTemplateVarHttps == null) {
143 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
145 return staticTemplateVarHttps;
147 if (staticTemplateVarHttp == null) {
148 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
150 return staticTemplateVarHttp;
155 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
156 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
157 addXSSHeaders(resp, isSecure);
158 // if (req.getHeader("Origin") != null) {
159 // resp.getWriter().println("No cross domain access allowed.");
162 HttpSession hs = req.getSession();
163 final Page p = getPage(req.getPathInfo());
166 if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
167 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
170 User currentPageUser = LoginPage.getUser(req);
171 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
172 String request = req.getPathInfo();
173 request = request.split("\\?")[0];
174 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
175 resp.sendRedirect("/login");
178 if (p.beforeTemplate(req, resp)) {
181 HashMap<String, Object> vars = new HashMap<String, Object>();
183 resp.setContentType("text/html; charset=utf-8");
184 Outputable content = new Outputable() {
187 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
189 if (req.getMethod().equals("POST")) {
190 if (req.getQueryString() != null) {
197 } catch (CSRFException err) {
199 resp.sendError(500, "CSRF invalid");
200 } catch (IOException e) {
203 } catch (IOException e) {
209 vars.put(Menu.USER_VALUE, currentPageUser);
210 vars.put("menu", rootMenu);
211 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
212 vars.put("static", getStaticTemplateVar(isSecure));
213 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
214 vars.put("content", content);
215 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
217 resp.sendError(404, "Page not found.");
222 private Page getPage(String pathInfo) {
223 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
224 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
226 Page page = pages.get(pathInfo);
230 page = pages.get(pathInfo + "/*");
234 int idx = pathInfo.lastIndexOf('/');
235 pathInfo = pathInfo.substring(0, idx);
237 page = pages.get(pathInfo + "/*");
245 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
246 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
247 hsr.addHeader("Access-Control-Max-Age", "60");
249 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
251 hsr.addHeader("Content-Security-Policy", getHttpCSP());
253 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
257 private static String httpsCSP = null;
259 private static String httpCSP = null;
261 private static String getHttpsCSP() {
262 if (httpsCSP == null) {
263 StringBuffer csp = new StringBuffer();
264 csp.append("default-src 'none'");
265 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
266 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
267 csp.append(";media-src 'none'; object-src 'none'");
268 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
269 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
270 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
271 csp.append(";report-url https://api.cacert.org/security/csp/report");
272 httpsCSP = csp.toString();
277 private static String getHttpCSP() {
278 if (httpCSP == null) {
279 StringBuffer csp = new StringBuffer();
280 csp.append("default-src 'none'");
281 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
282 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
283 csp.append(";media-src 'none'; object-src 'none'");
284 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
285 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
286 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
287 csp.append(";report-url http://api.cacert.org/security/csp/report");
288 httpCSP = csp.toString();
293 public static String getPathByPage(Page p) {
294 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
297 public static void notifyPinger() {
298 instance.pinger.interrupt();