1 package club.wpia.gigi.util;
3 import java.util.Arrays;
4 import java.util.Hashtable;
6 import javax.naming.Context;
7 import javax.naming.NameNotFoundException;
8 import javax.naming.NamingException;
9 import javax.naming.directory.Attribute;
10 import javax.naming.directory.Attributes;
11 import javax.naming.directory.InitialDirContext;
13 import club.wpia.gigi.util.CAA.CAARecord;
15 public class DNSUtil {
17 private static InitialDirContext context;
19 Hashtable<String, String> env = new Hashtable<String, String>();
20 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
22 context = new InitialDirContext(env);
23 } catch (NamingException e) {
29 public static String[] getNSNames(String name) throws NamingException {
30 Attributes dnsLookup = context.getAttributes(name, new String[] {
33 return extractTextEntries(dnsLookup.get("NS"));
36 public static String[] getTXTEntries(String name, String server) throws NamingException {
37 Hashtable<String, String> env = new Hashtable<String, String>();
38 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
39 env.put(Context.AUTHORITATIVE, "true");
40 env.put(Context.PROVIDER_URL, "dns://" + server);
42 InitialDirContext context = new InitialDirContext(env);
44 Attributes dnsLookup = context.getAttributes(name, new String[] {
48 return extractTextEntries(dnsLookup.get("TXT"));
54 private static String[] extractTextEntries(Attribute nsRecords) throws NamingException {
55 if (nsRecords == null) {
56 return new String[] {};
58 String[] result = new String[nsRecords.size()];
59 for (int i = 0; i < result.length; i++) {
60 result[i] = (String) nsRecords.get(i);
65 public static String[] getMXEntries(String domain) throws NamingException {
66 Attributes dnsLookup = context.getAttributes(domain, new String[] {
69 return extractTextEntries(dnsLookup.get("MX"));
72 public static CAARecord[] getCAAEntries(String domain) throws NamingException {
73 Hashtable<String, String> env = new Hashtable<String, String>();
74 env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
76 InitialDirContext context = new InitialDirContext(env);
80 dnsLookup = context.getAttributes(domain, new String[] {
83 } catch (NameNotFoundException e) {
84 // We treat non-existing names as names without CAA-records
85 return new CAARecord[0];
88 Attribute nsRecords = dnsLookup.get("257");
89 if (nsRecords == null) {
90 return new CAARecord[] {};
93 CAA.CAARecord[] result = new CAA.CAARecord[nsRecords.size()];
94 for (int i = 0; i < result.length; i++) {
95 byte[] rec = (byte[]) nsRecords.get(i);
97 result[i] = new CAA.CAARecord(rec);
106 public static void main(String[] args) throws NamingException {
107 if (args[0].equals("MX")) {
108 System.out.println(Arrays.toString(getMXEntries(args[1])));
109 } else if (args[0].equals("NS")) {
110 System.out.println(Arrays.toString(getNSNames(args[1])));
111 } else if (args[0].equals("TXT")) {
112 System.out.println(Arrays.toString(getTXTEntries(args[1], args[2])));
113 } else if (args[0].equals("CAA")) {
114 System.out.println(Arrays.toString(getCAAEntries(args[1])));