xref: /freebsd/contrib/unbound/iterator/iter_priv.c (revision 5ae59dec60e3815b621ae87f74a377cf3449ca55)
1 /*
2  * iterator/iter_priv.c - iterative resolver private address and domain store
3  *
4  * Copyright (c) 2008, 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 LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions to assist the iterator module.
40  * Keep track of the private addresses and lookup fast.
41  */
42 
43 #include "config.h"
44 #include <ldns/dname.h>
45 #include "iterator/iter_priv.h"
46 #include "util/regional.h"
47 #include "util/log.h"
48 #include "util/config_file.h"
49 #include "util/data/dname.h"
50 #include "util/data/msgparse.h"
51 #include "util/net_help.h"
52 #include "util/storage/dnstree.h"
53 
54 struct iter_priv* priv_create(void)
55 {
56 	struct iter_priv* priv = (struct iter_priv*)calloc(1, sizeof(*priv));
57 	if(!priv)
58 		return NULL;
59 	priv->region = regional_create();
60 	if(!priv->region) {
61 		priv_delete(priv);
62 		return NULL;
63 	}
64 	addr_tree_init(&priv->a);
65 	name_tree_init(&priv->n);
66 	return priv;
67 }
68 
69 void priv_delete(struct iter_priv* priv)
70 {
71 	if(!priv) return;
72 	regional_destroy(priv->region);
73 	free(priv);
74 }
75 
76 /** Read private-addr declarations from config */
77 static int read_addrs(struct iter_priv* priv, struct config_file* cfg)
78 {
79 	/* parse addresses, report errors, insert into tree */
80 	struct config_strlist* p;
81 	struct addr_tree_node* n;
82 	struct sockaddr_storage addr;
83 	int net;
84 	socklen_t addrlen;
85 
86 	for(p = cfg->private_address; p; p = p->next) {
87 		log_assert(p->str);
88 		if(!netblockstrtoaddr(p->str, UNBOUND_DNS_PORT, &addr,
89 			&addrlen, &net)) {
90 			log_err("cannot parse private-address: %s", p->str);
91 			return 0;
92 		}
93 		n = (struct addr_tree_node*)regional_alloc(priv->region,
94 			sizeof(*n));
95 		if(!n) {
96 			log_err("out of memory");
97 			return 0;
98 		}
99 		if(!addr_tree_insert(&priv->a, n, &addr, addrlen, net)) {
100 			verbose(VERB_QUERY, "ignoring duplicate "
101 				"private-address: %s", p->str);
102 		}
103 	}
104 	return 1;
105 }
106 
107 /** Read private-domain declarations from config */
108 static int read_names(struct iter_priv* priv, struct config_file* cfg)
109 {
110 	/* parse names, report errors, insert into tree */
111 	struct config_strlist* p;
112 	struct name_tree_node* n;
113 	uint8_t* nm;
114 	size_t nm_len;
115 	int nm_labs;
116 	ldns_rdf* rdf;
117 
118 	for(p = cfg->private_domain; p; p = p->next) {
119 		log_assert(p->str);
120 		rdf = ldns_dname_new_frm_str(p->str);
121 		if(!rdf) {
122 			log_err("cannot parse private-domain: %s", p->str);
123 			return 0;
124 		}
125 		nm = ldns_rdf_data(rdf);
126 		nm_labs = dname_count_size_labels(nm, &nm_len);
127 		nm = (uint8_t*)regional_alloc_init(priv->region, nm, nm_len);
128 		ldns_rdf_deep_free(rdf);
129 		if(!nm) {
130 			log_err("out of memory");
131 			return 0;
132 		}
133 		n = (struct name_tree_node*)regional_alloc(priv->region,
134 			sizeof(*n));
135 		if(!n) {
136 			log_err("out of memory");
137 			return 0;
138 		}
139 		if(!name_tree_insert(&priv->n, n, nm, nm_len, nm_labs,
140 			LDNS_RR_CLASS_IN)) {
141 			verbose(VERB_QUERY, "ignoring duplicate "
142 				"private-domain: %s", p->str);
143 		}
144 	}
145 	return 1;
146 }
147 
148 int priv_apply_cfg(struct iter_priv* priv, struct config_file* cfg)
149 {
150 	/* empty the current contents */
151 	regional_free_all(priv->region);
152 	addr_tree_init(&priv->a);
153 	name_tree_init(&priv->n);
154 
155 	/* read new contents */
156 	if(!read_addrs(priv, cfg))
157 		return 0;
158 	if(!read_names(priv, cfg))
159 		return 0;
160 
161 	/* prepare for lookups */
162 	addr_tree_init_parents(&priv->a);
163 	name_tree_init_parents(&priv->n);
164 	return 1;
165 }
166 
167 /**
168  * See if an address is blocked.
169  * @param priv: structure for address storage.
170  * @param addr: address to check
171  * @param addrlen: length of addr.
172  * @return: true if the address must not be queried. false if unlisted.
173  */
174 static int
175 priv_lookup_addr(struct iter_priv* priv, struct sockaddr_storage* addr,
176 	socklen_t addrlen)
177 {
178 	return addr_tree_lookup(&priv->a, addr, addrlen) != NULL;
179 }
180 
181 /**
182  * See if a name is whitelisted.
183  * @param priv: structure for address storage.
184  * @param pkt: the packet (for compression ptrs).
185  * @param name: name to check.
186  * @param name_len: uncompressed length of the name to check.
187  * @param dclass: class to check.
188  * @return: true if the name is OK. false if unlisted.
189  */
190 static int
191 priv_lookup_name(struct iter_priv* priv, ldns_buffer* pkt,
192 	uint8_t* name, size_t name_len, uint16_t dclass)
193 {
194 	size_t len;
195 	uint8_t decomp[256];
196 	int labs;
197 	if(name_len >= sizeof(decomp))
198 		return 0;
199 	dname_pkt_copy(pkt, decomp, name);
200 	labs = dname_count_size_labels(decomp, &len);
201 	log_assert(name_len == len);
202 	return name_tree_lookup(&priv->n, decomp, len, labs, dclass) != NULL;
203 }
204 
205 size_t priv_get_mem(struct iter_priv* priv)
206 {
207 	if(!priv) return 0;
208 	return sizeof(*priv) + regional_get_mem(priv->region);
209 }
210 
211 int priv_rrset_bad(struct iter_priv* priv, ldns_buffer* pkt,
212 	struct rrset_parse* rrset)
213 {
214 	if(priv->a.count == 0)
215 		return 0; /* there are no blocked addresses */
216 
217 	/* see if it is a private name, that is allowed to have any */
218 	if(priv_lookup_name(priv, pkt, rrset->dname, rrset->dname_len,
219 		ntohs(rrset->rrset_class))) {
220 		return 0;
221 	} else {
222 		/* so its a public name, check the address */
223 		socklen_t len;
224 		struct rr_parse* rr;
225 		if(rrset->type == LDNS_RR_TYPE_A) {
226 			struct sockaddr_storage addr;
227 			struct sockaddr_in sa;
228 
229 			len = (socklen_t)sizeof(sa);
230 			memset(&sa, 0, len);
231 			sa.sin_family = AF_INET;
232 			sa.sin_port = (in_port_t)htons(UNBOUND_DNS_PORT);
233 			for(rr = rrset->rr_first; rr; rr = rr->next) {
234 				if(ldns_read_uint16(rr->ttl_data+4)
235 					!= INET_SIZE)
236 					continue;
237 				memmove(&sa.sin_addr, rr->ttl_data+4+2,
238 					INET_SIZE);
239 				memmove(&addr, &sa, len);
240 				if(priv_lookup_addr(priv, &addr, len))
241 					return 1;
242 			}
243 		} else if(rrset->type == LDNS_RR_TYPE_AAAA) {
244 			struct sockaddr_storage addr;
245 			struct sockaddr_in6 sa;
246 			len = (socklen_t)sizeof(sa);
247 			memset(&sa, 0, len);
248 			sa.sin6_family = AF_INET6;
249 			sa.sin6_port = (in_port_t)htons(UNBOUND_DNS_PORT);
250 			for(rr = rrset->rr_first; rr; rr = rr->next) {
251 				if(ldns_read_uint16(rr->ttl_data+4)
252 					!= INET6_SIZE)
253 					continue;
254 				memmove(&sa.sin6_addr, rr->ttl_data+4+2,
255 					INET6_SIZE);
256 				memmove(&addr, &sa, len);
257 				if(priv_lookup_addr(priv, &addr, len))
258 					return 1;
259 			}
260 		}
261 	}
262 	return 0;
263 }
264