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