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