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