]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/main/Signup.java
Merge "Update notes about password security"
[gigi.git] / src / org / cacert / gigi / pages / main / Signup.java
1 package org.cacert.gigi.pages.main;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.sql.SQLException;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.servlet.http.HttpServletRequest;
10
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.database.GigiPreparedStatement;
13 import org.cacert.gigi.database.GigiResultSet;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.email.EmailProvider;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.output.DateSelector;
19 import org.cacert.gigi.output.template.Form;
20 import org.cacert.gigi.output.template.Template;
21 import org.cacert.gigi.pages.Page;
22 import org.cacert.gigi.util.CalendarUtil;
23 import org.cacert.gigi.util.HTMLEncoder;
24 import org.cacert.gigi.util.Notary;
25 import org.cacert.gigi.util.PasswordStrengthChecker;
26
27 public class Signup extends Form {
28
29     Name buildupName = new Name("", "", "", "");
30
31     String email = "";
32
33     private Template t;
34
35     boolean general = true, country = true, regional = true, radius = true;
36
37     public Signup(HttpServletRequest hsr) {
38         super(hsr);
39         t = new Template(Signup.class.getResource("Signup.templ"));
40     }
41
42     DateSelector myDoB = new DateSelector("day", "month", "year");
43
44     @Override
45     public void outputContent(PrintWriter out, Language l, Map<String, Object> outerVars) {
46         HashMap<String, Object> vars = new HashMap<String, Object>();
47         vars.put("fname", HTMLEncoder.encodeHTML(buildupName.getFname()));
48         vars.put("mname", HTMLEncoder.encodeHTML(buildupName.getMname()));
49         vars.put("lname", HTMLEncoder.encodeHTML(buildupName.getLname()));
50         vars.put("suffix", HTMLEncoder.encodeHTML(buildupName.getSuffix()));
51         vars.put("dob", myDoB);
52         vars.put("email", HTMLEncoder.encodeHTML(email));
53         vars.put("general", general ? " checked=\"checked\"" : "");
54         vars.put("country", country ? " checked=\"checked\"" : "");
55         vars.put("regional", regional ? " checked=\"checked\"" : "");
56         vars.put("radius", radius ? " checked=\"checked\"" : "");
57         vars.put("helpOnNames", String.format(l.getTranslation("Help on Names %sin the wiki%s"), "<a href=\"//wiki.cacert.org/FAQ/HowToEnterNamesInJoinForm\" target=\"_blank\">", "</a>"));
58         vars.put("csrf", getCSRFToken());
59         vars.put("dobmin", User.MINIMUM_AGE + "");
60         t.output(out, l, vars);
61     }
62
63     private void update(HttpServletRequest r) {
64         String fname = buildupName.getFname();
65         String lname = buildupName.getLname();
66         String mname = buildupName.getMname();
67         String suffix = buildupName.getSuffix();
68         if (r.getParameter("fname") != null) {
69             fname = r.getParameter("fname");
70         }
71         if (r.getParameter("lname") != null) {
72             lname = r.getParameter("lname");
73         }
74         if (r.getParameter("mname") != null) {
75             mname = r.getParameter("mname");
76         }
77         if (r.getParameter("suffix") != null) {
78             suffix = r.getParameter("suffix");
79         }
80         if (r.getParameter("email") != null) {
81             email = r.getParameter("email");
82         }
83         buildupName = new Name(fname, lname, mname, suffix);
84         general = "1".equals(r.getParameter("general"));
85         country = "1".equals(r.getParameter("country"));
86         regional = "1".equals(r.getParameter("regional"));
87         radius = "1".equals(r.getParameter("radius"));
88         try {
89             myDoB.update(r);
90         } catch (GigiApiException e) {
91         }
92     }
93
94     @Override
95     public synchronized boolean submit(PrintWriter out, HttpServletRequest req) {
96         if (RegisterPage.RATE_LIMIT.isLimitExceeded(req.getRemoteAddr())) {
97             outputError(out, req, "Rate Limit Exceeded");
98             return false;
99         }
100
101         update(req);
102         if (buildupName.getLname().trim().equals("")) {
103             outputError(out, req, "Last name were blank.");
104         }
105         if ( !myDoB.isValid()) {
106             outputError(out, req, "Invalid date of birth");
107         }
108
109         if ( !CalendarUtil.isOfAge(myDoB.getDate(), User.MINIMUM_AGE)) {
110             outputError(out, req, "Entered dated of birth is below the restricted age requirements.");
111         }
112
113         if ( !"1".equals(req.getParameter("tos_agree"))) {
114             outputError(out, req, "Acceptance of the ToS is required to continue.");
115         }
116         if (email.equals("")) {
117             outputError(out, req, "Email Address was blank");
118         }
119         String pw1 = req.getParameter("pword1");
120         String pw2 = req.getParameter("pword2");
121         if (pw1 == null || pw1.equals("")) {
122             outputError(out, req, "Pass Phrases were blank");
123         } else if ( !pw1.equals(pw2)) {
124             outputError(out, req, "Pass Phrases don't match");
125         }
126         int pwpoints = PasswordStrengthChecker.checkpw(pw1, buildupName, email);
127         if (pwpoints < 3) {
128             outputError(out, req, "The Pass Phrase you submitted failed to contain enough" + " differing characters and/or contained words from" + " your name and/or email address.");
129         }
130         if (isFailed(out)) {
131             return false;
132         }
133         try (GigiPreparedStatement q1 = new GigiPreparedStatement("SELECT * FROM `emails` WHERE `email`=? AND `deleted` IS NULL"); GigiPreparedStatement q2 = new GigiPreparedStatement("SELECT * FROM `certOwners` INNER JOIN `users` ON `users`.`id`=`certOwners`.`id` WHERE `email`=? AND `deleted` IS NULL")) {
134             q1.setString(1, email);
135             q2.setString(1, email);
136             GigiResultSet r1 = q1.executeQuery();
137             GigiResultSet r2 = q2.executeQuery();
138             if (r1.next() || r2.next()) {
139                 outputError(out, req, "This email address is currently valid in the system.");
140             }
141         }
142         try (GigiPreparedStatement q3 = new GigiPreparedStatement("SELECT `domain` FROM `baddomains` WHERE `domain`=RIGHT(?, LENGTH(`domain`))")) {
143             q3.setString(1, email);
144
145             GigiResultSet r3 = q3.executeQuery();
146             if (r3.next()) {
147                 String domain = r3.getString(1);
148                 outputError(out, req, "We don't allow signups from people using email addresses from %s", domain);
149             }
150         }
151         String mailResult = EmailProvider.FAIL;
152         try {
153             mailResult = HTMLEncoder.encodeHTML(EmailProvider.getInstance().checkEmailServer(0, email));
154         } catch (IOException e) {
155         }
156         if ( !mailResult.equals(EmailProvider.OK)) {
157             if (mailResult.startsWith("4")) {
158                 outputError(out, req, "The mail server responsible for your domain indicated" + " a temporary failure. This may be due to anti-SPAM measures, such" + " as greylisting. Please try again in a few minutes.");
159             } else {
160                 outputError(out, req, "Email Address given was invalid, or a test connection" + " couldn't be made to your server, or the server" + " rejected the email address as invalid");
161             }
162             if (mailResult.equals(EmailProvider.FAIL)) {
163                 outputError(out, req, "Failed to make a connection to the mail server");
164             } else {
165                 outputErrorPlain(out, mailResult);
166             }
167         }
168
169         if (isFailed(out)) {
170             return false;
171         }
172         try {
173             run(req, pw1);
174         } catch (SQLException e) {
175             e.printStackTrace();
176         } catch (GigiApiException e) {
177             e.format(out, Page.getLanguage(req));
178             return false;
179         }
180         return true;
181     }
182
183     private void run(HttpServletRequest req, String password) throws SQLException, GigiApiException {
184         User u = new User(email, password, buildupName, myDoB.getDate(), Page.getLanguage(req).getLocale());
185
186         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `alerts` SET `memid`=?," + " `general`=?, `country`=?, `regional`=?, `radius`=?")) {
187             ps.setInt(1, u.getId());
188             ps.setBoolean(2, general);
189             ps.setBoolean(3, country);
190             ps.setBoolean(4, regional);
191             ps.setBoolean(5, radius);
192             ps.execute();
193         }
194         Notary.writeUserAgreement(u, "ToS", "account creation", "", true, 0);
195
196     }
197 }