1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/*
* namedb.c -- common namedb operations.
*
* Copyright (c) 2001-2006, NLnet Labs. All rights reserved.
*
* See LICENSE for the license.
*
*/
#include <config.h>
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <string.h>
#include "namedb.h"
static domain_type *
allocate_domain_info(domain_table_type *table,
const dname_type *dname,
domain_type *parent)
{
domain_type *result;
assert(table);
assert(dname);
assert(parent);
result = (domain_type *) region_alloc(table->region,
sizeof(domain_type));
result->node.key = dname_partial_copy(
table->region, dname, domain_dname(parent)->label_count + 1);
result->parent = parent;
result->wildcard_child_closest_match = result;
result->rrsets = NULL;
result->number = 0;
#ifdef NSEC3
result->nsec3_cover = NULL;
result->nsec3_wcard_child_cover = NULL;
result->nsec3_ds_parent_cover = NULL;
result->nsec3_lookup = NULL;
result->nsec3_is_exact = 0;
result->nsec3_ds_parent_is_exact = 0;
#endif
result->is_existing = 0;
result->is_apex = 0;
return result;
}
domain_table_type *
domain_table_create(region_type *region)
{
const dname_type *origin;
domain_table_type *result;
domain_type *root;
assert(region);
origin = dname_make(region, (uint8_t *) "", 0);
root = (domain_type *) region_alloc(region, sizeof(domain_type));
root->node.key = origin;
root->parent = NULL;
root->wildcard_child_closest_match = root;
root->rrsets = NULL;
root->number = 1; /* 0 is used for after header */
root->is_existing = 0;
root->is_apex = 0;
#ifdef NSEC3
root->nsec3_is_exact = 0;
root->nsec3_ds_parent_is_exact = 0;
root->nsec3_cover = NULL;
root->nsec3_wcard_child_cover = NULL;
root->nsec3_ds_parent_cover = NULL;
root->nsec3_lookup = NULL;
#endif
result = (domain_table_type *) region_alloc(region,
sizeof(domain_table_type));
result->region = region;
result->names_to_domains = rbtree_create(
region, (int (*)(const void *, const void *)) dname_compare);
rbtree_insert(result->names_to_domains, (rbnode_t *) root);
result->root = root;
return result;
}
int
domain_table_search(domain_table_type *table,
const dname_type *dname,
domain_type **closest_match,
domain_type **closest_encloser)
{
int exact;
uint8_t label_match_count;
assert(table);
assert(dname);
assert(closest_match);
assert(closest_encloser);
exact = rbtree_find_less_equal(table->names_to_domains, dname, (rbnode_t **) closest_match);
assert(*closest_match);
*closest_encloser = *closest_match;
if (!exact) {
label_match_count = dname_label_match_count(
domain_dname(*closest_encloser),
dname);
assert(label_match_count < dname->label_count);
while (label_match_count < domain_dname(*closest_encloser)->label_count) {
(*closest_encloser) = (*closest_encloser)->parent;
assert(*closest_encloser);
}
}
return exact;
}
domain_type *
domain_table_find(domain_table_type *table,
const dname_type *dname)
{
domain_type *closest_match;
domain_type *closest_encloser;
int exact;
exact = domain_table_search(
table, dname, &closest_match, &closest_encloser);
return exact ? closest_encloser : NULL;
}
domain_type *
domain_table_insert(domain_table_type *table,
const dname_type *dname)
{
domain_type *closest_match;
domain_type *closest_encloser;
domain_type *result;
int exact;
assert(table);
assert(dname);
exact = domain_table_search(
table, dname, &closest_match, &closest_encloser);
if (exact) {
result = closest_encloser;
} else {
assert(domain_dname(closest_encloser)->label_count < dname->label_count);
/* Insert new node(s). */
do {
result = allocate_domain_info(table,
dname,
closest_encloser);
rbtree_insert(table->names_to_domains, (rbnode_t *) result);
result->number = table->names_to_domains->count;
/*
* If the newly added domain name is larger
* than the parent's current
* wildcard_child_closest_match but smaller or
* equal to the wildcard domain name, update
* the parent's wildcard_child_closest_match
* field.
*/
if (label_compare(dname_name(domain_dname(result)),
(const uint8_t *) "\001*") <= 0
&& dname_compare(domain_dname(result),
domain_dname(closest_encloser->wildcard_child_closest_match)) > 0)
{
closest_encloser->wildcard_child_closest_match
= result;
}
closest_encloser = result;
} while (domain_dname(closest_encloser)->label_count < dname->label_count);
}
return result;
}
int
domain_table_iterate(domain_table_type *table,
domain_table_iterator_type iterator,
void *user_data)
{
const void *dname;
void *node;
int error = 0;
assert(table);
RBTREE_WALK(table->names_to_domains, dname, node) {
error += iterator((domain_type *) node, user_data);
}
return error;
}
void
domain_add_rrset(domain_type *domain, rrset_type *rrset)
{
#if 0 /* fast */
rrset->next = domain->rrsets;
domain->rrsets = rrset;
#else
/* preserve ordering, add at end */
rrset_type** p = &domain->rrsets;
while(*p)
p = &((*p)->next);
*p = rrset;
rrset->next = 0;
#endif
while (domain && !domain->is_existing) {
domain->is_existing = 1;
domain = domain->parent;
}
}
rrset_type *
domain_find_rrset(domain_type *domain, zone_type *zone, uint16_t type)
{
rrset_type *result = domain->rrsets;
while (result) {
if (result->zone == zone && rrset_rrtype(result) == type) {
return result;
}
result = result->next;
}
return NULL;
}
rrset_type *
domain_find_any_rrset(domain_type *domain, zone_type *zone)
{
rrset_type *result = domain->rrsets;
while (result) {
if (result->zone == zone) {
return result;
}
result = result->next;
}
return NULL;
}
zone_type *
domain_find_zone(domain_type *domain)
{
rrset_type *rrset;
while (domain) {
for (rrset = domain->rrsets; rrset; rrset = rrset->next) {
if (rrset_rrtype(rrset) == TYPE_SOA) {
return rrset->zone;
}
}
domain = domain->parent;
}
return NULL;
}
zone_type *
domain_find_parent_zone(zone_type *zone)
{
rrset_type *rrset;
assert(zone);
for (rrset = zone->apex->rrsets; rrset; rrset = rrset->next) {
if (rrset->zone != zone && rrset_rrtype(rrset) == TYPE_NS) {
return rrset->zone;
}
}
return NULL;
}
domain_type *
domain_find_ns_rrsets(domain_type *domain, zone_type *zone, rrset_type **ns)
{
while (domain && domain != zone->apex) {
*ns = domain_find_rrset(domain, zone, TYPE_NS);
if (*ns)
return domain;
domain = domain->parent;
}
*ns = NULL;
return NULL;
}
int
domain_is_glue(domain_type *domain, zone_type *zone)
{
rrset_type *unused;
domain_type *ns_domain = domain_find_ns_rrsets(domain, zone, &unused);
return (ns_domain != NULL &&
domain_find_rrset(ns_domain, zone, TYPE_SOA) == NULL);
}
domain_type *
domain_wildcard_child(domain_type *domain)
{
domain_type *wildcard_child;
assert(domain);
assert(domain->wildcard_child_closest_match);
wildcard_child = domain->wildcard_child_closest_match;
if (wildcard_child != domain
&& label_is_wildcard(dname_name(domain_dname(wildcard_child))))
{
return wildcard_child;
} else {
return NULL;
}
}
int
zone_is_secure(zone_type *zone)
{
assert(zone);
return zone->is_secure;
}
uint16_t
rr_rrsig_type_covered(rr_type *rr)
{
assert(rr->type == TYPE_RRSIG);
assert(rr->rdata_count > 0);
assert(rdata_atom_size(rr->rdatas[0]) == sizeof(uint16_t));
return ntohs(* (uint16_t *) rdata_atom_data(rr->rdatas[0]));
}
zone_type *
namedb_find_zone(namedb_type *db, domain_type *domain)
{
zone_type *zone;
for (zone = db->zones; zone; zone = zone->next) {
if (zone->apex == domain)
break;
}
return zone;
}
rrset_type *
domain_find_non_cname_rrset(domain_type *domain, zone_type *zone)
{
/* find any rrset type that is not allowed next to a CNAME */
/* nothing is allowed next to a CNAME, except RRSIG, NSEC, NSEC3 */
rrset_type *result = domain->rrsets;
while (result) {
if (result->zone == zone && /* here is the list of exceptions*/
rrset_rrtype(result) != TYPE_CNAME &&
rrset_rrtype(result) != TYPE_RRSIG &&
rrset_rrtype(result) != TYPE_NXT &&
rrset_rrtype(result) != TYPE_SIG &&
rrset_rrtype(result) != TYPE_NSEC &&
rrset_rrtype(result) != TYPE_NSEC3 ) {
return result;
}
result = result->next;
}
return NULL;
}
|