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