add initial query string parsing code
[captive-validator.git] / src / com / verisign / cl / DNSSECReconciler.java
1 package com.verisign.cl;
2
3 import java.util.*;
4
5 import org.xbill.DNS.*;
6
7 import com.verisign.tat.dnssec.CaptiveValidator;
8
9 public class DNSSECReconciler {
10
11     /**
12      * Invoke with java -jar dnssecreconciler.jar server=127.0.0.1 \
13      * query_file=queries.txt dnskey_query=net dnskey_query=edu
14      */
15     private CaptiveValidator validator;
16
17     // Options
18     public String server;
19     public String query;
20     public String queryFile;
21     public String dnskeyFile;
22     public List<String> dnskeyNames;
23
24     DNSSECReconciler() {
25         validator = new CaptiveValidator();
26     }
27
28     /**
29      * Convert a query line of the form: <qname> <qtype> <flags> to a request
30      * message.
31      *
32      * @param query_line
33      * @return A query message
34      * @throws TextParseException
35      * @throws NameTooLongException
36      */
37     private Message queryFromString(String query_line)
38             throws TextParseException, NameTooLongException {
39
40         String[] tokens = query_line.split("[ \t]+");
41
42         Name qname = null;
43         int qtype = -1;
44         int qclass = -1;
45
46         if (tokens.length < 1)
47             return null;
48         qname = Name.fromString(tokens[0]);
49         if (!qname.isAbsolute()) {
50             qname = Name.concatenate(qname, Name.root);
51         }
52
53         for (int i = 1; i < tokens.length; i++) {
54             if (tokens[i].startsWith("+")) {
55                 // For now, we ignore flags as uninteresting
56                 continue;
57             }
58
59             int type = Type.value(tokens[i]);
60             if (type > 0) {
61                 qtype = type;
62                 continue;
63             }
64             int cls = DClass.value(tokens[i]);
65             if (cls > 0) {
66                 qclass = cls;
67                 continue;
68             }
69         }
70         if (qtype < 0) {
71             qtype = Type.A;
72         }
73         if (qclass < 0) {
74             qclass = DClass.IN;
75         }
76
77         Message query = Message.newQuery(Record.newRecord(qname, qtype, qclass));
78
79         return query;
80     }
81
82     public void execute() {
83     }
84
85     private static void usage() {
86         System.err.println("usage: java -jar dnssecreconiler.jar [..options..]");
87         System.err.println("       server: the DNS server to query.");
88         System.err.println("       query: a name [type [flags]] string.");
89         System.err.println("       query_file: a list of queries, one query per line.");
90         System.err.println("       dnskey_file: a file containing DNSKEY RRs to trust.");
91         System.err.println("       dnskey_query: query 'server' for DNSKEY at given name to trust, may repeat");
92     }
93
94     public static int main(String[] argv) {
95
96         DNSSECReconciler dr = new DNSSECReconciler();
97
98         try {
99             // Parse the command line options
100             for (String arg : argv) {
101
102                 if (arg.indexOf('=') < 0) {
103                     System.err.println("Unrecognized option: " + arg);
104                     usage();
105                     return 1;
106                 }
107
108                 String[] split_arg = arg.split("[ \t]*=[ \t]*", 2);
109                 String opt = split_arg[0];
110                 String optarg = split_arg[1];
111
112                 if (opt.equals("server")) {
113                     dr.server = optarg;
114                 } else if (opt.equals("query_file")) {
115                     dr.queryFile = optarg;
116                 } else if (opt.equals("dnskey_file")) {
117                     dr.dnskeyFile = optarg;
118                 } else if (opt.equals("dnskey_query")) {
119                     if (dr.dnskeyNames == null) {
120                         dr.dnskeyNames = new ArrayList<String>();
121                     }
122                     dr.dnskeyNames.add(optarg);
123                 } else {
124                     System.err.println("Unrecognized option: " + opt);
125                     usage();
126                     return 1;
127                 }
128             }
129
130             // Check for minimum usage
131             if (dr.server == null) {
132                 System.err.println("'server' must be specified");
133                 usage();
134                 return 1;
135             }
136             if (dr.query == null && dr.queryFile == null) {
137                 System.err.println("Either 'query' or 'query_file' must be specified");
138                 usage();
139                 return 1;
140             }
141             if (dr.dnskeyFile == null && dr.dnskeyNames == null) {
142                 System.err.println("Either 'dnskey_file' or 'dnskey_query' must be specified");
143                 usage();
144                 return 1;
145             }
146
147             // Execute the job
148             dr.execute();
149
150         } catch (Exception e) {
151             e.printStackTrace();
152             return 1;
153         }
154
155         return 0;
156     }
157 }