xref: /freebsd/contrib/unbound/iterator/iter_hints.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1 /*
2  * iterator/iter_hints.c - iterative resolver module stub and root hints.
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions to assist the iterator module.
40  * Keep track of stub and root hints, and read those from config.
41  */
42 #include "config.h"
43 #include "iterator/iter_hints.h"
44 #include "iterator/iter_delegpt.h"
45 #include "util/log.h"
46 #include "util/config_file.h"
47 #include "util/net_help.h"
48 #include "util/data/dname.h"
49 #include "ldns/rrdef.h"
50 #include "ldns/str2wire.h"
51 #include "ldns/wire2str.h"
52 
53 struct iter_hints*
54 hints_create(void)
55 {
56 	struct iter_hints* hints = (struct iter_hints*)calloc(1,
57 		sizeof(struct iter_hints));
58 	if(!hints)
59 		return NULL;
60 	return hints;
61 }
62 
63 static void hints_stub_free(struct iter_hints_stub* s)
64 {
65 	if(!s) return;
66 	delegpt_free_mlc(s->dp);
67 	free(s);
68 }
69 
70 static void delhintnode(rbnode_t* n, void* ATTR_UNUSED(arg))
71 {
72 	struct iter_hints_stub* node = (struct iter_hints_stub*)n;
73 	hints_stub_free(node);
74 }
75 
76 static void hints_del_tree(struct iter_hints* hints)
77 {
78 	traverse_postorder(&hints->tree, &delhintnode, NULL);
79 }
80 
81 void
82 hints_delete(struct iter_hints* hints)
83 {
84 	if(!hints)
85 		return;
86 	hints_del_tree(hints);
87 	free(hints);
88 }
89 
90 /** add hint to delegation hints */
91 static int
92 ah(struct delegpt* dp, const char* sv, const char* ip)
93 {
94 	struct sockaddr_storage addr;
95 	socklen_t addrlen;
96 	size_t dname_len;
97 	uint8_t* dname = sldns_str2wire_dname(sv, &dname_len);
98 	if(!dname) {
99 		log_err("could not parse %s", sv);
100 		return 0;
101 	}
102 	if(!delegpt_add_ns_mlc(dp, dname, 0) ||
103 	   !extstrtoaddr(ip, &addr, &addrlen) ||
104 	   !delegpt_add_target_mlc(dp, dname, dname_len,
105 		&addr, addrlen, 0, 0)) {
106 		free(dname);
107 		return 0;
108 	}
109 	free(dname);
110 	return 1;
111 }
112 
113 /** obtain compiletime provided root hints */
114 static struct delegpt*
115 compile_time_root_prime(int do_ip4, int do_ip6)
116 {
117 	/* from:
118 	 ;       This file is made available by InterNIC
119 	 ;       under anonymous FTP as
120 	 ;           file                /domain/named.cache
121 	 ;           on server           FTP.INTERNIC.NET
122 	 ;       -OR-                    RS.INTERNIC.NET
123 	 ;
124 	 ;       related version of root zone:   changes-on-20120103
125 	 */
126 	struct delegpt* dp = delegpt_create_mlc((uint8_t*)"\000");
127 	if(!dp)
128 		return NULL;
129 	dp->has_parent_side_NS = 1;
130       if(do_ip4) {
131 	if(!ah(dp, "A.ROOT-SERVERS.NET.", "198.41.0.4"))	goto failed;
132 	if(!ah(dp, "B.ROOT-SERVERS.NET.", "192.228.79.201")) goto failed;
133 	if(!ah(dp, "C.ROOT-SERVERS.NET.", "192.33.4.12"))	goto failed;
134 	if(!ah(dp, "D.ROOT-SERVERS.NET.", "199.7.91.13"))	goto failed;
135 	if(!ah(dp, "E.ROOT-SERVERS.NET.", "192.203.230.10")) goto failed;
136 	if(!ah(dp, "F.ROOT-SERVERS.NET.", "192.5.5.241"))	goto failed;
137 	if(!ah(dp, "G.ROOT-SERVERS.NET.", "192.112.36.4"))	goto failed;
138 	if(!ah(dp, "H.ROOT-SERVERS.NET.", "128.63.2.53"))	goto failed;
139 	if(!ah(dp, "I.ROOT-SERVERS.NET.", "192.36.148.17"))	goto failed;
140 	if(!ah(dp, "J.ROOT-SERVERS.NET.", "192.58.128.30"))	goto failed;
141 	if(!ah(dp, "K.ROOT-SERVERS.NET.", "193.0.14.129"))	goto failed;
142 	if(!ah(dp, "L.ROOT-SERVERS.NET.", "199.7.83.42"))	goto failed;
143 	if(!ah(dp, "M.ROOT-SERVERS.NET.", "202.12.27.33"))	goto failed;
144       }
145       if(do_ip6) {
146 	if(!ah(dp, "A.ROOT-SERVERS.NET.", "2001:503:ba3e::2:30")) goto failed;
147 	if(!ah(dp, "D.ROOT-SERVERS.NET.", "2001:500:2d::d")) goto failed;
148 	if(!ah(dp, "F.ROOT-SERVERS.NET.", "2001:500:2f::f")) goto failed;
149 	if(!ah(dp, "H.ROOT-SERVERS.NET.", "2001:500:1::803f:235")) goto failed;
150 	if(!ah(dp, "I.ROOT-SERVERS.NET.", "2001:7fe::53")) goto failed;
151 	if(!ah(dp, "J.ROOT-SERVERS.NET.", "2001:503:c27::2:30")) goto failed;
152 	if(!ah(dp, "K.ROOT-SERVERS.NET.", "2001:7fd::1")) goto failed;
153 	if(!ah(dp, "L.ROOT-SERVERS.NET.", "2001:500:3::42")) goto failed;
154 	if(!ah(dp, "M.ROOT-SERVERS.NET.", "2001:dc3::35")) goto failed;
155       }
156 	return dp;
157 failed:
158 	delegpt_free_mlc(dp);
159 	return 0;
160 }
161 
162 /** insert new hint info into hint structure */
163 static int
164 hints_insert(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
165 	int noprime)
166 {
167 	struct iter_hints_stub* node = (struct iter_hints_stub*)malloc(
168 		sizeof(struct iter_hints_stub));
169 	if(!node) {
170 		delegpt_free_mlc(dp);
171 		return 0;
172 	}
173 	node->dp = dp;
174 	node->noprime = (uint8_t)noprime;
175 	if(!name_tree_insert(&hints->tree, &node->node, dp->name, dp->namelen,
176 		dp->namelabs, c)) {
177 		char buf[257];
178 		dname_str(dp->name, buf);
179 		log_err("second hints for zone %s ignored.", buf);
180 		delegpt_free_mlc(dp);
181 		free(node);
182 	}
183 	return 1;
184 }
185 
186 /** set stub name */
187 static struct delegpt*
188 read_stubs_name(struct config_stub* s)
189 {
190 	struct delegpt* dp;
191 	size_t dname_len;
192 	uint8_t* dname;
193 	if(!s->name) {
194 		log_err("stub zone without a name");
195 		return NULL;
196 	}
197 	dname = sldns_str2wire_dname(s->name, &dname_len);
198 	if(!dname) {
199 		log_err("cannot parse stub zone name %s", s->name);
200 		return NULL;
201 	}
202 	if(!(dp=delegpt_create_mlc(dname))) {
203 		free(dname);
204 		log_err("out of memory");
205 		return NULL;
206 	}
207 	free(dname);
208 	return dp;
209 }
210 
211 /** set stub host names */
212 static int
213 read_stubs_host(struct config_stub* s, struct delegpt* dp)
214 {
215 	struct config_strlist* p;
216 	size_t dname_len;
217 	uint8_t* dname;
218 	for(p = s->hosts; p; p = p->next) {
219 		log_assert(p->str);
220 		dname = sldns_str2wire_dname(p->str, &dname_len);
221 		if(!dname) {
222 			log_err("cannot parse stub %s nameserver name: '%s'",
223 				s->name, p->str);
224 			return 0;
225 		}
226 		if(!delegpt_add_ns_mlc(dp, dname, 0)) {
227 			free(dname);
228 			log_err("out of memory");
229 			return 0;
230 		}
231 		free(dname);
232 	}
233 	return 1;
234 }
235 
236 /** set stub server addresses */
237 static int
238 read_stubs_addr(struct config_stub* s, struct delegpt* dp)
239 {
240 	struct config_strlist* p;
241 	struct sockaddr_storage addr;
242 	socklen_t addrlen;
243 	for(p = s->addrs; p; p = p->next) {
244 		log_assert(p->str);
245 		if(!extstrtoaddr(p->str, &addr, &addrlen)) {
246 			log_err("cannot parse stub %s ip address: '%s'",
247 				s->name, p->str);
248 			return 0;
249 		}
250 		if(!delegpt_add_addr_mlc(dp, &addr, addrlen, 0, 0)) {
251 			log_err("out of memory");
252 			return 0;
253 		}
254 	}
255 	return 1;
256 }
257 
258 /** read stubs config */
259 static int
260 read_stubs(struct iter_hints* hints, struct config_file* cfg)
261 {
262 	struct config_stub* s;
263 	struct delegpt* dp;
264 	for(s = cfg->stubs; s; s = s->next) {
265 		if(!(dp=read_stubs_name(s)))
266 			return 0;
267 		if(!read_stubs_host(s, dp) || !read_stubs_addr(s, dp)) {
268 			delegpt_free_mlc(dp);
269 			return 0;
270 		}
271 		/* the flag is turned off for 'stub-first' so that the
272 		 * last resort will ask for parent-side NS record and thus
273 		 * fallback to the internet name servers on a failure */
274 		dp->has_parent_side_NS = (uint8_t)!s->isfirst;
275 		delegpt_log(VERB_QUERY, dp);
276 		if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, !s->isprime))
277 			return 0;
278 	}
279 	return 1;
280 }
281 
282 /** read root hints from file */
283 static int
284 read_root_hints(struct iter_hints* hints, char* fname)
285 {
286 	struct sldns_file_parse_state pstate;
287 	struct delegpt* dp;
288 	uint8_t rr[LDNS_RR_BUF_SIZE];
289 	size_t rr_len, dname_len;
290 	int status;
291 	uint16_t c = LDNS_RR_CLASS_IN;
292 	FILE* f = fopen(fname, "r");
293 	if(!f) {
294 		log_err("could not read root hints %s: %s",
295 			fname, strerror(errno));
296 		return 0;
297 	}
298 	dp = delegpt_create_mlc(NULL);
299 	if(!dp) {
300 		log_err("out of memory reading root hints");
301 		fclose(f);
302 		return 0;
303 	}
304 	verbose(VERB_QUERY, "Reading root hints from %s", fname);
305 	memset(&pstate, 0, sizeof(pstate));
306 	pstate.lineno = 1;
307 	dp->has_parent_side_NS = 1;
308 	while(!feof(f)) {
309 		rr_len = sizeof(rr);
310 		dname_len = 0;
311 		status = sldns_fp2wire_rr_buf(f, rr, &rr_len, &dname_len,
312 			&pstate);
313 		if(status != 0) {
314 			log_err("reading root hints %s %d:%d: %s", fname,
315 				pstate.lineno, LDNS_WIREPARSE_OFFSET(status),
316 				sldns_get_errorstr_parse(status));
317 			goto stop_read;
318 		}
319 		if(rr_len == 0)
320 			continue; /* EMPTY line, TTL or ORIGIN */
321 		if(sldns_wirerr_get_type(rr, rr_len, dname_len)
322 			== LDNS_RR_TYPE_NS) {
323 			if(!delegpt_add_ns_mlc(dp, sldns_wirerr_get_rdata(rr,
324 				rr_len, dname_len), 0)) {
325 				log_err("out of memory reading root hints");
326 				goto stop_read;
327 			}
328 			c = sldns_wirerr_get_class(rr, rr_len, dname_len);
329 			if(!dp->name) {
330 				if(!delegpt_set_name_mlc(dp, rr)) {
331 					log_err("out of memory.");
332 					goto stop_read;
333 				}
334 			}
335 		} else if(sldns_wirerr_get_type(rr, rr_len, dname_len)
336 			== LDNS_RR_TYPE_A && sldns_wirerr_get_rdatalen(rr,
337 			rr_len, dname_len) == INET_SIZE) {
338 			struct sockaddr_in sa;
339 			socklen_t len = (socklen_t)sizeof(sa);
340 			memset(&sa, 0, len);
341 			sa.sin_family = AF_INET;
342 			sa.sin_port = (in_port_t)htons(UNBOUND_DNS_PORT);
343 			memmove(&sa.sin_addr,
344 				sldns_wirerr_get_rdata(rr, rr_len, dname_len),
345 				INET_SIZE);
346 			if(!delegpt_add_target_mlc(dp, rr, dname_len,
347 					(struct sockaddr_storage*)&sa, len,
348 					0, 0)) {
349 				log_err("out of memory reading root hints");
350 				goto stop_read;
351 			}
352 		} else if(sldns_wirerr_get_type(rr, rr_len, dname_len)
353 			== LDNS_RR_TYPE_AAAA && sldns_wirerr_get_rdatalen(rr,
354 			rr_len, dname_len) == INET6_SIZE) {
355 			struct sockaddr_in6 sa;
356 			socklen_t len = (socklen_t)sizeof(sa);
357 			memset(&sa, 0, len);
358 			sa.sin6_family = AF_INET6;
359 			sa.sin6_port = (in_port_t)htons(UNBOUND_DNS_PORT);
360 			memmove(&sa.sin6_addr,
361 				sldns_wirerr_get_rdata(rr, rr_len, dname_len),
362 				INET6_SIZE);
363 			if(!delegpt_add_target_mlc(dp, rr, dname_len,
364 					(struct sockaddr_storage*)&sa, len,
365 					0, 0)) {
366 				log_err("out of memory reading root hints");
367 				goto stop_read;
368 			}
369 		} else {
370 			char buf[17];
371 			sldns_wire2str_type_buf(sldns_wirerr_get_type(rr,
372 				rr_len, dname_len), buf, sizeof(buf));
373 			log_warn("root hints %s:%d skipping type %s",
374 				fname, pstate.lineno, buf);
375 		}
376 	}
377 	fclose(f);
378 	if(!dp->name) {
379 		log_warn("root hints %s: no NS content", fname);
380 		delegpt_free_mlc(dp);
381 		return 1;
382 	}
383 	if(!hints_insert(hints, c, dp, 0)) {
384 		return 0;
385 	}
386 	delegpt_log(VERB_QUERY, dp);
387 	return 1;
388 
389 stop_read:
390 	delegpt_free_mlc(dp);
391 	fclose(f);
392 	return 0;
393 }
394 
395 /** read root hints list */
396 static int
397 read_root_hints_list(struct iter_hints* hints, struct config_file* cfg)
398 {
399 	struct config_strlist* p;
400 	for(p = cfg->root_hints; p; p = p->next) {
401 		log_assert(p->str);
402 		if(p->str && p->str[0]) {
403 			char* f = p->str;
404 			if(cfg->chrootdir && cfg->chrootdir[0] &&
405 				strncmp(p->str, cfg->chrootdir,
406 				strlen(cfg->chrootdir)) == 0)
407 				f += strlen(cfg->chrootdir);
408 			if(!read_root_hints(hints, f))
409 				return 0;
410 		}
411 	}
412 	return 1;
413 }
414 
415 int
416 hints_apply_cfg(struct iter_hints* hints, struct config_file* cfg)
417 {
418 	hints_del_tree(hints);
419 	name_tree_init(&hints->tree);
420 
421 	/* read root hints */
422 	if(!read_root_hints_list(hints, cfg))
423 		return 0;
424 
425 	/* read stub hints */
426 	if(!read_stubs(hints, cfg))
427 		return 0;
428 
429 	/* use fallback compiletime root hints */
430 	if(!hints_lookup_root(hints, LDNS_RR_CLASS_IN)) {
431 		struct delegpt* dp = compile_time_root_prime(cfg->do_ip4,
432 			cfg->do_ip6);
433 		verbose(VERB_ALGO, "no config, using builtin root hints.");
434 		if(!dp)
435 			return 0;
436 		if(!hints_insert(hints, LDNS_RR_CLASS_IN, dp, 0))
437 			return 0;
438 	}
439 
440 	name_tree_init_parents(&hints->tree);
441 	return 1;
442 }
443 
444 struct delegpt*
445 hints_lookup_root(struct iter_hints* hints, uint16_t qclass)
446 {
447 	uint8_t rootlab = 0;
448 	struct iter_hints_stub *stub;
449 	stub = (struct iter_hints_stub*)name_tree_find(&hints->tree,
450 		&rootlab, 1, 1, qclass);
451 	if(!stub)
452 		return NULL;
453 	return stub->dp;
454 }
455 
456 struct iter_hints_stub*
457 hints_lookup_stub(struct iter_hints* hints, uint8_t* qname,
458 	uint16_t qclass, struct delegpt* cache_dp)
459 {
460 	size_t len;
461 	int labs;
462 	struct iter_hints_stub *r;
463 
464 	/* first lookup the stub */
465 	labs = dname_count_size_labels(qname, &len);
466 	r = (struct iter_hints_stub*)name_tree_lookup(&hints->tree, qname,
467 		len, labs, qclass);
468 	if(!r) return NULL;
469 
470 	/* If there is no cache (root prime situation) */
471 	if(cache_dp == NULL) {
472 		if(r->dp->namelabs != 1)
473 			return r; /* no cache dp, use any non-root stub */
474 		return NULL;
475 	}
476 
477 	/*
478 	 * If the stub is same as the delegation we got
479 	 * And has noprime set, we need to 'prime' to use this stub instead.
480 	 */
481 	if(r->noprime && query_dname_compare(cache_dp->name, r->dp->name)==0)
482 		return r; /* use this stub instead of cached dp */
483 
484 	/*
485 	 * If our cached delegation point is above the hint, we need to prime.
486 	 */
487 	if(dname_strict_subdomain(r->dp->name, r->dp->namelabs,
488 		cache_dp->name, cache_dp->namelabs))
489 		return r; /* need to prime this stub */
490 	return NULL;
491 }
492 
493 int hints_next_root(struct iter_hints* hints, uint16_t* qclass)
494 {
495 	return name_tree_next_root(&hints->tree, qclass);
496 }
497 
498 size_t
499 hints_get_mem(struct iter_hints* hints)
500 {
501 	size_t s;
502 	struct iter_hints_stub* p;
503 	if(!hints) return 0;
504 	s = sizeof(*hints);
505 	RBTREE_FOR(p, struct iter_hints_stub*, &hints->tree) {
506 		s += sizeof(*p) + delegpt_get_mem(p->dp);
507 	}
508 	return s;
509 }
510 
511 int
512 hints_add_stub(struct iter_hints* hints, uint16_t c, struct delegpt* dp,
513 	int noprime)
514 {
515 	struct iter_hints_stub *z;
516 	if((z=(struct iter_hints_stub*)name_tree_find(&hints->tree,
517 		dp->name, dp->namelen, dp->namelabs, c)) != NULL) {
518 		(void)rbtree_delete(&hints->tree, &z->node);
519 		hints_stub_free(z);
520 	}
521 	if(!hints_insert(hints, c, dp, noprime))
522 		return 0;
523 	name_tree_init_parents(&hints->tree);
524 	return 1;
525 }
526 
527 void
528 hints_delete_stub(struct iter_hints* hints, uint16_t c, uint8_t* nm)
529 {
530 	struct iter_hints_stub *z;
531 	size_t len;
532 	int labs = dname_count_size_labels(nm, &len);
533 	if(!(z=(struct iter_hints_stub*)name_tree_find(&hints->tree,
534 		nm, len, labs, c)))
535 		return; /* nothing to do */
536 	(void)rbtree_delete(&hints->tree, &z->node);
537 	hints_stub_free(z);
538 	name_tree_init_parents(&hints->tree);
539 }
540 
541