Home | History | Annotate | Line # | Download | only in cachedb
cachedb.c revision 1.1.1.2
      1      1.1  christos /*
      2      1.1  christos  * cachedb/cachedb.c - cache from a database external to the program module
      3      1.1  christos  *
      4      1.1  christos  * Copyright (c) 2016, NLnet Labs. All rights reserved.
      5      1.1  christos  *
      6      1.1  christos  * This software is open source.
      7      1.1  christos  *
      8      1.1  christos  * Redistribution and use in source and binary forms, with or without
      9      1.1  christos  * modification, are permitted provided that the following conditions
     10      1.1  christos  * are met:
     11      1.1  christos  *
     12      1.1  christos  * Redistributions of source code must retain the above copyright notice,
     13      1.1  christos  * this list of conditions and the following disclaimer.
     14      1.1  christos  *
     15      1.1  christos  * Redistributions in binary form must reproduce the above copyright notice,
     16      1.1  christos  * this list of conditions and the following disclaimer in the documentation
     17      1.1  christos  * and/or other materials provided with the distribution.
     18      1.1  christos  *
     19      1.1  christos  * Neither the name of the NLNET LABS nor the names of its contributors may
     20      1.1  christos  * be used to endorse or promote products derived from this software without
     21      1.1  christos  * specific prior written permission.
     22      1.1  christos  *
     23      1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     24      1.1  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     25      1.1  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     26      1.1  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     27      1.1  christos  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     28      1.1  christos  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
     29      1.1  christos  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     30      1.1  christos  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     31      1.1  christos  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     32      1.1  christos  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     33      1.1  christos  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34      1.1  christos  */
     35      1.1  christos 
     36      1.1  christos /**
     37      1.1  christos  * \file
     38      1.1  christos  *
     39      1.1  christos  * This file contains a module that uses an external database to cache
     40      1.1  christos  * dns responses.
     41      1.1  christos  */
     42      1.1  christos 
     43      1.1  christos #include "config.h"
     44      1.1  christos #ifdef USE_CACHEDB
     45      1.1  christos #include "cachedb/cachedb.h"
     46      1.1  christos #include "util/regional.h"
     47      1.1  christos #include "util/net_help.h"
     48      1.1  christos #include "util/config_file.h"
     49      1.1  christos #include "util/data/msgreply.h"
     50      1.1  christos #include "util/data/msgencode.h"
     51      1.1  christos #include "services/cache/dns.h"
     52      1.1  christos #include "validator/val_neg.h"
     53      1.1  christos #include "validator/val_secalgo.h"
     54      1.1  christos #include "iterator/iter_utils.h"
     55      1.1  christos #include "sldns/parseutil.h"
     56      1.1  christos #include "sldns/wire2str.h"
     57      1.1  christos #include "sldns/sbuffer.h"
     58      1.1  christos 
     59      1.1  christos #define CACHEDB_HASHSIZE 256 /* bit hash */
     60      1.1  christos 
     61      1.1  christos /** the unit test testframe for cachedb, its module state contains
     62      1.1  christos  * a cache for a couple queries (in memory). */
     63      1.1  christos struct testframe_moddata {
     64  1.1.1.2  christos 	/** lock for mutex */
     65  1.1.1.2  christos 	lock_basic_type lock;
     66      1.1  christos 	/** key for single stored data element, NULL if none */
     67      1.1  christos 	char* stored_key;
     68      1.1  christos 	/** data for single stored data element, NULL if none */
     69      1.1  christos 	uint8_t* stored_data;
     70      1.1  christos 	/** length of stored data */
     71      1.1  christos 	size_t stored_datalen;
     72      1.1  christos };
     73      1.1  christos 
     74      1.1  christos static int
     75      1.1  christos testframe_init(struct module_env* env, struct cachedb_env* cachedb_env)
     76      1.1  christos {
     77  1.1.1.2  christos 	struct testframe_moddata* d;
     78      1.1  christos 	(void)env;
     79      1.1  christos 	verbose(VERB_ALGO, "testframe_init");
     80  1.1.1.2  christos 	d = (struct testframe_moddata*)calloc(1,
     81      1.1  christos 		sizeof(struct testframe_moddata));
     82  1.1.1.2  christos 	cachedb_env->backend_data = (void*)d;
     83      1.1  christos 	if(!cachedb_env->backend_data) {
     84      1.1  christos 		log_err("out of memory");
     85      1.1  christos 		return 0;
     86      1.1  christos 	}
     87  1.1.1.2  christos 	lock_basic_init(&d->lock);
     88  1.1.1.2  christos 	lock_protect(&d->lock, d, sizeof(*d));
     89      1.1  christos 	return 1;
     90      1.1  christos }
     91      1.1  christos 
     92      1.1  christos static void
     93      1.1  christos testframe_deinit(struct module_env* env, struct cachedb_env* cachedb_env)
     94      1.1  christos {
     95      1.1  christos 	struct testframe_moddata* d = (struct testframe_moddata*)
     96      1.1  christos 		cachedb_env->backend_data;
     97      1.1  christos 	(void)env;
     98      1.1  christos 	verbose(VERB_ALGO, "testframe_deinit");
     99      1.1  christos 	if(!d)
    100      1.1  christos 		return;
    101  1.1.1.2  christos 	lock_basic_destroy(&d->lock);
    102      1.1  christos 	free(d->stored_key);
    103      1.1  christos 	free(d->stored_data);
    104      1.1  christos 	free(d);
    105      1.1  christos }
    106      1.1  christos 
    107      1.1  christos static int
    108      1.1  christos testframe_lookup(struct module_env* env, struct cachedb_env* cachedb_env,
    109      1.1  christos 	char* key, struct sldns_buffer* result_buffer)
    110      1.1  christos {
    111      1.1  christos 	struct testframe_moddata* d = (struct testframe_moddata*)
    112      1.1  christos 		cachedb_env->backend_data;
    113      1.1  christos 	(void)env;
    114      1.1  christos 	verbose(VERB_ALGO, "testframe_lookup of %s", key);
    115  1.1.1.2  christos 	lock_basic_lock(&d->lock);
    116      1.1  christos 	if(d->stored_key && strcmp(d->stored_key, key) == 0) {
    117  1.1.1.2  christos 		if(d->stored_datalen > sldns_buffer_capacity(result_buffer)) {
    118  1.1.1.2  christos 			lock_basic_unlock(&d->lock);
    119      1.1  christos 			return 0; /* too large */
    120  1.1.1.2  christos 		}
    121      1.1  christos 		verbose(VERB_ALGO, "testframe_lookup found %d bytes",
    122      1.1  christos 			(int)d->stored_datalen);
    123      1.1  christos 		sldns_buffer_clear(result_buffer);
    124      1.1  christos 		sldns_buffer_write(result_buffer, d->stored_data,
    125      1.1  christos 			d->stored_datalen);
    126      1.1  christos 		sldns_buffer_flip(result_buffer);
    127  1.1.1.2  christos 		lock_basic_unlock(&d->lock);
    128      1.1  christos 		return 1;
    129      1.1  christos 	}
    130  1.1.1.2  christos 	lock_basic_unlock(&d->lock);
    131      1.1  christos 	return 0;
    132      1.1  christos }
    133      1.1  christos 
    134      1.1  christos static void
    135      1.1  christos testframe_store(struct module_env* env, struct cachedb_env* cachedb_env,
    136      1.1  christos 	char* key, uint8_t* data, size_t data_len)
    137      1.1  christos {
    138      1.1  christos 	struct testframe_moddata* d = (struct testframe_moddata*)
    139      1.1  christos 		cachedb_env->backend_data;
    140      1.1  christos 	(void)env;
    141  1.1.1.2  christos 	lock_basic_lock(&d->lock);
    142      1.1  christos 	verbose(VERB_ALGO, "testframe_store %s (%d bytes)", key, (int)data_len);
    143      1.1  christos 
    144      1.1  christos 	/* free old data element (if any) */
    145      1.1  christos 	free(d->stored_key);
    146      1.1  christos 	d->stored_key = NULL;
    147      1.1  christos 	free(d->stored_data);
    148      1.1  christos 	d->stored_data = NULL;
    149      1.1  christos 	d->stored_datalen = 0;
    150      1.1  christos 
    151      1.1  christos 	d->stored_data = memdup(data, data_len);
    152      1.1  christos 	if(!d->stored_data) {
    153  1.1.1.2  christos 		lock_basic_unlock(&d->lock);
    154      1.1  christos 		log_err("out of memory");
    155      1.1  christos 		return;
    156      1.1  christos 	}
    157      1.1  christos 	d->stored_datalen = data_len;
    158      1.1  christos 	d->stored_key = strdup(key);
    159      1.1  christos 	if(!d->stored_key) {
    160      1.1  christos 		free(d->stored_data);
    161      1.1  christos 		d->stored_data = NULL;
    162      1.1  christos 		d->stored_datalen = 0;
    163  1.1.1.2  christos 		lock_basic_unlock(&d->lock);
    164      1.1  christos 		return;
    165      1.1  christos 	}
    166  1.1.1.2  christos 	lock_basic_unlock(&d->lock);
    167      1.1  christos 	/* (key,data) successfully stored */
    168      1.1  christos }
    169      1.1  christos 
    170      1.1  christos /** The testframe backend is for unit tests */
    171      1.1  christos static struct cachedb_backend testframe_backend = { "testframe",
    172      1.1  christos 	testframe_init, testframe_deinit, testframe_lookup, testframe_store
    173      1.1  christos };
    174      1.1  christos 
    175      1.1  christos /** find a particular backend from possible backends */
    176      1.1  christos static struct cachedb_backend*
    177      1.1  christos cachedb_find_backend(const char* str)
    178      1.1  christos {
    179      1.1  christos 	if(strcmp(str, testframe_backend.name) == 0)
    180      1.1  christos 		return &testframe_backend;
    181      1.1  christos 	/* TODO add more backends here */
    182      1.1  christos 	return NULL;
    183      1.1  christos }
    184      1.1  christos 
    185      1.1  christos /** apply configuration to cachedb module 'global' state */
    186      1.1  christos static int
    187      1.1  christos cachedb_apply_cfg(struct cachedb_env* cachedb_env, struct config_file* cfg)
    188      1.1  christos {
    189  1.1.1.2  christos 	const char* backend_str = cfg->cachedb_backend;
    190  1.1.1.2  christos 
    191  1.1.1.2  christos 	/* If unspecified we use the in-memory test DB. */
    192  1.1.1.2  christos 	if(!backend_str)
    193  1.1.1.2  christos 		backend_str = "testframe";
    194  1.1.1.2  christos 	cachedb_env->backend = cachedb_find_backend(backend_str);
    195  1.1.1.2  christos 	if(!cachedb_env->backend) {
    196  1.1.1.2  christos 		log_err("cachedb: cannot find backend name '%s'", backend_str);
    197  1.1.1.2  christos 		return 0;
    198      1.1  christos 	}
    199  1.1.1.2  christos 
    200      1.1  christos 	/* TODO see if more configuration needs to be applied or not */
    201      1.1  christos 	return 1;
    202      1.1  christos }
    203      1.1  christos 
    204      1.1  christos int
    205      1.1  christos cachedb_init(struct module_env* env, int id)
    206      1.1  christos {
    207      1.1  christos 	struct cachedb_env* cachedb_env = (struct cachedb_env*)calloc(1,
    208      1.1  christos 		sizeof(struct cachedb_env));
    209      1.1  christos 	if(!cachedb_env) {
    210      1.1  christos 		log_err("malloc failure");
    211      1.1  christos 		return 0;
    212      1.1  christos 	}
    213      1.1  christos 	env->modinfo[id] = (void*)cachedb_env;
    214      1.1  christos 	if(!cachedb_apply_cfg(cachedb_env, env->cfg)) {
    215      1.1  christos 		log_err("cachedb: could not apply configuration settings.");
    216      1.1  christos 		return 0;
    217      1.1  christos 	}
    218      1.1  christos 	/* see if a backend is selected */
    219      1.1  christos 	if(!cachedb_env->backend || !cachedb_env->backend->name)
    220      1.1  christos 		return 1;
    221      1.1  christos 	if(!(*cachedb_env->backend->init)(env, cachedb_env)) {
    222      1.1  christos 		log_err("cachedb: could not init %s backend",
    223      1.1  christos 			cachedb_env->backend->name);
    224      1.1  christos 		return 0;
    225      1.1  christos 	}
    226      1.1  christos 	cachedb_env->enabled = 1;
    227      1.1  christos 	return 1;
    228      1.1  christos }
    229      1.1  christos 
    230      1.1  christos void
    231      1.1  christos cachedb_deinit(struct module_env* env, int id)
    232      1.1  christos {
    233      1.1  christos 	struct cachedb_env* cachedb_env;
    234      1.1  christos 	if(!env || !env->modinfo[id])
    235      1.1  christos 		return;
    236      1.1  christos 	cachedb_env = (struct cachedb_env*)env->modinfo[id];
    237      1.1  christos 	/* free contents */
    238      1.1  christos 	/* TODO */
    239      1.1  christos 	if(cachedb_env->enabled) {
    240      1.1  christos 		(*cachedb_env->backend->deinit)(env, cachedb_env);
    241      1.1  christos 	}
    242      1.1  christos 
    243      1.1  christos 	free(cachedb_env);
    244      1.1  christos 	env->modinfo[id] = NULL;
    245      1.1  christos }
    246      1.1  christos 
    247      1.1  christos /** new query for cachedb */
    248      1.1  christos static int
    249      1.1  christos cachedb_new(struct module_qstate* qstate, int id)
    250      1.1  christos {
    251      1.1  christos 	struct cachedb_qstate* iq = (struct cachedb_qstate*)regional_alloc(
    252      1.1  christos 		qstate->region, sizeof(struct cachedb_qstate));
    253      1.1  christos 	qstate->minfo[id] = iq;
    254      1.1  christos 	if(!iq)
    255      1.1  christos 		return 0;
    256      1.1  christos 	memset(iq, 0, sizeof(*iq));
    257      1.1  christos 	/* initialise it */
    258      1.1  christos 	/* TODO */
    259      1.1  christos 
    260      1.1  christos 	return 1;
    261      1.1  christos }
    262      1.1  christos 
    263      1.1  christos /**
    264      1.1  christos  * Return an error
    265      1.1  christos  * @param qstate: our query state
    266      1.1  christos  * @param id: module id
    267      1.1  christos  * @param rcode: error code (DNS errcode).
    268      1.1  christos  * @return: 0 for use by caller, to make notation easy, like:
    269      1.1  christos  * 	return error_response(..).
    270      1.1  christos  */
    271      1.1  christos static int
    272      1.1  christos error_response(struct module_qstate* qstate, int id, int rcode)
    273      1.1  christos {
    274      1.1  christos 	verbose(VERB_QUERY, "return error response %s",
    275      1.1  christos 		sldns_lookup_by_id(sldns_rcodes, rcode)?
    276      1.1  christos 		sldns_lookup_by_id(sldns_rcodes, rcode)->name:"??");
    277      1.1  christos 	qstate->return_rcode = rcode;
    278      1.1  christos 	qstate->return_msg = NULL;
    279      1.1  christos 	qstate->ext_state[id] = module_finished;
    280      1.1  christos 	return 0;
    281      1.1  christos }
    282      1.1  christos 
    283      1.1  christos /**
    284      1.1  christos  * Hash the query name, type, class and dbacess-secret into lookup buffer.
    285      1.1  christos  * @param qstate: query state with query info
    286      1.1  christos  * 	and env->cfg with secret.
    287      1.1  christos  * @param buf: returned buffer with hash to lookup
    288      1.1  christos  * @param len: length of the buffer.
    289      1.1  christos  */
    290      1.1  christos static void
    291      1.1  christos calc_hash(struct module_qstate* qstate, char* buf, size_t len)
    292      1.1  christos {
    293      1.1  christos 	uint8_t clear[1024];
    294      1.1  christos 	size_t clen = 0;
    295      1.1  christos 	uint8_t hash[CACHEDB_HASHSIZE/8];
    296      1.1  christos 	const char* hex = "0123456789ABCDEF";
    297  1.1.1.2  christos 	const char* secret = qstate->env->cfg->cachedb_secret ?
    298  1.1.1.2  christos 		qstate->env->cfg->cachedb_secret : "default";
    299      1.1  christos 	size_t i;
    300  1.1.1.2  christos 
    301      1.1  christos 	/* copy the hash info into the clear buffer */
    302      1.1  christos 	if(clen + qstate->qinfo.qname_len < sizeof(clear)) {
    303      1.1  christos 		memmove(clear+clen, qstate->qinfo.qname,
    304      1.1  christos 			qstate->qinfo.qname_len);
    305      1.1  christos 		clen += qstate->qinfo.qname_len;
    306      1.1  christos 	}
    307      1.1  christos 	if(clen + 4 < sizeof(clear)) {
    308      1.1  christos 		uint16_t t = htons(qstate->qinfo.qtype);
    309      1.1  christos 		uint16_t c = htons(qstate->qinfo.qclass);
    310      1.1  christos 		memmove(clear+clen, &t, 2);
    311      1.1  christos 		memmove(clear+clen+2, &c, 2);
    312      1.1  christos 		clen += 4;
    313      1.1  christos 	}
    314      1.1  christos 	if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) {
    315      1.1  christos 		memmove(clear+clen, secret, strlen(secret));
    316      1.1  christos 		clen += strlen(secret);
    317      1.1  christos 	}
    318      1.1  christos 
    319      1.1  christos 	/* hash the buffer */
    320      1.1  christos 	secalgo_hash_sha256(clear, clen, hash);
    321      1.1  christos 	memset(clear, 0, clen);
    322      1.1  christos 
    323      1.1  christos 	/* hex encode output for portability (some online dbs need
    324      1.1  christos 	 * no nulls, no control characters, and so on) */
    325      1.1  christos 	log_assert(len >= sizeof(hash)*2 + 1);
    326      1.1  christos 	(void)len;
    327      1.1  christos 	for(i=0; i<sizeof(hash); i++) {
    328      1.1  christos 		buf[i*2] = hex[(hash[i]&0xf0)>>4];
    329      1.1  christos 		buf[i*2+1] = hex[hash[i]&0x0f];
    330      1.1  christos 	}
    331      1.1  christos 	buf[sizeof(hash)*2] = 0;
    332      1.1  christos }
    333      1.1  christos 
    334      1.1  christos /** convert data from return_msg into the data buffer */
    335      1.1  christos static int
    336      1.1  christos prep_data(struct module_qstate* qstate, struct sldns_buffer* buf)
    337      1.1  christos {
    338      1.1  christos 	uint64_t timestamp, expiry;
    339      1.1  christos 	size_t oldlim;
    340      1.1  christos 	struct edns_data edns;
    341      1.1  christos 	memset(&edns, 0, sizeof(edns));
    342      1.1  christos 	edns.edns_present = 1;
    343      1.1  christos 	edns.bits = EDNS_DO;
    344      1.1  christos 	edns.ext_rcode = 0;
    345      1.1  christos 	edns.edns_version = EDNS_ADVERTISED_VERSION;
    346      1.1  christos 	edns.udp_size = EDNS_ADVERTISED_SIZE;
    347      1.1  christos 
    348      1.1  christos 	if(!qstate->return_msg || !qstate->return_msg->rep)
    349      1.1  christos 		return 0;
    350  1.1.1.2  christos 	/* We don't store the reply if its TTL is 0 unless serve-expired is
    351  1.1.1.2  christos 	 * enabled.  Such a reply won't be reusable and simply be a waste for
    352  1.1.1.2  christos 	 * the backend.  It's also compatible with the default behavior of
    353  1.1.1.2  christos 	 * dns_cache_store_msg(). */
    354  1.1.1.2  christos 	if(qstate->return_msg->rep->ttl == 0 &&
    355  1.1.1.2  christos 		!qstate->env->cfg->serve_expired)
    356  1.1.1.2  christos 		return 0;
    357      1.1  christos 	if(verbosity >= VERB_ALGO)
    358      1.1  christos 		log_dns_msg("cachedb encoding", &qstate->return_msg->qinfo,
    359      1.1  christos 	                qstate->return_msg->rep);
    360      1.1  christos 	if(!reply_info_answer_encode(&qstate->return_msg->qinfo,
    361      1.1  christos 		qstate->return_msg->rep, 0, qstate->query_flags,
    362      1.1  christos 		buf, 0, 1, qstate->env->scratch, 65535, &edns, 1, 0))
    363      1.1  christos 		return 0;
    364      1.1  christos 
    365      1.1  christos 	/* TTLs in the return_msg are relative to time(0) so we have to
    366      1.1  christos 	 * store that, we also store the smallest ttl in the packet+time(0)
    367      1.1  christos 	 * as the packet expiry time */
    368      1.1  christos 	/* qstate->return_msg->rep->ttl contains that relative shortest ttl */
    369      1.1  christos 	timestamp = (uint64_t)*qstate->env->now;
    370      1.1  christos 	expiry = timestamp + (uint64_t)qstate->return_msg->rep->ttl;
    371      1.1  christos 	timestamp = htobe64(timestamp);
    372      1.1  christos 	expiry = htobe64(expiry);
    373      1.1  christos 	oldlim = sldns_buffer_limit(buf);
    374      1.1  christos 	if(oldlim + sizeof(timestamp)+sizeof(expiry) >=
    375      1.1  christos 		sldns_buffer_capacity(buf))
    376      1.1  christos 		return 0; /* doesn't fit. */
    377      1.1  christos 	sldns_buffer_set_limit(buf, oldlim + sizeof(timestamp)+sizeof(expiry));
    378      1.1  christos 	sldns_buffer_write_at(buf, oldlim, &timestamp, sizeof(timestamp));
    379      1.1  christos 	sldns_buffer_write_at(buf, oldlim+sizeof(timestamp), &expiry,
    380      1.1  christos 		sizeof(expiry));
    381      1.1  christos 
    382      1.1  christos 	return 1;
    383      1.1  christos }
    384      1.1  christos 
    385      1.1  christos /** check expiry, return true if matches OK */
    386      1.1  christos static int
    387      1.1  christos good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf)
    388      1.1  christos {
    389      1.1  christos 	uint64_t expiry;
    390      1.1  christos 	/* the expiry time is the last bytes of the buffer */
    391      1.1  christos 	if(sldns_buffer_limit(buf) < sizeof(expiry))
    392      1.1  christos 		return 0;
    393      1.1  christos 	sldns_buffer_read_at(buf, sldns_buffer_limit(buf)-sizeof(expiry),
    394      1.1  christos 		&expiry, sizeof(expiry));
    395      1.1  christos 	expiry = be64toh(expiry);
    396      1.1  christos 
    397  1.1.1.2  christos 	if((time_t)expiry < *qstate->env->now &&
    398  1.1.1.2  christos 		!qstate->env->cfg->serve_expired)
    399      1.1  christos 		return 0;
    400      1.1  christos 
    401      1.1  christos 	return 1;
    402      1.1  christos }
    403      1.1  christos 
    404  1.1.1.2  christos /* Adjust the TTL of the given RRset by 'subtract'.  If 'subtract' is
    405  1.1.1.2  christos  * negative, set the TTL to 0. */
    406  1.1.1.2  christos static void
    407  1.1.1.2  christos packed_rrset_ttl_subtract(struct packed_rrset_data* data, time_t subtract)
    408  1.1.1.2  christos {
    409  1.1.1.2  christos         size_t i;
    410  1.1.1.2  christos         size_t total = data->count + data->rrsig_count;
    411  1.1.1.2  christos 	if(subtract >= 0 && data->ttl > subtract)
    412  1.1.1.2  christos 		data->ttl -= subtract;
    413  1.1.1.2  christos 	else	data->ttl = 0;
    414  1.1.1.2  christos         for(i=0; i<total; i++) {
    415  1.1.1.2  christos 		if(subtract >= 0 && data->rr_ttl[i] > subtract)
    416  1.1.1.2  christos                 	data->rr_ttl[i] -= subtract;
    417  1.1.1.2  christos                 else	data->rr_ttl[i] = 0;
    418  1.1.1.2  christos 	}
    419  1.1.1.2  christos }
    420  1.1.1.2  christos 
    421  1.1.1.2  christos /* Adjust the TTL of a DNS message and its RRs by 'adjust'.  If 'adjust' is
    422  1.1.1.2  christos  * negative, set the TTLs to 0. */
    423  1.1.1.2  christos static void
    424  1.1.1.2  christos adjust_msg_ttl(struct dns_msg* msg, time_t adjust)
    425  1.1.1.2  christos {
    426  1.1.1.2  christos 	size_t i;
    427  1.1.1.2  christos 	if(adjust >= 0 && msg->rep->ttl > adjust)
    428  1.1.1.2  christos 		msg->rep->ttl -= adjust;
    429  1.1.1.2  christos 	else	msg->rep->ttl = 0;
    430  1.1.1.2  christos 	msg->rep->prefetch_ttl = PREFETCH_TTL_CALC(msg->rep->ttl);
    431  1.1.1.2  christos 
    432  1.1.1.2  christos 	for(i=0; i<msg->rep->rrset_count; i++) {
    433  1.1.1.2  christos 		packed_rrset_ttl_subtract((struct packed_rrset_data*)msg->
    434  1.1.1.2  christos 			rep->rrsets[i]->entry.data, adjust);
    435  1.1.1.2  christos 	}
    436  1.1.1.2  christos }
    437  1.1.1.2  christos 
    438      1.1  christos /** convert dns message in buffer to return_msg */
    439      1.1  christos static int
    440      1.1  christos parse_data(struct module_qstate* qstate, struct sldns_buffer* buf)
    441      1.1  christos {
    442      1.1  christos 	struct msg_parse* prs;
    443      1.1  christos 	struct edns_data edns;
    444      1.1  christos 	uint64_t timestamp, expiry;
    445      1.1  christos 	time_t adjust;
    446      1.1  christos 	size_t lim = sldns_buffer_limit(buf);
    447      1.1  christos 	if(lim < LDNS_HEADER_SIZE+sizeof(timestamp)+sizeof(expiry))
    448      1.1  christos 		return 0; /* too short */
    449      1.1  christos 
    450      1.1  christos 	/* remove timestamp and expiry from end */
    451      1.1  christos 	sldns_buffer_read_at(buf, lim-sizeof(expiry), &expiry, sizeof(expiry));
    452      1.1  christos 	sldns_buffer_read_at(buf, lim-sizeof(expiry)-sizeof(timestamp),
    453      1.1  christos 		&timestamp, sizeof(timestamp));
    454      1.1  christos 	expiry = be64toh(expiry);
    455      1.1  christos 	timestamp = be64toh(timestamp);
    456      1.1  christos 
    457      1.1  christos 	/* parse DNS packet */
    458      1.1  christos 	regional_free_all(qstate->env->scratch);
    459      1.1  christos 	prs = (struct msg_parse*)regional_alloc(qstate->env->scratch,
    460      1.1  christos 		sizeof(struct msg_parse));
    461      1.1  christos 	if(!prs)
    462      1.1  christos 		return 0; /* out of memory */
    463      1.1  christos 	memset(prs, 0, sizeof(*prs));
    464      1.1  christos 	memset(&edns, 0, sizeof(edns));
    465      1.1  christos 	sldns_buffer_set_limit(buf, lim - sizeof(expiry)-sizeof(timestamp));
    466      1.1  christos 	if(parse_packet(buf, prs, qstate->env->scratch) != LDNS_RCODE_NOERROR) {
    467      1.1  christos 		sldns_buffer_set_limit(buf, lim);
    468      1.1  christos 		return 0;
    469      1.1  christos 	}
    470      1.1  christos 	if(parse_extract_edns(prs, &edns, qstate->env->scratch) !=
    471      1.1  christos 		LDNS_RCODE_NOERROR) {
    472      1.1  christos 		sldns_buffer_set_limit(buf, lim);
    473      1.1  christos 		return 0;
    474      1.1  christos 	}
    475      1.1  christos 
    476      1.1  christos 	qstate->return_msg = dns_alloc_msg(buf, prs, qstate->region);
    477      1.1  christos 	sldns_buffer_set_limit(buf, lim);
    478      1.1  christos 	if(!qstate->return_msg)
    479      1.1  christos 		return 0;
    480      1.1  christos 
    481      1.1  christos 	qstate->return_rcode = LDNS_RCODE_NOERROR;
    482      1.1  christos 
    483      1.1  christos 	/* see how much of the TTL expired, and remove it */
    484  1.1.1.2  christos 	if(*qstate->env->now <= (time_t)timestamp) {
    485  1.1.1.2  christos 		verbose(VERB_ALGO, "cachedb msg adjust by zero");
    486  1.1.1.2  christos 		return 1; /* message from the future (clock skew?) */
    487  1.1.1.2  christos 	}
    488      1.1  christos 	adjust = *qstate->env->now - (time_t)timestamp;
    489  1.1.1.2  christos 	if(qstate->return_msg->rep->ttl < adjust) {
    490  1.1.1.2  christos 		verbose(VERB_ALGO, "cachedb msg expired");
    491  1.1.1.2  christos 		/* If serve-expired is enabled, we still use an expired message
    492  1.1.1.2  christos 		 * setting the TTL to 0. */
    493  1.1.1.2  christos 		if(qstate->env->cfg->serve_expired)
    494  1.1.1.2  christos 			adjust = -1;
    495  1.1.1.2  christos 		else
    496  1.1.1.2  christos 			return 0; /* message expired */
    497  1.1.1.2  christos 	}
    498      1.1  christos 	verbose(VERB_ALGO, "cachedb msg adjusted down by %d", (int)adjust);
    499  1.1.1.2  christos 	adjust_msg_ttl(qstate->return_msg, adjust);
    500      1.1  christos 
    501  1.1.1.2  christos 	/* Similar to the unbound worker, if serve-expired is enabled and
    502  1.1.1.2  christos 	 * the msg would be considered to be expired, mark the state so a
    503  1.1.1.2  christos 	 * refetch will be scheduled.  The comparison between 'expiry' and
    504  1.1.1.2  christos 	 * 'now' should be redundant given how these values were calculated,
    505  1.1.1.2  christos 	 * but we check it just in case as does good_expiry_and_qinfo(). */
    506  1.1.1.2  christos 	if(qstate->env->cfg->serve_expired &&
    507  1.1.1.2  christos 		(adjust == -1 || (time_t)expiry < *qstate->env->now)) {
    508  1.1.1.2  christos 		qstate->need_refetch = 1;
    509  1.1.1.2  christos 	}
    510  1.1.1.2  christos 
    511  1.1.1.2  christos 	return 1;
    512      1.1  christos }
    513      1.1  christos 
    514      1.1  christos /**
    515      1.1  christos  * Lookup the qstate.qinfo in extcache, store in qstate.return_msg.
    516      1.1  christos  * return true if lookup was successful.
    517      1.1  christos  */
    518      1.1  christos static int
    519      1.1  christos cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie)
    520      1.1  christos {
    521      1.1  christos 	char key[(CACHEDB_HASHSIZE/8)*2+1];
    522      1.1  christos 	calc_hash(qstate, key, sizeof(key));
    523      1.1  christos 
    524      1.1  christos 	/* call backend to fetch data for key into scratch buffer */
    525      1.1  christos 	if( !(*ie->backend->lookup)(qstate->env, ie, key,
    526      1.1  christos 		qstate->env->scratch_buffer)) {
    527      1.1  christos 		return 0;
    528      1.1  christos 	}
    529      1.1  christos 
    530      1.1  christos 	/* check expiry date and check if query-data matches */
    531      1.1  christos 	if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) {
    532      1.1  christos 		return 0;
    533      1.1  christos 	}
    534      1.1  christos 
    535      1.1  christos 	/* parse dns message into return_msg */
    536      1.1  christos 	if( !parse_data(qstate, qstate->env->scratch_buffer) ) {
    537      1.1  christos 		return 0;
    538      1.1  christos 	}
    539      1.1  christos 	return 1;
    540      1.1  christos }
    541      1.1  christos 
    542      1.1  christos /**
    543      1.1  christos  * Store the qstate.return_msg in extcache for key qstate.info
    544      1.1  christos  */
    545      1.1  christos static void
    546      1.1  christos cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie)
    547      1.1  christos {
    548      1.1  christos 	char key[(CACHEDB_HASHSIZE/8)*2+1];
    549      1.1  christos 	calc_hash(qstate, key, sizeof(key));
    550      1.1  christos 
    551      1.1  christos 	/* prepare data in scratch buffer */
    552      1.1  christos 	if(!prep_data(qstate, qstate->env->scratch_buffer))
    553      1.1  christos 		return;
    554      1.1  christos 
    555      1.1  christos 	/* call backend */
    556      1.1  christos 	(*ie->backend->store)(qstate->env, ie, key,
    557      1.1  christos 		sldns_buffer_begin(qstate->env->scratch_buffer),
    558      1.1  christos 		sldns_buffer_limit(qstate->env->scratch_buffer));
    559      1.1  christos }
    560      1.1  christos 
    561      1.1  christos /**
    562      1.1  christos  * See if unbound's internal cache can answer the query
    563      1.1  christos  */
    564      1.1  christos static int
    565      1.1  christos cachedb_intcache_lookup(struct module_qstate* qstate)
    566      1.1  christos {
    567      1.1  christos 	struct dns_msg* msg;
    568      1.1  christos 	msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname,
    569      1.1  christos 		qstate->qinfo.qname_len, qstate->qinfo.qtype,
    570      1.1  christos 		qstate->qinfo.qclass, qstate->query_flags,
    571      1.1  christos 		qstate->region, qstate->env->scratch);
    572      1.1  christos 	if(!msg && qstate->env->neg_cache) {
    573      1.1  christos 		/* lookup in negative cache; may result in
    574      1.1  christos 		 * NOERROR/NODATA or NXDOMAIN answers that need validation */
    575      1.1  christos 		msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo,
    576      1.1  christos 			qstate->region, qstate->env->rrset_cache,
    577      1.1  christos 			qstate->env->scratch_buffer,
    578      1.1  christos 			*qstate->env->now, 1/*add SOA*/, NULL);
    579      1.1  christos 	}
    580      1.1  christos 	if(!msg)
    581      1.1  christos 		return 0;
    582      1.1  christos 	/* this is the returned msg */
    583      1.1  christos 	qstate->return_rcode = LDNS_RCODE_NOERROR;
    584      1.1  christos 	qstate->return_msg = msg;
    585      1.1  christos 	return 1;
    586      1.1  christos }
    587      1.1  christos 
    588      1.1  christos /**
    589      1.1  christos  * Store query into the internal cache of unbound.
    590      1.1  christos  */
    591      1.1  christos static void
    592      1.1  christos cachedb_intcache_store(struct module_qstate* qstate)
    593      1.1  christos {
    594  1.1.1.2  christos 	uint32_t store_flags = qstate->query_flags;
    595  1.1.1.2  christos 
    596  1.1.1.2  christos 	if(qstate->env->cfg->serve_expired)
    597  1.1.1.2  christos 		store_flags |= DNSCACHE_STORE_ZEROTTL;
    598      1.1  christos 	if(!qstate->return_msg)
    599      1.1  christos 		return;
    600      1.1  christos 	(void)dns_cache_store(qstate->env, &qstate->qinfo,
    601      1.1  christos 		qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0,
    602  1.1.1.2  christos 		qstate->region, store_flags);
    603      1.1  christos }
    604      1.1  christos 
    605      1.1  christos /**
    606      1.1  christos  * Handle a cachedb module event with a query
    607      1.1  christos  * @param qstate: query state (from the mesh), passed between modules.
    608      1.1  christos  * 	contains qstate->env module environment with global caches and so on.
    609      1.1  christos  * @param iq: query state specific for this module.  per-query.
    610      1.1  christos  * @param ie: environment specific for this module.  global.
    611      1.1  christos  * @param id: module id.
    612      1.1  christos  */
    613      1.1  christos static void
    614      1.1  christos cachedb_handle_query(struct module_qstate* qstate,
    615      1.1  christos 	struct cachedb_qstate* ATTR_UNUSED(iq),
    616      1.1  christos 	struct cachedb_env* ie, int id)
    617      1.1  christos {
    618      1.1  christos 	/* check if we are enabled, and skip if so */
    619      1.1  christos 	if(!ie->enabled) {
    620      1.1  christos 		/* pass request to next module */
    621      1.1  christos 		qstate->ext_state[id] = module_wait_module;
    622      1.1  christos 		return;
    623      1.1  christos 	}
    624      1.1  christos 
    625  1.1.1.2  christos 	if(qstate->blacklist || qstate->no_cache_lookup) {
    626  1.1.1.2  christos 		/* cache is blacklisted or we are instructed from edns to not look */
    627      1.1  christos 		/* pass request to next module */
    628      1.1  christos 		qstate->ext_state[id] = module_wait_module;
    629      1.1  christos 		return;
    630      1.1  christos 	}
    631      1.1  christos 
    632      1.1  christos 	/* lookup inside unbound's internal cache */
    633      1.1  christos 	if(cachedb_intcache_lookup(qstate)) {
    634  1.1.1.2  christos 		if(verbosity >= VERB_ALGO) {
    635  1.1.1.2  christos 			if(qstate->return_msg->rep)
    636  1.1.1.2  christos 				log_dns_msg("cachedb internal cache lookup",
    637  1.1.1.2  christos 					&qstate->return_msg->qinfo,
    638  1.1.1.2  christos 					qstate->return_msg->rep);
    639  1.1.1.2  christos 			else log_info("cachedb internal cache lookup: rcode %s",
    640  1.1.1.2  christos 				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)?
    641  1.1.1.2  christos 				sldns_lookup_by_id(sldns_rcodes, qstate->return_rcode)->name:"??");
    642  1.1.1.2  christos 		}
    643      1.1  christos 		/* we are done with the query */
    644      1.1  christos 		qstate->ext_state[id] = module_finished;
    645      1.1  christos 		return;
    646      1.1  christos 	}
    647      1.1  christos 
    648      1.1  christos 	/* ask backend cache to see if we have data */
    649      1.1  christos 	if(cachedb_extcache_lookup(qstate, ie)) {
    650      1.1  christos 		if(verbosity >= VERB_ALGO)
    651      1.1  christos 			log_dns_msg(ie->backend->name,
    652      1.1  christos 				&qstate->return_msg->qinfo,
    653      1.1  christos 				qstate->return_msg->rep);
    654      1.1  christos 		/* store this result in internal cache */
    655      1.1  christos 		cachedb_intcache_store(qstate);
    656      1.1  christos 		/* we are done with the query */
    657      1.1  christos 		qstate->ext_state[id] = module_finished;
    658      1.1  christos 		return;
    659      1.1  christos 	}
    660      1.1  christos 
    661      1.1  christos 	/* no cache fetches */
    662      1.1  christos 	/* pass request to next module */
    663      1.1  christos 	qstate->ext_state[id] = module_wait_module;
    664      1.1  christos }
    665      1.1  christos 
    666      1.1  christos /**
    667      1.1  christos  * Handle a cachedb module event with a response from the iterator.
    668      1.1  christos  * @param qstate: query state (from the mesh), passed between modules.
    669      1.1  christos  * 	contains qstate->env module environment with global caches and so on.
    670      1.1  christos  * @param iq: query state specific for this module.  per-query.
    671      1.1  christos  * @param ie: environment specific for this module.  global.
    672      1.1  christos  * @param id: module id.
    673      1.1  christos  */
    674      1.1  christos static void
    675      1.1  christos cachedb_handle_response(struct module_qstate* qstate,
    676      1.1  christos 	struct cachedb_qstate* ATTR_UNUSED(iq), struct cachedb_env* ie, int id)
    677      1.1  christos {
    678  1.1.1.2  christos 	/* check if we are not enabled or instructed to not cache, and skip */
    679  1.1.1.2  christos 	if(!ie->enabled || qstate->no_cache_store) {
    680      1.1  christos 		/* we are done with the query */
    681      1.1  christos 		qstate->ext_state[id] = module_finished;
    682      1.1  christos 		return;
    683      1.1  christos 	}
    684      1.1  christos 
    685      1.1  christos 	/* store the item into the backend cache */
    686      1.1  christos 	cachedb_extcache_store(qstate, ie);
    687      1.1  christos 
    688      1.1  christos 	/* we are done with the query */
    689      1.1  christos 	qstate->ext_state[id] = module_finished;
    690      1.1  christos }
    691      1.1  christos 
    692      1.1  christos void
    693      1.1  christos cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id,
    694      1.1  christos 	struct outbound_entry* outbound)
    695      1.1  christos {
    696      1.1  christos 	struct cachedb_env* ie = (struct cachedb_env*)qstate->env->modinfo[id];
    697      1.1  christos 	struct cachedb_qstate* iq = (struct cachedb_qstate*)qstate->minfo[id];
    698      1.1  christos 	verbose(VERB_QUERY, "cachedb[module %d] operate: extstate:%s event:%s",
    699      1.1  christos 		id, strextstate(qstate->ext_state[id]), strmodulevent(event));
    700      1.1  christos 	if(iq) log_query_info(VERB_QUERY, "cachedb operate: query",
    701      1.1  christos 		&qstate->qinfo);
    702      1.1  christos 
    703      1.1  christos 	/* perform cachedb state machine */
    704      1.1  christos 	if((event == module_event_new || event == module_event_pass) &&
    705      1.1  christos 		iq == NULL) {
    706      1.1  christos 		if(!cachedb_new(qstate, id)) {
    707      1.1  christos 			(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
    708      1.1  christos 			return;
    709      1.1  christos 		}
    710      1.1  christos 		iq = (struct cachedb_qstate*)qstate->minfo[id];
    711      1.1  christos 	}
    712      1.1  christos 	if(iq && (event == module_event_pass || event == module_event_new)) {
    713      1.1  christos 		cachedb_handle_query(qstate, iq, ie, id);
    714      1.1  christos 		return;
    715      1.1  christos 	}
    716      1.1  christos 	if(iq && (event == module_event_moddone)) {
    717      1.1  christos 		cachedb_handle_response(qstate, iq, ie, id);
    718      1.1  christos 		return;
    719      1.1  christos 	}
    720      1.1  christos 	if(iq && outbound) {
    721      1.1  christos 		/* cachedb does not need to process responses at this time
    722      1.1  christos 		 * ignore it.
    723      1.1  christos 		cachedb_process_response(qstate, iq, ie, id, outbound, event);
    724      1.1  christos 		*/
    725      1.1  christos 		return;
    726      1.1  christos 	}
    727      1.1  christos 	if(event == module_event_error) {
    728      1.1  christos 		verbose(VERB_ALGO, "got called with event error, giving up");
    729      1.1  christos 		(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
    730      1.1  christos 		return;
    731      1.1  christos 	}
    732  1.1.1.2  christos 	if(!iq && (event == module_event_moddone)) {
    733  1.1.1.2  christos 		/* during priming, module done but we never started */
    734  1.1.1.2  christos 		qstate->ext_state[id] = module_finished;
    735  1.1.1.2  christos 		return;
    736  1.1.1.2  christos 	}
    737      1.1  christos 
    738      1.1  christos 	log_err("bad event for cachedb");
    739      1.1  christos 	(void)error_response(qstate, id, LDNS_RCODE_SERVFAIL);
    740      1.1  christos }
    741      1.1  christos 
    742      1.1  christos void
    743      1.1  christos cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate),
    744      1.1  christos 	int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super))
    745      1.1  christos {
    746      1.1  christos 	/* cachedb does not use subordinate requests at this time */
    747      1.1  christos 	verbose(VERB_ALGO, "cachedb inform_super was called");
    748      1.1  christos }
    749      1.1  christos 
    750      1.1  christos void
    751      1.1  christos cachedb_clear(struct module_qstate* qstate, int id)
    752      1.1  christos {
    753      1.1  christos 	struct cachedb_qstate* iq;
    754      1.1  christos 	if(!qstate)
    755      1.1  christos 		return;
    756      1.1  christos 	iq = (struct cachedb_qstate*)qstate->minfo[id];
    757      1.1  christos 	if(iq) {
    758      1.1  christos 		/* free contents of iq */
    759      1.1  christos 		/* TODO */
    760      1.1  christos 	}
    761      1.1  christos 	qstate->minfo[id] = NULL;
    762      1.1  christos }
    763      1.1  christos 
    764      1.1  christos size_t
    765      1.1  christos cachedb_get_mem(struct module_env* env, int id)
    766      1.1  christos {
    767      1.1  christos 	struct cachedb_env* ie = (struct cachedb_env*)env->modinfo[id];
    768      1.1  christos 	if(!ie)
    769      1.1  christos 		return 0;
    770      1.1  christos 	return sizeof(*ie); /* TODO - more mem */
    771      1.1  christos }
    772      1.1  christos 
    773      1.1  christos /**
    774      1.1  christos  * The cachedb function block
    775      1.1  christos  */
    776      1.1  christos static struct module_func_block cachedb_block = {
    777      1.1  christos 	"cachedb",
    778      1.1  christos 	&cachedb_init, &cachedb_deinit, &cachedb_operate,
    779      1.1  christos 	&cachedb_inform_super, &cachedb_clear, &cachedb_get_mem
    780      1.1  christos };
    781      1.1  christos 
    782      1.1  christos struct module_func_block*
    783      1.1  christos cachedb_get_funcblock(void)
    784      1.1  christos {
    785      1.1  christos 	return &cachedb_block;
    786      1.1  christos }
    787      1.1  christos #endif /* USE_CACHEDB */
    788