[UPDATE-CONFIG] Use 3 hosts www, secure and static.
[gigi.git] / src / org / cacert / gigi / Gigi.java
1 package org.cacert.gigi;
2
3 import java.io.BufferedReader;
4 import java.io.File;
5 import java.io.FileInputStream;
6 import java.io.IOException;
7 import java.io.InputStreamReader;
8 import java.util.Calendar;
9 import java.util.HashMap;
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.email.EmailProvider;
20 import org.cacert.gigi.pages.LoginPage;
21 import org.cacert.gigi.pages.MainPage;
22 import org.cacert.gigi.pages.Page;
23 import org.cacert.gigi.pages.TestSecure;
24 import org.cacert.gigi.pages.Verify;
25 import org.cacert.gigi.pages.account.MailAdd;
26 import org.cacert.gigi.pages.account.MailCertificates;
27 import org.cacert.gigi.pages.account.MailOverview;
28 import org.cacert.gigi.pages.account.MyDetails;
29 import org.cacert.gigi.pages.main.RegisterPage;
30 import org.cacert.gigi.pages.wot.AssurePage;
31 import org.cacert.gigi.util.ServerConstants;
32 import org.eclipse.jetty.util.log.Log;
33
34 public class Gigi extends HttpServlet {
35         public static final String LOGGEDIN = "loggedin";
36         public static final String USER = "user";
37         private static final long serialVersionUID = -6386785421902852904L;
38         private String[] baseTemplate;
39         private HashMap<String, Page> pages = new HashMap<String, Page>();
40
41         public Gigi(Properties conf) {
42                 EmailProvider.init(conf);
43                 DatabaseConnection.init(conf);
44         }
45         @Override
46         public void init() throws ServletException {
47                 pages.put("/login", new LoginPage("CACert - Login"));
48                 pages.put("/", new MainPage("CACert - Home"));
49                 pages.put("/secure", new TestSecure());
50                 pages.put(Verify.PATH, new Verify());
51                 pages.put(AssurePage.PATH + "/*", new AssurePage());
52                 pages.put(MailCertificates.PATH, new MailCertificates());
53                 pages.put(MyDetails.PATH, new MyDetails());
54                 pages.put(RegisterPage.PATH, new RegisterPage());
55                 pages.put(MailOverview.DEFAULT_PATH, new MailOverview(
56                                 "My email addresses"));
57                 pages.put(MailAdd.DEFAULT_PATH, new MailAdd("Add new email"));
58                 String templ = "";
59                 try (BufferedReader reader = new BufferedReader(new InputStreamReader(
60                                 new FileInputStream(new File("templates/base.html"))))) {
61                         String tmp;
62                         while ((tmp = reader.readLine()) != null) {
63                                 templ += tmp + "\n";
64                         }
65                         baseTemplate = templ.split("\\$content\\$");
66                 } catch (Exception e) {
67                         Log.getLogger(Gigi.class).warn("Error loading template!", e);
68                 }
69                 super.init();
70
71         }
72         @Override
73         protected void service(HttpServletRequest req, HttpServletResponse resp)
74                         throws ServletException, IOException {
75                 addXSSHeaders(resp);
76                 if (req.getHeader("Origin") != null) {
77                         resp.getWriter().println("No cross domain access allowed.");
78                         return;
79                 }
80                 HttpSession hs = req.getSession();
81                 if (req.getPathInfo() != null && req.getPathInfo().equals("/logout")) {
82                         if (hs != null) {
83                                 hs.setAttribute(LOGGEDIN, null);
84                                 hs.invalidate();
85                         }
86                         resp.sendRedirect("/");
87                         return;
88                 }
89
90                 Page p = getPage(req.getPathInfo());
91                 if (p != null) {
92
93                         if (p.needsLogin() && hs.getAttribute("loggedin") == null) {
94                                 String request = req.getPathInfo();
95                                 request = request.split("\\?")[0];
96                                 hs.setAttribute(LoginPage.LOGIN_RETURNPATH, request);
97                                 resp.sendRedirect("/login");
98                                 return;
99                         }
100                         if (p.beforeTemplate(req, resp)) {
101                                 return;
102                         }
103
104                         String b0 = baseTemplate[0];
105                         b0 = makeDynTempl(b0, p);
106                         resp.setContentType("text/html; charset=utf-8");
107                         resp.getWriter().print(b0);
108                         if (req.getMethod().equals("POST")) {
109                                 p.doPost(req, resp);
110                         } else {
111                                 p.doGet(req, resp);
112                         }
113                         String b1 = baseTemplate[1];
114                         b1 = makeDynTempl(b1, p);
115                         resp.getWriter().print(b1);
116                 } else {
117                         resp.sendError(404, "Page not found.");
118                 }
119
120         }
121         private Page getPage(String pathInfo) {
122                 if (pathInfo.endsWith("/") && !pathInfo.equals("/")) {
123                         pathInfo = pathInfo.substring(0, pathInfo.length() - 1);
124                 }
125                 Page page = pages.get(pathInfo);
126                 if (page != null) {
127                         return page;
128                 }
129                 page = pages.get(pathInfo + "/*");
130                 if (page != null) {
131                         return page;
132                 }
133                 int idx = pathInfo.lastIndexOf('/');
134                 pathInfo = pathInfo.substring(0, idx);
135
136                 page = pages.get(pathInfo + "/*");
137                 if (page != null) {
138                         return page;
139                 }
140                 return null;
141
142         }
143         private String makeDynTempl(String in, Page p) {
144                 int year = Calendar.getInstance().get(Calendar.YEAR);
145                 in = in.replaceAll("\\$title\\$", p.getTitle());
146                 in = in.replaceAll("\\$year\\$", year + "");
147                 return in;
148         }
149         public static void addXSSHeaders(HttpServletResponse hsr) {
150                 hsr.addHeader("Access-Control-Allow-Origin",
151                                 "http://cacert.org https://localhost");
152                 hsr.addHeader("Access-Control-Max-Age", "60");
153                 hsr.addHeader("Content-Security-Policy", "default-src 'self' https://"
154                                 + ServerConstants.getStaticHostNamePort()
155                                 + " https://www.cacert.org/*;frame-ancestors 'none'");
156                 // ;report-uri https://felix.dogcraft.de/report.php
157
158         }
159 }