2 * checkconf/unbound-host.c - replacement for host that supports validation.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file performs functionality like 'host', and also supports validation.
40 * It uses the libunbound library.
47 /* remove alloc checks, not in this part of the code */
48 #ifdef UNBOUND_ALLOC_STATS
54 #ifdef UNBOUND_ALLOC_LITE
60 #define unbound_lite_wrapstr(s) s
62 #include "libunbound/unbound.h"
63 #include "sldns/rrdef.h"
64 #include "sldns/wire2str.h"
70 /** verbosity for unbound-host app */
73 /** Give unbound-host usage, and exit (1). */
77 printf("Usage: unbound-host [-vdhr46] [-c class] [-t type] hostname\n");
78 printf(" [-y key] [-f keyfile] [-F namedkeyfile]\n");
79 printf(" [-C configfile]\n");
80 printf(" Queries the DNS for information.\n");
81 printf(" The hostname is looked up for IP4, IP6 and mail.\n");
82 printf(" If an ip-address is given a reverse lookup is done.\n");
83 printf(" Use the -v option to see DNSSEC security information.\n");
84 printf(" -t type what type to look for.\n");
85 printf(" -c class what class to look for, if not class IN.\n");
86 printf(" -y 'keystring' specify trust anchor, DS or DNSKEY, like\n");
87 printf(" -y 'example.com DS 31560 5 1 1CFED8478...'\n");
88 printf(" -D DNSSEC enable with default root anchor\n");
89 printf(" from %s\n", ROOT_ANCHOR_FILE);
90 printf(" -f keyfile read trust anchors from file, with lines as -y.\n");
91 printf(" -F keyfile read named.conf-style trust anchors.\n");
92 printf(" -C config use the specified unbound.conf (none read by default)\n");
93 printf(" -r read forwarder information from /etc/resolv.conf\n");
94 printf(" breaks validation if the forwarder does not do DNSSEC.\n");
95 printf(" -v be more verbose, shows nodata and security.\n");
96 printf(" -d debug, traces the action, -d -d shows more.\n");
97 printf(" -4 use ipv4 network, avoid ipv6.\n");
98 printf(" -6 use ipv6 network, avoid ipv4.\n");
99 printf(" -h show this usage help.\n");
100 printf("Version %s\n", PACKAGE_VERSION);
101 printf("BSD licensed, see LICENSE in source package for details.\n");
102 printf("Report bugs to %s\n", PACKAGE_BUGREPORT);
106 /** determine if str is ip4 and put into reverse lookup format */
108 isip4(const char* nm, char** res)
111 /* ddd.ddd.ddd.ddd.in-addr.arpa. is less than 32 */
113 if(inet_pton(AF_INET, nm, &addr) <= 0) {
116 snprintf(buf, sizeof(buf), "%u.%u.%u.%u.in-addr.arpa",
117 (unsigned)((uint8_t*)&addr)[3], (unsigned)((uint8_t*)&addr)[2],
118 (unsigned)((uint8_t*)&addr)[1], (unsigned)((uint8_t*)&addr)[0]);
123 /** determine if str is ip6 and put into reverse lookup format */
125 isip6(const char* nm, char** res)
127 struct in6_addr addr;
128 /* [nibble.]{32}.ip6.arpa. is less than 128 */
129 const char* hex = "0123456789abcdef";
133 if(inet_pton(AF_INET6, nm, &addr) <= 0) {
137 for(i=15; i>=0; i--) {
138 uint8_t b = ((uint8_t*)&addr)[i];
139 *p++ = hex[ (b&0x0f) ];
141 *p++ = hex[ (b&0xf0) >> 4 ];
144 snprintf(buf+16*4, sizeof(buf)-16*4, "ip6.arpa");
147 fprintf(stderr, "error: out of memory\n");
153 /** massage input name */
155 massage_qname(const char* nm, int* reverse)
157 /* recognise IP4 and IP6, create reverse addresses if needed */
159 if(isip4(nm, &res)) {
161 } else if(isip6(nm, &res)) {
167 fprintf(stderr, "error: out of memory\n");
173 /** massage input type */
175 massage_type(const char* t, int reverse, int* multi)
178 int r = sldns_get_rr_type_by_name(t);
179 if(r == 0 && strcasecmp(t, "TYPE0") != 0 &&
180 strcmp(t, "") != 0) {
181 fprintf(stderr, "error unknown type %s\n", t);
187 return LDNS_RR_TYPE_PTR;
189 return LDNS_RR_TYPE_A;
192 /** massage input class */
194 massage_class(const char* c)
197 int r = sldns_get_rr_class_by_name(c);
198 if(r == 0 && strcasecmp(c, "CLASS0") != 0 &&
199 strcmp(c, "") != 0) {
200 fprintf(stderr, "error unknown class %s\n", c);
205 return LDNS_RR_CLASS_IN;
208 /** nice security status string */
210 secure_str(struct ub_result* result)
212 if(result->rcode != 0 && result->rcode != 3) return "(error)";
213 if(result->secure) return "(secure)";
214 if(result->bogus) return "(BOGUS (security failure))";
218 /** nice string for type */
220 pretty_type(char* s, size_t len, int t)
223 sldns_wire2str_type_buf((uint16_t)t, d, sizeof(d));
224 snprintf(s, len, "%s", d);
227 /** nice string for class */
229 pretty_class(char* s, size_t len, int c)
232 sldns_wire2str_class_buf((uint16_t)c, d, sizeof(d));
233 snprintf(s, len, "%s", d);
236 /** nice string for rcode */
238 pretty_rcode(char* s, size_t len, int r)
241 sldns_wire2str_rcode_buf(r, d, sizeof(d));
242 snprintf(s, len, "%s", d);
245 /** convert and print rdata */
247 print_rd(int t, char* data, size_t len)
250 sldns_wire2str_rdata_buf((uint8_t*)data, len, s, sizeof(s), (uint16_t)t);
254 /** pretty line of RR data for results */
256 pretty_rdata(char* q, char* cstr, char* tstr, int t, const char* sec,
257 char* data, size_t len)
260 if(strcmp(cstr, "IN") != 0)
261 printf(" in class %s", cstr);
262 if(t == LDNS_RR_TYPE_A)
263 printf(" has address");
264 else if(t == LDNS_RR_TYPE_AAAA)
265 printf(" has IPv6 address");
266 else if(t == LDNS_RR_TYPE_MX)
267 printf(" mail is handled by");
268 else if(t == LDNS_RR_TYPE_PTR)
269 printf(" domain name pointer");
270 else printf(" has %s record", tstr);
271 print_rd(t, data, len);
277 /** pretty line of output for results */
279 pretty_output(char* q, int t, int c, struct ub_result* result, int docname)
282 const char *secstatus = secure_str(result);
286 pretty_type(tstr, 16, t);
287 pretty_class(cstr, 16, c);
288 pretty_rcode(rcodestr, 16, result->rcode);
290 if(!result->havedata && result->rcode) {
291 printf("Host %s not found: %d(%s).",
292 q, result->rcode, rcodestr);
294 printf(" %s", secstatus);
296 if(result->bogus && result->why_bogus)
297 printf("%s\n", result->why_bogus);
300 if(docname && result->canonname &&
301 result->canonname != result->qname) {
302 printf("%s is an alias for %s", result->qname,
305 printf(" %s", secstatus);
308 /* remove trailing . from long canonnames for nicer output */
309 if(result->canonname && strlen(result->canonname) > 1 &&
310 result->canonname[strlen(result->canonname)-1] == '.')
311 result->canonname[strlen(result->canonname)-1] = 0;
312 if(!result->havedata) {
314 printf("%s", result->canonname?result->canonname:q);
315 if(strcmp(cstr, "IN") != 0)
316 printf(" in class %s", cstr);
317 if(t == LDNS_RR_TYPE_A)
318 printf(" has no address");
319 else if(t == LDNS_RR_TYPE_AAAA)
320 printf(" has no IPv6 address");
321 else if(t == LDNS_RR_TYPE_PTR)
322 printf(" has no domain name ptr");
323 else if(t == LDNS_RR_TYPE_MX)
324 printf(" has no mail handler record");
325 else if(t == LDNS_RR_TYPE_ANY) {
326 char* s = sldns_wire2str_pkt(
327 result->answer_packet,
328 (size_t)result->answer_len);
330 fprintf(stderr, "alloc failure\n");
334 } else printf(" has no %s record", tstr);
335 printf(" %s\n", secstatus);
337 /* else: emptiness to indicate no data */
338 if(result->bogus && result->why_bogus)
339 printf("%s\n", result->why_bogus);
343 while(result->data[i])
346 result->canonname?result->canonname:q,
347 cstr, tstr, t, secstatus, result->data[i],
348 (size_t)result->len[i]);
351 if(result->bogus && result->why_bogus)
352 printf("%s\n", result->why_bogus);
355 /** perform a lookup and printout return if domain existed */
357 dnslook(struct ub_ctx* ctx, char* q, int t, int c, int docname)
360 struct ub_result* result;
362 ret = ub_resolve(ctx, q, t, c, &result);
364 fprintf(stderr, "resolve error: %s\n", ub_strerror(ret));
367 pretty_output(q, t, c, result, docname);
368 ret = result->nxdomain;
369 ub_resolve_free(result);
373 /** perform host lookup */
375 lookup(struct ub_ctx* ctx, const char* nm, const char* qt, const char* qc)
377 /* massage input into a query name, type and class */
378 int multi = 0; /* no type, so do A, AAAA, MX */
379 int reverse = 0; /* we are doing a reverse lookup */
380 char* realq = massage_qname(nm, &reverse);
381 int t = massage_type(qt, reverse, &multi);
382 int c = massage_class(qc);
384 /* perform the query */
386 if(!dnslook(ctx, realq, LDNS_RR_TYPE_A, c, 1)) {
387 /* domain exists, lookup more */
388 (void)dnslook(ctx, realq, LDNS_RR_TYPE_AAAA, c, 0);
389 (void)dnslook(ctx, realq, LDNS_RR_TYPE_MX, c, 0);
392 (void)dnslook(ctx, realq, t, c, 1);
398 /** print error if any */
403 fprintf(stderr, "error: %s\n", ub_strerror(r));
408 /** getopt global, in case header files fail to declare it. */
410 /** getopt global, in case header files fail to declare it. */
413 /** Main routine for unbound-host */
414 int main(int argc, char* argv[])
419 struct ub_ctx* ctx = NULL;
422 ctx = ub_ctx_create();
424 fprintf(stderr, "error: out of memory\n");
427 /* no need to fetch additional targets, we only do few lookups */
428 check_ub_res(ub_ctx_set_option(ctx, "target-fetch-policy:", "0 0 0 0 0"));
430 /* parse the options */
431 while( (c=getopt(argc, argv, "46DF:c:df:hrt:vy:C:")) != -1) {
434 check_ub_res(ub_ctx_set_option(ctx, "do-ip6:", "no"));
437 check_ub_res(ub_ctx_set_option(ctx, "do-ip4:", "no"));
443 check_ub_res(ub_ctx_config(ctx, optarg));
446 check_ub_res(ub_ctx_add_ta_file(ctx, ROOT_ANCHOR_FILE));
451 debuglevel = 2; /* at least VERB_DETAIL */
454 check_ub_res(ub_ctx_resolvconf(ctx, "/etc/resolv.conf"));
463 check_ub_res(ub_ctx_add_ta(ctx, optarg));
466 check_ub_res(ub_ctx_add_ta_file(ctx, optarg));
469 check_ub_res(ub_ctx_trustedkeys(ctx, optarg));
477 if(debuglevel != 0) /* set after possible -C options */
478 check_ub_res(ub_ctx_debuglevel(ctx, debuglevel));
479 if(ub_ctx_get_option(ctx, "use-syslog", &optarg) == 0) {
480 if(strcmp(optarg, "yes") == 0) /* disable use-syslog */
481 check_ub_res(ub_ctx_set_option(ctx,
482 "use-syslog:", "no"));
491 if(NSS_NoDB_Init(".") != SECSuccess) {
492 fprintf(stderr, "could not init NSS\n");
496 lookup(ctx, argv[0], qtype, qclass);