PageRenderTime 70ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/bind9/bin/named/update.c

https://bitbucket.org/freebsd/freebsd-head/
C | 4501 lines | 3106 code | 477 blank | 918 comment | 892 complexity | 9efd4520648bfa750d627d72b8b713d9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.0, LGPL-2.1, BSD-2-Clause, 0BSD, JSON, AGPL-1.0, GPL-2.0
  1. /*
  2. * Copyright (C) 2004-2011 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1999-2003 Internet Software Consortium.
  4. *
  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.
  8. *
  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.
  16. */
  17. /* $Id: update.c,v 1.186.16.7 2011/11/03 02:55:34 each Exp $ */
  18. #include <config.h>
  19. #include <isc/netaddr.h>
  20. #include <isc/print.h>
  21. #include <isc/serial.h>
  22. #include <isc/stats.h>
  23. #include <isc/string.h>
  24. #include <isc/taskpool.h>
  25. #include <isc/util.h>
  26. #include <dns/db.h>
  27. #include <dns/dbiterator.h>
  28. #include <dns/diff.h>
  29. #include <dns/dnssec.h>
  30. #include <dns/events.h>
  31. #include <dns/fixedname.h>
  32. #include <dns/journal.h>
  33. #include <dns/keyvalues.h>
  34. #include <dns/message.h>
  35. #include <dns/nsec.h>
  36. #include <dns/nsec3.h>
  37. #include <dns/private.h>
  38. #include <dns/rdataclass.h>
  39. #include <dns/rdataset.h>
  40. #include <dns/rdatasetiter.h>
  41. #include <dns/rdatastruct.h>
  42. #include <dns/rdatatype.h>
  43. #include <dns/soa.h>
  44. #include <dns/ssu.h>
  45. #include <dns/tsig.h>
  46. #include <dns/view.h>
  47. #include <dns/zone.h>
  48. #include <dns/zt.h>
  49. #include <named/client.h>
  50. #include <named/log.h>
  51. #include <named/server.h>
  52. #include <named/update.h>
  53. /*! \file
  54. * \brief
  55. * This module implements dynamic update as in RFC2136.
  56. */
  57. /*
  58. * XXX TODO:
  59. * - document strict minimality
  60. */
  61. /**************************************************************************/
  62. /*%
  63. * Log level for tracing dynamic update protocol requests.
  64. */
  65. #define LOGLEVEL_PROTOCOL ISC_LOG_INFO
  66. /*%
  67. * Log level for low-level debug tracing.
  68. */
  69. #define LOGLEVEL_DEBUG ISC_LOG_DEBUG(8)
  70. /*%
  71. * Check an operation for failure. These macros all assume that
  72. * the function using them has a 'result' variable and a 'failure'
  73. * label.
  74. */
  75. #define CHECK(op) \
  76. do { result = (op); \
  77. if (result != ISC_R_SUCCESS) goto failure; \
  78. } while (0)
  79. /*%
  80. * Fail unconditionally with result 'code', which must not
  81. * be ISC_R_SUCCESS. The reason for failure presumably has
  82. * been logged already.
  83. *
  84. * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
  85. * from complaining about "end-of-loop code not reached".
  86. */
  87. #define FAIL(code) \
  88. do { \
  89. result = (code); \
  90. if (result != ISC_R_SUCCESS) goto failure; \
  91. } while (0)
  92. /*%
  93. * Fail unconditionally and log as a client error.
  94. * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
  95. * from complaining about "end-of-loop code not reached".
  96. */
  97. #define FAILC(code, msg) \
  98. do { \
  99. const char *_what = "failed"; \
  100. result = (code); \
  101. switch (result) { \
  102. case DNS_R_NXDOMAIN: \
  103. case DNS_R_YXDOMAIN: \
  104. case DNS_R_YXRRSET: \
  105. case DNS_R_NXRRSET: \
  106. _what = "unsuccessful"; \
  107. } \
  108. update_log(client, zone, LOGLEVEL_PROTOCOL, \
  109. "update %s: %s (%s)", _what, \
  110. msg, isc_result_totext(result)); \
  111. if (result != ISC_R_SUCCESS) goto failure; \
  112. } while (0)
  113. #define PREREQFAILC(code, msg) \
  114. do { \
  115. inc_stats(zone, dns_nsstatscounter_updatebadprereq); \
  116. FAILC(code, msg); \
  117. } while (0)
  118. #define FAILN(code, name, msg) \
  119. do { \
  120. const char *_what = "failed"; \
  121. result = (code); \
  122. switch (result) { \
  123. case DNS_R_NXDOMAIN: \
  124. case DNS_R_YXDOMAIN: \
  125. case DNS_R_YXRRSET: \
  126. case DNS_R_NXRRSET: \
  127. _what = "unsuccessful"; \
  128. } \
  129. if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
  130. char _nbuf[DNS_NAME_FORMATSIZE]; \
  131. dns_name_format(name, _nbuf, sizeof(_nbuf)); \
  132. update_log(client, zone, LOGLEVEL_PROTOCOL, \
  133. "update %s: %s: %s (%s)", _what, _nbuf, \
  134. msg, isc_result_totext(result)); \
  135. } \
  136. if (result != ISC_R_SUCCESS) goto failure; \
  137. } while (0)
  138. #define PREREQFAILN(code, name, msg) \
  139. do { \
  140. inc_stats(zone, dns_nsstatscounter_updatebadprereq); \
  141. FAILN(code, name, msg); \
  142. } while (0)
  143. #define FAILNT(code, name, type, msg) \
  144. do { \
  145. const char *_what = "failed"; \
  146. result = (code); \
  147. switch (result) { \
  148. case DNS_R_NXDOMAIN: \
  149. case DNS_R_YXDOMAIN: \
  150. case DNS_R_YXRRSET: \
  151. case DNS_R_NXRRSET: \
  152. _what = "unsuccessful"; \
  153. } \
  154. if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) { \
  155. char _nbuf[DNS_NAME_FORMATSIZE]; \
  156. char _tbuf[DNS_RDATATYPE_FORMATSIZE]; \
  157. dns_name_format(name, _nbuf, sizeof(_nbuf)); \
  158. dns_rdatatype_format(type, _tbuf, sizeof(_tbuf)); \
  159. update_log(client, zone, LOGLEVEL_PROTOCOL, \
  160. "update %s: %s/%s: %s (%s)", \
  161. _what, _nbuf, _tbuf, msg, \
  162. isc_result_totext(result)); \
  163. } \
  164. if (result != ISC_R_SUCCESS) goto failure; \
  165. } while (0)
  166. #define PREREQFAILNT(code, name, type, msg) \
  167. do { \
  168. inc_stats(zone, dns_nsstatscounter_updatebadprereq); \
  169. FAILNT(code, name, type, msg); \
  170. } while (0)
  171. /*%
  172. * Fail unconditionally and log as a server error.
  173. * The test against ISC_R_SUCCESS is there to keep the Solaris compiler
  174. * from complaining about "end-of-loop code not reached".
  175. */
  176. #define FAILS(code, msg) \
  177. do { \
  178. result = (code); \
  179. update_log(client, zone, LOGLEVEL_PROTOCOL, \
  180. "error: %s: %s", \
  181. msg, isc_result_totext(result)); \
  182. if (result != ISC_R_SUCCESS) goto failure; \
  183. } while (0)
  184. /*
  185. * Return TRUE if NS_CLIENTATTR_TCP is set in the attributes other FALSE.
  186. */
  187. #define TCPCLIENT(client) (((client)->attributes & NS_CLIENTATTR_TCP) != 0)
  188. /**************************************************************************/
  189. typedef struct rr rr_t;
  190. struct rr {
  191. /* dns_name_t name; */
  192. isc_uint32_t ttl;
  193. dns_rdata_t rdata;
  194. };
  195. typedef struct update_event update_event_t;
  196. struct update_event {
  197. ISC_EVENT_COMMON(update_event_t);
  198. dns_zone_t *zone;
  199. isc_result_t result;
  200. dns_message_t *answer;
  201. };
  202. /**************************************************************************/
  203. /*
  204. * Forward declarations.
  205. */
  206. static void update_action(isc_task_t *task, isc_event_t *event);
  207. static void updatedone_action(isc_task_t *task, isc_event_t *event);
  208. static isc_result_t send_forward_event(ns_client_t *client, dns_zone_t *zone);
  209. static void forward_done(isc_task_t *task, isc_event_t *event);
  210. /**************************************************************************/
  211. static void
  212. update_log(ns_client_t *client, dns_zone_t *zone,
  213. int level, const char *fmt, ...) ISC_FORMAT_PRINTF(4, 5);
  214. static void
  215. update_log(ns_client_t *client, dns_zone_t *zone,
  216. int level, const char *fmt, ...)
  217. {
  218. va_list ap;
  219. char message[4096];
  220. char namebuf[DNS_NAME_FORMATSIZE];
  221. char classbuf[DNS_RDATACLASS_FORMATSIZE];
  222. if (client == NULL || zone == NULL)
  223. return;
  224. if (isc_log_wouldlog(ns_g_lctx, level) == ISC_FALSE)
  225. return;
  226. dns_name_format(dns_zone_getorigin(zone), namebuf,
  227. sizeof(namebuf));
  228. dns_rdataclass_format(dns_zone_getclass(zone), classbuf,
  229. sizeof(classbuf));
  230. va_start(ap, fmt);
  231. vsnprintf(message, sizeof(message), fmt, ap);
  232. va_end(ap);
  233. ns_client_log(client, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
  234. level, "updating zone '%s/%s': %s",
  235. namebuf, classbuf, message);
  236. }
  237. /*%
  238. * Increment updated-related statistics counters.
  239. */
  240. static inline void
  241. inc_stats(dns_zone_t *zone, isc_statscounter_t counter) {
  242. isc_stats_increment(ns_g_server->nsstats, counter);
  243. if (zone != NULL) {
  244. isc_stats_t *zonestats = dns_zone_getrequeststats(zone);
  245. if (zonestats != NULL)
  246. isc_stats_increment(zonestats, counter);
  247. }
  248. }
  249. /*%
  250. * Check if we could have queried for the contents of this zone or
  251. * if the zone is potentially updateable.
  252. * If the zone can potentially be updated and the check failed then
  253. * log a error otherwise we log a informational message.
  254. */
  255. static isc_result_t
  256. checkqueryacl(ns_client_t *client, dns_acl_t *queryacl, dns_name_t *zonename,
  257. dns_acl_t *updateacl, dns_ssutable_t *ssutable)
  258. {
  259. char namebuf[DNS_NAME_FORMATSIZE];
  260. char classbuf[DNS_RDATACLASS_FORMATSIZE];
  261. int level;
  262. isc_result_t result;
  263. result = ns_client_checkaclsilent(client, NULL, queryacl, ISC_TRUE);
  264. if (result != ISC_R_SUCCESS) {
  265. dns_name_format(zonename, namebuf, sizeof(namebuf));
  266. dns_rdataclass_format(client->view->rdclass, classbuf,
  267. sizeof(classbuf));
  268. level = (updateacl == NULL && ssutable == NULL) ?
  269. ISC_LOG_INFO : ISC_LOG_ERROR;
  270. ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
  271. NS_LOGMODULE_UPDATE, level,
  272. "update '%s/%s' denied due to allow-query",
  273. namebuf, classbuf);
  274. } else if (updateacl == NULL && ssutable == NULL) {
  275. dns_name_format(zonename, namebuf, sizeof(namebuf));
  276. dns_rdataclass_format(client->view->rdclass, classbuf,
  277. sizeof(classbuf));
  278. result = DNS_R_REFUSED;
  279. ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
  280. NS_LOGMODULE_UPDATE, ISC_LOG_INFO,
  281. "update '%s/%s' denied", namebuf, classbuf);
  282. }
  283. return (result);
  284. }
  285. /*%
  286. * Override the default acl logging when checking whether a client
  287. * can update the zone or whether we can forward the request to the
  288. * master based on IP address.
  289. *
  290. * 'message' contains the type of operation that is being attempted.
  291. * 'slave' indicates if this is a slave zone. If 'acl' is NULL then
  292. * log at debug=3.
  293. * If the zone has no access controls configured ('acl' == NULL &&
  294. * 'has_ssutable == ISC_FALS) log the attempt at info, otherwise
  295. * at error.
  296. *
  297. * If the request was signed log that we received it.
  298. */
  299. static isc_result_t
  300. checkupdateacl(ns_client_t *client, dns_acl_t *acl, const char *message,
  301. dns_name_t *zonename, isc_boolean_t slave,
  302. isc_boolean_t has_ssutable)
  303. {
  304. char namebuf[DNS_NAME_FORMATSIZE];
  305. char classbuf[DNS_RDATACLASS_FORMATSIZE];
  306. int level = ISC_LOG_ERROR;
  307. const char *msg = "denied";
  308. isc_result_t result;
  309. if (slave && acl == NULL) {
  310. result = DNS_R_NOTIMP;
  311. level = ISC_LOG_DEBUG(3);
  312. msg = "disabled";
  313. } else {
  314. result = ns_client_checkaclsilent(client, NULL, acl, ISC_FALSE);
  315. if (result == ISC_R_SUCCESS) {
  316. level = ISC_LOG_DEBUG(3);
  317. msg = "approved";
  318. } else if (acl == NULL && !has_ssutable) {
  319. level = ISC_LOG_INFO;
  320. }
  321. }
  322. if (client->signer != NULL) {
  323. dns_name_format(client->signer, namebuf, sizeof(namebuf));
  324. ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
  325. NS_LOGMODULE_UPDATE, ISC_LOG_INFO,
  326. "signer \"%s\" %s", namebuf, msg);
  327. }
  328. dns_name_format(zonename, namebuf, sizeof(namebuf));
  329. dns_rdataclass_format(client->view->rdclass, classbuf,
  330. sizeof(classbuf));
  331. ns_client_log(client, NS_LOGCATEGORY_UPDATE_SECURITY,
  332. NS_LOGMODULE_UPDATE, level, "%s '%s/%s' %s",
  333. message, namebuf, classbuf, msg);
  334. return (result);
  335. }
  336. /*%
  337. * Update a single RR in version 'ver' of 'db' and log the
  338. * update in 'diff'.
  339. *
  340. * Ensures:
  341. * \li '*tuple' == NULL. Either the tuple is freed, or its
  342. * ownership has been transferred to the diff.
  343. */
  344. static isc_result_t
  345. do_one_tuple(dns_difftuple_t **tuple, dns_db_t *db, dns_dbversion_t *ver,
  346. dns_diff_t *diff)
  347. {
  348. dns_diff_t temp_diff;
  349. isc_result_t result;
  350. /*
  351. * Create a singleton diff.
  352. */
  353. dns_diff_init(diff->mctx, &temp_diff);
  354. temp_diff.resign = diff->resign;
  355. ISC_LIST_APPEND(temp_diff.tuples, *tuple, link);
  356. /*
  357. * Apply it to the database.
  358. */
  359. result = dns_diff_apply(&temp_diff, db, ver);
  360. ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link);
  361. if (result != ISC_R_SUCCESS) {
  362. dns_difftuple_free(tuple);
  363. return (result);
  364. }
  365. /*
  366. * Merge it into the current pending journal entry.
  367. */
  368. dns_diff_appendminimal(diff, tuple);
  369. /*
  370. * Do not clear temp_diff.
  371. */
  372. return (ISC_R_SUCCESS);
  373. }
  374. /*%
  375. * Perform the updates in 'updates' in version 'ver' of 'db' and log the
  376. * update in 'diff'.
  377. *
  378. * Ensures:
  379. * \li 'updates' is empty.
  380. */
  381. static isc_result_t
  382. do_diff(dns_diff_t *updates, dns_db_t *db, dns_dbversion_t *ver,
  383. dns_diff_t *diff)
  384. {
  385. isc_result_t result;
  386. while (! ISC_LIST_EMPTY(updates->tuples)) {
  387. dns_difftuple_t *t = ISC_LIST_HEAD(updates->tuples);
  388. ISC_LIST_UNLINK(updates->tuples, t, link);
  389. CHECK(do_one_tuple(&t, db, ver, diff));
  390. }
  391. return (ISC_R_SUCCESS);
  392. failure:
  393. dns_diff_clear(diff);
  394. return (result);
  395. }
  396. static isc_result_t
  397. update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
  398. dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
  399. dns_rdata_t *rdata)
  400. {
  401. dns_difftuple_t *tuple = NULL;
  402. isc_result_t result;
  403. result = dns_difftuple_create(diff->mctx, op,
  404. name, ttl, rdata, &tuple);
  405. if (result != ISC_R_SUCCESS)
  406. return (result);
  407. return (do_one_tuple(&tuple, db, ver, diff));
  408. }
  409. /**************************************************************************/
  410. /*
  411. * Callback-style iteration over rdatasets and rdatas.
  412. *
  413. * foreach_rrset() can be used to iterate over the RRsets
  414. * of a name and call a callback function with each
  415. * one. Similarly, foreach_rr() can be used to iterate
  416. * over the individual RRs at name, optionally restricted
  417. * to RRs of a given type.
  418. *
  419. * The callback functions are called "actions" and take
  420. * two arguments: a void pointer for passing arbitrary
  421. * context information, and a pointer to the current RRset
  422. * or RR. By convention, their names end in "_action".
  423. */
  424. /*
  425. * XXXRTH We might want to make this public somewhere in libdns.
  426. */
  427. /*%
  428. * Function type for foreach_rrset() iterator actions.
  429. */
  430. typedef isc_result_t rrset_func(void *data, dns_rdataset_t *rrset);
  431. /*%
  432. * Function type for foreach_rr() iterator actions.
  433. */
  434. typedef isc_result_t rr_func(void *data, rr_t *rr);
  435. /*%
  436. * Internal context struct for foreach_node_rr().
  437. */
  438. typedef struct {
  439. rr_func * rr_action;
  440. void * rr_action_data;
  441. } foreach_node_rr_ctx_t;
  442. /*%
  443. * Internal helper function for foreach_node_rr().
  444. */
  445. static isc_result_t
  446. foreach_node_rr_action(void *data, dns_rdataset_t *rdataset) {
  447. isc_result_t result;
  448. foreach_node_rr_ctx_t *ctx = data;
  449. for (result = dns_rdataset_first(rdataset);
  450. result == ISC_R_SUCCESS;
  451. result = dns_rdataset_next(rdataset))
  452. {
  453. rr_t rr = { 0, DNS_RDATA_INIT };
  454. dns_rdataset_current(rdataset, &rr.rdata);
  455. rr.ttl = rdataset->ttl;
  456. result = (*ctx->rr_action)(ctx->rr_action_data, &rr);
  457. if (result != ISC_R_SUCCESS)
  458. return (result);
  459. }
  460. if (result != ISC_R_NOMORE)
  461. return (result);
  462. return (ISC_R_SUCCESS);
  463. }
  464. /*%
  465. * For each rdataset of 'name' in 'ver' of 'db', call 'action'
  466. * with the rdataset and 'action_data' as arguments. If the name
  467. * does not exist, do nothing.
  468. *
  469. * If 'action' returns an error, abort iteration and return the error.
  470. */
  471. static isc_result_t
  472. foreach_rrset(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  473. rrset_func *action, void *action_data)
  474. {
  475. isc_result_t result;
  476. dns_dbnode_t *node;
  477. dns_rdatasetiter_t *iter;
  478. node = NULL;
  479. result = dns_db_findnode(db, name, ISC_FALSE, &node);
  480. if (result == ISC_R_NOTFOUND)
  481. return (ISC_R_SUCCESS);
  482. if (result != ISC_R_SUCCESS)
  483. return (result);
  484. iter = NULL;
  485. result = dns_db_allrdatasets(db, node, ver,
  486. (isc_stdtime_t) 0, &iter);
  487. if (result != ISC_R_SUCCESS)
  488. goto cleanup_node;
  489. for (result = dns_rdatasetiter_first(iter);
  490. result == ISC_R_SUCCESS;
  491. result = dns_rdatasetiter_next(iter))
  492. {
  493. dns_rdataset_t rdataset;
  494. dns_rdataset_init(&rdataset);
  495. dns_rdatasetiter_current(iter, &rdataset);
  496. result = (*action)(action_data, &rdataset);
  497. dns_rdataset_disassociate(&rdataset);
  498. if (result != ISC_R_SUCCESS)
  499. goto cleanup_iterator;
  500. }
  501. if (result == ISC_R_NOMORE)
  502. result = ISC_R_SUCCESS;
  503. cleanup_iterator:
  504. dns_rdatasetiter_destroy(&iter);
  505. cleanup_node:
  506. dns_db_detachnode(db, &node);
  507. return (result);
  508. }
  509. /*%
  510. * For each RR of 'name' in 'ver' of 'db', call 'action'
  511. * with the RR and 'action_data' as arguments. If the name
  512. * does not exist, do nothing.
  513. *
  514. * If 'action' returns an error, abort iteration
  515. * and return the error.
  516. */
  517. static isc_result_t
  518. foreach_node_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  519. rr_func *rr_action, void *rr_action_data)
  520. {
  521. foreach_node_rr_ctx_t ctx;
  522. ctx.rr_action = rr_action;
  523. ctx.rr_action_data = rr_action_data;
  524. return (foreach_rrset(db, ver, name,
  525. foreach_node_rr_action, &ctx));
  526. }
  527. /*%
  528. * For each of the RRs specified by 'db', 'ver', 'name', 'type',
  529. * (which can be dns_rdatatype_any to match any type), and 'covers', call
  530. * 'action' with the RR and 'action_data' as arguments. If the name
  531. * does not exist, or if no RRset of the given type exists at the name,
  532. * do nothing.
  533. *
  534. * If 'action' returns an error, abort iteration and return the error.
  535. */
  536. static isc_result_t
  537. foreach_rr(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  538. dns_rdatatype_t type, dns_rdatatype_t covers, rr_func *rr_action,
  539. void *rr_action_data)
  540. {
  541. isc_result_t result;
  542. dns_dbnode_t *node;
  543. dns_rdataset_t rdataset;
  544. if (type == dns_rdatatype_any)
  545. return (foreach_node_rr(db, ver, name,
  546. rr_action, rr_action_data));
  547. node = NULL;
  548. if (type == dns_rdatatype_nsec3 ||
  549. (type == dns_rdatatype_rrsig && covers == dns_rdatatype_nsec3))
  550. result = dns_db_findnsec3node(db, name, ISC_FALSE, &node);
  551. else
  552. result = dns_db_findnode(db, name, ISC_FALSE, &node);
  553. if (result == ISC_R_NOTFOUND)
  554. return (ISC_R_SUCCESS);
  555. if (result != ISC_R_SUCCESS)
  556. return (result);
  557. dns_rdataset_init(&rdataset);
  558. result = dns_db_findrdataset(db, node, ver, type, covers,
  559. (isc_stdtime_t) 0, &rdataset, NULL);
  560. if (result == ISC_R_NOTFOUND) {
  561. result = ISC_R_SUCCESS;
  562. goto cleanup_node;
  563. }
  564. if (result != ISC_R_SUCCESS)
  565. goto cleanup_node;
  566. for (result = dns_rdataset_first(&rdataset);
  567. result == ISC_R_SUCCESS;
  568. result = dns_rdataset_next(&rdataset))
  569. {
  570. rr_t rr = { 0, DNS_RDATA_INIT };
  571. dns_rdataset_current(&rdataset, &rr.rdata);
  572. rr.ttl = rdataset.ttl;
  573. result = (*rr_action)(rr_action_data, &rr);
  574. if (result != ISC_R_SUCCESS)
  575. goto cleanup_rdataset;
  576. }
  577. if (result != ISC_R_NOMORE)
  578. goto cleanup_rdataset;
  579. result = ISC_R_SUCCESS;
  580. cleanup_rdataset:
  581. dns_rdataset_disassociate(&rdataset);
  582. cleanup_node:
  583. dns_db_detachnode(db, &node);
  584. return (result);
  585. }
  586. /**************************************************************************/
  587. /*
  588. * Various tests on the database contents (for prerequisites, etc).
  589. */
  590. /*%
  591. * Function type for predicate functions that compare a database RR 'db_rr'
  592. * against an update RR 'update_rr'.
  593. */
  594. typedef isc_boolean_t rr_predicate(dns_rdata_t *update_rr, dns_rdata_t *db_rr);
  595. /*%
  596. * Helper function for rrset_exists().
  597. */
  598. static isc_result_t
  599. rrset_exists_action(void *data, rr_t *rr) {
  600. UNUSED(data);
  601. UNUSED(rr);
  602. return (ISC_R_EXISTS);
  603. }
  604. /*%
  605. * Utility macro for RR existence checking functions.
  606. *
  607. * If the variable 'result' has the value ISC_R_EXISTS or
  608. * ISC_R_SUCCESS, set *exists to ISC_TRUE or ISC_FALSE,
  609. * respectively, and return success.
  610. *
  611. * If 'result' has any other value, there was a failure.
  612. * Return the failure result code and do not set *exists.
  613. *
  614. * This would be more readable as "do { if ... } while(0)",
  615. * but that form generates tons of warnings on Solaris 2.6.
  616. */
  617. #define RETURN_EXISTENCE_FLAG \
  618. return ((result == ISC_R_EXISTS) ? \
  619. (*exists = ISC_TRUE, ISC_R_SUCCESS) : \
  620. ((result == ISC_R_SUCCESS) ? \
  621. (*exists = ISC_FALSE, ISC_R_SUCCESS) : \
  622. result))
  623. /*%
  624. * Set '*exists' to true iff an rrset of the given type exists,
  625. * to false otherwise.
  626. */
  627. static isc_result_t
  628. rrset_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  629. dns_rdatatype_t type, dns_rdatatype_t covers,
  630. isc_boolean_t *exists)
  631. {
  632. isc_result_t result;
  633. result = foreach_rr(db, ver, name, type, covers,
  634. rrset_exists_action, NULL);
  635. RETURN_EXISTENCE_FLAG;
  636. }
  637. /*%
  638. * Set '*visible' to true if the RRset exists and is part of the
  639. * visible zone. Otherwise '*visible' is set to false unless a
  640. * error occurs.
  641. */
  642. static isc_result_t
  643. rrset_visible(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  644. dns_rdatatype_t type, isc_boolean_t *visible)
  645. {
  646. isc_result_t result;
  647. dns_fixedname_t fixed;
  648. dns_fixedname_init(&fixed);
  649. result = dns_db_find(db, name, ver, type, DNS_DBFIND_NOWILD,
  650. (isc_stdtime_t) 0, NULL,
  651. dns_fixedname_name(&fixed), NULL, NULL);
  652. switch (result) {
  653. case ISC_R_SUCCESS:
  654. *visible = ISC_TRUE;
  655. break;
  656. /*
  657. * Glue, obscured, deleted or replaced records.
  658. */
  659. case DNS_R_DELEGATION:
  660. case DNS_R_DNAME:
  661. case DNS_R_CNAME:
  662. case DNS_R_NXDOMAIN:
  663. case DNS_R_NXRRSET:
  664. case DNS_R_EMPTYNAME:
  665. case DNS_R_COVERINGNSEC:
  666. *visible = ISC_FALSE;
  667. result = ISC_R_SUCCESS;
  668. break;
  669. default:
  670. break;
  671. }
  672. return (result);
  673. }
  674. /*%
  675. * Helper function for cname_incompatible_rrset_exists.
  676. */
  677. static isc_result_t
  678. cname_compatibility_action(void *data, dns_rdataset_t *rrset) {
  679. UNUSED(data);
  680. if (rrset->type != dns_rdatatype_cname &&
  681. ! dns_rdatatype_isdnssec(rrset->type))
  682. return (ISC_R_EXISTS);
  683. return (ISC_R_SUCCESS);
  684. }
  685. /*%
  686. * Check whether there is an rrset incompatible with adding a CNAME RR,
  687. * i.e., anything but another CNAME (which can be replaced) or a
  688. * DNSSEC RR (which can coexist).
  689. *
  690. * If such an incompatible rrset exists, set '*exists' to ISC_TRUE.
  691. * Otherwise, set it to ISC_FALSE.
  692. */
  693. static isc_result_t
  694. cname_incompatible_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
  695. dns_name_t *name, isc_boolean_t *exists) {
  696. isc_result_t result;
  697. result = foreach_rrset(db, ver, name,
  698. cname_compatibility_action, NULL);
  699. RETURN_EXISTENCE_FLAG;
  700. }
  701. /*%
  702. * Helper function for rr_count().
  703. */
  704. static isc_result_t
  705. count_rr_action(void *data, rr_t *rr) {
  706. int *countp = data;
  707. UNUSED(rr);
  708. (*countp)++;
  709. return (ISC_R_SUCCESS);
  710. }
  711. /*%
  712. * Count the number of RRs of 'type' belonging to 'name' in 'ver' of 'db'.
  713. */
  714. static isc_result_t
  715. rr_count(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  716. dns_rdatatype_t type, dns_rdatatype_t covers, int *countp)
  717. {
  718. *countp = 0;
  719. return (foreach_rr(db, ver, name, type, covers,
  720. count_rr_action, countp));
  721. }
  722. /*%
  723. * Context struct and helper function for name_exists().
  724. */
  725. static isc_result_t
  726. name_exists_action(void *data, dns_rdataset_t *rrset) {
  727. UNUSED(data);
  728. UNUSED(rrset);
  729. return (ISC_R_EXISTS);
  730. }
  731. /*%
  732. * Set '*exists' to true iff the given name exists, to false otherwise.
  733. */
  734. static isc_result_t
  735. name_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  736. isc_boolean_t *exists)
  737. {
  738. isc_result_t result;
  739. result = foreach_rrset(db, ver, name,
  740. name_exists_action, NULL);
  741. RETURN_EXISTENCE_FLAG;
  742. }
  743. /*
  744. * 'ssu_check_t' is used to pass the arguments to
  745. * dns_ssutable_checkrules() to the callback function
  746. * ssu_checkrule().
  747. */
  748. typedef struct {
  749. /* The ownername of the record to be updated. */
  750. dns_name_t *name;
  751. /* The signature's name if the request was signed. */
  752. dns_name_t *signer;
  753. /* The address of the client if the request was received via TCP. */
  754. isc_netaddr_t *tcpaddr;
  755. /* The ssu table to check against. */
  756. dns_ssutable_t *table;
  757. /* the key used for TKEY requests */
  758. dst_key_t *key;
  759. } ssu_check_t;
  760. static isc_result_t
  761. ssu_checkrule(void *data, dns_rdataset_t *rrset) {
  762. ssu_check_t *ssuinfo = data;
  763. isc_boolean_t result;
  764. /*
  765. * If we're deleting all records, it's ok to delete RRSIG and NSEC even
  766. * if we're normally not allowed to.
  767. */
  768. if (rrset->type == dns_rdatatype_rrsig ||
  769. rrset->type == dns_rdatatype_nsec)
  770. return (ISC_R_SUCCESS);
  771. result = dns_ssutable_checkrules(ssuinfo->table, ssuinfo->signer,
  772. ssuinfo->name, ssuinfo->tcpaddr,
  773. rrset->type, ssuinfo->key);
  774. return (result == ISC_TRUE ? ISC_R_SUCCESS : ISC_R_FAILURE);
  775. }
  776. static isc_boolean_t
  777. ssu_checkall(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  778. dns_ssutable_t *ssutable, dns_name_t *signer,
  779. isc_netaddr_t *tcpaddr, dst_key_t *key)
  780. {
  781. isc_result_t result;
  782. ssu_check_t ssuinfo;
  783. ssuinfo.name = name;
  784. ssuinfo.table = ssutable;
  785. ssuinfo.signer = signer;
  786. ssuinfo.tcpaddr = tcpaddr;
  787. ssuinfo.key = key;
  788. result = foreach_rrset(db, ver, name, ssu_checkrule, &ssuinfo);
  789. return (ISC_TF(result == ISC_R_SUCCESS));
  790. }
  791. /**************************************************************************/
  792. /*
  793. * Checking of "RRset exists (value dependent)" prerequisites.
  794. *
  795. * In the RFC2136 section 3.2.5, this is the pseudocode involving
  796. * a variable called "temp", a mapping of <name, type> tuples to rrsets.
  797. *
  798. * Here, we represent the "temp" data structure as (non-minimal) "dns_diff_t"
  799. * where each tuple has op==DNS_DIFFOP_EXISTS.
  800. */
  801. /*%
  802. * Append a tuple asserting the existence of the RR with
  803. * 'name' and 'rdata' to 'diff'.
  804. */
  805. static isc_result_t
  806. temp_append(dns_diff_t *diff, dns_name_t *name, dns_rdata_t *rdata) {
  807. isc_result_t result;
  808. dns_difftuple_t *tuple = NULL;
  809. REQUIRE(DNS_DIFF_VALID(diff));
  810. CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_EXISTS,
  811. name, 0, rdata, &tuple));
  812. ISC_LIST_APPEND(diff->tuples, tuple, link);
  813. failure:
  814. return (result);
  815. }
  816. /*%
  817. * Compare two rdatasets represented as sorted lists of tuples.
  818. * All list elements must have the same owner name and type.
  819. * Return ISC_R_SUCCESS if the rdatasets are equal, rcode(dns_rcode_nxrrset)
  820. * if not.
  821. */
  822. static isc_result_t
  823. temp_check_rrset(dns_difftuple_t *a, dns_difftuple_t *b) {
  824. for (;;) {
  825. if (a == NULL || b == NULL)
  826. break;
  827. INSIST(a->op == DNS_DIFFOP_EXISTS &&
  828. b->op == DNS_DIFFOP_EXISTS);
  829. INSIST(a->rdata.type == b->rdata.type);
  830. INSIST(dns_name_equal(&a->name, &b->name));
  831. if (dns_rdata_casecompare(&a->rdata, &b->rdata) != 0)
  832. return (DNS_R_NXRRSET);
  833. a = ISC_LIST_NEXT(a, link);
  834. b = ISC_LIST_NEXT(b, link);
  835. }
  836. if (a != NULL || b != NULL)
  837. return (DNS_R_NXRRSET);
  838. return (ISC_R_SUCCESS);
  839. }
  840. /*%
  841. * A comparison function defining the sorting order for the entries
  842. * in the "temp" data structure. The major sort key is the owner name,
  843. * followed by the type and rdata.
  844. */
  845. static int
  846. temp_order(const void *av, const void *bv) {
  847. dns_difftuple_t const * const *ap = av;
  848. dns_difftuple_t const * const *bp = bv;
  849. dns_difftuple_t const *a = *ap;
  850. dns_difftuple_t const *b = *bp;
  851. int r;
  852. r = dns_name_compare(&a->name, &b->name);
  853. if (r != 0)
  854. return (r);
  855. r = (b->rdata.type - a->rdata.type);
  856. if (r != 0)
  857. return (r);
  858. r = dns_rdata_casecompare(&a->rdata, &b->rdata);
  859. return (r);
  860. }
  861. /*%
  862. * Check the "RRset exists (value dependent)" prerequisite information
  863. * in 'temp' against the contents of the database 'db'.
  864. *
  865. * Return ISC_R_SUCCESS if the prerequisites are satisfied,
  866. * rcode(dns_rcode_nxrrset) if not.
  867. *
  868. * 'temp' must be pre-sorted.
  869. */
  870. static isc_result_t
  871. temp_check(isc_mem_t *mctx, dns_diff_t *temp, dns_db_t *db,
  872. dns_dbversion_t *ver, dns_name_t *tmpname, dns_rdatatype_t *typep)
  873. {
  874. isc_result_t result;
  875. dns_name_t *name;
  876. dns_dbnode_t *node;
  877. dns_difftuple_t *t;
  878. dns_diff_t trash;
  879. dns_diff_init(mctx, &trash);
  880. /*
  881. * For each name and type in the prerequisites,
  882. * construct a sorted rdata list of the corresponding
  883. * database contents, and compare the lists.
  884. */
  885. t = ISC_LIST_HEAD(temp->tuples);
  886. while (t != NULL) {
  887. name = &t->name;
  888. (void)dns_name_copy(name, tmpname, NULL);
  889. *typep = t->rdata.type;
  890. /* A new unique name begins here. */
  891. node = NULL;
  892. result = dns_db_findnode(db, name, ISC_FALSE, &node);
  893. if (result == ISC_R_NOTFOUND) {
  894. dns_diff_clear(&trash);
  895. return (DNS_R_NXRRSET);
  896. }
  897. if (result != ISC_R_SUCCESS) {
  898. dns_diff_clear(&trash);
  899. return (result);
  900. }
  901. /* A new unique type begins here. */
  902. while (t != NULL && dns_name_equal(&t->name, name)) {
  903. dns_rdatatype_t type, covers;
  904. dns_rdataset_t rdataset;
  905. dns_diff_t d_rrs; /* Database RRs with
  906. this name and type */
  907. dns_diff_t u_rrs; /* Update RRs with
  908. this name and type */
  909. *typep = type = t->rdata.type;
  910. if (type == dns_rdatatype_rrsig ||
  911. type == dns_rdatatype_sig)
  912. covers = dns_rdata_covers(&t->rdata);
  913. else if (type == dns_rdatatype_any) {
  914. dns_db_detachnode(db, &node);
  915. dns_diff_clear(&trash);
  916. return (DNS_R_NXRRSET);
  917. } else
  918. covers = 0;
  919. /*
  920. * Collect all database RRs for this name and type
  921. * onto d_rrs and sort them.
  922. */
  923. dns_rdataset_init(&rdataset);
  924. result = dns_db_findrdataset(db, node, ver, type,
  925. covers, (isc_stdtime_t) 0,
  926. &rdataset, NULL);
  927. if (result != ISC_R_SUCCESS) {
  928. dns_db_detachnode(db, &node);
  929. dns_diff_clear(&trash);
  930. return (DNS_R_NXRRSET);
  931. }
  932. dns_diff_init(mctx, &d_rrs);
  933. dns_diff_init(mctx, &u_rrs);
  934. for (result = dns_rdataset_first(&rdataset);
  935. result == ISC_R_SUCCESS;
  936. result = dns_rdataset_next(&rdataset))
  937. {
  938. dns_rdata_t rdata = DNS_RDATA_INIT;
  939. dns_rdataset_current(&rdataset, &rdata);
  940. result = temp_append(&d_rrs, name, &rdata);
  941. if (result != ISC_R_SUCCESS)
  942. goto failure;
  943. }
  944. if (result != ISC_R_NOMORE)
  945. goto failure;
  946. result = dns_diff_sort(&d_rrs, temp_order);
  947. if (result != ISC_R_SUCCESS)
  948. goto failure;
  949. /*
  950. * Collect all update RRs for this name and type
  951. * onto u_rrs. No need to sort them here -
  952. * they are already sorted.
  953. */
  954. while (t != NULL &&
  955. dns_name_equal(&t->name, name) &&
  956. t->rdata.type == type)
  957. {
  958. dns_difftuple_t *next =
  959. ISC_LIST_NEXT(t, link);
  960. ISC_LIST_UNLINK(temp->tuples, t, link);
  961. ISC_LIST_APPEND(u_rrs.tuples, t, link);
  962. t = next;
  963. }
  964. /* Compare the two sorted lists. */
  965. result = temp_check_rrset(ISC_LIST_HEAD(u_rrs.tuples),
  966. ISC_LIST_HEAD(d_rrs.tuples));
  967. if (result != ISC_R_SUCCESS)
  968. goto failure;
  969. /*
  970. * We are done with the tuples, but we can't free
  971. * them yet because "name" still points into one
  972. * of them. Move them on a temporary list.
  973. */
  974. ISC_LIST_APPENDLIST(trash.tuples, u_rrs.tuples, link);
  975. ISC_LIST_APPENDLIST(trash.tuples, d_rrs.tuples, link);
  976. dns_rdataset_disassociate(&rdataset);
  977. continue;
  978. failure:
  979. dns_diff_clear(&d_rrs);
  980. dns_diff_clear(&u_rrs);
  981. dns_diff_clear(&trash);
  982. dns_rdataset_disassociate(&rdataset);
  983. dns_db_detachnode(db, &node);
  984. return (result);
  985. }
  986. dns_db_detachnode(db, &node);
  987. }
  988. dns_diff_clear(&trash);
  989. return (ISC_R_SUCCESS);
  990. }
  991. /**************************************************************************/
  992. /*
  993. * Conditional deletion of RRs.
  994. */
  995. /*%
  996. * Context structure for delete_if().
  997. */
  998. typedef struct {
  999. rr_predicate *predicate;
  1000. dns_db_t *db;
  1001. dns_dbversion_t *ver;
  1002. dns_diff_t *diff;
  1003. dns_name_t *name;
  1004. dns_rdata_t *update_rr;
  1005. } conditional_delete_ctx_t;
  1006. /*%
  1007. * Predicate functions for delete_if().
  1008. */
  1009. /*%
  1010. * Return true iff 'db_rr' is neither a SOA nor an NS RR nor
  1011. * an RRSIG nor an NSEC3PARAM nor a NSEC.
  1012. */
  1013. static isc_boolean_t
  1014. type_not_soa_nor_ns_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
  1015. UNUSED(update_rr);
  1016. return ((db_rr->type != dns_rdatatype_soa &&
  1017. db_rr->type != dns_rdatatype_ns &&
  1018. db_rr->type != dns_rdatatype_nsec3param &&
  1019. db_rr->type != dns_rdatatype_rrsig &&
  1020. db_rr->type != dns_rdatatype_nsec) ?
  1021. ISC_TRUE : ISC_FALSE);
  1022. }
  1023. /*%
  1024. * Return true iff 'db_rr' is neither a RRSIG nor a NSEC.
  1025. */
  1026. static isc_boolean_t
  1027. type_not_dnssec(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
  1028. UNUSED(update_rr);
  1029. return ((db_rr->type != dns_rdatatype_rrsig &&
  1030. db_rr->type != dns_rdatatype_nsec) ?
  1031. ISC_TRUE : ISC_FALSE);
  1032. }
  1033. /*%
  1034. * Return true always.
  1035. */
  1036. static isc_boolean_t
  1037. true_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
  1038. UNUSED(update_rr);
  1039. UNUSED(db_rr);
  1040. return (ISC_TRUE);
  1041. }
  1042. /*%
  1043. * Return true if the record is a RRSIG.
  1044. */
  1045. static isc_boolean_t
  1046. rrsig_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
  1047. UNUSED(update_rr);
  1048. return ((db_rr->type == dns_rdatatype_rrsig) ?
  1049. ISC_TRUE : ISC_FALSE);
  1050. }
  1051. /*%
  1052. * Return true iff the two RRs have identical rdata.
  1053. */
  1054. static isc_boolean_t
  1055. rr_equal_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
  1056. /*
  1057. * XXXRTH This is not a problem, but we should consider creating
  1058. * dns_rdata_equal() (that used dns_name_equal()), since it
  1059. * would be faster. Not a priority.
  1060. */
  1061. return (dns_rdata_casecompare(update_rr, db_rr) == 0 ?
  1062. ISC_TRUE : ISC_FALSE);
  1063. }
  1064. /*%
  1065. * Return true iff 'update_rr' should replace 'db_rr' according
  1066. * to the special RFC2136 rules for CNAME, SOA, and WKS records.
  1067. *
  1068. * RFC2136 does not mention NSEC or DNAME, but multiple NSECs or DNAMEs
  1069. * make little sense, so we replace those, too.
  1070. *
  1071. * Additionally replace RRSIG that have been generated by the same key
  1072. * for the same type. This simplifies refreshing a offline KSK by not
  1073. * requiring that the old RRSIG be deleted. It also simplifies key
  1074. * rollover by only requiring that the new RRSIG be added.
  1075. */
  1076. static isc_boolean_t
  1077. replaces_p(dns_rdata_t *update_rr, dns_rdata_t *db_rr) {
  1078. dns_rdata_rrsig_t updatesig, dbsig;
  1079. isc_result_t result;
  1080. if (db_rr->type != update_rr->type)
  1081. return (ISC_FALSE);
  1082. if (db_rr->type == dns_rdatatype_cname)
  1083. return (ISC_TRUE);
  1084. if (db_rr->type == dns_rdatatype_dname)
  1085. return (ISC_TRUE);
  1086. if (db_rr->type == dns_rdatatype_soa)
  1087. return (ISC_TRUE);
  1088. if (db_rr->type == dns_rdatatype_nsec)
  1089. return (ISC_TRUE);
  1090. if (db_rr->type == dns_rdatatype_rrsig) {
  1091. /*
  1092. * Replace existing RRSIG with the same keyid,
  1093. * covered and algorithm.
  1094. */
  1095. result = dns_rdata_tostruct(db_rr, &dbsig, NULL);
  1096. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  1097. result = dns_rdata_tostruct(update_rr, &updatesig, NULL);
  1098. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  1099. if (dbsig.keyid == updatesig.keyid &&
  1100. dbsig.covered == updatesig.covered &&
  1101. dbsig.algorithm == updatesig.algorithm)
  1102. return (ISC_TRUE);
  1103. }
  1104. if (db_rr->type == dns_rdatatype_wks) {
  1105. /*
  1106. * Compare the address and protocol fields only. These
  1107. * form the first five bytes of the RR data. Do a
  1108. * raw binary comparison; unpacking the WKS RRs using
  1109. * dns_rdata_tostruct() might be cleaner in some ways.
  1110. */
  1111. INSIST(db_rr->length >= 5 && update_rr->length >= 5);
  1112. return (memcmp(db_rr->data, update_rr->data, 5) == 0 ?
  1113. ISC_TRUE : ISC_FALSE);
  1114. }
  1115. if (db_rr->type == dns_rdatatype_nsec3param) {
  1116. if (db_rr->length != update_rr->length)
  1117. return (ISC_FALSE);
  1118. INSIST(db_rr->length >= 4 && update_rr->length >= 4);
  1119. /*
  1120. * Replace NSEC3PARAM records that only differ by the
  1121. * flags field.
  1122. */
  1123. if (db_rr->data[0] == update_rr->data[0] &&
  1124. memcmp(db_rr->data+2, update_rr->data+2,
  1125. update_rr->length - 2) == 0)
  1126. return (ISC_TRUE);
  1127. }
  1128. return (ISC_FALSE);
  1129. }
  1130. /*%
  1131. * Internal helper function for delete_if().
  1132. */
  1133. static isc_result_t
  1134. delete_if_action(void *data, rr_t *rr) {
  1135. conditional_delete_ctx_t *ctx = data;
  1136. if ((*ctx->predicate)(ctx->update_rr, &rr->rdata)) {
  1137. isc_result_t result;
  1138. result = update_one_rr(ctx->db, ctx->ver, ctx->diff,
  1139. DNS_DIFFOP_DEL, ctx->name,
  1140. rr->ttl, &rr->rdata);
  1141. return (result);
  1142. } else {
  1143. return (ISC_R_SUCCESS);
  1144. }
  1145. }
  1146. /*%
  1147. * Conditionally delete RRs. Apply 'predicate' to the RRs
  1148. * specified by 'db', 'ver', 'name', and 'type' (which can
  1149. * be dns_rdatatype_any to match any type). Delete those
  1150. * RRs for which the predicate returns true, and log the
  1151. * deletions in 'diff'.
  1152. */
  1153. static isc_result_t
  1154. delete_if(rr_predicate *predicate, dns_db_t *db, dns_dbversion_t *ver,
  1155. dns_name_t *name, dns_rdatatype_t type, dns_rdatatype_t covers,
  1156. dns_rdata_t *update_rr, dns_diff_t *diff)
  1157. {
  1158. conditional_delete_ctx_t ctx;
  1159. ctx.predicate = predicate;
  1160. ctx.db = db;
  1161. ctx.ver = ver;
  1162. ctx.diff = diff;
  1163. ctx.name = name;
  1164. ctx.update_rr = update_rr;
  1165. return (foreach_rr(db, ver, name, type, covers,
  1166. delete_if_action, &ctx));
  1167. }
  1168. /**************************************************************************/
  1169. /*%
  1170. * Prepare an RR for the addition of the new RR 'ctx->update_rr',
  1171. * with TTL 'ctx->update_rr_ttl', to its rdataset, by deleting
  1172. * the RRs if it is replaced by the new RR or has a conflicting TTL.
  1173. * The necessary changes are appended to ctx->del_diff and ctx->add_diff;
  1174. * we need to do all deletions before any additions so that we don't run
  1175. * into transient states with conflicting TTLs.
  1176. */
  1177. typedef struct {
  1178. dns_db_t *db;
  1179. dns_dbversion_t *ver;
  1180. dns_diff_t *diff;
  1181. dns_name_t *name;
  1182. dns_rdata_t *update_rr;
  1183. dns_ttl_t update_rr_ttl;
  1184. isc_boolean_t ignore_add;
  1185. dns_diff_t del_diff;
  1186. dns_diff_t add_diff;
  1187. } add_rr_prepare_ctx_t;
  1188. static isc_result_t
  1189. add_rr_prepare_action(void *data, rr_t *rr) {
  1190. isc_result_t result = ISC_R_SUCCESS;
  1191. add_rr_prepare_ctx_t *ctx = data;
  1192. dns_difftuple_t *tuple = NULL;
  1193. isc_boolean_t equal;
  1194. /*
  1195. * If the update RR is a "duplicate" of the update RR,
  1196. * the update should be silently ignored.
  1197. */
  1198. equal = ISC_TF(dns_rdata_casecompare(&rr->rdata, ctx->update_rr) == 0);
  1199. if (equal && rr->ttl == ctx->update_rr_ttl) {
  1200. ctx->ignore_add = ISC_TRUE;
  1201. return (ISC_R_SUCCESS);
  1202. }
  1203. /*
  1204. * If this RR is "equal" to the update RR, it should
  1205. * be deleted before the update RR is added.
  1206. */
  1207. if (replaces_p(ctx->update_rr, &rr->rdata)) {
  1208. CHECK(dns_difftuple_create(ctx->del_diff.mctx, DNS_DIFFOP_DEL,
  1209. ctx->name, rr->ttl, &rr->rdata,
  1210. &tuple));
  1211. dns_diff_append(&ctx->del_diff, &tuple);
  1212. return (ISC_R_SUCCESS);
  1213. }
  1214. /*
  1215. * If this RR differs in TTL from the update RR,
  1216. * its TTL must be adjusted.
  1217. */
  1218. if (rr->ttl != ctx->update_rr_ttl) {
  1219. CHECK(dns_difftuple_create(ctx->del_diff.mctx, DNS_DIFFOP_DEL,
  1220. ctx->name, rr->ttl, &rr->rdata,
  1221. &tuple));
  1222. dns_diff_append(&ctx->del_diff, &tuple);
  1223. if (!equal) {
  1224. CHECK(dns_difftuple_create(ctx->add_diff.mctx,
  1225. DNS_DIFFOP_ADD, ctx->name,
  1226. ctx->update_rr_ttl,
  1227. &rr->rdata, &tuple));
  1228. dns_diff_append(&ctx->add_diff, &tuple);
  1229. }
  1230. }
  1231. failure:
  1232. return (result);
  1233. }
  1234. /**************************************************************************/
  1235. /*
  1236. * Miscellaneous subroutines.
  1237. */
  1238. /*%
  1239. * Extract a single update RR from 'section' of dynamic update message
  1240. * 'msg', with consistency checking.
  1241. *
  1242. * Stores the owner name, rdata, and TTL of the update RR at 'name',
  1243. * 'rdata', and 'ttl', respectively.
  1244. */
  1245. static void
  1246. get_current_rr(dns_message_t *msg, dns_section_t section,
  1247. dns_rdataclass_t zoneclass, dns_name_t **name,
  1248. dns_rdata_t *rdata, dns_rdatatype_t *covers,
  1249. dns_ttl_t *ttl, dns_rdataclass_t *update_class)
  1250. {
  1251. dns_rdataset_t *rdataset;
  1252. isc_result_t result;
  1253. dns_message_currentname(msg, section, name);
  1254. rdataset = ISC_LIST_HEAD((*name)->list);
  1255. INSIST(rdataset != NULL);
  1256. INSIST(ISC_LIST_NEXT(rdataset, link) == NULL);
  1257. *covers = rdataset->covers;
  1258. *ttl = rdataset->ttl;
  1259. result = dns_rdataset_first(rdataset);
  1260. INSIST(result == ISC_R_SUCCESS);
  1261. dns_rdataset_current(rdataset, rdata);
  1262. INSIST(dns_rdataset_next(rdataset) == ISC_R_NOMORE);
  1263. *update_class = rdata->rdclass;
  1264. rdata->rdclass = zoneclass;
  1265. }
  1266. /*%
  1267. * Increment the SOA serial number of database 'db', version 'ver'.
  1268. * Replace the SOA record in the database, and log the
  1269. * change in 'diff'.
  1270. */
  1271. /*
  1272. * XXXRTH Failures in this routine will be worth logging, when
  1273. * we have a logging system. Failure to find the zonename
  1274. * or the SOA rdataset warrant at least an UNEXPECTED_ERROR().
  1275. */
  1276. static isc_result_t
  1277. increment_soa_serial(dns_db_t *db, dns_dbversion_t *ver,
  1278. dns_diff_t *diff, isc_mem_t *mctx)
  1279. {
  1280. dns_difftuple_t *deltuple = NULL;
  1281. dns_difftuple_t *addtuple = NULL;
  1282. isc_uint32_t serial;
  1283. isc_result_t result;
  1284. CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_DEL, &deltuple));
  1285. CHECK(dns_difftuple_copy(deltuple, &addtuple));
  1286. addtuple->op = DNS_DIFFOP_ADD;
  1287. serial = dns_soa_getserial(&addtuple->rdata);
  1288. /* RFC1982 */
  1289. serial = (serial + 1) & 0xFFFFFFFF;
  1290. if (serial == 0)
  1291. serial = 1;
  1292. dns_soa_setserial(serial, &addtuple->rdata);
  1293. CHECK(do_one_tuple(&deltuple, db, ver, diff));
  1294. CHECK(do_one_tuple(&addtuple, db, ver, diff));
  1295. result = ISC_R_SUCCESS;
  1296. failure:
  1297. if (addtuple != NULL)
  1298. dns_difftuple_free(&addtuple);
  1299. if (deltuple != NULL)
  1300. dns_difftuple_free(&deltuple);
  1301. return (result);
  1302. }
  1303. /*%
  1304. * Check that the new SOA record at 'update_rdata' does not
  1305. * illegally cause the SOA serial number to decrease or stay
  1306. * unchanged relative to the existing SOA in 'db'.
  1307. *
  1308. * Sets '*ok' to ISC_TRUE if the update is legal, ISC_FALSE if not.
  1309. *
  1310. * William King points out that RFC2136 is inconsistent about
  1311. * the case where the serial number stays unchanged:
  1312. *
  1313. * section 3.4.2.2 requires a server to ignore a SOA update request
  1314. * if the serial number on the update SOA is less_than_or_equal to
  1315. * the zone SOA serial.
  1316. *
  1317. * section 3.6 requires a server to ignore a SOA update request if
  1318. * the serial is less_than the zone SOA serial.
  1319. *
  1320. * Paul says 3.4.2.2 is correct.
  1321. *
  1322. */
  1323. static isc_result_t
  1324. check_soa_increment(dns_db_t *db, dns_dbversion_t *ver,
  1325. dns_rdata_t *update_rdata, isc_boolean_t *ok)
  1326. {
  1327. isc_uint32_t db_serial;
  1328. isc_uint32_t update_serial;
  1329. isc_result_t result;
  1330. update_serial = dns_soa_getserial(update_rdata);
  1331. result = dns_db_getsoaserial(db, ver, &db_serial);
  1332. if (result != ISC_R_SUCCESS)
  1333. return (result);
  1334. if (DNS_SERIAL_GE(db_serial, update_serial)) {
  1335. *ok = ISC_FALSE;
  1336. } else {
  1337. *ok = ISC_TRUE;
  1338. }
  1339. return (ISC_R_SUCCESS);
  1340. }
  1341. /**************************************************************************/
  1342. /*
  1343. * Incremental updating of NSECs and RRSIGs.
  1344. */
  1345. /*%
  1346. * We abuse the dns_diff_t type to represent a set of domain names
  1347. * affected by the update.
  1348. */
  1349. static isc_result_t
  1350. namelist_append_name(dns_diff_t *list, dns_name_t *name) {
  1351. isc_result_t result;
  1352. dns_difftuple_t *tuple = NULL;
  1353. static dns_rdata_t dummy_rdata = DNS_RDATA_INIT;
  1354. CHECK(dns_difftuple_create(list->mctx, DNS_DIFFOP_EXISTS, name, 0,
  1355. &dummy_rdata, &tuple));
  1356. dns_diff_append(list, &tuple);
  1357. failure:
  1358. return (result);
  1359. }
  1360. static isc_result_t
  1361. namelist_append_subdomain(dns_db_t *db, dns_name_t *name, dns_diff_t *affected)
  1362. {
  1363. isc_result_t result;
  1364. dns_fixedname_t fixedname;
  1365. dns_name_t *child;
  1366. dns_dbiterator_t *dbit = NULL;
  1367. dns_fixedname_init(&fixedname);
  1368. child = dns_fixedname_name(&fixedname);
  1369. CHECK(dns_db_createiterator(db, DNS_DB_NONSEC3, &dbit));
  1370. for (result = dns_dbiterator_seek(dbit, name);
  1371. result == ISC_R_SUCCESS;
  1372. result = dns_dbiterator_next(dbit))
  1373. {
  1374. dns_dbnode_t *node = NULL;
  1375. CHECK(dns_dbiterator_current(dbit, &node, child));
  1376. dns_db_detachnode(db, &node);
  1377. if (! dns_name_issubdomain(child, name))
  1378. break;
  1379. CHECK(namelist_append_name(affected, child));
  1380. }
  1381. if (result == ISC_R_NOMORE)
  1382. result = ISC_R_SUCCESS;
  1383. failure:
  1384. if (dbit != NULL)
  1385. dns_dbiterator_destroy(&dbit);
  1386. return (result);
  1387. }
  1388. /*%
  1389. * Helper function for non_nsec_rrset_exists().
  1390. */
  1391. static isc_result_t
  1392. is_non_nsec_action(void *data, dns_rdataset_t *rrset) {
  1393. UNUSED(data);
  1394. if (!(rrset->type == dns_rdatatype_nsec ||
  1395. rrset->type == dns_rdatatype_nsec3 ||
  1396. (rrset->type == dns_rdatatype_rrsig &&
  1397. (rrset->covers == dns_rdatatype_nsec ||
  1398. rrset->covers == dns_rdatatype_nsec3))))
  1399. return (ISC_R_EXISTS);
  1400. return (ISC_R_SUCCESS);
  1401. }
  1402. /*%
  1403. * Check whether there is an rrset other than a NSEC or RRSIG NSEC,
  1404. * i.e., anything that justifies the continued existence of a name
  1405. * after a secure update.
  1406. *
  1407. * If such an rrset exists, set '*exists' to ISC_TRUE.
  1408. * Otherwise, set it to ISC_FALSE.
  1409. */
  1410. static isc_result_t
  1411. non_nsec_rrset_exists(dns_db_t *db, dns_dbversion_t *ver,
  1412. dns_name_t *name, isc_boolean_t *exists)
  1413. {
  1414. isc_result_t result;
  1415. result = foreach_rrset(db, ver, name, is_non_nsec_action, NULL);
  1416. RETURN_EXISTENCE_FLAG;
  1417. }
  1418. /*%
  1419. * A comparison function for sorting dns_diff_t:s by name.
  1420. */
  1421. static int
  1422. name_order(const void *av, const void *bv) {
  1423. dns_difftuple_t const * const *ap = av;
  1424. dns_difftuple_t const * const *bp = bv;
  1425. dns_difftuple_t const *a = *ap;
  1426. dns_difftuple_t const *b = *bp;
  1427. return (dns_name_compare(&a->name, &b->name));
  1428. }
  1429. static isc_result_t
  1430. uniqify_name_list(dns_diff_t *list) {
  1431. isc_result_t result;
  1432. dns_difftuple_t *p, *q;
  1433. CHECK(dns_diff_sort(list, name_order));
  1434. p = ISC_LIST_HEAD(list->tuples);
  1435. while (p != NULL) {
  1436. do {
  1437. q = ISC_LIST_NEXT(p, link);
  1438. if (q == NULL || ! dns_name_equal(&p->name, &q->name))
  1439. break;
  1440. ISC_LIST_UNLINK(list->tuples, q, link);
  1441. dns_difftuple_free(&q);
  1442. } while (1);
  1443. p = ISC_LIST_NEXT(p, link);
  1444. }
  1445. failure:
  1446. return (result);
  1447. }
  1448. static isc_result_t
  1449. is_active(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  1450. isc_boolean_t *flag, isc_boolean_t *cut, isc_boolean_t *unsecure)
  1451. {
  1452. isc_result_t result;
  1453. dns_fixedname_t foundname;
  1454. dns_fixedname_init(&foundname);
  1455. result = dns_db_find(db, name, ver, dns_rdatatype_any,
  1456. DNS_DBFIND_GLUEOK | DNS_DBFIND_NOWILD,
  1457. (isc_stdtime_t) 0, NULL,
  1458. dns_fixedname_name(&foundname),
  1459. NULL, NULL);
  1460. if (result == ISC_R_SUCCESS || result == DNS_R_EMPTYNAME) {
  1461. *flag = ISC_TRUE;
  1462. *cut = ISC_FALSE;
  1463. if (unsecure != NULL)
  1464. *unsecure = ISC_FALSE;
  1465. return (ISC_R_SUCCESS);
  1466. } else if (result == DNS_R_ZONECUT) {
  1467. *flag = ISC_TRUE;
  1468. *cut = ISC_TRUE;
  1469. if (unsecure != NULL) {
  1470. /*
  1471. * We are at the zonecut. Check to see if there
  1472. * is a DS RRset.
  1473. */
  1474. if (dns_db_find(db, name, ver, dns_rdatatype_ds, 0,
  1475. (isc_stdtime_t) 0, NULL,
  1476. dns_fixedname_name(&foundname),
  1477. NULL, NULL) == DNS_R_NXRRSET)
  1478. *unsecure = ISC_TRUE;
  1479. else
  1480. *unsecure = ISC_FALSE;
  1481. }
  1482. return (ISC_R_SUCCESS);
  1483. } else if (result == DNS_R_GLUE || result == DNS_R_DNAME ||
  1484. result == DNS_R_DELEGATION || result == DNS_R_NXDOMAIN) {
  1485. *flag = ISC_FALSE;
  1486. *cut = ISC_FALSE;
  1487. if (unsecure != NULL)
  1488. *unsecure = ISC_FALSE;
  1489. return (ISC_R_SUCCESS);
  1490. } else {
  1491. /*
  1492. * Silence compiler.
  1493. */
  1494. *flag = ISC_FALSE;
  1495. *cut = ISC_FALSE;
  1496. if (unsecure != NULL)
  1497. *unsecure = ISC_FALSE;
  1498. return (result);
  1499. }
  1500. }
  1501. /*%
  1502. * Find the next/previous name that has a NSEC record.
  1503. * In other words, skip empty database nodes and names that
  1504. * have had their NSECs removed because they are obscured by
  1505. * a zone cut.
  1506. */
  1507. static isc_result_t
  1508. next_active(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
  1509. dns_dbversion_t *ver, dns_name_t *oldname, dns_name_t *newname,
  1510. isc_boolean_t forward)
  1511. {
  1512. isc_result_t result;
  1513. dns_dbiterator_t *dbit = NULL;
  1514. isc_boolean_t has_nsec = ISC_FALSE;
  1515. unsigned int wraps = 0;
  1516. isc_boolean_t secure = dns_db_issecure(db);
  1517. CHECK(dns_db_createiterator(db, 0, &dbit));
  1518. CHECK(dns_dbiterator_seek(dbit, oldname));
  1519. do {
  1520. dns_dbnode_t *node = NULL;
  1521. if (forward)
  1522. result = dns_dbiterator_next(dbit);
  1523. else
  1524. result = dns_dbiterator_prev(dbit);
  1525. if (result == ISC_R_NOMORE) {
  1526. /*
  1527. * Wrap around.
  1528. */
  1529. if (forward)
  1530. CHECK(dns_dbiterator_first(dbit));
  1531. else
  1532. CHECK(dns_dbiterator_last(dbit));
  1533. wraps++;
  1534. if (wraps == 2) {
  1535. update_log(client, zone, ISC_LOG_ERROR,
  1536. "secure zone with no NSECs");
  1537. result = DNS_R_BADZONE;
  1538. goto failure;
  1539. }
  1540. }
  1541. CHECK(dns_dbiterator_current(dbit, &node, newname));
  1542. dns_db_detachnode(db, &node);
  1543. /*
  1544. * The iterator may hold the tree lock, and
  1545. * rrset_exists() calls dns_db_findnode() which
  1546. * may try to reacquire it. To avoid deadlock
  1547. * we must pause the iterator first.
  1548. */
  1549. CHECK(dns_dbiterator_pause(dbit));
  1550. if (secure) {
  1551. CHECK(rrset_exists(db, ver, newname,
  1552. dns_rdatatype_nsec, 0, &has_nsec));
  1553. } else {
  1554. dns_fixedname_t ffound;
  1555. dns_name_t *found;
  1556. dns_fixedname_init(&ffound);
  1557. found = dns_fixedname_name(&ffound);
  1558. result = dns_db_find(db, newname, ver,
  1559. dns_rdatatype_soa,
  1560. DNS_DBFIND_NOWILD, 0, NULL, found,
  1561. NULL, NULL);
  1562. if (result == ISC_R_SUCCESS ||
  1563. result == DNS_R_EMPTYNAME ||
  1564. result == DNS_R_NXRRSET ||
  1565. result == DNS_R_CNAME ||
  1566. (result == DNS_R_DELEGATION &&
  1567. dns_name_equal(newname, found))) {
  1568. has_nsec = ISC_TRUE;
  1569. result = ISC_R_SUCCESS;
  1570. } else if (result != DNS_R_NXDOMAIN)
  1571. break;
  1572. }
  1573. } while (! has_nsec);
  1574. failure:
  1575. if (dbit != NULL)
  1576. dns_dbiterator_destroy(&dbit);
  1577. return (result);
  1578. }
  1579. /*%
  1580. * Add a NSEC record for "name", recording the change in "diff".
  1581. * The existing NSEC is removed.
  1582. */
  1583. static isc_result_t
  1584. add_nsec(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
  1585. dns_dbversion_t *ver, dns_name_t *name, dns_ttl_t nsecttl,
  1586. dns_diff_t *diff)
  1587. {
  1588. isc_result_t result;
  1589. dns_dbnode_t *node = NULL;
  1590. unsigned char buffer[DNS_NSEC_BUFFERSIZE];
  1591. dns_rdata_t rdata = DNS_RDATA_INIT;
  1592. dns_difftuple_t *tuple = NULL;
  1593. dns_fixedname_t fixedname;
  1594. dns_name_t *target;
  1595. dns_fixedname_init(&fixedname);
  1596. target = dns_fixedname_name(&fixedname);
  1597. /*
  1598. * Find the successor name, aka NSEC target.
  1599. */
  1600. CHECK(next_active(client, zone, db, ver, name, target, ISC_TRUE));
  1601. /*
  1602. * Create the NSEC RDATA.
  1603. */
  1604. CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
  1605. dns_rdata_init(&rdata);
  1606. CHECK(dns_nsec_buildrdata(db, ver, node, target, buffer, &rdata));
  1607. dns_db_detachnode(db, &node);
  1608. /*
  1609. * Delete the old NSEC and record the change.
  1610. */
  1611. CHECK(delete_if(true_p, db, ver, name, dns_rdatatype_nsec, 0,
  1612. NULL, diff));
  1613. /*
  1614. * Add the new NSEC and record the change.
  1615. */
  1616. CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name,
  1617. nsecttl, &rdata, &tuple));
  1618. CHECK(do_one_tuple(&tuple, db, ver, diff));
  1619. INSIST(tuple == NULL);
  1620. failure:
  1621. if (node != NULL)
  1622. dns_db_detachnode(db, &node);
  1623. return (result);
  1624. }
  1625. /*%
  1626. * Add a placeholder NSEC record for "name", recording the change in "diff".
  1627. */
  1628. static isc_result_t
  1629. add_placeholder_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  1630. dns_diff_t *diff)
  1631. {
  1632. isc_result_t result;
  1633. dns_difftuple_t *tuple = NULL;
  1634. isc_region_t r;
  1635. unsigned char data[1] = { 0 }; /* The root domain, no bits. */
  1636. dns_rdata_t rdata = DNS_RDATA_INIT;
  1637. r.base = data;
  1638. r.length = sizeof(data);
  1639. dns_rdata_fromregion(&rdata, dns_db_class(db), dns_rdatatype_nsec, &r);
  1640. CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, 0,
  1641. &rdata, &tuple));
  1642. CHECK(do_one_tuple(&tuple, db, ver, diff));
  1643. failure:
  1644. return (result);
  1645. }
  1646. static isc_result_t
  1647. find_zone_keys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
  1648. isc_mem_t *mctx, unsigned int maxkeys,
  1649. dst_key_t **keys, unsigned int *nkeys)
  1650. {
  1651. isc_result_t result;
  1652. dns_dbnode_t *node = NULL;
  1653. const char *directory = dns_zone_getkeydirectory(zone);
  1654. CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
  1655. CHECK(dns_dnssec_findzonekeys2(db, ver, node, dns_db_origin(db),
  1656. directory, mctx, maxkeys, keys, nkeys));
  1657. failure:
  1658. if (node != NULL)
  1659. dns_db_detachnode(db, &node);
  1660. return (result);
  1661. }
  1662. /*%
  1663. * Add RRSIG records for an RRset, recording the change in "diff".
  1664. */
  1665. static isc_result_t
  1666. add_sigs(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
  1667. dns_dbversion_t *ver, dns_name_t *name, dns_rdatatype_t type,
  1668. dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys,
  1669. isc_stdtime_t inception, isc_stdtime_t expire,
  1670. isc_boolean_t check_ksk, isc_boolean_t keyset_kskonly)
  1671. {
  1672. isc_result_t result;
  1673. dns_dbnode_t *node = NULL;
  1674. dns_rdataset_t rdataset;
  1675. dns_rdata_t sig_rdata = DNS_RDATA_INIT;
  1676. isc_buffer_t buffer;
  1677. unsigned char data[1024]; /* XXX */
  1678. unsigned int i, j;
  1679. isc_boolean_t added_sig = ISC_FALSE;
  1680. isc_mem_t *mctx = client->mctx;
  1681. dns_rdataset_init(&rdataset);
  1682. isc_buffer_init(&buffer, data, sizeof(data));
  1683. /* Get the rdataset to sign. */
  1684. if (type == dns_rdatatype_nsec3)
  1685. CHECK(dns_db_findnsec3node(db, name, ISC_FALSE, &node));
  1686. else
  1687. CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
  1688. CHECK(dns_db_findrdataset(db, node, ver, type, 0,
  1689. (isc_stdtime_t) 0, &rdataset, NULL));
  1690. dns_db_detachnode(db, &node);
  1691. #define REVOKE(x) ((dst_key_flags(x) & DNS_KEYFLAG_REVOKE) != 0)
  1692. #define KSK(x) ((dst_key_flags(x) & DNS_KEYFLAG_KSK) != 0)
  1693. #define ALG(x) dst_key_alg(x)
  1694. /*
  1695. * If we are honoring KSK flags then we need to check that we
  1696. * have both KSK and non-KSK keys that are not revoked per
  1697. * algorithm.
  1698. */
  1699. for (i = 0; i < nkeys; i++) {
  1700. isc_boolean_t both = ISC_FALSE;
  1701. if (!dst_key_isprivate(keys[i]))
  1702. continue;
  1703. if (check_ksk && !REVOKE(keys[i])) {
  1704. isc_boolean_t have_ksk, have_nonksk;
  1705. if (KSK(keys[i])) {
  1706. have_ksk = ISC_TRUE;
  1707. have_nonksk = ISC_FALSE;
  1708. } else {
  1709. have_ksk = ISC_FALSE;
  1710. have_nonksk = ISC_TRUE;
  1711. }
  1712. for (j = 0; j < nkeys; j++) {
  1713. if (j == i || ALG(keys[i]) != ALG(keys[j]))
  1714. continue;
  1715. if (REVOKE(keys[j]))
  1716. continue;
  1717. if (KSK(keys[j]))
  1718. have_ksk = ISC_TRUE;
  1719. else
  1720. have_nonksk = ISC_TRUE;
  1721. both = have_ksk && have_nonksk;
  1722. if (both)
  1723. break;
  1724. }
  1725. }
  1726. if (both) {
  1727. if (type == dns_rdatatype_dnskey) {
  1728. if (!KSK(keys[i]) && keyset_kskonly)
  1729. continue;
  1730. } else if (KSK(keys[i]))
  1731. continue;
  1732. } else if (REVOKE(keys[i]) && type != dns_rdatatype_dnskey)
  1733. continue;
  1734. /* Calculate the signature, creating a RRSIG RDATA. */
  1735. CHECK(dns_dnssec_sign(name, &rdataset, keys[i],
  1736. &inception, &expire,
  1737. mctx, &buffer, &sig_rdata));
  1738. /* Update the database and journal with the RRSIG. */
  1739. /* XXX inefficient - will cause dataset merging */
  1740. CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADDRESIGN, name,
  1741. rdataset.ttl, &sig_rdata));
  1742. dns_rdata_reset(&sig_rdata);
  1743. isc_buffer_init(&buffer, data, sizeof(data));
  1744. added_sig = ISC_TRUE;
  1745. }
  1746. if (!added_sig) {
  1747. update_log(client, zone, ISC_LOG_ERROR,
  1748. "found no active private keys, "
  1749. "unable to generate any signatures");
  1750. result = ISC_R_NOTFOUND;
  1751. }
  1752. failure:
  1753. if (dns_rdataset_isassociated(&rdataset))
  1754. dns_rdataset_disassociate(&rdataset);
  1755. if (node != NULL)
  1756. dns_db_detachnode(db, &node);
  1757. return (result);
  1758. }
  1759. /*
  1760. * Delete expired RRsigs and any RRsigs we are about to re-sign.
  1761. * See also zone.c:del_sigs().
  1762. */
  1763. static isc_result_t
  1764. del_keysigs(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  1765. dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys)
  1766. {
  1767. isc_result_t result;
  1768. dns_dbnode_t *node = NULL;
  1769. dns_rdataset_t rdataset;
  1770. dns_rdata_t rdata = DNS_RDATA_INIT;
  1771. unsigned int i;
  1772. dns_rdata_rrsig_t rrsig;
  1773. isc_boolean_t found;
  1774. dns_rdataset_init(&rdataset);
  1775. result = dns_db_findnode(db, name, ISC_FALSE, &node);
  1776. if (result == ISC_R_NOTFOUND)
  1777. return (ISC_R_SUCCESS);
  1778. if (result != ISC_R_SUCCESS)
  1779. goto failure;
  1780. result = dns_db_findrdataset(db, node, ver, dns_rdatatype_rrsig,
  1781. dns_rdatatype_dnskey, (isc_stdtime_t) 0,
  1782. &rdataset, NULL);
  1783. dns_db_detachnode(db, &node);
  1784. if (result == ISC_R_NOTFOUND)
  1785. return (ISC_R_SUCCESS);
  1786. if (result != ISC_R_SUCCESS)
  1787. goto failure;
  1788. for (result = dns_rdataset_first(&rdataset);
  1789. result == ISC_R_SUCCESS;
  1790. result = dns_rdataset_next(&rdataset)) {
  1791. dns_rdataset_current(&rdataset, &rdata);
  1792. result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
  1793. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  1794. found = ISC_FALSE;
  1795. for (i = 0; i < nkeys; i++) {
  1796. if (rrsig.keyid == dst_key_id(keys[i])) {
  1797. found = ISC_TRUE;
  1798. if (!dst_key_isprivate(keys[i])) {
  1799. /*
  1800. * The re-signing code in zone.c
  1801. * will mark this as offline.
  1802. * Just skip the record for now.
  1803. */
  1804. break;
  1805. }
  1806. result = update_one_rr(db, ver, diff,
  1807. DNS_DIFFOP_DEL, name,
  1808. rdataset.ttl, &rdata);
  1809. break;
  1810. }
  1811. }
  1812. /*
  1813. * If there is not a matching DNSKEY then delete the RRSIG.
  1814. */
  1815. if (!found)
  1816. result = update_one_rr(db, ver, diff, DNS_DIFFOP_DEL,
  1817. name, rdataset.ttl, &rdata);
  1818. dns_rdata_reset(&rdata);
  1819. if (result != ISC_R_SUCCESS)
  1820. break;
  1821. }
  1822. dns_rdataset_disassociate(&rdataset);
  1823. if (result == ISC_R_NOMORE)
  1824. result = ISC_R_SUCCESS;
  1825. failure:
  1826. if (node != NULL)
  1827. dns_db_detachnode(db, &node);
  1828. return (result);
  1829. }
  1830. static isc_result_t
  1831. add_exposed_sigs(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
  1832. dns_dbversion_t *ver, dns_name_t *name, isc_boolean_t cut,
  1833. dns_diff_t *diff, dst_key_t **keys, unsigned int nkeys,
  1834. isc_stdtime_t inception, isc_stdtime_t expire,
  1835. isc_boolean_t check_ksk, isc_boolean_t keyset_kskonly)
  1836. {
  1837. isc_result_t result;
  1838. dns_dbnode_t *node;
  1839. dns_rdatasetiter_t *iter;
  1840. node = NULL;
  1841. result = dns_db_findnode(db, name, ISC_FALSE, &node);
  1842. if (result == ISC_R_NOTFOUND)
  1843. return (ISC_R_SUCCESS);
  1844. if (result != ISC_R_SUCCESS)
  1845. return (result);
  1846. iter = NULL;
  1847. result = dns_db_allrdatasets(db, node, ver,
  1848. (isc_stdtime_t) 0, &iter);
  1849. if (result != ISC_R_SUCCESS)
  1850. goto cleanup_node;
  1851. for (result = dns_rdatasetiter_first(iter);
  1852. result == ISC_R_SUCCESS;
  1853. result = dns_rdatasetiter_next(iter))
  1854. {
  1855. dns_rdataset_t rdataset;
  1856. dns_rdatatype_t type;
  1857. isc_boolean_t flag;
  1858. dns_rdataset_init(&rdataset);
  1859. dns_rdatasetiter_current(iter, &rdataset);
  1860. type = rdataset.type;
  1861. dns_rdataset_disassociate(&rdataset);
  1862. /*
  1863. * We don't need to sign unsigned NSEC records at the cut
  1864. * as they are handled elsewhere.
  1865. */
  1866. if ((type == dns_rdatatype_rrsig) ||
  1867. (cut && type != dns_rdatatype_ds))
  1868. continue;
  1869. result = rrset_exists(db, ver, name, dns_rdatatype_rrsig,
  1870. type, &flag);
  1871. if (result != ISC_R_SUCCESS)
  1872. goto cleanup_iterator;
  1873. if (flag)
  1874. continue;;
  1875. result = add_sigs(client, zone, db, ver, name, type, diff,
  1876. keys, nkeys, inception, expire,
  1877. check_ksk, keyset_kskonly);
  1878. if (result != ISC_R_SUCCESS)
  1879. goto cleanup_iterator;
  1880. }
  1881. if (result == ISC_R_NOMORE)
  1882. result = ISC_R_SUCCESS;
  1883. cleanup_iterator:
  1884. dns_rdatasetiter_destroy(&iter);
  1885. cleanup_node:
  1886. dns_db_detachnode(db, &node);
  1887. return (result);
  1888. }
  1889. /*%
  1890. * Update RRSIG, NSEC and NSEC3 records affected by an update. The original
  1891. * update, including the SOA serial update but excluding the RRSIG & NSEC
  1892. * changes, is in "diff" and has already been applied to "newver" of "db".
  1893. * The database version prior to the update is "oldver".
  1894. *
  1895. * The necessary RRSIG, NSEC and NSEC3 changes will be applied to "newver"
  1896. * and added (as a minimal diff) to "diff".
  1897. *
  1898. * The RRSIGs generated will be valid for 'sigvalidityinterval' seconds.
  1899. */
  1900. static isc_result_t
  1901. update_signatures(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
  1902. dns_dbversion_t *oldver, dns_dbversion_t *newver,
  1903. dns_diff_t *diff, isc_uint32_t sigvalidityinterval)
  1904. {
  1905. isc_result_t result;
  1906. dns_difftuple_t *t;
  1907. dns_diff_t diffnames;
  1908. dns_diff_t affected;
  1909. dns_diff_t sig_diff;
  1910. dns_diff_t nsec_diff;
  1911. dns_diff_t nsec_mindiff;
  1912. isc_boolean_t flag, build_nsec, build_nsec3;
  1913. dst_key_t *zone_keys[DNS_MAXZONEKEYS];
  1914. unsigned int nkeys = 0;
  1915. unsigned int i;
  1916. isc_stdtime_t now, inception, expire;
  1917. dns_ttl_t nsecttl;
  1918. dns_rdata_soa_t soa;
  1919. dns_rdata_t rdata = DNS_RDATA_INIT;
  1920. dns_rdataset_t rdataset;
  1921. dns_dbnode_t *node = NULL;
  1922. isc_boolean_t check_ksk, keyset_kskonly;
  1923. isc_boolean_t unsecure;
  1924. isc_boolean_t cut;
  1925. dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
  1926. dns_diff_init(client->mctx, &diffnames);
  1927. dns_diff_init(client->mctx, &affected);
  1928. dns_diff_init(client->mctx, &sig_diff);
  1929. sig_diff.resign = dns_zone_getsigresigninginterval(zone);
  1930. dns_diff_init(client->mctx, &nsec_diff);
  1931. dns_diff_init(client->mctx, &nsec_mindiff);
  1932. result = find_zone_keys(zone, db, newver, client->mctx,
  1933. DNS_MAXZONEKEYS, zone_keys, &nkeys);
  1934. if (result != ISC_R_SUCCESS) {
  1935. update_log(client, zone, ISC_LOG_ERROR,
  1936. "could not get zone keys for secure dynamic update");
  1937. goto failure;
  1938. }
  1939. isc_stdtime_get(&now);
  1940. inception = now - 3600; /* Allow for some clock skew. */
  1941. expire = now + sigvalidityinterval;
  1942. /*
  1943. * Do we look at the KSK flag on the DNSKEY to determining which
  1944. * keys sign which RRsets? First check the zone option then
  1945. * check the keys flags to make sure at least one has a ksk set
  1946. * and one doesn't.
  1947. */
  1948. check_ksk = ISC_TF((dns_zone_getoptions(zone) &
  1949. DNS_ZONEOPT_UPDATECHECKKSK) != 0);
  1950. keyset_kskonly = ISC_TF((dns_zone_getoptions(zone) &
  1951. DNS_ZONEOPT_DNSKEYKSKONLY) != 0);
  1952. /*
  1953. * Get the NSEC/NSEC3 TTL from the SOA MINIMUM field.
  1954. */
  1955. CHECK(dns_db_findnode(db, dns_db_origin(db), ISC_FALSE, &node));
  1956. dns_rdataset_init(&rdataset);
  1957. CHECK(dns_db_findrdataset(db, node, newver, dns_rdatatype_soa, 0,
  1958. (isc_stdtime_t) 0, &rdataset, NULL));
  1959. CHECK(dns_rdataset_first(&rdataset));
  1960. dns_rdataset_current(&rdataset, &rdata);
  1961. CHECK(dns_rdata_tostruct(&rdata, &soa, NULL));
  1962. nsecttl = soa.minimum;
  1963. dns_rdataset_disassociate(&rdataset);
  1964. dns_db_detachnode(db, &node);
  1965. /*
  1966. * Find all RRsets directly affected by the update, and
  1967. * update their RRSIGs. Also build a list of names affected
  1968. * by the update in "diffnames".
  1969. */
  1970. CHECK(dns_diff_sort(diff, temp_order));
  1971. t = ISC_LIST_HEAD(diff->tuples);
  1972. while (t != NULL) {
  1973. dns_name_t *name = &t->name;
  1974. /* Now "name" is a new, unique name affected by the update. */
  1975. CHECK(namelist_append_name(&diffnames, name));
  1976. while (t != NULL && dns_name_equal(&t->name, name)) {
  1977. dns_rdatatype_t type;
  1978. type = t->rdata.type;
  1979. /*
  1980. * Now "name" and "type" denote a new unique RRset
  1981. * affected by the update.
  1982. */
  1983. /* Don't sign RRSIGs. */
  1984. if (type == dns_rdatatype_rrsig)
  1985. goto skip;
  1986. /*
  1987. * Delete all old RRSIGs covering this type, since they
  1988. * are all invalid when the signed RRset has changed.
  1989. * We may not be able to recreate all of them - tough.
  1990. * Special case changes to the zone's DNSKEY records
  1991. * to support offline KSKs.
  1992. */
  1993. if (type == dns_rdatatype_dnskey)
  1994. del_keysigs(db, newver, name, &sig_diff,
  1995. zone_keys, nkeys);
  1996. else
  1997. CHECK(delete_if(true_p, db, newver, name,
  1998. dns_rdatatype_rrsig, type,
  1999. NULL, &sig_diff));
  2000. /*
  2001. * If this RRset is still visible after the update,
  2002. * add a new signature for it.
  2003. */
  2004. CHECK(rrset_visible(db, newver, name, type, &flag));
  2005. if (flag) {
  2006. CHECK(add_sigs(client, zone, db, newver, name,
  2007. type, &sig_diff, zone_keys,
  2008. nkeys, inception, expire,
  2009. check_ksk, keyset_kskonly));
  2010. }
  2011. skip:
  2012. /* Skip any other updates to the same RRset. */
  2013. while (t != NULL &&
  2014. dns_name_equal(&t->name, name) &&
  2015. t->rdata.type == type)
  2016. {
  2017. t = ISC_LIST_NEXT(t, link);
  2018. }
  2019. }
  2020. }
  2021. update_log(client, zone, ISC_LOG_DEBUG(3), "updated data signatures");
  2022. /* Remove orphaned NSECs and RRSIG NSECs. */
  2023. for (t = ISC_LIST_HEAD(diffnames.tuples);
  2024. t != NULL;
  2025. t = ISC_LIST_NEXT(t, link))
  2026. {
  2027. CHECK(non_nsec_rrset_exists(db, newver, &t->name, &flag));
  2028. if (! flag) {
  2029. CHECK(delete_if(true_p, db, newver, &t->name,
  2030. dns_rdatatype_any, 0,
  2031. NULL, &sig_diff));
  2032. }
  2033. }
  2034. update_log(client, zone, ISC_LOG_DEBUG(3),
  2035. "removed any orphaned NSEC records");
  2036. /*
  2037. * See if we need to build NSEC or NSEC3 chains.
  2038. */
  2039. CHECK(dns_private_chains(db, newver, privatetype, &build_nsec,
  2040. &build_nsec3));
  2041. if (!build_nsec)
  2042. goto update_nsec3;
  2043. update_log(client, zone, ISC_LOG_DEBUG(3), "rebuilding NSEC chain");
  2044. /*
  2045. * When a name is created or deleted, its predecessor needs to
  2046. * have its NSEC updated.
  2047. */
  2048. for (t = ISC_LIST_HEAD(diffnames.tuples);
  2049. t != NULL;
  2050. t = ISC_LIST_NEXT(t, link))
  2051. {
  2052. isc_boolean_t existed, exists;
  2053. dns_fixedname_t fixedname;
  2054. dns_name_t *prevname;
  2055. dns_fixedname_init(&fixedname);
  2056. prevname = dns_fixedname_name(&fixedname);
  2057. CHECK(name_exists(db, oldver, &t->name, &existed));
  2058. CHECK(name_exists(db, newver, &t->name, &exists));
  2059. if (exists == existed)
  2060. continue;
  2061. /*
  2062. * Find the predecessor.
  2063. * When names become obscured or unobscured in this update
  2064. * transaction, we may find the wrong predecessor because
  2065. * the NSECs have not yet been updated to reflect the delegation
  2066. * change. This should not matter because in this case,
  2067. * the correct predecessor is either the delegation node or
  2068. * a newly unobscured node, and those nodes are on the
  2069. * "affected" list in any case.
  2070. */
  2071. CHECK(next_active(client, zone, db, newver,
  2072. &t->name, prevname, ISC_FALSE));
  2073. CHECK(namelist_append_name(&affected, prevname));
  2074. }
  2075. /*
  2076. * Find names potentially affected by delegation changes
  2077. * (obscured by adding an NS or DNAME, or unobscured by
  2078. * removing one).
  2079. */
  2080. for (t = ISC_LIST_HEAD(diffnames.tuples);
  2081. t != NULL;
  2082. t = ISC_LIST_NEXT(t, link))
  2083. {
  2084. isc_boolean_t ns_existed, dname_existed;
  2085. isc_boolean_t ns_exists, dname_exists;
  2086. CHECK(rrset_exists(db, oldver, &t->name, dns_rdatatype_ns, 0,
  2087. &ns_existed));
  2088. CHECK(rrset_exists(db, oldver, &t->name, dns_rdatatype_dname, 0,
  2089. &dname_existed));
  2090. CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_ns, 0,
  2091. &ns_exists));
  2092. CHECK(rrset_exists(db, newver, &t->name, dns_rdatatype_dname, 0,
  2093. &dname_exists));
  2094. if ((ns_exists || dname_exists) == (ns_existed || dname_existed))
  2095. continue;
  2096. /*
  2097. * There was a delegation change. Mark all subdomains
  2098. * of t->name as potentially needing a NSEC update.
  2099. */
  2100. CHECK(namelist_append_subdomain(db, &t->name, &affected));
  2101. }
  2102. ISC_LIST_APPENDLIST(affected.tuples, diffnames.tuples, link);
  2103. INSIST(ISC_LIST_EMPTY(diffnames.tuples));
  2104. CHECK(uniqify_name_list(&affected));
  2105. /*
  2106. * Determine which names should have NSECs, and delete/create
  2107. * NSECs to make it so. We don't know the final NSEC targets yet,
  2108. * so we just create placeholder NSECs with arbitrary contents
  2109. * to indicate that their respective owner names should be part of
  2110. * the NSEC chain.
  2111. */
  2112. for (t = ISC_LIST_HEAD(affected.tuples);
  2113. t != NULL;
  2114. t = ISC_LIST_NEXT(t, link))
  2115. {
  2116. isc_boolean_t exists;
  2117. dns_name_t *name = &t->name;
  2118. CHECK(name_exists(db, newver, name, &exists));
  2119. if (! exists)
  2120. continue;
  2121. CHECK(is_active(db, newver, name, &flag, &cut, NULL));
  2122. if (!flag) {
  2123. /*
  2124. * This name is obscured. Delete any
  2125. * existing NSEC record.
  2126. */
  2127. CHECK(delete_if(true_p, db, newver, name,
  2128. dns_rdatatype_nsec, 0,
  2129. NULL, &nsec_diff));
  2130. CHECK(delete_if(rrsig_p, db, newver, name,
  2131. dns_rdatatype_any, 0, NULL, diff));
  2132. } else {
  2133. /*
  2134. * This name is not obscured. It needs to have a
  2135. * NSEC unless it is the at the origin, in which
  2136. * case it should already exist if there is a complete
  2137. * NSEC chain and if there isn't a complete NSEC chain
  2138. * we don't want to add one as that would signal that
  2139. * there is a complete NSEC chain.
  2140. */
  2141. if (!dns_name_equal(name, dns_db_origin(db))) {
  2142. CHECK(rrset_exists(db, newver, name,
  2143. dns_rdatatype_nsec, 0,
  2144. &flag));
  2145. if (!flag)
  2146. CHECK(add_placeholder_nsec(db, newver,
  2147. name, diff));
  2148. }
  2149. CHECK(add_exposed_sigs(client, zone, db, newver, name,
  2150. cut, &sig_diff, zone_keys, nkeys,
  2151. inception, expire, check_ksk,
  2152. keyset_kskonly));
  2153. }
  2154. }
  2155. /*
  2156. * Now we know which names are part of the NSEC chain.
  2157. * Make them all point at their correct targets.
  2158. */
  2159. for (t = ISC_LIST_HEAD(affected.tuples);
  2160. t != NULL;
  2161. t = ISC_LIST_NEXT(t, link))
  2162. {
  2163. CHECK(rrset_exists(db, newver, &t->name,
  2164. dns_rdatatype_nsec, 0, &flag));
  2165. if (flag) {
  2166. /*
  2167. * There is a NSEC, but we don't know if it is correct.
  2168. * Delete it and create a correct one to be sure.
  2169. * If the update was unnecessary, the diff minimization
  2170. * will take care of eliminating it from the journal,
  2171. * IXFRs, etc.
  2172. *
  2173. * The RRSIG bit should always be set in the NSECs
  2174. * we generate, because they will all get RRSIG NSECs.
  2175. * (XXX what if the zone keys are missing?).
  2176. * Because the RRSIG NSECs have not necessarily been
  2177. * created yet, the correctness of the bit mask relies
  2178. * on the assumption that NSECs are only created if
  2179. * there is other data, and if there is other data,
  2180. * there are other RRSIGs.
  2181. */
  2182. CHECK(add_nsec(client, zone, db, newver, &t->name,
  2183. nsecttl, &nsec_diff));
  2184. }
  2185. }
  2186. /*
  2187. * Minimize the set of NSEC updates so that we don't
  2188. * have to regenerate the RRSIG NSECs for NSECs that were
  2189. * replaced with identical ones.
  2190. */
  2191. while ((t = ISC_LIST_HEAD(nsec_diff.tuples)) != NULL) {
  2192. ISC_LIST_UNLINK(nsec_diff.tuples, t, link);
  2193. dns_diff_appendminimal(&nsec_mindiff, &t);
  2194. }
  2195. update_log(client, zone, ISC_LOG_DEBUG(3),
  2196. "signing rebuilt NSEC chain");
  2197. /* Update RRSIG NSECs. */
  2198. for (t = ISC_LIST_HEAD(nsec_mindiff.tuples);
  2199. t != NULL;
  2200. t = ISC_LIST_NEXT(t, link))
  2201. {
  2202. if (t->op == DNS_DIFFOP_DEL) {
  2203. CHECK(delete_if(true_p, db, newver, &t->name,
  2204. dns_rdatatype_rrsig, dns_rdatatype_nsec,
  2205. NULL, &sig_diff));
  2206. } else if (t->op == DNS_DIFFOP_ADD) {
  2207. CHECK(add_sigs(client, zone, db, newver, &t->name,
  2208. dns_rdatatype_nsec, &sig_diff,
  2209. zone_keys, nkeys, inception, expire,
  2210. check_ksk, keyset_kskonly));
  2211. } else {
  2212. INSIST(0);
  2213. }
  2214. }
  2215. update_nsec3:
  2216. /* Record our changes for the journal. */
  2217. while ((t = ISC_LIST_HEAD(sig_diff.tuples)) != NULL) {
  2218. ISC_LIST_UNLINK(sig_diff.tuples, t, link);
  2219. dns_diff_appendminimal(diff, &t);
  2220. }
  2221. while ((t = ISC_LIST_HEAD(nsec_mindiff.tuples)) != NULL) {
  2222. ISC_LIST_UNLINK(nsec_mindiff.tuples, t, link);
  2223. dns_diff_appendminimal(diff, &t);
  2224. }
  2225. INSIST(ISC_LIST_EMPTY(sig_diff.tuples));
  2226. INSIST(ISC_LIST_EMPTY(nsec_diff.tuples));
  2227. INSIST(ISC_LIST_EMPTY(nsec_mindiff.tuples));
  2228. if (!build_nsec3) {
  2229. update_log(client, zone, ISC_LOG_DEBUG(3),
  2230. "no NSEC3 chains to rebuild");
  2231. goto failure;
  2232. }
  2233. update_log(client, zone, ISC_LOG_DEBUG(3), "rebuilding NSEC3 chains");
  2234. dns_diff_clear(&diffnames);
  2235. dns_diff_clear(&affected);
  2236. CHECK(dns_diff_sort(diff, temp_order));
  2237. /*
  2238. * Find names potentially affected by delegation changes
  2239. * (obscured by adding an NS or DNAME, or unobscured by
  2240. * removing one).
  2241. */
  2242. t = ISC_LIST_HEAD(diff->tuples);
  2243. while (t != NULL) {
  2244. dns_name_t *name = &t->name;
  2245. isc_boolean_t ns_existed, dname_existed;
  2246. isc_boolean_t ns_exists, dname_exists;
  2247. isc_boolean_t exists, existed;
  2248. if (t->rdata.type == dns_rdatatype_nsec ||
  2249. t->rdata.type == dns_rdatatype_rrsig) {
  2250. t = ISC_LIST_NEXT(t, link);
  2251. continue;
  2252. }
  2253. CHECK(namelist_append_name(&affected, name));
  2254. CHECK(rrset_exists(db, oldver, name, dns_rdatatype_ns, 0,
  2255. &ns_existed));
  2256. CHECK(rrset_exists(db, oldver, name, dns_rdatatype_dname, 0,
  2257. &dname_existed));
  2258. CHECK(rrset_exists(db, newver, name, dns_rdatatype_ns, 0,
  2259. &ns_exists));
  2260. CHECK(rrset_exists(db, newver, name, dns_rdatatype_dname, 0,
  2261. &dname_exists));
  2262. exists = ns_exists || dname_exists;
  2263. existed = ns_existed || dname_existed;
  2264. if (exists == existed)
  2265. goto nextname;
  2266. /*
  2267. * There was a delegation change. Mark all subdomains
  2268. * of t->name as potentially needing a NSEC3 update.
  2269. */
  2270. CHECK(namelist_append_subdomain(db, name, &affected));
  2271. nextname:
  2272. while (t != NULL && dns_name_equal(&t->name, name))
  2273. t = ISC_LIST_NEXT(t, link);
  2274. }
  2275. for (t = ISC_LIST_HEAD(affected.tuples);
  2276. t != NULL;
  2277. t = ISC_LIST_NEXT(t, link)) {
  2278. dns_name_t *name = &t->name;
  2279. unsecure = ISC_FALSE; /* Silence compiler warning. */
  2280. CHECK(is_active(db, newver, name, &flag, &cut, &unsecure));
  2281. if (!flag) {
  2282. CHECK(delete_if(rrsig_p, db, newver, name,
  2283. dns_rdatatype_any, 0, NULL, diff));
  2284. CHECK(dns_nsec3_delnsec3sx(db, newver, name,
  2285. privatetype, &nsec_diff));
  2286. } else {
  2287. CHECK(add_exposed_sigs(client, zone, db, newver, name,
  2288. cut, &sig_diff, zone_keys, nkeys,
  2289. inception, expire, check_ksk,
  2290. keyset_kskonly));
  2291. CHECK(dns_nsec3_addnsec3sx(db, newver, name, nsecttl,
  2292. unsecure, privatetype,
  2293. &nsec_diff));
  2294. }
  2295. }
  2296. /*
  2297. * Minimize the set of NSEC3 updates so that we don't
  2298. * have to regenerate the RRSIG NSEC3s for NSEC3s that were
  2299. * replaced with identical ones.
  2300. */
  2301. while ((t = ISC_LIST_HEAD(nsec_diff.tuples)) != NULL) {
  2302. ISC_LIST_UNLINK(nsec_diff.tuples, t, link);
  2303. dns_diff_appendminimal(&nsec_mindiff, &t);
  2304. }
  2305. update_log(client, zone, ISC_LOG_DEBUG(3),
  2306. "signing rebuilt NSEC3 chain");
  2307. /* Update RRSIG NSEC3s. */
  2308. for (t = ISC_LIST_HEAD(nsec_mindiff.tuples);
  2309. t != NULL;
  2310. t = ISC_LIST_NEXT(t, link))
  2311. {
  2312. if (t->op == DNS_DIFFOP_DEL) {
  2313. CHECK(delete_if(true_p, db, newver, &t->name,
  2314. dns_rdatatype_rrsig,
  2315. dns_rdatatype_nsec3,
  2316. NULL, &sig_diff));
  2317. } else if (t->op == DNS_DIFFOP_ADD) {
  2318. CHECK(add_sigs(client, zone, db, newver, &t->name,
  2319. dns_rdatatype_nsec3,
  2320. &sig_diff, zone_keys, nkeys,
  2321. inception, expire, check_ksk,
  2322. keyset_kskonly));
  2323. } else {
  2324. INSIST(0);
  2325. }
  2326. }
  2327. /* Record our changes for the journal. */
  2328. while ((t = ISC_LIST_HEAD(sig_diff.tuples)) != NULL) {
  2329. ISC_LIST_UNLINK(sig_diff.tuples, t, link);
  2330. dns_diff_appendminimal(diff, &t);
  2331. }
  2332. while ((t = ISC_LIST_HEAD(nsec_mindiff.tuples)) != NULL) {
  2333. ISC_LIST_UNLINK(nsec_mindiff.tuples, t, link);
  2334. dns_diff_appendminimal(diff, &t);
  2335. }
  2336. INSIST(ISC_LIST_EMPTY(sig_diff.tuples));
  2337. INSIST(ISC_LIST_EMPTY(nsec_diff.tuples));
  2338. INSIST(ISC_LIST_EMPTY(nsec_mindiff.tuples));
  2339. failure:
  2340. dns_diff_clear(&sig_diff);
  2341. dns_diff_clear(&nsec_diff);
  2342. dns_diff_clear(&nsec_mindiff);
  2343. dns_diff_clear(&affected);
  2344. dns_diff_clear(&diffnames);
  2345. for (i = 0; i < nkeys; i++)
  2346. dst_key_free(&zone_keys[i]);
  2347. return (result);
  2348. }
  2349. /**************************************************************************/
  2350. /*%
  2351. * The actual update code in all its glory. We try to follow
  2352. * the RFC2136 pseudocode as closely as possible.
  2353. */
  2354. static isc_result_t
  2355. send_update_event(ns_client_t *client, dns_zone_t *zone) {
  2356. isc_result_t result = ISC_R_SUCCESS;
  2357. update_event_t *event = NULL;
  2358. isc_task_t *zonetask = NULL;
  2359. ns_client_t *evclient;
  2360. event = (update_event_t *)
  2361. isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
  2362. update_action, NULL, sizeof(*event));
  2363. if (event == NULL)
  2364. FAIL(ISC_R_NOMEMORY);
  2365. event->zone = zone;
  2366. event->result = ISC_R_SUCCESS;
  2367. evclient = NULL;
  2368. ns_client_attach(client, &evclient);
  2369. INSIST(client->nupdates == 0);
  2370. client->nupdates++;
  2371. event->ev_arg = evclient;
  2372. dns_zone_gettask(zone, &zonetask);
  2373. isc_task_send(zonetask, ISC_EVENT_PTR(&event));
  2374. failure:
  2375. if (event != NULL)
  2376. isc_event_free(ISC_EVENT_PTR(&event));
  2377. return (result);
  2378. }
  2379. static void
  2380. respond(ns_client_t *client, isc_result_t result) {
  2381. isc_result_t msg_result;
  2382. msg_result = dns_message_reply(client->message, ISC_TRUE);
  2383. if (msg_result != ISC_R_SUCCESS)
  2384. goto msg_failure;
  2385. client->message->rcode = dns_result_torcode(result);
  2386. ns_client_send(client);
  2387. return;
  2388. msg_failure:
  2389. isc_log_write(ns_g_lctx, NS_LOGCATEGORY_UPDATE, NS_LOGMODULE_UPDATE,
  2390. ISC_LOG_ERROR,
  2391. "could not create update response message: %s",
  2392. isc_result_totext(msg_result));
  2393. ns_client_next(client, msg_result);
  2394. }
  2395. void
  2396. ns_update_start(ns_client_t *client, isc_result_t sigresult) {
  2397. dns_message_t *request = client->message;
  2398. isc_result_t result;
  2399. dns_name_t *zonename;
  2400. dns_rdataset_t *zone_rdataset;
  2401. dns_zone_t *zone = NULL;
  2402. /*
  2403. * Interpret the zone section.
  2404. */
  2405. result = dns_message_firstname(request, DNS_SECTION_ZONE);
  2406. if (result != ISC_R_SUCCESS)
  2407. FAILC(DNS_R_FORMERR, "update zone section empty");
  2408. /*
  2409. * The zone section must contain exactly one "question", and
  2410. * it must be of type SOA.
  2411. */
  2412. zonename = NULL;
  2413. dns_message_currentname(request, DNS_SECTION_ZONE, &zonename);
  2414. zone_rdataset = ISC_LIST_HEAD(zonename->list);
  2415. if (zone_rdataset->type != dns_rdatatype_soa)
  2416. FAILC(DNS_R_FORMERR,
  2417. "update zone section contains non-SOA");
  2418. if (ISC_LIST_NEXT(zone_rdataset, link) != NULL)
  2419. FAILC(DNS_R_FORMERR,
  2420. "update zone section contains multiple RRs");
  2421. /* The zone section must have exactly one name. */
  2422. result = dns_message_nextname(request, DNS_SECTION_ZONE);
  2423. if (result != ISC_R_NOMORE)
  2424. FAILC(DNS_R_FORMERR,
  2425. "update zone section contains multiple RRs");
  2426. result = dns_zt_find(client->view->zonetable, zonename, 0, NULL,
  2427. &zone);
  2428. if (result != ISC_R_SUCCESS)
  2429. FAILC(DNS_R_NOTAUTH, "not authoritative for update zone");
  2430. switch(dns_zone_gettype(zone)) {
  2431. case dns_zone_master:
  2432. case dns_zone_dlz:
  2433. /*
  2434. * We can now fail due to a bad signature as we now know
  2435. * that we are the master.
  2436. */
  2437. if (sigresult != ISC_R_SUCCESS)
  2438. FAIL(sigresult);
  2439. CHECK(send_update_event(client, zone));
  2440. break;
  2441. case dns_zone_slave:
  2442. CHECK(checkupdateacl(client, dns_zone_getforwardacl(zone),
  2443. "update forwarding", zonename, ISC_TRUE,
  2444. ISC_FALSE));
  2445. CHECK(send_forward_event(client, zone));
  2446. break;
  2447. default:
  2448. FAILC(DNS_R_NOTAUTH, "not authoritative for update zone");
  2449. }
  2450. return;
  2451. failure:
  2452. if (result == DNS_R_REFUSED) {
  2453. INSIST(dns_zone_gettype(zone) == dns_zone_slave);
  2454. inc_stats(zone, dns_nsstatscounter_updaterej);
  2455. }
  2456. /*
  2457. * We failed without having sent an update event to the zone.
  2458. * We are still in the client task context, so we can
  2459. * simply give an error response without switching tasks.
  2460. */
  2461. respond(client, result);
  2462. if (zone != NULL)
  2463. dns_zone_detach(&zone);
  2464. }
  2465. /*%
  2466. * DS records are not allowed to exist without corresponding NS records,
  2467. * RFC 3658, 2.2 Protocol Change,
  2468. * "DS RRsets MUST NOT appear at non-delegation points or at a zone's apex".
  2469. */
  2470. static isc_result_t
  2471. remove_orphaned_ds(dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff) {
  2472. isc_result_t result;
  2473. isc_boolean_t ns_exists;
  2474. dns_difftuple_t *tupple;
  2475. dns_diff_t temp_diff;
  2476. dns_diff_init(diff->mctx, &temp_diff);
  2477. for (tupple = ISC_LIST_HEAD(diff->tuples);
  2478. tupple != NULL;
  2479. tupple = ISC_LIST_NEXT(tupple, link)) {
  2480. if (!((tupple->op == DNS_DIFFOP_DEL &&
  2481. tupple->rdata.type == dns_rdatatype_ns) ||
  2482. (tupple->op == DNS_DIFFOP_ADD &&
  2483. tupple->rdata.type == dns_rdatatype_ds)))
  2484. continue;
  2485. CHECK(rrset_exists(db, newver, &tupple->name,
  2486. dns_rdatatype_ns, 0, &ns_exists));
  2487. if (ns_exists &&
  2488. !dns_name_equal(&tupple->name, dns_db_origin(db)))
  2489. continue;
  2490. CHECK(delete_if(true_p, db, newver, &tupple->name,
  2491. dns_rdatatype_ds, 0, NULL, &temp_diff));
  2492. }
  2493. result = ISC_R_SUCCESS;
  2494. failure:
  2495. for (tupple = ISC_LIST_HEAD(temp_diff.tuples);
  2496. tupple != NULL;
  2497. tupple = ISC_LIST_HEAD(temp_diff.tuples)) {
  2498. ISC_LIST_UNLINK(temp_diff.tuples, tupple, link);
  2499. dns_diff_appendminimal(diff, &tupple);
  2500. }
  2501. return (result);
  2502. }
  2503. /*
  2504. * This implements the post load integrity checks for mx records.
  2505. */
  2506. static isc_result_t
  2507. check_mx(ns_client_t *client, dns_zone_t *zone,
  2508. dns_db_t *db, dns_dbversion_t *newver, dns_diff_t *diff)
  2509. {
  2510. char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123.")];
  2511. char ownerbuf[DNS_NAME_FORMATSIZE];
  2512. char namebuf[DNS_NAME_FORMATSIZE];
  2513. char altbuf[DNS_NAME_FORMATSIZE];
  2514. dns_difftuple_t *t;
  2515. dns_fixedname_t fixed;
  2516. dns_name_t *foundname;
  2517. dns_rdata_mx_t mx;
  2518. dns_rdata_t rdata;
  2519. isc_boolean_t ok = ISC_TRUE;
  2520. isc_boolean_t isaddress;
  2521. isc_result_t result;
  2522. struct in6_addr addr6;
  2523. struct in_addr addr;
  2524. unsigned int options;
  2525. dns_fixedname_init(&fixed);
  2526. foundname = dns_fixedname_name(&fixed);
  2527. dns_rdata_init(&rdata);
  2528. options = dns_zone_getoptions(zone);
  2529. for (t = ISC_LIST_HEAD(diff->tuples);
  2530. t != NULL;
  2531. t = ISC_LIST_NEXT(t, link)) {
  2532. if (t->op != DNS_DIFFOP_ADD ||
  2533. t->rdata.type != dns_rdatatype_mx)
  2534. continue;
  2535. result = dns_rdata_tostruct(&t->rdata, &mx, NULL);
  2536. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  2537. /*
  2538. * Check if we will error out if we attempt to reload the
  2539. * zone.
  2540. */
  2541. dns_name_format(&mx.mx, namebuf, sizeof(namebuf));
  2542. dns_name_format(&t->name, ownerbuf, sizeof(ownerbuf));
  2543. isaddress = ISC_FALSE;
  2544. if ((options & DNS_RDATA_CHECKMX) != 0 &&
  2545. strlcpy(tmp, namebuf, sizeof(tmp)) < sizeof(tmp)) {
  2546. if (tmp[strlen(tmp) - 1] == '.')
  2547. tmp[strlen(tmp) - 1] = '\0';
  2548. if (inet_aton(tmp, &addr) == 1 ||
  2549. inet_pton(AF_INET6, tmp, &addr6) == 1)
  2550. isaddress = ISC_TRUE;
  2551. }
  2552. if (isaddress && (options & DNS_RDATA_CHECKMXFAIL) != 0) {
  2553. update_log(client, zone, ISC_LOG_ERROR,
  2554. "%s/MX: '%s': %s",
  2555. ownerbuf, namebuf,
  2556. dns_result_totext(DNS_R_MXISADDRESS));
  2557. ok = ISC_FALSE;
  2558. } else if (isaddress) {
  2559. update_log(client, zone, ISC_LOG_WARNING,
  2560. "%s/MX: warning: '%s': %s",
  2561. ownerbuf, namebuf,
  2562. dns_result_totext(DNS_R_MXISADDRESS));
  2563. }
  2564. /*
  2565. * Check zone integrity checks.
  2566. */
  2567. if ((options & DNS_ZONEOPT_CHECKINTEGRITY) == 0)
  2568. continue;
  2569. result = dns_db_find(db, &mx.mx, newver, dns_rdatatype_a,
  2570. 0, 0, NULL, foundname, NULL, NULL);
  2571. if (result == ISC_R_SUCCESS)
  2572. continue;
  2573. if (result == DNS_R_NXRRSET) {
  2574. result = dns_db_find(db, &mx.mx, newver,
  2575. dns_rdatatype_aaaa,
  2576. 0, 0, NULL, foundname,
  2577. NULL, NULL);
  2578. if (result == ISC_R_SUCCESS)
  2579. continue;
  2580. }
  2581. if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN) {
  2582. update_log(client, zone, ISC_LOG_ERROR,
  2583. "%s/MX '%s' has no address records "
  2584. "(A or AAAA)", ownerbuf, namebuf);
  2585. ok = ISC_FALSE;
  2586. } else if (result == DNS_R_CNAME) {
  2587. update_log(client, zone, ISC_LOG_ERROR,
  2588. "%s/MX '%s' is a CNAME (illegal)",
  2589. ownerbuf, namebuf);
  2590. ok = ISC_FALSE;
  2591. } else if (result == DNS_R_DNAME) {
  2592. dns_name_format(foundname, altbuf, sizeof altbuf);
  2593. update_log(client, zone, ISC_LOG_ERROR,
  2594. "%s/MX '%s' is below a DNAME '%s' (illegal)",
  2595. ownerbuf, namebuf, altbuf);
  2596. ok = ISC_FALSE;
  2597. }
  2598. }
  2599. return (ok ? ISC_R_SUCCESS : DNS_R_REFUSED);
  2600. }
  2601. static isc_result_t
  2602. rr_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
  2603. const dns_rdata_t *rdata, isc_boolean_t *flag)
  2604. {
  2605. dns_rdataset_t rdataset;
  2606. dns_dbnode_t *node = NULL;
  2607. isc_result_t result;
  2608. dns_rdataset_init(&rdataset);
  2609. if (rdata->type == dns_rdatatype_nsec3)
  2610. CHECK(dns_db_findnsec3node(db, name, ISC_FALSE, &node));
  2611. else
  2612. CHECK(dns_db_findnode(db, name, ISC_FALSE, &node));
  2613. result = dns_db_findrdataset(db, node, ver, rdata->type, 0,
  2614. (isc_stdtime_t) 0, &rdataset, NULL);
  2615. if (result == ISC_R_NOTFOUND) {
  2616. *flag = ISC_FALSE;
  2617. result = ISC_R_SUCCESS;
  2618. goto failure;
  2619. }
  2620. for (result = dns_rdataset_first(&rdataset);
  2621. result == ISC_R_SUCCESS;
  2622. result = dns_rdataset_next(&rdataset)) {
  2623. dns_rdata_t myrdata = DNS_RDATA_INIT;
  2624. dns_rdataset_current(&rdataset, &myrdata);
  2625. if (!dns_rdata_casecompare(&myrdata, rdata))
  2626. break;
  2627. }
  2628. dns_rdataset_disassociate(&rdataset);
  2629. if (result == ISC_R_SUCCESS) {
  2630. *flag = ISC_TRUE;
  2631. } else if (result == ISC_R_NOMORE) {
  2632. *flag = ISC_FALSE;
  2633. result = ISC_R_SUCCESS;
  2634. }
  2635. failure:
  2636. if (node != NULL)
  2637. dns_db_detachnode(db, &node);
  2638. return (result);
  2639. }
  2640. static isc_result_t
  2641. get_iterations(dns_db_t *db, dns_dbversion_t *ver, dns_rdatatype_t privatetype,
  2642. unsigned int *iterationsp)
  2643. {
  2644. dns_dbnode_t *node = NULL;
  2645. dns_rdata_nsec3param_t nsec3param;
  2646. dns_rdataset_t rdataset;
  2647. isc_result_t result;
  2648. unsigned int iterations = 0;
  2649. dns_rdataset_init(&rdataset);
  2650. result = dns_db_getoriginnode(db, &node);
  2651. if (result != ISC_R_SUCCESS)
  2652. return (result);
  2653. result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec3param,
  2654. 0, (isc_stdtime_t) 0, &rdataset, NULL);
  2655. if (result == ISC_R_NOTFOUND)
  2656. goto try_private;
  2657. if (result != ISC_R_SUCCESS)
  2658. goto failure;
  2659. for (result = dns_rdataset_first(&rdataset);
  2660. result == ISC_R_SUCCESS;
  2661. result = dns_rdataset_next(&rdataset)) {
  2662. dns_rdata_t rdata = DNS_RDATA_INIT;
  2663. dns_rdataset_current(&rdataset, &rdata);
  2664. CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL));
  2665. if ((nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0)
  2666. continue;
  2667. if (nsec3param.iterations > iterations)
  2668. iterations = nsec3param.iterations;
  2669. }
  2670. if (result != ISC_R_NOMORE)
  2671. goto failure;
  2672. dns_rdataset_disassociate(&rdataset);
  2673. try_private:
  2674. if (privatetype == 0)
  2675. goto success;
  2676. result = dns_db_findrdataset(db, node, ver, privatetype,
  2677. 0, (isc_stdtime_t) 0, &rdataset, NULL);
  2678. if (result == ISC_R_NOTFOUND)
  2679. goto success;
  2680. if (result != ISC_R_SUCCESS)
  2681. goto failure;
  2682. for (result = dns_rdataset_first(&rdataset);
  2683. result == ISC_R_SUCCESS;
  2684. result = dns_rdataset_next(&rdataset)) {
  2685. unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
  2686. dns_rdata_t private = DNS_RDATA_INIT;
  2687. dns_rdata_t rdata = DNS_RDATA_INIT;
  2688. dns_rdataset_current(&rdataset, &rdata);
  2689. if (!dns_nsec3param_fromprivate(&private, &rdata,
  2690. buf, sizeof(buf)))
  2691. continue;
  2692. CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL));
  2693. if ((nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0)
  2694. continue;
  2695. if (nsec3param.iterations > iterations)
  2696. iterations = nsec3param.iterations;
  2697. }
  2698. if (result != ISC_R_NOMORE)
  2699. goto failure;
  2700. success:
  2701. *iterationsp = iterations;
  2702. result = ISC_R_SUCCESS;
  2703. failure:
  2704. if (node != NULL)
  2705. dns_db_detachnode(db, &node);
  2706. if (dns_rdataset_isassociated(&rdataset))
  2707. dns_rdataset_disassociate(&rdataset);
  2708. return (result);
  2709. }
  2710. /*
  2711. * Prevent the zone entering a inconsistent state where
  2712. * NSEC only DNSKEYs are present with NSEC3 chains.
  2713. */
  2714. static isc_result_t
  2715. check_dnssec(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
  2716. dns_dbversion_t *ver, dns_diff_t *diff)
  2717. {
  2718. dns_difftuple_t *tuple;
  2719. isc_boolean_t nseconly = ISC_FALSE, nsec3 = ISC_FALSE;
  2720. isc_result_t result;
  2721. unsigned int iterations = 0, max;
  2722. dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
  2723. /* Scan the tuples for an NSEC-only DNSKEY or an NSEC3PARAM */
  2724. for (tuple = ISC_LIST_HEAD(diff->tuples);
  2725. tuple != NULL;
  2726. tuple = ISC_LIST_NEXT(tuple, link)) {
  2727. if (tuple->op != DNS_DIFFOP_ADD)
  2728. continue;
  2729. if (tuple->rdata.type == dns_rdatatype_dnskey) {
  2730. isc_uint8_t alg;
  2731. alg = tuple->rdata.data[3];
  2732. if (alg == DST_ALG_RSAMD5 || alg == DST_ALG_RSASHA1 ||
  2733. alg == DST_ALG_DSA || alg == DST_ALG_ECC) {
  2734. nseconly = ISC_TRUE;
  2735. break;
  2736. }
  2737. } else if (tuple->rdata.type == dns_rdatatype_nsec3param) {
  2738. nsec3 = ISC_TRUE;
  2739. break;
  2740. }
  2741. }
  2742. /* Check existing DB for NSEC-only DNSKEY */
  2743. if (!nseconly)
  2744. CHECK(dns_nsec_nseconly(db, ver, &nseconly));
  2745. /* Check existing DB for NSEC3 */
  2746. if (!nsec3)
  2747. CHECK(dns_nsec3_activex(db, ver, ISC_FALSE,
  2748. privatetype, &nsec3));
  2749. /* Refuse to allow NSEC3 with NSEC-only keys */
  2750. if (nseconly && nsec3) {
  2751. update_log(client, zone, ISC_LOG_ERROR,
  2752. "NSEC only DNSKEYs and NSEC3 chains not allowed");
  2753. result = DNS_R_REFUSED;
  2754. goto failure;
  2755. }
  2756. /* Verify NSEC3 params */
  2757. CHECK(get_iterations(db, ver, privatetype, &iterations));
  2758. CHECK(dns_nsec3_maxiterations(db, ver, client->mctx, &max));
  2759. if (max != 0 && iterations > max) {
  2760. update_log(client, zone, ISC_LOG_ERROR,
  2761. "too many NSEC3 iterations (%u) for "
  2762. "weakest DNSKEY (%u)", iterations, max);
  2763. result = DNS_R_REFUSED;
  2764. goto failure;
  2765. }
  2766. failure:
  2767. return (result);
  2768. }
  2769. /*
  2770. * Delay NSEC3PARAM changes as they need to be applied to the whole zone.
  2771. */
  2772. static isc_result_t
  2773. add_nsec3param_records(ns_client_t *client, dns_zone_t *zone, dns_db_t *db,
  2774. dns_dbversion_t *ver, dns_diff_t *diff)
  2775. {
  2776. isc_result_t result = ISC_R_SUCCESS;
  2777. dns_difftuple_t *tuple, *newtuple = NULL, *next;
  2778. dns_rdata_t rdata = DNS_RDATA_INIT;
  2779. unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE + 1];
  2780. dns_diff_t temp_diff;
  2781. dns_diffop_t op;
  2782. isc_boolean_t flag;
  2783. dns_name_t *name = dns_zone_getorigin(zone);
  2784. dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
  2785. isc_uint32_t ttl = 0;
  2786. isc_boolean_t ttl_good = ISC_FALSE;
  2787. update_log(client, zone, ISC_LOG_DEBUG(3),
  2788. "checking for NSEC3PARAM changes");
  2789. dns_diff_init(diff->mctx, &temp_diff);
  2790. /*
  2791. * Extract NSEC3PARAM tuples from list.
  2792. */
  2793. for (tuple = ISC_LIST_HEAD(diff->tuples);
  2794. tuple != NULL;
  2795. tuple = next) {
  2796. next = ISC_LIST_NEXT(tuple, link);
  2797. if (tuple->rdata.type != dns_rdatatype_nsec3param ||
  2798. !dns_name_equal(name, &tuple->name))
  2799. continue;
  2800. ISC_LIST_UNLINK(diff->tuples, tuple, link);
  2801. ISC_LIST_APPEND(temp_diff.tuples, tuple, link);
  2802. }
  2803. /*
  2804. * Extract TTL changes pairs, we don't need to convert these to
  2805. * delayed changes.
  2806. */
  2807. for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
  2808. tuple != NULL; tuple = next) {
  2809. if (tuple->op == DNS_DIFFOP_ADD) {
  2810. if (!ttl_good) {
  2811. /*
  2812. * Any adds here will contain the final
  2813. * NSEC3PARAM RRset TTL.
  2814. */
  2815. ttl = tuple->ttl;
  2816. ttl_good = ISC_TRUE;
  2817. }
  2818. /*
  2819. * Walk the temp_diff list looking for the
  2820. * corresponding delete.
  2821. */
  2822. next = ISC_LIST_HEAD(temp_diff.tuples);
  2823. while (next != NULL) {
  2824. unsigned char *next_data = next->rdata.data;
  2825. unsigned char *tuple_data = tuple->rdata.data;
  2826. if (next->op == DNS_DIFFOP_DEL &&
  2827. next->rdata.length == tuple->rdata.length &&
  2828. !memcmp(next_data, tuple_data,
  2829. next->rdata.length)) {
  2830. ISC_LIST_UNLINK(temp_diff.tuples, next,
  2831. link);
  2832. ISC_LIST_APPEND(diff->tuples, next,
  2833. link);
  2834. break;
  2835. }
  2836. next = ISC_LIST_NEXT(next, link);
  2837. }
  2838. /*
  2839. * If we have not found a pair move onto the next
  2840. * tuple.
  2841. */
  2842. if (next == NULL) {
  2843. next = ISC_LIST_NEXT(tuple, link);
  2844. continue;
  2845. }
  2846. /*
  2847. * Find the next tuple to be processed before
  2848. * unlinking then complete moving the pair to 'diff'.
  2849. */
  2850. next = ISC_LIST_NEXT(tuple, link);
  2851. ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
  2852. ISC_LIST_APPEND(diff->tuples, tuple, link);
  2853. } else
  2854. next = ISC_LIST_NEXT(tuple, link);
  2855. }
  2856. /*
  2857. * Preserve any ongoing changes from a BIND 9.6.x upgrade.
  2858. *
  2859. * Any NSEC3PARAM records with flags other than OPTOUT named
  2860. * in managing and should not be touched so revert such changes
  2861. * taking into account any TTL change of the NSEC3PARAM RRset.
  2862. */
  2863. for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
  2864. tuple != NULL; tuple = next) {
  2865. next = ISC_LIST_NEXT(tuple, link);
  2866. if ((tuple->rdata.data[1] & ~DNS_NSEC3FLAG_OPTOUT) != 0) {
  2867. /*
  2868. * If we havn't had any adds then the tuple->ttl must
  2869. * be the original ttl and should be used for any
  2870. * future changes.
  2871. */
  2872. if (!ttl_good) {
  2873. ttl = tuple->ttl;
  2874. ttl_good = ISC_TRUE;
  2875. }
  2876. op = (tuple->op == DNS_DIFFOP_DEL) ?
  2877. DNS_DIFFOP_ADD : DNS_DIFFOP_DEL;
  2878. CHECK(dns_difftuple_create(diff->mctx, op, name,
  2879. ttl, &tuple->rdata,
  2880. &newtuple));
  2881. CHECK(do_one_tuple(&newtuple, db, ver, diff));
  2882. ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
  2883. dns_diff_appendminimal(diff, &tuple);
  2884. }
  2885. }
  2886. /*
  2887. * We now have just the actual changes to the NSEC3PARAM RRset.
  2888. * Convert the adds to delayed adds and the deletions into delayed
  2889. * deletions.
  2890. */
  2891. for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
  2892. tuple != NULL; tuple = next) {
  2893. /*
  2894. * If we havn't had any adds then the tuple->ttl must be the
  2895. * original ttl and should be used for any future changes.
  2896. */
  2897. if (!ttl_good) {
  2898. ttl = tuple->ttl;
  2899. ttl_good = ISC_TRUE;
  2900. }
  2901. if (tuple->op == DNS_DIFFOP_ADD) {
  2902. /*
  2903. * Look for any deletes which match this ADD ignoring
  2904. * OPTOUT. We don't need to explictly remove them as
  2905. * they will be removed a side effect of processing
  2906. * the add.
  2907. */
  2908. next = ISC_LIST_HEAD(temp_diff.tuples);
  2909. while (next != NULL) {
  2910. unsigned char *next_data = next->rdata.data;
  2911. unsigned char *tuple_data = tuple->rdata.data;
  2912. if (next->op != DNS_DIFFOP_DEL ||
  2913. next->rdata.length != tuple->rdata.length ||
  2914. next_data[0] != tuple_data[0] ||
  2915. next_data[2] != tuple_data[2] ||
  2916. next_data[3] != tuple_data[3] ||
  2917. memcmp(next_data + 4, tuple_data + 4,
  2918. tuple->rdata.length - 4)) {
  2919. next = ISC_LIST_NEXT(next, link);
  2920. continue;
  2921. }
  2922. ISC_LIST_UNLINK(temp_diff.tuples, next, link);
  2923. ISC_LIST_APPEND(diff->tuples, next, link);
  2924. next = ISC_LIST_HEAD(temp_diff.tuples);
  2925. }
  2926. /*
  2927. * See if we already have a CREATE request in progress.
  2928. */
  2929. dns_nsec3param_toprivate(&tuple->rdata, &rdata,
  2930. privatetype, buf, sizeof(buf));
  2931. buf[2] |= DNS_NSEC3FLAG_CREATE;
  2932. CHECK(rr_exists(db, ver, name, &rdata, &flag));
  2933. if (!flag) {
  2934. CHECK(dns_difftuple_create(diff->mctx,
  2935. DNS_DIFFOP_ADD,
  2936. name, 0, &rdata,
  2937. &newtuple));
  2938. CHECK(do_one_tuple(&newtuple, db, ver, diff));
  2939. }
  2940. /*
  2941. * Remove any existing CREATE request to add an
  2942. * otherwise indentical chain with a reversed
  2943. * OPTOUT state.
  2944. */
  2945. buf[2] ^= DNS_NSEC3FLAG_OPTOUT;
  2946. CHECK(rr_exists(db, ver, name, &rdata, &flag));
  2947. if (flag) {
  2948. CHECK(dns_difftuple_create(diff->mctx,
  2949. DNS_DIFFOP_DEL,
  2950. name, 0, &rdata,
  2951. &newtuple));
  2952. CHECK(do_one_tuple(&newtuple, db, ver, diff));
  2953. }
  2954. /*
  2955. * Find the next tuple to be processed and remove the
  2956. * temporary add record.
  2957. */
  2958. next = ISC_LIST_NEXT(tuple, link);
  2959. CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_DEL,
  2960. name, ttl, &tuple->rdata,
  2961. &newtuple));
  2962. CHECK(do_one_tuple(&newtuple, db, ver, diff));
  2963. ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
  2964. dns_diff_appendminimal(diff, &tuple);
  2965. dns_rdata_reset(&rdata);
  2966. } else
  2967. next = ISC_LIST_NEXT(tuple, link);
  2968. }
  2969. for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
  2970. tuple != NULL; tuple = next) {
  2971. INSIST(ttl_good);
  2972. next = ISC_LIST_NEXT(tuple, link);
  2973. /*
  2974. * See if we already have a REMOVE request in progress.
  2975. */
  2976. dns_nsec3param_toprivate(&tuple->rdata, &rdata, privatetype,
  2977. buf, sizeof(buf));
  2978. buf[2] |= DNS_NSEC3FLAG_REMOVE | DNS_NSEC3FLAG_NONSEC;
  2979. CHECK(rr_exists(db, ver, name, &rdata, &flag));
  2980. if (!flag) {
  2981. buf[2] &= ~DNS_NSEC3FLAG_NONSEC;
  2982. CHECK(rr_exists(db, ver, name, &rdata, &flag));
  2983. }
  2984. if (!flag) {
  2985. CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD,
  2986. name, 0, &rdata, &newtuple));
  2987. CHECK(do_one_tuple(&newtuple, db, ver, diff));
  2988. }
  2989. CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name,
  2990. ttl, &tuple->rdata, &newtuple));
  2991. CHECK(do_one_tuple(&newtuple, db, ver, diff));
  2992. ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
  2993. dns_diff_appendminimal(diff, &tuple);
  2994. dns_rdata_reset(&rdata);
  2995. }
  2996. result = ISC_R_SUCCESS;
  2997. failure:
  2998. dns_diff_clear(&temp_diff);
  2999. return (result);
  3000. }
  3001. static isc_result_t
  3002. rollback_private(dns_db_t *db, dns_rdatatype_t privatetype,
  3003. dns_dbversion_t *ver, dns_diff_t *diff)
  3004. {
  3005. dns_diff_t temp_diff;
  3006. dns_diffop_t op;
  3007. dns_difftuple_t *tuple, *newtuple = NULL, *next;
  3008. dns_name_t *name = dns_db_origin(db);
  3009. isc_mem_t *mctx = diff->mctx;
  3010. isc_result_t result;
  3011. if (privatetype == 0)
  3012. return (ISC_R_SUCCESS);
  3013. dns_diff_init(mctx, &temp_diff);
  3014. /*
  3015. * Extract the changes to be rolled back.
  3016. */
  3017. for (tuple = ISC_LIST_HEAD(diff->tuples);
  3018. tuple != NULL; tuple = next) {
  3019. next = ISC_LIST_NEXT(tuple, link);
  3020. if (tuple->rdata.type != privatetype ||
  3021. !dns_name_equal(name, &tuple->name))
  3022. continue;
  3023. /*
  3024. * Allow records which indicate that a zone has been
  3025. * signed with a DNSKEY to be be removed.
  3026. */
  3027. if (tuple->op == DNS_DIFFOP_DEL &&
  3028. tuple->rdata.length == 5 &&
  3029. tuple->rdata.data[0] != 0 &&
  3030. tuple->rdata.data[4] != 0)
  3031. continue;
  3032. ISC_LIST_UNLINK(diff->tuples, tuple, link);
  3033. ISC_LIST_PREPEND(temp_diff.tuples, tuple, link);
  3034. }
  3035. /*
  3036. * Rollback the changes.
  3037. */
  3038. while ((tuple = ISC_LIST_HEAD(temp_diff.tuples)) != NULL) {
  3039. op = (tuple->op == DNS_DIFFOP_DEL) ?
  3040. DNS_DIFFOP_ADD : DNS_DIFFOP_DEL;
  3041. CHECK(dns_difftuple_create(mctx, op, name, tuple->ttl,
  3042. &tuple->rdata, &newtuple));
  3043. CHECK(do_one_tuple(&newtuple, db, ver, &temp_diff));
  3044. }
  3045. result = ISC_R_SUCCESS;
  3046. failure:
  3047. dns_diff_clear(&temp_diff);
  3048. return (result);
  3049. }
  3050. /*
  3051. * Add records to cause the delayed signing of the zone by added DNSKEY
  3052. * to remove the RRSIG records generated by a deleted DNSKEY.
  3053. */
  3054. static isc_result_t
  3055. add_signing_records(dns_db_t *db, dns_rdatatype_t privatetype,
  3056. dns_dbversion_t *ver, dns_diff_t *diff)
  3057. {
  3058. dns_difftuple_t *tuple, *newtuple = NULL, *next;
  3059. dns_rdata_dnskey_t dnskey;
  3060. dns_rdata_t rdata = DNS_RDATA_INIT;
  3061. isc_boolean_t flag;
  3062. isc_region_t r;
  3063. isc_result_t result = ISC_R_SUCCESS;
  3064. isc_uint16_t keyid;
  3065. unsigned char buf[5];
  3066. dns_name_t *name = dns_db_origin(db);
  3067. dns_diff_t temp_diff;
  3068. dns_diff_init(diff->mctx, &temp_diff);
  3069. /*
  3070. * Extract the DNSKEY tuples from the list.
  3071. */
  3072. for (tuple = ISC_LIST_HEAD(diff->tuples);
  3073. tuple != NULL; tuple = next) {
  3074. next = ISC_LIST_NEXT(tuple, link);
  3075. if (tuple->rdata.type != dns_rdatatype_dnskey)
  3076. continue;
  3077. ISC_LIST_UNLINK(diff->tuples, tuple, link);
  3078. ISC_LIST_APPEND(temp_diff.tuples, tuple, link);
  3079. }
  3080. /*
  3081. * Extract TTL changes pairs, we don't need signing records for these.
  3082. */
  3083. for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
  3084. tuple != NULL; tuple = next) {
  3085. if (tuple->op == DNS_DIFFOP_ADD) {
  3086. /*
  3087. * Walk the temp_diff list looking for the
  3088. * corresponding delete.
  3089. */
  3090. next = ISC_LIST_HEAD(temp_diff.tuples);
  3091. while (next != NULL) {
  3092. unsigned char *next_data = next->rdata.data;
  3093. unsigned char *tuple_data = tuple->rdata.data;
  3094. if (next->op == DNS_DIFFOP_DEL &&
  3095. dns_name_equal(&tuple->name, &next->name) &&
  3096. next->rdata.length == tuple->rdata.length &&
  3097. !memcmp(next_data, tuple_data,
  3098. next->rdata.length)) {
  3099. ISC_LIST_UNLINK(temp_diff.tuples, next,
  3100. link);
  3101. ISC_LIST_APPEND(diff->tuples, next,
  3102. link);
  3103. break;
  3104. }
  3105. next = ISC_LIST_NEXT(next, link);
  3106. }
  3107. /*
  3108. * If we have not found a pair move onto the next
  3109. * tuple.
  3110. */
  3111. if (next == NULL) {
  3112. next = ISC_LIST_NEXT(tuple, link);
  3113. continue;
  3114. }
  3115. /*
  3116. * Find the next tuple to be processed before
  3117. * unlinking then complete moving the pair to 'diff'.
  3118. */
  3119. next = ISC_LIST_NEXT(tuple, link);
  3120. ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
  3121. ISC_LIST_APPEND(diff->tuples, tuple, link);
  3122. } else
  3123. next = ISC_LIST_NEXT(tuple, link);
  3124. }
  3125. /*
  3126. * Process the remaining DNSKEY entries.
  3127. */
  3128. for (tuple = ISC_LIST_HEAD(temp_diff.tuples);
  3129. tuple != NULL;
  3130. tuple = ISC_LIST_HEAD(temp_diff.tuples)) {
  3131. ISC_LIST_UNLINK(temp_diff.tuples, tuple, link);
  3132. ISC_LIST_APPEND(diff->tuples, tuple, link);
  3133. dns_rdata_tostruct(&tuple->rdata, &dnskey, NULL);
  3134. if ((dnskey.flags &
  3135. (DNS_KEYFLAG_OWNERMASK|DNS_KEYTYPE_NOAUTH))
  3136. != DNS_KEYOWNER_ZONE)
  3137. continue;
  3138. dns_rdata_toregion(&tuple->rdata, &r);
  3139. keyid = dst_region_computeid(&r, dnskey.algorithm);
  3140. buf[0] = dnskey.algorithm;
  3141. buf[1] = (keyid & 0xff00) >> 8;
  3142. buf[2] = (keyid & 0xff);
  3143. buf[3] = (tuple->op == DNS_DIFFOP_ADD) ? 0 : 1;
  3144. buf[4] = 0;
  3145. rdata.data = buf;
  3146. rdata.length = sizeof(buf);
  3147. rdata.type = privatetype;
  3148. rdata.rdclass = tuple->rdata.rdclass;
  3149. CHECK(rr_exists(db, ver, name, &rdata, &flag));
  3150. if (flag)
  3151. continue;
  3152. CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD,
  3153. name, 0, &rdata, &newtuple));
  3154. CHECK(do_one_tuple(&newtuple, db, ver, diff));
  3155. INSIST(newtuple == NULL);
  3156. /*
  3157. * Remove any record which says this operation has already
  3158. * completed.
  3159. */
  3160. buf[4] = 1;
  3161. CHECK(rr_exists(db, ver, name, &rdata, &flag));
  3162. if (flag) {
  3163. CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_DEL,
  3164. name, 0, &rdata, &newtuple));
  3165. CHECK(do_one_tuple(&newtuple, db, ver, diff));
  3166. INSIST(newtuple == NULL);
  3167. }
  3168. }
  3169. failure:
  3170. dns_diff_clear(&temp_diff);
  3171. return (result);
  3172. }
  3173. static isc_boolean_t
  3174. isdnssec(dns_db_t *db, dns_dbversion_t *ver, dns_rdatatype_t privatetype) {
  3175. isc_result_t result;
  3176. isc_boolean_t build_nsec, build_nsec3;
  3177. if (dns_db_issecure(db))
  3178. return (ISC_TRUE);
  3179. result = dns_private_chains(db, ver, privatetype,
  3180. &build_nsec, &build_nsec3);
  3181. RUNTIME_CHECK(result == ISC_R_SUCCESS);
  3182. return (build_nsec || build_nsec3);
  3183. }
  3184. static void
  3185. update_action(isc_task_t *task, isc_event_t *event) {
  3186. update_event_t *uev = (update_event_t *) event;
  3187. dns_zone_t *zone = uev->zone;
  3188. ns_client_t *client = (ns_client_t *)event->ev_arg;
  3189. isc_result_t result;
  3190. dns_db_t *db = NULL;
  3191. dns_dbversion_t *oldver = NULL;
  3192. dns_dbversion_t *ver = NULL;
  3193. dns_diff_t diff; /* Pending updates. */
  3194. dns_diff_t temp; /* Pending RR existence assertions. */
  3195. isc_boolean_t soa_serial_changed = ISC_FALSE;
  3196. isc_mem_t *mctx = client->mctx;
  3197. dns_rdatatype_t covers;
  3198. dns_message_t *request = client->message;
  3199. dns_rdataclass_t zoneclass;
  3200. dns_name_t *zonename;
  3201. dns_ssutable_t *ssutable = NULL;
  3202. dns_fixedname_t tmpnamefixed;
  3203. dns_name_t *tmpname = NULL;
  3204. unsigned int options;
  3205. dns_difftuple_t *tuple;
  3206. dns_rdata_dnskey_t dnskey;
  3207. isc_boolean_t had_dnskey;
  3208. dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
  3209. INSIST(event->ev_type == DNS_EVENT_UPDATE);
  3210. dns_diff_init(mctx, &diff);
  3211. dns_diff_init(mctx, &temp);
  3212. CHECK(dns_zone_getdb(zone, &db));
  3213. zonename = dns_db_origin(db);
  3214. zoneclass = dns_db_class(db);
  3215. dns_zone_getssutable(zone, &ssutable);
  3216. /*
  3217. * Update message processing can leak record existance information
  3218. * so check that we are allowed to query this zone. Additionally
  3219. * if we would refuse all updates for this zone we bail out here.
  3220. */
  3221. CHECK(checkqueryacl(client, dns_zone_getqueryacl(zone), zonename,
  3222. dns_zone_getupdateacl(zone), ssutable));
  3223. /*
  3224. * Get old and new versions now that queryacl has been checked.
  3225. */
  3226. dns_db_currentversion(db, &oldver);
  3227. CHECK(dns_db_newversion(db, &ver));
  3228. /*
  3229. * Check prerequisites.
  3230. */
  3231. for (result = dns_message_firstname(request, DNS_SECTION_PREREQUISITE);
  3232. result == ISC_R_SUCCESS;
  3233. result = dns_message_nextname(request, DNS_SECTION_PREREQUISITE))
  3234. {
  3235. dns_name_t *name = NULL;
  3236. dns_rdata_t rdata = DNS_RDATA_INIT;
  3237. dns_ttl_t ttl;
  3238. dns_rdataclass_t update_class;
  3239. isc_boolean_t flag;
  3240. get_current_rr(request, DNS_SECTION_PREREQUISITE, zoneclass,
  3241. &name, &rdata, &covers, &ttl, &update_class);
  3242. if (ttl != 0)
  3243. PREREQFAILC(DNS_R_FORMERR,
  3244. "prerequisite TTL is not zero");
  3245. if (! dns_name_issubdomain(name, zonename))
  3246. PREREQFAILN(DNS_R_NOTZONE, name,
  3247. "prerequisite name is out of zone");
  3248. if (update_class == dns_rdataclass_any) {
  3249. if (rdata.length != 0)
  3250. PREREQFAILC(DNS_R_FORMERR,
  3251. "class ANY prerequisite "
  3252. "RDATA is not empty");
  3253. if (rdata.type == dns_rdatatype_any) {
  3254. CHECK(name_exists(db, ver, name, &flag));
  3255. if (! flag) {
  3256. PREREQFAILN(DNS_R_NXDOMAIN, name,
  3257. "'name in use' "
  3258. "prerequisite not "
  3259. "satisfied");
  3260. }
  3261. } else {
  3262. CHECK(rrset_exists(db, ver, name,
  3263. rdata.type, covers, &flag));
  3264. if (! flag) {
  3265. /* RRset does not exist. */
  3266. PREREQFAILNT(DNS_R_NXRRSET, name, rdata.type,
  3267. "'rrset exists (value independent)' "
  3268. "prerequisite not satisfied");
  3269. }
  3270. }
  3271. } else if (update_class == dns_rdataclass_none) {
  3272. if (rdata.length != 0)
  3273. PREREQFAILC(DNS_R_FORMERR,
  3274. "class NONE prerequisite "
  3275. "RDATA is not empty");
  3276. if (rdata.type == dns_rdatatype_any) {
  3277. CHECK(name_exists(db, ver, name, &flag));
  3278. if (flag) {
  3279. PREREQFAILN(DNS_R_YXDOMAIN, name,
  3280. "'name not in use' "
  3281. "prerequisite not "
  3282. "satisfied");
  3283. }
  3284. } else {
  3285. CHECK(rrset_exists(db, ver, name,
  3286. rdata.type, covers, &flag));
  3287. if (flag) {
  3288. /* RRset exists. */
  3289. PREREQFAILNT(DNS_R_YXRRSET, name,
  3290. rdata.type,
  3291. "'rrset does not exist' "
  3292. "prerequisite not "
  3293. "satisfied");
  3294. }
  3295. }
  3296. } else if (update_class == zoneclass) {
  3297. /* "temp<rr.name, rr.type> += rr;" */
  3298. result = temp_append(&temp, name, &rdata);
  3299. if (result != ISC_R_SUCCESS) {
  3300. UNEXPECTED_ERROR(__FILE__, __LINE__,
  3301. "temp entry creation failed: %s",
  3302. dns_result_totext(result));
  3303. FAIL(ISC_R_UNEXPECTED);
  3304. }
  3305. } else {
  3306. PREREQFAILC(DNS_R_FORMERR, "malformed prerequisite");
  3307. }
  3308. }
  3309. if (result != ISC_R_NOMORE)
  3310. FAIL(result);
  3311. /*
  3312. * Perform the final check of the "rrset exists (value dependent)"
  3313. * prerequisites.
  3314. */
  3315. if (ISC_LIST_HEAD(temp.tuples) != NULL) {
  3316. dns_rdatatype_t type;
  3317. /*
  3318. * Sort the prerequisite records by owner name,
  3319. * type, and rdata.
  3320. */
  3321. result = dns_diff_sort(&temp, temp_order);
  3322. if (result != ISC_R_SUCCESS)
  3323. FAILC(result, "'RRset exists (value dependent)' "
  3324. "prerequisite not satisfied");
  3325. dns_fixedname_init(&tmpnamefixed);
  3326. tmpname = dns_fixedname_name(&tmpnamefixed);
  3327. result = temp_check(mctx, &temp, db, ver, tmpname, &type);
  3328. if (result != ISC_R_SUCCESS)
  3329. FAILNT(result, tmpname, type,
  3330. "'RRset exists (value dependent)' "
  3331. "prerequisite not satisfied");
  3332. }
  3333. update_log(client, zone, LOGLEVEL_DEBUG,
  3334. "prerequisites are OK");
  3335. /*
  3336. * Check Requestor's Permissions. It seems a bit silly to do this
  3337. * only after prerequisite testing, but that is what RFC2136 says.
  3338. */
  3339. if (ssutable == NULL)
  3340. CHECK(checkupdateacl(client, dns_zone_getupdateacl(zone),
  3341. "update", zonename, ISC_FALSE, ISC_FALSE));
  3342. else if (client->signer == NULL && !TCPCLIENT(client))
  3343. CHECK(checkupdateacl(client, NULL, "update", zonename,
  3344. ISC_FALSE, ISC_TRUE));
  3345. if (dns_zone_getupdatedisabled(zone))
  3346. FAILC(DNS_R_REFUSED, "dynamic update temporarily disabled "
  3347. "because the zone is frozen. Use "
  3348. "'rndc thaw' to re-enable updates.");
  3349. /*
  3350. * Perform the Update Section Prescan.
  3351. */
  3352. for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
  3353. result == ISC_R_SUCCESS;
  3354. result = dns_message_nextname(request, DNS_SECTION_UPDATE))
  3355. {
  3356. dns_name_t *name = NULL;
  3357. dns_rdata_t rdata = DNS_RDATA_INIT;
  3358. dns_ttl_t ttl;
  3359. dns_rdataclass_t update_class;
  3360. get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
  3361. &name, &rdata, &covers, &ttl, &update_class);
  3362. if (! dns_name_issubdomain(name, zonename))
  3363. FAILC(DNS_R_NOTZONE,
  3364. "update RR is outside zone");
  3365. if (update_class == zoneclass) {
  3366. /*
  3367. * Check for meta-RRs. The RFC2136 pseudocode says
  3368. * check for ANY|AXFR|MAILA|MAILB, but the text adds
  3369. * "or any other QUERY metatype"
  3370. */
  3371. if (dns_rdatatype_ismeta(rdata.type)) {
  3372. FAILC(DNS_R_FORMERR,
  3373. "meta-RR in update");
  3374. }
  3375. result = dns_zone_checknames(zone, name, &rdata);
  3376. if (result != ISC_R_SUCCESS)
  3377. FAIL(DNS_R_REFUSED);
  3378. } else if (update_class == dns_rdataclass_any) {
  3379. if (ttl != 0 || rdata.length != 0 ||
  3380. (dns_rdatatype_ismeta(rdata.type) &&
  3381. rdata.type != dns_rdatatype_any))
  3382. FAILC(DNS_R_FORMERR,
  3383. "meta-RR in update");
  3384. } else if (update_class == dns_rdataclass_none) {
  3385. if (ttl != 0 ||
  3386. dns_rdatatype_ismeta(rdata.type))
  3387. FAILC(DNS_R_FORMERR,
  3388. "meta-RR in update");
  3389. } else {
  3390. update_log(client, zone, ISC_LOG_WARNING,
  3391. "update RR has incorrect class %d",
  3392. update_class);
  3393. FAIL(DNS_R_FORMERR);
  3394. }
  3395. /*
  3396. * draft-ietf-dnsind-simple-secure-update-01 says
  3397. * "Unlike traditional dynamic update, the client
  3398. * is forbidden from updating NSEC records."
  3399. */
  3400. if (rdata.type == dns_rdatatype_nsec3) {
  3401. FAILC(DNS_R_REFUSED,
  3402. "explicit NSEC3 updates are not allowed "
  3403. "in secure zones");
  3404. } else if (rdata.type == dns_rdatatype_nsec) {
  3405. FAILC(DNS_R_REFUSED,
  3406. "explicit NSEC updates are not allowed "
  3407. "in secure zones");
  3408. } else if (rdata.type == dns_rdatatype_rrsig &&
  3409. !dns_name_equal(name, zonename)) {
  3410. FAILC(DNS_R_REFUSED,
  3411. "explicit RRSIG updates are currently "
  3412. "not supported in secure zones except "
  3413. "at the apex");
  3414. }
  3415. if (ssutable != NULL) {
  3416. isc_netaddr_t *tcpaddr, netaddr;
  3417. dst_key_t *tsigkey = NULL;
  3418. /*
  3419. * If this is a TCP connection then pass the
  3420. * address of the client through for tcp-self
  3421. * and 6to4-self otherwise pass NULL. This
  3422. * provides weak address based authentication.
  3423. */
  3424. if (TCPCLIENT(client)) {
  3425. isc_netaddr_fromsockaddr(&netaddr,
  3426. &client->peeraddr);
  3427. tcpaddr = &netaddr;
  3428. } else
  3429. tcpaddr = NULL;
  3430. if (client->message->tsigkey != NULL)
  3431. tsigkey = client->message->tsigkey->key;
  3432. if (rdata.type != dns_rdatatype_any) {
  3433. if (!dns_ssutable_checkrules(ssutable,
  3434. client->signer,
  3435. name, tcpaddr,
  3436. rdata.type,
  3437. tsigkey))
  3438. FAILC(DNS_R_REFUSED,
  3439. "rejected by secure update");
  3440. } else {
  3441. if (!ssu_checkall(db, ver, name, ssutable,
  3442. client->signer, tcpaddr,
  3443. tsigkey))
  3444. FAILC(DNS_R_REFUSED,
  3445. "rejected by secure update");
  3446. }
  3447. }
  3448. }
  3449. if (result != ISC_R_NOMORE)
  3450. FAIL(result);
  3451. update_log(client, zone, LOGLEVEL_DEBUG,
  3452. "update section prescan OK");
  3453. /*
  3454. * Process the Update Section.
  3455. */
  3456. options = dns_zone_getoptions(zone);
  3457. for (result = dns_message_firstname(request, DNS_SECTION_UPDATE);
  3458. result == ISC_R_SUCCESS;
  3459. result = dns_message_nextname(request, DNS_SECTION_UPDATE))
  3460. {
  3461. dns_name_t *name = NULL;
  3462. dns_rdata_t rdata = DNS_RDATA_INIT;
  3463. dns_ttl_t ttl;
  3464. dns_rdataclass_t update_class;
  3465. isc_boolean_t flag;
  3466. get_current_rr(request, DNS_SECTION_UPDATE, zoneclass,
  3467. &name, &rdata, &covers, &ttl, &update_class);
  3468. if (update_class == zoneclass) {
  3469. /*
  3470. * RFC1123 doesn't allow MF and MD in master zones. */
  3471. if (rdata.type == dns_rdatatype_md ||
  3472. rdata.type == dns_rdatatype_mf) {
  3473. char typebuf[DNS_RDATATYPE_FORMATSIZE];
  3474. dns_rdatatype_format(rdata.type, typebuf,
  3475. sizeof(typebuf));
  3476. update_log(client, zone, LOGLEVEL_PROTOCOL,
  3477. "attempt to add %s ignored",
  3478. typebuf);
  3479. continue;
  3480. }
  3481. if ((rdata.type == dns_rdatatype_ns ||
  3482. rdata.type == dns_rdatatype_dname) &&
  3483. dns_name_iswildcard(name)) {
  3484. char typebuf[DNS_RDATATYPE_FORMATSIZE];
  3485. dns_rdatatype_format(rdata.type, typebuf,
  3486. sizeof(typebuf));
  3487. update_log(client, zone,
  3488. LOGLEVEL_PROTOCOL,
  3489. "attempt to add wildcard %s record "
  3490. "ignored", typebuf);
  3491. continue;
  3492. }
  3493. if (rdata.type == dns_rdatatype_cname) {
  3494. CHECK(cname_incompatible_rrset_exists(db, ver,
  3495. name,
  3496. &flag));
  3497. if (flag) {
  3498. update_log(client, zone,
  3499. LOGLEVEL_PROTOCOL,
  3500. "attempt to add CNAME "
  3501. "alongside non-CNAME "
  3502. "ignored");
  3503. continue;
  3504. }
  3505. } else {
  3506. CHECK(rrset_exists(db, ver, name,
  3507. dns_rdatatype_cname, 0,
  3508. &flag));
  3509. if (flag &&
  3510. ! dns_rdatatype_isdnssec(rdata.type))
  3511. {
  3512. update_log(client, zone,
  3513. LOGLEVEL_PROTOCOL,
  3514. "attempt to add non-CNAME "
  3515. "alongside CNAME ignored");
  3516. continue;
  3517. }
  3518. }
  3519. if (rdata.type == dns_rdatatype_soa) {
  3520. isc_boolean_t ok;
  3521. CHECK(rrset_exists(db, ver, name,
  3522. dns_rdatatype_soa, 0,
  3523. &flag));
  3524. if (! flag) {
  3525. update_log(client, zone,
  3526. LOGLEVEL_PROTOCOL,
  3527. "attempt to create 2nd "
  3528. "SOA ignored");
  3529. continue;
  3530. }
  3531. CHECK(check_soa_increment(db, ver, &rdata,
  3532. &ok));
  3533. if (! ok) {
  3534. update_log(client, zone,
  3535. LOGLEVEL_PROTOCOL,
  3536. "SOA update failed to "
  3537. "increment serial, "
  3538. "ignoring it");
  3539. continue;
  3540. }
  3541. soa_serial_changed = ISC_TRUE;
  3542. }
  3543. if (rdata.type == privatetype) {
  3544. update_log(client, zone, LOGLEVEL_PROTOCOL,
  3545. "attempt to add a private type "
  3546. "(%u) record rejected internal "
  3547. "use only", privatetype);
  3548. continue;
  3549. }
  3550. if (rdata.type == dns_rdatatype_nsec3param) {
  3551. /*
  3552. * Ignore attempts to add NSEC3PARAM records
  3553. * with any flags other than OPTOUT.
  3554. */
  3555. if ((rdata.data[1] & ~DNS_NSEC3FLAG_OPTOUT) != 0) {
  3556. update_log(client, zone,
  3557. LOGLEVEL_PROTOCOL,
  3558. "attempt to add NSEC3PARAM "
  3559. "record with non OPTOUT "
  3560. "flag");
  3561. continue;
  3562. }
  3563. }
  3564. if ((options & DNS_ZONEOPT_CHECKWILDCARD) != 0 &&
  3565. dns_name_internalwildcard(name)) {
  3566. char namestr[DNS_NAME_FORMATSIZE];
  3567. dns_name_format(name, namestr,
  3568. sizeof(namestr));
  3569. update_log(client, zone, LOGLEVEL_PROTOCOL,
  3570. "warning: ownername '%s' contains "
  3571. "a non-terminal wildcard", namestr);
  3572. }
  3573. if (isc_log_wouldlog(ns_g_lctx, LOGLEVEL_PROTOCOL)) {
  3574. char namestr[DNS_NAME_FORMATSIZE];
  3575. char typestr[DNS_RDATATYPE_FORMATSIZE];
  3576. dns_name_format(name, namestr,
  3577. sizeof(namestr));
  3578. dns_rdatatype_format(rdata.type, typestr,
  3579. sizeof(typestr));
  3580. update_log(client, zone, LOGLEVEL_PROTOCOL,
  3581. "adding an RR at '%s' %s",
  3582. namestr, typestr);
  3583. }
  3584. /* Prepare the affected RRset for the addition. */
  3585. {
  3586. add_rr_prepare_ctx_t ctx;
  3587. ctx.db = db;
  3588. ctx.ver = ver;
  3589. ctx.diff = &diff;
  3590. ctx.name = name;
  3591. ctx.update_rr = &rdata;
  3592. ctx.update_rr_ttl = ttl;
  3593. ctx.ignore_add = ISC_FALSE;
  3594. dns_diff_init(mctx, &ctx.del_diff);
  3595. dns_diff_init(mctx, &ctx.add_diff);
  3596. CHECK(foreach_rr(db, ver, name, rdata.type,
  3597. covers, add_rr_prepare_action,
  3598. &ctx));
  3599. if (ctx.ignore_add) {
  3600. dns_diff_clear(&ctx.del_diff);
  3601. dns_diff_clear(&ctx.add_diff);
  3602. } else {
  3603. CHECK(do_diff(&ctx.del_diff, db, ver,
  3604. &diff));
  3605. CHECK(do_diff(&ctx.add_diff, db, ver,
  3606. &diff));
  3607. CHECK(update_one_rr(db, ver, &diff,
  3608. DNS_DIFFOP_ADD,
  3609. name, ttl, &rdata));
  3610. }
  3611. }
  3612. } else if (update_class == dns_rdataclass_any) {
  3613. if (rdata.type == dns_rdatatype_any) {
  3614. if (isc_log_wouldlog(ns_g_lctx,
  3615. LOGLEVEL_PROTOCOL))
  3616. {
  3617. char namestr[DNS_NAME_FORMATSIZE];
  3618. dns_name_format(name, namestr,
  3619. sizeof(namestr));
  3620. update_log(client, zone,
  3621. LOGLEVEL_PROTOCOL,
  3622. "delete all rrsets from "
  3623. "name '%s'", namestr);
  3624. }
  3625. if (dns_name_equal(name, zonename)) {
  3626. CHECK(delete_if(type_not_soa_nor_ns_p,
  3627. db, ver, name,
  3628. dns_rdatatype_any, 0,
  3629. &rdata, &diff));
  3630. } else {
  3631. CHECK(delete_if(type_not_dnssec,
  3632. db, ver, name,
  3633. dns_rdatatype_any, 0,
  3634. &rdata, &diff));
  3635. }
  3636. } else if (dns_name_equal(name, zonename) &&
  3637. (rdata.type == dns_rdatatype_soa ||
  3638. rdata.type == dns_rdatatype_ns)) {
  3639. update_log(client, zone, LOGLEVEL_PROTOCOL,
  3640. "attempt to delete all SOA "
  3641. "or NS records ignored");
  3642. continue;
  3643. } else {
  3644. if (isc_log_wouldlog(ns_g_lctx,
  3645. LOGLEVEL_PROTOCOL))
  3646. {
  3647. char namestr[DNS_NAME_FORMATSIZE];
  3648. char typestr[DNS_RDATATYPE_FORMATSIZE];
  3649. dns_name_format(name, namestr,
  3650. sizeof(namestr));
  3651. dns_rdatatype_format(rdata.type,
  3652. typestr,
  3653. sizeof(typestr));
  3654. update_log(client, zone,
  3655. LOGLEVEL_PROTOCOL,
  3656. "deleting rrset at '%s' %s",
  3657. namestr, typestr);
  3658. }
  3659. CHECK(delete_if(true_p, db, ver, name,
  3660. rdata.type, covers, &rdata,
  3661. &diff));
  3662. }
  3663. } else if (update_class == dns_rdataclass_none) {
  3664. char namestr[DNS_NAME_FORMATSIZE];
  3665. char typestr[DNS_RDATATYPE_FORMATSIZE];
  3666. /*
  3667. * The (name == zonename) condition appears in
  3668. * RFC2136 3.4.2.4 but is missing from the pseudocode.
  3669. */
  3670. if (dns_name_equal(name, zonename)) {
  3671. if (rdata.type == dns_rdatatype_soa) {
  3672. update_log(client, zone,
  3673. LOGLEVEL_PROTOCOL,
  3674. "attempt to delete SOA "
  3675. "ignored");
  3676. continue;
  3677. }
  3678. if (rdata.type == dns_rdatatype_ns) {
  3679. int count;
  3680. CHECK(rr_count(db, ver, name,
  3681. dns_rdatatype_ns,
  3682. 0, &count));
  3683. if (count == 1) {
  3684. update_log(client, zone,
  3685. LOGLEVEL_PROTOCOL,
  3686. "attempt to "
  3687. "delete last "
  3688. "NS ignored");
  3689. continue;
  3690. }
  3691. }
  3692. }
  3693. dns_name_format(name, namestr, sizeof(namestr));
  3694. dns_rdatatype_format(rdata.type, typestr,
  3695. sizeof(typestr));
  3696. update_log(client, zone, LOGLEVEL_PROTOCOL,
  3697. "deleting an RR at %s %s", namestr, typestr);
  3698. CHECK(delete_if(rr_equal_p, db, ver, name, rdata.type,
  3699. covers, &rdata, &diff));
  3700. }
  3701. }
  3702. if (result != ISC_R_NOMORE)
  3703. FAIL(result);
  3704. /*
  3705. * Check that any changes to DNSKEY/NSEC3PARAM records make sense.
  3706. * If they don't then back out all changes to DNSKEY/NSEC3PARAM
  3707. * records.
  3708. */
  3709. if (! ISC_LIST_EMPTY(diff.tuples))
  3710. CHECK(check_dnssec(client, zone, db, ver, &diff));
  3711. if (! ISC_LIST_EMPTY(diff.tuples)) {
  3712. unsigned int errors = 0;
  3713. CHECK(dns_zone_nscheck(zone, db, ver, &errors));
  3714. if (errors != 0) {
  3715. update_log(client, zone, LOGLEVEL_PROTOCOL,
  3716. "update rejected: post update name server "
  3717. "sanity check failed");
  3718. result = DNS_R_REFUSED;
  3719. goto failure;
  3720. }
  3721. }
  3722. /*
  3723. * If any changes were made, increment the SOA serial number,
  3724. * update RRSIGs and NSECs (if zone is secure), and write the update
  3725. * to the journal.
  3726. */
  3727. if (! ISC_LIST_EMPTY(diff.tuples)) {
  3728. char *journalfile;
  3729. dns_journal_t *journal;
  3730. isc_boolean_t has_dnskey;
  3731. /*
  3732. * Increment the SOA serial, but only if it was not
  3733. * changed as a result of an update operation.
  3734. */
  3735. if (! soa_serial_changed) {
  3736. CHECK(increment_soa_serial(db, ver, &diff, mctx));
  3737. }
  3738. CHECK(check_mx(client, zone, db, ver, &diff));
  3739. CHECK(remove_orphaned_ds(db, ver, &diff));
  3740. CHECK(rrset_exists(db, ver, zonename, dns_rdatatype_dnskey,
  3741. 0, &has_dnskey));
  3742. #define ALLOW_SECURE_TO_INSECURE(zone) \
  3743. ((dns_zone_getoptions(zone) & DNS_ZONEOPT_SECURETOINSECURE) != 0)
  3744. if (!ALLOW_SECURE_TO_INSECURE(zone)) {
  3745. CHECK(rrset_exists(db, oldver, zonename,
  3746. dns_rdatatype_dnskey, 0,
  3747. &had_dnskey));
  3748. if (had_dnskey && !has_dnskey) {
  3749. update_log(client, zone, LOGLEVEL_PROTOCOL,
  3750. "update rejected: all DNSKEY "
  3751. "records removed and "
  3752. "'dnssec-secure-to-insecure' "
  3753. "not set");
  3754. result = DNS_R_REFUSED;
  3755. goto failure;
  3756. }
  3757. }
  3758. CHECK(rollback_private(db, privatetype, ver, &diff));
  3759. CHECK(add_signing_records(db, privatetype, ver, &diff));
  3760. CHECK(add_nsec3param_records(client, zone, db, ver, &diff));
  3761. if (!has_dnskey) {
  3762. /*
  3763. * We are transitioning from secure to insecure.
  3764. * Cause all NSEC3 chains to be deleted. When the
  3765. * the last signature for the DNSKEY records are
  3766. * remove any NSEC chain present will also be removed.
  3767. */
  3768. CHECK(dns_nsec3param_deletechains(db, ver, zone,
  3769. &diff));
  3770. } else if (has_dnskey && isdnssec(db, ver, privatetype)) {
  3771. isc_uint32_t interval;
  3772. interval = dns_zone_getsigvalidityinterval(zone);
  3773. result = update_signatures(client, zone, db, oldver,
  3774. ver, &diff, interval);
  3775. if (result != ISC_R_SUCCESS) {
  3776. update_log(client, zone,
  3777. ISC_LOG_ERROR,
  3778. "RRSIG/NSEC/NSEC3 update failed: %s",
  3779. isc_result_totext(result));
  3780. goto failure;
  3781. }
  3782. }
  3783. journalfile = dns_zone_getjournal(zone);
  3784. if (journalfile != NULL) {
  3785. update_log(client, zone, LOGLEVEL_DEBUG,
  3786. "writing journal %s", journalfile);
  3787. journal = NULL;
  3788. result = dns_journal_open(mctx, journalfile,
  3789. ISC_TRUE, &journal);
  3790. if (result != ISC_R_SUCCESS)
  3791. FAILS(result, "journal open failed");
  3792. result = dns_journal_write_transaction(journal, &diff);
  3793. if (result != ISC_R_SUCCESS) {
  3794. dns_journal_destroy(&journal);
  3795. FAILS(result, "journal write failed");
  3796. }
  3797. dns_journal_destroy(&journal);
  3798. }
  3799. /*
  3800. * XXXRTH Just a note that this committing code will have
  3801. * to change to handle databases that need two-phase
  3802. * commit, but this isn't a priority.
  3803. */
  3804. update_log(client, zone, LOGLEVEL_DEBUG,
  3805. "committing update transaction");
  3806. dns_db_closeversion(db, &ver, ISC_TRUE);
  3807. /*
  3808. * Mark the zone as dirty so that it will be written to disk.
  3809. */
  3810. dns_zone_markdirty(zone);
  3811. /*
  3812. * Notify slaves of the change we just made.
  3813. */
  3814. dns_zone_notify(zone);
  3815. /*
  3816. * Cause the zone to be signed with the key that we
  3817. * have just added or have the corresponding signatures
  3818. * deleted.
  3819. *
  3820. * Note: we are already committed to this course of action.
  3821. */
  3822. for (tuple = ISC_LIST_HEAD(diff.tuples);
  3823. tuple != NULL;
  3824. tuple = ISC_LIST_NEXT(tuple, link)) {
  3825. isc_region_t r;
  3826. dns_secalg_t algorithm;
  3827. isc_uint16_t keyid;
  3828. if (tuple->rdata.type != dns_rdatatype_dnskey)
  3829. continue;
  3830. dns_rdata_tostruct(&tuple->rdata, &dnskey, NULL);
  3831. if ((dnskey.flags &
  3832. (DNS_KEYFLAG_OWNERMASK|DNS_KEYTYPE_NOAUTH))
  3833. != DNS_KEYOWNER_ZONE)
  3834. continue;
  3835. dns_rdata_toregion(&tuple->rdata, &r);
  3836. algorithm = dnskey.algorithm;
  3837. keyid = dst_region_computeid(&r, algorithm);
  3838. result = dns_zone_signwithkey(zone, algorithm, keyid,
  3839. ISC_TF(tuple->op == DNS_DIFFOP_DEL));
  3840. if (result != ISC_R_SUCCESS) {
  3841. update_log(client, zone, ISC_LOG_ERROR,
  3842. "dns_zone_signwithkey failed: %s",
  3843. dns_result_totext(result));
  3844. }
  3845. }
  3846. /*
  3847. * Cause the zone to add/delete NSEC3 chains for the
  3848. * deferred NSEC3PARAM changes.
  3849. *
  3850. * Note: we are already committed to this course of action.
  3851. */
  3852. for (tuple = ISC_LIST_HEAD(diff.tuples);
  3853. tuple != NULL;
  3854. tuple = ISC_LIST_NEXT(tuple, link)) {
  3855. unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
  3856. dns_rdata_t rdata = DNS_RDATA_INIT;
  3857. dns_rdata_nsec3param_t nsec3param;
  3858. if (tuple->rdata.type != privatetype ||
  3859. tuple->op != DNS_DIFFOP_ADD)
  3860. continue;
  3861. if (!dns_nsec3param_fromprivate(&tuple->rdata, &rdata,
  3862. buf, sizeof(buf)))
  3863. continue;
  3864. dns_rdata_tostruct(&rdata, &nsec3param, NULL);
  3865. if (nsec3param.flags == 0)
  3866. continue;
  3867. result = dns_zone_addnsec3chain(zone, &nsec3param);
  3868. if (result != ISC_R_SUCCESS) {
  3869. update_log(client, zone, ISC_LOG_ERROR,
  3870. "dns_zone_addnsec3chain failed: %s",
  3871. dns_result_totext(result));
  3872. }
  3873. }
  3874. } else {
  3875. update_log(client, zone, LOGLEVEL_DEBUG, "redundant request");
  3876. dns_db_closeversion(db, &ver, ISC_TRUE);
  3877. }
  3878. result = ISC_R_SUCCESS;
  3879. goto common;
  3880. failure:
  3881. /*
  3882. * The reason for failure should have been logged at this point.
  3883. */
  3884. if (ver != NULL) {
  3885. update_log(client, zone, LOGLEVEL_DEBUG,
  3886. "rolling back");
  3887. dns_db_closeversion(db, &ver, ISC_FALSE);
  3888. }
  3889. common:
  3890. dns_diff_clear(&temp);
  3891. dns_diff_clear(&diff);
  3892. if (oldver != NULL)
  3893. dns_db_closeversion(db, &oldver, ISC_FALSE);
  3894. if (db != NULL)
  3895. dns_db_detach(&db);
  3896. if (ssutable != NULL)
  3897. dns_ssutable_detach(&ssutable);
  3898. isc_task_detach(&task);
  3899. uev->result = result;
  3900. if (zone != NULL)
  3901. INSIST(uev->zone == zone); /* we use this later */
  3902. uev->ev_type = DNS_EVENT_UPDATEDONE;
  3903. uev->ev_action = updatedone_action;
  3904. isc_task_send(client->task, &event);
  3905. INSIST(event == NULL);
  3906. }
  3907. static void
  3908. updatedone_action(isc_task_t *task, isc_event_t *event) {
  3909. update_event_t *uev = (update_event_t *) event;
  3910. ns_client_t *client = (ns_client_t *) event->ev_arg;
  3911. UNUSED(task);
  3912. INSIST(event->ev_type == DNS_EVENT_UPDATEDONE);
  3913. INSIST(task == client->task);
  3914. INSIST(client->nupdates > 0);
  3915. switch (uev->result) {
  3916. case ISC_R_SUCCESS:
  3917. inc_stats(uev->zone, dns_nsstatscounter_updatedone);
  3918. break;
  3919. case DNS_R_REFUSED:
  3920. inc_stats(uev->zone, dns_nsstatscounter_updaterej);
  3921. break;
  3922. default:
  3923. inc_stats(uev->zone, dns_nsstatscounter_updatefail);
  3924. break;
  3925. }
  3926. if (uev->zone != NULL)
  3927. dns_zone_detach(&uev->zone);
  3928. client->nupdates--;
  3929. respond(client, uev->result);
  3930. isc_event_free(&event);
  3931. ns_client_detach(&client);
  3932. }
  3933. /*%
  3934. * Update forwarding support.
  3935. */
  3936. static void
  3937. forward_fail(isc_task_t *task, isc_event_t *event) {
  3938. ns_client_t *client = (ns_client_t *)event->ev_arg;
  3939. UNUSED(task);
  3940. INSIST(client->nupdates > 0);
  3941. client->nupdates--;
  3942. respond(client, DNS_R_SERVFAIL);
  3943. isc_event_free(&event);
  3944. ns_client_detach(&client);
  3945. }
  3946. static void
  3947. forward_callback(void *arg, isc_result_t result, dns_message_t *answer) {
  3948. update_event_t *uev = arg;
  3949. ns_client_t *client = uev->ev_arg;
  3950. dns_zone_t *zone = uev->zone;
  3951. if (result != ISC_R_SUCCESS) {
  3952. INSIST(answer == NULL);
  3953. uev->ev_type = DNS_EVENT_UPDATEDONE;
  3954. uev->ev_action = forward_fail;
  3955. inc_stats(zone, dns_nsstatscounter_updatefwdfail);
  3956. } else {
  3957. uev->ev_type = DNS_EVENT_UPDATEDONE;
  3958. uev->ev_action = forward_done;
  3959. uev->answer = answer;
  3960. inc_stats(zone, dns_nsstatscounter_updaterespfwd);
  3961. }
  3962. isc_task_send(client->task, ISC_EVENT_PTR(&uev));
  3963. dns_zone_detach(&zone);
  3964. }
  3965. static void
  3966. forward_done(isc_task_t *task, isc_event_t *event) {
  3967. update_event_t *uev = (update_event_t *) event;
  3968. ns_client_t *client = (ns_client_t *)event->ev_arg;
  3969. UNUSED(task);
  3970. INSIST(client->nupdates > 0);
  3971. client->nupdates--;
  3972. ns_client_sendraw(client, uev->answer);
  3973. dns_message_destroy(&uev->answer);
  3974. isc_event_free(&event);
  3975. ns_client_detach(&client);
  3976. }
  3977. static void
  3978. forward_action(isc_task_t *task, isc_event_t *event) {
  3979. update_event_t *uev = (update_event_t *) event;
  3980. dns_zone_t *zone = uev->zone;
  3981. ns_client_t *client = (ns_client_t *)event->ev_arg;
  3982. isc_result_t result;
  3983. result = dns_zone_forwardupdate(zone, client->message,
  3984. forward_callback, event);
  3985. if (result != ISC_R_SUCCESS) {
  3986. uev->ev_type = DNS_EVENT_UPDATEDONE;
  3987. uev->ev_action = forward_fail;
  3988. isc_task_send(client->task, &event);
  3989. inc_stats(zone, dns_nsstatscounter_updatefwdfail);
  3990. dns_zone_detach(&zone);
  3991. } else
  3992. inc_stats(zone, dns_nsstatscounter_updatereqfwd);
  3993. isc_task_detach(&task);
  3994. }
  3995. static isc_result_t
  3996. send_forward_event(ns_client_t *client, dns_zone_t *zone) {
  3997. isc_result_t result = ISC_R_SUCCESS;
  3998. update_event_t *event = NULL;
  3999. isc_task_t *zonetask = NULL;
  4000. ns_client_t *evclient;
  4001. /*
  4002. * This may take some time so replace this client.
  4003. */
  4004. if (!client->mortal && (client->attributes & NS_CLIENTATTR_TCP) == 0)
  4005. CHECK(ns_client_replace(client));
  4006. event = (update_event_t *)
  4007. isc_event_allocate(client->mctx, client, DNS_EVENT_UPDATE,
  4008. forward_action, NULL, sizeof(*event));
  4009. if (event == NULL)
  4010. FAIL(ISC_R_NOMEMORY);
  4011. event->zone = zone;
  4012. event->result = ISC_R_SUCCESS;
  4013. evclient = NULL;
  4014. ns_client_attach(client, &evclient);
  4015. INSIST(client->nupdates == 0);
  4016. client->nupdates++;
  4017. event->ev_arg = evclient;
  4018. dns_zone_gettask(zone, &zonetask);
  4019. isc_task_send(zonetask, ISC_EVENT_PTR(&event));
  4020. failure:
  4021. if (event != NULL)
  4022. isc_event_free(ISC_EVENT_PTR(&event));
  4023. return (result);
  4024. }