1 package org.cacert.gigi;
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Calendar;
6 import java.util.HashMap;
7 import java.util.LinkedList;
9 import java.util.Properties;
11 import javax.servlet.ServletException;
12 import javax.servlet.http.HttpServlet;
13 import javax.servlet.http.HttpServletRequest;
14 import javax.servlet.http.HttpServletResponse;
15 import javax.servlet.http.HttpSession;
17 import org.cacert.gigi.database.DatabaseConnection;
18 import org.cacert.gigi.localisation.Language;
19 import org.cacert.gigi.output.Form.CSRFException;
20 import org.cacert.gigi.output.Menu;
21 import org.cacert.gigi.output.Outputable;
22 import org.cacert.gigi.output.PageMenuItem;
23 import org.cacert.gigi.output.template.Template;
24 import org.cacert.gigi.pages.LoginPage;
25 import org.cacert.gigi.pages.LogoutPage;
26 import org.cacert.gigi.pages.MainPage;
27 import org.cacert.gigi.pages.Page;
28 import org.cacert.gigi.pages.StaticPage;
29 import org.cacert.gigi.pages.TestSecure;
30 import org.cacert.gigi.pages.Verify;
31 import org.cacert.gigi.pages.account.CertificateAdd;
32 import org.cacert.gigi.pages.account.Certificates;
33 import org.cacert.gigi.pages.account.ChangePasswordPage;
34 import org.cacert.gigi.pages.account.DomainOverview;
35 import org.cacert.gigi.pages.account.MailOverview;
36 import org.cacert.gigi.pages.account.MyDetails;
37 import org.cacert.gigi.pages.error.PageNotFound;
38 import org.cacert.gigi.pages.main.RegisterPage;
39 import org.cacert.gigi.pages.wot.AssurePage;
40 import org.cacert.gigi.pages.wot.MyPoints;
41 import org.cacert.gigi.ping.PingerDaemon;
42 import org.cacert.gigi.util.ServerConstants;
44 public class Gigi extends HttpServlet {
46 private boolean firstInstanceInited = false;
48 public static final String LOGGEDIN = "loggedin";
50 public static final String USER = "user";
52 private static final long serialVersionUID = -6386785421902852904L;
54 private Template baseTemplate;
56 private LinkedList<Menu> categories = new LinkedList<Menu>();
58 private HashMap<String, Page> pages = new HashMap<String, Page>();
60 private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
62 private Menu rootMenu;
64 private static Gigi instance;
66 private PingerDaemon pinger = new PingerDaemon();
68 public Gigi(Properties conf) {
69 if (instance != null) {
70 throw new IllegalStateException("Multiple Gigi instances!");
73 DatabaseConnection.init(conf);
78 public void init() throws ServletException {
79 if ( !firstInstanceInited) {
80 putPage("/error", new PageNotFound(), null);
81 putPage("/login", new LoginPage("CAcert - Login"), "Join CAcert.org");
82 putPage("/", new MainPage("CAcert - Home"), null);
83 putPage(ChangePasswordPage.PATH, new ChangePasswordPage(), "My Account");
84 putPage(LogoutPage.PATH, new LogoutPage("Logout"), "My Account");
85 putPage("/secure", new TestSecure(), null);
86 putPage(Verify.PATH, new Verify(), null);
87 putPage(AssurePage.PATH + "/*", new AssurePage(), "CAcert Web of Trust");
88 putPage(Certificates.PATH + "/*", new Certificates(), "Certificates");
89 putPage(MyDetails.PATH, new MyDetails(), "My Account");
90 putPage(RegisterPage.PATH, new RegisterPage(), "Join CAcert.org");
91 putPage(CertificateAdd.PATH, new CertificateAdd(), "Certificates");
92 putPage(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"), "Certificates");
93 putPage(DomainOverview.PATH + "*", new DomainOverview("Domains"), "Certificates");
94 putPage(MyPoints.PATH, new MyPoints("My Points"), "CAcert Web of Trust");
95 putPage("/wot/rules", new StaticPage("CAcert Web of Trust Rules", AssurePage.class.getResourceAsStream("Rules.templ")), "CAcert Web of Trust");
96 baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
97 rootMenu = new Menu("Main");
98 for (Menu menu : categories) {
100 rootMenu.addItem(menu);
103 firstInstanceInited = true;
108 private void putPage(String path, Page p, String category) {
110 reveresePages.put(p, path);
111 if (category == null) {
115 for (Menu menu : categories) {
116 if (menu.getMenuName().equals(category)) {
122 m = new Menu(category);
125 m.addItem(new PageMenuItem(p));
129 private static String staticTemplateVarHttp;
131 private static String staticTemplateVarHttps;
133 private static String getStaticTemplateVar(boolean https) {
135 if (staticTemplateVarHttps == null) {
136 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
138 return staticTemplateVarHttps;
140 if (staticTemplateVarHttp == null) {
141 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
143 return staticTemplateVarHttp;
148 protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
149 boolean isSecure = req.getServerPort() == ServerConstants.getSecurePort();
150 addXSSHeaders(resp, isSecure);
151 // if (req.getHeader("Origin") != null) {
152 // resp.getWriter().println("No cross domain access allowed.");
155 HttpSession hs = req.getSession();
156 final Page p = getPage(req.getPathInfo());
159 if (!isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
160 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
163 User currentPageUser = LoginPage.getUser(req);
164 if ( !p.isPermitted(currentPageUser) && hs.getAttribute("loggedin") == null) {
165 String request = req.getPathInfo();
166 request = request.split("\\?")[0];
167 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
168 resp.sendRedirect("/login");
171 if (p.beforeTemplate(req, resp)) {
174 HashMap<String, Object> vars = new HashMap<String, Object>();
176 resp.setContentType("text/html; charset=utf-8");
177 Outputable content = new Outputable() {
180 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
182 if (req.getMethod().equals("POST")) {
183 if (req.getQueryString() != null) {
190 } catch (CSRFException err) {
192 resp.sendError(500, "CSRF invalid");
193 } catch (IOException e) {
196 } catch (IOException e) {
202 vars.put(Menu.USER_VALUE, currentPageUser);
203 vars.put("menu", rootMenu);
204 vars.put("title", Page.getLanguage(req).getTranslation(p.getTitle()));
205 vars.put("static", getStaticTemplateVar(isSecure));
206 vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
207 vars.put("content", content);
208 baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
210 resp.sendError(404, "Page not found.");
215 private Page getPage(String pathInfo) {
216 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
217 pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
219 Page page = pages.get(pathInfo);
223 page = pages.get(pathInfo + "/*");
227 int idx = pathInfo.lastIndexOf('/');
228 pathInfo = pathInfo.substring(0, idx);
230 page = pages.get(pathInfo + "/*");
238 public static void addXSSHeaders(HttpServletResponse hsr, boolean doHttps) {
239 hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePortSecure() + " https://" + ServerConstants.getSecureHostNamePort());
240 hsr.addHeader("Access-Control-Max-Age", "60");
242 hsr.addHeader("Content-Security-Policy", getHttpsCSP());
244 hsr.addHeader("Content-Security-Policy", getHttpCSP());
246 hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
250 private static String httpsCSP = null;
252 private static String httpCSP = null;
254 private static String getHttpsCSP() {
255 if (httpsCSP == null) {
256 StringBuffer csp = new StringBuffer();
257 csp.append("default-src 'none'");
258 csp.append(";font-src https://" + ServerConstants.getStaticHostNamePortSecure());
259 csp.append(";img-src https://" + ServerConstants.getStaticHostNamePortSecure());
260 csp.append(";media-src 'none'; object-src 'none'");
261 csp.append(";script-src https://" + ServerConstants.getStaticHostNamePortSecure());
262 csp.append(";style-src https://" + ServerConstants.getStaticHostNamePortSecure());
263 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePortSecure());
264 csp.append(";report-url https://api.cacert.org/security/csp/report");
265 httpsCSP = csp.toString();
270 private static String getHttpCSP() {
271 if (httpCSP == null) {
272 StringBuffer csp = new StringBuffer();
273 csp.append("default-src 'none'");
274 csp.append(";font-src http://" + ServerConstants.getStaticHostNamePort());
275 csp.append(";img-src http://" + ServerConstants.getStaticHostNamePort());
276 csp.append(";media-src 'none'; object-src 'none'");
277 csp.append(";script-src http://" + ServerConstants.getStaticHostNamePort());
278 csp.append(";style-src http://" + ServerConstants.getStaticHostNamePort());
279 csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
280 csp.append(";report-url http://api.cacert.org/security/csp/report");
281 httpCSP = csp.toString();
286 public static String getPathByPage(Page p) {
287 return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");