ssu.c revision 15a44745412679c30a6d022733925af70a38b715
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * Copyright (C) 2000 Internet Software Consortium.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt * Permission to use, copy, modify, and distribute this software for any
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * purpose with or without fee is hereby granted, provided that the above
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * copyright notice and this permission notice appear in all copies.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt/*
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * $Id: ssu.c,v 1.12 2000/07/27 09:46:37 tale Exp $
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt * Principal Author: Brian Wellington
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <config.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/magic.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/mem.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/string.h> /* Required for HP/UX (and others?) */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <isc/util.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <dns/name.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#include <dns/ssu.h>
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#define SSUTABLEMAGIC 0x53535554 /* SSUT */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#define VALID_SSUTABLE(table) ISC_MAGIC_VALID((table), SSUTABLEMAGIC)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#define SSURULEMAGIC 0x53535552 /* SSUR */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt#define VALID_SSURULE(table) ISC_MAGIC_VALID((table), SSURULEMAGIC)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstruct dns_ssurule {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_uint32_t magic;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_boolean_t grant; /* is this a grant or a deny? */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt unsigned int matchtype; /* which type of pattern match? */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_name_t *identity; /* the identity to match */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_name_t *name; /* the name being updated */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt unsigned int ntypes; /* number of data types covered */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_rdatatype_t *types; /* the data types. Can include ANY, */
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt /* defaults to all but SIG,SOA,NS if NULL*/
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ISC_LINK(dns_ssurule_t) link;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt};
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstruct dns_ssutable {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_uint32_t magic;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_t *mctx;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt unsigned int references;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mutex_t lock;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ISC_LIST(dns_ssurule_t) rules;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt};
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssutable_create(isc_mem_t *mctx, dns_ssutable_t **tablep) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_result_t result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_ssutable_t *table;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(tablep != NULL && *tablep == NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(mctx != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt table = isc_mem_get(mctx, sizeof(dns_ssutable_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (table == NULL)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_NOMEMORY);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = isc_mutex_init(&table->lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_put(mctx, table, sizeof(dns_ssutable_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt table->references = 1;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt table->mctx = mctx;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ISC_LIST_INIT(table->rules);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt table->magic = SSUTABLEMAGIC;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *tablep = table;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_SUCCESS);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic inline void
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdestroy(dns_ssutable_t *table) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_t *mctx;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSUTABLE(table));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt mctx = table->mctx;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt while (!ISC_LIST_EMPTY(table->rules)) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_ssurule_t *rule = ISC_LIST_HEAD(table->rules);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->identity != NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_name_free(rule->identity, mctx);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_put(mctx, rule->identity, sizeof(dns_name_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->name != NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_name_free(rule->name, mctx);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_put(mctx, rule->name, sizeof(dns_name_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->types != NULL)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_put(mctx, rule->types,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->ntypes * sizeof(dns_rdatatype_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ISC_LIST_UNLINK(table->rules, rule, link);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->magic = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_put(mctx, rule, sizeof(dns_ssurule_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mutex_destroy(&table->lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt table->magic = 0;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_put(mctx, table, sizeof(dns_ssutable_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssutable_attach(dns_ssutable_t *source, dns_ssutable_t **targetp) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSUTABLE(source));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(targetp != NULL && *targetp == NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt LOCK(&source->lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INSIST(source->references > 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt source->references++;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INSIST(source->references != 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNLOCK(&source->lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *targetp = source;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntvoid
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssutable_detach(dns_ssutable_t **tablep) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_ssutable_t *table;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_boolean_t done = ISC_FALSE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(tablep != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt table = *tablep;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSUTABLE(table));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt LOCK(&table->lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt INSIST(table->references > 0);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (--table->references == 0)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt done = ISC_TRUE;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt UNLOCK(&table->lock);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *tablep = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (done)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt destroy(table);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssutable_addrule(dns_ssutable_t *table, isc_boolean_t grant,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_name_t *identity, unsigned int matchtype,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_name_t *name, unsigned int ntypes,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_rdatatype_t *types)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_ssurule_t *rule;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_t *mctx;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_result_t result;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSUTABLE(table));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(dns_name_isabsolute(identity));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(dns_name_isabsolute(name));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(matchtype <= DNS_SSUMATCHTYPE_SELF);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (matchtype == DNS_SSUMATCHTYPE_WILDCARD)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(dns_name_iswildcard(name));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (ntypes > 0)
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt REQUIRE(types != NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt mctx = table->mctx;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule = isc_mem_get(mctx, sizeof(dns_ssurule_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule == NULL)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_R_NOMEMORY);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->identity = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->name = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->types = NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->grant = grant;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->identity = isc_mem_get(mctx, sizeof(dns_name_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->identity == NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = ISC_R_NOMEMORY;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto failure;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_name_init(rule->identity, NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = dns_name_dup(identity, mctx, rule->identity);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto failure;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->name = isc_mem_get(mctx, sizeof(dns_name_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->name == NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = ISC_R_NOMEMORY;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto failure;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_name_init(rule->name, NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = dns_name_dup(name, mctx, rule->name);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (result != ISC_R_SUCCESS)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto failure;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->matchtype = matchtype;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->ntypes = ntypes;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (ntypes > 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->types = isc_mem_get(mctx,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ntypes * sizeof(dns_rdatatype_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->types == NULL) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt result = ISC_R_NOMEMORY;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt goto failure;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt memcpy(rule->types, types, ntypes * sizeof(dns_rdatatype_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt else
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt rule->types = NULL;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt rule->magic = SSURULEMAGIC;
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt ISC_LIST_APPEND(table->rules, rule, link);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt return (ISC_R_SUCCESS);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt failure:
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (rule->identity != NULL) {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (dns_name_dynamic(rule->identity))
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt dns_name_free(rule->identity, mctx);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt isc_mem_put(mctx, rule->identity, sizeof(dns_name_t));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (rule->name != NULL) {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt if (dns_name_dynamic(rule->name))
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt dns_name_free(rule->name, mctx);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt isc_mem_put(mctx, rule->name, sizeof(dns_name_t));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->types != NULL)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_put(mctx, rule->types,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt ntypes * sizeof(dns_rdatatype_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt isc_mem_put(mctx, rule, sizeof(dns_ssurule_t));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (result);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntstatic inline isc_boolean_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisusertype(dns_rdatatype_t type) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_TF(type != dns_rdatatype_ns &&
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt type != dns_rdatatype_soa &&
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt type != dns_rdatatype_sig));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_boolean_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssutable_checkrules(dns_ssutable_t *table, dns_name_t *signer,
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_name_t *name, dns_rdatatype_t type)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt{
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt dns_ssurule_t *rule;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt unsigned int i;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSUTABLE(table));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(signer == NULL || dns_name_isabsolute(signer));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(dns_name_isabsolute(name));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (signer == NULL)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_FALSE);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule = ISC_LIST_HEAD(table->rules);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule = ISC_LIST_NEXT(rule, link);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt for (rule = ISC_LIST_HEAD(table->rules);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule != NULL;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule = ISC_LIST_NEXT(rule, link))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (dns_name_iswildcard(rule->identity)) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (!dns_name_matcheswildcard(signer, rule->identity))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt continue;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (!dns_name_equal(signer, rule->identity))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt continue;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->matchtype == DNS_SSUMATCHTYPE_NAME) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (!dns_name_equal(name, rule->name))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt continue;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else if (rule->matchtype == DNS_SSUMATCHTYPE_SUBDOMAIN) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (!dns_name_issubdomain(name, rule->name))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt continue;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else if (rule->matchtype == DNS_SSUMATCHTYPE_WILDCARD) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (!dns_name_matcheswildcard(name, rule->name))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt continue;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else if (rule->matchtype == DNS_SSUMATCHTYPE_SELF) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (!dns_name_equal(signer, name))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt continue;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->ntypes == 0) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (!isusertype(type))
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt continue;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt else {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt for (i = 0; i < rule->ntypes; i++) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (rule->types[i] == dns_rdatatype_any ||
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt rule->types[i] == type)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt break;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt if (i == rule->ntypes)
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt continue;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (rule->grant);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt }
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (ISC_FALSE);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_boolean_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssurule_isgrant(const dns_ssurule_t *rule) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSURULE(rule));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (rule->grant);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_name_t *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssurule_identity(const dns_ssurule_t *rule) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSURULE(rule));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (rule->identity);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntunsigned int
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssurule_matchtype(const dns_ssurule_t *rule) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSURULE(rule));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (rule->matchtype);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_name_t *
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssurule_name(const dns_ssurule_t *rule) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSURULE(rule));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt return (rule->name);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt}
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntunsigned int
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Huntdns_ssurule_types(const dns_ssurule_t *rule, dns_rdatatype_t **types) {
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt REQUIRE(VALID_SSURULE(rule));
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt REQUIRE(types != NULL && *types != NULL);
a747113422afaa29ce72d2c5ba7f0b7ea9ec2054Evan Hunt *types = rule->types;
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (rule->ntypes);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssutable_firstrule(const dns_ssutable_t *table, dns_ssurule_t **rule) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSUTABLE(table));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(rule != NULL && *rule == NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *rule = ISC_LIST_HEAD(table->rules);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (*rule != NULL ? ISC_R_SUCCESS : ISC_R_NOMORE);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntisc_result_t
ef421f66f47224a42073deaf087378c5d0c9952eEvan Huntdns_ssutable_nextrule(dns_ssurule_t *rule, dns_ssurule_t **nextrule) {
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(VALID_SSURULE(rule));
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt REQUIRE(nextrule != NULL && *nextrule == NULL);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt *nextrule = ISC_LIST_NEXT(rule, link);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt return (*nextrule != NULL ? ISC_R_SUCCESS : ISC_R_NOMORE);
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt}
ef421f66f47224a42073deaf087378c5d0c9952eEvan Hunt