xref: /freebsd/contrib/unbound/iterator/iter_priv.c (revision 1db64f89363c97858961c4df0b7d02f3223723cf)
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
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 the private addresses and lookup fast.
41  */
42 
43 #include "config.h"
44 #include "iterator/iter_priv.h"
45 #include "util/regional.h"
46 #include "util/log.h"
47 #include "util/config_file.h"
48 #include "util/data/dname.h"
49 #include "util/data/msgparse.h"
50 #include "util/net_help.h"
51 #include "util/storage/dnstree.h"
52 #include "sldns/str2wire.h"
53 #include "sldns/sbuffer.h"
54 
55 struct iter_priv* priv_create(void)
56 {
57 	struct iter_priv* priv = (struct iter_priv*)calloc(1, sizeof(*priv));
58 	if(!priv)
59 		return NULL;
60 	priv->region = regional_create();
61 	if(!priv->region) {
62 		priv_delete(priv);
63 		return NULL;
64 	}
65 	addr_tree_init(&priv->a);
66 	name_tree_init(&priv->n);
67 	return priv;
68 }
69 
70 void priv_delete(struct iter_priv* priv)
71 {
72 	if(!priv) return;
73 	regional_destroy(priv->region);
74 	free(priv);
75 }
76 
77 /** Read private-addr declarations from config */
78 static int read_addrs(struct iter_priv* priv, struct config_file* cfg)
79 {
80 	/* parse addresses, report errors, insert into tree */
81 	struct config_strlist* p;
82 	struct addr_tree_node* n;
83 	struct sockaddr_storage addr;
84 	int net;
85 	socklen_t addrlen;
86 
87 	for(p = cfg->private_address; p; p = p->next) {
88 		log_assert(p->str);
89 		if(!netblockstrtoaddr(p->str, UNBOUND_DNS_PORT, &addr,
90 			&addrlen, &net)) {
91 			log_err("cannot parse private-address: %s", p->str);
92 			return 0;
93 		}
94 		n = (struct addr_tree_node*)regional_alloc(priv->region,
95 			sizeof(*n));
96 		if(!n) {
97 			log_err("out of memory");
98 			return 0;
99 		}
100 		if(!addr_tree_insert(&priv->a, n, &addr, addrlen, net)) {
101 			verbose(VERB_QUERY, "ignoring duplicate "
102 				"private-address: %s", p->str);
103 		}
104 	}
105 	return 1;
106 }
107 
108 /** Read private-domain declarations from config */
109 static int read_names(struct iter_priv* priv, struct config_file* cfg)
110 {
111 	/* parse names, report errors, insert into tree */
112 	struct config_strlist* p;
113 	struct name_tree_node* n;
114 	uint8_t* nm, *nmr;
115 	size_t nm_len;
116 	int nm_labs;
117 
118 	for(p = cfg->private_domain; p; p = p->next) {
119 		log_assert(p->str);
120 		nm = sldns_str2wire_dname(p->str, &nm_len);
121 		if(!nm) {
122 			log_err("cannot parse private-domain: %s", p->str);
123 			return 0;
124 		}
125 		nm_labs = dname_count_size_labels(nm, &nm_len);
126 		nmr = (uint8_t*)regional_alloc_init(priv->region, nm, nm_len);
127 		free(nm);
128 		if(!nmr) {
129 			log_err("out of memory");
130 			return 0;
131 		}
132 		n = (struct name_tree_node*)regional_alloc(priv->region,
133 			sizeof(*n));
134 		if(!n) {
135 			log_err("out of memory");
136 			return 0;
137 		}
138 		if(!name_tree_insert(&priv->n, n, nmr, nm_len, nm_labs,
139 			LDNS_RR_CLASS_IN)) {
140 			verbose(VERB_QUERY, "ignoring duplicate "
141 				"private-domain: %s", p->str);
142 		}
143 	}
144 	return 1;
145 }
146 
147 int priv_apply_cfg(struct iter_priv* priv, struct config_file* cfg)
148 {
149 	/* empty the current contents */
150 	regional_free_all(priv->region);
151 	addr_tree_init(&priv->a);
152 	name_tree_init(&priv->n);
153 
154 	/* read new contents */
155 	if(!read_addrs(priv, cfg))
156 		return 0;
157 	if(!read_names(priv, cfg))
158 		return 0;
159 
160 	/* prepare for lookups */
161 	addr_tree_init_parents(&priv->a);
162 	name_tree_init_parents(&priv->n);
163 	return 1;
164 }
165 
166 /**
167  * See if an address is blocked.
168  * @param priv: structure for address storage.
169  * @param addr: address to check
170  * @param addrlen: length of addr.
171  * @return: true if the address must not be queried. false if unlisted.
172  */
173 static int
174 priv_lookup_addr(struct iter_priv* priv, struct sockaddr_storage* addr,
175 	socklen_t addrlen)
176 {
177 	return addr_tree_lookup(&priv->a, addr, addrlen) != NULL;
178 }
179 
180 /**
181  * See if a name is whitelisted.
182  * @param priv: structure for address storage.
183  * @param pkt: the packet (for compression ptrs).
184  * @param name: name to check.
185  * @param name_len: uncompressed length of the name to check.
186  * @param dclass: class to check.
187  * @return: true if the name is OK. false if unlisted.
188  */
189 static int
190 priv_lookup_name(struct iter_priv* priv, sldns_buffer* pkt,
191 	uint8_t* name, size_t name_len, uint16_t dclass)
192 {
193 	size_t len;
194 	uint8_t decomp[256];
195 	int labs;
196 	if(name_len >= sizeof(decomp))
197 		return 0;
198 	dname_pkt_copy(pkt, decomp, name);
199 	labs = dname_count_size_labels(decomp, &len);
200 	log_assert(name_len == len);
201 	return name_tree_lookup(&priv->n, decomp, len, labs, dclass) != NULL;
202 }
203 
204 size_t priv_get_mem(struct iter_priv* priv)
205 {
206 	if(!priv) return 0;
207 	return sizeof(*priv) + regional_get_mem(priv->region);
208 }
209 
210 int priv_rrset_bad(struct iter_priv* priv, sldns_buffer* pkt,
211 	struct rrset_parse* rrset)
212 {
213 	if(priv->a.count == 0)
214 		return 0; /* there are no blocked addresses */
215 
216 	/* see if it is a private name, that is allowed to have any */
217 	if(priv_lookup_name(priv, pkt, rrset->dname, rrset->dname_len,
218 		ntohs(rrset->rrset_class))) {
219 		return 0;
220 	} else {
221 		/* so its a public name, check the address */
222 		socklen_t len;
223 		struct rr_parse* rr, *prev = NULL;
224 		if(rrset->type == LDNS_RR_TYPE_A) {
225 			struct sockaddr_storage addr;
226 			struct sockaddr_in sa;
227 
228 			len = (socklen_t)sizeof(sa);
229 			memset(&sa, 0, len);
230 			sa.sin_family = AF_INET;
231 			sa.sin_port = (in_port_t)htons(UNBOUND_DNS_PORT);
232 			for(rr = rrset->rr_first; rr; rr = rr->next) {
233 				if(sldns_read_uint16(rr->ttl_data+4)
234 					!= INET_SIZE) {
235 					prev = rr;
236 					continue;
237 				}
238 				memmove(&sa.sin_addr, rr->ttl_data+4+2,
239 					INET_SIZE);
240 				memmove(&addr, &sa, len);
241 				if(priv_lookup_addr(priv, &addr, len)) {
242 					if(msgparse_rrset_remove_rr("sanitize: removing public name with private address", pkt, rrset, prev, rr, &addr, len))
243 						return 1;
244 					continue;
245 				}
246 				prev = rr;
247 			}
248 		} else if(rrset->type == LDNS_RR_TYPE_AAAA) {
249 			struct sockaddr_storage addr;
250 			struct sockaddr_in6 sa;
251 			len = (socklen_t)sizeof(sa);
252 			memset(&sa, 0, len);
253 			sa.sin6_family = AF_INET6;
254 			sa.sin6_port = (in_port_t)htons(UNBOUND_DNS_PORT);
255 			for(rr = rrset->rr_first; rr; rr = rr->next) {
256 				if(sldns_read_uint16(rr->ttl_data+4)
257 					!= INET6_SIZE) {
258 					prev = rr;
259 					continue;
260 				}
261 				memmove(&sa.sin6_addr, rr->ttl_data+4+2,
262 					INET6_SIZE);
263 				memmove(&addr, &sa, len);
264 				if(priv_lookup_addr(priv, &addr, len)) {
265 					if(msgparse_rrset_remove_rr("sanitize: removing public name with private address", pkt, rrset, prev, rr, &addr, len))
266 						return 1;
267 					continue;
268 				}
269 				prev = rr;
270 			}
271 		}
272 	}
273 	return 0;
274 }
275