public class DNSPinger extends DomainPinger {
@Override
- public String ping(Domain domain, String expToken, User u) {
+ public void ping(Domain domain, String expToken, User u, int confId) {
String[] tokenParts = expToken.split(":", 2);
List<String> nameservers;
try {
nameservers = Arrays.asList(DNSUtil.getNSNames(domain.getSuffix()));
} catch (NamingException e) {
- return "No authorative nameserver found.";
+ enterPingResult(confId, "error", "No authorative nameserver found.", null);
+ return;
}
StringBuffer result = new StringBuffer();
result.append("failed: ");
}
if ( !failed) {
- return PING_SUCCEDED;
+ enterPingResult(confId, PING_SUCCEDED, "", null);
+ } else {
+ enterPingResult(confId, "error", result.toString(), null);
}
- return result.toString();
}
}
package org.cacert.gigi.ping;
+import org.cacert.gigi.database.DatabaseConnection;
+import org.cacert.gigi.database.GigiPreparedStatement;
import org.cacert.gigi.dbObjects.Domain;
import org.cacert.gigi.dbObjects.User;
public static final String PING_SUCCEDED = "";
- public abstract String ping(Domain domain, String configuration, User user);
+ public abstract void ping(Domain domain, String configuration, User user, int confId);
+
+ protected static void enterPingResult(int configId, String state, String result, String token) {
+ GigiPreparedStatement enterPingResult = DatabaseConnection.getInstance().prepare("INSERT INTO `domainPinglog` SET `configId`=?, `state`=?::`pingState`, `result`=?, `challenge`=?");
+ enterPingResult.setInt(1, configId);
+ enterPingResult.setString(2, DomainPinger.PING_STILL_PENDING == state ? "open" : DomainPinger.PING_SUCCEDED.equals(state) ? "success" : "failed");
+ enterPingResult.setString(3, result);
+ enterPingResult.setString(4, token);
+ enterPingResult.execute();
+
+ }
+
+ protected static void updatePingResult(int configId, String token, String state, String result) {
+ GigiPreparedStatement updatePingResult = DatabaseConnection.getInstance().prepare("UPDATE `domainPinglog` SET `state`=?::`pingState`, `result`=? WHERE `configId`=? AND `challenge`=?");
+ updatePingResult.setString(1, DomainPinger.PING_STILL_PENDING == state ? "open" : DomainPinger.PING_SUCCEDED.equals(state) ? "success" : "failed");
+ updatePingResult.setString(2, result);
+ updatePingResult.setInt(3, configId);
+ updatePingResult.setString(4, token);
+ updatePingResult.execute();
+
+ }
}
import org.cacert.gigi.dbObjects.User;
import org.cacert.gigi.email.MailProbe;
import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.util.RandomToken;
public class EmailPinger extends DomainPinger {
@Override
- public String ping(Domain domain, String configuration, User u) {
- String[] parts = configuration.split(":", 2);
- String mail = parts[0] + "@" + domain.getSuffix();
+ public void ping(Domain domain, String configuration, User u, int confId) {
+ String mail = configuration + "@" + domain.getSuffix();
+ String token = RandomToken.generateToken(16);
try {
- MailProbe.sendMailProbe(Language.getInstance(u.getPreferredLocale()), "domain", domain.getId(), parts[1], mail);
+ enterPingResult(confId, PING_STILL_PENDING, "", token);
+ MailProbe.sendMailProbe(Language.getInstance(u.getPreferredLocale()), "domain", domain.getId(), token, mail);
} catch (IOException e) {
e.printStackTrace();
- return "Mail connection interrupted";
+ updatePingResult(confId, "error", "Mail connection interrupted", token);
}
- return PING_STILL_PENDING;
}
}
public class HTTPFetch extends DomainPinger {
@Override
- public String ping(Domain domain, String expToken, User user) {
+ public void ping(Domain domain, String expToken, User user, int confId) {
try {
String[] tokenParts = expToken.split(":", 2);
URL u = new URL("http://" + domain.getSuffix() + "/cacert-" + tokenParts[0] + ".txt");
HttpURLConnection huc = (HttpURLConnection) u.openConnection();
if (huc.getResponseCode() != 200) {
- return "Invalid status code.";
+ enterPingResult(confId, "error", "Invaild status code " + huc.getResponseCode() + ".", null);
+ return;
}
BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream(), "UTF-8"));
String line = br.readLine();
if (line == null) {
- return "No response from your server.";
+ enterPingResult(confId, "error", "Empty document.", null);
+ return;
}
if (line.trim().equals(tokenParts[1])) {
- return PING_SUCCEDED;
+ enterPingResult(confId, PING_SUCCEDED, "", null);
+ return;
}
- return "Challange tokens differed.";
+ enterPingResult(confId, "error", "Challange tokens differed.", null);
+ return;
} catch (IOException e) {
e.printStackTrace();
- return "Connection closed.";
+ enterPingResult(confId, "error", "Exception: connection closed.", null);
+ return;
}
}
}
private GigiPreparedStatement searchNeededPings;
- private GigiPreparedStatement enterPingResult;
-
private KeyStore truststore;
private Queue<DomainPingConfiguration> toExecute = new LinkedList<>();
@Override
public void run() {
searchNeededPings = DatabaseConnection.getInstance().prepare("SELECT `pingconfig`.`id` FROM `pingconfig` LEFT JOIN `domainPinglog` ON `domainPinglog`.`configId` = `pingconfig`.`id` INNER JOIN `domains` ON `domains`.`id` = `pingconfig`.`domainid` WHERE ( `domainPinglog`.`configId` IS NULL) AND `domains`.`deleted` IS NULL GROUP BY `pingconfig`.`id`");
- enterPingResult = DatabaseConnection.getInstance().prepare("INSERT INTO `domainPinglog` SET `configId`=?, `state`=?::`pingState`, `result`=?, `challenge`=?");
pingers.put(PingType.EMAIL, new EmailPinger());
pingers.put(PingType.SSL, new SSLPinger(truststore));
pingers.put(PingType.HTTP, new HTTPFetch());
String config = conf.getInfo();
DomainPinger dp = pingers.get(type);
if (dp != null) {
- String token = null;
if (dp instanceof EmailPinger) {
+ String token = null;
token = RandomToken.generateToken(16);
config = config + ":" + token;
}
- enterPingResult.setInt(1, conf.getId());
Domain target = conf.getTarget();
System.err.println("Executing " + dp + " on " + target + " (" + System.currentTimeMillis() + ")");
- String resp = dp.ping(target, config, target.getOwner());
+ try {
+ dp.ping(target, config, target.getOwner(), conf.getId());
+ } catch (Throwable t) {
+ DomainPinger.enterPingResult(conf.getId(), "error", "exception", null);
+ }
System.err.println("done (" + System.currentTimeMillis() + ")");
- enterPingResult.setString(2, DomainPinger.PING_STILL_PENDING == resp ? "open" : DomainPinger.PING_SUCCEDED.equals(resp) ? "success" : "failed");
- enterPingResult.setString(3, resp);
- enterPingResult.setString(4, token);
- enterPingResult.execute();
}
}
}
@Override
- public String ping(Domain domain, String configuration, User u) {
+ public void ping(Domain domain, String configuration, User u, int confId) {
try (SocketChannel sch = SocketChannel.open()) {
sch.socket().setSoTimeout(5000);
String[] parts = configuration.split(":", 2);
}
}
- return test(sch, domain.getSuffix(), u);
+ String res = test(sch, domain.getSuffix(), u);
+ enterPingResult(confId, res, res, null);
+ return;
} catch (IOException e) {
- return "Connecton failed";
+ enterPingResult(confId, "error", "connection Failed", null);
+ return;
}
}
BigInteger serial = first.getSerialNumber();
Certificate c = Certificate.getBySerial(serial.toString(16));
+ if (c == null) {
+ return "Certificate not found";
+ }
if (c.getOwner().getId() != subject.getId()) {
return "Owner mismatch";
}