xref: /openbsd-src/usr.sbin/unbound/iterator/iter_donotq.c (revision 5d76a658a62184f1f911e7dc8293a7e6a3baf214)
1933707f3Ssthen /*
2933707f3Ssthen  * iterator/iter_donotq.c - iterative resolver donotqueryaddresses storage.
3933707f3Ssthen  *
4933707f3Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5933707f3Ssthen  *
6933707f3Ssthen  * This software is open source.
7933707f3Ssthen  *
8933707f3Ssthen  * Redistribution and use in source and binary forms, with or without
9933707f3Ssthen  * modification, are permitted provided that the following conditions
10933707f3Ssthen  * are met:
11933707f3Ssthen  *
12933707f3Ssthen  * Redistributions of source code must retain the above copyright notice,
13933707f3Ssthen  * this list of conditions and the following disclaimer.
14933707f3Ssthen  *
15933707f3Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16933707f3Ssthen  * this list of conditions and the following disclaimer in the documentation
17933707f3Ssthen  * and/or other materials provided with the distribution.
18933707f3Ssthen  *
19933707f3Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20933707f3Ssthen  * be used to endorse or promote products derived from this software without
21933707f3Ssthen  * specific prior written permission.
22933707f3Ssthen  *
23933707f3Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*5d76a658Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*5d76a658Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*5d76a658Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*5d76a658Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*5d76a658Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*5d76a658Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*5d76a658Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*5d76a658Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*5d76a658Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*5d76a658Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34933707f3Ssthen  */
35933707f3Ssthen 
36933707f3Ssthen /**
37933707f3Ssthen  * \file
38933707f3Ssthen  *
39933707f3Ssthen  * This file contains functions to assist the iterator module.
40933707f3Ssthen  * The donotqueryaddresses are stored and looked up. These addresses
41933707f3Ssthen  * (like 127.0.0.1) must not be used to send queries to, and can be
42933707f3Ssthen  * discarded immediately from the server selection.
43933707f3Ssthen  */
44933707f3Ssthen #include "config.h"
45933707f3Ssthen #include "iterator/iter_donotq.h"
46933707f3Ssthen #include "util/regional.h"
47933707f3Ssthen #include "util/log.h"
48933707f3Ssthen #include "util/config_file.h"
49933707f3Ssthen #include "util/net_help.h"
50933707f3Ssthen 
51933707f3Ssthen struct iter_donotq*
donotq_create(void)52933707f3Ssthen donotq_create(void)
53933707f3Ssthen {
54933707f3Ssthen 	struct iter_donotq* dq = (struct iter_donotq*)calloc(1,
55933707f3Ssthen 		sizeof(struct iter_donotq));
56933707f3Ssthen 	if(!dq)
57933707f3Ssthen 		return NULL;
58933707f3Ssthen 	dq->region = regional_create();
59933707f3Ssthen 	if(!dq->region) {
60933707f3Ssthen 		donotq_delete(dq);
61933707f3Ssthen 		return NULL;
62933707f3Ssthen 	}
63933707f3Ssthen 	return dq;
64933707f3Ssthen }
65933707f3Ssthen 
66933707f3Ssthen void
donotq_delete(struct iter_donotq * dq)67933707f3Ssthen donotq_delete(struct iter_donotq* dq)
68933707f3Ssthen {
69933707f3Ssthen 	if(!dq)
70933707f3Ssthen 		return;
71933707f3Ssthen 	regional_destroy(dq->region);
72933707f3Ssthen 	free(dq);
73933707f3Ssthen }
74933707f3Ssthen 
75933707f3Ssthen /** insert new address into donotq structure */
76933707f3Ssthen static int
donotq_insert(struct iter_donotq * dq,struct sockaddr_storage * addr,socklen_t addrlen,int net)77933707f3Ssthen donotq_insert(struct iter_donotq* dq, struct sockaddr_storage* addr,
78933707f3Ssthen 	socklen_t addrlen, int net)
79933707f3Ssthen {
80933707f3Ssthen 	struct addr_tree_node* node = (struct addr_tree_node*)regional_alloc(
81933707f3Ssthen 		dq->region, sizeof(*node));
82933707f3Ssthen 	if(!node)
83933707f3Ssthen 		return 0;
84933707f3Ssthen 	if(!addr_tree_insert(&dq->tree, node, addr, addrlen, net)) {
85933707f3Ssthen 		verbose(VERB_QUERY, "duplicate donotquery address ignored.");
86933707f3Ssthen 	}
87933707f3Ssthen 	return 1;
88933707f3Ssthen }
89933707f3Ssthen 
90933707f3Ssthen /** apply donotq string */
91933707f3Ssthen static int
donotq_str_cfg(struct iter_donotq * dq,const char * str)92933707f3Ssthen donotq_str_cfg(struct iter_donotq* dq, const char* str)
93933707f3Ssthen {
94933707f3Ssthen 	struct sockaddr_storage addr;
95933707f3Ssthen 	int net;
96933707f3Ssthen 	socklen_t addrlen;
97933707f3Ssthen 	verbose(VERB_ALGO, "donotq: %s", str);
98933707f3Ssthen 	if(!netblockstrtoaddr(str, UNBOUND_DNS_PORT, &addr, &addrlen, &net)) {
99933707f3Ssthen 		log_err("cannot parse donotquery netblock: %s", str);
100933707f3Ssthen 		return 0;
101933707f3Ssthen 	}
102933707f3Ssthen 	if(!donotq_insert(dq, &addr, addrlen, net)) {
103933707f3Ssthen 		log_err("out of memory");
104933707f3Ssthen 		return 0;
105933707f3Ssthen 	}
106933707f3Ssthen 	return 1;
107933707f3Ssthen }
108933707f3Ssthen 
109933707f3Ssthen /** read donotq config */
110933707f3Ssthen static int
read_donotq(struct iter_donotq * dq,struct config_file * cfg)111933707f3Ssthen read_donotq(struct iter_donotq* dq, struct config_file* cfg)
112933707f3Ssthen {
113933707f3Ssthen 	struct config_strlist* p;
114933707f3Ssthen 	for(p = cfg->donotqueryaddrs; p; p = p->next) {
115933707f3Ssthen 		log_assert(p->str);
116933707f3Ssthen 		if(!donotq_str_cfg(dq, p->str))
117933707f3Ssthen 			return 0;
118933707f3Ssthen 	}
119933707f3Ssthen 	return 1;
120933707f3Ssthen }
121933707f3Ssthen 
122933707f3Ssthen int
donotq_apply_cfg(struct iter_donotq * dq,struct config_file * cfg)123933707f3Ssthen donotq_apply_cfg(struct iter_donotq* dq, struct config_file* cfg)
124933707f3Ssthen {
125933707f3Ssthen 	regional_free_all(dq->region);
126933707f3Ssthen 	addr_tree_init(&dq->tree);
127933707f3Ssthen 	if(!read_donotq(dq, cfg))
128933707f3Ssthen 		return 0;
129933707f3Ssthen 	if(cfg->donotquery_localhost) {
130933707f3Ssthen 		if(!donotq_str_cfg(dq, "127.0.0.0/8"))
131933707f3Ssthen 			return 0;
132933707f3Ssthen 		if(cfg->do_ip6) {
133933707f3Ssthen 			if(!donotq_str_cfg(dq, "::1"))
134933707f3Ssthen 				return 0;
135933707f3Ssthen 		}
136933707f3Ssthen 	}
137933707f3Ssthen 	addr_tree_init_parents(&dq->tree);
138933707f3Ssthen 	return 1;
139933707f3Ssthen }
140933707f3Ssthen 
141933707f3Ssthen int
donotq_lookup(struct iter_donotq * donotq,struct sockaddr_storage * addr,socklen_t addrlen)142933707f3Ssthen donotq_lookup(struct iter_donotq* donotq, struct sockaddr_storage* addr,
143933707f3Ssthen         socklen_t addrlen)
144933707f3Ssthen {
145933707f3Ssthen 	return addr_tree_lookup(&donotq->tree, addr, addrlen) != NULL;
146933707f3Ssthen }
147933707f3Ssthen 
148933707f3Ssthen size_t
donotq_get_mem(struct iter_donotq * donotq)149933707f3Ssthen donotq_get_mem(struct iter_donotq* donotq)
150933707f3Ssthen {
151933707f3Ssthen 	if(!donotq) return 0;
152933707f3Ssthen 	return sizeof(*donotq) + regional_get_mem(donotq->region);
153933707f3Ssthen }
154