ssu.c revision dafcb997e390efa4423883dafd100c975c4095d6
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * Copyright (C) 2000, 2001, 2003 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Permission to use, copy, modify, and distribute this software for any
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * purpose with or without fee is hereby granted, provided that the above
ec5347e2c775f027573ce5648b910361aa926c01Automatic Updater * copyright notice and this permission notice appear in all copies.
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington *
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
271c4c7ffafeb0bda21278af6cac4535c0193f18Automatic Updater * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews */
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington
cb3e854e181373807f7f011e5050c1a8013b4841Brian Wellington/*
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * $Id: ssu.c,v 1.24 2004/03/05 05:09:25 marka Exp $
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * Principal Author: Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt#include <config.h>
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein
ab023a65562e62b85a824509d829b6fad87e00b1Rob Austein#include <isc/magic.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isc/mem.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isc/string.h> /* Required for HP/UX (and others?) */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <isc/util.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/name.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#include <dns/ssu.h>
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence#define SSUTABLEMAGIC ISC_MAGIC('S', 'S', 'U', 'T')
7de2c6e6d51f38daeb2d346f3f21dc01ccece6daEvan Hunt#define VALID_SSUTABLE(table) ISC_MAGIC_VALID(table, SSUTABLEMAGIC)
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define SSURULEMAGIC ISC_MAGIC('S', 'S', 'U', 'R')
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington#define VALID_SSURULE(table) ISC_MAGIC_VALID(table, SSURULEMAGIC)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstruct dns_ssurule {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int magic;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_boolean_t grant; /* is this a grant or a deny? */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int matchtype; /* which type of pattern match? */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_t *identity; /* the identity to match */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_t *name; /* the name being updated */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int ntypes; /* number of data types covered */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_rdatatype_t *types; /* the data types. Can include ANY, */
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington /* defaults to all but SIG,SOA,NS if NULL*/
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ISC_LINK(dns_ssurule_t) link;
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence};
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstruct dns_ssutable {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int magic;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_t *mctx;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int references;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mutex_t lock;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ISC_LIST(dns_ssurule_t) rules;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington};
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_result_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtondns_ssutable_create(isc_mem_t *mctx, dns_ssutable_t **tablep) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_result_t result;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_ssutable_t *table;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(tablep != NULL && *tablep == NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(mctx != NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington table = isc_mem_get(mctx, sizeof(dns_ssutable_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (table == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_NOMEMORY);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington result = isc_mutex_init(&table->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (result != ISC_R_SUCCESS) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, table, sizeof(dns_ssutable_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (result);
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington table->references = 1;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington table->mctx = mctx;
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson ISC_LIST_INIT(table->rules);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington table->magic = SSUTABLEMAGIC;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *tablep = table;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_SUCCESS);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline void
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtondestroy(dns_ssutable_t *table) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_t *mctx;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(VALID_SSUTABLE(table));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington mctx = table->mctx;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington while (!ISC_LIST_EMPTY(table->rules)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_ssurule_t *rule = ISC_LIST_HEAD(table->rules);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->identity != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_free(rule->identity, mctx);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, rule->identity, sizeof(dns_name_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->name != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_free(rule->name, mctx);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, rule->name, sizeof(dns_name_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->types != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, rule->types,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->ntypes * sizeof(dns_rdatatype_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ISC_LIST_UNLINK(table->rules, rule, link);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->magic = 0;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, rule, sizeof(dns_ssurule_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington DESTROYLOCK(&table->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington table->magic = 0;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, table, sizeof(dns_ssutable_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonvoid
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtondns_ssutable_attach(dns_ssutable_t *source, dns_ssutable_t **targetp) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(VALID_SSUTABLE(source));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(targetp != NULL && *targetp == NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington LOCK(&source->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(source->references > 0);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington source->references++;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(source->references != 0);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington UNLOCK(&source->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *targetp = source;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonvoid
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtondns_ssutable_detach(dns_ssutable_t **tablep) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_ssutable_t *table;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_boolean_t done = ISC_FALSE;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(tablep != NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington table = *tablep;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(VALID_SSUTABLE(table));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington LOCK(&table->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington INSIST(table->references > 0);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (--table->references == 0)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington done = ISC_TRUE;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington UNLOCK(&table->lock);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington *tablep = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (done)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington destroy(table);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_result_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtondns_ssutable_addrule(dns_ssutable_t *table, isc_boolean_t grant,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_t *identity, unsigned int matchtype,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_t *name, unsigned int ntypes,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_rdatatype_t *types)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_ssurule_t *rule;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_t *mctx;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_result_t result;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(VALID_SSUTABLE(table));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(dns_name_isabsolute(identity));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(dns_name_isabsolute(name));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(matchtype <= DNS_SSUMATCHTYPE_SELF);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (matchtype == DNS_SSUMATCHTYPE_WILDCARD)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(dns_name_iswildcard(name));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (ntypes > 0)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(types != NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington mctx = table->mctx;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule = isc_mem_get(mctx, sizeof(dns_ssurule_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_NOMEMORY);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->identity = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->name = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->types = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->grant = grant;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->identity = isc_mem_get(mctx, sizeof(dns_name_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->identity == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington result = ISC_R_NOMEMORY;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington goto failure;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_init(rule->identity, NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington result = dns_name_dup(identity, mctx, rule->identity);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (result != ISC_R_SUCCESS)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington goto failure;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->name = isc_mem_get(mctx, sizeof(dns_name_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->name == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington result = ISC_R_NOMEMORY;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington goto failure;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_init(rule->name, NULL);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington result = dns_name_dup(name, mctx, rule->name);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (result != ISC_R_SUCCESS)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington goto failure;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->matchtype = matchtype;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->ntypes = ntypes;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (ntypes > 0) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->types = isc_mem_get(mctx,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ntypes * sizeof(dns_rdatatype_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->types == NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington result = ISC_R_NOMEMORY;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington goto failure;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington memcpy(rule->types, types, ntypes * sizeof(dns_rdatatype_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington else
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->types = NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule->magic = SSURULEMAGIC;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ISC_LIST_INITANDAPPEND(table->rules, rule, link);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_R_SUCCESS);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington failure:
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->identity != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (dns_name_dynamic(rule->identity))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_free(rule->identity, mctx);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, rule->identity, sizeof(dns_name_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->name != NULL) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (dns_name_dynamic(rule->name))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_free(rule->name, mctx);
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson isc_mem_put(mctx, rule->name, sizeof(dns_name_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->types != NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, rule->types,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington ntypes * sizeof(dns_rdatatype_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington isc_mem_put(mctx, rule, sizeof(dns_ssurule_t));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (result);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonstatic inline isc_boolean_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisusertype(dns_rdatatype_t type) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_TF(type != dns_rdatatype_ns &&
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington type != dns_rdatatype_soa &&
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington type != dns_rdatatype_rrsig));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington}
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtonisc_boolean_t
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellingtondns_ssutable_checkrules(dns_ssutable_t *table, dns_name_t *signer,
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_name_t *name, dns_rdatatype_t type)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington{
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington dns_ssurule_t *rule;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington unsigned int i;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(VALID_SSUTABLE(table));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(signer == NULL || dns_name_isabsolute(signer));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington REQUIRE(dns_name_isabsolute(name));
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (signer == NULL)
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington return (ISC_FALSE);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule = ISC_LIST_HEAD(table->rules);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule = ISC_LIST_NEXT(rule, link);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington for (rule = ISC_LIST_HEAD(table->rules);
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule != NULL;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington rule = ISC_LIST_NEXT(rule, link))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (dns_name_iswildcard(rule->identity)) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (!dns_name_matcheswildcard(signer, rule->identity))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington continue;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington else {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (!dns_name_equal(signer, rule->identity))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington continue;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (rule->matchtype == DNS_SSUMATCHTYPE_NAME) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (!dns_name_equal(name, rule->name))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington continue;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington else if (rule->matchtype == DNS_SSUMATCHTYPE_SUBDOMAIN) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (!dns_name_issubdomain(name, rule->name))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington continue;
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington }
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington else if (rule->matchtype == DNS_SSUMATCHTYPE_WILDCARD) {
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington if (!dns_name_matcheswildcard(name, rule->name))
1d92d8a2456b23842a649b6104c60a9d6ea25333Brian Wellington continue;
}
else if (rule->matchtype == DNS_SSUMATCHTYPE_SELF) {
if (!dns_name_equal(signer, name))
continue;
}
if (rule->ntypes == 0) {
if (!isusertype(type))
continue;
}
else {
for (i = 0; i < rule->ntypes; i++) {
if (rule->types[i] == dns_rdatatype_any ||
rule->types[i] == type)
break;
}
if (i == rule->ntypes)
continue;
}
return (rule->grant);
}
return (ISC_FALSE);
}
isc_boolean_t
dns_ssurule_isgrant(const dns_ssurule_t *rule) {
REQUIRE(VALID_SSURULE(rule));
return (rule->grant);
}
dns_name_t *
dns_ssurule_identity(const dns_ssurule_t *rule) {
REQUIRE(VALID_SSURULE(rule));
return (rule->identity);
}
unsigned int
dns_ssurule_matchtype(const dns_ssurule_t *rule) {
REQUIRE(VALID_SSURULE(rule));
return (rule->matchtype);
}
dns_name_t *
dns_ssurule_name(const dns_ssurule_t *rule) {
REQUIRE(VALID_SSURULE(rule));
return (rule->name);
}
unsigned int
dns_ssurule_types(const dns_ssurule_t *rule, dns_rdatatype_t **types) {
REQUIRE(VALID_SSURULE(rule));
REQUIRE(types != NULL && *types != NULL);
*types = rule->types;
return (rule->ntypes);
}
isc_result_t
dns_ssutable_firstrule(const dns_ssutable_t *table, dns_ssurule_t **rule) {
REQUIRE(VALID_SSUTABLE(table));
REQUIRE(rule != NULL && *rule == NULL);
*rule = ISC_LIST_HEAD(table->rules);
return (*rule != NULL ? ISC_R_SUCCESS : ISC_R_NOMORE);
}
isc_result_t
dns_ssutable_nextrule(dns_ssurule_t *rule, dns_ssurule_t **nextrule) {
REQUIRE(VALID_SSURULE(rule));
REQUIRE(nextrule != NULL && *nextrule == NULL);
*nextrule = ISC_LIST_NEXT(rule, link);
return (*nextrule != NULL ? ISC_R_SUCCESS : ISC_R_NOMORE);
}