2 * Copyright (C) 2004-2013 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: dig.c,v 1.245 2011/12/07 17:23:28 each Exp $ */
28 #include <isc/netaddr.h>
29 #include <isc/parseint.h>
30 #include <isc/print.h>
31 #include <isc/string.h>
35 #include <dns/byaddr.h>
36 #include <dns/fixedname.h>
37 #include <dns/masterdump.h>
38 #include <dns/message.h>
40 #include <dns/rdata.h>
41 #include <dns/rdataset.h>
42 #include <dns/rdatatype.h>
43 #include <dns/rdataclass.h>
44 #include <dns/result.h>
49 #define ADD_STRING(b, s) { \
50 if (strlen(s) >= isc_buffer_availablelength(b)) \
51 return (ISC_R_NOSPACE); \
53 isc_buffer_putstr(b, s); \
56 #define DIG_MAX_ADDRESSES 20
58 dig_lookup_t *default_lookup = NULL;
60 static char *batchname = NULL;
61 static FILE *batchfp = NULL;
63 static int addresscount = 0;
65 static char domainopt[DNS_NAME_MAXTEXT];
67 static isc_boolean_t short_form = ISC_FALSE, printcmd = ISC_TRUE,
68 ip6_int = ISC_FALSE, plusquest = ISC_FALSE, pluscomm = ISC_FALSE,
69 multiline = ISC_FALSE, nottl = ISC_FALSE, noclass = ISC_FALSE,
70 onesoa = ISC_FALSE, rrcomments = ISC_FALSE;
71 static isc_uint32_t splitwidth = 0xffffffff;
74 static const char * const opcodetext[] = {
93 /*% return code text */
94 static const char * const rcodetext[] = {
114 /*% safe rcodetext[] */
116 rcode_totext(dns_rcode_t rcode)
118 static char buf[sizeof("?65535")];
120 const char *consttext;
124 if (rcode >= (sizeof(rcodetext)/sizeof(rcodetext[0]))) {
125 snprintf(buf, sizeof(buf), "?%u", rcode);
126 totext.deconsttext = buf;
128 totext.consttext = rcodetext[rcode];
129 return totext.deconsttext;
134 print_usage(FILE *fp) {
136 "Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}\n"
137 " {global-d-opt} host [@local-server] {local-d-opt}\n"
138 " [ host [@local-server] {local-d-opt} [...]]\n", fp);
141 ISC_PLATFORM_NORETURN_PRE static void
142 usage(void) ISC_PLATFORM_NORETURN_POST;
147 fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
148 "for complete list of options\n", stderr);
155 fputs("DiG " VERSION "\n", stderr);
163 "Where: domain is in the Domain Name System\n"
164 " q-class is one of (in,hs,ch,...) [default: in]\n"
165 " q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
166 " (Use ixfr=version for type ixfr)\n"
167 " q-opt is one of:\n"
168 " -x dot-notation (shortcut for reverse lookups)\n"
169 " -i (use IP6.INT for IPv6 reverse lookups)\n"
170 " -f filename (batch mode)\n"
171 " -b address[#port] (bind to source address/port)\n"
172 " -p port (specify port number)\n"
173 " -q name (specify query name)\n"
174 " -t type (specify query type)\n"
175 " -c class (specify query class)\n"
176 " -k keyfile (specify tsig key file)\n"
177 " -y [hmac:]name:key (specify named base64 tsig key)\n"
178 " -4 (use IPv4 query transport only)\n"
179 " -6 (use IPv6 query transport only)\n"
180 " -m (enable memory usage debugging)\n"
181 " d-opt is of the form +keyword[=value], where keyword is:\n"
182 " +[no]vc (TCP mode)\n"
183 " +[no]tcp (TCP mode, alternate syntax)\n"
184 " +time=### (Set query timeout) [5]\n"
185 " +tries=### (Set number of UDP attempts) [3]\n"
186 " +retry=### (Set number of UDP retries) [2]\n"
187 " +domain=### (Set default domainname)\n"
188 " +bufsize=### (Set EDNS0 Max UDP packet size)\n"
189 " +ndots=### (Set NDOTS value)\n"
190 " +[no]edns[=###] (Set EDNS version) [0]\n"
191 " +[no]search (Set whether to use searchlist)\n"
192 " +[no]showsearch (Search with intermediate results)\n"
193 " +[no]defname (Ditto)\n"
194 " +[no]recurse (Recursive mode)\n"
195 " +[no]ignore (Don't revert to TCP for TC responses.)"
197 " +[no]fail (Don't try next server on SERVFAIL)\n"
198 " +[no]besteffort (Try to parse even illegal messages)\n"
199 " +[no]aaonly (Set AA flag in query (+[no]aaflag))\n"
200 " +[no]adflag (Set AD flag in query)\n"
201 " +[no]cdflag (Set CD flag in query)\n"
202 " +[no]cl (Control display of class in records)\n"
203 " +[no]cmd (Control display of command line)\n"
204 " +[no]comments (Control display of comment lines)\n"
205 " +[no]rrcomments (Control display of per-record "
207 " +[no]question (Control display of question)\n"
208 " +[no]answer (Control display of answer)\n"
209 " +[no]authority (Control display of authority)\n"
210 " +[no]additional (Control display of additional)\n"
211 " +[no]stats (Control display of statistics)\n"
212 " +[no]short (Disable everything except short\n"
214 " +[no]ttlid (Control display of ttls in records)\n"
215 " +[no]all (Set or clear all display flags)\n"
216 " +[no]qr (Print question before sending)\n"
217 " +[no]nssearch (Search all authoritative nameservers)\n"
218 " +[no]identify (ID responders in short answers)\n"
219 " +[no]trace (Trace delegation down from root [+dnssec])\n"
220 " +[no]dnssec (Request DNSSEC records)\n"
221 " +[no]nsid (Request Name Server ID)\n"
223 " +[no]sigchase (Chase DNSSEC signatures)\n"
224 " +trusted-key=#### (Trusted Key when chasing DNSSEC sigs)\n"
226 " +[no]topdown (Do DNSSEC validation top down mode)\n"
229 " +[no]split=## (Split hex/base64 fields into chunks)\n"
230 " +[no]multiline (Print records in an expanded format)\n"
231 " +[no]onesoa (AXFR prints only one soa record)\n"
232 " +[no]keepopen (Keep the TCP socket open between queries)\n"
233 " global d-opts and servers (before host name) affect all queries.\n"
234 " local d-opts and servers (after host name) affect only that lookup.\n"
235 " -h (print help and exit)\n"
236 " -v (print version and exit)\n",
241 * Callback from dighost.c to print the received message.
244 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
250 char fromtext[ISC_SOCKADDR_FORMATSIZE];
252 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
256 if (query->lookup->stats && !short_form) {
257 diff = isc_time_microdiff(&now, &query->time_sent);
258 printf(";; Query time: %ld msec\n", (long int)diff/1000);
259 printf(";; SERVER: %s(%s)\n", fromtext, query->servname);
261 tmnow = *localtime(&tnow);
262 if (strftime(time_str, sizeof(time_str),
263 "%a %b %d %H:%M:%S %Z %Y", &tmnow) > 0U)
264 printf(";; WHEN: %s\n", time_str);
265 if (query->lookup->doing_xfr) {
266 printf(";; XFR size: %u records (messages %u, "
267 "bytes %" ISC_PRINT_QUADFORMAT "u)\n",
268 query->rr_count, query->msg_count,
271 printf(";; MSG SIZE rcvd: %u\n", bytes);
275 puts(";; WARNING -- Some TSIG could not "
278 if ((key == NULL) && (keysecret[0] != 0)) {
279 puts(";; WARNING -- TSIG key was not used.");
282 } else if (query->lookup->identify && !short_form) {
283 diff = isc_time_microdiff(&now, &query->time_sent);
284 printf(";; Received %" ISC_PRINT_QUADFORMAT "u bytes "
285 "from %s(%s) in %d ms\n\n",
286 query->lookup->doing_xfr ?
287 query->byte_count : (isc_uint64_t)bytes,
288 fromtext, query->userarg,
294 * Callback from dighost.c to print that it is trying a server.
299 trying(char *frm, dig_lookup_t *lookup) {
305 * Internal print routine used to print short form replies.
308 say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
312 char store[sizeof("12345678901234567890")];
314 if (query->lookup->trace || query->lookup->ns_search_only) {
315 result = dns_rdatatype_totext(rdata->type, buf);
316 if (result != ISC_R_SUCCESS)
318 ADD_STRING(buf, " ");
320 result = dns_rdata_totext(rdata, NULL, buf);
321 if (result == ISC_R_NOSPACE)
323 check_result(result, "dns_rdata_totext");
324 if (query->lookup->identify) {
326 diff = isc_time_microdiff(&now, &query->time_sent);
327 ADD_STRING(buf, " from server ");
328 ADD_STRING(buf, query->servname);
329 snprintf(store, 19, " in %d ms.", (int)diff/1000);
330 ADD_STRING(buf, store);
332 ADD_STRING(buf, "\n");
333 return (ISC_R_SUCCESS);
337 * short_form message print handler. Calls above say_message()
340 short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
341 isc_buffer_t *buf, dig_query_t *query)
344 dns_rdataset_t *rdataset;
345 isc_result_t result, loopresult;
346 dns_name_t empty_name;
347 dns_rdata_t rdata = DNS_RDATA_INIT;
351 dns_name_init(&empty_name, NULL);
352 result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
353 if (result == ISC_R_NOMORE)
354 return (ISC_R_SUCCESS);
355 else if (result != ISC_R_SUCCESS)
360 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
362 for (rdataset = ISC_LIST_HEAD(name->list);
364 rdataset = ISC_LIST_NEXT(rdataset, link)) {
365 loopresult = dns_rdataset_first(rdataset);
366 while (loopresult == ISC_R_SUCCESS) {
367 dns_rdataset_current(rdataset, &rdata);
368 result = say_message(&rdata, query,
370 if (result == ISC_R_NOSPACE)
372 check_result(result, "say_message");
373 loopresult = dns_rdataset_next(rdataset);
374 dns_rdata_reset(&rdata);
377 result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
378 if (result == ISC_R_NOMORE)
380 else if (result != ISC_R_SUCCESS)
384 return (ISC_R_SUCCESS);
388 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
389 isc_buffer_t *target)
392 dns_master_style_t *style = NULL;
393 unsigned int styleflags = 0;
395 if (rdataset == NULL || owner_name == NULL || target == NULL)
398 styleflags |= DNS_STYLEFLAG_REL_OWNER;
400 styleflags |= DNS_STYLEFLAG_NO_TTL;
402 styleflags |= DNS_STYLEFLAG_NO_CLASS;
404 styleflags |= DNS_STYLEFLAG_RRCOMMENT;
406 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
407 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
408 styleflags |= DNS_STYLEFLAG_REL_DATA;
409 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
410 styleflags |= DNS_STYLEFLAG_TTL;
411 styleflags |= DNS_STYLEFLAG_MULTILINE;
412 styleflags |= DNS_STYLEFLAG_COMMENT;
413 styleflags |= DNS_STYLEFLAG_RRCOMMENT;
416 if (multiline || (nottl && noclass))
417 result = dns_master_stylecreate2(&style, styleflags,
418 24, 24, 24, 32, 80, 8,
420 else if (nottl || noclass)
421 result = dns_master_stylecreate2(&style, styleflags,
422 24, 24, 32, 40, 80, 8,
425 result = dns_master_stylecreate2(&style, styleflags,
426 24, 32, 40, 48, 80, 8,
428 check_result(result, "dns_master_stylecreate");
430 result = dns_master_rdatasettotext(owner_name, rdataset, style, target);
433 dns_master_styledestroy(&style, mctx);
440 * Callback from dighost.c to print the reply from a server
443 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
445 dns_messagetextflag_t flags;
446 isc_buffer_t *buf = NULL;
447 unsigned int len = OUTPUTBUF;
448 dns_master_style_t *style = NULL;
449 unsigned int styleflags = 0;
451 styleflags |= DNS_STYLEFLAG_REL_OWNER;
452 if (query->lookup->comments)
453 styleflags |= DNS_STYLEFLAG_COMMENT;
455 styleflags |= DNS_STYLEFLAG_RRCOMMENT;
457 styleflags |= DNS_STYLEFLAG_NO_TTL;
459 styleflags |= DNS_STYLEFLAG_NO_CLASS;
461 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
462 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
463 styleflags |= DNS_STYLEFLAG_REL_DATA;
464 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
465 styleflags |= DNS_STYLEFLAG_TTL;
466 styleflags |= DNS_STYLEFLAG_MULTILINE;
467 styleflags |= DNS_STYLEFLAG_RRCOMMENT;
469 if (multiline || (nottl && noclass))
470 result = dns_master_stylecreate2(&style, styleflags,
471 24, 24, 24, 32, 80, 8,
473 else if (nottl || noclass)
474 result = dns_master_stylecreate2(&style, styleflags,
475 24, 24, 32, 40, 80, 8,
478 result = dns_master_stylecreate2(&style, styleflags,
479 24, 32, 40, 48, 80, 8,
481 check_result(result, "dns_master_stylecreate");
483 if (query->lookup->cmdline[0] != 0) {
485 fputs(query->lookup->cmdline, stdout);
486 query->lookup->cmdline[0]=0;
488 debug("printmessage(%s %s %s)", headers ? "headers" : "noheaders",
489 query->lookup->comments ? "comments" : "nocomments",
490 short_form ? "short_form" : "long_form");
494 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
495 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
497 if (onesoa && query->lookup->rdtype == dns_rdatatype_axfr)
498 flags |= (query->msg_count == 0) ? DNS_MESSAGETEXTFLAG_ONESOA :
499 DNS_MESSAGETEXTFLAG_OMITSOA;
500 if (!query->lookup->comments)
501 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
503 result = isc_buffer_allocate(mctx, &buf, len);
504 check_result(result, "isc_buffer_allocate");
506 if (query->lookup->comments && !short_form) {
507 if (query->lookup->cmdline[0] != 0)
508 printf("; %s\n", query->lookup->cmdline);
509 if (msg == query->lookup->sendmsg)
510 printf(";; Sending:\n");
512 printf(";; Got answer:\n");
515 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
517 opcodetext[msg->opcode],
518 rcode_totext(msg->rcode),
521 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
523 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
525 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
527 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
529 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
531 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
533 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
535 if ((msg->flags & 0x0040U) != 0)
536 printf("; MBZ: 0x4");
538 printf("; QUERY: %u, ANSWER: %u, "
539 "AUTHORITY: %u, ADDITIONAL: %u\n",
540 msg->counts[DNS_SECTION_QUESTION],
541 msg->counts[DNS_SECTION_ANSWER],
542 msg->counts[DNS_SECTION_AUTHORITY],
543 msg->counts[DNS_SECTION_ADDITIONAL]);
545 if (msg != query->lookup->sendmsg &&
546 (msg->flags & DNS_MESSAGEFLAG_RD) != 0 &&
547 (msg->flags & DNS_MESSAGEFLAG_RA) == 0)
548 printf(";; WARNING: recursion requested "
549 "but not available\n");
551 if (msg != query->lookup->sendmsg &&
552 query->lookup->edns != -1 && msg->opt == NULL &&
553 (msg->rcode == dns_rcode_formerr ||
554 msg->rcode == dns_rcode_notimp))
555 printf("\n;; WARNING: EDNS query returned status "
556 "%s - retry with '%s+noedns'\n",
557 rcode_totext(msg->rcode),
558 query->lookup->dnssec ? "+nodnssec ": "");
559 if (msg != query->lookup->sendmsg && extrabytes != 0U)
560 printf(";; WARNING: Message has %u extra byte%s at "
561 "end\n", extrabytes, extrabytes != 0 ? "s" : "");
566 if (query->lookup->comments && headers && !short_form) {
567 result = dns_message_pseudosectiontotext(msg,
568 DNS_PSEUDOSECTION_OPT,
570 if (result == ISC_R_NOSPACE) {
573 isc_buffer_free(&buf);
574 result = isc_buffer_allocate(mctx, &buf, len);
575 if (result == ISC_R_SUCCESS)
576 goto repopulate_buffer;
581 "dns_message_pseudosectiontotext");
584 if (query->lookup->section_question && headers) {
586 result = dns_message_sectiontotext(msg,
587 DNS_SECTION_QUESTION,
589 if (result == ISC_R_NOSPACE)
591 check_result(result, "dns_message_sectiontotext");
594 if (query->lookup->section_answer) {
596 result = dns_message_sectiontotext(msg,
599 if (result == ISC_R_NOSPACE)
601 check_result(result, "dns_message_sectiontotext");
603 result = short_answer(msg, flags, buf, query);
604 if (result == ISC_R_NOSPACE)
606 check_result(result, "short_answer");
609 if (query->lookup->section_authority) {
611 result = dns_message_sectiontotext(msg,
612 DNS_SECTION_AUTHORITY,
614 if (result == ISC_R_NOSPACE)
616 check_result(result, "dns_message_sectiontotext");
619 if (query->lookup->section_additional) {
621 result = dns_message_sectiontotext(msg,
622 DNS_SECTION_ADDITIONAL,
624 if (result == ISC_R_NOSPACE)
626 check_result(result, "dns_message_sectiontotext");
628 * Only print the signature on the first record.
631 result = dns_message_pseudosectiontotext(
633 DNS_PSEUDOSECTION_TSIG,
635 if (result == ISC_R_NOSPACE)
638 "dns_message_pseudosectiontotext");
639 result = dns_message_pseudosectiontotext(
641 DNS_PSEUDOSECTION_SIG0,
643 if (result == ISC_R_NOSPACE)
646 "dns_message_pseudosectiontotext");
651 if (headers && query->lookup->comments && !short_form)
654 printf("%.*s", (int)isc_buffer_usedlength(buf),
655 (char *)isc_buffer_base(buf));
656 isc_buffer_free(&buf);
660 dns_master_styledestroy(&style, mctx);
665 * print the greeting message when the program first starts up.
668 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
671 static isc_boolean_t first = ISC_TRUE;
675 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
676 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
677 "%s; <<>> DiG " VERSION " <<>>",
681 snprintf(append, sizeof(append), " %s", argv[i++]);
682 remaining = sizeof(lookup->cmdline) -
683 strlen(lookup->cmdline) - 1;
684 strncat(lookup->cmdline, append, remaining);
686 remaining = sizeof(lookup->cmdline) -
687 strlen(lookup->cmdline) - 1;
688 strncat(lookup->cmdline, "\n", remaining);
689 if (first && addresscount != 0) {
690 snprintf(append, sizeof(append),
691 "; (%d server%s found)\n",
693 addresscount > 1 ? "s" : "");
694 remaining = sizeof(lookup->cmdline) -
695 strlen(lookup->cmdline) - 1;
696 strncat(lookup->cmdline, append, remaining);
699 snprintf(append, sizeof(append),
700 ";; global options:%s%s\n",
701 short_form ? " +short" : "",
702 printcmd ? " +cmd" : "");
704 remaining = sizeof(lookup->cmdline) -
705 strlen(lookup->cmdline) - 1;
706 strncat(lookup->cmdline, append, remaining);
712 * We're not using isc_commandline_parse() here since the command line
713 * syntax of dig is quite a bit different from that which can be described
719 plus_option(char *option, isc_boolean_t is_batchfile,
720 dig_lookup_t *lookup)
723 char option_store[256];
724 char *cmd, *value, *ptr;
726 isc_boolean_t state = ISC_TRUE;
731 strncpy(option_store, option, sizeof(option_store));
732 option_store[sizeof(option_store)-1]=0;
734 cmd = next_token(&ptr,"=");
736 printf(";; Invalid option %s\n", option_store);
740 if (strncasecmp(cmd, "no", 2)==0) {
745 #define FULLCHECK(A) \
747 size_t _l = strlen(cmd); \
748 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
749 goto invalid_option; \
751 #define FULLCHECK2(A, B) \
753 size_t _l = strlen(cmd); \
754 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
755 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
756 goto invalid_option; \
762 case 'a': /* aaonly / aaflag */
763 FULLCHECK2("aaonly", "aaflag");
764 lookup->aaonly = state;
768 case 'd': /* additional */
769 FULLCHECK("additional");
770 lookup->section_additional = state;
772 case 'f': /* adflag */
773 case '\0': /* +ad is a synonym for +adflag */
775 lookup->adflag = state;
783 lookup->section_question = state;
784 lookup->section_authority = state;
785 lookup->section_answer = state;
786 lookup->section_additional = state;
787 lookup->comments = state;
789 lookup->stats = state;
792 case 'n': /* answer */
794 lookup->section_answer = state;
796 case 'u': /* authority */
797 FULLCHECK("authority");
798 lookup->section_authority = state;
806 case 'e':/* besteffort */
807 FULLCHECK("besteffort");
808 lookup->besteffort = state;
810 case 'u':/* bufsize */
811 FULLCHECK("bufsize");
816 result = parse_uint(&num, value, COMMSIZE,
818 if (result != ISC_R_SUCCESS)
819 fatal("Couldn't parse buffer size");
820 lookup->udpsize = num;
828 case 'd':/* cdflag */
830 case 'f': /* cdflag */
831 case '\0': /* +cd is a synonym for +cdflag */
833 lookup->cdflag = state;
841 noclass = ISC_TF(!state);
847 case 'o': /* comments */
848 FULLCHECK("comments");
849 lookup->comments = state;
850 if (lookup == default_lookup)
859 case 'e': /* defname */
860 FULLCHECK("defname");
861 if (!lookup->trace) {
865 case 'n': /* dnssec */
867 if (state && lookup->edns == -1)
869 lookup->dnssec = state;
871 case 'o': /* domain */
877 strncpy(domainopt, value, sizeof(domainopt));
878 domainopt[sizeof(domainopt)-1] = '\0';
894 result = parse_uint(&num, value, 255, "edns");
895 if (result != ISC_R_SUCCESS)
896 fatal("Couldn't parse edns");
901 lookup->servfail_stops = state;
905 case 'd': /* identify */
906 FULLCHECK("identify");
907 lookup->identify = state;
909 case 'g': /* ignore */
910 default: /* Inherits default for compatibility */
912 lookup->ignore = ISC_TRUE;
916 FULLCHECK("keepopen");
919 case 'm': /* multiline */
920 FULLCHECK("multiline");
925 case 'd': /* ndots */
931 result = parse_uint(&num, value, MAXNDOTS, "ndots");
932 if (result != ISC_R_SUCCESS)
933 fatal("Couldn't parse ndots");
940 if (state && lookup->edns == -1)
942 lookup->nsid = state;
944 case 's': /* nssearch */
945 FULLCHECK("nssearch");
946 lookup->ns_search_only = state;
948 lookup->trace_root = ISC_TRUE;
949 lookup->recurse = ISC_TRUE;
950 lookup->identify = ISC_TRUE;
951 lookup->stats = ISC_FALSE;
952 lookup->comments = ISC_FALSE;
953 rrcomments = ISC_FALSE;
954 lookup->section_additional = ISC_FALSE;
955 lookup->section_authority = ISC_FALSE;
956 lookup->section_question = ISC_FALSE;
957 lookup->rdtype = dns_rdatatype_ns;
958 lookup->rdtypeset = ISC_TRUE;
959 short_form = ISC_TRUE;
980 case 'u': /* question */
981 FULLCHECK("question");
982 lookup->section_question = state;
983 if (lookup == default_lookup)
994 case 'c': /* recurse */
995 FULLCHECK("recurse");
996 lookup->recurse = state;
998 case 't': /* retry / retries */
999 FULLCHECK2("retry", "retries");
1003 goto invalid_option;
1004 result = parse_uint(&lookup->retries, value,
1005 MAXTRIES - 1, "retries");
1006 if (result != ISC_R_SUCCESS)
1007 fatal("Couldn't parse retries");
1011 goto invalid_option;
1014 case 'r': /* rrcomments */
1015 FULLCHECK("rrcomments");
1019 goto invalid_option;
1024 case 'e': /* search */
1025 FULLCHECK("search");
1026 if (!lookup->trace) {
1032 goto invalid_option;
1034 case 'r': /* short */
1038 printcmd = ISC_FALSE;
1039 lookup->section_additional = ISC_FALSE;
1040 lookup->section_answer = ISC_TRUE;
1041 lookup->section_authority = ISC_FALSE;
1042 lookup->section_question = ISC_FALSE;
1043 lookup->comments = ISC_FALSE;
1044 rrcomments = ISC_FALSE;
1045 lookup->stats = ISC_FALSE;
1048 case 'w': /* showsearch */
1049 FULLCHECK("showsearch");
1050 if (!lookup->trace) {
1056 goto invalid_option;
1060 case 'i': /* sigchase */
1061 FULLCHECK("sigchase");
1062 lookup->sigchase = state;
1063 if (lookup->sigchase)
1064 lookup->dnssec = ISC_TRUE;
1067 case 'p': /* split */
1069 if (value != NULL && !state)
1070 goto invalid_option;
1074 } else if (value == NULL)
1077 result = parse_uint(&splitwidth, value,
1079 if (splitwidth % 4 != 0) {
1080 splitwidth = ((splitwidth + 3) / 4) * 4;
1081 fprintf(stderr, ";; Warning, split must be "
1082 "a multiple of 4; adjusting "
1083 "to %d\n", splitwidth);
1086 * There is an adjustment done in the
1087 * totext_<rrtype>() functions which causes
1088 * splitwidth to shrink. This is okay when we're
1089 * using the default width but incorrect in this
1090 * case, so we correct for it
1094 if (result != ISC_R_SUCCESS)
1095 fatal("Couldn't parse retries");
1097 case 't': /* stats */
1099 lookup->stats = state;
1102 goto invalid_option;
1110 lookup->tcp_mode = state;
1112 case 'i': /* timeout */
1113 FULLCHECK("timeout");
1117 goto invalid_option;
1118 result = parse_uint(&timeout, value, MAXTIMEOUT,
1120 if (result != ISC_R_SUCCESS)
1121 fatal("Couldn't parse timeout");
1126 case 'o': /* topdown */
1127 FULLCHECK("topdown");
1128 lookup->do_topdown = state;
1133 case 'a': /* trace */
1135 lookup->trace = state;
1136 lookup->trace_root = state;
1138 lookup->recurse = ISC_FALSE;
1139 lookup->identify = ISC_TRUE;
1140 lookup->comments = ISC_FALSE;
1141 rrcomments = ISC_FALSE;
1142 lookup->stats = ISC_FALSE;
1143 lookup->section_additional = ISC_FALSE;
1144 lookup->section_authority = ISC_TRUE;
1145 lookup->section_question = ISC_FALSE;
1146 lookup->dnssec = ISC_TRUE;
1147 usesearch = ISC_FALSE;
1150 case 'i': /* tries */
1155 goto invalid_option;
1156 result = parse_uint(&lookup->retries, value,
1158 if (result != ISC_R_SUCCESS)
1159 fatal("Couldn't parse tries");
1160 if (lookup->retries == 0)
1161 lookup->retries = 1;
1164 case 'u': /* trusted-key */
1165 FULLCHECK("trusted-key");
1169 goto invalid_option;
1170 n = strlcpy(trustedkey, ptr,
1171 sizeof(trustedkey));
1172 if (n >= sizeof(trustedkey))
1173 fatal("trusted key too large");
1177 goto invalid_option;
1180 case 't': /* ttlid */
1182 nottl = ISC_TF(!state);
1185 goto invalid_option;
1191 lookup->tcp_mode = state;
1196 fprintf(stderr, "Invalid option: +%s\n",
1204 * #ISC_TRUE returned if value was used
1206 static const char *single_dash_opts = "46dhimnv";
1207 static const char *dash_opts = "46bcdfhikmnptvyx";
1208 static isc_boolean_t
1209 dash_option(char *option, char *next, dig_lookup_t **lookup,
1210 isc_boolean_t *open_type_class, isc_boolean_t *need_clone,
1211 isc_boolean_t config_only, int argc, char **argv,
1212 isc_boolean_t *firstarg)
1214 char opt, *value, *ptr, *ptr2, *ptr3;
1215 isc_result_t result;
1216 isc_boolean_t value_from_next;
1217 isc_textregion_t tr;
1218 dns_rdatatype_t rdtype;
1219 dns_rdataclass_t rdclass;
1220 char textname[MXNAME];
1222 struct in6_addr in6;
1227 while (strpbrk(option, single_dash_opts) == &option[0]) {
1229 * Since the -[46dhimnv] options do not take an argument,
1230 * account for them (in any number and/or combination)
1231 * if they appear as the first character(s) of a q-opt.
1237 isc_net_disableipv6();
1238 have_ipv6 = ISC_FALSE;
1240 fatal("can't find IPv4 networking");
1247 isc_net_disableipv4();
1248 have_ipv4 = ISC_FALSE;
1250 fatal("can't find IPv6 networking");
1256 ptr = strpbrk(&option[1], dash_opts);
1257 if (ptr != &option[1]) {
1260 debugging = ISC_TRUE;
1263 debugging = ISC_TRUE;
1272 case 'm': /* memdebug */
1273 /* memdebug is handled in preparse_args() */
1283 if (strlen(option) > 1U)
1284 option = &option[1];
1289 if (strlen(option) > 1U) {
1290 value_from_next = ISC_FALSE;
1293 value_from_next = ISC_TRUE;
1297 goto invalid_option;
1300 hash = strchr(value, '#');
1302 result = parse_uint(&num, hash + 1, MAXPORT,
1304 if (result != ISC_R_SUCCESS)
1305 fatal("Couldn't parse port number");
1310 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1311 isc_sockaddr_fromin6(&bind_address, &in6, srcport);
1312 isc_net_disableipv4();
1313 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1314 isc_sockaddr_fromin(&bind_address, &in4, srcport);
1315 isc_net_disableipv6();
1319 fatal("invalid address %s", value);
1323 specified_source = ISC_TRUE;
1324 return (value_from_next);
1326 if ((*lookup)->rdclassset) {
1327 fprintf(stderr, ";; Warning, extra class option\n");
1329 *open_type_class = ISC_FALSE;
1331 tr.length = strlen(value);
1332 result = dns_rdataclass_fromtext(&rdclass,
1333 (isc_textregion_t *)&tr);
1334 if (result == ISC_R_SUCCESS) {
1335 (*lookup)->rdclass = rdclass;
1336 (*lookup)->rdclassset = ISC_TRUE;
1338 fprintf(stderr, ";; Warning, ignoring "
1339 "invalid class %s\n",
1341 return (value_from_next);
1344 return (value_from_next);
1346 strncpy(keyfile, value, sizeof(keyfile));
1347 keyfile[sizeof(keyfile)-1]=0;
1348 return (value_from_next);
1350 result = parse_uint(&num, value, MAXPORT, "port number");
1351 if (result != ISC_R_SUCCESS)
1352 fatal("Couldn't parse port number");
1354 return (value_from_next);
1358 (*lookup) = clone_lookup(default_lookup,
1360 *need_clone = ISC_TRUE;
1361 strncpy((*lookup)->textname, value,
1362 sizeof((*lookup)->textname));
1363 (*lookup)->textname[sizeof((*lookup)->textname)-1]=0;
1364 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1365 (*lookup)->ns_search_only);
1366 (*lookup)->new_search = ISC_TRUE;
1368 printgreeting(argc, argv, *lookup);
1369 *firstarg = ISC_FALSE;
1371 ISC_LIST_APPEND(lookup_list, (*lookup), link);
1372 debug("looking up %s", (*lookup)->textname);
1374 return (value_from_next);
1376 *open_type_class = ISC_FALSE;
1377 if (strncasecmp(value, "ixfr=", 5) == 0) {
1378 rdtype = dns_rdatatype_ixfr;
1379 result = ISC_R_SUCCESS;
1382 tr.length = strlen(value);
1383 result = dns_rdatatype_fromtext(&rdtype,
1384 (isc_textregion_t *)&tr);
1385 if (result == ISC_R_SUCCESS &&
1386 rdtype == dns_rdatatype_ixfr) {
1387 result = DNS_R_UNKNOWN;
1390 if (result == ISC_R_SUCCESS) {
1391 if ((*lookup)->rdtypeset) {
1392 fprintf(stderr, ";; Warning, "
1393 "extra type option\n");
1395 if (rdtype == dns_rdatatype_ixfr) {
1396 isc_uint32_t serial;
1397 (*lookup)->rdtype = dns_rdatatype_ixfr;
1398 (*lookup)->rdtypeset = ISC_TRUE;
1399 result = parse_uint(&serial, &value[5],
1400 MAXSERIAL, "serial number");
1401 if (result != ISC_R_SUCCESS)
1402 fatal("Couldn't parse serial number");
1403 (*lookup)->ixfr_serial = serial;
1404 (*lookup)->section_question = plusquest;
1405 (*lookup)->comments = pluscomm;
1406 (*lookup)->tcp_mode = ISC_TRUE;
1408 (*lookup)->rdtype = rdtype;
1409 (*lookup)->rdtypeset = ISC_TRUE;
1410 if (rdtype == dns_rdatatype_axfr) {
1411 (*lookup)->section_question = plusquest;
1412 (*lookup)->comments = pluscomm;
1414 (*lookup)->ixfr_serial = ISC_FALSE;
1417 fprintf(stderr, ";; Warning, ignoring "
1418 "invalid type %s\n",
1420 return (value_from_next);
1422 ptr = next_token(&value,":"); /* hmac type or name */
1426 ptr2 = next_token(&value, ":"); /* name or secret */
1429 ptr3 = next_token(&value,":"); /* secret or NULL */
1435 hmacname = DNS_TSIG_HMACMD5_NAME;
1438 strncpy(keynametext, ptr, sizeof(keynametext));
1439 keynametext[sizeof(keynametext)-1]=0;
1440 strncpy(keysecret, ptr2, sizeof(keysecret));
1441 keysecret[sizeof(keysecret)-1]=0;
1442 return (value_from_next);
1445 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1446 *need_clone = ISC_TRUE;
1447 if (get_reverse(textname, sizeof(textname), value,
1448 ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
1449 strncpy((*lookup)->textname, textname,
1450 sizeof((*lookup)->textname));
1451 debug("looking up %s", (*lookup)->textname);
1452 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1453 (*lookup)->ns_search_only);
1454 (*lookup)->ip6_int = ip6_int;
1455 if (!(*lookup)->rdtypeset)
1456 (*lookup)->rdtype = dns_rdatatype_ptr;
1457 if (!(*lookup)->rdclassset)
1458 (*lookup)->rdclass = dns_rdataclass_in;
1459 (*lookup)->new_search = ISC_TRUE;
1461 printgreeting(argc, argv, *lookup);
1462 *firstarg = ISC_FALSE;
1464 ISC_LIST_APPEND(lookup_list, *lookup, link);
1466 fprintf(stderr, "Invalid IP address %s\n", value);
1469 return (value_from_next);
1472 fprintf(stderr, "Invalid option: -%s\n", option);
1480 * Because we may be trying to do memory allocation recording, we're going
1481 * to need to parse the arguments for the -m *before* we start the main
1482 * argument parsing routine.
1484 * I'd prefer not to have to do this, but I am not quite sure how else to
1485 * fix the problem. Argument parsing in dig involves memory allocation
1486 * by its nature, so it can't be done in the main argument parser.
1489 preparse_args(int argc, char **argv) {
1496 for (rc--, rv++; rc > 0; rc--, rv++) {
1497 if (rv[0][0] != '-')
1500 while (strpbrk(option, single_dash_opts) == &option[0]) {
1501 if (option[0] == 'm') {
1502 memdebugging = ISC_TRUE;
1503 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1504 ISC_MEM_DEBUGRECORD;
1507 option = &option[1];
1513 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1514 int argc, char **argv) {
1515 isc_result_t result;
1516 isc_textregion_t tr;
1517 isc_boolean_t firstarg = ISC_TRUE;
1518 dig_lookup_t *lookup = NULL;
1519 dns_rdatatype_t rdtype;
1520 dns_rdataclass_t rdclass;
1521 isc_boolean_t open_type_class = ISC_TRUE;
1522 char batchline[MXNAME];
1533 isc_boolean_t need_clone = ISC_TRUE;
1536 * The semantics for parsing the args is a bit complex; if
1537 * we don't have a host yet, make the arg apply globally,
1538 * otherwise make it apply to the latest host. This is
1539 * a bit different than the previous versions, but should
1540 * form a consistent user interface.
1542 * First, create a "default lookup" which won't actually be used
1543 * anywhere, except for cloning into new lookups
1546 debug("parse_args()");
1547 if (!is_batchfile) {
1548 debug("making new lookup");
1549 default_lookup = make_empty_lookup();
1550 default_lookup->adflag = ISC_TRUE;
1551 default_lookup->edns = 0;
1555 * Treat ${HOME}/.digrc as a special batchfile
1557 INSIST(batchfp == NULL);
1558 homedir = getenv("HOME");
1559 if (homedir != NULL) {
1561 n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc",
1563 if (n < sizeof(rcfile))
1564 batchfp = fopen(rcfile, "r");
1566 if (batchfp != NULL) {
1567 while (fgets(batchline, sizeof(batchline),
1569 debug("config line %s", batchline);
1572 bargv[bargc] = next_token(&input, " \t\r\n");
1573 while ((bargv[bargc] != NULL) &&
1577 next_token(&input, " \t\r\n");
1583 for(i = 0; i < bargc; i++)
1584 debug(".digrc argv %d: %s",
1586 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1594 if (is_batchfile && !config_only) {
1595 /* Processing '-f batchfile'. */
1596 lookup = clone_lookup(default_lookup, ISC_TRUE);
1597 need_clone = ISC_FALSE;
1599 lookup = default_lookup;
1603 for (rc--, rv++; rc > 0; rc--, rv++) {
1604 debug("main parsing %s", rv[0]);
1605 if (strncmp(rv[0], "%", 1) == 0)
1607 if (strncmp(rv[0], "@", 1) == 0) {
1608 addresscount = getaddresses(lookup, &rv[0][1], NULL);
1609 } else if (rv[0][0] == '+') {
1610 plus_option(&rv[0][1], is_batchfile,
1612 } else if (rv[0][0] == '-') {
1614 if (dash_option(&rv[0][1], NULL,
1615 &lookup, &open_type_class,
1616 &need_clone, config_only,
1617 argc, argv, &firstarg)) {
1622 if (dash_option(&rv[0][1], rv[1],
1623 &lookup, &open_type_class,
1624 &need_clone, config_only,
1625 argc, argv, &firstarg)) {
1632 * Anything which isn't an option
1634 if (open_type_class) {
1635 if (strncasecmp(rv[0], "ixfr=", 5) == 0) {
1636 rdtype = dns_rdatatype_ixfr;
1637 result = ISC_R_SUCCESS;
1640 tr.length = strlen(rv[0]);
1641 result = dns_rdatatype_fromtext(&rdtype,
1642 (isc_textregion_t *)&tr);
1643 if (result == ISC_R_SUCCESS &&
1644 rdtype == dns_rdatatype_ixfr) {
1645 fprintf(stderr, ";; Warning, "
1651 if (result == ISC_R_SUCCESS) {
1652 if (lookup->rdtypeset) {
1653 fprintf(stderr, ";; Warning, "
1654 "extra type option\n");
1656 if (rdtype == dns_rdatatype_ixfr) {
1657 isc_uint32_t serial;
1660 lookup->rdtypeset = ISC_TRUE;
1661 result = parse_uint(&serial,
1665 if (result != ISC_R_SUCCESS)
1666 fatal("Couldn't parse "
1668 lookup->ixfr_serial = serial;
1669 lookup->section_question =
1671 lookup->comments = pluscomm;
1672 lookup->tcp_mode = ISC_TRUE;
1674 lookup->rdtype = rdtype;
1675 lookup->rdtypeset = ISC_TRUE;
1677 dns_rdatatype_axfr) {
1678 lookup->section_question =
1680 lookup->comments = pluscomm;
1682 lookup->ixfr_serial = ISC_FALSE;
1686 result = dns_rdataclass_fromtext(&rdclass,
1687 (isc_textregion_t *)&tr);
1688 if (result == ISC_R_SUCCESS) {
1689 if (lookup->rdclassset) {
1690 fprintf(stderr, ";; Warning, "
1691 "extra class option\n");
1693 lookup->rdclass = rdclass;
1694 lookup->rdclassset = ISC_TRUE;
1701 lookup = clone_lookup(default_lookup,
1703 need_clone = ISC_TRUE;
1704 strncpy(lookup->textname, rv[0],
1705 sizeof(lookup->textname));
1706 lookup->textname[sizeof(lookup->textname)-1]=0;
1707 lookup->trace_root = ISC_TF(lookup->trace ||
1708 lookup->ns_search_only);
1709 lookup->new_search = ISC_TRUE;
1711 printgreeting(argc, argv, lookup);
1712 firstarg = ISC_FALSE;
1714 ISC_LIST_APPEND(lookup_list, lookup, link);
1715 debug("looking up %s", lookup->textname);
1717 /* XXX Error message */
1722 * If we have a batchfile, seed the lookup list with the
1723 * first entry, then trust the callback in dighost_shutdown
1726 if ((batchname != NULL) && !(is_batchfile)) {
1727 if (strcmp(batchname, "-") == 0)
1730 batchfp = fopen(batchname, "r");
1731 if (batchfp == NULL) {
1735 fatal("couldn't open specified batch file");
1737 /* XXX Remove code dup from shutdown code */
1739 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1741 debug("batch line %s", batchline);
1742 if (batchline[0] == '\r' || batchline[0] == '\n'
1743 || batchline[0] == '#' || batchline[0] == ';')
1746 bargv[bargc] = next_token(&input, " \t\r\n");
1747 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1749 bargv[bargc] = next_token(&input, " \t\r\n");
1755 for(i = 0; i < bargc; i++)
1756 debug("batch argv %d: %s", i, bargv[i]);
1757 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1763 * If no lookup specified, search for root
1765 if ((lookup_list.head == NULL) && !config_only) {
1767 lookup = clone_lookup(default_lookup, ISC_TRUE);
1768 need_clone = ISC_TRUE;
1769 lookup->trace_root = ISC_TF(lookup->trace ||
1770 lookup->ns_search_only);
1771 lookup->new_search = ISC_TRUE;
1772 strcpy(lookup->textname, ".");
1773 lookup->rdtype = dns_rdatatype_ns;
1774 lookup->rdtypeset = ISC_TRUE;
1776 printgreeting(argc, argv, lookup);
1777 firstarg = ISC_FALSE;
1779 ISC_LIST_APPEND(lookup_list, lookup, link);
1782 destroy_lookup(lookup);
1786 * Callback from dighost.c to allow program-specific shutdown code.
1787 * Here, we're possibly reading from a batch file, then shutting down
1788 * for real if there's nothing in the batch file to read.
1791 dighost_shutdown(void) {
1792 char batchline[MXNAME];
1798 if (batchname == NULL) {
1804 if (feof(batchfp)) {
1807 if (batchfp != stdin)
1812 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1813 debug("batch line %s", batchline);
1816 bargv[bargc] = next_token(&input, " \t\r\n");
1817 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1819 bargv[bargc] = next_token(&input, " \t\r\n");
1824 for(i = 0; i < bargc; i++)
1825 debug("batch argv %d: %s", i, bargv[i]);
1826 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1830 if (batchfp != stdin)
1837 /*% Main processing routine for dig */
1839 main(int argc, char **argv) {
1840 isc_result_t result;
1842 ISC_LIST_INIT(lookup_list);
1843 ISC_LIST_INIT(server_list);
1844 ISC_LIST_INIT(search_list);
1847 preparse_args(argc, argv);
1849 result = isc_app_start();
1850 check_result(result, "isc_app_start");
1852 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1854 if (domainopt[0] != '\0') {
1855 set_search_domain(domainopt);
1856 usesearch = ISC_TRUE;
1858 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1859 check_result(result, "isc_app_onrun");
1861 destroy_lookup(default_lookup);
1862 if (batchname != NULL) {
1863 if (batchfp != stdin)