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