xref: /freebsd/contrib/unbound/cachedb/cachedb.c (revision dd41de95a84d979615a2ef11df6850622bf6184e)
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, time_t ATTR_UNUSED(ttl))
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 	data->ttl_add = (subtract < data->ttl_add) ? (data->ttl_add - subtract) : 0;
469 }
470 
471 /* Adjust the TTL of a DNS message and its RRs by 'adjust'.  If 'adjust' is
472  * negative, set the TTLs to 0. */
473 static void
474 adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
475 {
476 	size_t i;
477 	if(adjust >= 0 && msg->rep->ttl > adjust)
478 		msg->rep->ttl -= adjust;
479 	else
480 		msg->rep->ttl = 0;
481 	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
482 	msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
483 
484 	for(i=0; i<msg->rep->rrset_count; i++) {
485 		packed_rrset_ttl_subtract((struct packed_rrset_data*)msg->
486 			rep->rrsets[i]->entry.data, adjust);
487 	}
488 }
489 
490 /** convert dns message in buffer to return_msg */
491 static int
492 parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
493 {
494 	struct msg_parse* prs;
495 	struct edns_data edns;
496 	uint64_t timestamp, expiry;
497 	time_t adjust;
498 	size_t lim = sldns_buffer_limit(buf);
499 	if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
500 		return 0; /* too short */
501 
502 	/* remove timestamp and expiry from end */
503 	sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry));
504 	sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp),
505 		&timestamp, sizeof(timestamp));
506 	expiry = be64toh(expiry);
507 	timestamp = be64toh(timestamp);
508 
509 	/* parse DNS packet */
510 	regional_free_all(qstate->env->scratch);
511 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
512 		sizeof(struct msg_parse));
513 	if(!prs)
514 		return 0; /* out of memory */
515 	memset(prs, 0, sizeof(*prs));
516 	memset(&edns, 0, sizeof(edns));
517 	sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp));
518 	if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
519 		sldns_buffer_set_limit(buf, lim);
520 		return 0;
521 	}
522 	if(parse_extract_edns(prs, &edns, qstate->env->scratch) !=
523 		LDNS_RCODE_NOERROR) {
524 		sldns_buffer_set_limit(buf, lim);
525 		return 0;
526 	}
527 
528 	qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
529 	sldns_buffer_set_limit(buf, lim);
530 	if(!qstate->return_msg)
531 		return 0;
532 
533 	qstate->return_rcode = LDNS_RCODE_NOERROR;
534 
535 	/* see how much of the TTL expired, and remove it */
536 	if(*qstate->env->now <= (time_t)timestamp) {
537 		verbose(VERB_ALGO, "cachedb msg adjust by zero");
538 		return 1; /* message from the future (clock skew?) */
539 	}
540 	adjust = *qstate->env->now - (time_t)timestamp;
541 	if(qstate->return_msg->rep->ttl < adjust) {
542 		verbose(VERB_ALGO, "cachedb msg expired");
543 		/* If serve-expired is enabled, we still use an expired message
544 		 * setting the TTL to 0. */
545 		if(qstate->env->cfg->serve_expired)
546 			adjust = -1;
547 		else
548 			return 0; /* message expired */
549 	}
550 	verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
551 	adjust_msg_ttl(qstate->return_msg, adjust);
552 
553 	/* Similar to the unbound worker, if serve-expired is enabled and
554 	 * the msg would be considered to be expired, mark the state so a
555 	 * refetch will be scheduled.  The comparison between 'expiry' and
556 	 * 'now' should be redundant given how these values were calculated,
557 	 * but we check it just in case as does good_expiry_and_qinfo(). */
558 	if(qstate->env->cfg->serve_expired &&
559 		(adjust == -1 || (time_t)expiry < *qstate->env->now)) {
560 		qstate->need_refetch = 1;
561 	}
562 
563 	return 1;
564 }
565 
566 /**
567  * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
568  * return true if lookup was successful.
569  */
570 static int
571 cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie)
572 {
573 	char key[(CACHEDB_HASHSIZE/8)*2+1];
574 	calc_hash(qstate, key, sizeof(key));
575 
576 	/* call backend to fetch data for key into scratch buffer */
577 	if( !(*ie->backend->lookup)(qstate->env, ie, key,
578 		qstate->env->scratch_buffer)) {
579 		return 0;
580 	}
581 
582 	/* check expiry date and check if query-data matches */
583 	if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
584 		return 0;
585 	}
586 
587 	/* parse dns message into return_msg */
588 	if( !parse_data(qstate, qstate->env->scratch_buffer) ) {
589 		return 0;
590 	}
591 	return 1;
592 }
593 
594 /**
595  * Store the qstate.return_msg in extcache for key qstate.info
596  */
597 static void
598 cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
599 {
600 	char key[(CACHEDB_HASHSIZE/8)*2+1];
601 	calc_hash(qstate, key, sizeof(key));
602 
603 	/* prepare data in scratch buffer */
604 	if(!prep_data(qstate, qstate->env->scratch_buffer))
605 		return;
606 
607 	/* call backend */
608 	(*ie->backend->store)(qstate->env, ie, key,
609 		sldns_buffer_begin(qstate->env->scratch_buffer),
610 		sldns_buffer_limit(qstate->env->scratch_buffer),
611 		qstate->return_msg->rep->ttl);
612 }
613 
614 /**
615  * See if unbound's internal cache can answer the query
616  */
617 static int
618 cachedb_intcache_lookup(struct module_qstate* qstate)
619 {
620 	struct dns_msg* msg;
621 	msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname,
622 		qstate->qinfo.qname_len, qstate->qinfo.qtype,
623 		qstate->qinfo.qclass, qstate->query_flags,
624 		qstate->region, qstate->env->scratch,
625 		1 /* no partial messages with only a CNAME */
626 		);
627 	if(!msg && qstate->env->neg_cache &&
628 		iter_qname_indicates_dnssec(qstate->env, &qstate->qinfo)) {
629 		/* lookup in negative cache; may result in
630 		 * NOERROR/NODATA or NXDOMAIN answers that need validation */
631 		msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo,
632 			qstate->region, qstate->env->rrset_cache,
633 			qstate->env->scratch_buffer,
634 			*qstate->env->now, 1/*add SOA*/, NULL,
635 			qstate->env->cfg);
636 	}
637 	if(!msg)
638 		return 0;
639 	/* this is the returned msg */
640 	qstate->return_rcode = LDNS_RCODE_NOERROR;
641 	qstate->return_msg = msg;
642 	return 1;
643 }
644 
645 /**
646  * Store query into the internal cache of unbound.
647  */
648 static void
649 cachedb_intcache_store(struct module_qstate* qstate)
650 {
651 	uint32_t store_flags = qstate->query_flags;
652 
653 	if(qstate->env->cfg->serve_expired)
654 		store_flags |= DNSCACHE_STORE_ZEROTTL;
655 	if(!qstate->return_msg)
656 		return;
657 	(void)dns_cache_store(qstate->env, &qstate->qinfo,
658 		qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
659 		qstate->region, store_flags);
660 }
661 
662 /**
663  * Handle a cachedb module event with a query
664  * @param qstate: query state (from the mesh), passed between modules.
665  * 	contains qstate->env module environment with global caches and so on.
666  * @param iq: query state specific for this module.  per-query.
667  * @param ie: environment specific for this module.  global.
668  * @param id: module id.
669  */
670 static void
671 cachedb_handle_query(struct module_qstate* qstate,
672 	struct cachedb_qstate* ATTR_UNUSED(iq),
673 	struct cachedb_env* ie, int id)
674 {
675 	/* check if we are enabled, and skip if so */
676 	if(!ie->enabled) {
677 		/* pass request to next module */
678 		qstate->ext_state[id] = module_wait_module;
679 		return;
680 	}
681 
682 	if(qstate->blacklist || qstate->no_cache_lookup) {
683 		/* cache is blacklisted or we are instructed from edns to not look */
684 		/* pass request to next module */
685 		qstate->ext_state[id] = module_wait_module;
686 		return;
687 	}
688 
689 	/* lookup inside unbound's internal cache.
690 	 * This does not look for expired entries. */
691 	if(cachedb_intcache_lookup(qstate)) {
692 		if(verbosity >= VERB_ALGO) {
693 			if(qstate->return_msg->rep)
694 				log_dns_msg("cachedb internal cache lookup",
695 					&qstate->return_msg->qinfo,
696 					qstate->return_msg->rep);
697 			else log_info("cachedb internal cache lookup: rcode %s",
698 				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)
699 				?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name
700 				:"??");
701 		}
702 		/* we are done with the query */
703 		qstate->ext_state[id] = module_finished;
704 		return;
705 	}
706 
707 	/* ask backend cache to see if we have data */
708 	if(cachedb_extcache_lookup(qstate, ie)) {
709 		if(verbosity >= VERB_ALGO)
710 			log_dns_msg(ie->backend->name,
711 				&qstate->return_msg->qinfo,
712 				qstate->return_msg->rep);
713 		/* store this result in internal cache */
714 		cachedb_intcache_store(qstate);
715 		/* In case we have expired data but there is a client timer for expired
716 		 * answers, pass execution to next module in order to try updating the
717 		 * data first.
718 		 * TODO: this needs revisit. The expired data stored from cachedb has
719 		 * 0 TTL which is picked up by iterator later when looking in the cache.
720 		 * Document that ext cachedb does not work properly with
721 		 * serve_stale_reply_ttl yet. */
722 		if(qstate->need_refetch && qstate->serve_expired_data &&
723 			qstate->serve_expired_data->timer) {
724 				qstate->return_msg = NULL;
725 				qstate->ext_state[id] = module_wait_module;
726 				return;
727 		}
728 		/* we are done with the query */
729 		qstate->ext_state[id] = module_finished;
730 		return;
731 	}
732 
733 	/* no cache fetches */
734 	/* pass request to next module */
735 	qstate->ext_state[id] = module_wait_module;
736 }
737 
738 /**
739  * Handle a cachedb module event with a response from the iterator.
740  * @param qstate: query state (from the mesh), passed between modules.
741  * 	contains qstate->env module environment with global caches and so on.
742  * @param iq: query state specific for this module.  per-query.
743  * @param ie: environment specific for this module.  global.
744  * @param id: module id.
745  */
746 static void
747 cachedb_handle_response(struct module_qstate* qstate,
748 	struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
749 {
750 	/* check if we are not enabled or instructed to not cache, and skip */
751 	if(!ie->enabled || qstate->no_cache_store) {
752 		/* we are done with the query */
753 		qstate->ext_state[id] = module_finished;
754 		return;
755 	}
756 
757 	/* store the item into the backend cache */
758 	cachedb_extcache_store(qstate, ie);
759 
760 	/* we are done with the query */
761 	qstate->ext_state[id] = module_finished;
762 }
763 
764 void
765 cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
766 	struct outbound_entry* outbound)
767 {
768 	struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id];
769 	struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id];
770 	verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s",
771 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
772 	if(iq) log_query_info(VERB_QUERY, "cachedb operate: query",
773 		&qstate->qinfo);
774 
775 	/* perform cachedb state machine */
776 	if((event == module_event_new || event == module_event_pass) &&
777 		iq == NULL) {
778 		if(!cachedb_new(qstate, id)) {
779 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
780 			return;
781 		}
782 		iq = (struct cachedb_qstate*)qstate->minfo[id];
783 	}
784 	if(iq && (event == module_event_pass || event == module_event_new)) {
785 		cachedb_handle_query(qstate, iq, ie, id);
786 		return;
787 	}
788 	if(iq && (event == module_event_moddone)) {
789 		cachedb_handle_response(qstate, iq, ie, id);
790 		return;
791 	}
792 	if(iq && outbound) {
793 		/* cachedb does not need to process responses at this time
794 		 * ignore it.
795 		cachedb_process_response(qstate, iq, ie, id, outbound, event);
796 		*/
797 		return;
798 	}
799 	if(event == module_event_error) {
800 		verbose(VERB_ALGO, "got called with event error, giving up");
801 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
802 		return;
803 	}
804 	if(!iq && (event == module_event_moddone)) {
805 		/* during priming, module done but we never started */
806 		qstate->ext_state[id] = module_finished;
807 		return;
808 	}
809 
810 	log_err("bad event for cachedb");
811 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
812 }
813 
814 void
815 cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
816 	int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
817 {
818 	/* cachedb does not use subordinate requests at this time */
819 	verbose(VERB_ALGO, "cachedb inform_super was called");
820 }
821 
822 void
823 cachedb_clear(struct module_qstate* qstate, int id)
824 {
825 	struct cachedb_qstate* iq;
826 	if(!qstate)
827 		return;
828 	iq = (struct cachedb_qstate*)qstate->minfo[id];
829 	if(iq) {
830 		/* free contents of iq */
831 		/* TODO */
832 	}
833 	qstate->minfo[id] = NULL;
834 }
835 
836 size_t
837 cachedb_get_mem(struct module_env* env, int id)
838 {
839 	struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
840 	if(!ie)
841 		return 0;
842 	return sizeof(*ie); /* TODO - more mem */
843 }
844 
845 /**
846  * The cachedb function block
847  */
848 static struct module_func_block cachedb_block = {
849 	"cachedb",
850 	&cachedb_init, &cachedb_deinit, &cachedb_operate,
851 	&cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
852 };
853 
854 struct module_func_block*
855 cachedb_get_funcblock(void)
856 {
857 	return &cachedb_block;
858 }
859 #endif /* USE_CACHEDB */
860