1 1.1 christos /* $NetBSD: update.c,v 1.1 2024/02/18 20:57:34 christos Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 1.1 christos * 6 1.1 christos * SPDX-License-Identifier: MPL-2.0 7 1.1 christos * 8 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public 9 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this 10 1.1 christos * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 1.1 christos * 12 1.1 christos * See the COPYRIGHT file distributed with this work for additional 13 1.1 christos * information regarding copyright ownership. 14 1.1 christos */ 15 1.1 christos 16 1.1 christos #include <inttypes.h> 17 1.1 christos #include <stdbool.h> 18 1.1 christos #include <time.h> 19 1.1 christos 20 1.1 christos #include <isc/log.h> 21 1.1 christos #include <isc/magic.h> 22 1.1 christos #include <isc/mem.h> 23 1.1 christos #include <isc/netaddr.h> 24 1.1 christos #include <isc/platform.h> 25 1.1 christos #include <isc/print.h> 26 1.1 christos #include <isc/random.h> 27 1.1 christos #include <isc/serial.h> 28 1.1 christos #include <isc/stats.h> 29 1.1 christos #include <isc/stdtime.h> 30 1.1 christos #include <isc/string.h> 31 1.1 christos #include <isc/taskpool.h> 32 1.1 christos #include <isc/time.h> 33 1.1 christos #include <isc/util.h> 34 1.1 christos 35 1.1 christos #include <dns/db.h> 36 1.1 christos #include <dns/dbiterator.h> 37 1.1 christos #include <dns/diff.h> 38 1.1 christos #include <dns/dnssec.h> 39 1.1 christos #include <dns/events.h> 40 1.1 christos #include <dns/fixedname.h> 41 1.1 christos #include <dns/journal.h> 42 1.1 christos #include <dns/kasp.h> 43 1.1 christos #include <dns/keyvalues.h> 44 1.1 christos #include <dns/log.h> 45 1.1 christos #include <dns/message.h> 46 1.1 christos #include <dns/nsec.h> 47 1.1 christos #include <dns/nsec3.h> 48 1.1 christos #include <dns/private.h> 49 1.1 christos #include <dns/rdataclass.h> 50 1.1 christos #include <dns/rdataset.h> 51 1.1 christos #include <dns/rdatasetiter.h> 52 1.1 christos #include <dns/rdatastruct.h> 53 1.1 christos #include <dns/rdatatype.h> 54 1.1 christos #include <dns/result.h> 55 1.1 christos #include <dns/soa.h> 56 1.1 christos #include <dns/ssu.h> 57 1.1 christos #include <dns/stats.h> 58 1.1 christos #include <dns/tsig.h> 59 1.1 christos #include <dns/update.h> 60 1.1 christos #include <dns/view.h> 61 1.1 christos #include <dns/zone.h> 62 1.1 christos #include <dns/zt.h> 63 1.1 christos 64 1.1 christos /**************************************************************************/ 65 1.1 christos 66 1.1 christos #define STATE_MAGIC ISC_MAGIC('S', 'T', 'T', 'E') 67 1.1 christos #define DNS_STATE_VALID(state) ISC_MAGIC_VALID(state, STATE_MAGIC) 68 1.1 christos 69 1.1 christos /*% 70 1.1 christos * Log level for tracing dynamic update protocol requests. 71 1.1 christos */ 72 1.1 christos #define LOGLEVEL_PROTOCOL ISC_LOG_INFO 73 1.1 christos 74 1.1 christos /*% 75 1.1 christos * Log level for low-level debug tracing. 76 1.1 christos */ 77 1.1 christos #define LOGLEVEL_DEBUG ISC_LOG_DEBUG(8) 78 1.1 christos 79 1.1 christos /*% 80 1.1 christos * Check an operation for failure. These macros all assume that 81 1.1 christos * the function using them has a 'result' variable and a 'failure' 82 1.1 christos * label. 83 1.1 christos */ 84 1.1 christos #define CHECK(op) \ 85 1.1 christos do { \ 86 1.1 christos result = (op); \ 87 1.1 christos if (result != ISC_R_SUCCESS) \ 88 1.1 christos goto failure; \ 89 1.1 christos } while (0) 90 1.1 christos 91 1.1 christos /*% 92 1.1 christos * Fail unconditionally with result 'code', which must not 93 1.1 christos * be ISC_R_SUCCESS. The reason for failure presumably has 94 1.1 christos * been logged already. 95 1.1 christos * 96 1.1 christos * The test against ISC_R_SUCCESS is there to keep the Solaris compiler 97 1.1 christos * from complaining about "end-of-loop code not reached". 98 1.1 christos */ 99 1.1 christos 100 1.1 christos #define FAIL(code) \ 101 1.1 christos do { \ 102 1.1 christos result = (code); \ 103 1.1 christos if (result != ISC_R_SUCCESS) \ 104 1.1 christos goto failure; \ 105 1.1 christos } while (0) 106 1.1 christos 107 1.1 christos /*% 108 1.1 christos * Fail unconditionally and log as a client error. 109 1.1 christos * The test against ISC_R_SUCCESS is there to keep the Solaris compiler 110 1.1 christos * from complaining about "end-of-loop code not reached". 111 1.1 christos */ 112 1.1 christos #define FAILC(code, msg) \ 113 1.1 christos do { \ 114 1.1 christos const char *_what = "failed"; \ 115 1.1 christos result = (code); \ 116 1.1 christos switch (result) { \ 117 1.1 christos case DNS_R_NXDOMAIN: \ 118 1.1 christos case DNS_R_YXDOMAIN: \ 119 1.1 christos case DNS_R_YXRRSET: \ 120 1.1 christos case DNS_R_NXRRSET: \ 121 1.1 christos _what = "unsuccessful"; \ 122 1.1 christos } \ 123 1.1 christos update_log(log, zone, LOGLEVEL_PROTOCOL, "update %s: %s (%s)", \ 124 1.1 christos _what, msg, isc_result_totext(result)); \ 125 1.1 christos if (result != ISC_R_SUCCESS) \ 126 1.1 christos goto failure; \ 127 1.1 christos } while (0) 128 1.1 christos 129 1.1 christos #define FAILN(code, name, msg) \ 130 1.1 christos do { \ 131 1.1 christos const char *_what = "failed"; \ 132 1.1 christos result = (code); \ 133 1.1 christos switch (result) { \ 134 1.1 christos case DNS_R_NXDOMAIN: \ 135 1.1 christos case DNS_R_YXDOMAIN: \ 136 1.1 christos case DNS_R_YXRRSET: \ 137 1.1 christos case DNS_R_NXRRSET: \ 138 1.1 christos _what = "unsuccessful"; \ 139 1.1 christos } \ 140 1.1 christos if (isc_log_wouldlog(dns_lctx, LOGLEVEL_PROTOCOL)) { \ 141 1.1 christos char _nbuf[DNS_NAME_FORMATSIZE]; \ 142 1.1 christos dns_name_format(name, _nbuf, sizeof(_nbuf)); \ 143 1.1 christos update_log(log, zone, LOGLEVEL_PROTOCOL, \ 144 1.1 christos "update %s: %s: %s (%s)", _what, _nbuf, \ 145 1.1 christos msg, isc_result_totext(result)); \ 146 1.1 christos } \ 147 1.1 christos if (result != ISC_R_SUCCESS) \ 148 1.1 christos goto failure; \ 149 1.1 christos } while (0) 150 1.1 christos 151 1.1 christos #define FAILNT(code, name, type, msg) \ 152 1.1 christos do { \ 153 1.1 christos const char *_what = "failed"; \ 154 1.1 christos result = (code); \ 155 1.1 christos switch (result) { \ 156 1.1 christos case DNS_R_NXDOMAIN: \ 157 1.1 christos case DNS_R_YXDOMAIN: \ 158 1.1 christos case DNS_R_YXRRSET: \ 159 1.1 christos case DNS_R_NXRRSET: \ 160 1.1 christos _what = "unsuccessful"; \ 161 1.1 christos } \ 162 1.1 christos if (isc_log_wouldlog(dns_lctx, LOGLEVEL_PROTOCOL)) { \ 163 1.1 christos char _nbuf[DNS_NAME_FORMATSIZE]; \ 164 1.1 christos char _tbuf[DNS_RDATATYPE_FORMATSIZE]; \ 165 1.1 christos dns_name_format(name, _nbuf, sizeof(_nbuf)); \ 166 1.1 christos dns_rdatatype_format(type, _tbuf, sizeof(_tbuf)); \ 167 1.1 christos update_log(log, zone, LOGLEVEL_PROTOCOL, \ 168 1.1 christos "update %s: %s/%s: %s (%s)", _what, _nbuf, \ 169 1.1 christos _tbuf, msg, isc_result_totext(result)); \ 170 1.1 christos } \ 171 1.1 christos if (result != ISC_R_SUCCESS) \ 172 1.1 christos goto failure; \ 173 1.1 christos } while (0) 174 1.1 christos 175 1.1 christos /*% 176 1.1 christos * Fail unconditionally and log as a server error. 177 1.1 christos * The test against ISC_R_SUCCESS is there to keep the Solaris compiler 178 1.1 christos * from complaining about "end-of-loop code not reached". 179 1.1 christos */ 180 1.1 christos #define FAILS(code, msg) \ 181 1.1 christos do { \ 182 1.1 christos result = (code); \ 183 1.1 christos update_log(log, zone, LOGLEVEL_PROTOCOL, "error: %s: %s", msg, \ 184 1.1 christos isc_result_totext(result)); \ 185 1.1 christos if (result != ISC_R_SUCCESS) \ 186 1.1 christos goto failure; \ 187 1.1 christos } while (0) 188 1.1 christos 189 1.1 christos /**************************************************************************/ 190 1.1 christos 191 1.1 christos typedef struct rr rr_t; 192 1.1 christos 193 1.1 christos struct rr { 194 1.1 christos /* dns_name_t name; */ 195 1.1 christos uint32_t ttl; 196 1.1 christos dns_rdata_t rdata; 197 1.1 christos }; 198 1.1 christos 199 1.1 christos typedef struct update_event update_event_t; 200 1.1 christos 201 1.1 christos /**************************************************************************/ 202 1.1 christos 203 1.1 christos static void 204 1.1 christos update_log(dns_update_log_t *callback, dns_zone_t *zone, int level, 205 1.1 christos const char *fmt, ...) ISC_FORMAT_PRINTF(4, 5); 206 1.1 christos 207 1.1 christos static void 208 1.1 christos update_log(dns_update_log_t *callback, dns_zone_t *zone, int level, 209 1.1 christos const char *fmt, ...) { 210 1.1 christos va_list ap; 211 1.1 christos char message[4096]; 212 1.1 christos 213 1.1 christos if (callback == NULL) { 214 1.1 christos return; 215 1.1 christos } 216 1.1 christos 217 1.1 christos if (!isc_log_wouldlog(dns_lctx, level)) { 218 1.1 christos return; 219 1.1 christos } 220 1.1 christos 221 1.1 christos va_start(ap, fmt); 222 1.1 christos vsnprintf(message, sizeof(message), fmt, ap); 223 1.1 christos va_end(ap); 224 1.1 christos 225 1.1 christos (callback->func)(callback->arg, zone, level, message); 226 1.1 christos } 227 1.1 christos 228 1.1 christos /*% 229 1.1 christos * Update a single RR in version 'ver' of 'db' and log the 230 1.1 christos * update in 'diff'. 231 1.1 christos * 232 1.1 christos * Ensures: 233 1.1 christos * \li '*tuple' == NULL. Either the tuple is freed, or its 234 1.1 christos * ownership has been transferred to the diff. 235 1.1 christos */ 236 1.1 christos static isc_result_t 237 1.1 christos do_one_tuple(dns_difftuple_t **tuple, dns_db_t *db, dns_dbversion_t *ver, 238 1.1 christos dns_diff_t *diff) { 239 1.1 christos dns_diff_t temp_diff; 240 1.1 christos isc_result_t result; 241 1.1 christos 242 1.1 christos /* 243 1.1 christos * Create a singleton diff. 244 1.1 christos */ 245 1.1 christos dns_diff_init(diff->mctx, &temp_diff); 246 1.1 christos ISC_LIST_APPEND(temp_diff.tuples, *tuple, link); 247 1.1 christos 248 1.1 christos /* 249 1.1 christos * Apply it to the database. 250 1.1 christos */ 251 1.1 christos result = dns_diff_apply(&temp_diff, db, ver); 252 1.1 christos ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link); 253 1.1 christos if (result != ISC_R_SUCCESS) { 254 1.1 christos dns_difftuple_free(tuple); 255 1.1 christos return (result); 256 1.1 christos } 257 1.1 christos 258 1.1 christos /* 259 1.1 christos * Merge it into the current pending journal entry. 260 1.1 christos */ 261 1.1 christos dns_diff_appendminimal(diff, tuple); 262 1.1 christos 263 1.1 christos /* 264 1.1 christos * Do not clear temp_diff. 265 1.1 christos */ 266 1.1 christos return (ISC_R_SUCCESS); 267 1.1 christos } 268 1.1 christos 269 1.1 christos static isc_result_t 270 1.1 christos update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff, 271 1.1 christos dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl, 272 1.1 christos dns_rdata_t *rdata) { 273 1.1 christos dns_difftuple_t *tuple = NULL; 274 1.1 christos isc_result_t result; 275 1.1 christos result = dns_difftuple_create(diff->mctx, op, name, ttl, rdata, &tuple); 276 1.1 christos if (result != ISC_R_SUCCESS) { 277 1.1 christos return (result); 278 1.1 christos } 279 1.1 christos return (do_one_tuple(&tuple, db, ver, diff)); 280 1.1 christos } 281 1.1 christos 282 1.1 christos /**************************************************************************/ 283 1.1 christos /* 284 1.1 christos * Callback-style iteration over rdatasets and rdatas. 285 1.1 christos * 286 1.1 christos * foreach_rrset() can be used to iterate over the RRsets 287 1.1 christos * of a name and call a callback function with each 288 1.1 christos * one. Similarly, foreach_rr() can be used to iterate 289 1.1 christos * over the individual RRs at name, optionally restricted 290 1.1 christos * to RRs of a given type. 291 1.1 christos * 292 1.1 christos * The callback functions are called "actions" and take 293 1.1 christos * two arguments: a void pointer for passing arbitrary 294 1.1 christos * context information, and a pointer to the current RRset 295 1.1 christos * or RR. By convention, their names end in "_action". 296 1.1 christos */ 297 1.1 christos 298 1.1 christos /* 299 1.1 christos * XXXRTH We might want to make this public somewhere in libdns. 300 1.1 christos */ 301 1.1 christos 302 1.1 christos /*% 303 1.1 christos * Function type for foreach_rrset() iterator actions. 304 1.1 christos */ 305 1.1 christos typedef isc_result_t 306 1.1 christos rrset_func(void *data, dns_rdataset_t *rrset); 307 1.1 christos 308 1.1 christos /*% 309 1.1 christos * Function type for foreach_rr() iterator actions. 310 1.1 christos */ 311 1.1 christos typedef isc_result_t 312 1.1 christos rr_func(void *data, rr_t *rr); 313 1.1 christos 314 1.1 christos /*% 315 1.1 christos * Internal context struct for foreach_node_rr(). 316 1.1 christos */ 317 1.1 christos typedef struct { 318 1.1 christos rr_func *rr_action; 319 1.1 christos void *rr_action_data; 320 1.1 christos } foreach_node_rr_ctx_t; 321 1.1 christos 322 1.1 christos /*% 323 1.1 christos * Internal helper function for foreach_node_rr(). 324 1.1 christos */ 325 1.1 christos static isc_result_t 326 1.1 christos foreach_node_rr_action(void *data, dns_rdataset_t *rdataset) { 327 1.1 christos isc_result_t result; 328 1.1 christos foreach_node_rr_ctx_t *ctx = data; 329 1.1 christos for (result = dns_rdataset_first(rdataset); result == ISC_R_SUCCESS; 330 1.1 christos result = dns_rdataset_next(rdataset)) 331 1.1 christos { 332 1.1 christos rr_t rr = { 0, DNS_RDATA_INIT }; 333 1.1 christos 334 1.1 christos dns_rdataset_current(rdataset, &rr.rdata); 335 1.1 christos rr.ttl = rdataset->ttl; 336 1.1 christos result = (*ctx->rr_action)(ctx->rr_action_data, &rr); 337 1.1 christos if (result != ISC_R_SUCCESS) { 338 1.1 christos return (result); 339 1.1 christos } 340 1.1 christos } 341 1.1 christos if (result != ISC_R_NOMORE) { 342 1.1 christos return (result); 343 1.1 christos } 344 1.1 christos return (ISC_R_SUCCESS); 345 1.1 christos } 346 1.1 christos 347 1.1 christos /*% 348 1.1 christos * For each rdataset of 'name' in 'ver' of 'db', call 'action' 349 1.1 christos * with the rdataset and 'action_data' as arguments. If the name 350 1.1 christos * does not exist, do nothing. 351 1.1 christos * 352 1.1 christos * If 'action' returns an error, abort iteration and return the error. 353 1.1 christos */ 354 1.1 christos static isc_result_t 355 1.1 christos foreach_rrset(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, 356 1.1 christos rrset_func *action, void *action_data) { 357 1.1 christos isc_result_t result; 358 1.1 christos dns_dbnode_t *node; 359 1.1 christos dns_rdatasetiter_t *iter; 360 1.1 christos 361 1.1 christos node = NULL; 362 1.1 christos result = dns_db_findnode(db, name, false, &node); 363 1.1 christos if (result == ISC_R_NOTFOUND) { 364 1.1 christos return (ISC_R_SUCCESS); 365 1.1 christos } 366 1.1 christos if (result != ISC_R_SUCCESS) { 367 1.1 christos return (result); 368 1.1 christos } 369 1.1 christos 370 1.1 christos iter = NULL; 371 1.1 christos result = dns_db_allrdatasets(db, node, ver, 0, (isc_stdtime_t)0, &iter); 372 1.1 christos if (result != ISC_R_SUCCESS) { 373 1.1 christos goto cleanup_node; 374 1.1 christos } 375 1.1 christos 376 1.1 christos for (result = dns_rdatasetiter_first(iter); result == ISC_R_SUCCESS; 377 1.1 christos result = dns_rdatasetiter_next(iter)) 378 1.1 christos { 379 1.1 christos dns_rdataset_t rdataset; 380 1.1 christos 381 1.1 christos dns_rdataset_init(&rdataset); 382 1.1 christos dns_rdatasetiter_current(iter, &rdataset); 383 1.1 christos 384 1.1 christos result = (*action)(action_data, &rdataset); 385 1.1 christos 386 1.1 christos dns_rdataset_disassociate(&rdataset); 387 1.1 christos if (result != ISC_R_SUCCESS) { 388 1.1 christos goto cleanup_iterator; 389 1.1 christos } 390 1.1 christos } 391 1.1 christos if (result == ISC_R_NOMORE) { 392 1.1 christos result = ISC_R_SUCCESS; 393 1.1 christos } 394 1.1 christos 395 1.1 christos cleanup_iterator: 396 1.1 christos dns_rdatasetiter_destroy(&iter); 397 1.1 christos 398 1.1 christos cleanup_node: 399 1.1 christos dns_db_detachnode(db, &node); 400 1.1 christos 401 1.1 christos return (result); 402 1.1 christos } 403 1.1 christos 404 1.1 christos /*% 405 1.1 christos * For each RR of 'name' in 'ver' of 'db', call 'action' 406 1.1 christos * with the RR and 'action_data' as arguments. If the name 407 1.1 christos * does not exist, do nothing. 408 1.1 christos * 409 1.1 christos * If 'action' returns an error, abort iteration 410 1.1 christos * and return the error. 411 1.1 christos */ 412 1.1 christos static isc_result_t 413 1.1 christos foreach_node_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, 414 1.1 christos rr_func *rr_action, void *rr_action_data) { 415 1.1 christos foreach_node_rr_ctx_t ctx; 416 1.1 christos ctx.rr_action = rr_action; 417 1.1 christos ctx.rr_action_data = rr_action_data; 418 1.1 christos return (foreach_rrset(db, ver, name, foreach_node_rr_action, &ctx)); 419 1.1 christos } 420 1.1 christos 421 1.1 christos /*% 422 1.1 christos * For each of the RRs specified by 'db', 'ver', 'name', 'type', 423 1.1 christos * (which can be dns_rdatatype_any to match any type), and 'covers', call 424 1.1 christos * 'action' with the RR and 'action_data' as arguments. If the name 425 1.1 christos * does not exist, or if no RRset of the given type exists at the name, 426 1.1 christos * do nothing. 427 1.1 christos * 428 1.1 christos * If 'action' returns an error, abort iteration and return the error. 429 1.1 christos */ 430 1.1 christos static isc_result_t 431 1.1 christos foreach_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, 432 1.1 christos dns_rdatatype_t type, dns_rdatatype_t covers, rr_func *rr_action, 433 1.1 christos void *rr_action_data) { 434 1.1 christos isc_result_t result; 435 1.1 christos dns_dbnode_t *node; 436 1.1 christos dns_rdataset_t rdataset; 437 1.1 christos 438 1.1 christos if (type == dns_rdatatype_any) { 439 1.1 christos return (foreach_node_rr(db, ver, name, rr_action, 440 1.1 christos rr_action_data)); 441 1.1 christos } 442 1.1 christos 443 1.1 christos node = NULL; 444 1.1 christos if (type == dns_rdatatype_nsec3 || 445 1.1 christos (type == dns_rdatatype_rrsig && covers == dns_rdatatype_nsec3)) 446 1.1 christos { 447 1.1 christos result = dns_db_findnsec3node(db, name, false, &node); 448 1.1 christos } else { 449 1.1 christos result = dns_db_findnode(db, name, false, &node); 450 1.1 christos } 451 1.1 christos if (result == ISC_R_NOTFOUND) { 452 1.1 christos return (ISC_R_SUCCESS); 453 1.1 christos } 454 1.1 christos if (result != ISC_R_SUCCESS) { 455 1.1 christos return (result); 456 1.1 christos } 457 1.1 christos 458 1.1 christos dns_rdataset_init(&rdataset); 459 1.1 christos result = dns_db_findrdataset(db, node, ver, type, covers, 460 1.1 christos (isc_stdtime_t)0, &rdataset, NULL); 461 1.1 christos if (result == ISC_R_NOTFOUND) { 462 1.1 christos result = ISC_R_SUCCESS; 463 1.1 christos goto cleanup_node; 464 1.1 christos } 465 1.1 christos if (result != ISC_R_SUCCESS) { 466 1.1 christos goto cleanup_node; 467 1.1 christos } 468 1.1 christos 469 1.1 christos for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS; 470 1.1 christos result = dns_rdataset_next(&rdataset)) 471 1.1 christos { 472 1.1 christos rr_t rr = { 0, DNS_RDATA_INIT }; 473 1.1 christos dns_rdataset_current(&rdataset, &rr.rdata); 474 1.1 christos rr.ttl = rdataset.ttl; 475 1.1 christos result = (*rr_action)(rr_action_data, &rr); 476 1.1 christos if (result != ISC_R_SUCCESS) { 477 1.1 christos goto cleanup_rdataset; 478 1.1 christos } 479 1.1 christos } 480 1.1 christos if (result != ISC_R_NOMORE) { 481 1.1 christos goto cleanup_rdataset; 482 1.1 christos } 483 1.1 christos result = ISC_R_SUCCESS; 484 1.1 christos 485 1.1 christos cleanup_rdataset: 486 1.1 christos dns_rdataset_disassociate(&rdataset); 487 1.1 christos cleanup_node: 488 1.1 christos dns_db_detachnode(db, &node); 489 1.1 christos 490 1.1 christos return (result); 491 1.1 christos } 492 1.1 christos 493 1.1 christos /**************************************************************************/ 494 1.1 christos /* 495 1.1 christos * Various tests on the database contents (for prerequisites, etc). 496 1.1 christos */ 497 1.1 christos 498 1.1 christos /*% 499 1.1 christos * Function type for predicate functions that compare a database RR 'db_rr' 500 1.1 christos * against an update RR 'update_rr'. 501 1.1 christos */ 502 1.1 christos typedef bool 503 1.1 christos rr_predicate(dns_rdata_t *update_rr, dns_rdata_t *db_rr); 504 1.1 christos 505 1.1 christos /*% 506 1.1 christos * Helper function for rrset_exists(). 507 1.1 christos */ 508 1.1 christos static isc_result_t 509 1.1 christos rrset_exists_action(void *data, rr_t *rr) { 510 1.1 christos UNUSED(data); 511 1.1 christos UNUSED(rr); 512 1.1 christos return (ISC_R_EXISTS); 513 1.1 christos } 514 1.1 christos 515 1.1 christos /*% 516 1.1 christos * Utility macro for RR existence checking functions. 517 1.1 christos * 518 1.1 christos * If the variable 'result' has the value ISC_R_EXISTS or 519 1.1 christos * ISC_R_SUCCESS, set *exists to true or false, 520 1.1 christos * respectively, and return success. 521 1.1 christos * 522 1.1 christos * If 'result' has any other value, there was a failure. 523 1.1 christos * Return the failure result code and do not set *exists. 524 1.1 christos * 525 1.1 christos * This would be more readable as "do { if ... } while(0)", 526 1.1 christos * but that form generates tons of warnings on Solaris 2.6. 527 1.1 christos */ 528 1.1 christos #define RETURN_EXISTENCE_FLAG \ 529 1.1 christos return ((result == ISC_R_EXISTS) \ 530 1.1 christos ? (*exists = true, ISC_R_SUCCESS) \ 531 1.1 christos : ((result == ISC_R_SUCCESS) \ 532 1.1 christos ? (*exists = false, ISC_R_SUCCESS) \ 533 1.1 christos : result)) 534 1.1 christos 535 1.1 christos /*% 536 1.1 christos * Set '*exists' to true iff an rrset of the given type exists, 537 1.1 christos * to false otherwise. 538 1.1 christos */ 539 1.1 christos static isc_result_t 540 1.1 christos rrset_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, 541 1.1 christos dns_rdatatype_t type, dns_rdatatype_t covers, bool *exists) { 542 1.1 christos isc_result_t result; 543 1.1 christos result = foreach_rr(db, ver, name, type, covers, rrset_exists_action, 544 1.1 christos NULL); 545 1.1 christos RETURN_EXISTENCE_FLAG; 546 1.1 christos } 547 1.1 christos 548 1.1 christos /*% 549 1.1 christos * Set '*visible' to true if the RRset exists and is part of the 550 1.1 christos * visible zone. Otherwise '*visible' is set to false unless a 551 1.1 christos * error occurs. 552 1.1 christos */ 553 1.1 christos static isc_result_t 554 1.1 christos rrset_visible(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, 555 1.1 christos dns_rdatatype_t type, bool *visible) { 556 1.1 christos isc_result_t result; 557 1.1 christos dns_fixedname_t fixed; 558 1.1 christos 559 1.1 christos dns_fixedname_init(&fixed); 560 1.1 christos result = dns_db_find(db, name, ver, type, DNS_DBFIND_NOWILD, 561 1.1 christos (isc_stdtime_t)0, NULL, dns_fixedname_name(&fixed), 562 1.1 christos NULL, NULL); 563 1.1 christos switch (result) { 564 1.1 christos case ISC_R_SUCCESS: 565 1.1 christos *visible = true; 566 1.1 christos break; 567 1.1 christos /* 568 1.1 christos * Glue, obscured, deleted or replaced records. 569 1.1 christos */ 570 1.1 christos case DNS_R_DELEGATION: 571 1.1 christos case DNS_R_DNAME: 572 1.1 christos case DNS_R_CNAME: 573 1.1 christos case DNS_R_NXDOMAIN: 574 1.1 christos case DNS_R_NXRRSET: 575 1.1 christos case DNS_R_EMPTYNAME: 576 1.1 christos case DNS_R_COVERINGNSEC: 577 1.1 christos *visible = false; 578 1.1 christos result = ISC_R_SUCCESS; 579 1.1 christos break; 580 1.1 christos default: 581 1.1 christos *visible = false; /* silence false compiler warning */ 582 1.1 christos break; 583 1.1 christos } 584 1.1 christos return (result); 585 1.1 christos } 586 1.1 christos 587 1.1 christos /*% 588 1.1 christos * Context struct and helper function for name_exists(). 589 1.1 christos */ 590 1.1 christos 591 1.1 christos static isc_result_t 592 1.1 christos name_exists_action(void *data, dns_rdataset_t *rrset) { 593 1.1 christos UNUSED(data); 594 1.1 christos UNUSED(rrset); 595 1.1 christos return (ISC_R_EXISTS); 596 1.1 christos } 597 1.1 christos 598 1.1 christos /*% 599 1.1 christos * Set '*exists' to true iff the given name exists, to false otherwise. 600 1.1 christos */ 601 1.1 christos static isc_result_t 602 1.1 christos name_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, 603 1.1 christos bool *exists) { 604 1.1 christos isc_result_t result; 605 1.1 christos result = foreach_rrset(db, ver, name, name_exists_action, NULL); 606 1.1 christos RETURN_EXISTENCE_FLAG; 607 1.1 christos } 608 1.1 christos 609 1.1 christos /**************************************************************************/ 610 1.1 christos /* 611 1.1 christos * Checking of "RRset exists (value dependent)" prerequisites. 612 1.1 christos * 613 1.1 christos * In the RFC2136 section 3.2.5, this is the pseudocode involving 614 1.1 christos * a variable called "temp", a mapping of <name, type> tuples to rrsets. 615 1.1 christos * 616 1.1 christos * Here, we represent the "temp" data structure as (non-minimal) "dns_diff_t" 617 1.1 christos * where each tuple has op==DNS_DIFFOP_EXISTS. 618 1.1 christos */ 619 1.1 christos 620 1.1 christos /*% 621 1.1 christos * A comparison function defining the sorting order for the entries 622 1.1 christos * in the "temp" data structure. The major sort key is the owner name, 623 1.1 christos * followed by the type and rdata. 624 1.1 christos */ 625 1.1 christos static int 626 1.1 christos temp_order(const void *av, const void *bv) { 627 1.1 christos dns_difftuple_t const *const *ap = av; 628 1.1 christos dns_difftuple_t const *const *bp = bv; 629 1.1 christos dns_difftuple_t const *a = *ap; 630 1.1 christos dns_difftuple_t const *b = *bp; 631 1.1 christos int r; 632 1.1 christos r = dns_name_compare(&a->name, &b->name); 633 1.1 christos if (r != 0) { 634 1.1 christos return (r); 635 1.1 christos } 636 1.1 christos r = (b->rdata.type - a->rdata.type); 637 1.1 christos if (r != 0) { 638 1.1 christos return (r); 639 1.1 christos } 640 1.1 christos r = dns_rdata_casecompare(&a->rdata, &b->rdata); 641 1.1 christos return (r); 642 1.1 christos } 643 1.1 christos 644 1.1 christos /**************************************************************************/ 645 1.1 christos /* 646 1.1 christos * Conditional deletion of RRs. 647 1.1 christos */ 648 1.1 christos 649 1.1 christos /*% 650 1.1 christos * Context structure for delete_if(). 651 1.1 christos */ 652 1.1 christos 653 1.1 christos typedef struct { 654 1.1 christos rr_predicate *predicate; 655 1.1 christos dns_db_t *db; 656 1.1 christos dns_dbversion_t *ver; 657 1.1 christos dns_diff_t *diff; 658 1.1 christos dns_name_t *name; 659 1.1 christos dns_rdata_t *update_rr; 660 1.1 christos } conditional_delete_ctx_t; 661 1.1 christos 662 1.1 christos /*% 663 1.1 christos * Predicate functions for delete_if(). 664 1.1 christos */ 665 1.1 christos 666 1.1 christos /*% 667 1.1 christos * Return true always. 668 1.1 christos */ 669 1.1 christos static bool 670 1.1 christos true_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) { 671 1.1 christos UNUSED(update_rr); 672 1.1 christos UNUSED(db_rr); 673 1.1 christos return (true); 674 1.1 christos } 675 1.1 christos 676 1.1 christos /*% 677 1.1 christos * Return true if the record is a RRSIG. 678 1.1 christos */ 679 1.1 christos static bool 680 1.1 christos rrsig_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) { 681 1.1 christos UNUSED(update_rr); 682 1.1 christos return ((db_rr->type == dns_rdatatype_rrsig) ? true : false); 683 1.1 christos } 684 1.1 christos 685 1.1 christos /*% 686 1.1 christos * Internal helper function for delete_if(). 687 1.1 christos */ 688 1.1 christos static isc_result_t 689 1.1 christos delete_if_action(void *data, rr_t *rr) { 690 1.1 christos conditional_delete_ctx_t *ctx = data; 691 1.1 christos if ((*ctx->predicate)(ctx->update_rr, &rr->rdata)) { 692 1.1 christos isc_result_t result; 693 1.1 christos result = update_one_rr(ctx->db, ctx->ver, ctx->diff, 694 1.1 christos DNS_DIFFOP_DEL, ctx->name, rr->ttl, 695 1.1 christos &rr->rdata); 696 1.1 christos return (result); 697 1.1 christos } else { 698 1.1 christos return (ISC_R_SUCCESS); 699 1.1 christos } 700 1.1 christos } 701 1.1 christos 702 1.1 christos /*% 703 1.1 christos * Conditionally delete RRs. Apply 'predicate' to the RRs 704 1.1 christos * specified by 'db', 'ver', 'name', and 'type' (which can 705 1.1 christos * be dns_rdatatype_any to match any type). Delete those 706 1.1 christos * RRs for which the predicate returns true, and log the 707 1.1 christos * deletions in 'diff'. 708 1.1 christos */ 709 1.1 christos static isc_result_t 710 1.1 christos delete_if(rr_predicate *predicate, dns_db_t *db, dns_dbversion_t *ver, 711 1.1 christos dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers, 712 1.1 christos dns_rdata_t *update_rr, dns_diff_t *diff) { 713 1.1 christos conditional_delete_ctx_t ctx; 714 1.1 christos ctx.predicate = predicate; 715 1.1 christos ctx.db = db; 716 1.1 christos ctx.ver = ver; 717 1.1 christos ctx.diff = diff; 718 1.1 christos ctx.name = name; 719 1.1 christos ctx.update_rr = update_rr; 720 1.1 christos return (foreach_rr(db, ver, name, type, covers, delete_if_action, 721 1.1 christos &ctx)); 722 1.1 christos } 723 1.1 christos 724 1.1 christos /**************************************************************************/ 725 1.1 christos /* 726 1.1 christos * Incremental updating of NSECs and RRSIGs. 727 1.1 christos */ 728 1.1 christos 729 1.1 christos /*% 730 1.1 christos * We abuse the dns_diff_t type to represent a set of domain names 731 1.1 christos * affected by the update. 732 1.1 christos */ 733 1.1 christos static isc_result_t 734 1.1 christos namelist_append_name(dns_diff_t *list, dns_name_t *name) { 735 1.1 christos isc_result_t result; 736 1.1 christos dns_difftuple_t *tuple = NULL; 737 1.1 christos static dns_rdata_t dummy_rdata = DNS_RDATA_INIT; 738 1.1 christos 739 1.1 christos CHECK(dns_difftuple_create(list->mctx, DNS_DIFFOP_EXISTS, name, 0, 740 1.1 christos &dummy_rdata, &tuple)); 741 1.1 christos dns_diff_append(list, &tuple); 742 1.1 christos failure: 743 1.1 christos return (result); 744 1.1 christos } 745 1.1 christos 746 1.1 christos static isc_result_t 747 1.1 christos namelist_append_subdomain(dns_db_t *db, dns_name_t *name, 748 1.1 christos dns_diff_t *affected) { 749 1.1 christos isc_result_t result; 750 1.1 christos dns_fixedname_t fixedname; 751 1.1 christos dns_name_t *child; 752 1.1 christos dns_dbiterator_t *dbit = NULL; 753 1.1 christos 754 1.1 christos child = dns_fixedname_initname(&fixedname); 755 1.1 christos 756 1.1 christos CHECK(dns_db_createiterator(db, DNS_DB_NONSEC3, &dbit)); 757 1.1 christos 758 1.1 christos for (result = dns_dbiterator_seek(dbit, name); result == ISC_R_SUCCESS; 759 1.1 christos result = dns_dbiterator_next(dbit)) 760 1.1 christos { 761 1.1 christos dns_dbnode_t *node = NULL; 762 1.1 christos CHECK(dns_dbiterator_current(dbit, &node, child)); 763 1.1 christos dns_db_detachnode(db, &node); 764 1.1 christos if (!dns_name_issubdomain(child, name)) { 765 1.1 christos break; 766 1.1 christos } 767 1.1 christos CHECK(namelist_append_name(affected, child)); 768 1.1 christos } 769 1.1 christos if (result == ISC_R_NOMORE) { 770 1.1 christos result = ISC_R_SUCCESS; 771 1.1 christos } 772 1.1 christos failure: 773 1.1 christos if (dbit != NULL) { 774 1.1 christos dns_dbiterator_destroy(&dbit); 775 1.1 christos } 776 1.1 christos return (result); 777 1.1 christos } 778 1.1 christos 779 1.1 christos /*% 780 1.1 christos * Helper function for non_nsec_rrset_exists(). 781 1.1 christos */ 782 1.1 christos static isc_result_t 783 1.1 christos is_non_nsec_action(void *data, dns_rdataset_t *rrset) { 784 1.1 christos UNUSED(data); 785 1.1 christos if (!(rrset->type == dns_rdatatype_nsec || 786 1.1 christos rrset->type == dns_rdatatype_nsec3 || 787 1.1 christos (rrset->type == dns_rdatatype_rrsig && 788 1.1 christos (rrset->covers == dns_rdatatype_nsec || 789 1.1 christos rrset->covers == dns_rdatatype_nsec3)))) 790 1.1 christos { 791 1.1 christos return (ISC_R_EXISTS); 792 1.1 christos } 793 1.1 christos return (ISC_R_SUCCESS); 794 1.1 christos } 795 1.1 christos 796 1.1 christos /*% 797 1.1 christos * Check whether there is an rrset other than a NSEC or RRSIG NSEC, 798 1.1 christos * i.e., anything that justifies the continued existence of a name 799 1.1 christos * after a secure update. 800 1.1 christos * 801 1.1 christos * If such an rrset exists, set '*exists' to true. 802 1.1 christos * Otherwise, set it to false. 803 1.1 christos */ 804 1.1 christos static isc_result_t 805 1.1 christos non_nsec_rrset_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, 806 1.1 christos bool *exists) { 807 1.1 christos isc_result_t result; 808 1.1 christos result = foreach_rrset(db, ver, name, is_non_nsec_action, NULL); 809 1.1 christos RETURN_EXISTENCE_FLAG; 810 1.1 christos } 811 1.1 christos 812 1.1 christos /*% 813 1.1 christos * A comparison function for sorting dns_diff_t:s by name. 814 1.1 christos */ 815 1.1 christos static int 816 1.1 christos name_order(const void *av, const void *bv) { 817 1.1 christos dns_difftuple_t const *const *ap = av; 818 1.1 christos dns_difftuple_t const *const *bp = bv; 819 1.1 christos dns_difftuple_t const *a = *ap; 820 1.1 christos dns_difftuple_t const *b = *bp; 821 1.1 christos return (dns_name_compare(&a->name, &b->name)); 822 1.1 christos } 823 1.1 christos 824 1.1 christos static isc_result_t 825 1.1 christos uniqify_name_list(dns_diff_t *list) { 826 1.1 christos isc_result_t result; 827 1.1 christos dns_difftuple_t *p, *q; 828 1.1 christos 829 1.1 christos CHECK(dns_diff_sort(list, name_order)); 830 1.1 christos 831 1.1 christos p = ISC_LIST_HEAD(list->tuples); 832 1.1 christos while (p != NULL) { 833 1.1 christos do { 834 1.1 christos q = ISC_LIST_NEXT(p, link); 835 1.1 christos if (q == NULL || !dns_name_equal(&p->name, &q->name)) { 836 1.1 christos break; 837 1.1 christos } 838 1.1 christos ISC_LIST_UNLINK(list->tuples, q, link); 839 1.1 christos dns_difftuple_free(&q); 840 1.1 christos } while (1); 841 1.1 christos p = ISC_LIST_NEXT(p, link); 842 1.1 christos } 843 1.1 christos failure: 844 1.1 christos return (result); 845 1.1 christos } 846 1.1 christos 847 1.1 christos static isc_result_t 848 1.1 christos is_active(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, bool *flag, 849 1.1 christos bool *cut, bool *unsecure) { 850 1.1 christos isc_result_t result; 851 1.1 christos dns_fixedname_t foundname; 852 1.1 christos dns_fixedname_init(&foundname); 853 1.1 christos result = dns_db_find(db, name, ver, dns_rdatatype_any, 854 1.1 christos DNS_DBFIND_GLUEOK | DNS_DBFIND_NOWILD, 855 1.1 christos (isc_stdtime_t)0, NULL, 856 1.1 christos dns_fixedname_name(&foundname), NULL, NULL); 857 1.1 christos if (result == ISC_R_SUCCESS || result == DNS_R_EMPTYNAME) { 858 1.1 christos *flag = true; 859 1.1 christos *cut = false; 860 1.1 christos if (unsecure != NULL) { 861 1.1 christos *unsecure = false; 862 1.1 christos } 863 1.1 christos return (ISC_R_SUCCESS); 864 1.1 christos } else if (result == DNS_R_ZONECUT) { 865 1.1 christos *flag = true; 866 1.1 christos *cut = true; 867 1.1 christos if (unsecure != NULL) { 868 1.1 christos /* 869 1.1 christos * We are at the zonecut. Check to see if there 870 1.1 christos * is a DS RRset. 871 1.1 christos */ 872 1.1 christos if (dns_db_find(db, name, ver, dns_rdatatype_ds, 0, 873 1.1 christos (isc_stdtime_t)0, NULL, 874 1.1 christos dns_fixedname_name(&foundname), NULL, 875 1.1 christos NULL) == DNS_R_NXRRSET) 876 1.1 christos { 877 1.1 christos *unsecure = true; 878 1.1 christos } else { 879 1.1 christos *unsecure = false; 880 1.1 christos } 881 1.1 christos } 882 1.1 christos return (ISC_R_SUCCESS); 883 1.1 christos } else if (result == DNS_R_GLUE || result == DNS_R_DNAME || 884 1.1 christos result == DNS_R_DELEGATION || result == DNS_R_NXDOMAIN) 885 1.1 christos { 886 1.1 christos *flag = false; 887 1.1 christos *cut = false; 888 1.1 christos if (unsecure != NULL) { 889 1.1 christos *unsecure = false; 890 1.1 christos } 891 1.1 christos return (ISC_R_SUCCESS); 892 1.1 christos } else { 893 1.1 christos /* 894 1.1 christos * Silence compiler. 895 1.1 christos */ 896 1.1 christos *flag = false; 897 1.1 christos *cut = false; 898 1.1 christos if (unsecure != NULL) { 899 1.1 christos *unsecure = false; 900 1.1 christos } 901 1.1 christos return (result); 902 1.1 christos } 903 1.1 christos } 904 1.1 christos 905 1.1 christos /*% 906 1.1 christos * Find the next/previous name that has a NSEC record. 907 1.1 christos * In other words, skip empty database nodes and names that 908 1.1 christos * have had their NSECs removed because they are obscured by 909 1.1 christos * a zone cut. 910 1.1 christos */ 911 1.1 christos static isc_result_t 912 1.1 christos next_active(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db, 913 1.1 christos dns_dbversion_t *ver, dns_name_t *oldname, dns_name_t *newname, 914 1.1 christos bool forward) { 915 1.1 christos isc_result_t result; 916 1.1 christos dns_dbiterator_t *dbit = NULL; 917 1.1 christos bool has_nsec = false; 918 1.1 christos unsigned int wraps = 0; 919 1.1 christos bool secure = dns_db_issecure(db); 920 1.1 christos 921 1.1 christos CHECK(dns_db_createiterator(db, 0, &dbit)); 922 1.1 christos 923 1.1 christos CHECK(dns_dbiterator_seek(dbit, oldname)); 924 1.1 christos do { 925 1.1 christos dns_dbnode_t *node = NULL; 926 1.1 christos 927 1.1 christos if (forward) { 928 1.1 christos result = dns_dbiterator_next(dbit); 929 1.1 christos } else { 930 1.1 christos result = dns_dbiterator_prev(dbit); 931 1.1 christos } 932 1.1 christos if (result == ISC_R_NOMORE) { 933 1.1 christos /* 934 1.1 christos * Wrap around. 935 1.1 christos */ 936 1.1 christos if (forward) { 937 1.1 christos CHECK(dns_dbiterator_first(dbit)); 938 1.1 christos } else { 939 1.1 christos CHECK(dns_dbiterator_last(dbit)); 940 1.1 christos } 941 1.1 christos wraps++; 942 1.1 christos if (wraps == 2) { 943 1.1 christos update_log(log, zone, ISC_LOG_ERROR, 944 1.1 christos "secure zone with no NSECs"); 945 1.1 christos result = DNS_R_BADZONE; 946 1.1 christos goto failure; 947 1.1 christos } 948 1.1 christos } 949 1.1 christos CHECK(dns_dbiterator_current(dbit, &node, newname)); 950 1.1 christos dns_db_detachnode(db, &node); 951 1.1 christos 952 1.1 christos /* 953 1.1 christos * The iterator may hold the tree lock, and 954 1.1 christos * rrset_exists() calls dns_db_findnode() which 955 1.1 christos * may try to reacquire it. To avoid deadlock 956 1.1 christos * we must pause the iterator first. 957 1.1 christos */ 958 1.1 christos CHECK(dns_dbiterator_pause(dbit)); 959 1.1 christos if (secure) { 960 1.1 christos CHECK(rrset_exists(db, ver, newname, dns_rdatatype_nsec, 961 1.1 christos 0, &has_nsec)); 962 1.1 christos } else { 963 1.1 christos dns_fixedname_t ffound; 964 1.1 christos dns_name_t *found; 965 1.1 christos found = dns_fixedname_initname(&ffound); 966 1.1 christos result = dns_db_find( 967 1.1 christos db, newname, ver, dns_rdatatype_soa, 968 1.1 christos DNS_DBFIND_NOWILD, 0, NULL, found, NULL, NULL); 969 1.1 christos if (result == ISC_R_SUCCESS || 970 1.1 christos result == DNS_R_EMPTYNAME || 971 1.1 christos result == DNS_R_NXRRSET || result == DNS_R_CNAME || 972 1.1 christos (result == DNS_R_DELEGATION && 973 1.1 christos dns_name_equal(newname, found))) 974 1.1 christos { 975 1.1 christos has_nsec = true; 976 1.1 christos result = ISC_R_SUCCESS; 977 1.1 christos } else if (result != DNS_R_NXDOMAIN) { 978 1.1 christos break; 979 1.1 christos } 980 1.1 christos } 981 1.1 christos } while (!has_nsec); 982 1.1 christos failure: 983 1.1 christos if (dbit != NULL) { 984 1.1 christos dns_dbiterator_destroy(&dbit); 985 1.1 christos } 986 1.1 christos 987 1.1 christos return (result); 988 1.1 christos } 989 1.1 christos 990 1.1 christos /*% 991 1.1 christos * Add a NSEC record for "name", recording the change in "diff". 992 1.1 christos * The existing NSEC is removed. 993 1.1 christos */ 994 1.1 christos static isc_result_t 995 1.1 christos add_nsec(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db, 996 1.1 christos dns_dbversion_t *ver, dns_name_t *name, dns_ttl_t nsecttl, 997 1.1 christos dns_diff_t *diff) { 998 1.1 christos isc_result_t result; 999 1.1 christos dns_dbnode_t *node = NULL; 1000 1.1 christos unsigned char buffer[DNS_NSEC_BUFFERSIZE]; 1001 1.1 christos dns_rdata_t rdata = DNS_RDATA_INIT; 1002 1.1 christos dns_difftuple_t *tuple = NULL; 1003 1.1 christos dns_fixedname_t fixedname; 1004 1.1 christos dns_name_t *target; 1005 1.1 christos 1006 1.1 christos target = dns_fixedname_initname(&fixedname); 1007 1.1 christos 1008 1.1 christos /* 1009 1.1 christos * Find the successor name, aka NSEC target. 1010 1.1 christos */ 1011 1.1 christos CHECK(next_active(log, zone, db, ver, name, target, true)); 1012 1.1 christos 1013 1.1 christos /* 1014 1.1 christos * Create the NSEC RDATA. 1015 1.1 christos */ 1016 1.1 christos CHECK(dns_db_findnode(db, name, false, &node)); 1017 1.1 christos dns_rdata_init(&rdata); 1018 1.1 christos CHECK(dns_nsec_buildrdata(db, ver, node, target, buffer, &rdata)); 1019 1.1 christos dns_db_detachnode(db, &node); 1020 1.1 christos 1021 1.1 christos /* 1022 1.1 christos * Delete the old NSEC and record the change. 1023 1.1 christos */ 1024 1.1 christos CHECK(delete_if(true_p, db, ver, name, dns_rdatatype_nsec, 0, NULL, 1025 1.1 christos diff)); 1026 1.1 christos /* 1027 1.1 christos * Add the new NSEC and record the change. 1028 1.1 christos */ 1029 1.1 christos CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, nsecttl, 1030 1.1 christos &rdata, &tuple)); 1031 1.1 christos CHECK(do_one_tuple(&tuple, db, ver, diff)); 1032 1.1 christos INSIST(tuple == NULL); 1033 1.1 christos 1034 1.1 christos failure: 1035 1.1 christos if (node != NULL) { 1036 1.1 christos dns_db_detachnode(db, &node); 1037 1.1 christos } 1038 1.1 christos return (result); 1039 1.1 christos } 1040 1.1 christos 1041 1.1 christos /*% 1042 1.1 christos * Add a placeholder NSEC record for "name", recording the change in "diff". 1043 1.1 christos */ 1044 1.1 christos static isc_result_t 1045 1.1 christos add_placeholder_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, 1046 1.1 christos dns_diff_t *diff) { 1047 1.1 christos isc_result_t result; 1048 1.1 christos dns_difftuple_t *tuple = NULL; 1049 1.1 christos isc_region_t r; 1050 1.1 christos unsigned char data[1] = { 0 }; /* The root domain, no bits. */ 1051 1.1 christos dns_rdata_t rdata = DNS_RDATA_INIT; 1052 1.1 christos 1053 1.1 christos r.base = data; 1054 1.1 christos r.length = sizeof(data); 1055 1.1 christos dns_rdata_fromregion(&rdata, dns_db_class(db), dns_rdatatype_nsec, &r); 1056 1.1 christos CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, 0, &rdata, 1057 1.1 christos &tuple)); 1058 1.1 christos CHECK(do_one_tuple(&tuple, db, ver, diff)); 1059 1.1 christos failure: 1060 1.1 christos return (result); 1061 1.1 christos } 1062 1.1 christos 1063 1.1 christos static isc_result_t 1064 1.1 christos find_zone_keys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, 1065 1.1 christos isc_mem_t *mctx, unsigned int maxkeys, dst_key_t **keys, 1066 1.1 christos unsigned int *nkeys) { 1067 1.1 christos isc_result_t result; 1068 1.1 christos isc_stdtime_t now; 1069 1.1 christos dns_dbnode_t *node = NULL; 1070 1.1 christos const char *directory = dns_zone_getkeydirectory(zone); 1071 1.1 christos 1072 1.1 christos CHECK(dns_db_findnode(db, dns_db_origin(db), false, &node)); 1073 1.1 christos isc_stdtime_get(&now); 1074 1.1 christos 1075 1.1 christos dns_zone_lock_keyfiles(zone); 1076 1.1 christos result = dns_dnssec_findzonekeys(db, ver, node, dns_db_origin(db), 1077 1.1 christos directory, now, mctx, maxkeys, keys, 1078 1.1 christos nkeys); 1079 1.1 christos dns_zone_unlock_keyfiles(zone); 1080 1.1 christos 1081 1.1 christos failure: 1082 1.1 christos if (node != NULL) { 1083 1.1 christos dns_db_detachnode(db, &node); 1084 1.1 christos } 1085 1.1 christos return (result); 1086 1.1 christos } 1087 1.1 christos 1088 1.1 christos /*% 1089 1.1 christos * Add RRSIG records for an RRset, recording the change in "diff". 1090 1.1 christos */ 1091 1.1 christos static isc_result_t 1092 1.1 christos add_sigs(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db, 1093 1.1 christos dns_dbversion_t *ver, dns_name_t *name, dns_rdatatype_t type, 1094 1.1 christos dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys, 1095 1.1 christos isc_stdtime_t inception, isc_stdtime_t expire, bool check_ksk, 1096 1.1 christos bool keyset_kskonly) { 1097 1.1 christos isc_result_t result; 1098 1.1 christos dns_dbnode_t *node = NULL; 1099 1.1 christos dns_kasp_t *kasp = dns_zone_getkasp(zone); 1100 1.1 christos dns_rdataset_t rdataset; 1101 1.1 christos dns_rdata_t sig_rdata = DNS_RDATA_INIT; 1102 1.1 christos dns_stats_t *dnssecsignstats = dns_zone_getdnssecsignstats(zone); 1103 1.1 christos isc_buffer_t buffer; 1104 1.1 christos unsigned char data[1024]; /* XXX */ 1105 1.1 christos unsigned int i, j; 1106 1.1 christos bool added_sig = false; 1107 1.1 christos bool use_kasp = false; 1108 1.1 christos isc_mem_t *mctx = diff->mctx; 1109 1.1 christos 1110 1.1 christos if (kasp != NULL) { 1111 1.1 christos check_ksk = false; 1112 1.1 christos keyset_kskonly = true; 1113 1.1 christos use_kasp = true; 1114 1.1 christos } 1115 1.1 christos 1116 1.1 christos dns_rdataset_init(&rdataset); 1117 1.1 christos isc_buffer_init(&buffer, data, sizeof(data)); 1118 1.1 christos 1119 1.1 christos /* Get the rdataset to sign. */ 1120 1.1 christos if (type == dns_rdatatype_nsec3) { 1121 1.1 christos CHECK(dns_db_findnsec3node(db, name, false, &node)); 1122 1.1 christos } else { 1123 1.1 christos CHECK(dns_db_findnode(db, name, false, &node)); 1124 1.1 christos } 1125 1.1 christos CHECK(dns_db_findrdataset(db, node, ver, type, 0, (isc_stdtime_t)0, 1126 1.1 christos &rdataset, NULL)); 1127 1.1 christos dns_db_detachnode(db, &node); 1128 1.1 christos 1129 1.1 christos #define REVOKE(x) ((dst_key_flags(x) & DNS_KEYFLAG_REVOKE) != 0) 1130 1.1 christos #define KSK(x) ((dst_key_flags(x) & DNS_KEYFLAG_KSK) != 0) 1131 1.1 christos #define ID(x) dst_key_id(x) 1132 1.1 christos #define ALG(x) dst_key_alg(x) 1133 1.1 christos 1134 1.1 christos /* 1135 1.1 christos * If we are honoring KSK flags then we need to check that we 1136 1.1 christos * have both KSK and non-KSK keys that are not revoked per 1137 1.1 christos * algorithm. 1138 1.1 christos */ 1139 1.1 christos for (i = 0; i < nkeys; i++) { 1140 1.1 christos bool both = false; 1141 1.1 christos 1142 1.1 christos /* Don't add signatures for offline or inactive keys */ 1143 1.1 christos if (!dst_key_isprivate(keys[i])) { 1144 1.1 christos continue; 1145 1.1 christos } 1146 1.1 christos if (dst_key_inactive(keys[i])) { 1147 1.1 christos continue; 1148 1.1 christos } 1149 1.1 christos 1150 1.1 christos if (check_ksk && !REVOKE(keys[i])) { 1151 1.1 christos bool have_ksk, have_nonksk; 1152 1.1 christos if (KSK(keys[i])) { 1153 1.1 christos have_ksk = true; 1154 1.1 christos have_nonksk = false; 1155 1.1 christos } else { 1156 1.1 christos have_ksk = false; 1157 1.1 christos have_nonksk = true; 1158 1.1 christos } 1159 1.1 christos for (j = 0; j < nkeys; j++) { 1160 1.1 christos if (j == i || ALG(keys[i]) != ALG(keys[j])) { 1161 1.1 christos continue; 1162 1.1 christos } 1163 1.1 christos 1164 1.1 christos /* Don't consider inactive keys, however 1165 1.1 christos * the KSK may be temporary offline, so do 1166 1.1 christos * consider KSKs which private key files are 1167 1.1 christos * unavailable. 1168 1.1 christos */ 1169 1.1 christos if (dst_key_inactive(keys[j])) { 1170 1.1 christos continue; 1171 1.1 christos } 1172 1.1 christos 1173 1.1 christos if (REVOKE(keys[j])) { 1174 1.1 christos continue; 1175 1.1 christos } 1176 1.1 christos if (KSK(keys[j])) { 1177 1.1 christos have_ksk = true; 1178 1.1 christos } else if (dst_key_isprivate(keys[j])) { 1179 1.1 christos have_nonksk = true; 1180 1.1 christos } 1181 1.1 christos both = have_ksk && have_nonksk; 1182 1.1 christos if (both) { 1183 1.1 christos break; 1184 1.1 christos } 1185 1.1 christos } 1186 1.1 christos } 1187 1.1 christos 1188 1.1 christos if (use_kasp) { 1189 1.1 christos /* 1190 1.1 christos * A dnssec-policy is found. Check what RRsets this 1191 1.1 christos * key should sign. 1192 1.1 christos */ 1193 1.1 christos isc_stdtime_t when; 1194 1.1 christos isc_result_t kresult; 1195 1.1 christos bool ksk = false; 1196 1.1 christos bool zsk = false; 1197 1.1 christos 1198 1.1 christos kresult = dst_key_getbool(keys[i], DST_BOOL_KSK, &ksk); 1199 1.1 christos if (kresult != ISC_R_SUCCESS) { 1200 1.1 christos if (KSK(keys[i])) { 1201 1.1 christos ksk = true; 1202 1.1 christos } 1203 1.1 christos } 1204 1.1 christos kresult = dst_key_getbool(keys[i], DST_BOOL_ZSK, &zsk); 1205 1.1 christos if (kresult != ISC_R_SUCCESS) { 1206 1.1 christos if (!KSK(keys[i])) { 1207 1.1 christos zsk = true; 1208 1.1 christos } 1209 1.1 christos } 1210 1.1 christos 1211 1.1 christos if (type == dns_rdatatype_dnskey || 1212 1.1 christos type == dns_rdatatype_cdnskey || 1213 1.1 christos type == dns_rdatatype_cds) 1214 1.1 christos { 1215 1.1 christos /* 1216 1.1 christos * DNSKEY RRset is signed with KSK. 1217 1.1 christos * CDS and CDNSKEY RRsets too (RFC 7344, 4.1). 1218 1.1 christos */ 1219 1.1 christos if (!ksk) { 1220 1.1 christos continue; 1221 1.1 christos } 1222 1.1 christos } else if (!zsk) { 1223 1.1 christos /* 1224 1.1 christos * Other RRsets are signed with ZSK. 1225 1.1 christos */ 1226 1.1 christos continue; 1227 1.1 christos } else if (zsk && 1228 1.1 christos !dst_key_is_signing(keys[i], DST_BOOL_ZSK, 1229 1.1 christos inception, &when)) 1230 1.1 christos { 1231 1.1 christos /* 1232 1.1 christos * This key is not active for zone-signing. 1233 1.1 christos */ 1234 1.1 christos continue; 1235 1.1 christos } 1236 1.1 christos 1237 1.1 christos /* 1238 1.1 christos * If this key is revoked, it may only sign the 1239 1.1 christos * DNSKEY RRset. 1240 1.1 christos */ 1241 1.1 christos if (REVOKE(keys[i]) && type != dns_rdatatype_dnskey) { 1242 1.1 christos continue; 1243 1.1 christos } 1244 1.1 christos } else if (both) { 1245 1.1 christos /* 1246 1.1 christos * CDS and CDNSKEY are signed with KSK (RFC 7344, 4.1). 1247 1.1 christos */ 1248 1.1 christos if (type == dns_rdatatype_dnskey || 1249 1.1 christos type == dns_rdatatype_cdnskey || 1250 1.1 christos type == dns_rdatatype_cds) 1251 1.1 christos { 1252 1.1 christos if (!KSK(keys[i]) && keyset_kskonly) { 1253 1.1 christos continue; 1254 1.1 christos } 1255 1.1 christos } else if (KSK(keys[i])) { 1256 1.1 christos continue; 1257 1.1 christos } 1258 1.1 christos } else if (REVOKE(keys[i]) && type != dns_rdatatype_dnskey) { 1259 1.1 christos continue; 1260 1.1 christos } 1261 1.1 christos 1262 1.1 christos /* Calculate the signature, creating a RRSIG RDATA. */ 1263 1.1 christos CHECK(dns_dnssec_sign(name, &rdataset, keys[i], &inception, 1264 1.1 christos &expire, mctx, &buffer, &sig_rdata)); 1265 1.1 christos 1266 1.1 christos /* Update the database and journal with the RRSIG. */ 1267 1.1 christos /* XXX inefficient - will cause dataset merging */ 1268 1.1 christos CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADDRESIGN, name, 1269 1.1 christos rdataset.ttl, &sig_rdata)); 1270 1.1 christos dns_rdata_reset(&sig_rdata); 1271 1.1 christos isc_buffer_init(&buffer, data, sizeof(data)); 1272 1.1 christos added_sig = true; 1273 1.1 christos /* Update DNSSEC sign statistics. */ 1274 1.1 christos if (dnssecsignstats != NULL) { 1275 1.1 christos dns_dnssecsignstats_increment(dnssecsignstats, 1276 1.1 christos ID(keys[i]), 1277 1.1 christos (uint8_t)ALG(keys[i]), 1278 1.1 christos dns_dnssecsignstats_sign); 1279 1.1 christos } 1280 1.1 christos } 1281 1.1 christos if (!added_sig) { 1282 1.1 christos update_log(log, zone, ISC_LOG_ERROR, 1283 1.1 christos "found no active private keys, " 1284 1.1 christos "unable to generate any signatures"); 1285 1.1 christos result = ISC_R_NOTFOUND; 1286 1.1 christos } 1287 1.1 christos 1288 1.1 christos failure: 1289 1.1 christos if (dns_rdataset_isassociated(&rdataset)) { 1290 1.1 christos dns_rdataset_disassociate(&rdataset); 1291 1.1 christos } 1292 1.1 christos if (node != NULL) { 1293 1.1 christos dns_db_detachnode(db, &node); 1294 1.1 christos } 1295 1.1 christos return (result); 1296 1.1 christos } 1297 1.1 christos 1298 1.1 christos /* 1299 1.1 christos * Delete expired RRsigs and any RRsigs we are about to re-sign. 1300 1.1 christos * See also zone.c:del_sigs(). 1301 1.1 christos */ 1302 1.1 christos static isc_result_t 1303 1.1 christos del_keysigs(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, 1304 1.1 christos dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys) { 1305 1.1 christos isc_result_t result; 1306 1.1 christos dns_dbnode_t *node = NULL; 1307 1.1 christos dns_rdataset_t rdataset; 1308 1.1 christos dns_rdata_t rdata = DNS_RDATA_INIT; 1309 1.1 christos unsigned int i; 1310 1.1 christos dns_rdata_rrsig_t rrsig; 1311 1.1 christos bool found; 1312 1.1 christos 1313 1.1 christos dns_rdataset_init(&rdataset); 1314 1.1 christos 1315 1.1 christos result = dns_db_findnode(db, name, false, &node); 1316 1.1 christos if (result == ISC_R_NOTFOUND) { 1317 1.1 christos return (ISC_R_SUCCESS); 1318 1.1 christos } 1319 1.1 christos if (result != ISC_R_SUCCESS) { 1320 1.1 christos goto failure; 1321 1.1 christos } 1322 1.1 christos result = dns_db_findrdataset(db, node, ver, dns_rdatatype_rrsig, 1323 1.1 christos dns_rdatatype_dnskey, (isc_stdtime_t)0, 1324 1.1 christos &rdataset, NULL); 1325 1.1 christos dns_db_detachnode(db, &node); 1326 1.1 christos 1327 1.1 christos if (result == ISC_R_NOTFOUND) { 1328 1.1 christos return (ISC_R_SUCCESS); 1329 1.1 christos } 1330 1.1 christos if (result != ISC_R_SUCCESS) { 1331 1.1 christos goto failure; 1332 1.1 christos } 1333 1.1 christos 1334 1.1 christos for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS; 1335 1.1 christos result = dns_rdataset_next(&rdataset)) 1336 1.1 christos { 1337 1.1 christos dns_rdataset_current(&rdataset, &rdata); 1338 1.1 christos result = dns_rdata_tostruct(&rdata, &rrsig, NULL); 1339 1.1 christos RUNTIME_CHECK(result == ISC_R_SUCCESS); 1340 1.1 christos found = false; 1341 1.1 christos for (i = 0; i < nkeys; i++) { 1342 1.1 christos if (rrsig.keyid == dst_key_id(keys[i])) { 1343 1.1 christos found = true; 1344 1.1 christos if (!dst_key_isprivate(keys[i]) && 1345 1.1 christos !dst_key_inactive(keys[i])) 1346 1.1 christos { 1347 1.1 christos /* 1348 1.1 christos * The re-signing code in zone.c 1349 1.1 christos * will mark this as offline. 1350 1.1 christos * Just skip the record for now. 1351 1.1 christos */ 1352 1.1 christos break; 1353 1.1 christos } 1354 1.1 christos result = update_one_rr(db, ver, diff, 1355 1.1 christos DNS_DIFFOP_DEL, name, 1356 1.1 christos rdataset.ttl, &rdata); 1357 1.1 christos break; 1358 1.1 christos } 1359 1.1 christos } 1360 1.1 christos /* 1361 1.1 christos * If there is not a matching DNSKEY then delete the RRSIG. 1362 1.1 christos */ 1363 1.1 christos if (!found) { 1364 1.1 christos result = update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, 1365 1.1 christos name, rdataset.ttl, &rdata); 1366 1.1 christos } 1367 1.1 christos dns_rdata_reset(&rdata); 1368 1.1 christos if (result != ISC_R_SUCCESS) { 1369 1.1 christos break; 1370 1.1 christos } 1371 1.1 christos } 1372 1.1 christos dns_rdataset_disassociate(&rdataset); 1373 1.1 christos if (result == ISC_R_NOMORE) { 1374 1.1 christos result = ISC_R_SUCCESS; 1375 1.1 christos } 1376 1.1 christos failure: 1377 1.1 christos if (node != NULL) { 1378 1.1 christos dns_db_detachnode(db, &node); 1379 1.1 christos } 1380 1.1 christos return (result); 1381 1.1 christos } 1382 1.1 christos 1383 1.1 christos static isc_result_t 1384 1.1 christos add_exposed_sigs(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db, 1385 1.1 christos dns_dbversion_t *ver, dns_name_t *name, bool cut, 1386 1.1 christos dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys, 1387 1.1 christos isc_stdtime_t inception, isc_stdtime_t expire, bool check_ksk, 1388 1.1 christos bool keyset_kskonly, unsigned int *sigs) { 1389 1.1 christos isc_result_t result; 1390 1.1 christos dns_dbnode_t *node; 1391 1.1 christos dns_rdatasetiter_t *iter; 1392 1.1 christos 1393 1.1 christos node = NULL; 1394 1.1 christos result = dns_db_findnode(db, name, false, &node); 1395 1.1 christos if (result == ISC_R_NOTFOUND) { 1396 1.1 christos return (ISC_R_SUCCESS); 1397 1.1 christos } 1398 1.1 christos if (result != ISC_R_SUCCESS) { 1399 1.1 christos return (result); 1400 1.1 christos } 1401 1.1 christos 1402 1.1 christos iter = NULL; 1403 1.1 christos result = dns_db_allrdatasets(db, node, ver, 0, (isc_stdtime_t)0, &iter); 1404 1.1 christos if (result != ISC_R_SUCCESS) { 1405 1.1 christos goto cleanup_node; 1406 1.1 christos } 1407 1.1 christos 1408 1.1 christos for (result = dns_rdatasetiter_first(iter); result == ISC_R_SUCCESS; 1409 1.1 christos result = dns_rdatasetiter_next(iter)) 1410 1.1 christos { 1411 1.1 christos dns_rdataset_t rdataset; 1412 1.1 christos dns_rdatatype_t type; 1413 1.1 christos bool flag; 1414 1.1 christos 1415 1.1 christos dns_rdataset_init(&rdataset); 1416 1.1 christos dns_rdatasetiter_current(iter, &rdataset); 1417 1.1 christos type = rdataset.type; 1418 1.1 christos dns_rdataset_disassociate(&rdataset); 1419 1.1 christos 1420 1.1 christos /* 1421 1.1 christos * We don't need to sign unsigned NSEC records at the cut 1422 1.1 christos * as they are handled elsewhere. 1423 1.1 christos */ 1424 1.1 christos if ((type == dns_rdatatype_rrsig) || 1425 1.1 christos (cut && type != dns_rdatatype_ds)) 1426 1.1 christos { 1427 1.1 christos continue; 1428 1.1 christos } 1429 1.1 christos result = rrset_exists(db, ver, name, dns_rdatatype_rrsig, type, 1430 1.1 christos &flag); 1431 1.1 christos if (result != ISC_R_SUCCESS) { 1432 1.1 christos goto cleanup_iterator; 1433 1.1 christos } 1434 1.1 christos if (flag) { 1435 1.1 christos continue; 1436 1.1 christos } 1437 1.1 christos result = add_sigs(log, zone, db, ver, name, type, diff, keys, 1438 1.1 christos nkeys, inception, expire, check_ksk, 1439 1.1 christos keyset_kskonly); 1440 1.1 christos if (result != ISC_R_SUCCESS) { 1441 1.1 christos goto cleanup_iterator; 1442 1.1 christos } 1443 1.1 christos (*sigs)++; 1444 1.1 christos } 1445 1.1 christos if (result == ISC_R_NOMORE) { 1446 1.1 christos result = ISC_R_SUCCESS; 1447 1.1 christos } 1448 1.1 christos 1449 1.1 christos cleanup_iterator: 1450 1.1 christos dns_rdatasetiter_destroy(&iter); 1451 1.1 christos 1452 1.1 christos cleanup_node: 1453 1.1 christos dns_db_detachnode(db, &node); 1454 1.1 christos 1455 1.1 christos return (result); 1456 1.1 christos } 1457 1.1 christos 1458 1.1 christos /*% 1459 1.1 christos * Update RRSIG, NSEC and NSEC3 records affected by an update. The original 1460 1.1 christos * update, including the SOA serial update but excluding the RRSIG & NSEC 1461 1.1 christos * changes, is in "diff" and has already been applied to "newver" of "db". 1462 1.1 christos * The database version prior to the update is "oldver". 1463 1.1 christos * 1464 1.1 christos * The necessary RRSIG, NSEC and NSEC3 changes will be applied to "newver" 1465 1.1 christos * and added (as a minimal diff) to "diff". 1466 1.1 christos * 1467 1.1 christos * The RRSIGs generated will be valid for 'sigvalidityinterval' seconds. 1468 1.1 christos */ 1469 1.1 christos isc_result_t 1470 1.1 christos dns_update_signatures(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db, 1471 1.1 christos dns_dbversion_t *oldver, dns_dbversion_t *newver, 1472 1.1 christos dns_diff_t *diff, uint32_t sigvalidityinterval) { 1473 1.1 christos return (dns_update_signaturesinc(log, zone, db, oldver, newver, diff, 1474 1.1 christos sigvalidityinterval, NULL)); 1475 1.1 christos } 1476 1.1 christos 1477 1.1 christos struct dns_update_state { 1478 1.1 christos unsigned int magic; 1479 1.1 christos dns_diff_t diffnames; 1480 1.1 christos dns_diff_t affected; 1481 1.1 christos dns_diff_t sig_diff; 1482 1.1 christos dns_diff_t nsec_diff; 1483 1.1 christos dns_diff_t nsec_mindiff; 1484 1.1 christos dns_diff_t work; 1485 1.1 christos dst_key_t *zone_keys[DNS_MAXZONEKEYS]; 1486 1.1 christos unsigned int nkeys; 1487 1.1 christos isc_stdtime_t inception, expire, soaexpire, keyexpire; 1488 1.1 christos dns_ttl_t nsecttl; 1489 1.1 christos bool check_ksk, keyset_kskonly, build_nsec3; 1490 1.1 christos enum { 1491 1.1 christos sign_updates, 1492 1.1 christos remove_orphaned, 1493 1.1 christos build_chain, 1494 1.1 christos process_nsec, 1495 1.1 christos sign_nsec, 1496 1.1 christos update_nsec3, 1497 1.1 christos process_nsec3, 1498 1.1 christos sign_nsec3 1499 1.1 christos } state; 1500 1.1 christos }; 1501 1.1 christos 1502 1.1 christos static uint32_t 1503 1.1 christos dns__jitter_expire(dns_zone_t *zone, uint32_t sigvalidityinterval) { 1504 1.1 christos /* Spread out signatures over time */ 1505 1.1 christos if (sigvalidityinterval >= 3600U) { 1506 1.1 christos uint32_t expiryinterval = 1507 1.1 christos dns_zone_getsigresigninginterval(zone); 1508 1.1 christos 1509 1.1 christos if (sigvalidityinterval < 7200U) { 1510 1.1 christos expiryinterval = 1200; 1511 1.1 christos } else if (expiryinterval > sigvalidityinterval) { 1512 1.1 christos expiryinterval = sigvalidityinterval; 1513 1.1 christos } else { 1514 1.1 christos expiryinterval = sigvalidityinterval - expiryinterval; 1515 1.1 christos } 1516 1.1 christos uint32_t jitter = isc_random_uniform(expiryinterval); 1517 1.1 christos sigvalidityinterval -= jitter; 1518 1.1 christos } 1519 1.1 christos return (sigvalidityinterval); 1520 1.1 christos } 1521 1.1 christos 1522 1.1 christos isc_result_t 1523 1.1 christos dns_update_signaturesinc(dns_update_log_t *log, dns_zone_t *zone, dns_db_t *db, 1524 1.1 christos dns_dbversion_t *oldver, dns_dbversion_t *newver, 1525 1.1 christos dns_diff_t *diff, uint32_t sigvalidityinterval, 1526 1.1 christos dns_update_state_t **statep) { 1527 1.1 christos isc_result_t result = ISC_R_SUCCESS; 1528 1.1 christos dns_update_state_t mystate, *state; 1529 1.1 christos 1530 1.1 christos dns_difftuple_t *t, *next; 1531 1.1 christos bool flag, build_nsec; 1532 1.1 christos unsigned int i; 1533 1.1 christos isc_stdtime_t now; 1534 1.1 christos dns_rdata_soa_t soa; 1535 1.1 christos dns_rdata_t rdata = DNS_RDATA_INIT; 1536 1.1 christos dns_rdataset_t rdataset; 1537 1.1 christos dns_dbnode_t *node = NULL; 1538 1.1 christos bool unsecure; 1539 1.1 christos bool cut; 1540 1.1 christos dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone); 1541 1.1 christos unsigned int sigs = 0; 1542 1.1 christos unsigned int maxsigs = dns_zone_getsignatures(zone); 1543 1.1 christos 1544 1.1 christos if (statep == NULL || *statep == NULL) { 1545 1.1 christos if (statep == NULL) { 1546 1.1 christos state = &mystate; 1547 1.1 christos } else { 1548 1.1 christos state = isc_mem_get(diff->mctx, sizeof(*state)); 1549 1.1 christos } 1550 1.1 christos 1551 1.1 christos dns_diff_init(diff->mctx, &state->diffnames); 1552 1.1 christos dns_diff_init(diff->mctx, &state->affected); 1553 1.1 christos dns_diff_init(diff->mctx, &state->sig_diff); 1554 1.1 christos dns_diff_init(diff->mctx, &state->nsec_diff); 1555 1.1 christos dns_diff_init(diff->mctx, &state->nsec_mindiff); 1556 1.1 christos dns_diff_init(diff->mctx, &state->work); 1557 1.1 christos state->nkeys = 0; 1558 1.1 christos state->build_nsec3 = false; 1559 1.1 christos 1560 1.1 christos result = find_zone_keys(zone, db, newver, diff->mctx, 1561 1.1 christos DNS_MAXZONEKEYS, state->zone_keys, 1562 1.1 christos &state->nkeys); 1563 1.1 christos if (result != ISC_R_SUCCESS) { 1564 1.1 christos update_log(log, zone, ISC_LOG_ERROR, 1565 1.1 christos "could not get zone keys for secure " 1566 1.1 christos "dynamic update"); 1567 1.1 christos goto failure; 1568 1.1 christos } 1569 1.1 christos 1570 1.1 christos isc_stdtime_get(&now); 1571 1.1 christos state->inception = now - 3600; /* Allow for some clock skew. */ 1572 1.1 christos state->expire = now + 1573 1.1 christos dns__jitter_expire(zone, sigvalidityinterval); 1574 1.1 christos state->soaexpire = now + sigvalidityinterval; 1575 1.1 christos state->keyexpire = dns_zone_getkeyvalidityinterval(zone); 1576 1.1 christos if (state->keyexpire == 0) { 1577 1.1 christos state->keyexpire = state->expire; 1578 1.1 christos } else { 1579 1.1 christos state->keyexpire += now; 1580 1.1 christos } 1581 1.1 christos 1582 1.1 christos /* 1583 1.1 christos * Do we look at the KSK flag on the DNSKEY to determining which 1584 1.1 christos * keys sign which RRsets? First check the zone option then 1585 1.1 christos * check the keys flags to make sure at least one has a ksk set 1586 1.1 christos * and one doesn't. 1587 1.1 christos */ 1588 1.1 christos state->check_ksk = ((dns_zone_getoptions(zone) & 1589 1.1 christos DNS_ZONEOPT_UPDATECHECKKSK) != 0); 1590 1.1 christos state->keyset_kskonly = ((dns_zone_getoptions(zone) & 1591 1.1 christos DNS_ZONEOPT_DNSKEYKSKONLY) != 0); 1592 1.1 christos 1593 1.1 christos /* 1594 1.1 christos * Calculate the NSEC/NSEC3 TTL as a minimum of the SOA TTL and 1595 1.1 christos * MINIMUM field. 1596 1.1 christos */ 1597 1.1 christos CHECK(dns_db_findnode(db, dns_db_origin(db), false, &node)); 1598 1.1 christos dns_rdataset_init(&rdataset); 1599 1.1 christos CHECK(dns_db_findrdataset(db, node, newver, dns_rdatatype_soa, 1600 1.1 christos 0, (isc_stdtime_t)0, &rdataset, 1601 1.1 christos NULL)); 1602 1.1 christos CHECK(dns_rdataset_first(&rdataset)); 1603 1.1 christos dns_rdataset_current(&rdataset, &rdata); 1604 1.1 christos CHECK(dns_rdata_tostruct(&rdata, &soa, NULL)); 1605 1.1 christos state->nsecttl = ISC_MIN(rdataset.ttl, soa.minimum); 1606 1.1 christos dns_rdataset_disassociate(&rdataset); 1607 1.1 christos dns_db_detachnode(db, &node); 1608 1.1 christos 1609 1.1 christos /* 1610 1.1 christos * Find all RRsets directly affected by the update, and 1611 1.1 christos * update their RRSIGs. Also build a list of names affected 1612 1.1 christos * by the update in "diffnames". 1613 1.1 christos */ 1614 1.1 christos CHECK(dns_diff_sort(diff, temp_order)); 1615 1.1 christos state->state = sign_updates; 1616 1.1 christos state->magic = STATE_MAGIC; 1617 1.1 christos if (statep != NULL) { 1618 1.1 christos *statep = state; 1619 1.1 christos } 1620 1.1 christos } else { 1621 1.1 christos REQUIRE(DNS_STATE_VALID(*statep)); 1622 1.1 christos state = *statep; 1623 1.1 christos } 1624 1.1 christos 1625 1.1 christos next_state: 1626 1.1 christos switch (state->state) { 1627 1.1 christos case sign_updates: 1628 1.1 christos t = ISC_LIST_HEAD(diff->tuples); 1629 1.1 christos while (t != NULL) { 1630 1.1 christos dns_name_t *name = &t->name; 1631 1.1 christos /* 1632 1.1 christos * Now "name" is a new, unique name affected by the 1633 1.1 christos * update. 1634 1.1 christos */ 1635 1.1 christos 1636 1.1 christos CHECK(namelist_append_name(&state->diffnames, name)); 1637 1.1 christos 1638 1.1 christos while (t != NULL && dns_name_equal(&t->name, name)) { 1639 1.1 christos dns_rdatatype_t type; 1640 1.1 christos type = t->rdata.type; 1641 1.1 christos 1642 1.1 christos /* 1643 1.1 christos * Now "name" and "type" denote a new unique 1644 1.1 christos * RRset affected by the update. 1645 1.1 christos */ 1646 1.1 christos 1647 1.1 christos /* Don't sign RRSIGs. */ 1648 1.1 christos if (type == dns_rdatatype_rrsig) { 1649 1.1 christos goto skip; 1650 1.1 christos } 1651 1.1 christos 1652 1.1 christos /* 1653 1.1 christos * Delete all old RRSIGs covering this type, 1654 1.1 christos * since they are all invalid when the signed 1655 1.1 christos * RRset has changed. We may not be able to 1656 1.1 christos * recreate all of them - tough. 1657 1.1 christos * Special case changes to the zone's DNSKEY 1658 1.1 christos * records to support offline KSKs. 1659 1.1 christos */ 1660 1.1 christos if (type == dns_rdatatype_dnskey) { 1661 1.1 christos del_keysigs(db, newver, name, 1662 1.1 christos &state->sig_diff, 1663 1.1 christos state->zone_keys, 1664 1.1 christos state->nkeys); 1665 1.1 christos } else { 1666 1.1 christos CHECK(delete_if( 1667 1.1 christos true_p, db, newver, name, 1668 1.1 christos dns_rdatatype_rrsig, type, NULL, 1669 1.1 christos &state->sig_diff)); 1670 1.1 christos } 1671 1.1 christos 1672 1.1 christos /* 1673 1.1 christos * If this RRset is still visible after the 1674 1.1 christos * update, add a new signature for it. 1675 1.1 christos */ 1676 1.1 christos CHECK(rrset_visible(db, newver, name, type, 1677 1.1 christos &flag)); 1678 1.1 christos if (flag) { 1679 1.1 christos isc_stdtime_t exp; 1680 1.1 christos if (type == dns_rdatatype_dnskey || 1681 1.1 christos type == dns_rdatatype_cdnskey || 1682 1.1 christos type == dns_rdatatype_cds) 1683 1.1 christos { 1684 1.1 christos exp = state->keyexpire; 1685 1.1 christos } else if (type == dns_rdatatype_soa) { 1686 1.1 christos exp = state->soaexpire; 1687 1.1 christos } else { 1688 1.1 christos exp = state->expire; 1689 1.1 christos } 1690 1.1 christos 1691 1.1 christos CHECK(add_sigs( 1692 1.1 christos log, zone, db, newver, name, 1693 1.1 christos type, &state->sig_diff, 1694 1.1 christos state->zone_keys, state->nkeys, 1695 1.1 christos state->inception, exp, 1696 1.1 christos state->check_ksk, 1697 1.1 christos state->keyset_kskonly)); 1698 1.1 christos sigs++; 1699 1.1 christos } 1700 1.1 christos skip: 1701 1.1 christos /* Skip any other updates to the same RRset. */ 1702 1.1 christos while (t != NULL && 1703 1.1 christos dns_name_equal(&t->name, name) && 1704 1.1 christos t->rdata.type == type) 1705 1.1 christos { 1706 1.1 christos next = ISC_LIST_NEXT(t, link); 1707 1.1 christos ISC_LIST_UNLINK(diff->tuples, t, link); 1708 1.1 christos ISC_LIST_APPEND(state->work.tuples, t, 1709 1.1 christos link); 1710 1.1 christos t = next; 1711 1.1 christos } 1712 1.1 christos } 1713 1.1 christos if (state != &mystate && sigs > maxsigs) { 1714 1.1 christos return (DNS_R_CONTINUE); 1715 1.1 christos } 1716 1.1 christos } 1717 1.1 christos ISC_LIST_APPENDLIST(diff->tuples, state->work.tuples, link); 1718 1.1 christos 1719 1.1 christos update_log(log, zone, ISC_LOG_DEBUG(3), 1720 1.1 christos "updated data signatures"); 1721 1.1 christos FALLTHROUGH; 1722 1.1 christos case remove_orphaned: 1723 1.1 christos state->state = remove_orphaned; 1724 1.1 christos 1725 1.1 christos /* Remove orphaned NSECs and RRSIG NSECs. */ 1726 1.1 christos for (t = ISC_LIST_HEAD(state->diffnames.tuples); t != NULL; 1727 1.1 christos t = ISC_LIST_NEXT(t, link)) 1728 1.1 christos { 1729 1.1 christos CHECK(non_nsec_rrset_exists(db, newver, &t->name, 1730 1.1 christos &flag)); 1731 1.1 christos if (!flag) { 1732 1.1 christos CHECK(delete_if(true_p, db, newver, &t->name, 1733 1.1 christos dns_rdatatype_any, 0, NULL, 1734 1.1 christos &state->sig_diff)); 1735 1.1 christos } 1736 1.1 christos } 1737 1.1 christos update_log(log, zone, ISC_LOG_DEBUG(3), 1738 1.1 christos "removed any orphaned NSEC records"); 1739 1.1 christos 1740 1.1 christos /* 1741 1.1 christos * See if we need to build NSEC or NSEC3 chains. 1742 1.1 christos */ 1743 1.1 christos CHECK(dns_private_chains(db, newver, privatetype, &build_nsec, 1744 1.1 christos &state->build_nsec3)); 1745 1.1 christos if (!build_nsec) { 1746 1.1 christos state->state = update_nsec3; 1747 1.1 christos goto next_state; 1748 1.1 christos } 1749 1.1 christos 1750 1.1 christos update_log(log, zone, ISC_LOG_DEBUG(3), 1751 1.1 christos "rebuilding NSEC chain"); 1752 1.1 christos 1753 1.1 christos FALLTHROUGH; 1754 1.1 christos case build_chain: 1755 1.1 christos state->state = build_chain; 1756 1.1 christos /* 1757 1.1 christos * When a name is created or deleted, its predecessor needs to 1758 1.1 christos * have its NSEC updated. 1759 1.1 christos */ 1760 1.1 christos for (t = ISC_LIST_HEAD(state->diffnames.tuples); t != NULL; 1761 1.1 christos t = ISC_LIST_NEXT(t, link)) 1762 1.1 christos { 1763 1.1 christos bool existed, exists; 1764 1.1 christos dns_fixedname_t fixedname; 1765 1.1 christos dns_name_t *prevname; 1766 1.1 christos 1767 1.1 christos prevname = dns_fixedname_initname(&fixedname); 1768 1.1 christos 1769 1.1 christos if (oldver != NULL) { 1770 1.1 christos CHECK(name_exists(db, oldver, &t->name, 1771 1.1 christos &existed)); 1772 1.1 christos } else { 1773 1.1 christos existed = false; 1774 1.1 christos } 1775 1.1 christos CHECK(name_exists(db, newver, &t->name, &exists)); 1776 1.1 christos if (exists == existed) { 1777 1.1 christos continue; 1778 1.1 christos } 1779 1.1 christos 1780 1.1 christos /* 1781 1.1 christos * Find the predecessor. 1782 1.1 christos * When names become obscured or unobscured in this 1783 1.1 christos * update transaction, we may find the wrong 1784 1.1 christos * predecessor because the NSECs have not yet been 1785 1.1 christos * updated to reflect the delegation change. This 1786 1.1 christos * should not matter because in this case, the correct 1787 1.1 christos * predecessor is either the delegation node or a 1788 1.1 christos * newly unobscured node, and those nodes are on the 1789 1.1 christos * "affected" list in any case. 1790 1.1 christos */ 1791 1.1 christos CHECK(next_active(log, zone, db, newver, &t->name, 1792 1.1 christos prevname, false)); 1793 1.1 christos CHECK(namelist_append_name(&state->affected, prevname)); 1794 1.1 christos } 1795 1.1 christos 1796 1.1 christos /* 1797 1.1 christos * Find names potentially affected by delegation changes 1798 1.1 christos * (obscured by adding an NS or DNAME, or unobscured by 1799 1.1 christos * removing one). 1800 1.1 christos */ 1801 1.1 christos for (t = ISC_LIST_HEAD(state->diffnames.tuples); t != NULL; 1802 1.1 christos t = ISC_LIST_NEXT(t, link)) 1803 1.1 christos { 1804 1.1 christos bool ns_existed, dname_existed; 1805 1.1 christos bool ns_exists, dname_exists; 1806 1.1 christos 1807 1.1 christos if (oldver != NULL) { 1808 1.1 christos CHECK(rrset_exists(db, oldver, &t->name, 1809 1.1 christos dns_rdatatype_ns, 0, 1810 1.1 christos &ns_existed)); 1811 1.1 christos } else { 1812 1.1 christos ns_existed = false; 1813 1.1 christos } 1814 1.1 christos if (oldver != NULL) { 1815 1.1 christos CHECK(rrset_exists(db, oldver, &t->name, 1816 1.1 christos dns_rdatatype_dname, 0, 1817 1.1 christos &dname_existed)); 1818 1.1 christos } else { 1819 1.1 christos dname_existed = false; 1820 1.1 christos } 1821 1.1 christos CHECK(rrset_exists(db, newver, &t->name, 1822 1.1 christos dns_rdatatype_ns, 0, &ns_exists)); 1823 1.1 christos CHECK(rrset_exists(db, newver, &t->name, 1824 1.1 christos dns_rdatatype_dname, 0, 1825 1.1 christos &dname_exists)); 1826 1.1 christos if ((ns_exists || dname_exists) == 1827 1.1 christos (ns_existed || dname_existed)) 1828 1.1 christos { 1829 1.1 christos continue; 1830 1.1 christos } 1831 1.1 christos /* 1832 1.1 christos * There was a delegation change. Mark all subdomains 1833 1.1 christos * of t->name as potentially needing a NSEC update. 1834 1.1 christos */ 1835 1.1 christos CHECK(namelist_append_subdomain(db, &t->name, 1836 1.1 christos &state->affected)); 1837 1.1 christos } 1838 1.1 christos ISC_LIST_APPENDLIST(state->affected.tuples, 1839 1.1 christos state->diffnames.tuples, link); 1840 1.1 christos INSIST(ISC_LIST_EMPTY(state->diffnames.tuples)); 1841 1.1 christos 1842 1.1 christos CHECK(uniqify_name_list(&state->affected)); 1843 1.1 christos 1844 1.1 christos FALLTHROUGH; 1845 1.1 christos case process_nsec: 1846 1.1 christos state->state = process_nsec; 1847 1.1 christos 1848 1.1 christos /* 1849 1.1 christos * Determine which names should have NSECs, and delete/create 1850 1.1 christos * NSECs to make it so. We don't know the final NSEC targets 1851 1.1 christos * yet, so we just create placeholder NSECs with arbitrary 1852 1.1 christos * contents to indicate that their respective owner names 1853 1.1 christos * should be part of the NSEC chain. 1854 1.1 christos */ 1855 1.1 christos while ((t = ISC_LIST_HEAD(state->affected.tuples)) != NULL) { 1856 1.1 christos bool exists; 1857 1.1 christos dns_name_t *name = &t->name; 1858 1.1 christos 1859 1.1 christos CHECK(name_exists(db, newver, name, &exists)); 1860 1.1 christos if (!exists) { 1861 1.1 christos goto unlink; 1862 1.1 christos } 1863 1.1 christos CHECK(is_active(db, newver, name, &flag, &cut, NULL)); 1864 1.1 christos if (!flag) { 1865 1.1 christos /* 1866 1.1 christos * This name is obscured. Delete any 1867 1.1 christos * existing NSEC record. 1868 1.1 christos */ 1869 1.1 christos CHECK(delete_if(true_p, db, newver, name, 1870 1.1 christos dns_rdatatype_nsec, 0, NULL, 1871 1.1 christos &state->nsec_diff)); 1872 1.1 christos CHECK(delete_if(rrsig_p, db, newver, name, 1873 1.1 christos dns_rdatatype_any, 0, NULL, 1874 1.1 christos diff)); 1875 1.1 christos } else { 1876 1.1 christos /* 1877 1.1 christos * This name is not obscured. It needs to have 1878 1.1 christos * a NSEC unless it is the at the origin, in 1879 1.1 christos * which case it should already exist if there 1880 1.1 christos * is a complete NSEC chain and if there isn't 1881 1.1 christos * a complete NSEC chain we don't want to add 1882 1.1 christos * one as that would signal that there is a 1883 1.1 christos * complete NSEC chain. 1884 1.1 christos */ 1885 1.1 christos if (!dns_name_equal(name, dns_db_origin(db))) { 1886 1.1 christos CHECK(rrset_exists(db, newver, name, 1887 1.1 christos dns_rdatatype_nsec, 1888 1.1 christos 0, &flag)); 1889 1.1 christos if (!flag) { 1890 1.1 christos CHECK(add_placeholder_nsec( 1891 1.1 christos db, newver, name, 1892 1.1 christos diff)); 1893 1.1 christos } 1894 1.1 christos } 1895 1.1 christos CHECK(add_exposed_sigs( 1896 1.1 christos log, zone, db, newver, name, cut, 1897 1.1 christos &state->sig_diff, state->zone_keys, 1898 1.1 christos state->nkeys, state->inception, 1899 1.1 christos state->expire, state->check_ksk, 1900 1.1 christos state->keyset_kskonly, &sigs)); 1901 1.1 christos } 1902 1.1 christos unlink: 1903 1.1 christos ISC_LIST_UNLINK(state->affected.tuples, t, link); 1904 1.1 christos ISC_LIST_APPEND(state->work.tuples, t, link); 1905 1.1 christos if (state != &mystate && sigs > maxsigs) { 1906 1.1 christos return (DNS_R_CONTINUE); 1907 1.1 christos } 1908 1.1 christos } 1909 1.1 christos ISC_LIST_APPENDLIST(state->affected.tuples, state->work.tuples, 1910 1.1 christos link); 1911 1.1 christos 1912 1.1 christos /* 1913 1.1 christos * Now we know which names are part of the NSEC chain. 1914 1.1 christos * Make them all point at their correct targets. 1915 1.1 christos */ 1916 1.1 christos for (t = ISC_LIST_HEAD(state->affected.tuples); t != NULL; 1917 1.1 christos t = ISC_LIST_NEXT(t, link)) 1918 1.1 christos { 1919 1.1 christos CHECK(rrset_exists(db, newver, &t->name, 1920 1.1 christos dns_rdatatype_nsec, 0, &flag)); 1921 1.1 christos if (flag) { 1922 1.1 christos /* 1923 1.1 christos * There is a NSEC, but we don't know if it 1924 1.1 christos * is correct. Delete it and create a correct 1925 1.1 christos * one to be sure. If the update was 1926 1.1 christos * unnecessary, the diff minimization 1927 1.1 christos * will take care of eliminating it from the 1928 1.1 christos * journal, IXFRs, etc. 1929 1.1 christos * 1930 1.1 christos * The RRSIG bit should always be set in the 1931 1.1 christos * NSECs we generate, because they will all 1932 1.1 christos * get RRSIG NSECs. 1933 1.1 christos * (XXX what if the zone keys are missing?). 1934 1.1 christos * Because the RRSIG NSECs have not necessarily 1935 1.1 christos * been created yet, the correctness of the 1936 1.1 christos * bit mask relies on the assumption that NSECs 1937 1.1 christos * are only created if there is other data, and 1938 1.1 christos * if there is other data, there are other 1939 1.1 christos * RRSIGs. 1940 1.1 christos */ 1941 1.1 christos CHECK(add_nsec(log, zone, db, newver, &t->name, 1942 1.1 christos state->nsecttl, 1943 1.1 christos &state->nsec_diff)); 1944 1.1 christos } 1945 1.1 christos } 1946 1.1 christos 1947 1.1 christos /* 1948 1.1 christos * Minimize the set of NSEC updates so that we don't 1949 1.1 christos * have to regenerate the RRSIG NSECs for NSECs that were 1950 1.1 christos * replaced with identical ones. 1951 1.1 christos */ 1952 1.1 christos while ((t = ISC_LIST_HEAD(state->nsec_diff.tuples)) != NULL) { 1953 1.1 christos ISC_LIST_UNLINK(state->nsec_diff.tuples, t, link); 1954 1.1 christos dns_diff_appendminimal(&state->nsec_mindiff, &t); 1955 1.1 christos } 1956 1.1 christos 1957 1.1 christos update_log(log, zone, ISC_LOG_DEBUG(3), 1958 1.1 christos "signing rebuilt NSEC chain"); 1959 1.1 christos 1960 1.1 christos FALLTHROUGH; 1961 1.1 christos case sign_nsec: 1962 1.1 christos state->state = sign_nsec; 1963 1.1 christos /* Update RRSIG NSECs. */ 1964 1.1 christos while ((t = ISC_LIST_HEAD(state->nsec_mindiff.tuples)) != NULL) 1965 1.1 christos { 1966 1.1 christos if (t->op == DNS_DIFFOP_DEL) { 1967 1.1 christos CHECK(delete_if(true_p, db, newver, &t->name, 1968 1.1 christos dns_rdatatype_rrsig, 1969 1.1 christos dns_rdatatype_nsec, NULL, 1970 1.1 christos &state->sig_diff)); 1971 1.1 christos } else if (t->op == DNS_DIFFOP_ADD) { 1972 1.1 christos CHECK(add_sigs(log, zone, db, newver, &t->name, 1973 1.1 christos dns_rdatatype_nsec, 1974 1.1 christos &state->sig_diff, 1975 1.1 christos state->zone_keys, state->nkeys, 1976 1.1 christos state->inception, state->expire, 1977 1.1 christos state->check_ksk, 1978 1.1 christos state->keyset_kskonly)); 1979 1.1 christos sigs++; 1980 1.1 christos } else { 1981 1.1 christos UNREACHABLE(); 1982 1.1 christos } 1983 1.1 christos ISC_LIST_UNLINK(state->nsec_mindiff.tuples, t, link); 1984 1.1 christos ISC_LIST_APPEND(state->work.tuples, t, link); 1985 1.1 christos if (state != &mystate && sigs > maxsigs) { 1986 1.1 christos return (DNS_R_CONTINUE); 1987 1.1 christos } 1988 1.1 christos } 1989 1.1 christos ISC_LIST_APPENDLIST(state->nsec_mindiff.tuples, 1990 1.1 christos state->work.tuples, link); 1991 1.1 christos FALLTHROUGH; 1992 1.1 christos case update_nsec3: 1993 1.1 christos state->state = update_nsec3; 1994 1.1 christos 1995 1.1 christos /* Record our changes for the journal. */ 1996 1.1 christos while ((t = ISC_LIST_HEAD(state->sig_diff.tuples)) != NULL) { 1997 1.1 christos ISC_LIST_UNLINK(state->sig_diff.tuples, t, link); 1998 1.1 christos dns_diff_appendminimal(diff, &t); 1999 1.1 christos } 2000 1.1 christos while ((t = ISC_LIST_HEAD(state->nsec_mindiff.tuples)) != NULL) 2001 1.1 christos { 2002 1.1 christos ISC_LIST_UNLINK(state->nsec_mindiff.tuples, t, link); 2003 1.1 christos dns_diff_appendminimal(diff, &t); 2004 1.1 christos } 2005 1.1 christos 2006 1.1 christos INSIST(ISC_LIST_EMPTY(state->sig_diff.tuples)); 2007 1.1 christos INSIST(ISC_LIST_EMPTY(state->nsec_diff.tuples)); 2008 1.1 christos INSIST(ISC_LIST_EMPTY(state->nsec_mindiff.tuples)); 2009 1.1 christos 2010 1.1 christos if (!state->build_nsec3) { 2011 1.1 christos update_log(log, zone, ISC_LOG_DEBUG(3), 2012 1.1 christos "no NSEC3 chains to rebuild"); 2013 1.1 christos goto failure; 2014 1.1 christos } 2015 1.1 christos 2016 1.1 christos update_log(log, zone, ISC_LOG_DEBUG(3), 2017 1.1 christos "rebuilding NSEC3 chains"); 2018 1.1 christos 2019 1.1 christos dns_diff_clear(&state->diffnames); 2020 1.1 christos dns_diff_clear(&state->affected); 2021 1.1 christos 2022 1.1 christos CHECK(dns_diff_sort(diff, temp_order)); 2023 1.1 christos 2024 1.1 christos /* 2025 1.1 christos * Find names potentially affected by delegation changes 2026 1.1 christos * (obscured by adding an NS or DNAME, or unobscured by 2027 1.1 christos * removing one). 2028 1.1 christos */ 2029 1.1 christos t = ISC_LIST_HEAD(diff->tuples); 2030 1.1 christos while (t != NULL) { 2031 1.1 christos dns_name_t *name = &t->name; 2032 1.1 christos 2033 1.1 christos bool ns_existed, dname_existed; 2034 1.1 christos bool ns_exists, dname_exists; 2035 1.1 christos bool exists, existed; 2036 1.1 christos 2037 1.1 christos if (t->rdata.type == dns_rdatatype_nsec || 2038 1.1 christos t->rdata.type == dns_rdatatype_rrsig) 2039 1.1 christos { 2040 1.1 christos t = ISC_LIST_NEXT(t, link); 2041 1.1 christos continue; 2042 1.1 christos } 2043 1.1 christos 2044 1.1 christos CHECK(namelist_append_name(&state->affected, name)); 2045 1.1 christos 2046 1.1 christos if (oldver != NULL) { 2047 1.1 christos CHECK(rrset_exists(db, oldver, name, 2048 1.1 christos dns_rdatatype_ns, 0, 2049 1.1 christos &ns_existed)); 2050 1.1 christos } else { 2051 1.1 christos ns_existed = false; 2052 1.1 christos } 2053 1.1 christos if (oldver != NULL) { 2054 1.1 christos CHECK(rrset_exists(db, oldver, name, 2055 1.1 christos dns_rdatatype_dname, 0, 2056 1.1 christos &dname_existed)); 2057 1.1 christos } else { 2058 1.1 christos dname_existed = false; 2059 1.1 christos } 2060 1.1 christos CHECK(rrset_exists(db, newver, name, dns_rdatatype_ns, 2061 1.1 christos 0, &ns_exists)); 2062 1.1 christos CHECK(rrset_exists(db, newver, name, 2063 1.1 christos dns_rdatatype_dname, 0, 2064 1.1 christos &dname_exists)); 2065 1.1 christos 2066 1.1 christos exists = ns_exists || dname_exists; 2067 1.1 christos existed = ns_existed || dname_existed; 2068 1.1 christos if (exists == existed) { 2069 1.1 christos goto nextname; 2070 1.1 christos } 2071 1.1 christos /* 2072 1.1 christos * There was a delegation change. Mark all subdomains 2073 1.1 christos * of t->name as potentially needing a NSEC3 update. 2074 1.1 christos */ 2075 1.1 christos CHECK(namelist_append_subdomain(db, name, 2076 1.1 christos &state->affected)); 2077 1.1 christos 2078 1.1 christos nextname: 2079 1.1 christos while (t != NULL && dns_name_equal(&t->name, name)) { 2080 1.1 christos t = ISC_LIST_NEXT(t, link); 2081 1.1 christos } 2082 1.1 christos } 2083 1.1 christos 2084 1.1 christos FALLTHROUGH; 2085 1.1 christos case process_nsec3: 2086 1.1 christos state->state = process_nsec3; 2087 1.1 christos while ((t = ISC_LIST_HEAD(state->affected.tuples)) != NULL) { 2088 1.1 christos dns_name_t *name = &t->name; 2089 1.1 christos 2090 1.1 christos unsecure = false; /* Silence compiler warning. */ 2091 1.1 christos CHECK(is_active(db, newver, name, &flag, &cut, 2092 1.1 christos &unsecure)); 2093 1.1 christos 2094 1.1 christos if (!flag) { 2095 1.1 christos CHECK(delete_if(rrsig_p, db, newver, name, 2096 1.1 christos dns_rdatatype_any, 0, NULL, 2097 1.1 christos diff)); 2098 1.1 christos CHECK(dns_nsec3_delnsec3sx(db, newver, name, 2099 1.1 christos privatetype, 2100 1.1 christos &state->nsec_diff)); 2101 1.1 christos } else { 2102 1.1 christos CHECK(add_exposed_sigs( 2103 1.1 christos log, zone, db, newver, name, cut, 2104 1.1 christos &state->sig_diff, state->zone_keys, 2105 1.1 christos state->nkeys, state->inception, 2106 1.1 christos state->expire, state->check_ksk, 2107 1.1 christos state->keyset_kskonly, &sigs)); 2108 1.1 christos CHECK(dns_nsec3_addnsec3sx( 2109 1.1 christos db, newver, name, state->nsecttl, 2110 1.1 christos unsecure, privatetype, 2111 1.1 christos &state->nsec_diff)); 2112 1.1 christos } 2113 1.1 christos ISC_LIST_UNLINK(state->affected.tuples, t, link); 2114 1.1 christos ISC_LIST_APPEND(state->work.tuples, t, link); 2115 1.1 christos if (state != &mystate && sigs > maxsigs) { 2116 1.1 christos return (DNS_R_CONTINUE); 2117 1.1 christos } 2118 1.1 christos } 2119 1.1 christos ISC_LIST_APPENDLIST(state->affected.tuples, state->work.tuples, 2120 1.1 christos link); 2121 1.1 christos 2122 1.1 christos /* 2123 1.1 christos * Minimize the set of NSEC3 updates so that we don't 2124 1.1 christos * have to regenerate the RRSIG NSEC3s for NSEC3s that were 2125 1.1 christos * replaced with identical ones. 2126 1.1 christos */ 2127 1.1 christos while ((t = ISC_LIST_HEAD(state->nsec_diff.tuples)) != NULL) { 2128 1.1 christos ISC_LIST_UNLINK(state->nsec_diff.tuples, t, link); 2129 1.1 christos dns_diff_appendminimal(&state->nsec_mindiff, &t); 2130 1.1 christos } 2131 1.1 christos 2132 1.1 christos update_log(log, zone, ISC_LOG_DEBUG(3), 2133 1.1 christos "signing rebuilt NSEC3 chain"); 2134 1.1 christos 2135 1.1 christos FALLTHROUGH; 2136 1.1 christos case sign_nsec3: 2137 1.1 christos state->state = sign_nsec3; 2138 1.1 christos /* Update RRSIG NSEC3s. */ 2139 1.1 christos while ((t = ISC_LIST_HEAD(state->nsec_mindiff.tuples)) != NULL) 2140 1.1 christos { 2141 1.1 christos if (t->op == DNS_DIFFOP_DEL) { 2142 1.1 christos CHECK(delete_if(true_p, db, newver, &t->name, 2143 1.1 christos dns_rdatatype_rrsig, 2144 1.1 christos dns_rdatatype_nsec3, NULL, 2145 1.1 christos &state->sig_diff)); 2146 1.1 christos } else if (t->op == DNS_DIFFOP_ADD) { 2147 1.1 christos CHECK(add_sigs(log, zone, db, newver, &t->name, 2148 1.1 christos dns_rdatatype_nsec3, 2149 1.1 christos &state->sig_diff, 2150 1.1 christos state->zone_keys, state->nkeys, 2151 1.1 christos state->inception, state->expire, 2152 1.1 christos state->check_ksk, 2153 1.1 christos state->keyset_kskonly)); 2154 1.1 christos sigs++; 2155 1.1 christos } else { 2156 1.1 christos UNREACHABLE(); 2157 1.1 christos } 2158 1.1 christos ISC_LIST_UNLINK(state->nsec_mindiff.tuples, t, link); 2159 1.1 christos ISC_LIST_APPEND(state->work.tuples, t, link); 2160 1.1 christos if (state != &mystate && sigs > maxsigs) { 2161 1.1 christos return (DNS_R_CONTINUE); 2162 1.1 christos } 2163 1.1 christos } 2164 1.1 christos ISC_LIST_APPENDLIST(state->nsec_mindiff.tuples, 2165 1.1 christos state->work.tuples, link); 2166 1.1 christos 2167 1.1 christos /* Record our changes for the journal. */ 2168 1.1 christos while ((t = ISC_LIST_HEAD(state->sig_diff.tuples)) != NULL) { 2169 1.1 christos ISC_LIST_UNLINK(state->sig_diff.tuples, t, link); 2170 1.1 christos dns_diff_appendminimal(diff, &t); 2171 1.1 christos } 2172 1.1 christos while ((t = ISC_LIST_HEAD(state->nsec_mindiff.tuples)) != NULL) 2173 1.1 christos { 2174 1.1 christos ISC_LIST_UNLINK(state->nsec_mindiff.tuples, t, link); 2175 1.1 christos dns_diff_appendminimal(diff, &t); 2176 1.1 christos } 2177 1.1 christos 2178 1.1 christos INSIST(ISC_LIST_EMPTY(state->sig_diff.tuples)); 2179 1.1 christos INSIST(ISC_LIST_EMPTY(state->nsec_diff.tuples)); 2180 1.1 christos INSIST(ISC_LIST_EMPTY(state->nsec_mindiff.tuples)); 2181 1.1 christos break; 2182 1.1 christos default: 2183 1.1 christos UNREACHABLE(); 2184 1.1 christos } 2185 1.1 christos 2186 1.1 christos failure: 2187 1.1 christos if (node != NULL) { 2188 1.1 christos dns_db_detachnode(db, &node); 2189 1.1 christos } 2190 1.1 christos 2191 1.1 christos dns_diff_clear(&state->sig_diff); 2192 1.1 christos dns_diff_clear(&state->nsec_diff); 2193 1.1 christos dns_diff_clear(&state->nsec_mindiff); 2194 1.1 christos 2195 1.1 christos dns_diff_clear(&state->affected); 2196 1.1 christos dns_diff_clear(&state->diffnames); 2197 1.1 christos dns_diff_clear(&state->work); 2198 1.1 christos 2199 1.1 christos for (i = 0; i < state->nkeys; i++) { 2200 1.1 christos dst_key_free(&state->zone_keys[i]); 2201 1.1 christos } 2202 1.1 christos 2203 1.1 christos if (state != &mystate) { 2204 1.1 christos *statep = NULL; 2205 1.1 christos state->magic = 0; 2206 1.1 christos isc_mem_put(diff->mctx, state, sizeof(*state)); 2207 1.1 christos } 2208 1.1 christos 2209 1.1 christos return (result); 2210 1.1 christos } 2211 1.1 christos 2212 1.1 christos static isc_stdtime_t 2213 1.1 christos epoch_to_yyyymmdd(time_t when) { 2214 1.1 christos struct tm t, *tm = localtime_r(&when, &t); 2215 1.1 christos if (tm == NULL) { 2216 1.1 christos return (0); 2217 1.1 christos } 2218 1.1 christos return (((tm->tm_year + 1900) * 10000) + ((tm->tm_mon + 1) * 100) + 2219 1.1 christos tm->tm_mday); 2220 1.1 christos } 2221 1.1 christos 2222 1.1 christos static uint32_t 2223 1.1 christos dns__update_soaserial(uint32_t serial, dns_updatemethod_t method) { 2224 1.1 christos isc_stdtime_t now; 2225 1.1 christos 2226 1.1 christos switch (method) { 2227 1.1 christos case dns_updatemethod_none: 2228 1.1 christos return (serial); 2229 1.1 christos case dns_updatemethod_unixtime: 2230 1.1 christos isc_stdtime_get(&now); 2231 1.1 christos return (now); 2232 1.1 christos case dns_updatemethod_date: 2233 1.1 christos isc_stdtime_get(&now); 2234 1.1 christos return (epoch_to_yyyymmdd((time_t)now) * 100); 2235 1.1 christos case dns_updatemethod_increment: 2236 1.1 christos /* RFC1982 */ 2237 1.1 christos serial = (serial + 1) & 0xFFFFFFFF; 2238 1.1 christos if (serial == 0) { 2239 1.1 christos return (1); 2240 1.1 christos } 2241 1.1 christos return (serial); 2242 1.1 christos default: 2243 1.1 christos UNREACHABLE(); 2244 1.1 christos } 2245 1.1 christos } 2246 1.1 christos 2247 1.1 christos uint32_t 2248 1.1 christos dns_update_soaserial(uint32_t serial, dns_updatemethod_t method, 2249 1.1 christos dns_updatemethod_t *used) { 2250 1.1 christos uint32_t new_serial = dns__update_soaserial(serial, method); 2251 1.1 christos switch (method) { 2252 1.1 christos case dns_updatemethod_none: 2253 1.1 christos case dns_updatemethod_increment: 2254 1.1 christos break; 2255 1.1 christos case dns_updatemethod_unixtime: 2256 1.1 christos case dns_updatemethod_date: 2257 1.1 christos if (!(new_serial != 0 && isc_serial_gt(new_serial, serial))) { 2258 1.1 christos /* 2259 1.1 christos * If the new date serial following YYYYMMDD00 is equal 2260 1.1 christos * to or smaller than the current serial, but YYYYMMDD99 2261 1.1 christos * would be larger, pretend we have used the 2262 1.1 christos * "dns_updatemethod_date" method. 2263 1.1 christos */ 2264 1.1 christos if (method == dns_updatemethod_unixtime || 2265 1.1 christos !isc_serial_gt(new_serial + 99, serial)) 2266 1.1 christos { 2267 1.1 christos method = dns_updatemethod_increment; 2268 1.1 christos } 2269 1.1 christos new_serial = dns__update_soaserial( 2270 1.1 christos serial, dns_updatemethod_increment); 2271 1.1 christos } 2272 1.1 christos break; 2273 1.1 christos default: 2274 1.1 christos UNREACHABLE(); 2275 1.1 christos } 2276 1.1 christos 2277 1.1 christos if (used != NULL) { 2278 1.1 christos *used = method; 2279 1.1 christos } 2280 1.1 christos 2281 1.1 christos return (new_serial); 2282 1.1 christos } 2283