2 * Copyright (C) 2004 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.20 2004/06/23 04:19:40 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>
46 extern ISC_LIST(dig_lookup_t) lookup_list;
47 extern dig_serverlist_t server_list;
48 extern ISC_LIST(dig_searchlist_t) search_list;
50 #define ADD_STRING(b, s) { \
51 if (strlen(s) >= isc_buffer_availablelength(b)) \
52 return (ISC_R_NOSPACE); \
54 isc_buffer_putstr(b, s); \
58 extern isc_boolean_t have_ipv4, have_ipv6, specified_source,
60 extern in_port_t port;
61 extern unsigned int timeout;
62 extern isc_mem_t *mctx;
63 extern dns_messageid_t id;
66 extern int lookup_counter;
68 extern isc_sockaddr_t bind_address;
69 extern char keynametext[MXNAME];
70 extern char keyfile[MXNAME];
71 extern char keysecret[MXNAME];
73 extern char trustedkey[MXNAME];
75 extern dns_tsigkey_t *key;
76 extern isc_boolean_t validated;
77 extern isc_taskmgr_t *taskmgr;
78 extern isc_task_t *global_task;
79 extern isc_boolean_t free_now;
80 dig_lookup_t *default_lookup = NULL;
82 extern isc_boolean_t debugging, memdebugging;
83 static char *batchname = NULL;
84 static FILE *batchfp = NULL;
87 static char domainopt[DNS_NAME_MAXTEXT];
89 static isc_boolean_t short_form = ISC_FALSE, printcmd = ISC_TRUE,
90 ip6_int = ISC_FALSE, plusquest = ISC_FALSE, pluscomm = ISC_FALSE,
91 multiline = ISC_FALSE, nottl = ISC_FALSE, noclass = ISC_FALSE;
93 static const char *opcodetext[] = {
112 static const char *rcodetext[] = {
132 extern char *progname;
135 print_usage(FILE *fp) {
137 "Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}\n"
138 " {global-d-opt} host [@local-server] {local-d-opt}\n"
139 " [ host [@local-server] {local-d-opt} [...]]\n", fp);
145 fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
146 "for complete list of options\n", stderr);
152 fputs("DiG " VERSION "\n", stderr);
159 "Where: domain is in the Domain Name System\n"
160 " q-class is one of (in,hs,ch,...) [default: in]\n"
161 " q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
162 " (Use ixfr=version for type ixfr)\n"
163 " q-opt is one of:\n"
164 " -x dot-notation (shortcut for in-addr lookups)\n"
165 " -i (IP6.INT reverse IPv6 lookups)\n"
166 " -f filename (batch mode)\n"
167 " -b address[#port] (bind to source address/port)\n"
168 " -p port (specify port number)\n"
169 " -t type (specify query type)\n"
170 " -c class (specify query class)\n"
171 " -k keyfile (specify tsig key file)\n"
172 " -y name:key (specify named base64 tsig key)\n"
173 " -4 (use IPv4 query transport only)\n"
174 " -6 (use IPv6 query transport only)\n"
175 " d-opt is of the form +keyword[=value], where keyword is:\n"
176 " +[no]vc (TCP mode)\n"
177 " +[no]tcp (TCP mode, alternate syntax)\n"
178 " +time=### (Set query timeout) [5]\n"
179 " +tries=### (Set number of UDP attempts) [3]\n"
180 " +retry=### (Set number of UDP retries) [2]\n"
181 " +domain=### (Set default domainname)\n"
182 " +bufsize=### (Set EDNS0 Max UDP packet size)\n"
183 " +ndots=### (Set NDOTS value)\n"
184 " +[no]search (Set whether to use searchlist)\n"
185 " +[no]defname (Ditto)\n"
186 " +[no]recurse (Recursive mode)\n"
187 " +[no]ignore (Don't revert to TCP for TC responses.)"
189 " +[no]fail (Don't try next server on SERVFAIL)\n"
190 " +[no]besteffort (Try to parse even illegal messages)\n"
191 " +[no]aaonly (Set AA flag in query (+[no]aaflag))\n"
192 " +[no]adflag (Set AD flag in query)\n"
193 " +[no]cdflag (Set CD flag in query)\n"
194 " +[no]cl (Control display of class in records)\n"
195 " +[no]cmd (Control display of command line)\n"
196 " +[no]comments (Control display of comment lines)\n"
197 " +[no]question (Control display of question)\n"
198 " +[no]answer (Control display of answer)\n"
199 " +[no]authority (Control display of authority)\n"
200 " +[no]additional (Control display of additional)\n"
201 " +[no]stats (Control display of statistics)\n"
202 " +[no]short (Disable everything except short\n"
204 " +[no]ttlid (Control display of ttls in records)\n"
205 " +[no]all (Set or clear all display flags)\n"
206 " +[no]qr (Print question before sending)\n"
207 " +[no]nssearch (Search all authoritative nameservers)\n"
208 " +[no]identify (ID responders in short answers)\n"
209 " +[no]trace (Trace delegation down from root)\n"
210 " +[no]dnssec (Request DNSSEC records)\n"
212 " +[no]sigchase (Chase DNSSEC signatures)\n"
213 " +trusted-key=#### (Trusted Key when chasing DNSSEC sigs)\n"
215 " +[no]topdown (Do DNSSEC validation top down mode)\n"
218 " +[no]multiline (Print records in an expanded format)\n"
219 " global d-opts and servers (before host name) affect all queries.\n"
220 " local d-opts and servers (after host name) affect only that lookup.\n"
221 " -h (print help and exit)\n"
222 " -v (print version and exit)\n",
227 * Callback from dighost.c to print the received message.
230 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
234 char fromtext[ISC_SOCKADDR_FORMATSIZE];
236 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
240 if (query->lookup->stats && !short_form) {
241 diff = isc_time_microdiff(&now, &query->time_sent);
242 printf(";; Query time: %ld msec\n", (long int)diff/1000);
243 printf(";; SERVER: %s(%s)\n", fromtext, query->servname);
245 printf(";; WHEN: %s", ctime(&tnow));
246 if (query->lookup->doing_xfr) {
247 printf(";; XFR size: %u records (messages %u)\n",
248 query->rr_count, query->msg_count);
250 printf(";; MSG SIZE rcvd: %d\n", bytes);
255 puts(";; WARNING -- Some TSIG could not "
258 if ((key == NULL) && (keysecret[0] != 0)) {
259 puts(";; WARNING -- TSIG key was not used.");
262 } else if (query->lookup->identify && !short_form) {
263 diff = isc_time_microdiff(&now, &query->time_sent);
264 printf(";; Received %u bytes from %s(%s) in %d ms\n\n",
265 bytes, fromtext, query->servname,
271 * Callback from dighost.c to print that it is trying a server.
276 trying(char *frm, dig_lookup_t *lookup) {
282 * Internal print routine used to print short form replies.
285 say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
289 char store[sizeof("12345678901234567890")];
291 if (query->lookup->trace || query->lookup->ns_search_only) {
292 result = dns_rdatatype_totext(rdata->type, buf);
293 if (result != ISC_R_SUCCESS)
295 ADD_STRING(buf, " ");
297 result = dns_rdata_totext(rdata, NULL, buf);
298 check_result(result, "dns_rdata_totext");
299 if (query->lookup->identify) {
301 diff = isc_time_microdiff(&now, &query->time_sent);
302 ADD_STRING(buf, " from server ");
303 ADD_STRING(buf, query->servname);
304 snprintf(store, 19, " in %d ms.", (int)diff/1000);
305 ADD_STRING(buf, store);
307 ADD_STRING(buf, "\n");
308 return (ISC_R_SUCCESS);
312 * short_form message print handler. Calls above say_message()
315 short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
316 isc_buffer_t *buf, dig_query_t *query)
319 dns_rdataset_t *rdataset;
321 isc_result_t result, loopresult;
322 dns_name_t empty_name;
324 dns_rdata_t rdata = DNS_RDATA_INIT;
328 dns_name_init(&empty_name, NULL);
329 result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
330 if (result == ISC_R_NOMORE)
331 return (ISC_R_SUCCESS);
332 else if (result != ISC_R_SUCCESS)
337 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
339 isc_buffer_init(&target, t, sizeof(t));
341 for (rdataset = ISC_LIST_HEAD(name->list);
343 rdataset = ISC_LIST_NEXT(rdataset, link)) {
344 loopresult = dns_rdataset_first(rdataset);
345 while (loopresult == ISC_R_SUCCESS) {
346 dns_rdataset_current(rdataset, &rdata);
347 result = say_message(&rdata, query,
349 check_result(result, "say_message");
350 loopresult = dns_rdataset_next(rdataset);
351 dns_rdata_reset(&rdata);
354 result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
355 if (result == ISC_R_NOMORE)
357 else if (result != ISC_R_SUCCESS)
361 return (ISC_R_SUCCESS);
365 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
366 isc_buffer_t *target)
369 dns_master_style_t *style = NULL;
370 unsigned int styleflags = 0;
372 if (rdataset == NULL || owner_name == NULL || target == NULL)
375 styleflags |= DNS_STYLEFLAG_REL_OWNER;
377 styleflags |= DNS_STYLEFLAG_NO_TTL;
379 styleflags |= DNS_STYLEFLAG_NO_CLASS;
381 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
382 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
383 styleflags |= DNS_STYLEFLAG_REL_DATA;
384 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
385 styleflags |= DNS_STYLEFLAG_TTL;
386 styleflags |= DNS_STYLEFLAG_MULTILINE;
387 styleflags |= DNS_STYLEFLAG_COMMENT;
389 if (multiline || (nottl && noclass))
390 result = dns_master_stylecreate(&style, styleflags,
391 24, 24, 24, 32, 80, 8, mctx);
392 else if (nottl || noclass)
393 result = dns_master_stylecreate(&style, styleflags,
394 24, 24, 32, 40, 80, 8, mctx);
396 result = dns_master_stylecreate(&style, styleflags,
397 24, 32, 40, 48, 80, 8, mctx);
398 check_result(result, "dns_master_stylecreate");
400 result = dns_master_rdatasettotext(owner_name, rdataset, style, target);
403 dns_master_styledestroy(&style, mctx);
410 * Callback from dighost.c to print the reply from a server
413 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
415 dns_messagetextflag_t flags;
416 isc_buffer_t *buf = NULL;
417 unsigned int len = OUTPUTBUF;
418 dns_master_style_t *style = NULL;
419 unsigned int styleflags = 0;
421 styleflags |= DNS_STYLEFLAG_REL_OWNER;
423 styleflags |= DNS_STYLEFLAG_NO_TTL;
425 styleflags |= DNS_STYLEFLAG_NO_CLASS;
427 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
428 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
429 styleflags |= DNS_STYLEFLAG_REL_DATA;
430 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
431 styleflags |= DNS_STYLEFLAG_TTL;
432 styleflags |= DNS_STYLEFLAG_MULTILINE;
433 styleflags |= DNS_STYLEFLAG_COMMENT;
435 if (multiline || (nottl && noclass))
436 result = dns_master_stylecreate(&style, styleflags,
437 24, 24, 24, 32, 80, 8, mctx);
438 else if (nottl || noclass)
439 result = dns_master_stylecreate(&style, styleflags,
440 24, 24, 32, 40, 80, 8, mctx);
442 result = dns_master_stylecreate(&style, styleflags,
443 24, 32, 40, 48, 80, 8, mctx);
444 check_result(result, "dns_master_stylecreate");
446 if (query->lookup->cmdline[0] != 0) {
448 fputs(query->lookup->cmdline, stdout);
449 query->lookup->cmdline[0]=0;
451 debug("printmessage(%s %s %s)", headers ? "headers" : "noheaders",
452 query->lookup->comments ? "comments" : "nocomments",
453 short_form ? "short_form" : "long_form");
457 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
458 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
460 if (!query->lookup->comments)
461 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
463 result = ISC_R_SUCCESS;
465 result = isc_buffer_allocate(mctx, &buf, len);
466 check_result(result, "isc_buffer_allocate");
468 if (query->lookup->comments && !short_form) {
469 if (query->lookup->cmdline[0] != 0)
470 printf("; %s\n", query->lookup->cmdline);
471 if (msg == query->lookup->sendmsg)
472 printf(";; Sending:\n");
474 printf(";; Got answer:\n");
477 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
479 opcodetext[msg->opcode], rcodetext[msg->rcode],
482 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
484 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
486 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
488 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
490 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
492 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
494 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
497 printf("; QUERY: %u, ANSWER: %u, "
498 "AUTHORITY: %u, ADDITIONAL: %u\n",
499 msg->counts[DNS_SECTION_QUESTION],
500 msg->counts[DNS_SECTION_ANSWER],
501 msg->counts[DNS_SECTION_AUTHORITY],
502 msg->counts[DNS_SECTION_ADDITIONAL]);
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");
592 if (headers && query->lookup->comments && !short_form)
595 printf("%.*s", (int)isc_buffer_usedlength(buf),
596 (char *)isc_buffer_base(buf));
597 isc_buffer_free(&buf);
601 dns_master_styledestroy(&style, mctx);
606 * print the greeting message when the program first starts up.
609 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
612 static isc_boolean_t first = ISC_TRUE;
616 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
617 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
618 "%s; <<>> DiG " VERSION " <<>>",
622 snprintf(append, sizeof(append), " %s", argv[i++]);
623 remaining = sizeof(lookup->cmdline) -
624 strlen(lookup->cmdline) - 1;
625 strncat(lookup->cmdline, append, remaining);
627 remaining = sizeof(lookup->cmdline) -
628 strlen(lookup->cmdline) - 1;
629 strncat(lookup->cmdline, "\n", remaining);
631 snprintf(append, sizeof(append),
632 ";; global options: %s %s\n",
633 short_form ? "short_form" : "",
634 printcmd ? "printcmd" : "");
636 remaining = sizeof(lookup->cmdline) -
637 strlen(lookup->cmdline) - 1;
638 strncat(lookup->cmdline, append, remaining);
644 * Reorder an argument list so that server names all come at the end.
645 * This is a bit of a hack, to allow batch-mode processing to properly
646 * handle the server options.
649 reorder_args(int argc, char *argv[]) {
654 debug("reorder_args()");
656 while (argv[end][0] == '@') {
661 debug("arg[end]=%s", argv[end]);
662 for (i = 1; i < end - 1; i++) {
663 if (argv[i][0] == '@') {
664 debug("arg[%d]=%s", i, argv[i]);
666 for (j = i + 1; j < end; j++) {
667 debug("Moving %s to %d", argv[j], j - 1);
668 argv[j - 1] = argv[j];
670 debug("moving %s to end, %d", ptr, end - 1);
680 parse_uint(char *arg, const char *desc, isc_uint32_t max) {
684 result = isc_parse_uint32(&tmp, arg, 10);
685 if (result == ISC_R_SUCCESS && tmp > max)
686 result = ISC_R_RANGE;
687 if (result != ISC_R_SUCCESS)
688 fatal("%s '%s': %s", desc, arg, isc_result_totext(result));
693 * We're not using isc_commandline_parse() here since the command line
694 * syntax of dig is quite a bit different from that which can be described
700 plus_option(char *option, isc_boolean_t is_batchfile,
701 dig_lookup_t *lookup)
703 char option_store[256];
704 char *cmd, *value, *ptr;
705 isc_boolean_t state = ISC_TRUE;
710 strncpy(option_store, option, sizeof(option_store));
711 option_store[sizeof(option_store)-1]=0;
713 cmd = next_token(&ptr,"=");
715 printf(";; Invalid option %s\n", option_store);
719 if (strncasecmp(cmd, "no", 2)==0) {
724 #define FULLCHECK(A) \
726 size_t _l = strlen(cmd); \
727 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
728 goto invalid_option; \
730 #define FULLCHECK2(A, B) \
732 size_t _l = strlen(cmd); \
733 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
734 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
735 goto invalid_option; \
741 case 'a': /* aaonly / aaflag */
742 FULLCHECK2("aaonly", "aaflag");
743 lookup->aaonly = state;
747 case 'd': /* additional */
748 FULLCHECK("additional");
749 lookup->section_additional = state;
751 case 'f': /* adflag */
753 lookup->adflag = state;
761 lookup->section_question = state;
762 lookup->section_authority = state;
763 lookup->section_answer = state;
764 lookup->section_additional = state;
765 lookup->comments = state;
766 lookup->stats = state;
769 case 'n': /* answer */
771 lookup->section_answer = state;
773 case 'u': /* authority */
774 FULLCHECK("authority");
775 lookup->section_authority = state;
783 case 'e':/* besteffort */
784 FULLCHECK("besteffort");
785 lookup->besteffort = state;
787 case 'u':/* bufsize */
788 FULLCHECK("bufsize");
793 lookup->udpsize = (isc_uint16_t) parse_uint(value,
794 "buffer size", COMMSIZE);
802 case 'd':/* cdflag */
804 lookup->cdflag = state;
814 case 'o': /* comments */
815 FULLCHECK("comments");
816 lookup->comments = state;
817 if (lookup == default_lookup)
826 case 'e': /* defname */
827 FULLCHECK("defname");
830 case 'n': /* dnssec */
832 lookup->dnssec = state;
834 case 'o': /* domain */
840 strncpy(domainopt, value, sizeof(domainopt));
841 domainopt[sizeof(domainopt)-1] = '\0';
849 lookup->servfail_stops = state;
853 case 'd': /* identify */
854 FULLCHECK("identify");
855 lookup->identify = state;
857 case 'g': /* ignore */
858 default: /* Inherets default for compatibility */
860 lookup->ignore = ISC_TRUE;
863 case 'm': /* multiline */
864 FULLCHECK("multiline");
869 case 'd': /* ndots */
875 ndots = parse_uint(value, "ndots", MAXNDOTS);
877 case 's': /* nssearch */
878 FULLCHECK("nssearch");
879 lookup->ns_search_only = state;
881 lookup->trace_root = ISC_TRUE;
882 lookup->recurse = ISC_FALSE;
883 lookup->identify = ISC_TRUE;
884 lookup->stats = ISC_FALSE;
885 lookup->comments = ISC_FALSE;
886 lookup->section_additional = ISC_FALSE;
887 lookup->section_authority = ISC_FALSE;
888 lookup->section_question = ISC_FALSE;
889 lookup->rdtype = dns_rdatatype_ns;
890 lookup->rdtypeset = ISC_TRUE;
891 short_form = ISC_TRUE;
904 case 'u': /* question */
905 FULLCHECK("question");
906 lookup->section_question = state;
907 if (lookup == default_lookup)
918 case 'c': /* recurse */
919 FULLCHECK("recurse");
920 lookup->recurse = state;
922 case 't': /* retry / retries */
923 FULLCHECK2("retry", "retries");
928 lookup->retries = parse_uint(value, "retries",
942 case 'e': /* search */
946 case 'h': /* short */
950 printcmd = ISC_FALSE;
951 lookup->section_additional = ISC_FALSE;
952 lookup->section_answer = ISC_TRUE;
953 lookup->section_authority = ISC_FALSE;
954 lookup->section_question = ISC_FALSE;
955 lookup->comments = ISC_FALSE;
956 lookup->stats = ISC_FALSE;
960 case 'i': /* sigchase */
961 FULLCHECK("sigchase");
962 lookup->sigchase = state;
963 if (lookup->sigchase)
964 lookup->dnssec = ISC_TRUE;
967 case 't': /* stats */
969 lookup->stats = state;
980 lookup->tcp_mode = state;
982 case 'i': /* timeout */
983 FULLCHECK("timeout");
988 timeout = parse_uint(value, "timeout", MAXTIMEOUT);
993 case 'o': /* topdown */
994 FULLCHECK("topdown");
995 lookup->do_topdown = state;
1000 case 'a': /* trace */
1002 lookup->trace = state;
1003 lookup->trace_root = state;
1005 lookup->recurse = ISC_FALSE;
1006 lookup->identify = ISC_TRUE;
1007 lookup->comments = ISC_FALSE;
1008 lookup->stats = ISC_FALSE;
1009 lookup->section_additional = ISC_FALSE;
1010 lookup->section_authority = ISC_TRUE;
1011 lookup->section_question = ISC_FALSE;
1014 case 'i': /* tries */
1019 goto invalid_option;
1020 lookup->retries = parse_uint(value, "tries",
1022 if (lookup->retries == 0)
1023 lookup->retries = 1;
1026 case 'u': /* 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 */
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 *firstarg,
1072 int argc, char **argv)
1074 char opt, *value, *ptr;
1075 isc_result_t result;
1076 isc_boolean_t value_from_next;
1077 isc_textregion_t tr;
1078 dns_rdatatype_t rdtype;
1079 dns_rdataclass_t rdclass;
1080 char textname[MXNAME];
1082 struct in6_addr in6;
1086 while (strpbrk(option, single_dash_opts) == &option[0]) {
1088 * Since the -[46dhimnv] options do not take an argument,
1089 * account for them (in any number and/or combination)
1090 * if they appear as the first character(s) of a q-opt.
1096 isc_net_disableipv6();
1097 have_ipv6 = ISC_FALSE;
1099 fatal("can't find IPv4 networking");
1105 isc_net_disableipv4();
1106 have_ipv4 = ISC_FALSE;
1108 fatal("can't find IPv6 networking");
1113 ptr = strpbrk(&option[1], dash_opts);
1114 if (ptr != &option[1]) {
1117 debugging = ISC_TRUE;
1120 debugging = ISC_TRUE;
1129 case 'm': /* memdebug */
1130 /* memdebug is handled in preparse_args() */
1140 if (strlen(option) > 1U)
1141 option = &option[1];
1146 if (strlen(option) > 1U) {
1147 value_from_next = ISC_FALSE;
1150 value_from_next = ISC_TRUE;
1154 goto invalid_option;
1157 hash = strchr(value, '#');
1159 srcport = (in_port_t)
1160 parse_uint(hash + 1,
1161 "port number", MAXPORT);
1165 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1166 isc_sockaddr_fromin6(&bind_address, &in6, srcport);
1167 isc_net_disableipv4();
1168 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1169 isc_sockaddr_fromin(&bind_address, &in4, srcport);
1170 isc_net_disableipv6();
1174 fatal("invalid address %s", value);
1178 specified_source = ISC_TRUE;
1179 return (value_from_next);
1181 if ((*lookup)->rdclassset) {
1182 fprintf(stderr, ";; Warning, extra class option\n");
1184 *open_type_class = ISC_FALSE;
1186 tr.length = strlen(value);
1187 result = dns_rdataclass_fromtext(&rdclass,
1188 (isc_textregion_t *)&tr);
1189 if (result == ISC_R_SUCCESS) {
1190 (*lookup)->rdclass = rdclass;
1191 (*lookup)->rdclassset = ISC_TRUE;
1193 fprintf(stderr, ";; Warning, ignoring "
1194 "invalid class %s\n",
1196 return (value_from_next);
1199 return (value_from_next);
1201 strncpy(keyfile, value, sizeof(keyfile));
1202 keyfile[sizeof(keyfile)-1]=0;
1203 return (value_from_next);
1205 port = (in_port_t) parse_uint(value, "port number", MAXPORT);
1206 return (value_from_next);
1208 *open_type_class = ISC_FALSE;
1209 if (strncasecmp(value, "ixfr=", 5) == 0) {
1210 rdtype = dns_rdatatype_ixfr;
1211 result = ISC_R_SUCCESS;
1214 tr.length = strlen(value);
1215 result = dns_rdatatype_fromtext(&rdtype,
1216 (isc_textregion_t *)&tr);
1217 if (result == ISC_R_SUCCESS &&
1218 rdtype == dns_rdatatype_ixfr) {
1219 result = DNS_R_UNKNOWN;
1222 if (result == ISC_R_SUCCESS) {
1223 if ((*lookup)->rdtypeset) {
1224 fprintf(stderr, ";; Warning, "
1225 "extra type option\n");
1227 if (rdtype == dns_rdatatype_ixfr) {
1228 (*lookup)->rdtype = dns_rdatatype_ixfr;
1229 (*lookup)->rdtypeset = ISC_TRUE;
1230 (*lookup)->ixfr_serial =
1231 parse_uint(&value[5], "serial number",
1233 (*lookup)->section_question = plusquest;
1234 (*lookup)->comments = pluscomm;
1236 (*lookup)->rdtype = rdtype;
1237 (*lookup)->rdtypeset = ISC_TRUE;
1238 if (rdtype == dns_rdatatype_axfr) {
1239 (*lookup)->section_question = plusquest;
1240 (*lookup)->comments = pluscomm;
1242 (*lookup)->ixfr_serial = ISC_FALSE;
1245 fprintf(stderr, ";; Warning, ignoring "
1246 "invalid type %s\n",
1248 return (value_from_next);
1250 ptr = next_token(&value,":");
1254 strncpy(keynametext, ptr, sizeof(keynametext));
1255 keynametext[sizeof(keynametext)-1]=0;
1256 ptr = next_token(&value, "");
1259 strncpy(keysecret, ptr, sizeof(keysecret));
1260 keysecret[sizeof(keysecret)-1]=0;
1261 return (value_from_next);
1263 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1264 if (get_reverse(textname, sizeof(textname), value,
1265 ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
1266 strncpy((*lookup)->textname, textname,
1267 sizeof((*lookup)->textname));
1268 debug("looking up %s", (*lookup)->textname);
1269 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1270 (*lookup)->ns_search_only);
1271 (*lookup)->ip6_int = ip6_int;
1272 if (!(*lookup)->rdtypeset)
1273 (*lookup)->rdtype = dns_rdatatype_ptr;
1274 if (!(*lookup)->rdclassset)
1275 (*lookup)->rdclass = dns_rdataclass_in;
1276 (*lookup)->new_search = ISC_TRUE;
1277 if (*lookup && *firstarg) {
1278 printgreeting(argc, argv, *lookup);
1279 *firstarg = ISC_FALSE;
1281 ISC_LIST_APPEND(lookup_list, *lookup, link);
1283 fprintf(stderr, "Invalid IP address %s\n", value);
1286 return (value_from_next);
1289 fprintf(stderr, "Invalid option: -%s\n", option);
1296 * Because we may be trying to do memory allocation recording, we're going
1297 * to need to parse the arguments for the -m *before* we start the main
1298 * argument parsing routine.
1299 * I'd prefer not to have to do this, but I am not quite sure how else to
1300 * fix the problem. Argument parsing in dig involves memory allocation
1301 * by its nature, so it can't be done in the main argument parser.
1304 preparse_args(int argc, char **argv) {
1311 for (rc--, rv++; rc > 0; rc--, rv++) {
1312 if (rv[0][0] != '-')
1315 while (strpbrk(option, single_dash_opts) == &option[0]) {
1316 if (option[0] == 'm') {
1317 memdebugging = ISC_TRUE;
1318 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1319 ISC_MEM_DEBUGRECORD;
1322 option = &option[1];
1328 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1329 int argc, char **argv) {
1330 isc_result_t result;
1331 isc_textregion_t tr;
1332 isc_boolean_t firstarg = ISC_TRUE;
1333 dig_server_t *srv = NULL;
1334 dig_lookup_t *lookup = NULL;
1335 dns_rdatatype_t rdtype;
1336 dns_rdataclass_t rdclass;
1337 isc_boolean_t open_type_class = ISC_TRUE;
1338 char batchline[MXNAME];
1350 * The semantics for parsing the args is a bit complex; if
1351 * we don't have a host yet, make the arg apply globally,
1352 * otherwise make it apply to the latest host. This is
1353 * a bit different than the previous versions, but should
1354 * form a consistent user interface.
1356 * First, create a "default lookup" which won't actually be used
1357 * anywhere, except for cloning into new lookups
1360 debug("parse_args()");
1361 if (!is_batchfile) {
1362 debug("making new lookup");
1363 default_lookup = make_empty_lookup();
1367 * Treat ${HOME}/.digrc as a special batchfile
1369 INSIST(batchfp == NULL);
1370 homedir = getenv("HOME");
1371 if (homedir != NULL) {
1373 n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc",
1375 if (n < sizeof(rcfile))
1376 batchfp = fopen(rcfile, "r");
1378 if (batchfp != NULL) {
1379 while (fgets(batchline, sizeof(batchline),
1381 debug("config line %s", batchline);
1384 bargv[bargc] = next_token(&input, " \t\r\n");
1385 while ((bargv[bargc] != NULL) &&
1389 next_token(&input, " \t\r\n");
1395 reorder_args(bargc, (char **)bargv);
1396 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1404 lookup = default_lookup;
1408 for (rc--, rv++; rc > 0; rc--, rv++) {
1409 debug("main parsing %s", rv[0]);
1410 if (strncmp(rv[0], "%", 1) == 0)
1412 if (strncmp(rv[0], "@", 1) == 0) {
1413 srv = make_server(&rv[0][1]);
1414 ISC_LIST_APPEND(lookup->my_server_list,
1416 } else if (rv[0][0] == '+') {
1417 plus_option(&rv[0][1], is_batchfile,
1419 } else if (rv[0][0] == '-') {
1421 if (dash_option(&rv[0][1], NULL,
1422 &lookup, &open_type_class,
1423 &firstarg, argc, argv)) {
1428 if (dash_option(&rv[0][1], rv[1],
1429 &lookup, &open_type_class,
1430 &firstarg, argc, argv)) {
1437 * Anything which isn't an option
1439 if (open_type_class) {
1440 if (strncmp(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,
1502 printgreeting(argc, argv, lookup);
1503 firstarg = ISC_FALSE;
1505 strncpy(lookup->textname, rv[0],
1506 sizeof(lookup->textname));
1507 lookup->textname[sizeof(lookup->textname)-1]=0;
1508 lookup->trace_root = ISC_TF(lookup->trace ||
1509 lookup->ns_search_only);
1510 lookup->new_search = ISC_TRUE;
1511 ISC_LIST_APPEND(lookup_list, lookup, link);
1512 debug("looking up %s", lookup->textname);
1514 /* XXX Error message */
1518 * If we have a batchfile, seed the lookup list with the
1519 * first entry, then trust the callback in dighost_shutdown
1522 if ((batchname != NULL) && !(is_batchfile)) {
1523 if (strcmp(batchname, "-") == 0)
1526 batchfp = fopen(batchname, "r");
1527 if (batchfp == NULL) {
1531 fatal("couldn't open specified batch file");
1533 /* XXX Remove code dup from shutdown code */
1535 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1537 debug("batch line %s", batchline);
1538 if (batchline[0] == '\r' || batchline[0] == '\n'
1539 || batchline[0] == '#' || batchline[0] == ';')
1542 bargv[bargc] = next_token(&input, " \t\r\n");
1543 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1545 bargv[bargc] = next_token(&input, " \t\r\n");
1551 reorder_args(bargc, (char **)bargv);
1552 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1556 * If no lookup specified, search for root
1558 if ((lookup_list.head == NULL) && !config_only) {
1559 lookup = clone_lookup(default_lookup, ISC_TRUE);
1560 lookup->trace_root = ISC_TF(lookup->trace ||
1561 lookup->ns_search_only);
1562 lookup->new_search = ISC_TRUE;
1563 strcpy(lookup->textname, ".");
1564 lookup->rdtype = dns_rdatatype_ns;
1565 lookup->rdtypeset = ISC_TRUE;
1567 printgreeting(argc, argv, lookup);
1568 firstarg = ISC_FALSE;
1570 ISC_LIST_APPEND(lookup_list, lookup, link);
1575 * Callback from dighost.c to allow program-specific shutdown code.
1576 * Here, we're possibly reading from a batch file, then shutting down
1577 * for real if there's nothing in the batch file to read.
1580 dighost_shutdown(void) {
1581 char batchline[MXNAME];
1587 if (batchname == NULL) {
1593 if (feof(batchfp)) {
1596 if (batchfp != stdin)
1601 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1602 debug("batch line %s", batchline);
1605 bargv[bargc] = next_token(&input, " \t\r\n");
1606 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1608 bargv[bargc] = next_token(&input, " \t\r\n");
1613 reorder_args(bargc, (char **)bargv);
1614 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1618 if (batchfp != stdin)
1626 main(int argc, char **argv) {
1627 isc_result_t result;
1628 dig_server_t *s, *s2;
1630 ISC_LIST_INIT(lookup_list);
1631 ISC_LIST_INIT(server_list);
1632 ISC_LIST_INIT(search_list);
1635 preparse_args(argc, argv);
1637 result = isc_app_start();
1638 check_result(result, "isc_app_start");
1640 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1642 if (domainopt[0] != '\0') {
1643 set_search_domain(domainopt);
1644 usesearch = ISC_TRUE;
1646 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1647 check_result(result, "isc_app_onrun");
1649 s = ISC_LIST_HEAD(default_lookup->my_server_list);
1651 debug("freeing server %p belonging to %p",
1654 s = ISC_LIST_NEXT(s, link);
1655 ISC_LIST_DEQUEUE(default_lookup->my_server_list, s2, link);
1656 isc_mem_free(mctx, s2);
1658 isc_mem_free(mctx, default_lookup);
1659 if (batchname != NULL) {
1660 if (batchfp != stdin)