host.c 22 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10
11
 */

12
/*! \file */
13

14
#include <config.h>
15
16

#include <inttypes.h>
17
#include <stdbool.h>
18
#include <stdlib.h>
19
#include <limits.h>
20

21
22
23
24
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif

Michael Sawyer's avatar
Michael Sawyer committed
25
26
#include <isc/app.h>
#include <isc/commandline.h>
27
#include <isc/netaddr.h>
Andreas Gustafsson's avatar
Andreas Gustafsson committed
28
#include <isc/print.h>
Michael Sawyer's avatar
Michael Sawyer committed
29
30
#include <isc/string.h>
#include <isc/util.h>
31
#include <isc/task.h>
32

33
34
#include <dns/byaddr.h>
#include <dns/fixedname.h>
35
36
37
#include <dns/message.h>
#include <dns/name.h>
#include <dns/rdata.h>
38
#include <dns/rdataclass.h>
39
#include <dns/rdataset.h>
40
#include <dns/rdatatype.h>
41
#include <dns/rdatastruct.h>
42

43
44
#include <dig/dig.h>

45
46
static bool short_form = true, listed_server = false;
static bool default_lookups = true;
47
static int seen_error = -1;
48
49
static bool list_addresses = true;
static bool list_almost_all = false;
50
static dns_rdatatype_t list_type = dns_rdatatype_a;
51
52
static bool printed_server = false;
static bool ipv4only = false, ipv6only = false;
53

David Lawrence's avatar
David Lawrence committed
54
static const char *opcodetext[] = {
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
	"QUERY",
	"IQUERY",
	"STATUS",
	"RESERVED3",
	"NOTIFY",
	"UPDATE",
	"RESERVED6",
	"RESERVED7",
	"RESERVED8",
	"RESERVED9",
	"RESERVED10",
	"RESERVED11",
	"RESERVED12",
	"RESERVED13",
	"RESERVED14",
	"RESERVED15"
};

David Lawrence's avatar
David Lawrence committed
73
static const char *rcodetext[] = {
74
75
76
77
	"NOERROR",
	"FORMERR",
	"SERVFAIL",
	"NXDOMAIN",
78
	"NOTIMP",
79
80
81
82
83
84
85
86
87
88
89
90
91
92
	"REFUSED",
	"YXDOMAIN",
	"YXRRSET",
	"NXRRSET",
	"NOTAUTH",
	"NOTZONE",
	"RESERVED11",
	"RESERVED12",
	"RESERVED13",
	"RESERVED14",
	"RESERVED15",
	"BADVERS"
};

93
94
95
96
struct rtype {
	unsigned int type;
	const char *text;
};
97

98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
struct rtype rtypes[] = {
	{ 1, 	"has address" },
	{ 2, 	"name server" },
	{ 5, 	"is an alias for" },
	{ 11,	"has well known services" },
	{ 12,	"domain name pointer" },
	{ 13,	"host information" },
	{ 15,	"mail is handled by" },
	{ 16,	"descriptive text" },
	{ 19,	"x25 address" },
	{ 20,	"ISDN address" },
	{ 24,	"has signature" },
	{ 25,	"has key" },
	{ 28,	"has IPv6 address" },
	{ 29,	"location" },
	{ 0, NULL }
};
115

116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
static char *
rcode_totext(dns_rcode_t rcode)
{
	static char buf[sizeof("?65535")];
	union {
		const char *consttext;
		char *deconsttext;
	} totext;

	if (rcode >= (sizeof(rcodetext)/sizeof(rcodetext[0]))) {
		snprintf(buf, sizeof(buf), "?%u", rcode);
		totext.deconsttext = buf;
	} else
		totext.consttext = rcodetext[rcode];
	return totext.deconsttext;
}

Francis Dupont's avatar
Francis Dupont committed
133
134
135
ISC_PLATFORM_NORETURN_PRE static void
show_usage(void) ISC_PLATFORM_NORETURN_POST;

136
static void
137
show_usage(void) {
138
	fputs(
139
"Usage: host [-aCdilrTvVw] [-c class] [-N ndots] [-t type] [-W time]\n"
140
"            [-R number] [-m flag] hostname [server]\n"
141
"       -a is equivalent to -v -t ANY\n"
Evan Hunt's avatar
Evan Hunt committed
142
"       -A is like -a but omits RRSIG, NSEC, NSEC3\n"
143
"       -c specifies query class for non-IN data\n"
Brian Wellington's avatar
Brian Wellington committed
144
"       -C compares SOA records on authoritative nameservers\n"
145
"       -d is equivalent to -v\n"
146
147
"       -l lists all hosts in a domain, using AXFR\n"
"       -m set memory debugging flag (trace|record|usage)\n"
148
149
150
"       -N changes the number of dots allowed before root lookup is done\n"
"       -r disables recursive processing\n"
"       -R specifies number of retries for UDP packets\n"
151
"       -s a SERVFAIL response should stop query\n"
152
153
"       -t specifies the query type\n"
"       -T enables TCP/IP mode\n"
154
"       -U enables UDP mode\n"
155
"       -v enables verbose output\n"
156
"       -V print version number and exit\n"
157
"       -w specifies to wait forever for a reply\n"
158
159
"       -W specifies how long to wait for a reply\n"
"       -4 use IPv4 query transport only\n"
160
"       -6 use IPv6 query transport only\n", stderr);
161
	exit(1);
162
}
163

164
165
166
static void
host_shutdown(void) {
	(void) isc_app_shutdown();
167
168
}

169
static void
Mark Andrews's avatar
Mark Andrews committed
170
received(unsigned int bytes, isc_sockaddr_t *from, dig_query_t *query) {
171
172
173
	isc_time_t now;
	int diff;

Michael Sawyer's avatar
Michael Sawyer committed
174
	if (!short_form) {
175
176
		char fromtext[ISC_SOCKADDR_FORMATSIZE];
		isc_sockaddr_format(from, fromtext, sizeof(fromtext));
177
		TIME_NOW(&now);
Danny Mayer's avatar
Danny Mayer committed
178
		diff = (int) isc_time_microdiff(&now, &query->time_sent);
179
180
		printf("Received %u bytes from %s in %d ms\n",
		       bytes, fromtext, diff/1000);
181
	}
182
183
}

184
static void
Brian Wellington's avatar
Brian Wellington committed
185
trying(char *frm, dig_lookup_t *lookup) {
186
	UNUSED(lookup);
187
188

	if (!short_form)
Brian Wellington's avatar
Brian Wellington committed
189
		printf("Trying \"%s\"\n", frm);
190
191
}

192
static void
David Lawrence's avatar
David Lawrence committed
193
say_message(dns_name_t *name, const char *msg, dns_rdata_t *rdata,
194
195
	    dig_query_t *query)
{
Brian Wellington's avatar
Brian Wellington committed
196
197
198
	isc_buffer_t *b = NULL;
	char namestr[DNS_NAME_FORMATSIZE];
	isc_region_t r;
199
	isc_result_t result;
200
	unsigned int bufsize = BUFSIZ;
201

Brian Wellington's avatar
Brian Wellington committed
202
	dns_name_format(name, namestr, sizeof(namestr));
203
204
205
 retry:
	result = isc_buffer_allocate(mctx, &b, bufsize);
	check_result(result, "isc_buffer_allocate");
Brian Wellington's avatar
Brian Wellington committed
206
	result = dns_rdata_totext(rdata, NULL, b);
207
208
209
210
211
	if (result == ISC_R_NOSPACE) {
		isc_buffer_free(&b);
		bufsize *= 2;
		goto retry;
	}
212
	check_result(result, "dns_rdata_totext");
Brian Wellington's avatar
Brian Wellington committed
213
	isc_buffer_usedregion(b, &r);
Ben Cottrell's avatar
Ben Cottrell committed
214
	if (query->lookup->identify_previous_line) {
215
		printf("Nameserver %s:\n\t",
Ben Cottrell's avatar
Ben Cottrell committed
216
217
			query->servname);
	}
Brian Wellington's avatar
Brian Wellington committed
218
219
	printf("%s %s %.*s", namestr,
	       msg, (int)r.length, (char *)r.base);
220
	if (query->lookup->identify) {
221
		printf(" on server %s", query->servname);
222
	}
223
	printf("\n");
224
225
	isc_buffer_free(&b);
}
Evan Hunt's avatar
Evan Hunt committed
226

227
static isc_result_t
David Lawrence's avatar
David Lawrence committed
228
printsection(dns_message_t *msg, dns_section_t sectionid,
229
	     const char *section_name, bool headers,
David Lawrence's avatar
David Lawrence committed
230
	     dig_query_t *query)
231
232
233
{
	dns_name_t *name, *print_name;
	dns_rdataset_t *rdataset;
234
	dns_rdata_t rdata = DNS_RDATA_INIT;
235
236
237
238
	isc_buffer_t target;
	isc_result_t result, loopresult;
	isc_region_t r;
	dns_name_t empty_name;
239
	char tbuf[4096];
240
241
	bool first;
	bool no_rdata;
242

243
	if (sectionid == DNS_SECTION_QUESTION)
244
		no_rdata = true;
245
	else
246
		no_rdata = false;
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

	if (headers)
		printf(";; %s SECTION:\n", section_name);

	dns_name_init(&empty_name, NULL);

	result = dns_message_firstname(msg, sectionid);
	if (result == ISC_R_NOMORE)
		return (ISC_R_SUCCESS);
	else if (result != ISC_R_SUCCESS)
		return (result);

	for (;;) {
		name = NULL;
		dns_message_currentname(msg, sectionid, &name);

263
		isc_buffer_init(&target, tbuf, sizeof(tbuf));
264
		first = true;
265
266
267
268
269
		print_name = name;

		for (rdataset = ISC_LIST_HEAD(name->list);
		     rdataset != NULL;
		     rdataset = ISC_LIST_NEXT(rdataset, link)) {
270
271
272
			if (query->lookup->rdtype == dns_rdatatype_axfr &&
			    !((!list_addresses &&
			       (list_type == dns_rdatatype_any ||
Automatic Updater's avatar
Automatic Updater committed
273
				rdataset->type == list_type)) ||
274
275
			      (list_addresses &&
			       (rdataset->type == dns_rdatatype_a ||
Automatic Updater's avatar
Automatic Updater committed
276
				rdataset->type == dns_rdatatype_aaaa ||
277
278
279
				rdataset->type == dns_rdatatype_ns ||
				rdataset->type == dns_rdatatype_ptr))))
				continue;
Evan Hunt's avatar
Evan Hunt committed
280
281
282
283
284
			if (list_almost_all &&
			       (rdataset->type == dns_rdatatype_rrsig ||
				rdataset->type == dns_rdatatype_nsec ||
				rdataset->type == dns_rdatatype_nsec3))
				continue;
285
286
287
			if (!short_form) {
				result = dns_rdataset_totext(rdataset,
							     print_name,
288
							     false,
289
290
291
292
293
294
295
							     no_rdata,
							     &target);
				if (result != ISC_R_SUCCESS)
					return (result);
#ifdef USEINITALWS
				if (first) {
					print_name = &empty_name;
296
					first = false;
297
				}
298
299
#else
				UNUSED(first); /* Shut up compiler. */
300
#endif
301
			} else {
302
303
				loopresult = dns_rdataset_first(rdataset);
				while (loopresult == ISC_R_SUCCESS) {
304
305
306
307
308
					struct rtype *t;
					const char *rtt;
					char typebuf[DNS_RDATATYPE_FORMATSIZE];
					char typebuf2[DNS_RDATATYPE_FORMATSIZE
						     + 20];
309
					dns_rdataset_current(rdataset, &rdata);
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

					for (t = rtypes; t->text != NULL; t++) {
						if (t->type == rdata.type) {
							rtt = t->text;
							goto found;
						}
					}

					dns_rdatatype_format(rdata.type,
							     typebuf,
							     sizeof(typebuf));
					snprintf(typebuf2, sizeof(typebuf2),
						 "has %s record", typebuf);
					rtt = typebuf2;
				found:
Michael Sawyer's avatar
Michael Sawyer committed
325
					say_message(print_name, rtt,
326
						    &rdata, query);
327
					dns_rdata_reset(&rdata);
David Lawrence's avatar
David Lawrence committed
328
329
					loopresult =
						dns_rdataset_next(rdataset);
330
331
332
333
				}
			}
		}
		if (!short_form) {
334
			isc_buffer_usedregion(&target, &r);
335
336
337
338
339
340
			if (no_rdata)
				printf(";%.*s", (int)r.length,
				       (char *)r.base);
			else
				printf("%.*s", (int)r.length, (char *)r.base);
		}
341

342
343
344
345
346
347
		result = dns_message_nextname(msg, sectionid);
		if (result == ISC_R_NOMORE)
			break;
		else if (result != ISC_R_SUCCESS)
			return (result);
	}
348

349
350
351
352
	return (ISC_R_SUCCESS);
}

static isc_result_t
353
354
printrdata(dns_message_t *msg, dns_rdataset_t *rdataset,
	   const dns_name_t *owner, const char *set_name,
355
	   bool headers)
356
357
358
359
{
	isc_buffer_t target;
	isc_result_t result;
	isc_region_t r;
360
	char tbuf[4096];
361
362

	UNUSED(msg);
363
	if (headers)
364
365
		printf(";; %s SECTION:\n", set_name);

366
	isc_buffer_init(&target, tbuf, sizeof(tbuf));
367

368
	result = dns_rdataset_totext(rdataset, owner, false, false,
369
370
371
				     &target);
	if (result != ISC_R_SUCCESS)
		return (result);
372
	isc_buffer_usedregion(&target, &r);
373
374
375
376
377
	printf("%.*s", (int)r.length, (char *)r.base);

	return (ISC_R_SUCCESS);
}

378
379
380
381
382
383
384
385
static void
chase_cnamechain(dns_message_t *msg, dns_name_t *qname) {
	isc_result_t result;
	dns_rdataset_t *rdataset;
	dns_rdata_cname_t cname;
	dns_rdata_t rdata = DNS_RDATA_INIT;
	unsigned int i = msg->counts[DNS_SECTION_ANSWER];

Automatic Updater's avatar
Automatic Updater committed
386
	while (i-- > 0) {
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
		rdataset = NULL;
		result = dns_message_findname(msg, DNS_SECTION_ANSWER, qname,
					      dns_rdatatype_cname, 0, NULL,
					      &rdataset);
		if (result != ISC_R_SUCCESS)
			return;
		result = dns_rdataset_first(rdataset);
		check_result(result, "dns_rdataset_first");
		dns_rdata_reset(&rdata);
		dns_rdataset_current(rdataset, &rdata);
		result = dns_rdata_tostruct(&rdata, &cname, NULL);
		check_result(result, "dns_rdata_tostruct");
		dns_name_copy(&cname.cname, qname, NULL);
		dns_rdata_freestruct(&cname);
	}
}

404
static isc_result_t
405
406
printmessage(dig_query_t *query, dns_message_t *msg, bool headers) {
	bool did_flag = false;
407
	dns_rdataset_t *opt, *tsig = NULL;
408
	const dns_name_t *tsigname;
409
	isc_result_t result = ISC_R_SUCCESS;
410
	int force_error;
411

412
	UNUSED(headers);
413

414
415
416
417
418
419
	/*
	 * We get called multiple times.
	 * Preserve any existing error status.
	 */
	force_error = (seen_error == 1) ? 1 : 0;
	seen_error = 1;
420
	if (listed_server && !printed_server) {
Brian Wellington's avatar
Brian Wellington committed
421
422
		char sockstr[ISC_SOCKADDR_FORMATSIZE];

Michael Sawyer's avatar
Michael Sawyer committed
423
		printf("Using domain server:\n");
424
		printf("Name: %s\n", query->userarg);
Brian Wellington's avatar
Brian Wellington committed
425
426
427
		isc_sockaddr_format(&query->sockaddr, sockstr,
				    sizeof(sockstr));
		printf("Address: %s\n", sockstr);
Michael Sawyer's avatar
Michael Sawyer committed
428
		printf("Aliases: \n\n");
429
		printed_server = true;
Michael Sawyer's avatar
Michael Sawyer committed
430
431
	}

432
	if (msg->rcode != 0) {
Brian Wellington's avatar
Brian Wellington committed
433
434
		char namestr[DNS_NAME_FORMATSIZE];
		dns_name_format(query->lookup->name, namestr, sizeof(namestr));
435
436
437
438
439
440
441
442
443
444
445
446

		if (query->lookup->identify_previous_line)
			printf("Nameserver %s:\n\t%s not found: %d(%s)\n",
			       query->servname,
			       (msg->rcode != dns_rcode_nxdomain) ? namestr :
			       query->lookup->textname, msg->rcode,
			       rcode_totext(msg->rcode));
		else
			printf("Host %s not found: %d(%s)\n",
			       (msg->rcode != dns_rcode_nxdomain) ? namestr :
			       query->lookup->textname, msg->rcode,
			       rcode_totext(msg->rcode));
447
448
		return (ISC_R_SUCCESS);
	}
449
450
451
452

	if (default_lookups && query->lookup->rdtype == dns_rdatatype_a) {
		char namestr[DNS_NAME_FORMATSIZE];
		dig_lookup_t *lookup;
453
454
		dns_fixedname_t fixed;
		dns_name_t *name;
455
456

		/* Add AAAA and MX lookups. */
457
		name = dns_fixedname_initname(&fixed);
458
459
460
		dns_name_copy(query->lookup->name, name, NULL);
		chase_cnamechain(msg, name);
		dns_name_format(name, namestr, sizeof(namestr));
461
		lookup = clone_lookup(query->lookup, false);
462
		if (lookup != NULL) {
463
			strlcpy(lookup->textname, namestr,
464
465
				sizeof(lookup->textname));
			lookup->rdtype = dns_rdatatype_aaaa;
466
			lookup->rdtypeset = true;
467
468
469
470
			lookup->origin = NULL;
			lookup->retries = tries;
			ISC_LIST_APPEND(lookup_list, lookup, link);
		}
471
		lookup = clone_lookup(query->lookup, false);
472
		if (lookup != NULL) {
473
			strlcpy(lookup->textname, namestr,
474
475
				sizeof(lookup->textname));
			lookup->rdtype = dns_rdatatype_mx;
476
			lookup->rdtypeset = true;
477
478
479
480
481
482
			lookup->origin = NULL;
			lookup->retries = tries;
			ISC_LIST_APPEND(lookup_list, lookup, link);
		}
	}

483
484
	if (!short_form) {
		printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
485
		       opcodetext[msg->opcode], rcode_totext(msg->rcode),
486
487
488
489
		       msg->id);
		printf(";; flags: ");
		if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
			printf("qr");
490
			did_flag = true;
491
492
493
		}
		if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
			printf("%saa", did_flag ? " " : "");
494
			did_flag = true;
495
496
497
		}
		if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
			printf("%stc", did_flag ? " " : "");
498
			did_flag = true;
499
500
501
		}
		if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
			printf("%srd", did_flag ? " " : "");
502
			did_flag = true;
503
504
505
		}
		if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
			printf("%sra", did_flag ? " " : "");
506
			did_flag = true;
507
508
509
		}
		if ((msg->flags & DNS_MESSAGEFLAG_AD) != 0) {
			printf("%sad", did_flag ? " " : "");
510
			did_flag = true;
511
512
513
		}
		if ((msg->flags & DNS_MESSAGEFLAG_CD) != 0) {
			printf("%scd", did_flag ? " " : "");
514
			did_flag = true;
515
			POST(did_flag);
516
		}
517
518
		printf("; QUERY: %u, ANSWER: %u, "
		       "AUTHORITY: %u, ADDITIONAL: %u\n",
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
		       msg->counts[DNS_SECTION_QUESTION],
		       msg->counts[DNS_SECTION_ANSWER],
		       msg->counts[DNS_SECTION_AUTHORITY],
		       msg->counts[DNS_SECTION_ADDITIONAL]);
		opt = dns_message_getopt(msg);
		if (opt != NULL)
			printf(";; EDNS: version: %u, udp=%u\n",
			       (unsigned int)((opt->ttl & 0x00ff0000) >> 16),
			       (unsigned int)opt->rdclass);
		tsigname = NULL;
		tsig = dns_message_gettsig(msg, &tsigname);
		if (tsig != NULL)
			printf(";; PSEUDOSECTIONS: TSIG\n");
	}
	if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_QUESTION]) &&
534
	    !short_form) {
535
536
		printf("\n");
		result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION",
537
				      true, query);
538
539
540
541
542
543
544
		if (result != ISC_R_SUCCESS)
			return (result);
	}
	if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
		if (!short_form)
			printf("\n");
		result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER",
545
				      !short_form, query);
546
547
548
		if (result != ISC_R_SUCCESS)
			return (result);
	}
Michael Sawyer's avatar
Michael Sawyer committed
549

550
	if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_AUTHORITY]) &&
551
	    !short_form) {
552
553
		printf("\n");
		result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY",
554
				      true, query);
555
556
557
558
		if (result != ISC_R_SUCCESS)
			return (result);
	}
	if (! ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ADDITIONAL]) &&
559
	    !short_form) {
560
561
		printf("\n");
		result = printsection(msg, DNS_SECTION_ADDITIONAL,
562
				      "ADDITIONAL", true, query);
563
564
565
566
567
568
		if (result != ISC_R_SUCCESS)
			return (result);
	}
	if ((tsig != NULL) && !short_form) {
		printf("\n");
		result = printrdata(msg, tsig, tsigname,
569
				    "PSEUDOSECTION TSIG", true);
570
571
572
573
574
575
		if (result != ISC_R_SUCCESS)
			return (result);
	}
	if (!short_form)
		printf("\n");

576
577
	if (short_form && !default_lookups &&
	    ISC_LIST_EMPTY(msg->sections[DNS_SECTION_ANSWER])) {
578
579
580
581
582
583
584
		char namestr[DNS_NAME_FORMATSIZE];
		char typestr[DNS_RDATATYPE_FORMATSIZE];
		dns_name_format(query->lookup->name, namestr, sizeof(namestr));
		dns_rdatatype_format(query->lookup->rdtype, typestr,
				     sizeof(typestr));
		printf("%s has no %s record\n", namestr, typestr);
	}
585
	seen_error = force_error;
586
587
588
	return (result);
}

589
static const char * optstring = "46aAc:dilnm:rst:vVwCDN:R:TUW:";
590
591
592
593
594
595

/*% version */
static void
version(void) {
	fputs("host " VERSION "\n", stderr);
}
596
597
598
599
600
601
602
603

static void
pre_parse_args(int argc, char **argv) {
	int c;

	while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
		switch (c) {
		case 'm':
604
			memdebugging = true;
605
606
			if (strcasecmp("trace", isc_commandline_argument) == 0)
				isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
Francis Dupont's avatar
Francis Dupont committed
607
608
			else if (strcasecmp("record",
					    isc_commandline_argument) == 0)
609
610
611
612
613
614
				isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
			else if (strcasecmp("usage",
					    isc_commandline_argument) == 0)
				isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
			break;

615
616
617
		case '4':
			if (ipv6only)
				fatal("only one of -4 and -6 allowed");
618
			ipv4only = true;
619
620
621
622
			break;
		case '6':
			if (ipv4only)
				fatal("only one of -4 and -6 allowed");
623
			ipv6only = true;
624
			break;
625
		case 'a': break;
Evan Hunt's avatar
Evan Hunt committed
626
		case 'A': break;
627
		case 'c': break;
628
		case 'C': break;
629
		case 'd': break;
630
631
632
633
634
		case 'D':
			if (debugging)
				debugtiming = true;
			debugging = true;
			break;
635
636
637
		case 'i': break;
		case 'l': break;
		case 'n': break;
638
		case 'N': break;
639
		case 'r': break;
640
		case 'R': break;
641
		case 's': break;
642
		case 't': break;
643
644
		case 'T': break;
		case 'U': break;
645
		case 'v': break;
Tinderbox User's avatar
Tinderbox User committed
646
		case 'V':
647
648
649
			  version();
			  exit(0);
			  break;
650
651
652
653
654
655
		case 'w': break;
		case 'W': break;
		default:
			show_usage();
		}
	}
656
	isc_commandline_reset = true;
657
	isc_commandline_index = 1;
658
659
}

660
static void
661
parse_args(bool is_batchfile, int argc, char **argv) {
662
663
	char hostname[MXNAME];
	dig_lookup_t *lookup;
664
	int c;
665
	char store[MXNAME];
666
	isc_textregion_t tr;
667
	isc_result_t result = ISC_R_SUCCESS;
668
669
	dns_rdatatype_t rdtype;
	dns_rdataclass_t rdclass;
670
	uint32_t serial = 0;
671

672
673
	UNUSED(is_batchfile);

674
675
	lookup = make_empty_lookup();

676
677
	lookup->servfail_stops = false;
	lookup->comments = false;
678
	short_form = !verbose;
679

680
	while ((c = isc_commandline_parse(argc, argv, optstring)) != -1) {
681
682
		switch (c) {
		case 'l':
683
			lookup->tcp_mode = true;
684
			lookup->rdtype = dns_rdatatype_axfr;
685
			lookup->rdtypeset = true;
686
			fatalexit = 3;
687
688
689
			break;
		case 'v':
		case 'd':
690
			short_form = false;
691
692
			break;
		case 'r':
693
			lookup->recurse = false;
694
695
			break;
		case 't':
696
697
698
699
700
701
702
703
704
705
706
			if (strncasecmp(isc_commandline_argument,
					"ixfr=", 5) == 0) {
				rdtype = dns_rdatatype_ixfr;
				/* XXXMPA add error checking */
				serial = strtoul(isc_commandline_argument + 5,
						 NULL, 10);
				result = ISC_R_SUCCESS;
			} else {
				tr.base = isc_commandline_argument;
				tr.length = strlen(isc_commandline_argument);
				result = dns_rdatatype_fromtext(&rdtype,
707
						   (isc_textregion_t *)&tr);
708
			}
709

710
711
712
713
			if (result != ISC_R_SUCCESS) {
				fatalexit = 2;
				fatal("invalid type: %s\n",
				      isc_commandline_argument);
Michael Sawyer's avatar
Michael Sawyer committed
714
			}
715
716
717
			if (!lookup->rdtypeset ||
			    lookup->rdtype != dns_rdatatype_axfr)
				lookup->rdtype = rdtype;
718
			lookup->rdtypeset = true;
719
720
721
			if (rdtype == dns_rdatatype_axfr) {
				/* -l -t any -v */
				list_type = dns_rdatatype_any;
722
723
				short_form = false;
				lookup->tcp_mode = true;
724
725
			} else if (rdtype == dns_rdatatype_ixfr) {
				lookup->ixfr_serial = serial;
726
				lookup->tcp_mode = true;
727
				list_type = rdtype;
728
729
			} else if (rdtype == dns_rdatatype_any) {
				if (!lookup->tcp_mode_set)
730
					lookup->tcp_mode = true;
731
			} else
732
				list_type = rdtype;
733
734
			list_addresses = false;
			default_lookups = false;
735
			break;
736
		case 'c':
737
738
739
740
			tr.base = isc_commandline_argument;
			tr.length = strlen(isc_commandline_argument);
			result = dns_rdataclass_fromtext(&rdclass,
						   (isc_textregion_t *)&tr);
741

742
743
744
745
746
			if (result != ISC_R_SUCCESS) {
				fatalexit = 2;
				fatal("invalid class: %s\n",
				      isc_commandline_argument);
			} else {
747
				lookup->rdclass = rdclass;
748
				lookup->rdclassset = true;
Michael Sawyer's avatar
Michael Sawyer committed
749
			}
750
			default_lookups = false;
751
			break;
Evan Hunt's avatar
Evan Hunt committed
752
		case 'A':
753
			list_almost_all = true;
Evan Hunt's avatar
Evan Hunt committed
754
			/* FALL THROUGH */
755
		case 'a':
756
757
758
759
			if (!lookup->rdtypeset ||
			    lookup->rdtype != dns_rdatatype_axfr)
				lookup->rdtype = dns_rdatatype_any;
			list_type = dns_rdatatype_any;
760
761
762
763
			list_addresses = false;
			lookup->rdtypeset = true;
			short_form = false;
			default_lookups = false;
764
			break;
765
		case 'i':
766
			/* deprecated */
767
			break;
768
		case 'n':
769
			/* deprecated */
770
			break;
771
772
773
		case 'm':
			/* Handled by pre_parse_args(). */
			break;
774
		case 'w':
Michael Sawyer's avatar
Michael Sawyer committed
775
776
777
778
			/*
			 * The timer routines are coded such that
			 * timeout==MAXINT doesn't enable the timer
			 */
779
			timeout = INT_MAX;
780
			break;
781
782
783
784
785
786
		case 'W':
			timeout = atoi(isc_commandline_argument);
			if (timeout < 1)
				timeout = 1;
			break;
		case 'R':
787
788
789
			tries = atoi(isc_commandline_argument) + 1;
			if (tries < 2)
				tries = 2;
790
			break;
791
		case 'T':
792
793
			lookup->tcp_mode = true;
			lookup->tcp_mode_set = true;
794
795
			break;
		case 'U':
796
797
			lookup->tcp_mode = false;
			lookup->tcp_mode_set = true;
798
			break;
799
		case 'C':
Michael Sawyer's avatar
Michael Sawyer committed
800
			debug("showing all SOAs");
801
			lookup->rdtype = dns_rdatatype_ns;
802
			lookup->rdtypeset = true;
803
			lookup->rdclass = dns_rdataclass_in;
804
805
806
807
808
			lookup->rdclassset = true;
			lookup->ns_search_only = true;
			lookup->trace_root = true;
			lookup->identify_previous_line = true;
			default_lookups = false;
809
			break;
810
		case 'N':
811
			debug("setting NDOTS to %s",
812
			      isc_commandline_argument);
813
814
			ndots = atoi(isc_commandline_argument);
			break;
Michael Sawyer's avatar
Michael Sawyer committed
815
		case 'D':
816
			/* Handled by pre_parse_args(). */
Michael Sawyer's avatar
Michael Sawyer committed
817
			break;
818
		case '4':
819
			/* Handled by pre_parse_args(). */
820
821
			break;
		case '6':
822
			/* Handled by pre_parse_args(). */
823
			break;
824
		case 's':
825
			lookup->servfail_stops = true;
826
			break;
827
828
		}
	}
Andreas Gustafsson's avatar
style    
Andreas Gustafsson committed
829

830
831
	lookup->retries = tries;

Andreas Gustafsson's avatar
style    
Andreas Gustafsson committed
832
	if (isc_commandline_index >= argc)
833
		show_usage();
Andreas Gustafsson's avatar
style    
Andreas Gustafsson committed
834

835
836
	strlcpy(hostname, argv[isc_commandline_index], sizeof(hostname));

837
	if (argc > isc_commandline_index + 1) {
838
839
		set_nameserver(argv[isc_commandline_index+1]);
		debug("server is %s", argv[isc_commandline_index+1]);
840
		listed_server = true;
841
	} else
842
		check_ra = true;
843

844
	lookup->pending = false;
845
846
	if (get_reverse(store, sizeof(store), hostname, true)
	    == ISC_R_SUCCESS) {
847
		strlcpy(lookup->textname, store, sizeof(lookup->textname));
848
		lookup->rdtype = dns_rdatatype_ptr;
849
850
		lookup->rdtypeset = true;
		default_lookups = false;
851
	} else {
852
		strlcpy(lookup->textname, hostname, sizeof(lookup->textname));
853
		usesearch = true;
854
	}
855
	lookup->new_search = true;
856
857
858
	ISC_LIST_APPEND(lookup_list, lookup, link);
}

859
860
int
main(int argc, char **argv) {
861
862
	isc_result_t result;

863
864
	tries = 2;

865
866
867
	ISC_LIST_INIT(lookup_list);
	ISC_LIST_INIT(server_list);
	ISC_LIST_INIT(search_list);
Automatic Updater's avatar
Automatic Updater committed
868

869
	fatalexit = 1;
870

871
872
873
874
875
876
	/* setup dighost callbacks */
	dighost_printmessage = printmessage;
	dighost_received = received;
	dighost_trying = trying;
	dighost_shutdown = host_shutdown;

Michael Sawyer's avatar
Michael Sawyer committed
877
878
	debug("main()");
	progname = argv[0];
879
	pre_parse_args(argc, argv);
880
881
	result = isc_app_start();
	check_result(result, "isc_app_start");
882
	setup_libs();
883
	setup_system(ipv4only, ipv6only);
884
	parse_args(false, argc, argv);
885
886
887
888
	if (keyfile[0] != 0)
		setup_file_key();
	else if (keysecret[0] != 0)
		setup_text_key();
889
890
	result = isc_app_onrun(mctx, global_task, onrun_callback, NULL);
	check_result(result, "isc_app_onrun");
891
	isc_app_run();
892
893
	cancel_all();
	destroy_libs();
894
	isc_app_finish();
895
	return ((seen_error == 0) ? 0 : 1);
896
}