2 * Copyright (C) 2004-2013 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: dig.c,v 1.237.124.4 2011/12/07 17:23:55 each 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>
49 #define ADD_STRING(b, s) { \
50 if (strlen(s) >= isc_buffer_availablelength(b)) \
51 return (ISC_R_NOSPACE); \
53 isc_buffer_putstr(b, s); \
56 #define DIG_MAX_ADDRESSES 20
58 dig_lookup_t *default_lookup = NULL;
60 static char *batchname = NULL;
61 static FILE *batchfp = NULL;
63 static int addresscount = 0;
65 static char domainopt[DNS_NAME_MAXTEXT];
67 static isc_boolean_t short_form = ISC_FALSE, printcmd = ISC_TRUE,
68 ip6_int = ISC_FALSE, plusquest = ISC_FALSE, pluscomm = ISC_FALSE,
69 multiline = ISC_FALSE, nottl = ISC_FALSE, noclass = ISC_FALSE,
73 static const char * const opcodetext[] = {
92 /*% return code text */
93 static const char * const rcodetext[] = {
113 /*% safe rcodetext[] */
115 rcode_totext(dns_rcode_t rcode)
117 static char buf[sizeof("?65535")];
119 const char *consttext;
123 if (rcode >= (sizeof(rcodetext)/sizeof(rcodetext[0]))) {
124 snprintf(buf, sizeof(buf), "?%u", rcode);
125 totext.deconsttext = buf;
127 totext.consttext = rcodetext[rcode];
128 return totext.deconsttext;
133 print_usage(FILE *fp) {
135 "Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}\n"
136 " {global-d-opt} host [@local-server] {local-d-opt}\n"
137 " [ host [@local-server] {local-d-opt} [...]]\n", fp);
140 ISC_PLATFORM_NORETURN_PRE static void
141 usage(void) ISC_PLATFORM_NORETURN_POST;
146 fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
147 "for complete list of options\n", stderr);
154 fputs("DiG " VERSION "\n", stderr);
162 "Where: domain is in the Domain Name System\n"
163 " q-class is one of (in,hs,ch,...) [default: in]\n"
164 " q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
165 " (Use ixfr=version for type ixfr)\n"
166 " q-opt is one of:\n"
167 " -x dot-notation (shortcut for reverse lookups)\n"
168 " -i (use IP6.INT for IPv6 reverse lookups)\n"
169 " -f filename (batch mode)\n"
170 " -b address[#port] (bind to source address/port)\n"
171 " -p port (specify port number)\n"
172 " -q name (specify query name)\n"
173 " -t type (specify query type)\n"
174 " -c class (specify query class)\n"
175 " -k keyfile (specify tsig key file)\n"
176 " -y [hmac:]name:key (specify named base64 tsig key)\n"
177 " -4 (use IPv4 query transport only)\n"
178 " -6 (use IPv6 query transport only)\n"
179 " -m (enable memory usage debugging)\n"
180 " d-opt is of the form +keyword[=value], where keyword is:\n"
181 " +[no]vc (TCP mode)\n"
182 " +[no]tcp (TCP mode, alternate syntax)\n"
183 " +time=### (Set query timeout) [5]\n"
184 " +tries=### (Set number of UDP attempts) [3]\n"
185 " +retry=### (Set number of UDP retries) [2]\n"
186 " +domain=### (Set default domainname)\n"
187 " +bufsize=### (Set EDNS0 Max UDP packet size)\n"
188 " +ndots=### (Set NDOTS value)\n"
189 " +[no]edns[=###] (Set EDNS version) [0]\n"
190 " +[no]search (Set whether to use searchlist)\n"
191 " +[no]showsearch (Search with intermediate results)\n"
192 " +[no]defname (Ditto)\n"
193 " +[no]recurse (Recursive mode)\n"
194 " +[no]ignore (Don't revert to TCP for TC responses.)"
196 " +[no]fail (Don't try next server on SERVFAIL)\n"
197 " +[no]besteffort (Try to parse even illegal messages)\n"
198 " +[no]aaonly (Set AA flag in query (+[no]aaflag))\n"
199 " +[no]adflag (Set AD flag in query)\n"
200 " +[no]cdflag (Set CD flag in query)\n"
201 " +[no]cl (Control display of class in records)\n"
202 " +[no]cmd (Control display of command line)\n"
203 " +[no]comments (Control display of comment lines)\n"
204 " +[no]question (Control display of question)\n"
205 " +[no]answer (Control display of answer)\n"
206 " +[no]authority (Control display of authority)\n"
207 " +[no]additional (Control display of additional)\n"
208 " +[no]stats (Control display of statistics)\n"
209 " +[no]short (Disable everything except short\n"
211 " +[no]ttlid (Control display of ttls in records)\n"
212 " +[no]all (Set or clear all display flags)\n"
213 " +[no]qr (Print question before sending)\n"
214 " +[no]nssearch (Search all authoritative nameservers)\n"
215 " +[no]identify (ID responders in short answers)\n"
216 " +[no]trace (Trace delegation down from root)\n"
217 " +[no]dnssec (Request DNSSEC records)\n"
218 " +[no]nsid (Request Name Server ID)\n"
220 " +[no]sigchase (Chase DNSSEC signatures)\n"
221 " +trusted-key=#### (Trusted Key when chasing DNSSEC sigs)\n"
223 " +[no]topdown (Do DNSSEC validation top down mode)\n"
226 " +[no]multiline (Print records in an expanded format)\n"
227 " +[no]onesoa (AXFR prints only one soa record)\n"
228 " +[no]keepopen (Keep the TCP socket open between queries)\n"
229 " global d-opts and servers (before host name) affect all queries.\n"
230 " local d-opts and servers (after host name) affect only that lookup.\n"
231 " -h (print help and exit)\n"
232 " -v (print version and exit)\n",
237 * Callback from dighost.c to print the received message.
240 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
246 char fromtext[ISC_SOCKADDR_FORMATSIZE];
248 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
252 if (query->lookup->stats && !short_form) {
253 diff = isc_time_microdiff(&now, &query->time_sent);
254 printf(";; Query time: %ld msec\n", (long int)diff/1000);
255 printf(";; SERVER: %s(%s)\n", fromtext, query->servname);
257 tmnow = *localtime(&tnow);
258 if (strftime(time_str, sizeof(time_str),
259 "%a %b %d %H:%M:%S %Z %Y", &tmnow) > 0U)
260 printf(";; WHEN: %s\n", time_str);
261 if (query->lookup->doing_xfr) {
262 printf(";; XFR size: %u records (messages %u, "
263 "bytes %" ISC_PRINT_QUADFORMAT "u)\n",
264 query->rr_count, query->msg_count,
267 printf(";; MSG SIZE rcvd: %u\n", bytes);
271 puts(";; WARNING -- Some TSIG could not "
274 if ((key == NULL) && (keysecret[0] != 0)) {
275 puts(";; WARNING -- TSIG key was not used.");
278 } else if (query->lookup->identify && !short_form) {
279 diff = isc_time_microdiff(&now, &query->time_sent);
280 printf(";; Received %" ISC_PRINT_QUADFORMAT "u bytes "
281 "from %s(%s) in %d ms\n\n",
282 query->lookup->doing_xfr ?
283 query->byte_count : (isc_uint64_t)bytes,
284 fromtext, query->userarg,
290 * Callback from dighost.c to print that it is trying a server.
295 trying(char *frm, dig_lookup_t *lookup) {
301 * Internal print routine used to print short form replies.
304 say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
308 char store[sizeof("12345678901234567890")];
310 if (query->lookup->trace || query->lookup->ns_search_only) {
311 result = dns_rdatatype_totext(rdata->type, buf);
312 if (result != ISC_R_SUCCESS)
314 ADD_STRING(buf, " ");
316 result = dns_rdata_totext(rdata, NULL, buf);
317 if (result == ISC_R_NOSPACE)
319 check_result(result, "dns_rdata_totext");
320 if (query->lookup->identify) {
322 diff = isc_time_microdiff(&now, &query->time_sent);
323 ADD_STRING(buf, " from server ");
324 ADD_STRING(buf, query->servname);
325 snprintf(store, 19, " in %d ms.", (int)diff/1000);
326 ADD_STRING(buf, store);
328 ADD_STRING(buf, "\n");
329 return (ISC_R_SUCCESS);
333 * short_form message print handler. Calls above say_message()
336 short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
337 isc_buffer_t *buf, dig_query_t *query)
340 dns_rdataset_t *rdataset;
341 isc_result_t result, loopresult;
342 dns_name_t empty_name;
343 dns_rdata_t rdata = DNS_RDATA_INIT;
347 dns_name_init(&empty_name, NULL);
348 result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
349 if (result == ISC_R_NOMORE)
350 return (ISC_R_SUCCESS);
351 else if (result != ISC_R_SUCCESS)
356 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
358 for (rdataset = ISC_LIST_HEAD(name->list);
360 rdataset = ISC_LIST_NEXT(rdataset, link)) {
361 loopresult = dns_rdataset_first(rdataset);
362 while (loopresult == ISC_R_SUCCESS) {
363 dns_rdataset_current(rdataset, &rdata);
364 result = say_message(&rdata, query,
366 if (result == ISC_R_NOSPACE)
368 check_result(result, "say_message");
369 loopresult = dns_rdataset_next(rdataset);
370 dns_rdata_reset(&rdata);
373 result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
374 if (result == ISC_R_NOMORE)
376 else if (result != ISC_R_SUCCESS)
380 return (ISC_R_SUCCESS);
384 printrdataset(dns_name_t *owner_name, dns_rdataset_t *rdataset,
385 isc_buffer_t *target)
388 dns_master_style_t *style = NULL;
389 unsigned int styleflags = 0;
391 if (rdataset == NULL || owner_name == NULL || target == NULL)
394 styleflags |= DNS_STYLEFLAG_REL_OWNER;
396 styleflags |= DNS_STYLEFLAG_NO_TTL;
398 styleflags |= DNS_STYLEFLAG_NO_CLASS;
400 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
401 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
402 styleflags |= DNS_STYLEFLAG_REL_DATA;
403 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
404 styleflags |= DNS_STYLEFLAG_TTL;
405 styleflags |= DNS_STYLEFLAG_MULTILINE;
406 styleflags |= DNS_STYLEFLAG_COMMENT;
408 if (multiline || (nottl && noclass))
409 result = dns_master_stylecreate(&style, styleflags,
410 24, 24, 24, 32, 80, 8, mctx);
411 else if (nottl || noclass)
412 result = dns_master_stylecreate(&style, styleflags,
413 24, 24, 32, 40, 80, 8, mctx);
415 result = dns_master_stylecreate(&style, styleflags,
416 24, 32, 40, 48, 80, 8, mctx);
417 check_result(result, "dns_master_stylecreate");
419 result = dns_master_rdatasettotext(owner_name, rdataset, style, target);
422 dns_master_styledestroy(&style, mctx);
429 * Callback from dighost.c to print the reply from a server
432 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
434 dns_messagetextflag_t flags;
435 isc_buffer_t *buf = NULL;
436 unsigned int len = OUTPUTBUF;
437 dns_master_style_t *style = NULL;
438 unsigned int styleflags = 0;
440 styleflags |= DNS_STYLEFLAG_REL_OWNER;
442 styleflags |= DNS_STYLEFLAG_NO_TTL;
444 styleflags |= DNS_STYLEFLAG_NO_CLASS;
446 styleflags |= DNS_STYLEFLAG_OMIT_OWNER;
447 styleflags |= DNS_STYLEFLAG_OMIT_CLASS;
448 styleflags |= DNS_STYLEFLAG_REL_DATA;
449 styleflags |= DNS_STYLEFLAG_OMIT_TTL;
450 styleflags |= DNS_STYLEFLAG_TTL;
451 styleflags |= DNS_STYLEFLAG_MULTILINE;
452 styleflags |= DNS_STYLEFLAG_COMMENT;
454 if (multiline || (nottl && noclass))
455 result = dns_master_stylecreate(&style, styleflags,
456 24, 24, 24, 32, 80, 8, mctx);
457 else if (nottl || noclass)
458 result = dns_master_stylecreate(&style, styleflags,
459 24, 24, 32, 40, 80, 8, mctx);
461 result = dns_master_stylecreate(&style, styleflags,
462 24, 32, 40, 48, 80, 8, mctx);
463 check_result(result, "dns_master_stylecreate");
465 if (query->lookup->cmdline[0] != 0) {
467 fputs(query->lookup->cmdline, stdout);
468 query->lookup->cmdline[0]=0;
470 debug("printmessage(%s %s %s)", headers ? "headers" : "noheaders",
471 query->lookup->comments ? "comments" : "nocomments",
472 short_form ? "short_form" : "long_form");
476 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
477 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
479 if (onesoa && query->lookup->rdtype == dns_rdatatype_axfr)
480 flags |= (query->msg_count == 0) ? DNS_MESSAGETEXTFLAG_ONESOA :
481 DNS_MESSAGETEXTFLAG_OMITSOA;
482 if (!query->lookup->comments)
483 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
485 result = isc_buffer_allocate(mctx, &buf, len);
486 check_result(result, "isc_buffer_allocate");
488 if (query->lookup->comments && !short_form) {
489 if (query->lookup->cmdline[0] != 0)
490 printf("; %s\n", query->lookup->cmdline);
491 if (msg == query->lookup->sendmsg)
492 printf(";; Sending:\n");
494 printf(";; Got answer:\n");
497 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
499 opcodetext[msg->opcode],
500 rcode_totext(msg->rcode),
503 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
505 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
507 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
509 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
511 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
513 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
515 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
517 if ((msg->flags & 0x0040U) != 0)
518 printf("; MBZ: 0x4");
520 printf("; QUERY: %u, ANSWER: %u, "
521 "AUTHORITY: %u, ADDITIONAL: %u\n",
522 msg->counts[DNS_SECTION_QUESTION],
523 msg->counts[DNS_SECTION_ANSWER],
524 msg->counts[DNS_SECTION_AUTHORITY],
525 msg->counts[DNS_SECTION_ADDITIONAL]);
527 if (msg != query->lookup->sendmsg &&
528 (msg->flags & DNS_MESSAGEFLAG_RD) != 0 &&
529 (msg->flags & DNS_MESSAGEFLAG_RA) == 0)
530 printf(";; WARNING: recursion requested "
531 "but not available\n");
533 if (msg != query->lookup->sendmsg &&
534 query->lookup->edns != -1 && msg->opt == NULL &&
535 (msg->rcode == dns_rcode_formerr ||
536 msg->rcode == dns_rcode_notimp))
537 printf("\n;; WARNING: EDNS query returned status "
538 "%s - retry with '%s+noedns'\n",
539 rcode_totext(msg->rcode),
540 query->lookup->dnssec ? "+nodnssec ": "");
541 if (msg != query->lookup->sendmsg && extrabytes != 0U)
542 printf(";; WARNING: Message has %u extra byte%s at "
543 "end\n", extrabytes, extrabytes != 0 ? "s" : "");
548 if (query->lookup->comments && headers && !short_form) {
549 result = dns_message_pseudosectiontotext(msg,
550 DNS_PSEUDOSECTION_OPT,
552 if (result == ISC_R_NOSPACE) {
555 isc_buffer_free(&buf);
556 result = isc_buffer_allocate(mctx, &buf, len);
557 if (result == ISC_R_SUCCESS)
558 goto repopulate_buffer;
563 "dns_message_pseudosectiontotext");
566 if (query->lookup->section_question && headers) {
568 result = dns_message_sectiontotext(msg,
569 DNS_SECTION_QUESTION,
571 if (result == ISC_R_NOSPACE)
573 check_result(result, "dns_message_sectiontotext");
576 if (query->lookup->section_answer) {
578 result = dns_message_sectiontotext(msg,
581 if (result == ISC_R_NOSPACE)
583 check_result(result, "dns_message_sectiontotext");
585 result = short_answer(msg, flags, buf, query);
586 if (result == ISC_R_NOSPACE)
588 check_result(result, "short_answer");
591 if (query->lookup->section_authority) {
593 result = dns_message_sectiontotext(msg,
594 DNS_SECTION_AUTHORITY,
596 if (result == ISC_R_NOSPACE)
598 check_result(result, "dns_message_sectiontotext");
601 if (query->lookup->section_additional) {
603 result = dns_message_sectiontotext(msg,
604 DNS_SECTION_ADDITIONAL,
606 if (result == ISC_R_NOSPACE)
608 check_result(result, "dns_message_sectiontotext");
610 * Only print the signature on the first record.
613 result = dns_message_pseudosectiontotext(
615 DNS_PSEUDOSECTION_TSIG,
617 if (result == ISC_R_NOSPACE)
620 "dns_message_pseudosectiontotext");
621 result = dns_message_pseudosectiontotext(
623 DNS_PSEUDOSECTION_SIG0,
625 if (result == ISC_R_NOSPACE)
628 "dns_message_pseudosectiontotext");
633 if (headers && query->lookup->comments && !short_form)
636 printf("%.*s", (int)isc_buffer_usedlength(buf),
637 (char *)isc_buffer_base(buf));
638 isc_buffer_free(&buf);
642 dns_master_styledestroy(&style, mctx);
647 * print the greeting message when the program first starts up.
650 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
653 static isc_boolean_t first = ISC_TRUE;
657 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
658 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
659 "%s; <<>> DiG " VERSION " <<>>",
663 snprintf(append, sizeof(append), " %s", argv[i++]);
664 remaining = sizeof(lookup->cmdline) -
665 strlen(lookup->cmdline) - 1;
666 strncat(lookup->cmdline, append, remaining);
668 remaining = sizeof(lookup->cmdline) -
669 strlen(lookup->cmdline) - 1;
670 strncat(lookup->cmdline, "\n", remaining);
671 if (first && addresscount != 0) {
672 snprintf(append, sizeof(append),
673 "; (%d server%s found)\n",
675 addresscount > 1 ? "s" : "");
676 remaining = sizeof(lookup->cmdline) -
677 strlen(lookup->cmdline) - 1;
678 strncat(lookup->cmdline, append, remaining);
681 snprintf(append, sizeof(append),
682 ";; global options:%s%s\n",
683 short_form ? " +short" : "",
684 printcmd ? " +cmd" : "");
686 remaining = sizeof(lookup->cmdline) -
687 strlen(lookup->cmdline) - 1;
688 strncat(lookup->cmdline, append, remaining);
694 * We're not using isc_commandline_parse() here since the command line
695 * syntax of dig is quite a bit different from that which can be described
701 plus_option(char *option, isc_boolean_t is_batchfile,
702 dig_lookup_t *lookup)
705 char option_store[256];
706 char *cmd, *value, *ptr;
708 isc_boolean_t state = ISC_TRUE;
713 strncpy(option_store, option, sizeof(option_store));
714 option_store[sizeof(option_store)-1]=0;
716 cmd = next_token(&ptr,"=");
718 printf(";; Invalid option %s\n", option_store);
722 if (strncasecmp(cmd, "no", 2)==0) {
727 #define FULLCHECK(A) \
729 size_t _l = strlen(cmd); \
730 if (_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) \
731 goto invalid_option; \
733 #define FULLCHECK2(A, B) \
735 size_t _l = strlen(cmd); \
736 if ((_l >= sizeof(A) || strncasecmp(cmd, A, _l) != 0) && \
737 (_l >= sizeof(B) || strncasecmp(cmd, B, _l) != 0)) \
738 goto invalid_option; \
744 case 'a': /* aaonly / aaflag */
745 FULLCHECK2("aaonly", "aaflag");
746 lookup->aaonly = state;
750 case 'd': /* additional */
751 FULLCHECK("additional");
752 lookup->section_additional = state;
754 case 'f': /* adflag */
755 case '\0': /* +ad is a synonym for +adflag */
757 lookup->adflag = state;
765 lookup->section_question = state;
766 lookup->section_authority = state;
767 lookup->section_answer = state;
768 lookup->section_additional = state;
769 lookup->comments = state;
770 lookup->stats = state;
773 case 'n': /* answer */
775 lookup->section_answer = state;
777 case 'u': /* authority */
778 FULLCHECK("authority");
779 lookup->section_authority = state;
787 case 'e':/* besteffort */
788 FULLCHECK("besteffort");
789 lookup->besteffort = state;
791 case 'u':/* bufsize */
792 FULLCHECK("bufsize");
797 result = parse_uint(&num, value, COMMSIZE,
799 if (result != ISC_R_SUCCESS)
800 fatal("Couldn't parse buffer size");
801 lookup->udpsize = num;
809 case 'd':/* cdflag */
811 case 'f': /* cdflag */
812 case '\0': /* +cd is a synonym for +cdflag */
814 lookup->cdflag = state;
822 noclass = ISC_TF(!state);
828 case 'o': /* comments */
829 FULLCHECK("comments");
830 lookup->comments = state;
831 if (lookup == default_lookup)
840 case 'e': /* defname */
841 FULLCHECK("defname");
842 if (!lookup->trace) {
846 case 'n': /* dnssec */
848 if (state && lookup->edns == -1)
850 lookup->dnssec = state;
852 case 'o': /* domain */
858 strncpy(domainopt, value, sizeof(domainopt));
859 domainopt[sizeof(domainopt)-1] = '\0';
875 result = parse_uint(&num, value, 255, "edns");
876 if (result != ISC_R_SUCCESS)
877 fatal("Couldn't parse edns");
882 lookup->servfail_stops = state;
886 case 'd': /* identify */
887 FULLCHECK("identify");
888 lookup->identify = state;
890 case 'g': /* ignore */
891 default: /* Inherits default for compatibility */
893 lookup->ignore = ISC_TRUE;
897 FULLCHECK("keepopen");
900 case 'm': /* multiline */
901 FULLCHECK("multiline");
906 case 'd': /* ndots */
912 result = parse_uint(&num, value, MAXNDOTS, "ndots");
913 if (result != ISC_R_SUCCESS)
914 fatal("Couldn't parse ndots");
921 if (state && lookup->edns == -1)
923 lookup->nsid = state;
925 case 's': /* nssearch */
926 FULLCHECK("nssearch");
927 lookup->ns_search_only = state;
929 lookup->trace_root = ISC_TRUE;
930 lookup->recurse = ISC_TRUE;
931 lookup->identify = ISC_TRUE;
932 lookup->stats = ISC_FALSE;
933 lookup->comments = ISC_FALSE;
934 lookup->section_additional = ISC_FALSE;
935 lookup->section_authority = ISC_FALSE;
936 lookup->section_question = ISC_FALSE;
937 lookup->rdtype = dns_rdatatype_ns;
938 lookup->rdtypeset = ISC_TRUE;
939 short_form = ISC_TRUE;
960 case 'u': /* question */
961 FULLCHECK("question");
962 lookup->section_question = state;
963 if (lookup == default_lookup)
974 case 'c': /* recurse */
975 FULLCHECK("recurse");
976 lookup->recurse = state;
978 case 't': /* retry / retries */
979 FULLCHECK2("retry", "retries");
984 result = parse_uint(&lookup->retries, value,
985 MAXTRIES - 1, "retries");
986 if (result != ISC_R_SUCCESS)
987 fatal("Couldn't parse retries");
1000 case 'e': /* search */
1001 FULLCHECK("search");
1002 if (!lookup->trace) {
1008 goto invalid_option;
1010 case 'r': /* short */
1014 printcmd = ISC_FALSE;
1015 lookup->section_additional = ISC_FALSE;
1016 lookup->section_answer = ISC_TRUE;
1017 lookup->section_authority = ISC_FALSE;
1018 lookup->section_question = ISC_FALSE;
1019 lookup->comments = ISC_FALSE;
1020 lookup->stats = ISC_FALSE;
1023 case 'w': /* showsearch */
1024 FULLCHECK("showsearch");
1025 if (!lookup->trace) {
1031 goto invalid_option;
1035 case 'i': /* sigchase */
1036 FULLCHECK("sigchase");
1037 lookup->sigchase = state;
1038 if (lookup->sigchase)
1039 lookup->dnssec = ISC_TRUE;
1042 case 't': /* stats */
1044 lookup->stats = state;
1047 goto invalid_option;
1055 lookup->tcp_mode = state;
1057 case 'i': /* timeout */
1058 FULLCHECK("timeout");
1062 goto invalid_option;
1063 result = parse_uint(&timeout, value, MAXTIMEOUT,
1065 if (result != ISC_R_SUCCESS)
1066 fatal("Couldn't parse timeout");
1071 case 'o': /* topdown */
1072 FULLCHECK("topdown");
1073 lookup->do_topdown = state;
1078 case 'a': /* trace */
1080 lookup->trace = state;
1081 lookup->trace_root = state;
1083 lookup->recurse = ISC_FALSE;
1084 lookup->identify = ISC_TRUE;
1085 lookup->comments = ISC_FALSE;
1086 lookup->stats = ISC_FALSE;
1087 lookup->section_additional = ISC_FALSE;
1088 lookup->section_authority = ISC_TRUE;
1089 lookup->section_question = ISC_FALSE;
1090 usesearch = ISC_FALSE;
1093 case 'i': /* tries */
1098 goto invalid_option;
1099 result = parse_uint(&lookup->retries, value,
1101 if (result != ISC_R_SUCCESS)
1102 fatal("Couldn't parse tries");
1103 if (lookup->retries == 0)
1104 lookup->retries = 1;
1107 case 'u': /* trusted-key */
1108 FULLCHECK("trusted-key");
1112 goto invalid_option;
1113 n = strlcpy(trustedkey, ptr,
1114 sizeof(trustedkey));
1115 if (n >= sizeof(trustedkey))
1116 fatal("trusted key too large");
1120 goto invalid_option;
1123 case 't': /* ttlid */
1125 nottl = ISC_TF(!state);
1128 goto invalid_option;
1134 lookup->tcp_mode = state;
1139 fprintf(stderr, "Invalid option: +%s\n",
1147 * #ISC_TRUE returned if value was used
1149 static const char *single_dash_opts = "46dhimnv";
1150 static const char *dash_opts = "46bcdfhikmnptvyx";
1151 static isc_boolean_t
1152 dash_option(char *option, char *next, dig_lookup_t **lookup,
1153 isc_boolean_t *open_type_class, isc_boolean_t *need_clone,
1154 isc_boolean_t config_only, int argc, char **argv,
1155 isc_boolean_t *firstarg)
1157 char opt, *value, *ptr, *ptr2, *ptr3;
1158 isc_result_t result;
1159 isc_boolean_t value_from_next;
1160 isc_textregion_t tr;
1161 dns_rdatatype_t rdtype;
1162 dns_rdataclass_t rdclass;
1163 char textname[MXNAME];
1165 struct in6_addr in6;
1170 while (strpbrk(option, single_dash_opts) == &option[0]) {
1172 * Since the -[46dhimnv] options do not take an argument,
1173 * account for them (in any number and/or combination)
1174 * if they appear as the first character(s) of a q-opt.
1180 isc_net_disableipv6();
1181 have_ipv6 = ISC_FALSE;
1183 fatal("can't find IPv4 networking");
1190 isc_net_disableipv4();
1191 have_ipv4 = ISC_FALSE;
1193 fatal("can't find IPv6 networking");
1199 ptr = strpbrk(&option[1], dash_opts);
1200 if (ptr != &option[1]) {
1203 debugging = ISC_TRUE;
1206 debugging = ISC_TRUE;
1215 case 'm': /* memdebug */
1216 /* memdebug is handled in preparse_args() */
1226 if (strlen(option) > 1U)
1227 option = &option[1];
1232 if (strlen(option) > 1U) {
1233 value_from_next = ISC_FALSE;
1236 value_from_next = ISC_TRUE;
1240 goto invalid_option;
1243 hash = strchr(value, '#');
1245 result = parse_uint(&num, hash + 1, MAXPORT,
1247 if (result != ISC_R_SUCCESS)
1248 fatal("Couldn't parse port number");
1253 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1) {
1254 isc_sockaddr_fromin6(&bind_address, &in6, srcport);
1255 isc_net_disableipv4();
1256 } else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1) {
1257 isc_sockaddr_fromin(&bind_address, &in4, srcport);
1258 isc_net_disableipv6();
1262 fatal("invalid address %s", value);
1266 specified_source = ISC_TRUE;
1267 return (value_from_next);
1269 if ((*lookup)->rdclassset) {
1270 fprintf(stderr, ";; Warning, extra class option\n");
1272 *open_type_class = ISC_FALSE;
1274 tr.length = strlen(value);
1275 result = dns_rdataclass_fromtext(&rdclass,
1276 (isc_textregion_t *)&tr);
1277 if (result == ISC_R_SUCCESS) {
1278 (*lookup)->rdclass = rdclass;
1279 (*lookup)->rdclassset = ISC_TRUE;
1281 fprintf(stderr, ";; Warning, ignoring "
1282 "invalid class %s\n",
1284 return (value_from_next);
1287 return (value_from_next);
1289 strncpy(keyfile, value, sizeof(keyfile));
1290 keyfile[sizeof(keyfile)-1]=0;
1291 return (value_from_next);
1293 result = parse_uint(&num, value, MAXPORT, "port number");
1294 if (result != ISC_R_SUCCESS)
1295 fatal("Couldn't parse port number");
1297 return (value_from_next);
1301 (*lookup) = clone_lookup(default_lookup,
1303 *need_clone = ISC_TRUE;
1304 strncpy((*lookup)->textname, value,
1305 sizeof((*lookup)->textname));
1306 (*lookup)->textname[sizeof((*lookup)->textname)-1]=0;
1307 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1308 (*lookup)->ns_search_only);
1309 (*lookup)->new_search = ISC_TRUE;
1311 printgreeting(argc, argv, *lookup);
1312 *firstarg = ISC_FALSE;
1314 ISC_LIST_APPEND(lookup_list, (*lookup), link);
1315 debug("looking up %s", (*lookup)->textname);
1317 return (value_from_next);
1319 *open_type_class = ISC_FALSE;
1320 if (strncasecmp(value, "ixfr=", 5) == 0) {
1321 rdtype = dns_rdatatype_ixfr;
1322 result = ISC_R_SUCCESS;
1325 tr.length = strlen(value);
1326 result = dns_rdatatype_fromtext(&rdtype,
1327 (isc_textregion_t *)&tr);
1328 if (result == ISC_R_SUCCESS &&
1329 rdtype == dns_rdatatype_ixfr) {
1330 result = DNS_R_UNKNOWN;
1333 if (result == ISC_R_SUCCESS) {
1334 if ((*lookup)->rdtypeset) {
1335 fprintf(stderr, ";; Warning, "
1336 "extra type option\n");
1338 if (rdtype == dns_rdatatype_ixfr) {
1339 isc_uint32_t serial;
1340 (*lookup)->rdtype = dns_rdatatype_ixfr;
1341 (*lookup)->rdtypeset = ISC_TRUE;
1342 result = parse_uint(&serial, &value[5],
1343 MAXSERIAL, "serial number");
1344 if (result != ISC_R_SUCCESS)
1345 fatal("Couldn't parse serial number");
1346 (*lookup)->ixfr_serial = serial;
1347 (*lookup)->section_question = plusquest;
1348 (*lookup)->comments = pluscomm;
1349 (*lookup)->tcp_mode = ISC_TRUE;
1351 (*lookup)->rdtype = rdtype;
1352 (*lookup)->rdtypeset = ISC_TRUE;
1353 if (rdtype == dns_rdatatype_axfr) {
1354 (*lookup)->section_question = plusquest;
1355 (*lookup)->comments = pluscomm;
1357 (*lookup)->ixfr_serial = ISC_FALSE;
1360 fprintf(stderr, ";; Warning, ignoring "
1361 "invalid type %s\n",
1363 return (value_from_next);
1365 ptr = next_token(&value,":"); /* hmac type or name */
1369 ptr2 = next_token(&value, ":"); /* name or secret */
1372 ptr3 = next_token(&value,":"); /* secret or NULL */
1378 hmacname = DNS_TSIG_HMACMD5_NAME;
1381 strncpy(keynametext, ptr, sizeof(keynametext));
1382 keynametext[sizeof(keynametext)-1]=0;
1383 strncpy(keysecret, ptr2, sizeof(keysecret));
1384 keysecret[sizeof(keysecret)-1]=0;
1385 return (value_from_next);
1388 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1389 *need_clone = ISC_TRUE;
1390 if (get_reverse(textname, sizeof(textname), value,
1391 ip6_int, ISC_FALSE) == ISC_R_SUCCESS) {
1392 strncpy((*lookup)->textname, textname,
1393 sizeof((*lookup)->textname));
1394 debug("looking up %s", (*lookup)->textname);
1395 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1396 (*lookup)->ns_search_only);
1397 (*lookup)->ip6_int = ip6_int;
1398 if (!(*lookup)->rdtypeset)
1399 (*lookup)->rdtype = dns_rdatatype_ptr;
1400 if (!(*lookup)->rdclassset)
1401 (*lookup)->rdclass = dns_rdataclass_in;
1402 (*lookup)->new_search = ISC_TRUE;
1404 printgreeting(argc, argv, *lookup);
1405 *firstarg = ISC_FALSE;
1407 ISC_LIST_APPEND(lookup_list, *lookup, link);
1409 fprintf(stderr, "Invalid IP address %s\n", value);
1412 return (value_from_next);
1415 fprintf(stderr, "Invalid option: -%s\n", option);
1423 * Because we may be trying to do memory allocation recording, we're going
1424 * to need to parse the arguments for the -m *before* we start the main
1425 * argument parsing routine.
1427 * I'd prefer not to have to do this, but I am not quite sure how else to
1428 * fix the problem. Argument parsing in dig involves memory allocation
1429 * by its nature, so it can't be done in the main argument parser.
1432 preparse_args(int argc, char **argv) {
1439 for (rc--, rv++; rc > 0; rc--, rv++) {
1440 if (rv[0][0] != '-')
1443 while (strpbrk(option, single_dash_opts) == &option[0]) {
1444 if (option[0] == 'm') {
1445 memdebugging = ISC_TRUE;
1446 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1447 ISC_MEM_DEBUGRECORD;
1450 option = &option[1];
1456 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1457 int argc, char **argv) {
1458 isc_result_t result;
1459 isc_textregion_t tr;
1460 isc_boolean_t firstarg = ISC_TRUE;
1461 dig_lookup_t *lookup = NULL;
1462 dns_rdatatype_t rdtype;
1463 dns_rdataclass_t rdclass;
1464 isc_boolean_t open_type_class = ISC_TRUE;
1465 char batchline[MXNAME];
1476 isc_boolean_t need_clone = ISC_TRUE;
1479 * The semantics for parsing the args is a bit complex; if
1480 * we don't have a host yet, make the arg apply globally,
1481 * otherwise make it apply to the latest host. This is
1482 * a bit different than the previous versions, but should
1483 * form a consistent user interface.
1485 * First, create a "default lookup" which won't actually be used
1486 * anywhere, except for cloning into new lookups
1489 debug("parse_args()");
1490 if (!is_batchfile) {
1491 debug("making new lookup");
1492 default_lookup = make_empty_lookup();
1496 * Treat ${HOME}/.digrc as a special batchfile
1498 INSIST(batchfp == NULL);
1499 homedir = getenv("HOME");
1500 if (homedir != NULL) {
1502 n = snprintf(rcfile, sizeof(rcfile), "%s/.digrc",
1504 if (n < sizeof(rcfile))
1505 batchfp = fopen(rcfile, "r");
1507 if (batchfp != NULL) {
1508 while (fgets(batchline, sizeof(batchline),
1510 debug("config line %s", batchline);
1513 bargv[bargc] = next_token(&input, " \t\r\n");
1514 while ((bargv[bargc] != NULL) &&
1518 next_token(&input, " \t\r\n");
1524 for(i = 0; i < bargc; i++)
1525 debug(".digrc argv %d: %s",
1527 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1535 if (is_batchfile && !config_only) {
1536 /* Processing '-f batchfile'. */
1537 lookup = clone_lookup(default_lookup, ISC_TRUE);
1538 need_clone = ISC_FALSE;
1540 lookup = default_lookup;
1544 for (rc--, rv++; rc > 0; rc--, rv++) {
1545 debug("main parsing %s", rv[0]);
1546 if (strncmp(rv[0], "%", 1) == 0)
1548 if (strncmp(rv[0], "@", 1) == 0) {
1549 addresscount = getaddresses(lookup, &rv[0][1], NULL);
1550 } else if (rv[0][0] == '+') {
1551 plus_option(&rv[0][1], is_batchfile,
1553 } else if (rv[0][0] == '-') {
1555 if (dash_option(&rv[0][1], NULL,
1556 &lookup, &open_type_class,
1557 &need_clone, config_only,
1558 argc, argv, &firstarg)) {
1563 if (dash_option(&rv[0][1], rv[1],
1564 &lookup, &open_type_class,
1565 &need_clone, config_only,
1566 argc, argv, &firstarg)) {
1573 * Anything which isn't an option
1575 if (open_type_class) {
1576 if (strncasecmp(rv[0], "ixfr=", 5) == 0) {
1577 rdtype = dns_rdatatype_ixfr;
1578 result = ISC_R_SUCCESS;
1581 tr.length = strlen(rv[0]);
1582 result = dns_rdatatype_fromtext(&rdtype,
1583 (isc_textregion_t *)&tr);
1584 if (result == ISC_R_SUCCESS &&
1585 rdtype == dns_rdatatype_ixfr) {
1586 fprintf(stderr, ";; Warning, "
1592 if (result == ISC_R_SUCCESS) {
1593 if (lookup->rdtypeset) {
1594 fprintf(stderr, ";; Warning, "
1595 "extra type option\n");
1597 if (rdtype == dns_rdatatype_ixfr) {
1598 isc_uint32_t serial;
1601 lookup->rdtypeset = ISC_TRUE;
1602 result = parse_uint(&serial,
1606 if (result != ISC_R_SUCCESS)
1607 fatal("Couldn't parse "
1609 lookup->ixfr_serial = serial;
1610 lookup->section_question =
1612 lookup->comments = pluscomm;
1613 lookup->tcp_mode = ISC_TRUE;
1615 lookup->rdtype = rdtype;
1616 lookup->rdtypeset = ISC_TRUE;
1618 dns_rdatatype_axfr) {
1619 lookup->section_question =
1621 lookup->comments = pluscomm;
1623 lookup->ixfr_serial = ISC_FALSE;
1627 result = dns_rdataclass_fromtext(&rdclass,
1628 (isc_textregion_t *)&tr);
1629 if (result == ISC_R_SUCCESS) {
1630 if (lookup->rdclassset) {
1631 fprintf(stderr, ";; Warning, "
1632 "extra class option\n");
1634 lookup->rdclass = rdclass;
1635 lookup->rdclassset = ISC_TRUE;
1642 lookup = clone_lookup(default_lookup,
1644 need_clone = ISC_TRUE;
1645 strncpy(lookup->textname, rv[0],
1646 sizeof(lookup->textname));
1647 lookup->textname[sizeof(lookup->textname)-1]=0;
1648 lookup->trace_root = ISC_TF(lookup->trace ||
1649 lookup->ns_search_only);
1650 lookup->new_search = ISC_TRUE;
1652 printgreeting(argc, argv, lookup);
1653 firstarg = ISC_FALSE;
1655 ISC_LIST_APPEND(lookup_list, lookup, link);
1656 debug("looking up %s", lookup->textname);
1658 /* XXX Error message */
1663 * If we have a batchfile, seed the lookup list with the
1664 * first entry, then trust the callback in dighost_shutdown
1667 if ((batchname != NULL) && !(is_batchfile)) {
1668 if (strcmp(batchname, "-") == 0)
1671 batchfp = fopen(batchname, "r");
1672 if (batchfp == NULL) {
1676 fatal("couldn't open specified batch file");
1678 /* XXX Remove code dup from shutdown code */
1680 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1682 debug("batch line %s", batchline);
1683 if (batchline[0] == '\r' || batchline[0] == '\n'
1684 || batchline[0] == '#' || batchline[0] == ';')
1687 bargv[bargc] = next_token(&input, " \t\r\n");
1688 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1690 bargv[bargc] = next_token(&input, " \t\r\n");
1696 for(i = 0; i < bargc; i++)
1697 debug("batch argv %d: %s", i, bargv[i]);
1698 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1704 * If no lookup specified, search for root
1706 if ((lookup_list.head == NULL) && !config_only) {
1708 lookup = clone_lookup(default_lookup, ISC_TRUE);
1709 need_clone = ISC_TRUE;
1710 lookup->trace_root = ISC_TF(lookup->trace ||
1711 lookup->ns_search_only);
1712 lookup->new_search = ISC_TRUE;
1713 strcpy(lookup->textname, ".");
1714 lookup->rdtype = dns_rdatatype_ns;
1715 lookup->rdtypeset = ISC_TRUE;
1717 printgreeting(argc, argv, lookup);
1718 firstarg = ISC_FALSE;
1720 ISC_LIST_APPEND(lookup_list, lookup, link);
1723 destroy_lookup(lookup);
1727 * Callback from dighost.c to allow program-specific shutdown code.
1728 * Here, we're possibly reading from a batch file, then shutting down
1729 * for real if there's nothing in the batch file to read.
1732 dighost_shutdown(void) {
1733 char batchline[MXNAME];
1739 if (batchname == NULL) {
1745 if (feof(batchfp)) {
1748 if (batchfp != stdin)
1753 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1754 debug("batch line %s", batchline);
1757 bargv[bargc] = next_token(&input, " \t\r\n");
1758 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1760 bargv[bargc] = next_token(&input, " \t\r\n");
1765 for(i = 0; i < bargc; i++)
1766 debug("batch argv %d: %s", i, bargv[i]);
1767 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1771 if (batchfp != stdin)
1778 /*% Main processing routine for dig */
1780 main(int argc, char **argv) {
1781 isc_result_t result;
1783 ISC_LIST_INIT(lookup_list);
1784 ISC_LIST_INIT(server_list);
1785 ISC_LIST_INIT(search_list);
1788 preparse_args(argc, argv);
1790 result = isc_app_start();
1791 check_result(result, "isc_app_start");
1793 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1795 if (domainopt[0] != '\0') {
1796 set_search_domain(domainopt);
1797 usesearch = ISC_TRUE;
1799 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1800 check_result(result, "isc_app_onrun");
1802 destroy_lookup(default_lookup);
1803 if (batchname != NULL) {
1804 if (batchfp != stdin)