2 * Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
3 * 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 DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: update.c,v 1.109.18.27 2008/02/07 03:16:08 marka Exp $ */
22 #include <isc/print.h>
23 #include <isc/string.h>
24 #include <isc/taskpool.h>
28 #include <dns/dbiterator.h>
30 #include <dns/dnssec.h>
31 #include <dns/events.h>
32 #include <dns/fixedname.h>
33 #include <dns/journal.h>
34 #include <dns/keyvalues.h>
35 #include <dns/message.h>
37 #include <dns/rdataclass.h>
38 #include <dns/rdataset.h>
39 #include <dns/rdatasetiter.h>
40 #include <dns/rdatastruct.h>
41 #include <dns/rdatatype.h>
48 #include <named/client.h>
49 #include <named/log.h>
50 #include <named/update.h>
54 * This module implements dynamic update as in RFC2136.
59 - document strict minimality
62 /**************************************************************************/
65 * Log level for tracing dynamic update protocol requests.
67 #define LOGLEVEL_PROTOCOL ISC_LOG_INFO
70 * Log level for low-level debug tracing.
72 #define LOGLEVEL_DEBUG ISC_LOG_DEBUG(8)
75 * Check an operation for failure. These macros all assume that
76 * the function using them has a 'result' variable and a 'failure'
81 if (result != ISC_R_SUCCESS) goto failure; \
85 * Fail unconditionally with result 'code', which must not
86 * be ISC_R_SUCCESS. The reason for failure presumably has
87 * been logged already.
89 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
90 * from complaining about "end-of-loop code not reached".
96 if (result != ISC_R_SUCCESS) goto failure; \
100 * Fail unconditionally and log as a client error.
101 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
102 * from complaining about "end-of-loop code not reached".
104 #define FAILC(code, msg) \
106 const char *_what = "failed"; \
109 case DNS_R_NXDOMAIN: \
110 case DNS_R_YXDOMAIN: \
111 case DNS_R_YXRRSET: \
112 case DNS_R_NXRRSET: \
113 _what = "unsuccessful"; \
115 update_log(client, zone, LOGLEVEL_PROTOCOL, \
116 "update %s: %s (%s)", _what, \
117 msg, isc_result_totext(result)); \
118 if (result != ISC_R_SUCCESS) goto failure; \
121 #define FAILN(code, name, msg) \
123 const char *_what = "failed"; \
126 case DNS_R_NXDOMAIN: \
127 case DNS_R_YXDOMAIN: \
128 case DNS_R_YXRRSET: \
129 case DNS_R_NXRRSET: \
130 _what = "unsuccessful"; \
132 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
133 char _nbuf[DNS_NAME_FORMATSIZE]; \
134 dns_name_format(name, _nbuf, sizeof(_nbuf)); \
135 update_log(client, zone, LOGLEVEL_PROTOCOL, \
136 "update %s: %s: %s (%s)", _what, _nbuf, \
137 msg, isc_result_totext(result)); \
139 if (result != ISC_R_SUCCESS) goto failure; \
142 #define FAILNT(code, name, type, msg) \
144 const char *_what = "failed"; \
147 case DNS_R_NXDOMAIN: \
148 case DNS_R_YXDOMAIN: \
149 case DNS_R_YXRRSET: \
150 case DNS_R_NXRRSET: \
151 _what = "unsuccessful"; \
153 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
154 char _nbuf[DNS_NAME_FORMATSIZE]; \
155 char _tbuf[DNS_RDATATYPE_FORMATSIZE]; \
156 dns_name_format(name, _nbuf, sizeof(_nbuf)); \
157 dns_rdatatype_format(type, _tbuf, sizeof(_tbuf)); \
158 update_log(client, zone, LOGLEVEL_PROTOCOL, \
159 "update %s: %s/%s: %s (%s)", \
160 _what, _nbuf, _tbuf, msg, \
161 isc_result_totext(result)); \
163 if (result != ISC_R_SUCCESS) goto failure; \
166 * Fail unconditionally and log as a server error.
167 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
168 * from complaining about "end-of-loop code not reached".
170 #define FAILS(code, msg) \
173 update_log(client, zone, LOGLEVEL_PROTOCOL, \
175 msg, isc_result_totext(result)); \
176 if (result != ISC_R_SUCCESS) goto failure; \
179 /**************************************************************************/
181 typedef struct rr rr_t;
184 /* dns_name_t name; */
189 typedef struct update_event update_event_t;
191 struct update_event {
192 ISC_EVENT_COMMON(update_event_t);
195 dns_message_t *answer;
198 /**************************************************************************/
200 * Forward declarations.
203 static void update_action(isc_task_t *task, isc_event_t *event);
204 static void updatedone_action(isc_task_t *task, isc_event_t *event);
205 static isc_result_t send_forward_event(ns_client_t *client, dns_zone_t *zone);
206 static void forward_done(isc_task_t *task, isc_event_t *event);
208 /**************************************************************************/
211 update_log(ns_client_t *client, dns_zone_t *zone,
212 int level, const char *fmt, ...) ISC_FORMAT_PRINTF(4, 5);
215 update_log(ns_client_t *client, dns_zone_t *zone,
216 int level, const char *fmt, ...)
220 char namebuf[DNS_NAME_FORMATSIZE];
221 char classbuf[DNS_RDATACLASS_FORMATSIZE];
223 if (client == NULL || zone == NULL)
226 if (isc_log_wouldlog(ns_g_lctx, level) == ISC_FALSE)
229 dns_name_format(dns_zone_getorigin(zone), namebuf,
231 dns_rdataclass_format(dns_zone_getclass(zone), classbuf,
235 vsnprintf(message, sizeof(message), fmt, ap);
238 ns_client_log(client, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
239 level, "updating zone '%s/%s': %s",
240 namebuf, classbuf, message);
244 checkupdateacl(ns_client_t *client, dns_acl_t *acl, const char *message,
245 dns_name_t *zonename, isc_boolean_t slave)
247 char namebuf[DNS_NAME_FORMATSIZE];
248 char classbuf[DNS_RDATACLASS_FORMATSIZE];
249 int level = ISC_LOG_ERROR;
250 const char *msg = "denied";
253 if (slave && acl == NULL) {
254 result = DNS_R_NOTIMP;
255 level = ISC_LOG_DEBUG(3);
258 result = ns_client_checkaclsilent(client, acl, ISC_FALSE);
260 if (result == ISC_R_SUCCESS) {
261 level = ISC_LOG_DEBUG(3);
265 dns_name_format(zonename, namebuf, sizeof(namebuf));
266 dns_rdataclass_format(client->view->rdclass, classbuf,
269 ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
270 NS_LOGMODULE_UPDATE, level, "%s '%s/%s' %s",
271 message, namebuf, classbuf, msg);
276 * Update a single RR in version 'ver' of 'db' and log the
280 * \li '*tuple' == NULL. Either the tuple is freed, or its
281 * ownership has been transferred to the diff.
284 do_one_tuple(dns_difftuple_t **tuple,
285 dns_db_t *db, dns_dbversion_t *ver,
288 dns_diff_t temp_diff;
292 * Create a singleton diff.
294 dns_diff_init(diff->mctx, &temp_diff);
295 ISC_LIST_APPEND(temp_diff.tuples, *tuple, link);
298 * Apply it to the database.
300 result = dns_diff_apply(&temp_diff, db, ver);
301 ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link);
302 if (result != ISC_R_SUCCESS) {
303 dns_difftuple_free(tuple);
308 * Merge it into the current pending journal entry.
310 dns_diff_appendminimal(diff, tuple);
313 * Do not clear temp_diff.
315 return (ISC_R_SUCCESS);
319 * Perform the updates in 'updates' in version 'ver' of 'db' and log the
323 * \li 'updates' is empty.
326 do_diff(dns_diff_t *updates, dns_db_t *db, dns_dbversion_t *ver,
330 while (! ISC_LIST_EMPTY(updates->tuples)) {
331 dns_difftuple_t *t = ISC_LIST_HEAD(updates->tuples);
332 ISC_LIST_UNLINK(updates->tuples, t, link);
333 CHECK(do_one_tuple(&t, db, ver, diff));
335 return (ISC_R_SUCCESS);
338 dns_diff_clear(diff);
343 update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
344 dns_diffop_t op, dns_name_t *name,
345 dns_ttl_t ttl, dns_rdata_t *rdata)
347 dns_difftuple_t *tuple = NULL;
349 result = dns_difftuple_create(diff->mctx, op,
350 name, ttl, rdata, &tuple);
351 if (result != ISC_R_SUCCESS)
353 return (do_one_tuple(&tuple, db, ver, diff));
356 /**************************************************************************/
358 * Callback-style iteration over rdatasets and rdatas.
360 * foreach_rrset() can be used to iterate over the RRsets
361 * of a name and call a callback function with each
362 * one. Similarly, foreach_rr() can be used to iterate
363 * over the individual RRs at name, optionally restricted
364 * to RRs of a given type.
366 * The callback functions are called "actions" and take
367 * two arguments: a void pointer for passing arbitrary
368 * context information, and a pointer to the current RRset
369 * or RR. By convention, their names end in "_action".
373 * XXXRTH We might want to make this public somewhere in libdns.
377 * Function type for foreach_rrset() iterator actions.
379 typedef isc_result_t rrset_func(void *data, dns_rdataset_t *rrset);
382 * Function type for foreach_rr() iterator actions.
384 typedef isc_result_t rr_func(void *data, rr_t *rr);
387 * Internal context struct for foreach_node_rr().
391 void * rr_action_data;
392 } foreach_node_rr_ctx_t;
395 * Internal helper function for foreach_node_rr().
398 foreach_node_rr_action(void *data, dns_rdataset_t *rdataset) {
400 foreach_node_rr_ctx_t *ctx = data;
401 for (result = dns_rdataset_first(rdataset);
402 result == ISC_R_SUCCESS;
403 result = dns_rdataset_next(rdataset))
405 rr_t rr = { 0, DNS_RDATA_INIT };
407 dns_rdataset_current(rdataset, &rr.rdata);
408 rr.ttl = rdataset->ttl;
409 result = (*ctx->rr_action)(ctx->rr_action_data, &rr);
410 if (result != ISC_R_SUCCESS)
413 if (result != ISC_R_NOMORE)
415 return (ISC_R_SUCCESS);
419 * For each rdataset of 'name' in 'ver' of 'db', call 'action'
420 * with the rdataset and 'action_data' as arguments. If the name
421 * does not exist, do nothing.
423 * If 'action' returns an error, abort iteration and return the error.
426 foreach_rrset(dns_db_t *db,
427 dns_dbversion_t *ver,
434 dns_rdatasetiter_t *iter;
437 result = dns_db_findnode(db, name, ISC_FALSE, &node);
438 if (result == ISC_R_NOTFOUND)
439 return (ISC_R_SUCCESS);
440 if (result != ISC_R_SUCCESS)
444 result = dns_db_allrdatasets(db, node, ver,
445 (isc_stdtime_t) 0, &iter);
446 if (result != ISC_R_SUCCESS)
449 for (result = dns_rdatasetiter_first(iter);
450 result == ISC_R_SUCCESS;
451 result = dns_rdatasetiter_next(iter))
453 dns_rdataset_t rdataset;
455 dns_rdataset_init(&rdataset);
456 dns_rdatasetiter_current(iter, &rdataset);
458 result = (*action)(action_data, &rdataset);
460 dns_rdataset_disassociate(&rdataset);
461 if (result != ISC_R_SUCCESS)
462 goto cleanup_iterator;
464 if (result == ISC_R_NOMORE)
465 result = ISC_R_SUCCESS;
468 dns_rdatasetiter_destroy(&iter);
471 dns_db_detachnode(db, &node);
477 * For each RR of 'name' in 'ver' of 'db', call 'action'
478 * with the RR and 'action_data' as arguments. If the name
479 * does not exist, do nothing.
481 * If 'action' returns an error, abort iteration
482 * and return the error.
485 foreach_node_rr(dns_db_t *db,
486 dns_dbversion_t *ver,
489 void *rr_action_data)
491 foreach_node_rr_ctx_t ctx;
492 ctx.rr_action = rr_action;
493 ctx.rr_action_data = rr_action_data;
494 return (foreach_rrset(db, ver, name,
495 foreach_node_rr_action, &ctx));
500 * For each of the RRs specified by 'db', 'ver', 'name', 'type',
501 * (which can be dns_rdatatype_any to match any type), and 'covers', call
502 * 'action' with the RR and 'action_data' as arguments. If the name
503 * does not exist, or if no RRset of the given type exists at the name,
506 * If 'action' returns an error, abort iteration and return the error.
509 foreach_rr(dns_db_t *db,
510 dns_dbversion_t *ver,
512 dns_rdatatype_t type,
513 dns_rdatatype_t covers,
515 void *rr_action_data)
520 dns_rdataset_t rdataset;
522 if (type == dns_rdatatype_any)
523 return (foreach_node_rr(db, ver, name,
524 rr_action, rr_action_data));
527 result = dns_db_findnode(db, name, ISC_FALSE, &node);
528 if (result == ISC_R_NOTFOUND)
529 return (ISC_R_SUCCESS);
530 if (result != ISC_R_SUCCESS)
533 dns_rdataset_init(&rdataset);
534 result = dns_db_findrdataset(db, node, ver, type, covers,
535 (isc_stdtime_t) 0, &rdataset, NULL);
536 if (result == ISC_R_NOTFOUND) {
537 result = ISC_R_SUCCESS;
540 if (result != ISC_R_SUCCESS)
543 for (result = dns_rdataset_first(&rdataset);
544 result == ISC_R_SUCCESS;
545 result = dns_rdataset_next(&rdataset))
547 rr_t rr = { 0, DNS_RDATA_INIT };
548 dns_rdataset_current(&rdataset, &rr.rdata);
549 rr.ttl = rdataset.ttl;
550 result = (*rr_action)(rr_action_data, &rr);
551 if (result != ISC_R_SUCCESS)
552 goto cleanup_rdataset;
554 if (result != ISC_R_NOMORE)
555 goto cleanup_rdataset;
556 result = ISC_R_SUCCESS;
559 dns_rdataset_disassociate(&rdataset);
561 dns_db_detachnode(db, &node);
566 /**************************************************************************/
568 * Various tests on the database contents (for prerequisites, etc).
572 * Function type for predicate functions that compare a database RR 'db_rr'
573 * against an update RR 'update_rr'.
575 typedef isc_boolean_t rr_predicate(dns_rdata_t *update_rr, dns_rdata_t *db_rr);
578 * Helper function for rrset_exists().
581 rrset_exists_action(void *data, rr_t *rr) {
584 return (ISC_R_EXISTS);
588 * Utility macro for RR existence checking functions.
590 * If the variable 'result' has the value ISC_R_EXISTS or
591 * ISC_R_SUCCESS, set *exists to ISC_TRUE or ISC_FALSE,
592 * respectively, and return success.
594 * If 'result' has any other value, there was a failure.
595 * Return the failure result code and do not set *exists.
597 * This would be more readable as "do { if ... } while(0)",
598 * but that form generates tons of warnings on Solaris 2.6.
600 #define RETURN_EXISTENCE_FLAG \
601 return ((result == ISC_R_EXISTS) ? \
602 (*exists = ISC_TRUE, ISC_R_SUCCESS) : \
603 ((result == ISC_R_SUCCESS) ? \
604 (*exists = ISC_FALSE, ISC_R_SUCCESS) : \
608 * Set '*exists' to true iff an rrset of the given type exists,
609 * to false otherwise.
612 rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
613 dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers,
614 isc_boolean_t *exists)
617 result = foreach_rr(db, ver, name, type, covers,
618 rrset_exists_action, NULL);
619 RETURN_EXISTENCE_FLAG;
623 * Helper function for cname_incompatible_rrset_exists.
626 cname_compatibility_action(void *data, dns_rdataset_t *rrset) {
628 if (rrset->type != dns_rdatatype_cname &&
629 ! dns_rdatatype_isdnssec(rrset->type))
630 return (ISC_R_EXISTS);
631 return (ISC_R_SUCCESS);
635 * Check whether there is an rrset incompatible with adding a CNAME RR,
636 * i.e., anything but another CNAME (which can be replaced) or a
637 * DNSSEC RR (which can coexist).
639 * If such an incompatible rrset exists, set '*exists' to ISC_TRUE.
640 * Otherwise, set it to ISC_FALSE.
643 cname_incompatible_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
644 dns_name_t *name, isc_boolean_t *exists) {
646 result = foreach_rrset(db, ver, name,
647 cname_compatibility_action, NULL);
648 RETURN_EXISTENCE_FLAG;
652 * Helper function for rr_count().
655 count_rr_action(void *data, rr_t *rr) {
659 return (ISC_R_SUCCESS);
663 * Count the number of RRs of 'type' belonging to 'name' in 'ver' of 'db'.
666 rr_count(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
667 dns_rdatatype_t type, dns_rdatatype_t covers, int *countp)
670 return (foreach_rr(db, ver, name, type, covers,
671 count_rr_action, countp));
675 * Context struct and helper function for name_exists().
679 name_exists_action(void *data, dns_rdataset_t *rrset) {
682 return (ISC_R_EXISTS);
686 * Set '*exists' to true iff the given name exists, to false otherwise.
689 name_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
690 isc_boolean_t *exists)
693 result = foreach_rrset(db, ver, name,
694 name_exists_action, NULL);
695 RETURN_EXISTENCE_FLAG;
699 dns_name_t *name, *signer;
700 dns_ssutable_t *table;
704 ssu_checkrule(void *data, dns_rdataset_t *rrset) {
705 ssu_check_t *ssuinfo = data;
706 isc_boolean_t result;
709 * If we're deleting all records, it's ok to delete RRSIG and NSEC even
710 * if we're normally not allowed to.
712 if (rrset->type == dns_rdatatype_rrsig ||
713 rrset->type == dns_rdatatype_nsec)
714 return (ISC_R_SUCCESS);
715 result = dns_ssutable_checkrules(ssuinfo->table, ssuinfo->signer,
716 ssuinfo->name, rrset->type);
717 return (result == ISC_TRUE ? ISC_R_SUCCESS : ISC_R_FAILURE);
721 ssu_checkall(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
722 dns_ssutable_t *ssutable, dns_name_t *signer)
728 ssuinfo.table = ssutable;
729 ssuinfo.signer = signer;
730 result = foreach_rrset(db, ver, name, ssu_checkrule, &ssuinfo);
731 return (ISC_TF(result == ISC_R_SUCCESS));
734 /**************************************************************************/
736 * Checking of "RRset exists (value dependent)" prerequisites.
738 * In the RFC2136 section 3.2.5, this is the pseudocode involving
739 * a variable called "temp", a mapping of <name, type> tuples to rrsets.
741 * Here, we represent the "temp" data structure as (non-minimial) "dns_diff_t"
742 * where each typle has op==DNS_DIFFOP_EXISTS.
747 * Append a tuple asserting the existence of the RR with
748 * 'name' and 'rdata' to 'diff'.
751 temp_append(dns_diff_t *diff, dns_name_t *name, dns_rdata_t *rdata) {
753 dns_difftuple_t *tuple = NULL;
755 REQUIRE(DNS_DIFF_VALID(diff));
756 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_EXISTS,
757 name, 0, rdata, &tuple));
758 ISC_LIST_APPEND(diff->tuples, tuple, link);
764 * Compare two rdatasets represented as sorted lists of tuples.
765 * All list elements must have the same owner name and type.
766 * Return ISC_R_SUCCESS if the rdatasets are equal, rcode(dns_rcode_nxrrset)
770 temp_check_rrset(dns_difftuple_t *a, dns_difftuple_t *b) {
772 if (a == NULL || b == NULL)
774 INSIST(a->op == DNS_DIFFOP_EXISTS &&
775 b->op == DNS_DIFFOP_EXISTS);
776 INSIST(a->rdata.type == b->rdata.type);
777 INSIST(dns_name_equal(&a->name, &b->name));
778 if (dns_rdata_compare(&a->rdata, &b->rdata) != 0)
779 return (DNS_R_NXRRSET);
780 a = ISC_LIST_NEXT(a, link);
781 b = ISC_LIST_NEXT(b, link);
783 if (a != NULL || b != NULL)
784 return (DNS_R_NXRRSET);
785 return (ISC_R_SUCCESS);
789 * A comparison function defining the sorting order for the entries
790 * in the "temp" data structure. The major sort key is the owner name,
791 * followed by the type and rdata.
794 temp_order(const void *av, const void *bv) {
795 dns_difftuple_t const * const *ap = av;
796 dns_difftuple_t const * const *bp = bv;
797 dns_difftuple_t const *a = *ap;
798 dns_difftuple_t const *b = *bp;
800 r = dns_name_compare(&a->name, &b->name);
803 r = (b->rdata.type - a->rdata.type);
806 r = dns_rdata_compare(&a->rdata, &b->rdata);
811 * Check the "RRset exists (value dependent)" prerequisite information
812 * in 'temp' against the contents of the database 'db'.
814 * Return ISC_R_SUCCESS if the prerequisites are satisfied,
815 * rcode(dns_rcode_nxrrset) if not.
817 * 'temp' must be pre-sorted.
821 temp_check(isc_mem_t *mctx, dns_diff_t *temp, dns_db_t *db,
822 dns_dbversion_t *ver, dns_name_t *tmpname, dns_rdatatype_t *typep)
830 dns_diff_init(mctx, &trash);
833 * For each name and type in the prerequisites,
834 * construct a sorted rdata list of the corresponding
835 * database contents, and compare the lists.
837 t = ISC_LIST_HEAD(temp->tuples);
840 (void)dns_name_copy(name, tmpname, NULL);
841 *typep = t->rdata.type;
843 /* A new unique name begins here. */
845 result = dns_db_findnode(db, name, ISC_FALSE, &node);
846 if (result == ISC_R_NOTFOUND) {
847 dns_diff_clear(&trash);
848 return (DNS_R_NXRRSET);
850 if (result != ISC_R_SUCCESS) {
851 dns_diff_clear(&trash);
855 /* A new unique type begins here. */
856 while (t != NULL && dns_name_equal(&t->name, name)) {
857 dns_rdatatype_t type, covers;
858 dns_rdataset_t rdataset;
859 dns_diff_t d_rrs; /* Database RRs with
860 this name and type */
861 dns_diff_t u_rrs; /* Update RRs with
862 this name and type */
864 *typep = type = t->rdata.type;
865 if (type == dns_rdatatype_rrsig ||
866 type == dns_rdatatype_sig)
867 covers = dns_rdata_covers(&t->rdata);
872 * Collect all database RRs for this name and type
873 * onto d_rrs and sort them.
875 dns_rdataset_init(&rdataset);
876 result = dns_db_findrdataset(db, node, ver, type,
877 covers, (isc_stdtime_t) 0,
879 if (result != ISC_R_SUCCESS) {
880 dns_db_detachnode(db, &node);
881 dns_diff_clear(&trash);
882 return (DNS_R_NXRRSET);
885 dns_diff_init(mctx, &d_rrs);
886 dns_diff_init(mctx, &u_rrs);
888 for (result = dns_rdataset_first(&rdataset);
889 result == ISC_R_SUCCESS;
890 result = dns_rdataset_next(&rdataset))
892 dns_rdata_t rdata = DNS_RDATA_INIT;
893 dns_rdataset_current(&rdataset, &rdata);
894 result = temp_append(&d_rrs, name, &rdata);
895 if (result != ISC_R_SUCCESS)
898 if (result != ISC_R_NOMORE)
900 result = dns_diff_sort(&d_rrs, temp_order);
901 if (result != ISC_R_SUCCESS)
905 * Collect all update RRs for this name and type
906 * onto u_rrs. No need to sort them here -
907 * they are already sorted.
910 dns_name_equal(&t->name, name) &&
911 t->rdata.type == type)
913 dns_difftuple_t *next =
914 ISC_LIST_NEXT(t, link);
915 ISC_LIST_UNLINK(temp->tuples, t, link);
916 ISC_LIST_APPEND(u_rrs.tuples, t, link);
920 /* Compare the two sorted lists. */
921 result = temp_check_rrset(ISC_LIST_HEAD(u_rrs.tuples),
922 ISC_LIST_HEAD(d_rrs.tuples));
923 if (result != ISC_R_SUCCESS)
927 * We are done with the tuples, but we can't free
928 * them yet because "name" still points into one
929 * of them. Move them on a temporary list.
931 ISC_LIST_APPENDLIST(trash.tuples, u_rrs.tuples, link);
932 ISC_LIST_APPENDLIST(trash.tuples, d_rrs.tuples, link);
933 dns_rdataset_disassociate(&rdataset);
938 dns_diff_clear(&d_rrs);
939 dns_diff_clear(&u_rrs);
940 dns_diff_clear(&trash);
941 dns_rdataset_disassociate(&rdataset);
942 dns_db_detachnode(db, &node);
946 dns_db_detachnode(db, &node);
949 dns_diff_clear(&trash);
950 return (ISC_R_SUCCESS);
953 /**************************************************************************/
955 * Conditional deletion of RRs.
959 * Context structure for delete_if().
963 rr_predicate *predicate;
965 dns_dbversion_t *ver;
968 dns_rdata_t *update_rr;
969 } conditional_delete_ctx_t;
972 * Predicate functions for delete_if().
976 * Return true iff 'db_rr' is neither a SOA nor an NS RR nor
977 * an RRSIG nor a NSEC.
980 type_not_soa_nor_ns_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
982 return ((db_rr->type != dns_rdatatype_soa &&
983 db_rr->type != dns_rdatatype_ns &&
984 db_rr->type != dns_rdatatype_rrsig &&
985 db_rr->type != dns_rdatatype_nsec) ?
986 ISC_TRUE : ISC_FALSE);
990 * Return true iff 'db_rr' is neither a RRSIG nor a NSEC.
993 type_not_dnssec(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
995 return ((db_rr->type != dns_rdatatype_rrsig &&
996 db_rr->type != dns_rdatatype_nsec) ?
997 ISC_TRUE : ISC_FALSE);
1001 * Return true always.
1003 static isc_boolean_t
1004 true_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1011 * Return true iff the two RRs have identical rdata.
1013 static isc_boolean_t
1014 rr_equal_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1016 * XXXRTH This is not a problem, but we should consider creating
1017 * dns_rdata_equal() (that used dns_name_equal()), since it
1018 * would be faster. Not a priority.
1020 return (dns_rdata_compare(update_rr, db_rr) == 0 ?
1021 ISC_TRUE : ISC_FALSE);
1025 * Return true iff 'update_rr' should replace 'db_rr' according
1026 * to the special RFC2136 rules for CNAME, SOA, and WKS records.
1028 * RFC2136 does not mention NSEC or DNAME, but multiple NSECs or DNAMEs
1029 * make little sense, so we replace those, too.
1031 static isc_boolean_t
1032 replaces_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1033 if (db_rr->type != update_rr->type)
1035 if (db_rr->type == dns_rdatatype_cname)
1037 if (db_rr->type == dns_rdatatype_dname)
1039 if (db_rr->type == dns_rdatatype_soa)
1041 if (db_rr->type == dns_rdatatype_nsec)
1043 if (db_rr->type == dns_rdatatype_wks) {
1045 * Compare the address and protocol fields only. These
1046 * form the first five bytes of the RR data. Do a
1047 * raw binary comparison; unpacking the WKS RRs using
1048 * dns_rdata_tostruct() might be cleaner in some ways,
1049 * but it would require us to pass around an mctx.
1051 INSIST(db_rr->length >= 5 && update_rr->length >= 5);
1052 return (memcmp(db_rr->data, update_rr->data, 5) == 0 ?
1053 ISC_TRUE : ISC_FALSE);
1059 * Internal helper function for delete_if().
1062 delete_if_action(void *data, rr_t *rr) {
1063 conditional_delete_ctx_t *ctx = data;
1064 if ((*ctx->predicate)(ctx->update_rr, &rr->rdata)) {
1065 isc_result_t result;
1066 result = update_one_rr(ctx->db, ctx->ver, ctx->diff,
1067 DNS_DIFFOP_DEL, ctx->name,
1068 rr->ttl, &rr->rdata);
1071 return (ISC_R_SUCCESS);
1076 * Conditionally delete RRs. Apply 'predicate' to the RRs
1077 * specified by 'db', 'ver', 'name', and 'type' (which can
1078 * be dns_rdatatype_any to match any type). Delete those
1079 * RRs for which the predicate returns true, and log the
1080 * deletions in 'diff'.
1083 delete_if(rr_predicate *predicate,
1085 dns_dbversion_t *ver,
1087 dns_rdatatype_t type,
1088 dns_rdatatype_t covers,
1089 dns_rdata_t *update_rr,
1092 conditional_delete_ctx_t ctx;
1093 ctx.predicate = predicate;
1098 ctx.update_rr = update_rr;
1099 return (foreach_rr(db, ver, name, type, covers,
1100 delete_if_action, &ctx));
1103 /**************************************************************************/
1105 * Prepare an RR for the addition of the new RR 'ctx->update_rr',
1106 * with TTL 'ctx->update_rr_ttl', to its rdataset, by deleting
1107 * the RRs if it is replaced by the new RR or has a conflicting TTL.
1108 * The necessary changes are appended to ctx->del_diff and ctx->add_diff;
1109 * we need to do all deletions before any additions so that we don't run
1110 * into transient states with conflicting TTLs.
1115 dns_dbversion_t *ver;
1118 dns_rdata_t *update_rr;
1119 dns_ttl_t update_rr_ttl;
1120 isc_boolean_t ignore_add;
1121 dns_diff_t del_diff;
1122 dns_diff_t add_diff;
1123 } add_rr_prepare_ctx_t;
1126 add_rr_prepare_action(void *data, rr_t *rr) {
1127 isc_result_t result = ISC_R_SUCCESS;
1128 add_rr_prepare_ctx_t *ctx = data;
1129 dns_difftuple_t *tuple = NULL;
1130 isc_boolean_t equal;
1133 * If the update RR is a "duplicate" of the update RR,
1134 * the update should be silently ignored.
1136 equal = ISC_TF(dns_rdata_compare(&rr->rdata, ctx->update_rr) == 0);
1137 if (equal && rr->ttl == ctx->update_rr_ttl) {
1138 ctx->ignore_add = ISC_TRUE;
1139 return (ISC_R_SUCCESS);
1143 * If this RR is "equal" to the update RR, it should
1144 * be deleted before the update RR is added.
1146 if (replaces_p(ctx->update_rr, &rr->rdata)) {
1147 CHECK(dns_difftuple_create(ctx->del_diff.mctx,
1148 DNS_DIFFOP_DEL, ctx->name,
1152 dns_diff_append(&ctx->del_diff, &tuple);
1153 return (ISC_R_SUCCESS);
1157 * If this RR differs in TTL from the update RR,
1158 * its TTL must be adjusted.
1160 if (rr->ttl != ctx->update_rr_ttl) {
1161 CHECK(dns_difftuple_create(ctx->del_diff.mctx,
1162 DNS_DIFFOP_DEL, ctx->name,
1166 dns_diff_append(&ctx->del_diff, &tuple);
1168 CHECK(dns_difftuple_create(ctx->add_diff.mctx,
1169 DNS_DIFFOP_ADD, ctx->name,
1173 dns_diff_append(&ctx->add_diff, &tuple);
1180 /**************************************************************************/
1182 * Miscellaneous subroutines.
1186 * Extract a single update RR from 'section' of dynamic update message
1187 * 'msg', with consistency checking.
1189 * Stores the owner name, rdata, and TTL of the update RR at 'name',
1190 * 'rdata', and 'ttl', respectively.
1193 get_current_rr(dns_message_t *msg, dns_section_t section,
1194 dns_rdataclass_t zoneclass,
1195 dns_name_t **name, dns_rdata_t *rdata, dns_rdatatype_t *covers,
1197 dns_rdataclass_t *update_class)
1199 dns_rdataset_t *rdataset;
1200 isc_result_t result;
1201 dns_message_currentname(msg, section, name);
1202 rdataset = ISC_LIST_HEAD((*name)->list);
1203 INSIST(rdataset != NULL);
1204 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
1205 *covers = rdataset->covers;
1206 *ttl = rdataset->ttl;
1207 result = dns_rdataset_first(rdataset);
1208 INSIST(result == ISC_R_SUCCESS);
1209 dns_rdataset_current(rdataset, rdata);
1210 INSIST(dns_rdataset_next(rdataset) == ISC_R_NOMORE);
1211 *update_class = rdata->rdclass;
1212 rdata->rdclass = zoneclass;
1216 * Increment the SOA serial number of database 'db', version 'ver'.
1217 * Replace the SOA record in the database, and log the
1222 * XXXRTH Failures in this routine will be worth logging, when
1223 * we have a logging system. Failure to find the zonename
1224 * or the SOA rdataset warrant at least an UNEXPECTED_ERROR().
1228 increment_soa_serial(dns_db_t *db, dns_dbversion_t *ver,
1229 dns_diff_t *diff, isc_mem_t *mctx)
1231 dns_difftuple_t *deltuple = NULL;
1232 dns_difftuple_t *addtuple = NULL;
1233 isc_uint32_t serial;
1234 isc_result_t result;
1236 CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_DEL, &deltuple));
1237 CHECK(dns_difftuple_copy(deltuple, &addtuple));
1238 addtuple->op = DNS_DIFFOP_ADD;
1240 serial = dns_soa_getserial(&addtuple->rdata);
1243 serial = (serial + 1) & 0xFFFFFFFF;
1247 dns_soa_setserial(serial, &addtuple->rdata);
1248 CHECK(do_one_tuple(&deltuple, db, ver, diff));
1249 CHECK(do_one_tuple(&addtuple, db, ver, diff));
1250 result = ISC_R_SUCCESS;
1253 if (addtuple != NULL)
1254 dns_difftuple_free(&addtuple);
1255 if (deltuple != NULL)
1256 dns_difftuple_free(&deltuple);
1261 * Check that the new SOA record at 'update_rdata' does not
1262 * illegally cause the SOA serial number to decrease or stay
1263 * unchanged relative to the existing SOA in 'db'.
1265 * Sets '*ok' to ISC_TRUE if the update is legal, ISC_FALSE if not.
1267 * William King points out that RFC2136 is inconsistent about
1268 * the case where the serial number stays unchanged:
1270 * section 3.4.2.2 requires a server to ignore a SOA update request
1271 * if the serial number on the update SOA is less_than_or_equal to
1272 * the zone SOA serial.
1274 * section 3.6 requires a server to ignore a SOA update request if
1275 * the serial is less_than the zone SOA serial.
1277 * Paul says 3.4.2.2 is correct.
1281 check_soa_increment(dns_db_t *db, dns_dbversion_t *ver,
1282 dns_rdata_t *update_rdata,
1285 isc_uint32_t db_serial;
1286 isc_uint32_t update_serial;
1287 isc_result_t result;
1289 update_serial = dns_soa_getserial(update_rdata);
1291 result = dns_db_getsoaserial(db, ver, &db_serial);
1292 if (result != ISC_R_SUCCESS)
1295 if (DNS_SERIAL_GE(db_serial, update_serial)) {
1301 return (ISC_R_SUCCESS);
1305 /**************************************************************************/
1307 * Incremental updating of NSECs and RRSIGs.
1310 #define MAXZONEKEYS 32 /*%< Maximum number of zone keys supported. */
1313 * We abuse the dns_diff_t type to represent a set of domain names
1314 * affected by the update.
1317 namelist_append_name(dns_diff_t *list, dns_name_t *name) {
1318 isc_result_t result;
1319 dns_difftuple_t *tuple = NULL;
1320 static dns_rdata_t dummy_rdata = DNS_RDATA_INIT;
1322 CHECK(dns_difftuple_create(list->mctx, DNS_DIFFOP_EXISTS, name, 0,
1323 &dummy_rdata, &tuple));
1324 dns_diff_append(list, &tuple);
1330 namelist_append_subdomain(dns_db_t *db, dns_name_t *name, dns_diff_t *affected)
1332 isc_result_t result;
1333 dns_fixedname_t fixedname;
1335 dns_dbiterator_t *dbit = NULL;
1337 dns_fixedname_init(&fixedname);
1338 child = dns_fixedname_name(&fixedname);
1340 CHECK(dns_db_createiterator(db, ISC_FALSE, &dbit));
1342 for (result = dns_dbiterator_seek(dbit, name);
1343 result == ISC_R_SUCCESS;
1344 result = dns_dbiterator_next(dbit))
1346 dns_dbnode_t *node = NULL;
1347 CHECK(dns_dbiterator_current(dbit, &node, child));
1348 dns_db_detachnode(db, &node);
1349 if (! dns_name_issubdomain(child, name))
1351 CHECK(namelist_append_name(affected, child));
1353 if (result == ISC_R_NOMORE)
1354 result = ISC_R_SUCCESS;
1357 dns_dbiterator_destroy(&dbit);
1364 * Helper function for non_nsec_rrset_exists().
1367 is_non_nsec_action(void *data, dns_rdataset_t *rrset) {
1369 if (!(rrset->type == dns_rdatatype_nsec ||
1370 (rrset->type == dns_rdatatype_rrsig &&
1371 rrset->covers == dns_rdatatype_nsec)))
1372 return (ISC_R_EXISTS);
1373 return (ISC_R_SUCCESS);
1377 * Check whether there is an rrset other than a NSEC or RRSIG NSEC,
1378 * i.e., anything that justifies the continued existence of a name
1379 * after a secure update.
1381 * If such an rrset exists, set '*exists' to ISC_TRUE.
1382 * Otherwise, set it to ISC_FALSE.
1385 non_nsec_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
1386 dns_name_t *name, isc_boolean_t *exists)
1388 isc_result_t result;
1389 result = foreach_rrset(db, ver, name,
1390 is_non_nsec_action, NULL);
1391 RETURN_EXISTENCE_FLAG;
1395 * A comparison function for sorting dns_diff_t:s by name.
1398 name_order(const void *av, const void *bv) {
1399 dns_difftuple_t const * const *ap = av;
1400 dns_difftuple_t const * const *bp = bv;
1401 dns_difftuple_t const *a = *ap;
1402 dns_difftuple_t const *b = *bp;
1403 return (dns_name_compare(&a->name, &b->name));
1407 uniqify_name_list(dns_diff_t *list) {
1408 isc_result_t result;
1409 dns_difftuple_t *p, *q;
1411 CHECK(dns_diff_sort(list, name_order));
1413 p = ISC_LIST_HEAD(list->tuples);
1416 q = ISC_LIST_NEXT(p, link);
1417 if (q == NULL || ! dns_name_equal(&p->name, &q->name))
1419 ISC_LIST_UNLINK(list->tuples, q, link);
1420 dns_difftuple_free(&q);
1422 p = ISC_LIST_NEXT(p, link);
1430 is_glue(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1431 isc_boolean_t *flag)
1433 isc_result_t result;
1434 dns_fixedname_t foundname;
1435 dns_fixedname_init(&foundname);
1436 result = dns_db_find(db, name, ver, dns_rdatatype_any,
1437 DNS_DBFIND_GLUEOK | DNS_DBFIND_NOWILD,
1438 (isc_stdtime_t) 0, NULL,
1439 dns_fixedname_name(&foundname),
1441 if (result == ISC_R_SUCCESS) {
1443 return (ISC_R_SUCCESS);
1444 } else if (result == DNS_R_ZONECUT) {
1446 * We are at the zonecut. The name will have an NSEC, but
1447 * non-delegation will be omitted from the type bit map.
1450 return (ISC_R_SUCCESS);
1451 } else if (result == DNS_R_GLUE || result == DNS_R_DNAME) {
1453 return (ISC_R_SUCCESS);
1460 * Find the next/previous name that has a NSEC record.
1461 * In other words, skip empty database nodes and names that
1462 * have had their NSECs removed because they are obscured by
1466 next_active(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1467 dns_dbversion_t *ver, dns_name_t *oldname, dns_name_t *newname,
1468 isc_boolean_t forward)
1470 isc_result_t result;
1471 dns_dbiterator_t *dbit = NULL;
1472 isc_boolean_t has_nsec;
1473 unsigned int wraps = 0;
1475 CHECK(dns_db_createiterator(db, ISC_FALSE, &dbit));
1477 CHECK(dns_dbiterator_seek(dbit, oldname));
1479 dns_dbnode_t *node = NULL;
1482 result = dns_dbiterator_next(dbit);
1484 result = dns_dbiterator_prev(dbit);
1485 if (result == ISC_R_NOMORE) {
1490 CHECK(dns_dbiterator_first(dbit));
1492 CHECK(dns_dbiterator_last(dbit));
1495 update_log(client, zone, ISC_LOG_ERROR,
1496 "secure zone with no NSECs");
1497 result = DNS_R_BADZONE;
1501 CHECK(dns_dbiterator_current(dbit, &node, newname));
1502 dns_db_detachnode(db, &node);
1505 * The iterator may hold the tree lock, and
1506 * rrset_exists() calls dns_db_findnode() which
1507 * may try to reacquire it. To avoid deadlock
1508 * we must pause the iterator first.
1510 CHECK(dns_dbiterator_pause(dbit));
1511 CHECK(rrset_exists(db, ver, newname,
1512 dns_rdatatype_nsec, 0, &has_nsec));
1514 } while (! has_nsec);
1517 dns_dbiterator_destroy(&dbit);
1523 * Add a NSEC record for "name", recording the change in "diff".
1524 * The existing NSEC is removed.
1527 add_nsec(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1528 dns_dbversion_t *ver, dns_name_t *name, dns_ttl_t nsecttl,
1531 isc_result_t result;
1532 dns_dbnode_t *node = NULL;
1533 unsigned char buffer[DNS_NSEC_BUFFERSIZE];
1534 dns_rdata_t rdata = DNS_RDATA_INIT;
1535 dns_difftuple_t *tuple = NULL;
1536 dns_fixedname_t fixedname;
1539 dns_fixedname_init(&fixedname);
1540 target = dns_fixedname_name(&fixedname);
1543 * Find the successor name, aka NSEC target.
1545 CHECK(next_active(client, zone, db, ver, name, target, ISC_TRUE));
1548 * Create the NSEC RDATA.
1550 CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
1551 dns_rdata_init(&rdata);
1552 CHECK(dns_nsec_buildrdata(db, ver, node, target, buffer, &rdata));
1553 dns_db_detachnode(db, &node);
1556 * Delete the old NSEC and record the change.
1558 CHECK(delete_if(true_p, db, ver, name, dns_rdatatype_nsec, 0,
1561 * Add the new NSEC and record the change.
1563 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name,
1564 nsecttl, &rdata, &tuple));
1565 CHECK(do_one_tuple(&tuple, db, ver, diff));
1566 INSIST(tuple == NULL);
1570 dns_db_detachnode(db, &node);
1575 * Add a placeholder NSEC record for "name", recording the change in "diff".
1578 add_placeholder_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1580 isc_result_t result;
1581 dns_difftuple_t *tuple = NULL;
1583 unsigned char data[1] = { 0 }; /* The root domain, no bits. */
1584 dns_rdata_t rdata = DNS_RDATA_INIT;
1587 r.length = sizeof(data);
1588 dns_rdata_fromregion(&rdata, dns_db_class(db), dns_rdatatype_nsec, &r);
1589 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, 0,
1591 CHECK(do_one_tuple(&tuple, db, ver, diff));
1597 find_zone_keys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
1598 isc_mem_t *mctx, unsigned int maxkeys,
1599 dst_key_t **keys, unsigned int *nkeys)
1601 isc_result_t result;
1602 dns_dbnode_t *node = NULL;
1603 const char *directory = dns_zone_getkeydirectory(zone);
1604 CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
1605 CHECK(dns_dnssec_findzonekeys2(db, ver, node, dns_db_origin(db),
1606 directory, mctx, maxkeys, keys, nkeys));
1609 dns_db_detachnode(db, &node);
1613 static isc_boolean_t
1614 ksk_sanity(dns_db_t *db, dns_dbversion_t *ver) {
1615 isc_boolean_t ret = ISC_FALSE;
1616 isc_boolean_t have_ksk = ISC_FALSE, have_nonksk = ISC_FALSE;
1617 isc_result_t result;
1618 dns_dbnode_t *node = NULL;
1619 dns_rdataset_t rdataset;
1620 dns_rdata_t rdata = DNS_RDATA_INIT;
1621 dns_rdata_dnskey_t dnskey;
1623 dns_rdataset_init(&rdataset);
1624 CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
1625 CHECK(dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey, 0, 0,
1627 CHECK(dns_rdataset_first(&rdataset));
1628 while (result == ISC_R_SUCCESS && (!have_ksk || !have_nonksk)) {
1629 dns_rdataset_current(&rdataset, &rdata);
1630 CHECK(dns_rdata_tostruct(&rdata, &dnskey, NULL));
1631 if ((dnskey.flags & (DNS_KEYFLAG_OWNERMASK|DNS_KEYTYPE_NOAUTH))
1632 == DNS_KEYOWNER_ZONE) {
1633 if ((dnskey.flags & DNS_KEYFLAG_KSK) != 0)
1634 have_ksk = ISC_TRUE;
1636 have_nonksk = ISC_TRUE;
1638 dns_rdata_reset(&rdata);
1639 result = dns_rdataset_next(&rdataset);
1641 if (have_ksk && have_nonksk)
1644 if (dns_rdataset_isassociated(&rdataset))
1645 dns_rdataset_disassociate(&rdataset);
1647 dns_db_detachnode(db, &node);
1652 * Add RRSIG records for an RRset, recording the change in "diff".
1655 add_sigs(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1656 dns_dbversion_t *ver, dns_name_t *name, dns_rdatatype_t type,
1657 dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys,
1658 isc_mem_t *mctx, isc_stdtime_t inception, isc_stdtime_t expire,
1659 isc_boolean_t check_ksk)
1661 isc_result_t result;
1662 dns_dbnode_t *node = NULL;
1663 dns_rdataset_t rdataset;
1664 dns_rdata_t sig_rdata = DNS_RDATA_INIT;
1665 isc_buffer_t buffer;
1666 unsigned char data[1024]; /* XXX */
1668 isc_boolean_t added_sig = ISC_FALSE;
1670 dns_rdataset_init(&rdataset);
1671 isc_buffer_init(&buffer, data, sizeof(data));
1673 /* Get the rdataset to sign. */
1674 CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
1675 CHECK(dns_db_findrdataset(db, node, ver, type, 0,
1678 dns_db_detachnode(db, &node);
1680 for (i = 0; i < nkeys; i++) {
1682 if (check_ksk && type != dns_rdatatype_dnskey &&
1683 (dst_key_flags(keys[i]) & DNS_KEYFLAG_KSK) != 0)
1686 if (!dst_key_isprivate(keys[i]))
1689 /* Calculate the signature, creating a RRSIG RDATA. */
1690 CHECK(dns_dnssec_sign(name, &rdataset, keys[i],
1691 &inception, &expire,
1692 mctx, &buffer, &sig_rdata));
1694 /* Update the database and journal with the RRSIG. */
1695 /* XXX inefficient - will cause dataset merging */
1696 CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADD, name,
1697 rdataset.ttl, &sig_rdata));
1698 dns_rdata_reset(&sig_rdata);
1699 added_sig = ISC_TRUE;
1702 update_log(client, zone, ISC_LOG_ERROR,
1703 "found no private keys, "
1704 "unable to generate any signatures");
1705 result = ISC_R_NOTFOUND;
1709 if (dns_rdataset_isassociated(&rdataset))
1710 dns_rdataset_disassociate(&rdataset);
1712 dns_db_detachnode(db, &node);
1717 * Update RRSIG and NSEC records affected by an update. The original
1718 * update, including the SOA serial update but exluding the RRSIG & NSEC
1719 * changes, is in "diff" and has already been applied to "newver" of "db".
1720 * The database version prior to the update is "oldver".
1722 * The necessary RRSIG and NSEC changes will be applied to "newver"
1723 * and added (as a minimal diff) to "diff".
1725 * The RRSIGs generated will be valid for 'sigvalidityinterval' seconds.
1728 update_signatures(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1729 dns_dbversion_t *oldver, dns_dbversion_t *newver,
1730 dns_diff_t *diff, isc_uint32_t sigvalidityinterval)
1732 isc_result_t result;
1734 dns_diff_t diffnames;
1735 dns_diff_t affected;
1736 dns_diff_t sig_diff;
1737 dns_diff_t nsec_diff;
1738 dns_diff_t nsec_mindiff;
1740 dst_key_t *zone_keys[MAXZONEKEYS];
1741 unsigned int nkeys = 0;
1743 isc_stdtime_t now, inception, expire;
1745 dns_rdata_soa_t soa;
1746 dns_rdata_t rdata = DNS_RDATA_INIT;
1747 dns_rdataset_t rdataset;
1748 dns_dbnode_t *node = NULL;
1749 isc_boolean_t check_ksk;
1751 dns_diff_init(client->mctx, &diffnames);
1752 dns_diff_init(client->mctx, &affected);
1754 dns_diff_init(client->mctx, &sig_diff);
1755 dns_diff_init(client->mctx, &nsec_diff);
1756 dns_diff_init(client->mctx, &nsec_mindiff);
1758 result = find_zone_keys(zone, db, newver, client->mctx,
1759 MAXZONEKEYS, zone_keys, &nkeys);
1760 if (result != ISC_R_SUCCESS) {
1761 update_log(client, zone, ISC_LOG_ERROR,
1762 "could not get zone keys for secure dynamic update");
1766 isc_stdtime_get(&now);
1767 inception = now - 3600; /* Allow for some clock skew. */
1768 expire = now + sigvalidityinterval;
1771 * Do we look at the KSK flag on the DNSKEY to determining which
1772 * keys sign which RRsets? First check the zone option then
1773 * check the keys flags to make sure atleast one has a ksk set
1776 check_ksk = ISC_TF((dns_zone_getoptions(zone) &
1777 DNS_ZONEOPT_UPDATECHECKKSK) != 0);
1779 check_ksk = ksk_sanity(db, newver);
1782 * Get the NSEC's TTL from the SOA MINIMUM field.
1784 CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
1785 dns_rdataset_init(&rdataset);
1786 CHECK(dns_db_findrdataset(db, node, newver, dns_rdatatype_soa, 0,
1787 (isc_stdtime_t) 0, &rdataset, NULL));
1788 CHECK(dns_rdataset_first(&rdataset));
1789 dns_rdataset_current(&rdataset, &rdata);
1790 CHECK(dns_rdata_tostruct(&rdata, &soa, NULL));
1791 nsecttl = soa.minimum;
1792 dns_rdataset_disassociate(&rdataset);
1793 dns_db_detachnode(db, &node);
1796 * Find all RRsets directly affected by the update, and
1797 * update their RRSIGs. Also build a list of names affected
1798 * by the update in "diffnames".
1800 CHECK(dns_diff_sort(diff, temp_order));
1802 t = ISC_LIST_HEAD(diff->tuples);
1804 dns_name_t *name = &t->name;
1805 /* Now "name" is a new, unique name affected by the update. */
1807 CHECK(namelist_append_name(&diffnames, name));
1809 while (t != NULL && dns_name_equal(&t->name, name)) {
1810 dns_rdatatype_t type;
1811 type = t->rdata.type;
1814 * Now "name" and "type" denote a new unique RRset
1815 * affected by the update.
1818 /* Don't sign RRSIGs. */
1819 if (type == dns_rdatatype_rrsig)
1823 * Delete all old RRSIGs covering this type, since they
1824 * are all invalid when the signed RRset has changed.
1825 * We may not be able to recreate all of them - tough.
1827 CHECK(delete_if(true_p, db, newver, name,
1828 dns_rdatatype_rrsig, type,
1832 * If this RRset still exists after the update,
1833 * add a new signature for it.
1835 CHECK(rrset_exists(db, newver, name, type, 0, &flag));
1837 CHECK(add_sigs(client, zone, db, newver, name,
1838 type, &sig_diff, zone_keys,
1839 nkeys, client->mctx, inception,
1840 expire, check_ksk));
1843 /* Skip any other updates to the same RRset. */
1845 dns_name_equal(&t->name, name) &&
1846 t->rdata.type == type)
1848 t = ISC_LIST_NEXT(t, link);
1853 /* Remove orphaned NSECs and RRSIG NSECs. */
1854 for (t = ISC_LIST_HEAD(diffnames.tuples);
1856 t = ISC_LIST_NEXT(t, link))
1858 CHECK(non_nsec_rrset_exists(db, newver, &t->name, &flag));
1860 CHECK(delete_if(true_p, db, newver, &t->name,
1861 dns_rdatatype_any, 0,
1867 * When a name is created or deleted, its predecessor needs to
1868 * have its NSEC updated.
1870 for (t = ISC_LIST_HEAD(diffnames.tuples);
1872 t = ISC_LIST_NEXT(t, link))
1874 isc_boolean_t existed, exists;
1875 dns_fixedname_t fixedname;
1876 dns_name_t *prevname;
1878 dns_fixedname_init(&fixedname);
1879 prevname = dns_fixedname_name(&fixedname);
1881 CHECK(name_exists(db, oldver, &t->name, &existed));
1882 CHECK(name_exists(db, newver, &t->name, &exists));
1883 if (exists == existed)
1887 * Find the predecessor.
1888 * When names become obscured or unobscured in this update
1889 * transaction, we may find the wrong predecessor because
1890 * the NSECs have not yet been updated to reflect the delegation
1891 * change. This should not matter because in this case,
1892 * the correct predecessor is either the delegation node or
1893 * a newly unobscured node, and those nodes are on the
1894 * "affected" list in any case.
1896 CHECK(next_active(client, zone, db, newver,
1897 &t->name, prevname, ISC_FALSE));
1898 CHECK(namelist_append_name(&affected, prevname));
1902 * Find names potentially affected by delegation changes
1903 * (obscured by adding an NS or DNAME, or unobscured by
1906 for (t = ISC_LIST_HEAD(diffnames.tuples);
1908 t = ISC_LIST_NEXT(t, link))
1910 isc_boolean_t ns_existed, dname_existed;
1911 isc_boolean_t ns_exists, dname_exists;
1913 CHECK(rrset_exists(db, oldver, &t->name, dns_rdatatype_ns, 0,
1915 CHECK(rrset_exists(db, oldver, &t->name, dns_rdatatype_dname, 0,
1917 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ns, 0,
1919 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_dname, 0,
1921 if ((ns_exists || dname_exists) == (ns_existed || dname_existed))
1924 * There was a delegation change. Mark all subdomains
1925 * of t->name as potentially needing a NSEC update.
1927 CHECK(namelist_append_subdomain(db, &t->name, &affected));
1930 ISC_LIST_APPENDLIST(affected.tuples, diffnames.tuples, link);
1931 INSIST(ISC_LIST_EMPTY(diffnames.tuples));
1933 CHECK(uniqify_name_list(&affected));
1936 * Determine which names should have NSECs, and delete/create
1937 * NSECs to make it so. We don't know the final NSEC targets yet,
1938 * so we just create placeholder NSECs with arbitrary contents
1939 * to indicate that their respective owner names should be part of
1942 for (t = ISC_LIST_HEAD(affected.tuples);
1944 t = ISC_LIST_NEXT(t, link))
1946 isc_boolean_t exists;
1947 CHECK(name_exists(db, newver, &t->name, &exists));
1950 CHECK(is_glue(db, newver, &t->name, &flag));
1953 * This name is obscured. Delete any
1954 * existing NSEC record.
1956 CHECK(delete_if(true_p, db, newver, &t->name,
1957 dns_rdatatype_nsec, 0,
1961 * This name is not obscured. It should have a NSEC.
1963 CHECK(rrset_exists(db, newver, &t->name,
1964 dns_rdatatype_nsec, 0, &flag));
1966 CHECK(add_placeholder_nsec(db, newver, &t->name,
1972 * Now we know which names are part of the NSEC chain.
1973 * Make them all point at their correct targets.
1975 for (t = ISC_LIST_HEAD(affected.tuples);
1977 t = ISC_LIST_NEXT(t, link))
1979 CHECK(rrset_exists(db, newver, &t->name,
1980 dns_rdatatype_nsec, 0, &flag));
1983 * There is a NSEC, but we don't know if it is correct.
1984 * Delete it and create a correct one to be sure.
1985 * If the update was unnecessary, the diff minimization
1986 * will take care of eliminating it from the journal,
1989 * The RRSIG bit should always be set in the NSECs
1990 * we generate, because they will all get RRSIG NSECs.
1991 * (XXX what if the zone keys are missing?).
1992 * Because the RRSIG NSECs have not necessarily been
1993 * created yet, the correctness of the bit mask relies
1994 * on the assumption that NSECs are only created if
1995 * there is other data, and if there is other data,
1996 * there are other RRSIGs.
1998 CHECK(add_nsec(client, zone, db, newver, &t->name,
1999 nsecttl, &nsec_diff));
2004 * Minimize the set of NSEC updates so that we don't
2005 * have to regenerate the RRSIG NSECs for NSECs that were
2006 * replaced with identical ones.
2008 while ((t = ISC_LIST_HEAD(nsec_diff.tuples)) != NULL) {
2009 ISC_LIST_UNLINK(nsec_diff.tuples, t, link);
2010 dns_diff_appendminimal(&nsec_mindiff, &t);
2013 /* Update RRSIG NSECs. */
2014 for (t = ISC_LIST_HEAD(nsec_mindiff.tuples);
2016 t = ISC_LIST_NEXT(t, link))
2018 if (t->op == DNS_DIFFOP_DEL) {
2019 CHECK(delete_if(true_p, db, newver, &t->name,
2020 dns_rdatatype_rrsig, dns_rdatatype_nsec,
2022 } else if (t->op == DNS_DIFFOP_ADD) {
2023 CHECK(add_sigs(client, zone, db, newver, &t->name,
2024 dns_rdatatype_nsec, &sig_diff,
2025 zone_keys, nkeys, client->mctx,
2026 inception, expire, check_ksk));
2032 /* Record our changes for the journal. */
2033 while ((t = ISC_LIST_HEAD(sig_diff.tuples)) != NULL) {
2034 ISC_LIST_UNLINK(sig_diff.tuples, t, link);
2035 dns_diff_appendminimal(diff, &t);
2037 while ((t = ISC_LIST_HEAD(nsec_mindiff.tuples)) != NULL) {
2038 ISC_LIST_UNLINK(nsec_mindiff.tuples, t, link);
2039 dns_diff_appendminimal(diff, &t);
2042 INSIST(ISC_LIST_EMPTY(sig_diff.tuples));
2043 INSIST(ISC_LIST_EMPTY(nsec_diff.tuples));
2044 INSIST(ISC_LIST_EMPTY(nsec_mindiff.tuples));
2047 dns_diff_clear(&sig_diff);
2048 dns_diff_clear(&nsec_diff);
2049 dns_diff_clear(&nsec_mindiff);
2051 dns_diff_clear(&affected);
2052 dns_diff_clear(&diffnames);
2054 for (i = 0; i < nkeys; i++)
2055 dst_key_free(&zone_keys[i]);
2061 /**************************************************************************/
2063 * The actual update code in all its glory. We try to follow
2064 * the RFC2136 pseudocode as closely as possible.
2068 send_update_event(ns_client_t *client, dns_zone_t *zone) {
2069 isc_result_t result = ISC_R_SUCCESS;
2070 update_event_t *event = NULL;
2071 isc_task_t *zonetask = NULL;
2072 ns_client_t *evclient;
2074 event = (update_event_t *)
2075 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
2076 update_action, NULL, sizeof(*event));
2078 FAIL(ISC_R_NOMEMORY);
2080 event->result = ISC_R_SUCCESS;
2083 ns_client_attach(client, &evclient);
2084 INSIST(client->nupdates == 0);
2086 event->ev_arg = evclient;
2088 dns_zone_gettask(zone, &zonetask);
2089 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
2093 isc_event_free(ISC_EVENT_PTR(&event));
2098 respond(ns_client_t *client, isc_result_t result) {
2099 isc_result_t msg_result;
2101 msg_result = dns_message_reply(client->message, ISC_TRUE);
2102 if (msg_result != ISC_R_SUCCESS)
2104 client->message->rcode = dns_result_torcode(result);
2106 ns_client_send(client);
2110 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
2112 "could not create update response message: %s",
2113 isc_result_totext(msg_result));
2114 ns_client_next(client, msg_result);
2118 ns_update_start(ns_client_t *client, isc_result_t sigresult) {
2119 dns_message_t *request = client->message;
2120 isc_result_t result;
2121 dns_name_t *zonename;
2122 dns_rdataset_t *zone_rdataset;
2123 dns_zone_t *zone = NULL;
2126 * Interpret the zone section.
2128 result = dns_message_firstname(request, DNS_SECTION_ZONE);
2129 if (result != ISC_R_SUCCESS)
2130 FAILC(DNS_R_FORMERR,
2131 "update zone section empty");
2134 * The zone section must contain exactly one "question", and
2135 * it must be of type SOA.
2138 dns_message_currentname(request, DNS_SECTION_ZONE, &zonename);
2139 zone_rdataset = ISC_LIST_HEAD(zonename->list);
2140 if (zone_rdataset->type != dns_rdatatype_soa)
2141 FAILC(DNS_R_FORMERR,
2142 "update zone section contains non-SOA");
2143 if (ISC_LIST_NEXT(zone_rdataset, link) != NULL)
2144 FAILC(DNS_R_FORMERR,
2145 "update zone section contains multiple RRs");
2147 /* The zone section must have exactly one name. */
2148 result = dns_message_nextname(request, DNS_SECTION_ZONE);
2149 if (result != ISC_R_NOMORE)
2150 FAILC(DNS_R_FORMERR,
2151 "update zone section contains multiple RRs");
2153 result = dns_zt_find(client->view->zonetable, zonename, 0, NULL,
2155 if (result != ISC_R_SUCCESS)
2156 FAILC(DNS_R_NOTAUTH,
2157 "not authoritative for update zone");
2159 switch(dns_zone_gettype(zone)) {
2160 case dns_zone_master:
2162 * We can now fail due to a bad signature as we now know
2163 * that we are the master.
2165 if (sigresult != ISC_R_SUCCESS)
2167 CHECK(send_update_event(client, zone));
2169 case dns_zone_slave:
2170 CHECK(checkupdateacl(client, dns_zone_getforwardacl(zone),
2171 "update forwarding", zonename, ISC_TRUE));
2172 CHECK(send_forward_event(client, zone));
2175 FAILC(DNS_R_NOTAUTH,
2176 "not authoritative for update zone");
2182 * We failed without having sent an update event to the zone.
2183 * We are still in the client task context, so we can
2184 * simply give an error response without switching tasks.
2186 respond(client, result);
2188 dns_zone_detach(&zone);
2192 * DS records are not allowed to exist without corresponding NS records,
2193 * draft-ietf-dnsext-delegation-signer-11.txt, 2.2 Protocol Change,
2194 * "DS RRsets MUST NOT appear at non-delegation points or at a zone's apex".
2198 remove_orphaned_ds(dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff) {
2199 isc_result_t result;
2200 isc_boolean_t ns_exists, ds_exists;
2203 for (t = ISC_LIST_HEAD(diff->tuples);
2205 t = ISC_LIST_NEXT(t, link)) {
2206 if (t->op != DNS_DIFFOP_ADD ||
2207 t->rdata.type != dns_rdatatype_ns)
2209 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ns, 0,
2213 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ds, 0,
2217 CHECK(delete_if(true_p, db, newver, &t->name,
2218 dns_rdatatype_ds, 0, NULL, diff));
2220 return (ISC_R_SUCCESS);
2227 * This implements the post load integrity checks for mx records.
2230 check_mx(ns_client_t *client, dns_zone_t *zone,
2231 dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff)
2233 char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123.")];
2234 char ownerbuf[DNS_NAME_FORMATSIZE];
2235 char namebuf[DNS_NAME_FORMATSIZE];
2236 char altbuf[DNS_NAME_FORMATSIZE];
2238 dns_fixedname_t fixed;
2239 dns_name_t *foundname;
2242 isc_boolean_t ok = ISC_TRUE;
2243 isc_boolean_t isaddress;
2244 isc_result_t result;
2245 struct in6_addr addr6;
2246 struct in_addr addr;
2247 unsigned int options;
2249 dns_fixedname_init(&fixed);
2250 foundname = dns_fixedname_name(&fixed);
2251 dns_rdata_init(&rdata);
2252 options = dns_zone_getoptions(zone);
2254 for (t = ISC_LIST_HEAD(diff->tuples);
2256 t = ISC_LIST_NEXT(t, link)) {
2257 if (t->op != DNS_DIFFOP_ADD ||
2258 t->rdata.type != dns_rdatatype_mx)
2261 result = dns_rdata_tostruct(&t->rdata, &mx, NULL);
2262 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2264 * Check if we will error out if we attempt to reload the
2267 dns_name_format(&mx.mx, namebuf, sizeof(namebuf));
2268 dns_name_format(&t->name, ownerbuf, sizeof(ownerbuf));
2269 isaddress = ISC_FALSE;
2270 if ((options & DNS_RDATA_CHECKMX) != 0 &&
2271 strlcpy(tmp, namebuf, sizeof(tmp)) < sizeof(tmp)) {
2272 if (tmp[strlen(tmp) - 1] == '.')
2273 tmp[strlen(tmp) - 1] = '\0';
2274 if (inet_aton(tmp, &addr) == 1 ||
2275 inet_pton(AF_INET6, tmp, &addr6) == 1)
2276 isaddress = ISC_TRUE;
2279 if (isaddress && (options & DNS_RDATA_CHECKMXFAIL) != 0) {
2280 update_log(client, zone, ISC_LOG_ERROR,
2283 dns_result_totext(DNS_R_MXISADDRESS));
2285 } else if (isaddress) {
2286 update_log(client, zone, ISC_LOG_WARNING,
2287 "%s/MX: warning: '%s': %s",
2289 dns_result_totext(DNS_R_MXISADDRESS));
2293 * Check zone integrity checks.
2295 if ((options & DNS_ZONEOPT_CHECKINTEGRITY) == 0)
2297 result = dns_db_find(db, &mx.mx, newver, dns_rdatatype_a,
2298 0, 0, NULL, foundname, NULL, NULL);
2299 if (result == ISC_R_SUCCESS)
2302 if (result == DNS_R_NXRRSET) {
2303 result = dns_db_find(db, &mx.mx, newver,
2305 0, 0, NULL, foundname,
2307 if (result == ISC_R_SUCCESS)
2311 if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN) {
2312 update_log(client, zone, ISC_LOG_ERROR,
2313 "%s/MX '%s' has no address records "
2314 "(A or AAAA)", ownerbuf, namebuf);
2316 } else if (result == DNS_R_CNAME) {
2317 update_log(client, zone, ISC_LOG_ERROR,
2318 "%s/MX '%s' is a CNAME (illegal)",
2321 } else if (result == DNS_R_DNAME) {
2322 dns_name_format(foundname, altbuf, sizeof altbuf);
2323 update_log(client, zone, ISC_LOG_ERROR,
2324 "%s/MX '%s' is below a DNAME '%s' (illegal)",
2325 ownerbuf, namebuf, altbuf);
2329 return (ok ? ISC_R_SUCCESS : DNS_R_REFUSED);
2333 update_action(isc_task_t *task, isc_event_t *event) {
2334 update_event_t *uev = (update_event_t *) event;
2335 dns_zone_t *zone = uev->zone;
2336 ns_client_t *client = (ns_client_t *)event->ev_arg;
2338 isc_result_t result;
2339 dns_db_t *db = NULL;
2340 dns_dbversion_t *oldver = NULL;
2341 dns_dbversion_t *ver = NULL;
2342 dns_diff_t diff; /* Pending updates. */
2343 dns_diff_t temp; /* Pending RR existence assertions. */
2344 isc_boolean_t soa_serial_changed = ISC_FALSE;
2345 isc_mem_t *mctx = client->mctx;
2346 dns_rdatatype_t covers;
2347 dns_message_t *request = client->message;
2348 dns_rdataclass_t zoneclass;
2349 dns_name_t *zonename;
2350 dns_ssutable_t *ssutable = NULL;
2351 dns_fixedname_t tmpnamefixed;
2352 dns_name_t *tmpname = NULL;
2353 unsigned int options;
2355 INSIST(event->ev_type == DNS_EVENT_UPDATE);
2357 dns_diff_init(mctx, &diff);
2358 dns_diff_init(mctx, &temp);
2360 CHECK(dns_zone_getdb(zone, &db));
2361 zonename = dns_db_origin(db);
2362 zoneclass = dns_db_class(db);
2363 dns_zone_getssutable(zone, &ssutable);
2364 dns_db_currentversion(db, &oldver);
2365 CHECK(dns_db_newversion(db, &ver));
2368 * Check prerequisites.
2371 for (result = dns_message_firstname(request, DNS_SECTION_PREREQUISITE);
2372 result == ISC_R_SUCCESS;
2373 result = dns_message_nextname(request, DNS_SECTION_PREREQUISITE))
2375 dns_name_t *name = NULL;
2376 dns_rdata_t rdata = DNS_RDATA_INIT;
2378 dns_rdataclass_t update_class;
2381 get_current_rr(request, DNS_SECTION_PREREQUISITE, zoneclass,
2382 &name, &rdata, &covers, &ttl, &update_class);
2385 FAILC(DNS_R_FORMERR, "prerequisite TTL is not zero");
2387 if (! dns_name_issubdomain(name, zonename))
2388 FAILN(DNS_R_NOTZONE, name,
2389 "prerequisite name is out of zone");
2391 if (update_class == dns_rdataclass_any) {
2392 if (rdata.length != 0)
2393 FAILC(DNS_R_FORMERR,
2394 "class ANY prerequisite "
2395 "RDATA is not empty");
2396 if (rdata.type == dns_rdatatype_any) {
2397 CHECK(name_exists(db, ver, name, &flag));
2399 FAILN(DNS_R_NXDOMAIN, name,
2400 "'name in use' prerequisite "
2404 CHECK(rrset_exists(db, ver, name,
2405 rdata.type, covers, &flag));
2407 /* RRset does not exist. */
2408 FAILNT(DNS_R_NXRRSET, name, rdata.type,
2409 "'rrset exists (value independent)' "
2410 "prerequisite not satisfied");
2413 } else if (update_class == dns_rdataclass_none) {
2414 if (rdata.length != 0)
2415 FAILC(DNS_R_FORMERR,
2416 "class NONE prerequisite "
2417 "RDATA is not empty");
2418 if (rdata.type == dns_rdatatype_any) {
2419 CHECK(name_exists(db, ver, name, &flag));
2421 FAILN(DNS_R_YXDOMAIN, name,
2422 "'name not in use' prerequisite "
2426 CHECK(rrset_exists(db, ver, name,
2427 rdata.type, covers, &flag));
2430 FAILNT(DNS_R_YXRRSET, name, rdata.type,
2431 "'rrset does not exist' "
2432 "prerequisite not satisfied");
2435 } else if (update_class == zoneclass) {
2436 /* "temp<rr.name, rr.type> += rr;" */
2437 result = temp_append(&temp, name, &rdata);
2438 if (result != ISC_R_SUCCESS) {
2439 UNEXPECTED_ERROR(__FILE__, __LINE__,
2440 "temp entry creation failed: %s",
2441 dns_result_totext(result));
2442 FAIL(ISC_R_UNEXPECTED);
2445 FAILC(DNS_R_FORMERR, "malformed prerequisite");
2448 if (result != ISC_R_NOMORE)
2453 * Perform the final check of the "rrset exists (value dependent)"
2456 if (ISC_LIST_HEAD(temp.tuples) != NULL) {
2457 dns_rdatatype_t type;
2460 * Sort the prerequisite records by owner name,
2463 result = dns_diff_sort(&temp, temp_order);
2464 if (result != ISC_R_SUCCESS)
2465 FAILC(result, "'RRset exists (value dependent)' "
2466 "prerequisite not satisfied");
2468 dns_fixedname_init(&tmpnamefixed);
2469 tmpname = dns_fixedname_name(&tmpnamefixed);
2470 result = temp_check(mctx, &temp, db, ver, tmpname, &type);
2471 if (result != ISC_R_SUCCESS)
2472 FAILNT(result, tmpname, type,
2473 "'RRset exists (value dependent)' "
2474 "prerequisite not satisfied");
2477 update_log(client, zone, LOGLEVEL_DEBUG,
2478 "prerequisites are OK");
2481 * Check Requestor's Permissions. It seems a bit silly to do this
2482 * only after prerequisite testing, but that is what RFC2136 says.
2484 result = ISC_R_SUCCESS;
2485 if (ssutable == NULL)
2486 CHECK(checkupdateacl(client, dns_zone_getupdateacl(zone),
2487 "update", zonename, ISC_FALSE));
2488 else if (client->signer == NULL)
2489 CHECK(checkupdateacl(client, NULL, "update", zonename,
2492 if (dns_zone_getupdatedisabled(zone))
2493 FAILC(DNS_R_REFUSED, "dynamic update temporarily disabled");
2496 * Perform the Update Section Prescan.
2499 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2500 result == ISC_R_SUCCESS;
2501 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2503 dns_name_t *name = NULL;
2504 dns_rdata_t rdata = DNS_RDATA_INIT;
2506 dns_rdataclass_t update_class;
2507 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2508 &name, &rdata, &covers, &ttl, &update_class);
2510 if (! dns_name_issubdomain(name, zonename))
2511 FAILC(DNS_R_NOTZONE,
2512 "update RR is outside zone");
2513 if (update_class == zoneclass) {
2515 * Check for meta-RRs. The RFC2136 pseudocode says
2516 * check for ANY|AXFR|MAILA|MAILB, but the text adds
2517 * "or any other QUERY metatype"
2519 if (dns_rdatatype_ismeta(rdata.type)) {
2520 FAILC(DNS_R_FORMERR,
2521 "meta-RR in update");
2523 result = dns_zone_checknames(zone, name, &rdata);
2524 if (result != ISC_R_SUCCESS)
2525 FAIL(DNS_R_REFUSED);
2526 } else if (update_class == dns_rdataclass_any) {
2527 if (ttl != 0 || rdata.length != 0 ||
2528 (dns_rdatatype_ismeta(rdata.type) &&
2529 rdata.type != dns_rdatatype_any))
2530 FAILC(DNS_R_FORMERR,
2531 "meta-RR in update");
2532 } else if (update_class == dns_rdataclass_none) {
2534 dns_rdatatype_ismeta(rdata.type))
2535 FAILC(DNS_R_FORMERR,
2536 "meta-RR in update");
2538 update_log(client, zone, ISC_LOG_WARNING,
2539 "update RR has incorrect class %d",
2541 FAIL(DNS_R_FORMERR);
2544 * draft-ietf-dnsind-simple-secure-update-01 says
2545 * "Unlike traditional dynamic update, the client
2546 * is forbidden from updating NSEC records."
2548 if (dns_db_issecure(db)) {
2549 if (rdata.type == dns_rdatatype_nsec) {
2550 FAILC(DNS_R_REFUSED,
2551 "explicit NSEC updates are not allowed "
2554 else if (rdata.type == dns_rdatatype_rrsig) {
2555 FAILC(DNS_R_REFUSED,
2556 "explicit RRSIG updates are currently not "
2557 "supported in secure zones");
2561 if (ssutable != NULL && client->signer != NULL) {
2562 if (rdata.type != dns_rdatatype_any) {
2563 if (!dns_ssutable_checkrules(ssutable,
2566 FAILC(DNS_R_REFUSED,
2567 "rejected by secure update");
2570 if (!ssu_checkall(db, ver, name, ssutable,
2572 FAILC(DNS_R_REFUSED,
2573 "rejected by secure update");
2577 if (result != ISC_R_NOMORE)
2580 update_log(client, zone, LOGLEVEL_DEBUG,
2581 "update section prescan OK");
2584 * Process the Update Section.
2587 options = dns_zone_getoptions(zone);
2588 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2589 result == ISC_R_SUCCESS;
2590 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2592 dns_name_t *name = NULL;
2593 dns_rdata_t rdata = DNS_RDATA_INIT;
2595 dns_rdataclass_t update_class;
2598 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2599 &name, &rdata, &covers, &ttl, &update_class);
2601 if (update_class == zoneclass) {
2604 * RFC1123 doesn't allow MF and MD in master zones. */
2605 if (rdata.type == dns_rdatatype_md ||
2606 rdata.type == dns_rdatatype_mf) {
2607 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2609 dns_rdatatype_format(rdata.type, typebuf,
2611 update_log(client, zone, LOGLEVEL_PROTOCOL,
2612 "attempt to add %s ignored",
2616 if (rdata.type == dns_rdatatype_ns &&
2617 dns_name_iswildcard(name)) {
2618 update_log(client, zone,
2620 "attempt to add wildcard NS record"
2624 if (rdata.type == dns_rdatatype_cname) {
2625 CHECK(cname_incompatible_rrset_exists(db, ver,
2629 update_log(client, zone,
2631 "attempt to add CNAME "
2632 "alongside non-CNAME "
2637 CHECK(rrset_exists(db, ver, name,
2638 dns_rdatatype_cname, 0,
2641 ! dns_rdatatype_isdnssec(rdata.type))
2643 update_log(client, zone,
2645 "attempt to add non-CNAME "
2646 "alongside CNAME ignored");
2650 if (rdata.type == dns_rdatatype_soa) {
2652 CHECK(rrset_exists(db, ver, name,
2653 dns_rdatatype_soa, 0,
2656 update_log(client, zone,
2658 "attempt to create 2nd "
2662 CHECK(check_soa_increment(db, ver, &rdata,
2665 update_log(client, zone,
2667 "SOA update failed to "
2668 "increment serial, "
2672 soa_serial_changed = ISC_TRUE;
2674 if ((options & DNS_ZONEOPT_CHECKWILDCARD) != 0 &&
2675 dns_name_internalwildcard(name)) {
2676 char namestr[DNS_NAME_FORMATSIZE];
2677 dns_name_format(name, namestr,
2679 update_log(client, zone, LOGLEVEL_PROTOCOL,
2680 "warning: ownername '%s' contains "
2681 "a non-terminal wildcard", namestr);
2684 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) {
2685 char namestr[DNS_NAME_FORMATSIZE];
2686 char typestr[DNS_RDATATYPE_FORMATSIZE];
2687 dns_name_format(name, namestr,
2689 dns_rdatatype_format(rdata.type, typestr,
2691 update_log(client, zone,
2693 "adding an RR at '%s' %s",
2697 /* Prepare the affected RRset for the addition. */
2699 add_rr_prepare_ctx_t ctx;
2704 ctx.update_rr = &rdata;
2705 ctx.update_rr_ttl = ttl;
2706 ctx.ignore_add = ISC_FALSE;
2707 dns_diff_init(mctx, &ctx.del_diff);
2708 dns_diff_init(mctx, &ctx.add_diff);
2709 CHECK(foreach_rr(db, ver, name, rdata.type,
2710 covers, add_rr_prepare_action,
2713 if (ctx.ignore_add) {
2714 dns_diff_clear(&ctx.del_diff);
2715 dns_diff_clear(&ctx.add_diff);
2717 CHECK(do_diff(&ctx.del_diff, db, ver, &diff));
2718 CHECK(do_diff(&ctx.add_diff, db, ver, &diff));
2719 CHECK(update_one_rr(db, ver, &diff,
2721 name, ttl, &rdata));
2724 } else if (update_class == dns_rdataclass_any) {
2725 if (rdata.type == dns_rdatatype_any) {
2726 if (isc_log_wouldlog(ns_g_lctx,
2729 char namestr[DNS_NAME_FORMATSIZE];
2730 dns_name_format(name, namestr,
2732 update_log(client, zone,
2734 "delete all rrsets from "
2735 "name '%s'", namestr);
2737 if (dns_name_equal(name, zonename)) {
2738 CHECK(delete_if(type_not_soa_nor_ns_p,
2740 dns_rdatatype_any, 0,
2743 CHECK(delete_if(type_not_dnssec,
2745 dns_rdatatype_any, 0,
2748 } else if (dns_name_equal(name, zonename) &&
2749 (rdata.type == dns_rdatatype_soa ||
2750 rdata.type == dns_rdatatype_ns)) {
2751 update_log(client, zone,
2753 "attempt to delete all SOA "
2754 "or NS records ignored");
2757 if (isc_log_wouldlog(ns_g_lctx,
2760 char namestr[DNS_NAME_FORMATSIZE];
2761 char typestr[DNS_RDATATYPE_FORMATSIZE];
2762 dns_name_format(name, namestr,
2764 dns_rdatatype_format(rdata.type,
2767 update_log(client, zone,
2769 "deleting rrset at '%s' %s",
2772 CHECK(delete_if(true_p, db, ver, name,
2773 rdata.type, covers, &rdata,
2776 } else if (update_class == dns_rdataclass_none) {
2778 * The (name == zonename) condition appears in
2779 * RFC2136 3.4.2.4 but is missing from the pseudocode.
2781 if (dns_name_equal(name, zonename)) {
2782 if (rdata.type == dns_rdatatype_soa) {
2783 update_log(client, zone,
2785 "attempt to delete SOA "
2789 if (rdata.type == dns_rdatatype_ns) {
2791 CHECK(rr_count(db, ver, name,
2795 update_log(client, zone,
2804 update_log(client, zone,
2807 CHECK(delete_if(rr_equal_p, db, ver, name,
2808 rdata.type, covers, &rdata, &diff));
2811 if (result != ISC_R_NOMORE)
2815 * If any changes were made, increment the SOA serial number,
2816 * update RRSIGs and NSECs (if zone is secure), and write the update
2819 if (! ISC_LIST_EMPTY(diff.tuples)) {
2821 dns_journal_t *journal;
2824 * Increment the SOA serial, but only if it was not
2825 * changed as a result of an update operation.
2827 if (! soa_serial_changed) {
2828 CHECK(increment_soa_serial(db, ver, &diff, mctx));
2831 CHECK(check_mx(client, zone, db, ver, &diff));
2833 CHECK(remove_orphaned_ds(db, ver, &diff));
2835 if (dns_db_issecure(db)) {
2836 result = update_signatures(client, zone, db, oldver,
2838 dns_zone_getsigvalidityinterval(zone));
2839 if (result != ISC_R_SUCCESS) {
2840 update_log(client, zone,
2842 "RRSIG/NSEC update failed: %s",
2843 isc_result_totext(result));
2848 journalfile = dns_zone_getjournal(zone);
2849 if (journalfile != NULL) {
2850 update_log(client, zone, LOGLEVEL_DEBUG,
2851 "writing journal %s", journalfile);
2854 result = dns_journal_open(mctx, journalfile,
2855 ISC_TRUE, &journal);
2856 if (result != ISC_R_SUCCESS)
2857 FAILS(result, "journal open failed");
2859 result = dns_journal_write_transaction(journal, &diff);
2860 if (result != ISC_R_SUCCESS) {
2861 dns_journal_destroy(&journal);
2862 FAILS(result, "journal write failed");
2865 dns_journal_destroy(&journal);
2869 * XXXRTH Just a note that this committing code will have
2870 * to change to handle databases that need two-phase
2871 * commit, but this isn't a priority.
2873 update_log(client, zone, LOGLEVEL_DEBUG,
2874 "committing update transaction");
2875 dns_db_closeversion(db, &ver, ISC_TRUE);
2878 * Mark the zone as dirty so that it will be written to disk.
2880 dns_zone_markdirty(zone);
2883 * Notify slaves of the change we just made.
2885 dns_zone_notify(zone);
2887 update_log(client, zone, LOGLEVEL_DEBUG, "redundant request");
2888 dns_db_closeversion(db, &ver, ISC_TRUE);
2890 result = ISC_R_SUCCESS;
2895 * The reason for failure should have been logged at this point.
2898 update_log(client, zone, LOGLEVEL_DEBUG,
2900 dns_db_closeversion(db, &ver, ISC_FALSE);
2904 dns_diff_clear(&temp);
2905 dns_diff_clear(&diff);
2908 dns_db_closeversion(db, &oldver, ISC_FALSE);
2913 if (ssutable != NULL)
2914 dns_ssutable_detach(&ssutable);
2917 dns_zone_detach(&zone);
2919 isc_task_detach(&task);
2920 uev->result = result;
2921 uev->ev_type = DNS_EVENT_UPDATEDONE;
2922 uev->ev_action = updatedone_action;
2923 isc_task_send(client->task, &event);
2924 INSIST(event == NULL);
2928 updatedone_action(isc_task_t *task, isc_event_t *event) {
2929 update_event_t *uev = (update_event_t *) event;
2930 ns_client_t *client = (ns_client_t *) event->ev_arg;
2934 INSIST(event->ev_type == DNS_EVENT_UPDATEDONE);
2935 INSIST(task == client->task);
2937 INSIST(client->nupdates > 0);
2939 respond(client, uev->result);
2940 isc_event_free(&event);
2941 ns_client_detach(&client);
2945 * Update forwarding support.
2949 forward_fail(isc_task_t *task, isc_event_t *event) {
2950 ns_client_t *client = (ns_client_t *)event->ev_arg;
2954 INSIST(client->nupdates > 0);
2956 respond(client, DNS_R_SERVFAIL);
2957 isc_event_free(&event);
2958 ns_client_detach(&client);
2963 forward_callback(void *arg, isc_result_t result, dns_message_t *answer) {
2964 update_event_t *uev = arg;
2965 ns_client_t *client = uev->ev_arg;
2967 if (result != ISC_R_SUCCESS) {
2968 INSIST(answer == NULL);
2969 uev->ev_type = DNS_EVENT_UPDATEDONE;
2970 uev->ev_action = forward_fail;
2972 uev->ev_type = DNS_EVENT_UPDATEDONE;
2973 uev->ev_action = forward_done;
2974 uev->answer = answer;
2976 isc_task_send(client->task, ISC_EVENT_PTR(&uev));
2980 forward_done(isc_task_t *task, isc_event_t *event) {
2981 update_event_t *uev = (update_event_t *) event;
2982 ns_client_t *client = (ns_client_t *)event->ev_arg;
2986 INSIST(client->nupdates > 0);
2988 ns_client_sendraw(client, uev->answer);
2989 dns_message_destroy(&uev->answer);
2990 isc_event_free(&event);
2991 ns_client_detach(&client);
2995 forward_action(isc_task_t *task, isc_event_t *event) {
2996 update_event_t *uev = (update_event_t *) event;
2997 dns_zone_t *zone = uev->zone;
2998 ns_client_t *client = (ns_client_t *)event->ev_arg;
2999 isc_result_t result;
3001 result = dns_zone_forwardupdate(zone, client->message,
3002 forward_callback, event);
3003 if (result != ISC_R_SUCCESS) {
3004 uev->ev_type = DNS_EVENT_UPDATEDONE;
3005 uev->ev_action = forward_fail;
3006 isc_task_send(client->task, &event);
3008 dns_zone_detach(&zone);
3009 isc_task_detach(&task);
3013 send_forward_event(ns_client_t *client, dns_zone_t *zone) {
3014 isc_result_t result = ISC_R_SUCCESS;
3015 update_event_t *event = NULL;
3016 isc_task_t *zonetask = NULL;
3017 ns_client_t *evclient;
3019 event = (update_event_t *)
3020 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
3021 forward_action, NULL, sizeof(*event));
3023 FAIL(ISC_R_NOMEMORY);
3025 event->result = ISC_R_SUCCESS;
3028 ns_client_attach(client, &evclient);
3029 INSIST(client->nupdates == 0);
3031 event->ev_arg = evclient;
3033 dns_zone_gettask(zone, &zonetask);
3034 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
3038 isc_event_free(ISC_EVENT_PTR(&event));