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