2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.157.2.13.2.31 2006/07/22 23:52:57 marka Exp $ */
26 #include <isc/netaddr.h>
27 #include <isc/parseint.h>
28 #include <isc/print.h>
29 #include <isc/string.h>
33 #include <dns/byaddr.h>
34 #include <dns/fixedname.h>
35 #include <dns/masterdump.h>
36 #include <dns/message.h>
38 #include <dns/rdata.h>
39 #include <dns/rdataset.h>
40 #include <dns/rdatatype.h>
41 #include <dns/rdataclass.h>
42 #include <dns/result.h>
44 #include <bind9/getaddresses.h>
48 #define ADD_STRING(b, s) { \
49 if (strlen(s) >= isc_buffer_availablelength(b)) \
50 return (ISC_R_NOSPACE); \
52 isc_buffer_putstr(b, s); \
55 #define DIG_MAX_ADDRESSES 20
57 dig_lookup_t *default_lookup = NULL;
59 static char *batchname = NULL;
60 static FILE *batchfp = NULL;
62 static int addresscount = 0;
64 static char domainopt[DNS_NAME_MAXTEXT];
66 static isc_boolean_t short_form = ISC_FALSE, printcmd = ISC_TRUE,
67 ip6_int = ISC_FALSE, plusquest = ISC_FALSE, pluscomm = ISC_FALSE,
68 multiline = ISC_FALSE, nottl = ISC_FALSE, noclass = ISC_FALSE;
70 static const char *opcodetext[] = {
89 static const char *rcodetext[] = {
110 print_usage(FILE *fp) {
112 "Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}\n"
113 " {global-d-opt} host [@local-server] {local-d-opt}\n"
114 " [ host [@local-server] {local-d-opt} [...]]\n", fp);
120 fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
121 "for complete list of options\n", stderr);
127 fputs("DiG " VERSION "\n", stderr);
134 "Where: domain is in the Domain Name System\n"
135 " q-class is one of (in,hs,ch,...) [default: in]\n"
136 " q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
137 " (Use ixfr=version for type ixfr)\n"
138 " q-opt is one of:\n"
139 " -x dot-notation (shortcut for in-addr lookups)\n"
140 " -i (IP6.INT reverse IPv6 lookups)\n"
141 " -f filename (batch mode)\n"
142 " -b address[#port] (bind to source address/port)\n"
143 " -p port (specify port number)\n"
144 " -t type (specify query type)\n"
145 " -c class (specify query class)\n"
146 " -k keyfile (specify tsig key file)\n"
147 " -y name:key (specify named base64 tsig key)\n"
148 " -4 (use IPv4 query transport only)\n"
149 " -6 (use IPv6 query transport only)\n"
150 " d-opt is of the form +keyword[=value], where keyword is:\n"
151 " +[no]vc (TCP mode)\n"
152 " +[no]tcp (TCP mode, alternate syntax)\n"
153 " +time=### (Set query timeout) [5]\n"
154 " +tries=### (Set number of UDP attempts) [3]\n"
155 " +retry=### (Set number of UDP retries) [2]\n"
156 " +domain=### (Set default domainname)\n"
157 " +bufsize=### (Set EDNS0 Max UDP packet size)\n"
158 " +ndots=### (Set NDOTS value)\n"
159 " +[no]search (Set whether to use searchlist)\n"
160 " +[no]defname (Ditto)\n"
161 " +[no]recurse (Recursive mode)\n"
162 " +[no]ignore (Don't revert to TCP for TC responses.)"
164 " +[no]fail (Don't try next server on SERVFAIL)\n"
165 " +[no]besteffort (Try to parse even illegal messages)\n"
166 " +[no]aaonly (Set AA flag in query (+[no]aaflag))\n"
167 " +[no]adflag (Set AD flag in query)\n"
168 " +[no]cdflag (Set CD flag in query)\n"
169 " +[no]cl (Control display of class in records)\n"
170 " +[no]cmd (Control display of command line)\n"
171 " +[no]comments (Control display of comment lines)\n"
172 " +[no]question (Control display of question)\n"
173 " +[no]answer (Control display of answer)\n"
174 " +[no]authority (Control display of authority)\n"
175 " +[no]additional (Control display of additional)\n"
176 " +[no]stats (Control display of statistics)\n"
177 " +[no]short (Disable everything except short\n"
179 " +[no]ttlid (Control display of ttls in records)\n"
180 " +[no]all (Set or clear all display flags)\n"
181 " +[no]qr (Print question before sending)\n"
182 " +[no]nssearch (Search all authoritative nameservers)\n"
183 " +[no]identify (ID responders in short answers)\n"
184 " +[no]trace (Trace delegation down from root)\n"
185 " +[no]dnssec (Request DNSSEC records)\n"
187 " +[no]sigchase (Chase DNSSEC signatures)\n"
188 " +trusted-key=#### (Trusted Key when chasing DNSSEC sigs)\n"
190 " +[no]topdown (Do DNSSEC validation top down mode)\n"
193 " +[no]multiline (Print records in an expanded format)\n"
194 " global d-opts and servers (before host name) affect all queries.\n"
195 " local d-opts and servers (after host name) affect only that lookup.\n"
196 " -h (print help and exit)\n"
197 " -v (print version and exit)\n",
202 * Callback from dighost.c to print the received message.
205 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
209 char fromtext[ISC_SOCKADDR_FORMATSIZE];
211 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
215 if (query->lookup->stats && !short_form) {
216 diff = isc_time_microdiff(&now, &query->time_sent);
217 printf(";; Query time: %ld msec\n", (long int)diff/1000);
218 printf(";; SERVER: %s(%s)\n", fromtext, query->servname);
220 printf(";; WHEN: %s", ctime(&tnow));
221 if (query->lookup->doing_xfr) {
222 printf(";; XFR size: %u records (messages %u)\n",
223 query->rr_count, query->msg_count);
225 printf(";; MSG SIZE rcvd: %d\n", bytes);
230 puts(";; WARNING -- Some TSIG could not "
233 if ((key == NULL) && (keysecret[0] != 0)) {
234 puts(";; WARNING -- TSIG key was not used.");
237 } else if (query->lookup->identify && !short_form) {
238 diff = isc_time_microdiff(&now, &query->time_sent);
239 printf(";; Received %u bytes from %s(%s) in %d ms\n\n",
240 bytes, fromtext, query->servname,
246 * Callback from dighost.c to print that it is trying a server.
251 trying(char *frm, dig_lookup_t *lookup) {
257 * Internal print routine used to print short form replies.
260 say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
264 char store[sizeof("12345678901234567890")];
266 if (query->lookup->trace || query->lookup->ns_search_only) {
267 result = dns_rdatatype_totext(rdata->type, buf);
268 if (result != ISC_R_SUCCESS)
270 ADD_STRING(buf, " ");
272 result = dns_rdata_totext(rdata, NULL, buf);
273 check_result(result, "dns_rdata_totext");
274 if (query->lookup->identify) {
276 diff = isc_time_microdiff(&now, &query->time_sent);
277 ADD_STRING(buf, " from server ");
278 ADD_STRING(buf, query->servname);
279 snprintf(store, 19, " in %d ms.", (int)diff/1000);
280 ADD_STRING(buf, store);
282 ADD_STRING(buf, "\n");
283 return (ISC_R_SUCCESS);
287 * short_form message print handler. Calls above say_message()
290 short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
291 isc_buffer_t *buf, dig_query_t *query)
294 dns_rdataset_t *rdataset;
296 isc_result_t result, loopresult;
297 dns_name_t empty_name;
299 dns_rdata_t rdata = DNS_RDATA_INIT;
303 dns_name_init(&empty_name, NULL);
304 result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
305 if (result == ISC_R_NOMORE)
306 return (ISC_R_SUCCESS);
307 else if (result != ISC_R_SUCCESS)
312 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
314 isc_buffer_init(&target, t, sizeof(t));
316 for (rdataset = ISC_LIST_HEAD(name->list);
318 rdataset = ISC_LIST_NEXT(rdataset, link)) {
319 loopresult = dns_rdataset_first(rdataset);
320 while (loopresult == ISC_R_SUCCESS) {
321 dns_rdataset_current(rdataset, &rdata);
322 result = say_message(&rdata, query,
324 check_result(result, "say_message");
325 loopresult = dns_rdataset_next(rdataset);
326 dns_rdata_reset(&rdata);
329 result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
330 if (result == ISC_R_NOMORE)
332 else if (result != ISC_R_SUCCESS)
336 return (ISC_R_SUCCESS);
340 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
341 isc_buffer_t *target)
344 dns_master_style_t *style = NULL;
345 unsigned int styleflags = 0;
347 if (rdataset == NULL || owner_name == NULL || target == NULL)
350 styleflags |= DNS_STYLEFLAG_REL_OWNER;
352 styleflags |= DNS_STYLEFLAG_NO_TTL;
354 styleflags |= DNS_STYLEFLAG_NO_CLASS;
356 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
357 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
358 styleflags |= DNS_STYLEFLAG_REL_DATA;
359 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
360 styleflags |= DNS_STYLEFLAG_TTL;
361 styleflags |= DNS_STYLEFLAG_MULTILINE;
362 styleflags |= DNS_STYLEFLAG_COMMENT;
364 if (multiline || (nottl && noclass))
365 result = dns_master_stylecreate(&style, styleflags,
366 24, 24, 24, 32, 80, 8, mctx);
367 else if (nottl || noclass)
368 result = dns_master_stylecreate(&style, styleflags,
369 24, 24, 32, 40, 80, 8, mctx);
371 result = dns_master_stylecreate(&style, styleflags,
372 24, 32, 40, 48, 80, 8, mctx);
373 check_result(result, "dns_master_stylecreate");
375 result = dns_master_rdatasettotext(owner_name, rdataset, style, target);
378 dns_master_styledestroy(&style, mctx);
385 * Callback from dighost.c to print the reply from a server
388 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
390 dns_messagetextflag_t flags;
391 isc_buffer_t *buf = NULL;
392 unsigned int len = OUTPUTBUF;
393 dns_master_style_t *style = NULL;
394 unsigned int styleflags = 0;
396 styleflags |= DNS_STYLEFLAG_REL_OWNER;
398 styleflags |= DNS_STYLEFLAG_NO_TTL;
400 styleflags |= DNS_STYLEFLAG_NO_CLASS;
402 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
403 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
404 styleflags |= DNS_STYLEFLAG_REL_DATA;
405 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
406 styleflags |= DNS_STYLEFLAG_TTL;
407 styleflags |= DNS_STYLEFLAG_MULTILINE;
408 styleflags |= DNS_STYLEFLAG_COMMENT;
410 if (multiline || (nottl && noclass))
411 result = dns_master_stylecreate(&style, styleflags,
412 24, 24, 24, 32, 80, 8, mctx);
413 else if (nottl || noclass)
414 result = dns_master_stylecreate(&style, styleflags,
415 24, 24, 32, 40, 80, 8, mctx);
417 result = dns_master_stylecreate(&style, styleflags,
418 24, 32, 40, 48, 80, 8, mctx);
419 check_result(result, "dns_master_stylecreate");
421 if (query->lookup->cmdline[0] != 0) {
423 fputs(query->lookup->cmdline, stdout);
424 query->lookup->cmdline[0]=0;
426 debug("printmessage(%s %s %s)", headers ? "headers" : "noheaders",
427 query->lookup->comments ? "comments" : "nocomments",
428 short_form ? "short_form" : "long_form");
432 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
433 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
435 if (!query->lookup->comments)
436 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
438 result = ISC_R_SUCCESS;
440 result = isc_buffer_allocate(mctx, &buf, len);
441 check_result(result, "isc_buffer_allocate");
443 if (query->lookup->comments && !short_form) {
444 if (query->lookup->cmdline[0] != 0)
445 printf("; %s\n", query->lookup->cmdline);
446 if (msg == query->lookup->sendmsg)
447 printf(";; Sending:\n");
449 printf(";; Got answer:\n");
452 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
454 opcodetext[msg->opcode], rcodetext[msg->rcode],
457 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
459 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
461 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
463 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
465 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
467 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
469 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
472 printf("; QUERY: %u, ANSWER: %u, "
473 "AUTHORITY: %u, ADDITIONAL: %u\n",
474 msg->counts[DNS_SECTION_QUESTION],
475 msg->counts[DNS_SECTION_ANSWER],
476 msg->counts[DNS_SECTION_AUTHORITY],
477 msg->counts[DNS_SECTION_ADDITIONAL]);
483 if (query->lookup->comments && headers && !short_form) {
484 result = dns_message_pseudosectiontotext(msg,
485 DNS_PSEUDOSECTION_OPT,
487 if (result == ISC_R_NOSPACE) {
490 isc_buffer_free(&buf);
491 result = isc_buffer_allocate(mctx, &buf, len);
492 if (result == ISC_R_SUCCESS)
493 goto repopulate_buffer;
498 "dns_message_pseudosectiontotext");
501 if (query->lookup->section_question && headers) {
503 result = dns_message_sectiontotext(msg,
504 DNS_SECTION_QUESTION,
506 if (result == ISC_R_NOSPACE)
508 check_result(result, "dns_message_sectiontotext");
511 if (query->lookup->section_answer) {
513 result = dns_message_sectiontotext(msg,
516 if (result == ISC_R_NOSPACE)
518 check_result(result, "dns_message_sectiontotext");
520 result = short_answer(msg, flags, buf, query);
521 if (result == ISC_R_NOSPACE)
523 check_result(result, "short_answer");
526 if (query->lookup->section_authority) {
528 result = dns_message_sectiontotext(msg,
529 DNS_SECTION_AUTHORITY,
531 if (result == ISC_R_NOSPACE)
533 check_result(result, "dns_message_sectiontotext");
536 if (query->lookup->section_additional) {
538 result = dns_message_sectiontotext(msg,
539 DNS_SECTION_ADDITIONAL,
541 if (result == ISC_R_NOSPACE)
543 check_result(result, "dns_message_sectiontotext");
545 * Only print the signature on the first record.
548 result = dns_message_pseudosectiontotext(
550 DNS_PSEUDOSECTION_TSIG,
552 if (result == ISC_R_NOSPACE)
555 "dns_message_pseudosectiontotext");
556 result = dns_message_pseudosectiontotext(
558 DNS_PSEUDOSECTION_SIG0,
560 if (result == ISC_R_NOSPACE)
563 "dns_message_pseudosectiontotext");
568 if (headers && query->lookup->comments && !short_form)
571 printf("%.*s", (int)isc_buffer_usedlength(buf),
572 (char *)isc_buffer_base(buf));
573 isc_buffer_free(&buf);
577 dns_master_styledestroy(&style, mctx);
582 * print the greeting message when the program first starts up.
585 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
588 static isc_boolean_t first = ISC_TRUE;
592 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
593 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
594 "%s; <<>> DiG " VERSION " <<>>",
598 snprintf(append, sizeof(append), " %s", argv[i++]);
599 remaining = sizeof(lookup->cmdline) -
600 strlen(lookup->cmdline) - 1;
601 strncat(lookup->cmdline, append, remaining);
603 remaining = sizeof(lookup->cmdline) -
604 strlen(lookup->cmdline) - 1;
605 strncat(lookup->cmdline, "\n", remaining);
606 if (first && addresscount != 0) {
607 snprintf(append, sizeof(append),
608 "; (%d server%s found)\n",
610 addresscount > 1 ? "s" : "");
611 remaining = sizeof(lookup->cmdline) -
612 strlen(lookup->cmdline) - 1;
613 strncat(lookup->cmdline, append, remaining);
616 snprintf(append, sizeof(append),
617 ";; global options: %s %s\n",
618 short_form ? "short_form" : "",
619 printcmd ? "printcmd" : "");
621 remaining = sizeof(lookup->cmdline) -
622 strlen(lookup->cmdline) - 1;
623 strncat(lookup->cmdline, append, remaining);
629 * Reorder an argument list so that server names all come at the end.
630 * This is a bit of a hack, to allow batch-mode processing to properly
631 * handle the server options.
634 reorder_args(int argc, char *argv[]) {
639 debug("reorder_args()");
641 while (argv[end][0] == '@') {
646 debug("arg[end]=%s", argv[end]);
647 for (i = 1; i < end - 1; i++) {
648 if (argv[i][0] == '@') {
649 debug("arg[%d]=%s", i, argv[i]);
651 for (j = i + 1; j < end; j++) {
652 debug("Moving %s to %d", argv[j], j - 1);
653 argv[j - 1] = argv[j];
655 debug("moving %s to end, %d", ptr, end - 1);
665 parse_uint(char *arg, const char *desc, isc_uint32_t max) {
669 result = isc_parse_uint32(&tmp, arg, 10);
670 if (result == ISC_R_SUCCESS && tmp > max)
671 result = ISC_R_RANGE;
672 if (result != ISC_R_SUCCESS)
673 fatal("%s '%s': %s", desc, arg, isc_result_totext(result));
678 * We're not using isc_commandline_parse() here since the command line
679 * syntax of dig is quite a bit different from that which can be described
685 plus_option(char *option, isc_boolean_t is_batchfile,
686 dig_lookup_t *lookup)
688 char option_store[256];
689 char *cmd, *value, *ptr;
690 isc_boolean_t state = ISC_TRUE;
695 strncpy(option_store, option, sizeof(option_store));
696 option_store[sizeof(option_store)-1]=0;
698 cmd = next_token(&ptr,"=");
700 printf(";; Invalid option %s\n", option_store);
704 if (strncasecmp(cmd, "no", 2)==0) {
709 #define FULLCHECK(A) \
711 size_t _l = strlen(cmd); \
712 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
713 goto invalid_option; \
715 #define FULLCHECK2(A, B) \
717 size_t _l = strlen(cmd); \
718 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
719 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
720 goto invalid_option; \
726 case 'a': /* aaonly / aaflag */
727 FULLCHECK2("aaonly", "aaflag");
728 lookup->aaonly = state;
732 case 'd': /* additional */
733 FULLCHECK("additional");
734 lookup->section_additional = state;
736 case 'f': /* adflag */
738 lookup->adflag = state;
746 lookup->section_question = state;
747 lookup->section_authority = state;
748 lookup->section_answer = state;
749 lookup->section_additional = state;
750 lookup->comments = state;
751 lookup->stats = state;
754 case 'n': /* answer */
756 lookup->section_answer = state;
758 case 'u': /* authority */
759 FULLCHECK("authority");
760 lookup->section_authority = state;
768 case 'e':/* besteffort */
769 FULLCHECK("besteffort");
770 lookup->besteffort = state;
772 case 'u':/* bufsize */
773 FULLCHECK("bufsize");
778 lookup->udpsize = (isc_uint16_t) parse_uint(value,
779 "buffer size", COMMSIZE);
787 case 'd':/* cdflag */
789 lookup->cdflag = state;
793 noclass = ISC_TF(!state);
799 case 'o': /* comments */
800 FULLCHECK("comments");
801 lookup->comments = state;
802 if (lookup == default_lookup)
811 case 'e': /* defname */
812 FULLCHECK("defname");
815 case 'n': /* dnssec */
817 lookup->dnssec = state;
819 case 'o': /* domain */
825 strncpy(domainopt, value, sizeof(domainopt));
826 domainopt[sizeof(domainopt)-1] = '\0';
834 lookup->servfail_stops = state;
838 case 'd': /* identify */
839 FULLCHECK("identify");
840 lookup->identify = state;
842 case 'g': /* ignore */
843 default: /* Inherets default for compatibility */
845 lookup->ignore = ISC_TRUE;
848 case 'm': /* multiline */
849 FULLCHECK("multiline");
854 case 'd': /* ndots */
860 ndots = parse_uint(value, "ndots", MAXNDOTS);
862 case 's': /* nssearch */
863 FULLCHECK("nssearch");
864 lookup->ns_search_only = state;
866 lookup->trace_root = ISC_TRUE;
867 lookup->recurse = ISC_TRUE;
868 lookup->identify = ISC_TRUE;
869 lookup->stats = ISC_FALSE;
870 lookup->comments = ISC_FALSE;
871 lookup->section_additional = ISC_FALSE;
872 lookup->section_authority = ISC_FALSE;
873 lookup->section_question = ISC_FALSE;
874 lookup->rdtype = dns_rdatatype_ns;
875 lookup->rdtypeset = ISC_TRUE;
876 short_form = ISC_TRUE;
889 case 'u': /* question */
890 FULLCHECK("question");
891 lookup->section_question = state;
892 if (lookup == default_lookup)
903 case 'c': /* recurse */
904 FULLCHECK("recurse");
905 lookup->recurse = state;
907 case 't': /* retry / retries */
908 FULLCHECK2("retry", "retries");
913 lookup->retries = parse_uint(value, "retries",
927 case 'e': /* search */
931 case 'h': /* short */
935 printcmd = ISC_FALSE;
936 lookup->section_additional = ISC_FALSE;
937 lookup->section_answer = ISC_TRUE;
938 lookup->section_authority = ISC_FALSE;
939 lookup->section_question = ISC_FALSE;
940 lookup->comments = ISC_FALSE;
941 lookup->stats = ISC_FALSE;
945 case 'i': /* sigchase */
946 FULLCHECK("sigchase");
947 lookup->sigchase = state;
948 if (lookup->sigchase)
949 lookup->dnssec = ISC_TRUE;
952 case 't': /* stats */
954 lookup->stats = state;
965 lookup->tcp_mode = state;
967 case 'i': /* timeout */
968 FULLCHECK("timeout");
973 timeout = parse_uint(value, "timeout", MAXTIMEOUT);
978 case 'o': /* topdown */
979 FULLCHECK("topdown");
980 lookup->do_topdown = state;
985 case 'a': /* trace */
987 lookup->trace = state;
988 lookup->trace_root = state;
990 lookup->recurse = ISC_FALSE;
991 lookup->identify = ISC_TRUE;
992 lookup->comments = ISC_FALSE;
993 lookup->stats = ISC_FALSE;
994 lookup->section_additional = ISC_FALSE;
995 lookup->section_authority = ISC_TRUE;
996 lookup->section_question = ISC_FALSE;
999 case 'i': /* tries */
1004 goto invalid_option;
1005 lookup->retries = parse_uint(value, "tries",
1007 if (lookup->retries == 0)
1008 lookup->retries = 1;
1011 case 'u': /* trusted-key */
1012 FULLCHECK("trusted-key");
1016 goto invalid_option;
1017 n = strlcpy(trustedkey, ptr,
1018 sizeof(trustedkey));
1019 if (n >= sizeof(trustedkey))
1020 fatal("trusted key too large");
1024 goto invalid_option;
1027 case 't': /* ttlid */
1029 nottl = ISC_TF(!state);
1032 goto invalid_option;
1038 lookup->tcp_mode = state;
1043 fprintf(stderr, "Invalid option: +%s\n",
1051 * ISC_TRUE returned if value was used
1053 static const char *single_dash_opts = "46dhimnv";
1054 static const char *dash_opts = "46bcdfhikmnptvyx";
1055 static isc_boolean_t
1056 dash_option(char *option, char *next, dig_lookup_t **lookup,
1057 isc_boolean_t *open_type_class)
1059 char opt, *value, *ptr;
1060 isc_result_t result;
1061 isc_boolean_t value_from_next;
1062 isc_textregion_t tr;
1063 dns_rdatatype_t rdtype;
1064 dns_rdataclass_t rdclass;
1065 char textname[MXNAME];
1067 struct in6_addr in6;
1071 while (strpbrk(option, single_dash_opts) == &option[0]) {
1073 * Since the -[46dhimnv] options do not take an argument,
1074 * account for them (in any number and/or combination)
1075 * if they appear as the first character(s) of a q-opt.
1081 isc_net_disableipv6();
1082 have_ipv6 = ISC_FALSE;
1084 fatal("can't find IPv4 networking");
1090 isc_net_disableipv4();
1091 have_ipv4 = ISC_FALSE;
1093 fatal("can't find IPv6 networking");
1098 ptr = strpbrk(&option[1], dash_opts);
1099 if (ptr != &option[1]) {
1102 debugging = ISC_TRUE;
1105 debugging = ISC_TRUE;
1114 case 'm': /* memdebug */
1115 /* memdebug is handled in preparse_args() */
1125 if (strlen(option) > 1U)
1126 option = &option[1];
1131 if (strlen(option) > 1U) {
1132 value_from_next = ISC_FALSE;
1135 value_from_next = ISC_TRUE;
1139 goto invalid_option;
1142 hash = strchr(value, '#');
1144 srcport = (in_port_t)
1145 parse_uint(hash + 1,
1146 "port number", MAXPORT);
1150 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1151 isc_sockaddr_fromin6(&bind_address, &in6, srcport);
1152 isc_net_disableipv4();
1153 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1154 isc_sockaddr_fromin(&bind_address, &in4, srcport);
1155 isc_net_disableipv6();
1159 fatal("invalid address %s", value);
1163 specified_source = ISC_TRUE;
1164 return (value_from_next);
1166 if ((*lookup)->rdclassset) {
1167 fprintf(stderr, ";; Warning, extra class option\n");
1169 *open_type_class = ISC_FALSE;
1171 tr.length = strlen(value);
1172 result = dns_rdataclass_fromtext(&rdclass,
1173 (isc_textregion_t *)&tr);
1174 if (result == ISC_R_SUCCESS) {
1175 (*lookup)->rdclass = rdclass;
1176 (*lookup)->rdclassset = ISC_TRUE;
1178 fprintf(stderr, ";; Warning, ignoring "
1179 "invalid class %s\n",
1181 return (value_from_next);
1184 return (value_from_next);
1186 strncpy(keyfile, value, sizeof(keyfile));
1187 keyfile[sizeof(keyfile)-1]=0;
1188 return (value_from_next);
1190 port = (in_port_t) parse_uint(value, "port number", MAXPORT);
1191 return (value_from_next);
1193 *open_type_class = ISC_FALSE;
1194 if (strncasecmp(value, "ixfr=", 5) == 0) {
1195 rdtype = dns_rdatatype_ixfr;
1196 result = ISC_R_SUCCESS;
1199 tr.length = strlen(value);
1200 result = dns_rdatatype_fromtext(&rdtype,
1201 (isc_textregion_t *)&tr);
1202 if (result == ISC_R_SUCCESS &&
1203 rdtype == dns_rdatatype_ixfr) {
1204 result = DNS_R_UNKNOWN;
1207 if (result == ISC_R_SUCCESS) {
1208 if ((*lookup)->rdtypeset) {
1209 fprintf(stderr, ";; Warning, "
1210 "extra type option\n");
1212 if (rdtype == dns_rdatatype_ixfr) {
1213 (*lookup)->rdtype = dns_rdatatype_ixfr;
1214 (*lookup)->rdtypeset = ISC_TRUE;
1215 (*lookup)->ixfr_serial =
1216 parse_uint(&value[5], "serial number",
1218 (*lookup)->section_question = plusquest;
1219 (*lookup)->comments = pluscomm;
1221 (*lookup)->rdtype = rdtype;
1222 (*lookup)->rdtypeset = ISC_TRUE;
1223 if (rdtype == dns_rdatatype_axfr) {
1224 (*lookup)->section_question = plusquest;
1225 (*lookup)->comments = pluscomm;
1227 (*lookup)->ixfr_serial = ISC_FALSE;
1230 fprintf(stderr, ";; Warning, ignoring "
1231 "invalid type %s\n",
1233 return (value_from_next);
1235 ptr = next_token(&value,":");
1239 strncpy(keynametext, ptr, sizeof(keynametext));
1240 keynametext[sizeof(keynametext)-1]=0;
1241 ptr = next_token(&value, "");
1244 strncpy(keysecret, ptr, sizeof(keysecret));
1245 keysecret[sizeof(keysecret)-1]=0;
1246 return (value_from_next);
1248 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1249 if (get_reverse(textname, sizeof(textname), value,
1250 ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
1251 strncpy((*lookup)->textname, textname,
1252 sizeof((*lookup)->textname));
1253 debug("looking up %s", (*lookup)->textname);
1254 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1255 (*lookup)->ns_search_only);
1256 (*lookup)->ip6_int = ip6_int;
1257 if (!(*lookup)->rdtypeset)
1258 (*lookup)->rdtype = dns_rdatatype_ptr;
1259 if (!(*lookup)->rdclassset)
1260 (*lookup)->rdclass = dns_rdataclass_in;
1261 (*lookup)->new_search = ISC_TRUE;
1262 ISC_LIST_APPEND(lookup_list, *lookup, link);
1264 fprintf(stderr, "Invalid IP address %s\n", value);
1267 return (value_from_next);
1270 fprintf(stderr, "Invalid option: -%s\n", option);
1277 * Because we may be trying to do memory allocation recording, we're going
1278 * to need to parse the arguments for the -m *before* we start the main
1279 * argument parsing routine.
1280 * I'd prefer not to have to do this, but I am not quite sure how else to
1281 * fix the problem. Argument parsing in dig involves memory allocation
1282 * by its nature, so it can't be done in the main argument parser.
1285 preparse_args(int argc, char **argv) {
1292 for (rc--, rv++; rc > 0; rc--, rv++) {
1293 if (rv[0][0] != '-')
1296 while (strpbrk(option, single_dash_opts) == &option[0]) {
1297 if (option[0] == 'm') {
1298 memdebugging = ISC_TRUE;
1299 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1300 ISC_MEM_DEBUGRECORD;
1303 option = &option[1];
1309 getaddresses(dig_lookup_t *lookup, const char *host) {
1310 isc_result_t result;
1311 isc_sockaddr_t sockaddrs[DIG_MAX_ADDRESSES];
1312 isc_netaddr_t netaddr;
1315 char tmp[ISC_NETADDR_FORMATSIZE];
1317 result = bind9_getaddresses(host, 0, sockaddrs,
1318 DIG_MAX_ADDRESSES, &count);
1319 if (result != ISC_R_SUCCESS)
1320 fatal("couldn't get address for '%s': %s",
1321 host, isc_result_totext(result));
1323 for (i = 0; i < count; i++) {
1324 isc_netaddr_fromsockaddr(&netaddr, &sockaddrs[i]);
1325 isc_netaddr_format(&netaddr, tmp, sizeof(tmp));
1326 srv = make_server(tmp, host);
1327 ISC_LIST_APPEND(lookup->my_server_list, srv, link);
1329 addresscount = count;
1333 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1334 int argc, char **argv) {
1335 isc_result_t result;
1336 isc_textregion_t tr;
1337 isc_boolean_t firstarg = ISC_TRUE;
1338 dig_lookup_t *lookup = NULL;
1339 dns_rdatatype_t rdtype;
1340 dns_rdataclass_t rdclass;
1341 isc_boolean_t open_type_class = ISC_TRUE;
1342 char batchline[MXNAME];
1354 * The semantics for parsing the args is a bit complex; if
1355 * we don't have a host yet, make the arg apply globally,
1356 * otherwise make it apply to the latest host. This is
1357 * a bit different than the previous versions, but should
1358 * form a consistent user interface.
1360 * First, create a "default lookup" which won't actually be used
1361 * anywhere, except for cloning into new lookups
1364 debug("parse_args()");
1365 if (!is_batchfile) {
1366 debug("making new lookup");
1367 default_lookup = make_empty_lookup();
1371 * Treat ${HOME}/.digrc as a special batchfile
1373 INSIST(batchfp == NULL);
1374 homedir = getenv("HOME");
1375 if (homedir != NULL) {
1377 n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc",
1379 if (n < sizeof(rcfile))
1380 batchfp = fopen(rcfile, "r");
1382 if (batchfp != NULL) {
1383 while (fgets(batchline, sizeof(batchline),
1385 debug("config line %s", batchline);
1388 bargv[bargc] = next_token(&input, " \t\r\n");
1389 while ((bargv[bargc] != NULL) &&
1393 next_token(&input, " \t\r\n");
1399 reorder_args(bargc, (char **)bargv);
1400 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1408 lookup = default_lookup;
1412 for (rc--, rv++; rc > 0; rc--, rv++) {
1413 debug("main parsing %s", rv[0]);
1414 if (strncmp(rv[0], "%", 1) == 0)
1416 if (strncmp(rv[0], "@", 1) == 0) {
1417 getaddresses(lookup, &rv[0][1]);
1418 } else if (rv[0][0] == '+') {
1419 plus_option(&rv[0][1], is_batchfile,
1421 } else if (rv[0][0] == '-') {
1423 if (dash_option(&rv[0][1], NULL,
1424 &lookup, &open_type_class)) {
1429 if (dash_option(&rv[0][1], rv[1],
1430 &lookup, &open_type_class)) {
1437 * Anything which isn't an option
1439 if (open_type_class) {
1440 if (strncasecmp(rv[0], "ixfr=", 5) == 0) {
1441 rdtype = dns_rdatatype_ixfr;
1442 result = ISC_R_SUCCESS;
1445 tr.length = strlen(rv[0]);
1446 result = dns_rdatatype_fromtext(&rdtype,
1447 (isc_textregion_t *)&tr);
1448 if (result == ISC_R_SUCCESS &&
1449 rdtype == dns_rdatatype_ixfr) {
1450 result = DNS_R_UNKNOWN;
1451 fprintf(stderr, ";; Warning, "
1457 if (result == ISC_R_SUCCESS) {
1458 if (lookup->rdtypeset) {
1459 fprintf(stderr, ";; Warning, "
1460 "extra type option\n");
1462 if (rdtype == dns_rdatatype_ixfr) {
1465 lookup->rdtypeset = ISC_TRUE;
1466 lookup->ixfr_serial =
1467 parse_uint(&rv[0][5],
1470 lookup->section_question =
1472 lookup->comments = pluscomm;
1474 lookup->rdtype = rdtype;
1475 lookup->rdtypeset = ISC_TRUE;
1477 dns_rdatatype_axfr) {
1478 lookup->section_question =
1480 lookup->comments = pluscomm;
1482 lookup->ixfr_serial = ISC_FALSE;
1486 result = dns_rdataclass_fromtext(&rdclass,
1487 (isc_textregion_t *)&tr);
1488 if (result == ISC_R_SUCCESS) {
1489 if (lookup->rdclassset) {
1490 fprintf(stderr, ";; Warning, "
1491 "extra class option\n");
1493 lookup->rdclass = rdclass;
1494 lookup->rdclassset = ISC_TRUE;
1499 lookup = clone_lookup(default_lookup,
1501 strncpy(lookup->textname, rv[0],
1502 sizeof(lookup->textname));
1503 lookup->textname[sizeof(lookup->textname)-1]=0;
1504 lookup->trace_root = ISC_TF(lookup->trace ||
1505 lookup->ns_search_only);
1506 lookup->new_search = ISC_TRUE;
1507 ISC_LIST_APPEND(lookup_list, lookup, link);
1508 debug("looking up %s", lookup->textname);
1510 /* XXX Error message */
1514 * If we have a batchfile, seed the lookup list with the
1515 * first entry, then trust the callback in dighost_shutdown
1518 if ((batchname != NULL) && !(is_batchfile)) {
1519 if (strcmp(batchname, "-") == 0)
1522 batchfp = fopen(batchname, "r");
1523 if (batchfp == NULL) {
1527 fatal("couldn't open specified batch file");
1529 /* XXX Remove code dup from shutdown code */
1531 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1533 debug("batch line %s", batchline);
1534 if (batchline[0] == '\r' || batchline[0] == '\n'
1535 || batchline[0] == '#' || batchline[0] == ';')
1538 bargv[bargc] = next_token(&input, " \t\r\n");
1539 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1541 bargv[bargc] = next_token(&input, " \t\r\n");
1547 reorder_args(bargc, (char **)bargv);
1548 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1552 * If no lookup specified, search for root
1554 if ((lookup_list.head == NULL) && !config_only) {
1555 lookup = clone_lookup(default_lookup, ISC_TRUE);
1556 lookup->trace_root = ISC_TF(lookup->trace ||
1557 lookup->ns_search_only);
1558 lookup->new_search = ISC_TRUE;
1559 strcpy(lookup->textname, ".");
1560 lookup->rdtype = dns_rdatatype_ns;
1561 lookup->rdtypeset = ISC_TRUE;
1563 printgreeting(argc, argv, lookup);
1564 firstarg = ISC_FALSE;
1566 ISC_LIST_APPEND(lookup_list, lookup, link);
1567 } else if (!config_only && firstarg) {
1568 printgreeting(argc, argv, lookup);
1569 firstarg = ISC_FALSE;
1574 * Callback from dighost.c to allow program-specific shutdown code.
1575 * Here, we're possibly reading from a batch file, then shutting down
1576 * for real if there's nothing in the batch file to read.
1579 dighost_shutdown(void) {
1580 char batchline[MXNAME];
1586 if (batchname == NULL) {
1592 if (feof(batchfp)) {
1595 if (batchfp != stdin)
1600 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1601 debug("batch line %s", batchline);
1604 bargv[bargc] = next_token(&input, " \t\r\n");
1605 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1607 bargv[bargc] = next_token(&input, " \t\r\n");
1612 reorder_args(bargc, (char **)bargv);
1613 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1617 if (batchfp != stdin)
1625 main(int argc, char **argv) {
1626 isc_result_t result;
1627 dig_server_t *s, *s2;
1629 ISC_LIST_INIT(lookup_list);
1630 ISC_LIST_INIT(server_list);
1631 ISC_LIST_INIT(search_list);
1634 preparse_args(argc, argv);
1636 result = isc_app_start();
1637 check_result(result, "isc_app_start");
1639 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1641 if (domainopt[0] != '\0') {
1642 set_search_domain(domainopt);
1643 usesearch = ISC_TRUE;
1645 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1646 check_result(result, "isc_app_onrun");
1648 s = ISC_LIST_HEAD(default_lookup->my_server_list);
1650 debug("freeing server %p belonging to %p",
1653 s = ISC_LIST_NEXT(s, link);
1654 ISC_LIST_DEQUEUE(default_lookup->my_server_list, s2, link);
1655 isc_mem_free(mctx, s2);
1657 isc_mem_free(mctx, default_lookup);
1658 if (batchname != NULL) {
1659 if (batchfp != stdin)