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