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