2 * Copyright (C) 2004-2010 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.233.62.3 2010/05/13 00:42:26 marka 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>
47 #include <bind9/getaddresses.h>
51 #define ADD_STRING(b, s) { \
52 if (strlen(s) >= isc_buffer_availablelength(b)) \
53 return (ISC_R_NOSPACE); \
55 isc_buffer_putstr(b, s); \
58 #define DIG_MAX_ADDRESSES 20
60 dig_lookup_t *default_lookup = NULL;
62 static char *batchname = NULL;
63 static FILE *batchfp = NULL;
65 static int addresscount = 0;
67 static char domainopt[DNS_NAME_MAXTEXT];
69 static isc_boolean_t short_form = ISC_FALSE, printcmd = ISC_TRUE,
70 ip6_int = ISC_FALSE, plusquest = ISC_FALSE, pluscomm = ISC_FALSE,
71 multiline = ISC_FALSE, nottl = ISC_FALSE, noclass = ISC_FALSE;
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 " +edns=### (Set EDNS version)\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]question (Control display of question)\n"
206 " +[no]answer (Control display of answer)\n"
207 " +[no]authority (Control display of authority)\n"
208 " +[no]additional (Control display of additional)\n"
209 " +[no]stats (Control display of statistics)\n"
210 " +[no]short (Disable everything except short\n"
212 " +[no]ttlid (Control display of ttls in records)\n"
213 " +[no]all (Set or clear all display flags)\n"
214 " +[no]qr (Print question before sending)\n"
215 " +[no]nssearch (Search all authoritative nameservers)\n"
216 " +[no]identify (ID responders in short answers)\n"
217 " +[no]trace (Trace delegation down from root)\n"
218 " +[no]dnssec (Request DNSSEC records)\n"
219 " +[no]nsid (Request Name Server ID)\n"
221 " +[no]sigchase (Chase DNSSEC signatures)\n"
222 " +trusted-key=#### (Trusted Key when chasing DNSSEC sigs)\n"
224 " +[no]topdown (Do DNSSEC validation top down mode)\n"
227 " +[no]multiline (Print records in an expanded format)\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 (!query->lookup->comments)
475 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
477 result = ISC_R_SUCCESS;
479 result = isc_buffer_allocate(mctx, &buf, len);
480 check_result(result, "isc_buffer_allocate");
482 if (query->lookup->comments && !short_form) {
483 if (query->lookup->cmdline[0] != 0)
484 printf("; %s\n", query->lookup->cmdline);
485 if (msg == query->lookup->sendmsg)
486 printf(";; Sending:\n");
488 printf(";; Got answer:\n");
491 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
493 opcodetext[msg->opcode],
494 rcode_totext(msg->rcode),
497 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
499 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
501 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
503 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
505 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
507 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
509 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
511 if ((msg->flags & 0x0040U) != 0)
512 printf("; MBZ: 0x4");
514 printf("; QUERY: %u, ANSWER: %u, "
515 "AUTHORITY: %u, ADDITIONAL: %u\n",
516 msg->counts[DNS_SECTION_QUESTION],
517 msg->counts[DNS_SECTION_ANSWER],
518 msg->counts[DNS_SECTION_AUTHORITY],
519 msg->counts[DNS_SECTION_ADDITIONAL]);
521 if (msg != query->lookup->sendmsg &&
522 (msg->flags & DNS_MESSAGEFLAG_RD) != 0 &&
523 (msg->flags & DNS_MESSAGEFLAG_RA) == 0)
524 printf(";; WARNING: recursion requested "
525 "but not available\n");
527 if (msg != query->lookup->sendmsg && extrabytes != 0U)
528 printf(";; WARNING: Messages has %u extra byte%s at "
529 "end\n", extrabytes, extrabytes != 0 ? "s" : "");
534 if (query->lookup->comments && headers && !short_form) {
535 result = dns_message_pseudosectiontotext(msg,
536 DNS_PSEUDOSECTION_OPT,
538 if (result == ISC_R_NOSPACE) {
541 isc_buffer_free(&buf);
542 result = isc_buffer_allocate(mctx, &buf, len);
543 if (result == ISC_R_SUCCESS)
544 goto repopulate_buffer;
549 "dns_message_pseudosectiontotext");
552 if (query->lookup->section_question && headers) {
554 result = dns_message_sectiontotext(msg,
555 DNS_SECTION_QUESTION,
557 if (result == ISC_R_NOSPACE)
559 check_result(result, "dns_message_sectiontotext");
562 if (query->lookup->section_answer) {
564 result = dns_message_sectiontotext(msg,
567 if (result == ISC_R_NOSPACE)
569 check_result(result, "dns_message_sectiontotext");
571 result = short_answer(msg, flags, buf, query);
572 if (result == ISC_R_NOSPACE)
574 check_result(result, "short_answer");
577 if (query->lookup->section_authority) {
579 result = dns_message_sectiontotext(msg,
580 DNS_SECTION_AUTHORITY,
582 if (result == ISC_R_NOSPACE)
584 check_result(result, "dns_message_sectiontotext");
587 if (query->lookup->section_additional) {
589 result = dns_message_sectiontotext(msg,
590 DNS_SECTION_ADDITIONAL,
592 if (result == ISC_R_NOSPACE)
594 check_result(result, "dns_message_sectiontotext");
596 * Only print the signature on the first record.
599 result = dns_message_pseudosectiontotext(
601 DNS_PSEUDOSECTION_TSIG,
603 if (result == ISC_R_NOSPACE)
606 "dns_message_pseudosectiontotext");
607 result = dns_message_pseudosectiontotext(
609 DNS_PSEUDOSECTION_SIG0,
611 if (result == ISC_R_NOSPACE)
614 "dns_message_pseudosectiontotext");
619 if (headers && query->lookup->comments && !short_form)
622 printf("%.*s", (int)isc_buffer_usedlength(buf),
623 (char *)isc_buffer_base(buf));
624 isc_buffer_free(&buf);
628 dns_master_styledestroy(&style, mctx);
633 * print the greeting message when the program first starts up.
636 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
639 static isc_boolean_t first = ISC_TRUE;
643 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
644 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
645 "%s; <<>> DiG " VERSION " <<>>",
649 snprintf(append, sizeof(append), " %s", argv[i++]);
650 remaining = sizeof(lookup->cmdline) -
651 strlen(lookup->cmdline) - 1;
652 strncat(lookup->cmdline, append, remaining);
654 remaining = sizeof(lookup->cmdline) -
655 strlen(lookup->cmdline) - 1;
656 strncat(lookup->cmdline, "\n", remaining);
657 if (first && addresscount != 0) {
658 snprintf(append, sizeof(append),
659 "; (%d server%s found)\n",
661 addresscount > 1 ? "s" : "");
662 remaining = sizeof(lookup->cmdline) -
663 strlen(lookup->cmdline) - 1;
664 strncat(lookup->cmdline, append, remaining);
667 snprintf(append, sizeof(append),
668 ";; global options:%s%s\n",
669 short_form ? " +short" : "",
670 printcmd ? " +cmd" : "");
672 remaining = sizeof(lookup->cmdline) -
673 strlen(lookup->cmdline) - 1;
674 strncat(lookup->cmdline, append, remaining);
680 * We're not using isc_commandline_parse() here since the command line
681 * syntax of dig is quite a bit different from that which can be described
687 plus_option(char *option, isc_boolean_t is_batchfile,
688 dig_lookup_t *lookup)
691 char option_store[256];
692 char *cmd, *value, *ptr;
694 isc_boolean_t state = ISC_TRUE;
699 strncpy(option_store, option, sizeof(option_store));
700 option_store[sizeof(option_store)-1]=0;
702 cmd = next_token(&ptr,"=");
704 printf(";; Invalid option %s\n", option_store);
708 if (strncasecmp(cmd, "no", 2)==0) {
713 #define FULLCHECK(A) \
715 size_t _l = strlen(cmd); \
716 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
717 goto invalid_option; \
719 #define FULLCHECK2(A, B) \
721 size_t _l = strlen(cmd); \
722 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
723 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
724 goto invalid_option; \
730 case 'a': /* aaonly / aaflag */
731 FULLCHECK2("aaonly", "aaflag");
732 lookup->aaonly = state;
736 case 'd': /* additional */
737 FULLCHECK("additional");
738 lookup->section_additional = state;
740 case 'f': /* adflag */
741 case '\0': /* +ad is a synonym for +adflag */
743 lookup->adflag = state;
751 lookup->section_question = state;
752 lookup->section_authority = state;
753 lookup->section_answer = state;
754 lookup->section_additional = state;
755 lookup->comments = state;
756 lookup->stats = state;
759 case 'n': /* answer */
761 lookup->section_answer = state;
763 case 'u': /* authority */
764 FULLCHECK("authority");
765 lookup->section_authority = state;
773 case 'e':/* besteffort */
774 FULLCHECK("besteffort");
775 lookup->besteffort = state;
777 case 'u':/* bufsize */
778 FULLCHECK("bufsize");
783 result = parse_uint(&num, value, COMMSIZE,
785 if (result != ISC_R_SUCCESS)
786 fatal("Couldn't parse buffer size");
787 lookup->udpsize = num;
795 case 'd':/* cdflag */
797 case 'f': /* cdflag */
798 case '\0': /* +cd is a synonym for +cdflag */
800 lookup->cdflag = state;
808 noclass = ISC_TF(!state);
814 case 'o': /* comments */
815 FULLCHECK("comments");
816 lookup->comments = state;
817 if (lookup == default_lookup)
826 case 'e': /* defname */
827 FULLCHECK("defname");
828 if (!lookup->trace) {
832 case 'n': /* dnssec */
834 if (state && lookup->edns == -1)
836 lookup->dnssec = state;
838 case 'o': /* domain */
844 strncpy(domainopt, value, sizeof(domainopt));
845 domainopt[sizeof(domainopt)-1] = '\0';
859 result = parse_uint(&num, value, 255, "edns");
860 if (result != ISC_R_SUCCESS)
861 fatal("Couldn't parse edns");
866 lookup->servfail_stops = state;
870 case 'd': /* identify */
871 FULLCHECK("identify");
872 lookup->identify = state;
874 case 'g': /* ignore */
875 default: /* Inherits default for compatibility */
877 lookup->ignore = ISC_TRUE;
880 case 'm': /* multiline */
881 FULLCHECK("multiline");
886 case 'd': /* ndots */
892 result = parse_uint(&num, value, MAXNDOTS, "ndots");
893 if (result != ISC_R_SUCCESS)
894 fatal("Couldn't parse ndots");
901 if (state && lookup->edns == -1)
903 lookup->nsid = state;
905 case 's': /* nssearch */
906 FULLCHECK("nssearch");
907 lookup->ns_search_only = state;
909 lookup->trace_root = ISC_TRUE;
910 lookup->recurse = ISC_TRUE;
911 lookup->identify = ISC_TRUE;
912 lookup->stats = ISC_FALSE;
913 lookup->comments = ISC_FALSE;
914 lookup->section_additional = ISC_FALSE;
915 lookup->section_authority = ISC_FALSE;
916 lookup->section_question = ISC_FALSE;
917 lookup->rdtype = dns_rdatatype_ns;
918 lookup->rdtypeset = ISC_TRUE;
919 short_form = ISC_TRUE;
936 case 'u': /* question */
937 FULLCHECK("question");
938 lookup->section_question = state;
939 if (lookup == default_lookup)
950 case 'c': /* recurse */
951 FULLCHECK("recurse");
952 lookup->recurse = state;
954 case 't': /* retry / retries */
955 FULLCHECK2("retry", "retries");
960 result = parse_uint(&lookup->retries, value,
961 MAXTRIES - 1, "retries");
962 if (result != ISC_R_SUCCESS)
963 fatal("Couldn't parse retries");
976 case 'e': /* search */
978 if (!lookup->trace) {
986 case 'r': /* short */
990 printcmd = ISC_FALSE;
991 lookup->section_additional = ISC_FALSE;
992 lookup->section_answer = ISC_TRUE;
993 lookup->section_authority = ISC_FALSE;
994 lookup->section_question = ISC_FALSE;
995 lookup->comments = ISC_FALSE;
996 lookup->stats = ISC_FALSE;
999 case 'w': /* showsearch */
1000 FULLCHECK("showsearch");
1001 if (!lookup->trace) {
1007 goto invalid_option;
1011 case 'i': /* sigchase */
1012 FULLCHECK("sigchase");
1013 lookup->sigchase = state;
1014 if (lookup->sigchase)
1015 lookup->dnssec = ISC_TRUE;
1018 case 't': /* stats */
1020 lookup->stats = state;
1023 goto invalid_option;
1031 lookup->tcp_mode = state;
1033 case 'i': /* timeout */
1034 FULLCHECK("timeout");
1038 goto invalid_option;
1039 result = parse_uint(&timeout, value, MAXTIMEOUT,
1041 if (result != ISC_R_SUCCESS)
1042 fatal("Couldn't parse timeout");
1047 case 'o': /* topdown */
1048 FULLCHECK("topdown");
1049 lookup->do_topdown = state;
1054 case 'a': /* trace */
1056 lookup->trace = state;
1057 lookup->trace_root = state;
1059 lookup->recurse = ISC_FALSE;
1060 lookup->identify = ISC_TRUE;
1061 lookup->comments = ISC_FALSE;
1062 lookup->stats = ISC_FALSE;
1063 lookup->section_additional = ISC_FALSE;
1064 lookup->section_authority = ISC_TRUE;
1065 lookup->section_question = ISC_FALSE;
1066 usesearch = ISC_FALSE;
1069 case 'i': /* tries */
1074 goto invalid_option;
1075 result = parse_uint(&lookup->retries, value,
1077 if (result != ISC_R_SUCCESS)
1078 fatal("Couldn't parse tries");
1079 if (lookup->retries == 0)
1080 lookup->retries = 1;
1083 case 'u': /* trusted-key */
1084 FULLCHECK("trusted-key");
1088 goto invalid_option;
1089 n = strlcpy(trustedkey, ptr,
1090 sizeof(trustedkey));
1091 if (n >= sizeof(trustedkey))
1092 fatal("trusted key too large");
1096 goto invalid_option;
1099 case 't': /* ttlid */
1101 nottl = ISC_TF(!state);
1104 goto invalid_option;
1110 lookup->tcp_mode = state;
1115 fprintf(stderr, "Invalid option: +%s\n",
1123 * #ISC_TRUE returned if value was used
1125 static const char *single_dash_opts = "46dhimnv";
1126 static const char *dash_opts = "46bcdfhikmnptvyx";
1127 static isc_boolean_t
1128 dash_option(char *option, char *next, dig_lookup_t **lookup,
1129 isc_boolean_t *open_type_class, isc_boolean_t *need_clone,
1130 isc_boolean_t config_only, int argc, char **argv,
1131 isc_boolean_t *firstarg)
1133 char opt, *value, *ptr, *ptr2, *ptr3;
1134 isc_result_t result;
1135 isc_boolean_t value_from_next;
1136 isc_textregion_t tr;
1137 dns_rdatatype_t rdtype;
1138 dns_rdataclass_t rdclass;
1139 char textname[MXNAME];
1141 struct in6_addr in6;
1146 while (strpbrk(option, single_dash_opts) == &option[0]) {
1148 * Since the -[46dhimnv] options do not take an argument,
1149 * account for them (in any number and/or combination)
1150 * if they appear as the first character(s) of a q-opt.
1156 isc_net_disableipv6();
1157 have_ipv6 = ISC_FALSE;
1159 fatal("can't find IPv4 networking");
1166 isc_net_disableipv4();
1167 have_ipv4 = ISC_FALSE;
1169 fatal("can't find IPv6 networking");
1175 ptr = strpbrk(&option[1], dash_opts);
1176 if (ptr != &option[1]) {
1179 debugging = ISC_TRUE;
1182 debugging = ISC_TRUE;
1191 case 'm': /* memdebug */
1192 /* memdebug is handled in preparse_args() */
1202 if (strlen(option) > 1U)
1203 option = &option[1];
1208 if (strlen(option) > 1U) {
1209 value_from_next = ISC_FALSE;
1212 value_from_next = ISC_TRUE;
1216 goto invalid_option;
1219 hash = strchr(value, '#');
1221 result = parse_uint(&num, hash + 1, MAXPORT,
1223 if (result != ISC_R_SUCCESS)
1224 fatal("Couldn't parse port number");
1229 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1230 isc_sockaddr_fromin6(&bind_address, &in6, srcport);
1231 isc_net_disableipv4();
1232 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1233 isc_sockaddr_fromin(&bind_address, &in4, srcport);
1234 isc_net_disableipv6();
1238 fatal("invalid address %s", value);
1242 specified_source = ISC_TRUE;
1243 return (value_from_next);
1245 if ((*lookup)->rdclassset) {
1246 fprintf(stderr, ";; Warning, extra class option\n");
1248 *open_type_class = ISC_FALSE;
1250 tr.length = strlen(value);
1251 result = dns_rdataclass_fromtext(&rdclass,
1252 (isc_textregion_t *)&tr);
1253 if (result == ISC_R_SUCCESS) {
1254 (*lookup)->rdclass = rdclass;
1255 (*lookup)->rdclassset = ISC_TRUE;
1257 fprintf(stderr, ";; Warning, ignoring "
1258 "invalid class %s\n",
1260 return (value_from_next);
1263 return (value_from_next);
1265 strncpy(keyfile, value, sizeof(keyfile));
1266 keyfile[sizeof(keyfile)-1]=0;
1267 return (value_from_next);
1269 result = parse_uint(&num, value, MAXPORT, "port number");
1270 if (result != ISC_R_SUCCESS)
1271 fatal("Couldn't parse port number");
1273 return (value_from_next);
1277 (*lookup) = clone_lookup(default_lookup,
1279 *need_clone = ISC_TRUE;
1280 strncpy((*lookup)->textname, value,
1281 sizeof((*lookup)->textname));
1282 (*lookup)->textname[sizeof((*lookup)->textname)-1]=0;
1283 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1284 (*lookup)->ns_search_only);
1285 (*lookup)->new_search = ISC_TRUE;
1287 printgreeting(argc, argv, *lookup);
1288 *firstarg = ISC_FALSE;
1290 ISC_LIST_APPEND(lookup_list, (*lookup), link);
1291 debug("looking up %s", (*lookup)->textname);
1293 return (value_from_next);
1295 *open_type_class = ISC_FALSE;
1296 if (strncasecmp(value, "ixfr=", 5) == 0) {
1297 rdtype = dns_rdatatype_ixfr;
1298 result = ISC_R_SUCCESS;
1301 tr.length = strlen(value);
1302 result = dns_rdatatype_fromtext(&rdtype,
1303 (isc_textregion_t *)&tr);
1304 if (result == ISC_R_SUCCESS &&
1305 rdtype == dns_rdatatype_ixfr) {
1306 result = DNS_R_UNKNOWN;
1309 if (result == ISC_R_SUCCESS) {
1310 if ((*lookup)->rdtypeset) {
1311 fprintf(stderr, ";; Warning, "
1312 "extra type option\n");
1314 if (rdtype == dns_rdatatype_ixfr) {
1315 isc_uint32_t serial;
1316 (*lookup)->rdtype = dns_rdatatype_ixfr;
1317 (*lookup)->rdtypeset = ISC_TRUE;
1318 result = parse_uint(&serial, &value[5],
1319 MAXSERIAL, "serial number");
1320 if (result != ISC_R_SUCCESS)
1321 fatal("Couldn't parse serial number");
1322 (*lookup)->ixfr_serial = serial;
1323 (*lookup)->section_question = plusquest;
1324 (*lookup)->comments = pluscomm;
1325 (*lookup)->tcp_mode = ISC_TRUE;
1327 (*lookup)->rdtype = rdtype;
1328 (*lookup)->rdtypeset = ISC_TRUE;
1329 if (rdtype == dns_rdatatype_axfr) {
1330 (*lookup)->section_question = plusquest;
1331 (*lookup)->comments = pluscomm;
1333 (*lookup)->ixfr_serial = ISC_FALSE;
1336 fprintf(stderr, ";; Warning, ignoring "
1337 "invalid type %s\n",
1339 return (value_from_next);
1341 ptr = next_token(&value,":"); /* hmac type or name */
1345 ptr2 = next_token(&value, ":"); /* name or secret */
1348 ptr3 = next_token(&value,":"); /* secret or NULL */
1354 hmacname = DNS_TSIG_HMACMD5_NAME;
1357 strncpy(keynametext, ptr, sizeof(keynametext));
1358 keynametext[sizeof(keynametext)-1]=0;
1359 strncpy(keysecret, ptr2, sizeof(keysecret));
1360 keysecret[sizeof(keysecret)-1]=0;
1361 return (value_from_next);
1364 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1365 *need_clone = ISC_TRUE;
1366 if (get_reverse(textname, sizeof(textname), value,
1367 ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
1368 strncpy((*lookup)->textname, textname,
1369 sizeof((*lookup)->textname));
1370 debug("looking up %s", (*lookup)->textname);
1371 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1372 (*lookup)->ns_search_only);
1373 (*lookup)->ip6_int = ip6_int;
1374 if (!(*lookup)->rdtypeset)
1375 (*lookup)->rdtype = dns_rdatatype_ptr;
1376 if (!(*lookup)->rdclassset)
1377 (*lookup)->rdclass = dns_rdataclass_in;
1378 (*lookup)->new_search = ISC_TRUE;
1380 printgreeting(argc, argv, *lookup);
1381 *firstarg = ISC_FALSE;
1383 ISC_LIST_APPEND(lookup_list, *lookup, link);
1385 fprintf(stderr, "Invalid IP address %s\n", value);
1388 return (value_from_next);
1391 fprintf(stderr, "Invalid option: -%s\n", option);
1399 * Because we may be trying to do memory allocation recording, we're going
1400 * to need to parse the arguments for the -m *before* we start the main
1401 * argument parsing routine.
1403 * I'd prefer not to have to do this, but I am not quite sure how else to
1404 * fix the problem. Argument parsing in dig involves memory allocation
1405 * by its nature, so it can't be done in the main argument parser.
1408 preparse_args(int argc, char **argv) {
1415 for (rc--, rv++; rc > 0; rc--, rv++) {
1416 if (rv[0][0] != '-')
1419 while (strpbrk(option, single_dash_opts) == &option[0]) {
1420 if (option[0] == 'm') {
1421 memdebugging = ISC_TRUE;
1422 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1423 ISC_MEM_DEBUGRECORD;
1426 option = &option[1];
1432 getaddresses(dig_lookup_t *lookup, const char *host) {
1433 isc_result_t result;
1434 isc_sockaddr_t sockaddrs[DIG_MAX_ADDRESSES];
1435 isc_netaddr_t netaddr;
1438 char tmp[ISC_NETADDR_FORMATSIZE];
1440 result = bind9_getaddresses(host, 0, sockaddrs,
1441 DIG_MAX_ADDRESSES, &count);
1442 if (result != ISC_R_SUCCESS)
1443 fatal("couldn't get address for '%s': %s",
1444 host, isc_result_totext(result));
1446 for (i = 0; i < count; i++) {
1447 isc_netaddr_fromsockaddr(&netaddr, &sockaddrs[i]);
1448 isc_netaddr_format(&netaddr, tmp, sizeof(tmp));
1449 srv = make_server(tmp, host);
1450 ISC_LIST_APPEND(lookup->my_server_list, srv, link);
1452 addresscount = count;
1456 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1457 int argc, char **argv) {
1458 isc_result_t result;
1459 isc_textregion_t tr;
1460 isc_boolean_t firstarg = ISC_TRUE;
1461 dig_lookup_t *lookup = NULL;
1462 dns_rdatatype_t rdtype;
1463 dns_rdataclass_t rdclass;
1464 isc_boolean_t open_type_class = ISC_TRUE;
1465 char batchline[MXNAME];
1476 isc_boolean_t need_clone = ISC_TRUE;
1479 * The semantics for parsing the args is a bit complex; if
1480 * we don't have a host yet, make the arg apply globally,
1481 * otherwise make it apply to the latest host. This is
1482 * a bit different than the previous versions, but should
1483 * form a consistent user interface.
1485 * First, create a "default lookup" which won't actually be used
1486 * anywhere, except for cloning into new lookups
1489 debug("parse_args()");
1490 if (!is_batchfile) {
1491 debug("making new lookup");
1492 default_lookup = make_empty_lookup();
1496 * Treat ${HOME}/.digrc as a special batchfile
1498 INSIST(batchfp == NULL);
1499 homedir = getenv("HOME");
1500 if (homedir != NULL) {
1502 n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc",
1504 if (n < sizeof(rcfile))
1505 batchfp = fopen(rcfile, "r");
1507 if (batchfp != NULL) {
1508 while (fgets(batchline, sizeof(batchline),
1510 debug("config line %s", batchline);
1513 bargv[bargc] = next_token(&input, " \t\r\n");
1514 while ((bargv[bargc] != NULL) &&
1518 next_token(&input, " \t\r\n");
1524 for(i = 0; i < bargc; i++)
1525 debug(".digrc argv %d: %s",
1527 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1535 if (is_batchfile && !config_only) {
1536 /* Processing '-f batchfile'. */
1537 lookup = clone_lookup(default_lookup, ISC_TRUE);
1538 need_clone = ISC_FALSE;
1540 lookup = default_lookup;
1544 for (rc--, rv++; rc > 0; rc--, rv++) {
1545 debug("main parsing %s", rv[0]);
1546 if (strncmp(rv[0], "%", 1) == 0)
1548 if (strncmp(rv[0], "@", 1) == 0) {
1549 getaddresses(lookup, &rv[0][1]);
1550 } else if (rv[0][0] == '+') {
1551 plus_option(&rv[0][1], is_batchfile,
1553 } else if (rv[0][0] == '-') {
1555 if (dash_option(&rv[0][1], NULL,
1556 &lookup, &open_type_class,
1557 &need_clone, config_only,
1558 argc, argv, &firstarg)) {
1563 if (dash_option(&rv[0][1], rv[1],
1564 &lookup, &open_type_class,
1565 &need_clone, config_only,
1566 argc, argv, &firstarg)) {
1573 * Anything which isn't an option
1575 if (open_type_class) {
1576 if (strncasecmp(rv[0], "ixfr=", 5) == 0) {
1577 rdtype = dns_rdatatype_ixfr;
1578 result = ISC_R_SUCCESS;
1581 tr.length = strlen(rv[0]);
1582 result = dns_rdatatype_fromtext(&rdtype,
1583 (isc_textregion_t *)&tr);
1584 if (result == ISC_R_SUCCESS &&
1585 rdtype == dns_rdatatype_ixfr) {
1586 result = DNS_R_UNKNOWN;
1587 fprintf(stderr, ";; Warning, "
1593 if (result == ISC_R_SUCCESS) {
1594 if (lookup->rdtypeset) {
1595 fprintf(stderr, ";; Warning, "
1596 "extra type option\n");
1598 if (rdtype == dns_rdatatype_ixfr) {
1599 isc_uint32_t serial;
1602 lookup->rdtypeset = ISC_TRUE;
1603 result = parse_uint(&serial,
1607 if (result != ISC_R_SUCCESS)
1608 fatal("Couldn't parse "
1610 lookup->ixfr_serial = serial;
1611 lookup->section_question =
1613 lookup->comments = pluscomm;
1614 lookup->tcp_mode = ISC_TRUE;
1616 lookup->rdtype = rdtype;
1617 lookup->rdtypeset = ISC_TRUE;
1619 dns_rdatatype_axfr) {
1620 lookup->section_question =
1622 lookup->comments = pluscomm;
1624 lookup->ixfr_serial = ISC_FALSE;
1628 result = dns_rdataclass_fromtext(&rdclass,
1629 (isc_textregion_t *)&tr);
1630 if (result == ISC_R_SUCCESS) {
1631 if (lookup->rdclassset) {
1632 fprintf(stderr, ";; Warning, "
1633 "extra class option\n");
1635 lookup->rdclass = rdclass;
1636 lookup->rdclassset = ISC_TRUE;
1643 lookup = clone_lookup(default_lookup,
1645 need_clone = ISC_TRUE;
1646 strncpy(lookup->textname, rv[0],
1647 sizeof(lookup->textname));
1648 lookup->textname[sizeof(lookup->textname)-1]=0;
1649 lookup->trace_root = ISC_TF(lookup->trace ||
1650 lookup->ns_search_only);
1651 lookup->new_search = ISC_TRUE;
1653 printgreeting(argc, argv, lookup);
1654 firstarg = ISC_FALSE;
1656 ISC_LIST_APPEND(lookup_list, lookup, link);
1657 debug("looking up %s", lookup->textname);
1659 /* XXX Error message */
1664 * If we have a batchfile, seed the lookup list with the
1665 * first entry, then trust the callback in dighost_shutdown
1668 if ((batchname != NULL) && !(is_batchfile)) {
1669 if (strcmp(batchname, "-") == 0)
1672 batchfp = fopen(batchname, "r");
1673 if (batchfp == NULL) {
1677 fatal("couldn't open specified batch file");
1679 /* XXX Remove code dup from shutdown code */
1681 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1683 debug("batch line %s", batchline);
1684 if (batchline[0] == '\r' || batchline[0] == '\n'
1685 || batchline[0] == '#' || batchline[0] == ';')
1688 bargv[bargc] = next_token(&input, " \t\r\n");
1689 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1691 bargv[bargc] = next_token(&input, " \t\r\n");
1697 for(i = 0; i < bargc; i++)
1698 debug("batch argv %d: %s", i, bargv[i]);
1699 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1705 * If no lookup specified, search for root
1707 if ((lookup_list.head == NULL) && !config_only) {
1709 lookup = clone_lookup(default_lookup, ISC_TRUE);
1710 need_clone = ISC_TRUE;
1711 lookup->trace_root = ISC_TF(lookup->trace ||
1712 lookup->ns_search_only);
1713 lookup->new_search = ISC_TRUE;
1714 strcpy(lookup->textname, ".");
1715 lookup->rdtype = dns_rdatatype_ns;
1716 lookup->rdtypeset = ISC_TRUE;
1718 printgreeting(argc, argv, lookup);
1719 firstarg = ISC_FALSE;
1721 ISC_LIST_APPEND(lookup_list, lookup, link);
1724 destroy_lookup(lookup);
1728 * Callback from dighost.c to allow program-specific shutdown code.
1729 * Here, we're possibly reading from a batch file, then shutting down
1730 * for real if there's nothing in the batch file to read.
1733 dighost_shutdown(void) {
1734 char batchline[MXNAME];
1740 if (batchname == NULL) {
1746 if (feof(batchfp)) {
1749 if (batchfp != stdin)
1754 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1755 debug("batch line %s", batchline);
1758 bargv[bargc] = next_token(&input, " \t\r\n");
1759 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1761 bargv[bargc] = next_token(&input, " \t\r\n");
1766 for(i = 0; i < bargc; i++)
1767 debug("batch argv %d: %s", i, bargv[i]);
1768 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1772 if (batchfp != stdin)
1779 /*% Main processing routine for dig */
1781 main(int argc, char **argv) {
1782 isc_result_t result;
1784 ISC_LIST_INIT(lookup_list);
1785 ISC_LIST_INIT(server_list);
1786 ISC_LIST_INIT(search_list);
1789 preparse_args(argc, argv);
1791 result = isc_app_start();
1792 check_result(result, "isc_app_start");
1794 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1796 if (domainopt[0] != '\0') {
1797 set_search_domain(domainopt);
1798 usesearch = ISC_TRUE;
1800 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1801 check_result(result, "isc_app_onrun");
1803 destroy_lookup(default_lookup);
1804 if (batchname != NULL) {
1805 if (batchfp != stdin)