]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/ping/DNSPinger.java
Merge "Suggestions to enhance the SQL call pattern."
[gigi.git] / src / org / cacert / gigi / ping / DNSPinger.java
index e4373c58b8fa9a79e65436c114133cd556bb8d62..eb6327b3f9a2968cd98332091b01bdbdf3a49d56 100644 (file)
@@ -1,71 +1,58 @@
 package org.cacert.gigi.ping;
 
-import java.io.BufferedReader;
-import java.io.IOException;
-import java.io.InputStreamReader;
 import java.util.Arrays;
-import java.util.LinkedList;
+import java.util.List;
 
-public class DNSPinger extends DomainPinger {
+import javax.naming.NamingException;
+
+import org.cacert.gigi.dbObjects.CertificateOwner;
+import org.cacert.gigi.dbObjects.Domain;
+import org.cacert.gigi.util.DNSUtil;
 
-       @Override
-       public void ping(String domain, String configuration, String expToken) {
-               try {
-                       Process p = Runtime.getRuntime().exec(
-                                       new String[]{"dig", "+short", "NS", domain});
-                       BufferedReader br = new BufferedReader(new InputStreamReader(
-                                       p.getInputStream()));
-                       String line;
-                       LinkedList<String> nameservers = new LinkedList<String>();
-                       while ((line = br.readLine()) != null) {
-                               nameservers.add(line);
-                       }
-                       p.destroy();
-                       StringBuffer result = new StringBuffer();
-                       result.append("failed: ");
-                       boolean failed = nameservers.isEmpty();
-                       nameservers : for (String NS : nameservers) {
-                               String[] call = new String[]{"dig", "+short", "TXT",
-                                               "cacert." + domain, NS};
-                               System.out.println(Arrays.toString(call));
-                               p = Runtime.getRuntime().exec(call);
-                               br = new BufferedReader(new InputStreamReader(
-                                               p.getInputStream()));
-                               String token = null;
-                               boolean found = false;
-                               while ((line = br.readLine()) != null) {
-                                       if (line.isEmpty()) {
-                                               continue;
-                                       }
-                                       found = true;
-                                       token = line.substring(1, line.length() - 1);
-                                       if (token.equals(expToken)) {
-                                               continue nameservers;
-                                       }
-                               }
-                               p.destroy();
-                               result.append(NS);
-                               if (found) {
-                                       result.append(" DIFFER;");
-                               } else {
-                                       result.append(" EMPTY;");
-                               }
-                               failed = true;
+public class DNSPinger extends DomainPinger {
 
-                       }
-                       if (!failed) {
-                               // Success
-                               return;
-                       }
-                       System.out.println(result.toString());
-               } catch (IOException e) {
-                       e.printStackTrace();
-                       // FAIL
-               }
-               // FAIL
-       }
-       public static void main(String[] args) {
-               new DNSPinger().ping("dyn.dogcraft.de", "", "salat");
-       }
+    @Override
+    public void ping(Domain domain, String expToken, CertificateOwner u, int confId) {
+        String[] tokenParts = expToken.split(":", 2);
+        List<String> nameservers;
+        try {
+            nameservers = Arrays.asList(DNSUtil.getNSNames(domain.getSuffix()));
+        } catch (NamingException e) {
+            enterPingResult(confId, "error", "No authorative nameserver found.", null);
+            return;
+        }
+        StringBuffer result = new StringBuffer();
+        result.append("failed: ");
+        boolean failed = nameservers.isEmpty();
+        nameservers:
+        for (String NS : nameservers) {
+            boolean found = false;
+            try {
+                for (String token : DNSUtil.getTXTEntries(tokenParts[0] + "._cacert._auth." + domain.getSuffix(), NS)) {
+                    if (token.isEmpty()) {
+                        continue;
+                    }
+                    found = true;
+                    if (token.equals(tokenParts[1])) {
+                        continue nameservers;
+                    }
+                }
+            } catch (NamingException e) {
+                found = false;
+            }
+            result.append(NS);
+            if (found) {
+                result.append(" DIFFER;");
+            } else {
+                result.append(" EMPTY;");
+            }
+            failed = true;
 
+        }
+        if ( !failed) {
+            enterPingResult(confId, PING_SUCCEDED, "", null);
+        } else {
+            enterPingResult(confId, "error", result.toString(), null);
+        }
+    }
 }