upd: un-localize OpenSSL-command-help
[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(PrintWriter out, 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 false;
146     }
147
148     @Override
149     protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
150         out.print("<table class=\"wrapper dataTable\"><tbody>");
151         outputEmbeddableContent(out, l, vars);
152         out.print("<tr><td></td><td><input type=\"submit\" value=\"Update\"/></td></tbody></table>");
153     }
154
155     protected void outputEmbeddableContent(PrintWriter out, Language l, Map<String, Object> vars) {
156         vars.put("tokenName", tokenName);
157         vars.put("tokenValue", tokenValue);
158         vars.put("openSSLHelp", "<code>" + HTMLEncoder.encodeHTML("-subj \"/CN=<domain>/OU=" + tokenValue + "\"") + "</code>");
159         vars.put("authEmails", new IterableDataset() {
160
161             int i = 0;
162
163             @Override
164             public boolean next(Language l, Map<String, Object> vars) {
165                 if (i >= AUTHORATIVE_EMAILS.length) {
166                     return false;
167                 }
168                 vars.put("i", i);
169                 vars.put("email", AUTHORATIVE_EMAILS[i]);
170                 if (i == selectedMail) {
171                     vars.put("checked", " checked=\"checked\"");
172                 } else {
173                     vars.put("checked", "");
174                 }
175
176                 i++;
177                 return true;
178             }
179         });
180         vars.put("mail", doMail ? " checked=\"checked\"" : "");
181         vars.put("dns", doDNS ? " checked=\"checked\"" : "");
182         vars.put("http", doHTTP ? " checked=\"checked\"" : "");
183         vars.put("ssl", doSSL ? " checked=\"checked\"" : "");
184         vars.put("ssl-services", new IterableDataset() {
185
186             int counter = 0;
187
188             @Override
189             public boolean next(Language l, Map<String, Object> vars) {
190                 if (counter >= MAX_SSL_TESTS) {
191                     return false;
192                 }
193                 vars.put("i", counter);
194                 vars.put("port", ports[counter] == 0 ? "" : Integer.toString(ports[counter]));
195                 final SSLType selectedType = sslTypes[counter];
196                 vars.put("ssl-types", new IterableDataset() {
197
198                     int i = 0;
199
200                     SSLType[] type = SSLType.values();
201
202                     @Override
203                     public boolean next(Language l, Map<String, Object> vars) {
204                         if (i >= type.length) {
205                             return false;
206                         }
207                         vars.put("name", type[i].toString());
208                         if (selectedType == type[i]) {
209                             vars.put("selected", " selected=\"selected\"");
210                         } else {
211                             vars.put("selected", "");
212                         }
213                         i++;
214                         return true;
215                     }
216                 });
217                 counter++;
218                 return true;
219             }
220         });
221         t.output(out, l, vars);
222     }
223 }