upd: get default variables into outputables for error messages
[gigi.git] / util-testing / club / wpia / gigi / pages / Manager.java
1 package club.wpia.gigi.pages;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.lang.reflect.Field;
6 import java.security.GeneralSecurityException;
7 import java.security.KeyPair;
8 import java.security.KeyPairGenerator;
9 import java.security.Signature;
10 import java.text.SimpleDateFormat;
11 import java.util.Base64;
12 import java.util.Calendar;
13 import java.util.Date;
14 import java.util.GregorianCalendar;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.LinkedList;
18 import java.util.List;
19 import java.util.Locale;
20 import java.util.Map;
21 import java.util.Properties;
22 import java.util.Random;
23 import java.util.TreeSet;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
29
30 import club.wpia.gigi.Gigi;
31 import club.wpia.gigi.GigiApiException;
32 import club.wpia.gigi.crypto.SPKAC;
33 import club.wpia.gigi.database.GigiPreparedStatement;
34 import club.wpia.gigi.dbObjects.CATS;
35 import club.wpia.gigi.dbObjects.CATS.CATSType;
36 import club.wpia.gigi.dbObjects.Certificate;
37 import club.wpia.gigi.dbObjects.Certificate.CertificateStatus;
38 import club.wpia.gigi.dbObjects.CertificateOwner;
39 import club.wpia.gigi.dbObjects.Country;
40 import club.wpia.gigi.dbObjects.Digest;
41 import club.wpia.gigi.dbObjects.Domain;
42 import club.wpia.gigi.dbObjects.DomainPingType;
43 import club.wpia.gigi.dbObjects.EmailAddress;
44 import club.wpia.gigi.dbObjects.Group;
45 import club.wpia.gigi.dbObjects.NamePart;
46 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
47 import club.wpia.gigi.dbObjects.User;
48 import club.wpia.gigi.dbObjects.Verification.VerificationType;
49 import club.wpia.gigi.email.DelegateMailProvider;
50 import club.wpia.gigi.email.EmailProvider;
51 import club.wpia.gigi.localisation.Language;
52 import club.wpia.gigi.output.template.IterableDataset;
53 import club.wpia.gigi.output.template.Template;
54 import club.wpia.gigi.pages.account.certs.CertificateRequest;
55 import club.wpia.gigi.ping.DomainPinger;
56 import club.wpia.gigi.ping.PingerDaemon;
57 import club.wpia.gigi.util.AuthorizationContext;
58 import club.wpia.gigi.util.DayDate;
59 import club.wpia.gigi.util.DomainAssessment;
60 import club.wpia.gigi.util.HTMLEncoder;
61 import club.wpia.gigi.util.Notary;
62 import club.wpia.gigi.util.TimeConditions;
63 import sun.security.x509.X509Key;
64
65 public class Manager extends Page {
66
67     public static String validVerificationDateString() {
68         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
69         Calendar c = Calendar.getInstance();
70         c.setTimeInMillis(System.currentTimeMillis());
71         c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION + 1);
72         return sdf.format(new Date(c.getTimeInMillis()));
73     }
74
75     public static Country getRandomCountry() {
76         List<Country> cc = Country.getCountries();
77         int rnd = new Random().nextInt(cc.size());
78         return cc.get(rnd);
79     }
80
81     public static final String PATH = "/manager";
82
83     private static HashMap<DomainPingType, DomainPinger> dps;
84
85     private Manager() {
86         super("Test Manager");
87
88         try {
89             Field gigiInstance = Gigi.class.getDeclaredField("instance");
90             gigiInstance.setAccessible(true);
91             Gigi g = (Gigi) gigiInstance.get(null);
92
93             Field gigiPinger = Gigi.class.getDeclaredField("pinger");
94             gigiPinger.setAccessible(true);
95             PingerDaemon pd = (PingerDaemon) gigiPinger.get(g);
96
97             Field f = PingerDaemon.class.getDeclaredField("pingers");
98             f.setAccessible(true);
99             dps = (HashMap<DomainPingType, DomainPinger>) f.get(pd);
100             HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
101             for (DomainPingType dpt : DomainPingType.values()) {
102                 pingers.put(dpt, new PingerFetcher(dpt));
103             }
104             f.set(pd, pingers);
105         } catch (ReflectiveOperationException e) {
106             e.printStackTrace();
107         }
108     }
109
110     public User getSupporter() {
111         if (supporter != null) {
112             return supporter;
113         }
114         try {
115             User u = createAgent( -1);
116             if ( !u.isInGroup(Group.SUPPORTER)) {
117                 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
118                     ps.setInt(1, u.getId());
119                     ps.setString(2, Group.SUPPORTER.getDBName());
120                     ps.setInt(3, u.getId());
121                     ps.execute();
122                 }
123                 u.refreshGroups();
124             }
125             supporter = u;
126         } catch (ReflectiveOperationException | GigiApiException e) {
127             e.printStackTrace();
128         }
129         return supporter;
130     }
131
132     public User getAgent(int i) {
133         if (agents[i] != null) {
134             return agents[i];
135         }
136         try {
137             User u = createAgent(i);
138             agents[i] = u;
139
140         } catch (ReflectiveOperationException | GigiApiException e) {
141             e.printStackTrace();
142         }
143         return agents[i];
144     }
145
146     private User createAgent(int i) throws GigiApiException, IllegalAccessException {
147         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `country`=?")) {
148             String mail = "test-agent" + i + "@example.com";
149             User u = User.getByEmail(mail);
150             if (u == null) {
151                 System.out.println("Creating RA-Agent");
152                 createUser(mail);
153                 u = User.getByEmail(mail);
154                 passCATS(u, CATSType.AGENT_CHALLENGE);
155                 ps.setInt(1, u.getId());
156                 ps.setInt(2, u.getPreferredName().getId());
157                 ps.setInt(3, 100);
158                 ps.setString(4, "Manager init code");
159                 ps.setString(5, "1990-01-01");
160                 ps.setString(6, getRandomCountry().getCode());
161                 ps.execute();
162             }
163             return u;
164         }
165     }
166
167     private void passCATS(User u, CATSType t) {
168         CATS.enterResult(u, t, new Date(System.currentTimeMillis()), "en_EN", "1");
169     }
170
171     private static Manager instance;
172
173     private static final Template t = new Template(Manager.class.getResource("ManagerMails.templ"));
174
175     HashMap<String, LinkedList<String>> emails = new HashMap<>();
176
177     private static TreeSet<String> pingExempt = new TreeSet<>();
178
179     public static Manager getInstance() {
180         if (instance == null) {
181             instance = new Manager();
182         }
183         return instance;
184     }
185
186     public static class MailFetcher extends DelegateMailProvider {
187
188         Pattern[] toForward;
189
190         public MailFetcher(Properties props) {
191             super(props, props.getProperty("emailProvider.manager.target"));
192             String str = props.getProperty("emailProvider.manager.filter");
193             if (str == null) {
194                 toForward = new Pattern[0];
195             } else {
196                 String[] parts = str.split(" ");
197                 toForward = new Pattern[parts.length];
198                 for (int i = 0; i < parts.length; i++) {
199                     toForward[i] = Pattern.compile(parts[i]);
200                 }
201             }
202         }
203
204         @Override
205         public String checkEmailServer(int forUid, String address) throws IOException {
206             return OK;
207         }
208
209         @Override
210         public synchronized void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
211             HashMap<String, LinkedList<String>> mails = Manager.getInstance().emails;
212             LinkedList<String> hismails = mails.get(to);
213             if (hismails == null) {
214                 mails.put(to, hismails = new LinkedList<>());
215             }
216             hismails.addFirst(subject + "\n" + message);
217             for (int i = 0; i < toForward.length; i++) {
218                 if (toForward[i].matcher(to).matches()) {
219                     super.sendMail(to, subject, message, replyto, toname, fromname, errorsto, extra);
220                     return;
221                 }
222             }
223         }
224
225     }
226
227     public class PingerFetcher extends DomainPinger {
228
229         private DomainPingType dpt;
230
231         public PingerFetcher(DomainPingType dpt) {
232             this.dpt = dpt;
233         }
234
235         @Override
236         public void ping(Domain domain, String configuration, CertificateOwner target, int confId) {
237             System.out.println("Test: " + domain);
238             if (pingExempt.contains(domain.getSuffix())) {
239                 enterPingResult(confId, DomainPinger.PING_SUCCEDED, "Succeeded by TestManager pass-by", null);
240             } else {
241                 dps.get(dpt).ping(domain, configuration, target, confId);
242             }
243         }
244
245     }
246
247     public void batchCreateUsers(String mailPrefix, String domain, int amount, PrintWriter out) {
248
249         try {
250             if (amount > 100) {
251                 out.print("100 at most, please.");
252                 return;
253             }
254             for (int i = 0; i < amount; i++) {
255                 String email = mailPrefix + i + "@" + domain;
256                 createUser(email);
257             }
258         } catch (ReflectiveOperationException e) {
259             out.println("failed");
260             e.printStackTrace();
261         } catch (GigiApiException e) {
262             out.println("failed: " + e.getMessage());
263             e.printStackTrace();
264         }
265     }
266
267     private void createUser(String email) throws GigiApiException, IllegalAccessException {
268         Calendar gc = GregorianCalendar.getInstance();
269         gc.setTimeInMillis(0);
270         gc.set(1990, 0, 1);
271
272         Country country = getRandomCountry();
273
274         User u = new User(email, "xvXV12°§", new DayDate(gc.getTime().getTime()), Locale.ENGLISH, country, //
275                 new NamePart(NamePartType.FIRST_NAME, "Först"), new NamePart(NamePartType.FIRST_NAME, "Müddle"), //
276                 new NamePart(NamePartType.LAST_NAME, "Läst"), new NamePart(NamePartType.SUFFIX, "Süffix"));
277         EmailAddress ea = u.getEmails()[0];
278         verify(email, ea);
279     }
280
281     private void verify(String email, EmailAddress ea) throws GigiApiException {
282         LinkedList<String> i = emails.get(email);
283         while (i.size() > 0 && !ea.isVerified()) {
284             String lst = i.getLast();
285             Pattern p = Pattern.compile("hash=([a-zA-Z0-9]+)");
286             Matcher m = p.matcher(lst);
287             if (m.find()) {
288                 ea.verify(m.group(1));
289             }
290             i.removeLast();
291         }
292         // ea.verify(hash);
293     }
294
295     User[] agents = new User[25];
296
297     User supporter;
298
299     @Override
300     public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
301         if (req.getParameter("create") != null) {
302             String prefix = req.getParameter("prefix");
303             String domain = req.getParameter("suffix");
304             try {
305                 if (null == prefix) {
306                     throw new GigiApiException("No prefix given.");
307                 }
308                 if (null == domain) {
309                     throw new GigiApiException("No domain given.");
310                 }
311
312                 DomainAssessment.checkCertifiableDomain(domain, false, true);
313
314                 if ( !EmailProvider.isValidMailAddress(prefix + "@" + domain)) {
315                     throw new GigiApiException("Invalid email address template.");
316                 }
317
318                 batchCreateUsers(prefix, domain, Integer.parseInt(req.getParameter("amount")), resp.getWriter());
319                 resp.getWriter().println("User batch created.");
320             } catch (GigiApiException e) {
321                 throw new Error(e);
322             }
323         } else if (req.getParameter("addpriv") != null || req.getParameter("delpriv") != null) {
324             User u = User.getByEmail(req.getParameter("email"));
325             if (u == null) {
326                 resp.getWriter().println("User not found.");
327                 return;
328             }
329             try {
330                 if (req.getParameter("addpriv") != null) {
331                     u.grantGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
332                     resp.getWriter().println("Privilege granted");
333                 } else {
334                     u.revokeGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
335                     resp.getWriter().println("Privilege revoked");
336                 }
337             } catch (GigiApiException e) {
338                 throw new Error(e);
339             }
340         } else if (req.getParameter("fetch") != null) {
341             String mail = req.getParameter("femail");
342             fetchMails(req, resp, mail);
343         } else if (req.getParameter("cats") != null) {
344             String mail = req.getParameter("catsEmail");
345             String testId = req.getParameter("catsType");
346             User byEmail = User.getByEmail(mail);
347             if (byEmail == null) {
348                 resp.getWriter().println("User not found.");
349                 return;
350             }
351             if (testId == null) {
352                 resp.getWriter().println("No test given.");
353                 return;
354             }
355             CATSType test = CATSType.values()[Integer.parseInt(testId)];
356             passCATS(byEmail, test);
357             resp.getWriter().println("Test '" + test.getDisplayName() + "' was added to user account.");
358         } else if (req.getParameter("verify") != null) {
359             String mail = req.getParameter("verifyEmail");
360             String verificationPoints = req.getParameter("verificationPoints");
361             User byEmail = User.getByEmail(mail);
362
363             if (byEmail == null) {
364                 resp.getWriter().println("User not found.");
365                 return;
366             }
367
368             int vp = 0;
369             int agentNumber = 0;
370
371             try {
372                 try {
373                     vp = Integer.parseInt(verificationPoints);
374                 } catch (NumberFormatException e) {
375                     throw new GigiApiException("No valid Verification Points entered.");
376                 }
377
378                 if (vp > 100) { // only allow max 100 Verification points
379                     vp = 100;
380                 }
381
382                 while (vp > 0) {
383                     int currentVP = 10;
384                     if (vp < 10) {
385                         currentVP = vp;
386                     }
387                     Notary.verify(getAgent(agentNumber), byEmail, byEmail.getPreferredName(), byEmail.getDoB(), currentVP, "Testmanager Verify up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
388                     agentNumber += 1;
389                     vp -= currentVP;
390                 }
391
392             } catch (GigiApiException e) {
393                 throw new Error(e);
394             }
395
396             resp.getWriter().println("User has been verified " + agentNumber + " times.");
397
398         } else if (req.getParameter("letverify") != null) {
399             String mail = req.getParameter("letverifyEmail");
400             User byEmail = User.getByEmail(mail);
401             if (byEmail == null || !byEmail.canVerify()) {
402                 resp.getWriter().println("User not found, or found user is not allowed to verify.");
403             } else {
404                 try {
405                     for (int i = 0; i < 25; i++) {
406                         User a = getAgent(i);
407                         Notary.verify(byEmail, a, a.getNames()[0], a.getDoB(), 10, "Testmanager exp up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
408                     }
409                     resp.getWriter().println("Successfully added experience points.");
410                 } catch (GigiApiException e) {
411                     throw new Error(e);
412                 }
413             }
414         } else if (req.getParameter("addEmail") != null) {
415             User u = User.getByEmail(req.getParameter("addEmailEmail"));
416             try {
417                 EmailAddress ea = new EmailAddress(u, req.getParameter("addEmailNew"), Locale.ENGLISH);
418                 verify(ea.getAddress(), ea);
419             } catch (IllegalArgumentException e) {
420                 e.printStackTrace();
421                 resp.getWriter().println("An internal error occured.");
422             } catch (GigiApiException e) {
423                 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH), getDefaultVars(req));
424             }
425         } else if (req.getParameter("addCert") != null) {
426             User u = User.getByEmail(req.getParameter("addCertEmail"));
427             try {
428                 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
429                 kpg.initialize(4096);
430                 KeyPair kp = kpg.generateKeyPair();
431                 SPKAC s = new SPKAC((X509Key) kp.getPublic(), "challenge");
432                 Signature sign = Signature.getInstance("SHA512withRSA");
433                 sign.initSign(kp.getPrivate());
434
435                 byte[] res = s.getEncoded(sign);
436
437                 CertificateRequest cr = new CertificateRequest(new AuthorizationContext(u, u), Base64.getEncoder().encodeToString(res), "challenge");
438                 cr.update(CertificateRequest.DEFAULT_CN, Digest.SHA512.toString(), "client", null, "", "email:" + u.getEmail());
439                 Certificate draft = cr.draft();
440                 draft.issue(null, "2y", u).waitFor(10000);
441                 if (draft.getStatus() == CertificateStatus.ISSUED) {
442                     resp.getWriter().println("added certificate");
443                 } else {
444                     resp.getWriter().println("signer failed");
445                 }
446             } catch (GeneralSecurityException e1) {
447                 e1.printStackTrace();
448                 resp.getWriter().println("error");
449             } catch (GigiApiException e) {
450                 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH), getDefaultVars(req));
451             }
452
453         } else if (req.getParameter("addExDom") != null) {
454             String dom = req.getParameter("exemptDom");
455             pingExempt.add(dom);
456             resp.getWriter().println("Updated domains exempt from pings. Current set: <br/>");
457             resp.getWriter().println(HTMLEncoder.encodeHTML(pingExempt.toString()));
458         } else if (req.getParameter("delExDom") != null) {
459             String dom = req.getParameter("exemptDom");
460             pingExempt.remove(dom);
461             resp.getWriter().println("Updated domains exempt from pings. Current set: <br/>");
462             resp.getWriter().println(HTMLEncoder.encodeHTML(pingExempt.toString()));
463         }
464         resp.getWriter().println("<br/><a href='" + PATH + "'>Go back</a>");
465     }
466
467     private void fetchMails(HttpServletRequest req, HttpServletResponse resp, String mail) throws IOException {
468         final LinkedList<String> mails = emails.get(mail);
469         HashMap<String, Object> vars = new HashMap<>();
470         vars.put("mail", mail);
471         if (mails != null) {
472             vars.put("mails", new IterableDataset() {
473
474                 Iterator<String> s = mails.iterator();
475
476                 @Override
477                 public boolean next(Language l, Map<String, Object> vars) {
478                     if ( !s.hasNext()) {
479                         return false;
480                     }
481                     vars.put("body", s.next().replaceAll("(https?://\\S+)", "<a href=\"$1\">$1</a>"));
482                     return true;
483                 }
484             });
485         }
486         t.output(resp.getWriter(), getLanguage(req), vars);
487         if (mails == null) {
488             resp.getWriter().println("No mails");
489
490         }
491     }
492
493     private static final Template form = new Template(Manager.class.getResource("Manager.templ"));
494
495     @Override
496     public boolean needsLogin() {
497         return false;
498     }
499
500     @Override
501     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
502         String pi = req.getPathInfo().substring(PATH.length());
503         if (pi.length() > 1 && pi.startsWith("/fetch-")) {
504             String mail = pi.substring(pi.indexOf('-', 2) + 1);
505             fetchMails(req, resp, mail);
506             return;
507         }
508         HashMap<String, Object> vars = new HashMap<>();
509         vars.put("cats_types", new IterableDataset() {
510
511             CATSType[] type = CATSType.values();
512
513             int i = 0;
514
515             @Override
516             public boolean next(Language l, Map<String, Object> vars) {
517                 if (i >= type.length) {
518                     return false;
519                 }
520                 CATSType t = type[i++];
521                 vars.put("id", i - 1);
522                 vars.put("name", t.getDisplayName());
523                 return true;
524             }
525         });
526
527         vars.put("testValidMonths", TimeConditions.getInstance().getTestMonths());
528         vars.put("reverificationDays", TimeConditions.getInstance().getVerificationLimitDays());
529         vars.put("verificationFreshMonths", TimeConditions.getInstance().getVerificationMonths());
530         vars.put("verificationMaxAgeMonths", TimeConditions.getInstance().getVerificationMaxAgeMonths());
531         vars.put("emailPingMonths", TimeConditions.getInstance().getEmailPingMonths());
532
533         form.output(resp.getWriter(), getLanguage(req), vars);
534     }
535 }