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