xref: /freebsd/contrib/unbound/cachedb/cachedb.c (revision e17f5b1d)
1 /*
2  * cachedb/cachedb.c - cache from a database external to the program module
3  *
4  * Copyright (c) 2016, 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 a module that uses an external database to cache
40  * dns responses.
41  */
42 
43 #include "config.h"
44 #ifdef USE_CACHEDB
45 #include "cachedb/cachedb.h"
46 #include "cachedb/redis.h"
47 #include "util/regional.h"
48 #include "util/net_help.h"
49 #include "util/config_file.h"
50 #include "util/data/msgreply.h"
51 #include "util/data/msgencode.h"
52 #include "services/cache/dns.h"
53 #include "validator/val_neg.h"
54 #include "validator/val_secalgo.h"
55 #include "iterator/iter_utils.h"
56 #include "sldns/parseutil.h"
57 #include "sldns/wire2str.h"
58 #include "sldns/sbuffer.h"
59 
60 /* header file for htobe64 */
61 #ifdef HAVE_ENDIAN_H
62 #  include <endian.h>
63 #endif
64 #ifdef HAVE_SYS_ENDIAN_H
65 #  include <sys/endian.h>
66 #endif
67 
68 #ifndef HAVE_HTOBE64
69 #  ifdef HAVE_LIBKERN_OSBYTEORDER_H
70      /* In practice this is specific to MacOS X.  We assume it doesn't have
71       * htobe64/be64toh but has alternatives with a different name. */
72 #    include <libkern/OSByteOrder.h>
73 #    define htobe64(x) OSSwapHostToBigInt64(x)
74 #    define be64toh(x) OSSwapBigToHostInt64(x)
75 #  else
76      /* not OSX */
77      /* Some compilers do not define __BYTE_ORDER__, like IBM XLC on AIX */
78 #    if __BIG_ENDIAN__
79 #      define be64toh(n) (n)
80 #      define htobe64(n) (n)
81 #    else
82 #      define be64toh(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
83 #      define htobe64(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
84 #    endif /* _ENDIAN */
85 #  endif /* HAVE_LIBKERN_OSBYTEORDER_H */
86 #endif /* HAVE_BE64TOH */
87 
88 /** the unit test testframe for cachedb, its module state contains
89  * a cache for a couple queries (in memory). */
90 struct testframe_moddata {
91 	/** lock for mutex */
92 	lock_basic_type lock;
93 	/** key for single stored data element, NULL if none */
94 	char* stored_key;
95 	/** data for single stored data element, NULL if none */
96 	uint8_t* stored_data;
97 	/** length of stored data */
98 	size_t stored_datalen;
99 };
100 
101 static int
102 testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
103 {
104 	struct testframe_moddata* d;
105 	(void)env;
106 	verbose(VERB_ALGO, "testframe_init");
107 	d = (struct testframe_moddata*)calloc(1,
108 		sizeof(struct testframe_moddata));
109 	cachedb_env->backend_data = (void*)d;
110 	if(!cachedb_env->backend_data) {
111 		log_err("out of memory");
112 		return 0;
113 	}
114 	lock_basic_init(&d->lock);
115 	lock_protect(&d->lock, d, sizeof(*d));
116 	return 1;
117 }
118 
119 static void
120 testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
121 {
122 	struct testframe_moddata* d = (struct testframe_moddata*)
123 		cachedb_env->backend_data;
124 	(void)env;
125 	verbose(VERB_ALGO, "testframe_deinit");
126 	if(!d)
127 		return;
128 	lock_basic_destroy(&d->lock);
129 	free(d->stored_key);
130 	free(d->stored_data);
131 	free(d);
132 }
133 
134 static int
135 testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
136 	char* key, struct sldns_buffer* result_buffer)
137 {
138 	struct testframe_moddata* d = (struct testframe_moddata*)
139 		cachedb_env->backend_data;
140 	(void)env;
141 	verbose(VERB_ALGO, "testframe_lookup of %s", key);
142 	lock_basic_lock(&d->lock);
143 	if(d->stored_key && strcmp(d->stored_key, key) == 0) {
144 		if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) {
145 			lock_basic_unlock(&d->lock);
146 			return 0; /* too large */
147 		}
148 		verbose(VERB_ALGO, "testframe_lookup found %d bytes",
149 			(int)d->stored_datalen);
150 		sldns_buffer_clear(result_buffer);
151 		sldns_buffer_write(result_buffer, d->stored_data,
152 			d->stored_datalen);
153 		sldns_buffer_flip(result_buffer);
154 		lock_basic_unlock(&d->lock);
155 		return 1;
156 	}
157 	lock_basic_unlock(&d->lock);
158 	return 0;
159 }
160 
161 static void
162 testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
163 	char* key, uint8_t* data, size_t data_len)
164 {
165 	struct testframe_moddata* d = (struct testframe_moddata*)
166 		cachedb_env->backend_data;
167 	(void)env;
168 	lock_basic_lock(&d->lock);
169 	verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
170 
171 	/* free old data element (if any) */
172 	free(d->stored_key);
173 	d->stored_key = NULL;
174 	free(d->stored_data);
175 	d->stored_data = NULL;
176 	d->stored_datalen = 0;
177 
178 	d->stored_data = memdup(data, data_len);
179 	if(!d->stored_data) {
180 		lock_basic_unlock(&d->lock);
181 		log_err("out of memory");
182 		return;
183 	}
184 	d->stored_datalen = data_len;
185 	d->stored_key = strdup(key);
186 	if(!d->stored_key) {
187 		free(d->stored_data);
188 		d->stored_data = NULL;
189 		d->stored_datalen = 0;
190 		lock_basic_unlock(&d->lock);
191 		return;
192 	}
193 	lock_basic_unlock(&d->lock);
194 	/* (key,data) successfully stored */
195 }
196 
197 /** The testframe backend is for unit tests */
198 static struct cachedb_backend testframe_backend = { "testframe",
199 	testframe_init, testframe_deinit, testframe_lookup, testframe_store
200 };
201 
202 /** find a particular backend from possible backends */
203 static struct cachedb_backend*
204 cachedb_find_backend(const char* str)
205 {
206 #ifdef USE_REDIS
207 	if(strcmp(str, redis_backend.name) == 0)
208 		return &redis_backend;
209 #endif
210 	if(strcmp(str, testframe_backend.name) == 0)
211 		return &testframe_backend;
212 	/* TODO add more backends here */
213 	return NULL;
214 }
215 
216 /** apply configuration to cachedb module 'global' state */
217 static int
218 cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
219 {
220 	const char* backend_str = cfg->cachedb_backend;
221 	cachedb_env->backend = cachedb_find_backend(backend_str);
222 	if(!cachedb_env->backend) {
223 		log_err("cachedb: cannot find backend name '%s'", backend_str);
224 		return 0;
225 	}
226 
227 	/* TODO see if more configuration needs to be applied or not */
228 	return 1;
229 }
230 
231 int
232 cachedb_init(struct module_env* env, int id)
233 {
234 	struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1,
235 		sizeof(struct cachedb_env));
236 	if(!cachedb_env) {
237 		log_err("malloc failure");
238 		return 0;
239 	}
240 	env->modinfo[id] = (void*)cachedb_env;
241 	if(!cachedb_apply_cfg(cachedb_env, env->cfg)) {
242 		log_err("cachedb: could not apply configuration settings.");
243 		free(cachedb_env);
244 		env->modinfo[id] = NULL;
245 		return 0;
246 	}
247 	/* see if a backend is selected */
248 	if(!cachedb_env->backend || !cachedb_env->backend->name)
249 		return 1;
250 	if(!(*cachedb_env->backend->init)(env, cachedb_env)) {
251 		log_err("cachedb: could not init %s backend",
252 			cachedb_env->backend->name);
253 		free(cachedb_env);
254 		env->modinfo[id] = NULL;
255 		return 0;
256 	}
257 	cachedb_env->enabled = 1;
258 	if(env->cfg->serve_expired_reply_ttl)
259 		log_warn(
260 			"cachedb: serve-expired-reply-ttl is set but not working for data "
261 			"originating from the external cache; 0 TLL is used for those.");
262 	if(env->cfg->serve_expired_client_timeout)
263 		log_warn(
264 			"cachedb: serve-expired-client-timeout is set but not working for "
265 			"data originating from the external cache; expired data are used "
266 			"in the reply without first trying to refresh the data.");
267 	return 1;
268 }
269 
270 void
271 cachedb_deinit(struct module_env* env, int id)
272 {
273 	struct cachedb_env* cachedb_env;
274 	if(!env || !env->modinfo[id])
275 		return;
276 	cachedb_env = (struct cachedb_env*)env->modinfo[id];
277 	/* free contents */
278 	/* TODO */
279 	if(cachedb_env->enabled) {
280 		(*cachedb_env->backend->deinit)(env, cachedb_env);
281 	}
282 
283 	free(cachedb_env);
284 	env->modinfo[id] = NULL;
285 }
286 
287 /** new query for cachedb */
288 static int
289 cachedb_new(struct module_qstate* qstate, int id)
290 {
291 	struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc(
292 		qstate->region, sizeof(struct cachedb_qstate));
293 	qstate->minfo[id] = iq;
294 	if(!iq)
295 		return 0;
296 	memset(iq, 0, sizeof(*iq));
297 	/* initialise it */
298 	/* TODO */
299 
300 	return 1;
301 }
302 
303 /**
304  * Return an error
305  * @param qstate: our query state
306  * @param id: module id
307  * @param rcode: error code (DNS errcode).
308  * @return: 0 for use by caller, to make notation easy, like:
309  * 	return error_response(..).
310  */
311 static int
312 error_response(struct module_qstate* qstate, int id, int rcode)
313 {
314 	verbose(VERB_QUERY, "return error response %s",
315 		sldns_lookup_by_id(sldns_rcodes, rcode)?
316 		sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
317 	qstate->return_rcode = rcode;
318 	qstate->return_msg = NULL;
319 	qstate->ext_state[id] = module_finished;
320 	return 0;
321 }
322 
323 /**
324  * Hash the query name, type, class and dbacess-secret into lookup buffer.
325  * @param qstate: query state with query info
326  * 	and env->cfg with secret.
327  * @param buf: returned buffer with hash to lookup
328  * @param len: length of the buffer.
329  */
330 static void
331 calc_hash(struct module_qstate* qstate, char* buf, size_t len)
332 {
333 	uint8_t clear[1024];
334 	size_t clen = 0;
335 	uint8_t hash[CACHEDB_HASHSIZE/8];
336 	const char* hex = "0123456789ABCDEF";
337 	const char* secret = qstate->env->cfg->cachedb_secret;
338 	size_t i;
339 
340 	/* copy the hash info into the clear buffer */
341 	if(clen + qstate->qinfo.qname_len < sizeof(clear)) {
342 		memmove(clear+clen, qstate->qinfo.qname,
343 			qstate->qinfo.qname_len);
344 		clen += qstate->qinfo.qname_len;
345 	}
346 	if(clen + 4 < sizeof(clear)) {
347 		uint16_t t = htons(qstate->qinfo.qtype);
348 		uint16_t c = htons(qstate->qinfo.qclass);
349 		memmove(clear+clen, &t, 2);
350 		memmove(clear+clen+2, &c, 2);
351 		clen += 4;
352 	}
353 	if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) {
354 		memmove(clear+clen, secret, strlen(secret));
355 		clen += strlen(secret);
356 	}
357 
358 	/* hash the buffer */
359 	secalgo_hash_sha256(clear, clen, hash);
360 #ifdef HAVE_EXPLICIT_BZERO
361 	explicit_bzero(clear, clen);
362 #else
363 	memset(clear, 0, clen);
364 #endif
365 
366 	/* hex encode output for portability (some online dbs need
367 	 * no nulls, no control characters, and so on) */
368 	log_assert(len >= sizeof(hash)*2 + 1);
369 	(void)len;
370 	for(i=0; i<sizeof(hash); i++) {
371 		buf[i*2] = hex[(hash[i]&0xf0)>>4];
372 		buf[i*2+1] = hex[hash[i]&0x0f];
373 	}
374 	buf[sizeof(hash)*2] = 0;
375 }
376 
377 /** convert data from return_msg into the data buffer */
378 static int
379 prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
380 {
381 	uint64_t timestamp, expiry;
382 	size_t oldlim;
383 	struct edns_data edns;
384 	memset(&edns, 0, sizeof(edns));
385 	edns.edns_present = 1;
386 	edns.bits = EDNS_DO;
387 	edns.ext_rcode = 0;
388 	edns.edns_version = EDNS_ADVERTISED_VERSION;
389 	edns.udp_size = EDNS_ADVERTISED_SIZE;
390 
391 	if(!qstate->return_msg || !qstate->return_msg->rep)
392 		return 0;
393 	/* We don't store the reply if its TTL is 0 unless serve-expired is
394 	 * enabled.  Such a reply won't be reusable and simply be a waste for
395 	 * the backend.  It's also compatible with the default behavior of
396 	 * dns_cache_store_msg(). */
397 	if(qstate->return_msg->rep->ttl == 0 &&
398 		!qstate->env->cfg->serve_expired)
399 		return 0;
400 	if(verbosity >= VERB_ALGO)
401 		log_dns_msg("cachedb encoding", &qstate->return_msg->qinfo,
402 	                qstate->return_msg->rep);
403 	if(!reply_info_answer_encode(&qstate->return_msg->qinfo,
404 		qstate->return_msg->rep, 0, qstate->query_flags,
405 		buf, 0, 1, qstate->env->scratch, 65535, &edns, 1, 0))
406 		return 0;
407 
408 	/* TTLs in the return_msg are relative to time(0) so we have to
409 	 * store that, we also store the smallest ttl in the packet+time(0)
410 	 * as the packet expiry time */
411 	/* qstate->return_msg->rep->ttl contains that relative shortest ttl */
412 	timestamp = (uint64_t)*qstate->env->now;
413 	expiry = timestamp + (uint64_t)qstate->return_msg->rep->ttl;
414 	timestamp = htobe64(timestamp);
415 	expiry = htobe64(expiry);
416 	oldlim = sldns_buffer_limit(buf);
417 	if(oldlim + sizeof(timestamp)+sizeof(expiry) >=
418 		sldns_buffer_capacity(buf))
419 		return 0; /* doesn't fit. */
420 	sldns_buffer_set_limit(buf, oldlim + sizeof(timestamp)+sizeof(expiry));
421 	sldns_buffer_write_at(buf, oldlim, &timestamp, sizeof(timestamp));
422 	sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry,
423 		sizeof(expiry));
424 
425 	return 1;
426 }
427 
428 /** check expiry, return true if matches OK */
429 static int
430 good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf)
431 {
432 	uint64_t expiry;
433 	/* the expiry time is the last bytes of the buffer */
434 	if(sldns_buffer_limit(buf) < sizeof(expiry))
435 		return 0;
436 	sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry),
437 		&expiry, sizeof(expiry));
438 	expiry = be64toh(expiry);
439 
440 	/* Check if we are allowed to return expired entries:
441 	 * - serve_expired needs to be set
442 	 * - if SERVE_EXPIRED_TTL is set make sure that the record is not older
443 	 *   than that. */
444 	if((time_t)expiry < *qstate->env->now &&
445 		(!qstate->env->cfg->serve_expired ||
446 			(SERVE_EXPIRED_TTL &&
447 			*qstate->env->now - (time_t)expiry > SERVE_EXPIRED_TTL)))
448 		return 0;
449 
450 	return 1;
451 }
452 
453 /* Adjust the TTL of the given RRset by 'subtract'.  If 'subtract' is
454  * negative, set the TTL to 0. */
455 static void
456 packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract)
457 {
458 	size_t i;
459 	size_t total = data->count + data->rrsig_count;
460 	if(subtract >= 0 && data->ttl > subtract)
461 		data->ttl -= subtract;
462 	else	data->ttl = 0;
463 	for(i=0; i<total; i++) {
464 		if(subtract >= 0 && data->rr_ttl[i] > subtract)
465 			data->rr_ttl[i] -= subtract;
466 		else	data->rr_ttl[i] = 0;
467 	}
468 }
469 
470 /* Adjust the TTL of a DNS message and its RRs by 'adjust'.  If 'adjust' is
471  * negative, set the TTLs to 0. */
472 static void
473 adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
474 {
475 	size_t i;
476 	if(adjust >= 0 && msg->rep->ttl > adjust)
477 		msg->rep->ttl -= adjust;
478 	else
479 		msg->rep->ttl = 0;
480 	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
481 	msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
482 
483 	for(i=0; i<msg->rep->rrset_count; i++) {
484 		packed_rrset_ttl_subtract((struct packed_rrset_data*)msg->
485 			rep->rrsets[i]->entry.data, adjust);
486 	}
487 }
488 
489 /** convert dns message in buffer to return_msg */
490 static int
491 parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
492 {
493 	struct msg_parse* prs;
494 	struct edns_data edns;
495 	uint64_t timestamp, expiry;
496 	time_t adjust;
497 	size_t lim = sldns_buffer_limit(buf);
498 	if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
499 		return 0; /* too short */
500 
501 	/* remove timestamp and expiry from end */
502 	sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry));
503 	sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp),
504 		&timestamp, sizeof(timestamp));
505 	expiry = be64toh(expiry);
506 	timestamp = be64toh(timestamp);
507 
508 	/* parse DNS packet */
509 	regional_free_all(qstate->env->scratch);
510 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
511 		sizeof(struct msg_parse));
512 	if(!prs)
513 		return 0; /* out of memory */
514 	memset(prs, 0, sizeof(*prs));
515 	memset(&edns, 0, sizeof(edns));
516 	sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp));
517 	if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
518 		sldns_buffer_set_limit(buf, lim);
519 		return 0;
520 	}
521 	if(parse_extract_edns(prs, &edns, qstate->env->scratch) !=
522 		LDNS_RCODE_NOERROR) {
523 		sldns_buffer_set_limit(buf, lim);
524 		return 0;
525 	}
526 
527 	qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
528 	sldns_buffer_set_limit(buf, lim);
529 	if(!qstate->return_msg)
530 		return 0;
531 
532 	qstate->return_rcode = LDNS_RCODE_NOERROR;
533 
534 	/* see how much of the TTL expired, and remove it */
535 	if(*qstate->env->now <= (time_t)timestamp) {
536 		verbose(VERB_ALGO, "cachedb msg adjust by zero");
537 		return 1; /* message from the future (clock skew?) */
538 	}
539 	adjust = *qstate->env->now - (time_t)timestamp;
540 	if(qstate->return_msg->rep->ttl < adjust) {
541 		verbose(VERB_ALGO, "cachedb msg expired");
542 		/* If serve-expired is enabled, we still use an expired message
543 		 * setting the TTL to 0. */
544 		if(qstate->env->cfg->serve_expired)
545 			adjust = -1;
546 		else
547 			return 0; /* message expired */
548 	}
549 	verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
550 	adjust_msg_ttl(qstate->return_msg, adjust);
551 
552 	/* Similar to the unbound worker, if serve-expired is enabled and
553 	 * the msg would be considered to be expired, mark the state so a
554 	 * refetch will be scheduled.  The comparison between 'expiry' and
555 	 * 'now' should be redundant given how these values were calculated,
556 	 * but we check it just in case as does good_expiry_and_qinfo(). */
557 	if(qstate->env->cfg->serve_expired &&
558 		(adjust == -1 || (time_t)expiry < *qstate->env->now)) {
559 		qstate->need_refetch = 1;
560 	}
561 
562 	return 1;
563 }
564 
565 /**
566  * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
567  * return true if lookup was successful.
568  */
569 static int
570 cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie)
571 {
572 	char key[(CACHEDB_HASHSIZE/8)*2+1];
573 	calc_hash(qstate, key, sizeof(key));
574 
575 	/* call backend to fetch data for key into scratch buffer */
576 	if( !(*ie->backend->lookup)(qstate->env, ie, key,
577 		qstate->env->scratch_buffer)) {
578 		return 0;
579 	}
580 
581 	/* check expiry date and check if query-data matches */
582 	if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
583 		return 0;
584 	}
585 
586 	/* parse dns message into return_msg */
587 	if( !parse_data(qstate, qstate->env->scratch_buffer) ) {
588 		return 0;
589 	}
590 	return 1;
591 }
592 
593 /**
594  * Store the qstate.return_msg in extcache for key qstate.info
595  */
596 static void
597 cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
598 {
599 	char key[(CACHEDB_HASHSIZE/8)*2+1];
600 	calc_hash(qstate, key, sizeof(key));
601 
602 	/* prepare data in scratch buffer */
603 	if(!prep_data(qstate, qstate->env->scratch_buffer))
604 		return;
605 
606 	/* call backend */
607 	(*ie->backend->store)(qstate->env, ie, key,
608 		sldns_buffer_begin(qstate->env->scratch_buffer),
609 		sldns_buffer_limit(qstate->env->scratch_buffer));
610 }
611 
612 /**
613  * See if unbound's internal cache can answer the query
614  */
615 static int
616 cachedb_intcache_lookup(struct module_qstate* qstate)
617 {
618 	struct dns_msg* msg;
619 	msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname,
620 		qstate->qinfo.qname_len, qstate->qinfo.qtype,
621 		qstate->qinfo.qclass, qstate->query_flags,
622 		qstate->region, qstate->env->scratch,
623 		1 /* no partial messages with only a CNAME */
624 		);
625 	if(!msg && qstate->env->neg_cache &&
626 		iter_qname_indicates_dnssec(qstate->env, &qstate->qinfo)) {
627 		/* lookup in negative cache; may result in
628 		 * NOERROR/NODATA or NXDOMAIN answers that need validation */
629 		msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo,
630 			qstate->region, qstate->env->rrset_cache,
631 			qstate->env->scratch_buffer,
632 			*qstate->env->now, 1/*add SOA*/, NULL,
633 			qstate->env->cfg);
634 	}
635 	if(!msg)
636 		return 0;
637 	/* this is the returned msg */
638 	qstate->return_rcode = LDNS_RCODE_NOERROR;
639 	qstate->return_msg = msg;
640 	return 1;
641 }
642 
643 /**
644  * Store query into the internal cache of unbound.
645  */
646 static void
647 cachedb_intcache_store(struct module_qstate* qstate)
648 {
649 	uint32_t store_flags = qstate->query_flags;
650 
651 	if(qstate->env->cfg->serve_expired)
652 		store_flags |= DNSCACHE_STORE_ZEROTTL;
653 	if(!qstate->return_msg)
654 		return;
655 	(void)dns_cache_store(qstate->env, &qstate->qinfo,
656 		qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
657 		qstate->region, store_flags);
658 }
659 
660 /**
661  * Handle a cachedb module event with a query
662  * @param qstate: query state (from the mesh), passed between modules.
663  * 	contains qstate->env module environment with global caches and so on.
664  * @param iq: query state specific for this module.  per-query.
665  * @param ie: environment specific for this module.  global.
666  * @param id: module id.
667  */
668 static void
669 cachedb_handle_query(struct module_qstate* qstate,
670 	struct cachedb_qstate* ATTR_UNUSED(iq),
671 	struct cachedb_env* ie, int id)
672 {
673 	/* check if we are enabled, and skip if so */
674 	if(!ie->enabled) {
675 		/* pass request to next module */
676 		qstate->ext_state[id] = module_wait_module;
677 		return;
678 	}
679 
680 	if(qstate->blacklist || qstate->no_cache_lookup) {
681 		/* cache is blacklisted or we are instructed from edns to not look */
682 		/* pass request to next module */
683 		qstate->ext_state[id] = module_wait_module;
684 		return;
685 	}
686 
687 	/* lookup inside unbound's internal cache.
688 	 * This does not look for expired entries. */
689 	if(cachedb_intcache_lookup(qstate)) {
690 		if(verbosity >= VERB_ALGO) {
691 			if(qstate->return_msg->rep)
692 				log_dns_msg("cachedb internal cache lookup",
693 					&qstate->return_msg->qinfo,
694 					qstate->return_msg->rep);
695 			else log_info("cachedb internal cache lookup: rcode %s",
696 				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)
697 				?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name
698 				:"??");
699 		}
700 		/* we are done with the query */
701 		qstate->ext_state[id] = module_finished;
702 		return;
703 	}
704 
705 	/* ask backend cache to see if we have data */
706 	if(cachedb_extcache_lookup(qstate, ie)) {
707 		if(verbosity >= VERB_ALGO)
708 			log_dns_msg(ie->backend->name,
709 				&qstate->return_msg->qinfo,
710 				qstate->return_msg->rep);
711 		/* store this result in internal cache */
712 		cachedb_intcache_store(qstate);
713 		/* In case we have expired data but there is a client timer for expired
714 		 * answers, pass execution to next module in order to try updating the
715 		 * data first.
716 		 * TODO: this needs revisit. The expired data stored from cachedb has
717 		 * 0 TTL which is picked up by iterator later when looking in the cache.
718 		 * Document that ext cachedb does not work properly with
719 		 * serve_stale_reply_ttl yet. */
720 		if(qstate->need_refetch && qstate->serve_expired_data &&
721 			qstate->serve_expired_data->timer) {
722 				qstate->return_msg = NULL;
723 				qstate->ext_state[id] = module_wait_module;
724 				return;
725 		}
726 		/* we are done with the query */
727 		qstate->ext_state[id] = module_finished;
728 		return;
729 	}
730 
731 	/* no cache fetches */
732 	/* pass request to next module */
733 	qstate->ext_state[id] = module_wait_module;
734 }
735 
736 /**
737  * Handle a cachedb module event with a response from the iterator.
738  * @param qstate: query state (from the mesh), passed between modules.
739  * 	contains qstate->env module environment with global caches and so on.
740  * @param iq: query state specific for this module.  per-query.
741  * @param ie: environment specific for this module.  global.
742  * @param id: module id.
743  */
744 static void
745 cachedb_handle_response(struct module_qstate* qstate,
746 	struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
747 {
748 	/* check if we are not enabled or instructed to not cache, and skip */
749 	if(!ie->enabled || qstate->no_cache_store) {
750 		/* we are done with the query */
751 		qstate->ext_state[id] = module_finished;
752 		return;
753 	}
754 
755 	/* store the item into the backend cache */
756 	cachedb_extcache_store(qstate, ie);
757 
758 	/* we are done with the query */
759 	qstate->ext_state[id] = module_finished;
760 }
761 
762 void
763 cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
764 	struct outbound_entry* outbound)
765 {
766 	struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id];
767 	struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id];
768 	verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s",
769 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
770 	if(iq) log_query_info(VERB_QUERY, "cachedb operate: query",
771 		&qstate->qinfo);
772 
773 	/* perform cachedb state machine */
774 	if((event == module_event_new || event == module_event_pass) &&
775 		iq == NULL) {
776 		if(!cachedb_new(qstate, id)) {
777 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
778 			return;
779 		}
780 		iq = (struct cachedb_qstate*)qstate->minfo[id];
781 	}
782 	if(iq && (event == module_event_pass || event == module_event_new)) {
783 		cachedb_handle_query(qstate, iq, ie, id);
784 		return;
785 	}
786 	if(iq && (event == module_event_moddone)) {
787 		cachedb_handle_response(qstate, iq, ie, id);
788 		return;
789 	}
790 	if(iq && outbound) {
791 		/* cachedb does not need to process responses at this time
792 		 * ignore it.
793 		cachedb_process_response(qstate, iq, ie, id, outbound, event);
794 		*/
795 		return;
796 	}
797 	if(event == module_event_error) {
798 		verbose(VERB_ALGO, "got called with event error, giving up");
799 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
800 		return;
801 	}
802 	if(!iq && (event == module_event_moddone)) {
803 		/* during priming, module done but we never started */
804 		qstate->ext_state[id] = module_finished;
805 		return;
806 	}
807 
808 	log_err("bad event for cachedb");
809 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
810 }
811 
812 void
813 cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
814 	int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
815 {
816 	/* cachedb does not use subordinate requests at this time */
817 	verbose(VERB_ALGO, "cachedb inform_super was called");
818 }
819 
820 void
821 cachedb_clear(struct module_qstate* qstate, int id)
822 {
823 	struct cachedb_qstate* iq;
824 	if(!qstate)
825 		return;
826 	iq = (struct cachedb_qstate*)qstate->minfo[id];
827 	if(iq) {
828 		/* free contents of iq */
829 		/* TODO */
830 	}
831 	qstate->minfo[id] = NULL;
832 }
833 
834 size_t
835 cachedb_get_mem(struct module_env* env, int id)
836 {
837 	struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
838 	if(!ie)
839 		return 0;
840 	return sizeof(*ie); /* TODO - more mem */
841 }
842 
843 /**
844  * The cachedb function block
845  */
846 static struct module_func_block cachedb_block = {
847 	"cachedb",
848 	&cachedb_init, &cachedb_deinit, &cachedb_operate,
849 	&cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
850 };
851 
852 struct module_func_block*
853 cachedb_get_funcblock(void)
854 {
855 	return &cachedb_block;
856 }
857 #endif /* USE_CACHEDB */
858