add: handling of RA Agent Contract
[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             int agentNumber = 0;
413
414             try {
415                 try {
416                     vp = Integer.parseInt(verificationPoints);
417                 } catch (NumberFormatException e) {
418                     throw new GigiApiException("No valid Verification Points entered.");
419                 }
420
421                 if (vp > 100) { // only allow max 100 Verification points
422                     vp = 100;
423                 }
424
425                 while (vp > 0) {
426                     int currentVP = 10;
427                     if (vp < 10) {
428                         currentVP = vp;
429                     }
430                     Notary.verify(getAgent(agentNumber), byEmail, byEmail.getPreferredName(), byEmail.getDoB(), currentVP, "Testmanager Verify up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
431                     agentNumber += 1;
432                     vp -= currentVP;
433                 }
434
435             } catch (GigiApiException e) {
436                 throw new Error(e);
437             }
438
439             resp.getWriter().println("User has been verified " + agentNumber + " times.");
440
441         } else if (req.getParameter("letverify") != null) {
442             String mail = req.getParameter("letverifyEmail");
443             User byEmail = User.getByEmail(mail);
444             if (byEmail == null || !byEmail.canVerify()) {
445                 resp.getWriter().println("User not found, or found user is not allowed to verify.");
446             } else {
447                 try {
448                     for (int i = 0; i < 25; i++) {
449                         User a = getAgent(i);
450                         Notary.verify(byEmail, a, a.getNames()[0], a.getDoB(), 10, "Testmanager exp up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
451                     }
452                     resp.getWriter().println("Successfully added experience points.");
453                 } catch (GigiApiException e) {
454                     throw new Error(e);
455                 }
456             }
457         } else if (req.getParameter("addEmail") != null) {
458             User u = User.getByEmail(req.getParameter("addEmailEmail"));
459             try {
460                 EmailAddress ea = new EmailAddress(u, req.getParameter("addEmailNew"), Locale.ENGLISH);
461                 verify(ea.getAddress(), ea);
462             } catch (IllegalArgumentException e) {
463                 e.printStackTrace();
464                 resp.getWriter().println("An internal error occured.");
465             } catch (GigiApiException e) {
466                 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH), getDefaultVars(req));
467             }
468         } else if (req.getParameter("addCert") != null) {
469             User u = User.getByEmail(req.getParameter("addCertEmail"));
470             try {
471                 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
472                 kpg.initialize(4096);
473                 KeyPair kp = kpg.generateKeyPair();
474                 SPKAC s = new SPKAC((X509Key) kp.getPublic(), "challenge");
475                 Signature sign = Signature.getInstance("SHA512withRSA");
476                 sign.initSign(kp.getPrivate());
477
478                 byte[] res = s.getEncoded(sign);
479
480                 CertificateRequest cr = new CertificateRequest(new AuthorizationContext(u, u, sessionAc.isStronglyAuthenticated()), Base64.getEncoder().encodeToString(res), "challenge");
481                 cr.update(CertificateRequest.DEFAULT_CN, Digest.SHA512.toString(), "client", null, "", "email:" + u.getEmail());
482                 Certificate draft = cr.draft();
483                 draft.issue(null, "2y", u).waitFor(10000);
484                 if (draft.getStatus() == CertificateStatus.ISSUED) {
485                     resp.getWriter().println("added certificate");
486                 } else {
487                     resp.getWriter().println("signer failed");
488                 }
489             } catch (GeneralSecurityException e1) {
490                 e1.printStackTrace();
491                 resp.getWriter().println("error");
492             } catch (GigiApiException e) {
493                 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH), getDefaultVars(req));
494             }
495
496         } else if (req.getParameter("addExDom") != null) {
497             String dom = req.getParameter("exemptDom");
498             pingExempt.add(dom);
499             resp.getWriter().println("Updated domains exempt from pings. Current set: <br/>");
500             resp.getWriter().println(HTMLEncoder.encodeHTML(pingExempt.toString()));
501         } else if (req.getParameter("delExDom") != null) {
502             String dom = req.getParameter("exemptDom");
503             pingExempt.remove(dom);
504             resp.getWriter().println("Updated domains exempt from pings. Current set: <br/>");
505             resp.getWriter().println(HTMLEncoder.encodeHTML(pingExempt.toString()));
506         }
507         resp.getWriter().println("<br/><a href='" + PATH + "'>Go back</a>");
508     }
509
510     private void fetchMails(HttpServletRequest req, HttpServletResponse resp, String mail) throws IOException {
511         final LinkedList<String> mails = emails.get(mail);
512         HashMap<String, Object> vars = new HashMap<>();
513         vars.put("mail", mail);
514         if (mails != null) {
515             vars.put("mails", new IterableDataset() {
516
517                 Iterator<String> s = mails.iterator();
518
519                 @Override
520                 public boolean next(Language l, Map<String, Object> vars) {
521                     if ( !s.hasNext()) {
522                         return false;
523                     }
524                     vars.put("body", s.next().replaceAll("(https?://\\S+)", "<a href=\"$1\">$1</a>"));
525                     return true;
526                 }
527             });
528         }
529         t.output(resp.getWriter(), getLanguage(req), vars);
530         if (mails == null) {
531             resp.getWriter().println("No mails");
532
533         }
534     }
535
536     private static final Template form = new Template(Manager.class.getResource("Manager.templ"));
537
538     @Override
539     public boolean needsLogin() {
540         return false;
541     }
542
543     @Override
544     public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
545         String pi = req.getPathInfo().substring(PATH.length());
546         if (pi.length() > 1 && pi.startsWith("/fetch-")) {
547             String mail = pi.substring(pi.indexOf('-', 2) + 1);
548             fetchMails(req, resp, mail);
549             return;
550         }
551         HashMap<String, Object> vars = new HashMap<>();
552         vars.put("cats_types", new IterableDataset() {
553
554             CATSType[] type = CATSType.values();
555
556             int i = 0;
557
558             @Override
559             public boolean next(Language l, Map<String, Object> vars) {
560                 if (i >= type.length) {
561                     return false;
562                 }
563                 CATSType t = type[i++];
564                 vars.put("id", i - 1);
565                 vars.put("name", t.getDisplayName());
566                 return true;
567             }
568         });
569
570         vars.put("testValidMonths", TimeConditions.getInstance().getTestMonths());
571         vars.put("reverificationDays", TimeConditions.getInstance().getVerificationLimitDays());
572         vars.put("verificationFreshMonths", TimeConditions.getInstance().getVerificationMonths());
573         vars.put("verificationMaxAgeMonths", TimeConditions.getInstance().getVerificationMaxAgeMonths());
574         vars.put("emailPingMonths", TimeConditions.getInstance().getEmailPingMonths());
575
576         form.output(resp.getWriter(), getLanguage(req), vars);
577     }
578 }