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