1 package club.wpia.gigi.pages;
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;
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;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.http.HttpServletResponse;
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.DomainPingConfiguration;
43 import club.wpia.gigi.dbObjects.DomainPingExecution;
44 import club.wpia.gigi.dbObjects.DomainPingType;
45 import club.wpia.gigi.dbObjects.EmailAddress;
46 import club.wpia.gigi.dbObjects.Group;
47 import club.wpia.gigi.dbObjects.NamePart;
48 import club.wpia.gigi.dbObjects.NamePart.NamePartType;
49 import club.wpia.gigi.dbObjects.User;
50 import club.wpia.gigi.dbObjects.Verification.VerificationType;
51 import club.wpia.gigi.email.DelegateMailProvider;
52 import club.wpia.gigi.email.EmailProvider;
53 import club.wpia.gigi.localisation.Language;
54 import club.wpia.gigi.output.template.IterableDataset;
55 import club.wpia.gigi.output.template.Template;
56 import club.wpia.gigi.pages.account.certs.CertificateRequest;
57 import club.wpia.gigi.ping.DomainPinger;
58 import club.wpia.gigi.ping.PingerDaemon;
59 import club.wpia.gigi.util.AuthorizationContext;
60 import club.wpia.gigi.util.DayDate;
61 import club.wpia.gigi.util.DomainAssessment;
62 import club.wpia.gigi.util.HTMLEncoder;
63 import club.wpia.gigi.util.Notary;
64 import club.wpia.gigi.util.TimeConditions;
65 import sun.security.x509.X509Key;
67 public class Manager extends Page {
69 public static String validVerificationDateString() {
70 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
71 Calendar c = Calendar.getInstance();
72 c.setTimeInMillis(System.currentTimeMillis());
73 c.add(Calendar.MONTH, -Notary.LIMIT_MAX_MONTHS_VERIFICATION + 1);
74 return sdf.format(new Date(c.getTimeInMillis()));
77 public static Country getRandomCountry() {
78 List<Country> cc = Country.getCountries();
79 int rnd = new Random().nextInt(cc.size());
83 public static final String PATH = "/manager";
85 private static HashMap<DomainPingType, DomainPinger> dps;
88 super("Test Manager");
91 Field gigiInstance = Gigi.class.getDeclaredField("instance");
92 gigiInstance.setAccessible(true);
93 Gigi g = (Gigi) gigiInstance.get(null);
95 Field gigiPinger = Gigi.class.getDeclaredField("pinger");
96 gigiPinger.setAccessible(true);
97 PingerDaemon pd = (PingerDaemon) gigiPinger.get(g);
99 Field f = PingerDaemon.class.getDeclaredField("pingers");
100 f.setAccessible(true);
101 dps = (HashMap<DomainPingType, DomainPinger>) f.get(pd);
102 HashMap<DomainPingType, DomainPinger> pingers = new HashMap<>();
103 for (DomainPingType dpt : DomainPingType.values()) {
104 pingers.put(dpt, new PingerFetcher(dpt));
107 } catch (ReflectiveOperationException e) {
112 public User getSupporter() {
113 if (supporter != null) {
117 User u = createAgent( -1);
118 if ( !u.isInGroup(Group.SUPPORTER)) {
119 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
120 ps.setInt(1, u.getId());
121 ps.setString(2, Group.SUPPORTER.getDBName());
122 ps.setInt(3, u.getId());
128 } catch (ReflectiveOperationException | GigiApiException e) {
134 public User getAgent(int i) {
135 if (agents[i] != null) {
139 User u = createAgent(i);
142 } catch (ReflectiveOperationException | GigiApiException e) {
148 private User createAgent(int i) throws GigiApiException, IllegalAccessException {
149 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `country`=?")) {
150 String mail = "test-agent" + i + "@example.com";
151 User u = User.getByEmail(mail);
153 System.out.println("Creating RA-Agent");
155 u = User.getByEmail(mail);
156 passCATS(u, CATSType.AGENT_CHALLENGE);
157 ps.setInt(1, u.getId());
158 ps.setInt(2, u.getPreferredName().getId());
160 ps.setString(4, "Manager init code");
161 ps.setString(5, "1990-01-01");
162 ps.setString(6, getRandomCountry().getCode());
169 private void passCATS(User u, CATSType t) {
170 CATS.enterResult(u, t, new Date(System.currentTimeMillis()), "en_EN", "1");
173 private static Manager instance;
175 private static final Template t = new Template(Manager.class.getResource("ManagerMails.templ"));
177 HashMap<String, LinkedList<String>> emails = new HashMap<>();
179 private static TreeSet<String> pingExempt = new TreeSet<>();
181 public static Manager getInstance() {
182 if (instance == null) {
183 instance = new Manager();
188 public static class MailFetcher extends DelegateMailProvider {
192 public MailFetcher(Properties props) {
193 super(props, props.getProperty("emailProvider.manager.target"));
194 String str = props.getProperty("emailProvider.manager.filter");
196 toForward = new Pattern[0];
198 String[] parts = str.split(" ");
199 toForward = new Pattern[parts.length];
200 for (int i = 0; i < parts.length; i++) {
201 toForward[i] = Pattern.compile(parts[i]);
207 public String checkEmailServer(int forUid, String address) throws IOException {
212 public synchronized void sendMail(String to, String subject, String message, String replyto, String toname, String fromname, String errorsto, boolean extra) throws IOException {
213 HashMap<String, LinkedList<String>> mails = Manager.getInstance().emails;
214 LinkedList<String> hismails = mails.get(to);
215 if (hismails == null) {
216 mails.put(to, hismails = new LinkedList<>());
218 hismails.addFirst(subject + "\n" + message);
219 for (int i = 0; i < toForward.length; i++) {
220 if (toForward[i].matcher(to).matches()) {
221 super.sendMail(to, subject, message, replyto, toname, fromname, errorsto, extra);
229 public class PingerFetcher extends DomainPinger {
231 private DomainPingType dpt;
233 public PingerFetcher(DomainPingType dpt) {
238 public DomainPingExecution ping(Domain domain, String configuration, CertificateOwner target, DomainPingConfiguration conf) {
239 System.err.println("TestManager: " + domain.getSuffix());
240 if (pingExempt.contains(domain.getSuffix())) {
241 return enterPingResult(conf, DomainPinger.PING_SUCCEDED, "Succeeded by TestManager pass-by", null);
243 DomainPinger pinger = dps.get(dpt);
244 System.err.println("Forward to old pinger: " + pinger);
245 return pinger.ping(domain, configuration, target, conf);
251 public void batchCreateUsers(String mailPrefix, String domain, int amount, PrintWriter out) {
255 out.print("100 at most, please.");
258 for (int i = 0; i < amount; i++) {
259 String email = mailPrefix + i + "@" + domain;
262 } catch (ReflectiveOperationException e) {
263 out.println("failed");
265 } catch (GigiApiException e) {
266 out.println("failed: " + e.getMessage());
271 private void createUser(String email) throws GigiApiException, IllegalAccessException {
272 Calendar gc = GregorianCalendar.getInstance();
273 gc.setTimeInMillis(0);
276 Country country = getRandomCountry();
278 User u = new User(email, "xvXV12°§", new DayDate(gc.getTime().getTime()), Locale.ENGLISH, country, //
279 new NamePart(NamePartType.FIRST_NAME, "Först"), new NamePart(NamePartType.FIRST_NAME, "Müddle"), //
280 new NamePart(NamePartType.LAST_NAME, "Läst"), new NamePart(NamePartType.SUFFIX, "Süffix"));
281 EmailAddress ea = u.getEmails()[0];
285 private void verify(String email, EmailAddress ea) throws GigiApiException {
286 LinkedList<String> i = emails.get(email);
287 while (i.size() > 0 && !ea.isVerified()) {
288 String lst = i.getLast();
289 Pattern p = Pattern.compile("hash=([a-zA-Z0-9]+)");
290 Matcher m = p.matcher(lst);
292 ea.verify(m.group(1));
299 User[] agents = new User[25];
304 public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
305 AuthorizationContext sessionAc = (AuthorizationContext) req.getSession().getAttribute(Gigi.AUTH_CONTEXT);
306 if (req.getParameter("create") != null) {
307 String prefix = req.getParameter("prefix");
308 String domain = req.getParameter("suffix");
310 if (null == prefix) {
311 throw new GigiApiException("No prefix given.");
313 if (null == domain) {
314 throw new GigiApiException("No domain given.");
317 DomainAssessment.checkCertifiableDomain(domain, false, true);
319 if ( !EmailProvider.isValidMailAddress(prefix + "@" + domain)) {
320 throw new GigiApiException("Invalid email address template.");
323 batchCreateUsers(prefix, domain, Integer.parseInt(req.getParameter("amount")), resp.getWriter());
324 resp.getWriter().println("User batch created.");
325 } catch (GigiApiException e) {
328 } else if (req.getParameter("addpriv") != null || req.getParameter("delpriv") != null) {
329 User u = User.getByEmail(req.getParameter("email"));
331 resp.getWriter().println("User not found.");
335 if (req.getParameter("addpriv") != null) {
336 u.grantGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
337 resp.getWriter().println("Privilege granted");
339 u.revokeGroup(getSupporter(), Group.getByString(req.getParameter("priv")));
340 resp.getWriter().println("Privilege revoked");
342 } catch (GigiApiException e) {
345 } else if (req.getParameter("fetch") != null) {
346 String mail = req.getParameter("femail");
347 fetchMails(req, resp, mail);
348 } else if (req.getParameter("cats") != null) {
349 String mail = req.getParameter("catsEmail");
350 String testId = req.getParameter("catsType");
351 User byEmail = User.getByEmail(mail);
352 if (byEmail == null) {
353 resp.getWriter().println("User not found.");
356 if (testId == null) {
357 resp.getWriter().println("No test given.");
360 CATSType test = CATSType.values()[Integer.parseInt(testId)];
361 passCATS(byEmail, test);
362 resp.getWriter().println("Test '" + test.getDisplayName() + "' was added to user account.");
363 } else if (req.getParameter("verify") != null) {
364 String mail = req.getParameter("verifyEmail");
365 String verificationPoints = req.getParameter("verificationPoints");
366 User byEmail = User.getByEmail(mail);
368 if (byEmail == null) {
369 resp.getWriter().println("User not found.");
378 vp = Integer.parseInt(verificationPoints);
379 } catch (NumberFormatException e) {
380 throw new GigiApiException("No valid Verification Points entered.");
383 if (vp > 100) { // only allow max 100 Verification points
392 Notary.verify(getAgent(agentNumber), byEmail, byEmail.getPreferredName(), byEmail.getDoB(), currentVP, "Testmanager Verify up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
397 } catch (GigiApiException e) {
401 resp.getWriter().println("User has been verified " + agentNumber + " times.");
403 } else if (req.getParameter("letverify") != null) {
404 String mail = req.getParameter("letverifyEmail");
405 User byEmail = User.getByEmail(mail);
406 if (byEmail == null || !byEmail.canVerify()) {
407 resp.getWriter().println("User not found, or found user is not allowed to verify.");
410 for (int i = 0; i < 25; i++) {
411 User a = getAgent(i);
412 Notary.verify(byEmail, a, a.getNames()[0], a.getDoB(), 10, "Testmanager exp up code", validVerificationDateString(), VerificationType.FACE_TO_FACE, getRandomCountry());
414 resp.getWriter().println("Successfully added experience points.");
415 } catch (GigiApiException e) {
419 } else if (req.getParameter("addEmail") != null) {
420 User u = User.getByEmail(req.getParameter("addEmailEmail"));
422 EmailAddress ea = new EmailAddress(u, req.getParameter("addEmailNew"), Locale.ENGLISH);
423 verify(ea.getAddress(), ea);
424 } catch (IllegalArgumentException e) {
426 resp.getWriter().println("An internal error occured.");
427 } catch (GigiApiException e) {
428 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH), getDefaultVars(req));
430 } else if (req.getParameter("addCert") != null) {
431 User u = User.getByEmail(req.getParameter("addCertEmail"));
433 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
434 kpg.initialize(4096);
435 KeyPair kp = kpg.generateKeyPair();
436 SPKAC s = new SPKAC((X509Key) kp.getPublic(), "challenge");
437 Signature sign = Signature.getInstance("SHA512withRSA");
438 sign.initSign(kp.getPrivate());
440 byte[] res = s.getEncoded(sign);
442 CertificateRequest cr = new CertificateRequest(new AuthorizationContext(u, u, sessionAc.isStronglyAuthenticated()), Base64.getEncoder().encodeToString(res), "challenge");
443 cr.update(CertificateRequest.DEFAULT_CN, Digest.SHA512.toString(), "client", null, "", "email:" + u.getEmail());
444 Certificate draft = cr.draft();
445 draft.issue(null, "2y", u).waitFor(10000);
446 if (draft.getStatus() == CertificateStatus.ISSUED) {
447 resp.getWriter().println("added certificate");
449 resp.getWriter().println("signer failed");
451 } catch (GeneralSecurityException e1) {
452 e1.printStackTrace();
453 resp.getWriter().println("error");
454 } catch (GigiApiException e) {
455 e.format(resp.getWriter(), Language.getInstance(Locale.ENGLISH), getDefaultVars(req));
458 } else if (req.getParameter("addExDom") != null) {
459 String dom = req.getParameter("exemptDom");
461 resp.getWriter().println("Updated domains exempt from pings. Current set: <br/>");
462 resp.getWriter().println(HTMLEncoder.encodeHTML(pingExempt.toString()));
463 } else if (req.getParameter("delExDom") != null) {
464 String dom = req.getParameter("exemptDom");
465 pingExempt.remove(dom);
466 resp.getWriter().println("Updated domains exempt from pings. Current set: <br/>");
467 resp.getWriter().println(HTMLEncoder.encodeHTML(pingExempt.toString()));
469 resp.getWriter().println("<br/><a href='" + PATH + "'>Go back</a>");
472 private void fetchMails(HttpServletRequest req, HttpServletResponse resp, String mail) throws IOException {
473 final LinkedList<String> mails = emails.get(mail);
474 HashMap<String, Object> vars = new HashMap<>();
475 vars.put("mail", mail);
477 vars.put("mails", new IterableDataset() {
479 Iterator<String> s = mails.iterator();
482 public boolean next(Language l, Map<String, Object> vars) {
486 vars.put("body", s.next().replaceAll("(https?://\\S+)", "<a href=\"$1\">$1</a>"));
491 t.output(resp.getWriter(), getLanguage(req), vars);
493 resp.getWriter().println("No mails");
498 private static final Template form = new Template(Manager.class.getResource("Manager.templ"));
501 public boolean needsLogin() {
506 public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
507 String pi = req.getPathInfo().substring(PATH.length());
508 if (pi.length() > 1 && pi.startsWith("/fetch-")) {
509 String mail = pi.substring(pi.indexOf('-', 2) + 1);
510 fetchMails(req, resp, mail);
513 HashMap<String, Object> vars = new HashMap<>();
514 vars.put("cats_types", new IterableDataset() {
516 CATSType[] type = CATSType.values();
521 public boolean next(Language l, Map<String, Object> vars) {
522 if (i >= type.length) {
525 CATSType t = type[i++];
526 vars.put("id", i - 1);
527 vars.put("name", t.getDisplayName());
532 vars.put("testValidMonths", TimeConditions.getInstance().getTestMonths());
533 vars.put("reverificationDays", TimeConditions.getInstance().getVerificationLimitDays());
534 vars.put("verificationFreshMonths", TimeConditions.getInstance().getVerificationMonths());
535 vars.put("verificationMaxAgeMonths", TimeConditions.getInstance().getVerificationMaxAgeMonths());
536 vars.put("emailPingMonths", TimeConditions.getInstance().getEmailPingMonths());
538 form.output(resp.getWriter(), getLanguage(req), vars);