Update repub branch u/fanf2/patch to rebasing branch u/fanf2/rebasing revision v9_13_...
[ipreg/bind9.git] / bin / dnssec / dnssec-signzone.c
CommitLineData
f690123c 1/*
0fabe0da 2 * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
ec5347e2 3 *
0c27b3fe
MA
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
ec5347e2 7 *
843d3896
OS
8 * See the COPYRIGHT file distributed with this work for additional
9 * information regarding copyright ownership.
10 *
11 * Portions Copyright (C) Network Associates, Inc.
40f53fa8 12 *
271c4c7f 13 * Permission to use, copy, modify, and/or distribute this software for any
f690123c
MG
14 * purpose with or without fee is hereby granted, provided that the above
15 * copyright notice and this permission notice appear in all copies.
40f53fa8 16 *
dafcb997
MA
17 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
18 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
20 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
23 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f690123c 24 */
f104fbbe 25
ab023a65 26/*! \file */
9c3531d7 27
cb6a185c 28#include <inttypes.h>
994e6569 29#include <stdbool.h>
f104fbbe 30#include <stdlib.h>
253f774e 31#include <time.h>
d9eebc08 32#include <unistd.h>
f104fbbe 33
c9d7e543 34#include <isc/app.h>
6098d364 35#include <isc/base32.h>
6a759e38 36#include <isc/commandline.h>
c9d7e543 37#include <isc/event.h>
bb74e8ce 38#include <isc/file.h>
40e7c805 39#include <isc/hash.h>
6098d364 40#include <isc/hex.h>
7fd3dc63 41#include <isc/md.h>
f104fbbe 42#include <isc/mem.h>
c9d7e543
BW
43#include <isc/mutex.h>
44#include <isc/os.h>
16a68807 45#include <isc/print.h>
6e8a8077 46#include <isc/random.h>
2534a73a 47#include <isc/rwlock.h>
b0c15bd9 48#include <isc/serial.h>
420a43c8 49#include <isc/safe.h>
e8892697 50#include <isc/stdio.h>
1a69a1a7 51#include <isc/string.h>
c9d7e543 52#include <isc/task.h>
acc63b06 53#include <isc/time.h>
6098d364 54#include <isc/util.h>
f104fbbe 55
f104fbbe
BW
56#include <dns/db.h>
57#include <dns/dbiterator.h>
911ed0d3 58#include <dns/diff.h>
09f22ac5 59#include <dns/dnssec.h>
0b09763c 60#include <dns/ds.h>
90fc2679 61#include <dns/fixedname.h>
09f22ac5
DL
62#include <dns/keyvalues.h>
63#include <dns/log.h>
e8892697
BW
64#include <dns/master.h>
65#include <dns/masterdump.h>
93d6dfaf 66#include <dns/nsec.h>
6098d364 67#include <dns/nsec3.h>
f104fbbe 68#include <dns/rdata.h>
6098d364 69#include <dns/rdatalist.h>
f104fbbe 70#include <dns/rdataset.h>
1f8f904a 71#include <dns/rdataclass.h>
f104fbbe 72#include <dns/rdatasetiter.h>
49f7148b 73#include <dns/rdatastruct.h>
a67fc7e1 74#include <dns/rdatatype.h>
f104fbbe 75#include <dns/result.h>
6ed53e59 76#include <dns/soa.h>
bf5582ad 77#include <dns/time.h>
b4ba66ba 78#include <dns/update.h>
3a14450d 79#include <dns/zoneverify.h>
f104fbbe 80
b1d234eb 81#include <dst/dst.h>
f104fbbe 82
c3b8130f 83#if USE_PKCS11
acbb301e
EH
84#include <pk11/result.h>
85#endif
86
b1d234eb
BW
87#include "dnssectool.h"
88
9edd523c 89#ifndef PATH_MAX
f0f71420 90#define PATH_MAX 1024 /* WIN32, and others don't define this. */
9edd523c
EH
91#endif
92
7efc8c3f 93const char *program = "dnssec-signzone";
b1d234eb 94int verbose;
3de75141 95
6098d364
MA
96typedef struct hashlist hashlist_t;
97
98static int nsec_datatype = dns_rdatatype_nsec;
99
3a14450d
MK
100#define check_dns_dbiterator_current(result) \
101 check_result((result == DNS_R_NEWORIGIN) ? ISC_R_SUCCESS : result, \
102 "dns_dbiterator_current()")
103
6098d364
MA
104#define IS_NSEC3 (nsec_datatype == dns_rdatatype_nsec3)
105#define OPTOUT(x) (((x) & DNS_NSEC3FLAG_OPTOUT) != 0)
106
3727725b
EH
107#define REVOKE(x) ((dst_key_flags(x) & DNS_KEYFLAG_REVOKE) != 0)
108
bf5582ad 109#define BUFSIZE 2048
0b09763c 110#define MAXDSKEYS 8
f104fbbe 111
c9d7e543
BW
112#define SIGNER_EVENTCLASS ISC_EVENTCLASS(0x4453)
113#define SIGNER_EVENT_WRITE (SIGNER_EVENTCLASS + 0)
114#define SIGNER_EVENT_WORK (SIGNER_EVENTCLASS + 1)
115
6ed53e59
DH
116#define SOA_SERIAL_KEEP 0
117#define SOA_SERIAL_INCREMENT 1
118#define SOA_SERIAL_UNIXTIME 2
b4ba66ba 119#define SOA_SERIAL_DATE 3
6ed53e59 120
c9d7e543
BW
121typedef struct signer_event sevent_t;
122struct signer_event {
123 ISC_EVENT_COMMON(sevent_t);
124 dns_fixedname_t *fname;
c9d7e543
BW
125 dns_dbnode_t *node;
126};
127
553ead32 128static dns_dnsseckeylist_t keylist;
66f5b00c 129static unsigned int keycount = 0;
2534a73a 130isc_rwlock_t keylist_lock;
61271cde 131static isc_stdtime_t starttime = 0, endtime = 0, dnskey_endtime = 0, now;
2b71493a 132static int cycle = -1;
6e8a8077 133static int jitter = 0;
d2890413 134static int jump = 0;
994e6569
OS
135static bool tryverify = false;
136static bool printstats = false;
f104fbbe 137static isc_mem_t *mctx = NULL;
553ead32 138static dns_ttl_t zone_soa_min_ttl;
eab9975b 139static dns_ttl_t soa_ttl;
11463c0a 140static FILE *outfp = NULL;
c9c630ee 141static char *tempfile = NULL;
a9c57ae2 142static const dns_master_style_t *masterstyle;
e1740442
MA
143static dns_masterformat_t inputformat = dns_masterformat_text;
144static dns_masterformat_t outputformat = dns_masterformat_text;
cb6a185c 145static uint32_t rawversion = 1, serialnum = 0;
994e6569 146static bool snset = false;
469f328a
BW
147static unsigned int nsigned = 0, nretained = 0, ndropped = 0;
148static unsigned int nverified = 0, nverifyfailed = 0;
553ead32 149static const char *directory = NULL, *dsdir = NULL;
c9d7e543
BW
150static isc_mutex_t namelock, statslock;
151static isc_taskmgr_t *taskmgr = NULL;
152static dns_db_t *gdb; /* The database */
153static dns_dbversion_t *gversion; /* The database version */
154static dns_dbiterator_t *gdbiter; /* The database iterator */
0b09763c 155static dns_rdataclass_t gclass; /* The class */
c9d7e543 156static dns_name_t *gorigin; /* The database origin */
6098d364 157static int nsec3flags = 0;
a93a66f6 158static dns_iterations_t nsec3iter = 10U;
fb596cc9 159static unsigned char saltbuf[255];
11463c0a 160static unsigned char *gsalt = saltbuf;
fb596cc9 161static size_t salt_length = 0;
c9d7e543
BW
162static isc_task_t *master = NULL;
163static unsigned int ntasks = 0;
994e6569
OS
164static bool shuttingdown = false, finished = false;
165static bool nokeys = false;
166static bool removefile = false;
167static bool generateds = false;
168static bool ignore_kskflag = false;
169static bool keyset_kskonly = false;
50105afc
MA
170static dns_name_t *dlv = NULL;
171static dns_fixedname_t dlv_fixed;
172static dns_master_style_t *dsstyle = NULL;
6ed53e59 173static unsigned int serialformat = SOA_SERIAL_KEEP;
6098d364 174static unsigned int hash_length = 0;
994e6569
OS
175static bool unknownalg = false;
176static bool disable_zone_check = false;
177static bool update_chain = false;
178static bool set_keyttl = false;
eab9975b 179static dns_ttl_t keyttl;
994e6569
OS
180static bool smartsign = false;
181static bool remove_orphansigs = false;
182static bool remove_inactkeysigs = false;
183static bool output_dnssec_only = false;
184static bool output_stdout = false;
185bool set_maxttl = false;
35f6a21f 186static dns_ttl_t maxttl = 0;
c9d7e543
BW
187
188#define INCSTAT(counter) \
189 if (printstats) { \
190 LOCK(&statslock); \
191 counter++; \
192 UNLOCK(&statslock); \
193 }
194
195static void
196sign(isc_task_t *task, isc_event_t *event);
197
7be900a9
MA
198/*%
199 * Store a copy of 'name' in 'fzonecut' and return a pointer to that copy.
200 */
201static dns_name_t *
202savezonecut(dns_fixedname_t *fzonecut, dns_name_t *name) {
203 dns_name_t *result;
204
205 result = dns_fixedname_initname(fzonecut);
206 dns_name_copy(name, result, NULL);
207
208 return (result);
209}
210
05a1fd60
BW
211static void
212dumpnode(dns_name_t *name, dns_dbnode_t *node) {
eff7f78b
MA
213 dns_rdataset_t rds;
214 dns_rdatasetiter_t *iter = NULL;
215 isc_buffer_t *buffer = NULL;
216 isc_region_t r;
05a1fd60 217 isc_result_t result;
eff7f78b 218 unsigned bufsize = 4096;
05a1fd60 219
e1740442
MA
220 if (outputformat != dns_masterformat_text)
221 return;
eff7f78b
MA
222
223 if (!output_dnssec_only) {
224 result = dns_master_dumpnodetostream(mctx, gdb, gversion, node,
11463c0a 225 name, masterstyle, outfp);
eff7f78b
MA
226 check_result(result, "dns_master_dumpnodetostream");
227 return;
228 }
229
230 result = dns_db_allrdatasets(gdb, node, gversion, 0, &iter);
231 check_result(result, "dns_db_allrdatasets");
232
233 dns_rdataset_init(&rds);
234
235 result = isc_buffer_allocate(mctx, &buffer, bufsize);
236 check_result(result, "isc_buffer_allocate");
237
238 for (result = dns_rdatasetiter_first(iter);
239 result == ISC_R_SUCCESS;
240 result = dns_rdatasetiter_next(iter)) {
0e27506c 241
eff7f78b
MA
242 dns_rdatasetiter_current(iter, &rds);
243
244 if (rds.type != dns_rdatatype_rrsig &&
245 rds.type != dns_rdatatype_nsec &&
246 rds.type != dns_rdatatype_nsec3 &&
247 rds.type != dns_rdatatype_nsec3param &&
248 (!smartsign || rds.type != dns_rdatatype_dnskey)) {
249 dns_rdataset_disassociate(&rds);
250 continue;
251 }
252
cf39976b 253 for (;;) {
eff7f78b
MA
254 result = dns_master_rdatasettotext(name, &rds,
255 masterstyle, buffer);
256 if (result != ISC_R_NOSPACE)
257 break;
258
259 bufsize <<= 1;
260 isc_buffer_free(&buffer);
261 result = isc_buffer_allocate(mctx, &buffer, bufsize);
262 check_result(result, "isc_buffer_allocate");
263 }
264 check_result(result, "dns_master_rdatasettotext");
265
266 isc_buffer_usedregion(buffer, &r);
11463c0a 267 result = isc_stdio_write(r.base, 1, r.length, outfp, NULL);
eff7f78b
MA
268 check_result(result, "isc_stdio_write");
269 isc_buffer_clear(buffer);
270
271 dns_rdataset_disassociate(&rds);
272 }
273
274 isc_buffer_free(&buffer);
275 dns_rdatasetiter_destroy(&iter);
05a1fd60
BW
276}
277
520cea04 278/*%
99a0cd02 279 * Sign the given RRset with given key, and add the signature record to the
520cea04
MA
280 * given tuple.
281 */
bf5582ad 282static void
520cea04
MA
283signwithkey(dns_name_t *name, dns_rdataset_t *rdataset, dst_key_t *key,
284 dns_ttl_t ttl, dns_diff_t *add, const char *logmsg)
b18192fd 285{
b18192fd 286 isc_result_t result;
61271cde 287 isc_stdtime_t jendtime, expiry;
77b8f88f 288 char keystr[DST_KEY_FORMATSIZE];
520cea04 289 dns_rdata_t trdata = DNS_RDATA_INIT;
553ead32
EH
290 unsigned char array[BUFSIZE];
291 isc_buffer_t b;
520cea04
MA
292 dns_difftuple_t *tuple;
293
77b8f88f 294 dst_key_format(key, keystr, sizeof(keystr));
520cea04 295 vbprintf(1, "\t%s %s\n", logmsg, keystr);
b18192fd 296
61271cde
EH
297 if (rdataset->type == dns_rdatatype_dnskey)
298 expiry = dnskey_endtime;
299 else
300 expiry = endtime;
301
3a4f820d 302 jendtime = (jitter != 0) ? expiry - isc_random_uniform(jitter) : expiry;
d2890413 303 jendtime -= (jump != 0) ? jendtime % jump : 0;
553ead32 304 isc_buffer_init(&b, array, sizeof(array));
6e8a8077 305 result = dns_dnssec_sign(name, rdataset, key, &starttime, &jendtime,
520cea04 306 mctx, &b, &trdata);
4fc4813e 307 if (result != ISC_R_SUCCESS) {
cc3aafe7 308 fatal("dnskey '%s' failed to sign data: %s",
4fc4813e
BW
309 keystr, isc_result_totext(result));
310 }
c9d7e543 311 INCSTAT(nsigned);
2b71493a 312
abce4875 313 if (tryverify) {
a15583c6 314 result = dns_dnssec_verify(name, rdataset, key,
994e6569 315 true, 0, mctx, &trdata, NULL);
25cd3168 316 if (result == ISC_R_SUCCESS || result == DNS_R_FROMWILDCARD) {
abce4875 317 vbprintf(3, "\tsignature verified\n");
c9d7e543 318 INCSTAT(nverified);
469f328a 319 } else {
abce4875 320 vbprintf(3, "\tsignature failed to verify\n");
c9d7e543 321 INCSTAT(nverifyfailed);
469f328a 322 }
2b71493a 323 }
520cea04
MA
324
325 tuple = NULL;
b7e40659
EH
326 result = dns_difftuple_create(mctx, DNS_DIFFOP_ADDRESIGN,
327 name, ttl, &trdata, &tuple);
520cea04
MA
328 check_result(result, "dns_difftuple_create");
329 dns_diff_append(add, &tuple);
bf5582ad 330}
b18192fd 331
994e6569 332static inline bool
553ead32
EH
333issigningkey(dns_dnsseckey_t *key) {
334 return (key->force_sign || key->hint_sign);
b18192fd
BW
335}
336
994e6569 337static inline bool
35f1a4fc
EH
338ispublishedkey(dns_dnsseckey_t *key) {
339 return ((key->force_publish || key->hint_publish) &&
340 !key->hint_remove);
341}
342
994e6569 343static inline bool
553ead32 344iszonekey(dns_dnsseckey_t *key) {
994e6569
OS
345 return (dns_name_equal(dst_key_name(key->key), gorigin) &&
346 dst_key_iszonekey(key->key));
bf5582ad
BW
347}
348
994e6569 349static inline bool
553ead32
EH
350isksk(dns_dnsseckey_t *key) {
351 return (key->ksk);
352}
353
994e6569 354static inline bool
553ead32
EH
355iszsk(dns_dnsseckey_t *key) {
356 return (ignore_kskflag || !key->ksk);
357}
358
ab023a65 359/*%
553ead32
EH
360 * Find the key that generated an RRSIG, if it is in the key list. If
361 * so, return a pointer to it, otherwise return NULL.
362 *
2534a73a 363 * No locking is performed here, this must be done by the caller.
2d478b70 364 */
553ead32 365static dns_dnsseckey_t *
2534a73a 366keythatsigned_unlocked(dns_rdata_rrsig_t *rrsig) {
553ead32 367 dns_dnsseckey_t *key;
bf5582ad 368
53c22b8e
EH
369 for (key = ISC_LIST_HEAD(keylist);
370 key != NULL;
371 key = ISC_LIST_NEXT(key, link)) {
cc3aafe7
MA
372 if (rrsig->keyid == dst_key_id(key->key) &&
373 rrsig->algorithm == dst_key_alg(key->key) &&
374 dns_name_equal(&rrsig->signer, dst_key_name(key->key)))
2534a73a 375 return (key);
bf5582ad 376 }
2534a73a
MA
377 return (NULL);
378}
379
380/*%
381 * Finds the key that generated a RRSIG, if possible. First look at the keys
382 * that we've loaded already, and then see if there's a key on disk.
383 */
553ead32 384static dns_dnsseckey_t *
2534a73a
MA
385keythatsigned(dns_rdata_rrsig_t *rrsig) {
386 isc_result_t result;
387 dst_key_t *pubkey = NULL, *privkey = NULL;
553ead32 388 dns_dnsseckey_t *key = NULL;
2534a73a
MA
389
390 isc_rwlock_lock(&keylist_lock, isc_rwlocktype_read);
391 key = keythatsigned_unlocked(rrsig);
392 isc_rwlock_unlock(&keylist_lock, isc_rwlocktype_read);
393 if (key != NULL)
394 return (key);
395
396 /*
397 * We did not find the key in our list. Get a write lock now, since
398 * we may be modifying the bits. We could do the tryupgrade() dance,
399 * but instead just get a write lock and check once again to see if
400 * it is on our list. It's possible someone else may have added it
401 * after all.
402 */
403 isc_rwlock_lock(&keylist_lock, isc_rwlocktype_write);
2534a73a
MA
404 key = keythatsigned_unlocked(rrsig);
405 if (key != NULL) {
406 isc_rwlock_unlock(&keylist_lock, isc_rwlocktype_write);
407 return (key);
408 }
49f7148b 409
cc3aafe7
MA
410 result = dst_key_fromfile(&rrsig->signer, rrsig->keyid,
411 rrsig->algorithm, DST_TYPE_PUBLIC,
553ead32 412 directory, mctx, &pubkey);
2534a73a
MA
413 if (result != ISC_R_SUCCESS) {
414 isc_rwlock_unlock(&keylist_lock, isc_rwlocktype_write);
bf5582ad 415 return (NULL);
2534a73a 416 }
49f7148b 417
cc3aafe7
MA
418 result = dst_key_fromfile(&rrsig->signer, rrsig->keyid,
419 rrsig->algorithm,
1ba1b524 420 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
553ead32 421 directory, mctx, &privkey);
bf5582ad 422 if (result == ISC_R_SUCCESS) {
c50936eb 423 dst_key_free(&pubkey);
c2255e86
EH
424 result = dns_dnsseckey_create(mctx, &privkey, &key);
425 } else
426 result = dns_dnsseckey_create(mctx, &pubkey, &key);
35f1a4fc 427
c2255e86 428 if (result == ISC_R_SUCCESS) {
994e6569
OS
429 key->force_publish = false;
430 key->force_sign = false;
653a78de 431 key->index = keycount++;
c2255e86
EH
432 ISC_LIST_APPEND(keylist, key, link);
433 }
2534a73a
MA
434
435 isc_rwlock_unlock(&keylist_lock, isc_rwlocktype_write);
469f328a 436 return (key);
49f7148b
BW
437}
438
ab023a65 439/*%
cc3aafe7
MA
440 * Check to see if we expect to find a key at this name. If we see a RRSIG
441 * and can't find the signing key that we expect to find, we drop the rrsig.
2d478b70
BW
442 * I'm not sure if this is completely correct, but it seems to work.
443 */
994e6569 444static bool
c9d7e543 445expecttofindkey(dns_name_t *name) {
bf5582ad 446 unsigned int options = DNS_DBFIND_NOWILD;
587368b2 447 dns_fixedname_t fname;
bf5582ad 448 isc_result_t result;
4fc4813e 449 char namestr[DNS_NAME_FORMATSIZE];
bf5582ad 450
587368b2 451 dns_fixedname_init(&fname);
93d6dfaf 452 result = dns_db_find(gdb, name, gversion, dns_rdatatype_dnskey, options,
587368b2 453 0, NULL, dns_fixedname_name(&fname), NULL, NULL);
bf5582ad 454 switch (result) {
4fc4813e
BW
455 case ISC_R_SUCCESS:
456 case DNS_R_NXDOMAIN:
457 case DNS_R_NXRRSET:
994e6569 458 return (true);
4fc4813e
BW
459 case DNS_R_DELEGATION:
460 case DNS_R_CNAME:
461 case DNS_R_DNAME:
994e6569 462 return (false);
bf5582ad 463 }
2f734e0a 464 dns_name_format(name, namestr, sizeof(namestr));
cc3aafe7 465 fatal("failure looking for '%s DNSKEY' in database: %s",
4fc4813e 466 namestr, isc_result_totext(result));
12107993 467 /* NOTREACHED */
994e6569 468 return (false); /* removes a warning */
bf5582ad
BW
469}
470
994e6569 471static inline bool
53c22b8e 472setverifies(dns_name_t *name, dns_rdataset_t *set, dst_key_t *key,
cc3aafe7 473 dns_rdata_t *rrsig)
3d452d43 474{
a15583c6 475 isc_result_t result;
994e6569 476 result = dns_dnssec_verify(name, set, key, false, 0, mctx, rrsig,
25cd3168
WK
477 NULL);
478 if (result == ISC_R_SUCCESS || result == DNS_R_FROMWILDCARD) {
c9d7e543 479 INCSTAT(nverified);
994e6569 480 return (true);
469f328a 481 } else {
c9d7e543 482 INCSTAT(nverifyfailed);
994e6569 483 return (false);
469f328a 484 }
bf5582ad
BW
485}
486
ab023a65 487/*%
cc3aafe7 488 * Signs a set. Goes through contortions to decide if each RRSIG should
2d478b70
BW
489 * be dropped or retained, and then determines if any new SIGs need to
490 * be generated.
491 */
bf5582ad 492static void
30b41c20 493signset(dns_diff_t *del, dns_diff_t *add, dns_dbnode_t *node, dns_name_t *name,
c9d7e543 494 dns_rdataset_t *set)
bf5582ad 495{
b4d8cb6b 496 dns_rdataset_t sigset;
c03bb27f 497 dns_rdata_t sigrdata = DNS_RDATA_INIT;
cc3aafe7 498 dns_rdata_rrsig_t rrsig;
553ead32 499 dns_dnsseckey_t *key;
3d452d43 500 isc_result_t result;
994e6569
OS
501 bool nosigs = false;
502 bool *wassignedby, *nowsignedby;
66f5b00c 503 int arraysize;
b4d8cb6b
BW
504 dns_difftuple_t *tuple;
505 dns_ttl_t ttl;
3d452d43 506 int i;
4fc4813e 507 char namestr[DNS_NAME_FORMATSIZE];
ffe8ddd9 508 char typestr[DNS_RDATATYPE_FORMATSIZE];
4fc4813e
BW
509 char sigstr[SIG_FORMATSIZE];
510
2f734e0a 511 dns_name_format(name, namestr, sizeof(namestr));
ffe8ddd9 512 dns_rdatatype_format(set->type, typestr, sizeof(typestr));
bf5582ad 513
b4d8cb6b 514 ttl = ISC_MIN(set->ttl, endtime - starttime);
bf5582ad 515
b4d8cb6b 516 dns_rdataset_init(&sigset);
93d6dfaf 517 result = dns_db_findrdataset(gdb, node, gversion, dns_rdatatype_rrsig,
b4d8cb6b 518 set->type, 0, &sigset, NULL);
2c02fd53 519 if (result == ISC_R_NOTFOUND) {
eeb13c7c
EH
520 vbprintf(2, "no existing signatures for %s/%s\n",
521 namestr, typestr);
2c02fd53 522 result = ISC_R_SUCCESS;
994e6569 523 nosigs = true;
2c02fd53 524 }
6a285c81 525 if (result != ISC_R_SUCCESS)
cc3aafe7 526 fatal("failed while looking for '%s RRSIG %s': %s",
4fc4813e 527 namestr, typestr, isc_result_totext(result));
bf5582ad 528
4fc4813e 529 vbprintf(1, "%s/%s:\n", namestr, typestr);
a67fc7e1 530
66f5b00c
BW
531 arraysize = keycount;
532 if (!nosigs)
533 arraysize += dns_rdataset_count(&sigset);
994e6569
OS
534 wassignedby = isc_mem_get(mctx, arraysize * sizeof(bool));
535 nowsignedby = isc_mem_get(mctx, arraysize * sizeof(bool));
66f5b00c
BW
536
537 for (i = 0; i < arraysize; i++)
994e6569 538 wassignedby[i] = nowsignedby[i] = false;
66f5b00c 539
924fbebf
BW
540 if (nosigs)
541 result = ISC_R_NOMORE;
542 else
b4d8cb6b 543 result = dns_rdataset_first(&sigset);
bf5582ad 544
924fbebf 545 while (result == ISC_R_SUCCESS) {
994e6569
OS
546 bool expired, future;
547 bool keep = false, resign = false;
bf5582ad 548
924fbebf 549 dns_rdataset_current(&sigset, &sigrdata);
bf5582ad 550
cc3aafe7 551 result = dns_rdata_tostruct(&sigrdata, &rrsig, NULL);
924fbebf 552 check_result(result, "dns_rdata_tostruct");
bf5582ad 553
cc3aafe7 554 future = isc_serial_lt(now, rrsig.timesigned);
bf5582ad 555
cc3aafe7
MA
556 key = keythatsigned(&rrsig);
557 sig_format(&rrsig, sigstr, sizeof(sigstr));
b0c15bd9 558 if (key != NULL && issigningkey(key))
cc3aafe7 559 expired = isc_serial_gt(now + cycle, rrsig.timeexpire);
b0c15bd9 560 else
cc3aafe7 561 expired = isc_serial_gt(now, rrsig.timeexpire);
924fbebf 562
cc3aafe7
MA
563 if (isc_serial_gt(rrsig.timesigned, rrsig.timeexpire)) {
564 /* rrsig is dropped and not replaced */
565 vbprintf(2, "\trrsig by %s dropped - "
924fbebf 566 "invalid validity period\n",
4fc4813e 567 sigstr);
924fbebf 568 } else if (key == NULL && !future &&
938dfe6d 569 expecttofindkey(&rrsig.signer)) {
cc3aafe7
MA
570 /* rrsig is dropped and not replaced */
571 vbprintf(2, "\trrsig by %s dropped - "
572 "private dnskey not found\n",
4fc4813e 573 sigstr);
924fbebf 574 } else if (key == NULL || future) {
0bbe3273 575 keep = (!expired && !remove_orphansigs);
cc3aafe7 576 vbprintf(2, "\trrsig by %s %s - dnskey not found\n",
35f1a4fc 577 keep ? "retained" : "dropped", sigstr);
0bbe3273
EH
578 } else if (!dns_dnssec_keyactive(key->key, now) &&
579 remove_inactkeysigs) {
994e6569 580 keep = false;
0bbe3273
EH
581 vbprintf(2, "\trrsig by %s dropped - key inactive\n",
582 sigstr);
924fbebf 583 } else if (issigningkey(key)) {
994e6569 584 wassignedby[key->index] = true;
653a78de 585
2f09e7c3
MA
586 if (!expired && rrsig.originalttl == set->ttl &&
587 setverifies(name, set, key->key, &sigrdata)) {
cc3aafe7 588 vbprintf(2, "\trrsig by %s retained\n", sigstr);
994e6569 589 keep = true;
b4d8cb6b 590 } else {
cc3aafe7 591 vbprintf(2, "\trrsig by %s dropped - %s\n",
2f09e7c3
MA
592 sigstr, expired ? "expired" :
593 rrsig.originalttl != set->ttl ?
594 "ttl change" : "failed to verify");
994e6569 595 resign = true;
a67fc7e1 596 }
0bbe3273 597 } else if (!ispublishedkey(key) && remove_orphansigs) {
35f1a4fc
EH
598 vbprintf(2, "\trrsig by %s dropped - dnskey removed\n",
599 sigstr);
c9d7e543 600 } else if (iszonekey(key)) {
994e6569 601 wassignedby[key->index] = true;
653a78de 602
2f09e7c3
MA
603 if (!expired && rrsig.originalttl == set->ttl &&
604 setverifies(name, set, key->key, &sigrdata)) {
cc3aafe7 605 vbprintf(2, "\trrsig by %s retained\n", sigstr);
994e6569 606 keep = true;
924fbebf 607 } else {
cc3aafe7 608 vbprintf(2, "\trrsig by %s dropped - %s\n",
2f09e7c3
MA
609 sigstr, expired ? "expired" :
610 rrsig.originalttl != set->ttl ?
611 "ttl change" : "failed to verify");
bf5582ad 612 }
924fbebf 613 } else if (!expired) {
cc3aafe7 614 vbprintf(2, "\trrsig by %s retained\n", sigstr);
994e6569 615 keep = true;
924fbebf 616 } else {
cc3aafe7 617 vbprintf(2, "\trrsig by %s expired\n", sigstr);
924fbebf 618 }
b4d8cb6b 619
469f328a 620 if (keep) {
64945263 621 if (key != NULL)
994e6569 622 nowsignedby[key->index] = true;
c9d7e543 623 INCSTAT(nretained);
30b41c20
MA
624 if (sigset.ttl != ttl) {
625 vbprintf(2, "\tfixing ttl %s\n", sigstr);
626 tuple = NULL;
627 result = dns_difftuple_create(mctx,
b7e40659
EH
628 DNS_DIFFOP_DELRESIGN,
629 name, sigset.ttl,
630 &sigrdata, &tuple);
30b41c20
MA
631 check_result(result, "dns_difftuple_create");
632 dns_diff_append(del, &tuple);
633 result = dns_difftuple_create(mctx,
b7e40659
EH
634 DNS_DIFFOP_ADDRESIGN,
635 name, ttl,
636 &sigrdata, &tuple);
30b41c20
MA
637 check_result(result, "dns_difftuple_create");
638 dns_diff_append(add, &tuple);
639 }
469f328a 640 } else {
924fbebf 641 tuple = NULL;
653a78de 642 vbprintf(2, "removing signature by %s\n", sigstr);
b7e40659
EH
643 result = dns_difftuple_create(mctx,
644 DNS_DIFFOP_DELRESIGN,
7dec36c2
BW
645 name, sigset.ttl,
646 &sigrdata, &tuple);
924fbebf 647 check_result(result, "dns_difftuple_create");
30b41c20 648 dns_diff_append(del, &tuple);
c9d7e543 649 INCSTAT(ndropped);
924fbebf
BW
650 }
651
652 if (resign) {
30b41c20
MA
653 INSIST(!keep);
654
520cea04
MA
655 signwithkey(name, set, key->key, ttl, add,
656 "resigning with dnskey");
994e6569 657 nowsignedby[key->index] = true;
3d452d43 658 }
924fbebf 659
368b37b6 660 dns_rdata_reset(&sigrdata);
cc3aafe7 661 dns_rdata_freestruct(&rrsig);
924fbebf 662 result = dns_rdataset_next(&sigset);
b18192fd 663 }
924fbebf
BW
664 if (result == ISC_R_NOMORE)
665 result = ISC_R_SUCCESS;
666
667 check_result(result, "dns_rdataset_first/next");
668 if (dns_rdataset_isassociated(&sigset))
669 dns_rdataset_disassociate(&sigset);
bf5582ad 670
56ae0ccb
BW
671 for (key = ISC_LIST_HEAD(keylist);
672 key != NULL;
673 key = ISC_LIST_NEXT(key, link))
674 {
653a78de 675 if (nowsignedby[key->index])
0b09763c
MA
676 continue;
677
553ead32 678 if (!issigningkey(key))
56ae0ccb
BW
679 continue;
680
20502f35
EH
681 if ((set->type == dns_rdatatype_cds ||
682 set->type == dns_rdatatype_cdnskey ||
683 set->type == dns_rdatatype_dnskey) &&
3727725b 684 dns_name_equal(name, gorigin)) {
994e6569 685 bool have_ksk;
3727725b
EH
686 dns_dnsseckey_t *tmpkey;
687
ef9ee925 688 have_ksk = isksk(key);
3727725b
EH
689 for (tmpkey = ISC_LIST_HEAD(keylist);
690 tmpkey != NULL;
691 tmpkey = ISC_LIST_NEXT(tmpkey, link)) {
692 if (dst_key_alg(key->key) !=
693 dst_key_alg(tmpkey->key))
694 continue;
695 if (REVOKE(tmpkey->key))
696 continue;
697 if (isksk(tmpkey))
994e6569 698 have_ksk = true;
3727725b
EH
699 }
700 if (isksk(key) || !have_ksk ||
701 (iszsk(key) && !keyset_kskonly))
702 signwithkey(name, set, key->key, ttl, add,
703 "signing with dnskey");
20502f35 704 } else if (iszsk(key)) {
520cea04
MA
705 signwithkey(name, set, key->key, ttl, add,
706 "signing with dnskey");
553ead32 707 }
bf5582ad 708 }
66f5b00c 709
994e6569
OS
710 isc_mem_put(mctx, wassignedby, arraysize * sizeof(bool));
711 isc_mem_put(mctx, nowsignedby, arraysize * sizeof(bool));
bf5582ad
BW
712}
713
6098d364
MA
714struct hashlist {
715 unsigned char *hashbuf;
716 size_t entries;
717 size_t size;
718 size_t length;
719};
720
721static void
722hashlist_init(hashlist_t *l, unsigned int nodes, unsigned int length) {
723
724 l->entries = 0;
725 l->length = length + 1;
726
727 if (nodes != 0) {
728 l->size = nodes;
729 l->hashbuf = malloc(l->size * l->length);
730 if (l->hashbuf == NULL)
731 l->size = 0;
732 } else {
733 l->size = 0;
734 l->hashbuf = NULL;
735 }
736}
737
8444b485
MA
738static void
739hashlist_free(hashlist_t *l) {
740 if (l->hashbuf) {
741 free(l->hashbuf);
742 l->hashbuf = NULL;
743 l->entries = 0;
744 l->length = 0;
745 l->size = 0;
746 }
747}
748
6098d364
MA
749static void
750hashlist_add(hashlist_t *l, const unsigned char *hash, size_t len)
751{
752
753 REQUIRE(len <= l->length);
754
755 if (l->entries == l->size) {
756 l->size = l->size * 2 + 100;
757 l->hashbuf = realloc(l->hashbuf, l->size * l->length);
040dc292
MA
758 if (l->hashbuf == NULL)
759 fatal("unable to grow hashlist: out of memory");
6098d364
MA
760 }
761 memset(l->hashbuf + l->entries * l->length, 0, l->length);
e851ea82 762 memmove(l->hashbuf + l->entries * l->length, hash, len);
6098d364
MA
763 l->entries++;
764}
765
766static void
767hashlist_add_dns_name(hashlist_t *l, /*const*/ dns_name_t *name,
768 unsigned int hashalg, unsigned int iterations,
11463c0a 769 const unsigned char *salt, size_t salt_len,
994e6569 770 bool speculative)
6098d364
MA
771{
772 char nametext[DNS_NAME_FORMATSIZE];
773 unsigned char hash[NSEC3_MAX_HASH_LENGTH + 1];
774 unsigned int len;
775 size_t i;
776
c3c8823f 777 len = isc_iterated_hash(hash, hashalg, iterations,
11463c0a 778 salt, (int)salt_len,
6098d364
MA
779 name->ndata, name->length);
780 if (verbose) {
781 dns_name_format(name, nametext, sizeof nametext);
782 for (i = 0 ; i < len; i++)
783 fprintf(stderr, "%02x", hash[i]);
784 fprintf(stderr, " %s\n", nametext);
785 }
786 hash[len++] = speculative ? 1 : 0;
787 hashlist_add(l, hash, len);
788}
789
790static int
791hashlist_comp(const void *a, const void *b) {
b105ccee 792 return (memcmp(a, b, hash_length + 1));
6098d364
MA
793}
794
795static void
796hashlist_sort(hashlist_t *l) {
797 qsort(l->hashbuf, l->entries, l->length, hashlist_comp);
798}
799
994e6569 800static bool
6098d364
MA
801hashlist_hasdup(hashlist_t *l) {
802 unsigned char *current;
803 unsigned char *next = l->hashbuf;
804 size_t entries = l->entries;
805
806 /*
807 * Skip initial speculative wild card hashs.
808 */
dd14c953 809 while (entries > 0U && next[l->length-1] != 0U) {
6098d364
MA
810 next += l->length;
811 entries--;
812 }
813
814 current = next;
815 while (entries-- > 1U) {
816 next += l->length;
817 if (next[l->length-1] != 0)
818 continue;
420a43c8 819 if (isc_safe_memequal(current, next, l->length - 1))
994e6569 820 return (true);
6098d364
MA
821 current = next;
822 }
994e6569 823 return (false);
6098d364
MA
824}
825
826static const unsigned char *
827hashlist_findnext(const hashlist_t *l,
828 const unsigned char hash[NSEC3_MAX_HASH_LENGTH])
829{
c3c8823f 830 size_t entries = l->entries;
6098d364
MA
831 const unsigned char *next = bsearch(hash, l->hashbuf, l->entries,
832 l->length, hashlist_comp);
833 INSIST(next != NULL);
834
835 do {
836 if (next < l->hashbuf + (l->entries - 1) * l->length)
837 next += l->length;
838 else
839 next = l->hashbuf;
840 if (next[l->length - 1] == 0)
841 break;
53f70575
MA
842 } while (entries-- > 1U);
843 INSIST(entries != 0U);
6098d364
MA
844 return (next);
845}
846
994e6569 847static bool
6098d364
MA
848hashlist_exists(const hashlist_t *l,
849 const unsigned char hash[NSEC3_MAX_HASH_LENGTH])
850{
851 if (bsearch(hash, l->hashbuf, l->entries, l->length, hashlist_comp))
994e6569 852 return (true);
6098d364 853 else
994e6569 854 return (false);
6098d364
MA
855}
856
857static void
858addnowildcardhash(hashlist_t *l, /*const*/ dns_name_t *name,
859 unsigned int hashalg, unsigned int iterations,
11463c0a 860 const unsigned char *salt, size_t salt_len)
6098d364
MA
861{
862 dns_fixedname_t fixed;
863 dns_name_t *wild;
864 dns_dbnode_t *node = NULL;
865 isc_result_t result;
866 char namestr[DNS_NAME_FORMATSIZE];
867
4df4a8e7 868 wild = dns_fixedname_initname(&fixed);
6098d364
MA
869
870 result = dns_name_concatenate(dns_wildcardname, name, wild, NULL);
871 if (result == ISC_R_NOSPACE)
872 return;
873 check_result(result,"addnowildcardhash: dns_name_concatenate()");
874
994e6569 875 result = dns_db_findnode(gdb, wild, false, &node);
6098d364
MA
876 if (result == ISC_R_SUCCESS) {
877 dns_db_detachnode(gdb, &node);
878 return;
879 }
880
881 if (verbose) {
882 dns_name_format(wild, namestr, sizeof(namestr));
883 fprintf(stderr, "adding no-wildcardhash for %s\n", namestr);
884 }
885
11463c0a 886 hashlist_add_dns_name(l, wild, hashalg, iterations, salt, salt_len,
994e6569 887 true);
6098d364
MA
888}
889
b4d8cb6b
BW
890static void
891opendb(const char *prefix, dns_name_t *name, dns_rdataclass_t rdclass,
c9d7e543 892 dns_db_t **dbp)
b4d8cb6b 893{
553ead32 894 char filename[PATH_MAX];
b4d8cb6b
BW
895 isc_buffer_t b;
896 isc_result_t result;
897
898 isc_buffer_init(&b, filename, sizeof(filename));
553ead32
EH
899 if (dsdir != NULL) {
900 /* allow room for a trailing slash */
901 if (strlen(dsdir) >= isc_buffer_availablelength(&b))
902 fatal("path '%s' is too long", dsdir);
903 isc_buffer_putstr(&b, dsdir);
904 if (dsdir[strlen(dsdir) - 1] != '/')
82f4c5bc
BW
905 isc_buffer_putstr(&b, "/");
906 }
553ead32
EH
907 if (strlen(prefix) > isc_buffer_availablelength(&b))
908 fatal("path '%s' is too long", dsdir);
b4d8cb6b 909 isc_buffer_putstr(&b, prefix);
994e6569 910 result = dns_name_tofilenametext(name, false, &b);
7d7215ba 911 check_result(result, "dns_name_tofilenametext()");
4fc4813e
BW
912 if (isc_buffer_availablelength(&b) == 0) {
913 char namestr[DNS_NAME_FORMATSIZE];
2f734e0a 914 dns_name_format(name, namestr, sizeof(namestr));
4fc4813e
BW
915 fatal("name '%s' is too long", namestr);
916 }
b4d8cb6b
BW
917 isc_buffer_putuint8(&b, 0);
918
d566e8ea 919 result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone,
c9d7e543 920 rdclass, 0, NULL, dbp);
b4d8cb6b
BW
921 check_result(result, "dns_db_create()");
922
275a6a3b 923 result = dns_db_load(*dbp, filename, inputformat, DNS_MASTER_HINT);
b20ee662 924 if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
c9d7e543 925 dns_db_detach(dbp);
b4d8cb6b
BW
926}
927
ab023a65 928/*%
553ead32
EH
929 * Load the DS set for a child zone, if a dsset-* file can be found.
930 * If not, try to find a keyset-* file from an earlier version of
931 * dnssec-signzone, and build DS records from that.
abce4875 932 */
0b09763c 933static isc_result_t
cb6a185c 934loadds(dns_name_t *name, uint32_t ttl, dns_rdataset_t *dsset) {
0b09763c
MA
935 dns_db_t *db = NULL;
936 dns_dbversion_t *ver = NULL;
937 dns_dbnode_t *node = NULL;
abce4875 938 isc_result_t result;
0b09763c
MA
939 dns_rdataset_t keyset;
940 dns_rdata_t key, ds;
941 unsigned char dsbuf[DNS_DS_BUFFERSIZE];
942 dns_diff_t diff;
943 dns_difftuple_t *tuple = NULL;
abce4875 944
553ead32
EH
945 opendb("dsset-", name, gclass, &db);
946 if (db != NULL) {
994e6569 947 result = dns_db_findnode(db, name, false, &node);
553ead32
EH
948 if (result == ISC_R_SUCCESS) {
949 dns_rdataset_init(dsset);
950 result = dns_db_findrdataset(db, node, NULL,
951 dns_rdatatype_ds, 0, 0,
952 dsset, NULL);
953 dns_db_detachnode(db, &node);
954 if (result == ISC_R_SUCCESS) {
955 vbprintf(2, "found DS records\n");
956 dsset->ttl = ttl;
957 dns_db_detach(&db);
958 return (result);
959 }
960 }
961 dns_db_detach(&db);
962 }
963
964 /* No DS records found; try again, looking for DNSKEY records */
0b09763c 965 opendb("keyset-", name, gclass, &db);
553ead32 966 if (db == NULL) {
0b09763c 967 return (ISC_R_NOTFOUND);
bf5582ad 968 }
553ead32 969
994e6569 970 result = dns_db_findnode(db, name, false, &node);
813b34eb
EH
971 if (result != ISC_R_SUCCESS) {
972 dns_db_detach(&db);
973 return (result);
974 }
975
0b09763c 976 dns_rdataset_init(&keyset);
93d6dfaf 977 result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_dnskey, 0, 0,
0b09763c
MA
978 &keyset, NULL);
979 if (result != ISC_R_SUCCESS) {
980 dns_db_detachnode(db, &node);
981 dns_db_detach(&db);
982 return (result);
b4d8cb6b 983 }
cc3aafe7 984 vbprintf(2, "found DNSKEY records\n");
417bd845 985
0b09763c
MA
986 result = dns_db_newversion(db, &ver);
987 check_result(result, "dns_db_newversion");
0b09763c 988 dns_diff_init(mctx, &diff);
abce4875 989
0b09763c
MA
990 for (result = dns_rdataset_first(&keyset);
991 result == ISC_R_SUCCESS;
992 result = dns_rdataset_next(&keyset))
993 {
994 dns_rdata_init(&key);
995 dns_rdata_init(&ds);
996 dns_rdataset_current(&keyset, &key);
997 result = dns_ds_buildrdata(name, &key, DNS_DSDIGEST_SHA1,
998 dsbuf, &ds);
999 check_result(result, "dns_ds_buildrdata");
abce4875 1000
b7e40659 1001 result = dns_difftuple_create(mctx, DNS_DIFFOP_ADDRESIGN, name,
b0c15bd9 1002 ttl, &ds, &tuple);
0b09763c
MA
1003 check_result(result, "dns_difftuple_create");
1004 dns_diff_append(&diff, &tuple);
c5387e69
MA
1005
1006 dns_rdata_reset(&ds);
1007 result = dns_ds_buildrdata(name, &key, DNS_DSDIGEST_SHA256,
1008 dsbuf, &ds);
1009 check_result(result, "dns_ds_buildrdata");
1010
b7e40659 1011 result = dns_difftuple_create(mctx, DNS_DIFFOP_ADDRESIGN, name,
c5387e69
MA
1012 ttl, &ds, &tuple);
1013 check_result(result, "dns_difftuple_create");
1014 dns_diff_append(&diff, &tuple);
abce4875 1015 }
553ead32 1016
0b09763c
MA
1017 result = dns_diff_apply(&diff, db, ver);
1018 check_result(result, "dns_diff_apply");
1019 dns_diff_clear(&diff);
abce4875 1020
994e6569 1021 dns_db_closeversion(db, &ver, true);
abce4875 1022
0b09763c
MA
1023 result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_ds, 0, 0,
1024 dsset, NULL);
1025 check_result(result, "dns_db_findrdataset");
1026
1027 dns_rdataset_disassociate(&keyset);
1028 dns_db_detachnode(db, &node);
1029 dns_db_detach(&db);
1030 return (result);
3d452d43
BW
1031}
1032
994e6569 1033static bool
6098d364
MA
1034secure(dns_name_t *name, dns_dbnode_t *node) {
1035 dns_rdataset_t dsset;
1036 isc_result_t result;
1037
1038 if (dns_name_equal(name, gorigin))
994e6569 1039 return (false);
6098d364
MA
1040
1041 dns_rdataset_init(&dsset);
1042 result = dns_db_findrdataset(gdb, node, gversion, dns_rdatatype_ds,
1043 0, 0, &dsset, NULL);
1044 if (dns_rdataset_isassociated(&dsset))
1045 dns_rdataset_disassociate(&dsset);
1046
994e6569 1047 return (result == ISC_R_SUCCESS);
6098d364
MA
1048}
1049
994e6569 1050static bool
3a14450d 1051is_delegation(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *origin,
cb6a185c 1052 dns_name_t *name, dns_dbnode_t *node, uint32_t *ttlp)
3a14450d
MK
1053{
1054 dns_rdataset_t nsset;
1055 isc_result_t result;
1056
1057 if (dns_name_equal(name, origin))
994e6569 1058 return (false);
3a14450d
MK
1059
1060 dns_rdataset_init(&nsset);
1061 result = dns_db_findrdataset(db, node, ver, dns_rdatatype_ns,
1062 0, 0, &nsset, NULL);
1063 if (dns_rdataset_isassociated(&nsset)) {
1064 if (ttlp != NULL)
1065 *ttlp = nsset.ttl;
1066 dns_rdataset_disassociate(&nsset);
1067 }
1068
994e6569 1069 return ((result == ISC_R_SUCCESS));
3a14450d
MK
1070}
1071
1072/*%
994e6569
OS
1073 * Return true if version 'ver' of database 'db' contains a DNAME RRset at
1074 * 'node'; return false otherwise.
3a14450d 1075 */
994e6569 1076static bool
3a14450d
MK
1077has_dname(dns_db_t *db, dns_dbversion_t *ver, dns_dbnode_t *node) {
1078 dns_rdataset_t dnameset;
1079 isc_result_t result;
1080
1081 dns_rdataset_init(&dnameset);
1082 result = dns_db_findrdataset(db, node, ver, dns_rdatatype_dname, 0, 0,
1083 &dnameset, NULL);
1084 if (dns_rdataset_isassociated(&dnameset)) {
1085 dns_rdataset_disassociate(&dnameset);
1086 }
1087
994e6569 1088 return ((result == ISC_R_SUCCESS));
3a14450d
MK
1089}
1090
ab023a65 1091/*%
6098d364 1092 * Signs all records at a name.
2d478b70 1093 */
f104fbbe 1094static void
c9d7e543 1095signname(dns_dbnode_t *node, dns_name_t *name) {
f104fbbe 1096 isc_result_t result;
abce4875 1097 dns_rdataset_t rdataset;
f104fbbe 1098 dns_rdatasetiter_t *rdsiter;
994e6569 1099 bool isdelegation = false;
30b41c20 1100 dns_diff_t del, add;
87647e12 1101 char namestr[DNS_NAME_FORMATSIZE];
2e9fe863 1102
6098d364 1103 dns_rdataset_init(&rdataset);
87647e12
BW
1104 dns_name_format(name, namestr, sizeof(namestr));
1105
b4d8cb6b 1106 /*
05a1fd60 1107 * Determine if this is a delegation point.
b4d8cb6b 1108 */
ad127d83 1109 if (is_delegation(gdb, gversion, gorigin, name, node, NULL))
994e6569 1110 isdelegation = true;
b4d8cb6b 1111
b4d8cb6b
BW
1112 /*
1113 * Now iterate through the rdatasets.
1114 */
30b41c20
MA
1115 dns_diff_init(mctx, &del);
1116 dns_diff_init(mctx, &add);
f104fbbe 1117 rdsiter = NULL;
c9d7e543 1118 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
f104fbbe
BW
1119 check_result(result, "dns_db_allrdatasets()");
1120 result = dns_rdatasetiter_first(rdsiter);
1121 while (result == ISC_R_SUCCESS) {
1122 dns_rdatasetiter_current(rdsiter, &rdataset);
b20de477 1123
cc3aafe7 1124 /* If this is a RRSIG set, skip it. */
93d6dfaf 1125 if (rdataset.type == dns_rdatatype_rrsig)
bf5582ad 1126 goto skip;
b20de477 1127
bf5582ad
BW
1128 /*
1129 * If this name is a delegation point, skip all records
93d6dfaf 1130 * except NSEC and DS sets. Otherwise check that there
4411b9b6 1131 * isn't a DS record.
bf5582ad
BW
1132 */
1133 if (isdelegation) {
6098d364 1134 if (rdataset.type != nsec_datatype &&
0b09763c 1135 rdataset.type != dns_rdatatype_ds)
abce4875 1136 goto skip;
4411b9b6
MG
1137 } else if (rdataset.type == dns_rdatatype_ds) {
1138 char namebuf[DNS_NAME_FORMATSIZE];
1139 dns_name_format(name, namebuf, sizeof(namebuf));
1140 fatal("'%s': found DS RRset without NS RRset\n",
1141 namebuf);
49f7148b 1142 }
49f7148b 1143
30b41c20 1144 signset(&del, &add, node, name, &rdataset);
b20de477 1145
bf5582ad 1146 skip:
f104fbbe
BW
1147 dns_rdataset_disassociate(&rdataset);
1148 result = dns_rdatasetiter_next(rdsiter);
1149 }
87647e12 1150 if (result != ISC_R_NOMORE)
6a285c81 1151 fatal("rdataset iteration for name '%s' failed: %s",
4fc4813e 1152 namestr, isc_result_totext(result));
87647e12 1153
f104fbbe 1154 dns_rdatasetiter_destroy(&rdsiter);
b4d8cb6b 1155
30b41c20
MA
1156 result = dns_diff_applysilently(&del, gdb, gversion);
1157 if (result != ISC_R_SUCCESS)
1158 fatal("failed to delete SIGs at node '%s': %s",
1159 namestr, isc_result_totext(result));
1160
1161 result = dns_diff_applysilently(&add, gdb, gversion);
87647e12 1162 if (result != ISC_R_SUCCESS)
7dec36c2
BW
1163 fatal("failed to add SIGs at node '%s': %s",
1164 namestr, isc_result_totext(result));
87647e12 1165
30b41c20
MA
1166 dns_diff_clear(&del);
1167 dns_diff_clear(&add);
f104fbbe
BW
1168}
1169
832ab79d
MA
1170/*
1171 * See if the node contains any non RRSIG/NSEC records and report to
1172 * caller. Clean out extranous RRSIG records for node.
1173 */
994e6569 1174static inline bool
c9d7e543 1175active_node(dns_dbnode_t *node) {
79794472
MA
1176 dns_rdatasetiter_t *rdsiter = NULL;
1177 dns_rdatasetiter_t *rdsiter2 = NULL;
994e6569 1178 bool active = false;
f104fbbe
BW
1179 isc_result_t result;
1180 dns_rdataset_t rdataset;
79794472
MA
1181 dns_rdatatype_t type;
1182 dns_rdatatype_t covers;
994e6569 1183 bool found;
f104fbbe
BW
1184
1185 dns_rdataset_init(&rdataset);
c9d7e543 1186 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
f104fbbe
BW
1187 check_result(result, "dns_db_allrdatasets()");
1188 result = dns_rdatasetiter_first(rdsiter);
1189 while (result == ISC_R_SUCCESS) {
1190 dns_rdatasetiter_current(rdsiter, &rdataset);
5e4346a4 1191 if (rdataset.type != dns_rdatatype_nsec &&
6098d364 1192 rdataset.type != dns_rdatatype_nsec3 &&
5e4346a4 1193 rdataset.type != dns_rdatatype_rrsig)
994e6569 1194 active = true;
f104fbbe
BW
1195 dns_rdataset_disassociate(&rdataset);
1196 if (!active)
1197 result = dns_rdatasetiter_next(rdsiter);
1198 else
41959049 1199 result = ISC_R_NOMORE;
f104fbbe 1200 }
41959049 1201 if (result != ISC_R_NOMORE)
6a285c81
BW
1202 fatal("rdataset iteration failed: %s",
1203 isc_result_totext(result));
f104fbbe 1204
6098d364 1205 if (!active && nsec_datatype == dns_rdatatype_nsec) {
fb827ed6 1206 /*%
79794472 1207 * The node is empty of everything but NSEC / RRSIG records.
f104fbbe 1208 */
5e4346a4
MA
1209 for (result = dns_rdatasetiter_first(rdsiter);
1210 result == ISC_R_SUCCESS;
1211 result = dns_rdatasetiter_next(rdsiter)) {
1212 dns_rdatasetiter_current(rdsiter, &rdataset);
79794472
MA
1213 result = dns_db_deleterdataset(gdb, node, gversion,
1214 rdataset.type,
1215 rdataset.covers);
1216 check_result(result, "dns_db_deleterdataset()");
1217 dns_rdataset_disassociate(&rdataset);
1218 }
1219 if (result != ISC_R_NOMORE)
1220 fatal("rdataset iteration failed: %s",
1221 isc_result_totext(result));
1222 } else {
177bcb46 1223 /*
79794472
MA
1224 * Delete RRSIGs for types that no longer exist.
1225 */
1226 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter2);
1227 check_result(result, "dns_db_allrdatasets()");
1228 for (result = dns_rdatasetiter_first(rdsiter);
1229 result == ISC_R_SUCCESS;
1230 result = dns_rdatasetiter_next(rdsiter)) {
1231 dns_rdatasetiter_current(rdsiter, &rdataset);
1232 type = rdataset.type;
1233 covers = rdataset.covers;
1234 dns_rdataset_disassociate(&rdataset);
4d0e2cf9
MA
1235 /*
1236 * Delete the NSEC chain if we are signing with
1237 * NSEC3.
1238 */
1239 if (nsec_datatype == dns_rdatatype_nsec3 &&
1240 (type == dns_rdatatype_nsec ||
1241 covers == dns_rdatatype_nsec)) {
1242 result = dns_db_deleterdataset(gdb, node,
1243 gversion, type,
1244 covers);
1245 check_result(result,
1246 "dns_db_deleterdataset(nsec/rrsig)");
1247 continue;
1248 }
79794472
MA
1249 if (type != dns_rdatatype_rrsig)
1250 continue;
994e6569 1251 found = false;
79794472
MA
1252 for (result = dns_rdatasetiter_first(rdsiter2);
1253 !found && result == ISC_R_SUCCESS;
1254 result = dns_rdatasetiter_next(rdsiter2)) {
1255 dns_rdatasetiter_current(rdsiter2, &rdataset);
1256 if (rdataset.type == covers)
994e6569 1257 found = true;
79794472
MA
1258 dns_rdataset_disassociate(&rdataset);
1259 }
1260 if (!found) {
1261 if (result != ISC_R_NOMORE)
1262 fatal("rdataset iteration failed: %s",
1263 isc_result_totext(result));
5e4346a4
MA
1264 result = dns_db_deleterdataset(gdb, node,
1265 gversion, type,
1266 covers);
5e4346a4
MA
1267 check_result(result,
1268 "dns_db_deleterdataset(rrsig)");
79794472
MA
1269 } else if (result != ISC_R_NOMORE &&
1270 result != ISC_R_SUCCESS)
1271 fatal("rdataset iteration failed: %s",
1272 isc_result_totext(result));
5e4346a4
MA
1273 }
1274 if (result != ISC_R_NOMORE)
1275 fatal("rdataset iteration failed: %s",
1276 isc_result_totext(result));
79794472 1277 dns_rdatasetiter_destroy(&rdsiter2);
f104fbbe 1278 }
5e4346a4 1279 dns_rdatasetiter_destroy(&rdsiter);
f104fbbe
BW
1280
1281 return (active);
1282}
1283
ab023a65 1284/*%
eab9975b 1285 * Extracts the minimum TTL from the SOA record, and the SOA record's TTL.
2d478b70 1286 */
eab9975b
EH
1287static void
1288get_soa_ttls(void) {
201b17e5 1289 dns_rdataset_t soaset;
417bd845
BW
1290 dns_fixedname_t fname;
1291 dns_name_t *name;
417bd845 1292 isc_result_t result;
b0c15bd9 1293 dns_rdata_t rdata = DNS_RDATA_INIT;
f104fbbe 1294
4df4a8e7 1295 name = dns_fixedname_initname(&fname);
201b17e5 1296 dns_rdataset_init(&soaset);
c9d7e543 1297 result = dns_db_find(gdb, gorigin, gversion, dns_rdatatype_soa,
201b17e5 1298 0, 0, NULL, name, &soaset, NULL);
87647e12
BW
1299 if (result != ISC_R_SUCCESS)
1300 fatal("failed to find an SOA at the zone apex: %s",
1301 isc_result_totext(result));
1302
b0c15bd9
MA
1303 result = dns_rdataset_first(&soaset);
1304 check_result(result, "dns_rdataset_first");
1305 dns_rdataset_current(&soaset, &rdata);
eab9975b
EH
1306 zone_soa_min_ttl = dns_soa_getminimum(&rdata);
1307 soa_ttl = soaset.ttl;
35f6a21f
EH
1308 if (set_maxttl) {
1309 zone_soa_min_ttl = ISC_MIN(zone_soa_min_ttl, maxttl);
1310 soa_ttl = ISC_MIN(soa_ttl, maxttl);
1311 }
201b17e5 1312 dns_rdataset_disassociate(&soaset);
417bd845
BW
1313}
1314
6ed53e59
DH
1315/*%
1316 * Increment (or set if nonzero) the SOA serial
1317 */
1318static isc_result_t
cb6a185c 1319setsoaserial(uint32_t serial, dns_updatemethod_t method) {
6ed53e59
DH
1320 isc_result_t result;
1321 dns_dbnode_t *node = NULL;
1322 dns_rdataset_t rdataset;
1323 dns_rdata_t rdata = DNS_RDATA_INIT;
cb6a185c 1324 uint32_t old_serial, new_serial;
6ed53e59
DH
1325
1326 result = dns_db_getoriginnode(gdb, &node);
1327 if (result != ISC_R_SUCCESS)
1328 return result;
1329
1330 dns_rdataset_init(&rdataset);
1331
1332 result = dns_db_findrdataset(gdb, node, gversion,
1333 dns_rdatatype_soa, 0,
1334 0, &rdataset, NULL);
1335 if (result != ISC_R_SUCCESS)
1336 goto cleanup;
1337
1338 result = dns_rdataset_first(&rdataset);
1339 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1340
1341 dns_rdataset_current(&rdataset, &rdata);
1342
1343 old_serial = dns_soa_getserial(&rdata);
1344
b4ba66ba
EH
1345 if (method == dns_updatemethod_date ||
1346 method == dns_updatemethod_unixtime) {
1347 new_serial = dns_update_soaserial(old_serial, method);
1348 } else if (serial != 0 || method == dns_updatemethod_none) {
6ed53e59
DH
1349 /* Set SOA serial to the value provided. */
1350 new_serial = serial;
1351 } else {
1352 /* Increment SOA serial using RFC 1982 arithmetics */
1353 new_serial = (old_serial + 1) & 0xFFFFFFFF;
1354 if (new_serial == 0)
1355 new_serial = 1;
1356 }
1357
1358 /* If the new serial is not likely to cause a zone transfer
1359 * (a/ixfr) from servers having the old serial, warn the user.
1360 *
1361 * RFC1982 section 7 defines the maximum increment to be
1362 * (2^(32-1))-1. Using u_int32_t arithmetic, we can do a single
1363 * comparison. (5 - 6 == (2^32)-1, not negative-one)
1364 */
1365 if (new_serial == old_serial ||
1366 (new_serial - old_serial) > 0x7fffffffU)
1367 fprintf(stderr, "%s: warning: Serial number not advanced, "
1368 "zone may not transfer\n", program);
1369
1370 dns_soa_setserial(new_serial, &rdata);
1371
1372 result = dns_db_deleterdataset(gdb, node, gversion,
1373 dns_rdatatype_soa, 0);
1374 check_result(result, "dns_db_deleterdataset");
1375 if (result != ISC_R_SUCCESS)
1376 goto cleanup;
1377
1378 result = dns_db_addrdataset(gdb, node, gversion,
1379 0, &rdataset, 0, NULL);
1380 check_result(result, "dns_db_addrdataset");
1381 if (result != ISC_R_SUCCESS)
1382 goto cleanup;
1383
1384cleanup:
1385 dns_rdataset_disassociate(&rdataset);
1386 if (node != NULL)
1387 dns_db_detachnode(gdb, &node);
1388 dns_rdata_reset(&rdata);
1389
1390 return (result);
1391}
1392
ab023a65 1393/*%
cc3aafe7 1394 * Delete any RRSIG records at a node.
c9d7e543 1395 */
e8892697 1396static void
af669cb4 1397cleannode(dns_db_t *db, dns_dbversion_t *dbversion, dns_dbnode_t *node) {
e8892697
BW
1398 dns_rdatasetiter_t *rdsiter = NULL;
1399 dns_rdataset_t set;
1400 isc_result_t result, dresult;
1401
2534a73a 1402 if (outputformat != dns_masterformat_text || !disable_zone_check)
2dafa707
MA
1403 return;
1404
e8892697 1405 dns_rdataset_init(&set);
af669cb4 1406 result = dns_db_allrdatasets(db, node, dbversion, 0, &rdsiter);
e8892697
BW
1407 check_result(result, "dns_db_allrdatasets");
1408 result = dns_rdatasetiter_first(rdsiter);
1409 while (result == ISC_R_SUCCESS) {
994e6569 1410 bool destroy = false;
e8892697
BW
1411 dns_rdatatype_t covers = 0;
1412 dns_rdatasetiter_current(rdsiter, &set);
93d6dfaf 1413 if (set.type == dns_rdatatype_rrsig) {
e8892697 1414 covers = set.covers;
994e6569 1415 destroy = true;
e8892697
BW
1416 }
1417 dns_rdataset_disassociate(&set);
1418 result = dns_rdatasetiter_next(rdsiter);
1419 if (destroy) {
af669cb4 1420 dresult = dns_db_deleterdataset(db, node, dbversion,
93d6dfaf 1421 dns_rdatatype_rrsig,
e8892697 1422 covers);
c9d7e543 1423 check_result(dresult, "dns_db_deleterdataset");
e8892697
BW
1424 }
1425 }
1426 if (result != ISC_R_NOMORE)
1427 fatal("rdataset iteration failed: %s",
1428 isc_result_totext(result));
1429 dns_rdatasetiter_destroy(&rdsiter);
1430}
1431
ab023a65 1432/*%
c9d7e543 1433 * Set up the iterator and global state before starting the tasks.
417bd845
BW
1434 */
1435static void
aeab5415 1436presign(void) {
c9d7e543 1437 isc_result_t result;
417bd845 1438
c9d7e543 1439 gdbiter = NULL;
6098d364 1440 result = dns_db_createiterator(gdb, 0, &gdbiter);
c9d7e543 1441 check_result(result, "dns_db_createiterator()");
c9d7e543
BW
1442}
1443
ab023a65 1444/*%
c9d7e543
BW
1445 * Clean up the iterator and global state after the tasks complete.
1446 */
1447static void
1448postsign(void) {
c9d7e543
BW
1449 dns_dbiterator_destroy(&gdbiter);
1450}
1451
2a35dc09
MA
1452/*%
1453 * Sign the apex of the zone.
6098d364
MA
1454 * Note the origin may not be the first node if there are out of zone
1455 * records.
2a35dc09
MA
1456 */
1457static void
1458signapex(void) {
1459 dns_dbnode_t *node = NULL;
1460 dns_fixedname_t fixed;
1461 dns_name_t *name;
1462 isc_result_t result;
177bcb46 1463
4df4a8e7 1464 name = dns_fixedname_initname(&fixed);
6098d364
MA
1465 result = dns_dbiterator_seek(gdbiter, gorigin);
1466 check_result(result, "dns_dbiterator_seek()");
2a35dc09 1467 result = dns_dbiterator_current(gdbiter, &node, name);
011d0b7d 1468 check_dns_dbiterator_current(result);
2a35dc09
MA
1469 signname(node, name);
1470 dumpnode(name, node);
1471 cleannode(gdb, gversion, node);
1472 dns_db_detachnode(gdb, &node);
6098d364 1473 result = dns_dbiterator_first(gdbiter);
2a35dc09 1474 if (result == ISC_R_NOMORE)
994e6569 1475 finished = true;
2a35dc09
MA
1476 else if (result != ISC_R_SUCCESS)
1477 fatal("failure iterating database: %s",
1478 isc_result_totext(result));
1479}
1480
ab023a65 1481/*%
c9d7e543
BW
1482 * Assigns a node to a worker thread. This is protected by the master task's
1483 * lock.
1484 */
1485static void
1486assignwork(isc_task_t *task, isc_task_t *worker) {
e57e32e5
BW
1487 dns_fixedname_t *fname;
1488 dns_name_t *name;
c9d7e543
BW
1489 dns_dbnode_t *node;
1490 sevent_t *sevent;
93d6dfaf 1491 dns_rdataset_t nsec;
994e6569 1492 bool found;
c9d7e543 1493 isc_result_t result;
6098d364
MA
1494 static dns_name_t *zonecut = NULL; /* Protected by namelock. */
1495 static dns_fixedname_t fzonecut; /* Protected by namelock. */
d87ad693 1496 static unsigned int ended = 0; /* Protected by namelock. */
c9d7e543 1497
e57e32e5
BW
1498 if (shuttingdown)
1499 return;
1500
d87ad693 1501 LOCK(&namelock);
e57e32e5 1502 if (finished) {
d87ad693
MA
1503 ended++;
1504 if (ended == ntasks) {
c9d7e543
BW
1505 isc_task_detach(&task);
1506 isc_app_shutdown();
1507 }
d87ad693 1508 goto unlock;
af9cc9f1 1509 }
e57e32e5
BW
1510
1511 fname = isc_mem_get(mctx, sizeof(dns_fixedname_t));
1512 if (fname == NULL)
1513 fatal("out of memory");
4df4a8e7 1514 name = dns_fixedname_initname(fname);
e57e32e5 1515 node = NULL;
994e6569 1516 found = false;
e57e32e5
BW
1517 while (!found) {
1518 result = dns_dbiterator_current(gdbiter, &node, name);
011d0b7d 1519 check_dns_dbiterator_current(result);
6098d364
MA
1520 /*
1521 * The origin was handled by signapex().
1522 */
1523 if (dns_name_equal(name, gorigin)) {
1524 dns_db_detachnode(gdb, &node);
1525 goto next;
1526 }
1527 /*
1528 * Sort the zone data from the glue and out-of-zone data.
1529 * For NSEC zones nodes with zone data have NSEC records.
1530 * For NSEC3 zones the NSEC3 nodes are zone data but
1531 * outside of the zone name space. For the rest we need
1532 * to track the bottom of zone cuts.
1533 * Nodes which don't need to be signed are dumped here.
1534 */
93d6dfaf 1535 dns_rdataset_init(&nsec);
e57e32e5 1536 result = dns_db_findrdataset(gdb, node, gversion,
6098d364 1537 nsec_datatype, 0, 0,
93d6dfaf 1538 &nsec, NULL);
93d6dfaf
MA
1539 if (dns_rdataset_isassociated(&nsec))
1540 dns_rdataset_disassociate(&nsec);
6098d364 1541 if (result == ISC_R_SUCCESS) {
994e6569 1542 found = true;
6098d364
MA
1543 } else if (nsec_datatype == dns_rdatatype_nsec3) {
1544 if (dns_name_issubdomain(name, gorigin) &&
1545 (zonecut == NULL ||
1546 !dns_name_issubdomain(name, zonecut))) {
7be900a9
MA
1547 if (is_delegation(gdb, gversion, gorigin,
1548 name, node, NULL))
1549 {
1550 zonecut = savezonecut(&fzonecut, name);
6098d364 1551 if (!OPTOUT(nsec3flags) ||
994e6569
OS
1552 secure(name, node)) {
1553 found = true;
1554 }
75c0d85f 1555 } else if (has_dname(gdb, gversion, node)) {
7be900a9 1556 zonecut = savezonecut(&fzonecut, name);
994e6569 1557 found = true;
75c0d85f 1558 } else {
994e6569 1559 found = true;
75c0d85f 1560 }
6098d364
MA
1561 }
1562 }
1563
1564 if (!found) {
1565 dumpnode(name, node);
8d77066b 1566 dns_db_detachnode(gdb, &node);
6098d364 1567 }
e57e32e5 1568
6098d364 1569 next:
e57e32e5 1570 result = dns_dbiterator_next(gdbiter);
6c62c34a 1571 if (result == ISC_R_NOMORE) {
994e6569 1572 finished = true;
6c62c34a
BW
1573 break;
1574 } else if (result != ISC_R_SUCCESS)
e57e32e5
BW
1575 fatal("failure iterating database: %s",
1576 isc_result_totext(result));
e57e32e5 1577 }
8d77066b 1578 if (!found) {
d87ad693
MA
1579 ended++;
1580 if (ended == ntasks) {
8d77066b
MA
1581 isc_task_detach(&task);
1582 isc_app_shutdown();
1583 }
1584 isc_mem_put(mctx, fname, sizeof(dns_fixedname_t));
d87ad693 1585 goto unlock;
8d77066b 1586 }
c9d7e543
BW
1587 sevent = (sevent_t *)
1588 isc_event_allocate(mctx, task, SIGNER_EVENT_WORK,
1589 sign, NULL, sizeof(sevent_t));
1590 if (sevent == NULL)
1591 fatal("failed to allocate event\n");
1592
1593 sevent->node = node;
1594 sevent->fname = fname;
42b48d11 1595 isc_task_send(worker, ISC_EVENT_PTR(&sevent));
d87ad693
MA
1596 unlock:
1597 UNLOCK(&namelock);
c9d7e543
BW
1598}
1599
ab023a65 1600/*%
c9d7e543
BW
1601 * Start a worker task
1602 */
1603static void
1604startworker(isc_task_t *task, isc_event_t *event) {
1605 isc_task_t *worker;
1606
1607 worker = (isc_task_t *)event->ev_arg;
1608 assignwork(task, worker);
1609 isc_event_free(&event);
1610}
1611
ab023a65 1612/*%
c9d7e543
BW
1613 * Write a node to the output file, and restart the worker task.
1614 */
1615static void
1616writenode(isc_task_t *task, isc_event_t *event) {
c9d7e543
BW
1617 isc_task_t *worker;
1618 sevent_t *sevent = (sevent_t *)event;
1619
c9d7e543 1620 worker = (isc_task_t *)event->ev_sender;
05a1fd60 1621 dumpnode(dns_fixedname_name(sevent->fname), sevent->node);
c9d7e543
BW
1622 cleannode(gdb, gversion, sevent->node);
1623 dns_db_detachnode(gdb, &sevent->node);
1624 isc_mem_put(mctx, sevent->fname, sizeof(dns_fixedname_t));
1625 assignwork(task, worker);
1626 isc_event_free(&event);
1627}
1628
ab023a65 1629/*%
e57e32e5 1630 * Sign a database node.
c9d7e543
BW
1631 */
1632static void
1633sign(isc_task_t *task, isc_event_t *event) {
e57e32e5 1634 dns_fixedname_t *fname;
c9d7e543
BW
1635 dns_dbnode_t *node;
1636 sevent_t *sevent, *wevent;
c9d7e543
BW
1637
1638 sevent = (sevent_t *)event;
1639 node = sevent->node;
1640 fname = sevent->fname;
c9d7e543
BW
1641 isc_event_free(&event);
1642
c9d7e543
BW
1643 signname(node, dns_fixedname_name(fname));
1644 wevent = (sevent_t *)
1645 isc_event_allocate(mctx, task, SIGNER_EVENT_WRITE,
1646 writenode, NULL, sizeof(sevent_t));
1647 if (wevent == NULL)
1648 fatal("failed to allocate event\n");
1649 wevent->node = node;
1650 wevent->fname = fname;
42b48d11 1651 isc_task_send(master, ISC_EVENT_PTR(&wevent));
bf5582ad
BW
1652}
1653
6098d364
MA
1654/*%
1655 * Update / remove the DS RRset. Preserve RRSIG(DS) if possible.
1656 */
1657static void
cb6a185c 1658add_ds(dns_name_t *name, dns_dbnode_t *node, uint32_t nsttl) {
6098d364
MA
1659 dns_rdataset_t dsset;
1660 dns_rdataset_t sigdsset;
1661 isc_result_t result;
1662
1663 dns_rdataset_init(&dsset);
1664 dns_rdataset_init(&sigdsset);
1665 result = dns_db_findrdataset(gdb, node, gversion,
1666 dns_rdatatype_ds,
1667 0, 0, &dsset, &sigdsset);
1668 if (result == ISC_R_SUCCESS) {
1669 dns_rdataset_disassociate(&dsset);
1670 result = dns_db_deleterdataset(gdb, node, gversion,
1671 dns_rdatatype_ds, 0);
1672 check_result(result, "dns_db_deleterdataset");
1673 }
553ead32 1674
6098d364
MA
1675 result = loadds(name, nsttl, &dsset);
1676 if (result == ISC_R_SUCCESS) {
1677 result = dns_db_addrdataset(gdb, node, gversion, 0,
1678 &dsset, 0, NULL);
1679 check_result(result, "dns_db_addrdataset");
1680 dns_rdataset_disassociate(&dsset);
1681 if (dns_rdataset_isassociated(&sigdsset))
1682 dns_rdataset_disassociate(&sigdsset);
1683 } else if (dns_rdataset_isassociated(&sigdsset)) {
1684 result = dns_db_deleterdataset(gdb, node, gversion,
1685 dns_rdatatype_rrsig,
1686 dns_rdatatype_ds);
1687 check_result(result, "dns_db_deleterdataset");
1688 dns_rdataset_disassociate(&sigdsset);
1689 }
1690}
1691
675cc809
MA
1692/*
1693 * Remove records of the given type and their signatures.
1694 */
1695static void
4b3d727d 1696remove_records(dns_dbnode_t *node, dns_rdatatype_t which,
994e6569 1697 bool checknsec)
4b3d727d 1698{
675cc809
MA
1699 isc_result_t result;
1700 dns_rdatatype_t type, covers;
1701 dns_rdatasetiter_t *rdsiter = NULL;
1702 dns_rdataset_t rdataset;
1703
1704 dns_rdataset_init(&rdataset);
1705
1706 /*
1707 * Delete any records of the given type at the apex.
1708 */
1709 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
1710 check_result(result, "dns_db_allrdatasets()");
1711 for (result = dns_rdatasetiter_first(rdsiter);
1712 result == ISC_R_SUCCESS;
1713 result = dns_rdatasetiter_next(rdsiter)) {
1714 dns_rdatasetiter_current(rdsiter, &rdataset);
1715 type = rdataset.type;
1716 covers = rdataset.covers;
1717 dns_rdataset_disassociate(&rdataset);
1718 if (type == which || covers == which) {
4b3d727d
EH
1719 if (which == dns_rdatatype_nsec &&
1720 checknsec && !update_chain)
675cc809
MA
1721 fatal("Zone contains NSEC records. Use -u "
1722 "to update to NSEC3.");
4b3d727d
EH
1723 if (which == dns_rdatatype_nsec3param &&
1724 checknsec && !update_chain)
675cc809
MA
1725 fatal("Zone contains NSEC3 chains. Use -u "
1726 "to update to NSEC.");
1727 result = dns_db_deleterdataset(gdb, node, gversion,
1728 type, covers);
1729 check_result(result, "dns_db_deleterdataset()");
1730 continue;
1731 }
1732 }
1733 dns_rdatasetiter_destroy(&rdsiter);
1734}
1735
4b3d727d 1736/*
eeb13c7c
EH
1737 * Remove signatures covering the given type. If type == 0,
1738 * then remove all signatures, unless this is a delegation, in
1739 * which case remove all signatures except for DS or nsec_datatype
4b3d727d
EH
1740 */
1741static void
994e6569 1742remove_sigs(dns_dbnode_t *node, bool delegation,
eeb13c7c
EH
1743 dns_rdatatype_t which)
1744{
4b3d727d
EH
1745 isc_result_t result;
1746 dns_rdatatype_t type, covers;
1747 dns_rdatasetiter_t *rdsiter = NULL;
1748 dns_rdataset_t rdataset;
1749
1750 dns_rdataset_init(&rdataset);
1751 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
1752 check_result(result, "dns_db_allrdatasets()");
1753 for (result = dns_rdatasetiter_first(rdsiter);
1754 result == ISC_R_SUCCESS;
1755 result = dns_rdatasetiter_next(rdsiter)) {
1756 dns_rdatasetiter_current(rdsiter, &rdataset);
1757 type = rdataset.type;
1758 covers = rdataset.covers;
1759 dns_rdataset_disassociate(&rdataset);
1760
eeb13c7c 1761 if (type != dns_rdatatype_rrsig)
4b3d727d 1762 continue;
eeb13c7c
EH
1763
1764 if (which == 0 && delegation &&
1765 (dns_rdatatype_atparent(covers) ||
1766 (nsec_datatype == dns_rdatatype_nsec &&
1767 covers == nsec_datatype)))
1768 continue;
1769
1770 if (which != 0 && covers != which)
1771 continue;
1772
1773 result = dns_db_deleterdataset(gdb, node, gversion,
1774 type, covers);
1775 check_result(result, "dns_db_deleterdataset()");
4b3d727d
EH
1776 }
1777 dns_rdatasetiter_destroy(&rdsiter);
1778}
1779
ab023a65 1780/*%
2162c1ed 1781 * Generate NSEC records for the zone and remove NSEC3/NSEC3PARAM records.
e57e32e5
BW
1782 */
1783static void
93d6dfaf 1784nsecify(void) {
e57e32e5
BW
1785 dns_dbiterator_t *dbiter = NULL;
1786 dns_dbnode_t *node = NULL, *nextnode = NULL;
1787 dns_fixedname_t fname, fnextname, fzonecut;
1788 dns_name_t *name, *nextname, *zonecut;
4d0e2cf9
MA
1789 dns_rdataset_t rdataset;
1790 dns_rdatasetiter_t *rdsiter = NULL;
1791 dns_rdatatype_t type, covers;
994e6569 1792 bool done = false;
e57e32e5 1793 isc_result_t result;
cb6a185c 1794 uint32_t nsttl = 0;
e57e32e5 1795
4d0e2cf9 1796 dns_rdataset_init(&rdataset);
4df4a8e7
MK
1797 name = dns_fixedname_initname(&fname);
1798 nextname = dns_fixedname_initname(&fnextname);
e57e32e5
BW
1799 zonecut = NULL;
1800
4d0e2cf9
MA
1801 /*
1802 * Remove any NSEC3 chains.
1803 */
1804 result = dns_db_createiterator(gdb, DNS_DB_NSEC3ONLY, &dbiter);
1805 check_result(result, "dns_db_createiterator()");
1806 for (result = dns_dbiterator_first(dbiter);
1807 result == ISC_R_SUCCESS;
1808 result = dns_dbiterator_next(dbiter)) {
1809 result = dns_dbiterator_current(dbiter, &node, name);
1810 check_dns_dbiterator_current(result);
1811 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
1812 check_result(result, "dns_db_allrdatasets()");
1813 for (result = dns_rdatasetiter_first(rdsiter);
1814 result == ISC_R_SUCCESS;
1815 result = dns_rdatasetiter_next(rdsiter)) {
1816 dns_rdatasetiter_current(rdsiter, &rdataset);
1817 type = rdataset.type;
1818 covers = rdataset.covers;
1819 dns_rdataset_disassociate(&rdataset);
fb596cc9
EH
1820 result = dns_db_deleterdataset(gdb, node, gversion,
1821 type, covers);
4d0e2cf9
MA
1822 check_result(result,
1823 "dns_db_deleterdataset(nsec3param/rrsig)");
1824 }
1825 dns_rdatasetiter_destroy(&rdsiter);
1826 dns_db_detachnode(gdb, &node);
1827 }
1828 dns_dbiterator_destroy(&dbiter);
d48690af 1829
6098d364 1830 result = dns_db_createiterator(gdb, DNS_DB_NONSEC3, &dbiter);
e57e32e5
BW
1831 check_result(result, "dns_db_createiterator()");
1832
1833 result = dns_dbiterator_first(dbiter);
1834 check_result(result, "dns_dbiterator_first()");
1835
1836 while (!done) {
011d0b7d
MA
1837 result = dns_dbiterator_current(dbiter, &node, name);
1838 check_dns_dbiterator_current(result);
675cc809
MA
1839 /*
1840 * Skip out-of-zone records.
1841 */
1842 if (!dns_name_issubdomain(name, gorigin)) {
1843 result = dns_dbiterator_next(dbiter);
1844 if (result == ISC_R_NOMORE)
994e6569 1845 done = true;
675cc809
MA
1846 else
1847 check_result(result, "dns_dbiterator_next()");
1848 dns_db_detachnode(gdb, &node);
1849 continue;
6e13ffa2 1850 }
675cc809 1851
832ab79d 1852 if (dns_name_equal(name, gorigin)) {
4b3d727d 1853 remove_records(node, dns_rdatatype_nsec3param,
994e6569 1854 true);
832ab79d
MA
1855 /* Clean old rrsigs at apex. */
1856 (void)active_node(node);
1857 }
675cc809 1858
ad127d83 1859 if (is_delegation(gdb, gversion, gorigin, name, node, &nsttl)) {
7be900a9 1860 zonecut = savezonecut(&fzonecut, name);
994e6569 1861 remove_sigs(node, true, 0);
6098d364
MA
1862 if (generateds)
1863 add_ds(name, node, nsttl);
75c0d85f 1864 } else if (has_dname(gdb, gversion, node)) {
7be900a9 1865 zonecut = savezonecut(&fzonecut, name);
e57e32e5 1866 }
4b3d727d 1867
e57e32e5
BW
1868 result = dns_dbiterator_next(dbiter);
1869 nextnode = NULL;
1870 while (result == ISC_R_SUCCESS) {
994e6569 1871 bool active = false;
e57e32e5
BW
1872 result = dns_dbiterator_current(dbiter, &nextnode,
1873 nextname);
011d0b7d 1874 check_dns_dbiterator_current(result);
e57e32e5
BW
1875 active = active_node(nextnode);
1876 if (!active) {
1877 dns_db_detachnode(gdb, &nextnode);
1878 result = dns_dbiterator_next(dbiter);
1879 continue;
1880 }
e57e32e5
BW
1881 if (!dns_name_issubdomain(nextname, gorigin) ||
1882 (zonecut != NULL &&
1883 dns_name_issubdomain(nextname, zonecut)))
1884 {
994e6569 1885 remove_sigs(nextnode, false, 0);
4b3d727d 1886 remove_records(nextnode, dns_rdatatype_nsec,
994e6569 1887 false);
e57e32e5
BW
1888 dns_db_detachnode(gdb, &nextnode);
1889 result = dns_dbiterator_next(dbiter);
1890 continue;
1891 }
1892 dns_db_detachnode(gdb, &nextnode);
1893 break;
1894 }
1895 if (result == ISC_R_NOMORE) {
1896 dns_name_clone(gorigin, nextname);
994e6569 1897 done = true;
e57e32e5
BW
1898 } else if (result != ISC_R_SUCCESS)
1899 fatal("iterating through the database failed: %s",
1900 isc_result_totext(result));
63d5a6f6 1901 dns_dbiterator_pause(dbiter);
93d6dfaf 1902 result = dns_nsec_build(gdb, gversion, node, nextname,
553ead32 1903 zone_soa_min_ttl);
93d6dfaf 1904 check_result(result, "dns_nsec_build()");
e57e32e5
BW
1905 dns_db_detachnode(gdb, &node);
1906 }
1907
1908 dns_dbiterator_destroy(&dbiter);
1909}
1910
6098d364 1911static void
11463c0a 1912addnsec3param(const unsigned char *salt, size_t salt_len,
c3c8823f 1913 dns_iterations_t iterations)
6098d364
MA
1914{
1915 dns_dbnode_t *node = NULL;
1916 dns_rdata_nsec3param_t nsec3param;
1917 unsigned char nsec3parambuf[5 + 255];
1918 dns_rdatalist_t rdatalist;
1919 dns_rdataset_t rdataset;
1920 dns_rdata_t rdata = DNS_RDATA_INIT;
1921 isc_buffer_t b;
1922 isc_result_t result;
1923
1924 dns_rdataset_init(&rdataset);
1925
1926 nsec3param.common.rdclass = gclass;
1927 nsec3param.common.rdtype = dns_rdatatype_nsec3param;
1928 ISC_LINK_INIT(&nsec3param.common, link);
1929 nsec3param.mctx = NULL;
1930 nsec3param.flags = 0;
1931 nsec3param.hash = unknownalg ? DNS_NSEC3_UNKNOWNALG : dns_hash_sha1;
1932 nsec3param.iterations = iterations;
11463c0a 1933 nsec3param.salt_length = (unsigned char)salt_len;
6098d364
MA
1934 DE_CONST(salt, nsec3param.salt);
1935
1936 isc_buffer_init(&b, nsec3parambuf, sizeof(nsec3parambuf));
1937 result = dns_rdata_fromstruct(&rdata, gclass,
1938 dns_rdatatype_nsec3param,
1939 &nsec3param, &b);
0874abad 1940 check_result(result, "dns_rdata_fromstruct()");
29d52c00 1941 dns_rdatalist_init(&rdatalist);
6098d364
MA
1942 rdatalist.rdclass = rdata.rdclass;
1943 rdatalist.type = rdata.type;
6098d364
MA
1944 ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
1945 result = dns_rdatalist_tordataset(&rdatalist, &rdataset);
1946 check_result(result, "dns_rdatalist_tordataset()");
1947
994e6569 1948 result = dns_db_findnode(gdb, gorigin, true, &node);
6098d364 1949 check_result(result, "dns_db_find(gorigin)");
011d0b7d
MA
1950
1951 /*
1952 * Delete any current NSEC3PARAM records.
1953 */
1954 result = dns_db_deleterdataset(gdb, node, gversion,
1955 dns_rdatatype_nsec3param, 0);
1956 if (result == DNS_R_UNCHANGED)
1957 result = ISC_R_SUCCESS;
1958 check_result(result, "dddnsec3param: dns_db_deleterdataset()");
1959
6098d364
MA
1960 result = dns_db_addrdataset(gdb, node, gversion, 0, &rdataset,
1961 DNS_DBADD_MERGE, NULL);
1962 if (result == DNS_R_UNCHANGED)
1963 result = ISC_R_SUCCESS;
1964 check_result(result, "addnsec3param: dns_db_addrdataset()");
1965 dns_db_detachnode(gdb, &node);
1966}
1967
1968static void
1969addnsec3(dns_name_t *name, dns_dbnode_t *node,
11463c0a 1970 const unsigned char *salt, size_t salt_len,
6098d364
MA
1971 unsigned int iterations, hashlist_t *hashlist,
1972 dns_ttl_t ttl)
1973{
1974 unsigned char hash[NSEC3_MAX_HASH_LENGTH];
1975 const unsigned char *nexthash;
1976 unsigned char nsec3buffer[DNS_NSEC3_BUFFERSIZE];
1977 dns_fixedname_t hashname;
1978 dns_rdatalist_t rdatalist;
1979 dns_rdataset_t rdataset;
1980 dns_rdata_t rdata = DNS_RDATA_INIT;
1981 isc_result_t result;
1982 dns_dbnode_t *nsec3node = NULL;
1983 char namebuf[DNS_NAME_FORMATSIZE];
11463c0a 1984 size_t hash_len;
6098d364
MA
1985
1986 dns_name_format(name, namebuf, sizeof(namebuf));
1987
1988 dns_fixedname_init(&hashname);
1989 dns_rdataset_init(&rdataset);
1990
1991 dns_name_downcase(name, name, NULL);
11463c0a 1992 result = dns_nsec3_hashname(&hashname, hash, &hash_len,
6098d364 1993 name, gorigin, dns_hash_sha1, iterations,
11463c0a 1994 salt, salt_len);
6098d364
MA
1995 check_result(result, "addnsec3: dns_nsec3_hashname()");
1996 nexthash = hashlist_findnext(hashlist, hash);
1997 result = dns_nsec3_buildrdata(gdb, gversion, node,
1998 unknownalg ?
1999 DNS_NSEC3_UNKNOWNALG : dns_hash_sha1,
2000 nsec3flags, iterations,
11463c0a 2001 salt, salt_len,
6098d364
MA
2002 nexthash, ISC_SHA1_DIGESTLENGTH,
2003 nsec3buffer, &rdata);
2004 check_result(result, "addnsec3: dns_nsec3_buildrdata()");
29d52c00 2005 dns_rdatalist_init(&rdatalist);
6098d364
MA
2006 rdatalist.rdclass = rdata.rdclass;
2007 rdatalist.type = rdata.type;
6098d364 2008 rdatalist.ttl = ttl;
6098d364
MA
2009 ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
2010 result = dns_rdatalist_tordataset(&rdatalist, &rdataset);
2011 check_result(result, "dns_rdatalist_tordataset()");
2012 result = dns_db_findnsec3node(gdb, dns_fixedname_name(&hashname),
994e6569 2013 true, &nsec3node);
6098d364
MA
2014 check_result(result, "addnsec3: dns_db_findnode()");
2015 result = dns_db_addrdataset(gdb, nsec3node, gversion, 0, &rdataset,
2016 0, NULL);
2017 if (result == DNS_R_UNCHANGED)
2018 result = ISC_R_SUCCESS;
2019 check_result(result, "addnsec3: dns_db_addrdataset()");
2020 dns_db_detachnode(gdb, &nsec3node);
2021}
2022
2023/*%
2024 * Clean out NSEC3 record and RRSIG(NSEC3) that are not in the hash list.
2025 *
2026 * Extract the hash from the first label of 'name' then see if it
2027 * is in hashlist. If 'name' is not in the hashlist then delete the
2028 * any NSEC3 records which have the same parameters as the chain we
2029 * are building.
2030 *
2361003a 2031 * XXXMPA Should we also check that it of the form &lt;hash&gt;.&lt;origin&gt;?
6098d364
MA
2032 */
2033static void
2034nsec3clean(dns_name_t *name, dns_dbnode_t *node,
2035 unsigned int hashalg, unsigned int iterations,
11463c0a 2036 const unsigned char *salt, size_t salt_len, hashlist_t *hashlist)
6098d364
MA
2037{
2038 dns_label_t label;
2039 dns_rdata_nsec3_t nsec3;
2040 dns_rdata_t rdata, delrdata;
2041 dns_rdatalist_t rdatalist;
2042 dns_rdataset_t rdataset, delrdataset;
994e6569 2043 bool delete_rrsigs = false;
6098d364
MA
2044 isc_buffer_t target;
2045 isc_result_t result;
2046 unsigned char hash[NSEC3_MAX_HASH_LENGTH + 1];
994e6569 2047 bool exists;
6098d364
MA
2048
2049 /*
2050 * Get the first label.
2051 */
2052 dns_name_getlabel(name, 0, &label);
2053
2054 /*
2055 * We want just the label contents.
2056 */
2057 isc_region_consume(&label, 1);
2058
2059 /*
2060 * Decode base32hex string.
2061 */
2062 isc_buffer_init(&target, hash, sizeof(hash) - 1);
2063 result = isc_base32hex_decoderegion(&label, &target);
2064 if (result != ISC_R_SUCCESS)
2065 return;
2066
2067 hash[isc_buffer_usedlength(&target)] = 0;
2068
011d0b7d 2069 exists = hashlist_exists(hashlist, hash);
6098d364
MA
2070
2071 /*
2072 * Verify that the NSEC3 parameters match the current ones
2073 * otherwise we are dealing with a different NSEC3 chain.
2074 */
2075 dns_rdataset_init(&rdataset);
2076 dns_rdataset_init(&delrdataset);
2077
2078 result = dns_db_findrdataset(gdb, node, gversion, dns_rdatatype_nsec3,
2079 0, 0, &rdataset, NULL);
2080 if (result != ISC_R_SUCCESS)
2081 return;
2082
2083 /*
011d0b7d
MA
2084 * Delete any NSEC3 records which are not part of the current
2085 * NSEC3 chain.
6098d364
MA
2086 */
2087 for (result = dns_rdataset_first(&rdataset);
2088 result == ISC_R_SUCCESS;
2089 result = dns_rdataset_next(&rdataset)) {
2090 dns_rdata_init(&rdata);
2091 dns_rdataset_current(&rdataset, &rdata);
2534a73a
MA
2092 result = dns_rdata_tostruct(&rdata, &nsec3, NULL);
2093 check_result(result, "dns_rdata_tostruct");
011d0b7d 2094 if (exists && nsec3.hash == hashalg &&
6098d364 2095 nsec3.iterations == iterations &&
11463c0a 2096 nsec3.salt_length == salt_len &&
420a43c8 2097 isc_safe_memequal(nsec3.salt, salt, salt_len))
011d0b7d 2098 continue;
29d52c00 2099 dns_rdatalist_init(&rdatalist);
6098d364
MA
2100 rdatalist.rdclass = rdata.rdclass;
2101 rdatalist.type = rdata.type;
35f6a21f
EH
2102 if (set_maxttl)
2103 rdatalist.ttl = ISC_MIN(rdataset.ttl, maxttl);
6098d364
MA
2104 dns_rdata_init(&delrdata);
2105 dns_rdata_clone(&rdata, &delrdata);
2106 ISC_LIST_APPEND(rdatalist.rdata, &delrdata, link);
2107 result = dns_rdatalist_tordataset(&rdatalist, &delrdataset);
2108 check_result(result, "dns_rdatalist_tordataset()");
2109 result = dns_db_subtractrdataset(gdb, node, gversion,
2110 &delrdataset, 0, NULL);
2111 dns_rdataset_disassociate(&delrdataset);
011d0b7d 2112 if (result != ISC_R_SUCCESS && result != DNS_R_NXRRSET)
6098d364 2113 check_result(result, "dns_db_subtractrdataset(NSEC3)");
994e6569 2114 delete_rrsigs = true;
6098d364
MA
2115 }
2116 dns_rdataset_disassociate(&rdataset);
2117 if (result != ISC_R_NOMORE)
2118 check_result(result, "dns_rdataset_first/next");
2119
2120 if (!delete_rrsigs)
2121 return;
2122 /*
2123 * Delete the NSEC3 RRSIGs
2124 */
2125 result = dns_db_deleterdataset(gdb, node, gversion,
2126 dns_rdatatype_rrsig,
2127 dns_rdatatype_nsec3);
2128 if (result != ISC_R_SUCCESS && result != DNS_R_UNCHANGED)
2129 check_result(result, "dns_db_deleterdataset(RRSIG(NSEC3))");
2130}
2131
3d17a3ba 2132static void
35f6a21f
EH
2133rrset_cleanup(dns_name_t *name, dns_rdataset_t *rdataset,
2134 dns_diff_t *add, dns_diff_t *del)
3d17a3ba 2135{
4b6dc226
AU
2136 isc_result_t result;
2137 unsigned int count1 = 0;
3d17a3ba 2138 dns_rdataset_t tmprdataset;
35f6a21f 2139 char namestr[DNS_NAME_FORMATSIZE];
ffe8ddd9 2140 char typestr[DNS_RDATATYPE_FORMATSIZE];
35f6a21f
EH
2141
2142 dns_name_format(name, namestr, sizeof(namestr));
ffe8ddd9 2143 dns_rdatatype_format(rdataset->type, typestr, sizeof(typestr));
4b6dc226
AU
2144
2145 dns_rdataset_init(&tmprdataset);
2146 for (result = dns_rdataset_first(rdataset);
2147 result == ISC_R_SUCCESS;
2148 result = dns_rdataset_next(rdataset)) {
2149 dns_rdata_t rdata1 = DNS_RDATA_INIT;
2150 unsigned int count2 = 0;
2151
2152 count1++;
2153 dns_rdataset_current(rdataset, &rdata1);
2154 dns_rdataset_clone(rdataset, &tmprdataset);
2155 for (result = dns_rdataset_first(&tmprdataset);
2156 result == ISC_R_SUCCESS;
2157 result = dns_rdataset_next(&tmprdataset)) {
2158 dns_rdata_t rdata2 = DNS_RDATA_INIT;
35f6a21f 2159 dns_difftuple_t *tuple = NULL;
4b6dc226 2160 count2++;
4b6dc226 2161 dns_rdataset_current(&tmprdataset, &rdata2);
35f6a21f
EH
2162 if (count1 < count2 &&
2163 dns_rdata_casecompare(&rdata1, &rdata2) == 0)
2164 {
2165 vbprintf(2, "removing duplicate at %s/%s\n",
2166 namestr, typestr);
2167 result = dns_difftuple_create(mctx,
2168 DNS_DIFFOP_DELRESIGN,
2169 name, rdataset->ttl,
2170 &rdata2, &tuple);
2171 check_result(result, "dns_difftuple_create");
2172 dns_diff_append(del, &tuple);
2173 } else if (set_maxttl && rdataset->ttl > maxttl) {
2174 vbprintf(2, "reducing ttl of %s/%s "
2175 "from %d to %d\n",
2176 namestr, typestr,
2177 rdataset->ttl, maxttl);
3d17a3ba 2178 result = dns_difftuple_create(mctx,
b7e40659
EH
2179 DNS_DIFFOP_DELRESIGN,
2180 name, rdataset->ttl,
2181 &rdata2, &tuple);
3d17a3ba 2182 check_result(result, "dns_difftuple_create");
35f6a21f
EH
2183 dns_diff_append(del, &tuple);
2184 tuple = NULL;
2185 result = dns_difftuple_create(mctx,
2186 DNS_DIFFOP_ADDRESIGN,
2187 name, maxttl,
2188 &rdata2, &tuple);
2189 check_result(result, "dns_difftuple_create");
2190 dns_diff_append(add, &tuple);
4b6dc226
AU
2191 }
2192 }
2193 dns_rdataset_disassociate(&tmprdataset);
2194 }
3d17a3ba
MA
2195}
2196
2197static void
35f6a21f 2198cleanup_zone(void) {
3d17a3ba
MA
2199 isc_result_t result;
2200 dns_dbiterator_t *dbiter = NULL;
2201 dns_rdatasetiter_t *rdsiter = NULL;
35f6a21f 2202 dns_diff_t add, del;
3d17a3ba
MA
2203 dns_dbnode_t *node = NULL;
2204 dns_rdataset_t rdataset;
2205 dns_fixedname_t fname;
2206 dns_name_t *name;
4b6dc226 2207
35f6a21f
EH
2208 dns_diff_init(mctx, &add);
2209 dns_diff_init(mctx, &del);
4df4a8e7 2210 name = dns_fixedname_initname(&fname);
3d17a3ba
MA
2211 dns_rdataset_init(&rdataset);
2212
2213 result = dns_db_createiterator(gdb, 0, &dbiter);
2214 check_result(result, "dns_db_createiterator()");
2215
2216 for (result = dns_dbiterator_first(dbiter);
2217 result == ISC_R_SUCCESS;
2218 result = dns_dbiterator_next(dbiter)) {
2219
2220 result = dns_dbiterator_current(dbiter, &node, name);
2221 check_dns_dbiterator_current(result);
2222 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
2223 check_result(result, "dns_db_allrdatasets()");
2224 for (result = dns_rdatasetiter_first(rdsiter);
2225 result == ISC_R_SUCCESS;
2226 result = dns_rdatasetiter_next(rdsiter)) {
2227 dns_rdatasetiter_current(rdsiter, &rdataset);
35f6a21f 2228 rrset_cleanup(name, &rdataset, &add, &del);
3d17a3ba
MA
2229 dns_rdataset_disassociate(&rdataset);
2230 }
2231 if (result != ISC_R_NOMORE)
2232 fatal("rdatasets iteration failed.");
2233 dns_rdatasetiter_destroy(&rdsiter);
2234 dns_db_detachnode(gdb, &node);
2235 }
2236 if (result != ISC_R_NOMORE)
2237 fatal("zone iteration failed.");
2238
35f6a21f
EH
2239 result = dns_diff_applysilently(&del, gdb, gversion);
2240 check_result(result, "dns_diff_applysilently");
2241
2242 result = dns_diff_applysilently(&add, gdb, gversion);
2243 check_result(result, "dns_diff_applysilently");
2244
2245 dns_diff_clear(&del);
2246 dns_diff_clear(&add);
3d17a3ba
MA
2247 dns_dbiterator_destroy(&dbiter);
2248}
2249
6098d364
MA
2250/*
2251 * Generate NSEC3 records for the zone.
2252 */
2253static void
c3c8823f 2254nsec3ify(unsigned int hashalg, dns_iterations_t iterations,
11463c0a 2255 const unsigned char *salt, size_t salt_len, hashlist_t *hashlist)
6098d364
MA
2256{
2257 dns_dbiterator_t *dbiter = NULL;
2258 dns_dbnode_t *node = NULL, *nextnode = NULL;
2259 dns_fixedname_t fname, fnextname, fzonecut;
2260 dns_name_t *name, *nextname, *zonecut;
4d0e2cf9 2261 dns_rdataset_t rdataset;
4d0e2cf9 2262 int order;
994e6569
OS
2263 bool active;
2264 bool done = false;
6098d364 2265 isc_result_t result;
cb6a185c 2266 uint32_t nsttl = 0;
6098d364 2267 unsigned int count, nlabels;
6098d364 2268
4d0e2cf9 2269 dns_rdataset_init(&rdataset);
4df4a8e7
MK
2270 name = dns_fixedname_initname(&fname);
2271 nextname = dns_fixedname_initname(&fnextname);
6098d364
MA
2272 zonecut = NULL;
2273
2274 /*
2275 * Walk the zone generating the hash names.
2276 */
2277 result = dns_db_createiterator(gdb, DNS_DB_NONSEC3, &dbiter);
2278 check_result(result, "dns_db_createiterator()");
2279
2280 result = dns_dbiterator_first(dbiter);
2281 check_result(result, "dns_dbiterator_first()");
2282
2283 while (!done) {
011d0b7d
MA
2284 result = dns_dbiterator_current(dbiter, &node, name);
2285 check_dns_dbiterator_current(result);
6e13ffa2
AU
2286 /*
2287 * Skip out-of-zone records.
2288 */
2289 if (!dns_name_issubdomain(name, gorigin)) {
2290 result = dns_dbiterator_next(dbiter);
2291 if (result == ISC_R_NOMORE)
994e6569 2292 done = true;
6e13ffa2
AU
2293 else
2294 check_result(result, "dns_dbiterator_next()");
2295 dns_db_detachnode(gdb, &node);
2296 continue;
2297 }
2298
832ab79d 2299 if (dns_name_equal(name, gorigin)) {
994e6569 2300 remove_records(node, dns_rdatatype_nsec, true);
832ab79d
MA
2301 /* Clean old rrsigs at apex. */
2302 (void)active_node(node);
2303 }
675cc809 2304
75c0d85f 2305 if (has_dname(gdb, gversion, node)) {
7be900a9 2306 zonecut = savezonecut(&fzonecut, name);
75c0d85f
MK
2307 }
2308
6098d364
MA
2309 result = dns_dbiterator_next(dbiter);
2310 nextnode = NULL;
2311 while (result == ISC_R_SUCCESS) {
2312 result = dns_dbiterator_current(dbiter, &nextnode,
2313 nextname);
011d0b7d 2314 check_dns_dbiterator_current(result);
6098d364
MA
2315 active = active_node(nextnode);
2316 if (!active) {
2317 dns_db_detachnode(gdb, &nextnode);
2318 result = dns_dbiterator_next(dbiter);
2319 continue;
2320 }
2321 if (!dns_name_issubdomain(nextname, gorigin) ||
2322 (zonecut != NULL &&
2323 dns_name_issubdomain(nextname, zonecut))) {
994e6569 2324 remove_sigs(nextnode, false, 0);
6098d364
MA
2325 dns_db_detachnode(gdb, &nextnode);
2326 result = dns_dbiterator_next(dbiter);
2327 continue;
2328 }
ad127d83
MA
2329 if (is_delegation(gdb, gversion, gorigin,
2330 nextname, nextnode, &nsttl))
2331 {
7be900a9 2332 zonecut = savezonecut(&fzonecut, nextname);
994e6569 2333 remove_sigs(nextnode, true, 0);
6098d364
MA
2334 if (generateds)
2335 add_ds(nextname, nextnode, nsttl);
2336 if (OPTOUT(nsec3flags) &&
2337 !secure(nextname, nextnode)) {
2338 dns_db_detachnode(gdb, &nextnode);
2339 result = dns_dbiterator_next(dbiter);
2340 continue;
2341 }
75c0d85f 2342 } else if (has_dname(gdb, gversion, nextnode)) {
7be900a9 2343 zonecut = savezonecut(&fzonecut, nextname);
6098d364
MA
2344 }
2345 dns_db_detachnode(gdb, &nextnode);
2346 break;
2347 }
2348 if (result == ISC_R_NOMORE) {
2349 dns_name_copy(gorigin, nextname, NULL);
994e6569 2350 done = true;
6098d364
MA
2351 } else if (result != ISC_R_SUCCESS)
2352 fatal("iterating through the database failed: %s",
2353 isc_result_totext(result));
2354 dns_name_downcase(name, name, NULL);
2355 hashlist_add_dns_name(hashlist, name, hashalg, iterations,
994e6569 2356 salt, salt_len, false);
6098d364
MA
2357 dns_db_detachnode(gdb, &node);
2358 /*
2359 * Add hashs for empty nodes. Use closest encloser logic.
2360 * The closest encloser either has data or is a empty
2361 * node for another <name,nextname> span so we don't add
2362 * it here. Empty labels on nextname are within the span.
2363 */
2364 dns_name_downcase(nextname, nextname, NULL);
2365 dns_name_fullcompare(name, nextname, &order, &nlabels);
2366 addnowildcardhash(hashlist, name, hashalg, iterations,
11463c0a 2367 salt, salt_len);
6098d364
MA
2368 count = dns_name_countlabels(nextname);
2369 while (count > nlabels + 1) {
2370 count--;
2371 dns_name_split(nextname, count, NULL, nextname);
2372 hashlist_add_dns_name(hashlist, nextname, hashalg,
11463c0a 2373 iterations, salt, salt_len,
994e6569 2374 false);
6098d364 2375 addnowildcardhash(hashlist, nextname, hashalg,
11463c0a 2376 iterations, salt, salt_len);
6098d364
MA
2377 }
2378 }
2379 dns_dbiterator_destroy(&dbiter);
2380
2381 /*
2382 * We have all the hashes now so we can sort them.
2383 */
2384 hashlist_sort(hashlist);
2385
2386 /*
2387 * Check for duplicate hashes. If found the salt needs to
2388 * be changed.
2389 */
2390 if (hashlist_hasdup(hashlist))
2391 fatal("Duplicate hash detected. Pick a different salt.");
2392
2393 /*
2394 * Generate the nsec3 records.
2395 */
2396 zonecut = NULL;
994e6569 2397 done = false;
6098d364 2398
11463c0a 2399 addnsec3param(salt, salt_len, iterations);
6098d364 2400
011d0b7d
MA
2401 /*
2402 * Clean out NSEC3 records which don't match this chain.
2403 */
2404 result = dns_db_createiterator(gdb, DNS_DB_NSEC3ONLY, &dbiter);
2405 check_result(result, "dns_db_createiterator()");
2406
2407 for (result = dns_dbiterator_first(dbiter);
2408 result == ISC_R_SUCCESS;
2409 result = dns_dbiterator_next(dbiter)) {
2410 result = dns_dbiterator_current(dbiter, &node, name);
2411 check_dns_dbiterator_current(result);
11463c0a 2412 nsec3clean(name, node, hashalg, iterations, salt, salt_len,
011d0b7d
MA
2413 hashlist);
2414 dns_db_detachnode(gdb, &node);
2415 }
2416 dns_dbiterator_destroy(&dbiter);
2417
2418 /*
2419 * Generate / complete the new chain.
2420 */
6098d364
MA
2421 result = dns_db_createiterator(gdb, DNS_DB_NONSEC3, &dbiter);
2422 check_result(result, "dns_db_createiterator()");
2423
2424 result = dns_dbiterator_first(dbiter);
2425 check_result(result, "dns_dbiterator_first()");
2426
2427 while (!done) {
011d0b7d
MA
2428 result = dns_dbiterator_current(dbiter, &node, name);
2429 check_dns_dbiterator_current(result);
675cc809
MA
2430 /*
2431 * Skip out-of-zone records.
2432 */
2433 if (!dns_name_issubdomain(name, gorigin)) {
2434 result = dns_dbiterator_next(dbiter);
2435 if (result == ISC_R_NOMORE)
994e6569 2436 done = true;
675cc809
MA
2437 else
2438 check_result(result, "dns_dbiterator_next()");
2439 dns_db_detachnode(gdb, &node);
2440 continue;
6e13ffa2 2441 }
75c0d85f
MK
2442
2443 if (has_dname(gdb, gversion, node)) {
7be900a9 2444 zonecut = savezonecut(&fzonecut, name);
75c0d85f
MK
2445 }
2446
6098d364
MA
2447 result = dns_dbiterator_next(dbiter);
2448 nextnode = NULL;
2449 while (result == ISC_R_SUCCESS) {
2450 result = dns_dbiterator_current(dbiter, &nextnode,
2451 nextname);
011d0b7d
MA
2452 check_dns_dbiterator_current(result);
2453 active = active_node(nextnode);
2454 if (!active) {
6098d364
MA
2455 dns_db_detachnode(gdb, &nextnode);
2456 result = dns_dbiterator_next(dbiter);
2457 continue;
2458 }
2459 if (!dns_name_issubdomain(nextname, gorigin) ||
2460 (zonecut != NULL &&
2461 dns_name_issubdomain(nextname, zonecut))) {
2462 dns_db_detachnode(gdb, &nextnode);
2463 result = dns_dbiterator_next(dbiter);
2464 continue;
2465 }
ad127d83 2466 if (is_delegation(gdb, gversion, gorigin,
9a0dd99a 2467 nextname, nextnode, NULL))
ad127d83 2468 {
7be900a9 2469 zonecut = savezonecut(&fzonecut, nextname);
6098d364
MA
2470 if (OPTOUT(nsec3flags) &&
2471 !secure(nextname, nextnode)) {
2472 dns_db_detachnode(gdb, &nextnode);
2473 result = dns_dbiterator_next(dbiter);
2474 continue;
2475 }
75c0d85f 2476 } else if (has_dname(gdb, gversion, nextnode)) {
7be900a9 2477 zonecut = savezonecut(&fzonecut, nextname);
6098d364
MA
2478 }
2479 dns_db_detachnode(gdb, &nextnode);
2480 break;
2481 }
2482 if (result == ISC_R_NOMORE) {
2483 dns_name_copy(gorigin, nextname, NULL);
994e6569 2484 done = true;
6098d364
MA
2485 } else if (result != ISC_R_SUCCESS)
2486 fatal("iterating through the database failed: %s",
2487 isc_result_totext(result));
2488 /*
2489 * We need to pause here to release the lock on the database.
2490 */
2491 dns_dbiterator_pause(dbiter);
11463c0a 2492 addnsec3(name, node, salt, salt_len, iterations,
553ead32 2493 hashlist, zone_soa_min_ttl);
6098d364
MA
2494 dns_db_detachnode(gdb, &node);
2495 /*
2496 * Add NSEC3's for empty nodes. Use closest encloser logic.
2497 */
2498 dns_name_fullcompare(name, nextname, &order, &nlabels);
2499 count = dns_name_countlabels(nextname);
2500 while (count > nlabels + 1) {
2501 count--;
2502 dns_name_split(nextname, count, NULL, nextname);
11463c0a 2503 addnsec3(nextname, NULL, salt, salt_len,
553ead32 2504 iterations, hashlist, zone_soa_min_ttl);
6098d364
MA
2505 }
2506 }
2507 dns_dbiterator_destroy(&dbiter);
2508}
2509
ab023a65 2510/*%
417bd845
BW
2511 * Load the zone file from disk
2512 */
bf5582ad 2513static void
1f8f904a 2514loadzone(char *file, char *origin, dns_rdataclass_t rdclass, dns_db_t **db) {
c9d7e543 2515 isc_buffer_t b;
bf5582ad 2516 int len;
c9d7e543
BW
2517 dns_fixedname_t fname;
2518 dns_name_t *name;
bf5582ad
BW
2519 isc_result_t result;
2520
2521 len = strlen(origin);
6e49e91b 2522 isc_buffer_init(&b, origin, len);
bf5582ad
BW
2523 isc_buffer_add(&b, len);
2524
4df4a8e7 2525 name = dns_fixedname_initname(&fname);
307d2084 2526 result = dns_name_fromtext(name, &b, dns_rootname, 0, NULL);
6a285c81
BW
2527 if (result != ISC_R_SUCCESS)
2528 fatal("failed converting name '%s' to dns format: %s",
2529 origin, isc_result_totext(result));
bf5582ad 2530
c9d7e543 2531 result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone,
1f8f904a 2532 rdclass, 0, NULL, db);
7a152bda 2533 check_result(result, "dns_db_create()");
19a3e16a 2534
275a6a3b 2535 result = dns_db_load(*db, file, inputformat, 0);
bdfb3735 2536 if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
6a285c81
BW
2537 fatal("failed loading zone from '%s': %s",
2538 file, isc_result_totext(result));
19a3e16a
BW
2539}
2540
ab023a65 2541/*%
2d478b70
BW
2542 * Finds all public zone keys in the zone, and attempts to load the
2543 * private keys from disk.
2544 */
bf5582ad 2545static void
994e6569 2546loadzonekeys(bool preserve_keys, bool load_public) {
bf5582ad 2547 dns_dbnode_t *node;
e8831e51 2548 dns_dbversion_t *currentversion = NULL;
bf5582ad 2549 isc_result_t result;
e8831e51 2550 dns_rdataset_t rdataset, keysigs, soasigs;
bf5582ad
BW
2551
2552 node = NULL;
994e6569 2553 result = dns_db_findnode(gdb, gorigin, false, &node);
6a285c81
BW
2554 if (result != ISC_R_SUCCESS)
2555 fatal("failed to find the zone's origin: %s",
2556 isc_result_totext(result));
bf5582ad 2557
e8831e51
EH
2558 dns_db_currentversion(gdb, &currentversion);
2559
553ead32 2560 dns_rdataset_init(&rdataset);
e8831e51
EH
2561 dns_rdataset_init(&soasigs);
2562 dns_rdataset_init(&keysigs);
2563
2564 /* Make note of the keys which signed the SOA, if any */
2565 result = dns_db_findrdataset(gdb, node, currentversion,
2566 dns_rdatatype_soa, 0, 0,
2567 &rdataset, &soasigs);
2568 if (result != ISC_R_SUCCESS)
2569 goto cleanup;
2570
2571 /* Preserve the TTL of the DNSKEY RRset, if any */
2572 dns_rdataset_disassociate(&rdataset);
77b8f88f 2573 result = dns_db_findrdataset(gdb, node, currentversion,
553ead32 2574 dns_rdatatype_dnskey, 0, 0,
e8831e51 2575 &rdataset, &keysigs);
553ead32 2576
6a285c81 2577 if (result != ISC_R_SUCCESS)
77b8f88f 2578 goto cleanup;
bf5582ad 2579
77b8f88f 2580 if (set_keyttl && keyttl != rdataset.ttl) {
ea8e149f 2581 fprintf(stderr, "User-specified TTL %u conflicts "
77b8f88f
EH
2582 "with existing DNSKEY RRset TTL.\n",
2583 keyttl);
2584 fprintf(stderr, "Imported keys will use the RRSet "
ea8e149f 2585 "TTL %u instead.\n",
77b8f88f 2586 rdataset.ttl);
bf5582ad 2587 }
77b8f88f 2588 keyttl = rdataset.ttl;
553ead32 2589
0874abad 2590 /* Load keys corresponding to the existing DNSKEY RRset. */
77b8f88f 2591 result = dns_dnssec_keylistfromrdataset(gorigin, directory, mctx,
e8831e51
EH
2592 &rdataset, &keysigs, &soasigs,
2593 preserve_keys, load_public,
2594 &keylist);
469f328a 2595 if (result != ISC_R_SUCCESS)
77b8f88f 2596 fatal("failed to load the zone keys: %s",
469f328a
BW
2597 isc_result_totext(result));
2598
553ead32
EH
2599 cleanup:
2600 if (dns_rdataset_isassociated(&rdataset))
2601 dns_rdataset_disassociate(&rdataset);
e8831e51
EH
2602 if (dns_rdataset_isassociated(&keysigs))
2603 dns_rdataset_disassociate(&keysigs);
2604 if (dns_rdataset_isassociated(&soasigs))
2605 dns_rdataset_disassociate(&soasigs);
77b8f88f 2606 dns_db_detachnode(gdb, &node);
994e6569 2607 dns_db_closeversion(gdb, &currentversion, false);
553ead32
EH
2608}
2609
e8831e51 2610static void
994e6569 2611loadexplicitkeys(char *keyfiles[], int n, bool setksk) {
e8831e51
EH
2612 isc_result_t result;
2613 int i;
2614
2615 for (i = 0; i < n; i++) {
2616 dns_dnsseckey_t *key = NULL;
2617 dst_key_t *newkey = NULL;
2618
2619 result = dst_key_fromnamedfile(keyfiles[i], directory,
2620 DST_TYPE_PUBLIC |
2621 DST_TYPE_PRIVATE,
2622 mctx, &newkey);
2623 if (result != ISC_R_SUCCESS)
2624 fatal("cannot load dnskey %s: %s", keyfiles[i],
2625 isc_result_totext(result));
2626
2627 if (!dns_name_equal(gorigin, dst_key_name(newkey)))
2628 fatal("key %s not at origin\n", keyfiles[i]);
2629
2630 if (!dst_key_isprivate(newkey))
2631 fatal("cannot sign zone with non-private dnskey %s",
2632 keyfiles[i]);
2633
2634 /* Skip any duplicates */
2635 for (key = ISC_LIST_HEAD(keylist);
2636 key != NULL;
2637 key = ISC_LIST_NEXT(key, link)) {
2638 if (dst_key_id(key->key) == dst_key_id(newkey) &&
2639 dst_key_alg(key->key) == dst_key_alg(newkey))
2640 break;
2641 }
2642
2643 if (key == NULL) {
2644 /* We haven't seen this key before */
2645 dns_dnsseckey_create(mctx, &newkey, &key);
2646 ISC_LIST_APPEND(keylist, key, link);
2647 key->source = dns_keysource_user;
2648 } else {
2649 dst_key_free(&key->key);
2650 key->key = newkey;
2651 }
2652
994e6569
OS
2653 key->force_publish = true;
2654 key->force_sign = true;
e8831e51
EH
2655
2656 if (setksk)
994e6569 2657 key->ksk = true;
e8831e51
EH
2658 }
2659}
2660
77b8f88f
EH
2661static void
2662report(const char *format, ...) {
2663 va_list args;
2664 va_start(args, format);
2665 vfprintf(stderr, format, args);
2666 va_end(args);
d312bc5d 2667 putc('\n', stderr);
553ead32
EH
2668}
2669
e515fae2 2670static void
b47814be 2671clear_keylist(dns_dnsseckeylist_t *list) {
e515fae2
EH
2672 dns_dnsseckey_t *key;
2673 while (!ISC_LIST_EMPTY(*list)) {
2674 key = ISC_LIST_HEAD(*list);
2675 ISC_LIST_UNLINK(*list, key, link);
2676 dns_dnsseckey_destroy(mctx, &key);
2677 }
2678}
2679
553ead32 2680static void
b454c031 2681build_final_keylist(void) {
553ead32 2682 isc_result_t result;
e515fae2 2683 dns_dbnode_t *node = NULL;
553ead32 2684 dns_dbversion_t *ver = NULL;
cef109ef 2685 dns_diff_t diff;
e515fae2 2686 dns_dnsseckeylist_t rmkeys, matchkeys;
553ead32 2687 char name[DNS_NAME_FORMATSIZE];
e515fae2 2688 dns_rdataset_t cdsset, cdnskeyset, soaset;
e515fae2
EH
2689
2690 ISC_LIST_INIT(rmkeys);
2691 ISC_LIST_INIT(matchkeys);
2692
2693 dns_rdataset_init(&soaset);
2694 dns_rdataset_init(&cdsset);
2695 dns_rdataset_init(&cdnskeyset);
2696
77b8f88f
EH
2697 /*
2698 * Find keys that match this zone in the key repository.
2699 */
553ead32 2700 result = dns_dnssec_findmatchingkeys(gorigin, directory,
25cd3168 2701 now, mctx, &matchkeys);
e515fae2 2702 if (result == ISC_R_NOTFOUND) {
553ead32 2703 result = ISC_R_SUCCESS;
e515fae2 2704 }
553ead32
EH
2705 check_result(result, "dns_dnssec_findmatchingkeys");
2706
e8831e51 2707 result = dns_db_newversion(gdb, &ver);
553ead32
EH
2708 check_result(result, "dns_db_newversion");
2709
e515fae2
EH
2710 result = dns_db_getoriginnode(gdb, &node);
2711 check_result(result, "dns_db_getoriginnode");
2712
e515fae2
EH
2713 /* Get the CDS rdataset */
2714 result = dns_db_findrdataset(gdb, node, ver, dns_rdatatype_cds,
2715 dns_rdatatype_none, 0, &cdsset, NULL);
2716 if (result != ISC_R_SUCCESS &&
2717 dns_rdataset_isassociated(&cdsset))
2718 {
2719 dns_rdataset_disassociate(&cdsset);
2720 }
2721
2722 /* Get the CDNSKEY rdataset */
2723 result = dns_db_findrdataset(gdb, node, ver, dns_rdatatype_cdnskey,
2724 dns_rdatatype_none, 0, &cdnskeyset, NULL);
2725 if (result != ISC_R_SUCCESS &&
2726 dns_rdataset_isassociated(&cdnskeyset))
2727 {
2728 dns_rdataset_disassociate(&cdnskeyset);
2729 }
2730
cef109ef 2731 dns_diff_init(mctx, &diff);
553ead32
EH
2732
2733 /*
77b8f88f 2734 * Update keylist with information from from the key repository.
553ead32 2735 */
9510de78 2736 dns_dnssec_updatekeys(&keylist, &matchkeys, NULL, gorigin, keyttl,
cef109ef 2737 &diff, ignore_kskflag, mctx, report);
553ead32 2738
e515fae2
EH
2739 /*
2740 * Update keylist with sync records.
2741 */
2742 dns_dnssec_syncupdate(&keylist, &rmkeys, &cdsset, &cdnskeyset,
9510de78 2743 now, keyttl, &diff, mctx);
e515fae2 2744
77b8f88f 2745 dns_name_format(gorigin, name, sizeof(name));
553ead32 2746
cef109ef 2747 result = dns_diff_applysilently(&diff, gdb, ver);
e515fae2 2748 if (result != ISC_R_SUCCESS) {
cef109ef 2749 fatal("failed to update DNSKEY RRset at node '%s': %s",
553ead32 2750 name, isc_result_totext(result));
e515fae2 2751 }
553ead32 2752
e515fae2 2753 dns_db_detachnode(gdb, &node);
994e6569 2754 dns_db_closeversion(gdb, &ver, true);
553ead32 2755
cef109ef 2756 dns_diff_clear(&diff);
e515fae2
EH
2757
2758 if (dns_rdataset_isassociated(&cdsset)) {
2759 dns_rdataset_disassociate(&cdsset);
2760 }
2761 if (dns_rdataset_isassociated(&cdnskeyset)) {
2762 dns_rdataset_disassociate(&cdnskeyset);
2763 }
2764
b47814be
EH
2765 clear_keylist(&rmkeys);
2766 clear_keylist(&matchkeys);
469f328a
BW
2767}
2768
b0c15bd9
MA
2769static void
2770warnifallksk(dns_db_t *db) {
2771 dns_dbversion_t *currentversion = NULL;
2772 dns_dbnode_t *node = NULL;
2773 dns_rdataset_t rdataset;
2774 dns_rdata_t rdata = DNS_RDATA_INIT;
b0c15bd9 2775 isc_result_t result;
2534a73a 2776 dns_rdata_dnskey_t dnskey;
994e6569 2777 bool have_non_ksk = false;
b0c15bd9
MA
2778
2779 dns_db_currentversion(db, &currentversion);
2780
994e6569 2781 result = dns_db_findnode(db, gorigin, false, &node);
b0c15bd9
MA
2782 if (result != ISC_R_SUCCESS)
2783 fatal("failed to find the zone's origin: %s",
2784 isc_result_totext(result));
2785
2786 dns_rdataset_init(&rdataset);
2787 result = dns_db_findrdataset(db, node, currentversion,
553ead32
EH
2788 dns_rdatatype_dnskey, 0, 0, &rdataset,
2789 NULL);
b0c15bd9
MA
2790 if (result != ISC_R_SUCCESS)
2791 fatal("failed to find keys at the zone apex: %s",
2792 isc_result_totext(result));
2793 result = dns_rdataset_first(&rdataset);
2794 check_result(result, "dns_rdataset_first");
2795 while (result == ISC_R_SUCCESS) {
b0c15bd9
MA
2796 dns_rdata_reset(&rdata);
2797 dns_rdataset_current(&rdataset, &rdata);
2534a73a 2798 result = dns_rdata_tostruct(&rdata, &dnskey, NULL);
b0c15bd9 2799 check_result(result, "dns_rdata_tostruct");
2534a73a 2800 if ((dnskey.flags & DNS_KEYFLAG_KSK) == 0) {
994e6569 2801 have_non_ksk = true;
b0c15bd9
MA
2802 result = ISC_R_NOMORE;
2803 } else
2804 result = dns_rdataset_next(&rdataset);
2534a73a 2805 dns_rdata_freestruct(&dnskey);
b0c15bd9
MA
2806 }
2807 dns_rdataset_disassociate(&rdataset);
2808 dns_db_detachnode(db, &node);
994e6569 2809 dns_db_closeversion(db, &currentversion, false);
553ead32 2810 if (!have_non_ksk && !ignore_kskflag) {
2534a73a 2811 if (disable_zone_check)
553ead32
EH
2812 fprintf(stderr, "%s: warning: No non-KSK DNSKEY found; "
2813 "supply a ZSK or use '-z'.\n",
2534a73a
MA
2814 program);
2815 else
553ead32
EH
2816 fatal("No non-KSK DNSKEY found; "
2817 "supply a ZSK or use '-z'.");
2534a73a 2818 }
b0c15bd9
MA
2819}
2820
fb596cc9 2821static void
994e6569
OS
2822set_nsec3params(bool update, bool set_salt,
2823 bool set_optout, bool set_iter)
fb596cc9
EH
2824{
2825 isc_result_t result;
2826 dns_dbversion_t *ver = NULL;
2827 dns_dbnode_t *node = NULL;
2828 dns_rdataset_t rdataset;
2829 dns_rdata_t rdata = DNS_RDATA_INIT;
2830 dns_rdata_nsec3_t nsec3;
2831 dns_fixedname_t fname;
2832 dns_name_t *hashname;
41bbb34b 2833 unsigned char orig_salt[255];
fb596cc9
EH
2834 size_t orig_saltlen;
2835 dns_hash_t orig_hash;
cb6a185c 2836 uint16_t orig_iter;
fb596cc9
EH
2837
2838 dns_db_currentversion(gdb, &ver);
1e3c9961 2839 dns_rdataset_init(&rdataset);
fb596cc9
EH
2840
2841 orig_saltlen = sizeof(orig_salt);
627f3e08 2842 result = dns_db_getnsec3parameters(gdb, ver, &orig_hash, NULL,
fb596cc9
EH
2843 &orig_iter, orig_salt,
2844 &orig_saltlen);
2845 if (result != ISC_R_SUCCESS)
2846 goto cleanup;
2847
2848 nsec_datatype = dns_rdatatype_nsec3;
2849
11463c0a 2850 if (!update && set_salt) {
fb596cc9 2851 if (salt_length != orig_saltlen ||
420a43c8 2852 !isc_safe_memequal(saltbuf, orig_salt, salt_length))
fb596cc9
EH
2853 fatal("An NSEC3 chain exists with a different salt. "
2854 "Use -u to update it.");
2855 } else if (!set_salt) {
2856 salt_length = orig_saltlen;
e851ea82 2857 memmove(saltbuf, orig_salt, orig_saltlen);
11463c0a 2858 gsalt = saltbuf;
fb596cc9
EH
2859 }
2860
11463c0a 2861 if (!update && set_iter) {
fb596cc9
EH
2862 if (nsec3iter != orig_iter)
2863 fatal("An NSEC3 chain exists with different "
2864 "iterations. Use -u to update it.");
2865 } else if (!set_iter)
2866 nsec3iter = orig_iter;
2867
2868 /*
2869 * Find an NSEC3 record to get the current OPTOUT value.
2870 * (This assumes all NSEC3 records agree.)
2871 */
2872
4df4a8e7 2873 hashname = dns_fixedname_initname(&fname);
fb596cc9
EH
2874 result = dns_nsec3_hashname(&fname, NULL, NULL,
2875 gorigin, gorigin, dns_hash_sha1,
2876 orig_iter, orig_salt, orig_saltlen);
2877 check_result(result, "dns_nsec3_hashname");
2878
994e6569 2879 result = dns_db_findnsec3node(gdb, hashname, false, &node);
fb596cc9
EH
2880 if (result != ISC_R_SUCCESS)
2881 goto cleanup;
2882
fb596cc9
EH
2883 result = dns_db_findrdataset(gdb, node, ver, dns_rdatatype_nsec3,
2884 0, 0, &rdataset, NULL);
2885 if (result != ISC_R_SUCCESS)
627f3e08 2886 goto cleanup;
fb596cc9
EH
2887
2888 result = dns_rdataset_first(&rdataset);
2889 check_result(result, "dns_rdataset_first");
2890 dns_rdataset_current(&rdataset, &rdata);
2891 result = dns_rdata_tostruct(&rdata, &nsec3, NULL);
2892 check_result(result, "dns_rdata_tostruct");
2893
11463c0a 2894 if (!update && set_optout) {
fb596cc9
EH
2895 if (nsec3flags != nsec3.flags)
2896 fatal("An NSEC3 chain exists with%s OPTOUT. "
2897 "Use -u -%s to %s it.",
2898 OPTOUT(nsec3.flags) ? "" : "out",
2899 OPTOUT(nsec3.flags) ? "AA" : "A",
2900 OPTOUT(nsec3.flags) ? "clear" : "set");
2901 } else if (!set_optout)
2902 nsec3flags = nsec3.flags;
2903
2904 dns_rdata_freestruct(&nsec3);
2905
2906 cleanup:
627f3e08
AU
2907 if (dns_rdataset_isassociated(&rdataset))
2908 dns_rdataset_disassociate(&rdataset);
2909 if (node != NULL)
2910 dns_db_detachnode(gdb, &node);
994e6569 2911 dns_db_closeversion(gdb, &ver, false);
fb596cc9
EH
2912}
2913
0b09763c 2914static void
50105afc
MA
2915writeset(const char *prefix, dns_rdatatype_t type) {
2916 char *filename;
0b09763c 2917 char namestr[DNS_NAME_FORMATSIZE];
0b09763c 2918 dns_db_t *db = NULL;
af669cb4 2919 dns_dbversion_t *dbversion = NULL;
50105afc
MA
2920 dns_diff_t diff;
2921 dns_difftuple_t *tuple = NULL;
2922 dns_fixedname_t fixed;
2923 dns_name_t *name;
2924 dns_rdata_t rdata, ds;
994e6569
OS
2925 bool have_ksk = false;
2926 bool have_non_ksk = false;
0b09763c 2927 isc_buffer_t b;
50105afc 2928 isc_buffer_t namebuf;
0b09763c
MA
2929 isc_region_t r;
2930 isc_result_t result;
3727725b 2931 dns_dnsseckey_t *key, *tmpkey;
50105afc
MA
2932 unsigned char dsbuf[DNS_DS_BUFFERSIZE];
2933 unsigned char keybuf[DST_KEY_MAXSIZE];
2934 unsigned int filenamelen;
177bcb46 2935 const dns_master_style_t *style =
50105afc 2936 (type == dns_rdatatype_dnskey) ? masterstyle : dsstyle;
0b09763c
MA
2937
2938 isc_buffer_init(&namebuf, namestr, sizeof(namestr));
994e6569 2939 result = dns_name_tofilenametext(gorigin, false, &namebuf);
0b09763c
MA
2940 check_result(result, "dns_name_tofilenametext");
2941 isc_buffer_putuint8(&namebuf, 0);
21c12d01 2942 filenamelen = strlen(prefix) + strlen(namestr) + 1;
553ead32
EH
2943 if (dsdir != NULL)
2944 filenamelen += strlen(dsdir) + 1;
21c12d01 2945 filename = isc_mem_get(mctx, filenamelen);
50105afc 2946 if (filename == NULL)
0b09763c 2947 fatal("out of memory");
553ead32 2948 if (dsdir != NULL)
a009d03a 2949 snprintf(filename, filenamelen, "%s/", dsdir);
0b09763c 2950 else
50105afc 2951 filename[0] = 0;
114f9508
EH
2952 strlcat(filename, prefix, filenamelen);
2953 strlcat(filename, namestr, filenamelen);
0b09763c
MA
2954
2955 dns_diff_init(mctx, &diff);
2956
50105afc
MA
2957 if (type == dns_rdatatype_dlv) {
2958 dns_name_t tname;
2959 unsigned int labels;
2960
2961 dns_name_init(&tname, NULL);
4df4a8e7 2962 name = dns_fixedname_initname(&fixed);
50105afc
MA
2963 labels = dns_name_countlabels(gorigin);
2964 dns_name_getlabelsequence(gorigin, 0, labels - 1, &tname);
2965 result = dns_name_concatenate(&tname, dlv, name, NULL);
2966 check_result(result, "dns_name_concatenate");
2967 } else
2968 name = gorigin;
2969
0b09763c
MA
2970 for (key = ISC_LIST_HEAD(keylist);
2971 key != NULL;
2972 key = ISC_LIST_NEXT(key, link))
2973 {
3727725b
EH
2974 if (REVOKE(key->key))
2975 continue;
2976 if (isksk(key)) {
994e6569
OS
2977 have_ksk = true;
2978 have_non_ksk = false;
3727725b 2979 } else {
994e6569
OS
2980 have_ksk = false;
2981 have_non_ksk = true;
3727725b
EH
2982 }
2983 for (tmpkey = ISC_LIST_HEAD(keylist);
2984 tmpkey != NULL;
2985 tmpkey = ISC_LIST_NEXT(tmpkey, link)) {
2986 if (dst_key_alg(key->key) != dst_key_alg(tmpkey->key))
2987 continue;
2988 if (REVOKE(tmpkey->key))
2989 continue;
2990 if (isksk(tmpkey))
994e6569 2991 have_ksk = true;
3727725b 2992 else
994e6569 2993 have_non_ksk = true;
3727725b 2994 }
553ead32 2995 if (have_ksk && have_non_ksk && !isksk(key))
0b09763c
MA
2996 continue;
2997 dns_rdata_init(&rdata);
50105afc 2998 dns_rdata_init(&ds);
0b09763c
MA
2999 isc_buffer_init(&b, keybuf, sizeof(keybuf));
3000 result = dst_key_todns(key->key, &b);
3001 check_result(result, "dst_key_todns");
3002 isc_buffer_usedregion(&b, &r);
93d6dfaf 3003 dns_rdata_fromregion(&rdata, gclass, dns_rdatatype_dnskey, &r);
50105afc
MA
3004 if (type != dns_rdatatype_dnskey) {
3005 result = dns_ds_buildrdata(gorigin, &rdata,
3006 DNS_DSDIGEST_SHA1,
3007 dsbuf, &ds);
3008 check_result(result, "dns_ds_buildrdata");
3009 if (type == dns_rdatatype_dlv)
3010 ds.type = dns_rdatatype_dlv;
b7e40659
EH
3011 result = dns_difftuple_create(mctx,
3012 DNS_DIFFOP_ADDRESIGN,
50105afc 3013 name, 0, &ds, &tuple);
c5387e69
MA
3014 check_result(result, "dns_difftuple_create");
3015 dns_diff_append(&diff, &tuple);
3016
3017 dns_rdata_reset(&ds);
3018 result = dns_ds_buildrdata(gorigin, &rdata,
3019 DNS_DSDIGEST_SHA256,
3020 dsbuf, &ds);
3021 check_result(result, "dns_ds_buildrdata");
3022 if (type == dns_rdatatype_dlv)
3023 ds.type = dns_rdatatype_dlv;
b7e40659
EH
3024 result = dns_difftuple_create(mctx,
3025 DNS_DIFFOP_ADDRESIGN,
c5387e69
MA
3026 name, 0, &ds, &tuple);
3027
50105afc 3028 } else
b7e40659
EH
3029 result = dns_difftuple_create(mctx,
3030 DNS_DIFFOP_ADDRESIGN,
553ead32 3031 gorigin, zone_soa_min_ttl,
50105afc 3032 &rdata, &tuple);
0b09763c
MA
3033 check_result(result, "dns_difftuple_create");
3034 dns_diff_append(&diff, &tuple);
3035 }
3036
3037 result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone,
3038 gclass, 0, NULL, &db);
3039 check_result(result, "dns_db_create");
3040
af669cb4 3041 result = dns_db_newversion(db, &dbversion);
0b09763c
MA
3042 check_result(result, "dns_db_newversion");
3043
af669cb4 3044 result = dns_diff_apply(&diff, db, dbversion);
0b09763c
MA
3045 check_result(result, "dns_diff_apply");
3046 dns_diff_clear(&diff);
3047
e2a06db7
WK
3048 result = dns_master_dump(mctx, db, dbversion, style, filename,
3049 dns_masterformat_text, NULL);
0b09763c
MA
3050 check_result(result, "dns_master_dump");
3051
21c12d01 3052 isc_mem_put(mctx, filename, filenamelen);
0b09763c 3053
994e6569 3054 dns_db_closeversion(db, &dbversion, false);
0b09763c
MA
3055 dns_db_detach(&db);
3056}
3057
253f774e
BW
3058static void
3059print_time(FILE *fp) {
3060 time_t currenttime;
3061
e1740442
MA
3062 if (outputformat != dns_masterformat_text)
3063 return;
3064
253f774e
BW
3065 currenttime = time(NULL);
3066 fprintf(fp, "; File written on %s", ctime(&currenttime));
3067}
3068
31874cf8
BW
3069static void
3070print_version(FILE *fp) {
e1740442
MA
3071 if (outputformat != dns_masterformat_text)
3072 return;
3073
31874cf8
BW
3074 fprintf(fp, "; dnssec_signzone version " VERSION "\n");
3075}
3076
debd489a
FD
3077ISC_PLATFORM_NORETURN_PRE static void
3078usage(void) ISC_PLATFORM_NORETURN_POST;
3079
bf5582ad 3080static void
d6a2af16 3081usage(void) {
bf5582ad 3082 fprintf(stderr, "Usage:\n");
b1d234eb 3083 fprintf(stderr, "\t%s [options] zonefile [keys]\n", program);
bf5582ad
BW
3084
3085 fprintf(stderr, "\n");
3086
93e35342
MA
3087 fprintf(stderr, "Version: %s\n", VERSION);
3088
803e1f0c 3089 fprintf(stderr, "Options: (default value in parenthesis) \n");
fb596cc9
EH
3090 fprintf(stderr, "\t-S:\tsmart signing: automatically finds key files\n"
3091 "\t\tfor the zone and determines how they are to "
3092 "be used\n");
553ead32
EH
3093 fprintf(stderr, "\t-K directory:\n");
3094 fprintf(stderr, "\t\tdirectory to find key files (.)\n");
3095 fprintf(stderr, "\t-d directory:\n");
00295e06 3096 fprintf(stderr, "\t\tdirectory to find dsset-* files (.)\n");
bf7f253e 3097 fprintf(stderr, "\t-g:\t");
00295e06
EH
3098 fprintf(stderr, "update DS records based on child zones' "
3099 "dsset-* files\n");
24efdccd 3100 fprintf(stderr, "\t-s [YYYYMMDDHHMMSS|+offset]:\n");
61271cde 3101 fprintf(stderr, "\t\tRRSIG start time "
26a73063 3102 "- absolute|offset (now - 1 hour)\n");
24efdccd 3103 fprintf(stderr, "\t-e [YYYYMMDDHHMMSS|+offset|\"now\"+offset]:\n");
61271cde 3104 fprintf(stderr, "\t\tRRSIG end time "
26a73063 3105 "- absolute|from start|from now "
d6a2af16 3106 "(now + 30 days)\n");