1 package org.cacert.gigi.pages.account.domain;
3 import java.io.PrintWriter;
4 import java.util.Arrays;
8 import javax.servlet.http.HttpServletRequest;
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;
23 public class PingConfigForm extends Form {
26 DIRECT, XMPP, XMPP_SERVER, SMTP, IMAP;
29 public String toString() {
30 return super.toString().toLowerCase();
34 private Domain target;
36 private String tokenName = RandomToken.generateToken(8);
38 private String tokenValue = RandomToken.generateToken(16);
40 private static final int MAX_SSL_TESTS = 4;
42 public static final String[] AUTHORATIVE_EMAILS = new String[] {
43 "root", "hostmaster", "postmaster", "admin", "webmaster"
46 private int selectedMail = -1;
48 private boolean doMail, doDNS, doHTTP, doSSL;
50 private int[] ports = new int[MAX_SSL_TESTS];
52 private SSLType[] sslTypes = new SSLType[MAX_SSL_TESTS];
54 private static final Template t = new Template(PingConfigForm.class.getResource("PingConfigForm.templ"));
56 public PingConfigForm(HttpServletRequest hsr, Domain target) throws GigiApiException {
62 List<DomainPingConfiguration> configs = target.getConfiguredPings();
64 for (DomainPingConfiguration dpc : configs) {
65 switch (dpc.getType()) {
68 for (int i = 0; i < AUTHORATIVE_EMAILS.length; i++) {
69 if (AUTHORATIVE_EMAILS[i].equals(dpc.getInfo())) {
76 String[] parts = dpc.getInfo().split(":");
78 tokenValue = parts[1];
83 String[] parts = dpc.getInfo().split(":");
85 tokenValue = parts[1];
90 String[] parts = dpc.getInfo().split(":");
92 tokenValue = parts[1];
93 ports[portpos] = Integer.parseInt(parts[2]);
94 if (parts.length == 4) {
95 sslTypes[portpos] = SSLType.valueOf(parts[3].toUpperCase());
97 sslTypes[portpos] = SSLType.DIRECT;
106 public void setTarget(Domain target) {
107 this.target = target;
111 public boolean submit(PrintWriter out, HttpServletRequest req) throws GigiApiException {
113 if (req.getParameter("emailType") != null && req.getParameter("email") != null) {
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");
121 if (req.getParameter("DNSType") != null) {
122 target.addPing(DomainPingType.DNS, tokenName + ":" + tokenValue);
124 if (req.getParameter("HTTPType") != null) {
125 target.addPing(DomainPingType.HTTP, tokenName + ":" + tokenValue);
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("")) {
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);
144 Gigi.notifyPinger(null);
149 protected void outputContent(PrintWriter out, Language l, Map<String, Object> vars) {
150 vars.put("notEmbedded", true);
151 outputEmbeddableContent(out, l, vars);
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() {
163 public boolean next(Language l, Map<String, Object> vars) {
164 if (i >= AUTHORATIVE_EMAILS.length) {
168 vars.put("email", AUTHORATIVE_EMAILS[i]);
169 if (i == selectedMail) {
170 vars.put("checked", " checked=\"checked\"");
172 vars.put("checked", "");
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() {
188 public boolean next(Language l, Map<String, Object> vars) {
189 if (counter >= MAX_SSL_TESTS) {
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() {
199 SSLType[] type = SSLType.values();
202 public boolean next(Language l, Map<String, Object> vars) {
203 if (i >= type.length) {
206 vars.put("name", type[i].toString());
207 if (selectedType == type[i]) {
208 vars.put("selected", " selected=\"selected\"");
210 vars.put("selected", "");
220 t.output(out, l, vars);