xref: /freebsd/contrib/unbound/cachedb/cachedb.c (revision 24e365220007c415f495cf8dcb228ece6002b8b7)
1e2d15004SDag-Erling Smørgrav /*
2e2d15004SDag-Erling Smørgrav  * cachedb/cachedb.c - cache from a database external to the program module
3e2d15004SDag-Erling Smørgrav  *
4e2d15004SDag-Erling Smørgrav  * Copyright (c) 2016, NLnet Labs. All rights reserved.
5e2d15004SDag-Erling Smørgrav  *
6e2d15004SDag-Erling Smørgrav  * This software is open source.
7e2d15004SDag-Erling Smørgrav  *
8e2d15004SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9e2d15004SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10e2d15004SDag-Erling Smørgrav  * are met:
11e2d15004SDag-Erling Smørgrav  *
12e2d15004SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13e2d15004SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14e2d15004SDag-Erling Smørgrav  *
15e2d15004SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16e2d15004SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17e2d15004SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18e2d15004SDag-Erling Smørgrav  *
19e2d15004SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20e2d15004SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21e2d15004SDag-Erling Smørgrav  * specific prior written permission.
22e2d15004SDag-Erling Smørgrav  *
23e2d15004SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24e2d15004SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25e2d15004SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26e2d15004SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27e2d15004SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28e2d15004SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29e2d15004SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30e2d15004SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31e2d15004SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32e2d15004SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33e2d15004SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34e2d15004SDag-Erling Smørgrav  */
35e2d15004SDag-Erling Smørgrav 
36e2d15004SDag-Erling Smørgrav /**
37e2d15004SDag-Erling Smørgrav  * \file
38e2d15004SDag-Erling Smørgrav  *
39e2d15004SDag-Erling Smørgrav  * This file contains a module that uses an external database to cache
40e2d15004SDag-Erling Smørgrav  * dns responses.
41e2d15004SDag-Erling Smørgrav  */
42e2d15004SDag-Erling Smørgrav 
43e2d15004SDag-Erling Smørgrav #include "config.h"
44e2d15004SDag-Erling Smørgrav #ifdef USE_CACHEDB
45e2d15004SDag-Erling Smørgrav #include "cachedb/cachedb.h"
460fb34990SDag-Erling Smørgrav #include "cachedb/redis.h"
47e2d15004SDag-Erling Smørgrav #include "util/regional.h"
48e2d15004SDag-Erling Smørgrav #include "util/net_help.h"
49e2d15004SDag-Erling Smørgrav #include "util/config_file.h"
50e2d15004SDag-Erling Smørgrav #include "util/data/msgreply.h"
51e2d15004SDag-Erling Smørgrav #include "util/data/msgencode.h"
52e2d15004SDag-Erling Smørgrav #include "services/cache/dns.h"
53e2d15004SDag-Erling Smørgrav #include "validator/val_neg.h"
54e2d15004SDag-Erling Smørgrav #include "validator/val_secalgo.h"
55e2d15004SDag-Erling Smørgrav #include "iterator/iter_utils.h"
56e2d15004SDag-Erling Smørgrav #include "sldns/parseutil.h"
57e2d15004SDag-Erling Smørgrav #include "sldns/wire2str.h"
58e2d15004SDag-Erling Smørgrav #include "sldns/sbuffer.h"
59e2d15004SDag-Erling Smørgrav 
600fb34990SDag-Erling Smørgrav /* header file for htobe64 */
610fb34990SDag-Erling Smørgrav #ifdef HAVE_ENDIAN_H
620fb34990SDag-Erling Smørgrav #  include <endian.h>
630fb34990SDag-Erling Smørgrav #endif
640fb34990SDag-Erling Smørgrav #ifdef HAVE_SYS_ENDIAN_H
650fb34990SDag-Erling Smørgrav #  include <sys/endian.h>
660fb34990SDag-Erling Smørgrav #endif
67091e9e46SCy Schubert 
68091e9e46SCy Schubert #ifndef HAVE_HTOBE64
690fb34990SDag-Erling Smørgrav #  ifdef HAVE_LIBKERN_OSBYTEORDER_H
700fb34990SDag-Erling Smørgrav      /* In practice this is specific to MacOS X.  We assume it doesn't have
710fb34990SDag-Erling Smørgrav       * htobe64/be64toh but has alternatives with a different name. */
720fb34990SDag-Erling Smørgrav #    include <libkern/OSByteOrder.h>
730fb34990SDag-Erling Smørgrav #    define htobe64(x) OSSwapHostToBigInt64(x)
740fb34990SDag-Erling Smørgrav #    define be64toh(x) OSSwapBigToHostInt64(x)
75091e9e46SCy Schubert #  else
76091e9e46SCy Schubert      /* not OSX */
77e86b9096SDag-Erling Smørgrav      /* Some compilers do not define __BYTE_ORDER__, like IBM XLC on AIX */
78e86b9096SDag-Erling Smørgrav #    if __BIG_ENDIAN__
79e86b9096SDag-Erling Smørgrav #      define be64toh(n) (n)
80e86b9096SDag-Erling Smørgrav #      define htobe64(n) (n)
81e86b9096SDag-Erling Smørgrav #    else
82e86b9096SDag-Erling Smørgrav #      define be64toh(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
83e86b9096SDag-Erling Smørgrav #      define htobe64(n) (((uint64_t)htonl((n) & 0xFFFFFFFF) << 32) | htonl((n) >> 32))
84091e9e46SCy Schubert #    endif /* _ENDIAN */
85091e9e46SCy Schubert #  endif /* HAVE_LIBKERN_OSBYTEORDER_H */
86091e9e46SCy Schubert #endif /* HAVE_BE64TOH */
87e86b9096SDag-Erling Smørgrav 
88e2d15004SDag-Erling Smørgrav /** the unit test testframe for cachedb, its module state contains
89e2d15004SDag-Erling Smørgrav  * a cache for a couple queries (in memory). */
90e2d15004SDag-Erling Smørgrav struct testframe_moddata {
91971980c3SDag-Erling Smørgrav 	/** lock for mutex */
92971980c3SDag-Erling Smørgrav 	lock_basic_type lock;
93e2d15004SDag-Erling Smørgrav 	/** key for single stored data element, NULL if none */
94e2d15004SDag-Erling Smørgrav 	char* stored_key;
95e2d15004SDag-Erling Smørgrav 	/** data for single stored data element, NULL if none */
96e2d15004SDag-Erling Smørgrav 	uint8_t* stored_data;
97e2d15004SDag-Erling Smørgrav 	/** length of stored data */
98e2d15004SDag-Erling Smørgrav 	size_t stored_datalen;
99e2d15004SDag-Erling Smørgrav };
100e2d15004SDag-Erling Smørgrav 
101e2d15004SDag-Erling Smørgrav static int
102e2d15004SDag-Erling Smørgrav testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
103e2d15004SDag-Erling Smørgrav {
104971980c3SDag-Erling Smørgrav 	struct testframe_moddata* d;
105e2d15004SDag-Erling Smørgrav 	(void)env;
106e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_init");
107971980c3SDag-Erling Smørgrav 	d = (struct testframe_moddata*)calloc(1,
108e2d15004SDag-Erling Smørgrav 		sizeof(struct testframe_moddata));
109971980c3SDag-Erling Smørgrav 	cachedb_env->backend_data = (void*)d;
110e2d15004SDag-Erling Smørgrav 	if(!cachedb_env->backend_data) {
111e2d15004SDag-Erling Smørgrav 		log_err("out of memory");
112e2d15004SDag-Erling Smørgrav 		return 0;
113e2d15004SDag-Erling Smørgrav 	}
114971980c3SDag-Erling Smørgrav 	lock_basic_init(&d->lock);
115971980c3SDag-Erling Smørgrav 	lock_protect(&d->lock, d, sizeof(*d));
116e2d15004SDag-Erling Smørgrav 	return 1;
117e2d15004SDag-Erling Smørgrav }
118e2d15004SDag-Erling Smørgrav 
119e2d15004SDag-Erling Smørgrav static void
120e2d15004SDag-Erling Smørgrav testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
121e2d15004SDag-Erling Smørgrav {
122e2d15004SDag-Erling Smørgrav 	struct testframe_moddata* d = (struct testframe_moddata*)
123e2d15004SDag-Erling Smørgrav 		cachedb_env->backend_data;
124e2d15004SDag-Erling Smørgrav 	(void)env;
125e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_deinit");
126e2d15004SDag-Erling Smørgrav 	if(!d)
127e2d15004SDag-Erling Smørgrav 		return;
128971980c3SDag-Erling Smørgrav 	lock_basic_destroy(&d->lock);
129e2d15004SDag-Erling Smørgrav 	free(d->stored_key);
130e2d15004SDag-Erling Smørgrav 	free(d->stored_data);
131e2d15004SDag-Erling Smørgrav 	free(d);
132e2d15004SDag-Erling Smørgrav }
133e2d15004SDag-Erling Smørgrav 
134e2d15004SDag-Erling Smørgrav static int
135e2d15004SDag-Erling Smørgrav testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
136e2d15004SDag-Erling Smørgrav 	char* key, struct sldns_buffer* result_buffer)
137e2d15004SDag-Erling Smørgrav {
138e2d15004SDag-Erling Smørgrav 	struct testframe_moddata* d = (struct testframe_moddata*)
139e2d15004SDag-Erling Smørgrav 		cachedb_env->backend_data;
140e2d15004SDag-Erling Smørgrav 	(void)env;
141e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_lookup of %s", key);
142971980c3SDag-Erling Smørgrav 	lock_basic_lock(&d->lock);
143e2d15004SDag-Erling Smørgrav 	if(d->stored_key && strcmp(d->stored_key, key) == 0) {
144971980c3SDag-Erling Smørgrav 		if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) {
145971980c3SDag-Erling Smørgrav 			lock_basic_unlock(&d->lock);
146e2d15004SDag-Erling Smørgrav 			return 0; /* too large */
147971980c3SDag-Erling Smørgrav 		}
148e2d15004SDag-Erling Smørgrav 		verbose(VERB_ALGO, "testframe_lookup found %d bytes",
149e2d15004SDag-Erling Smørgrav 			(int)d->stored_datalen);
150e2d15004SDag-Erling Smørgrav 		sldns_buffer_clear(result_buffer);
151e2d15004SDag-Erling Smørgrav 		sldns_buffer_write(result_buffer, d->stored_data,
152e2d15004SDag-Erling Smørgrav 			d->stored_datalen);
153e2d15004SDag-Erling Smørgrav 		sldns_buffer_flip(result_buffer);
154971980c3SDag-Erling Smørgrav 		lock_basic_unlock(&d->lock);
155e2d15004SDag-Erling Smørgrav 		return 1;
156e2d15004SDag-Erling Smørgrav 	}
157971980c3SDag-Erling Smørgrav 	lock_basic_unlock(&d->lock);
158e2d15004SDag-Erling Smørgrav 	return 0;
159e2d15004SDag-Erling Smørgrav }
160e2d15004SDag-Erling Smørgrav 
161e2d15004SDag-Erling Smørgrav static void
162e2d15004SDag-Erling Smørgrav testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
16325039b37SCy Schubert 	char* key, uint8_t* data, size_t data_len, time_t ATTR_UNUSED(ttl))
164e2d15004SDag-Erling Smørgrav {
165e2d15004SDag-Erling Smørgrav 	struct testframe_moddata* d = (struct testframe_moddata*)
166e2d15004SDag-Erling Smørgrav 		cachedb_env->backend_data;
167e2d15004SDag-Erling Smørgrav 	(void)env;
168971980c3SDag-Erling Smørgrav 	lock_basic_lock(&d->lock);
169e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
170e2d15004SDag-Erling Smørgrav 
171e2d15004SDag-Erling Smørgrav 	/* free old data element (if any) */
172e2d15004SDag-Erling Smørgrav 	free(d->stored_key);
173e2d15004SDag-Erling Smørgrav 	d->stored_key = NULL;
174e2d15004SDag-Erling Smørgrav 	free(d->stored_data);
175e2d15004SDag-Erling Smørgrav 	d->stored_data = NULL;
176e2d15004SDag-Erling Smørgrav 	d->stored_datalen = 0;
177e2d15004SDag-Erling Smørgrav 
178e2d15004SDag-Erling Smørgrav 	d->stored_data = memdup(data, data_len);
179e2d15004SDag-Erling Smørgrav 	if(!d->stored_data) {
180971980c3SDag-Erling Smørgrav 		lock_basic_unlock(&d->lock);
181e2d15004SDag-Erling Smørgrav 		log_err("out of memory");
182e2d15004SDag-Erling Smørgrav 		return;
183e2d15004SDag-Erling Smørgrav 	}
184e2d15004SDag-Erling Smørgrav 	d->stored_datalen = data_len;
185e2d15004SDag-Erling Smørgrav 	d->stored_key = strdup(key);
186e2d15004SDag-Erling Smørgrav 	if(!d->stored_key) {
187e2d15004SDag-Erling Smørgrav 		free(d->stored_data);
188e2d15004SDag-Erling Smørgrav 		d->stored_data = NULL;
189e2d15004SDag-Erling Smørgrav 		d->stored_datalen = 0;
190971980c3SDag-Erling Smørgrav 		lock_basic_unlock(&d->lock);
191e2d15004SDag-Erling Smørgrav 		return;
192e2d15004SDag-Erling Smørgrav 	}
193971980c3SDag-Erling Smørgrav 	lock_basic_unlock(&d->lock);
194e2d15004SDag-Erling Smørgrav 	/* (key,data) successfully stored */
195e2d15004SDag-Erling Smørgrav }
196e2d15004SDag-Erling Smørgrav 
197e2d15004SDag-Erling Smørgrav /** The testframe backend is for unit tests */
198e2d15004SDag-Erling Smørgrav static struct cachedb_backend testframe_backend = { "testframe",
199e2d15004SDag-Erling Smørgrav 	testframe_init, testframe_deinit, testframe_lookup, testframe_store
200e2d15004SDag-Erling Smørgrav };
201e2d15004SDag-Erling Smørgrav 
202e2d15004SDag-Erling Smørgrav /** find a particular backend from possible backends */
203e2d15004SDag-Erling Smørgrav static struct cachedb_backend*
204e2d15004SDag-Erling Smørgrav cachedb_find_backend(const char* str)
205e2d15004SDag-Erling Smørgrav {
2060fb34990SDag-Erling Smørgrav #ifdef USE_REDIS
2070fb34990SDag-Erling Smørgrav 	if(strcmp(str, redis_backend.name) == 0)
2080fb34990SDag-Erling Smørgrav 		return &redis_backend;
2090fb34990SDag-Erling Smørgrav #endif
210e2d15004SDag-Erling Smørgrav 	if(strcmp(str, testframe_backend.name) == 0)
211e2d15004SDag-Erling Smørgrav 		return &testframe_backend;
212e2d15004SDag-Erling Smørgrav 	/* TODO add more backends here */
213e2d15004SDag-Erling Smørgrav 	return NULL;
214e2d15004SDag-Erling Smørgrav }
215e2d15004SDag-Erling Smørgrav 
216e2d15004SDag-Erling Smørgrav /** apply configuration to cachedb module 'global' state */
217e2d15004SDag-Erling Smørgrav static int
218e2d15004SDag-Erling Smørgrav cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
219e2d15004SDag-Erling Smørgrav {
220971980c3SDag-Erling Smørgrav 	const char* backend_str = cfg->cachedb_backend;
221e2d15004SDag-Erling Smørgrav 	cachedb_env->backend = cachedb_find_backend(backend_str);
222e2d15004SDag-Erling Smørgrav 	if(!cachedb_env->backend) {
223971980c3SDag-Erling Smørgrav 		log_err("cachedb: cannot find backend name '%s'", backend_str);
224c7f4d7adSDag-Erling Smørgrav 		return 0;
225e2d15004SDag-Erling Smørgrav 	}
226971980c3SDag-Erling Smørgrav 
227e2d15004SDag-Erling Smørgrav 	/* TODO see if more configuration needs to be applied or not */
228e2d15004SDag-Erling Smørgrav 	return 1;
229e2d15004SDag-Erling Smørgrav }
230e2d15004SDag-Erling Smørgrav 
231e2d15004SDag-Erling Smørgrav int
232e2d15004SDag-Erling Smørgrav cachedb_init(struct module_env* env, int id)
233e2d15004SDag-Erling Smørgrav {
234e2d15004SDag-Erling Smørgrav 	struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1,
235e2d15004SDag-Erling Smørgrav 		sizeof(struct cachedb_env));
236e2d15004SDag-Erling Smørgrav 	if(!cachedb_env) {
237e2d15004SDag-Erling Smørgrav 		log_err("malloc failure");
238e2d15004SDag-Erling Smørgrav 		return 0;
239e2d15004SDag-Erling Smørgrav 	}
240e2d15004SDag-Erling Smørgrav 	env->modinfo[id] = (void*)cachedb_env;
241e2d15004SDag-Erling Smørgrav 	if(!cachedb_apply_cfg(cachedb_env, env->cfg)) {
242e2d15004SDag-Erling Smørgrav 		log_err("cachedb: could not apply configuration settings.");
2430eefd307SCy Schubert 		free(cachedb_env);
2440eefd307SCy Schubert 		env->modinfo[id] = NULL;
245e2d15004SDag-Erling Smørgrav 		return 0;
246e2d15004SDag-Erling Smørgrav 	}
247e2d15004SDag-Erling Smørgrav 	/* see if a backend is selected */
248e2d15004SDag-Erling Smørgrav 	if(!cachedb_env->backend || !cachedb_env->backend->name)
249e2d15004SDag-Erling Smørgrav 		return 1;
250e2d15004SDag-Erling Smørgrav 	if(!(*cachedb_env->backend->init)(env, cachedb_env)) {
251e2d15004SDag-Erling Smørgrav 		log_err("cachedb: could not init %s backend",
252e2d15004SDag-Erling Smørgrav 			cachedb_env->backend->name);
2530eefd307SCy Schubert 		free(cachedb_env);
2540eefd307SCy Schubert 		env->modinfo[id] = NULL;
255e2d15004SDag-Erling Smørgrav 		return 0;
256e2d15004SDag-Erling Smørgrav 	}
257e2d15004SDag-Erling Smørgrav 	cachedb_env->enabled = 1;
258091e9e46SCy Schubert 	if(env->cfg->serve_expired_reply_ttl)
259091e9e46SCy Schubert 		log_warn(
260091e9e46SCy Schubert 			"cachedb: serve-expired-reply-ttl is set but not working for data "
261091e9e46SCy Schubert 			"originating from the external cache; 0 TLL is used for those.");
262091e9e46SCy Schubert 	if(env->cfg->serve_expired_client_timeout)
263091e9e46SCy Schubert 		log_warn(
264091e9e46SCy Schubert 			"cachedb: serve-expired-client-timeout is set but not working for "
265091e9e46SCy Schubert 			"data originating from the external cache; expired data are used "
266091e9e46SCy Schubert 			"in the reply without first trying to refresh the data.");
267e2d15004SDag-Erling Smørgrav 	return 1;
268e2d15004SDag-Erling Smørgrav }
269e2d15004SDag-Erling Smørgrav 
270e2d15004SDag-Erling Smørgrav void
271e2d15004SDag-Erling Smørgrav cachedb_deinit(struct module_env* env, int id)
272e2d15004SDag-Erling Smørgrav {
273e2d15004SDag-Erling Smørgrav 	struct cachedb_env* cachedb_env;
274e2d15004SDag-Erling Smørgrav 	if(!env || !env->modinfo[id])
275e2d15004SDag-Erling Smørgrav 		return;
276e2d15004SDag-Erling Smørgrav 	cachedb_env = (struct cachedb_env*)env->modinfo[id];
277e2d15004SDag-Erling Smørgrav 	/* free contents */
278e2d15004SDag-Erling Smørgrav 	/* TODO */
279e2d15004SDag-Erling Smørgrav 	if(cachedb_env->enabled) {
280e2d15004SDag-Erling Smørgrav 		(*cachedb_env->backend->deinit)(env, cachedb_env);
281e2d15004SDag-Erling Smørgrav 	}
282e2d15004SDag-Erling Smørgrav 
283e2d15004SDag-Erling Smørgrav 	free(cachedb_env);
284e2d15004SDag-Erling Smørgrav 	env->modinfo[id] = NULL;
285e2d15004SDag-Erling Smørgrav }
286e2d15004SDag-Erling Smørgrav 
287e2d15004SDag-Erling Smørgrav /** new query for cachedb */
288e2d15004SDag-Erling Smørgrav static int
289e2d15004SDag-Erling Smørgrav cachedb_new(struct module_qstate* qstate, int id)
290e2d15004SDag-Erling Smørgrav {
291e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc(
292e2d15004SDag-Erling Smørgrav 		qstate->region, sizeof(struct cachedb_qstate));
293e2d15004SDag-Erling Smørgrav 	qstate->minfo[id] = iq;
294e2d15004SDag-Erling Smørgrav 	if(!iq)
295e2d15004SDag-Erling Smørgrav 		return 0;
296e2d15004SDag-Erling Smørgrav 	memset(iq, 0, sizeof(*iq));
297e2d15004SDag-Erling Smørgrav 	/* initialise it */
298e2d15004SDag-Erling Smørgrav 	/* TODO */
299e2d15004SDag-Erling Smørgrav 
300e2d15004SDag-Erling Smørgrav 	return 1;
301e2d15004SDag-Erling Smørgrav }
302e2d15004SDag-Erling Smørgrav 
303e2d15004SDag-Erling Smørgrav /**
304e2d15004SDag-Erling Smørgrav  * Return an error
305e2d15004SDag-Erling Smørgrav  * @param qstate: our query state
306e2d15004SDag-Erling Smørgrav  * @param id: module id
307e2d15004SDag-Erling Smørgrav  * @param rcode: error code (DNS errcode).
308e2d15004SDag-Erling Smørgrav  * @return: 0 for use by caller, to make notation easy, like:
309e2d15004SDag-Erling Smørgrav  * 	return error_response(..).
310e2d15004SDag-Erling Smørgrav  */
311e2d15004SDag-Erling Smørgrav static int
312e2d15004SDag-Erling Smørgrav error_response(struct module_qstate* qstate, int id, int rcode)
313e2d15004SDag-Erling Smørgrav {
314e2d15004SDag-Erling Smørgrav 	verbose(VERB_QUERY, "return error response %s",
315e2d15004SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rcodes, rcode)?
316e2d15004SDag-Erling Smørgrav 		sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
317e2d15004SDag-Erling Smørgrav 	qstate->return_rcode = rcode;
318e2d15004SDag-Erling Smørgrav 	qstate->return_msg = NULL;
319e2d15004SDag-Erling Smørgrav 	qstate->ext_state[id] = module_finished;
320e2d15004SDag-Erling Smørgrav 	return 0;
321e2d15004SDag-Erling Smørgrav }
322e2d15004SDag-Erling Smørgrav 
323e2d15004SDag-Erling Smørgrav /**
324e2d15004SDag-Erling Smørgrav  * Hash the query name, type, class and dbacess-secret into lookup buffer.
325e2d15004SDag-Erling Smørgrav  * @param qstate: query state with query info
326e2d15004SDag-Erling Smørgrav  * 	and env->cfg with secret.
327e2d15004SDag-Erling Smørgrav  * @param buf: returned buffer with hash to lookup
328e2d15004SDag-Erling Smørgrav  * @param len: length of the buffer.
329e2d15004SDag-Erling Smørgrav  */
330e2d15004SDag-Erling Smørgrav static void
331e2d15004SDag-Erling Smørgrav calc_hash(struct module_qstate* qstate, char* buf, size_t len)
332e2d15004SDag-Erling Smørgrav {
333e2d15004SDag-Erling Smørgrav 	uint8_t clear[1024];
334e2d15004SDag-Erling Smørgrav 	size_t clen = 0;
335e2d15004SDag-Erling Smørgrav 	uint8_t hash[CACHEDB_HASHSIZE/8];
336e2d15004SDag-Erling Smørgrav 	const char* hex = "0123456789ABCDEF";
337091e9e46SCy Schubert 	const char* secret = qstate->env->cfg->cachedb_secret;
338e2d15004SDag-Erling Smørgrav 	size_t i;
339e2d15004SDag-Erling Smørgrav 
340e2d15004SDag-Erling Smørgrav 	/* copy the hash info into the clear buffer */
341e2d15004SDag-Erling Smørgrav 	if(clen + qstate->qinfo.qname_len < sizeof(clear)) {
342e2d15004SDag-Erling Smørgrav 		memmove(clear+clen, qstate->qinfo.qname,
343e2d15004SDag-Erling Smørgrav 			qstate->qinfo.qname_len);
344e2d15004SDag-Erling Smørgrav 		clen += qstate->qinfo.qname_len;
345e2d15004SDag-Erling Smørgrav 	}
346e2d15004SDag-Erling Smørgrav 	if(clen + 4 < sizeof(clear)) {
347e2d15004SDag-Erling Smørgrav 		uint16_t t = htons(qstate->qinfo.qtype);
348e2d15004SDag-Erling Smørgrav 		uint16_t c = htons(qstate->qinfo.qclass);
349e2d15004SDag-Erling Smørgrav 		memmove(clear+clen, &t, 2);
350e2d15004SDag-Erling Smørgrav 		memmove(clear+clen+2, &c, 2);
351e2d15004SDag-Erling Smørgrav 		clen += 4;
352e2d15004SDag-Erling Smørgrav 	}
353e2d15004SDag-Erling Smørgrav 	if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) {
354e2d15004SDag-Erling Smørgrav 		memmove(clear+clen, secret, strlen(secret));
355e2d15004SDag-Erling Smørgrav 		clen += strlen(secret);
356e2d15004SDag-Erling Smørgrav 	}
357e2d15004SDag-Erling Smørgrav 
358e2d15004SDag-Erling Smørgrav 	/* hash the buffer */
359e2d15004SDag-Erling Smørgrav 	secalgo_hash_sha256(clear, clen, hash);
3600eefd307SCy Schubert #ifdef HAVE_EXPLICIT_BZERO
3610eefd307SCy Schubert 	explicit_bzero(clear, clen);
3620eefd307SCy Schubert #else
363e2d15004SDag-Erling Smørgrav 	memset(clear, 0, clen);
3640eefd307SCy Schubert #endif
365e2d15004SDag-Erling Smørgrav 
366e2d15004SDag-Erling Smørgrav 	/* hex encode output for portability (some online dbs need
367e2d15004SDag-Erling Smørgrav 	 * no nulls, no control characters, and so on) */
368e2d15004SDag-Erling Smørgrav 	log_assert(len >= sizeof(hash)*2 + 1);
369e2d15004SDag-Erling Smørgrav 	(void)len;
370e2d15004SDag-Erling Smørgrav 	for(i=0; i<sizeof(hash); i++) {
371e2d15004SDag-Erling Smørgrav 		buf[i*2] = hex[(hash[i]&0xf0)>>4];
372e2d15004SDag-Erling Smørgrav 		buf[i*2+1] = hex[hash[i]&0x0f];
373e2d15004SDag-Erling Smørgrav 	}
374e2d15004SDag-Erling Smørgrav 	buf[sizeof(hash)*2] = 0;
375e2d15004SDag-Erling Smørgrav }
376e2d15004SDag-Erling Smørgrav 
377e2d15004SDag-Erling Smørgrav /** convert data from return_msg into the data buffer */
378e2d15004SDag-Erling Smørgrav static int
379e2d15004SDag-Erling Smørgrav prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
380e2d15004SDag-Erling Smørgrav {
381e2d15004SDag-Erling Smørgrav 	uint64_t timestamp, expiry;
382e2d15004SDag-Erling Smørgrav 	size_t oldlim;
383e2d15004SDag-Erling Smørgrav 	struct edns_data edns;
384e2d15004SDag-Erling Smørgrav 	memset(&edns, 0, sizeof(edns));
385e2d15004SDag-Erling Smørgrav 	edns.edns_present = 1;
386e2d15004SDag-Erling Smørgrav 	edns.bits = EDNS_DO;
387e2d15004SDag-Erling Smørgrav 	edns.ext_rcode = 0;
388e2d15004SDag-Erling Smørgrav 	edns.edns_version = EDNS_ADVERTISED_VERSION;
389e2d15004SDag-Erling Smørgrav 	edns.udp_size = EDNS_ADVERTISED_SIZE;
390e2d15004SDag-Erling Smørgrav 
391e2d15004SDag-Erling Smørgrav 	if(!qstate->return_msg || !qstate->return_msg->rep)
392e2d15004SDag-Erling Smørgrav 		return 0;
3938a384985SDag-Erling Smørgrav 	/* We don't store the reply if its TTL is 0 unless serve-expired is
3948a384985SDag-Erling Smørgrav 	 * enabled.  Such a reply won't be reusable and simply be a waste for
3958a384985SDag-Erling Smørgrav 	 * the backend.  It's also compatible with the default behavior of
3968a384985SDag-Erling Smørgrav 	 * dns_cache_store_msg(). */
3978a384985SDag-Erling Smørgrav 	if(qstate->return_msg->rep->ttl == 0 &&
3988a384985SDag-Erling Smørgrav 		!qstate->env->cfg->serve_expired)
3998a384985SDag-Erling Smørgrav 		return 0;
400e2d15004SDag-Erling Smørgrav 	if(verbosity >= VERB_ALGO)
401e2d15004SDag-Erling Smørgrav 		log_dns_msg("cachedb encoding", &qstate->return_msg->qinfo,
402e2d15004SDag-Erling Smørgrav 	                qstate->return_msg->rep);
403e2d15004SDag-Erling Smørgrav 	if(!reply_info_answer_encode(&qstate->return_msg->qinfo,
404e2d15004SDag-Erling Smørgrav 		qstate->return_msg->rep, 0, qstate->query_flags,
405e2d15004SDag-Erling Smørgrav 		buf, 0, 1, qstate->env->scratch, 65535, &edns, 1, 0))
406e2d15004SDag-Erling Smørgrav 		return 0;
407e2d15004SDag-Erling Smørgrav 
408e2d15004SDag-Erling Smørgrav 	/* TTLs in the return_msg are relative to time(0) so we have to
409e2d15004SDag-Erling Smørgrav 	 * store that, we also store the smallest ttl in the packet+time(0)
410e2d15004SDag-Erling Smørgrav 	 * as the packet expiry time */
411e2d15004SDag-Erling Smørgrav 	/* qstate->return_msg->rep->ttl contains that relative shortest ttl */
412e2d15004SDag-Erling Smørgrav 	timestamp = (uint64_t)*qstate->env->now;
413e2d15004SDag-Erling Smørgrav 	expiry = timestamp + (uint64_t)qstate->return_msg->rep->ttl;
414e2d15004SDag-Erling Smørgrav 	timestamp = htobe64(timestamp);
415e2d15004SDag-Erling Smørgrav 	expiry = htobe64(expiry);
416e2d15004SDag-Erling Smørgrav 	oldlim = sldns_buffer_limit(buf);
417e2d15004SDag-Erling Smørgrav 	if(oldlim + sizeof(timestamp)+sizeof(expiry) >=
418e2d15004SDag-Erling Smørgrav 		sldns_buffer_capacity(buf))
419e2d15004SDag-Erling Smørgrav 		return 0; /* doesn't fit. */
420e2d15004SDag-Erling Smørgrav 	sldns_buffer_set_limit(buf, oldlim + sizeof(timestamp)+sizeof(expiry));
421e2d15004SDag-Erling Smørgrav 	sldns_buffer_write_at(buf, oldlim, &timestamp, sizeof(timestamp));
422e2d15004SDag-Erling Smørgrav 	sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry,
423e2d15004SDag-Erling Smørgrav 		sizeof(expiry));
424e2d15004SDag-Erling Smørgrav 
425e2d15004SDag-Erling Smørgrav 	return 1;
426e2d15004SDag-Erling Smørgrav }
427e2d15004SDag-Erling Smørgrav 
428e2d15004SDag-Erling Smørgrav /** check expiry, return true if matches OK */
429e2d15004SDag-Erling Smørgrav static int
430e2d15004SDag-Erling Smørgrav good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf)
431e2d15004SDag-Erling Smørgrav {
432e2d15004SDag-Erling Smørgrav 	uint64_t expiry;
433e2d15004SDag-Erling Smørgrav 	/* the expiry time is the last bytes of the buffer */
434e2d15004SDag-Erling Smørgrav 	if(sldns_buffer_limit(buf) < sizeof(expiry))
435e2d15004SDag-Erling Smørgrav 		return 0;
436e2d15004SDag-Erling Smørgrav 	sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry),
437e2d15004SDag-Erling Smørgrav 		&expiry, sizeof(expiry));
438e2d15004SDag-Erling Smørgrav 	expiry = be64toh(expiry);
439e2d15004SDag-Erling Smørgrav 
440091e9e46SCy Schubert 	/* Check if we are allowed to return expired entries:
441091e9e46SCy Schubert 	 * - serve_expired needs to be set
442091e9e46SCy Schubert 	 * - if SERVE_EXPIRED_TTL is set make sure that the record is not older
443091e9e46SCy Schubert 	 *   than that. */
4448a384985SDag-Erling Smørgrav 	if((time_t)expiry < *qstate->env->now &&
445091e9e46SCy Schubert 		(!qstate->env->cfg->serve_expired ||
446091e9e46SCy Schubert 			(SERVE_EXPIRED_TTL &&
447091e9e46SCy Schubert 			*qstate->env->now - (time_t)expiry > SERVE_EXPIRED_TTL)))
448e2d15004SDag-Erling Smørgrav 		return 0;
449e2d15004SDag-Erling Smørgrav 
450e2d15004SDag-Erling Smørgrav 	return 1;
451e2d15004SDag-Erling Smørgrav }
452e2d15004SDag-Erling Smørgrav 
4538a384985SDag-Erling Smørgrav /* Adjust the TTL of the given RRset by 'subtract'.  If 'subtract' is
4548a384985SDag-Erling Smørgrav  * negative, set the TTL to 0. */
455c7f4d7adSDag-Erling Smørgrav static void
456c7f4d7adSDag-Erling Smørgrav packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract)
457c7f4d7adSDag-Erling Smørgrav {
458c7f4d7adSDag-Erling Smørgrav 	size_t i;
459c7f4d7adSDag-Erling Smørgrav 	size_t total = data->count + data->rrsig_count;
4608a384985SDag-Erling Smørgrav 	if(subtract >= 0 && data->ttl > subtract)
461c7f4d7adSDag-Erling Smørgrav 		data->ttl -= subtract;
462c7f4d7adSDag-Erling Smørgrav 	else	data->ttl = 0;
463c7f4d7adSDag-Erling Smørgrav 	for(i=0; i<total; i++) {
4648a384985SDag-Erling Smørgrav 		if(subtract >= 0 && data->rr_ttl[i] > subtract)
465c7f4d7adSDag-Erling Smørgrav 			data->rr_ttl[i] -= subtract;
466c7f4d7adSDag-Erling Smørgrav 		else	data->rr_ttl[i] = 0;
467c7f4d7adSDag-Erling Smørgrav 	}
468f44e67d1SCy Schubert 	data->ttl_add = (subtract < data->ttl_add) ? (data->ttl_add - subtract) : 0;
469c7f4d7adSDag-Erling Smørgrav }
470c7f4d7adSDag-Erling Smørgrav 
4718a384985SDag-Erling Smørgrav /* Adjust the TTL of a DNS message and its RRs by 'adjust'.  If 'adjust' is
4728a384985SDag-Erling Smørgrav  * negative, set the TTLs to 0. */
473c7f4d7adSDag-Erling Smørgrav static void
474c7f4d7adSDag-Erling Smørgrav adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
475c7f4d7adSDag-Erling Smørgrav {
476c7f4d7adSDag-Erling Smørgrav 	size_t i;
4778a384985SDag-Erling Smørgrav 	if(adjust >= 0 && msg->rep->ttl > adjust)
478c7f4d7adSDag-Erling Smørgrav 		msg->rep->ttl -= adjust;
479091e9e46SCy Schubert 	else
480091e9e46SCy Schubert 		msg->rep->ttl = 0;
481c7f4d7adSDag-Erling Smørgrav 	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
4824c75e3aaSDag-Erling Smørgrav 	msg->rep->serve_expired_ttl = msg->rep->ttl + SERVE_EXPIRED_TTL;
483c7f4d7adSDag-Erling Smørgrav 
484c7f4d7adSDag-Erling Smørgrav 	for(i=0; i<msg->rep->rrset_count; i++) {
485c7f4d7adSDag-Erling Smørgrav 		packed_rrset_ttl_subtract((struct packed_rrset_data*)msg->
486c7f4d7adSDag-Erling Smørgrav 			rep->rrsets[i]->entry.data, adjust);
487c7f4d7adSDag-Erling Smørgrav 	}
488c7f4d7adSDag-Erling Smørgrav }
489c7f4d7adSDag-Erling Smørgrav 
490e2d15004SDag-Erling Smørgrav /** convert dns message in buffer to return_msg */
491e2d15004SDag-Erling Smørgrav static int
492e2d15004SDag-Erling Smørgrav parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
493e2d15004SDag-Erling Smørgrav {
494e2d15004SDag-Erling Smørgrav 	struct msg_parse* prs;
495e2d15004SDag-Erling Smørgrav 	struct edns_data edns;
496e2d15004SDag-Erling Smørgrav 	uint64_t timestamp, expiry;
497e2d15004SDag-Erling Smørgrav 	time_t adjust;
498e2d15004SDag-Erling Smørgrav 	size_t lim = sldns_buffer_limit(buf);
499e2d15004SDag-Erling Smørgrav 	if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
500e2d15004SDag-Erling Smørgrav 		return 0; /* too short */
501e2d15004SDag-Erling Smørgrav 
502e2d15004SDag-Erling Smørgrav 	/* remove timestamp and expiry from end */
503e2d15004SDag-Erling Smørgrav 	sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry));
504e2d15004SDag-Erling Smørgrav 	sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp),
505e2d15004SDag-Erling Smørgrav 		&timestamp, sizeof(timestamp));
506e2d15004SDag-Erling Smørgrav 	expiry = be64toh(expiry);
507e2d15004SDag-Erling Smørgrav 	timestamp = be64toh(timestamp);
508e2d15004SDag-Erling Smørgrav 
509e2d15004SDag-Erling Smørgrav 	/* parse DNS packet */
510e2d15004SDag-Erling Smørgrav 	regional_free_all(qstate->env->scratch);
511e2d15004SDag-Erling Smørgrav 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
512e2d15004SDag-Erling Smørgrav 		sizeof(struct msg_parse));
513e2d15004SDag-Erling Smørgrav 	if(!prs)
514e2d15004SDag-Erling Smørgrav 		return 0; /* out of memory */
515e2d15004SDag-Erling Smørgrav 	memset(prs, 0, sizeof(*prs));
516e2d15004SDag-Erling Smørgrav 	memset(&edns, 0, sizeof(edns));
517e2d15004SDag-Erling Smørgrav 	sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp));
518e2d15004SDag-Erling Smørgrav 	if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
519e2d15004SDag-Erling Smørgrav 		sldns_buffer_set_limit(buf, lim);
520e2d15004SDag-Erling Smørgrav 		return 0;
521e2d15004SDag-Erling Smørgrav 	}
522*24e36522SCy Schubert 	if(parse_extract_edns_from_response_msg(prs, &edns, qstate->env->scratch) !=
523e2d15004SDag-Erling Smørgrav 		LDNS_RCODE_NOERROR) {
524e2d15004SDag-Erling Smørgrav 		sldns_buffer_set_limit(buf, lim);
525e2d15004SDag-Erling Smørgrav 		return 0;
526e2d15004SDag-Erling Smørgrav 	}
527e2d15004SDag-Erling Smørgrav 
528e2d15004SDag-Erling Smørgrav 	qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
529e2d15004SDag-Erling Smørgrav 	sldns_buffer_set_limit(buf, lim);
530e2d15004SDag-Erling Smørgrav 	if(!qstate->return_msg)
531e2d15004SDag-Erling Smørgrav 		return 0;
532e2d15004SDag-Erling Smørgrav 
533e2d15004SDag-Erling Smørgrav 	qstate->return_rcode = LDNS_RCODE_NOERROR;
534e2d15004SDag-Erling Smørgrav 
535e2d15004SDag-Erling Smørgrav 	/* see how much of the TTL expired, and remove it */
536c7f4d7adSDag-Erling Smørgrav 	if(*qstate->env->now <= (time_t)timestamp) {
537c7f4d7adSDag-Erling Smørgrav 		verbose(VERB_ALGO, "cachedb msg adjust by zero");
538c7f4d7adSDag-Erling Smørgrav 		return 1; /* message from the future (clock skew?) */
539e2d15004SDag-Erling Smørgrav 	}
540c7f4d7adSDag-Erling Smørgrav 	adjust = *qstate->env->now - (time_t)timestamp;
541c7f4d7adSDag-Erling Smørgrav 	if(qstate->return_msg->rep->ttl < adjust) {
542c7f4d7adSDag-Erling Smørgrav 		verbose(VERB_ALGO, "cachedb msg expired");
5438a384985SDag-Erling Smørgrav 		/* If serve-expired is enabled, we still use an expired message
5448a384985SDag-Erling Smørgrav 		 * setting the TTL to 0. */
5458a384985SDag-Erling Smørgrav 		if(qstate->env->cfg->serve_expired)
5468a384985SDag-Erling Smørgrav 			adjust = -1;
5478a384985SDag-Erling Smørgrav 		else
548c7f4d7adSDag-Erling Smørgrav 			return 0; /* message expired */
549c7f4d7adSDag-Erling Smørgrav 	}
550c7f4d7adSDag-Erling Smørgrav 	verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
551c7f4d7adSDag-Erling Smørgrav 	adjust_msg_ttl(qstate->return_msg, adjust);
5528a384985SDag-Erling Smørgrav 
5538a384985SDag-Erling Smørgrav 	/* Similar to the unbound worker, if serve-expired is enabled and
5548a384985SDag-Erling Smørgrav 	 * the msg would be considered to be expired, mark the state so a
5558a384985SDag-Erling Smørgrav 	 * refetch will be scheduled.  The comparison between 'expiry' and
5568a384985SDag-Erling Smørgrav 	 * 'now' should be redundant given how these values were calculated,
5578a384985SDag-Erling Smørgrav 	 * but we check it just in case as does good_expiry_and_qinfo(). */
5588a384985SDag-Erling Smørgrav 	if(qstate->env->cfg->serve_expired &&
5598a384985SDag-Erling Smørgrav 		(adjust == -1 || (time_t)expiry < *qstate->env->now)) {
5608a384985SDag-Erling Smørgrav 		qstate->need_refetch = 1;
5618a384985SDag-Erling Smørgrav 	}
5628a384985SDag-Erling Smørgrav 
563c7f4d7adSDag-Erling Smørgrav 	return 1;
564e2d15004SDag-Erling Smørgrav }
565e2d15004SDag-Erling Smørgrav 
566e2d15004SDag-Erling Smørgrav /**
567e2d15004SDag-Erling Smørgrav  * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
568e2d15004SDag-Erling Smørgrav  * return true if lookup was successful.
569e2d15004SDag-Erling Smørgrav  */
570e2d15004SDag-Erling Smørgrav static int
571e2d15004SDag-Erling Smørgrav cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie)
572e2d15004SDag-Erling Smørgrav {
573e2d15004SDag-Erling Smørgrav 	char key[(CACHEDB_HASHSIZE/8)*2+1];
574e2d15004SDag-Erling Smørgrav 	calc_hash(qstate, key, sizeof(key));
575e2d15004SDag-Erling Smørgrav 
576e2d15004SDag-Erling Smørgrav 	/* call backend to fetch data for key into scratch buffer */
577e2d15004SDag-Erling Smørgrav 	if( !(*ie->backend->lookup)(qstate->env, ie, key,
578e2d15004SDag-Erling Smørgrav 		qstate->env->scratch_buffer)) {
579e2d15004SDag-Erling Smørgrav 		return 0;
580e2d15004SDag-Erling Smørgrav 	}
581e2d15004SDag-Erling Smørgrav 
582e2d15004SDag-Erling Smørgrav 	/* check expiry date and check if query-data matches */
583e2d15004SDag-Erling Smørgrav 	if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
584e2d15004SDag-Erling Smørgrav 		return 0;
585e2d15004SDag-Erling Smørgrav 	}
586e2d15004SDag-Erling Smørgrav 
587e2d15004SDag-Erling Smørgrav 	/* parse dns message into return_msg */
588e2d15004SDag-Erling Smørgrav 	if( !parse_data(qstate, qstate->env->scratch_buffer) ) {
589e2d15004SDag-Erling Smørgrav 		return 0;
590e2d15004SDag-Erling Smørgrav 	}
591e2d15004SDag-Erling Smørgrav 	return 1;
592e2d15004SDag-Erling Smørgrav }
593e2d15004SDag-Erling Smørgrav 
594e2d15004SDag-Erling Smørgrav /**
595e2d15004SDag-Erling Smørgrav  * Store the qstate.return_msg in extcache for key qstate.info
596e2d15004SDag-Erling Smørgrav  */
597e2d15004SDag-Erling Smørgrav static void
598e2d15004SDag-Erling Smørgrav cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
599e2d15004SDag-Erling Smørgrav {
600e2d15004SDag-Erling Smørgrav 	char key[(CACHEDB_HASHSIZE/8)*2+1];
601e2d15004SDag-Erling Smørgrav 	calc_hash(qstate, key, sizeof(key));
602e2d15004SDag-Erling Smørgrav 
603e2d15004SDag-Erling Smørgrav 	/* prepare data in scratch buffer */
604e2d15004SDag-Erling Smørgrav 	if(!prep_data(qstate, qstate->env->scratch_buffer))
605e2d15004SDag-Erling Smørgrav 		return;
606e2d15004SDag-Erling Smørgrav 
607e2d15004SDag-Erling Smørgrav 	/* call backend */
608e2d15004SDag-Erling Smørgrav 	(*ie->backend->store)(qstate->env, ie, key,
609e2d15004SDag-Erling Smørgrav 		sldns_buffer_begin(qstate->env->scratch_buffer),
61025039b37SCy Schubert 		sldns_buffer_limit(qstate->env->scratch_buffer),
61125039b37SCy Schubert 		qstate->return_msg->rep->ttl);
612e2d15004SDag-Erling Smørgrav }
613e2d15004SDag-Erling Smørgrav 
614e2d15004SDag-Erling Smørgrav /**
615e2d15004SDag-Erling Smørgrav  * See if unbound's internal cache can answer the query
616e2d15004SDag-Erling Smørgrav  */
617e2d15004SDag-Erling Smørgrav static int
618e2d15004SDag-Erling Smørgrav cachedb_intcache_lookup(struct module_qstate* qstate)
619e2d15004SDag-Erling Smørgrav {
6205469a995SCy Schubert 	uint8_t* dpname=NULL;
6215469a995SCy Schubert 	size_t dpnamelen=0;
622e2d15004SDag-Erling Smørgrav 	struct dns_msg* msg;
6235469a995SCy Schubert 	if(iter_stub_fwd_no_cache(qstate, &qstate->qinfo,
6245469a995SCy Schubert 		&dpname, &dpnamelen))
6255469a995SCy Schubert 		return 0; /* no cache for these queries */
626e2d15004SDag-Erling Smørgrav 	msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname,
627e2d15004SDag-Erling Smørgrav 		qstate->qinfo.qname_len, qstate->qinfo.qtype,
628e2d15004SDag-Erling Smørgrav 		qstate->qinfo.qclass, qstate->query_flags,
62957bddd21SDag-Erling Smørgrav 		qstate->region, qstate->env->scratch,
6305469a995SCy Schubert 		1, /* no partial messages with only a CNAME */
6315469a995SCy Schubert 		dpname, dpnamelen
63257bddd21SDag-Erling Smørgrav 		);
6330fb34990SDag-Erling Smørgrav 	if(!msg && qstate->env->neg_cache &&
6340fb34990SDag-Erling Smørgrav 		iter_qname_indicates_dnssec(qstate->env, &qstate->qinfo)) {
635e2d15004SDag-Erling Smørgrav 		/* lookup in negative cache; may result in
636e2d15004SDag-Erling Smørgrav 		 * NOERROR/NODATA or NXDOMAIN answers that need validation */
637e2d15004SDag-Erling Smørgrav 		msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo,
638e2d15004SDag-Erling Smørgrav 			qstate->region, qstate->env->rrset_cache,
639e2d15004SDag-Erling Smørgrav 			qstate->env->scratch_buffer,
64057bddd21SDag-Erling Smørgrav 			*qstate->env->now, 1/*add SOA*/, NULL,
64157bddd21SDag-Erling Smørgrav 			qstate->env->cfg);
642e2d15004SDag-Erling Smørgrav 	}
643e2d15004SDag-Erling Smørgrav 	if(!msg)
644e2d15004SDag-Erling Smørgrav 		return 0;
645e2d15004SDag-Erling Smørgrav 	/* this is the returned msg */
646e2d15004SDag-Erling Smørgrav 	qstate->return_rcode = LDNS_RCODE_NOERROR;
647e2d15004SDag-Erling Smørgrav 	qstate->return_msg = msg;
648e2d15004SDag-Erling Smørgrav 	return 1;
649e2d15004SDag-Erling Smørgrav }
650e2d15004SDag-Erling Smørgrav 
651e2d15004SDag-Erling Smørgrav /**
652e2d15004SDag-Erling Smørgrav  * Store query into the internal cache of unbound.
653e2d15004SDag-Erling Smørgrav  */
654e2d15004SDag-Erling Smørgrav static void
655e2d15004SDag-Erling Smørgrav cachedb_intcache_store(struct module_qstate* qstate)
656e2d15004SDag-Erling Smørgrav {
6578a384985SDag-Erling Smørgrav 	uint32_t store_flags = qstate->query_flags;
6588a384985SDag-Erling Smørgrav 
6598a384985SDag-Erling Smørgrav 	if(qstate->env->cfg->serve_expired)
6608a384985SDag-Erling Smørgrav 		store_flags |= DNSCACHE_STORE_ZEROTTL;
661e2d15004SDag-Erling Smørgrav 	if(!qstate->return_msg)
662e2d15004SDag-Erling Smørgrav 		return;
663e2d15004SDag-Erling Smørgrav 	(void)dns_cache_store(qstate->env, &qstate->qinfo,
664e2d15004SDag-Erling Smørgrav 		qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
6658a384985SDag-Erling Smørgrav 		qstate->region, store_flags);
666e2d15004SDag-Erling Smørgrav }
667e2d15004SDag-Erling Smørgrav 
668e2d15004SDag-Erling Smørgrav /**
669e2d15004SDag-Erling Smørgrav  * Handle a cachedb module event with a query
670e2d15004SDag-Erling Smørgrav  * @param qstate: query state (from the mesh), passed between modules.
671e2d15004SDag-Erling Smørgrav  * 	contains qstate->env module environment with global caches and so on.
672e2d15004SDag-Erling Smørgrav  * @param iq: query state specific for this module.  per-query.
673e2d15004SDag-Erling Smørgrav  * @param ie: environment specific for this module.  global.
674e2d15004SDag-Erling Smørgrav  * @param id: module id.
675e2d15004SDag-Erling Smørgrav  */
676e2d15004SDag-Erling Smørgrav static void
677e2d15004SDag-Erling Smørgrav cachedb_handle_query(struct module_qstate* qstate,
678e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* ATTR_UNUSED(iq),
679e2d15004SDag-Erling Smørgrav 	struct cachedb_env* ie, int id)
680e2d15004SDag-Erling Smørgrav {
681e2d15004SDag-Erling Smørgrav 	/* check if we are enabled, and skip if so */
682e2d15004SDag-Erling Smørgrav 	if(!ie->enabled) {
683e2d15004SDag-Erling Smørgrav 		/* pass request to next module */
684e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_module;
685e2d15004SDag-Erling Smørgrav 		return;
686e2d15004SDag-Erling Smørgrav 	}
687e2d15004SDag-Erling Smørgrav 
688bc892140SDag-Erling Smørgrav 	if(qstate->blacklist || qstate->no_cache_lookup) {
689bc892140SDag-Erling Smørgrav 		/* cache is blacklisted or we are instructed from edns to not look */
690e2d15004SDag-Erling Smørgrav 		/* pass request to next module */
691e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_wait_module;
692e2d15004SDag-Erling Smørgrav 		return;
693e2d15004SDag-Erling Smørgrav 	}
694e2d15004SDag-Erling Smørgrav 
695091e9e46SCy Schubert 	/* lookup inside unbound's internal cache.
696091e9e46SCy Schubert 	 * This does not look for expired entries. */
697e2d15004SDag-Erling Smørgrav 	if(cachedb_intcache_lookup(qstate)) {
698bc892140SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO) {
699bc892140SDag-Erling Smørgrav 			if(qstate->return_msg->rep)
700e2d15004SDag-Erling Smørgrav 				log_dns_msg("cachedb internal cache lookup",
701e2d15004SDag-Erling Smørgrav 					&qstate->return_msg->qinfo,
702e2d15004SDag-Erling Smørgrav 					qstate->return_msg->rep);
703bc892140SDag-Erling Smørgrav 			else log_info("cachedb internal cache lookup: rcode %s",
704091e9e46SCy Schubert 				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)
705091e9e46SCy Schubert 				?sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name
706091e9e46SCy Schubert 				:"??");
707bc892140SDag-Erling Smørgrav 		}
708e2d15004SDag-Erling Smørgrav 		/* we are done with the query */
709e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
710e2d15004SDag-Erling Smørgrav 		return;
711e2d15004SDag-Erling Smørgrav 	}
712e2d15004SDag-Erling Smørgrav 
713e2d15004SDag-Erling Smørgrav 	/* ask backend cache to see if we have data */
714e2d15004SDag-Erling Smørgrav 	if(cachedb_extcache_lookup(qstate, ie)) {
715e2d15004SDag-Erling Smørgrav 		if(verbosity >= VERB_ALGO)
716e2d15004SDag-Erling Smørgrav 			log_dns_msg(ie->backend->name,
717e2d15004SDag-Erling Smørgrav 				&qstate->return_msg->qinfo,
718e2d15004SDag-Erling Smørgrav 				qstate->return_msg->rep);
719e2d15004SDag-Erling Smørgrav 		/* store this result in internal cache */
720e2d15004SDag-Erling Smørgrav 		cachedb_intcache_store(qstate);
721091e9e46SCy Schubert 		/* In case we have expired data but there is a client timer for expired
722091e9e46SCy Schubert 		 * answers, pass execution to next module in order to try updating the
723091e9e46SCy Schubert 		 * data first.
724091e9e46SCy Schubert 		 * TODO: this needs revisit. The expired data stored from cachedb has
725091e9e46SCy Schubert 		 * 0 TTL which is picked up by iterator later when looking in the cache.
726091e9e46SCy Schubert 		 * Document that ext cachedb does not work properly with
727091e9e46SCy Schubert 		 * serve_stale_reply_ttl yet. */
728091e9e46SCy Schubert 		if(qstate->need_refetch && qstate->serve_expired_data &&
729091e9e46SCy Schubert 			qstate->serve_expired_data->timer) {
730091e9e46SCy Schubert 				qstate->return_msg = NULL;
731091e9e46SCy Schubert 				qstate->ext_state[id] = module_wait_module;
732091e9e46SCy Schubert 				return;
733091e9e46SCy Schubert 		}
734e2d15004SDag-Erling Smørgrav 		/* we are done with the query */
735e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
736e2d15004SDag-Erling Smørgrav 		return;
737e2d15004SDag-Erling Smørgrav 	}
738e2d15004SDag-Erling Smørgrav 
739e2d15004SDag-Erling Smørgrav 	/* no cache fetches */
740e2d15004SDag-Erling Smørgrav 	/* pass request to next module */
741e2d15004SDag-Erling Smørgrav 	qstate->ext_state[id] = module_wait_module;
742e2d15004SDag-Erling Smørgrav }
743e2d15004SDag-Erling Smørgrav 
744e2d15004SDag-Erling Smørgrav /**
745e2d15004SDag-Erling Smørgrav  * Handle a cachedb module event with a response from the iterator.
746e2d15004SDag-Erling Smørgrav  * @param qstate: query state (from the mesh), passed between modules.
747e2d15004SDag-Erling Smørgrav  * 	contains qstate->env module environment with global caches and so on.
748e2d15004SDag-Erling Smørgrav  * @param iq: query state specific for this module.  per-query.
749e2d15004SDag-Erling Smørgrav  * @param ie: environment specific for this module.  global.
750e2d15004SDag-Erling Smørgrav  * @param id: module id.
751e2d15004SDag-Erling Smørgrav  */
752e2d15004SDag-Erling Smørgrav static void
753e2d15004SDag-Erling Smørgrav cachedb_handle_response(struct module_qstate* qstate,
754e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
755e2d15004SDag-Erling Smørgrav {
756bc892140SDag-Erling Smørgrav 	/* check if we are not enabled or instructed to not cache, and skip */
757bc892140SDag-Erling Smørgrav 	if(!ie->enabled || qstate->no_cache_store) {
758e2d15004SDag-Erling Smørgrav 		/* we are done with the query */
759e2d15004SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
760e2d15004SDag-Erling Smørgrav 		return;
761e2d15004SDag-Erling Smørgrav 	}
762e2d15004SDag-Erling Smørgrav 
763e2d15004SDag-Erling Smørgrav 	/* store the item into the backend cache */
764e2d15004SDag-Erling Smørgrav 	cachedb_extcache_store(qstate, ie);
765e2d15004SDag-Erling Smørgrav 
766e2d15004SDag-Erling Smørgrav 	/* we are done with the query */
767e2d15004SDag-Erling Smørgrav 	qstate->ext_state[id] = module_finished;
768e2d15004SDag-Erling Smørgrav }
769e2d15004SDag-Erling Smørgrav 
770e2d15004SDag-Erling Smørgrav void
771e2d15004SDag-Erling Smørgrav cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
772e2d15004SDag-Erling Smørgrav 	struct outbound_entry* outbound)
773e2d15004SDag-Erling Smørgrav {
774e2d15004SDag-Erling Smørgrav 	struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id];
775e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id];
776e2d15004SDag-Erling Smørgrav 	verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s",
777e2d15004SDag-Erling Smørgrav 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
778e2d15004SDag-Erling Smørgrav 	if(iq) log_query_info(VERB_QUERY, "cachedb operate: query",
779e2d15004SDag-Erling Smørgrav 		&qstate->qinfo);
780e2d15004SDag-Erling Smørgrav 
781e2d15004SDag-Erling Smørgrav 	/* perform cachedb state machine */
782e2d15004SDag-Erling Smørgrav 	if((event == module_event_new || event == module_event_pass) &&
783e2d15004SDag-Erling Smørgrav 		iq == NULL) {
784e2d15004SDag-Erling Smørgrav 		if(!cachedb_new(qstate, id)) {
785e2d15004SDag-Erling Smørgrav 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
786e2d15004SDag-Erling Smørgrav 			return;
787e2d15004SDag-Erling Smørgrav 		}
788e2d15004SDag-Erling Smørgrav 		iq = (struct cachedb_qstate*)qstate->minfo[id];
789e2d15004SDag-Erling Smørgrav 	}
790e2d15004SDag-Erling Smørgrav 	if(iq && (event == module_event_pass || event == module_event_new)) {
791e2d15004SDag-Erling Smørgrav 		cachedb_handle_query(qstate, iq, ie, id);
792e2d15004SDag-Erling Smørgrav 		return;
793e2d15004SDag-Erling Smørgrav 	}
794e2d15004SDag-Erling Smørgrav 	if(iq && (event == module_event_moddone)) {
795e2d15004SDag-Erling Smørgrav 		cachedb_handle_response(qstate, iq, ie, id);
796e2d15004SDag-Erling Smørgrav 		return;
797e2d15004SDag-Erling Smørgrav 	}
798e2d15004SDag-Erling Smørgrav 	if(iq && outbound) {
799e2d15004SDag-Erling Smørgrav 		/* cachedb does not need to process responses at this time
800e2d15004SDag-Erling Smørgrav 		 * ignore it.
801e2d15004SDag-Erling Smørgrav 		cachedb_process_response(qstate, iq, ie, id, outbound, event);
802e2d15004SDag-Erling Smørgrav 		*/
803e2d15004SDag-Erling Smørgrav 		return;
804e2d15004SDag-Erling Smørgrav 	}
805e2d15004SDag-Erling Smørgrav 	if(event == module_event_error) {
806e2d15004SDag-Erling Smørgrav 		verbose(VERB_ALGO, "got called with event error, giving up");
807e2d15004SDag-Erling Smørgrav 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
808e2d15004SDag-Erling Smørgrav 		return;
809e2d15004SDag-Erling Smørgrav 	}
810bc892140SDag-Erling Smørgrav 	if(!iq && (event == module_event_moddone)) {
811bc892140SDag-Erling Smørgrav 		/* during priming, module done but we never started */
812bc892140SDag-Erling Smørgrav 		qstate->ext_state[id] = module_finished;
813bc892140SDag-Erling Smørgrav 		return;
814bc892140SDag-Erling Smørgrav 	}
815e2d15004SDag-Erling Smørgrav 
816e2d15004SDag-Erling Smørgrav 	log_err("bad event for cachedb");
817e2d15004SDag-Erling Smørgrav 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
818e2d15004SDag-Erling Smørgrav }
819e2d15004SDag-Erling Smørgrav 
820e2d15004SDag-Erling Smørgrav void
821e2d15004SDag-Erling Smørgrav cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
822e2d15004SDag-Erling Smørgrav 	int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
823e2d15004SDag-Erling Smørgrav {
824e2d15004SDag-Erling Smørgrav 	/* cachedb does not use subordinate requests at this time */
825e2d15004SDag-Erling Smørgrav 	verbose(VERB_ALGO, "cachedb inform_super was called");
826e2d15004SDag-Erling Smørgrav }
827e2d15004SDag-Erling Smørgrav 
828e2d15004SDag-Erling Smørgrav void
829e2d15004SDag-Erling Smørgrav cachedb_clear(struct module_qstate* qstate, int id)
830e2d15004SDag-Erling Smørgrav {
831e2d15004SDag-Erling Smørgrav 	struct cachedb_qstate* iq;
832e2d15004SDag-Erling Smørgrav 	if(!qstate)
833e2d15004SDag-Erling Smørgrav 		return;
834e2d15004SDag-Erling Smørgrav 	iq = (struct cachedb_qstate*)qstate->minfo[id];
835e2d15004SDag-Erling Smørgrav 	if(iq) {
836e2d15004SDag-Erling Smørgrav 		/* free contents of iq */
837e2d15004SDag-Erling Smørgrav 		/* TODO */
838e2d15004SDag-Erling Smørgrav 	}
839e2d15004SDag-Erling Smørgrav 	qstate->minfo[id] = NULL;
840e2d15004SDag-Erling Smørgrav }
841e2d15004SDag-Erling Smørgrav 
842e2d15004SDag-Erling Smørgrav size_t
843e2d15004SDag-Erling Smørgrav cachedb_get_mem(struct module_env* env, int id)
844e2d15004SDag-Erling Smørgrav {
845e2d15004SDag-Erling Smørgrav 	struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
846e2d15004SDag-Erling Smørgrav 	if(!ie)
847e2d15004SDag-Erling Smørgrav 		return 0;
848e2d15004SDag-Erling Smørgrav 	return sizeof(*ie); /* TODO - more mem */
849e2d15004SDag-Erling Smørgrav }
850e2d15004SDag-Erling Smørgrav 
851e2d15004SDag-Erling Smørgrav /**
852e2d15004SDag-Erling Smørgrav  * The cachedb function block
853e2d15004SDag-Erling Smørgrav  */
854e2d15004SDag-Erling Smørgrav static struct module_func_block cachedb_block = {
855e2d15004SDag-Erling Smørgrav 	"cachedb",
856e2d15004SDag-Erling Smørgrav 	&cachedb_init, &cachedb_deinit, &cachedb_operate,
857e2d15004SDag-Erling Smørgrav 	&cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
858e2d15004SDag-Erling Smørgrav };
859e2d15004SDag-Erling Smørgrav 
860e2d15004SDag-Erling Smørgrav struct module_func_block*
861e2d15004SDag-Erling Smørgrav cachedb_get_funcblock(void)
862e2d15004SDag-Erling Smørgrav {
863e2d15004SDag-Erling Smørgrav 	return &cachedb_block;
864e2d15004SDag-Erling Smørgrav }
865e2d15004SDag-Erling Smørgrav #endif /* USE_CACHEDB */
866