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.237 2010/05/13 00:40:46 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,
75 static const char * const opcodetext[] = {
94 /*% return code text */
95 static const char * const rcodetext[] = {
115 /*% safe rcodetext[] */
117 rcode_totext(dns_rcode_t rcode)
119 static char buf[sizeof("?65535")];
121 const char *consttext;
125 if (rcode >= (sizeof(rcodetext)/sizeof(rcodetext[0]))) {
126 snprintf(buf, sizeof(buf), "?%u", rcode);
127 totext.deconsttext = buf;
129 totext.consttext = rcodetext[rcode];
130 return totext.deconsttext;
135 print_usage(FILE *fp) {
137 "Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}\n"
138 " {global-d-opt} host [@local-server] {local-d-opt}\n"
139 " [ host [@local-server] {local-d-opt} [...]]\n", fp);
142 ISC_PLATFORM_NORETURN_PRE static void
143 usage(void) ISC_PLATFORM_NORETURN_POST;
148 fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
149 "for complete list of options\n", stderr);
156 fputs("DiG " VERSION "\n", stderr);
164 "Where: domain is in the Domain Name System\n"
165 " q-class is one of (in,hs,ch,...) [default: in]\n"
166 " q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
167 " (Use ixfr=version for type ixfr)\n"
168 " q-opt is one of:\n"
169 " -x dot-notation (shortcut for reverse lookups)\n"
170 " -i (use IP6.INT for IPv6 reverse lookups)\n"
171 " -f filename (batch mode)\n"
172 " -b address[#port] (bind to source address/port)\n"
173 " -p port (specify port number)\n"
174 " -q name (specify query name)\n"
175 " -t type (specify query type)\n"
176 " -c class (specify query class)\n"
177 " -k keyfile (specify tsig key file)\n"
178 " -y [hmac:]name:key (specify named base64 tsig key)\n"
179 " -4 (use IPv4 query transport only)\n"
180 " -6 (use IPv6 query transport only)\n"
181 " -m (enable memory usage debugging)\n"
182 " d-opt is of the form +keyword[=value], where keyword is:\n"
183 " +[no]vc (TCP mode)\n"
184 " +[no]tcp (TCP mode, alternate syntax)\n"
185 " +time=### (Set query timeout) [5]\n"
186 " +tries=### (Set number of UDP attempts) [3]\n"
187 " +retry=### (Set number of UDP retries) [2]\n"
188 " +domain=### (Set default domainname)\n"
189 " +bufsize=### (Set EDNS0 Max UDP packet size)\n"
190 " +ndots=### (Set NDOTS value)\n"
191 " +edns=### (Set EDNS version)\n"
192 " +[no]search (Set whether to use searchlist)\n"
193 " +[no]showsearch (Search with intermediate results)\n"
194 " +[no]defname (Ditto)\n"
195 " +[no]recurse (Recursive mode)\n"
196 " +[no]ignore (Don't revert to TCP for TC responses.)"
198 " +[no]fail (Don't try next server on SERVFAIL)\n"
199 " +[no]besteffort (Try to parse even illegal messages)\n"
200 " +[no]aaonly (Set AA flag in query (+[no]aaflag))\n"
201 " +[no]adflag (Set AD flag in query)\n"
202 " +[no]cdflag (Set CD flag in query)\n"
203 " +[no]cl (Control display of class in records)\n"
204 " +[no]cmd (Control display of command line)\n"
205 " +[no]comments (Control display of comment lines)\n"
206 " +[no]question (Control display of question)\n"
207 " +[no]answer (Control display of answer)\n"
208 " +[no]authority (Control display of authority)\n"
209 " +[no]additional (Control display of additional)\n"
210 " +[no]stats (Control display of statistics)\n"
211 " +[no]short (Disable everything except short\n"
213 " +[no]ttlid (Control display of ttls in records)\n"
214 " +[no]all (Set or clear all display flags)\n"
215 " +[no]qr (Print question before sending)\n"
216 " +[no]nssearch (Search all authoritative nameservers)\n"
217 " +[no]identify (ID responders in short answers)\n"
218 " +[no]trace (Trace delegation down from root)\n"
219 " +[no]dnssec (Request DNSSEC records)\n"
220 " +[no]nsid (Request Name Server ID)\n"
222 " +[no]sigchase (Chase DNSSEC signatures)\n"
223 " +trusted-key=#### (Trusted Key when chasing DNSSEC sigs)\n"
225 " +[no]topdown (Do DNSSEC validation top down mode)\n"
228 " +[no]multiline (Print records in an expanded format)\n"
229 " +[no]onesoa (AXFR prints only one soa record)\n"
230 " global d-opts and servers (before host name) affect all queries.\n"
231 " local d-opts and servers (after host name) affect only that lookup.\n"
232 " -h (print help and exit)\n"
233 " -v (print version and exit)\n",
238 * Callback from dighost.c to print the received message.
241 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
245 char fromtext[ISC_SOCKADDR_FORMATSIZE];
247 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
251 if (query->lookup->stats && !short_form) {
252 diff = isc_time_microdiff(&now, &query->time_sent);
253 printf(";; Query time: %ld msec\n", (long int)diff/1000);
254 printf(";; SERVER: %s(%s)\n", fromtext, query->servname);
256 printf(";; WHEN: %s", ctime(&tnow));
257 if (query->lookup->doing_xfr) {
258 printf(";; XFR size: %u records (messages %u, "
259 "bytes %" ISC_PRINT_QUADFORMAT "u)\n",
260 query->rr_count, query->msg_count,
263 printf(";; MSG SIZE rcvd: %u\n", bytes);
268 puts(";; WARNING -- Some TSIG could not "
271 if ((key == NULL) && (keysecret[0] != 0)) {
272 puts(";; WARNING -- TSIG key was not used.");
275 } else if (query->lookup->identify && !short_form) {
276 diff = isc_time_microdiff(&now, &query->time_sent);
277 printf(";; Received %" ISC_PRINT_QUADFORMAT "u bytes "
278 "from %s(%s) in %d ms\n\n",
279 query->lookup->doing_xfr ?
280 query->byte_count : (isc_uint64_t)bytes,
281 fromtext, query->servname,
287 * Callback from dighost.c to print that it is trying a server.
292 trying(char *frm, dig_lookup_t *lookup) {
298 * Internal print routine used to print short form replies.
301 say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
305 char store[sizeof("12345678901234567890")];
307 if (query->lookup->trace || query->lookup->ns_search_only) {
308 result = dns_rdatatype_totext(rdata->type, buf);
309 if (result != ISC_R_SUCCESS)
311 ADD_STRING(buf, " ");
313 result = dns_rdata_totext(rdata, NULL, buf);
314 if (result == ISC_R_NOSPACE)
316 check_result(result, "dns_rdata_totext");
317 if (query->lookup->identify) {
319 diff = isc_time_microdiff(&now, &query->time_sent);
320 ADD_STRING(buf, " from server ");
321 ADD_STRING(buf, query->servname);
322 snprintf(store, 19, " in %d ms.", (int)diff/1000);
323 ADD_STRING(buf, store);
325 ADD_STRING(buf, "\n");
326 return (ISC_R_SUCCESS);
330 * short_form message print handler. Calls above say_message()
333 short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
334 isc_buffer_t *buf, dig_query_t *query)
337 dns_rdataset_t *rdataset;
338 isc_result_t result, loopresult;
339 dns_name_t empty_name;
340 dns_rdata_t rdata = DNS_RDATA_INIT;
344 dns_name_init(&empty_name, NULL);
345 result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
346 if (result == ISC_R_NOMORE)
347 return (ISC_R_SUCCESS);
348 else if (result != ISC_R_SUCCESS)
353 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
355 for (rdataset = ISC_LIST_HEAD(name->list);
357 rdataset = ISC_LIST_NEXT(rdataset, link)) {
358 loopresult = dns_rdataset_first(rdataset);
359 while (loopresult == ISC_R_SUCCESS) {
360 dns_rdataset_current(rdataset, &rdata);
361 result = say_message(&rdata, query,
363 if (result == ISC_R_NOSPACE)
365 check_result(result, "say_message");
366 loopresult = dns_rdataset_next(rdataset);
367 dns_rdata_reset(&rdata);
370 result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
371 if (result == ISC_R_NOMORE)
373 else if (result != ISC_R_SUCCESS)
377 return (ISC_R_SUCCESS);
381 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
382 isc_buffer_t *target)
385 dns_master_style_t *style = NULL;
386 unsigned int styleflags = 0;
388 if (rdataset == NULL || owner_name == NULL || target == NULL)
391 styleflags |= DNS_STYLEFLAG_REL_OWNER;
393 styleflags |= DNS_STYLEFLAG_NO_TTL;
395 styleflags |= DNS_STYLEFLAG_NO_CLASS;
397 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
398 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
399 styleflags |= DNS_STYLEFLAG_REL_DATA;
400 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
401 styleflags |= DNS_STYLEFLAG_TTL;
402 styleflags |= DNS_STYLEFLAG_MULTILINE;
403 styleflags |= DNS_STYLEFLAG_COMMENT;
405 if (multiline || (nottl && noclass))
406 result = dns_master_stylecreate(&style, styleflags,
407 24, 24, 24, 32, 80, 8, mctx);
408 else if (nottl || noclass)
409 result = dns_master_stylecreate(&style, styleflags,
410 24, 24, 32, 40, 80, 8, mctx);
412 result = dns_master_stylecreate(&style, styleflags,
413 24, 32, 40, 48, 80, 8, mctx);
414 check_result(result, "dns_master_stylecreate");
416 result = dns_master_rdatasettotext(owner_name, rdataset, style, target);
419 dns_master_styledestroy(&style, mctx);
426 * Callback from dighost.c to print the reply from a server
429 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
431 dns_messagetextflag_t flags;
432 isc_buffer_t *buf = NULL;
433 unsigned int len = OUTPUTBUF;
434 dns_master_style_t *style = NULL;
435 unsigned int styleflags = 0;
437 styleflags |= DNS_STYLEFLAG_REL_OWNER;
439 styleflags |= DNS_STYLEFLAG_NO_TTL;
441 styleflags |= DNS_STYLEFLAG_NO_CLASS;
443 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
444 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
445 styleflags |= DNS_STYLEFLAG_REL_DATA;
446 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
447 styleflags |= DNS_STYLEFLAG_TTL;
448 styleflags |= DNS_STYLEFLAG_MULTILINE;
449 styleflags |= DNS_STYLEFLAG_COMMENT;
451 if (multiline || (nottl && noclass))
452 result = dns_master_stylecreate(&style, styleflags,
453 24, 24, 24, 32, 80, 8, mctx);
454 else if (nottl || noclass)
455 result = dns_master_stylecreate(&style, styleflags,
456 24, 24, 32, 40, 80, 8, mctx);
458 result = dns_master_stylecreate(&style, styleflags,
459 24, 32, 40, 48, 80, 8, mctx);
460 check_result(result, "dns_master_stylecreate");
462 if (query->lookup->cmdline[0] != 0) {
464 fputs(query->lookup->cmdline, stdout);
465 query->lookup->cmdline[0]=0;
467 debug("printmessage(%s %s %s)", headers ? "headers" : "noheaders",
468 query->lookup->comments ? "comments" : "nocomments",
469 short_form ? "short_form" : "long_form");
473 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
474 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
476 if (onesoa && query->lookup->rdtype == dns_rdatatype_axfr)
477 flags |= (query->msg_count == 0) ? DNS_MESSAGETEXTFLAG_ONESOA :
478 DNS_MESSAGETEXTFLAG_OMITSOA;
479 if (!query->lookup->comments)
480 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
482 result = ISC_R_SUCCESS;
484 result = isc_buffer_allocate(mctx, &buf, len);
485 check_result(result, "isc_buffer_allocate");
487 if (query->lookup->comments && !short_form) {
488 if (query->lookup->cmdline[0] != 0)
489 printf("; %s\n", query->lookup->cmdline);
490 if (msg == query->lookup->sendmsg)
491 printf(";; Sending:\n");
493 printf(";; Got answer:\n");
496 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
498 opcodetext[msg->opcode],
499 rcode_totext(msg->rcode),
502 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
504 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
506 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
508 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
510 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
512 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
514 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
516 if ((msg->flags & 0x0040U) != 0)
517 printf("; MBZ: 0x4");
519 printf("; QUERY: %u, ANSWER: %u, "
520 "AUTHORITY: %u, ADDITIONAL: %u\n",
521 msg->counts[DNS_SECTION_QUESTION],
522 msg->counts[DNS_SECTION_ANSWER],
523 msg->counts[DNS_SECTION_AUTHORITY],
524 msg->counts[DNS_SECTION_ADDITIONAL]);
526 if (msg != query->lookup->sendmsg &&
527 (msg->flags & DNS_MESSAGEFLAG_RD) != 0 &&
528 (msg->flags & DNS_MESSAGEFLAG_RA) == 0)
529 printf(";; WARNING: recursion requested "
530 "but not available\n");
532 if (msg != query->lookup->sendmsg && extrabytes != 0U)
533 printf(";; WARNING: Messages has %u extra byte%s at "
534 "end\n", extrabytes, extrabytes != 0 ? "s" : "");
539 if (query->lookup->comments && headers && !short_form) {
540 result = dns_message_pseudosectiontotext(msg,
541 DNS_PSEUDOSECTION_OPT,
543 if (result == ISC_R_NOSPACE) {
546 isc_buffer_free(&buf);
547 result = isc_buffer_allocate(mctx, &buf, len);
548 if (result == ISC_R_SUCCESS)
549 goto repopulate_buffer;
554 "dns_message_pseudosectiontotext");
557 if (query->lookup->section_question && headers) {
559 result = dns_message_sectiontotext(msg,
560 DNS_SECTION_QUESTION,
562 if (result == ISC_R_NOSPACE)
564 check_result(result, "dns_message_sectiontotext");
567 if (query->lookup->section_answer) {
569 result = dns_message_sectiontotext(msg,
572 if (result == ISC_R_NOSPACE)
574 check_result(result, "dns_message_sectiontotext");
576 result = short_answer(msg, flags, buf, query);
577 if (result == ISC_R_NOSPACE)
579 check_result(result, "short_answer");
582 if (query->lookup->section_authority) {
584 result = dns_message_sectiontotext(msg,
585 DNS_SECTION_AUTHORITY,
587 if (result == ISC_R_NOSPACE)
589 check_result(result, "dns_message_sectiontotext");
592 if (query->lookup->section_additional) {
594 result = dns_message_sectiontotext(msg,
595 DNS_SECTION_ADDITIONAL,
597 if (result == ISC_R_NOSPACE)
599 check_result(result, "dns_message_sectiontotext");
601 * Only print the signature on the first record.
604 result = dns_message_pseudosectiontotext(
606 DNS_PSEUDOSECTION_TSIG,
608 if (result == ISC_R_NOSPACE)
611 "dns_message_pseudosectiontotext");
612 result = dns_message_pseudosectiontotext(
614 DNS_PSEUDOSECTION_SIG0,
616 if (result == ISC_R_NOSPACE)
619 "dns_message_pseudosectiontotext");
624 if (headers && query->lookup->comments && !short_form)
627 printf("%.*s", (int)isc_buffer_usedlength(buf),
628 (char *)isc_buffer_base(buf));
629 isc_buffer_free(&buf);
633 dns_master_styledestroy(&style, mctx);
638 * print the greeting message when the program first starts up.
641 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
644 static isc_boolean_t first = ISC_TRUE;
648 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
649 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
650 "%s; <<>> DiG " VERSION " <<>>",
654 snprintf(append, sizeof(append), " %s", argv[i++]);
655 remaining = sizeof(lookup->cmdline) -
656 strlen(lookup->cmdline) - 1;
657 strncat(lookup->cmdline, append, remaining);
659 remaining = sizeof(lookup->cmdline) -
660 strlen(lookup->cmdline) - 1;
661 strncat(lookup->cmdline, "\n", remaining);
662 if (first && addresscount != 0) {
663 snprintf(append, sizeof(append),
664 "; (%d server%s found)\n",
666 addresscount > 1 ? "s" : "");
667 remaining = sizeof(lookup->cmdline) -
668 strlen(lookup->cmdline) - 1;
669 strncat(lookup->cmdline, append, remaining);
672 snprintf(append, sizeof(append),
673 ";; global options:%s%s\n",
674 short_form ? " +short" : "",
675 printcmd ? " +cmd" : "");
677 remaining = sizeof(lookup->cmdline) -
678 strlen(lookup->cmdline) - 1;
679 strncat(lookup->cmdline, append, remaining);
685 * We're not using isc_commandline_parse() here since the command line
686 * syntax of dig is quite a bit different from that which can be described
692 plus_option(char *option, isc_boolean_t is_batchfile,
693 dig_lookup_t *lookup)
696 char option_store[256];
697 char *cmd, *value, *ptr;
699 isc_boolean_t state = ISC_TRUE;
704 strncpy(option_store, option, sizeof(option_store));
705 option_store[sizeof(option_store)-1]=0;
707 cmd = next_token(&ptr,"=");
709 printf(";; Invalid option %s\n", option_store);
713 if (strncasecmp(cmd, "no", 2)==0) {
718 #define FULLCHECK(A) \
720 size_t _l = strlen(cmd); \
721 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
722 goto invalid_option; \
724 #define FULLCHECK2(A, B) \
726 size_t _l = strlen(cmd); \
727 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
728 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
729 goto invalid_option; \
735 case 'a': /* aaonly / aaflag */
736 FULLCHECK2("aaonly", "aaflag");
737 lookup->aaonly = state;
741 case 'd': /* additional */
742 FULLCHECK("additional");
743 lookup->section_additional = state;
745 case 'f': /* adflag */
746 case '\0': /* +ad is a synonym for +adflag */
748 lookup->adflag = state;
756 lookup->section_question = state;
757 lookup->section_authority = state;
758 lookup->section_answer = state;
759 lookup->section_additional = state;
760 lookup->comments = state;
761 lookup->stats = state;
764 case 'n': /* answer */
766 lookup->section_answer = state;
768 case 'u': /* authority */
769 FULLCHECK("authority");
770 lookup->section_authority = state;
778 case 'e':/* besteffort */
779 FULLCHECK("besteffort");
780 lookup->besteffort = state;
782 case 'u':/* bufsize */
783 FULLCHECK("bufsize");
788 result = parse_uint(&num, value, COMMSIZE,
790 if (result != ISC_R_SUCCESS)
791 fatal("Couldn't parse buffer size");
792 lookup->udpsize = num;
800 case 'd':/* cdflag */
802 case 'f': /* cdflag */
803 case '\0': /* +cd is a synonym for +cdflag */
805 lookup->cdflag = state;
813 noclass = ISC_TF(!state);
819 case 'o': /* comments */
820 FULLCHECK("comments");
821 lookup->comments = state;
822 if (lookup == default_lookup)
831 case 'e': /* defname */
832 FULLCHECK("defname");
833 if (!lookup->trace) {
837 case 'n': /* dnssec */
839 if (state && lookup->edns == -1)
841 lookup->dnssec = state;
843 case 'o': /* domain */
849 strncpy(domainopt, value, sizeof(domainopt));
850 domainopt[sizeof(domainopt)-1] = '\0';
864 result = parse_uint(&num, value, 255, "edns");
865 if (result != ISC_R_SUCCESS)
866 fatal("Couldn't parse edns");
871 lookup->servfail_stops = state;
875 case 'd': /* identify */
876 FULLCHECK("identify");
877 lookup->identify = state;
879 case 'g': /* ignore */
880 default: /* Inherits default for compatibility */
882 lookup->ignore = ISC_TRUE;
885 case 'm': /* multiline */
886 FULLCHECK("multiline");
891 case 'd': /* ndots */
897 result = parse_uint(&num, value, MAXNDOTS, "ndots");
898 if (result != ISC_R_SUCCESS)
899 fatal("Couldn't parse ndots");
906 if (state && lookup->edns == -1)
908 lookup->nsid = state;
910 case 's': /* nssearch */
911 FULLCHECK("nssearch");
912 lookup->ns_search_only = state;
914 lookup->trace_root = ISC_TRUE;
915 lookup->recurse = ISC_TRUE;
916 lookup->identify = ISC_TRUE;
917 lookup->stats = ISC_FALSE;
918 lookup->comments = ISC_FALSE;
919 lookup->section_additional = ISC_FALSE;
920 lookup->section_authority = ISC_FALSE;
921 lookup->section_question = ISC_FALSE;
922 lookup->rdtype = dns_rdatatype_ns;
923 lookup->rdtypeset = ISC_TRUE;
924 short_form = ISC_TRUE;
945 case 'u': /* question */
946 FULLCHECK("question");
947 lookup->section_question = state;
948 if (lookup == default_lookup)
959 case 'c': /* recurse */
960 FULLCHECK("recurse");
961 lookup->recurse = state;
963 case 't': /* retry / retries */
964 FULLCHECK2("retry", "retries");
969 result = parse_uint(&lookup->retries, value,
970 MAXTRIES - 1, "retries");
971 if (result != ISC_R_SUCCESS)
972 fatal("Couldn't parse retries");
985 case 'e': /* search */
987 if (!lookup->trace) {
995 case 'r': /* short */
999 printcmd = ISC_FALSE;
1000 lookup->section_additional = ISC_FALSE;
1001 lookup->section_answer = ISC_TRUE;
1002 lookup->section_authority = ISC_FALSE;
1003 lookup->section_question = ISC_FALSE;
1004 lookup->comments = ISC_FALSE;
1005 lookup->stats = ISC_FALSE;
1008 case 'w': /* showsearch */
1009 FULLCHECK("showsearch");
1010 if (!lookup->trace) {
1016 goto invalid_option;
1020 case 'i': /* sigchase */
1021 FULLCHECK("sigchase");
1022 lookup->sigchase = state;
1023 if (lookup->sigchase)
1024 lookup->dnssec = ISC_TRUE;
1027 case 't': /* stats */
1029 lookup->stats = state;
1032 goto invalid_option;
1040 lookup->tcp_mode = state;
1042 case 'i': /* timeout */
1043 FULLCHECK("timeout");
1047 goto invalid_option;
1048 result = parse_uint(&timeout, value, MAXTIMEOUT,
1050 if (result != ISC_R_SUCCESS)
1051 fatal("Couldn't parse timeout");
1056 case 'o': /* topdown */
1057 FULLCHECK("topdown");
1058 lookup->do_topdown = state;
1063 case 'a': /* trace */
1065 lookup->trace = state;
1066 lookup->trace_root = state;
1068 lookup->recurse = ISC_FALSE;
1069 lookup->identify = ISC_TRUE;
1070 lookup->comments = ISC_FALSE;
1071 lookup->stats = ISC_FALSE;
1072 lookup->section_additional = ISC_FALSE;
1073 lookup->section_authority = ISC_TRUE;
1074 lookup->section_question = ISC_FALSE;
1075 usesearch = ISC_FALSE;
1078 case 'i': /* tries */
1083 goto invalid_option;
1084 result = parse_uint(&lookup->retries, value,
1086 if (result != ISC_R_SUCCESS)
1087 fatal("Couldn't parse tries");
1088 if (lookup->retries == 0)
1089 lookup->retries = 1;
1092 case 'u': /* trusted-key */
1093 FULLCHECK("trusted-key");
1097 goto invalid_option;
1098 n = strlcpy(trustedkey, ptr,
1099 sizeof(trustedkey));
1100 if (n >= sizeof(trustedkey))
1101 fatal("trusted key too large");
1105 goto invalid_option;
1108 case 't': /* ttlid */
1110 nottl = ISC_TF(!state);
1113 goto invalid_option;
1119 lookup->tcp_mode = state;
1124 fprintf(stderr, "Invalid option: +%s\n",
1132 * #ISC_TRUE returned if value was used
1134 static const char *single_dash_opts = "46dhimnv";
1135 static const char *dash_opts = "46bcdfhikmnptvyx";
1136 static isc_boolean_t
1137 dash_option(char *option, char *next, dig_lookup_t **lookup,
1138 isc_boolean_t *open_type_class, isc_boolean_t *need_clone,
1139 isc_boolean_t config_only, int argc, char **argv,
1140 isc_boolean_t *firstarg)
1142 char opt, *value, *ptr, *ptr2, *ptr3;
1143 isc_result_t result;
1144 isc_boolean_t value_from_next;
1145 isc_textregion_t tr;
1146 dns_rdatatype_t rdtype;
1147 dns_rdataclass_t rdclass;
1148 char textname[MXNAME];
1150 struct in6_addr in6;
1155 while (strpbrk(option, single_dash_opts) == &option[0]) {
1157 * Since the -[46dhimnv] options do not take an argument,
1158 * account for them (in any number and/or combination)
1159 * if they appear as the first character(s) of a q-opt.
1165 isc_net_disableipv6();
1166 have_ipv6 = ISC_FALSE;
1168 fatal("can't find IPv4 networking");
1175 isc_net_disableipv4();
1176 have_ipv4 = ISC_FALSE;
1178 fatal("can't find IPv6 networking");
1184 ptr = strpbrk(&option[1], dash_opts);
1185 if (ptr != &option[1]) {
1188 debugging = ISC_TRUE;
1191 debugging = ISC_TRUE;
1200 case 'm': /* memdebug */
1201 /* memdebug is handled in preparse_args() */
1211 if (strlen(option) > 1U)
1212 option = &option[1];
1217 if (strlen(option) > 1U) {
1218 value_from_next = ISC_FALSE;
1221 value_from_next = ISC_TRUE;
1225 goto invalid_option;
1228 hash = strchr(value, '#');
1230 result = parse_uint(&num, hash + 1, MAXPORT,
1232 if (result != ISC_R_SUCCESS)
1233 fatal("Couldn't parse port number");
1238 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1239 isc_sockaddr_fromin6(&bind_address, &in6, srcport);
1240 isc_net_disableipv4();
1241 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1242 isc_sockaddr_fromin(&bind_address, &in4, srcport);
1243 isc_net_disableipv6();
1247 fatal("invalid address %s", value);
1251 specified_source = ISC_TRUE;
1252 return (value_from_next);
1254 if ((*lookup)->rdclassset) {
1255 fprintf(stderr, ";; Warning, extra class option\n");
1257 *open_type_class = ISC_FALSE;
1259 tr.length = strlen(value);
1260 result = dns_rdataclass_fromtext(&rdclass,
1261 (isc_textregion_t *)&tr);
1262 if (result == ISC_R_SUCCESS) {
1263 (*lookup)->rdclass = rdclass;
1264 (*lookup)->rdclassset = ISC_TRUE;
1266 fprintf(stderr, ";; Warning, ignoring "
1267 "invalid class %s\n",
1269 return (value_from_next);
1272 return (value_from_next);
1274 strncpy(keyfile, value, sizeof(keyfile));
1275 keyfile[sizeof(keyfile)-1]=0;
1276 return (value_from_next);
1278 result = parse_uint(&num, value, MAXPORT, "port number");
1279 if (result != ISC_R_SUCCESS)
1280 fatal("Couldn't parse port number");
1282 return (value_from_next);
1286 (*lookup) = clone_lookup(default_lookup,
1288 *need_clone = ISC_TRUE;
1289 strncpy((*lookup)->textname, value,
1290 sizeof((*lookup)->textname));
1291 (*lookup)->textname[sizeof((*lookup)->textname)-1]=0;
1292 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1293 (*lookup)->ns_search_only);
1294 (*lookup)->new_search = ISC_TRUE;
1296 printgreeting(argc, argv, *lookup);
1297 *firstarg = ISC_FALSE;
1299 ISC_LIST_APPEND(lookup_list, (*lookup), link);
1300 debug("looking up %s", (*lookup)->textname);
1302 return (value_from_next);
1304 *open_type_class = ISC_FALSE;
1305 if (strncasecmp(value, "ixfr=", 5) == 0) {
1306 rdtype = dns_rdatatype_ixfr;
1307 result = ISC_R_SUCCESS;
1310 tr.length = strlen(value);
1311 result = dns_rdatatype_fromtext(&rdtype,
1312 (isc_textregion_t *)&tr);
1313 if (result == ISC_R_SUCCESS &&
1314 rdtype == dns_rdatatype_ixfr) {
1315 result = DNS_R_UNKNOWN;
1318 if (result == ISC_R_SUCCESS) {
1319 if ((*lookup)->rdtypeset) {
1320 fprintf(stderr, ";; Warning, "
1321 "extra type option\n");
1323 if (rdtype == dns_rdatatype_ixfr) {
1324 isc_uint32_t serial;
1325 (*lookup)->rdtype = dns_rdatatype_ixfr;
1326 (*lookup)->rdtypeset = ISC_TRUE;
1327 result = parse_uint(&serial, &value[5],
1328 MAXSERIAL, "serial number");
1329 if (result != ISC_R_SUCCESS)
1330 fatal("Couldn't parse serial number");
1331 (*lookup)->ixfr_serial = serial;
1332 (*lookup)->section_question = plusquest;
1333 (*lookup)->comments = pluscomm;
1334 (*lookup)->tcp_mode = ISC_TRUE;
1336 (*lookup)->rdtype = rdtype;
1337 (*lookup)->rdtypeset = ISC_TRUE;
1338 if (rdtype == dns_rdatatype_axfr) {
1339 (*lookup)->section_question = plusquest;
1340 (*lookup)->comments = pluscomm;
1342 (*lookup)->ixfr_serial = ISC_FALSE;
1345 fprintf(stderr, ";; Warning, ignoring "
1346 "invalid type %s\n",
1348 return (value_from_next);
1350 ptr = next_token(&value,":"); /* hmac type or name */
1354 ptr2 = next_token(&value, ":"); /* name or secret */
1357 ptr3 = next_token(&value,":"); /* secret or NULL */
1363 hmacname = DNS_TSIG_HMACMD5_NAME;
1366 strncpy(keynametext, ptr, sizeof(keynametext));
1367 keynametext[sizeof(keynametext)-1]=0;
1368 strncpy(keysecret, ptr2, sizeof(keysecret));
1369 keysecret[sizeof(keysecret)-1]=0;
1370 return (value_from_next);
1373 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1374 *need_clone = ISC_TRUE;
1375 if (get_reverse(textname, sizeof(textname), value,
1376 ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
1377 strncpy((*lookup)->textname, textname,
1378 sizeof((*lookup)->textname));
1379 debug("looking up %s", (*lookup)->textname);
1380 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1381 (*lookup)->ns_search_only);
1382 (*lookup)->ip6_int = ip6_int;
1383 if (!(*lookup)->rdtypeset)
1384 (*lookup)->rdtype = dns_rdatatype_ptr;
1385 if (!(*lookup)->rdclassset)
1386 (*lookup)->rdclass = dns_rdataclass_in;
1387 (*lookup)->new_search = ISC_TRUE;
1389 printgreeting(argc, argv, *lookup);
1390 *firstarg = ISC_FALSE;
1392 ISC_LIST_APPEND(lookup_list, *lookup, link);
1394 fprintf(stderr, "Invalid IP address %s\n", value);
1397 return (value_from_next);
1400 fprintf(stderr, "Invalid option: -%s\n", option);
1408 * Because we may be trying to do memory allocation recording, we're going
1409 * to need to parse the arguments for the -m *before* we start the main
1410 * argument parsing routine.
1412 * I'd prefer not to have to do this, but I am not quite sure how else to
1413 * fix the problem. Argument parsing in dig involves memory allocation
1414 * by its nature, so it can't be done in the main argument parser.
1417 preparse_args(int argc, char **argv) {
1424 for (rc--, rv++; rc > 0; rc--, rv++) {
1425 if (rv[0][0] != '-')
1428 while (strpbrk(option, single_dash_opts) == &option[0]) {
1429 if (option[0] == 'm') {
1430 memdebugging = ISC_TRUE;
1431 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1432 ISC_MEM_DEBUGRECORD;
1435 option = &option[1];
1441 getaddresses(dig_lookup_t *lookup, const char *host) {
1442 isc_result_t result;
1443 isc_sockaddr_t sockaddrs[DIG_MAX_ADDRESSES];
1444 isc_netaddr_t netaddr;
1447 char tmp[ISC_NETADDR_FORMATSIZE];
1449 result = bind9_getaddresses(host, 0, sockaddrs,
1450 DIG_MAX_ADDRESSES, &count);
1451 if (result != ISC_R_SUCCESS)
1452 fatal("couldn't get address for '%s': %s",
1453 host, isc_result_totext(result));
1455 for (i = 0; i < count; i++) {
1456 isc_netaddr_fromsockaddr(&netaddr, &sockaddrs[i]);
1457 isc_netaddr_format(&netaddr, tmp, sizeof(tmp));
1458 srv = make_server(tmp, host);
1459 ISC_LIST_APPEND(lookup->my_server_list, srv, link);
1461 addresscount = count;
1465 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1466 int argc, char **argv) {
1467 isc_result_t result;
1468 isc_textregion_t tr;
1469 isc_boolean_t firstarg = ISC_TRUE;
1470 dig_lookup_t *lookup = NULL;
1471 dns_rdatatype_t rdtype;
1472 dns_rdataclass_t rdclass;
1473 isc_boolean_t open_type_class = ISC_TRUE;
1474 char batchline[MXNAME];
1485 isc_boolean_t need_clone = ISC_TRUE;
1488 * The semantics for parsing the args is a bit complex; if
1489 * we don't have a host yet, make the arg apply globally,
1490 * otherwise make it apply to the latest host. This is
1491 * a bit different than the previous versions, but should
1492 * form a consistent user interface.
1494 * First, create a "default lookup" which won't actually be used
1495 * anywhere, except for cloning into new lookups
1498 debug("parse_args()");
1499 if (!is_batchfile) {
1500 debug("making new lookup");
1501 default_lookup = make_empty_lookup();
1505 * Treat ${HOME}/.digrc as a special batchfile
1507 INSIST(batchfp == NULL);
1508 homedir = getenv("HOME");
1509 if (homedir != NULL) {
1511 n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc",
1513 if (n < sizeof(rcfile))
1514 batchfp = fopen(rcfile, "r");
1516 if (batchfp != NULL) {
1517 while (fgets(batchline, sizeof(batchline),
1519 debug("config line %s", batchline);
1522 bargv[bargc] = next_token(&input, " \t\r\n");
1523 while ((bargv[bargc] != NULL) &&
1527 next_token(&input, " \t\r\n");
1533 for(i = 0; i < bargc; i++)
1534 debug(".digrc argv %d: %s",
1536 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1544 if (is_batchfile && !config_only) {
1545 /* Processing '-f batchfile'. */
1546 lookup = clone_lookup(default_lookup, ISC_TRUE);
1547 need_clone = ISC_FALSE;
1549 lookup = default_lookup;
1553 for (rc--, rv++; rc > 0; rc--, rv++) {
1554 debug("main parsing %s", rv[0]);
1555 if (strncmp(rv[0], "%", 1) == 0)
1557 if (strncmp(rv[0], "@", 1) == 0) {
1558 getaddresses(lookup, &rv[0][1]);
1559 } else if (rv[0][0] == '+') {
1560 plus_option(&rv[0][1], is_batchfile,
1562 } else if (rv[0][0] == '-') {
1564 if (dash_option(&rv[0][1], NULL,
1565 &lookup, &open_type_class,
1566 &need_clone, config_only,
1567 argc, argv, &firstarg)) {
1572 if (dash_option(&rv[0][1], rv[1],
1573 &lookup, &open_type_class,
1574 &need_clone, config_only,
1575 argc, argv, &firstarg)) {
1582 * Anything which isn't an option
1584 if (open_type_class) {
1585 if (strncasecmp(rv[0], "ixfr=", 5) == 0) {
1586 rdtype = dns_rdatatype_ixfr;
1587 result = ISC_R_SUCCESS;
1590 tr.length = strlen(rv[0]);
1591 result = dns_rdatatype_fromtext(&rdtype,
1592 (isc_textregion_t *)&tr);
1593 if (result == ISC_R_SUCCESS &&
1594 rdtype == dns_rdatatype_ixfr) {
1595 result = DNS_R_UNKNOWN;
1596 fprintf(stderr, ";; Warning, "
1602 if (result == ISC_R_SUCCESS) {
1603 if (lookup->rdtypeset) {
1604 fprintf(stderr, ";; Warning, "
1605 "extra type option\n");
1607 if (rdtype == dns_rdatatype_ixfr) {
1608 isc_uint32_t serial;
1611 lookup->rdtypeset = ISC_TRUE;
1612 result = parse_uint(&serial,
1616 if (result != ISC_R_SUCCESS)
1617 fatal("Couldn't parse "
1619 lookup->ixfr_serial = serial;
1620 lookup->section_question =
1622 lookup->comments = pluscomm;
1623 lookup->tcp_mode = ISC_TRUE;
1625 lookup->rdtype = rdtype;
1626 lookup->rdtypeset = ISC_TRUE;
1628 dns_rdatatype_axfr) {
1629 lookup->section_question =
1631 lookup->comments = pluscomm;
1633 lookup->ixfr_serial = ISC_FALSE;
1637 result = dns_rdataclass_fromtext(&rdclass,
1638 (isc_textregion_t *)&tr);
1639 if (result == ISC_R_SUCCESS) {
1640 if (lookup->rdclassset) {
1641 fprintf(stderr, ";; Warning, "
1642 "extra class option\n");
1644 lookup->rdclass = rdclass;
1645 lookup->rdclassset = ISC_TRUE;
1652 lookup = clone_lookup(default_lookup,
1654 need_clone = ISC_TRUE;
1655 strncpy(lookup->textname, rv[0],
1656 sizeof(lookup->textname));
1657 lookup->textname[sizeof(lookup->textname)-1]=0;
1658 lookup->trace_root = ISC_TF(lookup->trace ||
1659 lookup->ns_search_only);
1660 lookup->new_search = ISC_TRUE;
1662 printgreeting(argc, argv, lookup);
1663 firstarg = ISC_FALSE;
1665 ISC_LIST_APPEND(lookup_list, lookup, link);
1666 debug("looking up %s", lookup->textname);
1668 /* XXX Error message */
1673 * If we have a batchfile, seed the lookup list with the
1674 * first entry, then trust the callback in dighost_shutdown
1677 if ((batchname != NULL) && !(is_batchfile)) {
1678 if (strcmp(batchname, "-") == 0)
1681 batchfp = fopen(batchname, "r");
1682 if (batchfp == NULL) {
1686 fatal("couldn't open specified batch file");
1688 /* XXX Remove code dup from shutdown code */
1690 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1692 debug("batch line %s", batchline);
1693 if (batchline[0] == '\r' || batchline[0] == '\n'
1694 || batchline[0] == '#' || batchline[0] == ';')
1697 bargv[bargc] = next_token(&input, " \t\r\n");
1698 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1700 bargv[bargc] = next_token(&input, " \t\r\n");
1706 for(i = 0; i < bargc; i++)
1707 debug("batch argv %d: %s", i, bargv[i]);
1708 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1714 * If no lookup specified, search for root
1716 if ((lookup_list.head == NULL) && !config_only) {
1718 lookup = clone_lookup(default_lookup, ISC_TRUE);
1719 need_clone = ISC_TRUE;
1720 lookup->trace_root = ISC_TF(lookup->trace ||
1721 lookup->ns_search_only);
1722 lookup->new_search = ISC_TRUE;
1723 strcpy(lookup->textname, ".");
1724 lookup->rdtype = dns_rdatatype_ns;
1725 lookup->rdtypeset = ISC_TRUE;
1727 printgreeting(argc, argv, lookup);
1728 firstarg = ISC_FALSE;
1730 ISC_LIST_APPEND(lookup_list, lookup, link);
1733 destroy_lookup(lookup);
1737 * Callback from dighost.c to allow program-specific shutdown code.
1738 * Here, we're possibly reading from a batch file, then shutting down
1739 * for real if there's nothing in the batch file to read.
1742 dighost_shutdown(void) {
1743 char batchline[MXNAME];
1749 if (batchname == NULL) {
1755 if (feof(batchfp)) {
1758 if (batchfp != stdin)
1763 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1764 debug("batch line %s", batchline);
1767 bargv[bargc] = next_token(&input, " \t\r\n");
1768 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1770 bargv[bargc] = next_token(&input, " \t\r\n");
1775 for(i = 0; i < bargc; i++)
1776 debug("batch argv %d: %s", i, bargv[i]);
1777 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1781 if (batchfp != stdin)
1788 /*% Main processing routine for dig */
1790 main(int argc, char **argv) {
1791 isc_result_t result;
1793 ISC_LIST_INIT(lookup_list);
1794 ISC_LIST_INIT(server_list);
1795 ISC_LIST_INIT(search_list);
1798 preparse_args(argc, argv);
1800 result = isc_app_start();
1801 check_result(result, "isc_app_start");
1803 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1805 if (domainopt[0] != '\0') {
1806 set_search_domain(domainopt);
1807 usesearch = ISC_TRUE;
1809 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1810 check_result(result, "isc_app_onrun");
1812 destroy_lookup(default_lookup);
1813 if (batchname != NULL) {
1814 if (batchfp != stdin)