Coverage Report

Created: 2026-06-15 06:55

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