]> WPIA git - gigi.git/blob - src/org/cacert/gigi/Gigi.java
Move "About CAcert"-Menu to dynamic content.
[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.security.KeyStore;
6 import java.util.Calendar;
7 import java.util.HashMap;
8 import java.util.LinkedList;
9 import java.util.Map;
10 import java.util.Properties;
11
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;
17
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;
46
47 public class Gigi extends HttpServlet {
48
49     private boolean firstInstanceInited = false;
50
51     public static final String LOGGEDIN = "loggedin";
52
53     public static final String USER = "user";
54
55     private static final long serialVersionUID = -6386785421902852904L;
56
57     private Template baseTemplate;
58
59     private LinkedList<Menu> categories = new LinkedList<Menu>();
60
61     private HashMap<String, Page> pages = new HashMap<String, Page>();
62
63     private HashMap<Page, String> reveresePages = new HashMap<Page, String>();
64
65     private Menu rootMenu;
66
67     private static Gigi instance;
68
69     private PingerDaemon pinger;
70
71     private KeyStore truststore;
72
73     public Gigi(Properties conf, KeyStore truststore) {
74         if (instance != null) {
75             throw new IllegalStateException("Multiple Gigi instances!");
76         }
77         instance = this;
78         DatabaseConnection.init(conf);
79         this.truststore = truststore;
80         pinger = new PingerDaemon(truststore);
81         pinger.start();
82     }
83
84     @Override
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) {
107                 menu.prepare();
108                 rootMenu.addItem(menu);
109             }
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"));
119             about.prepare();
120             rootMenu.addItem(about);
121
122             rootMenu.prepare();
123             firstInstanceInited = true;
124         }
125         super.init();
126     }
127
128     private void putPage(String path, Page p, String category) {
129         pages.put(path, p);
130         reveresePages.put(p, path);
131         if (category == null) {
132             return;
133         }
134         Menu m = null;
135         for (Menu menu : categories) {
136             if (menu.getMenuName().equals(category)) {
137                 m = menu;
138                 break;
139             }
140         }
141         if (m == null) {
142             m = new Menu(category);
143             categories.add(m);
144         }
145         m.addItem(new PageMenuItem(p));
146
147     }
148
149     private static String staticTemplateVarHttp;
150
151     private static String staticTemplateVarHttps;
152
153     private static String getStaticTemplateVar(boolean https) {
154         if (https) {
155             if (staticTemplateVarHttps == null) {
156                 staticTemplateVarHttps = "https://" + ServerConstants.getStaticHostNamePortSecure();
157             }
158             return staticTemplateVarHttps;
159         } else {
160             if (staticTemplateVarHttp == null) {
161                 staticTemplateVarHttp = "http://" + ServerConstants.getStaticHostNamePort();
162             }
163             return staticTemplateVarHttp;
164         }
165     }
166
167     @Override
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.");
173         // return;
174         // }
175         HttpSession hs = req.getSession();
176         final Page p = getPage(req.getPathInfo());
177
178         if (p != null) {
179             if ( !isSecure && (p.needsLogin() || p instanceof LoginPage || p instanceof RegisterPage)) {
180                 resp.sendRedirect("https://" + ServerConstants.getWwwHostNamePortSecure() + req.getPathInfo());
181                 return;
182             }
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");
189                 return;
190             }
191             if (p.beforeTemplate(req, resp)) {
192                 return;
193             }
194             HashMap<String, Object> vars = new HashMap<String, Object>();
195
196             resp.setContentType("text/html; charset=utf-8");
197             Outputable content = new Outputable() {
198
199                 @Override
200                 public void output(PrintWriter out, Language l, Map<String, Object> vars) {
201                     try {
202                         if (req.getMethod().equals("POST")) {
203                             if (req.getQueryString() != null) {
204                                 return;
205                             }
206                             p.doPost(req, resp);
207                         } else {
208                             p.doGet(req, resp);
209                         }
210                     } catch (CSRFException err) {
211                         try {
212                             resp.sendError(500, "CSRF invalid");
213                         } catch (IOException e) {
214                             e.printStackTrace();
215                         }
216                     } catch (IOException e) {
217                         e.printStackTrace();
218                     }
219
220                 }
221             };
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);
229         } else {
230             resp.sendError(404, "Page not found.");
231         }
232
233     }
234
235     private Page getPage(String pathInfo) {
236         if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
237             pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
238         }
239         Page page = pages.get(pathInfo);
240         if (page != null) {
241             return page;
242         }
243         page = pages.get(pathInfo + "/*");
244         if (page != null) {
245             return page;
246         }
247         int idx = pathInfo.lastIndexOf('/');
248         pathInfo = pathInfo.substring(0, idx);
249
250         page = pages.get(pathInfo + "/*");
251         if (page != null) {
252             return page;
253         }
254         return null;
255
256     }
257
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");
261         if (doHttps) {
262             hsr.addHeader("Content-Security-Policy", getHttpsCSP());
263         } else {
264             hsr.addHeader("Content-Security-Policy", getHttpCSP());
265         }
266         hsr.addHeader("Strict-Transport-Security", "max-age=31536000");
267
268     }
269
270     private static String httpsCSP = null;
271
272     private static String httpCSP = null;
273
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();
286         }
287         return httpsCSP;
288     }
289
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();
302         }
303         return httpCSP;
304     }
305
306     public static String getPathByPage(Page p) {
307         return instance.reveresePages.get(p).replaceFirst("/?\\*$", "");
308     }
309
310     public static void notifyPinger() {
311         instance.pinger.interrupt();
312     }
313
314 }