]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Gigi.java
20a2b1ca3e72d7ab323a90cad3df42ca095182a4
[gigi.git] / src / org / cacert / gigi / Gigi.java
1 package org.cacert.gigi;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.util.Calendar;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.Properties;
9
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 import javax.servlet.http.HttpSession;
15
16 import org.cacert.gigi.database.DatabaseConnection;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.output.Form.CSRFException;
19 import org.cacert.gigi.output.Menu;
20 import org.cacert.gigi.output.MenuItem;
21 import org.cacert.gigi.output.Outputable;
22 import org.cacert.gigi.output.template.Template;
23 import org.cacert.gigi.pages.LoginPage;
24 import org.cacert.gigi.pages.MainPage;
25 import org.cacert.gigi.pages.Page;
26 import org.cacert.gigi.pages.TestSecure;
27 import org.cacert.gigi.pages.Verify;
28 import org.cacert.gigi.pages.account.CertificateAdd;
29 import org.cacert.gigi.pages.account.Certificates;
30 import org.cacert.gigi.pages.account.ChangePasswordPage;
31 import org.cacert.gigi.pages.account.DomainOverview;
32 import org.cacert.gigi.pages.account.MailOverview;
33 import org.cacert.gigi.pages.account.MyDetails;
34 import org.cacert.gigi.pages.error.PageNotFound;
35 import org.cacert.gigi.pages.main.RegisterPage;
36 import org.cacert.gigi.pages.wot.AssurePage;
37 import org.cacert.gigi.util.ServerConstants;
38
39 public class Gigi extends HttpServlet {
40
41     public static final String LOGGEDIN = "loggedin";
42
43     public static final String USER = "user";
44
45     private static final long serialVersionUID = -6386785421902852904L;
46
47     private Template baseTemplate;
48
49     private HashMap<String, Page> pages = new HashMap<String, Page>();
50
51     Menu m;
52
53     public Gigi(Properties conf) {
54         DatabaseConnection.init(conf);
55     }
56
57     @Override
58     public void init() throws ServletException {
59         pages.put("/error", new PageNotFound());
60         pages.put("/login", new LoginPage("CACert - Login"));
61         pages.put("/", new MainPage("CACert - Home"));
62         pages.put("/secure", new TestSecure());
63         pages.put(Verify.PATH, new Verify());
64         pages.put(AssurePage.PATH + "/*", new AssurePage());
65         pages.put(Certificates.PATH + "/*", new Certificates());
66         pages.put(MyDetails.PATH, new MyDetails());
67         pages.put(ChangePasswordPage.PATH, new ChangePasswordPage());
68         pages.put(RegisterPage.PATH, new RegisterPage());
69         pages.put(CertificateAdd.PATH, new CertificateAdd());
70         pages.put(MailOverview.DEFAULT_PATH, new MailOverview("My email addresses"));
71         pages.put(DomainOverview.PATH, new DomainOverview("Domains"));
72         baseTemplate = new Template(Gigi.class.getResource("Gigi.templ"));
73         m = new Menu("Certificates", "cert", new MenuItem(MailOverview.DEFAULT_PATH, "Emails"), new MenuItem("", "Client Certificates"), new MenuItem("", "Domains"), new MenuItem("", "Server Certificates"));
74         super.init();
75
76     }
77
78     @Override
79     protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
80         addXSSHeaders(resp);
81         // if (req.getHeader("Origin") != null) {
82         // resp.getWriter().println("No cross domain access allowed.");
83         // return;
84         // }
85         HttpSession hs = req.getSession();
86         if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
87             if (hs != null) {
88                 hs.setAttribute(LOGGEDIN, null);
89                 hs.invalidate();
90             }
91             resp.sendRedirect("/");
92             return;
93         }
94
95         final Page p = getPage(req.getPathInfo());
96         if (p != null) {
97
98             if ( !p.isPermitted(LoginPage.getUser(req)) && hs.getAttribute("loggedin") == null) {
99                 String request = req.getPathInfo();
100                 request = request.split("\\?")[0];
101                 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
102                 resp.sendRedirect("/login");
103                 return;
104             }
105             if (p.beforeTemplate(req, resp)) {
106                 return;
107             }
108             HashMap<String, Object> vars = new HashMap<String, Object>();
109
110             resp.setContentType("text/html; charset=utf-8");
111             Outputable content = new Outputable() {
112
113                 @Override
114                 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
115                     try {
116                         if (req.getMethod().equals("POST")) {
117                             if (req.getQueryString() != null) {
118                                 return;
119                             }
120                             p.doPost(req, resp);
121                         } else {
122                             p.doGet(req, resp);
123                         }
124                     } catch (CSRFException err) {
125                         try {
126                             resp.sendError(500, "CSRF invalid");
127                         } catch (IOException e) {
128                             e.printStackTrace();
129                         }
130                     } catch (IOException e) {
131                         e.printStackTrace();
132                     }
133
134                 }
135             };
136             vars.put("menu", m);
137             vars.put("title", p.getTitle());
138             vars.put("static", ServerConstants.getStaticHostNamePort());
139             vars.put("year", Calendar.getInstance().get(Calendar.YEAR));
140             vars.put("content", content);
141             baseTemplate.output(resp.getWriter(), Page.getLanguage(req), vars);
142         } else {
143             resp.sendError(404, "Page not found.");
144         }
145
146     }
147
148     private Page getPage(String pathInfo) {
149         if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
150             pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
151         }
152         Page page = pages.get(pathInfo);
153         if (page != null) {
154             return page;
155         }
156         page = pages.get(pathInfo + "/*");
157         if (page != null) {
158             return page;
159         }
160         int idx = pathInfo.lastIndexOf('/');
161         pathInfo = pathInfo.substring(0, idx);
162
163         page = pages.get(pathInfo + "/*");
164         if (page != null) {
165             return page;
166         }
167         return null;
168
169     }
170
171     public static void addXSSHeaders(HttpServletResponse hsr) {
172         hsr.addHeader("Access-Control-Allow-Origin", "https://" + ServerConstants.getWwwHostNamePort() + " https://" + ServerConstants.getSecureHostNamePort());
173         hsr.addHeader("Access-Control-Max-Age", "60");
174
175         hsr.addHeader("Content-Security-Policy", getDefaultCSP());
176         hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
177
178     }
179
180     private static String defaultCSP = null;
181
182     private static String getDefaultCSP() {
183         if (defaultCSP == null) {
184             StringBuffer csp = new StringBuffer();
185             csp.append("default-src 'none';");
186             csp.append("font-src https://" + ServerConstants.getStaticHostNamePort());
187             csp.append(";img-src https://" + ServerConstants.getStaticHostNamePort());
188             csp.append(";media-src 'none'; object-src 'none';");
189             csp.append("script-src https://" + ServerConstants.getStaticHostNamePort());
190             csp.append(";style-src https://" + ServerConstants.getStaticHostNamePort());
191             csp.append(";form-action https://" + ServerConstants.getSecureHostNamePort() + " https://" + ServerConstants.getWwwHostNamePort());
192             csp.append("report-url https://api.cacert.org/security/csp/report");
193             defaultCSP = csp.toString();
194         }
195         return defaultCSP;
196     }
197 }