]> WPIA git - gigi.git/blob - src/org/cacert/gigi/pages/account/domain/PingConfigForm.java
upd: use a more strict pattern for handling forms
[gigi.git] / src / org / cacert / gigi / pages / account / domain / PingConfigForm.java
1 package org.cacert.gigi.pages.account.domain;
2
3 import java.io.PrintWriter;
4 import java.util.Arrays;
5 import java.util.List;
6 import java.util.Map;
7
8 import javax.servlet.http.HttpServletRequest;
9
10 import org.cacert.gigi.Gigi;
11 import org.cacert.gigi.GigiApiException;
12 import org.cacert.gigi.dbObjects.Domain;
13 import org.cacert.gigi.dbObjects.DomainPingConfiguration;
14 import org.cacert.gigi.dbObjects.DomainPingType;
15 import org.cacert.gigi.localisation.Language;
16 import org.cacert.gigi.output.template.Form;
17 import org.cacert.gigi.output.template.IterableDataset;
18 import org.cacert.gigi.output.template.Template;
19 import org.cacert.gigi.ping.SSLPinger;
20 import org.cacert.gigi.util.HTMLEncoder;
21 import org.cacert.gigi.util.RandomToken;
22
23 public class PingConfigForm extends Form {
24
25     public enum SSLType {
26         DIRECT, XMPP, XMPP_SERVER, SMTP, IMAP;
27
28         @Override
29         public String toString() {
30             return super.toString().toLowerCase();
31         }
32     }
33
34     private Domain target;
35
36     private String tokenName = RandomToken.generateToken(8);
37
38     private String tokenValue = RandomToken.generateToken(16);
39
40     private static final int MAX_SSL_TESTS = 4;
41
42     public static final String[] AUTHORATIVE_EMAILS = new String[] {
43             "root", "hostmaster", "postmaster", "admin", "webmaster"
44     };
45
46     private int selectedMail = -1;
47
48     private boolean doMail, doDNS, doHTTP, doSSL;
49
50     private int[] ports = new int[MAX_SSL_TESTS];
51
52     private SSLType[] sslTypes = new SSLType[MAX_SSL_TESTS];
53
54     private static final Template t = new Template(PingConfigForm.class.getResource("PingConfigForm.templ"));
55
56     public PingConfigForm(HttpServletRequest hsr, Domain target) throws GigiApiException {
57         super(hsr);
58         this.target = target;
59         if (target == null) {
60             return;
61         }
62         List<DomainPingConfiguration> configs = target.getConfiguredPings();
63         int portpos = 0;
64         for (DomainPingConfiguration dpc : configs) {
65             switch (dpc.getType()) {
66             case EMAIL:
67                 doMail = true;
68                 for (int i = 0; i < AUTHORATIVE_EMAILS.length; i++) {
69                     if (AUTHORATIVE_EMAILS[i].equals(dpc.getInfo())) {
70                         selectedMail = i;
71                     }
72                 }
73                 break;
74             case DNS: {
75                 doDNS = true;
76                 String[] parts = dpc.getInfo().split(":");
77                 tokenName = parts[0];
78                 tokenValue = parts[1];
79                 break;
80             }
81             case HTTP: {
82                 doHTTP = true;
83                 String[] parts = dpc.getInfo().split(":");
84                 tokenName = parts[0];
85                 tokenValue = parts[1];
86                 break;
87             }
88             case SSL: {
89                 doSSL = true;
90                 String[] parts = dpc.getInfo().split(":");
91                 tokenName = parts[0];
92                 tokenValue = parts[1];
93                 ports[portpos] = Integer.parseInt(parts[2]);
94                 if (parts.length == 4) {
95                     sslTypes[portpos] = SSLType.valueOf(parts[3].toUpperCase());
96                 } else {
97                     sslTypes[portpos] = SSLType.DIRECT;
98                 }
99                 portpos++;
100                 break;
101             }
102             }
103         }
104     }
105
106     public void setTarget(Domain target) {
107         this.target = target;
108     }
109
110     @Override
111     public boolean submit(HttpServletRequest req) throws GigiApiException {
112         target.clearPings();
113         if (req.getParameter("emailType") != null && req.getParameter("email") != null) {
114             try {
115                 String mail = AUTHORATIVE_EMAILS[Integer.parseInt(req.getParameter("email"))];
116                 target.addPing(DomainPingType.EMAIL, mail);
117             } catch (NumberFormatException e) {
118                 throw new GigiApiException("A email address is required");
119             }
120         }
121         if (req.getParameter("DNSType") != null) {
122             target.addPing(DomainPingType.DNS, tokenName + ":" + tokenValue);
123         }
124         if (req.getParameter("HTTPType") != null) {
125             target.addPing(DomainPingType.HTTP, tokenName + ":" + tokenValue);
126         }
127         if (req.getParameter("SSLType") != null) {
128             List<String> types = Arrays.asList(SSLPinger.TYPES);
129             for (int i = 0; i < MAX_SSL_TESTS; i++) {
130                 String type = req.getParameter("ssl-type-" + i);
131                 String port = req.getParameter("ssl-port-" + i);
132                 if (type == null || port == null || port.equals("")) {
133                     continue;
134                 }
135                 int portInt = Integer.parseInt(port);
136                 if ("direct".equals(type)) {
137                     target.addPing(DomainPingType.SSL, tokenName + ":" + tokenValue + ":" + port);
138                 } else if (types.contains(type)) {
139                     target.addPing(DomainPingType.SSL, tokenName + ":" + tokenValue + ":" + portInt + ":" + type);
140                 }
141
142             }
143         }
144         Gigi.notifyPinger(null);
145         return true;
146     }
147
148     @Override
149     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
150         vars.put("notEmbedded", true);
151         outputEmbeddableContent(out, l, vars);
152     }
153
154     protected void outputEmbeddableContent(PrintWriter out, Language l, Map<String, Object> vars) {
155         vars.put("tokenName", tokenName);
156         vars.put("tokenValue", tokenValue);
157         vars.put("openSSLHelp", "<code>" + HTMLEncoder.encodeHTML("-subj \"/CN=<domain>/OU=" + tokenValue + "\"") + "</code>");
158         vars.put("authEmails", new IterableDataset() {
159
160             int i = 0;
161
162             @Override
163             public boolean next(Language l, Map<String, Object> vars) {
164                 if (i >= AUTHORATIVE_EMAILS.length) {
165                     return false;
166                 }
167                 vars.put("i", i);
168                 vars.put("email", AUTHORATIVE_EMAILS[i]);
169                 if (i == selectedMail) {
170                     vars.put("checked", " checked=\"checked\"");
171                 } else {
172                     vars.put("checked", "");
173                 }
174
175                 i++;
176                 return true;
177             }
178         });
179         vars.put("mail", doMail ? " checked=\"checked\"" : "");
180         vars.put("dns", doDNS ? " checked=\"checked\"" : "");
181         vars.put("http", doHTTP ? " checked=\"checked\"" : "");
182         vars.put("ssl", doSSL ? " checked=\"checked\"" : "");
183         vars.put("ssl-services", new IterableDataset() {
184
185             int counter = 0;
186
187             @Override
188             public boolean next(Language l, Map<String, Object> vars) {
189                 if (counter >= MAX_SSL_TESTS) {
190                     return false;
191                 }
192                 vars.put("i", counter);
193                 vars.put("port", ports[counter] == 0 ? "" : Integer.toString(ports[counter]));
194                 final SSLType selectedType = sslTypes[counter];
195                 vars.put("ssl-types", new IterableDataset() {
196
197                     int i = 0;
198
199                     SSLType[] type = SSLType.values();
200
201                     @Override
202                     public boolean next(Language l, Map<String, Object> vars) {
203                         if (i >= type.length) {
204                             return false;
205                         }
206                         vars.put("name", type[i].toString());
207                         if (selectedType == type[i]) {
208                             vars.put("selected", " selected=\"selected\"");
209                         } else {
210                             vars.put("selected", "");
211                         }
212                         i++;
213                         return true;
214                     }
215                 });
216                 counter++;
217                 return true;
218             }
219         });
220         t.output(out, l, vars);
221     }
222 }