Commit | Line | Data |
---|---|---|
b0f83da6 | 1 | /* |
d1cff511 | 2 | * adh-opts.c |
b0f83da6 | 3 | * - useful general-purpose resolver client program |
d1cff511 | 4 | * option handling tables etc. |
b0f83da6 | 5 | */ |
6 | /* | |
ae8cc977 | 7 | * This file is part of adns, which is |
8 | * Copyright (C) 1997-2000,2003,2006 Ian Jackson | |
9 | * Copyright (C) 1999-2000,2003,2006 Tony Finch | |
10 | * Copyright (C) 1991 Massachusetts Institute of Technology | |
11 | * (See the file INSTALL for full details.) | |
b0f83da6 | 12 | * |
13 | * This program is free software; you can redistribute it and/or modify | |
14 | * it under the terms of the GNU General Public License as published by | |
15 | * the Free Software Foundation; either version 2, or (at your option) | |
16 | * any later version. | |
17 | * | |
18 | * This program is distributed in the hope that it will be useful, | |
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | * GNU General Public License for more details. | |
22 | * | |
23 | * You should have received a copy of the GNU General Public License | |
24 | * along with this program; if not, write to the Free Software Foundation, | |
25 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
26 | */ | |
27 | ||
e3f575ff | 28 | #include "adnshost.h" |
29 | ||
d1cff511 | 30 | int ov_env=1, ov_pipe=0, ov_asynch=0; |
31 | int ov_verbose= 0; | |
e3f575ff | 32 | adns_rrtype ov_type= adns_r_none; |
d1cff511 | 33 | int ov_search=0, ov_qc_query=0, ov_qc_anshost=0, ov_qc_cname=1; |
413b9ad6 | 34 | int ov_tcp=0, ov_cname=0, ov_format=fmt_default; |
e3f575ff | 35 | char *ov_id= 0; |
36 | struct perqueryflags_remember ov_pqfr = { 1,1,1, tm_none }; | |
c8bea377 | 37 | |
e3f575ff | 38 | static const struct optioninfo global_options[]= { |
c8bea377 | 39 | { ot_desconly, "global binary options:" }, |
40 | { ot_flag, "Do not look at environment variables at all", | |
41 | "e", "env", &ov_env, 0 }, | |
42 | { ot_flag, "Read queries on stdin instead of using args", | |
43 | "f", "pipe", &ov_pipe, 1 }, | |
44 | { ot_flag, "Allow answers to be reordered", | |
45 | "a", "asynch", &ov_asynch, 1 }, | |
413b9ad6 | 46 | |
47 | { ot_desconly, "answer/error output format and destination (see below):" }, | |
48 | { ot_value, "Answers to stdout, errors as messages to stderr (default)", | |
49 | "Fs", "fmt-simple", &ov_format, fmt_simple }, | |
50 | { ot_value, "Answers and errors both to stdout in parseable format", | |
51 | "Fi", "fmt-inline", &ov_format, fmt_inline }, | |
52 | { ot_value, "Fully-parseable output format (default for --asynch)", | |
53 | "Fa", "fmt-asynch", &ov_format, fmt_asynch }, | |
c8bea377 | 54 | |
55 | { ot_desconly, "global verbosity level:" }, | |
56 | { ot_value, "Do not print anything to stderr", | |
57 | "Vq", "quiet", &ov_verbose, adns_if_noerrprint }, | |
58 | { ot_value, "Report unexpected kinds of problem only (default)", | |
59 | "Vn", "no-quiet", &ov_verbose, 0 }, | |
60 | { ot_value, "Debugging mode", | |
61 | "Vd", "debug", &ov_verbose, adns_if_debug }, | |
62 | ||
63 | { ot_desconly, "other global options:" }, | |
0ebff22d | 64 | { ot_funcarg, "Configuration to use instead of /etc/resolv.conf", |
65 | 0, "config", 0,0, of_config, "<config-text>" }, | |
aa3ffb57 | 66 | { ot_func, "Print version number", |
67 | 0, "version", 0,0, of_version }, | |
c8bea377 | 68 | { ot_func, "Print usage information", |
69 | 0, "help", 0,0, of_help }, | |
70 | ||
71 | { ot_end } | |
72 | }; | |
73 | ||
e3f575ff | 74 | static const struct optioninfo perquery_options[]= { |
c8bea377 | 75 | { ot_desconly, "per-query options:" }, |
76 | { ot_funcarg, "Query type (see below)", | |
77 | "t", "type", 0,0, &of_type, "type" }, | |
94be415a | 78 | { ot_funcarg, "Do reverse query (address -> name lookup)", |
d7449548 | 79 | "i", "ptr", 0,0, &of_ptr, "addr" }, |
2b1c6979 | 80 | { ot_funcarg2, "Lookup in in-addr-like `zone' (eg MAPS RBL)", |
81 | 0, "reverse", 0,0, &of_reverse, "addr","zone" }, | |
c8bea377 | 82 | |
83 | { ot_desconly, "per-query binary options:" }, | |
84 | { ot_flag, "Use the search list", | |
85 | "s", "search", &ov_search, 1 }, | |
86 | { ot_flag, "Let query domains contain quote-requiring chars", | |
87 | "Qq", "qc-query", &ov_qc_query, 1 }, | |
88 | { ot_flag, "Let hostnames in answers contain ...", | |
89 | "Qa", "qc-anshost", &ov_qc_anshost, 1 }, | |
90 | { ot_flag, "Prevent CNAME target domains from containing ...", | |
91 | "Qc", "qc-cname", &ov_qc_cname, 0 }, | |
92 | { ot_flag, "Force use of a virtual circuit", | |
93 | "u", "tcp", &ov_tcp, 1 }, | |
94 | { ot_flag, "Do not display owner name in output", | |
d1cff511 | 95 | "Do", "show-owner", &ov_pqfr.show_owner, 0 }, |
c8bea377 | 96 | { ot_flag, "Do not display RR type in output", |
d1cff511 | 97 | "Dt", "show-type", &ov_pqfr.show_type, 0 }, |
c8bea377 | 98 | { ot_flag, "Do not display CNAME target in output", |
d1cff511 | 99 | "Dc", "show-cname", &ov_pqfr.show_cname, 0 }, |
c8bea377 | 100 | |
101 | { ot_desconly, "per-query TTL mode (NB TTL is minimum across all info in reply):" }, | |
102 | { ot_value, "Show the TTL as a TTL", | |
d1cff511 | 103 | "Tt", "ttl-ttl", &ov_pqfr.ttl, tm_rel }, |
c8bea377 | 104 | { ot_value, "Show the TTL as a time_t when the data might expire", |
d1cff511 | 105 | "Ta", "ttl-abs", &ov_pqfr.ttl, tm_abs }, |
c8bea377 | 106 | { ot_value, "Do not show the TTL (default)", |
d1cff511 | 107 | "Tn", "no-ttl", &ov_pqfr.ttl, tm_none }, |
c8bea377 | 108 | |
109 | { ot_desconly, "per-query CNAME handling mode:" }, | |
110 | { ot_value, "Call it an error if a CNAME is found", | |
111 | "Cf", "cname-reject", &ov_cname, adns_qf_cname_forbid }, | |
112 | { ot_value, "Allow references to CNAMEs in other RRs", | |
113 | "Cl", "cname-loose", &ov_cname, adns_qf_cname_loose }, | |
114 | { ot_value, "CNAME ok for query domain, but not in RRs (default)", | |
115 | "Cs", "cname-ok", &ov_cname, 0 }, | |
116 | ||
d1cff511 | 117 | { ot_desconly, "asynchronous/pipe mode options:" }, |
c8bea377 | 118 | { ot_funcarg, "Set <id>, default is decimal sequence starting 0", |
94be415a | 119 | 0, "asynch-id", 0,0, &of_asynch_id, "id" }, |
9fa14499 | 120 | { ot_funcarg, "Cancel the query with id <id> (no error if not found)", |
c8bea377 | 121 | 0, "cancel-id", 0,0, &of_cancel_id, "id" }, |
122 | ||
123 | { ot_end } | |
124 | }; | |
b0f83da6 | 125 | |
126 | static void printusage(void) { | |
e3f575ff | 127 | static const struct optioninfo *const all_optiontables[]= { |
d1cff511 | 128 | global_options, perquery_options, 0 |
c8bea377 | 129 | }; |
130 | ||
e3f575ff | 131 | const struct optioninfo *const *oiap, *oip=0; |
c8bea377 | 132 | int maxsopt, maxlopt, l; |
133 | ||
134 | maxsopt= maxlopt= 0; | |
135 | ||
e3f575ff | 136 | for (oiap=all_optiontables; *oiap; oiap++) { |
c8bea377 | 137 | for (oip=*oiap; oip->type != ot_end; oip++) { |
138 | if (oip->type == ot_funcarg) continue; | |
139 | if (oip->sopt) { l= strlen(oip->sopt); if (l>maxsopt) maxsopt= l; } | |
140 | if (oip->lopt) { | |
141 | l= strlen(oip->lopt); | |
142 | if (oip->type == ot_flag && !oip->value) l+= 3; | |
143 | if (l>maxlopt) maxlopt= l; | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | fputs("usage: adnshost [global-opts] [query-opts] query-domain\n" | |
149 | " [[query-opts] query-domain ...]\n" | |
150 | " adnshost [global-opts] [query-opts] -f|--pipe\n", | |
151 | stdout); | |
152 | ||
e3f575ff | 153 | for (oiap=all_optiontables; *oiap; oiap++) { |
c8bea377 | 154 | putchar('\n'); |
155 | for (oip=*oiap; oip->type != ot_end; oip++) { | |
156 | switch (oip->type) { | |
157 | case ot_flag: | |
158 | if (!oip->value) { | |
159 | if (oip->sopt) { | |
160 | printf(" +%-*s --no-%-*s %s\n", | |
161 | maxsopt, oip->sopt, | |
162 | maxlopt-2, oip->lopt, | |
163 | oip->desc); | |
164 | } else { | |
165 | printf(" --no-%-*s %s\n", | |
166 | maxlopt+maxsopt+1, oip->lopt, | |
167 | oip->desc); | |
168 | } | |
169 | break; | |
170 | } | |
171 | case ot_value: case ot_func: /* fall through */ | |
172 | if (oip->sopt) { | |
173 | printf(" -%-*s --%-*s %s\n", | |
174 | maxsopt, oip->sopt, | |
175 | maxlopt+1, oip->lopt, | |
176 | oip->desc); | |
177 | } else { | |
178 | printf(" --%-*s %s\n", | |
179 | maxlopt+maxsopt+3, oip->lopt, | |
180 | oip->desc); | |
181 | } | |
182 | break; | |
183 | case ot_funcarg: | |
184 | if (oip->sopt) { | |
185 | l= (maxlopt + maxsopt - 9 - | |
186 | (strlen(oip->sopt) + strlen(oip->lopt) + 2*strlen(oip->argdesc))); | |
187 | printf(" -%s<%s> / --%s <%s>%*s%s\n", | |
188 | oip->sopt, oip->argdesc, oip->lopt, oip->argdesc, | |
189 | l>2 ? l : 2, "", | |
190 | oip->desc); | |
191 | } else { | |
192 | l= (maxlopt + maxsopt + 1 - | |
193 | (strlen(oip->lopt) + strlen(oip->argdesc))); | |
194 | printf(" --%s <%s>%*s%s\n", | |
195 | oip->lopt, oip->argdesc, | |
196 | l>2 ? l : 2, "", | |
197 | oip->desc); | |
198 | } | |
199 | break; | |
2b1c6979 | 200 | case ot_funcarg2: |
201 | assert(!oip->sopt); | |
202 | l= (maxlopt + maxsopt - 2 - | |
203 | (strlen(oip->lopt) + strlen(oip->argdesc) + strlen(oip->argdesc2))); | |
204 | printf(" --%s <%s> <%s>%*s%s\n", | |
205 | oip->lopt, oip->argdesc, oip->argdesc2, | |
206 | l>2 ? l : 2, "", | |
207 | oip->desc); | |
208 | break; | |
c8bea377 | 209 | case ot_desconly: |
210 | printf("%s\n", oip->desc); | |
211 | break; | |
212 | default: | |
213 | abort(); | |
214 | } | |
215 | } | |
216 | } | |
217 | ||
218 | printf("\nEscaping domains which might start with `-':\n" | |
219 | " - %-*s Next argument is a domain, but more options may follow\n", | |
220 | maxlopt+maxsopt+3, "<domain>"); | |
221 | ||
222 | fputs("\n" | |
223 | "Query domains should always be quoted according to master file format.\n" | |
224 | "\n" | |
225 | "For binary options, --FOO and --no-FOO are opposites, as are\n" | |
226 | "-X and +X. In each case the default is the one not listed.\n" | |
227 | "Per query options stay set a particular way until they are reset,\n" | |
228 | "whether they appear on the command line or on stdin.\n" | |
229 | "All global options must preceed the first query domain.\n" | |
230 | "\n" | |
231 | "With -f, the input should be lines with either an option, possibly\n" | |
232 | "with a value argument (separated from the option by a space if it's a long\n" | |
233 | "option), or a domain (possibly preceded by a hyphen and a space to\n" | |
234 | "distinguish it from an option).\n" | |
235 | "\n" | |
236 | "Output format is master file format without class or TTL by default:\n" | |
237 | " [<owner>] [<ttl>] [<type>] <data>\n" | |
238 | "or if the <owner> domain refers to a CNAME and --show-cname is on\n" | |
239 | " [<owner>] [<ttl>] CNAME <cname>\n" | |
240 | " [<cname>] [<ttl>] <type> <data>\n" | |
413b9ad6 | 241 | "When a query fails you get an error message to stderr (with --fmt-simple).\n" |
242 | "Specify --fmt-inline for lines like this (broken here for readability):\n" | |
57d68ed1 | 243 | " ; failed <statustype> <statusnum> <statusabbrev> \\\n" |
244 | " [<owner>] [<ttl>] [<cname>] \"<status string>\"\n" | |
413b9ad6 | 245 | "If you use --fmt-asynch, which is the default for --asynch,\n" |
246 | "each answer (success or failure) is preceded by a line\n" | |
57d68ed1 | 247 | " <id> <nrrs> <statustype> <statusnum> <statusabbrev> \\\n" |
248 | " [<owner>] [<ttl>] [<cname>] \"<status string>\"\n" | |
c8bea377 | 249 | "where <nrrs> is the number of RRs that follow and <cname> will be `$' or\n" |
250 | "the CNAME target; the CNAME indirection and error formats above are not used.\n" | |
251 | "\n" | |
252 | "Exit status:\n" | |
253 | " 0 all went well\n" | |
254 | " 1-6 at least one query failed with statustype:\n" | |
255 | " 1 localfail )\n" | |
256 | " 2 remotefail ) temporary errors\n" | |
257 | " 3 tempfail __)_________________\n" | |
258 | " 4 misconfig )\n" | |
259 | " 5 misquery ) permanent errors\n" | |
260 | " 6 permfail )\n" | |
261 | " 10 system trouble\n" | |
262 | " 11 usage problems\n" | |
263 | "\n" | |
264 | "Query types (see adns.h; default is addr):\n" | |
eeee7503 MW |
265 | " ns soa ptr mx rp srv addr - enhanced versions\n" |
266 | " cname hinfo txt - types with only one version\n" | |
267 | " a aaaa ns- soa- ptr- mx- rp- srv- - _raw versions\n" | |
268 | " type<number> - `unknown' type, RFC3597\n" | |
94be415a | 269 | "Default is addr, or ptr for -i/--ptr queries\n", |
c8bea377 | 270 | stdout); |
271 | if (ferror(stdout)) sysfail("write usage message",errno); | |
b0f83da6 | 272 | } |
273 | ||
aa3ffb57 | 274 | void of_version(const struct optioninfo *oi, const char *arg, const char *arg2) { |
275 | VERSION_PRINT_QUIT("adnshost"); | |
276 | } | |
277 | ||
2b1c6979 | 278 | void of_help(const struct optioninfo *oi, const char *arg, const char *arg2) { |
b0f83da6 | 279 | printusage(); |
280 | if (fclose(stdout)) sysfail("finish writing output",errno); | |
0ebff22d | 281 | quitnow(0); |
b0f83da6 | 282 | } |
c8bea377 | 283 | |
e3f575ff | 284 | typedef int comparer_type(const char **optp, const struct optioninfo *entry); |
285 | ||
286 | static int oc_long(const char **optp, const struct optioninfo *entry) { | |
287 | return entry->lopt && !strcmp(*optp,entry->lopt); | |
288 | } | |
289 | ||
290 | static int oc_short(const char **optp, const struct optioninfo *entry) { | |
291 | const char *sopt; | |
292 | int l; | |
293 | ||
294 | sopt= entry->sopt; | |
295 | if (!sopt) return 0; | |
296 | l= strlen(sopt); | |
297 | if (memcmp(*optp,sopt,l)) return 0; | |
298 | (*optp) += l; | |
299 | return 1; | |
300 | } | |
301 | ||
302 | static const struct optioninfo *find1(const char **optp, | |
303 | const struct optioninfo *table, | |
304 | comparer_type *comparer) { | |
305 | for (;;) { | |
306 | if (table->type == ot_end) return 0; | |
307 | if (comparer(optp,table)) return table; | |
308 | table++; | |
309 | } | |
310 | } | |
311 | ||
312 | static const struct optioninfo *find(const char **optp, | |
313 | const char *prefix, | |
314 | comparer_type *comparer) { | |
315 | const struct optioninfo *oip; | |
4f257c51 | 316 | const char *opt; |
e3f575ff | 317 | |
4f257c51 | 318 | opt= *optp; |
e3f575ff | 319 | oip= find1(optp,perquery_options,comparer); |
320 | if (oip) return oip; | |
321 | oip= find1(optp,global_options,comparer); | |
4f257c51 | 322 | if (!oip) usageerr("unknown option %s%s",prefix,opt); |
323 | if (ads) usageerr("global option %s%s specified after query domain(s)",prefix,opt); | |
e3f575ff | 324 | return oip; |
325 | } | |
326 | ||
327 | const struct optioninfo *opt_findl(const char *opt) { return find(&opt,"--",oc_long); } | |
328 | const struct optioninfo *opt_finds(const char **optp) { return find(optp,"-",oc_short); } | |
329 | ||
0b6f56cc | 330 | static void noninvert(const struct optioninfo *oip) NONRETURNING; |
331 | static void noninvert(const struct optioninfo *oip) { | |
332 | usageerr("option %s%s%s%s%s may not be inverted", | |
333 | oip->sopt ? "-" : "", oip->sopt ? oip->sopt : "", | |
334 | oip->lopt && oip->sopt ? " / " : "", | |
335 | oip->lopt ? "--" : "", oip->lopt ? oip->lopt : ""); | |
336 | } | |
337 | ||
2b1c6979 | 338 | void opt_do(const struct optioninfo *oip, int invert, |
339 | const char *arg, const char *arg2) { | |
e3f575ff | 340 | switch (oip->type) { |
0b6f56cc | 341 | case ot_flag: |
342 | assert(!arg); | |
e0d855e1 | 343 | *oip->storep= !invert; |
0b6f56cc | 344 | return; |
345 | case ot_value: | |
e3f575ff | 346 | assert(!arg); |
0b6f56cc | 347 | if (invert) noninvert(oip); |
e3f575ff | 348 | *oip->storep= oip->value; |
349 | return; | |
2b1c6979 | 350 | case ot_func: case ot_funcarg: case ot_funcarg2: |
0b6f56cc | 351 | if (invert) noninvert(oip); |
2b1c6979 | 352 | oip->func(oip,arg,arg2); |
e3f575ff | 353 | return; |
354 | default: | |
355 | abort(); | |
356 | } | |
357 | } |