2 * Copyright (C) 2004-2007 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.186.18.29 2007/08/28 07:19:55 tbox 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 *opcodetext[] = {
93 /*% return code text */
94 static const char *rcodetext[] = {
116 print_usage(FILE *fp) {
118 "Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}\n"
119 " {global-d-opt} host [@local-server] {local-d-opt}\n"
120 " [ host [@local-server] {local-d-opt} [...]]\n", fp);
126 fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
127 "for complete list of options\n", stderr);
134 fputs("DiG " VERSION "\n", stderr);
142 "Where: domain is in the Domain Name System\n"
143 " q-class is one of (in,hs,ch,...) [default: in]\n"
144 " q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
145 " (Use ixfr=version for type ixfr)\n"
146 " q-opt is one of:\n"
147 " -x dot-notation (shortcut for in-addr lookups)\n"
148 " -i (IP6.INT reverse IPv6 lookups)\n"
149 " -f filename (batch mode)\n"
150 " -b address[#port] (bind to source address/port)\n"
151 " -p port (specify port number)\n"
152 " -q name (specify query name)\n"
153 " -t type (specify query type)\n"
154 " -c class (specify query class)\n"
155 " -k keyfile (specify tsig key file)\n"
156 " -y [hmac:]name:key (specify named base64 tsig key)\n"
157 " -4 (use IPv4 query transport only)\n"
158 " -6 (use IPv6 query transport only)\n"
159 " d-opt is of the form +keyword[=value], where keyword is:\n"
160 " +[no]vc (TCP mode)\n"
161 " +[no]tcp (TCP mode, alternate syntax)\n"
162 " +time=### (Set query timeout) [5]\n"
163 " +tries=### (Set number of UDP attempts) [3]\n"
164 " +retry=### (Set number of UDP retries) [2]\n"
165 " +domain=### (Set default domainname)\n"
166 " +bufsize=### (Set EDNS0 Max UDP packet size)\n"
167 " +ndots=### (Set NDOTS value)\n"
168 " +edns=### (Set EDNS version)\n"
169 " +[no]search (Set whether to use searchlist)\n"
170 " +[no]showsearch (Search with intermediate results)\n"
171 " +[no]defname (Ditto)\n"
172 " +[no]recurse (Recursive mode)\n"
173 " +[no]ignore (Don't revert to TCP for TC responses.)"
175 " +[no]fail (Don't try next server on SERVFAIL)\n"
176 " +[no]besteffort (Try to parse even illegal messages)\n"
177 " +[no]aaonly (Set AA flag in query (+[no]aaflag))\n"
178 " +[no]adflag (Set AD flag in query)\n"
179 " +[no]cdflag (Set CD flag in query)\n"
180 " +[no]cl (Control display of class in records)\n"
181 " +[no]cmd (Control display of command line)\n"
182 " +[no]comments (Control display of comment lines)\n"
183 " +[no]question (Control display of question)\n"
184 " +[no]answer (Control display of answer)\n"
185 " +[no]authority (Control display of authority)\n"
186 " +[no]additional (Control display of additional)\n"
187 " +[no]stats (Control display of statistics)\n"
188 " +[no]short (Disable everything except short\n"
190 " +[no]ttlid (Control display of ttls in records)\n"
191 " +[no]all (Set or clear all display flags)\n"
192 " +[no]qr (Print question before sending)\n"
193 " +[no]nssearch (Search all authoritative nameservers)\n"
194 " +[no]identify (ID responders in short answers)\n"
195 " +[no]trace (Trace delegation down from root)\n"
196 " +[no]dnssec (Request DNSSEC records)\n"
198 " +[no]sigchase (Chase DNSSEC signatures)\n"
199 " +trusted-key=#### (Trusted Key when chasing DNSSEC sigs)\n"
201 " +[no]topdown (Do DNSSEC validation top down mode)\n"
204 " +[no]multiline (Print records in an expanded format)\n"
205 " global d-opts and servers (before host name) affect all queries.\n"
206 " local d-opts and servers (after host name) affect only that lookup.\n"
207 " -h (print help and exit)\n"
208 " -v (print version and exit)\n",
213 * Callback from dighost.c to print the received message.
216 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
220 char fromtext[ISC_SOCKADDR_FORMATSIZE];
222 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
226 if (query->lookup->stats && !short_form) {
227 diff = isc_time_microdiff(&now, &query->time_sent);
228 printf(";; Query time: %ld msec\n", (long int)diff/1000);
229 printf(";; SERVER: %s(%s)\n", fromtext, query->servname);
231 printf(";; WHEN: %s", ctime(&tnow));
232 if (query->lookup->doing_xfr) {
233 printf(";; XFR size: %u records (messages %u, "
234 "bytes %" ISC_PRINT_QUADFORMAT "u)\n",
235 query->rr_count, query->msg_count,
238 printf(";; MSG SIZE rcvd: %u\n", bytes);
243 puts(";; WARNING -- Some TSIG could not "
246 if ((key == NULL) && (keysecret[0] != 0)) {
247 puts(";; WARNING -- TSIG key was not used.");
250 } else if (query->lookup->identify && !short_form) {
251 diff = isc_time_microdiff(&now, &query->time_sent);
252 printf(";; Received %" ISC_PRINT_QUADFORMAT "u bytes "
253 "from %s(%s) in %d ms\n\n",
254 query->lookup->doing_xfr ?
255 query->byte_count : (isc_uint64_t)bytes,
256 fromtext, query->servname,
262 * Callback from dighost.c to print that it is trying a server.
267 trying(char *frm, dig_lookup_t *lookup) {
273 * Internal print routine used to print short form replies.
276 say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
280 char store[sizeof("12345678901234567890")];
282 if (query->lookup->trace || query->lookup->ns_search_only) {
283 result = dns_rdatatype_totext(rdata->type, buf);
284 if (result != ISC_R_SUCCESS)
286 ADD_STRING(buf, " ");
288 result = dns_rdata_totext(rdata, NULL, buf);
289 check_result(result, "dns_rdata_totext");
290 if (query->lookup->identify) {
292 diff = isc_time_microdiff(&now, &query->time_sent);
293 ADD_STRING(buf, " from server ");
294 ADD_STRING(buf, query->servname);
295 snprintf(store, 19, " in %d ms.", (int)diff/1000);
296 ADD_STRING(buf, store);
298 ADD_STRING(buf, "\n");
299 return (ISC_R_SUCCESS);
303 * short_form message print handler. Calls above say_message()
306 short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
307 isc_buffer_t *buf, dig_query_t *query)
310 dns_rdataset_t *rdataset;
312 isc_result_t result, loopresult;
313 dns_name_t empty_name;
315 dns_rdata_t rdata = DNS_RDATA_INIT;
319 dns_name_init(&empty_name, NULL);
320 result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
321 if (result == ISC_R_NOMORE)
322 return (ISC_R_SUCCESS);
323 else if (result != ISC_R_SUCCESS)
328 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
330 isc_buffer_init(&target, t, sizeof(t));
332 for (rdataset = ISC_LIST_HEAD(name->list);
334 rdataset = ISC_LIST_NEXT(rdataset, link)) {
335 loopresult = dns_rdataset_first(rdataset);
336 while (loopresult == ISC_R_SUCCESS) {
337 dns_rdataset_current(rdataset, &rdata);
338 result = say_message(&rdata, query,
340 check_result(result, "say_message");
341 loopresult = dns_rdataset_next(rdataset);
342 dns_rdata_reset(&rdata);
345 result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
346 if (result == ISC_R_NOMORE)
348 else if (result != ISC_R_SUCCESS)
352 return (ISC_R_SUCCESS);
356 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
357 isc_buffer_t *target)
360 dns_master_style_t *style = NULL;
361 unsigned int styleflags = 0;
363 if (rdataset == NULL || owner_name == NULL || target == NULL)
366 styleflags |= DNS_STYLEFLAG_REL_OWNER;
368 styleflags |= DNS_STYLEFLAG_NO_TTL;
370 styleflags |= DNS_STYLEFLAG_NO_CLASS;
372 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
373 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
374 styleflags |= DNS_STYLEFLAG_REL_DATA;
375 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
376 styleflags |= DNS_STYLEFLAG_TTL;
377 styleflags |= DNS_STYLEFLAG_MULTILINE;
378 styleflags |= DNS_STYLEFLAG_COMMENT;
380 if (multiline || (nottl && noclass))
381 result = dns_master_stylecreate(&style, styleflags,
382 24, 24, 24, 32, 80, 8, mctx);
383 else if (nottl || noclass)
384 result = dns_master_stylecreate(&style, styleflags,
385 24, 24, 32, 40, 80, 8, mctx);
387 result = dns_master_stylecreate(&style, styleflags,
388 24, 32, 40, 48, 80, 8, mctx);
389 check_result(result, "dns_master_stylecreate");
391 result = dns_master_rdatasettotext(owner_name, rdataset, style, target);
394 dns_master_styledestroy(&style, mctx);
401 * Callback from dighost.c to print the reply from a server
404 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
406 dns_messagetextflag_t flags;
407 isc_buffer_t *buf = NULL;
408 unsigned int len = OUTPUTBUF;
409 dns_master_style_t *style = NULL;
410 unsigned int styleflags = 0;
412 styleflags |= DNS_STYLEFLAG_REL_OWNER;
414 styleflags |= DNS_STYLEFLAG_NO_TTL;
416 styleflags |= DNS_STYLEFLAG_NO_CLASS;
418 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
419 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
420 styleflags |= DNS_STYLEFLAG_REL_DATA;
421 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
422 styleflags |= DNS_STYLEFLAG_TTL;
423 styleflags |= DNS_STYLEFLAG_MULTILINE;
424 styleflags |= DNS_STYLEFLAG_COMMENT;
426 if (multiline || (nottl && noclass))
427 result = dns_master_stylecreate(&style, styleflags,
428 24, 24, 24, 32, 80, 8, mctx);
429 else if (nottl || noclass)
430 result = dns_master_stylecreate(&style, styleflags,
431 24, 24, 32, 40, 80, 8, mctx);
433 result = dns_master_stylecreate(&style, styleflags,
434 24, 32, 40, 48, 80, 8, mctx);
435 check_result(result, "dns_master_stylecreate");
437 if (query->lookup->cmdline[0] != 0) {
439 fputs(query->lookup->cmdline, stdout);
440 query->lookup->cmdline[0]=0;
442 debug("printmessage(%s %s %s)", headers ? "headers" : "noheaders",
443 query->lookup->comments ? "comments" : "nocomments",
444 short_form ? "short_form" : "long_form");
448 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
449 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
451 if (!query->lookup->comments)
452 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
454 result = ISC_R_SUCCESS;
456 result = isc_buffer_allocate(mctx, &buf, len);
457 check_result(result, "isc_buffer_allocate");
459 if (query->lookup->comments && !short_form) {
460 if (query->lookup->cmdline[0] != 0)
461 printf("; %s\n", query->lookup->cmdline);
462 if (msg == query->lookup->sendmsg)
463 printf(";; Sending:\n");
465 printf(";; Got answer:\n");
468 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
470 opcodetext[msg->opcode], rcodetext[msg->rcode],
473 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
475 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
477 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
479 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
481 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
483 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
485 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
488 printf("; QUERY: %u, ANSWER: %u, "
489 "AUTHORITY: %u, ADDITIONAL: %u\n",
490 msg->counts[DNS_SECTION_QUESTION],
491 msg->counts[DNS_SECTION_ANSWER],
492 msg->counts[DNS_SECTION_AUTHORITY],
493 msg->counts[DNS_SECTION_ADDITIONAL]);
495 if (msg != query->lookup->sendmsg &&
496 (msg->flags & DNS_MESSAGEFLAG_RD) != 0 &&
497 (msg->flags & DNS_MESSAGEFLAG_RA) == 0)
498 printf(";; WARNING: recursion requested "
499 "but not available\n");
501 if (msg != query->lookup->sendmsg && extrabytes != 0U)
502 printf(";; WARNING: Messages has %u extra byte%s at "
503 "end\n", extrabytes, extrabytes != 0 ? "s" : "");
508 if (query->lookup->comments && headers && !short_form) {
509 result = dns_message_pseudosectiontotext(msg,
510 DNS_PSEUDOSECTION_OPT,
512 if (result == ISC_R_NOSPACE) {
515 isc_buffer_free(&buf);
516 result = isc_buffer_allocate(mctx, &buf, len);
517 if (result == ISC_R_SUCCESS)
518 goto repopulate_buffer;
523 "dns_message_pseudosectiontotext");
526 if (query->lookup->section_question && headers) {
528 result = dns_message_sectiontotext(msg,
529 DNS_SECTION_QUESTION,
531 if (result == ISC_R_NOSPACE)
533 check_result(result, "dns_message_sectiontotext");
536 if (query->lookup->section_answer) {
538 result = dns_message_sectiontotext(msg,
541 if (result == ISC_R_NOSPACE)
543 check_result(result, "dns_message_sectiontotext");
545 result = short_answer(msg, flags, buf, query);
546 if (result == ISC_R_NOSPACE)
548 check_result(result, "short_answer");
551 if (query->lookup->section_authority) {
553 result = dns_message_sectiontotext(msg,
554 DNS_SECTION_AUTHORITY,
556 if (result == ISC_R_NOSPACE)
558 check_result(result, "dns_message_sectiontotext");
561 if (query->lookup->section_additional) {
563 result = dns_message_sectiontotext(msg,
564 DNS_SECTION_ADDITIONAL,
566 if (result == ISC_R_NOSPACE)
568 check_result(result, "dns_message_sectiontotext");
570 * Only print the signature on the first record.
573 result = dns_message_pseudosectiontotext(
575 DNS_PSEUDOSECTION_TSIG,
577 if (result == ISC_R_NOSPACE)
580 "dns_message_pseudosectiontotext");
581 result = dns_message_pseudosectiontotext(
583 DNS_PSEUDOSECTION_SIG0,
585 if (result == ISC_R_NOSPACE)
588 "dns_message_pseudosectiontotext");
593 if (headers && query->lookup->comments && !short_form)
596 printf("%.*s", (int)isc_buffer_usedlength(buf),
597 (char *)isc_buffer_base(buf));
598 isc_buffer_free(&buf);
602 dns_master_styledestroy(&style, mctx);
607 * print the greeting message when the program first starts up.
610 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
613 static isc_boolean_t first = ISC_TRUE;
617 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
618 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
619 "%s; <<>> DiG " VERSION " <<>>",
623 snprintf(append, sizeof(append), " %s", argv[i++]);
624 remaining = sizeof(lookup->cmdline) -
625 strlen(lookup->cmdline) - 1;
626 strncat(lookup->cmdline, append, remaining);
628 remaining = sizeof(lookup->cmdline) -
629 strlen(lookup->cmdline) - 1;
630 strncat(lookup->cmdline, "\n", remaining);
631 if (first && addresscount != 0) {
632 snprintf(append, sizeof(append),
633 "; (%d server%s found)\n",
635 addresscount > 1 ? "s" : "");
636 remaining = sizeof(lookup->cmdline) -
637 strlen(lookup->cmdline) - 1;
638 strncat(lookup->cmdline, append, remaining);
641 snprintf(append, sizeof(append),
642 ";; global options: %s %s\n",
643 short_form ? "short_form" : "",
644 printcmd ? "printcmd" : "");
646 remaining = sizeof(lookup->cmdline) -
647 strlen(lookup->cmdline) - 1;
648 strncat(lookup->cmdline, append, remaining);
654 parse_uint(char *arg, const char *desc, isc_uint32_t max) {
658 result = isc_parse_uint32(&tmp, arg, 10);
659 if (result == ISC_R_SUCCESS && tmp > max)
660 result = ISC_R_RANGE;
661 if (result != ISC_R_SUCCESS)
662 fatal("%s '%s': %s", desc, arg, isc_result_totext(result));
667 * We're not using isc_commandline_parse() here since the command line
668 * syntax of dig is quite a bit different from that which can be described
674 plus_option(char *option, isc_boolean_t is_batchfile,
675 dig_lookup_t *lookup)
677 char option_store[256];
678 char *cmd, *value, *ptr;
679 isc_boolean_t state = ISC_TRUE;
684 strncpy(option_store, option, sizeof(option_store));
685 option_store[sizeof(option_store)-1]=0;
687 cmd = next_token(&ptr,"=");
689 printf(";; Invalid option %s\n", option_store);
693 if (strncasecmp(cmd, "no", 2)==0) {
698 #define FULLCHECK(A) \
700 size_t _l = strlen(cmd); \
701 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
702 goto invalid_option; \
704 #define FULLCHECK2(A, B) \
706 size_t _l = strlen(cmd); \
707 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
708 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
709 goto invalid_option; \
715 case 'a': /* aaonly / aaflag */
716 FULLCHECK2("aaonly", "aaflag");
717 lookup->aaonly = state;
721 case 'd': /* additional */
722 FULLCHECK("additional");
723 lookup->section_additional = state;
725 case 'f': /* adflag */
727 lookup->adflag = state;
735 lookup->section_question = state;
736 lookup->section_authority = state;
737 lookup->section_answer = state;
738 lookup->section_additional = state;
739 lookup->comments = state;
740 lookup->stats = state;
743 case 'n': /* answer */
745 lookup->section_answer = state;
747 case 'u': /* authority */
748 FULLCHECK("authority");
749 lookup->section_authority = state;
757 case 'e':/* besteffort */
758 FULLCHECK("besteffort");
759 lookup->besteffort = state;
761 case 'u':/* bufsize */
762 FULLCHECK("bufsize");
767 lookup->udpsize = (isc_uint16_t) parse_uint(value,
768 "buffer size", COMMSIZE);
776 case 'd':/* cdflag */
778 lookup->cdflag = state;
782 noclass = ISC_TF(!state);
788 case 'o': /* comments */
789 FULLCHECK("comments");
790 lookup->comments = state;
791 if (lookup == default_lookup)
800 case 'e': /* defname */
801 FULLCHECK("defname");
804 case 'n': /* dnssec */
806 if (state && lookup->edns == -1)
808 lookup->dnssec = state;
810 case 'o': /* domain */
816 strncpy(domainopt, value, sizeof(domainopt));
817 domainopt[sizeof(domainopt)-1] = '\0';
831 lookup->edns = (isc_int16_t) parse_uint(value, "edns", 255);
835 lookup->servfail_stops = state;
839 case 'd': /* identify */
840 FULLCHECK("identify");
841 lookup->identify = state;
843 case 'g': /* ignore */
844 default: /* Inherets default for compatibility */
846 lookup->ignore = ISC_TRUE;
849 case 'm': /* multiline */
850 FULLCHECK("multiline");
855 case 'd': /* ndots */
861 ndots = parse_uint(value, "ndots", MAXNDOTS);
863 case 's': /* nssearch */
864 FULLCHECK("nssearch");
865 lookup->ns_search_only = state;
867 lookup->trace_root = ISC_TRUE;
868 lookup->recurse = ISC_TRUE;
869 lookup->identify = ISC_TRUE;
870 lookup->stats = ISC_FALSE;
871 lookup->comments = ISC_FALSE;
872 lookup->section_additional = ISC_FALSE;
873 lookup->section_authority = ISC_FALSE;
874 lookup->section_question = ISC_FALSE;
875 lookup->rdtype = dns_rdatatype_ns;
876 lookup->rdtypeset = ISC_TRUE;
877 short_form = ISC_TRUE;
890 case 'u': /* question */
891 FULLCHECK("question");
892 lookup->section_question = state;
893 if (lookup == default_lookup)
904 case 'c': /* recurse */
905 FULLCHECK("recurse");
906 lookup->recurse = state;
908 case 't': /* retry / retries */
909 FULLCHECK2("retry", "retries");
914 lookup->retries = parse_uint(value, "retries",
928 case 'e': /* search */
936 case 'r': /* short */
940 printcmd = ISC_FALSE;
941 lookup->section_additional = ISC_FALSE;
942 lookup->section_answer = ISC_TRUE;
943 lookup->section_authority = ISC_FALSE;
944 lookup->section_question = ISC_FALSE;
945 lookup->comments = ISC_FALSE;
946 lookup->stats = ISC_FALSE;
949 case 'w': /* showsearch */
950 FULLCHECK("showsearch");
959 case 'i': /* sigchase */
960 FULLCHECK("sigchase");
961 lookup->sigchase = state;
962 if (lookup->sigchase)
963 lookup->dnssec = ISC_TRUE;
966 case 't': /* stats */
968 lookup->stats = state;
979 lookup->tcp_mode = state;
981 case 'i': /* timeout */
982 FULLCHECK("timeout");
987 timeout = parse_uint(value, "timeout", MAXTIMEOUT);
992 case 'o': /* topdown */
993 FULLCHECK("topdown");
994 lookup->do_topdown = state;
999 case 'a': /* trace */
1001 lookup->trace = state;
1002 lookup->trace_root = state;
1004 lookup->recurse = ISC_FALSE;
1005 lookup->identify = ISC_TRUE;
1006 lookup->comments = ISC_FALSE;
1007 lookup->stats = ISC_FALSE;
1008 lookup->section_additional = ISC_FALSE;
1009 lookup->section_authority = ISC_TRUE;
1010 lookup->section_question = ISC_FALSE;
1013 case 'i': /* tries */
1018 goto invalid_option;
1019 lookup->retries = parse_uint(value, "tries",
1021 if (lookup->retries == 0)
1022 lookup->retries = 1;
1025 case 'u': /* trusted-key */
1026 FULLCHECK("trusted-key");
1030 goto invalid_option;
1031 n = strlcpy(trustedkey, ptr,
1032 sizeof(trustedkey));
1033 if (n >= sizeof(trustedkey))
1034 fatal("trusted key too large");
1038 goto invalid_option;
1041 case 't': /* ttlid */
1043 nottl = ISC_TF(!state);
1046 goto invalid_option;
1052 lookup->tcp_mode = state;
1057 fprintf(stderr, "Invalid option: +%s\n",
1065 * #ISC_TRUE returned if value was used
1067 static const char *single_dash_opts = "46dhimnv";
1068 static const char *dash_opts = "46bcdfhikmnptvyx";
1069 static isc_boolean_t
1070 dash_option(char *option, char *next, dig_lookup_t **lookup,
1071 isc_boolean_t *open_type_class, isc_boolean_t *need_clone,
1072 isc_boolean_t config_only, int argc, char **argv,
1073 isc_boolean_t *firstarg)
1075 char opt, *value, *ptr, *ptr2, *ptr3;
1076 isc_result_t result;
1077 isc_boolean_t value_from_next;
1078 isc_textregion_t tr;
1079 dns_rdatatype_t rdtype;
1080 dns_rdataclass_t rdclass;
1081 char textname[MXNAME];
1083 struct in6_addr in6;
1087 while (strpbrk(option, single_dash_opts) == &option[0]) {
1089 * Since the -[46dhimnv] options do not take an argument,
1090 * account for them (in any number and/or combination)
1091 * if they appear as the first character(s) of a q-opt.
1097 isc_net_disableipv6();
1098 have_ipv6 = ISC_FALSE;
1100 fatal("can't find IPv4 networking");
1106 isc_net_disableipv4();
1107 have_ipv4 = ISC_FALSE;
1109 fatal("can't find IPv6 networking");
1114 ptr = strpbrk(&option[1], dash_opts);
1115 if (ptr != &option[1]) {
1118 debugging = ISC_TRUE;
1121 debugging = ISC_TRUE;
1130 case 'm': /* memdebug */
1131 /* memdebug is handled in preparse_args() */
1141 if (strlen(option) > 1U)
1142 option = &option[1];
1147 if (strlen(option) > 1U) {
1148 value_from_next = ISC_FALSE;
1151 value_from_next = ISC_TRUE;
1155 goto invalid_option;
1158 hash = strchr(value, '#');
1160 srcport = (in_port_t)
1161 parse_uint(hash + 1,
1162 "port number", MAXPORT);
1166 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1167 isc_sockaddr_fromin6(&bind_address, &in6, srcport);
1168 isc_net_disableipv4();
1169 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1170 isc_sockaddr_fromin(&bind_address, &in4, srcport);
1171 isc_net_disableipv6();
1175 fatal("invalid address %s", value);
1179 specified_source = ISC_TRUE;
1180 return (value_from_next);
1182 if ((*lookup)->rdclassset) {
1183 fprintf(stderr, ";; Warning, extra class option\n");
1185 *open_type_class = ISC_FALSE;
1187 tr.length = strlen(value);
1188 result = dns_rdataclass_fromtext(&rdclass,
1189 (isc_textregion_t *)&tr);
1190 if (result == ISC_R_SUCCESS) {
1191 (*lookup)->rdclass = rdclass;
1192 (*lookup)->rdclassset = ISC_TRUE;
1194 fprintf(stderr, ";; Warning, ignoring "
1195 "invalid class %s\n",
1197 return (value_from_next);
1200 return (value_from_next);
1202 strncpy(keyfile, value, sizeof(keyfile));
1203 keyfile[sizeof(keyfile)-1]=0;
1204 return (value_from_next);
1206 port = (in_port_t) parse_uint(value, "port number", MAXPORT);
1207 return (value_from_next);
1211 (*lookup) = clone_lookup(default_lookup,
1213 *need_clone = ISC_TRUE;
1214 strncpy((*lookup)->textname, value,
1215 sizeof((*lookup)->textname));
1216 (*lookup)->textname[sizeof((*lookup)->textname)-1]=0;
1217 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1218 (*lookup)->ns_search_only);
1219 (*lookup)->new_search = ISC_TRUE;
1221 printgreeting(argc, argv, *lookup);
1222 *firstarg = ISC_FALSE;
1224 ISC_LIST_APPEND(lookup_list, (*lookup), link);
1225 debug("looking up %s", (*lookup)->textname);
1227 return (value_from_next);
1229 *open_type_class = ISC_FALSE;
1230 if (strncasecmp(value, "ixfr=", 5) == 0) {
1231 rdtype = dns_rdatatype_ixfr;
1232 result = ISC_R_SUCCESS;
1235 tr.length = strlen(value);
1236 result = dns_rdatatype_fromtext(&rdtype,
1237 (isc_textregion_t *)&tr);
1238 if (result == ISC_R_SUCCESS &&
1239 rdtype == dns_rdatatype_ixfr) {
1240 result = DNS_R_UNKNOWN;
1243 if (result == ISC_R_SUCCESS) {
1244 if ((*lookup)->rdtypeset) {
1245 fprintf(stderr, ";; Warning, "
1246 "extra type option\n");
1248 if (rdtype == dns_rdatatype_ixfr) {
1249 (*lookup)->rdtype = dns_rdatatype_ixfr;
1250 (*lookup)->rdtypeset = ISC_TRUE;
1251 (*lookup)->ixfr_serial =
1252 parse_uint(&value[5], "serial number",
1254 (*lookup)->section_question = plusquest;
1255 (*lookup)->comments = pluscomm;
1257 (*lookup)->rdtype = rdtype;
1258 (*lookup)->rdtypeset = ISC_TRUE;
1259 if (rdtype == dns_rdatatype_axfr) {
1260 (*lookup)->section_question = plusquest;
1261 (*lookup)->comments = pluscomm;
1263 (*lookup)->ixfr_serial = ISC_FALSE;
1266 fprintf(stderr, ";; Warning, ignoring "
1267 "invalid type %s\n",
1269 return (value_from_next);
1271 ptr = next_token(&value,":"); /* hmac type or name */
1275 ptr2 = next_token(&value, ":"); /* name or secret */
1278 ptr3 = next_token(&value,":"); /* secret or NULL */
1280 if (strcasecmp(ptr, "hmac-md5") == 0) {
1281 hmacname = DNS_TSIG_HMACMD5_NAME;
1283 } else if (strncasecmp(ptr, "hmac-md5-", 9) == 0) {
1284 hmacname = DNS_TSIG_HMACMD5_NAME;
1285 digestbits = parse_uint(&ptr[9],
1286 "digest-bits [0..128]",
1288 digestbits = (digestbits + 7) & ~0x7U;
1289 } else if (strcasecmp(ptr, "hmac-sha1") == 0) {
1290 hmacname = DNS_TSIG_HMACSHA1_NAME;
1292 } else if (strncasecmp(ptr, "hmac-sha1-", 10) == 0) {
1293 hmacname = DNS_TSIG_HMACSHA1_NAME;
1294 digestbits = parse_uint(&ptr[10],
1295 "digest-bits [0..160]",
1297 digestbits = (digestbits + 7) & ~0x7U;
1298 } else if (strcasecmp(ptr, "hmac-sha224") == 0) {
1299 hmacname = DNS_TSIG_HMACSHA224_NAME;
1301 } else if (strncasecmp(ptr, "hmac-sha224-", 12) == 0) {
1302 hmacname = DNS_TSIG_HMACSHA224_NAME;
1303 digestbits = parse_uint(&ptr[12],
1304 "digest-bits [0..224]",
1306 digestbits = (digestbits + 7) & ~0x7U;
1307 } else if (strcasecmp(ptr, "hmac-sha256") == 0) {
1308 hmacname = DNS_TSIG_HMACSHA256_NAME;
1310 } else if (strncasecmp(ptr, "hmac-sha256-", 12) == 0) {
1311 hmacname = DNS_TSIG_HMACSHA256_NAME;
1312 digestbits = parse_uint(&ptr[12],
1313 "digest-bits [0..256]",
1315 digestbits = (digestbits + 7) & ~0x7U;
1316 } else if (strcasecmp(ptr, "hmac-sha384") == 0) {
1317 hmacname = DNS_TSIG_HMACSHA384_NAME;
1319 } else if (strncasecmp(ptr, "hmac-sha384-", 12) == 0) {
1320 hmacname = DNS_TSIG_HMACSHA384_NAME;
1321 digestbits = parse_uint(&ptr[12],
1322 "digest-bits [0..384]",
1324 digestbits = (digestbits + 7) & ~0x7U;
1325 } else if (strcasecmp(ptr, "hmac-sha512") == 0) {
1326 hmacname = DNS_TSIG_HMACSHA512_NAME;
1328 } else if (strncasecmp(ptr, "hmac-sha512-", 12) == 0) {
1329 hmacname = DNS_TSIG_HMACSHA512_NAME;
1330 digestbits = parse_uint(&ptr[12],
1331 "digest-bits [0..512]",
1333 digestbits = (digestbits + 7) & ~0x7U;
1335 fprintf(stderr, ";; Warning, ignoring "
1336 "invalid TSIG algorithm %s\n", ptr);
1337 return (value_from_next);
1342 hmacname = DNS_TSIG_HMACMD5_NAME;
1345 strncpy(keynametext, ptr, sizeof(keynametext));
1346 keynametext[sizeof(keynametext)-1]=0;
1347 strncpy(keysecret, ptr2, sizeof(keysecret));
1348 keysecret[sizeof(keysecret)-1]=0;
1349 return (value_from_next);
1352 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1353 *need_clone = ISC_TRUE;
1354 if (get_reverse(textname, sizeof(textname), value,
1355 ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
1356 strncpy((*lookup)->textname, textname,
1357 sizeof((*lookup)->textname));
1358 debug("looking up %s", (*lookup)->textname);
1359 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1360 (*lookup)->ns_search_only);
1361 (*lookup)->ip6_int = ip6_int;
1362 if (!(*lookup)->rdtypeset)
1363 (*lookup)->rdtype = dns_rdatatype_ptr;
1364 if (!(*lookup)->rdclassset)
1365 (*lookup)->rdclass = dns_rdataclass_in;
1366 (*lookup)->new_search = ISC_TRUE;
1368 printgreeting(argc, argv, *lookup);
1369 *firstarg = ISC_FALSE;
1371 ISC_LIST_APPEND(lookup_list, *lookup, link);
1373 fprintf(stderr, "Invalid IP address %s\n", value);
1376 return (value_from_next);
1379 fprintf(stderr, "Invalid option: -%s\n", option);
1386 * Because we may be trying to do memory allocation recording, we're going
1387 * to need to parse the arguments for the -m *before* we start the main
1388 * argument parsing routine.
1390 * I'd prefer not to have to do this, but I am not quite sure how else to
1391 * fix the problem. Argument parsing in dig involves memory allocation
1392 * by its nature, so it can't be done in the main argument parser.
1395 preparse_args(int argc, char **argv) {
1402 for (rc--, rv++; rc > 0; rc--, rv++) {
1403 if (rv[0][0] != '-')
1406 while (strpbrk(option, single_dash_opts) == &option[0]) {
1407 if (option[0] == 'm') {
1408 memdebugging = ISC_TRUE;
1409 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1410 ISC_MEM_DEBUGRECORD;
1413 option = &option[1];
1419 getaddresses(dig_lookup_t *lookup, const char *host) {
1420 isc_result_t result;
1421 isc_sockaddr_t sockaddrs[DIG_MAX_ADDRESSES];
1422 isc_netaddr_t netaddr;
1425 char tmp[ISC_NETADDR_FORMATSIZE];
1427 result = bind9_getaddresses(host, 0, sockaddrs,
1428 DIG_MAX_ADDRESSES, &count);
1429 if (result != ISC_R_SUCCESS)
1430 fatal("couldn't get address for '%s': %s",
1431 host, isc_result_totext(result));
1433 for (i = 0; i < count; i++) {
1434 isc_netaddr_fromsockaddr(&netaddr, &sockaddrs[i]);
1435 isc_netaddr_format(&netaddr, tmp, sizeof(tmp));
1436 srv = make_server(tmp, host);
1437 ISC_LIST_APPEND(lookup->my_server_list, srv, link);
1439 addresscount = count;
1443 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1444 int argc, char **argv) {
1445 isc_result_t result;
1446 isc_textregion_t tr;
1447 isc_boolean_t firstarg = ISC_TRUE;
1448 dig_lookup_t *lookup = NULL;
1449 dns_rdatatype_t rdtype;
1450 dns_rdataclass_t rdclass;
1451 isc_boolean_t open_type_class = ISC_TRUE;
1452 char batchline[MXNAME];
1463 isc_boolean_t need_clone = ISC_TRUE;
1466 * The semantics for parsing the args is a bit complex; if
1467 * we don't have a host yet, make the arg apply globally,
1468 * otherwise make it apply to the latest host. This is
1469 * a bit different than the previous versions, but should
1470 * form a consistent user interface.
1472 * First, create a "default lookup" which won't actually be used
1473 * anywhere, except for cloning into new lookups
1476 debug("parse_args()");
1477 if (!is_batchfile) {
1478 debug("making new lookup");
1479 default_lookup = make_empty_lookup();
1483 * Treat ${HOME}/.digrc as a special batchfile
1485 INSIST(batchfp == NULL);
1486 homedir = getenv("HOME");
1487 if (homedir != NULL) {
1489 n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc",
1491 if (n < sizeof(rcfile))
1492 batchfp = fopen(rcfile, "r");
1494 if (batchfp != NULL) {
1495 while (fgets(batchline, sizeof(batchline),
1497 debug("config line %s", batchline);
1500 bargv[bargc] = next_token(&input, " \t\r\n");
1501 while ((bargv[bargc] != NULL) &&
1505 next_token(&input, " \t\r\n");
1511 for(i = 0; i < bargc; i++)
1512 debug(".digrc argv %d: %s",
1514 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1522 if (is_batchfile && !config_only) {
1523 /* Processing '-f batchfile'. */
1524 lookup = clone_lookup(default_lookup, ISC_TRUE);
1525 need_clone = ISC_FALSE;
1527 lookup = default_lookup;
1531 for (rc--, rv++; rc > 0; rc--, rv++) {
1532 debug("main parsing %s", rv[0]);
1533 if (strncmp(rv[0], "%", 1) == 0)
1535 if (strncmp(rv[0], "@", 1) == 0) {
1536 getaddresses(lookup, &rv[0][1]);
1537 } else if (rv[0][0] == '+') {
1538 plus_option(&rv[0][1], is_batchfile,
1540 } else if (rv[0][0] == '-') {
1542 if (dash_option(&rv[0][1], NULL,
1543 &lookup, &open_type_class,
1544 &need_clone, config_only,
1545 argc, argv, &firstarg)) {
1550 if (dash_option(&rv[0][1], rv[1],
1551 &lookup, &open_type_class,
1552 &need_clone, config_only,
1553 argc, argv, &firstarg)) {
1560 * Anything which isn't an option
1562 if (open_type_class) {
1563 if (strncasecmp(rv[0], "ixfr=", 5) == 0) {
1564 rdtype = dns_rdatatype_ixfr;
1565 result = ISC_R_SUCCESS;
1568 tr.length = strlen(rv[0]);
1569 result = dns_rdatatype_fromtext(&rdtype,
1570 (isc_textregion_t *)&tr);
1571 if (result == ISC_R_SUCCESS &&
1572 rdtype == dns_rdatatype_ixfr) {
1573 result = DNS_R_UNKNOWN;
1574 fprintf(stderr, ";; Warning, "
1580 if (result == ISC_R_SUCCESS) {
1581 if (lookup->rdtypeset) {
1582 fprintf(stderr, ";; Warning, "
1583 "extra type option\n");
1585 if (rdtype == dns_rdatatype_ixfr) {
1588 lookup->rdtypeset = ISC_TRUE;
1589 lookup->ixfr_serial =
1590 parse_uint(&rv[0][5],
1593 lookup->section_question =
1595 lookup->comments = pluscomm;
1597 lookup->rdtype = rdtype;
1598 lookup->rdtypeset = ISC_TRUE;
1600 dns_rdatatype_axfr) {
1601 lookup->section_question =
1603 lookup->comments = pluscomm;
1605 lookup->ixfr_serial = ISC_FALSE;
1609 result = dns_rdataclass_fromtext(&rdclass,
1610 (isc_textregion_t *)&tr);
1611 if (result == ISC_R_SUCCESS) {
1612 if (lookup->rdclassset) {
1613 fprintf(stderr, ";; Warning, "
1614 "extra class option\n");
1616 lookup->rdclass = rdclass;
1617 lookup->rdclassset = ISC_TRUE;
1624 lookup = clone_lookup(default_lookup,
1626 need_clone = ISC_TRUE;
1627 strncpy(lookup->textname, rv[0],
1628 sizeof(lookup->textname));
1629 lookup->textname[sizeof(lookup->textname)-1]=0;
1630 lookup->trace_root = ISC_TF(lookup->trace ||
1631 lookup->ns_search_only);
1632 lookup->new_search = ISC_TRUE;
1634 printgreeting(argc, argv, lookup);
1635 firstarg = ISC_FALSE;
1637 ISC_LIST_APPEND(lookup_list, lookup, link);
1638 debug("looking up %s", lookup->textname);
1640 /* XXX Error message */
1645 * If we have a batchfile, seed the lookup list with the
1646 * first entry, then trust the callback in dighost_shutdown
1649 if ((batchname != NULL) && !(is_batchfile)) {
1650 if (strcmp(batchname, "-") == 0)
1653 batchfp = fopen(batchname, "r");
1654 if (batchfp == NULL) {
1658 fatal("couldn't open specified batch file");
1660 /* XXX Remove code dup from shutdown code */
1662 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1664 debug("batch line %s", batchline);
1665 if (batchline[0] == '\r' || batchline[0] == '\n'
1666 || batchline[0] == '#' || batchline[0] == ';')
1669 bargv[bargc] = next_token(&input, " \t\r\n");
1670 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1672 bargv[bargc] = next_token(&input, " \t\r\n");
1678 for(i = 0; i < bargc; i++)
1679 debug("batch argv %d: %s", i, bargv[i]);
1680 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1686 * If no lookup specified, search for root
1688 if ((lookup_list.head == NULL) && !config_only) {
1690 lookup = clone_lookup(default_lookup, ISC_TRUE);
1691 need_clone = ISC_TRUE;
1692 lookup->trace_root = ISC_TF(lookup->trace ||
1693 lookup->ns_search_only);
1694 lookup->new_search = ISC_TRUE;
1695 strcpy(lookup->textname, ".");
1696 lookup->rdtype = dns_rdatatype_ns;
1697 lookup->rdtypeset = ISC_TRUE;
1699 printgreeting(argc, argv, lookup);
1700 firstarg = ISC_FALSE;
1702 ISC_LIST_APPEND(lookup_list, lookup, link);
1705 destroy_lookup(lookup);
1709 * Callback from dighost.c to allow program-specific shutdown code.
1710 * Here, we're possibly reading from a batch file, then shutting down
1711 * for real if there's nothing in the batch file to read.
1714 dighost_shutdown(void) {
1715 char batchline[MXNAME];
1721 if (batchname == NULL) {
1727 if (feof(batchfp)) {
1730 if (batchfp != stdin)
1735 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1736 debug("batch line %s", batchline);
1739 bargv[bargc] = next_token(&input, " \t\r\n");
1740 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1742 bargv[bargc] = next_token(&input, " \t\r\n");
1747 for(i = 0; i < bargc; i++)
1748 debug("batch argv %d: %s", i, bargv[i]);
1749 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1753 if (batchfp != stdin)
1760 /*% Main processing routine for dig */
1762 main(int argc, char **argv) {
1763 isc_result_t result;
1765 ISC_LIST_INIT(lookup_list);
1766 ISC_LIST_INIT(server_list);
1767 ISC_LIST_INIT(search_list);
1770 preparse_args(argc, argv);
1772 result = isc_app_start();
1773 check_result(result, "isc_app_start");
1775 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1777 if (domainopt[0] != '\0') {
1778 set_search_domain(domainopt);
1779 usesearch = ISC_TRUE;
1781 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1782 check_result(result, "isc_app_onrun");
1784 destroy_lookup(default_lookup);
1785 if (batchname != NULL) {
1786 if (batchfp != stdin)