X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Fclub%2Fwpia%2Fgigi%2Futil%2FDNSUtil.java;h=64a2096461d5d0ce1c322eddc10a0e47f0bc6712;hp=5bec99693d2c00fe8da3cc24b29ad985c47f3391;hb=448caf5e1e56a72fa2c5217e2e3a2eced0ab1c9b;hpb=bccd4cc0dba0f89aa045b113bac46eb8cc1dab4e diff --git a/src/club/wpia/gigi/util/DNSUtil.java b/src/club/wpia/gigi/util/DNSUtil.java index 5bec9969..64a20964 100644 --- a/src/club/wpia/gigi/util/DNSUtil.java +++ b/src/club/wpia/gigi/util/DNSUtil.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Hashtable; import javax.naming.Context; +import javax.naming.NameNotFoundException; import javax.naming.NamingException; import javax.naming.directory.Attribute; import javax.naming.directory.Attributes; @@ -37,17 +38,17 @@ public class DNSUtil { env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); env.put(Context.AUTHORITATIVE, "true"); env.put(Context.PROVIDER_URL, "dns://" + server); + InitialDirContext context = new InitialDirContext(env); try { - Attributes dnsLookup = context.getAttributes(name, new String[] { "TXT" }); + return extractTextEntries(dnsLookup.get("TXT")); } finally { context.close(); } - } private static String[] extractTextEntries(Attribute nsRecords) throws NamingException { @@ -71,22 +72,35 @@ public class DNSUtil { public static CAARecord[] getCAAEntries(String domain) throws NamingException { Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory"); - InitialDirContext context = new InitialDirContext(env); - - Attributes dnsLookup = context.getAttributes(domain, new String[] { - "257" - }); - Attribute nsRecords = dnsLookup.get("257"); - if (nsRecords == null) { - return new CAARecord[] {}; - } - CAA.CAARecord[] result = new CAA.CAARecord[nsRecords.size()]; - for (int i = 0; i < result.length; i++) { - byte[] rec = (byte[]) nsRecords.get(i); - result[i] = new CAA.CAARecord(rec); + InitialDirContext context = new InitialDirContext(env); + try { + Attributes dnsLookup; + try { + dnsLookup = context.getAttributes(domain, new String[] { + "257" + }); + } catch (NameNotFoundException e) { + // We treat non-existing names as names without CAA-records + return new CAARecord[0]; + } + + Attribute nsRecords = dnsLookup.get("257"); + if (nsRecords == null) { + return new CAARecord[] {}; + } + + CAA.CAARecord[] result = new CAA.CAARecord[nsRecords.size()]; + for (int i = 0; i < result.length; i++) { + byte[] rec = (byte[]) nsRecords.get(i); + + result[i] = new CAA.CAARecord(rec); + } + + return result; + } finally { + context.close(); } - return result; } public static void main(String[] args) throws NamingException {