2 * Copyright (C) 2004-2011 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.237.124.4 2011/12/07 17:23:55 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,
73 static const char * const opcodetext[] = {
92 /*% return code text */
93 static const char * const rcodetext[] = {
113 /*% safe rcodetext[] */
115 rcode_totext(dns_rcode_t rcode)
117 static char buf[sizeof("?65535")];
119 const char *consttext;
123 if (rcode >= (sizeof(rcodetext)/sizeof(rcodetext[0]))) {
124 snprintf(buf, sizeof(buf), "?%u", rcode);
125 totext.deconsttext = buf;
127 totext.consttext = rcodetext[rcode];
128 return totext.deconsttext;
133 print_usage(FILE *fp) {
135 "Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}\n"
136 " {global-d-opt} host [@local-server] {local-d-opt}\n"
137 " [ host [@local-server] {local-d-opt} [...]]\n", fp);
140 ISC_PLATFORM_NORETURN_PRE static void
141 usage(void) ISC_PLATFORM_NORETURN_POST;
146 fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
147 "for complete list of options\n", stderr);
154 fputs("DiG " VERSION "\n", stderr);
162 "Where: domain is in the Domain Name System\n"
163 " q-class is one of (in,hs,ch,...) [default: in]\n"
164 " q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
165 " (Use ixfr=version for type ixfr)\n"
166 " q-opt is one of:\n"
167 " -x dot-notation (shortcut for reverse lookups)\n"
168 " -i (use IP6.INT for IPv6 reverse lookups)\n"
169 " -f filename (batch mode)\n"
170 " -b address[#port] (bind to source address/port)\n"
171 " -p port (specify port number)\n"
172 " -q name (specify query name)\n"
173 " -t type (specify query type)\n"
174 " -c class (specify query class)\n"
175 " -k keyfile (specify tsig key file)\n"
176 " -y [hmac:]name:key (specify named base64 tsig key)\n"
177 " -4 (use IPv4 query transport only)\n"
178 " -6 (use IPv6 query transport only)\n"
179 " -m (enable memory usage debugging)\n"
180 " d-opt is of the form +keyword[=value], where keyword is:\n"
181 " +[no]vc (TCP mode)\n"
182 " +[no]tcp (TCP mode, alternate syntax)\n"
183 " +time=### (Set query timeout) [5]\n"
184 " +tries=### (Set number of UDP attempts) [3]\n"
185 " +retry=### (Set number of UDP retries) [2]\n"
186 " +domain=### (Set default domainname)\n"
187 " +bufsize=### (Set EDNS0 Max UDP packet size)\n"
188 " +ndots=### (Set NDOTS value)\n"
189 " +edns=### (Set EDNS version)\n"
190 " +[no]search (Set whether to use searchlist)\n"
191 " +[no]showsearch (Search with intermediate results)\n"
192 " +[no]defname (Ditto)\n"
193 " +[no]recurse (Recursive mode)\n"
194 " +[no]ignore (Don't revert to TCP for TC responses.)"
196 " +[no]fail (Don't try next server on SERVFAIL)\n"
197 " +[no]besteffort (Try to parse even illegal messages)\n"
198 " +[no]aaonly (Set AA flag in query (+[no]aaflag))\n"
199 " +[no]adflag (Set AD flag in query)\n"
200 " +[no]cdflag (Set CD flag in query)\n"
201 " +[no]cl (Control display of class in records)\n"
202 " +[no]cmd (Control display of command line)\n"
203 " +[no]comments (Control display of comment lines)\n"
204 " +[no]question (Control display of question)\n"
205 " +[no]answer (Control display of answer)\n"
206 " +[no]authority (Control display of authority)\n"
207 " +[no]additional (Control display of additional)\n"
208 " +[no]stats (Control display of statistics)\n"
209 " +[no]short (Disable everything except short\n"
211 " +[no]ttlid (Control display of ttls in records)\n"
212 " +[no]all (Set or clear all display flags)\n"
213 " +[no]qr (Print question before sending)\n"
214 " +[no]nssearch (Search all authoritative nameservers)\n"
215 " +[no]identify (ID responders in short answers)\n"
216 " +[no]trace (Trace delegation down from root)\n"
217 " +[no]dnssec (Request DNSSEC records)\n"
218 " +[no]nsid (Request Name Server ID)\n"
220 " +[no]sigchase (Chase DNSSEC signatures)\n"
221 " +trusted-key=#### (Trusted Key when chasing DNSSEC sigs)\n"
223 " +[no]topdown (Do DNSSEC validation top down mode)\n"
226 " +[no]multiline (Print records in an expanded format)\n"
227 " +[no]onesoa (AXFR prints only one soa record)\n"
228 " global d-opts and servers (before host name) affect all queries.\n"
229 " local d-opts and servers (after host name) affect only that lookup.\n"
230 " -h (print help and exit)\n"
231 " -v (print version and exit)\n",
236 * Callback from dighost.c to print the received message.
239 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
243 char fromtext[ISC_SOCKADDR_FORMATSIZE];
245 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
249 if (query->lookup->stats && !short_form) {
250 diff = isc_time_microdiff(&now, &query->time_sent);
251 printf(";; Query time: %ld msec\n", (long int)diff/1000);
252 printf(";; SERVER: %s(%s)\n", fromtext, query->servname);
254 printf(";; WHEN: %s", ctime(&tnow));
255 if (query->lookup->doing_xfr) {
256 printf(";; XFR size: %u records (messages %u, "
257 "bytes %" ISC_PRINT_QUADFORMAT "u)\n",
258 query->rr_count, query->msg_count,
261 printf(";; MSG SIZE rcvd: %u\n", bytes);
266 puts(";; WARNING -- Some TSIG could not "
269 if ((key == NULL) && (keysecret[0] != 0)) {
270 puts(";; WARNING -- TSIG key was not used.");
273 } else if (query->lookup->identify && !short_form) {
274 diff = isc_time_microdiff(&now, &query->time_sent);
275 printf(";; Received %" ISC_PRINT_QUADFORMAT "u bytes "
276 "from %s(%s) in %d ms\n\n",
277 query->lookup->doing_xfr ?
278 query->byte_count : (isc_uint64_t)bytes,
279 fromtext, query->servname,
285 * Callback from dighost.c to print that it is trying a server.
290 trying(char *frm, dig_lookup_t *lookup) {
296 * Internal print routine used to print short form replies.
299 say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
303 char store[sizeof("12345678901234567890")];
305 if (query->lookup->trace || query->lookup->ns_search_only) {
306 result = dns_rdatatype_totext(rdata->type, buf);
307 if (result != ISC_R_SUCCESS)
309 ADD_STRING(buf, " ");
311 result = dns_rdata_totext(rdata, NULL, buf);
312 if (result == ISC_R_NOSPACE)
314 check_result(result, "dns_rdata_totext");
315 if (query->lookup->identify) {
317 diff = isc_time_microdiff(&now, &query->time_sent);
318 ADD_STRING(buf, " from server ");
319 ADD_STRING(buf, query->servname);
320 snprintf(store, 19, " in %d ms.", (int)diff/1000);
321 ADD_STRING(buf, store);
323 ADD_STRING(buf, "\n");
324 return (ISC_R_SUCCESS);
328 * short_form message print handler. Calls above say_message()
331 short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
332 isc_buffer_t *buf, dig_query_t *query)
335 dns_rdataset_t *rdataset;
336 isc_result_t result, loopresult;
337 dns_name_t empty_name;
338 dns_rdata_t rdata = DNS_RDATA_INIT;
342 dns_name_init(&empty_name, NULL);
343 result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
344 if (result == ISC_R_NOMORE)
345 return (ISC_R_SUCCESS);
346 else if (result != ISC_R_SUCCESS)
351 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
353 for (rdataset = ISC_LIST_HEAD(name->list);
355 rdataset = ISC_LIST_NEXT(rdataset, link)) {
356 loopresult = dns_rdataset_first(rdataset);
357 while (loopresult == ISC_R_SUCCESS) {
358 dns_rdataset_current(rdataset, &rdata);
359 result = say_message(&rdata, query,
361 if (result == ISC_R_NOSPACE)
363 check_result(result, "say_message");
364 loopresult = dns_rdataset_next(rdataset);
365 dns_rdata_reset(&rdata);
368 result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
369 if (result == ISC_R_NOMORE)
371 else if (result != ISC_R_SUCCESS)
375 return (ISC_R_SUCCESS);
379 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
380 isc_buffer_t *target)
383 dns_master_style_t *style = NULL;
384 unsigned int styleflags = 0;
386 if (rdataset == NULL || owner_name == NULL || target == NULL)
389 styleflags |= DNS_STYLEFLAG_REL_OWNER;
391 styleflags |= DNS_STYLEFLAG_NO_TTL;
393 styleflags |= DNS_STYLEFLAG_NO_CLASS;
395 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
396 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
397 styleflags |= DNS_STYLEFLAG_REL_DATA;
398 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
399 styleflags |= DNS_STYLEFLAG_TTL;
400 styleflags |= DNS_STYLEFLAG_MULTILINE;
401 styleflags |= DNS_STYLEFLAG_COMMENT;
403 if (multiline || (nottl && noclass))
404 result = dns_master_stylecreate(&style, styleflags,
405 24, 24, 24, 32, 80, 8, mctx);
406 else if (nottl || noclass)
407 result = dns_master_stylecreate(&style, styleflags,
408 24, 24, 32, 40, 80, 8, mctx);
410 result = dns_master_stylecreate(&style, styleflags,
411 24, 32, 40, 48, 80, 8, mctx);
412 check_result(result, "dns_master_stylecreate");
414 result = dns_master_rdatasettotext(owner_name, rdataset, style, target);
417 dns_master_styledestroy(&style, mctx);
424 * Callback from dighost.c to print the reply from a server
427 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
429 dns_messagetextflag_t flags;
430 isc_buffer_t *buf = NULL;
431 unsigned int len = OUTPUTBUF;
432 dns_master_style_t *style = NULL;
433 unsigned int styleflags = 0;
435 styleflags |= DNS_STYLEFLAG_REL_OWNER;
437 styleflags |= DNS_STYLEFLAG_NO_TTL;
439 styleflags |= DNS_STYLEFLAG_NO_CLASS;
441 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
442 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
443 styleflags |= DNS_STYLEFLAG_REL_DATA;
444 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
445 styleflags |= DNS_STYLEFLAG_TTL;
446 styleflags |= DNS_STYLEFLAG_MULTILINE;
447 styleflags |= DNS_STYLEFLAG_COMMENT;
449 if (multiline || (nottl && noclass))
450 result = dns_master_stylecreate(&style, styleflags,
451 24, 24, 24, 32, 80, 8, mctx);
452 else if (nottl || noclass)
453 result = dns_master_stylecreate(&style, styleflags,
454 24, 24, 32, 40, 80, 8, mctx);
456 result = dns_master_stylecreate(&style, styleflags,
457 24, 32, 40, 48, 80, 8, mctx);
458 check_result(result, "dns_master_stylecreate");
460 if (query->lookup->cmdline[0] != 0) {
462 fputs(query->lookup->cmdline, stdout);
463 query->lookup->cmdline[0]=0;
465 debug("printmessage(%s %s %s)", headers ? "headers" : "noheaders",
466 query->lookup->comments ? "comments" : "nocomments",
467 short_form ? "short_form" : "long_form");
471 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
472 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
474 if (onesoa && query->lookup->rdtype == dns_rdatatype_axfr)
475 flags |= (query->msg_count == 0) ? DNS_MESSAGETEXTFLAG_ONESOA :
476 DNS_MESSAGETEXTFLAG_OMITSOA;
477 if (!query->lookup->comments)
478 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
480 result = isc_buffer_allocate(mctx, &buf, len);
481 check_result(result, "isc_buffer_allocate");
483 if (query->lookup->comments && !short_form) {
484 if (query->lookup->cmdline[0] != 0)
485 printf("; %s\n", query->lookup->cmdline);
486 if (msg == query->lookup->sendmsg)
487 printf(";; Sending:\n");
489 printf(";; Got answer:\n");
492 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
494 opcodetext[msg->opcode],
495 rcode_totext(msg->rcode),
498 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
500 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
502 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
504 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
506 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
508 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
510 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
512 if ((msg->flags & 0x0040U) != 0)
513 printf("; MBZ: 0x4");
515 printf("; QUERY: %u, ANSWER: %u, "
516 "AUTHORITY: %u, ADDITIONAL: %u\n",
517 msg->counts[DNS_SECTION_QUESTION],
518 msg->counts[DNS_SECTION_ANSWER],
519 msg->counts[DNS_SECTION_AUTHORITY],
520 msg->counts[DNS_SECTION_ADDITIONAL]);
522 if (msg != query->lookup->sendmsg &&
523 (msg->flags & DNS_MESSAGEFLAG_RD) != 0 &&
524 (msg->flags & DNS_MESSAGEFLAG_RA) == 0)
525 printf(";; WARNING: recursion requested "
526 "but not available\n");
528 if (msg != query->lookup->sendmsg && extrabytes != 0U)
529 printf(";; WARNING: Messages has %u extra byte%s at "
530 "end\n", extrabytes, extrabytes != 0 ? "s" : "");
535 if (query->lookup->comments && headers && !short_form) {
536 result = dns_message_pseudosectiontotext(msg,
537 DNS_PSEUDOSECTION_OPT,
539 if (result == ISC_R_NOSPACE) {
542 isc_buffer_free(&buf);
543 result = isc_buffer_allocate(mctx, &buf, len);
544 if (result == ISC_R_SUCCESS)
545 goto repopulate_buffer;
550 "dns_message_pseudosectiontotext");
553 if (query->lookup->section_question && headers) {
555 result = dns_message_sectiontotext(msg,
556 DNS_SECTION_QUESTION,
558 if (result == ISC_R_NOSPACE)
560 check_result(result, "dns_message_sectiontotext");
563 if (query->lookup->section_answer) {
565 result = dns_message_sectiontotext(msg,
568 if (result == ISC_R_NOSPACE)
570 check_result(result, "dns_message_sectiontotext");
572 result = short_answer(msg, flags, buf, query);
573 if (result == ISC_R_NOSPACE)
575 check_result(result, "short_answer");
578 if (query->lookup->section_authority) {
580 result = dns_message_sectiontotext(msg,
581 DNS_SECTION_AUTHORITY,
583 if (result == ISC_R_NOSPACE)
585 check_result(result, "dns_message_sectiontotext");
588 if (query->lookup->section_additional) {
590 result = dns_message_sectiontotext(msg,
591 DNS_SECTION_ADDITIONAL,
593 if (result == ISC_R_NOSPACE)
595 check_result(result, "dns_message_sectiontotext");
597 * Only print the signature on the first record.
600 result = dns_message_pseudosectiontotext(
602 DNS_PSEUDOSECTION_TSIG,
604 if (result == ISC_R_NOSPACE)
607 "dns_message_pseudosectiontotext");
608 result = dns_message_pseudosectiontotext(
610 DNS_PSEUDOSECTION_SIG0,
612 if (result == ISC_R_NOSPACE)
615 "dns_message_pseudosectiontotext");
620 if (headers && query->lookup->comments && !short_form)
623 printf("%.*s", (int)isc_buffer_usedlength(buf),
624 (char *)isc_buffer_base(buf));
625 isc_buffer_free(&buf);
629 dns_master_styledestroy(&style, mctx);
634 * print the greeting message when the program first starts up.
637 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
640 static isc_boolean_t first = ISC_TRUE;
644 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
645 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
646 "%s; <<>> DiG " VERSION " <<>>",
650 snprintf(append, sizeof(append), " %s", argv[i++]);
651 remaining = sizeof(lookup->cmdline) -
652 strlen(lookup->cmdline) - 1;
653 strncat(lookup->cmdline, append, remaining);
655 remaining = sizeof(lookup->cmdline) -
656 strlen(lookup->cmdline) - 1;
657 strncat(lookup->cmdline, "\n", remaining);
658 if (first && addresscount != 0) {
659 snprintf(append, sizeof(append),
660 "; (%d server%s found)\n",
662 addresscount > 1 ? "s" : "");
663 remaining = sizeof(lookup->cmdline) -
664 strlen(lookup->cmdline) - 1;
665 strncat(lookup->cmdline, append, remaining);
668 snprintf(append, sizeof(append),
669 ";; global options:%s%s\n",
670 short_form ? " +short" : "",
671 printcmd ? " +cmd" : "");
673 remaining = sizeof(lookup->cmdline) -
674 strlen(lookup->cmdline) - 1;
675 strncat(lookup->cmdline, append, remaining);
681 * We're not using isc_commandline_parse() here since the command line
682 * syntax of dig is quite a bit different from that which can be described
688 plus_option(char *option, isc_boolean_t is_batchfile,
689 dig_lookup_t *lookup)
692 char option_store[256];
693 char *cmd, *value, *ptr;
695 isc_boolean_t state = ISC_TRUE;
700 strncpy(option_store, option, sizeof(option_store));
701 option_store[sizeof(option_store)-1]=0;
703 cmd = next_token(&ptr,"=");
705 printf(";; Invalid option %s\n", option_store);
709 if (strncasecmp(cmd, "no", 2)==0) {
714 #define FULLCHECK(A) \
716 size_t _l = strlen(cmd); \
717 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
718 goto invalid_option; \
720 #define FULLCHECK2(A, B) \
722 size_t _l = strlen(cmd); \
723 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
724 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
725 goto invalid_option; \
731 case 'a': /* aaonly / aaflag */
732 FULLCHECK2("aaonly", "aaflag");
733 lookup->aaonly = state;
737 case 'd': /* additional */
738 FULLCHECK("additional");
739 lookup->section_additional = state;
741 case 'f': /* adflag */
742 case '\0': /* +ad is a synonym for +adflag */
744 lookup->adflag = state;
752 lookup->section_question = state;
753 lookup->section_authority = state;
754 lookup->section_answer = state;
755 lookup->section_additional = state;
756 lookup->comments = state;
757 lookup->stats = state;
760 case 'n': /* answer */
762 lookup->section_answer = state;
764 case 'u': /* authority */
765 FULLCHECK("authority");
766 lookup->section_authority = state;
774 case 'e':/* besteffort */
775 FULLCHECK("besteffort");
776 lookup->besteffort = state;
778 case 'u':/* bufsize */
779 FULLCHECK("bufsize");
784 result = parse_uint(&num, value, COMMSIZE,
786 if (result != ISC_R_SUCCESS)
787 fatal("Couldn't parse buffer size");
788 lookup->udpsize = num;
796 case 'd':/* cdflag */
798 case 'f': /* cdflag */
799 case '\0': /* +cd is a synonym for +cdflag */
801 lookup->cdflag = state;
809 noclass = ISC_TF(!state);
815 case 'o': /* comments */
816 FULLCHECK("comments");
817 lookup->comments = state;
818 if (lookup == default_lookup)
827 case 'e': /* defname */
828 FULLCHECK("defname");
829 if (!lookup->trace) {
833 case 'n': /* dnssec */
835 if (state && lookup->edns == -1)
837 lookup->dnssec = state;
839 case 'o': /* domain */
845 strncpy(domainopt, value, sizeof(domainopt));
846 domainopt[sizeof(domainopt)-1] = '\0';
860 result = parse_uint(&num, value, 255, "edns");
861 if (result != ISC_R_SUCCESS)
862 fatal("Couldn't parse edns");
867 lookup->servfail_stops = state;
871 case 'd': /* identify */
872 FULLCHECK("identify");
873 lookup->identify = state;
875 case 'g': /* ignore */
876 default: /* Inherits default for compatibility */
878 lookup->ignore = ISC_TRUE;
881 case 'm': /* multiline */
882 FULLCHECK("multiline");
887 case 'd': /* ndots */
893 result = parse_uint(&num, value, MAXNDOTS, "ndots");
894 if (result != ISC_R_SUCCESS)
895 fatal("Couldn't parse ndots");
902 if (state && lookup->edns == -1)
904 lookup->nsid = state;
906 case 's': /* nssearch */
907 FULLCHECK("nssearch");
908 lookup->ns_search_only = state;
910 lookup->trace_root = ISC_TRUE;
911 lookup->recurse = ISC_TRUE;
912 lookup->identify = ISC_TRUE;
913 lookup->stats = ISC_FALSE;
914 lookup->comments = ISC_FALSE;
915 lookup->section_additional = ISC_FALSE;
916 lookup->section_authority = ISC_FALSE;
917 lookup->section_question = ISC_FALSE;
918 lookup->rdtype = dns_rdatatype_ns;
919 lookup->rdtypeset = ISC_TRUE;
920 short_form = ISC_TRUE;
941 case 'u': /* question */
942 FULLCHECK("question");
943 lookup->section_question = state;
944 if (lookup == default_lookup)
955 case 'c': /* recurse */
956 FULLCHECK("recurse");
957 lookup->recurse = state;
959 case 't': /* retry / retries */
960 FULLCHECK2("retry", "retries");
965 result = parse_uint(&lookup->retries, value,
966 MAXTRIES - 1, "retries");
967 if (result != ISC_R_SUCCESS)
968 fatal("Couldn't parse retries");
981 case 'e': /* search */
983 if (!lookup->trace) {
991 case 'r': /* short */
995 printcmd = ISC_FALSE;
996 lookup->section_additional = ISC_FALSE;
997 lookup->section_answer = ISC_TRUE;
998 lookup->section_authority = ISC_FALSE;
999 lookup->section_question = ISC_FALSE;
1000 lookup->comments = ISC_FALSE;
1001 lookup->stats = ISC_FALSE;
1004 case 'w': /* showsearch */
1005 FULLCHECK("showsearch");
1006 if (!lookup->trace) {
1012 goto invalid_option;
1016 case 'i': /* sigchase */
1017 FULLCHECK("sigchase");
1018 lookup->sigchase = state;
1019 if (lookup->sigchase)
1020 lookup->dnssec = ISC_TRUE;
1023 case 't': /* stats */
1025 lookup->stats = state;
1028 goto invalid_option;
1036 lookup->tcp_mode = state;
1038 case 'i': /* timeout */
1039 FULLCHECK("timeout");
1043 goto invalid_option;
1044 result = parse_uint(&timeout, value, MAXTIMEOUT,
1046 if (result != ISC_R_SUCCESS)
1047 fatal("Couldn't parse timeout");
1052 case 'o': /* topdown */
1053 FULLCHECK("topdown");
1054 lookup->do_topdown = state;
1059 case 'a': /* trace */
1061 lookup->trace = state;
1062 lookup->trace_root = state;
1064 lookup->recurse = ISC_FALSE;
1065 lookup->identify = ISC_TRUE;
1066 lookup->comments = ISC_FALSE;
1067 lookup->stats = ISC_FALSE;
1068 lookup->section_additional = ISC_FALSE;
1069 lookup->section_authority = ISC_TRUE;
1070 lookup->section_question = ISC_FALSE;
1071 usesearch = ISC_FALSE;
1074 case 'i': /* tries */
1079 goto invalid_option;
1080 result = parse_uint(&lookup->retries, value,
1082 if (result != ISC_R_SUCCESS)
1083 fatal("Couldn't parse tries");
1084 if (lookup->retries == 0)
1085 lookup->retries = 1;
1088 case 'u': /* trusted-key */
1089 FULLCHECK("trusted-key");
1093 goto invalid_option;
1094 n = strlcpy(trustedkey, ptr,
1095 sizeof(trustedkey));
1096 if (n >= sizeof(trustedkey))
1097 fatal("trusted key too large");
1101 goto invalid_option;
1104 case 't': /* ttlid */
1106 nottl = ISC_TF(!state);
1109 goto invalid_option;
1115 lookup->tcp_mode = state;
1120 fprintf(stderr, "Invalid option: +%s\n",
1128 * #ISC_TRUE returned if value was used
1130 static const char *single_dash_opts = "46dhimnv";
1131 static const char *dash_opts = "46bcdfhikmnptvyx";
1132 static isc_boolean_t
1133 dash_option(char *option, char *next, dig_lookup_t **lookup,
1134 isc_boolean_t *open_type_class, isc_boolean_t *need_clone,
1135 isc_boolean_t config_only, int argc, char **argv,
1136 isc_boolean_t *firstarg)
1138 char opt, *value, *ptr, *ptr2, *ptr3;
1139 isc_result_t result;
1140 isc_boolean_t value_from_next;
1141 isc_textregion_t tr;
1142 dns_rdatatype_t rdtype;
1143 dns_rdataclass_t rdclass;
1144 char textname[MXNAME];
1146 struct in6_addr in6;
1151 while (strpbrk(option, single_dash_opts) == &option[0]) {
1153 * Since the -[46dhimnv] options do not take an argument,
1154 * account for them (in any number and/or combination)
1155 * if they appear as the first character(s) of a q-opt.
1161 isc_net_disableipv6();
1162 have_ipv6 = ISC_FALSE;
1164 fatal("can't find IPv4 networking");
1171 isc_net_disableipv4();
1172 have_ipv4 = ISC_FALSE;
1174 fatal("can't find IPv6 networking");
1180 ptr = strpbrk(&option[1], dash_opts);
1181 if (ptr != &option[1]) {
1184 debugging = ISC_TRUE;
1187 debugging = ISC_TRUE;
1196 case 'm': /* memdebug */
1197 /* memdebug is handled in preparse_args() */
1207 if (strlen(option) > 1U)
1208 option = &option[1];
1213 if (strlen(option) > 1U) {
1214 value_from_next = ISC_FALSE;
1217 value_from_next = ISC_TRUE;
1221 goto invalid_option;
1224 hash = strchr(value, '#');
1226 result = parse_uint(&num, hash + 1, MAXPORT,
1228 if (result != ISC_R_SUCCESS)
1229 fatal("Couldn't parse port number");
1234 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1235 isc_sockaddr_fromin6(&bind_address, &in6, srcport);
1236 isc_net_disableipv4();
1237 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1238 isc_sockaddr_fromin(&bind_address, &in4, srcport);
1239 isc_net_disableipv6();
1243 fatal("invalid address %s", value);
1247 specified_source = ISC_TRUE;
1248 return (value_from_next);
1250 if ((*lookup)->rdclassset) {
1251 fprintf(stderr, ";; Warning, extra class option\n");
1253 *open_type_class = ISC_FALSE;
1255 tr.length = strlen(value);
1256 result = dns_rdataclass_fromtext(&rdclass,
1257 (isc_textregion_t *)&tr);
1258 if (result == ISC_R_SUCCESS) {
1259 (*lookup)->rdclass = rdclass;
1260 (*lookup)->rdclassset = ISC_TRUE;
1262 fprintf(stderr, ";; Warning, ignoring "
1263 "invalid class %s\n",
1265 return (value_from_next);
1268 return (value_from_next);
1270 strncpy(keyfile, value, sizeof(keyfile));
1271 keyfile[sizeof(keyfile)-1]=0;
1272 return (value_from_next);
1274 result = parse_uint(&num, value, MAXPORT, "port number");
1275 if (result != ISC_R_SUCCESS)
1276 fatal("Couldn't parse port number");
1278 return (value_from_next);
1282 (*lookup) = clone_lookup(default_lookup,
1284 *need_clone = ISC_TRUE;
1285 strncpy((*lookup)->textname, value,
1286 sizeof((*lookup)->textname));
1287 (*lookup)->textname[sizeof((*lookup)->textname)-1]=0;
1288 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1289 (*lookup)->ns_search_only);
1290 (*lookup)->new_search = ISC_TRUE;
1292 printgreeting(argc, argv, *lookup);
1293 *firstarg = ISC_FALSE;
1295 ISC_LIST_APPEND(lookup_list, (*lookup), link);
1296 debug("looking up %s", (*lookup)->textname);
1298 return (value_from_next);
1300 *open_type_class = ISC_FALSE;
1301 if (strncasecmp(value, "ixfr=", 5) == 0) {
1302 rdtype = dns_rdatatype_ixfr;
1303 result = ISC_R_SUCCESS;
1306 tr.length = strlen(value);
1307 result = dns_rdatatype_fromtext(&rdtype,
1308 (isc_textregion_t *)&tr);
1309 if (result == ISC_R_SUCCESS &&
1310 rdtype == dns_rdatatype_ixfr) {
1311 result = DNS_R_UNKNOWN;
1314 if (result == ISC_R_SUCCESS) {
1315 if ((*lookup)->rdtypeset) {
1316 fprintf(stderr, ";; Warning, "
1317 "extra type option\n");
1319 if (rdtype == dns_rdatatype_ixfr) {
1320 isc_uint32_t serial;
1321 (*lookup)->rdtype = dns_rdatatype_ixfr;
1322 (*lookup)->rdtypeset = ISC_TRUE;
1323 result = parse_uint(&serial, &value[5],
1324 MAXSERIAL, "serial number");
1325 if (result != ISC_R_SUCCESS)
1326 fatal("Couldn't parse serial number");
1327 (*lookup)->ixfr_serial = serial;
1328 (*lookup)->section_question = plusquest;
1329 (*lookup)->comments = pluscomm;
1330 (*lookup)->tcp_mode = ISC_TRUE;
1332 (*lookup)->rdtype = rdtype;
1333 (*lookup)->rdtypeset = ISC_TRUE;
1334 if (rdtype == dns_rdatatype_axfr) {
1335 (*lookup)->section_question = plusquest;
1336 (*lookup)->comments = pluscomm;
1338 (*lookup)->ixfr_serial = ISC_FALSE;
1341 fprintf(stderr, ";; Warning, ignoring "
1342 "invalid type %s\n",
1344 return (value_from_next);
1346 ptr = next_token(&value,":"); /* hmac type or name */
1350 ptr2 = next_token(&value, ":"); /* name or secret */
1353 ptr3 = next_token(&value,":"); /* secret or NULL */
1359 hmacname = DNS_TSIG_HMACMD5_NAME;
1362 strncpy(keynametext, ptr, sizeof(keynametext));
1363 keynametext[sizeof(keynametext)-1]=0;
1364 strncpy(keysecret, ptr2, sizeof(keysecret));
1365 keysecret[sizeof(keysecret)-1]=0;
1366 return (value_from_next);
1369 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1370 *need_clone = ISC_TRUE;
1371 if (get_reverse(textname, sizeof(textname), value,
1372 ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
1373 strncpy((*lookup)->textname, textname,
1374 sizeof((*lookup)->textname));
1375 debug("looking up %s", (*lookup)->textname);
1376 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1377 (*lookup)->ns_search_only);
1378 (*lookup)->ip6_int = ip6_int;
1379 if (!(*lookup)->rdtypeset)
1380 (*lookup)->rdtype = dns_rdatatype_ptr;
1381 if (!(*lookup)->rdclassset)
1382 (*lookup)->rdclass = dns_rdataclass_in;
1383 (*lookup)->new_search = ISC_TRUE;
1385 printgreeting(argc, argv, *lookup);
1386 *firstarg = ISC_FALSE;
1388 ISC_LIST_APPEND(lookup_list, *lookup, link);
1390 fprintf(stderr, "Invalid IP address %s\n", value);
1393 return (value_from_next);
1396 fprintf(stderr, "Invalid option: -%s\n", option);
1404 * Because we may be trying to do memory allocation recording, we're going
1405 * to need to parse the arguments for the -m *before* we start the main
1406 * argument parsing routine.
1408 * I'd prefer not to have to do this, but I am not quite sure how else to
1409 * fix the problem. Argument parsing in dig involves memory allocation
1410 * by its nature, so it can't be done in the main argument parser.
1413 preparse_args(int argc, char **argv) {
1420 for (rc--, rv++; rc > 0; rc--, rv++) {
1421 if (rv[0][0] != '-')
1424 while (strpbrk(option, single_dash_opts) == &option[0]) {
1425 if (option[0] == 'm') {
1426 memdebugging = ISC_TRUE;
1427 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1428 ISC_MEM_DEBUGRECORD;
1431 option = &option[1];
1437 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1438 int argc, char **argv) {
1439 isc_result_t result;
1440 isc_textregion_t tr;
1441 isc_boolean_t firstarg = ISC_TRUE;
1442 dig_lookup_t *lookup = NULL;
1443 dns_rdatatype_t rdtype;
1444 dns_rdataclass_t rdclass;
1445 isc_boolean_t open_type_class = ISC_TRUE;
1446 char batchline[MXNAME];
1457 isc_boolean_t need_clone = ISC_TRUE;
1460 * The semantics for parsing the args is a bit complex; if
1461 * we don't have a host yet, make the arg apply globally,
1462 * otherwise make it apply to the latest host. This is
1463 * a bit different than the previous versions, but should
1464 * form a consistent user interface.
1466 * First, create a "default lookup" which won't actually be used
1467 * anywhere, except for cloning into new lookups
1470 debug("parse_args()");
1471 if (!is_batchfile) {
1472 debug("making new lookup");
1473 default_lookup = make_empty_lookup();
1477 * Treat ${HOME}/.digrc as a special batchfile
1479 INSIST(batchfp == NULL);
1480 homedir = getenv("HOME");
1481 if (homedir != NULL) {
1483 n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc",
1485 if (n < sizeof(rcfile))
1486 batchfp = fopen(rcfile, "r");
1488 if (batchfp != NULL) {
1489 while (fgets(batchline, sizeof(batchline),
1491 debug("config line %s", batchline);
1494 bargv[bargc] = next_token(&input, " \t\r\n");
1495 while ((bargv[bargc] != NULL) &&
1499 next_token(&input, " \t\r\n");
1505 for(i = 0; i < bargc; i++)
1506 debug(".digrc argv %d: %s",
1508 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1516 if (is_batchfile && !config_only) {
1517 /* Processing '-f batchfile'. */
1518 lookup = clone_lookup(default_lookup, ISC_TRUE);
1519 need_clone = ISC_FALSE;
1521 lookup = default_lookup;
1525 for (rc--, rv++; rc > 0; rc--, rv++) {
1526 debug("main parsing %s", rv[0]);
1527 if (strncmp(rv[0], "%", 1) == 0)
1529 if (strncmp(rv[0], "@", 1) == 0) {
1530 addresscount = getaddresses(lookup, &rv[0][1], NULL);
1531 } else if (rv[0][0] == '+') {
1532 plus_option(&rv[0][1], is_batchfile,
1534 } else if (rv[0][0] == '-') {
1536 if (dash_option(&rv[0][1], NULL,
1537 &lookup, &open_type_class,
1538 &need_clone, config_only,
1539 argc, argv, &firstarg)) {
1544 if (dash_option(&rv[0][1], rv[1],
1545 &lookup, &open_type_class,
1546 &need_clone, config_only,
1547 argc, argv, &firstarg)) {
1554 * Anything which isn't an option
1556 if (open_type_class) {
1557 if (strncasecmp(rv[0], "ixfr=", 5) == 0) {
1558 rdtype = dns_rdatatype_ixfr;
1559 result = ISC_R_SUCCESS;
1562 tr.length = strlen(rv[0]);
1563 result = dns_rdatatype_fromtext(&rdtype,
1564 (isc_textregion_t *)&tr);
1565 if (result == ISC_R_SUCCESS &&
1566 rdtype == dns_rdatatype_ixfr) {
1567 fprintf(stderr, ";; Warning, "
1573 if (result == ISC_R_SUCCESS) {
1574 if (lookup->rdtypeset) {
1575 fprintf(stderr, ";; Warning, "
1576 "extra type option\n");
1578 if (rdtype == dns_rdatatype_ixfr) {
1579 isc_uint32_t serial;
1582 lookup->rdtypeset = ISC_TRUE;
1583 result = parse_uint(&serial,
1587 if (result != ISC_R_SUCCESS)
1588 fatal("Couldn't parse "
1590 lookup->ixfr_serial = serial;
1591 lookup->section_question =
1593 lookup->comments = pluscomm;
1594 lookup->tcp_mode = ISC_TRUE;
1596 lookup->rdtype = rdtype;
1597 lookup->rdtypeset = ISC_TRUE;
1599 dns_rdatatype_axfr) {
1600 lookup->section_question =
1602 lookup->comments = pluscomm;
1604 lookup->ixfr_serial = ISC_FALSE;
1608 result = dns_rdataclass_fromtext(&rdclass,
1609 (isc_textregion_t *)&tr);
1610 if (result == ISC_R_SUCCESS) {
1611 if (lookup->rdclassset) {
1612 fprintf(stderr, ";; Warning, "
1613 "extra class option\n");
1615 lookup->rdclass = rdclass;
1616 lookup->rdclassset = ISC_TRUE;
1623 lookup = clone_lookup(default_lookup,
1625 need_clone = ISC_TRUE;
1626 strncpy(lookup->textname, rv[0],
1627 sizeof(lookup->textname));
1628 lookup->textname[sizeof(lookup->textname)-1]=0;
1629 lookup->trace_root = ISC_TF(lookup->trace ||
1630 lookup->ns_search_only);
1631 lookup->new_search = ISC_TRUE;
1633 printgreeting(argc, argv, lookup);
1634 firstarg = ISC_FALSE;
1636 ISC_LIST_APPEND(lookup_list, lookup, link);
1637 debug("looking up %s", lookup->textname);
1639 /* XXX Error message */
1644 * If we have a batchfile, seed the lookup list with the
1645 * first entry, then trust the callback in dighost_shutdown
1648 if ((batchname != NULL) && !(is_batchfile)) {
1649 if (strcmp(batchname, "-") == 0)
1652 batchfp = fopen(batchname, "r");
1653 if (batchfp == NULL) {
1657 fatal("couldn't open specified batch file");
1659 /* XXX Remove code dup from shutdown code */
1661 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1663 debug("batch line %s", batchline);
1664 if (batchline[0] == '\r' || batchline[0] == '\n'
1665 || batchline[0] == '#' || batchline[0] == ';')
1668 bargv[bargc] = next_token(&input, " \t\r\n");
1669 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1671 bargv[bargc] = next_token(&input, " \t\r\n");
1677 for(i = 0; i < bargc; i++)
1678 debug("batch argv %d: %s", i, bargv[i]);
1679 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1685 * If no lookup specified, search for root
1687 if ((lookup_list.head == NULL) && !config_only) {
1689 lookup = clone_lookup(default_lookup, ISC_TRUE);
1690 need_clone = ISC_TRUE;
1691 lookup->trace_root = ISC_TF(lookup->trace ||
1692 lookup->ns_search_only);
1693 lookup->new_search = ISC_TRUE;
1694 strcpy(lookup->textname, ".");
1695 lookup->rdtype = dns_rdatatype_ns;
1696 lookup->rdtypeset = ISC_TRUE;
1698 printgreeting(argc, argv, lookup);
1699 firstarg = ISC_FALSE;
1701 ISC_LIST_APPEND(lookup_list, lookup, link);
1704 destroy_lookup(lookup);
1708 * Callback from dighost.c to allow program-specific shutdown code.
1709 * Here, we're possibly reading from a batch file, then shutting down
1710 * for real if there's nothing in the batch file to read.
1713 dighost_shutdown(void) {
1714 char batchline[MXNAME];
1720 if (batchname == NULL) {
1726 if (feof(batchfp)) {
1729 if (batchfp != stdin)
1734 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1735 debug("batch line %s", batchline);
1738 bargv[bargc] = next_token(&input, " \t\r\n");
1739 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1741 bargv[bargc] = next_token(&input, " \t\r\n");
1746 for(i = 0; i < bargc; i++)
1747 debug("batch argv %d: %s", i, bargv[i]);
1748 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1752 if (batchfp != stdin)
1759 /*% Main processing routine for dig */
1761 main(int argc, char **argv) {
1762 isc_result_t result;
1764 ISC_LIST_INIT(lookup_list);
1765 ISC_LIST_INIT(server_list);
1766 ISC_LIST_INIT(search_list);
1769 preparse_args(argc, argv);
1771 result = isc_app_start();
1772 check_result(result, "isc_app_start");
1774 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1776 if (domainopt[0] != '\0') {
1777 set_search_domain(domainopt);
1778 usesearch = ISC_TRUE;
1780 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1781 check_result(result, "isc_app_onrun");
1783 destroy_lookup(default_lookup);
1784 if (batchname != NULL) {
1785 if (batchfp != stdin)