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