2 * Copyright (C) 2004-2013 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 ISC_LIST_APPEND(temp_diff.tuples, *tuple, link);
410 * Apply it to the database.
412 result = dns_diff_apply(&temp_diff, db, ver);
413 ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link);
414 if (result != ISC_R_SUCCESS) {
415 dns_difftuple_free(tuple);
420 * Merge it into the current pending journal entry.
422 dns_diff_appendminimal(diff, tuple);
425 * Do not clear temp_diff.
427 return (ISC_R_SUCCESS);
431 * Perform the updates in 'updates' in version 'ver' of 'db' and log the
435 * \li 'updates' is empty.
438 do_diff(dns_diff_t *updates, dns_db_t *db, dns_dbversion_t *ver,
442 while (! ISC_LIST_EMPTY(updates->tuples)) {
443 dns_difftuple_t *t = ISC_LIST_HEAD(updates->tuples);
444 ISC_LIST_UNLINK(updates->tuples, t, link);
445 CHECK(do_one_tuple(&t, db, ver, diff));
447 return (ISC_R_SUCCESS);
450 dns_diff_clear(diff);
455 update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
456 dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
459 dns_difftuple_t *tuple = NULL;
461 result = dns_difftuple_create(diff->mctx, op,
462 name, ttl, rdata, &tuple);
463 if (result != ISC_R_SUCCESS)
465 return (do_one_tuple(&tuple, db, ver, diff));
468 /**************************************************************************/
470 * Callback-style iteration over rdatasets and rdatas.
472 * foreach_rrset() can be used to iterate over the RRsets
473 * of a name and call a callback function with each
474 * one. Similarly, foreach_rr() can be used to iterate
475 * over the individual RRs at name, optionally restricted
476 * to RRs of a given type.
478 * The callback functions are called "actions" and take
479 * two arguments: a void pointer for passing arbitrary
480 * context information, and a pointer to the current RRset
481 * or RR. By convention, their names end in "_action".
485 * XXXRTH We might want to make this public somewhere in libdns.
489 * Function type for foreach_rrset() iterator actions.
491 typedef isc_result_t rrset_func(void *data, dns_rdataset_t *rrset);
494 * Function type for foreach_rr() iterator actions.
496 typedef isc_result_t rr_func(void *data, rr_t *rr);
499 * Internal context struct for foreach_node_rr().
503 void * rr_action_data;
504 } foreach_node_rr_ctx_t;
507 * Internal helper function for foreach_node_rr().
510 foreach_node_rr_action(void *data, dns_rdataset_t *rdataset) {
512 foreach_node_rr_ctx_t *ctx = data;
513 for (result = dns_rdataset_first(rdataset);
514 result == ISC_R_SUCCESS;
515 result = dns_rdataset_next(rdataset))
517 rr_t rr = { 0, DNS_RDATA_INIT };
519 dns_rdataset_current(rdataset, &rr.rdata);
520 rr.ttl = rdataset->ttl;
521 result = (*ctx->rr_action)(ctx->rr_action_data, &rr);
522 if (result != ISC_R_SUCCESS)
525 if (result != ISC_R_NOMORE)
527 return (ISC_R_SUCCESS);
531 * For each rdataset of 'name' in 'ver' of 'db', call 'action'
532 * with the rdataset and 'action_data' as arguments. If the name
533 * does not exist, do nothing.
535 * If 'action' returns an error, abort iteration and return the error.
538 foreach_rrset(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
539 rrset_func *action, void *action_data)
543 dns_rdatasetiter_t *iter;
546 result = dns_db_findnode(db, name, ISC_FALSE, &node);
547 if (result == ISC_R_NOTFOUND)
548 return (ISC_R_SUCCESS);
549 if (result != ISC_R_SUCCESS)
553 result = dns_db_allrdatasets(db, node, ver,
554 (isc_stdtime_t) 0, &iter);
555 if (result != ISC_R_SUCCESS)
558 for (result = dns_rdatasetiter_first(iter);
559 result == ISC_R_SUCCESS;
560 result = dns_rdatasetiter_next(iter))
562 dns_rdataset_t rdataset;
564 dns_rdataset_init(&rdataset);
565 dns_rdatasetiter_current(iter, &rdataset);
567 result = (*action)(action_data, &rdataset);
569 dns_rdataset_disassociate(&rdataset);
570 if (result != ISC_R_SUCCESS)
571 goto cleanup_iterator;
573 if (result == ISC_R_NOMORE)
574 result = ISC_R_SUCCESS;
577 dns_rdatasetiter_destroy(&iter);
580 dns_db_detachnode(db, &node);
586 * For each RR of 'name' in 'ver' of 'db', call 'action'
587 * with the RR and 'action_data' as arguments. If the name
588 * does not exist, do nothing.
590 * If 'action' returns an error, abort iteration
591 * and return the error.
594 foreach_node_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
595 rr_func *rr_action, void *rr_action_data)
597 foreach_node_rr_ctx_t ctx;
598 ctx.rr_action = rr_action;
599 ctx.rr_action_data = rr_action_data;
600 return (foreach_rrset(db, ver, name,
601 foreach_node_rr_action, &ctx));
606 * For each of the RRs specified by 'db', 'ver', 'name', 'type',
607 * (which can be dns_rdatatype_any to match any type), and 'covers', call
608 * 'action' with the RR and 'action_data' as arguments. If the name
609 * does not exist, or if no RRset of the given type exists at the name,
612 * If 'action' returns an error, abort iteration and return the error.
615 foreach_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
616 dns_rdatatype_t type, dns_rdatatype_t covers, rr_func *rr_action,
617 void *rr_action_data)
622 dns_rdataset_t rdataset;
624 if (type == dns_rdatatype_any)
625 return (foreach_node_rr(db, ver, name,
626 rr_action, rr_action_data));
629 if (type == dns_rdatatype_nsec3 ||
630 (type == dns_rdatatype_rrsig && covers == dns_rdatatype_nsec3))
631 result = dns_db_findnsec3node(db, name, ISC_FALSE, &node);
633 result = dns_db_findnode(db, name, ISC_FALSE, &node);
634 if (result == ISC_R_NOTFOUND)
635 return (ISC_R_SUCCESS);
636 if (result != ISC_R_SUCCESS)
639 dns_rdataset_init(&rdataset);
640 result = dns_db_findrdataset(db, node, ver, type, covers,
641 (isc_stdtime_t) 0, &rdataset, NULL);
642 if (result == ISC_R_NOTFOUND) {
643 result = ISC_R_SUCCESS;
646 if (result != ISC_R_SUCCESS)
649 for (result = dns_rdataset_first(&rdataset);
650 result == ISC_R_SUCCESS;
651 result = dns_rdataset_next(&rdataset))
653 rr_t rr = { 0, DNS_RDATA_INIT };
654 dns_rdataset_current(&rdataset, &rr.rdata);
655 rr.ttl = rdataset.ttl;
656 result = (*rr_action)(rr_action_data, &rr);
657 if (result != ISC_R_SUCCESS)
658 goto cleanup_rdataset;
660 if (result != ISC_R_NOMORE)
661 goto cleanup_rdataset;
662 result = ISC_R_SUCCESS;
665 dns_rdataset_disassociate(&rdataset);
667 dns_db_detachnode(db, &node);
672 /**************************************************************************/
674 * Various tests on the database contents (for prerequisites, etc).
678 * Function type for predicate functions that compare a database RR 'db_rr'
679 * against an update RR 'update_rr'.
681 typedef isc_boolean_t rr_predicate(dns_rdata_t *update_rr, dns_rdata_t *db_rr);
684 * Helper function for rrset_exists().
687 rrset_exists_action(void *data, rr_t *rr) {
690 return (ISC_R_EXISTS);
694 * Utility macro for RR existence checking functions.
696 * If the variable 'result' has the value ISC_R_EXISTS or
697 * ISC_R_SUCCESS, set *exists to ISC_TRUE or ISC_FALSE,
698 * respectively, and return success.
700 * If 'result' has any other value, there was a failure.
701 * Return the failure result code and do not set *exists.
703 * This would be more readable as "do { if ... } while(0)",
704 * but that form generates tons of warnings on Solaris 2.6.
706 #define RETURN_EXISTENCE_FLAG \
707 return ((result == ISC_R_EXISTS) ? \
708 (*exists = ISC_TRUE, ISC_R_SUCCESS) : \
709 ((result == ISC_R_SUCCESS) ? \
710 (*exists = ISC_FALSE, ISC_R_SUCCESS) : \
714 * Set '*exists' to true iff an rrset of the given type exists,
715 * to false otherwise.
718 rrset_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
719 dns_rdatatype_t type, dns_rdatatype_t covers,
720 isc_boolean_t *exists)
723 result = foreach_rr(db, ver, name, type, covers,
724 rrset_exists_action, NULL);
725 RETURN_EXISTENCE_FLAG;
729 * Helper function for cname_incompatible_rrset_exists.
732 cname_compatibility_action(void *data, dns_rdataset_t *rrset) {
734 if (rrset->type != dns_rdatatype_cname &&
735 ! dns_rdatatype_isdnssec(rrset->type))
736 return (ISC_R_EXISTS);
737 return (ISC_R_SUCCESS);
741 * Check whether there is an rrset incompatible with adding a CNAME RR,
742 * i.e., anything but another CNAME (which can be replaced) or a
743 * DNSSEC RR (which can coexist).
745 * If such an incompatible rrset exists, set '*exists' to ISC_TRUE.
746 * Otherwise, set it to ISC_FALSE.
749 cname_incompatible_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
750 dns_name_t *name, isc_boolean_t *exists) {
752 result = foreach_rrset(db, ver, name,
753 cname_compatibility_action, NULL);
754 RETURN_EXISTENCE_FLAG;
758 * Helper function for rr_count().
761 count_rr_action(void *data, rr_t *rr) {
765 return (ISC_R_SUCCESS);
769 * Count the number of RRs of 'type' belonging to 'name' in 'ver' of 'db'.
772 rr_count(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
773 dns_rdatatype_t type, dns_rdatatype_t covers, int *countp)
776 return (foreach_rr(db, ver, name, type, covers,
777 count_rr_action, countp));
781 * Context struct and helper function for name_exists().
785 name_exists_action(void *data, dns_rdataset_t *rrset) {
788 return (ISC_R_EXISTS);
792 * Set '*exists' to true iff the given name exists, to false otherwise.
795 name_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
796 isc_boolean_t *exists)
799 result = foreach_rrset(db, ver, name,
800 name_exists_action, NULL);
801 RETURN_EXISTENCE_FLAG;
805 * 'ssu_check_t' is used to pass the arguments to
806 * dns_ssutable_checkrules() to the callback function
810 /* The ownername of the record to be updated. */
813 /* The signature's name if the request was signed. */
816 /* The address of the client if the request was received via TCP. */
817 isc_netaddr_t *tcpaddr;
819 /* The ssu table to check against. */
820 dns_ssutable_t *table;
822 /* the key used for TKEY requests */
827 ssu_checkrule(void *data, dns_rdataset_t *rrset) {
828 ssu_check_t *ssuinfo = data;
829 isc_boolean_t result;
832 * If we're deleting all records, it's ok to delete RRSIG and NSEC even
833 * if we're normally not allowed to.
835 if (rrset->type == dns_rdatatype_rrsig ||
836 rrset->type == dns_rdatatype_nsec)
837 return (ISC_R_SUCCESS);
838 result = dns_ssutable_checkrules(ssuinfo->table, ssuinfo->signer,
839 ssuinfo->name, ssuinfo->tcpaddr,
840 rrset->type, ssuinfo->key);
841 return (result == ISC_TRUE ? ISC_R_SUCCESS : ISC_R_FAILURE);
845 ssu_checkall(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
846 dns_ssutable_t *ssutable, dns_name_t *signer,
847 isc_netaddr_t *tcpaddr, dst_key_t *key)
853 ssuinfo.table = ssutable;
854 ssuinfo.signer = signer;
855 ssuinfo.tcpaddr = tcpaddr;
857 result = foreach_rrset(db, ver, name, ssu_checkrule, &ssuinfo);
858 return (ISC_TF(result == ISC_R_SUCCESS));
861 /**************************************************************************/
863 * Checking of "RRset exists (value dependent)" prerequisites.
865 * In the RFC2136 section 3.2.5, this is the pseudocode involving
866 * a variable called "temp", a mapping of <name, type> tuples to rrsets.
868 * Here, we represent the "temp" data structure as (non-minimal) "dns_diff_t"
869 * where each tuple has op==DNS_DIFFOP_EXISTS.
874 * Append a tuple asserting the existence of the RR with
875 * 'name' and 'rdata' to 'diff'.
878 temp_append(dns_diff_t *diff, dns_name_t *name, dns_rdata_t *rdata) {
880 dns_difftuple_t *tuple = NULL;
882 REQUIRE(DNS_DIFF_VALID(diff));
883 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_EXISTS,
884 name, 0, rdata, &tuple));
885 ISC_LIST_APPEND(diff->tuples, tuple, link);
891 * Compare two rdatasets represented as sorted lists of tuples.
892 * All list elements must have the same owner name and type.
893 * Return ISC_R_SUCCESS if the rdatasets are equal, rcode(dns_rcode_nxrrset)
897 temp_check_rrset(dns_difftuple_t *a, dns_difftuple_t *b) {
899 if (a == NULL || b == NULL)
901 INSIST(a->op == DNS_DIFFOP_EXISTS &&
902 b->op == DNS_DIFFOP_EXISTS);
903 INSIST(a->rdata.type == b->rdata.type);
904 INSIST(dns_name_equal(&a->name, &b->name));
905 if (dns_rdata_casecompare(&a->rdata, &b->rdata) != 0)
906 return (DNS_R_NXRRSET);
907 a = ISC_LIST_NEXT(a, link);
908 b = ISC_LIST_NEXT(b, link);
910 if (a != NULL || b != NULL)
911 return (DNS_R_NXRRSET);
912 return (ISC_R_SUCCESS);
916 * A comparison function defining the sorting order for the entries
917 * in the "temp" data structure. The major sort key is the owner name,
918 * followed by the type and rdata.
921 temp_order(const void *av, const void *bv) {
922 dns_difftuple_t const * const *ap = av;
923 dns_difftuple_t const * const *bp = bv;
924 dns_difftuple_t const *a = *ap;
925 dns_difftuple_t const *b = *bp;
927 r = dns_name_compare(&a->name, &b->name);
930 r = (b->rdata.type - a->rdata.type);
933 r = dns_rdata_casecompare(&a->rdata, &b->rdata);
938 * Check the "RRset exists (value dependent)" prerequisite information
939 * in 'temp' against the contents of the database 'db'.
941 * Return ISC_R_SUCCESS if the prerequisites are satisfied,
942 * rcode(dns_rcode_nxrrset) if not.
944 * 'temp' must be pre-sorted.
948 temp_check(isc_mem_t *mctx, dns_diff_t *temp, dns_db_t *db,
949 dns_dbversion_t *ver, dns_name_t *tmpname, dns_rdatatype_t *typep)
957 dns_diff_init(mctx, &trash);
960 * For each name and type in the prerequisites,
961 * construct a sorted rdata list of the corresponding
962 * database contents, and compare the lists.
964 t = ISC_LIST_HEAD(temp->tuples);
967 (void)dns_name_copy(name, tmpname, NULL);
968 *typep = t->rdata.type;
970 /* A new unique name begins here. */
972 result = dns_db_findnode(db, name, ISC_FALSE, &node);
973 if (result == ISC_R_NOTFOUND) {
974 dns_diff_clear(&trash);
975 return (DNS_R_NXRRSET);
977 if (result != ISC_R_SUCCESS) {
978 dns_diff_clear(&trash);
982 /* A new unique type begins here. */
983 while (t != NULL && dns_name_equal(&t->name, name)) {
984 dns_rdatatype_t type, covers;
985 dns_rdataset_t rdataset;
986 dns_diff_t d_rrs; /* Database RRs with
987 this name and type */
988 dns_diff_t u_rrs; /* Update RRs with
989 this name and type */
991 *typep = type = t->rdata.type;
992 if (type == dns_rdatatype_rrsig ||
993 type == dns_rdatatype_sig)
994 covers = dns_rdata_covers(&t->rdata);
995 else if (type == dns_rdatatype_any) {
996 dns_db_detachnode(db, &node);
997 dns_diff_clear(&trash);
998 return (DNS_R_NXRRSET);
1003 * Collect all database RRs for this name and type
1004 * onto d_rrs and sort them.
1006 dns_rdataset_init(&rdataset);
1007 result = dns_db_findrdataset(db, node, ver, type,
1008 covers, (isc_stdtime_t) 0,
1010 if (result != ISC_R_SUCCESS) {
1011 dns_db_detachnode(db, &node);
1012 dns_diff_clear(&trash);
1013 return (DNS_R_NXRRSET);
1016 dns_diff_init(mctx, &d_rrs);
1017 dns_diff_init(mctx, &u_rrs);
1019 for (result = dns_rdataset_first(&rdataset);
1020 result == ISC_R_SUCCESS;
1021 result = dns_rdataset_next(&rdataset))
1023 dns_rdata_t rdata = DNS_RDATA_INIT;
1024 dns_rdataset_current(&rdataset, &rdata);
1025 result = temp_append(&d_rrs, name, &rdata);
1026 if (result != ISC_R_SUCCESS)
1029 if (result != ISC_R_NOMORE)
1031 result = dns_diff_sort(&d_rrs, temp_order);
1032 if (result != ISC_R_SUCCESS)
1036 * Collect all update RRs for this name and type
1037 * onto u_rrs. No need to sort them here -
1038 * they are already sorted.
1041 dns_name_equal(&t->name, name) &&
1042 t->rdata.type == type)
1044 dns_difftuple_t *next =
1045 ISC_LIST_NEXT(t, link);
1046 ISC_LIST_UNLINK(temp->tuples, t, link);
1047 ISC_LIST_APPEND(u_rrs.tuples, t, link);
1051 /* Compare the two sorted lists. */
1052 result = temp_check_rrset(ISC_LIST_HEAD(u_rrs.tuples),
1053 ISC_LIST_HEAD(d_rrs.tuples));
1054 if (result != ISC_R_SUCCESS)
1058 * We are done with the tuples, but we can't free
1059 * them yet because "name" still points into one
1060 * of them. Move them on a temporary list.
1062 ISC_LIST_APPENDLIST(trash.tuples, u_rrs.tuples, link);
1063 ISC_LIST_APPENDLIST(trash.tuples, d_rrs.tuples, link);
1064 dns_rdataset_disassociate(&rdataset);
1069 dns_diff_clear(&d_rrs);
1070 dns_diff_clear(&u_rrs);
1071 dns_diff_clear(&trash);
1072 dns_rdataset_disassociate(&rdataset);
1073 dns_db_detachnode(db, &node);
1077 dns_db_detachnode(db, &node);
1080 dns_diff_clear(&trash);
1081 return (ISC_R_SUCCESS);
1084 /**************************************************************************/
1086 * Conditional deletion of RRs.
1090 * Context structure for delete_if().
1094 rr_predicate *predicate;
1096 dns_dbversion_t *ver;
1099 dns_rdata_t *update_rr;
1100 } conditional_delete_ctx_t;
1103 * Predicate functions for delete_if().
1107 * Return true iff 'db_rr' is neither a SOA nor an NS RR nor
1108 * an RRSIG nor an NSEC3PARAM nor a NSEC.
1110 static isc_boolean_t
1111 type_not_soa_nor_ns_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1113 return ((db_rr->type != dns_rdatatype_soa &&
1114 db_rr->type != dns_rdatatype_ns &&
1115 db_rr->type != dns_rdatatype_nsec3param &&
1116 db_rr->type != dns_rdatatype_rrsig &&
1117 db_rr->type != dns_rdatatype_nsec) ?
1118 ISC_TRUE : ISC_FALSE);
1122 * Return true iff 'db_rr' is neither a RRSIG nor a NSEC.
1124 static isc_boolean_t
1125 type_not_dnssec(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1127 return ((db_rr->type != dns_rdatatype_rrsig &&
1128 db_rr->type != dns_rdatatype_nsec) ?
1129 ISC_TRUE : ISC_FALSE);
1133 * Return true always.
1135 static isc_boolean_t
1136 true_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1143 * Return true iff the two RRs have identical rdata.
1145 static isc_boolean_t
1146 rr_equal_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1148 * XXXRTH This is not a problem, but we should consider creating
1149 * dns_rdata_equal() (that used dns_name_equal()), since it
1150 * would be faster. Not a priority.
1152 return (dns_rdata_casecompare(update_rr, db_rr) == 0 ?
1153 ISC_TRUE : ISC_FALSE);
1157 * Return true iff 'update_rr' should replace 'db_rr' according
1158 * to the special RFC2136 rules for CNAME, SOA, and WKS records.
1160 * RFC2136 does not mention NSEC or DNAME, but multiple NSECs or DNAMEs
1161 * make little sense, so we replace those, too.
1163 * Additionally replace RRSIG that have been generated by the same key
1164 * for the same type. This simplifies refreshing a offline KSK by not
1165 * requiring that the old RRSIG be deleted. It also simplifies key
1166 * rollover by only requiring that the new RRSIG be added.
1168 static isc_boolean_t
1169 replaces_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
1170 dns_rdata_rrsig_t updatesig, dbsig;
1171 isc_result_t result;
1173 if (db_rr->type != update_rr->type)
1175 if (db_rr->type == dns_rdatatype_cname)
1177 if (db_rr->type == dns_rdatatype_dname)
1179 if (db_rr->type == dns_rdatatype_soa)
1181 if (db_rr->type == dns_rdatatype_nsec)
1183 if (db_rr->type == dns_rdatatype_rrsig) {
1185 * Replace existing RRSIG with the same keyid,
1186 * covered and algorithm.
1188 result = dns_rdata_tostruct(db_rr, &dbsig, NULL);
1189 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1190 result = dns_rdata_tostruct(update_rr, &updatesig, NULL);
1191 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1192 if (dbsig.keyid == updatesig.keyid &&
1193 dbsig.covered == updatesig.covered &&
1194 dbsig.algorithm == updatesig.algorithm)
1197 if (db_rr->type == dns_rdatatype_wks) {
1199 * Compare the address and protocol fields only. These
1200 * form the first five bytes of the RR data. Do a
1201 * raw binary comparison; unpacking the WKS RRs using
1202 * dns_rdata_tostruct() might be cleaner in some ways.
1204 INSIST(db_rr->length >= 5 && update_rr->length >= 5);
1205 return (memcmp(db_rr->data, update_rr->data, 5) == 0 ?
1206 ISC_TRUE : ISC_FALSE);
1209 if (db_rr->type == dns_rdatatype_nsec3param) {
1210 if (db_rr->length != update_rr->length)
1212 INSIST(db_rr->length >= 4 && update_rr->length >= 4);
1214 * Replace NSEC3PARAM records that only differ by the
1217 if (db_rr->data[0] == update_rr->data[0] &&
1218 memcmp(db_rr->data+2, update_rr->data+2,
1219 update_rr->length - 2) == 0)
1226 * Internal helper function for delete_if().
1229 delete_if_action(void *data, rr_t *rr) {
1230 conditional_delete_ctx_t *ctx = data;
1231 if ((*ctx->predicate)(ctx->update_rr, &rr->rdata)) {
1232 isc_result_t result;
1233 result = update_one_rr(ctx->db, ctx->ver, ctx->diff,
1234 DNS_DIFFOP_DEL, ctx->name,
1235 rr->ttl, &rr->rdata);
1238 return (ISC_R_SUCCESS);
1243 * Conditionally delete RRs. Apply 'predicate' to the RRs
1244 * specified by 'db', 'ver', 'name', and 'type' (which can
1245 * be dns_rdatatype_any to match any type). Delete those
1246 * RRs for which the predicate returns true, and log the
1247 * deletions in 'diff'.
1250 delete_if(rr_predicate *predicate, dns_db_t *db, dns_dbversion_t *ver,
1251 dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers,
1252 dns_rdata_t *update_rr, dns_diff_t *diff)
1254 conditional_delete_ctx_t ctx;
1255 ctx.predicate = predicate;
1260 ctx.update_rr = update_rr;
1261 return (foreach_rr(db, ver, name, type, covers,
1262 delete_if_action, &ctx));
1265 /**************************************************************************/
1267 * Prepare an RR for the addition of the new RR 'ctx->update_rr',
1268 * with TTL 'ctx->update_rr_ttl', to its rdataset, by deleting
1269 * the RRs if it is replaced by the new RR or has a conflicting TTL.
1270 * The necessary changes are appended to ctx->del_diff and ctx->add_diff;
1271 * we need to do all deletions before any additions so that we don't run
1272 * into transient states with conflicting TTLs.
1277 dns_dbversion_t *ver;
1280 dns_rdata_t *update_rr;
1281 dns_ttl_t update_rr_ttl;
1282 isc_boolean_t ignore_add;
1283 dns_diff_t del_diff;
1284 dns_diff_t add_diff;
1285 } add_rr_prepare_ctx_t;
1288 add_rr_prepare_action(void *data, rr_t *rr) {
1289 isc_result_t result = ISC_R_SUCCESS;
1290 add_rr_prepare_ctx_t *ctx = data;
1291 dns_difftuple_t *tuple = NULL;
1292 isc_boolean_t equal;
1295 * If the update RR is a "duplicate" of the update RR,
1296 * the update should be silently ignored.
1298 equal = ISC_TF(dns_rdata_casecompare(&rr->rdata, ctx->update_rr) == 0);
1299 if (equal && rr->ttl == ctx->update_rr_ttl) {
1300 ctx->ignore_add = ISC_TRUE;
1301 return (ISC_R_SUCCESS);
1305 * If this RR is "equal" to the update RR, it should
1306 * be deleted before the update RR is added.
1308 if (replaces_p(ctx->update_rr, &rr->rdata)) {
1309 CHECK(dns_difftuple_create(ctx->del_diff.mctx, DNS_DIFFOP_DEL,
1310 ctx->name, rr->ttl, &rr->rdata,
1312 dns_diff_append(&ctx->del_diff, &tuple);
1313 return (ISC_R_SUCCESS);
1317 * If this RR differs in TTL from the update RR,
1318 * its TTL must be adjusted.
1320 if (rr->ttl != ctx->update_rr_ttl) {
1321 CHECK(dns_difftuple_create(ctx->del_diff.mctx, DNS_DIFFOP_DEL,
1322 ctx->name, rr->ttl, &rr->rdata,
1324 dns_diff_append(&ctx->del_diff, &tuple);
1326 CHECK(dns_difftuple_create(ctx->add_diff.mctx,
1327 DNS_DIFFOP_ADD, ctx->name,
1329 &rr->rdata, &tuple));
1330 dns_diff_append(&ctx->add_diff, &tuple);
1337 /**************************************************************************/
1339 * Miscellaneous subroutines.
1343 * Extract a single update RR from 'section' of dynamic update message
1344 * 'msg', with consistency checking.
1346 * Stores the owner name, rdata, and TTL of the update RR at 'name',
1347 * 'rdata', and 'ttl', respectively.
1350 get_current_rr(dns_message_t *msg, dns_section_t section,
1351 dns_rdataclass_t zoneclass, dns_name_t **name,
1352 dns_rdata_t *rdata, dns_rdatatype_t *covers,
1353 dns_ttl_t *ttl, dns_rdataclass_t *update_class)
1355 dns_rdataset_t *rdataset;
1356 isc_result_t result;
1357 dns_message_currentname(msg, section, name);
1358 rdataset = ISC_LIST_HEAD((*name)->list);
1359 INSIST(rdataset != NULL);
1360 INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
1361 *covers = rdataset->covers;
1362 *ttl = rdataset->ttl;
1363 result = dns_rdataset_first(rdataset);
1364 INSIST(result == ISC_R_SUCCESS);
1365 dns_rdataset_current(rdataset, rdata);
1366 INSIST(dns_rdataset_next(rdataset) == ISC_R_NOMORE);
1367 *update_class = rdata->rdclass;
1368 rdata->rdclass = zoneclass;
1372 * Increment the SOA serial number of database 'db', version 'ver'.
1373 * Replace the SOA record in the database, and log the
1378 * XXXRTH Failures in this routine will be worth logging, when
1379 * we have a logging system. Failure to find the zonename
1380 * or the SOA rdataset warrant at least an UNEXPECTED_ERROR().
1384 update_soa_serial(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
1385 isc_mem_t *mctx, dns_updatemethod_t method)
1387 dns_difftuple_t *deltuple = NULL;
1388 dns_difftuple_t *addtuple = NULL;
1389 isc_uint32_t serial;
1390 isc_result_t result;
1392 CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_DEL, &deltuple));
1393 CHECK(dns_difftuple_copy(deltuple, &addtuple));
1394 addtuple->op = DNS_DIFFOP_ADD;
1396 serial = dns_soa_getserial(&addtuple->rdata);
1397 serial = dns_update_soaserial(serial, method);
1398 dns_soa_setserial(serial, &addtuple->rdata);
1399 CHECK(do_one_tuple(&deltuple, db, ver, diff));
1400 CHECK(do_one_tuple(&addtuple, db, ver, diff));
1401 result = ISC_R_SUCCESS;
1404 if (addtuple != NULL)
1405 dns_difftuple_free(&addtuple);
1406 if (deltuple != NULL)
1407 dns_difftuple_free(&deltuple);
1412 * Check that the new SOA record at 'update_rdata' does not
1413 * illegally cause the SOA serial number to decrease or stay
1414 * unchanged relative to the existing SOA in 'db'.
1416 * Sets '*ok' to ISC_TRUE if the update is legal, ISC_FALSE if not.
1418 * William King points out that RFC2136 is inconsistent about
1419 * the case where the serial number stays unchanged:
1421 * section 3.4.2.2 requires a server to ignore a SOA update request
1422 * if the serial number on the update SOA is less_than_or_equal to
1423 * the zone SOA serial.
1425 * section 3.6 requires a server to ignore a SOA update request if
1426 * the serial is less_than the zone SOA serial.
1428 * Paul says 3.4.2.2 is correct.
1432 check_soa_increment(dns_db_t *db, dns_dbversion_t *ver,
1433 dns_rdata_t *update_rdata, isc_boolean_t *ok)
1435 isc_uint32_t db_serial;
1436 isc_uint32_t update_serial;
1437 isc_result_t result;
1439 update_serial = dns_soa_getserial(update_rdata);
1441 result = dns_db_getsoaserial(db, ver, &db_serial);
1442 if (result != ISC_R_SUCCESS)
1445 if (DNS_SERIAL_GE(db_serial, update_serial)) {
1451 return (ISC_R_SUCCESS);
1455 /**************************************************************************/
1457 * The actual update code in all its glory. We try to follow
1458 * the RFC2136 pseudocode as closely as possible.
1462 send_update_event(ns_client_t *client, dns_zone_t *zone) {
1463 isc_result_t result = ISC_R_SUCCESS;
1464 update_event_t *event = NULL;
1465 isc_task_t *zonetask = NULL;
1466 ns_client_t *evclient;
1468 event = (update_event_t *)
1469 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
1470 update_action, NULL, sizeof(*event));
1472 FAIL(ISC_R_NOMEMORY);
1474 event->result = ISC_R_SUCCESS;
1477 ns_client_attach(client, &evclient);
1478 INSIST(client->nupdates == 0);
1480 event->ev_arg = evclient;
1482 dns_zone_gettask(zone, &zonetask);
1483 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
1487 isc_event_free(ISC_EVENT_PTR(&event));
1492 respond(ns_client_t *client, isc_result_t result) {
1493 isc_result_t msg_result;
1495 msg_result = dns_message_reply(client->message, ISC_TRUE);
1496 if (msg_result != ISC_R_SUCCESS)
1498 client->message->rcode = dns_result_torcode(result);
1500 ns_client_send(client);
1504 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
1506 "could not create update response message: %s",
1507 isc_result_totext(msg_result));
1508 ns_client_next(client, msg_result);
1512 ns_update_start(ns_client_t *client, isc_result_t sigresult) {
1513 dns_message_t *request = client->message;
1514 isc_result_t result;
1515 dns_name_t *zonename;
1516 dns_rdataset_t *zone_rdataset;
1517 dns_zone_t *zone = NULL, *raw = NULL;
1520 * Interpret the zone section.
1522 result = dns_message_firstname(request, DNS_SECTION_ZONE);
1523 if (result != ISC_R_SUCCESS)
1524 FAILC(DNS_R_FORMERR, "update zone section empty");
1527 * The zone section must contain exactly one "question", and
1528 * it must be of type SOA.
1531 dns_message_currentname(request, DNS_SECTION_ZONE, &zonename);
1532 zone_rdataset = ISC_LIST_HEAD(zonename->list);
1533 if (zone_rdataset->type != dns_rdatatype_soa)
1534 FAILC(DNS_R_FORMERR,
1535 "update zone section contains non-SOA");
1536 if (ISC_LIST_NEXT(zone_rdataset, link) != NULL)
1537 FAILC(DNS_R_FORMERR,
1538 "update zone section contains multiple RRs");
1540 /* The zone section must have exactly one name. */
1541 result = dns_message_nextname(request, DNS_SECTION_ZONE);
1542 if (result != ISC_R_NOMORE)
1543 FAILC(DNS_R_FORMERR,
1544 "update zone section contains multiple RRs");
1546 result = dns_zt_find(client->view->zonetable, zonename, 0, NULL,
1548 if (result != ISC_R_SUCCESS)
1549 FAILC(DNS_R_NOTAUTH, "not authoritative for update zone");
1552 * If there is a raw (unsigned) zone associated with this
1553 * zone then it processes the UPDATE request.
1555 dns_zone_getraw(zone, &raw);
1557 dns_zone_detach(&zone);
1558 dns_zone_attach(raw, &zone);
1559 dns_zone_detach(&raw);
1562 switch(dns_zone_gettype(zone)) {
1563 case dns_zone_master:
1566 * We can now fail due to a bad signature as we now know
1567 * that we are the master.
1569 if (sigresult != ISC_R_SUCCESS)
1571 CHECK(send_update_event(client, zone));
1573 case dns_zone_slave:
1574 CHECK(checkupdateacl(client, dns_zone_getforwardacl(zone),
1575 "update forwarding", zonename, ISC_TRUE,
1577 CHECK(send_forward_event(client, zone));
1580 FAILC(DNS_R_NOTAUTH, "not authoritative for update zone");
1585 if (result == DNS_R_REFUSED) {
1586 INSIST(dns_zone_gettype(zone) == dns_zone_slave);
1587 inc_stats(zone, dns_nsstatscounter_updaterej);
1590 * We failed without having sent an update event to the zone.
1591 * We are still in the client task context, so we can
1592 * simply give an error response without switching tasks.
1594 respond(client, result);
1596 dns_zone_detach(&zone);
1600 * DS records are not allowed to exist without corresponding NS records,
1601 * RFC 3658, 2.2 Protocol Change,
1602 * "DS RRsets MUST NOT appear at non-delegation points or at a zone's apex".
1606 remove_orphaned_ds(dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff) {
1607 isc_result_t result;
1608 isc_boolean_t ns_exists;
1609 dns_difftuple_t *tupple;
1610 dns_diff_t temp_diff;
1612 dns_diff_init(diff->mctx, &temp_diff);
1614 for (tupple = ISC_LIST_HEAD(diff->tuples);
1616 tupple = ISC_LIST_NEXT(tupple, link)) {
1617 if (!((tupple->op == DNS_DIFFOP_DEL &&
1618 tupple->rdata.type == dns_rdatatype_ns) ||
1619 (tupple->op == DNS_DIFFOP_ADD &&
1620 tupple->rdata.type == dns_rdatatype_ds)))
1622 CHECK(rrset_exists(db, newver, &tupple->name,
1623 dns_rdatatype_ns, 0, &ns_exists));
1625 !dns_name_equal(&tupple->name, dns_db_origin(db)))
1627 CHECK(delete_if(true_p, db, newver, &tupple->name,
1628 dns_rdatatype_ds, 0, NULL, &temp_diff));
1630 result = ISC_R_SUCCESS;
1633 for (tupple = ISC_LIST_HEAD(temp_diff.tuples);
1635 tupple = ISC_LIST_HEAD(temp_diff.tuples)) {
1636 ISC_LIST_UNLINK(temp_diff.tuples, tupple, link);
1637 dns_diff_appendminimal(diff, &tupple);
1643 * This implements the post load integrity checks for mx records.
1646 check_mx(ns_client_t *client, dns_zone_t *zone,
1647 dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff)
1649 char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123.")];
1650 char ownerbuf[DNS_NAME_FORMATSIZE];
1651 char namebuf[DNS_NAME_FORMATSIZE];
1652 char altbuf[DNS_NAME_FORMATSIZE];
1654 dns_fixedname_t fixed;
1655 dns_name_t *foundname;
1658 isc_boolean_t ok = ISC_TRUE;
1659 isc_boolean_t isaddress;
1660 isc_result_t result;
1661 struct in6_addr addr6;
1662 struct in_addr addr;
1663 unsigned int options;
1665 dns_fixedname_init(&fixed);
1666 foundname = dns_fixedname_name(&fixed);
1667 dns_rdata_init(&rdata);
1668 options = dns_zone_getoptions(zone);
1670 for (t = ISC_LIST_HEAD(diff->tuples);
1672 t = ISC_LIST_NEXT(t, link)) {
1673 if (t->op != DNS_DIFFOP_ADD ||
1674 t->rdata.type != dns_rdatatype_mx)
1677 result = dns_rdata_tostruct(&t->rdata, &mx, NULL);
1678 RUNTIME_CHECK(result == ISC_R_SUCCESS);
1680 * Check if we will error out if we attempt to reload the
1683 dns_name_format(&mx.mx, namebuf, sizeof(namebuf));
1684 dns_name_format(&t->name, ownerbuf, sizeof(ownerbuf));
1685 isaddress = ISC_FALSE;
1686 if ((options & DNS_RDATA_CHECKMX) != 0 &&
1687 strlcpy(tmp, namebuf, sizeof(tmp)) < sizeof(tmp)) {
1688 if (tmp[strlen(tmp) - 1] == '.')
1689 tmp[strlen(tmp) - 1] = '\0';
1690 if (inet_aton(tmp, &addr) == 1 ||
1691 inet_pton(AF_INET6, tmp, &addr6) == 1)
1692 isaddress = ISC_TRUE;
1695 if (isaddress && (options & DNS_RDATA_CHECKMXFAIL) != 0) {
1696 update_log(client, zone, ISC_LOG_ERROR,
1699 dns_result_totext(DNS_R_MXISADDRESS));
1701 } else if (isaddress) {
1702 update_log(client, zone, ISC_LOG_WARNING,
1703 "%s/MX: warning: '%s': %s",
1705 dns_result_totext(DNS_R_MXISADDRESS));
1709 * Check zone integrity checks.
1711 if ((options & DNS_ZONEOPT_CHECKINTEGRITY) == 0)
1713 result = dns_db_find(db, &mx.mx, newver, dns_rdatatype_a,
1714 0, 0, NULL, foundname, NULL, NULL);
1715 if (result == ISC_R_SUCCESS)
1718 if (result == DNS_R_NXRRSET) {
1719 result = dns_db_find(db, &mx.mx, newver,
1721 0, 0, NULL, foundname,
1723 if (result == ISC_R_SUCCESS)
1727 if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN) {
1728 update_log(client, zone, ISC_LOG_ERROR,
1729 "%s/MX '%s' has no address records "
1730 "(A or AAAA)", ownerbuf, namebuf);
1732 } else if (result == DNS_R_CNAME) {
1733 update_log(client, zone, ISC_LOG_ERROR,
1734 "%s/MX '%s' is a CNAME (illegal)",
1737 } else if (result == DNS_R_DNAME) {
1738 dns_name_format(foundname, altbuf, sizeof altbuf);
1739 update_log(client, zone, ISC_LOG_ERROR,
1740 "%s/MX '%s' is below a DNAME '%s' (illegal)",
1741 ownerbuf, namebuf, altbuf);
1745 return (ok ? ISC_R_SUCCESS : DNS_R_REFUSED);
1749 rr_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
1750 const dns_rdata_t *rdata, isc_boolean_t *flag)
1752 dns_rdataset_t rdataset;
1753 dns_dbnode_t *node = NULL;
1754 isc_result_t result;
1756 dns_rdataset_init(&rdataset);
1757 if (rdata->type == dns_rdatatype_nsec3)
1758 CHECK(dns_db_findnsec3node(db, name, ISC_FALSE, &node));
1760 CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
1761 result = dns_db_findrdataset(db, node, ver, rdata->type, 0,
1762 (isc_stdtime_t) 0, &rdataset, NULL);
1763 if (result == ISC_R_NOTFOUND) {
1765 result = ISC_R_SUCCESS;
1769 for (result = dns_rdataset_first(&rdataset);
1770 result == ISC_R_SUCCESS;
1771 result = dns_rdataset_next(&rdataset)) {
1772 dns_rdata_t myrdata = DNS_RDATA_INIT;
1773 dns_rdataset_current(&rdataset, &myrdata);
1774 if (!dns_rdata_casecompare(&myrdata, rdata))
1777 dns_rdataset_disassociate(&rdataset);
1778 if (result == ISC_R_SUCCESS) {
1780 } else if (result == ISC_R_NOMORE) {
1782 result = ISC_R_SUCCESS;
1787 dns_db_detachnode(db, &node);
1792 get_iterations(dns_db_t *db, dns_dbversion_t *ver, dns_rdatatype_t privatetype,
1793 unsigned int *iterationsp)
1795 dns_dbnode_t *node = NULL;
1796 dns_rdata_nsec3param_t nsec3param;
1797 dns_rdataset_t rdataset;
1798 isc_result_t result;
1799 unsigned int iterations = 0;
1801 dns_rdataset_init(&rdataset);
1803 result = dns_db_getoriginnode(db, &node);
1804 if (result != ISC_R_SUCCESS)
1806 result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec3param,
1807 0, (isc_stdtime_t) 0, &rdataset, NULL);
1808 if (result == ISC_R_NOTFOUND)
1810 if (result != ISC_R_SUCCESS)
1813 for (result = dns_rdataset_first(&rdataset);
1814 result == ISC_R_SUCCESS;
1815 result = dns_rdataset_next(&rdataset)) {
1816 dns_rdata_t rdata = DNS_RDATA_INIT;
1817 dns_rdataset_current(&rdataset, &rdata);
1818 CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL));
1819 if ((nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0)
1821 if (nsec3param.iterations > iterations)
1822 iterations = nsec3param.iterations;
1824 if (result != ISC_R_NOMORE)
1827 dns_rdataset_disassociate(&rdataset);
1830 if (privatetype == 0)
1833 result = dns_db_findrdataset(db, node, ver, privatetype,
1834 0, (isc_stdtime_t) 0, &rdataset, NULL);
1835 if (result == ISC_R_NOTFOUND)
1837 if (result != ISC_R_SUCCESS)
1840 for (result = dns_rdataset_first(&rdataset);
1841 result == ISC_R_SUCCESS;
1842 result = dns_rdataset_next(&rdataset)) {
1843 unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
1844 dns_rdata_t private = DNS_RDATA_INIT;
1845 dns_rdata_t rdata = DNS_RDATA_INIT;
1847 dns_rdataset_current(&rdataset, &rdata);
1848 if (!dns_nsec3param_fromprivate(&private, &rdata,
1851 CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL));
1852 if ((nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0)
1854 if (nsec3param.iterations > iterations)
1855 iterations = nsec3param.iterations;
1857 if (result != ISC_R_NOMORE)
1861 *iterationsp = iterations;
1862 result = ISC_R_SUCCESS;
1866 dns_db_detachnode(db, &node);
1867 if (dns_rdataset_isassociated(&rdataset))
1868 dns_rdataset_disassociate(&rdataset);
1873 * Prevent the zone entering a inconsistent state where
1874 * NSEC only DNSKEYs are present with NSEC3 chains.
1877 check_dnssec(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1878 dns_dbversion_t *ver, dns_diff_t *diff)
1880 dns_difftuple_t *tuple;
1881 isc_boolean_t nseconly = ISC_FALSE, nsec3 = ISC_FALSE;
1882 isc_result_t result;
1883 unsigned int iterations = 0, max;
1884 dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
1886 /* Scan the tuples for an NSEC-only DNSKEY or an NSEC3PARAM */
1887 for (tuple = ISC_LIST_HEAD(diff->tuples);
1889 tuple = ISC_LIST_NEXT(tuple, link)) {
1890 if (tuple->op != DNS_DIFFOP_ADD)
1893 if (tuple->rdata.type == dns_rdatatype_dnskey) {
1895 alg = tuple->rdata.data[3];
1896 if (alg == DST_ALG_RSAMD5 || alg == DST_ALG_RSASHA1 ||
1897 alg == DST_ALG_DSA || alg == DST_ALG_ECC) {
1898 nseconly = ISC_TRUE;
1901 } else if (tuple->rdata.type == dns_rdatatype_nsec3param) {
1907 /* Check existing DB for NSEC-only DNSKEY */
1909 result = dns_nsec_nseconly(db, ver, &nseconly);
1912 * An NSEC3PARAM update can proceed without a DNSKEY (it
1913 * will trigger a delayed change), so we can ignore
1914 * ISC_R_NOTFOUND here.
1916 if (result == ISC_R_NOTFOUND)
1917 result = ISC_R_SUCCESS;
1922 /* Check existing DB for NSEC3 */
1924 CHECK(dns_nsec3_activex(db, ver, ISC_FALSE,
1925 privatetype, &nsec3));
1927 /* Refuse to allow NSEC3 with NSEC-only keys */
1928 if (nseconly && nsec3) {
1929 update_log(client, zone, ISC_LOG_ERROR,
1930 "NSEC only DNSKEYs and NSEC3 chains not allowed");
1931 result = DNS_R_REFUSED;
1935 /* Verify NSEC3 params */
1936 CHECK(get_iterations(db, ver, privatetype, &iterations));
1937 CHECK(dns_nsec3_maxiterations(db, ver, client->mctx, &max));
1938 if (max != 0 && iterations > max) {
1939 update_log(client, zone, ISC_LOG_ERROR,
1940 "too many NSEC3 iterations (%u) for "
1941 "weakest DNSKEY (%u)", iterations, max);
1942 result = DNS_R_REFUSED;
1951 * Delay NSEC3PARAM changes as they need to be applied to the whole zone.
1954 add_nsec3param_records(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
1955 dns_dbversion_t *ver, dns_diff_t *diff)
1957 isc_result_t result = ISC_R_SUCCESS;
1958 dns_difftuple_t *tuple, *newtuple = NULL, *next;
1959 dns_rdata_t rdata = DNS_RDATA_INIT;
1960 unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE + 1];
1961 dns_diff_t temp_diff;
1964 dns_name_t *name = dns_zone_getorigin(zone);
1965 dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
1966 isc_uint32_t ttl = 0;
1967 isc_boolean_t ttl_good = ISC_FALSE;
1969 update_log(client, zone, ISC_LOG_DEBUG(3),
1970 "checking for NSEC3PARAM changes");
1972 dns_diff_init(diff->mctx, &temp_diff);
1975 * Extract NSEC3PARAM tuples from list.
1977 for (tuple = ISC_LIST_HEAD(diff->tuples);
1981 next = ISC_LIST_NEXT(tuple, link);
1983 if (tuple->rdata.type != dns_rdatatype_nsec3param ||
1984 !dns_name_equal(name, &tuple->name))
1986 ISC_LIST_UNLINK(diff->tuples, tuple, link);
1987 ISC_LIST_APPEND(temp_diff.tuples, tuple, link);
1991 * Extract TTL changes pairs, we don't need to convert these to
1994 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
1995 tuple != NULL; tuple = next) {
1996 if (tuple->op == DNS_DIFFOP_ADD) {
1999 * Any adds here will contain the final
2000 * NSEC3PARAM RRset TTL.
2003 ttl_good = ISC_TRUE;
2006 * Walk the temp_diff list looking for the
2007 * corresponding delete.
2009 next = ISC_LIST_HEAD(temp_diff.tuples);
2010 while (next != NULL) {
2011 unsigned char *next_data = next->rdata.data;
2012 unsigned char *tuple_data = tuple->rdata.data;
2013 if (next->op == DNS_DIFFOP_DEL &&
2014 next->rdata.length == tuple->rdata.length &&
2015 !memcmp(next_data, tuple_data,
2016 next->rdata.length)) {
2017 ISC_LIST_UNLINK(temp_diff.tuples, next,
2019 ISC_LIST_APPEND(diff->tuples, next,
2023 next = ISC_LIST_NEXT(next, link);
2026 * If we have not found a pair move onto the next
2030 next = ISC_LIST_NEXT(tuple, link);
2034 * Find the next tuple to be processed before
2035 * unlinking then complete moving the pair to 'diff'.
2037 next = ISC_LIST_NEXT(tuple, link);
2038 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2039 ISC_LIST_APPEND(diff->tuples, tuple, link);
2041 next = ISC_LIST_NEXT(tuple, link);
2045 * Preserve any ongoing changes from a BIND 9.6.x upgrade.
2047 * Any NSEC3PARAM records with flags other than OPTOUT named
2048 * in managing and should not be touched so revert such changes
2049 * taking into account any TTL change of the NSEC3PARAM RRset.
2051 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2052 tuple != NULL; tuple = next) {
2053 next = ISC_LIST_NEXT(tuple, link);
2054 if ((tuple->rdata.data[1] & ~DNS_NSEC3FLAG_OPTOUT) != 0) {
2056 * If we havn't had any adds then the tuple->ttl must
2057 * be the original ttl and should be used for any
2062 ttl_good = ISC_TRUE;
2064 op = (tuple->op == DNS_DIFFOP_DEL) ?
2065 DNS_DIFFOP_ADD : DNS_DIFFOP_DEL;
2066 CHECK(dns_difftuple_create(diff->mctx, op, name,
2069 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2070 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2071 dns_diff_appendminimal(diff, &tuple);
2076 * We now have just the actual changes to the NSEC3PARAM RRset.
2077 * Convert the adds to delayed adds and the deletions into delayed
2080 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2081 tuple != NULL; tuple = next) {
2083 * If we havn't had any adds then the tuple->ttl must be the
2084 * original ttl and should be used for any future changes.
2088 ttl_good = ISC_TRUE;
2090 if (tuple->op == DNS_DIFFOP_ADD) {
2091 isc_boolean_t nseconly = ISC_FALSE;
2094 * Look for any deletes which match this ADD ignoring
2095 * flags. We don't need to explictly remove them as
2096 * they will be removed a side effect of processing
2099 next = ISC_LIST_HEAD(temp_diff.tuples);
2100 while (next != NULL) {
2101 unsigned char *next_data = next->rdata.data;
2102 unsigned char *tuple_data = tuple->rdata.data;
2103 if (next->op != DNS_DIFFOP_DEL ||
2104 next->rdata.length != tuple->rdata.length ||
2105 next_data[0] != tuple_data[0] ||
2106 next_data[2] != tuple_data[2] ||
2107 next_data[3] != tuple_data[3] ||
2108 memcmp(next_data + 4, tuple_data + 4,
2109 tuple->rdata.length - 4)) {
2110 next = ISC_LIST_NEXT(next, link);
2113 ISC_LIST_UNLINK(temp_diff.tuples, next, link);
2114 ISC_LIST_APPEND(diff->tuples, next, link);
2115 next = ISC_LIST_HEAD(temp_diff.tuples);
2119 * Create a private-type record to signal that
2120 * we want a delayed NSEC3 chain add/delete
2122 dns_nsec3param_toprivate(&tuple->rdata, &rdata,
2123 privatetype, buf, sizeof(buf));
2124 buf[2] |= DNS_NSEC3FLAG_CREATE;
2127 * If the zone is not currently capable of
2128 * supporting an NSEC3 chain, then we set the
2129 * INITIAL flag to indicate that these parameters
2130 * are to be used later.
2132 result = dns_nsec_nseconly(db, ver, &nseconly);
2133 if (result == ISC_R_NOTFOUND || nseconly)
2134 buf[2] |= DNS_NSEC3FLAG_INITIAL;
2137 * See if this CREATE request already exists.
2139 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2142 CHECK(dns_difftuple_create(diff->mctx,
2146 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2150 * Remove any existing CREATE request to add an
2151 * otherwise indentical chain with a reversed
2154 buf[2] ^= DNS_NSEC3FLAG_OPTOUT;
2155 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2158 CHECK(dns_difftuple_create(diff->mctx,
2162 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2166 * Find the next tuple to be processed and remove the
2167 * temporary add record.
2169 next = ISC_LIST_NEXT(tuple, link);
2170 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_DEL,
2171 name, ttl, &tuple->rdata,
2173 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2174 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2175 dns_diff_appendminimal(diff, &tuple);
2176 dns_rdata_reset(&rdata);
2178 next = ISC_LIST_NEXT(tuple, link);
2181 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2182 tuple != NULL; tuple = next) {
2186 next = ISC_LIST_NEXT(tuple, link);
2188 * See if we already have a REMOVE request in progress.
2190 dns_nsec3param_toprivate(&tuple->rdata, &rdata, privatetype,
2193 buf[2] |= DNS_NSEC3FLAG_REMOVE | DNS_NSEC3FLAG_NONSEC;
2195 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2197 buf[2] &= ~DNS_NSEC3FLAG_NONSEC;
2198 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2202 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD,
2203 name, 0, &rdata, &newtuple));
2204 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2206 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name,
2207 ttl, &tuple->rdata, &newtuple));
2208 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2209 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2210 dns_diff_appendminimal(diff, &tuple);
2211 dns_rdata_reset(&rdata);
2214 result = ISC_R_SUCCESS;
2216 dns_diff_clear(&temp_diff);
2221 rollback_private(dns_db_t *db, dns_rdatatype_t privatetype,
2222 dns_dbversion_t *ver, dns_diff_t *diff)
2224 dns_diff_t temp_diff;
2226 dns_difftuple_t *tuple, *newtuple = NULL, *next;
2227 dns_name_t *name = dns_db_origin(db);
2228 isc_mem_t *mctx = diff->mctx;
2229 isc_result_t result;
2231 if (privatetype == 0)
2232 return (ISC_R_SUCCESS);
2234 dns_diff_init(mctx, &temp_diff);
2237 * Extract the changes to be rolled back.
2239 for (tuple = ISC_LIST_HEAD(diff->tuples);
2240 tuple != NULL; tuple = next) {
2242 next = ISC_LIST_NEXT(tuple, link);
2244 if (tuple->rdata.type != privatetype ||
2245 !dns_name_equal(name, &tuple->name))
2249 * Allow records which indicate that a zone has been
2250 * signed with a DNSKEY to be removed.
2252 if (tuple->op == DNS_DIFFOP_DEL &&
2253 tuple->rdata.length == 5 &&
2254 tuple->rdata.data[0] != 0 &&
2255 tuple->rdata.data[4] != 0)
2258 ISC_LIST_UNLINK(diff->tuples, tuple, link);
2259 ISC_LIST_PREPEND(temp_diff.tuples, tuple, link);
2263 * Rollback the changes.
2265 while ((tuple = ISC_LIST_HEAD(temp_diff.tuples)) != NULL) {
2266 op = (tuple->op == DNS_DIFFOP_DEL) ?
2267 DNS_DIFFOP_ADD : DNS_DIFFOP_DEL;
2268 CHECK(dns_difftuple_create(mctx, op, name, tuple->ttl,
2269 &tuple->rdata, &newtuple));
2270 CHECK(do_one_tuple(&newtuple, db, ver, &temp_diff));
2272 result = ISC_R_SUCCESS;
2275 dns_diff_clear(&temp_diff);
2280 * Add records to cause the delayed signing of the zone by added DNSKEY
2281 * to remove the RRSIG records generated by a deleted DNSKEY.
2284 add_signing_records(dns_db_t *db, dns_rdatatype_t privatetype,
2285 dns_dbversion_t *ver, dns_diff_t *diff)
2287 dns_difftuple_t *tuple, *newtuple = NULL, *next;
2288 dns_rdata_dnskey_t dnskey;
2289 dns_rdata_t rdata = DNS_RDATA_INIT;
2292 isc_result_t result = ISC_R_SUCCESS;
2294 unsigned char buf[5];
2295 dns_name_t *name = dns_db_origin(db);
2296 dns_diff_t temp_diff;
2298 dns_diff_init(diff->mctx, &temp_diff);
2301 * Extract the DNSKEY tuples from the list.
2303 for (tuple = ISC_LIST_HEAD(diff->tuples);
2304 tuple != NULL; tuple = next) {
2306 next = ISC_LIST_NEXT(tuple, link);
2308 if (tuple->rdata.type != dns_rdatatype_dnskey)
2311 ISC_LIST_UNLINK(diff->tuples, tuple, link);
2312 ISC_LIST_APPEND(temp_diff.tuples, tuple, link);
2316 * Extract TTL changes pairs, we don't need signing records for these.
2318 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2319 tuple != NULL; tuple = next) {
2320 if (tuple->op == DNS_DIFFOP_ADD) {
2322 * Walk the temp_diff list looking for the
2323 * corresponding delete.
2325 next = ISC_LIST_HEAD(temp_diff.tuples);
2326 while (next != NULL) {
2327 unsigned char *next_data = next->rdata.data;
2328 unsigned char *tuple_data = tuple->rdata.data;
2329 if (next->op == DNS_DIFFOP_DEL &&
2330 dns_name_equal(&tuple->name, &next->name) &&
2331 next->rdata.length == tuple->rdata.length &&
2332 !memcmp(next_data, tuple_data,
2333 next->rdata.length)) {
2334 ISC_LIST_UNLINK(temp_diff.tuples, next,
2336 ISC_LIST_APPEND(diff->tuples, next,
2340 next = ISC_LIST_NEXT(next, link);
2343 * If we have not found a pair move onto the next
2347 next = ISC_LIST_NEXT(tuple, link);
2351 * Find the next tuple to be processed before
2352 * unlinking then complete moving the pair to 'diff'.
2354 next = ISC_LIST_NEXT(tuple, link);
2355 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2356 ISC_LIST_APPEND(diff->tuples, tuple, link);
2358 next = ISC_LIST_NEXT(tuple, link);
2362 * Process the remaining DNSKEY entries.
2364 for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
2366 tuple = ISC_LIST_HEAD(temp_diff.tuples)) {
2368 ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
2369 ISC_LIST_APPEND(diff->tuples, tuple, link);
2371 result = dns_rdata_tostruct(&tuple->rdata, &dnskey, NULL);
2372 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2374 (DNS_KEYFLAG_OWNERMASK|DNS_KEYTYPE_NOAUTH))
2375 != DNS_KEYOWNER_ZONE)
2378 dns_rdata_toregion(&tuple->rdata, &r);
2380 keyid = dst_region_computeid(&r, dnskey.algorithm);
2382 buf[0] = dnskey.algorithm;
2383 buf[1] = (keyid & 0xff00) >> 8;
2384 buf[2] = (keyid & 0xff);
2385 buf[3] = (tuple->op == DNS_DIFFOP_ADD) ? 0 : 1;
2388 rdata.length = sizeof(buf);
2389 rdata.type = privatetype;
2390 rdata.rdclass = tuple->rdata.rdclass;
2392 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2395 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD,
2396 name, 0, &rdata, &newtuple));
2397 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2398 INSIST(newtuple == NULL);
2400 * Remove any record which says this operation has already
2404 CHECK(rr_exists(db, ver, name, &rdata, &flag));
2406 CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_DEL,
2407 name, 0, &rdata, &newtuple));
2408 CHECK(do_one_tuple(&newtuple, db, ver, diff));
2409 INSIST(newtuple == NULL);
2414 dns_diff_clear(&temp_diff);
2418 static isc_boolean_t
2419 isdnssec(dns_db_t *db, dns_dbversion_t *ver, dns_rdatatype_t privatetype) {
2420 isc_result_t result;
2421 isc_boolean_t build_nsec, build_nsec3;
2423 if (dns_db_issecure(db))
2426 result = dns_private_chains(db, ver, privatetype,
2427 &build_nsec, &build_nsec3);
2428 RUNTIME_CHECK(result == ISC_R_SUCCESS);
2429 return (build_nsec || build_nsec3);
2433 update_action(isc_task_t *task, isc_event_t *event) {
2434 update_event_t *uev = (update_event_t *) event;
2435 dns_zone_t *zone = uev->zone;
2436 ns_client_t *client = (ns_client_t *)event->ev_arg;
2438 isc_result_t result;
2439 dns_db_t *db = NULL;
2440 dns_dbversion_t *oldver = NULL;
2441 dns_dbversion_t *ver = NULL;
2442 dns_diff_t diff; /* Pending updates. */
2443 dns_diff_t temp; /* Pending RR existence assertions. */
2444 isc_boolean_t soa_serial_changed = ISC_FALSE;
2445 isc_mem_t *mctx = client->mctx;
2446 dns_rdatatype_t covers;
2447 dns_message_t *request = client->message;
2448 dns_rdataclass_t zoneclass;
2449 dns_name_t *zonename;
2450 dns_ssutable_t *ssutable = NULL;
2451 dns_fixedname_t tmpnamefixed;
2452 dns_name_t *tmpname = NULL;
2453 unsigned int options;
2454 dns_difftuple_t *tuple;
2455 dns_rdata_dnskey_t dnskey;
2456 isc_boolean_t had_dnskey;
2457 dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
2459 INSIST(event->ev_type == DNS_EVENT_UPDATE);
2461 dns_diff_init(mctx, &diff);
2462 dns_diff_init(mctx, &temp);
2464 CHECK(dns_zone_getdb(zone, &db));
2465 zonename = dns_db_origin(db);
2466 zoneclass = dns_db_class(db);
2467 dns_zone_getssutable(zone, &ssutable);
2470 * Update message processing can leak record existance information
2471 * so check that we are allowed to query this zone. Additionally
2472 * if we would refuse all updates for this zone we bail out here.
2474 CHECK(checkqueryacl(client, dns_zone_getqueryacl(zone), zonename,
2475 dns_zone_getupdateacl(zone), ssutable));
2478 * Get old and new versions now that queryacl has been checked.
2480 dns_db_currentversion(db, &oldver);
2481 CHECK(dns_db_newversion(db, &ver));
2484 * Check prerequisites.
2487 for (result = dns_message_firstname(request, DNS_SECTION_PREREQUISITE);
2488 result == ISC_R_SUCCESS;
2489 result = dns_message_nextname(request, DNS_SECTION_PREREQUISITE))
2491 dns_name_t *name = NULL;
2492 dns_rdata_t rdata = DNS_RDATA_INIT;
2494 dns_rdataclass_t update_class;
2497 get_current_rr(request, DNS_SECTION_PREREQUISITE, zoneclass,
2498 &name, &rdata, &covers, &ttl, &update_class);
2501 PREREQFAILC(DNS_R_FORMERR,
2502 "prerequisite TTL is not zero");
2504 if (! dns_name_issubdomain(name, zonename))
2505 PREREQFAILN(DNS_R_NOTZONE, name,
2506 "prerequisite name is out of zone");
2508 if (update_class == dns_rdataclass_any) {
2509 if (rdata.length != 0)
2510 PREREQFAILC(DNS_R_FORMERR,
2511 "class ANY prerequisite "
2512 "RDATA is not empty");
2513 if (rdata.type == dns_rdatatype_any) {
2514 CHECK(name_exists(db, ver, name, &flag));
2516 PREREQFAILN(DNS_R_NXDOMAIN, name,
2522 CHECK(rrset_exists(db, ver, name,
2523 rdata.type, covers, &flag));
2525 /* RRset does not exist. */
2526 PREREQFAILNT(DNS_R_NXRRSET, name, rdata.type,
2527 "'rrset exists (value independent)' "
2528 "prerequisite not satisfied");
2531 } else if (update_class == dns_rdataclass_none) {
2532 if (rdata.length != 0)
2533 PREREQFAILC(DNS_R_FORMERR,
2534 "class NONE prerequisite "
2535 "RDATA is not empty");
2536 if (rdata.type == dns_rdatatype_any) {
2537 CHECK(name_exists(db, ver, name, &flag));
2539 PREREQFAILN(DNS_R_YXDOMAIN, name,
2540 "'name not in use' "
2545 CHECK(rrset_exists(db, ver, name,
2546 rdata.type, covers, &flag));
2549 PREREQFAILNT(DNS_R_YXRRSET, name,
2551 "'rrset does not exist' "
2556 } else if (update_class == zoneclass) {
2557 /* "temp<rr.name, rr.type> += rr;" */
2558 result = temp_append(&temp, name, &rdata);
2559 if (result != ISC_R_SUCCESS) {
2560 UNEXPECTED_ERROR(__FILE__, __LINE__,
2561 "temp entry creation failed: %s",
2562 dns_result_totext(result));
2563 FAIL(ISC_R_UNEXPECTED);
2566 PREREQFAILC(DNS_R_FORMERR, "malformed prerequisite");
2569 if (result != ISC_R_NOMORE)
2573 * Perform the final check of the "rrset exists (value dependent)"
2576 if (ISC_LIST_HEAD(temp.tuples) != NULL) {
2577 dns_rdatatype_t type;
2580 * Sort the prerequisite records by owner name,
2583 result = dns_diff_sort(&temp, temp_order);
2584 if (result != ISC_R_SUCCESS)
2585 FAILC(result, "'RRset exists (value dependent)' "
2586 "prerequisite not satisfied");
2588 dns_fixedname_init(&tmpnamefixed);
2589 tmpname = dns_fixedname_name(&tmpnamefixed);
2590 result = temp_check(mctx, &temp, db, ver, tmpname, &type);
2591 if (result != ISC_R_SUCCESS)
2592 FAILNT(result, tmpname, type,
2593 "'RRset exists (value dependent)' "
2594 "prerequisite not satisfied");
2597 update_log(client, zone, LOGLEVEL_DEBUG,
2598 "prerequisites are OK");
2601 * Check Requestor's Permissions. It seems a bit silly to do this
2602 * only after prerequisite testing, but that is what RFC2136 says.
2604 if (ssutable == NULL)
2605 CHECK(checkupdateacl(client, dns_zone_getupdateacl(zone),
2606 "update", zonename, ISC_FALSE, ISC_FALSE));
2607 else if (client->signer == NULL && !TCPCLIENT(client))
2608 CHECK(checkupdateacl(client, NULL, "update", zonename,
2609 ISC_FALSE, ISC_TRUE));
2611 if (dns_zone_getupdatedisabled(zone))
2612 FAILC(DNS_R_REFUSED, "dynamic update temporarily disabled "
2613 "because the zone is frozen. Use "
2614 "'rndc thaw' to re-enable updates.");
2617 * Perform the Update Section Prescan.
2620 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2621 result == ISC_R_SUCCESS;
2622 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2624 dns_name_t *name = NULL;
2625 dns_rdata_t rdata = DNS_RDATA_INIT;
2627 dns_rdataclass_t update_class;
2628 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2629 &name, &rdata, &covers, &ttl, &update_class);
2631 if (! dns_name_issubdomain(name, zonename))
2632 FAILC(DNS_R_NOTZONE,
2633 "update RR is outside zone");
2634 if (update_class == zoneclass) {
2636 * Check for meta-RRs. The RFC2136 pseudocode says
2637 * check for ANY|AXFR|MAILA|MAILB, but the text adds
2638 * "or any other QUERY metatype"
2640 if (dns_rdatatype_ismeta(rdata.type)) {
2641 FAILC(DNS_R_FORMERR,
2642 "meta-RR in update");
2644 result = dns_zone_checknames(zone, name, &rdata);
2645 if (result != ISC_R_SUCCESS)
2646 FAIL(DNS_R_REFUSED);
2647 } else if (update_class == dns_rdataclass_any) {
2648 if (ttl != 0 || rdata.length != 0 ||
2649 (dns_rdatatype_ismeta(rdata.type) &&
2650 rdata.type != dns_rdatatype_any))
2651 FAILC(DNS_R_FORMERR,
2652 "meta-RR in update");
2653 } else if (update_class == dns_rdataclass_none) {
2655 dns_rdatatype_ismeta(rdata.type))
2656 FAILC(DNS_R_FORMERR,
2657 "meta-RR in update");
2659 update_log(client, zone, ISC_LOG_WARNING,
2660 "update RR has incorrect class %d",
2662 FAIL(DNS_R_FORMERR);
2666 * draft-ietf-dnsind-simple-secure-update-01 says
2667 * "Unlike traditional dynamic update, the client
2668 * is forbidden from updating NSEC records."
2670 if (rdata.type == dns_rdatatype_nsec3) {
2671 FAILC(DNS_R_REFUSED,
2672 "explicit NSEC3 updates are not allowed "
2674 } else if (rdata.type == dns_rdatatype_nsec) {
2675 FAILC(DNS_R_REFUSED,
2676 "explicit NSEC updates are not allowed "
2678 } else if (rdata.type == dns_rdatatype_rrsig &&
2679 !dns_name_equal(name, zonename)) {
2680 FAILC(DNS_R_REFUSED,
2681 "explicit RRSIG updates are currently "
2682 "not supported in secure zones except "
2686 if (ssutable != NULL) {
2687 isc_netaddr_t *tcpaddr, netaddr;
2688 dst_key_t *tsigkey = NULL;
2690 * If this is a TCP connection then pass the
2691 * address of the client through for tcp-self
2692 * and 6to4-self otherwise pass NULL. This
2693 * provides weak address based authentication.
2695 if (TCPCLIENT(client)) {
2696 isc_netaddr_fromsockaddr(&netaddr,
2702 if (client->message->tsigkey != NULL)
2703 tsigkey = client->message->tsigkey->key;
2705 if (rdata.type != dns_rdatatype_any) {
2706 if (!dns_ssutable_checkrules(ssutable,
2711 FAILC(DNS_R_REFUSED,
2712 "rejected by secure update");
2714 if (!ssu_checkall(db, ver, name, ssutable,
2715 client->signer, tcpaddr,
2717 FAILC(DNS_R_REFUSED,
2718 "rejected by secure update");
2722 if (result != ISC_R_NOMORE)
2725 update_log(client, zone, LOGLEVEL_DEBUG,
2726 "update section prescan OK");
2729 * Process the Update Section.
2732 options = dns_zone_getoptions(zone);
2733 for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
2734 result == ISC_R_SUCCESS;
2735 result = dns_message_nextname(request, DNS_SECTION_UPDATE))
2737 dns_name_t *name = NULL;
2738 dns_rdata_t rdata = DNS_RDATA_INIT;
2740 dns_rdataclass_t update_class;
2743 get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
2744 &name, &rdata, &covers, &ttl, &update_class);
2746 if (update_class == zoneclass) {
2749 * RFC1123 doesn't allow MF and MD in master zones. */
2750 if (rdata.type == dns_rdatatype_md ||
2751 rdata.type == dns_rdatatype_mf) {
2752 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2754 dns_rdatatype_format(rdata.type, typebuf,
2756 update_log(client, zone, LOGLEVEL_PROTOCOL,
2757 "attempt to add %s ignored",
2761 if ((rdata.type == dns_rdatatype_ns ||
2762 rdata.type == dns_rdatatype_dname) &&
2763 dns_name_iswildcard(name)) {
2764 char typebuf[DNS_RDATATYPE_FORMATSIZE];
2766 dns_rdatatype_format(rdata.type, typebuf,
2768 update_log(client, zone,
2770 "attempt to add wildcard %s record "
2771 "ignored", typebuf);
2774 if (rdata.type == dns_rdatatype_cname) {
2775 CHECK(cname_incompatible_rrset_exists(db, ver,
2779 update_log(client, zone,
2781 "attempt to add CNAME "
2782 "alongside non-CNAME "
2787 CHECK(rrset_exists(db, ver, name,
2788 dns_rdatatype_cname, 0,
2791 ! dns_rdatatype_isdnssec(rdata.type))
2793 update_log(client, zone,
2795 "attempt to add non-CNAME "
2796 "alongside CNAME ignored");
2800 if (rdata.type == dns_rdatatype_soa) {
2802 CHECK(rrset_exists(db, ver, name,
2803 dns_rdatatype_soa, 0,
2806 update_log(client, zone,
2808 "attempt to create 2nd "
2812 CHECK(check_soa_increment(db, ver, &rdata,
2815 update_log(client, zone,
2817 "SOA update failed to "
2818 "increment serial, "
2822 soa_serial_changed = ISC_TRUE;
2825 if (rdata.type == privatetype) {
2826 update_log(client, zone, LOGLEVEL_PROTOCOL,
2827 "attempt to add a private type "
2828 "(%u) record rejected internal "
2829 "use only", privatetype);
2833 if (rdata.type == dns_rdatatype_nsec3param) {
2835 * Ignore attempts to add NSEC3PARAM records
2836 * with any flags other than OPTOUT.
2838 if ((rdata.data[1] & ~DNS_NSEC3FLAG_OPTOUT) != 0) {
2839 update_log(client, zone,
2841 "attempt to add NSEC3PARAM "
2842 "record with non OPTOUT "
2848 if ((options & DNS_ZONEOPT_CHECKWILDCARD) != 0 &&
2849 dns_name_internalwildcard(name)) {
2850 char namestr[DNS_NAME_FORMATSIZE];
2851 dns_name_format(name, namestr,
2853 update_log(client, zone, LOGLEVEL_PROTOCOL,
2854 "warning: ownername '%s' contains "
2855 "a non-terminal wildcard", namestr);
2858 if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) {
2859 char namestr[DNS_NAME_FORMATSIZE];
2860 char typestr[DNS_RDATATYPE_FORMATSIZE];
2861 dns_name_format(name, namestr,
2863 dns_rdatatype_format(rdata.type, typestr,
2865 update_log(client, zone, LOGLEVEL_PROTOCOL,
2866 "adding an RR at '%s' %s",
2870 /* Prepare the affected RRset for the addition. */
2872 add_rr_prepare_ctx_t ctx;
2877 ctx.update_rr = &rdata;
2878 ctx.update_rr_ttl = ttl;
2879 ctx.ignore_add = ISC_FALSE;
2880 dns_diff_init(mctx, &ctx.del_diff);
2881 dns_diff_init(mctx, &ctx.add_diff);
2882 CHECK(foreach_rr(db, ver, name, rdata.type,
2883 covers, add_rr_prepare_action,
2886 if (ctx.ignore_add) {
2887 dns_diff_clear(&ctx.del_diff);
2888 dns_diff_clear(&ctx.add_diff);
2890 CHECK(do_diff(&ctx.del_diff, db, ver,
2892 CHECK(do_diff(&ctx.add_diff, db, ver,
2894 CHECK(update_one_rr(db, ver, &diff,
2896 name, ttl, &rdata));
2899 } else if (update_class == dns_rdataclass_any) {
2900 if (rdata.type == dns_rdatatype_any) {
2901 if (isc_log_wouldlog(ns_g_lctx,
2904 char namestr[DNS_NAME_FORMATSIZE];
2905 dns_name_format(name, namestr,
2907 update_log(client, zone,
2909 "delete all rrsets from "
2910 "name '%s'", namestr);
2912 if (dns_name_equal(name, zonename)) {
2913 CHECK(delete_if(type_not_soa_nor_ns_p,
2915 dns_rdatatype_any, 0,
2918 CHECK(delete_if(type_not_dnssec,
2920 dns_rdatatype_any, 0,
2923 } else if (dns_name_equal(name, zonename) &&
2924 (rdata.type == dns_rdatatype_soa ||
2925 rdata.type == dns_rdatatype_ns)) {
2926 update_log(client, zone, LOGLEVEL_PROTOCOL,
2927 "attempt to delete all SOA "
2928 "or NS records ignored");
2931 if (isc_log_wouldlog(ns_g_lctx,
2934 char namestr[DNS_NAME_FORMATSIZE];
2935 char typestr[DNS_RDATATYPE_FORMATSIZE];
2936 dns_name_format(name, namestr,
2938 dns_rdatatype_format(rdata.type,
2941 update_log(client, zone,
2943 "deleting rrset at '%s' %s",
2946 CHECK(delete_if(true_p, db, ver, name,
2947 rdata.type, covers, &rdata,
2950 } else if (update_class == dns_rdataclass_none) {
2951 char namestr[DNS_NAME_FORMATSIZE];
2952 char typestr[DNS_RDATATYPE_FORMATSIZE];
2955 * The (name == zonename) condition appears in
2956 * RFC2136 3.4.2.4 but is missing from the pseudocode.
2958 if (dns_name_equal(name, zonename)) {
2959 if (rdata.type == dns_rdatatype_soa) {
2960 update_log(client, zone,
2962 "attempt to delete SOA "
2966 if (rdata.type == dns_rdatatype_ns) {
2968 CHECK(rr_count(db, ver, name,
2972 update_log(client, zone,
2981 dns_name_format(name, namestr, sizeof(namestr));
2982 dns_rdatatype_format(rdata.type, typestr,
2984 update_log(client, zone, LOGLEVEL_PROTOCOL,
2985 "deleting an RR at %s %s", namestr, typestr);
2986 CHECK(delete_if(rr_equal_p, db, ver, name, rdata.type,
2987 covers, &rdata, &diff));
2990 if (result != ISC_R_NOMORE)
2994 * Check that any changes to DNSKEY/NSEC3PARAM records make sense.
2995 * If they don't then back out all changes to DNSKEY/NSEC3PARAM
2998 if (! ISC_LIST_EMPTY(diff.tuples))
2999 CHECK(check_dnssec(client, zone, db, ver, &diff));
3001 if (! ISC_LIST_EMPTY(diff.tuples)) {
3002 unsigned int errors = 0;
3003 CHECK(dns_zone_nscheck(zone, db, ver, &errors));
3005 update_log(client, zone, LOGLEVEL_PROTOCOL,
3006 "update rejected: post update name server "
3007 "sanity check failed");
3008 result = DNS_R_REFUSED;
3014 * If any changes were made, increment the SOA serial number,
3015 * update RRSIGs and NSECs (if zone is secure), and write the update
3018 if (! ISC_LIST_EMPTY(diff.tuples)) {
3020 dns_journal_t *journal;
3021 isc_boolean_t has_dnskey;
3024 * Increment the SOA serial, but only if it was not
3025 * changed as a result of an update operation.
3027 if (! soa_serial_changed) {
3028 CHECK(update_soa_serial(db, ver, &diff, mctx,
3029 dns_zone_getserialupdatemethod(zone)));
3032 CHECK(check_mx(client, zone, db, ver, &diff));
3034 CHECK(remove_orphaned_ds(db, ver, &diff));
3036 CHECK(rrset_exists(db, ver, zonename, dns_rdatatype_dnskey,
3039 #define ALLOW_SECURE_TO_INSECURE(zone) \
3040 ((dns_zone_getoptions(zone) & DNS_ZONEOPT_SECURETOINSECURE) != 0)
3042 if (!ALLOW_SECURE_TO_INSECURE(zone)) {
3043 CHECK(rrset_exists(db, oldver, zonename,
3044 dns_rdatatype_dnskey, 0,
3046 if (had_dnskey && !has_dnskey) {
3047 update_log(client, zone, LOGLEVEL_PROTOCOL,
3048 "update rejected: all DNSKEY "
3049 "records removed and "
3050 "'dnssec-secure-to-insecure' "
3052 result = DNS_R_REFUSED;
3057 CHECK(rollback_private(db, privatetype, ver, &diff));
3059 CHECK(add_signing_records(db, privatetype, ver, &diff));
3061 CHECK(add_nsec3param_records(client, zone, db, ver, &diff));
3063 if (had_dnskey && !has_dnskey) {
3065 * We are transitioning from secure to insecure.
3066 * Cause all NSEC3 chains to be deleted. When the
3067 * the last signature for the DNSKEY records are
3068 * remove any NSEC chain present will also be removed.
3070 CHECK(dns_nsec3param_deletechains(db, ver, zone,
3072 } else if (has_dnskey && isdnssec(db, ver, privatetype)) {
3073 isc_uint32_t interval;
3074 dns_update_log_t log;
3076 interval = dns_zone_getsigvalidityinterval(zone);
3077 log.func = update_log_cb;
3079 result = dns_update_signatures(&log, zone, db, oldver,
3080 ver, &diff, interval);
3082 if (result != ISC_R_SUCCESS) {
3083 update_log(client, zone,
3085 "RRSIG/NSEC/NSEC3 update failed: %s",
3086 isc_result_totext(result));
3091 journalfile = dns_zone_getjournal(zone);
3092 if (journalfile != NULL) {
3093 update_log(client, zone, LOGLEVEL_DEBUG,
3094 "writing journal %s", journalfile);
3097 result = dns_journal_open(mctx, journalfile,
3098 DNS_JOURNAL_CREATE, &journal);
3099 if (result != ISC_R_SUCCESS)
3100 FAILS(result, "journal open failed");
3102 result = dns_journal_write_transaction(journal, &diff);
3103 if (result != ISC_R_SUCCESS) {
3104 dns_journal_destroy(&journal);
3105 FAILS(result, "journal write failed");
3108 dns_journal_destroy(&journal);
3112 * XXXRTH Just a note that this committing code will have
3113 * to change to handle databases that need two-phase
3114 * commit, but this isn't a priority.
3116 update_log(client, zone, LOGLEVEL_DEBUG,
3117 "committing update transaction");
3119 dns_db_closeversion(db, &ver, ISC_TRUE);
3122 * Mark the zone as dirty so that it will be written to disk.
3124 dns_zone_markdirty(zone);
3127 * Notify slaves of the change we just made.
3129 dns_zone_notify(zone);
3132 * Cause the zone to be signed with the key that we
3133 * have just added or have the corresponding signatures
3136 * Note: we are already committed to this course of action.
3138 for (tuple = ISC_LIST_HEAD(diff.tuples);
3140 tuple = ISC_LIST_NEXT(tuple, link)) {
3142 dns_secalg_t algorithm;
3145 if (tuple->rdata.type != dns_rdatatype_dnskey)
3148 dns_rdata_tostruct(&tuple->rdata, &dnskey, NULL);
3150 (DNS_KEYFLAG_OWNERMASK|DNS_KEYTYPE_NOAUTH))
3151 != DNS_KEYOWNER_ZONE)
3154 dns_rdata_toregion(&tuple->rdata, &r);
3155 algorithm = dnskey.algorithm;
3156 keyid = dst_region_computeid(&r, algorithm);
3158 result = dns_zone_signwithkey(zone, algorithm, keyid,
3159 ISC_TF(tuple->op == DNS_DIFFOP_DEL));
3160 if (result != ISC_R_SUCCESS) {
3161 update_log(client, zone, ISC_LOG_ERROR,
3162 "dns_zone_signwithkey failed: %s",
3163 dns_result_totext(result));
3168 * Cause the zone to add/delete NSEC3 chains for the
3169 * deferred NSEC3PARAM changes.
3171 * Note: we are already committed to this course of action.
3173 for (tuple = ISC_LIST_HEAD(diff.tuples);
3175 tuple = ISC_LIST_NEXT(tuple, link)) {
3176 unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
3177 dns_rdata_t rdata = DNS_RDATA_INIT;
3178 dns_rdata_nsec3param_t nsec3param;
3180 if (tuple->rdata.type != privatetype ||
3181 tuple->op != DNS_DIFFOP_ADD)
3184 if (!dns_nsec3param_fromprivate(&tuple->rdata, &rdata,
3187 dns_rdata_tostruct(&rdata, &nsec3param, NULL);
3188 if (nsec3param.flags == 0)
3191 result = dns_zone_addnsec3chain(zone, &nsec3param);
3192 if (result != ISC_R_SUCCESS) {
3193 update_log(client, zone, ISC_LOG_ERROR,
3194 "dns_zone_addnsec3chain failed: %s",
3195 dns_result_totext(result));
3199 update_log(client, zone, LOGLEVEL_DEBUG, "redundant request");
3200 dns_db_closeversion(db, &ver, ISC_TRUE);
3202 result = ISC_R_SUCCESS;
3207 * The reason for failure should have been logged at this point.
3210 update_log(client, zone, LOGLEVEL_DEBUG,
3212 dns_db_closeversion(db, &ver, ISC_FALSE);
3216 dns_diff_clear(&temp);
3217 dns_diff_clear(&diff);
3220 dns_db_closeversion(db, &oldver, ISC_FALSE);
3225 if (ssutable != NULL)
3226 dns_ssutable_detach(&ssutable);
3228 isc_task_detach(&task);
3229 uev->result = result;
3231 INSIST(uev->zone == zone); /* we use this later */
3232 uev->ev_type = DNS_EVENT_UPDATEDONE;
3233 uev->ev_action = updatedone_action;
3234 isc_task_send(client->task, &event);
3235 INSIST(event == NULL);
3239 updatedone_action(isc_task_t *task, isc_event_t *event) {
3240 update_event_t *uev = (update_event_t *) event;
3241 ns_client_t *client = (ns_client_t *) event->ev_arg;
3245 INSIST(event->ev_type == DNS_EVENT_UPDATEDONE);
3246 INSIST(task == client->task);
3248 INSIST(client->nupdates > 0);
3249 switch (uev->result) {
3251 inc_stats(uev->zone, dns_nsstatscounter_updatedone);
3254 inc_stats(uev->zone, dns_nsstatscounter_updaterej);
3257 inc_stats(uev->zone, dns_nsstatscounter_updatefail);
3260 if (uev->zone != NULL)
3261 dns_zone_detach(&uev->zone);
3263 respond(client, uev->result);
3264 isc_event_free(&event);
3265 ns_client_detach(&client);
3269 * Update forwarding support.
3273 forward_fail(isc_task_t *task, isc_event_t *event) {
3274 ns_client_t *client = (ns_client_t *)event->ev_arg;
3278 INSIST(client->nupdates > 0);
3280 respond(client, DNS_R_SERVFAIL);
3281 isc_event_free(&event);
3282 ns_client_detach(&client);
3287 forward_callback(void *arg, isc_result_t result, dns_message_t *answer) {
3288 update_event_t *uev = arg;
3289 ns_client_t *client = uev->ev_arg;
3290 dns_zone_t *zone = uev->zone;
3292 if (result != ISC_R_SUCCESS) {
3293 INSIST(answer == NULL);
3294 uev->ev_type = DNS_EVENT_UPDATEDONE;
3295 uev->ev_action = forward_fail;
3296 inc_stats(zone, dns_nsstatscounter_updatefwdfail);
3298 uev->ev_type = DNS_EVENT_UPDATEDONE;
3299 uev->ev_action = forward_done;
3300 uev->answer = answer;
3301 inc_stats(zone, dns_nsstatscounter_updaterespfwd);
3303 isc_task_send(client->task, ISC_EVENT_PTR(&uev));
3304 dns_zone_detach(&zone);
3308 forward_done(isc_task_t *task, isc_event_t *event) {
3309 update_event_t *uev = (update_event_t *) event;
3310 ns_client_t *client = (ns_client_t *)event->ev_arg;
3314 INSIST(client->nupdates > 0);
3316 ns_client_sendraw(client, uev->answer);
3317 dns_message_destroy(&uev->answer);
3318 isc_event_free(&event);
3319 ns_client_detach(&client);
3323 forward_action(isc_task_t *task, isc_event_t *event) {
3324 update_event_t *uev = (update_event_t *) event;
3325 dns_zone_t *zone = uev->zone;
3326 ns_client_t *client = (ns_client_t *)event->ev_arg;
3327 isc_result_t result;
3329 result = dns_zone_forwardupdate(zone, client->message,
3330 forward_callback, event);
3331 if (result != ISC_R_SUCCESS) {
3332 uev->ev_type = DNS_EVENT_UPDATEDONE;
3333 uev->ev_action = forward_fail;
3334 isc_task_send(client->task, &event);
3335 inc_stats(zone, dns_nsstatscounter_updatefwdfail);
3336 dns_zone_detach(&zone);
3338 inc_stats(zone, dns_nsstatscounter_updatereqfwd);
3339 isc_task_detach(&task);
3343 send_forward_event(ns_client_t *client, dns_zone_t *zone) {
3344 char namebuf[DNS_NAME_FORMATSIZE];
3345 char classbuf[DNS_RDATACLASS_FORMATSIZE];
3346 isc_result_t result = ISC_R_SUCCESS;
3347 update_event_t *event = NULL;
3348 isc_task_t *zonetask = NULL;
3349 ns_client_t *evclient;
3352 * This may take some time so replace this client.
3354 if (!client->mortal && (client->attributes & NS_CLIENTATTR_TCP) == 0)
3355 CHECK(ns_client_replace(client));
3357 event = (update_event_t *)
3358 isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
3359 forward_action, NULL, sizeof(*event));
3361 FAIL(ISC_R_NOMEMORY);
3363 event->result = ISC_R_SUCCESS;
3366 ns_client_attach(client, &evclient);
3367 INSIST(client->nupdates == 0);
3369 event->ev_arg = evclient;
3371 dns_name_format(dns_zone_getorigin(zone), namebuf,
3373 dns_rdataclass_format(dns_zone_getclass(zone), classbuf,
3376 ns_client_log(client, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
3377 LOGLEVEL_PROTOCOL, "forwarding update for zone '%s/%s'",
3380 dns_zone_gettask(zone, &zonetask);
3381 isc_task_send(zonetask, ISC_EVENT_PTR(&event));
3385 isc_event_free(ISC_EVENT_PTR(&event));