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.16 2004/06/07 03:59:08 marka Exp $ */
26 #include <isc/netaddr.h>
27 #include <isc/print.h>
28 #include <isc/string.h>
32 #include <dns/byaddr.h>
33 #include <dns/fixedname.h>
34 #include <dns/masterdump.h>
35 #include <dns/message.h>
37 #include <dns/rdata.h>
38 #include <dns/rdataset.h>
39 #include <dns/rdatatype.h>
40 #include <dns/rdataclass.h>
41 #include <dns/result.h>
45 extern ISC_LIST(dig_lookup_t) lookup_list;
46 extern dig_serverlist_t server_list;
47 extern ISC_LIST(dig_searchlist_t) search_list;
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); \
57 extern isc_boolean_t have_ipv4, have_ipv6, specified_source,
59 extern in_port_t port;
60 extern unsigned int timeout;
61 extern isc_mem_t *mctx;
62 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];
72 extern dns_tsigkey_t *key;
73 extern isc_boolean_t validated;
74 extern isc_taskmgr_t *taskmgr;
75 extern isc_task_t *global_task;
76 extern isc_boolean_t free_now;
77 dig_lookup_t *default_lookup = NULL;
79 extern isc_boolean_t debugging, memdebugging;
80 static char *batchname = NULL;
81 static FILE *batchfp = NULL;
84 static char domainopt[DNS_NAME_MAXTEXT];
86 static isc_boolean_t short_form = ISC_FALSE, printcmd = ISC_TRUE,
87 ip6_int = ISC_FALSE, plusquest = ISC_FALSE, pluscomm = ISC_FALSE,
88 multiline = ISC_FALSE;
90 static const char *opcodetext[] = {
109 static const char *rcodetext[] = {
129 extern char *progname;
132 print_usage(FILE *fp) {
134 "Usage: dig [@global-server] [domain] [q-type] [q-class] {q-opt}\n"
135 " {global-d-opt} host [@local-server] {local-d-opt}\n"
136 " [ host [@local-server] {local-d-opt} [...]]\n", fp);
142 fputs("\nUse \"dig -h\" (or \"dig -h | more\") "
143 "for complete list of options\n", stderr);
151 "Where: domain are in the Domain Name System\n"
152 " q-class is one of (in,hs,ch,...) [default: in]\n"
153 " q-type is one of (a,any,mx,ns,soa,hinfo,axfr,txt,...) [default:a]\n"
154 " (Use ixfr=version for type ixfr)\n"
155 " q-opt is one of:\n"
156 " -x dot-notation (shortcut for in-addr lookups)\n"
157 " -i (IP6.INT reverse IPv6 lookups)\n"
158 " -f filename (batch mode)\n"
159 " -b address (bind to source address)\n"
160 " -p port (specify port number)\n"
161 " -t type (specify query type)\n"
162 " -c class (specify query class)\n"
163 " -k keyfile (specify tsig key file)\n"
164 " -y name:key (specify named base64 tsig key)\n"
165 " d-opt is of the form +keyword[=value], where keyword is:\n"
166 " +[no]vc (TCP mode)\n"
167 " +[no]tcp (TCP mode, alternate syntax)\n"
168 " +time=### (Set query timeout) [5]\n"
169 " +tries=### (Set number of UDP attempts) [3]\n"
170 " +domain=### (Set default domainname)\n"
171 " +bufsize=### (Set EDNS0 Max UDP packet size)\n"
172 " +ndots=### (Set NDOTS value)\n"
173 " +[no]search (Set whether to use searchlist)\n"
174 " +[no]defname (Ditto)\n"
175 " +[no]recurse (Recursive mode)\n"
176 " +[no]ignore (Don't revert to TCP for TC responses.)"
178 " +[no]fail (Don't try next server on SERVFAIL)\n"
179 " +[no]besteffort (Try to parse even illegal messages)\n"
180 " +[no]aaonly (Set AA flag in query)\n"
181 " +[no]adflag (Set AD flag in query)\n"
182 " +[no]cdflag (Set CD flag in query)\n"
183 " +[no]cmd (Control display of command line)\n"
184 " +[no]comments (Control display of comment lines)\n"
185 " +[no]question (Control display of question)\n"
186 " +[no]answer (Control display of answer)\n"
187 " +[no]authority (Control display of authority)\n"
188 " +[no]additional (Control display of additional)\n"
189 " +[no]stats (Control display of statistics)\n"
190 " +[no]short (Disable everything except short\n"
192 " +[no]all (Set or clear all display flags)\n"
193 " +[no]qr (Print question before sending)\n"
194 " +[no]nssearch (Search all authoritative nameservers)\n"
195 " +[no]identify (ID responders in short answers)\n"
196 " +[no]trace (Trace delegation down from root)\n"
197 " +[no]dnssec (Request DNSSEC records)\n"
198 " +[no]multiline (Print records in an expanded format)\n"
199 " global d-opts and servers (before host name) affect all queries.\n"
200 " local d-opts and servers (after host name) affect only that lookup.\n",
205 * Callback from dighost.c to print the received message.
208 received(int bytes, isc_sockaddr_t *from, dig_query_t *query) {
213 char fromtext[ISC_SOCKADDR_FORMATSIZE];
215 isc_sockaddr_format(from, fromtext, sizeof(fromtext));
217 result = isc_time_now(&now);
218 check_result(result, "isc_time_now");
220 if (query->lookup->stats && !short_form) {
221 diff = isc_time_microdiff(&now, &query->time_sent);
222 printf(";; Query time: %ld msec\n", (long int)diff/1000);
223 printf(";; SERVER: %s(%s)\n", fromtext, query->servname);
225 printf(";; WHEN: %s", ctime(&tnow));
226 if (query->lookup->doing_xfr) {
227 printf(";; XFR size: %d records\n",
230 printf(";; MSG SIZE rcvd: %d\n", bytes);
235 puts(";; WARNING -- Some TSIG could not "
238 if ((key == NULL) && (keysecret[0] != 0)) {
239 puts(";; WARNING -- TSIG key was not used.");
242 } else if (query->lookup->identify && !short_form) {
243 diff = isc_time_microdiff(&now, &query->time_sent);
244 printf(";; Received %u bytes from %s(%s) in %d ms\n\n",
245 bytes, fromtext, query->servname,
251 * Callback from dighost.c to print that it is trying a server.
256 trying(char *frm, dig_lookup_t *lookup) {
262 * Internal print routine used to print short form replies.
265 say_message(dns_rdata_t *rdata, dig_query_t *query, isc_buffer_t *buf) {
269 char store[sizeof("12345678901234567890")];
271 if (query->lookup->trace || query->lookup->ns_search_only) {
272 result = dns_rdatatype_totext(rdata->type, buf);
273 if (result != ISC_R_SUCCESS)
275 ADD_STRING(buf, " ");
277 result = dns_rdata_totext(rdata, NULL, buf);
278 check_result(result, "dns_rdata_totext");
279 if (query->lookup->identify) {
280 result = isc_time_now(&now);
281 if (result != ISC_R_SUCCESS)
283 diff = isc_time_microdiff(&now, &query->time_sent);
284 ADD_STRING(buf, " from server ");
285 ADD_STRING(buf, query->servname);
286 snprintf(store, 19, " in %d ms.", (int)diff/1000);
287 ADD_STRING(buf, store);
289 ADD_STRING(buf, "\n");
290 return (ISC_R_SUCCESS);
294 * short_form message print handler. Calls above say_message()
297 short_answer(dns_message_t *msg, dns_messagetextflag_t flags,
298 isc_buffer_t *buf, dig_query_t *query)
301 dns_rdataset_t *rdataset;
303 isc_result_t result, loopresult;
304 dns_name_t empty_name;
306 dns_rdata_t rdata = DNS_RDATA_INIT;
310 dns_name_init(&empty_name, NULL);
311 result = dns_message_firstname(msg, DNS_SECTION_ANSWER);
312 if (result == ISC_R_NOMORE)
313 return (ISC_R_SUCCESS);
314 else if (result != ISC_R_SUCCESS)
319 dns_message_currentname(msg, DNS_SECTION_ANSWER, &name);
321 isc_buffer_init(&target, t, sizeof(t));
323 for (rdataset = ISC_LIST_HEAD(name->list);
325 rdataset = ISC_LIST_NEXT(rdataset, link)) {
326 loopresult = dns_rdataset_first(rdataset);
327 while (loopresult == ISC_R_SUCCESS) {
328 dns_rdataset_current(rdataset, &rdata);
329 result = say_message(&rdata, query,
331 check_result(result, "say_message");
332 loopresult = dns_rdataset_next(rdataset);
333 dns_rdata_reset(&rdata);
336 result = dns_message_nextname(msg, DNS_SECTION_ANSWER);
337 if (result == ISC_R_NOMORE)
339 else if (result != ISC_R_SUCCESS)
343 return (ISC_R_SUCCESS);
347 * Callback from dighost.c to print the reply from a server
350 printmessage(dig_query_t *query, dns_message_t *msg, isc_boolean_t headers) {
352 dns_messagetextflag_t flags;
353 isc_buffer_t *buf = NULL;
354 unsigned int len = OUTPUTBUF;
355 const dns_master_style_t *style;
358 style = &dns_master_style_default;
360 style = &dns_master_style_debug;
362 if (query->lookup->cmdline[0] != 0) {
364 fputs(query->lookup->cmdline, stdout);
365 query->lookup->cmdline[0]=0;
367 debug("printmessage(%s %s %s)", headers ? "headers" : "noheaders",
368 query->lookup->comments ? "comments" : "nocomments",
369 short_form ? "short_form" : "long_form");
373 flags |= DNS_MESSAGETEXTFLAG_NOHEADERS;
374 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
376 if (!query->lookup->comments)
377 flags |= DNS_MESSAGETEXTFLAG_NOCOMMENTS;
379 result = ISC_R_SUCCESS;
381 result = isc_buffer_allocate(mctx, &buf, len);
382 check_result(result, "isc_buffer_allocate");
384 if (query->lookup->comments && !short_form) {
385 if (query->lookup->cmdline[0] != 0)
386 printf("; %s\n", query->lookup->cmdline);
387 if (msg == query->lookup->sendmsg)
388 printf(";; Sending:\n");
390 printf(";; Got answer:\n");
393 printf(";; ->>HEADER<<- opcode: %s, status: %s, "
395 opcodetext[msg->opcode], rcodetext[msg->rcode],
398 if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0)
400 if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0)
402 if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0)
404 if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0)
406 if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0)
408 if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0)
410 if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0)
413 printf("; QUERY: %u, ANSWER: %u, "
414 "AUTHORITY: %u, ADDITIONAL: %u\n",
415 msg->counts[DNS_SECTION_QUESTION],
416 msg->counts[DNS_SECTION_ANSWER],
417 msg->counts[DNS_SECTION_AUTHORITY],
418 msg->counts[DNS_SECTION_ADDITIONAL]);
424 if (query->lookup->comments && headers && !short_form)
426 result = dns_message_pseudosectiontotext(msg,
427 DNS_PSEUDOSECTION_OPT,
429 if (result == ISC_R_NOSPACE) {
432 isc_buffer_free(&buf);
433 result = isc_buffer_allocate(mctx, &buf, len);
434 if (result == ISC_R_SUCCESS)
435 goto repopulate_buffer;
440 "dns_message_pseudosectiontotext");
443 if (query->lookup->section_question && headers) {
445 result = dns_message_sectiontotext(msg,
446 DNS_SECTION_QUESTION,
448 if (result == ISC_R_NOSPACE)
450 check_result(result, "dns_message_sectiontotext");
453 if (query->lookup->section_answer) {
455 result = dns_message_sectiontotext(msg,
458 if (result == ISC_R_NOSPACE)
460 check_result(result, "dns_message_sectiontotext");
462 result = short_answer(msg, flags, buf, query);
463 if (result == ISC_R_NOSPACE)
465 check_result(result, "short_answer");
468 if (query->lookup->section_authority) {
470 result = dns_message_sectiontotext(msg,
471 DNS_SECTION_AUTHORITY,
473 if (result == ISC_R_NOSPACE)
475 check_result(result, "dns_message_sectiontotext");
478 if (query->lookup->section_additional) {
480 result = dns_message_sectiontotext(msg,
481 DNS_SECTION_ADDITIONAL,
483 if (result == ISC_R_NOSPACE)
485 check_result(result, "dns_message_sectiontotext");
487 * Only print the signature on the first record.
490 result = dns_message_pseudosectiontotext(
492 DNS_PSEUDOSECTION_TSIG,
494 if (result == ISC_R_NOSPACE)
497 "dns_message_pseudosectiontotext");
498 result = dns_message_pseudosectiontotext(
500 DNS_PSEUDOSECTION_SIG0,
502 if (result == ISC_R_NOSPACE)
505 "dns_message_pseudosectiontotext");
509 if (headers && query->lookup->comments && !short_form)
512 printf("%.*s", (int)isc_buffer_usedlength(buf),
513 (char *)isc_buffer_base(buf));
514 isc_buffer_free(&buf);
519 * print the greeting message when the program first starts up.
522 printgreeting(int argc, char **argv, dig_lookup_t *lookup) {
525 static isc_boolean_t first = ISC_TRUE;
529 lookup->cmdline[sizeof(lookup->cmdline) - 1] = 0;
530 snprintf(lookup->cmdline, sizeof(lookup->cmdline),
531 "%s; <<>> DiG " VERSION " <<>>",
535 snprintf(append, sizeof(append), " %s", argv[i++]);
536 remaining = sizeof(lookup->cmdline) -
537 strlen(lookup->cmdline) - 1;
538 strncat(lookup->cmdline, append, remaining);
540 remaining = sizeof(lookup->cmdline) -
541 strlen(lookup->cmdline) - 1;
542 strncat(lookup->cmdline, "\n", remaining);
544 snprintf(append, sizeof (append),
545 ";; global options: %s %s\n",
546 short_form ? "short_form" : "",
547 printcmd ? "printcmd" : "");
549 remaining = sizeof(lookup->cmdline) -
550 strlen(lookup->cmdline) - 1;
551 strncat(lookup->cmdline, append, remaining);
557 * Reorder an argument list so that server names all come at the end.
558 * This is a bit of a hack, to allow batch-mode processing to properly
559 * handle the server options.
562 reorder_args(int argc, char *argv[]) {
567 debug("reorder_args()");
569 while (argv[end][0] == '@') {
574 debug("arg[end]=%s", argv[end]);
575 for (i = 1; i < end - 1; i++) {
576 if (argv[i][0] == '@') {
577 debug("arg[%d]=%s", i, argv[i]);
579 for (j = i + 1; j < end; j++) {
580 debug("Moving %s to %d", argv[j], j - 1);
581 argv[j - 1] = argv[j];
583 debug("moving %s to end, %d", ptr, end - 1);
593 parse_uint(char *arg, const char *desc, isc_uint32_t max) {
597 tmp = strtoul(arg, &endp, 10);
599 fatal("%s '%s' must be numeric", desc, arg);
601 fatal("%s '%s' out of range", desc, arg);
606 * We're not using isc_commandline_parse() here since the command line
607 * syntax of dig is quite a bit different from that which can be described
613 plus_option(char *option, isc_boolean_t is_batchfile,
614 dig_lookup_t *lookup)
616 char option_store[256];
617 char *cmd, *value, *ptr;
618 isc_boolean_t state = ISC_TRUE;
620 strncpy(option_store, option, sizeof(option_store));
621 option_store[sizeof(option_store)-1]=0;
623 cmd=next_token(&ptr,"=");
625 printf(";; Invalid option %s\n",option_store);
629 if (strncasecmp(cmd,"no",2)==0) {
636 case 'a': /* aaflag */
637 lookup->aaonly = state;
641 case 'd': /* additional */
642 lookup->section_additional = state;
644 case 'f': /* adflag */
645 lookup->adflag = state;
652 lookup->section_question = state;
653 lookup->section_authority = state;
654 lookup->section_answer = state;
655 lookup->section_additional = state;
656 lookup->comments = state;
657 lookup->stats = state;
660 case 'n': /* answer */
661 lookup->section_answer = state;
663 case 'u': /* authority */
664 lookup->section_authority = state;
672 case 'e':/* besteffort */
673 lookup->besteffort = state;
675 case 'u':/* bufsize */
680 lookup->udpsize = (isc_uint16_t) parse_uint(value,
681 "buffer size", COMMSIZE);
689 case 'd':/* cdflag */
690 lookup->cdflag = state;
695 case 'o': /* comments */
696 lookup->comments = state;
697 if (lookup == default_lookup)
706 case 'e': /* defname */
709 case 'n': /* dnssec */
710 lookup->dnssec = state;
712 case 'o': /* domain */
717 strncpy(domainopt, value, sizeof(domainopt));
718 domainopt[sizeof(domainopt)-1] = '\0';
725 lookup->servfail_stops = state;
729 case 'd': /* identify */
730 lookup->identify = state;
732 case 'g': /* ignore */
733 default: /* Inherets default for compatibility */
734 lookup->ignore = ISC_TRUE;
737 case 'm': /* multiline */
742 case 'd': /* ndots */
747 ndots = parse_uint(value, "ndots", MAXNDOTS);
749 case 's': /* nssearch */
750 lookup->ns_search_only = state;
752 lookup->trace_root = ISC_TRUE;
753 lookup->recurse = ISC_FALSE;
754 lookup->identify = ISC_TRUE;
755 lookup->stats = ISC_FALSE;
756 lookup->comments = ISC_FALSE;
757 lookup->section_additional = ISC_FALSE;
758 lookup->section_authority = ISC_FALSE;
759 lookup->section_question = ISC_FALSE;
760 lookup->rdtype = dns_rdatatype_ns;
761 lookup->rdtypeset = ISC_TRUE;
762 short_form = ISC_TRUE;
774 case 'u': /* question */
775 lookup->section_question = state;
776 if (lookup == default_lookup)
783 case 'r': /* recurse */
784 lookup->recurse = state;
788 case 'e': /* search */
791 case 'h': /* short */
794 printcmd = ISC_FALSE;
795 lookup->section_additional = ISC_FALSE;
796 lookup->section_answer = ISC_TRUE;
797 lookup->section_authority = ISC_FALSE;
798 lookup->section_question = ISC_FALSE;
799 lookup->comments = ISC_FALSE;
800 lookup->stats = ISC_FALSE;
803 case 't': /* stats */
804 lookup->stats = state;
814 lookup->tcp_mode = state;
816 case 'i': /* timeout */
821 timeout = parse_uint(value, "timeout", MAXTIMEOUT);
827 case 'a': /* trace */
828 lookup->trace = state;
829 lookup->trace_root = state;
831 lookup->recurse = ISC_FALSE;
832 lookup->identify = ISC_TRUE;
833 lookup->comments = ISC_FALSE;
834 lookup->stats = ISC_FALSE;
835 lookup->section_additional = ISC_FALSE;
836 lookup->section_authority = ISC_TRUE;
837 lookup->section_question = ISC_FALSE;
840 case 'i': /* tries */
845 lookup->retries = parse_uint(value, "retries",
847 if (lookup->retries == 0)
860 lookup->tcp_mode = state;
865 fprintf(stderr, "Invalid option: +%s\n",
873 * ISC_TRUE returned if value was used
876 dash_option(char *option, char *next, dig_lookup_t **lookup,
877 isc_boolean_t *open_type_class,
878 isc_boolean_t *firstarg,
879 int argc, char **argv)
881 char cmd, *value, *ptr;
883 isc_boolean_t value_from_next;
885 dns_rdatatype_t rdtype;
886 dns_rdataclass_t rdclass;
887 char textname[MXNAME];
892 if (strlen(option) > 1U) {
893 value_from_next = ISC_FALSE;
896 value_from_next = ISC_TRUE;
901 debugging = ISC_TRUE;
910 case 'm': /* memdebug */
911 /* memdebug is handled in preparse_args() */
921 if (have_ipv6 && inet_pton(AF_INET6, value, &in6) == 1)
922 isc_sockaddr_fromin6(&bind_address, &in6, 0);
923 else if (have_ipv4 && inet_pton(AF_INET, value, &in4) == 1)
924 isc_sockaddr_fromin(&bind_address, &in4, 0);
926 fatal("invalid address %s", value);
927 specified_source = ISC_TRUE;
928 return (value_from_next);
930 if ((*lookup)->rdclassset) {
931 fprintf(stderr, ";; Warning, extra class option\n");
933 *open_type_class = ISC_FALSE;
935 tr.length = strlen(value);
936 result = dns_rdataclass_fromtext(&rdclass,
937 (isc_textregion_t *)&tr);
938 if (result == ISC_R_SUCCESS) {
939 (*lookup)->rdclass = rdclass;
940 (*lookup)->rdclassset = ISC_TRUE;
942 fprintf(stderr, ";; Warning, ignoring "
943 "invalid class %s\n",
945 return (value_from_next);
948 return (value_from_next);
950 strncpy(keyfile, value, sizeof(keyfile));
951 keyfile[sizeof(keyfile)-1]=0;
952 return (value_from_next);
954 port = (in_port_t) parse_uint(value, "port number", MAXPORT);
955 return (value_from_next);
957 *open_type_class = ISC_FALSE;
958 if (strncasecmp(value, "ixfr=", 5) == 0) {
959 rdtype = dns_rdatatype_ixfr;
960 result = ISC_R_SUCCESS;
963 tr.length = strlen(value);
964 result = dns_rdatatype_fromtext(&rdtype,
965 (isc_textregion_t *)&tr);
966 if (result == ISC_R_SUCCESS &&
967 rdtype == dns_rdatatype_ixfr)
969 result = DNS_R_UNKNOWN;
972 if (result == ISC_R_SUCCESS) {
973 if ((*lookup)->rdtypeset) {
974 fprintf(stderr, ";; Warning, "
975 "extra type option\n");
977 if (rdtype == dns_rdatatype_ixfr) {
978 (*lookup)->rdtype = dns_rdatatype_ixfr;
979 (*lookup)->rdtypeset = ISC_TRUE;
980 (*lookup)->ixfr_serial =
981 parse_uint(&value[5], "serial number",
983 (*lookup)->section_question = plusquest;
984 (*lookup)->comments = pluscomm;
986 (*lookup)->rdtype = rdtype;
987 (*lookup)->rdtypeset = ISC_TRUE;
988 if (rdtype == dns_rdatatype_axfr) {
989 (*lookup)->section_question = plusquest;
990 (*lookup)->comments = pluscomm;
992 (*lookup)->ixfr_serial = ISC_FALSE;
995 fprintf(stderr, ";; Warning, ignoring "
998 return (value_from_next);
1000 ptr = next_token(&value,":");
1004 strncpy(keynametext, ptr, sizeof(keynametext));
1005 keynametext[sizeof(keynametext)-1]=0;
1006 ptr = next_token(&value, "");
1009 strncpy(keysecret, ptr, sizeof(keysecret));
1010 keysecret[sizeof(keysecret)-1]=0;
1011 return (value_from_next);
1013 *lookup = clone_lookup(default_lookup, ISC_TRUE);
1014 if (get_reverse(textname, value, ip6_int, ISC_FALSE)
1017 strncpy((*lookup)->textname, textname,
1018 sizeof((*lookup)->textname));
1019 debug("looking up %s", (*lookup)->textname);
1020 (*lookup)->trace_root = ISC_TF((*lookup)->trace ||
1021 (*lookup)->ns_search_only);
1022 (*lookup)->ip6_int = ip6_int;
1023 if (!(*lookup)->rdtypeset)
1024 (*lookup)->rdtype = dns_rdatatype_ptr;
1025 if (!(*lookup)->rdclassset)
1026 (*lookup)->rdclass = dns_rdataclass_in;
1027 (*lookup)->new_search = ISC_TRUE;
1028 if (*lookup && *firstarg)
1030 printgreeting(argc, argv, *lookup);
1031 *firstarg = ISC_FALSE;
1033 ISC_LIST_APPEND(lookup_list, *lookup, link);
1035 fprintf(stderr, "Invalid IP address %s\n", value);
1038 return (value_from_next);
1041 fprintf(stderr, "Invalid option: -%s\n", option);
1048 * Because we may be trying to do memory allocation recording, we're going
1049 * to need to parse the arguments for the -m *before* we start the main
1050 * argument parsing routine.
1051 * I'd prefer not to have to do this, but I am not quite sure how else to
1052 * fix the problem. Argument parsing in dig involves memory allocation
1053 * by its nature, so it can't be done in the main argument parser.
1056 preparse_args(int argc, char **argv) {
1062 for (rc--, rv++; rc > 0; rc--, rv++) {
1063 if (strcmp(rv[0], "-m") == 0) {
1064 memdebugging = ISC_TRUE;
1065 isc_mem_debugging = ISC_MEM_DEBUGTRACE |
1066 ISC_MEM_DEBUGRECORD;
1074 parse_args(isc_boolean_t is_batchfile, isc_boolean_t config_only,
1075 int argc, char **argv) {
1076 isc_result_t result;
1077 isc_textregion_t tr;
1078 isc_boolean_t firstarg = ISC_TRUE;
1079 dig_server_t *srv = NULL;
1080 dig_lookup_t *lookup = NULL;
1081 dns_rdatatype_t rdtype;
1082 dns_rdataclass_t rdclass;
1083 isc_boolean_t open_type_class = ISC_TRUE;
1084 char batchline[MXNAME];
1096 * The semantics for parsing the args is a bit complex; if
1097 * we don't have a host yet, make the arg apply globally,
1098 * otherwise make it apply to the latest host. This is
1099 * a bit different than the previous versions, but should
1100 * form a consistent user interface.
1102 * First, create a "default lookup" which won't actually be used
1103 * anywhere, except for cloning into new lookups
1106 debug("parse_args()");
1107 if (!is_batchfile) {
1108 debug("making new lookup");
1109 default_lookup = make_empty_lookup();
1113 * Treat .digrc as a special batchfile
1115 homedir = getenv("HOME");
1116 if (homedir != NULL)
1117 snprintf(rcfile, sizeof(rcfile), "%s/.digrc", homedir);
1119 strcpy(rcfile, ".digrc");
1120 batchfp = fopen(rcfile, "r");
1121 if (batchfp != NULL) {
1122 while (fgets(batchline, sizeof(batchline),
1124 debug("config line %s", batchline);
1127 bargv[bargc] = next_token(&input, " \t\r\n");
1128 while ((bargv[bargc] != NULL) &&
1131 bargv[bargc] = next_token(&input, " \t\r\n");
1137 reorder_args(bargc, (char **)bargv);
1138 parse_args(ISC_TRUE, ISC_TRUE, bargc,
1146 lookup = default_lookup;
1150 for (rc--, rv++; rc > 0; rc--, rv++) {
1151 debug("main parsing %s", rv[0]);
1152 if (strncmp(rv[0], "%", 1) == 0)
1154 if (strncmp(rv[0], "@", 1) == 0) {
1155 srv = make_server(&rv[0][1]);
1156 ISC_LIST_APPEND(lookup->my_server_list,
1158 } else if (rv[0][0] == '+') {
1159 plus_option(&rv[0][1], is_batchfile,
1161 } else if (rv[0][0] == '-') {
1163 if (dash_option(&rv[0][1], NULL,
1164 &lookup, &open_type_class,
1165 &firstarg, argc, argv)) {
1170 if (dash_option(&rv[0][1], rv[1],
1171 &lookup, &open_type_class,
1172 &firstarg, argc, argv)) {
1179 * Anything which isn't an option
1181 if (open_type_class) {
1182 if (strncmp(rv[0], "ixfr=", 5) == 0) {
1183 rdtype = dns_rdatatype_ixfr;
1184 result = ISC_R_SUCCESS;
1187 tr.length = strlen(rv[0]);
1188 result = dns_rdatatype_fromtext(&rdtype,
1189 (isc_textregion_t *)&tr);
1190 if (result == ISC_R_SUCCESS &&
1191 rdtype == dns_rdatatype_ixfr)
1193 result = DNS_R_UNKNOWN;
1194 fprintf(stderr, ";; Warning, "
1200 if (result == ISC_R_SUCCESS)
1202 if (lookup->rdtypeset) {
1203 fprintf(stderr, ";; Warning, "
1204 "extra type option\n");
1206 if (rdtype == dns_rdatatype_ixfr) {
1207 lookup->rdtype = dns_rdatatype_ixfr;
1208 lookup->rdtypeset = ISC_TRUE;
1209 lookup->ixfr_serial =
1210 parse_uint(&rv[0][5],
1213 lookup->section_question = plusquest;
1214 lookup->comments = pluscomm;
1216 lookup->rdtype = rdtype;
1217 lookup->rdtypeset = ISC_TRUE;
1218 if (rdtype == dns_rdatatype_axfr) {
1219 lookup->section_question =
1221 lookup->comments = pluscomm;
1223 lookup->ixfr_serial = ISC_FALSE;
1227 result = dns_rdataclass_fromtext(&rdclass,
1228 (isc_textregion_t *)&tr);
1229 if (result == ISC_R_SUCCESS) {
1230 if (lookup->rdclassset) {
1231 fprintf(stderr, ";; Warning, "
1232 "extra class option\n");
1234 lookup->rdclass = rdclass;
1235 lookup->rdclassset = ISC_TRUE;
1240 lookup = clone_lookup(default_lookup,
1243 printgreeting(argc, argv, lookup);
1244 firstarg = ISC_FALSE;
1246 strncpy(lookup->textname, rv[0],
1247 sizeof(lookup->textname));
1248 lookup->textname[sizeof(lookup->textname)-1]=0;
1249 lookup->trace_root = ISC_TF(lookup->trace ||
1250 lookup->ns_search_only);
1251 lookup->new_search = ISC_TRUE;
1252 ISC_LIST_APPEND(lookup_list, lookup, link);
1253 debug("looking up %s", lookup->textname);
1255 /* XXX Error message */
1259 * If we have a batchfile, seed the lookup list with the
1260 * first entry, then trust the callback in dighost_shutdown
1263 if ((batchname != NULL) && !(is_batchfile)) {
1264 if (strcmp(batchname, "-") == 0)
1267 batchfp = fopen(batchname, "r");
1268 if (batchfp == NULL) {
1272 fatal("Couldn't open specified batch file");
1274 /* XXX Remove code dup from shutdown code */
1276 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1278 debug("batch line %s", batchline);
1279 if (batchline[0] == '\r' || batchline[0] == '\n'
1280 || batchline[0] == '#' || batchline[0] == ';')
1283 bargv[bargc] = next_token(&input, " \t\r\n");
1284 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1286 bargv[bargc] = next_token(&input, " \t\r\n");
1292 reorder_args(bargc, (char **)bargv);
1293 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1297 * If no lookup specified, search for root
1299 if ((lookup_list.head == NULL) && !config_only) {
1300 lookup = clone_lookup(default_lookup, ISC_TRUE);
1301 lookup->trace_root = ISC_TF(lookup->trace ||
1302 lookup->ns_search_only);
1303 lookup->new_search = ISC_TRUE;
1304 strcpy(lookup->textname, ".");
1305 lookup->rdtype = dns_rdatatype_ns;
1306 lookup->rdtypeset = ISC_TRUE;
1308 printgreeting(argc, argv, lookup);
1309 firstarg = ISC_FALSE;
1311 ISC_LIST_APPEND(lookup_list, lookup, link);
1316 * Callback from dighost.c to allow program-specific shutdown code. Here,
1317 * Here, we're possibly reading from a batch file, then shutting down for
1318 * real if there's nothing in the batch file to read.
1321 dighost_shutdown(void) {
1322 char batchline[MXNAME];
1328 if (batchname == NULL) {
1334 if (feof(batchfp)) {
1337 if (batchfp != stdin)
1342 if (fgets(batchline, sizeof(batchline), batchfp) != 0) {
1343 debug("batch line %s", batchline);
1346 bargv[bargc] = next_token(&input, " \t\r\n");
1347 while ((bargv[bargc] != NULL) && (bargc < 14)) {
1349 bargv[bargc] = next_token(&input, " \t\r\n");
1354 reorder_args(bargc, (char **)bargv);
1355 parse_args(ISC_TRUE, ISC_FALSE, bargc, (char **)bargv);
1359 if (batchfp != stdin)
1367 main(int argc, char **argv) {
1368 isc_result_t result;
1369 dig_server_t *s, *s2;
1371 ISC_LIST_INIT(lookup_list);
1372 ISC_LIST_INIT(server_list);
1373 ISC_LIST_INIT(search_list);
1376 preparse_args(argc, argv);
1378 result = isc_app_start();
1379 check_result(result, "isc_app_start");
1381 parse_args(ISC_FALSE, ISC_FALSE, argc, argv);
1383 if (domainopt[0] != '\0') {
1384 set_search_domain(domainopt);
1385 usesearch = ISC_TRUE;
1387 result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
1388 check_result(result, "isc_app_onrun");
1390 s = ISC_LIST_HEAD(default_lookup->my_server_list);
1392 debug("freeing server %p belonging to %p",
1395 s = ISC_LIST_NEXT(s, link);
1396 ISC_LIST_DEQUEUE(default_lookup->my_server_list, s2, link);
1397 isc_mem_free(mctx, s2);
1399 isc_mem_free(mctx, default_lookup);
1400 if (batchname != NULL) {
1401 if (batchfp != stdin)