2 * Copyright (C) 2004-2012 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.199 2011/12/22 07:32:40 each Exp $ */
22 #include <isc/netaddr.h>
23 #include <isc/print.h>
24 #include <isc/serial.h>
25 #include <isc/stats.h>
26 #include <isc/string.h>
27 #include <isc/taskpool.h>
31 #include <dns/dbiterator.h>
33 #include <dns/dnssec.h>
34 #include <dns/events.h>
35 #include <dns/fixedname.h>
36 #include <dns/journal.h>
37 #include <dns/keyvalues.h>
38 #include <dns/message.h>
40 #include <dns/nsec3.h>
41 #include <dns/private.h>
42 #include <dns/rdataclass.h>
43 #include <dns/rdataset.h>
44 #include <dns/rdatasetiter.h>
45 #include <dns/rdatastruct.h>
46 #include <dns/rdatatype.h>
50 #include <dns/update.h>
55 #include <named/client.h>
56 #include <named/log.h>
57 #include <named/server.h>
58 #include <named/update.h>
62 * This module implements dynamic update as in RFC2136.
67 * - document strict minimality
70 /**************************************************************************/
73 * Log level for tracing dynamic update protocol requests.
75 #define LOGLEVEL_PROTOCOL ISC_LOG_INFO
78 * Log level for low-level debug tracing.
80 #define LOGLEVEL_DEBUG ISC_LOG_DEBUG(8)
83 * Check an operation for failure. These macros all assume that
84 * the function using them has a 'result' variable and a 'failure'
89 if (result != ISC_R_SUCCESS) goto failure; \
93 * Fail unconditionally with result 'code', which must not
94 * be ISC_R_SUCCESS. The reason for failure presumably has
95 * been logged already.
97 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
98 * from complaining about "end-of-loop code not reached".
104 if (result != ISC_R_SUCCESS) goto failure; \
108 * Fail unconditionally and log as a client error.
109 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
110 * from complaining about "end-of-loop code not reached".
112 #define FAILC(code, msg) \
114 const char *_what = "failed"; \
117 case DNS_R_NXDOMAIN: \
118 case DNS_R_YXDOMAIN: \
119 case DNS_R_YXRRSET: \
120 case DNS_R_NXRRSET: \
121 _what = "unsuccessful"; \
123 update_log(client, zone, LOGLEVEL_PROTOCOL, \
124 "update %s: %s (%s)", _what, \
125 msg, isc_result_totext(result)); \
126 if (result != ISC_R_SUCCESS) goto failure; \
128 #define PREREQFAILC(code, msg) \
130 inc_stats(zone, dns_nsstatscounter_updatebadprereq); \
134 #define FAILN(code, name, msg) \
136 const char *_what = "failed"; \
139 case DNS_R_NXDOMAIN: \
140 case DNS_R_YXDOMAIN: \
141 case DNS_R_YXRRSET: \
142 case DNS_R_NXRRSET: \
143 _what = "unsuccessful"; \
145 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
146 char _nbuf[DNS_NAME_FORMATSIZE]; \
147 dns_name_format(name, _nbuf, sizeof(_nbuf)); \
148 update_log(client, zone, LOGLEVEL_PROTOCOL, \
149 "update %s: %s: %s (%s)", _what, _nbuf, \
150 msg, isc_result_totext(result)); \
152 if (result != ISC_R_SUCCESS) goto failure; \
154 #define PREREQFAILN(code, name, msg) \
156 inc_stats(zone, dns_nsstatscounter_updatebadprereq); \
157 FAILN(code, name, msg); \
160 #define FAILNT(code, name, type, msg) \
162 const char *_what = "failed"; \
165 case DNS_R_NXDOMAIN: \
166 case DNS_R_YXDOMAIN: \
167 case DNS_R_YXRRSET: \
168 case DNS_R_NXRRSET: \
169 _what = "unsuccessful"; \
171 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
172 char _nbuf[DNS_NAME_FORMATSIZE]; \
173 char _tbuf[DNS_RDATATYPE_FORMATSIZE]; \
174 dns_name_format(name, _nbuf, sizeof(_nbuf)); \
175 dns_rdatatype_format(type, _tbuf, sizeof(_tbuf)); \
176 update_log(client, zone, LOGLEVEL_PROTOCOL, \
177 "update %s: %s/%s: %s (%s)", \
178 _what, _nbuf, _tbuf, msg, \
179 isc_result_totext(result)); \
181 if (result != ISC_R_SUCCESS) goto failure; \
183 #define PREREQFAILNT(code, name, type, msg) \
185 inc_stats(zone, dns_nsstatscounter_updatebadprereq); \
186 FAILNT(code, name, type, msg); \
190 * Fail unconditionally and log as a server error.
191 * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
192 * from complaining about "end-of-loop code not reached".
194 #define FAILS(code, msg) \
197 update_log(client, zone, LOGLEVEL_PROTOCOL, \
199 msg, isc_result_totext(result)); \
200 if (result != ISC_R_SUCCESS) goto failure; \
204 * Return TRUE if NS_CLIENTATTR_TCP is set in the attributes other FALSE.
206 #define TCPCLIENT(client) (((client)->attributes & NS_CLIENTATTR_TCP) != 0)
208 /**************************************************************************/
210 typedef struct rr rr_t;
213 /* dns_name_t name; */
218 typedef struct update_event update_event_t;
220 struct update_event {
221 ISC_EVENT_COMMON(update_event_t);
224 dns_message_t *answer;
227 /**************************************************************************/
229 * Forward declarations.
232 static void update_action(isc_task_t *task, isc_event_t *event);
233 static void updatedone_action(isc_task_t *task, isc_event_t *event);
234 static isc_result_t send_forward_event(ns_client_t *client, dns_zone_t *zone);
235 static void forward_done(isc_task_t *task, isc_event_t *event);
237 /**************************************************************************/
240 update_log(ns_client_t *client, dns_zone_t *zone,
241 int level, const char *fmt, ...) ISC_FORMAT_PRINTF(4, 5);
244 update_log(ns_client_t *client, dns_zone_t *zone,
245 int level, const char *fmt, ...)
249 char namebuf[DNS_NAME_FORMATSIZE];
250 char classbuf[DNS_RDATACLASS_FORMATSIZE];
252 if (client == NULL || zone == NULL)
255 if (isc_log_wouldlog(ns_g_lctx, level) == ISC_FALSE)
258 dns_name_format(dns_zone_getorigin(zone), namebuf,
260 dns_rdataclass_format(dns_zone_getclass(zone), classbuf,
264 vsnprintf(message, sizeof(message), fmt, ap);
267 ns_client_log(client, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
268 level, "updating zone '%s/%s': %s",
269 namebuf, classbuf, message);
273 update_log_cb(void *arg, dns_zone_t *zone, int level, const char *message) {
274 update_log(arg, zone, level, "%s", message);
278 * Increment updated-related statistics counters.
281 inc_stats(dns_zone_t *zone, isc_statscounter_t counter) {
282 isc_stats_increment(ns_g_server->nsstats, counter);
285 isc_stats_t *zonestats = dns_zone_getrequeststats(zone);
286 if (zonestats != NULL)
287 isc_stats_increment(zonestats, counter);
292 * Check if we could have queried for the contents of this zone or
293 * if the zone is potentially updateable.
294 * If the zone can potentially be updated and the check failed then
295 * log a error otherwise we log a informational message.
298 checkqueryacl(ns_client_t *client, dns_acl_t *queryacl, dns_name_t *zonename,
299 dns_acl_t *updateacl, dns_ssutable_t *ssutable)
301 char namebuf[DNS_NAME_FORMATSIZE];
302 char classbuf[DNS_RDATACLASS_FORMATSIZE];
306 result = ns_client_checkaclsilent(client, NULL, queryacl, ISC_TRUE);
307 if (result != ISC_R_SUCCESS) {
308 dns_name_format(zonename, namebuf, sizeof(namebuf));
309 dns_rdataclass_format(client->view->rdclass, classbuf,
312 level = (updateacl == NULL && ssutable == NULL) ?
313 ISC_LOG_INFO : ISC_LOG_ERROR;
315 ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
316 NS_LOGMODULE_UPDATE, level,
317 "update '%s/%s' denied due to allow-query",
319 } else if (updateacl == NULL && ssutable == NULL) {
320 dns_name_format(zonename, namebuf, sizeof(namebuf));
321 dns_rdataclass_format(client->view->rdclass, classbuf,
324 result = DNS_R_REFUSED;
325 ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
326 NS_LOGMODULE_UPDATE, ISC_LOG_INFO,
327 "update '%s/%s' denied", namebuf, classbuf);
333 * Override the default acl logging when checking whether a client
334 * can update the zone or whether we can forward the request to the
335 * master based on IP address.
337 * 'message' contains the type of operation that is being attempted.
338 * 'slave' indicates if this is a slave zone. If 'acl' is NULL then
340 * If the zone has no access controls configured ('acl' == NULL &&
341 * 'has_ssutable == ISC_FALS) log the attempt at info, otherwise
344 * If the request was signed log that we received it.
347 checkupdateacl(ns_client_t *client, dns_acl_t *acl, const char *message,
348 dns_name_t *zonename, isc_boolean_t slave,
349 isc_boolean_t has_ssutable)
351 char namebuf[DNS_NAME_FORMATSIZE];
352 char classbuf[DNS_RDATACLASS_FORMATSIZE];
353 int level = ISC_LOG_ERROR;
354 const char *msg = "denied";
357 if (slave && acl == NULL) {
358 result = DNS_R_NOTIMP;
359 level = ISC_LOG_DEBUG(3);
362 result = ns_client_checkaclsilent(client, NULL, acl, ISC_FALSE);
363 if (result == ISC_R_SUCCESS) {
364 level = ISC_LOG_DEBUG(3);
366 } else if (acl == NULL && !has_ssutable) {
367 level = ISC_LOG_INFO;
371 if (client->signer != NULL) {
372 dns_name_format(client->signer, namebuf, sizeof(namebuf));
373 ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
374 NS_LOGMODULE_UPDATE, ISC_LOG_INFO,
375 "signer \"%s\" %s", namebuf, msg);
378 dns_name_format(zonename, namebuf, sizeof(namebuf));
379 dns_rdataclass_format(client->view->rdclass, classbuf,
382 ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
383 NS_LOGMODULE_UPDATE, level, "%s '%s/%s' %s",
384 message, namebuf, classbuf, msg);
389 * Update a single RR in version 'ver' of 'db' and log the
393 * \li '*tuple' == NULL. Either the tuple is freed, or its
394 * ownership has been transferred to the diff.
397 do_one_tuple(dns_difftuple_t **tuple, dns_db_t *db, dns_dbversion_t *ver,
400 dns_diff_t temp_diff;
404 * Create a singleton diff.
406 dns_diff_init(diff->mctx, &temp_diff);
407 temp_diff.resign = diff->resign;
408 ISC_LIST_APPEND(temp_diff.tuples, *tuple, link);
411 * Apply it to the database.
413 result = dns_diff_apply(&temp_diff, db, ver);
414 ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link);
415 if (result != ISC_R_SUCCESS) {
416 dns_difftuple_free(tuple);
421 * Merge it into the current pending journal entry.
423 dns_diff_appendminimal(diff, tuple);
426 * Do not clear temp_diff.
428 return (ISC_R_SUCCESS);
432 * Perform the updates in 'updates' in version 'ver' of 'db' and log the
436 * \li 'updates' is empty.
439 do_diff(dns_diff_t *updates, dns_db_t *db, dns_dbversion_t *ver,
443 while (! ISC_LIST_EMPTY(updates->tuples)) {
444 dns_difftuple_t *t = ISC_LIST_HEAD(updates->tuples);
445 ISC_LIST_UNLINK(updates->tuples, t, link);
446 CHECK(do_one_tuple(&t, db, ver, diff));
448 return (ISC_R_SUCCESS);
451 dns_diff_clear(diff);
456 update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
457 dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
460 dns_difftuple_t *tuple = NULL;
462 result = dns_difftuple_create(diff->mctx, op,
463 name, ttl, rdata, &tuple);
464 if (result != ISC_R_SUCCESS)
466 return (do_one_tuple(&tuple, db, ver, diff));
469 /**************************************************************************/
471 * Callback-style iteration over rdatasets and rdatas.
473 * foreach_rrset() can be used to iterate over the RRsets
474 * of a name and call a callback function with each
475 * one. Similarly, foreach_rr() can be used to iterate
476 * over the individual RRs at name, optionally restricted
477 * to RRs of a given type.
479 * The callback functions are called "actions" and take
480 * two arguments: a void pointer for passing arbitrary
481 * context information, and a pointer to the current RRset
482 * or RR. By convention, their names end in "_action".
486 * XXXRTH We might want to make this public somewhere in libdns.
490 * Function type for foreach_rrset() iterator actions.
492 typedef isc_result_t rrset_func(void *data, dns_rdataset_t *rrset);
495 * Function type for foreach_rr() iterator actions.
497 typedef isc_result_t rr_func(void *data, rr_t *rr);
500 * Internal context struct for foreach_node_rr().
504 void * rr_action_data;
505 } foreach_node_rr_ctx_t;
508 * Internal helper function for foreach_node_rr().
511 foreach_node_rr_action(void *data, dns_rdataset_t *rdataset) {
513 foreach_node_rr_ctx_t *ctx = data;
514 for (result = dns_rdataset_first(rdataset);
515 result == ISC_R_SUCCESS;
516 result = dns_rdataset_next(rdataset))
518 rr_t rr = { 0, DNS_RDATA_INIT };
520 dns_rdataset_current(rdataset, &rr.rdata);
521 rr.ttl = rdataset->ttl;
522 result = (*ctx->rr_action)(ctx->rr_action_data, &rr);
523 if (result != ISC_R_SUCCESS)
526 if (result != ISC_R_NOMORE)
528 return (ISC_R_SUCCESS);
532 * For each rdataset of 'name' in 'ver' of 'db', call 'action'
533 * with the rdataset and 'action_data' as arguments. If the name
534 * does not exist, do nothing.
536 * If 'action' returns an error, abort iteration and return the error.
539 foreach_rrset(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
540 rrset_func *action, void *action_data)
544 dns_rdatasetiter_t *iter;
547 result = dns_db_findnode(db, name, ISC_FALSE, &node);
548 if (result == ISC_R_NOTFOUND)
549 return (ISC_R_SUCCESS);
550 if (result != ISC_R_SUCCESS)
554 result = dns_db_allrdatasets(db, node, ver,
555 (isc_stdtime_t) 0, &iter);
556 if (result != ISC_R_SUCCESS)
559 for (result = dns_rdatasetiter_first(iter);
560 result == ISC_R_SUCCESS;
561 result = dns_rdatasetiter_next(iter))
563 dns_rdataset_t rdataset;
565 dns_rdataset_init(&rdataset);
566 dns_rdatasetiter_current(iter, &rdataset);
568 result = (*action)(action_data, &rdataset);
570 dns_rdataset_disassociate(&rdataset);
571 if (result != ISC_R_SUCCESS)
572 goto cleanup_iterator;
574 if (result == ISC_R_NOMORE)
575 result = ISC_R_SUCCESS;
578 dns_rdatasetiter_destroy(&iter);
581 dns_db_detachnode(db, &node);
587 * For each RR of 'name' in 'ver' of 'db', call 'action'
588 * with the RR and 'action_data' as arguments. If the name
589 * does not exist, do nothing.
591 * If 'action' returns an error, abort iteration
592 * and return the error.
595 foreach_node_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
596 rr_func *rr_action, void *rr_action_data)
598 foreach_node_rr_ctx_t ctx;
599 ctx.rr_action = rr_action;
600 ctx.rr_action_data = rr_action_data;
601 return (foreach_rrset(db, ver, name,
602 foreach_node_rr_action, &ctx));
607 * For each of the RRs specified by 'db', 'ver', 'name', 'type',
608 * (which can be dns_rdatatype_any to match any type), and 'covers', call
609 * 'action' with the RR and 'action_data' as arguments. If the name
610 * does not exist, or if no RRset of the given type exists at the name,
613 * If 'action' returns an error, abort iteration and return the error.
616 foreach_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
617 dns_rdatatype_t type, dns_rdatatype_t covers, rr_func *rr_action,
618 void *rr_action_data)
623 dns_rdataset_t rdataset;
625 if (type == dns_rdatatype_any)
626 return (foreach_node_rr(db, ver, name,
627 rr_action, rr_action_data));
630 if (type == dns_rdatatype_nsec3 ||
631 (type == dns_rdatatype_rrsig && covers == dns_rdatatype_nsec3))
632 result = dns_db_findnsec3node(db, name, ISC_FALSE, &node);
634 result = dns_db_findnode(db, name, ISC_FALSE, &node);
635 if (result == ISC_R_NOTFOUND)
636 return (ISC_R_SUCCESS);
637 if (result != ISC_R_SUCCESS)
640 dns_rdataset_init(&rdataset);
641 result = dns_db_findrdataset(db, node, ver, type, covers,
642 (isc_stdtime_t) 0, &rdataset, NULL);
643 if (result == ISC_R_NOTFOUND) {
644 result = ISC_R_SUCCESS;
647 if (result != ISC_R_SUCCESS)
650 for (result = dns_rdataset_first(&rdataset);
651 result == ISC_R_SUCCESS;
652 result = dns_rdataset_next(&rdataset))
654 rr_t rr = { 0, DNS_RDATA_INIT };
655 dns_rdataset_current(&rdataset, &rr.rdata);
656 rr.ttl = rdataset.ttl;
657 result = (*rr_action)(rr_action_data, &rr);
658 if (result != ISC_R_SUCCESS)
659 goto cleanup_rdataset;
661 if (result != ISC_R_NOMORE)
662 goto cleanup_rdataset;
663 result = ISC_R_SUCCESS;
666 dns_rdataset_disassociate(&rdataset);
668 dns_db_detachnode(db, &node);
673 /**************************************************************************/
675 * Various tests on the database contents (for prerequisites, etc).
679 * Function type for predicate functions that compare a database RR 'db_rr'
680 * against an update RR 'update_rr'.
682 typedef isc_boolean_t rr_predicate(dns_rdata_t *update_rr, dns_rdata_t *db_rr);
685 * Helper function for rrset_exists().
688 rrset_exists_action(void *data, rr_t *rr) {
691 return (ISC_R_EXISTS);
695 * Utility macro for RR existence checking functions.
697 * If the variable 'result' has the value ISC_R_EXISTS or
698 * ISC_R_SUCCESS, set *exists to ISC_TRUE or ISC_FALSE,
699 * respectively, and return success.
701 * If 'result' has any other value, there was a failure.
702 * Return the failure result code and do not set *exists.
704 * This would be more readable as "do { if ... } while(0)",
705 * but that form generates tons of warnings on Solaris 2.6.
707 #define RETURN_EXISTENCE_FLAG \
708 return ((result == ISC_R_EXISTS) ? \
709 (*exists = ISC_TRUE, ISC_R_SUCCESS) : \
710 ((result == ISC_R_SUCCESS) ? \
711 (*exists = ISC_FALSE, ISC_R_SUCCESS) : \
715 * Set '*exists' to true iff an rrset of the given type exists,
716 * to false otherwise.
719 rrset_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
720 dns_rdatatype_t type, dns_rdatatype_t covers,
721 isc_boolean_t *exists)
724 result = foreach_rr(db, ver, name, type, covers,
725 rrset_exists_action, NULL);
726 RETURN_EXISTENCE_FLAG;
730 * Helper function for cname_incompatible_rrset_exists.
733 cname_compatibility_action(void *data, dns_rdataset_t *rrset) {
735 if (rrset->type != dns_rdatatype_cname &&
736 ! dns_rdatatype_isdnssec(rrset->type))
737 return (ISC_R_EXISTS);
738 return (ISC_R_SUCCESS);
742 * Check whether there is an rrset incompatible with adding a CNAME RR,
743 * i.e., anything but another CNAME (which can be replaced) or a
744 * DNSSEC RR (which can coexist).
746 * If such an incompatible rrset exists, set '*exists' to ISC_TRUE.
747 * Otherwise, set it to ISC_FALSE.
750 cname_incompatible_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
751 dns_name_t *name, isc_boolean_t *exists) {
753 result = foreach_rrset(db, ver, name,
754 cname_compatibility_action, NULL);
755 RETURN_EXISTENCE_FLAG;
759 * Helper function for rr_count().
762 count_rr_action(void *data, rr_t *rr) {
766 return (ISC_R_SUCCESS);
770 * Count the number of RRs of 'type' belonging to 'name' in 'ver' of 'db'.
773 rr_count(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
774 dns_rdatatype_t type, dns_rdatatype_t covers, int *countp)
777 return (foreach_rr(db, ver, name, type, covers,
778 count_rr_action, countp));
782 * Context struct and helper function for name_exists().
786 name_exists_action(void *data, dns_rdataset_t *rrset) {
789 return (ISC_R_EXISTS);
793 * Set '*exists' to true iff the given name exists, to false otherwise.
796 name_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
797 isc_boolean_t *exists)
800 result = foreach_rrset(db, ver, name,
801 name_exists_action, NULL);
802 RETURN_EXISTENCE_FLAG;
806 * 'ssu_check_t' is used to pass the arguments to
807 * dns_ssutable_checkrules() to the callback function
811 /* The ownername of the record to be updated. */
814 /* The signature's name if the request was signed. */
817 /* The address of the client if the request was received via TCP. */
818 isc_netaddr_t *tcpaddr;
820 /* The ssu table to check against. */
821 dns_ssutable_t *table;
823 /* the key used for TKEY requests */
828 ssu_checkrule(void *data, dns_rdataset_t *rrset) {
829 ssu_check_t *ssuinfo = data;
830 isc_boolean_t result;
833 * If we're deleting all records, it's ok to delete RRSIG and NSEC even
834 * if we're normally not allowed to.
836 if (rrset->type == dns_rdatatype_rrsig ||
837 rrset->type == dns_rdatatype_nsec)
838 return (ISC_R_SUCCESS);
839 result = dns_ssutable_checkrules(ssuinfo->table, ssuinfo->signer,
840 ssuinfo->name, ssuinfo->tcpaddr,
841 rrset->type, ssuinfo->key);
842 return (result == ISC_TRUE ? ISC_R_SUCCESS : ISC_R_FAILURE);
846 ssu_checkall(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
847 dns_ssutable_t *ssutable, dns_name_t *signer,
848 isc_netaddr_t *tcpaddr, dst_key_t *key)
854 ssuinfo.table = ssutable;
855 ssuinfo.signer = signer;
856 ssuinfo.tcpaddr = tcpaddr;
858 result = foreach_rrset(db, ver, name, ssu_checkrule, &ssuinfo);
859 return (ISC_TF(result == ISC_R_SUCCESS));
862 /**************************************************************************/
864 * Checking of "RRset exists (value dependent)" prerequisites.
866 * In the RFC2136 section 3.2.5, this is the pseudocode involving
867 * a variable called "temp", a mapping of <name, type> tuples to rrsets.
869 * Here, we represent the "temp" data structure as (non-minimal) "dns_diff_t"
870 * where each tuple has op==DNS_DIFFOP_EXISTS.
875 * Append a tuple asserting the existence of the RR with
876 * 'name' and 'rdata' to 'diff'.
879 temp_append(dns_diff_t *diff, dns_name_t *name, dns_rdata_t *rdata) {
881 dns_difftuple_t *tuple = NULL;
883 REQUIRE(DNS_DIFF_VALID(diff));
884 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_EXISTS,
885 name, 0, rdata, &tuple));
886 ISC_LIST_APPEND(diff->tuples, tuple, link);
892 * Compare two rdatasets represented as sorted lists of tuples.
893 * All list elements must have the same owner name and type.
894 * Return ISC_R_SUCCESS if the rdatasets are equal, rcode(dns_rcode_nxrrset)
898 temp_check_rrset(dns_difftuple_t *a, dns_difftuple_t *b) {
900 if (a == NULL || b == NULL)
902 INSIST(a->op == DNS_DIFFOP_EXISTS &&
903 b->op == DNS_DIFFOP_EXISTS);
904 INSIST(a->rdata.type == b->rdata.type);
905 INSIST(dns_name_equal(&a->name, &b->name));
906 if (dns_rdata_casecompare(&a->rdata, &b->rdata) != 0)
907 return (DNS_R_NXRRSET);
908 a = ISC_LIST_NEXT(a, link);
909 b = ISC_LIST_NEXT(b, link);
911 if (a != NULL || b != NULL)
912 return (DNS_R_NXRRSET);
913 return (ISC_R_SUCCESS);
917 * A comparison function defining the sorting order for the entries
918 * in the "temp" data structure. The major sort key is the owner name,
919 * followed by the type and rdata.
922 temp_order(const void *av, const void *bv) {
923 dns_difftuple_t const * const *ap = av;
924 dns_difftuple_t const * const *bp = bv;
925 dns_difftuple_t const *a = *ap;
926 dns_difftuple_t const *b = *bp;
928 r = dns_name_compare(&a->name, &b->name);
931 r = (b->rdata.type - a->rdata.type);
934 r = dns_rdata_casecompare(&a->rdata, &b->rdata);
939 * Check the "RRset exists (value dependent)" prerequisite information
940 * in 'temp' against the contents of the database 'db'.
942 * Return ISC_R_SUCCESS if the prerequisites are satisfied,
943 * rcode(dns_rcode_nxrrset) if not.
945 * 'temp' must be pre-sorted.
949 temp_check(isc_mem_t *mctx, dns_diff_t *temp, dns_db_t *db,
950 dns_dbversion_t *ver, dns_name_t *tmpname, dns_rdatatype_t *typep)
958 dns_diff_init(mctx, &trash);
961 * For each name and type in the prerequisites,
962 * construct a sorted rdata list of the corresponding
963 * database contents, and compare the lists.
965 t = ISC_LIST_HEAD(temp->tuples);
968 (void)dns_name_copy(name, tmpname, NULL);
969 *typep = t->rdata.type;
971 /* A new unique name begins here. */
973 result = dns_db_findnode(db, name, ISC_FALSE, &node);
974 if (result == ISC_R_NOTFOUND) {
975 dns_diff_clear(&trash);
976 return (DNS_R_NXRRSET);
978 if (result != ISC_R_SUCCESS) {
979 dns_diff_clear(&trash);
983 /* A new unique type begins here. */
984 while (t != NULL && dns_name_equal(&t->name, name)) {
985 dns_rdatatype_t type, covers;
986 dns_rdataset_t rdataset;
987 dns_diff_t d_rrs; /* Database RRs with
988 this name and type */
989 dns_diff_t u_rrs; /* Update RRs with
990 this name and type */
992 *typep = type = t->rdata.type;
993 if (type == dns_rdatatype_rrsig ||
994 type == dns_rdatatype_sig)
995 covers = dns_rdata_covers(&t->rdata);
996 else if (type == dns_rdatatype_any) {
997 dns_db_detachnode(db, &node);
998 dns_diff_clear(&trash);
999 return (DNS_R_NXRRSET);
1004 * Collect all database RRs for this name and type
1005 * onto d_rrs and sort them.
1007 dns_rdataset_init(&rdataset);
1008 result = dns_db_findrdataset(db, node, ver, type,
1009 covers, (isc_stdtime_t) 0,
1011 if (result != ISC_R_SUCCESS) {
1012 dns_db_detachnode(db, &node);
1013 dns_diff_clear(&trash);
1014 return (DNS_R_NXRRSET);
1017 dns_diff_init(mctx, &d_rrs);
1018 dns_diff_init(mctx, &u_rrs);
1020 for (result = dns_rdataset_first(&rdataset);
1021 result == ISC_R_SUCCESS;
1022 result = dns_rdataset_next(&rdataset))
1024 dns_rdata_t rdata = DNS_RDATA_INIT;
1025 dns_rdataset_current(&rdataset, &rdata);
1026 result = temp_append(&d_rrs, name, &rdata);
1027 if (result != ISC_R_SUCCESS)
1030 if (result != ISC_R_NOMORE)
1032 result = dns_diff_sort(&d_rrs, temp_order);
1033 if (result != ISC_R_SUCCESS)
1037 * Collect all update RRs for this name and type
1038 * onto u_rrs. No need to sort them here -
1039 * they are already sorted.
1042 dns_name_equal(&t->name, name) &&
1043 t->rdata.type == type)
1045 dns_difftuple_t *next =
1046 ISC_LIST_NEXT(t, link);
1047 ISC_LIST_UNLINK(temp->tuples, t, link);
1048 ISC_LIST_APPEND(u_rrs.tuples, t, link);
1052 /* Compare the two sorted lists. */
1053 result = temp_check_rrset(ISC_LIST_HEAD(u_rrs.tuples),
1054 ISC_LIST_HEAD(d_rrs.tuples));
1055 if (result != ISC_R_SUCCESS)
1059 * We are done with the tuples, but we can't free
1060 * them yet because "name" still points into one
1061 * of them. Move them on a temporary list.
1063 ISC_LIST_APPENDLIST(trash.tuples, u_rrs.tuples, link);
1064 ISC_LIST_APPENDLIST(trash.tuples, d_rrs.tuples, link);
1065 dns_rdataset_disassociate(&rdataset);
1070 dns_diff_clear(&d_rrs);
1071 dns_diff_clear(&u_rrs);
1072 dns_diff_clear(&trash);
1073 dns_rdataset_disassociate(&rdataset);
1074 dns_db_detachnode(db, &node);
1078 dns_db_detachnode(db, &node);
1081 dns_diff_clear(&trash);
1082 return (ISC_R_SUCCESS);
1085 /**************************************************************************/
1087 * Conditional deletion of RRs.
1091 * Context structure for delete_if().
1095 rr_predicate *predicate;
1097 dns_dbversion_t *ver;
1100 dns_rdata_t *update_rr;
1101 } conditional_delete_ctx_t;
1104 * Predicate functions for delete_if().
1108 * Return true iff 'db_rr' is neither a SOA nor an NS RR nor
1109 * an RRSIG nor an NSEC3PARAM nor a NSEC.
1111 static isc_boolean_t
1112 type_not_soa_nor_ns_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1114 return ((db_rr->type != dns_rdatatype_soa &&
1115 db_rr->type != dns_rdatatype_ns &&
1116 db_rr->type != dns_rdatatype_nsec3param &&
1117 db_rr->type != dns_rdatatype_rrsig &&
1118 db_rr->type != dns_rdatatype_nsec) ?
1119 ISC_TRUE : ISC_FALSE);
1123 * Return true iff 'db_rr' is neither a RRSIG nor a NSEC.
1125 static isc_boolean_t
1126 type_not_dnssec(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1128 return ((db_rr->type != dns_rdatatype_rrsig &&
1129 db_rr->type != dns_rdatatype_nsec) ?
1130 ISC_TRUE : ISC_FALSE);
1134 * Return true always.
1136 static isc_boolean_t
1137 true_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1144 * Return true iff the two RRs have identical rdata.
1146 static isc_boolean_t
1147 rr_equal_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1149 * XXXRTH This is not a problem, but we should consider creating
1150 * dns_rdata_equal() (that used dns_name_equal()), since it
1151 * would be faster. Not a priority.
1153 return (dns_rdata_casecompare(update_rr, db_rr) == 0 ?
1154 ISC_TRUE : ISC_FALSE);
1158 * Return true iff 'update_rr' should replace 'db_rr' according
1159 * to the special RFC2136 rules for CNAME, SOA, and WKS records.
1161 * RFC2136 does not mention NSEC or DNAME, but multiple NSECs or DNAMEs
1162 * make little sense, so we replace those, too.
1164 * Additionally replace RRSIG that have been generated by the same key
1165 * for the same type. This simplifies refreshing a offline KSK by not
1166 * requiring that the old RRSIG be deleted. It also simplifies key
1167 * rollover by only requiring that the new RRSIG be added.
1169 static isc_boolean_t
1170 replaces_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1171 dns_rdata_rrsig_t updatesig, dbsig;
1172 isc_result_t result;
1174 if (db_rr->type != update_rr->type)
1176 if (db_rr->type == dns_rdatatype_cname)
1178 if (db_rr->type == dns_rdatatype_dname)
1180 if (db_rr->type == dns_rdatatype_soa)
1182 if (db_rr->type == dns_rdatatype_nsec)
1184 if (db_rr->type == dns_rdatatype_rrsig) {
1186 * Replace existing RRSIG with the same keyid,
1187 * covered and algorithm.
1189 result = dns_rdata_tostruct(db_rr, &dbsig, NULL);
1190 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1191 result = dns_rdata_tostruct(update_rr, &updatesig, NULL);
1192 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1193 if (dbsig.keyid == updatesig.keyid &&
1194 dbsig.covered == updatesig.covered &&
1195 dbsig.algorithm == updatesig.algorithm)
1198 if (db_rr->type == dns_rdatatype_wks) {
1200 * Compare the address and protocol fields only. These
1201 * form the first five bytes of the RR data. Do a
1202 * raw binary comparison; unpacking the WKS RRs using
1203 * dns_rdata_tostruct() might be cleaner in some ways.
1205 INSIST(db_rr->length >= 5 && update_rr->length >= 5);
1206 return (memcmp(db_rr->data, update_rr->data, 5) == 0 ?
1207 ISC_TRUE : ISC_FALSE);
1210 if (db_rr->type == dns_rdatatype_nsec3param) {
1211 if (db_rr->length != update_rr->length)
1213 INSIST(db_rr->length >= 4 && update_rr->length >= 4);
1215 * Replace NSEC3PARAM records that only differ by the
1218 if (db_rr->data[0] == update_rr->data[0] &&
1219 memcmp(db_rr->data+2, update_rr->data+2,
1220 update_rr->length - 2) == 0)
1227 * Internal helper function for delete_if().
1230 delete_if_action(void *data, rr_t *rr) {
1231 conditional_delete_ctx_t *ctx = data;
1232 if ((*ctx->predicate)(ctx->update_rr, &rr->rdata)) {
1233 isc_result_t result;
1234 result = update_one_rr(ctx->db, ctx->ver, ctx->diff,
1235 DNS_DIFFOP_DEL, ctx->name,
1236 rr->ttl, &rr->rdata);
1239 return (ISC_R_SUCCESS);
1244 * Conditionally delete RRs. Apply 'predicate' to the RRs
1245 * specified by 'db', 'ver', 'name', and 'type' (which can
1246 * be dns_rdatatype_any to match any type). Delete those
1247 * RRs for which the predicate returns true, and log the
1248 * deletions in 'diff'.
1251 delete_if(rr_predicate *predicate, dns_db_t *db, dns_dbversion_t *ver,
1252 dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers,
1253 dns_rdata_t *update_rr, dns_diff_t *diff)
1255 conditional_delete_ctx_t ctx;
1256 ctx.predicate = predicate;
1261 ctx.update_rr = update_rr;
1262 return (foreach_rr(db, ver, name, type, covers,
1263 delete_if_action, &ctx));
1266 /**************************************************************************/
1268 * Prepare an RR for the addition of the new RR 'ctx->update_rr',
1269 * with TTL 'ctx->update_rr_ttl', to its rdataset, by deleting
1270 * the RRs if it is replaced by the new RR or has a conflicting TTL.
1271 * The necessary changes are appended to ctx->del_diff and ctx->add_diff;
1272 * we need to do all deletions before any additions so that we don't run
1273 * into transient states with conflicting TTLs.
1278 dns_dbversion_t *ver;
1281 dns_rdata_t *update_rr;
1282 dns_ttl_t update_rr_ttl;
1283 isc_boolean_t ignore_add;
1284 dns_diff_t del_diff;
1285 dns_diff_t add_diff;
1286 } add_rr_prepare_ctx_t;
1289 add_rr_prepare_action(void *data, rr_t *rr) {
1290 isc_result_t result = ISC_R_SUCCESS;
1291 add_rr_prepare_ctx_t *ctx = data;
1292 dns_difftuple_t *tuple = NULL;
1293 isc_boolean_t equal;
1296 * If the update RR is a "duplicate" of the update RR,
1297 * the update should be silently ignored.
1299 equal = ISC_TF(dns_rdata_casecompare(&rr->rdata, ctx->update_rr) == 0);
1300 if (equal && rr->ttl == ctx->update_rr_ttl) {
1301 ctx->ignore_add = ISC_TRUE;
1302 return (ISC_R_SUCCESS);
1306 * If this RR is "equal" to the update RR, it should
1307 * be deleted before the update RR is added.
1309 if (replaces_p(ctx->update_rr, &rr->rdata)) {
1310 CHECK(dns_difftuple_create(ctx->del_diff.mctx, DNS_DIFFOP_DEL,
1311 ctx->name, rr->ttl, &rr->rdata,
1313 dns_diff_append(&ctx->del_diff, &tuple);
1314 return (ISC_R_SUCCESS);
1318 * If this RR differs in TTL from the update RR,
1319 * its TTL must be adjusted.
1321 if (rr->ttl != ctx->update_rr_ttl) {
1322 CHECK(dns_difftuple_create(ctx->del_diff.mctx, DNS_DIFFOP_DEL,
1323 ctx->name, rr->ttl, &rr->rdata,
1325 dns_diff_append(&ctx->del_diff, &tuple);
1327 CHECK(dns_difftuple_create(ctx->add_diff.mctx,
1328 DNS_DIFFOP_ADD, ctx->name,
1330 &rr->rdata, &tuple));
1331 dns_diff_append(&ctx->add_diff, &tuple);
1338 /**************************************************************************/
1340 * Miscellaneous subroutines.
1344 * Extract a single update RR from 'section' of dynamic update message
1345 * 'msg', with consistency checking.
1347 * Stores the owner name, rdata, and TTL of the update RR at 'name',
1348 * 'rdata', and 'ttl', respectively.
1351 get_current_rr(dns_message_t *msg, dns_section_t section,
1352 dns_rdataclass_t zoneclass, dns_name_t **name,
1353 dns_rdata_t *rdata, dns_rdatatype_t *covers,
1354 dns_ttl_t *ttl, dns_rdataclass_t *update_class)
1356 dns_rdataset_t *rdataset;
1357 isc_result_t result;
1358 dns_message_currentname(msg, section, name);
1359 rdataset = ISC_LIST_HEAD((*name)->list);
1360 INSIST(rdataset != NULL);
1361 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
1362 *covers = rdataset->covers;
1363 *ttl = rdataset->ttl;
1364 result = dns_rdataset_first(rdataset);
1365 INSIST(result == ISC_R_SUCCESS);
1366 dns_rdataset_current(rdataset, rdata);
1367 INSIST(dns_rdataset_next(rdataset) == ISC_R_NOMORE);
1368 *update_class = rdata->rdclass;
1369 rdata->rdclass = zoneclass;
1373 * Increment the SOA serial number of database 'db', version 'ver'.
1374 * Replace the SOA record in the database, and log the
1379 * XXXRTH Failures in this routine will be worth logging, when
1380 * we have a logging system. Failure to find the zonename
1381 * or the SOA rdataset warrant at least an UNEXPECTED_ERROR().
1385 update_soa_serial(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
1386 isc_mem_t *mctx, dns_updatemethod_t method)
1388 dns_difftuple_t *deltuple = NULL;
1389 dns_difftuple_t *addtuple = NULL;
1390 isc_uint32_t serial;
1391 isc_result_t result;
1393 CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_DEL, &deltuple));
1394 CHECK(dns_difftuple_copy(deltuple, &addtuple));
1395 addtuple->op = DNS_DIFFOP_ADD;
1397 serial = dns_soa_getserial(&addtuple->rdata);
1398 serial = dns_update_soaserial(serial, method);
1399 dns_soa_setserial(serial, &addtuple->rdata);
1400 CHECK(do_one_tuple(&deltuple, db, ver, diff));
1401 CHECK(do_one_tuple(&addtuple, db, ver, diff));
1402 result = ISC_R_SUCCESS;
1405 if (addtuple != NULL)
1406 dns_difftuple_free(&addtuple);
1407 if (deltuple != NULL)
1408 dns_difftuple_free(&deltuple);
1413 * Check that the new SOA record at 'update_rdata' does not
1414 * illegally cause the SOA serial number to decrease or stay
1415 * unchanged relative to the existing SOA in 'db'.
1417 * Sets '*ok' to ISC_TRUE if the update is legal, ISC_FALSE if not.
1419 * William King points out that RFC2136 is inconsistent about
1420 * the case where the serial number stays unchanged:
1422 * section 3.4.2.2 requires a server to ignore a SOA update request
1423 * if the serial number on the update SOA is less_than_or_equal to
1424 * the zone SOA serial.
1426 * section 3.6 requires a server to ignore a SOA update request if
1427 * the serial is less_than the zone SOA serial.
1429 * Paul says 3.4.2.2 is correct.
1433 check_soa_increment(dns_db_t *db, dns_dbversion_t *ver,
1434 dns_rdata_t *update_rdata, isc_boolean_t *ok)
1436 isc_uint32_t db_serial;
1437 isc_uint32_t update_serial;
1438 isc_result_t result;
1440 update_serial = dns_soa_getserial(update_rdata);
1442 result = dns_db_getsoaserial(db, ver, &db_serial);
1443 if (result != ISC_R_SUCCESS)
1446 if (DNS_SERIAL_GE(db_serial, update_serial)) {
1452 return (ISC_R_SUCCESS);
1456 /**************************************************************************/
1458 * The actual update code in all its glory. We try to follow
1459 * the RFC2136 pseudocode as closely as possible.
1463 send_update_event(ns_client_t *client, dns_zone_t *zone) {
1464 isc_result_t result = ISC_R_SUCCESS;
1465 update_event_t *event = NULL;
1466 isc_task_t *zonetask = NULL;
1467 ns_client_t *evclient;
1469 event = (update_event_t *)
1470 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
1471 update_action, NULL, sizeof(*event));
1473 FAIL(ISC_R_NOMEMORY);
1475 event->result = ISC_R_SUCCESS;
1478 ns_client_attach(client, &evclient);
1479 INSIST(client->nupdates == 0);
1481 event->ev_arg = evclient;
1483 dns_zone_gettask(zone, &zonetask);
1484 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
1488 isc_event_free(ISC_EVENT_PTR(&event));
1493 respond(ns_client_t *client, isc_result_t result) {
1494 isc_result_t msg_result;
1496 msg_result = dns_message_reply(client->message, ISC_TRUE);
1497 if (msg_result != ISC_R_SUCCESS)
1499 client->message->rcode = dns_result_torcode(result);
1501 ns_client_send(client);
1505 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
1507 "could not create update response message: %s",
1508 isc_result_totext(msg_result));
1509 ns_client_next(client, msg_result);
1513 ns_update_start(ns_client_t *client, isc_result_t sigresult) {
1514 dns_message_t *request = client->message;
1515 isc_result_t result;
1516 dns_name_t *zonename;
1517 dns_rdataset_t *zone_rdataset;
1518 dns_zone_t *zone = NULL, *raw = NULL;
1521 * Interpret the zone section.
1523 result = dns_message_firstname(request, DNS_SECTION_ZONE);
1524 if (result != ISC_R_SUCCESS)
1525 FAILC(DNS_R_FORMERR, "update zone section empty");
1528 * The zone section must contain exactly one "question", and
1529 * it must be of type SOA.
1532 dns_message_currentname(request, DNS_SECTION_ZONE, &zonename);
1533 zone_rdataset = ISC_LIST_HEAD(zonename->list);
1534 if (zone_rdataset->type != dns_rdatatype_soa)
1535 FAILC(DNS_R_FORMERR,
1536 "update zone section contains non-SOA");
1537 if (ISC_LIST_NEXT(zone_rdataset, link) != NULL)
1538 FAILC(DNS_R_FORMERR,
1539 "update zone section contains multiple RRs");
1541 /* The zone section must have exactly one name. */
1542 result = dns_message_nextname(request, DNS_SECTION_ZONE);
1543 if (result != ISC_R_NOMORE)
1544 FAILC(DNS_R_FORMERR,
1545 "update zone section contains multiple RRs");
1547 result = dns_zt_find(client->view->zonetable, zonename, 0, NULL,
1549 if (result != ISC_R_SUCCESS)
1550 FAILC(DNS_R_NOTAUTH, "not authoritative for update zone");
1553 * If there is a raw (unsigned) zone associated with this
1554 * zone then it processes the UPDATE request.
1556 dns_zone_getraw(zone, &raw);
1558 dns_zone_detach(&zone);
1559 dns_zone_attach(raw, &zone);
1560 dns_zone_detach(&raw);
1563 switch(dns_zone_gettype(zone)) {
1564 case dns_zone_master:
1567 * We can now fail due to a bad signature as we now know
1568 * that we are the master.
1570 if (sigresult != ISC_R_SUCCESS)
1572 CHECK(send_update_event(client, zone));
1574 case dns_zone_slave:
1575 CHECK(checkupdateacl(client, dns_zone_getforwardacl(zone),
1576 "update forwarding", zonename, ISC_TRUE,
1578 CHECK(send_forward_event(client, zone));
1581 FAILC(DNS_R_NOTAUTH, "not authoritative for update zone");
1586 if (result == DNS_R_REFUSED) {
1587 INSIST(dns_zone_gettype(zone) == dns_zone_slave);
1588 inc_stats(zone, dns_nsstatscounter_updaterej);
1591 * We failed without having sent an update event to the zone.
1592 * We are still in the client task context, so we can
1593 * simply give an error response without switching tasks.
1595 respond(client, result);
1597 dns_zone_detach(&zone);
1601 * DS records are not allowed to exist without corresponding NS records,
1602 * RFC 3658, 2.2 Protocol Change,
1603 * "DS RRsets MUST NOT appear at non-delegation points or at a zone's apex".
1607 remove_orphaned_ds(dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff) {
1608 isc_result_t result;
1609 isc_boolean_t ns_exists;
1610 dns_difftuple_t *tupple;
1611 dns_diff_t temp_diff;
1613 dns_diff_init(diff->mctx, &temp_diff);
1615 for (tupple = ISC_LIST_HEAD(diff->tuples);
1617 tupple = ISC_LIST_NEXT(tupple, link)) {
1618 if (!((tupple->op == DNS_DIFFOP_DEL &&
1619 tupple->rdata.type == dns_rdatatype_ns) ||
1620 (tupple->op == DNS_DIFFOP_ADD &&
1621 tupple->rdata.type == dns_rdatatype_ds)))
1623 CHECK(rrset_exists(db, newver, &tupple->name,
1624 dns_rdatatype_ns, 0, &ns_exists));
1626 !dns_name_equal(&tupple->name, dns_db_origin(db)))
1628 CHECK(delete_if(true_p, db, newver, &tupple->name,
1629 dns_rdatatype_ds, 0, NULL, &temp_diff));
1631 result = ISC_R_SUCCESS;
1634 for (tupple = ISC_LIST_HEAD(temp_diff.tuples);
1636 tupple = ISC_LIST_HEAD(temp_diff.tuples)) {
1637 ISC_LIST_UNLINK(temp_diff.tuples, tupple, link);
1638 dns_diff_appendminimal(diff, &tupple);
1644 * This implements the post load integrity checks for mx records.
1647 check_mx(ns_client_t *client, dns_zone_t *zone,
1648 dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff)
1650 char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123.")];
1651 char ownerbuf[DNS_NAME_FORMATSIZE];
1652 char namebuf[DNS_NAME_FORMATSIZE];
1653 char altbuf[DNS_NAME_FORMATSIZE];
1655 dns_fixedname_t fixed;
1656 dns_name_t *foundname;
1659 isc_boolean_t ok = ISC_TRUE;
1660 isc_boolean_t isaddress;
1661 isc_result_t result;
1662 struct in6_addr addr6;
1663 struct in_addr addr;
1664 unsigned int options;
1666 dns_fixedname_init(&fixed);
1667 foundname = dns_fixedname_name(&fixed);
1668 dns_rdata_init(&rdata);
1669 options = dns_zone_getoptions(zone);
1671 for (t = ISC_LIST_HEAD(diff->tuples);
1673 t = ISC_LIST_NEXT(t, link)) {
1674 if (t->op != DNS_DIFFOP_ADD ||
1675 t->rdata.type != dns_rdatatype_mx)
1678 result = dns_rdata_tostruct(&t->rdata, &mx, NULL);
1679 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1681 * Check if we will error out if we attempt to reload the
1684 dns_name_format(&mx.mx, namebuf, sizeof(namebuf));
1685 dns_name_format(&t->name, ownerbuf, sizeof(ownerbuf));
1686 isaddress = ISC_FALSE;
1687 if ((options & DNS_RDATA_CHECKMX) != 0 &&
1688 strlcpy(tmp, namebuf, sizeof(tmp)) < sizeof(tmp)) {
1689 if (tmp[strlen(tmp) - 1] == '.')
1690 tmp[strlen(tmp) - 1] = '\0';
1691 if (inet_aton(tmp, &addr) == 1 ||
1692 inet_pton(AF_INET6, tmp, &addr6) == 1)
1693 isaddress = ISC_TRUE;
1696 if (isaddress && (options & DNS_RDATA_CHECKMXFAIL) != 0) {
1697 update_log(client, zone, ISC_LOG_ERROR,
1700 dns_result_totext(DNS_R_MXISADDRESS));
1702 } else if (isaddress) {
1703 update_log(client, zone, ISC_LOG_WARNING,
1704 "%s/MX: warning: '%s': %s",
1706 dns_result_totext(DNS_R_MXISADDRESS));
1710 * Check zone integrity checks.
1712 if ((options & DNS_ZONEOPT_CHECKINTEGRITY) == 0)
1714 result = dns_db_find(db, &mx.mx, newver, dns_rdatatype_a,
1715 0, 0, NULL, foundname, NULL, NULL);
1716 if (result == ISC_R_SUCCESS)
1719 if (result == DNS_R_NXRRSET) {
1720 result = dns_db_find(db, &mx.mx, newver,
1722 0, 0, NULL, foundname,
1724 if (result == ISC_R_SUCCESS)
1728 if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN) {
1729 update_log(client, zone, ISC_LOG_ERROR,
1730 "%s/MX '%s' has no address records "
1731 "(A or AAAA)", ownerbuf, namebuf);
1733 } else if (result == DNS_R_CNAME) {
1734 update_log(client, zone, ISC_LOG_ERROR,
1735 "%s/MX '%s' is a CNAME (illegal)",
1738 } else if (result == DNS_R_DNAME) {
1739 dns_name_format(foundname, altbuf, sizeof altbuf);
1740 update_log(client, zone, ISC_LOG_ERROR,
1741 "%s/MX '%s' is below a DNAME '%s' (illegal)",
1742 ownerbuf, namebuf, altbuf);
1746 return (ok ? ISC_R_SUCCESS : DNS_R_REFUSED);
1750 rr_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1751 const dns_rdata_t *rdata, isc_boolean_t *flag)
1753 dns_rdataset_t rdataset;
1754 dns_dbnode_t *node = NULL;
1755 isc_result_t result;
1757 dns_rdataset_init(&rdataset);
1758 if (rdata->type == dns_rdatatype_nsec3)
1759 CHECK(dns_db_findnsec3node(db, name, ISC_FALSE, &node));
1761 CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
1762 result = dns_db_findrdataset(db, node, ver, rdata->type, 0,
1763 (isc_stdtime_t) 0, &rdataset, NULL);
1764 if (result == ISC_R_NOTFOUND) {
1766 result = ISC_R_SUCCESS;
1770 for (result = dns_rdataset_first(&rdataset);
1771 result == ISC_R_SUCCESS;
1772 result = dns_rdataset_next(&rdataset)) {
1773 dns_rdata_t myrdata = DNS_RDATA_INIT;
1774 dns_rdataset_current(&rdataset, &myrdata);
1775 if (!dns_rdata_casecompare(&myrdata, rdata))
1778 dns_rdataset_disassociate(&rdataset);
1779 if (result == ISC_R_SUCCESS) {
1781 } else if (result == ISC_R_NOMORE) {
1783 result = ISC_R_SUCCESS;
1788 dns_db_detachnode(db, &node);
1793 get_iterations(dns_db_t *db, dns_dbversion_t *ver, dns_rdatatype_t privatetype,
1794 unsigned int *iterationsp)
1796 dns_dbnode_t *node = NULL;
1797 dns_rdata_nsec3param_t nsec3param;
1798 dns_rdataset_t rdataset;
1799 isc_result_t result;
1800 unsigned int iterations = 0;
1802 dns_rdataset_init(&rdataset);
1804 result = dns_db_getoriginnode(db, &node);
1805 if (result != ISC_R_SUCCESS)
1807 result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec3param,
1808 0, (isc_stdtime_t) 0, &rdataset, NULL);
1809 if (result == ISC_R_NOTFOUND)
1811 if (result != ISC_R_SUCCESS)
1814 for (result = dns_rdataset_first(&rdataset);
1815 result == ISC_R_SUCCESS;
1816 result = dns_rdataset_next(&rdataset)) {
1817 dns_rdata_t rdata = DNS_RDATA_INIT;
1818 dns_rdataset_current(&rdataset, &rdata);
1819 CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL));
1820 if ((nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0)
1822 if (nsec3param.iterations > iterations)
1823 iterations = nsec3param.iterations;
1825 if (result != ISC_R_NOMORE)
1828 dns_rdataset_disassociate(&rdataset);
1831 if (privatetype == 0)
1834 result = dns_db_findrdataset(db, node, ver, privatetype,
1835 0, (isc_stdtime_t) 0, &rdataset, NULL);
1836 if (result == ISC_R_NOTFOUND)
1838 if (result != ISC_R_SUCCESS)
1841 for (result = dns_rdataset_first(&rdataset);
1842 result == ISC_R_SUCCESS;
1843 result = dns_rdataset_next(&rdataset)) {
1844 unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
1845 dns_rdata_t private = DNS_RDATA_INIT;
1846 dns_rdata_t rdata = DNS_RDATA_INIT;
1848 dns_rdataset_current(&rdataset, &rdata);
1849 if (!dns_nsec3param_fromprivate(&private, &rdata,
1852 CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL));
1853 if ((nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0)
1855 if (nsec3param.iterations > iterations)
1856 iterations = nsec3param.iterations;
1858 if (result != ISC_R_NOMORE)
1862 *iterationsp = iterations;
1863 result = ISC_R_SUCCESS;
1867 dns_db_detachnode(db, &node);
1868 if (dns_rdataset_isassociated(&rdataset))
1869 dns_rdataset_disassociate(&rdataset);
1874 * Prevent the zone entering a inconsistent state where
1875 * NSEC only DNSKEYs are present with NSEC3 chains.
1878 check_dnssec(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1879 dns_dbversion_t *ver, dns_diff_t *diff)
1881 dns_difftuple_t *tuple;
1882 isc_boolean_t nseconly = ISC_FALSE, nsec3 = ISC_FALSE;
1883 isc_result_t result;
1884 unsigned int iterations = 0, max;
1885 dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
1887 /* Scan the tuples for an NSEC-only DNSKEY or an NSEC3PARAM */
1888 for (tuple = ISC_LIST_HEAD(diff->tuples);
1890 tuple = ISC_LIST_NEXT(tuple, link)) {
1891 if (tuple->op != DNS_DIFFOP_ADD)
1894 if (tuple->rdata.type == dns_rdatatype_dnskey) {
1896 alg = tuple->rdata.data[3];
1897 if (alg == DST_ALG_RSAMD5 || alg == DST_ALG_RSASHA1 ||
1898 alg == DST_ALG_DSA || alg == DST_ALG_ECC) {
1899 nseconly = ISC_TRUE;
1902 } else if (tuple->rdata.type == dns_rdatatype_nsec3param) {
1908 /* Check existing DB for NSEC-only DNSKEY */
1910 result = dns_nsec_nseconly(db, ver, &nseconly);
1913 * An NSEC3PARAM update can proceed without a DNSKEY (it
1914 * will trigger a delayed change), so we can ignore
1915 * ISC_R_NOTFOUND here.
1917 if (result == ISC_R_NOTFOUND)
1918 result = ISC_R_SUCCESS;
1923 /* Check existing DB for NSEC3 */
1925 CHECK(dns_nsec3_activex(db, ver, ISC_FALSE,
1926 privatetype, &nsec3));
1928 /* Refuse to allow NSEC3 with NSEC-only keys */
1929 if (nseconly && nsec3) {
1930 update_log(client, zone, ISC_LOG_ERROR,
1931 "NSEC only DNSKEYs and NSEC3 chains not allowed");
1932 result = DNS_R_REFUSED;
1936 /* Verify NSEC3 params */
1937 CHECK(get_iterations(db, ver, privatetype, &iterations));
1938 CHECK(dns_nsec3_maxiterations(db, ver, client->mctx, &max));
1939 if (max != 0 && iterations > max) {
1940 update_log(client, zone, ISC_LOG_ERROR,
1941 "too many NSEC3 iterations (%u) for "
1942 "weakest DNSKEY (%u)", iterations, max);
1943 result = DNS_R_REFUSED;
1952 * Delay NSEC3PARAM changes as they need to be applied to the whole zone.
1955 add_nsec3param_records(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1956 dns_dbversion_t *ver, dns_diff_t *diff)
1958 isc_result_t result = ISC_R_SUCCESS;
1959 dns_difftuple_t *tuple, *newtuple = NULL, *next;
1960 dns_rdata_t rdata = DNS_RDATA_INIT;
1961 unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE + 1];
1962 dns_diff_t temp_diff;
1965 dns_name_t *name = dns_zone_getorigin(zone);
1966 dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
1967 isc_uint32_t ttl = 0;
1968 isc_boolean_t ttl_good = ISC_FALSE;
1970 update_log(client, zone, ISC_LOG_DEBUG(3),
1971 "checking for NSEC3PARAM changes");
1973 dns_diff_init(diff->mctx, &temp_diff);
1976 * Extract NSEC3PARAM tuples from list.
1978 for (tuple = ISC_LIST_HEAD(diff->tuples);
1982 next = ISC_LIST_NEXT(tuple, link);
1984 if (tuple->rdata.type != dns_rdatatype_nsec3param ||
1985 !dns_name_equal(name, &tuple->name))
1987 ISC_LIST_UNLINK(diff->tuples, tuple, link);
1988 ISC_LIST_APPEND(temp_diff.tuples, tuple, link);
1992 * Extract TTL changes pairs, we don't need to convert these to
1995 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
1996 tuple != NULL; tuple = next) {
1997 if (tuple->op == DNS_DIFFOP_ADD) {
2000 * Any adds here will contain the final
2001 * NSEC3PARAM RRset TTL.
2004 ttl_good = ISC_TRUE;
2007 * Walk the temp_diff list looking for the
2008 * corresponding delete.
2010 next = ISC_LIST_HEAD(temp_diff.tuples);
2011 while (next != NULL) {
2012 unsigned char *next_data = next->rdata.data;
2013 unsigned char *tuple_data = tuple->rdata.data;
2014 if (next->op == DNS_DIFFOP_DEL &&
2015 next->rdata.length == tuple->rdata.length &&
2016 !memcmp(next_data, tuple_data,
2017 next->rdata.length)) {
2018 ISC_LIST_UNLINK(temp_diff.tuples, next,
2020 ISC_LIST_APPEND(diff->tuples, next,
2024 next = ISC_LIST_NEXT(next, link);
2027 * If we have not found a pair move onto the next
2031 next = ISC_LIST_NEXT(tuple, link);
2035 * Find the next tuple to be processed before
2036 * unlinking then complete moving the pair to 'diff'.
2038 next = ISC_LIST_NEXT(tuple, link);
2039 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2040 ISC_LIST_APPEND(diff->tuples, tuple, link);
2042 next = ISC_LIST_NEXT(tuple, link);
2046 * Preserve any ongoing changes from a BIND 9.6.x upgrade.
2048 * Any NSEC3PARAM records with flags other than OPTOUT named
2049 * in managing and should not be touched so revert such changes
2050 * taking into account any TTL change of the NSEC3PARAM RRset.
2052 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2053 tuple != NULL; tuple = next) {
2054 next = ISC_LIST_NEXT(tuple, link);
2055 if ((tuple->rdata.data[1] & ~DNS_NSEC3FLAG_OPTOUT) != 0) {
2057 * If we havn't had any adds then the tuple->ttl must
2058 * be the original ttl and should be used for any
2063 ttl_good = ISC_TRUE;
2065 op = (tuple->op == DNS_DIFFOP_DEL) ?
2066 DNS_DIFFOP_ADD : DNS_DIFFOP_DEL;
2067 CHECK(dns_difftuple_create(diff->mctx, op, name,
2070 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2071 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2072 dns_diff_appendminimal(diff, &tuple);
2077 * We now have just the actual changes to the NSEC3PARAM RRset.
2078 * Convert the adds to delayed adds and the deletions into delayed
2081 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2082 tuple != NULL; tuple = next) {
2084 * If we havn't had any adds then the tuple->ttl must be the
2085 * original ttl and should be used for any future changes.
2089 ttl_good = ISC_TRUE;
2091 if (tuple->op == DNS_DIFFOP_ADD) {
2092 isc_boolean_t nseconly = ISC_FALSE;
2095 * Look for any deletes which match this ADD ignoring
2096 * flags. We don't need to explictly remove them as
2097 * they will be removed a side effect of processing
2100 next = ISC_LIST_HEAD(temp_diff.tuples);
2101 while (next != NULL) {
2102 unsigned char *next_data = next->rdata.data;
2103 unsigned char *tuple_data = tuple->rdata.data;
2104 if (next->op != DNS_DIFFOP_DEL ||
2105 next->rdata.length != tuple->rdata.length ||
2106 next_data[0] != tuple_data[0] ||
2107 next_data[2] != tuple_data[2] ||
2108 next_data[3] != tuple_data[3] ||
2109 memcmp(next_data + 4, tuple_data + 4,
2110 tuple->rdata.length - 4)) {
2111 next = ISC_LIST_NEXT(next, link);
2114 ISC_LIST_UNLINK(temp_diff.tuples, next, link);
2115 ISC_LIST_APPEND(diff->tuples, next, link);
2116 next = ISC_LIST_HEAD(temp_diff.tuples);
2120 * Create a private-type record to signal that
2121 * we want a delayed NSEC3 chain add/delete
2123 dns_nsec3param_toprivate(&tuple->rdata, &rdata,
2124 privatetype, buf, sizeof(buf));
2125 buf[2] |= DNS_NSEC3FLAG_CREATE;
2128 * If the zone is not currently capable of
2129 * supporting an NSEC3 chain, then we set the
2130 * INITIAL flag to indicate that these parameters
2131 * are to be used later.
2133 result = dns_nsec_nseconly(db, ver, &nseconly);
2134 if (result == ISC_R_NOTFOUND || nseconly)
2135 buf[2] |= DNS_NSEC3FLAG_INITIAL;
2138 * See if this CREATE request already exists.
2140 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2143 CHECK(dns_difftuple_create(diff->mctx,
2147 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2151 * Remove any existing CREATE request to add an
2152 * otherwise indentical chain with a reversed
2155 buf[2] ^= DNS_NSEC3FLAG_OPTOUT;
2156 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2159 CHECK(dns_difftuple_create(diff->mctx,
2163 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2167 * Find the next tuple to be processed and remove the
2168 * temporary add record.
2170 next = ISC_LIST_NEXT(tuple, link);
2171 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_DEL,
2172 name, ttl, &tuple->rdata,
2174 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2175 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2176 dns_diff_appendminimal(diff, &tuple);
2177 dns_rdata_reset(&rdata);
2179 next = ISC_LIST_NEXT(tuple, link);
2182 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2183 tuple != NULL; tuple = next) {
2187 next = ISC_LIST_NEXT(tuple, link);
2189 * See if we already have a REMOVE request in progress.
2191 dns_nsec3param_toprivate(&tuple->rdata, &rdata, privatetype,
2194 buf[2] |= DNS_NSEC3FLAG_REMOVE | DNS_NSEC3FLAG_NONSEC;
2196 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2198 buf[2] &= ~DNS_NSEC3FLAG_NONSEC;
2199 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2203 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD,
2204 name, 0, &rdata, &newtuple));
2205 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2207 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name,
2208 ttl, &tuple->rdata, &newtuple));
2209 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2210 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2211 dns_diff_appendminimal(diff, &tuple);
2212 dns_rdata_reset(&rdata);
2215 result = ISC_R_SUCCESS;
2217 dns_diff_clear(&temp_diff);
2222 rollback_private(dns_db_t *db, dns_rdatatype_t privatetype,
2223 dns_dbversion_t *ver, dns_diff_t *diff)
2225 dns_diff_t temp_diff;
2227 dns_difftuple_t *tuple, *newtuple = NULL, *next;
2228 dns_name_t *name = dns_db_origin(db);
2229 isc_mem_t *mctx = diff->mctx;
2230 isc_result_t result;
2232 if (privatetype == 0)
2233 return (ISC_R_SUCCESS);
2235 dns_diff_init(mctx, &temp_diff);
2238 * Extract the changes to be rolled back.
2240 for (tuple = ISC_LIST_HEAD(diff->tuples);
2241 tuple != NULL; tuple = next) {
2243 next = ISC_LIST_NEXT(tuple, link);
2245 if (tuple->rdata.type != privatetype ||
2246 !dns_name_equal(name, &tuple->name))
2250 * Allow records which indicate that a zone has been
2251 * signed with a DNSKEY to be removed.
2253 if (tuple->op == DNS_DIFFOP_DEL &&
2254 tuple->rdata.length == 5 &&
2255 tuple->rdata.data[0] != 0 &&
2256 tuple->rdata.data[4] != 0)
2259 ISC_LIST_UNLINK(diff->tuples, tuple, link);
2260 ISC_LIST_PREPEND(temp_diff.tuples, tuple, link);
2264 * Rollback the changes.
2266 while ((tuple = ISC_LIST_HEAD(temp_diff.tuples)) != NULL) {
2267 op = (tuple->op == DNS_DIFFOP_DEL) ?
2268 DNS_DIFFOP_ADD : DNS_DIFFOP_DEL;
2269 CHECK(dns_difftuple_create(mctx, op, name, tuple->ttl,
2270 &tuple->rdata, &newtuple));
2271 CHECK(do_one_tuple(&newtuple, db, ver, &temp_diff));
2273 result = ISC_R_SUCCESS;
2276 dns_diff_clear(&temp_diff);
2281 * Add records to cause the delayed signing of the zone by added DNSKEY
2282 * to remove the RRSIG records generated by a deleted DNSKEY.
2285 add_signing_records(dns_db_t *db, dns_rdatatype_t privatetype,
2286 dns_dbversion_t *ver, dns_diff_t *diff)
2288 dns_difftuple_t *tuple, *newtuple = NULL, *next;
2289 dns_rdata_dnskey_t dnskey;
2290 dns_rdata_t rdata = DNS_RDATA_INIT;
2293 isc_result_t result = ISC_R_SUCCESS;
2295 unsigned char buf[5];
2296 dns_name_t *name = dns_db_origin(db);
2297 dns_diff_t temp_diff;
2299 dns_diff_init(diff->mctx, &temp_diff);
2302 * Extract the DNSKEY tuples from the list.
2304 for (tuple = ISC_LIST_HEAD(diff->tuples);
2305 tuple != NULL; tuple = next) {
2307 next = ISC_LIST_NEXT(tuple, link);
2309 if (tuple->rdata.type != dns_rdatatype_dnskey)
2312 ISC_LIST_UNLINK(diff->tuples, tuple, link);
2313 ISC_LIST_APPEND(temp_diff.tuples, tuple, link);
2317 * Extract TTL changes pairs, we don't need signing records for these.
2319 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2320 tuple != NULL; tuple = next) {
2321 if (tuple->op == DNS_DIFFOP_ADD) {
2323 * Walk the temp_diff list looking for the
2324 * corresponding delete.
2326 next = ISC_LIST_HEAD(temp_diff.tuples);
2327 while (next != NULL) {
2328 unsigned char *next_data = next->rdata.data;
2329 unsigned char *tuple_data = tuple->rdata.data;
2330 if (next->op == DNS_DIFFOP_DEL &&
2331 dns_name_equal(&tuple->name, &next->name) &&
2332 next->rdata.length == tuple->rdata.length &&
2333 !memcmp(next_data, tuple_data,
2334 next->rdata.length)) {
2335 ISC_LIST_UNLINK(temp_diff.tuples, next,
2337 ISC_LIST_APPEND(diff->tuples, next,
2341 next = ISC_LIST_NEXT(next, link);
2344 * If we have not found a pair move onto the next
2348 next = ISC_LIST_NEXT(tuple, link);
2352 * Find the next tuple to be processed before
2353 * unlinking then complete moving the pair to 'diff'.
2355 next = ISC_LIST_NEXT(tuple, link);
2356 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2357 ISC_LIST_APPEND(diff->tuples, tuple, link);
2359 next = ISC_LIST_NEXT(tuple, link);
2363 * Process the remaining DNSKEY entries.
2365 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2367 tuple = ISC_LIST_HEAD(temp_diff.tuples)) {
2369 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2370 ISC_LIST_APPEND(diff->tuples, tuple, link);
2372 result = dns_rdata_tostruct(&tuple->rdata, &dnskey, NULL);
2373 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2375 (DNS_KEYFLAG_OWNERMASK|DNS_KEYTYPE_NOAUTH))
2376 != DNS_KEYOWNER_ZONE)
2379 dns_rdata_toregion(&tuple->rdata, &r);
2381 keyid = dst_region_computeid(&r, dnskey.algorithm);
2383 buf[0] = dnskey.algorithm;
2384 buf[1] = (keyid & 0xff00) >> 8;
2385 buf[2] = (keyid & 0xff);
2386 buf[3] = (tuple->op == DNS_DIFFOP_ADD) ? 0 : 1;
2389 rdata.length = sizeof(buf);
2390 rdata.type = privatetype;
2391 rdata.rdclass = tuple->rdata.rdclass;
2393 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2396 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD,
2397 name, 0, &rdata, &newtuple));
2398 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2399 INSIST(newtuple == NULL);
2401 * Remove any record which says this operation has already
2405 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2407 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_DEL,
2408 name, 0, &rdata, &newtuple));
2409 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2410 INSIST(newtuple == NULL);
2415 dns_diff_clear(&temp_diff);
2419 static isc_boolean_t
2420 isdnssec(dns_db_t *db, dns_dbversion_t *ver, dns_rdatatype_t privatetype) {
2421 isc_result_t result;
2422 isc_boolean_t build_nsec, build_nsec3;
2424 if (dns_db_issecure(db))
2427 result = dns_private_chains(db, ver, privatetype,
2428 &build_nsec, &build_nsec3);
2429 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2430 return (build_nsec || build_nsec3);
2434 update_action(isc_task_t *task, isc_event_t *event) {
2435 update_event_t *uev = (update_event_t *) event;
2436 dns_zone_t *zone = uev->zone;
2437 ns_client_t *client = (ns_client_t *)event->ev_arg;
2439 isc_result_t result;
2440 dns_db_t *db = NULL;
2441 dns_dbversion_t *oldver = NULL;
2442 dns_dbversion_t *ver = NULL;
2443 dns_diff_t diff; /* Pending updates. */
2444 dns_diff_t temp; /* Pending RR existence assertions. */
2445 isc_boolean_t soa_serial_changed = ISC_FALSE;
2446 isc_mem_t *mctx = client->mctx;
2447 dns_rdatatype_t covers;
2448 dns_message_t *request = client->message;
2449 dns_rdataclass_t zoneclass;
2450 dns_name_t *zonename;
2451 dns_ssutable_t *ssutable = NULL;
2452 dns_fixedname_t tmpnamefixed;
2453 dns_name_t *tmpname = NULL;
2454 unsigned int options;
2455 dns_difftuple_t *tuple;
2456 dns_rdata_dnskey_t dnskey;
2457 isc_boolean_t had_dnskey;
2458 dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
2460 INSIST(event->ev_type == DNS_EVENT_UPDATE);
2462 dns_diff_init(mctx, &diff);
2463 dns_diff_init(mctx, &temp);
2465 CHECK(dns_zone_getdb(zone, &db));
2466 zonename = dns_db_origin(db);
2467 zoneclass = dns_db_class(db);
2468 dns_zone_getssutable(zone, &ssutable);
2471 * Update message processing can leak record existance information
2472 * so check that we are allowed to query this zone. Additionally
2473 * if we would refuse all updates for this zone we bail out here.
2475 CHECK(checkqueryacl(client, dns_zone_getqueryacl(zone), zonename,
2476 dns_zone_getupdateacl(zone), ssutable));
2479 * Get old and new versions now that queryacl has been checked.
2481 dns_db_currentversion(db, &oldver);
2482 CHECK(dns_db_newversion(db, &ver));
2485 * Check prerequisites.
2488 for (result = dns_message_firstname(request, DNS_SECTION_PREREQUISITE);
2489 result == ISC_R_SUCCESS;
2490 result = dns_message_nextname(request, DNS_SECTION_PREREQUISITE))
2492 dns_name_t *name = NULL;
2493 dns_rdata_t rdata = DNS_RDATA_INIT;
2495 dns_rdataclass_t update_class;
2498 get_current_rr(request, DNS_SECTION_PREREQUISITE, zoneclass,
2499 &name, &rdata, &covers, &ttl, &update_class);
2502 PREREQFAILC(DNS_R_FORMERR,
2503 "prerequisite TTL is not zero");
2505 if (! dns_name_issubdomain(name, zonename))
2506 PREREQFAILN(DNS_R_NOTZONE, name,
2507 "prerequisite name is out of zone");
2509 if (update_class == dns_rdataclass_any) {
2510 if (rdata.length != 0)
2511 PREREQFAILC(DNS_R_FORMERR,
2512 "class ANY prerequisite "
2513 "RDATA is not empty");
2514 if (rdata.type == dns_rdatatype_any) {
2515 CHECK(name_exists(db, ver, name, &flag));
2517 PREREQFAILN(DNS_R_NXDOMAIN, name,
2523 CHECK(rrset_exists(db, ver, name,
2524 rdata.type, covers, &flag));
2526 /* RRset does not exist. */
2527 PREREQFAILNT(DNS_R_NXRRSET, name, rdata.type,
2528 "'rrset exists (value independent)' "
2529 "prerequisite not satisfied");
2532 } else if (update_class == dns_rdataclass_none) {
2533 if (rdata.length != 0)
2534 PREREQFAILC(DNS_R_FORMERR,
2535 "class NONE prerequisite "
2536 "RDATA is not empty");
2537 if (rdata.type == dns_rdatatype_any) {
2538 CHECK(name_exists(db, ver, name, &flag));
2540 PREREQFAILN(DNS_R_YXDOMAIN, name,
2541 "'name not in use' "
2546 CHECK(rrset_exists(db, ver, name,
2547 rdata.type, covers, &flag));
2550 PREREQFAILNT(DNS_R_YXRRSET, name,
2552 "'rrset does not exist' "
2557 } else if (update_class == zoneclass) {
2558 /* "temp<rr.name, rr.type> += rr;" */
2559 result = temp_append(&temp, name, &rdata);
2560 if (result != ISC_R_SUCCESS) {
2561 UNEXPECTED_ERROR(__FILE__, __LINE__,
2562 "temp entry creation failed: %s",
2563 dns_result_totext(result));
2564 FAIL(ISC_R_UNEXPECTED);
2567 PREREQFAILC(DNS_R_FORMERR, "malformed prerequisite");
2570 if (result != ISC_R_NOMORE)
2574 * Perform the final check of the "rrset exists (value dependent)"
2577 if (ISC_LIST_HEAD(temp.tuples) != NULL) {
2578 dns_rdatatype_t type;
2581 * Sort the prerequisite records by owner name,
2584 result = dns_diff_sort(&temp, temp_order);
2585 if (result != ISC_R_SUCCESS)
2586 FAILC(result, "'RRset exists (value dependent)' "
2587 "prerequisite not satisfied");
2589 dns_fixedname_init(&tmpnamefixed);
2590 tmpname = dns_fixedname_name(&tmpnamefixed);
2591 result = temp_check(mctx, &temp, db, ver, tmpname, &type);
2592 if (result != ISC_R_SUCCESS)
2593 FAILNT(result, tmpname, type,
2594 "'RRset exists (value dependent)' "
2595 "prerequisite not satisfied");
2598 update_log(client, zone, LOGLEVEL_DEBUG,
2599 "prerequisites are OK");
2602 * Check Requestor's Permissions. It seems a bit silly to do this
2603 * only after prerequisite testing, but that is what RFC2136 says.
2605 if (ssutable == NULL)
2606 CHECK(checkupdateacl(client, dns_zone_getupdateacl(zone),
2607 "update", zonename, ISC_FALSE, ISC_FALSE));
2608 else if (client->signer == NULL && !TCPCLIENT(client))
2609 CHECK(checkupdateacl(client, NULL, "update", zonename,
2610 ISC_FALSE, ISC_TRUE));
2612 if (dns_zone_getupdatedisabled(zone))
2613 FAILC(DNS_R_REFUSED, "dynamic update temporarily disabled "
2614 "because the zone is frozen. Use "
2615 "'rndc thaw' to re-enable updates.");
2618 * Perform the Update Section Prescan.
2621 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2622 result == ISC_R_SUCCESS;
2623 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2625 dns_name_t *name = NULL;
2626 dns_rdata_t rdata = DNS_RDATA_INIT;
2628 dns_rdataclass_t update_class;
2629 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2630 &name, &rdata, &covers, &ttl, &update_class);
2632 if (! dns_name_issubdomain(name, zonename))
2633 FAILC(DNS_R_NOTZONE,
2634 "update RR is outside zone");
2635 if (update_class == zoneclass) {
2637 * Check for meta-RRs. The RFC2136 pseudocode says
2638 * check for ANY|AXFR|MAILA|MAILB, but the text adds
2639 * "or any other QUERY metatype"
2641 if (dns_rdatatype_ismeta(rdata.type)) {
2642 FAILC(DNS_R_FORMERR,
2643 "meta-RR in update");
2645 result = dns_zone_checknames(zone, name, &rdata);
2646 if (result != ISC_R_SUCCESS)
2647 FAIL(DNS_R_REFUSED);
2648 } else if (update_class == dns_rdataclass_any) {
2649 if (ttl != 0 || rdata.length != 0 ||
2650 (dns_rdatatype_ismeta(rdata.type) &&
2651 rdata.type != dns_rdatatype_any))
2652 FAILC(DNS_R_FORMERR,
2653 "meta-RR in update");
2654 } else if (update_class == dns_rdataclass_none) {
2656 dns_rdatatype_ismeta(rdata.type))
2657 FAILC(DNS_R_FORMERR,
2658 "meta-RR in update");
2660 update_log(client, zone, ISC_LOG_WARNING,
2661 "update RR has incorrect class %d",
2663 FAIL(DNS_R_FORMERR);
2667 * draft-ietf-dnsind-simple-secure-update-01 says
2668 * "Unlike traditional dynamic update, the client
2669 * is forbidden from updating NSEC records."
2671 if (rdata.type == dns_rdatatype_nsec3) {
2672 FAILC(DNS_R_REFUSED,
2673 "explicit NSEC3 updates are not allowed "
2675 } else if (rdata.type == dns_rdatatype_nsec) {
2676 FAILC(DNS_R_REFUSED,
2677 "explicit NSEC updates are not allowed "
2679 } else if (rdata.type == dns_rdatatype_rrsig &&
2680 !dns_name_equal(name, zonename)) {
2681 FAILC(DNS_R_REFUSED,
2682 "explicit RRSIG updates are currently "
2683 "not supported in secure zones except "
2687 if (ssutable != NULL) {
2688 isc_netaddr_t *tcpaddr, netaddr;
2689 dst_key_t *tsigkey = NULL;
2691 * If this is a TCP connection then pass the
2692 * address of the client through for tcp-self
2693 * and 6to4-self otherwise pass NULL. This
2694 * provides weak address based authentication.
2696 if (TCPCLIENT(client)) {
2697 isc_netaddr_fromsockaddr(&netaddr,
2703 if (client->message->tsigkey != NULL)
2704 tsigkey = client->message->tsigkey->key;
2706 if (rdata.type != dns_rdatatype_any) {
2707 if (!dns_ssutable_checkrules(ssutable,
2712 FAILC(DNS_R_REFUSED,
2713 "rejected by secure update");
2715 if (!ssu_checkall(db, ver, name, ssutable,
2716 client->signer, tcpaddr,
2718 FAILC(DNS_R_REFUSED,
2719 "rejected by secure update");
2723 if (result != ISC_R_NOMORE)
2726 update_log(client, zone, LOGLEVEL_DEBUG,
2727 "update section prescan OK");
2730 * Process the Update Section.
2733 options = dns_zone_getoptions(zone);
2734 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2735 result == ISC_R_SUCCESS;
2736 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2738 dns_name_t *name = NULL;
2739 dns_rdata_t rdata = DNS_RDATA_INIT;
2741 dns_rdataclass_t update_class;
2744 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2745 &name, &rdata, &covers, &ttl, &update_class);
2747 if (update_class == zoneclass) {
2750 * RFC1123 doesn't allow MF and MD in master zones. */
2751 if (rdata.type == dns_rdatatype_md ||
2752 rdata.type == dns_rdatatype_mf) {
2753 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2755 dns_rdatatype_format(rdata.type, typebuf,
2757 update_log(client, zone, LOGLEVEL_PROTOCOL,
2758 "attempt to add %s ignored",
2762 if ((rdata.type == dns_rdatatype_ns ||
2763 rdata.type == dns_rdatatype_dname) &&
2764 dns_name_iswildcard(name)) {
2765 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2767 dns_rdatatype_format(rdata.type, typebuf,
2769 update_log(client, zone,
2771 "attempt to add wildcard %s record "
2772 "ignored", typebuf);
2775 if (rdata.type == dns_rdatatype_cname) {
2776 CHECK(cname_incompatible_rrset_exists(db, ver,
2780 update_log(client, zone,
2782 "attempt to add CNAME "
2783 "alongside non-CNAME "
2788 CHECK(rrset_exists(db, ver, name,
2789 dns_rdatatype_cname, 0,
2792 ! dns_rdatatype_isdnssec(rdata.type))
2794 update_log(client, zone,
2796 "attempt to add non-CNAME "
2797 "alongside CNAME ignored");
2801 if (rdata.type == dns_rdatatype_soa) {
2803 CHECK(rrset_exists(db, ver, name,
2804 dns_rdatatype_soa, 0,
2807 update_log(client, zone,
2809 "attempt to create 2nd "
2813 CHECK(check_soa_increment(db, ver, &rdata,
2816 update_log(client, zone,
2818 "SOA update failed to "
2819 "increment serial, "
2823 soa_serial_changed = ISC_TRUE;
2826 if (rdata.type == privatetype) {
2827 update_log(client, zone, LOGLEVEL_PROTOCOL,
2828 "attempt to add a private type "
2829 "(%u) record rejected internal "
2830 "use only", privatetype);
2834 if (rdata.type == dns_rdatatype_nsec3param) {
2836 * Ignore attempts to add NSEC3PARAM records
2837 * with any flags other than OPTOUT.
2839 if ((rdata.data[1] & ~DNS_NSEC3FLAG_OPTOUT) != 0) {
2840 update_log(client, zone,
2842 "attempt to add NSEC3PARAM "
2843 "record with non OPTOUT "
2849 if ((options & DNS_ZONEOPT_CHECKWILDCARD) != 0 &&
2850 dns_name_internalwildcard(name)) {
2851 char namestr[DNS_NAME_FORMATSIZE];
2852 dns_name_format(name, namestr,
2854 update_log(client, zone, LOGLEVEL_PROTOCOL,
2855 "warning: ownername '%s' contains "
2856 "a non-terminal wildcard", namestr);
2859 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) {
2860 char namestr[DNS_NAME_FORMATSIZE];
2861 char typestr[DNS_RDATATYPE_FORMATSIZE];
2862 dns_name_format(name, namestr,
2864 dns_rdatatype_format(rdata.type, typestr,
2866 update_log(client, zone, LOGLEVEL_PROTOCOL,
2867 "adding an RR at '%s' %s",
2871 /* Prepare the affected RRset for the addition. */
2873 add_rr_prepare_ctx_t ctx;
2878 ctx.update_rr = &rdata;
2879 ctx.update_rr_ttl = ttl;
2880 ctx.ignore_add = ISC_FALSE;
2881 dns_diff_init(mctx, &ctx.del_diff);
2882 dns_diff_init(mctx, &ctx.add_diff);
2883 CHECK(foreach_rr(db, ver, name, rdata.type,
2884 covers, add_rr_prepare_action,
2887 if (ctx.ignore_add) {
2888 dns_diff_clear(&ctx.del_diff);
2889 dns_diff_clear(&ctx.add_diff);
2891 CHECK(do_diff(&ctx.del_diff, db, ver,
2893 CHECK(do_diff(&ctx.add_diff, db, ver,
2895 CHECK(update_one_rr(db, ver, &diff,
2897 name, ttl, &rdata));
2900 } else if (update_class == dns_rdataclass_any) {
2901 if (rdata.type == dns_rdatatype_any) {
2902 if (isc_log_wouldlog(ns_g_lctx,
2905 char namestr[DNS_NAME_FORMATSIZE];
2906 dns_name_format(name, namestr,
2908 update_log(client, zone,
2910 "delete all rrsets from "
2911 "name '%s'", namestr);
2913 if (dns_name_equal(name, zonename)) {
2914 CHECK(delete_if(type_not_soa_nor_ns_p,
2916 dns_rdatatype_any, 0,
2919 CHECK(delete_if(type_not_dnssec,
2921 dns_rdatatype_any, 0,
2924 } else if (dns_name_equal(name, zonename) &&
2925 (rdata.type == dns_rdatatype_soa ||
2926 rdata.type == dns_rdatatype_ns)) {
2927 update_log(client, zone, LOGLEVEL_PROTOCOL,
2928 "attempt to delete all SOA "
2929 "or NS records ignored");
2932 if (isc_log_wouldlog(ns_g_lctx,
2935 char namestr[DNS_NAME_FORMATSIZE];
2936 char typestr[DNS_RDATATYPE_FORMATSIZE];
2937 dns_name_format(name, namestr,
2939 dns_rdatatype_format(rdata.type,
2942 update_log(client, zone,
2944 "deleting rrset at '%s' %s",
2947 CHECK(delete_if(true_p, db, ver, name,
2948 rdata.type, covers, &rdata,
2951 } else if (update_class == dns_rdataclass_none) {
2952 char namestr[DNS_NAME_FORMATSIZE];
2953 char typestr[DNS_RDATATYPE_FORMATSIZE];
2956 * The (name == zonename) condition appears in
2957 * RFC2136 3.4.2.4 but is missing from the pseudocode.
2959 if (dns_name_equal(name, zonename)) {
2960 if (rdata.type == dns_rdatatype_soa) {
2961 update_log(client, zone,
2963 "attempt to delete SOA "
2967 if (rdata.type == dns_rdatatype_ns) {
2969 CHECK(rr_count(db, ver, name,
2973 update_log(client, zone,
2982 dns_name_format(name, namestr, sizeof(namestr));
2983 dns_rdatatype_format(rdata.type, typestr,
2985 update_log(client, zone, LOGLEVEL_PROTOCOL,
2986 "deleting an RR at %s %s", namestr, typestr);
2987 CHECK(delete_if(rr_equal_p, db, ver, name, rdata.type,
2988 covers, &rdata, &diff));
2991 if (result != ISC_R_NOMORE)
2995 * Check that any changes to DNSKEY/NSEC3PARAM records make sense.
2996 * If they don't then back out all changes to DNSKEY/NSEC3PARAM
2999 if (! ISC_LIST_EMPTY(diff.tuples))
3000 CHECK(check_dnssec(client, zone, db, ver, &diff));
3002 if (! ISC_LIST_EMPTY(diff.tuples)) {
3003 unsigned int errors = 0;
3004 CHECK(dns_zone_nscheck(zone, db, ver, &errors));
3006 update_log(client, zone, LOGLEVEL_PROTOCOL,
3007 "update rejected: post update name server "
3008 "sanity check failed");
3009 result = DNS_R_REFUSED;
3015 * If any changes were made, increment the SOA serial number,
3016 * update RRSIGs and NSECs (if zone is secure), and write the update
3019 if (! ISC_LIST_EMPTY(diff.tuples)) {
3021 dns_journal_t *journal;
3022 isc_boolean_t has_dnskey;
3025 * Increment the SOA serial, but only if it was not
3026 * changed as a result of an update operation.
3028 if (! soa_serial_changed) {
3029 CHECK(update_soa_serial(db, ver, &diff, mctx,
3030 dns_zone_getserialupdatemethod(zone)));
3033 CHECK(check_mx(client, zone, db, ver, &diff));
3035 CHECK(remove_orphaned_ds(db, ver, &diff));
3037 CHECK(rrset_exists(db, ver, zonename, dns_rdatatype_dnskey,
3040 #define ALLOW_SECURE_TO_INSECURE(zone) \
3041 ((dns_zone_getoptions(zone) & DNS_ZONEOPT_SECURETOINSECURE) != 0)
3043 if (!ALLOW_SECURE_TO_INSECURE(zone)) {
3044 CHECK(rrset_exists(db, oldver, zonename,
3045 dns_rdatatype_dnskey, 0,
3047 if (had_dnskey && !has_dnskey) {
3048 update_log(client, zone, LOGLEVEL_PROTOCOL,
3049 "update rejected: all DNSKEY "
3050 "records removed and "
3051 "'dnssec-secure-to-insecure' "
3053 result = DNS_R_REFUSED;
3058 CHECK(rollback_private(db, privatetype, ver, &diff));
3060 CHECK(add_signing_records(db, privatetype, ver, &diff));
3062 CHECK(add_nsec3param_records(client, zone, db, ver, &diff));
3064 if (had_dnskey && !has_dnskey) {
3066 * We are transitioning from secure to insecure.
3067 * Cause all NSEC3 chains to be deleted. When the
3068 * the last signature for the DNSKEY records are
3069 * remove any NSEC chain present will also be removed.
3071 CHECK(dns_nsec3param_deletechains(db, ver, zone,
3073 } else if (has_dnskey && isdnssec(db, ver, privatetype)) {
3074 isc_uint32_t interval;
3075 dns_update_log_t log;
3077 interval = dns_zone_getsigvalidityinterval(zone);
3078 log.func = update_log_cb;
3080 result = dns_update_signatures(&log, zone, db, oldver,
3081 ver, &diff, interval);
3083 if (result != ISC_R_SUCCESS) {
3084 update_log(client, zone,
3086 "RRSIG/NSEC/NSEC3 update failed: %s",
3087 isc_result_totext(result));
3092 journalfile = dns_zone_getjournal(zone);
3093 if (journalfile != NULL) {
3094 update_log(client, zone, LOGLEVEL_DEBUG,
3095 "writing journal %s", journalfile);
3098 result = dns_journal_open(mctx, journalfile,
3099 DNS_JOURNAL_CREATE, &journal);
3100 if (result != ISC_R_SUCCESS)
3101 FAILS(result, "journal open failed");
3103 result = dns_journal_write_transaction(journal, &diff);
3104 if (result != ISC_R_SUCCESS) {
3105 dns_journal_destroy(&journal);
3106 FAILS(result, "journal write failed");
3109 dns_journal_destroy(&journal);
3113 * XXXRTH Just a note that this committing code will have
3114 * to change to handle databases that need two-phase
3115 * commit, but this isn't a priority.
3117 update_log(client, zone, LOGLEVEL_DEBUG,
3118 "committing update transaction");
3120 dns_db_closeversion(db, &ver, ISC_TRUE);
3123 * Mark the zone as dirty so that it will be written to disk.
3125 dns_zone_markdirty(zone);
3128 * Notify slaves of the change we just made.
3130 dns_zone_notify(zone);
3133 * Cause the zone to be signed with the key that we
3134 * have just added or have the corresponding signatures
3137 * Note: we are already committed to this course of action.
3139 for (tuple = ISC_LIST_HEAD(diff.tuples);
3141 tuple = ISC_LIST_NEXT(tuple, link)) {
3143 dns_secalg_t algorithm;
3146 if (tuple->rdata.type != dns_rdatatype_dnskey)
3149 dns_rdata_tostruct(&tuple->rdata, &dnskey, NULL);
3151 (DNS_KEYFLAG_OWNERMASK|DNS_KEYTYPE_NOAUTH))
3152 != DNS_KEYOWNER_ZONE)
3155 dns_rdata_toregion(&tuple->rdata, &r);
3156 algorithm = dnskey.algorithm;
3157 keyid = dst_region_computeid(&r, algorithm);
3159 result = dns_zone_signwithkey(zone, algorithm, keyid,
3160 ISC_TF(tuple->op == DNS_DIFFOP_DEL));
3161 if (result != ISC_R_SUCCESS) {
3162 update_log(client, zone, ISC_LOG_ERROR,
3163 "dns_zone_signwithkey failed: %s",
3164 dns_result_totext(result));
3169 * Cause the zone to add/delete NSEC3 chains for the
3170 * deferred NSEC3PARAM changes.
3172 * Note: we are already committed to this course of action.
3174 for (tuple = ISC_LIST_HEAD(diff.tuples);
3176 tuple = ISC_LIST_NEXT(tuple, link)) {
3177 unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
3178 dns_rdata_t rdata = DNS_RDATA_INIT;
3179 dns_rdata_nsec3param_t nsec3param;
3181 if (tuple->rdata.type != privatetype ||
3182 tuple->op != DNS_DIFFOP_ADD)
3185 if (!dns_nsec3param_fromprivate(&tuple->rdata, &rdata,
3188 dns_rdata_tostruct(&rdata, &nsec3param, NULL);
3189 if (nsec3param.flags == 0)
3192 result = dns_zone_addnsec3chain(zone, &nsec3param);
3193 if (result != ISC_R_SUCCESS) {
3194 update_log(client, zone, ISC_LOG_ERROR,
3195 "dns_zone_addnsec3chain failed: %s",
3196 dns_result_totext(result));
3200 update_log(client, zone, LOGLEVEL_DEBUG, "redundant request");
3201 dns_db_closeversion(db, &ver, ISC_TRUE);
3203 result = ISC_R_SUCCESS;
3208 * The reason for failure should have been logged at this point.
3211 update_log(client, zone, LOGLEVEL_DEBUG,
3213 dns_db_closeversion(db, &ver, ISC_FALSE);
3217 dns_diff_clear(&temp);
3218 dns_diff_clear(&diff);
3221 dns_db_closeversion(db, &oldver, ISC_FALSE);
3226 if (ssutable != NULL)
3227 dns_ssutable_detach(&ssutable);
3229 isc_task_detach(&task);
3230 uev->result = result;
3232 INSIST(uev->zone == zone); /* we use this later */
3233 uev->ev_type = DNS_EVENT_UPDATEDONE;
3234 uev->ev_action = updatedone_action;
3235 isc_task_send(client->task, &event);
3236 INSIST(event == NULL);
3240 updatedone_action(isc_task_t *task, isc_event_t *event) {
3241 update_event_t *uev = (update_event_t *) event;
3242 ns_client_t *client = (ns_client_t *) event->ev_arg;
3246 INSIST(event->ev_type == DNS_EVENT_UPDATEDONE);
3247 INSIST(task == client->task);
3249 INSIST(client->nupdates > 0);
3250 switch (uev->result) {
3252 inc_stats(uev->zone, dns_nsstatscounter_updatedone);
3255 inc_stats(uev->zone, dns_nsstatscounter_updaterej);
3258 inc_stats(uev->zone, dns_nsstatscounter_updatefail);
3261 if (uev->zone != NULL)
3262 dns_zone_detach(&uev->zone);
3264 respond(client, uev->result);
3265 isc_event_free(&event);
3266 ns_client_detach(&client);
3270 * Update forwarding support.
3274 forward_fail(isc_task_t *task, isc_event_t *event) {
3275 ns_client_t *client = (ns_client_t *)event->ev_arg;
3279 INSIST(client->nupdates > 0);
3281 respond(client, DNS_R_SERVFAIL);
3282 isc_event_free(&event);
3283 ns_client_detach(&client);
3288 forward_callback(void *arg, isc_result_t result, dns_message_t *answer) {
3289 update_event_t *uev = arg;
3290 ns_client_t *client = uev->ev_arg;
3291 dns_zone_t *zone = uev->zone;
3293 if (result != ISC_R_SUCCESS) {
3294 INSIST(answer == NULL);
3295 uev->ev_type = DNS_EVENT_UPDATEDONE;
3296 uev->ev_action = forward_fail;
3297 inc_stats(zone, dns_nsstatscounter_updatefwdfail);
3299 uev->ev_type = DNS_EVENT_UPDATEDONE;
3300 uev->ev_action = forward_done;
3301 uev->answer = answer;
3302 inc_stats(zone, dns_nsstatscounter_updaterespfwd);
3304 isc_task_send(client->task, ISC_EVENT_PTR(&uev));
3305 dns_zone_detach(&zone);
3309 forward_done(isc_task_t *task, isc_event_t *event) {
3310 update_event_t *uev = (update_event_t *) event;
3311 ns_client_t *client = (ns_client_t *)event->ev_arg;
3315 INSIST(client->nupdates > 0);
3317 ns_client_sendraw(client, uev->answer);
3318 dns_message_destroy(&uev->answer);
3319 isc_event_free(&event);
3320 ns_client_detach(&client);
3324 forward_action(isc_task_t *task, isc_event_t *event) {
3325 update_event_t *uev = (update_event_t *) event;
3326 dns_zone_t *zone = uev->zone;
3327 ns_client_t *client = (ns_client_t *)event->ev_arg;
3328 isc_result_t result;
3330 result = dns_zone_forwardupdate(zone, client->message,
3331 forward_callback, event);
3332 if (result != ISC_R_SUCCESS) {
3333 uev->ev_type = DNS_EVENT_UPDATEDONE;
3334 uev->ev_action = forward_fail;
3335 isc_task_send(client->task, &event);
3336 inc_stats(zone, dns_nsstatscounter_updatefwdfail);
3337 dns_zone_detach(&zone);
3339 inc_stats(zone, dns_nsstatscounter_updatereqfwd);
3340 isc_task_detach(&task);
3344 send_forward_event(ns_client_t *client, dns_zone_t *zone) {
3345 isc_result_t result = ISC_R_SUCCESS;
3346 update_event_t *event = NULL;
3347 isc_task_t *zonetask = NULL;
3348 ns_client_t *evclient;
3351 * This may take some time so replace this client.
3353 if (!client->mortal && (client->attributes & NS_CLIENTATTR_TCP) == 0)
3354 CHECK(ns_client_replace(client));
3356 event = (update_event_t *)
3357 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
3358 forward_action, NULL, sizeof(*event));
3360 FAIL(ISC_R_NOMEMORY);
3362 event->result = ISC_R_SUCCESS;
3365 ns_client_attach(client, &evclient);
3366 INSIST(client->nupdates == 0);
3368 event->ev_arg = evclient;
3370 dns_zone_gettask(zone, &zonetask);
3371 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
3375 isc_event_free(ISC_EVENT_PTR(&event));