xref: /freebsd/contrib/unbound/libunbound/context.c (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1 /*
2  * libunbound/context.c - validating context for unbound internal use
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 the validator context structure.
40  */
41 #include "config.h"
42 #include "libunbound/context.h"
43 #include "util/module.h"
44 #include "util/config_file.h"
45 #include "util/net_help.h"
46 #include "services/modstack.h"
47 #include "services/localzone.h"
48 #include "services/cache/rrset.h"
49 #include "services/cache/infra.h"
50 #include "services/authzone.h"
51 #include "services/listen_dnsport.h"
52 #include "util/data/msgreply.h"
53 #include "util/storage/slabhash.h"
54 #include "util/edns.h"
55 #include "sldns/sbuffer.h"
56 
57 int
58 context_finalize(struct ub_ctx* ctx)
59 {
60 	int is_rpz = 0;
61 	struct config_file* cfg = ctx->env->cfg;
62 	verbosity = cfg->verbosity;
63 	if(ctx_logfile_overridden && !ctx->logfile_override) {
64 		log_file(NULL); /* clear that override */
65 		ctx_logfile_overridden = 0;
66 	}
67 	if(ctx->logfile_override) {
68 		ctx_logfile_overridden = 1;
69 		log_file(ctx->log_out);
70 	} else {
71 		log_init(cfg->logfile, cfg->use_syslog, NULL);
72 	}
73 	ctx->pipe_pid = getpid();
74 	cfg_apply_local_port_policy(cfg, 65536);
75 	config_apply(cfg);
76 	if(!modstack_setup(&ctx->mods, cfg->module_conf, ctx->env))
77 		return UB_INITFAIL;
78 	listen_setup_locks();
79 	log_edns_known_options(VERB_ALGO, ctx->env);
80 	ctx->local_zones = local_zones_create();
81 	if(!ctx->local_zones)
82 		return UB_NOMEM;
83 	if(!local_zones_apply_cfg(ctx->local_zones, cfg))
84 		return UB_INITFAIL;
85 	if(!auth_zones_apply_cfg(ctx->env->auth_zones, cfg, 1, &is_rpz,
86 		ctx->env, &ctx->mods))
87 		return UB_INITFAIL;
88 	if(!edns_strings_apply_cfg(ctx->env->edns_strings, cfg))
89 		return UB_INITFAIL;
90 	if(!slabhash_is_size(ctx->env->msg_cache, cfg->msg_cache_size,
91 		cfg->msg_cache_slabs)) {
92 		slabhash_delete(ctx->env->msg_cache);
93 		ctx->env->msg_cache = slabhash_create(cfg->msg_cache_slabs,
94 			HASH_DEFAULT_STARTARRAY, cfg->msg_cache_size,
95 			msgreply_sizefunc, query_info_compare,
96 			query_entry_delete, reply_info_delete, NULL);
97 		if(!ctx->env->msg_cache)
98 			return UB_NOMEM;
99 	}
100 	ctx->env->rrset_cache = rrset_cache_adjust(ctx->env->rrset_cache,
101 		ctx->env->cfg, ctx->env->alloc);
102 	if(!ctx->env->rrset_cache)
103 		return UB_NOMEM;
104 	ctx->env->infra_cache = infra_adjust(ctx->env->infra_cache, cfg);
105 	if(!ctx->env->infra_cache)
106 		return UB_NOMEM;
107 	ctx->finalized = 1;
108 	return UB_NOERROR;
109 }
110 
111 int context_query_cmp(const void* a, const void* b)
112 {
113 	if( *(int*)a < *(int*)b )
114 		return -1;
115 	if( *(int*)a > *(int*)b )
116 		return 1;
117 	return 0;
118 }
119 
120 void
121 context_query_delete(struct ctx_query* q)
122 {
123 	if(!q) return;
124 	ub_resolve_free(q->res);
125 	free(q->msg);
126 	free(q);
127 }
128 
129 /** How many times to try to find an unused query-id-number for async */
130 #define NUM_ID_TRIES 100000
131 /** find next useful id number of 0 on error */
132 static int
133 find_id(struct ub_ctx* ctx, int* id)
134 {
135 	size_t tries = 0;
136 	ctx->next_querynum++;
137 	while(rbtree_search(&ctx->queries, &ctx->next_querynum)) {
138 		ctx->next_querynum++; /* numerical wraparound is fine */
139 		if(tries++ > NUM_ID_TRIES)
140 			return 0;
141 	}
142 	*id = ctx->next_querynum;
143 	return 1;
144 }
145 
146 struct ctx_query*
147 context_new(struct ub_ctx* ctx, const char* name, int rrtype, int rrclass,
148 	ub_callback_type cb, ub_event_callback_type cb_event, void* cbarg)
149 {
150 	struct ctx_query* q = (struct ctx_query*)calloc(1, sizeof(*q));
151 	if(!q) return NULL;
152 	lock_basic_lock(&ctx->cfglock);
153 	if(!find_id(ctx, &q->querynum)) {
154 		lock_basic_unlock(&ctx->cfglock);
155 		free(q);
156 		return NULL;
157 	}
158 	lock_basic_unlock(&ctx->cfglock);
159 	q->node.key = &q->querynum;
160 	q->async = (cb != NULL || cb_event != NULL);
161 	q->cb = cb;
162 	q->cb_event = cb_event;
163 	q->cb_arg = cbarg;
164 	q->res = (struct ub_result*)calloc(1, sizeof(*q->res));
165 	if(!q->res) {
166 		free(q);
167 		return NULL;
168 	}
169 	q->res->qname = strdup(name);
170 	if(!q->res->qname) {
171 		free(q->res);
172 		free(q);
173 		return NULL;
174 	}
175 	q->res->qtype = rrtype;
176 	q->res->qclass = rrclass;
177 
178 	/* add to query list */
179 	lock_basic_lock(&ctx->cfglock);
180 	if(q->async)
181 		ctx->num_async ++;
182 	(void)rbtree_insert(&ctx->queries, &q->node);
183 	lock_basic_unlock(&ctx->cfglock);
184 	return q;
185 }
186 
187 struct alloc_cache*
188 context_obtain_alloc(struct ub_ctx* ctx, int locking)
189 {
190 	struct alloc_cache* a;
191 	int tnum = 0;
192 	if(locking) {
193 		lock_basic_lock(&ctx->cfglock);
194 	}
195 	a = ctx->alloc_list;
196 	if(a)
197 		ctx->alloc_list = a->super; /* snip off list */
198 	else	tnum = ctx->thr_next_num++;
199 	if(locking) {
200 		lock_basic_unlock(&ctx->cfglock);
201 	}
202 	if(a) {
203 		a->super = &ctx->superalloc;
204 		return a;
205 	}
206 	a = (struct alloc_cache*)calloc(1, sizeof(*a));
207 	if(!a)
208 		return NULL;
209 	alloc_init(a, &ctx->superalloc, tnum);
210 	return a;
211 }
212 
213 void
214 context_release_alloc(struct ub_ctx* ctx, struct alloc_cache* alloc,
215 	int locking)
216 {
217 	if(!ctx || !alloc)
218 		return;
219 	if(locking) {
220 		lock_basic_lock(&ctx->cfglock);
221 	}
222 	alloc->super = ctx->alloc_list;
223 	ctx->alloc_list = alloc;
224 	if(locking) {
225 		lock_basic_unlock(&ctx->cfglock);
226 	}
227 }
228 
229 uint8_t*
230 context_serialize_new_query(struct ctx_query* q, uint32_t* len)
231 {
232 	/* format for new query is
233 	 * 	o uint32 cmd
234 	 * 	o uint32 id
235 	 * 	o uint32 type
236 	 * 	o uint32 class
237 	 * 	o rest queryname (string)
238 	 */
239 	uint8_t* p;
240 	size_t slen = strlen(q->res->qname) + 1/*end of string*/;
241 	*len = sizeof(uint32_t)*4 + slen;
242 	p = (uint8_t*)malloc(*len);
243 	if(!p) return NULL;
244 	sldns_write_uint32(p, UB_LIBCMD_NEWQUERY);
245 	sldns_write_uint32(p+sizeof(uint32_t), (uint32_t)q->querynum);
246 	sldns_write_uint32(p+2*sizeof(uint32_t), (uint32_t)q->res->qtype);
247 	sldns_write_uint32(p+3*sizeof(uint32_t), (uint32_t)q->res->qclass);
248 	memmove(p+4*sizeof(uint32_t), q->res->qname, slen);
249 	return p;
250 }
251 
252 struct ctx_query*
253 context_deserialize_new_query(struct ub_ctx* ctx, uint8_t* p, uint32_t len)
254 {
255 	struct ctx_query* q = (struct ctx_query*)calloc(1, sizeof(*q));
256 	if(!q) return NULL;
257 	if(len < 4*sizeof(uint32_t)+1) {
258 		free(q);
259 		return NULL;
260 	}
261 	log_assert( sldns_read_uint32(p) == UB_LIBCMD_NEWQUERY);
262 	q->querynum = (int)sldns_read_uint32(p+sizeof(uint32_t));
263 	q->node.key = &q->querynum;
264 	q->async = 1;
265 	q->res = (struct ub_result*)calloc(1, sizeof(*q->res));
266 	if(!q->res) {
267 		free(q);
268 		return NULL;
269 	}
270 	q->res->qtype = (int)sldns_read_uint32(p+2*sizeof(uint32_t));
271 	q->res->qclass = (int)sldns_read_uint32(p+3*sizeof(uint32_t));
272 	q->res->qname = strdup((char*)(p+4*sizeof(uint32_t)));
273 	if(!q->res->qname) {
274 		free(q->res);
275 		free(q);
276 		return NULL;
277 	}
278 
279 	/** add to query list */
280 	ctx->num_async++;
281 	(void)rbtree_insert(&ctx->queries, &q->node);
282 	return q;
283 }
284 
285 struct ctx_query*
286 context_lookup_new_query(struct ub_ctx* ctx, uint8_t* p, uint32_t len)
287 {
288 	struct ctx_query* q;
289 	int querynum;
290 	if(len < 4*sizeof(uint32_t)+1) {
291 		return NULL;
292 	}
293 	log_assert( sldns_read_uint32(p) == UB_LIBCMD_NEWQUERY);
294 	querynum = (int)sldns_read_uint32(p+sizeof(uint32_t));
295 	q = (struct ctx_query*)rbtree_search(&ctx->queries, &querynum);
296 	if(!q) {
297 		return NULL;
298 	}
299 	log_assert(q->async);
300 	return q;
301 }
302 
303 uint8_t*
304 context_serialize_answer(struct ctx_query* q, int err, sldns_buffer* pkt,
305 	uint32_t* len)
306 {
307 	/* answer format
308 	 * 	o uint32 cmd
309 	 * 	o uint32 id
310 	 * 	o uint32 error_code
311 	 * 	o uint32 msg_security
312 	 * 	o uint32 was_ratelimited
313 	 * 	o uint32 length of why_bogus string (+1 for eos); 0 absent.
314 	 * 	o why_bogus_string
315 	 * 	o the remainder is the answer msg from resolver lookup.
316 	 * 	  remainder can be length 0.
317 	 */
318 	size_t size_of_uint32s = 6 * sizeof(uint32_t);
319 	size_t pkt_len = pkt?sldns_buffer_remaining(pkt):0;
320 	size_t wlen = (pkt&&q->res->why_bogus)?strlen(q->res->why_bogus)+1:0;
321 	uint8_t* p;
322 	*len = size_of_uint32s + pkt_len + wlen;
323 	p = (uint8_t*)malloc(*len);
324 	if(!p) return NULL;
325 	sldns_write_uint32(p, UB_LIBCMD_ANSWER);
326 	sldns_write_uint32(p+sizeof(uint32_t), (uint32_t)q->querynum);
327 	sldns_write_uint32(p+2*sizeof(uint32_t), (uint32_t)err);
328 	sldns_write_uint32(p+3*sizeof(uint32_t), (uint32_t)q->msg_security);
329 	sldns_write_uint32(p+4*sizeof(uint32_t), (uint32_t)q->res->was_ratelimited);
330 	sldns_write_uint32(p+5*sizeof(uint32_t), (uint32_t)wlen);
331 	if(wlen > 0)
332 		memmove(p+size_of_uint32s, q->res->why_bogus, wlen);
333 	if(pkt_len > 0)
334 		memmove(p+size_of_uint32s+wlen,
335 			sldns_buffer_begin(pkt), pkt_len);
336 	return p;
337 }
338 
339 struct ctx_query*
340 context_deserialize_answer(struct ub_ctx* ctx,
341         uint8_t* p, uint32_t len, int* err)
342 {
343 	size_t size_of_uint32s = 6 * sizeof(uint32_t);
344 	struct ctx_query* q = NULL ;
345 	int id;
346 	size_t wlen;
347 	if(len < size_of_uint32s) return NULL;
348 	log_assert( sldns_read_uint32(p) == UB_LIBCMD_ANSWER);
349 	id = (int)sldns_read_uint32(p+sizeof(uint32_t));
350 	q = (struct ctx_query*)rbtree_search(&ctx->queries, &id);
351 	if(!q) return NULL;
352 	*err = (int)sldns_read_uint32(p+2*sizeof(uint32_t));
353 	q->msg_security = sldns_read_uint32(p+3*sizeof(uint32_t));
354 	q->res->was_ratelimited = (int)sldns_read_uint32(p+4*sizeof(uint32_t));
355 	wlen = (size_t)sldns_read_uint32(p+5*sizeof(uint32_t));
356 	if(len > size_of_uint32s && wlen > 0) {
357 		if(len >= size_of_uint32s+wlen)
358 			q->res->why_bogus = (char*)memdup(
359 				p+size_of_uint32s, wlen);
360 		if(!q->res->why_bogus) {
361 			/* pass malloc failure to the user callback */
362 			q->msg_len = 0;
363 			*err = UB_NOMEM;
364 			return q;
365 		}
366 		q->res->why_bogus[wlen-1] = 0; /* zero terminated for sure */
367 	}
368 	if(len > size_of_uint32s+wlen) {
369 		q->msg_len = len - size_of_uint32s - wlen;
370 		q->msg = (uint8_t*)memdup(p+size_of_uint32s+wlen,
371 			q->msg_len);
372 		if(!q->msg) {
373 			/* pass malloc failure to the user callback */
374 			q->msg_len = 0;
375 			*err = UB_NOMEM;
376 			return q;
377 		}
378 	}
379 	return q;
380 }
381 
382 uint8_t*
383 context_serialize_cancel(struct ctx_query* q, uint32_t* len)
384 {
385 	/* format of cancel:
386 	 * 	o uint32 cmd
387 	 * 	o uint32 async-id */
388 	uint8_t* p = (uint8_t*)reallocarray(NULL, sizeof(uint32_t), 2);
389 	if(!p) return NULL;
390 	*len = 2*sizeof(uint32_t);
391 	sldns_write_uint32(p, UB_LIBCMD_CANCEL);
392 	sldns_write_uint32(p+sizeof(uint32_t), (uint32_t)q->querynum);
393 	return p;
394 }
395 
396 struct ctx_query* context_deserialize_cancel(struct ub_ctx* ctx,
397         uint8_t* p, uint32_t len)
398 {
399 	struct ctx_query* q;
400 	int id;
401 	if(len != 2*sizeof(uint32_t)) return NULL;
402 	log_assert( sldns_read_uint32(p) == UB_LIBCMD_CANCEL);
403 	id = (int)sldns_read_uint32(p+sizeof(uint32_t));
404 	q = (struct ctx_query*)rbtree_search(&ctx->queries, &id);
405 	return q;
406 }
407 
408 uint8_t*
409 context_serialize_quit(uint32_t* len)
410 {
411 	uint32_t* p = (uint32_t*)malloc(sizeof(uint32_t));
412 	if(!p)
413 		return NULL;
414 	*len = sizeof(uint32_t);
415 	sldns_write_uint32(p, UB_LIBCMD_QUIT);
416 	return (uint8_t*)p;
417 }
418 
419 enum ub_ctx_cmd context_serial_getcmd(uint8_t* p, uint32_t len)
420 {
421 	uint32_t v;
422 	if((size_t)len < sizeof(v))
423 		return UB_LIBCMD_QUIT;
424 	v = sldns_read_uint32(p);
425 	return v;
426 }
427