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