xref: /freebsd/contrib/unbound/services/view.c (revision be771a7b7f4580a30d99e41a5bb1b93a385a119d)
1 /*
2  * services/view.c - named views containing local zones authority service.
3  *
4  * Copyright (c) 2016, 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 enable named views that can hold local zone
40  * authority service.
41  */
42 #include "config.h"
43 #include "services/view.h"
44 #include "services/localzone.h"
45 #include "util/config_file.h"
46 #include "respip/respip.h"
47 
48 int
49 view_cmp(const void* v1, const void* v2)
50 {
51 	struct view* a = (struct view*)v1;
52 	struct view* b = (struct view*)v2;
53 
54 	return strcmp(a->name, b->name);
55 }
56 
57 struct views*
58 views_create(void)
59 {
60 	struct views* v = (struct views*)calloc(1,
61 		sizeof(*v));
62 	if(!v)
63 		return NULL;
64 	rbtree_init(&v->vtree, &view_cmp);
65 	lock_rw_init(&v->lock);
66 	lock_protect(&v->lock, &v->vtree, sizeof(v->vtree));
67 	return v;
68 }
69 
70 void
71 view_delete(struct view* v)
72 {
73 	if(!v)
74 		return;
75 	lock_rw_destroy(&v->lock);
76 	local_zones_delete(v->local_zones);
77 	respip_set_delete(v->respip_set);
78 	free(v->name);
79 	free(v);
80 }
81 
82 static void
83 delviewnode(rbnode_type* n, void* ATTR_UNUSED(arg))
84 {
85 	struct view* v = (struct view*)n;
86 	view_delete(v);
87 }
88 
89 void
90 views_delete(struct views* v)
91 {
92 	if(!v)
93 		return;
94 	lock_rw_destroy(&v->lock);
95 	traverse_postorder(&v->vtree, delviewnode, NULL);
96 	free(v);
97 }
98 
99 /** create a new view */
100 static struct view*
101 view_create(char* name)
102 {
103 	struct view* v = (struct view*)calloc(1, sizeof(*v));
104 	if(!v)
105 		return NULL;
106 	v->node.key = v;
107 	if(!(v->name = strdup(name))) {
108 		free(v);
109 		return NULL;
110 	}
111 	lock_rw_init(&v->lock);
112 	lock_protect(&v->lock, &v->name, sizeof(*v)-sizeof(rbnode_type));
113 	return v;
114 }
115 
116 /** enter a new view returns with WRlock */
117 static struct view*
118 views_enter_view_name(struct views* vs, char* name)
119 {
120 	struct view* v = view_create(name);
121 	if(!v) {
122 		log_err("out of memory");
123 		return NULL;
124 	}
125 
126 	/* add to rbtree */
127 	lock_rw_wrlock(&vs->lock);
128 	lock_rw_wrlock(&v->lock);
129 	if(!rbtree_insert(&vs->vtree, &v->node)) {
130 		log_warn("duplicate view: %s", name);
131 		lock_rw_unlock(&v->lock);
132 		view_delete(v);
133 		lock_rw_unlock(&vs->lock);
134 		return NULL;
135 	}
136 	lock_rw_unlock(&vs->lock);
137 	return v;
138 }
139 
140 int
141 views_apply_cfg(struct views* vs, struct config_file* cfg)
142 {
143 	struct config_view* cv;
144 	struct view* v;
145 	struct config_file lz_cfg;
146 	/* Check existence of name in first view (last in config). Rest of
147 	 * views are already checked when parsing config. */
148 	if(cfg->views && !cfg->views->name) {
149 		log_err("view without a name");
150 		return 0;
151 	}
152 	for(cv = cfg->views; cv; cv = cv->next) {
153 		/* create and enter view */
154 		if(!(v = views_enter_view_name(vs, cv->name)))
155 			return 0;
156 		v->isfirst = cv->isfirst;
157 		if(cv->local_zones || cv->local_data) {
158 			if(!(v->local_zones = local_zones_create())){
159 				lock_rw_unlock(&v->lock);
160 				return 0;
161 			}
162 			memset(&lz_cfg, 0, sizeof(lz_cfg));
163 			lz_cfg.local_zones = cv->local_zones;
164 			lz_cfg.local_data = cv->local_data;
165 			lz_cfg.local_zones_nodefault =
166 				cv->local_zones_nodefault;
167 			if(v->isfirst) {
168 				/* Do not add defaults to view-specific
169 				 * local-zone when global local zone will be
170 				 * used. */
171 				struct config_strlist* nd;
172 				lz_cfg.local_zones_disable_default = 1;
173 				/* Add nodefault zones to list of zones to add,
174 				 * so they will be used as if they are
175 				 * configured as type transparent */
176 				for(nd = cv->local_zones_nodefault; nd;
177 					nd = nd->next) {
178 					char* nd_str, *nd_type;
179 					nd_str = strdup(nd->str);
180 					if(!nd_str) {
181 						log_err("out of memory");
182 						lock_rw_unlock(&v->lock);
183 						return 0;
184 					}
185 					nd_type = strdup("nodefault");
186 					if(!nd_type) {
187 						log_err("out of memory");
188 						free(nd_str);
189 						lock_rw_unlock(&v->lock);
190 						return 0;
191 					}
192 					if(!cfg_str2list_insert(
193 						&lz_cfg.local_zones, nd_str,
194 						nd_type)) {
195 						log_err("failed to insert "
196 							"default zones into "
197 							"local-zone list");
198 						lock_rw_unlock(&v->lock);
199 						return 0;
200 					}
201 				}
202 			}
203 			if(!local_zones_apply_cfg(v->local_zones, &lz_cfg)){
204 				lock_rw_unlock(&v->lock);
205 				return 0;
206 			}
207 			/* local_zones, local_zones_nodefault and local_data
208 			 * are free'd from config_view by local_zones_apply_cfg.
209 			 * Set pointers to NULL. */
210 			cv->local_zones = NULL;
211 			cv->local_data = NULL;
212 			cv->local_zones_nodefault = NULL;
213 		}
214 		lock_rw_unlock(&v->lock);
215 	}
216 	return 1;
217 }
218 
219 /** find a view by name */
220 struct view*
221 views_find_view(struct views* vs, const char* name, int write)
222 {
223 	struct view* v;
224 	struct view key;
225 	key.node.key = &v;
226 	key.name = (char *)name;
227 	lock_rw_rdlock(&vs->lock);
228 	if(!(v = (struct view*)rbtree_search(&vs->vtree, &key.node))) {
229 		lock_rw_unlock(&vs->lock);
230 		return 0;
231 	}
232 	if(write) {
233 		lock_rw_wrlock(&v->lock);
234 	} else {
235 		lock_rw_rdlock(&v->lock);
236 	}
237 	lock_rw_unlock(&vs->lock);
238 	return v;
239 }
240 
241 void views_print(struct views* v)
242 {
243 	/* TODO implement print */
244 	(void)v;
245 }
246 
247 size_t views_get_mem(struct views* vs)
248 {
249 	struct view* v;
250 	size_t m;
251 	if(!vs) return 0;
252 	m = sizeof(struct views);
253 	lock_rw_rdlock(&vs->lock);
254 	RBTREE_FOR(v, struct view*, &vs->vtree) {
255 		m += view_get_mem(v);
256 	}
257 	lock_rw_unlock(&vs->lock);
258 	return m;
259 }
260 
261 size_t view_get_mem(struct view* v)
262 {
263 	size_t m = sizeof(*v);
264 	lock_rw_rdlock(&v->lock);
265 	m += getmem_str(v->name);
266 	m += local_zones_get_mem(v->local_zones);
267 	m += respip_set_get_mem(v->respip_set);
268 	lock_rw_unlock(&v->lock);
269 	return m;
270 }
271 
272 void views_swap_tree(struct views* vs, struct views* data)
273 {
274 	rbnode_type* oldroot = vs->vtree.root;
275 	size_t oldcount = vs->vtree.count;
276 	vs->vtree.root = data->vtree.root;
277 	vs->vtree.count = data->vtree.count;
278 	data->vtree.root = oldroot;
279 	data->vtree.count = oldcount;
280 }
281