diff --git a/Makefile.in b/Makefile.in index 30d2e4213..11551c128 100644 --- a/Makefile.in +++ b/Makefile.in @@ -721,7 +721,7 @@ modstack.lo modstack.o: $(srcdir)/services/modstack.c config.h $(srcdir)/service $(srcdir)/sldns/pkthdr.h $(srcdir)/sldns/rrdef.h $(srcdir)/util/fptr_wlist.h $(srcdir)/util/netevent.h \ $(srcdir)/util/tube.h $(srcdir)/services/mesh.h $(srcdir)/util/rbtree.h $(srcdir)/dns64/dns64.h \ $(srcdir)/iterator/iterator.h $(srcdir)/services/outbound_list.h $(srcdir)/validator/validator.h \ - $(srcdir)/validator/val_utils.h + $(srcdir)/validator/val_utils.h $(srcdir)/cachedb/cachedb.h outbound_list.lo outbound_list.o: $(srcdir)/services/outbound_list.c config.h \ $(srcdir)/services/outbound_list.h $(srcdir)/services/outside_network.h $(srcdir)/util/rbtree.h \ $(srcdir)/util/netevent.h @@ -765,7 +765,7 @@ fptr_wlist.lo fptr_wlist.o: $(srcdir)/util/fptr_wlist.c config.h $(srcdir)/util/ $(srcdir)/validator/val_nsec3.h $(srcdir)/validator/val_sigcrypt.h $(srcdir)/validator/val_kentry.h \ $(srcdir)/validator/val_neg.h $(srcdir)/validator/autotrust.h $(srcdir)/libunbound/libworker.h \ $(srcdir)/libunbound/context.h $(srcdir)/util/alloc.h $(srcdir)/libunbound/unbound.h \ - $(srcdir)/libunbound/worker.h $(srcdir)/sldns/sbuffer.h $(srcdir)/util/config_file.h + $(srcdir)/libunbound/worker.h $(srcdir)/sldns/sbuffer.h $(srcdir)/util/config_file.h $(srcdir)/cachedb/cachedb.h locks.lo locks.o: $(srcdir)/util/locks.c config.h $(srcdir)/util/locks.h $(srcdir)/util/log.h log.lo log.o: $(srcdir)/util/log.c config.h $(srcdir)/util/log.h $(srcdir)/util/locks.h $(srcdir)/sldns/sbuffer.h mini_event.lo mini_event.o: $(srcdir)/util/mini_event.c config.h $(srcdir)/util/mini_event.h $(srcdir)/util/rbtree.h \ @@ -902,7 +902,9 @@ dns64.lo dns64.o: $(srcdir)/dns64/dns64.c config.h $(srcdir)/dns64/dns64.h $(src cachedb.lo cachedb.o: $(srcdir)/cachedb/cachedb.c config.h $(srcdir)/cachedb/cachedb.h $(srcdir)/util/module.h \ $(srcdir)/util/storage/lruhash.h $(srcdir)/util/locks.h $(srcdir)/util/log.h $(srcdir)/util/data/msgreply.h \ $(srcdir)/util/data/packed_rrset.h $(srcdir)/util/data/msgparse.h $(srcdir)/sldns/pkthdr.h \ - $(srcdir)/sldns/rrdef.h + $(srcdir)/sldns/rrdef.h $(srcdir)/util/regional.h $(srcdir)/util/config_file.h $(srcdir)/services/cache/dns.h \ + $(srcdir)/validator/val_neg.h $(srcdir)/util/rbtree.h $(srcdir)/validator/val_secalgo.h \ + $(srcdir)/sldns/parseutil.h $(srcdir)/sldns/wire2str.h $(srcdir)/sldns/sbuffer.h checklocks.lo checklocks.o: $(srcdir)/testcode/checklocks.c config.h $(srcdir)/util/locks.h $(srcdir)/util/log.h \ $(srcdir)/testcode/checklocks.h unitanchor.lo unitanchor.o: $(srcdir)/testcode/unitanchor.c config.h $(srcdir)/util/log.h $(srcdir)/util/data/dname.h \ diff --git a/cachedb/cachedb.c b/cachedb/cachedb.c index 719dc220c..b57025849 100644 --- a/cachedb/cachedb.c +++ b/cachedb/cachedb.c @@ -44,8 +44,16 @@ #ifdef USE_CACHEDB #include "cachedb/cachedb.h" #include "util/regional.h" +#include "util/config_file.h" +#include "util/data/msgreply.h" +#include "services/cache/dns.h" +#include "validator/val_neg.h" +#include "validator/val_secalgo.h" #include "sldns/parseutil.h" #include "sldns/wire2str.h" +#include "sldns/sbuffer.h" + +#define CACHEDB_HASHSIZE 256 /* bit hash */ /** apply configuration to cachedb module 'global' state */ static int @@ -69,6 +77,15 @@ cachedb_init(struct module_env* env, int id) log_err("cachedb: could not apply configuration settings."); return 0; } + /* see if a backend is selected */ + if(!cachedb_env->backend || !cachedb_env->backend->name) + return 1; + if(!(*cachedb_env->backend->init)(env, cachedb_env)) { + log_err("cachedb: could not init %s backend", + cachedb_env->backend->name); + return 0; + } + cachedb_env->enabled = 1; return 1; } @@ -81,6 +98,9 @@ cachedb_deinit(struct module_env* env, int id) cachedb_env = (struct cachedb_env*)env->modinfo[id]; /* free contents */ /* TODO */ + if(cachedb_env->enabled) { + (*cachedb_env->backend->deinit)(env, cachedb_env); + } free(cachedb_env); env->modinfo[id] = NULL; @@ -123,7 +143,166 @@ error_response(struct module_qstate* qstate, int id, int rcode) } /** - * Handle a cachedb module event + * Hash the query name, type, class and dbacess-secret into lookup buffer. + * @param qstate: query state with query info + * and env->cfg with secret. + * @param buf: returned buffer with hash to lookup + * @param len: length of the buffer. + */ +static void +calc_hash(struct module_qstate* qstate, char* buf, size_t len) +{ + uint8_t clear[1024]; + size_t clen = 0; + uint8_t hash[CACHEDB_HASHSIZE/8]; + const char* hex = "0123456789ABCDEF"; + const char* secret = "default"; /* TODO: from qstate->env->cfg */ + size_t i; + + /* copy the hash info into the clear buffer */ + if(clen + qstate->qinfo.qname_len < sizeof(clear)) { + memmove(clear+clen, qstate->qinfo.qname, + qstate->qinfo.qname_len); + clen += qstate->qinfo.qname_len; + } + if(clen + 4 < sizeof(clear)) { + uint16_t t = htons(qstate->qinfo.qtype); + uint16_t c = htons(qstate->qinfo.qclass); + memmove(clear+clen, &t, 2); + memmove(clear+clen+2, &c, 2); + clen += 4; + } + if(secret && secret[0] && clen + strlen(secret) < sizeof(clear)) { + memmove(clear+clen, secret, strlen(secret)); + clen += strlen(secret); + } + + /* hash the buffer */ + secalgo_hash_sha256(clear, clen, hash); + memset(clear, 0, clen); + + /* hex encode output for portability (some online dbs need + * no nulls, no control characters, and so on) */ + log_assert(len >= sizeof(hash)*2 + 1); + (void)len; + for(i=0; i>4]; + buf[i*2+1] = hex[hash[i]&0x0f]; + } + buf[sizeof(hash)*2] = 0; +} + +/** convert data from return_msg into the data buffer */ +static void +prep_data(struct module_qstate* qstate, struct sldns_buffer* buf) +{ + /* TODO */ +} + +/** check expiry and query details, return true if matches OK */ +static int +good_expiry_and_qinfo(struct module_qstate* qstate, struct sldns_buffer* buf) +{ + /* TODO */ + return 0; +} + +/** convert dns message in buffer to return_msg */ +static int +parse_data(struct module_qstate* qstate, struct sldns_buffer* buf) +{ + /* TODO */ + return 0; +} + +/** + * Lookup the qstate.qinfo in extcache, store in qstate.return_msg. + * return true if lookup was successful. + */ +static int +cachedb_extcache_lookup(struct module_qstate* qstate, struct cachedb_env* ie) +{ + char key[(CACHEDB_HASHSIZE/8)*2+1]; + calc_hash(qstate, key, sizeof(key)); + + /* call backend to fetch data for key into scratch buffer */ + if( !(*ie->backend->lookup)(qstate->env, ie, key, + qstate->env->scratch_buffer)) { + return 0; + } + + /* check expiry date and check if query-data matches */ + if( !good_expiry_and_qinfo(qstate, qstate->env->scratch_buffer) ) { + return 0; + } + + /* parse dns message into return_msg */ + if( !parse_data(qstate, qstate->env->scratch_buffer) ) { + return 0; + } + return 1; +} + +/** + * Store the qstate.return_msg in extcache for key qstate.info + */ +static void +cachedb_extcache_store(struct module_qstate* qstate, struct cachedb_env* ie) +{ + char key[(CACHEDB_HASHSIZE/8)*2+1]; + calc_hash(qstate, key, sizeof(key)); + + /* prepare data in scratch buffer */ + prep_data(qstate, qstate->env->scratch_buffer); + + /* call backend */ + (*ie->backend->store)(qstate->env, ie, key, + sldns_buffer_begin(qstate->env->scratch_buffer), + sldns_buffer_limit(qstate->env->scratch_buffer)); +} + +/** + * See if unbound's internal cache can answer the query + */ +static int +cachedb_intcache_lookup(struct module_qstate* qstate) +{ + struct dns_msg* msg; + msg = dns_cache_lookup(qstate->env, qstate->qinfo.qname, + qstate->qinfo.qname_len, qstate->qinfo.qtype, + qstate->qinfo.qclass, qstate->query_flags, + qstate->region, qstate->env->scratch); + if(!msg && qstate->env->neg_cache) { + /* lookup in negative cache; may result in + * NOERROR/NODATA or NXDOMAIN answers that need validation */ + msg = val_neg_getmsg(qstate->env->neg_cache, &qstate->qinfo, + qstate->region, qstate->env->rrset_cache, + qstate->env->scratch_buffer, + *qstate->env->now, 1/*add SOA*/, NULL); + } + if(!msg) + return 0; + /* this is the returned msg */ + qstate->return_rcode = LDNS_RCODE_NOERROR; + qstate->return_msg = msg; + return 1; +} + +/** + * Store query into the internal cache of unbound. + */ +static void +cachedb_intcache_store(struct module_qstate* qstate) +{ + if(!qstate->return_msg) + return; + (void)dns_cache_store(qstate->env, &qstate->qinfo, + qstate->return_msg->rep, 0, qstate->prefetch_leeway, 0, + qstate->region, qstate->query_flags); +} + +/** + * Handle a cachedb module event with a query * @param qstate: query state (from the mesh), passed between modules. * contains qstate->env module environment with global caches and so on. * @param iq: query state specific for this module. per-query. @@ -131,11 +310,76 @@ error_response(struct module_qstate* qstate, int id, int rcode) * @param id: module id. */ static void -cachedb_handle(struct module_qstate* qstate, struct cachedb_qstate* iq, +cachedb_handle_query(struct module_qstate* qstate, struct cachedb_qstate* iq, struct cachedb_env* ie, int id) { - /* figure out if this is a lookup or a store moment */ - /* TODO */ + /* check if we are enabled, and skip if so */ + if(!ie->enabled) { + /* pass request to next module */ + qstate->ext_state[id] = module_wait_module; + return; + } + + if(qstate->blacklist) { + /* cache is blacklisted */ + /* pass request to next module */ + qstate->ext_state[id] = module_wait_module; + return; + } + + /* lookup inside unbound's internal cache */ + if(cachedb_intcache_lookup(qstate)) { + if(verbosity >= VERB_ALGO) + log_dns_msg("cachedb internal cache lookup", + &qstate->return_msg->qinfo, + qstate->return_msg->rep); + /* we are done with the query */ + qstate->ext_state[id] = module_finished; + return; + } + + /* ask backend cache to see if we have data */ + if(cachedb_extcache_lookup(qstate, ie)) { + if(verbosity >= VERB_ALGO) + log_dns_msg(ie->backend->name, + &qstate->return_msg->qinfo, + qstate->return_msg->rep); + /* store this result in internal cache */ + cachedb_intcache_store(qstate); + /* we are done with the query */ + qstate->ext_state[id] = module_finished; + return; + } + + /* no cache fetches */ + /* pass request to next module */ + qstate->ext_state[id] = module_wait_module; +} + +/** + * Handle a cachedb module event with a response from the iterator. + * @param qstate: query state (from the mesh), passed between modules. + * contains qstate->env module environment with global caches and so on. + * @param iq: query state specific for this module. per-query. + * @param ie: environment specific for this module. global. + * @param id: module id. + */ +static void +cachedb_handle_response(struct module_qstate* qstate, + struct cachedb_qstate* iq, struct cachedb_env* ie, int id) +{ + /* check if we are enabled, and skip if not */ + if(!ie->enabled) { + /* we are done with the query */ + qstate->ext_state[id] = module_finished; + return; + } + + /* store the item into the backend cache */ + cachedb_extcache_store(qstate, ie); + + /* we are done with the query */ + qstate->ext_state[id] = module_finished; } void @@ -157,11 +401,13 @@ cachedb_operate(struct module_qstate* qstate, enum module_ev event, int id, return; } iq = (struct cachedb_qstate*)qstate->minfo[id]; - cachedb_handle(qstate, iq, ie, id); + } + if(iq && (event == module_event_pass || event == module_event_new)) { + cachedb_handle_query(qstate, iq, ie, id); return; } - if(iq && event == module_event_pass) { - cachedb_handle(qstate, iq, ie, id); + if(iq && (event == module_event_moddone)) { + cachedb_handle_response(qstate, iq, ie, id); return; } if(iq && outbound) { @@ -186,6 +432,7 @@ cachedb_inform_super(struct module_qstate* ATTR_UNUSED(qstate), int ATTR_UNUSED(id), struct module_qstate* ATTR_UNUSED(super)) { /* cachedb does not use subordinate requests at this time */ + verbose(VERB_ALGO, "cachedb inform_super was called"); } void diff --git a/cachedb/cachedb.h b/cachedb/cachedb.h index 3f29df0d6..d477e90a7 100644 --- a/cachedb/cachedb.h +++ b/cachedb/cachedb.h @@ -40,6 +40,7 @@ * dns responses. */ #include "util/module.h" +struct cachedb_backend; /** * The global variable environment contents for the cachedb @@ -47,7 +48,14 @@ * Like database connections. */ struct cachedb_env { - int todo; + /** true is cachedb is enabled, the backend is turned on */ + int enabled; + + /** the backend routines */ + struct cachedb_backend* backend; + + /** backend specific data here */ + void* backend_data; }; /** @@ -57,6 +65,28 @@ struct cachedb_qstate { int todo; }; +/** + * Backend call routines + */ +struct cachedb_backend { + /** backend name */ + const char* name; + + /** Init(env, cachedb_env): false on setup failure */ + int (*init)(struct module_env*, struct cachedb_env*); + + /** Deinit - close db for program exit */ + void (*deinit)(struct module_env*, struct cachedb_env*); + + /** Lookup (env, cachedb_env, key, result_buffer): true if found */ + int (*lookup)(struct module_env*, struct cachedb_env*, char*, + struct sldns_buffer*); + + /** Store (env, cachedb_env, key, data, data_len) */ + void (*store)(struct module_env*, struct cachedb_env*, char*, + uint8_t*, size_t); +}; + /** Init the cachedb module */ int cachedb_init(struct module_env* env, int id); /** Deinit the cachedb module */ diff --git a/doc/Changelog b/doc/Changelog index 5440b7afd..3d6ee8b6e 100644 --- a/doc/Changelog +++ b/doc/Changelog @@ -1,3 +1,6 @@ +15 April 2016: Wouter + - cachedb module event handling design. + 14 April 2016: Wouter - cachedb module framework (empty). - iana portlist update. diff --git a/validator/val_secalgo.c b/validator/val_secalgo.c index 912448a2e..6ea82ba1c 100644 --- a/validator/val_secalgo.c +++ b/validator/val_secalgo.c @@ -98,6 +98,12 @@ secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, } } +void +secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) +{ + (void)SHA256(buf, len, res); +} + /** * Return size of DS digest according to its hash algorithm. * @param algo: DS digest algo. @@ -670,6 +676,12 @@ secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, } } +void +secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) +{ + (void)HASH_HashBuf(HASH_AlgSHA256, res, buf, (unsigned long)len); +} + size_t ds_digest_size_supported(int algo) { @@ -1262,6 +1274,12 @@ secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, } } +void +secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res) +{ + _digest_nettle(SHA256_DIGEST_SIZE, (uint8_t*)buf, len, res); +} + /** * Return size of DS digest according to its hash algorithm. * @param algo: DS digest algo. diff --git a/validator/val_secalgo.h b/validator/val_secalgo.h index 589f1f11d..52aaeb9f6 100644 --- a/validator/val_secalgo.h +++ b/validator/val_secalgo.h @@ -59,6 +59,14 @@ size_t nsec3_hash_algo_size_supported(int id); int secalgo_nsec3_hash(int algo, unsigned char* buf, size_t len, unsigned char* res); +/** + * Calculate the sha256 hash for the data buffer into the result. + * @param buf: buffer to digest. + * @param len: length of the buffer to digest. + * @param res: result is stored here (space 256/8 bytes). + */ +void secalgo_hash_sha256(unsigned char* buf, size_t len, unsigned char* res); + /** * Return size of DS digest according to its hash algorithm. * @param algo: DS digest algo.