1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * SPDX-License-Identifier: MPL-2.0
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 /*! \file */
15 
16 #include <inttypes.h>
17 #include <limits.h>
18 #include <locale.h>
19 #include <stdbool.h>
20 #include <stdlib.h>
21 
22 #include <isc/app.h>
23 #include <isc/commandline.h>
24 #include <isc/netaddr.h>
25 #include <isc/print.h>
26 #include <isc/string.h>
27 #include <isc/task.h>
28 #include <isc/util.h>
29 
30 #include <dns/byaddr.h>
31 #include <dns/fixedname.h>
32 #include <dns/message.h>
33 #include <dns/name.h>
34 #include <dns/rdata.h>
35 #include <dns/rdataclass.h>
36 #include <dns/rdataset.h>
37 #include <dns/rdatastruct.h>
38 #include <dns/rdatatype.h>
39 
40 #include <dig/dig.h>
41 
42 static bool short_form = true, listed_server = false;
43 static bool default_lookups = true;
44 static int seen_error = -1;
45 static bool list_addresses = true;
46 static bool list_almost_all = false;
47 static dns_rdatatype_t list_type = dns_rdatatype_a;
48 static bool printed_server = false;
49 static bool ipv4only = false, ipv6only = false;
50 
51 static const char *opcodetext[] = { "QUERY",	  "IQUERY",	"STATUS",
52 				    "RESERVED3",  "NOTIFY",	"UPDATE",
53 				    "RESERVED6",  "RESERVED7",	"RESERVED8",
54 				    "RESERVED9",  "RESERVED10", "RESERVED11",
55 				    "RESERVED12", "RESERVED13", "RESERVED14",
56 				    "RESERVED15" };
57 
58 static const char *rcodetext[] = { "NOERROR",	 "FORMERR",    "SERVFAIL",
59 				   "NXDOMAIN",	 "NOTIMP",     "REFUSED",
60 				   "YXDOMAIN",	 "YXRRSET",    "NXRRSET",
61 				   "NOTAUTH",	 "NOTZONE",    "RESERVED11",
62 				   "RESERVED12", "RESERVED13", "RESERVED14",
63 				   "RESERVED15", "BADVERS" };
64 
65 struct rtype {
66 	unsigned int type;
67 	const char *text;
68 };
69 
70 struct rtype rtypes[] = { { 1, "has address" },
71 			  { 2, "name server" },
72 			  { 5, "is an alias for" },
73 			  { 11, "has well known services" },
74 			  { 12, "domain name pointer" },
75 			  { 13, "host information" },
76 			  { 15, "mail is handled by" },
77 			  { 16, "descriptive text" },
78 			  { 19, "x25 address" },
79 			  { 20, "ISDN address" },
80 			  { 24, "has signature" },
81 			  { 25, "has key" },
82 			  { 28, "has IPv6 address" },
83 			  { 29, "location" },
84 			  { 0, NULL } };
85 
86 static char *
rcode_totext(dns_rcode_t rcode)87 rcode_totext(dns_rcode_t rcode) {
88 	static char buf[sizeof("?65535")];
89 	union {
90 		const char *consttext;
91 		char *deconsttext;
92 	} totext;
93 
94 	if (rcode >= (sizeof(rcodetext) / sizeof(rcodetext[0]))) {
95 		snprintf(buf, sizeof(buf), "?%u", rcode);
96 		totext.deconsttext = buf;
97 	} else {
98 		totext.consttext = rcodetext[rcode];
99 	}
100 	return (totext.deconsttext);
101 }
102 
103 ISC_PLATFORM_NORETURN_PRE static void
104 show_usage(void) ISC_PLATFORM_NORETURN_POST;
105 
106 static void
show_usage(void)107 show_usage(void) {
108 	fputs("Usage: host [-aCdilrTvVw] [-c class] [-N ndots] [-t type] [-W "
109 	      "time]\n"
110 	      "            [-R number] [-m flag] [-p port] hostname [server]\n"
111 	      "       -a is equivalent to -v -t ANY\n"
112 	      "       -A is like -a but omits RRSIG, NSEC, NSEC3\n"
113 	      "       -c specifies query class for non-IN data\n"
114 	      "       -C compares SOA records on authoritative nameservers\n"
115 	      "       -d is equivalent to -v\n"
116 	      "       -l lists all hosts in a domain, using AXFR\n"
117 	      "       -m set memory debugging flag (trace|record|usage)\n"
118 	      "       -N changes the number of dots allowed before root lookup "
119 	      "is done\n"
120 	      "       -p specifies the port on the server to query\n"
121 	      "       -r disables recursive processing\n"
122 	      "       -R specifies number of retries for UDP packets\n"
123 	      "       -s a SERVFAIL response should stop query\n"
124 	      "       -t specifies the query type\n"
125 	      "       -T enables TCP/IP mode\n"
126 	      "       -U enables UDP mode\n"
127 	      "       -v enables verbose output\n"
128 	      "       -V print version number and exit\n"
129 	      "       -w specifies to wait forever for a reply\n"
130 	      "       -W specifies how long to wait for a reply\n"
131 	      "       -4 use IPv4 query transport only\n"
132 	      "       -6 use IPv6 query transport only\n",
133 	      stderr);
134 	exit(1);
135 }
136 
137 static void
host_shutdown(void)138 host_shutdown(void) {
139 	(void)isc_app_shutdown();
140 }
141 
142 static void
received(unsigned int bytes,isc_sockaddr_t * from,dig_query_t * query)143 received(unsigned int bytes, isc_sockaddr_t *from, dig_query_t *query) {
144 	isc_time_t now;
145 	int diff;
146 
147 	if (!short_form) {
148 		char fromtext[ISC_SOCKADDR_FORMATSIZE];
149 		isc_sockaddr_format(from, fromtext, sizeof(fromtext));
150 		if (query->lookup->use_usec) {
151 			TIME_NOW_HIRES(&now);
152 		} else {
153 			TIME_NOW(&now);
154 		}
155 		diff = (int)isc_time_microdiff(&now, &query->time_sent);
156 		printf("Received %u bytes from %s in %d ms\n", bytes, fromtext,
157 		       diff / 1000);
158 	}
159 }
160 
161 static void
trying(char * frm,dig_lookup_t * lookup)162 trying(char *frm, dig_lookup_t *lookup) {
163 	UNUSED(lookup);
164 
165 	if (!short_form) {
166 		printf("Trying \"%s\"\n", frm);
167 	}
168 }
169 
170 static void
say_message(dns_name_t * name,const char * msg,dns_rdata_t * rdata,dig_query_t * query)171 say_message(dns_name_t *name, const char *msg, dns_rdata_t *rdata,
172 	    dig_query_t *query) {
173 	isc_buffer_t *b = NULL;
174 	char namestr[DNS_NAME_FORMATSIZE];
175 	isc_region_t r;
176 	isc_result_t result;
177 	unsigned int bufsize = BUFSIZ;
178 
179 	dns_name_format(name, namestr, sizeof(namestr));
180 retry:
181 	isc_buffer_allocate(mctx, &b, bufsize);
182 	result = dns_rdata_totext(rdata, NULL, b);
183 	if (result == ISC_R_NOSPACE) {
184 		isc_buffer_free(&b);
185 		bufsize *= 2;
186 		goto retry;
187 	}
188 	check_result(result, "dns_rdata_totext");
189 	isc_buffer_usedregion(b, &r);
190 	if (query->lookup->identify_previous_line) {
191 		printf("Nameserver %s:\n\t", query->servname);
192 	}
193 	printf("%s %s %.*s", namestr, msg, (int)r.length, (char *)r.base);
194 	if (query->lookup->identify) {
195 		printf(" on server %s", query->servname);
196 	}
197 	printf("\n");
198 	isc_buffer_free(&b);
199 }
200 
201 static isc_result_t
printsection(dns_message_t * msg,dns_section_t sectionid,const char * section_name,bool headers,dig_query_t * query)202 printsection(dns_message_t *msg, dns_section_t sectionid,
203 	     const char *section_name, bool headers, dig_query_t *query) {
204 	dns_name_t *name, *print_name;
205 	dns_rdataset_t *rdataset;
206 	dns_rdata_t rdata = DNS_RDATA_INIT;
207 	isc_buffer_t target;
208 	isc_result_t result, loopresult;
209 	isc_region_t r;
210 	dns_name_t empty_name;
211 	char tbuf[4096];
212 	bool first;
213 	bool no_rdata;
214 
215 	if (sectionid == DNS_SECTION_QUESTION) {
216 		no_rdata = true;
217 	} else {
218 		no_rdata = false;
219 	}
220 
221 	if (headers) {
222 		printf(";; %s SECTION:\n", section_name);
223 	}
224 
225 	dns_name_init(&empty_name, NULL);
226 
227 	result = dns_message_firstname(msg, sectionid);
228 	if (result == ISC_R_NOMORE) {
229 		return (ISC_R_SUCCESS);
230 	} else if (result != ISC_R_SUCCESS) {
231 		return (result);
232 	}
233 
234 	for (;;) {
235 		name = NULL;
236 		dns_message_currentname(msg, sectionid, &name);
237 
238 		isc_buffer_init(&target, tbuf, sizeof(tbuf));
239 		first = true;
240 		print_name = name;
241 
242 		for (rdataset = ISC_LIST_HEAD(name->list); rdataset != NULL;
243 		     rdataset = ISC_LIST_NEXT(rdataset, link))
244 		{
245 			if (query->lookup->rdtype == dns_rdatatype_axfr &&
246 			    !((!list_addresses &&
247 			       (list_type == dns_rdatatype_any ||
248 				rdataset->type == list_type)) ||
249 			      (list_addresses &&
250 			       (rdataset->type == dns_rdatatype_a ||
251 				rdataset->type == dns_rdatatype_aaaa ||
252 				rdataset->type == dns_rdatatype_ns ||
253 				rdataset->type == dns_rdatatype_ptr))))
254 			{
255 				continue;
256 			}
257 			if (list_almost_all &&
258 			    (rdataset->type == dns_rdatatype_rrsig ||
259 			     rdataset->type == dns_rdatatype_nsec ||
260 			     rdataset->type == dns_rdatatype_nsec3))
261 			{
262 				continue;
263 			}
264 			if (!short_form) {
265 				result = dns_rdataset_totext(rdataset,
266 							     print_name, false,
267 							     no_rdata, &target);
268 				if (result != ISC_R_SUCCESS) {
269 					return (result);
270 				}
271 #ifdef USEINITALWS
272 				if (first) {
273 					print_name = &empty_name;
274 					first = false;
275 				}
276 #else  /* ifdef USEINITALWS */
277 				UNUSED(first); /* Shut up compiler. */
278 #endif /* ifdef USEINITALWS */
279 			} else {
280 				loopresult = dns_rdataset_first(rdataset);
281 				while (loopresult == ISC_R_SUCCESS) {
282 					struct rtype *t;
283 					const char *rtt;
284 					char typebuf[DNS_RDATATYPE_FORMATSIZE];
285 					char typebuf2[DNS_RDATATYPE_FORMATSIZE +
286 						      20];
287 					dns_rdataset_current(rdataset, &rdata);
288 
289 					for (t = rtypes; t->text != NULL; t++) {
290 						if (t->type == rdata.type) {
291 							rtt = t->text;
292 							goto found;
293 						}
294 					}
295 
296 					dns_rdatatype_format(rdata.type,
297 							     typebuf,
298 							     sizeof(typebuf));
299 					snprintf(typebuf2, sizeof(typebuf2),
300 						 "has %s record", typebuf);
301 					rtt = typebuf2;
302 				found:
303 					say_message(print_name, rtt, &rdata,
304 						    query);
305 					dns_rdata_reset(&rdata);
306 					loopresult =
307 						dns_rdataset_next(rdataset);
308 				}
309 			}
310 		}
311 		if (!short_form) {
312 			isc_buffer_usedregion(&target, &r);
313 			if (no_rdata) {
314 				printf(";%.*s", (int)r.length, (char *)r.base);
315 			} else {
316 				printf("%.*s", (int)r.length, (char *)r.base);
317 			}
318 		}
319 
320 		result = dns_message_nextname(msg, sectionid);
321 		if (result == ISC_R_NOMORE) {
322 			break;
323 		} else if (result != ISC_R_SUCCESS) {
324 			return (result);
325 		}
326 	}
327 
328 	return (ISC_R_SUCCESS);
329 }
330 
331 static isc_result_t
printrdata(dns_message_t * msg,dns_rdataset_t * rdataset,const dns_name_t * owner,const char * set_name,bool headers)332 printrdata(dns_message_t *msg, dns_rdataset_t *rdataset,
333 	   const dns_name_t *owner, const char *set_name, bool headers) {
334 	isc_buffer_t target;
335 	isc_result_t result;
336 	isc_region_t r;
337 	char tbuf[4096];
338 
339 	UNUSED(msg);
340 	if (headers) {
341 		printf(";; %s SECTION:\n", set_name);
342 	}
343 
344 	isc_buffer_init(&target, tbuf, sizeof(tbuf));
345 
346 	result = dns_rdataset_totext(rdataset, owner, false, false, &target);
347 	if (result != ISC_R_SUCCESS) {
348 		return (result);
349 	}
350 	isc_buffer_usedregion(&target, &r);
351 	printf("%.*s", (int)r.length, (char *)r.base);
352 
353 	return (ISC_R_SUCCESS);
354 }
355 
356 static void
chase_cnamechain(dns_message_t * msg,dns_name_t * qname)357 chase_cnamechain(dns_message_t *msg, dns_name_t *qname) {
358 	isc_result_t result;
359 	dns_rdataset_t *rdataset;
360 	dns_rdata_cname_t cname;
361 	dns_rdata_t rdata = DNS_RDATA_INIT;
362 	unsigned int i = msg->counts[DNS_SECTION_ANSWER];
363 
364 	while (i-- > 0) {
365 		rdataset = NULL;
366 		result = dns_message_findname(msg, DNS_SECTION_ANSWER, qname,
367 					      dns_rdatatype_cname, 0, NULL,
368 					      &rdataset);
369 		if (result != ISC_R_SUCCESS) {
370 			return;
371 		}
372 		result = dns_rdataset_first(rdataset);
373 		check_result(result, "dns_rdataset_first");
374 		dns_rdata_reset(&rdata);
375 		dns_rdataset_current(rdataset, &rdata);
376 		result = dns_rdata_tostruct(&rdata, &cname, NULL);
377 		check_result(result, "dns_rdata_tostruct");
378 		dns_name_copynf(&cname.cname, qname);
379 		dns_rdata_freestruct(&cname);
380 	}
381 }
382 
383 static isc_result_t
printmessage(dig_query_t * query,const isc_buffer_t * msgbuf,dns_message_t * msg,bool headers)384 printmessage(dig_query_t *query, const isc_buffer_t *msgbuf, dns_message_t *msg,
385 	     bool headers) {
386 	bool did_flag = false;
387 	dns_rdataset_t *opt, *tsig = NULL;
388 	const dns_name_t *tsigname;
389 	isc_result_t result = ISC_R_SUCCESS;
390 	int force_error;
391 
392 	UNUSED(msgbuf);
393 	UNUSED(headers);
394 
395 	/*
396 	 * We get called multiple times.
397 	 * Preserve any existing error status.
398 	 */
399 	force_error = (seen_error == 1) ? 1 : 0;
400 	seen_error = 1;
401 	if (listed_server && !printed_server) {
402 		char sockstr[ISC_SOCKADDR_FORMATSIZE];
403 
404 		printf("Using domain server:\n");
405 		printf("Name: %s\n", query->userarg);
406 		isc_sockaddr_format(&query->sockaddr, sockstr, sizeof(sockstr));
407 		printf("Address: %s\n", sockstr);
408 		printf("Aliases: \n\n");
409 		printed_server = true;
410 	}
411 
412 	if (msg->rcode != 0) {
413 		char namestr[DNS_NAME_FORMATSIZE];
414 		dns_name_format(query->lookup->name, namestr, sizeof(namestr));
415 
416 		if (query->lookup->identify_previous_line) {
417 			printf("Nameserver %s:\n\t%s not found: %d(%s)\n",
418 			       query->servname,
419 			       (msg->rcode != dns_rcode_nxdomain)
420 				       ? namestr
421 				       : query->lookup->textname,
422 			       msg->rcode, rcode_totext(msg->rcode));
423 		} else {
424 			printf("Host %s not found: %d(%s)\n",
425 			       (msg->rcode != dns_rcode_nxdomain)
426 				       ? namestr
427 				       : query->lookup->textname,
428 			       msg->rcode, rcode_totext(msg->rcode));
429 		}
430 		return (ISC_R_SUCCESS);
431 	}
432 
433 	if (default_lookups && query->lookup->rdtype == dns_rdatatype_a) {
434 		char namestr[DNS_NAME_FORMATSIZE];
435 		dig_lookup_t *lookup;
436 		dns_fixedname_t fixed;
437 		dns_name_t *name;
438 
439 		/* Add AAAA and MX lookups. */
440 		name = dns_fixedname_initname(&fixed);
441 		dns_name_copynf(query->lookup->name, name);
442 		chase_cnamechain(msg, name);
443 		dns_name_format(name, namestr, sizeof(namestr));
444 		lookup = clone_lookup(query->lookup, false);
445 		if (lookup != NULL) {
446 			strlcpy(lookup->textname, namestr,
447 				sizeof(lookup->textname));
448 			lookup->rdtype = dns_rdatatype_aaaa;
449 			lookup->rdtypeset = true;
450 			lookup->origin = NULL;
451 			lookup->retries = tries;
452 			ISC_LIST_APPEND(lookup_list, lookup, link);
453 		}
454 		lookup = clone_lookup(query->lookup, false);
455 		if (lookup != NULL) {
456 			strlcpy(lookup->textname, namestr,
457 				sizeof(lookup->textname));
458 			lookup->rdtype = dns_rdatatype_mx;
459 			lookup->rdtypeset = true;
460 			lookup->origin = NULL;
461 			lookup->retries = tries;
462 			ISC_LIST_APPEND(lookup_list, lookup, link);
463 		}
464 	}
465 
466 	if (!short_form) {
467 		printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
468 		       opcodetext[msg->opcode], rcode_totext(msg->rcode),
469 		       msg->id);
470 		printf(";; flags: ");
471 		if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
472 			printf("qr");
473 			did_flag = true;
474 		}
475 		if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
476 			printf("%saa", did_flag ? " " : "");
477 			did_flag = true;
478 		}
479 		if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
480 			printf("%stc", did_flag ? " " : "");
481 			did_flag = true;
482 		}
483 		if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
484 			printf("%srd", did_flag ? " " : "");
485 			did_flag = true;
486 		}
487 		if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
488 			printf("%sra", did_flag ? " " : "");
489 			did_flag = true;
490 		}
491 		if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0) {
492 			printf("%sad", did_flag ? " " : "");
493 			did_flag = true;
494 		}
495 		if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0) {
496 			printf("%scd", did_flag ? " " : "");
497 			did_flag = true;
498 			POST(did_flag);
499 		}
500 		printf("; QUERY: %u, ANSWER: %u, "
501 		       "AUTHORITY: %u, ADDITIONAL: %u\n",
502 		       msg->counts[DNS_SECTION_QUESTION],
503 		       msg->counts[DNS_SECTION_ANSWER],
504 		       msg->counts[DNS_SECTION_AUTHORITY],
505 		       msg->counts[DNS_SECTION_ADDITIONAL]);
506 		opt = dns_message_getopt(msg);
507 		if (opt != NULL) {
508 			printf(";; EDNS: version: %u, udp=%u\n",
509 			       (unsigned int)((opt->ttl & 0x00ff0000) >> 16),
510 			       (unsigned int)opt->rdclass);
511 		}
512 		tsigname = NULL;
513 		tsig = dns_message_gettsig(msg, &tsigname);
514 		if (tsig != NULL) {
515 			printf(";; PSEUDOSECTIONS: TSIG\n");
516 		}
517 	}
518 	if (!ISC_LIST_EMPTY(msg->sections[DNS_SECTION_QUESTION]) && !short_form)
519 	{
520 		printf("\n");
521 		result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION",
522 				      true, query);
523 		if (result != ISC_R_SUCCESS) {
524 			return (result);
525 		}
526 	}
527 	if (!ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
528 		if (!short_form) {
529 			printf("\n");
530 		}
531 		result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER",
532 				      !short_form, query);
533 		if (result != ISC_R_SUCCESS) {
534 			return (result);
535 		}
536 	}
537 
538 	if (!ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) &&
539 	    !short_form) {
540 		printf("\n");
541 		result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY",
542 				      true, query);
543 		if (result != ISC_R_SUCCESS) {
544 			return (result);
545 		}
546 	}
547 	if (!ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL]) &&
548 	    !short_form) {
549 		printf("\n");
550 		result = printsection(msg, DNS_SECTION_ADDITIONAL, "ADDITIONAL",
551 				      true, query);
552 		if (result != ISC_R_SUCCESS) {
553 			return (result);
554 		}
555 	}
556 	if ((tsig != NULL) && !short_form) {
557 		printf("\n");
558 		result = printrdata(msg, tsig, tsigname, "PSEUDOSECTION TSIG",
559 				    true);
560 		if (result != ISC_R_SUCCESS) {
561 			return (result);
562 		}
563 	}
564 	if (!short_form) {
565 		printf("\n");
566 	}
567 
568 	if (short_form && !default_lookups &&
569 	    ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER]))
570 	{
571 		char namestr[DNS_NAME_FORMATSIZE];
572 		char typestr[DNS_RDATATYPE_FORMATSIZE];
573 		dns_name_format(query->lookup->name, namestr, sizeof(namestr));
574 		dns_rdatatype_format(query->lookup->rdtype, typestr,
575 				     sizeof(typestr));
576 		printf("%s has no %s record\n", namestr, typestr);
577 	}
578 	seen_error = force_error;
579 	return (result);
580 }
581 
582 static const char *optstring = "46aAc:dilnm:p:rst:vVwCDN:R:TUW:";
583 
584 /*% version */
585 static void
version(void)586 version(void) {
587 	fputs("host " VERSION "\n", stderr);
588 }
589 
590 static void
pre_parse_args(int argc,char ** argv)591 pre_parse_args(int argc, char **argv) {
592 	int c;
593 
594 	while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
595 		switch (c) {
596 		case 'm':
597 			memdebugging = true;
598 			if (strcasecmp("trace", isc_commandline_argument) == 0)
599 			{
600 				isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
601 			} else if (strcasecmp("record",
602 					      isc_commandline_argument) == 0) {
603 				isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
604 			} else if (strcasecmp("usage",
605 					      isc_commandline_argument) == 0) {
606 				isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
607 			}
608 			break;
609 
610 		case '4':
611 			if (ipv6only) {
612 				fatal("only one of -4 and -6 allowed");
613 			}
614 			ipv4only = true;
615 			break;
616 		case '6':
617 			if (ipv4only) {
618 				fatal("only one of -4 and -6 allowed");
619 			}
620 			ipv6only = true;
621 			break;
622 		case 'a':
623 			break;
624 		case 'A':
625 			break;
626 		case 'c':
627 			break;
628 		case 'C':
629 			break;
630 		case 'd':
631 			break;
632 		case 'D':
633 			if (debugging) {
634 				debugtiming = true;
635 			}
636 			debugging = true;
637 			break;
638 		case 'i':
639 			break;
640 		case 'l':
641 			break;
642 		case 'n':
643 			break;
644 		case 'N':
645 			break;
646 		case 'p':
647 			break;
648 		case 'r':
649 			break;
650 		case 'R':
651 			break;
652 		case 's':
653 			break;
654 		case 't':
655 			break;
656 		case 'T':
657 			break;
658 		case 'U':
659 			break;
660 		case 'v':
661 			break;
662 		case 'V':
663 			version();
664 			exit(0);
665 			break;
666 		case 'w':
667 			break;
668 		case 'W':
669 			break;
670 		default:
671 			show_usage();
672 		}
673 	}
674 	isc_commandline_reset = true;
675 	isc_commandline_index = 1;
676 }
677 
678 static void
parse_args(bool is_batchfile,int argc,char ** argv)679 parse_args(bool is_batchfile, int argc, char **argv) {
680 	char hostname[MXNAME];
681 	dig_lookup_t *lookup;
682 	int c;
683 	char store[MXNAME];
684 	isc_textregion_t tr;
685 	isc_result_t result = ISC_R_SUCCESS;
686 	dns_rdatatype_t rdtype;
687 	dns_rdataclass_t rdclass;
688 	uint32_t serial = 0;
689 
690 	UNUSED(is_batchfile);
691 
692 	lookup = make_empty_lookup();
693 
694 	lookup->servfail_stops = false;
695 	lookup->besteffort = false;
696 	lookup->comments = false;
697 	short_form = !verbose;
698 
699 	while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
700 		switch (c) {
701 		case 'l':
702 			lookup->tcp_mode = true;
703 			lookup->rdtype = dns_rdatatype_axfr;
704 			lookup->rdtypeset = true;
705 			fatalexit = 3;
706 			break;
707 		case 'v':
708 		case 'd':
709 			short_form = false;
710 			break;
711 		case 'r':
712 			lookup->recurse = false;
713 			break;
714 		case 't':
715 			if (strncasecmp(isc_commandline_argument, "ixfr=", 5) ==
716 			    0) {
717 				rdtype = dns_rdatatype_ixfr;
718 				/* XXXMPA add error checking */
719 				serial = strtoul(isc_commandline_argument + 5,
720 						 NULL, 10);
721 				result = ISC_R_SUCCESS;
722 			} else {
723 				tr.base = isc_commandline_argument;
724 				tr.length = strlen(isc_commandline_argument);
725 				result = dns_rdatatype_fromtext(
726 					&rdtype, (isc_textregion_t *)&tr);
727 			}
728 
729 			if (result != ISC_R_SUCCESS) {
730 				fatalexit = 2;
731 				fatal("invalid type: %s\n",
732 				      isc_commandline_argument);
733 			}
734 			if (!lookup->rdtypeset ||
735 			    lookup->rdtype != dns_rdatatype_axfr) {
736 				lookup->rdtype = rdtype;
737 			}
738 			lookup->rdtypeset = true;
739 			if (rdtype == dns_rdatatype_axfr) {
740 				/* -l -t any -v */
741 				list_type = dns_rdatatype_any;
742 				short_form = false;
743 				lookup->tcp_mode = true;
744 			} else if (rdtype == dns_rdatatype_ixfr) {
745 				lookup->ixfr_serial = serial;
746 				lookup->tcp_mode = true;
747 				list_type = rdtype;
748 			} else if (rdtype == dns_rdatatype_any) {
749 				if (!lookup->tcp_mode_set) {
750 					lookup->tcp_mode = true;
751 				}
752 			} else {
753 				list_type = rdtype;
754 			}
755 			list_addresses = false;
756 			default_lookups = false;
757 			break;
758 		case 'c':
759 			tr.base = isc_commandline_argument;
760 			tr.length = strlen(isc_commandline_argument);
761 			result = dns_rdataclass_fromtext(
762 				&rdclass, (isc_textregion_t *)&tr);
763 
764 			if (result != ISC_R_SUCCESS) {
765 				fatalexit = 2;
766 				fatal("invalid class: %s\n",
767 				      isc_commandline_argument);
768 			} else {
769 				lookup->rdclass = rdclass;
770 				lookup->rdclassset = true;
771 			}
772 			default_lookups = false;
773 			break;
774 		case 'A':
775 			list_almost_all = true;
776 		/* FALL THROUGH */
777 		case 'a':
778 			if (!lookup->rdtypeset ||
779 			    lookup->rdtype != dns_rdatatype_axfr) {
780 				lookup->rdtype = dns_rdatatype_any;
781 			}
782 			list_type = dns_rdatatype_any;
783 			list_addresses = false;
784 			lookup->rdtypeset = true;
785 			short_form = false;
786 			default_lookups = false;
787 			break;
788 		case 'i':
789 			/* deprecated */
790 			break;
791 		case 'n':
792 			/* deprecated */
793 			break;
794 		case 'm':
795 			/* Handled by pre_parse_args(). */
796 			break;
797 		case 'w':
798 			/*
799 			 * The timer routines are coded such that
800 			 * timeout==MAXINT doesn't enable the timer
801 			 */
802 			timeout = INT_MAX;
803 			break;
804 		case 'W':
805 			timeout = atoi(isc_commandline_argument);
806 			if (timeout < 1) {
807 				timeout = 1;
808 			}
809 			break;
810 		case 'R':
811 			tries = atoi(isc_commandline_argument) + 1;
812 			if (tries < 2) {
813 				tries = 2;
814 			}
815 			break;
816 		case 'T':
817 			lookup->tcp_mode = true;
818 			lookup->tcp_mode_set = true;
819 			break;
820 		case 'U':
821 			lookup->tcp_mode = false;
822 			lookup->tcp_mode_set = true;
823 			break;
824 		case 'C':
825 			debug("showing all SOAs");
826 			lookup->rdtype = dns_rdatatype_ns;
827 			lookup->rdtypeset = true;
828 			lookup->rdclass = dns_rdataclass_in;
829 			lookup->rdclassset = true;
830 			lookup->ns_search_only = true;
831 			lookup->trace_root = true;
832 			lookup->identify_previous_line = true;
833 			default_lookups = false;
834 			break;
835 		case 'N':
836 			debug("setting NDOTS to %s", isc_commandline_argument);
837 			ndots = atoi(isc_commandline_argument);
838 			break;
839 		case 'D':
840 			/* Handled by pre_parse_args(). */
841 			break;
842 		case '4':
843 			/* Handled by pre_parse_args(). */
844 			break;
845 		case '6':
846 			/* Handled by pre_parse_args(). */
847 			break;
848 		case 's':
849 			lookup->servfail_stops = true;
850 			break;
851 		case 'p':
852 			port = atoi(isc_commandline_argument);
853 			break;
854 		}
855 	}
856 
857 	lookup->retries = tries;
858 
859 	if (isc_commandline_index >= argc) {
860 		show_usage();
861 	}
862 
863 	strlcpy(hostname, argv[isc_commandline_index], sizeof(hostname));
864 
865 	if (argc > isc_commandline_index + 1) {
866 		set_nameserver(argv[isc_commandline_index + 1]);
867 		debug("server is %s", argv[isc_commandline_index + 1]);
868 		listed_server = true;
869 	} else {
870 		check_ra = true;
871 	}
872 
873 	lookup->pending = false;
874 	if (get_reverse(store, sizeof(store), hostname, true) == ISC_R_SUCCESS)
875 	{
876 		strlcpy(lookup->textname, store, sizeof(lookup->textname));
877 		lookup->rdtype = dns_rdatatype_ptr;
878 		lookup->rdtypeset = true;
879 		default_lookups = false;
880 	} else {
881 		strlcpy(lookup->textname, hostname, sizeof(lookup->textname));
882 		usesearch = true;
883 	}
884 	lookup->new_search = true;
885 	ISC_LIST_APPEND(lookup_list, lookup, link);
886 }
887 
888 int
main(int argc,char ** argv)889 main(int argc, char **argv) {
890 	isc_result_t result;
891 
892 	tries = 2;
893 
894 	ISC_LIST_INIT(lookup_list);
895 	ISC_LIST_INIT(server_list);
896 	ISC_LIST_INIT(search_list);
897 
898 	fatalexit = 1;
899 
900 	/* setup dighost callbacks */
901 	dighost_printmessage = printmessage;
902 	dighost_received = received;
903 	dighost_trying = trying;
904 	dighost_shutdown = host_shutdown;
905 
906 	debug("main()");
907 	progname = argv[0];
908 	pre_parse_args(argc, argv);
909 	result = isc_app_start();
910 	check_result(result, "isc_app_start");
911 	setup_libs();
912 	setup_system(ipv4only, ipv6only);
913 	parse_args(false, argc, argv);
914 	if (keyfile[0] != 0) {
915 		setup_file_key();
916 	} else if (keysecret[0] != 0) {
917 		setup_text_key();
918 	}
919 	result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
920 	check_result(result, "isc_app_onrun");
921 	isc_app_run();
922 	cancel_all();
923 	destroy_libs();
924 	isc_app_finish();
925 	return ((seen_error == 0) ? 0 : 1);
926 }
927