keytable.c revision ea14aa24aa9f90cadb49cbaf492237b6cf2b04a7
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt/*
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Copyright (C) 2000 Internet Software Consortium.
0666e6db543cda2de2b8472ba49ed9b53c836326Tinderbox User *
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Permission to use, copy, modify, and distribute this software for any
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * purpose with or without fee is hereby granted, provided that the above
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * copyright notice and this permission notice appear in all copies.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt *
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * SOFTWARE.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <config.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <isc/mem.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <isc/rwlock.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <isc/string.h> /* Required for HP/UX (and others?) */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <isc/util.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <dns/keytable.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <dns/fixedname.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt#include <dns/rbt.h>
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt#include <dns/result.h>
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntstruct dns_keytable {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt /* Unlocked. */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt unsigned int magic;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_mem_t *mctx;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_mutex_t lock;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_rwlock_t rwlock;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt /* Locked by lock. */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_uint32_t active_nodes;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt /* Locked by rwlock. */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_uint32_t references;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt dns_rbt_t *table;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt};
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt#define KEYTABLE_MAGIC 0x4b54626cU /* KTbl */
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt#define VALID_KEYTABLE(kt) ISC_MAGIC_VALID(kt, KEYTABLE_MAGIC)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntstruct dns_keynode {
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt unsigned int magic;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt dst_key_t * key;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt struct dns_keynode * next;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt};
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt#define KEYNODE_MAGIC 0x4b4e6f64U /* KNod */
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt#define VALID_KEYNODE(kn) ISC_MAGIC_VALID(kn, KEYNODE_MAGIC)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntstatic void
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntfree_keynode(void *node, void *arg) {
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt dns_keynode_t *keynode = node;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_mem_t *mctx = arg;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(VALID_KEYNODE(keynode));
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt dst_key_free(&keynode->key);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt if (keynode->next != NULL)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt free_keynode(keynode->next, mctx);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_mem_put(mctx, keynode, sizeof(dns_keynode_t));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt}
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntisc_result_t
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntdns_keytable_create(isc_mem_t *mctx, dns_keytable_t **keytablep) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt dns_keytable_t *keytable;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_result_t result;
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt /*
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Create a keytable.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt REQUIRE(keytablep != NULL && *keytablep == NULL);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable = isc_mem_get(mctx, sizeof *keytable);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (keytable == NULL)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt return (ISC_R_NOMEMORY);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable->table = NULL;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_rbt_create(mctx, free_keynode, mctx, &keytable->table);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (result != ISC_R_SUCCESS)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt goto cleanup_keytable;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = isc_mutex_init(&keytable->lock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (result != ISC_R_SUCCESS) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt "isc_mutex_init() failed: %s",
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_result_totext(result));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = ISC_R_UNEXPECTED;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt goto cleanup_rbt;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt }
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = isc_rwlock_init(&keytable->rwlock, 0, 0);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (result != ISC_R_SUCCESS) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt "isc_rwlock_init() failed: %s",
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_result_totext(result));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = ISC_R_UNEXPECTED;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt goto cleanup_lock;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt }
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable->mctx = mctx;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable->active_nodes = 0;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable->references = 1;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable->magic = KEYTABLE_MAGIC;
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt *keytablep = keytable;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt return (ISC_R_SUCCESS);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt cleanup_lock:
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_mutex_destroy(&keytable->lock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt cleanup_rbt:
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt dns_rbt_destroy(&keytable->table);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt cleanup_keytable:
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_mem_put(mctx, keytable, sizeof *keytable);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt return (result);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt}
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntvoid
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntdns_keytable_attach(dns_keytable_t *source, dns_keytable_t **targetp) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt /*
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Attach *targetp to source.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt REQUIRE(VALID_KEYTABLE(source));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt REQUIRE(targetp != NULL && *targetp == NULL);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWLOCK(&source->rwlock, isc_rwlocktype_write);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt INSIST(source->references > 0);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt source->references++;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt INSIST(source->references != 0);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt RWUNLOCK(&source->rwlock, isc_rwlocktype_write);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt *targetp = source;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt}
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntvoid
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntdns_keytable_detach(dns_keytable_t **keytablep) {
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_boolean_t destroy = ISC_FALSE;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt dns_keytable_t *keytable;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt /*
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * Detach *keytablep from its keytable.
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt */
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(keytablep != NULL && VALID_KEYTABLE(*keytablep));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable = *keytablep;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWLOCK(&keytable->rwlock, isc_rwlocktype_write);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt INSIST(keytable->references > 0);
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt keytable->references--;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt LOCK(&keytable->lock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (keytable->references == 0 && keytable->active_nodes == 0)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt destroy = ISC_TRUE;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt UNLOCK(&keytable->lock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWUNLOCK(&keytable->rwlock, isc_rwlocktype_write);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (destroy) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt dns_rbt_destroy(&keytable->table);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_rwlock_destroy(&keytable->rwlock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_mutex_destroy(&keytable->lock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable->magic = 0;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_mem_put(keytable->mctx, keytable, sizeof *keytable);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt }
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt *keytablep = NULL;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt}
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntisc_result_t
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntdns_keytable_add(dns_keytable_t *keytable, dst_key_t **keyp) {
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_result_t result;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt dns_keynode_t *knode;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt dns_rbtnode_t *node;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt dns_fixedname_t fname;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt char *keyname;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_buffer_t buffer;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt size_t len;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt /*
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Add '*keyp' to 'keytable'.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt REQUIRE(VALID_KEYTABLE(keytable));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt REQUIRE(keyp != NULL);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keyname = dst_key_name(*keyp);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt INSIST(keyname != NULL);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt len = strlen(keyname);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_buffer_init(&buffer, keyname, len);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_buffer_add(&buffer, len);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt dns_fixedname_init(&fname);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_name_fromtext(dns_fixedname_name(&fname), &buffer,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt dns_rootname, ISC_FALSE, NULL);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (result != ISC_R_SUCCESS)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt return (result);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt knode = isc_mem_get(keytable->mctx, sizeof *knode);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt if (knode == NULL)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt return (ISC_R_NOMEMORY);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt RWLOCK(&keytable->rwlock, isc_rwlocktype_write);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt node = NULL;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt result = dns_rbt_addnode(keytable->table, dns_fixedname_name(&fname),
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt &node);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt knode->magic = KEYNODE_MAGIC;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt knode->key = *keyp;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt knode->next = node->data;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt node->data = knode;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt *keyp = NULL;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt knode = NULL;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = ISC_R_SUCCESS;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt }
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWUNLOCK(&keytable->rwlock, isc_rwlocktype_write);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (knode != NULL)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_mem_put(keytable->mctx, knode, sizeof *knode);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt return (result);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt}
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntisc_result_t
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Huntdns_keytable_findkeynode(dns_keytable_t *keytable, dns_name_t *name,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt dns_secalg_t algorithm, dns_keytag_t tag,
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt dns_keynode_t **keynodep)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt{
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_result_t result;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt dns_keynode_t *knode;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt void *data;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt /*
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * Search for a key named 'name', matching 'algorithm' and 'tag' in
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * 'keytable'.
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt */
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(VALID_KEYTABLE(keytable));
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(dns_name_isabsolute(name));
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(keynodep != NULL && *keynodep == NULL);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWLOCK(&keytable->rwlock, isc_rwlocktype_read);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt knode = NULL;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt data = NULL;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_rbt_findname(keytable->table, name, 0, NULL, &data);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt if (result == ISC_R_SUCCESS) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt INSIST(data != NULL);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt for (knode = data; knode != NULL; knode = knode->next) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (algorithm == (dns_secalg_t)dst_key_alg(knode->key)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt && tag == (dns_keytag_t)dst_key_id(knode->key))
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt break;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt }
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (knode != NULL) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt LOCK(&keytable->lock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable->active_nodes++;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt UNLOCK(&keytable->lock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt *keynodep = knode;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt } else
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt result = ISC_R_NOTFOUND;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt } else if (result == DNS_R_PARTIALMATCH)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt result = ISC_R_NOTFOUND;
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt return (result);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt}
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntisc_result_t
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntdns_keytable_finddeepestmatch(dns_keytable_t *keytable, dns_name_t *name,
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt dns_name_t *foundname)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt{
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt isc_result_t result;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt void *data;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt /*
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt * Search for the deepest match in 'keytable'.
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt */
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt REQUIRE(VALID_KEYTABLE(keytable));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt REQUIRE(dns_name_isabsolute(name));
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt REQUIRE(foundname != NULL);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWLOCK(&keytable->rwlock, isc_rwlocktype_read);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt data = NULL;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_rbt_findname(keytable->table, name, 0, foundname, &data);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH)
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = ISC_R_SUCCESS;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt return (result);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt}
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntvoid
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntdns_keytable_detachkeynode(dns_keytable_t *keytable, dns_keynode_t **keynodep)
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt{
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt /*
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * Give back a keynode found via dns_keytable_findkeynode().
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt */
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(VALID_KEYTABLE(keytable));
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(keynodep != NULL && VALID_KEYNODE(*keynodep));
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt LOCK(&keytable->lock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt INSIST(keytable->active_nodes > 0);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt keytable->active_nodes--;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt UNLOCK(&keytable->lock);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt *keynodep = NULL;
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt}
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Huntisc_result_t
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Huntdns_keytable_issecuredomain(dns_keytable_t *keytable, dns_name_t *name,
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt isc_boolean_t *wantdnssecp)
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt{
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt isc_result_t result;
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt void *data;
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt /*
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt * Is 'name' at or beneath a trusted key?
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt */
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt REQUIRE(VALID_KEYTABLE(keytable));
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt REQUIRE(dns_name_isabsolute(name));
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt REQUIRE(wantdnssecp != NULL);
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt RWLOCK(&keytable->rwlock, isc_rwlocktype_read);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt data = NULL;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = dns_rbt_findname(keytable->table, name, 0, NULL, &data);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt INSIST(data != NULL);
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt *wantdnssecp = ISC_TRUE;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = ISC_R_SUCCESS;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt } else if (result == ISC_R_NOTFOUND) {
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt *wantdnssecp = ISC_FALSE;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt result = ISC_R_SUCCESS;
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt }
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt return (result);
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt}
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntdst_key_t *
d46855caedd5cb101795707f6f467fa363ef1448Evan Huntdns_keynode_key(dns_keynode_t *keynode) {
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt /*
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt * Get the DST key associated with keynode.
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt */
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt REQUIRE(VALID_KEYNODE(keynode));
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt
d46855caedd5cb101795707f6f467fa363ef1448Evan Hunt return (keynode->key);
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt}
180319f572fb6c1ca7000d22ea79a8dd77ae3f91Evan Hunt
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Huntdns_keynode_t *
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Huntdns_keynode_next(dns_keynode_t *keynode) {
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt /*
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt * Get the next keynode in the list.
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt */
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt REQUIRE(VALID_KEYNODE(keynode));
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt return (keynode->next);
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt}
c0c4512020c0a4a9e5b087cb8cad1cd68fb3f52eEvan Hunt