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