2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.27 2005/10/08 00:21:06 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/message.h>
36 #include <dns/rdataclass.h>
37 #include <dns/rdataset.h>
38 #include <dns/rdatasetiter.h>
39 #include <dns/rdatatype.h>
46 #include <named/client.h>
47 #include <named/log.h>
48 #include <named/update.h>
51 * This module implements dynamic update as in RFC2136.
56 - document strict minimality
59 /**************************************************************************/
62 * Log level for tracing dynamic update protocol requests.
64 #define LOGLEVEL_PROTOCOL ISC_LOG_INFO
67 * Log level for low-level debug tracing.
69 #define LOGLEVEL_DEBUG ISC_LOG_DEBUG(8)
72 * Check an operation for failure. These macros all assume that
73 * the function using them has a 'result' variable and a 'failure'
78 if (result != ISC_R_SUCCESS) goto failure; \
82 * Fail unconditionally with result 'code', which must not
83 * be ISC_R_SUCCESS. The reason for failure presumably has
84 * been logged already.
86 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
87 * from complaining about "end-of-loop code not reached".
93 if (result != ISC_R_SUCCESS) goto failure; \
97 * Fail unconditionally and log as a client error.
98 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
99 * from complaining about "end-of-loop code not reached".
101 #define FAILC(code, msg) \
103 const char *_what = "failed"; \
106 case DNS_R_NXDOMAIN: \
107 case DNS_R_YXDOMAIN: \
108 case DNS_R_YXRRSET: \
109 case DNS_R_NXRRSET: \
110 _what = "unsuccessful"; \
112 update_log(client, zone, LOGLEVEL_PROTOCOL, \
113 "update %s: %s (%s)", _what, \
114 msg, isc_result_totext(result)); \
115 if (result != ISC_R_SUCCESS) goto failure; \
118 #define FAILN(code, name, msg) \
120 const char *_what = "failed"; \
123 case DNS_R_NXDOMAIN: \
124 case DNS_R_YXDOMAIN: \
125 case DNS_R_YXRRSET: \
126 case DNS_R_NXRRSET: \
127 _what = "unsuccessful"; \
129 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
130 char _nbuf[DNS_NAME_FORMATSIZE]; \
131 dns_name_format(name, _nbuf, sizeof(_nbuf)); \
132 update_log(client, zone, LOGLEVEL_PROTOCOL, \
133 "update %s: %s: %s (%s)", _what, _nbuf, \
134 msg, isc_result_totext(result)); \
136 if (result != ISC_R_SUCCESS) goto failure; \
139 #define FAILNT(code, name, type, msg) \
141 const char *_what = "failed"; \
144 case DNS_R_NXDOMAIN: \
145 case DNS_R_YXDOMAIN: \
146 case DNS_R_YXRRSET: \
147 case DNS_R_NXRRSET: \
148 _what = "unsuccessful"; \
150 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
151 char _nbuf[DNS_NAME_FORMATSIZE]; \
152 char _tbuf[DNS_RDATATYPE_FORMATSIZE]; \
153 dns_name_format(name, _nbuf, sizeof(_nbuf)); \
154 dns_rdatatype_format(type, _tbuf, sizeof(_tbuf)); \
155 update_log(client, zone, LOGLEVEL_PROTOCOL, \
156 "update %s: %s/%s: %s (%s)", \
157 _what, _nbuf, _tbuf, msg, \
158 isc_result_totext(result)); \
160 if (result != ISC_R_SUCCESS) goto failure; \
163 * Fail unconditionally and log as a server error.
164 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
165 * from complaining about "end-of-loop code not reached".
167 #define FAILS(code, msg) \
170 update_log(client, zone, LOGLEVEL_PROTOCOL, \
172 msg, isc_result_totext(result)); \
173 if (result != ISC_R_SUCCESS) goto failure; \
176 /**************************************************************************/
178 typedef struct rr rr_t;
181 /* dns_name_t name; */
186 typedef struct update_event update_event_t;
188 struct update_event {
189 ISC_EVENT_COMMON(update_event_t);
192 dns_message_t *answer;
195 /**************************************************************************/
197 * Forward declarations.
200 static void update_action(isc_task_t *task, isc_event_t *event);
201 static void updatedone_action(isc_task_t *task, isc_event_t *event);
202 static isc_result_t send_forward_event(ns_client_t *client, dns_zone_t *zone);
203 static void forward_done(isc_task_t *task, isc_event_t *event);
205 /**************************************************************************/
208 update_log(ns_client_t *client, dns_zone_t *zone,
209 int level, const char *fmt, ...) ISC_FORMAT_PRINTF(4, 5);
212 update_log(ns_client_t *client, dns_zone_t *zone,
213 int level, const char *fmt, ...)
217 char namebuf[DNS_NAME_FORMATSIZE];
218 char classbuf[DNS_RDATACLASS_FORMATSIZE];
220 if (client == NULL || zone == NULL)
223 if (isc_log_wouldlog(ns_g_lctx, level) == ISC_FALSE)
226 dns_name_format(dns_zone_getorigin(zone), namebuf,
228 dns_rdataclass_format(dns_zone_getclass(zone), classbuf,
232 vsnprintf(message, sizeof(message), fmt, ap);
235 ns_client_log(client, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
236 level, "updating zone '%s/%s': %s",
237 namebuf, classbuf, message);
241 checkupdateacl(ns_client_t *client, dns_acl_t *acl, const char *message,
242 dns_name_t *zonename, isc_boolean_t slave)
244 char namebuf[DNS_NAME_FORMATSIZE];
245 char classbuf[DNS_RDATACLASS_FORMATSIZE];
246 int level = ISC_LOG_ERROR;
247 const char *msg = "denied";
250 if (slave && acl == NULL) {
251 result = DNS_R_NOTIMP;
252 level = ISC_LOG_DEBUG(3);
255 result = ns_client_checkaclsilent(client, acl, ISC_FALSE);
257 if (result == ISC_R_SUCCESS) {
258 level = ISC_LOG_DEBUG(3);
262 dns_name_format(zonename, namebuf, sizeof(namebuf));
263 dns_rdataclass_format(client->view->rdclass, classbuf,
266 ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
267 NS_LOGMODULE_UPDATE, level, "%s '%s/%s' %s",
268 message, namebuf, classbuf, msg);
273 * Update a single RR in version 'ver' of 'db' and log the
277 * '*tuple' == NULL. Either the tuple is freed, or its
278 * ownership has been transferred to the diff.
281 do_one_tuple(dns_difftuple_t **tuple,
282 dns_db_t *db, dns_dbversion_t *ver,
285 dns_diff_t temp_diff;
289 * Create a singleton diff.
291 dns_diff_init(diff->mctx, &temp_diff);
292 ISC_LIST_APPEND(temp_diff.tuples, *tuple, link);
295 * Apply it to the database.
297 result = dns_diff_apply(&temp_diff, db, ver);
298 ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link);
299 if (result != ISC_R_SUCCESS) {
300 dns_difftuple_free(tuple);
305 * Merge it into the current pending journal entry.
307 dns_diff_appendminimal(diff, tuple);
310 * Do not clear temp_diff.
312 return (ISC_R_SUCCESS);
316 * Perform the updates in 'updates' in version 'ver' of 'db' and log the
320 * 'updates' is empty.
323 do_diff(dns_diff_t *updates, dns_db_t *db, dns_dbversion_t *ver,
327 while (! ISC_LIST_EMPTY(updates->tuples)) {
328 dns_difftuple_t *t = ISC_LIST_HEAD(updates->tuples);
329 ISC_LIST_UNLINK(updates->tuples, t, link);
330 CHECK(do_one_tuple(&t, db, ver, diff));
332 return (ISC_R_SUCCESS);
335 dns_diff_clear(diff);
340 update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
341 dns_diffop_t op, dns_name_t *name,
342 dns_ttl_t ttl, dns_rdata_t *rdata)
344 dns_difftuple_t *tuple = NULL;
346 result = dns_difftuple_create(diff->mctx, op,
347 name, ttl, rdata, &tuple);
348 if (result != ISC_R_SUCCESS)
350 return (do_one_tuple(&tuple, db, ver, diff));
353 /**************************************************************************/
355 * Callback-style iteration over rdatasets and rdatas.
357 * foreach_rrset() can be used to iterate over the RRsets
358 * of a name and call a callback function with each
359 * one. Similarly, foreach_rr() can be used to iterate
360 * over the individual RRs at name, optionally restricted
361 * to RRs of a given type.
363 * The callback functions are called "actions" and take
364 * two arguments: a void pointer for passing arbitrary
365 * context information, and a pointer to the current RRset
366 * or RR. By convention, their names end in "_action".
370 * XXXRTH We might want to make this public somewhere in libdns.
374 * Function type for foreach_rrset() iterator actions.
376 typedef isc_result_t rrset_func(void *data, dns_rdataset_t *rrset);
379 * Function type for foreach_rr() iterator actions.
381 typedef isc_result_t rr_func(void *data, rr_t *rr);
384 * Internal context struct for foreach_node_rr().
388 void * rr_action_data;
389 } foreach_node_rr_ctx_t;
392 * Internal helper function for foreach_node_rr().
395 foreach_node_rr_action(void *data, dns_rdataset_t *rdataset) {
397 foreach_node_rr_ctx_t *ctx = data;
398 for (result = dns_rdataset_first(rdataset);
399 result == ISC_R_SUCCESS;
400 result = dns_rdataset_next(rdataset))
402 rr_t rr = { 0, DNS_RDATA_INIT };
404 dns_rdataset_current(rdataset, &rr.rdata);
405 rr.ttl = rdataset->ttl;
406 result = (*ctx->rr_action)(ctx->rr_action_data, &rr);
407 if (result != ISC_R_SUCCESS)
410 if (result != ISC_R_NOMORE)
412 return (ISC_R_SUCCESS);
416 * For each rdataset of 'name' in 'ver' of 'db', call 'action'
417 * with the rdataset and 'action_data' as arguments. If the name
418 * does not exist, do nothing.
420 * If 'action' returns an error, abort iteration and return the error.
423 foreach_rrset(dns_db_t *db,
424 dns_dbversion_t *ver,
431 dns_rdatasetiter_t *iter;
434 result = dns_db_findnode(db, name, ISC_FALSE, &node);
435 if (result == ISC_R_NOTFOUND)
436 return (ISC_R_SUCCESS);
437 if (result != ISC_R_SUCCESS)
441 result = dns_db_allrdatasets(db, node, ver,
442 (isc_stdtime_t) 0, &iter);
443 if (result != ISC_R_SUCCESS)
446 for (result = dns_rdatasetiter_first(iter);
447 result == ISC_R_SUCCESS;
448 result = dns_rdatasetiter_next(iter))
450 dns_rdataset_t rdataset;
452 dns_rdataset_init(&rdataset);
453 dns_rdatasetiter_current(iter, &rdataset);
455 result = (*action)(action_data, &rdataset);
457 dns_rdataset_disassociate(&rdataset);
458 if (result != ISC_R_SUCCESS)
459 goto cleanup_iterator;
461 if (result == ISC_R_NOMORE)
462 result = ISC_R_SUCCESS;
465 dns_rdatasetiter_destroy(&iter);
468 dns_db_detachnode(db, &node);
474 * For each RR of 'name' in 'ver' of 'db', call 'action'
475 * with the RR and 'action_data' as arguments. If the name
476 * does not exist, do nothing.
478 * If 'action' returns an error, abort iteration
479 * and return the error.
482 foreach_node_rr(dns_db_t *db,
483 dns_dbversion_t *ver,
486 void *rr_action_data)
488 foreach_node_rr_ctx_t ctx;
489 ctx.rr_action = rr_action;
490 ctx.rr_action_data = rr_action_data;
491 return (foreach_rrset(db, ver, name,
492 foreach_node_rr_action, &ctx));
497 * For each of the RRs specified by 'db', 'ver', 'name', 'type',
498 * (which can be dns_rdatatype_any to match any type), and 'covers', call
499 * 'action' with the RR and 'action_data' as arguments. If the name
500 * does not exist, or if no RRset of the given type exists at the name,
503 * If 'action' returns an error, abort iteration and return the error.
506 foreach_rr(dns_db_t *db,
507 dns_dbversion_t *ver,
509 dns_rdatatype_t type,
510 dns_rdatatype_t covers,
512 void *rr_action_data)
517 dns_rdataset_t rdataset;
519 if (type == dns_rdatatype_any)
520 return (foreach_node_rr(db, ver, name,
521 rr_action, rr_action_data));
524 result = dns_db_findnode(db, name, ISC_FALSE, &node);
525 if (result == ISC_R_NOTFOUND)
526 return (ISC_R_SUCCESS);
527 if (result != ISC_R_SUCCESS)
530 dns_rdataset_init(&rdataset);
531 result = dns_db_findrdataset(db, node, ver, type, covers,
532 (isc_stdtime_t) 0, &rdataset, NULL);
533 if (result == ISC_R_NOTFOUND) {
534 result = ISC_R_SUCCESS;
537 if (result != ISC_R_SUCCESS)
540 for (result = dns_rdataset_first(&rdataset);
541 result == ISC_R_SUCCESS;
542 result = dns_rdataset_next(&rdataset))
544 rr_t rr = { 0, DNS_RDATA_INIT };
545 dns_rdataset_current(&rdataset, &rr.rdata);
546 rr.ttl = rdataset.ttl;
547 result = (*rr_action)(rr_action_data, &rr);
548 if (result != ISC_R_SUCCESS)
549 goto cleanup_rdataset;
551 if (result != ISC_R_NOMORE)
552 goto cleanup_rdataset;
553 result = ISC_R_SUCCESS;
556 dns_rdataset_disassociate(&rdataset);
558 dns_db_detachnode(db, &node);
563 /**************************************************************************/
565 * Various tests on the database contents (for prerequisites, etc).
569 * Function type for predicate functions that compare a database RR 'db_rr'
570 * against an update RR 'update_rr'.
572 typedef isc_boolean_t rr_predicate(dns_rdata_t *update_rr, dns_rdata_t *db_rr);
575 * Helper function for rrset_exists().
578 rrset_exists_action(void *data, rr_t *rr) {
581 return (ISC_R_EXISTS);
585 * Utility macro for RR existence checking functions.
587 * If the variable 'result' has the value ISC_R_EXISTS or
588 * ISC_R_SUCCESS, set *exists to ISC_TRUE or ISC_FALSE,
589 * respectively, and return success.
591 * If 'result' has any other value, there was a failure.
592 * Return the failure result code and do not set *exists.
594 * This would be more readable as "do { if ... } while(0)",
595 * but that form generates tons of warnings on Solaris 2.6.
597 #define RETURN_EXISTENCE_FLAG \
598 return ((result == ISC_R_EXISTS) ? \
599 (*exists = ISC_TRUE, ISC_R_SUCCESS) : \
600 ((result == ISC_R_SUCCESS) ? \
601 (*exists = ISC_FALSE, ISC_R_SUCCESS) : \
605 * Set '*exists' to true iff an rrset of the given type exists,
606 * to false otherwise.
609 rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
610 dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers,
611 isc_boolean_t *exists)
614 result = foreach_rr(db, ver, name, type, covers,
615 rrset_exists_action, NULL);
616 RETURN_EXISTENCE_FLAG;
620 * Helper function for cname_incompatible_rrset_exists.
623 cname_compatibility_action(void *data, dns_rdataset_t *rrset) {
625 if (rrset->type != dns_rdatatype_cname &&
626 ! dns_rdatatype_isdnssec(rrset->type))
627 return (ISC_R_EXISTS);
628 return (ISC_R_SUCCESS);
632 * Check whether there is an rrset incompatible with adding a CNAME RR,
633 * i.e., anything but another CNAME (which can be replaced) or a
634 * DNSSEC RR (which can coexist).
636 * If such an incompatible rrset exists, set '*exists' to ISC_TRUE.
637 * Otherwise, set it to ISC_FALSE.
640 cname_incompatible_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
641 dns_name_t *name, isc_boolean_t *exists) {
643 result = foreach_rrset(db, ver, name,
644 cname_compatibility_action, NULL);
645 RETURN_EXISTENCE_FLAG;
649 * Helper function for rr_count().
652 count_rr_action(void *data, rr_t *rr) {
656 return (ISC_R_SUCCESS);
660 * Count the number of RRs of 'type' belonging to 'name' in 'ver' of 'db'.
663 rr_count(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
664 dns_rdatatype_t type, dns_rdatatype_t covers, int *countp)
667 return (foreach_rr(db, ver, name, type, covers,
668 count_rr_action, countp));
672 * Context struct and helper function for name_exists().
676 name_exists_action(void *data, dns_rdataset_t *rrset) {
679 return (ISC_R_EXISTS);
683 * Set '*exists' to true iff the given name exists, to false otherwise.
686 name_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
687 isc_boolean_t *exists)
690 result = foreach_rrset(db, ver, name,
691 name_exists_action, NULL);
692 RETURN_EXISTENCE_FLAG;
696 dns_name_t *name, *signer;
697 dns_ssutable_t *table;
701 ssu_checkrule(void *data, dns_rdataset_t *rrset) {
702 ssu_check_t *ssuinfo = data;
703 isc_boolean_t result;
706 * If we're deleting all records, it's ok to delete RRSIG and NSEC even
707 * if we're normally not allowed to.
709 if (rrset->type == dns_rdatatype_rrsig ||
710 rrset->type == dns_rdatatype_nsec)
711 return (ISC_R_SUCCESS);
712 result = dns_ssutable_checkrules(ssuinfo->table, ssuinfo->signer,
713 ssuinfo->name, rrset->type);
714 return (result == ISC_TRUE ? ISC_R_SUCCESS : ISC_R_FAILURE);
718 ssu_checkall(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
719 dns_ssutable_t *ssutable, dns_name_t *signer)
725 ssuinfo.table = ssutable;
726 ssuinfo.signer = signer;
727 result = foreach_rrset(db, ver, name, ssu_checkrule, &ssuinfo);
728 return (ISC_TF(result == ISC_R_SUCCESS));
731 /**************************************************************************/
733 * Checking of "RRset exists (value dependent)" prerequisites.
735 * In the RFC2136 section 3.2.5, this is the pseudocode involving
736 * a variable called "temp", a mapping of <name, type> tuples to rrsets.
738 * Here, we represent the "temp" data structure as (non-minimial) "dns_diff_t"
739 * where each typle has op==DNS_DIFFOP_EXISTS.
744 * Append a tuple asserting the existence of the RR with
745 * 'name' and 'rdata' to 'diff'.
748 temp_append(dns_diff_t *diff, dns_name_t *name, dns_rdata_t *rdata) {
750 dns_difftuple_t *tuple = NULL;
752 REQUIRE(DNS_DIFF_VALID(diff));
753 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_EXISTS,
754 name, 0, rdata, &tuple));
755 ISC_LIST_APPEND(diff->tuples, tuple, link);
761 * Compare two rdatasets represented as sorted lists of tuples.
762 * All list elements must have the same owner name and type.
763 * Return ISC_R_SUCCESS if the rdatasets are equal, rcode(dns_rcode_nxrrset)
767 temp_check_rrset(dns_difftuple_t *a, dns_difftuple_t *b) {
769 if (a == NULL || b == NULL)
771 INSIST(a->op == DNS_DIFFOP_EXISTS &&
772 b->op == DNS_DIFFOP_EXISTS);
773 INSIST(a->rdata.type == b->rdata.type);
774 INSIST(dns_name_equal(&a->name, &b->name));
775 if (dns_rdata_compare(&a->rdata, &b->rdata) != 0)
776 return (DNS_R_NXRRSET);
777 a = ISC_LIST_NEXT(a, link);
778 b = ISC_LIST_NEXT(b, link);
780 if (a != NULL || b != NULL)
781 return (DNS_R_NXRRSET);
782 return (ISC_R_SUCCESS);
786 * A comparison function defining the sorting order for the entries
787 * in the "temp" data structure. The major sort key is the owner name,
788 * followed by the type and rdata.
791 temp_order(const void *av, const void *bv) {
792 dns_difftuple_t const * const *ap = av;
793 dns_difftuple_t const * const *bp = bv;
794 dns_difftuple_t const *a = *ap;
795 dns_difftuple_t const *b = *bp;
797 r = dns_name_compare(&a->name, &b->name);
800 r = (b->rdata.type - a->rdata.type);
803 r = dns_rdata_compare(&a->rdata, &b->rdata);
808 * Check the "RRset exists (value dependent)" prerequisite information
809 * in 'temp' against the contents of the database 'db'.
811 * Return ISC_R_SUCCESS if the prerequisites are satisfied,
812 * rcode(dns_rcode_nxrrset) if not.
814 * 'temp' must be pre-sorted.
818 temp_check(isc_mem_t *mctx, dns_diff_t *temp, dns_db_t *db,
819 dns_dbversion_t *ver, dns_name_t *tmpname, dns_rdatatype_t *typep)
827 dns_diff_init(mctx, &trash);
830 * For each name and type in the prerequisites,
831 * construct a sorted rdata list of the corresponding
832 * database contents, and compare the lists.
834 t = ISC_LIST_HEAD(temp->tuples);
837 (void)dns_name_copy(name, tmpname, NULL);
838 *typep = t->rdata.type;
840 /* A new unique name begins here. */
842 result = dns_db_findnode(db, name, ISC_FALSE, &node);
843 if (result == ISC_R_NOTFOUND)
844 return (DNS_R_NXRRSET);
845 if (result != ISC_R_SUCCESS)
848 /* A new unique type begins here. */
849 while (t != NULL && dns_name_equal(&t->name, name)) {
850 dns_rdatatype_t type, covers;
851 dns_rdataset_t rdataset;
852 dns_diff_t d_rrs; /* Database RRs with
853 this name and type */
854 dns_diff_t u_rrs; /* Update RRs with
855 this name and type */
857 *typep = type = t->rdata.type;
858 if (type == dns_rdatatype_rrsig ||
859 type == dns_rdatatype_sig)
860 covers = dns_rdata_covers(&t->rdata);
865 * Collect all database RRs for this name and type
866 * onto d_rrs and sort them.
868 dns_rdataset_init(&rdataset);
869 result = dns_db_findrdataset(db, node, ver, type,
870 covers, (isc_stdtime_t) 0,
872 if (result != ISC_R_SUCCESS) {
873 dns_db_detachnode(db, &node);
874 return (DNS_R_NXRRSET);
877 dns_diff_init(mctx, &d_rrs);
878 dns_diff_init(mctx, &u_rrs);
880 for (result = dns_rdataset_first(&rdataset);
881 result == ISC_R_SUCCESS;
882 result = dns_rdataset_next(&rdataset))
884 dns_rdata_t rdata = DNS_RDATA_INIT;
885 dns_rdataset_current(&rdataset, &rdata);
886 result = temp_append(&d_rrs, name, &rdata);
887 if (result != ISC_R_SUCCESS)
890 if (result != ISC_R_NOMORE)
892 result = dns_diff_sort(&d_rrs, temp_order);
893 if (result != ISC_R_SUCCESS)
897 * Collect all update RRs for this name and type
898 * onto u_rrs. No need to sort them here -
899 * they are already sorted.
902 dns_name_equal(&t->name, name) &&
903 t->rdata.type == type)
905 dns_difftuple_t *next =
906 ISC_LIST_NEXT(t, link);
907 ISC_LIST_UNLINK(temp->tuples, t, link);
908 ISC_LIST_APPEND(u_rrs.tuples, t, link);
912 /* Compare the two sorted lists. */
913 result = temp_check_rrset(ISC_LIST_HEAD(u_rrs.tuples),
914 ISC_LIST_HEAD(d_rrs.tuples));
915 if (result != ISC_R_SUCCESS)
919 * We are done with the tuples, but we can't free
920 * them yet because "name" still points into one
921 * of them. Move them on a temporary list.
923 ISC_LIST_APPENDLIST(trash.tuples, u_rrs.tuples, link);
924 ISC_LIST_APPENDLIST(trash.tuples, d_rrs.tuples, link);
925 dns_rdataset_disassociate(&rdataset);
930 dns_diff_clear(&d_rrs);
931 dns_diff_clear(&u_rrs);
932 dns_diff_clear(&trash);
933 dns_rdataset_disassociate(&rdataset);
934 dns_db_detachnode(db, &node);
938 dns_db_detachnode(db, &node);
941 dns_diff_clear(&trash);
942 return (ISC_R_SUCCESS);
945 /**************************************************************************/
947 * Conditional deletion of RRs.
951 * Context structure for delete_if().
955 rr_predicate *predicate;
957 dns_dbversion_t *ver;
960 dns_rdata_t *update_rr;
961 } conditional_delete_ctx_t;
964 * Predicate functions for delete_if().
968 * Return true iff 'db_rr' is neither a SOA nor an NS RR nor
969 * an RRSIG nor a NSEC.
972 type_not_soa_nor_ns_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
974 return ((db_rr->type != dns_rdatatype_soa &&
975 db_rr->type != dns_rdatatype_ns &&
976 db_rr->type != dns_rdatatype_rrsig &&
977 db_rr->type != dns_rdatatype_nsec) ?
978 ISC_TRUE : ISC_FALSE);
982 * Return true iff 'db_rr' is neither a RRSIG nor a NSEC.
985 type_not_dnssec(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
987 return ((db_rr->type != dns_rdatatype_rrsig &&
988 db_rr->type != dns_rdatatype_nsec) ?
989 ISC_TRUE : ISC_FALSE);
993 * Return true always.
996 true_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1003 * Return true iff the two RRs have identical rdata.
1005 static isc_boolean_t
1006 rr_equal_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1008 * XXXRTH This is not a problem, but we should consider creating
1009 * dns_rdata_equal() (that used dns_name_equal()), since it
1010 * would be faster. Not a priority.
1012 return (dns_rdata_compare(update_rr, db_rr) == 0 ?
1013 ISC_TRUE : ISC_FALSE);
1017 * Return true iff 'update_rr' should replace 'db_rr' according
1018 * to the special RFC2136 rules for CNAME, SOA, and WKS records.
1020 * RFC2136 does not mention NSEC or DNAME, but multiple NSECs or DNAMEs
1021 * make little sense, so we replace those, too.
1023 static isc_boolean_t
1024 replaces_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1025 if (db_rr->type != update_rr->type)
1027 if (db_rr->type == dns_rdatatype_cname)
1029 if (db_rr->type == dns_rdatatype_dname)
1031 if (db_rr->type == dns_rdatatype_soa)
1033 if (db_rr->type == dns_rdatatype_nsec)
1035 if (db_rr->type == dns_rdatatype_wks) {
1037 * Compare the address and protocol fields only. These
1038 * form the first five bytes of the RR data. Do a
1039 * raw binary comparison; unpacking the WKS RRs using
1040 * dns_rdata_tostruct() might be cleaner in some ways,
1041 * but it would require us to pass around an mctx.
1043 INSIST(db_rr->length >= 5 && update_rr->length >= 5);
1044 return (memcmp(db_rr->data, update_rr->data, 5) == 0 ?
1045 ISC_TRUE : ISC_FALSE);
1051 * Internal helper function for delete_if().
1054 delete_if_action(void *data, rr_t *rr) {
1055 conditional_delete_ctx_t *ctx = data;
1056 if ((*ctx->predicate)(ctx->update_rr, &rr->rdata)) {
1057 isc_result_t result;
1058 result = update_one_rr(ctx->db, ctx->ver, ctx->diff,
1059 DNS_DIFFOP_DEL, ctx->name,
1060 rr->ttl, &rr->rdata);
1063 return (ISC_R_SUCCESS);
1068 * Conditionally delete RRs. Apply 'predicate' to the RRs
1069 * specified by 'db', 'ver', 'name', and 'type' (which can
1070 * be dns_rdatatype_any to match any type). Delete those
1071 * RRs for which the predicate returns true, and log the
1072 * deletions in 'diff'.
1075 delete_if(rr_predicate *predicate,
1077 dns_dbversion_t *ver,
1079 dns_rdatatype_t type,
1080 dns_rdatatype_t covers,
1081 dns_rdata_t *update_rr,
1084 conditional_delete_ctx_t ctx;
1085 ctx.predicate = predicate;
1090 ctx.update_rr = update_rr;
1091 return (foreach_rr(db, ver, name, type, covers,
1092 delete_if_action, &ctx));
1095 /**************************************************************************/
1097 * Prepare an RR for the addition of the new RR 'ctx->update_rr',
1098 * with TTL 'ctx->update_rr_ttl', to its rdataset, by deleting
1099 * the RRs if it is replaced by the new RR or has a conflicting TTL.
1100 * The necessary changes are appended to ctx->del_diff and ctx->add_diff;
1101 * we need to do all deletions before any additions so that we don't run
1102 * into transient states with conflicting TTLs.
1107 dns_dbversion_t *ver;
1110 dns_rdata_t *update_rr;
1111 dns_ttl_t update_rr_ttl;
1112 isc_boolean_t ignore_add;
1113 dns_diff_t del_diff;
1114 dns_diff_t add_diff;
1115 } add_rr_prepare_ctx_t;
1118 add_rr_prepare_action(void *data, rr_t *rr) {
1119 isc_result_t result = ISC_R_SUCCESS;
1120 add_rr_prepare_ctx_t *ctx = data;
1121 dns_difftuple_t *tuple = NULL;
1122 isc_boolean_t equal;
1125 * If the update RR is a "duplicate" of the update RR,
1126 * the update should be silently ignored.
1128 equal = ISC_TF(dns_rdata_compare(&rr->rdata, ctx->update_rr) == 0);
1129 if (equal && rr->ttl == ctx->update_rr_ttl) {
1130 ctx->ignore_add = ISC_TRUE;
1131 return (ISC_R_SUCCESS);
1135 * If this RR is "equal" to the update RR, it should
1136 * be deleted before the update RR is added.
1138 if (replaces_p(ctx->update_rr, &rr->rdata)) {
1139 CHECK(dns_difftuple_create(ctx->del_diff.mctx,
1140 DNS_DIFFOP_DEL, ctx->name,
1144 dns_diff_append(&ctx->del_diff, &tuple);
1145 return (ISC_R_SUCCESS);
1149 * If this RR differs in TTL from the update RR,
1150 * its TTL must be adjusted.
1152 if (rr->ttl != ctx->update_rr_ttl) {
1153 CHECK(dns_difftuple_create(ctx->del_diff.mctx,
1154 DNS_DIFFOP_DEL, ctx->name,
1158 dns_diff_append(&ctx->del_diff, &tuple);
1160 CHECK(dns_difftuple_create(ctx->add_diff.mctx,
1161 DNS_DIFFOP_ADD, ctx->name,
1165 dns_diff_append(&ctx->add_diff, &tuple);
1172 /**************************************************************************/
1174 * Miscellaneous subroutines.
1178 * Extract a single update RR from 'section' of dynamic update message
1179 * 'msg', with consistency checking.
1181 * Stores the owner name, rdata, and TTL of the update RR at 'name',
1182 * 'rdata', and 'ttl', respectively.
1185 get_current_rr(dns_message_t *msg, dns_section_t section,
1186 dns_rdataclass_t zoneclass,
1187 dns_name_t **name, dns_rdata_t *rdata, dns_rdatatype_t *covers,
1189 dns_rdataclass_t *update_class)
1191 dns_rdataset_t *rdataset;
1192 isc_result_t result;
1193 dns_message_currentname(msg, section, name);
1194 rdataset = ISC_LIST_HEAD((*name)->list);
1195 INSIST(rdataset != NULL);
1196 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
1197 *covers = rdataset->covers;
1198 *ttl = rdataset->ttl;
1199 result = dns_rdataset_first(rdataset);
1200 INSIST(result == ISC_R_SUCCESS);
1201 dns_rdataset_current(rdataset, rdata);
1202 INSIST(dns_rdataset_next(rdataset) == ISC_R_NOMORE);
1203 *update_class = rdata->rdclass;
1204 rdata->rdclass = zoneclass;
1208 * Increment the SOA serial number of database 'db', version 'ver'.
1209 * Replace the SOA record in the database, and log the
1214 * XXXRTH Failures in this routine will be worth logging, when
1215 * we have a logging system. Failure to find the zonename
1216 * or the SOA rdataset warrant at least an UNEXPECTED_ERROR().
1220 increment_soa_serial(dns_db_t *db, dns_dbversion_t *ver,
1221 dns_diff_t *diff, isc_mem_t *mctx)
1223 dns_difftuple_t *deltuple = NULL;
1224 dns_difftuple_t *addtuple = NULL;
1225 isc_uint32_t serial;
1226 isc_result_t result;
1228 CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_DEL, &deltuple));
1229 CHECK(dns_difftuple_copy(deltuple, &addtuple));
1230 addtuple->op = DNS_DIFFOP_ADD;
1232 serial = dns_soa_getserial(&addtuple->rdata);
1235 serial = (serial + 1) & 0xFFFFFFFF;
1239 dns_soa_setserial(serial, &addtuple->rdata);
1240 CHECK(do_one_tuple(&deltuple, db, ver, diff));
1241 CHECK(do_one_tuple(&addtuple, db, ver, diff));
1242 result = ISC_R_SUCCESS;
1245 if (addtuple != NULL)
1246 dns_difftuple_free(&addtuple);
1247 if (deltuple != NULL)
1248 dns_difftuple_free(&deltuple);
1253 * Check that the new SOA record at 'update_rdata' does not
1254 * illegally cause the SOA serial number to decrease or stay
1255 * unchanged relative to the existing SOA in 'db'.
1257 * Sets '*ok' to ISC_TRUE if the update is legal, ISC_FALSE if not.
1259 * William King points out that RFC2136 is inconsistent about
1260 * the case where the serial number stays unchanged:
1262 * section 3.4.2.2 requires a server to ignore a SOA update request
1263 * if the serial number on the update SOA is less_than_or_equal to
1264 * the zone SOA serial.
1266 * section 3.6 requires a server to ignore a SOA update request if
1267 * the serial is less_than the zone SOA serial.
1269 * Paul says 3.4.2.2 is correct.
1273 check_soa_increment(dns_db_t *db, dns_dbversion_t *ver,
1274 dns_rdata_t *update_rdata,
1277 isc_uint32_t db_serial;
1278 isc_uint32_t update_serial;
1279 isc_result_t result;
1281 update_serial = dns_soa_getserial(update_rdata);
1283 result = dns_db_getsoaserial(db, ver, &db_serial);
1284 if (result != ISC_R_SUCCESS)
1287 if (DNS_SERIAL_GE(db_serial, update_serial)) {
1293 return (ISC_R_SUCCESS);
1297 /**************************************************************************/
1299 * Incremental updating of NSECs and RRSIGs.
1302 #define MAXZONEKEYS 32 /* Maximum number of zone keys supported. */
1305 * We abuse the dns_diff_t type to represent a set of domain names
1306 * affected by the update.
1309 namelist_append_name(dns_diff_t *list, dns_name_t *name) {
1310 isc_result_t result;
1311 dns_difftuple_t *tuple = NULL;
1312 static dns_rdata_t dummy_rdata = { NULL, 0, 0, 0, 0,
1313 { (void*)(-1), (void*)(-1) } };
1314 CHECK(dns_difftuple_create(list->mctx, DNS_DIFFOP_EXISTS, name, 0,
1315 &dummy_rdata, &tuple));
1316 dns_diff_append(list, &tuple);
1322 namelist_append_subdomain(dns_db_t *db, dns_name_t *name, dns_diff_t *affected)
1324 isc_result_t result;
1325 dns_fixedname_t fixedname;
1327 dns_dbiterator_t *dbit = NULL;
1329 dns_fixedname_init(&fixedname);
1330 child = dns_fixedname_name(&fixedname);
1332 CHECK(dns_db_createiterator(db, ISC_FALSE, &dbit));
1334 for (result = dns_dbiterator_seek(dbit, name);
1335 result == ISC_R_SUCCESS;
1336 result = dns_dbiterator_next(dbit))
1338 dns_dbnode_t *node = NULL;
1339 CHECK(dns_dbiterator_current(dbit, &node, child));
1340 dns_db_detachnode(db, &node);
1341 if (! dns_name_issubdomain(child, name))
1343 CHECK(namelist_append_name(affected, child));
1345 if (result == ISC_R_NOMORE)
1346 result = ISC_R_SUCCESS;
1349 dns_dbiterator_destroy(&dbit);
1356 * Helper function for non_nsec_rrset_exists().
1359 is_non_nsec_action(void *data, dns_rdataset_t *rrset) {
1361 if (!(rrset->type == dns_rdatatype_nsec ||
1362 (rrset->type == dns_rdatatype_rrsig &&
1363 rrset->covers == dns_rdatatype_nsec)))
1364 return (ISC_R_EXISTS);
1365 return (ISC_R_SUCCESS);
1369 * Check whether there is an rrset other than a NSEC or RRSIG NSEC,
1370 * i.e., anything that justifies the continued existence of a name
1371 * after a secure update.
1373 * If such an rrset exists, set '*exists' to ISC_TRUE.
1374 * Otherwise, set it to ISC_FALSE.
1377 non_nsec_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
1378 dns_name_t *name, isc_boolean_t *exists)
1380 isc_result_t result;
1381 result = foreach_rrset(db, ver, name,
1382 is_non_nsec_action, NULL);
1383 RETURN_EXISTENCE_FLAG;
1387 * A comparison function for sorting dns_diff_t:s by name.
1390 name_order(const void *av, const void *bv) {
1391 dns_difftuple_t const * const *ap = av;
1392 dns_difftuple_t const * const *bp = bv;
1393 dns_difftuple_t const *a = *ap;
1394 dns_difftuple_t const *b = *bp;
1395 return (dns_name_compare(&a->name, &b->name));
1399 uniqify_name_list(dns_diff_t *list) {
1400 isc_result_t result;
1401 dns_difftuple_t *p, *q;
1403 CHECK(dns_diff_sort(list, name_order));
1405 p = ISC_LIST_HEAD(list->tuples);
1408 q = ISC_LIST_NEXT(p, link);
1409 if (q == NULL || ! dns_name_equal(&p->name, &q->name))
1411 ISC_LIST_UNLINK(list->tuples, q, link);
1412 dns_difftuple_free(&q);
1414 p = ISC_LIST_NEXT(p, link);
1422 is_glue(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1423 isc_boolean_t *flag)
1425 isc_result_t result;
1426 dns_fixedname_t foundname;
1427 dns_fixedname_init(&foundname);
1428 result = dns_db_find(db, name, ver, dns_rdatatype_any,
1429 DNS_DBFIND_GLUEOK | DNS_DBFIND_NOWILD,
1430 (isc_stdtime_t) 0, NULL,
1431 dns_fixedname_name(&foundname),
1433 if (result == ISC_R_SUCCESS) {
1435 return (ISC_R_SUCCESS);
1436 } else if (result == DNS_R_ZONECUT) {
1438 * We are at the zonecut. The name will have an NSEC, but
1439 * non-delegation will be omitted from the type bit map.
1442 return (ISC_R_SUCCESS);
1443 } else if (result == DNS_R_GLUE || result == DNS_R_DNAME) {
1445 return (ISC_R_SUCCESS);
1452 * Find the next/previous name that has a NSEC record.
1453 * In other words, skip empty database nodes and names that
1454 * have had their NSECs removed because they are obscured by
1458 next_active(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1459 dns_dbversion_t *ver, dns_name_t *oldname, dns_name_t *newname,
1460 isc_boolean_t forward)
1462 isc_result_t result;
1463 dns_dbiterator_t *dbit = NULL;
1464 isc_boolean_t has_nsec;
1465 unsigned int wraps = 0;
1467 CHECK(dns_db_createiterator(db, ISC_FALSE, &dbit));
1469 CHECK(dns_dbiterator_seek(dbit, oldname));
1471 dns_dbnode_t *node = NULL;
1474 result = dns_dbiterator_next(dbit);
1476 result = dns_dbiterator_prev(dbit);
1477 if (result == ISC_R_NOMORE) {
1482 CHECK(dns_dbiterator_first(dbit));
1484 CHECK(dns_dbiterator_last(dbit));
1487 update_log(client, zone, ISC_LOG_ERROR,
1488 "secure zone with no NSECs");
1489 result = DNS_R_BADZONE;
1493 CHECK(dns_dbiterator_current(dbit, &node, newname));
1494 dns_db_detachnode(db, &node);
1497 * The iterator may hold the tree lock, and
1498 * rrset_exists() calls dns_db_findnode() which
1499 * may try to reacquire it. To avoid deadlock
1500 * we must pause the iterator first.
1502 CHECK(dns_dbiterator_pause(dbit));
1503 CHECK(rrset_exists(db, ver, newname,
1504 dns_rdatatype_nsec, 0, &has_nsec));
1506 } while (! has_nsec);
1509 dns_dbiterator_destroy(&dbit);
1515 * Add a NSEC record for "name", recording the change in "diff".
1516 * The existing NSEC is removed.
1519 add_nsec(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1520 dns_dbversion_t *ver, dns_name_t *name, dns_diff_t *diff)
1522 isc_result_t result;
1523 dns_dbnode_t *node = NULL;
1524 unsigned char buffer[DNS_NSEC_BUFFERSIZE];
1525 dns_rdata_t rdata = DNS_RDATA_INIT;
1526 dns_difftuple_t *tuple = NULL;
1527 dns_fixedname_t fixedname;
1530 dns_fixedname_init(&fixedname);
1531 target = dns_fixedname_name(&fixedname);
1534 * Find the successor name, aka NSEC target.
1536 CHECK(next_active(client, zone, db, ver, name, target, ISC_TRUE));
1539 * Create the NSEC RDATA.
1541 CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
1542 dns_rdata_init(&rdata);
1543 CHECK(dns_nsec_buildrdata(db, ver, node, target, buffer, &rdata));
1544 dns_db_detachnode(db, &node);
1547 * Delete the old NSEC and record the change.
1549 CHECK(delete_if(true_p, db, ver, name, dns_rdatatype_nsec, 0,
1552 * Add the new NSEC and record the change.
1554 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name,
1557 CHECK(do_one_tuple(&tuple, db, ver, diff));
1558 INSIST(tuple == NULL);
1562 dns_db_detachnode(db, &node);
1567 * Add a placeholder NSEC record for "name", recording the change in "diff".
1570 add_placeholder_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1572 isc_result_t result;
1573 dns_difftuple_t *tuple = NULL;
1575 unsigned char data[1] = { 0 }; /* The root domain, no bits. */
1576 dns_rdata_t rdata = DNS_RDATA_INIT;
1579 r.length = sizeof(data);
1580 dns_rdata_fromregion(&rdata, dns_db_class(db), dns_rdatatype_nsec, &r);
1581 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, 0,
1583 CHECK(do_one_tuple(&tuple, db, ver, diff));
1589 find_zone_keys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
1590 isc_mem_t *mctx, unsigned int maxkeys,
1591 dst_key_t **keys, unsigned int *nkeys)
1593 isc_result_t result;
1594 dns_dbnode_t *node = NULL;
1595 const char *directory = dns_zone_getkeydirectory(zone);
1596 CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
1597 CHECK(dns_dnssec_findzonekeys2(db, ver, node, dns_db_origin(db),
1598 directory, mctx, maxkeys, keys, nkeys));
1601 dns_db_detachnode(db, &node);
1606 * Add RRSIG records for an RRset, recording the change in "diff".
1609 add_sigs(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1610 dns_rdatatype_t type, dns_diff_t *diff, dst_key_t **keys,
1611 unsigned int nkeys, isc_mem_t *mctx, isc_stdtime_t inception,
1612 isc_stdtime_t expire)
1614 isc_result_t result;
1615 dns_dbnode_t *node = NULL;
1616 dns_rdataset_t rdataset;
1617 dns_rdata_t sig_rdata = DNS_RDATA_INIT;
1618 isc_buffer_t buffer;
1619 unsigned char data[1024]; /* XXX */
1622 dns_rdataset_init(&rdataset);
1623 isc_buffer_init(&buffer, data, sizeof(data));
1625 /* Get the rdataset to sign. */
1626 CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
1627 CHECK(dns_db_findrdataset(db, node, ver, type, 0,
1630 dns_db_detachnode(db, &node);
1632 for (i = 0; i < nkeys; i++) {
1633 /* Calculate the signature, creating a RRSIG RDATA. */
1634 CHECK(dns_dnssec_sign(name, &rdataset, keys[i],
1635 &inception, &expire,
1636 mctx, &buffer, &sig_rdata));
1638 /* Update the database and journal with the RRSIG. */
1639 /* XXX inefficient - will cause dataset merging */
1640 CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADD, name,
1641 rdataset.ttl, &sig_rdata));
1642 dns_rdata_reset(&sig_rdata);
1646 if (dns_rdataset_isassociated(&rdataset))
1647 dns_rdataset_disassociate(&rdataset);
1649 dns_db_detachnode(db, &node);
1654 * Update RRSIG and NSEC records affected by an update. The original
1655 * update, including the SOA serial update but exluding the RRSIG & NSEC
1656 * changes, is in "diff" and has already been applied to "newver" of "db".
1657 * The database version prior to the update is "oldver".
1659 * The necessary RRSIG and NSEC changes will be applied to "newver"
1660 * and added (as a minimal diff) to "diff".
1662 * The RRSIGs generated will be valid for 'sigvalidityinterval' seconds.
1665 update_signatures(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1666 dns_dbversion_t *oldver, dns_dbversion_t *newver,
1667 dns_diff_t *diff, isc_uint32_t sigvalidityinterval)
1669 isc_result_t result;
1671 dns_diff_t diffnames;
1672 dns_diff_t affected;
1673 dns_diff_t sig_diff;
1674 dns_diff_t nsec_diff;
1675 dns_diff_t nsec_mindiff;
1677 dst_key_t *zone_keys[MAXZONEKEYS];
1678 unsigned int nkeys = 0;
1680 isc_stdtime_t now, inception, expire;
1682 dns_diff_init(client->mctx, &diffnames);
1683 dns_diff_init(client->mctx, &affected);
1685 dns_diff_init(client->mctx, &sig_diff);
1686 dns_diff_init(client->mctx, &nsec_diff);
1687 dns_diff_init(client->mctx, &nsec_mindiff);
1689 result = find_zone_keys(zone, db, newver, client->mctx,
1690 MAXZONEKEYS, zone_keys, &nkeys);
1691 if (result != ISC_R_SUCCESS) {
1692 update_log(client, zone, ISC_LOG_ERROR,
1693 "could not get zone keys for secure dynamic update");
1697 isc_stdtime_get(&now);
1698 inception = now - 3600; /* Allow for some clock skew. */
1699 expire = now + sigvalidityinterval;
1702 * Find all RRsets directly affected by the update, and
1703 * update their RRSIGs. Also build a list of names affected
1704 * by the update in "diffnames".
1706 CHECK(dns_diff_sort(diff, temp_order));
1708 t = ISC_LIST_HEAD(diff->tuples);
1710 dns_name_t *name = &t->name;
1711 /* Now "name" is a new, unique name affected by the update. */
1713 CHECK(namelist_append_name(&diffnames, name));
1715 while (t != NULL && dns_name_equal(&t->name, name)) {
1716 dns_rdatatype_t type;
1717 type = t->rdata.type;
1720 * Now "name" and "type" denote a new unique RRset
1721 * affected by the update.
1724 /* Don't sign RRSIGs. */
1725 if (type == dns_rdatatype_rrsig)
1729 * Delete all old RRSIGs covering this type, since they
1730 * are all invalid when the signed RRset has changed.
1731 * We may not be able to recreate all of them - tough.
1733 CHECK(delete_if(true_p, db, newver, name,
1734 dns_rdatatype_rrsig, type,
1738 * If this RRset still exists after the update,
1739 * add a new signature for it.
1741 CHECK(rrset_exists(db, newver, name, type, 0, &flag));
1743 CHECK(add_sigs(db, newver, name, type,
1744 &sig_diff, zone_keys, nkeys,
1745 client->mctx, inception,
1749 /* Skip any other updates to the same RRset. */
1751 dns_name_equal(&t->name, name) &&
1752 t->rdata.type == type)
1754 t = ISC_LIST_NEXT(t, link);
1759 /* Remove orphaned NSECs and RRSIG NSECs. */
1760 for (t = ISC_LIST_HEAD(diffnames.tuples);
1762 t = ISC_LIST_NEXT(t, link))
1764 CHECK(non_nsec_rrset_exists(db, newver, &t->name, &flag));
1766 CHECK(delete_if(true_p, db, newver, &t->name,
1767 dns_rdatatype_any, 0,
1773 * When a name is created or deleted, its predecessor needs to
1774 * have its NSEC updated.
1776 for (t = ISC_LIST_HEAD(diffnames.tuples);
1778 t = ISC_LIST_NEXT(t, link))
1780 isc_boolean_t existed, exists;
1781 dns_fixedname_t fixedname;
1782 dns_name_t *prevname;
1784 dns_fixedname_init(&fixedname);
1785 prevname = dns_fixedname_name(&fixedname);
1787 CHECK(name_exists(db, oldver, &t->name, &existed));
1788 CHECK(name_exists(db, newver, &t->name, &exists));
1789 if (exists == existed)
1793 * Find the predecessor.
1794 * When names become obscured or unobscured in this update
1795 * transaction, we may find the wrong predecessor because
1796 * the NSECs have not yet been updated to reflect the delegation
1797 * change. This should not matter because in this case,
1798 * the correct predecessor is either the delegation node or
1799 * a newly unobscured node, and those nodes are on the
1800 * "affected" list in any case.
1802 CHECK(next_active(client, zone, db, newver,
1803 &t->name, prevname, ISC_FALSE));
1804 CHECK(namelist_append_name(&affected, prevname));
1808 * Find names potentially affected by delegation changes
1809 * (obscured by adding an NS or DNAME, or unobscured by
1812 for (t = ISC_LIST_HEAD(diffnames.tuples);
1814 t = ISC_LIST_NEXT(t, link))
1816 isc_boolean_t ns_existed, dname_existed;
1817 isc_boolean_t ns_exists, dname_exists;
1819 CHECK(rrset_exists(db, oldver, &t->name, dns_rdatatype_ns, 0,
1821 CHECK(rrset_exists(db, oldver, &t->name, dns_rdatatype_dname, 0,
1823 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ns, 0,
1825 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_dname, 0,
1827 if ((ns_exists || dname_exists) == (ns_existed || dname_existed))
1830 * There was a delegation change. Mark all subdomains
1831 * of t->name as potentially needing a NSEC update.
1833 CHECK(namelist_append_subdomain(db, &t->name, &affected));
1836 ISC_LIST_APPENDLIST(affected.tuples, diffnames.tuples, link);
1837 INSIST(ISC_LIST_EMPTY(diffnames.tuples));
1839 CHECK(uniqify_name_list(&affected));
1842 * Determine which names should have NSECs, and delete/create
1843 * NSECs to make it so. We don't know the final NSEC targets yet,
1844 * so we just create placeholder NSECs with arbitrary contents
1845 * to indicate that their respective owner names should be part of
1848 for (t = ISC_LIST_HEAD(affected.tuples);
1850 t = ISC_LIST_NEXT(t, link))
1852 isc_boolean_t exists;
1853 CHECK(name_exists(db, newver, &t->name, &exists));
1856 CHECK(is_glue(db, newver, &t->name, &flag));
1859 * This name is obscured. Delete any
1860 * existing NSEC record.
1862 CHECK(delete_if(true_p, db, newver, &t->name,
1863 dns_rdatatype_nsec, 0,
1867 * This name is not obscured. It should have a NSEC.
1869 CHECK(rrset_exists(db, newver, &t->name,
1870 dns_rdatatype_nsec, 0, &flag));
1872 CHECK(add_placeholder_nsec(db, newver, &t->name,
1878 * Now we know which names are part of the NSEC chain.
1879 * Make them all point at their correct targets.
1881 for (t = ISC_LIST_HEAD(affected.tuples);
1883 t = ISC_LIST_NEXT(t, link))
1885 CHECK(rrset_exists(db, newver, &t->name,
1886 dns_rdatatype_nsec, 0, &flag));
1889 * There is a NSEC, but we don't know if it is correct.
1890 * Delete it and create a correct one to be sure.
1891 * If the update was unnecessary, the diff minimization
1892 * will take care of eliminating it from the journal,
1895 * The RRSIG bit should always be set in the NSECs
1896 * we generate, because they will all get RRSIG NSECs.
1897 * (XXX what if the zone keys are missing?).
1898 * Because the RRSIG NSECs have not necessarily been
1899 * created yet, the correctness of the bit mask relies
1900 * on the assumption that NSECs are only created if
1901 * there is other data, and if there is other data,
1902 * there are other RRSIGs.
1904 CHECK(add_nsec(client, zone, db, newver,
1905 &t->name, &nsec_diff));
1910 * Minimize the set of NSEC updates so that we don't
1911 * have to regenerate the RRSIG NSECs for NSECs that were
1912 * replaced with identical ones.
1914 while ((t = ISC_LIST_HEAD(nsec_diff.tuples)) != NULL) {
1915 ISC_LIST_UNLINK(nsec_diff.tuples, t, link);
1916 dns_diff_appendminimal(&nsec_mindiff, &t);
1919 /* Update RRSIG NSECs. */
1920 for (t = ISC_LIST_HEAD(nsec_mindiff.tuples);
1922 t = ISC_LIST_NEXT(t, link))
1924 if (t->op == DNS_DIFFOP_DEL) {
1925 CHECK(delete_if(true_p, db, newver, &t->name,
1926 dns_rdatatype_rrsig, dns_rdatatype_nsec,
1928 } else if (t->op == DNS_DIFFOP_ADD) {
1929 CHECK(add_sigs(db, newver, &t->name, dns_rdatatype_nsec,
1930 &sig_diff, zone_keys, nkeys,
1931 client->mctx, inception, expire));
1937 /* Record our changes for the journal. */
1938 while ((t = ISC_LIST_HEAD(sig_diff.tuples)) != NULL) {
1939 ISC_LIST_UNLINK(sig_diff.tuples, t, link);
1940 dns_diff_appendminimal(diff, &t);
1942 while ((t = ISC_LIST_HEAD(nsec_mindiff.tuples)) != NULL) {
1943 ISC_LIST_UNLINK(nsec_mindiff.tuples, t, link);
1944 dns_diff_appendminimal(diff, &t);
1947 INSIST(ISC_LIST_EMPTY(sig_diff.tuples));
1948 INSIST(ISC_LIST_EMPTY(nsec_diff.tuples));
1949 INSIST(ISC_LIST_EMPTY(nsec_mindiff.tuples));
1952 dns_diff_clear(&sig_diff);
1953 dns_diff_clear(&nsec_diff);
1954 dns_diff_clear(&nsec_mindiff);
1956 dns_diff_clear(&affected);
1957 dns_diff_clear(&diffnames);
1959 for (i = 0; i < nkeys; i++)
1960 dst_key_free(&zone_keys[i]);
1966 /**************************************************************************/
1968 * The actual update code in all its glory. We try to follow
1969 * the RFC2136 pseudocode as closely as possible.
1973 send_update_event(ns_client_t *client, dns_zone_t *zone) {
1974 isc_result_t result = ISC_R_SUCCESS;
1975 update_event_t *event = NULL;
1976 isc_task_t *zonetask = NULL;
1977 ns_client_t *evclient;
1979 event = (update_event_t *)
1980 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
1981 update_action, NULL, sizeof(*event));
1983 FAIL(ISC_R_NOMEMORY);
1985 event->result = ISC_R_SUCCESS;
1988 ns_client_attach(client, &evclient);
1989 INSIST(client->nupdates == 0);
1991 event->ev_arg = evclient;
1993 dns_zone_gettask(zone, &zonetask);
1994 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
1998 isc_event_free(ISC_EVENT_PTR(&event));
2003 respond(ns_client_t *client, isc_result_t result) {
2004 isc_result_t msg_result;
2006 msg_result = dns_message_reply(client->message, ISC_TRUE);
2007 if (msg_result != ISC_R_SUCCESS)
2009 client->message->rcode = dns_result_torcode(result);
2011 ns_client_send(client);
2015 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
2017 "could not create update response message: %s",
2018 isc_result_totext(msg_result));
2019 ns_client_next(client, msg_result);
2023 ns_update_start(ns_client_t *client, isc_result_t sigresult) {
2024 dns_message_t *request = client->message;
2025 isc_result_t result;
2026 dns_name_t *zonename;
2027 dns_rdataset_t *zone_rdataset;
2028 dns_zone_t *zone = NULL;
2031 * Interpret the zone section.
2033 result = dns_message_firstname(request, DNS_SECTION_ZONE);
2034 if (result != ISC_R_SUCCESS)
2035 FAILC(DNS_R_FORMERR,
2036 "update zone section empty");
2039 * The zone section must contain exactly one "question", and
2040 * it must be of type SOA.
2043 dns_message_currentname(request, DNS_SECTION_ZONE, &zonename);
2044 zone_rdataset = ISC_LIST_HEAD(zonename->list);
2045 if (zone_rdataset->type != dns_rdatatype_soa)
2046 FAILC(DNS_R_FORMERR,
2047 "update zone section contains non-SOA");
2048 if (ISC_LIST_NEXT(zone_rdataset, link) != NULL)
2049 FAILC(DNS_R_FORMERR,
2050 "update zone section contains multiple RRs");
2052 /* The zone section must have exactly one name. */
2053 result = dns_message_nextname(request, DNS_SECTION_ZONE);
2054 if (result != ISC_R_NOMORE)
2055 FAILC(DNS_R_FORMERR,
2056 "update zone section contains multiple RRs");
2058 result = dns_zt_find(client->view->zonetable, zonename, 0, NULL,
2060 if (result != ISC_R_SUCCESS)
2061 FAILC(DNS_R_NOTAUTH,
2062 "not authoritative for update zone");
2064 switch(dns_zone_gettype(zone)) {
2065 case dns_zone_master:
2067 * We can now fail due to a bad signature as we now know
2068 * that we are the master.
2070 if (sigresult != ISC_R_SUCCESS)
2072 CHECK(send_update_event(client, zone));
2074 case dns_zone_slave:
2075 CHECK(checkupdateacl(client, dns_zone_getforwardacl(zone),
2076 "update forwarding", zonename, ISC_TRUE));
2077 CHECK(send_forward_event(client, zone));
2080 FAILC(DNS_R_NOTAUTH,
2081 "not authoritative for update zone");
2087 * We failed without having sent an update event to the zone.
2088 * We are still in the client task context, so we can
2089 * simply give an error response without switching tasks.
2091 respond(client, result);
2093 dns_zone_detach(&zone);
2097 * DS records are not allowed to exist without corresponding NS records,
2098 * draft-ietf-dnsext-delegation-signer-11.txt, 2.2 Protocol Change,
2099 * "DS RRsets MUST NOT appear at non-delegation points or at a zone's apex".
2103 remove_orphaned_ds(dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff) {
2104 isc_result_t result;
2105 isc_boolean_t ns_exists, ds_exists;
2108 for (t = ISC_LIST_HEAD(diff->tuples);
2110 t = ISC_LIST_NEXT(t, link)) {
2111 if (t->op != DNS_DIFFOP_DEL ||
2112 t->rdata.type != dns_rdatatype_ns)
2114 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ns, 0,
2118 CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ds, 0,
2122 CHECK(delete_if(true_p, db, newver, &t->name,
2123 dns_rdatatype_ds, 0, NULL, diff));
2125 return (ISC_R_SUCCESS);
2132 update_action(isc_task_t *task, isc_event_t *event) {
2133 update_event_t *uev = (update_event_t *) event;
2134 dns_zone_t *zone = uev->zone;
2135 ns_client_t *client = (ns_client_t *)event->ev_arg;
2137 isc_result_t result;
2138 dns_db_t *db = NULL;
2139 dns_dbversion_t *oldver = NULL;
2140 dns_dbversion_t *ver = NULL;
2141 dns_diff_t diff; /* Pending updates. */
2142 dns_diff_t temp; /* Pending RR existence assertions. */
2143 isc_boolean_t soa_serial_changed = ISC_FALSE;
2144 isc_mem_t *mctx = client->mctx;
2145 dns_rdatatype_t covers;
2146 dns_message_t *request = client->message;
2147 dns_rdataclass_t zoneclass;
2148 dns_name_t *zonename;
2149 dns_ssutable_t *ssutable = NULL;
2150 dns_fixedname_t tmpnamefixed;
2151 dns_name_t *tmpname = NULL;
2153 INSIST(event->ev_type == DNS_EVENT_UPDATE);
2155 dns_diff_init(mctx, &diff);
2156 dns_diff_init(mctx, &temp);
2158 CHECK(dns_zone_getdb(zone, &db));
2159 zonename = dns_db_origin(db);
2160 zoneclass = dns_db_class(db);
2161 dns_zone_getssutable(zone, &ssutable);
2162 dns_db_currentversion(db, &oldver);
2163 CHECK(dns_db_newversion(db, &ver));
2166 * Check prerequisites.
2169 for (result = dns_message_firstname(request, DNS_SECTION_PREREQUISITE);
2170 result == ISC_R_SUCCESS;
2171 result = dns_message_nextname(request, DNS_SECTION_PREREQUISITE))
2173 dns_name_t *name = NULL;
2174 dns_rdata_t rdata = DNS_RDATA_INIT;
2176 dns_rdataclass_t update_class;
2179 get_current_rr(request, DNS_SECTION_PREREQUISITE, zoneclass,
2180 &name, &rdata, &covers, &ttl, &update_class);
2183 FAILC(DNS_R_FORMERR, "prerequisite TTL is not zero");
2185 if (! dns_name_issubdomain(name, zonename))
2186 FAILN(DNS_R_NOTZONE, name,
2187 "prerequisite name is out of zone");
2189 if (update_class == dns_rdataclass_any) {
2190 if (rdata.length != 0)
2191 FAILC(DNS_R_FORMERR,
2192 "class ANY prerequisite "
2193 "RDATA is not empty");
2194 if (rdata.type == dns_rdatatype_any) {
2195 CHECK(name_exists(db, ver, name, &flag));
2197 FAILN(DNS_R_NXDOMAIN, name,
2198 "'name in use' prerequisite "
2202 CHECK(rrset_exists(db, ver, name,
2203 rdata.type, covers, &flag));
2205 /* RRset does not exist. */
2206 FAILNT(DNS_R_NXRRSET, name, rdata.type,
2207 "'rrset exists (value independent)' "
2208 "prerequisite not satisfied");
2211 } else if (update_class == dns_rdataclass_none) {
2212 if (rdata.length != 0)
2213 FAILC(DNS_R_FORMERR,
2214 "class NONE prerequisite "
2215 "RDATA is not empty");
2216 if (rdata.type == dns_rdatatype_any) {
2217 CHECK(name_exists(db, ver, name, &flag));
2219 FAILN(DNS_R_YXDOMAIN, name,
2220 "'name not in use' prerequisite "
2224 CHECK(rrset_exists(db, ver, name,
2225 rdata.type, covers, &flag));
2228 FAILNT(DNS_R_YXRRSET, name, rdata.type,
2229 "'rrset does not exist' "
2230 "prerequisite not satisfied");
2233 } else if (update_class == zoneclass) {
2234 /* "temp<rr.name, rr.type> += rr;" */
2235 result = temp_append(&temp, name, &rdata);
2236 if (result != ISC_R_SUCCESS) {
2237 UNEXPECTED_ERROR(__FILE__, __LINE__,
2238 "temp entry creation failed: %s",
2239 dns_result_totext(result));
2240 FAIL(ISC_R_UNEXPECTED);
2243 FAILC(DNS_R_FORMERR, "malformed prerequisite");
2246 if (result != ISC_R_NOMORE)
2251 * Perform the final check of the "rrset exists (value dependent)"
2254 if (ISC_LIST_HEAD(temp.tuples) != NULL) {
2255 dns_rdatatype_t type;
2258 * Sort the prerequisite records by owner name,
2261 result = dns_diff_sort(&temp, temp_order);
2262 if (result != ISC_R_SUCCESS)
2263 FAILC(result, "'RRset exists (value dependent)' "
2264 "prerequisite not satisfied");
2266 dns_fixedname_init(&tmpnamefixed);
2267 tmpname = dns_fixedname_name(&tmpnamefixed);
2268 result = temp_check(mctx, &temp, db, ver, tmpname, &type);
2269 if (result != ISC_R_SUCCESS)
2270 FAILNT(result, tmpname, type,
2271 "'RRset exists (value dependent)' "
2272 "prerequisite not satisfied");
2275 update_log(client, zone, LOGLEVEL_DEBUG,
2276 "prerequisites are OK");
2279 * Check Requestor's Permissions. It seems a bit silly to do this
2280 * only after prerequisite testing, but that is what RFC2136 says.
2282 result = ISC_R_SUCCESS;
2283 if (ssutable == NULL)
2284 CHECK(checkupdateacl(client, dns_zone_getupdateacl(zone),
2285 "update", zonename, ISC_FALSE));
2286 else if (client->signer == NULL)
2287 CHECK(checkupdateacl(client, NULL, "update", zonename,
2290 if (dns_zone_getupdatedisabled(zone))
2291 FAILC(DNS_R_REFUSED, "dynamic update temporarily disabled");
2294 * Perform the Update Section Prescan.
2297 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2298 result == ISC_R_SUCCESS;
2299 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2301 dns_name_t *name = NULL;
2302 dns_rdata_t rdata = DNS_RDATA_INIT;
2304 dns_rdataclass_t update_class;
2305 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2306 &name, &rdata, &covers, &ttl, &update_class);
2308 if (! dns_name_issubdomain(name, zonename))
2309 FAILC(DNS_R_NOTZONE,
2310 "update RR is outside zone");
2311 if (update_class == zoneclass) {
2313 * Check for meta-RRs. The RFC2136 pseudocode says
2314 * check for ANY|AXFR|MAILA|MAILB, but the text adds
2315 * "or any other QUERY metatype"
2317 if (dns_rdatatype_ismeta(rdata.type)) {
2318 FAILC(DNS_R_FORMERR,
2319 "meta-RR in update");
2321 result = dns_zone_checknames(zone, name, &rdata);
2322 if (result != ISC_R_SUCCESS)
2323 FAIL(DNS_R_REFUSED);
2324 } else if (update_class == dns_rdataclass_any) {
2325 if (ttl != 0 || rdata.length != 0 ||
2326 (dns_rdatatype_ismeta(rdata.type) &&
2327 rdata.type != dns_rdatatype_any))
2328 FAILC(DNS_R_FORMERR,
2329 "meta-RR in update");
2330 } else if (update_class == dns_rdataclass_none) {
2332 dns_rdatatype_ismeta(rdata.type))
2333 FAILC(DNS_R_FORMERR,
2334 "meta-RR in update");
2336 update_log(client, zone, ISC_LOG_WARNING,
2337 "update RR has incorrect class %d",
2339 FAIL(DNS_R_FORMERR);
2342 * draft-ietf-dnsind-simple-secure-update-01 says
2343 * "Unlike traditional dynamic update, the client
2344 * is forbidden from updating NSEC records."
2346 if (dns_db_issecure(db)) {
2347 if (rdata.type == dns_rdatatype_nsec) {
2348 FAILC(DNS_R_REFUSED,
2349 "explicit NSEC updates are not allowed "
2352 else if (rdata.type == dns_rdatatype_rrsig) {
2353 FAILC(DNS_R_REFUSED,
2354 "explicit RRSIG updates are currently not "
2355 "supported in secure zones");
2359 if (ssutable != NULL && client->signer != NULL) {
2360 if (rdata.type != dns_rdatatype_any) {
2361 if (!dns_ssutable_checkrules(ssutable,
2364 FAILC(DNS_R_REFUSED,
2365 "rejected by secure update");
2368 if (!ssu_checkall(db, ver, name, ssutable,
2370 FAILC(DNS_R_REFUSED,
2371 "rejected by secure update");
2375 if (result != ISC_R_NOMORE)
2378 update_log(client, zone, LOGLEVEL_DEBUG,
2379 "update section prescan OK");
2382 * Process the Update Section.
2385 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2386 result == ISC_R_SUCCESS;
2387 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2389 dns_name_t *name = NULL;
2390 dns_rdata_t rdata = DNS_RDATA_INIT;
2392 dns_rdataclass_t update_class;
2395 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2396 &name, &rdata, &covers, &ttl, &update_class);
2398 if (update_class == zoneclass) {
2401 * RFC 1123 doesn't allow MF and MD in master zones. */
2402 if (rdata.type == dns_rdatatype_md ||
2403 rdata.type == dns_rdatatype_mf) {
2404 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2406 dns_rdatatype_format(rdata.type, typebuf,
2408 update_log(client, zone, LOGLEVEL_PROTOCOL,
2409 "attempt to add %s ignored",
2413 if (rdata.type == dns_rdatatype_ns &&
2414 dns_name_iswildcard(name)) {
2415 update_log(client, zone,
2417 "attempt to add wildcard NS record"
2421 if (rdata.type == dns_rdatatype_cname) {
2422 CHECK(cname_incompatible_rrset_exists(db, ver,
2426 update_log(client, zone,
2428 "attempt to add CNAME "
2429 "alongside non-CNAME "
2434 CHECK(rrset_exists(db, ver, name,
2435 dns_rdatatype_cname, 0,
2438 ! dns_rdatatype_isdnssec(rdata.type))
2440 update_log(client, zone,
2442 "attempt to add non-CNAME "
2443 "alongside CNAME ignored");
2447 if (rdata.type == dns_rdatatype_soa) {
2449 CHECK(rrset_exists(db, ver, name,
2450 dns_rdatatype_soa, 0,
2453 update_log(client, zone,
2455 "attempt to create 2nd "
2459 CHECK(check_soa_increment(db, ver, &rdata,
2462 update_log(client, zone,
2464 "SOA update failed to "
2465 "increment serial, "
2469 soa_serial_changed = ISC_TRUE;
2472 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) {
2473 char namestr[DNS_NAME_FORMATSIZE];
2474 char typestr[DNS_RDATATYPE_FORMATSIZE];
2475 dns_name_format(name, namestr,
2477 dns_rdatatype_format(rdata.type, typestr,
2479 update_log(client, zone,
2481 "adding an RR at '%s' %s",
2485 /* Prepare the affected RRset for the addition. */
2487 add_rr_prepare_ctx_t ctx;
2492 ctx.update_rr = &rdata;
2493 ctx.update_rr_ttl = ttl;
2494 ctx.ignore_add = ISC_FALSE;
2495 dns_diff_init(mctx, &ctx.del_diff);
2496 dns_diff_init(mctx, &ctx.add_diff);
2497 CHECK(foreach_rr(db, ver, name, rdata.type,
2498 covers, add_rr_prepare_action,
2501 if (ctx.ignore_add) {
2502 dns_diff_clear(&ctx.del_diff);
2503 dns_diff_clear(&ctx.add_diff);
2505 CHECK(do_diff(&ctx.del_diff, db, ver, &diff));
2506 CHECK(do_diff(&ctx.add_diff, db, ver, &diff));
2507 CHECK(update_one_rr(db, ver, &diff,
2509 name, ttl, &rdata));
2512 } else if (update_class == dns_rdataclass_any) {
2513 if (rdata.type == dns_rdatatype_any) {
2514 if (isc_log_wouldlog(ns_g_lctx,
2517 char namestr[DNS_NAME_FORMATSIZE];
2518 dns_name_format(name, namestr,
2520 update_log(client, zone,
2522 "delete all rrsets from "
2523 "name '%s'", namestr);
2525 if (dns_name_equal(name, zonename)) {
2526 CHECK(delete_if(type_not_soa_nor_ns_p,
2528 dns_rdatatype_any, 0,
2531 CHECK(delete_if(type_not_dnssec,
2533 dns_rdatatype_any, 0,
2536 } else if (dns_name_equal(name, zonename) &&
2537 (rdata.type == dns_rdatatype_soa ||
2538 rdata.type == dns_rdatatype_ns)) {
2539 update_log(client, zone,
2541 "attempt to delete all SOA "
2542 "or NS records ignored");
2545 if (isc_log_wouldlog(ns_g_lctx,
2548 char namestr[DNS_NAME_FORMATSIZE];
2549 char typestr[DNS_RDATATYPE_FORMATSIZE];
2550 dns_name_format(name, namestr,
2552 dns_rdatatype_format(rdata.type,
2555 update_log(client, zone,
2557 "deleting rrset at '%s' %s",
2560 CHECK(delete_if(true_p, db, ver, name,
2561 rdata.type, covers, &rdata,
2564 } else if (update_class == dns_rdataclass_none) {
2566 * The (name == zonename) condition appears in
2567 * RFC2136 3.4.2.4 but is missing from the pseudocode.
2569 if (dns_name_equal(name, zonename)) {
2570 if (rdata.type == dns_rdatatype_soa) {
2571 update_log(client, zone,
2573 "attempt to delete SOA "
2577 if (rdata.type == dns_rdatatype_ns) {
2579 CHECK(rr_count(db, ver, name,
2583 update_log(client, zone,
2592 update_log(client, zone,
2595 CHECK(delete_if(rr_equal_p, db, ver, name,
2596 rdata.type, covers, &rdata, &diff));
2599 if (result != ISC_R_NOMORE)
2603 * If any changes were made, increment the SOA serial number,
2604 * update RRSIGs and NSECs (if zone is secure), and write the update
2607 if (! ISC_LIST_EMPTY(diff.tuples)) {
2609 dns_journal_t *journal;
2612 * Increment the SOA serial, but only if it was not
2613 * changed as a result of an update operation.
2615 if (! soa_serial_changed) {
2616 CHECK(increment_soa_serial(db, ver, &diff, mctx));
2619 CHECK(remove_orphaned_ds(db, ver, &diff));
2621 if (dns_db_issecure(db)) {
2622 result = update_signatures(client, zone, db, oldver,
2624 dns_zone_getsigvalidityinterval(zone));
2625 if (result != ISC_R_SUCCESS) {
2626 update_log(client, zone,
2628 "RRSIG/NSEC update failed: %s",
2629 isc_result_totext(result));
2634 journalfile = dns_zone_getjournal(zone);
2635 if (journalfile != NULL) {
2636 update_log(client, zone, LOGLEVEL_DEBUG,
2637 "writing journal %s", journalfile);
2640 result = dns_journal_open(mctx, journalfile,
2641 ISC_TRUE, &journal);
2642 if (result != ISC_R_SUCCESS)
2643 FAILS(result, "journal open failed");
2645 result = dns_journal_write_transaction(journal, &diff);
2646 if (result != ISC_R_SUCCESS) {
2647 dns_journal_destroy(&journal);
2648 FAILS(result, "journal write failed");
2651 dns_journal_destroy(&journal);
2655 * XXXRTH Just a note that this committing code will have
2656 * to change to handle databases that need two-phase
2657 * commit, but this isn't a priority.
2659 update_log(client, zone, LOGLEVEL_DEBUG,
2660 "committing update transaction");
2661 dns_db_closeversion(db, &ver, ISC_TRUE);
2664 * Mark the zone as dirty so that it will be written to disk.
2666 dns_zone_markdirty(zone);
2669 * Notify slaves of the change we just made.
2671 dns_zone_notify(zone);
2673 update_log(client, zone, LOGLEVEL_DEBUG, "redundant request");
2674 dns_db_closeversion(db, &ver, ISC_TRUE);
2676 result = ISC_R_SUCCESS;
2681 * The reason for failure should have been logged at this point.
2684 update_log(client, zone, LOGLEVEL_DEBUG,
2686 dns_db_closeversion(db, &ver, ISC_FALSE);
2690 dns_diff_clear(&temp);
2691 dns_diff_clear(&diff);
2694 dns_db_closeversion(db, &oldver, ISC_FALSE);
2699 if (ssutable != NULL)
2700 dns_ssutable_detach(&ssutable);
2703 dns_zone_detach(&zone);
2705 isc_task_detach(&task);
2706 uev->result = result;
2707 uev->ev_type = DNS_EVENT_UPDATEDONE;
2708 uev->ev_action = updatedone_action;
2709 isc_task_send(client->task, &event);
2710 INSIST(event == NULL);
2714 updatedone_action(isc_task_t *task, isc_event_t *event) {
2715 update_event_t *uev = (update_event_t *) event;
2716 ns_client_t *client = (ns_client_t *) event->ev_arg;
2720 INSIST(event->ev_type == DNS_EVENT_UPDATEDONE);
2721 INSIST(task == client->task);
2723 INSIST(client->nupdates > 0);
2725 respond(client, uev->result);
2726 isc_event_free(&event);
2727 ns_client_detach(&client);
2731 * Update forwarding support.
2735 forward_fail(isc_task_t *task, isc_event_t *event) {
2736 ns_client_t *client = (ns_client_t *)event->ev_arg;
2740 INSIST(client->nupdates > 0);
2742 respond(client, DNS_R_SERVFAIL);
2743 isc_event_free(&event);
2744 ns_client_detach(&client);
2749 forward_callback(void *arg, isc_result_t result, dns_message_t *answer) {
2750 update_event_t *uev = arg;
2751 ns_client_t *client = uev->ev_arg;
2753 if (result != ISC_R_SUCCESS) {
2754 INSIST(answer == NULL);
2755 uev->ev_type = DNS_EVENT_UPDATEDONE;
2756 uev->ev_action = forward_fail;
2758 uev->ev_type = DNS_EVENT_UPDATEDONE;
2759 uev->ev_action = forward_done;
2760 uev->answer = answer;
2762 isc_task_send(client->task, ISC_EVENT_PTR(&uev));
2766 forward_done(isc_task_t *task, isc_event_t *event) {
2767 update_event_t *uev = (update_event_t *) event;
2768 ns_client_t *client = (ns_client_t *)event->ev_arg;
2772 INSIST(client->nupdates > 0);
2774 ns_client_sendraw(client, uev->answer);
2775 dns_message_destroy(&uev->answer);
2776 isc_event_free(&event);
2777 ns_client_detach(&client);
2781 forward_action(isc_task_t *task, isc_event_t *event) {
2782 update_event_t *uev = (update_event_t *) event;
2783 dns_zone_t *zone = uev->zone;
2784 ns_client_t *client = (ns_client_t *)event->ev_arg;
2785 isc_result_t result;
2787 result = dns_zone_forwardupdate(zone, client->message,
2788 forward_callback, event);
2789 if (result != ISC_R_SUCCESS) {
2790 uev->ev_type = DNS_EVENT_UPDATEDONE;
2791 uev->ev_action = forward_fail;
2792 isc_task_send(client->task, &event);
2794 dns_zone_detach(&zone);
2795 isc_task_detach(&task);
2799 send_forward_event(ns_client_t *client, dns_zone_t *zone) {
2800 isc_result_t result = ISC_R_SUCCESS;
2801 update_event_t *event = NULL;
2802 isc_task_t *zonetask = NULL;
2803 ns_client_t *evclient;
2805 event = (update_event_t *)
2806 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
2807 forward_action, NULL, sizeof(*event));
2809 FAIL(ISC_R_NOMEMORY);
2811 event->result = ISC_R_SUCCESS;
2814 ns_client_attach(client, &evclient);
2815 INSIST(client->nupdates == 0);
2817 event->ev_arg = evclient;
2819 dns_zone_gettask(zone, &zonetask);
2820 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
2824 isc_event_free(ISC_EVENT_PTR(&event));