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