1 /*
2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * SPDX-License-Identifier: MPL-2.0
5  *
6  * This Source Code Form is subject to the terms of the Mozilla Public
7  * License, v. 2.0. If a copy of the MPL was not distributed with this
8  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9  *
10  * See the COPYRIGHT file distributed with this work for additional
11  * information regarding copyright ownership.
12  */
13 
14 /*! \file */
15 
16 #include <errno.h>
17 #include <inttypes.h>
18 #include <stdbool.h>
19 
20 #include <isc/atomic.h>
21 #include <isc/file.h>
22 #include <isc/hex.h>
23 #include <isc/md.h>
24 #include <isc/mutex.h>
25 #include <isc/pool.h>
26 #include <isc/print.h>
27 #include <isc/random.h>
28 #include <isc/ratelimiter.h>
29 #include <isc/refcount.h>
30 #include <isc/rwlock.h>
31 #include <isc/serial.h>
32 #include <isc/stats.h>
33 #include <isc/stdtime.h>
34 #include <isc/strerr.h>
35 #include <isc/string.h>
36 #include <isc/taskpool.h>
37 #include <isc/thread.h>
38 #include <isc/timer.h>
39 #include <isc/util.h>
40 
41 #include <dns/acl.h>
42 #include <dns/adb.h>
43 #include <dns/callbacks.h>
44 #include <dns/catz.h>
45 #include <dns/db.h>
46 #include <dns/dbiterator.h>
47 #include <dns/dlz.h>
48 #include <dns/dnssec.h>
49 #include <dns/events.h>
50 #include <dns/journal.h>
51 #include <dns/kasp.h>
52 #include <dns/keydata.h>
53 #include <dns/keymgr.h>
54 #include <dns/keytable.h>
55 #include <dns/keyvalues.h>
56 #include <dns/log.h>
57 #include <dns/master.h>
58 #include <dns/masterdump.h>
59 #include <dns/message.h>
60 #include <dns/name.h>
61 #include <dns/nsec.h>
62 #include <dns/nsec3.h>
63 #include <dns/opcode.h>
64 #include <dns/peer.h>
65 #include <dns/private.h>
66 #include <dns/rcode.h>
67 #include <dns/rdata.h>
68 #include <dns/rdataclass.h>
69 #include <dns/rdatalist.h>
70 #include <dns/rdataset.h>
71 #include <dns/rdatasetiter.h>
72 #include <dns/rdatastruct.h>
73 #include <dns/rdatatype.h>
74 #include <dns/request.h>
75 #include <dns/resolver.h>
76 #include <dns/result.h>
77 #include <dns/rriterator.h>
78 #include <dns/soa.h>
79 #include <dns/ssu.h>
80 #include <dns/stats.h>
81 #include <dns/time.h>
82 #include <dns/tsig.h>
83 #include <dns/update.h>
84 #include <dns/xfrin.h>
85 #include <dns/zone.h>
86 #include <dns/zoneverify.h>
87 #include <dns/zt.h>
88 
89 #include <dst/dst.h>
90 
91 #include "zone_p.h"
92 
93 #define ZONE_MAGIC	     ISC_MAGIC('Z', 'O', 'N', 'E')
94 #define DNS_ZONE_VALID(zone) ISC_MAGIC_VALID(zone, ZONE_MAGIC)
95 
96 #define NOTIFY_MAGIC		 ISC_MAGIC('N', 't', 'f', 'y')
97 #define DNS_NOTIFY_VALID(notify) ISC_MAGIC_VALID(notify, NOTIFY_MAGIC)
98 
99 #define CHECKDS_MAGIC		   ISC_MAGIC('C', 'h', 'D', 'S')
100 #define DNS_CHECKDS_VALID(checkds) ISC_MAGIC_VALID(checkds, CHECKDS_MAGIC)
101 
102 #define STUB_MAGIC	     ISC_MAGIC('S', 't', 'u', 'b')
103 #define DNS_STUB_VALID(stub) ISC_MAGIC_VALID(stub, STUB_MAGIC)
104 
105 #define ZONEMGR_MAGIC		ISC_MAGIC('Z', 'm', 'g', 'r')
106 #define DNS_ZONEMGR_VALID(stub) ISC_MAGIC_VALID(stub, ZONEMGR_MAGIC)
107 
108 #define LOAD_MAGIC	     ISC_MAGIC('L', 'o', 'a', 'd')
109 #define DNS_LOAD_VALID(load) ISC_MAGIC_VALID(load, LOAD_MAGIC)
110 
111 #define FORWARD_MAGIC		ISC_MAGIC('F', 'o', 'r', 'w')
112 #define DNS_FORWARD_VALID(load) ISC_MAGIC_VALID(load, FORWARD_MAGIC)
113 
114 #define IO_MAGIC	   ISC_MAGIC('Z', 'm', 'I', 'O')
115 #define DNS_IO_VALID(load) ISC_MAGIC_VALID(load, IO_MAGIC)
116 
117 #define KEYMGMT_MAGIC		ISC_MAGIC('M', 'g', 'm', 't')
118 #define DNS_KEYMGMT_VALID(load) ISC_MAGIC_VALID(load, KEYMGMT_MAGIC)
119 
120 /*%
121  * Ensure 'a' is at least 'min' but not more than 'max'.
122  */
123 #define RANGE(a, min, max) (((a) < (min)) ? (min) : ((a) < (max) ? (a) : (max)))
124 
125 #define NSEC3REMOVE(x) (((x)&DNS_NSEC3FLAG_REMOVE) != 0)
126 
127 /*%
128  * Key flags
129  */
130 #define REVOKE(x) ((dst_key_flags(x) & DNS_KEYFLAG_REVOKE) != 0)
131 #define KSK(x)	  ((dst_key_flags(x) & DNS_KEYFLAG_KSK) != 0)
132 #define ID(x)	  dst_key_id(x)
133 #define ALG(x)	  dst_key_alg(x)
134 
135 /*%
136  * KASP flags
137  */
138 #define KASP_LOCK(k)                  \
139 	if ((k) != NULL) {            \
140 		LOCK((&((k)->lock))); \
141 	}
142 
143 #define KASP_UNLOCK(k)                  \
144 	if ((k) != NULL) {              \
145 		UNLOCK((&((k)->lock))); \
146 	}
147 
148 /*
149  * Default values.
150  */
151 #define DNS_DEFAULT_IDLEIN  3600       /*%< 1 hour */
152 #define DNS_DEFAULT_IDLEOUT 3600       /*%< 1 hour */
153 #define MAX_XFER_TIME	    (2 * 3600) /*%< Documented default is 2 hours */
154 #define RESIGN_DELAY	    3600       /*%< 1 hour */
155 
156 #ifndef DNS_MAX_EXPIRE
157 #define DNS_MAX_EXPIRE 14515200 /*%< 24 weeks */
158 #endif				/* ifndef DNS_MAX_EXPIRE */
159 
160 #ifndef DNS_DUMP_DELAY
161 #define DNS_DUMP_DELAY 900 /*%< 15 minutes */
162 #endif			   /* ifndef DNS_DUMP_DELAY */
163 
164 typedef struct dns_notify dns_notify_t;
165 typedef struct dns_checkds dns_checkds_t;
166 typedef struct dns_stub dns_stub_t;
167 typedef struct dns_load dns_load_t;
168 typedef struct dns_forward dns_forward_t;
169 typedef ISC_LIST(dns_forward_t) dns_forwardlist_t;
170 typedef struct dns_io dns_io_t;
171 typedef ISC_LIST(dns_io_t) dns_iolist_t;
172 typedef struct dns_keymgmt dns_keymgmt_t;
173 typedef struct dns_signing dns_signing_t;
174 typedef ISC_LIST(dns_signing_t) dns_signinglist_t;
175 typedef struct dns_nsec3chain dns_nsec3chain_t;
176 typedef ISC_LIST(dns_nsec3chain_t) dns_nsec3chainlist_t;
177 typedef struct dns_keyfetch dns_keyfetch_t;
178 typedef struct dns_asyncload dns_asyncload_t;
179 typedef struct dns_include dns_include_t;
180 
181 #define DNS_ZONE_CHECKLOCK
182 #ifdef DNS_ZONE_CHECKLOCK
183 #define LOCK_ZONE(z)                  \
184 	do {                          \
185 		LOCK(&(z)->lock);     \
186 		INSIST(!(z)->locked); \
187 		(z)->locked = true;   \
188 	} while (0)
189 #define UNLOCK_ZONE(z)               \
190 	do {                         \
191 		(z)->locked = false; \
192 		UNLOCK(&(z)->lock);  \
193 	} while (0)
194 #define LOCKED_ZONE(z) ((z)->locked)
195 #define TRYLOCK_ZONE(result, z)                         \
196 	do {                                            \
197 		result = isc_mutex_trylock(&(z)->lock); \
198 		if (result == ISC_R_SUCCESS) {          \
199 			INSIST(!(z)->locked);           \
200 			(z)->locked = true;             \
201 		}                                       \
202 	} while (0)
203 #else /* ifdef DNS_ZONE_CHECKLOCK */
204 #define LOCK_ZONE(z)   LOCK(&(z)->lock)
205 #define UNLOCK_ZONE(z) UNLOCK(&(z)->lock)
206 #define LOCKED_ZONE(z) true
207 #define TRYLOCK_ZONE(result, z)                         \
208 	do {                                            \
209 		result = isc_mutex_trylock(&(z)->lock); \
210 	} while (0)
211 #endif /* ifdef DNS_ZONE_CHECKLOCK */
212 
213 #define ZONEDB_INITLOCK(l)    isc_rwlock_init((l), 0, 0)
214 #define ZONEDB_DESTROYLOCK(l) isc_rwlock_destroy(l)
215 #define ZONEDB_LOCK(l, t)     RWLOCK((l), (t))
216 #define ZONEDB_UNLOCK(l, t)   RWUNLOCK((l), (t))
217 
218 #ifdef ENABLE_AFL
219 extern bool dns_fuzzing_resolver;
220 #endif /* ifdef ENABLE_AFL */
221 
222 struct dns_zone {
223 	/* Unlocked */
224 	unsigned int magic;
225 	isc_mutex_t lock;
226 #ifdef DNS_ZONE_CHECKLOCK
227 	bool locked;
228 #endif /* ifdef DNS_ZONE_CHECKLOCK */
229 	isc_mem_t *mctx;
230 	isc_refcount_t erefs;
231 
232 	isc_rwlock_t dblock;
233 	dns_db_t *db; /* Locked by dblock */
234 
235 	/* Locked */
236 	dns_zonemgr_t *zmgr;
237 	ISC_LINK(dns_zone_t) link; /* Used by zmgr. */
238 	isc_timer_t *timer;
239 	isc_refcount_t irefs;
240 	dns_name_t origin;
241 	char *masterfile;
242 	ISC_LIST(dns_include_t) includes;    /* Include files */
243 	ISC_LIST(dns_include_t) newincludes; /* Loading */
244 	unsigned int nincludes;
245 	dns_masterformat_t masterformat;
246 	const dns_master_style_t *masterstyle;
247 	char *journal;
248 	int32_t journalsize;
249 	dns_rdataclass_t rdclass;
250 	dns_zonetype_t type;
251 	atomic_uint_fast64_t flags;
252 	atomic_uint_fast64_t options;
253 	unsigned int db_argc;
254 	char **db_argv;
255 	isc_time_t expiretime;
256 	isc_time_t refreshtime;
257 	isc_time_t dumptime;
258 	isc_time_t loadtime;
259 	isc_time_t notifytime;
260 	isc_time_t resigntime;
261 	isc_time_t keywarntime;
262 	isc_time_t signingtime;
263 	isc_time_t nsec3chaintime;
264 	isc_time_t refreshkeytime;
265 	uint32_t refreshkeyinterval;
266 	uint32_t refreshkeycount;
267 	uint32_t refresh;
268 	uint32_t retry;
269 	uint32_t expire;
270 	uint32_t minimum;
271 	isc_stdtime_t key_expiry;
272 	isc_stdtime_t log_key_expired_timer;
273 	char *keydirectory;
274 
275 	uint32_t maxrefresh;
276 	uint32_t minrefresh;
277 	uint32_t maxretry;
278 	uint32_t minretry;
279 
280 	uint32_t maxrecords;
281 
282 	isc_sockaddr_t *masters;
283 	isc_dscp_t *masterdscps;
284 	dns_name_t **masterkeynames;
285 	bool *mastersok;
286 	unsigned int masterscnt;
287 	unsigned int curmaster;
288 	isc_sockaddr_t masteraddr;
289 
290 	isc_sockaddr_t *parentals;
291 	isc_dscp_t *parentaldscps;
292 	dns_name_t **parentalkeynames;
293 	dns_dnsseckeylist_t checkds_ok;
294 	unsigned int parentalscnt;
295 	isc_sockaddr_t parentaladdr;
296 
297 	dns_notifytype_t notifytype;
298 	isc_sockaddr_t *notify;
299 	dns_name_t **notifykeynames;
300 	isc_dscp_t *notifydscp;
301 	unsigned int notifycnt;
302 	isc_sockaddr_t notifyfrom;
303 	isc_task_t *task;
304 	isc_task_t *loadtask;
305 	isc_sockaddr_t notifysrc4;
306 	isc_sockaddr_t notifysrc6;
307 	isc_sockaddr_t parentalsrc4;
308 	isc_sockaddr_t parentalsrc6;
309 	isc_sockaddr_t xfrsource4;
310 	isc_sockaddr_t xfrsource6;
311 	isc_sockaddr_t altxfrsource4;
312 	isc_sockaddr_t altxfrsource6;
313 	isc_sockaddr_t sourceaddr;
314 	isc_dscp_t notifysrc4dscp;
315 	isc_dscp_t notifysrc6dscp;
316 	isc_dscp_t parentalsrc4dscp;
317 	isc_dscp_t parentalsrc6dscp;
318 	isc_dscp_t xfrsource4dscp;
319 	isc_dscp_t xfrsource6dscp;
320 	isc_dscp_t altxfrsource4dscp;
321 	isc_dscp_t altxfrsource6dscp;
322 	dns_xfrin_ctx_t *xfr;	/* task locked */
323 	dns_tsigkey_t *tsigkey; /* key used for xfr */
324 	/* Access Control Lists */
325 	dns_acl_t *update_acl;
326 	dns_acl_t *forward_acl;
327 	dns_acl_t *notify_acl;
328 	dns_acl_t *query_acl;
329 	dns_acl_t *queryon_acl;
330 	dns_acl_t *xfr_acl;
331 	bool update_disabled;
332 	bool zero_no_soa_ttl;
333 	dns_severity_t check_names;
334 	ISC_LIST(dns_notify_t) notifies;
335 	ISC_LIST(dns_checkds_t) checkds_requests;
336 	dns_request_t *request;
337 	dns_loadctx_t *lctx;
338 	dns_io_t *readio;
339 	dns_dumpctx_t *dctx;
340 	dns_io_t *writeio;
341 	uint32_t maxxfrin;
342 	uint32_t maxxfrout;
343 	uint32_t idlein;
344 	uint32_t idleout;
345 	isc_event_t ctlevent;
346 	dns_ssutable_t *ssutable;
347 	uint32_t sigvalidityinterval;
348 	uint32_t keyvalidityinterval;
349 	uint32_t sigresigninginterval;
350 	dns_view_t *view;
351 	dns_view_t *prev_view;
352 	dns_kasp_t *kasp;
353 	dns_checkmxfunc_t checkmx;
354 	dns_checksrvfunc_t checksrv;
355 	dns_checknsfunc_t checkns;
356 	/*%
357 	 * Zones in certain states such as "waiting for zone transfer"
358 	 * or "zone transfer in progress" are kept on per-state linked lists
359 	 * in the zone manager using the 'statelink' field.  The 'statelist'
360 	 * field points at the list the zone is currently on.  It the zone
361 	 * is not on any such list, statelist is NULL.
362 	 */
363 	ISC_LINK(dns_zone_t) statelink;
364 	dns_zonelist_t *statelist;
365 	/*%
366 	 * Statistics counters about zone management.
367 	 */
368 	isc_stats_t *stats;
369 	/*%
370 	 * Optional per-zone statistics counters.  Counted outside of this
371 	 * module.
372 	 */
373 	dns_zonestat_level_t statlevel;
374 	bool requeststats_on;
375 	isc_stats_t *requeststats;
376 	dns_stats_t *rcvquerystats;
377 	dns_stats_t *dnssecsignstats;
378 	uint32_t notifydelay;
379 	dns_isselffunc_t isself;
380 	void *isselfarg;
381 
382 	char *strnamerd;
383 	char *strname;
384 	char *strrdclass;
385 	char *strviewname;
386 
387 	/*%
388 	 * Serial number for deferred journal compaction.
389 	 */
390 	uint32_t compact_serial;
391 	/*%
392 	 * Keys that are signing the zone for the first time.
393 	 */
394 	dns_signinglist_t signing;
395 	dns_nsec3chainlist_t nsec3chain;
396 	/*%
397 	 * List of outstanding NSEC3PARAM change requests.
398 	 */
399 	isc_eventlist_t setnsec3param_queue;
400 	/*%
401 	 * Signing / re-signing quantum stopping parameters.
402 	 */
403 	uint32_t signatures;
404 	uint32_t nodes;
405 	dns_rdatatype_t privatetype;
406 
407 	/*%
408 	 * Autosigning/key-maintenance options
409 	 */
410 	atomic_uint_fast64_t keyopts;
411 
412 	/*%
413 	 * True if added by "rndc addzone"
414 	 */
415 	bool added;
416 
417 	/*%
418 	 * True if added by automatically by named.
419 	 */
420 	bool automatic;
421 
422 	/*%
423 	 * response policy data to be relayed to the database
424 	 */
425 	dns_rpz_zones_t *rpzs;
426 	dns_rpz_num_t rpz_num;
427 
428 	/*%
429 	 * catalog zone data
430 	 */
431 	dns_catz_zones_t *catzs;
432 
433 	/*%
434 	 * parent catalog zone
435 	 */
436 	dns_catz_zone_t *parentcatz;
437 
438 	/*%
439 	 * Serial number update method.
440 	 */
441 	dns_updatemethod_t updatemethod;
442 
443 	/*%
444 	 * whether ixfr is requested
445 	 */
446 	bool requestixfr;
447 	uint32_t ixfr_ratio;
448 
449 	/*%
450 	 * whether EDNS EXPIRE is requested
451 	 */
452 	bool requestexpire;
453 
454 	/*%
455 	 * Outstanding forwarded UPDATE requests.
456 	 */
457 	dns_forwardlist_t forwards;
458 
459 	dns_zone_t *raw;
460 	dns_zone_t *secure;
461 
462 	bool sourceserialset;
463 	uint32_t sourceserial;
464 
465 	/*%
466 	 * soa and maximum zone ttl
467 	 */
468 	dns_ttl_t soattl;
469 	dns_ttl_t maxttl;
470 
471 	/*
472 	 * Inline zone signing state.
473 	 */
474 	dns_diff_t rss_diff;
475 	isc_eventlist_t rss_events;
476 	isc_eventlist_t rss_post;
477 	dns_dbversion_t *rss_newver;
478 	dns_dbversion_t *rss_oldver;
479 	dns_db_t *rss_db;
480 	dns_zone_t *rss_raw;
481 	isc_event_t *rss_event;
482 	dns_update_state_t *rss_state;
483 
484 	isc_stats_t *gluecachestats;
485 };
486 
487 #define zonediff_init(z, d)                \
488 	do {                               \
489 		dns__zonediff_t *_z = (z); \
490 		(_z)->diff = (d);          \
491 		(_z)->offline = false;     \
492 	} while (0)
493 
494 #define DNS_ZONE_FLAG(z, f)    ((atomic_load_relaxed(&(z)->flags) & (f)) != 0)
495 #define DNS_ZONE_SETFLAG(z, f) atomic_fetch_or(&(z)->flags, (f))
496 #define DNS_ZONE_CLRFLAG(z, f) atomic_fetch_and(&(z)->flags, ~(f))
497 typedef enum {
498 	DNS_ZONEFLG_REFRESH = 0x00000001U,     /*%< refresh check in progress */
499 	DNS_ZONEFLG_NEEDDUMP = 0x00000002U,    /*%< zone need consolidation */
500 	DNS_ZONEFLG_USEVC = 0x00000004U,       /*%< use tcp for refresh query */
501 	DNS_ZONEFLG_DUMPING = 0x00000008U,     /*%< a dump is in progress */
502 	DNS_ZONEFLG_HASINCLUDE = 0x00000010U,  /*%< $INCLUDE in zone file */
503 	DNS_ZONEFLG_LOADED = 0x00000020U,      /*%< database has loaded */
504 	DNS_ZONEFLG_EXITING = 0x00000040U,     /*%< zone is being destroyed */
505 	DNS_ZONEFLG_EXPIRED = 0x00000080U,     /*%< zone has expired */
506 	DNS_ZONEFLG_NEEDREFRESH = 0x00000100U, /*%< refresh check needed */
507 	DNS_ZONEFLG_UPTODATE = 0x00000200U,    /*%< zone contents are
508 						* up-to-date */
509 	DNS_ZONEFLG_NEEDNOTIFY = 0x00000400U,  /*%< need to send out notify
510 						* messages */
511 	DNS_ZONEFLG_FIXJOURNAL = 0x00000800U,  /*%< journal file had
512 						* recoverable error,
513 						* needs rewriting */
514 	DNS_ZONEFLG_NOMASTERS = 0x00001000U,   /*%< an attempt to refresh a
515 						* zone with no primaries
516 						* occurred */
517 	DNS_ZONEFLG_LOADING = 0x00002000U,     /*%< load from disk in progress*/
518 	DNS_ZONEFLG_HAVETIMERS = 0x00004000U,  /*%< timer values have been set
519 						* from SOA (if not set, we
520 						* are still using
521 						* default timer values) */
522 	DNS_ZONEFLG_FORCEXFER = 0x00008000U,   /*%< Force a zone xfer */
523 	DNS_ZONEFLG_NOREFRESH = 0x00010000U,
524 	DNS_ZONEFLG_DIALNOTIFY = 0x00020000U,
525 	DNS_ZONEFLG_DIALREFRESH = 0x00040000U,
526 	DNS_ZONEFLG_SHUTDOWN = 0x00080000U,
527 	DNS_ZONEFLG_NOIXFR = 0x00100000U, /*%< IXFR failed, force AXFR */
528 	DNS_ZONEFLG_FLUSH = 0x00200000U,
529 	DNS_ZONEFLG_NOEDNS = 0x00400000U,
530 	DNS_ZONEFLG_USEALTXFRSRC = 0x00800000U,
531 	DNS_ZONEFLG_SOABEFOREAXFR = 0x01000000U,
532 	DNS_ZONEFLG_NEEDCOMPACT = 0x02000000U,
533 	DNS_ZONEFLG_REFRESHING = 0x04000000U, /*%< Refreshing keydata */
534 	DNS_ZONEFLG_THAW = 0x08000000U,
535 	DNS_ZONEFLG_LOADPENDING = 0x10000000U, /*%< Loading scheduled */
536 	DNS_ZONEFLG_NODELAY = 0x20000000U,
537 	DNS_ZONEFLG_SENDSECURE = 0x40000000U,
538 	DNS_ZONEFLG_NEEDSTARTUPNOTIFY = 0x80000000U, /*%< need to send out
539 						      * notify due to the zone
540 						      * just being loaded for
541 						      * the first time. */
542 	/*
543 	 * DO NOT add any new zone flags here until all platforms
544 	 * support 64-bit enum values. Currently they fail on
545 	 * Windows.
546 	 */
547 	DNS_ZONEFLG___MAX = UINT64_MAX, /* trick to make the ENUM 64-bit wide */
548 } dns_zoneflg_t;
549 
550 #define DNS_ZONE_OPTION(z, o)	 ((atomic_load_relaxed(&(z)->options) & (o)) != 0)
551 #define DNS_ZONE_SETOPTION(z, o) atomic_fetch_or(&(z)->options, (o))
552 #define DNS_ZONE_CLROPTION(z, o) atomic_fetch_and(&(z)->options, ~(o))
553 
554 #define DNS_ZONEKEY_OPTION(z, o) \
555 	((atomic_load_relaxed(&(z)->keyopts) & (o)) != 0)
556 #define DNS_ZONEKEY_SETOPTION(z, o) atomic_fetch_or(&(z)->keyopts, (o))
557 #define DNS_ZONEKEY_CLROPTION(z, o) atomic_fetch_and(&(z)->keyopts, ~(o))
558 
559 /* Flags for zone_load() */
560 typedef enum {
561 	DNS_ZONELOADFLAG_NOSTAT = 0x00000001U, /* Do not stat() master files */
562 	DNS_ZONELOADFLAG_THAW = 0x00000002U,   /* Thaw the zone on successful
563 						* load. */
564 } dns_zoneloadflag_t;
565 
566 #define UNREACH_CACHE_SIZE 10U
567 #define UNREACH_HOLD_TIME  600 /* 10 minutes */
568 
569 #define CHECK(op)                            \
570 	do {                                 \
571 		result = (op);               \
572 		if (result != ISC_R_SUCCESS) \
573 			goto failure;        \
574 	} while (0)
575 
576 struct dns_unreachable {
577 	isc_sockaddr_t remote;
578 	isc_sockaddr_t local;
579 	atomic_uint_fast32_t expire;
580 	atomic_uint_fast32_t last;
581 	uint32_t count;
582 };
583 
584 struct dns_zonemgr {
585 	unsigned int magic;
586 	isc_mem_t *mctx;
587 	isc_refcount_t refs;
588 	isc_taskmgr_t *taskmgr;
589 	isc_timermgr_t *timermgr;
590 	isc_socketmgr_t *socketmgr;
591 	isc_taskpool_t *zonetasks;
592 	isc_taskpool_t *loadtasks;
593 	isc_task_t *task;
594 	isc_pool_t *mctxpool;
595 	isc_ratelimiter_t *checkdsrl;
596 	isc_ratelimiter_t *notifyrl;
597 	isc_ratelimiter_t *refreshrl;
598 	isc_ratelimiter_t *startupnotifyrl;
599 	isc_ratelimiter_t *startuprefreshrl;
600 	isc_rwlock_t rwlock;
601 	isc_mutex_t iolock;
602 	isc_rwlock_t urlock;
603 
604 	/* Locked by rwlock. */
605 	dns_zonelist_t zones;
606 	dns_zonelist_t waiting_for_xfrin;
607 	dns_zonelist_t xfrin_in_progress;
608 
609 	/* Configuration data. */
610 	uint32_t transfersin;
611 	uint32_t transfersperns;
612 	unsigned int checkdsrate;
613 	unsigned int notifyrate;
614 	unsigned int startupnotifyrate;
615 	unsigned int serialqueryrate;
616 	unsigned int startupserialqueryrate;
617 
618 	/* Locked by iolock */
619 	uint32_t iolimit;
620 	uint32_t ioactive;
621 	dns_iolist_t high;
622 	dns_iolist_t low;
623 
624 	/* Locked by urlock. */
625 	/* LRU cache */
626 	struct dns_unreachable unreachable[UNREACH_CACHE_SIZE];
627 
628 	dns_keymgmt_t *keymgmt;
629 };
630 
631 /*%
632  * Hold notify state.
633  */
634 struct dns_notify {
635 	unsigned int magic;
636 	unsigned int flags;
637 	isc_mem_t *mctx;
638 	dns_zone_t *zone;
639 	dns_adbfind_t *find;
640 	dns_request_t *request;
641 	dns_name_t ns;
642 	isc_sockaddr_t dst;
643 	dns_tsigkey_t *key;
644 	isc_dscp_t dscp;
645 	ISC_LINK(dns_notify_t) link;
646 	isc_event_t *event;
647 };
648 
649 #define DNS_NOTIFY_NOSOA   0x0001U
650 #define DNS_NOTIFY_STARTUP 0x0002U
651 
652 /*%
653  * Hold checkds state.
654  */
655 struct dns_checkds {
656 	unsigned int magic;
657 	unsigned int flags;
658 	isc_mem_t *mctx;
659 	dns_zone_t *zone;
660 	dns_request_t *request;
661 	isc_sockaddr_t dst;
662 	dns_tsigkey_t *key;
663 	isc_dscp_t dscp;
664 	ISC_LINK(dns_checkds_t) link;
665 	isc_event_t *event;
666 };
667 
668 /*%
669  *	dns_stub holds state while performing a 'stub' transfer.
670  *	'db' is the zone's 'db' or a new one if this is the initial
671  *	transfer.
672  */
673 
674 struct dns_stub {
675 	unsigned int magic;
676 	isc_mem_t *mctx;
677 	dns_zone_t *zone;
678 	dns_db_t *db;
679 	dns_dbversion_t *version;
680 	atomic_uint_fast32_t pending_requests;
681 };
682 
683 /*%
684  *	Hold load state.
685  */
686 struct dns_load {
687 	unsigned int magic;
688 	isc_mem_t *mctx;
689 	dns_zone_t *zone;
690 	dns_db_t *db;
691 	isc_time_t loadtime;
692 	dns_rdatacallbacks_t callbacks;
693 };
694 
695 /*%
696  *	Hold forward state.
697  */
698 struct dns_forward {
699 	unsigned int magic;
700 	isc_mem_t *mctx;
701 	dns_zone_t *zone;
702 	isc_buffer_t *msgbuf;
703 	dns_request_t *request;
704 	uint32_t which;
705 	isc_sockaddr_t addr;
706 	dns_updatecallback_t callback;
707 	void *callback_arg;
708 	unsigned int options;
709 	ISC_LINK(dns_forward_t) link;
710 };
711 
712 /*%
713  *	Hold IO request state.
714  */
715 struct dns_io {
716 	unsigned int magic;
717 	dns_zonemgr_t *zmgr;
718 	bool high;
719 	isc_task_t *task;
720 	ISC_LINK(dns_io_t) link;
721 	isc_event_t *event;
722 };
723 
724 /*%
725  *	Hold state for when we are signing a zone with a new
726  *	DNSKEY as result of an update.
727  */
728 struct dns_signing {
729 	unsigned int magic;
730 	dns_db_t *db;
731 	dns_dbiterator_t *dbiterator;
732 	dns_secalg_t algorithm;
733 	uint16_t keyid;
734 	bool deleteit;
735 	bool done;
736 	ISC_LINK(dns_signing_t) link;
737 };
738 
739 struct dns_nsec3chain {
740 	unsigned int magic;
741 	dns_db_t *db;
742 	dns_dbiterator_t *dbiterator;
743 	dns_rdata_nsec3param_t nsec3param;
744 	unsigned char salt[255];
745 	bool done;
746 	bool seen_nsec;
747 	bool delete_nsec;
748 	bool save_delete_nsec;
749 	ISC_LINK(dns_nsec3chain_t) link;
750 };
751 
752 /*%
753  *	Hold key file IO locks.
754  */
755 typedef struct dns_keyfileio {
756 	struct dns_keyfileio *next;
757 	uint32_t hashval;
758 	dns_fixedname_t fname;
759 	dns_name_t *name;
760 	atomic_uint_fast32_t count;
761 	isc_mutex_t lock;
762 } dns_keyfileio_t;
763 
764 struct dns_keymgmt {
765 	unsigned int magic;
766 	isc_rwlock_t lock;
767 	isc_mem_t *mctx;
768 
769 	dns_keyfileio_t **table;
770 
771 	atomic_uint_fast32_t count;
772 
773 	uint32_t bits;
774 };
775 
776 /*%<
777  * 'dbiterator' contains a iterator for the database.  If we are creating
778  * a NSEC3 chain only the non-NSEC3 nodes will be iterated.  If we are
779  * removing a NSEC3 chain then both NSEC3 and non-NSEC3 nodes will be
780  * iterated.
781  *
782  * 'nsec3param' contains the parameters of the NSEC3 chain being created
783  * or removed.
784  *
785  * 'salt' is buffer space and is referenced via 'nsec3param.salt'.
786  *
787  * 'seen_nsec' will be set to true if, while iterating the zone to create a
788  * NSEC3 chain, a NSEC record is seen.
789  *
790  * 'delete_nsec' will be set to true if, at the completion of the creation
791  * of a NSEC3 chain, 'seen_nsec' is true.  If 'delete_nsec' is true then we
792  * are in the process of deleting the NSEC chain.
793  *
794  * 'save_delete_nsec' is used to store the initial state of 'delete_nsec'
795  * so it can be recovered in the event of a error.
796  */
797 
798 struct dns_keyfetch {
799 	dns_fixedname_t name;
800 	dns_rdataset_t keydataset;
801 	dns_rdataset_t dnskeyset;
802 	dns_rdataset_t dnskeysigset;
803 	dns_zone_t *zone;
804 	dns_db_t *db;
805 	dns_fetch_t *fetch;
806 };
807 
808 /*%
809  * Hold state for an asynchronous load
810  */
811 struct dns_asyncload {
812 	dns_zone_t *zone;
813 	unsigned int flags;
814 	dns_zt_zoneloaded_t loaded;
815 	void *loaded_arg;
816 };
817 
818 /*%
819  * Reference to an include file encountered during loading
820  */
821 struct dns_include {
822 	char *name;
823 	isc_time_t filetime;
824 	ISC_LINK(dns_include_t) link;
825 };
826 
827 /*
828  * These can be overridden by the -T mkeytimers option on the command
829  * line, so that we can test with shorter periods than specified in
830  * RFC 5011.
831  */
832 #define HOUR  3600
833 #define DAY   (24 * HOUR)
834 #define MONTH (30 * DAY)
835 LIBDNS_EXTERNAL_DATA unsigned int dns_zone_mkey_hour = HOUR;
836 LIBDNS_EXTERNAL_DATA unsigned int dns_zone_mkey_day = DAY;
837 LIBDNS_EXTERNAL_DATA unsigned int dns_zone_mkey_month = MONTH;
838 
839 #define SEND_BUFFER_SIZE 2048
840 
841 static void
842 zone_settimer(dns_zone_t *, isc_time_t *);
843 static void
844 cancel_refresh(dns_zone_t *);
845 static void
846 zone_debuglog(dns_zone_t *zone, const char *, int debuglevel, const char *msg,
847 	      ...) ISC_FORMAT_PRINTF(4, 5);
848 static void
849 notify_log(dns_zone_t *zone, int level, const char *fmt, ...)
850 	ISC_FORMAT_PRINTF(3, 4);
851 static void
852 dnssec_log(dns_zone_t *zone, int level, const char *fmt, ...)
853 	ISC_FORMAT_PRINTF(3, 4);
854 static void
855 queue_xfrin(dns_zone_t *zone);
856 static isc_result_t
857 update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
858 	      dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
859 	      dns_rdata_t *rdata);
860 static void
861 zone_unload(dns_zone_t *zone);
862 static void
863 zone_expire(dns_zone_t *zone);
864 static void
865 zone_iattach(dns_zone_t *source, dns_zone_t **target);
866 static void
867 zone_idetach(dns_zone_t **zonep);
868 static isc_result_t
869 zone_replacedb(dns_zone_t *zone, dns_db_t *db, bool dump);
870 static inline void
871 zone_attachdb(dns_zone_t *zone, dns_db_t *db);
872 static inline void
873 zone_detachdb(dns_zone_t *zone);
874 static void
875 zone_catz_enable(dns_zone_t *zone, dns_catz_zones_t *catzs);
876 static void
877 zone_catz_disable(dns_zone_t *zone);
878 static isc_result_t
879 default_journal(dns_zone_t *zone);
880 static void
881 zone_xfrdone(dns_zone_t *zone, isc_result_t result);
882 static isc_result_t
883 zone_postload(dns_zone_t *zone, dns_db_t *db, isc_time_t loadtime,
884 	      isc_result_t result);
885 static void
886 zone_needdump(dns_zone_t *zone, unsigned int delay);
887 static void
888 zone_shutdown(isc_task_t *, isc_event_t *);
889 static void
890 zone_loaddone(void *arg, isc_result_t result);
891 static isc_result_t
892 zone_startload(dns_db_t *db, dns_zone_t *zone, isc_time_t loadtime);
893 static void
894 zone_namerd_tostr(dns_zone_t *zone, char *buf, size_t length);
895 static void
896 zone_name_tostr(dns_zone_t *zone, char *buf, size_t length);
897 static void
898 zone_rdclass_tostr(dns_zone_t *zone, char *buf, size_t length);
899 static void
900 zone_viewname_tostr(dns_zone_t *zone, char *buf, size_t length);
901 static isc_result_t
902 zone_send_secureserial(dns_zone_t *zone, uint32_t serial);
903 static void
904 refresh_callback(isc_task_t *, isc_event_t *);
905 static void
906 stub_callback(isc_task_t *, isc_event_t *);
907 static void
908 queue_soa_query(dns_zone_t *zone);
909 static void
910 soa_query(isc_task_t *, isc_event_t *);
911 static void
912 ns_query(dns_zone_t *zone, dns_rdataset_t *soardataset, dns_stub_t *stub);
913 static int
914 message_count(dns_message_t *msg, dns_section_t section, dns_rdatatype_t type);
915 static void
916 checkds_cancel(dns_zone_t *zone);
917 static void
918 checkds_send(dns_zone_t *zone);
919 static isc_result_t
920 checkds_createmessage(dns_zone_t *zone, dns_message_t **messagep);
921 static void
922 checkds_done(isc_task_t *task, isc_event_t *event);
923 static void
924 checkds_send_toaddr(isc_task_t *task, isc_event_t *event);
925 static void
926 notify_cancel(dns_zone_t *zone);
927 static void
928 notify_find_address(dns_notify_t *notify);
929 static void
930 notify_send(dns_notify_t *notify);
931 static isc_result_t
932 notify_createmessage(dns_zone_t *zone, unsigned int flags,
933 		     dns_message_t **messagep);
934 static void
935 notify_done(isc_task_t *task, isc_event_t *event);
936 static void
937 notify_send_toaddr(isc_task_t *task, isc_event_t *event);
938 static isc_result_t
939 zone_dump(dns_zone_t *, bool);
940 static void
941 got_transfer_quota(isc_task_t *task, isc_event_t *event);
942 static isc_result_t
943 zmgr_start_xfrin_ifquota(dns_zonemgr_t *zmgr, dns_zone_t *zone);
944 static void
945 zmgr_resume_xfrs(dns_zonemgr_t *zmgr, bool multi);
946 static void
947 zonemgr_free(dns_zonemgr_t *zmgr);
948 static isc_result_t
949 zonemgr_getio(dns_zonemgr_t *zmgr, bool high, isc_task_t *task,
950 	      isc_taskaction_t action, void *arg, dns_io_t **iop);
951 static void
952 zonemgr_putio(dns_io_t **iop);
953 static void
954 zonemgr_cancelio(dns_io_t *io);
955 static void
956 rss_post(dns_zone_t *, isc_event_t *);
957 
958 static isc_result_t
959 zone_get_from_db(dns_zone_t *zone, dns_db_t *db, unsigned int *nscount,
960 		 unsigned int *soacount, uint32_t *soattl, uint32_t *serial,
961 		 uint32_t *refresh, uint32_t *retry, uint32_t *expire,
962 		 uint32_t *minimum, unsigned int *errors);
963 
964 static void
965 zone_freedbargs(dns_zone_t *zone);
966 static void
967 forward_callback(isc_task_t *task, isc_event_t *event);
968 static void
969 zone_saveunique(dns_zone_t *zone, const char *path, const char *templat);
970 static void
971 zone_maintenance(dns_zone_t *zone);
972 static void
973 zone_notify(dns_zone_t *zone, isc_time_t *now);
974 static void
975 dump_done(void *arg, isc_result_t result);
976 static isc_result_t
977 zone_signwithkey(dns_zone_t *zone, dns_secalg_t algorithm, uint16_t keyid,
978 		 bool deleteit);
979 static isc_result_t
980 delete_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_dbnode_t *node,
981 	    dns_name_t *name, dns_diff_t *diff);
982 static void
983 zone_rekey(dns_zone_t *zone);
984 static isc_result_t
985 zone_send_securedb(dns_zone_t *zone, dns_db_t *db);
986 static dns_ttl_t
987 zone_nsecttl(dns_zone_t *zone);
988 static void
989 setrl(isc_ratelimiter_t *rl, unsigned int *rate, unsigned int value);
990 static void
991 zone_journal_compact(dns_zone_t *zone, dns_db_t *db, uint32_t serial);
992 static isc_result_t
993 zone_journal_rollforward(dns_zone_t *zone, dns_db_t *db, bool *needdump,
994 			 bool *fixjournal);
995 
996 #define ENTER zone_debuglog(zone, me, 1, "enter")
997 
998 static const unsigned int dbargc_default = 1;
999 static const char *dbargv_default[] = { "rbt" };
1000 
1001 #define DNS_ZONE_JITTER_ADD(a, b, c)                                         \
1002 	do {                                                                 \
1003 		isc_interval_t _i;                                           \
1004 		uint32_t _j;                                                 \
1005 		_j = (b)-isc_random_uniform((b) / 4);                        \
1006 		isc_interval_set(&_i, _j, 0);                                \
1007 		if (isc_time_add((a), &_i, (c)) != ISC_R_SUCCESS) {          \
1008 			dns_zone_log(zone, ISC_LOG_WARNING,                  \
1009 				     "epoch approaching: upgrade required: " \
1010 				     "now + %s failed",                      \
1011 				     #b);                                    \
1012 			isc_interval_set(&_i, _j / 2, 0);                    \
1013 			(void)isc_time_add((a), &_i, (c));                   \
1014 		}                                                            \
1015 	} while (0)
1016 
1017 #define DNS_ZONE_TIME_ADD(a, b, c)                                           \
1018 	do {                                                                 \
1019 		isc_interval_t _i;                                           \
1020 		isc_interval_set(&_i, (b), 0);                               \
1021 		if (isc_time_add((a), &_i, (c)) != ISC_R_SUCCESS) {          \
1022 			dns_zone_log(zone, ISC_LOG_WARNING,                  \
1023 				     "epoch approaching: upgrade required: " \
1024 				     "now + %s failed",                      \
1025 				     #b);                                    \
1026 			isc_interval_set(&_i, (b) / 2, 0);                   \
1027 			(void)isc_time_add((a), &_i, (c));                   \
1028 		}                                                            \
1029 	} while (0)
1030 
1031 typedef struct nsec3param nsec3param_t;
1032 struct nsec3param {
1033 	dns_rdata_nsec3param_t rdata;
1034 	unsigned char data[DNS_NSEC3PARAM_BUFFERSIZE + 1];
1035 	unsigned int length;
1036 	bool nsec;
1037 	bool replace;
1038 	bool resalt;
1039 	bool lookup;
1040 	ISC_LINK(nsec3param_t) link;
1041 };
1042 typedef ISC_LIST(nsec3param_t) nsec3paramlist_t;
1043 struct np3event {
1044 	isc_event_t event;
1045 	nsec3param_t params;
1046 };
1047 
1048 struct ssevent {
1049 	isc_event_t event;
1050 	uint32_t serial;
1051 };
1052 
1053 struct stub_cb_args {
1054 	dns_stub_t *stub;
1055 	dns_tsigkey_t *tsig_key;
1056 	isc_dscp_t dscp;
1057 	uint16_t udpsize;
1058 	int timeout;
1059 	bool reqnsid;
1060 };
1061 
1062 struct stub_glue_request {
1063 	dns_request_t *request;
1064 	dns_name_t name;
1065 	struct stub_cb_args *args;
1066 	bool ipv4;
1067 };
1068 
1069 /*%
1070  * Increment resolver-related statistics counters.  Zone must be locked.
1071  */
1072 static inline void
inc_stats(dns_zone_t * zone,isc_statscounter_t counter)1073 inc_stats(dns_zone_t *zone, isc_statscounter_t counter) {
1074 	if (zone->stats != NULL) {
1075 		isc_stats_increment(zone->stats, counter);
1076 	}
1077 }
1078 
1079 /***
1080  ***	Public functions.
1081  ***/
1082 
1083 isc_result_t
dns_zone_create(dns_zone_t ** zonep,isc_mem_t * mctx)1084 dns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) {
1085 	isc_result_t result;
1086 	isc_time_t now;
1087 	dns_zone_t *zone = NULL;
1088 	dns_zone_t z = { .masterformat = dns_masterformat_none,
1089 			 .journalsize = -1,
1090 			 .rdclass = dns_rdataclass_none,
1091 			 .type = dns_zone_none,
1092 			 .refresh = DNS_ZONE_DEFAULTREFRESH,
1093 			 .retry = DNS_ZONE_DEFAULTRETRY,
1094 			 .maxrefresh = DNS_ZONE_MAXREFRESH,
1095 			 .minrefresh = DNS_ZONE_MINREFRESH,
1096 			 .maxretry = DNS_ZONE_MAXRETRY,
1097 			 .minretry = DNS_ZONE_MINRETRY,
1098 			 .notifytype = dns_notifytype_yes,
1099 			 .zero_no_soa_ttl = true,
1100 			 .check_names = dns_severity_ignore,
1101 			 .idlein = DNS_DEFAULT_IDLEIN,
1102 			 .idleout = DNS_DEFAULT_IDLEOUT,
1103 			 .notifysrc4dscp = -1,
1104 			 .notifysrc6dscp = -1,
1105 			 .parentalsrc4dscp = -1,
1106 			 .parentalsrc6dscp = -1,
1107 			 .xfrsource4dscp = -1,
1108 			 .xfrsource6dscp = -1,
1109 			 .altxfrsource4dscp = -1,
1110 			 .altxfrsource6dscp = -1,
1111 			 .maxxfrin = MAX_XFER_TIME,
1112 			 .maxxfrout = MAX_XFER_TIME,
1113 			 .sigvalidityinterval = 30 * 24 * 3600,
1114 			 .sigresigninginterval = 7 * 24 * 3600,
1115 			 .statlevel = dns_zonestat_none,
1116 			 .notifydelay = 5,
1117 			 .signatures = 10,
1118 			 .nodes = 100,
1119 			 .privatetype = (dns_rdatatype_t)0xffffU,
1120 			 .rpz_num = DNS_RPZ_INVALID_NUM,
1121 			 .requestixfr = true,
1122 			 .ixfr_ratio = 100,
1123 			 .requestexpire = true,
1124 			 .updatemethod = dns_updatemethod_increment,
1125 			 .magic = ZONE_MAGIC };
1126 
1127 	REQUIRE(zonep != NULL && *zonep == NULL);
1128 	REQUIRE(mctx != NULL);
1129 
1130 	TIME_NOW(&now);
1131 	zone = isc_mem_get(mctx, sizeof(*zone));
1132 	*zone = z;
1133 
1134 	zone->mctx = NULL;
1135 	isc_mem_attach(mctx, &zone->mctx);
1136 	isc_mutex_init(&zone->lock);
1137 	ZONEDB_INITLOCK(&zone->dblock);
1138 	/* XXX MPA check that all elements are initialised */
1139 #ifdef DNS_ZONE_CHECKLOCK
1140 	zone->locked = false;
1141 #endif /* ifdef DNS_ZONE_CHECKLOCK */
1142 
1143 	zone->notifytime = now;
1144 
1145 	ISC_LINK_INIT(zone, link);
1146 	isc_refcount_init(&zone->erefs, 1);
1147 	isc_refcount_init(&zone->irefs, 0);
1148 	dns_name_init(&zone->origin, NULL);
1149 	ISC_LIST_INIT(zone->includes);
1150 	ISC_LIST_INIT(zone->newincludes);
1151 	atomic_init(&zone->flags, 0);
1152 	atomic_init(&zone->options, 0);
1153 	atomic_init(&zone->keyopts, 0);
1154 	isc_time_settoepoch(&zone->expiretime);
1155 	isc_time_settoepoch(&zone->refreshtime);
1156 	isc_time_settoepoch(&zone->dumptime);
1157 	isc_time_settoepoch(&zone->loadtime);
1158 	isc_time_settoepoch(&zone->resigntime);
1159 	isc_time_settoepoch(&zone->keywarntime);
1160 	isc_time_settoepoch(&zone->signingtime);
1161 	isc_time_settoepoch(&zone->nsec3chaintime);
1162 	isc_time_settoepoch(&zone->refreshkeytime);
1163 	ISC_LIST_INIT(zone->notifies);
1164 	ISC_LIST_INIT(zone->checkds_requests);
1165 	isc_sockaddr_any(&zone->notifysrc4);
1166 	isc_sockaddr_any6(&zone->notifysrc6);
1167 	isc_sockaddr_any(&zone->parentalsrc4);
1168 	isc_sockaddr_any6(&zone->parentalsrc6);
1169 	isc_sockaddr_any(&zone->xfrsource4);
1170 	isc_sockaddr_any6(&zone->xfrsource6);
1171 	isc_sockaddr_any(&zone->altxfrsource4);
1172 	isc_sockaddr_any6(&zone->altxfrsource6);
1173 	ISC_LINK_INIT(zone, statelink);
1174 	ISC_LIST_INIT(zone->signing);
1175 	ISC_LIST_INIT(zone->nsec3chain);
1176 	ISC_LIST_INIT(zone->setnsec3param_queue);
1177 	ISC_LIST_INIT(zone->forwards);
1178 	ISC_LIST_INIT(zone->rss_events);
1179 	ISC_LIST_INIT(zone->rss_post);
1180 
1181 	result = isc_stats_create(mctx, &zone->gluecachestats,
1182 				  dns_gluecachestatscounter_max);
1183 	if (result != ISC_R_SUCCESS) {
1184 		goto free_refs;
1185 	}
1186 
1187 	/* Must be after magic is set. */
1188 	dns_zone_setdbtype(zone, dbargc_default, dbargv_default);
1189 
1190 	ISC_EVENT_INIT(&zone->ctlevent, sizeof(zone->ctlevent), 0, NULL,
1191 		       DNS_EVENT_ZONECONTROL, zone_shutdown, zone, zone, NULL,
1192 		       NULL);
1193 	*zonep = zone;
1194 	return (ISC_R_SUCCESS);
1195 
1196 free_refs:
1197 	isc_refcount_decrement0(&zone->erefs);
1198 	isc_refcount_destroy(&zone->erefs);
1199 	isc_refcount_destroy(&zone->irefs);
1200 	ZONEDB_DESTROYLOCK(&zone->dblock);
1201 	isc_mutex_destroy(&zone->lock);
1202 	isc_mem_putanddetach(&zone->mctx, zone, sizeof(*zone));
1203 	return (result);
1204 }
1205 
1206 static void
clear_keylist(dns_dnsseckeylist_t * list,isc_mem_t * mctx)1207 clear_keylist(dns_dnsseckeylist_t *list, isc_mem_t *mctx) {
1208 	dns_dnsseckey_t *key;
1209 	while (!ISC_LIST_EMPTY(*list)) {
1210 		key = ISC_LIST_HEAD(*list);
1211 		ISC_LIST_UNLINK(*list, key, link);
1212 		dns_dnsseckey_destroy(mctx, &key);
1213 	}
1214 }
1215 
1216 /*
1217  * Free a zone.  Because we require that there be no more
1218  * outstanding events or references, no locking is necessary.
1219  */
1220 static void
zone_free(dns_zone_t * zone)1221 zone_free(dns_zone_t *zone) {
1222 	dns_signing_t *signing;
1223 	dns_nsec3chain_t *nsec3chain;
1224 	isc_event_t *event;
1225 	dns_include_t *include;
1226 
1227 	REQUIRE(DNS_ZONE_VALID(zone));
1228 	isc_refcount_destroy(&zone->erefs);
1229 	isc_refcount_destroy(&zone->irefs);
1230 	REQUIRE(!LOCKED_ZONE(zone));
1231 	REQUIRE(zone->timer == NULL);
1232 	REQUIRE(zone->zmgr == NULL);
1233 
1234 	/*
1235 	 * Managed objects.  Order is important.
1236 	 */
1237 	if (zone->request != NULL) {
1238 		dns_request_destroy(&zone->request); /* XXXMPA */
1239 	}
1240 	INSIST(zone->readio == NULL);
1241 	INSIST(zone->statelist == NULL);
1242 	INSIST(zone->writeio == NULL);
1243 
1244 	if (zone->task != NULL) {
1245 		isc_task_detach(&zone->task);
1246 	}
1247 	if (zone->loadtask != NULL) {
1248 		isc_task_detach(&zone->loadtask);
1249 	}
1250 	if (zone->view != NULL) {
1251 		dns_view_weakdetach(&zone->view);
1252 	}
1253 	if (zone->prev_view != NULL) {
1254 		dns_view_weakdetach(&zone->prev_view);
1255 	}
1256 
1257 	/* Unmanaged objects */
1258 	while (!ISC_LIST_EMPTY(zone->setnsec3param_queue)) {
1259 		event = ISC_LIST_HEAD(zone->setnsec3param_queue);
1260 		ISC_LIST_UNLINK(zone->setnsec3param_queue, event, ev_link);
1261 		isc_event_free(&event);
1262 	}
1263 	while (!ISC_LIST_EMPTY(zone->rss_post)) {
1264 		event = ISC_LIST_HEAD(zone->rss_post);
1265 		ISC_LIST_UNLINK(zone->rss_post, event, ev_link);
1266 		isc_event_free(&event);
1267 	}
1268 	for (signing = ISC_LIST_HEAD(zone->signing); signing != NULL;
1269 	     signing = ISC_LIST_HEAD(zone->signing))
1270 	{
1271 		ISC_LIST_UNLINK(zone->signing, signing, link);
1272 		dns_db_detach(&signing->db);
1273 		dns_dbiterator_destroy(&signing->dbiterator);
1274 		isc_mem_put(zone->mctx, signing, sizeof *signing);
1275 	}
1276 	for (nsec3chain = ISC_LIST_HEAD(zone->nsec3chain); nsec3chain != NULL;
1277 	     nsec3chain = ISC_LIST_HEAD(zone->nsec3chain))
1278 	{
1279 		ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain, link);
1280 		dns_db_detach(&nsec3chain->db);
1281 		dns_dbiterator_destroy(&nsec3chain->dbiterator);
1282 		isc_mem_put(zone->mctx, nsec3chain, sizeof *nsec3chain);
1283 	}
1284 	for (include = ISC_LIST_HEAD(zone->includes); include != NULL;
1285 	     include = ISC_LIST_HEAD(zone->includes))
1286 	{
1287 		ISC_LIST_UNLINK(zone->includes, include, link);
1288 		isc_mem_free(zone->mctx, include->name);
1289 		isc_mem_put(zone->mctx, include, sizeof *include);
1290 	}
1291 	for (include = ISC_LIST_HEAD(zone->newincludes); include != NULL;
1292 	     include = ISC_LIST_HEAD(zone->newincludes))
1293 	{
1294 		ISC_LIST_UNLINK(zone->newincludes, include, link);
1295 		isc_mem_free(zone->mctx, include->name);
1296 		isc_mem_put(zone->mctx, include, sizeof *include);
1297 	}
1298 	if (zone->masterfile != NULL) {
1299 		isc_mem_free(zone->mctx, zone->masterfile);
1300 	}
1301 	zone->masterfile = NULL;
1302 	if (zone->keydirectory != NULL) {
1303 		isc_mem_free(zone->mctx, zone->keydirectory);
1304 	}
1305 	zone->keydirectory = NULL;
1306 	if (zone->kasp != NULL) {
1307 		dns_kasp_detach(&zone->kasp);
1308 	}
1309 	if (!ISC_LIST_EMPTY(zone->checkds_ok)) {
1310 		clear_keylist(&zone->checkds_ok, zone->mctx);
1311 	}
1312 
1313 	zone->journalsize = -1;
1314 	if (zone->journal != NULL) {
1315 		isc_mem_free(zone->mctx, zone->journal);
1316 	}
1317 	zone->journal = NULL;
1318 	if (zone->stats != NULL) {
1319 		isc_stats_detach(&zone->stats);
1320 	}
1321 	if (zone->requeststats != NULL) {
1322 		isc_stats_detach(&zone->requeststats);
1323 	}
1324 	if (zone->rcvquerystats != NULL) {
1325 		dns_stats_detach(&zone->rcvquerystats);
1326 	}
1327 	if (zone->dnssecsignstats != NULL) {
1328 		dns_stats_detach(&zone->dnssecsignstats);
1329 	}
1330 	if (zone->db != NULL) {
1331 		zone_detachdb(zone);
1332 	}
1333 	if (zone->rpzs != NULL) {
1334 		REQUIRE(zone->rpz_num < zone->rpzs->p.num_zones);
1335 		dns_rpz_detach_rpzs(&zone->rpzs);
1336 		zone->rpz_num = DNS_RPZ_INVALID_NUM;
1337 	}
1338 	if (zone->catzs != NULL) {
1339 		dns_catz_catzs_detach(&zone->catzs);
1340 	}
1341 	zone_freedbargs(zone);
1342 
1343 	RUNTIME_CHECK(dns_zone_setparentals(zone, NULL, NULL, 0) ==
1344 		      ISC_R_SUCCESS);
1345 	RUNTIME_CHECK(dns_zone_setprimarieswithkeys(zone, NULL, NULL, 0) ==
1346 		      ISC_R_SUCCESS);
1347 	RUNTIME_CHECK(dns_zone_setalsonotify(zone, NULL, 0) == ISC_R_SUCCESS);
1348 	zone->check_names = dns_severity_ignore;
1349 	if (zone->update_acl != NULL) {
1350 		dns_acl_detach(&zone->update_acl);
1351 	}
1352 	if (zone->forward_acl != NULL) {
1353 		dns_acl_detach(&zone->forward_acl);
1354 	}
1355 	if (zone->notify_acl != NULL) {
1356 		dns_acl_detach(&zone->notify_acl);
1357 	}
1358 	if (zone->query_acl != NULL) {
1359 		dns_acl_detach(&zone->query_acl);
1360 	}
1361 	if (zone->queryon_acl != NULL) {
1362 		dns_acl_detach(&zone->queryon_acl);
1363 	}
1364 	if (zone->xfr_acl != NULL) {
1365 		dns_acl_detach(&zone->xfr_acl);
1366 	}
1367 	if (dns_name_dynamic(&zone->origin)) {
1368 		dns_name_free(&zone->origin, zone->mctx);
1369 	}
1370 	if (zone->strnamerd != NULL) {
1371 		isc_mem_free(zone->mctx, zone->strnamerd);
1372 	}
1373 	if (zone->strname != NULL) {
1374 		isc_mem_free(zone->mctx, zone->strname);
1375 	}
1376 	if (zone->strrdclass != NULL) {
1377 		isc_mem_free(zone->mctx, zone->strrdclass);
1378 	}
1379 	if (zone->strviewname != NULL) {
1380 		isc_mem_free(zone->mctx, zone->strviewname);
1381 	}
1382 	if (zone->ssutable != NULL) {
1383 		dns_ssutable_detach(&zone->ssutable);
1384 	}
1385 	if (zone->gluecachestats != NULL) {
1386 		isc_stats_detach(&zone->gluecachestats);
1387 	}
1388 
1389 	/* last stuff */
1390 	ZONEDB_DESTROYLOCK(&zone->dblock);
1391 	isc_mutex_destroy(&zone->lock);
1392 	zone->magic = 0;
1393 	isc_mem_putanddetach(&zone->mctx, zone, sizeof(*zone));
1394 }
1395 
1396 /*
1397  * Returns true iff this the signed side of an inline-signing zone.
1398  * Caller should hold zone lock.
1399  */
1400 static inline bool
inline_secure(dns_zone_t * zone)1401 inline_secure(dns_zone_t *zone) {
1402 	REQUIRE(DNS_ZONE_VALID(zone));
1403 	if (zone->raw != NULL) {
1404 		return (true);
1405 	}
1406 	return (false);
1407 }
1408 
1409 /*
1410  * Returns true iff this the unsigned side of an inline-signing zone
1411  * Caller should hold zone lock.
1412  */
1413 static inline bool
inline_raw(dns_zone_t * zone)1414 inline_raw(dns_zone_t *zone) {
1415 	REQUIRE(DNS_ZONE_VALID(zone));
1416 	if (zone->secure != NULL) {
1417 		return (true);
1418 	}
1419 	return (false);
1420 }
1421 
1422 /*
1423  *	Single shot.
1424  */
1425 void
dns_zone_setclass(dns_zone_t * zone,dns_rdataclass_t rdclass)1426 dns_zone_setclass(dns_zone_t *zone, dns_rdataclass_t rdclass) {
1427 	char namebuf[1024];
1428 
1429 	REQUIRE(DNS_ZONE_VALID(zone));
1430 	REQUIRE(rdclass != dns_rdataclass_none);
1431 
1432 	/*
1433 	 * Test and set.
1434 	 */
1435 	LOCK_ZONE(zone);
1436 	INSIST(zone != zone->raw);
1437 	REQUIRE(zone->rdclass == dns_rdataclass_none ||
1438 		zone->rdclass == rdclass);
1439 	zone->rdclass = rdclass;
1440 
1441 	if (zone->strnamerd != NULL) {
1442 		isc_mem_free(zone->mctx, zone->strnamerd);
1443 	}
1444 	if (zone->strrdclass != NULL) {
1445 		isc_mem_free(zone->mctx, zone->strrdclass);
1446 	}
1447 
1448 	zone_namerd_tostr(zone, namebuf, sizeof namebuf);
1449 	zone->strnamerd = isc_mem_strdup(zone->mctx, namebuf);
1450 	zone_rdclass_tostr(zone, namebuf, sizeof namebuf);
1451 	zone->strrdclass = isc_mem_strdup(zone->mctx, namebuf);
1452 
1453 	if (inline_secure(zone)) {
1454 		dns_zone_setclass(zone->raw, rdclass);
1455 	}
1456 	UNLOCK_ZONE(zone);
1457 }
1458 
1459 dns_rdataclass_t
dns_zone_getclass(dns_zone_t * zone)1460 dns_zone_getclass(dns_zone_t *zone) {
1461 	REQUIRE(DNS_ZONE_VALID(zone));
1462 
1463 	return (zone->rdclass);
1464 }
1465 
1466 void
dns_zone_setnotifytype(dns_zone_t * zone,dns_notifytype_t notifytype)1467 dns_zone_setnotifytype(dns_zone_t *zone, dns_notifytype_t notifytype) {
1468 	REQUIRE(DNS_ZONE_VALID(zone));
1469 
1470 	LOCK_ZONE(zone);
1471 	zone->notifytype = notifytype;
1472 	UNLOCK_ZONE(zone);
1473 }
1474 
1475 isc_result_t
dns_zone_getserial(dns_zone_t * zone,uint32_t * serialp)1476 dns_zone_getserial(dns_zone_t *zone, uint32_t *serialp) {
1477 	isc_result_t result;
1478 	unsigned int soacount;
1479 
1480 	REQUIRE(DNS_ZONE_VALID(zone));
1481 	REQUIRE(serialp != NULL);
1482 
1483 	LOCK_ZONE(zone);
1484 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
1485 	if (zone->db != NULL) {
1486 		result = zone_get_from_db(zone, zone->db, NULL, &soacount, NULL,
1487 					  serialp, NULL, NULL, NULL, NULL,
1488 					  NULL);
1489 		if (result == ISC_R_SUCCESS && soacount == 0) {
1490 			result = ISC_R_FAILURE;
1491 		}
1492 	} else {
1493 		result = DNS_R_NOTLOADED;
1494 	}
1495 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
1496 	UNLOCK_ZONE(zone);
1497 
1498 	return (result);
1499 }
1500 
1501 /*
1502  *	Single shot.
1503  */
1504 void
dns_zone_settype(dns_zone_t * zone,dns_zonetype_t type)1505 dns_zone_settype(dns_zone_t *zone, dns_zonetype_t type) {
1506 	char namebuf[1024];
1507 
1508 	REQUIRE(DNS_ZONE_VALID(zone));
1509 	REQUIRE(type != dns_zone_none);
1510 
1511 	/*
1512 	 * Test and set.
1513 	 */
1514 	LOCK_ZONE(zone);
1515 	REQUIRE(zone->type == dns_zone_none || zone->type == type);
1516 	zone->type = type;
1517 
1518 	if (zone->strnamerd != NULL) {
1519 		isc_mem_free(zone->mctx, zone->strnamerd);
1520 	}
1521 
1522 	zone_namerd_tostr(zone, namebuf, sizeof namebuf);
1523 	zone->strnamerd = isc_mem_strdup(zone->mctx, namebuf);
1524 	UNLOCK_ZONE(zone);
1525 }
1526 
1527 static void
zone_freedbargs(dns_zone_t * zone)1528 zone_freedbargs(dns_zone_t *zone) {
1529 	unsigned int i;
1530 
1531 	/* Free the old database argument list. */
1532 	if (zone->db_argv != NULL) {
1533 		for (i = 0; i < zone->db_argc; i++) {
1534 			isc_mem_free(zone->mctx, zone->db_argv[i]);
1535 		}
1536 		isc_mem_put(zone->mctx, zone->db_argv,
1537 			    zone->db_argc * sizeof(*zone->db_argv));
1538 	}
1539 	zone->db_argc = 0;
1540 	zone->db_argv = NULL;
1541 }
1542 
1543 isc_result_t
dns_zone_getdbtype(dns_zone_t * zone,char *** argv,isc_mem_t * mctx)1544 dns_zone_getdbtype(dns_zone_t *zone, char ***argv, isc_mem_t *mctx) {
1545 	size_t size = 0;
1546 	unsigned int i;
1547 	isc_result_t result = ISC_R_SUCCESS;
1548 	void *mem;
1549 	char **tmp, *tmp2, *base;
1550 
1551 	REQUIRE(DNS_ZONE_VALID(zone));
1552 	REQUIRE(argv != NULL && *argv == NULL);
1553 
1554 	LOCK_ZONE(zone);
1555 	size = (zone->db_argc + 1) * sizeof(char *);
1556 	for (i = 0; i < zone->db_argc; i++) {
1557 		size += strlen(zone->db_argv[i]) + 1;
1558 	}
1559 	mem = isc_mem_allocate(mctx, size);
1560 	{
1561 		tmp = mem;
1562 		tmp2 = mem;
1563 		base = mem;
1564 		tmp2 += (zone->db_argc + 1) * sizeof(char *);
1565 		for (i = 0; i < zone->db_argc; i++) {
1566 			*tmp++ = tmp2;
1567 			strlcpy(tmp2, zone->db_argv[i], size - (tmp2 - base));
1568 			tmp2 += strlen(tmp2) + 1;
1569 		}
1570 		*tmp = NULL;
1571 	}
1572 	UNLOCK_ZONE(zone);
1573 	*argv = mem;
1574 	return (result);
1575 }
1576 
1577 void
dns_zone_setdbtype(dns_zone_t * zone,unsigned int dbargc,const char * const * dbargv)1578 dns_zone_setdbtype(dns_zone_t *zone, unsigned int dbargc,
1579 		   const char *const *dbargv) {
1580 	char **argv = NULL;
1581 	unsigned int i;
1582 
1583 	REQUIRE(DNS_ZONE_VALID(zone));
1584 	REQUIRE(dbargc >= 1);
1585 	REQUIRE(dbargv != NULL);
1586 
1587 	LOCK_ZONE(zone);
1588 
1589 	/* Set up a new database argument list. */
1590 	argv = isc_mem_get(zone->mctx, dbargc * sizeof(*argv));
1591 	for (i = 0; i < dbargc; i++) {
1592 		argv[i] = NULL;
1593 	}
1594 	for (i = 0; i < dbargc; i++) {
1595 		argv[i] = isc_mem_strdup(zone->mctx, dbargv[i]);
1596 	}
1597 
1598 	/* Free the old list. */
1599 	zone_freedbargs(zone);
1600 
1601 	zone->db_argc = dbargc;
1602 	zone->db_argv = argv;
1603 
1604 	UNLOCK_ZONE(zone);
1605 }
1606 
1607 static void
dns_zone_setview_helper(dns_zone_t * zone,dns_view_t * view)1608 dns_zone_setview_helper(dns_zone_t *zone, dns_view_t *view) {
1609 	char namebuf[1024];
1610 
1611 	if (zone->prev_view == NULL && zone->view != NULL) {
1612 		dns_view_weakattach(zone->view, &zone->prev_view);
1613 	}
1614 
1615 	INSIST(zone != zone->raw);
1616 	if (zone->view != NULL) {
1617 		dns_view_weakdetach(&zone->view);
1618 	}
1619 	dns_view_weakattach(view, &zone->view);
1620 
1621 	if (zone->strviewname != NULL) {
1622 		isc_mem_free(zone->mctx, zone->strviewname);
1623 	}
1624 	if (zone->strnamerd != NULL) {
1625 		isc_mem_free(zone->mctx, zone->strnamerd);
1626 	}
1627 
1628 	zone_namerd_tostr(zone, namebuf, sizeof namebuf);
1629 	zone->strnamerd = isc_mem_strdup(zone->mctx, namebuf);
1630 	zone_viewname_tostr(zone, namebuf, sizeof namebuf);
1631 	zone->strviewname = isc_mem_strdup(zone->mctx, namebuf);
1632 
1633 	if (inline_secure(zone)) {
1634 		dns_zone_setview(zone->raw, view);
1635 	}
1636 }
1637 
1638 void
dns_zone_setview(dns_zone_t * zone,dns_view_t * view)1639 dns_zone_setview(dns_zone_t *zone, dns_view_t *view) {
1640 	REQUIRE(DNS_ZONE_VALID(zone));
1641 
1642 	LOCK_ZONE(zone);
1643 	dns_zone_setview_helper(zone, view);
1644 	UNLOCK_ZONE(zone);
1645 }
1646 
1647 dns_view_t *
dns_zone_getview(dns_zone_t * zone)1648 dns_zone_getview(dns_zone_t *zone) {
1649 	REQUIRE(DNS_ZONE_VALID(zone));
1650 
1651 	return (zone->view);
1652 }
1653 
1654 void
dns_zone_setviewcommit(dns_zone_t * zone)1655 dns_zone_setviewcommit(dns_zone_t *zone) {
1656 	REQUIRE(DNS_ZONE_VALID(zone));
1657 
1658 	LOCK_ZONE(zone);
1659 	if (zone->prev_view != NULL) {
1660 		dns_view_weakdetach(&zone->prev_view);
1661 	}
1662 	if (inline_secure(zone)) {
1663 		dns_zone_setviewcommit(zone->raw);
1664 	}
1665 	UNLOCK_ZONE(zone);
1666 }
1667 
1668 void
dns_zone_setviewrevert(dns_zone_t * zone)1669 dns_zone_setviewrevert(dns_zone_t *zone) {
1670 	REQUIRE(DNS_ZONE_VALID(zone));
1671 
1672 	LOCK_ZONE(zone);
1673 	if (zone->prev_view != NULL) {
1674 		dns_zone_setview_helper(zone, zone->prev_view);
1675 		dns_view_weakdetach(&zone->prev_view);
1676 	}
1677 	if (zone->catzs != NULL) {
1678 		zone_catz_enable(zone, zone->catzs);
1679 	}
1680 	if (inline_secure(zone)) {
1681 		dns_zone_setviewrevert(zone->raw);
1682 	}
1683 	UNLOCK_ZONE(zone);
1684 }
1685 
1686 isc_result_t
dns_zone_setorigin(dns_zone_t * zone,const dns_name_t * origin)1687 dns_zone_setorigin(dns_zone_t *zone, const dns_name_t *origin) {
1688 	isc_result_t result = ISC_R_SUCCESS;
1689 	char namebuf[1024];
1690 
1691 	REQUIRE(DNS_ZONE_VALID(zone));
1692 	REQUIRE(origin != NULL);
1693 
1694 	LOCK_ZONE(zone);
1695 	INSIST(zone != zone->raw);
1696 	if (dns_name_dynamic(&zone->origin)) {
1697 		dns_name_free(&zone->origin, zone->mctx);
1698 		dns_name_init(&zone->origin, NULL);
1699 	}
1700 	dns_name_dup(origin, zone->mctx, &zone->origin);
1701 
1702 	if (zone->strnamerd != NULL) {
1703 		isc_mem_free(zone->mctx, zone->strnamerd);
1704 	}
1705 	if (zone->strname != NULL) {
1706 		isc_mem_free(zone->mctx, zone->strname);
1707 	}
1708 
1709 	zone_namerd_tostr(zone, namebuf, sizeof namebuf);
1710 	zone->strnamerd = isc_mem_strdup(zone->mctx, namebuf);
1711 	zone_name_tostr(zone, namebuf, sizeof namebuf);
1712 	zone->strname = isc_mem_strdup(zone->mctx, namebuf);
1713 
1714 	if (inline_secure(zone)) {
1715 		result = dns_zone_setorigin(zone->raw, origin);
1716 	}
1717 	UNLOCK_ZONE(zone);
1718 	return (result);
1719 }
1720 
1721 static isc_result_t
dns_zone_setstring(dns_zone_t * zone,char ** field,const char * value)1722 dns_zone_setstring(dns_zone_t *zone, char **field, const char *value) {
1723 	char *copy;
1724 
1725 	if (value != NULL) {
1726 		copy = isc_mem_strdup(zone->mctx, value);
1727 	} else {
1728 		copy = NULL;
1729 	}
1730 
1731 	if (*field != NULL) {
1732 		isc_mem_free(zone->mctx, *field);
1733 	}
1734 
1735 	*field = copy;
1736 	return (ISC_R_SUCCESS);
1737 }
1738 
1739 isc_result_t
dns_zone_setfile(dns_zone_t * zone,const char * file,dns_masterformat_t format,const dns_master_style_t * style)1740 dns_zone_setfile(dns_zone_t *zone, const char *file, dns_masterformat_t format,
1741 		 const dns_master_style_t *style) {
1742 	isc_result_t result = ISC_R_SUCCESS;
1743 
1744 	REQUIRE(DNS_ZONE_VALID(zone));
1745 
1746 	LOCK_ZONE(zone);
1747 	result = dns_zone_setstring(zone, &zone->masterfile, file);
1748 	if (result == ISC_R_SUCCESS) {
1749 		zone->masterformat = format;
1750 		if (format == dns_masterformat_text) {
1751 			zone->masterstyle = style;
1752 		}
1753 		result = default_journal(zone);
1754 	}
1755 	UNLOCK_ZONE(zone);
1756 
1757 	return (result);
1758 }
1759 
1760 const char *
dns_zone_getfile(dns_zone_t * zone)1761 dns_zone_getfile(dns_zone_t *zone) {
1762 	REQUIRE(DNS_ZONE_VALID(zone));
1763 
1764 	return (zone->masterfile);
1765 }
1766 
1767 dns_ttl_t
dns_zone_getmaxttl(dns_zone_t * zone)1768 dns_zone_getmaxttl(dns_zone_t *zone) {
1769 	REQUIRE(DNS_ZONE_VALID(zone));
1770 
1771 	return (zone->maxttl);
1772 }
1773 
1774 void
dns_zone_setmaxttl(dns_zone_t * zone,dns_ttl_t maxttl)1775 dns_zone_setmaxttl(dns_zone_t *zone, dns_ttl_t maxttl) {
1776 	REQUIRE(DNS_ZONE_VALID(zone));
1777 
1778 	LOCK_ZONE(zone);
1779 	if (maxttl != 0) {
1780 		DNS_ZONE_SETOPTION(zone, DNS_ZONEOPT_CHECKTTL);
1781 	} else {
1782 		DNS_ZONE_CLROPTION(zone, DNS_ZONEOPT_CHECKTTL);
1783 	}
1784 	zone->maxttl = maxttl;
1785 	UNLOCK_ZONE(zone);
1786 
1787 	return;
1788 }
1789 
1790 static isc_result_t
default_journal(dns_zone_t * zone)1791 default_journal(dns_zone_t *zone) {
1792 	isc_result_t result;
1793 	char *journal;
1794 
1795 	REQUIRE(DNS_ZONE_VALID(zone));
1796 	REQUIRE(LOCKED_ZONE(zone));
1797 
1798 	if (zone->masterfile != NULL) {
1799 		/* Calculate string length including '\0'. */
1800 		int len = strlen(zone->masterfile) + sizeof(".jnl");
1801 		journal = isc_mem_allocate(zone->mctx, len);
1802 		strlcpy(journal, zone->masterfile, len);
1803 		strlcat(journal, ".jnl", len);
1804 	} else {
1805 		journal = NULL;
1806 	}
1807 	result = dns_zone_setstring(zone, &zone->journal, journal);
1808 	if (journal != NULL) {
1809 		isc_mem_free(zone->mctx, journal);
1810 	}
1811 	return (result);
1812 }
1813 
1814 isc_result_t
dns_zone_setjournal(dns_zone_t * zone,const char * myjournal)1815 dns_zone_setjournal(dns_zone_t *zone, const char *myjournal) {
1816 	isc_result_t result = ISC_R_SUCCESS;
1817 
1818 	REQUIRE(DNS_ZONE_VALID(zone));
1819 
1820 	LOCK_ZONE(zone);
1821 	result = dns_zone_setstring(zone, &zone->journal, myjournal);
1822 	UNLOCK_ZONE(zone);
1823 
1824 	return (result);
1825 }
1826 
1827 char *
dns_zone_getjournal(dns_zone_t * zone)1828 dns_zone_getjournal(dns_zone_t *zone) {
1829 	REQUIRE(DNS_ZONE_VALID(zone));
1830 
1831 	return (zone->journal);
1832 }
1833 
1834 /*
1835  * Return true iff the zone is "dynamic", in the sense that the zone's
1836  * master file (if any) is written by the server, rather than being
1837  * updated manually and read by the server.
1838  *
1839  * This is true for slave zones, mirror zones, stub zones, key zones,
1840  * and zones that allow dynamic updates either by having an update
1841  * policy ("ssutable") or an "allow-update" ACL with a value other than
1842  * exactly "{ none; }".
1843  */
1844 bool
dns_zone_isdynamic(dns_zone_t * zone,bool ignore_freeze)1845 dns_zone_isdynamic(dns_zone_t *zone, bool ignore_freeze) {
1846 	REQUIRE(DNS_ZONE_VALID(zone));
1847 
1848 	if (zone->type == dns_zone_secondary || zone->type == dns_zone_mirror ||
1849 	    zone->type == dns_zone_stub || zone->type == dns_zone_key ||
1850 	    (zone->type == dns_zone_redirect && zone->masters != NULL))
1851 	{
1852 		return (true);
1853 	}
1854 
1855 	/* Inline zones are always dynamic. */
1856 	if (zone->type == dns_zone_primary && zone->raw != NULL) {
1857 		return (true);
1858 	}
1859 
1860 	/* If !ignore_freeze, we need check whether updates are disabled.  */
1861 	if (zone->type == dns_zone_primary &&
1862 	    (!zone->update_disabled || ignore_freeze) &&
1863 	    ((zone->ssutable != NULL) ||
1864 	     (zone->update_acl != NULL && !dns_acl_isnone(zone->update_acl))))
1865 	{
1866 		return (true);
1867 	}
1868 
1869 	return (false);
1870 }
1871 
1872 /*
1873  * Set the response policy index and information for a zone.
1874  */
1875 isc_result_t
dns_zone_rpz_enable(dns_zone_t * zone,dns_rpz_zones_t * rpzs,dns_rpz_num_t rpz_num)1876 dns_zone_rpz_enable(dns_zone_t *zone, dns_rpz_zones_t *rpzs,
1877 		    dns_rpz_num_t rpz_num) {
1878 	/*
1879 	 * Only RBTDB zones can be used for response policy zones,
1880 	 * because only they have the code to create the summary data.
1881 	 * Only zones that are loaded instead of mmap()ed create the
1882 	 * summary data and so can be policy zones.
1883 	 */
1884 	if (strcmp(zone->db_argv[0], "rbt") != 0 &&
1885 	    strcmp(zone->db_argv[0], "rbt64") != 0)
1886 	{
1887 		return (ISC_R_NOTIMPLEMENTED);
1888 	}
1889 	if (zone->masterformat == dns_masterformat_map) {
1890 		return (ISC_R_NOTIMPLEMENTED);
1891 	}
1892 
1893 	/*
1894 	 * This must happen only once or be redundant.
1895 	 */
1896 	LOCK_ZONE(zone);
1897 	if (zone->rpzs != NULL) {
1898 		REQUIRE(zone->rpzs == rpzs && zone->rpz_num == rpz_num);
1899 	} else {
1900 		REQUIRE(zone->rpz_num == DNS_RPZ_INVALID_NUM);
1901 		dns_rpz_attach_rpzs(rpzs, &zone->rpzs);
1902 		zone->rpz_num = rpz_num;
1903 	}
1904 	rpzs->defined |= DNS_RPZ_ZBIT(rpz_num);
1905 	UNLOCK_ZONE(zone);
1906 
1907 	return (ISC_R_SUCCESS);
1908 }
1909 
1910 dns_rpz_num_t
dns_zone_get_rpz_num(dns_zone_t * zone)1911 dns_zone_get_rpz_num(dns_zone_t *zone) {
1912 	return (zone->rpz_num);
1913 }
1914 
1915 /*
1916  * If a zone is a response policy zone, mark its new database.
1917  */
1918 void
dns_zone_rpz_enable_db(dns_zone_t * zone,dns_db_t * db)1919 dns_zone_rpz_enable_db(dns_zone_t *zone, dns_db_t *db) {
1920 	isc_result_t result;
1921 	if (zone->rpz_num == DNS_RPZ_INVALID_NUM) {
1922 		return;
1923 	}
1924 	REQUIRE(zone->rpzs != NULL);
1925 	result = dns_db_updatenotify_register(db, dns_rpz_dbupdate_callback,
1926 					      zone->rpzs->zones[zone->rpz_num]);
1927 	REQUIRE(result == ISC_R_SUCCESS);
1928 }
1929 
1930 static void
dns_zone_rpz_disable_db(dns_zone_t * zone,dns_db_t * db)1931 dns_zone_rpz_disable_db(dns_zone_t *zone, dns_db_t *db) {
1932 	if (zone->rpz_num == DNS_RPZ_INVALID_NUM) {
1933 		return;
1934 	}
1935 	REQUIRE(zone->rpzs != NULL);
1936 	(void)dns_db_updatenotify_unregister(db, dns_rpz_dbupdate_callback,
1937 					     zone->rpzs->zones[zone->rpz_num]);
1938 }
1939 
1940 static void
zone_catz_enable(dns_zone_t * zone,dns_catz_zones_t * catzs)1941 zone_catz_enable(dns_zone_t *zone, dns_catz_zones_t *catzs) {
1942 	REQUIRE(DNS_ZONE_VALID(zone));
1943 	REQUIRE(catzs != NULL);
1944 
1945 	INSIST(zone->catzs == NULL || zone->catzs == catzs);
1946 	dns_catz_catzs_set_view(catzs, zone->view);
1947 	if (zone->catzs == NULL) {
1948 		dns_catz_catzs_attach(catzs, &zone->catzs);
1949 	}
1950 }
1951 
1952 void
dns_zone_catz_enable(dns_zone_t * zone,dns_catz_zones_t * catzs)1953 dns_zone_catz_enable(dns_zone_t *zone, dns_catz_zones_t *catzs) {
1954 	REQUIRE(DNS_ZONE_VALID(zone));
1955 
1956 	LOCK_ZONE(zone);
1957 	zone_catz_enable(zone, catzs);
1958 	UNLOCK_ZONE(zone);
1959 }
1960 
1961 static void
zone_catz_disable(dns_zone_t * zone)1962 zone_catz_disable(dns_zone_t *zone) {
1963 	REQUIRE(DNS_ZONE_VALID(zone));
1964 
1965 	if (zone->catzs != NULL) {
1966 		dns_catz_catzs_detach(&zone->catzs);
1967 	}
1968 }
1969 
1970 void
dns_zone_catz_disable(dns_zone_t * zone)1971 dns_zone_catz_disable(dns_zone_t *zone) {
1972 	REQUIRE(DNS_ZONE_VALID(zone));
1973 
1974 	LOCK_ZONE(zone);
1975 	zone_catz_disable(zone);
1976 	UNLOCK_ZONE(zone);
1977 }
1978 
1979 bool
dns_zone_catz_is_enabled(dns_zone_t * zone)1980 dns_zone_catz_is_enabled(dns_zone_t *zone) {
1981 	REQUIRE(DNS_ZONE_VALID(zone));
1982 
1983 	return (zone->catzs != NULL);
1984 }
1985 
1986 /*
1987  * If a zone is a catalog zone, attach it to update notification in database.
1988  */
1989 void
dns_zone_catz_enable_db(dns_zone_t * zone,dns_db_t * db)1990 dns_zone_catz_enable_db(dns_zone_t *zone, dns_db_t *db) {
1991 	REQUIRE(DNS_ZONE_VALID(zone));
1992 	REQUIRE(db != NULL);
1993 
1994 	if (zone->catzs != NULL) {
1995 		dns_db_updatenotify_register(db, dns_catz_dbupdate_callback,
1996 					     zone->catzs);
1997 	}
1998 }
1999 
2000 static void
dns_zone_catz_disable_db(dns_zone_t * zone,dns_db_t * db)2001 dns_zone_catz_disable_db(dns_zone_t *zone, dns_db_t *db) {
2002 	REQUIRE(DNS_ZONE_VALID(zone));
2003 	REQUIRE(db != NULL);
2004 
2005 	if (zone->catzs != NULL) {
2006 		dns_db_updatenotify_unregister(db, dns_catz_dbupdate_callback,
2007 					       zone->catzs);
2008 	}
2009 }
2010 
2011 /*
2012  * Set catalog zone ownership of the zone
2013  */
2014 void
dns_zone_set_parentcatz(dns_zone_t * zone,dns_catz_zone_t * catz)2015 dns_zone_set_parentcatz(dns_zone_t *zone, dns_catz_zone_t *catz) {
2016 	REQUIRE(DNS_ZONE_VALID(zone));
2017 	REQUIRE(catz != NULL);
2018 	LOCK_ZONE(zone);
2019 	INSIST(zone->parentcatz == NULL || zone->parentcatz == catz);
2020 	zone->parentcatz = catz;
2021 	UNLOCK_ZONE(zone);
2022 }
2023 
2024 dns_catz_zone_t *
dns_zone_get_parentcatz(const dns_zone_t * zone)2025 dns_zone_get_parentcatz(const dns_zone_t *zone) {
2026 	REQUIRE(DNS_ZONE_VALID(zone));
2027 	return (zone->parentcatz);
2028 }
2029 
2030 static bool
zone_touched(dns_zone_t * zone)2031 zone_touched(dns_zone_t *zone) {
2032 	isc_result_t result;
2033 	isc_time_t modtime;
2034 	dns_include_t *include;
2035 
2036 	REQUIRE(DNS_ZONE_VALID(zone));
2037 
2038 	result = isc_file_getmodtime(zone->masterfile, &modtime);
2039 	if (result != ISC_R_SUCCESS ||
2040 	    isc_time_compare(&modtime, &zone->loadtime) > 0) {
2041 		return (true);
2042 	}
2043 
2044 	for (include = ISC_LIST_HEAD(zone->includes); include != NULL;
2045 	     include = ISC_LIST_NEXT(include, link))
2046 	{
2047 		result = isc_file_getmodtime(include->name, &modtime);
2048 		if (result != ISC_R_SUCCESS ||
2049 		    isc_time_compare(&modtime, &include->filetime) > 0)
2050 		{
2051 			return (true);
2052 		}
2053 	}
2054 
2055 	return (false);
2056 }
2057 
2058 /*
2059  * Note: when dealing with inline-signed zones, external callers will always
2060  * call zone_load() for the secure zone; zone_load() calls itself recursively
2061  * in order to load the raw zone.
2062  */
2063 static isc_result_t
zone_load(dns_zone_t * zone,unsigned int flags,bool locked)2064 zone_load(dns_zone_t *zone, unsigned int flags, bool locked) {
2065 	isc_result_t result;
2066 	isc_time_t now;
2067 	isc_time_t loadtime;
2068 	dns_db_t *db = NULL;
2069 	bool rbt, hasraw, is_dynamic;
2070 
2071 	REQUIRE(DNS_ZONE_VALID(zone));
2072 
2073 	if (!locked) {
2074 		LOCK_ZONE(zone);
2075 	}
2076 
2077 	INSIST(zone != zone->raw);
2078 	hasraw = inline_secure(zone);
2079 	if (hasraw) {
2080 		/*
2081 		 * We are trying to load an inline-signed zone.  First call
2082 		 * self recursively to try loading the raw version of the zone.
2083 		 * Assuming the raw zone file is readable, there are two
2084 		 * possibilities:
2085 		 *
2086 		 *  a) the raw zone was not yet loaded and thus it will be
2087 		 *     loaded now, synchronously; if this succeeds, a
2088 		 *     subsequent attempt to load the signed zone file will
2089 		 *     take place and thus zone_postload() will be called
2090 		 *     twice: first for the raw zone and then for the secure
2091 		 *     zone; the latter call will take care of syncing the raw
2092 		 *     version with the secure version,
2093 		 *
2094 		 *  b) the raw zone was already loaded and we are trying to
2095 		 *     reload it, which will happen asynchronously; this means
2096 		 *     zone_postload() will only be called for the raw zone
2097 		 *     because "result" returned by the zone_load() call below
2098 		 *     will not be ISC_R_SUCCESS but rather DNS_R_CONTINUE;
2099 		 *     zone_postload() called for the raw zone will take care
2100 		 *     of syncing the raw version with the secure version.
2101 		 */
2102 		result = zone_load(zone->raw, flags, false);
2103 		if (result != ISC_R_SUCCESS) {
2104 			if (!locked) {
2105 				UNLOCK_ZONE(zone);
2106 			}
2107 			return (result);
2108 		}
2109 		LOCK_ZONE(zone->raw);
2110 	}
2111 
2112 	TIME_NOW(&now);
2113 
2114 	INSIST(zone->type != dns_zone_none);
2115 
2116 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADING)) {
2117 		if ((flags & DNS_ZONELOADFLAG_THAW) != 0) {
2118 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_THAW);
2119 		}
2120 		result = DNS_R_CONTINUE;
2121 		goto cleanup;
2122 	}
2123 
2124 	INSIST(zone->db_argc >= 1);
2125 
2126 	rbt = strcmp(zone->db_argv[0], "rbt") == 0 ||
2127 	      strcmp(zone->db_argv[0], "rbt64") == 0;
2128 
2129 	if (zone->db != NULL && zone->masterfile == NULL && rbt) {
2130 		/*
2131 		 * The zone has no master file configured.
2132 		 */
2133 		result = ISC_R_SUCCESS;
2134 		goto cleanup;
2135 	}
2136 
2137 	is_dynamic = dns_zone_isdynamic(zone, false);
2138 	if (zone->db != NULL && is_dynamic) {
2139 		/*
2140 		 * This is a slave, stub, or dynamically updated zone being
2141 		 * reloaded.  Do nothing - the database we already
2142 		 * have is guaranteed to be up-to-date.
2143 		 */
2144 		if (zone->type == dns_zone_primary && !hasraw) {
2145 			result = DNS_R_DYNAMIC;
2146 		} else {
2147 			result = ISC_R_SUCCESS;
2148 		}
2149 		goto cleanup;
2150 	}
2151 
2152 	/*
2153 	 * Store the current time before the zone is loaded, so that if the
2154 	 * file changes between the time of the load and the time that
2155 	 * zone->loadtime is set, then the file will still be reloaded
2156 	 * the next time dns_zone_load is called.
2157 	 */
2158 	TIME_NOW(&loadtime);
2159 
2160 	/*
2161 	 * Don't do the load if the file that stores the zone is older
2162 	 * than the last time the zone was loaded.  If the zone has not
2163 	 * been loaded yet, zone->loadtime will be the epoch.
2164 	 */
2165 	if (zone->masterfile != NULL) {
2166 		isc_time_t filetime;
2167 
2168 		/*
2169 		 * The file is already loaded.	If we are just doing a
2170 		 * "rndc reconfig", we are done.
2171 		 */
2172 		if (!isc_time_isepoch(&zone->loadtime) &&
2173 		    (flags & DNS_ZONELOADFLAG_NOSTAT) != 0)
2174 		{
2175 			result = ISC_R_SUCCESS;
2176 			goto cleanup;
2177 		}
2178 
2179 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) &&
2180 		    !zone_touched(zone)) {
2181 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
2182 				      ISC_LOG_DEBUG(1),
2183 				      "skipping load: master file "
2184 				      "older than last load");
2185 			result = DNS_R_UPTODATE;
2186 			goto cleanup;
2187 		}
2188 
2189 		/*
2190 		 * If the file modification time is in the past
2191 		 * set loadtime to that value.
2192 		 */
2193 		result = isc_file_getmodtime(zone->masterfile, &filetime);
2194 		if (result == ISC_R_SUCCESS &&
2195 		    isc_time_compare(&loadtime, &filetime) > 0) {
2196 			loadtime = filetime;
2197 		}
2198 	}
2199 
2200 	/*
2201 	 * Built in zones (with the exception of empty zones) don't need
2202 	 * to be reloaded.
2203 	 */
2204 	if (zone->type == dns_zone_primary &&
2205 	    strcmp(zone->db_argv[0], "_builtin") == 0 &&
2206 	    (zone->db_argc < 2 || strcmp(zone->db_argv[1], "empty") != 0) &&
2207 	    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED))
2208 	{
2209 		result = ISC_R_SUCCESS;
2210 		goto cleanup;
2211 	}
2212 
2213 	/*
2214 	 * Zones associated with a DLZ don't need to be loaded either,
2215 	 * but we need to associate the database with the zone object.
2216 	 */
2217 	if (strcmp(zone->db_argv[0], "dlz") == 0) {
2218 		dns_dlzdb_t *dlzdb;
2219 		dns_dlzfindzone_t findzone;
2220 
2221 		for (dlzdb = ISC_LIST_HEAD(zone->view->dlz_unsearched);
2222 		     dlzdb != NULL; dlzdb = ISC_LIST_NEXT(dlzdb, link))
2223 		{
2224 			INSIST(DNS_DLZ_VALID(dlzdb));
2225 			if (strcmp(zone->db_argv[1], dlzdb->dlzname) == 0) {
2226 				break;
2227 			}
2228 		}
2229 
2230 		if (dlzdb == NULL) {
2231 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
2232 				      ISC_LOG_ERROR,
2233 				      "DLZ %s does not exist or is set "
2234 				      "to 'search yes;'",
2235 				      zone->db_argv[1]);
2236 			result = ISC_R_NOTFOUND;
2237 			goto cleanup;
2238 		}
2239 
2240 		ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write);
2241 		/* ask SDLZ driver if the zone is supported */
2242 		findzone = dlzdb->implementation->methods->findzone;
2243 		result = (*findzone)(dlzdb->implementation->driverarg,
2244 				     dlzdb->dbdata, dlzdb->mctx,
2245 				     zone->view->rdclass, &zone->origin, NULL,
2246 				     NULL, &db);
2247 		if (result != ISC_R_NOTFOUND) {
2248 			if (zone->db != NULL) {
2249 				zone_detachdb(zone);
2250 			}
2251 			zone_attachdb(zone, db);
2252 			dns_db_detach(&db);
2253 			result = ISC_R_SUCCESS;
2254 		}
2255 		ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write);
2256 
2257 		if (result == ISC_R_SUCCESS) {
2258 			if (dlzdb->configure_callback == NULL) {
2259 				goto cleanup;
2260 			}
2261 
2262 			result = (*dlzdb->configure_callback)(zone->view, dlzdb,
2263 							      zone);
2264 			if (result != ISC_R_SUCCESS) {
2265 				dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
2266 					      ISC_LOG_ERROR,
2267 					      "DLZ configuration callback: %s",
2268 					      isc_result_totext(result));
2269 			}
2270 		}
2271 		goto cleanup;
2272 	}
2273 
2274 	if ((zone->type == dns_zone_secondary ||
2275 	     zone->type == dns_zone_mirror || zone->type == dns_zone_stub ||
2276 	     (zone->type == dns_zone_redirect && zone->masters != NULL)) &&
2277 	    rbt)
2278 	{
2279 		if (zone->masterfile == NULL ||
2280 		    !isc_file_exists(zone->masterfile)) {
2281 			if (zone->masterfile != NULL) {
2282 				dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
2283 					      ISC_LOG_DEBUG(1),
2284 					      "no master file");
2285 			}
2286 			zone->refreshtime = now;
2287 			if (zone->task != NULL) {
2288 				zone_settimer(zone, &now);
2289 			}
2290 			result = ISC_R_SUCCESS;
2291 			goto cleanup;
2292 		}
2293 	}
2294 
2295 	dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(1),
2296 		      "starting load");
2297 
2298 	result = dns_db_create(zone->mctx, zone->db_argv[0], &zone->origin,
2299 			       (zone->type == dns_zone_stub) ? dns_dbtype_stub
2300 							     : dns_dbtype_zone,
2301 			       zone->rdclass, zone->db_argc - 1,
2302 			       zone->db_argv + 1, &db);
2303 
2304 	if (result != ISC_R_SUCCESS) {
2305 		dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR,
2306 			      "loading zone: creating database: %s",
2307 			      isc_result_totext(result));
2308 		goto cleanup;
2309 	}
2310 	dns_db_settask(db, zone->task);
2311 
2312 	if (zone->type == dns_zone_primary ||
2313 	    zone->type == dns_zone_secondary || zone->type == dns_zone_mirror)
2314 	{
2315 		result = dns_db_setgluecachestats(db, zone->gluecachestats);
2316 		if (result == ISC_R_NOTIMPLEMENTED) {
2317 			result = ISC_R_SUCCESS;
2318 		}
2319 		if (result != ISC_R_SUCCESS) {
2320 			goto cleanup;
2321 		}
2322 	}
2323 
2324 	if (!dns_db_ispersistent(db)) {
2325 		if (zone->masterfile != NULL) {
2326 			result = zone_startload(db, zone, loadtime);
2327 		} else {
2328 			result = DNS_R_NOMASTERFILE;
2329 			if (zone->type == dns_zone_primary ||
2330 			    (zone->type == dns_zone_redirect &&
2331 			     zone->masters == NULL))
2332 			{
2333 				dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
2334 					      ISC_LOG_ERROR,
2335 					      "loading zone: "
2336 					      "no master file configured");
2337 				goto cleanup;
2338 			}
2339 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
2340 				      ISC_LOG_INFO,
2341 				      "loading zone: "
2342 				      "no master file configured: continuing");
2343 		}
2344 	}
2345 
2346 	if (result == DNS_R_CONTINUE) {
2347 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADING);
2348 		if ((flags & DNS_ZONELOADFLAG_THAW) != 0) {
2349 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_THAW);
2350 		}
2351 		goto cleanup;
2352 	}
2353 
2354 	result = zone_postload(zone, db, loadtime, result);
2355 
2356 cleanup:
2357 	if (hasraw) {
2358 		UNLOCK_ZONE(zone->raw);
2359 	}
2360 	if (!locked) {
2361 		UNLOCK_ZONE(zone);
2362 	}
2363 	if (db != NULL) {
2364 		dns_db_detach(&db);
2365 	}
2366 	return (result);
2367 }
2368 
2369 isc_result_t
dns_zone_load(dns_zone_t * zone,bool newonly)2370 dns_zone_load(dns_zone_t *zone, bool newonly) {
2371 	return (zone_load(zone, newonly ? DNS_ZONELOADFLAG_NOSTAT : 0, false));
2372 }
2373 
2374 static void
zone_asyncload(isc_task_t * task,isc_event_t * event)2375 zone_asyncload(isc_task_t *task, isc_event_t *event) {
2376 	dns_asyncload_t *asl = event->ev_arg;
2377 	dns_zone_t *zone = asl->zone;
2378 	isc_result_t result;
2379 
2380 	UNUSED(task);
2381 
2382 	REQUIRE(DNS_ZONE_VALID(zone));
2383 
2384 	isc_event_free(&event);
2385 
2386 	LOCK_ZONE(zone);
2387 	result = zone_load(zone, asl->flags, true);
2388 	if (result != DNS_R_CONTINUE) {
2389 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADPENDING);
2390 	}
2391 	UNLOCK_ZONE(zone);
2392 
2393 	/* Inform the zone table we've finished loading */
2394 	if (asl->loaded != NULL) {
2395 		(asl->loaded)(asl->loaded_arg, zone, task);
2396 	}
2397 
2398 	isc_mem_put(zone->mctx, asl, sizeof(*asl));
2399 	dns_zone_idetach(&zone);
2400 }
2401 
2402 isc_result_t
dns_zone_asyncload(dns_zone_t * zone,bool newonly,dns_zt_zoneloaded_t done,void * arg)2403 dns_zone_asyncload(dns_zone_t *zone, bool newonly, dns_zt_zoneloaded_t done,
2404 		   void *arg) {
2405 	isc_event_t *e;
2406 	dns_asyncload_t *asl = NULL;
2407 
2408 	REQUIRE(DNS_ZONE_VALID(zone));
2409 
2410 	if (zone->zmgr == NULL) {
2411 		return (ISC_R_FAILURE);
2412 	}
2413 
2414 	/* If we already have a load pending, stop now */
2415 	LOCK_ZONE(zone);
2416 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING)) {
2417 		UNLOCK_ZONE(zone);
2418 		return (ISC_R_ALREADYRUNNING);
2419 	}
2420 
2421 	asl = isc_mem_get(zone->mctx, sizeof(*asl));
2422 
2423 	asl->zone = NULL;
2424 	asl->flags = newonly ? DNS_ZONELOADFLAG_NOSTAT : 0;
2425 	asl->loaded = done;
2426 	asl->loaded_arg = arg;
2427 
2428 	e = isc_event_allocate(zone->zmgr->mctx, zone->zmgr, DNS_EVENT_ZONELOAD,
2429 			       zone_asyncload, asl, sizeof(isc_event_t));
2430 
2431 	zone_iattach(zone, &asl->zone);
2432 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADPENDING);
2433 	isc_task_send(zone->loadtask, &e);
2434 	UNLOCK_ZONE(zone);
2435 
2436 	return (ISC_R_SUCCESS);
2437 }
2438 
2439 bool
dns__zone_loadpending(dns_zone_t * zone)2440 dns__zone_loadpending(dns_zone_t *zone) {
2441 	REQUIRE(DNS_ZONE_VALID(zone));
2442 
2443 	return (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING));
2444 }
2445 
2446 isc_result_t
dns_zone_loadandthaw(dns_zone_t * zone)2447 dns_zone_loadandthaw(dns_zone_t *zone) {
2448 	isc_result_t result;
2449 
2450 	if (inline_raw(zone)) {
2451 		result = zone_load(zone->secure, DNS_ZONELOADFLAG_THAW, false);
2452 	} else {
2453 		/*
2454 		 * When thawing a zone, we don't know what changes
2455 		 * have been made. If we do DNSSEC maintenance on this
2456 		 * zone, schedule a full sign for this zone.
2457 		 */
2458 		if (zone->type == dns_zone_primary &&
2459 		    DNS_ZONEKEY_OPTION(zone, DNS_ZONEKEY_MAINTAIN))
2460 		{
2461 			DNS_ZONEKEY_SETOPTION(zone, DNS_ZONEKEY_FULLSIGN);
2462 		}
2463 		result = zone_load(zone, DNS_ZONELOADFLAG_THAW, false);
2464 	}
2465 
2466 	switch (result) {
2467 	case DNS_R_CONTINUE:
2468 		/* Deferred thaw. */
2469 		break;
2470 	case DNS_R_UPTODATE:
2471 	case ISC_R_SUCCESS:
2472 	case DNS_R_SEENINCLUDE:
2473 		zone->update_disabled = false;
2474 		break;
2475 	case DNS_R_NOMASTERFILE:
2476 		zone->update_disabled = false;
2477 		break;
2478 	default:
2479 		/* Error, remain in disabled state. */
2480 		break;
2481 	}
2482 	return (result);
2483 }
2484 
2485 static unsigned int
get_master_options(dns_zone_t * zone)2486 get_master_options(dns_zone_t *zone) {
2487 	unsigned int options;
2488 
2489 	options = DNS_MASTER_ZONE | DNS_MASTER_RESIGN;
2490 	if (zone->type == dns_zone_secondary || zone->type == dns_zone_mirror ||
2491 	    (zone->type == dns_zone_redirect && zone->masters == NULL))
2492 	{
2493 		options |= DNS_MASTER_SLAVE;
2494 	}
2495 	if (zone->type == dns_zone_key) {
2496 		options |= DNS_MASTER_KEY;
2497 	}
2498 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNS)) {
2499 		options |= DNS_MASTER_CHECKNS;
2500 	}
2501 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_FATALNS)) {
2502 		options |= DNS_MASTER_FATALNS;
2503 	}
2504 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNAMES)) {
2505 		options |= DNS_MASTER_CHECKNAMES;
2506 	}
2507 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNAMESFAIL)) {
2508 		options |= DNS_MASTER_CHECKNAMESFAIL;
2509 	}
2510 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKMX)) {
2511 		options |= DNS_MASTER_CHECKMX;
2512 	}
2513 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKMXFAIL)) {
2514 		options |= DNS_MASTER_CHECKMXFAIL;
2515 	}
2516 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKWILDCARD)) {
2517 		options |= DNS_MASTER_CHECKWILDCARD;
2518 	}
2519 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKTTL)) {
2520 		options |= DNS_MASTER_CHECKTTL;
2521 	}
2522 
2523 	return (options);
2524 }
2525 
2526 static void
zone_registerinclude(const char * filename,void * arg)2527 zone_registerinclude(const char *filename, void *arg) {
2528 	isc_result_t result;
2529 	dns_zone_t *zone = (dns_zone_t *)arg;
2530 	dns_include_t *inc = NULL;
2531 
2532 	REQUIRE(DNS_ZONE_VALID(zone));
2533 
2534 	if (filename == NULL) {
2535 		return;
2536 	}
2537 
2538 	/*
2539 	 * Suppress duplicates.
2540 	 */
2541 	for (inc = ISC_LIST_HEAD(zone->newincludes); inc != NULL;
2542 	     inc = ISC_LIST_NEXT(inc, link))
2543 	{
2544 		if (strcmp(filename, inc->name) == 0) {
2545 			return;
2546 		}
2547 	}
2548 
2549 	inc = isc_mem_get(zone->mctx, sizeof(dns_include_t));
2550 	inc->name = isc_mem_strdup(zone->mctx, filename);
2551 	ISC_LINK_INIT(inc, link);
2552 
2553 	result = isc_file_getmodtime(filename, &inc->filetime);
2554 	if (result != ISC_R_SUCCESS) {
2555 		isc_time_settoepoch(&inc->filetime);
2556 	}
2557 
2558 	ISC_LIST_APPEND(zone->newincludes, inc, link);
2559 }
2560 
2561 static void
zone_gotreadhandle(isc_task_t * task,isc_event_t * event)2562 zone_gotreadhandle(isc_task_t *task, isc_event_t *event) {
2563 	dns_load_t *load = event->ev_arg;
2564 	isc_result_t result = ISC_R_SUCCESS;
2565 	unsigned int options;
2566 
2567 	REQUIRE(DNS_LOAD_VALID(load));
2568 
2569 	if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0) {
2570 		result = ISC_R_CANCELED;
2571 	}
2572 	isc_event_free(&event);
2573 	if (result == ISC_R_CANCELED) {
2574 		goto fail;
2575 	}
2576 
2577 	options = get_master_options(load->zone);
2578 
2579 	result = dns_master_loadfileinc(
2580 		load->zone->masterfile, dns_db_origin(load->db),
2581 		dns_db_origin(load->db), load->zone->rdclass, options, 0,
2582 		&load->callbacks, task, zone_loaddone, load, &load->zone->lctx,
2583 		zone_registerinclude, load->zone, load->zone->mctx,
2584 		load->zone->masterformat, load->zone->maxttl);
2585 	if (result != ISC_R_SUCCESS && result != DNS_R_CONTINUE &&
2586 	    result != DNS_R_SEENINCLUDE)
2587 	{
2588 		goto fail;
2589 	}
2590 	return;
2591 
2592 fail:
2593 	zone_loaddone(load, result);
2594 }
2595 
2596 static void
get_raw_serial(dns_zone_t * raw,dns_masterrawheader_t * rawdata)2597 get_raw_serial(dns_zone_t *raw, dns_masterrawheader_t *rawdata) {
2598 	isc_result_t result;
2599 	unsigned int soacount;
2600 
2601 	LOCK(&raw->lock);
2602 	if (raw->db != NULL) {
2603 		result = zone_get_from_db(raw, raw->db, NULL, &soacount, NULL,
2604 					  &rawdata->sourceserial, NULL, NULL,
2605 					  NULL, NULL, NULL);
2606 		if (result == ISC_R_SUCCESS && soacount > 0U) {
2607 			rawdata->flags |= DNS_MASTERRAW_SOURCESERIALSET;
2608 		}
2609 	}
2610 	UNLOCK(&raw->lock);
2611 }
2612 
2613 static void
zone_gotwritehandle(isc_task_t * task,isc_event_t * event)2614 zone_gotwritehandle(isc_task_t *task, isc_event_t *event) {
2615 	const char me[] = "zone_gotwritehandle";
2616 	dns_zone_t *zone = event->ev_arg;
2617 	isc_result_t result = ISC_R_SUCCESS;
2618 	dns_dbversion_t *version = NULL;
2619 	dns_masterrawheader_t rawdata;
2620 	dns_db_t *db = NULL;
2621 
2622 	REQUIRE(DNS_ZONE_VALID(zone));
2623 	INSIST(task == zone->task);
2624 	ENTER;
2625 
2626 	if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0) {
2627 		result = ISC_R_CANCELED;
2628 	}
2629 	isc_event_free(&event);
2630 	if (result == ISC_R_CANCELED) {
2631 		goto fail;
2632 	}
2633 
2634 	LOCK_ZONE(zone);
2635 	INSIST(zone != zone->raw);
2636 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
2637 	if (zone->db != NULL) {
2638 		dns_db_attach(zone->db, &db);
2639 	}
2640 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
2641 	if (db != NULL) {
2642 		const dns_master_style_t *output_style;
2643 		dns_db_currentversion(db, &version);
2644 		dns_master_initrawheader(&rawdata);
2645 		if (inline_secure(zone)) {
2646 			get_raw_serial(zone->raw, &rawdata);
2647 		}
2648 		if (zone->type == dns_zone_key) {
2649 			output_style = &dns_master_style_keyzone;
2650 		} else if (zone->masterstyle != NULL) {
2651 			output_style = zone->masterstyle;
2652 		} else {
2653 			output_style = &dns_master_style_default;
2654 		}
2655 		result = dns_master_dumpasync(
2656 			zone->mctx, db, version, output_style, zone->masterfile,
2657 			zone->task, dump_done, zone, &zone->dctx,
2658 			zone->masterformat, &rawdata);
2659 		dns_db_closeversion(db, &version, false);
2660 	} else {
2661 		result = ISC_R_CANCELED;
2662 	}
2663 	if (db != NULL) {
2664 		dns_db_detach(&db);
2665 	}
2666 	UNLOCK_ZONE(zone);
2667 	if (result != DNS_R_CONTINUE) {
2668 		goto fail;
2669 	}
2670 	return;
2671 
2672 fail:
2673 	dump_done(zone, result);
2674 }
2675 
2676 /*
2677  * Save the raw serial number for inline-signing zones.
2678  * (XXX: Other information from the header will be used
2679  * for other purposes in the future, but for now this is
2680  * all we're interested in.)
2681  */
2682 static void
zone_setrawdata(dns_zone_t * zone,dns_masterrawheader_t * header)2683 zone_setrawdata(dns_zone_t *zone, dns_masterrawheader_t *header) {
2684 	if ((header->flags & DNS_MASTERRAW_SOURCESERIALSET) == 0) {
2685 		return;
2686 	}
2687 
2688 	zone->sourceserial = header->sourceserial;
2689 	zone->sourceserialset = true;
2690 }
2691 
2692 void
dns_zone_setrawdata(dns_zone_t * zone,dns_masterrawheader_t * header)2693 dns_zone_setrawdata(dns_zone_t *zone, dns_masterrawheader_t *header) {
2694 	if (zone == NULL) {
2695 		return;
2696 	}
2697 
2698 	LOCK_ZONE(zone);
2699 	zone_setrawdata(zone, header);
2700 	UNLOCK_ZONE(zone);
2701 }
2702 
2703 static isc_result_t
zone_startload(dns_db_t * db,dns_zone_t * zone,isc_time_t loadtime)2704 zone_startload(dns_db_t *db, dns_zone_t *zone, isc_time_t loadtime) {
2705 	const char me[] = "zone_startload";
2706 	dns_load_t *load;
2707 	isc_result_t result;
2708 	isc_result_t tresult;
2709 	unsigned int options;
2710 
2711 	ENTER;
2712 
2713 	dns_zone_rpz_enable_db(zone, db);
2714 	dns_zone_catz_enable_db(zone, db);
2715 
2716 	options = get_master_options(zone);
2717 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_MANYERRORS)) {
2718 		options |= DNS_MASTER_MANYERRORS;
2719 	}
2720 
2721 	if (zone->zmgr != NULL && zone->db != NULL && zone->loadtask != NULL) {
2722 		load = isc_mem_get(zone->mctx, sizeof(*load));
2723 
2724 		load->mctx = NULL;
2725 		load->zone = NULL;
2726 		load->db = NULL;
2727 		load->loadtime = loadtime;
2728 		load->magic = LOAD_MAGIC;
2729 
2730 		isc_mem_attach(zone->mctx, &load->mctx);
2731 		zone_iattach(zone, &load->zone);
2732 		dns_db_attach(db, &load->db);
2733 		dns_rdatacallbacks_init(&load->callbacks);
2734 		load->callbacks.rawdata = zone_setrawdata;
2735 		zone_iattach(zone, &load->callbacks.zone);
2736 		result = dns_db_beginload(db, &load->callbacks);
2737 		if (result != ISC_R_SUCCESS) {
2738 			goto cleanup;
2739 		}
2740 		result = zonemgr_getio(zone->zmgr, true, zone->loadtask,
2741 				       zone_gotreadhandle, load, &zone->readio);
2742 		if (result != ISC_R_SUCCESS) {
2743 			/*
2744 			 * We can't report multiple errors so ignore
2745 			 * the result of dns_db_endload().
2746 			 */
2747 			(void)dns_db_endload(load->db, &load->callbacks);
2748 			goto cleanup;
2749 		} else {
2750 			result = DNS_R_CONTINUE;
2751 		}
2752 	} else {
2753 		dns_rdatacallbacks_t callbacks;
2754 
2755 		dns_rdatacallbacks_init(&callbacks);
2756 		callbacks.rawdata = zone_setrawdata;
2757 		zone_iattach(zone, &callbacks.zone);
2758 		result = dns_db_beginload(db, &callbacks);
2759 		if (result != ISC_R_SUCCESS) {
2760 			zone_idetach(&callbacks.zone);
2761 			return (result);
2762 		}
2763 		result = dns_master_loadfile(
2764 			zone->masterfile, &zone->origin, &zone->origin,
2765 			zone->rdclass, options, 0, &callbacks,
2766 			zone_registerinclude, zone, zone->mctx,
2767 			zone->masterformat, zone->maxttl);
2768 		tresult = dns_db_endload(db, &callbacks);
2769 		if (result == ISC_R_SUCCESS) {
2770 			result = tresult;
2771 		}
2772 		zone_idetach(&callbacks.zone);
2773 	}
2774 
2775 	return (result);
2776 
2777 cleanup:
2778 	load->magic = 0;
2779 	dns_db_detach(&load->db);
2780 	zone_idetach(&load->zone);
2781 	zone_idetach(&load->callbacks.zone);
2782 	isc_mem_detach(&load->mctx);
2783 	isc_mem_put(zone->mctx, load, sizeof(*load));
2784 	return (result);
2785 }
2786 
2787 static bool
zone_check_mx(dns_zone_t * zone,dns_db_t * db,dns_name_t * name,dns_name_t * owner)2788 zone_check_mx(dns_zone_t *zone, dns_db_t *db, dns_name_t *name,
2789 	      dns_name_t *owner) {
2790 	isc_result_t result;
2791 	char ownerbuf[DNS_NAME_FORMATSIZE];
2792 	char namebuf[DNS_NAME_FORMATSIZE];
2793 	char altbuf[DNS_NAME_FORMATSIZE];
2794 	dns_fixedname_t fixed;
2795 	dns_name_t *foundname;
2796 	int level;
2797 
2798 	/*
2799 	 * "." means the services does not exist.
2800 	 */
2801 	if (dns_name_equal(name, dns_rootname)) {
2802 		return (true);
2803 	}
2804 
2805 	/*
2806 	 * Outside of zone.
2807 	 */
2808 	if (!dns_name_issubdomain(name, &zone->origin)) {
2809 		if (zone->checkmx != NULL) {
2810 			return ((zone->checkmx)(zone, name, owner));
2811 		}
2812 		return (true);
2813 	}
2814 
2815 	if (zone->type == dns_zone_primary) {
2816 		level = ISC_LOG_ERROR;
2817 	} else {
2818 		level = ISC_LOG_WARNING;
2819 	}
2820 
2821 	foundname = dns_fixedname_initname(&fixed);
2822 
2823 	result = dns_db_find(db, name, NULL, dns_rdatatype_a, 0, 0, NULL,
2824 			     foundname, NULL, NULL);
2825 	if (result == ISC_R_SUCCESS) {
2826 		return (true);
2827 	}
2828 
2829 	if (result == DNS_R_NXRRSET) {
2830 		result = dns_db_find(db, name, NULL, dns_rdatatype_aaaa, 0, 0,
2831 				     NULL, foundname, NULL, NULL);
2832 		if (result == ISC_R_SUCCESS) {
2833 			return (true);
2834 		}
2835 	}
2836 
2837 	dns_name_format(owner, ownerbuf, sizeof ownerbuf);
2838 	dns_name_format(name, namebuf, sizeof namebuf);
2839 	if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN ||
2840 	    result == DNS_R_EMPTYNAME)
2841 	{
2842 		if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKMXFAIL)) {
2843 			level = ISC_LOG_WARNING;
2844 		}
2845 		dns_zone_log(zone, level,
2846 			     "%s/MX '%s' has no address records (A or AAAA)",
2847 			     ownerbuf, namebuf);
2848 		return ((level == ISC_LOG_WARNING) ? true : false);
2849 	}
2850 
2851 	if (result == DNS_R_CNAME) {
2852 		if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_WARNMXCNAME) ||
2853 		    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNOREMXCNAME))
2854 		{
2855 			level = ISC_LOG_WARNING;
2856 		}
2857 		if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNOREMXCNAME)) {
2858 			dns_zone_log(zone, level,
2859 				     "%s/MX '%s' is a CNAME (illegal)",
2860 				     ownerbuf, namebuf);
2861 		}
2862 		return ((level == ISC_LOG_WARNING) ? true : false);
2863 	}
2864 
2865 	if (result == DNS_R_DNAME) {
2866 		if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_WARNMXCNAME) ||
2867 		    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNOREMXCNAME))
2868 		{
2869 			level = ISC_LOG_WARNING;
2870 		}
2871 		if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNOREMXCNAME)) {
2872 			dns_name_format(foundname, altbuf, sizeof altbuf);
2873 			dns_zone_log(zone, level,
2874 				     "%s/MX '%s' is below a DNAME"
2875 				     " '%s' (illegal)",
2876 				     ownerbuf, namebuf, altbuf);
2877 		}
2878 		return ((level == ISC_LOG_WARNING) ? true : false);
2879 	}
2880 
2881 	if (zone->checkmx != NULL && result == DNS_R_DELEGATION) {
2882 		return ((zone->checkmx)(zone, name, owner));
2883 	}
2884 
2885 	return (true);
2886 }
2887 
2888 static bool
zone_check_srv(dns_zone_t * zone,dns_db_t * db,dns_name_t * name,dns_name_t * owner)2889 zone_check_srv(dns_zone_t *zone, dns_db_t *db, dns_name_t *name,
2890 	       dns_name_t *owner) {
2891 	isc_result_t result;
2892 	char ownerbuf[DNS_NAME_FORMATSIZE];
2893 	char namebuf[DNS_NAME_FORMATSIZE];
2894 	char altbuf[DNS_NAME_FORMATSIZE];
2895 	dns_fixedname_t fixed;
2896 	dns_name_t *foundname;
2897 	int level;
2898 
2899 	/*
2900 	 * "." means the services does not exist.
2901 	 */
2902 	if (dns_name_equal(name, dns_rootname)) {
2903 		return (true);
2904 	}
2905 
2906 	/*
2907 	 * Outside of zone.
2908 	 */
2909 	if (!dns_name_issubdomain(name, &zone->origin)) {
2910 		if (zone->checksrv != NULL) {
2911 			return ((zone->checksrv)(zone, name, owner));
2912 		}
2913 		return (true);
2914 	}
2915 
2916 	if (zone->type == dns_zone_primary) {
2917 		level = ISC_LOG_ERROR;
2918 	} else {
2919 		level = ISC_LOG_WARNING;
2920 	}
2921 
2922 	foundname = dns_fixedname_initname(&fixed);
2923 
2924 	result = dns_db_find(db, name, NULL, dns_rdatatype_a, 0, 0, NULL,
2925 			     foundname, NULL, NULL);
2926 	if (result == ISC_R_SUCCESS) {
2927 		return (true);
2928 	}
2929 
2930 	if (result == DNS_R_NXRRSET) {
2931 		result = dns_db_find(db, name, NULL, dns_rdatatype_aaaa, 0, 0,
2932 				     NULL, foundname, NULL, NULL);
2933 		if (result == ISC_R_SUCCESS) {
2934 			return (true);
2935 		}
2936 	}
2937 
2938 	dns_name_format(owner, ownerbuf, sizeof ownerbuf);
2939 	dns_name_format(name, namebuf, sizeof namebuf);
2940 	if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN ||
2941 	    result == DNS_R_EMPTYNAME)
2942 	{
2943 		dns_zone_log(zone, level,
2944 			     "%s/SRV '%s' has no address records (A or AAAA)",
2945 			     ownerbuf, namebuf);
2946 		/* XXX950 make fatal for 9.5.0. */
2947 		return (true);
2948 	}
2949 
2950 	if (result == DNS_R_CNAME) {
2951 		if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_WARNSRVCNAME) ||
2952 		    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNORESRVCNAME))
2953 		{
2954 			level = ISC_LOG_WARNING;
2955 		}
2956 		if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNORESRVCNAME)) {
2957 			dns_zone_log(zone, level,
2958 				     "%s/SRV '%s' is a CNAME (illegal)",
2959 				     ownerbuf, namebuf);
2960 		}
2961 		return ((level == ISC_LOG_WARNING) ? true : false);
2962 	}
2963 
2964 	if (result == DNS_R_DNAME) {
2965 		if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_WARNSRVCNAME) ||
2966 		    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNORESRVCNAME))
2967 		{
2968 			level = ISC_LOG_WARNING;
2969 		}
2970 		if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNORESRVCNAME)) {
2971 			dns_name_format(foundname, altbuf, sizeof altbuf);
2972 			dns_zone_log(zone, level,
2973 				     "%s/SRV '%s' is below a "
2974 				     "DNAME '%s' (illegal)",
2975 				     ownerbuf, namebuf, altbuf);
2976 		}
2977 		return ((level == ISC_LOG_WARNING) ? true : false);
2978 	}
2979 
2980 	if (zone->checksrv != NULL && result == DNS_R_DELEGATION) {
2981 		return ((zone->checksrv)(zone, name, owner));
2982 	}
2983 
2984 	return (true);
2985 }
2986 
2987 static bool
zone_check_glue(dns_zone_t * zone,dns_db_t * db,dns_name_t * name,dns_name_t * owner)2988 zone_check_glue(dns_zone_t *zone, dns_db_t *db, dns_name_t *name,
2989 		dns_name_t *owner) {
2990 	bool answer = true;
2991 	isc_result_t result, tresult;
2992 	char ownerbuf[DNS_NAME_FORMATSIZE];
2993 	char namebuf[DNS_NAME_FORMATSIZE];
2994 	char altbuf[DNS_NAME_FORMATSIZE];
2995 	dns_fixedname_t fixed;
2996 	dns_name_t *foundname;
2997 	dns_rdataset_t a;
2998 	dns_rdataset_t aaaa;
2999 	int level;
3000 
3001 	/*
3002 	 * Outside of zone.
3003 	 */
3004 	if (!dns_name_issubdomain(name, &zone->origin)) {
3005 		if (zone->checkns != NULL) {
3006 			return ((zone->checkns)(zone, name, owner, NULL, NULL));
3007 		}
3008 		return (true);
3009 	}
3010 
3011 	if (zone->type == dns_zone_primary) {
3012 		level = ISC_LOG_ERROR;
3013 	} else {
3014 		level = ISC_LOG_WARNING;
3015 	}
3016 
3017 	foundname = dns_fixedname_initname(&fixed);
3018 	dns_rdataset_init(&a);
3019 	dns_rdataset_init(&aaaa);
3020 
3021 	/*
3022 	 * Perform a regular lookup to catch DNAME records then look
3023 	 * for glue.
3024 	 */
3025 	result = dns_db_find(db, name, NULL, dns_rdatatype_a, 0, 0, NULL,
3026 			     foundname, &a, NULL);
3027 	switch (result) {
3028 	case ISC_R_SUCCESS:
3029 	case DNS_R_DNAME:
3030 	case DNS_R_CNAME:
3031 		break;
3032 	default:
3033 		if (dns_rdataset_isassociated(&a)) {
3034 			dns_rdataset_disassociate(&a);
3035 		}
3036 		result = dns_db_find(db, name, NULL, dns_rdatatype_a,
3037 				     DNS_DBFIND_GLUEOK, 0, NULL, foundname, &a,
3038 				     NULL);
3039 	}
3040 	if (result == ISC_R_SUCCESS) {
3041 		dns_rdataset_disassociate(&a);
3042 		return (true);
3043 	} else if (result == DNS_R_DELEGATION) {
3044 		dns_rdataset_disassociate(&a);
3045 	}
3046 
3047 	if (result == DNS_R_NXRRSET || result == DNS_R_DELEGATION ||
3048 	    result == DNS_R_GLUE)
3049 	{
3050 		tresult = dns_db_find(db, name, NULL, dns_rdatatype_aaaa,
3051 				      DNS_DBFIND_GLUEOK, 0, NULL, foundname,
3052 				      &aaaa, NULL);
3053 		if (tresult == ISC_R_SUCCESS) {
3054 			if (dns_rdataset_isassociated(&a)) {
3055 				dns_rdataset_disassociate(&a);
3056 			}
3057 			dns_rdataset_disassociate(&aaaa);
3058 			return (true);
3059 		}
3060 		if (tresult == DNS_R_DELEGATION || tresult == DNS_R_DNAME) {
3061 			dns_rdataset_disassociate(&aaaa);
3062 		}
3063 		if (result == DNS_R_GLUE || tresult == DNS_R_GLUE) {
3064 			/*
3065 			 * Check glue against child zone.
3066 			 */
3067 			if (zone->checkns != NULL) {
3068 				answer = (zone->checkns)(zone, name, owner, &a,
3069 							 &aaaa);
3070 			}
3071 			if (dns_rdataset_isassociated(&a)) {
3072 				dns_rdataset_disassociate(&a);
3073 			}
3074 			if (dns_rdataset_isassociated(&aaaa)) {
3075 				dns_rdataset_disassociate(&aaaa);
3076 			}
3077 			return (answer);
3078 		}
3079 	}
3080 
3081 	dns_name_format(owner, ownerbuf, sizeof ownerbuf);
3082 	dns_name_format(name, namebuf, sizeof namebuf);
3083 	if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN ||
3084 	    result == DNS_R_EMPTYNAME || result == DNS_R_DELEGATION)
3085 	{
3086 		const char *what;
3087 		bool required = false;
3088 		if (dns_name_issubdomain(name, owner)) {
3089 			what = "REQUIRED GLUE ";
3090 			required = true;
3091 		} else if (result == DNS_R_DELEGATION) {
3092 			what = "SIBLING GLUE ";
3093 		} else {
3094 			what = "";
3095 		}
3096 
3097 		if (result != DNS_R_DELEGATION || required ||
3098 		    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKSIBLING))
3099 		{
3100 			dns_zone_log(zone, level,
3101 				     "%s/NS '%s' has no %s"
3102 				     "address records (A or AAAA)",
3103 				     ownerbuf, namebuf, what);
3104 			/*
3105 			 * Log missing address record.
3106 			 */
3107 			if (result == DNS_R_DELEGATION && zone->checkns != NULL)
3108 			{
3109 				(void)(zone->checkns)(zone, name, owner, &a,
3110 						      &aaaa);
3111 			}
3112 			/* XXX950 make fatal for 9.5.0. */
3113 			/* answer = false; */
3114 		}
3115 	} else if (result == DNS_R_CNAME) {
3116 		dns_zone_log(zone, level, "%s/NS '%s' is a CNAME (illegal)",
3117 			     ownerbuf, namebuf);
3118 		/* XXX950 make fatal for 9.5.0. */
3119 		/* answer = false; */
3120 	} else if (result == DNS_R_DNAME) {
3121 		dns_name_format(foundname, altbuf, sizeof altbuf);
3122 		dns_zone_log(zone, level,
3123 			     "%s/NS '%s' is below a DNAME '%s' (illegal)",
3124 			     ownerbuf, namebuf, altbuf);
3125 		/* XXX950 make fatal for 9.5.0. */
3126 		/* answer = false; */
3127 	}
3128 
3129 	if (dns_rdataset_isassociated(&a)) {
3130 		dns_rdataset_disassociate(&a);
3131 	}
3132 	if (dns_rdataset_isassociated(&aaaa)) {
3133 		dns_rdataset_disassociate(&aaaa);
3134 	}
3135 	return (answer);
3136 }
3137 
3138 static bool
zone_rrset_check_dup(dns_zone_t * zone,dns_name_t * owner,dns_rdataset_t * rdataset)3139 zone_rrset_check_dup(dns_zone_t *zone, dns_name_t *owner,
3140 		     dns_rdataset_t *rdataset) {
3141 	dns_rdataset_t tmprdataset;
3142 	isc_result_t result;
3143 	bool answer = true;
3144 	bool format = true;
3145 	int level = ISC_LOG_WARNING;
3146 	char ownerbuf[DNS_NAME_FORMATSIZE];
3147 	char typebuf[DNS_RDATATYPE_FORMATSIZE];
3148 	unsigned int count1 = 0;
3149 
3150 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKDUPRRFAIL)) {
3151 		level = ISC_LOG_ERROR;
3152 	}
3153 
3154 	dns_rdataset_init(&tmprdataset);
3155 	for (result = dns_rdataset_first(rdataset); result == ISC_R_SUCCESS;
3156 	     result = dns_rdataset_next(rdataset))
3157 	{
3158 		dns_rdata_t rdata1 = DNS_RDATA_INIT;
3159 		unsigned int count2 = 0;
3160 
3161 		count1++;
3162 		dns_rdataset_current(rdataset, &rdata1);
3163 		dns_rdataset_clone(rdataset, &tmprdataset);
3164 		for (result = dns_rdataset_first(&tmprdataset);
3165 		     result == ISC_R_SUCCESS;
3166 		     result = dns_rdataset_next(&tmprdataset))
3167 		{
3168 			dns_rdata_t rdata2 = DNS_RDATA_INIT;
3169 			count2++;
3170 			if (count1 >= count2) {
3171 				continue;
3172 			}
3173 			dns_rdataset_current(&tmprdataset, &rdata2);
3174 			if (dns_rdata_casecompare(&rdata1, &rdata2) == 0) {
3175 				if (format) {
3176 					dns_name_format(owner, ownerbuf,
3177 							sizeof ownerbuf);
3178 					dns_rdatatype_format(rdata1.type,
3179 							     typebuf,
3180 							     sizeof(typebuf));
3181 					format = false;
3182 				}
3183 				dns_zone_log(zone, level,
3184 					     "%s/%s has "
3185 					     "semantically identical records",
3186 					     ownerbuf, typebuf);
3187 				if (level == ISC_LOG_ERROR) {
3188 					answer = false;
3189 				}
3190 				break;
3191 			}
3192 		}
3193 		dns_rdataset_disassociate(&tmprdataset);
3194 		if (!format) {
3195 			break;
3196 		}
3197 	}
3198 	return (answer);
3199 }
3200 
3201 static bool
zone_check_dup(dns_zone_t * zone,dns_db_t * db)3202 zone_check_dup(dns_zone_t *zone, dns_db_t *db) {
3203 	dns_dbiterator_t *dbiterator = NULL;
3204 	dns_dbnode_t *node = NULL;
3205 	dns_fixedname_t fixed;
3206 	dns_name_t *name;
3207 	dns_rdataset_t rdataset;
3208 	dns_rdatasetiter_t *rdsit = NULL;
3209 	bool ok = true;
3210 	isc_result_t result;
3211 
3212 	name = dns_fixedname_initname(&fixed);
3213 	dns_rdataset_init(&rdataset);
3214 
3215 	result = dns_db_createiterator(db, 0, &dbiterator);
3216 	if (result != ISC_R_SUCCESS) {
3217 		return (true);
3218 	}
3219 
3220 	for (result = dns_dbiterator_first(dbiterator); result == ISC_R_SUCCESS;
3221 	     result = dns_dbiterator_next(dbiterator))
3222 	{
3223 		result = dns_dbiterator_current(dbiterator, &node, name);
3224 		if (result != ISC_R_SUCCESS) {
3225 			continue;
3226 		}
3227 
3228 		result = dns_db_allrdatasets(db, node, NULL, 0, &rdsit);
3229 		if (result != ISC_R_SUCCESS) {
3230 			continue;
3231 		}
3232 
3233 		for (result = dns_rdatasetiter_first(rdsit);
3234 		     result == ISC_R_SUCCESS;
3235 		     result = dns_rdatasetiter_next(rdsit))
3236 		{
3237 			dns_rdatasetiter_current(rdsit, &rdataset);
3238 			if (!zone_rrset_check_dup(zone, name, &rdataset)) {
3239 				ok = false;
3240 			}
3241 			dns_rdataset_disassociate(&rdataset);
3242 		}
3243 		dns_rdatasetiter_destroy(&rdsit);
3244 		dns_db_detachnode(db, &node);
3245 	}
3246 
3247 	if (node != NULL) {
3248 		dns_db_detachnode(db, &node);
3249 	}
3250 	dns_dbiterator_destroy(&dbiterator);
3251 
3252 	return (ok);
3253 }
3254 
3255 static bool
isspf(const dns_rdata_t * rdata)3256 isspf(const dns_rdata_t *rdata) {
3257 	char buf[1024];
3258 	const unsigned char *data = rdata->data;
3259 	unsigned int rdl = rdata->length, i = 0, tl, len;
3260 
3261 	while (rdl > 0U) {
3262 		len = tl = *data;
3263 		++data;
3264 		--rdl;
3265 		INSIST(tl <= rdl);
3266 		if (len > sizeof(buf) - i - 1) {
3267 			len = sizeof(buf) - i - 1;
3268 		}
3269 		memmove(buf + i, data, len);
3270 		i += len;
3271 		data += tl;
3272 		rdl -= tl;
3273 	}
3274 
3275 	if (i < 6U) {
3276 		return (false);
3277 	}
3278 
3279 	buf[i] = 0;
3280 	if (strncmp(buf, "v=spf1", 6) == 0 && (buf[6] == 0 || buf[6] == ' ')) {
3281 		return (true);
3282 	}
3283 	return (false);
3284 }
3285 
3286 static bool
integrity_checks(dns_zone_t * zone,dns_db_t * db)3287 integrity_checks(dns_zone_t *zone, dns_db_t *db) {
3288 	dns_dbiterator_t *dbiterator = NULL;
3289 	dns_dbnode_t *node = NULL;
3290 	dns_rdataset_t rdataset;
3291 	dns_fixedname_t fixed;
3292 	dns_fixedname_t fixedbottom;
3293 	dns_rdata_mx_t mx;
3294 	dns_rdata_ns_t ns;
3295 	dns_rdata_in_srv_t srv;
3296 	dns_rdata_t rdata;
3297 	dns_name_t *name;
3298 	dns_name_t *bottom;
3299 	isc_result_t result;
3300 	bool ok = true, have_spf, have_txt;
3301 
3302 	name = dns_fixedname_initname(&fixed);
3303 	bottom = dns_fixedname_initname(&fixedbottom);
3304 	dns_rdataset_init(&rdataset);
3305 	dns_rdata_init(&rdata);
3306 
3307 	result = dns_db_createiterator(db, 0, &dbiterator);
3308 	if (result != ISC_R_SUCCESS) {
3309 		return (true);
3310 	}
3311 
3312 	result = dns_dbiterator_first(dbiterator);
3313 	while (result == ISC_R_SUCCESS) {
3314 		result = dns_dbiterator_current(dbiterator, &node, name);
3315 		if (result != ISC_R_SUCCESS) {
3316 			goto cleanup;
3317 		}
3318 
3319 		/*
3320 		 * Is this name visible in the zone?
3321 		 */
3322 		if (!dns_name_issubdomain(name, &zone->origin) ||
3323 		    (dns_name_countlabels(bottom) > 0 &&
3324 		     dns_name_issubdomain(name, bottom)))
3325 		{
3326 			goto next;
3327 		}
3328 
3329 		dns_dbiterator_pause(dbiterator);
3330 
3331 		/*
3332 		 * Don't check the NS records at the origin.
3333 		 */
3334 		if (dns_name_equal(name, &zone->origin)) {
3335 			goto checkfordname;
3336 		}
3337 
3338 		result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_ns,
3339 					     0, 0, &rdataset, NULL);
3340 		if (result != ISC_R_SUCCESS) {
3341 			goto checkfordname;
3342 		}
3343 		/*
3344 		 * Remember bottom of zone due to NS.
3345 		 */
3346 		dns_name_copynf(name, bottom);
3347 
3348 		result = dns_rdataset_first(&rdataset);
3349 		while (result == ISC_R_SUCCESS) {
3350 			dns_rdataset_current(&rdataset, &rdata);
3351 			result = dns_rdata_tostruct(&rdata, &ns, NULL);
3352 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
3353 			if (!zone_check_glue(zone, db, &ns.name, name)) {
3354 				ok = false;
3355 			}
3356 			dns_rdata_reset(&rdata);
3357 			result = dns_rdataset_next(&rdataset);
3358 		}
3359 		dns_rdataset_disassociate(&rdataset);
3360 		goto next;
3361 
3362 	checkfordname:
3363 		result = dns_db_findrdataset(db, node, NULL,
3364 					     dns_rdatatype_dname, 0, 0,
3365 					     &rdataset, NULL);
3366 		if (result == ISC_R_SUCCESS) {
3367 			/*
3368 			 * Remember bottom of zone due to DNAME.
3369 			 */
3370 			dns_name_copynf(name, bottom);
3371 			dns_rdataset_disassociate(&rdataset);
3372 		}
3373 
3374 		result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_mx,
3375 					     0, 0, &rdataset, NULL);
3376 		if (result != ISC_R_SUCCESS) {
3377 			goto checksrv;
3378 		}
3379 		result = dns_rdataset_first(&rdataset);
3380 		while (result == ISC_R_SUCCESS) {
3381 			dns_rdataset_current(&rdataset, &rdata);
3382 			result = dns_rdata_tostruct(&rdata, &mx, NULL);
3383 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
3384 			if (!zone_check_mx(zone, db, &mx.mx, name)) {
3385 				ok = false;
3386 			}
3387 			dns_rdata_reset(&rdata);
3388 			result = dns_rdataset_next(&rdataset);
3389 		}
3390 		dns_rdataset_disassociate(&rdataset);
3391 
3392 	checksrv:
3393 		if (zone->rdclass != dns_rdataclass_in) {
3394 			goto next;
3395 		}
3396 		result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_srv,
3397 					     0, 0, &rdataset, NULL);
3398 		if (result != ISC_R_SUCCESS) {
3399 			goto checkspf;
3400 		}
3401 		result = dns_rdataset_first(&rdataset);
3402 		while (result == ISC_R_SUCCESS) {
3403 			dns_rdataset_current(&rdataset, &rdata);
3404 			result = dns_rdata_tostruct(&rdata, &srv, NULL);
3405 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
3406 			if (!zone_check_srv(zone, db, &srv.target, name)) {
3407 				ok = false;
3408 			}
3409 			dns_rdata_reset(&rdata);
3410 			result = dns_rdataset_next(&rdataset);
3411 		}
3412 		dns_rdataset_disassociate(&rdataset);
3413 
3414 	checkspf:
3415 		/*
3416 		 * Check if there is a type SPF record without an
3417 		 * SPF-formatted type TXT record also being present.
3418 		 */
3419 		if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKSPF)) {
3420 			goto next;
3421 		}
3422 		if (zone->rdclass != dns_rdataclass_in) {
3423 			goto next;
3424 		}
3425 		have_spf = have_txt = false;
3426 		result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_spf,
3427 					     0, 0, &rdataset, NULL);
3428 		if (result == ISC_R_SUCCESS) {
3429 			dns_rdataset_disassociate(&rdataset);
3430 			have_spf = true;
3431 		}
3432 		result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_txt,
3433 					     0, 0, &rdataset, NULL);
3434 		if (result != ISC_R_SUCCESS) {
3435 			goto notxt;
3436 		}
3437 		result = dns_rdataset_first(&rdataset);
3438 		while (result == ISC_R_SUCCESS) {
3439 			dns_rdataset_current(&rdataset, &rdata);
3440 			have_txt = isspf(&rdata);
3441 			dns_rdata_reset(&rdata);
3442 			if (have_txt) {
3443 				break;
3444 			}
3445 			result = dns_rdataset_next(&rdataset);
3446 		}
3447 		dns_rdataset_disassociate(&rdataset);
3448 
3449 	notxt:
3450 		if (have_spf && !have_txt) {
3451 			char namebuf[DNS_NAME_FORMATSIZE];
3452 
3453 			dns_name_format(name, namebuf, sizeof(namebuf));
3454 			dns_zone_log(zone, ISC_LOG_WARNING,
3455 				     "'%s' found type "
3456 				     "SPF record but no SPF TXT record found, "
3457 				     "add matching type TXT record",
3458 				     namebuf);
3459 		}
3460 
3461 	next:
3462 		dns_db_detachnode(db, &node);
3463 		result = dns_dbiterator_next(dbiterator);
3464 	}
3465 
3466 cleanup:
3467 	if (node != NULL) {
3468 		dns_db_detachnode(db, &node);
3469 	}
3470 	dns_dbiterator_destroy(&dbiterator);
3471 
3472 	return (ok);
3473 }
3474 
3475 /*
3476  * OpenSSL verification of RSA keys with exponent 3 is known to be
3477  * broken prior OpenSSL 0.9.8c/0.9.7k.	Look for such keys and warn
3478  * if they are in use.
3479  */
3480 static void
zone_check_dnskeys(dns_zone_t * zone,dns_db_t * db)3481 zone_check_dnskeys(dns_zone_t *zone, dns_db_t *db) {
3482 	dns_dbnode_t *node = NULL;
3483 	dns_dbversion_t *version = NULL;
3484 	dns_rdata_dnskey_t dnskey;
3485 	dns_rdata_t rdata = DNS_RDATA_INIT;
3486 	dns_rdataset_t rdataset;
3487 	isc_result_t result;
3488 
3489 	result = dns_db_findnode(db, &zone->origin, false, &node);
3490 	if (result != ISC_R_SUCCESS) {
3491 		goto cleanup;
3492 	}
3493 
3494 	dns_db_currentversion(db, &version);
3495 	dns_rdataset_init(&rdataset);
3496 	result = dns_db_findrdataset(db, node, version, dns_rdatatype_dnskey,
3497 				     dns_rdatatype_none, 0, &rdataset, NULL);
3498 	if (result != ISC_R_SUCCESS) {
3499 		goto cleanup;
3500 	}
3501 
3502 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
3503 	     result = dns_rdataset_next(&rdataset))
3504 	{
3505 		dns_rdataset_current(&rdataset, &rdata);
3506 		result = dns_rdata_tostruct(&rdata, &dnskey, NULL);
3507 		INSIST(result == ISC_R_SUCCESS);
3508 
3509 		/*
3510 		 * RFC 3110, section 4: Performance Considerations:
3511 		 *
3512 		 * A public exponent of 3 minimizes the effort needed to verify
3513 		 * a signature.  Use of 3 as the public exponent is weak for
3514 		 * confidentiality uses since, if the same data can be collected
3515 		 * encrypted under three different keys with an exponent of 3
3516 		 * then, using the Chinese Remainder Theorem [NETSEC], the
3517 		 * original plain text can be easily recovered.  If a key is
3518 		 * known to be used only for authentication, as is the case with
3519 		 * DNSSEC, then an exponent of 3 is acceptable.  However other
3520 		 * applications in the future may wish to leverage DNS
3521 		 * distributed keys for applications that do require
3522 		 * confidentiality.  For keys which might have such other uses,
3523 		 * a more conservative choice would be 65537 (F4, the fourth
3524 		 * fermat number).
3525 		 */
3526 		if (dnskey.datalen > 1 && dnskey.data[0] == 1 &&
3527 		    dnskey.data[1] == 3) {
3528 			const char *algorithm = "";
3529 			isc_region_t r;
3530 			bool logit = true;
3531 
3532 			dns_rdata_toregion(&rdata, &r);
3533 
3534 			switch (dnskey.algorithm) {
3535 			case DNS_KEYALG_RSAMD5:
3536 				algorithm = "RSAMD5";
3537 				break;
3538 			case DNS_KEYALG_RSASHA1:
3539 				algorithm = "RSASHA1";
3540 				break;
3541 			case DNS_KEYALG_NSEC3RSASHA1:
3542 				algorithm = "NSEC3RSASHA1";
3543 				break;
3544 			case DNS_KEYALG_RSASHA256:
3545 				algorithm = "RSASHA236";
3546 				break;
3547 			case DNS_KEYALG_RSASHA512:
3548 				algorithm = "RSASHA512";
3549 				break;
3550 			default:
3551 				logit = false;
3552 				break;
3553 			}
3554 
3555 			if (logit) {
3556 				dnssec_log(zone, ISC_LOG_WARNING,
3557 					   "weak %s (%u) key found "
3558 					   "(exponent=3, id=%u)",
3559 					   algorithm, dnskey.algorithm,
3560 					   dst_region_computeid(&r));
3561 			}
3562 		}
3563 		dns_rdata_reset(&rdata);
3564 	}
3565 	dns_rdataset_disassociate(&rdataset);
3566 
3567 cleanup:
3568 	if (node != NULL) {
3569 		dns_db_detachnode(db, &node);
3570 	}
3571 	if (version != NULL) {
3572 		dns_db_closeversion(db, &version, false);
3573 	}
3574 }
3575 
3576 static void
resume_signingwithkey(dns_zone_t * zone)3577 resume_signingwithkey(dns_zone_t *zone) {
3578 	dns_dbnode_t *node = NULL;
3579 	dns_dbversion_t *version = NULL;
3580 	dns_rdata_t rdata = DNS_RDATA_INIT;
3581 	dns_rdataset_t rdataset;
3582 	isc_result_t result;
3583 	dns_db_t *db = NULL;
3584 
3585 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
3586 	if (zone->db != NULL) {
3587 		dns_db_attach(zone->db, &db);
3588 	}
3589 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
3590 	if (db == NULL) {
3591 		goto cleanup;
3592 	}
3593 
3594 	result = dns_db_findnode(db, &zone->origin, false, &node);
3595 	if (result != ISC_R_SUCCESS) {
3596 		goto cleanup;
3597 	}
3598 
3599 	dns_db_currentversion(db, &version);
3600 	dns_rdataset_init(&rdataset);
3601 	result = dns_db_findrdataset(db, node, version, zone->privatetype,
3602 				     dns_rdatatype_none, 0, &rdataset, NULL);
3603 	if (result != ISC_R_SUCCESS) {
3604 		INSIST(!dns_rdataset_isassociated(&rdataset));
3605 		goto cleanup;
3606 	}
3607 
3608 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
3609 	     result = dns_rdataset_next(&rdataset))
3610 	{
3611 		dns_rdataset_current(&rdataset, &rdata);
3612 		if (rdata.length != 5 || rdata.data[0] == 0 ||
3613 		    rdata.data[4] != 0) {
3614 			dns_rdata_reset(&rdata);
3615 			continue;
3616 		}
3617 
3618 		result = zone_signwithkey(zone, rdata.data[0],
3619 					  (rdata.data[1] << 8) | rdata.data[2],
3620 					  rdata.data[3]);
3621 		if (result != ISC_R_SUCCESS) {
3622 			dnssec_log(zone, ISC_LOG_ERROR,
3623 				   "zone_signwithkey failed: %s",
3624 				   dns_result_totext(result));
3625 		}
3626 		dns_rdata_reset(&rdata);
3627 	}
3628 	dns_rdataset_disassociate(&rdataset);
3629 
3630 cleanup:
3631 	if (db != NULL) {
3632 		if (node != NULL) {
3633 			dns_db_detachnode(db, &node);
3634 		}
3635 		if (version != NULL) {
3636 			dns_db_closeversion(db, &version, false);
3637 		}
3638 		dns_db_detach(&db);
3639 	}
3640 }
3641 
3642 /*
3643  * Initiate adding/removing NSEC3 records belonging to the chain defined by the
3644  * supplied NSEC3PARAM RDATA.
3645  *
3646  * Zone must be locked by caller.
3647  */
3648 static isc_result_t
zone_addnsec3chain(dns_zone_t * zone,dns_rdata_nsec3param_t * nsec3param)3649 zone_addnsec3chain(dns_zone_t *zone, dns_rdata_nsec3param_t *nsec3param) {
3650 	dns_nsec3chain_t *nsec3chain, *current;
3651 	dns_dbversion_t *version = NULL;
3652 	bool nseconly = false, nsec3ok = false;
3653 	isc_result_t result;
3654 	isc_time_t now;
3655 	unsigned int options = 0;
3656 	char saltbuf[255 * 2 + 1];
3657 	char flags[sizeof("INITIAL|REMOVE|CREATE|NONSEC|OPTOUT")];
3658 	dns_db_t *db = NULL;
3659 
3660 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
3661 	if (zone->db != NULL) {
3662 		dns_db_attach(zone->db, &db);
3663 	}
3664 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
3665 
3666 	if (db == NULL) {
3667 		result = ISC_R_SUCCESS;
3668 		goto cleanup;
3669 	}
3670 
3671 	/*
3672 	 * If this zone is not NSEC3-capable, attempting to remove any NSEC3
3673 	 * chain from it is pointless as it would not be possible for the
3674 	 * latter to exist in the first place.
3675 	 */
3676 	dns_db_currentversion(db, &version);
3677 	result = dns_nsec_nseconly(db, version, &nseconly);
3678 	nsec3ok = (result == ISC_R_SUCCESS && !nseconly);
3679 	dns_db_closeversion(db, &version, false);
3680 	if (!nsec3ok && (nsec3param->flags & DNS_NSEC3FLAG_REMOVE) == 0) {
3681 		result = ISC_R_SUCCESS;
3682 		goto cleanup;
3683 	}
3684 
3685 	/*
3686 	 * Allocate and initialize structure preserving state of
3687 	 * adding/removing records belonging to this NSEC3 chain between
3688 	 * separate zone_nsec3chain() calls.
3689 	 */
3690 	nsec3chain = isc_mem_get(zone->mctx, sizeof *nsec3chain);
3691 
3692 	nsec3chain->magic = 0;
3693 	nsec3chain->done = false;
3694 	nsec3chain->db = NULL;
3695 	nsec3chain->dbiterator = NULL;
3696 	nsec3chain->nsec3param.common.rdclass = nsec3param->common.rdclass;
3697 	nsec3chain->nsec3param.common.rdtype = nsec3param->common.rdtype;
3698 	nsec3chain->nsec3param.hash = nsec3param->hash;
3699 	nsec3chain->nsec3param.iterations = nsec3param->iterations;
3700 	nsec3chain->nsec3param.flags = nsec3param->flags;
3701 	nsec3chain->nsec3param.salt_length = nsec3param->salt_length;
3702 	memmove(nsec3chain->salt, nsec3param->salt, nsec3param->salt_length);
3703 	nsec3chain->nsec3param.salt = nsec3chain->salt;
3704 	nsec3chain->seen_nsec = false;
3705 	nsec3chain->delete_nsec = false;
3706 	nsec3chain->save_delete_nsec = false;
3707 
3708 	/*
3709 	 * Log NSEC3 parameters defined by supplied NSEC3PARAM RDATA.
3710 	 */
3711 	if (nsec3param->flags == 0) {
3712 		strlcpy(flags, "NONE", sizeof(flags));
3713 	} else {
3714 		flags[0] = '\0';
3715 		if ((nsec3param->flags & DNS_NSEC3FLAG_REMOVE) != 0) {
3716 			strlcat(flags, "REMOVE", sizeof(flags));
3717 		}
3718 		if ((nsec3param->flags & DNS_NSEC3FLAG_INITIAL) != 0) {
3719 			if (flags[0] == '\0') {
3720 				strlcpy(flags, "INITIAL", sizeof(flags));
3721 			} else {
3722 				strlcat(flags, "|INITIAL", sizeof(flags));
3723 			}
3724 		}
3725 		if ((nsec3param->flags & DNS_NSEC3FLAG_CREATE) != 0) {
3726 			if (flags[0] == '\0') {
3727 				strlcpy(flags, "CREATE", sizeof(flags));
3728 			} else {
3729 				strlcat(flags, "|CREATE", sizeof(flags));
3730 			}
3731 		}
3732 		if ((nsec3param->flags & DNS_NSEC3FLAG_NONSEC) != 0) {
3733 			if (flags[0] == '\0') {
3734 				strlcpy(flags, "NONSEC", sizeof(flags));
3735 			} else {
3736 				strlcat(flags, "|NONSEC", sizeof(flags));
3737 			}
3738 		}
3739 		if ((nsec3param->flags & DNS_NSEC3FLAG_OPTOUT) != 0) {
3740 			if (flags[0] == '\0') {
3741 				strlcpy(flags, "OPTOUT", sizeof(flags));
3742 			} else {
3743 				strlcat(flags, "|OPTOUT", sizeof(flags));
3744 			}
3745 		}
3746 	}
3747 	result = dns_nsec3param_salttotext(nsec3param, saltbuf,
3748 					   sizeof(saltbuf));
3749 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
3750 	dnssec_log(zone, ISC_LOG_INFO, "zone_addnsec3chain(%u,%s,%u,%s)",
3751 		   nsec3param->hash, flags, nsec3param->iterations, saltbuf);
3752 
3753 	/*
3754 	 * If the NSEC3 chain defined by the supplied NSEC3PARAM RDATA is
3755 	 * currently being processed, interrupt its processing to avoid
3756 	 * simultaneously adding and removing records for the same NSEC3 chain.
3757 	 */
3758 	for (current = ISC_LIST_HEAD(zone->nsec3chain); current != NULL;
3759 	     current = ISC_LIST_NEXT(current, link))
3760 	{
3761 		if ((current->db == db) &&
3762 		    (current->nsec3param.hash == nsec3param->hash) &&
3763 		    (current->nsec3param.iterations ==
3764 		     nsec3param->iterations) &&
3765 		    (current->nsec3param.salt_length ==
3766 		     nsec3param->salt_length) &&
3767 		    memcmp(current->nsec3param.salt, nsec3param->salt,
3768 			   nsec3param->salt_length) == 0)
3769 		{
3770 			current->done = true;
3771 		}
3772 	}
3773 
3774 	/*
3775 	 * Attach zone database to the structure initialized above and create
3776 	 * an iterator for it with appropriate options in order to avoid
3777 	 * creating NSEC3 records for NSEC3 records.
3778 	 */
3779 	dns_db_attach(db, &nsec3chain->db);
3780 	if ((nsec3chain->nsec3param.flags & DNS_NSEC3FLAG_CREATE) != 0) {
3781 		options = DNS_DB_NONSEC3;
3782 	}
3783 	result = dns_db_createiterator(nsec3chain->db, options,
3784 				       &nsec3chain->dbiterator);
3785 	if (result == ISC_R_SUCCESS) {
3786 		result = dns_dbiterator_first(nsec3chain->dbiterator);
3787 	}
3788 	if (result == ISC_R_SUCCESS) {
3789 		/*
3790 		 * Database iterator initialization succeeded.  We are now
3791 		 * ready to kick off adding/removing records belonging to this
3792 		 * NSEC3 chain.  Append the structure initialized above to the
3793 		 * "nsec3chain" list for the zone and set the appropriate zone
3794 		 * timer so that zone_nsec3chain() is called as soon as
3795 		 * possible.
3796 		 */
3797 		dns_dbiterator_pause(nsec3chain->dbiterator);
3798 		ISC_LIST_INITANDAPPEND(zone->nsec3chain, nsec3chain, link);
3799 		nsec3chain = NULL;
3800 		if (isc_time_isepoch(&zone->nsec3chaintime)) {
3801 			TIME_NOW(&now);
3802 			zone->nsec3chaintime = now;
3803 			if (zone->task != NULL) {
3804 				zone_settimer(zone, &now);
3805 			}
3806 		}
3807 	}
3808 
3809 	if (nsec3chain != NULL) {
3810 		if (nsec3chain->db != NULL) {
3811 			dns_db_detach(&nsec3chain->db);
3812 		}
3813 		if (nsec3chain->dbiterator != NULL) {
3814 			dns_dbiterator_destroy(&nsec3chain->dbiterator);
3815 		}
3816 		isc_mem_put(zone->mctx, nsec3chain, sizeof *nsec3chain);
3817 	}
3818 
3819 cleanup:
3820 	if (db != NULL) {
3821 		dns_db_detach(&db);
3822 	}
3823 	return (result);
3824 }
3825 
3826 /*
3827  * Find private-type records at the zone apex which signal that an NSEC3 chain
3828  * should be added or removed.  For each such record, extract NSEC3PARAM RDATA
3829  * and pass it to zone_addnsec3chain().
3830  *
3831  * Zone must be locked by caller.
3832  */
3833 static void
resume_addnsec3chain(dns_zone_t * zone)3834 resume_addnsec3chain(dns_zone_t *zone) {
3835 	dns_dbnode_t *node = NULL;
3836 	dns_dbversion_t *version = NULL;
3837 	dns_rdataset_t rdataset;
3838 	isc_result_t result;
3839 	dns_rdata_nsec3param_t nsec3param;
3840 	bool nseconly = false, nsec3ok = false;
3841 	dns_db_t *db = NULL;
3842 
3843 	INSIST(LOCKED_ZONE(zone));
3844 
3845 	if (zone->privatetype == 0) {
3846 		return;
3847 	}
3848 
3849 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
3850 	if (zone->db != NULL) {
3851 		dns_db_attach(zone->db, &db);
3852 	}
3853 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
3854 	if (db == NULL) {
3855 		goto cleanup;
3856 	}
3857 
3858 	result = dns_db_findnode(db, &zone->origin, false, &node);
3859 	if (result != ISC_R_SUCCESS) {
3860 		goto cleanup;
3861 	}
3862 
3863 	dns_db_currentversion(db, &version);
3864 
3865 	/*
3866 	 * In order to create NSEC3 chains we need the DNSKEY RRset at zone
3867 	 * apex to exist and contain no keys using NSEC-only algorithms.
3868 	 */
3869 	result = dns_nsec_nseconly(db, version, &nseconly);
3870 	nsec3ok = (result == ISC_R_SUCCESS && !nseconly);
3871 
3872 	/*
3873 	 * Get the RRset containing all private-type records at the zone apex.
3874 	 */
3875 	dns_rdataset_init(&rdataset);
3876 	result = dns_db_findrdataset(db, node, version, zone->privatetype,
3877 				     dns_rdatatype_none, 0, &rdataset, NULL);
3878 	if (result != ISC_R_SUCCESS) {
3879 		INSIST(!dns_rdataset_isassociated(&rdataset));
3880 		goto cleanup;
3881 	}
3882 
3883 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
3884 	     result = dns_rdataset_next(&rdataset))
3885 	{
3886 		unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
3887 		dns_rdata_t rdata = DNS_RDATA_INIT;
3888 		dns_rdata_t private = DNS_RDATA_INIT;
3889 
3890 		dns_rdataset_current(&rdataset, &private);
3891 		/*
3892 		 * Try extracting NSEC3PARAM RDATA from this private-type
3893 		 * record.  Failure means this private-type record does not
3894 		 * represent an NSEC3PARAM record, so skip it.
3895 		 */
3896 		if (!dns_nsec3param_fromprivate(&private, &rdata, buf,
3897 						sizeof(buf))) {
3898 			continue;
3899 		}
3900 		result = dns_rdata_tostruct(&rdata, &nsec3param, NULL);
3901 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
3902 		if (((nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0) ||
3903 		    ((nsec3param.flags & DNS_NSEC3FLAG_CREATE) != 0 && nsec3ok))
3904 		{
3905 			/*
3906 			 * Pass the NSEC3PARAM RDATA contained in this
3907 			 * private-type record to zone_addnsec3chain() so that
3908 			 * it can kick off adding or removing NSEC3 records.
3909 			 */
3910 			result = zone_addnsec3chain(zone, &nsec3param);
3911 			if (result != ISC_R_SUCCESS) {
3912 				dnssec_log(zone, ISC_LOG_ERROR,
3913 					   "zone_addnsec3chain failed: %s",
3914 					   dns_result_totext(result));
3915 			}
3916 		}
3917 	}
3918 	dns_rdataset_disassociate(&rdataset);
3919 
3920 cleanup:
3921 	if (db != NULL) {
3922 		if (node != NULL) {
3923 			dns_db_detachnode(db, &node);
3924 		}
3925 		if (version != NULL) {
3926 			dns_db_closeversion(db, &version, false);
3927 		}
3928 		dns_db_detach(&db);
3929 	}
3930 }
3931 
3932 static void
set_resigntime(dns_zone_t * zone)3933 set_resigntime(dns_zone_t *zone) {
3934 	dns_rdataset_t rdataset;
3935 	dns_fixedname_t fixed;
3936 	unsigned int resign;
3937 	isc_result_t result;
3938 	uint32_t nanosecs;
3939 	dns_db_t *db = NULL;
3940 
3941 	INSIST(LOCKED_ZONE(zone));
3942 
3943 	/* We only re-sign zones that can be dynamically updated */
3944 	if (zone->update_disabled) {
3945 		return;
3946 	}
3947 
3948 	if (!inline_secure(zone) &&
3949 	    (zone->type != dns_zone_primary ||
3950 	     (zone->ssutable == NULL &&
3951 	      (zone->update_acl == NULL || dns_acl_isnone(zone->update_acl)))))
3952 	{
3953 		return;
3954 	}
3955 
3956 	dns_rdataset_init(&rdataset);
3957 	dns_fixedname_init(&fixed);
3958 
3959 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
3960 	if (zone->db != NULL) {
3961 		dns_db_attach(zone->db, &db);
3962 	}
3963 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
3964 	if (db == NULL) {
3965 		isc_time_settoepoch(&zone->resigntime);
3966 		return;
3967 	}
3968 
3969 	result = dns_db_getsigningtime(db, &rdataset,
3970 				       dns_fixedname_name(&fixed));
3971 	if (result != ISC_R_SUCCESS) {
3972 		isc_time_settoepoch(&zone->resigntime);
3973 		goto cleanup;
3974 	}
3975 
3976 	resign = rdataset.resign - dns_zone_getsigresigninginterval(zone);
3977 	dns_rdataset_disassociate(&rdataset);
3978 	nanosecs = isc_random_uniform(1000000000);
3979 	isc_time_set(&zone->resigntime, resign, nanosecs);
3980 
3981 cleanup:
3982 	dns_db_detach(&db);
3983 	return;
3984 }
3985 
3986 static isc_result_t
check_nsec3param(dns_zone_t * zone,dns_db_t * db)3987 check_nsec3param(dns_zone_t *zone, dns_db_t *db) {
3988 	bool ok = false;
3989 	dns_dbnode_t *node = NULL;
3990 	dns_dbversion_t *version = NULL;
3991 	dns_rdata_nsec3param_t nsec3param;
3992 	dns_rdataset_t rdataset;
3993 	isc_result_t result;
3994 	bool dynamic = (zone->type == dns_zone_primary)
3995 			       ? dns_zone_isdynamic(zone, false)
3996 			       : false;
3997 
3998 	dns_rdataset_init(&rdataset);
3999 	result = dns_db_findnode(db, &zone->origin, false, &node);
4000 	if (result != ISC_R_SUCCESS) {
4001 		dns_zone_log(zone, ISC_LOG_ERROR,
4002 			     "nsec3param lookup failure: %s",
4003 			     dns_result_totext(result));
4004 		return (result);
4005 	}
4006 	dns_db_currentversion(db, &version);
4007 
4008 	result = dns_db_findrdataset(db, node, version,
4009 				     dns_rdatatype_nsec3param,
4010 				     dns_rdatatype_none, 0, &rdataset, NULL);
4011 	if (result == ISC_R_NOTFOUND) {
4012 		INSIST(!dns_rdataset_isassociated(&rdataset));
4013 		result = ISC_R_SUCCESS;
4014 		goto cleanup;
4015 	}
4016 	if (result != ISC_R_SUCCESS) {
4017 		INSIST(!dns_rdataset_isassociated(&rdataset));
4018 		dns_zone_log(zone, ISC_LOG_ERROR,
4019 			     "nsec3param lookup failure: %s",
4020 			     dns_result_totext(result));
4021 		goto cleanup;
4022 	}
4023 
4024 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
4025 	     result = dns_rdataset_next(&rdataset))
4026 	{
4027 		dns_rdata_t rdata = DNS_RDATA_INIT;
4028 
4029 		dns_rdataset_current(&rdataset, &rdata);
4030 		result = dns_rdata_tostruct(&rdata, &nsec3param, NULL);
4031 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
4032 
4033 		/*
4034 		 * For dynamic zones we must support every algorithm so we
4035 		 * can regenerate all the NSEC3 chains.
4036 		 * For non-dynamic zones we only need to find a supported
4037 		 * algorithm.
4038 		 */
4039 		if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NSEC3TESTZONE) &&
4040 		    nsec3param.hash == DNS_NSEC3_UNKNOWNALG && !dynamic)
4041 		{
4042 			dns_zone_log(zone, ISC_LOG_WARNING,
4043 				     "nsec3 test \"unknown\" hash algorithm "
4044 				     "found: %u",
4045 				     nsec3param.hash);
4046 			ok = true;
4047 		} else if (!dns_nsec3_supportedhash(nsec3param.hash)) {
4048 			if (dynamic) {
4049 				dns_zone_log(zone, ISC_LOG_ERROR,
4050 					     "unsupported nsec3 hash algorithm"
4051 					     " in dynamic zone: %u",
4052 					     nsec3param.hash);
4053 				result = DNS_R_BADZONE;
4054 				/* Stop second error message. */
4055 				ok = true;
4056 				break;
4057 			} else {
4058 				dns_zone_log(zone, ISC_LOG_WARNING,
4059 					     "unsupported nsec3 hash "
4060 					     "algorithm: %u",
4061 					     nsec3param.hash);
4062 			}
4063 		} else {
4064 			ok = true;
4065 		}
4066 
4067 		/*
4068 		 * Warn if the zone has excessive NSEC3 iterations.
4069 		 */
4070 		if (nsec3param.iterations > dns_nsec3_maxiterations()) {
4071 			dnssec_log(zone, ISC_LOG_WARNING,
4072 				   "excessive NSEC3PARAM iterations %u > %u",
4073 				   nsec3param.iterations,
4074 				   dns_nsec3_maxiterations());
4075 		}
4076 	}
4077 	if (result == ISC_R_NOMORE) {
4078 		result = ISC_R_SUCCESS;
4079 	}
4080 
4081 	if (!ok) {
4082 		result = DNS_R_BADZONE;
4083 		dns_zone_log(zone, ISC_LOG_ERROR,
4084 			     "no supported nsec3 hash algorithm");
4085 	}
4086 
4087 cleanup:
4088 	if (dns_rdataset_isassociated(&rdataset)) {
4089 		dns_rdataset_disassociate(&rdataset);
4090 	}
4091 	dns_db_closeversion(db, &version, false);
4092 	dns_db_detachnode(db, &node);
4093 	return (result);
4094 }
4095 
4096 /*
4097  * Set the timer for refreshing the key zone to the soonest future time
4098  * of the set (current timer, keydata->refresh, keydata->addhd,
4099  * keydata->removehd).
4100  */
4101 static void
set_refreshkeytimer(dns_zone_t * zone,dns_rdata_keydata_t * key,isc_stdtime_t now,bool force)4102 set_refreshkeytimer(dns_zone_t *zone, dns_rdata_keydata_t *key,
4103 		    isc_stdtime_t now, bool force) {
4104 	const char me[] = "set_refreshkeytimer";
4105 	isc_stdtime_t then;
4106 	isc_time_t timenow, timethen;
4107 	char timebuf[80];
4108 
4109 	ENTER;
4110 	then = key->refresh;
4111 	if (force) {
4112 		then = now;
4113 	}
4114 	if (key->addhd > now && key->addhd < then) {
4115 		then = key->addhd;
4116 	}
4117 	if (key->removehd > now && key->removehd < then) {
4118 		then = key->removehd;
4119 	}
4120 
4121 	TIME_NOW(&timenow);
4122 	if (then > now) {
4123 		DNS_ZONE_TIME_ADD(&timenow, then - now, &timethen);
4124 	} else {
4125 		timethen = timenow;
4126 	}
4127 	if (isc_time_compare(&zone->refreshkeytime, &timenow) < 0 ||
4128 	    isc_time_compare(&timethen, &zone->refreshkeytime) < 0)
4129 	{
4130 		zone->refreshkeytime = timethen;
4131 	}
4132 
4133 	isc_time_formattimestamp(&zone->refreshkeytime, timebuf, 80);
4134 	dns_zone_log(zone, ISC_LOG_DEBUG(1), "next key refresh: %s", timebuf);
4135 	zone_settimer(zone, &timenow);
4136 }
4137 
4138 /*
4139  * If keynode references a key or a DS rdataset, and if the key
4140  * zone does not contain a KEYDATA record for the corresponding name,
4141  * then create an empty KEYDATA and push it into the zone as a placeholder,
4142  * then schedule a key refresh immediately. This new KEYDATA record will be
4143  * updated during the refresh.
4144  *
4145  * If the key zone is changed, set '*changed' to true.
4146  */
4147 static isc_result_t
create_keydata(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,dns_diff_t * diff,dns_keynode_t * keynode,dns_name_t * keyname,bool * changed)4148 create_keydata(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
4149 	       dns_diff_t *diff, dns_keynode_t *keynode, dns_name_t *keyname,
4150 	       bool *changed) {
4151 	const char me[] = "create_keydata";
4152 	isc_result_t result = ISC_R_SUCCESS;
4153 	dns_rdata_t rdata = DNS_RDATA_INIT;
4154 	dns_rdata_keydata_t kd;
4155 	unsigned char rrdata[4096];
4156 	isc_buffer_t rrdatabuf;
4157 	isc_stdtime_t now;
4158 
4159 	REQUIRE(keynode != NULL);
4160 
4161 	ENTER;
4162 	isc_stdtime_get(&now);
4163 
4164 	/*
4165 	 * If the keynode has no trust anchor set, we shouldn't be here.
4166 	 */
4167 	if (!dns_keynode_dsset(keynode, NULL)) {
4168 		return (ISC_R_FAILURE);
4169 	}
4170 
4171 	memset(&kd, 0, sizeof(kd));
4172 	kd.common.rdclass = zone->rdclass;
4173 	kd.common.rdtype = dns_rdatatype_keydata;
4174 	ISC_LINK_INIT(&kd.common, link);
4175 
4176 	isc_buffer_init(&rrdatabuf, rrdata, sizeof(rrdata));
4177 
4178 	CHECK(dns_rdata_fromstruct(&rdata, zone->rdclass, dns_rdatatype_keydata,
4179 				   &kd, &rrdatabuf));
4180 	/* Add rdata to zone. */
4181 	CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADD, keyname, 0, &rdata));
4182 	*changed = true;
4183 
4184 	/* Refresh new keys from the zone apex as soon as possible. */
4185 	set_refreshkeytimer(zone, &kd, now, true);
4186 	return (ISC_R_SUCCESS);
4187 
4188 failure:
4189 	return (result);
4190 }
4191 
4192 /*
4193  * Remove from the key zone all the KEYDATA records found in rdataset.
4194  */
4195 static isc_result_t
delete_keydata(dns_db_t * db,dns_dbversion_t * ver,dns_diff_t * diff,dns_name_t * name,dns_rdataset_t * rdataset)4196 delete_keydata(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
4197 	       dns_name_t *name, dns_rdataset_t *rdataset) {
4198 	dns_rdata_t rdata = DNS_RDATA_INIT;
4199 	isc_result_t result, uresult;
4200 
4201 	for (result = dns_rdataset_first(rdataset); result == ISC_R_SUCCESS;
4202 	     result = dns_rdataset_next(rdataset))
4203 	{
4204 		dns_rdata_reset(&rdata);
4205 		dns_rdataset_current(rdataset, &rdata);
4206 		uresult = update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name, 0,
4207 					&rdata);
4208 		if (uresult != ISC_R_SUCCESS) {
4209 			return (uresult);
4210 		}
4211 	}
4212 	if (result == ISC_R_NOMORE) {
4213 		result = ISC_R_SUCCESS;
4214 	}
4215 	return (result);
4216 }
4217 
4218 /*
4219  * Compute the DNSSEC key ID for a DNSKEY record.
4220  */
4221 static isc_result_t
compute_tag(dns_name_t * name,dns_rdata_dnskey_t * dnskey,isc_mem_t * mctx,dns_keytag_t * tag)4222 compute_tag(dns_name_t *name, dns_rdata_dnskey_t *dnskey, isc_mem_t *mctx,
4223 	    dns_keytag_t *tag) {
4224 	isc_result_t result;
4225 	dns_rdata_t rdata = DNS_RDATA_INIT;
4226 	unsigned char data[4096];
4227 	isc_buffer_t buffer;
4228 	dst_key_t *dstkey = NULL;
4229 
4230 	isc_buffer_init(&buffer, data, sizeof(data));
4231 	dns_rdata_fromstruct(&rdata, dnskey->common.rdclass,
4232 			     dns_rdatatype_dnskey, dnskey, &buffer);
4233 
4234 	result = dns_dnssec_keyfromrdata(name, &rdata, mctx, &dstkey);
4235 	if (result == ISC_R_SUCCESS) {
4236 		*tag = dst_key_id(dstkey);
4237 		dst_key_free(&dstkey);
4238 	}
4239 
4240 	return (result);
4241 }
4242 
4243 /*
4244  * Add key to the security roots.
4245  */
4246 static void
trust_key(dns_zone_t * zone,dns_name_t * keyname,dns_rdata_dnskey_t * dnskey,bool initial)4247 trust_key(dns_zone_t *zone, dns_name_t *keyname, dns_rdata_dnskey_t *dnskey,
4248 	  bool initial) {
4249 	isc_result_t result;
4250 	dns_rdata_t rdata = DNS_RDATA_INIT;
4251 	unsigned char data[4096], digest[ISC_MAX_MD_SIZE];
4252 	isc_buffer_t buffer;
4253 	dns_keytable_t *sr = NULL;
4254 	dns_rdata_ds_t ds;
4255 
4256 	result = dns_view_getsecroots(zone->view, &sr);
4257 	if (result != ISC_R_SUCCESS) {
4258 		return;
4259 	}
4260 
4261 	/* Build DS record for key. */
4262 	isc_buffer_init(&buffer, data, sizeof(data));
4263 	dns_rdata_fromstruct(&rdata, dnskey->common.rdclass,
4264 			     dns_rdatatype_dnskey, dnskey, &buffer);
4265 	CHECK(dns_ds_fromkeyrdata(keyname, &rdata, DNS_DSDIGEST_SHA256, digest,
4266 				  &ds));
4267 	CHECK(dns_keytable_add(sr, true, initial, keyname, &ds));
4268 
4269 	dns_keytable_detach(&sr);
4270 
4271 failure:
4272 	if (sr != NULL) {
4273 		dns_keytable_detach(&sr);
4274 	}
4275 	return;
4276 }
4277 
4278 /*
4279  * Add a null key to the security roots for so that all queries
4280  * to the zone will fail.
4281  */
4282 static void
fail_secure(dns_zone_t * zone,dns_name_t * keyname)4283 fail_secure(dns_zone_t *zone, dns_name_t *keyname) {
4284 	isc_result_t result;
4285 	dns_keytable_t *sr = NULL;
4286 
4287 	result = dns_view_getsecroots(zone->view, &sr);
4288 	if (result == ISC_R_SUCCESS) {
4289 		dns_keytable_marksecure(sr, keyname);
4290 		dns_keytable_detach(&sr);
4291 	}
4292 }
4293 
4294 /*
4295  * Scan a set of KEYDATA records from the key zone.  The ones that are
4296  * valid (i.e., the add holddown timer has expired) become trusted keys.
4297  */
4298 static void
load_secroots(dns_zone_t * zone,dns_name_t * name,dns_rdataset_t * rdataset)4299 load_secroots(dns_zone_t *zone, dns_name_t *name, dns_rdataset_t *rdataset) {
4300 	isc_result_t result;
4301 	dns_rdata_t rdata = DNS_RDATA_INIT;
4302 	dns_rdata_keydata_t keydata;
4303 	dns_rdata_dnskey_t dnskey;
4304 	int trusted = 0, revoked = 0, pending = 0;
4305 	isc_stdtime_t now;
4306 	dns_keytable_t *sr = NULL;
4307 
4308 	isc_stdtime_get(&now);
4309 
4310 	result = dns_view_getsecroots(zone->view, &sr);
4311 	if (result == ISC_R_SUCCESS) {
4312 		dns_keytable_delete(sr, name);
4313 		dns_keytable_detach(&sr);
4314 	}
4315 
4316 	/* Now insert all the accepted trust anchors from this keydata set. */
4317 	for (result = dns_rdataset_first(rdataset); result == ISC_R_SUCCESS;
4318 	     result = dns_rdataset_next(rdataset))
4319 	{
4320 		dns_rdata_reset(&rdata);
4321 		dns_rdataset_current(rdataset, &rdata);
4322 
4323 		/* Convert rdata to keydata. */
4324 		result = dns_rdata_tostruct(&rdata, &keydata, NULL);
4325 		if (result == ISC_R_UNEXPECTEDEND) {
4326 			continue;
4327 		}
4328 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
4329 
4330 		/* Set the key refresh timer to force a fast refresh. */
4331 		set_refreshkeytimer(zone, &keydata, now, true);
4332 
4333 		/* If the removal timer is nonzero, this key was revoked. */
4334 		if (keydata.removehd != 0) {
4335 			revoked++;
4336 			continue;
4337 		}
4338 
4339 		/*
4340 		 * If the add timer is still pending, this key is not
4341 		 * trusted yet.
4342 		 */
4343 		if (now < keydata.addhd) {
4344 			pending++;
4345 			continue;
4346 		}
4347 
4348 		/* Convert keydata to dnskey. */
4349 		dns_keydata_todnskey(&keydata, &dnskey, NULL);
4350 
4351 		/* Add to keytables. */
4352 		trusted++;
4353 		trust_key(zone, name, &dnskey, (keydata.addhd == 0));
4354 	}
4355 
4356 	if (trusted == 0 && pending != 0) {
4357 		char namebuf[DNS_NAME_FORMATSIZE];
4358 		dns_name_format(name, namebuf, sizeof namebuf);
4359 		dnssec_log(zone, ISC_LOG_ERROR,
4360 			   "No valid trust anchors for '%s'!", namebuf);
4361 		dnssec_log(zone, ISC_LOG_ERROR,
4362 			   "%d key(s) revoked, %d still pending", revoked,
4363 			   pending);
4364 		dnssec_log(zone, ISC_LOG_ERROR, "All queries to '%s' will fail",
4365 			   namebuf);
4366 		fail_secure(zone, name);
4367 	}
4368 }
4369 
4370 static isc_result_t
do_one_tuple(dns_difftuple_t ** tuple,dns_db_t * db,dns_dbversion_t * ver,dns_diff_t * diff)4371 do_one_tuple(dns_difftuple_t **tuple, dns_db_t *db, dns_dbversion_t *ver,
4372 	     dns_diff_t *diff) {
4373 	dns_diff_t temp_diff;
4374 	isc_result_t result;
4375 
4376 	/*
4377 	 * Create a singleton diff.
4378 	 */
4379 	dns_diff_init(diff->mctx, &temp_diff);
4380 	ISC_LIST_APPEND(temp_diff.tuples, *tuple, link);
4381 
4382 	/*
4383 	 * Apply it to the database.
4384 	 */
4385 	result = dns_diff_apply(&temp_diff, db, ver);
4386 	ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link);
4387 	if (result != ISC_R_SUCCESS) {
4388 		dns_difftuple_free(tuple);
4389 		return (result);
4390 	}
4391 
4392 	/*
4393 	 * Merge it into the current pending journal entry.
4394 	 */
4395 	dns_diff_appendminimal(diff, tuple);
4396 
4397 	/*
4398 	 * Do not clear temp_diff.
4399 	 */
4400 	return (ISC_R_SUCCESS);
4401 }
4402 
4403 static isc_result_t
update_one_rr(dns_db_t * db,dns_dbversion_t * ver,dns_diff_t * diff,dns_diffop_t op,dns_name_t * name,dns_ttl_t ttl,dns_rdata_t * rdata)4404 update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff,
4405 	      dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl,
4406 	      dns_rdata_t *rdata) {
4407 	dns_difftuple_t *tuple = NULL;
4408 	isc_result_t result;
4409 	result = dns_difftuple_create(diff->mctx, op, name, ttl, rdata, &tuple);
4410 	if (result != ISC_R_SUCCESS) {
4411 		return (result);
4412 	}
4413 	return (do_one_tuple(&tuple, db, ver, diff));
4414 }
4415 
4416 static isc_result_t
update_soa_serial(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,dns_diff_t * diff,isc_mem_t * mctx,dns_updatemethod_t method)4417 update_soa_serial(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
4418 		  dns_diff_t *diff, isc_mem_t *mctx,
4419 		  dns_updatemethod_t method) {
4420 	dns_difftuple_t *deltuple = NULL;
4421 	dns_difftuple_t *addtuple = NULL;
4422 	uint32_t serial;
4423 	isc_result_t result;
4424 	dns_updatemethod_t used = dns_updatemethod_none;
4425 
4426 	INSIST(method != dns_updatemethod_none);
4427 
4428 	CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_DEL, &deltuple));
4429 	CHECK(dns_difftuple_copy(deltuple, &addtuple));
4430 	addtuple->op = DNS_DIFFOP_ADD;
4431 
4432 	serial = dns_soa_getserial(&addtuple->rdata);
4433 	serial = dns_update_soaserial(serial, method, &used);
4434 	if (method != used) {
4435 		dns_zone_log(zone, ISC_LOG_WARNING,
4436 			     "update_soa_serial:new serial would be lower than "
4437 			     "old serial, using increment method instead");
4438 	}
4439 	dns_soa_setserial(serial, &addtuple->rdata);
4440 	CHECK(do_one_tuple(&deltuple, db, ver, diff));
4441 	CHECK(do_one_tuple(&addtuple, db, ver, diff));
4442 	result = ISC_R_SUCCESS;
4443 
4444 failure:
4445 	if (addtuple != NULL) {
4446 		dns_difftuple_free(&addtuple);
4447 	}
4448 	if (deltuple != NULL) {
4449 		dns_difftuple_free(&deltuple);
4450 	}
4451 	return (result);
4452 }
4453 
4454 /*
4455  * Write all transactions in 'diff' to the zone journal file.
4456  */
4457 static isc_result_t
zone_journal(dns_zone_t * zone,dns_diff_t * diff,uint32_t * sourceserial,const char * caller)4458 zone_journal(dns_zone_t *zone, dns_diff_t *diff, uint32_t *sourceserial,
4459 	     const char *caller) {
4460 	const char me[] = "zone_journal";
4461 	const char *journalfile;
4462 	isc_result_t result = ISC_R_SUCCESS;
4463 	dns_journal_t *journal = NULL;
4464 	unsigned int mode = DNS_JOURNAL_CREATE | DNS_JOURNAL_WRITE;
4465 
4466 	ENTER;
4467 	journalfile = dns_zone_getjournal(zone);
4468 	if (journalfile != NULL) {
4469 		result = dns_journal_open(zone->mctx, journalfile, mode,
4470 					  &journal);
4471 		if (result != ISC_R_SUCCESS) {
4472 			dns_zone_log(zone, ISC_LOG_ERROR,
4473 				     "%s:dns_journal_open -> %s", caller,
4474 				     dns_result_totext(result));
4475 			return (result);
4476 		}
4477 
4478 		if (sourceserial != NULL) {
4479 			dns_journal_set_sourceserial(journal, *sourceserial);
4480 		}
4481 
4482 		result = dns_journal_write_transaction(journal, diff);
4483 		if (result != ISC_R_SUCCESS) {
4484 			dns_zone_log(zone, ISC_LOG_ERROR,
4485 				     "%s:dns_journal_write_transaction -> %s",
4486 				     caller, dns_result_totext(result));
4487 		}
4488 		dns_journal_destroy(&journal);
4489 	}
4490 
4491 	return (result);
4492 }
4493 
4494 /*
4495  * Create an SOA record for a newly-created zone
4496  */
4497 static isc_result_t
add_soa(dns_zone_t * zone,dns_db_t * db)4498 add_soa(dns_zone_t *zone, dns_db_t *db) {
4499 	isc_result_t result;
4500 	dns_rdata_t rdata = DNS_RDATA_INIT;
4501 	unsigned char buf[DNS_SOA_BUFFERSIZE];
4502 	dns_dbversion_t *ver = NULL;
4503 	dns_diff_t diff;
4504 
4505 	dns_zone_log(zone, ISC_LOG_DEBUG(1), "creating SOA");
4506 
4507 	dns_diff_init(zone->mctx, &diff);
4508 	result = dns_db_newversion(db, &ver);
4509 	if (result != ISC_R_SUCCESS) {
4510 		dns_zone_log(zone, ISC_LOG_ERROR,
4511 			     "add_soa:dns_db_newversion -> %s",
4512 			     dns_result_totext(result));
4513 		goto failure;
4514 	}
4515 
4516 	/* Build SOA record */
4517 	result = dns_soa_buildrdata(&zone->origin, dns_rootname, zone->rdclass,
4518 				    0, 0, 0, 0, 0, buf, &rdata);
4519 	if (result != ISC_R_SUCCESS) {
4520 		dns_zone_log(zone, ISC_LOG_ERROR,
4521 			     "add_soa:dns_soa_buildrdata -> %s",
4522 			     dns_result_totext(result));
4523 		goto failure;
4524 	}
4525 
4526 	result = update_one_rr(db, ver, &diff, DNS_DIFFOP_ADD, &zone->origin, 0,
4527 			       &rdata);
4528 
4529 failure:
4530 	dns_diff_clear(&diff);
4531 	if (ver != NULL) {
4532 		dns_db_closeversion(db, &ver, (result == ISC_R_SUCCESS));
4533 	}
4534 
4535 	INSIST(ver == NULL);
4536 
4537 	return (result);
4538 }
4539 
4540 struct addifmissing_arg {
4541 	dns_db_t *db;
4542 	dns_dbversion_t *ver;
4543 	dns_diff_t *diff;
4544 	dns_zone_t *zone;
4545 	bool *changed;
4546 	isc_result_t result;
4547 };
4548 
4549 static void
addifmissing(dns_keytable_t * keytable,dns_keynode_t * keynode,dns_name_t * keyname,void * arg)4550 addifmissing(dns_keytable_t *keytable, dns_keynode_t *keynode,
4551 	     dns_name_t *keyname, void *arg) {
4552 	dns_db_t *db = ((struct addifmissing_arg *)arg)->db;
4553 	dns_dbversion_t *ver = ((struct addifmissing_arg *)arg)->ver;
4554 	dns_diff_t *diff = ((struct addifmissing_arg *)arg)->diff;
4555 	dns_zone_t *zone = ((struct addifmissing_arg *)arg)->zone;
4556 	bool *changed = ((struct addifmissing_arg *)arg)->changed;
4557 	isc_result_t result;
4558 	dns_fixedname_t fname;
4559 
4560 	UNUSED(keytable);
4561 
4562 	if (((struct addifmissing_arg *)arg)->result != ISC_R_SUCCESS) {
4563 		return;
4564 	}
4565 
4566 	if (!dns_keynode_managed(keynode)) {
4567 		return;
4568 	}
4569 
4570 	/*
4571 	 * If the keynode has no trust anchor set, return.
4572 	 */
4573 	if (!dns_keynode_dsset(keynode, NULL)) {
4574 		return;
4575 	}
4576 
4577 	/*
4578 	 * Check whether there's already a KEYDATA entry for this name;
4579 	 * if so, we don't need to add another.
4580 	 */
4581 	dns_fixedname_init(&fname);
4582 	result = dns_db_find(db, keyname, ver, dns_rdatatype_keydata,
4583 			     DNS_DBFIND_NOWILD, 0, NULL,
4584 			     dns_fixedname_name(&fname), NULL, NULL);
4585 	if (result == ISC_R_SUCCESS) {
4586 		return;
4587 	}
4588 
4589 	/*
4590 	 * Create the keydata.
4591 	 */
4592 	result = create_keydata(zone, db, ver, diff, keynode, keyname, changed);
4593 	if (result != ISC_R_SUCCESS && result != ISC_R_NOMORE) {
4594 		((struct addifmissing_arg *)arg)->result = result;
4595 	}
4596 }
4597 
4598 /*
4599  * Synchronize the set of initializing keys found in managed-keys {}
4600  * statements with the set of trust anchors found in the managed-keys.bind
4601  * zone.  If a domain is no longer named in managed-keys, delete all keys
4602  * from that domain from the key zone.	If a domain is configured as an
4603  * initial-key in trust-anchors, but there are no references to it in the
4604  * key zone, load the key zone with the initializing key(s) for that
4605  * domain and schedule a key refresh. If a domain is configured as
4606  * an initial-ds in trust-anchors, fetch the DNSKEY RRset, load the key
4607  * zone with the matching key, and schedule a key refresh.
4608  */
4609 static isc_result_t
sync_keyzone(dns_zone_t * zone,dns_db_t * db)4610 sync_keyzone(dns_zone_t *zone, dns_db_t *db) {
4611 	isc_result_t result = ISC_R_SUCCESS;
4612 	bool changed = false;
4613 	bool commit = false;
4614 	dns_keynode_t *keynode = NULL;
4615 	dns_view_t *view = zone->view;
4616 	dns_keytable_t *sr = NULL;
4617 	dns_dbversion_t *ver = NULL;
4618 	dns_diff_t diff;
4619 	dns_rriterator_t rrit;
4620 	struct addifmissing_arg arg;
4621 
4622 	dns_zone_log(zone, ISC_LOG_DEBUG(1), "synchronizing trusted keys");
4623 
4624 	dns_diff_init(zone->mctx, &diff);
4625 
4626 	CHECK(dns_view_getsecroots(view, &sr));
4627 
4628 	result = dns_db_newversion(db, &ver);
4629 	if (result != ISC_R_SUCCESS) {
4630 		dnssec_log(zone, ISC_LOG_ERROR,
4631 			   "sync_keyzone:dns_db_newversion -> %s",
4632 			   dns_result_totext(result));
4633 		goto failure;
4634 	}
4635 
4636 	/*
4637 	 * Walk the zone DB.  If we find any keys whose names are no longer
4638 	 * in trust-anchors, or which have been changed from initial to static,
4639 	 * (meaning they are permanent and not RFC5011-maintained), delete
4640 	 * them from the zone.  Otherwise call load_secroots(), which
4641 	 * loads keys into secroots as appropriate.
4642 	 */
4643 	dns_rriterator_init(&rrit, db, ver, 0);
4644 	for (result = dns_rriterator_first(&rrit); result == ISC_R_SUCCESS;
4645 	     result = dns_rriterator_nextrrset(&rrit))
4646 	{
4647 		dns_rdataset_t *rdataset = NULL;
4648 		dns_name_t *rrname = NULL;
4649 		uint32_t ttl;
4650 
4651 		dns_rriterator_current(&rrit, &rrname, &ttl, &rdataset, NULL);
4652 		if (!dns_rdataset_isassociated(rdataset)) {
4653 			dns_rriterator_destroy(&rrit);
4654 			goto failure;
4655 		}
4656 
4657 		if (rdataset->type != dns_rdatatype_keydata) {
4658 			continue;
4659 		}
4660 		/*
4661 		 * Release db wrlock to prevent LOR reports against
4662 		 * dns_keytable_forall() call below.
4663 		 */
4664 		dns_rriterator_pause(&rrit);
4665 		result = dns_keytable_find(sr, rrname, &keynode);
4666 		if (result != ISC_R_SUCCESS || !dns_keynode_managed(keynode)) {
4667 			CHECK(delete_keydata(db, ver, &diff, rrname, rdataset));
4668 			changed = true;
4669 		} else {
4670 			load_secroots(zone, rrname, rdataset);
4671 		}
4672 
4673 		if (keynode != NULL) {
4674 			dns_keytable_detachkeynode(sr, &keynode);
4675 		}
4676 	}
4677 	dns_rriterator_destroy(&rrit);
4678 
4679 	/*
4680 	 * Walk secroots to find any initial keys that aren't in
4681 	 * the zone.  If we find any, add them to the zone directly.
4682 	 * If any DS-style initial keys are found, refresh the key
4683 	 * zone so that they'll be looked up.
4684 	 */
4685 	arg.db = db;
4686 	arg.ver = ver;
4687 	arg.result = ISC_R_SUCCESS;
4688 	arg.diff = &diff;
4689 	arg.zone = zone;
4690 	arg.changed = &changed;
4691 	dns_keytable_forall(sr, addifmissing, &arg);
4692 	result = arg.result;
4693 	if (changed) {
4694 		/* Write changes to journal file. */
4695 		CHECK(update_soa_serial(zone, db, ver, &diff, zone->mctx,
4696 					zone->updatemethod));
4697 		CHECK(zone_journal(zone, &diff, NULL, "sync_keyzone"));
4698 
4699 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED);
4700 		zone_needdump(zone, 30);
4701 		commit = true;
4702 	}
4703 
4704 failure:
4705 	if (result != ISC_R_SUCCESS && !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED))
4706 	{
4707 		dnssec_log(zone, ISC_LOG_ERROR,
4708 			   "unable to synchronize managed keys: %s",
4709 			   dns_result_totext(result));
4710 		isc_time_settoepoch(&zone->refreshkeytime);
4711 	}
4712 	if (keynode != NULL) {
4713 		dns_keytable_detachkeynode(sr, &keynode);
4714 	}
4715 	if (sr != NULL) {
4716 		dns_keytable_detach(&sr);
4717 	}
4718 	if (ver != NULL) {
4719 		dns_db_closeversion(db, &ver, commit);
4720 	}
4721 	dns_diff_clear(&diff);
4722 
4723 	INSIST(ver == NULL);
4724 
4725 	return (result);
4726 }
4727 
4728 isc_result_t
dns_zone_synckeyzone(dns_zone_t * zone)4729 dns_zone_synckeyzone(dns_zone_t *zone) {
4730 	isc_result_t result;
4731 	dns_db_t *db = NULL;
4732 
4733 	if (zone->type != dns_zone_key) {
4734 		return (DNS_R_BADZONE);
4735 	}
4736 
4737 	CHECK(dns_zone_getdb(zone, &db));
4738 
4739 	LOCK_ZONE(zone);
4740 	result = sync_keyzone(zone, db);
4741 	UNLOCK_ZONE(zone);
4742 
4743 failure:
4744 	if (db != NULL) {
4745 		dns_db_detach(&db);
4746 	}
4747 	return (result);
4748 }
4749 
4750 static void
maybe_send_secure(dns_zone_t * zone)4751 maybe_send_secure(dns_zone_t *zone) {
4752 	isc_result_t result;
4753 
4754 	/*
4755 	 * We've finished loading, or else failed to load, an inline-signing
4756 	 * 'secure' zone.  We now need information about the status of the
4757 	 * 'raw' zone.  If we failed to load, then we need it to send a
4758 	 * copy of its database; if we succeeded, we need it to send its
4759 	 * serial number so that we can sync with it.  If it has not yet
4760 	 * loaded, we set a flag so that it will send the necessary
4761 	 * information when it has finished loading.
4762 	 */
4763 	if (zone->raw->db != NULL) {
4764 		if (zone->db != NULL) {
4765 			uint32_t serial;
4766 			unsigned int soacount;
4767 
4768 			result = zone_get_from_db(
4769 				zone->raw, zone->raw->db, NULL, &soacount, NULL,
4770 				&serial, NULL, NULL, NULL, NULL, NULL);
4771 			if (result == ISC_R_SUCCESS && soacount > 0U) {
4772 				zone_send_secureserial(zone->raw, serial);
4773 			}
4774 		} else {
4775 			zone_send_securedb(zone->raw, zone->raw->db);
4776 		}
4777 	} else {
4778 		DNS_ZONE_SETFLAG(zone->raw, DNS_ZONEFLG_SENDSECURE);
4779 	}
4780 }
4781 
4782 static bool
zone_unchanged(dns_db_t * db1,dns_db_t * db2,isc_mem_t * mctx)4783 zone_unchanged(dns_db_t *db1, dns_db_t *db2, isc_mem_t *mctx) {
4784 	isc_result_t result;
4785 	bool answer = false;
4786 	dns_diff_t diff;
4787 
4788 	dns_diff_init(mctx, &diff);
4789 	result = dns_db_diffx(&diff, db1, NULL, db2, NULL, NULL);
4790 	if (result == ISC_R_SUCCESS && ISC_LIST_EMPTY(diff.tuples)) {
4791 		answer = true;
4792 	}
4793 	dns_diff_clear(&diff);
4794 	return (answer);
4795 }
4796 
4797 /*
4798  * The zone is presumed to be locked.
4799  * If this is a inline_raw zone the secure version is also locked.
4800  */
4801 static isc_result_t
zone_postload(dns_zone_t * zone,dns_db_t * db,isc_time_t loadtime,isc_result_t result)4802 zone_postload(dns_zone_t *zone, dns_db_t *db, isc_time_t loadtime,
4803 	      isc_result_t result) {
4804 	unsigned int soacount = 0;
4805 	unsigned int nscount = 0;
4806 	unsigned int errors = 0;
4807 	uint32_t serial, oldserial, refresh, retry, expire, minimum, soattl;
4808 	isc_time_t now;
4809 	bool needdump = false;
4810 	bool fixjournal = false;
4811 	bool hasinclude = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HASINCLUDE);
4812 	bool nomaster = false;
4813 	bool had_db = false;
4814 	dns_include_t *inc;
4815 	bool is_dynamic = false;
4816 
4817 	INSIST(LOCKED_ZONE(zone));
4818 	if (inline_raw(zone)) {
4819 		INSIST(LOCKED_ZONE(zone->secure));
4820 	}
4821 
4822 	TIME_NOW(&now);
4823 
4824 	/*
4825 	 * Initiate zone transfer?  We may need a error code that
4826 	 * indicates that the "permanent" form does not exist.
4827 	 * XXX better error feedback to log.
4828 	 */
4829 	if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) {
4830 		if (zone->type == dns_zone_secondary ||
4831 		    zone->type == dns_zone_mirror ||
4832 		    zone->type == dns_zone_stub ||
4833 		    (zone->type == dns_zone_redirect && zone->masters == NULL))
4834 		{
4835 			if (result == ISC_R_FILENOTFOUND) {
4836 				dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
4837 					      ISC_LOG_DEBUG(1),
4838 					      "no master file");
4839 			} else if (result != DNS_R_NOMASTERFILE) {
4840 				dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
4841 					      ISC_LOG_ERROR,
4842 					      "loading from master file %s "
4843 					      "failed: %s",
4844 					      zone->masterfile,
4845 					      dns_result_totext(result));
4846 			}
4847 		} else if (zone->type == dns_zone_primary &&
4848 			   inline_secure(zone) && result == ISC_R_FILENOTFOUND)
4849 		{
4850 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
4851 				      ISC_LOG_DEBUG(1),
4852 				      "no master file, requesting db");
4853 			maybe_send_secure(zone);
4854 		} else {
4855 			int level = ISC_LOG_ERROR;
4856 			if (zone->type == dns_zone_key &&
4857 			    result == ISC_R_FILENOTFOUND) {
4858 				level = ISC_LOG_DEBUG(1);
4859 			}
4860 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, level,
4861 				      "loading from master file %s failed: %s",
4862 				      zone->masterfile,
4863 				      dns_result_totext(result));
4864 			nomaster = true;
4865 		}
4866 
4867 		if (zone->type != dns_zone_key) {
4868 			goto cleanup;
4869 		}
4870 	}
4871 
4872 	dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(2),
4873 		      "number of nodes in database: %u", dns_db_nodecount(db));
4874 
4875 	if (result == DNS_R_SEENINCLUDE) {
4876 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HASINCLUDE);
4877 	} else {
4878 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HASINCLUDE);
4879 	}
4880 
4881 	/*
4882 	 * If there's no master file for a key zone, then the zone is new:
4883 	 * create an SOA record.  (We do this now, instead of later, so that
4884 	 * if there happens to be a journal file, we can roll forward from
4885 	 * a sane starting point.)
4886 	 */
4887 	if (nomaster && zone->type == dns_zone_key) {
4888 		result = add_soa(zone, db);
4889 		if (result != ISC_R_SUCCESS) {
4890 			goto cleanup;
4891 		}
4892 	}
4893 
4894 	/*
4895 	 * Apply update log, if any, on initial load.
4896 	 */
4897 	if (zone->journal != NULL &&
4898 	    !DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NOMERGE) &&
4899 	    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED))
4900 	{
4901 		result = zone_journal_rollforward(zone, db, &needdump,
4902 						  &fixjournal);
4903 		if (result != ISC_R_SUCCESS) {
4904 			goto cleanup;
4905 		}
4906 	}
4907 
4908 	/*
4909 	 * Obtain ns, soa and cname counts for top of zone.
4910 	 */
4911 	INSIST(db != NULL);
4912 	result = zone_get_from_db(zone, db, &nscount, &soacount, &soattl,
4913 				  &serial, &refresh, &retry, &expire, &minimum,
4914 				  &errors);
4915 	if (result != ISC_R_SUCCESS && zone->type != dns_zone_key) {
4916 		dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR,
4917 			      "could not find NS and/or SOA records");
4918 	}
4919 
4920 	/*
4921 	 * Process any queued NSEC3PARAM change requests. Only for dynamic
4922 	 * zones, an inline-signing zone will perform this action when
4923 	 * receiving the secure db (receive_secure_db).
4924 	 */
4925 	is_dynamic = dns_zone_isdynamic(zone, true);
4926 	if (is_dynamic) {
4927 		isc_event_t *setnsec3param_event;
4928 		dns_zone_t *dummy;
4929 
4930 		while (!ISC_LIST_EMPTY(zone->setnsec3param_queue)) {
4931 			setnsec3param_event =
4932 				ISC_LIST_HEAD(zone->setnsec3param_queue);
4933 			ISC_LIST_UNLINK(zone->setnsec3param_queue,
4934 					setnsec3param_event, ev_link);
4935 			dummy = NULL;
4936 			zone_iattach(zone, &dummy);
4937 			isc_task_send(zone->task, &setnsec3param_event);
4938 		}
4939 	}
4940 
4941 	/*
4942 	 * Check to make sure the journal is up to date, and remove the
4943 	 * journal file if it isn't, as we wouldn't be able to apply
4944 	 * updates otherwise.
4945 	 */
4946 	if (zone->journal != NULL && is_dynamic &&
4947 	    !DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IXFRFROMDIFFS))
4948 	{
4949 		uint32_t jserial;
4950 		dns_journal_t *journal = NULL;
4951 		bool empty = false;
4952 
4953 		result = dns_journal_open(zone->mctx, zone->journal,
4954 					  DNS_JOURNAL_READ, &journal);
4955 		if (result == ISC_R_SUCCESS) {
4956 			jserial = dns_journal_last_serial(journal);
4957 			empty = dns_journal_empty(journal);
4958 			dns_journal_destroy(&journal);
4959 		} else {
4960 			jserial = serial;
4961 			result = ISC_R_SUCCESS;
4962 		}
4963 
4964 		if (jserial != serial) {
4965 			if (!empty) {
4966 				dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
4967 					      ISC_LOG_INFO,
4968 					      "journal file is out of date: "
4969 					      "removing journal file");
4970 			}
4971 			if (remove(zone->journal) < 0 && errno != ENOENT) {
4972 				char strbuf[ISC_STRERRORSIZE];
4973 				strerror_r(errno, strbuf, sizeof(strbuf));
4974 				isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
4975 					      DNS_LOGMODULE_ZONE,
4976 					      ISC_LOG_WARNING,
4977 					      "unable to remove journal "
4978 					      "'%s': '%s'",
4979 					      zone->journal, strbuf);
4980 			}
4981 		}
4982 	}
4983 
4984 	dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(1),
4985 		      "loaded; checking validity");
4986 
4987 	/*
4988 	 * Master / Slave / Mirror / Stub zones require both NS and SOA records
4989 	 * at the top of the zone.
4990 	 */
4991 
4992 	switch (zone->type) {
4993 	case dns_zone_dlz:
4994 	case dns_zone_primary:
4995 	case dns_zone_secondary:
4996 	case dns_zone_mirror:
4997 	case dns_zone_stub:
4998 	case dns_zone_redirect:
4999 		if (soacount != 1) {
5000 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
5001 				      ISC_LOG_ERROR, "has %d SOA records",
5002 				      soacount);
5003 			result = DNS_R_BADZONE;
5004 		}
5005 		if (nscount == 0) {
5006 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
5007 				      ISC_LOG_ERROR, "has no NS records");
5008 			result = DNS_R_BADZONE;
5009 		}
5010 		if (result != ISC_R_SUCCESS) {
5011 			goto cleanup;
5012 		}
5013 		if (zone->type == dns_zone_primary && errors != 0) {
5014 			result = DNS_R_BADZONE;
5015 			goto cleanup;
5016 		}
5017 		if (zone->type != dns_zone_stub &&
5018 		    zone->type != dns_zone_redirect) {
5019 			result = check_nsec3param(zone, db);
5020 			if (result != ISC_R_SUCCESS) {
5021 				goto cleanup;
5022 			}
5023 		}
5024 		if (zone->type == dns_zone_primary &&
5025 		    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKINTEGRITY) &&
5026 		    !integrity_checks(zone, db))
5027 		{
5028 			result = DNS_R_BADZONE;
5029 			goto cleanup;
5030 		}
5031 		if (zone->type == dns_zone_primary &&
5032 		    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKDUPRR) &&
5033 		    !zone_check_dup(zone, db))
5034 		{
5035 			result = DNS_R_BADZONE;
5036 			goto cleanup;
5037 		}
5038 
5039 		if (zone->type == dns_zone_primary) {
5040 			result = dns_zone_cdscheck(zone, db, NULL);
5041 			if (result != ISC_R_SUCCESS) {
5042 				dns_zone_log(zone, ISC_LOG_ERROR,
5043 					     "CDS/CDNSKEY consistency checks "
5044 					     "failed");
5045 				goto cleanup;
5046 			}
5047 		}
5048 
5049 		result = dns_zone_verifydb(zone, db, NULL);
5050 		if (result != ISC_R_SUCCESS) {
5051 			goto cleanup;
5052 		}
5053 
5054 		if (zone->db != NULL) {
5055 			unsigned int oldsoacount;
5056 
5057 			/*
5058 			 * This is checked in zone_replacedb() for slave zones
5059 			 * as they don't reload from disk.
5060 			 */
5061 			result = zone_get_from_db(
5062 				zone, zone->db, NULL, &oldsoacount, NULL,
5063 				&oldserial, NULL, NULL, NULL, NULL, NULL);
5064 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
5065 			RUNTIME_CHECK(oldsoacount > 0U);
5066 			if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IXFRFROMDIFFS) &&
5067 			    !isc_serial_gt(serial, oldserial))
5068 			{
5069 				uint32_t serialmin, serialmax;
5070 
5071 				INSIST(zone->type == dns_zone_primary);
5072 				INSIST(zone->raw == NULL);
5073 
5074 				if (serial == oldserial &&
5075 				    zone_unchanged(zone->db, db, zone->mctx)) {
5076 					dns_zone_logc(zone,
5077 						      DNS_LOGCATEGORY_ZONELOAD,
5078 						      ISC_LOG_INFO,
5079 						      "ixfr-from-differences: "
5080 						      "unchanged");
5081 					zone->loadtime = loadtime;
5082 					goto done;
5083 				}
5084 
5085 				serialmin = (oldserial + 1) & 0xffffffffU;
5086 				serialmax = (oldserial + 0x7fffffffU) &
5087 					    0xffffffffU;
5088 				dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
5089 					      ISC_LOG_ERROR,
5090 					      "ixfr-from-differences: "
5091 					      "new serial (%u) out of range "
5092 					      "[%u - %u]",
5093 					      serial, serialmin, serialmax);
5094 				result = DNS_R_BADZONE;
5095 				goto cleanup;
5096 			} else if (!isc_serial_ge(serial, oldserial)) {
5097 				dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
5098 					      ISC_LOG_ERROR,
5099 					      "zone serial (%u/%u) has gone "
5100 					      "backwards",
5101 					      serial, oldserial);
5102 			} else if (serial == oldserial && !hasinclude &&
5103 				   strcmp(zone->db_argv[0], "_builtin") != 0)
5104 			{
5105 				dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
5106 					      ISC_LOG_ERROR,
5107 					      "zone serial (%u) unchanged. "
5108 					      "zone may fail to transfer "
5109 					      "to slaves.",
5110 					      serial);
5111 			}
5112 		}
5113 
5114 		if (zone->type == dns_zone_primary &&
5115 		    (zone->update_acl != NULL || zone->ssutable != NULL) &&
5116 		    dns_zone_getsigresigninginterval(zone) < (3 * refresh) &&
5117 		    dns_db_issecure(db))
5118 		{
5119 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
5120 				      ISC_LOG_WARNING,
5121 				      "sig-re-signing-interval less than "
5122 				      "3 * refresh.");
5123 		}
5124 
5125 		zone->refresh = RANGE(refresh, zone->minrefresh,
5126 				      zone->maxrefresh);
5127 		zone->retry = RANGE(retry, zone->minretry, zone->maxretry);
5128 		zone->expire = RANGE(expire, zone->refresh + zone->retry,
5129 				     DNS_MAX_EXPIRE);
5130 		zone->soattl = soattl;
5131 		zone->minimum = minimum;
5132 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
5133 
5134 		if (zone->type == dns_zone_secondary ||
5135 		    zone->type == dns_zone_mirror ||
5136 		    zone->type == dns_zone_stub ||
5137 		    (zone->type == dns_zone_redirect && zone->masters != NULL))
5138 		{
5139 			isc_time_t t;
5140 			uint32_t delay;
5141 
5142 			result = isc_file_getmodtime(zone->journal, &t);
5143 			if (result != ISC_R_SUCCESS) {
5144 				result = isc_file_getmodtime(zone->masterfile,
5145 							     &t);
5146 			}
5147 			if (result == ISC_R_SUCCESS) {
5148 				DNS_ZONE_TIME_ADD(&t, zone->expire,
5149 						  &zone->expiretime);
5150 			} else {
5151 				DNS_ZONE_TIME_ADD(&now, zone->retry,
5152 						  &zone->expiretime);
5153 			}
5154 
5155 			delay = (zone->retry -
5156 				 isc_random_uniform((zone->retry * 3) / 4));
5157 			DNS_ZONE_TIME_ADD(&now, delay, &zone->refreshtime);
5158 			if (isc_time_compare(&zone->refreshtime,
5159 					     &zone->expiretime) >= 0) {
5160 				zone->refreshtime = now;
5161 			}
5162 		}
5163 
5164 		break;
5165 
5166 	case dns_zone_key:
5167 		result = sync_keyzone(zone, db);
5168 		if (result != ISC_R_SUCCESS) {
5169 			goto cleanup;
5170 		}
5171 		break;
5172 
5173 	default:
5174 		UNEXPECTED_ERROR(__FILE__, __LINE__, "unexpected zone type %d",
5175 				 zone->type);
5176 		result = ISC_R_UNEXPECTED;
5177 		goto cleanup;
5178 	}
5179 
5180 	/*
5181 	 * Check for weak DNSKEY's.
5182 	 */
5183 	if (zone->type == dns_zone_primary) {
5184 		zone_check_dnskeys(zone, db);
5185 	}
5186 
5187 	/*
5188 	 * Schedule DNSSEC key refresh.
5189 	 */
5190 	if (zone->type == dns_zone_primary &&
5191 	    DNS_ZONEKEY_OPTION(zone, DNS_ZONEKEY_MAINTAIN))
5192 	{
5193 		zone->refreshkeytime = now;
5194 	}
5195 
5196 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write);
5197 	if (zone->db != NULL) {
5198 		had_db = true;
5199 		result = zone_replacedb(zone, db, false);
5200 		ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write);
5201 		if (result != ISC_R_SUCCESS) {
5202 			goto cleanup;
5203 		}
5204 	} else {
5205 		zone_attachdb(zone, db);
5206 		ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write);
5207 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED |
5208 					       DNS_ZONEFLG_NEEDSTARTUPNOTIFY);
5209 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SENDSECURE) &&
5210 		    inline_raw(zone)) {
5211 			if (zone->secure->db == NULL) {
5212 				zone_send_securedb(zone, db);
5213 			} else {
5214 				zone_send_secureserial(zone, serial);
5215 			}
5216 		}
5217 	}
5218 
5219 	/*
5220 	 * Finished loading inline-signing zone; need to get status
5221 	 * from the raw side now.
5222 	 */
5223 	if (zone->type == dns_zone_primary && inline_secure(zone)) {
5224 		maybe_send_secure(zone);
5225 	}
5226 
5227 	result = ISC_R_SUCCESS;
5228 
5229 	if (fixjournal) {
5230 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FIXJOURNAL);
5231 		zone_journal_compact(zone, zone->db, 0);
5232 	}
5233 	if (needdump) {
5234 		if (zone->type == dns_zone_key) {
5235 			zone_needdump(zone, 30);
5236 		} else {
5237 			zone_needdump(zone, DNS_DUMP_DELAY);
5238 		}
5239 	}
5240 
5241 	if (zone->task != NULL) {
5242 		if (zone->type == dns_zone_primary) {
5243 			set_resigntime(zone);
5244 			resume_signingwithkey(zone);
5245 			resume_addnsec3chain(zone);
5246 		}
5247 
5248 		is_dynamic = dns_zone_isdynamic(zone, false);
5249 		if (zone->type == dns_zone_primary &&
5250 		    !DNS_ZONEKEY_OPTION(zone, DNS_ZONEKEY_NORESIGN) &&
5251 		    is_dynamic && dns_db_issecure(db))
5252 		{
5253 			dns_name_t *name;
5254 			dns_fixedname_t fixed;
5255 			dns_rdataset_t next;
5256 
5257 			dns_rdataset_init(&next);
5258 			name = dns_fixedname_initname(&fixed);
5259 
5260 			result = dns_db_getsigningtime(db, &next, name);
5261 			if (result == ISC_R_SUCCESS) {
5262 				isc_stdtime_t timenow;
5263 				char namebuf[DNS_NAME_FORMATSIZE];
5264 				char typebuf[DNS_RDATATYPE_FORMATSIZE];
5265 
5266 				isc_stdtime_get(&timenow);
5267 				dns_name_format(name, namebuf, sizeof(namebuf));
5268 				dns_rdatatype_format(next.covers, typebuf,
5269 						     sizeof(typebuf));
5270 				dnssec_log(
5271 					zone, ISC_LOG_DEBUG(3),
5272 					"next resign: %s/%s "
5273 					"in %d seconds",
5274 					namebuf, typebuf,
5275 					next.resign - timenow -
5276 						dns_zone_getsigresigninginterval(
5277 							zone));
5278 				dns_rdataset_disassociate(&next);
5279 			} else {
5280 				dnssec_log(zone, ISC_LOG_WARNING,
5281 					   "signed dynamic zone has no "
5282 					   "resign event scheduled");
5283 			}
5284 		}
5285 
5286 		zone_settimer(zone, &now);
5287 	}
5288 
5289 	/*
5290 	 * Clear old include list.
5291 	 */
5292 	for (inc = ISC_LIST_HEAD(zone->includes); inc != NULL;
5293 	     inc = ISC_LIST_HEAD(zone->includes))
5294 	{
5295 		ISC_LIST_UNLINK(zone->includes, inc, link);
5296 		isc_mem_free(zone->mctx, inc->name);
5297 		isc_mem_put(zone->mctx, inc, sizeof(*inc));
5298 	}
5299 	zone->nincludes = 0;
5300 
5301 	/*
5302 	 * Transfer new include list.
5303 	 */
5304 	for (inc = ISC_LIST_HEAD(zone->newincludes); inc != NULL;
5305 	     inc = ISC_LIST_HEAD(zone->newincludes))
5306 	{
5307 		ISC_LIST_UNLINK(zone->newincludes, inc, link);
5308 		ISC_LIST_APPEND(zone->includes, inc, link);
5309 		zone->nincludes++;
5310 	}
5311 
5312 	if (!dns_db_ispersistent(db)) {
5313 		dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_INFO,
5314 			      "loaded serial %u%s", serial,
5315 			      dns_db_issecure(db) ? " (DNSSEC signed)" : "");
5316 	}
5317 
5318 	if (!had_db && zone->type == dns_zone_mirror) {
5319 		dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_INFO,
5320 			      "mirror zone is now in use");
5321 	}
5322 
5323 	zone->loadtime = loadtime;
5324 	goto done;
5325 
5326 cleanup:
5327 	if (zone->type == dns_zone_key && result != ISC_R_SUCCESS) {
5328 		dnssec_log(zone, ISC_LOG_ERROR,
5329 			   "failed to initialize managed-keys (%s): "
5330 			   "DNSSEC validation is at risk",
5331 			   isc_result_totext(result));
5332 	}
5333 
5334 	for (inc = ISC_LIST_HEAD(zone->newincludes); inc != NULL;
5335 	     inc = ISC_LIST_HEAD(zone->newincludes))
5336 	{
5337 		ISC_LIST_UNLINK(zone->newincludes, inc, link);
5338 		isc_mem_free(zone->mctx, inc->name);
5339 		isc_mem_put(zone->mctx, inc, sizeof(*inc));
5340 	}
5341 	if (zone->type == dns_zone_secondary || zone->type == dns_zone_mirror ||
5342 	    zone->type == dns_zone_stub || zone->type == dns_zone_key ||
5343 	    (zone->type == dns_zone_redirect && zone->masters != NULL))
5344 	{
5345 		if (result != ISC_R_NOMEMORY) {
5346 			if (zone->journal != NULL) {
5347 				zone_saveunique(zone, zone->journal,
5348 						"jn-XXXXXXXX");
5349 			}
5350 			if (zone->masterfile != NULL) {
5351 				zone_saveunique(zone, zone->masterfile,
5352 						"db-XXXXXXXX");
5353 			}
5354 		}
5355 
5356 		/* Mark the zone for immediate refresh. */
5357 		zone->refreshtime = now;
5358 		if (zone->task != NULL) {
5359 			zone_settimer(zone, &now);
5360 		}
5361 		result = ISC_R_SUCCESS;
5362 	} else if (zone->type == dns_zone_primary ||
5363 		   zone->type == dns_zone_redirect) {
5364 		if (!(inline_secure(zone) && result == ISC_R_FILENOTFOUND)) {
5365 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
5366 				      ISC_LOG_ERROR,
5367 				      "not loaded due to errors.");
5368 		} else if (zone->type == dns_zone_primary) {
5369 			result = ISC_R_SUCCESS;
5370 		}
5371 	}
5372 
5373 done:
5374 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADPENDING);
5375 	/*
5376 	 * If this is an inline-signed zone and we were called for the raw
5377 	 * zone, we need to clear DNS_ZONEFLG_LOADPENDING for the secure zone
5378 	 * as well, but only if this is a reload, not an initial zone load: in
5379 	 * the former case, zone_postload() will not be run for the secure
5380 	 * zone; in the latter case, it will be.  Check which case we are
5381 	 * dealing with by consulting the DNS_ZONEFLG_LOADED flag for the
5382 	 * secure zone: if it is set, this must be a reload.
5383 	 */
5384 	if (inline_raw(zone) && DNS_ZONE_FLAG(zone->secure, DNS_ZONEFLG_LOADED))
5385 	{
5386 		DNS_ZONE_CLRFLAG(zone->secure, DNS_ZONEFLG_LOADPENDING);
5387 		/*
5388 		 * Re-start zone maintenance if it had been stalled
5389 		 * due to DNS_ZONEFLG_LOADPENDING being set when
5390 		 * zone_maintenance was called.
5391 		 */
5392 		if (zone->secure->task != NULL) {
5393 			zone_settimer(zone->secure, &now);
5394 		}
5395 	}
5396 
5397 	zone_debuglog(zone, "zone_postload", 99, "done");
5398 
5399 	return (result);
5400 }
5401 
5402 static bool
exit_check(dns_zone_t * zone)5403 exit_check(dns_zone_t *zone) {
5404 	REQUIRE(LOCKED_ZONE(zone));
5405 
5406 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SHUTDOWN) &&
5407 	    isc_refcount_current(&zone->irefs) == 0)
5408 	{
5409 		/*
5410 		 * DNS_ZONEFLG_SHUTDOWN can only be set if erefs == 0.
5411 		 */
5412 		INSIST(isc_refcount_current(&zone->erefs) == 0);
5413 		return (true);
5414 	}
5415 	return (false);
5416 }
5417 
5418 static bool
zone_check_ns(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * version,dns_name_t * name,bool logit)5419 zone_check_ns(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version,
5420 	      dns_name_t *name, bool logit) {
5421 	isc_result_t result;
5422 	char namebuf[DNS_NAME_FORMATSIZE];
5423 	char altbuf[DNS_NAME_FORMATSIZE];
5424 	dns_fixedname_t fixed;
5425 	dns_name_t *foundname;
5426 	int level;
5427 
5428 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NOCHECKNS)) {
5429 		return (true);
5430 	}
5431 
5432 	if (zone->type == dns_zone_primary) {
5433 		level = ISC_LOG_ERROR;
5434 	} else {
5435 		level = ISC_LOG_WARNING;
5436 	}
5437 
5438 	foundname = dns_fixedname_initname(&fixed);
5439 
5440 	result = dns_db_find(db, name, version, dns_rdatatype_a, 0, 0, NULL,
5441 			     foundname, NULL, NULL);
5442 	if (result == ISC_R_SUCCESS) {
5443 		return (true);
5444 	}
5445 
5446 	if (result == DNS_R_NXRRSET) {
5447 		result = dns_db_find(db, name, version, dns_rdatatype_aaaa, 0,
5448 				     0, NULL, foundname, NULL, NULL);
5449 		if (result == ISC_R_SUCCESS) {
5450 			return (true);
5451 		}
5452 	}
5453 
5454 	if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN ||
5455 	    result == DNS_R_EMPTYNAME)
5456 	{
5457 		if (logit) {
5458 			dns_name_format(name, namebuf, sizeof namebuf);
5459 			dns_zone_log(zone, level,
5460 				     "NS '%s' has no address "
5461 				     "records (A or AAAA)",
5462 				     namebuf);
5463 		}
5464 		return (false);
5465 	}
5466 
5467 	if (result == DNS_R_CNAME) {
5468 		if (logit) {
5469 			dns_name_format(name, namebuf, sizeof namebuf);
5470 			dns_zone_log(zone, level,
5471 				     "NS '%s' is a CNAME "
5472 				     "(illegal)",
5473 				     namebuf);
5474 		}
5475 		return (false);
5476 	}
5477 
5478 	if (result == DNS_R_DNAME) {
5479 		if (logit) {
5480 			dns_name_format(name, namebuf, sizeof namebuf);
5481 			dns_name_format(foundname, altbuf, sizeof altbuf);
5482 			dns_zone_log(zone, level,
5483 				     "NS '%s' is below a DNAME "
5484 				     "'%s' (illegal)",
5485 				     namebuf, altbuf);
5486 		}
5487 		return (false);
5488 	}
5489 
5490 	return (true);
5491 }
5492 
5493 static isc_result_t
zone_count_ns_rr(dns_zone_t * zone,dns_db_t * db,dns_dbnode_t * node,dns_dbversion_t * version,unsigned int * nscount,unsigned int * errors,bool logit)5494 zone_count_ns_rr(dns_zone_t *zone, dns_db_t *db, dns_dbnode_t *node,
5495 		 dns_dbversion_t *version, unsigned int *nscount,
5496 		 unsigned int *errors, bool logit) {
5497 	isc_result_t result;
5498 	unsigned int count = 0;
5499 	unsigned int ecount = 0;
5500 	dns_rdataset_t rdataset;
5501 	dns_rdata_t rdata;
5502 	dns_rdata_ns_t ns;
5503 
5504 	dns_rdataset_init(&rdataset);
5505 	result = dns_db_findrdataset(db, node, version, dns_rdatatype_ns,
5506 				     dns_rdatatype_none, 0, &rdataset, NULL);
5507 	if (result == ISC_R_NOTFOUND) {
5508 		INSIST(!dns_rdataset_isassociated(&rdataset));
5509 		goto success;
5510 	}
5511 	if (result != ISC_R_SUCCESS) {
5512 		INSIST(!dns_rdataset_isassociated(&rdataset));
5513 		goto invalidate_rdataset;
5514 	}
5515 
5516 	result = dns_rdataset_first(&rdataset);
5517 	while (result == ISC_R_SUCCESS) {
5518 		if (errors != NULL && zone->rdclass == dns_rdataclass_in &&
5519 		    (zone->type == dns_zone_primary ||
5520 		     zone->type == dns_zone_secondary ||
5521 		     zone->type == dns_zone_mirror))
5522 		{
5523 			dns_rdata_init(&rdata);
5524 			dns_rdataset_current(&rdataset, &rdata);
5525 			result = dns_rdata_tostruct(&rdata, &ns, NULL);
5526 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
5527 			if (dns_name_issubdomain(&ns.name, &zone->origin) &&
5528 			    !zone_check_ns(zone, db, version, &ns.name, logit))
5529 			{
5530 				ecount++;
5531 			}
5532 		}
5533 		count++;
5534 		result = dns_rdataset_next(&rdataset);
5535 	}
5536 	dns_rdataset_disassociate(&rdataset);
5537 
5538 success:
5539 	if (nscount != NULL) {
5540 		*nscount = count;
5541 	}
5542 	if (errors != NULL) {
5543 		*errors = ecount;
5544 	}
5545 
5546 	result = ISC_R_SUCCESS;
5547 
5548 invalidate_rdataset:
5549 	dns_rdataset_invalidate(&rdataset);
5550 
5551 	return (result);
5552 }
5553 
5554 #define SET_IF_NOT_NULL(obj, val) \
5555 	if (obj != NULL) {        \
5556 		*obj = val;       \
5557 	}
5558 
5559 #define SET_SOA_VALUES(soattl_v, serial_v, refresh_v, retry_v, expire_v, \
5560 		       minimum_v)                                        \
5561 	{                                                                \
5562 		SET_IF_NOT_NULL(soattl, soattl_v);                       \
5563 		SET_IF_NOT_NULL(serial, serial_v);                       \
5564 		SET_IF_NOT_NULL(refresh, refresh_v);                     \
5565 		SET_IF_NOT_NULL(retry, retry_v);                         \
5566 		SET_IF_NOT_NULL(expire, expire_v);                       \
5567 		SET_IF_NOT_NULL(minimum, minimum_v);                     \
5568 	}
5569 
5570 #define CLR_SOA_VALUES()                          \
5571 	{                                         \
5572 		SET_SOA_VALUES(0, 0, 0, 0, 0, 0); \
5573 	}
5574 
5575 static isc_result_t
zone_load_soa_rr(dns_db_t * db,dns_dbnode_t * node,dns_dbversion_t * version,unsigned int * soacount,uint32_t * soattl,uint32_t * serial,uint32_t * refresh,uint32_t * retry,uint32_t * expire,uint32_t * minimum)5576 zone_load_soa_rr(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
5577 		 unsigned int *soacount, uint32_t *soattl, uint32_t *serial,
5578 		 uint32_t *refresh, uint32_t *retry, uint32_t *expire,
5579 		 uint32_t *minimum) {
5580 	isc_result_t result;
5581 	unsigned int count = 0;
5582 	dns_rdataset_t rdataset;
5583 	dns_rdata_t rdata = DNS_RDATA_INIT;
5584 
5585 	dns_rdataset_init(&rdataset);
5586 	result = dns_db_findrdataset(db, node, version, dns_rdatatype_soa,
5587 				     dns_rdatatype_none, 0, &rdataset, NULL);
5588 	if (result == ISC_R_NOTFOUND) {
5589 		INSIST(!dns_rdataset_isassociated(&rdataset));
5590 		result = ISC_R_SUCCESS;
5591 		goto invalidate_rdataset;
5592 	}
5593 	if (result != ISC_R_SUCCESS) {
5594 		INSIST(!dns_rdataset_isassociated(&rdataset));
5595 		goto invalidate_rdataset;
5596 	}
5597 
5598 	result = dns_rdataset_first(&rdataset);
5599 	while (result == ISC_R_SUCCESS) {
5600 		dns_rdata_init(&rdata);
5601 		dns_rdataset_current(&rdataset, &rdata);
5602 		count++;
5603 		if (count == 1) {
5604 			dns_rdata_soa_t soa;
5605 			result = dns_rdata_tostruct(&rdata, &soa, NULL);
5606 			SET_SOA_VALUES(rdataset.ttl, soa.serial, soa.refresh,
5607 				       soa.retry, soa.expire, soa.minimum);
5608 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
5609 		}
5610 
5611 		result = dns_rdataset_next(&rdataset);
5612 		dns_rdata_reset(&rdata);
5613 	}
5614 	dns_rdataset_disassociate(&rdataset);
5615 
5616 	result = ISC_R_SUCCESS;
5617 
5618 invalidate_rdataset:
5619 	SET_IF_NOT_NULL(soacount, count);
5620 	if (count == 0) {
5621 		CLR_SOA_VALUES();
5622 	}
5623 
5624 	dns_rdataset_invalidate(&rdataset);
5625 
5626 	return (result);
5627 }
5628 
5629 /*
5630  * zone must be locked.
5631  */
5632 static isc_result_t
zone_get_from_db(dns_zone_t * zone,dns_db_t * db,unsigned int * nscount,unsigned int * soacount,uint32_t * soattl,uint32_t * serial,uint32_t * refresh,uint32_t * retry,uint32_t * expire,uint32_t * minimum,unsigned int * errors)5633 zone_get_from_db(dns_zone_t *zone, dns_db_t *db, unsigned int *nscount,
5634 		 unsigned int *soacount, uint32_t *soattl, uint32_t *serial,
5635 		 uint32_t *refresh, uint32_t *retry, uint32_t *expire,
5636 		 uint32_t *minimum, unsigned int *errors) {
5637 	isc_result_t result;
5638 	isc_result_t answer = ISC_R_SUCCESS;
5639 	dns_dbversion_t *version = NULL;
5640 	dns_dbnode_t *node;
5641 
5642 	REQUIRE(db != NULL);
5643 	REQUIRE(zone != NULL);
5644 
5645 	dns_db_currentversion(db, &version);
5646 
5647 	SET_IF_NOT_NULL(nscount, 0);
5648 	SET_IF_NOT_NULL(soacount, 0);
5649 	SET_IF_NOT_NULL(errors, 0);
5650 	CLR_SOA_VALUES();
5651 
5652 	node = NULL;
5653 	result = dns_db_findnode(db, &zone->origin, false, &node);
5654 	if (result != ISC_R_SUCCESS) {
5655 		answer = result;
5656 		goto closeversion;
5657 	}
5658 
5659 	if (nscount != NULL || errors != NULL) {
5660 		result = zone_count_ns_rr(zone, db, node, version, nscount,
5661 					  errors, true);
5662 		if (result != ISC_R_SUCCESS) {
5663 			answer = result;
5664 		}
5665 	}
5666 
5667 	if (soacount != NULL || soattl != NULL || serial != NULL ||
5668 	    refresh != NULL || retry != NULL || expire != NULL ||
5669 	    minimum != NULL)
5670 	{
5671 		result = zone_load_soa_rr(db, node, version, soacount, soattl,
5672 					  serial, refresh, retry, expire,
5673 					  minimum);
5674 		if (result != ISC_R_SUCCESS) {
5675 			answer = result;
5676 		}
5677 	}
5678 
5679 	dns_db_detachnode(db, &node);
5680 closeversion:
5681 	dns_db_closeversion(db, &version, false);
5682 
5683 	return (answer);
5684 }
5685 
5686 void
dns_zone_attach(dns_zone_t * source,dns_zone_t ** target)5687 dns_zone_attach(dns_zone_t *source, dns_zone_t **target) {
5688 	REQUIRE(DNS_ZONE_VALID(source));
5689 	REQUIRE(target != NULL && *target == NULL);
5690 	isc_refcount_increment(&source->erefs);
5691 	*target = source;
5692 }
5693 
5694 void
dns_zone_detach(dns_zone_t ** zonep)5695 dns_zone_detach(dns_zone_t **zonep) {
5696 	REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
5697 	dns_zone_t *zone = *zonep;
5698 	*zonep = NULL;
5699 
5700 	bool free_now = false;
5701 	dns_zone_t *raw = NULL;
5702 	dns_zone_t *secure = NULL;
5703 	if (isc_refcount_decrement(&zone->erefs) == 1) {
5704 		isc_refcount_destroy(&zone->erefs);
5705 
5706 		LOCK_ZONE(zone);
5707 		INSIST(zone != zone->raw);
5708 		/*
5709 		 * We just detached the last external reference.
5710 		 */
5711 		if (zone->task != NULL) {
5712 			/*
5713 			 * This zone is being managed.	Post
5714 			 * its control event and let it clean
5715 			 * up synchronously in the context of
5716 			 * its task.
5717 			 */
5718 			isc_event_t *ev = &zone->ctlevent;
5719 			isc_task_send(zone->task, &ev);
5720 		} else {
5721 			/*
5722 			 * This zone is not being managed; it has
5723 			 * no task and can have no outstanding
5724 			 * events.  Free it immediately.
5725 			 */
5726 			/*
5727 			 * Unmanaged zones should not have non-null views;
5728 			 * we have no way of detaching from the view here
5729 			 * without causing deadlock because this code is called
5730 			 * with the view already locked.
5731 			 */
5732 			INSIST(zone->view == NULL);
5733 			free_now = true;
5734 			raw = zone->raw;
5735 			zone->raw = NULL;
5736 			secure = zone->secure;
5737 			zone->secure = NULL;
5738 		}
5739 		UNLOCK_ZONE(zone);
5740 	}
5741 	if (free_now) {
5742 		if (raw != NULL) {
5743 			dns_zone_detach(&raw);
5744 		}
5745 		if (secure != NULL) {
5746 			dns_zone_idetach(&secure);
5747 		}
5748 		zone_free(zone);
5749 	}
5750 }
5751 
5752 void
dns_zone_iattach(dns_zone_t * source,dns_zone_t ** target)5753 dns_zone_iattach(dns_zone_t *source, dns_zone_t **target) {
5754 	REQUIRE(DNS_ZONE_VALID(source));
5755 
5756 	LOCK_ZONE(source);
5757 	zone_iattach(source, target);
5758 	UNLOCK_ZONE(source);
5759 }
5760 
5761 static void
zone_iattach(dns_zone_t * source,dns_zone_t ** target)5762 zone_iattach(dns_zone_t *source, dns_zone_t **target) {
5763 	REQUIRE(DNS_ZONE_VALID(source));
5764 	REQUIRE(LOCKED_ZONE(source));
5765 	REQUIRE(target != NULL && *target == NULL);
5766 	INSIST(isc_refcount_increment0(&source->irefs) +
5767 		       isc_refcount_current(&source->erefs) >
5768 	       0);
5769 	*target = source;
5770 }
5771 
5772 static void
zone_idetach(dns_zone_t ** zonep)5773 zone_idetach(dns_zone_t **zonep) {
5774 	dns_zone_t *zone;
5775 
5776 	/*
5777 	 * 'zone' locked by caller.
5778 	 */
5779 	REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
5780 	REQUIRE(LOCKED_ZONE(*zonep));
5781 
5782 	zone = *zonep;
5783 	*zonep = NULL;
5784 
5785 	INSIST(isc_refcount_decrement(&zone->irefs) - 1 +
5786 		       isc_refcount_current(&zone->erefs) >
5787 	       0);
5788 }
5789 
5790 void
dns_zone_idetach(dns_zone_t ** zonep)5791 dns_zone_idetach(dns_zone_t **zonep) {
5792 	dns_zone_t *zone;
5793 	bool free_needed;
5794 
5795 	REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep));
5796 
5797 	zone = *zonep;
5798 	*zonep = NULL;
5799 
5800 	if (isc_refcount_decrement(&zone->irefs) == 1) {
5801 		LOCK_ZONE(zone);
5802 		free_needed = exit_check(zone);
5803 		UNLOCK_ZONE(zone);
5804 		if (free_needed) {
5805 			zone_free(zone);
5806 		}
5807 	}
5808 }
5809 
5810 isc_mem_t *
dns_zone_getmctx(dns_zone_t * zone)5811 dns_zone_getmctx(dns_zone_t *zone) {
5812 	REQUIRE(DNS_ZONE_VALID(zone));
5813 
5814 	return (zone->mctx);
5815 }
5816 
5817 dns_zonemgr_t *
dns_zone_getmgr(dns_zone_t * zone)5818 dns_zone_getmgr(dns_zone_t *zone) {
5819 	REQUIRE(DNS_ZONE_VALID(zone));
5820 
5821 	return (zone->zmgr);
5822 }
5823 
5824 void
dns_zone_setkasp(dns_zone_t * zone,dns_kasp_t * kasp)5825 dns_zone_setkasp(dns_zone_t *zone, dns_kasp_t *kasp) {
5826 	REQUIRE(DNS_ZONE_VALID(zone));
5827 
5828 	LOCK_ZONE(zone);
5829 	if (zone->kasp != NULL) {
5830 		dns_kasp_t *oldkasp = zone->kasp;
5831 		zone->kasp = NULL;
5832 		dns_kasp_detach(&oldkasp);
5833 	}
5834 	zone->kasp = kasp;
5835 	UNLOCK_ZONE(zone);
5836 }
5837 
5838 dns_kasp_t *
dns_zone_getkasp(dns_zone_t * zone)5839 dns_zone_getkasp(dns_zone_t *zone) {
5840 	REQUIRE(DNS_ZONE_VALID(zone));
5841 
5842 	return (zone->kasp);
5843 }
5844 
5845 void
dns_zone_setoption(dns_zone_t * zone,dns_zoneopt_t option,bool value)5846 dns_zone_setoption(dns_zone_t *zone, dns_zoneopt_t option, bool value) {
5847 	REQUIRE(DNS_ZONE_VALID(zone));
5848 
5849 	if (value) {
5850 		DNS_ZONE_SETOPTION(zone, option);
5851 	} else {
5852 		DNS_ZONE_CLROPTION(zone, option);
5853 	}
5854 }
5855 
5856 dns_zoneopt_t
dns_zone_getoptions(dns_zone_t * zone)5857 dns_zone_getoptions(dns_zone_t *zone) {
5858 	REQUIRE(DNS_ZONE_VALID(zone));
5859 
5860 	return (atomic_load_relaxed(&zone->options));
5861 }
5862 
5863 void
dns_zone_setkeyopt(dns_zone_t * zone,unsigned int keyopt,bool value)5864 dns_zone_setkeyopt(dns_zone_t *zone, unsigned int keyopt, bool value) {
5865 	REQUIRE(DNS_ZONE_VALID(zone));
5866 
5867 	if (value) {
5868 		DNS_ZONEKEY_SETOPTION(zone, keyopt);
5869 	} else {
5870 		DNS_ZONEKEY_CLROPTION(zone, keyopt);
5871 	}
5872 }
5873 
5874 unsigned int
dns_zone_getkeyopts(dns_zone_t * zone)5875 dns_zone_getkeyopts(dns_zone_t *zone) {
5876 	REQUIRE(DNS_ZONE_VALID(zone));
5877 
5878 	return (atomic_load_relaxed(&zone->keyopts));
5879 }
5880 
5881 isc_result_t
dns_zone_setxfrsource4(dns_zone_t * zone,const isc_sockaddr_t * xfrsource)5882 dns_zone_setxfrsource4(dns_zone_t *zone, const isc_sockaddr_t *xfrsource) {
5883 	REQUIRE(DNS_ZONE_VALID(zone));
5884 
5885 	LOCK_ZONE(zone);
5886 	zone->xfrsource4 = *xfrsource;
5887 	UNLOCK_ZONE(zone);
5888 
5889 	return (ISC_R_SUCCESS);
5890 }
5891 
5892 isc_sockaddr_t *
dns_zone_getxfrsource4(dns_zone_t * zone)5893 dns_zone_getxfrsource4(dns_zone_t *zone) {
5894 	REQUIRE(DNS_ZONE_VALID(zone));
5895 	return (&zone->xfrsource4);
5896 }
5897 
5898 isc_result_t
dns_zone_setxfrsource4dscp(dns_zone_t * zone,isc_dscp_t dscp)5899 dns_zone_setxfrsource4dscp(dns_zone_t *zone, isc_dscp_t dscp) {
5900 	REQUIRE(DNS_ZONE_VALID(zone));
5901 
5902 	LOCK_ZONE(zone);
5903 	zone->xfrsource4dscp = dscp;
5904 	UNLOCK_ZONE(zone);
5905 
5906 	return (ISC_R_SUCCESS);
5907 }
5908 
5909 isc_dscp_t
dns_zone_getxfrsource4dscp(dns_zone_t * zone)5910 dns_zone_getxfrsource4dscp(dns_zone_t *zone) {
5911 	REQUIRE(DNS_ZONE_VALID(zone));
5912 	return (zone->xfrsource4dscp);
5913 }
5914 
5915 isc_result_t
dns_zone_setxfrsource6(dns_zone_t * zone,const isc_sockaddr_t * xfrsource)5916 dns_zone_setxfrsource6(dns_zone_t *zone, const isc_sockaddr_t *xfrsource) {
5917 	REQUIRE(DNS_ZONE_VALID(zone));
5918 
5919 	LOCK_ZONE(zone);
5920 	zone->xfrsource6 = *xfrsource;
5921 	UNLOCK_ZONE(zone);
5922 
5923 	return (ISC_R_SUCCESS);
5924 }
5925 
5926 isc_sockaddr_t *
dns_zone_getxfrsource6(dns_zone_t * zone)5927 dns_zone_getxfrsource6(dns_zone_t *zone) {
5928 	REQUIRE(DNS_ZONE_VALID(zone));
5929 	return (&zone->xfrsource6);
5930 }
5931 
5932 isc_dscp_t
dns_zone_getxfrsource6dscp(dns_zone_t * zone)5933 dns_zone_getxfrsource6dscp(dns_zone_t *zone) {
5934 	REQUIRE(DNS_ZONE_VALID(zone));
5935 	return (zone->xfrsource6dscp);
5936 }
5937 
5938 isc_result_t
dns_zone_setxfrsource6dscp(dns_zone_t * zone,isc_dscp_t dscp)5939 dns_zone_setxfrsource6dscp(dns_zone_t *zone, isc_dscp_t dscp) {
5940 	REQUIRE(DNS_ZONE_VALID(zone));
5941 
5942 	LOCK_ZONE(zone);
5943 	zone->xfrsource6dscp = dscp;
5944 	UNLOCK_ZONE(zone);
5945 
5946 	return (ISC_R_SUCCESS);
5947 }
5948 
5949 isc_result_t
dns_zone_setaltxfrsource4(dns_zone_t * zone,const isc_sockaddr_t * altxfrsource)5950 dns_zone_setaltxfrsource4(dns_zone_t *zone,
5951 			  const isc_sockaddr_t *altxfrsource) {
5952 	REQUIRE(DNS_ZONE_VALID(zone));
5953 
5954 	LOCK_ZONE(zone);
5955 	zone->altxfrsource4 = *altxfrsource;
5956 	UNLOCK_ZONE(zone);
5957 
5958 	return (ISC_R_SUCCESS);
5959 }
5960 
5961 isc_sockaddr_t *
dns_zone_getaltxfrsource4(dns_zone_t * zone)5962 dns_zone_getaltxfrsource4(dns_zone_t *zone) {
5963 	REQUIRE(DNS_ZONE_VALID(zone));
5964 	return (&zone->altxfrsource4);
5965 }
5966 
5967 isc_result_t
dns_zone_setaltxfrsource4dscp(dns_zone_t * zone,isc_dscp_t dscp)5968 dns_zone_setaltxfrsource4dscp(dns_zone_t *zone, isc_dscp_t dscp) {
5969 	REQUIRE(DNS_ZONE_VALID(zone));
5970 
5971 	LOCK_ZONE(zone);
5972 	zone->altxfrsource4dscp = dscp;
5973 	UNLOCK_ZONE(zone);
5974 
5975 	return (ISC_R_SUCCESS);
5976 }
5977 
5978 isc_dscp_t
dns_zone_getaltxfrsource4dscp(dns_zone_t * zone)5979 dns_zone_getaltxfrsource4dscp(dns_zone_t *zone) {
5980 	REQUIRE(DNS_ZONE_VALID(zone));
5981 	return (zone->altxfrsource4dscp);
5982 }
5983 
5984 isc_result_t
dns_zone_setaltxfrsource6(dns_zone_t * zone,const isc_sockaddr_t * altxfrsource)5985 dns_zone_setaltxfrsource6(dns_zone_t *zone,
5986 			  const isc_sockaddr_t *altxfrsource) {
5987 	REQUIRE(DNS_ZONE_VALID(zone));
5988 
5989 	LOCK_ZONE(zone);
5990 	zone->altxfrsource6 = *altxfrsource;
5991 	UNLOCK_ZONE(zone);
5992 
5993 	return (ISC_R_SUCCESS);
5994 }
5995 
5996 isc_sockaddr_t *
dns_zone_getaltxfrsource6(dns_zone_t * zone)5997 dns_zone_getaltxfrsource6(dns_zone_t *zone) {
5998 	REQUIRE(DNS_ZONE_VALID(zone));
5999 	return (&zone->altxfrsource6);
6000 }
6001 
6002 isc_result_t
dns_zone_setaltxfrsource6dscp(dns_zone_t * zone,isc_dscp_t dscp)6003 dns_zone_setaltxfrsource6dscp(dns_zone_t *zone, isc_dscp_t dscp) {
6004 	REQUIRE(DNS_ZONE_VALID(zone));
6005 
6006 	LOCK_ZONE(zone);
6007 	zone->altxfrsource6dscp = dscp;
6008 	UNLOCK_ZONE(zone);
6009 
6010 	return (ISC_R_SUCCESS);
6011 }
6012 
6013 isc_dscp_t
dns_zone_getaltxfrsource6dscp(dns_zone_t * zone)6014 dns_zone_getaltxfrsource6dscp(dns_zone_t *zone) {
6015 	REQUIRE(DNS_ZONE_VALID(zone));
6016 	return (zone->altxfrsource6dscp);
6017 }
6018 
6019 isc_result_t
dns_zone_setparentalsrc4(dns_zone_t * zone,const isc_sockaddr_t * parentalsrc)6020 dns_zone_setparentalsrc4(dns_zone_t *zone, const isc_sockaddr_t *parentalsrc) {
6021 	REQUIRE(DNS_ZONE_VALID(zone));
6022 
6023 	LOCK_ZONE(zone);
6024 	zone->parentalsrc4 = *parentalsrc;
6025 	UNLOCK_ZONE(zone);
6026 
6027 	return (ISC_R_SUCCESS);
6028 }
6029 
6030 isc_sockaddr_t *
dns_zone_getparentalsrc4(dns_zone_t * zone)6031 dns_zone_getparentalsrc4(dns_zone_t *zone) {
6032 	REQUIRE(DNS_ZONE_VALID(zone));
6033 	return (&zone->parentalsrc4);
6034 }
6035 
6036 isc_result_t
dns_zone_setparentalsrc4dscp(dns_zone_t * zone,isc_dscp_t dscp)6037 dns_zone_setparentalsrc4dscp(dns_zone_t *zone, isc_dscp_t dscp) {
6038 	REQUIRE(DNS_ZONE_VALID(zone));
6039 
6040 	LOCK_ZONE(zone);
6041 	zone->parentalsrc4dscp = dscp;
6042 	UNLOCK_ZONE(zone);
6043 
6044 	return (ISC_R_SUCCESS);
6045 }
6046 
6047 isc_dscp_t
dns_zone_getparentalsrc4dscp(dns_zone_t * zone)6048 dns_zone_getparentalsrc4dscp(dns_zone_t *zone) {
6049 	REQUIRE(DNS_ZONE_VALID(zone));
6050 	return (zone->parentalsrc4dscp);
6051 }
6052 
6053 isc_result_t
dns_zone_setparentalsrc6(dns_zone_t * zone,const isc_sockaddr_t * parentalsrc)6054 dns_zone_setparentalsrc6(dns_zone_t *zone, const isc_sockaddr_t *parentalsrc) {
6055 	REQUIRE(DNS_ZONE_VALID(zone));
6056 
6057 	LOCK_ZONE(zone);
6058 	zone->parentalsrc6 = *parentalsrc;
6059 	UNLOCK_ZONE(zone);
6060 
6061 	return (ISC_R_SUCCESS);
6062 }
6063 
6064 isc_sockaddr_t *
dns_zone_getparentalsrc6(dns_zone_t * zone)6065 dns_zone_getparentalsrc6(dns_zone_t *zone) {
6066 	REQUIRE(DNS_ZONE_VALID(zone));
6067 	return (&zone->parentalsrc6);
6068 }
6069 
6070 isc_result_t
dns_zone_setparentalsrc6dscp(dns_zone_t * zone,isc_dscp_t dscp)6071 dns_zone_setparentalsrc6dscp(dns_zone_t *zone, isc_dscp_t dscp) {
6072 	REQUIRE(DNS_ZONE_VALID(zone));
6073 
6074 	LOCK_ZONE(zone);
6075 	zone->parentalsrc6dscp = dscp;
6076 	UNLOCK_ZONE(zone);
6077 
6078 	return (ISC_R_SUCCESS);
6079 }
6080 
6081 isc_dscp_t
dns_zone_getparentalsrc6dscp(dns_zone_t * zone)6082 dns_zone_getparentalsrc6dscp(dns_zone_t *zone) {
6083 	REQUIRE(DNS_ZONE_VALID(zone));
6084 	return (zone->parentalsrc6dscp);
6085 }
6086 
6087 isc_result_t
dns_zone_setnotifysrc4(dns_zone_t * zone,const isc_sockaddr_t * notifysrc)6088 dns_zone_setnotifysrc4(dns_zone_t *zone, const isc_sockaddr_t *notifysrc) {
6089 	REQUIRE(DNS_ZONE_VALID(zone));
6090 
6091 	LOCK_ZONE(zone);
6092 	zone->notifysrc4 = *notifysrc;
6093 	UNLOCK_ZONE(zone);
6094 
6095 	return (ISC_R_SUCCESS);
6096 }
6097 
6098 isc_sockaddr_t *
dns_zone_getnotifysrc4(dns_zone_t * zone)6099 dns_zone_getnotifysrc4(dns_zone_t *zone) {
6100 	REQUIRE(DNS_ZONE_VALID(zone));
6101 	return (&zone->notifysrc4);
6102 }
6103 
6104 isc_result_t
dns_zone_setnotifysrc4dscp(dns_zone_t * zone,isc_dscp_t dscp)6105 dns_zone_setnotifysrc4dscp(dns_zone_t *zone, isc_dscp_t dscp) {
6106 	REQUIRE(DNS_ZONE_VALID(zone));
6107 
6108 	LOCK_ZONE(zone);
6109 	zone->notifysrc4dscp = dscp;
6110 	UNLOCK_ZONE(zone);
6111 
6112 	return (ISC_R_SUCCESS);
6113 }
6114 
6115 isc_dscp_t
dns_zone_getnotifysrc4dscp(dns_zone_t * zone)6116 dns_zone_getnotifysrc4dscp(dns_zone_t *zone) {
6117 	REQUIRE(DNS_ZONE_VALID(zone));
6118 	return (zone->notifysrc4dscp);
6119 }
6120 
6121 isc_result_t
dns_zone_setnotifysrc6(dns_zone_t * zone,const isc_sockaddr_t * notifysrc)6122 dns_zone_setnotifysrc6(dns_zone_t *zone, const isc_sockaddr_t *notifysrc) {
6123 	REQUIRE(DNS_ZONE_VALID(zone));
6124 
6125 	LOCK_ZONE(zone);
6126 	zone->notifysrc6 = *notifysrc;
6127 	UNLOCK_ZONE(zone);
6128 
6129 	return (ISC_R_SUCCESS);
6130 }
6131 
6132 isc_sockaddr_t *
dns_zone_getnotifysrc6(dns_zone_t * zone)6133 dns_zone_getnotifysrc6(dns_zone_t *zone) {
6134 	REQUIRE(DNS_ZONE_VALID(zone));
6135 	return (&zone->notifysrc6);
6136 }
6137 
6138 isc_result_t
dns_zone_setnotifysrc6dscp(dns_zone_t * zone,isc_dscp_t dscp)6139 dns_zone_setnotifysrc6dscp(dns_zone_t *zone, isc_dscp_t dscp) {
6140 	REQUIRE(DNS_ZONE_VALID(zone));
6141 
6142 	LOCK_ZONE(zone);
6143 	zone->notifysrc6dscp = dscp;
6144 	UNLOCK_ZONE(zone);
6145 
6146 	return (ISC_R_SUCCESS);
6147 }
6148 
6149 isc_dscp_t
dns_zone_getnotifysrc6dscp(dns_zone_t * zone)6150 dns_zone_getnotifysrc6dscp(dns_zone_t *zone) {
6151 	REQUIRE(DNS_ZONE_VALID(zone));
6152 	return (zone->notifysrc6dscp);
6153 }
6154 
6155 static bool
same_addrs(isc_sockaddr_t const * oldlist,isc_sockaddr_t const * newlist,uint32_t count)6156 same_addrs(isc_sockaddr_t const *oldlist, isc_sockaddr_t const *newlist,
6157 	   uint32_t count) {
6158 	unsigned int i;
6159 
6160 	for (i = 0; i < count; i++) {
6161 		if (!isc_sockaddr_equal(&oldlist[i], &newlist[i])) {
6162 			return (false);
6163 		}
6164 	}
6165 	return (true);
6166 }
6167 
6168 static bool
same_keynames(dns_name_t * const * oldlist,dns_name_t * const * newlist,uint32_t count)6169 same_keynames(dns_name_t *const *oldlist, dns_name_t *const *newlist,
6170 	      uint32_t count) {
6171 	unsigned int i;
6172 
6173 	if (oldlist == NULL && newlist == NULL) {
6174 		return (true);
6175 	}
6176 	if (oldlist == NULL || newlist == NULL) {
6177 		return (false);
6178 	}
6179 
6180 	for (i = 0; i < count; i++) {
6181 		if (oldlist[i] == NULL && newlist[i] == NULL) {
6182 			continue;
6183 		}
6184 		if (oldlist[i] == NULL || newlist[i] == NULL ||
6185 		    !dns_name_equal(oldlist[i], newlist[i]))
6186 		{
6187 			return (false);
6188 		}
6189 	}
6190 	return (true);
6191 }
6192 
6193 static void
clear_serverslist(isc_sockaddr_t ** addrsp,isc_dscp_t ** dscpsp,dns_name_t *** keynamesp,unsigned int * countp,isc_mem_t * mctx)6194 clear_serverslist(isc_sockaddr_t **addrsp, isc_dscp_t **dscpsp,
6195 		  dns_name_t ***keynamesp, unsigned int *countp,
6196 		  isc_mem_t *mctx) {
6197 	unsigned int count;
6198 	isc_sockaddr_t *addrs;
6199 	isc_dscp_t *dscps;
6200 	dns_name_t **keynames;
6201 
6202 	REQUIRE(countp != NULL && addrsp != NULL && dscpsp != NULL &&
6203 		keynamesp != NULL);
6204 
6205 	count = *countp;
6206 	*countp = 0;
6207 	addrs = *addrsp;
6208 	*addrsp = NULL;
6209 	dscps = *dscpsp;
6210 	*dscpsp = NULL;
6211 	keynames = *keynamesp;
6212 	*keynamesp = NULL;
6213 
6214 	if (addrs != NULL) {
6215 		isc_mem_put(mctx, addrs, count * sizeof(isc_sockaddr_t));
6216 	}
6217 
6218 	if (dscps != NULL) {
6219 		isc_mem_put(mctx, dscps, count * sizeof(isc_dscp_t));
6220 	}
6221 
6222 	if (keynames != NULL) {
6223 		unsigned int i;
6224 		for (i = 0; i < count; i++) {
6225 			if (keynames[i] != NULL) {
6226 				dns_name_free(keynames[i], mctx);
6227 				isc_mem_put(mctx, keynames[i],
6228 					    sizeof(dns_name_t));
6229 				keynames[i] = NULL;
6230 			}
6231 		}
6232 		isc_mem_put(mctx, keynames, count * sizeof(dns_name_t *));
6233 	}
6234 }
6235 
6236 static isc_result_t
set_serverslist(unsigned int count,const isc_sockaddr_t * addrs,isc_sockaddr_t ** newaddrsp,const isc_dscp_t * dscp,isc_dscp_t ** newdscpp,dns_name_t ** names,dns_name_t *** newnamesp,isc_mem_t * mctx)6237 set_serverslist(unsigned int count, const isc_sockaddr_t *addrs,
6238 		isc_sockaddr_t **newaddrsp, const isc_dscp_t *dscp,
6239 		isc_dscp_t **newdscpp, dns_name_t **names,
6240 		dns_name_t ***newnamesp, isc_mem_t *mctx) {
6241 	isc_sockaddr_t *newaddrs = NULL;
6242 	isc_dscp_t *newdscp = NULL;
6243 	dns_name_t **newnames = NULL;
6244 	unsigned int i;
6245 
6246 	REQUIRE(newaddrsp != NULL && *newaddrsp == NULL);
6247 	REQUIRE(newdscpp != NULL && *newdscpp == NULL);
6248 	REQUIRE(newnamesp != NULL && *newnamesp == NULL);
6249 
6250 	newaddrs = isc_mem_get(mctx, count * sizeof(*newaddrs));
6251 	memmove(newaddrs, addrs, count * sizeof(*newaddrs));
6252 
6253 	if (dscp != NULL) {
6254 		newdscp = isc_mem_get(mctx, count * sizeof(*newdscp));
6255 		memmove(newdscp, dscp, count * sizeof(*newdscp));
6256 	} else {
6257 		newdscp = NULL;
6258 	}
6259 
6260 	if (names != NULL) {
6261 		newnames = isc_mem_get(mctx, count * sizeof(*newnames));
6262 		for (i = 0; i < count; i++) {
6263 			newnames[i] = NULL;
6264 		}
6265 		for (i = 0; i < count; i++) {
6266 			if (names[i] != NULL) {
6267 				newnames[i] = isc_mem_get(mctx,
6268 							  sizeof(dns_name_t));
6269 				dns_name_init(newnames[i], NULL);
6270 				dns_name_dup(names[i], mctx, newnames[i]);
6271 			}
6272 		}
6273 	} else {
6274 		newnames = NULL;
6275 	}
6276 
6277 	*newdscpp = newdscp;
6278 	*newaddrsp = newaddrs;
6279 	*newnamesp = newnames;
6280 	return (ISC_R_SUCCESS);
6281 }
6282 
6283 isc_result_t
dns_zone_setalsonotify(dns_zone_t * zone,const isc_sockaddr_t * notify,uint32_t count)6284 dns_zone_setalsonotify(dns_zone_t *zone, const isc_sockaddr_t *notify,
6285 		       uint32_t count) {
6286 	return (dns_zone_setalsonotifydscpkeys(zone, notify, NULL, NULL,
6287 					       count));
6288 }
6289 
6290 isc_result_t
dns_zone_setalsonotifywithkeys(dns_zone_t * zone,const isc_sockaddr_t * notify,dns_name_t ** keynames,uint32_t count)6291 dns_zone_setalsonotifywithkeys(dns_zone_t *zone, const isc_sockaddr_t *notify,
6292 			       dns_name_t **keynames, uint32_t count) {
6293 	return (dns_zone_setalsonotifydscpkeys(zone, notify, NULL, keynames,
6294 					       count));
6295 }
6296 
6297 isc_result_t
dns_zone_setalsonotifydscpkeys(dns_zone_t * zone,const isc_sockaddr_t * notify,const isc_dscp_t * dscps,dns_name_t ** keynames,uint32_t count)6298 dns_zone_setalsonotifydscpkeys(dns_zone_t *zone, const isc_sockaddr_t *notify,
6299 			       const isc_dscp_t *dscps, dns_name_t **keynames,
6300 			       uint32_t count) {
6301 	isc_result_t result;
6302 	isc_sockaddr_t *newaddrs = NULL;
6303 	isc_dscp_t *newdscps = NULL;
6304 	dns_name_t **newnames = NULL;
6305 
6306 	REQUIRE(DNS_ZONE_VALID(zone));
6307 	REQUIRE(count == 0 || notify != NULL);
6308 	if (keynames != NULL) {
6309 		REQUIRE(count != 0);
6310 	}
6311 
6312 	LOCK_ZONE(zone);
6313 
6314 	if (count == zone->notifycnt &&
6315 	    same_addrs(zone->notify, notify, count) &&
6316 	    same_keynames(zone->notifykeynames, keynames, count))
6317 	{
6318 		goto unlock;
6319 	}
6320 
6321 	clear_serverslist(&zone->notify, &zone->notifydscp,
6322 			  &zone->notifykeynames, &zone->notifycnt, zone->mctx);
6323 
6324 	if (count == 0) {
6325 		goto unlock;
6326 	}
6327 
6328 	/*
6329 	 * Set up the notify and notifykey lists
6330 	 */
6331 	result = set_serverslist(count, notify, &newaddrs, dscps, &newdscps,
6332 				 keynames, &newnames, zone->mctx);
6333 	if (result != ISC_R_SUCCESS) {
6334 		goto unlock;
6335 	}
6336 
6337 	/*
6338 	 * Everything is ok so attach to the zone.
6339 	 */
6340 	zone->notify = newaddrs;
6341 	zone->notifydscp = newdscps;
6342 	zone->notifykeynames = newnames;
6343 	zone->notifycnt = count;
6344 unlock:
6345 	UNLOCK_ZONE(zone);
6346 	return (ISC_R_SUCCESS);
6347 }
6348 
6349 isc_result_t
dns_zone_setprimaries(dns_zone_t * zone,const isc_sockaddr_t * masters,uint32_t count)6350 dns_zone_setprimaries(dns_zone_t *zone, const isc_sockaddr_t *masters,
6351 		      uint32_t count) {
6352 	isc_result_t result;
6353 
6354 	result = dns_zone_setprimarieswithkeys(zone, masters, NULL, count);
6355 	return (result);
6356 }
6357 
6358 isc_result_t
dns_zone_setprimarieswithkeys(dns_zone_t * zone,const isc_sockaddr_t * masters,dns_name_t ** keynames,uint32_t count)6359 dns_zone_setprimarieswithkeys(dns_zone_t *zone, const isc_sockaddr_t *masters,
6360 			      dns_name_t **keynames, uint32_t count) {
6361 	isc_result_t result = ISC_R_SUCCESS;
6362 	isc_sockaddr_t *newaddrs = NULL;
6363 	isc_dscp_t *newdscps = NULL;
6364 	dns_name_t **newnames = NULL;
6365 	bool *newok;
6366 	unsigned int i;
6367 
6368 	REQUIRE(DNS_ZONE_VALID(zone));
6369 	REQUIRE(count == 0 || masters != NULL);
6370 	if (keynames != NULL) {
6371 		REQUIRE(count != 0);
6372 	}
6373 
6374 	LOCK_ZONE(zone);
6375 	/*
6376 	 * The refresh code assumes that 'primaries' wouldn't change under it.
6377 	 * If it will change then kill off any current refresh in progress
6378 	 * and update the primaries info.  If it won't change then we can just
6379 	 * unlock and exit.
6380 	 */
6381 	if (count != zone->masterscnt ||
6382 	    !same_addrs(zone->masters, masters, count) ||
6383 	    !same_keynames(zone->masterkeynames, keynames, count))
6384 	{
6385 		if (zone->request != NULL) {
6386 			dns_request_cancel(zone->request);
6387 		}
6388 	} else {
6389 		goto unlock;
6390 	}
6391 
6392 	/*
6393 	 * This needs to happen before clear_addresskeylist() sets
6394 	 * zone->masterscnt to 0:
6395 	 */
6396 	if (zone->mastersok != NULL) {
6397 		isc_mem_put(zone->mctx, zone->mastersok,
6398 			    zone->masterscnt * sizeof(bool));
6399 		zone->mastersok = NULL;
6400 	}
6401 	clear_serverslist(&zone->masters, &zone->masterdscps,
6402 			  &zone->masterkeynames, &zone->masterscnt, zone->mctx);
6403 	/*
6404 	 * If count == 0, don't allocate any space for masters, mastersok or
6405 	 * keynames so internally, those pointers are NULL if count == 0
6406 	 */
6407 	if (count == 0) {
6408 		goto unlock;
6409 	}
6410 
6411 	/*
6412 	 * mastersok must contain count elements
6413 	 */
6414 	newok = isc_mem_get(zone->mctx, count * sizeof(*newok));
6415 	for (i = 0; i < count; i++) {
6416 		newok[i] = false;
6417 	}
6418 
6419 	/*
6420 	 * Now set up the primaries and primary key lists
6421 	 */
6422 	result = set_serverslist(count, masters, &newaddrs, NULL, &newdscps,
6423 				 keynames, &newnames, zone->mctx);
6424 	INSIST(newdscps == NULL);
6425 	if (result != ISC_R_SUCCESS) {
6426 		isc_mem_put(zone->mctx, newok, count * sizeof(*newok));
6427 		goto unlock;
6428 	}
6429 
6430 	/*
6431 	 * Everything is ok so attach to the zone.
6432 	 */
6433 	zone->curmaster = 0;
6434 	zone->mastersok = newok;
6435 	zone->masters = newaddrs;
6436 	zone->masterdscps = newdscps;
6437 	zone->masterkeynames = newnames;
6438 	zone->masterscnt = count;
6439 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOMASTERS);
6440 
6441 unlock:
6442 	UNLOCK_ZONE(zone);
6443 	return (result);
6444 }
6445 
6446 isc_result_t
dns_zone_setparentals(dns_zone_t * zone,const isc_sockaddr_t * parentals,dns_name_t ** keynames,uint32_t count)6447 dns_zone_setparentals(dns_zone_t *zone, const isc_sockaddr_t *parentals,
6448 		      dns_name_t **keynames, uint32_t count) {
6449 	isc_result_t result = ISC_R_SUCCESS;
6450 	isc_sockaddr_t *newaddrs = NULL;
6451 	isc_dscp_t *newdscps = NULL;
6452 	dns_name_t **newkeynames = NULL;
6453 
6454 	REQUIRE(DNS_ZONE_VALID(zone));
6455 	REQUIRE(count == 0 || parentals != NULL);
6456 	if (keynames != NULL) {
6457 		REQUIRE(count != 0);
6458 	}
6459 
6460 	LOCK_ZONE(zone);
6461 
6462 	clear_serverslist(&zone->parentals, &zone->parentaldscps,
6463 			  &zone->parentalkeynames, &zone->parentalscnt,
6464 			  zone->mctx);
6465 	/*
6466 	 * If count == 0, don't allocate any space for parentals, or keynames
6467 	 * so internally, those pointers are NULL if count == 0
6468 	 */
6469 	if (count == 0) {
6470 		goto unlock;
6471 	}
6472 
6473 	/*
6474 	 * Now set up the parentals and parental key lists
6475 	 */
6476 	result = set_serverslist(count, parentals, &newaddrs, NULL, &newdscps,
6477 				 keynames, &newkeynames, zone->mctx);
6478 	INSIST(newdscps == NULL);
6479 	if (result != ISC_R_SUCCESS) {
6480 		goto unlock;
6481 	}
6482 
6483 	/*
6484 	 * Everything is ok so attach to the zone.
6485 	 */
6486 	zone->parentals = newaddrs;
6487 	zone->parentaldscps = newdscps;
6488 	zone->parentalkeynames = newkeynames;
6489 	zone->parentalscnt = count;
6490 
6491 	dns_zone_log(zone, ISC_LOG_NOTICE, "checkds: set %u parentals", count);
6492 
6493 unlock:
6494 	UNLOCK_ZONE(zone);
6495 	return (result);
6496 }
6497 
6498 isc_result_t
dns_zone_getdb(dns_zone_t * zone,dns_db_t ** dpb)6499 dns_zone_getdb(dns_zone_t *zone, dns_db_t **dpb) {
6500 	isc_result_t result = ISC_R_SUCCESS;
6501 
6502 	REQUIRE(DNS_ZONE_VALID(zone));
6503 
6504 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
6505 	if (zone->db == NULL) {
6506 		result = DNS_R_NOTLOADED;
6507 	} else {
6508 		dns_db_attach(zone->db, dpb);
6509 	}
6510 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
6511 
6512 	return (result);
6513 }
6514 
6515 void
dns_zone_setdb(dns_zone_t * zone,dns_db_t * db)6516 dns_zone_setdb(dns_zone_t *zone, dns_db_t *db) {
6517 	REQUIRE(DNS_ZONE_VALID(zone));
6518 	REQUIRE(zone->type == dns_zone_staticstub);
6519 
6520 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write);
6521 	REQUIRE(zone->db == NULL);
6522 	dns_db_attach(db, &zone->db);
6523 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write);
6524 }
6525 
6526 /*
6527  * Coordinates the starting of routine jobs.
6528  */
6529 void
dns_zone_maintenance(dns_zone_t * zone)6530 dns_zone_maintenance(dns_zone_t *zone) {
6531 	const char me[] = "dns_zone_maintenance";
6532 	isc_time_t now;
6533 
6534 	REQUIRE(DNS_ZONE_VALID(zone));
6535 	ENTER;
6536 
6537 	LOCK_ZONE(zone);
6538 	TIME_NOW(&now);
6539 	zone_settimer(zone, &now);
6540 	UNLOCK_ZONE(zone);
6541 }
6542 
6543 static inline bool
was_dumping(dns_zone_t * zone)6544 was_dumping(dns_zone_t *zone) {
6545 	REQUIRE(LOCKED_ZONE(zone));
6546 
6547 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) {
6548 		return (true);
6549 	}
6550 
6551 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DUMPING);
6552 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
6553 	isc_time_settoepoch(&zone->dumptime);
6554 	return (false);
6555 }
6556 
6557 /*%
6558  * Find up to 'maxkeys' DNSSEC keys used for signing version 'ver' of database
6559  * 'db' for zone 'zone' in its key directory, then load these keys into 'keys'.
6560  * Only load the public part of a given key if it is not active at timestamp
6561  * 'now'.  Store the number of keys found in 'nkeys'.
6562  */
6563 isc_result_t
dns__zone_findkeys(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,isc_stdtime_t now,isc_mem_t * mctx,unsigned int maxkeys,dst_key_t ** keys,unsigned int * nkeys)6564 dns__zone_findkeys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
6565 		   isc_stdtime_t now, isc_mem_t *mctx, unsigned int maxkeys,
6566 		   dst_key_t **keys, unsigned int *nkeys) {
6567 	isc_result_t result;
6568 	dns_dbnode_t *node = NULL;
6569 	const char *directory = dns_zone_getkeydirectory(zone);
6570 
6571 	CHECK(dns_db_findnode(db, dns_db_origin(db), false, &node));
6572 	memset(keys, 0, sizeof(*keys) * maxkeys);
6573 
6574 	dns_zone_lock_keyfiles(zone);
6575 
6576 	result = dns_dnssec_findzonekeys(db, ver, node, dns_db_origin(db),
6577 					 directory, now, mctx, maxkeys, keys,
6578 					 nkeys);
6579 
6580 	dns_zone_unlock_keyfiles(zone);
6581 
6582 	if (result == ISC_R_NOTFOUND) {
6583 		result = ISC_R_SUCCESS;
6584 	}
6585 
6586 failure:
6587 
6588 	if (node != NULL) {
6589 		dns_db_detachnode(db, &node);
6590 	}
6591 	return (result);
6592 }
6593 
6594 /*%
6595  * Find DNSSEC keys used for signing zone with dnssec-policy. Load these keys
6596  * into 'keys'. Requires KASP to be locked.
6597  */
6598 isc_result_t
dns_zone_getdnsseckeys(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,isc_stdtime_t now,dns_dnsseckeylist_t * keys)6599 dns_zone_getdnsseckeys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
6600 		       isc_stdtime_t now, dns_dnsseckeylist_t *keys) {
6601 	isc_result_t result;
6602 	const char *dir = dns_zone_getkeydirectory(zone);
6603 	dns_dbnode_t *node = NULL;
6604 	dns_dnsseckey_t *key, *key_next;
6605 	dns_dnsseckeylist_t dnskeys;
6606 	dns_name_t *origin = dns_zone_getorigin(zone);
6607 	dns_kasp_t *kasp = dns_zone_getkasp(zone);
6608 	dns_rdataset_t keyset;
6609 
6610 	REQUIRE(DNS_ZONE_VALID(zone));
6611 	REQUIRE(kasp != NULL);
6612 
6613 	ISC_LIST_INIT(dnskeys);
6614 
6615 	dns_rdataset_init(&keyset);
6616 
6617 	CHECK(dns_db_findnode(db, origin, false, &node));
6618 
6619 	/* Get keys from private key files. */
6620 	dns_zone_lock_keyfiles(zone);
6621 	result = dns_dnssec_findmatchingkeys(origin, dir, now,
6622 					     dns_zone_getmctx(zone), keys);
6623 	dns_zone_unlock_keyfiles(zone);
6624 
6625 	if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
6626 		goto failure;
6627 	}
6628 
6629 	/* Get public keys (dnskeys). */
6630 	dns_rdataset_init(&keyset);
6631 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey,
6632 				     dns_rdatatype_none, 0, &keyset, NULL);
6633 	if (result == ISC_R_SUCCESS) {
6634 		CHECK(dns_dnssec_keylistfromrdataset(
6635 			origin, dir, dns_zone_getmctx(zone), &keyset, NULL,
6636 			NULL, false, false, &dnskeys));
6637 	} else if (result != ISC_R_NOTFOUND) {
6638 		CHECK(result);
6639 	}
6640 
6641 	/* Add new 'dnskeys' to 'keys'. */
6642 	for (dns_dnsseckey_t *k1 = ISC_LIST_HEAD(dnskeys); k1 != NULL;
6643 	     k1 = key_next) {
6644 		dns_dnsseckey_t *k2 = NULL;
6645 		key_next = ISC_LIST_NEXT(k1, link);
6646 
6647 		for (k2 = ISC_LIST_HEAD(*keys); k2 != NULL;
6648 		     k2 = ISC_LIST_NEXT(k2, link)) {
6649 			if (dst_key_compare(k1->key, k2->key)) {
6650 				break;
6651 			}
6652 		}
6653 		/* No match found, add the new key. */
6654 		if (k2 == NULL) {
6655 			ISC_LIST_UNLINK(dnskeys, k1, link);
6656 			ISC_LIST_APPEND(*keys, k1, link);
6657 		}
6658 	}
6659 
6660 failure:
6661 	if (dns_rdataset_isassociated(&keyset)) {
6662 		dns_rdataset_disassociate(&keyset);
6663 	}
6664 	if (node != NULL) {
6665 		dns_db_detachnode(db, &node);
6666 	}
6667 	while (!ISC_LIST_EMPTY(dnskeys)) {
6668 		key = ISC_LIST_HEAD(dnskeys);
6669 		ISC_LIST_UNLINK(dnskeys, key, link);
6670 		dns_dnsseckey_destroy(dns_zone_getmctx(zone), &key);
6671 	}
6672 	return (result);
6673 }
6674 
6675 static isc_result_t
offline(dns_db_t * db,dns_dbversion_t * ver,dns__zonediff_t * zonediff,dns_name_t * name,dns_ttl_t ttl,dns_rdata_t * rdata)6676 offline(dns_db_t *db, dns_dbversion_t *ver, dns__zonediff_t *zonediff,
6677 	dns_name_t *name, dns_ttl_t ttl, dns_rdata_t *rdata) {
6678 	isc_result_t result;
6679 
6680 	if ((rdata->flags & DNS_RDATA_OFFLINE) != 0) {
6681 		return (ISC_R_SUCCESS);
6682 	}
6683 	result = update_one_rr(db, ver, zonediff->diff, DNS_DIFFOP_DELRESIGN,
6684 			       name, ttl, rdata);
6685 	if (result != ISC_R_SUCCESS) {
6686 		return (result);
6687 	}
6688 	rdata->flags |= DNS_RDATA_OFFLINE;
6689 	result = update_one_rr(db, ver, zonediff->diff, DNS_DIFFOP_ADDRESIGN,
6690 			       name, ttl, rdata);
6691 	zonediff->offline = true;
6692 	return (result);
6693 }
6694 
6695 static void
set_key_expiry_warning(dns_zone_t * zone,isc_stdtime_t when,isc_stdtime_t now)6696 set_key_expiry_warning(dns_zone_t *zone, isc_stdtime_t when,
6697 		       isc_stdtime_t now) {
6698 	unsigned int delta;
6699 	char timebuf[80];
6700 
6701 	LOCK_ZONE(zone);
6702 	zone->key_expiry = when;
6703 	if (when <= now) {
6704 		dns_zone_log(zone, ISC_LOG_ERROR,
6705 			     "DNSKEY RRSIG(s) have expired");
6706 		isc_time_settoepoch(&zone->keywarntime);
6707 	} else if (when < now + 7 * 24 * 3600) {
6708 		isc_time_t t;
6709 		isc_time_set(&t, when, 0);
6710 		isc_time_formattimestamp(&t, timebuf, 80);
6711 		dns_zone_log(zone, ISC_LOG_WARNING,
6712 			     "DNSKEY RRSIG(s) will expire within 7 days: %s",
6713 			     timebuf);
6714 		delta = when - now;
6715 		delta--;	    /* loop prevention */
6716 		delta /= 24 * 3600; /* to whole days */
6717 		delta *= 24 * 3600; /* to seconds */
6718 		isc_time_set(&zone->keywarntime, when - delta, 0);
6719 	} else {
6720 		isc_time_set(&zone->keywarntime, when - 7 * 24 * 3600, 0);
6721 		isc_time_formattimestamp(&zone->keywarntime, timebuf, 80);
6722 		dns_zone_log(zone, ISC_LOG_NOTICE, "setting keywarntime to %s",
6723 			     timebuf);
6724 	}
6725 	UNLOCK_ZONE(zone);
6726 }
6727 
6728 /*
6729  * Helper function to del_sigs(). We don't want to delete RRSIGs that
6730  * have no new key.
6731  */
6732 static bool
delsig_ok(dns_rdata_rrsig_t * rrsig_ptr,dst_key_t ** keys,unsigned int nkeys,bool kasp,bool * warn)6733 delsig_ok(dns_rdata_rrsig_t *rrsig_ptr, dst_key_t **keys, unsigned int nkeys,
6734 	  bool kasp, bool *warn) {
6735 	unsigned int i = 0;
6736 	isc_result_t ret;
6737 	bool have_ksk = false, have_zsk = false;
6738 	bool have_pksk = false, have_pzsk = false;
6739 
6740 	for (i = 0; i < nkeys; i++) {
6741 		bool ksk, zsk;
6742 
6743 		if (have_pksk && have_ksk && have_pzsk && have_zsk) {
6744 			break;
6745 		}
6746 
6747 		if (rrsig_ptr->algorithm != dst_key_alg(keys[i])) {
6748 			continue;
6749 		}
6750 
6751 		ret = dst_key_getbool(keys[i], DST_BOOL_KSK, &ksk);
6752 		if (ret != ISC_R_SUCCESS) {
6753 			ksk = KSK(keys[i]);
6754 		}
6755 		ret = dst_key_getbool(keys[i], DST_BOOL_ZSK, &zsk);
6756 		if (ret != ISC_R_SUCCESS) {
6757 			zsk = !KSK(keys[i]);
6758 		}
6759 
6760 		if (ksk) {
6761 			have_ksk = true;
6762 			if (dst_key_isprivate(keys[i])) {
6763 				have_pksk = true;
6764 			}
6765 		}
6766 		if (zsk) {
6767 			have_zsk = true;
6768 			if (dst_key_isprivate(keys[i])) {
6769 				have_pzsk = true;
6770 			}
6771 		}
6772 	}
6773 
6774 	if (have_zsk && have_ksk && !have_pzsk) {
6775 		*warn = true;
6776 	}
6777 
6778 	if (have_pksk && have_pzsk) {
6779 		return (true);
6780 	}
6781 
6782 	/*
6783 	 * Deleting the SOA RRSIG is always okay.
6784 	 */
6785 	if (rrsig_ptr->covered == dns_rdatatype_soa) {
6786 		return (true);
6787 	}
6788 
6789 	/*
6790 	 * It's okay to delete a signature if there is an active key with the
6791 	 * same algorithm to replace it, unless that violates the DNSSEC
6792 	 * policy.
6793 	 */
6794 	if (have_pksk || have_pzsk) {
6795 		if (kasp && have_pzsk) {
6796 			return (true);
6797 		}
6798 		return (!kasp);
6799 	}
6800 
6801 	/*
6802 	 * Failing that, it is *not* okay to delete a signature
6803 	 * if the associated public key is still in the DNSKEY RRset
6804 	 */
6805 	for (i = 0; i < nkeys; i++) {
6806 		if ((rrsig_ptr->algorithm == dst_key_alg(keys[i])) &&
6807 		    (rrsig_ptr->keyid == dst_key_id(keys[i])))
6808 		{
6809 			return (false);
6810 		}
6811 	}
6812 
6813 	/*
6814 	 * But if the key is gone, then go ahead.
6815 	 */
6816 	return (true);
6817 }
6818 
6819 /*
6820  * Delete expired RRsigs and any RRsigs we are about to re-sign.
6821  * See also update.c:del_keysigs().
6822  */
6823 static isc_result_t
del_sigs(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,dns_name_t * name,dns_rdatatype_t type,dns__zonediff_t * zonediff,dst_key_t ** keys,unsigned int nkeys,isc_stdtime_t now,bool incremental)6824 del_sigs(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
6825 	 dns_rdatatype_t type, dns__zonediff_t *zonediff, dst_key_t **keys,
6826 	 unsigned int nkeys, isc_stdtime_t now, bool incremental) {
6827 	isc_result_t result;
6828 	dns_dbnode_t *node = NULL;
6829 	dns_rdataset_t rdataset;
6830 	unsigned int i;
6831 	dns_rdata_rrsig_t rrsig;
6832 	bool kasp = (dns_zone_getkasp(zone) != NULL);
6833 	bool found;
6834 	int64_t timewarn = 0, timemaybe = 0;
6835 
6836 	dns_rdataset_init(&rdataset);
6837 
6838 	if (type == dns_rdatatype_nsec3) {
6839 		result = dns_db_findnsec3node(db, name, false, &node);
6840 	} else {
6841 		result = dns_db_findnode(db, name, false, &node);
6842 	}
6843 	if (result == ISC_R_NOTFOUND) {
6844 		return (ISC_R_SUCCESS);
6845 	}
6846 	if (result != ISC_R_SUCCESS) {
6847 		goto failure;
6848 	}
6849 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_rrsig, type,
6850 				     (isc_stdtime_t)0, &rdataset, NULL);
6851 	dns_db_detachnode(db, &node);
6852 
6853 	if (result == ISC_R_NOTFOUND) {
6854 		INSIST(!dns_rdataset_isassociated(&rdataset));
6855 		return (ISC_R_SUCCESS);
6856 	}
6857 	if (result != ISC_R_SUCCESS) {
6858 		INSIST(!dns_rdataset_isassociated(&rdataset));
6859 		goto failure;
6860 	}
6861 
6862 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
6863 	     result = dns_rdataset_next(&rdataset))
6864 	{
6865 		dns_rdata_t rdata = DNS_RDATA_INIT;
6866 
6867 		dns_rdataset_current(&rdataset, &rdata);
6868 		result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
6869 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
6870 
6871 		if (type != dns_rdatatype_dnskey && type != dns_rdatatype_cds &&
6872 		    type != dns_rdatatype_cdnskey)
6873 		{
6874 			bool warn = false, deleted = false;
6875 			if (delsig_ok(&rrsig, keys, nkeys, kasp, &warn)) {
6876 				result = update_one_rr(db, ver, zonediff->diff,
6877 						       DNS_DIFFOP_DELRESIGN,
6878 						       name, rdataset.ttl,
6879 						       &rdata);
6880 				if (result != ISC_R_SUCCESS) {
6881 					break;
6882 				}
6883 				deleted = true;
6884 			}
6885 			if (warn && !deleted) {
6886 				/*
6887 				 * At this point, we've got an RRSIG,
6888 				 * which is signed by an inactive key.
6889 				 * An administrator needs to provide a new
6890 				 * key/alg, but until that time, we want to
6891 				 * keep the old RRSIG.  Marking the key as
6892 				 * offline will prevent us spinning waiting
6893 				 * for the private part.
6894 				 */
6895 				if (incremental) {
6896 					result = offline(db, ver, zonediff,
6897 							 name, rdataset.ttl,
6898 							 &rdata);
6899 					if (result != ISC_R_SUCCESS) {
6900 						break;
6901 					}
6902 				}
6903 
6904 				/*
6905 				 * Log the key id and algorithm of
6906 				 * the inactive key with no replacement
6907 				 */
6908 				if (zone->log_key_expired_timer <= now) {
6909 					char origin[DNS_NAME_FORMATSIZE];
6910 					char algbuf[DNS_NAME_FORMATSIZE];
6911 					dns_name_format(&zone->origin, origin,
6912 							sizeof(origin));
6913 					dns_secalg_format(rrsig.algorithm,
6914 							  algbuf,
6915 							  sizeof(algbuf));
6916 					dns_zone_log(zone, ISC_LOG_WARNING,
6917 						     "Key %s/%s/%d "
6918 						     "missing or inactive "
6919 						     "and has no replacement: "
6920 						     "retaining signatures.",
6921 						     origin, algbuf,
6922 						     rrsig.keyid);
6923 					zone->log_key_expired_timer = now +
6924 								      3600;
6925 				}
6926 			}
6927 			continue;
6928 		}
6929 
6930 		/*
6931 		 * KSK RRSIGs requires special processing.
6932 		 */
6933 		found = false;
6934 		for (i = 0; i < nkeys; i++) {
6935 			if (rrsig.algorithm == dst_key_alg(keys[i]) &&
6936 			    rrsig.keyid == dst_key_id(keys[i]))
6937 			{
6938 				found = true;
6939 				/*
6940 				 * Mark offline DNSKEY.
6941 				 * We want the earliest offline expire time
6942 				 * iff there is a new offline signature.
6943 				 */
6944 				if (!dst_key_inactive(keys[i]) &&
6945 				    !dst_key_isprivate(keys[i])) {
6946 					int64_t timeexpire = dns_time64_from32(
6947 						rrsig.timeexpire);
6948 					if (timewarn != 0 &&
6949 					    timewarn > timeexpire) {
6950 						timewarn = timeexpire;
6951 					}
6952 					if (rdata.flags & DNS_RDATA_OFFLINE) {
6953 						if (timemaybe == 0 ||
6954 						    timemaybe > timeexpire) {
6955 							timemaybe = timeexpire;
6956 						}
6957 						break;
6958 					}
6959 					if (timewarn == 0) {
6960 						timewarn = timemaybe;
6961 					}
6962 					if (timewarn == 0 ||
6963 					    timewarn > timeexpire) {
6964 						timewarn = timeexpire;
6965 					}
6966 					result = offline(db, ver, zonediff,
6967 							 name, rdataset.ttl,
6968 							 &rdata);
6969 					break;
6970 				}
6971 				result = update_one_rr(db, ver, zonediff->diff,
6972 						       DNS_DIFFOP_DELRESIGN,
6973 						       name, rdataset.ttl,
6974 						       &rdata);
6975 				break;
6976 			}
6977 		}
6978 
6979 		/*
6980 		 * If there is not a matching DNSKEY then
6981 		 * delete the RRSIG.
6982 		 */
6983 		if (!found) {
6984 			result = update_one_rr(db, ver, zonediff->diff,
6985 					       DNS_DIFFOP_DELRESIGN, name,
6986 					       rdataset.ttl, &rdata);
6987 		}
6988 		if (result != ISC_R_SUCCESS) {
6989 			break;
6990 		}
6991 	}
6992 
6993 	dns_rdataset_disassociate(&rdataset);
6994 	if (result == ISC_R_NOMORE) {
6995 		result = ISC_R_SUCCESS;
6996 	}
6997 	if (timewarn > 0) {
6998 		isc_stdtime_t stdwarn = (isc_stdtime_t)timewarn;
6999 		if (timewarn == stdwarn) {
7000 			set_key_expiry_warning(zone, (isc_stdtime_t)timewarn,
7001 					       now);
7002 		} else {
7003 			dns_zone_log(zone, ISC_LOG_ERROR,
7004 				     "key expiry warning time out of range");
7005 		}
7006 	}
7007 failure:
7008 	if (node != NULL) {
7009 		dns_db_detachnode(db, &node);
7010 	}
7011 	return (result);
7012 }
7013 
7014 static isc_result_t
add_sigs(dns_db_t * db,dns_dbversion_t * ver,dns_name_t * name,dns_zone_t * zone,dns_rdatatype_t type,dns_diff_t * diff,dst_key_t ** keys,unsigned int nkeys,isc_mem_t * mctx,isc_stdtime_t inception,isc_stdtime_t expire,bool check_ksk,bool keyset_kskonly)7015 add_sigs(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, dns_zone_t *zone,
7016 	 dns_rdatatype_t type, dns_diff_t *diff, dst_key_t **keys,
7017 	 unsigned int nkeys, isc_mem_t *mctx, isc_stdtime_t inception,
7018 	 isc_stdtime_t expire, bool check_ksk, bool keyset_kskonly) {
7019 	isc_result_t result;
7020 	dns_dbnode_t *node = NULL;
7021 	dns_stats_t *dnssecsignstats;
7022 	dns_rdataset_t rdataset;
7023 	dns_rdata_t sig_rdata = DNS_RDATA_INIT;
7024 	unsigned char data[1024]; /* XXX */
7025 	isc_buffer_t buffer;
7026 	unsigned int i, j;
7027 	bool use_kasp = false;
7028 
7029 	if (dns_zone_getkasp(zone) != NULL) {
7030 		check_ksk = false;
7031 		keyset_kskonly = true;
7032 		use_kasp = true;
7033 	}
7034 
7035 	dns_rdataset_init(&rdataset);
7036 	isc_buffer_init(&buffer, data, sizeof(data));
7037 
7038 	if (type == dns_rdatatype_nsec3) {
7039 		result = dns_db_findnsec3node(db, name, false, &node);
7040 	} else {
7041 		result = dns_db_findnode(db, name, false, &node);
7042 	}
7043 	if (result == ISC_R_NOTFOUND) {
7044 		return (ISC_R_SUCCESS);
7045 	}
7046 	if (result != ISC_R_SUCCESS) {
7047 		goto failure;
7048 	}
7049 	result = dns_db_findrdataset(db, node, ver, type, 0, (isc_stdtime_t)0,
7050 				     &rdataset, NULL);
7051 	dns_db_detachnode(db, &node);
7052 	if (result == ISC_R_NOTFOUND) {
7053 		INSIST(!dns_rdataset_isassociated(&rdataset));
7054 		return (ISC_R_SUCCESS);
7055 	}
7056 	if (result != ISC_R_SUCCESS) {
7057 		INSIST(!dns_rdataset_isassociated(&rdataset));
7058 		goto failure;
7059 	}
7060 
7061 	for (i = 0; i < nkeys; i++) {
7062 		bool both = false;
7063 
7064 		/* Don't add signatures for offline or inactive keys */
7065 		if (!dst_key_isprivate(keys[i])) {
7066 			continue;
7067 		}
7068 		if (dst_key_inactive(keys[i])) {
7069 			continue;
7070 		}
7071 
7072 		if (check_ksk && !REVOKE(keys[i])) {
7073 			bool have_ksk, have_nonksk;
7074 			if (KSK(keys[i])) {
7075 				have_ksk = true;
7076 				have_nonksk = false;
7077 			} else {
7078 				have_ksk = false;
7079 				have_nonksk = true;
7080 			}
7081 
7082 			for (j = 0; j < nkeys; j++) {
7083 				if (j == i || ALG(keys[i]) != ALG(keys[j])) {
7084 					continue;
7085 				}
7086 
7087 				/*
7088 				 * Don't consider inactive keys, however
7089 				 * the KSK may be temporary offline, so do
7090 				 * consider keys which private key files are
7091 				 * unavailable.
7092 				 */
7093 				if (dst_key_inactive(keys[j])) {
7094 					continue;
7095 				}
7096 
7097 				if (REVOKE(keys[j])) {
7098 					continue;
7099 				}
7100 				if (KSK(keys[j])) {
7101 					have_ksk = true;
7102 				} else if (dst_key_isprivate(keys[j])) {
7103 					have_nonksk = true;
7104 				}
7105 				both = have_ksk && have_nonksk;
7106 				if (both) {
7107 					break;
7108 				}
7109 			}
7110 		}
7111 		if (use_kasp) {
7112 			/*
7113 			 * A dnssec-policy is found. Check what RRsets this
7114 			 * key should sign.
7115 			 */
7116 			isc_result_t kresult;
7117 			isc_stdtime_t when;
7118 			bool ksk = false;
7119 			bool zsk = false;
7120 			bool have_ksk = false;
7121 			bool have_zsk = false;
7122 
7123 			kresult = dst_key_getbool(keys[i], DST_BOOL_KSK, &ksk);
7124 			if (kresult != ISC_R_SUCCESS) {
7125 				if (KSK(keys[i])) {
7126 					ksk = true;
7127 				}
7128 			}
7129 			kresult = dst_key_getbool(keys[i], DST_BOOL_ZSK, &zsk);
7130 			if (kresult != ISC_R_SUCCESS) {
7131 				if (!KSK(keys[i])) {
7132 					zsk = true;
7133 				}
7134 			}
7135 
7136 			have_ksk = ksk;
7137 			have_zsk = zsk;
7138 			both = have_ksk && have_zsk;
7139 
7140 			for (j = 0; j < nkeys; j++) {
7141 				if (both) {
7142 					break;
7143 				}
7144 
7145 				if (j == i || ALG(keys[i]) != ALG(keys[j])) {
7146 					continue;
7147 				}
7148 
7149 				/*
7150 				 * Don't consider inactive keys or offline keys.
7151 				 */
7152 				if (!dst_key_isprivate(keys[j])) {
7153 					continue;
7154 				}
7155 				if (dst_key_inactive(keys[j])) {
7156 					continue;
7157 				}
7158 
7159 				if (REVOKE(keys[j])) {
7160 					continue;
7161 				}
7162 
7163 				if (!have_ksk) {
7164 					kresult = dst_key_getbool(keys[j],
7165 								  DST_BOOL_KSK,
7166 								  &have_ksk);
7167 					if (kresult != ISC_R_SUCCESS) {
7168 						if (KSK(keys[j])) {
7169 							have_ksk = true;
7170 						}
7171 					}
7172 				}
7173 				if (!have_zsk) {
7174 					kresult = dst_key_getbool(keys[j],
7175 								  DST_BOOL_ZSK,
7176 								  &have_zsk);
7177 					if (kresult != ISC_R_SUCCESS) {
7178 						if (!KSK(keys[j])) {
7179 							have_zsk = true;
7180 						}
7181 					}
7182 				}
7183 				both = have_ksk && have_zsk;
7184 			}
7185 
7186 			if (type == dns_rdatatype_dnskey ||
7187 			    type == dns_rdatatype_cdnskey ||
7188 			    type == dns_rdatatype_cds)
7189 			{
7190 				/*
7191 				 * DNSKEY RRset is signed with KSK.
7192 				 * CDS and CDNSKEY RRsets too (RFC 7344, 4.1).
7193 				 */
7194 				if (!ksk) {
7195 					continue;
7196 				}
7197 			} else if (!zsk) {
7198 				/*
7199 				 * Other RRsets are signed with ZSK.
7200 				 */
7201 				if (type != dns_rdatatype_soa &&
7202 				    type != zone->privatetype) {
7203 					continue;
7204 				}
7205 				if (have_zsk) {
7206 					continue;
7207 				}
7208 			} else if (!dst_key_is_signing(keys[i], DST_BOOL_ZSK,
7209 						       inception, &when)) {
7210 				/*
7211 				 * This key is not active for zone-signing.
7212 				 */
7213 				continue;
7214 			}
7215 
7216 			/*
7217 			 * If this key is revoked, it may only sign the
7218 			 * DNSKEY RRset.
7219 			 */
7220 			if (REVOKE(keys[i]) && type != dns_rdatatype_dnskey) {
7221 				continue;
7222 			}
7223 		} else if (both) {
7224 			/*
7225 			 * CDS and CDNSKEY are signed with KSK (RFC 7344, 4.1).
7226 			 */
7227 			if (type == dns_rdatatype_dnskey ||
7228 			    type == dns_rdatatype_cdnskey ||
7229 			    type == dns_rdatatype_cds)
7230 			{
7231 				if (!KSK(keys[i]) && keyset_kskonly) {
7232 					continue;
7233 				}
7234 			} else if (KSK(keys[i])) {
7235 				continue;
7236 			}
7237 		} else if (REVOKE(keys[i]) && type != dns_rdatatype_dnskey) {
7238 			continue;
7239 		}
7240 
7241 		/* Calculate the signature, creating a RRSIG RDATA. */
7242 		isc_buffer_clear(&buffer);
7243 		CHECK(dns_dnssec_sign(name, &rdataset, keys[i], &inception,
7244 				      &expire, mctx, &buffer, &sig_rdata));
7245 
7246 		/* Update the database and journal with the RRSIG. */
7247 		/* XXX inefficient - will cause dataset merging */
7248 		CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADDRESIGN, name,
7249 				    rdataset.ttl, &sig_rdata));
7250 		dns_rdata_reset(&sig_rdata);
7251 		isc_buffer_init(&buffer, data, sizeof(data));
7252 
7253 		/* Update DNSSEC sign statistics. */
7254 		dnssecsignstats = dns_zone_getdnssecsignstats(zone);
7255 		if (dnssecsignstats != NULL) {
7256 			/* Generated a new signature. */
7257 			dns_dnssecsignstats_increment(dnssecsignstats,
7258 						      ID(keys[i]),
7259 						      (uint8_t)ALG(keys[i]),
7260 						      dns_dnssecsignstats_sign);
7261 			/* This is a refresh. */
7262 			dns_dnssecsignstats_increment(
7263 				dnssecsignstats, ID(keys[i]),
7264 				(uint8_t)ALG(keys[i]),
7265 				dns_dnssecsignstats_refresh);
7266 		}
7267 	}
7268 
7269 failure:
7270 	if (dns_rdataset_isassociated(&rdataset)) {
7271 		dns_rdataset_disassociate(&rdataset);
7272 	}
7273 	if (node != NULL) {
7274 		dns_db_detachnode(db, &node);
7275 	}
7276 	return (result);
7277 }
7278 
7279 static void
zone_resigninc(dns_zone_t * zone)7280 zone_resigninc(dns_zone_t *zone) {
7281 	const char *me = "zone_resigninc";
7282 	dns_db_t *db = NULL;
7283 	dns_dbversion_t *version = NULL;
7284 	dns_diff_t _sig_diff;
7285 	dns__zonediff_t zonediff;
7286 	dns_fixedname_t fixed;
7287 	dns_name_t *name;
7288 	dns_rdataset_t rdataset;
7289 	dns_rdatatype_t covers;
7290 	dst_key_t *zone_keys[DNS_MAXZONEKEYS];
7291 	bool check_ksk, keyset_kskonly = false;
7292 	isc_result_t result;
7293 	isc_stdtime_t now, inception, soaexpire, expire, fullexpire, stop;
7294 	uint32_t sigvalidityinterval, expiryinterval;
7295 	unsigned int i;
7296 	unsigned int nkeys = 0;
7297 	unsigned int resign;
7298 
7299 	ENTER;
7300 
7301 	dns_rdataset_init(&rdataset);
7302 	dns_diff_init(zone->mctx, &_sig_diff);
7303 	zonediff_init(&zonediff, &_sig_diff);
7304 
7305 	/*
7306 	 * Zone is frozen or automatic resigning is disabled.
7307 	 * Pause for 5 minutes.
7308 	 */
7309 	if (zone->update_disabled ||
7310 	    DNS_ZONEKEY_OPTION(zone, DNS_ZONEKEY_NORESIGN)) {
7311 		result = ISC_R_FAILURE;
7312 		goto failure;
7313 	}
7314 
7315 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
7316 	dns_db_attach(zone->db, &db);
7317 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
7318 
7319 	result = dns_db_newversion(db, &version);
7320 	if (result != ISC_R_SUCCESS) {
7321 		dns_zone_log(zone, ISC_LOG_ERROR,
7322 			     "zone_resigninc:dns_db_newversion -> %s",
7323 			     dns_result_totext(result));
7324 		goto failure;
7325 	}
7326 
7327 	isc_stdtime_get(&now);
7328 
7329 	result = dns__zone_findkeys(zone, db, version, now, zone->mctx,
7330 				    DNS_MAXZONEKEYS, zone_keys, &nkeys);
7331 	if (result != ISC_R_SUCCESS) {
7332 		dns_zone_log(zone, ISC_LOG_ERROR,
7333 			     "zone_resigninc:dns__zone_findkeys -> %s",
7334 			     dns_result_totext(result));
7335 		goto failure;
7336 	}
7337 
7338 	sigvalidityinterval = dns_zone_getsigvalidityinterval(zone);
7339 	inception = now - 3600; /* Allow for clock skew. */
7340 	soaexpire = now + sigvalidityinterval;
7341 	expiryinterval = dns_zone_getsigresigninginterval(zone);
7342 	if (expiryinterval > sigvalidityinterval) {
7343 		expiryinterval = sigvalidityinterval;
7344 	} else {
7345 		expiryinterval = sigvalidityinterval - expiryinterval;
7346 	}
7347 
7348 	/*
7349 	 * Spread out signatures over time if they happen to be
7350 	 * clumped.  We don't do this for each add_sigs() call as
7351 	 * we still want some clustering to occur.  In normal operations
7352 	 * the records should be re-signed as they fall due and they should
7353 	 * already be spread out.  However if the server is off for a
7354 	 * period we need to ensure that the clusters don't become
7355 	 * synchronised by using the full jitter range.
7356 	 */
7357 	if (sigvalidityinterval >= 3600U) {
7358 		uint32_t normaljitter, fulljitter;
7359 		if (sigvalidityinterval > 7200U) {
7360 			normaljitter = isc_random_uniform(3600);
7361 			fulljitter = isc_random_uniform(expiryinterval);
7362 		} else {
7363 			normaljitter = fulljitter = isc_random_uniform(1200);
7364 		}
7365 		expire = soaexpire - normaljitter - 1;
7366 		fullexpire = soaexpire - fulljitter - 1;
7367 	} else {
7368 		expire = fullexpire = soaexpire - 1;
7369 	}
7370 	stop = now + 5;
7371 
7372 	check_ksk = DNS_ZONE_OPTION(zone, DNS_ZONEOPT_UPDATECHECKKSK);
7373 	keyset_kskonly = DNS_ZONE_OPTION(zone, DNS_ZONEOPT_DNSKEYKSKONLY);
7374 
7375 	name = dns_fixedname_initname(&fixed);
7376 	result = dns_db_getsigningtime(db, &rdataset, name);
7377 	if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
7378 		dns_zone_log(zone, ISC_LOG_ERROR,
7379 			     "zone_resigninc:dns_db_getsigningtime -> %s",
7380 			     dns_result_totext(result));
7381 	}
7382 
7383 	i = 0;
7384 	while (result == ISC_R_SUCCESS) {
7385 		resign = rdataset.resign -
7386 			 dns_zone_getsigresigninginterval(zone);
7387 		covers = rdataset.covers;
7388 		dns_rdataset_disassociate(&rdataset);
7389 
7390 		/*
7391 		 * Stop if we hit the SOA as that means we have walked the
7392 		 * entire zone.  The SOA record should always be the most
7393 		 * recent signature.
7394 		 */
7395 		/* XXXMPA increase number of RRsets signed pre call */
7396 		if ((covers == dns_rdatatype_soa &&
7397 		     dns_name_equal(name, &zone->origin)) ||
7398 		    i++ > zone->signatures || resign > stop)
7399 		{
7400 			break;
7401 		}
7402 
7403 		result = del_sigs(zone, db, version, name, covers, &zonediff,
7404 				  zone_keys, nkeys, now, true);
7405 		if (result != ISC_R_SUCCESS) {
7406 			dns_zone_log(zone, ISC_LOG_ERROR,
7407 				     "zone_resigninc:del_sigs -> %s",
7408 				     dns_result_totext(result));
7409 			break;
7410 		}
7411 
7412 		/*
7413 		 * If re-signing is over 5 minutes late use 'fullexpire'
7414 		 * to redistribute the signature over the complete
7415 		 * re-signing window, otherwise only add a small amount
7416 		 * of jitter.
7417 		 */
7418 		result = add_sigs(db, version, name, zone, covers,
7419 				  zonediff.diff, zone_keys, nkeys, zone->mctx,
7420 				  inception,
7421 				  resign > (now - 300) ? expire : fullexpire,
7422 				  check_ksk, keyset_kskonly);
7423 		if (result != ISC_R_SUCCESS) {
7424 			dns_zone_log(zone, ISC_LOG_ERROR,
7425 				     "zone_resigninc:add_sigs -> %s",
7426 				     dns_result_totext(result));
7427 			break;
7428 		}
7429 		result = dns_db_getsigningtime(db, &rdataset, name);
7430 		if (nkeys == 0 && result == ISC_R_NOTFOUND) {
7431 			result = ISC_R_SUCCESS;
7432 			break;
7433 		}
7434 		if (result != ISC_R_SUCCESS) {
7435 			dns_zone_log(zone, ISC_LOG_ERROR,
7436 				     "zone_resigninc:dns_db_getsigningtime -> "
7437 				     "%s",
7438 				     dns_result_totext(result));
7439 		}
7440 	}
7441 
7442 	if (result != ISC_R_NOMORE && result != ISC_R_SUCCESS) {
7443 		goto failure;
7444 	}
7445 
7446 	result = del_sigs(zone, db, version, &zone->origin, dns_rdatatype_soa,
7447 			  &zonediff, zone_keys, nkeys, now, true);
7448 	if (result != ISC_R_SUCCESS) {
7449 		dns_zone_log(zone, ISC_LOG_ERROR,
7450 			     "zone_resigninc:del_sigs -> %s",
7451 			     dns_result_totext(result));
7452 		goto failure;
7453 	}
7454 
7455 	/*
7456 	 * Did we change anything in the zone?
7457 	 */
7458 	if (ISC_LIST_EMPTY(zonediff.diff->tuples)) {
7459 		/*
7460 		 * Commit the changes if any key has been marked as offline.
7461 		 */
7462 		if (zonediff.offline) {
7463 			dns_db_closeversion(db, &version, true);
7464 		}
7465 		goto failure;
7466 	}
7467 
7468 	/* Increment SOA serial if we have made changes */
7469 	result = update_soa_serial(zone, db, version, zonediff.diff, zone->mctx,
7470 				   zone->updatemethod);
7471 	if (result != ISC_R_SUCCESS) {
7472 		dns_zone_log(zone, ISC_LOG_ERROR,
7473 			     "zone_resigninc:update_soa_serial -> %s",
7474 			     dns_result_totext(result));
7475 		goto failure;
7476 	}
7477 
7478 	/*
7479 	 * Generate maximum life time signatures so that the above loop
7480 	 * termination is sensible.
7481 	 */
7482 	result = add_sigs(db, version, &zone->origin, zone, dns_rdatatype_soa,
7483 			  zonediff.diff, zone_keys, nkeys, zone->mctx,
7484 			  inception, soaexpire, check_ksk, keyset_kskonly);
7485 	if (result != ISC_R_SUCCESS) {
7486 		dns_zone_log(zone, ISC_LOG_ERROR,
7487 			     "zone_resigninc:add_sigs -> %s",
7488 			     dns_result_totext(result));
7489 		goto failure;
7490 	}
7491 
7492 	/* Write changes to journal file. */
7493 	CHECK(zone_journal(zone, zonediff.diff, NULL, "zone_resigninc"));
7494 
7495 	/* Everything has succeeded. Commit the changes. */
7496 	dns_db_closeversion(db, &version, true);
7497 
7498 failure:
7499 	dns_diff_clear(&_sig_diff);
7500 	for (i = 0; i < nkeys; i++) {
7501 		dst_key_free(&zone_keys[i]);
7502 	}
7503 	if (version != NULL) {
7504 		dns_db_closeversion(db, &version, false);
7505 		dns_db_detach(&db);
7506 	} else if (db != NULL) {
7507 		dns_db_detach(&db);
7508 	}
7509 
7510 	LOCK_ZONE(zone);
7511 	if (result == ISC_R_SUCCESS) {
7512 		set_resigntime(zone);
7513 		zone_needdump(zone, DNS_DUMP_DELAY);
7514 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
7515 	} else {
7516 		/*
7517 		 * Something failed.  Retry in 5 minutes.
7518 		 */
7519 		isc_interval_t ival;
7520 		isc_interval_set(&ival, 300, 0);
7521 		isc_time_nowplusinterval(&zone->resigntime, &ival);
7522 	}
7523 	UNLOCK_ZONE(zone);
7524 
7525 	INSIST(version == NULL);
7526 }
7527 
7528 static isc_result_t
next_active(dns_db_t * db,dns_dbversion_t * version,dns_name_t * oldname,dns_name_t * newname,bool bottom)7529 next_active(dns_db_t *db, dns_dbversion_t *version, dns_name_t *oldname,
7530 	    dns_name_t *newname, bool bottom) {
7531 	isc_result_t result;
7532 	dns_dbiterator_t *dbit = NULL;
7533 	dns_rdatasetiter_t *rdsit = NULL;
7534 	dns_dbnode_t *node = NULL;
7535 
7536 	CHECK(dns_db_createiterator(db, DNS_DB_NONSEC3, &dbit));
7537 	CHECK(dns_dbiterator_seek(dbit, oldname));
7538 	do {
7539 		result = dns_dbiterator_next(dbit);
7540 		if (result == ISC_R_NOMORE) {
7541 			CHECK(dns_dbiterator_first(dbit));
7542 		}
7543 		CHECK(dns_dbiterator_current(dbit, &node, newname));
7544 		if (bottom && dns_name_issubdomain(newname, oldname) &&
7545 		    !dns_name_equal(newname, oldname))
7546 		{
7547 			dns_db_detachnode(db, &node);
7548 			continue;
7549 		}
7550 		/*
7551 		 * Is this node empty?
7552 		 */
7553 		CHECK(dns_db_allrdatasets(db, node, version, 0, &rdsit));
7554 		result = dns_rdatasetiter_first(rdsit);
7555 		dns_db_detachnode(db, &node);
7556 		dns_rdatasetiter_destroy(&rdsit);
7557 		if (result != ISC_R_NOMORE) {
7558 			break;
7559 		}
7560 	} while (1);
7561 failure:
7562 	if (node != NULL) {
7563 		dns_db_detachnode(db, &node);
7564 	}
7565 	if (dbit != NULL) {
7566 		dns_dbiterator_destroy(&dbit);
7567 	}
7568 	return (result);
7569 }
7570 
7571 static bool
signed_with_good_key(dns_zone_t * zone,dns_db_t * db,dns_dbnode_t * node,dns_dbversion_t * version,dns_rdatatype_t type,dst_key_t * key)7572 signed_with_good_key(dns_zone_t *zone, dns_db_t *db, dns_dbnode_t *node,
7573 		     dns_dbversion_t *version, dns_rdatatype_t type,
7574 		     dst_key_t *key) {
7575 	isc_result_t result;
7576 	dns_rdataset_t rdataset;
7577 	dns_rdata_t rdata = DNS_RDATA_INIT;
7578 	dns_rdata_rrsig_t rrsig;
7579 	int count = 0;
7580 	dns_kasp_t *kasp = dns_zone_getkasp(zone);
7581 
7582 	dns_rdataset_init(&rdataset);
7583 	result = dns_db_findrdataset(db, node, version, dns_rdatatype_rrsig,
7584 				     type, 0, &rdataset, NULL);
7585 	if (result != ISC_R_SUCCESS) {
7586 		INSIST(!dns_rdataset_isassociated(&rdataset));
7587 		return (false);
7588 	}
7589 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
7590 	     result = dns_rdataset_next(&rdataset))
7591 	{
7592 		dns_rdataset_current(&rdataset, &rdata);
7593 		result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
7594 		INSIST(result == ISC_R_SUCCESS);
7595 		if (rrsig.algorithm == dst_key_alg(key) &&
7596 		    rrsig.keyid == dst_key_id(key)) {
7597 			dns_rdataset_disassociate(&rdataset);
7598 			return (true);
7599 		}
7600 		if (rrsig.algorithm == dst_key_alg(key)) {
7601 			count++;
7602 		}
7603 		dns_rdata_reset(&rdata);
7604 	}
7605 
7606 	if (dns_zone_getkasp(zone) != NULL) {
7607 		dns_kasp_key_t *kkey;
7608 		int zsk_count = 0;
7609 		bool approved;
7610 
7611 		KASP_LOCK(kasp);
7612 		for (kkey = ISC_LIST_HEAD(dns_kasp_keys(kasp)); kkey != NULL;
7613 		     kkey = ISC_LIST_NEXT(kkey, link))
7614 		{
7615 			if (dns_kasp_key_algorithm(kkey) != dst_key_alg(key)) {
7616 				continue;
7617 			}
7618 			if (dns_kasp_key_zsk(kkey)) {
7619 				zsk_count++;
7620 			}
7621 		}
7622 		KASP_UNLOCK(kasp);
7623 
7624 		if (type == dns_rdatatype_dnskey ||
7625 		    type == dns_rdatatype_cdnskey || type == dns_rdatatype_cds)
7626 		{
7627 			/*
7628 			 * CDS and CDNSKEY are signed with KSK like DNSKEY.
7629 			 * (RFC 7344, section 4.1 specifies that they must
7630 			 * be signed with a key in the current DS RRset,
7631 			 * which would only include KSK's.)
7632 			 */
7633 			approved = false;
7634 		} else {
7635 			approved = (zsk_count == count);
7636 		}
7637 
7638 		dns_rdataset_disassociate(&rdataset);
7639 		return (approved);
7640 	}
7641 
7642 	dns_rdataset_disassociate(&rdataset);
7643 	return (false);
7644 }
7645 
7646 static isc_result_t
add_nsec(dns_db_t * db,dns_dbversion_t * version,dns_name_t * name,dns_dbnode_t * node,dns_ttl_t ttl,bool bottom,dns_diff_t * diff)7647 add_nsec(dns_db_t *db, dns_dbversion_t *version, dns_name_t *name,
7648 	 dns_dbnode_t *node, dns_ttl_t ttl, bool bottom, dns_diff_t *diff) {
7649 	dns_fixedname_t fixed;
7650 	dns_name_t *next;
7651 	dns_rdata_t rdata = DNS_RDATA_INIT;
7652 	isc_result_t result;
7653 	unsigned char nsecbuffer[DNS_NSEC_BUFFERSIZE];
7654 
7655 	next = dns_fixedname_initname(&fixed);
7656 
7657 	CHECK(next_active(db, version, name, next, bottom));
7658 	CHECK(dns_nsec_buildrdata(db, version, node, next, nsecbuffer, &rdata));
7659 	CHECK(update_one_rr(db, version, diff, DNS_DIFFOP_ADD, name, ttl,
7660 			    &rdata));
7661 failure:
7662 	return (result);
7663 }
7664 
7665 static isc_result_t
check_if_bottom_of_zone(dns_db_t * db,dns_dbnode_t * node,dns_dbversion_t * version,bool * is_bottom_of_zone)7666 check_if_bottom_of_zone(dns_db_t *db, dns_dbnode_t *node,
7667 			dns_dbversion_t *version, bool *is_bottom_of_zone) {
7668 	isc_result_t result;
7669 	dns_rdatasetiter_t *iterator = NULL;
7670 	dns_rdataset_t rdataset;
7671 	bool seen_soa = false, seen_ns = false, seen_dname = false;
7672 
7673 	REQUIRE(is_bottom_of_zone != NULL);
7674 
7675 	result = dns_db_allrdatasets(db, node, version, 0, &iterator);
7676 	if (result != ISC_R_SUCCESS) {
7677 		if (result == ISC_R_NOTFOUND) {
7678 			result = ISC_R_SUCCESS;
7679 		}
7680 		return (result);
7681 	}
7682 
7683 	dns_rdataset_init(&rdataset);
7684 	for (result = dns_rdatasetiter_first(iterator); result == ISC_R_SUCCESS;
7685 	     result = dns_rdatasetiter_next(iterator))
7686 	{
7687 		dns_rdatasetiter_current(iterator, &rdataset);
7688 		switch (rdataset.type) {
7689 		case dns_rdatatype_soa:
7690 			seen_soa = true;
7691 			break;
7692 		case dns_rdatatype_ns:
7693 			seen_ns = true;
7694 			break;
7695 		case dns_rdatatype_dname:
7696 			seen_dname = true;
7697 			break;
7698 		}
7699 		dns_rdataset_disassociate(&rdataset);
7700 	}
7701 	if (result != ISC_R_NOMORE) {
7702 		goto failure;
7703 	}
7704 	if ((seen_ns && !seen_soa) || seen_dname) {
7705 		*is_bottom_of_zone = true;
7706 	}
7707 	result = ISC_R_SUCCESS;
7708 
7709 failure:
7710 	dns_rdatasetiter_destroy(&iterator);
7711 
7712 	return (result);
7713 }
7714 
7715 static isc_result_t
sign_a_node(dns_db_t * db,dns_zone_t * zone,dns_name_t * name,dns_dbnode_t * node,dns_dbversion_t * version,bool build_nsec3,bool build_nsec,dst_key_t * key,isc_stdtime_t inception,isc_stdtime_t expire,dns_ttl_t nsecttl,bool is_ksk,bool is_zsk,bool keyset_kskonly,bool is_bottom_of_zone,dns_diff_t * diff,int32_t * signatures,isc_mem_t * mctx)7716 sign_a_node(dns_db_t *db, dns_zone_t *zone, dns_name_t *name,
7717 	    dns_dbnode_t *node, dns_dbversion_t *version, bool build_nsec3,
7718 	    bool build_nsec, dst_key_t *key, isc_stdtime_t inception,
7719 	    isc_stdtime_t expire, dns_ttl_t nsecttl, bool is_ksk, bool is_zsk,
7720 	    bool keyset_kskonly, bool is_bottom_of_zone, dns_diff_t *diff,
7721 	    int32_t *signatures, isc_mem_t *mctx) {
7722 	isc_result_t result;
7723 	dns_rdatasetiter_t *iterator = NULL;
7724 	dns_rdataset_t rdataset;
7725 	dns_rdata_t rdata = DNS_RDATA_INIT;
7726 	dns_stats_t *dnssecsignstats;
7727 
7728 	isc_buffer_t buffer;
7729 	unsigned char data[1024];
7730 	bool seen_soa, seen_ns, seen_rr, seen_nsec, seen_nsec3, seen_ds;
7731 
7732 	result = dns_db_allrdatasets(db, node, version, 0, &iterator);
7733 	if (result != ISC_R_SUCCESS) {
7734 		if (result == ISC_R_NOTFOUND) {
7735 			result = ISC_R_SUCCESS;
7736 		}
7737 		return (result);
7738 	}
7739 
7740 	dns_rdataset_init(&rdataset);
7741 	isc_buffer_init(&buffer, data, sizeof(data));
7742 	seen_rr = seen_soa = seen_ns = seen_nsec = seen_nsec3 = seen_ds = false;
7743 	for (result = dns_rdatasetiter_first(iterator); result == ISC_R_SUCCESS;
7744 	     result = dns_rdatasetiter_next(iterator))
7745 	{
7746 		dns_rdatasetiter_current(iterator, &rdataset);
7747 		if (rdataset.type == dns_rdatatype_soa) {
7748 			seen_soa = true;
7749 		} else if (rdataset.type == dns_rdatatype_ns) {
7750 			seen_ns = true;
7751 		} else if (rdataset.type == dns_rdatatype_ds) {
7752 			seen_ds = true;
7753 		} else if (rdataset.type == dns_rdatatype_nsec) {
7754 			seen_nsec = true;
7755 		} else if (rdataset.type == dns_rdatatype_nsec3) {
7756 			seen_nsec3 = true;
7757 		}
7758 		if (rdataset.type != dns_rdatatype_rrsig) {
7759 			seen_rr = true;
7760 		}
7761 		dns_rdataset_disassociate(&rdataset);
7762 	}
7763 	if (result != ISC_R_NOMORE) {
7764 		goto failure;
7765 	}
7766 	/*
7767 	 * Going from insecure to NSEC3.
7768 	 * Don't generate NSEC3 records for NSEC3 records.
7769 	 */
7770 	if (build_nsec3 && !seen_nsec3 && seen_rr) {
7771 		bool unsecure = !seen_ds && seen_ns && !seen_soa;
7772 		CHECK(dns_nsec3_addnsec3s(db, version, name, nsecttl, unsecure,
7773 					  diff));
7774 		(*signatures)--;
7775 	}
7776 	/*
7777 	 * Going from insecure to NSEC.
7778 	 * Don't generate NSEC records for NSEC3 records.
7779 	 */
7780 	if (build_nsec && !seen_nsec3 && !seen_nsec && seen_rr) {
7781 		/*
7782 		 * Build a NSEC record except at the origin.
7783 		 */
7784 		if (!dns_name_equal(name, dns_db_origin(db))) {
7785 			CHECK(add_nsec(db, version, name, node, nsecttl,
7786 				       is_bottom_of_zone, diff));
7787 			/* Count a NSEC generation as a signature generation. */
7788 			(*signatures)--;
7789 		}
7790 	}
7791 	result = dns_rdatasetiter_first(iterator);
7792 	while (result == ISC_R_SUCCESS) {
7793 		isc_stdtime_t when;
7794 
7795 		dns_rdatasetiter_current(iterator, &rdataset);
7796 		if (rdataset.type == dns_rdatatype_soa ||
7797 		    rdataset.type == dns_rdatatype_rrsig) {
7798 			goto next_rdataset;
7799 		}
7800 		if (rdataset.type == dns_rdatatype_dnskey ||
7801 		    rdataset.type == dns_rdatatype_cdnskey ||
7802 		    rdataset.type == dns_rdatatype_cds)
7803 		{
7804 			/*
7805 			 * CDS and CDNSKEY are signed with KSK like DNSKEY.
7806 			 * (RFC 7344, section 4.1 specifies that they must
7807 			 * be signed with a key in the current DS RRset,
7808 			 * which would only include KSK's.)
7809 			 */
7810 			if (!is_ksk && keyset_kskonly) {
7811 				goto next_rdataset;
7812 			}
7813 		} else if (!is_zsk) {
7814 			goto next_rdataset;
7815 		} else if (is_zsk && !dst_key_is_signing(key, DST_BOOL_ZSK,
7816 							 inception, &when)) {
7817 			/* Only applies to dnssec-policy. */
7818 			if (dns_zone_getkasp(zone) != NULL) {
7819 				goto next_rdataset;
7820 			}
7821 		}
7822 
7823 		if (seen_ns && !seen_soa && rdataset.type != dns_rdatatype_ds &&
7824 		    rdataset.type != dns_rdatatype_nsec)
7825 		{
7826 			goto next_rdataset;
7827 		}
7828 		if (signed_with_good_key(zone, db, node, version, rdataset.type,
7829 					 key)) {
7830 			goto next_rdataset;
7831 		}
7832 
7833 		/* Calculate the signature, creating a RRSIG RDATA. */
7834 		isc_buffer_clear(&buffer);
7835 		CHECK(dns_dnssec_sign(name, &rdataset, key, &inception, &expire,
7836 				      mctx, &buffer, &rdata));
7837 		/* Update the database and journal with the RRSIG. */
7838 		/* XXX inefficient - will cause dataset merging */
7839 		CHECK(update_one_rr(db, version, diff, DNS_DIFFOP_ADDRESIGN,
7840 				    name, rdataset.ttl, &rdata));
7841 		dns_rdata_reset(&rdata);
7842 
7843 		/* Update DNSSEC sign statistics. */
7844 		dnssecsignstats = dns_zone_getdnssecsignstats(zone);
7845 		if (dnssecsignstats != NULL) {
7846 			/* Generated a new signature. */
7847 			dns_dnssecsignstats_increment(dnssecsignstats, ID(key),
7848 						      ALG(key),
7849 						      dns_dnssecsignstats_sign);
7850 			/* This is a refresh. */
7851 			dns_dnssecsignstats_increment(
7852 				dnssecsignstats, ID(key), ALG(key),
7853 				dns_dnssecsignstats_refresh);
7854 		}
7855 
7856 		(*signatures)--;
7857 	next_rdataset:
7858 		dns_rdataset_disassociate(&rdataset);
7859 		result = dns_rdatasetiter_next(iterator);
7860 	}
7861 	if (result == ISC_R_NOMORE) {
7862 		result = ISC_R_SUCCESS;
7863 	}
7864 failure:
7865 	if (dns_rdataset_isassociated(&rdataset)) {
7866 		dns_rdataset_disassociate(&rdataset);
7867 	}
7868 	if (iterator != NULL) {
7869 		dns_rdatasetiter_destroy(&iterator);
7870 	}
7871 	return (result);
7872 }
7873 
7874 /*
7875  * If 'update_only' is set then don't create a NSEC RRset if it doesn't exist.
7876  */
7877 static isc_result_t
updatesecure(dns_db_t * db,dns_dbversion_t * version,dns_name_t * name,dns_ttl_t nsecttl,bool update_only,dns_diff_t * diff)7878 updatesecure(dns_db_t *db, dns_dbversion_t *version, dns_name_t *name,
7879 	     dns_ttl_t nsecttl, bool update_only, dns_diff_t *diff) {
7880 	isc_result_t result;
7881 	dns_rdataset_t rdataset;
7882 	dns_dbnode_t *node = NULL;
7883 
7884 	CHECK(dns_db_getoriginnode(db, &node));
7885 	if (update_only) {
7886 		dns_rdataset_init(&rdataset);
7887 		result = dns_db_findrdataset(
7888 			db, node, version, dns_rdatatype_nsec,
7889 			dns_rdatatype_none, 0, &rdataset, NULL);
7890 		if (dns_rdataset_isassociated(&rdataset)) {
7891 			dns_rdataset_disassociate(&rdataset);
7892 		}
7893 		if (result == ISC_R_NOTFOUND) {
7894 			goto success;
7895 		}
7896 		if (result != ISC_R_SUCCESS) {
7897 			goto failure;
7898 		}
7899 	}
7900 	CHECK(delete_nsec(db, version, node, name, diff));
7901 	CHECK(add_nsec(db, version, name, node, nsecttl, false, diff));
7902 success:
7903 	result = ISC_R_SUCCESS;
7904 failure:
7905 	if (node != NULL) {
7906 		dns_db_detachnode(db, &node);
7907 	}
7908 	return (result);
7909 }
7910 
7911 static isc_result_t
updatesignwithkey(dns_zone_t * zone,dns_signing_t * signing,dns_dbversion_t * version,bool build_nsec3,dns_ttl_t nsecttl,dns_diff_t * diff)7912 updatesignwithkey(dns_zone_t *zone, dns_signing_t *signing,
7913 		  dns_dbversion_t *version, bool build_nsec3, dns_ttl_t nsecttl,
7914 		  dns_diff_t *diff) {
7915 	isc_result_t result;
7916 	dns_dbnode_t *node = NULL;
7917 	dns_rdataset_t rdataset;
7918 	dns_rdata_t rdata = DNS_RDATA_INIT;
7919 	unsigned char data[5];
7920 	bool seen_done = false;
7921 	bool have_rr = false;
7922 
7923 	dns_rdataset_init(&rdataset);
7924 	result = dns_db_getoriginnode(signing->db, &node);
7925 	if (result != ISC_R_SUCCESS) {
7926 		goto failure;
7927 	}
7928 
7929 	result = dns_db_findrdataset(signing->db, node, version,
7930 				     zone->privatetype, dns_rdatatype_none, 0,
7931 				     &rdataset, NULL);
7932 	if (result == ISC_R_NOTFOUND) {
7933 		INSIST(!dns_rdataset_isassociated(&rdataset));
7934 		result = ISC_R_SUCCESS;
7935 		goto failure;
7936 	}
7937 	if (result != ISC_R_SUCCESS) {
7938 		INSIST(!dns_rdataset_isassociated(&rdataset));
7939 		goto failure;
7940 	}
7941 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
7942 	     result = dns_rdataset_next(&rdataset))
7943 	{
7944 		dns_rdataset_current(&rdataset, &rdata);
7945 		/*
7946 		 * If we don't match the algorithm or keyid skip the record.
7947 		 */
7948 		if (rdata.length != 5 || rdata.data[0] != signing->algorithm ||
7949 		    rdata.data[1] != ((signing->keyid >> 8) & 0xff) ||
7950 		    rdata.data[2] != (signing->keyid & 0xff))
7951 		{
7952 			have_rr = true;
7953 			dns_rdata_reset(&rdata);
7954 			continue;
7955 		}
7956 		/*
7957 		 * We have a match.  If we were signing (!signing->deleteit)
7958 		 * and we already have a record indicating that we have
7959 		 * finished signing (rdata.data[4] != 0) then keep it.
7960 		 * Otherwise it needs to be deleted as we have removed all
7961 		 * the signatures (signing->deleteit), so any record indicating
7962 		 * completion is now out of date, or we have finished signing
7963 		 * with the new record so we no longer need to remember that
7964 		 * we need to sign the zone with the matching key across a
7965 		 * nameserver re-start.
7966 		 */
7967 		if (!signing->deleteit && rdata.data[4] != 0) {
7968 			seen_done = true;
7969 			have_rr = true;
7970 		} else {
7971 			CHECK(update_one_rr(signing->db, version, diff,
7972 					    DNS_DIFFOP_DEL, &zone->origin,
7973 					    rdataset.ttl, &rdata));
7974 		}
7975 		dns_rdata_reset(&rdata);
7976 	}
7977 	if (result == ISC_R_NOMORE) {
7978 		result = ISC_R_SUCCESS;
7979 	}
7980 	if (!signing->deleteit && !seen_done) {
7981 		/*
7982 		 * If we were signing then we need to indicate that we have
7983 		 * finished signing the zone with this key.  If it is already
7984 		 * there we don't need to add it a second time.
7985 		 */
7986 		data[0] = signing->algorithm;
7987 		data[1] = (signing->keyid >> 8) & 0xff;
7988 		data[2] = signing->keyid & 0xff;
7989 		data[3] = 0;
7990 		data[4] = 1;
7991 		rdata.length = sizeof(data);
7992 		rdata.data = data;
7993 		rdata.type = zone->privatetype;
7994 		rdata.rdclass = dns_db_class(signing->db);
7995 		CHECK(update_one_rr(signing->db, version, diff, DNS_DIFFOP_ADD,
7996 				    &zone->origin, rdataset.ttl, &rdata));
7997 	} else if (!have_rr) {
7998 		dns_name_t *origin = dns_db_origin(signing->db);
7999 		/*
8000 		 * Rebuild the NSEC/NSEC3 record for the origin as we no
8001 		 * longer have any private records.
8002 		 */
8003 		if (build_nsec3) {
8004 			CHECK(dns_nsec3_addnsec3s(signing->db, version, origin,
8005 						  nsecttl, false, diff));
8006 		}
8007 		CHECK(updatesecure(signing->db, version, origin, nsecttl, true,
8008 				   diff));
8009 	}
8010 
8011 failure:
8012 	if (dns_rdataset_isassociated(&rdataset)) {
8013 		dns_rdataset_disassociate(&rdataset);
8014 	}
8015 	if (node != NULL) {
8016 		dns_db_detachnode(signing->db, &node);
8017 	}
8018 	return (result);
8019 }
8020 
8021 /*
8022  * Called from zone_nsec3chain() in order to update zone records indicating
8023  * processing status of given NSEC3 chain:
8024  *
8025  *   - If the supplied dns_nsec3chain_t structure has been fully processed
8026  *     (which is indicated by "active" being set to false):
8027  *
8028  *       - remove all NSEC3PARAM records matching the relevant NSEC3 chain,
8029  *
8030  *       - remove all private-type records containing NSEC3PARAM RDATA matching
8031  *         the relevant NSEC3 chain.
8032  *
8033  *   - If the supplied dns_nsec3chain_t structure has not been fully processed
8034  *     (which is indicated by "active" being set to true), only remove the
8035  *     NSEC3PARAM record which matches the relevant NSEC3 chain and has the
8036  *     "flags" field set to 0.
8037  *
8038  *   - If given NSEC3 chain is being added, add an NSEC3PARAM record contained
8039  *     in the relevant private-type record, but with the "flags" field set to
8040  *     0, indicating that this NSEC3 chain is now complete for this zone.
8041  *
8042  * Note that this function is called at different processing stages for NSEC3
8043  * chain additions vs. removals and needs to handle all cases properly.
8044  */
8045 static isc_result_t
fixup_nsec3param(dns_db_t * db,dns_dbversion_t * ver,dns_nsec3chain_t * chain,bool active,dns_rdatatype_t privatetype,dns_diff_t * diff)8046 fixup_nsec3param(dns_db_t *db, dns_dbversion_t *ver, dns_nsec3chain_t *chain,
8047 		 bool active, dns_rdatatype_t privatetype, dns_diff_t *diff) {
8048 	dns_dbnode_t *node = NULL;
8049 	dns_name_t *name = dns_db_origin(db);
8050 	dns_rdata_t rdata = DNS_RDATA_INIT;
8051 	dns_rdataset_t rdataset;
8052 	dns_rdata_nsec3param_t nsec3param;
8053 	isc_result_t result;
8054 	isc_buffer_t buffer;
8055 	unsigned char parambuf[DNS_NSEC3PARAM_BUFFERSIZE];
8056 	dns_ttl_t ttl = 0;
8057 	bool nseconly = false, nsec3ok = false;
8058 
8059 	dns_rdataset_init(&rdataset);
8060 
8061 	result = dns_db_getoriginnode(db, &node);
8062 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
8063 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec3param, 0,
8064 				     0, &rdataset, NULL);
8065 	if (result == ISC_R_NOTFOUND) {
8066 		goto try_private;
8067 	}
8068 	if (result != ISC_R_SUCCESS) {
8069 		goto failure;
8070 	}
8071 
8072 	/*
8073 	 * Preserve the existing ttl.
8074 	 */
8075 	ttl = rdataset.ttl;
8076 
8077 	/*
8078 	 * Delete all NSEC3PARAM records which match that in nsec3chain.
8079 	 */
8080 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
8081 	     result = dns_rdataset_next(&rdataset))
8082 	{
8083 		dns_rdataset_current(&rdataset, &rdata);
8084 		CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL));
8085 
8086 		if (nsec3param.hash != chain->nsec3param.hash ||
8087 		    (active && nsec3param.flags != 0) ||
8088 		    nsec3param.iterations != chain->nsec3param.iterations ||
8089 		    nsec3param.salt_length != chain->nsec3param.salt_length ||
8090 		    memcmp(nsec3param.salt, chain->nsec3param.salt,
8091 			   nsec3param.salt_length))
8092 		{
8093 			dns_rdata_reset(&rdata);
8094 			continue;
8095 		}
8096 
8097 		CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name,
8098 				    rdataset.ttl, &rdata));
8099 		dns_rdata_reset(&rdata);
8100 	}
8101 	if (result != ISC_R_NOMORE) {
8102 		goto failure;
8103 	}
8104 
8105 	dns_rdataset_disassociate(&rdataset);
8106 
8107 try_private:
8108 
8109 	if (active) {
8110 		goto add;
8111 	}
8112 
8113 	result = dns_nsec_nseconly(db, ver, &nseconly);
8114 	nsec3ok = (result == ISC_R_SUCCESS && !nseconly);
8115 
8116 	/*
8117 	 * Delete all private records which match that in nsec3chain.
8118 	 */
8119 	result = dns_db_findrdataset(db, node, ver, privatetype, 0, 0,
8120 				     &rdataset, NULL);
8121 	if (result == ISC_R_NOTFOUND) {
8122 		goto add;
8123 	}
8124 	if (result != ISC_R_SUCCESS) {
8125 		goto failure;
8126 	}
8127 
8128 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
8129 	     result = dns_rdataset_next(&rdataset))
8130 	{
8131 		dns_rdata_t private = DNS_RDATA_INIT;
8132 		unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
8133 
8134 		dns_rdataset_current(&rdataset, &private);
8135 		if (!dns_nsec3param_fromprivate(&private, &rdata, buf,
8136 						sizeof(buf))) {
8137 			continue;
8138 		}
8139 		CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL));
8140 
8141 		if ((!nsec3ok &&
8142 		     (nsec3param.flags & DNS_NSEC3FLAG_INITIAL) != 0) ||
8143 		    nsec3param.hash != chain->nsec3param.hash ||
8144 		    nsec3param.iterations != chain->nsec3param.iterations ||
8145 		    nsec3param.salt_length != chain->nsec3param.salt_length ||
8146 		    memcmp(nsec3param.salt, chain->nsec3param.salt,
8147 			   nsec3param.salt_length))
8148 		{
8149 			dns_rdata_reset(&rdata);
8150 			continue;
8151 		}
8152 
8153 		CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name,
8154 				    rdataset.ttl, &private));
8155 		dns_rdata_reset(&rdata);
8156 	}
8157 	if (result != ISC_R_NOMORE) {
8158 		goto failure;
8159 	}
8160 
8161 add:
8162 	if ((chain->nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0) {
8163 		result = ISC_R_SUCCESS;
8164 		goto failure;
8165 	}
8166 
8167 	/*
8168 	 * Add a NSEC3PARAM record which matches that in nsec3chain but
8169 	 * with all flags bits cleared.
8170 	 *
8171 	 * Note: we do not clear chain->nsec3param.flags as this change
8172 	 * may be reversed.
8173 	 */
8174 	isc_buffer_init(&buffer, &parambuf, sizeof(parambuf));
8175 	CHECK(dns_rdata_fromstruct(&rdata, dns_db_class(db),
8176 				   dns_rdatatype_nsec3param, &chain->nsec3param,
8177 				   &buffer));
8178 	rdata.data[1] = 0; /* Clear flag bits. */
8179 	CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADD, name, ttl, &rdata));
8180 
8181 failure:
8182 	dns_db_detachnode(db, &node);
8183 	if (dns_rdataset_isassociated(&rdataset)) {
8184 		dns_rdataset_disassociate(&rdataset);
8185 	}
8186 	return (result);
8187 }
8188 
8189 static isc_result_t
delete_nsec(dns_db_t * db,dns_dbversion_t * ver,dns_dbnode_t * node,dns_name_t * name,dns_diff_t * diff)8190 delete_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_dbnode_t *node,
8191 	    dns_name_t *name, dns_diff_t *diff) {
8192 	dns_rdataset_t rdataset;
8193 	isc_result_t result;
8194 
8195 	dns_rdataset_init(&rdataset);
8196 
8197 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec, 0, 0,
8198 				     &rdataset, NULL);
8199 	if (result == ISC_R_NOTFOUND) {
8200 		return (ISC_R_SUCCESS);
8201 	}
8202 	if (result != ISC_R_SUCCESS) {
8203 		return (result);
8204 	}
8205 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
8206 	     result = dns_rdataset_next(&rdataset))
8207 	{
8208 		dns_rdata_t rdata = DNS_RDATA_INIT;
8209 
8210 		dns_rdataset_current(&rdataset, &rdata);
8211 		CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name,
8212 				    rdataset.ttl, &rdata));
8213 	}
8214 	if (result == ISC_R_NOMORE) {
8215 		result = ISC_R_SUCCESS;
8216 	}
8217 failure:
8218 	dns_rdataset_disassociate(&rdataset);
8219 	return (result);
8220 }
8221 
8222 static isc_result_t
deletematchingnsec3(dns_db_t * db,dns_dbversion_t * ver,dns_dbnode_t * node,dns_name_t * name,const dns_rdata_nsec3param_t * param,dns_diff_t * diff)8223 deletematchingnsec3(dns_db_t *db, dns_dbversion_t *ver, dns_dbnode_t *node,
8224 		    dns_name_t *name, const dns_rdata_nsec3param_t *param,
8225 		    dns_diff_t *diff) {
8226 	dns_rdataset_t rdataset;
8227 	dns_rdata_nsec3_t nsec3;
8228 	isc_result_t result;
8229 
8230 	dns_rdataset_init(&rdataset);
8231 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec3, 0, 0,
8232 				     &rdataset, NULL);
8233 	if (result == ISC_R_NOTFOUND) {
8234 		return (ISC_R_SUCCESS);
8235 	}
8236 	if (result != ISC_R_SUCCESS) {
8237 		return (result);
8238 	}
8239 
8240 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
8241 	     result = dns_rdataset_next(&rdataset))
8242 	{
8243 		dns_rdata_t rdata = DNS_RDATA_INIT;
8244 
8245 		dns_rdataset_current(&rdataset, &rdata);
8246 		CHECK(dns_rdata_tostruct(&rdata, &nsec3, NULL));
8247 		if (nsec3.hash != param->hash ||
8248 		    nsec3.iterations != param->iterations ||
8249 		    nsec3.salt_length != param->salt_length ||
8250 		    memcmp(nsec3.salt, param->salt, nsec3.salt_length))
8251 		{
8252 			continue;
8253 		}
8254 		CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name,
8255 				    rdataset.ttl, &rdata));
8256 	}
8257 	if (result == ISC_R_NOMORE) {
8258 		result = ISC_R_SUCCESS;
8259 	}
8260 failure:
8261 	dns_rdataset_disassociate(&rdataset);
8262 	return (result);
8263 }
8264 
8265 static isc_result_t
need_nsec_chain(dns_db_t * db,dns_dbversion_t * ver,const dns_rdata_nsec3param_t * param,bool * answer)8266 need_nsec_chain(dns_db_t *db, dns_dbversion_t *ver,
8267 		const dns_rdata_nsec3param_t *param, bool *answer) {
8268 	dns_dbnode_t *node = NULL;
8269 	dns_rdata_t rdata = DNS_RDATA_INIT;
8270 	dns_rdata_nsec3param_t myparam;
8271 	dns_rdataset_t rdataset;
8272 	isc_result_t result;
8273 
8274 	*answer = false;
8275 
8276 	result = dns_db_getoriginnode(db, &node);
8277 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
8278 
8279 	dns_rdataset_init(&rdataset);
8280 
8281 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec, 0, 0,
8282 				     &rdataset, NULL);
8283 	if (result == ISC_R_SUCCESS) {
8284 		dns_rdataset_disassociate(&rdataset);
8285 		dns_db_detachnode(db, &node);
8286 		return (result);
8287 	}
8288 	if (result != ISC_R_NOTFOUND) {
8289 		dns_db_detachnode(db, &node);
8290 		return (result);
8291 	}
8292 
8293 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec3param, 0,
8294 				     0, &rdataset, NULL);
8295 	if (result == ISC_R_NOTFOUND) {
8296 		*answer = true;
8297 		dns_db_detachnode(db, &node);
8298 		return (ISC_R_SUCCESS);
8299 	}
8300 	if (result != ISC_R_SUCCESS) {
8301 		dns_db_detachnode(db, &node);
8302 		return (result);
8303 	}
8304 
8305 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
8306 	     result = dns_rdataset_next(&rdataset))
8307 	{
8308 		dns_rdataset_current(&rdataset, &rdata);
8309 		CHECK(dns_rdata_tostruct(&rdata, &myparam, NULL));
8310 		dns_rdata_reset(&rdata);
8311 		/*
8312 		 * Ignore any NSEC3PARAM removals.
8313 		 */
8314 		if (NSEC3REMOVE(myparam.flags)) {
8315 			continue;
8316 		}
8317 		/*
8318 		 * Ignore the chain that we are in the process of deleting.
8319 		 */
8320 		if (myparam.hash == param->hash &&
8321 		    myparam.iterations == param->iterations &&
8322 		    myparam.salt_length == param->salt_length &&
8323 		    !memcmp(myparam.salt, param->salt, myparam.salt_length))
8324 		{
8325 			continue;
8326 		}
8327 		/*
8328 		 * Found an active NSEC3 chain.
8329 		 */
8330 		break;
8331 	}
8332 	if (result == ISC_R_NOMORE) {
8333 		*answer = true;
8334 		result = ISC_R_SUCCESS;
8335 	}
8336 
8337 failure:
8338 	if (dns_rdataset_isassociated(&rdataset)) {
8339 		dns_rdataset_disassociate(&rdataset);
8340 	}
8341 	dns_db_detachnode(db, &node);
8342 	return (result);
8343 }
8344 
8345 /*%
8346  * Given a tuple which is part of a diff, return a pointer to the next tuple in
8347  * that diff which has the same name and type (or NULL if no such tuple is
8348  * found).
8349  */
8350 static dns_difftuple_t *
find_next_matching_tuple(dns_difftuple_t * cur)8351 find_next_matching_tuple(dns_difftuple_t *cur) {
8352 	dns_difftuple_t *next = cur;
8353 
8354 	while ((next = ISC_LIST_NEXT(next, link)) != NULL) {
8355 		if (cur->rdata.type == next->rdata.type &&
8356 		    dns_name_equal(&cur->name, &next->name))
8357 		{
8358 			return (next);
8359 		}
8360 	}
8361 
8362 	return (NULL);
8363 }
8364 
8365 /*%
8366  * Remove all tuples with the same name and type as 'cur' from 'src' and append
8367  * them to 'dst'.
8368  */
8369 static void
move_matching_tuples(dns_difftuple_t * cur,dns_diff_t * src,dns_diff_t * dst)8370 move_matching_tuples(dns_difftuple_t *cur, dns_diff_t *src, dns_diff_t *dst) {
8371 	do {
8372 		dns_difftuple_t *next = find_next_matching_tuple(cur);
8373 		ISC_LIST_UNLINK(src->tuples, cur, link);
8374 		dns_diff_appendminimal(dst, &cur);
8375 		cur = next;
8376 	} while (cur != NULL);
8377 }
8378 
8379 /*%
8380  * Add/remove DNSSEC signatures for the list of "raw" zone changes supplied in
8381  * 'diff'.  Gradually remove tuples from 'diff' and append them to 'zonediff'
8382  * along with tuples representing relevant signature changes.
8383  */
8384 isc_result_t
dns__zone_updatesigs(dns_diff_t * diff,dns_db_t * db,dns_dbversion_t * version,dst_key_t * zone_keys[],unsigned int nkeys,dns_zone_t * zone,isc_stdtime_t inception,isc_stdtime_t expire,isc_stdtime_t keyexpire,isc_stdtime_t now,bool check_ksk,bool keyset_kskonly,dns__zonediff_t * zonediff)8385 dns__zone_updatesigs(dns_diff_t *diff, dns_db_t *db, dns_dbversion_t *version,
8386 		     dst_key_t *zone_keys[], unsigned int nkeys,
8387 		     dns_zone_t *zone, isc_stdtime_t inception,
8388 		     isc_stdtime_t expire, isc_stdtime_t keyexpire,
8389 		     isc_stdtime_t now, bool check_ksk, bool keyset_kskonly,
8390 		     dns__zonediff_t *zonediff) {
8391 	dns_difftuple_t *tuple;
8392 	isc_result_t result;
8393 
8394 	while ((tuple = ISC_LIST_HEAD(diff->tuples)) != NULL) {
8395 		isc_stdtime_t exp = expire;
8396 
8397 		if (keyexpire != 0 &&
8398 		    (tuple->rdata.type == dns_rdatatype_dnskey ||
8399 		     tuple->rdata.type == dns_rdatatype_cdnskey ||
8400 		     tuple->rdata.type == dns_rdatatype_cds))
8401 		{
8402 			exp = keyexpire;
8403 		}
8404 
8405 		result = del_sigs(zone, db, version, &tuple->name,
8406 				  tuple->rdata.type, zonediff, zone_keys, nkeys,
8407 				  now, false);
8408 		if (result != ISC_R_SUCCESS) {
8409 			dns_zone_log(zone, ISC_LOG_ERROR,
8410 				     "dns__zone_updatesigs:del_sigs -> %s",
8411 				     dns_result_totext(result));
8412 			return (result);
8413 		}
8414 		result = add_sigs(db, version, &tuple->name, zone,
8415 				  tuple->rdata.type, zonediff->diff, zone_keys,
8416 				  nkeys, zone->mctx, inception, exp, check_ksk,
8417 				  keyset_kskonly);
8418 		if (result != ISC_R_SUCCESS) {
8419 			dns_zone_log(zone, ISC_LOG_ERROR,
8420 				     "dns__zone_updatesigs:add_sigs -> %s",
8421 				     dns_result_totext(result));
8422 			return (result);
8423 		}
8424 
8425 		/*
8426 		 * Signature changes for all RRs with name tuple->name and type
8427 		 * tuple->rdata.type were appended to zonediff->diff.  Now we
8428 		 * remove all the "raw" changes with the same name and type
8429 		 * from diff (so that they are not processed by this loop
8430 		 * again) and append them to zonediff so that they get applied.
8431 		 */
8432 		move_matching_tuples(tuple, diff, zonediff->diff);
8433 	}
8434 	return (ISC_R_SUCCESS);
8435 }
8436 
8437 /*
8438  * Incrementally build and sign a new NSEC3 chain using the parameters
8439  * requested.
8440  */
8441 static void
zone_nsec3chain(dns_zone_t * zone)8442 zone_nsec3chain(dns_zone_t *zone) {
8443 	const char *me = "zone_nsec3chain";
8444 	dns_db_t *db = NULL;
8445 	dns_dbnode_t *node = NULL;
8446 	dns_dbversion_t *version = NULL;
8447 	dns_diff_t _sig_diff;
8448 	dns_diff_t nsec_diff;
8449 	dns_diff_t nsec3_diff;
8450 	dns_diff_t param_diff;
8451 	dns__zonediff_t zonediff;
8452 	dns_fixedname_t fixed;
8453 	dns_fixedname_t nextfixed;
8454 	dns_name_t *name, *nextname;
8455 	dns_rdataset_t rdataset;
8456 	dns_nsec3chain_t *nsec3chain = NULL, *nextnsec3chain;
8457 	dns_nsec3chainlist_t cleanup;
8458 	dst_key_t *zone_keys[DNS_MAXZONEKEYS];
8459 	int32_t signatures;
8460 	bool check_ksk, keyset_kskonly;
8461 	bool delegation;
8462 	bool first;
8463 	isc_result_t result;
8464 	isc_stdtime_t now, inception, soaexpire, expire;
8465 	uint32_t jitter, sigvalidityinterval, expiryinterval;
8466 	unsigned int i;
8467 	unsigned int nkeys = 0;
8468 	uint32_t nodes;
8469 	bool unsecure = false;
8470 	bool seen_soa, seen_ns, seen_dname, seen_ds;
8471 	bool seen_nsec, seen_nsec3, seen_rr;
8472 	dns_rdatasetiter_t *iterator = NULL;
8473 	bool buildnsecchain;
8474 	bool updatensec = false;
8475 	dns_rdatatype_t privatetype = zone->privatetype;
8476 
8477 	ENTER;
8478 
8479 	dns_rdataset_init(&rdataset);
8480 	name = dns_fixedname_initname(&fixed);
8481 	nextname = dns_fixedname_initname(&nextfixed);
8482 	dns_diff_init(zone->mctx, &param_diff);
8483 	dns_diff_init(zone->mctx, &nsec3_diff);
8484 	dns_diff_init(zone->mctx, &nsec_diff);
8485 	dns_diff_init(zone->mctx, &_sig_diff);
8486 	zonediff_init(&zonediff, &_sig_diff);
8487 	ISC_LIST_INIT(cleanup);
8488 
8489 	/*
8490 	 * Updates are disabled.  Pause for 5 minutes.
8491 	 */
8492 	if (zone->update_disabled) {
8493 		result = ISC_R_FAILURE;
8494 		goto failure;
8495 	}
8496 
8497 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
8498 	/*
8499 	 * This function is called when zone timer fires, after the latter gets
8500 	 * set by zone_addnsec3chain().  If the action triggering the call to
8501 	 * zone_addnsec3chain() is closely followed by a zone deletion request,
8502 	 * it might turn out that the timer thread will not be woken up until
8503 	 * after the zone is deleted by rmzone(), which calls dns_db_detach()
8504 	 * for zone->db, causing the latter to become NULL.  Return immediately
8505 	 * if that happens.
8506 	 */
8507 	if (zone->db != NULL) {
8508 		dns_db_attach(zone->db, &db);
8509 	}
8510 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
8511 	if (db == NULL) {
8512 		return;
8513 	}
8514 
8515 	result = dns_db_newversion(db, &version);
8516 	if (result != ISC_R_SUCCESS) {
8517 		dnssec_log(zone, ISC_LOG_ERROR,
8518 			   "zone_nsec3chain:dns_db_newversion -> %s",
8519 			   dns_result_totext(result));
8520 		goto failure;
8521 	}
8522 
8523 	isc_stdtime_get(&now);
8524 
8525 	result = dns__zone_findkeys(zone, db, version, now, zone->mctx,
8526 				    DNS_MAXZONEKEYS, zone_keys, &nkeys);
8527 	if (result != ISC_R_SUCCESS) {
8528 		dnssec_log(zone, ISC_LOG_ERROR,
8529 			   "zone_nsec3chain:dns__zone_findkeys -> %s",
8530 			   dns_result_totext(result));
8531 		goto failure;
8532 	}
8533 
8534 	sigvalidityinterval = dns_zone_getsigvalidityinterval(zone);
8535 	inception = now - 3600; /* Allow for clock skew. */
8536 	soaexpire = now + sigvalidityinterval;
8537 	expiryinterval = dns_zone_getsigresigninginterval(zone);
8538 	if (expiryinterval > sigvalidityinterval) {
8539 		expiryinterval = sigvalidityinterval;
8540 	} else {
8541 		expiryinterval = sigvalidityinterval - expiryinterval;
8542 	}
8543 
8544 	/*
8545 	 * Spread out signatures over time if they happen to be
8546 	 * clumped.  We don't do this for each add_sigs() call as
8547 	 * we still want some clustering to occur.
8548 	 */
8549 	if (sigvalidityinterval >= 3600U) {
8550 		if (sigvalidityinterval > 7200U) {
8551 			jitter = isc_random_uniform(expiryinterval);
8552 		} else {
8553 			jitter = isc_random_uniform(1200);
8554 		}
8555 		expire = soaexpire - jitter - 1;
8556 	} else {
8557 		expire = soaexpire - 1;
8558 	}
8559 
8560 	check_ksk = DNS_ZONE_OPTION(zone, DNS_ZONEOPT_UPDATECHECKKSK);
8561 	keyset_kskonly = DNS_ZONE_OPTION(zone, DNS_ZONEOPT_DNSKEYKSKONLY);
8562 
8563 	/*
8564 	 * We keep pulling nodes off each iterator in turn until
8565 	 * we have no more nodes to pull off or we reach the limits
8566 	 * for this quantum.
8567 	 */
8568 	nodes = zone->nodes;
8569 	signatures = zone->signatures;
8570 	LOCK_ZONE(zone);
8571 	nsec3chain = ISC_LIST_HEAD(zone->nsec3chain);
8572 	UNLOCK_ZONE(zone);
8573 	first = true;
8574 
8575 	if (nsec3chain != NULL) {
8576 		nsec3chain->save_delete_nsec = nsec3chain->delete_nsec;
8577 	}
8578 	/*
8579 	 * Generate new NSEC3 chains first.
8580 	 *
8581 	 * The following while loop iterates over nodes in the zone database,
8582 	 * updating the NSEC3 chain by calling dns_nsec3_addnsec3() for each of
8583 	 * them.  Once all nodes are processed, the "delete_nsec" field is
8584 	 * consulted to check whether we are supposed to remove NSEC records
8585 	 * from the zone database; if so, the database iterator is reset to
8586 	 * point to the first node and the loop traverses all of them again,
8587 	 * this time removing NSEC records.  If we hit a node which is obscured
8588 	 * by a delegation or a DNAME, nodes are skipped over until we find one
8589 	 * that is not obscured by the same obscuring name and then normal
8590 	 * processing is resumed.
8591 	 *
8592 	 * The above is repeated until all requested NSEC3 chain changes are
8593 	 * applied or when we reach the limits for this quantum, whichever
8594 	 * happens first.
8595 	 *
8596 	 * Note that the "signatures" variable is only used here to limit the
8597 	 * amount of work performed.  Actual DNSSEC signatures are only
8598 	 * generated by dns__zone_updatesigs() calls later in this function.
8599 	 */
8600 	while (nsec3chain != NULL && nodes-- > 0 && signatures > 0) {
8601 		dns_dbiterator_pause(nsec3chain->dbiterator);
8602 
8603 		LOCK_ZONE(zone);
8604 		nextnsec3chain = ISC_LIST_NEXT(nsec3chain, link);
8605 
8606 		ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
8607 		if (nsec3chain->done || nsec3chain->db != zone->db) {
8608 			ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain, link);
8609 			ISC_LIST_APPEND(cleanup, nsec3chain, link);
8610 		}
8611 		ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
8612 		UNLOCK_ZONE(zone);
8613 		if (ISC_LIST_TAIL(cleanup) == nsec3chain) {
8614 			goto next_addchain;
8615 		}
8616 
8617 		/*
8618 		 * Possible future db.
8619 		 */
8620 		if (nsec3chain->db != db) {
8621 			goto next_addchain;
8622 		}
8623 
8624 		if (NSEC3REMOVE(nsec3chain->nsec3param.flags)) {
8625 			goto next_addchain;
8626 		}
8627 
8628 		dns_dbiterator_current(nsec3chain->dbiterator, &node, name);
8629 
8630 		if (nsec3chain->delete_nsec) {
8631 			delegation = false;
8632 			dns_dbiterator_pause(nsec3chain->dbiterator);
8633 			CHECK(delete_nsec(db, version, node, name, &nsec_diff));
8634 			goto next_addnode;
8635 		}
8636 		/*
8637 		 * On the first pass we need to check if the current node
8638 		 * has not been obscured.
8639 		 */
8640 		delegation = false;
8641 		unsecure = false;
8642 		if (first) {
8643 			dns_fixedname_t ffound;
8644 			dns_name_t *found;
8645 			found = dns_fixedname_initname(&ffound);
8646 			result = dns_db_find(
8647 				db, name, version, dns_rdatatype_soa,
8648 				DNS_DBFIND_NOWILD, 0, NULL, found, NULL, NULL);
8649 			if ((result == DNS_R_DELEGATION ||
8650 			     result == DNS_R_DNAME) &&
8651 			    !dns_name_equal(name, found))
8652 			{
8653 				/*
8654 				 * Remember the obscuring name so that
8655 				 * we skip all obscured names.
8656 				 */
8657 				dns_name_copynf(found, name);
8658 				delegation = true;
8659 				goto next_addnode;
8660 			}
8661 		}
8662 
8663 		/*
8664 		 * Check to see if this is a bottom of zone node.
8665 		 */
8666 		result = dns_db_allrdatasets(db, node, version, 0, &iterator);
8667 		if (result == ISC_R_NOTFOUND) {
8668 			/* Empty node? */
8669 			goto next_addnode;
8670 		}
8671 		if (result != ISC_R_SUCCESS) {
8672 			goto failure;
8673 		}
8674 
8675 		seen_soa = seen_ns = seen_dname = seen_ds = seen_nsec = false;
8676 		for (result = dns_rdatasetiter_first(iterator);
8677 		     result == ISC_R_SUCCESS;
8678 		     result = dns_rdatasetiter_next(iterator))
8679 		{
8680 			dns_rdatasetiter_current(iterator, &rdataset);
8681 			INSIST(rdataset.type != dns_rdatatype_nsec3);
8682 			if (rdataset.type == dns_rdatatype_soa) {
8683 				seen_soa = true;
8684 			} else if (rdataset.type == dns_rdatatype_ns) {
8685 				seen_ns = true;
8686 			} else if (rdataset.type == dns_rdatatype_dname) {
8687 				seen_dname = true;
8688 			} else if (rdataset.type == dns_rdatatype_ds) {
8689 				seen_ds = true;
8690 			} else if (rdataset.type == dns_rdatatype_nsec) {
8691 				seen_nsec = true;
8692 			}
8693 			dns_rdataset_disassociate(&rdataset);
8694 		}
8695 		dns_rdatasetiter_destroy(&iterator);
8696 		/*
8697 		 * Is there a NSEC chain than needs to be cleaned up?
8698 		 */
8699 		if (seen_nsec) {
8700 			nsec3chain->seen_nsec = true;
8701 		}
8702 		if (seen_ns && !seen_soa && !seen_ds) {
8703 			unsecure = true;
8704 		}
8705 		if ((seen_ns && !seen_soa) || seen_dname) {
8706 			delegation = true;
8707 		}
8708 
8709 		/*
8710 		 * Process one node.
8711 		 */
8712 		dns_dbiterator_pause(nsec3chain->dbiterator);
8713 		result = dns_nsec3_addnsec3(
8714 			db, version, name, &nsec3chain->nsec3param,
8715 			zone_nsecttl(zone), unsecure, &nsec3_diff);
8716 		if (result != ISC_R_SUCCESS) {
8717 			dnssec_log(zone, ISC_LOG_ERROR,
8718 				   "zone_nsec3chain:"
8719 				   "dns_nsec3_addnsec3 -> %s",
8720 				   dns_result_totext(result));
8721 			goto failure;
8722 		}
8723 
8724 		/*
8725 		 * Treat each call to dns_nsec3_addnsec3() as if it's cost is
8726 		 * two signatures.  Additionally there will, in general, be
8727 		 * two signature generated below.
8728 		 *
8729 		 * If we are only changing the optout flag the cost is half
8730 		 * that of the cost of generating a completely new chain.
8731 		 */
8732 		signatures -= 4;
8733 
8734 		/*
8735 		 * Go onto next node.
8736 		 */
8737 	next_addnode:
8738 		first = false;
8739 		dns_db_detachnode(db, &node);
8740 		do {
8741 			result = dns_dbiterator_next(nsec3chain->dbiterator);
8742 
8743 			if (result == ISC_R_NOMORE && nsec3chain->delete_nsec) {
8744 				dns_dbiterator_pause(nsec3chain->dbiterator);
8745 				CHECK(fixup_nsec3param(db, version, nsec3chain,
8746 						       false, privatetype,
8747 						       &param_diff));
8748 				LOCK_ZONE(zone);
8749 				ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain,
8750 						link);
8751 				UNLOCK_ZONE(zone);
8752 				ISC_LIST_APPEND(cleanup, nsec3chain, link);
8753 				goto next_addchain;
8754 			}
8755 			if (result == ISC_R_NOMORE) {
8756 				dns_dbiterator_pause(nsec3chain->dbiterator);
8757 				if (nsec3chain->seen_nsec) {
8758 					CHECK(fixup_nsec3param(
8759 						db, version, nsec3chain, true,
8760 						privatetype, &param_diff));
8761 					nsec3chain->delete_nsec = true;
8762 					goto same_addchain;
8763 				}
8764 				CHECK(fixup_nsec3param(db, version, nsec3chain,
8765 						       false, privatetype,
8766 						       &param_diff));
8767 				LOCK_ZONE(zone);
8768 				ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain,
8769 						link);
8770 				UNLOCK_ZONE(zone);
8771 				ISC_LIST_APPEND(cleanup, nsec3chain, link);
8772 				goto next_addchain;
8773 			} else if (result != ISC_R_SUCCESS) {
8774 				dnssec_log(zone, ISC_LOG_ERROR,
8775 					   "zone_nsec3chain:"
8776 					   "dns_dbiterator_next -> %s",
8777 					   dns_result_totext(result));
8778 				goto failure;
8779 			} else if (delegation) {
8780 				dns_dbiterator_current(nsec3chain->dbiterator,
8781 						       &node, nextname);
8782 				dns_db_detachnode(db, &node);
8783 				if (!dns_name_issubdomain(nextname, name)) {
8784 					break;
8785 				}
8786 			} else {
8787 				break;
8788 			}
8789 		} while (1);
8790 		continue;
8791 
8792 	same_addchain:
8793 		CHECK(dns_dbiterator_first(nsec3chain->dbiterator));
8794 		first = true;
8795 		continue;
8796 
8797 	next_addchain:
8798 		dns_dbiterator_pause(nsec3chain->dbiterator);
8799 		nsec3chain = nextnsec3chain;
8800 		first = true;
8801 		if (nsec3chain != NULL) {
8802 			nsec3chain->save_delete_nsec = nsec3chain->delete_nsec;
8803 		}
8804 	}
8805 
8806 	if (nsec3chain != NULL) {
8807 		goto skip_removals;
8808 	}
8809 
8810 	/*
8811 	 * Process removals.
8812 	 *
8813 	 * This is a counterpart of the above while loop which takes care of
8814 	 * removing an NSEC3 chain.  It starts with determining whether the
8815 	 * zone needs to switch from NSEC3 to NSEC; if so, it first builds an
8816 	 * NSEC chain by iterating over all nodes in the zone database and only
8817 	 * then goes on to remove NSEC3 records be iterating over all nodes
8818 	 * again and calling deletematchingnsec3() for each of them; otherwise,
8819 	 * it starts removing NSEC3 records immediately.  Rules for processing
8820 	 * obscured nodes and interrupting work are the same as for the while
8821 	 * loop above.
8822 	 */
8823 	LOCK_ZONE(zone);
8824 	nsec3chain = ISC_LIST_HEAD(zone->nsec3chain);
8825 	UNLOCK_ZONE(zone);
8826 	first = true;
8827 	buildnsecchain = false;
8828 	while (nsec3chain != NULL && nodes-- > 0 && signatures > 0) {
8829 		dns_dbiterator_pause(nsec3chain->dbiterator);
8830 
8831 		LOCK_ZONE(zone);
8832 		nextnsec3chain = ISC_LIST_NEXT(nsec3chain, link);
8833 		UNLOCK_ZONE(zone);
8834 
8835 		if (nsec3chain->db != db) {
8836 			goto next_removechain;
8837 		}
8838 
8839 		if (!NSEC3REMOVE(nsec3chain->nsec3param.flags)) {
8840 			goto next_removechain;
8841 		}
8842 
8843 		/*
8844 		 * Work out if we need to build a NSEC chain as a consequence
8845 		 * of removing this NSEC3 chain.
8846 		 */
8847 		if (first && !updatensec &&
8848 		    (nsec3chain->nsec3param.flags & DNS_NSEC3FLAG_NONSEC) == 0)
8849 		{
8850 			result = need_nsec_chain(db, version,
8851 						 &nsec3chain->nsec3param,
8852 						 &buildnsecchain);
8853 			if (result != ISC_R_SUCCESS) {
8854 				dnssec_log(zone, ISC_LOG_ERROR,
8855 					   "zone_nsec3chain:"
8856 					   "need_nsec_chain -> %s",
8857 					   dns_result_totext(result));
8858 				goto failure;
8859 			}
8860 		}
8861 
8862 		if (first) {
8863 			dnssec_log(zone, ISC_LOG_DEBUG(3),
8864 				   "zone_nsec3chain:buildnsecchain = %u\n",
8865 				   buildnsecchain);
8866 		}
8867 
8868 		dns_dbiterator_current(nsec3chain->dbiterator, &node, name);
8869 		dns_dbiterator_pause(nsec3chain->dbiterator);
8870 		delegation = false;
8871 
8872 		if (!buildnsecchain) {
8873 			/*
8874 			 * Delete the NSEC3PARAM record matching this chain.
8875 			 */
8876 			if (first) {
8877 				result = fixup_nsec3param(
8878 					db, version, nsec3chain, true,
8879 					privatetype, &param_diff);
8880 				if (result != ISC_R_SUCCESS) {
8881 					dnssec_log(zone, ISC_LOG_ERROR,
8882 						   "zone_nsec3chain:"
8883 						   "fixup_nsec3param -> %s",
8884 						   dns_result_totext(result));
8885 					goto failure;
8886 				}
8887 			}
8888 
8889 			/*
8890 			 * Delete the NSEC3 records.
8891 			 */
8892 			result = deletematchingnsec3(db, version, node, name,
8893 						     &nsec3chain->nsec3param,
8894 						     &nsec3_diff);
8895 			if (result != ISC_R_SUCCESS) {
8896 				dnssec_log(zone, ISC_LOG_ERROR,
8897 					   "zone_nsec3chain:"
8898 					   "deletematchingnsec3 -> %s",
8899 					   dns_result_totext(result));
8900 				goto failure;
8901 			}
8902 			goto next_removenode;
8903 		}
8904 
8905 		if (first) {
8906 			dns_fixedname_t ffound;
8907 			dns_name_t *found;
8908 			found = dns_fixedname_initname(&ffound);
8909 			result = dns_db_find(
8910 				db, name, version, dns_rdatatype_soa,
8911 				DNS_DBFIND_NOWILD, 0, NULL, found, NULL, NULL);
8912 			if ((result == DNS_R_DELEGATION ||
8913 			     result == DNS_R_DNAME) &&
8914 			    !dns_name_equal(name, found))
8915 			{
8916 				/*
8917 				 * Remember the obscuring name so that
8918 				 * we skip all obscured names.
8919 				 */
8920 				dns_name_copynf(found, name);
8921 				delegation = true;
8922 				goto next_removenode;
8923 			}
8924 		}
8925 
8926 		/*
8927 		 * Check to see if this is a bottom of zone node.
8928 		 */
8929 		result = dns_db_allrdatasets(db, node, version, 0, &iterator);
8930 		if (result == ISC_R_NOTFOUND) {
8931 			/* Empty node? */
8932 			goto next_removenode;
8933 		}
8934 		if (result != ISC_R_SUCCESS) {
8935 			goto failure;
8936 		}
8937 
8938 		seen_soa = seen_ns = seen_dname = seen_nsec3 = seen_nsec =
8939 			seen_rr = false;
8940 		for (result = dns_rdatasetiter_first(iterator);
8941 		     result == ISC_R_SUCCESS;
8942 		     result = dns_rdatasetiter_next(iterator))
8943 		{
8944 			dns_rdatasetiter_current(iterator, &rdataset);
8945 			if (rdataset.type == dns_rdatatype_soa) {
8946 				seen_soa = true;
8947 			} else if (rdataset.type == dns_rdatatype_ns) {
8948 				seen_ns = true;
8949 			} else if (rdataset.type == dns_rdatatype_dname) {
8950 				seen_dname = true;
8951 			} else if (rdataset.type == dns_rdatatype_nsec) {
8952 				seen_nsec = true;
8953 			} else if (rdataset.type == dns_rdatatype_nsec3) {
8954 				seen_nsec3 = true;
8955 			} else if (rdataset.type != dns_rdatatype_rrsig) {
8956 				seen_rr = true;
8957 			}
8958 			dns_rdataset_disassociate(&rdataset);
8959 		}
8960 		dns_rdatasetiter_destroy(&iterator);
8961 
8962 		if (!seen_rr || seen_nsec3 || seen_nsec) {
8963 			goto next_removenode;
8964 		}
8965 		if ((seen_ns && !seen_soa) || seen_dname) {
8966 			delegation = true;
8967 		}
8968 
8969 		/*
8970 		 * Add a NSEC record except at the origin.
8971 		 */
8972 		if (!dns_name_equal(name, dns_db_origin(db))) {
8973 			dns_dbiterator_pause(nsec3chain->dbiterator);
8974 			CHECK(add_nsec(db, version, name, node,
8975 				       zone_nsecttl(zone), delegation,
8976 				       &nsec_diff));
8977 			signatures--;
8978 		}
8979 
8980 	next_removenode:
8981 		first = false;
8982 		dns_db_detachnode(db, &node);
8983 		do {
8984 			result = dns_dbiterator_next(nsec3chain->dbiterator);
8985 			if (result == ISC_R_NOMORE && buildnsecchain) {
8986 				/*
8987 				 * The NSEC chain should now be built.
8988 				 * We can now remove the NSEC3 chain.
8989 				 */
8990 				updatensec = true;
8991 				goto same_removechain;
8992 			}
8993 			if (result == ISC_R_NOMORE) {
8994 				dns_dbiterator_pause(nsec3chain->dbiterator);
8995 				LOCK_ZONE(zone);
8996 				ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain,
8997 						link);
8998 				UNLOCK_ZONE(zone);
8999 				ISC_LIST_APPEND(cleanup, nsec3chain, link);
9000 				result = fixup_nsec3param(
9001 					db, version, nsec3chain, false,
9002 					privatetype, &param_diff);
9003 				if (result != ISC_R_SUCCESS) {
9004 					dnssec_log(zone, ISC_LOG_ERROR,
9005 						   "zone_nsec3chain:"
9006 						   "fixup_nsec3param -> %s",
9007 						   dns_result_totext(result));
9008 					goto failure;
9009 				}
9010 				goto next_removechain;
9011 			} else if (result != ISC_R_SUCCESS) {
9012 				dnssec_log(zone, ISC_LOG_ERROR,
9013 					   "zone_nsec3chain:"
9014 					   "dns_dbiterator_next -> %s",
9015 					   dns_result_totext(result));
9016 				goto failure;
9017 			} else if (delegation) {
9018 				dns_dbiterator_current(nsec3chain->dbiterator,
9019 						       &node, nextname);
9020 				dns_db_detachnode(db, &node);
9021 				if (!dns_name_issubdomain(nextname, name)) {
9022 					break;
9023 				}
9024 			} else {
9025 				break;
9026 			}
9027 		} while (1);
9028 		continue;
9029 
9030 	same_removechain:
9031 		CHECK(dns_dbiterator_first(nsec3chain->dbiterator));
9032 		buildnsecchain = false;
9033 		first = true;
9034 		continue;
9035 
9036 	next_removechain:
9037 		dns_dbiterator_pause(nsec3chain->dbiterator);
9038 		nsec3chain = nextnsec3chain;
9039 		first = true;
9040 	}
9041 
9042 skip_removals:
9043 	/*
9044 	 * We may need to update the NSEC/NSEC3 records for the zone apex.
9045 	 */
9046 	if (!ISC_LIST_EMPTY(param_diff.tuples)) {
9047 		bool rebuild_nsec = false, rebuild_nsec3 = false;
9048 		result = dns_db_getoriginnode(db, &node);
9049 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
9050 		result = dns_db_allrdatasets(db, node, version, 0, &iterator);
9051 		if (result != ISC_R_SUCCESS) {
9052 			dnssec_log(zone, ISC_LOG_ERROR,
9053 				   "zone_nsec3chain:dns_db_allrdatasets -> %s",
9054 				   dns_result_totext(result));
9055 			goto failure;
9056 		}
9057 		for (result = dns_rdatasetiter_first(iterator);
9058 		     result == ISC_R_SUCCESS;
9059 		     result = dns_rdatasetiter_next(iterator))
9060 		{
9061 			dns_rdatasetiter_current(iterator, &rdataset);
9062 			if (rdataset.type == dns_rdatatype_nsec) {
9063 				rebuild_nsec = true;
9064 			} else if (rdataset.type == dns_rdatatype_nsec3param) {
9065 				rebuild_nsec3 = true;
9066 			}
9067 			dns_rdataset_disassociate(&rdataset);
9068 		}
9069 		dns_rdatasetiter_destroy(&iterator);
9070 		dns_db_detachnode(db, &node);
9071 
9072 		if (rebuild_nsec) {
9073 			if (nsec3chain != NULL) {
9074 				dns_dbiterator_pause(nsec3chain->dbiterator);
9075 			}
9076 
9077 			result = updatesecure(db, version, &zone->origin,
9078 					      zone_nsecttl(zone), true,
9079 					      &nsec_diff);
9080 			if (result != ISC_R_SUCCESS) {
9081 				dnssec_log(zone, ISC_LOG_ERROR,
9082 					   "zone_nsec3chain:updatesecure -> %s",
9083 					   dns_result_totext(result));
9084 				goto failure;
9085 			}
9086 		}
9087 
9088 		if (rebuild_nsec3) {
9089 			if (nsec3chain != NULL) {
9090 				dns_dbiterator_pause(nsec3chain->dbiterator);
9091 			}
9092 
9093 			result = dns_nsec3_addnsec3s(
9094 				db, version, dns_db_origin(db),
9095 				zone_nsecttl(zone), false, &nsec3_diff);
9096 			if (result != ISC_R_SUCCESS) {
9097 				dnssec_log(zone, ISC_LOG_ERROR,
9098 					   "zone_nsec3chain:"
9099 					   "dns_nsec3_addnsec3s -> %s",
9100 					   dns_result_totext(result));
9101 				goto failure;
9102 			}
9103 		}
9104 	}
9105 
9106 	/*
9107 	 * Add / update signatures for the NSEC3 records.
9108 	 */
9109 	if (nsec3chain != NULL) {
9110 		dns_dbiterator_pause(nsec3chain->dbiterator);
9111 	}
9112 	result = dns__zone_updatesigs(&nsec3_diff, db, version, zone_keys,
9113 				      nkeys, zone, inception, expire, 0, now,
9114 				      check_ksk, keyset_kskonly, &zonediff);
9115 	if (result != ISC_R_SUCCESS) {
9116 		dnssec_log(zone, ISC_LOG_ERROR,
9117 			   "zone_nsec3chain:dns__zone_updatesigs -> %s",
9118 			   dns_result_totext(result));
9119 		goto failure;
9120 	}
9121 
9122 	/*
9123 	 * We have changed the NSEC3PARAM or private RRsets
9124 	 * above so we need to update the signatures.
9125 	 */
9126 	result = dns__zone_updatesigs(&param_diff, db, version, zone_keys,
9127 				      nkeys, zone, inception, expire, 0, now,
9128 				      check_ksk, keyset_kskonly, &zonediff);
9129 	if (result != ISC_R_SUCCESS) {
9130 		dnssec_log(zone, ISC_LOG_ERROR,
9131 			   "zone_nsec3chain:dns__zone_updatesigs -> %s",
9132 			   dns_result_totext(result));
9133 		goto failure;
9134 	}
9135 
9136 	if (updatensec) {
9137 		result = updatesecure(db, version, &zone->origin,
9138 				      zone_nsecttl(zone), false, &nsec_diff);
9139 		if (result != ISC_R_SUCCESS) {
9140 			dnssec_log(zone, ISC_LOG_ERROR,
9141 				   "zone_nsec3chain:updatesecure -> %s",
9142 				   dns_result_totext(result));
9143 			goto failure;
9144 		}
9145 	}
9146 
9147 	result = dns__zone_updatesigs(&nsec_diff, db, version, zone_keys, nkeys,
9148 				      zone, inception, expire, 0, now,
9149 				      check_ksk, keyset_kskonly, &zonediff);
9150 	if (result != ISC_R_SUCCESS) {
9151 		dnssec_log(zone, ISC_LOG_ERROR,
9152 			   "zone_nsec3chain:dns__zone_updatesigs -> %s",
9153 			   dns_result_totext(result));
9154 		goto failure;
9155 	}
9156 
9157 	/*
9158 	 * If we made no effective changes to the zone then we can just
9159 	 * cleanup otherwise we need to increment the serial.
9160 	 */
9161 	if (ISC_LIST_EMPTY(zonediff.diff->tuples)) {
9162 		/*
9163 		 * No need to call dns_db_closeversion() here as it is
9164 		 * called with commit = true below.
9165 		 */
9166 		goto done;
9167 	}
9168 
9169 	result = del_sigs(zone, db, version, &zone->origin, dns_rdatatype_soa,
9170 			  &zonediff, zone_keys, nkeys, now, false);
9171 	if (result != ISC_R_SUCCESS) {
9172 		dnssec_log(zone, ISC_LOG_ERROR,
9173 			   "zone_nsec3chain:del_sigs -> %s",
9174 			   dns_result_totext(result));
9175 		goto failure;
9176 	}
9177 
9178 	result = update_soa_serial(zone, db, version, zonediff.diff, zone->mctx,
9179 				   zone->updatemethod);
9180 	if (result != ISC_R_SUCCESS) {
9181 		dnssec_log(zone, ISC_LOG_ERROR,
9182 			   "zone_nsec3chain:update_soa_serial -> %s",
9183 			   dns_result_totext(result));
9184 		goto failure;
9185 	}
9186 
9187 	result = add_sigs(db, version, &zone->origin, zone, dns_rdatatype_soa,
9188 			  zonediff.diff, zone_keys, nkeys, zone->mctx,
9189 			  inception, soaexpire, check_ksk, keyset_kskonly);
9190 	if (result != ISC_R_SUCCESS) {
9191 		dnssec_log(zone, ISC_LOG_ERROR,
9192 			   "zone_nsec3chain:add_sigs -> %s",
9193 			   dns_result_totext(result));
9194 		goto failure;
9195 	}
9196 
9197 	/* Write changes to journal file. */
9198 	CHECK(zone_journal(zone, zonediff.diff, NULL, "zone_nsec3chain"));
9199 
9200 	LOCK_ZONE(zone);
9201 	zone_needdump(zone, DNS_DUMP_DELAY);
9202 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
9203 	UNLOCK_ZONE(zone);
9204 
9205 done:
9206 	/*
9207 	 * Pause all iterators so that dns_db_closeversion() can succeed.
9208 	 */
9209 	LOCK_ZONE(zone);
9210 	for (nsec3chain = ISC_LIST_HEAD(zone->nsec3chain); nsec3chain != NULL;
9211 	     nsec3chain = ISC_LIST_NEXT(nsec3chain, link))
9212 	{
9213 		dns_dbiterator_pause(nsec3chain->dbiterator);
9214 	}
9215 	UNLOCK_ZONE(zone);
9216 
9217 	/*
9218 	 * Everything has succeeded. Commit the changes.
9219 	 * Unconditionally commit as zonediff.offline not checked above.
9220 	 */
9221 	dns_db_closeversion(db, &version, true);
9222 
9223 	/*
9224 	 * Everything succeeded so we can clean these up now.
9225 	 */
9226 	nsec3chain = ISC_LIST_HEAD(cleanup);
9227 	while (nsec3chain != NULL) {
9228 		ISC_LIST_UNLINK(cleanup, nsec3chain, link);
9229 		dns_db_detach(&nsec3chain->db);
9230 		dns_dbiterator_destroy(&nsec3chain->dbiterator);
9231 		isc_mem_put(zone->mctx, nsec3chain, sizeof *nsec3chain);
9232 		nsec3chain = ISC_LIST_HEAD(cleanup);
9233 	}
9234 
9235 	LOCK_ZONE(zone);
9236 	set_resigntime(zone);
9237 	UNLOCK_ZONE(zone);
9238 
9239 failure:
9240 	if (result != ISC_R_SUCCESS) {
9241 		dnssec_log(zone, ISC_LOG_ERROR, "zone_nsec3chain: %s",
9242 			   dns_result_totext(result));
9243 	}
9244 
9245 	/*
9246 	 * On error roll back the current nsec3chain.
9247 	 */
9248 	if (result != ISC_R_SUCCESS && nsec3chain != NULL) {
9249 		if (nsec3chain->done) {
9250 			dns_db_detach(&nsec3chain->db);
9251 			dns_dbiterator_destroy(&nsec3chain->dbiterator);
9252 			isc_mem_put(zone->mctx, nsec3chain, sizeof *nsec3chain);
9253 		} else {
9254 			result = dns_dbiterator_first(nsec3chain->dbiterator);
9255 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
9256 			dns_dbiterator_pause(nsec3chain->dbiterator);
9257 			nsec3chain->delete_nsec = nsec3chain->save_delete_nsec;
9258 		}
9259 	}
9260 
9261 	/*
9262 	 * Rollback the cleanup list.
9263 	 */
9264 	nsec3chain = ISC_LIST_TAIL(cleanup);
9265 	while (nsec3chain != NULL) {
9266 		ISC_LIST_UNLINK(cleanup, nsec3chain, link);
9267 		if (nsec3chain->done) {
9268 			dns_db_detach(&nsec3chain->db);
9269 			dns_dbiterator_destroy(&nsec3chain->dbiterator);
9270 			isc_mem_put(zone->mctx, nsec3chain, sizeof *nsec3chain);
9271 		} else {
9272 			LOCK_ZONE(zone);
9273 			ISC_LIST_PREPEND(zone->nsec3chain, nsec3chain, link);
9274 			UNLOCK_ZONE(zone);
9275 			result = dns_dbiterator_first(nsec3chain->dbiterator);
9276 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
9277 			dns_dbiterator_pause(nsec3chain->dbiterator);
9278 			nsec3chain->delete_nsec = nsec3chain->save_delete_nsec;
9279 		}
9280 		nsec3chain = ISC_LIST_TAIL(cleanup);
9281 	}
9282 
9283 	LOCK_ZONE(zone);
9284 	for (nsec3chain = ISC_LIST_HEAD(zone->nsec3chain); nsec3chain != NULL;
9285 	     nsec3chain = ISC_LIST_NEXT(nsec3chain, link))
9286 	{
9287 		dns_dbiterator_pause(nsec3chain->dbiterator);
9288 	}
9289 	UNLOCK_ZONE(zone);
9290 
9291 	dns_diff_clear(&param_diff);
9292 	dns_diff_clear(&nsec3_diff);
9293 	dns_diff_clear(&nsec_diff);
9294 	dns_diff_clear(&_sig_diff);
9295 
9296 	if (iterator != NULL) {
9297 		dns_rdatasetiter_destroy(&iterator);
9298 	}
9299 
9300 	for (i = 0; i < nkeys; i++) {
9301 		dst_key_free(&zone_keys[i]);
9302 	}
9303 
9304 	if (node != NULL) {
9305 		dns_db_detachnode(db, &node);
9306 	}
9307 	if (version != NULL) {
9308 		dns_db_closeversion(db, &version, false);
9309 		dns_db_detach(&db);
9310 	} else if (db != NULL) {
9311 		dns_db_detach(&db);
9312 	}
9313 
9314 	LOCK_ZONE(zone);
9315 	if (ISC_LIST_HEAD(zone->nsec3chain) != NULL) {
9316 		isc_interval_t interval;
9317 		if (zone->update_disabled || result != ISC_R_SUCCESS) {
9318 			isc_interval_set(&interval, 60, 0); /* 1 minute */
9319 		} else {
9320 			isc_interval_set(&interval, 0, 10000000); /* 10 ms */
9321 		}
9322 		isc_time_nowplusinterval(&zone->nsec3chaintime, &interval);
9323 	} else {
9324 		isc_time_settoepoch(&zone->nsec3chaintime);
9325 	}
9326 	UNLOCK_ZONE(zone);
9327 
9328 	INSIST(version == NULL);
9329 }
9330 
9331 /*%
9332  * Delete all RRSIG records with the given algorithm and keyid.
9333  * Remove the NSEC record and RRSIGs if nkeys is zero.
9334  * If all remaining RRsets are signed with the given algorithm
9335  * set *has_algp to true.
9336  */
9337 static isc_result_t
del_sig(dns_db_t * db,dns_dbversion_t * version,dns_name_t * name,dns_dbnode_t * node,unsigned int nkeys,dns_secalg_t algorithm,uint16_t keyid,bool * has_algp,dns_diff_t * diff)9338 del_sig(dns_db_t *db, dns_dbversion_t *version, dns_name_t *name,
9339 	dns_dbnode_t *node, unsigned int nkeys, dns_secalg_t algorithm,
9340 	uint16_t keyid, bool *has_algp, dns_diff_t *diff) {
9341 	dns_rdata_rrsig_t rrsig;
9342 	dns_rdataset_t rdataset;
9343 	dns_rdatasetiter_t *iterator = NULL;
9344 	isc_result_t result;
9345 	bool alg_missed = false;
9346 	bool alg_found = false;
9347 
9348 	char namebuf[DNS_NAME_FORMATSIZE];
9349 	dns_name_format(name, namebuf, sizeof(namebuf));
9350 
9351 	result = dns_db_allrdatasets(db, node, version, 0, &iterator);
9352 	if (result != ISC_R_SUCCESS) {
9353 		if (result == ISC_R_NOTFOUND) {
9354 			result = ISC_R_SUCCESS;
9355 		}
9356 		return (result);
9357 	}
9358 
9359 	dns_rdataset_init(&rdataset);
9360 	for (result = dns_rdatasetiter_first(iterator); result == ISC_R_SUCCESS;
9361 	     result = dns_rdatasetiter_next(iterator))
9362 	{
9363 		bool has_alg = false;
9364 		dns_rdatasetiter_current(iterator, &rdataset);
9365 		if (nkeys == 0 && rdataset.type == dns_rdatatype_nsec) {
9366 			for (result = dns_rdataset_first(&rdataset);
9367 			     result == ISC_R_SUCCESS;
9368 			     result = dns_rdataset_next(&rdataset))
9369 			{
9370 				dns_rdata_t rdata = DNS_RDATA_INIT;
9371 				dns_rdataset_current(&rdataset, &rdata);
9372 				CHECK(update_one_rr(db, version, diff,
9373 						    DNS_DIFFOP_DEL, name,
9374 						    rdataset.ttl, &rdata));
9375 			}
9376 			if (result != ISC_R_NOMORE) {
9377 				goto failure;
9378 			}
9379 			dns_rdataset_disassociate(&rdataset);
9380 			continue;
9381 		}
9382 		if (rdataset.type != dns_rdatatype_rrsig) {
9383 			dns_rdataset_disassociate(&rdataset);
9384 			continue;
9385 		}
9386 		for (result = dns_rdataset_first(&rdataset);
9387 		     result == ISC_R_SUCCESS;
9388 		     result = dns_rdataset_next(&rdataset))
9389 		{
9390 			dns_rdata_t rdata = DNS_RDATA_INIT;
9391 			dns_rdataset_current(&rdataset, &rdata);
9392 			CHECK(dns_rdata_tostruct(&rdata, &rrsig, NULL));
9393 			if (nkeys != 0 && (rrsig.algorithm != algorithm ||
9394 					   rrsig.keyid != keyid)) {
9395 				if (rrsig.algorithm == algorithm) {
9396 					has_alg = true;
9397 				}
9398 				continue;
9399 			}
9400 			CHECK(update_one_rr(db, version, diff,
9401 					    DNS_DIFFOP_DELRESIGN, name,
9402 					    rdataset.ttl, &rdata));
9403 		}
9404 		dns_rdataset_disassociate(&rdataset);
9405 		if (result != ISC_R_NOMORE) {
9406 			break;
9407 		}
9408 
9409 		/*
9410 		 * After deleting, if there's still a signature for
9411 		 * 'algorithm', set alg_found; if not, set alg_missed.
9412 		 */
9413 		if (has_alg) {
9414 			alg_found = true;
9415 		} else {
9416 			alg_missed = true;
9417 		}
9418 	}
9419 	if (result == ISC_R_NOMORE) {
9420 		result = ISC_R_SUCCESS;
9421 	}
9422 
9423 	/*
9424 	 * Set `has_algp` if the algorithm was found in every RRset:
9425 	 * i.e., found in at least one, and not missing from any.
9426 	 */
9427 	*has_algp = (alg_found && !alg_missed);
9428 failure:
9429 	if (dns_rdataset_isassociated(&rdataset)) {
9430 		dns_rdataset_disassociate(&rdataset);
9431 	}
9432 	dns_rdatasetiter_destroy(&iterator);
9433 	return (result);
9434 }
9435 
9436 /*
9437  * Incrementally sign the zone using the keys requested.
9438  * Builds the NSEC chain if required.
9439  */
9440 static void
zone_sign(dns_zone_t * zone)9441 zone_sign(dns_zone_t *zone) {
9442 	const char *me = "zone_sign";
9443 	dns_db_t *db = NULL;
9444 	dns_dbnode_t *node = NULL;
9445 	dns_dbversion_t *version = NULL;
9446 	dns_diff_t _sig_diff;
9447 	dns_diff_t post_diff;
9448 	dns__zonediff_t zonediff;
9449 	dns_fixedname_t fixed;
9450 	dns_fixedname_t nextfixed;
9451 	dns_kasp_t *kasp;
9452 	dns_name_t *name, *nextname;
9453 	dns_rdataset_t rdataset;
9454 	dns_signing_t *signing, *nextsigning;
9455 	dns_signinglist_t cleanup;
9456 	dst_key_t *zone_keys[DNS_MAXZONEKEYS];
9457 	int32_t signatures;
9458 	bool check_ksk, keyset_kskonly, is_ksk, is_zsk;
9459 	bool with_ksk, with_zsk;
9460 	bool commit = false;
9461 	bool is_bottom_of_zone;
9462 	bool build_nsec = false;
9463 	bool build_nsec3 = false;
9464 	bool use_kasp = false;
9465 	bool first;
9466 	isc_result_t result;
9467 	isc_stdtime_t now, inception, soaexpire, expire;
9468 	uint32_t jitter, sigvalidityinterval, expiryinterval;
9469 	unsigned int i, j;
9470 	unsigned int nkeys = 0;
9471 	uint32_t nodes;
9472 
9473 	ENTER;
9474 
9475 	dns_rdataset_init(&rdataset);
9476 	name = dns_fixedname_initname(&fixed);
9477 	nextname = dns_fixedname_initname(&nextfixed);
9478 	dns_diff_init(zone->mctx, &_sig_diff);
9479 	dns_diff_init(zone->mctx, &post_diff);
9480 	zonediff_init(&zonediff, &_sig_diff);
9481 	ISC_LIST_INIT(cleanup);
9482 
9483 	/*
9484 	 * Updates are disabled.  Pause for 1 minute.
9485 	 */
9486 	if (zone->update_disabled) {
9487 		result = ISC_R_FAILURE;
9488 		goto cleanup;
9489 	}
9490 
9491 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
9492 	if (zone->db != NULL) {
9493 		dns_db_attach(zone->db, &db);
9494 	}
9495 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
9496 	if (db == NULL) {
9497 		result = ISC_R_FAILURE;
9498 		goto cleanup;
9499 	}
9500 
9501 	result = dns_db_newversion(db, &version);
9502 	if (result != ISC_R_SUCCESS) {
9503 		dnssec_log(zone, ISC_LOG_ERROR,
9504 			   "zone_sign:dns_db_newversion -> %s",
9505 			   dns_result_totext(result));
9506 		goto cleanup;
9507 	}
9508 
9509 	isc_stdtime_get(&now);
9510 
9511 	result = dns__zone_findkeys(zone, db, version, now, zone->mctx,
9512 				    DNS_MAXZONEKEYS, zone_keys, &nkeys);
9513 	if (result != ISC_R_SUCCESS) {
9514 		dnssec_log(zone, ISC_LOG_ERROR,
9515 			   "zone_sign:dns__zone_findkeys -> %s",
9516 			   dns_result_totext(result));
9517 		goto cleanup;
9518 	}
9519 
9520 	kasp = dns_zone_getkasp(zone);
9521 	sigvalidityinterval = dns_zone_getsigvalidityinterval(zone);
9522 	inception = now - 3600; /* Allow for clock skew. */
9523 	soaexpire = now + sigvalidityinterval;
9524 	expiryinterval = dns_zone_getsigresigninginterval(zone);
9525 	if (expiryinterval > sigvalidityinterval) {
9526 		expiryinterval = sigvalidityinterval;
9527 	} else {
9528 		expiryinterval = sigvalidityinterval - expiryinterval;
9529 	}
9530 
9531 	/*
9532 	 * Spread out signatures over time if they happen to be
9533 	 * clumped.  We don't do this for each add_sigs() call as
9534 	 * we still want some clustering to occur.
9535 	 */
9536 	if (sigvalidityinterval >= 3600U) {
9537 		if (sigvalidityinterval > 7200U) {
9538 			jitter = isc_random_uniform(expiryinterval);
9539 		} else {
9540 			jitter = isc_random_uniform(1200);
9541 		}
9542 		expire = soaexpire - jitter - 1;
9543 	} else {
9544 		expire = soaexpire - 1;
9545 	}
9546 
9547 	/*
9548 	 * We keep pulling nodes off each iterator in turn until
9549 	 * we have no more nodes to pull off or we reach the limits
9550 	 * for this quantum.
9551 	 */
9552 	nodes = zone->nodes;
9553 	signatures = zone->signatures;
9554 	signing = ISC_LIST_HEAD(zone->signing);
9555 	first = true;
9556 
9557 	if (dns_zone_getkasp(zone) != NULL) {
9558 		check_ksk = false;
9559 		keyset_kskonly = true;
9560 		use_kasp = true;
9561 	} else {
9562 		check_ksk = DNS_ZONE_OPTION(zone, DNS_ZONEOPT_UPDATECHECKKSK);
9563 		keyset_kskonly = DNS_ZONE_OPTION(zone,
9564 						 DNS_ZONEOPT_DNSKEYKSKONLY);
9565 	}
9566 	dnssec_log(zone, ISC_LOG_DEBUG(3), "zone_sign:use kasp -> %s",
9567 		   use_kasp ? "yes" : "no");
9568 
9569 	/* Determine which type of chain to build */
9570 	if (use_kasp) {
9571 		build_nsec3 = dns_kasp_nsec3(kasp);
9572 		build_nsec = !build_nsec3;
9573 	} else {
9574 		CHECK(dns_private_chains(db, version, zone->privatetype,
9575 					 &build_nsec, &build_nsec3));
9576 		/* If neither chain is found, default to NSEC */
9577 		if (!build_nsec && !build_nsec3) {
9578 			build_nsec = true;
9579 		}
9580 	}
9581 
9582 	while (signing != NULL && nodes-- > 0 && signatures > 0) {
9583 		bool has_alg = false;
9584 
9585 		dns_dbiterator_pause(signing->dbiterator);
9586 		nextsigning = ISC_LIST_NEXT(signing, link);
9587 
9588 		ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
9589 		if (signing->done || signing->db != zone->db) {
9590 			/*
9591 			 * The zone has been reloaded.	We will have to
9592 			 * created new signings as part of the reload
9593 			 * process so we can destroy this one.
9594 			 */
9595 			ISC_LIST_UNLINK(zone->signing, signing, link);
9596 			ISC_LIST_APPEND(cleanup, signing, link);
9597 			ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
9598 			goto next_signing;
9599 		}
9600 		ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
9601 
9602 		if (signing->db != db) {
9603 			goto next_signing;
9604 		}
9605 
9606 		is_bottom_of_zone = false;
9607 
9608 		if (first && signing->deleteit) {
9609 			/*
9610 			 * Remove the key we are deleting from consideration.
9611 			 */
9612 			for (i = 0, j = 0; i < nkeys; i++) {
9613 				/*
9614 				 * Find the key we want to remove.
9615 				 */
9616 				if (ALG(zone_keys[i]) == signing->algorithm &&
9617 				    dst_key_id(zone_keys[i]) == signing->keyid)
9618 				{
9619 					bool ksk = false;
9620 					isc_result_t ret = dst_key_getbool(
9621 						zone_keys[i], DST_BOOL_KSK,
9622 						&ksk);
9623 					if (ret != ISC_R_SUCCESS) {
9624 						ksk = KSK(zone_keys[i]);
9625 					}
9626 					if (ksk) {
9627 						dst_key_free(&zone_keys[i]);
9628 					}
9629 					continue;
9630 				}
9631 				zone_keys[j] = zone_keys[i];
9632 				j++;
9633 			}
9634 			for (i = j; i < nkeys; i++) {
9635 				zone_keys[i] = NULL;
9636 			}
9637 			nkeys = j;
9638 		}
9639 
9640 		dns_dbiterator_current(signing->dbiterator, &node, name);
9641 
9642 		if (signing->deleteit) {
9643 			dns_dbiterator_pause(signing->dbiterator);
9644 			CHECK(del_sig(db, version, name, node, nkeys,
9645 				      signing->algorithm, signing->keyid,
9646 				      &has_alg, zonediff.diff));
9647 		}
9648 
9649 		/*
9650 		 * On the first pass we need to check if the current node
9651 		 * has not been obscured.
9652 		 */
9653 		if (first) {
9654 			dns_fixedname_t ffound;
9655 			dns_name_t *found;
9656 			found = dns_fixedname_initname(&ffound);
9657 			result = dns_db_find(
9658 				db, name, version, dns_rdatatype_soa,
9659 				DNS_DBFIND_NOWILD, 0, NULL, found, NULL, NULL);
9660 			if ((result == DNS_R_DELEGATION ||
9661 			     result == DNS_R_DNAME) &&
9662 			    !dns_name_equal(name, found))
9663 			{
9664 				/*
9665 				 * Remember the obscuring name so that
9666 				 * we skip all obscured names.
9667 				 */
9668 				dns_name_copynf(found, name);
9669 				is_bottom_of_zone = true;
9670 				goto next_node;
9671 			}
9672 		}
9673 
9674 		/*
9675 		 * Process one node.
9676 		 */
9677 		with_ksk = false;
9678 		with_zsk = false;
9679 		dns_dbiterator_pause(signing->dbiterator);
9680 
9681 		CHECK(check_if_bottom_of_zone(db, node, version,
9682 					      &is_bottom_of_zone));
9683 
9684 		for (i = 0; !has_alg && i < nkeys; i++) {
9685 			bool both = false;
9686 
9687 			/*
9688 			 * Find the keys we want to sign with.
9689 			 */
9690 			if (!dst_key_isprivate(zone_keys[i])) {
9691 				continue;
9692 			}
9693 			if (dst_key_inactive(zone_keys[i])) {
9694 				continue;
9695 			}
9696 
9697 			/*
9698 			 * When adding look for the specific key.
9699 			 */
9700 			if (!signing->deleteit &&
9701 			    (dst_key_alg(zone_keys[i]) != signing->algorithm ||
9702 			     dst_key_id(zone_keys[i]) != signing->keyid))
9703 			{
9704 				continue;
9705 			}
9706 
9707 			/*
9708 			 * When deleting make sure we are properly signed
9709 			 * with the algorithm that was being removed.
9710 			 */
9711 			if (signing->deleteit &&
9712 			    ALG(zone_keys[i]) != signing->algorithm) {
9713 				continue;
9714 			}
9715 
9716 			/*
9717 			 * Do we do KSK processing?
9718 			 */
9719 			if (check_ksk && !REVOKE(zone_keys[i])) {
9720 				bool have_ksk, have_nonksk;
9721 				if (KSK(zone_keys[i])) {
9722 					have_ksk = true;
9723 					have_nonksk = false;
9724 				} else {
9725 					have_ksk = false;
9726 					have_nonksk = true;
9727 				}
9728 				for (j = 0; j < nkeys; j++) {
9729 					if (j == i || (ALG(zone_keys[i]) !=
9730 						       ALG(zone_keys[j]))) {
9731 						continue;
9732 					}
9733 					/*
9734 					 * Don't consider inactive keys, however
9735 					 * the key may be temporary offline, so
9736 					 * do consider KSKs which private key
9737 					 * files are unavailable.
9738 					 */
9739 					if (dst_key_inactive(zone_keys[j])) {
9740 						continue;
9741 					}
9742 					if (REVOKE(zone_keys[j])) {
9743 						continue;
9744 					}
9745 					if (KSK(zone_keys[j])) {
9746 						have_ksk = true;
9747 					} else if (dst_key_isprivate(
9748 							   zone_keys[j])) {
9749 						have_nonksk = true;
9750 					}
9751 					both = have_ksk && have_nonksk;
9752 					if (both) {
9753 						break;
9754 					}
9755 				}
9756 			}
9757 			if (use_kasp) {
9758 				/*
9759 				 * A dnssec-policy is found. Check what
9760 				 * RRsets this key can sign.
9761 				 */
9762 				isc_result_t kresult;
9763 				is_ksk = false;
9764 				kresult = dst_key_getbool(
9765 					zone_keys[i], DST_BOOL_KSK, &is_ksk);
9766 				if (kresult != ISC_R_SUCCESS) {
9767 					if (KSK(zone_keys[i])) {
9768 						is_ksk = true;
9769 					}
9770 				}
9771 
9772 				is_zsk = false;
9773 				kresult = dst_key_getbool(
9774 					zone_keys[i], DST_BOOL_ZSK, &is_zsk);
9775 				if (kresult != ISC_R_SUCCESS) {
9776 					if (!KSK(zone_keys[i])) {
9777 						is_zsk = true;
9778 					}
9779 				}
9780 				/* Treat as if we have both KSK and ZSK. */
9781 				both = true;
9782 			} else if (both || REVOKE(zone_keys[i])) {
9783 				is_ksk = KSK(zone_keys[i]);
9784 				is_zsk = !KSK(zone_keys[i]);
9785 			} else {
9786 				is_ksk = false;
9787 				is_zsk = true;
9788 			}
9789 
9790 			/*
9791 			 * If deleting signatures, we need to ensure that
9792 			 * the RRset is still signed at least once by a
9793 			 * KSK and a ZSK.
9794 			 */
9795 			if (signing->deleteit && is_zsk && with_zsk) {
9796 				continue;
9797 			}
9798 
9799 			if (signing->deleteit && is_ksk && with_ksk) {
9800 				continue;
9801 			}
9802 
9803 			CHECK(sign_a_node(
9804 				db, zone, name, node, version, build_nsec3,
9805 				build_nsec, zone_keys[i], inception, expire,
9806 				zone_nsecttl(zone), is_ksk, is_zsk,
9807 				(both && keyset_kskonly), is_bottom_of_zone,
9808 				zonediff.diff, &signatures, zone->mctx));
9809 			/*
9810 			 * If we are adding we are done.  Look for other keys
9811 			 * of the same algorithm if deleting.
9812 			 */
9813 			if (!signing->deleteit) {
9814 				break;
9815 			}
9816 			if (is_zsk) {
9817 				with_zsk = true;
9818 			}
9819 			if (is_ksk) {
9820 				with_ksk = true;
9821 			}
9822 		}
9823 
9824 		/*
9825 		 * Go onto next node.
9826 		 */
9827 	next_node:
9828 		first = false;
9829 		dns_db_detachnode(db, &node);
9830 		do {
9831 			result = dns_dbiterator_next(signing->dbiterator);
9832 			if (result == ISC_R_NOMORE) {
9833 				ISC_LIST_UNLINK(zone->signing, signing, link);
9834 				ISC_LIST_APPEND(cleanup, signing, link);
9835 				dns_dbiterator_pause(signing->dbiterator);
9836 				if (nkeys != 0 && build_nsec) {
9837 					/*
9838 					 * We have finished regenerating the
9839 					 * zone with a zone signing key.
9840 					 * The NSEC chain is now complete and
9841 					 * there is a full set of signatures
9842 					 * for the zone.  We can now clear the
9843 					 * OPT bit from the NSEC record.
9844 					 */
9845 					result = updatesecure(
9846 						db, version, &zone->origin,
9847 						zone_nsecttl(zone), false,
9848 						&post_diff);
9849 					if (result != ISC_R_SUCCESS) {
9850 						dnssec_log(zone, ISC_LOG_ERROR,
9851 							   "updatesecure -> %s",
9852 							   dns_result_totext(
9853 								   result));
9854 						goto cleanup;
9855 					}
9856 				}
9857 				result = updatesignwithkey(
9858 					zone, signing, version, build_nsec3,
9859 					zone_nsecttl(zone), &post_diff);
9860 				if (result != ISC_R_SUCCESS) {
9861 					dnssec_log(zone, ISC_LOG_ERROR,
9862 						   "updatesignwithkey -> %s",
9863 						   dns_result_totext(result));
9864 					goto cleanup;
9865 				}
9866 				build_nsec = false;
9867 				goto next_signing;
9868 			} else if (result != ISC_R_SUCCESS) {
9869 				dnssec_log(zone, ISC_LOG_ERROR,
9870 					   "zone_sign:"
9871 					   "dns_dbiterator_next -> %s",
9872 					   dns_result_totext(result));
9873 				goto cleanup;
9874 			} else if (is_bottom_of_zone) {
9875 				dns_dbiterator_current(signing->dbiterator,
9876 						       &node, nextname);
9877 				dns_db_detachnode(db, &node);
9878 				if (!dns_name_issubdomain(nextname, name)) {
9879 					break;
9880 				}
9881 			} else {
9882 				break;
9883 			}
9884 		} while (1);
9885 		continue;
9886 
9887 	next_signing:
9888 		dns_dbiterator_pause(signing->dbiterator);
9889 		signing = nextsigning;
9890 		first = true;
9891 	}
9892 
9893 	if (ISC_LIST_HEAD(post_diff.tuples) != NULL) {
9894 		result = dns__zone_updatesigs(&post_diff, db, version,
9895 					      zone_keys, nkeys, zone, inception,
9896 					      expire, 0, now, check_ksk,
9897 					      keyset_kskonly, &zonediff);
9898 		if (result != ISC_R_SUCCESS) {
9899 			dnssec_log(zone, ISC_LOG_ERROR,
9900 				   "zone_sign:dns__zone_updatesigs -> %s",
9901 				   dns_result_totext(result));
9902 			goto cleanup;
9903 		}
9904 	}
9905 
9906 	/*
9907 	 * Have we changed anything?
9908 	 */
9909 	if (ISC_LIST_EMPTY(zonediff.diff->tuples)) {
9910 		if (zonediff.offline) {
9911 			commit = true;
9912 		}
9913 		result = ISC_R_SUCCESS;
9914 		goto pauseall;
9915 	}
9916 
9917 	commit = true;
9918 
9919 	result = del_sigs(zone, db, version, &zone->origin, dns_rdatatype_soa,
9920 			  &zonediff, zone_keys, nkeys, now, false);
9921 	if (result != ISC_R_SUCCESS) {
9922 		dnssec_log(zone, ISC_LOG_ERROR, "zone_sign:del_sigs -> %s",
9923 			   dns_result_totext(result));
9924 		goto cleanup;
9925 	}
9926 
9927 	result = update_soa_serial(zone, db, version, zonediff.diff, zone->mctx,
9928 				   zone->updatemethod);
9929 	if (result != ISC_R_SUCCESS) {
9930 		dnssec_log(zone, ISC_LOG_ERROR,
9931 			   "zone_sign:update_soa_serial -> %s",
9932 			   dns_result_totext(result));
9933 		goto cleanup;
9934 	}
9935 
9936 	/*
9937 	 * Generate maximum life time signatures so that the above loop
9938 	 * termination is sensible.
9939 	 */
9940 	result = add_sigs(db, version, &zone->origin, zone, dns_rdatatype_soa,
9941 			  zonediff.diff, zone_keys, nkeys, zone->mctx,
9942 			  inception, soaexpire, check_ksk, keyset_kskonly);
9943 	if (result != ISC_R_SUCCESS) {
9944 		dnssec_log(zone, ISC_LOG_ERROR, "zone_sign:add_sigs -> %s",
9945 			   dns_result_totext(result));
9946 		goto cleanup;
9947 	}
9948 
9949 	/*
9950 	 * Write changes to journal file.
9951 	 */
9952 	CHECK(zone_journal(zone, zonediff.diff, NULL, "zone_sign"));
9953 
9954 pauseall:
9955 	/*
9956 	 * Pause all iterators so that dns_db_closeversion() can succeed.
9957 	 */
9958 	for (signing = ISC_LIST_HEAD(zone->signing); signing != NULL;
9959 	     signing = ISC_LIST_NEXT(signing, link))
9960 	{
9961 		dns_dbiterator_pause(signing->dbiterator);
9962 	}
9963 
9964 	for (signing = ISC_LIST_HEAD(cleanup); signing != NULL;
9965 	     signing = ISC_LIST_NEXT(signing, link))
9966 	{
9967 		dns_dbiterator_pause(signing->dbiterator);
9968 	}
9969 
9970 	/*
9971 	 * Everything has succeeded. Commit the changes.
9972 	 */
9973 	dns_db_closeversion(db, &version, commit);
9974 
9975 	/*
9976 	 * Everything succeeded so we can clean these up now.
9977 	 */
9978 	signing = ISC_LIST_HEAD(cleanup);
9979 	while (signing != NULL) {
9980 		ISC_LIST_UNLINK(cleanup, signing, link);
9981 		dns_db_detach(&signing->db);
9982 		dns_dbiterator_destroy(&signing->dbiterator);
9983 		isc_mem_put(zone->mctx, signing, sizeof *signing);
9984 		signing = ISC_LIST_HEAD(cleanup);
9985 	}
9986 
9987 	LOCK_ZONE(zone);
9988 	set_resigntime(zone);
9989 	if (commit) {
9990 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
9991 		zone_needdump(zone, DNS_DUMP_DELAY);
9992 	}
9993 	UNLOCK_ZONE(zone);
9994 
9995 failure:
9996 	if (result != ISC_R_SUCCESS) {
9997 		dnssec_log(zone, ISC_LOG_ERROR, "zone_sign: failed: %s",
9998 			   dns_result_totext(result));
9999 	}
10000 
10001 cleanup:
10002 	/*
10003 	 * Pause all dbiterators.
10004 	 */
10005 	for (signing = ISC_LIST_HEAD(zone->signing); signing != NULL;
10006 	     signing = ISC_LIST_NEXT(signing, link))
10007 	{
10008 		dns_dbiterator_pause(signing->dbiterator);
10009 	}
10010 
10011 	/*
10012 	 * Rollback the cleanup list.
10013 	 */
10014 	signing = ISC_LIST_HEAD(cleanup);
10015 	while (signing != NULL) {
10016 		ISC_LIST_UNLINK(cleanup, signing, link);
10017 		ISC_LIST_PREPEND(zone->signing, signing, link);
10018 		dns_dbiterator_first(signing->dbiterator);
10019 		dns_dbiterator_pause(signing->dbiterator);
10020 		signing = ISC_LIST_HEAD(cleanup);
10021 	}
10022 
10023 	dns_diff_clear(&_sig_diff);
10024 
10025 	for (i = 0; i < nkeys; i++) {
10026 		dst_key_free(&zone_keys[i]);
10027 	}
10028 
10029 	if (node != NULL) {
10030 		dns_db_detachnode(db, &node);
10031 	}
10032 
10033 	if (version != NULL) {
10034 		dns_db_closeversion(db, &version, false);
10035 		dns_db_detach(&db);
10036 	} else if (db != NULL) {
10037 		dns_db_detach(&db);
10038 	}
10039 
10040 	LOCK_ZONE(zone);
10041 	if (ISC_LIST_HEAD(zone->signing) != NULL) {
10042 		isc_interval_t interval;
10043 		if (zone->update_disabled || result != ISC_R_SUCCESS) {
10044 			isc_interval_set(&interval, 60, 0); /* 1 minute */
10045 		} else {
10046 			isc_interval_set(&interval, 0, 10000000); /* 10 ms */
10047 		}
10048 		isc_time_nowplusinterval(&zone->signingtime, &interval);
10049 	} else {
10050 		isc_time_settoepoch(&zone->signingtime);
10051 	}
10052 	UNLOCK_ZONE(zone);
10053 
10054 	INSIST(version == NULL);
10055 }
10056 
10057 static isc_result_t
normalize_key(dns_rdata_t * rr,dns_rdata_t * target,unsigned char * data,int size)10058 normalize_key(dns_rdata_t *rr, dns_rdata_t *target, unsigned char *data,
10059 	      int size) {
10060 	dns_rdata_dnskey_t dnskey;
10061 	dns_rdata_keydata_t keydata;
10062 	isc_buffer_t buf;
10063 	isc_result_t result;
10064 
10065 	dns_rdata_reset(target);
10066 	isc_buffer_init(&buf, data, size);
10067 
10068 	switch (rr->type) {
10069 	case dns_rdatatype_dnskey:
10070 		result = dns_rdata_tostruct(rr, &dnskey, NULL);
10071 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
10072 		dnskey.flags &= ~DNS_KEYFLAG_REVOKE;
10073 		dns_rdata_fromstruct(target, rr->rdclass, dns_rdatatype_dnskey,
10074 				     &dnskey, &buf);
10075 		break;
10076 	case dns_rdatatype_keydata:
10077 		result = dns_rdata_tostruct(rr, &keydata, NULL);
10078 		if (result == ISC_R_UNEXPECTEDEND) {
10079 			return (result);
10080 		}
10081 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
10082 		dns_keydata_todnskey(&keydata, &dnskey, NULL);
10083 		dns_rdata_fromstruct(target, rr->rdclass, dns_rdatatype_dnskey,
10084 				     &dnskey, &buf);
10085 		break;
10086 	default:
10087 		INSIST(0);
10088 		ISC_UNREACHABLE();
10089 	}
10090 	return (ISC_R_SUCCESS);
10091 }
10092 
10093 /*
10094  * 'rdset' contains either a DNSKEY rdataset from the zone apex, or
10095  * a KEYDATA rdataset from the key zone.
10096  *
10097  * 'rr' contains either a DNSKEY record, or a KEYDATA record
10098  *
10099  * After normalizing keys to the same format (DNSKEY, with revoke bit
10100  * cleared), return true if a key that matches 'rr' is found in
10101  * 'rdset', or false if not.
10102  */
10103 
10104 static bool
matchkey(dns_rdataset_t * rdset,dns_rdata_t * rr)10105 matchkey(dns_rdataset_t *rdset, dns_rdata_t *rr) {
10106 	unsigned char data1[4096], data2[4096];
10107 	dns_rdata_t rdata, rdata1, rdata2;
10108 	isc_result_t result;
10109 
10110 	dns_rdata_init(&rdata);
10111 	dns_rdata_init(&rdata1);
10112 	dns_rdata_init(&rdata2);
10113 
10114 	result = normalize_key(rr, &rdata1, data1, sizeof(data1));
10115 	if (result != ISC_R_SUCCESS) {
10116 		return (false);
10117 	}
10118 
10119 	for (result = dns_rdataset_first(rdset); result == ISC_R_SUCCESS;
10120 	     result = dns_rdataset_next(rdset))
10121 	{
10122 		dns_rdata_reset(&rdata);
10123 		dns_rdataset_current(rdset, &rdata);
10124 		result = normalize_key(&rdata, &rdata2, data2, sizeof(data2));
10125 		if (result != ISC_R_SUCCESS) {
10126 			continue;
10127 		}
10128 		if (dns_rdata_compare(&rdata1, &rdata2) == 0) {
10129 			return (true);
10130 		}
10131 	}
10132 
10133 	return (false);
10134 }
10135 
10136 /*
10137  * Calculate the refresh interval for a keydata zone, per
10138  * RFC5011: MAX(1 hr,
10139  *		MIN(15 days,
10140  *		    1/2 * OrigTTL,
10141  *		    1/2 * RRSigExpirationInterval))
10142  * or for retries: MAX(1 hr,
10143  *		       MIN(1 day,
10144  *			   1/10 * OrigTTL,
10145  *			   1/10 * RRSigExpirationInterval))
10146  */
10147 static inline isc_stdtime_t
refresh_time(dns_keyfetch_t * kfetch,bool retry)10148 refresh_time(dns_keyfetch_t *kfetch, bool retry) {
10149 	isc_result_t result;
10150 	uint32_t t;
10151 	dns_rdataset_t *rdset;
10152 	dns_rdata_t sigrr = DNS_RDATA_INIT;
10153 	dns_rdata_sig_t sig;
10154 	isc_stdtime_t now;
10155 
10156 	isc_stdtime_get(&now);
10157 
10158 	if (dns_rdataset_isassociated(&kfetch->dnskeysigset)) {
10159 		rdset = &kfetch->dnskeysigset;
10160 	} else {
10161 		return (now + dns_zone_mkey_hour);
10162 	}
10163 
10164 	result = dns_rdataset_first(rdset);
10165 	if (result != ISC_R_SUCCESS) {
10166 		return (now + dns_zone_mkey_hour);
10167 	}
10168 
10169 	dns_rdataset_current(rdset, &sigrr);
10170 	result = dns_rdata_tostruct(&sigrr, &sig, NULL);
10171 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
10172 
10173 	if (!retry) {
10174 		t = sig.originalttl / 2;
10175 
10176 		if (isc_serial_gt(sig.timeexpire, now)) {
10177 			uint32_t exp = (sig.timeexpire - now) / 2;
10178 			if (t > exp) {
10179 				t = exp;
10180 			}
10181 		}
10182 
10183 		if (t > (15 * dns_zone_mkey_day)) {
10184 			t = (15 * dns_zone_mkey_day);
10185 		}
10186 
10187 		if (t < dns_zone_mkey_hour) {
10188 			t = dns_zone_mkey_hour;
10189 		}
10190 	} else {
10191 		t = sig.originalttl / 10;
10192 
10193 		if (isc_serial_gt(sig.timeexpire, now)) {
10194 			uint32_t exp = (sig.timeexpire - now) / 10;
10195 			if (t > exp) {
10196 				t = exp;
10197 			}
10198 		}
10199 
10200 		if (t > dns_zone_mkey_day) {
10201 			t = dns_zone_mkey_day;
10202 		}
10203 
10204 		if (t < dns_zone_mkey_hour) {
10205 			t = dns_zone_mkey_hour;
10206 		}
10207 	}
10208 
10209 	return (now + t);
10210 }
10211 
10212 /*
10213  * This routine is called when no changes are needed in a KEYDATA
10214  * record except to simply update the refresh timer.  Caller should
10215  * hold zone lock.
10216  */
10217 static isc_result_t
minimal_update(dns_keyfetch_t * kfetch,dns_dbversion_t * ver,dns_diff_t * diff)10218 minimal_update(dns_keyfetch_t *kfetch, dns_dbversion_t *ver, dns_diff_t *diff) {
10219 	isc_result_t result;
10220 	isc_buffer_t keyb;
10221 	unsigned char key_buf[4096];
10222 	dns_rdata_t rdata = DNS_RDATA_INIT;
10223 	dns_rdata_keydata_t keydata;
10224 	dns_name_t *name;
10225 	dns_zone_t *zone = kfetch->zone;
10226 	isc_stdtime_t now;
10227 
10228 	name = dns_fixedname_name(&kfetch->name);
10229 	isc_stdtime_get(&now);
10230 
10231 	for (result = dns_rdataset_first(&kfetch->keydataset);
10232 	     result == ISC_R_SUCCESS;
10233 	     result = dns_rdataset_next(&kfetch->keydataset))
10234 	{
10235 		dns_rdata_reset(&rdata);
10236 		dns_rdataset_current(&kfetch->keydataset, &rdata);
10237 
10238 		/* Delete old version */
10239 		CHECK(update_one_rr(kfetch->db, ver, diff, DNS_DIFFOP_DEL, name,
10240 				    0, &rdata));
10241 
10242 		/* Update refresh timer */
10243 		result = dns_rdata_tostruct(&rdata, &keydata, NULL);
10244 		if (result == ISC_R_UNEXPECTEDEND) {
10245 			continue;
10246 		}
10247 		if (result != ISC_R_SUCCESS) {
10248 			goto failure;
10249 		}
10250 		keydata.refresh = refresh_time(kfetch, true);
10251 		set_refreshkeytimer(zone, &keydata, now, false);
10252 
10253 		dns_rdata_reset(&rdata);
10254 		isc_buffer_init(&keyb, key_buf, sizeof(key_buf));
10255 		CHECK(dns_rdata_fromstruct(&rdata, zone->rdclass,
10256 					   dns_rdatatype_keydata, &keydata,
10257 					   &keyb));
10258 
10259 		/* Insert updated version */
10260 		CHECK(update_one_rr(kfetch->db, ver, diff, DNS_DIFFOP_ADD, name,
10261 				    0, &rdata));
10262 	}
10263 	result = ISC_R_SUCCESS;
10264 failure:
10265 	return (result);
10266 }
10267 
10268 /*
10269  * Verify that DNSKEY set is signed by the key specified in 'keydata'.
10270  */
10271 static bool
revocable(dns_keyfetch_t * kfetch,dns_rdata_keydata_t * keydata)10272 revocable(dns_keyfetch_t *kfetch, dns_rdata_keydata_t *keydata) {
10273 	isc_result_t result;
10274 	dns_name_t *keyname;
10275 	isc_mem_t *mctx;
10276 	dns_rdata_t sigrr = DNS_RDATA_INIT;
10277 	dns_rdata_t rr = DNS_RDATA_INIT;
10278 	dns_rdata_rrsig_t sig;
10279 	dns_rdata_dnskey_t dnskey;
10280 	dst_key_t *dstkey = NULL;
10281 	unsigned char key_buf[4096];
10282 	isc_buffer_t keyb;
10283 	bool answer = false;
10284 
10285 	REQUIRE(kfetch != NULL && keydata != NULL);
10286 	REQUIRE(dns_rdataset_isassociated(&kfetch->dnskeysigset));
10287 
10288 	keyname = dns_fixedname_name(&kfetch->name);
10289 	mctx = kfetch->zone->view->mctx;
10290 
10291 	/* Generate a key from keydata */
10292 	isc_buffer_init(&keyb, key_buf, sizeof(key_buf));
10293 	dns_keydata_todnskey(keydata, &dnskey, NULL);
10294 	dns_rdata_fromstruct(&rr, keydata->common.rdclass, dns_rdatatype_dnskey,
10295 			     &dnskey, &keyb);
10296 	result = dns_dnssec_keyfromrdata(keyname, &rr, mctx, &dstkey);
10297 	if (result != ISC_R_SUCCESS) {
10298 		return (false);
10299 	}
10300 
10301 	/* See if that key generated any of the signatures */
10302 	for (result = dns_rdataset_first(&kfetch->dnskeysigset);
10303 	     result == ISC_R_SUCCESS;
10304 	     result = dns_rdataset_next(&kfetch->dnskeysigset))
10305 	{
10306 		dns_fixedname_t fixed;
10307 		dns_fixedname_init(&fixed);
10308 
10309 		dns_rdata_reset(&sigrr);
10310 		dns_rdataset_current(&kfetch->dnskeysigset, &sigrr);
10311 		result = dns_rdata_tostruct(&sigrr, &sig, NULL);
10312 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
10313 
10314 		if (dst_key_alg(dstkey) == sig.algorithm &&
10315 		    dst_key_rid(dstkey) == sig.keyid) {
10316 			result = dns_dnssec_verify(
10317 				keyname, &kfetch->dnskeyset, dstkey, false, 0,
10318 				mctx, &sigrr, dns_fixedname_name(&fixed));
10319 
10320 			dnssec_log(kfetch->zone, ISC_LOG_DEBUG(3),
10321 				   "Confirm revoked DNSKEY is self-signed: %s",
10322 				   dns_result_totext(result));
10323 
10324 			if (result == ISC_R_SUCCESS) {
10325 				answer = true;
10326 				break;
10327 			}
10328 		}
10329 	}
10330 
10331 	dst_key_free(&dstkey);
10332 	return (answer);
10333 }
10334 
10335 /*
10336  * A DNSKEY set has been fetched from the zone apex of a zone whose trust
10337  * anchors are being managed; scan the keyset, and update the key zone and the
10338  * local trust anchors according to RFC5011.
10339  */
10340 static void
keyfetch_done(isc_task_t * task,isc_event_t * event)10341 keyfetch_done(isc_task_t *task, isc_event_t *event) {
10342 	isc_result_t result, eresult;
10343 	dns_fetchevent_t *devent;
10344 	dns_keyfetch_t *kfetch;
10345 	dns_zone_t *zone;
10346 	isc_mem_t *mctx = NULL;
10347 	dns_keytable_t *secroots = NULL;
10348 	dns_dbversion_t *ver = NULL;
10349 	dns_diff_t diff;
10350 	bool alldone = false;
10351 	bool commit = false;
10352 	dns_name_t *keyname = NULL;
10353 	dns_rdata_t sigrr = DNS_RDATA_INIT;
10354 	dns_rdata_t dnskeyrr = DNS_RDATA_INIT;
10355 	dns_rdata_t keydatarr = DNS_RDATA_INIT;
10356 	dns_rdata_rrsig_t sig;
10357 	dns_rdata_dnskey_t dnskey;
10358 	dns_rdata_keydata_t keydata;
10359 	bool initializing;
10360 	char namebuf[DNS_NAME_FORMATSIZE];
10361 	unsigned char key_buf[4096];
10362 	isc_buffer_t keyb;
10363 	dst_key_t *dstkey = NULL;
10364 	isc_stdtime_t now;
10365 	int pending = 0;
10366 	bool secure = false, initial = false;
10367 	bool free_needed;
10368 	dns_keynode_t *keynode = NULL;
10369 	dns_rdataset_t *dnskeys = NULL, *dnskeysigs = NULL;
10370 	dns_rdataset_t *keydataset = NULL, dsset;
10371 
10372 	UNUSED(task);
10373 	INSIST(event != NULL && event->ev_type == DNS_EVENT_FETCHDONE);
10374 	INSIST(event->ev_arg != NULL);
10375 
10376 	kfetch = event->ev_arg;
10377 	zone = kfetch->zone;
10378 	isc_mem_attach(zone->mctx, &mctx);
10379 	keyname = dns_fixedname_name(&kfetch->name);
10380 	dnskeys = &kfetch->dnskeyset;
10381 	dnskeysigs = &kfetch->dnskeysigset;
10382 	keydataset = &kfetch->keydataset;
10383 
10384 	devent = (dns_fetchevent_t *)event;
10385 	eresult = devent->result;
10386 
10387 	/* Free resources which are not of interest */
10388 	if (devent->node != NULL) {
10389 		dns_db_detachnode(devent->db, &devent->node);
10390 	}
10391 	if (devent->db != NULL) {
10392 		dns_db_detach(&devent->db);
10393 	}
10394 	isc_event_free(&event);
10395 	dns_resolver_destroyfetch(&kfetch->fetch);
10396 
10397 	LOCK_ZONE(zone);
10398 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING) || zone->view == NULL) {
10399 		goto cleanup;
10400 	}
10401 
10402 	isc_stdtime_get(&now);
10403 	dns_name_format(keyname, namebuf, sizeof(namebuf));
10404 
10405 	result = dns_view_getsecroots(zone->view, &secroots);
10406 	INSIST(result == ISC_R_SUCCESS);
10407 
10408 	dns_diff_init(mctx, &diff);
10409 
10410 	CHECK(dns_db_newversion(kfetch->db, &ver));
10411 
10412 	zone->refreshkeycount--;
10413 	alldone = (zone->refreshkeycount == 0);
10414 
10415 	if (alldone) {
10416 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESHING);
10417 	}
10418 
10419 	dnssec_log(zone, ISC_LOG_DEBUG(3),
10420 		   "Returned from key fetch in keyfetch_done() for '%s': %s",
10421 		   namebuf, dns_result_totext(eresult));
10422 
10423 	/* Fetch failed */
10424 	if (eresult != ISC_R_SUCCESS || !dns_rdataset_isassociated(dnskeys)) {
10425 		dnssec_log(zone, ISC_LOG_WARNING,
10426 			   "Unable to fetch DNSKEY set '%s': %s", namebuf,
10427 			   dns_result_totext(eresult));
10428 		CHECK(minimal_update(kfetch, ver, &diff));
10429 		goto done;
10430 	}
10431 
10432 	/* No RRSIGs found */
10433 	if (!dns_rdataset_isassociated(dnskeysigs)) {
10434 		dnssec_log(zone, ISC_LOG_WARNING,
10435 			   "No DNSKEY RRSIGs found for '%s': %s", namebuf,
10436 			   dns_result_totext(eresult));
10437 		CHECK(minimal_update(kfetch, ver, &diff));
10438 		goto done;
10439 	}
10440 
10441 	/*
10442 	 * Clear any cached trust level, as we need to run validation
10443 	 * over again; trusted keys might have changed.
10444 	 */
10445 	dnskeys->trust = dnskeysigs->trust = dns_trust_none;
10446 
10447 	/* Look up the trust anchor */
10448 	result = dns_keytable_find(secroots, keyname, &keynode);
10449 	if (result != ISC_R_SUCCESS) {
10450 		goto anchors_done;
10451 	}
10452 
10453 	/*
10454 	 * If the keynode has a DS trust anchor, use it for verification.
10455 	 */
10456 	dns_rdataset_init(&dsset);
10457 	if (dns_keynode_dsset(keynode, &dsset)) {
10458 		for (result = dns_rdataset_first(dnskeysigs);
10459 		     result == ISC_R_SUCCESS;
10460 		     result = dns_rdataset_next(dnskeysigs))
10461 		{
10462 			isc_result_t tresult;
10463 			dns_rdata_t keyrdata = DNS_RDATA_INIT;
10464 
10465 			dns_rdata_reset(&sigrr);
10466 			dns_rdataset_current(dnskeysigs, &sigrr);
10467 			result = dns_rdata_tostruct(&sigrr, &sig, NULL);
10468 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
10469 
10470 			for (tresult = dns_rdataset_first(&dsset);
10471 			     tresult == ISC_R_SUCCESS;
10472 			     tresult = dns_rdataset_next(&dsset))
10473 			{
10474 				dns_rdata_t dsrdata = DNS_RDATA_INIT;
10475 				dns_rdata_ds_t ds;
10476 
10477 				dns_rdata_reset(&dsrdata);
10478 				dns_rdataset_current(&dsset, &dsrdata);
10479 				tresult = dns_rdata_tostruct(&dsrdata, &ds,
10480 							     NULL);
10481 				RUNTIME_CHECK(tresult == ISC_R_SUCCESS);
10482 
10483 				if (ds.key_tag != sig.keyid ||
10484 				    ds.algorithm != sig.algorithm) {
10485 					continue;
10486 				}
10487 
10488 				result = dns_dnssec_matchdskey(
10489 					keyname, &dsrdata, dnskeys, &keyrdata);
10490 				if (result == ISC_R_SUCCESS) {
10491 					break;
10492 				}
10493 			}
10494 
10495 			if (tresult == ISC_R_NOMORE) {
10496 				continue;
10497 			}
10498 
10499 			result = dns_dnssec_keyfromrdata(keyname, &keyrdata,
10500 							 mctx, &dstkey);
10501 			if (result != ISC_R_SUCCESS) {
10502 				continue;
10503 			}
10504 
10505 			result = dns_dnssec_verify(keyname, dnskeys, dstkey,
10506 						   false, 0, mctx, &sigrr,
10507 						   NULL);
10508 			dst_key_free(&dstkey);
10509 
10510 			dnssec_log(zone, ISC_LOG_DEBUG(3),
10511 				   "Verifying DNSKEY set for zone "
10512 				   "'%s' using DS %d/%d: %s",
10513 				   namebuf, sig.keyid, sig.algorithm,
10514 				   dns_result_totext(result));
10515 
10516 			if (result == ISC_R_SUCCESS) {
10517 				dnskeys->trust = dns_trust_secure;
10518 				dnskeysigs->trust = dns_trust_secure;
10519 				initial = dns_keynode_initial(keynode);
10520 				dns_keynode_trust(keynode);
10521 				secure = true;
10522 				break;
10523 			}
10524 		}
10525 		dns_rdataset_disassociate(&dsset);
10526 	}
10527 
10528 anchors_done:
10529 	if (keynode != NULL) {
10530 		dns_keytable_detachkeynode(secroots, &keynode);
10531 	}
10532 
10533 	/*
10534 	 * If we were not able to verify the answer using the current
10535 	 * trusted keys then all we can do is look at any revoked keys.
10536 	 */
10537 	if (!secure) {
10538 		dnssec_log(zone, ISC_LOG_INFO,
10539 			   "DNSKEY set for zone '%s' could not be verified "
10540 			   "with current keys",
10541 			   namebuf);
10542 	}
10543 
10544 	/*
10545 	 * First scan keydataset to find keys that are not in dnskeyset
10546 	 *   - Missing keys which are not scheduled for removal,
10547 	 *     log a warning
10548 	 *   - Missing keys which are scheduled for removal and
10549 	 *     the remove hold-down timer has completed should
10550 	 *     be removed from the key zone
10551 	 *   - Missing keys whose acceptance timers have not yet
10552 	 *     completed, log a warning and reset the acceptance
10553 	 *     timer to 30 days in the future
10554 	 *   - All keys not being removed have their refresh timers
10555 	 *     updated
10556 	 */
10557 	initializing = true;
10558 	for (result = dns_rdataset_first(keydataset); result == ISC_R_SUCCESS;
10559 	     result = dns_rdataset_next(keydataset))
10560 	{
10561 		dns_keytag_t keytag;
10562 
10563 		dns_rdata_reset(&keydatarr);
10564 		dns_rdataset_current(keydataset, &keydatarr);
10565 		result = dns_rdata_tostruct(&keydatarr, &keydata, NULL);
10566 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
10567 
10568 		dns_keydata_todnskey(&keydata, &dnskey, NULL);
10569 		result = compute_tag(keyname, &dnskey, mctx, &keytag);
10570 		if (result != ISC_R_SUCCESS) {
10571 			/*
10572 			 * Skip if we cannot compute the key tag.
10573 			 * This may happen if the algorithm is unsupported
10574 			 */
10575 			dns_zone_log(zone, ISC_LOG_ERROR,
10576 				     "Cannot compute tag for key in zone %s: "
10577 				     "%s "
10578 				     "(skipping)",
10579 				     namebuf, dns_result_totext(result));
10580 			continue;
10581 		}
10582 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
10583 
10584 		/*
10585 		 * If any keydata record has a nonzero add holddown, then
10586 		 * there was a pre-existing trust anchor for this domain;
10587 		 * that means we are *not* initializing it and shouldn't
10588 		 * automatically trust all the keys we find at the zone apex.
10589 		 */
10590 		initializing = initializing && (keydata.addhd == 0);
10591 
10592 		if (!matchkey(dnskeys, &keydatarr)) {
10593 			bool deletekey = false;
10594 
10595 			if (!secure) {
10596 				if (keydata.removehd != 0 &&
10597 				    keydata.removehd <= now) {
10598 					deletekey = true;
10599 				}
10600 			} else if (keydata.addhd == 0) {
10601 				deletekey = true;
10602 			} else if (keydata.addhd > now) {
10603 				dnssec_log(zone, ISC_LOG_INFO,
10604 					   "Pending key %d for zone %s "
10605 					   "unexpectedly missing "
10606 					   "restarting 30-day acceptance "
10607 					   "timer",
10608 					   keytag, namebuf);
10609 				if (keydata.addhd < now + dns_zone_mkey_month) {
10610 					keydata.addhd = now +
10611 							dns_zone_mkey_month;
10612 				}
10613 				keydata.refresh = refresh_time(kfetch, false);
10614 			} else if (keydata.removehd == 0) {
10615 				dnssec_log(zone, ISC_LOG_INFO,
10616 					   "Active key %d for zone %s "
10617 					   "unexpectedly missing",
10618 					   keytag, namebuf);
10619 				keydata.refresh = now + dns_zone_mkey_hour;
10620 			} else if (keydata.removehd <= now) {
10621 				deletekey = true;
10622 				dnssec_log(zone, ISC_LOG_INFO,
10623 					   "Revoked key %d for zone %s "
10624 					   "missing: deleting from "
10625 					   "managed keys database",
10626 					   keytag, namebuf);
10627 			} else {
10628 				keydata.refresh = refresh_time(kfetch, false);
10629 			}
10630 
10631 			if (secure || deletekey) {
10632 				/* Delete old version */
10633 				CHECK(update_one_rr(kfetch->db, ver, &diff,
10634 						    DNS_DIFFOP_DEL, keyname, 0,
10635 						    &keydatarr));
10636 			}
10637 
10638 			if (!secure || deletekey) {
10639 				continue;
10640 			}
10641 
10642 			dns_rdata_reset(&keydatarr);
10643 			isc_buffer_init(&keyb, key_buf, sizeof(key_buf));
10644 			dns_rdata_fromstruct(&keydatarr, zone->rdclass,
10645 					     dns_rdatatype_keydata, &keydata,
10646 					     &keyb);
10647 
10648 			/* Insert updated version */
10649 			CHECK(update_one_rr(kfetch->db, ver, &diff,
10650 					    DNS_DIFFOP_ADD, keyname, 0,
10651 					    &keydatarr));
10652 
10653 			set_refreshkeytimer(zone, &keydata, now, false);
10654 		}
10655 	}
10656 
10657 	/*
10658 	 * Next scan dnskeyset:
10659 	 *   - If new keys are found (i.e., lacking a match in keydataset)
10660 	 *     add them to the key zone and set the acceptance timer
10661 	 *     to 30 days in the future (or to immediately if we've
10662 	 *     determined that we're initializing the zone for the
10663 	 *     first time)
10664 	 *   - Previously-known keys that have been revoked
10665 	 *     must be scheduled for removal from the key zone (or,
10666 	 *     if they hadn't been accepted as trust anchors yet
10667 	 *     anyway, removed at once)
10668 	 *   - Previously-known unrevoked keys whose acceptance timers
10669 	 *     have completed are promoted to trust anchors
10670 	 *   - All keys not being removed have their refresh
10671 	 *     timers updated
10672 	 */
10673 	for (result = dns_rdataset_first(dnskeys); result == ISC_R_SUCCESS;
10674 	     result = dns_rdataset_next(dnskeys))
10675 	{
10676 		bool revoked = false;
10677 		bool newkey = false;
10678 		bool updatekey = false;
10679 		bool deletekey = false;
10680 		bool trustkey = false;
10681 		dns_keytag_t keytag;
10682 
10683 		dns_rdata_reset(&dnskeyrr);
10684 		dns_rdataset_current(dnskeys, &dnskeyrr);
10685 		result = dns_rdata_tostruct(&dnskeyrr, &dnskey, NULL);
10686 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
10687 
10688 		/* Skip ZSK's */
10689 		if ((dnskey.flags & DNS_KEYFLAG_KSK) == 0) {
10690 			continue;
10691 		}
10692 
10693 		result = compute_tag(keyname, &dnskey, mctx, &keytag);
10694 		if (result != ISC_R_SUCCESS) {
10695 			/*
10696 			 * Skip if we cannot compute the key tag.
10697 			 * This may happen if the algorithm is unsupported
10698 			 */
10699 			dns_zone_log(zone, ISC_LOG_ERROR,
10700 				     "Cannot compute tag for key in zone %s: "
10701 				     "%s "
10702 				     "(skipping)",
10703 				     namebuf, dns_result_totext(result));
10704 			continue;
10705 		}
10706 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
10707 
10708 		revoked = ((dnskey.flags & DNS_KEYFLAG_REVOKE) != 0);
10709 
10710 		if (matchkey(keydataset, &dnskeyrr)) {
10711 			dns_rdata_reset(&keydatarr);
10712 			dns_rdataset_current(keydataset, &keydatarr);
10713 			result = dns_rdata_tostruct(&keydatarr, &keydata, NULL);
10714 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
10715 
10716 			if (revoked && revocable(kfetch, &keydata)) {
10717 				if (keydata.addhd > now) {
10718 					/*
10719 					 * Key wasn't trusted yet, and now
10720 					 * it's been revoked?  Just remove it
10721 					 */
10722 					deletekey = true;
10723 					dnssec_log(zone, ISC_LOG_INFO,
10724 						   "Pending key %d for "
10725 						   "zone %s is now revoked: "
10726 						   "deleting from the "
10727 						   "managed keys database",
10728 						   keytag, namebuf);
10729 				} else if (keydata.removehd == 0) {
10730 					/*
10731 					 * Remove key from secroots.
10732 					 */
10733 					dns_view_untrust(zone->view, keyname,
10734 							 &dnskey);
10735 
10736 					/* If initializing, delete now */
10737 					if (keydata.addhd == 0) {
10738 						deletekey = true;
10739 					} else {
10740 						keydata.removehd =
10741 							now +
10742 							dns_zone_mkey_month;
10743 						keydata.flags |=
10744 							DNS_KEYFLAG_REVOKE;
10745 					}
10746 
10747 					dnssec_log(zone, ISC_LOG_INFO,
10748 						   "Trusted key %d for "
10749 						   "zone %s is now revoked",
10750 						   keytag, namebuf);
10751 				} else if (keydata.removehd < now) {
10752 					/* Scheduled for removal */
10753 					deletekey = true;
10754 
10755 					dnssec_log(zone, ISC_LOG_INFO,
10756 						   "Revoked key %d for "
10757 						   "zone %s removal timer "
10758 						   "complete: deleting from "
10759 						   "the managed keys database",
10760 						   keytag, namebuf);
10761 				}
10762 			} else if (revoked && keydata.removehd == 0) {
10763 				dnssec_log(zone, ISC_LOG_WARNING,
10764 					   "Active key %d for zone "
10765 					   "%s is revoked but "
10766 					   "did not self-sign; "
10767 					   "ignoring",
10768 					   keytag, namebuf);
10769 				continue;
10770 			} else if (secure) {
10771 				if (keydata.removehd != 0) {
10772 					/*
10773 					 * Key isn't revoked--but it
10774 					 * seems it used to be.
10775 					 * Remove it now and add it
10776 					 * back as if it were a fresh key,
10777 					 * with a 30-day acceptance timer.
10778 					 */
10779 					deletekey = true;
10780 					newkey = true;
10781 					keydata.removehd = 0;
10782 					keydata.addhd = now +
10783 							dns_zone_mkey_month;
10784 
10785 					dnssec_log(zone, ISC_LOG_INFO,
10786 						   "Revoked key %d for "
10787 						   "zone %s has returned: "
10788 						   "starting 30-day "
10789 						   "acceptance timer",
10790 						   keytag, namebuf);
10791 				} else if (keydata.addhd > now) {
10792 					pending++;
10793 				} else if (keydata.addhd == 0) {
10794 					keydata.addhd = now;
10795 				}
10796 
10797 				if (keydata.addhd <= now) {
10798 					trustkey = true;
10799 					dnssec_log(zone, ISC_LOG_INFO,
10800 						   "Key %d for zone %s "
10801 						   "is now trusted (%s)",
10802 						   keytag, namebuf,
10803 						   initial ? "initializing key "
10804 							     "verified"
10805 							   : "acceptance timer "
10806 							     "complete");
10807 				}
10808 			} else if (keydata.addhd > now) {
10809 				/*
10810 				 * Not secure, and key is pending:
10811 				 * reset the acceptance timer
10812 				 */
10813 				pending++;
10814 				keydata.addhd = now + dns_zone_mkey_month;
10815 				dnssec_log(zone, ISC_LOG_INFO,
10816 					   "Pending key %d "
10817 					   "for zone %s was "
10818 					   "not validated: restarting "
10819 					   "30-day acceptance timer",
10820 					   keytag, namebuf);
10821 			}
10822 
10823 			if (!deletekey && !newkey) {
10824 				updatekey = true;
10825 			}
10826 		} else if (secure) {
10827 			/*
10828 			 * Key wasn't in the key zone but it's
10829 			 * revoked now anyway, so just skip it
10830 			 */
10831 			if (revoked) {
10832 				continue;
10833 			}
10834 
10835 			/* Key wasn't in the key zone: add it */
10836 			newkey = true;
10837 
10838 			if (initializing) {
10839 				dnssec_log(zone, ISC_LOG_WARNING,
10840 					   "Initializing automatic trust "
10841 					   "anchor management for zone '%s'; "
10842 					   "DNSKEY ID %d is now trusted, "
10843 					   "waiving the normal 30-day "
10844 					   "waiting period.",
10845 					   namebuf, keytag);
10846 				trustkey = true;
10847 			} else {
10848 				dnssec_log(zone, ISC_LOG_INFO,
10849 					   "New key %d observed "
10850 					   "for zone '%s': "
10851 					   "starting 30-day "
10852 					   "acceptance timer",
10853 					   keytag, namebuf);
10854 			}
10855 		} else {
10856 			/*
10857 			 * No previously known key, and the key is not
10858 			 * secure, so skip it.
10859 			 */
10860 			continue;
10861 		}
10862 
10863 		/* Delete old version */
10864 		if (deletekey || !newkey) {
10865 			CHECK(update_one_rr(kfetch->db, ver, &diff,
10866 					    DNS_DIFFOP_DEL, keyname, 0,
10867 					    &keydatarr));
10868 		}
10869 
10870 		if (updatekey) {
10871 			/* Set refresh timer */
10872 			keydata.refresh = refresh_time(kfetch, false);
10873 			dns_rdata_reset(&keydatarr);
10874 			isc_buffer_init(&keyb, key_buf, sizeof(key_buf));
10875 			dns_rdata_fromstruct(&keydatarr, zone->rdclass,
10876 					     dns_rdatatype_keydata, &keydata,
10877 					     &keyb);
10878 
10879 			/* Insert updated version */
10880 			CHECK(update_one_rr(kfetch->db, ver, &diff,
10881 					    DNS_DIFFOP_ADD, keyname, 0,
10882 					    &keydatarr));
10883 		} else if (newkey) {
10884 			/* Convert DNSKEY to KEYDATA */
10885 			result = dns_rdata_tostruct(&dnskeyrr, &dnskey, NULL);
10886 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
10887 			dns_keydata_fromdnskey(&keydata, &dnskey, 0, 0, 0,
10888 					       NULL);
10889 			keydata.addhd = initializing
10890 						? now
10891 						: now + dns_zone_mkey_month;
10892 			keydata.refresh = refresh_time(kfetch, false);
10893 			dns_rdata_reset(&keydatarr);
10894 			isc_buffer_init(&keyb, key_buf, sizeof(key_buf));
10895 			dns_rdata_fromstruct(&keydatarr, zone->rdclass,
10896 					     dns_rdatatype_keydata, &keydata,
10897 					     &keyb);
10898 
10899 			/* Insert into key zone */
10900 			CHECK(update_one_rr(kfetch->db, ver, &diff,
10901 					    DNS_DIFFOP_ADD, keyname, 0,
10902 					    &keydatarr));
10903 		}
10904 
10905 		if (trustkey) {
10906 			/* Trust this key. */
10907 			result = dns_rdata_tostruct(&dnskeyrr, &dnskey, NULL);
10908 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
10909 			trust_key(zone, keyname, &dnskey, false);
10910 		}
10911 
10912 		if (secure && !deletekey) {
10913 			INSIST(newkey || updatekey);
10914 			set_refreshkeytimer(zone, &keydata, now, false);
10915 		}
10916 	}
10917 
10918 	/*
10919 	 * RFC5011 says, "A trust point that has all of its trust anchors
10920 	 * revoked is considered deleted and is treated as if the trust
10921 	 * point was never configured."  But if someone revoked their
10922 	 * active key before the standby was trusted, that would mean the
10923 	 * zone would suddenly be nonsecured.  We avoid this by checking to
10924 	 * see if there's pending keydata.  If so, we put a null key in
10925 	 * the security roots; then all queries to the zone will fail.
10926 	 */
10927 	if (pending != 0) {
10928 		fail_secure(zone, keyname);
10929 	}
10930 
10931 done:
10932 	if (!ISC_LIST_EMPTY(diff.tuples)) {
10933 		/* Write changes to journal file. */
10934 		CHECK(update_soa_serial(zone, kfetch->db, ver, &diff, mctx,
10935 					zone->updatemethod));
10936 		CHECK(zone_journal(zone, &diff, NULL, "keyfetch_done"));
10937 		commit = true;
10938 
10939 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED);
10940 		zone_needdump(zone, 30);
10941 	} else if (result == ISC_R_NOMORE) {
10942 		/*
10943 		 * If "updatekey" was true for all keys found in the DNSKEY
10944 		 * response and the previous update of those keys happened
10945 		 * during the same second (only possible if a key refresh was
10946 		 * externally triggered), it may happen that all relevant
10947 		 * update_one_rr() calls will return ISC_R_SUCCESS, but
10948 		 * diff.tuples will remain empty.  Reset result to
10949 		 * ISC_R_SUCCESS to prevent a bogus warning from being logged.
10950 		 */
10951 		result = ISC_R_SUCCESS;
10952 	}
10953 
10954 failure:
10955 	if (result != ISC_R_SUCCESS) {
10956 		dnssec_log(zone, ISC_LOG_ERROR,
10957 			   "error during managed-keys processing (%s): "
10958 			   "DNSSEC validation may be at risk",
10959 			   isc_result_totext(result));
10960 	}
10961 	dns_diff_clear(&diff);
10962 	if (ver != NULL) {
10963 		dns_db_closeversion(kfetch->db, &ver, commit);
10964 	}
10965 
10966 cleanup:
10967 	dns_db_detach(&kfetch->db);
10968 
10969 	isc_refcount_decrement(&zone->irefs);
10970 
10971 	kfetch->zone = NULL;
10972 
10973 	if (dns_rdataset_isassociated(keydataset)) {
10974 		dns_rdataset_disassociate(keydataset);
10975 	}
10976 	if (dns_rdataset_isassociated(dnskeys)) {
10977 		dns_rdataset_disassociate(dnskeys);
10978 	}
10979 	if (dns_rdataset_isassociated(dnskeysigs)) {
10980 		dns_rdataset_disassociate(dnskeysigs);
10981 	}
10982 
10983 	dns_name_free(keyname, mctx);
10984 	isc_mem_putanddetach(&mctx, kfetch, sizeof(dns_keyfetch_t));
10985 
10986 	if (secroots != NULL) {
10987 		dns_keytable_detach(&secroots);
10988 	}
10989 
10990 	free_needed = exit_check(zone);
10991 	UNLOCK_ZONE(zone);
10992 	if (free_needed) {
10993 		zone_free(zone);
10994 	}
10995 
10996 	INSIST(ver == NULL);
10997 }
10998 
10999 /*
11000  * Refresh the data in the key zone.  Initiate a fetch to look up
11001  * DNSKEY records at the trust anchor name.
11002  */
11003 static void
zone_refreshkeys(dns_zone_t * zone)11004 zone_refreshkeys(dns_zone_t *zone) {
11005 	const char me[] = "zone_refreshkeys";
11006 	isc_result_t result;
11007 	dns_rriterator_t rrit;
11008 	dns_db_t *db = NULL;
11009 	dns_dbversion_t *ver = NULL;
11010 	dns_diff_t diff;
11011 	dns_rdata_t rdata = DNS_RDATA_INIT;
11012 	dns_rdata_keydata_t kd;
11013 	isc_stdtime_t now;
11014 	bool commit = false;
11015 	bool fetching = false, fetch_err = false;
11016 	bool timerset = false;
11017 
11018 	ENTER;
11019 	REQUIRE(zone->db != NULL);
11020 
11021 	isc_stdtime_get(&now);
11022 
11023 	LOCK_ZONE(zone);
11024 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
11025 		isc_time_settoepoch(&zone->refreshkeytime);
11026 		UNLOCK_ZONE(zone);
11027 		return;
11028 	}
11029 
11030 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
11031 	dns_db_attach(zone->db, &db);
11032 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
11033 
11034 	dns_diff_init(zone->mctx, &diff);
11035 
11036 	CHECK(dns_db_newversion(db, &ver));
11037 
11038 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESHING);
11039 
11040 	dns_rriterator_init(&rrit, db, ver, 0);
11041 	for (result = dns_rriterator_first(&rrit); result == ISC_R_SUCCESS;
11042 	     result = dns_rriterator_nextrrset(&rrit))
11043 	{
11044 		isc_stdtime_t timer = 0xffffffff;
11045 		dns_name_t *name = NULL, *kname = NULL;
11046 		dns_rdataset_t *kdset = NULL;
11047 		dns_keyfetch_t *kfetch;
11048 		uint32_t ttl;
11049 
11050 		dns_rriterator_current(&rrit, &name, &ttl, &kdset, NULL);
11051 		if (kdset == NULL || kdset->type != dns_rdatatype_keydata ||
11052 		    !dns_rdataset_isassociated(kdset))
11053 		{
11054 			continue;
11055 		}
11056 
11057 		/*
11058 		 * Scan the stored keys looking for ones that need
11059 		 * removal or refreshing
11060 		 */
11061 		for (result = dns_rdataset_first(kdset);
11062 		     result == ISC_R_SUCCESS; result = dns_rdataset_next(kdset))
11063 		{
11064 			dns_rdata_reset(&rdata);
11065 			dns_rdataset_current(kdset, &rdata);
11066 			result = dns_rdata_tostruct(&rdata, &kd, NULL);
11067 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
11068 
11069 			/* Removal timer expired? */
11070 			if (kd.removehd != 0 && kd.removehd < now) {
11071 				dns_rriterator_pause(&rrit);
11072 				CHECK(update_one_rr(db, ver, &diff,
11073 						    DNS_DIFFOP_DEL, name, ttl,
11074 						    &rdata));
11075 				continue;
11076 			}
11077 
11078 			/* Acceptance timer expired? */
11079 			if (kd.addhd <= now) {
11080 				timer = kd.addhd;
11081 			}
11082 
11083 			/* Or do we just need to refresh the keyset? */
11084 			if (timer > kd.refresh) {
11085 				timer = kd.refresh;
11086 			}
11087 
11088 			dns_rriterator_pause(&rrit);
11089 			set_refreshkeytimer(zone, &kd, now, false);
11090 			timerset = true;
11091 		}
11092 
11093 		if (timer > now) {
11094 			continue;
11095 		}
11096 
11097 		dns_rriterator_pause(&rrit);
11098 
11099 		kfetch = isc_mem_get(zone->mctx, sizeof(dns_keyfetch_t));
11100 
11101 		zone->refreshkeycount++;
11102 		kfetch->zone = zone;
11103 		isc_refcount_increment0(&zone->irefs);
11104 		kname = dns_fixedname_initname(&kfetch->name);
11105 		dns_name_dup(name, zone->mctx, kname);
11106 		dns_rdataset_init(&kfetch->dnskeyset);
11107 		dns_rdataset_init(&kfetch->dnskeysigset);
11108 		dns_rdataset_init(&kfetch->keydataset);
11109 		dns_rdataset_clone(kdset, &kfetch->keydataset);
11110 		kfetch->db = NULL;
11111 		dns_db_attach(db, &kfetch->db);
11112 		kfetch->fetch = NULL;
11113 
11114 		if (isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3))) {
11115 			char namebuf[DNS_NAME_FORMATSIZE];
11116 			dns_name_format(kname, namebuf, sizeof(namebuf));
11117 			dnssec_log(zone, ISC_LOG_DEBUG(3),
11118 				   "Creating key fetch in "
11119 				   "zone_refreshkeys() for '%s'",
11120 				   namebuf);
11121 		}
11122 
11123 		/*
11124 		 * Use of DNS_FETCHOPT_NOCACHED is essential here.  If it is
11125 		 * not set and the cache still holds a non-expired, validated
11126 		 * version of the RRset being queried for by the time the
11127 		 * response is received, the cached RRset will be passed to
11128 		 * keyfetch_done() instead of the one received in the response
11129 		 * as the latter will have a lower trust level due to not being
11130 		 * validated until keyfetch_done() is called.
11131 		 */
11132 
11133 #ifdef ENABLE_AFL
11134 		if (!dns_fuzzing_resolver) {
11135 #endif /* ifdef ENABLE_AFL */
11136 			UNLOCK_ZONE(zone);
11137 			result = dns_resolver_createfetch(
11138 				zone->view->resolver, kname,
11139 				dns_rdatatype_dnskey, NULL, NULL, NULL, NULL, 0,
11140 				DNS_FETCHOPT_NOVALIDATE |
11141 					DNS_FETCHOPT_UNSHARED |
11142 					DNS_FETCHOPT_NOCACHED,
11143 				0, NULL, zone->task, keyfetch_done, kfetch,
11144 				&kfetch->dnskeyset, &kfetch->dnskeysigset,
11145 				&kfetch->fetch);
11146 			LOCK_ZONE(zone);
11147 #ifdef ENABLE_AFL
11148 		} else {
11149 			result = ISC_R_FAILURE;
11150 		}
11151 #endif /* ifdef ENABLE_AFL */
11152 		if (result == ISC_R_SUCCESS) {
11153 			fetching = true;
11154 		} else {
11155 			zone->refreshkeycount--;
11156 			isc_refcount_decrement(&zone->irefs);
11157 			dns_db_detach(&kfetch->db);
11158 			dns_rdataset_disassociate(&kfetch->keydataset);
11159 			dns_name_free(kname, zone->mctx);
11160 			isc_mem_put(zone->mctx, kfetch, sizeof(dns_keyfetch_t));
11161 			dnssec_log(zone, ISC_LOG_WARNING,
11162 				   "Failed to create fetch for DNSKEY update");
11163 			fetch_err = true;
11164 		}
11165 	}
11166 	if (!ISC_LIST_EMPTY(diff.tuples)) {
11167 		CHECK(update_soa_serial(zone, db, ver, &diff, zone->mctx,
11168 					zone->updatemethod));
11169 		CHECK(zone_journal(zone, &diff, NULL, "zone_refreshkeys"));
11170 		commit = true;
11171 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED);
11172 		zone_needdump(zone, 30);
11173 	}
11174 
11175 failure:
11176 	if (fetch_err) {
11177 		/*
11178 		 * Error during a key fetch; retry in an hour.
11179 		 */
11180 		isc_time_t timenow, timethen;
11181 		char timebuf[80];
11182 
11183 		TIME_NOW(&timenow);
11184 		DNS_ZONE_TIME_ADD(&timenow, dns_zone_mkey_hour, &timethen);
11185 		zone->refreshkeytime = timethen;
11186 		zone_settimer(zone, &timenow);
11187 
11188 		isc_time_formattimestamp(&zone->refreshkeytime, timebuf, 80);
11189 		dnssec_log(zone, ISC_LOG_DEBUG(1), "retry key refresh: %s",
11190 			   timebuf);
11191 	} else if (!timerset) {
11192 		isc_time_settoepoch(&zone->refreshkeytime);
11193 	}
11194 
11195 	if (!fetching) {
11196 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESHING);
11197 	}
11198 
11199 	dns_diff_clear(&diff);
11200 	if (ver != NULL) {
11201 		dns_rriterator_destroy(&rrit);
11202 		dns_db_closeversion(db, &ver, commit);
11203 	}
11204 	dns_db_detach(&db);
11205 
11206 	UNLOCK_ZONE(zone);
11207 
11208 	INSIST(ver == NULL);
11209 }
11210 
11211 static void
zone_maintenance(dns_zone_t * zone)11212 zone_maintenance(dns_zone_t *zone) {
11213 	const char me[] = "zone_maintenance";
11214 	isc_time_t now;
11215 	isc_result_t result;
11216 	bool dumping, load_pending, viewok;
11217 	bool need_notify;
11218 
11219 	REQUIRE(DNS_ZONE_VALID(zone));
11220 	ENTER;
11221 
11222 	/*
11223 	 * Are we pending load/reload?
11224 	 */
11225 	LOCK_ZONE(zone);
11226 	load_pending = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING);
11227 	UNLOCK_ZONE(zone);
11228 
11229 	if (load_pending) {
11230 		return;
11231 	}
11232 
11233 	/*
11234 	 * Configuring the view of this zone may have
11235 	 * failed, for example because the config file
11236 	 * had a syntax error.	In that case, the view
11237 	 * adb or resolver will be NULL, and we had better not try
11238 	 * to do further maintenance on it.
11239 	 */
11240 	LOCK_ZONE(zone);
11241 	viewok = (zone->view != NULL && zone->view->adb != NULL);
11242 	UNLOCK_ZONE(zone);
11243 	if (!viewok) {
11244 		return;
11245 	}
11246 
11247 	TIME_NOW(&now);
11248 
11249 	/*
11250 	 * Expire check.
11251 	 */
11252 	switch (zone->type) {
11253 	case dns_zone_redirect:
11254 		if (zone->masters == NULL) {
11255 			break;
11256 		}
11257 	/* FALLTHROUGH */
11258 	case dns_zone_secondary:
11259 	case dns_zone_mirror:
11260 	case dns_zone_stub:
11261 		LOCK_ZONE(zone);
11262 		if (isc_time_compare(&now, &zone->expiretime) >= 0 &&
11263 		    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED))
11264 		{
11265 			zone_expire(zone);
11266 			zone->refreshtime = now;
11267 		}
11268 		UNLOCK_ZONE(zone);
11269 		break;
11270 	default:
11271 		break;
11272 	}
11273 
11274 	/*
11275 	 * Up to date check.
11276 	 */
11277 	switch (zone->type) {
11278 	case dns_zone_redirect:
11279 		if (zone->masters == NULL) {
11280 			break;
11281 		}
11282 	/* FALLTHROUGH */
11283 	case dns_zone_secondary:
11284 	case dns_zone_mirror:
11285 	case dns_zone_stub:
11286 		if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH) &&
11287 		    isc_time_compare(&now, &zone->refreshtime) >= 0)
11288 		{
11289 			dns_zone_refresh(zone);
11290 		}
11291 		break;
11292 	default:
11293 		break;
11294 	}
11295 
11296 	/*
11297 	 * Secondaries send notifies before backing up to disk,
11298 	 * primaries after.
11299 	 */
11300 	LOCK_ZONE(zone);
11301 	need_notify = (zone->type == dns_zone_secondary ||
11302 		       zone->type == dns_zone_mirror) &&
11303 		      (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY) ||
11304 		       DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDSTARTUPNOTIFY)) &&
11305 		      (isc_time_compare(&now, &zone->notifytime) >= 0);
11306 	UNLOCK_ZONE(zone);
11307 
11308 	if (need_notify) {
11309 		zone_notify(zone, &now);
11310 	}
11311 
11312 	/*
11313 	 * Do we need to consolidate the backing store?
11314 	 */
11315 	switch (zone->type) {
11316 	case dns_zone_primary:
11317 	case dns_zone_secondary:
11318 	case dns_zone_mirror:
11319 	case dns_zone_key:
11320 	case dns_zone_redirect:
11321 	case dns_zone_stub:
11322 		LOCK_ZONE(zone);
11323 		if (zone->masterfile != NULL &&
11324 		    isc_time_compare(&now, &zone->dumptime) >= 0 &&
11325 		    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) &&
11326 		    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP))
11327 		{
11328 			dumping = was_dumping(zone);
11329 		} else {
11330 			dumping = true;
11331 		}
11332 		UNLOCK_ZONE(zone);
11333 		if (!dumping) {
11334 			result = zone_dump(zone, true); /* task locked */
11335 			if (result != ISC_R_SUCCESS) {
11336 				dns_zone_log(zone, ISC_LOG_WARNING,
11337 					     "dump failed: %s",
11338 					     dns_result_totext(result));
11339 			}
11340 		}
11341 		break;
11342 	default:
11343 		break;
11344 	}
11345 
11346 	/*
11347 	 * Master/redirect zones send notifies now, if needed
11348 	 */
11349 	switch (zone->type) {
11350 	case dns_zone_primary:
11351 	case dns_zone_redirect:
11352 		if ((DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY) ||
11353 		     DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDSTARTUPNOTIFY)) &&
11354 		    isc_time_compare(&now, &zone->notifytime) >= 0)
11355 		{
11356 			zone_notify(zone, &now);
11357 		}
11358 	default:
11359 		break;
11360 	}
11361 
11362 	/*
11363 	 * Do we need to refresh keys?
11364 	 */
11365 	switch (zone->type) {
11366 	case dns_zone_key:
11367 		if (isc_time_compare(&now, &zone->refreshkeytime) >= 0) {
11368 			if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) &&
11369 			    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESHING))
11370 			{
11371 				zone_refreshkeys(zone);
11372 			}
11373 		}
11374 		break;
11375 	case dns_zone_primary:
11376 		if (!isc_time_isepoch(&zone->refreshkeytime) &&
11377 		    isc_time_compare(&now, &zone->refreshkeytime) >= 0 &&
11378 		    zone->rss_event == NULL)
11379 		{
11380 			zone_rekey(zone);
11381 		}
11382 	default:
11383 		break;
11384 	}
11385 
11386 	switch (zone->type) {
11387 	case dns_zone_primary:
11388 	case dns_zone_redirect:
11389 	case dns_zone_secondary:
11390 		/*
11391 		 * Do we need to sign/resign some RRsets?
11392 		 */
11393 		if (zone->rss_event != NULL) {
11394 			break;
11395 		}
11396 		if (!isc_time_isepoch(&zone->signingtime) &&
11397 		    isc_time_compare(&now, &zone->signingtime) >= 0)
11398 		{
11399 			zone_sign(zone);
11400 		} else if (!isc_time_isepoch(&zone->resigntime) &&
11401 			   isc_time_compare(&now, &zone->resigntime) >= 0)
11402 		{
11403 			zone_resigninc(zone);
11404 		} else if (!isc_time_isepoch(&zone->nsec3chaintime) &&
11405 			   isc_time_compare(&now, &zone->nsec3chaintime) >= 0)
11406 		{
11407 			zone_nsec3chain(zone);
11408 		}
11409 		/*
11410 		 * Do we need to issue a key expiry warning?
11411 		 */
11412 		if (!isc_time_isepoch(&zone->keywarntime) &&
11413 		    isc_time_compare(&now, &zone->keywarntime) >= 0)
11414 		{
11415 			set_key_expiry_warning(zone, zone->key_expiry,
11416 					       isc_time_seconds(&now));
11417 		}
11418 		break;
11419 
11420 	default:
11421 		break;
11422 	}
11423 	LOCK_ZONE(zone);
11424 	zone_settimer(zone, &now);
11425 	UNLOCK_ZONE(zone);
11426 }
11427 
11428 void
dns_zone_markdirty(dns_zone_t * zone)11429 dns_zone_markdirty(dns_zone_t *zone) {
11430 	uint32_t serial;
11431 	isc_result_t result = ISC_R_SUCCESS;
11432 	dns_zone_t *secure = NULL;
11433 
11434 	/*
11435 	 * Obtaining a lock on the zone->secure (see zone_send_secureserial)
11436 	 * could result in a deadlock due to a LOR so we will spin if we
11437 	 * can't obtain the both locks.
11438 	 */
11439 again:
11440 	LOCK_ZONE(zone);
11441 	if (zone->type == dns_zone_primary) {
11442 		if (inline_raw(zone)) {
11443 			unsigned int soacount;
11444 			secure = zone->secure;
11445 			INSIST(secure != zone);
11446 			TRYLOCK_ZONE(result, secure);
11447 			if (result != ISC_R_SUCCESS) {
11448 				UNLOCK_ZONE(zone);
11449 				secure = NULL;
11450 				isc_thread_yield();
11451 				goto again;
11452 			}
11453 
11454 			ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
11455 			if (zone->db != NULL) {
11456 				result = zone_get_from_db(
11457 					zone, zone->db, NULL, &soacount, NULL,
11458 					&serial, NULL, NULL, NULL, NULL, NULL);
11459 			} else {
11460 				result = DNS_R_NOTLOADED;
11461 			}
11462 			ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
11463 			if (result == ISC_R_SUCCESS && soacount > 0U) {
11464 				zone_send_secureserial(zone, serial);
11465 			}
11466 		}
11467 
11468 		/* XXXMPA make separate call back */
11469 		if (result == ISC_R_SUCCESS) {
11470 			set_resigntime(zone);
11471 			if (zone->task != NULL) {
11472 				isc_time_t now;
11473 				TIME_NOW(&now);
11474 				zone_settimer(zone, &now);
11475 			}
11476 		}
11477 	}
11478 	if (secure != NULL) {
11479 		UNLOCK_ZONE(secure);
11480 	}
11481 	zone_needdump(zone, DNS_DUMP_DELAY);
11482 	UNLOCK_ZONE(zone);
11483 }
11484 
11485 void
dns_zone_expire(dns_zone_t * zone)11486 dns_zone_expire(dns_zone_t *zone) {
11487 	REQUIRE(DNS_ZONE_VALID(zone));
11488 
11489 	LOCK_ZONE(zone);
11490 	zone_expire(zone);
11491 	UNLOCK_ZONE(zone);
11492 }
11493 
11494 static void
zone_expire(dns_zone_t * zone)11495 zone_expire(dns_zone_t *zone) {
11496 	dns_db_t *db = NULL;
11497 
11498 	/*
11499 	 * 'zone' locked by caller.
11500 	 */
11501 
11502 	REQUIRE(LOCKED_ZONE(zone));
11503 
11504 	dns_zone_log(zone, ISC_LOG_WARNING, "expired");
11505 
11506 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_EXPIRED);
11507 	zone->refresh = DNS_ZONE_DEFAULTREFRESH;
11508 	zone->retry = DNS_ZONE_DEFAULTRETRY;
11509 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
11510 
11511 	/*
11512 	 * An RPZ zone has expired; before unloading it, we must
11513 	 * first remove it from the RPZ summary database. The
11514 	 * easiest way to do this is "update" it with an empty
11515 	 * database so that the update callback synchronizes
11516 	 * the diff automatically.
11517 	 */
11518 	if (zone->rpzs != NULL && zone->rpz_num != DNS_RPZ_INVALID_NUM) {
11519 		isc_result_t result;
11520 		dns_rpz_zone_t *rpz = zone->rpzs->zones[zone->rpz_num];
11521 
11522 		CHECK(dns_db_create(zone->mctx, "rbt", &zone->origin,
11523 				    dns_dbtype_zone, zone->rdclass, 0, NULL,
11524 				    &db));
11525 		CHECK(dns_rpz_dbupdate_callback(db, rpz));
11526 		dns_zone_log(zone, ISC_LOG_WARNING,
11527 			     "response-policy zone expired; "
11528 			     "policies unloaded");
11529 	}
11530 
11531 failure:
11532 	if (db != NULL) {
11533 		dns_db_detach(&db);
11534 	}
11535 
11536 	zone_unload(zone);
11537 }
11538 
11539 void
dns_zone_refresh(dns_zone_t * zone)11540 dns_zone_refresh(dns_zone_t *zone) {
11541 	isc_interval_t i;
11542 	uint32_t oldflags;
11543 	unsigned int j;
11544 	isc_result_t result;
11545 
11546 	REQUIRE(DNS_ZONE_VALID(zone));
11547 
11548 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
11549 		return;
11550 	}
11551 
11552 	/*
11553 	 * Set DNS_ZONEFLG_REFRESH so that there is only one refresh operation
11554 	 * in progress at a time.
11555 	 */
11556 
11557 	LOCK_ZONE(zone);
11558 	oldflags = atomic_load(&zone->flags);
11559 	if (zone->masterscnt == 0) {
11560 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOMASTERS);
11561 		if ((oldflags & DNS_ZONEFLG_NOMASTERS) == 0) {
11562 			dns_zone_log(zone, ISC_LOG_ERROR,
11563 				     "cannot refresh: no primaries");
11564 		}
11565 		goto unlock;
11566 	}
11567 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESH);
11568 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOEDNS);
11569 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_USEALTXFRSRC);
11570 	if ((oldflags & (DNS_ZONEFLG_REFRESH | DNS_ZONEFLG_LOADING)) != 0) {
11571 		goto unlock;
11572 	}
11573 
11574 	/*
11575 	 * Set the next refresh time as if refresh check has failed.
11576 	 * Setting this to the retry time will do that.  XXXMLG
11577 	 * If we are successful it will be reset using zone->refresh.
11578 	 */
11579 	isc_interval_set(&i, zone->retry - isc_random_uniform(zone->retry / 4),
11580 			 0);
11581 	result = isc_time_nowplusinterval(&zone->refreshtime, &i);
11582 	if (result != ISC_R_SUCCESS) {
11583 		dns_zone_log(zone, ISC_LOG_WARNING,
11584 			     "isc_time_nowplusinterval() failed: %s",
11585 			     dns_result_totext(result));
11586 	}
11587 
11588 	/*
11589 	 * When lacking user-specified timer values from the SOA,
11590 	 * do exponential backoff of the retry time up to a
11591 	 * maximum of six hours.
11592 	 */
11593 	if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HAVETIMERS)) {
11594 		zone->retry = ISC_MIN(zone->retry * 2, 6 * 3600);
11595 	}
11596 
11597 	zone->curmaster = 0;
11598 	for (j = 0; j < zone->masterscnt; j++) {
11599 		zone->mastersok[j] = false;
11600 	}
11601 	/* initiate soa query */
11602 	queue_soa_query(zone);
11603 unlock:
11604 	UNLOCK_ZONE(zone);
11605 }
11606 
11607 static isc_result_t
zone_journal_rollforward(dns_zone_t * zone,dns_db_t * db,bool * needdump,bool * fixjournal)11608 zone_journal_rollforward(dns_zone_t *zone, dns_db_t *db, bool *needdump,
11609 			 bool *fixjournal) {
11610 	dns_journal_t *journal = NULL;
11611 	unsigned int options;
11612 	isc_result_t result;
11613 
11614 	if (zone->type == dns_zone_primary &&
11615 	    (inline_secure(zone) ||
11616 	     (zone->update_acl != NULL || zone->ssutable != NULL)))
11617 	{
11618 		options = DNS_JOURNALOPT_RESIGN;
11619 	} else {
11620 		options = 0;
11621 	}
11622 
11623 	result = dns_journal_open(zone->mctx, zone->journal, DNS_JOURNAL_READ,
11624 				  &journal);
11625 	if (result == ISC_R_NOTFOUND) {
11626 		dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(3),
11627 			      "no journal file, but that's OK ");
11628 		return (ISC_R_SUCCESS);
11629 	} else if (result != ISC_R_SUCCESS) {
11630 		dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR,
11631 			      "journal open failed: %s",
11632 			      dns_result_totext(result));
11633 		return (result);
11634 	}
11635 
11636 	if (dns_journal_empty(journal)) {
11637 		dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(1),
11638 			      "journal empty");
11639 		dns_journal_destroy(&journal);
11640 		return (ISC_R_SUCCESS);
11641 	}
11642 
11643 	result = dns_journal_rollforward(journal, db, options);
11644 	switch (result) {
11645 	case ISC_R_SUCCESS:
11646 		*needdump = true;
11647 		/* FALLTHROUGH */
11648 	case DNS_R_UPTODATE:
11649 		if (dns_journal_recovered(journal)) {
11650 			*fixjournal = true;
11651 			dns_zone_logc(
11652 				zone, DNS_LOGCATEGORY_ZONELOAD,
11653 				ISC_LOG_DEBUG(1),
11654 				"journal rollforward completed successfully "
11655 				"using old journal format: %s",
11656 				dns_result_totext(result));
11657 		} else {
11658 			dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD,
11659 				      ISC_LOG_DEBUG(1),
11660 				      "journal rollforward completed "
11661 				      "successfully: %s",
11662 				      dns_result_totext(result));
11663 		}
11664 
11665 		dns_journal_destroy(&journal);
11666 		return (ISC_R_SUCCESS);
11667 	case ISC_R_NOTFOUND:
11668 	case ISC_R_RANGE:
11669 		dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR,
11670 			      "journal rollforward failed: journal out of sync "
11671 			      "with zone");
11672 		dns_journal_destroy(&journal);
11673 		return (result);
11674 	default:
11675 		dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR,
11676 			      "journal rollforward failed: %s",
11677 			      dns_result_totext(result));
11678 		dns_journal_destroy(&journal);
11679 		return (result);
11680 	}
11681 }
11682 
11683 static void
zone_journal_compact(dns_zone_t * zone,dns_db_t * db,uint32_t serial)11684 zone_journal_compact(dns_zone_t *zone, dns_db_t *db, uint32_t serial) {
11685 	isc_result_t result;
11686 	int32_t journalsize;
11687 	dns_dbversion_t *ver = NULL;
11688 	uint64_t dbsize;
11689 	uint32_t options = 0;
11690 
11691 	INSIST(LOCKED_ZONE(zone));
11692 	if (inline_raw(zone)) {
11693 		INSIST(LOCKED_ZONE(zone->secure));
11694 	}
11695 
11696 	journalsize = zone->journalsize;
11697 	if (journalsize == -1) {
11698 		journalsize = DNS_JOURNAL_SIZE_MAX;
11699 		dns_db_currentversion(db, &ver);
11700 		result = dns_db_getsize(db, ver, NULL, &dbsize);
11701 		dns_db_closeversion(db, &ver, false);
11702 		if (result != ISC_R_SUCCESS) {
11703 			dns_zone_log(zone, ISC_LOG_ERROR,
11704 				     "zone_journal_compact: "
11705 				     "could not get zone size: %s",
11706 				     isc_result_totext(result));
11707 		} else if (dbsize < DNS_JOURNAL_SIZE_MAX / 2) {
11708 			journalsize = (int32_t)dbsize * 2;
11709 		}
11710 	}
11711 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FIXJOURNAL)) {
11712 		options |= DNS_JOURNAL_COMPACTALL;
11713 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FIXJOURNAL);
11714 		zone_debuglog(zone, "zone_journal_compact", 1,
11715 			      "repair full journal");
11716 	} else {
11717 		zone_debuglog(zone, "zone_journal_compact", 1,
11718 			      "target journal size %d", journalsize);
11719 	}
11720 	result = dns_journal_compact(zone->mctx, zone->journal, serial, options,
11721 				     journalsize);
11722 	switch (result) {
11723 	case ISC_R_SUCCESS:
11724 	case ISC_R_NOSPACE:
11725 	case ISC_R_NOTFOUND:
11726 		dns_zone_log(zone, ISC_LOG_DEBUG(3), "dns_journal_compact: %s",
11727 			     dns_result_totext(result));
11728 		break;
11729 	default:
11730 		dns_zone_log(zone, ISC_LOG_ERROR,
11731 			     "dns_journal_compact failed: %s",
11732 			     dns_result_totext(result));
11733 		break;
11734 	}
11735 }
11736 
11737 isc_result_t
dns_zone_flush(dns_zone_t * zone)11738 dns_zone_flush(dns_zone_t *zone) {
11739 	isc_result_t result = ISC_R_SUCCESS;
11740 	bool dumping;
11741 
11742 	REQUIRE(DNS_ZONE_VALID(zone));
11743 
11744 	LOCK_ZONE(zone);
11745 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FLUSH);
11746 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
11747 	    zone->masterfile != NULL) {
11748 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDCOMPACT);
11749 		result = ISC_R_ALREADYRUNNING;
11750 		dumping = was_dumping(zone);
11751 	} else {
11752 		dumping = true;
11753 	}
11754 	UNLOCK_ZONE(zone);
11755 	if (!dumping) {
11756 		result = zone_dump(zone, true); /* Unknown task. */
11757 	}
11758 	return (result);
11759 }
11760 
11761 isc_result_t
dns_zone_dump(dns_zone_t * zone)11762 dns_zone_dump(dns_zone_t *zone) {
11763 	isc_result_t result = ISC_R_ALREADYRUNNING;
11764 	bool dumping;
11765 
11766 	REQUIRE(DNS_ZONE_VALID(zone));
11767 
11768 	LOCK_ZONE(zone);
11769 	dumping = was_dumping(zone);
11770 	UNLOCK_ZONE(zone);
11771 	if (!dumping) {
11772 		result = zone_dump(zone, false); /* Unknown task. */
11773 	}
11774 	return (result);
11775 }
11776 
11777 static void
zone_needdump(dns_zone_t * zone,unsigned int delay)11778 zone_needdump(dns_zone_t *zone, unsigned int delay) {
11779 	const char me[] = "zone_needdump";
11780 	isc_time_t dumptime;
11781 	isc_time_t now;
11782 
11783 	/*
11784 	 * 'zone' locked by caller
11785 	 */
11786 
11787 	REQUIRE(DNS_ZONE_VALID(zone));
11788 	REQUIRE(LOCKED_ZONE(zone));
11789 	ENTER;
11790 
11791 	/*
11792 	 * Do we have a place to dump to and are we loaded?
11793 	 */
11794 	if (zone->masterfile == NULL ||
11795 	    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) == 0) {
11796 		return;
11797 	}
11798 
11799 	TIME_NOW(&now);
11800 	/* add some noise */
11801 	DNS_ZONE_JITTER_ADD(&now, delay, &dumptime);
11802 
11803 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
11804 	if (isc_time_isepoch(&zone->dumptime) ||
11805 	    isc_time_compare(&zone->dumptime, &dumptime) > 0)
11806 	{
11807 		zone->dumptime = dumptime;
11808 	}
11809 	if (zone->task != NULL) {
11810 		zone_settimer(zone, &now);
11811 	}
11812 }
11813 
11814 static void
dump_done(void * arg,isc_result_t result)11815 dump_done(void *arg, isc_result_t result) {
11816 	const char me[] = "dump_done";
11817 	dns_zone_t *zone = arg;
11818 	dns_zone_t *secure = NULL;
11819 	dns_db_t *db;
11820 	dns_dbversion_t *version;
11821 	bool again = false;
11822 	bool compact = false;
11823 	uint32_t serial;
11824 	isc_result_t tresult;
11825 
11826 	REQUIRE(DNS_ZONE_VALID(zone));
11827 
11828 	ENTER;
11829 
11830 	if (result == ISC_R_SUCCESS && zone->journal != NULL) {
11831 		/*
11832 		 * We don't own these, zone->dctx must stay valid.
11833 		 */
11834 		db = dns_dumpctx_db(zone->dctx);
11835 		version = dns_dumpctx_version(zone->dctx);
11836 		tresult = dns_db_getsoaserial(db, version, &serial);
11837 
11838 		/*
11839 		 * Handle lock order inversion.
11840 		 */
11841 	again:
11842 		LOCK_ZONE(zone);
11843 		if (inline_raw(zone)) {
11844 			secure = zone->secure;
11845 			INSIST(secure != zone);
11846 			TRYLOCK_ZONE(result, secure);
11847 			if (result != ISC_R_SUCCESS) {
11848 				UNLOCK_ZONE(zone);
11849 				secure = NULL;
11850 				isc_thread_yield();
11851 				goto again;
11852 			}
11853 		}
11854 
11855 		/*
11856 		 * If there is a secure version of this zone
11857 		 * use its serial if it is less than ours.
11858 		 */
11859 		if (tresult == ISC_R_SUCCESS && secure != NULL) {
11860 			uint32_t sserial;
11861 			isc_result_t mresult;
11862 
11863 			ZONEDB_LOCK(&secure->dblock, isc_rwlocktype_read);
11864 			if (secure->db != NULL) {
11865 				mresult = dns_db_getsoaserial(zone->secure->db,
11866 							      NULL, &sserial);
11867 				if (mresult == ISC_R_SUCCESS &&
11868 				    isc_serial_lt(sserial, serial)) {
11869 					serial = sserial;
11870 				}
11871 			}
11872 			ZONEDB_UNLOCK(&secure->dblock, isc_rwlocktype_read);
11873 		}
11874 		if (tresult == ISC_R_SUCCESS && zone->xfr == NULL) {
11875 			dns_db_t *zdb = NULL;
11876 			if (dns_zone_getdb(zone, &zdb) == ISC_R_SUCCESS) {
11877 				zone_journal_compact(zone, zdb, serial);
11878 				dns_db_detach(&zdb);
11879 			}
11880 		} else if (tresult == ISC_R_SUCCESS) {
11881 			compact = true;
11882 			zone->compact_serial = serial;
11883 		}
11884 		if (secure != NULL) {
11885 			UNLOCK_ZONE(secure);
11886 		}
11887 		UNLOCK_ZONE(zone);
11888 	}
11889 
11890 	LOCK_ZONE(zone);
11891 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_DUMPING);
11892 	if (compact) {
11893 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDCOMPACT);
11894 	}
11895 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SHUTDOWN)) {
11896 		/*
11897 		 * If DNS_ZONEFLG_SHUTDOWN is set, all external references to
11898 		 * the zone are gone, which means it is in the process of being
11899 		 * cleaned up, so do not reschedule dumping.
11900 		 *
11901 		 * Detach from the raw version of the zone in case this
11902 		 * operation has been deferred in zone_shutdown().
11903 		 */
11904 		if (zone->raw != NULL) {
11905 			dns_zone_detach(&zone->raw);
11906 		}
11907 		if (result == ISC_R_SUCCESS) {
11908 			DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FLUSH);
11909 		}
11910 	} else if (result != ISC_R_SUCCESS && result != ISC_R_CANCELED) {
11911 		/*
11912 		 * Try again in a short while.
11913 		 */
11914 		zone_needdump(zone, DNS_DUMP_DELAY);
11915 	} else if (result == ISC_R_SUCCESS &&
11916 		   DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) &&
11917 		   DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
11918 		   DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED))
11919 	{
11920 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
11921 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DUMPING);
11922 		isc_time_settoepoch(&zone->dumptime);
11923 		again = true;
11924 	} else if (result == ISC_R_SUCCESS) {
11925 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FLUSH);
11926 	}
11927 
11928 	if (zone->dctx != NULL) {
11929 		dns_dumpctx_detach(&zone->dctx);
11930 	}
11931 	zonemgr_putio(&zone->writeio);
11932 	UNLOCK_ZONE(zone);
11933 	if (again) {
11934 		(void)zone_dump(zone, false);
11935 	}
11936 	dns_zone_idetach(&zone);
11937 }
11938 
11939 static isc_result_t
zone_dump(dns_zone_t * zone,bool compact)11940 zone_dump(dns_zone_t *zone, bool compact) {
11941 	const char me[] = "zone_dump";
11942 	isc_result_t result;
11943 	dns_dbversion_t *version = NULL;
11944 	bool again;
11945 	dns_db_t *db = NULL;
11946 	char *masterfile = NULL;
11947 	dns_masterformat_t masterformat = dns_masterformat_none;
11948 
11949 	/*
11950 	 * 'compact' MUST only be set if we are task locked.
11951 	 */
11952 
11953 	REQUIRE(DNS_ZONE_VALID(zone));
11954 	ENTER;
11955 
11956 redo:
11957 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
11958 	if (zone->db != NULL) {
11959 		dns_db_attach(zone->db, &db);
11960 	}
11961 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
11962 	LOCK_ZONE(zone);
11963 	if (zone->masterfile != NULL) {
11964 		masterfile = isc_mem_strdup(zone->mctx, zone->masterfile);
11965 		masterformat = zone->masterformat;
11966 	}
11967 	UNLOCK_ZONE(zone);
11968 	if (db == NULL) {
11969 		result = DNS_R_NOTLOADED;
11970 		goto fail;
11971 	}
11972 	if (masterfile == NULL) {
11973 		result = DNS_R_NOMASTERFILE;
11974 		goto fail;
11975 	}
11976 
11977 	if (compact && zone->type != dns_zone_stub) {
11978 		dns_zone_t *dummy = NULL;
11979 		LOCK_ZONE(zone);
11980 		zone_iattach(zone, &dummy);
11981 		result = zonemgr_getio(zone->zmgr, false, zone->task,
11982 				       zone_gotwritehandle, zone,
11983 				       &zone->writeio);
11984 		if (result != ISC_R_SUCCESS) {
11985 			zone_idetach(&dummy);
11986 		} else {
11987 			result = DNS_R_CONTINUE;
11988 		}
11989 		UNLOCK_ZONE(zone);
11990 	} else {
11991 		const dns_master_style_t *output_style;
11992 
11993 		dns_masterrawheader_t rawdata;
11994 		dns_db_currentversion(db, &version);
11995 		dns_master_initrawheader(&rawdata);
11996 		if (inline_secure(zone)) {
11997 			get_raw_serial(zone->raw, &rawdata);
11998 		}
11999 		if (zone->type == dns_zone_key) {
12000 			output_style = &dns_master_style_keyzone;
12001 		} else {
12002 			output_style = &dns_master_style_default;
12003 		}
12004 		result = dns_master_dump(zone->mctx, db, version, output_style,
12005 					 masterfile, masterformat, &rawdata);
12006 		dns_db_closeversion(db, &version, false);
12007 	}
12008 fail:
12009 	if (db != NULL) {
12010 		dns_db_detach(&db);
12011 	}
12012 	if (masterfile != NULL) {
12013 		isc_mem_free(zone->mctx, masterfile);
12014 	}
12015 	masterfile = NULL;
12016 
12017 	if (result == DNS_R_CONTINUE) {
12018 		return (ISC_R_SUCCESS); /* XXXMPA */
12019 	}
12020 
12021 	again = false;
12022 	LOCK_ZONE(zone);
12023 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_DUMPING);
12024 	if (result != ISC_R_SUCCESS) {
12025 		/*
12026 		 * Try again in a short while.
12027 		 */
12028 		zone_needdump(zone, DNS_DUMP_DELAY);
12029 	} else if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) &&
12030 		   DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
12031 		   DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED))
12032 	{
12033 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
12034 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DUMPING);
12035 		isc_time_settoepoch(&zone->dumptime);
12036 		again = true;
12037 	} else {
12038 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FLUSH);
12039 	}
12040 	UNLOCK_ZONE(zone);
12041 	if (again) {
12042 		goto redo;
12043 	}
12044 
12045 	return (result);
12046 }
12047 
12048 static isc_result_t
dumptostream(dns_zone_t * zone,FILE * fd,const dns_master_style_t * style,dns_masterformat_t format,const uint32_t rawversion)12049 dumptostream(dns_zone_t *zone, FILE *fd, const dns_master_style_t *style,
12050 	     dns_masterformat_t format, const uint32_t rawversion) {
12051 	isc_result_t result;
12052 	dns_dbversion_t *version = NULL;
12053 	dns_db_t *db = NULL;
12054 	dns_masterrawheader_t rawdata;
12055 
12056 	REQUIRE(DNS_ZONE_VALID(zone));
12057 
12058 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
12059 	if (zone->db != NULL) {
12060 		dns_db_attach(zone->db, &db);
12061 	}
12062 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
12063 	if (db == NULL) {
12064 		return (DNS_R_NOTLOADED);
12065 	}
12066 
12067 	dns_db_currentversion(db, &version);
12068 	dns_master_initrawheader(&rawdata);
12069 	if (rawversion == 0) {
12070 		rawdata.flags |= DNS_MASTERRAW_COMPAT;
12071 	} else if (inline_secure(zone)) {
12072 		get_raw_serial(zone->raw, &rawdata);
12073 	} else if (zone->sourceserialset) {
12074 		rawdata.flags = DNS_MASTERRAW_SOURCESERIALSET;
12075 		rawdata.sourceserial = zone->sourceserial;
12076 	}
12077 	result = dns_master_dumptostream(zone->mctx, db, version, style, format,
12078 					 &rawdata, fd);
12079 	dns_db_closeversion(db, &version, false);
12080 	dns_db_detach(&db);
12081 	return (result);
12082 }
12083 
12084 isc_result_t
dns_zone_dumptostream(dns_zone_t * zone,FILE * fd,dns_masterformat_t format,const dns_master_style_t * style,const uint32_t rawversion)12085 dns_zone_dumptostream(dns_zone_t *zone, FILE *fd, dns_masterformat_t format,
12086 		      const dns_master_style_t *style,
12087 		      const uint32_t rawversion) {
12088 	return (dumptostream(zone, fd, style, format, rawversion));
12089 }
12090 
12091 void
dns_zone_unload(dns_zone_t * zone)12092 dns_zone_unload(dns_zone_t *zone) {
12093 	REQUIRE(DNS_ZONE_VALID(zone));
12094 
12095 	LOCK_ZONE(zone);
12096 	zone_unload(zone);
12097 	UNLOCK_ZONE(zone);
12098 }
12099 
12100 static void
notify_cancel(dns_zone_t * zone)12101 notify_cancel(dns_zone_t *zone) {
12102 	dns_notify_t *notify;
12103 
12104 	/*
12105 	 * 'zone' locked by caller.
12106 	 */
12107 
12108 	REQUIRE(LOCKED_ZONE(zone));
12109 
12110 	for (notify = ISC_LIST_HEAD(zone->notifies); notify != NULL;
12111 	     notify = ISC_LIST_NEXT(notify, link))
12112 	{
12113 		if (notify->find != NULL) {
12114 			dns_adb_cancelfind(notify->find);
12115 		}
12116 		if (notify->request != NULL) {
12117 			dns_request_cancel(notify->request);
12118 		}
12119 	}
12120 }
12121 
12122 static void
checkds_cancel(dns_zone_t * zone)12123 checkds_cancel(dns_zone_t *zone) {
12124 	dns_checkds_t *checkds;
12125 
12126 	/*
12127 	 * 'zone' locked by caller.
12128 	 */
12129 
12130 	REQUIRE(LOCKED_ZONE(zone));
12131 
12132 	for (checkds = ISC_LIST_HEAD(zone->checkds_requests); checkds != NULL;
12133 	     checkds = ISC_LIST_NEXT(checkds, link))
12134 	{
12135 		if (checkds->request != NULL) {
12136 			dns_request_cancel(checkds->request);
12137 		}
12138 	}
12139 }
12140 
12141 static void
forward_cancel(dns_zone_t * zone)12142 forward_cancel(dns_zone_t *zone) {
12143 	dns_forward_t *forward;
12144 
12145 	/*
12146 	 * 'zone' locked by caller.
12147 	 */
12148 
12149 	REQUIRE(LOCKED_ZONE(zone));
12150 
12151 	for (forward = ISC_LIST_HEAD(zone->forwards); forward != NULL;
12152 	     forward = ISC_LIST_NEXT(forward, link))
12153 	{
12154 		if (forward->request != NULL) {
12155 			dns_request_cancel(forward->request);
12156 		}
12157 	}
12158 }
12159 
12160 static void
zone_unload(dns_zone_t * zone)12161 zone_unload(dns_zone_t *zone) {
12162 	/*
12163 	 * 'zone' locked by caller.
12164 	 */
12165 
12166 	REQUIRE(LOCKED_ZONE(zone));
12167 
12168 	if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) ||
12169 	    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING))
12170 	{
12171 		if (zone->writeio != NULL) {
12172 			zonemgr_cancelio(zone->writeio);
12173 		}
12174 
12175 		if (zone->dctx != NULL) {
12176 			dns_dumpctx_cancel(zone->dctx);
12177 		}
12178 	}
12179 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write);
12180 	zone_detachdb(zone);
12181 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write);
12182 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADED);
12183 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP);
12184 
12185 	if (zone->type == dns_zone_mirror) {
12186 		dns_zone_log(zone, ISC_LOG_INFO,
12187 			     "mirror zone is no longer in use; "
12188 			     "reverting to normal recursion");
12189 	}
12190 }
12191 
12192 void
dns_zone_setminrefreshtime(dns_zone_t * zone,uint32_t val)12193 dns_zone_setminrefreshtime(dns_zone_t *zone, uint32_t val) {
12194 	REQUIRE(DNS_ZONE_VALID(zone));
12195 	REQUIRE(val > 0);
12196 
12197 	zone->minrefresh = val;
12198 }
12199 
12200 void
dns_zone_setmaxrefreshtime(dns_zone_t * zone,uint32_t val)12201 dns_zone_setmaxrefreshtime(dns_zone_t *zone, uint32_t val) {
12202 	REQUIRE(DNS_ZONE_VALID(zone));
12203 	REQUIRE(val > 0);
12204 
12205 	zone->maxrefresh = val;
12206 }
12207 
12208 void
dns_zone_setminretrytime(dns_zone_t * zone,uint32_t val)12209 dns_zone_setminretrytime(dns_zone_t *zone, uint32_t val) {
12210 	REQUIRE(DNS_ZONE_VALID(zone));
12211 	REQUIRE(val > 0);
12212 
12213 	zone->minretry = val;
12214 }
12215 
12216 void
dns_zone_setmaxretrytime(dns_zone_t * zone,uint32_t val)12217 dns_zone_setmaxretrytime(dns_zone_t *zone, uint32_t val) {
12218 	REQUIRE(DNS_ZONE_VALID(zone));
12219 	REQUIRE(val > 0);
12220 
12221 	zone->maxretry = val;
12222 }
12223 
12224 uint32_t
dns_zone_getmaxrecords(dns_zone_t * zone)12225 dns_zone_getmaxrecords(dns_zone_t *zone) {
12226 	REQUIRE(DNS_ZONE_VALID(zone));
12227 
12228 	return (zone->maxrecords);
12229 }
12230 
12231 void
dns_zone_setmaxrecords(dns_zone_t * zone,uint32_t val)12232 dns_zone_setmaxrecords(dns_zone_t *zone, uint32_t val) {
12233 	REQUIRE(DNS_ZONE_VALID(zone));
12234 
12235 	zone->maxrecords = val;
12236 }
12237 
12238 static bool
notify_isqueued(dns_zone_t * zone,unsigned int flags,dns_name_t * name,isc_sockaddr_t * addr,dns_tsigkey_t * key)12239 notify_isqueued(dns_zone_t *zone, unsigned int flags, dns_name_t *name,
12240 		isc_sockaddr_t *addr, dns_tsigkey_t *key) {
12241 	dns_notify_t *notify;
12242 	dns_zonemgr_t *zmgr;
12243 	isc_result_t result;
12244 
12245 	for (notify = ISC_LIST_HEAD(zone->notifies); notify != NULL;
12246 	     notify = ISC_LIST_NEXT(notify, link))
12247 	{
12248 		if (notify->request != NULL) {
12249 			continue;
12250 		}
12251 		if (name != NULL && dns_name_dynamic(&notify->ns) &&
12252 		    dns_name_equal(name, &notify->ns))
12253 		{
12254 			goto requeue;
12255 		}
12256 		if (addr != NULL && isc_sockaddr_equal(addr, &notify->dst) &&
12257 		    notify->key == key)
12258 		{
12259 			goto requeue;
12260 		}
12261 	}
12262 	return (false);
12263 
12264 requeue:
12265 	/*
12266 	 * If we are enqueued on the startup ratelimiter and this is
12267 	 * not a startup notify, re-enqueue on the normal notify
12268 	 * ratelimiter.
12269 	 */
12270 	if (notify->event != NULL && (flags & DNS_NOTIFY_STARTUP) == 0 &&
12271 	    (notify->flags & DNS_NOTIFY_STARTUP) != 0)
12272 	{
12273 		zmgr = notify->zone->zmgr;
12274 		result = isc_ratelimiter_dequeue(zmgr->startupnotifyrl,
12275 						 notify->event);
12276 		if (result != ISC_R_SUCCESS) {
12277 			return (true);
12278 		}
12279 
12280 		notify->flags &= ~DNS_NOTIFY_STARTUP;
12281 		result = isc_ratelimiter_enqueue(notify->zone->zmgr->notifyrl,
12282 						 notify->zone->task,
12283 						 &notify->event);
12284 		if (result != ISC_R_SUCCESS) {
12285 			isc_event_free(&notify->event);
12286 			return (false);
12287 		}
12288 	}
12289 
12290 	return (true);
12291 }
12292 
12293 static bool
notify_isself(dns_zone_t * zone,isc_sockaddr_t * dst)12294 notify_isself(dns_zone_t *zone, isc_sockaddr_t *dst) {
12295 	dns_tsigkey_t *key = NULL;
12296 	isc_sockaddr_t src;
12297 	isc_sockaddr_t any;
12298 	bool isself;
12299 	isc_netaddr_t dstaddr;
12300 	isc_result_t result;
12301 
12302 	if (zone->view == NULL || zone->isself == NULL) {
12303 		return (false);
12304 	}
12305 
12306 	switch (isc_sockaddr_pf(dst)) {
12307 	case PF_INET:
12308 		src = zone->notifysrc4;
12309 		isc_sockaddr_any(&any);
12310 		break;
12311 	case PF_INET6:
12312 		src = zone->notifysrc6;
12313 		isc_sockaddr_any6(&any);
12314 		break;
12315 	default:
12316 		return (false);
12317 	}
12318 
12319 	/*
12320 	 * When sending from any the kernel will assign a source address
12321 	 * that matches the destination address.
12322 	 */
12323 	if (isc_sockaddr_eqaddr(&any, &src)) {
12324 		src = *dst;
12325 	}
12326 
12327 	isc_netaddr_fromsockaddr(&dstaddr, dst);
12328 	result = dns_view_getpeertsig(zone->view, &dstaddr, &key);
12329 	if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
12330 		return (false);
12331 	}
12332 	isself = (zone->isself)(zone->view, key, &src, dst, zone->rdclass,
12333 				zone->isselfarg);
12334 	if (key != NULL) {
12335 		dns_tsigkey_detach(&key);
12336 	}
12337 	return (isself);
12338 }
12339 
12340 static void
notify_destroy(dns_notify_t * notify,bool locked)12341 notify_destroy(dns_notify_t *notify, bool locked) {
12342 	isc_mem_t *mctx;
12343 
12344 	REQUIRE(DNS_NOTIFY_VALID(notify));
12345 
12346 	if (notify->zone != NULL) {
12347 		if (!locked) {
12348 			LOCK_ZONE(notify->zone);
12349 		}
12350 		REQUIRE(LOCKED_ZONE(notify->zone));
12351 		if (ISC_LINK_LINKED(notify, link)) {
12352 			ISC_LIST_UNLINK(notify->zone->notifies, notify, link);
12353 		}
12354 		if (!locked) {
12355 			UNLOCK_ZONE(notify->zone);
12356 		}
12357 		if (locked) {
12358 			zone_idetach(&notify->zone);
12359 		} else {
12360 			dns_zone_idetach(&notify->zone);
12361 		}
12362 	}
12363 	if (notify->find != NULL) {
12364 		dns_adb_destroyfind(&notify->find);
12365 	}
12366 	if (notify->request != NULL) {
12367 		dns_request_destroy(&notify->request);
12368 	}
12369 	if (dns_name_dynamic(&notify->ns)) {
12370 		dns_name_free(&notify->ns, notify->mctx);
12371 	}
12372 	if (notify->key != NULL) {
12373 		dns_tsigkey_detach(&notify->key);
12374 	}
12375 	mctx = notify->mctx;
12376 	isc_mem_put(notify->mctx, notify, sizeof(*notify));
12377 	isc_mem_detach(&mctx);
12378 }
12379 
12380 static isc_result_t
notify_create(isc_mem_t * mctx,unsigned int flags,dns_notify_t ** notifyp)12381 notify_create(isc_mem_t *mctx, unsigned int flags, dns_notify_t **notifyp) {
12382 	dns_notify_t *notify;
12383 
12384 	REQUIRE(notifyp != NULL && *notifyp == NULL);
12385 
12386 	notify = isc_mem_get(mctx, sizeof(*notify));
12387 
12388 	notify->mctx = NULL;
12389 	isc_mem_attach(mctx, &notify->mctx);
12390 	notify->flags = flags;
12391 	notify->zone = NULL;
12392 	notify->find = NULL;
12393 	notify->request = NULL;
12394 	notify->key = NULL;
12395 	notify->event = NULL;
12396 	isc_sockaddr_any(&notify->dst);
12397 	dns_name_init(&notify->ns, NULL);
12398 	ISC_LINK_INIT(notify, link);
12399 	notify->magic = NOTIFY_MAGIC;
12400 	*notifyp = notify;
12401 	return (ISC_R_SUCCESS);
12402 }
12403 
12404 /*
12405  * XXXAG should check for DNS_ZONEFLG_EXITING
12406  */
12407 static void
process_adb_event(isc_task_t * task,isc_event_t * ev)12408 process_adb_event(isc_task_t *task, isc_event_t *ev) {
12409 	dns_notify_t *notify;
12410 	isc_eventtype_t result;
12411 
12412 	UNUSED(task);
12413 
12414 	notify = ev->ev_arg;
12415 	REQUIRE(DNS_NOTIFY_VALID(notify));
12416 	INSIST(task == notify->zone->task);
12417 	result = ev->ev_type;
12418 	isc_event_free(&ev);
12419 	if (result == DNS_EVENT_ADBMOREADDRESSES) {
12420 		dns_adb_destroyfind(&notify->find);
12421 		notify_find_address(notify);
12422 		return;
12423 	}
12424 	if (result == DNS_EVENT_ADBNOMOREADDRESSES) {
12425 		LOCK_ZONE(notify->zone);
12426 		notify_send(notify);
12427 		UNLOCK_ZONE(notify->zone);
12428 	}
12429 	notify_destroy(notify, false);
12430 }
12431 
12432 static void
notify_find_address(dns_notify_t * notify)12433 notify_find_address(dns_notify_t *notify) {
12434 	isc_result_t result;
12435 	unsigned int options;
12436 
12437 	REQUIRE(DNS_NOTIFY_VALID(notify));
12438 	options = DNS_ADBFIND_WANTEVENT | DNS_ADBFIND_INET | DNS_ADBFIND_INET6 |
12439 		  DNS_ADBFIND_RETURNLAME;
12440 
12441 	if (notify->zone->view->adb == NULL) {
12442 		goto destroy;
12443 	}
12444 
12445 	result = dns_adb_createfind(
12446 		notify->zone->view->adb, notify->zone->task, process_adb_event,
12447 		notify, &notify->ns, dns_rootname, 0, options, 0, NULL,
12448 		notify->zone->view->dstport, 0, NULL, &notify->find);
12449 
12450 	/* Something failed? */
12451 	if (result != ISC_R_SUCCESS) {
12452 		goto destroy;
12453 	}
12454 
12455 	/* More addresses pending? */
12456 	if ((notify->find->options & DNS_ADBFIND_WANTEVENT) != 0) {
12457 		return;
12458 	}
12459 
12460 	/* We have as many addresses as we can get. */
12461 	LOCK_ZONE(notify->zone);
12462 	notify_send(notify);
12463 	UNLOCK_ZONE(notify->zone);
12464 
12465 destroy:
12466 	notify_destroy(notify, false);
12467 }
12468 
12469 static isc_result_t
notify_send_queue(dns_notify_t * notify,bool startup)12470 notify_send_queue(dns_notify_t *notify, bool startup) {
12471 	isc_event_t *e;
12472 	isc_result_t result;
12473 
12474 	INSIST(notify->event == NULL);
12475 	e = isc_event_allocate(notify->mctx, NULL, DNS_EVENT_NOTIFYSENDTOADDR,
12476 			       notify_send_toaddr, notify, sizeof(isc_event_t));
12477 	if (startup) {
12478 		notify->event = e;
12479 	}
12480 	e->ev_arg = notify;
12481 	e->ev_sender = NULL;
12482 	result = isc_ratelimiter_enqueue(
12483 		startup ? notify->zone->zmgr->startupnotifyrl
12484 			: notify->zone->zmgr->notifyrl,
12485 		notify->zone->task, &e);
12486 	if (result != ISC_R_SUCCESS) {
12487 		isc_event_free(&e);
12488 		notify->event = NULL;
12489 	}
12490 	return (result);
12491 }
12492 
12493 static void
notify_send_toaddr(isc_task_t * task,isc_event_t * event)12494 notify_send_toaddr(isc_task_t *task, isc_event_t *event) {
12495 	dns_notify_t *notify;
12496 	isc_result_t result;
12497 	dns_message_t *message = NULL;
12498 	isc_netaddr_t dstip;
12499 	dns_tsigkey_t *key = NULL;
12500 	char addrbuf[ISC_SOCKADDR_FORMATSIZE];
12501 	isc_sockaddr_t src;
12502 	unsigned int options, timeout;
12503 	bool have_notifysource = false;
12504 	bool have_notifydscp = false;
12505 	isc_dscp_t dscp = -1;
12506 
12507 	notify = event->ev_arg;
12508 	REQUIRE(DNS_NOTIFY_VALID(notify));
12509 
12510 	UNUSED(task);
12511 
12512 	LOCK_ZONE(notify->zone);
12513 
12514 	notify->event = NULL;
12515 
12516 	if (DNS_ZONE_FLAG(notify->zone, DNS_ZONEFLG_LOADED) == 0) {
12517 		result = ISC_R_CANCELED;
12518 		goto cleanup;
12519 	}
12520 
12521 	if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0 ||
12522 	    DNS_ZONE_FLAG(notify->zone, DNS_ZONEFLG_EXITING) ||
12523 	    notify->zone->view->requestmgr == NULL || notify->zone->db == NULL)
12524 	{
12525 		result = ISC_R_CANCELED;
12526 		goto cleanup;
12527 	}
12528 
12529 	/*
12530 	 * The raw IPv4 address should also exist.  Don't send to the
12531 	 * mapped form.
12532 	 */
12533 	if (isc_sockaddr_pf(&notify->dst) == PF_INET6 &&
12534 	    IN6_IS_ADDR_V4MAPPED(&notify->dst.type.sin6.sin6_addr))
12535 	{
12536 		isc_sockaddr_format(&notify->dst, addrbuf, sizeof(addrbuf));
12537 		notify_log(notify->zone, ISC_LOG_DEBUG(3),
12538 			   "notify: ignoring IPv6 mapped IPV4 address: %s",
12539 			   addrbuf);
12540 		result = ISC_R_CANCELED;
12541 		goto cleanup;
12542 	}
12543 
12544 	result = notify_createmessage(notify->zone, notify->flags, &message);
12545 	if (result != ISC_R_SUCCESS) {
12546 		goto cleanup;
12547 	}
12548 
12549 	isc_sockaddr_format(&notify->dst, addrbuf, sizeof(addrbuf));
12550 	if (notify->key != NULL) {
12551 		/* Transfer ownership of key */
12552 		key = notify->key;
12553 		notify->key = NULL;
12554 	} else {
12555 		isc_netaddr_fromsockaddr(&dstip, &notify->dst);
12556 		result = dns_view_getpeertsig(notify->zone->view, &dstip, &key);
12557 		if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
12558 			notify_log(notify->zone, ISC_LOG_ERROR,
12559 				   "NOTIFY to %s not sent. "
12560 				   "Peer TSIG key lookup failure.",
12561 				   addrbuf);
12562 			goto cleanup_message;
12563 		}
12564 	}
12565 
12566 	if (key != NULL) {
12567 		char namebuf[DNS_NAME_FORMATSIZE];
12568 
12569 		dns_name_format(&key->name, namebuf, sizeof(namebuf));
12570 		notify_log(notify->zone, ISC_LOG_DEBUG(3),
12571 			   "sending notify to %s : TSIG (%s)", addrbuf,
12572 			   namebuf);
12573 	} else {
12574 		notify_log(notify->zone, ISC_LOG_DEBUG(3),
12575 			   "sending notify to %s", addrbuf);
12576 	}
12577 	options = 0;
12578 	if (notify->zone->view->peers != NULL) {
12579 		dns_peer_t *peer = NULL;
12580 		bool usetcp = false;
12581 		result = dns_peerlist_peerbyaddr(notify->zone->view->peers,
12582 						 &dstip, &peer);
12583 		if (result == ISC_R_SUCCESS) {
12584 			result = dns_peer_getnotifysource(peer, &src);
12585 			if (result == ISC_R_SUCCESS) {
12586 				have_notifysource = true;
12587 			}
12588 			dns_peer_getnotifydscp(peer, &dscp);
12589 			if (dscp != -1) {
12590 				have_notifydscp = true;
12591 			}
12592 			result = dns_peer_getforcetcp(peer, &usetcp);
12593 			if (result == ISC_R_SUCCESS && usetcp) {
12594 				options |= DNS_FETCHOPT_TCP;
12595 			}
12596 		}
12597 	}
12598 	switch (isc_sockaddr_pf(&notify->dst)) {
12599 	case PF_INET:
12600 		if (!have_notifysource) {
12601 			src = notify->zone->notifysrc4;
12602 		}
12603 		if (!have_notifydscp) {
12604 			dscp = notify->zone->notifysrc4dscp;
12605 		}
12606 		break;
12607 	case PF_INET6:
12608 		if (!have_notifysource) {
12609 			src = notify->zone->notifysrc6;
12610 		}
12611 		if (!have_notifydscp) {
12612 			dscp = notify->zone->notifysrc6dscp;
12613 		}
12614 		break;
12615 	default:
12616 		result = ISC_R_NOTIMPLEMENTED;
12617 		goto cleanup_key;
12618 	}
12619 	timeout = 15;
12620 	if (DNS_ZONE_FLAG(notify->zone, DNS_ZONEFLG_DIALNOTIFY)) {
12621 		timeout = 30;
12622 	}
12623 	result = dns_request_createvia(
12624 		notify->zone->view->requestmgr, message, &src, &notify->dst,
12625 		dscp, options, key, timeout * 3, timeout, 0, notify->zone->task,
12626 		notify_done, notify, &notify->request);
12627 	if (result == ISC_R_SUCCESS) {
12628 		if (isc_sockaddr_pf(&notify->dst) == AF_INET) {
12629 			inc_stats(notify->zone,
12630 				  dns_zonestatscounter_notifyoutv4);
12631 		} else {
12632 			inc_stats(notify->zone,
12633 				  dns_zonestatscounter_notifyoutv6);
12634 		}
12635 	}
12636 
12637 cleanup_key:
12638 	if (key != NULL) {
12639 		dns_tsigkey_detach(&key);
12640 	}
12641 cleanup_message:
12642 	dns_message_detach(&message);
12643 cleanup:
12644 	UNLOCK_ZONE(notify->zone);
12645 	isc_event_free(&event);
12646 	if (result != ISC_R_SUCCESS) {
12647 		notify_destroy(notify, false);
12648 	}
12649 }
12650 
12651 static void
notify_send(dns_notify_t * notify)12652 notify_send(dns_notify_t *notify) {
12653 	dns_adbaddrinfo_t *ai;
12654 	isc_sockaddr_t dst;
12655 	isc_result_t result;
12656 	dns_notify_t *newnotify = NULL;
12657 	unsigned int flags;
12658 	bool startup;
12659 
12660 	/*
12661 	 * Zone lock held by caller.
12662 	 */
12663 	REQUIRE(DNS_NOTIFY_VALID(notify));
12664 	REQUIRE(LOCKED_ZONE(notify->zone));
12665 
12666 	if (DNS_ZONE_FLAG(notify->zone, DNS_ZONEFLG_EXITING)) {
12667 		return;
12668 	}
12669 
12670 	for (ai = ISC_LIST_HEAD(notify->find->list); ai != NULL;
12671 	     ai = ISC_LIST_NEXT(ai, publink))
12672 	{
12673 		dst = ai->sockaddr;
12674 		if (notify_isqueued(notify->zone, notify->flags, NULL, &dst,
12675 				    NULL)) {
12676 			continue;
12677 		}
12678 		if (notify_isself(notify->zone, &dst)) {
12679 			continue;
12680 		}
12681 		newnotify = NULL;
12682 		flags = notify->flags & DNS_NOTIFY_NOSOA;
12683 		result = notify_create(notify->mctx, flags, &newnotify);
12684 		if (result != ISC_R_SUCCESS) {
12685 			goto cleanup;
12686 		}
12687 		zone_iattach(notify->zone, &newnotify->zone);
12688 		ISC_LIST_APPEND(newnotify->zone->notifies, newnotify, link);
12689 		newnotify->dst = dst;
12690 		startup = ((notify->flags & DNS_NOTIFY_STARTUP) != 0);
12691 		result = notify_send_queue(newnotify, startup);
12692 		if (result != ISC_R_SUCCESS) {
12693 			goto cleanup;
12694 		}
12695 		newnotify = NULL;
12696 	}
12697 
12698 cleanup:
12699 	if (newnotify != NULL) {
12700 		notify_destroy(newnotify, true);
12701 	}
12702 }
12703 
12704 void
dns_zone_notify(dns_zone_t * zone)12705 dns_zone_notify(dns_zone_t *zone) {
12706 	isc_time_t now;
12707 
12708 	REQUIRE(DNS_ZONE_VALID(zone));
12709 
12710 	LOCK_ZONE(zone);
12711 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
12712 
12713 	TIME_NOW(&now);
12714 	zone_settimer(zone, &now);
12715 	UNLOCK_ZONE(zone);
12716 }
12717 
12718 static void
zone_notify(dns_zone_t * zone,isc_time_t * now)12719 zone_notify(dns_zone_t *zone, isc_time_t *now) {
12720 	dns_dbnode_t *node = NULL;
12721 	dns_db_t *zonedb = NULL;
12722 	dns_dbversion_t *version = NULL;
12723 	dns_name_t *origin = NULL;
12724 	dns_name_t master;
12725 	dns_rdata_ns_t ns;
12726 	dns_rdata_soa_t soa;
12727 	uint32_t serial;
12728 	dns_rdata_t rdata = DNS_RDATA_INIT;
12729 	dns_rdataset_t nsrdset;
12730 	dns_rdataset_t soardset;
12731 	isc_result_t result;
12732 	unsigned int i;
12733 	isc_sockaddr_t dst;
12734 	bool isqueued;
12735 	dns_notifytype_t notifytype;
12736 	unsigned int flags = 0;
12737 	bool loggednotify = false;
12738 	bool startup;
12739 
12740 	REQUIRE(DNS_ZONE_VALID(zone));
12741 
12742 	LOCK_ZONE(zone);
12743 	startup = !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
12744 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
12745 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDSTARTUPNOTIFY);
12746 	notifytype = zone->notifytype;
12747 	DNS_ZONE_TIME_ADD(now, zone->notifydelay, &zone->notifytime);
12748 	UNLOCK_ZONE(zone);
12749 
12750 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING) ||
12751 	    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED))
12752 	{
12753 		return;
12754 	}
12755 
12756 	if (notifytype == dns_notifytype_no) {
12757 		return;
12758 	}
12759 
12760 	if (notifytype == dns_notifytype_masteronly &&
12761 	    zone->type != dns_zone_primary) {
12762 		return;
12763 	}
12764 
12765 	origin = &zone->origin;
12766 
12767 	/*
12768 	 * If the zone is dialup we are done as we don't want to send
12769 	 * the current soa so as to force a refresh query.
12770 	 */
12771 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALNOTIFY)) {
12772 		flags |= DNS_NOTIFY_NOSOA;
12773 	}
12774 
12775 	/*
12776 	 * Record that this was a notify due to starting up.
12777 	 */
12778 	if (startup) {
12779 		flags |= DNS_NOTIFY_STARTUP;
12780 	}
12781 
12782 	/*
12783 	 * Get SOA RRset.
12784 	 */
12785 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
12786 	if (zone->db != NULL) {
12787 		dns_db_attach(zone->db, &zonedb);
12788 	}
12789 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
12790 	if (zonedb == NULL) {
12791 		return;
12792 	}
12793 	dns_db_currentversion(zonedb, &version);
12794 	result = dns_db_findnode(zonedb, origin, false, &node);
12795 	if (result != ISC_R_SUCCESS) {
12796 		goto cleanup1;
12797 	}
12798 
12799 	dns_rdataset_init(&soardset);
12800 	result = dns_db_findrdataset(zonedb, node, version, dns_rdatatype_soa,
12801 				     dns_rdatatype_none, 0, &soardset, NULL);
12802 	if (result != ISC_R_SUCCESS) {
12803 		goto cleanup2;
12804 	}
12805 
12806 	/*
12807 	 * Find serial and master server's name.
12808 	 */
12809 	dns_name_init(&master, NULL);
12810 	result = dns_rdataset_first(&soardset);
12811 	if (result != ISC_R_SUCCESS) {
12812 		goto cleanup3;
12813 	}
12814 	dns_rdataset_current(&soardset, &rdata);
12815 	result = dns_rdata_tostruct(&rdata, &soa, NULL);
12816 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
12817 	dns_rdata_reset(&rdata);
12818 	dns_name_dup(&soa.origin, zone->mctx, &master);
12819 	serial = soa.serial;
12820 	dns_rdataset_disassociate(&soardset);
12821 
12822 	/*
12823 	 * Enqueue notify requests for 'also-notify' servers.
12824 	 */
12825 	LOCK_ZONE(zone);
12826 	for (i = 0; i < zone->notifycnt; i++) {
12827 		dns_tsigkey_t *key = NULL;
12828 		dns_notify_t *notify = NULL;
12829 
12830 		if ((zone->notifykeynames != NULL) &&
12831 		    (zone->notifykeynames[i] != NULL)) {
12832 			dns_view_t *view = dns_zone_getview(zone);
12833 			dns_name_t *keyname = zone->notifykeynames[i];
12834 			(void)dns_view_gettsig(view, keyname, &key);
12835 		}
12836 
12837 		dst = zone->notify[i];
12838 		if (notify_isqueued(zone, flags, NULL, &dst, key)) {
12839 			if (key != NULL) {
12840 				dns_tsigkey_detach(&key);
12841 			}
12842 			continue;
12843 		}
12844 
12845 		result = notify_create(zone->mctx, flags, &notify);
12846 		if (result != ISC_R_SUCCESS) {
12847 			if (key != NULL) {
12848 				dns_tsigkey_detach(&key);
12849 			}
12850 			continue;
12851 		}
12852 
12853 		zone_iattach(zone, &notify->zone);
12854 		notify->dst = dst;
12855 
12856 		INSIST(notify->key == NULL);
12857 
12858 		if (key != NULL) {
12859 			notify->key = key;
12860 			key = NULL;
12861 		}
12862 
12863 		ISC_LIST_APPEND(zone->notifies, notify, link);
12864 		result = notify_send_queue(notify, startup);
12865 		if (result != ISC_R_SUCCESS) {
12866 			notify_destroy(notify, true);
12867 		}
12868 		if (!loggednotify) {
12869 			notify_log(zone, ISC_LOG_INFO,
12870 				   "sending notifies (serial %u)", serial);
12871 			loggednotify = true;
12872 		}
12873 	}
12874 	UNLOCK_ZONE(zone);
12875 
12876 	if (notifytype == dns_notifytype_explicit) {
12877 		goto cleanup3;
12878 	}
12879 
12880 	/*
12881 	 * Process NS RRset to generate notifies.
12882 	 */
12883 
12884 	dns_rdataset_init(&nsrdset);
12885 	result = dns_db_findrdataset(zonedb, node, version, dns_rdatatype_ns,
12886 				     dns_rdatatype_none, 0, &nsrdset, NULL);
12887 	if (result != ISC_R_SUCCESS) {
12888 		goto cleanup3;
12889 	}
12890 
12891 	result = dns_rdataset_first(&nsrdset);
12892 	while (result == ISC_R_SUCCESS) {
12893 		dns_notify_t *notify = NULL;
12894 
12895 		dns_rdataset_current(&nsrdset, &rdata);
12896 		result = dns_rdata_tostruct(&rdata, &ns, NULL);
12897 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
12898 		dns_rdata_reset(&rdata);
12899 		/*
12900 		 * Don't notify the master server unless explicitly
12901 		 * configured to do so.
12902 		 */
12903 		if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NOTIFYTOSOA) &&
12904 		    dns_name_compare(&master, &ns.name) == 0)
12905 		{
12906 			result = dns_rdataset_next(&nsrdset);
12907 			continue;
12908 		}
12909 
12910 		if (!loggednotify) {
12911 			notify_log(zone, ISC_LOG_INFO,
12912 				   "sending notifies (serial %u)", serial);
12913 			loggednotify = true;
12914 		}
12915 
12916 		LOCK_ZONE(zone);
12917 		isqueued = notify_isqueued(zone, flags, &ns.name, NULL, NULL);
12918 		UNLOCK_ZONE(zone);
12919 		if (isqueued) {
12920 			result = dns_rdataset_next(&nsrdset);
12921 			continue;
12922 		}
12923 		result = notify_create(zone->mctx, flags, &notify);
12924 		if (result != ISC_R_SUCCESS) {
12925 			continue;
12926 		}
12927 		dns_zone_iattach(zone, &notify->zone);
12928 		dns_name_dup(&ns.name, zone->mctx, &notify->ns);
12929 		LOCK_ZONE(zone);
12930 		ISC_LIST_APPEND(zone->notifies, notify, link);
12931 		UNLOCK_ZONE(zone);
12932 		notify_find_address(notify);
12933 		result = dns_rdataset_next(&nsrdset);
12934 	}
12935 	dns_rdataset_disassociate(&nsrdset);
12936 
12937 cleanup3:
12938 	if (dns_name_dynamic(&master)) {
12939 		dns_name_free(&master, zone->mctx);
12940 	}
12941 cleanup2:
12942 	dns_db_detachnode(zonedb, &node);
12943 cleanup1:
12944 	dns_db_closeversion(zonedb, &version, false);
12945 	dns_db_detach(&zonedb);
12946 }
12947 
12948 /***
12949  *** Private
12950  ***/
12951 static inline isc_result_t
create_query(dns_zone_t * zone,dns_rdatatype_t rdtype,dns_name_t * name,dns_message_t ** messagep)12952 create_query(dns_zone_t *zone, dns_rdatatype_t rdtype, dns_name_t *name,
12953 	     dns_message_t **messagep) {
12954 	dns_message_t *message = NULL;
12955 	dns_name_t *qname = NULL;
12956 	dns_rdataset_t *qrdataset = NULL;
12957 	isc_result_t result;
12958 
12959 	dns_message_create(zone->mctx, DNS_MESSAGE_INTENTRENDER, &message);
12960 
12961 	message->opcode = dns_opcode_query;
12962 	message->rdclass = zone->rdclass;
12963 
12964 	result = dns_message_gettempname(message, &qname);
12965 	if (result != ISC_R_SUCCESS) {
12966 		goto cleanup;
12967 	}
12968 
12969 	result = dns_message_gettemprdataset(message, &qrdataset);
12970 	if (result != ISC_R_SUCCESS) {
12971 		goto cleanup;
12972 	}
12973 
12974 	/*
12975 	 * Make question.
12976 	 */
12977 	dns_name_clone(name, qname);
12978 	dns_rdataset_makequestion(qrdataset, zone->rdclass, rdtype);
12979 	ISC_LIST_APPEND(qname->list, qrdataset, link);
12980 	dns_message_addname(message, qname, DNS_SECTION_QUESTION);
12981 
12982 	*messagep = message;
12983 	return (ISC_R_SUCCESS);
12984 
12985 cleanup:
12986 	if (qname != NULL) {
12987 		dns_message_puttempname(message, &qname);
12988 	}
12989 	if (qrdataset != NULL) {
12990 		dns_message_puttemprdataset(message, &qrdataset);
12991 	}
12992 	dns_message_detach(&message);
12993 	return (result);
12994 }
12995 
12996 static isc_result_t
add_opt(dns_message_t * message,uint16_t udpsize,bool reqnsid,bool reqexpire)12997 add_opt(dns_message_t *message, uint16_t udpsize, bool reqnsid,
12998 	bool reqexpire) {
12999 	isc_result_t result;
13000 	dns_rdataset_t *rdataset = NULL;
13001 	dns_ednsopt_t ednsopts[DNS_EDNSOPTIONS];
13002 	int count = 0;
13003 
13004 	/* Set EDNS options if applicable. */
13005 	if (reqnsid) {
13006 		INSIST(count < DNS_EDNSOPTIONS);
13007 		ednsopts[count].code = DNS_OPT_NSID;
13008 		ednsopts[count].length = 0;
13009 		ednsopts[count].value = NULL;
13010 		count++;
13011 	}
13012 	if (reqexpire) {
13013 		INSIST(count < DNS_EDNSOPTIONS);
13014 		ednsopts[count].code = DNS_OPT_EXPIRE;
13015 		ednsopts[count].length = 0;
13016 		ednsopts[count].value = NULL;
13017 		count++;
13018 	}
13019 	result = dns_message_buildopt(message, &rdataset, 0, udpsize, 0,
13020 				      ednsopts, count);
13021 	if (result != ISC_R_SUCCESS) {
13022 		return (result);
13023 	}
13024 
13025 	return (dns_message_setopt(message, rdataset));
13026 }
13027 
13028 /*
13029  * Called when stub zone update is finished.
13030  * Update zone refresh, retry, expire values accordingly with
13031  * SOA received from master, sync database to file, restart
13032  * zone management timer.
13033  */
13034 static void
stub_finish_zone_update(dns_stub_t * stub,isc_time_t now)13035 stub_finish_zone_update(dns_stub_t *stub, isc_time_t now) {
13036 	uint32_t refresh, retry, expire;
13037 	isc_result_t result;
13038 	isc_interval_t i;
13039 	unsigned int soacount;
13040 	dns_zone_t *zone = stub->zone;
13041 
13042 	/*
13043 	 * Tidy up.
13044 	 */
13045 	dns_db_closeversion(stub->db, &stub->version, true);
13046 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write);
13047 	if (zone->db == NULL) {
13048 		zone_attachdb(zone, stub->db);
13049 	}
13050 	result = zone_get_from_db(zone, zone->db, NULL, &soacount, NULL, NULL,
13051 				  &refresh, &retry, &expire, NULL, NULL);
13052 	if (result == ISC_R_SUCCESS && soacount > 0U) {
13053 		zone->refresh = RANGE(refresh, zone->minrefresh,
13054 				      zone->maxrefresh);
13055 		zone->retry = RANGE(retry, zone->minretry, zone->maxretry);
13056 		zone->expire = RANGE(expire, zone->refresh + zone->retry,
13057 				     DNS_MAX_EXPIRE);
13058 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
13059 	}
13060 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write);
13061 	dns_db_detach(&stub->db);
13062 
13063 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
13064 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED);
13065 	DNS_ZONE_JITTER_ADD(&now, zone->refresh, &zone->refreshtime);
13066 	isc_interval_set(&i, zone->expire, 0);
13067 	DNS_ZONE_TIME_ADD(&now, zone->expire, &zone->expiretime);
13068 
13069 	if (zone->masterfile != NULL) {
13070 		zone_needdump(zone, 0);
13071 	}
13072 
13073 	zone_settimer(zone, &now);
13074 }
13075 
13076 /*
13077  * Process answers for A and AAAA queries when
13078  * resolving nameserver addresses for which glue
13079  * was missing in a previous answer for a NS query.
13080  */
13081 static void
stub_glue_response_cb(isc_task_t * task,isc_event_t * event)13082 stub_glue_response_cb(isc_task_t *task, isc_event_t *event) {
13083 	const char me[] = "stub_glue_response_cb";
13084 	dns_requestevent_t *revent = (dns_requestevent_t *)event;
13085 	dns_stub_t *stub = NULL;
13086 	dns_message_t *msg = NULL;
13087 	dns_zone_t *zone = NULL;
13088 	char master[ISC_SOCKADDR_FORMATSIZE];
13089 	char source[ISC_SOCKADDR_FORMATSIZE];
13090 	uint32_t addr_count, cnamecnt;
13091 	isc_result_t result;
13092 	isc_time_t now;
13093 	struct stub_glue_request *request;
13094 	struct stub_cb_args *cb_args;
13095 	dns_rdataset_t *addr_rdataset = NULL;
13096 	dns_dbnode_t *node = NULL;
13097 
13098 	UNUSED(task);
13099 
13100 	request = revent->ev_arg;
13101 	cb_args = request->args;
13102 	stub = cb_args->stub;
13103 	INSIST(DNS_STUB_VALID(stub));
13104 
13105 	zone = stub->zone;
13106 
13107 	ENTER;
13108 
13109 	TIME_NOW(&now);
13110 
13111 	LOCK_ZONE(zone);
13112 
13113 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
13114 		zone_debuglog(zone, me, 1, "exiting");
13115 		goto cleanup;
13116 	}
13117 
13118 	isc_sockaddr_format(&zone->masteraddr, master, sizeof(master));
13119 	isc_sockaddr_format(&zone->sourceaddr, source, sizeof(source));
13120 
13121 	if (revent->result != ISC_R_SUCCESS) {
13122 		dns_zonemgr_unreachableadd(zone->zmgr, &zone->masteraddr,
13123 					   &zone->sourceaddr, &now);
13124 		dns_zone_log(zone, ISC_LOG_INFO,
13125 			     "could not refresh stub from master %s"
13126 			     " (source %s): %s",
13127 			     master, source, dns_result_totext(revent->result));
13128 		goto cleanup;
13129 	}
13130 
13131 	dns_message_create(zone->mctx, DNS_MESSAGE_INTENTPARSE, &msg);
13132 	result = dns_request_getresponse(revent->request, msg, 0);
13133 	if (result != ISC_R_SUCCESS) {
13134 		dns_zone_log(zone, ISC_LOG_INFO,
13135 			     "refreshing stub: unable to parse response (%s)",
13136 			     isc_result_totext(result));
13137 		goto cleanup;
13138 	}
13139 
13140 	/*
13141 	 * Unexpected opcode.
13142 	 */
13143 	if (msg->opcode != dns_opcode_query) {
13144 		char opcode[128];
13145 		isc_buffer_t rb;
13146 
13147 		isc_buffer_init(&rb, opcode, sizeof(opcode));
13148 		(void)dns_opcode_totext(msg->rcode, &rb);
13149 
13150 		dns_zone_log(zone, ISC_LOG_INFO,
13151 			     "refreshing stub: "
13152 			     "unexpected opcode (%.*s) from %s (source %s)",
13153 			     (int)rb.used, opcode, master, source);
13154 		goto cleanup;
13155 	}
13156 
13157 	/*
13158 	 * Unexpected rcode.
13159 	 */
13160 	if (msg->rcode != dns_rcode_noerror) {
13161 		char rcode[128];
13162 		isc_buffer_t rb;
13163 
13164 		isc_buffer_init(&rb, rcode, sizeof(rcode));
13165 		(void)dns_rcode_totext(msg->rcode, &rb);
13166 
13167 		dns_zone_log(zone, ISC_LOG_INFO,
13168 			     "refreshing stub: "
13169 			     "unexpected rcode (%.*s) from %s (source %s)",
13170 			     (int)rb.used, rcode, master, source);
13171 		goto cleanup;
13172 	}
13173 
13174 	/*
13175 	 * We need complete messages.
13176 	 */
13177 	if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
13178 		if (dns_request_usedtcp(revent->request)) {
13179 			dns_zone_log(zone, ISC_LOG_INFO,
13180 				     "refreshing stub: truncated TCP "
13181 				     "response from master %s (source %s)",
13182 				     master, source);
13183 		}
13184 		goto cleanup;
13185 	}
13186 
13187 	/*
13188 	 * If non-auth log.
13189 	 */
13190 	if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0) {
13191 		dns_zone_log(zone, ISC_LOG_INFO,
13192 			     "refreshing stub: "
13193 			     "non-authoritative answer from "
13194 			     "master %s (source %s)",
13195 			     master, source);
13196 		goto cleanup;
13197 	}
13198 
13199 	/*
13200 	 * Sanity checks.
13201 	 */
13202 	cnamecnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_cname);
13203 	addr_count = message_count(msg, DNS_SECTION_ANSWER,
13204 				   request->ipv4 ? dns_rdatatype_a
13205 						 : dns_rdatatype_aaaa);
13206 
13207 	if (cnamecnt != 0) {
13208 		dns_zone_log(zone, ISC_LOG_INFO,
13209 			     "refreshing stub: unexpected CNAME response "
13210 			     "from master %s (source %s)",
13211 			     master, source);
13212 		goto cleanup;
13213 	}
13214 
13215 	if (addr_count == 0) {
13216 		dns_zone_log(zone, ISC_LOG_INFO,
13217 			     "refreshing stub: no %s records in response "
13218 			     "from master %s (source %s)",
13219 			     request->ipv4 ? "A" : "AAAA", master, source);
13220 		goto cleanup;
13221 	}
13222 	/*
13223 	 * Extract A or AAAA RRset from message.
13224 	 */
13225 	result = dns_message_findname(msg, DNS_SECTION_ANSWER, &request->name,
13226 				      request->ipv4 ? dns_rdatatype_a
13227 						    : dns_rdatatype_aaaa,
13228 				      dns_rdatatype_none, NULL, &addr_rdataset);
13229 	if (result != ISC_R_SUCCESS) {
13230 		if (result != DNS_R_NXDOMAIN && result != DNS_R_NXRRSET) {
13231 			char namebuf[DNS_NAME_FORMATSIZE];
13232 			dns_name_format(&request->name, namebuf,
13233 					sizeof(namebuf));
13234 			dns_zone_log(
13235 				zone, ISC_LOG_INFO,
13236 				"refreshing stub: dns_message_findname(%s/%s) "
13237 				"failed (%s)",
13238 				namebuf, request->ipv4 ? "A" : "AAAA",
13239 				isc_result_totext(result));
13240 		}
13241 		goto cleanup;
13242 	}
13243 
13244 	result = dns_db_findnode(stub->db, &request->name, true, &node);
13245 	if (result != ISC_R_SUCCESS) {
13246 		dns_zone_log(zone, ISC_LOG_INFO,
13247 			     "refreshing stub: "
13248 			     "dns_db_findnode() failed: %s",
13249 			     dns_result_totext(result));
13250 		goto cleanup;
13251 	}
13252 
13253 	result = dns_db_addrdataset(stub->db, node, stub->version, 0,
13254 				    addr_rdataset, 0, NULL);
13255 	if (result != ISC_R_SUCCESS) {
13256 		dns_zone_log(zone, ISC_LOG_INFO,
13257 			     "refreshing stub: "
13258 			     "dns_db_addrdataset() failed: %s",
13259 			     dns_result_totext(result));
13260 	}
13261 	dns_db_detachnode(stub->db, &node);
13262 
13263 cleanup:
13264 	if (msg != NULL) {
13265 		dns_message_detach(&msg);
13266 	}
13267 	isc_event_free(&event);
13268 	dns_name_free(&request->name, zone->mctx);
13269 	dns_request_destroy(&request->request);
13270 	isc_mem_put(zone->mctx, request, sizeof(*request));
13271 
13272 	/* If last request, release all related resources */
13273 	if (atomic_fetch_sub_release(&stub->pending_requests, 1) == 1) {
13274 		isc_mem_put(zone->mctx, cb_args, sizeof(*cb_args));
13275 		stub_finish_zone_update(stub, now);
13276 		UNLOCK_ZONE(zone);
13277 		stub->magic = 0;
13278 		dns_zone_idetach(&stub->zone);
13279 		INSIST(stub->db == NULL);
13280 		INSIST(stub->version == NULL);
13281 		isc_mem_put(stub->mctx, stub, sizeof(*stub));
13282 	} else {
13283 		UNLOCK_ZONE(zone);
13284 	}
13285 }
13286 
13287 /*
13288  * Create and send an A or AAAA query to the master
13289  * server of the stub zone given.
13290  */
13291 static isc_result_t
stub_request_nameserver_address(struct stub_cb_args * args,bool ipv4,const dns_name_t * name)13292 stub_request_nameserver_address(struct stub_cb_args *args, bool ipv4,
13293 				const dns_name_t *name) {
13294 	dns_message_t *message = NULL;
13295 	dns_zone_t *zone;
13296 	isc_result_t result;
13297 	struct stub_glue_request *request;
13298 
13299 	zone = args->stub->zone;
13300 	request = isc_mem_get(zone->mctx, sizeof(*request));
13301 	request->request = NULL;
13302 	request->args = args;
13303 	request->name = (dns_name_t)DNS_NAME_INITEMPTY;
13304 	request->ipv4 = ipv4;
13305 	dns_name_dup(name, zone->mctx, &request->name);
13306 
13307 	result = create_query(zone, ipv4 ? dns_rdatatype_a : dns_rdatatype_aaaa,
13308 			      &request->name, &message);
13309 	INSIST(result == ISC_R_SUCCESS);
13310 
13311 	if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) {
13312 		result = add_opt(message, args->udpsize, args->reqnsid, false);
13313 		if (result != ISC_R_SUCCESS) {
13314 			zone_debuglog(zone, "stub_send_query", 1,
13315 				      "unable to add opt record: %s",
13316 				      dns_result_totext(result));
13317 			goto fail;
13318 		}
13319 	}
13320 
13321 	atomic_fetch_add_release(&args->stub->pending_requests, 1);
13322 
13323 	result = dns_request_createvia(
13324 		zone->view->requestmgr, message, &zone->sourceaddr,
13325 		&zone->masteraddr, args->dscp, DNS_REQUESTOPT_TCP,
13326 		args->tsig_key, args->timeout * 3, args->timeout, 0, zone->task,
13327 		stub_glue_response_cb, request, &request->request);
13328 
13329 	if (result != ISC_R_SUCCESS) {
13330 		INSIST(atomic_fetch_sub_release(&args->stub->pending_requests,
13331 						1) > 1);
13332 		zone_debuglog(zone, "stub_send_query", 1,
13333 			      "dns_request_createvia() failed: %s",
13334 			      dns_result_totext(result));
13335 		goto fail;
13336 	}
13337 
13338 	dns_message_detach(&message);
13339 
13340 	return (ISC_R_SUCCESS);
13341 
13342 fail:
13343 	dns_name_free(&request->name, zone->mctx);
13344 	isc_mem_put(zone->mctx, request, sizeof(*request));
13345 
13346 	if (message != NULL) {
13347 		dns_message_detach(&message);
13348 	}
13349 
13350 	return (result);
13351 }
13352 
13353 static inline isc_result_t
save_nsrrset(dns_message_t * message,dns_name_t * name,struct stub_cb_args * cb_args,dns_db_t * db,dns_dbversion_t * version)13354 save_nsrrset(dns_message_t *message, dns_name_t *name,
13355 	     struct stub_cb_args *cb_args, dns_db_t *db,
13356 	     dns_dbversion_t *version) {
13357 	dns_rdataset_t *nsrdataset = NULL;
13358 	dns_rdataset_t *rdataset = NULL;
13359 	dns_dbnode_t *node = NULL;
13360 	dns_rdata_ns_t ns;
13361 	isc_result_t result;
13362 	dns_rdata_t rdata = DNS_RDATA_INIT;
13363 	bool has_glue = false;
13364 	dns_name_t *ns_name;
13365 	/*
13366 	 * List of NS entries in answer, keep names that will be used
13367 	 * to resolve missing A/AAAA glue for each entry.
13368 	 */
13369 	dns_namelist_t ns_list;
13370 	ISC_LIST_INIT(ns_list);
13371 
13372 	/*
13373 	 * Extract NS RRset from message.
13374 	 */
13375 	result = dns_message_findname(message, DNS_SECTION_ANSWER, name,
13376 				      dns_rdatatype_ns, dns_rdatatype_none,
13377 				      NULL, &nsrdataset);
13378 	if (result != ISC_R_SUCCESS) {
13379 		goto done;
13380 	}
13381 
13382 	/*
13383 	 * Add NS rdataset.
13384 	 */
13385 	result = dns_db_findnode(db, name, true, &node);
13386 	if (result != ISC_R_SUCCESS) {
13387 		goto done;
13388 	}
13389 	result = dns_db_addrdataset(db, node, version, 0, nsrdataset, 0, NULL);
13390 	dns_db_detachnode(db, &node);
13391 	if (result != ISC_R_SUCCESS) {
13392 		goto done;
13393 	}
13394 	/*
13395 	 * Add glue rdatasets.
13396 	 */
13397 	for (result = dns_rdataset_first(nsrdataset); result == ISC_R_SUCCESS;
13398 	     result = dns_rdataset_next(nsrdataset))
13399 	{
13400 		dns_rdataset_current(nsrdataset, &rdata);
13401 		result = dns_rdata_tostruct(&rdata, &ns, NULL);
13402 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
13403 		dns_rdata_reset(&rdata);
13404 
13405 		if (!dns_name_issubdomain(&ns.name, name)) {
13406 			continue;
13407 		}
13408 		rdataset = NULL;
13409 		result = dns_message_findname(message, DNS_SECTION_ADDITIONAL,
13410 					      &ns.name, dns_rdatatype_aaaa,
13411 					      dns_rdatatype_none, NULL,
13412 					      &rdataset);
13413 		if (result == ISC_R_SUCCESS) {
13414 			has_glue = true;
13415 			result = dns_db_findnode(db, &ns.name, true, &node);
13416 			if (result != ISC_R_SUCCESS) {
13417 				goto done;
13418 			}
13419 			result = dns_db_addrdataset(db, node, version, 0,
13420 						    rdataset, 0, NULL);
13421 			dns_db_detachnode(db, &node);
13422 			if (result != ISC_R_SUCCESS) {
13423 				goto done;
13424 			}
13425 		}
13426 
13427 		rdataset = NULL;
13428 		result = dns_message_findname(
13429 			message, DNS_SECTION_ADDITIONAL, &ns.name,
13430 			dns_rdatatype_a, dns_rdatatype_none, NULL, &rdataset);
13431 		if (result == ISC_R_SUCCESS) {
13432 			has_glue = true;
13433 			result = dns_db_findnode(db, &ns.name, true, &node);
13434 			if (result != ISC_R_SUCCESS) {
13435 				goto done;
13436 			}
13437 			result = dns_db_addrdataset(db, node, version, 0,
13438 						    rdataset, 0, NULL);
13439 			dns_db_detachnode(db, &node);
13440 			if (result != ISC_R_SUCCESS) {
13441 				goto done;
13442 			}
13443 		}
13444 
13445 		/*
13446 		 * If no glue is found so far, we add the name to the list to
13447 		 * resolve the A/AAAA glue later. If any glue is found in any
13448 		 * iteration step, this list will be discarded and only the glue
13449 		 * provided in this message will be used.
13450 		 */
13451 		if (!has_glue && dns_name_issubdomain(&ns.name, name)) {
13452 			dns_name_t *tmp_name;
13453 			tmp_name = isc_mem_get(cb_args->stub->mctx,
13454 					       sizeof(*tmp_name));
13455 			dns_name_init(tmp_name, NULL);
13456 			dns_name_dup(&ns.name, cb_args->stub->mctx, tmp_name);
13457 			ISC_LIST_APPEND(ns_list, tmp_name, link);
13458 		}
13459 	}
13460 
13461 	if (result != ISC_R_NOMORE) {
13462 		goto done;
13463 	}
13464 
13465 	/*
13466 	 * If no glue records were found, we attempt to resolve A/AAAA
13467 	 * for each NS entry found in the answer.
13468 	 */
13469 	if (!has_glue) {
13470 		for (ns_name = ISC_LIST_HEAD(ns_list); ns_name != NULL;
13471 		     ns_name = ISC_LIST_NEXT(ns_name, link))
13472 		{
13473 			/*
13474 			 * Resolve NS IPv4 address/A.
13475 			 */
13476 			result = stub_request_nameserver_address(cb_args, true,
13477 								 ns_name);
13478 			if (result != ISC_R_SUCCESS) {
13479 				goto done;
13480 			}
13481 			/*
13482 			 * Resolve NS IPv6 address/AAAA.
13483 			 */
13484 			result = stub_request_nameserver_address(cb_args, false,
13485 								 ns_name);
13486 			if (result != ISC_R_SUCCESS) {
13487 				goto done;
13488 			}
13489 		}
13490 	}
13491 
13492 	result = ISC_R_SUCCESS;
13493 
13494 done:
13495 	while ((ns_name = ISC_LIST_HEAD(ns_list)) != NULL) {
13496 		ISC_LIST_UNLINK(ns_list, ns_name, link);
13497 		dns_name_free(ns_name, cb_args->stub->mctx);
13498 		isc_mem_put(cb_args->stub->mctx, ns_name, sizeof(*ns_name));
13499 	}
13500 	return (result);
13501 }
13502 
13503 static void
stub_callback(isc_task_t * task,isc_event_t * event)13504 stub_callback(isc_task_t *task, isc_event_t *event) {
13505 	const char me[] = "stub_callback";
13506 	dns_requestevent_t *revent = (dns_requestevent_t *)event;
13507 	dns_stub_t *stub = NULL;
13508 	dns_message_t *msg = NULL;
13509 	dns_zone_t *zone = NULL;
13510 	char master[ISC_SOCKADDR_FORMATSIZE];
13511 	char source[ISC_SOCKADDR_FORMATSIZE];
13512 	uint32_t nscnt, cnamecnt;
13513 	isc_result_t result;
13514 	isc_time_t now;
13515 	bool exiting = false;
13516 	unsigned int j;
13517 	struct stub_cb_args *cb_args;
13518 
13519 	cb_args = revent->ev_arg;
13520 	stub = cb_args->stub;
13521 	INSIST(DNS_STUB_VALID(stub));
13522 
13523 	UNUSED(task);
13524 
13525 	zone = stub->zone;
13526 
13527 	ENTER;
13528 
13529 	TIME_NOW(&now);
13530 
13531 	LOCK_ZONE(zone);
13532 
13533 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
13534 		zone_debuglog(zone, me, 1, "exiting");
13535 		exiting = true;
13536 		goto next_master;
13537 	}
13538 
13539 	isc_sockaddr_format(&zone->masteraddr, master, sizeof(master));
13540 	isc_sockaddr_format(&zone->sourceaddr, source, sizeof(source));
13541 
13542 	if (revent->result != ISC_R_SUCCESS) {
13543 		if (revent->result == ISC_R_TIMEDOUT &&
13544 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS))
13545 		{
13546 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
13547 			dns_zone_log(zone, ISC_LOG_DEBUG(1),
13548 				     "refreshing stub: timeout retrying "
13549 				     " without EDNS master %s (source %s)",
13550 				     master, source);
13551 			goto same_master;
13552 		}
13553 		dns_zonemgr_unreachableadd(zone->zmgr, &zone->masteraddr,
13554 					   &zone->sourceaddr, &now);
13555 		dns_zone_log(zone, ISC_LOG_INFO,
13556 			     "could not refresh stub from master %s"
13557 			     " (source %s): %s",
13558 			     master, source, dns_result_totext(revent->result));
13559 		goto next_master;
13560 	}
13561 
13562 	dns_message_create(zone->mctx, DNS_MESSAGE_INTENTPARSE, &msg);
13563 
13564 	result = dns_request_getresponse(revent->request, msg, 0);
13565 	if (result != ISC_R_SUCCESS) {
13566 		goto next_master;
13567 	}
13568 
13569 	/*
13570 	 * Unexpected opcode.
13571 	 */
13572 	if (msg->opcode != dns_opcode_query) {
13573 		char opcode[128];
13574 		isc_buffer_t rb;
13575 
13576 		isc_buffer_init(&rb, opcode, sizeof(opcode));
13577 		(void)dns_opcode_totext(msg->rcode, &rb);
13578 
13579 		dns_zone_log(zone, ISC_LOG_INFO,
13580 			     "refreshing stub: "
13581 			     "unexpected opcode (%.*s) from %s (source %s)",
13582 			     (int)rb.used, opcode, master, source);
13583 		goto next_master;
13584 	}
13585 
13586 	/*
13587 	 * Unexpected rcode.
13588 	 */
13589 	if (msg->rcode != dns_rcode_noerror) {
13590 		char rcode[128];
13591 		isc_buffer_t rb;
13592 
13593 		isc_buffer_init(&rb, rcode, sizeof(rcode));
13594 		(void)dns_rcode_totext(msg->rcode, &rb);
13595 
13596 		if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS) &&
13597 		    (msg->rcode == dns_rcode_servfail ||
13598 		     msg->rcode == dns_rcode_notimp ||
13599 		     msg->rcode == dns_rcode_formerr))
13600 		{
13601 			dns_zone_log(zone, ISC_LOG_DEBUG(1),
13602 				     "refreshing stub: rcode (%.*s) retrying "
13603 				     "without EDNS master %s (source %s)",
13604 				     (int)rb.used, rcode, master, source);
13605 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
13606 			goto same_master;
13607 		}
13608 
13609 		dns_zone_log(zone, ISC_LOG_INFO,
13610 			     "refreshing stub: "
13611 			     "unexpected rcode (%.*s) from %s (source %s)",
13612 			     (int)rb.used, rcode, master, source);
13613 		goto next_master;
13614 	}
13615 
13616 	/*
13617 	 * We need complete messages.
13618 	 */
13619 	if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
13620 		if (dns_request_usedtcp(revent->request)) {
13621 			dns_zone_log(zone, ISC_LOG_INFO,
13622 				     "refreshing stub: truncated TCP "
13623 				     "response from master %s (source %s)",
13624 				     master, source);
13625 			goto next_master;
13626 		}
13627 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_USEVC);
13628 		goto same_master;
13629 	}
13630 
13631 	/*
13632 	 * If non-auth log and next master.
13633 	 */
13634 	if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0) {
13635 		dns_zone_log(zone, ISC_LOG_INFO,
13636 			     "refreshing stub: "
13637 			     "non-authoritative answer from "
13638 			     "master %s (source %s)",
13639 			     master, source);
13640 		goto next_master;
13641 	}
13642 
13643 	/*
13644 	 * Sanity checks.
13645 	 */
13646 	cnamecnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_cname);
13647 	nscnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_ns);
13648 
13649 	if (cnamecnt != 0) {
13650 		dns_zone_log(zone, ISC_LOG_INFO,
13651 			     "refreshing stub: unexpected CNAME response "
13652 			     "from master %s (source %s)",
13653 			     master, source);
13654 		goto next_master;
13655 	}
13656 
13657 	if (nscnt == 0) {
13658 		dns_zone_log(zone, ISC_LOG_INFO,
13659 			     "refreshing stub: no NS records in response "
13660 			     "from master %s (source %s)",
13661 			     master, source);
13662 		goto next_master;
13663 	}
13664 
13665 	atomic_fetch_add(&stub->pending_requests, 1);
13666 
13667 	/*
13668 	 * Save answer.
13669 	 */
13670 	result = save_nsrrset(msg, &zone->origin, cb_args, stub->db,
13671 			      stub->version);
13672 	if (result != ISC_R_SUCCESS) {
13673 		dns_zone_log(zone, ISC_LOG_INFO,
13674 			     "refreshing stub: unable to save NS records "
13675 			     "from master %s (source %s)",
13676 			     master, source);
13677 		goto next_master;
13678 	}
13679 
13680 	dns_message_detach(&msg);
13681 	isc_event_free(&event);
13682 	dns_request_destroy(&zone->request);
13683 
13684 	/*
13685 	 * Check to see if there are no outstanding requests and
13686 	 * finish off if that is so.
13687 	 */
13688 	if (atomic_fetch_sub(&stub->pending_requests, 1) == 1) {
13689 		isc_mem_put(zone->mctx, cb_args, sizeof(*cb_args));
13690 		stub_finish_zone_update(stub, now);
13691 		goto free_stub;
13692 	}
13693 
13694 	UNLOCK_ZONE(zone);
13695 	return;
13696 
13697 next_master:
13698 	isc_mem_put(zone->mctx, cb_args, sizeof(*cb_args));
13699 	if (stub->version != NULL) {
13700 		dns_db_closeversion(stub->db, &stub->version, false);
13701 	}
13702 	if (stub->db != NULL) {
13703 		dns_db_detach(&stub->db);
13704 	}
13705 	if (msg != NULL) {
13706 		dns_message_detach(&msg);
13707 	}
13708 	isc_event_free(&event);
13709 	dns_request_destroy(&zone->request);
13710 	/*
13711 	 * Skip to next failed / untried master.
13712 	 */
13713 	do {
13714 		zone->curmaster++;
13715 	} while (zone->curmaster < zone->masterscnt &&
13716 		 zone->mastersok[zone->curmaster]);
13717 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOEDNS);
13718 	if (exiting || zone->curmaster >= zone->masterscnt) {
13719 		bool done = true;
13720 		if (!exiting &&
13721 		    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_USEALTXFRSRC) &&
13722 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEALTXFRSRC))
13723 		{
13724 			/*
13725 			 * Did we get a good answer from all the primaries?
13726 			 */
13727 			for (j = 0; j < zone->masterscnt; j++) {
13728 				if (!zone->mastersok[j]) {
13729 					{
13730 						done = false;
13731 						break;
13732 					}
13733 				}
13734 			}
13735 		} else {
13736 			done = true;
13737 		}
13738 		if (!done) {
13739 			zone->curmaster = 0;
13740 			/*
13741 			 * Find the next failed master.
13742 			 */
13743 			while (zone->curmaster < zone->masterscnt &&
13744 			       zone->mastersok[zone->curmaster]) {
13745 				zone->curmaster++;
13746 			}
13747 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_USEALTXFRSRC);
13748 		} else {
13749 			DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
13750 
13751 			zone_settimer(zone, &now);
13752 			goto free_stub;
13753 		}
13754 	}
13755 	queue_soa_query(zone);
13756 	goto free_stub;
13757 
13758 same_master:
13759 	isc_mem_put(zone->mctx, cb_args, sizeof(*cb_args));
13760 	if (msg != NULL) {
13761 		dns_message_detach(&msg);
13762 	}
13763 	isc_event_free(&event);
13764 	dns_request_destroy(&zone->request);
13765 	ns_query(zone, NULL, stub);
13766 	UNLOCK_ZONE(zone);
13767 	goto done;
13768 
13769 free_stub:
13770 	UNLOCK_ZONE(zone);
13771 	stub->magic = 0;
13772 	dns_zone_idetach(&stub->zone);
13773 	INSIST(stub->db == NULL);
13774 	INSIST(stub->version == NULL);
13775 	isc_mem_put(stub->mctx, stub, sizeof(*stub));
13776 
13777 done:
13778 	INSIST(event == NULL);
13779 	return;
13780 }
13781 
13782 /*
13783  * Get the EDNS EXPIRE option from the response and if it exists trim
13784  * expire to be not more than it.
13785  */
13786 static void
get_edns_expire(dns_zone_t * zone,dns_message_t * message,uint32_t * expirep)13787 get_edns_expire(dns_zone_t *zone, dns_message_t *message, uint32_t *expirep) {
13788 	isc_result_t result;
13789 	uint32_t expire;
13790 	dns_rdata_t rdata = DNS_RDATA_INIT;
13791 	isc_buffer_t optbuf;
13792 	uint16_t optcode;
13793 	uint16_t optlen;
13794 
13795 	REQUIRE(expirep != NULL);
13796 	REQUIRE(message != NULL);
13797 
13798 	if (message->opt == NULL) {
13799 		return;
13800 	}
13801 
13802 	result = dns_rdataset_first(message->opt);
13803 	if (result == ISC_R_SUCCESS) {
13804 		dns_rdataset_current(message->opt, &rdata);
13805 		isc_buffer_init(&optbuf, rdata.data, rdata.length);
13806 		isc_buffer_add(&optbuf, rdata.length);
13807 		while (isc_buffer_remaininglength(&optbuf) >= 4) {
13808 			optcode = isc_buffer_getuint16(&optbuf);
13809 			optlen = isc_buffer_getuint16(&optbuf);
13810 			/*
13811 			 * A EDNS EXPIRE response has a length of 4.
13812 			 */
13813 			if (optcode != DNS_OPT_EXPIRE || optlen != 4) {
13814 				isc_buffer_forward(&optbuf, optlen);
13815 				continue;
13816 			}
13817 			expire = isc_buffer_getuint32(&optbuf);
13818 			dns_zone_log(zone, ISC_LOG_DEBUG(1),
13819 				     "got EDNS EXPIRE of %u", expire);
13820 			/*
13821 			 * Trim *expirep?
13822 			 */
13823 			if (expire < *expirep) {
13824 				*expirep = expire;
13825 			}
13826 			break;
13827 		}
13828 	}
13829 }
13830 
13831 /*
13832  * Set the file modification time zone->expire seconds before expiretime.
13833  */
13834 static void
setmodtime(dns_zone_t * zone,isc_time_t * expiretime)13835 setmodtime(dns_zone_t *zone, isc_time_t *expiretime) {
13836 	isc_result_t result;
13837 	isc_time_t when;
13838 	isc_interval_t i;
13839 
13840 	isc_interval_set(&i, zone->expire, 0);
13841 	result = isc_time_subtract(expiretime, &i, &when);
13842 	if (result != ISC_R_SUCCESS) {
13843 		return;
13844 	}
13845 
13846 	result = ISC_R_FAILURE;
13847 	if (zone->journal != NULL) {
13848 		result = isc_file_settime(zone->journal, &when);
13849 	}
13850 	if (result == ISC_R_SUCCESS &&
13851 	    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
13852 	    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING))
13853 	{
13854 		result = isc_file_settime(zone->masterfile, &when);
13855 	} else if (result != ISC_R_SUCCESS) {
13856 		result = isc_file_settime(zone->masterfile, &when);
13857 	}
13858 
13859 	/*
13860 	 * Someone removed the file from underneath us!
13861 	 */
13862 	if (result == ISC_R_FILENOTFOUND) {
13863 		zone_needdump(zone, DNS_DUMP_DELAY);
13864 	} else if (result != ISC_R_SUCCESS) {
13865 		dns_zone_log(zone, ISC_LOG_ERROR,
13866 			     "refresh: could not set "
13867 			     "file modification time of '%s': %s",
13868 			     zone->masterfile, dns_result_totext(result));
13869 	}
13870 }
13871 
13872 /*
13873  * An SOA query has finished (successfully or not).
13874  */
13875 static void
refresh_callback(isc_task_t * task,isc_event_t * event)13876 refresh_callback(isc_task_t *task, isc_event_t *event) {
13877 	const char me[] = "refresh_callback";
13878 	dns_requestevent_t *revent = (dns_requestevent_t *)event;
13879 	dns_zone_t *zone;
13880 	dns_message_t *msg = NULL;
13881 	uint32_t soacnt, cnamecnt, soacount, nscount;
13882 	isc_time_t now;
13883 	char master[ISC_SOCKADDR_FORMATSIZE];
13884 	char source[ISC_SOCKADDR_FORMATSIZE];
13885 	dns_rdataset_t *rdataset = NULL;
13886 	dns_rdata_t rdata = DNS_RDATA_INIT;
13887 	dns_rdata_soa_t soa;
13888 	isc_result_t result;
13889 	uint32_t serial, oldserial = 0;
13890 	unsigned int j;
13891 	bool do_queue_xfrin = false;
13892 
13893 	zone = revent->ev_arg;
13894 	INSIST(DNS_ZONE_VALID(zone));
13895 
13896 	UNUSED(task);
13897 
13898 	ENTER;
13899 
13900 	TIME_NOW(&now);
13901 
13902 	LOCK_ZONE(zone);
13903 
13904 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
13905 		isc_event_free(&event);
13906 		dns_request_destroy(&zone->request);
13907 		goto detach;
13908 	}
13909 
13910 	/*
13911 	 * if timeout log and next master;
13912 	 */
13913 
13914 	isc_sockaddr_format(&zone->masteraddr, master, sizeof(master));
13915 	isc_sockaddr_format(&zone->sourceaddr, source, sizeof(source));
13916 
13917 	if (revent->result != ISC_R_SUCCESS) {
13918 		if (revent->result == ISC_R_TIMEDOUT &&
13919 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS))
13920 		{
13921 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
13922 			dns_zone_log(zone, ISC_LOG_DEBUG(1),
13923 				     "refresh: timeout retrying without EDNS "
13924 				     "master %s (source %s)",
13925 				     master, source);
13926 			goto same_master;
13927 		}
13928 		if (revent->result == ISC_R_TIMEDOUT &&
13929 		    !dns_request_usedtcp(revent->request)) {
13930 			dns_zone_log(zone, ISC_LOG_INFO,
13931 				     "refresh: retry limit for "
13932 				     "master %s exceeded (source %s)",
13933 				     master, source);
13934 			/* Try with slave with TCP. */
13935 			if ((zone->type == dns_zone_secondary ||
13936 			     zone->type == dns_zone_mirror ||
13937 			     zone->type == dns_zone_redirect) &&
13938 			    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_TRYTCPREFRESH))
13939 			{
13940 				if (!dns_zonemgr_unreachable(
13941 					    zone->zmgr, &zone->masteraddr,
13942 					    &zone->sourceaddr, &now))
13943 				{
13944 					DNS_ZONE_SETFLAG(
13945 						zone,
13946 						DNS_ZONEFLG_SOABEFOREAXFR);
13947 					goto tcp_transfer;
13948 				}
13949 				dns_zone_log(zone, ISC_LOG_DEBUG(1),
13950 					     "refresh: skipped tcp fallback "
13951 					     "as master %s (source %s) is "
13952 					     "unreachable (cached)",
13953 					     master, source);
13954 			}
13955 		} else {
13956 			dns_zone_log(zone, ISC_LOG_INFO,
13957 				     "refresh: failure trying master "
13958 				     "%s (source %s): %s",
13959 				     master, source,
13960 				     dns_result_totext(revent->result));
13961 		}
13962 		goto next_master;
13963 	}
13964 
13965 	dns_message_create(zone->mctx, DNS_MESSAGE_INTENTPARSE, &msg);
13966 	result = dns_request_getresponse(revent->request, msg, 0);
13967 	if (result != ISC_R_SUCCESS) {
13968 		dns_zone_log(zone, ISC_LOG_INFO,
13969 			     "refresh: failure trying master "
13970 			     "%s (source %s): %s",
13971 			     master, source, dns_result_totext(result));
13972 		goto next_master;
13973 	}
13974 
13975 	/*
13976 	 * Unexpected opcode.
13977 	 */
13978 	if (msg->opcode != dns_opcode_query) {
13979 		char opcode[128];
13980 		isc_buffer_t rb;
13981 
13982 		isc_buffer_init(&rb, opcode, sizeof(opcode));
13983 		(void)dns_opcode_totext(msg->rcode, &rb);
13984 
13985 		dns_zone_log(zone, ISC_LOG_INFO,
13986 			     "refresh: "
13987 			     "unexpected opcode (%.*s) from %s (source %s)",
13988 			     (int)rb.used, opcode, master, source);
13989 		goto next_master;
13990 	}
13991 
13992 	/*
13993 	 * Unexpected rcode.
13994 	 */
13995 	if (msg->rcode != dns_rcode_noerror) {
13996 		char rcode[128];
13997 		isc_buffer_t rb;
13998 
13999 		isc_buffer_init(&rb, rcode, sizeof(rcode));
14000 		(void)dns_rcode_totext(msg->rcode, &rb);
14001 
14002 		if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS) &&
14003 		    (msg->rcode == dns_rcode_servfail ||
14004 		     msg->rcode == dns_rcode_notimp ||
14005 		     msg->rcode == dns_rcode_formerr))
14006 		{
14007 			dns_zone_log(zone, ISC_LOG_DEBUG(1),
14008 				     "refresh: rcode (%.*s) retrying without "
14009 				     "EDNS master %s (source %s)",
14010 				     (int)rb.used, rcode, master, source);
14011 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
14012 			goto same_master;
14013 		}
14014 		if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS) &&
14015 		    msg->rcode == dns_rcode_badvers) {
14016 			dns_zone_log(zone, ISC_LOG_DEBUG(1),
14017 				     "refresh: rcode (%.*s) retrying without "
14018 				     "EDNS EXPIRE OPTION master %s (source %s)",
14019 				     (int)rb.used, rcode, master, source);
14020 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
14021 			goto same_master;
14022 		}
14023 		dns_zone_log(zone, ISC_LOG_INFO,
14024 			     "refresh: unexpected rcode (%.*s) from "
14025 			     "master %s (source %s)",
14026 			     (int)rb.used, rcode, master, source);
14027 		/*
14028 		 * Perhaps AXFR/IXFR is allowed even if SOA queries aren't.
14029 		 */
14030 		if (msg->rcode == dns_rcode_refused &&
14031 		    (zone->type == dns_zone_secondary ||
14032 		     zone->type == dns_zone_mirror ||
14033 		     zone->type == dns_zone_redirect))
14034 		{
14035 			goto tcp_transfer;
14036 		}
14037 		goto next_master;
14038 	}
14039 
14040 	/*
14041 	 * If truncated punt to zone transfer which will query again.
14042 	 */
14043 	if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
14044 		if (zone->type == dns_zone_secondary ||
14045 		    zone->type == dns_zone_mirror ||
14046 		    zone->type == dns_zone_redirect)
14047 		{
14048 			dns_zone_log(zone, ISC_LOG_INFO,
14049 				     "refresh: truncated UDP answer, "
14050 				     "initiating TCP zone xfer "
14051 				     "for master %s (source %s)",
14052 				     master, source);
14053 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_SOABEFOREAXFR);
14054 			goto tcp_transfer;
14055 		} else {
14056 			INSIST(zone->type == dns_zone_stub);
14057 			if (dns_request_usedtcp(revent->request)) {
14058 				dns_zone_log(zone, ISC_LOG_INFO,
14059 					     "refresh: truncated TCP response "
14060 					     "from master %s (source %s)",
14061 					     master, source);
14062 				goto next_master;
14063 			}
14064 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_USEVC);
14065 			goto same_master;
14066 		}
14067 	}
14068 
14069 	/*
14070 	 * if non-auth log and next master;
14071 	 */
14072 	if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0) {
14073 		dns_zone_log(zone, ISC_LOG_INFO,
14074 			     "refresh: non-authoritative answer from "
14075 			     "master %s (source %s)",
14076 			     master, source);
14077 		goto next_master;
14078 	}
14079 
14080 	cnamecnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_cname);
14081 	soacnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_soa);
14082 	nscount = message_count(msg, DNS_SECTION_AUTHORITY, dns_rdatatype_ns);
14083 	soacount = message_count(msg, DNS_SECTION_AUTHORITY, dns_rdatatype_soa);
14084 
14085 	/*
14086 	 * There should not be a CNAME record at top of zone.
14087 	 */
14088 	if (cnamecnt != 0) {
14089 		dns_zone_log(zone, ISC_LOG_INFO,
14090 			     "refresh: CNAME at top of zone "
14091 			     "in master %s (source %s)",
14092 			     master, source);
14093 		goto next_master;
14094 	}
14095 
14096 	/*
14097 	 * if referral log and next master;
14098 	 */
14099 	if (soacnt == 0 && soacount == 0 && nscount != 0) {
14100 		dns_zone_log(zone, ISC_LOG_INFO,
14101 			     "refresh: referral response "
14102 			     "from master %s (source %s)",
14103 			     master, source);
14104 		goto next_master;
14105 	}
14106 
14107 	/*
14108 	 * if nodata log and next master;
14109 	 */
14110 	if (soacnt == 0 && (nscount == 0 || soacount != 0)) {
14111 		dns_zone_log(zone, ISC_LOG_INFO,
14112 			     "refresh: NODATA response "
14113 			     "from master %s (source %s)",
14114 			     master, source);
14115 		goto next_master;
14116 	}
14117 
14118 	/*
14119 	 * Only one soa at top of zone.
14120 	 */
14121 	if (soacnt != 1) {
14122 		dns_zone_log(zone, ISC_LOG_INFO,
14123 			     "refresh: answer SOA count (%d) != 1 "
14124 			     "from master %s (source %s)",
14125 			     soacnt, master, source);
14126 		goto next_master;
14127 	}
14128 
14129 	/*
14130 	 * Extract serial
14131 	 */
14132 	rdataset = NULL;
14133 	result = dns_message_findname(msg, DNS_SECTION_ANSWER, &zone->origin,
14134 				      dns_rdatatype_soa, dns_rdatatype_none,
14135 				      NULL, &rdataset);
14136 	if (result != ISC_R_SUCCESS) {
14137 		dns_zone_log(zone, ISC_LOG_INFO,
14138 			     "refresh: unable to get SOA record "
14139 			     "from master %s (source %s)",
14140 			     master, source);
14141 		goto next_master;
14142 	}
14143 
14144 	result = dns_rdataset_first(rdataset);
14145 	if (result != ISC_R_SUCCESS) {
14146 		dns_zone_log(zone, ISC_LOG_INFO,
14147 			     "refresh: dns_rdataset_first() failed");
14148 		goto next_master;
14149 	}
14150 
14151 	dns_rdataset_current(rdataset, &rdata);
14152 	result = dns_rdata_tostruct(&rdata, &soa, NULL);
14153 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
14154 
14155 	serial = soa.serial;
14156 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) {
14157 		unsigned int dbsoacount;
14158 		result = zone_get_from_db(zone, zone->db, NULL, &dbsoacount,
14159 					  NULL, &oldserial, NULL, NULL, NULL,
14160 					  NULL, NULL);
14161 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
14162 		RUNTIME_CHECK(dbsoacount > 0U);
14163 		zone_debuglog(zone, me, 1, "serial: new %u, old %u", serial,
14164 			      oldserial);
14165 	} else {
14166 		zone_debuglog(zone, me, 1, "serial: new %u, old not loaded",
14167 			      serial);
14168 	}
14169 
14170 	if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) ||
14171 	    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER) ||
14172 	    isc_serial_gt(serial, oldserial))
14173 	{
14174 		if (dns_zonemgr_unreachable(zone->zmgr, &zone->masteraddr,
14175 					    &zone->sourceaddr, &now))
14176 		{
14177 			dns_zone_log(zone, ISC_LOG_INFO,
14178 				     "refresh: skipping %s as master %s "
14179 				     "(source %s) is unreachable (cached)",
14180 				     (zone->type == dns_zone_secondary ||
14181 				      zone->type == dns_zone_mirror ||
14182 				      zone->type == dns_zone_redirect)
14183 					     ? "zone transfer"
14184 					     : "NS query",
14185 				     master, source);
14186 			goto next_master;
14187 		}
14188 	tcp_transfer:
14189 		isc_event_free(&event);
14190 		dns_request_destroy(&zone->request);
14191 		if (zone->type == dns_zone_secondary ||
14192 		    zone->type == dns_zone_mirror ||
14193 		    zone->type == dns_zone_redirect)
14194 		{
14195 			do_queue_xfrin = true;
14196 		} else {
14197 			INSIST(zone->type == dns_zone_stub);
14198 			ns_query(zone, rdataset, NULL);
14199 		}
14200 		if (msg != NULL) {
14201 			dns_message_detach(&msg);
14202 		}
14203 	} else if (isc_serial_eq(soa.serial, oldserial)) {
14204 		isc_time_t expiretime;
14205 		uint32_t expire;
14206 
14207 		/*
14208 		 * Compute the new expire time based on this response.
14209 		 */
14210 		expire = zone->expire;
14211 		get_edns_expire(zone, msg, &expire);
14212 		DNS_ZONE_TIME_ADD(&now, expire, &expiretime);
14213 
14214 		/*
14215 		 * Has the expire time improved?
14216 		 */
14217 		if (isc_time_compare(&expiretime, &zone->expiretime) > 0) {
14218 			zone->expiretime = expiretime;
14219 			if (zone->masterfile != NULL) {
14220 				setmodtime(zone, &expiretime);
14221 			}
14222 		}
14223 
14224 		DNS_ZONE_JITTER_ADD(&now, zone->refresh, &zone->refreshtime);
14225 		zone->mastersok[zone->curmaster] = true;
14226 		goto next_master;
14227 	} else {
14228 		if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_MULTIMASTER)) {
14229 			dns_zone_log(zone, ISC_LOG_INFO,
14230 				     "serial number (%u) "
14231 				     "received from master %s < ours (%u)",
14232 				     soa.serial, master, oldserial);
14233 		} else {
14234 			zone_debuglog(zone, me, 1, "ahead");
14235 		}
14236 		zone->mastersok[zone->curmaster] = true;
14237 		goto next_master;
14238 	}
14239 	if (msg != NULL) {
14240 		dns_message_detach(&msg);
14241 	}
14242 	goto detach;
14243 
14244 next_master:
14245 	if (msg != NULL) {
14246 		dns_message_detach(&msg);
14247 	}
14248 	isc_event_free(&event);
14249 	dns_request_destroy(&zone->request);
14250 	/*
14251 	 * Skip to next failed / untried master.
14252 	 */
14253 	do {
14254 		zone->curmaster++;
14255 	} while (zone->curmaster < zone->masterscnt &&
14256 		 zone->mastersok[zone->curmaster]);
14257 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOEDNS);
14258 	if (zone->curmaster >= zone->masterscnt) {
14259 		bool done = true;
14260 		if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_USEALTXFRSRC) &&
14261 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEALTXFRSRC))
14262 		{
14263 			/*
14264 			 * Did we get a good answer from all the primaries?
14265 			 */
14266 			for (j = 0; j < zone->masterscnt; j++) {
14267 				if (!zone->mastersok[j]) {
14268 					{
14269 						done = false;
14270 						break;
14271 					}
14272 				}
14273 			}
14274 		} else {
14275 			done = true;
14276 		}
14277 		if (!done) {
14278 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_USEALTXFRSRC);
14279 			zone->curmaster = 0;
14280 			/*
14281 			 * Find the next failed master.
14282 			 */
14283 			while (zone->curmaster < zone->masterscnt &&
14284 			       zone->mastersok[zone->curmaster]) {
14285 				zone->curmaster++;
14286 			}
14287 			goto requeue;
14288 		}
14289 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
14290 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDREFRESH)) {
14291 			DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDREFRESH);
14292 			zone->refreshtime = now;
14293 		}
14294 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_USEALTXFRSRC);
14295 		zone_settimer(zone, &now);
14296 		goto detach;
14297 	}
14298 
14299 requeue:
14300 	queue_soa_query(zone);
14301 	goto detach;
14302 
14303 same_master:
14304 	if (msg != NULL) {
14305 		dns_message_detach(&msg);
14306 	}
14307 	isc_event_free(&event);
14308 	dns_request_destroy(&zone->request);
14309 	queue_soa_query(zone);
14310 
14311 detach:
14312 	UNLOCK_ZONE(zone);
14313 	if (do_queue_xfrin) {
14314 		queue_xfrin(zone);
14315 	}
14316 	dns_zone_idetach(&zone);
14317 	return;
14318 }
14319 
14320 static void
queue_soa_query(dns_zone_t * zone)14321 queue_soa_query(dns_zone_t *zone) {
14322 	const char me[] = "queue_soa_query";
14323 	isc_event_t *e;
14324 	dns_zone_t *dummy = NULL;
14325 	isc_result_t result;
14326 
14327 	ENTER;
14328 	/*
14329 	 * Locked by caller
14330 	 */
14331 	REQUIRE(LOCKED_ZONE(zone));
14332 
14333 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
14334 		cancel_refresh(zone);
14335 		return;
14336 	}
14337 
14338 	e = isc_event_allocate(zone->mctx, NULL, DNS_EVENT_ZONE, soa_query,
14339 			       zone, sizeof(isc_event_t));
14340 
14341 	/*
14342 	 * Attach so that we won't clean up
14343 	 * until the event is delivered.
14344 	 */
14345 	zone_iattach(zone, &dummy);
14346 
14347 	e->ev_arg = zone;
14348 	e->ev_sender = NULL;
14349 	result = isc_ratelimiter_enqueue(zone->zmgr->refreshrl, zone->task, &e);
14350 	if (result != ISC_R_SUCCESS) {
14351 		zone_idetach(&dummy);
14352 		isc_event_free(&e);
14353 		cancel_refresh(zone);
14354 	}
14355 }
14356 
14357 static void
soa_query(isc_task_t * task,isc_event_t * event)14358 soa_query(isc_task_t *task, isc_event_t *event) {
14359 	const char me[] = "soa_query";
14360 	isc_result_t result = ISC_R_FAILURE;
14361 	dns_message_t *message = NULL;
14362 	dns_zone_t *zone = event->ev_arg;
14363 	dns_zone_t *dummy = NULL;
14364 	isc_netaddr_t masterip;
14365 	dns_tsigkey_t *key = NULL;
14366 	uint32_t options;
14367 	bool cancel = true;
14368 	int timeout;
14369 	bool have_xfrsource, have_xfrdscp, reqnsid, reqexpire;
14370 	uint16_t udpsize = SEND_BUFFER_SIZE;
14371 	isc_dscp_t dscp = -1;
14372 
14373 	REQUIRE(DNS_ZONE_VALID(zone));
14374 
14375 	UNUSED(task);
14376 
14377 	ENTER;
14378 
14379 	LOCK_ZONE(zone);
14380 	if (((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0) ||
14381 	    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING) ||
14382 	    zone->view->requestmgr == NULL)
14383 	{
14384 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
14385 			cancel = false;
14386 		}
14387 		goto cleanup;
14388 	}
14389 
14390 again:
14391 	result = create_query(zone, dns_rdatatype_soa, &zone->origin, &message);
14392 	if (result != ISC_R_SUCCESS) {
14393 		goto cleanup;
14394 	}
14395 
14396 	INSIST(zone->masterscnt > 0);
14397 	INSIST(zone->curmaster < zone->masterscnt);
14398 
14399 	zone->masteraddr = zone->masters[zone->curmaster];
14400 
14401 	isc_netaddr_fromsockaddr(&masterip, &zone->masteraddr);
14402 	/*
14403 	 * First, look for a tsig key in the master statement, then
14404 	 * try for a server key.
14405 	 */
14406 	if ((zone->masterkeynames != NULL) &&
14407 	    (zone->masterkeynames[zone->curmaster] != NULL))
14408 	{
14409 		dns_view_t *view = dns_zone_getview(zone);
14410 		dns_name_t *keyname = zone->masterkeynames[zone->curmaster];
14411 		result = dns_view_gettsig(view, keyname, &key);
14412 		if (result != ISC_R_SUCCESS) {
14413 			char namebuf[DNS_NAME_FORMATSIZE];
14414 			dns_name_format(keyname, namebuf, sizeof(namebuf));
14415 			dns_zone_log(zone, ISC_LOG_ERROR,
14416 				     "unable to find key: %s", namebuf);
14417 			goto skip_master;
14418 		}
14419 	}
14420 	if (key == NULL) {
14421 		result = dns_view_getpeertsig(zone->view, &masterip, &key);
14422 		if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
14423 			char addrbuf[ISC_NETADDR_FORMATSIZE];
14424 			isc_netaddr_format(&masterip, addrbuf, sizeof(addrbuf));
14425 			dns_zone_log(zone, ISC_LOG_ERROR,
14426 				     "unable to find TSIG key for %s", addrbuf);
14427 			goto skip_master;
14428 		}
14429 	}
14430 
14431 	options = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEVC) ? DNS_REQUESTOPT_TCP
14432 							 : 0;
14433 	have_xfrsource = have_xfrdscp = false;
14434 	reqnsid = zone->view->requestnsid;
14435 	reqexpire = zone->requestexpire;
14436 	if (zone->view->peers != NULL) {
14437 		dns_peer_t *peer = NULL;
14438 		bool edns, usetcp;
14439 		result = dns_peerlist_peerbyaddr(zone->view->peers, &masterip,
14440 						 &peer);
14441 		if (result == ISC_R_SUCCESS) {
14442 			result = dns_peer_getsupportedns(peer, &edns);
14443 			if (result == ISC_R_SUCCESS && !edns) {
14444 				DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
14445 			}
14446 			result = dns_peer_gettransfersource(peer,
14447 							    &zone->sourceaddr);
14448 			if (result == ISC_R_SUCCESS) {
14449 				have_xfrsource = true;
14450 			}
14451 			(void)dns_peer_gettransferdscp(peer, &dscp);
14452 			if (dscp != -1) {
14453 				have_xfrdscp = true;
14454 			}
14455 			if (zone->view->resolver != NULL) {
14456 				udpsize = dns_resolver_getudpsize(
14457 					zone->view->resolver);
14458 			}
14459 			(void)dns_peer_getudpsize(peer, &udpsize);
14460 			(void)dns_peer_getrequestnsid(peer, &reqnsid);
14461 			(void)dns_peer_getrequestexpire(peer, &reqexpire);
14462 			result = dns_peer_getforcetcp(peer, &usetcp);
14463 			if (result == ISC_R_SUCCESS && usetcp) {
14464 				options |= DNS_REQUESTOPT_TCP;
14465 			}
14466 		}
14467 	}
14468 
14469 	switch (isc_sockaddr_pf(&zone->masteraddr)) {
14470 	case PF_INET:
14471 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEALTXFRSRC)) {
14472 			if (isc_sockaddr_equal(&zone->altxfrsource4,
14473 					       &zone->xfrsource4)) {
14474 				goto skip_master;
14475 			}
14476 			zone->sourceaddr = zone->altxfrsource4;
14477 			if (!have_xfrdscp) {
14478 				dscp = zone->altxfrsource4dscp;
14479 			}
14480 		} else if (!have_xfrsource) {
14481 			zone->sourceaddr = zone->xfrsource4;
14482 			if (!have_xfrdscp) {
14483 				dscp = zone->xfrsource4dscp;
14484 			}
14485 		}
14486 		break;
14487 	case PF_INET6:
14488 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEALTXFRSRC)) {
14489 			if (isc_sockaddr_equal(&zone->altxfrsource6,
14490 					       &zone->xfrsource6)) {
14491 				goto skip_master;
14492 			}
14493 			zone->sourceaddr = zone->altxfrsource6;
14494 			if (!have_xfrdscp) {
14495 				dscp = zone->altxfrsource6dscp;
14496 			}
14497 		} else if (!have_xfrsource) {
14498 			zone->sourceaddr = zone->xfrsource6;
14499 			if (!have_xfrdscp) {
14500 				dscp = zone->xfrsource6dscp;
14501 			}
14502 		}
14503 		break;
14504 	default:
14505 		result = ISC_R_NOTIMPLEMENTED;
14506 		goto cleanup;
14507 	}
14508 
14509 	if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) {
14510 		result = add_opt(message, udpsize, reqnsid, reqexpire);
14511 		if (result != ISC_R_SUCCESS) {
14512 			zone_debuglog(zone, me, 1,
14513 				      "unable to add opt record: %s",
14514 				      dns_result_totext(result));
14515 		}
14516 	}
14517 
14518 	zone_iattach(zone, &dummy);
14519 	timeout = 15;
14520 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH)) {
14521 		timeout = 30;
14522 	}
14523 	result = dns_request_createvia(
14524 		zone->view->requestmgr, message, &zone->sourceaddr,
14525 		&zone->masteraddr, dscp, options, key, timeout * 3, timeout, 0,
14526 		zone->task, refresh_callback, zone, &zone->request);
14527 	if (result != ISC_R_SUCCESS) {
14528 		zone_idetach(&dummy);
14529 		zone_debuglog(zone, me, 1,
14530 			      "dns_request_createvia4() failed: %s",
14531 			      dns_result_totext(result));
14532 		goto skip_master;
14533 	} else {
14534 		if (isc_sockaddr_pf(&zone->masteraddr) == PF_INET) {
14535 			inc_stats(zone, dns_zonestatscounter_soaoutv4);
14536 		} else {
14537 			inc_stats(zone, dns_zonestatscounter_soaoutv6);
14538 		}
14539 	}
14540 	cancel = false;
14541 
14542 cleanup:
14543 	if (key != NULL) {
14544 		dns_tsigkey_detach(&key);
14545 	}
14546 	if (result != ISC_R_SUCCESS) {
14547 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
14548 	}
14549 	if (message != NULL) {
14550 		dns_message_detach(&message);
14551 	}
14552 	if (cancel) {
14553 		cancel_refresh(zone);
14554 	}
14555 	isc_event_free(&event);
14556 	UNLOCK_ZONE(zone);
14557 	dns_zone_idetach(&zone);
14558 	return;
14559 
14560 skip_master:
14561 	if (key != NULL) {
14562 		dns_tsigkey_detach(&key);
14563 	}
14564 	dns_message_detach(&message);
14565 	/*
14566 	 * Skip to next failed / untried master.
14567 	 */
14568 	do {
14569 		zone->curmaster++;
14570 	} while (zone->curmaster < zone->masterscnt &&
14571 		 zone->mastersok[zone->curmaster]);
14572 	if (zone->curmaster < zone->masterscnt) {
14573 		goto again;
14574 	}
14575 	zone->curmaster = 0;
14576 	goto cleanup;
14577 }
14578 
14579 static void
ns_query(dns_zone_t * zone,dns_rdataset_t * soardataset,dns_stub_t * stub)14580 ns_query(dns_zone_t *zone, dns_rdataset_t *soardataset, dns_stub_t *stub) {
14581 	const char me[] = "ns_query";
14582 	isc_result_t result;
14583 	dns_message_t *message = NULL;
14584 	isc_netaddr_t masterip;
14585 	dns_tsigkey_t *key = NULL;
14586 	dns_dbnode_t *node = NULL;
14587 	int timeout;
14588 	bool have_xfrsource = false, have_xfrdscp = false;
14589 	bool reqnsid;
14590 	uint16_t udpsize = SEND_BUFFER_SIZE;
14591 	isc_dscp_t dscp = -1;
14592 	struct stub_cb_args *cb_args;
14593 
14594 	REQUIRE(DNS_ZONE_VALID(zone));
14595 	REQUIRE(LOCKED_ZONE(zone));
14596 	REQUIRE((soardataset != NULL && stub == NULL) ||
14597 		(soardataset == NULL && stub != NULL));
14598 	REQUIRE(stub == NULL || DNS_STUB_VALID(stub));
14599 
14600 	ENTER;
14601 
14602 	if (stub == NULL) {
14603 		stub = isc_mem_get(zone->mctx, sizeof(*stub));
14604 		stub->magic = STUB_MAGIC;
14605 		stub->mctx = zone->mctx;
14606 		stub->zone = NULL;
14607 		stub->db = NULL;
14608 		stub->version = NULL;
14609 		atomic_init(&stub->pending_requests, 0);
14610 
14611 		/*
14612 		 * Attach so that the zone won't disappear from under us.
14613 		 */
14614 		zone_iattach(zone, &stub->zone);
14615 
14616 		/*
14617 		 * If a db exists we will update it, otherwise we create a
14618 		 * new one and attach it to the zone once we have the NS
14619 		 * RRset and glue.
14620 		 */
14621 		ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
14622 		if (zone->db != NULL) {
14623 			dns_db_attach(zone->db, &stub->db);
14624 			ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
14625 		} else {
14626 			ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
14627 
14628 			INSIST(zone->db_argc >= 1);
14629 			result = dns_db_create(zone->mctx, zone->db_argv[0],
14630 					       &zone->origin, dns_dbtype_stub,
14631 					       zone->rdclass, zone->db_argc - 1,
14632 					       zone->db_argv + 1, &stub->db);
14633 			if (result != ISC_R_SUCCESS) {
14634 				dns_zone_log(zone, ISC_LOG_ERROR,
14635 					     "refreshing stub: "
14636 					     "could not create "
14637 					     "database: %s",
14638 					     dns_result_totext(result));
14639 				goto cleanup;
14640 			}
14641 			dns_db_settask(stub->db, zone->task);
14642 		}
14643 
14644 		result = dns_db_newversion(stub->db, &stub->version);
14645 		if (result != ISC_R_SUCCESS) {
14646 			dns_zone_log(zone, ISC_LOG_INFO,
14647 				     "refreshing stub: "
14648 				     "dns_db_newversion() failed: %s",
14649 				     dns_result_totext(result));
14650 			goto cleanup;
14651 		}
14652 
14653 		/*
14654 		 * Update SOA record.
14655 		 */
14656 		result = dns_db_findnode(stub->db, &zone->origin, true, &node);
14657 		if (result != ISC_R_SUCCESS) {
14658 			dns_zone_log(zone, ISC_LOG_INFO,
14659 				     "refreshing stub: "
14660 				     "dns_db_findnode() failed: %s",
14661 				     dns_result_totext(result));
14662 			goto cleanup;
14663 		}
14664 
14665 		result = dns_db_addrdataset(stub->db, node, stub->version, 0,
14666 					    soardataset, 0, NULL);
14667 		dns_db_detachnode(stub->db, &node);
14668 		if (result != ISC_R_SUCCESS) {
14669 			dns_zone_log(zone, ISC_LOG_INFO,
14670 				     "refreshing stub: "
14671 				     "dns_db_addrdataset() failed: %s",
14672 				     dns_result_totext(result));
14673 			goto cleanup;
14674 		}
14675 	}
14676 
14677 	/*
14678 	 * XXX Optimisation: Create message when zone is setup and reuse.
14679 	 */
14680 	result = create_query(zone, dns_rdatatype_ns, &zone->origin, &message);
14681 	INSIST(result == ISC_R_SUCCESS);
14682 
14683 	INSIST(zone->masterscnt > 0);
14684 	INSIST(zone->curmaster < zone->masterscnt);
14685 	zone->masteraddr = zone->masters[zone->curmaster];
14686 
14687 	isc_netaddr_fromsockaddr(&masterip, &zone->masteraddr);
14688 	/*
14689 	 * First, look for a tsig key in the master statement, then
14690 	 * try for a server key.
14691 	 */
14692 	if ((zone->masterkeynames != NULL) &&
14693 	    (zone->masterkeynames[zone->curmaster] != NULL))
14694 	{
14695 		dns_view_t *view = dns_zone_getview(zone);
14696 		dns_name_t *keyname = zone->masterkeynames[zone->curmaster];
14697 		result = dns_view_gettsig(view, keyname, &key);
14698 		if (result != ISC_R_SUCCESS) {
14699 			char namebuf[DNS_NAME_FORMATSIZE];
14700 			dns_name_format(keyname, namebuf, sizeof(namebuf));
14701 			dns_zone_log(zone, ISC_LOG_ERROR,
14702 				     "unable to find key: %s", namebuf);
14703 		}
14704 	}
14705 	if (key == NULL) {
14706 		(void)dns_view_getpeertsig(zone->view, &masterip, &key);
14707 	}
14708 
14709 	reqnsid = zone->view->requestnsid;
14710 	if (zone->view->peers != NULL) {
14711 		dns_peer_t *peer = NULL;
14712 		bool edns;
14713 		result = dns_peerlist_peerbyaddr(zone->view->peers, &masterip,
14714 						 &peer);
14715 		if (result == ISC_R_SUCCESS) {
14716 			result = dns_peer_getsupportedns(peer, &edns);
14717 			if (result == ISC_R_SUCCESS && !edns) {
14718 				DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS);
14719 			}
14720 			result = dns_peer_gettransfersource(peer,
14721 							    &zone->sourceaddr);
14722 			if (result == ISC_R_SUCCESS) {
14723 				have_xfrsource = true;
14724 			}
14725 			result = dns_peer_gettransferdscp(peer, &dscp);
14726 			if (result == ISC_R_SUCCESS && dscp != -1) {
14727 				have_xfrdscp = true;
14728 			}
14729 			if (zone->view->resolver != NULL) {
14730 				udpsize = dns_resolver_getudpsize(
14731 					zone->view->resolver);
14732 			}
14733 			(void)dns_peer_getudpsize(peer, &udpsize);
14734 			(void)dns_peer_getrequestnsid(peer, &reqnsid);
14735 		}
14736 	}
14737 	if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) {
14738 		result = add_opt(message, udpsize, reqnsid, false);
14739 		if (result != ISC_R_SUCCESS) {
14740 			zone_debuglog(zone, me, 1,
14741 				      "unable to add opt record: %s",
14742 				      dns_result_totext(result));
14743 		}
14744 	}
14745 
14746 	/*
14747 	 * Always use TCP so that we shouldn't truncate in additional section.
14748 	 */
14749 	switch (isc_sockaddr_pf(&zone->masteraddr)) {
14750 	case PF_INET:
14751 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEALTXFRSRC)) {
14752 			zone->sourceaddr = zone->altxfrsource4;
14753 			if (!have_xfrdscp) {
14754 				dscp = zone->altxfrsource4dscp;
14755 			}
14756 		} else if (!have_xfrsource) {
14757 			zone->sourceaddr = zone->xfrsource4;
14758 			if (!have_xfrdscp) {
14759 				dscp = zone->xfrsource4dscp;
14760 			}
14761 		}
14762 		break;
14763 	case PF_INET6:
14764 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEALTXFRSRC)) {
14765 			zone->sourceaddr = zone->altxfrsource6;
14766 			if (!have_xfrdscp) {
14767 				dscp = zone->altxfrsource6dscp;
14768 			}
14769 		} else if (!have_xfrsource) {
14770 			zone->sourceaddr = zone->xfrsource6;
14771 			if (!have_xfrdscp) {
14772 				dscp = zone->xfrsource6dscp;
14773 			}
14774 		}
14775 		break;
14776 	default:
14777 		result = ISC_R_NOTIMPLEMENTED;
14778 		POST(result);
14779 		goto cleanup;
14780 	}
14781 	timeout = 15;
14782 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH)) {
14783 		timeout = 30;
14784 	}
14785 
14786 	/*
14787 	 * Save request parameters so we can reuse them later on
14788 	 * for resolving missing glue A/AAAA records.
14789 	 */
14790 	cb_args = isc_mem_get(zone->mctx, sizeof(*cb_args));
14791 	cb_args->stub = stub;
14792 	cb_args->tsig_key = key;
14793 	cb_args->dscp = dscp;
14794 	cb_args->udpsize = udpsize;
14795 	cb_args->timeout = timeout;
14796 	cb_args->reqnsid = reqnsid;
14797 
14798 	result = dns_request_createvia(
14799 		zone->view->requestmgr, message, &zone->sourceaddr,
14800 		&zone->masteraddr, dscp, DNS_REQUESTOPT_TCP, key, timeout * 3,
14801 		timeout, 0, zone->task, stub_callback, cb_args, &zone->request);
14802 	if (result != ISC_R_SUCCESS) {
14803 		zone_debuglog(zone, me, 1, "dns_request_createvia() failed: %s",
14804 			      dns_result_totext(result));
14805 		goto cleanup;
14806 	}
14807 	dns_message_detach(&message);
14808 	goto unlock;
14809 
14810 cleanup:
14811 	cancel_refresh(zone);
14812 	stub->magic = 0;
14813 	if (stub->version != NULL) {
14814 		dns_db_closeversion(stub->db, &stub->version, false);
14815 	}
14816 	if (stub->db != NULL) {
14817 		dns_db_detach(&stub->db);
14818 	}
14819 	if (stub->zone != NULL) {
14820 		zone_idetach(&stub->zone);
14821 	}
14822 	isc_mem_put(stub->mctx, stub, sizeof(*stub));
14823 	if (message != NULL) {
14824 		dns_message_detach(&message);
14825 	}
14826 unlock:
14827 	if (key != NULL) {
14828 		dns_tsigkey_detach(&key);
14829 	}
14830 	return;
14831 }
14832 
14833 /*
14834  * Handle the control event.  Note that although this event causes the zone
14835  * to shut down, it is not a shutdown event in the sense of the task library.
14836  */
14837 static void
zone_shutdown(isc_task_t * task,isc_event_t * event)14838 zone_shutdown(isc_task_t *task, isc_event_t *event) {
14839 	dns_zone_t *zone = (dns_zone_t *)event->ev_arg;
14840 	bool free_needed, linked = false;
14841 	dns_zone_t *raw = NULL, *secure = NULL;
14842 
14843 	UNUSED(task);
14844 	REQUIRE(DNS_ZONE_VALID(zone));
14845 	INSIST(event->ev_type == DNS_EVENT_ZONECONTROL);
14846 	INSIST(isc_refcount_current(&zone->erefs) == 0);
14847 
14848 	zone_debuglog(zone, "zone_shutdown", 3, "shutting down");
14849 
14850 	/*
14851 	 * Stop things being restarted after we cancel them below.
14852 	 */
14853 	LOCK_ZONE(zone);
14854 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_EXITING);
14855 	UNLOCK_ZONE(zone);
14856 
14857 	/*
14858 	 * If we were waiting for xfrin quota, step out of
14859 	 * the queue.
14860 	 * If there's no zone manager, we can't be waiting for the
14861 	 * xfrin quota
14862 	 */
14863 	if (zone->zmgr != NULL) {
14864 		RWLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write);
14865 		if (zone->statelist == &zone->zmgr->waiting_for_xfrin) {
14866 			ISC_LIST_UNLINK(zone->zmgr->waiting_for_xfrin, zone,
14867 					statelink);
14868 			linked = true;
14869 			zone->statelist = NULL;
14870 		}
14871 		if (zone->statelist == &zone->zmgr->xfrin_in_progress) {
14872 			ISC_LIST_UNLINK(zone->zmgr->xfrin_in_progress, zone,
14873 					statelink);
14874 			zone->statelist = NULL;
14875 			zmgr_resume_xfrs(zone->zmgr, false);
14876 		}
14877 		RWUNLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write);
14878 	}
14879 
14880 	/*
14881 	 * In task context, no locking required.  See zone_xfrdone().
14882 	 */
14883 	if (zone->xfr != NULL) {
14884 		dns_xfrin_shutdown(zone->xfr);
14885 	}
14886 
14887 	/* Safe to release the zone now */
14888 	if (zone->zmgr != NULL) {
14889 		dns_zonemgr_releasezone(zone->zmgr, zone);
14890 	}
14891 
14892 	LOCK_ZONE(zone);
14893 	INSIST(zone != zone->raw);
14894 	if (linked) {
14895 		isc_refcount_decrement(&zone->irefs);
14896 	}
14897 	if (zone->request != NULL) {
14898 		dns_request_cancel(zone->request);
14899 	}
14900 
14901 	if (zone->readio != NULL) {
14902 		zonemgr_cancelio(zone->readio);
14903 	}
14904 
14905 	if (zone->lctx != NULL) {
14906 		dns_loadctx_cancel(zone->lctx);
14907 	}
14908 
14909 	if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) ||
14910 	    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING))
14911 	{
14912 		if (zone->writeio != NULL) {
14913 			zonemgr_cancelio(zone->writeio);
14914 		}
14915 
14916 		if (zone->dctx != NULL) {
14917 			dns_dumpctx_cancel(zone->dctx);
14918 		}
14919 	}
14920 
14921 	checkds_cancel(zone);
14922 
14923 	notify_cancel(zone);
14924 
14925 	forward_cancel(zone);
14926 
14927 	if (zone->timer != NULL) {
14928 		isc_timer_detach(&zone->timer);
14929 		isc_refcount_decrement(&zone->irefs);
14930 	}
14931 
14932 	/*
14933 	 * We have now canceled everything set the flag to allow exit_check()
14934 	 * to succeed.	We must not unlock between setting this flag and
14935 	 * calling exit_check().
14936 	 */
14937 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_SHUTDOWN);
14938 	free_needed = exit_check(zone);
14939 	/*
14940 	 * If a dump is in progress for the secure zone, defer detaching from
14941 	 * the raw zone as it may prevent the unsigned serial number from being
14942 	 * stored in the raw-format dump of the secure zone.  In this scenario,
14943 	 * dump_done() takes care of cleaning up the zone->raw reference.
14944 	 */
14945 	if (inline_secure(zone) && !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) {
14946 		raw = zone->raw;
14947 		zone->raw = NULL;
14948 	}
14949 	if (inline_raw(zone)) {
14950 		secure = zone->secure;
14951 		zone->secure = NULL;
14952 	}
14953 	UNLOCK_ZONE(zone);
14954 	if (raw != NULL) {
14955 		dns_zone_detach(&raw);
14956 	}
14957 	if (secure != NULL) {
14958 		dns_zone_idetach(&secure);
14959 	}
14960 	if (free_needed) {
14961 		zone_free(zone);
14962 	}
14963 }
14964 
14965 static void
zone_timer(isc_task_t * task,isc_event_t * event)14966 zone_timer(isc_task_t *task, isc_event_t *event) {
14967 	const char me[] = "zone_timer";
14968 	dns_zone_t *zone = (dns_zone_t *)event->ev_arg;
14969 
14970 	UNUSED(task);
14971 	REQUIRE(DNS_ZONE_VALID(zone));
14972 
14973 	ENTER;
14974 
14975 	zone_maintenance(zone);
14976 
14977 	isc_event_free(&event);
14978 }
14979 
14980 static void
zone_settimer(dns_zone_t * zone,isc_time_t * now)14981 zone_settimer(dns_zone_t *zone, isc_time_t *now) {
14982 	const char me[] = "zone_settimer";
14983 	isc_time_t next;
14984 	isc_result_t result;
14985 
14986 	REQUIRE(DNS_ZONE_VALID(zone));
14987 	REQUIRE(LOCKED_ZONE(zone));
14988 	ENTER;
14989 
14990 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
14991 		return;
14992 	}
14993 
14994 	isc_time_settoepoch(&next);
14995 
14996 	switch (zone->type) {
14997 	case dns_zone_redirect:
14998 		if (zone->masters != NULL) {
14999 			goto treat_as_slave;
15000 		}
15001 		/* FALLTHROUGH */
15002 
15003 	case dns_zone_primary:
15004 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY) ||
15005 		    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDSTARTUPNOTIFY))
15006 		{
15007 			next = zone->notifytime;
15008 		}
15009 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
15010 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING))
15011 		{
15012 			INSIST(!isc_time_isepoch(&zone->dumptime));
15013 			if (isc_time_isepoch(&next) ||
15014 			    isc_time_compare(&zone->dumptime, &next) < 0) {
15015 				next = zone->dumptime;
15016 			}
15017 		}
15018 		if (zone->type == dns_zone_redirect) {
15019 			break;
15020 		}
15021 		if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESHING) &&
15022 		    !isc_time_isepoch(&zone->refreshkeytime))
15023 		{
15024 			if (isc_time_isepoch(&next) ||
15025 			    isc_time_compare(&zone->refreshkeytime, &next) < 0)
15026 			{
15027 				next = zone->refreshkeytime;
15028 			}
15029 		}
15030 		if (!isc_time_isepoch(&zone->resigntime)) {
15031 			if (isc_time_isepoch(&next) ||
15032 			    isc_time_compare(&zone->resigntime, &next) < 0) {
15033 				next = zone->resigntime;
15034 			}
15035 		}
15036 		if (!isc_time_isepoch(&zone->keywarntime)) {
15037 			if (isc_time_isepoch(&next) ||
15038 			    isc_time_compare(&zone->keywarntime, &next) < 0) {
15039 				next = zone->keywarntime;
15040 			}
15041 		}
15042 		if (!isc_time_isepoch(&zone->signingtime)) {
15043 			if (isc_time_isepoch(&next) ||
15044 			    isc_time_compare(&zone->signingtime, &next) < 0) {
15045 				next = zone->signingtime;
15046 			}
15047 		}
15048 		if (!isc_time_isepoch(&zone->nsec3chaintime)) {
15049 			if (isc_time_isepoch(&next) ||
15050 			    isc_time_compare(&zone->nsec3chaintime, &next) < 0)
15051 			{
15052 				next = zone->nsec3chaintime;
15053 			}
15054 		}
15055 		break;
15056 
15057 	case dns_zone_secondary:
15058 	case dns_zone_mirror:
15059 	treat_as_slave:
15060 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY) ||
15061 		    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDSTARTUPNOTIFY))
15062 		{
15063 			next = zone->notifytime;
15064 		}
15065 		/* FALLTHROUGH */
15066 
15067 	case dns_zone_stub:
15068 		if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH) &&
15069 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOMASTERS) &&
15070 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOREFRESH) &&
15071 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADING) &&
15072 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING) &&
15073 		    !isc_time_isepoch(&zone->refreshtime) &&
15074 		    (isc_time_isepoch(&next) ||
15075 		     isc_time_compare(&zone->refreshtime, &next) < 0))
15076 		{
15077 			next = zone->refreshtime;
15078 		}
15079 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) &&
15080 		    !isc_time_isepoch(&zone->expiretime))
15081 		{
15082 			if (isc_time_isepoch(&next) ||
15083 			    isc_time_compare(&zone->expiretime, &next) < 0) {
15084 				next = zone->expiretime;
15085 			}
15086 		}
15087 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
15088 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING))
15089 		{
15090 			INSIST(!isc_time_isepoch(&zone->dumptime));
15091 			if (isc_time_isepoch(&next) ||
15092 			    isc_time_compare(&zone->dumptime, &next) < 0) {
15093 				next = zone->dumptime;
15094 			}
15095 		}
15096 		break;
15097 
15098 	case dns_zone_key:
15099 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) &&
15100 		    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING))
15101 		{
15102 			INSIST(!isc_time_isepoch(&zone->dumptime));
15103 			if (isc_time_isepoch(&next) ||
15104 			    isc_time_compare(&zone->dumptime, &next) < 0) {
15105 				next = zone->dumptime;
15106 			}
15107 		}
15108 		if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESHING)) {
15109 			if (isc_time_isepoch(&next) ||
15110 			    (!isc_time_isepoch(&zone->refreshkeytime) &&
15111 			     isc_time_compare(&zone->refreshkeytime, &next) <
15112 				     0))
15113 			{
15114 				next = zone->refreshkeytime;
15115 			}
15116 		}
15117 		break;
15118 
15119 	default:
15120 		break;
15121 	}
15122 
15123 	if (isc_time_isepoch(&next)) {
15124 		zone_debuglog(zone, me, 10, "settimer inactive");
15125 		result = isc_timer_reset(zone->timer, isc_timertype_inactive,
15126 					 NULL, NULL, true);
15127 		if (result != ISC_R_SUCCESS) {
15128 			dns_zone_log(zone, ISC_LOG_ERROR,
15129 				     "could not deactivate zone timer: %s",
15130 				     isc_result_totext(result));
15131 		}
15132 	} else {
15133 		if (isc_time_compare(&next, now) <= 0) {
15134 			next = *now;
15135 		}
15136 		result = isc_timer_reset(zone->timer, isc_timertype_once, &next,
15137 					 NULL, true);
15138 		if (result != ISC_R_SUCCESS) {
15139 			dns_zone_log(zone, ISC_LOG_ERROR,
15140 				     "could not reset zone timer: %s",
15141 				     isc_result_totext(result));
15142 		}
15143 	}
15144 }
15145 
15146 static void
cancel_refresh(dns_zone_t * zone)15147 cancel_refresh(dns_zone_t *zone) {
15148 	const char me[] = "cancel_refresh";
15149 	isc_time_t now;
15150 
15151 	/*
15152 	 * 'zone' locked by caller.
15153 	 */
15154 
15155 	REQUIRE(DNS_ZONE_VALID(zone));
15156 	REQUIRE(LOCKED_ZONE(zone));
15157 
15158 	ENTER;
15159 
15160 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
15161 	TIME_NOW(&now);
15162 	zone_settimer(zone, &now);
15163 }
15164 
15165 static isc_result_t
notify_createmessage(dns_zone_t * zone,unsigned int flags,dns_message_t ** messagep)15166 notify_createmessage(dns_zone_t *zone, unsigned int flags,
15167 		     dns_message_t **messagep) {
15168 	dns_db_t *zonedb = NULL;
15169 	dns_dbnode_t *node = NULL;
15170 	dns_dbversion_t *version = NULL;
15171 	dns_message_t *message = NULL;
15172 	dns_rdataset_t rdataset;
15173 	dns_rdata_t rdata = DNS_RDATA_INIT;
15174 
15175 	dns_name_t *tempname = NULL;
15176 	dns_rdata_t *temprdata = NULL;
15177 	dns_rdatalist_t *temprdatalist = NULL;
15178 	dns_rdataset_t *temprdataset = NULL;
15179 
15180 	isc_result_t result;
15181 	isc_region_t r;
15182 	isc_buffer_t *b = NULL;
15183 
15184 	REQUIRE(DNS_ZONE_VALID(zone));
15185 	REQUIRE(messagep != NULL && *messagep == NULL);
15186 
15187 	dns_message_create(zone->mctx, DNS_MESSAGE_INTENTRENDER, &message);
15188 
15189 	message->opcode = dns_opcode_notify;
15190 	message->flags |= DNS_MESSAGEFLAG_AA;
15191 	message->rdclass = zone->rdclass;
15192 
15193 	result = dns_message_gettempname(message, &tempname);
15194 	if (result != ISC_R_SUCCESS) {
15195 		goto cleanup;
15196 	}
15197 
15198 	result = dns_message_gettemprdataset(message, &temprdataset);
15199 	if (result != ISC_R_SUCCESS) {
15200 		goto cleanup;
15201 	}
15202 
15203 	/*
15204 	 * Make question.
15205 	 */
15206 	dns_name_clone(&zone->origin, tempname);
15207 	dns_rdataset_makequestion(temprdataset, zone->rdclass,
15208 				  dns_rdatatype_soa);
15209 	ISC_LIST_APPEND(tempname->list, temprdataset, link);
15210 	dns_message_addname(message, tempname, DNS_SECTION_QUESTION);
15211 	tempname = NULL;
15212 	temprdataset = NULL;
15213 
15214 	if ((flags & DNS_NOTIFY_NOSOA) != 0) {
15215 		goto done;
15216 	}
15217 
15218 	result = dns_message_gettempname(message, &tempname);
15219 	if (result != ISC_R_SUCCESS) {
15220 		goto soa_cleanup;
15221 	}
15222 	result = dns_message_gettemprdata(message, &temprdata);
15223 	if (result != ISC_R_SUCCESS) {
15224 		goto soa_cleanup;
15225 	}
15226 	result = dns_message_gettemprdataset(message, &temprdataset);
15227 	if (result != ISC_R_SUCCESS) {
15228 		goto soa_cleanup;
15229 	}
15230 	result = dns_message_gettemprdatalist(message, &temprdatalist);
15231 	if (result != ISC_R_SUCCESS) {
15232 		goto soa_cleanup;
15233 	}
15234 
15235 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
15236 	INSIST(zone->db != NULL); /* XXXJT: is this assumption correct? */
15237 	dns_db_attach(zone->db, &zonedb);
15238 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
15239 
15240 	dns_name_clone(&zone->origin, tempname);
15241 	dns_db_currentversion(zonedb, &version);
15242 	result = dns_db_findnode(zonedb, tempname, false, &node);
15243 	if (result != ISC_R_SUCCESS) {
15244 		goto soa_cleanup;
15245 	}
15246 
15247 	dns_rdataset_init(&rdataset);
15248 	result = dns_db_findrdataset(zonedb, node, version, dns_rdatatype_soa,
15249 				     dns_rdatatype_none, 0, &rdataset, NULL);
15250 	if (result != ISC_R_SUCCESS) {
15251 		goto soa_cleanup;
15252 	}
15253 	result = dns_rdataset_first(&rdataset);
15254 	if (result != ISC_R_SUCCESS) {
15255 		goto soa_cleanup;
15256 	}
15257 	dns_rdataset_current(&rdataset, &rdata);
15258 	dns_rdata_toregion(&rdata, &r);
15259 	isc_buffer_allocate(zone->mctx, &b, r.length);
15260 	isc_buffer_putmem(b, r.base, r.length);
15261 	isc_buffer_usedregion(b, &r);
15262 	dns_rdata_init(temprdata);
15263 	dns_rdata_fromregion(temprdata, rdata.rdclass, rdata.type, &r);
15264 	dns_message_takebuffer(message, &b);
15265 	result = dns_rdataset_next(&rdataset);
15266 	dns_rdataset_disassociate(&rdataset);
15267 	if (result != ISC_R_NOMORE) {
15268 		goto soa_cleanup;
15269 	}
15270 	temprdatalist->rdclass = rdata.rdclass;
15271 	temprdatalist->type = rdata.type;
15272 	temprdatalist->ttl = rdataset.ttl;
15273 	ISC_LIST_APPEND(temprdatalist->rdata, temprdata, link);
15274 
15275 	result = dns_rdatalist_tordataset(temprdatalist, temprdataset);
15276 	if (result != ISC_R_SUCCESS) {
15277 		goto soa_cleanup;
15278 	}
15279 
15280 	ISC_LIST_APPEND(tempname->list, temprdataset, link);
15281 	dns_message_addname(message, tempname, DNS_SECTION_ANSWER);
15282 	temprdatalist = NULL;
15283 	temprdataset = NULL;
15284 	temprdata = NULL;
15285 	tempname = NULL;
15286 
15287 soa_cleanup:
15288 	if (node != NULL) {
15289 		dns_db_detachnode(zonedb, &node);
15290 	}
15291 	if (version != NULL) {
15292 		dns_db_closeversion(zonedb, &version, false);
15293 	}
15294 	if (zonedb != NULL) {
15295 		dns_db_detach(&zonedb);
15296 	}
15297 	if (tempname != NULL) {
15298 		dns_message_puttempname(message, &tempname);
15299 	}
15300 	if (temprdata != NULL) {
15301 		dns_message_puttemprdata(message, &temprdata);
15302 	}
15303 	if (temprdataset != NULL) {
15304 		dns_message_puttemprdataset(message, &temprdataset);
15305 	}
15306 	if (temprdatalist != NULL) {
15307 		dns_message_puttemprdatalist(message, &temprdatalist);
15308 	}
15309 
15310 done:
15311 	*messagep = message;
15312 	return (ISC_R_SUCCESS);
15313 
15314 cleanup:
15315 	if (tempname != NULL) {
15316 		dns_message_puttempname(message, &tempname);
15317 	}
15318 	if (temprdataset != NULL) {
15319 		dns_message_puttemprdataset(message, &temprdataset);
15320 	}
15321 	dns_message_detach(&message);
15322 	return (result);
15323 }
15324 
15325 isc_result_t
dns_zone_notifyreceive(dns_zone_t * zone,isc_sockaddr_t * from,isc_sockaddr_t * to,dns_message_t * msg)15326 dns_zone_notifyreceive(dns_zone_t *zone, isc_sockaddr_t *from,
15327 		       isc_sockaddr_t *to, dns_message_t *msg) {
15328 	unsigned int i;
15329 	dns_rdata_soa_t soa;
15330 	dns_rdataset_t *rdataset = NULL;
15331 	dns_rdata_t rdata = DNS_RDATA_INIT;
15332 	isc_result_t result;
15333 	char fromtext[ISC_SOCKADDR_FORMATSIZE];
15334 	int match = 0;
15335 	isc_netaddr_t netaddr;
15336 	uint32_t serial = 0;
15337 	bool have_serial = false;
15338 	dns_tsigkey_t *tsigkey;
15339 	const dns_name_t *tsig;
15340 
15341 	REQUIRE(DNS_ZONE_VALID(zone));
15342 
15343 	/*
15344 	 * If type != T_SOA return DNS_R_NOTIMP.  We don't yet support
15345 	 * ROLLOVER.
15346 	 *
15347 	 * SOA:	RFC1996
15348 	 * Check that 'from' is a valid notify source, (zone->masters).
15349 	 *	Return DNS_R_REFUSED if not.
15350 	 *
15351 	 * If the notify message contains a serial number check it
15352 	 * against the zones serial and return if <= current serial
15353 	 *
15354 	 * If a refresh check is progress, if so just record the
15355 	 * fact we received a NOTIFY and from where and return.
15356 	 * We will perform a new refresh check when the current one
15357 	 * completes. Return ISC_R_SUCCESS.
15358 	 *
15359 	 * Otherwise initiate a refresh check using 'from' as the
15360 	 * first address to check.  Return ISC_R_SUCCESS.
15361 	 */
15362 
15363 	isc_sockaddr_format(from, fromtext, sizeof(fromtext));
15364 
15365 	/*
15366 	 * Notify messages are processed by the raw zone.
15367 	 */
15368 	LOCK_ZONE(zone);
15369 	INSIST(zone != zone->raw);
15370 	if (inline_secure(zone)) {
15371 		result = dns_zone_notifyreceive(zone->raw, from, to, msg);
15372 		UNLOCK_ZONE(zone);
15373 		return (result);
15374 	}
15375 	/*
15376 	 *  We only handle NOTIFY (SOA) at the present.
15377 	 */
15378 	if (isc_sockaddr_pf(from) == PF_INET) {
15379 		inc_stats(zone, dns_zonestatscounter_notifyinv4);
15380 	} else {
15381 		inc_stats(zone, dns_zonestatscounter_notifyinv6);
15382 	}
15383 	if (msg->counts[DNS_SECTION_QUESTION] == 0 ||
15384 	    dns_message_findname(msg, DNS_SECTION_QUESTION, &zone->origin,
15385 				 dns_rdatatype_soa, dns_rdatatype_none, NULL,
15386 				 NULL) != ISC_R_SUCCESS)
15387 	{
15388 		UNLOCK_ZONE(zone);
15389 		if (msg->counts[DNS_SECTION_QUESTION] == 0) {
15390 			dns_zone_log(zone, ISC_LOG_NOTICE,
15391 				     "NOTIFY with no "
15392 				     "question section from: %s",
15393 				     fromtext);
15394 			return (DNS_R_FORMERR);
15395 		}
15396 		dns_zone_log(zone, ISC_LOG_NOTICE,
15397 			     "NOTIFY zone does not match");
15398 		return (DNS_R_NOTIMP);
15399 	}
15400 
15401 	/*
15402 	 * If we are a master zone just succeed.
15403 	 */
15404 	if (zone->type == dns_zone_primary) {
15405 		UNLOCK_ZONE(zone);
15406 		return (ISC_R_SUCCESS);
15407 	}
15408 
15409 	isc_netaddr_fromsockaddr(&netaddr, from);
15410 	for (i = 0; i < zone->masterscnt; i++) {
15411 		if (isc_sockaddr_eqaddr(from, &zone->masters[i])) {
15412 			break;
15413 		}
15414 		if (zone->view->aclenv.match_mapped &&
15415 		    IN6_IS_ADDR_V4MAPPED(&from->type.sin6.sin6_addr) &&
15416 		    isc_sockaddr_pf(&zone->masters[i]) == AF_INET)
15417 		{
15418 			isc_netaddr_t na1, na2;
15419 			isc_netaddr_fromv4mapped(&na1, &netaddr);
15420 			isc_netaddr_fromsockaddr(&na2, &zone->masters[i]);
15421 			if (isc_netaddr_equal(&na1, &na2)) {
15422 				break;
15423 			}
15424 		}
15425 	}
15426 
15427 	/*
15428 	 * Accept notify requests from non masters if they are on
15429 	 * 'zone->notify_acl'.
15430 	 */
15431 	tsigkey = dns_message_gettsigkey(msg);
15432 	tsig = dns_tsigkey_identity(tsigkey);
15433 	if (i >= zone->masterscnt && zone->notify_acl != NULL &&
15434 	    (dns_acl_match(&netaddr, tsig, zone->notify_acl,
15435 			   &zone->view->aclenv, &match,
15436 			   NULL) == ISC_R_SUCCESS) &&
15437 	    match > 0)
15438 	{
15439 		/* Accept notify. */
15440 	} else if (i >= zone->masterscnt) {
15441 		UNLOCK_ZONE(zone);
15442 		dns_zone_log(zone, ISC_LOG_INFO,
15443 			     "refused notify from non-master: %s", fromtext);
15444 		inc_stats(zone, dns_zonestatscounter_notifyrej);
15445 		return (DNS_R_REFUSED);
15446 	}
15447 
15448 	/*
15449 	 * If the zone is loaded and there are answers check the serial
15450 	 * to see if we need to do a refresh.  Do not worry about this
15451 	 * check if we are a dialup zone as we use the notify request
15452 	 * to trigger a refresh check.
15453 	 */
15454 	if (msg->counts[DNS_SECTION_ANSWER] > 0 &&
15455 	    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) &&
15456 	    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOREFRESH))
15457 	{
15458 		result = dns_message_findname(
15459 			msg, DNS_SECTION_ANSWER, &zone->origin,
15460 			dns_rdatatype_soa, dns_rdatatype_none, NULL, &rdataset);
15461 		if (result == ISC_R_SUCCESS) {
15462 			result = dns_rdataset_first(rdataset);
15463 		}
15464 		if (result == ISC_R_SUCCESS) {
15465 			uint32_t oldserial;
15466 			unsigned int soacount;
15467 
15468 			dns_rdataset_current(rdataset, &rdata);
15469 			result = dns_rdata_tostruct(&rdata, &soa, NULL);
15470 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
15471 			serial = soa.serial;
15472 			have_serial = true;
15473 			/*
15474 			 * The following should safely be performed without DB
15475 			 * lock and succeed in this context.
15476 			 */
15477 			result = zone_get_from_db(zone, zone->db, NULL,
15478 						  &soacount, NULL, &oldserial,
15479 						  NULL, NULL, NULL, NULL, NULL);
15480 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
15481 			RUNTIME_CHECK(soacount > 0U);
15482 			if (isc_serial_le(serial, oldserial)) {
15483 				dns_zone_log(zone, ISC_LOG_INFO,
15484 					     "notify from %s: "
15485 					     "zone is up to date",
15486 					     fromtext);
15487 				UNLOCK_ZONE(zone);
15488 				return (ISC_R_SUCCESS);
15489 			}
15490 		}
15491 	}
15492 
15493 	/*
15494 	 * If we got this far and there was a refresh in progress just
15495 	 * let it complete.  Record where we got the notify from so we
15496 	 * can perform a refresh check when the current one completes
15497 	 */
15498 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH)) {
15499 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDREFRESH);
15500 		zone->notifyfrom = *from;
15501 		UNLOCK_ZONE(zone);
15502 		if (have_serial) {
15503 			dns_zone_log(zone, ISC_LOG_INFO,
15504 				     "notify from %s: serial %u: refresh in "
15505 				     "progress, refresh check queued",
15506 				     fromtext, serial);
15507 		} else {
15508 			dns_zone_log(zone, ISC_LOG_INFO,
15509 				     "notify from %s: refresh in progress, "
15510 				     "refresh check queued",
15511 				     fromtext);
15512 		}
15513 		return (ISC_R_SUCCESS);
15514 	}
15515 	if (have_serial) {
15516 		dns_zone_log(zone, ISC_LOG_INFO, "notify from %s: serial %u",
15517 			     fromtext, serial);
15518 	} else {
15519 		dns_zone_log(zone, ISC_LOG_INFO, "notify from %s: no serial",
15520 			     fromtext);
15521 	}
15522 	zone->notifyfrom = *from;
15523 	UNLOCK_ZONE(zone);
15524 
15525 	if (to != NULL) {
15526 		dns_zonemgr_unreachabledel(zone->zmgr, from, to);
15527 	}
15528 	dns_zone_refresh(zone);
15529 	return (ISC_R_SUCCESS);
15530 }
15531 
15532 void
dns_zone_setnotifyacl(dns_zone_t * zone,dns_acl_t * acl)15533 dns_zone_setnotifyacl(dns_zone_t *zone, dns_acl_t *acl) {
15534 	REQUIRE(DNS_ZONE_VALID(zone));
15535 
15536 	LOCK_ZONE(zone);
15537 	if (zone->notify_acl != NULL) {
15538 		dns_acl_detach(&zone->notify_acl);
15539 	}
15540 	dns_acl_attach(acl, &zone->notify_acl);
15541 	UNLOCK_ZONE(zone);
15542 }
15543 
15544 void
dns_zone_setqueryacl(dns_zone_t * zone,dns_acl_t * acl)15545 dns_zone_setqueryacl(dns_zone_t *zone, dns_acl_t *acl) {
15546 	REQUIRE(DNS_ZONE_VALID(zone));
15547 
15548 	LOCK_ZONE(zone);
15549 	if (zone->query_acl != NULL) {
15550 		dns_acl_detach(&zone->query_acl);
15551 	}
15552 	dns_acl_attach(acl, &zone->query_acl);
15553 	UNLOCK_ZONE(zone);
15554 }
15555 
15556 void
dns_zone_setqueryonacl(dns_zone_t * zone,dns_acl_t * acl)15557 dns_zone_setqueryonacl(dns_zone_t *zone, dns_acl_t *acl) {
15558 	REQUIRE(DNS_ZONE_VALID(zone));
15559 
15560 	LOCK_ZONE(zone);
15561 	if (zone->queryon_acl != NULL) {
15562 		dns_acl_detach(&zone->queryon_acl);
15563 	}
15564 	dns_acl_attach(acl, &zone->queryon_acl);
15565 	UNLOCK_ZONE(zone);
15566 }
15567 
15568 void
dns_zone_setupdateacl(dns_zone_t * zone,dns_acl_t * acl)15569 dns_zone_setupdateacl(dns_zone_t *zone, dns_acl_t *acl) {
15570 	REQUIRE(DNS_ZONE_VALID(zone));
15571 
15572 	LOCK_ZONE(zone);
15573 	if (zone->update_acl != NULL) {
15574 		dns_acl_detach(&zone->update_acl);
15575 	}
15576 	dns_acl_attach(acl, &zone->update_acl);
15577 	UNLOCK_ZONE(zone);
15578 }
15579 
15580 void
dns_zone_setforwardacl(dns_zone_t * zone,dns_acl_t * acl)15581 dns_zone_setforwardacl(dns_zone_t *zone, dns_acl_t *acl) {
15582 	REQUIRE(DNS_ZONE_VALID(zone));
15583 
15584 	LOCK_ZONE(zone);
15585 	if (zone->forward_acl != NULL) {
15586 		dns_acl_detach(&zone->forward_acl);
15587 	}
15588 	dns_acl_attach(acl, &zone->forward_acl);
15589 	UNLOCK_ZONE(zone);
15590 }
15591 
15592 void
dns_zone_setxfracl(dns_zone_t * zone,dns_acl_t * acl)15593 dns_zone_setxfracl(dns_zone_t *zone, dns_acl_t *acl) {
15594 	REQUIRE(DNS_ZONE_VALID(zone));
15595 
15596 	LOCK_ZONE(zone);
15597 	if (zone->xfr_acl != NULL) {
15598 		dns_acl_detach(&zone->xfr_acl);
15599 	}
15600 	dns_acl_attach(acl, &zone->xfr_acl);
15601 	UNLOCK_ZONE(zone);
15602 }
15603 
15604 dns_acl_t *
dns_zone_getnotifyacl(dns_zone_t * zone)15605 dns_zone_getnotifyacl(dns_zone_t *zone) {
15606 	REQUIRE(DNS_ZONE_VALID(zone));
15607 
15608 	return (zone->notify_acl);
15609 }
15610 
15611 dns_acl_t *
dns_zone_getqueryacl(dns_zone_t * zone)15612 dns_zone_getqueryacl(dns_zone_t *zone) {
15613 	REQUIRE(DNS_ZONE_VALID(zone));
15614 
15615 	return (zone->query_acl);
15616 }
15617 
15618 dns_acl_t *
dns_zone_getqueryonacl(dns_zone_t * zone)15619 dns_zone_getqueryonacl(dns_zone_t *zone) {
15620 	REQUIRE(DNS_ZONE_VALID(zone));
15621 
15622 	return (zone->queryon_acl);
15623 }
15624 
15625 dns_acl_t *
dns_zone_getupdateacl(dns_zone_t * zone)15626 dns_zone_getupdateacl(dns_zone_t *zone) {
15627 	REQUIRE(DNS_ZONE_VALID(zone));
15628 
15629 	return (zone->update_acl);
15630 }
15631 
15632 dns_acl_t *
dns_zone_getforwardacl(dns_zone_t * zone)15633 dns_zone_getforwardacl(dns_zone_t *zone) {
15634 	REQUIRE(DNS_ZONE_VALID(zone));
15635 
15636 	return (zone->forward_acl);
15637 }
15638 
15639 dns_acl_t *
dns_zone_getxfracl(dns_zone_t * zone)15640 dns_zone_getxfracl(dns_zone_t *zone) {
15641 	REQUIRE(DNS_ZONE_VALID(zone));
15642 
15643 	return (zone->xfr_acl);
15644 }
15645 
15646 void
dns_zone_clearupdateacl(dns_zone_t * zone)15647 dns_zone_clearupdateacl(dns_zone_t *zone) {
15648 	REQUIRE(DNS_ZONE_VALID(zone));
15649 
15650 	LOCK_ZONE(zone);
15651 	if (zone->update_acl != NULL) {
15652 		dns_acl_detach(&zone->update_acl);
15653 	}
15654 	UNLOCK_ZONE(zone);
15655 }
15656 
15657 void
dns_zone_clearforwardacl(dns_zone_t * zone)15658 dns_zone_clearforwardacl(dns_zone_t *zone) {
15659 	REQUIRE(DNS_ZONE_VALID(zone));
15660 
15661 	LOCK_ZONE(zone);
15662 	if (zone->forward_acl != NULL) {
15663 		dns_acl_detach(&zone->forward_acl);
15664 	}
15665 	UNLOCK_ZONE(zone);
15666 }
15667 
15668 void
dns_zone_clearnotifyacl(dns_zone_t * zone)15669 dns_zone_clearnotifyacl(dns_zone_t *zone) {
15670 	REQUIRE(DNS_ZONE_VALID(zone));
15671 
15672 	LOCK_ZONE(zone);
15673 	if (zone->notify_acl != NULL) {
15674 		dns_acl_detach(&zone->notify_acl);
15675 	}
15676 	UNLOCK_ZONE(zone);
15677 }
15678 
15679 void
dns_zone_clearqueryacl(dns_zone_t * zone)15680 dns_zone_clearqueryacl(dns_zone_t *zone) {
15681 	REQUIRE(DNS_ZONE_VALID(zone));
15682 
15683 	LOCK_ZONE(zone);
15684 	if (zone->query_acl != NULL) {
15685 		dns_acl_detach(&zone->query_acl);
15686 	}
15687 	UNLOCK_ZONE(zone);
15688 }
15689 
15690 void
dns_zone_clearqueryonacl(dns_zone_t * zone)15691 dns_zone_clearqueryonacl(dns_zone_t *zone) {
15692 	REQUIRE(DNS_ZONE_VALID(zone));
15693 
15694 	LOCK_ZONE(zone);
15695 	if (zone->queryon_acl != NULL) {
15696 		dns_acl_detach(&zone->queryon_acl);
15697 	}
15698 	UNLOCK_ZONE(zone);
15699 }
15700 
15701 void
dns_zone_clearxfracl(dns_zone_t * zone)15702 dns_zone_clearxfracl(dns_zone_t *zone) {
15703 	REQUIRE(DNS_ZONE_VALID(zone));
15704 
15705 	LOCK_ZONE(zone);
15706 	if (zone->xfr_acl != NULL) {
15707 		dns_acl_detach(&zone->xfr_acl);
15708 	}
15709 	UNLOCK_ZONE(zone);
15710 }
15711 
15712 bool
dns_zone_getupdatedisabled(dns_zone_t * zone)15713 dns_zone_getupdatedisabled(dns_zone_t *zone) {
15714 	REQUIRE(DNS_ZONE_VALID(zone));
15715 	return (zone->update_disabled);
15716 }
15717 
15718 void
dns_zone_setupdatedisabled(dns_zone_t * zone,bool state)15719 dns_zone_setupdatedisabled(dns_zone_t *zone, bool state) {
15720 	REQUIRE(DNS_ZONE_VALID(zone));
15721 	zone->update_disabled = state;
15722 }
15723 
15724 bool
dns_zone_getzeronosoattl(dns_zone_t * zone)15725 dns_zone_getzeronosoattl(dns_zone_t *zone) {
15726 	REQUIRE(DNS_ZONE_VALID(zone));
15727 	return (zone->zero_no_soa_ttl);
15728 }
15729 
15730 void
dns_zone_setzeronosoattl(dns_zone_t * zone,bool state)15731 dns_zone_setzeronosoattl(dns_zone_t *zone, bool state) {
15732 	REQUIRE(DNS_ZONE_VALID(zone));
15733 	zone->zero_no_soa_ttl = state;
15734 }
15735 
15736 void
dns_zone_setchecknames(dns_zone_t * zone,dns_severity_t severity)15737 dns_zone_setchecknames(dns_zone_t *zone, dns_severity_t severity) {
15738 	REQUIRE(DNS_ZONE_VALID(zone));
15739 
15740 	zone->check_names = severity;
15741 }
15742 
15743 dns_severity_t
dns_zone_getchecknames(dns_zone_t * zone)15744 dns_zone_getchecknames(dns_zone_t *zone) {
15745 	REQUIRE(DNS_ZONE_VALID(zone));
15746 
15747 	return (zone->check_names);
15748 }
15749 
15750 void
dns_zone_setjournalsize(dns_zone_t * zone,int32_t size)15751 dns_zone_setjournalsize(dns_zone_t *zone, int32_t size) {
15752 	REQUIRE(DNS_ZONE_VALID(zone));
15753 
15754 	zone->journalsize = size;
15755 }
15756 
15757 int32_t
dns_zone_getjournalsize(dns_zone_t * zone)15758 dns_zone_getjournalsize(dns_zone_t *zone) {
15759 	REQUIRE(DNS_ZONE_VALID(zone));
15760 
15761 	return (zone->journalsize);
15762 }
15763 
15764 static void
zone_namerd_tostr(dns_zone_t * zone,char * buf,size_t length)15765 zone_namerd_tostr(dns_zone_t *zone, char *buf, size_t length) {
15766 	isc_result_t result = ISC_R_FAILURE;
15767 	isc_buffer_t buffer;
15768 
15769 	REQUIRE(buf != NULL);
15770 	REQUIRE(length > 1U);
15771 
15772 	/*
15773 	 * Leave space for terminating '\0'.
15774 	 */
15775 	isc_buffer_init(&buffer, buf, (unsigned int)length - 1);
15776 	if (zone->type != dns_zone_redirect && zone->type != dns_zone_key) {
15777 		if (dns_name_dynamic(&zone->origin)) {
15778 			result = dns_name_totext(&zone->origin, true, &buffer);
15779 		}
15780 		if (result != ISC_R_SUCCESS &&
15781 		    isc_buffer_availablelength(&buffer) >=
15782 			    (sizeof("<UNKNOWN>") - 1))
15783 		{
15784 			isc_buffer_putstr(&buffer, "<UNKNOWN>");
15785 		}
15786 
15787 		if (isc_buffer_availablelength(&buffer) > 0) {
15788 			isc_buffer_putstr(&buffer, "/");
15789 		}
15790 		(void)dns_rdataclass_totext(zone->rdclass, &buffer);
15791 	}
15792 
15793 	if (zone->view != NULL && strcmp(zone->view->name, "_bind") != 0 &&
15794 	    strcmp(zone->view->name, "_default") != 0 &&
15795 	    strlen(zone->view->name) < isc_buffer_availablelength(&buffer))
15796 	{
15797 		isc_buffer_putstr(&buffer, "/");
15798 		isc_buffer_putstr(&buffer, zone->view->name);
15799 	}
15800 	if (inline_secure(zone) && 9U < isc_buffer_availablelength(&buffer)) {
15801 		isc_buffer_putstr(&buffer, " (signed)");
15802 	}
15803 	if (inline_raw(zone) && 11U < isc_buffer_availablelength(&buffer)) {
15804 		isc_buffer_putstr(&buffer, " (unsigned)");
15805 	}
15806 
15807 	buf[isc_buffer_usedlength(&buffer)] = '\0';
15808 }
15809 
15810 static void
zone_name_tostr(dns_zone_t * zone,char * buf,size_t length)15811 zone_name_tostr(dns_zone_t *zone, char *buf, size_t length) {
15812 	isc_result_t result = ISC_R_FAILURE;
15813 	isc_buffer_t buffer;
15814 
15815 	REQUIRE(buf != NULL);
15816 	REQUIRE(length > 1U);
15817 
15818 	/*
15819 	 * Leave space for terminating '\0'.
15820 	 */
15821 	isc_buffer_init(&buffer, buf, (unsigned int)length - 1);
15822 	if (dns_name_dynamic(&zone->origin)) {
15823 		result = dns_name_totext(&zone->origin, true, &buffer);
15824 	}
15825 	if (result != ISC_R_SUCCESS &&
15826 	    isc_buffer_availablelength(&buffer) >= (sizeof("<UNKNOWN>") - 1))
15827 	{
15828 		isc_buffer_putstr(&buffer, "<UNKNOWN>");
15829 	}
15830 
15831 	buf[isc_buffer_usedlength(&buffer)] = '\0';
15832 }
15833 
15834 static void
zone_rdclass_tostr(dns_zone_t * zone,char * buf,size_t length)15835 zone_rdclass_tostr(dns_zone_t *zone, char *buf, size_t length) {
15836 	isc_buffer_t buffer;
15837 
15838 	REQUIRE(buf != NULL);
15839 	REQUIRE(length > 1U);
15840 
15841 	/*
15842 	 * Leave space for terminating '\0'.
15843 	 */
15844 	isc_buffer_init(&buffer, buf, (unsigned int)length - 1);
15845 	(void)dns_rdataclass_totext(zone->rdclass, &buffer);
15846 
15847 	buf[isc_buffer_usedlength(&buffer)] = '\0';
15848 }
15849 
15850 static void
zone_viewname_tostr(dns_zone_t * zone,char * buf,size_t length)15851 zone_viewname_tostr(dns_zone_t *zone, char *buf, size_t length) {
15852 	isc_buffer_t buffer;
15853 
15854 	REQUIRE(buf != NULL);
15855 	REQUIRE(length > 1U);
15856 
15857 	/*
15858 	 * Leave space for terminating '\0'.
15859 	 */
15860 	isc_buffer_init(&buffer, buf, (unsigned int)length - 1);
15861 
15862 	if (zone->view == NULL) {
15863 		isc_buffer_putstr(&buffer, "_none");
15864 	} else if (strlen(zone->view->name) <
15865 		   isc_buffer_availablelength(&buffer)) {
15866 		isc_buffer_putstr(&buffer, zone->view->name);
15867 	} else {
15868 		isc_buffer_putstr(&buffer, "_toolong");
15869 	}
15870 
15871 	buf[isc_buffer_usedlength(&buffer)] = '\0';
15872 }
15873 
15874 void
dns_zone_name(dns_zone_t * zone,char * buf,size_t length)15875 dns_zone_name(dns_zone_t *zone, char *buf, size_t length) {
15876 	REQUIRE(DNS_ZONE_VALID(zone));
15877 	REQUIRE(buf != NULL);
15878 
15879 	LOCK_ZONE(zone);
15880 	zone_namerd_tostr(zone, buf, length);
15881 	UNLOCK_ZONE(zone);
15882 }
15883 
15884 void
dns_zone_nameonly(dns_zone_t * zone,char * buf,size_t length)15885 dns_zone_nameonly(dns_zone_t *zone, char *buf, size_t length) {
15886 	REQUIRE(DNS_ZONE_VALID(zone));
15887 	REQUIRE(buf != NULL);
15888 	zone_name_tostr(zone, buf, length);
15889 }
15890 
15891 void
dns_zone_logv(dns_zone_t * zone,isc_logcategory_t * category,int level,const char * prefix,const char * fmt,va_list ap)15892 dns_zone_logv(dns_zone_t *zone, isc_logcategory_t *category, int level,
15893 	      const char *prefix, const char *fmt, va_list ap) {
15894 	char message[4096];
15895 	const char *zstr;
15896 
15897 	REQUIRE(DNS_ZONE_VALID(zone));
15898 
15899 	if (!isc_log_wouldlog(dns_lctx, level)) {
15900 		return;
15901 	}
15902 
15903 	vsnprintf(message, sizeof(message), fmt, ap);
15904 
15905 	switch (zone->type) {
15906 	case dns_zone_key:
15907 		zstr = "managed-keys-zone";
15908 		break;
15909 	case dns_zone_redirect:
15910 		zstr = "redirect-zone";
15911 		break;
15912 	default:
15913 		zstr = "zone ";
15914 	}
15915 
15916 	isc_log_write(dns_lctx, category, DNS_LOGMODULE_ZONE, level,
15917 		      "%s%s%s%s: %s", (prefix != NULL ? prefix : ""),
15918 		      (prefix != NULL ? ": " : ""), zstr, zone->strnamerd,
15919 		      message);
15920 }
15921 
15922 static void
notify_log(dns_zone_t * zone,int level,const char * fmt,...)15923 notify_log(dns_zone_t *zone, int level, const char *fmt, ...) {
15924 	va_list ap;
15925 
15926 	va_start(ap, fmt);
15927 	dns_zone_logv(zone, DNS_LOGCATEGORY_NOTIFY, level, NULL, fmt, ap);
15928 	va_end(ap);
15929 }
15930 
15931 void
dns_zone_logc(dns_zone_t * zone,isc_logcategory_t * category,int level,const char * fmt,...)15932 dns_zone_logc(dns_zone_t *zone, isc_logcategory_t *category, int level,
15933 	      const char *fmt, ...) {
15934 	va_list ap;
15935 
15936 	va_start(ap, fmt);
15937 	dns_zone_logv(zone, category, level, NULL, fmt, ap);
15938 	va_end(ap);
15939 }
15940 
15941 void
dns_zone_log(dns_zone_t * zone,int level,const char * fmt,...)15942 dns_zone_log(dns_zone_t *zone, int level, const char *fmt, ...) {
15943 	va_list ap;
15944 
15945 	va_start(ap, fmt);
15946 	dns_zone_logv(zone, DNS_LOGCATEGORY_GENERAL, level, NULL, fmt, ap);
15947 	va_end(ap);
15948 }
15949 
15950 static void
zone_debuglog(dns_zone_t * zone,const char * me,int debuglevel,const char * fmt,...)15951 zone_debuglog(dns_zone_t *zone, const char *me, int debuglevel, const char *fmt,
15952 	      ...) {
15953 	int level = ISC_LOG_DEBUG(debuglevel);
15954 	va_list ap;
15955 
15956 	va_start(ap, fmt);
15957 	dns_zone_logv(zone, DNS_LOGCATEGORY_GENERAL, level, me, fmt, ap);
15958 	va_end(ap);
15959 }
15960 
15961 static void
dnssec_log(dns_zone_t * zone,int level,const char * fmt,...)15962 dnssec_log(dns_zone_t *zone, int level, const char *fmt, ...) {
15963 	va_list ap;
15964 
15965 	va_start(ap, fmt);
15966 	dns_zone_logv(zone, DNS_LOGCATEGORY_DNSSEC, level, NULL, fmt, ap);
15967 	va_end(ap);
15968 }
15969 
15970 static int
message_count(dns_message_t * msg,dns_section_t section,dns_rdatatype_t type)15971 message_count(dns_message_t *msg, dns_section_t section, dns_rdatatype_t type) {
15972 	isc_result_t result;
15973 	dns_name_t *name;
15974 	dns_rdataset_t *curr;
15975 	int count = 0;
15976 
15977 	result = dns_message_firstname(msg, section);
15978 	while (result == ISC_R_SUCCESS) {
15979 		name = NULL;
15980 		dns_message_currentname(msg, section, &name);
15981 
15982 		for (curr = ISC_LIST_TAIL(name->list); curr != NULL;
15983 		     curr = ISC_LIST_PREV(curr, link))
15984 		{
15985 			if (curr->type == type) {
15986 				count++;
15987 			}
15988 		}
15989 		result = dns_message_nextname(msg, section);
15990 	}
15991 
15992 	return (count);
15993 }
15994 
15995 void
dns_zone_setmaxxfrin(dns_zone_t * zone,uint32_t maxxfrin)15996 dns_zone_setmaxxfrin(dns_zone_t *zone, uint32_t maxxfrin) {
15997 	REQUIRE(DNS_ZONE_VALID(zone));
15998 
15999 	zone->maxxfrin = maxxfrin;
16000 }
16001 
16002 uint32_t
dns_zone_getmaxxfrin(dns_zone_t * zone)16003 dns_zone_getmaxxfrin(dns_zone_t *zone) {
16004 	REQUIRE(DNS_ZONE_VALID(zone));
16005 
16006 	return (zone->maxxfrin);
16007 }
16008 
16009 void
dns_zone_setmaxxfrout(dns_zone_t * zone,uint32_t maxxfrout)16010 dns_zone_setmaxxfrout(dns_zone_t *zone, uint32_t maxxfrout) {
16011 	REQUIRE(DNS_ZONE_VALID(zone));
16012 	zone->maxxfrout = maxxfrout;
16013 }
16014 
16015 uint32_t
dns_zone_getmaxxfrout(dns_zone_t * zone)16016 dns_zone_getmaxxfrout(dns_zone_t *zone) {
16017 	REQUIRE(DNS_ZONE_VALID(zone));
16018 
16019 	return (zone->maxxfrout);
16020 }
16021 
16022 dns_zonetype_t
dns_zone_gettype(dns_zone_t * zone)16023 dns_zone_gettype(dns_zone_t *zone) {
16024 	REQUIRE(DNS_ZONE_VALID(zone));
16025 
16026 	return (zone->type);
16027 }
16028 
16029 const char *
dns_zonetype_name(dns_zonetype_t type)16030 dns_zonetype_name(dns_zonetype_t type) {
16031 	switch (type) {
16032 	case dns_zone_none:
16033 		return ("none");
16034 	case dns_zone_primary:
16035 		return ("primary");
16036 	case dns_zone_secondary:
16037 		return ("secondary");
16038 	case dns_zone_mirror:
16039 		return ("mirror");
16040 	case dns_zone_stub:
16041 		return ("stub");
16042 	case dns_zone_staticstub:
16043 		return ("static-stub");
16044 	case dns_zone_key:
16045 		return ("key");
16046 	case dns_zone_dlz:
16047 		return ("dlz");
16048 	case dns_zone_redirect:
16049 		return ("redirect");
16050 	default:
16051 		return ("unknown");
16052 	}
16053 }
16054 
16055 dns_zonetype_t
dns_zone_getredirecttype(dns_zone_t * zone)16056 dns_zone_getredirecttype(dns_zone_t *zone) {
16057 	REQUIRE(DNS_ZONE_VALID(zone));
16058 	REQUIRE(zone->type == dns_zone_redirect);
16059 
16060 	return (zone->masters == NULL ? dns_zone_primary : dns_zone_secondary);
16061 }
16062 
16063 dns_name_t *
dns_zone_getorigin(dns_zone_t * zone)16064 dns_zone_getorigin(dns_zone_t *zone) {
16065 	REQUIRE(DNS_ZONE_VALID(zone));
16066 
16067 	return (&zone->origin);
16068 }
16069 
16070 void
dns_zone_settask(dns_zone_t * zone,isc_task_t * task)16071 dns_zone_settask(dns_zone_t *zone, isc_task_t *task) {
16072 	REQUIRE(DNS_ZONE_VALID(zone));
16073 
16074 	LOCK_ZONE(zone);
16075 	if (zone->task != NULL) {
16076 		isc_task_detach(&zone->task);
16077 	}
16078 	isc_task_attach(task, &zone->task);
16079 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
16080 	if (zone->db != NULL) {
16081 		dns_db_settask(zone->db, zone->task);
16082 	}
16083 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
16084 	UNLOCK_ZONE(zone);
16085 }
16086 
16087 void
dns_zone_gettask(dns_zone_t * zone,isc_task_t ** target)16088 dns_zone_gettask(dns_zone_t *zone, isc_task_t **target) {
16089 	REQUIRE(DNS_ZONE_VALID(zone));
16090 	isc_task_attach(zone->task, target);
16091 }
16092 
16093 void
dns_zone_setidlein(dns_zone_t * zone,uint32_t idlein)16094 dns_zone_setidlein(dns_zone_t *zone, uint32_t idlein) {
16095 	REQUIRE(DNS_ZONE_VALID(zone));
16096 
16097 	if (idlein == 0) {
16098 		idlein = DNS_DEFAULT_IDLEIN;
16099 	}
16100 	zone->idlein = idlein;
16101 }
16102 
16103 uint32_t
dns_zone_getidlein(dns_zone_t * zone)16104 dns_zone_getidlein(dns_zone_t *zone) {
16105 	REQUIRE(DNS_ZONE_VALID(zone));
16106 
16107 	return (zone->idlein);
16108 }
16109 
16110 void
dns_zone_setidleout(dns_zone_t * zone,uint32_t idleout)16111 dns_zone_setidleout(dns_zone_t *zone, uint32_t idleout) {
16112 	REQUIRE(DNS_ZONE_VALID(zone));
16113 
16114 	zone->idleout = idleout;
16115 }
16116 
16117 uint32_t
dns_zone_getidleout(dns_zone_t * zone)16118 dns_zone_getidleout(dns_zone_t *zone) {
16119 	REQUIRE(DNS_ZONE_VALID(zone));
16120 
16121 	return (zone->idleout);
16122 }
16123 
16124 static void
notify_done(isc_task_t * task,isc_event_t * event)16125 notify_done(isc_task_t *task, isc_event_t *event) {
16126 	dns_requestevent_t *revent = (dns_requestevent_t *)event;
16127 	dns_notify_t *notify;
16128 	isc_result_t result;
16129 	dns_message_t *message = NULL;
16130 	isc_buffer_t buf;
16131 	char rcode[128];
16132 	char addrbuf[ISC_SOCKADDR_FORMATSIZE];
16133 
16134 	UNUSED(task);
16135 
16136 	notify = event->ev_arg;
16137 	REQUIRE(DNS_NOTIFY_VALID(notify));
16138 	INSIST(task == notify->zone->task);
16139 
16140 	isc_buffer_init(&buf, rcode, sizeof(rcode));
16141 	isc_sockaddr_format(&notify->dst, addrbuf, sizeof(addrbuf));
16142 	dns_message_create(notify->zone->mctx, DNS_MESSAGE_INTENTPARSE,
16143 			   &message);
16144 
16145 	result = revent->result;
16146 	if (result == ISC_R_SUCCESS) {
16147 		result =
16148 			dns_request_getresponse(revent->request, message,
16149 						DNS_MESSAGEPARSE_PRESERVEORDER);
16150 	}
16151 	if (result == ISC_R_SUCCESS) {
16152 		result = dns_rcode_totext(message->rcode, &buf);
16153 	}
16154 	if (result == ISC_R_SUCCESS) {
16155 		notify_log(notify->zone, ISC_LOG_DEBUG(3),
16156 			   "notify response from %s: %.*s", addrbuf,
16157 			   (int)buf.used, rcode);
16158 	} else {
16159 		notify_log(notify->zone, ISC_LOG_DEBUG(2),
16160 			   "notify to %s failed: %s", addrbuf,
16161 			   dns_result_totext(result));
16162 	}
16163 
16164 	/*
16165 	 * Old bind's return formerr if they see a soa record.	Retry w/o
16166 	 * the soa if we see a formerr and had sent a SOA.
16167 	 */
16168 	isc_event_free(&event);
16169 	if (message->rcode == dns_rcode_formerr &&
16170 	    (notify->flags & DNS_NOTIFY_NOSOA) == 0)
16171 	{
16172 		bool startup;
16173 
16174 		notify->flags |= DNS_NOTIFY_NOSOA;
16175 		dns_request_destroy(&notify->request);
16176 		startup = (notify->flags & DNS_NOTIFY_STARTUP);
16177 		result = notify_send_queue(notify, startup);
16178 		if (result != ISC_R_SUCCESS) {
16179 			notify_destroy(notify, false);
16180 		}
16181 	} else {
16182 		if (result == ISC_R_TIMEDOUT) {
16183 			notify_log(notify->zone, ISC_LOG_DEBUG(1),
16184 				   "notify to %s: retries exceeded", addrbuf);
16185 		}
16186 		notify_destroy(notify, false);
16187 	}
16188 	dns_message_detach(&message);
16189 }
16190 
16191 struct secure_event {
16192 	isc_event_t e;
16193 	dns_db_t *db;
16194 	uint32_t serial;
16195 };
16196 
16197 static void
update_log_cb(void * arg,dns_zone_t * zone,int level,const char * message)16198 update_log_cb(void *arg, dns_zone_t *zone, int level, const char *message) {
16199 	UNUSED(arg);
16200 	dns_zone_log(zone, level, "%s", message);
16201 }
16202 
16203 static isc_result_t
sync_secure_journal(dns_zone_t * zone,dns_zone_t * raw,dns_journal_t * journal,uint32_t start,uint32_t end,dns_difftuple_t ** soatuplep,dns_diff_t * diff)16204 sync_secure_journal(dns_zone_t *zone, dns_zone_t *raw, dns_journal_t *journal,
16205 		    uint32_t start, uint32_t end, dns_difftuple_t **soatuplep,
16206 		    dns_diff_t *diff) {
16207 	isc_result_t result;
16208 	dns_difftuple_t *tuple = NULL;
16209 	dns_diffop_t op = DNS_DIFFOP_ADD;
16210 	int n_soa = 0;
16211 
16212 	REQUIRE(soatuplep != NULL);
16213 
16214 	if (start == end) {
16215 		return (DNS_R_UNCHANGED);
16216 	}
16217 
16218 	CHECK(dns_journal_iter_init(journal, start, end, NULL));
16219 	for (result = dns_journal_first_rr(journal); result == ISC_R_SUCCESS;
16220 	     result = dns_journal_next_rr(journal))
16221 	{
16222 		dns_name_t *name = NULL;
16223 		uint32_t ttl;
16224 		dns_rdata_t *rdata = NULL;
16225 		dns_journal_current_rr(journal, &name, &ttl, &rdata);
16226 
16227 		if (rdata->type == dns_rdatatype_soa) {
16228 			n_soa++;
16229 			if (n_soa == 2) {
16230 				/*
16231 				 * Save the latest raw SOA record.
16232 				 */
16233 				if (*soatuplep != NULL) {
16234 					dns_difftuple_free(soatuplep);
16235 				}
16236 				CHECK(dns_difftuple_create(
16237 					diff->mctx, DNS_DIFFOP_ADD, name, ttl,
16238 					rdata, soatuplep));
16239 			}
16240 			if (n_soa == 3) {
16241 				n_soa = 1;
16242 			}
16243 			continue;
16244 		}
16245 
16246 		/* Sanity. */
16247 		if (n_soa == 0) {
16248 			dns_zone_log(raw, ISC_LOG_ERROR,
16249 				     "corrupt journal file: '%s'\n",
16250 				     raw->journal);
16251 			return (ISC_R_FAILURE);
16252 		}
16253 
16254 		if (zone->privatetype != 0 && rdata->type == zone->privatetype)
16255 		{
16256 			continue;
16257 		}
16258 
16259 		if (rdata->type == dns_rdatatype_nsec ||
16260 		    rdata->type == dns_rdatatype_rrsig ||
16261 		    rdata->type == dns_rdatatype_nsec3 ||
16262 		    rdata->type == dns_rdatatype_dnskey ||
16263 		    rdata->type == dns_rdatatype_nsec3param)
16264 		{
16265 			continue;
16266 		}
16267 
16268 		op = (n_soa == 1) ? DNS_DIFFOP_DEL : DNS_DIFFOP_ADD;
16269 
16270 		CHECK(dns_difftuple_create(diff->mctx, op, name, ttl, rdata,
16271 					   &tuple));
16272 		dns_diff_appendminimal(diff, &tuple);
16273 	}
16274 	if (result == ISC_R_NOMORE) {
16275 		result = ISC_R_SUCCESS;
16276 	}
16277 
16278 failure:
16279 	return (result);
16280 }
16281 
16282 static isc_result_t
sync_secure_db(dns_zone_t * seczone,dns_zone_t * raw,dns_db_t * secdb,dns_dbversion_t * secver,dns_difftuple_t ** soatuple,dns_diff_t * diff)16283 sync_secure_db(dns_zone_t *seczone, dns_zone_t *raw, dns_db_t *secdb,
16284 	       dns_dbversion_t *secver, dns_difftuple_t **soatuple,
16285 	       dns_diff_t *diff) {
16286 	isc_result_t result;
16287 	dns_db_t *rawdb = NULL;
16288 	dns_dbversion_t *rawver = NULL;
16289 	dns_difftuple_t *tuple = NULL, *next;
16290 	dns_difftuple_t *oldtuple = NULL, *newtuple = NULL;
16291 	dns_rdata_soa_t oldsoa, newsoa;
16292 
16293 	REQUIRE(DNS_ZONE_VALID(seczone));
16294 	REQUIRE(soatuple != NULL && *soatuple == NULL);
16295 
16296 	if (!seczone->sourceserialset) {
16297 		return (DNS_R_UNCHANGED);
16298 	}
16299 
16300 	dns_db_attach(raw->db, &rawdb);
16301 	dns_db_currentversion(rawdb, &rawver);
16302 	result = dns_db_diffx(diff, rawdb, rawver, secdb, secver, NULL);
16303 	dns_db_closeversion(rawdb, &rawver, false);
16304 	dns_db_detach(&rawdb);
16305 
16306 	if (result != ISC_R_SUCCESS) {
16307 		return (result);
16308 	}
16309 
16310 	for (tuple = ISC_LIST_HEAD(diff->tuples); tuple != NULL; tuple = next) {
16311 		next = ISC_LIST_NEXT(tuple, link);
16312 		if (tuple->rdata.type == dns_rdatatype_nsec ||
16313 		    tuple->rdata.type == dns_rdatatype_rrsig ||
16314 		    tuple->rdata.type == dns_rdatatype_dnskey ||
16315 		    tuple->rdata.type == dns_rdatatype_nsec3 ||
16316 		    tuple->rdata.type == dns_rdatatype_nsec3param)
16317 		{
16318 			ISC_LIST_UNLINK(diff->tuples, tuple, link);
16319 			dns_difftuple_free(&tuple);
16320 			continue;
16321 		}
16322 		if (tuple->rdata.type == dns_rdatatype_soa) {
16323 			if (tuple->op == DNS_DIFFOP_DEL) {
16324 				INSIST(oldtuple == NULL);
16325 				oldtuple = tuple;
16326 			}
16327 			if (tuple->op == DNS_DIFFOP_ADD) {
16328 				INSIST(newtuple == NULL);
16329 				newtuple = tuple;
16330 			}
16331 		}
16332 	}
16333 
16334 	if (oldtuple != NULL && newtuple != NULL) {
16335 		result = dns_rdata_tostruct(&oldtuple->rdata, &oldsoa, NULL);
16336 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
16337 
16338 		result = dns_rdata_tostruct(&newtuple->rdata, &newsoa, NULL);
16339 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
16340 
16341 		/*
16342 		 * If the SOA records are the same except for the serial
16343 		 * remove them from the diff.
16344 		 */
16345 		if (oldtuple->ttl == newtuple->ttl &&
16346 		    oldsoa.refresh == newsoa.refresh &&
16347 		    oldsoa.retry == newsoa.retry &&
16348 		    oldsoa.minimum == newsoa.minimum &&
16349 		    oldsoa.expire == newsoa.expire &&
16350 		    dns_name_equal(&oldsoa.origin, &newsoa.origin) &&
16351 		    dns_name_equal(&oldsoa.contact, &newsoa.contact))
16352 		{
16353 			ISC_LIST_UNLINK(diff->tuples, oldtuple, link);
16354 			dns_difftuple_free(&oldtuple);
16355 			ISC_LIST_UNLINK(diff->tuples, newtuple, link);
16356 			dns_difftuple_free(&newtuple);
16357 		}
16358 	}
16359 
16360 	if (ISC_LIST_EMPTY(diff->tuples)) {
16361 		return (DNS_R_UNCHANGED);
16362 	}
16363 
16364 	/*
16365 	 * If there are still SOA records in the diff they can now be removed
16366 	 * saving the new SOA record.
16367 	 */
16368 	if (oldtuple != NULL) {
16369 		ISC_LIST_UNLINK(diff->tuples, oldtuple, link);
16370 		dns_difftuple_free(&oldtuple);
16371 	}
16372 
16373 	if (newtuple != NULL) {
16374 		ISC_LIST_UNLINK(diff->tuples, newtuple, link);
16375 		*soatuple = newtuple;
16376 	}
16377 
16378 	return (ISC_R_SUCCESS);
16379 }
16380 
16381 static void
receive_secure_serial(isc_task_t * task,isc_event_t * event)16382 receive_secure_serial(isc_task_t *task, isc_event_t *event) {
16383 	static char me[] = "receive_secure_serial";
16384 	isc_result_t result = ISC_R_SUCCESS;
16385 	dns_journal_t *rjournal = NULL;
16386 	dns_journal_t *sjournal = NULL;
16387 	uint32_t start, end;
16388 	dns_zone_t *zone;
16389 	dns_difftuple_t *tuple = NULL, *soatuple = NULL;
16390 	dns_update_log_t log = { update_log_cb, NULL };
16391 	uint32_t newserial = 0, desired = 0;
16392 	isc_time_t timenow;
16393 	int level = ISC_LOG_ERROR;
16394 
16395 	UNUSED(task);
16396 
16397 	zone = event->ev_arg;
16398 	end = ((struct secure_event *)event)->serial;
16399 
16400 	ENTER;
16401 
16402 	LOCK_ZONE(zone);
16403 
16404 	/*
16405 	 * If we are already processing a receive secure serial event
16406 	 * for the zone, just queue the new one and exit.
16407 	 */
16408 	if (zone->rss_event != NULL && zone->rss_event != event) {
16409 		ISC_LIST_APPEND(zone->rss_events, event, ev_link);
16410 		UNLOCK_ZONE(zone);
16411 		return;
16412 	}
16413 
16414 nextevent:
16415 	if (zone->rss_event != NULL) {
16416 		INSIST(zone->rss_event == event);
16417 		UNLOCK_ZONE(zone);
16418 	} else {
16419 		zone->rss_event = event;
16420 		dns_diff_init(zone->mctx, &zone->rss_diff);
16421 
16422 		/*
16423 		 * zone->db may be NULL, if the load from disk failed.
16424 		 */
16425 		result = ISC_R_SUCCESS;
16426 		ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
16427 		if (zone->db != NULL) {
16428 			dns_db_attach(zone->db, &zone->rss_db);
16429 		} else {
16430 			result = ISC_R_FAILURE;
16431 		}
16432 		ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
16433 
16434 		if (result == ISC_R_SUCCESS && zone->raw != NULL) {
16435 			dns_zone_attach(zone->raw, &zone->rss_raw);
16436 		} else {
16437 			result = ISC_R_FAILURE;
16438 		}
16439 
16440 		UNLOCK_ZONE(zone);
16441 
16442 		CHECK(result);
16443 
16444 		/*
16445 		 * We first attempt to sync the raw zone to the secure zone
16446 		 * by using the raw zone's journal, applying all the deltas
16447 		 * from the latest source-serial of the secure zone up to
16448 		 * the current serial number of the raw zone.
16449 		 *
16450 		 * If that fails, then we'll fall back to a direct comparison
16451 		 * between raw and secure zones.
16452 		 */
16453 		CHECK(dns_journal_open(zone->rss_raw->mctx,
16454 				       zone->rss_raw->journal,
16455 				       DNS_JOURNAL_WRITE, &rjournal));
16456 
16457 		result = dns_journal_open(zone->mctx, zone->journal,
16458 					  DNS_JOURNAL_READ, &sjournal);
16459 		if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
16460 			goto failure;
16461 		}
16462 
16463 		if (!dns_journal_get_sourceserial(rjournal, &start)) {
16464 			start = dns_journal_first_serial(rjournal);
16465 			dns_journal_set_sourceserial(rjournal, start);
16466 		}
16467 		if (sjournal != NULL) {
16468 			uint32_t serial;
16469 			/*
16470 			 * We read the secure journal first, if that
16471 			 * exists use its value provided it is greater
16472 			 * that from the raw journal.
16473 			 */
16474 			if (dns_journal_get_sourceserial(sjournal, &serial)) {
16475 				if (isc_serial_gt(serial, start)) {
16476 					start = serial;
16477 				}
16478 			}
16479 			dns_journal_destroy(&sjournal);
16480 		}
16481 
16482 		dns_db_currentversion(zone->rss_db, &zone->rss_oldver);
16483 		CHECK(dns_db_newversion(zone->rss_db, &zone->rss_newver));
16484 
16485 		/*
16486 		 * Try to apply diffs from the raw zone's journal to the secure
16487 		 * zone.  If that fails, we recover by syncing up the databases
16488 		 * directly.
16489 		 */
16490 		result = sync_secure_journal(zone, zone->rss_raw, rjournal,
16491 					     start, end, &soatuple,
16492 					     &zone->rss_diff);
16493 		if (result == DNS_R_UNCHANGED) {
16494 			level = ISC_LOG_INFO;
16495 			goto failure;
16496 		} else if (result != ISC_R_SUCCESS) {
16497 			CHECK(sync_secure_db(zone, zone->rss_raw, zone->rss_db,
16498 					     zone->rss_oldver, &soatuple,
16499 					     &zone->rss_diff));
16500 		}
16501 
16502 		CHECK(dns_diff_apply(&zone->rss_diff, zone->rss_db,
16503 				     zone->rss_newver));
16504 
16505 		if (soatuple != NULL) {
16506 			uint32_t oldserial;
16507 
16508 			CHECK(dns_db_createsoatuple(
16509 				zone->rss_db, zone->rss_oldver,
16510 				zone->rss_diff.mctx, DNS_DIFFOP_DEL, &tuple));
16511 			oldserial = dns_soa_getserial(&tuple->rdata);
16512 			newserial = desired =
16513 				dns_soa_getserial(&soatuple->rdata);
16514 			if (!isc_serial_gt(newserial, oldserial)) {
16515 				newserial = oldserial + 1;
16516 				if (newserial == 0) {
16517 					newserial++;
16518 				}
16519 				dns_soa_setserial(newserial, &soatuple->rdata);
16520 			}
16521 			CHECK(do_one_tuple(&tuple, zone->rss_db,
16522 					   zone->rss_newver, &zone->rss_diff));
16523 			CHECK(do_one_tuple(&soatuple, zone->rss_db,
16524 					   zone->rss_newver, &zone->rss_diff));
16525 		} else {
16526 			CHECK(update_soa_serial(zone, zone->rss_db,
16527 						zone->rss_newver,
16528 						&zone->rss_diff, zone->mctx,
16529 						zone->updatemethod));
16530 		}
16531 	}
16532 	result = dns_update_signaturesinc(
16533 		&log, zone, zone->rss_db, zone->rss_oldver, zone->rss_newver,
16534 		&zone->rss_diff, zone->sigvalidityinterval, &zone->rss_state);
16535 	if (result == DNS_R_CONTINUE) {
16536 		if (rjournal != NULL) {
16537 			dns_journal_destroy(&rjournal);
16538 		}
16539 		isc_task_send(task, &event);
16540 		return;
16541 	}
16542 	/*
16543 	 * If something went wrong while trying to update the secure zone and
16544 	 * the latter was already signed before, do not apply raw zone deltas
16545 	 * to it as that would break existing DNSSEC signatures.  However, if
16546 	 * the secure zone was not yet signed (e.g. because no signing keys
16547 	 * were created for it), commence applying raw zone deltas to it so
16548 	 * that contents of the raw zone and the secure zone are kept in sync.
16549 	 */
16550 	if (result != ISC_R_SUCCESS && dns_db_issecure(zone->rss_db)) {
16551 		goto failure;
16552 	}
16553 
16554 	if (rjournal == NULL) {
16555 		CHECK(dns_journal_open(zone->rss_raw->mctx,
16556 				       zone->rss_raw->journal,
16557 				       DNS_JOURNAL_WRITE, &rjournal));
16558 	}
16559 	CHECK(zone_journal(zone, &zone->rss_diff, &end,
16560 			   "receive_secure_serial"));
16561 
16562 	dns_journal_set_sourceserial(rjournal, end);
16563 	dns_journal_commit(rjournal);
16564 
16565 	LOCK_ZONE(zone);
16566 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
16567 
16568 	zone->sourceserial = end;
16569 	zone->sourceserialset = true;
16570 	zone_needdump(zone, DNS_DUMP_DELAY);
16571 
16572 	/*
16573 	 * Set resign time to make sure it is set to the earliest
16574 	 * signature expiration.
16575 	 */
16576 	set_resigntime(zone);
16577 	TIME_NOW(&timenow);
16578 	zone_settimer(zone, &timenow);
16579 	UNLOCK_ZONE(zone);
16580 
16581 	dns_db_closeversion(zone->rss_db, &zone->rss_oldver, false);
16582 	dns_db_closeversion(zone->rss_db, &zone->rss_newver, true);
16583 
16584 	if (newserial != 0) {
16585 		dns_zone_log(zone, ISC_LOG_INFO, "serial %u (unsigned %u)",
16586 			     newserial, desired);
16587 	}
16588 
16589 failure:
16590 	isc_event_free(&zone->rss_event);
16591 	event = ISC_LIST_HEAD(zone->rss_events);
16592 
16593 	if (zone->rss_raw != NULL) {
16594 		dns_zone_detach(&zone->rss_raw);
16595 	}
16596 	if (result != ISC_R_SUCCESS) {
16597 		LOCK_ZONE(zone);
16598 		set_resigntime(zone);
16599 		TIME_NOW(&timenow);
16600 		zone_settimer(zone, &timenow);
16601 		UNLOCK_ZONE(zone);
16602 		dns_zone_log(zone, level, "receive_secure_serial: %s",
16603 			     dns_result_totext(result));
16604 	}
16605 	if (tuple != NULL) {
16606 		dns_difftuple_free(&tuple);
16607 	}
16608 	if (soatuple != NULL) {
16609 		dns_difftuple_free(&soatuple);
16610 	}
16611 	if (zone->rss_db != NULL) {
16612 		if (zone->rss_oldver != NULL) {
16613 			dns_db_closeversion(zone->rss_db, &zone->rss_oldver,
16614 					    false);
16615 		}
16616 		if (zone->rss_newver != NULL) {
16617 			dns_db_closeversion(zone->rss_db, &zone->rss_newver,
16618 					    false);
16619 		}
16620 		dns_db_detach(&zone->rss_db);
16621 	}
16622 	INSIST(zone->rss_oldver == NULL);
16623 	INSIST(zone->rss_newver == NULL);
16624 	if (rjournal != NULL) {
16625 		dns_journal_destroy(&rjournal);
16626 	}
16627 	dns_diff_clear(&zone->rss_diff);
16628 
16629 	if (event != NULL) {
16630 		LOCK_ZONE(zone);
16631 		isc_refcount_decrement(&zone->irefs);
16632 		ISC_LIST_UNLINK(zone->rss_events, event, ev_link);
16633 		goto nextevent;
16634 	}
16635 
16636 	event = ISC_LIST_HEAD(zone->rss_post);
16637 	while (event != NULL) {
16638 		ISC_LIST_UNLINK(zone->rss_post, event, ev_link);
16639 		rss_post(zone, event);
16640 		event = ISC_LIST_HEAD(zone->rss_post);
16641 	}
16642 
16643 	dns_zone_idetach(&zone);
16644 }
16645 
16646 static isc_result_t
zone_send_secureserial(dns_zone_t * zone,uint32_t serial)16647 zone_send_secureserial(dns_zone_t *zone, uint32_t serial) {
16648 	isc_event_t *e;
16649 	dns_zone_t *dummy = NULL;
16650 
16651 	e = isc_event_allocate(zone->secure->mctx, zone,
16652 			       DNS_EVENT_ZONESECURESERIAL,
16653 			       receive_secure_serial, zone->secure,
16654 			       sizeof(struct secure_event));
16655 	((struct secure_event *)e)->serial = serial;
16656 	INSIST(LOCKED_ZONE(zone->secure));
16657 	zone_iattach(zone->secure, &dummy);
16658 	isc_task_send(zone->secure->task, &e);
16659 
16660 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_SENDSECURE);
16661 	return (ISC_R_SUCCESS);
16662 }
16663 
16664 static isc_result_t
checkandaddsoa(dns_db_t * db,dns_dbnode_t * node,dns_dbversion_t * version,dns_rdataset_t * rdataset,uint32_t oldserial)16665 checkandaddsoa(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
16666 	       dns_rdataset_t *rdataset, uint32_t oldserial) {
16667 	dns_rdata_soa_t soa;
16668 	dns_rdata_t rdata = DNS_RDATA_INIT;
16669 	dns_rdatalist_t temprdatalist;
16670 	dns_rdataset_t temprdataset;
16671 	isc_buffer_t b;
16672 	isc_result_t result;
16673 	unsigned char buf[DNS_SOA_BUFFERSIZE];
16674 	dns_fixedname_t fixed;
16675 	dns_name_t *name;
16676 
16677 	result = dns_rdataset_first(rdataset);
16678 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
16679 	dns_rdataset_current(rdataset, &rdata);
16680 	result = dns_rdata_tostruct(&rdata, &soa, NULL);
16681 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
16682 
16683 	if (isc_serial_gt(soa.serial, oldserial)) {
16684 		return (dns_db_addrdataset(db, node, version, 0, rdataset, 0,
16685 					   NULL));
16686 	}
16687 	/*
16688 	 * Always bump the serial.
16689 	 */
16690 	oldserial++;
16691 	if (oldserial == 0) {
16692 		oldserial++;
16693 	}
16694 	soa.serial = oldserial;
16695 
16696 	/*
16697 	 * Construct a replacement rdataset.
16698 	 */
16699 	dns_rdata_reset(&rdata);
16700 	isc_buffer_init(&b, buf, sizeof(buf));
16701 	result = dns_rdata_fromstruct(&rdata, rdataset->rdclass,
16702 				      dns_rdatatype_soa, &soa, &b);
16703 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
16704 	dns_rdatalist_init(&temprdatalist);
16705 	temprdatalist.rdclass = rdata.rdclass;
16706 	temprdatalist.type = rdata.type;
16707 	temprdatalist.ttl = rdataset->ttl;
16708 	ISC_LIST_APPEND(temprdatalist.rdata, &rdata, link);
16709 
16710 	dns_rdataset_init(&temprdataset);
16711 	result = dns_rdatalist_tordataset(&temprdatalist, &temprdataset);
16712 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
16713 
16714 	name = dns_fixedname_initname(&fixed);
16715 	result = dns_db_nodefullname(db, node, name);
16716 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
16717 	dns_rdataset_getownercase(rdataset, name);
16718 	dns_rdataset_setownercase(&temprdataset, name);
16719 	return (dns_db_addrdataset(db, node, version, 0, &temprdataset, 0,
16720 				   NULL));
16721 }
16722 
16723 /*
16724  * This function should populate an nsec3paramlist_t with the
16725  * nsecparam_t data from a zone.
16726  */
16727 static isc_result_t
save_nsec3param(dns_zone_t * zone,nsec3paramlist_t * nsec3list)16728 save_nsec3param(dns_zone_t *zone, nsec3paramlist_t *nsec3list) {
16729 	isc_result_t result;
16730 	dns_dbnode_t *node = NULL;
16731 	dns_rdataset_t rdataset, prdataset;
16732 	dns_dbversion_t *version = NULL;
16733 	nsec3param_t *nsec3param = NULL;
16734 	nsec3param_t *nsec3p = NULL;
16735 	nsec3param_t *next;
16736 	dns_db_t *db = NULL;
16737 	unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
16738 
16739 	REQUIRE(DNS_ZONE_VALID(zone));
16740 	REQUIRE(nsec3list != NULL);
16741 	REQUIRE(ISC_LIST_EMPTY(*nsec3list));
16742 
16743 	dns_rdataset_init(&rdataset);
16744 	dns_rdataset_init(&prdataset);
16745 
16746 	dns_db_attach(zone->db, &db);
16747 	CHECK(dns_db_getoriginnode(db, &node));
16748 
16749 	dns_db_currentversion(db, &version);
16750 	result = dns_db_findrdataset(db, node, version,
16751 				     dns_rdatatype_nsec3param,
16752 				     dns_rdatatype_none, 0, &rdataset, NULL);
16753 
16754 	if (result != ISC_R_SUCCESS) {
16755 		goto getprivate;
16756 	}
16757 
16758 	/*
16759 	 * Walk nsec3param rdataset making a list of parameters (note that
16760 	 * multiple simultaneous nsec3 chains are annoyingly legal -- this
16761 	 * is why we use an nsec3list, even though we will usually only
16762 	 * have one).
16763 	 */
16764 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
16765 	     result = dns_rdataset_next(&rdataset))
16766 	{
16767 		dns_rdata_t rdata = DNS_RDATA_INIT;
16768 		dns_rdata_t private = DNS_RDATA_INIT;
16769 
16770 		dns_rdataset_current(&rdataset, &rdata);
16771 		isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
16772 			      DNS_LOGMODULE_ZONE, ISC_LOG_DEBUG(3),
16773 			      "looping through nsec3param data");
16774 		nsec3param = isc_mem_get(zone->mctx, sizeof(nsec3param_t));
16775 		ISC_LINK_INIT(nsec3param, link);
16776 
16777 		/*
16778 		 * now transfer the data from the rdata to
16779 		 * the nsec3param
16780 		 */
16781 		dns_nsec3param_toprivate(&rdata, &private, zone->privatetype,
16782 					 nsec3param->data,
16783 					 sizeof(nsec3param->data));
16784 		nsec3param->length = private.length;
16785 		ISC_LIST_APPEND(*nsec3list, nsec3param, link);
16786 	}
16787 
16788 getprivate:
16789 	result = dns_db_findrdataset(db, node, version, zone->privatetype,
16790 				     dns_rdatatype_none, 0, &prdataset, NULL);
16791 	if (result != ISC_R_SUCCESS) {
16792 		goto done;
16793 	}
16794 
16795 	/*
16796 	 * walk private type records, converting them to nsec3 parameters
16797 	 * using dns_nsec3param_fromprivate(), do the right thing based on
16798 	 * CREATE and REMOVE flags
16799 	 */
16800 	for (result = dns_rdataset_first(&prdataset); result == ISC_R_SUCCESS;
16801 	     result = dns_rdataset_next(&prdataset))
16802 	{
16803 		dns_rdata_t rdata = DNS_RDATA_INIT;
16804 		dns_rdata_t private = DNS_RDATA_INIT;
16805 
16806 		dns_rdataset_current(&prdataset, &private);
16807 		isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
16808 			      DNS_LOGMODULE_ZONE, ISC_LOG_DEBUG(3),
16809 			      "looping through nsec3param private data");
16810 
16811 		/*
16812 		 * Do we have a valid private record?
16813 		 */
16814 		if (!dns_nsec3param_fromprivate(&private, &rdata, buf,
16815 						sizeof(buf))) {
16816 			continue;
16817 		}
16818 
16819 		/*
16820 		 * Remove any NSEC3PARAM records scheduled to be removed.
16821 		 */
16822 		if (NSEC3REMOVE(rdata.data[1])) {
16823 			/*
16824 			 * Zero out the flags.
16825 			 */
16826 			rdata.data[1] = 0;
16827 
16828 			for (nsec3p = ISC_LIST_HEAD(*nsec3list); nsec3p != NULL;
16829 			     nsec3p = next) {
16830 				next = ISC_LIST_NEXT(nsec3p, link);
16831 
16832 				if (nsec3p->length == rdata.length + 1 &&
16833 				    memcmp(rdata.data, nsec3p->data + 1,
16834 					   nsec3p->length - 1) == 0)
16835 				{
16836 					ISC_LIST_UNLINK(*nsec3list, nsec3p,
16837 							link);
16838 					isc_mem_put(zone->mctx, nsec3p,
16839 						    sizeof(nsec3param_t));
16840 				}
16841 			}
16842 			continue;
16843 		}
16844 
16845 		nsec3param = isc_mem_get(zone->mctx, sizeof(nsec3param_t));
16846 		ISC_LINK_INIT(nsec3param, link);
16847 
16848 		/*
16849 		 * Copy the remaining private records so the nsec/nsec3
16850 		 * chain gets created.
16851 		 */
16852 		INSIST(private.length <= sizeof(nsec3param->data));
16853 		memmove(nsec3param->data, private.data, private.length);
16854 		nsec3param->length = private.length;
16855 		ISC_LIST_APPEND(*nsec3list, nsec3param, link);
16856 	}
16857 
16858 done:
16859 	if (result == ISC_R_NOMORE || result == ISC_R_NOTFOUND) {
16860 		result = ISC_R_SUCCESS;
16861 	}
16862 
16863 failure:
16864 	if (node != NULL) {
16865 		dns_db_detachnode(db, &node);
16866 	}
16867 	if (version != NULL) {
16868 		dns_db_closeversion(db, &version, false);
16869 	}
16870 	if (db != NULL) {
16871 		dns_db_detach(&db);
16872 	}
16873 	if (dns_rdataset_isassociated(&rdataset)) {
16874 		dns_rdataset_disassociate(&rdataset);
16875 	}
16876 	if (dns_rdataset_isassociated(&prdataset)) {
16877 		dns_rdataset_disassociate(&prdataset);
16878 	}
16879 	return (result);
16880 }
16881 
16882 /*
16883  * Populate new zone db with private type records found by save_nsec3param().
16884  */
16885 static isc_result_t
restore_nsec3param(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * version,nsec3paramlist_t * nsec3list)16886 restore_nsec3param(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version,
16887 		   nsec3paramlist_t *nsec3list) {
16888 	isc_result_t result = ISC_R_SUCCESS;
16889 	dns_diff_t diff;
16890 	dns_rdata_t rdata;
16891 	nsec3param_t *nsec3p = NULL;
16892 	nsec3param_t *next;
16893 
16894 	REQUIRE(DNS_ZONE_VALID(zone));
16895 	REQUIRE(!ISC_LIST_EMPTY(*nsec3list));
16896 
16897 	dns_diff_init(zone->mctx, &diff);
16898 
16899 	/*
16900 	 * Loop through the list of private-type records, set the INITIAL
16901 	 * and CREATE flags, and the add the record to the apex of the tree
16902 	 * in db.
16903 	 */
16904 	for (nsec3p = ISC_LIST_HEAD(*nsec3list); nsec3p != NULL; nsec3p = next)
16905 	{
16906 		next = ISC_LIST_NEXT(nsec3p, link);
16907 		dns_rdata_init(&rdata);
16908 		nsec3p->data[2] = DNS_NSEC3FLAG_CREATE | DNS_NSEC3FLAG_INITIAL;
16909 		rdata.length = nsec3p->length;
16910 		rdata.data = nsec3p->data;
16911 		rdata.type = zone->privatetype;
16912 		rdata.rdclass = zone->rdclass;
16913 		result = update_one_rr(db, version, &diff, DNS_DIFFOP_ADD,
16914 				       &zone->origin, 0, &rdata);
16915 		if (result != ISC_R_SUCCESS) {
16916 			break;
16917 		}
16918 	}
16919 
16920 	dns_diff_clear(&diff);
16921 	return (result);
16922 }
16923 
16924 static isc_result_t
copy_non_dnssec_records(dns_zone_t * zone,dns_db_t * db,dns_db_t * version,dns_db_t * rawdb,dns_dbiterator_t * dbiterator,unsigned int * oldserial)16925 copy_non_dnssec_records(dns_zone_t *zone, dns_db_t *db, dns_db_t *version,
16926 			dns_db_t *rawdb, dns_dbiterator_t *dbiterator,
16927 			unsigned int *oldserial) {
16928 	dns_dbnode_t *rawnode = NULL, *node = NULL;
16929 	dns_fixedname_t fixed;
16930 	dns_name_t *name = dns_fixedname_initname(&fixed);
16931 	dns_rdataset_t rdataset;
16932 	dns_rdatasetiter_t *rdsit = NULL;
16933 	isc_result_t result;
16934 
16935 	result = dns_dbiterator_current(dbiterator, &rawnode, name);
16936 	if (result != ISC_R_SUCCESS) {
16937 		return (ISC_R_SUCCESS);
16938 	}
16939 
16940 	dns_dbiterator_pause(dbiterator);
16941 
16942 	result = dns_db_findnode(db, name, true, &node);
16943 	if (result != ISC_R_SUCCESS) {
16944 		goto cleanup;
16945 	}
16946 
16947 	result = dns_db_allrdatasets(rawdb, rawnode, NULL, 0, &rdsit);
16948 	if (result != ISC_R_SUCCESS) {
16949 		goto cleanup;
16950 	}
16951 
16952 	dns_rdataset_init(&rdataset);
16953 
16954 	for (result = dns_rdatasetiter_first(rdsit); result == ISC_R_SUCCESS;
16955 	     result = dns_rdatasetiter_next(rdsit))
16956 	{
16957 		dns_rdatasetiter_current(rdsit, &rdataset);
16958 		if (rdataset.type == dns_rdatatype_nsec ||
16959 		    rdataset.type == dns_rdatatype_rrsig ||
16960 		    rdataset.type == dns_rdatatype_nsec3 ||
16961 		    rdataset.type == dns_rdatatype_dnskey ||
16962 		    rdataset.type == dns_rdatatype_nsec3param)
16963 		{
16964 			/*
16965 			 * Allow DNSSEC records with dnssec-policy.
16966 			 * WMM: Perhaps add config option for it.
16967 			 */
16968 			if (dns_zone_getkasp(zone) == NULL) {
16969 				dns_rdataset_disassociate(&rdataset);
16970 				continue;
16971 			}
16972 		}
16973 		if (rdataset.type == dns_rdatatype_soa && oldserial != NULL) {
16974 			result = checkandaddsoa(db, node, version, &rdataset,
16975 						*oldserial);
16976 		} else {
16977 			result = dns_db_addrdataset(db, node, version, 0,
16978 						    &rdataset, 0, NULL);
16979 		}
16980 		dns_rdataset_disassociate(&rdataset);
16981 		if (result != ISC_R_SUCCESS) {
16982 			goto cleanup;
16983 		}
16984 	}
16985 	if (result == ISC_R_NOMORE) {
16986 		result = ISC_R_SUCCESS;
16987 	}
16988 
16989 cleanup:
16990 	if (rdsit != NULL) {
16991 		dns_rdatasetiter_destroy(&rdsit);
16992 	}
16993 	if (rawnode) {
16994 		dns_db_detachnode(rawdb, &rawnode);
16995 	}
16996 	if (node) {
16997 		dns_db_detachnode(db, &node);
16998 	}
16999 	return (result);
17000 }
17001 
17002 static void
receive_secure_db(isc_task_t * task,isc_event_t * event)17003 receive_secure_db(isc_task_t *task, isc_event_t *event) {
17004 	isc_result_t result;
17005 	dns_zone_t *zone;
17006 	dns_db_t *rawdb, *db = NULL;
17007 	dns_dbiterator_t *dbiterator = NULL;
17008 	dns_dbversion_t *version = NULL;
17009 	isc_time_t loadtime;
17010 	unsigned int oldserial = 0, *oldserialp = NULL;
17011 	nsec3paramlist_t nsec3list;
17012 	isc_event_t *setnsec3param_event;
17013 	dns_zone_t *dummy;
17014 
17015 	UNUSED(task);
17016 
17017 	ISC_LIST_INIT(nsec3list);
17018 
17019 	zone = event->ev_arg;
17020 	rawdb = ((struct secure_event *)event)->db;
17021 	isc_event_free(&event);
17022 
17023 	LOCK_ZONE(zone);
17024 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING) || !inline_secure(zone)) {
17025 		result = ISC_R_SHUTTINGDOWN;
17026 		goto failure;
17027 	}
17028 
17029 	TIME_NOW(&loadtime);
17030 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
17031 	if (zone->db != NULL) {
17032 		result = dns_db_getsoaserial(zone->db, NULL, &oldserial);
17033 		if (result == ISC_R_SUCCESS) {
17034 			oldserialp = &oldserial;
17035 		}
17036 
17037 		/*
17038 		 * assemble nsec3parameters from the old zone, and set a flag
17039 		 * if any are found
17040 		 */
17041 		result = save_nsec3param(zone, &nsec3list);
17042 		if (result != ISC_R_SUCCESS) {
17043 			ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
17044 			goto failure;
17045 		}
17046 	}
17047 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
17048 
17049 	result = dns_db_create(zone->mctx, zone->db_argv[0], &zone->origin,
17050 			       dns_dbtype_zone, zone->rdclass,
17051 			       zone->db_argc - 1, zone->db_argv + 1, &db);
17052 	if (result != ISC_R_SUCCESS) {
17053 		goto failure;
17054 	}
17055 
17056 	result = dns_db_setgluecachestats(db, zone->gluecachestats);
17057 	if (result != ISC_R_SUCCESS && result != ISC_R_NOTIMPLEMENTED) {
17058 		goto failure;
17059 	}
17060 
17061 	result = dns_db_newversion(db, &version);
17062 	if (result != ISC_R_SUCCESS) {
17063 		goto failure;
17064 	}
17065 
17066 	result = dns_db_createiterator(rawdb, 0, &dbiterator);
17067 	if (result != ISC_R_SUCCESS) {
17068 		goto failure;
17069 	}
17070 
17071 	for (result = dns_dbiterator_first(dbiterator); result == ISC_R_SUCCESS;
17072 	     result = dns_dbiterator_next(dbiterator))
17073 	{
17074 		result = copy_non_dnssec_records(zone, db, version, rawdb,
17075 						 dbiterator, oldserialp);
17076 		if (result != ISC_R_SUCCESS) {
17077 			goto failure;
17078 		}
17079 	}
17080 	dns_dbiterator_destroy(&dbiterator);
17081 	if (result != ISC_R_NOMORE) {
17082 		goto failure;
17083 	}
17084 
17085 	/*
17086 	 * Call restore_nsec3param() to create private-type records from
17087 	 * the old nsec3 parameters and insert them into db
17088 	 */
17089 	if (!ISC_LIST_EMPTY(nsec3list)) {
17090 		result = restore_nsec3param(zone, db, version, &nsec3list);
17091 		if (result != ISC_R_SUCCESS) {
17092 			goto failure;
17093 		}
17094 	}
17095 
17096 	dns_db_closeversion(db, &version, true);
17097 
17098 	/*
17099 	 * Lock hierarchy: zmgr, zone, raw.
17100 	 */
17101 	INSIST(zone != zone->raw);
17102 	LOCK_ZONE(zone->raw);
17103 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
17104 	result = zone_postload(zone, db, loadtime, ISC_R_SUCCESS);
17105 	zone_needdump(zone, 0); /* XXXMPA */
17106 	UNLOCK_ZONE(zone->raw);
17107 
17108 	/*
17109 	 * Process any queued NSEC3PARAM change requests.
17110 	 */
17111 	while (!ISC_LIST_EMPTY(zone->setnsec3param_queue)) {
17112 		setnsec3param_event = ISC_LIST_HEAD(zone->setnsec3param_queue);
17113 		ISC_LIST_UNLINK(zone->setnsec3param_queue, setnsec3param_event,
17114 				ev_link);
17115 		dummy = NULL;
17116 		zone_iattach(zone, &dummy);
17117 		isc_task_send(zone->task, &setnsec3param_event);
17118 	}
17119 
17120 failure:
17121 	UNLOCK_ZONE(zone);
17122 	if (dbiterator != NULL) {
17123 		dns_dbiterator_destroy(&dbiterator);
17124 	}
17125 	if (result != ISC_R_SUCCESS) {
17126 		dns_zone_log(zone, ISC_LOG_ERROR, "receive_secure_db: %s",
17127 			     dns_result_totext(result));
17128 	}
17129 
17130 	while (!ISC_LIST_EMPTY(nsec3list)) {
17131 		nsec3param_t *nsec3p;
17132 		nsec3p = ISC_LIST_HEAD(nsec3list);
17133 		ISC_LIST_UNLINK(nsec3list, nsec3p, link);
17134 		isc_mem_put(zone->mctx, nsec3p, sizeof(nsec3param_t));
17135 	}
17136 	if (db != NULL) {
17137 		if (version != NULL) {
17138 			dns_db_closeversion(db, &version, false);
17139 		}
17140 		dns_db_detach(&db);
17141 	}
17142 	dns_db_detach(&rawdb);
17143 	dns_zone_idetach(&zone);
17144 
17145 	INSIST(version == NULL);
17146 }
17147 
17148 static isc_result_t
zone_send_securedb(dns_zone_t * zone,dns_db_t * db)17149 zone_send_securedb(dns_zone_t *zone, dns_db_t *db) {
17150 	isc_event_t *e;
17151 	dns_db_t *dummy = NULL;
17152 	dns_zone_t *secure = NULL;
17153 
17154 	e = isc_event_allocate(zone->secure->mctx, zone, DNS_EVENT_ZONESECUREDB,
17155 			       receive_secure_db, zone->secure,
17156 			       sizeof(struct secure_event));
17157 	dns_db_attach(db, &dummy);
17158 	((struct secure_event *)e)->db = dummy;
17159 	INSIST(LOCKED_ZONE(zone->secure));
17160 	zone_iattach(zone->secure, &secure);
17161 	isc_task_send(zone->secure->task, &e);
17162 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_SENDSECURE);
17163 	return (ISC_R_SUCCESS);
17164 }
17165 
17166 isc_result_t
dns_zone_replacedb(dns_zone_t * zone,dns_db_t * db,bool dump)17167 dns_zone_replacedb(dns_zone_t *zone, dns_db_t *db, bool dump) {
17168 	isc_result_t result;
17169 	dns_zone_t *secure = NULL;
17170 
17171 	REQUIRE(DNS_ZONE_VALID(zone));
17172 again:
17173 	LOCK_ZONE(zone);
17174 	if (inline_raw(zone)) {
17175 		secure = zone->secure;
17176 		INSIST(secure != zone);
17177 		TRYLOCK_ZONE(result, secure);
17178 		if (result != ISC_R_SUCCESS) {
17179 			UNLOCK_ZONE(zone);
17180 			secure = NULL;
17181 			isc_thread_yield();
17182 			goto again;
17183 		}
17184 	}
17185 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write);
17186 	result = zone_replacedb(zone, db, dump);
17187 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write);
17188 	if (secure != NULL) {
17189 		UNLOCK_ZONE(secure);
17190 	}
17191 	UNLOCK_ZONE(zone);
17192 	return (result);
17193 }
17194 
17195 static isc_result_t
zone_replacedb(dns_zone_t * zone,dns_db_t * db,bool dump)17196 zone_replacedb(dns_zone_t *zone, dns_db_t *db, bool dump) {
17197 	dns_dbversion_t *ver;
17198 	isc_result_t result;
17199 	unsigned int soacount = 0;
17200 	unsigned int nscount = 0;
17201 
17202 	/*
17203 	 * 'zone' and 'zone->db' locked by caller.
17204 	 */
17205 	REQUIRE(DNS_ZONE_VALID(zone));
17206 	REQUIRE(LOCKED_ZONE(zone));
17207 	if (inline_raw(zone)) {
17208 		REQUIRE(LOCKED_ZONE(zone->secure));
17209 	}
17210 
17211 	result = zone_get_from_db(zone, db, &nscount, &soacount, NULL, NULL,
17212 				  NULL, NULL, NULL, NULL, NULL);
17213 	if (result == ISC_R_SUCCESS) {
17214 		if (soacount != 1) {
17215 			dns_zone_log(zone, ISC_LOG_ERROR, "has %d SOA records",
17216 				     soacount);
17217 			result = DNS_R_BADZONE;
17218 		}
17219 		if (nscount == 0 && zone->type != dns_zone_key) {
17220 			dns_zone_log(zone, ISC_LOG_ERROR, "has no NS records");
17221 			result = DNS_R_BADZONE;
17222 		}
17223 		if (result != ISC_R_SUCCESS) {
17224 			return (result);
17225 		}
17226 	} else {
17227 		dns_zone_log(zone, ISC_LOG_ERROR,
17228 			     "retrieving SOA and NS records failed: %s",
17229 			     dns_result_totext(result));
17230 		return (result);
17231 	}
17232 
17233 	result = check_nsec3param(zone, db);
17234 	if (result != ISC_R_SUCCESS) {
17235 		return (result);
17236 	}
17237 
17238 	ver = NULL;
17239 	dns_db_currentversion(db, &ver);
17240 
17241 	/*
17242 	 * The initial version of a slave zone is always dumped;
17243 	 * subsequent versions may be journaled instead if this
17244 	 * is enabled in the configuration.
17245 	 */
17246 	if (zone->db != NULL && zone->journal != NULL &&
17247 	    DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IXFRFROMDIFFS) &&
17248 	    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER))
17249 	{
17250 		uint32_t serial, oldserial;
17251 
17252 		dns_zone_log(zone, ISC_LOG_DEBUG(3), "generating diffs");
17253 
17254 		result = dns_db_getsoaserial(db, ver, &serial);
17255 		if (result != ISC_R_SUCCESS) {
17256 			dns_zone_log(zone, ISC_LOG_ERROR,
17257 				     "ixfr-from-differences: unable to get "
17258 				     "new serial");
17259 			goto fail;
17260 		}
17261 
17262 		/*
17263 		 * This is checked in zone_postload() for master zones.
17264 		 */
17265 		result = zone_get_from_db(zone, zone->db, NULL, &soacount, NULL,
17266 					  &oldserial, NULL, NULL, NULL, NULL,
17267 					  NULL);
17268 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
17269 		RUNTIME_CHECK(soacount > 0U);
17270 		if ((zone->type == dns_zone_secondary ||
17271 		     (zone->type == dns_zone_redirect &&
17272 		      zone->masters != NULL)) &&
17273 		    !isc_serial_gt(serial, oldserial))
17274 		{
17275 			uint32_t serialmin, serialmax;
17276 			serialmin = (oldserial + 1) & 0xffffffffU;
17277 			serialmax = (oldserial + 0x7fffffffU) & 0xffffffffU;
17278 			dns_zone_log(zone, ISC_LOG_ERROR,
17279 				     "ixfr-from-differences: failed: "
17280 				     "new serial (%u) out of range [%u - %u]",
17281 				     serial, serialmin, serialmax);
17282 			result = ISC_R_RANGE;
17283 			goto fail;
17284 		}
17285 
17286 		result = dns_db_diff(zone->mctx, db, ver, zone->db, NULL,
17287 				     zone->journal);
17288 		if (result != ISC_R_SUCCESS) {
17289 			char strbuf[ISC_STRERRORSIZE];
17290 			strerror_r(errno, strbuf, sizeof(strbuf));
17291 			dns_zone_log(zone, ISC_LOG_ERROR,
17292 				     "ixfr-from-differences: failed: "
17293 				     "%s",
17294 				     strbuf);
17295 			goto fallback;
17296 		}
17297 		if (dump) {
17298 			zone_needdump(zone, DNS_DUMP_DELAY);
17299 		} else {
17300 			zone_journal_compact(zone, zone->db, serial);
17301 		}
17302 		if (zone->type == dns_zone_primary && inline_raw(zone)) {
17303 			zone_send_secureserial(zone, serial);
17304 		}
17305 	} else {
17306 	fallback:
17307 		if (dump && zone->masterfile != NULL) {
17308 			/*
17309 			 * If DNS_ZONEFLG_FORCEXFER was set we don't want
17310 			 * to keep the old masterfile.
17311 			 */
17312 			if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER) &&
17313 			    remove(zone->masterfile) < 0 && errno != ENOENT)
17314 			{
17315 				char strbuf[ISC_STRERRORSIZE];
17316 				strerror_r(errno, strbuf, sizeof(strbuf));
17317 				isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
17318 					      DNS_LOGMODULE_ZONE,
17319 					      ISC_LOG_WARNING,
17320 					      "unable to remove masterfile "
17321 					      "'%s': '%s'",
17322 					      zone->masterfile, strbuf);
17323 			}
17324 			if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) == 0) {
17325 				DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NODELAY);
17326 			} else {
17327 				zone_needdump(zone, 0);
17328 			}
17329 		}
17330 		if (dump && zone->journal != NULL) {
17331 			/*
17332 			 * The in-memory database just changed, and
17333 			 * because 'dump' is set, it didn't change by
17334 			 * being loaded from disk.  Also, we have not
17335 			 * journaled diffs for this change.
17336 			 * Therefore, the on-disk journal is missing
17337 			 * the deltas for this change.	Since it can
17338 			 * no longer be used to bring the zone
17339 			 * up-to-date, it is useless and should be
17340 			 * removed.
17341 			 */
17342 			isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
17343 				      DNS_LOGMODULE_ZONE, ISC_LOG_DEBUG(3),
17344 				      "removing journal file");
17345 			if (remove(zone->journal) < 0 && errno != ENOENT) {
17346 				char strbuf[ISC_STRERRORSIZE];
17347 				strerror_r(errno, strbuf, sizeof(strbuf));
17348 				isc_log_write(dns_lctx, DNS_LOGCATEGORY_GENERAL,
17349 					      DNS_LOGMODULE_ZONE,
17350 					      ISC_LOG_WARNING,
17351 					      "unable to remove journal "
17352 					      "'%s': '%s'",
17353 					      zone->journal, strbuf);
17354 			}
17355 		}
17356 
17357 		if (inline_raw(zone)) {
17358 			zone_send_securedb(zone, db);
17359 		}
17360 	}
17361 
17362 	dns_db_closeversion(db, &ver, false);
17363 
17364 	dns_zone_log(zone, ISC_LOG_DEBUG(3), "replacing zone database");
17365 
17366 	if (zone->db != NULL) {
17367 		zone_detachdb(zone);
17368 	}
17369 	zone_attachdb(zone, db);
17370 	dns_db_settask(zone->db, zone->task);
17371 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED | DNS_ZONEFLG_NEEDNOTIFY);
17372 	return (ISC_R_SUCCESS);
17373 
17374 fail:
17375 	dns_db_closeversion(db, &ver, false);
17376 	return (result);
17377 }
17378 
17379 /* The caller must hold the dblock as a writer. */
17380 static inline void
zone_attachdb(dns_zone_t * zone,dns_db_t * db)17381 zone_attachdb(dns_zone_t *zone, dns_db_t *db) {
17382 	REQUIRE(zone->db == NULL && db != NULL);
17383 
17384 	dns_db_attach(db, &zone->db);
17385 }
17386 
17387 /* The caller must hold the dblock as a writer. */
17388 static inline void
zone_detachdb(dns_zone_t * zone)17389 zone_detachdb(dns_zone_t *zone) {
17390 	REQUIRE(zone->db != NULL);
17391 
17392 	dns_db_detach(&zone->db);
17393 }
17394 
17395 static void
zone_xfrdone(dns_zone_t * zone,isc_result_t result)17396 zone_xfrdone(dns_zone_t *zone, isc_result_t result) {
17397 	isc_time_t now;
17398 	bool again = false;
17399 	unsigned int soacount;
17400 	unsigned int nscount;
17401 	uint32_t serial, refresh, retry, expire, minimum, soattl;
17402 	isc_result_t xfrresult = result;
17403 	bool free_needed;
17404 	dns_zone_t *secure = NULL;
17405 
17406 	REQUIRE(DNS_ZONE_VALID(zone));
17407 
17408 	dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_DEBUG(1),
17409 		      "zone transfer finished: %s", dns_result_totext(result));
17410 
17411 	/*
17412 	 * Obtaining a lock on the zone->secure (see zone_send_secureserial)
17413 	 * could result in a deadlock due to a LOR so we will spin if we
17414 	 * can't obtain the both locks.
17415 	 */
17416 again:
17417 	LOCK_ZONE(zone);
17418 	if (inline_raw(zone)) {
17419 		secure = zone->secure;
17420 		INSIST(secure != zone);
17421 		TRYLOCK_ZONE(result, secure);
17422 		if (result != ISC_R_SUCCESS) {
17423 			UNLOCK_ZONE(zone);
17424 			secure = NULL;
17425 			isc_thread_yield();
17426 			goto again;
17427 		}
17428 	}
17429 
17430 	INSIST(DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH));
17431 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH);
17432 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_SOABEFOREAXFR);
17433 
17434 	TIME_NOW(&now);
17435 	switch (xfrresult) {
17436 	case ISC_R_SUCCESS:
17437 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
17438 	/* FALLTHROUGH */
17439 	case DNS_R_UPTODATE:
17440 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FORCEXFER);
17441 		/*
17442 		 * Has the zone expired underneath us?
17443 		 */
17444 		ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
17445 		if (zone->db == NULL) {
17446 			ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
17447 			goto same_master;
17448 		}
17449 
17450 		/*
17451 		 * Update the zone structure's data from the actual
17452 		 * SOA received.
17453 		 */
17454 		nscount = 0;
17455 		soacount = 0;
17456 		INSIST(zone->db != NULL);
17457 		result = zone_get_from_db(zone, zone->db, &nscount, &soacount,
17458 					  &soattl, &serial, &refresh, &retry,
17459 					  &expire, &minimum, NULL);
17460 		ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
17461 		if (result == ISC_R_SUCCESS) {
17462 			if (soacount != 1) {
17463 				dns_zone_log(zone, ISC_LOG_ERROR,
17464 					     "transferred zone "
17465 					     "has %d SOA records",
17466 					     soacount);
17467 				if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HAVETIMERS))
17468 				{
17469 					zone->refresh = DNS_ZONE_DEFAULTREFRESH;
17470 					zone->retry = DNS_ZONE_DEFAULTRETRY;
17471 				}
17472 				DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
17473 				zone_unload(zone);
17474 				goto next_master;
17475 			}
17476 			if (nscount == 0) {
17477 				dns_zone_log(zone, ISC_LOG_ERROR,
17478 					     "transferred zone "
17479 					     "has no NS records");
17480 				if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HAVETIMERS))
17481 				{
17482 					zone->refresh = DNS_ZONE_DEFAULTREFRESH;
17483 					zone->retry = DNS_ZONE_DEFAULTRETRY;
17484 				}
17485 				DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
17486 				zone_unload(zone);
17487 				goto next_master;
17488 			}
17489 			zone->refresh = RANGE(refresh, zone->minrefresh,
17490 					      zone->maxrefresh);
17491 			zone->retry = RANGE(retry, zone->minretry,
17492 					    zone->maxretry);
17493 			zone->expire = RANGE(expire,
17494 					     zone->refresh + zone->retry,
17495 					     DNS_MAX_EXPIRE);
17496 			zone->soattl = soattl;
17497 			zone->minimum = minimum;
17498 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HAVETIMERS);
17499 		}
17500 
17501 		/*
17502 		 * Set our next update/expire times.
17503 		 */
17504 		if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDREFRESH)) {
17505 			DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDREFRESH);
17506 			zone->refreshtime = now;
17507 			DNS_ZONE_TIME_ADD(&now, zone->expire,
17508 					  &zone->expiretime);
17509 		} else {
17510 			DNS_ZONE_JITTER_ADD(&now, zone->refresh,
17511 					    &zone->refreshtime);
17512 			DNS_ZONE_TIME_ADD(&now, zone->expire,
17513 					  &zone->expiretime);
17514 		}
17515 		if (result == ISC_R_SUCCESS && xfrresult == ISC_R_SUCCESS) {
17516 			char buf[DNS_NAME_FORMATSIZE + sizeof(": TSIG ''")];
17517 			if (zone->tsigkey != NULL) {
17518 				char namebuf[DNS_NAME_FORMATSIZE];
17519 				dns_name_format(&zone->tsigkey->name, namebuf,
17520 						sizeof(namebuf));
17521 				snprintf(buf, sizeof(buf), ": TSIG '%s'",
17522 					 namebuf);
17523 			} else {
17524 				buf[0] = '\0';
17525 			}
17526 			dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN,
17527 				      ISC_LOG_INFO, "transferred serial %u%s",
17528 				      serial, buf);
17529 			if (inline_raw(zone)) {
17530 				zone_send_secureserial(zone, serial);
17531 			}
17532 		}
17533 
17534 		/*
17535 		 * This is not necessary if we just performed a AXFR
17536 		 * however it is necessary for an IXFR / UPTODATE and
17537 		 * won't hurt with an AXFR.
17538 		 */
17539 		if (zone->masterfile != NULL || zone->journal != NULL) {
17540 			unsigned int delay = DNS_DUMP_DELAY;
17541 
17542 			result = ISC_R_FAILURE;
17543 			if (zone->journal != NULL) {
17544 				result = isc_file_settime(zone->journal, &now);
17545 			}
17546 			if (result != ISC_R_SUCCESS && zone->masterfile != NULL)
17547 			{
17548 				result = isc_file_settime(zone->masterfile,
17549 							  &now);
17550 			}
17551 
17552 			if ((DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NODELAY) != 0) ||
17553 			    result == ISC_R_FILENOTFOUND)
17554 			{
17555 				delay = 0;
17556 			}
17557 
17558 			if ((result == ISC_R_SUCCESS ||
17559 			     result == ISC_R_FILENOTFOUND) &&
17560 			    zone->masterfile != NULL)
17561 			{
17562 				zone_needdump(zone, delay);
17563 			} else if (result != ISC_R_SUCCESS) {
17564 				dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN,
17565 					      ISC_LOG_ERROR,
17566 					      "transfer: could not set file "
17567 					      "modification time of '%s': %s",
17568 					      zone->masterfile,
17569 					      dns_result_totext(result));
17570 			}
17571 		}
17572 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NODELAY);
17573 		inc_stats(zone, dns_zonestatscounter_xfrsuccess);
17574 		break;
17575 
17576 	case DNS_R_BADIXFR:
17577 		/* Force retry with AXFR. */
17578 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOIXFR);
17579 		goto same_master;
17580 
17581 	case DNS_R_TOOMANYRECORDS:
17582 	case DNS_R_VERIFYFAILURE:
17583 		DNS_ZONE_JITTER_ADD(&now, zone->refresh, &zone->refreshtime);
17584 		inc_stats(zone, dns_zonestatscounter_xfrfail);
17585 		break;
17586 
17587 	default:
17588 	next_master:
17589 		/*
17590 		 * Skip to next failed / untried master.
17591 		 */
17592 		do {
17593 			zone->curmaster++;
17594 		} while (zone->curmaster < zone->masterscnt &&
17595 			 zone->mastersok[zone->curmaster]);
17596 		/* FALLTHROUGH */
17597 	same_master:
17598 		if (zone->curmaster >= zone->masterscnt) {
17599 			zone->curmaster = 0;
17600 			if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_USEALTXFRSRC) &&
17601 			    !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEALTXFRSRC))
17602 			{
17603 				DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESH);
17604 				DNS_ZONE_SETFLAG(zone,
17605 						 DNS_ZONEFLG_USEALTXFRSRC);
17606 				while (zone->curmaster < zone->masterscnt &&
17607 				       zone->mastersok[zone->curmaster]) {
17608 					zone->curmaster++;
17609 				}
17610 				again = true;
17611 			} else {
17612 				DNS_ZONE_CLRFLAG(zone,
17613 						 DNS_ZONEFLG_USEALTXFRSRC);
17614 			}
17615 		} else {
17616 			DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESH);
17617 			again = true;
17618 		}
17619 		inc_stats(zone, dns_zonestatscounter_xfrfail);
17620 		break;
17621 	}
17622 	zone_settimer(zone, &now);
17623 
17624 	/*
17625 	 * If creating the transfer object failed, zone->xfr is NULL.
17626 	 * Otherwise, we are called as the done callback of a zone
17627 	 * transfer object that just entered its shutting-down
17628 	 * state.  Since we are no longer responsible for shutting
17629 	 * it down, we can detach our reference.
17630 	 */
17631 	if (zone->xfr != NULL) {
17632 		dns_xfrin_detach(&zone->xfr);
17633 	}
17634 
17635 	if (zone->tsigkey != NULL) {
17636 		dns_tsigkey_detach(&zone->tsigkey);
17637 	}
17638 
17639 	/*
17640 	 * Handle any deferred journal compaction.
17641 	 */
17642 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDCOMPACT)) {
17643 		dns_db_t *db = NULL;
17644 		if (dns_zone_getdb(zone, &db) == ISC_R_SUCCESS) {
17645 			zone_journal_compact(zone, db, zone->compact_serial);
17646 			dns_db_detach(&db);
17647 			DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDCOMPACT);
17648 		}
17649 	}
17650 
17651 	if (secure != NULL) {
17652 		UNLOCK_ZONE(secure);
17653 	}
17654 	/*
17655 	 * This transfer finishing freed up a transfer quota slot.
17656 	 * Let any other zones waiting for quota have it.
17657 	 */
17658 	if (zone->zmgr != NULL &&
17659 	    zone->statelist == &zone->zmgr->xfrin_in_progress) {
17660 		UNLOCK_ZONE(zone);
17661 		RWLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write);
17662 		ISC_LIST_UNLINK(zone->zmgr->xfrin_in_progress, zone, statelink);
17663 		zone->statelist = NULL;
17664 		zmgr_resume_xfrs(zone->zmgr, false);
17665 		RWUNLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write);
17666 		LOCK_ZONE(zone);
17667 	}
17668 
17669 	/*
17670 	 * Retry with a different server if necessary.
17671 	 */
17672 	if (again && !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
17673 		queue_soa_query(zone);
17674 	}
17675 
17676 	isc_refcount_decrement(&zone->irefs);
17677 	free_needed = exit_check(zone);
17678 	UNLOCK_ZONE(zone);
17679 	if (free_needed) {
17680 		zone_free(zone);
17681 	}
17682 }
17683 
17684 static void
zone_loaddone(void * arg,isc_result_t result)17685 zone_loaddone(void *arg, isc_result_t result) {
17686 	static char me[] = "zone_loaddone";
17687 	dns_load_t *load = arg;
17688 	dns_zone_t *zone;
17689 	isc_result_t tresult;
17690 	dns_zone_t *secure = NULL;
17691 
17692 	REQUIRE(DNS_LOAD_VALID(load));
17693 	zone = load->zone;
17694 
17695 	ENTER;
17696 
17697 	/*
17698 	 * If zone loading failed, remove the update db callbacks prior
17699 	 * to calling the list of callbacks in the zone load structure.
17700 	 */
17701 	if (result != ISC_R_SUCCESS) {
17702 		dns_zone_rpz_disable_db(zone, load->db);
17703 		dns_zone_catz_disable_db(zone, load->db);
17704 	}
17705 
17706 	tresult = dns_db_endload(load->db, &load->callbacks);
17707 	if (tresult != ISC_R_SUCCESS &&
17708 	    (result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE))
17709 	{
17710 		result = tresult;
17711 	}
17712 
17713 	/*
17714 	 * Lock hierarchy: zmgr, zone, raw.
17715 	 */
17716 again:
17717 	LOCK_ZONE(zone);
17718 	INSIST(zone != zone->raw);
17719 	if (inline_secure(zone)) {
17720 		LOCK_ZONE(zone->raw);
17721 	} else if (inline_raw(zone)) {
17722 		secure = zone->secure;
17723 		TRYLOCK_ZONE(tresult, secure);
17724 		if (tresult != ISC_R_SUCCESS) {
17725 			UNLOCK_ZONE(zone);
17726 			secure = NULL;
17727 			isc_thread_yield();
17728 			goto again;
17729 		}
17730 	}
17731 	(void)zone_postload(zone, load->db, load->loadtime, result);
17732 	zonemgr_putio(&zone->readio);
17733 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADING);
17734 	zone_idetach(&load->callbacks.zone);
17735 	/*
17736 	 * Leave the zone frozen if the reload fails.
17737 	 */
17738 	if ((result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE) &&
17739 	    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_THAW))
17740 	{
17741 		zone->update_disabled = false;
17742 	}
17743 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_THAW);
17744 	if (inline_secure(zone)) {
17745 		UNLOCK_ZONE(zone->raw);
17746 	} else if (secure != NULL) {
17747 		UNLOCK_ZONE(secure);
17748 	}
17749 	UNLOCK_ZONE(zone);
17750 
17751 	load->magic = 0;
17752 	dns_db_detach(&load->db);
17753 	if (load->zone->lctx != NULL) {
17754 		dns_loadctx_detach(&load->zone->lctx);
17755 	}
17756 	dns_zone_idetach(&load->zone);
17757 	isc_mem_putanddetach(&load->mctx, load, sizeof(*load));
17758 }
17759 
17760 void
dns_zone_getssutable(dns_zone_t * zone,dns_ssutable_t ** table)17761 dns_zone_getssutable(dns_zone_t *zone, dns_ssutable_t **table) {
17762 	REQUIRE(DNS_ZONE_VALID(zone));
17763 	REQUIRE(table != NULL);
17764 	REQUIRE(*table == NULL);
17765 
17766 	LOCK_ZONE(zone);
17767 	if (zone->ssutable != NULL) {
17768 		dns_ssutable_attach(zone->ssutable, table);
17769 	}
17770 	UNLOCK_ZONE(zone);
17771 }
17772 
17773 void
dns_zone_setssutable(dns_zone_t * zone,dns_ssutable_t * table)17774 dns_zone_setssutable(dns_zone_t *zone, dns_ssutable_t *table) {
17775 	REQUIRE(DNS_ZONE_VALID(zone));
17776 
17777 	LOCK_ZONE(zone);
17778 	if (zone->ssutable != NULL) {
17779 		dns_ssutable_detach(&zone->ssutable);
17780 	}
17781 	if (table != NULL) {
17782 		dns_ssutable_attach(table, &zone->ssutable);
17783 	}
17784 	UNLOCK_ZONE(zone);
17785 }
17786 
17787 void
dns_zone_setsigvalidityinterval(dns_zone_t * zone,uint32_t interval)17788 dns_zone_setsigvalidityinterval(dns_zone_t *zone, uint32_t interval) {
17789 	REQUIRE(DNS_ZONE_VALID(zone));
17790 
17791 	zone->sigvalidityinterval = interval;
17792 }
17793 
17794 uint32_t
dns_zone_getsigvalidityinterval(dns_zone_t * zone)17795 dns_zone_getsigvalidityinterval(dns_zone_t *zone) {
17796 	REQUIRE(DNS_ZONE_VALID(zone));
17797 
17798 	return (zone->sigvalidityinterval);
17799 }
17800 
17801 void
dns_zone_setkeyvalidityinterval(dns_zone_t * zone,uint32_t interval)17802 dns_zone_setkeyvalidityinterval(dns_zone_t *zone, uint32_t interval) {
17803 	REQUIRE(DNS_ZONE_VALID(zone));
17804 
17805 	zone->keyvalidityinterval = interval;
17806 }
17807 
17808 uint32_t
dns_zone_getkeyvalidityinterval(dns_zone_t * zone)17809 dns_zone_getkeyvalidityinterval(dns_zone_t *zone) {
17810 	REQUIRE(DNS_ZONE_VALID(zone));
17811 
17812 	return (zone->keyvalidityinterval);
17813 }
17814 
17815 void
dns_zone_setsigresigninginterval(dns_zone_t * zone,uint32_t interval)17816 dns_zone_setsigresigninginterval(dns_zone_t *zone, uint32_t interval) {
17817 	isc_time_t now;
17818 
17819 	REQUIRE(DNS_ZONE_VALID(zone));
17820 
17821 	LOCK_ZONE(zone);
17822 	zone->sigresigninginterval = interval;
17823 	set_resigntime(zone);
17824 	if (zone->task != NULL) {
17825 		TIME_NOW(&now);
17826 		zone_settimer(zone, &now);
17827 	}
17828 	UNLOCK_ZONE(zone);
17829 }
17830 
17831 uint32_t
dns_zone_getsigresigninginterval(dns_zone_t * zone)17832 dns_zone_getsigresigninginterval(dns_zone_t *zone) {
17833 	REQUIRE(DNS_ZONE_VALID(zone));
17834 
17835 	return (zone->sigresigninginterval);
17836 }
17837 
17838 static void
queue_xfrin(dns_zone_t * zone)17839 queue_xfrin(dns_zone_t *zone) {
17840 	const char me[] = "queue_xfrin";
17841 	isc_result_t result;
17842 	dns_zonemgr_t *zmgr = zone->zmgr;
17843 
17844 	ENTER;
17845 
17846 	INSIST(zone->statelist == NULL);
17847 
17848 	RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
17849 	ISC_LIST_APPEND(zmgr->waiting_for_xfrin, zone, statelink);
17850 	isc_refcount_increment0(&zone->irefs);
17851 	zone->statelist = &zmgr->waiting_for_xfrin;
17852 	result = zmgr_start_xfrin_ifquota(zmgr, zone);
17853 	RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
17854 
17855 	if (result == ISC_R_QUOTA) {
17856 		dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO,
17857 			      "zone transfer deferred due to quota");
17858 	} else if (result != ISC_R_SUCCESS) {
17859 		dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_ERROR,
17860 			      "starting zone transfer: %s",
17861 			      isc_result_totext(result));
17862 	}
17863 }
17864 
17865 /*
17866  * This event callback is called when a zone has received
17867  * any necessary zone transfer quota.  This is the time
17868  * to go ahead and start the transfer.
17869  */
17870 static void
got_transfer_quota(isc_task_t * task,isc_event_t * event)17871 got_transfer_quota(isc_task_t *task, isc_event_t *event) {
17872 	isc_result_t result = ISC_R_SUCCESS;
17873 	dns_peer_t *peer = NULL;
17874 	char master[ISC_SOCKADDR_FORMATSIZE];
17875 	char source[ISC_SOCKADDR_FORMATSIZE];
17876 	dns_rdatatype_t xfrtype;
17877 	dns_zone_t *zone = event->ev_arg;
17878 	isc_netaddr_t masterip;
17879 	isc_sockaddr_t sourceaddr;
17880 	isc_sockaddr_t masteraddr;
17881 	isc_time_t now;
17882 	const char *soa_before = "";
17883 	isc_dscp_t dscp = -1;
17884 	bool loaded;
17885 
17886 	UNUSED(task);
17887 
17888 	INSIST(task == zone->task);
17889 
17890 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
17891 		result = ISC_R_CANCELED;
17892 		goto cleanup;
17893 	}
17894 
17895 	TIME_NOW(&now);
17896 
17897 	isc_sockaddr_format(&zone->masteraddr, master, sizeof(master));
17898 	if (dns_zonemgr_unreachable(zone->zmgr, &zone->masteraddr,
17899 				    &zone->sourceaddr, &now))
17900 	{
17901 		isc_sockaddr_format(&zone->sourceaddr, source, sizeof(source));
17902 		dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO,
17903 			      "got_transfer_quota: skipping zone transfer as "
17904 			      "master %s (source %s) is unreachable (cached)",
17905 			      master, source);
17906 		result = ISC_R_CANCELED;
17907 		goto cleanup;
17908 	}
17909 
17910 	isc_netaddr_fromsockaddr(&masterip, &zone->masteraddr);
17911 	(void)dns_peerlist_peerbyaddr(zone->view->peers, &masterip, &peer);
17912 
17913 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SOABEFOREAXFR)) {
17914 		soa_before = "SOA before ";
17915 	}
17916 	/*
17917 	 * Decide whether we should request IXFR or AXFR.
17918 	 */
17919 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
17920 	loaded = (zone->db != NULL);
17921 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
17922 
17923 	if (!loaded) {
17924 		dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_DEBUG(1),
17925 			      "no database exists yet, requesting AXFR of "
17926 			      "initial version from %s",
17927 			      master);
17928 		xfrtype = dns_rdatatype_axfr;
17929 	} else if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER)) {
17930 		dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_DEBUG(1),
17931 			      "forced reload, requesting AXFR of "
17932 			      "initial version from %s",
17933 			      master);
17934 		xfrtype = dns_rdatatype_axfr;
17935 	} else if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOIXFR)) {
17936 		dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_DEBUG(1),
17937 			      "retrying with AXFR from %s due to "
17938 			      "previous IXFR failure",
17939 			      master);
17940 		xfrtype = dns_rdatatype_axfr;
17941 		LOCK_ZONE(zone);
17942 		DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOIXFR);
17943 		UNLOCK_ZONE(zone);
17944 	} else {
17945 		bool use_ixfr = true;
17946 		if (peer != NULL) {
17947 			result = dns_peer_getrequestixfr(peer, &use_ixfr);
17948 		}
17949 		if (peer == NULL || result != ISC_R_SUCCESS) {
17950 			use_ixfr = zone->requestixfr;
17951 		}
17952 		if (!use_ixfr) {
17953 			dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN,
17954 				      ISC_LOG_DEBUG(1),
17955 				      "IXFR disabled, "
17956 				      "requesting %sAXFR from %s",
17957 				      soa_before, master);
17958 			if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SOABEFOREAXFR)) {
17959 				xfrtype = dns_rdatatype_soa;
17960 			} else {
17961 				xfrtype = dns_rdatatype_axfr;
17962 			}
17963 		} else {
17964 			dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN,
17965 				      ISC_LOG_DEBUG(1),
17966 				      "requesting IXFR from %s", master);
17967 			xfrtype = dns_rdatatype_ixfr;
17968 		}
17969 	}
17970 
17971 	/*
17972 	 * Determine if we should attempt to sign the request with TSIG.
17973 	 */
17974 	result = ISC_R_NOTFOUND;
17975 
17976 	/*
17977 	 * First, look for a tsig key in the master statement, then
17978 	 * try for a server key.
17979 	 */
17980 	if ((zone->masterkeynames != NULL) &&
17981 	    (zone->masterkeynames[zone->curmaster] != NULL))
17982 	{
17983 		dns_view_t *view = dns_zone_getview(zone);
17984 		dns_name_t *keyname = zone->masterkeynames[zone->curmaster];
17985 		result = dns_view_gettsig(view, keyname, &zone->tsigkey);
17986 	}
17987 	if (zone->tsigkey == NULL) {
17988 		result = dns_view_getpeertsig(zone->view, &masterip,
17989 					      &zone->tsigkey);
17990 	}
17991 
17992 	if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
17993 		dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_ERROR,
17994 			      "could not get TSIG key for zone transfer: %s",
17995 			      isc_result_totext(result));
17996 	}
17997 
17998 	if (zone->masterdscps != NULL) {
17999 		dscp = zone->masterdscps[zone->curmaster];
18000 	}
18001 
18002 	LOCK_ZONE(zone);
18003 	masteraddr = zone->masteraddr;
18004 	sourceaddr = zone->sourceaddr;
18005 	switch (isc_sockaddr_pf(&masteraddr)) {
18006 	case PF_INET:
18007 		if (dscp == -1) {
18008 			dscp = zone->xfrsource4dscp;
18009 		}
18010 		break;
18011 	case PF_INET6:
18012 		if (dscp == -1) {
18013 			dscp = zone->xfrsource6dscp;
18014 		}
18015 		break;
18016 	default:
18017 		INSIST(0);
18018 		ISC_UNREACHABLE();
18019 	}
18020 	UNLOCK_ZONE(zone);
18021 	INSIST(isc_sockaddr_pf(&masteraddr) == isc_sockaddr_pf(&sourceaddr));
18022 	result = dns_xfrin_create(zone, xfrtype, &masteraddr, &sourceaddr, dscp,
18023 				  zone->tsigkey, zone->mctx,
18024 				  zone->zmgr->timermgr, zone->zmgr->socketmgr,
18025 				  zone->task, zone_xfrdone, &zone->xfr);
18026 	if (result == ISC_R_SUCCESS) {
18027 		LOCK_ZONE(zone);
18028 		if (xfrtype == dns_rdatatype_axfr) {
18029 			if (isc_sockaddr_pf(&masteraddr) == PF_INET) {
18030 				inc_stats(zone, dns_zonestatscounter_axfrreqv4);
18031 			} else {
18032 				inc_stats(zone, dns_zonestatscounter_axfrreqv6);
18033 			}
18034 		} else if (xfrtype == dns_rdatatype_ixfr) {
18035 			if (isc_sockaddr_pf(&masteraddr) == PF_INET) {
18036 				inc_stats(zone, dns_zonestatscounter_ixfrreqv4);
18037 			} else {
18038 				inc_stats(zone, dns_zonestatscounter_ixfrreqv6);
18039 			}
18040 		}
18041 		UNLOCK_ZONE(zone);
18042 	}
18043 cleanup:
18044 	/*
18045 	 * Any failure in this function is handled like a failed
18046 	 * zone transfer.  This ensures that we get removed from
18047 	 * zmgr->xfrin_in_progress.
18048 	 */
18049 	if (result != ISC_R_SUCCESS) {
18050 		zone_xfrdone(zone, result);
18051 	}
18052 
18053 	isc_event_free(&event);
18054 }
18055 
18056 /*
18057  * Update forwarding support.
18058  */
18059 
18060 static void
forward_destroy(dns_forward_t * forward)18061 forward_destroy(dns_forward_t *forward) {
18062 	forward->magic = 0;
18063 	if (forward->request != NULL) {
18064 		dns_request_destroy(&forward->request);
18065 	}
18066 	if (forward->msgbuf != NULL) {
18067 		isc_buffer_free(&forward->msgbuf);
18068 	}
18069 	if (forward->zone != NULL) {
18070 		LOCK(&forward->zone->lock);
18071 		if (ISC_LINK_LINKED(forward, link)) {
18072 			ISC_LIST_UNLINK(forward->zone->forwards, forward, link);
18073 		}
18074 		UNLOCK(&forward->zone->lock);
18075 		dns_zone_idetach(&forward->zone);
18076 	}
18077 	isc_mem_putanddetach(&forward->mctx, forward, sizeof(*forward));
18078 }
18079 
18080 static isc_result_t
sendtomaster(dns_forward_t * forward)18081 sendtomaster(dns_forward_t *forward) {
18082 	isc_result_t result;
18083 	isc_sockaddr_t src;
18084 	isc_dscp_t dscp = -1;
18085 
18086 	LOCK_ZONE(forward->zone);
18087 
18088 	if (DNS_ZONE_FLAG(forward->zone, DNS_ZONEFLG_EXITING)) {
18089 		UNLOCK_ZONE(forward->zone);
18090 		return (ISC_R_CANCELED);
18091 	}
18092 
18093 	if (forward->which >= forward->zone->masterscnt) {
18094 		UNLOCK_ZONE(forward->zone);
18095 		return (ISC_R_NOMORE);
18096 	}
18097 
18098 	forward->addr = forward->zone->masters[forward->which];
18099 	/*
18100 	 * Always use TCP regardless of whether the original update
18101 	 * used TCP.
18102 	 * XXX The timeout may but a bit small if we are far down a
18103 	 * transfer graph and the master has to try several masters.
18104 	 */
18105 	switch (isc_sockaddr_pf(&forward->addr)) {
18106 	case PF_INET:
18107 		src = forward->zone->xfrsource4;
18108 		dscp = forward->zone->xfrsource4dscp;
18109 		break;
18110 	case PF_INET6:
18111 		src = forward->zone->xfrsource6;
18112 		dscp = forward->zone->xfrsource6dscp;
18113 		break;
18114 	default:
18115 		result = ISC_R_NOTIMPLEMENTED;
18116 		goto unlock;
18117 	}
18118 	result = dns_request_createraw(forward->zone->view->requestmgr,
18119 				       forward->msgbuf, &src, &forward->addr,
18120 				       dscp, forward->options, 15 /* XXX */, 0,
18121 				       0, forward->zone->task, forward_callback,
18122 				       forward, &forward->request);
18123 	if (result == ISC_R_SUCCESS) {
18124 		if (!ISC_LINK_LINKED(forward, link)) {
18125 			ISC_LIST_APPEND(forward->zone->forwards, forward, link);
18126 		}
18127 	}
18128 
18129 unlock:
18130 	UNLOCK_ZONE(forward->zone);
18131 	return (result);
18132 }
18133 
18134 static void
forward_callback(isc_task_t * task,isc_event_t * event)18135 forward_callback(isc_task_t *task, isc_event_t *event) {
18136 	const char me[] = "forward_callback";
18137 	dns_requestevent_t *revent = (dns_requestevent_t *)event;
18138 	dns_message_t *msg = NULL;
18139 	char master[ISC_SOCKADDR_FORMATSIZE];
18140 	isc_result_t result;
18141 	dns_forward_t *forward;
18142 	dns_zone_t *zone;
18143 
18144 	UNUSED(task);
18145 
18146 	forward = revent->ev_arg;
18147 	INSIST(DNS_FORWARD_VALID(forward));
18148 	zone = forward->zone;
18149 	INSIST(DNS_ZONE_VALID(zone));
18150 
18151 	ENTER;
18152 
18153 	isc_sockaddr_format(&forward->addr, master, sizeof(master));
18154 
18155 	if (revent->result != ISC_R_SUCCESS) {
18156 		dns_zone_log(zone, ISC_LOG_INFO,
18157 			     "could not forward dynamic update to %s: %s",
18158 			     master, dns_result_totext(revent->result));
18159 		goto next_master;
18160 	}
18161 
18162 	dns_message_create(zone->mctx, DNS_MESSAGE_INTENTPARSE, &msg);
18163 
18164 	result = dns_request_getresponse(revent->request, msg,
18165 					 DNS_MESSAGEPARSE_PRESERVEORDER |
18166 						 DNS_MESSAGEPARSE_CLONEBUFFER);
18167 	if (result != ISC_R_SUCCESS) {
18168 		goto next_master;
18169 	}
18170 
18171 	/*
18172 	 * Unexpected opcode.
18173 	 */
18174 	if (msg->opcode != dns_opcode_update) {
18175 		char opcode[128];
18176 		isc_buffer_t rb;
18177 
18178 		isc_buffer_init(&rb, opcode, sizeof(opcode));
18179 		(void)dns_opcode_totext(msg->rcode, &rb);
18180 
18181 		dns_zone_log(zone, ISC_LOG_INFO,
18182 			     "forwarding dynamic update: "
18183 			     "unexpected opcode (%.*s) from %s",
18184 			     (int)rb.used, opcode, master);
18185 		goto next_master;
18186 	}
18187 
18188 	switch (msg->rcode) {
18189 	/*
18190 	 * Pass these rcodes back to client.
18191 	 */
18192 	case dns_rcode_noerror:
18193 	case dns_rcode_yxdomain:
18194 	case dns_rcode_yxrrset:
18195 	case dns_rcode_nxrrset:
18196 	case dns_rcode_refused:
18197 	case dns_rcode_nxdomain: {
18198 		char rcode[128];
18199 		isc_buffer_t rb;
18200 
18201 		isc_buffer_init(&rb, rcode, sizeof(rcode));
18202 		(void)dns_rcode_totext(msg->rcode, &rb);
18203 		dns_zone_log(zone, ISC_LOG_INFO,
18204 			     "forwarded dynamic update: "
18205 			     "master %s returned: %.*s",
18206 			     master, (int)rb.used, rcode);
18207 		break;
18208 	}
18209 
18210 	/* These should not occur if the primaries/zone are valid. */
18211 	case dns_rcode_notzone:
18212 	case dns_rcode_notauth: {
18213 		char rcode[128];
18214 		isc_buffer_t rb;
18215 
18216 		isc_buffer_init(&rb, rcode, sizeof(rcode));
18217 		(void)dns_rcode_totext(msg->rcode, &rb);
18218 		dns_zone_log(zone, ISC_LOG_WARNING,
18219 			     "forwarding dynamic update: "
18220 			     "unexpected response: master %s returned: %.*s",
18221 			     master, (int)rb.used, rcode);
18222 		goto next_master;
18223 	}
18224 
18225 	/* Try another server for these rcodes. */
18226 	case dns_rcode_formerr:
18227 	case dns_rcode_servfail:
18228 	case dns_rcode_notimp:
18229 	case dns_rcode_badvers:
18230 	default:
18231 		goto next_master;
18232 	}
18233 
18234 	/* call callback */
18235 	(forward->callback)(forward->callback_arg, ISC_R_SUCCESS, msg);
18236 	msg = NULL;
18237 	dns_request_destroy(&forward->request);
18238 	forward_destroy(forward);
18239 	isc_event_free(&event);
18240 	return;
18241 
18242 next_master:
18243 	if (msg != NULL) {
18244 		dns_message_detach(&msg);
18245 	}
18246 	isc_event_free(&event);
18247 	forward->which++;
18248 	dns_request_destroy(&forward->request);
18249 	result = sendtomaster(forward);
18250 	if (result != ISC_R_SUCCESS) {
18251 		/* call callback */
18252 		dns_zone_log(zone, ISC_LOG_DEBUG(3),
18253 			     "exhausted dynamic update forwarder list");
18254 		(forward->callback)(forward->callback_arg, result, NULL);
18255 		forward_destroy(forward);
18256 	}
18257 }
18258 
18259 isc_result_t
dns_zone_forwardupdate(dns_zone_t * zone,dns_message_t * msg,dns_updatecallback_t callback,void * callback_arg)18260 dns_zone_forwardupdate(dns_zone_t *zone, dns_message_t *msg,
18261 		       dns_updatecallback_t callback, void *callback_arg) {
18262 	dns_forward_t *forward;
18263 	isc_result_t result;
18264 	isc_region_t *mr;
18265 
18266 	REQUIRE(DNS_ZONE_VALID(zone));
18267 	REQUIRE(msg != NULL);
18268 	REQUIRE(callback != NULL);
18269 
18270 	forward = isc_mem_get(zone->mctx, sizeof(*forward));
18271 
18272 	forward->request = NULL;
18273 	forward->zone = NULL;
18274 	forward->msgbuf = NULL;
18275 	forward->which = 0;
18276 	forward->mctx = 0;
18277 	forward->callback = callback;
18278 	forward->callback_arg = callback_arg;
18279 	ISC_LINK_INIT(forward, link);
18280 	forward->magic = FORWARD_MAGIC;
18281 	forward->options = DNS_REQUESTOPT_TCP;
18282 	/*
18283 	 * If we have a SIG(0) signed message we need to preserve the
18284 	 * query id as that is included in the SIG(0) computation.
18285 	 */
18286 	if (msg->sig0 != NULL) {
18287 		forward->options |= DNS_REQUESTOPT_FIXEDID;
18288 	}
18289 
18290 	mr = dns_message_getrawmessage(msg);
18291 	if (mr == NULL) {
18292 		result = ISC_R_UNEXPECTEDEND;
18293 		goto cleanup;
18294 	}
18295 
18296 	isc_buffer_allocate(zone->mctx, &forward->msgbuf, mr->length);
18297 	result = isc_buffer_copyregion(forward->msgbuf, mr);
18298 	if (result != ISC_R_SUCCESS) {
18299 		goto cleanup;
18300 	}
18301 
18302 	isc_mem_attach(zone->mctx, &forward->mctx);
18303 	dns_zone_iattach(zone, &forward->zone);
18304 	result = sendtomaster(forward);
18305 
18306 cleanup:
18307 	if (result != ISC_R_SUCCESS) {
18308 		forward_destroy(forward);
18309 	}
18310 	return (result);
18311 }
18312 
18313 isc_result_t
dns_zone_next(dns_zone_t * zone,dns_zone_t ** next)18314 dns_zone_next(dns_zone_t *zone, dns_zone_t **next) {
18315 	REQUIRE(DNS_ZONE_VALID(zone));
18316 	REQUIRE(next != NULL && *next == NULL);
18317 
18318 	*next = ISC_LIST_NEXT(zone, link);
18319 	if (*next == NULL) {
18320 		return (ISC_R_NOMORE);
18321 	} else {
18322 		return (ISC_R_SUCCESS);
18323 	}
18324 }
18325 
18326 isc_result_t
dns_zone_first(dns_zonemgr_t * zmgr,dns_zone_t ** first)18327 dns_zone_first(dns_zonemgr_t *zmgr, dns_zone_t **first) {
18328 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
18329 	REQUIRE(first != NULL && *first == NULL);
18330 
18331 	*first = ISC_LIST_HEAD(zmgr->zones);
18332 	if (*first == NULL) {
18333 		return (ISC_R_NOMORE);
18334 	} else {
18335 		return (ISC_R_SUCCESS);
18336 	}
18337 }
18338 
18339 /***
18340  ***	Zone manager.
18341  ***/
18342 
18343 #define KEYMGMT_OVERCOMMIT 3
18344 #define KEYMGMT_BITS_MIN   2U
18345 #define KEYMGMT_BITS_MAX   32U
18346 
18347 /*
18348  * WMM: Static hash functions copied from lib/dns/rbtdb.c. Should be moved to
18349  * lib/isc/hash.c when we refactor the hash table code.
18350  */
18351 #define GOLDEN_RATIO_32 0x61C88647
18352 #define HASHSIZE(bits)	(UINT64_C(1) << (bits))
18353 
18354 static inline uint32_t
hash_index(uint32_t val,uint32_t bits)18355 hash_index(uint32_t val, uint32_t bits) {
18356 	return (val * GOLDEN_RATIO_32 >> (32 - bits));
18357 }
18358 
18359 static uint32_t
hash_bits_grow(uint32_t bits,uint32_t count)18360 hash_bits_grow(uint32_t bits, uint32_t count) {
18361 	uint32_t newbits = bits;
18362 	while (count >= HASHSIZE(newbits) && newbits < KEYMGMT_BITS_MAX) {
18363 		newbits++;
18364 	}
18365 	return (newbits);
18366 }
18367 
18368 static uint32_t
hash_bits_shrink(uint32_t bits,uint32_t count)18369 hash_bits_shrink(uint32_t bits, uint32_t count) {
18370 	uint32_t newbits = bits;
18371 	while (count <= HASHSIZE(newbits) && newbits > KEYMGMT_BITS_MIN) {
18372 		newbits--;
18373 	}
18374 	return (newbits);
18375 }
18376 
18377 static void
zonemgr_keymgmt_init(dns_zonemgr_t * zmgr)18378 zonemgr_keymgmt_init(dns_zonemgr_t *zmgr) {
18379 	dns_keymgmt_t *mgmt = isc_mem_get(zmgr->mctx, sizeof(*mgmt));
18380 	uint32_t size;
18381 
18382 	*mgmt = (dns_keymgmt_t){
18383 		.bits = KEYMGMT_BITS_MIN,
18384 	};
18385 	isc_mem_attach(zmgr->mctx, &mgmt->mctx);
18386 	isc_rwlock_init(&mgmt->lock, 0, 0);
18387 
18388 	size = HASHSIZE(mgmt->bits);
18389 	mgmt->table = isc_mem_get(mgmt->mctx, sizeof(*mgmt->table) * size);
18390 	memset(mgmt->table, 0, size * sizeof(mgmt->table[0]));
18391 
18392 	atomic_init(&mgmt->count, 0);
18393 	mgmt->magic = KEYMGMT_MAGIC;
18394 
18395 	zmgr->keymgmt = mgmt;
18396 }
18397 
18398 static void
zonemgr_keymgmt_destroy(dns_zonemgr_t * zmgr)18399 zonemgr_keymgmt_destroy(dns_zonemgr_t *zmgr) {
18400 	dns_keymgmt_t *mgmt = zmgr->keymgmt;
18401 	dns_keyfileio_t *curr, *next;
18402 	uint32_t size;
18403 
18404 	REQUIRE(DNS_KEYMGMT_VALID(mgmt));
18405 
18406 	RWLOCK(&mgmt->lock, isc_rwlocktype_write);
18407 	size = HASHSIZE(mgmt->bits);
18408 	for (unsigned int i = 0;
18409 	     atomic_load_relaxed(&mgmt->count) > 0 && i < size; i++) {
18410 		for (curr = mgmt->table[i]; curr != NULL; curr = next) {
18411 			next = curr->next;
18412 			isc_mutex_destroy(&curr->lock);
18413 			isc_mem_put(mgmt->mctx, curr, sizeof(*curr));
18414 			atomic_fetch_sub_relaxed(&mgmt->count, 1);
18415 		}
18416 		mgmt->table[i] = NULL;
18417 	}
18418 	RWUNLOCK(&mgmt->lock, isc_rwlocktype_write);
18419 
18420 	mgmt->magic = 0;
18421 	isc_rwlock_destroy(&mgmt->lock);
18422 	isc_mem_put(mgmt->mctx, mgmt->table, size * sizeof(mgmt->table[0]));
18423 	isc_mem_putanddetach(&mgmt->mctx, mgmt, sizeof(dns_keymgmt_t));
18424 }
18425 
18426 static void
zonemgr_keymgmt_resize(dns_zonemgr_t * zmgr)18427 zonemgr_keymgmt_resize(dns_zonemgr_t *zmgr) {
18428 	dns_keyfileio_t **newtable;
18429 	dns_keymgmt_t *mgmt = zmgr->keymgmt;
18430 	uint32_t bits, newbits, count, size, newsize;
18431 	bool grow;
18432 
18433 	REQUIRE(DNS_KEYMGMT_VALID(mgmt));
18434 
18435 	RWLOCK(&mgmt->lock, isc_rwlocktype_read);
18436 	count = atomic_load_relaxed(&mgmt->count);
18437 	bits = mgmt->bits;
18438 	RWUNLOCK(&mgmt->lock, isc_rwlocktype_read);
18439 
18440 	size = HASHSIZE(bits);
18441 	INSIST(size > 0);
18442 
18443 	if (count >= (size * KEYMGMT_OVERCOMMIT)) {
18444 		grow = true;
18445 	} else if (count < (size / 2)) {
18446 		grow = false;
18447 	} else {
18448 		/* No need to resize. */
18449 		return;
18450 	}
18451 
18452 	if (grow) {
18453 		newbits = hash_bits_grow(bits, count);
18454 	} else {
18455 		newbits = hash_bits_shrink(bits, count);
18456 	}
18457 
18458 	if (newbits == bits) {
18459 		/*
18460 		 * Bit values may stay the same if maximum or minimum is
18461 		 * reached.
18462 		 */
18463 		return;
18464 	}
18465 
18466 	newsize = HASHSIZE(newbits);
18467 	INSIST(newsize > 0);
18468 
18469 	RWLOCK(&mgmt->lock, isc_rwlocktype_write);
18470 
18471 	newtable = isc_mem_get(mgmt->mctx, sizeof(dns_keyfileio_t *) * newsize);
18472 	memset(newtable, 0, sizeof(dns_keyfileio_t *) * newsize);
18473 
18474 	for (unsigned int i = 0; i < size; i++) {
18475 		dns_keyfileio_t *kfio, *next;
18476 		for (kfio = mgmt->table[i]; kfio != NULL; kfio = next) {
18477 			uint32_t hash = hash_index(kfio->hashval, newbits);
18478 			next = kfio->next;
18479 			kfio->next = newtable[hash];
18480 			newtable[hash] = kfio;
18481 		}
18482 		mgmt->table[i] = NULL;
18483 	}
18484 
18485 	isc_mem_put(mgmt->mctx, mgmt->table, sizeof(*mgmt->table) * size);
18486 	mgmt->bits = newbits;
18487 	mgmt->table = newtable;
18488 
18489 	RWUNLOCK(&mgmt->lock, isc_rwlocktype_write);
18490 }
18491 
18492 static void
zonemgr_keymgmt_add(dns_zonemgr_t * zmgr,dns_zone_t * zone,dns_keyfileio_t ** added)18493 zonemgr_keymgmt_add(dns_zonemgr_t *zmgr, dns_zone_t *zone,
18494 		    dns_keyfileio_t **added) {
18495 	dns_keymgmt_t *mgmt = zmgr->keymgmt;
18496 	uint32_t hashval, hash;
18497 	dns_keyfileio_t *kfio, *next;
18498 
18499 	REQUIRE(DNS_KEYMGMT_VALID(mgmt));
18500 
18501 	RWLOCK(&mgmt->lock, isc_rwlocktype_write);
18502 
18503 	hashval = dns_name_hash(&zone->origin, false);
18504 	hash = hash_index(hashval, mgmt->bits);
18505 
18506 	for (kfio = mgmt->table[hash]; kfio != NULL; kfio = next) {
18507 		next = kfio->next;
18508 		if (dns_name_equal(kfio->name, &zone->origin)) {
18509 			/* Already in table, increment the counter. */
18510 			atomic_fetch_add_relaxed(&kfio->count, 1);
18511 			break;
18512 		}
18513 	}
18514 
18515 	if (kfio == NULL) {
18516 		/* No entry found, add it. */
18517 		kfio = isc_mem_get(mgmt->mctx, sizeof(*kfio));
18518 		*kfio = (dns_keyfileio_t){ .hashval = hashval,
18519 					   .next = mgmt->table[hash] };
18520 
18521 		kfio->name = dns_fixedname_initname(&kfio->fname);
18522 		dns_name_copynf(&zone->origin, kfio->name);
18523 
18524 		atomic_fetch_add_relaxed(&kfio->count, 1);
18525 		isc_mutex_init(&kfio->lock);
18526 
18527 		mgmt->table[hash] = kfio;
18528 		if (added != NULL) {
18529 			*added = kfio;
18530 		}
18531 
18532 		atomic_fetch_add_relaxed(&mgmt->count, 1);
18533 	}
18534 
18535 	RWUNLOCK(&mgmt->lock, isc_rwlocktype_write);
18536 
18537 	/*
18538 	 * Call resize, that function will also check if resize is necessary.
18539 	 */
18540 	zonemgr_keymgmt_resize(zmgr);
18541 }
18542 
18543 static void
zonemgr_keymgmt_delete(dns_zonemgr_t * zmgr,dns_zone_t * zone)18544 zonemgr_keymgmt_delete(dns_zonemgr_t *zmgr, dns_zone_t *zone) {
18545 	dns_keymgmt_t *mgmt = zmgr->keymgmt;
18546 	uint32_t hashval, hash;
18547 	dns_keyfileio_t *kfio, *prev, *next;
18548 
18549 	REQUIRE(DNS_KEYMGMT_VALID(mgmt));
18550 
18551 	RWLOCK(&mgmt->lock, isc_rwlocktype_write);
18552 
18553 	hashval = dns_name_hash(&zone->origin, false);
18554 	hash = hash_index(hashval, mgmt->bits);
18555 
18556 	prev = NULL;
18557 	for (kfio = mgmt->table[hash]; kfio != NULL; kfio = next) {
18558 		next = kfio->next;
18559 		if (dns_name_equal(kfio->name, &zone->origin)) {
18560 			unsigned int count;
18561 
18562 			count = atomic_fetch_sub_relaxed(&kfio->count, 1);
18563 			if (count > 0) {
18564 				/* Keep the entry. */
18565 				break;
18566 			}
18567 
18568 			/* Delete the entry. */
18569 			if (prev == NULL) {
18570 				mgmt->table[hash] = kfio->next;
18571 			} else {
18572 				prev->next = kfio->next;
18573 			}
18574 
18575 			isc_mutex_destroy(&kfio->lock);
18576 			isc_mem_put(mgmt->mctx, kfio, sizeof(*kfio));
18577 
18578 			atomic_fetch_sub_relaxed(&mgmt->count, 1);
18579 
18580 			break;
18581 		}
18582 
18583 		prev = kfio;
18584 	}
18585 
18586 	RWUNLOCK(&mgmt->lock, isc_rwlocktype_write);
18587 
18588 	/*
18589 	 * Call resize, that function will also check if resize is necessary.
18590 	 */
18591 	zonemgr_keymgmt_resize(zmgr);
18592 }
18593 
18594 static void
zonemgr_keymgmt_find(dns_zonemgr_t * zmgr,dns_zone_t * zone,dns_keyfileio_t ** match)18595 zonemgr_keymgmt_find(dns_zonemgr_t *zmgr, dns_zone_t *zone,
18596 		     dns_keyfileio_t **match) {
18597 	dns_keymgmt_t *mgmt = zmgr->keymgmt;
18598 	uint32_t hashval, hash;
18599 	dns_keyfileio_t *kfio, *next;
18600 
18601 	REQUIRE(DNS_KEYMGMT_VALID(mgmt));
18602 	REQUIRE(match != NULL && *match == NULL);
18603 
18604 	RWLOCK(&mgmt->lock, isc_rwlocktype_read);
18605 
18606 	if (atomic_load_relaxed(&mgmt->count) == 0) {
18607 		RWUNLOCK(&mgmt->lock, isc_rwlocktype_read);
18608 		return;
18609 	}
18610 
18611 	hashval = dns_name_hash(&zone->origin, false);
18612 	hash = hash_index(hashval, mgmt->bits);
18613 
18614 	for (kfio = mgmt->table[hash]; kfio != NULL; kfio = next) {
18615 		next = kfio->next;
18616 
18617 		if (dns_name_equal(kfio->name, &zone->origin)) {
18618 			*match = kfio;
18619 			break;
18620 		}
18621 	}
18622 
18623 	RWUNLOCK(&mgmt->lock, isc_rwlocktype_read);
18624 }
18625 
18626 isc_result_t
dns_zonemgr_create(isc_mem_t * mctx,isc_taskmgr_t * taskmgr,isc_timermgr_t * timermgr,isc_socketmgr_t * socketmgr,dns_zonemgr_t ** zmgrp)18627 dns_zonemgr_create(isc_mem_t *mctx, isc_taskmgr_t *taskmgr,
18628 		   isc_timermgr_t *timermgr, isc_socketmgr_t *socketmgr,
18629 		   dns_zonemgr_t **zmgrp) {
18630 	dns_zonemgr_t *zmgr;
18631 	isc_result_t result;
18632 
18633 	zmgr = isc_mem_get(mctx, sizeof(*zmgr));
18634 	zmgr->mctx = NULL;
18635 	isc_refcount_init(&zmgr->refs, 1);
18636 	isc_mem_attach(mctx, &zmgr->mctx);
18637 	zmgr->taskmgr = taskmgr;
18638 	zmgr->timermgr = timermgr;
18639 	zmgr->socketmgr = socketmgr;
18640 	zmgr->zonetasks = NULL;
18641 	zmgr->loadtasks = NULL;
18642 	zmgr->mctxpool = NULL;
18643 	zmgr->task = NULL;
18644 	zmgr->checkdsrl = NULL;
18645 	zmgr->notifyrl = NULL;
18646 	zmgr->refreshrl = NULL;
18647 	zmgr->startupnotifyrl = NULL;
18648 	zmgr->startuprefreshrl = NULL;
18649 	ISC_LIST_INIT(zmgr->zones);
18650 	ISC_LIST_INIT(zmgr->waiting_for_xfrin);
18651 	ISC_LIST_INIT(zmgr->xfrin_in_progress);
18652 	memset(zmgr->unreachable, 0, sizeof(zmgr->unreachable));
18653 	for (size_t i = 0; i < UNREACH_CACHE_SIZE; i++) {
18654 		atomic_init(&zmgr->unreachable[i].expire, 0);
18655 	}
18656 	isc_rwlock_init(&zmgr->rwlock, 0, 0);
18657 
18658 	zmgr->transfersin = 10;
18659 	zmgr->transfersperns = 2;
18660 
18661 	/* Unreachable lock. */
18662 	isc_rwlock_init(&zmgr->urlock, 0, 0);
18663 
18664 	/* Create a single task for queueing of SOA queries. */
18665 	result = isc_task_create(taskmgr, 1, &zmgr->task);
18666 	if (result != ISC_R_SUCCESS) {
18667 		goto free_urlock;
18668 	}
18669 
18670 	isc_task_setname(zmgr->task, "zmgr", zmgr);
18671 	result = isc_ratelimiter_create(mctx, timermgr, zmgr->task,
18672 					&zmgr->checkdsrl);
18673 	if (result != ISC_R_SUCCESS) {
18674 		goto free_task;
18675 	}
18676 
18677 	result = isc_ratelimiter_create(mctx, timermgr, zmgr->task,
18678 					&zmgr->notifyrl);
18679 	if (result != ISC_R_SUCCESS) {
18680 		goto free_checkdsrl;
18681 	}
18682 
18683 	result = isc_ratelimiter_create(mctx, timermgr, zmgr->task,
18684 					&zmgr->refreshrl);
18685 	if (result != ISC_R_SUCCESS) {
18686 		goto free_notifyrl;
18687 	}
18688 
18689 	result = isc_ratelimiter_create(mctx, timermgr, zmgr->task,
18690 					&zmgr->startupnotifyrl);
18691 	if (result != ISC_R_SUCCESS) {
18692 		goto free_refreshrl;
18693 	}
18694 
18695 	result = isc_ratelimiter_create(mctx, timermgr, zmgr->task,
18696 					&zmgr->startuprefreshrl);
18697 	if (result != ISC_R_SUCCESS) {
18698 		goto free_startupnotifyrl;
18699 	}
18700 
18701 	/* Key file I/O locks. */
18702 	zonemgr_keymgmt_init(zmgr);
18703 
18704 	/* Default to 20 refresh queries / notifies / checkds per second. */
18705 	setrl(zmgr->checkdsrl, &zmgr->checkdsrate, 20);
18706 	setrl(zmgr->notifyrl, &zmgr->notifyrate, 20);
18707 	setrl(zmgr->startupnotifyrl, &zmgr->startupnotifyrate, 20);
18708 	setrl(zmgr->refreshrl, &zmgr->serialqueryrate, 20);
18709 	setrl(zmgr->startuprefreshrl, &zmgr->startupserialqueryrate, 20);
18710 	isc_ratelimiter_setpushpop(zmgr->startupnotifyrl, true);
18711 	isc_ratelimiter_setpushpop(zmgr->startuprefreshrl, true);
18712 
18713 	zmgr->iolimit = 1;
18714 	zmgr->ioactive = 0;
18715 	ISC_LIST_INIT(zmgr->high);
18716 	ISC_LIST_INIT(zmgr->low);
18717 
18718 	isc_mutex_init(&zmgr->iolock);
18719 
18720 	zmgr->magic = ZONEMGR_MAGIC;
18721 
18722 	*zmgrp = zmgr;
18723 	return (ISC_R_SUCCESS);
18724 
18725 #if 0
18726  free_iolock:
18727 	isc_mutex_destroy(&zmgr->iolock);
18728 #endif /* if 0 */
18729 free_startupnotifyrl:
18730 	isc_ratelimiter_detach(&zmgr->startupnotifyrl);
18731 free_refreshrl:
18732 	isc_ratelimiter_detach(&zmgr->refreshrl);
18733 free_notifyrl:
18734 	isc_ratelimiter_detach(&zmgr->notifyrl);
18735 free_checkdsrl:
18736 	isc_ratelimiter_detach(&zmgr->checkdsrl);
18737 free_task:
18738 	isc_task_detach(&zmgr->task);
18739 free_urlock:
18740 	isc_rwlock_destroy(&zmgr->urlock);
18741 	isc_rwlock_destroy(&zmgr->rwlock);
18742 	isc_mem_put(zmgr->mctx, zmgr, sizeof(*zmgr));
18743 	isc_mem_detach(&mctx);
18744 	return (result);
18745 }
18746 
18747 isc_result_t
dns_zonemgr_createzone(dns_zonemgr_t * zmgr,dns_zone_t ** zonep)18748 dns_zonemgr_createzone(dns_zonemgr_t *zmgr, dns_zone_t **zonep) {
18749 	isc_result_t result;
18750 	isc_mem_t *mctx = NULL;
18751 	dns_zone_t *zone = NULL;
18752 	void *item;
18753 
18754 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
18755 	REQUIRE(zonep != NULL && *zonep == NULL);
18756 
18757 	if (zmgr->mctxpool == NULL) {
18758 		return (ISC_R_FAILURE);
18759 	}
18760 
18761 	item = isc_pool_get(zmgr->mctxpool);
18762 	if (item == NULL) {
18763 		return (ISC_R_FAILURE);
18764 	}
18765 
18766 	isc_mem_attach((isc_mem_t *)item, &mctx);
18767 	result = dns_zone_create(&zone, mctx);
18768 	isc_mem_detach(&mctx);
18769 
18770 	if (result == ISC_R_SUCCESS) {
18771 		*zonep = zone;
18772 	}
18773 
18774 	return (result);
18775 }
18776 
18777 isc_result_t
dns_zonemgr_managezone(dns_zonemgr_t * zmgr,dns_zone_t * zone)18778 dns_zonemgr_managezone(dns_zonemgr_t *zmgr, dns_zone_t *zone) {
18779 	isc_result_t result;
18780 
18781 	REQUIRE(DNS_ZONE_VALID(zone));
18782 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
18783 
18784 	if (zmgr->zonetasks == NULL) {
18785 		return (ISC_R_FAILURE);
18786 	}
18787 
18788 	RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
18789 	LOCK_ZONE(zone);
18790 	REQUIRE(zone->task == NULL);
18791 	REQUIRE(zone->timer == NULL);
18792 	REQUIRE(zone->zmgr == NULL);
18793 
18794 	isc_taskpool_gettask(zmgr->zonetasks, &zone->task);
18795 	isc_taskpool_gettask(zmgr->loadtasks, &zone->loadtask);
18796 
18797 	/*
18798 	 * Set the task name.  The tag will arbitrarily point to one
18799 	 * of the zones sharing the task (in practice, the one
18800 	 * to be managed last).
18801 	 */
18802 	isc_task_setname(zone->task, "zone", zone);
18803 	isc_task_setname(zone->loadtask, "loadzone", zone);
18804 
18805 	result = isc_timer_create(zmgr->timermgr, isc_timertype_inactive, NULL,
18806 				  NULL, zone->task, zone_timer, zone,
18807 				  &zone->timer);
18808 
18809 	if (result != ISC_R_SUCCESS) {
18810 		goto cleanup_tasks;
18811 	}
18812 
18813 	/*
18814 	 * The timer "holds" a iref.
18815 	 */
18816 	isc_refcount_increment0(&zone->irefs);
18817 
18818 	zonemgr_keymgmt_add(zmgr, zone, NULL);
18819 
18820 	ISC_LIST_APPEND(zmgr->zones, zone, link);
18821 	zone->zmgr = zmgr;
18822 	isc_refcount_increment(&zmgr->refs);
18823 
18824 	goto unlock;
18825 
18826 cleanup_tasks:
18827 	isc_task_detach(&zone->loadtask);
18828 	isc_task_detach(&zone->task);
18829 
18830 unlock:
18831 	UNLOCK_ZONE(zone);
18832 	RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
18833 	return (result);
18834 }
18835 
18836 void
dns_zonemgr_releasezone(dns_zonemgr_t * zmgr,dns_zone_t * zone)18837 dns_zonemgr_releasezone(dns_zonemgr_t *zmgr, dns_zone_t *zone) {
18838 	bool free_now = false;
18839 
18840 	REQUIRE(DNS_ZONE_VALID(zone));
18841 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
18842 	REQUIRE(zone->zmgr == zmgr);
18843 
18844 	RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
18845 	LOCK_ZONE(zone);
18846 
18847 	ISC_LIST_UNLINK(zmgr->zones, zone, link);
18848 
18849 	zonemgr_keymgmt_delete(zmgr, zone);
18850 
18851 	zone->zmgr = NULL;
18852 
18853 	if (isc_refcount_decrement(&zmgr->refs) == 1) {
18854 		free_now = true;
18855 	}
18856 
18857 	UNLOCK_ZONE(zone);
18858 	RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
18859 
18860 	if (free_now) {
18861 		zonemgr_free(zmgr);
18862 	}
18863 	ENSURE(zone->zmgr == NULL);
18864 }
18865 
18866 void
dns_zonemgr_attach(dns_zonemgr_t * source,dns_zonemgr_t ** target)18867 dns_zonemgr_attach(dns_zonemgr_t *source, dns_zonemgr_t **target) {
18868 	REQUIRE(DNS_ZONEMGR_VALID(source));
18869 	REQUIRE(target != NULL && *target == NULL);
18870 
18871 	isc_refcount_increment(&source->refs);
18872 
18873 	*target = source;
18874 }
18875 
18876 void
dns_zonemgr_detach(dns_zonemgr_t ** zmgrp)18877 dns_zonemgr_detach(dns_zonemgr_t **zmgrp) {
18878 	dns_zonemgr_t *zmgr;
18879 
18880 	REQUIRE(zmgrp != NULL);
18881 	zmgr = *zmgrp;
18882 	*zmgrp = NULL;
18883 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
18884 
18885 	if (isc_refcount_decrement(&zmgr->refs) == 1) {
18886 		zonemgr_free(zmgr);
18887 	}
18888 }
18889 
18890 isc_result_t
dns_zonemgr_forcemaint(dns_zonemgr_t * zmgr)18891 dns_zonemgr_forcemaint(dns_zonemgr_t *zmgr) {
18892 	dns_zone_t *p;
18893 
18894 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
18895 
18896 	RWLOCK(&zmgr->rwlock, isc_rwlocktype_read);
18897 	for (p = ISC_LIST_HEAD(zmgr->zones); p != NULL;
18898 	     p = ISC_LIST_NEXT(p, link)) {
18899 		dns_zone_maintenance(p);
18900 	}
18901 	RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_read);
18902 
18903 	/*
18904 	 * Recent configuration changes may have increased the
18905 	 * amount of available transfers quota.  Make sure any
18906 	 * transfers currently blocked on quota get started if
18907 	 * possible.
18908 	 */
18909 	RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
18910 	zmgr_resume_xfrs(zmgr, true);
18911 	RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
18912 	return (ISC_R_SUCCESS);
18913 }
18914 
18915 void
dns_zonemgr_resumexfrs(dns_zonemgr_t * zmgr)18916 dns_zonemgr_resumexfrs(dns_zonemgr_t *zmgr) {
18917 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
18918 
18919 	RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
18920 	zmgr_resume_xfrs(zmgr, true);
18921 	RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
18922 }
18923 
18924 void
dns_zonemgr_shutdown(dns_zonemgr_t * zmgr)18925 dns_zonemgr_shutdown(dns_zonemgr_t *zmgr) {
18926 	dns_zone_t *zone;
18927 
18928 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
18929 
18930 	isc_ratelimiter_shutdown(zmgr->checkdsrl);
18931 	isc_ratelimiter_shutdown(zmgr->notifyrl);
18932 	isc_ratelimiter_shutdown(zmgr->refreshrl);
18933 	isc_ratelimiter_shutdown(zmgr->startupnotifyrl);
18934 	isc_ratelimiter_shutdown(zmgr->startuprefreshrl);
18935 
18936 	if (zmgr->task != NULL) {
18937 		isc_task_destroy(&zmgr->task);
18938 	}
18939 	if (zmgr->zonetasks != NULL) {
18940 		isc_taskpool_destroy(&zmgr->zonetasks);
18941 	}
18942 	if (zmgr->loadtasks != NULL) {
18943 		isc_taskpool_destroy(&zmgr->loadtasks);
18944 	}
18945 	if (zmgr->mctxpool != NULL) {
18946 		isc_pool_destroy(&zmgr->mctxpool);
18947 	}
18948 
18949 	RWLOCK(&zmgr->rwlock, isc_rwlocktype_read);
18950 	for (zone = ISC_LIST_HEAD(zmgr->zones); zone != NULL;
18951 	     zone = ISC_LIST_NEXT(zone, link))
18952 	{
18953 		LOCK_ZONE(zone);
18954 		forward_cancel(zone);
18955 		UNLOCK_ZONE(zone);
18956 	}
18957 	RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_read);
18958 }
18959 
18960 static isc_result_t
mctxinit(void ** target,void * arg)18961 mctxinit(void **target, void *arg) {
18962 	isc_mem_t *mctx = NULL;
18963 
18964 	UNUSED(arg);
18965 
18966 	REQUIRE(target != NULL && *target == NULL);
18967 
18968 	isc_mem_create(&mctx);
18969 	isc_mem_setname(mctx, "zonemgr-pool", NULL);
18970 
18971 	*target = mctx;
18972 	return (ISC_R_SUCCESS);
18973 }
18974 
18975 static void
mctxfree(void ** target)18976 mctxfree(void **target) {
18977 	isc_mem_t *mctx = *(isc_mem_t **)target;
18978 	isc_mem_detach(&mctx);
18979 	*target = NULL;
18980 }
18981 
18982 #define ZONES_PER_TASK 100
18983 #define ZONES_PER_MCTX 1000
18984 
18985 isc_result_t
dns_zonemgr_setsize(dns_zonemgr_t * zmgr,int num_zones)18986 dns_zonemgr_setsize(dns_zonemgr_t *zmgr, int num_zones) {
18987 	isc_result_t result;
18988 	int ntasks = num_zones / ZONES_PER_TASK;
18989 	int nmctx = num_zones / ZONES_PER_MCTX;
18990 	isc_taskpool_t *pool = NULL;
18991 	isc_pool_t *mctxpool = NULL;
18992 
18993 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
18994 
18995 	/*
18996 	 * For anything fewer than 1000 zones we use 10 tasks in
18997 	 * the task pools.  More than that, and we'll scale at one
18998 	 * task per 100 zones.  Similarly, for anything smaller than
18999 	 * 2000 zones we use 2 memory contexts, then scale at 1:1000.
19000 	 */
19001 	if (ntasks < 10) {
19002 		ntasks = 10;
19003 	}
19004 	if (nmctx < 2) {
19005 		nmctx = 2;
19006 	}
19007 
19008 	/* Create or resize the zone task pools. */
19009 	if (zmgr->zonetasks == NULL) {
19010 		result = isc_taskpool_create(zmgr->taskmgr, zmgr->mctx, ntasks,
19011 					     2, false, &pool);
19012 	} else {
19013 		result = isc_taskpool_expand(&zmgr->zonetasks, ntasks, false,
19014 					     &pool);
19015 	}
19016 
19017 	if (result == ISC_R_SUCCESS) {
19018 		zmgr->zonetasks = pool;
19019 	}
19020 
19021 	pool = NULL;
19022 	if (zmgr->loadtasks == NULL) {
19023 		result = isc_taskpool_create(zmgr->taskmgr, zmgr->mctx, ntasks,
19024 					     2, true, &pool);
19025 	} else {
19026 		result = isc_taskpool_expand(&zmgr->loadtasks, ntasks, true,
19027 					     &pool);
19028 	}
19029 
19030 	if (result == ISC_R_SUCCESS) {
19031 		zmgr->loadtasks = pool;
19032 	}
19033 
19034 	/* Create or resize the zone memory context pool. */
19035 	if (zmgr->mctxpool == NULL) {
19036 		result = isc_pool_create(zmgr->mctx, nmctx, mctxfree, mctxinit,
19037 					 NULL, &mctxpool);
19038 	} else {
19039 		result = isc_pool_expand(&zmgr->mctxpool, nmctx, &mctxpool);
19040 	}
19041 
19042 	if (result == ISC_R_SUCCESS) {
19043 		zmgr->mctxpool = mctxpool;
19044 	}
19045 
19046 	return (result);
19047 }
19048 
19049 static void
zonemgr_free(dns_zonemgr_t * zmgr)19050 zonemgr_free(dns_zonemgr_t *zmgr) {
19051 	isc_mem_t *mctx;
19052 
19053 	INSIST(ISC_LIST_EMPTY(zmgr->zones));
19054 
19055 	zmgr->magic = 0;
19056 
19057 	isc_refcount_destroy(&zmgr->refs);
19058 	isc_mutex_destroy(&zmgr->iolock);
19059 	isc_ratelimiter_detach(&zmgr->checkdsrl);
19060 	isc_ratelimiter_detach(&zmgr->notifyrl);
19061 	isc_ratelimiter_detach(&zmgr->refreshrl);
19062 	isc_ratelimiter_detach(&zmgr->startupnotifyrl);
19063 	isc_ratelimiter_detach(&zmgr->startuprefreshrl);
19064 
19065 	isc_rwlock_destroy(&zmgr->urlock);
19066 	isc_rwlock_destroy(&zmgr->rwlock);
19067 
19068 	zonemgr_keymgmt_destroy(zmgr);
19069 
19070 	mctx = zmgr->mctx;
19071 	isc_mem_put(zmgr->mctx, zmgr, sizeof(*zmgr));
19072 	isc_mem_detach(&mctx);
19073 }
19074 
19075 void
dns_zonemgr_settransfersin(dns_zonemgr_t * zmgr,uint32_t value)19076 dns_zonemgr_settransfersin(dns_zonemgr_t *zmgr, uint32_t value) {
19077 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19078 
19079 	zmgr->transfersin = value;
19080 }
19081 
19082 uint32_t
dns_zonemgr_getttransfersin(dns_zonemgr_t * zmgr)19083 dns_zonemgr_getttransfersin(dns_zonemgr_t *zmgr) {
19084 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19085 
19086 	return (zmgr->transfersin);
19087 }
19088 
19089 void
dns_zonemgr_settransfersperns(dns_zonemgr_t * zmgr,uint32_t value)19090 dns_zonemgr_settransfersperns(dns_zonemgr_t *zmgr, uint32_t value) {
19091 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19092 
19093 	zmgr->transfersperns = value;
19094 }
19095 
19096 uint32_t
dns_zonemgr_getttransfersperns(dns_zonemgr_t * zmgr)19097 dns_zonemgr_getttransfersperns(dns_zonemgr_t *zmgr) {
19098 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19099 
19100 	return (zmgr->transfersperns);
19101 }
19102 
19103 isc_taskmgr_t *
dns_zonemgr_gettaskmgr(dns_zonemgr_t * zmgr)19104 dns_zonemgr_gettaskmgr(dns_zonemgr_t *zmgr) {
19105 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19106 
19107 	return (zmgr->taskmgr);
19108 }
19109 
19110 /*
19111  * Try to start a new incoming zone transfer to fill a quota
19112  * slot that was just vacated.
19113  *
19114  * Requires:
19115  *	The zone manager is locked by the caller.
19116  */
19117 static void
zmgr_resume_xfrs(dns_zonemgr_t * zmgr,bool multi)19118 zmgr_resume_xfrs(dns_zonemgr_t *zmgr, bool multi) {
19119 	dns_zone_t *zone;
19120 	dns_zone_t *next;
19121 
19122 	for (zone = ISC_LIST_HEAD(zmgr->waiting_for_xfrin); zone != NULL;
19123 	     zone = next) {
19124 		isc_result_t result;
19125 		next = ISC_LIST_NEXT(zone, statelink);
19126 		result = zmgr_start_xfrin_ifquota(zmgr, zone);
19127 		if (result == ISC_R_SUCCESS) {
19128 			if (multi) {
19129 				continue;
19130 			}
19131 			/*
19132 			 * We successfully filled the slot.  We're done.
19133 			 */
19134 			break;
19135 		} else if (result == ISC_R_QUOTA) {
19136 			/*
19137 			 * Not enough quota.  This is probably the per-server
19138 			 * quota, because we usually get called when a unit of
19139 			 * global quota has just been freed.  Try the next
19140 			 * zone, it may succeed if it uses another master.
19141 			 */
19142 			continue;
19143 		} else {
19144 			dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN,
19145 				      ISC_LOG_DEBUG(1),
19146 				      "starting zone transfer: %s",
19147 				      isc_result_totext(result));
19148 			break;
19149 		}
19150 	}
19151 }
19152 
19153 /*
19154  * Try to start an incoming zone transfer for 'zone', quota permitting.
19155  *
19156  * Requires:
19157  *	The zone manager is locked by the caller.
19158  *
19159  * Returns:
19160  *	ISC_R_SUCCESS	There was enough quota and we attempted to
19161  *			start a transfer.  zone_xfrdone() has been or will
19162  *			be called.
19163  *	ISC_R_QUOTA	Not enough quota.
19164  *	Others		Failure.
19165  */
19166 static isc_result_t
zmgr_start_xfrin_ifquota(dns_zonemgr_t * zmgr,dns_zone_t * zone)19167 zmgr_start_xfrin_ifquota(dns_zonemgr_t *zmgr, dns_zone_t *zone) {
19168 	dns_peer_t *peer = NULL;
19169 	isc_netaddr_t masterip;
19170 	uint32_t nxfrsin, nxfrsperns;
19171 	dns_zone_t *x;
19172 	uint32_t maxtransfersin, maxtransfersperns;
19173 	isc_event_t *e;
19174 
19175 	/*
19176 	 * If we are exiting just pretend we got quota so the zone will
19177 	 * be cleaned up in the zone's task context.
19178 	 */
19179 	LOCK_ZONE(zone);
19180 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
19181 		UNLOCK_ZONE(zone);
19182 		goto gotquota;
19183 	}
19184 
19185 	/*
19186 	 * Find any configured information about the server we'd
19187 	 * like to transfer this zone from.
19188 	 */
19189 	isc_netaddr_fromsockaddr(&masterip, &zone->masteraddr);
19190 	(void)dns_peerlist_peerbyaddr(zone->view->peers, &masterip, &peer);
19191 	UNLOCK_ZONE(zone);
19192 
19193 	/*
19194 	 * Determine the total maximum number of simultaneous
19195 	 * transfers allowed, and the maximum for this specific
19196 	 * master.
19197 	 */
19198 	maxtransfersin = zmgr->transfersin;
19199 	maxtransfersperns = zmgr->transfersperns;
19200 	if (peer != NULL) {
19201 		(void)dns_peer_gettransfers(peer, &maxtransfersperns);
19202 	}
19203 
19204 	/*
19205 	 * Count the total number of transfers that are in progress,
19206 	 * and the number of transfers in progress from this master.
19207 	 * We linearly scan a list of all transfers; if this turns
19208 	 * out to be too slow, we could hash on the master address.
19209 	 */
19210 	nxfrsin = nxfrsperns = 0;
19211 	for (x = ISC_LIST_HEAD(zmgr->xfrin_in_progress); x != NULL;
19212 	     x = ISC_LIST_NEXT(x, statelink))
19213 	{
19214 		isc_netaddr_t xip;
19215 
19216 		LOCK_ZONE(x);
19217 		isc_netaddr_fromsockaddr(&xip, &x->masteraddr);
19218 		UNLOCK_ZONE(x);
19219 
19220 		nxfrsin++;
19221 		if (isc_netaddr_equal(&xip, &masterip)) {
19222 			nxfrsperns++;
19223 		}
19224 	}
19225 
19226 	/* Enforce quota. */
19227 	if (nxfrsin >= maxtransfersin) {
19228 		return (ISC_R_QUOTA);
19229 	}
19230 
19231 	if (nxfrsperns >= maxtransfersperns) {
19232 		return (ISC_R_QUOTA);
19233 	}
19234 
19235 gotquota:
19236 	/*
19237 	 * We have sufficient quota.  Move the zone to the "xfrin_in_progress"
19238 	 * list and send it an event to let it start the actual transfer in the
19239 	 * context of its own task.
19240 	 */
19241 	e = isc_event_allocate(zmgr->mctx, zmgr, DNS_EVENT_ZONESTARTXFRIN,
19242 			       got_transfer_quota, zone, sizeof(isc_event_t));
19243 
19244 	LOCK_ZONE(zone);
19245 	INSIST(zone->statelist == &zmgr->waiting_for_xfrin);
19246 	ISC_LIST_UNLINK(zmgr->waiting_for_xfrin, zone, statelink);
19247 	ISC_LIST_APPEND(zmgr->xfrin_in_progress, zone, statelink);
19248 	zone->statelist = &zmgr->xfrin_in_progress;
19249 	isc_task_send(zone->task, &e);
19250 	dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO,
19251 		      "Transfer started.");
19252 	UNLOCK_ZONE(zone);
19253 
19254 	return (ISC_R_SUCCESS);
19255 }
19256 
19257 void
dns_zonemgr_setiolimit(dns_zonemgr_t * zmgr,uint32_t iolimit)19258 dns_zonemgr_setiolimit(dns_zonemgr_t *zmgr, uint32_t iolimit) {
19259 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19260 	REQUIRE(iolimit > 0);
19261 
19262 	zmgr->iolimit = iolimit;
19263 }
19264 
19265 uint32_t
dns_zonemgr_getiolimit(dns_zonemgr_t * zmgr)19266 dns_zonemgr_getiolimit(dns_zonemgr_t *zmgr) {
19267 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19268 
19269 	return (zmgr->iolimit);
19270 }
19271 
19272 /*
19273  * Get permission to request a file handle from the OS.
19274  * An event will be sent to action when one is available.
19275  * There are two queues available (high and low), the high
19276  * queue will be serviced before the low one.
19277  *
19278  * zonemgr_putio() must be called after the event is delivered to
19279  * 'action'.
19280  */
19281 
19282 static isc_result_t
zonemgr_getio(dns_zonemgr_t * zmgr,bool high,isc_task_t * task,isc_taskaction_t action,void * arg,dns_io_t ** iop)19283 zonemgr_getio(dns_zonemgr_t *zmgr, bool high, isc_task_t *task,
19284 	      isc_taskaction_t action, void *arg, dns_io_t **iop) {
19285 	dns_io_t *io;
19286 	bool queue;
19287 
19288 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19289 	REQUIRE(iop != NULL && *iop == NULL);
19290 
19291 	io = isc_mem_get(zmgr->mctx, sizeof(*io));
19292 
19293 	io->event = isc_event_allocate(zmgr->mctx, task, DNS_EVENT_IOREADY,
19294 				       action, arg, sizeof(*io->event));
19295 
19296 	io->zmgr = zmgr;
19297 	io->high = high;
19298 	io->task = NULL;
19299 	isc_task_attach(task, &io->task);
19300 	ISC_LINK_INIT(io, link);
19301 	io->magic = IO_MAGIC;
19302 
19303 	LOCK(&zmgr->iolock);
19304 	zmgr->ioactive++;
19305 	queue = (zmgr->ioactive > zmgr->iolimit);
19306 	if (queue) {
19307 		if (io->high) {
19308 			ISC_LIST_APPEND(zmgr->high, io, link);
19309 		} else {
19310 			ISC_LIST_APPEND(zmgr->low, io, link);
19311 		}
19312 	}
19313 	UNLOCK(&zmgr->iolock);
19314 	*iop = io;
19315 
19316 	if (!queue) {
19317 		isc_task_send(io->task, &io->event);
19318 	}
19319 	return (ISC_R_SUCCESS);
19320 }
19321 
19322 static void
zonemgr_putio(dns_io_t ** iop)19323 zonemgr_putio(dns_io_t **iop) {
19324 	dns_io_t *io;
19325 	dns_io_t *next;
19326 	dns_zonemgr_t *zmgr;
19327 
19328 	REQUIRE(iop != NULL);
19329 	io = *iop;
19330 	*iop = NULL;
19331 	REQUIRE(DNS_IO_VALID(io));
19332 
19333 	INSIST(!ISC_LINK_LINKED(io, link));
19334 	INSIST(io->event == NULL);
19335 
19336 	zmgr = io->zmgr;
19337 	isc_task_detach(&io->task);
19338 	io->magic = 0;
19339 	isc_mem_put(zmgr->mctx, io, sizeof(*io));
19340 
19341 	LOCK(&zmgr->iolock);
19342 	INSIST(zmgr->ioactive > 0);
19343 	zmgr->ioactive--;
19344 	next = HEAD(zmgr->high);
19345 	if (next == NULL) {
19346 		next = HEAD(zmgr->low);
19347 	}
19348 	if (next != NULL) {
19349 		if (next->high) {
19350 			ISC_LIST_UNLINK(zmgr->high, next, link);
19351 		} else {
19352 			ISC_LIST_UNLINK(zmgr->low, next, link);
19353 		}
19354 		INSIST(next->event != NULL);
19355 	}
19356 	UNLOCK(&zmgr->iolock);
19357 	if (next != NULL) {
19358 		isc_task_send(next->task, &next->event);
19359 	}
19360 }
19361 
19362 static void
zonemgr_cancelio(dns_io_t * io)19363 zonemgr_cancelio(dns_io_t *io) {
19364 	bool send_event = false;
19365 
19366 	REQUIRE(DNS_IO_VALID(io));
19367 
19368 	/*
19369 	 * If we are queued to be run then dequeue.
19370 	 */
19371 	LOCK(&io->zmgr->iolock);
19372 	if (ISC_LINK_LINKED(io, link)) {
19373 		if (io->high) {
19374 			ISC_LIST_UNLINK(io->zmgr->high, io, link);
19375 		} else {
19376 			ISC_LIST_UNLINK(io->zmgr->low, io, link);
19377 		}
19378 
19379 		send_event = true;
19380 		INSIST(io->event != NULL);
19381 	}
19382 	UNLOCK(&io->zmgr->iolock);
19383 	if (send_event) {
19384 		io->event->ev_attributes |= ISC_EVENTATTR_CANCELED;
19385 		isc_task_send(io->task, &io->event);
19386 	}
19387 }
19388 
19389 static void
zone_saveunique(dns_zone_t * zone,const char * path,const char * templat)19390 zone_saveunique(dns_zone_t *zone, const char *path, const char *templat) {
19391 	char *buf;
19392 	int buflen;
19393 	isc_result_t result;
19394 
19395 	buflen = strlen(path) + strlen(templat) + 2;
19396 
19397 	buf = isc_mem_get(zone->mctx, buflen);
19398 
19399 	result = isc_file_template(path, templat, buf, buflen);
19400 	if (result != ISC_R_SUCCESS) {
19401 		goto cleanup;
19402 	}
19403 
19404 	result = isc_file_renameunique(path, buf);
19405 	if (result != ISC_R_SUCCESS) {
19406 		goto cleanup;
19407 	}
19408 
19409 	dns_zone_log(zone, ISC_LOG_WARNING,
19410 		     "unable to load from '%s'; "
19411 		     "renaming file to '%s' for failure analysis and "
19412 		     "retransferring.",
19413 		     path, buf);
19414 
19415 cleanup:
19416 	isc_mem_put(zone->mctx, buf, buflen);
19417 }
19418 
19419 static void
setrl(isc_ratelimiter_t * rl,unsigned int * rate,unsigned int value)19420 setrl(isc_ratelimiter_t *rl, unsigned int *rate, unsigned int value) {
19421 	isc_interval_t interval;
19422 	uint32_t s, ns;
19423 	uint32_t pertic;
19424 	isc_result_t result;
19425 
19426 	if (value == 0) {
19427 		value = 1;
19428 	}
19429 
19430 	if (value == 1) {
19431 		s = 1;
19432 		ns = 0;
19433 		pertic = 1;
19434 	} else if (value <= 10) {
19435 		s = 0;
19436 		ns = 1000000000 / value;
19437 		pertic = 1;
19438 	} else {
19439 		s = 0;
19440 		ns = (1000000000 / value) * 10;
19441 		pertic = 10;
19442 	}
19443 
19444 	isc_interval_set(&interval, s, ns);
19445 
19446 	result = isc_ratelimiter_setinterval(rl, &interval);
19447 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
19448 	isc_ratelimiter_setpertic(rl, pertic);
19449 
19450 	*rate = value;
19451 }
19452 
19453 void
dns_zonemgr_setcheckdsrate(dns_zonemgr_t * zmgr,unsigned int value)19454 dns_zonemgr_setcheckdsrate(dns_zonemgr_t *zmgr, unsigned int value) {
19455 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19456 
19457 	setrl(zmgr->checkdsrl, &zmgr->checkdsrate, value);
19458 }
19459 
19460 void
dns_zonemgr_setnotifyrate(dns_zonemgr_t * zmgr,unsigned int value)19461 dns_zonemgr_setnotifyrate(dns_zonemgr_t *zmgr, unsigned int value) {
19462 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19463 
19464 	setrl(zmgr->notifyrl, &zmgr->notifyrate, value);
19465 }
19466 
19467 void
dns_zonemgr_setstartupnotifyrate(dns_zonemgr_t * zmgr,unsigned int value)19468 dns_zonemgr_setstartupnotifyrate(dns_zonemgr_t *zmgr, unsigned int value) {
19469 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19470 
19471 	setrl(zmgr->startupnotifyrl, &zmgr->startupnotifyrate, value);
19472 }
19473 
19474 void
dns_zonemgr_setserialqueryrate(dns_zonemgr_t * zmgr,unsigned int value)19475 dns_zonemgr_setserialqueryrate(dns_zonemgr_t *zmgr, unsigned int value) {
19476 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19477 
19478 	setrl(zmgr->refreshrl, &zmgr->serialqueryrate, value);
19479 	/* XXXMPA separate out once we have the code to support this. */
19480 	setrl(zmgr->startuprefreshrl, &zmgr->startupserialqueryrate, value);
19481 }
19482 
19483 unsigned int
dns_zonemgr_getnotifyrate(dns_zonemgr_t * zmgr)19484 dns_zonemgr_getnotifyrate(dns_zonemgr_t *zmgr) {
19485 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19486 
19487 	return (zmgr->notifyrate);
19488 }
19489 
19490 unsigned int
dns_zonemgr_getstartupnotifyrate(dns_zonemgr_t * zmgr)19491 dns_zonemgr_getstartupnotifyrate(dns_zonemgr_t *zmgr) {
19492 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19493 
19494 	return (zmgr->startupnotifyrate);
19495 }
19496 
19497 unsigned int
dns_zonemgr_getserialqueryrate(dns_zonemgr_t * zmgr)19498 dns_zonemgr_getserialqueryrate(dns_zonemgr_t *zmgr) {
19499 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19500 
19501 	return (zmgr->serialqueryrate);
19502 }
19503 
19504 bool
dns_zonemgr_unreachable(dns_zonemgr_t * zmgr,isc_sockaddr_t * remote,isc_sockaddr_t * local,isc_time_t * now)19505 dns_zonemgr_unreachable(dns_zonemgr_t *zmgr, isc_sockaddr_t *remote,
19506 			isc_sockaddr_t *local, isc_time_t *now) {
19507 	unsigned int i;
19508 	uint32_t seconds = isc_time_seconds(now);
19509 	uint32_t count = 0;
19510 
19511 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19512 
19513 	RWLOCK(&zmgr->urlock, isc_rwlocktype_read);
19514 	for (i = 0; i < UNREACH_CACHE_SIZE; i++) {
19515 		if (atomic_load(&zmgr->unreachable[i].expire) >= seconds &&
19516 		    isc_sockaddr_equal(&zmgr->unreachable[i].remote, remote) &&
19517 		    isc_sockaddr_equal(&zmgr->unreachable[i].local, local))
19518 		{
19519 			atomic_store_relaxed(&zmgr->unreachable[i].last,
19520 					     seconds);
19521 			count = zmgr->unreachable[i].count;
19522 			break;
19523 		}
19524 	}
19525 	RWUNLOCK(&zmgr->urlock, isc_rwlocktype_read);
19526 	return (i < UNREACH_CACHE_SIZE && count > 1U);
19527 }
19528 
19529 void
dns_zonemgr_unreachabledel(dns_zonemgr_t * zmgr,isc_sockaddr_t * remote,isc_sockaddr_t * local)19530 dns_zonemgr_unreachabledel(dns_zonemgr_t *zmgr, isc_sockaddr_t *remote,
19531 			   isc_sockaddr_t *local) {
19532 	unsigned int i;
19533 	char master[ISC_SOCKADDR_FORMATSIZE];
19534 	char source[ISC_SOCKADDR_FORMATSIZE];
19535 
19536 	isc_sockaddr_format(remote, master, sizeof(master));
19537 	isc_sockaddr_format(local, source, sizeof(source));
19538 
19539 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19540 
19541 	RWLOCK(&zmgr->urlock, isc_rwlocktype_read);
19542 	for (i = 0; i < UNREACH_CACHE_SIZE; i++) {
19543 		if (isc_sockaddr_equal(&zmgr->unreachable[i].remote, remote) &&
19544 		    isc_sockaddr_equal(&zmgr->unreachable[i].local, local))
19545 		{
19546 			atomic_store_relaxed(&zmgr->unreachable[i].expire, 0);
19547 			break;
19548 		}
19549 	}
19550 	RWUNLOCK(&zmgr->urlock, isc_rwlocktype_read);
19551 }
19552 
19553 void
dns_zonemgr_unreachableadd(dns_zonemgr_t * zmgr,isc_sockaddr_t * remote,isc_sockaddr_t * local,isc_time_t * now)19554 dns_zonemgr_unreachableadd(dns_zonemgr_t *zmgr, isc_sockaddr_t *remote,
19555 			   isc_sockaddr_t *local, isc_time_t *now) {
19556 	uint32_t seconds = isc_time_seconds(now);
19557 	uint32_t expire = 0, last = seconds;
19558 	unsigned int slot = UNREACH_CACHE_SIZE, oldest = 0;
19559 	bool update_entry = true;
19560 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19561 
19562 	RWLOCK(&zmgr->urlock, isc_rwlocktype_write);
19563 	for (unsigned int i = 0; i < UNREACH_CACHE_SIZE; i++) {
19564 		/* Existing entry? */
19565 		if (isc_sockaddr_equal(&zmgr->unreachable[i].remote, remote) &&
19566 		    isc_sockaddr_equal(&zmgr->unreachable[i].local, local))
19567 		{
19568 			update_entry = false;
19569 			slot = i;
19570 			expire = atomic_load_relaxed(
19571 				&zmgr->unreachable[i].expire);
19572 			break;
19573 		}
19574 		/* Pick first empty slot? */
19575 		if (atomic_load_relaxed(&zmgr->unreachable[i].expire) < seconds)
19576 		{
19577 			slot = i;
19578 			break;
19579 		}
19580 		/* The worst case, least recently used slot? */
19581 		if (atomic_load_relaxed(&zmgr->unreachable[i].last) < last) {
19582 			last = atomic_load_relaxed(&zmgr->unreachable[i].last);
19583 			oldest = i;
19584 		}
19585 	}
19586 
19587 	/* We haven't found any existing or free slots, use the oldest */
19588 	if (slot == UNREACH_CACHE_SIZE) {
19589 		slot = oldest;
19590 	}
19591 
19592 	if (expire < seconds) {
19593 		/* Expired or new entry, reset count to 1 */
19594 		zmgr->unreachable[slot].count = 1;
19595 	} else {
19596 		zmgr->unreachable[slot].count++;
19597 	}
19598 	atomic_store_relaxed(&zmgr->unreachable[slot].expire,
19599 			     seconds + UNREACH_HOLD_TIME);
19600 	atomic_store_relaxed(&zmgr->unreachable[slot].last, seconds);
19601 	if (update_entry) {
19602 		zmgr->unreachable[slot].remote = *remote;
19603 		zmgr->unreachable[slot].local = *local;
19604 	}
19605 
19606 	RWUNLOCK(&zmgr->urlock, isc_rwlocktype_write);
19607 }
19608 
19609 void
dns_zone_forcereload(dns_zone_t * zone)19610 dns_zone_forcereload(dns_zone_t *zone) {
19611 	REQUIRE(DNS_ZONE_VALID(zone));
19612 
19613 	if (zone->type == dns_zone_primary ||
19614 	    (zone->type == dns_zone_redirect && zone->masters == NULL))
19615 	{
19616 		return;
19617 	}
19618 
19619 	LOCK_ZONE(zone);
19620 	DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FORCEXFER);
19621 	UNLOCK_ZONE(zone);
19622 	dns_zone_refresh(zone);
19623 }
19624 
19625 bool
dns_zone_isforced(dns_zone_t * zone)19626 dns_zone_isforced(dns_zone_t *zone) {
19627 	REQUIRE(DNS_ZONE_VALID(zone));
19628 
19629 	return (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER));
19630 }
19631 
19632 isc_result_t
dns_zone_setstatistics(dns_zone_t * zone,bool on)19633 dns_zone_setstatistics(dns_zone_t *zone, bool on) {
19634 	/*
19635 	 * This function is obsoleted.
19636 	 */
19637 	UNUSED(zone);
19638 	UNUSED(on);
19639 	return (ISC_R_NOTIMPLEMENTED);
19640 }
19641 
19642 uint64_t *
dns_zone_getstatscounters(dns_zone_t * zone)19643 dns_zone_getstatscounters(dns_zone_t *zone) {
19644 	/*
19645 	 * This function is obsoleted.
19646 	 */
19647 	UNUSED(zone);
19648 	return (NULL);
19649 }
19650 
19651 void
dns_zone_setstats(dns_zone_t * zone,isc_stats_t * stats)19652 dns_zone_setstats(dns_zone_t *zone, isc_stats_t *stats) {
19653 	REQUIRE(DNS_ZONE_VALID(zone));
19654 	REQUIRE(zone->stats == NULL);
19655 
19656 	LOCK_ZONE(zone);
19657 	zone->stats = NULL;
19658 	isc_stats_attach(stats, &zone->stats);
19659 	UNLOCK_ZONE(zone);
19660 }
19661 
19662 void
dns_zone_setrequeststats(dns_zone_t * zone,isc_stats_t * stats)19663 dns_zone_setrequeststats(dns_zone_t *zone, isc_stats_t *stats) {
19664 	REQUIRE(DNS_ZONE_VALID(zone));
19665 
19666 	LOCK_ZONE(zone);
19667 	if (zone->requeststats_on && stats == NULL) {
19668 		zone->requeststats_on = false;
19669 	} else if (!zone->requeststats_on && stats != NULL) {
19670 		if (zone->requeststats == NULL) {
19671 			isc_stats_attach(stats, &zone->requeststats);
19672 		}
19673 		zone->requeststats_on = true;
19674 	}
19675 	UNLOCK_ZONE(zone);
19676 }
19677 
19678 void
dns_zone_setrcvquerystats(dns_zone_t * zone,dns_stats_t * stats)19679 dns_zone_setrcvquerystats(dns_zone_t *zone, dns_stats_t *stats) {
19680 	REQUIRE(DNS_ZONE_VALID(zone));
19681 
19682 	LOCK_ZONE(zone);
19683 	if (zone->requeststats_on && stats != NULL) {
19684 		if (zone->rcvquerystats == NULL) {
19685 			dns_stats_attach(stats, &zone->rcvquerystats);
19686 			zone->requeststats_on = true;
19687 		}
19688 	}
19689 	UNLOCK_ZONE(zone);
19690 }
19691 
19692 void
dns_zone_setdnssecsignstats(dns_zone_t * zone,dns_stats_t * stats)19693 dns_zone_setdnssecsignstats(dns_zone_t *zone, dns_stats_t *stats) {
19694 	REQUIRE(DNS_ZONE_VALID(zone));
19695 
19696 	LOCK_ZONE(zone);
19697 	if (stats != NULL && zone->dnssecsignstats == NULL) {
19698 		dns_stats_attach(stats, &zone->dnssecsignstats);
19699 	}
19700 	UNLOCK_ZONE(zone);
19701 }
19702 
19703 dns_stats_t *
dns_zone_getdnssecsignstats(dns_zone_t * zone)19704 dns_zone_getdnssecsignstats(dns_zone_t *zone) {
19705 	REQUIRE(DNS_ZONE_VALID(zone));
19706 
19707 	return (zone->dnssecsignstats);
19708 }
19709 
19710 isc_stats_t *
dns_zone_getrequeststats(dns_zone_t * zone)19711 dns_zone_getrequeststats(dns_zone_t *zone) {
19712 	/*
19713 	 * We don't lock zone for efficiency reason.  This is not catastrophic
19714 	 * because requeststats must always be valid when requeststats_on is
19715 	 * true.
19716 	 * Some counters may be incremented while requeststats_on is becoming
19717 	 * false, or some cannot be incremented just after the statistics are
19718 	 * installed, but it shouldn't matter much in practice.
19719 	 */
19720 	if (zone->requeststats_on) {
19721 		return (zone->requeststats);
19722 	} else {
19723 		return (NULL);
19724 	}
19725 }
19726 
19727 /*
19728  * Return the received query stats bucket
19729  * see note from dns_zone_getrequeststats()
19730  */
19731 dns_stats_t *
dns_zone_getrcvquerystats(dns_zone_t * zone)19732 dns_zone_getrcvquerystats(dns_zone_t *zone) {
19733 	if (zone->requeststats_on) {
19734 		return (zone->rcvquerystats);
19735 	} else {
19736 		return (NULL);
19737 	}
19738 }
19739 
19740 void
dns_zone_dialup(dns_zone_t * zone)19741 dns_zone_dialup(dns_zone_t *zone) {
19742 	REQUIRE(DNS_ZONE_VALID(zone));
19743 
19744 	zone_debuglog(zone, "dns_zone_dialup", 3, "notify = %d, refresh = %d",
19745 		      DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALNOTIFY),
19746 		      DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH));
19747 
19748 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALNOTIFY)) {
19749 		dns_zone_notify(zone);
19750 	}
19751 	if (zone->type != dns_zone_primary && zone->masters != NULL &&
19752 	    DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DIALREFRESH))
19753 	{
19754 		dns_zone_refresh(zone);
19755 	}
19756 }
19757 
19758 void
dns_zone_setdialup(dns_zone_t * zone,dns_dialuptype_t dialup)19759 dns_zone_setdialup(dns_zone_t *zone, dns_dialuptype_t dialup) {
19760 	REQUIRE(DNS_ZONE_VALID(zone));
19761 
19762 	LOCK_ZONE(zone);
19763 	DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_DIALNOTIFY |
19764 				       DNS_ZONEFLG_DIALREFRESH |
19765 				       DNS_ZONEFLG_NOREFRESH);
19766 	switch (dialup) {
19767 	case dns_dialuptype_no:
19768 		break;
19769 	case dns_dialuptype_yes:
19770 		DNS_ZONE_SETFLAG(zone, (DNS_ZONEFLG_DIALNOTIFY |
19771 					DNS_ZONEFLG_DIALREFRESH |
19772 					DNS_ZONEFLG_NOREFRESH));
19773 		break;
19774 	case dns_dialuptype_notify:
19775 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DIALNOTIFY);
19776 		break;
19777 	case dns_dialuptype_notifypassive:
19778 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DIALNOTIFY);
19779 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOREFRESH);
19780 		break;
19781 	case dns_dialuptype_refresh:
19782 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DIALREFRESH);
19783 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOREFRESH);
19784 		break;
19785 	case dns_dialuptype_passive:
19786 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOREFRESH);
19787 		break;
19788 	default:
19789 		INSIST(0);
19790 		ISC_UNREACHABLE();
19791 	}
19792 	UNLOCK_ZONE(zone);
19793 }
19794 
19795 isc_result_t
dns_zone_setkeydirectory(dns_zone_t * zone,const char * directory)19796 dns_zone_setkeydirectory(dns_zone_t *zone, const char *directory) {
19797 	isc_result_t result = ISC_R_SUCCESS;
19798 
19799 	REQUIRE(DNS_ZONE_VALID(zone));
19800 
19801 	LOCK_ZONE(zone);
19802 	result = dns_zone_setstring(zone, &zone->keydirectory, directory);
19803 	UNLOCK_ZONE(zone);
19804 
19805 	return (result);
19806 }
19807 
19808 const char *
dns_zone_getkeydirectory(dns_zone_t * zone)19809 dns_zone_getkeydirectory(dns_zone_t *zone) {
19810 	REQUIRE(DNS_ZONE_VALID(zone));
19811 
19812 	return (zone->keydirectory);
19813 }
19814 
19815 unsigned int
dns_zonemgr_getcount(dns_zonemgr_t * zmgr,int state)19816 dns_zonemgr_getcount(dns_zonemgr_t *zmgr, int state) {
19817 	dns_zone_t *zone;
19818 	unsigned int count = 0;
19819 
19820 	REQUIRE(DNS_ZONEMGR_VALID(zmgr));
19821 
19822 	RWLOCK(&zmgr->rwlock, isc_rwlocktype_read);
19823 	switch (state) {
19824 	case DNS_ZONESTATE_XFERRUNNING:
19825 		for (zone = ISC_LIST_HEAD(zmgr->xfrin_in_progress);
19826 		     zone != NULL; zone = ISC_LIST_NEXT(zone, statelink))
19827 		{
19828 			count++;
19829 		}
19830 		break;
19831 	case DNS_ZONESTATE_XFERDEFERRED:
19832 		for (zone = ISC_LIST_HEAD(zmgr->waiting_for_xfrin);
19833 		     zone != NULL; zone = ISC_LIST_NEXT(zone, statelink))
19834 		{
19835 			count++;
19836 		}
19837 		break;
19838 	case DNS_ZONESTATE_SOAQUERY:
19839 		for (zone = ISC_LIST_HEAD(zmgr->zones); zone != NULL;
19840 		     zone = ISC_LIST_NEXT(zone, link))
19841 		{
19842 			if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH)) {
19843 				count++;
19844 			}
19845 		}
19846 		break;
19847 	case DNS_ZONESTATE_ANY:
19848 		for (zone = ISC_LIST_HEAD(zmgr->zones); zone != NULL;
19849 		     zone = ISC_LIST_NEXT(zone, link))
19850 		{
19851 			dns_view_t *view = zone->view;
19852 			if (view != NULL && strcmp(view->name, "_bind") == 0) {
19853 				continue;
19854 			}
19855 			count++;
19856 		}
19857 		break;
19858 	case DNS_ZONESTATE_AUTOMATIC:
19859 		for (zone = ISC_LIST_HEAD(zmgr->zones); zone != NULL;
19860 		     zone = ISC_LIST_NEXT(zone, link))
19861 		{
19862 			dns_view_t *view = zone->view;
19863 			if (view != NULL && strcmp(view->name, "_bind") == 0) {
19864 				continue;
19865 			}
19866 			if (zone->automatic) {
19867 				count++;
19868 			}
19869 		}
19870 		break;
19871 	default:
19872 		INSIST(0);
19873 		ISC_UNREACHABLE();
19874 	}
19875 
19876 	RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_read);
19877 
19878 	return (count);
19879 }
19880 
19881 static void
dns__zone_lockunlock_keyfiles(dns_zone_t * zone,bool lock)19882 dns__zone_lockunlock_keyfiles(dns_zone_t *zone, bool lock) {
19883 	dns_keyfileio_t *kfio = NULL;
19884 
19885 	REQUIRE(DNS_ZONE_VALID(zone));
19886 
19887 	if (zone->kasp == NULL) {
19888 		/* No need to lock, nothing is writing key files. */
19889 		return;
19890 	}
19891 
19892 	zonemgr_keymgmt_find(zone->zmgr, zone, &kfio);
19893 	if (kfio == NULL) {
19894 		/* Should not happen, but if so, add the entry now. */
19895 		dns_zone_log(zone, ISC_LOG_WARNING,
19896 			     "attempt to lock key files, but no key file lock "
19897 			     "available, abort");
19898 		return;
19899 	}
19900 
19901 	if (lock) {
19902 		isc_mutex_lock(&kfio->lock);
19903 	} else {
19904 		isc_mutex_unlock(&kfio->lock);
19905 	}
19906 }
19907 
19908 void
dns_zone_lock_keyfiles(dns_zone_t * zone)19909 dns_zone_lock_keyfiles(dns_zone_t *zone) {
19910 	dns__zone_lockunlock_keyfiles(zone, true);
19911 }
19912 
19913 void
dns_zone_unlock_keyfiles(dns_zone_t * zone)19914 dns_zone_unlock_keyfiles(dns_zone_t *zone) {
19915 	dns__zone_lockunlock_keyfiles(zone, false);
19916 }
19917 
19918 isc_result_t
dns_zone_checknames(dns_zone_t * zone,const dns_name_t * name,dns_rdata_t * rdata)19919 dns_zone_checknames(dns_zone_t *zone, const dns_name_t *name,
19920 		    dns_rdata_t *rdata) {
19921 	bool ok = true;
19922 	bool fail = false;
19923 	char namebuf[DNS_NAME_FORMATSIZE];
19924 	char namebuf2[DNS_NAME_FORMATSIZE];
19925 	char typebuf[DNS_RDATATYPE_FORMATSIZE];
19926 	int level = ISC_LOG_WARNING;
19927 	dns_name_t bad;
19928 
19929 	REQUIRE(DNS_ZONE_VALID(zone));
19930 
19931 	if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNAMES) &&
19932 	    rdata->type != dns_rdatatype_nsec3)
19933 	{
19934 		return (ISC_R_SUCCESS);
19935 	}
19936 
19937 	if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNAMESFAIL) ||
19938 	    rdata->type == dns_rdatatype_nsec3)
19939 	{
19940 		level = ISC_LOG_ERROR;
19941 		fail = true;
19942 	}
19943 
19944 	ok = dns_rdata_checkowner(name, rdata->rdclass, rdata->type, true);
19945 	if (!ok) {
19946 		dns_name_format(name, namebuf, sizeof(namebuf));
19947 		dns_rdatatype_format(rdata->type, typebuf, sizeof(typebuf));
19948 		dns_zone_log(zone, level, "%s/%s: %s", namebuf, typebuf,
19949 			     dns_result_totext(DNS_R_BADOWNERNAME));
19950 		if (fail) {
19951 			return (DNS_R_BADOWNERNAME);
19952 		}
19953 	}
19954 
19955 	dns_name_init(&bad, NULL);
19956 	ok = dns_rdata_checknames(rdata, name, &bad);
19957 	if (!ok) {
19958 		dns_name_format(name, namebuf, sizeof(namebuf));
19959 		dns_name_format(&bad, namebuf2, sizeof(namebuf2));
19960 		dns_rdatatype_format(rdata->type, typebuf, sizeof(typebuf));
19961 		dns_zone_log(zone, level, "%s/%s: %s: %s ", namebuf, typebuf,
19962 			     namebuf2, dns_result_totext(DNS_R_BADNAME));
19963 		if (fail) {
19964 			return (DNS_R_BADNAME);
19965 		}
19966 	}
19967 
19968 	return (ISC_R_SUCCESS);
19969 }
19970 
19971 void
dns_zone_setcheckmx(dns_zone_t * zone,dns_checkmxfunc_t checkmx)19972 dns_zone_setcheckmx(dns_zone_t *zone, dns_checkmxfunc_t checkmx) {
19973 	REQUIRE(DNS_ZONE_VALID(zone));
19974 	zone->checkmx = checkmx;
19975 }
19976 
19977 void
dns_zone_setchecksrv(dns_zone_t * zone,dns_checksrvfunc_t checksrv)19978 dns_zone_setchecksrv(dns_zone_t *zone, dns_checksrvfunc_t checksrv) {
19979 	REQUIRE(DNS_ZONE_VALID(zone));
19980 	zone->checksrv = checksrv;
19981 }
19982 
19983 void
dns_zone_setcheckns(dns_zone_t * zone,dns_checknsfunc_t checkns)19984 dns_zone_setcheckns(dns_zone_t *zone, dns_checknsfunc_t checkns) {
19985 	REQUIRE(DNS_ZONE_VALID(zone));
19986 	zone->checkns = checkns;
19987 }
19988 
19989 void
dns_zone_setisself(dns_zone_t * zone,dns_isselffunc_t isself,void * arg)19990 dns_zone_setisself(dns_zone_t *zone, dns_isselffunc_t isself, void *arg) {
19991 	REQUIRE(DNS_ZONE_VALID(zone));
19992 
19993 	LOCK_ZONE(zone);
19994 	zone->isself = isself;
19995 	zone->isselfarg = arg;
19996 	UNLOCK_ZONE(zone);
19997 }
19998 
19999 void
dns_zone_setnotifydelay(dns_zone_t * zone,uint32_t delay)20000 dns_zone_setnotifydelay(dns_zone_t *zone, uint32_t delay) {
20001 	REQUIRE(DNS_ZONE_VALID(zone));
20002 
20003 	LOCK_ZONE(zone);
20004 	zone->notifydelay = delay;
20005 	UNLOCK_ZONE(zone);
20006 }
20007 
20008 uint32_t
dns_zone_getnotifydelay(dns_zone_t * zone)20009 dns_zone_getnotifydelay(dns_zone_t *zone) {
20010 	REQUIRE(DNS_ZONE_VALID(zone));
20011 
20012 	return (zone->notifydelay);
20013 }
20014 
20015 isc_result_t
dns_zone_signwithkey(dns_zone_t * zone,dns_secalg_t algorithm,uint16_t keyid,bool deleteit)20016 dns_zone_signwithkey(dns_zone_t *zone, dns_secalg_t algorithm, uint16_t keyid,
20017 		     bool deleteit) {
20018 	isc_result_t result;
20019 	REQUIRE(DNS_ZONE_VALID(zone));
20020 
20021 	dnssec_log(zone, ISC_LOG_NOTICE,
20022 		   "dns_zone_signwithkey(algorithm=%u, keyid=%u)", algorithm,
20023 		   keyid);
20024 	LOCK_ZONE(zone);
20025 	result = zone_signwithkey(zone, algorithm, keyid, deleteit);
20026 	UNLOCK_ZONE(zone);
20027 
20028 	return (result);
20029 }
20030 
20031 /*
20032  * Called when a dynamic update for an NSEC3PARAM record is received.
20033  *
20034  * If set, transform the NSEC3 salt into human-readable form so that it can be
20035  * logged.  Then call zone_addnsec3chain(), passing NSEC3PARAM RDATA to it.
20036  */
20037 isc_result_t
dns_zone_addnsec3chain(dns_zone_t * zone,dns_rdata_nsec3param_t * nsec3param)20038 dns_zone_addnsec3chain(dns_zone_t *zone, dns_rdata_nsec3param_t *nsec3param) {
20039 	isc_result_t result;
20040 	char salt[255 * 2 + 1];
20041 
20042 	REQUIRE(DNS_ZONE_VALID(zone));
20043 
20044 	result = dns_nsec3param_salttotext(nsec3param, salt, sizeof(salt));
20045 	RUNTIME_CHECK(result == ISC_R_SUCCESS);
20046 	dnssec_log(zone, ISC_LOG_NOTICE,
20047 		   "dns_zone_addnsec3chain(hash=%u, iterations=%u, salt=%s)",
20048 		   nsec3param->hash, nsec3param->iterations, salt);
20049 	LOCK_ZONE(zone);
20050 	result = zone_addnsec3chain(zone, nsec3param);
20051 	UNLOCK_ZONE(zone);
20052 
20053 	return (result);
20054 }
20055 
20056 void
dns_zone_setnodes(dns_zone_t * zone,uint32_t nodes)20057 dns_zone_setnodes(dns_zone_t *zone, uint32_t nodes) {
20058 	REQUIRE(DNS_ZONE_VALID(zone));
20059 
20060 	if (nodes == 0) {
20061 		nodes = 1;
20062 	}
20063 	zone->nodes = nodes;
20064 }
20065 
20066 void
dns_zone_setsignatures(dns_zone_t * zone,uint32_t signatures)20067 dns_zone_setsignatures(dns_zone_t *zone, uint32_t signatures) {
20068 	REQUIRE(DNS_ZONE_VALID(zone));
20069 
20070 	/*
20071 	 * We treat signatures as a signed value so explicitly
20072 	 * limit its range here.
20073 	 */
20074 	if (signatures > INT32_MAX) {
20075 		signatures = INT32_MAX;
20076 	} else if (signatures == 0) {
20077 		signatures = 1;
20078 	}
20079 	zone->signatures = signatures;
20080 }
20081 
20082 uint32_t
dns_zone_getsignatures(dns_zone_t * zone)20083 dns_zone_getsignatures(dns_zone_t *zone) {
20084 	REQUIRE(DNS_ZONE_VALID(zone));
20085 	return (zone->signatures);
20086 }
20087 
20088 void
dns_zone_setprivatetype(dns_zone_t * zone,dns_rdatatype_t type)20089 dns_zone_setprivatetype(dns_zone_t *zone, dns_rdatatype_t type) {
20090 	REQUIRE(DNS_ZONE_VALID(zone));
20091 	zone->privatetype = type;
20092 }
20093 
20094 dns_rdatatype_t
dns_zone_getprivatetype(dns_zone_t * zone)20095 dns_zone_getprivatetype(dns_zone_t *zone) {
20096 	REQUIRE(DNS_ZONE_VALID(zone));
20097 	return (zone->privatetype);
20098 }
20099 
20100 static isc_result_t
zone_signwithkey(dns_zone_t * zone,dns_secalg_t algorithm,uint16_t keyid,bool deleteit)20101 zone_signwithkey(dns_zone_t *zone, dns_secalg_t algorithm, uint16_t keyid,
20102 		 bool deleteit) {
20103 	dns_signing_t *signing;
20104 	dns_signing_t *current;
20105 	isc_result_t result = ISC_R_SUCCESS;
20106 	isc_time_t now;
20107 	dns_db_t *db = NULL;
20108 
20109 	signing = isc_mem_get(zone->mctx, sizeof *signing);
20110 
20111 	signing->magic = 0;
20112 	signing->db = NULL;
20113 	signing->dbiterator = NULL;
20114 	signing->algorithm = algorithm;
20115 	signing->keyid = keyid;
20116 	signing->deleteit = deleteit;
20117 	signing->done = false;
20118 
20119 	TIME_NOW(&now);
20120 
20121 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
20122 	if (zone->db != NULL) {
20123 		dns_db_attach(zone->db, &db);
20124 	}
20125 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
20126 
20127 	if (db == NULL) {
20128 		result = ISC_R_NOTFOUND;
20129 		goto cleanup;
20130 	}
20131 
20132 	dns_db_attach(db, &signing->db);
20133 
20134 	for (current = ISC_LIST_HEAD(zone->signing); current != NULL;
20135 	     current = ISC_LIST_NEXT(current, link))
20136 	{
20137 		if (current->db == signing->db &&
20138 		    current->algorithm == signing->algorithm &&
20139 		    current->keyid == signing->keyid)
20140 		{
20141 			if (current->deleteit != signing->deleteit) {
20142 				current->done = true;
20143 			} else {
20144 				goto cleanup;
20145 			}
20146 		}
20147 	}
20148 
20149 	result = dns_db_createiterator(signing->db, 0, &signing->dbiterator);
20150 
20151 	if (result == ISC_R_SUCCESS) {
20152 		result = dns_dbiterator_first(signing->dbiterator);
20153 	}
20154 	if (result == ISC_R_SUCCESS) {
20155 		dns_dbiterator_pause(signing->dbiterator);
20156 		ISC_LIST_INITANDAPPEND(zone->signing, signing, link);
20157 		signing = NULL;
20158 		if (isc_time_isepoch(&zone->signingtime)) {
20159 			zone->signingtime = now;
20160 			if (zone->task != NULL) {
20161 				zone_settimer(zone, &now);
20162 			}
20163 		}
20164 	}
20165 
20166 cleanup:
20167 	if (signing != NULL) {
20168 		if (signing->db != NULL) {
20169 			dns_db_detach(&signing->db);
20170 		}
20171 		if (signing->dbiterator != NULL) {
20172 			dns_dbiterator_destroy(&signing->dbiterator);
20173 		}
20174 		isc_mem_put(zone->mctx, signing, sizeof *signing);
20175 	}
20176 	if (db != NULL) {
20177 		dns_db_detach(&db);
20178 	}
20179 	return (result);
20180 }
20181 
20182 /* Called once; *timep should be set to the current time. */
20183 static isc_result_t
next_keyevent(dst_key_t * key,isc_stdtime_t * timep)20184 next_keyevent(dst_key_t *key, isc_stdtime_t *timep) {
20185 	isc_result_t result;
20186 	isc_stdtime_t now, then = 0, event;
20187 	int i;
20188 
20189 	now = *timep;
20190 
20191 	for (i = 0; i <= DST_MAX_TIMES; i++) {
20192 		result = dst_key_gettime(key, i, &event);
20193 		if (result == ISC_R_SUCCESS && event > now &&
20194 		    (then == 0 || event < then)) {
20195 			then = event;
20196 		}
20197 	}
20198 
20199 	if (then != 0) {
20200 		*timep = then;
20201 		return (ISC_R_SUCCESS);
20202 	}
20203 
20204 	return (ISC_R_NOTFOUND);
20205 }
20206 
20207 static isc_result_t
rr_exists(dns_db_t * db,dns_dbversion_t * ver,dns_name_t * name,const dns_rdata_t * rdata,bool * flag)20208 rr_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name,
20209 	  const dns_rdata_t *rdata, bool *flag) {
20210 	dns_rdataset_t rdataset;
20211 	dns_dbnode_t *node = NULL;
20212 	isc_result_t result;
20213 
20214 	dns_rdataset_init(&rdataset);
20215 	if (rdata->type == dns_rdatatype_nsec3) {
20216 		CHECK(dns_db_findnsec3node(db, name, false, &node));
20217 	} else {
20218 		CHECK(dns_db_findnode(db, name, false, &node));
20219 	}
20220 	result = dns_db_findrdataset(db, node, ver, rdata->type, 0,
20221 				     (isc_stdtime_t)0, &rdataset, NULL);
20222 	if (result == ISC_R_NOTFOUND) {
20223 		*flag = false;
20224 		result = ISC_R_SUCCESS;
20225 		goto failure;
20226 	}
20227 
20228 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
20229 	     result = dns_rdataset_next(&rdataset))
20230 	{
20231 		dns_rdata_t myrdata = DNS_RDATA_INIT;
20232 		dns_rdataset_current(&rdataset, &myrdata);
20233 		if (!dns_rdata_compare(&myrdata, rdata)) {
20234 			break;
20235 		}
20236 	}
20237 	dns_rdataset_disassociate(&rdataset);
20238 	if (result == ISC_R_SUCCESS) {
20239 		*flag = true;
20240 	} else if (result == ISC_R_NOMORE) {
20241 		*flag = false;
20242 		result = ISC_R_SUCCESS;
20243 	}
20244 
20245 failure:
20246 	if (node != NULL) {
20247 		dns_db_detachnode(db, &node);
20248 	}
20249 	return (result);
20250 }
20251 
20252 /*
20253  * Add records to signal the state of signing or of key removal.
20254  */
20255 static isc_result_t
add_signing_records(dns_db_t * db,dns_rdatatype_t privatetype,dns_dbversion_t * ver,dns_diff_t * diff,bool sign_all)20256 add_signing_records(dns_db_t *db, dns_rdatatype_t privatetype,
20257 		    dns_dbversion_t *ver, dns_diff_t *diff, bool sign_all) {
20258 	dns_difftuple_t *tuple, *newtuple = NULL;
20259 	dns_rdata_dnskey_t dnskey;
20260 	dns_rdata_t rdata = DNS_RDATA_INIT;
20261 	bool flag;
20262 	isc_region_t r;
20263 	isc_result_t result = ISC_R_SUCCESS;
20264 	uint16_t keyid;
20265 	unsigned char buf[5];
20266 	dns_name_t *name = dns_db_origin(db);
20267 
20268 	for (tuple = ISC_LIST_HEAD(diff->tuples); tuple != NULL;
20269 	     tuple = ISC_LIST_NEXT(tuple, link))
20270 	{
20271 		if (tuple->rdata.type != dns_rdatatype_dnskey) {
20272 			continue;
20273 		}
20274 
20275 		result = dns_rdata_tostruct(&tuple->rdata, &dnskey, NULL);
20276 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
20277 		if ((dnskey.flags & (DNS_KEYFLAG_OWNERMASK |
20278 				     DNS_KEYTYPE_NOAUTH)) != DNS_KEYOWNER_ZONE)
20279 		{
20280 			continue;
20281 		}
20282 
20283 		dns_rdata_toregion(&tuple->rdata, &r);
20284 
20285 		keyid = dst_region_computeid(&r);
20286 
20287 		buf[0] = dnskey.algorithm;
20288 		buf[1] = (keyid & 0xff00) >> 8;
20289 		buf[2] = (keyid & 0xff);
20290 		buf[3] = (tuple->op == DNS_DIFFOP_ADD) ? 0 : 1;
20291 		buf[4] = 0;
20292 		rdata.data = buf;
20293 		rdata.length = sizeof(buf);
20294 		rdata.type = privatetype;
20295 		rdata.rdclass = tuple->rdata.rdclass;
20296 
20297 		if (sign_all || tuple->op == DNS_DIFFOP_DEL) {
20298 			CHECK(rr_exists(db, ver, name, &rdata, &flag));
20299 			if (flag) {
20300 				continue;
20301 			}
20302 
20303 			CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD,
20304 						   name, 0, &rdata, &newtuple));
20305 			CHECK(do_one_tuple(&newtuple, db, ver, diff));
20306 			INSIST(newtuple == NULL);
20307 		}
20308 
20309 		/*
20310 		 * Remove any record which says this operation has already
20311 		 * completed.
20312 		 */
20313 		buf[4] = 1;
20314 		CHECK(rr_exists(db, ver, name, &rdata, &flag));
20315 		if (flag) {
20316 			CHECK(dns_difftuple_create(diff->mctx, DNS_DIFFOP_DEL,
20317 						   name, 0, &rdata, &newtuple));
20318 			CHECK(do_one_tuple(&newtuple, db, ver, diff));
20319 			INSIST(newtuple == NULL);
20320 		}
20321 	}
20322 failure:
20323 	return (result);
20324 }
20325 
20326 /*
20327  * See if dns__zone_updatesigs() will update signature for RRset 'rrtype' at
20328  * the apex, and if not tickle them and cause to sign so that newly activated
20329  * keys are used.
20330  */
20331 static isc_result_t
tickle_apex_rrset(dns_rdatatype_t rrtype,dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,isc_stdtime_t now,dns_diff_t * diff,dns__zonediff_t * zonediff,dst_key_t ** keys,unsigned int nkeys,isc_stdtime_t inception,isc_stdtime_t keyexpire,bool check_ksk,bool keyset_kskonly)20332 tickle_apex_rrset(dns_rdatatype_t rrtype, dns_zone_t *zone, dns_db_t *db,
20333 		  dns_dbversion_t *ver, isc_stdtime_t now, dns_diff_t *diff,
20334 		  dns__zonediff_t *zonediff, dst_key_t **keys,
20335 		  unsigned int nkeys, isc_stdtime_t inception,
20336 		  isc_stdtime_t keyexpire, bool check_ksk,
20337 		  bool keyset_kskonly) {
20338 	dns_difftuple_t *tuple;
20339 	isc_result_t result;
20340 
20341 	for (tuple = ISC_LIST_HEAD(diff->tuples); tuple != NULL;
20342 	     tuple = ISC_LIST_NEXT(tuple, link))
20343 	{
20344 		if (tuple->rdata.type == rrtype &&
20345 		    dns_name_equal(&tuple->name, &zone->origin)) {
20346 			break;
20347 		}
20348 	}
20349 
20350 	if (tuple == NULL) {
20351 		result = del_sigs(zone, db, ver, &zone->origin, rrtype,
20352 				  zonediff, keys, nkeys, now, false);
20353 		if (result != ISC_R_SUCCESS) {
20354 			dnssec_log(zone, ISC_LOG_ERROR,
20355 				   "sign_apex:del_sigs -> %s",
20356 				   dns_result_totext(result));
20357 			return (result);
20358 		}
20359 		result = add_sigs(db, ver, &zone->origin, zone, rrtype,
20360 				  zonediff->diff, keys, nkeys, zone->mctx,
20361 				  inception, keyexpire, check_ksk,
20362 				  keyset_kskonly);
20363 		if (result != ISC_R_SUCCESS) {
20364 			dnssec_log(zone, ISC_LOG_ERROR,
20365 				   "sign_apex:add_sigs -> %s",
20366 				   dns_result_totext(result));
20367 			return (result);
20368 		}
20369 	}
20370 
20371 	return (ISC_R_SUCCESS);
20372 }
20373 
20374 static isc_result_t
sign_apex(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,isc_stdtime_t now,dns_diff_t * diff,dns__zonediff_t * zonediff)20375 sign_apex(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
20376 	  isc_stdtime_t now, dns_diff_t *diff, dns__zonediff_t *zonediff) {
20377 	isc_result_t result;
20378 	isc_stdtime_t inception, soaexpire, keyexpire;
20379 	bool check_ksk, keyset_kskonly;
20380 	dst_key_t *zone_keys[DNS_MAXZONEKEYS];
20381 	unsigned int nkeys = 0, i;
20382 
20383 	result = dns__zone_findkeys(zone, db, ver, now, zone->mctx,
20384 				    DNS_MAXZONEKEYS, zone_keys, &nkeys);
20385 	if (result != ISC_R_SUCCESS) {
20386 		dnssec_log(zone, ISC_LOG_ERROR,
20387 			   "sign_apex:dns__zone_findkeys -> %s",
20388 			   dns_result_totext(result));
20389 		return (result);
20390 	}
20391 
20392 	inception = now - 3600; /* Allow for clock skew. */
20393 	soaexpire = now + dns_zone_getsigvalidityinterval(zone);
20394 
20395 	keyexpire = dns_zone_getkeyvalidityinterval(zone);
20396 	if (keyexpire == 0) {
20397 		keyexpire = soaexpire - 1;
20398 	} else {
20399 		keyexpire += now;
20400 	}
20401 
20402 	check_ksk = DNS_ZONE_OPTION(zone, DNS_ZONEOPT_UPDATECHECKKSK);
20403 	keyset_kskonly = DNS_ZONE_OPTION(zone, DNS_ZONEOPT_DNSKEYKSKONLY);
20404 
20405 	/*
20406 	 * See if dns__zone_updatesigs() will update DNSKEY/CDS/CDNSKEY
20407 	 * signature and if not cause them to sign so that newly activated
20408 	 * keys are used.
20409 	 */
20410 	result = tickle_apex_rrset(dns_rdatatype_dnskey, zone, db, ver, now,
20411 				   diff, zonediff, zone_keys, nkeys, inception,
20412 				   keyexpire, check_ksk, keyset_kskonly);
20413 	if (result != ISC_R_SUCCESS) {
20414 		goto failure;
20415 	}
20416 	result = tickle_apex_rrset(dns_rdatatype_cds, zone, db, ver, now, diff,
20417 				   zonediff, zone_keys, nkeys, inception,
20418 				   keyexpire, check_ksk, keyset_kskonly);
20419 	if (result != ISC_R_SUCCESS) {
20420 		goto failure;
20421 	}
20422 	result = tickle_apex_rrset(dns_rdatatype_cdnskey, zone, db, ver, now,
20423 				   diff, zonediff, zone_keys, nkeys, inception,
20424 				   keyexpire, check_ksk, keyset_kskonly);
20425 	if (result != ISC_R_SUCCESS) {
20426 		goto failure;
20427 	}
20428 
20429 	result = dns__zone_updatesigs(diff, db, ver, zone_keys, nkeys, zone,
20430 				      inception, soaexpire, keyexpire, now,
20431 				      check_ksk, keyset_kskonly, zonediff);
20432 
20433 	if (result != ISC_R_SUCCESS) {
20434 		dnssec_log(zone, ISC_LOG_ERROR,
20435 			   "sign_apex:dns__zone_updatesigs -> %s",
20436 			   dns_result_totext(result));
20437 		goto failure;
20438 	}
20439 
20440 failure:
20441 	for (i = 0; i < nkeys; i++) {
20442 		dst_key_free(&zone_keys[i]);
20443 	}
20444 	return (result);
20445 }
20446 
20447 /*
20448  * Prevent the zone entering a inconsistent state where
20449  * NSEC only DNSKEYs are present with NSEC3 chains.
20450  * See update.c:check_dnssec()
20451  */
20452 static bool
dnskey_sane(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,dns_diff_t * diff)20453 dnskey_sane(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
20454 	    dns_diff_t *diff) {
20455 	isc_result_t result;
20456 	dns_difftuple_t *tuple;
20457 	bool nseconly = false, nsec3 = false;
20458 	dns_rdatatype_t privatetype = dns_zone_getprivatetype(zone);
20459 
20460 	/* Scan the tuples for an NSEC-only DNSKEY */
20461 	for (tuple = ISC_LIST_HEAD(diff->tuples); tuple != NULL;
20462 	     tuple = ISC_LIST_NEXT(tuple, link))
20463 	{
20464 		uint8_t alg;
20465 		if (tuple->rdata.type != dns_rdatatype_dnskey ||
20466 		    tuple->op != DNS_DIFFOP_ADD) {
20467 			continue;
20468 		}
20469 
20470 		alg = tuple->rdata.data[3];
20471 		if (alg == DST_ALG_RSASHA1) {
20472 			nseconly = true;
20473 			break;
20474 		}
20475 	}
20476 
20477 	/* Check existing DB for NSEC-only DNSKEY */
20478 	if (!nseconly) {
20479 		result = dns_nsec_nseconly(db, ver, &nseconly);
20480 		if (result == ISC_R_NOTFOUND) {
20481 			result = ISC_R_SUCCESS;
20482 		}
20483 		CHECK(result);
20484 	}
20485 
20486 	/* Check existing DB for NSEC3 */
20487 	if (!nsec3) {
20488 		CHECK(dns_nsec3_activex(db, ver, false, privatetype, &nsec3));
20489 	}
20490 
20491 	/* Refuse to allow NSEC3 with NSEC-only keys */
20492 	if (nseconly && nsec3) {
20493 		dnssec_log(zone, ISC_LOG_ERROR,
20494 			   "NSEC only DNSKEYs and NSEC3 chains not allowed");
20495 		goto failure;
20496 	}
20497 
20498 	return (true);
20499 
20500 failure:
20501 	return (false);
20502 }
20503 
20504 static isc_result_t
clean_nsec3param(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,dns_diff_t * diff)20505 clean_nsec3param(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
20506 		 dns_diff_t *diff) {
20507 	isc_result_t result;
20508 	dns_dbnode_t *node = NULL;
20509 	dns_rdataset_t rdataset;
20510 
20511 	dns_rdataset_init(&rdataset);
20512 	CHECK(dns_db_getoriginnode(db, &node));
20513 
20514 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey,
20515 				     dns_rdatatype_none, 0, &rdataset, NULL);
20516 	if (dns_rdataset_isassociated(&rdataset)) {
20517 		dns_rdataset_disassociate(&rdataset);
20518 	}
20519 	if (result != ISC_R_NOTFOUND) {
20520 		goto failure;
20521 	}
20522 
20523 	result = dns_nsec3param_deletechains(db, ver, zone, true, diff);
20524 
20525 failure:
20526 	if (node != NULL) {
20527 		dns_db_detachnode(db, &node);
20528 	}
20529 	return (result);
20530 }
20531 
20532 /*
20533  * Given an RRSIG rdataset and an algorithm, determine whether there
20534  * are any signatures using that algorithm.
20535  */
20536 static bool
signed_with_alg(dns_rdataset_t * rdataset,dns_secalg_t alg)20537 signed_with_alg(dns_rdataset_t *rdataset, dns_secalg_t alg) {
20538 	dns_rdata_t rdata = DNS_RDATA_INIT;
20539 	dns_rdata_rrsig_t rrsig;
20540 	isc_result_t result;
20541 
20542 	REQUIRE(rdataset == NULL || rdataset->type == dns_rdatatype_rrsig);
20543 	if (rdataset == NULL || !dns_rdataset_isassociated(rdataset)) {
20544 		return (false);
20545 	}
20546 
20547 	for (result = dns_rdataset_first(rdataset); result == ISC_R_SUCCESS;
20548 	     result = dns_rdataset_next(rdataset))
20549 	{
20550 		dns_rdataset_current(rdataset, &rdata);
20551 		result = dns_rdata_tostruct(&rdata, &rrsig, NULL);
20552 		RUNTIME_CHECK(result == ISC_R_SUCCESS);
20553 		dns_rdata_reset(&rdata);
20554 		if (rrsig.algorithm == alg) {
20555 			return (true);
20556 		}
20557 	}
20558 
20559 	return (false);
20560 }
20561 
20562 static isc_result_t
add_chains(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver,dns_diff_t * diff)20563 add_chains(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver,
20564 	   dns_diff_t *diff) {
20565 	dns_name_t *origin;
20566 	bool build_nsec3;
20567 	isc_result_t result;
20568 
20569 	origin = dns_db_origin(db);
20570 	CHECK(dns_private_chains(db, ver, zone->privatetype, NULL,
20571 				 &build_nsec3));
20572 	if (build_nsec3) {
20573 		CHECK(dns_nsec3_addnsec3sx(db, ver, origin, zone_nsecttl(zone),
20574 					   false, zone->privatetype, diff));
20575 	}
20576 	CHECK(updatesecure(db, ver, origin, zone_nsecttl(zone), true, diff));
20577 
20578 failure:
20579 	return (result);
20580 }
20581 
20582 static void
dnssec_report(const char * format,...)20583 dnssec_report(const char *format, ...) {
20584 	va_list args;
20585 	va_start(args, format);
20586 	isc_log_vwrite(dns_lctx, DNS_LOGCATEGORY_DNSSEC, DNS_LOGMODULE_ZONE,
20587 		       ISC_LOG_INFO, format, args);
20588 	va_end(args);
20589 }
20590 
20591 static void
checkds_destroy(dns_checkds_t * checkds,bool locked)20592 checkds_destroy(dns_checkds_t *checkds, bool locked) {
20593 	isc_mem_t *mctx;
20594 
20595 	REQUIRE(DNS_CHECKDS_VALID(checkds));
20596 
20597 	dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3),
20598 		     "checkds: destroy DS query");
20599 
20600 	if (checkds->zone != NULL) {
20601 		if (!locked) {
20602 			LOCK_ZONE(checkds->zone);
20603 		}
20604 		REQUIRE(LOCKED_ZONE(checkds->zone));
20605 		if (ISC_LINK_LINKED(checkds, link)) {
20606 			ISC_LIST_UNLINK(checkds->zone->checkds_requests,
20607 					checkds, link);
20608 		}
20609 		if (!locked) {
20610 			UNLOCK_ZONE(checkds->zone);
20611 		}
20612 		if (locked) {
20613 			zone_idetach(&checkds->zone);
20614 		} else {
20615 			dns_zone_idetach(&checkds->zone);
20616 		}
20617 	}
20618 	if (checkds->request != NULL) {
20619 		dns_request_destroy(&checkds->request);
20620 	}
20621 	if (checkds->key != NULL) {
20622 		dns_tsigkey_detach(&checkds->key);
20623 	}
20624 	mctx = checkds->mctx;
20625 	isc_mem_put(checkds->mctx, checkds, sizeof(*checkds));
20626 	isc_mem_detach(&mctx);
20627 }
20628 
20629 static isc_result_t
make_dnskey(dst_key_t * key,unsigned char * buf,int bufsize,dns_rdata_t * target)20630 make_dnskey(dst_key_t *key, unsigned char *buf, int bufsize,
20631 	    dns_rdata_t *target) {
20632 	isc_result_t result;
20633 	isc_buffer_t b;
20634 	isc_region_t r;
20635 
20636 	isc_buffer_init(&b, buf, bufsize);
20637 	result = dst_key_todns(key, &b);
20638 	if (result != ISC_R_SUCCESS) {
20639 		return (result);
20640 	}
20641 
20642 	dns_rdata_reset(target);
20643 	isc_buffer_usedregion(&b, &r);
20644 	dns_rdata_fromregion(target, dst_key_class(key), dns_rdatatype_dnskey,
20645 			     &r);
20646 	return (ISC_R_SUCCESS);
20647 }
20648 
20649 static bool
do_checkds(dns_zone_t * zone,dst_key_t * key,isc_stdtime_t now,bool dspublish)20650 do_checkds(dns_zone_t *zone, dst_key_t *key, isc_stdtime_t now,
20651 	   bool dspublish) {
20652 	dns_kasp_t *kasp = dns_zone_getkasp(zone);
20653 	const char *dir = dns_zone_getkeydirectory(zone);
20654 	isc_result_t result;
20655 	uint32_t count = 0;
20656 
20657 	if (dspublish) {
20658 		(void)dst_key_getnum(key, DST_NUM_DSPUBCOUNT, &count);
20659 		count += 1;
20660 		dst_key_setnum(key, DST_NUM_DSPUBCOUNT, count);
20661 		dns_zone_log(zone, ISC_LOG_DEBUG(3),
20662 			     "checkds: %u DS published "
20663 			     "for key %u",
20664 			     count, dst_key_id(key));
20665 
20666 		if (count != zone->parentalscnt) {
20667 			return false;
20668 		}
20669 	} else {
20670 		(void)dst_key_getnum(key, DST_NUM_DSDELCOUNT, &count);
20671 		count += 1;
20672 		dst_key_setnum(key, DST_NUM_DSDELCOUNT, count);
20673 		dns_zone_log(zone, ISC_LOG_DEBUG(3),
20674 			     "checkds: %u DS withdrawn "
20675 			     "for key %u",
20676 			     count, dst_key_id(key));
20677 
20678 		if (count != zone->parentalscnt) {
20679 			return false;
20680 		}
20681 	}
20682 
20683 	dns_zone_log(zone, ISC_LOG_DEBUG(3),
20684 		     "checkds: checkds %s for key "
20685 		     "%u",
20686 		     dspublish ? "published" : "withdrawn", dst_key_id(key));
20687 
20688 	dns_zone_lock_keyfiles(zone);
20689 	result = dns_keymgr_checkds_id(kasp, &zone->checkds_ok, dir, now, now,
20690 				       dspublish, dst_key_id(key),
20691 				       dst_key_alg(key));
20692 	dns_zone_unlock_keyfiles(zone);
20693 
20694 	if (result != ISC_R_SUCCESS) {
20695 		dns_zone_log(zone, ISC_LOG_WARNING,
20696 			     "checkds: checkds for key %u failed: %s",
20697 			     dst_key_id(key), isc_result_totext(result));
20698 		return false;
20699 	}
20700 
20701 	return true;
20702 }
20703 
20704 static isc_result_t
validate_ds(dns_zone_t * zone,dns_message_t * message)20705 validate_ds(dns_zone_t *zone, dns_message_t *message) {
20706 	UNUSED(zone);
20707 	UNUSED(message);
20708 
20709 	/* Get closest trust anchor */
20710 
20711 	/* Check that trust anchor is (grand)parent of zone. */
20712 
20713 	/* Find the DNSKEY signing the message. */
20714 
20715 	/* Check that DNSKEY is in chain of trust. */
20716 
20717 	/* Validate DS RRset. */
20718 
20719 	return (ISC_R_SUCCESS);
20720 }
20721 
20722 static void
checkds_done(isc_task_t * task,isc_event_t * event)20723 checkds_done(isc_task_t *task, isc_event_t *event) {
20724 	char addrbuf[ISC_SOCKADDR_FORMATSIZE];
20725 	char rcode[128];
20726 	dns_checkds_t *checkds;
20727 	dns_zone_t *zone;
20728 	dns_db_t *db = NULL;
20729 	dns_dbversion_t *version = NULL;
20730 	dns_dnsseckey_t *key;
20731 	dns_dnsseckeylist_t keys;
20732 	dns_kasp_t *kasp = NULL;
20733 	dns_message_t *message = NULL;
20734 	dns_rdataset_t *ds_rrset = NULL;
20735 	dns_requestevent_t *revent = (dns_requestevent_t *)event;
20736 	isc_buffer_t buf;
20737 	isc_result_t result;
20738 	isc_stdtime_t now;
20739 	isc_time_t timenow;
20740 	bool rekey = false;
20741 	bool empty = false;
20742 
20743 	UNUSED(task);
20744 
20745 	checkds = event->ev_arg;
20746 	REQUIRE(DNS_CHECKDS_VALID(checkds));
20747 
20748 	zone = checkds->zone;
20749 	INSIST(task == zone->task);
20750 
20751 	ISC_LIST_INIT(keys);
20752 
20753 	kasp = zone->kasp;
20754 	INSIST(kasp != NULL);
20755 
20756 	isc_buffer_init(&buf, rcode, sizeof(rcode));
20757 	isc_sockaddr_format(&checkds->dst, addrbuf, sizeof(addrbuf));
20758 
20759 	dns_zone_log(zone, ISC_LOG_DEBUG(1), "checkds: DS query to %s: done",
20760 		     addrbuf);
20761 
20762 	dns_message_create(zone->mctx, DNS_MESSAGE_INTENTPARSE, &message);
20763 	INSIST(message != NULL);
20764 
20765 	CHECK(revent->result);
20766 	CHECK(dns_request_getresponse(revent->request, message,
20767 				      DNS_MESSAGEPARSE_PRESERVEORDER));
20768 	CHECK(dns_rcode_totext(message->rcode, &buf));
20769 
20770 	dns_zone_log(zone, ISC_LOG_DEBUG(3),
20771 		     "checkds: DS response from %s: %.*s", addrbuf,
20772 		     (int)buf.used, rcode);
20773 
20774 	/* Validate response. */
20775 	CHECK(validate_ds(zone, message));
20776 
20777 	if (message->rcode != dns_rcode_noerror) {
20778 		dns_zone_log(zone, ISC_LOG_NOTICE,
20779 			     "checkds: bad DS response from %s: %.*s", addrbuf,
20780 			     (int)buf.used, rcode);
20781 		goto failure;
20782 	}
20783 
20784 	/* Lookup DS RRset. */
20785 	result = dns_message_firstname(message, DNS_SECTION_ANSWER);
20786 	while (result == ISC_R_SUCCESS) {
20787 		dns_name_t *name = NULL;
20788 		dns_rdataset_t *rdataset;
20789 
20790 		dns_message_currentname(message, DNS_SECTION_ANSWER, &name);
20791 		if (dns_name_compare(&zone->origin, name) != 0) {
20792 			goto next;
20793 		}
20794 
20795 		for (rdataset = ISC_LIST_HEAD(name->list); rdataset != NULL;
20796 		     rdataset = ISC_LIST_NEXT(rdataset, link))
20797 		{
20798 			if (rdataset->type != dns_rdatatype_ds) {
20799 				goto next;
20800 			}
20801 
20802 			ds_rrset = rdataset;
20803 			break;
20804 		}
20805 
20806 		if (ds_rrset != NULL) {
20807 			break;
20808 		}
20809 
20810 	next:
20811 		result = dns_message_nextname(message, DNS_SECTION_ANSWER);
20812 	}
20813 
20814 	if (ds_rrset == NULL) {
20815 		empty = true;
20816 		dns_zone_log(zone, ISC_LOG_NOTICE,
20817 			     "checkds: empty DS response from %s", addrbuf);
20818 	}
20819 
20820 	TIME_NOW(&timenow);
20821 	now = isc_time_seconds(&timenow);
20822 
20823 	CHECK(dns_zone_getdb(zone, &db));
20824 	dns_db_currentversion(db, &version);
20825 
20826 	KASP_LOCK(kasp);
20827 	LOCK_ZONE(zone);
20828 	for (key = ISC_LIST_HEAD(zone->checkds_ok); key != NULL;
20829 	     key = ISC_LIST_NEXT(key, link))
20830 	{
20831 		bool alldone = false, found = false;
20832 		bool checkdspub = false, checkdsdel = false, ksk = false;
20833 		dst_key_state_t ds_state = DST_KEY_STATE_NA;
20834 		isc_stdtime_t published = 0, withdrawn = 0;
20835 		isc_result_t ret = ISC_R_SUCCESS;
20836 
20837 		/* Is this key have the KSK role? */
20838 		(void)dst_key_role(key->key, &ksk, NULL);
20839 		if (!ksk) {
20840 			continue;
20841 		}
20842 
20843 		/* Do we need to check the DS RRset for this key? */
20844 		(void)dst_key_getstate(key->key, DST_KEY_DS, &ds_state);
20845 		(void)dst_key_gettime(key->key, DST_TIME_DSPUBLISH, &published);
20846 		(void)dst_key_gettime(key->key, DST_TIME_DSDELETE, &withdrawn);
20847 
20848 		if (ds_state == DST_KEY_STATE_RUMOURED && published == 0) {
20849 			checkdspub = true;
20850 		} else if (ds_state == DST_KEY_STATE_UNRETENTIVE &&
20851 			   withdrawn == 0) {
20852 			checkdsdel = true;
20853 		}
20854 		if (!checkdspub && !checkdsdel) {
20855 			continue;
20856 		}
20857 
20858 		if (empty) {
20859 			goto dswithdrawn;
20860 		}
20861 
20862 		/* Find the appropriate DS record. */
20863 		ret = dns_rdataset_first(ds_rrset);
20864 		while (ret == ISC_R_SUCCESS) {
20865 			dns_rdata_ds_t ds;
20866 			dns_rdata_t dnskey = DNS_RDATA_INIT;
20867 			dns_rdata_t dsrdata = DNS_RDATA_INIT;
20868 			dns_rdata_t rdata = DNS_RDATA_INIT;
20869 			isc_result_t r;
20870 			unsigned char dsbuf[DNS_DS_BUFFERSIZE];
20871 			unsigned char keybuf[DST_KEY_MAXSIZE];
20872 
20873 			dns_rdataset_current(ds_rrset, &rdata);
20874 			r = dns_rdata_tostruct(&rdata, &ds, NULL);
20875 			if (r != ISC_R_SUCCESS) {
20876 				goto nextds;
20877 			}
20878 			/* Check key tag and algorithm. */
20879 			if (dst_key_id(key->key) != ds.key_tag) {
20880 				goto nextds;
20881 			}
20882 			if (dst_key_alg(key->key) != ds.algorithm) {
20883 				goto nextds;
20884 			}
20885 			/* Derive DS from DNSKEY, see if the rdata is equal. */
20886 			make_dnskey(key->key, keybuf, sizeof(keybuf), &dnskey);
20887 			r = dns_ds_buildrdata(&zone->origin, &dnskey,
20888 					      ds.digest_type, dsbuf, &dsrdata);
20889 			if (r != ISC_R_SUCCESS) {
20890 				goto nextds;
20891 			}
20892 			if (dns_rdata_compare(&rdata, &dsrdata) == 0) {
20893 				found = true;
20894 				if (checkdspub) {
20895 					/* DS Published. */
20896 					alldone = do_checkds(zone, key->key,
20897 							     now, true);
20898 					if (alldone) {
20899 						rekey = true;
20900 					}
20901 				}
20902 			}
20903 
20904 		nextds:
20905 			ret = dns_rdataset_next(ds_rrset);
20906 		}
20907 
20908 	dswithdrawn:
20909 		/* DS withdrawn. */
20910 		if (checkdsdel && !found) {
20911 			alldone = do_checkds(zone, key->key, now, false);
20912 			if (alldone) {
20913 				rekey = true;
20914 			}
20915 		}
20916 	}
20917 	UNLOCK_ZONE(zone);
20918 	KASP_UNLOCK(kasp);
20919 
20920 	/* Rekey after checkds. */
20921 	if (rekey) {
20922 		dns_zone_rekey(zone, false);
20923 	}
20924 
20925 failure:
20926 	if (result != ISC_R_SUCCESS) {
20927 		dns_zone_log(zone, ISC_LOG_DEBUG(3),
20928 			     "checkds: DS request failed: %s",
20929 			     isc_result_totext(result));
20930 	}
20931 
20932 	if (version != NULL) {
20933 		dns_db_closeversion(db, &version, false);
20934 	}
20935 	if (db != NULL) {
20936 		dns_db_detach(&db);
20937 	}
20938 
20939 	while (!ISC_LIST_EMPTY(keys)) {
20940 		key = ISC_LIST_HEAD(keys);
20941 		ISC_LIST_UNLINK(keys, key, link);
20942 		dns_dnsseckey_destroy(dns_zone_getmctx(zone), &key);
20943 	}
20944 
20945 	isc_event_free(&event);
20946 	checkds_destroy(checkds, false);
20947 	dns_message_detach(&message);
20948 }
20949 
20950 static bool
checkds_isqueued(dns_zone_t * zone,isc_sockaddr_t * addr,dns_tsigkey_t * key)20951 checkds_isqueued(dns_zone_t *zone, isc_sockaddr_t *addr, dns_tsigkey_t *key) {
20952 	dns_checkds_t *checkds;
20953 
20954 	for (checkds = ISC_LIST_HEAD(zone->checkds_requests); checkds != NULL;
20955 	     checkds = ISC_LIST_NEXT(checkds, link))
20956 	{
20957 		if (checkds->request != NULL) {
20958 			continue;
20959 		}
20960 		if (addr != NULL && isc_sockaddr_equal(addr, &checkds->dst) &&
20961 		    checkds->key == key)
20962 		{
20963 			return (true);
20964 		}
20965 	}
20966 	return (false);
20967 }
20968 
20969 static isc_result_t
checkds_create(isc_mem_t * mctx,unsigned int flags,dns_checkds_t ** checkdsp)20970 checkds_create(isc_mem_t *mctx, unsigned int flags, dns_checkds_t **checkdsp) {
20971 	dns_checkds_t *checkds;
20972 
20973 	REQUIRE(checkdsp != NULL && *checkdsp == NULL);
20974 
20975 	checkds = isc_mem_get(mctx, sizeof(*checkds));
20976 	*checkds = (dns_checkds_t){
20977 		.flags = flags,
20978 	};
20979 
20980 	isc_mem_attach(mctx, &checkds->mctx);
20981 	isc_sockaddr_any(&checkds->dst);
20982 	ISC_LINK_INIT(checkds, link);
20983 	checkds->magic = CHECKDS_MAGIC;
20984 	*checkdsp = checkds;
20985 	return (ISC_R_SUCCESS);
20986 }
20987 
20988 static isc_result_t
checkds_createmessage(dns_zone_t * zone,dns_message_t ** messagep)20989 checkds_createmessage(dns_zone_t *zone, dns_message_t **messagep) {
20990 	dns_message_t *message = NULL;
20991 
20992 	dns_name_t *tempname = NULL;
20993 	dns_rdataset_t *temprdataset = NULL;
20994 
20995 	isc_result_t result;
20996 
20997 	REQUIRE(DNS_ZONE_VALID(zone));
20998 	REQUIRE(messagep != NULL && *messagep == NULL);
20999 
21000 	dns_message_create(zone->mctx, DNS_MESSAGE_INTENTRENDER, &message);
21001 
21002 	message->opcode = dns_opcode_query;
21003 	message->rdclass = zone->rdclass;
21004 
21005 	result = dns_message_gettempname(message, &tempname);
21006 	if (result != ISC_R_SUCCESS) {
21007 		goto cleanup;
21008 	}
21009 
21010 	result = dns_message_gettemprdataset(message, &temprdataset);
21011 	if (result != ISC_R_SUCCESS) {
21012 		goto cleanup;
21013 	}
21014 
21015 	/*
21016 	 * Make question.
21017 	 */
21018 	dns_name_init(tempname, NULL);
21019 	dns_name_clone(&zone->origin, tempname);
21020 	dns_rdataset_makequestion(temprdataset, zone->rdclass,
21021 				  dns_rdatatype_ds);
21022 	ISC_LIST_APPEND(tempname->list, temprdataset, link);
21023 	dns_message_addname(message, tempname, DNS_SECTION_QUESTION);
21024 	tempname = NULL;
21025 	temprdataset = NULL;
21026 
21027 	*messagep = message;
21028 	return (ISC_R_SUCCESS);
21029 
21030 cleanup:
21031 	if (tempname != NULL) {
21032 		dns_message_puttempname(message, &tempname);
21033 	}
21034 	if (temprdataset != NULL) {
21035 		dns_message_puttemprdataset(message, &temprdataset);
21036 	}
21037 	dns_message_detach(&message);
21038 	return (result);
21039 }
21040 
21041 static void
checkds_send_toaddr(isc_task_t * task,isc_event_t * event)21042 checkds_send_toaddr(isc_task_t *task, isc_event_t *event) {
21043 	dns_checkds_t *checkds;
21044 	isc_result_t result;
21045 	dns_message_t *message = NULL;
21046 	isc_netaddr_t dstip;
21047 	dns_tsigkey_t *key = NULL;
21048 	char addrbuf[ISC_SOCKADDR_FORMATSIZE];
21049 	isc_sockaddr_t src;
21050 	unsigned int options, timeout;
21051 	bool have_checkdssource = false;
21052 	bool have_checkdsdscp = false;
21053 	isc_dscp_t dscp = -1;
21054 
21055 	checkds = event->ev_arg;
21056 	REQUIRE(DNS_CHECKDS_VALID(checkds));
21057 
21058 	UNUSED(task);
21059 
21060 	LOCK_ZONE(checkds->zone);
21061 
21062 	checkds->event = NULL;
21063 
21064 	if (DNS_ZONE_FLAG(checkds->zone, DNS_ZONEFLG_LOADED) == 0) {
21065 		result = ISC_R_CANCELED;
21066 		goto cleanup;
21067 	}
21068 
21069 	if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0 ||
21070 	    DNS_ZONE_FLAG(checkds->zone, DNS_ZONEFLG_EXITING) ||
21071 	    checkds->zone->view->requestmgr == NULL ||
21072 	    checkds->zone->db == NULL)
21073 	{
21074 		result = ISC_R_CANCELED;
21075 		goto cleanup;
21076 	}
21077 
21078 	/*
21079 	 * The raw IPv4 address should also exist.  Don't send to the
21080 	 * mapped form.
21081 	 */
21082 	if (isc_sockaddr_pf(&checkds->dst) == PF_INET6 &&
21083 	    IN6_IS_ADDR_V4MAPPED(&checkds->dst.type.sin6.sin6_addr))
21084 	{
21085 		isc_sockaddr_format(&checkds->dst, addrbuf, sizeof(addrbuf));
21086 		dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3),
21087 			     "checkds: ignoring IPv6 mapped IPV4 address: %s",
21088 			     addrbuf);
21089 		result = ISC_R_CANCELED;
21090 		goto cleanup;
21091 	}
21092 
21093 	result = checkds_createmessage(checkds->zone, &message);
21094 	if (result != ISC_R_SUCCESS) {
21095 		goto cleanup;
21096 	}
21097 
21098 	isc_sockaddr_format(&checkds->dst, addrbuf, sizeof(addrbuf));
21099 	if (checkds->key != NULL) {
21100 		/* Transfer ownership of key */
21101 		key = checkds->key;
21102 		checkds->key = NULL;
21103 	} else {
21104 		isc_netaddr_fromsockaddr(&dstip, &checkds->dst);
21105 		result = dns_view_getpeertsig(checkds->zone->view, &dstip,
21106 					      &key);
21107 		if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
21108 			dns_zone_log(checkds->zone, ISC_LOG_ERROR,
21109 				     "checkds: DS query to %s not sent. "
21110 				     "Peer TSIG key lookup failure.",
21111 				     addrbuf);
21112 			goto cleanup_message;
21113 		}
21114 	}
21115 
21116 	if (key != NULL) {
21117 		char namebuf[DNS_NAME_FORMATSIZE];
21118 
21119 		dns_name_format(&key->name, namebuf, sizeof(namebuf));
21120 		dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3),
21121 			     "checkds: sending DS query to %s : TSIG (%s)",
21122 			     addrbuf, namebuf);
21123 	} else {
21124 		dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3),
21125 			     "checkds: sending DS query to %s", addrbuf);
21126 	}
21127 	options = 0;
21128 	if (checkds->zone->view->peers != NULL) {
21129 		dns_peer_t *peer = NULL;
21130 		bool usetcp = false;
21131 		result = dns_peerlist_peerbyaddr(checkds->zone->view->peers,
21132 						 &dstip, &peer);
21133 		if (result == ISC_R_SUCCESS) {
21134 			result = dns_peer_getquerysource(peer, &src);
21135 			if (result == ISC_R_SUCCESS) {
21136 				have_checkdssource = true;
21137 			}
21138 			dns_peer_getquerydscp(peer, &dscp);
21139 			if (dscp != -1) {
21140 				have_checkdsdscp = true;
21141 			}
21142 			result = dns_peer_getforcetcp(peer, &usetcp);
21143 			if (result == ISC_R_SUCCESS && usetcp) {
21144 				options |= DNS_FETCHOPT_TCP;
21145 			}
21146 		}
21147 	}
21148 	switch (isc_sockaddr_pf(&checkds->dst)) {
21149 	case PF_INET:
21150 		if (!have_checkdssource) {
21151 			src = checkds->zone->parentalsrc4;
21152 		}
21153 		if (!have_checkdsdscp) {
21154 			dscp = checkds->zone->parentalsrc4dscp;
21155 		}
21156 		break;
21157 	case PF_INET6:
21158 		if (!have_checkdssource) {
21159 			src = checkds->zone->parentalsrc6;
21160 		}
21161 		if (!have_checkdsdscp) {
21162 			dscp = checkds->zone->parentalsrc6dscp;
21163 		}
21164 		break;
21165 	default:
21166 		result = ISC_R_NOTIMPLEMENTED;
21167 		goto cleanup_key;
21168 	}
21169 
21170 	dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3),
21171 		     "checkds: create request for DS query to %s", addrbuf);
21172 
21173 	timeout = 15;
21174 	options |= DNS_REQUESTOPT_TCP;
21175 	result = dns_request_createvia(
21176 		checkds->zone->view->requestmgr, message, &src, &checkds->dst,
21177 		dscp, options, key, timeout * 3, timeout, 0,
21178 		checkds->zone->task, checkds_done, checkds, &checkds->request);
21179 	if (result != ISC_R_SUCCESS) {
21180 		dns_zone_log(
21181 			checkds->zone, ISC_LOG_DEBUG(3),
21182 			"checkds: dns_request_createvia() to %s failed: %s",
21183 			addrbuf, dns_result_totext(result));
21184 	}
21185 
21186 cleanup_key:
21187 	if (key != NULL) {
21188 		dns_tsigkey_detach(&key);
21189 	}
21190 cleanup_message:
21191 	dns_message_detach(&message);
21192 cleanup:
21193 	UNLOCK_ZONE(checkds->zone);
21194 	isc_event_free(&event);
21195 	if (result != ISC_R_SUCCESS) {
21196 		checkds_destroy(checkds, false);
21197 	}
21198 }
21199 
21200 static isc_result_t
checkds_send_queue(dns_checkds_t * checkds)21201 checkds_send_queue(dns_checkds_t *checkds) {
21202 	isc_event_t *e;
21203 	isc_result_t result;
21204 
21205 	INSIST(checkds->event == NULL);
21206 	e = isc_event_allocate(checkds->mctx, NULL, DNS_EVENT_CHECKDSSENDTOADDR,
21207 			       checkds_send_toaddr, checkds,
21208 			       sizeof(isc_event_t));
21209 	e->ev_arg = checkds;
21210 	e->ev_sender = NULL;
21211 	result = isc_ratelimiter_enqueue(checkds->zone->zmgr->checkdsrl,
21212 					 checkds->zone->task, &e);
21213 	if (result != ISC_R_SUCCESS) {
21214 		isc_event_free(&e);
21215 		checkds->event = NULL;
21216 	}
21217 	return (result);
21218 }
21219 
21220 static void
checkds_send(dns_zone_t * zone)21221 checkds_send(dns_zone_t *zone) {
21222 	dns_view_t *view = dns_zone_getview(zone);
21223 	isc_result_t result;
21224 	unsigned int flags = 0;
21225 
21226 	/*
21227 	 * Zone lock held by caller.
21228 	 */
21229 	REQUIRE(LOCKED_ZONE(zone));
21230 
21231 	dns_zone_log(zone, ISC_LOG_DEBUG(3),
21232 		     "checkds: start sending DS queries to %u parentals",
21233 		     zone->parentalscnt);
21234 
21235 	if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) {
21236 		dns_zone_log(zone, ISC_LOG_DEBUG(3),
21237 			     "checkds: abort, named exiting");
21238 		return;
21239 	}
21240 
21241 	for (unsigned int i = 0; i < zone->parentalscnt; i++) {
21242 		dns_tsigkey_t *key = NULL;
21243 		isc_sockaddr_t dst;
21244 		dns_checkds_t *checkds = NULL;
21245 
21246 		if ((zone->parentalkeynames != NULL) &&
21247 		    (zone->parentalkeynames[i] != NULL)) {
21248 			dns_name_t *keyname = zone->parentalkeynames[i];
21249 			(void)dns_view_gettsig(view, keyname, &key);
21250 		}
21251 
21252 		dst = zone->parentals[i];
21253 
21254 		if (checkds_isqueued(zone, &dst, key)) {
21255 			dns_zone_log(zone, ISC_LOG_DEBUG(3),
21256 				     "checkds: DS query to parent "
21257 				     "%d is queued",
21258 				     i);
21259 			if (key != NULL) {
21260 				dns_tsigkey_detach(&key);
21261 			}
21262 			continue;
21263 		}
21264 
21265 		dns_zone_log(zone, ISC_LOG_DEBUG(3),
21266 			     "checkds: create DS query for "
21267 			     "parent %d",
21268 			     i);
21269 
21270 		result = checkds_create(zone->mctx, flags, &checkds);
21271 		if (result != ISC_R_SUCCESS) {
21272 			dns_zone_log(zone, ISC_LOG_DEBUG(3),
21273 				     "checkds: create DS query for "
21274 				     "parent %d failed",
21275 				     i);
21276 			continue;
21277 		}
21278 		zone_iattach(zone, &checkds->zone);
21279 		checkds->dst = dst;
21280 
21281 		INSIST(checkds->key == NULL);
21282 		if (key != NULL) {
21283 			checkds->key = key;
21284 			key = NULL;
21285 		}
21286 
21287 		ISC_LIST_APPEND(zone->checkds_requests, checkds, link);
21288 		result = checkds_send_queue(checkds);
21289 		if (result != ISC_R_SUCCESS) {
21290 			dns_zone_log(zone, ISC_LOG_DEBUG(3),
21291 				     "checkds: send DS query to "
21292 				     "parent %d failed",
21293 				     i);
21294 			checkds_destroy(checkds, true);
21295 		}
21296 	}
21297 }
21298 
21299 static void
zone_checkds(dns_zone_t * zone)21300 zone_checkds(dns_zone_t *zone) {
21301 	bool cdscheck = false;
21302 
21303 	for (dns_dnsseckey_t *key = ISC_LIST_HEAD(zone->checkds_ok);
21304 	     key != NULL; key = ISC_LIST_NEXT(key, link))
21305 	{
21306 		dst_key_state_t ds_state = DST_KEY_STATE_NA;
21307 		bool ksk = false;
21308 		isc_stdtime_t published = 0, withdrawn = 0;
21309 
21310 		/* Is this key have the KSK role? */
21311 		(void)dst_key_role(key->key, &ksk, NULL);
21312 		if (!ksk) {
21313 			continue;
21314 		}
21315 
21316 		/* Do we need to check the DS RRset? */
21317 		(void)dst_key_getstate(key->key, DST_KEY_DS, &ds_state);
21318 		(void)dst_key_gettime(key->key, DST_TIME_DSPUBLISH, &published);
21319 		(void)dst_key_gettime(key->key, DST_TIME_DSDELETE, &withdrawn);
21320 
21321 		if (ds_state == DST_KEY_STATE_RUMOURED && published == 0) {
21322 			dst_key_setnum(key->key, DST_NUM_DSPUBCOUNT, 0);
21323 			cdscheck = true;
21324 		} else if (ds_state == DST_KEY_STATE_UNRETENTIVE &&
21325 			   withdrawn == 0) {
21326 			dst_key_setnum(key->key, DST_NUM_DSDELCOUNT, 0);
21327 			cdscheck = true;
21328 		}
21329 	}
21330 
21331 	if (cdscheck) {
21332 		/* Request the DS RRset. */
21333 		LOCK_ZONE(zone);
21334 		checkds_send(zone);
21335 		UNLOCK_ZONE(zone);
21336 	}
21337 }
21338 
21339 static void
zone_rekey(dns_zone_t * zone)21340 zone_rekey(dns_zone_t *zone) {
21341 	isc_result_t result;
21342 	dns_db_t *db = NULL;
21343 	dns_dbnode_t *node = NULL;
21344 	dns_dbversion_t *ver = NULL;
21345 	dns_rdataset_t cdsset, soaset, soasigs, keyset, keysigs, cdnskeyset;
21346 	dns_dnsseckeylist_t dnskeys, keys, rmkeys;
21347 	dns_dnsseckey_t *key = NULL;
21348 	dns_diff_t diff, _sig_diff;
21349 	dns_kasp_t *kasp;
21350 	dns__zonediff_t zonediff;
21351 	bool commit = false, newactive = false;
21352 	bool newalg = false;
21353 	bool fullsign;
21354 	dns_ttl_t ttl = 3600;
21355 	const char *dir = NULL;
21356 	isc_mem_t *mctx = NULL;
21357 	isc_stdtime_t now, nexttime = 0;
21358 	isc_time_t timenow;
21359 	isc_interval_t ival;
21360 	char timebuf[80];
21361 
21362 	REQUIRE(DNS_ZONE_VALID(zone));
21363 
21364 	ISC_LIST_INIT(dnskeys);
21365 	ISC_LIST_INIT(keys);
21366 	ISC_LIST_INIT(rmkeys);
21367 	dns_rdataset_init(&soaset);
21368 	dns_rdataset_init(&soasigs);
21369 	dns_rdataset_init(&keyset);
21370 	dns_rdataset_init(&keysigs);
21371 	dns_rdataset_init(&cdsset);
21372 	dns_rdataset_init(&cdnskeyset);
21373 	dir = dns_zone_getkeydirectory(zone);
21374 	mctx = zone->mctx;
21375 	dns_diff_init(mctx, &diff);
21376 	dns_diff_init(mctx, &_sig_diff);
21377 	zonediff_init(&zonediff, &_sig_diff);
21378 
21379 	CHECK(dns_zone_getdb(zone, &db));
21380 	CHECK(dns_db_newversion(db, &ver));
21381 	CHECK(dns_db_getoriginnode(db, &node));
21382 
21383 	TIME_NOW(&timenow);
21384 	now = isc_time_seconds(&timenow);
21385 
21386 	kasp = dns_zone_getkasp(zone);
21387 
21388 	dnssec_log(zone, ISC_LOG_INFO, "reconfiguring zone keys");
21389 
21390 	/* Get the SOA record's TTL */
21391 	CHECK(dns_db_findrdataset(db, node, ver, dns_rdatatype_soa,
21392 				  dns_rdatatype_none, 0, &soaset, &soasigs));
21393 	ttl = soaset.ttl;
21394 	dns_rdataset_disassociate(&soaset);
21395 
21396 	/* Get the DNSKEY rdataset */
21397 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey,
21398 				     dns_rdatatype_none, 0, &keyset, &keysigs);
21399 	if (result == ISC_R_SUCCESS) {
21400 		ttl = keyset.ttl;
21401 
21402 		dns_zone_lock_keyfiles(zone);
21403 
21404 		result = dns_dnssec_keylistfromrdataset(
21405 			&zone->origin, dir, mctx, &keyset, &keysigs, &soasigs,
21406 			false, false, &dnskeys);
21407 
21408 		dns_zone_unlock_keyfiles(zone);
21409 
21410 		if (result != ISC_R_SUCCESS) {
21411 			goto failure;
21412 		}
21413 	} else if (result != ISC_R_NOTFOUND) {
21414 		goto failure;
21415 	}
21416 
21417 	/* Get the CDS rdataset */
21418 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_cds,
21419 				     dns_rdatatype_none, 0, &cdsset, NULL);
21420 	if (result != ISC_R_SUCCESS && dns_rdataset_isassociated(&cdsset)) {
21421 		dns_rdataset_disassociate(&cdsset);
21422 	}
21423 
21424 	/* Get the CDNSKEY rdataset */
21425 	result = dns_db_findrdataset(db, node, ver, dns_rdatatype_cdnskey,
21426 				     dns_rdatatype_none, 0, &cdnskeyset, NULL);
21427 	if (result != ISC_R_SUCCESS && dns_rdataset_isassociated(&cdnskeyset)) {
21428 		dns_rdataset_disassociate(&cdnskeyset);
21429 	}
21430 
21431 	/*
21432 	 * True when called from "rndc sign".  Indicates the zone should be
21433 	 * fully signed now.
21434 	 */
21435 	fullsign = DNS_ZONEKEY_OPTION(zone, DNS_ZONEKEY_FULLSIGN);
21436 
21437 	KASP_LOCK(kasp);
21438 
21439 	dns_zone_lock_keyfiles(zone);
21440 	result = dns_dnssec_findmatchingkeys(&zone->origin, dir, now, mctx,
21441 					     &keys);
21442 	dns_zone_unlock_keyfiles(zone);
21443 
21444 	if (result != ISC_R_SUCCESS) {
21445 		dnssec_log(zone, ISC_LOG_DEBUG(1),
21446 			   "zone_rekey:dns_dnssec_findmatchingkeys failed: %s",
21447 			   isc_result_totext(result));
21448 	}
21449 
21450 	if (kasp != NULL) {
21451 		/*
21452 		 * Check DS at parental agents. Clear ongoing checks.
21453 		 */
21454 		LOCK_ZONE(zone);
21455 		checkds_cancel(zone);
21456 		clear_keylist(&zone->checkds_ok, zone->mctx);
21457 		ISC_LIST_INIT(zone->checkds_ok);
21458 		UNLOCK_ZONE(zone);
21459 
21460 		result = dns_zone_getdnsseckeys(zone, db, ver, now,
21461 						&zone->checkds_ok);
21462 
21463 		if (result != ISC_R_SUCCESS) {
21464 			dnssec_log(zone, ISC_LOG_ERROR,
21465 				   "zone_rekey:dns_zone_getdnsseckeys failed: "
21466 				   "%s",
21467 				   isc_result_totext(result));
21468 		} else {
21469 			zone_checkds(zone);
21470 		}
21471 
21472 		if (result == ISC_R_SUCCESS || result == ISC_R_NOTFOUND) {
21473 			dns_zone_lock_keyfiles(zone);
21474 			result = dns_keymgr_run(&zone->origin, zone->rdclass,
21475 						dir, mctx, &keys, &dnskeys,
21476 						kasp, now, &nexttime);
21477 			dns_zone_unlock_keyfiles(zone);
21478 
21479 			if (result != ISC_R_SUCCESS) {
21480 				dnssec_log(zone, ISC_LOG_ERROR,
21481 					   "zone_rekey:dns_dnssec_keymgr "
21482 					   "failed: %s",
21483 					   isc_result_totext(result));
21484 				KASP_UNLOCK(kasp);
21485 				goto failure;
21486 			}
21487 		}
21488 	}
21489 
21490 	KASP_UNLOCK(kasp);
21491 
21492 	if (result == ISC_R_SUCCESS) {
21493 		bool cds_delete = false;
21494 		isc_stdtime_t when;
21495 
21496 		/*
21497 		 * Publish CDS/CDNSKEY DELETE records if the zone is
21498 		 * transitioning from secure to insecure.
21499 		 */
21500 		if (kasp != NULL &&
21501 		    strcmp(dns_kasp_getname(kasp), "insecure") == 0) {
21502 			cds_delete = true;
21503 		}
21504 
21505 		/*
21506 		 * Only update DNSKEY TTL if we have a policy.
21507 		 */
21508 		if (kasp != NULL) {
21509 			ttl = dns_kasp_dnskeyttl(kasp);
21510 		}
21511 
21512 		result = dns_dnssec_updatekeys(&dnskeys, &keys, &rmkeys,
21513 					       &zone->origin, ttl, &diff, mctx,
21514 					       dnssec_report);
21515 		/*
21516 		 * Keys couldn't be updated for some reason;
21517 		 * try again later.
21518 		 */
21519 		if (result != ISC_R_SUCCESS) {
21520 			dnssec_log(zone, ISC_LOG_ERROR,
21521 				   "zone_rekey:couldn't update zone keys: %s",
21522 				   isc_result_totext(result));
21523 			goto failure;
21524 		}
21525 
21526 		/*
21527 		 * Update CDS / CDNSKEY records.
21528 		 */
21529 		result = dns_dnssec_syncupdate(&dnskeys, &rmkeys, &cdsset,
21530 					       &cdnskeyset, now, ttl, &diff,
21531 					       mctx);
21532 		if (result != ISC_R_SUCCESS) {
21533 			dnssec_log(zone, ISC_LOG_ERROR,
21534 				   "zone_rekey:couldn't update CDS/CDNSKEY: %s",
21535 				   isc_result_totext(result));
21536 			goto failure;
21537 		}
21538 
21539 		if (cds_delete) {
21540 			/*
21541 			 * Only publish CDS/CDNSKEY DELETE records if there is
21542 			 * a KSK that can be used to verify the RRset. This
21543 			 * means there must be a key with the KSK role that is
21544 			 * published and is used for signing.
21545 			 */
21546 			cds_delete = false;
21547 			for (key = ISC_LIST_HEAD(dnskeys); key != NULL;
21548 			     key = ISC_LIST_NEXT(key, link)) {
21549 				dst_key_t *dstk = key->key;
21550 				bool ksk = false;
21551 				(void)dst_key_getbool(dstk, DST_BOOL_KSK, &ksk);
21552 				if (!ksk) {
21553 					continue;
21554 				}
21555 
21556 				if (dst_key_haskasp(dstk) &&
21557 				    dst_key_is_published(dstk, now, &when) &&
21558 				    dst_key_is_signing(dstk, DST_BOOL_KSK, now,
21559 						       &when))
21560 				{
21561 					cds_delete = true;
21562 					break;
21563 				}
21564 			}
21565 		}
21566 		result = dns_dnssec_syncdelete(&cdsset, &cdnskeyset,
21567 					       &zone->origin, zone->rdclass,
21568 					       ttl, &diff, mctx, cds_delete);
21569 		if (result != ISC_R_SUCCESS) {
21570 			dnssec_log(zone, ISC_LOG_ERROR,
21571 				   "zone_rekey:couldn't update CDS/CDNSKEY "
21572 				   "DELETE records: %s",
21573 				   isc_result_totext(result));
21574 			goto failure;
21575 		}
21576 
21577 		/*
21578 		 * See if any pre-existing keys have newly become active;
21579 		 * also, see if any new key is for a new algorithm, as in that
21580 		 * event, we need to sign the zone fully.  (If there's a new
21581 		 * key, but it's for an already-existing algorithm, then
21582 		 * the zone signing can be handled incrementally.)
21583 		 */
21584 		for (key = ISC_LIST_HEAD(dnskeys); key != NULL;
21585 		     key = ISC_LIST_NEXT(key, link)) {
21586 			if (!key->first_sign) {
21587 				continue;
21588 			}
21589 
21590 			newactive = true;
21591 
21592 			if (!dns_rdataset_isassociated(&keysigs)) {
21593 				newalg = true;
21594 				break;
21595 			}
21596 
21597 			if (signed_with_alg(&keysigs, dst_key_alg(key->key))) {
21598 				/*
21599 				 * This isn't a new algorithm; clear
21600 				 * first_sign so we won't sign the
21601 				 * whole zone with this key later.
21602 				 */
21603 				key->first_sign = false;
21604 			} else {
21605 				newalg = true;
21606 				break;
21607 			}
21608 		}
21609 
21610 		if ((newactive || fullsign || !ISC_LIST_EMPTY(diff.tuples)) &&
21611 		    dnskey_sane(zone, db, ver, &diff))
21612 		{
21613 			CHECK(dns_diff_apply(&diff, db, ver));
21614 			CHECK(clean_nsec3param(zone, db, ver, &diff));
21615 			CHECK(add_signing_records(db, zone->privatetype, ver,
21616 						  &diff, (newalg || fullsign)));
21617 			CHECK(update_soa_serial(zone, db, ver, &diff, mctx,
21618 						zone->updatemethod));
21619 			CHECK(add_chains(zone, db, ver, &diff));
21620 			CHECK(sign_apex(zone, db, ver, now, &diff, &zonediff));
21621 			CHECK(zone_journal(zone, zonediff.diff, NULL,
21622 					   "zone_rekey"));
21623 			commit = true;
21624 		}
21625 	}
21626 
21627 	dns_db_closeversion(db, &ver, true);
21628 
21629 	LOCK_ZONE(zone);
21630 
21631 	if (commit) {
21632 		dns_difftuple_t *tuple;
21633 		dns_stats_t *dnssecsignstats =
21634 			dns_zone_getdnssecsignstats(zone);
21635 
21636 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY);
21637 
21638 		zone_needdump(zone, DNS_DUMP_DELAY);
21639 
21640 		zone_settimer(zone, &timenow);
21641 
21642 		/* Remove any signatures from removed keys.  */
21643 		if (!ISC_LIST_EMPTY(rmkeys)) {
21644 			for (key = ISC_LIST_HEAD(rmkeys); key != NULL;
21645 			     key = ISC_LIST_NEXT(key, link)) {
21646 				result = zone_signwithkey(
21647 					zone, dst_key_alg(key->key),
21648 					dst_key_id(key->key), true);
21649 				if (result != ISC_R_SUCCESS) {
21650 					dnssec_log(zone, ISC_LOG_ERROR,
21651 						   "zone_signwithkey failed: "
21652 						   "%s",
21653 						   dns_result_totext(result));
21654 				}
21655 
21656 				/* Clear DNSSEC sign statistics. */
21657 				if (dnssecsignstats != NULL) {
21658 					dns_dnssecsignstats_clear(
21659 						dnssecsignstats,
21660 						dst_key_id(key->key),
21661 						dst_key_alg(key->key));
21662 					/*
21663 					 * Also clear the dnssec-sign
21664 					 * statistics of the revoked key id.
21665 					 */
21666 					dns_dnssecsignstats_clear(
21667 						dnssecsignstats,
21668 						dst_key_rid(key->key),
21669 						dst_key_alg(key->key));
21670 				}
21671 			}
21672 		}
21673 
21674 		if (fullsign) {
21675 			/*
21676 			 * "rndc sign" was called, so we now sign the zone
21677 			 * with all active keys, whether they're new or not.
21678 			 */
21679 			for (key = ISC_LIST_HEAD(dnskeys); key != NULL;
21680 			     key = ISC_LIST_NEXT(key, link)) {
21681 				if (!key->force_sign && !key->hint_sign) {
21682 					continue;
21683 				}
21684 
21685 				result = zone_signwithkey(
21686 					zone, dst_key_alg(key->key),
21687 					dst_key_id(key->key), false);
21688 				if (result != ISC_R_SUCCESS) {
21689 					dnssec_log(zone, ISC_LOG_ERROR,
21690 						   "zone_signwithkey failed: "
21691 						   "%s",
21692 						   dns_result_totext(result));
21693 				}
21694 			}
21695 		} else if (newalg) {
21696 			/*
21697 			 * We haven't been told to sign fully, but a new
21698 			 * algorithm was added to the DNSKEY.  We sign
21699 			 * the full zone, but only with newly active
21700 			 * keys.
21701 			 */
21702 			for (key = ISC_LIST_HEAD(dnskeys); key != NULL;
21703 			     key = ISC_LIST_NEXT(key, link)) {
21704 				if (!key->first_sign) {
21705 					continue;
21706 				}
21707 
21708 				result = zone_signwithkey(
21709 					zone, dst_key_alg(key->key),
21710 					dst_key_id(key->key), false);
21711 				if (result != ISC_R_SUCCESS) {
21712 					dnssec_log(zone, ISC_LOG_ERROR,
21713 						   "zone_signwithkey failed: "
21714 						   "%s",
21715 						   dns_result_totext(result));
21716 				}
21717 			}
21718 		}
21719 
21720 		/*
21721 		 * Clear fullsign flag, if it was set, so we don't do
21722 		 * another full signing next time.
21723 		 */
21724 		DNS_ZONEKEY_CLROPTION(zone, DNS_ZONEKEY_FULLSIGN);
21725 
21726 		/*
21727 		 * Cause the zone to add/delete NSEC3 chains for the
21728 		 * deferred NSEC3PARAM changes.
21729 		 */
21730 		for (tuple = ISC_LIST_HEAD(zonediff.diff->tuples);
21731 		     tuple != NULL; tuple = ISC_LIST_NEXT(tuple, link))
21732 		{
21733 			unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE];
21734 			dns_rdata_t rdata = DNS_RDATA_INIT;
21735 			dns_rdata_nsec3param_t nsec3param;
21736 
21737 			if (tuple->rdata.type != zone->privatetype ||
21738 			    tuple->op != DNS_DIFFOP_ADD) {
21739 				continue;
21740 			}
21741 
21742 			if (!dns_nsec3param_fromprivate(&tuple->rdata, &rdata,
21743 							buf, sizeof(buf))) {
21744 				continue;
21745 			}
21746 
21747 			result = dns_rdata_tostruct(&rdata, &nsec3param, NULL);
21748 			RUNTIME_CHECK(result == ISC_R_SUCCESS);
21749 			if (nsec3param.flags == 0) {
21750 				continue;
21751 			}
21752 
21753 			result = zone_addnsec3chain(zone, &nsec3param);
21754 			if (result != ISC_R_SUCCESS) {
21755 				dnssec_log(zone, ISC_LOG_ERROR,
21756 					   "zone_addnsec3chain failed: %s",
21757 					   dns_result_totext(result));
21758 			}
21759 		}
21760 
21761 		/*
21762 		 * Activate any NSEC3 chain updates that may have
21763 		 * been scheduled before this rekey.
21764 		 */
21765 		if (fullsign || newalg) {
21766 			resume_addnsec3chain(zone);
21767 		}
21768 
21769 		/*
21770 		 * Schedule the next resigning event
21771 		 */
21772 		set_resigntime(zone);
21773 	}
21774 
21775 	isc_time_settoepoch(&zone->refreshkeytime);
21776 
21777 	/*
21778 	 * If keymgr provided a next time, use the calculated next rekey time.
21779 	 */
21780 	if (kasp != NULL) {
21781 		isc_time_t timenext;
21782 		uint32_t nexttime_seconds;
21783 
21784 		/*
21785 		 * Set the key refresh timer to the next scheduled key event
21786 		 * or to 'dnssec-loadkeys-interval' seconds in the future
21787 		 * if no next key event is scheduled (nexttime == 0).
21788 		 */
21789 		if (nexttime > 0) {
21790 			nexttime_seconds = nexttime - now;
21791 		} else {
21792 			nexttime_seconds = zone->refreshkeyinterval;
21793 		}
21794 
21795 		DNS_ZONE_TIME_ADD(&timenow, nexttime_seconds, &timenext);
21796 		zone->refreshkeytime = timenext;
21797 		zone_settimer(zone, &timenow);
21798 		isc_time_formattimestamp(&zone->refreshkeytime, timebuf, 80);
21799 
21800 		dnssec_log(zone, ISC_LOG_DEBUG(3),
21801 			   "next key event in %u seconds", nexttime_seconds);
21802 		dnssec_log(zone, ISC_LOG_INFO, "next key event: %s", timebuf);
21803 	}
21804 	/*
21805 	 * If we're doing key maintenance, set the key refresh timer to
21806 	 * the next scheduled key event or to 'dnssec-loadkeys-interval'
21807 	 * seconds in the future, whichever is sooner.
21808 	 */
21809 	else if (DNS_ZONEKEY_OPTION(zone, DNS_ZONEKEY_MAINTAIN))
21810 	{
21811 		isc_time_t timethen;
21812 		isc_stdtime_t then;
21813 
21814 		DNS_ZONE_TIME_ADD(&timenow, zone->refreshkeyinterval,
21815 				  &timethen);
21816 		zone->refreshkeytime = timethen;
21817 
21818 		for (key = ISC_LIST_HEAD(dnskeys); key != NULL;
21819 		     key = ISC_LIST_NEXT(key, link)) {
21820 			then = now;
21821 			result = next_keyevent(key->key, &then);
21822 			if (result != ISC_R_SUCCESS) {
21823 				continue;
21824 			}
21825 
21826 			DNS_ZONE_TIME_ADD(&timenow, then - now, &timethen);
21827 			if (isc_time_compare(&timethen, &zone->refreshkeytime) <
21828 			    0) {
21829 				zone->refreshkeytime = timethen;
21830 			}
21831 		}
21832 
21833 		zone_settimer(zone, &timenow);
21834 
21835 		isc_time_formattimestamp(&zone->refreshkeytime, timebuf, 80);
21836 		dnssec_log(zone, ISC_LOG_INFO, "next key event: %s", timebuf);
21837 	}
21838 	UNLOCK_ZONE(zone);
21839 
21840 	if (isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3))) {
21841 		for (key = ISC_LIST_HEAD(dnskeys); key != NULL;
21842 		     key = ISC_LIST_NEXT(key, link)) {
21843 			/* This debug log is used in the kasp system test */
21844 			char algbuf[DNS_SECALG_FORMATSIZE];
21845 			dns_secalg_format(dst_key_alg(key->key), algbuf,
21846 					  sizeof(algbuf));
21847 			dnssec_log(zone, ISC_LOG_DEBUG(3),
21848 				   "zone_rekey done: key %d/%s",
21849 				   dst_key_id(key->key), algbuf);
21850 		}
21851 	}
21852 
21853 	result = ISC_R_SUCCESS;
21854 
21855 failure:
21856 	LOCK_ZONE(zone);
21857 	if (result != ISC_R_SUCCESS) {
21858 		/*
21859 		 * Something went wrong; try again in ten minutes or
21860 		 * after a key refresh interval, whichever is shorter.
21861 		 */
21862 		dnssec_log(zone, ISC_LOG_DEBUG(3),
21863 			   "zone_rekey failure: %s (retry in %u seconds)",
21864 			   isc_result_totext(result),
21865 			   ISC_MIN(zone->refreshkeyinterval, 600));
21866 		isc_interval_set(&ival, ISC_MIN(zone->refreshkeyinterval, 600),
21867 				 0);
21868 		isc_time_nowplusinterval(&zone->refreshkeytime, &ival);
21869 	}
21870 	UNLOCK_ZONE(zone);
21871 
21872 	dns_diff_clear(&diff);
21873 	dns_diff_clear(&_sig_diff);
21874 
21875 	clear_keylist(&dnskeys, mctx);
21876 	clear_keylist(&keys, mctx);
21877 	clear_keylist(&rmkeys, mctx);
21878 
21879 	if (ver != NULL) {
21880 		dns_db_closeversion(db, &ver, false);
21881 	}
21882 	if (dns_rdataset_isassociated(&cdsset)) {
21883 		dns_rdataset_disassociate(&cdsset);
21884 	}
21885 	if (dns_rdataset_isassociated(&keyset)) {
21886 		dns_rdataset_disassociate(&keyset);
21887 	}
21888 	if (dns_rdataset_isassociated(&keysigs)) {
21889 		dns_rdataset_disassociate(&keysigs);
21890 	}
21891 	if (dns_rdataset_isassociated(&soasigs)) {
21892 		dns_rdataset_disassociate(&soasigs);
21893 	}
21894 	if (dns_rdataset_isassociated(&cdnskeyset)) {
21895 		dns_rdataset_disassociate(&cdnskeyset);
21896 	}
21897 	if (node != NULL) {
21898 		dns_db_detachnode(db, &node);
21899 	}
21900 	if (db != NULL) {
21901 		dns_db_detach(&db);
21902 	}
21903 
21904 	INSIST(ver == NULL);
21905 }
21906 
21907 void
dns_zone_rekey(dns_zone_t * zone,bool fullsign)21908 dns_zone_rekey(dns_zone_t *zone, bool fullsign) {
21909 	isc_time_t now;
21910 
21911 	if (zone->type == dns_zone_primary && zone->task != NULL) {
21912 		LOCK_ZONE(zone);
21913 
21914 		if (fullsign) {
21915 			DNS_ZONEKEY_SETOPTION(zone, DNS_ZONEKEY_FULLSIGN);
21916 		}
21917 
21918 		TIME_NOW(&now);
21919 		zone->refreshkeytime = now;
21920 		zone_settimer(zone, &now);
21921 
21922 		UNLOCK_ZONE(zone);
21923 	}
21924 }
21925 
21926 isc_result_t
dns_zone_nscheck(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * version,unsigned int * errors)21927 dns_zone_nscheck(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version,
21928 		 unsigned int *errors) {
21929 	isc_result_t result;
21930 	dns_dbnode_t *node = NULL;
21931 
21932 	REQUIRE(DNS_ZONE_VALID(zone));
21933 	REQUIRE(errors != NULL);
21934 
21935 	result = dns_db_getoriginnode(db, &node);
21936 	if (result != ISC_R_SUCCESS) {
21937 		return (result);
21938 	}
21939 	result = zone_count_ns_rr(zone, db, node, version, NULL, errors, false);
21940 	dns_db_detachnode(db, &node);
21941 	return (result);
21942 }
21943 
21944 isc_result_t
dns_zone_cdscheck(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * version)21945 dns_zone_cdscheck(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version) {
21946 	isc_result_t result;
21947 	dns_dbnode_t *node = NULL;
21948 	dns_rdataset_t dnskey, cds, cdnskey;
21949 	unsigned char algorithms[256];
21950 	unsigned int i;
21951 	bool empty = false;
21952 
21953 	enum { notexpected = 0, expected = 1, found = 2 };
21954 
21955 	REQUIRE(DNS_ZONE_VALID(zone));
21956 
21957 	result = dns_db_getoriginnode(db, &node);
21958 	if (result != ISC_R_SUCCESS) {
21959 		return (result);
21960 	}
21961 
21962 	dns_rdataset_init(&cds);
21963 	dns_rdataset_init(&dnskey);
21964 	dns_rdataset_init(&cdnskey);
21965 
21966 	result = dns_db_findrdataset(db, node, version, dns_rdatatype_cds,
21967 				     dns_rdatatype_none, 0, &cds, NULL);
21968 	if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
21969 		goto failure;
21970 	}
21971 
21972 	result = dns_db_findrdataset(db, node, version, dns_rdatatype_cdnskey,
21973 				     dns_rdatatype_none, 0, &cdnskey, NULL);
21974 	if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) {
21975 		goto failure;
21976 	}
21977 
21978 	if (!dns_rdataset_isassociated(&cds) &&
21979 	    !dns_rdataset_isassociated(&cdnskey)) {
21980 		result = ISC_R_SUCCESS;
21981 		goto failure;
21982 	}
21983 
21984 	result = dns_db_findrdataset(db, node, version, dns_rdatatype_dnskey,
21985 				     dns_rdatatype_none, 0, &dnskey, NULL);
21986 	if (result == ISC_R_NOTFOUND) {
21987 		empty = true;
21988 	} else if (result != ISC_R_SUCCESS) {
21989 		goto failure;
21990 	}
21991 
21992 	/*
21993 	 * For each DNSSEC algorithm in the CDS RRset there must be
21994 	 * a matching DNSKEY record with the exception of a CDS deletion
21995 	 * record which must be by itself.
21996 	 */
21997 	if (dns_rdataset_isassociated(&cds)) {
21998 		bool delete = false;
21999 		memset(algorithms, notexpected, sizeof(algorithms));
22000 		for (result = dns_rdataset_first(&cds); result == ISC_R_SUCCESS;
22001 		     result = dns_rdataset_next(&cds))
22002 		{
22003 			dns_rdata_t crdata = DNS_RDATA_INIT;
22004 			dns_rdata_cds_t structcds;
22005 
22006 			dns_rdataset_current(&cds, &crdata);
22007 			/*
22008 			 * CDS deletion record has this form "0 0 0 00" which
22009 			 * is 5 zero octets.
22010 			 */
22011 			if (crdata.length == 5U &&
22012 			    memcmp(crdata.data,
22013 				   (unsigned char[5]){ 0, 0, 0, 0, 0 }, 5) == 0)
22014 			{
22015 				delete = true;
22016 				continue;
22017 			}
22018 
22019 			if (empty) {
22020 				result = DNS_R_BADCDS;
22021 				goto failure;
22022 			}
22023 
22024 			CHECK(dns_rdata_tostruct(&crdata, &structcds, NULL));
22025 			if (algorithms[structcds.algorithm] == 0) {
22026 				algorithms[structcds.algorithm] = expected;
22027 			}
22028 			for (result = dns_rdataset_first(&dnskey);
22029 			     result == ISC_R_SUCCESS;
22030 			     result = dns_rdataset_next(&dnskey))
22031 			{
22032 				dns_rdata_t rdata = DNS_RDATA_INIT;
22033 				dns_rdata_dnskey_t structdnskey;
22034 
22035 				dns_rdataset_current(&dnskey, &rdata);
22036 				CHECK(dns_rdata_tostruct(&rdata, &structdnskey,
22037 							 NULL));
22038 
22039 				if (structdnskey.algorithm ==
22040 				    structcds.algorithm) {
22041 					algorithms[structcds.algorithm] = found;
22042 				}
22043 			}
22044 			if (result != ISC_R_NOMORE) {
22045 				goto failure;
22046 			}
22047 		}
22048 		for (i = 0; i < sizeof(algorithms); i++) {
22049 			if (delete) {
22050 				if (algorithms[i] != notexpected) {
22051 					result = DNS_R_BADCDS;
22052 					goto failure;
22053 				}
22054 			} else if (algorithms[i] == expected) {
22055 				result = DNS_R_BADCDS;
22056 				goto failure;
22057 			}
22058 		}
22059 	}
22060 
22061 	/*
22062 	 * For each DNSSEC algorithm in the CDNSKEY RRset there must be
22063 	 * a matching DNSKEY record with the exception of a CDNSKEY deletion
22064 	 * record which must be by itself.
22065 	 */
22066 	if (dns_rdataset_isassociated(&cdnskey)) {
22067 		bool delete = false;
22068 		memset(algorithms, notexpected, sizeof(algorithms));
22069 		for (result = dns_rdataset_first(&cdnskey);
22070 		     result == ISC_R_SUCCESS;
22071 		     result = dns_rdataset_next(&cdnskey))
22072 		{
22073 			dns_rdata_t crdata = DNS_RDATA_INIT;
22074 			dns_rdata_cdnskey_t structcdnskey;
22075 
22076 			dns_rdataset_current(&cdnskey, &crdata);
22077 			/*
22078 			 * CDNSKEY deletion record has this form
22079 			 * "0 3 0 AA==" which is 2 zero octets, a 3,
22080 			 * and 2 zero octets.
22081 			 */
22082 			if (crdata.length == 5U &&
22083 			    memcmp(crdata.data,
22084 				   (unsigned char[5]){ 0, 0, 3, 0, 0 }, 5) == 0)
22085 			{
22086 				delete = true;
22087 				continue;
22088 			}
22089 
22090 			if (empty) {
22091 				result = DNS_R_BADCDNSKEY;
22092 				goto failure;
22093 			}
22094 
22095 			CHECK(dns_rdata_tostruct(&crdata, &structcdnskey,
22096 						 NULL));
22097 			if (algorithms[structcdnskey.algorithm] == 0) {
22098 				algorithms[structcdnskey.algorithm] = expected;
22099 			}
22100 			for (result = dns_rdataset_first(&dnskey);
22101 			     result == ISC_R_SUCCESS;
22102 			     result = dns_rdataset_next(&dnskey))
22103 			{
22104 				dns_rdata_t rdata = DNS_RDATA_INIT;
22105 				dns_rdata_dnskey_t structdnskey;
22106 
22107 				dns_rdataset_current(&dnskey, &rdata);
22108 				CHECK(dns_rdata_tostruct(&rdata, &structdnskey,
22109 							 NULL));
22110 
22111 				if (structdnskey.algorithm ==
22112 				    structcdnskey.algorithm) {
22113 					algorithms[structcdnskey.algorithm] =
22114 						found;
22115 				}
22116 			}
22117 			if (result != ISC_R_NOMORE) {
22118 				goto failure;
22119 			}
22120 		}
22121 		for (i = 0; i < sizeof(algorithms); i++) {
22122 			if (delete) {
22123 				if (algorithms[i] != notexpected) {
22124 					result = DNS_R_BADCDNSKEY;
22125 					goto failure;
22126 				}
22127 			} else if (algorithms[i] == expected) {
22128 				result = DNS_R_BADCDNSKEY;
22129 				goto failure;
22130 			}
22131 		}
22132 	}
22133 	result = ISC_R_SUCCESS;
22134 
22135 failure:
22136 	if (dns_rdataset_isassociated(&cds)) {
22137 		dns_rdataset_disassociate(&cds);
22138 	}
22139 	if (dns_rdataset_isassociated(&dnskey)) {
22140 		dns_rdataset_disassociate(&dnskey);
22141 	}
22142 	if (dns_rdataset_isassociated(&cdnskey)) {
22143 		dns_rdataset_disassociate(&cdnskey);
22144 	}
22145 	dns_db_detachnode(db, &node);
22146 	return (result);
22147 }
22148 
22149 void
dns_zone_setautomatic(dns_zone_t * zone,bool automatic)22150 dns_zone_setautomatic(dns_zone_t *zone, bool automatic) {
22151 	REQUIRE(DNS_ZONE_VALID(zone));
22152 
22153 	LOCK_ZONE(zone);
22154 	zone->automatic = automatic;
22155 	UNLOCK_ZONE(zone);
22156 }
22157 
22158 bool
dns_zone_getautomatic(dns_zone_t * zone)22159 dns_zone_getautomatic(dns_zone_t *zone) {
22160 	REQUIRE(DNS_ZONE_VALID(zone));
22161 	return (zone->automatic);
22162 }
22163 
22164 void
dns_zone_setadded(dns_zone_t * zone,bool added)22165 dns_zone_setadded(dns_zone_t *zone, bool added) {
22166 	REQUIRE(DNS_ZONE_VALID(zone));
22167 
22168 	LOCK_ZONE(zone);
22169 	zone->added = added;
22170 	UNLOCK_ZONE(zone);
22171 }
22172 
22173 bool
dns_zone_getadded(dns_zone_t * zone)22174 dns_zone_getadded(dns_zone_t *zone) {
22175 	REQUIRE(DNS_ZONE_VALID(zone));
22176 	return (zone->added);
22177 }
22178 
22179 isc_result_t
dns_zone_dlzpostload(dns_zone_t * zone,dns_db_t * db)22180 dns_zone_dlzpostload(dns_zone_t *zone, dns_db_t *db) {
22181 	isc_time_t loadtime;
22182 	isc_result_t result;
22183 	dns_zone_t *secure = NULL;
22184 
22185 	TIME_NOW(&loadtime);
22186 
22187 	/*
22188 	 * Lock hierarchy: zmgr, zone, raw.
22189 	 */
22190 again:
22191 	LOCK_ZONE(zone);
22192 	INSIST(zone != zone->raw);
22193 	if (inline_secure(zone)) {
22194 		LOCK_ZONE(zone->raw);
22195 	} else if (inline_raw(zone)) {
22196 		secure = zone->secure;
22197 		TRYLOCK_ZONE(result, secure);
22198 		if (result != ISC_R_SUCCESS) {
22199 			UNLOCK_ZONE(zone);
22200 			secure = NULL;
22201 			isc_thread_yield();
22202 			goto again;
22203 		}
22204 	}
22205 	result = zone_postload(zone, db, loadtime, ISC_R_SUCCESS);
22206 	if (inline_secure(zone)) {
22207 		UNLOCK_ZONE(zone->raw);
22208 	} else if (secure != NULL) {
22209 		UNLOCK_ZONE(secure);
22210 	}
22211 	UNLOCK_ZONE(zone);
22212 	return (result);
22213 }
22214 
22215 isc_result_t
dns_zone_setrefreshkeyinterval(dns_zone_t * zone,uint32_t interval)22216 dns_zone_setrefreshkeyinterval(dns_zone_t *zone, uint32_t interval) {
22217 	REQUIRE(DNS_ZONE_VALID(zone));
22218 	if (interval == 0) {
22219 		return (ISC_R_RANGE);
22220 	}
22221 	/* Maximum value: 24 hours (3600 minutes) */
22222 	if (interval > (24 * 60)) {
22223 		interval = (24 * 60);
22224 	}
22225 	/* Multiply by 60 for seconds */
22226 	zone->refreshkeyinterval = interval * 60;
22227 	return (ISC_R_SUCCESS);
22228 }
22229 
22230 void
dns_zone_setrequestixfr(dns_zone_t * zone,bool flag)22231 dns_zone_setrequestixfr(dns_zone_t *zone, bool flag) {
22232 	REQUIRE(DNS_ZONE_VALID(zone));
22233 	zone->requestixfr = flag;
22234 }
22235 
22236 bool
dns_zone_getrequestixfr(dns_zone_t * zone)22237 dns_zone_getrequestixfr(dns_zone_t *zone) {
22238 	REQUIRE(DNS_ZONE_VALID(zone));
22239 	return (zone->requestixfr);
22240 }
22241 
22242 void
dns_zone_setixfrratio(dns_zone_t * zone,uint32_t ratio)22243 dns_zone_setixfrratio(dns_zone_t *zone, uint32_t ratio) {
22244 	REQUIRE(DNS_ZONE_VALID(zone));
22245 	zone->ixfr_ratio = ratio;
22246 }
22247 
22248 uint32_t
dns_zone_getixfrratio(dns_zone_t * zone)22249 dns_zone_getixfrratio(dns_zone_t *zone) {
22250 	REQUIRE(DNS_ZONE_VALID(zone));
22251 	return (zone->ixfr_ratio);
22252 }
22253 
22254 void
dns_zone_setrequestexpire(dns_zone_t * zone,bool flag)22255 dns_zone_setrequestexpire(dns_zone_t *zone, bool flag) {
22256 	REQUIRE(DNS_ZONE_VALID(zone));
22257 	zone->requestexpire = flag;
22258 }
22259 
22260 bool
dns_zone_getrequestexpire(dns_zone_t * zone)22261 dns_zone_getrequestexpire(dns_zone_t *zone) {
22262 	REQUIRE(DNS_ZONE_VALID(zone));
22263 	return (zone->requestexpire);
22264 }
22265 
22266 void
dns_zone_setserialupdatemethod(dns_zone_t * zone,dns_updatemethod_t method)22267 dns_zone_setserialupdatemethod(dns_zone_t *zone, dns_updatemethod_t method) {
22268 	REQUIRE(DNS_ZONE_VALID(zone));
22269 	zone->updatemethod = method;
22270 }
22271 
22272 dns_updatemethod_t
dns_zone_getserialupdatemethod(dns_zone_t * zone)22273 dns_zone_getserialupdatemethod(dns_zone_t *zone) {
22274 	REQUIRE(DNS_ZONE_VALID(zone));
22275 	return (zone->updatemethod);
22276 }
22277 
22278 /*
22279  * Lock hierarchy: zmgr, zone, raw.
22280  */
22281 isc_result_t
dns_zone_link(dns_zone_t * zone,dns_zone_t * raw)22282 dns_zone_link(dns_zone_t *zone, dns_zone_t *raw) {
22283 	isc_result_t result;
22284 	dns_zonemgr_t *zmgr;
22285 
22286 	REQUIRE(DNS_ZONE_VALID(zone));
22287 	REQUIRE(zone->zmgr != NULL);
22288 	REQUIRE(zone->task != NULL);
22289 	REQUIRE(zone->loadtask != NULL);
22290 	REQUIRE(zone->raw == NULL);
22291 
22292 	REQUIRE(DNS_ZONE_VALID(raw));
22293 	REQUIRE(raw->zmgr == NULL);
22294 	REQUIRE(raw->task == NULL);
22295 	REQUIRE(raw->loadtask == NULL);
22296 	REQUIRE(raw->secure == NULL);
22297 
22298 	REQUIRE(zone != raw);
22299 
22300 	/*
22301 	 * Lock hierarchy: zmgr, zone, raw.
22302 	 */
22303 	zmgr = zone->zmgr;
22304 	RWLOCK(&zmgr->rwlock, isc_rwlocktype_write);
22305 	LOCK_ZONE(zone);
22306 	LOCK_ZONE(raw);
22307 
22308 	result = isc_timer_create(zmgr->timermgr, isc_timertype_inactive, NULL,
22309 				  NULL, zone->task, zone_timer, raw,
22310 				  &raw->timer);
22311 	if (result != ISC_R_SUCCESS) {
22312 		goto unlock;
22313 	}
22314 
22315 	/*
22316 	 * The timer "holds" a iref.
22317 	 */
22318 	isc_refcount_increment0(&raw->irefs);
22319 
22320 	/* dns_zone_attach(raw, &zone->raw); */
22321 	isc_refcount_increment(&raw->erefs);
22322 	zone->raw = raw;
22323 
22324 	/* dns_zone_iattach(zone,  &raw->secure); */
22325 	zone_iattach(zone, &raw->secure);
22326 
22327 	isc_task_attach(zone->task, &raw->task);
22328 	isc_task_attach(zone->loadtask, &raw->loadtask);
22329 
22330 	ISC_LIST_APPEND(zmgr->zones, raw, link);
22331 	raw->zmgr = zmgr;
22332 	isc_refcount_increment(&zmgr->refs);
22333 
22334 unlock:
22335 	UNLOCK_ZONE(raw);
22336 	UNLOCK_ZONE(zone);
22337 	RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write);
22338 	return (result);
22339 }
22340 
22341 void
dns_zone_getraw(dns_zone_t * zone,dns_zone_t ** raw)22342 dns_zone_getraw(dns_zone_t *zone, dns_zone_t **raw) {
22343 	REQUIRE(DNS_ZONE_VALID(zone));
22344 	REQUIRE(raw != NULL && *raw == NULL);
22345 
22346 	LOCK(&zone->lock);
22347 	INSIST(zone != zone->raw);
22348 	if (zone->raw != NULL) {
22349 		dns_zone_attach(zone->raw, raw);
22350 	}
22351 	UNLOCK(&zone->lock);
22352 }
22353 
22354 struct keydone {
22355 	isc_event_t event;
22356 	bool all;
22357 	unsigned char data[5];
22358 };
22359 
22360 #define PENDINGFLAGS (DNS_NSEC3FLAG_CREATE | DNS_NSEC3FLAG_INITIAL)
22361 
22362 static void
keydone(isc_task_t * task,isc_event_t * event)22363 keydone(isc_task_t *task, isc_event_t *event) {
22364 	const char *me = "keydone";
22365 	bool commit = false;
22366 	isc_result_t result;
22367 	dns_rdata_t rdata = DNS_RDATA_INIT;
22368 	dns_dbversion_t *oldver = NULL, *newver = NULL;
22369 	dns_zone_t *zone;
22370 	dns_db_t *db = NULL;
22371 	dns_dbnode_t *node = NULL;
22372 	dns_rdataset_t rdataset;
22373 	dns_diff_t diff;
22374 	struct keydone *kd = (struct keydone *)event;
22375 	dns_update_log_t log = { update_log_cb, NULL };
22376 	bool clear_pending = false;
22377 
22378 	UNUSED(task);
22379 
22380 	zone = event->ev_arg;
22381 	INSIST(DNS_ZONE_VALID(zone));
22382 
22383 	ENTER;
22384 
22385 	dns_rdataset_init(&rdataset);
22386 	dns_diff_init(zone->mctx, &diff);
22387 
22388 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
22389 	if (zone->db != NULL) {
22390 		dns_db_attach(zone->db, &db);
22391 	}
22392 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
22393 	if (db == NULL) {
22394 		goto failure;
22395 	}
22396 
22397 	dns_db_currentversion(db, &oldver);
22398 	result = dns_db_newversion(db, &newver);
22399 	if (result != ISC_R_SUCCESS) {
22400 		dnssec_log(zone, ISC_LOG_ERROR,
22401 			   "keydone:dns_db_newversion -> %s",
22402 			   dns_result_totext(result));
22403 		goto failure;
22404 	}
22405 
22406 	result = dns_db_getoriginnode(db, &node);
22407 	if (result != ISC_R_SUCCESS) {
22408 		goto failure;
22409 	}
22410 
22411 	result = dns_db_findrdataset(db, node, newver, zone->privatetype,
22412 				     dns_rdatatype_none, 0, &rdataset, NULL);
22413 	if (result == ISC_R_NOTFOUND) {
22414 		INSIST(!dns_rdataset_isassociated(&rdataset));
22415 		goto failure;
22416 	}
22417 	if (result != ISC_R_SUCCESS) {
22418 		INSIST(!dns_rdataset_isassociated(&rdataset));
22419 		goto failure;
22420 	}
22421 
22422 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
22423 	     result = dns_rdataset_next(&rdataset))
22424 	{
22425 		bool found = false;
22426 
22427 		dns_rdataset_current(&rdataset, &rdata);
22428 
22429 		if (kd->all) {
22430 			if (rdata.length == 5 && rdata.data[0] != 0 &&
22431 			    rdata.data[3] == 0 && rdata.data[4] == 1)
22432 			{
22433 				found = true;
22434 			} else if (rdata.data[0] == 0 &&
22435 				   (rdata.data[2] & PENDINGFLAGS) != 0) {
22436 				found = true;
22437 				clear_pending = true;
22438 			}
22439 		} else if (rdata.length == 5 &&
22440 			   memcmp(rdata.data, kd->data, 5) == 0) {
22441 			found = true;
22442 		}
22443 
22444 		if (found) {
22445 			CHECK(update_one_rr(db, newver, &diff, DNS_DIFFOP_DEL,
22446 					    &zone->origin, rdataset.ttl,
22447 					    &rdata));
22448 		}
22449 		dns_rdata_reset(&rdata);
22450 	}
22451 
22452 	if (!ISC_LIST_EMPTY(diff.tuples)) {
22453 		/* Write changes to journal file. */
22454 		CHECK(update_soa_serial(zone, db, newver, &diff, zone->mctx,
22455 					zone->updatemethod));
22456 
22457 		result = dns_update_signatures(&log, zone, db, oldver, newver,
22458 					       &diff,
22459 					       zone->sigvalidityinterval);
22460 		if (!clear_pending) {
22461 			CHECK(result);
22462 		}
22463 
22464 		CHECK(zone_journal(zone, &diff, NULL, "keydone"));
22465 		commit = true;
22466 
22467 		LOCK_ZONE(zone);
22468 		DNS_ZONE_SETFLAG(zone,
22469 				 DNS_ZONEFLG_LOADED | DNS_ZONEFLG_NEEDNOTIFY);
22470 		zone_needdump(zone, 30);
22471 		UNLOCK_ZONE(zone);
22472 	}
22473 
22474 failure:
22475 	if (dns_rdataset_isassociated(&rdataset)) {
22476 		dns_rdataset_disassociate(&rdataset);
22477 	}
22478 	if (db != NULL) {
22479 		if (node != NULL) {
22480 			dns_db_detachnode(db, &node);
22481 		}
22482 		if (oldver != NULL) {
22483 			dns_db_closeversion(db, &oldver, false);
22484 		}
22485 		if (newver != NULL) {
22486 			dns_db_closeversion(db, &newver, commit);
22487 		}
22488 		dns_db_detach(&db);
22489 	}
22490 	dns_diff_clear(&diff);
22491 	isc_event_free(&event);
22492 	dns_zone_idetach(&zone);
22493 
22494 	INSIST(oldver == NULL);
22495 	INSIST(newver == NULL);
22496 }
22497 
22498 isc_result_t
dns_zone_keydone(dns_zone_t * zone,const char * keystr)22499 dns_zone_keydone(dns_zone_t *zone, const char *keystr) {
22500 	isc_result_t result = ISC_R_SUCCESS;
22501 	isc_event_t *e;
22502 	isc_buffer_t b;
22503 	dns_zone_t *dummy = NULL;
22504 	struct keydone *kd;
22505 
22506 	REQUIRE(DNS_ZONE_VALID(zone));
22507 
22508 	LOCK_ZONE(zone);
22509 
22510 	e = isc_event_allocate(zone->mctx, zone, DNS_EVENT_KEYDONE, keydone,
22511 			       zone, sizeof(struct keydone));
22512 
22513 	kd = (struct keydone *)e;
22514 	if (strcasecmp(keystr, "all") == 0) {
22515 		kd->all = true;
22516 	} else {
22517 		isc_textregion_t r;
22518 		const char *algstr;
22519 		dns_keytag_t keyid;
22520 		dns_secalg_t alg;
22521 		size_t n;
22522 
22523 		kd->all = false;
22524 
22525 		n = sscanf(keystr, "%hu/", &keyid);
22526 		if (n == 0U) {
22527 			CHECK(ISC_R_FAILURE);
22528 		}
22529 
22530 		algstr = strchr(keystr, '/');
22531 		if (algstr != NULL) {
22532 			algstr++;
22533 		} else {
22534 			CHECK(ISC_R_FAILURE);
22535 		}
22536 
22537 		n = sscanf(algstr, "%hhu", &alg);
22538 		if (n == 0U) {
22539 			DE_CONST(algstr, r.base);
22540 			r.length = strlen(algstr);
22541 			CHECK(dns_secalg_fromtext(&alg, &r));
22542 		}
22543 
22544 		/* construct a private-type rdata */
22545 		isc_buffer_init(&b, kd->data, sizeof(kd->data));
22546 		isc_buffer_putuint8(&b, alg);
22547 		isc_buffer_putuint8(&b, (keyid & 0xff00) >> 8);
22548 		isc_buffer_putuint8(&b, (keyid & 0xff));
22549 		isc_buffer_putuint8(&b, 0);
22550 		isc_buffer_putuint8(&b, 1);
22551 	}
22552 
22553 	zone_iattach(zone, &dummy);
22554 	isc_task_send(zone->task, &e);
22555 
22556 failure:
22557 	if (e != NULL) {
22558 		isc_event_free(&e);
22559 	}
22560 	UNLOCK_ZONE(zone);
22561 	return (result);
22562 }
22563 
22564 /*
22565  * Called from the zone task's queue after the relevant event is posted by
22566  * dns_zone_setnsec3param().
22567  */
22568 static void
setnsec3param(isc_task_t * task,isc_event_t * event)22569 setnsec3param(isc_task_t *task, isc_event_t *event) {
22570 	const char *me = "setnsec3param";
22571 	dns_zone_t *zone = event->ev_arg;
22572 	bool loadpending;
22573 
22574 	INSIST(DNS_ZONE_VALID(zone));
22575 
22576 	UNUSED(task);
22577 
22578 	ENTER;
22579 
22580 	LOCK_ZONE(zone);
22581 	loadpending = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING);
22582 	UNLOCK_ZONE(zone);
22583 
22584 	/*
22585 	 * If receive_secure_serial is still processing or we have a
22586 	 * queued event append rss_post queue.
22587 	 */
22588 	if (zone->rss_newver != NULL || ISC_LIST_HEAD(zone->rss_post) != NULL) {
22589 		/*
22590 		 * Wait for receive_secure_serial() to finish processing.
22591 		 */
22592 		ISC_LIST_APPEND(zone->rss_post, event, ev_link);
22593 	} else {
22594 		bool rescheduled = false;
22595 		ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
22596 		/*
22597 		 * The zone is not yet fully loaded. Reschedule the event to
22598 		 * be picked up later. This turns this function into a busy
22599 		 * wait, but it only happens at startup.
22600 		 */
22601 		if (zone->db == NULL && loadpending) {
22602 			rescheduled = true;
22603 			isc_task_send(task, &event);
22604 		}
22605 		ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
22606 		if (rescheduled) {
22607 			return;
22608 		}
22609 
22610 		rss_post(zone, event);
22611 	}
22612 	dns_zone_idetach(&zone);
22613 }
22614 
22615 static void
salt2text(unsigned char * salt,uint8_t saltlen,unsigned char * text,unsigned int textlen)22616 salt2text(unsigned char *salt, uint8_t saltlen, unsigned char *text,
22617 	  unsigned int textlen) {
22618 	isc_region_t r;
22619 	isc_buffer_t buf;
22620 	isc_result_t result;
22621 
22622 	r.base = salt;
22623 	r.length = (unsigned int)saltlen;
22624 
22625 	isc_buffer_init(&buf, text, textlen);
22626 	result = isc_hex_totext(&r, 2, "", &buf);
22627 	if (result == ISC_R_SUCCESS) {
22628 		text[saltlen * 2] = 0;
22629 	} else {
22630 		text[0] = 0;
22631 	}
22632 }
22633 
22634 /*
22635  * Check whether NSEC3 chain addition or removal specified by the private-type
22636  * record passed with the event was already queued (or even fully performed).
22637  * If not, modify the relevant private-type records at the zone apex and call
22638  * resume_addnsec3chain().
22639  */
22640 static void
rss_post(dns_zone_t * zone,isc_event_t * event)22641 rss_post(dns_zone_t *zone, isc_event_t *event) {
22642 	const char *me = "rss_post";
22643 	bool commit = false;
22644 	isc_result_t result;
22645 	dns_dbversion_t *oldver = NULL, *newver = NULL;
22646 	dns_db_t *db = NULL;
22647 	dns_dbnode_t *node = NULL;
22648 	dns_rdataset_t prdataset, nrdataset;
22649 	dns_diff_t diff;
22650 	struct np3event *npe = (struct np3event *)event;
22651 	nsec3param_t *np;
22652 	dns_update_log_t log = { update_log_cb, NULL };
22653 	dns_rdata_t rdata;
22654 	bool nseconly;
22655 	bool exists = false;
22656 
22657 	ENTER;
22658 
22659 	np = &npe->params;
22660 
22661 	dns_rdataset_init(&prdataset);
22662 	dns_rdataset_init(&nrdataset);
22663 	dns_diff_init(zone->mctx, &diff);
22664 
22665 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
22666 	if (zone->db != NULL) {
22667 		dns_db_attach(zone->db, &db);
22668 	}
22669 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
22670 	if (db == NULL) {
22671 		goto failure;
22672 	}
22673 
22674 	dns_db_currentversion(db, &oldver);
22675 	result = dns_db_newversion(db, &newver);
22676 	if (result != ISC_R_SUCCESS) {
22677 		dnssec_log(zone, ISC_LOG_ERROR,
22678 			   "setnsec3param:dns_db_newversion -> %s",
22679 			   dns_result_totext(result));
22680 		goto failure;
22681 	}
22682 
22683 	CHECK(dns_db_getoriginnode(db, &node));
22684 
22685 	/*
22686 	 * Do we need to look up the NSEC3 parameters?
22687 	 */
22688 	if (np->lookup) {
22689 		dns_rdata_nsec3param_t param;
22690 		dns_rdata_t nrdata = DNS_RDATA_INIT;
22691 		dns_rdata_t prdata = DNS_RDATA_INIT;
22692 		unsigned char nbuf[DNS_NSEC3PARAM_BUFFERSIZE];
22693 		unsigned char saltbuf[255];
22694 		isc_buffer_t b;
22695 
22696 		param.salt = NULL;
22697 		result = dns__zone_lookup_nsec3param(zone, &np->rdata, &param,
22698 						     saltbuf, np->resalt);
22699 		if (result == ISC_R_SUCCESS) {
22700 			/*
22701 			 * Success because the NSEC3PARAM already exists, but
22702 			 * function returns void, so goto failure to clean up.
22703 			 */
22704 			goto failure;
22705 		}
22706 		if (result != DNS_R_NSEC3RESALT && result != ISC_R_NOTFOUND) {
22707 			dnssec_log(zone, ISC_LOG_DEBUG(3),
22708 				   "setnsec3param:lookup nsec3param -> %s",
22709 				   isc_result_totext(result));
22710 			goto failure;
22711 		}
22712 
22713 		INSIST(param.salt != NULL);
22714 
22715 		/* Update NSEC3 parameters. */
22716 		np->rdata.hash = param.hash;
22717 		np->rdata.flags = param.flags;
22718 		np->rdata.iterations = param.iterations;
22719 		np->rdata.salt_length = param.salt_length;
22720 		np->rdata.salt = param.salt;
22721 
22722 		isc_buffer_init(&b, nbuf, sizeof(nbuf));
22723 		CHECK(dns_rdata_fromstruct(&nrdata, zone->rdclass,
22724 					   dns_rdatatype_nsec3param, &np->rdata,
22725 					   &b));
22726 		dns_nsec3param_toprivate(&nrdata, &prdata, zone->privatetype,
22727 					 np->data, sizeof(np->data));
22728 		np->length = prdata.length;
22729 		np->nsec = false;
22730 	}
22731 
22732 	/*
22733 	 * Does a private-type record already exist for this chain?
22734 	 */
22735 	result = dns_db_findrdataset(db, node, newver, zone->privatetype,
22736 				     dns_rdatatype_none, 0, &prdataset, NULL);
22737 	if (result == ISC_R_SUCCESS) {
22738 		for (result = dns_rdataset_first(&prdataset);
22739 		     result == ISC_R_SUCCESS;
22740 		     result = dns_rdataset_next(&prdataset))
22741 		{
22742 			dns_rdata_init(&rdata);
22743 			dns_rdataset_current(&prdataset, &rdata);
22744 
22745 			if (np->length == rdata.length &&
22746 			    memcmp(rdata.data, np->data, np->length) == 0) {
22747 				exists = true;
22748 				break;
22749 			}
22750 		}
22751 	} else if (result != ISC_R_NOTFOUND) {
22752 		INSIST(!dns_rdataset_isassociated(&prdataset));
22753 		goto failure;
22754 	}
22755 
22756 	/*
22757 	 * Does the chain already exist?
22758 	 */
22759 	result = dns_db_findrdataset(db, node, newver, dns_rdatatype_nsec3param,
22760 				     dns_rdatatype_none, 0, &nrdataset, NULL);
22761 	if (result == ISC_R_SUCCESS) {
22762 		for (result = dns_rdataset_first(&nrdataset);
22763 		     result == ISC_R_SUCCESS;
22764 		     result = dns_rdataset_next(&nrdataset))
22765 		{
22766 			dns_rdata_init(&rdata);
22767 			dns_rdataset_current(&nrdataset, &rdata);
22768 
22769 			if (np->length == (rdata.length + 1) &&
22770 			    memcmp(rdata.data, np->data + 1, np->length - 1) ==
22771 				    0)
22772 			{
22773 				exists = true;
22774 				break;
22775 			}
22776 		}
22777 	} else if (result != ISC_R_NOTFOUND) {
22778 		INSIST(!dns_rdataset_isassociated(&nrdataset));
22779 		goto failure;
22780 	}
22781 
22782 	/*
22783 	 * We need to remove any existing NSEC3 chains if the supplied NSEC3
22784 	 * parameters are supposed to replace the current ones or if we are
22785 	 * switching to NSEC.
22786 	 */
22787 	if (!exists && np->replace && (np->length != 0 || np->nsec)) {
22788 		CHECK(dns_nsec3param_deletechains(db, newver, zone, !np->nsec,
22789 						  &diff));
22790 	}
22791 
22792 	if (!exists && np->length != 0) {
22793 		/*
22794 		 * We're creating an NSEC3 chain.  Add the private-type record
22795 		 * passed in the event handler's argument to the zone apex.
22796 		 *
22797 		 * If the zone is not currently capable of supporting an NSEC3
22798 		 * chain (due to the DNSKEY RRset at the zone apex not existing
22799 		 * or containing at least one key using an NSEC-only
22800 		 * algorithm), add the INITIAL flag, so these parameters can be
22801 		 * used later when NSEC3 becomes available.
22802 		 */
22803 		dns_rdata_init(&rdata);
22804 
22805 		np->data[2] |= DNS_NSEC3FLAG_CREATE;
22806 		result = dns_nsec_nseconly(db, newver, &nseconly);
22807 		if (result == ISC_R_NOTFOUND || nseconly) {
22808 			np->data[2] |= DNS_NSEC3FLAG_INITIAL;
22809 		}
22810 
22811 		rdata.length = np->length;
22812 		rdata.data = np->data;
22813 		rdata.type = zone->privatetype;
22814 		rdata.rdclass = zone->rdclass;
22815 		CHECK(update_one_rr(db, newver, &diff, DNS_DIFFOP_ADD,
22816 				    &zone->origin, 0, &rdata));
22817 	}
22818 
22819 	/*
22820 	 * If we changed anything in the zone, write changes to journal file
22821 	 * and set commit to true so that resume_addnsec3chain() will be
22822 	 * called below in order to kick off adding/removing relevant NSEC3
22823 	 * records.
22824 	 */
22825 	if (!ISC_LIST_EMPTY(diff.tuples)) {
22826 		CHECK(update_soa_serial(zone, db, newver, &diff, zone->mctx,
22827 					zone->updatemethod));
22828 		result = dns_update_signatures(&log, zone, db, oldver, newver,
22829 					       &diff,
22830 					       zone->sigvalidityinterval);
22831 		if (result != ISC_R_NOTFOUND) {
22832 			CHECK(result);
22833 		}
22834 		CHECK(zone_journal(zone, &diff, NULL, "setnsec3param"));
22835 		commit = true;
22836 
22837 		LOCK_ZONE(zone);
22838 		DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED);
22839 		zone_needdump(zone, 30);
22840 		UNLOCK_ZONE(zone);
22841 	}
22842 
22843 failure:
22844 	if (dns_rdataset_isassociated(&prdataset)) {
22845 		dns_rdataset_disassociate(&prdataset);
22846 	}
22847 	if (dns_rdataset_isassociated(&nrdataset)) {
22848 		dns_rdataset_disassociate(&nrdataset);
22849 	}
22850 	if (node != NULL) {
22851 		dns_db_detachnode(db, &node);
22852 	}
22853 	if (oldver != NULL) {
22854 		dns_db_closeversion(db, &oldver, false);
22855 	}
22856 	if (newver != NULL) {
22857 		dns_db_closeversion(db, &newver, commit);
22858 	}
22859 	if (db != NULL) {
22860 		dns_db_detach(&db);
22861 	}
22862 	if (commit) {
22863 		LOCK_ZONE(zone);
22864 		resume_addnsec3chain(zone);
22865 		UNLOCK_ZONE(zone);
22866 	}
22867 	dns_diff_clear(&diff);
22868 	isc_event_free(&event);
22869 
22870 	INSIST(oldver == NULL);
22871 	INSIST(newver == NULL);
22872 }
22873 
22874 /*
22875  * Check if zone has NSEC3PARAM (and thus a chain) with the right parameters.
22876  *
22877  * If 'salt' is NULL, a match is found if the salt has the requested length,
22878  * otherwise the NSEC3 salt must match the requested salt value too.
22879  *
22880  * Returns  ISC_R_SUCCESS, if a match is found, or an error if no match is
22881  * found, or if the db lookup failed.
22882  */
22883 isc_result_t
dns__zone_lookup_nsec3param(dns_zone_t * zone,dns_rdata_nsec3param_t * lookup,dns_rdata_nsec3param_t * param,unsigned char saltbuf[255],bool resalt)22884 dns__zone_lookup_nsec3param(dns_zone_t *zone, dns_rdata_nsec3param_t *lookup,
22885 			    dns_rdata_nsec3param_t *param,
22886 			    unsigned char saltbuf[255], bool resalt) {
22887 	isc_result_t result = ISC_R_UNEXPECTED;
22888 	dns_dbnode_t *node = NULL;
22889 	dns_db_t *db = NULL;
22890 	dns_dbversion_t *version = NULL;
22891 	dns_rdataset_t rdataset;
22892 	dns_rdata_nsec3param_t nsec3param;
22893 	dns_rdata_t rdata = DNS_RDATA_INIT;
22894 
22895 	REQUIRE(DNS_ZONE_VALID(zone));
22896 
22897 	dns_rdataset_init(&rdataset);
22898 
22899 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
22900 	if (zone->db != NULL) {
22901 		dns_db_attach(zone->db, &db);
22902 	}
22903 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
22904 	if (db == NULL) {
22905 		result = ISC_R_FAILURE;
22906 		goto setparam;
22907 	}
22908 
22909 	result = dns_db_findnode(db, &zone->origin, false, &node);
22910 	if (result != ISC_R_SUCCESS) {
22911 		dns_zone_log(zone, ISC_LOG_ERROR,
22912 			     "dns__zone_lookup_nsec3param:"
22913 			     "dns_db_findnode -> %s",
22914 			     dns_result_totext(result));
22915 		result = ISC_R_FAILURE;
22916 		goto setparam;
22917 	}
22918 	dns_db_currentversion(db, &version);
22919 
22920 	result = dns_db_findrdataset(db, node, version,
22921 				     dns_rdatatype_nsec3param,
22922 				     dns_rdatatype_none, 0, &rdataset, NULL);
22923 	if (result != ISC_R_SUCCESS) {
22924 		INSIST(!dns_rdataset_isassociated(&rdataset));
22925 		if (result != ISC_R_NOTFOUND) {
22926 			dns_zone_log(zone, ISC_LOG_ERROR,
22927 				     "dns__zone_lookup_nsec3param:"
22928 				     "dns_db_findrdataset -> %s",
22929 				     dns_result_totext(result));
22930 		}
22931 		goto setparam;
22932 	}
22933 
22934 	for (result = dns_rdataset_first(&rdataset); result == ISC_R_SUCCESS;
22935 	     result = dns_rdataset_next(&rdataset))
22936 	{
22937 		dns_rdataset_current(&rdataset, &rdata);
22938 		result = dns_rdata_tostruct(&rdata, &nsec3param, NULL);
22939 		INSIST(result == ISC_R_SUCCESS);
22940 		dns_rdata_reset(&rdata);
22941 
22942 		/* Check parameters. */
22943 		if (nsec3param.hash != lookup->hash) {
22944 			continue;
22945 		}
22946 		if (nsec3param.iterations != lookup->iterations) {
22947 			continue;
22948 		}
22949 		if (nsec3param.salt_length != lookup->salt_length) {
22950 			continue;
22951 		}
22952 		if (lookup->salt != NULL) {
22953 			if (memcmp(nsec3param.salt, lookup->salt,
22954 				   lookup->salt_length) != 0) {
22955 				continue;
22956 			}
22957 		}
22958 		/* Found a match. */
22959 		result = ISC_R_SUCCESS;
22960 		param->hash = nsec3param.hash;
22961 		param->flags = nsec3param.flags;
22962 		param->iterations = nsec3param.iterations;
22963 		param->salt_length = nsec3param.salt_length;
22964 		param->salt = nsec3param.salt;
22965 		break;
22966 	}
22967 
22968 	if (result == ISC_R_NOMORE) {
22969 		result = ISC_R_NOTFOUND;
22970 	}
22971 
22972 setparam:
22973 	if (result != ISC_R_SUCCESS) {
22974 		/* Found no match. */
22975 		param->hash = lookup->hash;
22976 		param->flags = lookup->flags;
22977 		param->iterations = lookup->iterations;
22978 		param->salt_length = lookup->salt_length;
22979 		param->salt = lookup->salt;
22980 	}
22981 
22982 	if (result != ISC_R_NOTFOUND && result != ISC_R_SUCCESS) {
22983 		goto failure;
22984 	}
22985 
22986 	if (param->salt_length == 0) {
22987 		DE_CONST("-", param->salt);
22988 	} else if (resalt || param->salt == NULL) {
22989 		unsigned char *newsalt;
22990 		unsigned char salttext[255 * 2 + 1];
22991 		do {
22992 			/* Generate a new salt. */
22993 			result = dns_nsec3_generate_salt(saltbuf,
22994 							 param->salt_length);
22995 			if (result != ISC_R_SUCCESS) {
22996 				break;
22997 			}
22998 			newsalt = saltbuf;
22999 			salt2text(newsalt, param->salt_length, salttext,
23000 				  sizeof(salttext));
23001 			dnssec_log(zone, ISC_LOG_INFO, "generated salt: %s",
23002 				   salttext);
23003 			/* Check for salt conflict. */
23004 			if (param->salt != NULL &&
23005 			    memcmp(newsalt, param->salt, param->salt_length) ==
23006 				    0)
23007 			{
23008 				result = ISC_R_SUCCESS;
23009 			} else {
23010 				param->salt = newsalt;
23011 				result = DNS_R_NSEC3RESALT;
23012 			}
23013 		} while (result == ISC_R_SUCCESS);
23014 
23015 		INSIST(result != ISC_R_SUCCESS);
23016 	}
23017 
23018 failure:
23019 	if (dns_rdataset_isassociated(&rdataset)) {
23020 		dns_rdataset_disassociate(&rdataset);
23021 	}
23022 	if (node != NULL) {
23023 		dns_db_detachnode(db, &node);
23024 	}
23025 	if (version != NULL) {
23026 		dns_db_closeversion(db, &version, false);
23027 	}
23028 	if (db != NULL) {
23029 		dns_db_detach(&db);
23030 	}
23031 
23032 	return (result);
23033 }
23034 
23035 /*
23036  * Called when an "rndc signing -nsec3param ..." command is received, or the
23037  * 'dnssec-policy' has changed.
23038  *
23039  * Allocate and prepare an nsec3param_t structure which holds information about
23040  * the NSEC3 changes requested for the zone:
23041  *
23042  *   - if NSEC3 is to be disabled ("-nsec3param none"), only set the "nsec"
23043  *     field of the structure to true and the "replace" field to the value
23044  *     of the "replace" argument, leaving other fields initialized to zeros, to
23045  *     signal that the zone should be signed using NSEC instead of NSEC3,
23046  *
23047  *   - otherwise, prepare NSEC3PARAM RDATA that will eventually be inserted at
23048  *     the zone apex, convert it to a private-type record and store the latter
23049  *     in the "data" field of the nsec3param_t structure.
23050  *
23051  * Once the nsec3param_t structure is prepared, post an event to the zone's
23052  * task which will cause setnsec3param() to be called with the prepared
23053  * structure passed as an argument.
23054  */
23055 isc_result_t
dns_zone_setnsec3param(dns_zone_t * zone,uint8_t hash,uint8_t flags,uint16_t iter,uint8_t saltlen,unsigned char * salt,bool replace,bool resalt)23056 dns_zone_setnsec3param(dns_zone_t *zone, uint8_t hash, uint8_t flags,
23057 		       uint16_t iter, uint8_t saltlen, unsigned char *salt,
23058 		       bool replace, bool resalt) {
23059 	isc_result_t result = ISC_R_SUCCESS;
23060 	dns_rdata_nsec3param_t param, lookup;
23061 	dns_rdata_t nrdata = DNS_RDATA_INIT;
23062 	dns_rdata_t prdata = DNS_RDATA_INIT;
23063 	unsigned char nbuf[DNS_NSEC3PARAM_BUFFERSIZE];
23064 	unsigned char saltbuf[255];
23065 	struct np3event *npe;
23066 	nsec3param_t *np;
23067 	dns_zone_t *dummy = NULL;
23068 	isc_buffer_t b;
23069 	isc_event_t *e = NULL;
23070 	bool do_lookup = false;
23071 
23072 	REQUIRE(DNS_ZONE_VALID(zone));
23073 
23074 	LOCK_ZONE(zone);
23075 
23076 	/*
23077 	 * First check if the requested NSEC3 parameters are already set,
23078 	 * if so, no need to set again.
23079 	 */
23080 	if (hash != 0) {
23081 		lookup.hash = hash;
23082 		lookup.flags = flags;
23083 		lookup.iterations = iter;
23084 		lookup.salt_length = saltlen;
23085 		lookup.salt = salt;
23086 		param.salt = NULL;
23087 		result = dns__zone_lookup_nsec3param(zone, &lookup, &param,
23088 						     saltbuf, resalt);
23089 		if (result == ISC_R_SUCCESS) {
23090 			UNLOCK_ZONE(zone);
23091 			return (ISC_R_SUCCESS);
23092 		}
23093 		/*
23094 		 * Schedule lookup if lookup above failed (may happen if zone
23095 		 * db is NULL for example).
23096 		 */
23097 		do_lookup = (param.salt == NULL) ? true : false;
23098 	}
23099 
23100 	e = isc_event_allocate(zone->mctx, zone, DNS_EVENT_SETNSEC3PARAM,
23101 			       setnsec3param, zone, sizeof(struct np3event));
23102 
23103 	npe = (struct np3event *)e;
23104 	np = &npe->params;
23105 	np->replace = replace;
23106 	np->resalt = resalt;
23107 	np->lookup = do_lookup;
23108 	if (hash == 0) {
23109 		np->length = 0;
23110 		np->nsec = true;
23111 		dnssec_log(zone, ISC_LOG_DEBUG(3), "setnsec3param:nsec");
23112 	} else {
23113 		param.common.rdclass = zone->rdclass;
23114 		param.common.rdtype = dns_rdatatype_nsec3param;
23115 		ISC_LINK_INIT(&param.common, link);
23116 		param.mctx = NULL;
23117 		/* nsec3 specific param set in dns__zone_lookup_nsec3param() */
23118 		isc_buffer_init(&b, nbuf, sizeof(nbuf));
23119 
23120 		if (param.salt != NULL) {
23121 			CHECK(dns_rdata_fromstruct(&nrdata, zone->rdclass,
23122 						   dns_rdatatype_nsec3param,
23123 						   &param, &b));
23124 			dns_nsec3param_toprivate(&nrdata, &prdata,
23125 						 zone->privatetype, np->data,
23126 						 sizeof(np->data));
23127 			np->length = prdata.length;
23128 		}
23129 
23130 		np->rdata = param;
23131 		np->nsec = false;
23132 
23133 		if (isc_log_wouldlog(dns_lctx, ISC_LOG_DEBUG(3))) {
23134 			unsigned char salttext[255 * 2 + 1];
23135 			if (param.salt != NULL) {
23136 				salt2text(param.salt, param.salt_length,
23137 					  salttext, sizeof(salttext));
23138 			}
23139 			dnssec_log(zone, ISC_LOG_DEBUG(3),
23140 				   "setnsec3param:nsec3 %u %u %u %u:%s",
23141 				   param.hash, param.flags, param.iterations,
23142 				   param.salt_length,
23143 				   param.salt == NULL ? "unknown"
23144 						      : (char *)salttext);
23145 		}
23146 	}
23147 
23148 	/*
23149 	 * setnsec3param() will silently return early if the zone does not yet
23150 	 * have a database.  Prevent that by queueing the event up if zone->db
23151 	 * is NULL.  All events queued here are subsequently processed by
23152 	 * receive_secure_db() if it ever gets called or simply freed by
23153 	 * zone_free() otherwise.
23154 	 */
23155 
23156 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
23157 	if (zone->db != NULL) {
23158 		zone_iattach(zone, &dummy);
23159 		isc_task_send(zone->task, &e);
23160 	} else {
23161 		ISC_LIST_APPEND(zone->setnsec3param_queue, e, ev_link);
23162 		e = NULL;
23163 	}
23164 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
23165 
23166 	result = ISC_R_SUCCESS;
23167 
23168 failure:
23169 	if (e != NULL) {
23170 		isc_event_free(&e);
23171 	}
23172 	UNLOCK_ZONE(zone);
23173 	return (result);
23174 }
23175 
23176 isc_result_t
dns_zone_getloadtime(dns_zone_t * zone,isc_time_t * loadtime)23177 dns_zone_getloadtime(dns_zone_t *zone, isc_time_t *loadtime) {
23178 	REQUIRE(DNS_ZONE_VALID(zone));
23179 	REQUIRE(loadtime != NULL);
23180 
23181 	LOCK_ZONE(zone);
23182 	*loadtime = zone->loadtime;
23183 	UNLOCK_ZONE(zone);
23184 	return (ISC_R_SUCCESS);
23185 }
23186 
23187 isc_result_t
dns_zone_getexpiretime(dns_zone_t * zone,isc_time_t * expiretime)23188 dns_zone_getexpiretime(dns_zone_t *zone, isc_time_t *expiretime) {
23189 	REQUIRE(DNS_ZONE_VALID(zone));
23190 	REQUIRE(expiretime != NULL);
23191 
23192 	LOCK_ZONE(zone);
23193 	*expiretime = zone->expiretime;
23194 	UNLOCK_ZONE(zone);
23195 	return (ISC_R_SUCCESS);
23196 }
23197 
23198 isc_result_t
dns_zone_getrefreshtime(dns_zone_t * zone,isc_time_t * refreshtime)23199 dns_zone_getrefreshtime(dns_zone_t *zone, isc_time_t *refreshtime) {
23200 	REQUIRE(DNS_ZONE_VALID(zone));
23201 	REQUIRE(refreshtime != NULL);
23202 
23203 	LOCK_ZONE(zone);
23204 	*refreshtime = zone->refreshtime;
23205 	UNLOCK_ZONE(zone);
23206 	return (ISC_R_SUCCESS);
23207 }
23208 
23209 isc_result_t
dns_zone_getrefreshkeytime(dns_zone_t * zone,isc_time_t * refreshkeytime)23210 dns_zone_getrefreshkeytime(dns_zone_t *zone, isc_time_t *refreshkeytime) {
23211 	REQUIRE(DNS_ZONE_VALID(zone));
23212 	REQUIRE(refreshkeytime != NULL);
23213 
23214 	LOCK_ZONE(zone);
23215 	*refreshkeytime = zone->refreshkeytime;
23216 	UNLOCK_ZONE(zone);
23217 	return (ISC_R_SUCCESS);
23218 }
23219 
23220 unsigned int
dns_zone_getincludes(dns_zone_t * zone,char *** includesp)23221 dns_zone_getincludes(dns_zone_t *zone, char ***includesp) {
23222 	dns_include_t *include;
23223 	char **array = NULL;
23224 	unsigned int n = 0;
23225 
23226 	REQUIRE(DNS_ZONE_VALID(zone));
23227 	REQUIRE(includesp != NULL && *includesp == NULL);
23228 
23229 	LOCK_ZONE(zone);
23230 	if (zone->nincludes == 0) {
23231 		goto done;
23232 	}
23233 
23234 	array = isc_mem_allocate(zone->mctx, sizeof(char *) * zone->nincludes);
23235 	for (include = ISC_LIST_HEAD(zone->includes); include != NULL;
23236 	     include = ISC_LIST_NEXT(include, link))
23237 	{
23238 		INSIST(n < zone->nincludes);
23239 		array[n++] = isc_mem_strdup(zone->mctx, include->name);
23240 	}
23241 	INSIST(n == zone->nincludes);
23242 	*includesp = array;
23243 
23244 done:
23245 	UNLOCK_ZONE(zone);
23246 	return (n);
23247 }
23248 
23249 void
dns_zone_setstatlevel(dns_zone_t * zone,dns_zonestat_level_t level)23250 dns_zone_setstatlevel(dns_zone_t *zone, dns_zonestat_level_t level) {
23251 	REQUIRE(DNS_ZONE_VALID(zone));
23252 
23253 	zone->statlevel = level;
23254 }
23255 
23256 dns_zonestat_level_t
dns_zone_getstatlevel(dns_zone_t * zone)23257 dns_zone_getstatlevel(dns_zone_t *zone) {
23258 	REQUIRE(DNS_ZONE_VALID(zone));
23259 
23260 	return (zone->statlevel);
23261 }
23262 
23263 static void
setserial(isc_task_t * task,isc_event_t * event)23264 setserial(isc_task_t *task, isc_event_t *event) {
23265 	uint32_t oldserial, desired;
23266 	const char *me = "setserial";
23267 	bool commit = false;
23268 	isc_result_t result;
23269 	dns_dbversion_t *oldver = NULL, *newver = NULL;
23270 	dns_zone_t *zone;
23271 	dns_db_t *db = NULL;
23272 	dns_diff_t diff;
23273 	struct ssevent *sse = (struct ssevent *)event;
23274 	dns_update_log_t log = { update_log_cb, NULL };
23275 	dns_difftuple_t *oldtuple = NULL, *newtuple = NULL;
23276 
23277 	UNUSED(task);
23278 
23279 	zone = event->ev_arg;
23280 	INSIST(DNS_ZONE_VALID(zone));
23281 
23282 	ENTER;
23283 
23284 	if (zone->update_disabled) {
23285 		goto failure;
23286 	}
23287 
23288 	desired = sse->serial;
23289 
23290 	dns_diff_init(zone->mctx, &diff);
23291 
23292 	ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read);
23293 	if (zone->db != NULL) {
23294 		dns_db_attach(zone->db, &db);
23295 	}
23296 	ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read);
23297 	if (db == NULL) {
23298 		goto failure;
23299 	}
23300 
23301 	dns_db_currentversion(db, &oldver);
23302 	result = dns_db_newversion(db, &newver);
23303 	if (result != ISC_R_SUCCESS) {
23304 		dns_zone_log(zone, ISC_LOG_ERROR,
23305 			     "setserial:dns_db_newversion -> %s",
23306 			     dns_result_totext(result));
23307 		goto failure;
23308 	}
23309 
23310 	CHECK(dns_db_createsoatuple(db, oldver, diff.mctx, DNS_DIFFOP_DEL,
23311 				    &oldtuple));
23312 	CHECK(dns_difftuple_copy(oldtuple, &newtuple));
23313 	newtuple->op = DNS_DIFFOP_ADD;
23314 
23315 	oldserial = dns_soa_getserial(&oldtuple->rdata);
23316 	if (desired == 0U) {
23317 		desired = 1;
23318 	}
23319 	if (!isc_serial_gt(desired, oldserial)) {
23320 		if (desired != oldserial) {
23321 			dns_zone_log(zone, ISC_LOG_INFO,
23322 				     "setserial: desired serial (%u) "
23323 				     "out of range (%u-%u)",
23324 				     desired, oldserial + 1,
23325 				     (oldserial + 0x7fffffff));
23326 		}
23327 		goto failure;
23328 	}
23329 
23330 	dns_soa_setserial(desired, &newtuple->rdata);
23331 	CHECK(do_one_tuple(&oldtuple, db, newver, &diff));
23332 	CHECK(do_one_tuple(&newtuple, db, newver, &diff));
23333 	result = dns_update_signatures(&log, zone, db, oldver, newver, &diff,
23334 				       zone->sigvalidityinterval);
23335 	if (result != ISC_R_NOTFOUND) {
23336 		CHECK(result);
23337 	}
23338 
23339 	/* Write changes to journal file. */
23340 	CHECK(zone_journal(zone, &diff, NULL, "setserial"));
23341 	commit = true;
23342 
23343 	LOCK_ZONE(zone);
23344 	zone_needdump(zone, 30);
23345 	UNLOCK_ZONE(zone);
23346 
23347 failure:
23348 	if (oldtuple != NULL) {
23349 		dns_difftuple_free(&oldtuple);
23350 	}
23351 	if (newtuple != NULL) {
23352 		dns_difftuple_free(&newtuple);
23353 	}
23354 	if (oldver != NULL) {
23355 		dns_db_closeversion(db, &oldver, false);
23356 	}
23357 	if (newver != NULL) {
23358 		dns_db_closeversion(db, &newver, commit);
23359 	}
23360 	if (db != NULL) {
23361 		dns_db_detach(&db);
23362 	}
23363 	dns_diff_clear(&diff);
23364 	isc_event_free(&event);
23365 	dns_zone_idetach(&zone);
23366 
23367 	INSIST(oldver == NULL);
23368 	INSIST(newver == NULL);
23369 }
23370 
23371 isc_result_t
dns_zone_setserial(dns_zone_t * zone,uint32_t serial)23372 dns_zone_setserial(dns_zone_t *zone, uint32_t serial) {
23373 	isc_result_t result = ISC_R_SUCCESS;
23374 	dns_zone_t *dummy = NULL;
23375 	isc_event_t *e = NULL;
23376 	struct ssevent *sse;
23377 
23378 	REQUIRE(DNS_ZONE_VALID(zone));
23379 
23380 	LOCK_ZONE(zone);
23381 
23382 	if (!inline_secure(zone)) {
23383 		if (!dns_zone_isdynamic(zone, true)) {
23384 			result = DNS_R_NOTDYNAMIC;
23385 			goto failure;
23386 		}
23387 	}
23388 
23389 	if (zone->update_disabled) {
23390 		result = DNS_R_FROZEN;
23391 		goto failure;
23392 	}
23393 
23394 	e = isc_event_allocate(zone->mctx, zone, DNS_EVENT_SETSERIAL, setserial,
23395 			       zone, sizeof(struct ssevent));
23396 
23397 	sse = (struct ssevent *)e;
23398 	sse->serial = serial;
23399 
23400 	zone_iattach(zone, &dummy);
23401 	isc_task_send(zone->task, &e);
23402 
23403 failure:
23404 	if (e != NULL) {
23405 		isc_event_free(&e);
23406 	}
23407 	UNLOCK_ZONE(zone);
23408 	return (result);
23409 }
23410 
23411 isc_stats_t *
dns_zone_getgluecachestats(dns_zone_t * zone)23412 dns_zone_getgluecachestats(dns_zone_t *zone) {
23413 	REQUIRE(DNS_ZONE_VALID(zone));
23414 
23415 	return (zone->gluecachestats);
23416 }
23417 
23418 bool
dns_zone_isloaded(dns_zone_t * zone)23419 dns_zone_isloaded(dns_zone_t *zone) {
23420 	REQUIRE(DNS_ZONE_VALID(zone));
23421 
23422 	return (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED));
23423 }
23424 
23425 isc_result_t
dns_zone_verifydb(dns_zone_t * zone,dns_db_t * db,dns_dbversion_t * ver)23426 dns_zone_verifydb(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver) {
23427 	dns_dbversion_t *version = NULL;
23428 	dns_keytable_t *secroots = NULL;
23429 	isc_result_t result;
23430 	dns_name_t *origin;
23431 
23432 	const char me[] = "dns_zone_verifydb";
23433 
23434 	REQUIRE(DNS_ZONE_VALID(zone));
23435 	REQUIRE(db != NULL);
23436 
23437 	ENTER;
23438 
23439 	if (dns_zone_gettype(zone) != dns_zone_mirror) {
23440 		return (ISC_R_SUCCESS);
23441 	}
23442 
23443 	if (ver == NULL) {
23444 		dns_db_currentversion(db, &version);
23445 	} else {
23446 		version = ver;
23447 	}
23448 
23449 	if (zone->view != NULL) {
23450 		result = dns_view_getsecroots(zone->view, &secroots);
23451 		if (result != ISC_R_SUCCESS) {
23452 			goto done;
23453 		}
23454 	}
23455 
23456 	origin = dns_db_origin(db);
23457 	result = dns_zoneverify_dnssec(zone, db, version, origin, secroots,
23458 				       zone->mctx, true, false, dnssec_report);
23459 
23460 done:
23461 	if (secroots != NULL) {
23462 		dns_keytable_detach(&secroots);
23463 	}
23464 
23465 	if (ver == NULL) {
23466 		dns_db_closeversion(db, &version, false);
23467 	}
23468 
23469 	if (result != ISC_R_SUCCESS) {
23470 		dnssec_log(zone, ISC_LOG_ERROR, "zone verification failed: %s",
23471 			   isc_result_totext(result));
23472 		result = DNS_R_VERIFYFAILURE;
23473 	}
23474 
23475 	return (result);
23476 }
23477 
23478 static inline dns_ttl_t
zone_nsecttl(dns_zone_t * zone)23479 zone_nsecttl(dns_zone_t *zone) {
23480 	REQUIRE(DNS_ZONE_VALID(zone));
23481 
23482 	return (ISC_MIN(zone->minimum, zone->soattl));
23483 }
23484