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.88.2.5.2.35 2008/01/17 23:45:27 tbox 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/message.h>
36 #include <dns/rdataclass.h>
37 #include <dns/rdataset.h>
38 #include <dns/rdatasetiter.h>
39 #include <dns/rdatastruct.h>
40 #include <dns/rdatatype.h>
47 #include <named/client.h>
48 #include <named/log.h>
49 #include <named/update.h>
52 * This module implements dynamic update as in RFC2136.
57 - document strict minimality
60 /**************************************************************************/
63 * Log level for tracing dynamic update protocol requests.
65 #define LOGLEVEL_PROTOCOL ISC_LOG_INFO
68 * Log level for low-level debug tracing.
70 #define LOGLEVEL_DEBUG ISC_LOG_DEBUG(8)
73 * Check an operation for failure. These macros all assume that
74 * the function using them has a 'result' variable and a 'failure'
79 if (result != ISC_R_SUCCESS) goto failure; \
83 * Fail unconditionally with result 'code', which must not
84 * be ISC_R_SUCCESS. The reason for failure presumably has
85 * been logged already.
87 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
88 * from complaining about "end-of-loop code not reached".
94 if (result != ISC_R_SUCCESS) goto failure; \
98 * Fail unconditionally and log as a client error.
99 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
100 * from complaining about "end-of-loop code not reached".
102 #define FAILC(code, msg) \
104 const char *_what = "failed"; \
107 case DNS_R_NXDOMAIN: \
108 case DNS_R_YXDOMAIN: \
109 case DNS_R_YXRRSET: \
110 case DNS_R_NXRRSET: \
111 _what = "unsuccessful"; \
113 update_log(client, zone, LOGLEVEL_PROTOCOL, \
114 "update %s: %s (%s)", _what, \
115 msg, isc_result_totext(result)); \
116 if (result != ISC_R_SUCCESS) goto failure; \
119 #define FAILN(code, name, msg) \
121 const char *_what = "failed"; \
124 case DNS_R_NXDOMAIN: \
125 case DNS_R_YXDOMAIN: \
126 case DNS_R_YXRRSET: \
127 case DNS_R_NXRRSET: \
128 _what = "unsuccessful"; \
130 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
131 char _nbuf[DNS_NAME_FORMATSIZE]; \
132 dns_name_format(name, _nbuf, sizeof(_nbuf)); \
133 update_log(client, zone, LOGLEVEL_PROTOCOL, \
134 "update %s: %s: %s (%s)", _what, _nbuf, \
135 msg, isc_result_totext(result)); \
137 if (result != ISC_R_SUCCESS) goto failure; \
140 #define FAILNT(code, name, type, msg) \
142 const char *_what = "failed"; \
145 case DNS_R_NXDOMAIN: \
146 case DNS_R_YXDOMAIN: \
147 case DNS_R_YXRRSET: \
148 case DNS_R_NXRRSET: \
149 _what = "unsuccessful"; \
151 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
152 char _nbuf[DNS_NAME_FORMATSIZE]; \
153 char _tbuf[DNS_RDATATYPE_FORMATSIZE]; \
154 dns_name_format(name, _nbuf, sizeof(_nbuf)); \
155 dns_rdatatype_format(type, _tbuf, sizeof(_tbuf)); \
156 update_log(client, zone, LOGLEVEL_PROTOCOL, \
157 "update %s: %s/%s: %s (%s)", \
158 _what, _nbuf, _tbuf, msg, \
159 isc_result_totext(result)); \
161 if (result != ISC_R_SUCCESS) goto failure; \
164 * Fail unconditionally and log as a server error.
165 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
166 * from complaining about "end-of-loop code not reached".
168 #define FAILS(code, msg) \
171 update_log(client, zone, LOGLEVEL_PROTOCOL, \
173 msg, isc_result_totext(result)); \
174 if (result != ISC_R_SUCCESS) goto failure; \
177 /**************************************************************************/
179 typedef struct rr rr_t;
182 /* dns_name_t name; */
187 typedef struct update_event update_event_t;
189 struct update_event {
190 ISC_EVENT_COMMON(update_event_t);
193 dns_message_t *answer;
196 /**************************************************************************/
198 * Forward declarations.
201 static void update_action(isc_task_t *task, isc_event_t *event);
202 static void updatedone_action(isc_task_t *task, isc_event_t *event);
203 static isc_result_t send_forward_event(ns_client_t *client, dns_zone_t *zone);
204 static void forward_done(isc_task_t *task, isc_event_t *event);
206 /**************************************************************************/
209 update_log(ns_client_t *client, dns_zone_t *zone,
210 int level, const char *fmt, ...) ISC_FORMAT_PRINTF(4, 5);
213 update_log(ns_client_t *client, dns_zone_t *zone,
214 int level, const char *fmt, ...)
218 char namebuf[DNS_NAME_FORMATSIZE];
219 char classbuf[DNS_RDATACLASS_FORMATSIZE];
221 if (client == NULL || zone == NULL)
224 if (isc_log_wouldlog(ns_g_lctx, level) == ISC_FALSE)
227 dns_name_format(dns_zone_getorigin(zone), namebuf,
229 dns_rdataclass_format(dns_zone_getclass(zone), classbuf,
233 vsnprintf(message, sizeof(message), fmt, ap);
236 ns_client_log(client, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
237 level, "updating zone '%s/%s': %s",
238 namebuf, classbuf, message);
242 checkupdateacl(ns_client_t *client, dns_acl_t *acl, const char *message,
243 dns_name_t *zonename, isc_boolean_t slave)
245 char namebuf[DNS_NAME_FORMATSIZE];
246 char classbuf[DNS_RDATACLASS_FORMATSIZE];
247 int level = ISC_LOG_ERROR;
248 const char *msg = "denied";
251 if (slave && acl == NULL) {
252 result = DNS_R_NOTIMP;
253 level = ISC_LOG_DEBUG(3);
256 result = ns_client_checkaclsilent(client, acl, ISC_FALSE);
258 if (result == ISC_R_SUCCESS) {
259 level = ISC_LOG_DEBUG(3);
263 dns_name_format(zonename, namebuf, sizeof(namebuf));
264 dns_rdataclass_format(client->view->rdclass, classbuf,
267 ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
268 NS_LOGMODULE_UPDATE, level, "%s '%s/%s' %s",
269 message, namebuf, classbuf, msg);
274 * Update a single RR in version 'ver' of 'db' and log the
278 * '*tuple' == NULL. Either the tuple is freed, or its
279 * ownership has been transferred to the diff.
282 do_one_tuple(dns_difftuple_t **tuple,
283 dns_db_t *db, dns_dbversion_t *ver,
286 dns_diff_t temp_diff;
290 * Create a singleton diff.
292 dns_diff_init(diff->mctx, &temp_diff);
293 ISC_LIST_APPEND(temp_diff.tuples, *tuple, link);
296 * Apply it to the database.
298 result = dns_diff_apply(&temp_diff, db, ver);
299 ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link);
300 if (result != ISC_R_SUCCESS) {
301 dns_difftuple_free(tuple);
306 * Merge it into the current pending journal entry.
308 dns_diff_appendminimal(diff, tuple);
311 * Do not clear temp_diff.
313 return (ISC_R_SUCCESS);
317 * Perform the updates in 'updates' in version 'ver' of 'db' and log the
321 * 'updates' is empty.
324 do_diff(dns_diff_t *updates, dns_db_t *db, dns_dbversion_t *ver,
328 while (! ISC_LIST_EMPTY(updates->tuples)) {
329 dns_difftuple_t *t = ISC_LIST_HEAD(updates->tuples);
330 ISC_LIST_UNLINK(updates->tuples, t, link);
331 CHECK(do_one_tuple(&t, db, ver, diff));
333 return (ISC_R_SUCCESS);
336 dns_diff_clear(diff);
341 update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
342 dns_diffop_t op, dns_name_t *name,
343 dns_ttl_t ttl, dns_rdata_t *rdata)
345 dns_difftuple_t *tuple = NULL;
347 result = dns_difftuple_create(diff->mctx, op,
348 name, ttl, rdata, &tuple);
349 if (result != ISC_R_SUCCESS)
351 return (do_one_tuple(&tuple, db, ver, diff));
354 /**************************************************************************/
356 * Callback-style iteration over rdatasets and rdatas.
358 * foreach_rrset() can be used to iterate over the RRsets
359 * of a name and call a callback function with each
360 * one. Similarly, foreach_rr() can be used to iterate
361 * over the individual RRs at name, optionally restricted
362 * to RRs of a given type.
364 * The callback functions are called "actions" and take
365 * two arguments: a void pointer for passing arbitrary
366 * context information, and a pointer to the current RRset
367 * or RR. By convention, their names end in "_action".
371 * XXXRTH We might want to make this public somewhere in libdns.
375 * Function type for foreach_rrset() iterator actions.
377 typedef isc_result_t rrset_func(void *data, dns_rdataset_t *rrset);
380 * Function type for foreach_rr() iterator actions.
382 typedef isc_result_t rr_func(void *data, rr_t *rr);
385 * Internal context struct for foreach_node_rr().
389 void * rr_action_data;
390 } foreach_node_rr_ctx_t;
393 * Internal helper function for foreach_node_rr().
396 foreach_node_rr_action(void *data, dns_rdataset_t *rdataset) {
398 foreach_node_rr_ctx_t *ctx = data;
399 for (result = dns_rdataset_first(rdataset);
400 result == ISC_R_SUCCESS;
401 result = dns_rdataset_next(rdataset))
403 rr_t rr = { 0, DNS_RDATA_INIT };
405 dns_rdataset_current(rdataset, &rr.rdata);
406 rr.ttl = rdataset->ttl;
407 result = (*ctx->rr_action)(ctx->rr_action_data, &rr);
408 if (result != ISC_R_SUCCESS)
411 if (result != ISC_R_NOMORE)
413 return (ISC_R_SUCCESS);
417 * For each rdataset of 'name' in 'ver' of 'db', call 'action'
418 * with the rdataset and 'action_data' as arguments. If the name
419 * does not exist, do nothing.
421 * If 'action' returns an error, abort iteration and return the error.
424 foreach_rrset(dns_db_t *db,
425 dns_dbversion_t *ver,
432 dns_rdatasetiter_t *iter;
435 result = dns_db_findnode(db, name, ISC_FALSE, &node);
436 if (result == ISC_R_NOTFOUND)
437 return (ISC_R_SUCCESS);
438 if (result != ISC_R_SUCCESS)
442 result = dns_db_allrdatasets(db, node, ver,
443 (isc_stdtime_t) 0, &iter);
444 if (result != ISC_R_SUCCESS)
447 for (result = dns_rdatasetiter_first(iter);
448 result == ISC_R_SUCCESS;
449 result = dns_rdatasetiter_next(iter))
451 dns_rdataset_t rdataset;
453 dns_rdataset_init(&rdataset);
454 dns_rdatasetiter_current(iter, &rdataset);
456 result = (*action)(action_data, &rdataset);
458 dns_rdataset_disassociate(&rdataset);
459 if (result != ISC_R_SUCCESS)
460 goto cleanup_iterator;
462 if (result == ISC_R_NOMORE)
463 result = ISC_R_SUCCESS;
466 dns_rdatasetiter_destroy(&iter);
469 dns_db_detachnode(db, &node);
475 * For each RR of 'name' in 'ver' of 'db', call 'action'
476 * with the RR and 'action_data' as arguments. If the name
477 * does not exist, do nothing.
479 * If 'action' returns an error, abort iteration
480 * and return the error.
483 foreach_node_rr(dns_db_t *db,
484 dns_dbversion_t *ver,
487 void *rr_action_data)
489 foreach_node_rr_ctx_t ctx;
490 ctx.rr_action = rr_action;
491 ctx.rr_action_data = rr_action_data;
492 return (foreach_rrset(db, ver, name,
493 foreach_node_rr_action, &ctx));
498 * For each of the RRs specified by 'db', 'ver', 'name', 'type',
499 * (which can be dns_rdatatype_any to match any type), and 'covers', call
500 * 'action' with the RR and 'action_data' as arguments. If the name
501 * does not exist, or if no RRset of the given type exists at the name,
504 * If 'action' returns an error, abort iteration and return the error.
507 foreach_rr(dns_db_t *db,
508 dns_dbversion_t *ver,
510 dns_rdatatype_t type,
511 dns_rdatatype_t covers,
513 void *rr_action_data)
518 dns_rdataset_t rdataset;
520 if (type == dns_rdatatype_any)
521 return (foreach_node_rr(db, ver, name,
522 rr_action, rr_action_data));
525 result = dns_db_findnode(db, name, ISC_FALSE, &node);
526 if (result == ISC_R_NOTFOUND)
527 return (ISC_R_SUCCESS);
528 if (result != ISC_R_SUCCESS)
531 dns_rdataset_init(&rdataset);
532 result = dns_db_findrdataset(db, node, ver, type, covers,
533 (isc_stdtime_t) 0, &rdataset, NULL);
534 if (result == ISC_R_NOTFOUND) {
535 result = ISC_R_SUCCESS;
538 if (result != ISC_R_SUCCESS)
541 for (result = dns_rdataset_first(&rdataset);
542 result == ISC_R_SUCCESS;
543 result = dns_rdataset_next(&rdataset))
545 rr_t rr = { 0, DNS_RDATA_INIT };
546 dns_rdataset_current(&rdataset, &rr.rdata);
547 rr.ttl = rdataset.ttl;
548 result = (*rr_action)(rr_action_data, &rr);
549 if (result != ISC_R_SUCCESS)
550 goto cleanup_rdataset;
552 if (result != ISC_R_NOMORE)
553 goto cleanup_rdataset;
554 result = ISC_R_SUCCESS;
557 dns_rdataset_disassociate(&rdataset);
559 dns_db_detachnode(db, &node);
564 /**************************************************************************/
566 * Various tests on the database contents (for prerequisites, etc).
570 * Function type for predicate functions that compare a database RR 'db_rr'
571 * against an update RR 'update_rr'.
573 typedef isc_boolean_t rr_predicate(dns_rdata_t *update_rr, dns_rdata_t *db_rr);
576 * Helper function for rrset_exists().
579 rrset_exists_action(void *data, rr_t *rr) {
582 return (ISC_R_EXISTS);
586 * Utility macro for RR existence checking functions.
588 * If the variable 'result' has the value ISC_R_EXISTS or
589 * ISC_R_SUCCESS, set *exists to ISC_TRUE or ISC_FALSE,
590 * respectively, and return success.
592 * If 'result' has any other value, there was a failure.
593 * Return the failure result code and do not set *exists.
595 * This would be more readable as "do { if ... } while(0)",
596 * but that form generates tons of warnings on Solaris 2.6.
598 #define RETURN_EXISTENCE_FLAG \
599 return ((result == ISC_R_EXISTS) ? \
600 (*exists = ISC_TRUE, ISC_R_SUCCESS) : \
601 ((result == ISC_R_SUCCESS) ? \
602 (*exists = ISC_FALSE, ISC_R_SUCCESS) : \
606 * Set '*exists' to true iff an rrset of the given type exists,
607 * to false otherwise.
610 rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
611 dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers,
612 isc_boolean_t *exists)
615 result = foreach_rr(db, ver, name, type, covers,
616 rrset_exists_action, NULL);
617 RETURN_EXISTENCE_FLAG;
621 * Helper function for cname_incompatible_rrset_exists.
624 cname_compatibility_action(void *data, dns_rdataset_t *rrset) {
626 if (rrset->type != dns_rdatatype_cname &&
627 ! dns_rdatatype_isdnssec(rrset->type))
628 return (ISC_R_EXISTS);
629 return (ISC_R_SUCCESS);
633 * Check whether there is an rrset incompatible with adding a CNAME RR,
634 * i.e., anything but another CNAME (which can be replaced) or a
635 * DNSSEC RR (which can coexist).
637 * If such an incompatible rrset exists, set '*exists' to ISC_TRUE.
638 * Otherwise, set it to ISC_FALSE.
641 cname_incompatible_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
642 dns_name_t *name, isc_boolean_t *exists) {
644 result = foreach_rrset(db, ver, name,
645 cname_compatibility_action, NULL);
646 RETURN_EXISTENCE_FLAG;
650 * Helper function for rr_count().
653 count_rr_action(void *data, rr_t *rr) {
657 return (ISC_R_SUCCESS);
661 * Count the number of RRs of 'type' belonging to 'name' in 'ver' of 'db'.
664 rr_count(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
665 dns_rdatatype_t type, dns_rdatatype_t covers, int *countp)
668 return (foreach_rr(db, ver, name, type, covers,
669 count_rr_action, countp));
673 * Context struct and helper function for name_exists().
677 name_exists_action(void *data, dns_rdataset_t *rrset) {
680 return (ISC_R_EXISTS);
684 * Set '*exists' to true iff the given name exists, to false otherwise.
687 name_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
688 isc_boolean_t *exists)
691 result = foreach_rrset(db, ver, name,
692 name_exists_action, NULL);
693 RETURN_EXISTENCE_FLAG;
697 dns_name_t *name, *signer;
698 dns_ssutable_t *table;
702 ssu_checkrule(void *data, dns_rdataset_t *rrset) {
703 ssu_check_t *ssuinfo = data;
704 isc_boolean_t result;
707 * If we're deleting all records, it's ok to delete RRSIG and NSEC even
708 * if we're normally not allowed to.
710 if (rrset->type == dns_rdatatype_rrsig ||
711 rrset->type == dns_rdatatype_nsec)
712 return (ISC_R_SUCCESS);
713 result = dns_ssutable_checkrules(ssuinfo->table, ssuinfo->signer,
714 ssuinfo->name, rrset->type);
715 return (result == ISC_TRUE ? ISC_R_SUCCESS : ISC_R_FAILURE);
719 ssu_checkall(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
720 dns_ssutable_t *ssutable, dns_name_t *signer)
726 ssuinfo.table = ssutable;
727 ssuinfo.signer = signer;
728 result = foreach_rrset(db, ver, name, ssu_checkrule, &ssuinfo);
729 return (ISC_TF(result == ISC_R_SUCCESS));
732 /**************************************************************************/
734 * Checking of "RRset exists (value dependent)" prerequisites.
736 * In the RFC2136 section 3.2.5, this is the pseudocode involving
737 * a variable called "temp", a mapping of <name, type> tuples to rrsets.
739 * Here, we represent the "temp" data structure as (non-minimial) "dns_diff_t"
740 * where each typle has op==DNS_DIFFOP_EXISTS.
745 * Append a tuple asserting the existence of the RR with
746 * 'name' and 'rdata' to 'diff'.
749 temp_append(dns_diff_t *diff, dns_name_t *name, dns_rdata_t *rdata) {
751 dns_difftuple_t *tuple = NULL;
753 REQUIRE(DNS_DIFF_VALID(diff));
754 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_EXISTS,
755 name, 0, rdata, &tuple));
756 ISC_LIST_APPEND(diff->tuples, tuple, link);
762 * Compare two rdatasets represented as sorted lists of tuples.
763 * All list elements must have the same owner name and type.
764 * Return ISC_R_SUCCESS if the rdatasets are equal, rcode(dns_rcode_nxrrset)
768 temp_check_rrset(dns_difftuple_t *a, dns_difftuple_t *b) {
770 if (a == NULL || b == NULL)
772 INSIST(a->op == DNS_DIFFOP_EXISTS &&
773 b->op == DNS_DIFFOP_EXISTS);
774 INSIST(a->rdata.type == b->rdata.type);
775 INSIST(dns_name_equal(&a->name, &b->name));
776 if (dns_rdata_compare(&a->rdata, &b->rdata) != 0)
777 return (DNS_R_NXRRSET);
778 a = ISC_LIST_NEXT(a, link);
779 b = ISC_LIST_NEXT(b, link);
781 if (a != NULL || b != NULL)
782 return (DNS_R_NXRRSET);
783 return (ISC_R_SUCCESS);
787 * A comparison function defining the sorting order for the entries
788 * in the "temp" data structure. The major sort key is the owner name,
789 * followed by the type and rdata.
792 temp_order(const void *av, const void *bv) {
793 dns_difftuple_t const * const *ap = av;
794 dns_difftuple_t const * const *bp = bv;
795 dns_difftuple_t const *a = *ap;
796 dns_difftuple_t const *b = *bp;
798 r = dns_name_compare(&a->name, &b->name);
801 r = (b->rdata.type - a->rdata.type);
804 r = dns_rdata_compare(&a->rdata, &b->rdata);
809 * Check the "RRset exists (value dependent)" prerequisite information
810 * in 'temp' against the contents of the database 'db'.
812 * Return ISC_R_SUCCESS if the prerequisites are satisfied,
813 * rcode(dns_rcode_nxrrset) if not.
815 * 'temp' must be pre-sorted.
819 temp_check(isc_mem_t *mctx, dns_diff_t *temp, dns_db_t *db,
820 dns_dbversion_t *ver, dns_name_t *tmpname, dns_rdatatype_t *typep)
828 dns_diff_init(mctx, &trash);
831 * For each name and type in the prerequisites,
832 * construct a sorted rdata list of the corresponding
833 * database contents, and compare the lists.
835 t = ISC_LIST_HEAD(temp->tuples);
838 (void)dns_name_copy(name, tmpname, NULL);
839 *typep = t->rdata.type;
841 /* A new unique name begins here. */
843 result = dns_db_findnode(db, name, ISC_FALSE, &node);
844 if (result == ISC_R_NOTFOUND) {
845 dns_diff_clear(&trash);
846 return (DNS_R_NXRRSET);
848 if (result != ISC_R_SUCCESS) {
849 dns_diff_clear(&trash);
853 /* A new unique type begins here. */
854 while (t != NULL && dns_name_equal(&t->name, name)) {
855 dns_rdatatype_t type, covers;
856 dns_rdataset_t rdataset;
857 dns_diff_t d_rrs; /* Database RRs with
858 this name and type */
859 dns_diff_t u_rrs; /* Update RRs with
860 this name and type */
862 *typep = type = t->rdata.type;
863 if (type == dns_rdatatype_rrsig ||
864 type == dns_rdatatype_sig)
865 covers = dns_rdata_covers(&t->rdata);
870 * Collect all database RRs for this name and type
871 * onto d_rrs and sort them.
873 dns_rdataset_init(&rdataset);
874 result = dns_db_findrdataset(db, node, ver, type,
875 covers, (isc_stdtime_t) 0,
877 if (result != ISC_R_SUCCESS) {
878 dns_db_detachnode(db, &node);
879 dns_diff_clear(&trash);
880 return (DNS_R_NXRRSET);
883 dns_diff_init(mctx, &d_rrs);
884 dns_diff_init(mctx, &u_rrs);
886 for (result = dns_rdataset_first(&rdataset);
887 result == ISC_R_SUCCESS;
888 result = dns_rdataset_next(&rdataset))
890 dns_rdata_t rdata = DNS_RDATA_INIT;
891 dns_rdataset_current(&rdataset, &rdata);
892 result = temp_append(&d_rrs, name, &rdata);
893 if (result != ISC_R_SUCCESS)
896 if (result != ISC_R_NOMORE)
898 result = dns_diff_sort(&d_rrs, temp_order);
899 if (result != ISC_R_SUCCESS)
903 * Collect all update RRs for this name and type
904 * onto u_rrs. No need to sort them here -
905 * they are already sorted.
908 dns_name_equal(&t->name, name) &&
909 t->rdata.type == type)
911 dns_difftuple_t *next =
912 ISC_LIST_NEXT(t, link);
913 ISC_LIST_UNLINK(temp->tuples, t, link);
914 ISC_LIST_APPEND(u_rrs.tuples, t, link);
918 /* Compare the two sorted lists. */
919 result = temp_check_rrset(ISC_LIST_HEAD(u_rrs.tuples),
920 ISC_LIST_HEAD(d_rrs.tuples));
921 if (result != ISC_R_SUCCESS)
925 * We are done with the tuples, but we can't free
926 * them yet because "name" still points into one
927 * of them. Move them on a temporary list.
929 ISC_LIST_APPENDLIST(trash.tuples, u_rrs.tuples, link);
930 ISC_LIST_APPENDLIST(trash.tuples, d_rrs.tuples, link);
931 dns_rdataset_disassociate(&rdataset);
936 dns_diff_clear(&d_rrs);
937 dns_diff_clear(&u_rrs);
938 dns_diff_clear(&trash);
939 dns_rdataset_disassociate(&rdataset);
940 dns_db_detachnode(db, &node);
944 dns_db_detachnode(db, &node);
947 dns_diff_clear(&trash);
948 return (ISC_R_SUCCESS);
951 /**************************************************************************/
953 * Conditional deletion of RRs.
957 * Context structure for delete_if().
961 rr_predicate *predicate;
963 dns_dbversion_t *ver;
966 dns_rdata_t *update_rr;
967 } conditional_delete_ctx_t;
970 * Predicate functions for delete_if().
974 * Return true iff 'db_rr' is neither a SOA nor an NS RR nor
975 * an RRSIG nor a NSEC.
978 type_not_soa_nor_ns_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
980 return ((db_rr->type != dns_rdatatype_soa &&
981 db_rr->type != dns_rdatatype_ns &&
982 db_rr->type != dns_rdatatype_rrsig &&
983 db_rr->type != dns_rdatatype_nsec) ?
984 ISC_TRUE : ISC_FALSE);
988 * Return true iff 'db_rr' is neither a RRSIG nor a NSEC.
991 type_not_dnssec(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
993 return ((db_rr->type != dns_rdatatype_rrsig &&
994 db_rr->type != dns_rdatatype_nsec) ?
995 ISC_TRUE : ISC_FALSE);
999 * Return true always.
1001 static isc_boolean_t
1002 true_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1009 * Return true iff the two RRs have identical rdata.
1011 static isc_boolean_t
1012 rr_equal_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1014 * XXXRTH This is not a problem, but we should consider creating
1015 * dns_rdata_equal() (that used dns_name_equal()), since it
1016 * would be faster. Not a priority.
1018 return (dns_rdata_compare(update_rr, db_rr) == 0 ?
1019 ISC_TRUE : ISC_FALSE);
1023 * Return true iff 'update_rr' should replace 'db_rr' according
1024 * to the special RFC2136 rules for CNAME, SOA, and WKS records.
1026 * RFC2136 does not mention NSEC or DNAME, but multiple NSECs or DNAMEs
1027 * make little sense, so we replace those, too.
1029 static isc_boolean_t
1030 replaces_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1031 if (db_rr->type != update_rr->type)
1033 if (db_rr->type == dns_rdatatype_cname)
1035 if (db_rr->type == dns_rdatatype_dname)
1037 if (db_rr->type == dns_rdatatype_soa)
1039 if (db_rr->type == dns_rdatatype_nsec)
1041 if (db_rr->type == dns_rdatatype_wks) {
1043 * Compare the address and protocol fields only. These
1044 * form the first five bytes of the RR data. Do a
1045 * raw binary comparison; unpacking the WKS RRs using
1046 * dns_rdata_tostruct() might be cleaner in some ways,
1047 * but it would require us to pass around an mctx.
1049 INSIST(db_rr->length >= 5 && update_rr->length >= 5);
1050 return (memcmp(db_rr->data, update_rr->data, 5) == 0 ?
1051 ISC_TRUE : ISC_FALSE);
1057 * Internal helper function for delete_if().
1060 delete_if_action(void *data, rr_t *rr) {
1061 conditional_delete_ctx_t *ctx = data;
1062 if ((*ctx->predicate)(ctx->update_rr, &rr->rdata)) {
1063 isc_result_t result;
1064 result = update_one_rr(ctx->db, ctx->ver, ctx->diff,
1065 DNS_DIFFOP_DEL, ctx->name,
1066 rr->ttl, &rr->rdata);
1069 return (ISC_R_SUCCESS);
1074 * Conditionally delete RRs. Apply 'predicate' to the RRs
1075 * specified by 'db', 'ver', 'name', and 'type' (which can
1076 * be dns_rdatatype_any to match any type). Delete those
1077 * RRs for which the predicate returns true, and log the
1078 * deletions in 'diff'.
1081 delete_if(rr_predicate *predicate,
1083 dns_dbversion_t *ver,
1085 dns_rdatatype_t type,
1086 dns_rdatatype_t covers,
1087 dns_rdata_t *update_rr,
1090 conditional_delete_ctx_t ctx;
1091 ctx.predicate = predicate;
1096 ctx.update_rr = update_rr;
1097 return (foreach_rr(db, ver, name, type, covers,
1098 delete_if_action, &ctx));
1101 /**************************************************************************/
1103 * Prepare an RR for the addition of the new RR 'ctx->update_rr',
1104 * with TTL 'ctx->update_rr_ttl', to its rdataset, by deleting
1105 * the RRs if it is replaced by the new RR or has a conflicting TTL.
1106 * The necessary changes are appended to ctx->del_diff and ctx->add_diff;
1107 * we need to do all deletions before any additions so that we don't run
1108 * into transient states with conflicting TTLs.
1113 dns_dbversion_t *ver;
1116 dns_rdata_t *update_rr;
1117 dns_ttl_t update_rr_ttl;
1118 isc_boolean_t ignore_add;
1119 dns_diff_t del_diff;
1120 dns_diff_t add_diff;
1121 } add_rr_prepare_ctx_t;
1124 add_rr_prepare_action(void *data, rr_t *rr) {
1125 isc_result_t result = ISC_R_SUCCESS;
1126 add_rr_prepare_ctx_t *ctx = data;
1127 dns_difftuple_t *tuple = NULL;
1128 isc_boolean_t equal;
1131 * If the update RR is a "duplicate" of the update RR,
1132 * the update should be silently ignored.
1134 equal = ISC_TF(dns_rdata_compare(&rr->rdata, ctx->update_rr) == 0);
1135 if (equal && rr->ttl == ctx->update_rr_ttl) {
1136 ctx->ignore_add = ISC_TRUE;
1137 return (ISC_R_SUCCESS);
1141 * If this RR is "equal" to the update RR, it should
1142 * be deleted before the update RR is added.
1144 if (replaces_p(ctx->update_rr, &rr->rdata)) {
1145 CHECK(dns_difftuple_create(ctx->del_diff.mctx,
1146 DNS_DIFFOP_DEL, ctx->name,
1150 dns_diff_append(&ctx->del_diff, &tuple);
1151 return (ISC_R_SUCCESS);
1155 * If this RR differs in TTL from the update RR,
1156 * its TTL must be adjusted.
1158 if (rr->ttl != ctx->update_rr_ttl) {
1159 CHECK(dns_difftuple_create(ctx->del_diff.mctx,
1160 DNS_DIFFOP_DEL, ctx->name,
1164 dns_diff_append(&ctx->del_diff, &tuple);
1166 CHECK(dns_difftuple_create(ctx->add_diff.mctx,
1167 DNS_DIFFOP_ADD, ctx->name,
1171 dns_diff_append(&ctx->add_diff, &tuple);
1178 /**************************************************************************/
1180 * Miscellaneous subroutines.
1184 * Extract a single update RR from 'section' of dynamic update message
1185 * 'msg', with consistency checking.
1187 * Stores the owner name, rdata, and TTL of the update RR at 'name',
1188 * 'rdata', and 'ttl', respectively.
1191 get_current_rr(dns_message_t *msg, dns_section_t section,
1192 dns_rdataclass_t zoneclass,
1193 dns_name_t **name, dns_rdata_t *rdata, dns_rdatatype_t *covers,
1195 dns_rdataclass_t *update_class)
1197 dns_rdataset_t *rdataset;
1198 isc_result_t result;
1199 dns_message_currentname(msg, section, name);
1200 rdataset = ISC_LIST_HEAD((*name)->list);
1201 INSIST(rdataset != NULL);
1202 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
1203 *covers = rdataset->covers;
1204 *ttl = rdataset->ttl;
1205 result = dns_rdataset_first(rdataset);
1206 INSIST(result == ISC_R_SUCCESS);
1207 dns_rdataset_current(rdataset, rdata);
1208 INSIST(dns_rdataset_next(rdataset) == ISC_R_NOMORE);
1209 *update_class = rdata->rdclass;
1210 rdata->rdclass = zoneclass;
1214 * Increment the SOA serial number of database 'db', version 'ver'.
1215 * Replace the SOA record in the database, and log the
1220 * XXXRTH Failures in this routine will be worth logging, when
1221 * we have a logging system. Failure to find the zonename
1222 * or the SOA rdataset warrant at least an UNEXPECTED_ERROR().
1226 increment_soa_serial(dns_db_t *db, dns_dbversion_t *ver,
1227 dns_diff_t *diff, isc_mem_t *mctx)
1229 dns_difftuple_t *deltuple = NULL;
1230 dns_difftuple_t *addtuple = NULL;
1231 isc_uint32_t serial;
1232 isc_result_t result;
1234 CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_DEL, &deltuple));
1235 CHECK(dns_difftuple_copy(deltuple, &addtuple));
1236 addtuple->op = DNS_DIFFOP_ADD;
1238 serial = dns_soa_getserial(&addtuple->rdata);
1241 serial = (serial + 1) & 0xFFFFFFFF;
1245 dns_soa_setserial(serial, &addtuple->rdata);
1246 CHECK(do_one_tuple(&deltuple, db, ver, diff));
1247 CHECK(do_one_tuple(&addtuple, db, ver, diff));
1248 result = ISC_R_SUCCESS;
1251 if (addtuple != NULL)
1252 dns_difftuple_free(&addtuple);
1253 if (deltuple != NULL)
1254 dns_difftuple_free(&deltuple);
1259 * Check that the new SOA record at 'update_rdata' does not
1260 * illegally cause the SOA serial number to decrease or stay
1261 * unchanged relative to the existing SOA in 'db'.
1263 * Sets '*ok' to ISC_TRUE if the update is legal, ISC_FALSE if not.
1265 * William King points out that RFC2136 is inconsistent about
1266 * the case where the serial number stays unchanged:
1268 * section 3.4.2.2 requires a server to ignore a SOA update request
1269 * if the serial number on the update SOA is less_than_or_equal to
1270 * the zone SOA serial.
1272 * section 3.6 requires a server to ignore a SOA update request if
1273 * the serial is less_than the zone SOA serial.
1275 * Paul says 3.4.2.2 is correct.
1279 check_soa_increment(dns_db_t *db, dns_dbversion_t *ver,
1280 dns_rdata_t *update_rdata,
1283 isc_uint32_t db_serial;
1284 isc_uint32_t update_serial;
1285 isc_result_t result;
1287 update_serial = dns_soa_getserial(update_rdata);
1289 result = dns_db_getsoaserial(db, ver, &db_serial);
1290 if (result != ISC_R_SUCCESS)
1293 if (DNS_SERIAL_GE(db_serial, update_serial)) {
1299 return (ISC_R_SUCCESS);
1303 /**************************************************************************/
1305 * Incremental updating of NSECs and RRSIGs.
1308 #define MAXZONEKEYS 32 /* Maximum number of zone keys supported. */
1311 * We abuse the dns_diff_t type to represent a set of domain names
1312 * affected by the update.
1315 namelist_append_name(dns_diff_t *list, dns_name_t *name) {
1316 isc_result_t result;
1317 dns_difftuple_t *tuple = NULL;
1318 static dns_rdata_t dummy_rdata = { NULL, 0, 0, 0, 0,
1319 { (void*)(-1), (void*)(-1) } };
1320 CHECK(dns_difftuple_create(list->mctx, DNS_DIFFOP_EXISTS, name, 0,
1321 &dummy_rdata, &tuple));
1322 dns_diff_append(list, &tuple);
1328 namelist_append_subdomain(dns_db_t *db, dns_name_t *name, dns_diff_t *affected)
1330 isc_result_t result;
1331 dns_fixedname_t fixedname;
1333 dns_dbiterator_t *dbit = NULL;
1335 dns_fixedname_init(&fixedname);
1336 child = dns_fixedname_name(&fixedname);
1338 CHECK(dns_db_createiterator(db, ISC_FALSE, &dbit));
1340 for (result = dns_dbiterator_seek(dbit, name);
1341 result == ISC_R_SUCCESS;
1342 result = dns_dbiterator_next(dbit))
1344 dns_dbnode_t *node = NULL;
1345 CHECK(dns_dbiterator_current(dbit, &node, child));
1346 dns_db_detachnode(db, &node);
1347 if (! dns_name_issubdomain(child, name))
1349 CHECK(namelist_append_name(affected, child));
1351 if (result == ISC_R_NOMORE)
1352 result = ISC_R_SUCCESS;
1355 dns_dbiterator_destroy(&dbit);
1362 * Helper function for non_nsec_rrset_exists().
1365 is_non_nsec_action(void *data, dns_rdataset_t *rrset) {
1367 if (!(rrset->type == dns_rdatatype_nsec ||
1368 (rrset->type == dns_rdatatype_rrsig &&
1369 rrset->covers == dns_rdatatype_nsec)))
1370 return (ISC_R_EXISTS);
1371 return (ISC_R_SUCCESS);
1375 * Check whether there is an rrset other than a NSEC or RRSIG NSEC,
1376 * i.e., anything that justifies the continued existence of a name
1377 * after a secure update.
1379 * If such an rrset exists, set '*exists' to ISC_TRUE.
1380 * Otherwise, set it to ISC_FALSE.
1383 non_nsec_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
1384 dns_name_t *name, isc_boolean_t *exists)
1386 isc_result_t result;
1387 result = foreach_rrset(db, ver, name,
1388 is_non_nsec_action, NULL);
1389 RETURN_EXISTENCE_FLAG;
1393 * A comparison function for sorting dns_diff_t:s by name.
1396 name_order(const void *av, const void *bv) {
1397 dns_difftuple_t const * const *ap = av;
1398 dns_difftuple_t const * const *bp = bv;
1399 dns_difftuple_t const *a = *ap;
1400 dns_difftuple_t const *b = *bp;
1401 return (dns_name_compare(&a->name, &b->name));
1405 uniqify_name_list(dns_diff_t *list) {
1406 isc_result_t result;
1407 dns_difftuple_t *p, *q;
1409 CHECK(dns_diff_sort(list, name_order));
1411 p = ISC_LIST_HEAD(list->tuples);
1414 q = ISC_LIST_NEXT(p, link);
1415 if (q == NULL || ! dns_name_equal(&p->name, &q->name))
1417 ISC_LIST_UNLINK(list->tuples, q, link);
1418 dns_difftuple_free(&q);
1420 p = ISC_LIST_NEXT(p, link);
1428 is_glue(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1429 isc_boolean_t *flag)
1431 isc_result_t result;
1432 dns_fixedname_t foundname;
1433 dns_fixedname_init(&foundname);
1434 result = dns_db_find(db, name, ver, dns_rdatatype_any,
1435 DNS_DBFIND_GLUEOK | DNS_DBFIND_NOWILD,
1436 (isc_stdtime_t) 0, NULL,
1437 dns_fixedname_name(&foundname),
1439 if (result == ISC_R_SUCCESS) {
1441 return (ISC_R_SUCCESS);
1442 } else if (result == DNS_R_ZONECUT) {
1444 * We are at the zonecut. The name will have an NSEC, but
1445 * non-delegation will be omitted from the type bit map.
1448 return (ISC_R_SUCCESS);
1449 } else if (result == DNS_R_GLUE || result == DNS_R_DNAME) {
1451 return (ISC_R_SUCCESS);
1458 * Find the next/previous name that has a NSEC record.
1459 * In other words, skip empty database nodes and names that
1460 * have had their NSECs removed because they are obscured by
1464 next_active(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1465 dns_dbversion_t *ver, dns_name_t *oldname, dns_name_t *newname,
1466 isc_boolean_t forward)
1468 isc_result_t result;
1469 dns_dbiterator_t *dbit = NULL;
1470 isc_boolean_t has_nsec;
1471 unsigned int wraps = 0;
1473 CHECK(dns_db_createiterator(db, ISC_FALSE, &dbit));
1475 CHECK(dns_dbiterator_seek(dbit, oldname));
1477 dns_dbnode_t *node = NULL;
1480 result = dns_dbiterator_next(dbit);
1482 result = dns_dbiterator_prev(dbit);
1483 if (result == ISC_R_NOMORE) {
1488 CHECK(dns_dbiterator_first(dbit));
1490 CHECK(dns_dbiterator_last(dbit));
1493 update_log(client, zone, ISC_LOG_ERROR,
1494 "secure zone with no NSECs");
1495 result = DNS_R_BADZONE;
1499 CHECK(dns_dbiterator_current(dbit, &node, newname));
1500 dns_db_detachnode(db, &node);
1503 * The iterator may hold the tree lock, and
1504 * rrset_exists() calls dns_db_findnode() which
1505 * may try to reacquire it. To avoid deadlock
1506 * we must pause the iterator first.
1508 CHECK(dns_dbiterator_pause(dbit));
1509 CHECK(rrset_exists(db, ver, newname,
1510 dns_rdatatype_nsec, 0, &has_nsec));
1512 } while (! has_nsec);
1515 dns_dbiterator_destroy(&dbit);
1521 * Add a NSEC record for "name", recording the change in "diff".
1522 * The existing NSEC is removed.
1525 add_nsec(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1526 dns_dbversion_t *ver, dns_name_t *name, dns_ttl_t nsecttl,
1529 isc_result_t result;
1530 dns_dbnode_t *node = NULL;
1531 unsigned char buffer[DNS_NSEC_BUFFERSIZE];
1532 dns_rdata_t rdata = DNS_RDATA_INIT;
1533 dns_difftuple_t *tuple = NULL;
1534 dns_fixedname_t fixedname;
1537 dns_fixedname_init(&fixedname);
1538 target = dns_fixedname_name(&fixedname);
1541 * Find the successor name, aka NSEC target.
1543 CHECK(next_active(client, zone, db, ver, name, target, ISC_TRUE));
1546 * Create the NSEC RDATA.
1548 CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
1549 dns_rdata_init(&rdata);
1550 CHECK(dns_nsec_buildrdata(db, ver, node, target, buffer, &rdata));
1551 dns_db_detachnode(db, &node);
1554 * Delete the old NSEC and record the change.
1556 CHECK(delete_if(true_p, db, ver, name, dns_rdatatype_nsec, 0,
1559 * Add the new NSEC and record the change.
1561 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name,
1562 nsecttl, &rdata, &tuple));
1563 CHECK(do_one_tuple(&tuple, db, ver, diff));
1564 INSIST(tuple == NULL);
1568 dns_db_detachnode(db, &node);
1573 * Add a placeholder NSEC record for "name", recording the change in "diff".
1576 add_placeholder_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1578 isc_result_t result;
1579 dns_difftuple_t *tuple = NULL;
1581 unsigned char data[1] = { 0 }; /* The root domain, no bits. */
1582 dns_rdata_t rdata = DNS_RDATA_INIT;
1585 r.length = sizeof(data);
1586 dns_rdata_fromregion(&rdata, dns_db_class(db), dns_rdatatype_nsec, &r);
1587 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, 0,
1589 CHECK(do_one_tuple(&tuple, db, ver, diff));
1595 find_zone_keys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
1596 isc_mem_t *mctx, unsigned int maxkeys,
1597 dst_key_t **keys, unsigned int *nkeys)
1599 isc_result_t result;
1600 dns_dbnode_t *node = NULL;
1601 const char *directory = dns_zone_getkeydirectory(zone);
1602 CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
1603 CHECK(dns_dnssec_findzonekeys2(db, ver, node, dns_db_origin(db),
1604 directory, mctx, maxkeys, keys, nkeys));
1607 dns_db_detachnode(db, &node);
1612 * Add RRSIG records for an RRset, recording the change in "diff".
1615 add_sigs(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1616 dns_rdatatype_t type, dns_diff_t *diff, dst_key_t **keys,
1617 unsigned int nkeys, isc_mem_t *mctx, isc_stdtime_t inception,
1618 isc_stdtime_t expire)
1620 isc_result_t result;
1621 dns_dbnode_t *node = NULL;
1622 dns_rdataset_t rdataset;
1623 dns_rdata_t sig_rdata = DNS_RDATA_INIT;
1624 isc_buffer_t buffer;
1625 unsigned char data[1024]; /* XXX */
1628 dns_rdataset_init(&rdataset);
1629 isc_buffer_init(&buffer, data, sizeof(data));
1631 /* Get the rdataset to sign. */
1632 CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
1633 CHECK(dns_db_findrdataset(db, node, ver, type, 0,
1636 dns_db_detachnode(db, &node);
1638 for (i = 0; i < nkeys; i++) {
1639 if (!dst_key_isprivate(keys[i]))
1641 /* Calculate the signature, creating a RRSIG RDATA. */
1642 CHECK(dns_dnssec_sign(name, &rdataset, keys[i],
1643 &inception, &expire,
1644 mctx, &buffer, &sig_rdata));
1646 /* Update the database and journal with the RRSIG. */
1647 /* XXX inefficient - will cause dataset merging */
1648 CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADD, name,
1649 rdataset.ttl, &sig_rdata));
1650 dns_rdata_reset(&sig_rdata);
1654 if (dns_rdataset_isassociated(&rdataset))
1655 dns_rdataset_disassociate(&rdataset);
1657 dns_db_detachnode(db, &node);
1662 * Update RRSIG and NSEC records affected by an update. The original
1663 * update, including the SOA serial update but exluding the RRSIG & NSEC
1664 * changes, is in "diff" and has already been applied to "newver" of "db".
1665 * The database version prior to the update is "oldver".
1667 * The necessary RRSIG and NSEC changes will be applied to "newver"
1668 * and added (as a minimal diff) to "diff".
1670 * The RRSIGs generated will be valid for 'sigvalidityinterval' seconds.
1673 update_signatures(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1674 dns_dbversion_t *oldver, dns_dbversion_t *newver,
1675 dns_diff_t *diff, isc_uint32_t sigvalidityinterval)
1677 isc_result_t result;
1679 dns_diff_t diffnames;
1680 dns_diff_t affected;
1681 dns_diff_t sig_diff;
1682 dns_diff_t nsec_diff;
1683 dns_diff_t nsec_mindiff;
1685 dst_key_t *zone_keys[MAXZONEKEYS];
1686 unsigned int nkeys = 0;
1688 isc_stdtime_t now, inception, expire;
1690 dns_rdata_soa_t soa;
1691 dns_rdata_t rdata = DNS_RDATA_INIT;
1692 dns_rdataset_t rdataset;
1693 dns_dbnode_t *node = NULL;
1695 dns_diff_init(client->mctx, &diffnames);
1696 dns_diff_init(client->mctx, &affected);
1698 dns_diff_init(client->mctx, &sig_diff);
1699 dns_diff_init(client->mctx, &nsec_diff);
1700 dns_diff_init(client->mctx, &nsec_mindiff);
1702 result = find_zone_keys(zone, db, newver, client->mctx,
1703 MAXZONEKEYS, zone_keys, &nkeys);
1704 if (result != ISC_R_SUCCESS) {
1705 update_log(client, zone, ISC_LOG_ERROR,
1706 "could not get zone keys for secure dynamic update");
1710 isc_stdtime_get(&now);
1711 inception = now - 3600; /* Allow for some clock skew. */
1712 expire = now + sigvalidityinterval;
1715 * Get the NSEC's TTL from the SOA MINIMUM field.
1717 CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
1718 dns_rdataset_init(&rdataset);
1719 CHECK(dns_db_findrdataset(db, node, newver, dns_rdatatype_soa, 0,
1720 (isc_stdtime_t) 0, &rdataset, NULL));
1721 CHECK(dns_rdataset_first(&rdataset));
1722 dns_rdataset_current(&rdataset, &rdata);
1723 CHECK(dns_rdata_tostruct(&rdata, &soa, NULL));
1724 nsecttl = soa.minimum;
1725 dns_rdataset_disassociate(&rdataset);
1726 dns_db_detachnode(db, &node);
1729 * Find all RRsets directly affected by the update, and
1730 * update their RRSIGs. Also build a list of names affected
1731 * by the update in "diffnames".
1733 CHECK(dns_diff_sort(diff, temp_order));
1735 t = ISC_LIST_HEAD(diff->tuples);
1737 dns_name_t *name = &t->name;
1738 /* Now "name" is a new, unique name affected by the update. */
1740 CHECK(namelist_append_name(&diffnames, name));
1742 while (t != NULL && dns_name_equal(&t->name, name)) {
1743 dns_rdatatype_t type;
1744 type = t->rdata.type;
1747 * Now "name" and "type" denote a new unique RRset
1748 * affected by the update.
1751 /* Don't sign RRSIGs. */
1752 if (type == dns_rdatatype_rrsig)
1756 * Delete all old RRSIGs covering this type, since they
1757 * are all invalid when the signed RRset has changed.
1758 * We may not be able to recreate all of them - tough.
1760 CHECK(delete_if(true_p, db, newver, name,
1761 dns_rdatatype_rrsig, type,
1765 * If this RRset still exists after the update,
1766 * add a new signature for it.
1768 CHECK(rrset_exists(db, newver, name, type, 0, &flag));
1770 CHECK(add_sigs(db, newver, name, type,
1771 &sig_diff, zone_keys, nkeys,
1772 client->mctx, inception,
1776 /* Skip any other updates to the same RRset. */
1778 dns_name_equal(&t->name, name) &&
1779 t->rdata.type == type)
1781 t = ISC_LIST_NEXT(t, link);
1786 /* Remove orphaned NSECs and RRSIG NSECs. */
1787 for (t = ISC_LIST_HEAD(diffnames.tuples);
1789 t = ISC_LIST_NEXT(t, link))
1791 CHECK(non_nsec_rrset_exists(db, newver, &t->name, &flag));
1793 CHECK(delete_if(true_p, db, newver, &t->name,
1794 dns_rdatatype_any, 0,
1800 * When a name is created or deleted, its predecessor needs to
1801 * have its NSEC updated.
1803 for (t = ISC_LIST_HEAD(diffnames.tuples);
1805 t = ISC_LIST_NEXT(t, link))
1807 isc_boolean_t existed, exists;
1808 dns_fixedname_t fixedname;
1809 dns_name_t *prevname;
1811 dns_fixedname_init(&fixedname);
1812 prevname = dns_fixedname_name(&fixedname);
1814 CHECK(name_exists(db, oldver, &t->name, &existed));
1815 CHECK(name_exists(db, newver, &t->name, &exists));
1816 if (exists == existed)
1820 * Find the predecessor.
1821 * When names become obscured or unobscured in this update
1822 * transaction, we may find the wrong predecessor because
1823 * the NSECs have not yet been updated to reflect the delegation
1824 * change. This should not matter because in this case,
1825 * the correct predecessor is either the delegation node or
1826 * a newly unobscured node, and those nodes are on the
1827 * "affected" list in any case.
1829 CHECK(next_active(client, zone, db, newver,
1830 &t->name, prevname, ISC_FALSE));
1831 CHECK(namelist_append_name(&affected, prevname));
1835 * Find names potentially affected by delegation changes
1836 * (obscured by adding an NS or DNAME, or unobscured by
1839 for (t = ISC_LIST_HEAD(diffnames.tuples);
1841 t = ISC_LIST_NEXT(t, link))
1843 isc_boolean_t ns_existed, dname_existed;
1844 isc_boolean_t ns_exists, dname_exists;
1846 CHECK(rrset_exists(db, oldver, &t->name, dns_rdatatype_ns, 0,
1848 CHECK(rrset_exists(db, oldver, &t->name, dns_rdatatype_dname, 0,
1850 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ns, 0,
1852 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_dname, 0,
1854 if ((ns_exists || dname_exists) == (ns_existed || dname_existed))
1857 * There was a delegation change. Mark all subdomains
1858 * of t->name as potentially needing a NSEC update.
1860 CHECK(namelist_append_subdomain(db, &t->name, &affected));
1863 ISC_LIST_APPENDLIST(affected.tuples, diffnames.tuples, link);
1864 INSIST(ISC_LIST_EMPTY(diffnames.tuples));
1866 CHECK(uniqify_name_list(&affected));
1869 * Determine which names should have NSECs, and delete/create
1870 * NSECs to make it so. We don't know the final NSEC targets yet,
1871 * so we just create placeholder NSECs with arbitrary contents
1872 * to indicate that their respective owner names should be part of
1875 for (t = ISC_LIST_HEAD(affected.tuples);
1877 t = ISC_LIST_NEXT(t, link))
1879 isc_boolean_t exists;
1880 CHECK(name_exists(db, newver, &t->name, &exists));
1883 CHECK(is_glue(db, newver, &t->name, &flag));
1886 * This name is obscured. Delete any
1887 * existing NSEC record.
1889 CHECK(delete_if(true_p, db, newver, &t->name,
1890 dns_rdatatype_nsec, 0,
1894 * This name is not obscured. It should have a NSEC.
1896 CHECK(rrset_exists(db, newver, &t->name,
1897 dns_rdatatype_nsec, 0, &flag));
1899 CHECK(add_placeholder_nsec(db, newver, &t->name,
1905 * Now we know which names are part of the NSEC chain.
1906 * Make them all point at their correct targets.
1908 for (t = ISC_LIST_HEAD(affected.tuples);
1910 t = ISC_LIST_NEXT(t, link))
1912 CHECK(rrset_exists(db, newver, &t->name,
1913 dns_rdatatype_nsec, 0, &flag));
1916 * There is a NSEC, but we don't know if it is correct.
1917 * Delete it and create a correct one to be sure.
1918 * If the update was unnecessary, the diff minimization
1919 * will take care of eliminating it from the journal,
1922 * The RRSIG bit should always be set in the NSECs
1923 * we generate, because they will all get RRSIG NSECs.
1924 * (XXX what if the zone keys are missing?).
1925 * Because the RRSIG NSECs have not necessarily been
1926 * created yet, the correctness of the bit mask relies
1927 * on the assumption that NSECs are only created if
1928 * there is other data, and if there is other data,
1929 * there are other RRSIGs.
1931 CHECK(add_nsec(client, zone, db, newver, &t->name,
1932 nsecttl, &nsec_diff));
1937 * Minimize the set of NSEC updates so that we don't
1938 * have to regenerate the RRSIG NSECs for NSECs that were
1939 * replaced with identical ones.
1941 while ((t = ISC_LIST_HEAD(nsec_diff.tuples)) != NULL) {
1942 ISC_LIST_UNLINK(nsec_diff.tuples, t, link);
1943 dns_diff_appendminimal(&nsec_mindiff, &t);
1946 /* Update RRSIG NSECs. */
1947 for (t = ISC_LIST_HEAD(nsec_mindiff.tuples);
1949 t = ISC_LIST_NEXT(t, link))
1951 if (t->op == DNS_DIFFOP_DEL) {
1952 CHECK(delete_if(true_p, db, newver, &t->name,
1953 dns_rdatatype_rrsig, dns_rdatatype_nsec,
1955 } else if (t->op == DNS_DIFFOP_ADD) {
1956 CHECK(add_sigs(db, newver, &t->name, dns_rdatatype_nsec,
1957 &sig_diff, zone_keys, nkeys,
1958 client->mctx, inception, expire));
1964 /* Record our changes for the journal. */
1965 while ((t = ISC_LIST_HEAD(sig_diff.tuples)) != NULL) {
1966 ISC_LIST_UNLINK(sig_diff.tuples, t, link);
1967 dns_diff_appendminimal(diff, &t);
1969 while ((t = ISC_LIST_HEAD(nsec_mindiff.tuples)) != NULL) {
1970 ISC_LIST_UNLINK(nsec_mindiff.tuples, t, link);
1971 dns_diff_appendminimal(diff, &t);
1974 INSIST(ISC_LIST_EMPTY(sig_diff.tuples));
1975 INSIST(ISC_LIST_EMPTY(nsec_diff.tuples));
1976 INSIST(ISC_LIST_EMPTY(nsec_mindiff.tuples));
1979 dns_diff_clear(&sig_diff);
1980 dns_diff_clear(&nsec_diff);
1981 dns_diff_clear(&nsec_mindiff);
1983 dns_diff_clear(&affected);
1984 dns_diff_clear(&diffnames);
1986 for (i = 0; i < nkeys; i++)
1987 dst_key_free(&zone_keys[i]);
1993 /**************************************************************************/
1995 * The actual update code in all its glory. We try to follow
1996 * the RFC2136 pseudocode as closely as possible.
2000 send_update_event(ns_client_t *client, dns_zone_t *zone) {
2001 isc_result_t result = ISC_R_SUCCESS;
2002 update_event_t *event = NULL;
2003 isc_task_t *zonetask = NULL;
2004 ns_client_t *evclient;
2006 event = (update_event_t *)
2007 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
2008 update_action, NULL, sizeof(*event));
2010 FAIL(ISC_R_NOMEMORY);
2012 event->result = ISC_R_SUCCESS;
2015 ns_client_attach(client, &evclient);
2016 INSIST(client->nupdates == 0);
2018 event->ev_arg = evclient;
2020 dns_zone_gettask(zone, &zonetask);
2021 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
2025 isc_event_free(ISC_EVENT_PTR(&event));
2030 respond(ns_client_t *client, isc_result_t result) {
2031 isc_result_t msg_result;
2033 msg_result = dns_message_reply(client->message, ISC_TRUE);
2034 if (msg_result != ISC_R_SUCCESS)
2036 client->message->rcode = dns_result_torcode(result);
2038 ns_client_send(client);
2042 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
2044 "could not create update response message: %s",
2045 isc_result_totext(msg_result));
2046 ns_client_next(client, msg_result);
2050 ns_update_start(ns_client_t *client, isc_result_t sigresult) {
2051 dns_message_t *request = client->message;
2052 isc_result_t result;
2053 dns_name_t *zonename;
2054 dns_rdataset_t *zone_rdataset;
2055 dns_zone_t *zone = NULL;
2058 * Interpret the zone section.
2060 result = dns_message_firstname(request, DNS_SECTION_ZONE);
2061 if (result != ISC_R_SUCCESS)
2062 FAILC(DNS_R_FORMERR,
2063 "update zone section empty");
2066 * The zone section must contain exactly one "question", and
2067 * it must be of type SOA.
2070 dns_message_currentname(request, DNS_SECTION_ZONE, &zonename);
2071 zone_rdataset = ISC_LIST_HEAD(zonename->list);
2072 if (zone_rdataset->type != dns_rdatatype_soa)
2073 FAILC(DNS_R_FORMERR,
2074 "update zone section contains non-SOA");
2075 if (ISC_LIST_NEXT(zone_rdataset, link) != NULL)
2076 FAILC(DNS_R_FORMERR,
2077 "update zone section contains multiple RRs");
2079 /* The zone section must have exactly one name. */
2080 result = dns_message_nextname(request, DNS_SECTION_ZONE);
2081 if (result != ISC_R_NOMORE)
2082 FAILC(DNS_R_FORMERR,
2083 "update zone section contains multiple RRs");
2085 result = dns_zt_find(client->view->zonetable, zonename, 0, NULL,
2087 if (result != ISC_R_SUCCESS)
2088 FAILC(DNS_R_NOTAUTH,
2089 "not authoritative for update zone");
2091 switch(dns_zone_gettype(zone)) {
2092 case dns_zone_master:
2094 * We can now fail due to a bad signature as we now know
2095 * that we are the master.
2097 if (sigresult != ISC_R_SUCCESS)
2099 CHECK(send_update_event(client, zone));
2101 case dns_zone_slave:
2102 CHECK(checkupdateacl(client, dns_zone_getforwardacl(zone),
2103 "update forwarding", zonename, ISC_TRUE));
2104 CHECK(send_forward_event(client, zone));
2107 FAILC(DNS_R_NOTAUTH,
2108 "not authoritative for update zone");
2114 * We failed without having sent an update event to the zone.
2115 * We are still in the client task context, so we can
2116 * simply give an error response without switching tasks.
2118 respond(client, result);
2120 dns_zone_detach(&zone);
2124 * DS records are not allowed to exist without corresponding NS records,
2125 * draft-ietf-dnsext-delegation-signer-11.txt, 2.2 Protocol Change,
2126 * "DS RRsets MUST NOT appear at non-delegation points or at a zone's apex".
2130 remove_orphaned_ds(dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff) {
2131 isc_result_t result;
2132 isc_boolean_t ns_exists, ds_exists;
2135 for (t = ISC_LIST_HEAD(diff->tuples);
2137 t = ISC_LIST_NEXT(t, link)) {
2138 if (t->op != DNS_DIFFOP_DEL ||
2139 t->rdata.type != dns_rdatatype_ns)
2141 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ns, 0,
2145 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ds, 0,
2149 CHECK(delete_if(true_p, db, newver, &t->name,
2150 dns_rdatatype_ds, 0, NULL, diff));
2152 return (ISC_R_SUCCESS);
2159 update_action(isc_task_t *task, isc_event_t *event) {
2160 update_event_t *uev = (update_event_t *) event;
2161 dns_zone_t *zone = uev->zone;
2162 ns_client_t *client = (ns_client_t *)event->ev_arg;
2164 isc_result_t result;
2165 dns_db_t *db = NULL;
2166 dns_dbversion_t *oldver = NULL;
2167 dns_dbversion_t *ver = NULL;
2168 dns_diff_t diff; /* Pending updates. */
2169 dns_diff_t temp; /* Pending RR existence assertions. */
2170 isc_boolean_t soa_serial_changed = ISC_FALSE;
2171 isc_mem_t *mctx = client->mctx;
2172 dns_rdatatype_t covers;
2173 dns_message_t *request = client->message;
2174 dns_rdataclass_t zoneclass;
2175 dns_name_t *zonename;
2176 dns_ssutable_t *ssutable = NULL;
2177 dns_fixedname_t tmpnamefixed;
2178 dns_name_t *tmpname = NULL;
2180 INSIST(event->ev_type == DNS_EVENT_UPDATE);
2182 dns_diff_init(mctx, &diff);
2183 dns_diff_init(mctx, &temp);
2185 CHECK(dns_zone_getdb(zone, &db));
2186 zonename = dns_db_origin(db);
2187 zoneclass = dns_db_class(db);
2188 dns_zone_getssutable(zone, &ssutable);
2189 dns_db_currentversion(db, &oldver);
2190 CHECK(dns_db_newversion(db, &ver));
2193 * Check prerequisites.
2196 for (result = dns_message_firstname(request, DNS_SECTION_PREREQUISITE);
2197 result == ISC_R_SUCCESS;
2198 result = dns_message_nextname(request, DNS_SECTION_PREREQUISITE))
2200 dns_name_t *name = NULL;
2201 dns_rdata_t rdata = DNS_RDATA_INIT;
2203 dns_rdataclass_t update_class;
2206 get_current_rr(request, DNS_SECTION_PREREQUISITE, zoneclass,
2207 &name, &rdata, &covers, &ttl, &update_class);
2210 FAILC(DNS_R_FORMERR, "prerequisite TTL is not zero");
2212 if (! dns_name_issubdomain(name, zonename))
2213 FAILN(DNS_R_NOTZONE, name,
2214 "prerequisite name is out of zone");
2216 if (update_class == dns_rdataclass_any) {
2217 if (rdata.length != 0)
2218 FAILC(DNS_R_FORMERR,
2219 "class ANY prerequisite "
2220 "RDATA is not empty");
2221 if (rdata.type == dns_rdatatype_any) {
2222 CHECK(name_exists(db, ver, name, &flag));
2224 FAILN(DNS_R_NXDOMAIN, name,
2225 "'name in use' prerequisite "
2229 CHECK(rrset_exists(db, ver, name,
2230 rdata.type, covers, &flag));
2232 /* RRset does not exist. */
2233 FAILNT(DNS_R_NXRRSET, name, rdata.type,
2234 "'rrset exists (value independent)' "
2235 "prerequisite not satisfied");
2238 } else if (update_class == dns_rdataclass_none) {
2239 if (rdata.length != 0)
2240 FAILC(DNS_R_FORMERR,
2241 "class NONE prerequisite "
2242 "RDATA is not empty");
2243 if (rdata.type == dns_rdatatype_any) {
2244 CHECK(name_exists(db, ver, name, &flag));
2246 FAILN(DNS_R_YXDOMAIN, name,
2247 "'name not in use' prerequisite "
2251 CHECK(rrset_exists(db, ver, name,
2252 rdata.type, covers, &flag));
2255 FAILNT(DNS_R_YXRRSET, name, rdata.type,
2256 "'rrset does not exist' "
2257 "prerequisite not satisfied");
2260 } else if (update_class == zoneclass) {
2261 /* "temp<rr.name, rr.type> += rr;" */
2262 result = temp_append(&temp, name, &rdata);
2263 if (result != ISC_R_SUCCESS) {
2264 UNEXPECTED_ERROR(__FILE__, __LINE__,
2265 "temp entry creation failed: %s",
2266 dns_result_totext(result));
2267 FAIL(ISC_R_UNEXPECTED);
2270 FAILC(DNS_R_FORMERR, "malformed prerequisite");
2273 if (result != ISC_R_NOMORE)
2278 * Perform the final check of the "rrset exists (value dependent)"
2281 if (ISC_LIST_HEAD(temp.tuples) != NULL) {
2282 dns_rdatatype_t type;
2285 * Sort the prerequisite records by owner name,
2288 result = dns_diff_sort(&temp, temp_order);
2289 if (result != ISC_R_SUCCESS)
2290 FAILC(result, "'RRset exists (value dependent)' "
2291 "prerequisite not satisfied");
2293 dns_fixedname_init(&tmpnamefixed);
2294 tmpname = dns_fixedname_name(&tmpnamefixed);
2295 result = temp_check(mctx, &temp, db, ver, tmpname, &type);
2296 if (result != ISC_R_SUCCESS)
2297 FAILNT(result, tmpname, type,
2298 "'RRset exists (value dependent)' "
2299 "prerequisite not satisfied");
2302 update_log(client, zone, LOGLEVEL_DEBUG,
2303 "prerequisites are OK");
2306 * Check Requestor's Permissions. It seems a bit silly to do this
2307 * only after prerequisite testing, but that is what RFC2136 says.
2309 result = ISC_R_SUCCESS;
2310 if (ssutable == NULL)
2311 CHECK(checkupdateacl(client, dns_zone_getupdateacl(zone),
2312 "update", zonename, ISC_FALSE));
2313 else if (client->signer == NULL)
2314 CHECK(checkupdateacl(client, NULL, "update", zonename,
2317 if (dns_zone_getupdatedisabled(zone))
2318 FAILC(DNS_R_REFUSED, "dynamic update temporarily disabled");
2321 * Perform the Update Section Prescan.
2324 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2325 result == ISC_R_SUCCESS;
2326 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2328 dns_name_t *name = NULL;
2329 dns_rdata_t rdata = DNS_RDATA_INIT;
2331 dns_rdataclass_t update_class;
2332 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2333 &name, &rdata, &covers, &ttl, &update_class);
2335 if (! dns_name_issubdomain(name, zonename))
2336 FAILC(DNS_R_NOTZONE,
2337 "update RR is outside zone");
2338 if (update_class == zoneclass) {
2340 * Check for meta-RRs. The RFC2136 pseudocode says
2341 * check for ANY|AXFR|MAILA|MAILB, but the text adds
2342 * "or any other QUERY metatype"
2344 if (dns_rdatatype_ismeta(rdata.type)) {
2345 FAILC(DNS_R_FORMERR,
2346 "meta-RR in update");
2348 result = dns_zone_checknames(zone, name, &rdata);
2349 if (result != ISC_R_SUCCESS)
2350 FAIL(DNS_R_REFUSED);
2351 } else if (update_class == dns_rdataclass_any) {
2352 if (ttl != 0 || rdata.length != 0 ||
2353 (dns_rdatatype_ismeta(rdata.type) &&
2354 rdata.type != dns_rdatatype_any))
2355 FAILC(DNS_R_FORMERR,
2356 "meta-RR in update");
2357 } else if (update_class == dns_rdataclass_none) {
2359 dns_rdatatype_ismeta(rdata.type))
2360 FAILC(DNS_R_FORMERR,
2361 "meta-RR in update");
2363 update_log(client, zone, ISC_LOG_WARNING,
2364 "update RR has incorrect class %d",
2366 FAIL(DNS_R_FORMERR);
2369 * draft-ietf-dnsind-simple-secure-update-01 says
2370 * "Unlike traditional dynamic update, the client
2371 * is forbidden from updating NSEC records."
2373 if (dns_db_issecure(db)) {
2374 if (rdata.type == dns_rdatatype_nsec) {
2375 FAILC(DNS_R_REFUSED,
2376 "explicit NSEC updates are not allowed "
2379 else if (rdata.type == dns_rdatatype_rrsig) {
2380 FAILC(DNS_R_REFUSED,
2381 "explicit RRSIG updates are currently not "
2382 "supported in secure zones");
2386 if (ssutable != NULL && client->signer != NULL) {
2387 if (rdata.type != dns_rdatatype_any) {
2388 if (!dns_ssutable_checkrules(ssutable,
2391 FAILC(DNS_R_REFUSED,
2392 "rejected by secure update");
2395 if (!ssu_checkall(db, ver, name, ssutable,
2397 FAILC(DNS_R_REFUSED,
2398 "rejected by secure update");
2402 if (result != ISC_R_NOMORE)
2405 update_log(client, zone, LOGLEVEL_DEBUG,
2406 "update section prescan OK");
2409 * Process the Update Section.
2412 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2413 result == ISC_R_SUCCESS;
2414 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2416 dns_name_t *name = NULL;
2417 dns_rdata_t rdata = DNS_RDATA_INIT;
2419 dns_rdataclass_t update_class;
2422 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2423 &name, &rdata, &covers, &ttl, &update_class);
2425 if (update_class == zoneclass) {
2428 * RFC 1123 doesn't allow MF and MD in master zones. */
2429 if (rdata.type == dns_rdatatype_md ||
2430 rdata.type == dns_rdatatype_mf) {
2431 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2433 dns_rdatatype_format(rdata.type, typebuf,
2435 update_log(client, zone, LOGLEVEL_PROTOCOL,
2436 "attempt to add %s ignored",
2440 if (rdata.type == dns_rdatatype_ns &&
2441 dns_name_iswildcard(name)) {
2442 update_log(client, zone,
2444 "attempt to add wildcard NS record"
2448 if (rdata.type == dns_rdatatype_cname) {
2449 CHECK(cname_incompatible_rrset_exists(db, ver,
2453 update_log(client, zone,
2455 "attempt to add CNAME "
2456 "alongside non-CNAME "
2461 CHECK(rrset_exists(db, ver, name,
2462 dns_rdatatype_cname, 0,
2465 ! dns_rdatatype_isdnssec(rdata.type))
2467 update_log(client, zone,
2469 "attempt to add non-CNAME "
2470 "alongside CNAME ignored");
2474 if (rdata.type == dns_rdatatype_soa) {
2476 CHECK(rrset_exists(db, ver, name,
2477 dns_rdatatype_soa, 0,
2480 update_log(client, zone,
2482 "attempt to create 2nd "
2486 CHECK(check_soa_increment(db, ver, &rdata,
2489 update_log(client, zone,
2491 "SOA update failed to "
2492 "increment serial, "
2496 soa_serial_changed = ISC_TRUE;
2499 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) {
2500 char namestr[DNS_NAME_FORMATSIZE];
2501 char typestr[DNS_RDATATYPE_FORMATSIZE];
2502 dns_name_format(name, namestr,
2504 dns_rdatatype_format(rdata.type, typestr,
2506 update_log(client, zone,
2508 "adding an RR at '%s' %s",
2512 /* Prepare the affected RRset for the addition. */
2514 add_rr_prepare_ctx_t ctx;
2519 ctx.update_rr = &rdata;
2520 ctx.update_rr_ttl = ttl;
2521 ctx.ignore_add = ISC_FALSE;
2522 dns_diff_init(mctx, &ctx.del_diff);
2523 dns_diff_init(mctx, &ctx.add_diff);
2524 CHECK(foreach_rr(db, ver, name, rdata.type,
2525 covers, add_rr_prepare_action,
2528 if (ctx.ignore_add) {
2529 dns_diff_clear(&ctx.del_diff);
2530 dns_diff_clear(&ctx.add_diff);
2532 CHECK(do_diff(&ctx.del_diff, db, ver, &diff));
2533 CHECK(do_diff(&ctx.add_diff, db, ver, &diff));
2534 CHECK(update_one_rr(db, ver, &diff,
2536 name, ttl, &rdata));
2539 } else if (update_class == dns_rdataclass_any) {
2540 if (rdata.type == dns_rdatatype_any) {
2541 if (isc_log_wouldlog(ns_g_lctx,
2544 char namestr[DNS_NAME_FORMATSIZE];
2545 dns_name_format(name, namestr,
2547 update_log(client, zone,
2549 "delete all rrsets from "
2550 "name '%s'", namestr);
2552 if (dns_name_equal(name, zonename)) {
2553 CHECK(delete_if(type_not_soa_nor_ns_p,
2555 dns_rdatatype_any, 0,
2558 CHECK(delete_if(type_not_dnssec,
2560 dns_rdatatype_any, 0,
2563 } else if (dns_name_equal(name, zonename) &&
2564 (rdata.type == dns_rdatatype_soa ||
2565 rdata.type == dns_rdatatype_ns)) {
2566 update_log(client, zone,
2568 "attempt to delete all SOA "
2569 "or NS records ignored");
2572 if (isc_log_wouldlog(ns_g_lctx,
2575 char namestr[DNS_NAME_FORMATSIZE];
2576 char typestr[DNS_RDATATYPE_FORMATSIZE];
2577 dns_name_format(name, namestr,
2579 dns_rdatatype_format(rdata.type,
2582 update_log(client, zone,
2584 "deleting rrset at '%s' %s",
2587 CHECK(delete_if(true_p, db, ver, name,
2588 rdata.type, covers, &rdata,
2591 } else if (update_class == dns_rdataclass_none) {
2593 * The (name == zonename) condition appears in
2594 * RFC2136 3.4.2.4 but is missing from the pseudocode.
2596 if (dns_name_equal(name, zonename)) {
2597 if (rdata.type == dns_rdatatype_soa) {
2598 update_log(client, zone,
2600 "attempt to delete SOA "
2604 if (rdata.type == dns_rdatatype_ns) {
2606 CHECK(rr_count(db, ver, name,
2610 update_log(client, zone,
2619 update_log(client, zone,
2622 CHECK(delete_if(rr_equal_p, db, ver, name,
2623 rdata.type, covers, &rdata, &diff));
2626 if (result != ISC_R_NOMORE)
2630 * If any changes were made, increment the SOA serial number,
2631 * update RRSIGs and NSECs (if zone is secure), and write the update
2634 if (! ISC_LIST_EMPTY(diff.tuples)) {
2636 dns_journal_t *journal;
2639 * Increment the SOA serial, but only if it was not
2640 * changed as a result of an update operation.
2642 if (! soa_serial_changed) {
2643 CHECK(increment_soa_serial(db, ver, &diff, mctx));
2646 CHECK(remove_orphaned_ds(db, ver, &diff));
2648 if (dns_db_issecure(db)) {
2649 result = update_signatures(client, zone, db, oldver,
2651 dns_zone_getsigvalidityinterval(zone));
2652 if (result != ISC_R_SUCCESS) {
2653 update_log(client, zone,
2655 "RRSIG/NSEC update failed: %s",
2656 isc_result_totext(result));
2661 journalfile = dns_zone_getjournal(zone);
2662 if (journalfile != NULL) {
2663 update_log(client, zone, LOGLEVEL_DEBUG,
2664 "writing journal %s", journalfile);
2667 result = dns_journal_open(mctx, journalfile,
2668 ISC_TRUE, &journal);
2669 if (result != ISC_R_SUCCESS)
2670 FAILS(result, "journal open failed");
2672 result = dns_journal_write_transaction(journal, &diff);
2673 if (result != ISC_R_SUCCESS) {
2674 dns_journal_destroy(&journal);
2675 FAILS(result, "journal write failed");
2678 dns_journal_destroy(&journal);
2682 * XXXRTH Just a note that this committing code will have
2683 * to change to handle databases that need two-phase
2684 * commit, but this isn't a priority.
2686 update_log(client, zone, LOGLEVEL_DEBUG,
2687 "committing update transaction");
2688 dns_db_closeversion(db, &ver, ISC_TRUE);
2691 * Mark the zone as dirty so that it will be written to disk.
2693 dns_zone_markdirty(zone);
2696 * Notify slaves of the change we just made.
2698 dns_zone_notify(zone);
2700 update_log(client, zone, LOGLEVEL_DEBUG, "redundant request");
2701 dns_db_closeversion(db, &ver, ISC_TRUE);
2703 result = ISC_R_SUCCESS;
2708 * The reason for failure should have been logged at this point.
2711 update_log(client, zone, LOGLEVEL_DEBUG,
2713 dns_db_closeversion(db, &ver, ISC_FALSE);
2717 dns_diff_clear(&temp);
2718 dns_diff_clear(&diff);
2721 dns_db_closeversion(db, &oldver, ISC_FALSE);
2726 if (ssutable != NULL)
2727 dns_ssutable_detach(&ssutable);
2730 dns_zone_detach(&zone);
2732 isc_task_detach(&task);
2733 uev->result = result;
2734 uev->ev_type = DNS_EVENT_UPDATEDONE;
2735 uev->ev_action = updatedone_action;
2736 isc_task_send(client->task, &event);
2737 INSIST(event == NULL);
2741 updatedone_action(isc_task_t *task, isc_event_t *event) {
2742 update_event_t *uev = (update_event_t *) event;
2743 ns_client_t *client = (ns_client_t *) event->ev_arg;
2747 INSIST(event->ev_type == DNS_EVENT_UPDATEDONE);
2748 INSIST(task == client->task);
2750 INSIST(client->nupdates > 0);
2752 respond(client, uev->result);
2753 isc_event_free(&event);
2754 ns_client_detach(&client);
2758 * Update forwarding support.
2762 forward_fail(isc_task_t *task, isc_event_t *event) {
2763 ns_client_t *client = (ns_client_t *)event->ev_arg;
2767 INSIST(client->nupdates > 0);
2769 respond(client, DNS_R_SERVFAIL);
2770 isc_event_free(&event);
2771 ns_client_detach(&client);
2776 forward_callback(void *arg, isc_result_t result, dns_message_t *answer) {
2777 update_event_t *uev = arg;
2778 ns_client_t *client = uev->ev_arg;
2780 if (result != ISC_R_SUCCESS) {
2781 INSIST(answer == NULL);
2782 uev->ev_type = DNS_EVENT_UPDATEDONE;
2783 uev->ev_action = forward_fail;
2785 uev->ev_type = DNS_EVENT_UPDATEDONE;
2786 uev->ev_action = forward_done;
2787 uev->answer = answer;
2789 isc_task_send(client->task, ISC_EVENT_PTR(&uev));
2793 forward_done(isc_task_t *task, isc_event_t *event) {
2794 update_event_t *uev = (update_event_t *) event;
2795 ns_client_t *client = (ns_client_t *)event->ev_arg;
2799 INSIST(client->nupdates > 0);
2801 ns_client_sendraw(client, uev->answer);
2802 dns_message_destroy(&uev->answer);
2803 isc_event_free(&event);
2804 ns_client_detach(&client);
2808 forward_action(isc_task_t *task, isc_event_t *event) {
2809 update_event_t *uev = (update_event_t *) event;
2810 dns_zone_t *zone = uev->zone;
2811 ns_client_t *client = (ns_client_t *)event->ev_arg;
2812 isc_result_t result;
2814 result = dns_zone_forwardupdate(zone, client->message,
2815 forward_callback, event);
2816 if (result != ISC_R_SUCCESS) {
2817 uev->ev_type = DNS_EVENT_UPDATEDONE;
2818 uev->ev_action = forward_fail;
2819 isc_task_send(client->task, &event);
2821 dns_zone_detach(&zone);
2822 isc_task_detach(&task);
2826 send_forward_event(ns_client_t *client, dns_zone_t *zone) {
2827 isc_result_t result = ISC_R_SUCCESS;
2828 update_event_t *event = NULL;
2829 isc_task_t *zonetask = NULL;
2830 ns_client_t *evclient;
2832 event = (update_event_t *)
2833 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
2834 forward_action, NULL, sizeof(*event));
2836 FAIL(ISC_R_NOMEMORY);
2838 event->result = ISC_R_SUCCESS;
2841 ns_client_attach(client, &evclient);
2842 INSIST(client->nupdates == 0);
2844 event->ev_arg = evclient;
2846 dns_zone_gettask(zone, &zonetask);
2847 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
2851 isc_event_free(ISC_EVENT_PTR(&event));