]> WPIA git - gigi.git/blob - src/club/wpia/gigi/util/DNSUtil.java
fix: Close resources we no longer need
[gigi.git] / src / club / wpia / gigi / util / DNSUtil.java
1 package club.wpia.gigi.util;
2
3 import java.util.Arrays;
4 import java.util.Hashtable;
5
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;
12
13 import club.wpia.gigi.util.CAA.CAARecord;
14
15 public class DNSUtil {
16
17     private static InitialDirContext context;
18     static {
19         Hashtable<String, String> env = new Hashtable<String, String>();
20         env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
21         try {
22             context = new InitialDirContext(env);
23         } catch (NamingException e) {
24             e.printStackTrace();
25         }
26
27     }
28
29     public static String[] getNSNames(String name) throws NamingException {
30         Attributes dnsLookup = context.getAttributes(name, new String[] {
31                 "NS"
32         });
33         return extractTextEntries(dnsLookup.get("NS"));
34     }
35
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);
41
42         InitialDirContext context = new InitialDirContext(env);
43         try {
44             Attributes dnsLookup = context.getAttributes(name, new String[] {
45                     "TXT"
46             });
47
48             return extractTextEntries(dnsLookup.get("TXT"));
49         } finally {
50             context.close();
51         }
52     }
53
54     private static String[] extractTextEntries(Attribute nsRecords) throws NamingException {
55         if (nsRecords == null) {
56             return new String[] {};
57         }
58         String[] result = new String[nsRecords.size()];
59         for (int i = 0; i < result.length; i++) {
60             result[i] = (String) nsRecords.get(i);
61         }
62         return result;
63     }
64
65     public static String[] getMXEntries(String domain) throws NamingException {
66         Attributes dnsLookup = context.getAttributes(domain, new String[] {
67                 "MX"
68         });
69         return extractTextEntries(dnsLookup.get("MX"));
70     }
71
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");
75
76         InitialDirContext context = new InitialDirContext(env);
77         try {
78             Attributes dnsLookup;
79             try {
80                 dnsLookup = context.getAttributes(domain, new String[] {
81                     "257"
82                 });
83             } catch (NameNotFoundException e) {
84                 // We treat non-existing names as names without CAA-records
85                 return new CAARecord[0];
86             }
87
88             Attribute nsRecords = dnsLookup.get("257");
89             if (nsRecords == null) {
90                 return new CAARecord[] {};
91             }
92
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);
96
97                 result[i] = new CAA.CAARecord(rec);
98             }
99
100             return result;
101         } finally {
102             context.close();
103         }
104     }
105
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])));
115         }
116     }
117
118 }