2 * Portions Copyright (C) 2004-2010 Internet Systems Consortium, Inc. ("ISC")
3 * Portions Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
10 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
12 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 * Portions Copyright (C) 1995-2000 by Network Associates, Inc.
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
23 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NETWORK ASSOCIATES DISCLAIMS
24 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE
26 * FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
29 * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 /* $Id: dnssec-signzone.c,v 1.209.12.20 2010-06-03 23:47:48 tbox Exp $ */
42 #include <isc/base32.h>
43 #include <isc/commandline.h>
44 #include <isc/entropy.h>
45 #include <isc/event.h>
50 #include <isc/mutex.h>
52 #include <isc/print.h>
53 #include <isc/random.h>
54 #include <isc/rwlock.h>
55 #include <isc/serial.h>
56 #include <isc/stdio.h>
57 #include <isc/stdlib.h>
58 #include <isc/string.h>
64 #include <dns/dbiterator.h>
66 #include <dns/dnssec.h>
68 #include <dns/fixedname.h>
69 #include <dns/keyvalues.h>
71 #include <dns/master.h>
72 #include <dns/masterdump.h>
74 #include <dns/nsec3.h>
75 #include <dns/rdata.h>
76 #include <dns/rdatalist.h>
77 #include <dns/rdataset.h>
78 #include <dns/rdataclass.h>
79 #include <dns/rdatasetiter.h>
80 #include <dns/rdatastruct.h>
81 #include <dns/rdatatype.h>
82 #include <dns/result.h>
88 #include "dnssectool.h"
90 const char *program = "dnssec-signzone";
93 typedef struct hashlist hashlist_t;
95 static int nsec_datatype = dns_rdatatype_nsec;
97 #define IS_NSEC3 (nsec_datatype == dns_rdatatype_nsec3)
98 #define OPTOUT(x) (((x) & DNS_NSEC3FLAG_OPTOUT) != 0)
103 typedef struct signer_key_struct signer_key_t;
105 struct signer_key_struct {
107 isc_boolean_t issigningkey;
110 isc_boolean_t wasused;
111 isc_boolean_t commandline;
112 unsigned int position;
113 ISC_LINK(signer_key_t) link;
116 #define SIGNER_EVENTCLASS ISC_EVENTCLASS(0x4453)
117 #define SIGNER_EVENT_WRITE (SIGNER_EVENTCLASS + 0)
118 #define SIGNER_EVENT_WORK (SIGNER_EVENTCLASS + 1)
120 #define SOA_SERIAL_KEEP 0
121 #define SOA_SERIAL_INCREMENT 1
122 #define SOA_SERIAL_UNIXTIME 2
124 typedef struct signer_event sevent_t;
125 struct signer_event {
126 ISC_EVENT_COMMON(sevent_t);
127 dns_fixedname_t *fname;
131 static ISC_LIST(signer_key_t) keylist;
132 static unsigned int keycount = 0;
133 isc_rwlock_t keylist_lock;
134 static isc_stdtime_t starttime = 0, endtime = 0, now;
135 static int cycle = -1;
136 static int jitter = 0;
137 static isc_boolean_t tryverify = ISC_FALSE;
138 static isc_boolean_t printstats = ISC_FALSE;
139 static isc_mem_t *mctx = NULL;
140 static isc_entropy_t *ectx = NULL;
141 static dns_ttl_t zonettl;
143 static char *tempfile = NULL;
144 static const dns_master_style_t *masterstyle;
145 static dns_masterformat_t inputformat = dns_masterformat_text;
146 static dns_masterformat_t outputformat = dns_masterformat_text;
147 static unsigned int nsigned = 0, nretained = 0, ndropped = 0;
148 static unsigned int nverified = 0, nverifyfailed = 0;
149 static const char *directory;
150 static isc_mutex_t namelock, statslock;
151 static isc_taskmgr_t *taskmgr = NULL;
152 static dns_db_t *gdb; /* The database */
153 static dns_dbversion_t *gversion; /* The database version */
154 static dns_dbiterator_t *gdbiter; /* The database iterator */
155 static dns_rdataclass_t gclass; /* The class */
156 static dns_name_t *gorigin; /* The database origin */
157 static int nsec3flags = 0;
158 static isc_task_t *master = NULL;
159 static unsigned int ntasks = 0;
160 static isc_boolean_t shuttingdown = ISC_FALSE, finished = ISC_FALSE;
161 static isc_boolean_t nokeys = ISC_FALSE;
162 static isc_boolean_t removefile = ISC_FALSE;
163 static isc_boolean_t generateds = ISC_FALSE;
164 static isc_boolean_t ignoreksk = ISC_FALSE;
165 static dns_name_t *dlv = NULL;
166 static dns_fixedname_t dlv_fixed;
167 static dns_master_style_t *dsstyle = NULL;
168 static unsigned int serialformat = SOA_SERIAL_KEEP;
169 static unsigned int hash_length = 0;
170 static isc_boolean_t unknownalg = ISC_FALSE;
171 static isc_boolean_t disable_zone_check = ISC_FALSE;
173 #define INCSTAT(counter) \
177 UNLOCK(&statslock); \
181 sign(isc_task_t *task, isc_event_t *event);
183 #define check_dns_dbiterator_current(result) \
184 check_result((result == DNS_R_NEWORIGIN) ? ISC_R_SUCCESS : result, \
185 "dns_dbiterator_current()")
188 dumpnode(dns_name_t *name, dns_dbnode_t *node) {
191 if (outputformat != dns_masterformat_text)
193 result = dns_master_dumpnodetostream(mctx, gdb, gversion, node, name,
195 check_result(result, "dns_master_dumpnodetostream");
198 static signer_key_t *
199 newkeystruct(dst_key_t *dstkey, isc_boolean_t signwithkey) {
202 key = isc_mem_get(mctx, sizeof(signer_key_t));
204 fatal("out of memory");
206 if ((dst_key_flags(dstkey) & DNS_KEYFLAG_KSK) != 0) {
207 key->issigningkey = signwithkey;
208 key->isksk = ISC_TRUE;
209 key->isdsk = ISC_FALSE;
211 key->issigningkey = signwithkey;
212 key->isksk = ISC_FALSE;
213 key->isdsk = ISC_TRUE;
215 key->wasused = ISC_FALSE;
216 key->commandline = ISC_FALSE;
217 key->position = keycount++;
218 ISC_LINK_INIT(key, link);
223 * Sign the given RRset with given key, and add the signature record to the
228 signwithkey(dns_name_t *name, dns_rdataset_t *rdataset, dst_key_t *key,
229 dns_ttl_t ttl, dns_diff_t *add, const char *logmsg)
232 isc_stdtime_t jendtime;
233 char keystr[KEY_FORMATSIZE];
234 dns_rdata_t trdata = DNS_RDATA_INIT;
235 unsigned char array[BUFSIZE];
237 dns_difftuple_t *tuple;
239 key_format(key, keystr, sizeof(keystr));
240 vbprintf(1, "\t%s %s\n", logmsg, keystr);
242 jendtime = (jitter != 0) ? isc_random_jitter(endtime, jitter) : endtime;
243 isc_buffer_init(&b, array, sizeof(array));
244 result = dns_dnssec_sign(name, rdataset, key, &starttime, &jendtime,
246 isc_entropy_stopcallbacksources(ectx);
247 if (result != ISC_R_SUCCESS) {
248 char keystr[KEY_FORMATSIZE];
249 key_format(key, keystr, sizeof(keystr));
250 fatal("dnskey '%s' failed to sign data: %s",
251 keystr, isc_result_totext(result));
256 result = dns_dnssec_verify(name, rdataset, key,
257 ISC_TRUE, mctx, &trdata);
258 if (result == ISC_R_SUCCESS) {
259 vbprintf(3, "\tsignature verified\n");
262 vbprintf(3, "\tsignature failed to verify\n");
263 INCSTAT(nverifyfailed);
268 result = dns_difftuple_create(mctx, DNS_DIFFOP_ADD, name, ttl, &trdata,
270 check_result(result, "dns_difftuple_create");
271 dns_diff_append(add, &tuple);
274 static inline isc_boolean_t
275 issigningkey(signer_key_t *key) {
276 return (key->issigningkey);
279 static inline isc_boolean_t
280 iszonekey(signer_key_t *key) {
281 return (ISC_TF(dns_name_equal(dst_key_name(key->key), gorigin) &&
282 dst_key_iszonekey(key->key)));
286 * Find the key if it is in our list. If it is, return it, otherwise null.
287 * No locking is performed here, this must be done by the caller.
289 static signer_key_t *
290 keythatsigned_unlocked(dns_rdata_rrsig_t *rrsig) {
293 key = ISC_LIST_HEAD(keylist);
294 while (key != NULL) {
295 if (rrsig->keyid == dst_key_id(key->key) &&
296 rrsig->algorithm == dst_key_alg(key->key) &&
297 dns_name_equal(&rrsig->signer, dst_key_name(key->key)))
299 key = ISC_LIST_NEXT(key, link);
305 * Finds the key that generated a RRSIG, if possible. First look at the keys
306 * that we've loaded already, and then see if there's a key on disk.
308 static signer_key_t *
309 keythatsigned(dns_rdata_rrsig_t *rrsig) {
311 dst_key_t *pubkey = NULL, *privkey = NULL;
314 isc_rwlock_lock(&keylist_lock, isc_rwlocktype_read);
315 key = keythatsigned_unlocked(rrsig);
316 isc_rwlock_unlock(&keylist_lock, isc_rwlocktype_read);
321 * We did not find the key in our list. Get a write lock now, since
322 * we may be modifying the bits. We could do the tryupgrade() dance,
323 * but instead just get a write lock and check once again to see if
324 * it is on our list. It's possible someone else may have added it
327 isc_rwlock_lock(&keylist_lock, isc_rwlocktype_write);
329 key = keythatsigned_unlocked(rrsig);
331 isc_rwlock_unlock(&keylist_lock, isc_rwlocktype_write);
335 result = dst_key_fromfile(&rrsig->signer, rrsig->keyid,
336 rrsig->algorithm, DST_TYPE_PUBLIC,
337 NULL, mctx, &pubkey);
338 if (result != ISC_R_SUCCESS) {
339 isc_rwlock_unlock(&keylist_lock, isc_rwlocktype_write);
343 result = dst_key_fromfile(&rrsig->signer, rrsig->keyid,
345 DST_TYPE_PUBLIC | DST_TYPE_PRIVATE,
346 NULL, mctx, &privkey);
347 if (result == ISC_R_SUCCESS) {
348 dst_key_free(&pubkey);
349 key = newkeystruct(privkey, ISC_FALSE);
351 key = newkeystruct(pubkey, ISC_FALSE);
352 ISC_LIST_APPEND(keylist, key, link);
354 isc_rwlock_unlock(&keylist_lock, isc_rwlocktype_write);
359 * Check to see if we expect to find a key at this name. If we see a RRSIG
360 * and can't find the signing key that we expect to find, we drop the rrsig.
361 * I'm not sure if this is completely correct, but it seems to work.
364 expecttofindkey(dns_name_t *name) {
365 unsigned int options = DNS_DBFIND_NOWILD;
366 dns_fixedname_t fname;
368 char namestr[DNS_NAME_FORMATSIZE];
370 dns_fixedname_init(&fname);
371 result = dns_db_find(gdb, name, gversion, dns_rdatatype_dnskey, options,
372 0, NULL, dns_fixedname_name(&fname), NULL, NULL);
378 case DNS_R_DELEGATION:
383 dns_name_format(name, namestr, sizeof(namestr));
384 fatal("failure looking for '%s DNSKEY' in database: %s",
385 namestr, isc_result_totext(result));
386 return (ISC_FALSE); /* removes a warning */
389 static inline isc_boolean_t
390 setverifies(dns_name_t *name, dns_rdataset_t *set, signer_key_t *key,
394 result = dns_dnssec_verify(name, set, key->key, ISC_FALSE, mctx, rrsig);
395 if (result == ISC_R_SUCCESS) {
399 INCSTAT(nverifyfailed);
405 * Signs a set. Goes through contortions to decide if each RRSIG should
406 * be dropped or retained, and then determines if any new SIGs need to
410 signset(dns_diff_t *del, dns_diff_t *add, dns_dbnode_t *node, dns_name_t *name,
413 dns_rdataset_t sigset;
414 dns_rdata_t sigrdata = DNS_RDATA_INIT;
415 dns_rdata_rrsig_t rrsig;
418 isc_boolean_t nosigs = ISC_FALSE;
419 isc_boolean_t *wassignedby, *nowsignedby;
421 dns_difftuple_t *tuple;
424 char namestr[DNS_NAME_FORMATSIZE];
425 char typestr[TYPE_FORMATSIZE];
426 char sigstr[SIG_FORMATSIZE];
428 dns_name_format(name, namestr, sizeof(namestr));
429 type_format(set->type, typestr, sizeof(typestr));
431 ttl = ISC_MIN(set->ttl, endtime - starttime);
433 dns_rdataset_init(&sigset);
434 result = dns_db_findrdataset(gdb, node, gversion, dns_rdatatype_rrsig,
435 set->type, 0, &sigset, NULL);
436 if (result == ISC_R_NOTFOUND) {
437 result = ISC_R_SUCCESS;
440 if (result != ISC_R_SUCCESS)
441 fatal("failed while looking for '%s RRSIG %s': %s",
442 namestr, typestr, isc_result_totext(result));
444 vbprintf(1, "%s/%s:\n", namestr, typestr);
446 arraysize = keycount;
448 arraysize += dns_rdataset_count(&sigset);
449 wassignedby = isc_mem_get(mctx, arraysize * sizeof(isc_boolean_t));
450 nowsignedby = isc_mem_get(mctx, arraysize * sizeof(isc_boolean_t));
451 if (wassignedby == NULL || nowsignedby == NULL)
452 fatal("out of memory");
454 for (i = 0; i < arraysize; i++)
455 wassignedby[i] = nowsignedby[i] = ISC_FALSE;
458 result = ISC_R_NOMORE;
460 result = dns_rdataset_first(&sigset);
462 while (result == ISC_R_SUCCESS) {
463 isc_boolean_t expired, future;
464 isc_boolean_t keep = ISC_FALSE, resign = ISC_FALSE;
466 dns_rdataset_current(&sigset, &sigrdata);
468 result = dns_rdata_tostruct(&sigrdata, &rrsig, NULL);
469 check_result(result, "dns_rdata_tostruct");
471 future = isc_serial_lt(now, rrsig.timesigned);
473 key = keythatsigned(&rrsig);
474 sig_format(&rrsig, sigstr, sizeof(sigstr));
475 if (key != NULL && issigningkey(key))
476 expired = isc_serial_gt(now + cycle, rrsig.timeexpire);
478 expired = isc_serial_gt(now, rrsig.timeexpire);
480 if (isc_serial_gt(rrsig.timesigned, rrsig.timeexpire)) {
481 /* rrsig is dropped and not replaced */
482 vbprintf(2, "\trrsig by %s dropped - "
483 "invalid validity period\n",
485 } else if (key == NULL && !future &&
486 expecttofindkey(&rrsig.signer))
488 /* rrsig is dropped and not replaced */
489 vbprintf(2, "\trrsig by %s dropped - "
490 "private dnskey not found\n",
492 } else if (key == NULL || future) {
493 vbprintf(2, "\trrsig by %s %s - dnskey not found\n",
494 expired ? "retained" : "dropped", sigstr);
497 } else if (issigningkey(key)) {
498 if (!expired && setverifies(name, set, key, &sigrdata))
500 vbprintf(2, "\trrsig by %s retained\n", sigstr);
502 wassignedby[key->position] = ISC_TRUE;
503 nowsignedby[key->position] = ISC_TRUE;
504 key->wasused = ISC_TRUE;
506 vbprintf(2, "\trrsig by %s dropped - %s\n",
508 expired ? "expired" :
510 wassignedby[key->position] = ISC_TRUE;
513 } else if (iszonekey(key)) {
514 if (!expired && setverifies(name, set, key, &sigrdata))
516 vbprintf(2, "\trrsig by %s retained\n", sigstr);
518 wassignedby[key->position] = ISC_TRUE;
519 nowsignedby[key->position] = ISC_TRUE;
520 key->wasused = ISC_TRUE;
522 vbprintf(2, "\trrsig by %s dropped - %s\n",
524 expired ? "expired" :
526 wassignedby[key->position] = ISC_TRUE;
528 } else if (!expired) {
529 vbprintf(2, "\trrsig by %s retained\n", sigstr);
532 vbprintf(2, "\trrsig by %s expired\n", sigstr);
536 nowsignedby[key->position] = ISC_TRUE;
538 if (sigset.ttl != ttl) {
539 vbprintf(2, "\tfixing ttl %s\n", sigstr);
541 result = dns_difftuple_create(mctx,
546 check_result(result, "dns_difftuple_create");
547 dns_diff_append(del, &tuple);
548 result = dns_difftuple_create(mctx,
553 check_result(result, "dns_difftuple_create");
554 dns_diff_append(add, &tuple);
558 result = dns_difftuple_create(mctx, DNS_DIFFOP_DEL,
561 check_result(result, "dns_difftuple_create");
562 dns_diff_append(del, &tuple);
569 signwithkey(name, set, key->key, ttl, add,
570 "resigning with dnskey");
571 nowsignedby[key->position] = ISC_TRUE;
572 key->wasused = ISC_TRUE;
575 dns_rdata_reset(&sigrdata);
576 dns_rdata_freestruct(&rrsig);
577 result = dns_rdataset_next(&sigset);
579 if (result == ISC_R_NOMORE)
580 result = ISC_R_SUCCESS;
582 check_result(result, "dns_rdataset_first/next");
583 if (dns_rdataset_isassociated(&sigset))
584 dns_rdataset_disassociate(&sigset);
586 for (key = ISC_LIST_HEAD(keylist);
588 key = ISC_LIST_NEXT(key, link))
590 if (nowsignedby[key->position])
593 if (!key->issigningkey)
595 if (!(ignoreksk || key->isdsk ||
597 set->type == dns_rdatatype_dnskey &&
598 dns_name_equal(name, gorigin))))
601 signwithkey(name, set, key->key, ttl, add,
602 "signing with dnskey");
603 key->wasused = ISC_TRUE;
606 isc_mem_put(mctx, wassignedby, arraysize * sizeof(isc_boolean_t));
607 isc_mem_put(mctx, nowsignedby, arraysize * sizeof(isc_boolean_t));
611 unsigned char *hashbuf;
618 hashlist_init(hashlist_t *l, unsigned int nodes, unsigned int length) {
621 l->length = length + 1;
625 l->hashbuf = malloc(l->size * l->length);
626 if (l->hashbuf == NULL)
635 hashlist_add(hashlist_t *l, const unsigned char *hash, size_t len)
638 REQUIRE(len <= l->length);
640 if (l->entries == l->size) {
641 l->size = l->size * 2 + 100;
642 l->hashbuf = realloc(l->hashbuf, l->size * l->length);
644 memset(l->hashbuf + l->entries * l->length, 0, l->length);
645 memcpy(l->hashbuf + l->entries * l->length, hash, len);
650 hashlist_add_dns_name(hashlist_t *l, /*const*/ dns_name_t *name,
651 unsigned int hashalg, unsigned int iterations,
652 const unsigned char *salt, size_t salt_length,
653 isc_boolean_t speculative)
655 char nametext[DNS_NAME_FORMATSIZE];
656 unsigned char hash[NSEC3_MAX_HASH_LENGTH + 1];
660 len = isc_iterated_hash(hash, hashalg, iterations, salt, salt_length,
661 name->ndata, name->length);
663 dns_name_format(name, nametext, sizeof nametext);
664 for (i = 0 ; i < len; i++)
665 fprintf(stderr, "%02x", hash[i]);
666 fprintf(stderr, " %s\n", nametext);
668 hash[len++] = speculative ? 1 : 0;
669 hashlist_add(l, hash, len);
673 hashlist_comp(const void *a, const void *b) {
674 return (memcmp(a, b, hash_length + 1));
678 hashlist_sort(hashlist_t *l) {
679 qsort(l->hashbuf, l->entries, l->length, hashlist_comp);
683 hashlist_hasdup(hashlist_t *l) {
684 unsigned char *current;
685 unsigned char *next = l->hashbuf;
686 size_t entries = l->entries;
689 * Skip initial speculative wild card hashs.
691 while (entries > 0U && next[l->length-1] != 0U) {
697 while (entries-- > 1U) {
699 if (next[l->length-1] != 0)
701 if (memcmp(current, next, l->length - 1) == 0)
708 static const unsigned char *
709 hashlist_findnext(const hashlist_t *l,
710 const unsigned char hash[NSEC3_MAX_HASH_LENGTH])
712 unsigned int entries = l->entries;
713 const unsigned char *next = bsearch(hash, l->hashbuf, l->entries,
714 l->length, hashlist_comp);
715 INSIST(next != NULL);
718 if (next < l->hashbuf + (l->entries - 1) * l->length)
722 if (next[l->length - 1] == 0)
724 } while (entries-- > 1);
725 INSIST(entries != 0);
730 hashlist_exists(const hashlist_t *l,
731 const unsigned char hash[NSEC3_MAX_HASH_LENGTH])
733 if (bsearch(hash, l->hashbuf, l->entries, l->length, hashlist_comp))
740 addnowildcardhash(hashlist_t *l, /*const*/ dns_name_t *name,
741 unsigned int hashalg, unsigned int iterations,
742 const unsigned char *salt, size_t salt_length)
744 dns_fixedname_t fixed;
746 dns_dbnode_t *node = NULL;
748 char namestr[DNS_NAME_FORMATSIZE];
750 dns_fixedname_init(&fixed);
751 wild = dns_fixedname_name(&fixed);
753 result = dns_name_concatenate(dns_wildcardname, name, wild, NULL);
754 if (result == ISC_R_NOSPACE)
756 check_result(result,"addnowildcardhash: dns_name_concatenate()");
758 result = dns_db_findnode(gdb, wild, ISC_FALSE, &node);
759 if (result == ISC_R_SUCCESS) {
760 dns_db_detachnode(gdb, &node);
765 dns_name_format(wild, namestr, sizeof(namestr));
766 fprintf(stderr, "adding no-wildcardhash for %s\n", namestr);
769 hashlist_add_dns_name(l, wild, hashalg, iterations, salt, salt_length,
774 opendb(const char *prefix, dns_name_t *name, dns_rdataclass_t rdclass,
781 isc_buffer_init(&b, filename, sizeof(filename));
782 if (directory != NULL) {
783 isc_buffer_putstr(&b, directory);
784 if (directory[strlen(directory) - 1] != '/')
785 isc_buffer_putstr(&b, "/");
787 isc_buffer_putstr(&b, prefix);
788 result = dns_name_tofilenametext(name, ISC_FALSE, &b);
789 check_result(result, "dns_name_tofilenametext()");
790 if (isc_buffer_availablelength(&b) == 0) {
791 char namestr[DNS_NAME_FORMATSIZE];
792 dns_name_format(name, namestr, sizeof(namestr));
793 fatal("name '%s' is too long", namestr);
795 isc_buffer_putuint8(&b, 0);
797 result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone,
798 rdclass, 0, NULL, dbp);
799 check_result(result, "dns_db_create()");
801 result = dns_db_load(*dbp, filename);
802 if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
807 * Loads the key set for a child zone, if there is one, and builds DS records.
810 loadds(dns_name_t *name, isc_uint32_t ttl, dns_rdataset_t *dsset) {
812 dns_dbversion_t *ver = NULL;
813 dns_dbnode_t *node = NULL;
815 dns_rdataset_t keyset;
817 unsigned char dsbuf[DNS_DS_BUFFERSIZE];
819 dns_difftuple_t *tuple = NULL;
821 opendb("keyset-", name, gclass, &db);
823 return (ISC_R_NOTFOUND);
825 result = dns_db_findnode(db, name, ISC_FALSE, &node);
826 if (result != ISC_R_SUCCESS) {
828 return (DNS_R_BADDB);
830 dns_rdataset_init(&keyset);
831 result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_dnskey, 0,
833 if (result != ISC_R_SUCCESS) {
834 dns_db_detachnode(db, &node);
839 vbprintf(2, "found DNSKEY records\n");
841 result = dns_db_newversion(db, &ver);
842 check_result(result, "dns_db_newversion");
844 dns_diff_init(mctx, &diff);
846 for (result = dns_rdataset_first(&keyset);
847 result == ISC_R_SUCCESS;
848 result = dns_rdataset_next(&keyset))
850 dns_rdata_init(&key);
852 dns_rdataset_current(&keyset, &key);
853 result = dns_ds_buildrdata(name, &key, DNS_DSDIGEST_SHA1,
855 check_result(result, "dns_ds_buildrdata");
857 result = dns_difftuple_create(mctx, DNS_DIFFOP_ADD, name,
859 check_result(result, "dns_difftuple_create");
860 dns_diff_append(&diff, &tuple);
862 dns_rdata_reset(&ds);
863 result = dns_ds_buildrdata(name, &key, DNS_DSDIGEST_SHA256,
865 check_result(result, "dns_ds_buildrdata");
867 result = dns_difftuple_create(mctx, DNS_DIFFOP_ADD, name,
869 check_result(result, "dns_difftuple_create");
870 dns_diff_append(&diff, &tuple);
872 result = dns_diff_apply(&diff, db, ver);
873 check_result(result, "dns_diff_apply");
874 dns_diff_clear(&diff);
876 dns_db_closeversion(db, &ver, ISC_TRUE);
878 result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_ds, 0, 0,
880 check_result(result, "dns_db_findrdataset");
882 dns_rdataset_disassociate(&keyset);
883 dns_db_detachnode(db, &node);
889 delegation(dns_name_t *name, dns_dbnode_t *node, isc_uint32_t *ttlp) {
890 dns_rdataset_t nsset;
893 if (dns_name_equal(name, gorigin))
896 dns_rdataset_init(&nsset);
897 result = dns_db_findrdataset(gdb, node, gversion, dns_rdatatype_ns,
899 if (dns_rdataset_isassociated(&nsset)) {
902 dns_rdataset_disassociate(&nsset);
905 return (ISC_TF(result == ISC_R_SUCCESS));
909 secure(dns_name_t *name, dns_dbnode_t *node) {
910 dns_rdataset_t dsset;
913 if (dns_name_equal(name, gorigin))
916 dns_rdataset_init(&dsset);
917 result = dns_db_findrdataset(gdb, node, gversion, dns_rdatatype_ds,
919 if (dns_rdataset_isassociated(&dsset))
920 dns_rdataset_disassociate(&dsset);
922 return (ISC_TF(result == ISC_R_SUCCESS));
926 * Signs all records at a name.
929 signname(dns_dbnode_t *node, dns_name_t *name) {
931 dns_rdataset_t rdataset;
932 dns_rdatasetiter_t *rdsiter;
933 isc_boolean_t isdelegation = ISC_FALSE;
935 char namestr[DNS_NAME_FORMATSIZE];
937 dns_rdataset_init(&rdataset);
938 dns_name_format(name, namestr, sizeof(namestr));
941 * Determine if this is a delegation point.
943 if (delegation(name, node, NULL))
944 isdelegation = ISC_TRUE;
947 * Now iterate through the rdatasets.
949 dns_diff_init(mctx, &del);
950 dns_diff_init(mctx, &add);
952 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
953 check_result(result, "dns_db_allrdatasets()");
954 result = dns_rdatasetiter_first(rdsiter);
955 while (result == ISC_R_SUCCESS) {
956 dns_rdatasetiter_current(rdsiter, &rdataset);
958 /* If this is a RRSIG set, skip it. */
959 if (rdataset.type == dns_rdatatype_rrsig)
963 * If this name is a delegation point, skip all records
964 * except NSEC and DS sets. Otherwise check that there
968 if (rdataset.type != nsec_datatype &&
969 rdataset.type != dns_rdatatype_ds)
971 } else if (rdataset.type == dns_rdatatype_ds) {
972 char namebuf[DNS_NAME_FORMATSIZE];
973 dns_name_format(name, namebuf, sizeof(namebuf));
974 fatal("'%s': found DS RRset without NS RRset\n",
978 signset(&del, &add, node, name, &rdataset);
981 dns_rdataset_disassociate(&rdataset);
982 result = dns_rdatasetiter_next(rdsiter);
984 if (result != ISC_R_NOMORE)
985 fatal("rdataset iteration for name '%s' failed: %s",
986 namestr, isc_result_totext(result));
988 dns_rdatasetiter_destroy(&rdsiter);
990 result = dns_diff_applysilently(&del, gdb, gversion);
991 if (result != ISC_R_SUCCESS)
992 fatal("failed to delete SIGs at node '%s': %s",
993 namestr, isc_result_totext(result));
995 result = dns_diff_applysilently(&add, gdb, gversion);
996 if (result != ISC_R_SUCCESS)
997 fatal("failed to add SIGs at node '%s': %s",
998 namestr, isc_result_totext(result));
1000 dns_diff_clear(&del);
1001 dns_diff_clear(&add);
1004 static inline isc_boolean_t
1005 active_node(dns_dbnode_t *node) {
1006 dns_rdatasetiter_t *rdsiter = NULL;
1007 dns_rdatasetiter_t *rdsiter2 = NULL;
1008 isc_boolean_t active = ISC_FALSE;
1009 isc_result_t result;
1010 dns_rdataset_t rdataset;
1011 dns_rdatatype_t type;
1012 dns_rdatatype_t covers;
1013 isc_boolean_t found;
1015 dns_rdataset_init(&rdataset);
1016 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
1017 check_result(result, "dns_db_allrdatasets()");
1018 result = dns_rdatasetiter_first(rdsiter);
1019 while (result == ISC_R_SUCCESS) {
1020 dns_rdatasetiter_current(rdsiter, &rdataset);
1021 if (rdataset.type != dns_rdatatype_nsec &&
1022 rdataset.type != dns_rdatatype_nsec3 &&
1023 rdataset.type != dns_rdatatype_rrsig)
1025 dns_rdataset_disassociate(&rdataset);
1027 result = dns_rdatasetiter_next(rdsiter);
1029 result = ISC_R_NOMORE;
1031 if (result != ISC_R_NOMORE)
1032 fatal("rdataset iteration failed: %s",
1033 isc_result_totext(result));
1035 if (!active && nsec_datatype == dns_rdatatype_nsec) {
1037 * The node is empty of everything but NSEC / RRSIG records.
1039 for (result = dns_rdatasetiter_first(rdsiter);
1040 result == ISC_R_SUCCESS;
1041 result = dns_rdatasetiter_next(rdsiter)) {
1042 dns_rdatasetiter_current(rdsiter, &rdataset);
1043 result = dns_db_deleterdataset(gdb, node, gversion,
1046 check_result(result, "dns_db_deleterdataset()");
1047 dns_rdataset_disassociate(&rdataset);
1049 if (result != ISC_R_NOMORE)
1050 fatal("rdataset iteration failed: %s",
1051 isc_result_totext(result));
1054 * Delete RRSIGs for types that no longer exist.
1056 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter2);
1057 check_result(result, "dns_db_allrdatasets()");
1058 for (result = dns_rdatasetiter_first(rdsiter);
1059 result == ISC_R_SUCCESS;
1060 result = dns_rdatasetiter_next(rdsiter)) {
1061 dns_rdatasetiter_current(rdsiter, &rdataset);
1062 type = rdataset.type;
1063 covers = rdataset.covers;
1064 dns_rdataset_disassociate(&rdataset);
1066 * Delete the NSEC chain if we are signing with
1069 if (nsec_datatype == dns_rdatatype_nsec3 &&
1070 (type == dns_rdatatype_nsec ||
1071 covers == dns_rdatatype_nsec)) {
1072 result = dns_db_deleterdataset(gdb, node,
1075 check_result(result,
1076 "dns_db_deleterdataset(nsec/rrsig)");
1079 if (type != dns_rdatatype_rrsig)
1082 for (result = dns_rdatasetiter_first(rdsiter2);
1083 !found && result == ISC_R_SUCCESS;
1084 result = dns_rdatasetiter_next(rdsiter2)) {
1085 dns_rdatasetiter_current(rdsiter2, &rdataset);
1086 if (rdataset.type == covers)
1088 dns_rdataset_disassociate(&rdataset);
1091 if (result != ISC_R_NOMORE)
1092 fatal("rdataset iteration failed: %s",
1093 isc_result_totext(result));
1094 result = dns_db_deleterdataset(gdb, node,
1097 check_result(result,
1098 "dns_db_deleterdataset(rrsig)");
1099 } else if (result != ISC_R_NOMORE &&
1100 result != ISC_R_SUCCESS)
1101 fatal("rdataset iteration failed: %s",
1102 isc_result_totext(result));
1104 if (result != ISC_R_NOMORE)
1105 fatal("rdataset iteration failed: %s",
1106 isc_result_totext(result));
1107 dns_rdatasetiter_destroy(&rdsiter2);
1109 dns_rdatasetiter_destroy(&rdsiter);
1115 * Extracts the TTL from the SOA.
1119 dns_rdataset_t soaset;
1120 dns_fixedname_t fname;
1122 isc_result_t result;
1124 dns_rdata_t rdata = DNS_RDATA_INIT;
1125 dns_rdata_soa_t soa;
1127 dns_fixedname_init(&fname);
1128 name = dns_fixedname_name(&fname);
1129 dns_rdataset_init(&soaset);
1130 result = dns_db_find(gdb, gorigin, gversion, dns_rdatatype_soa,
1131 0, 0, NULL, name, &soaset, NULL);
1132 if (result != ISC_R_SUCCESS)
1133 fatal("failed to find an SOA at the zone apex: %s",
1134 isc_result_totext(result));
1136 result = dns_rdataset_first(&soaset);
1137 check_result(result, "dns_rdataset_first");
1138 dns_rdataset_current(&soaset, &rdata);
1139 result = dns_rdata_tostruct(&rdata, &soa, NULL);
1140 check_result(result, "dns_rdata_tostruct");
1142 dns_rdataset_disassociate(&soaset);
1147 * Increment (or set if nonzero) the SOA serial
1150 setsoaserial(isc_uint32_t serial) {
1151 isc_result_t result;
1152 dns_dbnode_t *node = NULL;
1153 dns_rdataset_t rdataset;
1154 dns_rdata_t rdata = DNS_RDATA_INIT;
1155 isc_uint32_t old_serial, new_serial;
1157 result = dns_db_getoriginnode(gdb, &node);
1158 if (result != ISC_R_SUCCESS)
1161 dns_rdataset_init(&rdataset);
1163 result = dns_db_findrdataset(gdb, node, gversion,
1164 dns_rdatatype_soa, 0,
1165 0, &rdataset, NULL);
1166 if (result != ISC_R_SUCCESS)
1169 result = dns_rdataset_first(&rdataset);
1170 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1172 dns_rdataset_current(&rdataset, &rdata);
1174 old_serial = dns_soa_getserial(&rdata);
1177 /* Set SOA serial to the value provided. */
1178 new_serial = serial;
1180 /* Increment SOA serial using RFC 1982 arithmetics */
1181 new_serial = (old_serial + 1) & 0xFFFFFFFF;
1182 if (new_serial == 0)
1186 /* If the new serial is not likely to cause a zone transfer
1187 * (a/ixfr) from servers having the old serial, warn the user.
1189 * RFC1982 section 7 defines the maximum increment to be
1190 * (2^(32-1))-1. Using u_int32_t arithmetic, we can do a single
1191 * comparison. (5 - 6 == (2^32)-1, not negative-one)
1193 if (new_serial == old_serial ||
1194 (new_serial - old_serial) > 0x7fffffffU)
1195 fprintf(stderr, "%s: warning: Serial number not advanced, "
1196 "zone may not transfer\n", program);
1198 dns_soa_setserial(new_serial, &rdata);
1200 result = dns_db_deleterdataset(gdb, node, gversion,
1201 dns_rdatatype_soa, 0);
1202 check_result(result, "dns_db_deleterdataset");
1203 if (result != ISC_R_SUCCESS)
1206 result = dns_db_addrdataset(gdb, node, gversion,
1207 0, &rdataset, 0, NULL);
1208 check_result(result, "dns_db_addrdataset");
1209 if (result != ISC_R_SUCCESS)
1213 dns_rdataset_disassociate(&rdataset);
1215 dns_db_detachnode(gdb, &node);
1216 dns_rdata_reset(&rdata);
1222 * Delete any RRSIG records at a node.
1225 cleannode(dns_db_t *db, dns_dbversion_t *version, dns_dbnode_t *node) {
1226 dns_rdatasetiter_t *rdsiter = NULL;
1228 isc_result_t result, dresult;
1230 if (outputformat != dns_masterformat_text || !disable_zone_check)
1233 dns_rdataset_init(&set);
1234 result = dns_db_allrdatasets(db, node, version, 0, &rdsiter);
1235 check_result(result, "dns_db_allrdatasets");
1236 result = dns_rdatasetiter_first(rdsiter);
1237 while (result == ISC_R_SUCCESS) {
1238 isc_boolean_t destroy = ISC_FALSE;
1239 dns_rdatatype_t covers = 0;
1240 dns_rdatasetiter_current(rdsiter, &set);
1241 if (set.type == dns_rdatatype_rrsig) {
1242 covers = set.covers;
1245 dns_rdataset_disassociate(&set);
1246 result = dns_rdatasetiter_next(rdsiter);
1248 dresult = dns_db_deleterdataset(db, node, version,
1249 dns_rdatatype_rrsig,
1251 check_result(dresult, "dns_db_deleterdataset");
1254 if (result != ISC_R_NOMORE)
1255 fatal("rdataset iteration failed: %s",
1256 isc_result_totext(result));
1257 dns_rdatasetiter_destroy(&rdsiter);
1261 * Set up the iterator and global state before starting the tasks.
1265 isc_result_t result;
1268 result = dns_db_createiterator(gdb, 0, &gdbiter);
1269 check_result(result, "dns_db_createiterator()");
1273 * Clean up the iterator and global state after the tasks complete.
1277 dns_dbiterator_destroy(&gdbiter);
1280 static isc_boolean_t
1281 goodsig(dns_rdata_t *sigrdata, dns_name_t *name, dns_rdataset_t *keyrdataset,
1282 dns_rdataset_t *rdataset)
1284 dns_rdata_dnskey_t key;
1285 dns_rdata_rrsig_t sig;
1286 dst_key_t *dstkey = NULL;
1287 isc_result_t result;
1289 dns_rdata_tostruct(sigrdata, &sig, NULL);
1291 for (result = dns_rdataset_first(keyrdataset);
1292 result == ISC_R_SUCCESS;
1293 result = dns_rdataset_next(keyrdataset)) {
1294 dns_rdata_t rdata = DNS_RDATA_INIT;
1295 dns_rdataset_current(keyrdataset, &rdata);
1296 dns_rdata_tostruct(&rdata, &key, NULL);
1297 result = dns_dnssec_keyfromrdata(gorigin, &rdata, mctx,
1299 if (result != ISC_R_SUCCESS)
1301 if (sig.algorithm != key.algorithm ||
1302 sig.keyid != dst_key_id(dstkey) ||
1303 !dns_name_equal(&sig.signer, gorigin)) {
1304 dst_key_free(&dstkey);
1307 result = dns_dnssec_verify(name, rdataset, dstkey, ISC_FALSE,
1309 dst_key_free(&dstkey);
1310 if (result == ISC_R_SUCCESS)
1317 verifyset(dns_rdataset_t *rdataset, dns_name_t *name, dns_dbnode_t *node,
1318 dns_rdataset_t *keyrdataset, unsigned char *ksk_algorithms,
1319 unsigned char *bad_algorithms)
1321 unsigned char set_algorithms[256];
1322 char namebuf[DNS_NAME_FORMATSIZE];
1325 dns_rdataset_t sigrdataset;
1326 dns_rdatasetiter_t *rdsiter = NULL;
1327 isc_result_t result;
1330 dns_rdataset_init(&sigrdataset);
1331 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
1332 check_result(result, "dns_db_allrdatasets()");
1333 for (result = dns_rdatasetiter_first(rdsiter);
1334 result == ISC_R_SUCCESS;
1335 result = dns_rdatasetiter_next(rdsiter)) {
1336 dns_rdatasetiter_current(rdsiter, &sigrdataset);
1337 if (sigrdataset.type == dns_rdatatype_rrsig &&
1338 sigrdataset.covers == rdataset->type)
1340 dns_rdataset_disassociate(&sigrdataset);
1342 if (result != ISC_R_SUCCESS) {
1343 dns_name_format(name, namebuf, sizeof(namebuf));
1344 type_format(rdataset->type, typebuf, sizeof(typebuf));
1345 fprintf(stderr, "no signatures for %s/%s\n", namebuf, typebuf);
1346 for (i = 0; i < 256; i++)
1347 if (ksk_algorithms[i] != 0)
1348 bad_algorithms[i] = 1;
1352 memset(set_algorithms, 0, sizeof(set_algorithms));
1353 for (result = dns_rdataset_first(&sigrdataset);
1354 result == ISC_R_SUCCESS;
1355 result = dns_rdataset_next(&sigrdataset)) {
1356 dns_rdata_t rdata = DNS_RDATA_INIT;
1357 dns_rdata_rrsig_t sig;
1359 dns_rdataset_current(&sigrdataset, &rdata);
1360 dns_rdata_tostruct(&rdata, &sig, NULL);
1361 if ((set_algorithms[sig.algorithm] != 0) ||
1362 (ksk_algorithms[sig.algorithm] == 0))
1364 if (goodsig(&rdata, name, keyrdataset, rdataset))
1365 set_algorithms[sig.algorithm] = 1;
1367 dns_rdatasetiter_destroy(&rdsiter);
1368 if (memcmp(set_algorithms, ksk_algorithms, sizeof(set_algorithms))) {
1369 dns_name_format(name, namebuf, sizeof(namebuf));
1370 type_format(rdataset->type, typebuf, sizeof(typebuf));
1371 for (i = 0; i < 256; i++)
1372 if ((ksk_algorithms[i] != 0) &&
1373 (set_algorithms[i] == 0)) {
1374 alg_format(i, algbuf, sizeof(algbuf));
1375 fprintf(stderr, "Missing %s signature for "
1376 "%s %s\n", algbuf, namebuf, typebuf);
1377 bad_algorithms[i] = 1;
1380 dns_rdataset_disassociate(&sigrdataset);
1384 verifynode(dns_name_t *name, dns_dbnode_t *node, isc_boolean_t delegation,
1385 dns_rdataset_t *keyrdataset, unsigned char *ksk_algorithms,
1386 unsigned char *bad_algorithms)
1388 dns_rdataset_t rdataset;
1389 dns_rdatasetiter_t *rdsiter = NULL;
1390 isc_result_t result;
1392 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
1393 check_result(result, "dns_db_allrdatasets()");
1394 result = dns_rdatasetiter_first(rdsiter);
1395 dns_rdataset_init(&rdataset);
1396 while (result == ISC_R_SUCCESS) {
1397 dns_rdatasetiter_current(rdsiter, &rdataset);
1398 if (rdataset.type != dns_rdatatype_rrsig &&
1399 rdataset.type != dns_rdatatype_dnskey &&
1400 (!delegation || rdataset.type == dns_rdatatype_ds ||
1401 rdataset.type == dns_rdatatype_nsec)) {
1402 verifyset(&rdataset, name, node, keyrdataset,
1403 ksk_algorithms, bad_algorithms);
1405 dns_rdataset_disassociate(&rdataset);
1406 result = dns_rdatasetiter_next(rdsiter);
1408 if (result != ISC_R_NOMORE)
1409 fatal("rdataset iteration failed: %s",
1410 isc_result_totext(result));
1411 dns_rdatasetiter_destroy(&rdsiter);
1415 * Verify that certain things are sane:
1417 * The apex has a DNSKEY record with at least one KSK and at least
1420 * The DNSKEY record was signed with at least one of the KSKs in this
1423 * The rest of the zone was signed with at least one of the ZSKs
1424 * present in the DNSKEY RRSET.
1429 dns_dbiterator_t *dbiter = NULL;
1430 dns_dbnode_t *node = NULL, *nextnode = NULL;
1431 dns_fixedname_t fname, fnextname, fzonecut;
1432 dns_name_t *name, *nextname, *zonecut;
1433 dns_rdata_dnskey_t dnskey;
1434 dns_rdata_t rdata = DNS_RDATA_INIT;
1435 dns_rdataset_t rdataset;
1436 dns_rdataset_t sigrdataset;
1438 isc_boolean_t done = ISC_FALSE;
1439 isc_boolean_t first = ISC_TRUE;
1440 isc_boolean_t goodksk = ISC_FALSE;
1441 isc_boolean_t goodzsk = ISC_FALSE;
1442 isc_result_t result;
1443 unsigned char revoked[256];
1444 unsigned char standby[256];
1445 unsigned char ksk_algorithms[256];
1446 unsigned char zsk_algorithms[256];
1447 unsigned char bad_algorithms[256];
1448 #ifdef ALLOW_KSKLESS_ZONES
1449 isc_boolean_t allzsksigned = ISC_TRUE;
1450 unsigned char self_algorithms[256];
1453 if (disable_zone_check)
1456 result = dns_db_findnode(gdb, gorigin, ISC_FALSE, &node);
1457 if (result != ISC_R_SUCCESS)
1458 fatal("failed to find the zone's origin: %s",
1459 isc_result_totext(result));
1461 dns_rdataset_init(&rdataset);
1462 dns_rdataset_init(&sigrdataset);
1463 result = dns_db_findrdataset(gdb, node, gversion,
1464 dns_rdatatype_dnskey,
1465 0, 0, &rdataset, &sigrdataset);
1466 dns_db_detachnode(gdb, &node);
1467 if (result != ISC_R_SUCCESS)
1468 fatal("cannot find DNSKEY rrset\n");
1470 if (!dns_rdataset_isassociated(&sigrdataset))
1471 fatal("cannot find DNSKEY RRSIGs\n");
1473 memset(revoked, 0, sizeof(revoked));
1474 memset(standby, 0, sizeof(revoked));
1475 memset(ksk_algorithms, 0, sizeof(ksk_algorithms));
1476 memset(zsk_algorithms, 0, sizeof(zsk_algorithms));
1477 memset(bad_algorithms, 0, sizeof(bad_algorithms));
1478 #ifdef ALLOW_KSKLESS_ZONES
1479 memset(self_algorithms, 0, sizeof(self_algorithms));
1483 * Check that the DNSKEY RR has at least one self signing KSK and
1484 * one ZSK per algorithm in it.
1486 for (result = dns_rdataset_first(&rdataset);
1487 result == ISC_R_SUCCESS;
1488 result = dns_rdataset_next(&rdataset)) {
1489 dns_rdataset_current(&rdataset, &rdata);
1490 result = dns_rdata_tostruct(&rdata, &dnskey, NULL);
1491 check_result(result, "dns_rdata_tostruct");
1493 if ((dnskey.flags & DNS_KEYOWNER_ZONE) == 0)
1495 else if ((dnskey.flags & DNS_KEYFLAG_REVOKE) != 0) {
1496 if ((dnskey.flags & DNS_KEYFLAG_KSK) != 0 &&
1497 !dns_dnssec_selfsigns(&rdata, gorigin, &rdataset,
1498 &sigrdataset, ISC_FALSE,
1500 char namebuf[DNS_NAME_FORMATSIZE];
1504 dns_name_format(gorigin, namebuf,
1506 isc_buffer_init(&buf, buffer, sizeof(buffer));
1507 result = dns_rdata_totext(&rdata, NULL, &buf);
1508 check_result(result, "dns_rdata_totext");
1509 fatal("revoked KSK is not self signed:\n"
1510 "%s DNSKEY %.*s", namebuf,
1511 (int)isc_buffer_usedlength(&buf), buffer);
1513 if ((dnskey.flags & DNS_KEYFLAG_KSK) != 0 &&
1514 revoked[dnskey.algorithm] != 255)
1515 revoked[dnskey.algorithm]++;
1516 } else if ((dnskey.flags & DNS_KEYFLAG_KSK) != 0) {
1517 if (dns_dnssec_selfsigns(&rdata, gorigin, &rdataset,
1518 &sigrdataset, ISC_FALSE, mctx)) {
1519 if (ksk_algorithms[dnskey.algorithm] != 255)
1520 ksk_algorithms[dnskey.algorithm]++;
1523 if (standby[dnskey.algorithm] != 255)
1524 standby[dnskey.algorithm]++;
1526 } else if (dns_dnssec_selfsigns(&rdata, gorigin, &rdataset,
1527 &sigrdataset, ISC_FALSE,
1529 #ifdef ALLOW_KSKLESS_ZONES
1530 if (self_algorithms[dnskey.algorithm] != 255)
1531 self_algorithms[dnskey.algorithm]++;
1533 if (zsk_algorithms[dnskey.algorithm] != 255)
1534 zsk_algorithms[dnskey.algorithm]++;
1537 if (zsk_algorithms[dnskey.algorithm] != 255)
1538 zsk_algorithms[dnskey.algorithm]++;
1539 #ifdef ALLOW_KSKLESS_ZONES
1540 allzsksigned = ISC_FALSE;
1543 dns_rdata_freestruct(&dnskey);
1544 dns_rdata_reset(&rdata);
1546 dns_rdataset_disassociate(&sigrdataset);
1549 #ifdef ALLOW_KSKLESS_ZONES
1551 fatal("no self signing keys found");
1552 fprintf(stderr, "No self signing KSK found. Using self signed "
1553 "ZSK's for active algorithm list.\n");
1554 memcpy(ksk_algorithms, self_algorithms, sizeof(ksk_algorithms));
1556 fprintf(stderr, "warning: not all ZSK's are self "
1559 fatal("no self signed KSK's found");
1563 fprintf(stderr, "Verifying the zone using the following algorithms:");
1564 for (i = 0; i < 256; i++) {
1565 if (ksk_algorithms[i] != 0) {
1566 alg_format(i, algbuf, sizeof(algbuf));
1567 fprintf(stderr, " %s", algbuf);
1570 fprintf(stderr, ".\n");
1572 for (i = 0; i < 256; i++) {
1574 * The counts should both be zero or both be non-zero.
1575 * Mark the algorithm as bad if this is not met.
1577 if ((ksk_algorithms[i] != 0) == (zsk_algorithms[i] != 0))
1579 alg_format(i, algbuf, sizeof(algbuf));
1580 fprintf(stderr, "Missing %s for algorithm %s\n",
1581 (ksk_algorithms[i] != 0) ? "ZSK" : "self signing KSK",
1583 bad_algorithms[i] = 1;
1587 * Check that all the other records were signed by keys that are
1588 * present in the DNSKEY RRSET.
1591 dns_fixedname_init(&fname);
1592 name = dns_fixedname_name(&fname);
1593 dns_fixedname_init(&fnextname);
1594 nextname = dns_fixedname_name(&fnextname);
1595 dns_fixedname_init(&fzonecut);
1598 result = dns_db_createiterator(gdb, DNS_DB_NONSEC3, &dbiter);
1599 check_result(result, "dns_db_createiterator()");
1601 result = dns_dbiterator_first(dbiter);
1602 check_result(result, "dns_dbiterator_first()");
1605 isc_boolean_t isdelegation = ISC_FALSE;
1607 result = dns_dbiterator_current(dbiter, &node, name);
1608 check_dns_dbiterator_current(result);
1609 if (!dns_name_issubdomain(name, gorigin)) {
1610 dns_db_detachnode(gdb, &node);
1611 result = dns_dbiterator_next(dbiter);
1612 if (result == ISC_R_NOMORE)
1615 check_result(result, "dns_dbiterator_next()");
1618 if (delegation(name, node, NULL)) {
1619 zonecut = dns_fixedname_name(&fzonecut);
1620 dns_name_copy(name, zonecut, NULL);
1621 isdelegation = ISC_TRUE;
1623 verifynode(name, node, isdelegation, &rdataset,
1624 ksk_algorithms, bad_algorithms);
1625 result = dns_dbiterator_next(dbiter);
1627 while (result == ISC_R_SUCCESS) {
1628 result = dns_dbiterator_current(dbiter, &nextnode,
1630 check_dns_dbiterator_current(result);
1631 if (!dns_name_issubdomain(nextname, gorigin) ||
1633 dns_name_issubdomain(nextname, zonecut)))
1635 dns_db_detachnode(gdb, &nextnode);
1636 result = dns_dbiterator_next(dbiter);
1639 dns_db_detachnode(gdb, &nextnode);
1642 if (result == ISC_R_NOMORE) {
1644 } else if (result != ISC_R_SUCCESS)
1645 fatal("iterating through the database failed: %s",
1646 isc_result_totext(result));
1647 dns_db_detachnode(gdb, &node);
1650 dns_dbiterator_destroy(&dbiter);
1652 result = dns_db_createiterator(gdb, DNS_DB_NSEC3ONLY, &dbiter);
1653 check_result(result, "dns_db_createiterator()");
1655 for (result = dns_dbiterator_first(dbiter);
1656 result == ISC_R_SUCCESS;
1657 result = dns_dbiterator_next(dbiter) ) {
1658 result = dns_dbiterator_current(dbiter, &node, name);
1659 check_dns_dbiterator_current(result);
1660 verifynode(name, node, ISC_FALSE, &rdataset,
1661 ksk_algorithms, bad_algorithms);
1662 dns_db_detachnode(gdb, &node);
1664 dns_dbiterator_destroy(&dbiter);
1666 dns_rdataset_disassociate(&rdataset);
1669 * If we made it this far, we have what we consider a properly signed
1670 * zone. Set the good flag.
1672 for (i = 0; i < 256; i++) {
1673 if (bad_algorithms[i] != 0) {
1675 fprintf(stderr, "The zone is not fully signed "
1676 "for the following algorithms:");
1677 alg_format(i, algbuf, sizeof(algbuf));
1678 fprintf(stderr, " %s", algbuf);
1683 fprintf(stderr, ".\n");
1684 fatal("DNSSEC completeness test failed.");
1689 * Print the success summary.
1691 fprintf(stderr, "Zone signing complete:\n");
1692 for (i = 0; i < 256; i++) {
1693 if ((zsk_algorithms[i] != 0) ||
1694 (ksk_algorithms[i] != 0) ||
1695 (revoked[i] != 0) || (standby[i] != 0)) {
1696 alg_format(i, algbuf, sizeof(algbuf));
1697 fprintf(stderr, "Algorithm: %s: ZSKs: %u, "
1698 "KSKs: %u active, %u revoked, %u "
1699 "stand-by\n", algbuf,
1700 zsk_algorithms[i], ksk_algorithms[i],
1701 revoked[i], standby[i]);
1708 * Sign the apex of the zone.
1709 * Note the origin may not be the first node if there are out of zone
1714 dns_dbnode_t *node = NULL;
1715 dns_fixedname_t fixed;
1717 isc_result_t result;
1719 dns_fixedname_init(&fixed);
1720 name = dns_fixedname_name(&fixed);
1721 result = dns_dbiterator_seek(gdbiter, gorigin);
1722 check_result(result, "dns_dbiterator_seek()");
1723 result = dns_dbiterator_current(gdbiter, &node, name);
1724 check_dns_dbiterator_current(result);
1725 signname(node, name);
1726 dumpnode(name, node);
1727 cleannode(gdb, gversion, node);
1728 dns_db_detachnode(gdb, &node);
1729 result = dns_dbiterator_first(gdbiter);
1730 if (result == ISC_R_NOMORE)
1731 finished = ISC_TRUE;
1732 else if (result != ISC_R_SUCCESS)
1733 fatal("failure iterating database: %s",
1734 isc_result_totext(result));
1738 * Assigns a node to a worker thread. This is protected by the master task's
1742 assignwork(isc_task_t *task, isc_task_t *worker) {
1743 dns_fixedname_t *fname;
1747 dns_rdataset_t nsec;
1748 isc_boolean_t found;
1749 isc_result_t result;
1750 static dns_name_t *zonecut = NULL; /* Protected by namelock. */
1751 static dns_fixedname_t fzonecut; /* Protected by namelock. */
1752 static unsigned int ended = 0; /* Protected by namelock. */
1760 if (ended == ntasks) {
1761 isc_task_detach(&task);
1767 fname = isc_mem_get(mctx, sizeof(dns_fixedname_t));
1769 fatal("out of memory");
1770 dns_fixedname_init(fname);
1771 name = dns_fixedname_name(fname);
1775 result = dns_dbiterator_current(gdbiter, &node, name);
1776 check_dns_dbiterator_current(result);
1778 * The origin was handled by signapex().
1780 if (dns_name_equal(name, gorigin)) {
1781 dns_db_detachnode(gdb, &node);
1785 * Sort the zone data from the glue and out-of-zone data.
1786 * For NSEC zones nodes with zone data have NSEC records.
1787 * For NSEC3 zones the NSEC3 nodes are zone data but
1788 * outside of the zone name space. For the rest we need
1789 * to track the bottom of zone cuts.
1790 * Nodes which don't need to be signed are dumped here.
1792 dns_rdataset_init(&nsec);
1793 result = dns_db_findrdataset(gdb, node, gversion,
1794 nsec_datatype, 0, 0,
1796 if (dns_rdataset_isassociated(&nsec))
1797 dns_rdataset_disassociate(&nsec);
1798 if (result == ISC_R_SUCCESS) {
1800 } else if (nsec_datatype == dns_rdatatype_nsec3) {
1801 if (dns_name_issubdomain(name, gorigin) &&
1803 !dns_name_issubdomain(name, zonecut))) {
1804 if (delegation(name, node, NULL)) {
1805 dns_fixedname_init(&fzonecut);
1806 zonecut = dns_fixedname_name(&fzonecut);
1807 dns_name_copy(name, zonecut, NULL);
1808 if (!OPTOUT(nsec3flags) ||
1817 dumpnode(name, node);
1818 dns_db_detachnode(gdb, &node);
1822 result = dns_dbiterator_next(gdbiter);
1823 if (result == ISC_R_NOMORE) {
1824 finished = ISC_TRUE;
1826 } else if (result != ISC_R_SUCCESS)
1827 fatal("failure iterating database: %s",
1828 isc_result_totext(result));
1832 if (ended == ntasks) {
1833 isc_task_detach(&task);
1836 isc_mem_put(mctx, fname, sizeof(dns_fixedname_t));
1839 sevent = (sevent_t *)
1840 isc_event_allocate(mctx, task, SIGNER_EVENT_WORK,
1841 sign, NULL, sizeof(sevent_t));
1843 fatal("failed to allocate event\n");
1845 sevent->node = node;
1846 sevent->fname = fname;
1847 isc_task_send(worker, ISC_EVENT_PTR(&sevent));
1853 * Start a worker task
1856 startworker(isc_task_t *task, isc_event_t *event) {
1859 worker = (isc_task_t *)event->ev_arg;
1860 assignwork(task, worker);
1861 isc_event_free(&event);
1865 * Write a node to the output file, and restart the worker task.
1868 writenode(isc_task_t *task, isc_event_t *event) {
1870 sevent_t *sevent = (sevent_t *)event;
1872 worker = (isc_task_t *)event->ev_sender;
1873 dumpnode(dns_fixedname_name(sevent->fname), sevent->node);
1874 cleannode(gdb, gversion, sevent->node);
1875 dns_db_detachnode(gdb, &sevent->node);
1876 isc_mem_put(mctx, sevent->fname, sizeof(dns_fixedname_t));
1877 assignwork(task, worker);
1878 isc_event_free(&event);
1882 * Sign a database node.
1885 sign(isc_task_t *task, isc_event_t *event) {
1886 dns_fixedname_t *fname;
1888 sevent_t *sevent, *wevent;
1890 sevent = (sevent_t *)event;
1891 node = sevent->node;
1892 fname = sevent->fname;
1893 isc_event_free(&event);
1895 signname(node, dns_fixedname_name(fname));
1896 wevent = (sevent_t *)
1897 isc_event_allocate(mctx, task, SIGNER_EVENT_WRITE,
1898 writenode, NULL, sizeof(sevent_t));
1900 fatal("failed to allocate event\n");
1901 wevent->node = node;
1902 wevent->fname = fname;
1903 isc_task_send(master, ISC_EVENT_PTR(&wevent));
1907 * Update / remove the DS RRset. Preserve RRSIG(DS) if possible.
1910 add_ds(dns_name_t *name, dns_dbnode_t *node, isc_uint32_t nsttl) {
1911 dns_rdataset_t dsset;
1912 dns_rdataset_t sigdsset;
1913 isc_result_t result;
1915 dns_rdataset_init(&dsset);
1916 dns_rdataset_init(&sigdsset);
1917 result = dns_db_findrdataset(gdb, node, gversion,
1919 0, 0, &dsset, &sigdsset);
1920 if (result == ISC_R_SUCCESS) {
1921 dns_rdataset_disassociate(&dsset);
1922 result = dns_db_deleterdataset(gdb, node, gversion,
1923 dns_rdatatype_ds, 0);
1924 check_result(result, "dns_db_deleterdataset");
1926 result = loadds(name, nsttl, &dsset);
1927 if (result == ISC_R_SUCCESS) {
1928 result = dns_db_addrdataset(gdb, node, gversion, 0,
1930 check_result(result, "dns_db_addrdataset");
1931 dns_rdataset_disassociate(&dsset);
1932 if (dns_rdataset_isassociated(&sigdsset))
1933 dns_rdataset_disassociate(&sigdsset);
1934 } else if (dns_rdataset_isassociated(&sigdsset)) {
1935 result = dns_db_deleterdataset(gdb, node, gversion,
1936 dns_rdatatype_rrsig,
1938 check_result(result, "dns_db_deleterdataset");
1939 dns_rdataset_disassociate(&sigdsset);
1944 * Remove records of the given type and their signatures.
1947 remove_records(dns_dbnode_t *node, dns_rdatatype_t which) {
1948 isc_result_t result;
1949 dns_rdatatype_t type, covers;
1950 dns_rdatasetiter_t *rdsiter = NULL;
1951 dns_rdataset_t rdataset;
1953 dns_rdataset_init(&rdataset);
1956 * Delete any NSEC records at the apex.
1958 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
1959 check_result(result, "dns_db_allrdatasets()");
1960 for (result = dns_rdatasetiter_first(rdsiter);
1961 result == ISC_R_SUCCESS;
1962 result = dns_rdatasetiter_next(rdsiter)) {
1963 dns_rdatasetiter_current(rdsiter, &rdataset);
1964 type = rdataset.type;
1965 covers = rdataset.covers;
1966 dns_rdataset_disassociate(&rdataset);
1967 if (type == which || covers == which) {
1968 result = dns_db_deleterdataset(gdb, node, gversion,
1970 check_result(result, "dns_db_deleterdataset()");
1974 dns_rdatasetiter_destroy(&rdsiter);
1978 * Generate NSEC records for the zone and remove NSEC3/NSEC3PARAM records.
1982 dns_dbiterator_t *dbiter = NULL;
1983 dns_dbnode_t *node = NULL, *nextnode = NULL;
1984 dns_fixedname_t fname, fnextname, fzonecut;
1985 dns_name_t *name, *nextname, *zonecut;
1986 dns_rdataset_t rdataset;
1987 dns_rdatasetiter_t *rdsiter = NULL;
1988 dns_rdatatype_t type, covers;
1989 isc_boolean_t done = ISC_FALSE;
1990 isc_result_t result;
1991 isc_uint32_t nsttl = 0;
1993 dns_rdataset_init(&rdataset);
1994 dns_fixedname_init(&fname);
1995 name = dns_fixedname_name(&fname);
1996 dns_fixedname_init(&fnextname);
1997 nextname = dns_fixedname_name(&fnextname);
1998 dns_fixedname_init(&fzonecut);
2002 * Remove any NSEC3 chains.
2004 result = dns_db_createiterator(gdb, DNS_DB_NSEC3ONLY, &dbiter);
2005 check_result(result, "dns_db_createiterator()");
2006 for (result = dns_dbiterator_first(dbiter);
2007 result == ISC_R_SUCCESS;
2008 result = dns_dbiterator_next(dbiter)) {
2009 result = dns_dbiterator_current(dbiter, &node, name);
2010 check_dns_dbiterator_current(result);
2011 result = dns_db_allrdatasets(gdb, node, gversion, 0, &rdsiter);
2012 check_result(result, "dns_db_allrdatasets()");
2013 for (result = dns_rdatasetiter_first(rdsiter);
2014 result == ISC_R_SUCCESS;
2015 result = dns_rdatasetiter_next(rdsiter)) {
2016 dns_rdatasetiter_current(rdsiter, &rdataset);
2017 type = rdataset.type;
2018 covers = rdataset.covers;
2019 dns_rdataset_disassociate(&rdataset);
2020 result = dns_db_deleterdataset(gdb, node, gversion,
2022 check_result(result,
2023 "dns_db_deleterdataset(nsec3param/rrsig)");
2025 dns_rdatasetiter_destroy(&rdsiter);
2026 dns_db_detachnode(gdb, &node);
2028 dns_dbiterator_destroy(&dbiter);
2030 result = dns_db_createiterator(gdb, DNS_DB_NONSEC3, &dbiter);
2031 check_result(result, "dns_db_createiterator()");
2033 result = dns_dbiterator_first(dbiter);
2034 check_result(result, "dns_dbiterator_first()");
2037 result = dns_dbiterator_current(dbiter, &node, name);
2038 check_dns_dbiterator_current(result);
2040 * Skip out-of-zone records.
2042 if (!dns_name_issubdomain(name, gorigin)) {
2043 result = dns_dbiterator_next(dbiter);
2044 if (result == ISC_R_NOMORE)
2047 check_result(result, "dns_dbiterator_next()");
2048 dns_db_detachnode(gdb, &node);
2052 if (dns_name_equal(name, gorigin))
2053 remove_records(node, dns_rdatatype_nsec3param);
2055 if (delegation(name, node, &nsttl)) {
2056 zonecut = dns_fixedname_name(&fzonecut);
2057 dns_name_copy(name, zonecut, NULL);
2059 add_ds(name, node, nsttl);
2061 result = dns_dbiterator_next(dbiter);
2063 while (result == ISC_R_SUCCESS) {
2064 isc_boolean_t active = ISC_FALSE;
2065 result = dns_dbiterator_current(dbiter, &nextnode,
2067 check_dns_dbiterator_current(result);
2068 active = active_node(nextnode);
2070 dns_db_detachnode(gdb, &nextnode);
2071 result = dns_dbiterator_next(dbiter);
2074 if (!dns_name_issubdomain(nextname, gorigin) ||
2076 dns_name_issubdomain(nextname, zonecut)))
2078 dns_db_detachnode(gdb, &nextnode);
2079 result = dns_dbiterator_next(dbiter);
2082 dns_db_detachnode(gdb, &nextnode);
2085 if (result == ISC_R_NOMORE) {
2086 dns_name_clone(gorigin, nextname);
2088 } else if (result != ISC_R_SUCCESS)
2089 fatal("iterating through the database failed: %s",
2090 isc_result_totext(result));
2091 result = dns_nsec_build(gdb, gversion, node, nextname,
2093 check_result(result, "dns_nsec_build()");
2094 dns_db_detachnode(gdb, &node);
2097 dns_dbiterator_destroy(&dbiter);
2101 addnsec3param(const unsigned char *salt, size_t salt_length,
2102 unsigned int iterations)
2104 dns_dbnode_t *node = NULL;
2105 dns_rdata_nsec3param_t nsec3param;
2106 unsigned char nsec3parambuf[5 + 255];
2107 dns_rdatalist_t rdatalist;
2108 dns_rdataset_t rdataset;
2109 dns_rdata_t rdata = DNS_RDATA_INIT;
2111 isc_result_t result;
2113 dns_rdataset_init(&rdataset);
2115 nsec3param.common.rdclass = gclass;
2116 nsec3param.common.rdtype = dns_rdatatype_nsec3param;
2117 ISC_LINK_INIT(&nsec3param.common, link);
2118 nsec3param.mctx = NULL;
2119 nsec3param.flags = 0;
2120 nsec3param.hash = unknownalg ? DNS_NSEC3_UNKNOWNALG : dns_hash_sha1;
2121 nsec3param.iterations = iterations;
2122 nsec3param.salt_length = salt_length;
2123 DE_CONST(salt, nsec3param.salt);
2125 isc_buffer_init(&b, nsec3parambuf, sizeof(nsec3parambuf));
2126 result = dns_rdata_fromstruct(&rdata, gclass,
2127 dns_rdatatype_nsec3param,
2129 rdatalist.rdclass = rdata.rdclass;
2130 rdatalist.type = rdata.type;
2131 rdatalist.covers = 0;
2133 ISC_LIST_INIT(rdatalist.rdata);
2134 ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
2135 result = dns_rdatalist_tordataset(&rdatalist, &rdataset);
2136 check_result(result, "dns_rdatalist_tordataset()");
2138 result = dns_db_findnode(gdb, gorigin, ISC_TRUE, &node);
2139 check_result(result, "dns_db_find(gorigin)");
2142 * Delete any current NSEC3PARAM records.
2144 result = dns_db_deleterdataset(gdb, node, gversion,
2145 dns_rdatatype_nsec3param, 0);
2146 if (result == DNS_R_UNCHANGED)
2147 result = ISC_R_SUCCESS;
2148 check_result(result, "dddnsec3param: dns_db_deleterdataset()");
2150 result = dns_db_addrdataset(gdb, node, gversion, 0, &rdataset,
2151 DNS_DBADD_MERGE, NULL);
2152 if (result == DNS_R_UNCHANGED)
2153 result = ISC_R_SUCCESS;
2154 check_result(result, "addnsec3param: dns_db_addrdataset()");
2155 dns_db_detachnode(gdb, &node);
2159 addnsec3(dns_name_t *name, dns_dbnode_t *node,
2160 const unsigned char *salt, size_t salt_length,
2161 unsigned int iterations, hashlist_t *hashlist,
2164 unsigned char hash[NSEC3_MAX_HASH_LENGTH];
2165 const unsigned char *nexthash;
2166 unsigned char nsec3buffer[DNS_NSEC3_BUFFERSIZE];
2167 dns_fixedname_t hashname;
2168 dns_rdatalist_t rdatalist;
2169 dns_rdataset_t rdataset;
2170 dns_rdata_t rdata = DNS_RDATA_INIT;
2171 isc_result_t result;
2172 dns_dbnode_t *nsec3node = NULL;
2173 char namebuf[DNS_NAME_FORMATSIZE];
2176 dns_name_format(name, namebuf, sizeof(namebuf));
2178 dns_fixedname_init(&hashname);
2179 dns_rdataset_init(&rdataset);
2181 dns_name_downcase(name, name, NULL);
2182 result = dns_nsec3_hashname(&hashname, hash, &hash_length,
2183 name, gorigin, dns_hash_sha1, iterations,
2185 check_result(result, "addnsec3: dns_nsec3_hashname()");
2186 nexthash = hashlist_findnext(hashlist, hash);
2187 result = dns_nsec3_buildrdata(gdb, gversion, node,
2189 DNS_NSEC3_UNKNOWNALG : dns_hash_sha1,
2190 nsec3flags, iterations,
2192 nexthash, ISC_SHA1_DIGESTLENGTH,
2193 nsec3buffer, &rdata);
2194 check_result(result, "addnsec3: dns_nsec3_buildrdata()");
2195 rdatalist.rdclass = rdata.rdclass;
2196 rdatalist.type = rdata.type;
2197 rdatalist.covers = 0;
2198 rdatalist.ttl = ttl;
2199 ISC_LIST_INIT(rdatalist.rdata);
2200 ISC_LIST_APPEND(rdatalist.rdata, &rdata, link);
2201 result = dns_rdatalist_tordataset(&rdatalist, &rdataset);
2202 check_result(result, "dns_rdatalist_tordataset()");
2203 result = dns_db_findnsec3node(gdb, dns_fixedname_name(&hashname),
2204 ISC_TRUE, &nsec3node);
2205 check_result(result, "addnsec3: dns_db_findnode()");
2206 result = dns_db_addrdataset(gdb, nsec3node, gversion, 0, &rdataset,
2208 if (result == DNS_R_UNCHANGED)
2209 result = ISC_R_SUCCESS;
2210 check_result(result, "addnsec3: dns_db_addrdataset()");
2211 dns_db_detachnode(gdb, &nsec3node);
2215 * Clean out NSEC3 record and RRSIG(NSEC3) that are not in the hash list.
2217 * Extract the hash from the first label of 'name' then see if it
2218 * is in hashlist. If 'name' is not in the hashlist then delete the
2219 * any NSEC3 records which have the same parameters as the chain we
2222 * XXXMPA Should we also check that it of the form <hash>.<origin>?
2225 nsec3clean(dns_name_t *name, dns_dbnode_t *node,
2226 unsigned int hashalg, unsigned int iterations,
2227 const unsigned char *salt, size_t salt_length, hashlist_t *hashlist)
2230 dns_rdata_nsec3_t nsec3;
2231 dns_rdata_t rdata, delrdata;
2232 dns_rdatalist_t rdatalist;
2233 dns_rdataset_t rdataset, delrdataset;
2234 isc_boolean_t delete_rrsigs = ISC_FALSE;
2235 isc_buffer_t target;
2236 isc_result_t result;
2237 unsigned char hash[NSEC3_MAX_HASH_LENGTH + 1];
2238 isc_boolean_t exists;
2241 * Get the first label.
2243 dns_name_getlabel(name, 0, &label);
2246 * We want just the label contents.
2248 isc_region_consume(&label, 1);
2251 * Decode base32hex string.
2253 isc_buffer_init(&target, hash, sizeof(hash) - 1);
2254 result = isc_base32hex_decoderegion(&label, &target);
2255 if (result != ISC_R_SUCCESS)
2258 hash[isc_buffer_usedlength(&target)] = 0;
2260 exists = hashlist_exists(hashlist, hash);
2263 * Verify that the NSEC3 parameters match the current ones
2264 * otherwise we are dealing with a different NSEC3 chain.
2266 dns_rdataset_init(&rdataset);
2267 dns_rdataset_init(&delrdataset);
2269 result = dns_db_findrdataset(gdb, node, gversion, dns_rdatatype_nsec3,
2270 0, 0, &rdataset, NULL);
2271 if (result != ISC_R_SUCCESS)
2275 * Delete any NSEC3 records which are not part of the current
2278 for (result = dns_rdataset_first(&rdataset);
2279 result == ISC_R_SUCCESS;
2280 result = dns_rdataset_next(&rdataset)) {
2281 dns_rdata_init(&rdata);
2282 dns_rdataset_current(&rdataset, &rdata);
2283 result = dns_rdata_tostruct(&rdata, &nsec3, NULL);
2284 check_result(result, "dns_rdata_tostruct");
2285 if (exists && nsec3.hash == hashalg &&
2286 nsec3.iterations == iterations &&
2287 nsec3.salt_length == salt_length &&
2288 !memcmp(nsec3.salt, salt, salt_length))
2290 rdatalist.rdclass = rdata.rdclass;
2291 rdatalist.type = rdata.type;
2292 rdatalist.covers = 0;
2293 rdatalist.ttl = rdataset.ttl;
2294 ISC_LIST_INIT(rdatalist.rdata);
2295 dns_rdata_init(&delrdata);
2296 dns_rdata_clone(&rdata, &delrdata);
2297 ISC_LIST_APPEND(rdatalist.rdata, &delrdata, link);
2298 result = dns_rdatalist_tordataset(&rdatalist, &delrdataset);
2299 check_result(result, "dns_rdatalist_tordataset()");
2300 result = dns_db_subtractrdataset(gdb, node, gversion,
2301 &delrdataset, 0, NULL);
2302 dns_rdataset_disassociate(&delrdataset);
2303 if (result != ISC_R_SUCCESS && result != DNS_R_NXRRSET)
2304 check_result(result, "dns_db_subtractrdataset(NSEC3)");
2305 delete_rrsigs = ISC_TRUE;
2307 dns_rdataset_disassociate(&rdataset);
2308 if (result != ISC_R_NOMORE)
2309 check_result(result, "dns_rdataset_first/next");
2314 * Delete the NSEC3 RRSIGs
2316 result = dns_db_deleterdataset(gdb, node, gversion,
2317 dns_rdatatype_rrsig,
2318 dns_rdatatype_nsec3);
2319 if (result != ISC_R_SUCCESS && result != DNS_R_UNCHANGED)
2320 check_result(result, "dns_db_deleterdataset(RRSIG(NSEC3))");
2324 * Generate NSEC3 records for the zone.
2327 nsec3ify(unsigned int hashalg, unsigned int iterations,
2328 const unsigned char *salt, size_t salt_length, hashlist_t *hashlist)
2330 dns_dbiterator_t *dbiter = NULL;
2331 dns_dbnode_t *node = NULL, *nextnode = NULL;
2332 dns_fixedname_t fname, fnextname, fzonecut;
2333 dns_name_t *name, *nextname, *zonecut;
2334 dns_rdataset_t rdataset;
2336 isc_boolean_t active;
2337 isc_boolean_t done = ISC_FALSE;
2338 isc_result_t result;
2339 isc_uint32_t nsttl = 0;
2340 unsigned int count, nlabels;
2342 dns_rdataset_init(&rdataset);
2343 dns_fixedname_init(&fname);
2344 name = dns_fixedname_name(&fname);
2345 dns_fixedname_init(&fnextname);
2346 nextname = dns_fixedname_name(&fnextname);
2347 dns_fixedname_init(&fzonecut);
2351 * Walk the zone generating the hash names.
2353 result = dns_db_createiterator(gdb, DNS_DB_NONSEC3, &dbiter);
2354 check_result(result, "dns_db_createiterator()");
2356 result = dns_dbiterator_first(dbiter);
2357 check_result(result, "dns_dbiterator_first()");
2360 result = dns_dbiterator_current(dbiter, &node, name);
2361 check_dns_dbiterator_current(result);
2363 * Skip out-of-zone records.
2365 if (!dns_name_issubdomain(name, gorigin)) {
2366 result = dns_dbiterator_next(dbiter);
2367 if (result == ISC_R_NOMORE)
2370 check_result(result, "dns_dbiterator_next()");
2371 dns_db_detachnode(gdb, &node);
2375 if (dns_name_equal(name, gorigin))
2376 remove_records(node, dns_rdatatype_nsec);
2378 result = dns_dbiterator_next(dbiter);
2380 while (result == ISC_R_SUCCESS) {
2381 result = dns_dbiterator_current(dbiter, &nextnode,
2383 check_dns_dbiterator_current(result);
2384 active = active_node(nextnode);
2386 dns_db_detachnode(gdb, &nextnode);
2387 result = dns_dbiterator_next(dbiter);
2390 if (!dns_name_issubdomain(nextname, gorigin) ||
2392 dns_name_issubdomain(nextname, zonecut))) {
2393 dns_db_detachnode(gdb, &nextnode);
2394 result = dns_dbiterator_next(dbiter);
2397 if (delegation(nextname, nextnode, &nsttl)) {
2398 zonecut = dns_fixedname_name(&fzonecut);
2399 dns_name_copy(nextname, zonecut, NULL);
2401 add_ds(nextname, nextnode, nsttl);
2402 if (OPTOUT(nsec3flags) &&
2403 !secure(nextname, nextnode)) {
2404 dns_db_detachnode(gdb, &nextnode);
2405 result = dns_dbiterator_next(dbiter);
2409 dns_db_detachnode(gdb, &nextnode);
2412 if (result == ISC_R_NOMORE) {
2413 dns_name_copy(gorigin, nextname, NULL);
2415 } else if (result != ISC_R_SUCCESS)
2416 fatal("iterating through the database failed: %s",
2417 isc_result_totext(result));
2418 dns_name_downcase(name, name, NULL);
2419 hashlist_add_dns_name(hashlist, name, hashalg, iterations,
2420 salt, salt_length, ISC_FALSE);
2421 dns_db_detachnode(gdb, &node);
2423 * Add hashs for empty nodes. Use closest encloser logic.
2424 * The closest encloser either has data or is a empty
2425 * node for another <name,nextname> span so we don't add
2426 * it here. Empty labels on nextname are within the span.
2428 dns_name_downcase(nextname, nextname, NULL);
2429 dns_name_fullcompare(name, nextname, &order, &nlabels);
2430 addnowildcardhash(hashlist, name, hashalg, iterations,
2432 count = dns_name_countlabels(nextname);
2433 while (count > nlabels + 1) {
2435 dns_name_split(nextname, count, NULL, nextname);
2436 hashlist_add_dns_name(hashlist, nextname, hashalg,
2437 iterations, salt, salt_length,
2439 addnowildcardhash(hashlist, nextname, hashalg,
2440 iterations, salt, salt_length);
2443 dns_dbiterator_destroy(&dbiter);
2446 * We have all the hashes now so we can sort them.
2448 hashlist_sort(hashlist);
2451 * Check for duplicate hashes. If found the salt needs to
2454 if (hashlist_hasdup(hashlist))
2455 fatal("Duplicate hash detected. Pick a different salt.");
2458 * Generate the nsec3 records.
2463 addnsec3param(salt, salt_length, iterations);
2466 * Clean out NSEC3 records which don't match this chain.
2468 result = dns_db_createiterator(gdb, DNS_DB_NSEC3ONLY, &dbiter);
2469 check_result(result, "dns_db_createiterator()");
2471 for (result = dns_dbiterator_first(dbiter);
2472 result == ISC_R_SUCCESS;
2473 result = dns_dbiterator_next(dbiter)) {
2474 result = dns_dbiterator_current(dbiter, &node, name);
2475 check_dns_dbiterator_current(result);
2476 nsec3clean(name, node, hashalg, iterations, salt, salt_length,
2478 dns_db_detachnode(gdb, &node);
2480 dns_dbiterator_destroy(&dbiter);
2483 * Generate / complete the new chain.
2485 result = dns_db_createiterator(gdb, DNS_DB_NONSEC3, &dbiter);
2486 check_result(result, "dns_db_createiterator()");
2488 result = dns_dbiterator_first(dbiter);
2489 check_result(result, "dns_dbiterator_first()");
2492 result = dns_dbiterator_current(dbiter, &node, name);
2493 check_dns_dbiterator_current(result);
2495 * Skip out-of-zone records.
2497 if (!dns_name_issubdomain(name, gorigin)) {
2498 result = dns_dbiterator_next(dbiter);
2499 if (result == ISC_R_NOMORE)
2502 check_result(result, "dns_dbiterator_next()");
2503 dns_db_detachnode(gdb, &node);
2506 result = dns_dbiterator_next(dbiter);
2508 while (result == ISC_R_SUCCESS) {
2509 result = dns_dbiterator_current(dbiter, &nextnode,
2511 check_dns_dbiterator_current(result);
2512 active = active_node(nextnode);
2514 dns_db_detachnode(gdb, &nextnode);
2515 result = dns_dbiterator_next(dbiter);
2518 if (!dns_name_issubdomain(nextname, gorigin) ||
2520 dns_name_issubdomain(nextname, zonecut))) {
2521 dns_db_detachnode(gdb, &nextnode);
2522 result = dns_dbiterator_next(dbiter);
2525 if (delegation(nextname, nextnode, NULL)) {
2526 zonecut = dns_fixedname_name(&fzonecut);
2527 dns_name_copy(nextname, zonecut, NULL);
2528 if (OPTOUT(nsec3flags) &&
2529 !secure(nextname, nextnode)) {
2530 dns_db_detachnode(gdb, &nextnode);
2531 result = dns_dbiterator_next(dbiter);
2535 dns_db_detachnode(gdb, &nextnode);
2538 if (result == ISC_R_NOMORE) {
2539 dns_name_copy(gorigin, nextname, NULL);
2541 } else if (result != ISC_R_SUCCESS)
2542 fatal("iterating through the database failed: %s",
2543 isc_result_totext(result));
2545 * We need to pause here to release the lock on the database.
2547 dns_dbiterator_pause(dbiter);
2548 addnsec3(name, node, salt, salt_length, iterations,
2550 dns_db_detachnode(gdb, &node);
2552 * Add NSEC3's for empty nodes. Use closest encloser logic.
2554 dns_name_fullcompare(name, nextname, &order, &nlabels);
2555 count = dns_name_countlabels(nextname);
2556 while (count > nlabels + 1) {
2558 dns_name_split(nextname, count, NULL, nextname);
2559 addnsec3(nextname, NULL, salt, salt_length,
2560 iterations, hashlist, zonettl);
2563 dns_dbiterator_destroy(&dbiter);
2567 * Load the zone file from disk
2570 loadzone(char *file, char *origin, dns_rdataclass_t rdclass, dns_db_t **db) {
2573 dns_fixedname_t fname;
2575 isc_result_t result;
2577 len = strlen(origin);
2578 isc_buffer_init(&b, origin, len);
2579 isc_buffer_add(&b, len);
2581 dns_fixedname_init(&fname);
2582 name = dns_fixedname_name(&fname);
2583 result = dns_name_fromtext(name, &b, dns_rootname, ISC_FALSE, NULL);
2584 if (result != ISC_R_SUCCESS)
2585 fatal("failed converting name '%s' to dns format: %s",
2586 origin, isc_result_totext(result));
2588 result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone,
2589 rdclass, 0, NULL, db);
2590 check_result(result, "dns_db_create()");
2592 result = dns_db_load2(*db, file, inputformat);
2593 if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
2594 fatal("failed loading zone from '%s': %s",
2595 file, isc_result_totext(result));
2599 * Finds all public zone keys in the zone, and attempts to load the
2600 * private keys from disk.
2603 loadzonekeys(dns_db_t *db) {
2605 dns_dbversion_t *currentversion;
2606 isc_result_t result;
2607 dst_key_t *keys[20];
2608 unsigned int nkeys, i;
2610 currentversion = NULL;
2611 dns_db_currentversion(db, ¤tversion);
2614 result = dns_db_findnode(db, gorigin, ISC_FALSE, &node);
2615 if (result != ISC_R_SUCCESS)
2616 fatal("failed to find the zone's origin: %s",
2617 isc_result_totext(result));
2619 result = dns_dnssec_findzonekeys(db, currentversion, node, gorigin,
2620 mctx, 20, keys, &nkeys);
2621 if (result == ISC_R_NOTFOUND)
2622 result = ISC_R_SUCCESS;
2623 if (result != ISC_R_SUCCESS)
2624 fatal("failed to find the zone keys: %s",
2625 isc_result_totext(result));
2627 for (i = 0; i < nkeys; i++) {
2630 key = newkeystruct(keys[i], dst_key_isprivate(keys[i]));
2631 ISC_LIST_APPEND(keylist, key, link);
2633 dns_db_detachnode(db, &node);
2634 dns_db_closeversion(db, ¤tversion, ISC_FALSE);
2638 * Finds all public zone keys in the zone.
2641 loadzonepubkeys(dns_db_t *db) {
2642 dns_dbversion_t *currentversion = NULL;
2643 dns_dbnode_t *node = NULL;
2644 dns_rdataset_t rdataset;
2645 dns_rdata_t rdata = DNS_RDATA_INIT;
2648 isc_result_t result;
2650 dns_db_currentversion(db, ¤tversion);
2652 result = dns_db_findnode(db, gorigin, ISC_FALSE, &node);
2653 if (result != ISC_R_SUCCESS)
2654 fatal("failed to find the zone's origin: %s",
2655 isc_result_totext(result));
2657 dns_rdataset_init(&rdataset);
2658 result = dns_db_findrdataset(db, node, currentversion,
2659 dns_rdatatype_dnskey, 0, 0, &rdataset,
2661 if (result != ISC_R_SUCCESS)
2662 fatal("failed to find keys at the zone apex: %s",
2663 isc_result_totext(result));
2664 result = dns_rdataset_first(&rdataset);
2665 check_result(result, "dns_rdataset_first");
2666 while (result == ISC_R_SUCCESS) {
2668 dns_rdata_reset(&rdata);
2669 dns_rdataset_current(&rdataset, &rdata);
2670 result = dns_dnssec_keyfromrdata(gorigin, &rdata, mctx,
2672 if (result != ISC_R_SUCCESS)
2674 if (!dst_key_iszonekey(pubkey)) {
2675 dst_key_free(&pubkey);
2679 key = newkeystruct(pubkey, ISC_FALSE);
2680 ISC_LIST_APPEND(keylist, key, link);
2682 result = dns_rdataset_next(&rdataset);
2684 dns_rdataset_disassociate(&rdataset);
2685 dns_db_detachnode(db, &node);
2686 dns_db_closeversion(db, ¤tversion, ISC_FALSE);
2690 warnifallksk(dns_db_t *db) {
2691 dns_dbversion_t *currentversion = NULL;
2692 dns_dbnode_t *node = NULL;
2693 dns_rdataset_t rdataset;
2694 dns_rdata_t rdata = DNS_RDATA_INIT;
2695 isc_result_t result;
2696 dns_rdata_dnskey_t dnskey;
2697 isc_boolean_t have_non_ksk = ISC_FALSE;
2699 dns_db_currentversion(db, ¤tversion);
2701 result = dns_db_findnode(db, gorigin, ISC_FALSE, &node);
2702 if (result != ISC_R_SUCCESS)
2703 fatal("failed to find the zone's origin: %s",
2704 isc_result_totext(result));
2706 dns_rdataset_init(&rdataset);
2707 result = dns_db_findrdataset(db, node, currentversion,
2708 dns_rdatatype_dnskey, 0, 0, &rdataset,
2710 if (result != ISC_R_SUCCESS)
2711 fatal("failed to find keys at the zone apex: %s",
2712 isc_result_totext(result));
2713 result = dns_rdataset_first(&rdataset);
2714 check_result(result, "dns_rdataset_first");
2715 while (result == ISC_R_SUCCESS) {
2716 dns_rdata_reset(&rdata);
2717 dns_rdataset_current(&rdataset, &rdata);
2718 result = dns_rdata_tostruct(&rdata, &dnskey, NULL);
2719 check_result(result, "dns_rdata_tostruct");
2720 if ((dnskey.flags & DNS_KEYFLAG_KSK) == 0) {
2721 have_non_ksk = ISC_TRUE;
2722 result = ISC_R_NOMORE;
2724 result = dns_rdataset_next(&rdataset);
2725 dns_rdata_freestruct(&dnskey);
2727 dns_rdataset_disassociate(&rdataset);
2728 dns_db_detachnode(db, &node);
2729 dns_db_closeversion(db, ¤tversion, ISC_FALSE);
2730 if (!have_non_ksk && !ignoreksk) {
2731 if (disable_zone_check)
2732 fprintf(stderr, "%s: warning: No non-KSK dnskey found. "
2733 "Supply non-KSK dnskey or use '-z'.\n",
2736 fatal("No non-KSK dnskey found. "
2737 "Supply non-KSK dnskey or use '-z'.");
2742 writeset(const char *prefix, dns_rdatatype_t type) {
2744 char namestr[DNS_NAME_FORMATSIZE];
2745 dns_db_t *db = NULL;
2746 dns_dbversion_t *version = NULL;
2748 dns_difftuple_t *tuple = NULL;
2749 dns_fixedname_t fixed;
2751 dns_rdata_t rdata, ds;
2752 isc_boolean_t have_ksk = ISC_FALSE;
2753 isc_boolean_t have_non_ksk = ISC_FALSE;
2755 isc_buffer_t namebuf;
2757 isc_result_t result;
2759 unsigned char dsbuf[DNS_DS_BUFFERSIZE];
2760 unsigned char keybuf[DST_KEY_MAXSIZE];
2761 unsigned int filenamelen;
2762 const dns_master_style_t *style =
2763 (type == dns_rdatatype_dnskey) ? masterstyle : dsstyle;
2765 isc_buffer_init(&namebuf, namestr, sizeof(namestr));
2766 result = dns_name_tofilenametext(gorigin, ISC_FALSE, &namebuf);
2767 check_result(result, "dns_name_tofilenametext");
2768 isc_buffer_putuint8(&namebuf, 0);
2769 filenamelen = strlen(prefix) + strlen(namestr);
2770 if (directory != NULL)
2771 filenamelen += strlen(directory) + 1;
2772 filename = isc_mem_get(mctx, filenamelen + 1);
2773 if (filename == NULL)
2774 fatal("out of memory");
2775 if (directory != NULL)
2776 sprintf(filename, "%s/", directory);
2779 strcat(filename, prefix);
2780 strcat(filename, namestr);
2782 dns_diff_init(mctx, &diff);
2784 for (key = ISC_LIST_HEAD(keylist);
2786 key = ISC_LIST_NEXT(key, link))
2788 have_non_ksk = ISC_TRUE;
2792 for (key = ISC_LIST_HEAD(keylist);
2794 key = ISC_LIST_NEXT(key, link))
2796 have_ksk = ISC_TRUE;
2800 if (type == dns_rdatatype_dlv) {
2802 unsigned int labels;
2804 dns_name_init(&tname, NULL);
2805 dns_fixedname_init(&fixed);
2806 name = dns_fixedname_name(&fixed);
2807 labels = dns_name_countlabels(gorigin);
2808 dns_name_getlabelsequence(gorigin, 0, labels - 1, &tname);
2809 result = dns_name_concatenate(&tname, dlv, name, NULL);
2810 check_result(result, "dns_name_concatenate");
2814 for (key = ISC_LIST_HEAD(keylist);
2816 key = ISC_LIST_NEXT(key, link))
2818 if (have_ksk && have_non_ksk && !key->isksk)
2820 dns_rdata_init(&rdata);
2821 dns_rdata_init(&ds);
2822 isc_buffer_init(&b, keybuf, sizeof(keybuf));
2823 result = dst_key_todns(key->key, &b);
2824 check_result(result, "dst_key_todns");
2825 isc_buffer_usedregion(&b, &r);
2826 dns_rdata_fromregion(&rdata, gclass, dns_rdatatype_dnskey, &r);
2827 if (type != dns_rdatatype_dnskey) {
2828 result = dns_ds_buildrdata(gorigin, &rdata,
2831 check_result(result, "dns_ds_buildrdata");
2832 if (type == dns_rdatatype_dlv)
2833 ds.type = dns_rdatatype_dlv;
2834 result = dns_difftuple_create(mctx, DNS_DIFFOP_ADD,
2835 name, 0, &ds, &tuple);
2836 check_result(result, "dns_difftuple_create");
2837 dns_diff_append(&diff, &tuple);
2839 dns_rdata_reset(&ds);
2840 result = dns_ds_buildrdata(gorigin, &rdata,
2841 DNS_DSDIGEST_SHA256,
2843 check_result(result, "dns_ds_buildrdata");
2844 if (type == dns_rdatatype_dlv)
2845 ds.type = dns_rdatatype_dlv;
2846 result = dns_difftuple_create(mctx, DNS_DIFFOP_ADD,
2847 name, 0, &ds, &tuple);
2850 result = dns_difftuple_create(mctx, DNS_DIFFOP_ADD,
2853 check_result(result, "dns_difftuple_create");
2854 dns_diff_append(&diff, &tuple);
2857 result = dns_db_create(mctx, "rbt", dns_rootname, dns_dbtype_zone,
2858 gclass, 0, NULL, &db);
2859 check_result(result, "dns_db_create");
2861 result = dns_db_newversion(db, &version);
2862 check_result(result, "dns_db_newversion");
2864 result = dns_diff_apply(&diff, db, version);
2865 check_result(result, "dns_diff_apply");
2866 dns_diff_clear(&diff);
2868 result = dns_master_dump(mctx, db, version, style, filename);
2869 check_result(result, "dns_master_dump");
2871 isc_mem_put(mctx, filename, filenamelen + 1);
2873 dns_db_closeversion(db, &version, ISC_FALSE);
2878 print_time(FILE *fp) {
2881 if (outputformat != dns_masterformat_text)
2884 currenttime = time(NULL);
2885 fprintf(fp, "; File written on %s", ctime(¤ttime));
2889 print_version(FILE *fp) {
2890 if (outputformat != dns_masterformat_text)
2893 fprintf(fp, "; dnssec_signzone version " VERSION "\n");
2898 fprintf(stderr, "Usage:\n");
2899 fprintf(stderr, "\t%s [options] zonefile [keys]\n", program);
2901 fprintf(stderr, "\n");
2903 fprintf(stderr, "Version: %s\n", VERSION);
2905 fprintf(stderr, "Options: (default value in parenthesis) \n");
2906 fprintf(stderr, "\t-c class (IN)\n");
2907 fprintf(stderr, "\t-d directory\n");
2908 fprintf(stderr, "\t\tdirectory to find keyset files (.)\n");
2909 fprintf(stderr, "\t-g:\t");
2910 fprintf(stderr, "generate DS records from keyset files\n");
2911 fprintf(stderr, "\t-s [YYYYMMDDHHMMSS|+offset]:\n");
2912 fprintf(stderr, "\t\tRRSIG start time - absolute|offset "
2913 "(now - 1 hour)\n");
2914 fprintf(stderr, "\t-e [YYYYMMDDHHMMSS|+offset|\"now\"+offset]:\n");
2915 fprintf(stderr, "\t\tRRSIG end time - absolute|from start|from now "
2916 "(now + 30 days)\n");
2917 fprintf(stderr, "\t-i interval:\n");
2918 fprintf(stderr, "\t\tcycle interval - resign "
2919 "if < interval from end ( (end-start)/4 )\n");
2920 fprintf(stderr, "\t-j jitter:\n");
2921 fprintf(stderr, "\t\trandomize signature end time up to jitter "
2923 fprintf(stderr, "\t-v debuglevel (0)\n");
2924 fprintf(stderr, "\t-o origin:\n");
2925 fprintf(stderr, "\t\tzone origin (name of zonefile)\n");
2926 fprintf(stderr, "\t-f outfile:\n");
2927 fprintf(stderr, "\t\tfile the signed zone is written in "
2928 "(zonefile + .signed)\n");
2929 fprintf(stderr, "\t-I format:\n");
2930 fprintf(stderr, "\t\tfile format of input zonefile (text)\n");
2931 fprintf(stderr, "\t-O format:\n");
2932 fprintf(stderr, "\t\tfile format of signed zone file (text)\n");
2933 fprintf(stderr, "\t-N format:\n");
2934 fprintf(stderr, "\t\tsoa serial format of signed zone file (keep)\n");
2935 fprintf(stderr, "\t-r randomdev:\n");
2936 fprintf(stderr, "\t\ta file containing random data\n");
2937 fprintf(stderr, "\t-a:\t");
2938 fprintf(stderr, "verify generated signatures\n");
2939 fprintf(stderr, "\t-p:\t");
2940 fprintf(stderr, "use pseudorandom data (faster but less secure)\n");
2941 fprintf(stderr, "\t-P:\t");
2942 fprintf(stderr, "disable post-sign verification\n");
2943 fprintf(stderr, "\t-t:\t");
2944 fprintf(stderr, "print statistics\n");
2945 fprintf(stderr, "\t-n ncpus (number of cpus present)\n");
2946 fprintf(stderr, "\t-k key_signing_key\n");
2947 fprintf(stderr, "\t-l lookasidezone\n");
2948 fprintf(stderr, "\t-3 salt (NSEC3 salt)\n");
2949 fprintf(stderr, "\t-H iterations (NSEC3 iterations)\n");
2950 fprintf(stderr, "\t-A (NSEC3 optout)\n");
2951 fprintf(stderr, "\t-z:\t");
2952 fprintf(stderr, "ignore KSK flag in DNSKEYs");
2954 fprintf(stderr, "\n");
2956 fprintf(stderr, "Signing Keys: ");
2957 fprintf(stderr, "(default: all zone keys that have private keys)\n");
2958 fprintf(stderr, "\tkeyfile (Kname+alg+tag)\n");
2963 removetempfile(void) {
2965 isc_file_remove(tempfile);
2969 print_stats(isc_time_t *timer_start, isc_time_t *timer_finish) {
2970 isc_uint64_t runtime_us; /* Runtime in microseconds */
2971 isc_uint64_t runtime_ms; /* Runtime in milliseconds */
2972 isc_uint64_t sig_ms; /* Signatures per millisecond */
2974 runtime_us = isc_time_microdiff(timer_finish, timer_start);
2976 printf("Signatures generated: %10d\n", nsigned);
2977 printf("Signatures retained: %10d\n", nretained);
2978 printf("Signatures dropped: %10d\n", ndropped);
2979 printf("Signatures successfully verified: %10d\n", nverified);
2980 printf("Signatures unsuccessfully verified: %10d\n", nverifyfailed);
2981 runtime_ms = runtime_us / 1000;
2982 printf("Runtime in seconds: %7u.%03u\n",
2983 (unsigned int) (runtime_ms / 1000),
2984 (unsigned int) (runtime_ms % 1000));
2985 if (runtime_us > 0) {
2986 sig_ms = ((isc_uint64_t)nsigned * 1000000000) / runtime_us;
2987 printf("Signatures per second: %7u.%03u\n",
2988 (unsigned int) sig_ms / 1000,
2989 (unsigned int) sig_ms % 1000);
2994 main(int argc, char *argv[]) {
2996 char *startstr = NULL, *endstr = NULL, *classname = NULL;
2997 char *origin = NULL, *file = NULL, *output = NULL;
2998 char *inputformatstr = NULL, *outputformatstr = NULL;
2999 char *serialformatstr = NULL;
3000 char *dskeyfile[MAXDSKEYS];
3003 isc_time_t timer_start, timer_finish;
3005 isc_result_t result;
3006 isc_log_t *log = NULL;
3007 isc_boolean_t pseudorandom = ISC_FALSE;
3008 unsigned int eflags;
3009 isc_boolean_t free_output = ISC_FALSE;
3011 dns_rdataclass_t rdclass;
3012 isc_task_t **tasks = NULL;
3015 unsigned int iterations = 100U;
3016 const unsigned char *salt = NULL;
3017 size_t salt_length = 0;
3018 unsigned char saltbuf[255];
3019 hashlist_t hashlist;
3021 #define CMDLINE_FLAGS "3:aAc:d:e:f:FghH:i:I:j:k:l:m:n:N:o:O:pPr:s:StUv:z"
3024 * Process memory debugging argument first.
3026 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
3029 if (strcasecmp(isc_commandline_argument, "record") == 0)
3030 isc_mem_debugging |= ISC_MEM_DEBUGRECORD;
3031 if (strcasecmp(isc_commandline_argument, "trace") == 0)
3032 isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
3033 if (strcasecmp(isc_commandline_argument, "usage") == 0)
3034 isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
3035 if (strcasecmp(isc_commandline_argument, "size") == 0)
3036 isc_mem_debugging |= ISC_MEM_DEBUGSIZE;
3037 if (strcasecmp(isc_commandline_argument, "mctx") == 0)
3038 isc_mem_debugging |= ISC_MEM_DEBUGCTX;
3044 isc_commandline_reset = ISC_TRUE;
3046 masterstyle = &dns_master_style_explicitttl;
3048 check_result(isc_app_start(), "isc_app_start");
3050 result = isc_mem_create(0, 0, &mctx);
3051 if (result != ISC_R_SUCCESS)
3052 fatal("out of memory");
3054 dns_result_register();
3056 isc_commandline_errprint = ISC_FALSE;
3058 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
3061 if (strcmp(isc_commandline_argument, "-")) {
3062 isc_buffer_t target;
3065 sarg = isc_commandline_argument;
3066 isc_buffer_init(&target, saltbuf,
3068 result = isc_hex_decodestring(sarg, &target);
3069 check_result(result,
3070 "isc_hex_decodestring(salt)");
3072 salt_length = isc_buffer_usedlength(&target);
3077 nsec_datatype = dns_rdatatype_nsec3;
3081 nsec3flags |= DNS_NSEC3FLAG_OPTOUT;
3085 tryverify = ISC_TRUE;
3089 classname = isc_commandline_argument;
3093 directory = isc_commandline_argument;
3097 endstr = isc_commandline_argument;
3101 output = isc_commandline_argument;
3105 generateds = ISC_TRUE;
3109 iterations = strtoul(isc_commandline_argument,
3112 fatal("iterations must be numeric");
3113 if (iterations > 0xffffU)
3114 fatal("iterations too big");
3123 cycle = strtol(isc_commandline_argument, &endp, 0);
3124 if (*endp != '\0' || cycle < 0)
3125 fatal("cycle period must be numeric and "
3130 inputformatstr = isc_commandline_argument;
3135 jitter = strtol(isc_commandline_argument, &endp, 0);
3136 if (*endp != '\0' || jitter < 0)
3137 fatal("jitter must be numeric and positive");
3141 if (ndskeys == MAXDSKEYS)
3142 fatal("too many key-signing keys specified");
3143 dskeyfile[ndskeys++] = isc_commandline_argument;
3147 len = strlen(isc_commandline_argument);
3148 isc_buffer_init(&b, isc_commandline_argument, len);
3149 isc_buffer_add(&b, len);
3151 dns_fixedname_init(&dlv_fixed);
3152 dlv = dns_fixedname_name(&dlv_fixed);
3153 result = dns_name_fromtext(dlv, &b, dns_rootname,
3155 check_result(result, "dns_name_fromtext(dlv)");
3163 ntasks = strtol(isc_commandline_argument, &endp, 0);
3164 if (*endp != '\0' || ntasks > ISC_INT32_MAX)
3165 fatal("number of cpus must be numeric");
3169 serialformatstr = isc_commandline_argument;
3173 origin = isc_commandline_argument;
3177 outputformatstr = isc_commandline_argument;
3181 pseudorandom = ISC_TRUE;
3185 disable_zone_check = ISC_TRUE;
3189 setup_entropy(mctx, isc_commandline_argument, &ectx);
3193 startstr = isc_commandline_argument;
3197 /* This is intentionally undocumented */
3198 /* -S: simple output style */
3199 masterstyle = &dns_master_style_simple;
3203 printstats = ISC_TRUE;
3206 case 'U': /* Undocumented for testing only. */
3207 unknownalg = ISC_TRUE;
3212 verbose = strtol(isc_commandline_argument, &endp, 0);
3214 fatal("verbose level must be numeric");
3218 ignoreksk = ISC_TRUE;
3222 /* Reserved for FIPS mode */
3225 if (isc_commandline_option != '?')
3226 fprintf(stderr, "%s: invalid argument -%c\n",
3227 program, isc_commandline_option);
3232 fprintf(stderr, "%s: unhandled option -%c\n",
3233 program, isc_commandline_option);
3239 setup_entropy(mctx, NULL, &ectx);
3240 eflags = ISC_ENTROPY_BLOCKING;
3242 eflags |= ISC_ENTROPY_GOODONLY;
3244 result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
3245 if (result != ISC_R_SUCCESS)
3246 fatal("could not create hash context");
3248 result = dst_lib_init(mctx, ectx, eflags);
3249 if (result != ISC_R_SUCCESS)
3250 fatal("could not initialize dst");
3252 isc_stdtime_get(&now);
3254 if (startstr != NULL)
3255 starttime = strtotime(startstr, now, now);
3257 starttime = now - 3600; /* Allow for some clock skew. */
3260 endtime = strtotime(endstr, now, starttime);
3262 endtime = starttime + (30 * 24 * 60 * 60);
3265 cycle = (endtime - starttime) / 4;
3268 ntasks = isc_os_ncpus() * 2;
3269 vbprintf(4, "using %d cpus\n", ntasks);
3271 rdclass = strtoclass(classname);
3273 setup_logging(verbose, mctx, &log);
3275 argc -= isc_commandline_index;
3276 argv += isc_commandline_index;
3289 if (output == NULL) {
3290 free_output = ISC_TRUE;
3291 output = isc_mem_allocate(mctx,
3292 strlen(file) + strlen(".signed") + 1);
3294 fatal("out of memory");
3295 sprintf(output, "%s.signed", file);
3298 if (inputformatstr != NULL) {
3299 if (strcasecmp(inputformatstr, "text") == 0)
3300 inputformat = dns_masterformat_text;
3301 else if (strcasecmp(inputformatstr, "raw") == 0)
3302 inputformat = dns_masterformat_raw;
3304 fatal("unknown file format: %s\n", inputformatstr);
3307 if (outputformatstr != NULL) {
3308 if (strcasecmp(outputformatstr, "text") == 0)
3309 outputformat = dns_masterformat_text;
3310 else if (strcasecmp(outputformatstr, "raw") == 0)
3311 outputformat = dns_masterformat_raw;
3313 fatal("unknown file format: %s\n", outputformatstr);
3316 if (serialformatstr != NULL) {
3317 if (strcasecmp(serialformatstr, "keep") == 0)
3318 serialformat = SOA_SERIAL_KEEP;
3319 else if (strcasecmp(serialformatstr, "increment") == 0 ||
3320 strcasecmp(serialformatstr, "incr") == 0)
3321 serialformat = SOA_SERIAL_INCREMENT;
3322 else if (strcasecmp(serialformatstr, "unixtime") == 0)
3323 serialformat = SOA_SERIAL_UNIXTIME;
3325 fatal("unknown soa serial format: %s\n",
3329 result = dns_master_stylecreate(&dsstyle, DNS_STYLEFLAG_NO_TTL,
3330 0, 24, 0, 0, 0, 8, mctx);
3331 check_result(result, "dns_master_stylecreate");
3334 TIME_NOW(&timer_start);
3335 loadzone(file, origin, rdclass, &gdb);
3336 gorigin = dns_db_origin(gdb);
3337 gclass = dns_db_class(gdb);
3341 isc_boolean_t answer;
3342 hash_length = dns_nsec3_hashlength(dns_hash_sha1);
3343 hashlist_init(&hashlist, dns_db_nodecount(gdb) * 2,
3345 result = dns_nsec_nseconly(gdb, gversion, &answer);
3346 check_result(result, "dns_nsec_nseconly");
3348 fatal("NSEC3 generation requested with "
3349 "NSEC only DNSKEY");
3353 * We need to do this early on, as we start messing with the list
3354 * of keys rather early.
3356 ISC_LIST_INIT(keylist);
3357 isc_rwlock_init(&keylist_lock, 0, 0);
3362 for (i = 0; i < argc; i++) {
3363 dst_key_t *newkey = NULL;
3365 result = dst_key_fromnamedfile(argv[i],
3369 if (result != ISC_R_SUCCESS)
3370 fatal("cannot load dnskey %s: %s", argv[i],
3371 isc_result_totext(result));
3373 if (!dns_name_equal(gorigin, dst_key_name(newkey)))
3374 fatal("key %s not at origin\n", argv[i]);
3376 key = ISC_LIST_HEAD(keylist);
3377 while (key != NULL) {
3378 dst_key_t *dkey = key->key;
3379 if (dst_key_id(dkey) == dst_key_id(newkey) &&
3380 dst_key_alg(dkey) == dst_key_alg(newkey) &&
3381 dns_name_equal(dst_key_name(dkey),
3382 dst_key_name(newkey)))
3384 if (!dst_key_isprivate(dkey))
3385 fatal("cannot sign zone with "
3386 "non-private dnskey %s",
3390 key = ISC_LIST_NEXT(key, link);
3393 key = newkeystruct(newkey, ISC_TRUE);
3394 key->commandline = ISC_TRUE;
3395 ISC_LIST_APPEND(keylist, key, link);
3397 dst_key_free(&newkey);
3400 loadzonepubkeys(gdb);
3403 for (i = 0; i < ndskeys; i++) {
3404 dst_key_t *newkey = NULL;
3406 result = dst_key_fromnamedfile(dskeyfile[i],
3410 if (result != ISC_R_SUCCESS)
3411 fatal("cannot load dnskey %s: %s", dskeyfile[i],
3412 isc_result_totext(result));
3414 if (!dns_name_equal(gorigin, dst_key_name(newkey)))
3415 fatal("key %s not at origin\n", dskeyfile[i]);
3417 key = ISC_LIST_HEAD(keylist);
3418 while (key != NULL) {
3419 dst_key_t *dkey = key->key;
3420 if (dst_key_id(dkey) == dst_key_id(newkey) &&
3421 dst_key_alg(dkey) == dst_key_alg(newkey) &&
3422 dns_name_equal(dst_key_name(dkey),
3423 dst_key_name(newkey)))
3425 /* Override key flags. */
3426 key->issigningkey = ISC_TRUE;
3427 key->isksk = ISC_TRUE;
3428 key->isdsk = ISC_FALSE;
3429 dst_key_free(&dkey);
3433 key = ISC_LIST_NEXT(key, link);
3436 /* Override dnskey flags. */
3437 key = newkeystruct(newkey, ISC_TRUE);
3438 key->isksk = ISC_TRUE;
3439 key->isdsk = ISC_FALSE;
3440 ISC_LIST_APPEND(keylist, key, link);
3444 if (ISC_LIST_EMPTY(keylist)) {
3445 if (disable_zone_check)
3446 fprintf(stderr, "%s: warning: No keys specified "
3447 "or found\n", program);
3449 fatal("No signing keys specified or found.");
3455 result = dns_nsec3_maxiterations(gdb, NULL, mctx, &max);
3456 check_result(result, "dns_nsec3_maxiterations()");
3457 if (iterations > max)
3458 fatal("NSEC3 iterations too big for weakest DNSKEY "
3459 "strength. Maximum iterations allowed %u.", max);
3465 result = dns_db_newversion(gdb, &gversion);
3466 check_result(result, "dns_db_newversion()");
3468 switch (serialformat) {
3469 case SOA_SERIAL_INCREMENT:
3472 case SOA_SERIAL_UNIXTIME:
3475 case SOA_SERIAL_KEEP:
3482 nsec3ify(dns_hash_sha1, iterations, salt, salt_length,
3488 writeset("keyset-", dns_rdatatype_dnskey);
3489 writeset("dsset-", dns_rdatatype_ds);
3491 writeset("dlvset-", dns_rdatatype_dlv);
3495 tempfilelen = strlen(output) + 20;
3496 tempfile = isc_mem_get(mctx, tempfilelen);
3497 if (tempfile == NULL)
3498 fatal("out of memory");
3500 result = isc_file_mktemplate(output, tempfile, tempfilelen);
3501 check_result(result, "isc_file_mktemplate");
3504 result = isc_file_openunique(tempfile, &fp);
3505 if (result != ISC_R_SUCCESS)
3506 fatal("failed to open temporary output file: %s",
3507 isc_result_totext(result));
3508 removefile = ISC_TRUE;
3509 setfatalcallback(&removetempfile);
3514 result = isc_taskmgr_create(mctx, ntasks, 0, &taskmgr);
3515 if (result != ISC_R_SUCCESS)
3516 fatal("failed to create task manager: %s",
3517 isc_result_totext(result));
3520 result = isc_task_create(taskmgr, 0, &master);
3521 if (result != ISC_R_SUCCESS)
3522 fatal("failed to create task: %s", isc_result_totext(result));
3524 tasks = isc_mem_get(mctx, ntasks * sizeof(isc_task_t *));
3526 fatal("out of memory");
3527 for (i = 0; i < (int)ntasks; i++) {
3529 result = isc_task_create(taskmgr, 0, &tasks[i]);
3530 if (result != ISC_R_SUCCESS)
3531 fatal("failed to create task: %s",
3532 isc_result_totext(result));
3535 RUNTIME_CHECK(isc_mutex_init(&namelock) == ISC_R_SUCCESS);
3537 RUNTIME_CHECK(isc_mutex_init(&statslock) == ISC_R_SUCCESS);
3543 * There is more work to do. Spread it out over multiple
3544 * processors if possible.
3546 for (i = 0; i < (int)ntasks; i++) {
3547 result = isc_app_onrun(mctx, master, startworker,
3549 if (result != ISC_R_SUCCESS)
3550 fatal("failed to start task: %s",
3551 isc_result_totext(result));
3553 (void)isc_app_run();
3555 fatal("process aborted by user");
3557 isc_task_detach(&master);
3558 shuttingdown = ISC_TRUE;
3559 for (i = 0; i < (int)ntasks; i++)
3560 isc_task_detach(&tasks[i]);
3561 isc_taskmgr_destroy(&taskmgr);
3562 isc_mem_put(mctx, tasks, ntasks * sizeof(isc_task_t *));
3566 if (outputformat != dns_masterformat_text) {
3567 result = dns_master_dumptostream2(mctx, gdb, gversion,
3568 masterstyle, outputformat,
3570 check_result(result, "dns_master_dumptostream2");
3573 result = isc_stdio_close(fp);
3574 check_result(result, "isc_stdio_close");
3575 removefile = ISC_FALSE;
3577 result = isc_file_rename(tempfile, output);
3578 if (result != ISC_R_SUCCESS)
3579 fatal("failed to rename temp file to %s: %s\n",
3580 output, isc_result_totext(result));
3582 DESTROYLOCK(&namelock);
3584 DESTROYLOCK(&statslock);
3586 printf("%s\n", output);
3588 dns_db_closeversion(gdb, &gversion, ISC_FALSE);
3589 dns_db_detach(&gdb);
3591 while (!ISC_LIST_EMPTY(keylist)) {
3592 key = ISC_LIST_HEAD(keylist);
3593 ISC_LIST_UNLINK(keylist, key, link);
3594 dst_key_free(&key->key);
3595 isc_mem_put(mctx, key, sizeof(signer_key_t));
3598 isc_mem_put(mctx, tempfile, tempfilelen);
3601 isc_mem_free(mctx, output);
3603 dns_master_styledestroy(&dsstyle, mctx);
3605 cleanup_logging(&log);
3608 cleanup_entropy(&ectx);
3611 isc_mem_stats(mctx, stdout);
3612 isc_mem_destroy(&mctx);
3614 (void) isc_app_finish();
3617 TIME_NOW(&timer_finish);
3618 print_stats(&timer_start, &timer_finish);