2 * Copyright (C) 2004-2009 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.225.26.4 2009/05/06 10:18:33 fdupont 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);
144 fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
145 "for complete list of options\n", stderr);
152 fputs("DiG " VERSION "\n", stderr);
160 "Where: domain is in the Domain Name System\n"
161 " q-class is one of (in,hs,ch,...) [default: in]\n"
162 " q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
163 " (Use ixfr=version for type ixfr)\n"
164 " q-opt is one of:\n"
165 " -x dot-notation (shortcut for reverse lookups)\n"
166 " -i (use IP6.INT for IPv6 reverse lookups)\n"
167 " -f filename (batch mode)\n"
168 " -b address[#port] (bind to source address/port)\n"
169 " -p port (specify port number)\n"
170 " -q name (specify query name)\n"
171 " -t type (specify query type)\n"
172 " -c class (specify query class)\n"
173 " -k keyfile (specify tsig key file)\n"
174 " -y [hmac:]name:key (specify named base64 tsig key)\n"
175 " -4 (use IPv4 query transport only)\n"
176 " -6 (use IPv6 query transport only)\n"
177 " -m (enable memory usage debugging)\n"
178 " d-opt is of the form +keyword[=value], where keyword is:\n"
179 " +[no]vc (TCP mode)\n"
180 " +[no]tcp (TCP mode, alternate syntax)\n"
181 " +time=### (Set query timeout) [5]\n"
182 " +tries=### (Set number of UDP attempts) [3]\n"
183 " +retry=### (Set number of UDP retries) [2]\n"
184 " +domain=### (Set default domainname)\n"
185 " +bufsize=### (Set EDNS0 Max UDP packet size)\n"
186 " +ndots=### (Set NDOTS value)\n"
187 " +edns=### (Set EDNS version)\n"
188 " +[no]search (Set whether to use searchlist)\n"
189 " +[no]showsearch (Search with intermediate results)\n"
190 " +[no]defname (Ditto)\n"
191 " +[no]recurse (Recursive mode)\n"
192 " +[no]ignore (Don't revert to TCP for TC responses.)"
194 " +[no]fail (Don't try next server on SERVFAIL)\n"
195 " +[no]besteffort (Try to parse even illegal messages)\n"
196 " +[no]aaonly (Set AA flag in query (+[no]aaflag))\n"
197 " +[no]adflag (Set AD flag in query)\n"
198 " +[no]cdflag (Set CD flag in query)\n"
199 " +[no]cl (Control display of class in records)\n"
200 " +[no]cmd (Control display of command line)\n"
201 " +[no]comments (Control display of comment lines)\n"
202 " +[no]question (Control display of question)\n"
203 " +[no]answer (Control display of answer)\n"
204 " +[no]authority (Control display of authority)\n"
205 " +[no]additional (Control display of additional)\n"
206 " +[no]stats (Control display of statistics)\n"
207 " +[no]short (Disable everything except short\n"
209 " +[no]ttlid (Control display of ttls in records)\n"
210 " +[no]all (Set or clear all display flags)\n"
211 " +[no]qr (Print question before sending)\n"
212 " +[no]nssearch (Search all authoritative nameservers)\n"
213 " +[no]identify (ID responders in short answers)\n"
214 " +[no]trace (Trace delegation down from root)\n"
215 " +[no]dnssec (Request DNSSEC records)\n"
216 " +[no]nsid (Request Name Server ID)\n"
218 " +[no]sigchase (Chase DNSSEC signatures)\n"
219 " +trusted-key=#### (Trusted Key when chasing DNSSEC sigs)\n"
221 " +[no]topdown (Do DNSSEC validation top down mode)\n"
224 " +[no]multiline (Print records in an expanded format)\n"
225 " global d-opts and servers (before host name) affect all queries.\n"
226 " local d-opts and servers (after host name) affect only that lookup.\n"
227 " -h (print help and exit)\n"
228 " -v (print version and exit)\n",
233 * Callback from dighost.c to print the received message.
236 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
240 char fromtext[ISC_SOCKADDR_FORMATSIZE];
242 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
246 if (query->lookup->stats && !short_form) {
247 diff = isc_time_microdiff(&now, &query->time_sent);
248 printf(";; Query time: %ld msec\n", (long int)diff/1000);
249 printf(";; SERVER: %s(%s)\n", fromtext, query->servname);
251 printf(";; WHEN: %s", ctime(&tnow));
252 if (query->lookup->doing_xfr) {
253 printf(";; XFR size: %u records (messages %u, "
254 "bytes %" ISC_PRINT_QUADFORMAT "u)\n",
255 query->rr_count, query->msg_count,
258 printf(";; MSG SIZE rcvd: %u\n", bytes);
263 puts(";; WARNING -- Some TSIG could not "
266 if ((key == NULL) && (keysecret[0] != 0)) {
267 puts(";; WARNING -- TSIG key was not used.");
270 } else if (query->lookup->identify && !short_form) {
271 diff = isc_time_microdiff(&now, &query->time_sent);
272 printf(";; Received %" ISC_PRINT_QUADFORMAT "u bytes "
273 "from %s(%s) in %d ms\n\n",
274 query->lookup->doing_xfr ?
275 query->byte_count : (isc_uint64_t)bytes,
276 fromtext, query->servname,
282 * Callback from dighost.c to print that it is trying a server.
287 trying(char *frm, dig_lookup_t *lookup) {
293 * Internal print routine used to print short form replies.
296 say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
300 char store[sizeof("12345678901234567890")];
302 if (query->lookup->trace || query->lookup->ns_search_only) {
303 result = dns_rdatatype_totext(rdata->type, buf);
304 if (result != ISC_R_SUCCESS)
306 ADD_STRING(buf, " ");
308 result = dns_rdata_totext(rdata, NULL, buf);
309 check_result(result, "dns_rdata_totext");
310 if (query->lookup->identify) {
312 diff = isc_time_microdiff(&now, &query->time_sent);
313 ADD_STRING(buf, " from server ");
314 ADD_STRING(buf, query->servname);
315 snprintf(store, 19, " in %d ms.", (int)diff/1000);
316 ADD_STRING(buf, store);
318 ADD_STRING(buf, "\n");
319 return (ISC_R_SUCCESS);
323 * short_form message print handler. Calls above say_message()
326 short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
327 isc_buffer_t *buf, dig_query_t *query)
330 dns_rdataset_t *rdataset;
332 isc_result_t result, loopresult;
333 dns_name_t empty_name;
335 dns_rdata_t rdata = DNS_RDATA_INIT;
339 dns_name_init(&empty_name, NULL);
340 result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
341 if (result == ISC_R_NOMORE)
342 return (ISC_R_SUCCESS);
343 else if (result != ISC_R_SUCCESS)
348 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
350 isc_buffer_init(&target, t, sizeof(t));
352 for (rdataset = ISC_LIST_HEAD(name->list);
354 rdataset = ISC_LIST_NEXT(rdataset, link)) {
355 loopresult = dns_rdataset_first(rdataset);
356 while (loopresult == ISC_R_SUCCESS) {
357 dns_rdataset_current(rdataset, &rdata);
358 result = say_message(&rdata, query,
360 check_result(result, "say_message");
361 loopresult = dns_rdataset_next(rdataset);
362 dns_rdata_reset(&rdata);
365 result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
366 if (result == ISC_R_NOMORE)
368 else if (result != ISC_R_SUCCESS)
372 return (ISC_R_SUCCESS);
376 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
377 isc_buffer_t *target)
380 dns_master_style_t *style = NULL;
381 unsigned int styleflags = 0;
383 if (rdataset == NULL || owner_name == NULL || target == NULL)
386 styleflags |= DNS_STYLEFLAG_REL_OWNER;
388 styleflags |= DNS_STYLEFLAG_NO_TTL;
390 styleflags |= DNS_STYLEFLAG_NO_CLASS;
392 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
393 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
394 styleflags |= DNS_STYLEFLAG_REL_DATA;
395 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
396 styleflags |= DNS_STYLEFLAG_TTL;
397 styleflags |= DNS_STYLEFLAG_MULTILINE;
398 styleflags |= DNS_STYLEFLAG_COMMENT;
400 if (multiline || (nottl && noclass))
401 result = dns_master_stylecreate(&style, styleflags,
402 24, 24, 24, 32, 80, 8, mctx);
403 else if (nottl || noclass)
404 result = dns_master_stylecreate(&style, styleflags,
405 24, 24, 32, 40, 80, 8, mctx);
407 result = dns_master_stylecreate(&style, styleflags,
408 24, 32, 40, 48, 80, 8, mctx);
409 check_result(result, "dns_master_stylecreate");
411 result = dns_master_rdatasettotext(owner_name, rdataset, style, target);
414 dns_master_styledestroy(&style, mctx);
421 * Callback from dighost.c to print the reply from a server
424 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
426 dns_messagetextflag_t flags;
427 isc_buffer_t *buf = NULL;
428 unsigned int len = OUTPUTBUF;
429 dns_master_style_t *style = NULL;
430 unsigned int styleflags = 0;
432 styleflags |= DNS_STYLEFLAG_REL_OWNER;
434 styleflags |= DNS_STYLEFLAG_NO_TTL;
436 styleflags |= DNS_STYLEFLAG_NO_CLASS;
438 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
439 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
440 styleflags |= DNS_STYLEFLAG_REL_DATA;
441 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
442 styleflags |= DNS_STYLEFLAG_TTL;
443 styleflags |= DNS_STYLEFLAG_MULTILINE;
444 styleflags |= DNS_STYLEFLAG_COMMENT;
446 if (multiline || (nottl && noclass))
447 result = dns_master_stylecreate(&style, styleflags,
448 24, 24, 24, 32, 80, 8, mctx);
449 else if (nottl || noclass)
450 result = dns_master_stylecreate(&style, styleflags,
451 24, 24, 32, 40, 80, 8, mctx);
453 result = dns_master_stylecreate(&style, styleflags,
454 24, 32, 40, 48, 80, 8, mctx);
455 check_result(result, "dns_master_stylecreate");
457 if (query->lookup->cmdline[0] != 0) {
459 fputs(query->lookup->cmdline, stdout);
460 query->lookup->cmdline[0]=0;
462 debug("printmessage(%s %s %s)", headers ? "headers" : "noheaders",
463 query->lookup->comments ? "comments" : "nocomments",
464 short_form ? "short_form" : "long_form");
468 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
469 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
471 if (!query->lookup->comments)
472 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
474 result = ISC_R_SUCCESS;
476 result = isc_buffer_allocate(mctx, &buf, len);
477 check_result(result, "isc_buffer_allocate");
479 if (query->lookup->comments && !short_form) {
480 if (query->lookup->cmdline[0] != 0)
481 printf("; %s\n", query->lookup->cmdline);
482 if (msg == query->lookup->sendmsg)
483 printf(";; Sending:\n");
485 printf(";; Got answer:\n");
488 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
490 opcodetext[msg->opcode],
491 rcode_totext(msg->rcode),
494 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
496 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
498 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
500 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
502 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
504 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
506 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
509 printf("; QUERY: %u, ANSWER: %u, "
510 "AUTHORITY: %u, ADDITIONAL: %u\n",
511 msg->counts[DNS_SECTION_QUESTION],
512 msg->counts[DNS_SECTION_ANSWER],
513 msg->counts[DNS_SECTION_AUTHORITY],
514 msg->counts[DNS_SECTION_ADDITIONAL]);
516 if (msg != query->lookup->sendmsg &&
517 (msg->flags & DNS_MESSAGEFLAG_RD) != 0 &&
518 (msg->flags & DNS_MESSAGEFLAG_RA) == 0)
519 printf(";; WARNING: recursion requested "
520 "but not available\n");
522 if (msg != query->lookup->sendmsg && extrabytes != 0U)
523 printf(";; WARNING: Messages has %u extra byte%s at "
524 "end\n", extrabytes, extrabytes != 0 ? "s" : "");
529 if (query->lookup->comments && headers && !short_form) {
530 result = dns_message_pseudosectiontotext(msg,
531 DNS_PSEUDOSECTION_OPT,
533 if (result == ISC_R_NOSPACE) {
536 isc_buffer_free(&buf);
537 result = isc_buffer_allocate(mctx, &buf, len);
538 if (result == ISC_R_SUCCESS)
539 goto repopulate_buffer;
544 "dns_message_pseudosectiontotext");
547 if (query->lookup->section_question && headers) {
549 result = dns_message_sectiontotext(msg,
550 DNS_SECTION_QUESTION,
552 if (result == ISC_R_NOSPACE)
554 check_result(result, "dns_message_sectiontotext");
557 if (query->lookup->section_answer) {
559 result = dns_message_sectiontotext(msg,
562 if (result == ISC_R_NOSPACE)
564 check_result(result, "dns_message_sectiontotext");
566 result = short_answer(msg, flags, buf, query);
567 if (result == ISC_R_NOSPACE)
569 check_result(result, "short_answer");
572 if (query->lookup->section_authority) {
574 result = dns_message_sectiontotext(msg,
575 DNS_SECTION_AUTHORITY,
577 if (result == ISC_R_NOSPACE)
579 check_result(result, "dns_message_sectiontotext");
582 if (query->lookup->section_additional) {
584 result = dns_message_sectiontotext(msg,
585 DNS_SECTION_ADDITIONAL,
587 if (result == ISC_R_NOSPACE)
589 check_result(result, "dns_message_sectiontotext");
591 * Only print the signature on the first record.
594 result = dns_message_pseudosectiontotext(
596 DNS_PSEUDOSECTION_TSIG,
598 if (result == ISC_R_NOSPACE)
601 "dns_message_pseudosectiontotext");
602 result = dns_message_pseudosectiontotext(
604 DNS_PSEUDOSECTION_SIG0,
606 if (result == ISC_R_NOSPACE)
609 "dns_message_pseudosectiontotext");
614 if (headers && query->lookup->comments && !short_form)
617 printf("%.*s", (int)isc_buffer_usedlength(buf),
618 (char *)isc_buffer_base(buf));
619 isc_buffer_free(&buf);
623 dns_master_styledestroy(&style, mctx);
628 * print the greeting message when the program first starts up.
631 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
634 static isc_boolean_t first = ISC_TRUE;
638 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
639 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
640 "%s; <<>> DiG " VERSION " <<>>",
644 snprintf(append, sizeof(append), " %s", argv[i++]);
645 remaining = sizeof(lookup->cmdline) -
646 strlen(lookup->cmdline) - 1;
647 strncat(lookup->cmdline, append, remaining);
649 remaining = sizeof(lookup->cmdline) -
650 strlen(lookup->cmdline) - 1;
651 strncat(lookup->cmdline, "\n", remaining);
652 if (first && addresscount != 0) {
653 snprintf(append, sizeof(append),
654 "; (%d server%s found)\n",
656 addresscount > 1 ? "s" : "");
657 remaining = sizeof(lookup->cmdline) -
658 strlen(lookup->cmdline) - 1;
659 strncat(lookup->cmdline, append, remaining);
662 snprintf(append, sizeof(append),
663 ";; global options:%s%s\n",
664 short_form ? " +short" : "",
665 printcmd ? " +cmd" : "");
667 remaining = sizeof(lookup->cmdline) -
668 strlen(lookup->cmdline) - 1;
669 strncat(lookup->cmdline, append, remaining);
675 parse_uint(char *arg, const char *desc, isc_uint32_t max) {
679 result = isc_parse_uint32(&tmp, arg, 10);
680 if (result == ISC_R_SUCCESS && tmp > max)
681 result = ISC_R_RANGE;
682 if (result != ISC_R_SUCCESS)
683 fatal("%s '%s': %s", desc, arg, isc_result_totext(result));
688 * We're not using isc_commandline_parse() here since the command line
689 * syntax of dig is quite a bit different from that which can be described
695 plus_option(char *option, isc_boolean_t is_batchfile,
696 dig_lookup_t *lookup)
698 char option_store[256];
699 char *cmd, *value, *ptr;
700 isc_boolean_t state = ISC_TRUE;
705 strncpy(option_store, option, sizeof(option_store));
706 option_store[sizeof(option_store)-1]=0;
708 cmd = next_token(&ptr,"=");
710 printf(";; Invalid option %s\n", option_store);
714 if (strncasecmp(cmd, "no", 2)==0) {
719 #define FULLCHECK(A) \
721 size_t _l = strlen(cmd); \
722 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
723 goto invalid_option; \
725 #define FULLCHECK2(A, B) \
727 size_t _l = strlen(cmd); \
728 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
729 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
730 goto invalid_option; \
736 case 'a': /* aaonly / aaflag */
737 FULLCHECK2("aaonly", "aaflag");
738 lookup->aaonly = state;
742 case 'd': /* additional */
743 FULLCHECK("additional");
744 lookup->section_additional = state;
746 case 'f': /* 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 lookup->udpsize = (isc_uint16_t) parse_uint(value,
789 "buffer size", COMMSIZE);
797 case 'd':/* cdflag */
799 lookup->cdflag = state;
803 noclass = ISC_TF(!state);
809 case 'o': /* comments */
810 FULLCHECK("comments");
811 lookup->comments = state;
812 if (lookup == default_lookup)
821 case 'e': /* defname */
822 FULLCHECK("defname");
823 if (!lookup->trace) {
827 case 'n': /* dnssec */
829 if (state && lookup->edns == -1)
831 lookup->dnssec = state;
833 case 'o': /* domain */
839 strncpy(domainopt, value, sizeof(domainopt));
840 domainopt[sizeof(domainopt)-1] = '\0';
854 lookup->edns = (isc_int16_t) parse_uint(value, "edns", 255);
858 lookup->servfail_stops = state;
862 case 'd': /* identify */
863 FULLCHECK("identify");
864 lookup->identify = state;
866 case 'g': /* ignore */
867 default: /* Inherits default for compatibility */
869 lookup->ignore = ISC_TRUE;
872 case 'm': /* multiline */
873 FULLCHECK("multiline");
878 case 'd': /* ndots */
884 ndots = parse_uint(value, "ndots", MAXNDOTS);
890 if (state && lookup->edns == -1)
892 lookup->nsid = state;
894 case 's': /* nssearch */
895 FULLCHECK("nssearch");
896 lookup->ns_search_only = state;
898 lookup->trace_root = ISC_TRUE;
899 lookup->recurse = ISC_TRUE;
900 lookup->identify = ISC_TRUE;
901 lookup->stats = ISC_FALSE;
902 lookup->comments = ISC_FALSE;
903 lookup->section_additional = ISC_FALSE;
904 lookup->section_authority = ISC_FALSE;
905 lookup->section_question = ISC_FALSE;
906 lookup->rdtype = dns_rdatatype_ns;
907 lookup->rdtypeset = ISC_TRUE;
908 short_form = ISC_TRUE;
925 case 'u': /* question */
926 FULLCHECK("question");
927 lookup->section_question = state;
928 if (lookup == default_lookup)
939 case 'c': /* recurse */
940 FULLCHECK("recurse");
941 lookup->recurse = state;
943 case 't': /* retry / retries */
944 FULLCHECK2("retry", "retries");
949 lookup->retries = parse_uint(value, "retries",
963 case 'e': /* search */
965 if (!lookup->trace) {
973 case 'r': /* short */
977 printcmd = ISC_FALSE;
978 lookup->section_additional = ISC_FALSE;
979 lookup->section_answer = ISC_TRUE;
980 lookup->section_authority = ISC_FALSE;
981 lookup->section_question = ISC_FALSE;
982 lookup->comments = ISC_FALSE;
983 lookup->stats = ISC_FALSE;
986 case 'w': /* showsearch */
987 FULLCHECK("showsearch");
988 if (!lookup->trace) {
998 case 'i': /* sigchase */
999 FULLCHECK("sigchase");
1000 lookup->sigchase = state;
1001 if (lookup->sigchase)
1002 lookup->dnssec = ISC_TRUE;
1005 case 't': /* stats */
1007 lookup->stats = state;
1010 goto invalid_option;
1018 lookup->tcp_mode = state;
1020 case 'i': /* timeout */
1021 FULLCHECK("timeout");
1025 goto invalid_option;
1026 timeout = parse_uint(value, "timeout", MAXTIMEOUT);
1031 case 'o': /* topdown */
1032 FULLCHECK("topdown");
1033 lookup->do_topdown = state;
1038 case 'a': /* trace */
1040 lookup->trace = state;
1041 lookup->trace_root = state;
1043 lookup->recurse = ISC_FALSE;
1044 lookup->identify = ISC_TRUE;
1045 lookup->comments = ISC_FALSE;
1046 lookup->stats = ISC_FALSE;
1047 lookup->section_additional = ISC_FALSE;
1048 lookup->section_authority = ISC_TRUE;
1049 lookup->section_question = ISC_FALSE;
1050 usesearch = ISC_FALSE;
1053 case 'i': /* tries */
1058 goto invalid_option;
1059 lookup->retries = parse_uint(value, "tries",
1061 if (lookup->retries == 0)
1062 lookup->retries = 1;
1065 case 'u': /* trusted-key */
1066 FULLCHECK("trusted-key");
1070 goto invalid_option;
1071 n = strlcpy(trustedkey, ptr,
1072 sizeof(trustedkey));
1073 if (n >= sizeof(trustedkey))
1074 fatal("trusted key too large");
1078 goto invalid_option;
1081 case 't': /* ttlid */
1083 nottl = ISC_TF(!state);
1086 goto invalid_option;
1092 lookup->tcp_mode = state;
1097 fprintf(stderr, "Invalid option: +%s\n",
1105 * #ISC_TRUE returned if value was used
1107 static const char *single_dash_opts = "46dhimnv";
1108 static const char *dash_opts = "46bcdfhikmnptvyx";
1109 static isc_boolean_t
1110 dash_option(char *option, char *next, dig_lookup_t **lookup,
1111 isc_boolean_t *open_type_class, isc_boolean_t *need_clone,
1112 isc_boolean_t config_only, int argc, char **argv,
1113 isc_boolean_t *firstarg)
1115 char opt, *value, *ptr, *ptr2, *ptr3;
1116 isc_result_t result;
1117 isc_boolean_t value_from_next;
1118 isc_textregion_t tr;
1119 dns_rdatatype_t rdtype;
1120 dns_rdataclass_t rdclass;
1121 char textname[MXNAME];
1123 struct in6_addr in6;
1127 while (strpbrk(option, single_dash_opts) == &option[0]) {
1129 * Since the -[46dhimnv] options do not take an argument,
1130 * account for them (in any number and/or combination)
1131 * if they appear as the first character(s) of a q-opt.
1137 isc_net_disableipv6();
1138 have_ipv6 = ISC_FALSE;
1140 fatal("can't find IPv4 networking");
1146 isc_net_disableipv4();
1147 have_ipv4 = ISC_FALSE;
1149 fatal("can't find IPv6 networking");
1154 ptr = strpbrk(&option[1], dash_opts);
1155 if (ptr != &option[1]) {
1158 debugging = ISC_TRUE;
1161 debugging = ISC_TRUE;
1170 case 'm': /* memdebug */
1171 /* memdebug is handled in preparse_args() */
1181 if (strlen(option) > 1U)
1182 option = &option[1];
1187 if (strlen(option) > 1U) {
1188 value_from_next = ISC_FALSE;
1191 value_from_next = ISC_TRUE;
1195 goto invalid_option;
1198 hash = strchr(value, '#');
1200 srcport = (in_port_t)
1201 parse_uint(hash + 1,
1202 "port number", MAXPORT);
1206 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1207 isc_sockaddr_fromin6(&bind_address, &in6, srcport);
1208 isc_net_disableipv4();
1209 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1210 isc_sockaddr_fromin(&bind_address, &in4, srcport);
1211 isc_net_disableipv6();
1215 fatal("invalid address %s", value);
1219 specified_source = ISC_TRUE;
1220 return (value_from_next);
1222 if ((*lookup)->rdclassset) {
1223 fprintf(stderr, ";; Warning, extra class option\n");
1225 *open_type_class = ISC_FALSE;
1227 tr.length = strlen(value);
1228 result = dns_rdataclass_fromtext(&rdclass,
1229 (isc_textregion_t *)&tr);
1230 if (result == ISC_R_SUCCESS) {
1231 (*lookup)->rdclass = rdclass;
1232 (*lookup)->rdclassset = ISC_TRUE;
1234 fprintf(stderr, ";; Warning, ignoring "
1235 "invalid class %s\n",
1237 return (value_from_next);
1240 return (value_from_next);
1242 strncpy(keyfile, value, sizeof(keyfile));
1243 keyfile[sizeof(keyfile)-1]=0;
1244 return (value_from_next);
1246 port = (in_port_t) parse_uint(value, "port number", MAXPORT);
1247 return (value_from_next);
1251 (*lookup) = clone_lookup(default_lookup,
1253 *need_clone = ISC_TRUE;
1254 strncpy((*lookup)->textname, value,
1255 sizeof((*lookup)->textname));
1256 (*lookup)->textname[sizeof((*lookup)->textname)-1]=0;
1257 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1258 (*lookup)->ns_search_only);
1259 (*lookup)->new_search = ISC_TRUE;
1261 printgreeting(argc, argv, *lookup);
1262 *firstarg = ISC_FALSE;
1264 ISC_LIST_APPEND(lookup_list, (*lookup), link);
1265 debug("looking up %s", (*lookup)->textname);
1267 return (value_from_next);
1269 *open_type_class = ISC_FALSE;
1270 if (strncasecmp(value, "ixfr=", 5) == 0) {
1271 rdtype = dns_rdatatype_ixfr;
1272 result = ISC_R_SUCCESS;
1275 tr.length = strlen(value);
1276 result = dns_rdatatype_fromtext(&rdtype,
1277 (isc_textregion_t *)&tr);
1278 if (result == ISC_R_SUCCESS &&
1279 rdtype == dns_rdatatype_ixfr) {
1280 result = DNS_R_UNKNOWN;
1283 if (result == ISC_R_SUCCESS) {
1284 if ((*lookup)->rdtypeset) {
1285 fprintf(stderr, ";; Warning, "
1286 "extra type option\n");
1288 if (rdtype == dns_rdatatype_ixfr) {
1289 (*lookup)->rdtype = dns_rdatatype_ixfr;
1290 (*lookup)->rdtypeset = ISC_TRUE;
1291 (*lookup)->ixfr_serial =
1292 parse_uint(&value[5], "serial number",
1294 (*lookup)->section_question = plusquest;
1295 (*lookup)->comments = pluscomm;
1296 (*lookup)->tcp_mode = ISC_TRUE;
1298 (*lookup)->rdtype = rdtype;
1299 (*lookup)->rdtypeset = ISC_TRUE;
1300 if (rdtype == dns_rdatatype_axfr) {
1301 (*lookup)->section_question = plusquest;
1302 (*lookup)->comments = pluscomm;
1304 (*lookup)->ixfr_serial = ISC_FALSE;
1307 fprintf(stderr, ";; Warning, ignoring "
1308 "invalid type %s\n",
1310 return (value_from_next);
1312 ptr = next_token(&value,":"); /* hmac type or name */
1316 ptr2 = next_token(&value, ":"); /* name or secret */
1319 ptr3 = next_token(&value,":"); /* secret or NULL */
1321 if (strcasecmp(ptr, "hmac-md5") == 0) {
1322 hmacname = DNS_TSIG_HMACMD5_NAME;
1324 } else if (strncasecmp(ptr, "hmac-md5-", 9) == 0) {
1325 hmacname = DNS_TSIG_HMACMD5_NAME;
1326 digestbits = parse_uint(&ptr[9],
1327 "digest-bits [0..128]",
1329 digestbits = (digestbits + 7) & ~0x7U;
1330 } else if (strcasecmp(ptr, "hmac-sha1") == 0) {
1331 hmacname = DNS_TSIG_HMACSHA1_NAME;
1333 } else if (strncasecmp(ptr, "hmac-sha1-", 10) == 0) {
1334 hmacname = DNS_TSIG_HMACSHA1_NAME;
1335 digestbits = parse_uint(&ptr[10],
1336 "digest-bits [0..160]",
1338 digestbits = (digestbits + 7) & ~0x7U;
1339 } else if (strcasecmp(ptr, "hmac-sha224") == 0) {
1340 hmacname = DNS_TSIG_HMACSHA224_NAME;
1342 } else if (strncasecmp(ptr, "hmac-sha224-", 12) == 0) {
1343 hmacname = DNS_TSIG_HMACSHA224_NAME;
1344 digestbits = parse_uint(&ptr[12],
1345 "digest-bits [0..224]",
1347 digestbits = (digestbits + 7) & ~0x7U;
1348 } else if (strcasecmp(ptr, "hmac-sha256") == 0) {
1349 hmacname = DNS_TSIG_HMACSHA256_NAME;
1351 } else if (strncasecmp(ptr, "hmac-sha256-", 12) == 0) {
1352 hmacname = DNS_TSIG_HMACSHA256_NAME;
1353 digestbits = parse_uint(&ptr[12],
1354 "digest-bits [0..256]",
1356 digestbits = (digestbits + 7) & ~0x7U;
1357 } else if (strcasecmp(ptr, "hmac-sha384") == 0) {
1358 hmacname = DNS_TSIG_HMACSHA384_NAME;
1360 } else if (strncasecmp(ptr, "hmac-sha384-", 12) == 0) {
1361 hmacname = DNS_TSIG_HMACSHA384_NAME;
1362 digestbits = parse_uint(&ptr[12],
1363 "digest-bits [0..384]",
1365 digestbits = (digestbits + 7) & ~0x7U;
1366 } else if (strcasecmp(ptr, "hmac-sha512") == 0) {
1367 hmacname = DNS_TSIG_HMACSHA512_NAME;
1369 } else if (strncasecmp(ptr, "hmac-sha512-", 12) == 0) {
1370 hmacname = DNS_TSIG_HMACSHA512_NAME;
1371 digestbits = parse_uint(&ptr[12],
1372 "digest-bits [0..512]",
1374 digestbits = (digestbits + 7) & ~0x7U;
1376 fprintf(stderr, ";; Warning, ignoring "
1377 "invalid TSIG algorithm %s\n", ptr);
1378 return (value_from_next);
1383 hmacname = DNS_TSIG_HMACMD5_NAME;
1386 strncpy(keynametext, ptr, sizeof(keynametext));
1387 keynametext[sizeof(keynametext)-1]=0;
1388 strncpy(keysecret, ptr2, sizeof(keysecret));
1389 keysecret[sizeof(keysecret)-1]=0;
1390 return (value_from_next);
1393 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1394 *need_clone = ISC_TRUE;
1395 if (get_reverse(textname, sizeof(textname), value,
1396 ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
1397 strncpy((*lookup)->textname, textname,
1398 sizeof((*lookup)->textname));
1399 debug("looking up %s", (*lookup)->textname);
1400 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1401 (*lookup)->ns_search_only);
1402 (*lookup)->ip6_int = ip6_int;
1403 if (!(*lookup)->rdtypeset)
1404 (*lookup)->rdtype = dns_rdatatype_ptr;
1405 if (!(*lookup)->rdclassset)
1406 (*lookup)->rdclass = dns_rdataclass_in;
1407 (*lookup)->new_search = ISC_TRUE;
1409 printgreeting(argc, argv, *lookup);
1410 *firstarg = ISC_FALSE;
1412 ISC_LIST_APPEND(lookup_list, *lookup, link);
1414 fprintf(stderr, "Invalid IP address %s\n", value);
1417 return (value_from_next);
1420 fprintf(stderr, "Invalid option: -%s\n", option);
1427 * Because we may be trying to do memory allocation recording, we're going
1428 * to need to parse the arguments for the -m *before* we start the main
1429 * argument parsing routine.
1431 * I'd prefer not to have to do this, but I am not quite sure how else to
1432 * fix the problem. Argument parsing in dig involves memory allocation
1433 * by its nature, so it can't be done in the main argument parser.
1436 preparse_args(int argc, char **argv) {
1443 for (rc--, rv++; rc > 0; rc--, rv++) {
1444 if (rv[0][0] != '-')
1447 while (strpbrk(option, single_dash_opts) == &option[0]) {
1448 if (option[0] == 'm') {
1449 memdebugging = ISC_TRUE;
1450 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1451 ISC_MEM_DEBUGRECORD;
1454 option = &option[1];
1460 getaddresses(dig_lookup_t *lookup, const char *host) {
1461 isc_result_t result;
1462 isc_sockaddr_t sockaddrs[DIG_MAX_ADDRESSES];
1463 isc_netaddr_t netaddr;
1466 char tmp[ISC_NETADDR_FORMATSIZE];
1468 result = bind9_getaddresses(host, 0, sockaddrs,
1469 DIG_MAX_ADDRESSES, &count);
1470 if (result != ISC_R_SUCCESS)
1471 fatal("couldn't get address for '%s': %s",
1472 host, isc_result_totext(result));
1474 for (i = 0; i < count; i++) {
1475 isc_netaddr_fromsockaddr(&netaddr, &sockaddrs[i]);
1476 isc_netaddr_format(&netaddr, tmp, sizeof(tmp));
1477 srv = make_server(tmp, host);
1478 ISC_LIST_APPEND(lookup->my_server_list, srv, link);
1480 addresscount = count;
1484 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1485 int argc, char **argv) {
1486 isc_result_t result;
1487 isc_textregion_t tr;
1488 isc_boolean_t firstarg = ISC_TRUE;
1489 dig_lookup_t *lookup = NULL;
1490 dns_rdatatype_t rdtype;
1491 dns_rdataclass_t rdclass;
1492 isc_boolean_t open_type_class = ISC_TRUE;
1493 char batchline[MXNAME];
1504 isc_boolean_t need_clone = ISC_TRUE;
1507 * The semantics for parsing the args is a bit complex; if
1508 * we don't have a host yet, make the arg apply globally,
1509 * otherwise make it apply to the latest host. This is
1510 * a bit different than the previous versions, but should
1511 * form a consistent user interface.
1513 * First, create a "default lookup" which won't actually be used
1514 * anywhere, except for cloning into new lookups
1517 debug("parse_args()");
1518 if (!is_batchfile) {
1519 debug("making new lookup");
1520 default_lookup = make_empty_lookup();
1524 * Treat ${HOME}/.digrc as a special batchfile
1526 INSIST(batchfp == NULL);
1527 homedir = getenv("HOME");
1528 if (homedir != NULL) {
1530 n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc",
1532 if (n < sizeof(rcfile))
1533 batchfp = fopen(rcfile, "r");
1535 if (batchfp != NULL) {
1536 while (fgets(batchline, sizeof(batchline),
1538 debug("config line %s", batchline);
1541 bargv[bargc] = next_token(&input, " \t\r\n");
1542 while ((bargv[bargc] != NULL) &&
1546 next_token(&input, " \t\r\n");
1552 for(i = 0; i < bargc; i++)
1553 debug(".digrc argv %d: %s",
1555 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1563 if (is_batchfile && !config_only) {
1564 /* Processing '-f batchfile'. */
1565 lookup = clone_lookup(default_lookup, ISC_TRUE);
1566 need_clone = ISC_FALSE;
1568 lookup = default_lookup;
1572 for (rc--, rv++; rc > 0; rc--, rv++) {
1573 debug("main parsing %s", rv[0]);
1574 if (strncmp(rv[0], "%", 1) == 0)
1576 if (strncmp(rv[0], "@", 1) == 0) {
1577 getaddresses(lookup, &rv[0][1]);
1578 } else if (rv[0][0] == '+') {
1579 plus_option(&rv[0][1], is_batchfile,
1581 } else if (rv[0][0] == '-') {
1583 if (dash_option(&rv[0][1], NULL,
1584 &lookup, &open_type_class,
1585 &need_clone, config_only,
1586 argc, argv, &firstarg)) {
1591 if (dash_option(&rv[0][1], rv[1],
1592 &lookup, &open_type_class,
1593 &need_clone, config_only,
1594 argc, argv, &firstarg)) {
1601 * Anything which isn't an option
1603 if (open_type_class) {
1604 if (strncasecmp(rv[0], "ixfr=", 5) == 0) {
1605 rdtype = dns_rdatatype_ixfr;
1606 result = ISC_R_SUCCESS;
1609 tr.length = strlen(rv[0]);
1610 result = dns_rdatatype_fromtext(&rdtype,
1611 (isc_textregion_t *)&tr);
1612 if (result == ISC_R_SUCCESS &&
1613 rdtype == dns_rdatatype_ixfr) {
1614 result = DNS_R_UNKNOWN;
1615 fprintf(stderr, ";; Warning, "
1621 if (result == ISC_R_SUCCESS) {
1622 if (lookup->rdtypeset) {
1623 fprintf(stderr, ";; Warning, "
1624 "extra type option\n");
1626 if (rdtype == dns_rdatatype_ixfr) {
1629 lookup->rdtypeset = ISC_TRUE;
1630 lookup->ixfr_serial =
1631 parse_uint(&rv[0][5],
1634 lookup->section_question =
1636 lookup->comments = pluscomm;
1637 lookup->tcp_mode = ISC_TRUE;
1639 lookup->rdtype = rdtype;
1640 lookup->rdtypeset = ISC_TRUE;
1642 dns_rdatatype_axfr) {
1643 lookup->section_question =
1645 lookup->comments = pluscomm;
1647 lookup->ixfr_serial = ISC_FALSE;
1651 result = dns_rdataclass_fromtext(&rdclass,
1652 (isc_textregion_t *)&tr);
1653 if (result == ISC_R_SUCCESS) {
1654 if (lookup->rdclassset) {
1655 fprintf(stderr, ";; Warning, "
1656 "extra class option\n");
1658 lookup->rdclass = rdclass;
1659 lookup->rdclassset = ISC_TRUE;
1666 lookup = clone_lookup(default_lookup,
1668 need_clone = ISC_TRUE;
1669 strncpy(lookup->textname, rv[0],
1670 sizeof(lookup->textname));
1671 lookup->textname[sizeof(lookup->textname)-1]=0;
1672 lookup->trace_root = ISC_TF(lookup->trace ||
1673 lookup->ns_search_only);
1674 lookup->new_search = ISC_TRUE;
1676 printgreeting(argc, argv, lookup);
1677 firstarg = ISC_FALSE;
1679 ISC_LIST_APPEND(lookup_list, lookup, link);
1680 debug("looking up %s", lookup->textname);
1682 /* XXX Error message */
1687 * If we have a batchfile, seed the lookup list with the
1688 * first entry, then trust the callback in dighost_shutdown
1691 if ((batchname != NULL) && !(is_batchfile)) {
1692 if (strcmp(batchname, "-") == 0)
1695 batchfp = fopen(batchname, "r");
1696 if (batchfp == NULL) {
1700 fatal("couldn't open specified batch file");
1702 /* XXX Remove code dup from shutdown code */
1704 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1706 debug("batch line %s", batchline);
1707 if (batchline[0] == '\r' || batchline[0] == '\n'
1708 || batchline[0] == '#' || batchline[0] == ';')
1711 bargv[bargc] = next_token(&input, " \t\r\n");
1712 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1714 bargv[bargc] = next_token(&input, " \t\r\n");
1720 for(i = 0; i < bargc; i++)
1721 debug("batch argv %d: %s", i, bargv[i]);
1722 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1728 * If no lookup specified, search for root
1730 if ((lookup_list.head == NULL) && !config_only) {
1732 lookup = clone_lookup(default_lookup, ISC_TRUE);
1733 need_clone = ISC_TRUE;
1734 lookup->trace_root = ISC_TF(lookup->trace ||
1735 lookup->ns_search_only);
1736 lookup->new_search = ISC_TRUE;
1737 strcpy(lookup->textname, ".");
1738 lookup->rdtype = dns_rdatatype_ns;
1739 lookup->rdtypeset = ISC_TRUE;
1741 printgreeting(argc, argv, lookup);
1742 firstarg = ISC_FALSE;
1744 ISC_LIST_APPEND(lookup_list, lookup, link);
1747 destroy_lookup(lookup);
1751 * Callback from dighost.c to allow program-specific shutdown code.
1752 * Here, we're possibly reading from a batch file, then shutting down
1753 * for real if there's nothing in the batch file to read.
1756 dighost_shutdown(void) {
1757 char batchline[MXNAME];
1763 if (batchname == NULL) {
1769 if (feof(batchfp)) {
1772 if (batchfp != stdin)
1777 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1778 debug("batch line %s", batchline);
1781 bargv[bargc] = next_token(&input, " \t\r\n");
1782 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1784 bargv[bargc] = next_token(&input, " \t\r\n");
1789 for(i = 0; i < bargc; i++)
1790 debug("batch argv %d: %s", i, bargv[i]);
1791 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1795 if (batchfp != stdin)
1802 /*% Main processing routine for dig */
1804 main(int argc, char **argv) {
1805 isc_result_t result;
1807 ISC_LIST_INIT(lookup_list);
1808 ISC_LIST_INIT(server_list);
1809 ISC_LIST_INIT(search_list);
1812 preparse_args(argc, argv);
1814 result = isc_app_start();
1815 check_result(result, "isc_app_start");
1817 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1819 if (domainopt[0] != '\0') {
1820 set_search_domain(domainopt);
1821 usesearch = ISC_TRUE;
1823 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1824 check_result(result, "isc_app_onrun");
1826 destroy_lookup(default_lookup);
1827 if (batchname != NULL) {
1828 if (batchfp != stdin)