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