/src/bind9/lib/dns/zone.c
Line | Count | Source |
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/async.h> |
21 | | #include <isc/atomic.h> |
22 | | #include <isc/file.h> |
23 | | #include <isc/hash.h> |
24 | | #include <isc/hex.h> |
25 | | #include <isc/list.h> |
26 | | #include <isc/log.h> |
27 | | #include <isc/loop.h> |
28 | | #include <isc/md.h> |
29 | | #include <isc/mutex.h> |
30 | | #include <isc/netmgr.h> |
31 | | #include <isc/os.h> |
32 | | #include <isc/overflow.h> |
33 | | #include <isc/random.h> |
34 | | #include <isc/ratelimiter.h> |
35 | | #include <isc/refcount.h> |
36 | | #include <isc/result.h> |
37 | | #include <isc/rwlock.h> |
38 | | #include <isc/serial.h> |
39 | | #include <isc/stats.h> |
40 | | #include <isc/stdtime.h> |
41 | | #include <isc/strerr.h> |
42 | | #include <isc/string.h> |
43 | | #include <isc/thread.h> |
44 | | #include <isc/tid.h> |
45 | | #include <isc/timer.h> |
46 | | #include <isc/tls.h> |
47 | | #include <isc/util.h> |
48 | | |
49 | | #include <dns/acl.h> |
50 | | #include <dns/adb.h> |
51 | | #include <dns/callbacks.h> |
52 | | #include <dns/catz.h> |
53 | | #include <dns/db.h> |
54 | | #include <dns/dbiterator.h> |
55 | | #include <dns/dlz.h> |
56 | | #include <dns/dnssec.h> |
57 | | #include <dns/dsync.h> |
58 | | #include <dns/journal.h> |
59 | | #include <dns/kasp.h> |
60 | | #include <dns/keydata.h> |
61 | | #include <dns/keymgr.h> |
62 | | #include <dns/keytable.h> |
63 | | #include <dns/keyvalues.h> |
64 | | #include <dns/master.h> |
65 | | #include <dns/masterdump.h> |
66 | | #include <dns/message.h> |
67 | | #include <dns/name.h> |
68 | | #include <dns/notify.h> |
69 | | #include <dns/nsec.h> |
70 | | #include <dns/nsec3.h> |
71 | | #include <dns/opcode.h> |
72 | | #include <dns/peer.h> |
73 | | #include <dns/private.h> |
74 | | #include <dns/rcode.h> |
75 | | #include <dns/rdata.h> |
76 | | #include <dns/rdataclass.h> |
77 | | #include <dns/rdatalist.h> |
78 | | #include <dns/rdataset.h> |
79 | | #include <dns/rdatasetiter.h> |
80 | | #include <dns/rdatastruct.h> |
81 | | #include <dns/rdatatype.h> |
82 | | #include <dns/remote.h> |
83 | | #include <dns/request.h> |
84 | | #include <dns/resolver.h> |
85 | | #include <dns/rriterator.h> |
86 | | #include <dns/skr.h> |
87 | | #include <dns/soa.h> |
88 | | #include <dns/ssu.h> |
89 | | #include <dns/stats.h> |
90 | | #include <dns/time.h> |
91 | | #include <dns/tsig.h> |
92 | | #include <dns/ttl.h> |
93 | | #include <dns/unreachcache.h> |
94 | | #include <dns/update.h> |
95 | | #include <dns/xfrin.h> |
96 | | #include <dns/zone.h> |
97 | | #include <dns/zonefetch.h> |
98 | | #include <dns/zonemgr.h> |
99 | | #include <dns/zoneproperties.h> |
100 | | #include <dns/zoneverify.h> |
101 | | #include <dns/zt.h> |
102 | | |
103 | | #include <dst/dst.h> |
104 | | |
105 | | #include "zone_p.h" |
106 | | |
107 | | /*% |
108 | | * Ensure 'a' is at least 'min' but not more than 'max'. |
109 | | */ |
110 | 6 | #define RANGE(a, min, max) (((a) < (min)) ? (min) : ((a) < (max) ? (a) : (max))) |
111 | | |
112 | 0 | #define NSEC3REMOVE(x) (((x) & DNS_NSEC3FLAG_REMOVE) != 0) |
113 | | |
114 | | /*% |
115 | | * Key flags |
116 | | */ |
117 | 0 | #define REVOKE(x) ((dst_key_flags(x) & DNS_KEYFLAG_REVOKE) != 0) |
118 | 0 | #define KSK(x) ((dst_key_flags(x) & DNS_KEYFLAG_KSK) != 0) |
119 | 0 | #define ZONEKEY(x) ((dst_key_flags(x) & DNS_KEYOWNER_ZONE) != 0) |
120 | 0 | #define ID(x) dst_key_id(x) |
121 | 0 | #define ALG(x) dst_key_alg(x) |
122 | | |
123 | | /*% |
124 | | * KASP flags |
125 | | */ |
126 | | #define KASP_LOCK(k) \ |
127 | 0 | if ((k) != NULL) { \ |
128 | 0 | LOCK(&((k)->lock)); \ |
129 | 0 | } |
130 | | |
131 | | #define KASP_UNLOCK(k) \ |
132 | 0 | if ((k) != NULL) { \ |
133 | 0 | UNLOCK(&((k)->lock)); \ |
134 | 0 | } |
135 | | |
136 | | typedef struct dns_stub dns_stub_t; |
137 | | typedef struct dns_load dns_load_t; |
138 | | typedef struct dns_asyncload dns_asyncload_t; |
139 | | |
140 | | #ifdef ENABLE_AFL |
141 | | extern bool dns_fuzzing_resolver; |
142 | | #endif /* ifdef ENABLE_AFL */ |
143 | | |
144 | | /*% |
145 | | * Key file I/O lock pool. |
146 | | */ |
147 | | typedef struct dns_keymgmt_bucket { |
148 | | isc_mutex_t lock; |
149 | | uint8_t __padding[ISC_OS_CACHELINE_SIZE - |
150 | | sizeof(isc_mutex_t) % ISC_OS_CACHELINE_SIZE]; |
151 | | } dns_keymgmt_bucket_t; |
152 | | |
153 | | static dns_keymgmt_bucket_t keymgmt_buckets_g[1024]; |
154 | | |
155 | | #define zonediff_init(z, d) \ |
156 | 0 | do { \ |
157 | 0 | dns__zonediff_t *_z = (z); \ |
158 | 0 | (_z)->diff = (d); \ |
159 | 0 | (_z)->offline = false; \ |
160 | 0 | } while (0) |
161 | | |
162 | | /* Flags for zone_load() */ |
163 | | typedef enum { |
164 | | DNS_ZONELOADFLAG_NOSTAT = 0x00000001U, /* Do not stat() master files */ |
165 | | DNS_ZONELOADFLAG_THAW = 0x00000002U, /* Thaw the zone on successful |
166 | | * load. */ |
167 | | } dns_zoneloadflag_t; |
168 | | |
169 | | /*% |
170 | | * dns_stub holds state while performing a 'stub' transfer. |
171 | | * 'db' is the zone's 'db' or a new one if this is the initial |
172 | | * transfer. |
173 | | */ |
174 | | |
175 | | struct dns_stub { |
176 | | unsigned int magic; |
177 | | isc_mem_t *mctx; |
178 | | dns_zone_t *zone; |
179 | | dns_db_t *db; |
180 | | dns_dbversion_t *version; |
181 | | atomic_uint_fast32_t pending_requests; |
182 | | }; |
183 | | |
184 | | /*% |
185 | | * Hold load state. |
186 | | */ |
187 | | struct dns_load { |
188 | | dns_zone_t *zone; |
189 | | dns_db_t *db; |
190 | | isc_time_t loadtime; |
191 | | dns_rdatacallbacks_t callbacks; |
192 | | }; |
193 | | |
194 | | /*% |
195 | | * Hold state for an asynchronous load |
196 | | */ |
197 | | struct dns_asyncload { |
198 | | dns_zone_t *zone; |
199 | | unsigned int flags; |
200 | | dns_zt_callback_t *loaded; |
201 | | void *loaded_arg; |
202 | | }; |
203 | | |
204 | | /* |
205 | | * These can be overridden by the -T mkeytimers option on the command |
206 | | * line, so that we can test with shorter periods than specified in |
207 | | * RFC 5011. |
208 | | */ |
209 | | #define HOUR 3600 |
210 | | #define DAY (24 * HOUR) |
211 | | #define MONTH (30 * DAY) |
212 | | unsigned int dns_zone_mkey_hour = HOUR; |
213 | | unsigned int dns_zone_mkey_day = DAY; |
214 | | unsigned int dns_zone_mkey_month = MONTH; |
215 | | |
216 | 0 | #define SEND_BUFFER_SIZE 2048 |
217 | | |
218 | | static void |
219 | | zone_timer_set(dns_zone_t *zone, isc_time_t *next, isc_time_t *now); |
220 | | |
221 | | static void |
222 | | cancel_refresh(dns_zone_t *); |
223 | | static void |
224 | | zone_debuglogc(dns_zone_t *zone, isc_logcategory_t category, const char *me, |
225 | | int debuglevel, const char *fmt, ...); |
226 | | static void |
227 | | zone_debuglog(dns_zone_t *zone, const char *, int debuglevel, const char *msg, |
228 | | ...) ISC_FORMAT_PRINTF(4, 5); |
229 | | static void |
230 | | dnssec_log(dns_zone_t *zone, int level, const char *fmt, ...) |
231 | | ISC_FORMAT_PRINTF(3, 4); |
232 | | |
233 | | typedef struct nsec3param nsec3param_t; |
234 | | |
235 | | static void |
236 | | queue_xfrin(dns_zone_t *zone); |
237 | | static isc_result_t |
238 | | update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff, |
239 | | dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl, |
240 | | dns_rdata_t *rdata); |
241 | | static void |
242 | | zone_unload(dns_zone_t *zone); |
243 | | static void |
244 | | zone_expire(dns_zone_t *zone); |
245 | | static void |
246 | | zone_refresh(dns_zone_t *zone); |
247 | | static void |
248 | | zone_iattach(dns_zone_t *source, dns_zone_t **target); |
249 | | static void |
250 | | zone_idetach(dns_zone_t **zonep); |
251 | | static isc_result_t |
252 | | zone_replacedb(dns_zone_t *zone, dns_db_t *db, bool dump); |
253 | | static void |
254 | | zone_attachdb(dns_zone_t *zone, dns_db_t *db); |
255 | | static void |
256 | | zone_detachdb(dns_zone_t *zone); |
257 | | static void |
258 | | zone_catz_enable(dns_zone_t *zone, dns_catz_zones_t *catzs); |
259 | | static void |
260 | | zone_catz_disable(dns_zone_t *zone); |
261 | | static isc_result_t |
262 | | zone_postload(dns_zone_t *zone, dns_db_t *db, isc_time_t loadtime, |
263 | | isc_result_t result); |
264 | | static void |
265 | | zone_needdump(dns_zone_t *zone, unsigned int delay); |
266 | | static void |
267 | | zone_shutdown(void *arg); |
268 | | static void |
269 | | zone_loaddone(void *arg, isc_result_t result); |
270 | | static isc_result_t |
271 | | zone_startload(dns_db_t *db, dns_zone_t *zone, isc_time_t loadtime); |
272 | | static void |
273 | | zone_namerd_tostr(dns_zone_t *zone, char *buf, size_t length); |
274 | | static void |
275 | | zone_viewname_tostr(dns_zone_t *zone, char *buf, size_t length); |
276 | | static void |
277 | | zone_schedule_inline_sync(dns_zone_t *zone, inline_sync_phase_t state); |
278 | | static void |
279 | | refresh_callback(void *arg); |
280 | | static void |
281 | | stub_callback(void *arg); |
282 | | static void |
283 | | queue_soa_query(dns_zone_t *zone); |
284 | | static void |
285 | | soa_query(void *arg); |
286 | | static void |
287 | | ns_query(dns_zone_t *zone, dns_rdataset_t *soardataset, dns_stub_t *stub); |
288 | | static int |
289 | | message_count(dns_message_t *msg, dns_section_t section, dns_rdatatype_t type); |
290 | | static void |
291 | | checkds_cancel(dns_zone_t *zone); |
292 | | static void |
293 | | checkds_find_address(dns_checkds_t *checkds); |
294 | | static void |
295 | | checkds_send(dns_zone_t *zone); |
296 | | static void |
297 | | checkds_createmessage(dns_zone_t *zone, dns_message_t **messagep); |
298 | | static void |
299 | | checkds_done(void *arg); |
300 | | static void |
301 | | checkds_send_tons(dns_checkds_t *checkds); |
302 | | static void |
303 | | checkds_send_toaddr(void *arg); |
304 | | static isc_result_t |
305 | | zone_dump(dns_zone_t *, bool); |
306 | | static void |
307 | | rss_post(dns_zone_t *zone, nsec3param_t *np); |
308 | | static void |
309 | | zone_process_maintenance_request(dns_zone_t *zone); |
310 | | |
311 | | static isc_result_t |
312 | | zone_get_from_db(dns_zone_t *zone, dns_db_t *db, unsigned int *nscount, |
313 | | unsigned int *soacount, uint32_t *soattl, uint32_t *serial, |
314 | | uint32_t *refresh, uint32_t *retry, uint32_t *expire, |
315 | | uint32_t *minimum, unsigned int *errors); |
316 | | |
317 | | static void |
318 | | forward_callback(void *arg); |
319 | | static void |
320 | | zone_saveunique(dns_zone_t *zone, const char *path, const char *templat); |
321 | | static void |
322 | | zone_maintenance(dns_zone_t *zone); |
323 | | |
324 | | static void |
325 | | inline_sync_run(dns_zone_t *zone); |
326 | | static void |
327 | | inline_sync_resume(dns_zone_t *zone); |
328 | | static void |
329 | | zone_clear_inline_sync(dns_zone_t *zone); |
330 | | static void |
331 | | inline_secure_bootstrap(dns_zone_t *zone); |
332 | | static isc_result_t |
333 | | secure_db_create_from_raw(dns_zone_t *zone, dns_db_t *rawdb, dns_db_t **dbp); |
334 | | static void |
335 | | zone_notify(dns_zone_t *zone, isc_time_t *now); |
336 | | static void |
337 | | zone_notifycds(dns_zone_t *zone); |
338 | | static void |
339 | | dump_done(void *arg, isc_result_t result); |
340 | | static isc_result_t |
341 | | zone_signwithkey(dns_zone_t *zone, dst_algorithm_t algorithm, uint16_t keyid, |
342 | | bool deleteit, bool fullsign); |
343 | | static isc_result_t |
344 | | delete_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_dbnode_t *node, |
345 | | dns_name_t *name, dns_diff_t *diff); |
346 | | static void |
347 | | zone_rekey(dns_zone_t *zone); |
348 | | static dns_ttl_t |
349 | | zone_nsecttl(dns_zone_t *zone); |
350 | | static void |
351 | | zone_journal_compact(dns_zone_t *zone, dns_db_t *db, uint32_t serial); |
352 | | static isc_result_t |
353 | | zone_journal_rollforward(dns_zone_t *zone, dns_db_t *db, bool *needdump, |
354 | | bool *fixjournal); |
355 | 4 | #define ENTER zone_debuglog(zone, __func__, 1, "enter") |
356 | | |
357 | | static const unsigned int dbargc_default = 1; |
358 | | static const char *dbargv_default[] = { ZONEDB_DEFAULT }; |
359 | | |
360 | | #define DNS_ZONE_JITTER_ADD(a, b, c) \ |
361 | 0 | do { \ |
362 | 0 | isc_interval_t _i; \ |
363 | 0 | uint32_t _j; \ |
364 | 0 | _j = (b) - isc_random_uniform((b) / 4); \ |
365 | 0 | isc_interval_set(&_i, _j, 0); \ |
366 | 0 | if (isc_time_add((a), &_i, (c)) != ISC_R_SUCCESS) { \ |
367 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, \ |
368 | 0 | "epoch approaching: upgrade required: " \ |
369 | 0 | "now + %s failed", \ |
370 | 0 | #b); \ |
371 | 0 | isc_interval_set(&_i, _j / 2, 0); \ |
372 | 0 | (void)isc_time_add((a), &_i, (c)); \ |
373 | 0 | } \ |
374 | 0 | } while (0) |
375 | | |
376 | | #define DNS_ZONE_TIME_ADD(a, b, c) \ |
377 | 0 | do { \ |
378 | 0 | isc_interval_t _i; \ |
379 | 0 | isc_interval_set(&_i, (b), 0); \ |
380 | 0 | if (isc_time_add((a), &_i, (c)) != ISC_R_SUCCESS) { \ |
381 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, \ |
382 | 0 | "epoch approaching: upgrade required: " \ |
383 | 0 | "now + %s failed", \ |
384 | 0 | #b); \ |
385 | 0 | isc_interval_set(&_i, (b) / 2, 0); \ |
386 | 0 | (void)isc_time_add((a), &_i, (c)); \ |
387 | 0 | } \ |
388 | 0 | } while (0) |
389 | | |
390 | | #define DNS_ZONE_TIME_SUBTRACT(a, b, c) \ |
391 | 0 | do { \ |
392 | 0 | isc_interval_t _i; \ |
393 | 0 | isc_interval_set(&_i, (b), 0); \ |
394 | 0 | if (isc_time_subtract((a), &_i, (c)) != ISC_R_SUCCESS) { \ |
395 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, \ |
396 | 0 | "epoch approaching: upgrade required: " \ |
397 | 0 | "isc_time_subtract() failed"); \ |
398 | 0 | isc_interval_set(&_i, (b) / 2, 0); \ |
399 | 0 | (void)isc_time_subtract((a), &_i, (c)); \ |
400 | 0 | } \ |
401 | 0 | } while (0) |
402 | | |
403 | | struct nsec3param { |
404 | | dns_rdata_nsec3param_t rdata; |
405 | | unsigned char data[DNS_NSEC3PARAM_BUFFERSIZE + 1]; |
406 | | unsigned int length; |
407 | | bool nsec; |
408 | | bool replace; |
409 | | bool resalt; |
410 | | bool lookup; |
411 | | ISC_LINK(nsec3param_t) link; |
412 | | }; |
413 | | typedef ISC_LIST(nsec3param_t) nsec3paramlist_t; |
414 | | |
415 | 0 | #define OLD_SIGNING_RECORD_SIZE 5 |
416 | 0 | #define SIGNING_RECORD_SIZE 7 |
417 | | |
418 | | typedef enum zone_maintenance_request_type { |
419 | | zone_maintenance_request_setnsec3param, |
420 | | zone_maintenance_request_keydone, |
421 | | zone_maintenance_request_setserial, |
422 | | } zone_maintenance_request_type_t; |
423 | | |
424 | | typedef struct zone_maintenance_request { |
425 | | ISC_LINK(struct zone_maintenance_request) link; |
426 | | zone_maintenance_request_type_t type; |
427 | | union { |
428 | | nsec3param_t nsec3param; |
429 | | struct { |
430 | | bool all; |
431 | | unsigned char data[SIGNING_RECORD_SIZE]; |
432 | | } keydone; |
433 | | struct { |
434 | | uint32_t serial; |
435 | | } setserial; |
436 | | } u; |
437 | | } zone_maintenance_request_t; |
438 | | |
439 | | struct stub_cb_args { |
440 | | dns_stub_t *stub; |
441 | | dns_tsigkey_t *tsig_key; |
442 | | uint16_t udpsize; |
443 | | unsigned int connect_timeout; |
444 | | unsigned int timeout; |
445 | | bool reqnsid; |
446 | | }; |
447 | | |
448 | | struct stub_glue_request { |
449 | | dns_request_t *request; |
450 | | dns_name_t name; |
451 | | struct stub_cb_args *args; |
452 | | bool ipv4; |
453 | | }; |
454 | | |
455 | | /*% |
456 | | * Increment resolver-related statistics counters. Zone must be locked. |
457 | | */ |
458 | | static void |
459 | 0 | inc_stats(dns_zone_t *zone, isc_statscounter_t counter) { |
460 | 0 | if (zone->stats != NULL) { |
461 | 0 | isc_stats_increment(zone->stats, counter); |
462 | 0 | } |
463 | 0 | } |
464 | | |
465 | | /*** |
466 | | *** Public functions. |
467 | | ***/ |
468 | | |
469 | | void |
470 | 2 | dns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx, isc_tid_t tid) { |
471 | 2 | isc_time_t now; |
472 | 2 | dns_zone_t *zone = NULL; |
473 | | |
474 | 2 | REQUIRE(zonep != NULL && *zonep == NULL); |
475 | 2 | REQUIRE(mctx != NULL); |
476 | | |
477 | 2 | now = isc_time_now(); |
478 | 2 | zone = isc_mem_get(mctx, sizeof(*zone)); |
479 | 2 | *zone = (dns_zone_t){ |
480 | 2 | .masterformat = dns_masterformat_none, |
481 | 2 | .journalsize = -1, |
482 | 2 | .rdclass = dns_rdataclass_none, |
483 | 2 | .type = dns_zone_none, |
484 | 2 | .refresh = DNS_ZONE_DEFAULTREFRESH, |
485 | 2 | .retry = DNS_ZONE_DEFAULTRETRY, |
486 | 2 | .maxrefresh = DNS_ZONE_MAXREFRESH, |
487 | 2 | .minrefresh = DNS_ZONE_MINREFRESH, |
488 | 2 | .maxretry = DNS_ZONE_MAXRETRY, |
489 | 2 | .minretry = DNS_ZONE_MINRETRY, |
490 | 2 | .checkdstype = dns_checkdstype_yes, |
491 | 2 | .zero_no_soa_ttl = true, |
492 | 2 | .check_names = dns_severity_ignore, |
493 | 2 | .idlein = DNS_DEFAULT_IDLEIN, |
494 | 2 | .idleout = DNS_DEFAULT_IDLEOUT, |
495 | 2 | .maxxfrin = MAX_XFER_TIME, |
496 | 2 | .maxxfrout = MAX_XFER_TIME, |
497 | 2 | .sigvalidityinterval = 30 * 24 * 3600, |
498 | 2 | .sigresigninginterval = 7 * 24 * 3600, |
499 | 2 | .statlevel = dns_zonestat_none, |
500 | 2 | .signatures = 10, |
501 | 2 | .nodes = 100, |
502 | 2 | .privatetype = (dns_rdatatype_t)0xffffU, |
503 | 2 | .rpz_num = DNS_RPZ_INVALID_NUM, |
504 | 2 | .requestixfr = true, |
505 | 2 | .ixfr_ratio = 100, |
506 | 2 | .requestexpire = true, |
507 | 2 | .updatemethod = dns_updatemethod_increment, |
508 | 2 | .tid = tid, |
509 | 2 | .notifytime = now, |
510 | 2 | .newincludes = ISC_LIST_INITIALIZER, |
511 | 2 | .checkds_requests = ISC_LIST_INITIALIZER, |
512 | 2 | .signing = ISC_LIST_INITIALIZER, |
513 | 2 | .nsec3chain = ISC_LIST_INITIALIZER, |
514 | 2 | .maintenance_queue = ISC_LIST_INITIALIZER, |
515 | 2 | .forwards = ISC_LIST_INITIALIZER, |
516 | 2 | .link = ISC_LINK_INITIALIZER, |
517 | 2 | .statelink = ISC_LINK_INITIALIZER, |
518 | 2 | }; |
519 | 2 | dns_remote_t r = { |
520 | 2 | .magic = DNS_REMOTE_MAGIC, |
521 | 2 | }; |
522 | | |
523 | 2 | isc_mem_attach(mctx, &zone->mctx); |
524 | 2 | isc_mutex_init(&zone->lock); |
525 | 2 | ZONEDB_INITLOCK(&zone->dblock); |
526 | | |
527 | 2 | isc_refcount_init(&zone->references, 1); |
528 | 2 | isc_refcount_init(&zone->irefs, 0); |
529 | 2 | dns_name_init(&zone->origin); |
530 | 2 | isc_sockaddr_any(&zone->parentalsrc4); |
531 | 2 | isc_sockaddr_any6(&zone->parentalsrc6); |
532 | 2 | isc_sockaddr_any(&zone->xfrsource4); |
533 | 2 | isc_sockaddr_any6(&zone->xfrsource6); |
534 | | |
535 | 2 | zone->primaries = r; |
536 | 2 | zone->parentals = r; |
537 | 2 | zone->alsonotify = r; |
538 | 2 | zone->cds_endpoints = r; |
539 | 2 | zone->defaultkasp = NULL; |
540 | 2 | ISC_LIST_INIT(zone->keyring); |
541 | | |
542 | 2 | dns_notifyctx_init(&zone->notifysoa, dns_rdatatype_soa); |
543 | 2 | dns_notifyctx_init(&zone->notifycds, dns_rdatatype_cds); |
544 | | |
545 | 2 | isc_stats_create(mctx, &zone->gluecachestats, |
546 | 2 | dns_gluecachestatscounter_max); |
547 | | |
548 | 2 | zone->magic = ZONE_MAGIC; |
549 | | |
550 | | /* Must be after magic is set. */ |
551 | 2 | dns_zone_setdbtype(zone, dbargc_default, dbargv_default); |
552 | | |
553 | 2 | *zonep = zone; |
554 | 2 | } |
555 | | |
556 | | static void |
557 | 0 | clear_keylist(dns_dnsseckeylist_t *list, isc_mem_t *mctx) { |
558 | 0 | ISC_LIST_FOREACH(*list, key, link) { |
559 | 0 | ISC_LIST_UNLINK(*list, key, link); |
560 | 0 | dns_dnsseckey_destroy(mctx, &key); |
561 | 0 | } |
562 | 0 | } |
563 | | |
564 | | void |
565 | 0 | dns__zone_free(dns_zone_t *zone) { |
566 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
567 | 0 | REQUIRE(!LOCKED_ZONE(zone)); |
568 | 0 | REQUIRE(zone->timer == NULL); |
569 | 0 | REQUIRE(zone->zmgr == NULL); |
570 | |
|
571 | 0 | dns_zone_unloadplugins(zone); |
572 | |
|
573 | 0 | isc_refcount_destroy(&zone->references); |
574 | 0 | isc_refcount_destroy(&zone->irefs); |
575 | | |
576 | | /* |
577 | | * Managed objects. Order is important. |
578 | | */ |
579 | 0 | if (zone->request != NULL) { |
580 | 0 | dns_request_destroy(&zone->request); /* XXXMPA */ |
581 | 0 | } |
582 | 0 | INSIST(zone->statelist == NULL); |
583 | 0 | INSIST(zone->view == NULL); |
584 | 0 | INSIST(zone->prev_view == NULL); |
585 | | |
586 | | /* Unmanaged objects */ |
587 | 0 | ISC_LIST_FOREACH(zone->maintenance_queue, request, link) { |
588 | 0 | ISC_LIST_UNLINK(zone->maintenance_queue, request, link); |
589 | 0 | isc_mem_put(zone->mctx, request, sizeof(*request)); |
590 | 0 | } |
591 | |
|
592 | 0 | ISC_LIST_FOREACH(zone->signing, signing, link) { |
593 | 0 | ISC_LIST_UNLINK(zone->signing, signing, link); |
594 | 0 | dns_db_detach(&signing->db); |
595 | 0 | dns_dbiterator_destroy(&signing->dbiterator); |
596 | 0 | isc_mem_put(zone->mctx, signing, sizeof *signing); |
597 | 0 | } |
598 | |
|
599 | 0 | ISC_LIST_FOREACH(zone->nsec3chain, nsec3chain, link) { |
600 | 0 | ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain, link); |
601 | 0 | dns_db_detach(&nsec3chain->db); |
602 | 0 | dns_dbiterator_destroy(&nsec3chain->dbiterator); |
603 | 0 | isc_mem_put(zone->mctx, nsec3chain, sizeof *nsec3chain); |
604 | 0 | } |
605 | |
|
606 | 0 | ISC_LIST_FOREACH(zone->includes, include, link) { |
607 | 0 | ISC_LIST_UNLINK(zone->includes, include, link); |
608 | 0 | isc_mem_free(zone->mctx, include->name); |
609 | 0 | isc_mem_put(zone->mctx, include, sizeof *include); |
610 | 0 | } |
611 | |
|
612 | 0 | ISC_LIST_FOREACH(zone->newincludes, include, link) { |
613 | 0 | ISC_LIST_UNLINK(zone->newincludes, include, link); |
614 | 0 | isc_mem_free(zone->mctx, include->name); |
615 | 0 | isc_mem_put(zone->mctx, include, sizeof *include); |
616 | 0 | } |
617 | |
|
618 | 0 | zone_clear_inline_sync(zone); |
619 | 0 | INSIST(zone->iss == NULL); |
620 | |
|
621 | 0 | if (zone->masterfile != NULL) { |
622 | 0 | isc_mem_free(zone->mctx, zone->masterfile); |
623 | 0 | } |
624 | 0 | if (zone->initfile != NULL) { |
625 | 0 | isc_mem_free(zone->mctx, zone->initfile); |
626 | 0 | } |
627 | 0 | if (zone->keydirectory != NULL) { |
628 | 0 | isc_mem_free(zone->mctx, zone->keydirectory); |
629 | 0 | } |
630 | |
|
631 | 0 | if (zone->kasp != NULL) { |
632 | 0 | dns_kasp_detach(&zone->kasp); |
633 | 0 | } |
634 | 0 | if (zone->defaultkasp != NULL) { |
635 | 0 | dns_kasp_detach(&zone->defaultkasp); |
636 | 0 | } |
637 | 0 | if (!ISC_LIST_EMPTY(zone->keyring)) { |
638 | 0 | clear_keylist(&zone->keyring, zone->mctx); |
639 | 0 | } |
640 | 0 | if (!ISC_LIST_EMPTY(zone->checkds_ok)) { |
641 | 0 | clear_keylist(&zone->checkds_ok, zone->mctx); |
642 | 0 | } |
643 | 0 | if (zone->skr != NULL) { |
644 | 0 | zone->skrbundle = NULL; |
645 | 0 | dns_skr_detach(&zone->skr); |
646 | 0 | } |
647 | |
|
648 | 0 | zone->journalsize = -1; |
649 | 0 | if (zone->journal != NULL) { |
650 | 0 | isc_mem_free(zone->mctx, zone->journal); |
651 | 0 | } |
652 | 0 | if (zone->stats != NULL) { |
653 | 0 | isc_stats_detach(&zone->stats); |
654 | 0 | } |
655 | 0 | if (zone->requeststats != NULL) { |
656 | 0 | isc_stats_detach(&zone->requeststats); |
657 | 0 | } |
658 | 0 | if (zone->rcvquerystats != NULL) { |
659 | 0 | isc_statsmulti_detach(&zone->rcvquerystats); |
660 | 0 | } |
661 | 0 | if (zone->dnssecsignstats != NULL) { |
662 | 0 | dns_stats_detach(&zone->dnssecsignstats); |
663 | 0 | } |
664 | 0 | if (zone->db != NULL) { |
665 | 0 | zone_detachdb(zone); |
666 | 0 | } |
667 | 0 | if (zone->rpzs != NULL) { |
668 | 0 | REQUIRE(zone->rpz_num < zone->rpzs->p.num_zones); |
669 | 0 | dns_rpz_zones_detach(&zone->rpzs); |
670 | 0 | zone->rpz_num = DNS_RPZ_INVALID_NUM; |
671 | 0 | } |
672 | 0 | if (zone->catzs != NULL) { |
673 | 0 | dns_catz_zones_detach(&zone->catzs); |
674 | 0 | } |
675 | 0 | dns__zone_freedbargs(zone); |
676 | |
|
677 | 0 | dns_zone_setparentals(zone, NULL, NULL, NULL, NULL, 0); |
678 | 0 | dns_zone_setprimaries(zone, NULL, NULL, NULL, NULL, 0); |
679 | 0 | dns_zone_setalsonotify(zone, NULL, NULL, NULL, NULL, 0); |
680 | 0 | dns_zone_setcdsendpoints(zone, NULL, NULL, NULL, NULL, 0); |
681 | |
|
682 | 0 | zone->check_names = dns_severity_ignore; |
683 | 0 | if (zone->update_acl != NULL) { |
684 | 0 | dns_acl_detach(&zone->update_acl); |
685 | 0 | } |
686 | 0 | if (zone->forward_acl != NULL) { |
687 | 0 | dns_acl_detach(&zone->forward_acl); |
688 | 0 | } |
689 | 0 | if (zone->notifysoa.notify_acl != NULL) { |
690 | 0 | dns_acl_detach(&zone->notifysoa.notify_acl); |
691 | 0 | } |
692 | 0 | if (zone->notifycds.notify_acl != NULL) { |
693 | 0 | dns_acl_detach(&zone->notifycds.notify_acl); |
694 | 0 | } |
695 | 0 | if (zone->query_acl != NULL) { |
696 | 0 | dns_acl_detach(&zone->query_acl); |
697 | 0 | } |
698 | 0 | if (zone->queryon_acl != NULL) { |
699 | 0 | dns_acl_detach(&zone->queryon_acl); |
700 | 0 | } |
701 | 0 | if (zone->xfr_acl != NULL) { |
702 | 0 | dns_acl_detach(&zone->xfr_acl); |
703 | 0 | } |
704 | 0 | if (dns_name_dynamic(&zone->origin)) { |
705 | 0 | dns_name_free(&zone->origin, zone->mctx); |
706 | 0 | } |
707 | |
|
708 | 0 | dns_zone_setrad(zone, NULL); |
709 | |
|
710 | 0 | if (zone->strnamerd != NULL) { |
711 | 0 | isc_mem_free(zone->mctx, zone->strnamerd); |
712 | 0 | } |
713 | 0 | if (zone->strname != NULL) { |
714 | 0 | isc_mem_free(zone->mctx, zone->strname); |
715 | 0 | } |
716 | 0 | if (zone->strrdclass != NULL) { |
717 | 0 | isc_mem_free(zone->mctx, zone->strrdclass); |
718 | 0 | } |
719 | 0 | if (zone->strviewname != NULL) { |
720 | 0 | isc_mem_free(zone->mctx, zone->strviewname); |
721 | 0 | } |
722 | 0 | if (zone->ssutable != NULL) { |
723 | 0 | dns_ssutable_detach(&zone->ssutable); |
724 | 0 | } |
725 | 0 | if (zone->gluecachestats != NULL) { |
726 | 0 | isc_stats_detach(&zone->gluecachestats); |
727 | 0 | } |
728 | | |
729 | | /* last stuff */ |
730 | 0 | ZONEDB_DESTROYLOCK(&zone->dblock); |
731 | 0 | isc_mutex_destroy(&zone->lock); |
732 | 0 | zone->magic = 0; |
733 | 0 | isc_mem_putanddetach(&zone->mctx, zone, sizeof(*zone)); |
734 | 0 | } |
735 | | |
736 | | /* |
737 | | * Returns true iff this the signed side of an inline-signing zone. |
738 | | * Caller should hold zone lock. |
739 | | */ |
740 | | bool |
741 | 18 | dns__zone_inline_secure(dns_zone_t *zone) { |
742 | 18 | REQUIRE(DNS_ZONE_VALID(zone)); |
743 | 18 | REQUIRE(LOCKED_ZONE(zone)); |
744 | 18 | if (zone->raw != NULL) { |
745 | 0 | return true; |
746 | 0 | } |
747 | 18 | return false; |
748 | 18 | } |
749 | | |
750 | | /* |
751 | | * Returns true iff this the unsigned side of an inline-signing zone |
752 | | * Caller should hold zone lock. |
753 | | */ |
754 | | bool |
755 | 14 | dns__zone_inline_raw(dns_zone_t *zone) { |
756 | 14 | REQUIRE(DNS_ZONE_VALID(zone)); |
757 | 14 | REQUIRE(LOCKED_ZONE(zone)); |
758 | 14 | if (zone->secure != NULL) { |
759 | 0 | return true; |
760 | 0 | } |
761 | 14 | return false; |
762 | 14 | } |
763 | | |
764 | | isc_result_t |
765 | 0 | dns_zone_getserial(dns_zone_t *zone, uint32_t *serialp) { |
766 | 0 | isc_result_t result; |
767 | 0 | unsigned int soacount; |
768 | |
|
769 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
770 | 0 | REQUIRE(serialp != NULL); |
771 | |
|
772 | 0 | LOCK_ZONE(zone); |
773 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
774 | 0 | if (zone->db != NULL) { |
775 | 0 | result = zone_get_from_db(zone, zone->db, NULL, &soacount, NULL, |
776 | 0 | serialp, NULL, NULL, NULL, NULL, |
777 | 0 | NULL); |
778 | 0 | if (result == ISC_R_SUCCESS && soacount == 0) { |
779 | 0 | result = ISC_R_FAILURE; |
780 | 0 | } |
781 | 0 | } else { |
782 | 0 | result = DNS_R_NOTLOADED; |
783 | 0 | } |
784 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
785 | 0 | UNLOCK_ZONE(zone); |
786 | |
|
787 | 0 | return result; |
788 | 0 | } |
789 | | |
790 | | isc_result_t |
791 | 0 | dns_zone_getzoneversion(dns_zone_t *zone, isc_buffer_t *b) { |
792 | 0 | isc_result_t result = DNS_R_NOTLOADED; |
793 | 0 | unsigned int soacount; |
794 | 0 | uint32_t serial; |
795 | 0 | dns_zone_t *mayberaw = zone; |
796 | |
|
797 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
798 | 0 | REQUIRE(b != NULL); |
799 | |
|
800 | 0 | LOCK_ZONE(zone); |
801 | 0 | if (zone->raw != NULL) { |
802 | 0 | LOCK_ZONE(zone->raw); |
803 | 0 | mayberaw = zone->raw; |
804 | 0 | } |
805 | 0 | ZONEDB_LOCK(&mayberaw->dblock, isc_rwlocktype_read); |
806 | 0 | if (DNS_ZONE_OPTION(mayberaw, DNS_ZONEOPT_ZONEVERSION) && |
807 | 0 | mayberaw->db != NULL) |
808 | 0 | { |
809 | 0 | result = dns_db_getzoneversion(mayberaw->db, b); |
810 | 0 | if (result == ISC_R_NOTIMPLEMENTED) { |
811 | 0 | result = zone_get_from_db(mayberaw, mayberaw->db, NULL, |
812 | 0 | &soacount, NULL, &serial, |
813 | 0 | NULL, NULL, NULL, NULL, NULL); |
814 | 0 | if (result == ISC_R_SUCCESS && soacount == 0) { |
815 | 0 | result = ISC_R_FAILURE; |
816 | 0 | } |
817 | 0 | if (result == ISC_R_SUCCESS) { |
818 | 0 | if (isc_buffer_availablelength(b) >= 6) { |
819 | 0 | isc_buffer_putuint8( |
820 | 0 | b, dns_name_countlabels( |
821 | 0 | &mayberaw->origin) - |
822 | 0 | 1); |
823 | 0 | isc_buffer_putuint8(b, 0); |
824 | 0 | isc_buffer_putuint32(b, serial); |
825 | 0 | } else { |
826 | 0 | result = ISC_R_NOSPACE; |
827 | 0 | } |
828 | 0 | } |
829 | 0 | } |
830 | 0 | } |
831 | 0 | ZONEDB_UNLOCK(&mayberaw->dblock, isc_rwlocktype_read); |
832 | 0 | if (zone->raw != NULL) { |
833 | 0 | UNLOCK_ZONE(zone->raw); |
834 | 0 | } |
835 | 0 | UNLOCK_ZONE(zone); |
836 | |
|
837 | 0 | return result; |
838 | 0 | } |
839 | | |
840 | | void |
841 | 2 | dns__zone_freedbargs(dns_zone_t *zone) { |
842 | 2 | unsigned int i; |
843 | | |
844 | | /* Free the old database argument list. */ |
845 | 2 | if (zone->db_argv != NULL) { |
846 | 0 | for (i = 0; i < zone->db_argc; i++) { |
847 | 0 | isc_mem_free(zone->mctx, zone->db_argv[i]); |
848 | 0 | } |
849 | 0 | isc_mem_cput(zone->mctx, zone->db_argv, zone->db_argc, |
850 | 0 | sizeof(*zone->db_argv)); |
851 | 0 | } |
852 | 2 | zone->db_argc = 0; |
853 | 2 | zone->db_argv = NULL; |
854 | 2 | } |
855 | | |
856 | | void |
857 | 2 | dns__zone_setview_helper(dns_zone_t *zone, dns_view_t *view) { |
858 | 2 | char namebuf[1024]; |
859 | | |
860 | 2 | if (zone->prev_view == NULL && zone->view != NULL) { |
861 | 0 | dns_view_weakattach(zone->view, &zone->prev_view); |
862 | 0 | } |
863 | | |
864 | 2 | INSIST(zone != zone->raw); |
865 | 2 | if (zone->view != NULL) { |
866 | 0 | dns_view_sfd_del(zone->view, &zone->origin); |
867 | 0 | dns_view_weakdetach(&zone->view); |
868 | 0 | } |
869 | 2 | dns_view_weakattach(view, &zone->view); |
870 | 2 | dns_view_sfd_add(view, &zone->origin); |
871 | | |
872 | 2 | if (zone->strviewname != NULL) { |
873 | 0 | isc_mem_free(zone->mctx, zone->strviewname); |
874 | 0 | } |
875 | 2 | if (zone->strnamerd != NULL) { |
876 | 2 | isc_mem_free(zone->mctx, zone->strnamerd); |
877 | 2 | } |
878 | | |
879 | 2 | zone_namerd_tostr(zone, namebuf, sizeof namebuf); |
880 | 2 | zone->strnamerd = isc_mem_strdup(zone->mctx, namebuf); |
881 | 2 | zone_viewname_tostr(zone, namebuf, sizeof namebuf); |
882 | 2 | zone->strviewname = isc_mem_strdup(zone->mctx, namebuf); |
883 | | |
884 | 2 | if (dns__zone_inline_secure(zone)) { |
885 | 0 | dns_zone_setview(zone->raw, view); |
886 | 0 | } |
887 | 2 | } |
888 | | |
889 | | void |
890 | 0 | dns_zone_setviewcommit(dns_zone_t *zone) { |
891 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
892 | |
|
893 | 0 | LOCK_ZONE(zone); |
894 | 0 | if (zone->prev_view != NULL) { |
895 | 0 | dns_view_weakdetach(&zone->prev_view); |
896 | 0 | } |
897 | 0 | if (dns__zone_inline_secure(zone)) { |
898 | 0 | dns_zone_setviewcommit(zone->raw); |
899 | 0 | } |
900 | 0 | UNLOCK_ZONE(zone); |
901 | 0 | } |
902 | | |
903 | | void |
904 | 0 | dns_zone_setviewrevert(dns_zone_t *zone) { |
905 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
906 | |
|
907 | 0 | LOCK_ZONE(zone); |
908 | 0 | if (zone->prev_view != NULL) { |
909 | 0 | dns__zone_setview_helper(zone, zone->prev_view); |
910 | 0 | dns_view_weakdetach(&zone->prev_view); |
911 | 0 | } |
912 | 0 | if (zone->catzs != NULL) { |
913 | 0 | zone_catz_enable(zone, zone->catzs); |
914 | 0 | } |
915 | 0 | if (dns__zone_inline_secure(zone)) { |
916 | 0 | dns_zone_setviewrevert(zone->raw); |
917 | 0 | } |
918 | 0 | UNLOCK_ZONE(zone); |
919 | 0 | } |
920 | | |
921 | | /* |
922 | | * Return true iff the zone is "dynamic", in the sense that the zone's |
923 | | * master file (if any) is written by the server, rather than being |
924 | | * updated manually and read by the server. |
925 | | * |
926 | | * This is true for secondary zones, mirror zones, stub zones, key zones, |
927 | | * and zones that allow dynamic updates either by having an update |
928 | | * policy ("ssutable") or an "allow-update" ACL with a value other than |
929 | | * exactly "{ none; }". |
930 | | */ |
931 | | bool |
932 | 6 | dns_zone_isdynamic(dns_zone_t *zone, bool ignore_freeze) { |
933 | 6 | REQUIRE(DNS_ZONE_VALID(zone)); |
934 | | |
935 | 6 | if (zone->type == dns_zone_secondary || zone->type == dns_zone_mirror || |
936 | 6 | zone->type == dns_zone_stub || zone->type == dns_zone_key || |
937 | 6 | (zone->type == dns_zone_redirect && |
938 | 0 | dns_remote_addresses(&zone->primaries) != NULL)) |
939 | 0 | { |
940 | 0 | return true; |
941 | 0 | } |
942 | | |
943 | | /* Inline zones are always dynamic. */ |
944 | 6 | if (zone->type == dns_zone_primary && zone->raw != NULL) { |
945 | 0 | return true; |
946 | 0 | } |
947 | | |
948 | | /* If !ignore_freeze, we need check whether updates are disabled. */ |
949 | 6 | if (zone->type == dns_zone_primary && |
950 | 6 | (!zone->update_disabled || ignore_freeze) && |
951 | 6 | ((zone->ssutable != NULL) || |
952 | 6 | (zone->update_acl != NULL && !dns_acl_isnone(zone->update_acl)))) |
953 | 0 | { |
954 | 0 | return true; |
955 | 0 | } |
956 | | |
957 | 6 | return false; |
958 | 6 | } |
959 | | |
960 | | /* |
961 | | * Set the response policy index and information for a zone. |
962 | | */ |
963 | | isc_result_t |
964 | | dns_zone_rpz_enable(dns_zone_t *zone, dns_rpz_zones_t *rpzs, |
965 | 0 | dns_rpz_num_t rpz_num) { |
966 | | /* |
967 | | * This must happen only once or be redundant. |
968 | | */ |
969 | 0 | LOCK_ZONE(zone); |
970 | 0 | if (zone->rpzs != NULL) { |
971 | 0 | REQUIRE(zone->rpzs == rpzs && zone->rpz_num == rpz_num); |
972 | 0 | } else { |
973 | 0 | REQUIRE(zone->rpz_num == DNS_RPZ_INVALID_NUM); |
974 | 0 | dns_rpz_zones_attach(rpzs, &zone->rpzs); |
975 | 0 | zone->rpz_num = rpz_num; |
976 | 0 | } |
977 | 0 | rpzs->defined |= DNS_RPZ_ZBIT(rpz_num); |
978 | 0 | UNLOCK_ZONE(zone); |
979 | |
|
980 | 0 | return ISC_R_SUCCESS; |
981 | 0 | } |
982 | | |
983 | | dns_rpz_num_t |
984 | 0 | dns_zone_get_rpz_num(dns_zone_t *zone) { |
985 | 0 | return zone->rpz_num; |
986 | 0 | } |
987 | | |
988 | | /* |
989 | | * If a zone is a response policy zone, mark its new database. |
990 | | */ |
991 | | void |
992 | 2 | dns_zone_rpz_enable_db(dns_zone_t *zone, dns_db_t *db) { |
993 | 2 | if (zone->rpz_num == DNS_RPZ_INVALID_NUM) { |
994 | 2 | return; |
995 | 2 | } |
996 | 0 | REQUIRE(zone->rpzs != NULL); |
997 | 0 | dns_rpz_dbupdate_register(db, zone->rpzs->zones[zone->rpz_num]); |
998 | 0 | } |
999 | | |
1000 | | static void |
1001 | 0 | dns_zone_rpz_disable_db(dns_zone_t *zone, dns_db_t *db) { |
1002 | 0 | if (zone->rpz_num == DNS_RPZ_INVALID_NUM) { |
1003 | 0 | return; |
1004 | 0 | } |
1005 | 0 | REQUIRE(zone->rpzs != NULL); |
1006 | 0 | dns_rpz_dbupdate_unregister(db, zone->rpzs->zones[zone->rpz_num]); |
1007 | 0 | } |
1008 | | |
1009 | | /* |
1010 | | * If a zone is a catalog zone, attach it to update notification in database. |
1011 | | */ |
1012 | | void |
1013 | 2 | dns_zone_catz_enable_db(dns_zone_t *zone, dns_db_t *db) { |
1014 | 2 | REQUIRE(DNS_ZONE_VALID(zone)); |
1015 | 2 | REQUIRE(db != NULL); |
1016 | | |
1017 | 2 | if (zone->catzs != NULL) { |
1018 | 0 | dns_catz_dbupdate_register(db, zone->catzs); |
1019 | 0 | } |
1020 | 2 | } |
1021 | | |
1022 | | static void |
1023 | 0 | dns_zone_catz_disable_db(dns_zone_t *zone, dns_db_t *db) { |
1024 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1025 | 0 | REQUIRE(db != NULL); |
1026 | |
|
1027 | 0 | if (zone->catzs != NULL) { |
1028 | 0 | dns_catz_dbupdate_unregister(db, zone->catzs); |
1029 | 0 | } |
1030 | 0 | } |
1031 | | |
1032 | | static void |
1033 | 0 | zone_catz_enable(dns_zone_t *zone, dns_catz_zones_t *catzs) { |
1034 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1035 | 0 | REQUIRE(catzs != NULL); |
1036 | |
|
1037 | 0 | INSIST(zone->catzs == NULL || zone->catzs == catzs); |
1038 | 0 | dns_catz_catzs_set_view(catzs, zone->view); |
1039 | 0 | if (zone->catzs == NULL) { |
1040 | 0 | dns_catz_zones_attach(catzs, &zone->catzs); |
1041 | 0 | } |
1042 | 0 | } |
1043 | | |
1044 | | void |
1045 | 0 | dns_zone_catz_enable(dns_zone_t *zone, dns_catz_zones_t *catzs) { |
1046 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1047 | |
|
1048 | 0 | LOCK_ZONE(zone); |
1049 | 0 | zone_catz_enable(zone, catzs); |
1050 | 0 | UNLOCK_ZONE(zone); |
1051 | 0 | } |
1052 | | |
1053 | | static void |
1054 | 0 | zone_catz_disable(dns_zone_t *zone) { |
1055 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1056 | |
|
1057 | 0 | if (zone->catzs != NULL) { |
1058 | 0 | if (zone->db != NULL) { |
1059 | 0 | dns_zone_catz_disable_db(zone, zone->db); |
1060 | 0 | } |
1061 | 0 | dns_catz_zones_detach(&zone->catzs); |
1062 | 0 | } |
1063 | 0 | } |
1064 | | |
1065 | | void |
1066 | 0 | dns_zone_catz_disable(dns_zone_t *zone) { |
1067 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1068 | |
|
1069 | 0 | LOCK_ZONE(zone); |
1070 | 0 | zone_catz_disable(zone); |
1071 | 0 | UNLOCK_ZONE(zone); |
1072 | 0 | } |
1073 | | |
1074 | | bool |
1075 | 0 | dns_zone_catz_is_enabled(dns_zone_t *zone) { |
1076 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1077 | |
|
1078 | 0 | return zone->catzs != NULL; |
1079 | 0 | } |
1080 | | |
1081 | | /* |
1082 | | * Set catalog zone ownership of the zone |
1083 | | */ |
1084 | | void |
1085 | 0 | dns_zone_set_parentcatz(dns_zone_t *zone, dns_catz_zone_t *catz) { |
1086 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1087 | 0 | REQUIRE(catz != NULL); |
1088 | 0 | LOCK_ZONE(zone); |
1089 | 0 | INSIST(zone->parentcatz == NULL || zone->parentcatz == catz); |
1090 | 0 | zone->parentcatz = catz; |
1091 | 0 | UNLOCK_ZONE(zone); |
1092 | 0 | } |
1093 | | |
1094 | | dns_catz_zone_t * |
1095 | 0 | dns_zone_get_parentcatz(dns_zone_t *zone) { |
1096 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1097 | |
|
1098 | 0 | dns_catz_zone_t *parentcatz = NULL; |
1099 | |
|
1100 | 0 | LOCK_ZONE(zone); |
1101 | 0 | parentcatz = zone->parentcatz; |
1102 | 0 | UNLOCK_ZONE(zone); |
1103 | |
|
1104 | 0 | return parentcatz; |
1105 | 0 | } |
1106 | | |
1107 | | static bool |
1108 | 0 | zone_touched(dns_zone_t *zone) { |
1109 | 0 | isc_result_t result; |
1110 | 0 | isc_time_t modtime; |
1111 | |
|
1112 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1113 | |
|
1114 | 0 | result = isc_file_getmodtime(zone->masterfile, &modtime); |
1115 | 0 | if (result != ISC_R_SUCCESS || |
1116 | 0 | isc_time_compare(&modtime, &zone->loadtime) > 0) |
1117 | 0 | { |
1118 | 0 | return true; |
1119 | 0 | } |
1120 | | |
1121 | 0 | ISC_LIST_FOREACH(zone->includes, include, link) { |
1122 | 0 | result = isc_file_getmodtime(include->name, &modtime); |
1123 | 0 | if (result != ISC_R_SUCCESS || |
1124 | 0 | isc_time_compare(&modtime, &include->filetime) > 0) |
1125 | 0 | { |
1126 | 0 | return true; |
1127 | 0 | } |
1128 | 0 | } |
1129 | | |
1130 | 0 | return false; |
1131 | 0 | } |
1132 | | |
1133 | | static isc_result_t |
1134 | 0 | copy_initfile(dns_zone_t *zone) { |
1135 | 0 | isc_result_t result; |
1136 | 0 | FILE *input = NULL, *output = NULL; |
1137 | 0 | off_t len; |
1138 | |
|
1139 | 0 | CHECK(isc_stdio_open(zone->initfile, "r", &input)); |
1140 | 0 | CHECK(isc_stdio_open(zone->masterfile, "w", &output)); |
1141 | |
|
1142 | 0 | CHECK(isc_file_getsizefd(fileno(input), &len)); |
1143 | |
|
1144 | 0 | do { |
1145 | 0 | char buf[BUFSIZ]; |
1146 | 0 | size_t rval; |
1147 | |
|
1148 | 0 | result = isc_stdio_read(buf, 1, sizeof(buf), input, &rval); |
1149 | 0 | if (result != ISC_R_EOF) { |
1150 | 0 | CHECK(result); |
1151 | 0 | } |
1152 | 0 | CHECK(isc_stdio_write(buf, rval, 1, output, NULL)); |
1153 | 0 | len -= rval; |
1154 | 0 | } while (len > 0); |
1155 | | |
1156 | 0 | cleanup: |
1157 | 0 | if (input != NULL) { |
1158 | 0 | isc_stdio_close(input); |
1159 | 0 | } |
1160 | 0 | if (output != NULL) { |
1161 | 0 | if (result != ISC_R_SUCCESS) { |
1162 | 0 | isc_file_remove(zone->masterfile); |
1163 | 0 | } |
1164 | 0 | isc_stdio_close(output); |
1165 | 0 | } |
1166 | 0 | return result; |
1167 | 0 | } |
1168 | | |
1169 | | /* |
1170 | | * Note: when dealing with inline-signed zones, external callers will always |
1171 | | * call zone_load() for the secure zone; zone_load() calls itself recursively |
1172 | | * in order to load the raw zone. |
1173 | | */ |
1174 | | static isc_result_t |
1175 | 2 | zone_load(dns_zone_t *zone, unsigned int flags, bool locked) { |
1176 | 2 | isc_result_t result; |
1177 | 2 | isc_time_t now; |
1178 | 2 | isc_time_t loadtime; |
1179 | 2 | dns_db_t *db = NULL; |
1180 | 2 | bool rbt, hasraw, is_dynamic; |
1181 | | |
1182 | 2 | REQUIRE(DNS_ZONE_VALID(zone)); |
1183 | | |
1184 | 2 | if (!locked) { |
1185 | 2 | LOCK_ZONE(zone); |
1186 | 2 | } |
1187 | | |
1188 | 2 | INSIST(zone != zone->raw); |
1189 | 2 | hasraw = dns__zone_inline_secure(zone); |
1190 | 2 | if (hasraw) { |
1191 | | /* |
1192 | | * We are trying to load an inline-signed zone. First call |
1193 | | * self recursively to try loading the raw version of the zone. |
1194 | | * Assuming the raw zone file is readable, there are two |
1195 | | * possibilities: |
1196 | | * |
1197 | | * a) the raw zone was not yet loaded and thus it will be |
1198 | | * loaded now, synchronously; if this succeeds, a |
1199 | | * subsequent attempt to load the signed zone file will |
1200 | | * take place and thus zone_postload() will be called |
1201 | | * twice: first for the raw zone and then for the secure |
1202 | | * zone; the latter call will be followed by scheduling |
1203 | | * secure maintenance to sync against the raw version, |
1204 | | * |
1205 | | * b) the raw zone was already loaded and we are trying to |
1206 | | * reload it, which will happen asynchronously; this means |
1207 | | * zone_postload() will only be called for the raw zone |
1208 | | * because "result" returned by the zone_load() call below |
1209 | | * will not be ISC_R_SUCCESS but rather DNS_R_CONTINUE; |
1210 | | * zone_postload() called for the raw zone will schedule |
1211 | | * secure maintenance to sync against the raw version. |
1212 | | */ |
1213 | 0 | result = zone_load(zone->raw, flags, false); |
1214 | 0 | if (result != ISC_R_SUCCESS) { |
1215 | 0 | if (!locked) { |
1216 | 0 | UNLOCK_ZONE(zone); |
1217 | 0 | } |
1218 | 0 | return result; |
1219 | 0 | } |
1220 | 0 | } |
1221 | | |
1222 | 2 | now = isc_time_now(); |
1223 | | |
1224 | 2 | INSIST(zone->type != dns_zone_none); |
1225 | | |
1226 | | /* load was already in progress */ |
1227 | 2 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADING)) { |
1228 | 0 | if ((flags & DNS_ZONELOADFLAG_THAW) != 0) { |
1229 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_THAW); |
1230 | 0 | } |
1231 | 0 | CLEANUP(ISC_R_LOADING); |
1232 | 0 | } |
1233 | | |
1234 | 2 | INSIST(zone->db_argc >= 1); |
1235 | | |
1236 | 2 | rbt = strcmp(zone->db_argv[0], ZONEDB_DEFAULT) == 0; |
1237 | | |
1238 | 2 | if (zone->db != NULL && zone->masterfile == NULL && rbt) { |
1239 | | /* |
1240 | | * The zone has no master file configured. |
1241 | | */ |
1242 | 0 | result = ISC_R_SUCCESS; |
1243 | 0 | goto cleanup; |
1244 | 0 | } |
1245 | | |
1246 | 2 | is_dynamic = dns_zone_isdynamic(zone, false); |
1247 | 2 | if (zone->db != NULL && is_dynamic) { |
1248 | | /* |
1249 | | * This is a secondary, stub, or dynamically updated zone |
1250 | | * being reloaded. Do nothing - the database we already |
1251 | | * have is guaranteed to be up-to-date. |
1252 | | */ |
1253 | 0 | if (zone->type == dns_zone_primary && !hasraw) { |
1254 | 0 | result = DNS_R_DYNAMIC; |
1255 | 0 | } else { |
1256 | 0 | result = ISC_R_SUCCESS; |
1257 | 0 | } |
1258 | 0 | goto cleanup; |
1259 | 0 | } |
1260 | | |
1261 | | /* |
1262 | | * Store the current time before the zone is loaded, so that if the |
1263 | | * file changes between the time of the load and the time that |
1264 | | * zone->loadtime is set, then the file will still be reloaded |
1265 | | * the next time dns_zone_load is called. |
1266 | | */ |
1267 | 2 | loadtime = isc_time_now(); |
1268 | | |
1269 | | /* |
1270 | | * Don't do the load if the file that stores the zone is older |
1271 | | * than the last time the zone was loaded. If the zone has not |
1272 | | * been loaded yet, zone->loadtime will be the epoch. |
1273 | | */ |
1274 | 2 | if (zone->masterfile != NULL) { |
1275 | 2 | isc_time_t filetime; |
1276 | | |
1277 | | /* |
1278 | | * The file is already loaded. If we are just doing a |
1279 | | * "rndc reconfig", we are done. |
1280 | | */ |
1281 | 2 | if (!isc_time_isepoch(&zone->loadtime) && |
1282 | 0 | (flags & DNS_ZONELOADFLAG_NOSTAT) != 0) |
1283 | 0 | { |
1284 | 0 | result = ISC_R_SUCCESS; |
1285 | 0 | goto cleanup; |
1286 | 0 | } |
1287 | | |
1288 | 2 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) && |
1289 | 0 | !zone_touched(zone)) |
1290 | 0 | { |
1291 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
1292 | 0 | ISC_LOG_DEBUG(1), |
1293 | 0 | "skipping load: master file " |
1294 | 0 | "older than last load"); |
1295 | 0 | CLEANUP(DNS_R_UPTODATE); |
1296 | 0 | } |
1297 | | |
1298 | | /* |
1299 | | * If the file modification time is in the past |
1300 | | * set loadtime to that value. |
1301 | | */ |
1302 | 2 | result = isc_file_getmodtime(zone->masterfile, &filetime); |
1303 | 2 | if (result == ISC_R_SUCCESS && |
1304 | 2 | isc_time_compare(&loadtime, &filetime) > 0) |
1305 | 0 | { |
1306 | 0 | loadtime = filetime; |
1307 | 0 | } |
1308 | 2 | } |
1309 | | |
1310 | | /* |
1311 | | * Built in zones (with the exception of empty zones) don't need |
1312 | | * to be reloaded. |
1313 | | */ |
1314 | 2 | if (zone->type == dns_zone_primary && |
1315 | 2 | strcmp(zone->db_argv[0], "_builtin") == 0 && |
1316 | 0 | (zone->db_argc < 2 || strcmp(zone->db_argv[1], "empty") != 0) && |
1317 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) |
1318 | 0 | { |
1319 | 0 | result = ISC_R_SUCCESS; |
1320 | 0 | goto cleanup; |
1321 | 0 | } |
1322 | | |
1323 | | /* |
1324 | | * Zones associated with a DLZ don't need to be loaded either, |
1325 | | * but we need to associate the database with the zone object. |
1326 | | */ |
1327 | 2 | if (strcmp(zone->db_argv[0], "dlz") == 0) { |
1328 | 0 | dns_dlzdb_t *dlzdb = NULL; |
1329 | 0 | dns_dlzfindzone_t findzone; |
1330 | |
|
1331 | 0 | ISC_LIST_FOREACH(zone->view->dlz_unsearched, d, link) { |
1332 | 0 | INSIST(DNS_DLZ_VALID(d)); |
1333 | 0 | if (strcmp(zone->db_argv[1], d->dlzname) == 0) { |
1334 | 0 | dlzdb = d; |
1335 | 0 | break; |
1336 | 0 | } |
1337 | 0 | } |
1338 | |
|
1339 | 0 | if (dlzdb == NULL) { |
1340 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
1341 | 0 | ISC_LOG_ERROR, |
1342 | 0 | "DLZ %s does not exist or is set " |
1343 | 0 | "to 'search yes;'", |
1344 | 0 | zone->db_argv[1]); |
1345 | 0 | CLEANUP(ISC_R_NOTFOUND); |
1346 | 0 | } |
1347 | | |
1348 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write); |
1349 | | /* ask SDLZ driver if the zone is supported */ |
1350 | 0 | findzone = dlzdb->implementation->methods->findzone; |
1351 | 0 | result = (*findzone)(dlzdb->implementation->driverarg, |
1352 | 0 | dlzdb->dbdata, dlzdb->mctx, |
1353 | 0 | zone->view->rdclass, &zone->origin, NULL, |
1354 | 0 | NULL, &db); |
1355 | 0 | if (result != ISC_R_NOTFOUND) { |
1356 | 0 | if (zone->db != NULL) { |
1357 | 0 | zone_detachdb(zone); |
1358 | 0 | } |
1359 | 0 | zone_attachdb(zone, db); |
1360 | 0 | dns_db_detach(&db); |
1361 | 0 | result = ISC_R_SUCCESS; |
1362 | 0 | } |
1363 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write); |
1364 | |
|
1365 | 0 | if (result == ISC_R_SUCCESS) { |
1366 | 0 | if (dlzdb->configure_callback == NULL) { |
1367 | 0 | goto cleanup; |
1368 | 0 | } |
1369 | | |
1370 | 0 | result = (*dlzdb->configure_callback)(zone->view, dlzdb, |
1371 | 0 | zone); |
1372 | 0 | if (result != ISC_R_SUCCESS) { |
1373 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
1374 | 0 | ISC_LOG_ERROR, |
1375 | 0 | "DLZ configuration callback: %s", |
1376 | 0 | isc_result_totext(result)); |
1377 | 0 | } |
1378 | 0 | } |
1379 | 0 | goto cleanup; |
1380 | 0 | } |
1381 | | |
1382 | 2 | if ((zone->type == dns_zone_secondary || |
1383 | 2 | zone->type == dns_zone_mirror || zone->type == dns_zone_stub || |
1384 | 2 | (zone->type == dns_zone_redirect && |
1385 | 0 | dns_remote_addresses(&zone->primaries) != NULL)) && |
1386 | 0 | rbt) |
1387 | 0 | { |
1388 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FIRSTREFRESH); |
1389 | |
|
1390 | 0 | if (zone->stream == NULL && |
1391 | 0 | (zone->masterfile == NULL || |
1392 | 0 | !isc_file_exists(zone->masterfile))) |
1393 | 0 | { |
1394 | 0 | if (zone->masterfile != NULL) { |
1395 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
1396 | 0 | ISC_LOG_DEBUG(1), |
1397 | 0 | "no master file"); |
1398 | 0 | } |
1399 | 0 | zone->refreshtime = now; |
1400 | 0 | if (zone->loop != NULL) { |
1401 | 0 | dns__zone_settimer(zone, now); |
1402 | 0 | } |
1403 | 0 | result = ISC_R_SUCCESS; |
1404 | 0 | goto cleanup; |
1405 | 0 | } |
1406 | 0 | } |
1407 | | |
1408 | 2 | if (zone->type == dns_zone_primary && zone->masterfile != NULL && |
1409 | 2 | !isc_file_exists(zone->masterfile) && zone->initfile != NULL) |
1410 | 0 | { |
1411 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_INFO, |
1412 | 0 | "zone file %s not found; copying initial " |
1413 | 0 | "file %s", |
1414 | 0 | zone->masterfile, zone->initfile); |
1415 | 0 | result = copy_initfile(zone); |
1416 | 0 | if (result != ISC_R_SUCCESS) { |
1417 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
1418 | 0 | ISC_LOG_ERROR, "copy from %s failed: %s", |
1419 | 0 | zone->initfile, |
1420 | 0 | isc_result_totext(result)); |
1421 | 0 | } |
1422 | 0 | } |
1423 | | |
1424 | 2 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(1), |
1425 | 2 | "starting load"); |
1426 | | |
1427 | 2 | result = dns_zone_makedb(zone, &db); |
1428 | 2 | if (result != ISC_R_SUCCESS) { |
1429 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR, |
1430 | 0 | "loading zone: creating database: %s", |
1431 | 0 | isc_result_totext(result)); |
1432 | 0 | goto cleanup; |
1433 | 0 | } |
1434 | | |
1435 | 2 | if (!dns_db_ispersistent(db)) { |
1436 | 2 | if (zone->masterfile != NULL || zone->stream != NULL) { |
1437 | 2 | result = zone_startload(db, zone, loadtime); |
1438 | 2 | } else { |
1439 | 0 | result = DNS_R_NOMASTERFILE; |
1440 | 0 | if (zone->type == dns_zone_primary || |
1441 | 0 | (zone->type == dns_zone_redirect && |
1442 | 0 | dns_remote_addresses(&zone->primaries) == NULL)) |
1443 | 0 | { |
1444 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
1445 | 0 | ISC_LOG_ERROR, |
1446 | 0 | "loading zone: " |
1447 | 0 | "no master file configured"); |
1448 | 0 | goto cleanup; |
1449 | 0 | } |
1450 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
1451 | 0 | ISC_LOG_INFO, |
1452 | 0 | "loading zone: " |
1453 | 0 | "no master file configured: continuing"); |
1454 | 0 | } |
1455 | 2 | } |
1456 | | |
1457 | 2 | if (result == DNS_R_CONTINUE) { |
1458 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADING); |
1459 | 0 | if ((flags & DNS_ZONELOADFLAG_THAW) != 0) { |
1460 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_THAW); |
1461 | 0 | } |
1462 | 0 | goto cleanup; |
1463 | 0 | } |
1464 | | |
1465 | 2 | result = zone_postload(zone, db, loadtime, result); |
1466 | 2 | if (hasraw && result == ISC_R_SUCCESS) { |
1467 | 0 | zone_schedule_inline_sync(zone, inline_sync_incremental); |
1468 | 0 | } |
1469 | | |
1470 | 2 | cleanup: |
1471 | 2 | if (!locked) { |
1472 | 2 | UNLOCK_ZONE(zone); |
1473 | 2 | } |
1474 | 2 | if (db != NULL) { |
1475 | 2 | dns_db_detach(&db); |
1476 | 2 | } |
1477 | 2 | return result; |
1478 | 2 | } |
1479 | | |
1480 | | isc_result_t |
1481 | 2 | dns_zone_load(dns_zone_t *zone, bool newonly) { |
1482 | 2 | return zone_load(zone, newonly ? DNS_ZONELOADFLAG_NOSTAT : 0, false); |
1483 | 2 | } |
1484 | | |
1485 | | static void |
1486 | 0 | zone_asyncload(void *arg) { |
1487 | 0 | dns_asyncload_t *asl = arg; |
1488 | 0 | dns_zone_t *zone = asl->zone; |
1489 | 0 | isc_result_t result; |
1490 | |
|
1491 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1492 | |
|
1493 | 0 | LOCK_ZONE(zone); |
1494 | 0 | result = zone_load(zone, asl->flags, true); |
1495 | 0 | if (result != DNS_R_CONTINUE && result != ISC_R_LOADING) { |
1496 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADPENDING); |
1497 | 0 | } |
1498 | 0 | UNLOCK_ZONE(zone); |
1499 | | |
1500 | | /* Inform the zone table we've finished loading */ |
1501 | 0 | if (asl->loaded != NULL) { |
1502 | 0 | asl->loaded(asl->loaded_arg); |
1503 | 0 | } |
1504 | |
|
1505 | 0 | isc_mem_put(zone->mctx, asl, sizeof(*asl)); |
1506 | 0 | dns_zone_idetach(&zone); |
1507 | 0 | } |
1508 | | |
1509 | | isc_result_t |
1510 | | dns_zone_asyncload(dns_zone_t *zone, bool newonly, dns_zt_callback_t *done, |
1511 | 0 | void *arg) { |
1512 | 0 | dns_asyncload_t *asl = NULL; |
1513 | |
|
1514 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1515 | |
|
1516 | 0 | if (zone->zmgr == NULL) { |
1517 | 0 | return ISC_R_FAILURE; |
1518 | 0 | } |
1519 | | |
1520 | | /* If we already have a load pending, stop now */ |
1521 | 0 | LOCK_ZONE(zone); |
1522 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING)) { |
1523 | 0 | UNLOCK_ZONE(zone); |
1524 | 0 | return ISC_R_ALREADYRUNNING; |
1525 | 0 | } |
1526 | | |
1527 | 0 | asl = isc_mem_get(zone->mctx, sizeof(*asl)); |
1528 | |
|
1529 | 0 | asl->zone = NULL; |
1530 | 0 | asl->flags = newonly ? DNS_ZONELOADFLAG_NOSTAT : 0; |
1531 | 0 | asl->loaded = done; |
1532 | 0 | asl->loaded_arg = arg; |
1533 | |
|
1534 | 0 | zone_iattach(zone, &asl->zone); |
1535 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADPENDING); |
1536 | 0 | isc_async_run(zone->loop, zone_asyncload, asl); |
1537 | 0 | UNLOCK_ZONE(zone); |
1538 | |
|
1539 | 0 | return ISC_R_SUCCESS; |
1540 | 0 | } |
1541 | | |
1542 | | bool |
1543 | 0 | dns__zone_loadpending(dns_zone_t *zone) { |
1544 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1545 | |
|
1546 | 0 | return DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING); |
1547 | 0 | } |
1548 | | |
1549 | | isc_result_t |
1550 | 0 | dns_zone_loadandthaw(dns_zone_t *zone) { |
1551 | 0 | isc_result_t result; |
1552 | 0 | bool inline_raw; |
1553 | |
|
1554 | 0 | LOCK_ZONE(zone); |
1555 | 0 | inline_raw = dns__zone_inline_raw(zone); |
1556 | 0 | UNLOCK_ZONE(zone); |
1557 | |
|
1558 | 0 | if (inline_raw) { |
1559 | 0 | result = zone_load(zone->secure, DNS_ZONELOADFLAG_THAW, false); |
1560 | 0 | } else { |
1561 | | /* |
1562 | | * When thawing a zone, we don't know what changes |
1563 | | * have been made. If we do DNSSEC maintenance on this |
1564 | | * zone, schedule a full sign for this zone. |
1565 | | */ |
1566 | 0 | if (zone->type == dns_zone_primary && zone->kasp != NULL) { |
1567 | 0 | DNS_ZONE_SETOPTION(zone, DNS_ZONEOPT_FULLSIGN); |
1568 | 0 | } |
1569 | 0 | result = zone_load(zone, DNS_ZONELOADFLAG_THAW, false); |
1570 | 0 | } |
1571 | |
|
1572 | 0 | switch (result) { |
1573 | 0 | case DNS_R_CONTINUE: |
1574 | 0 | case ISC_R_LOADING: |
1575 | | /* Deferred thaw. */ |
1576 | 0 | break; |
1577 | 0 | case DNS_R_UPTODATE: |
1578 | 0 | case ISC_R_SUCCESS: |
1579 | 0 | case DNS_R_SEENINCLUDE: |
1580 | 0 | zone->update_disabled = false; |
1581 | 0 | break; |
1582 | 0 | case DNS_R_NOMASTERFILE: |
1583 | 0 | zone->update_disabled = false; |
1584 | 0 | break; |
1585 | 0 | default: |
1586 | | /* Error, remain in disabled state. */ |
1587 | 0 | break; |
1588 | 0 | } |
1589 | 0 | return result; |
1590 | 0 | } |
1591 | | |
1592 | | static unsigned int |
1593 | 2 | get_primary_options(dns_zone_t *zone) { |
1594 | 2 | unsigned int options; |
1595 | | |
1596 | 2 | options = DNS_MASTER_ZONE | DNS_MASTER_RESIGN; |
1597 | 2 | if (zone->type == dns_zone_secondary || zone->type == dns_zone_mirror || |
1598 | 2 | (zone->type == dns_zone_redirect && |
1599 | 0 | dns_remote_addresses(&zone->primaries) == NULL)) |
1600 | 0 | { |
1601 | 0 | options |= DNS_MASTER_SECONDARY; |
1602 | 0 | } |
1603 | 2 | if (zone->type == dns_zone_key) { |
1604 | 0 | options |= DNS_MASTER_KEY; |
1605 | 0 | } |
1606 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNS)) { |
1607 | 0 | options |= DNS_MASTER_CHECKNS; |
1608 | 0 | } |
1609 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_FATALNS)) { |
1610 | 0 | options |= DNS_MASTER_FATALNS; |
1611 | 0 | } |
1612 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNAMES)) { |
1613 | 0 | options |= DNS_MASTER_CHECKNAMES; |
1614 | 0 | } |
1615 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNAMESFAIL)) { |
1616 | 0 | options |= DNS_MASTER_CHECKNAMESFAIL; |
1617 | 0 | } |
1618 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKMX)) { |
1619 | 0 | options |= DNS_MASTER_CHECKMX; |
1620 | 0 | } |
1621 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKMXFAIL)) { |
1622 | 0 | options |= DNS_MASTER_CHECKMXFAIL; |
1623 | 0 | } |
1624 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKWILDCARD)) { |
1625 | 0 | options |= DNS_MASTER_CHECKWILDCARD; |
1626 | 0 | } |
1627 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKTTL)) { |
1628 | 0 | options |= DNS_MASTER_CHECKTTL; |
1629 | 0 | } |
1630 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKSVCB)) { |
1631 | 0 | options |= DNS_MASTER_CHECKSVCB; |
1632 | 0 | } |
1633 | | |
1634 | 2 | return options; |
1635 | 2 | } |
1636 | | |
1637 | | static void |
1638 | 0 | zone_registerinclude(const char *filename, void *arg) { |
1639 | 0 | isc_result_t result; |
1640 | 0 | dns_zone_t *zone = (dns_zone_t *)arg; |
1641 | |
|
1642 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
1643 | |
|
1644 | 0 | if (filename == NULL) { |
1645 | 0 | return; |
1646 | 0 | } |
1647 | | |
1648 | | /* |
1649 | | * Suppress duplicates. |
1650 | | */ |
1651 | 0 | ISC_LIST_FOREACH(zone->newincludes, inc, link) { |
1652 | 0 | if (strcmp(filename, inc->name) == 0) { |
1653 | 0 | return; |
1654 | 0 | } |
1655 | 0 | } |
1656 | | |
1657 | 0 | dns_include_t *inc = isc_mem_get(zone->mctx, sizeof(dns_include_t)); |
1658 | 0 | inc->name = isc_mem_strdup(zone->mctx, filename); |
1659 | 0 | ISC_LINK_INIT(inc, link); |
1660 | |
|
1661 | 0 | result = isc_file_getmodtime(filename, &inc->filetime); |
1662 | 0 | if (result != ISC_R_SUCCESS) { |
1663 | 0 | isc_time_settoepoch(&inc->filetime); |
1664 | 0 | } |
1665 | |
|
1666 | 0 | ISC_LIST_APPEND(zone->newincludes, inc, link); |
1667 | 0 | } |
1668 | | |
1669 | | static void |
1670 | 0 | get_raw_serial(dns_zone_t *raw, dns_masterrawheader_t *rawdata) { |
1671 | 0 | isc_result_t result; |
1672 | 0 | unsigned int soacount; |
1673 | |
|
1674 | 0 | LOCK(&raw->lock); |
1675 | 0 | if (raw->db != NULL) { |
1676 | 0 | result = zone_get_from_db(raw, raw->db, NULL, &soacount, NULL, |
1677 | 0 | &rawdata->sourceserial, NULL, NULL, |
1678 | 0 | NULL, NULL, NULL); |
1679 | 0 | if (result == ISC_R_SUCCESS && soacount > 0U) { |
1680 | 0 | rawdata->flags |= DNS_MASTERRAW_SOURCESERIALSET; |
1681 | 0 | } |
1682 | 0 | } |
1683 | 0 | UNLOCK(&raw->lock); |
1684 | 0 | } |
1685 | | |
1686 | | /* |
1687 | | * Save the raw serial number for inline-signing zones. |
1688 | | * (XXX: Other information from the header will be used |
1689 | | * for other purposes in the future, but for now this is |
1690 | | * all we're interested in.) |
1691 | | */ |
1692 | | static void |
1693 | 0 | zone_setrawdata(dns_zone_t *zone, dns_masterrawheader_t *header) { |
1694 | 0 | if ((header->flags & DNS_MASTERRAW_SOURCESERIALSET) == 0) { |
1695 | 0 | return; |
1696 | 0 | } |
1697 | | |
1698 | 0 | zone->sourceserial = header->sourceserial; |
1699 | 0 | zone->sourceserialset = true; |
1700 | 0 | } |
1701 | | |
1702 | | void |
1703 | 0 | dns_zone_setrawdata(dns_zone_t *zone, dns_masterrawheader_t *header) { |
1704 | 0 | if (zone == NULL) { |
1705 | 0 | return; |
1706 | 0 | } |
1707 | | |
1708 | 0 | LOCK_ZONE(zone); |
1709 | 0 | zone_setrawdata(zone, header); |
1710 | 0 | UNLOCK_ZONE(zone); |
1711 | 0 | } |
1712 | | |
1713 | | static isc_result_t |
1714 | 2 | zone_startload(dns_db_t *db, dns_zone_t *zone, isc_time_t loadtime) { |
1715 | 2 | isc_result_t result; |
1716 | 2 | isc_result_t tresult; |
1717 | 2 | unsigned int options; |
1718 | 2 | dns_load_t *load = isc_mem_get(zone->mctx, sizeof(*load)); |
1719 | | |
1720 | 2 | ENTER; |
1721 | | |
1722 | 2 | *load = (dns_load_t){ |
1723 | 2 | .loadtime = loadtime, |
1724 | 2 | }; |
1725 | | |
1726 | 2 | dns_zone_rpz_enable_db(zone, db); |
1727 | 2 | dns_zone_catz_enable_db(zone, db); |
1728 | | |
1729 | 2 | options = get_primary_options(zone); |
1730 | 2 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_MANYERRORS)) { |
1731 | 0 | options |= DNS_MASTER_MANYERRORS; |
1732 | 0 | } |
1733 | | |
1734 | 2 | zone_iattach(zone, &load->zone); |
1735 | 2 | dns_db_attach(db, &load->db); |
1736 | | |
1737 | 2 | dns_rdatacallbacks_init(&load->callbacks); |
1738 | 2 | load->callbacks.rawdata = zone_setrawdata; |
1739 | 2 | zone_iattach(zone, &load->callbacks.zone); |
1740 | | |
1741 | 2 | CHECK(dns_db_beginload(db, &load->callbacks)); |
1742 | | |
1743 | 2 | if (zone->zmgr != NULL && zone->db != NULL) { |
1744 | 0 | CHECK(dns_master_loadfileasync( |
1745 | 0 | zone->masterfile, dns_db_origin(db), dns_db_origin(db), |
1746 | 0 | zone->rdclass, options, 0, &load->callbacks, zone->loop, |
1747 | 0 | zone_loaddone, load, &zone->loadctx, |
1748 | 0 | zone_registerinclude, zone, zone->mctx, |
1749 | 0 | zone->masterformat, zone->maxttl)); |
1750 | |
|
1751 | 0 | return DNS_R_CONTINUE; |
1752 | 2 | } else if (zone->stream != NULL) { |
1753 | 0 | FILE *stream = UNCONST(zone->stream); |
1754 | 0 | result = dns_master_loadstream( |
1755 | 0 | stream, &zone->origin, &zone->origin, zone->rdclass, |
1756 | 0 | options, &load->callbacks, zone->mctx); |
1757 | 2 | } else { |
1758 | 2 | result = dns_master_loadfile( |
1759 | 2 | zone->masterfile, &zone->origin, &zone->origin, |
1760 | 2 | zone->rdclass, options, 0, &load->callbacks, |
1761 | 2 | zone_registerinclude, zone, zone->mctx, |
1762 | 2 | zone->masterformat, zone->maxttl); |
1763 | 2 | } |
1764 | | |
1765 | 2 | cleanup: |
1766 | 2 | if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) { |
1767 | 0 | dns_zone_rpz_disable_db(zone, load->db); |
1768 | 0 | dns_zone_catz_disable_db(zone, load->db); |
1769 | 0 | } |
1770 | | |
1771 | 2 | tresult = dns_db_endload(db, &load->callbacks); |
1772 | 2 | if (result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE) { |
1773 | 2 | result = tresult; |
1774 | 2 | } |
1775 | | |
1776 | 2 | zone_idetach(&load->callbacks.zone); |
1777 | 2 | dns_db_detach(&load->db); |
1778 | 2 | zone_idetach(&load->zone); |
1779 | | |
1780 | 2 | isc_mem_put(zone->mctx, load, sizeof(*load)); |
1781 | 2 | return result; |
1782 | 2 | } |
1783 | | |
1784 | | static bool |
1785 | | zone_check_mx(dns_zone_t *zone, dns_db_t *db, dns_name_t *name, |
1786 | 0 | dns_name_t *owner) { |
1787 | 0 | isc_result_t result; |
1788 | 0 | char ownerbuf[DNS_NAME_FORMATSIZE]; |
1789 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
1790 | 0 | char altbuf[DNS_NAME_FORMATSIZE]; |
1791 | 0 | dns_fixedname_t fixed; |
1792 | 0 | dns_name_t *foundname; |
1793 | 0 | int level; |
1794 | | |
1795 | | /* |
1796 | | * "." means the services does not exist. |
1797 | | */ |
1798 | 0 | if (dns_name_equal(name, dns_rootname)) { |
1799 | 0 | return true; |
1800 | 0 | } |
1801 | | |
1802 | | /* |
1803 | | * Outside of zone. |
1804 | | */ |
1805 | 0 | if (!dns_name_issubdomain(name, &zone->origin)) { |
1806 | 0 | if (zone->checkmx != NULL) { |
1807 | 0 | return (zone->checkmx)(zone, name, owner); |
1808 | 0 | } |
1809 | 0 | return true; |
1810 | 0 | } |
1811 | | |
1812 | 0 | if (zone->type == dns_zone_primary) { |
1813 | 0 | level = ISC_LOG_ERROR; |
1814 | 0 | } else { |
1815 | 0 | level = ISC_LOG_WARNING; |
1816 | 0 | } |
1817 | |
|
1818 | 0 | foundname = dns_fixedname_initname(&fixed); |
1819 | |
|
1820 | 0 | result = dns_db_find(db, name, NULL, dns_rdatatype_a, 0, 0, foundname, |
1821 | 0 | NULL, NULL); |
1822 | 0 | if (result == ISC_R_SUCCESS) { |
1823 | 0 | return true; |
1824 | 0 | } |
1825 | | |
1826 | 0 | if (result == DNS_R_NXRRSET) { |
1827 | 0 | result = dns_db_find(db, name, NULL, dns_rdatatype_aaaa, 0, 0, |
1828 | 0 | foundname, NULL, NULL); |
1829 | 0 | if (result == ISC_R_SUCCESS) { |
1830 | 0 | return true; |
1831 | 0 | } |
1832 | 0 | } |
1833 | | |
1834 | 0 | dns_name_format(owner, ownerbuf, sizeof ownerbuf); |
1835 | 0 | dns_name_format(name, namebuf, sizeof namebuf); |
1836 | 0 | if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN || |
1837 | 0 | result == DNS_R_EMPTYNAME) |
1838 | 0 | { |
1839 | 0 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKMXFAIL)) { |
1840 | 0 | level = ISC_LOG_WARNING; |
1841 | 0 | } |
1842 | 0 | dns_zone_log(zone, level, |
1843 | 0 | "%s/MX '%s' has no address records (A or AAAA)", |
1844 | 0 | ownerbuf, namebuf); |
1845 | 0 | return (level == ISC_LOG_WARNING) ? true : false; |
1846 | 0 | } |
1847 | | |
1848 | 0 | if (result == DNS_R_CNAME) { |
1849 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_WARNMXCNAME) || |
1850 | 0 | DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNOREMXCNAME)) |
1851 | 0 | { |
1852 | 0 | level = ISC_LOG_WARNING; |
1853 | 0 | } |
1854 | 0 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNOREMXCNAME)) { |
1855 | 0 | dns_zone_log(zone, level, |
1856 | 0 | "%s/MX '%s' is a CNAME (illegal)", |
1857 | 0 | ownerbuf, namebuf); |
1858 | 0 | } |
1859 | 0 | return (level == ISC_LOG_WARNING) ? true : false; |
1860 | 0 | } |
1861 | | |
1862 | 0 | if (result == DNS_R_DNAME) { |
1863 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_WARNMXCNAME) || |
1864 | 0 | DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNOREMXCNAME)) |
1865 | 0 | { |
1866 | 0 | level = ISC_LOG_WARNING; |
1867 | 0 | } |
1868 | 0 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNOREMXCNAME)) { |
1869 | 0 | dns_name_format(foundname, altbuf, sizeof altbuf); |
1870 | 0 | dns_zone_log(zone, level, |
1871 | 0 | "%s/MX '%s' is below a DNAME" |
1872 | 0 | " '%s' (illegal)", |
1873 | 0 | ownerbuf, namebuf, altbuf); |
1874 | 0 | } |
1875 | 0 | return (level == ISC_LOG_WARNING) ? true : false; |
1876 | 0 | } |
1877 | | |
1878 | 0 | if (zone->checkmx != NULL && result == DNS_R_DELEGATION) { |
1879 | 0 | return (zone->checkmx)(zone, name, owner); |
1880 | 0 | } |
1881 | | |
1882 | 0 | return true; |
1883 | 0 | } |
1884 | | |
1885 | | static bool |
1886 | | zone_check_srv(dns_zone_t *zone, dns_db_t *db, dns_name_t *name, |
1887 | 0 | dns_name_t *owner) { |
1888 | 0 | isc_result_t result; |
1889 | 0 | char ownerbuf[DNS_NAME_FORMATSIZE]; |
1890 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
1891 | 0 | char altbuf[DNS_NAME_FORMATSIZE]; |
1892 | 0 | dns_fixedname_t fixed; |
1893 | 0 | dns_name_t *foundname; |
1894 | 0 | int level; |
1895 | | |
1896 | | /* |
1897 | | * "." means the services does not exist. |
1898 | | */ |
1899 | 0 | if (dns_name_equal(name, dns_rootname)) { |
1900 | 0 | return true; |
1901 | 0 | } |
1902 | | |
1903 | | /* |
1904 | | * Outside of zone. |
1905 | | */ |
1906 | 0 | if (!dns_name_issubdomain(name, &zone->origin)) { |
1907 | 0 | if (zone->checksrv != NULL) { |
1908 | 0 | return (zone->checksrv)(zone, name, owner); |
1909 | 0 | } |
1910 | 0 | return true; |
1911 | 0 | } |
1912 | | |
1913 | 0 | if (zone->type == dns_zone_primary) { |
1914 | 0 | level = ISC_LOG_ERROR; |
1915 | 0 | } else { |
1916 | 0 | level = ISC_LOG_WARNING; |
1917 | 0 | } |
1918 | |
|
1919 | 0 | foundname = dns_fixedname_initname(&fixed); |
1920 | |
|
1921 | 0 | result = dns_db_find(db, name, NULL, dns_rdatatype_a, 0, 0, foundname, |
1922 | 0 | NULL, NULL); |
1923 | 0 | if (result == ISC_R_SUCCESS) { |
1924 | 0 | return true; |
1925 | 0 | } |
1926 | | |
1927 | 0 | if (result == DNS_R_NXRRSET) { |
1928 | 0 | result = dns_db_find(db, name, NULL, dns_rdatatype_aaaa, 0, 0, |
1929 | 0 | foundname, NULL, NULL); |
1930 | 0 | if (result == ISC_R_SUCCESS) { |
1931 | 0 | return true; |
1932 | 0 | } |
1933 | 0 | } |
1934 | | |
1935 | 0 | dns_name_format(owner, ownerbuf, sizeof ownerbuf); |
1936 | 0 | dns_name_format(name, namebuf, sizeof namebuf); |
1937 | 0 | if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN || |
1938 | 0 | result == DNS_R_EMPTYNAME) |
1939 | 0 | { |
1940 | 0 | dns_zone_log(zone, level, |
1941 | 0 | "%s/SRV '%s' has no address records (A or AAAA)", |
1942 | 0 | ownerbuf, namebuf); |
1943 | | /* XXX950 make fatal for 9.5.0. */ |
1944 | 0 | return true; |
1945 | 0 | } |
1946 | | |
1947 | 0 | if (result == DNS_R_CNAME) { |
1948 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_WARNSRVCNAME) || |
1949 | 0 | DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNORESRVCNAME)) |
1950 | 0 | { |
1951 | 0 | level = ISC_LOG_WARNING; |
1952 | 0 | } |
1953 | 0 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNORESRVCNAME)) { |
1954 | 0 | dns_zone_log(zone, level, |
1955 | 0 | "%s/SRV '%s' is a CNAME (illegal)", |
1956 | 0 | ownerbuf, namebuf); |
1957 | 0 | } |
1958 | 0 | return (level == ISC_LOG_WARNING) ? true : false; |
1959 | 0 | } |
1960 | | |
1961 | 0 | if (result == DNS_R_DNAME) { |
1962 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_WARNSRVCNAME) || |
1963 | 0 | DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNORESRVCNAME)) |
1964 | 0 | { |
1965 | 0 | level = ISC_LOG_WARNING; |
1966 | 0 | } |
1967 | 0 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IGNORESRVCNAME)) { |
1968 | 0 | dns_name_format(foundname, altbuf, sizeof altbuf); |
1969 | 0 | dns_zone_log(zone, level, |
1970 | 0 | "%s/SRV '%s' is below a " |
1971 | 0 | "DNAME '%s' (illegal)", |
1972 | 0 | ownerbuf, namebuf, altbuf); |
1973 | 0 | } |
1974 | 0 | return (level == ISC_LOG_WARNING) ? true : false; |
1975 | 0 | } |
1976 | | |
1977 | 0 | if (zone->checksrv != NULL && result == DNS_R_DELEGATION) { |
1978 | 0 | return (zone->checksrv)(zone, name, owner); |
1979 | 0 | } |
1980 | | |
1981 | 0 | return true; |
1982 | 0 | } |
1983 | | |
1984 | | static bool |
1985 | | zone_check_glue(dns_zone_t *zone, dns_db_t *db, bool *has_a, bool *has_aaaa, |
1986 | 0 | dns_name_t *name, dns_name_t *owner) { |
1987 | 0 | bool answer = true; |
1988 | 0 | isc_result_t result, tresult; |
1989 | 0 | char ownerbuf[DNS_NAME_FORMATSIZE]; |
1990 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
1991 | 0 | char altbuf[DNS_NAME_FORMATSIZE]; |
1992 | 0 | dns_fixedname_t fixed; |
1993 | 0 | dns_name_t *foundname; |
1994 | 0 | dns_rdataset_t a; |
1995 | 0 | dns_rdataset_t aaaa; |
1996 | 0 | int level; |
1997 | | |
1998 | | /* |
1999 | | * Outside of zone. |
2000 | | */ |
2001 | 0 | if (!dns_name_issubdomain(name, &zone->origin)) { |
2002 | 0 | if (zone->checkns != NULL) { |
2003 | 0 | return (zone->checkns)(zone, name, owner, NULL, NULL); |
2004 | 0 | } |
2005 | 0 | return true; |
2006 | 0 | } |
2007 | | |
2008 | 0 | if (zone->type == dns_zone_primary) { |
2009 | 0 | level = ISC_LOG_ERROR; |
2010 | 0 | } else { |
2011 | 0 | level = ISC_LOG_WARNING; |
2012 | 0 | } |
2013 | |
|
2014 | 0 | foundname = dns_fixedname_initname(&fixed); |
2015 | 0 | dns_rdataset_init(&a); |
2016 | 0 | dns_rdataset_init(&aaaa); |
2017 | | |
2018 | | /* |
2019 | | * Perform a regular lookup to catch DNAME records then look |
2020 | | * for glue. |
2021 | | */ |
2022 | 0 | result = dns_db_find(db, name, NULL, dns_rdatatype_a, 0, 0, foundname, |
2023 | 0 | &a, NULL); |
2024 | 0 | switch (result) { |
2025 | 0 | case ISC_R_SUCCESS: |
2026 | 0 | case DNS_R_DNAME: |
2027 | 0 | case DNS_R_CNAME: |
2028 | 0 | break; |
2029 | 0 | default: |
2030 | 0 | dns_rdataset_cleanup(&a); |
2031 | 0 | result = dns_db_find(db, name, NULL, dns_rdatatype_a, |
2032 | 0 | DNS_DBFIND_GLUEOK, 0, foundname, &a, NULL); |
2033 | 0 | } |
2034 | 0 | if (result == ISC_R_SUCCESS) { |
2035 | 0 | SET_IF_NOT_NULL(has_a, true); |
2036 | 0 | dns_rdataset_disassociate(&a); |
2037 | 0 | if (has_aaaa != NULL && !*has_aaaa) { |
2038 | 0 | result = dns_db_find(db, name, NULL, dns_rdatatype_aaaa, |
2039 | 0 | DNS_DBFIND_GLUEOK, 0, foundname, |
2040 | 0 | &aaaa, NULL); |
2041 | 0 | if (result == ISC_R_SUCCESS) { |
2042 | 0 | *has_aaaa = true; |
2043 | 0 | } |
2044 | 0 | dns_rdataset_cleanup(&aaaa); |
2045 | 0 | } |
2046 | 0 | return true; |
2047 | 0 | } else if (result == DNS_R_GLUE && has_a != NULL) { |
2048 | 0 | *has_a = true; |
2049 | 0 | } else if (result == DNS_R_DELEGATION) { |
2050 | 0 | dns_rdataset_disassociate(&a); |
2051 | 0 | } |
2052 | | |
2053 | 0 | if (result == DNS_R_NXRRSET || result == DNS_R_DELEGATION || |
2054 | 0 | result == DNS_R_GLUE) |
2055 | 0 | { |
2056 | 0 | tresult = dns_db_find(db, name, NULL, dns_rdatatype_aaaa, |
2057 | 0 | DNS_DBFIND_GLUEOK, 0, foundname, &aaaa, |
2058 | 0 | NULL); |
2059 | 0 | if (tresult == ISC_R_SUCCESS) { |
2060 | 0 | dns_rdataset_cleanup(&a); |
2061 | 0 | SET_IF_NOT_NULL(has_aaaa, true); |
2062 | 0 | dns_rdataset_disassociate(&aaaa); |
2063 | 0 | return true; |
2064 | 0 | } |
2065 | 0 | if (tresult == DNS_R_DELEGATION || tresult == DNS_R_DNAME) { |
2066 | 0 | dns_rdataset_disassociate(&aaaa); |
2067 | 0 | } |
2068 | 0 | if (tresult == DNS_R_GLUE && has_aaaa != NULL) { |
2069 | 0 | *has_aaaa = true; |
2070 | 0 | } |
2071 | 0 | if (result == DNS_R_GLUE || tresult == DNS_R_GLUE) { |
2072 | | /* |
2073 | | * Check glue against child zone. |
2074 | | */ |
2075 | 0 | if (zone->checkns != NULL) { |
2076 | 0 | answer = (zone->checkns)(zone, name, owner, &a, |
2077 | 0 | &aaaa); |
2078 | 0 | } |
2079 | 0 | dns_rdataset_cleanup(&a); |
2080 | 0 | dns_rdataset_cleanup(&aaaa); |
2081 | 0 | return answer; |
2082 | 0 | } |
2083 | 0 | } |
2084 | | |
2085 | 0 | dns_name_format(owner, ownerbuf, sizeof ownerbuf); |
2086 | 0 | dns_name_format(name, namebuf, sizeof namebuf); |
2087 | 0 | if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN || |
2088 | 0 | result == DNS_R_EMPTYNAME || result == DNS_R_DELEGATION) |
2089 | 0 | { |
2090 | 0 | const char *what; |
2091 | 0 | bool required = false; |
2092 | 0 | if (dns_name_issubdomain(name, owner)) { |
2093 | 0 | what = "REQUIRED GLUE "; |
2094 | 0 | required = true; |
2095 | 0 | } else if (result == DNS_R_DELEGATION) { |
2096 | 0 | what = "SIBLING GLUE "; |
2097 | 0 | } else { |
2098 | 0 | what = ""; |
2099 | 0 | } |
2100 | |
|
2101 | 0 | if (result != DNS_R_DELEGATION || required || |
2102 | 0 | DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKSIBLING)) |
2103 | 0 | { |
2104 | 0 | dns_zone_log(zone, level, |
2105 | 0 | "%s/NS '%s' has no %s" |
2106 | 0 | "address records (A or AAAA)", |
2107 | 0 | ownerbuf, namebuf, what); |
2108 | | /* |
2109 | | * Log missing address record. |
2110 | | */ |
2111 | 0 | if (result == DNS_R_DELEGATION && zone->checkns != NULL) |
2112 | 0 | { |
2113 | 0 | (void)(zone->checkns)(zone, name, owner, &a, |
2114 | 0 | &aaaa); |
2115 | 0 | } |
2116 | | /* XXX950 make fatal for 9.5.0. */ |
2117 | | /* answer = false; */ |
2118 | 0 | } |
2119 | 0 | } else if (result == DNS_R_CNAME) { |
2120 | 0 | dns_zone_log(zone, level, "%s/NS '%s' is a CNAME (illegal)", |
2121 | 0 | ownerbuf, namebuf); |
2122 | | /* XXX950 make fatal for 9.5.0. */ |
2123 | | /* answer = false; */ |
2124 | 0 | } else if (result == DNS_R_DNAME) { |
2125 | 0 | dns_name_format(foundname, altbuf, sizeof altbuf); |
2126 | 0 | dns_zone_log(zone, level, |
2127 | 0 | "%s/NS '%s' is below a DNAME '%s' (illegal)", |
2128 | 0 | ownerbuf, namebuf, altbuf); |
2129 | | /* XXX950 make fatal for 9.5.0. */ |
2130 | | /* answer = false; */ |
2131 | 0 | } |
2132 | |
|
2133 | 0 | dns_rdataset_cleanup(&a); |
2134 | 0 | dns_rdataset_cleanup(&aaaa); |
2135 | 0 | return answer; |
2136 | 0 | } |
2137 | | |
2138 | | static bool |
2139 | | zone_rrset_check_dup(dns_zone_t *zone, dns_name_t *owner, |
2140 | 0 | dns_rdataset_t *rdataset) { |
2141 | 0 | dns_rdataset_t tmprdataset; |
2142 | 0 | bool answer = true; |
2143 | 0 | bool format = true; |
2144 | 0 | int level = ISC_LOG_WARNING; |
2145 | 0 | char ownerbuf[DNS_NAME_FORMATSIZE]; |
2146 | 0 | char typebuf[DNS_RDATATYPE_FORMATSIZE]; |
2147 | 0 | unsigned int count1 = 0; |
2148 | |
|
2149 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKDUPRRFAIL)) { |
2150 | 0 | level = ISC_LOG_ERROR; |
2151 | 0 | } |
2152 | |
|
2153 | 0 | dns_rdataset_init(&tmprdataset); |
2154 | 0 | DNS_RDATASET_FOREACH(rdataset) { |
2155 | 0 | dns_rdata_t rdata1 = DNS_RDATA_INIT; |
2156 | 0 | unsigned int count2 = 0; |
2157 | |
|
2158 | 0 | count1++; |
2159 | 0 | dns_rdataset_current(rdataset, &rdata1); |
2160 | 0 | dns_rdataset_clone(rdataset, &tmprdataset); |
2161 | 0 | DNS_RDATASET_FOREACH(&tmprdataset) { |
2162 | 0 | dns_rdata_t rdata2 = DNS_RDATA_INIT; |
2163 | 0 | count2++; |
2164 | 0 | if (count1 >= count2) { |
2165 | 0 | continue; |
2166 | 0 | } |
2167 | 0 | dns_rdataset_current(&tmprdataset, &rdata2); |
2168 | 0 | if (dns_rdata_casecompare(&rdata1, &rdata2) == 0) { |
2169 | 0 | if (format) { |
2170 | 0 | dns_name_format(owner, ownerbuf, |
2171 | 0 | sizeof ownerbuf); |
2172 | 0 | dns_rdatatype_format(rdata1.type, |
2173 | 0 | typebuf, |
2174 | 0 | sizeof(typebuf)); |
2175 | 0 | format = false; |
2176 | 0 | } |
2177 | 0 | dns_zone_log(zone, level, |
2178 | 0 | "%s/%s has " |
2179 | 0 | "semantically identical records", |
2180 | 0 | ownerbuf, typebuf); |
2181 | 0 | if (level == ISC_LOG_ERROR) { |
2182 | 0 | answer = false; |
2183 | 0 | } |
2184 | 0 | break; |
2185 | 0 | } |
2186 | 0 | } |
2187 | 0 | dns_rdataset_disassociate(&tmprdataset); |
2188 | 0 | if (!format) { |
2189 | 0 | break; |
2190 | 0 | } |
2191 | 0 | } |
2192 | 0 | return answer; |
2193 | 0 | } |
2194 | | |
2195 | | static bool |
2196 | 0 | zone_check_dup(dns_zone_t *zone, dns_db_t *db) { |
2197 | 0 | dns_dbiterator_t *dbiterator = NULL; |
2198 | 0 | dns_dbnode_t *node = NULL; |
2199 | 0 | dns_fixedname_t fixed; |
2200 | 0 | dns_name_t *name = dns_fixedname_initname(&fixed); |
2201 | 0 | dns_rdatasetiter_t *rdsit = NULL; |
2202 | 0 | bool ok = true; |
2203 | 0 | isc_result_t result; |
2204 | |
|
2205 | 0 | result = dns_db_createiterator(db, 0, &dbiterator); |
2206 | 0 | if (result != ISC_R_SUCCESS) { |
2207 | 0 | return true; |
2208 | 0 | } |
2209 | | |
2210 | 0 | DNS_DBITERATOR_FOREACH(dbiterator) { |
2211 | 0 | result = dns_dbiterator_current(dbiterator, &node, name); |
2212 | 0 | if (result != ISC_R_SUCCESS) { |
2213 | 0 | continue; |
2214 | 0 | } |
2215 | | |
2216 | 0 | result = dns_db_allrdatasets(db, node, NULL, 0, 0, &rdsit); |
2217 | 0 | if (result != ISC_R_SUCCESS) { |
2218 | 0 | continue; |
2219 | 0 | } |
2220 | | |
2221 | 0 | DNS_RDATASETITER_FOREACH(rdsit) { |
2222 | 0 | dns_rdataset_t rdataset = DNS_RDATASET_INIT; |
2223 | 0 | dns_rdatasetiter_current(rdsit, &rdataset); |
2224 | 0 | if (!zone_rrset_check_dup(zone, name, &rdataset)) { |
2225 | 0 | ok = false; |
2226 | 0 | } |
2227 | 0 | dns_rdataset_disassociate(&rdataset); |
2228 | 0 | } |
2229 | 0 | dns_rdatasetiter_destroy(&rdsit); |
2230 | 0 | dns_db_detachnode(&node); |
2231 | 0 | } |
2232 | |
|
2233 | 0 | if (node != NULL) { |
2234 | 0 | dns_db_detachnode(&node); |
2235 | 0 | } |
2236 | 0 | dns_dbiterator_destroy(&dbiterator); |
2237 | |
|
2238 | 0 | return ok; |
2239 | 0 | } |
2240 | | |
2241 | | static bool |
2242 | 0 | isspf(const dns_rdata_t *rdata) { |
2243 | 0 | char buf[1024]; |
2244 | 0 | const unsigned char *data = rdata->data; |
2245 | 0 | unsigned int rdl = rdata->length, i = 0, tl, len; |
2246 | |
|
2247 | 0 | while (rdl > 0U) { |
2248 | 0 | len = tl = *data; |
2249 | 0 | ++data; |
2250 | 0 | --rdl; |
2251 | 0 | INSIST(tl <= rdl); |
2252 | 0 | if (len > sizeof(buf) - i - 1) { |
2253 | 0 | len = sizeof(buf) - i - 1; |
2254 | 0 | } |
2255 | 0 | memmove(buf + i, data, len); |
2256 | 0 | i += len; |
2257 | 0 | data += tl; |
2258 | 0 | rdl -= tl; |
2259 | 0 | } |
2260 | |
|
2261 | 0 | if (i < 6U) { |
2262 | 0 | return false; |
2263 | 0 | } |
2264 | | |
2265 | 0 | buf[i] = 0; |
2266 | 0 | if (strncmp(buf, "v=spf1", 6) == 0 && (buf[6] == 0 || buf[6] == ' ')) { |
2267 | 0 | return true; |
2268 | 0 | } |
2269 | 0 | return false; |
2270 | 0 | } |
2271 | | |
2272 | | static bool |
2273 | | zone_is_served_by(dns_zone_t *zone, dns_db_t *db, dns_rdatatype_t type, |
2274 | 0 | dns_name_t *name) { |
2275 | 0 | dns_rdataset_t rdataset; |
2276 | 0 | dns_fixedname_t found; |
2277 | 0 | dns_name_t *foundname = dns_fixedname_initname(&found); |
2278 | 0 | isc_result_t result; |
2279 | | |
2280 | | /* |
2281 | | * Outside of zone, assume good when loading in named. |
2282 | | */ |
2283 | 0 | if (!dns_name_issubdomain(name, &zone->origin)) { |
2284 | 0 | if (zone->checkisservedby != NULL) { |
2285 | 0 | return zone->checkisservedby(zone, type, name); |
2286 | 0 | } |
2287 | 0 | return true; |
2288 | 0 | } |
2289 | | |
2290 | 0 | dns_rdataset_init(&rdataset); |
2291 | 0 | result = dns_db_find(db, name, NULL, type, 0, 0, foundname, &rdataset, |
2292 | 0 | NULL); |
2293 | 0 | dns_rdataset_cleanup(&rdataset); |
2294 | 0 | switch (result) { |
2295 | 0 | case DNS_R_DELEGATION: |
2296 | 0 | if (zone->checkisservedby != NULL) { |
2297 | 0 | return zone->checkisservedby(zone, type, name); |
2298 | 0 | } |
2299 | | /* |
2300 | | * Treat as success. |
2301 | | */ |
2302 | 0 | return true; |
2303 | 0 | case ISC_R_SUCCESS: |
2304 | 0 | return true; |
2305 | 0 | default: |
2306 | 0 | return false; |
2307 | 0 | } |
2308 | 0 | } |
2309 | | |
2310 | | static bool |
2311 | 0 | integrity_checks(dns_zone_t *zone, dns_db_t *db) { |
2312 | 0 | dns_dbiterator_t *dbiterator = NULL; |
2313 | 0 | dns_dbnode_t *node = NULL; |
2314 | 0 | dns_rdataset_t rdataset; |
2315 | 0 | dns_fixedname_t fixed; |
2316 | 0 | dns_fixedname_t fixedbottom; |
2317 | 0 | dns_rdata_mx_t mx; |
2318 | 0 | dns_rdata_ns_t ns; |
2319 | 0 | dns_rdata_in_srv_t srv; |
2320 | 0 | dns_name_t *name; |
2321 | 0 | dns_name_t *bottom; |
2322 | 0 | isc_result_t result; |
2323 | 0 | bool ok = true, have_spf, have_txt; |
2324 | 0 | bool has_a = false; |
2325 | 0 | bool has_aaaa = false; |
2326 | 0 | int level; |
2327 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
2328 | 0 | bool logged_algorithm[DST_MAX_ALGS]; |
2329 | 0 | bool logged_digest_type[DNS_DSDIGEST_MAX + 1]; |
2330 | |
|
2331 | 0 | name = dns_fixedname_initname(&fixed); |
2332 | 0 | bottom = dns_fixedname_initname(&fixedbottom); |
2333 | 0 | dns_rdataset_init(&rdataset); |
2334 | |
|
2335 | 0 | result = dns_db_createiterator(db, 0, &dbiterator); |
2336 | 0 | if (result != ISC_R_SUCCESS) { |
2337 | 0 | return true; |
2338 | 0 | } |
2339 | | |
2340 | 0 | DNS_DBITERATOR_FOREACH(dbiterator) { |
2341 | 0 | CHECK(dns_dbiterator_current(dbiterator, &node, name)); |
2342 | | |
2343 | | /* |
2344 | | * Is this name visible in the zone? |
2345 | | */ |
2346 | 0 | if (!dns_name_issubdomain(name, &zone->origin) || |
2347 | 0 | (dns_name_countlabels(bottom) > 0 && |
2348 | 0 | dns_name_issubdomain(name, bottom))) |
2349 | 0 | { |
2350 | 0 | goto next; |
2351 | 0 | } |
2352 | | |
2353 | 0 | dns_dbiterator_pause(dbiterator); |
2354 | | |
2355 | | /* |
2356 | | * Check for deprecated KEY algorithms |
2357 | | */ |
2358 | 0 | result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_key, |
2359 | 0 | 0, 0, &rdataset, NULL); |
2360 | 0 | if (result != ISC_R_SUCCESS) { |
2361 | 0 | goto checkforns; |
2362 | 0 | } |
2363 | | |
2364 | 0 | memset(logged_algorithm, 0, sizeof(logged_algorithm)); |
2365 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2366 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2367 | 0 | dns_rdata_key_t key; |
2368 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2369 | |
|
2370 | 0 | result = dns_rdata_tostruct(&rdata, &key, NULL); |
2371 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
2372 | | |
2373 | | /* |
2374 | | * If we ever deprecate a private algorithm use |
2375 | | * dst_algorithm_fromdata() here. |
2376 | | */ |
2377 | 0 | switch (key.algorithm) { |
2378 | 0 | case DNS_KEYALG_RSASHA1: |
2379 | 0 | case DNS_KEYALG_NSEC3RSASHA1: |
2380 | 0 | if (!logged_algorithm[key.algorithm]) { |
2381 | 0 | char algbuf[DNS_SECALG_FORMATSIZE]; |
2382 | 0 | dns_name_format(name, namebuf, |
2383 | 0 | sizeof(namebuf)); |
2384 | 0 | dns_secalg_format(key.algorithm, algbuf, |
2385 | 0 | sizeof(algbuf)); |
2386 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
2387 | 0 | "%s/KEY deprecated " |
2388 | 0 | "algorithm %u (%s)", |
2389 | 0 | namebuf, key.algorithm, |
2390 | 0 | algbuf); |
2391 | 0 | logged_algorithm[key.algorithm] = true; |
2392 | 0 | } |
2393 | 0 | break; |
2394 | 0 | default: |
2395 | 0 | break; |
2396 | 0 | } |
2397 | 0 | } |
2398 | 0 | dns_rdataset_disassociate(&rdataset); |
2399 | |
|
2400 | 0 | checkforns: |
2401 | | /* |
2402 | | * Don't check the NS records at the origin. |
2403 | | */ |
2404 | 0 | if (dns_name_equal(name, &zone->origin)) { |
2405 | 0 | goto checkfords; |
2406 | 0 | } |
2407 | | |
2408 | 0 | result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_ns, |
2409 | 0 | 0, 0, &rdataset, NULL); |
2410 | 0 | if (result != ISC_R_SUCCESS) { |
2411 | 0 | goto checkfords; |
2412 | 0 | } |
2413 | | |
2414 | | /* |
2415 | | * Remember bottom of zone due to NS. |
2416 | | */ |
2417 | 0 | dns_name_copy(name, bottom); |
2418 | |
|
2419 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2420 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2421 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2422 | |
|
2423 | 0 | result = dns_rdata_tostruct(&rdata, &ns, NULL); |
2424 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
2425 | 0 | if (!zone_check_glue(zone, db, &has_a, &has_aaaa, |
2426 | 0 | &ns.name, name)) |
2427 | 0 | { |
2428 | 0 | ok = false; |
2429 | 0 | } |
2430 | 0 | } |
2431 | 0 | dns_rdataset_disassociate(&rdataset); |
2432 | | |
2433 | | /* |
2434 | | * Check for deprecated DS digest types. |
2435 | | */ |
2436 | 0 | result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_ds, |
2437 | 0 | 0, 0, &rdataset, NULL); |
2438 | 0 | if (result != ISC_R_SUCCESS) { |
2439 | 0 | goto next; |
2440 | 0 | } |
2441 | | |
2442 | 0 | memset(logged_algorithm, 0, sizeof(logged_algorithm)); |
2443 | 0 | memset(logged_digest_type, 0, sizeof(logged_digest_type)); |
2444 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2445 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2446 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2447 | 0 | dns_rdata_ds_t ds; |
2448 | |
|
2449 | 0 | result = dns_rdata_tostruct(&rdata, &ds, NULL); |
2450 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
2451 | 0 | switch (ds.digest_type) { |
2452 | 0 | case DNS_DSDIGEST_SHA1: |
2453 | 0 | case DNS_DSDIGEST_GOST: |
2454 | 0 | if (!logged_digest_type[ds.digest_type]) { |
2455 | 0 | char algbuf[DNS_DSDIGEST_FORMATSIZE]; |
2456 | 0 | dns_name_format(name, namebuf, |
2457 | 0 | sizeof(namebuf)); |
2458 | 0 | dns_dsdigest_format(ds.digest_type, |
2459 | 0 | algbuf, |
2460 | 0 | sizeof(algbuf)); |
2461 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
2462 | 0 | "%s/DS deprecated digest " |
2463 | 0 | "type %u (%s)", |
2464 | 0 | namebuf, ds.digest_type, |
2465 | 0 | algbuf); |
2466 | 0 | logged_digest_type[ds.digest_type] = |
2467 | 0 | true; |
2468 | 0 | } |
2469 | 0 | break; |
2470 | 0 | } |
2471 | | |
2472 | | /* |
2473 | | * If we ever deprecate a private algorithm use |
2474 | | * dst_algorithm_fromdata() here. |
2475 | | */ |
2476 | 0 | switch (ds.algorithm) { |
2477 | 0 | case DNS_KEYALG_RSASHA1: |
2478 | 0 | case DNS_KEYALG_NSEC3RSASHA1: |
2479 | 0 | if (!logged_algorithm[ds.algorithm]) { |
2480 | 0 | char algbuf[DNS_SECALG_FORMATSIZE]; |
2481 | 0 | dns_name_format(name, namebuf, |
2482 | 0 | sizeof(namebuf)); |
2483 | 0 | dns_secalg_format(ds.algorithm, algbuf, |
2484 | 0 | sizeof(algbuf)); |
2485 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
2486 | 0 | "%s/DS deprecated algorithm " |
2487 | 0 | "%u (%s)", |
2488 | 0 | namebuf, ds.algorithm, |
2489 | 0 | algbuf); |
2490 | 0 | logged_algorithm[ds.algorithm] = true; |
2491 | 0 | } |
2492 | 0 | break; |
2493 | 0 | } |
2494 | 0 | } |
2495 | 0 | dns_rdataset_disassociate(&rdataset); |
2496 | |
|
2497 | 0 | goto next; |
2498 | | |
2499 | 0 | checkfords: |
2500 | 0 | result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_ds, |
2501 | 0 | 0, 0, &rdataset, NULL); |
2502 | 0 | if (result != ISC_R_SUCCESS) { |
2503 | 0 | goto checkfordname; |
2504 | 0 | } |
2505 | 0 | dns_rdataset_disassociate(&rdataset); |
2506 | |
|
2507 | 0 | if (zone->type == dns_zone_primary) { |
2508 | 0 | level = ISC_LOG_ERROR; |
2509 | 0 | ok = false; |
2510 | 0 | } else { |
2511 | 0 | level = ISC_LOG_WARNING; |
2512 | 0 | } |
2513 | 0 | dns_name_format(name, namebuf, sizeof(namebuf)); |
2514 | 0 | dns_zone_log(zone, level, "DS not at delegation point (%s)", |
2515 | 0 | namebuf); |
2516 | |
|
2517 | 0 | checkfordname: |
2518 | 0 | result = dns_db_findrdataset(db, node, NULL, |
2519 | 0 | dns_rdatatype_dname, 0, 0, |
2520 | 0 | &rdataset, NULL); |
2521 | 0 | if (result == ISC_R_SUCCESS) { |
2522 | | /* |
2523 | | * Remember bottom of zone due to DNAME. |
2524 | | */ |
2525 | 0 | dns_name_copy(name, bottom); |
2526 | 0 | dns_rdataset_disassociate(&rdataset); |
2527 | 0 | } |
2528 | |
|
2529 | 0 | result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_mx, |
2530 | 0 | 0, 0, &rdataset, NULL); |
2531 | 0 | if (result != ISC_R_SUCCESS) { |
2532 | 0 | goto checksrv; |
2533 | 0 | } |
2534 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2535 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2536 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2537 | |
|
2538 | 0 | result = dns_rdata_tostruct(&rdata, &mx, NULL); |
2539 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
2540 | 0 | if (!zone_check_mx(zone, db, &mx.mx, name)) { |
2541 | 0 | ok = false; |
2542 | 0 | } |
2543 | 0 | } |
2544 | 0 | dns_rdataset_disassociate(&rdataset); |
2545 | |
|
2546 | 0 | checksrv: |
2547 | 0 | if (zone->rdclass != dns_rdataclass_in) { |
2548 | 0 | goto next; |
2549 | 0 | } |
2550 | 0 | result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_srv, |
2551 | 0 | 0, 0, &rdataset, NULL); |
2552 | 0 | if (result != ISC_R_SUCCESS) { |
2553 | 0 | goto checkforaaaa; |
2554 | 0 | } |
2555 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2556 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2557 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2558 | |
|
2559 | 0 | result = dns_rdata_tostruct(&rdata, &srv, NULL); |
2560 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
2561 | 0 | if (!zone_check_srv(zone, db, &srv.target, name)) { |
2562 | 0 | ok = false; |
2563 | 0 | } |
2564 | 0 | } |
2565 | 0 | dns_rdataset_disassociate(&rdataset); |
2566 | |
|
2567 | 0 | checkforaaaa: |
2568 | | /* |
2569 | | * Check if there is an A or AAAA RRset in the zone. |
2570 | | */ |
2571 | 0 | if (!has_a) { |
2572 | 0 | result = dns_db_findrdataset(db, node, NULL, |
2573 | 0 | dns_rdatatype_a, 0, 0, |
2574 | 0 | &rdataset, NULL); |
2575 | 0 | if (result == ISC_R_SUCCESS) { |
2576 | 0 | has_a = true; |
2577 | 0 | dns_rdataset_disassociate(&rdataset); |
2578 | 0 | } |
2579 | 0 | } |
2580 | 0 | if (!has_aaaa) { |
2581 | 0 | result = dns_db_findrdataset(db, node, NULL, |
2582 | 0 | dns_rdatatype_aaaa, 0, 0, |
2583 | 0 | &rdataset, NULL); |
2584 | 0 | if (result == ISC_R_SUCCESS) { |
2585 | 0 | has_aaaa = true; |
2586 | 0 | dns_rdataset_disassociate(&rdataset); |
2587 | 0 | } |
2588 | 0 | } |
2589 | | |
2590 | | /* |
2591 | | * Check if there is a type SPF record without an |
2592 | | * SPF-formatted type TXT record also being present. |
2593 | | */ |
2594 | 0 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKSPF)) { |
2595 | 0 | goto next; |
2596 | 0 | } |
2597 | 0 | if (zone->rdclass != dns_rdataclass_in) { |
2598 | 0 | goto next; |
2599 | 0 | } |
2600 | 0 | have_spf = have_txt = false; |
2601 | 0 | result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_spf, |
2602 | 0 | 0, 0, &rdataset, NULL); |
2603 | 0 | if (result == ISC_R_SUCCESS) { |
2604 | 0 | dns_rdataset_disassociate(&rdataset); |
2605 | 0 | have_spf = true; |
2606 | 0 | } |
2607 | 0 | result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_txt, |
2608 | 0 | 0, 0, &rdataset, NULL); |
2609 | 0 | if (result != ISC_R_SUCCESS) { |
2610 | 0 | goto notxt; |
2611 | 0 | } |
2612 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2613 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2614 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2615 | |
|
2616 | 0 | have_txt = isspf(&rdata); |
2617 | 0 | if (have_txt) { |
2618 | 0 | break; |
2619 | 0 | } |
2620 | 0 | } |
2621 | 0 | dns_rdataset_disassociate(&rdataset); |
2622 | |
|
2623 | 0 | notxt: |
2624 | 0 | if (have_spf && !have_txt) { |
2625 | 0 | dns_name_format(name, namebuf, sizeof(namebuf)); |
2626 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
2627 | 0 | "'%s' found type " |
2628 | 0 | "SPF record but no SPF TXT record found, " |
2629 | 0 | "add matching type TXT record", |
2630 | 0 | namebuf); |
2631 | 0 | } |
2632 | |
|
2633 | 0 | next: |
2634 | 0 | dns_db_detachnode(&node); |
2635 | 0 | } |
2636 | | |
2637 | 0 | if (has_a) { |
2638 | 0 | has_a = false; |
2639 | 0 | result = dns_db_find(db, &zone->origin, NULL, dns_rdatatype_ns, |
2640 | 0 | 0, 0, name, &rdataset, NULL); |
2641 | 0 | if (result != ISC_R_SUCCESS) { |
2642 | 0 | dns_rdataset_cleanup(&rdataset); |
2643 | 0 | goto cleanup; |
2644 | 0 | } |
2645 | | |
2646 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2647 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2648 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2649 | |
|
2650 | 0 | result = dns_rdata_tostruct(&rdata, &ns, NULL); |
2651 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
2652 | 0 | if (zone_is_served_by(zone, db, dns_rdatatype_a, |
2653 | 0 | &ns.name)) |
2654 | 0 | { |
2655 | 0 | has_a = true; |
2656 | 0 | break; |
2657 | 0 | } |
2658 | 0 | } |
2659 | 0 | dns_rdataset_disassociate(&rdataset); |
2660 | 0 | if (!has_a) { |
2661 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
2662 | 0 | "zone has A records but is not served " |
2663 | 0 | "by IPv4 servers"); |
2664 | 0 | } |
2665 | 0 | } |
2666 | | |
2667 | 0 | if (has_aaaa) { |
2668 | 0 | has_aaaa = false; |
2669 | 0 | result = dns_db_find(db, &zone->origin, NULL, dns_rdatatype_ns, |
2670 | 0 | 0, 0, name, &rdataset, NULL); |
2671 | 0 | if (result != ISC_R_SUCCESS) { |
2672 | 0 | dns_rdataset_cleanup(&rdataset); |
2673 | 0 | goto cleanup; |
2674 | 0 | } |
2675 | | |
2676 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2677 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2678 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2679 | |
|
2680 | 0 | result = dns_rdata_tostruct(&rdata, &ns, NULL); |
2681 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
2682 | 0 | if (zone_is_served_by(zone, db, dns_rdatatype_aaaa, |
2683 | 0 | &ns.name)) |
2684 | 0 | { |
2685 | 0 | has_aaaa = true; |
2686 | 0 | break; |
2687 | 0 | } |
2688 | 0 | } |
2689 | 0 | dns_rdataset_disassociate(&rdataset); |
2690 | 0 | if (!has_aaaa) { |
2691 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
2692 | 0 | "zone has AAAA records but is not served " |
2693 | 0 | "by IPv6 servers"); |
2694 | 0 | } |
2695 | 0 | } |
2696 | | |
2697 | 0 | cleanup: |
2698 | 0 | if (node != NULL) { |
2699 | 0 | dns_db_detachnode(&node); |
2700 | 0 | } |
2701 | 0 | dns_dbiterator_destroy(&dbiterator); |
2702 | |
|
2703 | 0 | return ok; |
2704 | 0 | } |
2705 | | |
2706 | | /* |
2707 | | * OpenSSL verification of RSA keys with exponent 3 is known to be |
2708 | | * broken prior OpenSSL 0.9.8c/0.9.7k. Look for such keys and warn |
2709 | | * if they are in use. |
2710 | | */ |
2711 | | static void |
2712 | 2 | zone_check_dnskeys(dns_zone_t *zone, dns_db_t *db) { |
2713 | 2 | dns_dbnode_t *node = NULL; |
2714 | 2 | dns_dbversion_t *version = NULL; |
2715 | 2 | dns_rdata_dnskey_t dnskey; |
2716 | 2 | dns_rdataset_t rdataset; |
2717 | 2 | isc_result_t result; |
2718 | 2 | bool logged_algorithm[DST_MAX_ALGS] = { 0 }; |
2719 | 2 | bool alldeprecated = true; |
2720 | | |
2721 | 2 | CHECK(dns_db_findnode(db, &zone->origin, false, &node)); |
2722 | | |
2723 | 2 | dns_db_currentversion(db, &version); |
2724 | 2 | dns_rdataset_init(&rdataset); |
2725 | 2 | CHECK(dns_db_findrdataset(db, node, version, dns_rdatatype_dnskey, |
2726 | 2 | dns_rdatatype_none, 0, &rdataset, NULL)); |
2727 | |
|
2728 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2729 | 0 | char algbuf[DNS_SECALG_FORMATSIZE]; |
2730 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2731 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2732 | 0 | result = dns_rdata_tostruct(&rdata, &dnskey, NULL); |
2733 | 0 | INSIST(result == ISC_R_SUCCESS); |
2734 | | |
2735 | | /* |
2736 | | * RFC 3110, section 4: Performance Considerations: |
2737 | | * |
2738 | | * A public exponent of 3 minimizes the effort needed to verify |
2739 | | * a signature. Use of 3 as the public exponent is weak for |
2740 | | * confidentiality uses since, if the same data can be collected |
2741 | | * encrypted under three different keys with an exponent of 3 |
2742 | | * then, using the Chinese Remainder Theorem [NETSEC], the |
2743 | | * original plain text can be easily recovered. If a key is |
2744 | | * known to be used only for authentication, as is the case with |
2745 | | * DNSSEC, then an exponent of 3 is acceptable. However other |
2746 | | * applications in the future may wish to leverage DNS |
2747 | | * distributed keys for applications that do require |
2748 | | * confidentiality. For keys which might have such other uses, |
2749 | | * a more conservative choice would be 65537 (F4, the fourth |
2750 | | * fermat number). |
2751 | | */ |
2752 | 0 | if (dnskey.datalen > 1 && dnskey.data[0] == 1 && |
2753 | 0 | dnskey.data[1] == 3 && |
2754 | 0 | (dnskey.algorithm == DNS_KEYALG_RSAMD5 || |
2755 | 0 | dnskey.algorithm == DNS_KEYALG_RSASHA1 || |
2756 | 0 | dnskey.algorithm == DNS_KEYALG_NSEC3RSASHA1 || |
2757 | 0 | dnskey.algorithm == DNS_KEYALG_RSASHA256 || |
2758 | 0 | dnskey.algorithm == DNS_KEYALG_RSASHA512)) |
2759 | 0 | { |
2760 | 0 | char algorithm[DNS_SECALG_FORMATSIZE]; |
2761 | 0 | isc_region_t r; |
2762 | |
|
2763 | 0 | dns_rdata_toregion(&rdata, &r); |
2764 | 0 | dns_secalg_format(dnskey.algorithm, algorithm, |
2765 | 0 | sizeof(algorithm)); |
2766 | |
|
2767 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
2768 | 0 | "weak %s (%u) key found (exponent=3, id=%u)", |
2769 | 0 | algorithm, dnskey.algorithm, |
2770 | 0 | dst_region_computeid(&r)); |
2771 | 0 | } |
2772 | 0 | switch (dnskey.algorithm) { |
2773 | 0 | case DNS_KEYALG_RSAMD5: |
2774 | 0 | case DNS_KEYALG_DSA: |
2775 | 0 | case DNS_KEYALG_RSASHA1: |
2776 | 0 | case DNS_KEYALG_NSEC3DSA: |
2777 | 0 | case DNS_KEYALG_NSEC3RSASHA1: |
2778 | 0 | case DNS_KEYALG_ECCGOST: |
2779 | 0 | if (!logged_algorithm[dnskey.algorithm]) { |
2780 | 0 | dns_secalg_format(dnskey.algorithm, algbuf, |
2781 | 0 | sizeof(algbuf)); |
2782 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
2783 | 0 | "deprecated DNSKEY algorithm found: " |
2784 | 0 | "%u (%s)\n", |
2785 | 0 | dnskey.algorithm, algbuf); |
2786 | 0 | logged_algorithm[dnskey.algorithm] = true; |
2787 | 0 | } |
2788 | 0 | break; |
2789 | 0 | default: |
2790 | 0 | alldeprecated = false; |
2791 | 0 | break; |
2792 | 0 | } |
2793 | 0 | } |
2794 | 0 | dns_rdataset_disassociate(&rdataset); |
2795 | |
|
2796 | 0 | if (alldeprecated) { |
2797 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
2798 | 0 | "all DNSKEY algorithms found are deprecated"); |
2799 | 0 | } |
2800 | |
|
2801 | 2 | cleanup: |
2802 | 2 | if (node != NULL) { |
2803 | 2 | dns_db_detachnode(&node); |
2804 | 2 | } |
2805 | 2 | if (version != NULL) { |
2806 | 2 | dns_db_closeversion(db, &version, false); |
2807 | 2 | } |
2808 | 2 | } |
2809 | | |
2810 | | static void |
2811 | 0 | resume_signingwithkey(dns_zone_t *zone) { |
2812 | 0 | dns_dbnode_t *node = NULL; |
2813 | 0 | dns_dbversion_t *version = NULL; |
2814 | 0 | dns_rdataset_t rdataset; |
2815 | 0 | isc_result_t result; |
2816 | 0 | dns_db_t *db = NULL; |
2817 | |
|
2818 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
2819 | 0 | if (zone->db != NULL) { |
2820 | 0 | dns_db_attach(zone->db, &db); |
2821 | 0 | } |
2822 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
2823 | 0 | if (db == NULL) { |
2824 | 0 | goto cleanup; |
2825 | 0 | } |
2826 | | |
2827 | 0 | CHECK(dns_db_findnode(db, &zone->origin, false, &node)); |
2828 | |
|
2829 | 0 | dns_db_currentversion(db, &version); |
2830 | 0 | dns_rdataset_init(&rdataset); |
2831 | 0 | result = dns_db_findrdataset(db, node, version, zone->privatetype, |
2832 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
2833 | 0 | if (result != ISC_R_SUCCESS) { |
2834 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
2835 | 0 | goto cleanup; |
2836 | 0 | } |
2837 | | |
2838 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
2839 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
2840 | 0 | dst_algorithm_t alg; |
2841 | |
|
2842 | 0 | dns_rdataset_current(&rdataset, &rdata); |
2843 | | /* |
2844 | | * Old or New Forms |
2845 | | */ |
2846 | 0 | if ((rdata.length != OLD_SIGNING_RECORD_SIZE && |
2847 | 0 | rdata.length != SIGNING_RECORD_SIZE) || |
2848 | 0 | rdata.data[0] == 0 || rdata.data[4] != 0) |
2849 | 0 | { |
2850 | 0 | continue; |
2851 | 0 | } |
2852 | 0 | alg = (rdata.length == OLD_SIGNING_RECORD_SIZE) |
2853 | 0 | ? rdata.data[0] |
2854 | 0 | : ((rdata.data[5] << 8) | rdata.data[6]); |
2855 | 0 | result = zone_signwithkey(zone, alg, |
2856 | 0 | (rdata.data[1] << 8) | rdata.data[2], |
2857 | 0 | rdata.data[3], false); |
2858 | 0 | if (result != ISC_R_SUCCESS) { |
2859 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
2860 | 0 | "zone_signwithkey failed: %s", |
2861 | 0 | isc_result_totext(result)); |
2862 | 0 | } |
2863 | 0 | } |
2864 | 0 | dns_rdataset_disassociate(&rdataset); |
2865 | |
|
2866 | 0 | cleanup: |
2867 | 0 | if (db != NULL) { |
2868 | 0 | if (node != NULL) { |
2869 | 0 | dns_db_detachnode(&node); |
2870 | 0 | } |
2871 | 0 | if (version != NULL) { |
2872 | 0 | dns_db_closeversion(db, &version, false); |
2873 | 0 | } |
2874 | 0 | dns_db_detach(&db); |
2875 | 0 | } |
2876 | 0 | } |
2877 | | |
2878 | | /* |
2879 | | * Initiate adding/removing NSEC3 records belonging to the chain defined by the |
2880 | | * supplied NSEC3PARAM RDATA. |
2881 | | * |
2882 | | * Zone must be locked by caller. |
2883 | | */ |
2884 | | static isc_result_t |
2885 | 0 | zone_addnsec3chain(dns_zone_t *zone, dns_rdata_nsec3param_t *nsec3param) { |
2886 | 0 | dns_nsec3chain_t *nsec3chain; |
2887 | 0 | dns_dbversion_t *version = NULL; |
2888 | 0 | bool nseconly = false, nsec3ok = false; |
2889 | 0 | isc_result_t result; |
2890 | 0 | isc_time_t now; |
2891 | 0 | unsigned int options = 0; |
2892 | 0 | char saltbuf[255 * 2 + 1]; |
2893 | 0 | char flags[sizeof("INITIAL|REMOVE|CREATE|NONSEC|OPTOUT")]; |
2894 | 0 | dns_db_t *db = NULL; |
2895 | |
|
2896 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
2897 | 0 | if (zone->db != NULL) { |
2898 | 0 | dns_db_attach(zone->db, &db); |
2899 | 0 | } |
2900 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
2901 | |
|
2902 | 0 | if (db == NULL) { |
2903 | 0 | result = ISC_R_SUCCESS; |
2904 | 0 | goto cleanup; |
2905 | 0 | } |
2906 | | |
2907 | | /* |
2908 | | * If this zone is not NSEC3-capable, attempting to remove any NSEC3 |
2909 | | * chain from it is pointless as it would not be possible for the |
2910 | | * latter to exist in the first place. |
2911 | | */ |
2912 | 0 | dns_db_currentversion(db, &version); |
2913 | 0 | result = dns_nsec_nseconly(db, version, NULL, &nseconly); |
2914 | 0 | nsec3ok = (result == ISC_R_SUCCESS && !nseconly); |
2915 | 0 | dns_db_closeversion(db, &version, false); |
2916 | 0 | if (!nsec3ok && (nsec3param->flags & DNS_NSEC3FLAG_REMOVE) == 0) { |
2917 | 0 | result = ISC_R_SUCCESS; |
2918 | 0 | goto cleanup; |
2919 | 0 | } |
2920 | | |
2921 | | /* |
2922 | | * Allocate and initialize structure preserving state of |
2923 | | * adding/removing records belonging to this NSEC3 chain between |
2924 | | * separate zone_nsec3chain() calls. |
2925 | | */ |
2926 | 0 | nsec3chain = isc_mem_get(zone->mctx, sizeof *nsec3chain); |
2927 | |
|
2928 | 0 | nsec3chain->magic = 0; |
2929 | 0 | nsec3chain->done = false; |
2930 | 0 | nsec3chain->db = NULL; |
2931 | 0 | nsec3chain->dbiterator = NULL; |
2932 | 0 | nsec3chain->nsec3param.common.rdclass = nsec3param->common.rdclass; |
2933 | 0 | nsec3chain->nsec3param.common.rdtype = nsec3param->common.rdtype; |
2934 | 0 | nsec3chain->nsec3param.hash = nsec3param->hash; |
2935 | 0 | nsec3chain->nsec3param.iterations = nsec3param->iterations; |
2936 | 0 | nsec3chain->nsec3param.flags = nsec3param->flags; |
2937 | 0 | nsec3chain->nsec3param.salt.length = nsec3param->salt.length; |
2938 | 0 | memmove(nsec3chain->salt, nsec3param->salt.base, |
2939 | 0 | nsec3param->salt.length); |
2940 | 0 | nsec3chain->nsec3param.salt.base = nsec3chain->salt; |
2941 | 0 | nsec3chain->seen_nsec = false; |
2942 | 0 | nsec3chain->delete_nsec = false; |
2943 | 0 | nsec3chain->save_delete_nsec = false; |
2944 | | |
2945 | | /* |
2946 | | * Log NSEC3 parameters defined by supplied NSEC3PARAM RDATA. |
2947 | | */ |
2948 | 0 | if (nsec3param->flags == 0) { |
2949 | 0 | strlcpy(flags, "NONE", sizeof(flags)); |
2950 | 0 | } else { |
2951 | 0 | flags[0] = '\0'; |
2952 | 0 | if ((nsec3param->flags & DNS_NSEC3FLAG_REMOVE) != 0) { |
2953 | 0 | strlcat(flags, "REMOVE", sizeof(flags)); |
2954 | 0 | } |
2955 | 0 | if ((nsec3param->flags & DNS_NSEC3FLAG_INITIAL) != 0) { |
2956 | 0 | if (flags[0] == '\0') { |
2957 | 0 | strlcpy(flags, "INITIAL", sizeof(flags)); |
2958 | 0 | } else { |
2959 | 0 | strlcat(flags, "|INITIAL", sizeof(flags)); |
2960 | 0 | } |
2961 | 0 | } |
2962 | 0 | if ((nsec3param->flags & DNS_NSEC3FLAG_CREATE) != 0) { |
2963 | 0 | if (flags[0] == '\0') { |
2964 | 0 | strlcpy(flags, "CREATE", sizeof(flags)); |
2965 | 0 | } else { |
2966 | 0 | strlcat(flags, "|CREATE", sizeof(flags)); |
2967 | 0 | } |
2968 | 0 | } |
2969 | 0 | if ((nsec3param->flags & DNS_NSEC3FLAG_NONSEC) != 0) { |
2970 | 0 | if (flags[0] == '\0') { |
2971 | 0 | strlcpy(flags, "NONSEC", sizeof(flags)); |
2972 | 0 | } else { |
2973 | 0 | strlcat(flags, "|NONSEC", sizeof(flags)); |
2974 | 0 | } |
2975 | 0 | } |
2976 | 0 | if ((nsec3param->flags & DNS_NSEC3FLAG_OPTOUT) != 0) { |
2977 | 0 | if (flags[0] == '\0') { |
2978 | 0 | strlcpy(flags, "OPTOUT", sizeof(flags)); |
2979 | 0 | } else { |
2980 | 0 | strlcat(flags, "|OPTOUT", sizeof(flags)); |
2981 | 0 | } |
2982 | 0 | } |
2983 | 0 | } |
2984 | 0 | result = dns_nsec3param_salttotext(nsec3param, saltbuf, |
2985 | 0 | sizeof(saltbuf)); |
2986 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
2987 | 0 | dnssec_log(zone, ISC_LOG_INFO, "zone_addnsec3chain(%u,%s,%u,%s)", |
2988 | 0 | nsec3param->hash, flags, nsec3param->iterations, saltbuf); |
2989 | | |
2990 | | /* |
2991 | | * If the NSEC3 chain defined by the supplied NSEC3PARAM RDATA is |
2992 | | * currently being processed, interrupt its processing to avoid |
2993 | | * simultaneously adding and removing records for the same NSEC3 chain. |
2994 | | */ |
2995 | 0 | ISC_LIST_FOREACH(zone->nsec3chain, current, link) { |
2996 | 0 | if ((current->db == db) && |
2997 | 0 | (current->nsec3param.hash == nsec3param->hash) && |
2998 | 0 | (current->nsec3param.iterations == |
2999 | 0 | nsec3param->iterations) && |
3000 | 0 | (current->nsec3param.salt.length == |
3001 | 0 | nsec3param->salt.length) && |
3002 | 0 | memcmp(current->nsec3param.salt.base, nsec3param->salt.base, |
3003 | 0 | nsec3param->salt.length) == 0) |
3004 | 0 | { |
3005 | 0 | current->done = true; |
3006 | 0 | } |
3007 | 0 | } |
3008 | | |
3009 | | /* |
3010 | | * Attach zone database to the structure initialized above and create |
3011 | | * an iterator for it with appropriate options in order to avoid |
3012 | | * creating NSEC3 records for NSEC3 records. |
3013 | | */ |
3014 | 0 | dns_db_attach(db, &nsec3chain->db); |
3015 | 0 | if ((nsec3chain->nsec3param.flags & DNS_NSEC3FLAG_CREATE) != 0) { |
3016 | 0 | options = DNS_DB_NONSEC3; |
3017 | 0 | } |
3018 | 0 | result = dns_db_createiterator(nsec3chain->db, options, |
3019 | 0 | &nsec3chain->dbiterator); |
3020 | 0 | if (result == ISC_R_SUCCESS) { |
3021 | 0 | result = dns_dbiterator_first(nsec3chain->dbiterator); |
3022 | 0 | } |
3023 | 0 | if (result == ISC_R_SUCCESS) { |
3024 | | /* |
3025 | | * Database iterator initialization succeeded. We are now |
3026 | | * ready to kick off adding/removing records belonging to this |
3027 | | * NSEC3 chain. Append the structure initialized above to the |
3028 | | * "nsec3chain" list for the zone and set the appropriate zone |
3029 | | * timer so that zone_nsec3chain() is called as soon as |
3030 | | * possible. |
3031 | | */ |
3032 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
3033 | 0 | ISC_LIST_INITANDAPPEND(zone->nsec3chain, nsec3chain, link); |
3034 | 0 | nsec3chain = NULL; |
3035 | 0 | if (isc_time_isepoch(&zone->nsec3chaintime)) { |
3036 | 0 | now = isc_time_now(); |
3037 | 0 | zone->nsec3chaintime = now; |
3038 | 0 | if (zone->loop != NULL) { |
3039 | 0 | dns__zone_settimer(zone, now); |
3040 | 0 | } |
3041 | 0 | } |
3042 | 0 | } |
3043 | |
|
3044 | 0 | if (nsec3chain != NULL) { |
3045 | 0 | if (nsec3chain->db != NULL) { |
3046 | 0 | dns_db_detach(&nsec3chain->db); |
3047 | 0 | } |
3048 | 0 | if (nsec3chain->dbiterator != NULL) { |
3049 | 0 | dns_dbiterator_destroy(&nsec3chain->dbiterator); |
3050 | 0 | } |
3051 | 0 | isc_mem_put(zone->mctx, nsec3chain, sizeof *nsec3chain); |
3052 | 0 | } |
3053 | |
|
3054 | 0 | cleanup: |
3055 | 0 | if (db != NULL) { |
3056 | 0 | dns_db_detach(&db); |
3057 | 0 | } |
3058 | 0 | return result; |
3059 | 0 | } |
3060 | | |
3061 | | /* |
3062 | | * Find private-type records at the zone apex which signal that an NSEC3 chain |
3063 | | * should be added or removed. For each such record, extract NSEC3PARAM RDATA |
3064 | | * and pass it to zone_addnsec3chain(). |
3065 | | * |
3066 | | * Zone must be locked by caller. |
3067 | | */ |
3068 | | static void |
3069 | 0 | resume_addnsec3chain(dns_zone_t *zone) { |
3070 | 0 | dns_dbnode_t *node = NULL; |
3071 | 0 | dns_dbversion_t *version = NULL; |
3072 | 0 | dns_rdataset_t rdataset; |
3073 | 0 | isc_result_t result; |
3074 | 0 | dns_rdata_nsec3param_t nsec3param; |
3075 | 0 | bool nseconly = false, nsec3ok = false; |
3076 | 0 | dns_db_t *db = NULL; |
3077 | |
|
3078 | 0 | INSIST(LOCKED_ZONE(zone)); |
3079 | |
|
3080 | 0 | if (zone->privatetype == 0) { |
3081 | 0 | return; |
3082 | 0 | } |
3083 | | |
3084 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
3085 | 0 | if (zone->db != NULL) { |
3086 | 0 | dns_db_attach(zone->db, &db); |
3087 | 0 | } |
3088 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
3089 | 0 | if (db == NULL) { |
3090 | 0 | goto cleanup; |
3091 | 0 | } |
3092 | | |
3093 | 0 | CHECK(dns_db_findnode(db, &zone->origin, false, &node)); |
3094 | |
|
3095 | 0 | dns_db_currentversion(db, &version); |
3096 | | |
3097 | | /* |
3098 | | * In order to create NSEC3 chains we need the DNSKEY RRset at zone |
3099 | | * apex to exist and contain no keys using NSEC-only algorithms. |
3100 | | */ |
3101 | 0 | result = dns_nsec_nseconly(db, version, NULL, &nseconly); |
3102 | 0 | nsec3ok = (result == ISC_R_SUCCESS && !nseconly); |
3103 | | |
3104 | | /* |
3105 | | * Get the RRset containing all private-type records at the zone apex. |
3106 | | */ |
3107 | 0 | dns_rdataset_init(&rdataset); |
3108 | 0 | result = dns_db_findrdataset(db, node, version, zone->privatetype, |
3109 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
3110 | 0 | if (result != ISC_R_SUCCESS) { |
3111 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
3112 | 0 | goto cleanup; |
3113 | 0 | } |
3114 | | |
3115 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
3116 | 0 | unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE]; |
3117 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
3118 | 0 | dns_rdata_t private = DNS_RDATA_INIT; |
3119 | |
|
3120 | 0 | dns_rdataset_current(&rdataset, &private); |
3121 | | /* |
3122 | | * Try extracting NSEC3PARAM RDATA from this private-type |
3123 | | * record. Failure means this private-type record does not |
3124 | | * represent an NSEC3PARAM record, so skip it. |
3125 | | */ |
3126 | 0 | if (!dns_nsec3param_fromprivate(&private, &rdata, buf, |
3127 | 0 | sizeof(buf))) |
3128 | 0 | { |
3129 | 0 | continue; |
3130 | 0 | } |
3131 | 0 | result = dns_rdata_tostruct(&rdata, &nsec3param, NULL); |
3132 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
3133 | 0 | if (((nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0) || |
3134 | 0 | ((nsec3param.flags & DNS_NSEC3FLAG_CREATE) != 0 && nsec3ok)) |
3135 | 0 | { |
3136 | | /* |
3137 | | * Pass the NSEC3PARAM RDATA contained in this |
3138 | | * private-type record to zone_addnsec3chain() so that |
3139 | | * it can kick off adding or removing NSEC3 records. |
3140 | | */ |
3141 | 0 | result = zone_addnsec3chain(zone, &nsec3param); |
3142 | 0 | if (result != ISC_R_SUCCESS) { |
3143 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
3144 | 0 | "zone_addnsec3chain failed: %s", |
3145 | 0 | isc_result_totext(result)); |
3146 | 0 | } |
3147 | 0 | } |
3148 | 0 | } |
3149 | 0 | dns_rdataset_disassociate(&rdataset); |
3150 | |
|
3151 | 0 | cleanup: |
3152 | 0 | if (db != NULL) { |
3153 | 0 | if (node != NULL) { |
3154 | 0 | dns_db_detachnode(&node); |
3155 | 0 | } |
3156 | 0 | if (version != NULL) { |
3157 | 0 | dns_db_closeversion(db, &version, false); |
3158 | 0 | } |
3159 | 0 | dns_db_detach(&db); |
3160 | 0 | } |
3161 | 0 | } |
3162 | | |
3163 | | void |
3164 | 0 | dns__zone_set_resigntime(dns_zone_t *zone) { |
3165 | 0 | dns_fixedname_t fixed; |
3166 | 0 | isc_stdtime_t resign; |
3167 | 0 | isc_result_t result; |
3168 | 0 | uint32_t nanosecs; |
3169 | 0 | dns_db_t *db = NULL; |
3170 | 0 | dns_typepair_t typepair; |
3171 | |
|
3172 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
3173 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
3174 | | |
3175 | | /* We only re-sign zones that can be dynamically updated */ |
3176 | 0 | if (!dns_zone_isdynamic(zone, false)) { |
3177 | 0 | return; |
3178 | 0 | } |
3179 | | |
3180 | 0 | if (dns__zone_inline_raw(zone)) { |
3181 | 0 | return; |
3182 | 0 | } |
3183 | | |
3184 | 0 | dns_fixedname_init(&fixed); |
3185 | |
|
3186 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
3187 | 0 | if (zone->db != NULL) { |
3188 | 0 | dns_db_attach(zone->db, &db); |
3189 | 0 | } |
3190 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
3191 | 0 | if (db == NULL) { |
3192 | 0 | isc_time_settoepoch(&zone->resigntime); |
3193 | 0 | return; |
3194 | 0 | } |
3195 | | |
3196 | 0 | result = dns_db_getsigningtime(db, &resign, dns_fixedname_name(&fixed), |
3197 | 0 | &typepair); |
3198 | 0 | if (result != ISC_R_SUCCESS) { |
3199 | 0 | isc_time_settoepoch(&zone->resigntime); |
3200 | 0 | goto cleanup; |
3201 | 0 | } |
3202 | | |
3203 | 0 | resign -= dns_zone_getsigresigninginterval(zone); |
3204 | 0 | nanosecs = isc_random_uniform(1000000000); |
3205 | 0 | isc_time_set(&zone->resigntime, resign, nanosecs); |
3206 | |
|
3207 | 0 | cleanup: |
3208 | 0 | dns_db_detach(&db); |
3209 | 0 | return; |
3210 | 0 | } |
3211 | | |
3212 | | static isc_result_t |
3213 | 2 | check_nsec3param(dns_zone_t *zone, dns_db_t *db) { |
3214 | 2 | bool ok = false; |
3215 | 2 | dns_dbnode_t *node = NULL; |
3216 | 2 | dns_dbversion_t *version = NULL; |
3217 | 2 | dns_rdata_nsec3param_t nsec3param; |
3218 | 2 | dns_rdataset_t rdataset; |
3219 | 2 | isc_result_t result; |
3220 | 2 | bool dynamic = (zone->type == dns_zone_primary) |
3221 | 2 | ? dns_zone_isdynamic(zone, false) |
3222 | 2 | : false; |
3223 | | |
3224 | 2 | dns_rdataset_init(&rdataset); |
3225 | 2 | result = dns_db_findnode(db, &zone->origin, false, &node); |
3226 | 2 | if (result != ISC_R_SUCCESS) { |
3227 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
3228 | 0 | "nsec3param lookup failure: %s", |
3229 | 0 | isc_result_totext(result)); |
3230 | 0 | return result; |
3231 | 0 | } |
3232 | 2 | dns_db_currentversion(db, &version); |
3233 | | |
3234 | 2 | result = dns_db_findrdataset(db, node, version, |
3235 | 2 | dns_rdatatype_nsec3param, |
3236 | 2 | dns_rdatatype_none, 0, &rdataset, NULL); |
3237 | 2 | if (result == ISC_R_NOTFOUND) { |
3238 | 2 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
3239 | 2 | result = ISC_R_SUCCESS; |
3240 | 2 | goto cleanup; |
3241 | 2 | } |
3242 | 0 | if (result != ISC_R_SUCCESS) { |
3243 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
3244 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
3245 | 0 | "nsec3param lookup failure: %s", |
3246 | 0 | isc_result_totext(result)); |
3247 | 0 | goto cleanup; |
3248 | 0 | } |
3249 | | |
3250 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
3251 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
3252 | |
|
3253 | 0 | dns_rdataset_current(&rdataset, &rdata); |
3254 | 0 | result = dns_rdata_tostruct(&rdata, &nsec3param, NULL); |
3255 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
3256 | | |
3257 | | /* |
3258 | | * For dynamic zones we must support every algorithm so we |
3259 | | * can regenerate all the NSEC3 chains. |
3260 | | * For non-dynamic zones we only need to find a supported |
3261 | | * algorithm. |
3262 | | */ |
3263 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NSEC3TESTZONE) && |
3264 | 0 | nsec3param.hash == DNS_NSEC3_UNKNOWNALG && !dynamic) |
3265 | 0 | { |
3266 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
3267 | 0 | "nsec3 test \"unknown\" hash algorithm " |
3268 | 0 | "found: %u", |
3269 | 0 | nsec3param.hash); |
3270 | 0 | ok = true; |
3271 | 0 | } else if (!dns_nsec3_supportedhash(nsec3param.hash)) { |
3272 | 0 | if (dynamic) { |
3273 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
3274 | 0 | "unsupported nsec3 hash algorithm" |
3275 | 0 | " in dynamic zone: %u", |
3276 | 0 | nsec3param.hash); |
3277 | 0 | result = DNS_R_BADZONE; |
3278 | | /* Stop second error message. */ |
3279 | 0 | ok = true; |
3280 | 0 | break; |
3281 | 0 | } else { |
3282 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
3283 | 0 | "unsupported nsec3 hash " |
3284 | 0 | "algorithm: %u", |
3285 | 0 | nsec3param.hash); |
3286 | 0 | } |
3287 | 0 | } else { |
3288 | 0 | ok = true; |
3289 | 0 | } |
3290 | | |
3291 | | /* |
3292 | | * Warn if the zone has excessive NSEC3 iterations. |
3293 | | */ |
3294 | 0 | if (nsec3param.iterations > dns_nsec3_maxiterations()) { |
3295 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
3296 | 0 | "excessive NSEC3PARAM iterations %u > %u", |
3297 | 0 | nsec3param.iterations, |
3298 | 0 | dns_nsec3_maxiterations()); |
3299 | 0 | } |
3300 | 0 | } |
3301 | |
|
3302 | 0 | if (!ok) { |
3303 | 0 | result = DNS_R_BADZONE; |
3304 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
3305 | 0 | "no supported nsec3 hash algorithm"); |
3306 | 0 | } |
3307 | |
|
3308 | 2 | cleanup: |
3309 | 2 | dns_rdataset_cleanup(&rdataset); |
3310 | 2 | dns_db_closeversion(db, &version, false); |
3311 | 2 | dns_db_detachnode(&node); |
3312 | 2 | return result; |
3313 | 0 | } |
3314 | | |
3315 | | /* |
3316 | | * Set the timer for refreshing the key zone to the soonest future time |
3317 | | * of the set (current timer, keydata->refresh, keydata->addhd, |
3318 | | * keydata->removehd). |
3319 | | */ |
3320 | | static void |
3321 | | set_refreshkeytimer(dns_zone_t *zone, dns_rdata_keydata_t *key, |
3322 | 0 | isc_stdtime_t now, bool force) { |
3323 | 0 | isc_stdtime_t then; |
3324 | 0 | isc_time_t timenow, timethen; |
3325 | 0 | char timebuf[80]; |
3326 | |
|
3327 | 0 | ENTER; |
3328 | 0 | then = key->refresh; |
3329 | 0 | if (force) { |
3330 | 0 | then = now; |
3331 | 0 | } |
3332 | 0 | if (key->addhd > now && key->addhd < then) { |
3333 | 0 | then = key->addhd; |
3334 | 0 | } |
3335 | 0 | if (key->removehd > now && key->removehd < then) { |
3336 | 0 | then = key->removehd; |
3337 | 0 | } |
3338 | |
|
3339 | 0 | timenow = isc_time_now(); |
3340 | 0 | if (then > now) { |
3341 | 0 | DNS_ZONE_TIME_ADD(&timenow, then - now, &timethen); |
3342 | 0 | } else { |
3343 | 0 | timethen = timenow; |
3344 | 0 | } |
3345 | 0 | if (isc_time_compare(&zone->refreshkeytime, &timenow) < 0 || |
3346 | 0 | isc_time_compare(&timethen, &zone->refreshkeytime) < 0) |
3347 | 0 | { |
3348 | 0 | zone->refreshkeytime = timethen; |
3349 | 0 | } |
3350 | |
|
3351 | 0 | isc_time_formattimestamp(&zone->refreshkeytime, timebuf, 80); |
3352 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), "next key refresh: %s", timebuf); |
3353 | 0 | dns__zone_settimer(zone, timenow); |
3354 | 0 | } |
3355 | | |
3356 | | /* |
3357 | | * If keynode references a key or a DS rdataset, and if the key |
3358 | | * zone does not contain a KEYDATA record for the corresponding name, |
3359 | | * then create an empty KEYDATA and push it into the zone as a placeholder, |
3360 | | * then schedule a key refresh immediately. This new KEYDATA record will be |
3361 | | * updated during the refresh. |
3362 | | * |
3363 | | * If the key zone is changed, set '*changed' to true. |
3364 | | */ |
3365 | | static isc_result_t |
3366 | | create_keydata(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, |
3367 | | dns_diff_t *diff, dns_keynode_t *keynode, dns_name_t *keyname, |
3368 | 0 | bool *changed) { |
3369 | 0 | isc_result_t result = ISC_R_SUCCESS; |
3370 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
3371 | 0 | dns_rdata_keydata_t kd; |
3372 | 0 | unsigned char rrdata[4096]; |
3373 | 0 | isc_buffer_t rrdatabuf; |
3374 | 0 | isc_stdtime_t now = isc_stdtime_now(); |
3375 | |
|
3376 | 0 | REQUIRE(keynode != NULL); |
3377 | |
|
3378 | 0 | ENTER; |
3379 | | |
3380 | | /* |
3381 | | * If the keynode has no trust anchor set, we shouldn't be here. |
3382 | | */ |
3383 | 0 | if (!dns_keynode_dsset(keynode, NULL)) { |
3384 | 0 | return ISC_R_FAILURE; |
3385 | 0 | } |
3386 | | |
3387 | 0 | memset(&kd, 0, sizeof(kd)); |
3388 | 0 | kd.common.rdclass = zone->rdclass; |
3389 | 0 | kd.common.rdtype = dns_rdatatype_keydata; |
3390 | |
|
3391 | 0 | isc_buffer_init(&rrdatabuf, rrdata, sizeof(rrdata)); |
3392 | |
|
3393 | 0 | CHECK(dns_rdata_fromstruct(&rdata, zone->rdclass, dns_rdatatype_keydata, |
3394 | 0 | &kd, &rrdatabuf)); |
3395 | | /* Add rdata to zone. */ |
3396 | 0 | CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADD, keyname, 0, &rdata)); |
3397 | 0 | *changed = true; |
3398 | | |
3399 | | /* Refresh new keys from the zone apex as soon as possible. */ |
3400 | 0 | set_refreshkeytimer(zone, &kd, now, true); |
3401 | 0 | return ISC_R_SUCCESS; |
3402 | | |
3403 | 0 | cleanup: |
3404 | 0 | return result; |
3405 | 0 | } |
3406 | | |
3407 | | /* |
3408 | | * Remove from the key zone all the KEYDATA records found in rdataset. |
3409 | | */ |
3410 | | static isc_result_t |
3411 | | delete_keydata(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff, |
3412 | 0 | dns_name_t *name, dns_rdataset_t *rdataset) { |
3413 | 0 | DNS_RDATASET_FOREACH(rdataset) { |
3414 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
3415 | 0 | dns_rdataset_current(rdataset, &rdata); |
3416 | 0 | RETERR(update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name, 0, |
3417 | 0 | &rdata)); |
3418 | 0 | } |
3419 | | |
3420 | 0 | return ISC_R_SUCCESS; |
3421 | 0 | } |
3422 | | |
3423 | | /* |
3424 | | * Compute the DNSSEC key ID for a DNSKEY record. |
3425 | | */ |
3426 | | static isc_result_t |
3427 | | compute_tag(dns_name_t *name, dns_rdata_dnskey_t *dnskey, isc_mem_t *mctx, |
3428 | 0 | dns_keytag_t *tag) { |
3429 | 0 | isc_result_t result; |
3430 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
3431 | 0 | unsigned char data[DNS_RDATA_MAXLENGTH]; |
3432 | 0 | isc_buffer_t buffer; |
3433 | 0 | dst_key_t *dstkey = NULL; |
3434 | |
|
3435 | 0 | isc_buffer_init(&buffer, data, sizeof(data)); |
3436 | |
|
3437 | 0 | CHECK(dns_rdata_fromstruct(&rdata, dnskey->common.rdclass, |
3438 | 0 | dns_rdatatype_dnskey, dnskey, &buffer)); |
3439 | 0 | CHECK(dns_dnssec_keyfromrdata(name, &rdata, mctx, &dstkey)); |
3440 | |
|
3441 | 0 | *tag = dst_key_id(dstkey); |
3442 | 0 | dst_key_free(&dstkey); |
3443 | |
|
3444 | 0 | cleanup: |
3445 | 0 | return result; |
3446 | 0 | } |
3447 | | |
3448 | | /* |
3449 | | * Synth-from-dnssec callbacks to add/delete names from namespace tree. |
3450 | | */ |
3451 | | static void |
3452 | 0 | sfd_add(const dns_name_t *name, void *arg) { |
3453 | 0 | if (arg != NULL) { |
3454 | 0 | dns_view_sfd_add(arg, name); |
3455 | 0 | } |
3456 | 0 | } |
3457 | | |
3458 | | static void |
3459 | 0 | sfd_del(const dns_name_t *name, void *arg) { |
3460 | 0 | if (arg != NULL) { |
3461 | 0 | dns_view_sfd_del(arg, name); |
3462 | 0 | } |
3463 | 0 | } |
3464 | | |
3465 | | /* |
3466 | | * Add key to the security roots. |
3467 | | */ |
3468 | | static void |
3469 | | trust_key(dns_zone_t *zone, dns_name_t *keyname, dns_rdata_dnskey_t *dnskey, |
3470 | 0 | bool initial) { |
3471 | 0 | isc_result_t result; |
3472 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
3473 | 0 | unsigned char data[DNS_RDATA_MAXLENGTH]; |
3474 | 0 | unsigned char digest[DNS_DS_BUFFERSIZE]; |
3475 | 0 | isc_buffer_t buffer; |
3476 | 0 | dns_keytable_t *sr = NULL; |
3477 | 0 | dns_rdata_ds_t ds; |
3478 | |
|
3479 | 0 | CHECK(dns_view_getsecroots(zone->view, &sr)); |
3480 | | |
3481 | | /* Build DS record for key. */ |
3482 | 0 | isc_buffer_init(&buffer, data, sizeof(data)); |
3483 | 0 | CHECK(dns_rdata_fromstruct(&rdata, dnskey->common.rdclass, |
3484 | 0 | dns_rdatatype_dnskey, dnskey, &buffer)); |
3485 | 0 | CHECK(dns_ds_fromkeyrdata(keyname, &rdata, DNS_DSDIGEST_SHA256, digest, |
3486 | 0 | sizeof(digest), &ds)); |
3487 | 0 | CHECK(dns_keytable_add(sr, true, initial, keyname, &ds, sfd_add, |
3488 | 0 | zone->view)); |
3489 | |
|
3490 | 0 | dns_keytable_detach(&sr); |
3491 | |
|
3492 | 0 | cleanup: |
3493 | 0 | if (sr != NULL) { |
3494 | 0 | dns_keytable_detach(&sr); |
3495 | 0 | } |
3496 | 0 | return; |
3497 | 0 | } |
3498 | | |
3499 | | /* |
3500 | | * Add a null key to the security roots for so that all queries |
3501 | | * to the zone will fail. |
3502 | | */ |
3503 | | static void |
3504 | 0 | fail_secure(dns_zone_t *zone, dns_name_t *keyname) { |
3505 | 0 | isc_result_t result; |
3506 | 0 | dns_keytable_t *sr = NULL; |
3507 | |
|
3508 | 0 | result = dns_view_getsecroots(zone->view, &sr); |
3509 | 0 | if (result == ISC_R_SUCCESS) { |
3510 | 0 | dns_keytable_marksecure(sr, keyname); |
3511 | 0 | dns_keytable_detach(&sr); |
3512 | 0 | } |
3513 | 0 | } |
3514 | | |
3515 | | /* |
3516 | | * Scan a set of KEYDATA records from the key zone. The ones that are |
3517 | | * valid (i.e., the add holddown timer has expired) become trusted keys. |
3518 | | */ |
3519 | | static void |
3520 | 0 | load_secroots(dns_zone_t *zone, dns_name_t *name, dns_rdataset_t *rdataset) { |
3521 | 0 | isc_result_t result; |
3522 | 0 | dns_rdata_keydata_t keydata; |
3523 | 0 | dns_rdata_dnskey_t dnskey; |
3524 | 0 | int trusted = 0, revoked = 0, pending = 0; |
3525 | 0 | isc_stdtime_t now = isc_stdtime_now(); |
3526 | 0 | dns_keytable_t *sr = NULL; |
3527 | |
|
3528 | 0 | result = dns_view_getsecroots(zone->view, &sr); |
3529 | 0 | if (result == ISC_R_SUCCESS) { |
3530 | 0 | dns_keytable_delete(sr, name, sfd_del, zone->view); |
3531 | 0 | dns_keytable_detach(&sr); |
3532 | 0 | } |
3533 | | |
3534 | | /* Now insert all the accepted trust anchors from this keydata set. */ |
3535 | 0 | DNS_RDATASET_FOREACH(rdataset) { |
3536 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
3537 | 0 | dns_rdataset_current(rdataset, &rdata); |
3538 | | |
3539 | | /* Convert rdata to keydata. */ |
3540 | 0 | result = dns_rdata_tostruct(&rdata, &keydata, NULL); |
3541 | 0 | if (result == ISC_R_NOTIMPLEMENTED) { |
3542 | 0 | continue; |
3543 | 0 | } |
3544 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
3545 | | |
3546 | | /* Set the key refresh timer to force a fast refresh. */ |
3547 | 0 | set_refreshkeytimer(zone, &keydata, now, true); |
3548 | | |
3549 | | /* If the removal timer is nonzero, this key was revoked. */ |
3550 | 0 | if (keydata.removehd != 0) { |
3551 | 0 | revoked++; |
3552 | 0 | continue; |
3553 | 0 | } |
3554 | | |
3555 | | /* |
3556 | | * If the add timer is still pending, this key is not |
3557 | | * trusted yet. |
3558 | | */ |
3559 | 0 | if (now < keydata.addhd) { |
3560 | 0 | pending++; |
3561 | 0 | continue; |
3562 | 0 | } |
3563 | | |
3564 | | /* Convert keydata to dnskey. */ |
3565 | 0 | dns_keydata_todnskey(&keydata, &dnskey, NULL); |
3566 | | |
3567 | | /* Add to keytables. */ |
3568 | 0 | trusted++; |
3569 | 0 | trust_key(zone, name, &dnskey, keydata.addhd == 0); |
3570 | 0 | } |
3571 | |
|
3572 | 0 | if (trusted == 0 && pending != 0) { |
3573 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
3574 | 0 | dns_name_format(name, namebuf, sizeof namebuf); |
3575 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
3576 | 0 | "No valid trust anchors for '%s'!", namebuf); |
3577 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
3578 | 0 | "%d key(s) revoked, %d still pending", revoked, |
3579 | 0 | pending); |
3580 | 0 | dnssec_log(zone, ISC_LOG_ERROR, "All queries to '%s' will fail", |
3581 | 0 | namebuf); |
3582 | 0 | fail_secure(zone, name); |
3583 | 0 | } |
3584 | 0 | } |
3585 | | |
3586 | | static isc_result_t |
3587 | | do_one_tuple(dns_difftuple_t **tuple, dns_db_t *db, dns_dbversion_t *ver, |
3588 | 0 | dns_diff_t *diff) { |
3589 | 0 | dns_diff_t temp_diff; |
3590 | 0 | isc_result_t result; |
3591 | | |
3592 | | /* |
3593 | | * Create a singleton diff. |
3594 | | */ |
3595 | 0 | dns_diff_init(diff->mctx, &temp_diff); |
3596 | 0 | ISC_LIST_APPEND(temp_diff.tuples, *tuple, link); |
3597 | | |
3598 | | /* |
3599 | | * Apply it to the database. |
3600 | | */ |
3601 | 0 | result = dns_diff_apply(&temp_diff, db, ver); |
3602 | 0 | ISC_LIST_UNLINK(temp_diff.tuples, *tuple, link); |
3603 | 0 | if (result != ISC_R_SUCCESS) { |
3604 | 0 | dns_difftuple_free(tuple); |
3605 | 0 | return result; |
3606 | 0 | } |
3607 | | |
3608 | | /* |
3609 | | * Merge it into the current pending journal entry. |
3610 | | */ |
3611 | 0 | dns_diff_appendminimal(diff, tuple); |
3612 | | |
3613 | | /* |
3614 | | * Do not clear temp_diff. |
3615 | | */ |
3616 | 0 | return ISC_R_SUCCESS; |
3617 | 0 | } |
3618 | | |
3619 | | static isc_result_t |
3620 | | update_one_rr(dns_db_t *db, dns_dbversion_t *ver, dns_diff_t *diff, |
3621 | | dns_diffop_t op, dns_name_t *name, dns_ttl_t ttl, |
3622 | 0 | dns_rdata_t *rdata) { |
3623 | 0 | dns_difftuple_t *tuple = NULL; |
3624 | |
|
3625 | 0 | dns_difftuple_create(diff->mctx, op, name, ttl, rdata, &tuple); |
3626 | 0 | return do_one_tuple(&tuple, db, ver, diff); |
3627 | 0 | } |
3628 | | |
3629 | | static isc_result_t |
3630 | | update_soa_serial(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, |
3631 | | dns_diff_t *diff, isc_mem_t *mctx, |
3632 | 0 | dns_updatemethod_t method) { |
3633 | 0 | dns_difftuple_t *deltuple = NULL; |
3634 | 0 | dns_difftuple_t *addtuple = NULL; |
3635 | 0 | uint32_t serial; |
3636 | 0 | isc_result_t result; |
3637 | 0 | dns_updatemethod_t used = dns_updatemethod_none; |
3638 | |
|
3639 | 0 | INSIST(method != dns_updatemethod_none); |
3640 | |
|
3641 | 0 | CHECK(dns_db_createsoatuple(db, ver, mctx, DNS_DIFFOP_DEL, &deltuple)); |
3642 | 0 | dns_difftuple_copy(deltuple, &addtuple); |
3643 | 0 | addtuple->op = DNS_DIFFOP_ADD; |
3644 | |
|
3645 | 0 | serial = dns_soa_getserial(&addtuple->rdata); |
3646 | 0 | serial = dns_update_soaserial(serial, method, &used); |
3647 | 0 | if (method != used) { |
3648 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
3649 | 0 | "update_soa_serial:new serial would be lower than " |
3650 | 0 | "old serial, using increment method instead"); |
3651 | 0 | } |
3652 | 0 | dns_soa_setserial(serial, &addtuple->rdata); |
3653 | 0 | CHECK(do_one_tuple(&deltuple, db, ver, diff)); |
3654 | 0 | CHECK(do_one_tuple(&addtuple, db, ver, diff)); |
3655 | 0 | result = ISC_R_SUCCESS; |
3656 | |
|
3657 | 0 | cleanup: |
3658 | 0 | if (addtuple != NULL) { |
3659 | 0 | dns_difftuple_free(&addtuple); |
3660 | 0 | } |
3661 | 0 | if (deltuple != NULL) { |
3662 | 0 | dns_difftuple_free(&deltuple); |
3663 | 0 | } |
3664 | 0 | return result; |
3665 | 0 | } |
3666 | | |
3667 | | /* |
3668 | | * Write all transactions in 'diff' to the zone journal file. |
3669 | | */ |
3670 | | static isc_result_t |
3671 | | zone_journal(dns_zone_t *zone, dns_diff_t *diff, uint32_t *sourceserial, |
3672 | 0 | const char *caller) { |
3673 | 0 | const char *journalfile; |
3674 | 0 | isc_result_t result = ISC_R_SUCCESS; |
3675 | 0 | dns_journal_t *journal = NULL; |
3676 | 0 | unsigned int mode = DNS_JOURNAL_CREATE | DNS_JOURNAL_WRITE; |
3677 | |
|
3678 | 0 | ENTER; |
3679 | 0 | journalfile = dns_zone_getjournal(zone); |
3680 | 0 | if (journalfile != NULL) { |
3681 | 0 | result = dns_journal_open(zone->mctx, journalfile, mode, |
3682 | 0 | &journal); |
3683 | 0 | if (result != ISC_R_SUCCESS) { |
3684 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
3685 | 0 | "%s:dns_journal_open -> %s", caller, |
3686 | 0 | isc_result_totext(result)); |
3687 | 0 | return result; |
3688 | 0 | } |
3689 | | |
3690 | 0 | if (sourceserial != NULL) { |
3691 | 0 | dns_journal_set_sourceserial(journal, *sourceserial); |
3692 | 0 | } |
3693 | |
|
3694 | 0 | result = dns_journal_write_transaction(journal, diff); |
3695 | 0 | if (result != ISC_R_SUCCESS) { |
3696 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
3697 | 0 | "%s:dns_journal_write_transaction -> %s", |
3698 | 0 | caller, isc_result_totext(result)); |
3699 | 0 | } |
3700 | 0 | dns_journal_destroy(&journal); |
3701 | 0 | } |
3702 | | |
3703 | 0 | return result; |
3704 | 0 | } |
3705 | | |
3706 | | /* |
3707 | | * Create an SOA record for a newly-created zone |
3708 | | */ |
3709 | | static isc_result_t |
3710 | 0 | add_soa(dns_zone_t *zone, dns_db_t *db) { |
3711 | 0 | isc_result_t result; |
3712 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
3713 | 0 | unsigned char buf[DNS_SOA_BUFFERSIZE]; |
3714 | 0 | dns_dbversion_t *ver = NULL; |
3715 | 0 | dns_diff_t diff; |
3716 | |
|
3717 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), "creating SOA"); |
3718 | |
|
3719 | 0 | dns_diff_init(zone->mctx, &diff); |
3720 | 0 | result = dns_db_newversion(db, &ver); |
3721 | 0 | if (result != ISC_R_SUCCESS) { |
3722 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
3723 | 0 | "add_soa:dns_db_newversion -> %s", |
3724 | 0 | isc_result_totext(result)); |
3725 | 0 | goto cleanup; |
3726 | 0 | } |
3727 | | |
3728 | | /* Build SOA record */ |
3729 | 0 | result = dns_soa_buildrdata(&zone->origin, dns_rootname, zone->rdclass, |
3730 | 0 | 0, 0, 0, 0, 0, buf, &rdata); |
3731 | 0 | if (result != ISC_R_SUCCESS) { |
3732 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
3733 | 0 | "add_soa:dns_soa_buildrdata -> %s", |
3734 | 0 | isc_result_totext(result)); |
3735 | 0 | goto cleanup; |
3736 | 0 | } |
3737 | | |
3738 | 0 | result = update_one_rr(db, ver, &diff, DNS_DIFFOP_ADD, &zone->origin, 0, |
3739 | 0 | &rdata); |
3740 | |
|
3741 | 0 | cleanup: |
3742 | 0 | dns_diff_clear(&diff); |
3743 | 0 | if (ver != NULL) { |
3744 | 0 | dns_db_closeversion(db, &ver, result == ISC_R_SUCCESS); |
3745 | 0 | } |
3746 | |
|
3747 | 0 | INSIST(ver == NULL); |
3748 | |
|
3749 | 0 | return result; |
3750 | 0 | } |
3751 | | |
3752 | | struct addifmissing_arg { |
3753 | | dns_db_t *db; |
3754 | | dns_dbversion_t *ver; |
3755 | | dns_diff_t *diff; |
3756 | | dns_zone_t *zone; |
3757 | | bool *changed; |
3758 | | isc_result_t result; |
3759 | | }; |
3760 | | |
3761 | | static void |
3762 | | addifmissing(dns_keytable_t *keytable, dns_keynode_t *keynode, |
3763 | 0 | dns_name_t *keyname, void *arg) { |
3764 | 0 | dns_db_t *db = ((struct addifmissing_arg *)arg)->db; |
3765 | 0 | dns_dbversion_t *ver = ((struct addifmissing_arg *)arg)->ver; |
3766 | 0 | dns_diff_t *diff = ((struct addifmissing_arg *)arg)->diff; |
3767 | 0 | dns_zone_t *zone = ((struct addifmissing_arg *)arg)->zone; |
3768 | 0 | bool *changed = ((struct addifmissing_arg *)arg)->changed; |
3769 | 0 | isc_result_t result; |
3770 | 0 | dns_fixedname_t fname; |
3771 | |
|
3772 | 0 | UNUSED(keytable); |
3773 | |
|
3774 | 0 | if (((struct addifmissing_arg *)arg)->result != ISC_R_SUCCESS) { |
3775 | 0 | return; |
3776 | 0 | } |
3777 | | |
3778 | 0 | if (!dns_keynode_managed(keynode)) { |
3779 | 0 | return; |
3780 | 0 | } |
3781 | | |
3782 | | /* |
3783 | | * If the keynode has no trust anchor set, return. |
3784 | | */ |
3785 | 0 | if (!dns_keynode_dsset(keynode, NULL)) { |
3786 | 0 | return; |
3787 | 0 | } |
3788 | | |
3789 | | /* |
3790 | | * Check whether there's already a KEYDATA entry for this name; |
3791 | | * if so, we don't need to add another. |
3792 | | */ |
3793 | 0 | dns_fixedname_init(&fname); |
3794 | 0 | result = dns_db_find(db, keyname, ver, dns_rdatatype_keydata, |
3795 | 0 | DNS_DBFIND_NOWILD, 0, dns_fixedname_name(&fname), |
3796 | 0 | NULL, NULL); |
3797 | 0 | if (result == ISC_R_SUCCESS) { |
3798 | 0 | return; |
3799 | 0 | } |
3800 | | |
3801 | | /* |
3802 | | * Create the keydata. |
3803 | | */ |
3804 | 0 | result = create_keydata(zone, db, ver, diff, keynode, keyname, changed); |
3805 | 0 | if (result != ISC_R_SUCCESS && result != ISC_R_NOMORE) { |
3806 | 0 | ((struct addifmissing_arg *)arg)->result = result; |
3807 | 0 | } |
3808 | 0 | } |
3809 | | |
3810 | | /* |
3811 | | * Synchronize the set of initializing keys found in trust-anchors {} |
3812 | | * statements with the set of trust anchors found in the managed-keys.bind |
3813 | | * zone. If a domain is no longer named in trust-anchors, delete all keys |
3814 | | * from that domain from the key zone. If a domain is configured as an |
3815 | | * initial-key in trust-anchors, but there are no references to it in the |
3816 | | * key zone, load the key zone with the initializing key(s) for that |
3817 | | * domain and schedule a key refresh. If a domain is configured as |
3818 | | * an initial-ds in trust-anchors, fetch the DNSKEY RRset, load the key |
3819 | | * zone with the matching key, and schedule a key refresh. |
3820 | | */ |
3821 | | static isc_result_t |
3822 | 0 | sync_keyzone(dns_zone_t *zone, dns_db_t *db) { |
3823 | 0 | isc_result_t result = ISC_R_SUCCESS; |
3824 | 0 | bool changed = false; |
3825 | 0 | bool commit = false; |
3826 | 0 | dns_keynode_t *keynode = NULL; |
3827 | 0 | dns_view_t *view = zone->view; |
3828 | 0 | dns_keytable_t *sr = NULL; |
3829 | 0 | dns_dbversion_t *ver = NULL; |
3830 | 0 | dns_diff_t diff; |
3831 | 0 | dns_rriterator_t rrit; |
3832 | 0 | struct addifmissing_arg arg; |
3833 | |
|
3834 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), "synchronizing trusted keys"); |
3835 | |
|
3836 | 0 | dns_diff_init(zone->mctx, &diff); |
3837 | |
|
3838 | 0 | CHECK(dns_view_getsecroots(view, &sr)); |
3839 | |
|
3840 | 0 | result = dns_db_newversion(db, &ver); |
3841 | 0 | if (result != ISC_R_SUCCESS) { |
3842 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
3843 | 0 | "sync_keyzone:dns_db_newversion -> %s", |
3844 | 0 | isc_result_totext(result)); |
3845 | 0 | goto cleanup; |
3846 | 0 | } |
3847 | | |
3848 | | /* |
3849 | | * Walk the zone DB. If we find any keys whose names are no longer |
3850 | | * in trust-anchors, or which have been changed from initial to static, |
3851 | | * (meaning they are permanent and not RFC5011-maintained), delete |
3852 | | * them from the zone. Otherwise call load_secroots(), which |
3853 | | * loads keys into secroots as appropriate. |
3854 | | */ |
3855 | 0 | dns_rriterator_init(&rrit, db, ver, 0); |
3856 | 0 | for (result = dns_rriterator_first(&rrit); result == ISC_R_SUCCESS; |
3857 | 0 | result = dns_rriterator_nextrrset(&rrit)) |
3858 | 0 | { |
3859 | 0 | dns_rdataset_t *rdataset = NULL; |
3860 | 0 | dns_rdata_keydata_t keydata; |
3861 | 0 | isc_stdtime_t now = isc_stdtime_now(); |
3862 | 0 | bool load = true; |
3863 | 0 | dns_name_t *rrname = NULL; |
3864 | 0 | uint32_t ttl; |
3865 | |
|
3866 | 0 | dns_rriterator_current(&rrit, &rrname, &ttl, &rdataset, NULL); |
3867 | 0 | if (!dns_rdataset_isassociated(rdataset)) { |
3868 | 0 | dns_rriterator_destroy(&rrit); |
3869 | 0 | goto cleanup; |
3870 | 0 | } |
3871 | | |
3872 | 0 | if (rdataset->type != dns_rdatatype_keydata) { |
3873 | 0 | continue; |
3874 | 0 | } |
3875 | | |
3876 | | /* |
3877 | | * The managed-keys zone can contain a placeholder instead of |
3878 | | * legitimate data, in which case we will not use it, and we |
3879 | | * will try to refresh it. |
3880 | | */ |
3881 | 0 | DNS_RDATASET_FOREACH(rdataset) { |
3882 | 0 | isc_result_t iresult; |
3883 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
3884 | |
|
3885 | 0 | dns_rdataset_current(rdataset, &rdata); |
3886 | |
|
3887 | 0 | iresult = dns_rdata_tostruct(&rdata, &keydata, NULL); |
3888 | | /* Do we have a valid placeholder KEYDATA record? */ |
3889 | 0 | if (iresult == ISC_R_SUCCESS && keydata.flags == 0 && |
3890 | 0 | keydata.protocol == 0 && keydata.algorithm == 0) |
3891 | 0 | { |
3892 | 0 | set_refreshkeytimer(zone, &keydata, now, true); |
3893 | 0 | load = false; |
3894 | 0 | } |
3895 | 0 | } |
3896 | | |
3897 | | /* |
3898 | | * Release db wrlock to prevent LOR reports against |
3899 | | * dns_keytable_forall() call below. |
3900 | | */ |
3901 | 0 | dns_rriterator_pause(&rrit); |
3902 | 0 | result = dns_keytable_find(sr, rrname, &keynode); |
3903 | 0 | if (result != ISC_R_SUCCESS || !dns_keynode_managed(keynode)) { |
3904 | 0 | CHECK(delete_keydata(db, ver, &diff, rrname, rdataset)); |
3905 | 0 | changed = true; |
3906 | 0 | } else if (load) { |
3907 | 0 | load_secroots(zone, rrname, rdataset); |
3908 | 0 | } |
3909 | | |
3910 | 0 | if (keynode != NULL) { |
3911 | 0 | dns_keynode_detach(&keynode); |
3912 | 0 | } |
3913 | 0 | } |
3914 | 0 | dns_rriterator_destroy(&rrit); |
3915 | | |
3916 | | /* |
3917 | | * Walk secroots to find any initial keys that aren't in |
3918 | | * the zone. If we find any, add them to the zone directly. |
3919 | | * If any DS-style initial keys are found, refresh the key |
3920 | | * zone so that they'll be looked up. |
3921 | | */ |
3922 | 0 | arg.db = db; |
3923 | 0 | arg.ver = ver; |
3924 | 0 | arg.result = ISC_R_SUCCESS; |
3925 | 0 | arg.diff = &diff; |
3926 | 0 | arg.zone = zone; |
3927 | 0 | arg.changed = &changed; |
3928 | 0 | dns_keytable_forall(sr, addifmissing, &arg); |
3929 | 0 | result = arg.result; |
3930 | 0 | if (changed) { |
3931 | | /* Write changes to journal file. */ |
3932 | 0 | CHECK(update_soa_serial(zone, db, ver, &diff, zone->mctx, |
3933 | 0 | zone->updatemethod)); |
3934 | 0 | CHECK(zone_journal(zone, &diff, NULL, "sync_keyzone")); |
3935 | |
|
3936 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED); |
3937 | 0 | zone_needdump(zone, 30); |
3938 | 0 | commit = true; |
3939 | 0 | } |
3940 | | |
3941 | 0 | cleanup: |
3942 | 0 | if (result != ISC_R_SUCCESS) { |
3943 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
3944 | 0 | "unable to synchronize managed keys: %s", |
3945 | 0 | isc_result_totext(result)); |
3946 | 0 | isc_time_settoepoch(&zone->refreshkeytime); |
3947 | 0 | } |
3948 | 0 | if (keynode != NULL) { |
3949 | 0 | dns_keynode_detach(&keynode); |
3950 | 0 | } |
3951 | 0 | if (sr != NULL) { |
3952 | 0 | dns_keytable_detach(&sr); |
3953 | 0 | } |
3954 | 0 | if (ver != NULL) { |
3955 | 0 | dns_db_closeversion(db, &ver, commit); |
3956 | 0 | } |
3957 | 0 | dns_diff_clear(&diff); |
3958 | |
|
3959 | 0 | INSIST(ver == NULL); |
3960 | |
|
3961 | 0 | return result; |
3962 | 0 | } |
3963 | | |
3964 | | isc_result_t |
3965 | 0 | dns_zone_synckeyzone(dns_zone_t *zone) { |
3966 | 0 | isc_result_t result; |
3967 | 0 | dns_db_t *db = NULL; |
3968 | |
|
3969 | 0 | if (zone->type != dns_zone_key) { |
3970 | 0 | return DNS_R_BADZONE; |
3971 | 0 | } |
3972 | | |
3973 | 0 | CHECK(dns_zone_getdb(zone, &db)); |
3974 | |
|
3975 | 0 | LOCK_ZONE(zone); |
3976 | 0 | result = sync_keyzone(zone, db); |
3977 | 0 | UNLOCK_ZONE(zone); |
3978 | |
|
3979 | 0 | cleanup: |
3980 | 0 | if (db != NULL) { |
3981 | 0 | dns_db_detach(&db); |
3982 | 0 | } |
3983 | 0 | return result; |
3984 | 0 | } |
3985 | | |
3986 | | static bool |
3987 | 0 | zone_unchanged(dns_db_t *db1, dns_db_t *db2, isc_mem_t *mctx) { |
3988 | 0 | isc_result_t result; |
3989 | 0 | bool answer = false; |
3990 | 0 | dns_diff_t diff; |
3991 | |
|
3992 | 0 | dns_diff_init(mctx, &diff); |
3993 | 0 | result = dns_db_diffx(&diff, db1, NULL, db2, NULL, NULL); |
3994 | 0 | if (result == ISC_R_SUCCESS && ISC_LIST_EMPTY(diff.tuples)) { |
3995 | 0 | answer = true; |
3996 | 0 | } |
3997 | 0 | dns_diff_clear(&diff); |
3998 | 0 | return answer; |
3999 | 0 | } |
4000 | | |
4001 | | /* |
4002 | | * Compare times treating epoch as "unset". |
4003 | | */ |
4004 | | static inline bool |
4005 | 0 | time_greater_equal(isc_time_t a, isc_time_t b) { |
4006 | 0 | return !isc_time_isepoch(&b) && isc_time_compare(&a, &b) >= 0; |
4007 | 0 | } |
4008 | | |
4009 | | static inline isc_time_t |
4010 | 0 | time_min(isc_time_t a, isc_time_t b) { |
4011 | 0 | if (isc_time_isepoch(&b)) { |
4012 | 0 | return a; |
4013 | 0 | } |
4014 | 0 | return isc_time_isepoch(&a) || isc_time_compare(&b, &a) < 0 ? b : a; |
4015 | 0 | } |
4016 | | |
4017 | | static bool |
4018 | 0 | zone_maintenance_request_pending(dns_zone_t *zone) { |
4019 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
4020 | |
|
4021 | 0 | return zone->iss == NULL && DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) && |
4022 | 0 | !ISC_LIST_EMPTY(zone->maintenance_queue); |
4023 | 0 | } |
4024 | | |
4025 | | static unsigned char er_ndata[] = "\001*\003_er"; |
4026 | | static dns_name_t er = DNS_NAME_INITNONABSOLUTE(er_ndata); |
4027 | | |
4028 | | static isc_result_t |
4029 | 2 | check_reportchannel(dns_zone_t *zone, dns_db_t *db) { |
4030 | 2 | isc_result_t result; |
4031 | 2 | dns_rdataset_t rdataset = DNS_RDATASET_INIT; |
4032 | 2 | dns_dbnode_t *node = NULL; |
4033 | 2 | dns_dbversion_t *version = NULL; |
4034 | 2 | dns_fixedname_t fixed; |
4035 | 2 | dns_name_t *name = NULL; |
4036 | | |
4037 | | /* |
4038 | | * If this zone isn't logging reports, it's fine. |
4039 | | */ |
4040 | 2 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_LOGREPORTS)) { |
4041 | 2 | return ISC_R_SUCCESS; |
4042 | 2 | } |
4043 | | |
4044 | | /* |
4045 | | * Otherwise, we need a '*._er' wildcard with a TXT rdataset. |
4046 | | */ |
4047 | 0 | name = dns_fixedname_initname(&fixed); |
4048 | 0 | CHECK(dns_name_concatenate(&er, &zone->origin, name)); |
4049 | 0 | CHECK(dns_db_findnode(db, name, false, &node)); |
4050 | |
|
4051 | 0 | dns_db_currentversion(db, &version); |
4052 | |
|
4053 | 0 | result = dns_db_findrdataset(db, node, version, dns_rdatatype_txt, |
4054 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
4055 | 0 | dns_db_closeversion(db, &version, false); |
4056 | 0 | dns_db_detachnode(&node); |
4057 | 0 | if (result == ISC_R_SUCCESS) { |
4058 | 0 | dns_rdataset_disassociate(&rdataset); |
4059 | 0 | } |
4060 | |
|
4061 | 0 | cleanup: |
4062 | 0 | return result; |
4063 | 0 | } |
4064 | | |
4065 | | /* |
4066 | | * The zone is presumed to be locked. |
4067 | | * If this is a inline_raw zone the secure version is also locked. |
4068 | | */ |
4069 | | static isc_result_t |
4070 | | zone_postload(dns_zone_t *zone, dns_db_t *db, isc_time_t loadtime, |
4071 | 2 | isc_result_t result) { |
4072 | 2 | unsigned int soacount = 0; |
4073 | 2 | unsigned int nscount = 0; |
4074 | 2 | unsigned int errors = 0; |
4075 | 2 | uint32_t serial, oldserial, refresh, retry, expire, minimum, soattl; |
4076 | 2 | isc_time_t now; |
4077 | 2 | bool needdump = false; |
4078 | 2 | bool fixjournal = false; |
4079 | 2 | bool hasinclude = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HASINCLUDE); |
4080 | 2 | bool noprimary = false; |
4081 | 2 | bool had_db = false; |
4082 | 2 | bool is_dynamic = false; |
4083 | | |
4084 | 2 | INSIST(LOCKED_ZONE(zone)); |
4085 | 2 | if (dns__zone_inline_raw(zone)) { |
4086 | 0 | INSIST(LOCKED_ZONE(zone->secure)); |
4087 | 0 | } |
4088 | | |
4089 | 2 | now = isc_time_now(); |
4090 | | |
4091 | | /* |
4092 | | * Initiate zone transfer? We may need a error code that |
4093 | | * indicates that the "permanent" form does not exist. |
4094 | | * XXX better error feedback to log. |
4095 | | */ |
4096 | 2 | if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) { |
4097 | 0 | if (zone->type == dns_zone_secondary || |
4098 | 0 | zone->type == dns_zone_mirror || |
4099 | 0 | zone->type == dns_zone_stub || |
4100 | 0 | (zone->type == dns_zone_redirect && |
4101 | 0 | dns_remote_addresses(&zone->primaries) == NULL)) |
4102 | 0 | { |
4103 | 0 | if (result == ISC_R_FILENOTFOUND) { |
4104 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4105 | 0 | ISC_LOG_DEBUG(1), |
4106 | 0 | "no master file"); |
4107 | 0 | } else if (result != DNS_R_NOMASTERFILE) { |
4108 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4109 | 0 | ISC_LOG_ERROR, |
4110 | 0 | "loading from master file %s " |
4111 | 0 | "failed: %s", |
4112 | 0 | zone->masterfile, |
4113 | 0 | isc_result_totext(result)); |
4114 | 0 | } |
4115 | 0 | } else if (zone->type == dns_zone_primary && |
4116 | 0 | dns__zone_inline_secure(zone) && |
4117 | 0 | result == ISC_R_FILENOTFOUND) |
4118 | 0 | { |
4119 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4120 | 0 | ISC_LOG_DEBUG(1), |
4121 | 0 | "no master file; secure db will be " |
4122 | 0 | "bootstrapped from raw zone"); |
4123 | 0 | } else { |
4124 | 0 | int level = ISC_LOG_ERROR; |
4125 | 0 | if (zone->type == dns_zone_key && |
4126 | 0 | result == ISC_R_FILENOTFOUND) |
4127 | 0 | { |
4128 | 0 | level = ISC_LOG_DEBUG(1); |
4129 | 0 | } |
4130 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, level, |
4131 | 0 | "loading from master file %s failed: %s", |
4132 | 0 | zone->masterfile, |
4133 | 0 | isc_result_totext(result)); |
4134 | 0 | noprimary = true; |
4135 | 0 | } |
4136 | |
|
4137 | 0 | if (zone->type != dns_zone_key) { |
4138 | 0 | goto cleanup; |
4139 | 0 | } |
4140 | 0 | } |
4141 | | |
4142 | 2 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(2), |
4143 | 2 | "number of nodes in database: %u", dns_db_nodecount(db)); |
4144 | | |
4145 | 2 | if (result == DNS_R_SEENINCLUDE) { |
4146 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HASINCLUDE); |
4147 | 2 | } else { |
4148 | 2 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HASINCLUDE); |
4149 | 2 | } |
4150 | | |
4151 | | /* |
4152 | | * If there's no master file for a key zone, then the zone is new: |
4153 | | * create an SOA record. (We do this now, instead of later, so that |
4154 | | * if there happens to be a journal file, we can roll forward from |
4155 | | * a sane starting point.) |
4156 | | */ |
4157 | 2 | if (noprimary && zone->type == dns_zone_key) { |
4158 | 0 | CHECK(add_soa(zone, db)); |
4159 | 0 | } |
4160 | | |
4161 | | /* |
4162 | | * Apply update log, if any, on initial load. |
4163 | | */ |
4164 | 2 | if (zone->journal != NULL && |
4165 | 2 | !DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NOMERGE) && |
4166 | 2 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) |
4167 | 2 | { |
4168 | 2 | CHECK(zone_journal_rollforward(zone, db, &needdump, |
4169 | 2 | &fixjournal)); |
4170 | 2 | } |
4171 | | |
4172 | | /* |
4173 | | * Obtain ns, soa and cname counts for top of zone. |
4174 | | */ |
4175 | 2 | INSIST(db != NULL); |
4176 | 2 | result = zone_get_from_db(zone, db, &nscount, &soacount, &soattl, |
4177 | 2 | &serial, &refresh, &retry, &expire, &minimum, |
4178 | 2 | &errors); |
4179 | 2 | if (result != ISC_R_SUCCESS && zone->type != dns_zone_key) { |
4180 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR, |
4181 | 0 | "could not find NS and/or SOA records"); |
4182 | 0 | } |
4183 | | |
4184 | 2 | is_dynamic = dns_zone_isdynamic(zone, true); |
4185 | | |
4186 | | /* |
4187 | | * Check to make sure the journal is up to date, and remove the |
4188 | | * journal file if it isn't, as we wouldn't be able to apply |
4189 | | * updates otherwise. |
4190 | | */ |
4191 | 2 | if (zone->journal != NULL && is_dynamic && |
4192 | 0 | !DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IXFRFROMDIFFS)) |
4193 | 0 | { |
4194 | 0 | uint32_t jserial; |
4195 | 0 | dns_journal_t *journal = NULL; |
4196 | 0 | bool empty = false; |
4197 | |
|
4198 | 0 | result = dns_journal_open(zone->mctx, zone->journal, |
4199 | 0 | DNS_JOURNAL_READ, &journal); |
4200 | 0 | if (result == ISC_R_SUCCESS) { |
4201 | 0 | jserial = dns_journal_last_serial(journal); |
4202 | 0 | empty = dns_journal_empty(journal); |
4203 | 0 | dns_journal_destroy(&journal); |
4204 | 0 | } else { |
4205 | 0 | jserial = serial; |
4206 | 0 | result = ISC_R_SUCCESS; |
4207 | 0 | } |
4208 | |
|
4209 | 0 | if (jserial != serial) { |
4210 | 0 | if (!empty) { |
4211 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4212 | 0 | ISC_LOG_INFO, |
4213 | 0 | "journal file is out of date: " |
4214 | 0 | "removing journal file"); |
4215 | 0 | } |
4216 | 0 | if (remove(zone->journal) < 0 && errno != ENOENT) { |
4217 | 0 | char strbuf[ISC_STRERRORSIZE]; |
4218 | 0 | strerror_r(errno, strbuf, sizeof(strbuf)); |
4219 | 0 | isc_log_write(DNS_LOGCATEGORY_GENERAL, |
4220 | 0 | DNS_LOGMODULE_ZONE, |
4221 | 0 | ISC_LOG_WARNING, |
4222 | 0 | "unable to remove journal " |
4223 | 0 | "'%s': '%s'", |
4224 | 0 | zone->journal, strbuf); |
4225 | 0 | } |
4226 | 0 | } |
4227 | 0 | } |
4228 | | |
4229 | 2 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(1), |
4230 | 2 | "loaded; checking validity"); |
4231 | | |
4232 | | /* |
4233 | | * Primary / Secondary / Mirror / Stub zones require both NS and SOA |
4234 | | * records at the top of the zone. |
4235 | | */ |
4236 | | |
4237 | 2 | switch (zone->type) { |
4238 | 0 | case dns_zone_dlz: |
4239 | 2 | case dns_zone_primary: |
4240 | 2 | case dns_zone_secondary: |
4241 | 2 | case dns_zone_mirror: |
4242 | 2 | case dns_zone_stub: |
4243 | 2 | case dns_zone_redirect: |
4244 | 2 | if (soacount != 1) { |
4245 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4246 | 0 | ISC_LOG_ERROR, "has %d SOA records", |
4247 | 0 | soacount); |
4248 | 0 | result = DNS_R_BADZONE; |
4249 | 0 | } |
4250 | 2 | if (nscount == 0) { |
4251 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4252 | 0 | ISC_LOG_ERROR, "has no NS records"); |
4253 | 0 | result = DNS_R_BADZONE; |
4254 | 0 | } |
4255 | 2 | if (result != ISC_R_SUCCESS) { |
4256 | 0 | goto cleanup; |
4257 | 0 | } |
4258 | 2 | if (zone->type == dns_zone_primary && errors != 0) { |
4259 | 0 | CLEANUP(DNS_R_BADZONE); |
4260 | 0 | } |
4261 | 2 | if (zone->type != dns_zone_stub && |
4262 | 2 | zone->type != dns_zone_redirect) |
4263 | 2 | { |
4264 | 2 | CHECK(check_nsec3param(zone, db)); |
4265 | 2 | } |
4266 | 2 | if (zone->type == dns_zone_primary && |
4267 | 2 | DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKINTEGRITY) && |
4268 | 0 | !integrity_checks(zone, db)) |
4269 | 0 | { |
4270 | 0 | CLEANUP(DNS_R_BADZONE); |
4271 | 0 | } |
4272 | 2 | if (zone->type == dns_zone_primary && |
4273 | 2 | DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKDUPRR) && |
4274 | 0 | !zone_check_dup(zone, db)) |
4275 | 0 | { |
4276 | 0 | CLEANUP(DNS_R_BADZONE); |
4277 | 0 | } |
4278 | | |
4279 | 2 | if (zone->type == dns_zone_primary) { |
4280 | 2 | result = dns_zone_cdscheck(zone, db, NULL); |
4281 | 2 | if (result != ISC_R_SUCCESS) { |
4282 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
4283 | 0 | "CDS/CDNSKEY consistency checks " |
4284 | 0 | "failed"); |
4285 | 0 | goto cleanup; |
4286 | 0 | } |
4287 | 2 | } |
4288 | | |
4289 | 2 | result = check_reportchannel(zone, db); |
4290 | 2 | if (result != ISC_R_SUCCESS) { |
4291 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
4292 | 0 | "'log-report-channel' is set, but no " |
4293 | 0 | "'*._er/TXT' wildcard found"); |
4294 | 0 | CLEANUP(DNS_R_BADZONE); |
4295 | 0 | } |
4296 | | |
4297 | 2 | CHECK(dns_zone_verifydb(zone, db, NULL)); |
4298 | | |
4299 | 2 | if (zone->db != NULL) { |
4300 | 0 | unsigned int oldsoacount; |
4301 | | |
4302 | | /* |
4303 | | * This is checked in zone_replacedb() for |
4304 | | * secondary zones as they don't reload from disk. |
4305 | | */ |
4306 | 0 | result = zone_get_from_db( |
4307 | 0 | zone, zone->db, NULL, &oldsoacount, NULL, |
4308 | 0 | &oldserial, NULL, NULL, NULL, NULL, NULL); |
4309 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
4310 | 0 | RUNTIME_CHECK(oldsoacount > 0U); |
4311 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IXFRFROMDIFFS) && |
4312 | 0 | !isc_serial_gt(serial, oldserial)) |
4313 | 0 | { |
4314 | 0 | uint32_t serialmin, serialmax; |
4315 | |
|
4316 | 0 | INSIST(zone->type == dns_zone_primary); |
4317 | 0 | INSIST(zone->raw == NULL); |
4318 | |
|
4319 | 0 | if (serial == oldserial && |
4320 | 0 | zone_unchanged(zone->db, db, zone->mctx)) |
4321 | 0 | { |
4322 | 0 | dns_zone_logc(zone, |
4323 | 0 | DNS_LOGCATEGORY_ZONELOAD, |
4324 | 0 | ISC_LOG_INFO, |
4325 | 0 | "ixfr-from-differences: " |
4326 | 0 | "unchanged"); |
4327 | 0 | zone->loadtime = loadtime; |
4328 | 0 | goto done; |
4329 | 0 | } |
4330 | | |
4331 | 0 | serialmin = (oldserial + 1) & 0xffffffffU; |
4332 | 0 | serialmax = (oldserial + 0x7fffffffU) & |
4333 | 0 | 0xffffffffU; |
4334 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4335 | 0 | ISC_LOG_ERROR, |
4336 | 0 | "ixfr-from-differences: " |
4337 | 0 | "new serial (%u) out of range " |
4338 | 0 | "[%u - %u]", |
4339 | 0 | serial, serialmin, serialmax); |
4340 | 0 | CLEANUP(DNS_R_BADZONE); |
4341 | 0 | } else if (!isc_serial_ge(serial, oldserial)) { |
4342 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4343 | 0 | ISC_LOG_ERROR, |
4344 | 0 | "zone serial (%u/%u) has gone " |
4345 | 0 | "backwards", |
4346 | 0 | serial, oldserial); |
4347 | 0 | } else if (serial == oldserial && !hasinclude && |
4348 | 0 | strcmp(zone->db_argv[0], "_builtin") != 0) |
4349 | 0 | { |
4350 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4351 | 0 | ISC_LOG_ERROR, |
4352 | 0 | "zone serial (%u) unchanged. " |
4353 | 0 | "zone may fail to transfer " |
4354 | 0 | "to secondaries.", |
4355 | 0 | serial); |
4356 | 0 | } |
4357 | 0 | } |
4358 | | |
4359 | 2 | if (zone->type == dns_zone_primary && |
4360 | 2 | (zone->update_acl != NULL || zone->ssutable != NULL) && |
4361 | 0 | dns_zone_getsigresigninginterval(zone) < (3 * refresh) && |
4362 | 0 | dns_db_issecure(db)) |
4363 | 0 | { |
4364 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4365 | 0 | ISC_LOG_WARNING, |
4366 | 0 | "sig-re-signing-interval less than " |
4367 | 0 | "3 * refresh."); |
4368 | 0 | } |
4369 | | |
4370 | 2 | zone->refresh = RANGE(refresh, zone->minrefresh, |
4371 | 2 | zone->maxrefresh); |
4372 | 2 | zone->retry = RANGE(retry, zone->minretry, zone->maxretry); |
4373 | 2 | zone->expire = RANGE(expire, zone->refresh + zone->retry, |
4374 | 2 | DNS_MAX_EXPIRE); |
4375 | 2 | zone->soattl = soattl; |
4376 | 2 | zone->minimum = minimum; |
4377 | 2 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HAVETIMERS); |
4378 | | |
4379 | 2 | if (zone->type == dns_zone_secondary || |
4380 | 2 | zone->type == dns_zone_mirror || |
4381 | 2 | zone->type == dns_zone_stub || |
4382 | 2 | (zone->type == dns_zone_redirect && |
4383 | 0 | dns_remote_addresses(&zone->primaries) != NULL)) |
4384 | 0 | { |
4385 | 0 | isc_time_t t; |
4386 | 0 | uint32_t delay; |
4387 | |
|
4388 | 0 | result = isc_file_getmodtime(zone->journal, &t); |
4389 | 0 | if (result != ISC_R_SUCCESS) { |
4390 | 0 | result = isc_file_getmodtime(zone->masterfile, |
4391 | 0 | &t); |
4392 | 0 | } |
4393 | 0 | if (result == ISC_R_SUCCESS) { |
4394 | 0 | DNS_ZONE_TIME_ADD(&t, zone->expire, |
4395 | 0 | &zone->expiretime); |
4396 | 0 | } else { |
4397 | 0 | DNS_ZONE_TIME_ADD(&now, zone->retry, |
4398 | 0 | &zone->expiretime); |
4399 | 0 | } |
4400 | |
|
4401 | 0 | delay = (zone->retry - |
4402 | 0 | isc_random_uniform((zone->retry * 3) / 4)); |
4403 | 0 | DNS_ZONE_TIME_ADD(&now, delay, &zone->refreshtime); |
4404 | 0 | if (isc_time_compare(&zone->refreshtime, |
4405 | 0 | &zone->expiretime) >= 0) |
4406 | 0 | { |
4407 | 0 | DNS_ZONE_SETFLAG(zone, |
4408 | 0 | DNS_ZONEFLG_FIRSTREFRESH); |
4409 | 0 | zone->refreshtime = now; |
4410 | 0 | } else { |
4411 | | /* The zone is up to date. */ |
4412 | 0 | DNS_ZONE_CLRFLAG(zone, |
4413 | 0 | DNS_ZONEFLG_FIRSTREFRESH); |
4414 | 0 | } |
4415 | 0 | } |
4416 | | |
4417 | 2 | break; |
4418 | | |
4419 | 0 | case dns_zone_key: |
4420 | | /* Nothing needs to be done now */ |
4421 | 0 | break; |
4422 | | |
4423 | 0 | default: |
4424 | 0 | UNEXPECTED_ERROR("unexpected zone type %d", zone->type); |
4425 | 0 | CLEANUP(ISC_R_UNEXPECTED); |
4426 | 2 | } |
4427 | | |
4428 | | /* |
4429 | | * Check for weak DNSKEY's. |
4430 | | */ |
4431 | 2 | if (zone->type == dns_zone_primary) { |
4432 | 2 | zone_check_dnskeys(zone, db); |
4433 | 2 | } |
4434 | | |
4435 | | /* |
4436 | | * Schedule DNSSEC key refresh. |
4437 | | */ |
4438 | 2 | if (zone->type == dns_zone_primary && zone->kasp != NULL) { |
4439 | 0 | zone->refreshkeytime = now; |
4440 | 0 | } |
4441 | | |
4442 | 2 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write); |
4443 | 2 | if (zone->db != NULL) { |
4444 | 0 | had_db = true; |
4445 | 0 | result = zone_replacedb(zone, db, false); |
4446 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write); |
4447 | 0 | if (result != ISC_R_SUCCESS) { |
4448 | 0 | goto cleanup; |
4449 | 0 | } |
4450 | 2 | } else { |
4451 | 2 | zone_attachdb(zone, db); |
4452 | 2 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write); |
4453 | 2 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED | |
4454 | 2 | DNS_ZONEFLG_NEEDSTARTUPNOTIFY); |
4455 | 2 | if (dns__zone_inline_raw(zone)) { |
4456 | 0 | zone_schedule_inline_sync(zone->secure, |
4457 | 0 | inline_sync_incremental); |
4458 | 0 | } |
4459 | 2 | } |
4460 | | |
4461 | 2 | result = ISC_R_SUCCESS; |
4462 | | |
4463 | 2 | if (fixjournal) { |
4464 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FIXJOURNAL); |
4465 | 0 | zone_journal_compact(zone, zone->db, 0); |
4466 | 0 | } |
4467 | 2 | if (needdump) { |
4468 | 0 | if (zone->type == dns_zone_key) { |
4469 | 0 | zone_needdump(zone, 30); |
4470 | 0 | } else { |
4471 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
4472 | 0 | } |
4473 | 0 | } |
4474 | | |
4475 | 2 | if (zone->loop != NULL) { |
4476 | 0 | if (zone->type == dns_zone_primary) { |
4477 | 0 | dns__zone_set_resigntime(zone); |
4478 | 0 | resume_signingwithkey(zone); |
4479 | 0 | resume_addnsec3chain(zone); |
4480 | 0 | } |
4481 | |
|
4482 | 0 | is_dynamic = dns_zone_isdynamic(zone, false); |
4483 | 0 | if (zone->type == dns_zone_primary && is_dynamic && |
4484 | 0 | dns_db_issecure(db) && !dns__zone_inline_raw(zone)) |
4485 | 0 | { |
4486 | 0 | isc_stdtime_t resign; |
4487 | 0 | dns_name_t *name; |
4488 | 0 | dns_fixedname_t fixed; |
4489 | 0 | dns_typepair_t typepair; |
4490 | |
|
4491 | 0 | name = dns_fixedname_initname(&fixed); |
4492 | |
|
4493 | 0 | result = dns_db_getsigningtime(db, &resign, name, |
4494 | 0 | &typepair); |
4495 | 0 | if (result == ISC_R_SUCCESS) { |
4496 | 0 | isc_stdtime_t timenow = isc_stdtime_now(); |
4497 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
4498 | 0 | char typebuf[DNS_RDATATYPE_FORMATSIZE]; |
4499 | |
|
4500 | 0 | dns_name_format(name, namebuf, sizeof(namebuf)); |
4501 | 0 | dns_rdatatype_format( |
4502 | 0 | DNS_TYPEPAIR_COVERS(typepair), typebuf, |
4503 | 0 | sizeof(typebuf)); |
4504 | 0 | dnssec_log( |
4505 | 0 | zone, ISC_LOG_DEBUG(3), |
4506 | 0 | "next resign: %s/%s " |
4507 | 0 | "in %d seconds", |
4508 | 0 | namebuf, typebuf, |
4509 | 0 | resign - timenow - |
4510 | 0 | dns_zone_getsigresigninginterval( |
4511 | 0 | zone)); |
4512 | 0 | } else { |
4513 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
4514 | 0 | "signed dynamic zone has no " |
4515 | 0 | "resign event scheduled"); |
4516 | 0 | } |
4517 | 0 | } |
4518 | |
|
4519 | 0 | dns__zone_settimer(zone, now); |
4520 | 0 | } |
4521 | | |
4522 | | /* |
4523 | | * Clear old include list. |
4524 | | */ |
4525 | 2 | ISC_LIST_FOREACH(zone->includes, inc, link) { |
4526 | 0 | ISC_LIST_UNLINK(zone->includes, inc, link); |
4527 | 0 | isc_mem_free(zone->mctx, inc->name); |
4528 | 0 | isc_mem_put(zone->mctx, inc, sizeof(*inc)); |
4529 | 0 | } |
4530 | 2 | zone->nincludes = 0; |
4531 | | |
4532 | | /* |
4533 | | * Transfer new include list. |
4534 | | */ |
4535 | 2 | ISC_LIST_FOREACH(zone->newincludes, inc, link) { |
4536 | 0 | ISC_LIST_UNLINK(zone->newincludes, inc, link); |
4537 | 0 | ISC_LIST_APPEND(zone->includes, inc, link); |
4538 | 0 | zone->nincludes++; |
4539 | 0 | } |
4540 | | |
4541 | 2 | if (!dns_db_ispersistent(db)) { |
4542 | 2 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_INFO, |
4543 | 2 | "loaded serial %u%s", serial, |
4544 | 2 | dns_db_issecure(db) ? " (DNSSEC signed)" : ""); |
4545 | 2 | } |
4546 | | |
4547 | 2 | if (!had_db && zone->type == dns_zone_mirror) { |
4548 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_INFO, |
4549 | 0 | "mirror zone is now in use"); |
4550 | 0 | } |
4551 | | |
4552 | 2 | zone->loadtime = loadtime; |
4553 | 2 | goto done; |
4554 | | |
4555 | 0 | cleanup: |
4556 | 0 | if (result != ISC_R_SUCCESS) { |
4557 | 0 | dns_zone_rpz_disable_db(zone, db); |
4558 | 0 | dns_zone_catz_disable_db(zone, db); |
4559 | 0 | } |
4560 | |
|
4561 | 0 | ISC_LIST_FOREACH(zone->newincludes, inc, link) { |
4562 | 0 | ISC_LIST_UNLINK(zone->newincludes, inc, link); |
4563 | 0 | isc_mem_free(zone->mctx, inc->name); |
4564 | 0 | isc_mem_put(zone->mctx, inc, sizeof(*inc)); |
4565 | 0 | } |
4566 | 0 | if (zone->type == dns_zone_secondary || zone->type == dns_zone_mirror || |
4567 | 0 | zone->type == dns_zone_stub || zone->type == dns_zone_key || |
4568 | 0 | (zone->type == dns_zone_redirect && |
4569 | 0 | dns_remote_addresses(&zone->primaries) != NULL)) |
4570 | 0 | { |
4571 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FIRSTREFRESH); |
4572 | |
|
4573 | 0 | if (zone->journal != NULL) { |
4574 | 0 | zone_saveunique(zone, zone->journal, "jn-XXXXXXXX"); |
4575 | 0 | } |
4576 | 0 | if (zone->masterfile != NULL) { |
4577 | 0 | zone_saveunique(zone, zone->masterfile, "db-XXXXXXXX"); |
4578 | 0 | } |
4579 | | |
4580 | | /* Mark the zone for immediate refresh. */ |
4581 | 0 | zone->refreshtime = now; |
4582 | 0 | if (zone->loop != NULL) { |
4583 | 0 | dns__zone_settimer(zone, now); |
4584 | 0 | } |
4585 | 0 | result = ISC_R_SUCCESS; |
4586 | 0 | } else if (zone->type == dns_zone_primary || |
4587 | 0 | zone->type == dns_zone_redirect) |
4588 | 0 | { |
4589 | 0 | if (!(dns__zone_inline_secure(zone) && |
4590 | 0 | result == ISC_R_FILENOTFOUND)) |
4591 | 0 | { |
4592 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
4593 | 0 | ISC_LOG_ERROR, |
4594 | 0 | "not loaded due to errors."); |
4595 | 0 | } else if (zone->type == dns_zone_primary) { |
4596 | 0 | result = ISC_R_SUCCESS; |
4597 | 0 | } |
4598 | 0 | } |
4599 | |
|
4600 | 2 | done: |
4601 | 2 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADPENDING); |
4602 | | /* |
4603 | | * If this is an inline-signed zone and we were called for the raw |
4604 | | * zone, we need to clear DNS_ZONEFLG_LOADPENDING for the secure zone |
4605 | | * as well, but only if this is a reload, not an initial zone load: in |
4606 | | * the former case, zone_postload() will not be run for the secure |
4607 | | * zone; in the latter case, it will be. Check which case we are |
4608 | | * dealing with by consulting the DNS_ZONEFLG_LOADED flag for the |
4609 | | * secure zone: if it is set, this must be a reload. |
4610 | | */ |
4611 | 2 | if (dns__zone_inline_raw(zone) && |
4612 | 0 | DNS_ZONE_FLAG(zone->secure, DNS_ZONEFLG_LOADED)) |
4613 | 0 | { |
4614 | 0 | DNS_ZONE_CLRFLAG(zone->secure, DNS_ZONEFLG_LOADPENDING); |
4615 | | /* |
4616 | | * Re-start zone maintenance if it had been stalled |
4617 | | * due to DNS_ZONEFLG_LOADPENDING being set when |
4618 | | * zone_maintenance was called. |
4619 | | */ |
4620 | 0 | if (zone->secure->loop != NULL) { |
4621 | 0 | dns__zone_settimer(zone->secure, now); |
4622 | 0 | } |
4623 | 0 | } |
4624 | | |
4625 | 2 | zone_debuglog(zone, __func__, 99, "done"); |
4626 | | |
4627 | 2 | return result; |
4628 | 0 | } |
4629 | | |
4630 | | bool |
4631 | 0 | dns__zone_free_check(dns_zone_t *zone) { |
4632 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
4633 | |
|
4634 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SHUTDOWN) && |
4635 | 0 | isc_refcount_current(&zone->irefs) == 0) |
4636 | 0 | { |
4637 | | /* |
4638 | | * DNS_ZONEFLG_SHUTDOWN can only be set if references == 0. |
4639 | | */ |
4640 | 0 | INSIST(isc_refcount_current(&zone->references) == 0); |
4641 | 0 | return true; |
4642 | 0 | } |
4643 | 0 | return false; |
4644 | 0 | } |
4645 | | |
4646 | | static bool |
4647 | | zone_check_ns(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version, |
4648 | 0 | dns_name_t *name, bool logit) { |
4649 | 0 | isc_result_t result; |
4650 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
4651 | 0 | char altbuf[DNS_NAME_FORMATSIZE]; |
4652 | 0 | dns_fixedname_t fixed; |
4653 | 0 | dns_name_t *foundname; |
4654 | 0 | int level; |
4655 | |
|
4656 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NOCHECKNS)) { |
4657 | 0 | return true; |
4658 | 0 | } |
4659 | | |
4660 | 0 | if (zone->type == dns_zone_primary) { |
4661 | 0 | level = ISC_LOG_ERROR; |
4662 | 0 | } else { |
4663 | 0 | level = ISC_LOG_WARNING; |
4664 | 0 | } |
4665 | |
|
4666 | 0 | foundname = dns_fixedname_initname(&fixed); |
4667 | |
|
4668 | 0 | result = dns_db_find(db, name, version, dns_rdatatype_a, 0, 0, |
4669 | 0 | foundname, NULL, NULL); |
4670 | 0 | if (result == ISC_R_SUCCESS) { |
4671 | 0 | return true; |
4672 | 0 | } |
4673 | | |
4674 | 0 | if (result == DNS_R_NXRRSET) { |
4675 | 0 | result = dns_db_find(db, name, version, dns_rdatatype_aaaa, 0, |
4676 | 0 | 0, foundname, NULL, NULL); |
4677 | 0 | if (result == ISC_R_SUCCESS) { |
4678 | 0 | return true; |
4679 | 0 | } |
4680 | 0 | } |
4681 | | |
4682 | 0 | if (result == DNS_R_NXRRSET || result == DNS_R_NXDOMAIN || |
4683 | 0 | result == DNS_R_EMPTYNAME) |
4684 | 0 | { |
4685 | 0 | if (logit) { |
4686 | 0 | dns_name_format(name, namebuf, sizeof namebuf); |
4687 | 0 | dns_zone_log(zone, level, |
4688 | 0 | "NS '%s' has no address " |
4689 | 0 | "records (A or AAAA)", |
4690 | 0 | namebuf); |
4691 | 0 | } |
4692 | 0 | return false; |
4693 | 0 | } |
4694 | | |
4695 | 0 | if (result == DNS_R_CNAME) { |
4696 | 0 | if (logit) { |
4697 | 0 | dns_name_format(name, namebuf, sizeof namebuf); |
4698 | 0 | dns_zone_log(zone, level, |
4699 | 0 | "NS '%s' is a CNAME " |
4700 | 0 | "(illegal)", |
4701 | 0 | namebuf); |
4702 | 0 | } |
4703 | 0 | return false; |
4704 | 0 | } |
4705 | | |
4706 | 0 | if (result == DNS_R_DNAME) { |
4707 | 0 | if (logit) { |
4708 | 0 | dns_name_format(name, namebuf, sizeof namebuf); |
4709 | 0 | dns_name_format(foundname, altbuf, sizeof altbuf); |
4710 | 0 | dns_zone_log(zone, level, |
4711 | 0 | "NS '%s' is below a DNAME " |
4712 | 0 | "'%s' (illegal)", |
4713 | 0 | namebuf, altbuf); |
4714 | 0 | } |
4715 | 0 | return false; |
4716 | 0 | } |
4717 | | |
4718 | 0 | return true; |
4719 | 0 | } |
4720 | | |
4721 | | static isc_result_t |
4722 | | zone_count_ns_rr(dns_zone_t *zone, dns_db_t *db, dns_dbnode_t *node, |
4723 | | dns_dbversion_t *version, unsigned int *nscount, |
4724 | 2 | unsigned int *errors, bool logit) { |
4725 | 2 | isc_result_t result; |
4726 | 2 | unsigned int count = 0; |
4727 | 2 | unsigned int ecount = 0; |
4728 | 2 | dns_rdataset_t rdataset; |
4729 | 2 | dns_rdata_ns_t ns; |
4730 | | |
4731 | 2 | dns_rdataset_init(&rdataset); |
4732 | 2 | result = dns_db_findrdataset(db, node, version, dns_rdatatype_ns, |
4733 | 2 | dns_rdatatype_none, 0, &rdataset, NULL); |
4734 | 2 | if (result == ISC_R_NOTFOUND) { |
4735 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
4736 | 0 | goto success; |
4737 | 0 | } |
4738 | 2 | if (result != ISC_R_SUCCESS) { |
4739 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
4740 | 0 | goto invalidate_rdataset; |
4741 | 0 | } |
4742 | | |
4743 | 2 | DNS_RDATASET_FOREACH(&rdataset) { |
4744 | 2 | if (errors != NULL && zone->rdclass == dns_rdataclass_in && |
4745 | 2 | (zone->type == dns_zone_primary || |
4746 | 0 | zone->type == dns_zone_secondary || |
4747 | 0 | zone->type == dns_zone_mirror)) |
4748 | 2 | { |
4749 | 2 | dns_rdata_t rdata = DNS_RDATA_INIT; |
4750 | 2 | dns_rdataset_current(&rdataset, &rdata); |
4751 | | |
4752 | 2 | result = dns_rdata_tostruct(&rdata, &ns, NULL); |
4753 | 2 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
4754 | 2 | if (dns_name_issubdomain(&ns.name, &zone->origin) && |
4755 | 0 | !zone_check_ns(zone, db, version, &ns.name, logit)) |
4756 | 0 | { |
4757 | 0 | ecount++; |
4758 | 0 | } |
4759 | 2 | } |
4760 | 2 | count++; |
4761 | 2 | } |
4762 | 2 | dns_rdataset_disassociate(&rdataset); |
4763 | | |
4764 | 2 | success: |
4765 | 2 | SET_IF_NOT_NULL(nscount, count); |
4766 | 2 | SET_IF_NOT_NULL(errors, ecount); |
4767 | | |
4768 | 2 | result = ISC_R_SUCCESS; |
4769 | | |
4770 | 2 | invalidate_rdataset: |
4771 | 2 | dns_rdataset_invalidate(&rdataset); |
4772 | | |
4773 | 2 | return result; |
4774 | 2 | } |
4775 | | |
4776 | | #define SET_SOA_VALUES(soattl_v, serial_v, refresh_v, retry_v, expire_v, \ |
4777 | | minimum_v) \ |
4778 | 4 | { \ |
4779 | 4 | SET_IF_NOT_NULL(soattl, soattl_v); \ |
4780 | 4 | SET_IF_NOT_NULL(serial, serial_v); \ |
4781 | 4 | SET_IF_NOT_NULL(refresh, refresh_v); \ |
4782 | 4 | SET_IF_NOT_NULL(retry, retry_v); \ |
4783 | 4 | SET_IF_NOT_NULL(expire, expire_v); \ |
4784 | 4 | SET_IF_NOT_NULL(minimum, minimum_v); \ |
4785 | 4 | } |
4786 | | |
4787 | | #define CLR_SOA_VALUES() \ |
4788 | 2 | { \ |
4789 | 2 | SET_SOA_VALUES(0, 0, 0, 0, 0, 0); \ |
4790 | 2 | } |
4791 | | |
4792 | | static isc_result_t |
4793 | | zone_load_soa_rr(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version, |
4794 | | unsigned int *soacount, uint32_t *soattl, uint32_t *serial, |
4795 | | uint32_t *refresh, uint32_t *retry, uint32_t *expire, |
4796 | 2 | uint32_t *minimum) { |
4797 | 2 | isc_result_t result; |
4798 | 2 | unsigned int count = 0; |
4799 | 2 | dns_rdataset_t rdataset; |
4800 | | |
4801 | 2 | dns_rdataset_init(&rdataset); |
4802 | 2 | result = dns_db_findrdataset(db, node, version, dns_rdatatype_soa, |
4803 | 2 | dns_rdatatype_none, 0, &rdataset, NULL); |
4804 | 2 | if (result == ISC_R_NOTFOUND) { |
4805 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
4806 | 0 | result = ISC_R_SUCCESS; |
4807 | 0 | goto invalidate_rdataset; |
4808 | 0 | } |
4809 | 2 | if (result != ISC_R_SUCCESS) { |
4810 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
4811 | 0 | goto invalidate_rdataset; |
4812 | 0 | } |
4813 | | |
4814 | 2 | DNS_RDATASET_FOREACH(&rdataset) { |
4815 | 2 | dns_rdata_t rdata = DNS_RDATA_INIT; |
4816 | 2 | dns_rdataset_current(&rdataset, &rdata); |
4817 | | |
4818 | 2 | count++; |
4819 | 2 | if (count == 1) { |
4820 | 2 | dns_rdata_soa_t soa; |
4821 | 2 | result = dns_rdata_tostruct(&rdata, &soa, NULL); |
4822 | 2 | SET_SOA_VALUES(rdataset.ttl, soa.serial, soa.refresh, |
4823 | 2 | soa.retry, soa.expire, soa.minimum); |
4824 | 2 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
4825 | 2 | } |
4826 | 2 | } |
4827 | 2 | dns_rdataset_disassociate(&rdataset); |
4828 | | |
4829 | 2 | result = ISC_R_SUCCESS; |
4830 | | |
4831 | 2 | invalidate_rdataset: |
4832 | 2 | SET_IF_NOT_NULL(soacount, count); |
4833 | 2 | if (count == 0) { |
4834 | 0 | CLR_SOA_VALUES(); |
4835 | 0 | } |
4836 | | |
4837 | 2 | dns_rdataset_invalidate(&rdataset); |
4838 | | |
4839 | 2 | return result; |
4840 | 2 | } |
4841 | | |
4842 | | /* |
4843 | | * zone must be locked. |
4844 | | */ |
4845 | | static isc_result_t |
4846 | | zone_get_from_db(dns_zone_t *zone, dns_db_t *db, unsigned int *nscount, |
4847 | | unsigned int *soacount, uint32_t *soattl, uint32_t *serial, |
4848 | | uint32_t *refresh, uint32_t *retry, uint32_t *expire, |
4849 | 2 | uint32_t *minimum, unsigned int *errors) { |
4850 | 2 | isc_result_t result; |
4851 | 2 | isc_result_t answer = ISC_R_SUCCESS; |
4852 | 2 | dns_dbversion_t *version = NULL; |
4853 | 2 | dns_dbnode_t *node; |
4854 | | |
4855 | 2 | REQUIRE(db != NULL); |
4856 | 2 | REQUIRE(zone != NULL); |
4857 | | |
4858 | 2 | dns_db_currentversion(db, &version); |
4859 | | |
4860 | 2 | SET_IF_NOT_NULL(nscount, 0); |
4861 | 2 | SET_IF_NOT_NULL(soacount, 0); |
4862 | 2 | SET_IF_NOT_NULL(errors, 0); |
4863 | 2 | CLR_SOA_VALUES(); |
4864 | | |
4865 | 2 | node = NULL; |
4866 | 2 | result = dns_db_findnode(db, &zone->origin, false, &node); |
4867 | 2 | if (result != ISC_R_SUCCESS) { |
4868 | 0 | answer = result; |
4869 | 0 | goto closeversion; |
4870 | 0 | } |
4871 | | |
4872 | 2 | if (nscount != NULL || errors != NULL) { |
4873 | 2 | result = zone_count_ns_rr(zone, db, node, version, nscount, |
4874 | 2 | errors, true); |
4875 | 2 | if (result != ISC_R_SUCCESS) { |
4876 | 0 | answer = result; |
4877 | 0 | } |
4878 | 2 | } |
4879 | | |
4880 | 2 | if (soacount != NULL || soattl != NULL || serial != NULL || |
4881 | 0 | refresh != NULL || retry != NULL || expire != NULL || |
4882 | 0 | minimum != NULL) |
4883 | 2 | { |
4884 | 2 | result = zone_load_soa_rr(db, node, version, soacount, soattl, |
4885 | 2 | serial, refresh, retry, expire, |
4886 | 2 | minimum); |
4887 | 2 | if (result != ISC_R_SUCCESS) { |
4888 | 0 | answer = result; |
4889 | 0 | } |
4890 | 2 | } |
4891 | | |
4892 | 2 | dns_db_detachnode(&node); |
4893 | 2 | closeversion: |
4894 | 2 | dns_db_closeversion(db, &version, false); |
4895 | | |
4896 | 2 | return answer; |
4897 | 2 | } |
4898 | | |
4899 | | static void |
4900 | 0 | zone_destroy(dns_zone_t *zone) { |
4901 | | /* |
4902 | | * Stop things being restarted after we cancel them below. |
4903 | | */ |
4904 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_EXITING); |
4905 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), "final reference detached"); |
4906 | |
|
4907 | 0 | if (zone->loop == NULL) { |
4908 | | /* |
4909 | | * This zone is unmanaged; we're probably running in |
4910 | | * named-checkzone or a unit test. There's no loop, so we |
4911 | | * need to free it immediately. |
4912 | | */ |
4913 | 0 | zone_shutdown(zone); |
4914 | 0 | } else { |
4915 | | /* |
4916 | | * This zone has a loop; it can clean |
4917 | | * itself up asynchronously. |
4918 | | */ |
4919 | 0 | isc_async_run(zone->loop, zone_shutdown, zone); |
4920 | 0 | } |
4921 | 0 | } |
4922 | | |
4923 | | #if DNS_ZONE_TRACE |
4924 | | ISC_REFCOUNT_TRACE_IMPL(dns_zone, zone_destroy); |
4925 | | #else |
4926 | 606 | ISC_REFCOUNT_IMPL(dns_zone, zone_destroy); Line | Count | Source | 4926 | | ISC_REFCOUNT_IMPL(dns_zone, zone_destroy); |
Line | Count | Source | 4926 | | ISC_REFCOUNT_IMPL(dns_zone, zone_destroy); |
Line | Count | Source | 4926 | | ISC_REFCOUNT_IMPL(dns_zone, zone_destroy); |
|
4927 | 606 | #endif |
4928 | 606 | |
4929 | 606 | static void |
4930 | 606 | zone_iattach(dns_zone_t *source, dns_zone_t **target) { |
4931 | 4 | REQUIRE(DNS_ZONE_VALID(source)); |
4932 | 4 | REQUIRE(LOCKED_ZONE(source)); |
4933 | 4 | REQUIRE(target != NULL && *target == NULL); |
4934 | 4 | INSIST(isc_refcount_increment0(&source->irefs) + |
4935 | 4 | isc_refcount_current(&source->references) > |
4936 | 4 | 0); |
4937 | 4 | *target = source; |
4938 | 4 | } |
4939 | | |
4940 | | void |
4941 | 0 | dns__zone_iattach_locked(dns_zone_t *source, dns_zone_t **target) { |
4942 | 0 | zone_iattach(source, target); |
4943 | 0 | } |
4944 | | |
4945 | | void |
4946 | 0 | dns_zone_iattach(dns_zone_t *source, dns_zone_t **target) { |
4947 | 0 | REQUIRE(DNS_ZONE_VALID(source)); |
4948 | |
|
4949 | 0 | LOCK_ZONE(source); |
4950 | 0 | zone_iattach(source, target); |
4951 | 0 | UNLOCK_ZONE(source); |
4952 | 0 | } |
4953 | | |
4954 | | static void |
4955 | 4 | zone_idetach(dns_zone_t **zonep) { |
4956 | 4 | dns_zone_t *zone; |
4957 | | |
4958 | | /* |
4959 | | * 'zone' locked by caller. |
4960 | | */ |
4961 | 4 | REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep)); |
4962 | 4 | REQUIRE(LOCKED_ZONE(*zonep)); |
4963 | | |
4964 | 4 | zone = *zonep; |
4965 | 4 | *zonep = NULL; |
4966 | | |
4967 | 4 | INSIST(isc_refcount_decrement(&zone->irefs) - 1 + |
4968 | 4 | isc_refcount_current(&zone->references) > |
4969 | 4 | 0); |
4970 | 4 | } |
4971 | | |
4972 | | void |
4973 | 0 | dns__zone_idetach_locked(dns_zone_t **zonep) { |
4974 | 0 | zone_idetach(zonep); |
4975 | 0 | } |
4976 | | |
4977 | | void |
4978 | 0 | dns_zone_idetach(dns_zone_t **zonep) { |
4979 | 0 | dns_zone_t *zone; |
4980 | |
|
4981 | 0 | REQUIRE(zonep != NULL && DNS_ZONE_VALID(*zonep)); |
4982 | |
|
4983 | 0 | zone = *zonep; |
4984 | 0 | *zonep = NULL; |
4985 | |
|
4986 | 0 | if (isc_refcount_decrement(&zone->irefs) == 1) { |
4987 | 0 | bool free_needed; |
4988 | 0 | LOCK_ZONE(zone); |
4989 | 0 | free_needed = dns__zone_free_check(zone); |
4990 | 0 | UNLOCK_ZONE(zone); |
4991 | 0 | if (free_needed) { |
4992 | 0 | dns__zone_free(zone); |
4993 | 0 | } |
4994 | 0 | } |
4995 | 0 | } |
4996 | | |
4997 | | isc_refcount_t * |
4998 | 0 | dns__zone_irefs(dns_zone_t *zone) { |
4999 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
5000 | |
|
5001 | 0 | return &zone->irefs; |
5002 | 0 | } |
5003 | | |
5004 | | static void |
5005 | 0 | dns_zone_setskr(dns_zone_t *zone, dns_skr_t *skr) { |
5006 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
5007 | |
|
5008 | 0 | LOCK_ZONE(zone); |
5009 | 0 | zone->skrbundle = NULL; |
5010 | 0 | if (zone->skr != NULL) { |
5011 | 0 | dns_skr_detach(&zone->skr); |
5012 | 0 | } |
5013 | 0 | if (skr != NULL) { |
5014 | 0 | dns_skr_attach(skr, &zone->skr); |
5015 | 0 | } |
5016 | 0 | UNLOCK_ZONE(zone); |
5017 | 0 | } |
5018 | | |
5019 | | dns_skrbundle_t * |
5020 | 0 | dns_zone_getskrbundle(dns_zone_t *zone) { |
5021 | 0 | dns_skrbundle_t *bundle; |
5022 | |
|
5023 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
5024 | |
|
5025 | 0 | LOCK_ZONE(zone); |
5026 | 0 | if (dns__zone_inline_raw(zone) && zone->secure != NULL) { |
5027 | 0 | bundle = zone->secure->skrbundle; |
5028 | 0 | } else { |
5029 | 0 | bundle = zone->skrbundle; |
5030 | 0 | } |
5031 | 0 | UNLOCK_ZONE(zone); |
5032 | |
|
5033 | 0 | return bundle; |
5034 | 0 | } |
5035 | | |
5036 | | void |
5037 | 0 | dns_zone_setoption(dns_zone_t *zone, dns_zoneopt_t option, bool value) { |
5038 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
5039 | |
|
5040 | 0 | if (value) { |
5041 | 0 | DNS_ZONE_SETOPTION(zone, option); |
5042 | 0 | } else { |
5043 | 0 | DNS_ZONE_CLROPTION(zone, option); |
5044 | 0 | } |
5045 | 0 | } |
5046 | | |
5047 | | dns_zoneopt_t |
5048 | 0 | dns_zone_getoptions(dns_zone_t *zone) { |
5049 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
5050 | |
|
5051 | 0 | return atomic_load_relaxed(&zone->options); |
5052 | 0 | } |
5053 | | |
5054 | | static bool |
5055 | 0 | was_dumping(dns_zone_t *zone) { |
5056 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
5057 | |
|
5058 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) { |
5059 | 0 | return true; |
5060 | 0 | } |
5061 | | |
5062 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DUMPING); |
5063 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP); |
5064 | 0 | isc_time_settoepoch(&zone->dumptime); |
5065 | 0 | return false; |
5066 | 0 | } |
5067 | | |
5068 | | static isc_result_t |
5069 | | keyfromfile(dns_zone_t *zone, dst_key_t *pubkey, isc_mem_t *mctx, |
5070 | 0 | dst_key_t **key) { |
5071 | 0 | const char *directory = zone->keydirectory; |
5072 | 0 | dns_kasp_t *kasp = zone->kasp; |
5073 | 0 | dst_key_t *foundkey = NULL; |
5074 | 0 | isc_result_t result = ISC_R_NOTFOUND; |
5075 | |
|
5076 | 0 | if (kasp == NULL || (strcmp(dns_kasp_getname(kasp), "none") == 0) || |
5077 | 0 | (strcmp(dns_kasp_getname(kasp), "insecure") == 0)) |
5078 | 0 | { |
5079 | 0 | result = dst_key_fromfile( |
5080 | 0 | dst_key_name(pubkey), dst_key_id(pubkey), |
5081 | 0 | dst_key_alg(pubkey), |
5082 | 0 | DST_TYPE_PUBLIC | DST_TYPE_PRIVATE | DST_TYPE_STATE, |
5083 | 0 | directory, mctx, &foundkey); |
5084 | 0 | } else { |
5085 | 0 | ISC_LIST_FOREACH(dns_kasp_keys(kasp), kkey, link) { |
5086 | 0 | dns_keystore_t *ks = dns_kasp_key_keystore(kkey); |
5087 | 0 | directory = dns_keystore_directory(ks, |
5088 | 0 | zone->keydirectory); |
5089 | |
|
5090 | 0 | result = dst_key_fromfile( |
5091 | 0 | dst_key_name(pubkey), dst_key_id(pubkey), |
5092 | 0 | dst_key_alg(pubkey), |
5093 | 0 | DST_TYPE_PUBLIC | DST_TYPE_PRIVATE | |
5094 | 0 | DST_TYPE_STATE, |
5095 | 0 | directory, mctx, &foundkey); |
5096 | 0 | if (result == ISC_R_SUCCESS) { |
5097 | 0 | break; |
5098 | 0 | } |
5099 | 0 | } |
5100 | 0 | } |
5101 | |
|
5102 | 0 | *key = foundkey; |
5103 | 0 | return result; |
5104 | 0 | } |
5105 | | |
5106 | | static isc_result_t |
5107 | | findzonekeys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, |
5108 | | dns_dbnode_t *node, const dns_name_t *name, isc_stdtime_t now, |
5109 | | isc_mem_t *mctx, unsigned int maxkeys, dst_key_t **keys, |
5110 | 0 | unsigned int *nkeys) { |
5111 | 0 | dns_rdataset_t rdataset; |
5112 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
5113 | 0 | isc_result_t result; |
5114 | 0 | dst_key_t *pubkey = NULL; |
5115 | 0 | unsigned int count = 0; |
5116 | |
|
5117 | 0 | *nkeys = 0; |
5118 | 0 | memset(keys, 0, sizeof(*keys) * maxkeys); |
5119 | 0 | dns_rdataset_init(&rdataset); |
5120 | 0 | CHECK(dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey, 0, 0, |
5121 | 0 | &rdataset, NULL)); |
5122 | 0 | CHECK(dns_rdataset_first(&rdataset)); |
5123 | 0 | while (result == ISC_R_SUCCESS && count < maxkeys) { |
5124 | 0 | pubkey = NULL; |
5125 | 0 | dns_rdataset_current(&rdataset, &rdata); |
5126 | 0 | CHECK(dns_dnssec_keyfromrdata(name, &rdata, mctx, &pubkey)); |
5127 | 0 | dst_key_setttl(pubkey, rdataset.ttl); |
5128 | |
|
5129 | 0 | if (!ZONEKEY(pubkey)) { |
5130 | 0 | goto next; |
5131 | 0 | } |
5132 | | /* Corrupted .key file? */ |
5133 | 0 | if (!dns_name_equal(name, dst_key_name(pubkey))) { |
5134 | 0 | goto next; |
5135 | 0 | } |
5136 | 0 | keys[count] = NULL; |
5137 | 0 | result = keyfromfile(zone, pubkey, mctx, &keys[count]); |
5138 | | |
5139 | | /* |
5140 | | * If the key was revoked and the private file |
5141 | | * doesn't exist, maybe it was revoked internally |
5142 | | * by named. Try loading the unrevoked version. |
5143 | | */ |
5144 | 0 | if (result == ISC_R_FILENOTFOUND) { |
5145 | 0 | uint32_t flags; |
5146 | 0 | flags = dst_key_flags(pubkey); |
5147 | 0 | if ((flags & DNS_KEYFLAG_REVOKE) != 0) { |
5148 | 0 | dst_key_setflags(pubkey, |
5149 | 0 | flags & ~DNS_KEYFLAG_REVOKE); |
5150 | 0 | result = keyfromfile(zone, pubkey, mctx, |
5151 | 0 | &keys[count]); |
5152 | 0 | if (result == ISC_R_SUCCESS && |
5153 | 0 | dst_key_pubcompare(pubkey, keys[count], |
5154 | 0 | false)) |
5155 | 0 | { |
5156 | 0 | dst_key_setflags(keys[count], flags); |
5157 | 0 | } |
5158 | 0 | dst_key_setflags(pubkey, flags); |
5159 | 0 | } |
5160 | 0 | } |
5161 | |
|
5162 | 0 | if (result != ISC_R_SUCCESS) { |
5163 | 0 | char filename[DNS_NAME_FORMATSIZE + |
5164 | 0 | DNS_SECALG_FORMATSIZE + |
5165 | 0 | sizeof("key file for //65535")]; |
5166 | 0 | isc_result_t result2; |
5167 | 0 | isc_buffer_t buf; |
5168 | |
|
5169 | 0 | isc_buffer_init(&buf, filename, sizeof(filename)); |
5170 | 0 | result2 = dst_key_getfilename( |
5171 | 0 | dst_key_name(pubkey), dst_key_id(pubkey), |
5172 | 0 | dst_key_alg(pubkey), |
5173 | 0 | DST_TYPE_PUBLIC | DST_TYPE_PRIVATE | |
5174 | 0 | DST_TYPE_STATE, |
5175 | 0 | NULL, mctx, &buf); |
5176 | 0 | if (result2 != ISC_R_SUCCESS) { |
5177 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
5178 | 0 | char algbuf[DNS_SECALG_FORMATSIZE]; |
5179 | |
|
5180 | 0 | dns_name_format(dst_key_name(pubkey), namebuf, |
5181 | 0 | sizeof(namebuf)); |
5182 | 0 | dns_secalg_format(dst_key_alg(pubkey), algbuf, |
5183 | 0 | sizeof(algbuf)); |
5184 | 0 | snprintf(filename, sizeof(filename) - 1, |
5185 | 0 | "key file for %s/%s/%d", namebuf, |
5186 | 0 | algbuf, dst_key_id(pubkey)); |
5187 | 0 | } |
5188 | |
|
5189 | 0 | isc_log_write(DNS_LOGCATEGORY_GENERAL, |
5190 | 0 | DNS_LOGMODULE_DNSSEC, ISC_LOG_WARNING, |
5191 | 0 | "dns_zone_findkeys: error reading %s: %s", |
5192 | 0 | filename, isc_result_totext(result)); |
5193 | 0 | } |
5194 | |
|
5195 | 0 | if (result == ISC_R_FILENOTFOUND || result == ISC_R_NOPERM) { |
5196 | 0 | keys[count] = pubkey; |
5197 | 0 | pubkey = NULL; |
5198 | 0 | count++; |
5199 | 0 | goto next; |
5200 | 0 | } |
5201 | | |
5202 | 0 | CHECK(result); |
5203 | | |
5204 | | /* |
5205 | | * If a key is marked inactive, skip it |
5206 | | */ |
5207 | 0 | if (!dns_dnssec_keyactive(keys[count], now)) { |
5208 | 0 | dst_key_setinactive(pubkey, true); |
5209 | 0 | dst_key_free(&keys[count]); |
5210 | 0 | keys[count] = pubkey; |
5211 | 0 | pubkey = NULL; |
5212 | 0 | count++; |
5213 | 0 | goto next; |
5214 | 0 | } |
5215 | | |
5216 | | /* |
5217 | | * Whatever the key's default TTL may have |
5218 | | * been, the rdataset TTL takes priority. |
5219 | | */ |
5220 | 0 | dst_key_setttl(keys[count], rdataset.ttl); |
5221 | 0 | count++; |
5222 | 0 | next: |
5223 | 0 | if (pubkey != NULL) { |
5224 | 0 | dst_key_free(&pubkey); |
5225 | 0 | } |
5226 | 0 | dns_rdata_reset(&rdata); |
5227 | 0 | result = dns_rdataset_next(&rdataset); |
5228 | 0 | } |
5229 | 0 | if (result != ISC_R_NOMORE) { |
5230 | 0 | CHECK(result); |
5231 | 0 | } |
5232 | 0 | if (count == 0) { |
5233 | 0 | result = ISC_R_NOTFOUND; |
5234 | 0 | } else { |
5235 | 0 | result = ISC_R_SUCCESS; |
5236 | 0 | } |
5237 | |
|
5238 | 0 | cleanup: |
5239 | 0 | dns_rdataset_cleanup(&rdataset); |
5240 | 0 | if (pubkey != NULL) { |
5241 | 0 | dst_key_free(&pubkey); |
5242 | 0 | } |
5243 | 0 | if (result != ISC_R_SUCCESS) { |
5244 | 0 | while (count > 0) { |
5245 | 0 | dst_key_free(&keys[--count]); |
5246 | 0 | } |
5247 | 0 | } |
5248 | 0 | *nkeys = count; |
5249 | 0 | return result; |
5250 | 0 | } |
5251 | | |
5252 | | /*% |
5253 | | * Find up to 'maxkeys' DNSSEC keys used for signing version 'ver' of database |
5254 | | * 'db' for zone 'zone' in its key directory, then load these keys into 'keys'. |
5255 | | * Only load the public part of a given key if it is not active at timestamp |
5256 | | * 'now'. Store the number of keys found in 'nkeys'. |
5257 | | */ |
5258 | | isc_result_t |
5259 | | dns_zone_findkeys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, |
5260 | | isc_stdtime_t now, isc_mem_t *mctx, unsigned int maxkeys, |
5261 | 0 | dst_key_t **keys, unsigned int *nkeys) { |
5262 | 0 | isc_result_t result; |
5263 | 0 | dns_dbnode_t *node = NULL; |
5264 | |
|
5265 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
5266 | 0 | REQUIRE(mctx != NULL); |
5267 | 0 | REQUIRE(nkeys != NULL); |
5268 | 0 | REQUIRE(keys != NULL); |
5269 | |
|
5270 | 0 | CHECK(dns_db_findnode(db, dns_db_origin(db), false, &node)); |
5271 | |
|
5272 | 0 | dns_zone_lock_keyfiles(zone); |
5273 | |
|
5274 | 0 | result = findzonekeys(zone, db, ver, node, dns_db_origin(db), now, mctx, |
5275 | 0 | maxkeys, keys, nkeys); |
5276 | |
|
5277 | 0 | dns_zone_unlock_keyfiles(zone); |
5278 | |
|
5279 | 0 | if (result == ISC_R_NOTFOUND) { |
5280 | 0 | result = ISC_R_SUCCESS; |
5281 | 0 | } |
5282 | |
|
5283 | 0 | cleanup: |
5284 | |
|
5285 | 0 | if (node != NULL) { |
5286 | 0 | dns_db_detachnode(&node); |
5287 | 0 | } |
5288 | 0 | return result; |
5289 | 0 | } |
5290 | | |
5291 | | void |
5292 | 0 | dns_zone_prepare_shutdown(dns_zone_t *zone) { |
5293 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
5294 | |
|
5295 | 0 | LOCK_ZONE(zone); |
5296 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_EXITING); |
5297 | 0 | UNLOCK_ZONE(zone); |
5298 | 0 | } |
5299 | | |
5300 | | /*% |
5301 | | * Find DNSSEC keys used for signing zone with dnssec-policy. Load these keys |
5302 | | * into 'keys'. Requires KASP to be locked. |
5303 | | */ |
5304 | | isc_result_t |
5305 | | dns_zone_getdnsseckeys(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, |
5306 | 0 | isc_stdtime_t now, dns_dnsseckeylist_t *keys) { |
5307 | 0 | isc_result_t result; |
5308 | 0 | const char *dir = dns_zone_getkeydirectory(zone); |
5309 | 0 | dns_dbnode_t *node = NULL; |
5310 | 0 | dns_dnsseckeylist_t dnskeys; |
5311 | 0 | dns_name_t *origin = dns_zone_getorigin(zone); |
5312 | 0 | dns_kasp_t *kasp = zone->kasp; |
5313 | 0 | dns_rdataset_t keyset; |
5314 | |
|
5315 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
5316 | 0 | REQUIRE(kasp != NULL); |
5317 | |
|
5318 | 0 | ISC_LIST_INIT(dnskeys); |
5319 | |
|
5320 | 0 | dns_rdataset_init(&keyset); |
5321 | |
|
5322 | 0 | CHECK(dns_db_findnode(db, origin, false, &node)); |
5323 | | |
5324 | | /* Get keys from private key files. */ |
5325 | 0 | dns_zone_lock_keyfiles(zone); |
5326 | 0 | result = dns_dnssec_findmatchingkeys( |
5327 | 0 | origin, kasp, dir, dns_zone_getkeystores(zone), now, false, |
5328 | 0 | dns_zone_getmctx(zone), keys); |
5329 | 0 | dns_zone_unlock_keyfiles(zone); |
5330 | |
|
5331 | 0 | if (result != ISC_R_NOTFOUND) { |
5332 | 0 | CHECK(result); |
5333 | 0 | } |
5334 | | |
5335 | | /* Get public keys (dnskeys). */ |
5336 | 0 | dns_rdataset_init(&keyset); |
5337 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey, |
5338 | 0 | dns_rdatatype_none, 0, &keyset, NULL); |
5339 | 0 | if (result == ISC_R_SUCCESS) { |
5340 | 0 | CHECK(dns_dnssec_keylistfromrdataset( |
5341 | 0 | origin, kasp, dir, dns_zone_getmctx(zone), &keyset, |
5342 | 0 | NULL, NULL, false, false, &dnskeys)); |
5343 | 0 | } else if (result != ISC_R_NOTFOUND) { |
5344 | 0 | CHECK(result); |
5345 | 0 | } |
5346 | | |
5347 | | /* Add new 'dnskeys' to 'keys'. */ |
5348 | 0 | ISC_LIST_FOREACH(dnskeys, k1, link) { |
5349 | 0 | bool match = false; |
5350 | |
|
5351 | 0 | ISC_LIST_FOREACH(*keys, k2, link) { |
5352 | 0 | if (dst_key_compare(k1->key, k2->key)) { |
5353 | 0 | match = true; |
5354 | 0 | break; |
5355 | 0 | } |
5356 | 0 | } |
5357 | | |
5358 | | /* No match found, add the new key. */ |
5359 | 0 | if (!match) { |
5360 | 0 | ISC_LIST_UNLINK(dnskeys, k1, link); |
5361 | 0 | ISC_LIST_APPEND(*keys, k1, link); |
5362 | 0 | } |
5363 | 0 | } |
5364 | |
|
5365 | 0 | cleanup: |
5366 | 0 | dns_rdataset_cleanup(&keyset); |
5367 | 0 | if (node != NULL) { |
5368 | 0 | dns_db_detachnode(&node); |
5369 | 0 | } |
5370 | 0 | ISC_LIST_FOREACH(dnskeys, key, link) { |
5371 | 0 | ISC_LIST_UNLINK(dnskeys, key, link); |
5372 | 0 | dns_dnsseckey_destroy(dns_zone_getmctx(zone), &key); |
5373 | 0 | } |
5374 | 0 | return result; |
5375 | 0 | } |
5376 | | |
5377 | | static isc_result_t |
5378 | | offline(dns_db_t *db, dns_dbversion_t *ver, dns__zonediff_t *zonediff, |
5379 | 0 | dns_name_t *name, dns_ttl_t ttl, dns_rdata_t *rdata) { |
5380 | 0 | isc_result_t result; |
5381 | |
|
5382 | 0 | if ((rdata->flags & DNS_RDATA_OFFLINE) != 0) { |
5383 | 0 | return ISC_R_SUCCESS; |
5384 | 0 | } |
5385 | 0 | RETERR(update_one_rr(db, ver, zonediff->diff, DNS_DIFFOP_DELRESIGN, |
5386 | 0 | name, ttl, rdata)); |
5387 | 0 | rdata->flags |= DNS_RDATA_OFFLINE; |
5388 | 0 | result = update_one_rr(db, ver, zonediff->diff, DNS_DIFFOP_ADDRESIGN, |
5389 | 0 | name, ttl, rdata); |
5390 | 0 | zonediff->offline = true; |
5391 | 0 | return result; |
5392 | 0 | } |
5393 | | |
5394 | | static void |
5395 | | set_key_expiry_warning(dns_zone_t *zone, isc_stdtime_t when, |
5396 | 0 | isc_stdtime_t now) { |
5397 | 0 | unsigned int delta; |
5398 | 0 | char timebuf[80]; |
5399 | |
|
5400 | 0 | LOCK_ZONE(zone); |
5401 | 0 | zone->key_expiry = when; |
5402 | 0 | if (when <= now) { |
5403 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
5404 | 0 | "DNSKEY RRSIG(s) have expired"); |
5405 | 0 | isc_time_settoepoch(&zone->keywarntime); |
5406 | 0 | } else if (when < now + 7 * 24 * 3600) { |
5407 | 0 | isc_time_t t; |
5408 | 0 | isc_time_set(&t, when, 0); |
5409 | 0 | isc_time_formattimestamp(&t, timebuf, 80); |
5410 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
5411 | 0 | "DNSKEY RRSIG(s) will expire within 7 days: %s", |
5412 | 0 | timebuf); |
5413 | 0 | delta = when - now; |
5414 | 0 | delta--; /* loop prevention */ |
5415 | 0 | delta /= 24 * 3600; /* to whole days */ |
5416 | 0 | delta *= 24 * 3600; /* to seconds */ |
5417 | 0 | isc_time_set(&zone->keywarntime, when - delta, 0); |
5418 | 0 | } else { |
5419 | 0 | isc_time_set(&zone->keywarntime, when - 7 * 24 * 3600, 0); |
5420 | 0 | isc_time_formattimestamp(&zone->keywarntime, timebuf, 80); |
5421 | 0 | dns_zone_log(zone, ISC_LOG_NOTICE, "setting keywarntime to %s", |
5422 | 0 | timebuf); |
5423 | 0 | } |
5424 | 0 | UNLOCK_ZONE(zone); |
5425 | 0 | } |
5426 | | |
5427 | | /* |
5428 | | * Helper function to del_sigs(). We don't want to delete RRSIGs that |
5429 | | * have no new key. |
5430 | | */ |
5431 | | static bool |
5432 | | delsig_ok(dns_rdata_rrsig_t *rrsig_ptr, dst_key_t **keys, unsigned int nkeys, |
5433 | 0 | bool kasp, bool *warn) { |
5434 | 0 | unsigned int i = 0; |
5435 | 0 | isc_result_t ret; |
5436 | 0 | bool have_ksk = false, have_zsk = false; |
5437 | 0 | bool have_pksk = false, have_pzsk = false; |
5438 | 0 | dst_algorithm_t algorithm; |
5439 | |
|
5440 | 0 | algorithm = dst_algorithm_fromdata( |
5441 | 0 | rrsig_ptr->algorithm, rrsig_ptr->signature, rrsig_ptr->siglen); |
5442 | |
|
5443 | 0 | for (i = 0; i < nkeys; i++) { |
5444 | 0 | bool ksk, zsk; |
5445 | |
|
5446 | 0 | if (have_pksk && have_ksk && have_pzsk && have_zsk) { |
5447 | 0 | break; |
5448 | 0 | } |
5449 | | |
5450 | 0 | if (algorithm != dst_key_alg(keys[i])) { |
5451 | 0 | continue; |
5452 | 0 | } |
5453 | | |
5454 | 0 | ret = dst_key_getbool(keys[i], DST_BOOL_KSK, &ksk); |
5455 | 0 | if (ret != ISC_R_SUCCESS) { |
5456 | 0 | ksk = KSK(keys[i]); |
5457 | 0 | } |
5458 | 0 | ret = dst_key_getbool(keys[i], DST_BOOL_ZSK, &zsk); |
5459 | 0 | if (ret != ISC_R_SUCCESS) { |
5460 | 0 | zsk = !KSK(keys[i]); |
5461 | 0 | } |
5462 | |
|
5463 | 0 | if (ksk) { |
5464 | 0 | have_ksk = true; |
5465 | 0 | if (dst_key_isprivate(keys[i])) { |
5466 | 0 | have_pksk = true; |
5467 | 0 | } |
5468 | 0 | } |
5469 | 0 | if (zsk) { |
5470 | 0 | have_zsk = true; |
5471 | 0 | if (dst_key_isprivate(keys[i])) { |
5472 | 0 | have_pzsk = true; |
5473 | 0 | } |
5474 | 0 | } |
5475 | 0 | } |
5476 | |
|
5477 | 0 | if (have_zsk && have_ksk && !have_pzsk) { |
5478 | 0 | *warn = true; |
5479 | 0 | } |
5480 | |
|
5481 | 0 | if (have_pksk && have_pzsk) { |
5482 | 0 | return true; |
5483 | 0 | } |
5484 | | |
5485 | | /* |
5486 | | * Deleting the SOA RRSIG is always okay. |
5487 | | */ |
5488 | 0 | if (rrsig_ptr->covered == dns_rdatatype_soa) { |
5489 | 0 | return true; |
5490 | 0 | } |
5491 | | |
5492 | | /* |
5493 | | * It's okay to delete a signature if there is an active key with the |
5494 | | * same algorithm to replace it, unless that violates the DNSSEC |
5495 | | * policy. |
5496 | | */ |
5497 | 0 | if (have_pksk || have_pzsk) { |
5498 | 0 | if (kasp && have_pzsk) { |
5499 | 0 | return true; |
5500 | 0 | } |
5501 | 0 | return !kasp; |
5502 | 0 | } |
5503 | | |
5504 | | /* |
5505 | | * Failing that, it is *not* okay to delete a signature |
5506 | | * if the associated public key is still in the DNSKEY RRset |
5507 | | */ |
5508 | 0 | for (i = 0; i < nkeys; i++) { |
5509 | 0 | if ((algorithm == dst_key_alg(keys[i])) && |
5510 | 0 | (rrsig_ptr->keyid == dst_key_id(keys[i]))) |
5511 | 0 | { |
5512 | 0 | return false; |
5513 | 0 | } |
5514 | 0 | } |
5515 | | |
5516 | | /* |
5517 | | * But if the key is gone, then go ahead. |
5518 | | */ |
5519 | 0 | return true; |
5520 | 0 | } |
5521 | | |
5522 | | /* |
5523 | | * Delete expired RRsigs and any RRsigs we are about to re-sign. |
5524 | | * See also update.c:del_keysigs(). |
5525 | | */ |
5526 | | static isc_result_t |
5527 | | del_sigs(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, |
5528 | | dns_rdatatype_t type, dns__zonediff_t *zonediff, dst_key_t **keys, |
5529 | 0 | unsigned int nkeys, isc_stdtime_t now, bool incremental) { |
5530 | 0 | isc_result_t result; |
5531 | 0 | dns_dbnode_t *node = NULL; |
5532 | 0 | dns_rdataset_t rdataset; |
5533 | 0 | unsigned int i; |
5534 | 0 | dns_rdata_rrsig_t rrsig; |
5535 | 0 | dns_kasp_t *kasp = zone->kasp; |
5536 | 0 | bool found; |
5537 | 0 | bool offlineksk = false; |
5538 | 0 | int64_t timewarn = 0, timemaybe = 0; |
5539 | |
|
5540 | 0 | dns_rdataset_init(&rdataset); |
5541 | |
|
5542 | 0 | if (kasp != NULL) { |
5543 | 0 | offlineksk = dns_kasp_offlineksk(kasp); |
5544 | 0 | } |
5545 | |
|
5546 | 0 | if (type == dns_rdatatype_nsec3) { |
5547 | 0 | result = dns_db_findnsec3node(db, name, false, &node); |
5548 | 0 | } else { |
5549 | 0 | result = dns_db_findnode(db, name, false, &node); |
5550 | 0 | } |
5551 | 0 | if (result == ISC_R_NOTFOUND) { |
5552 | 0 | return ISC_R_SUCCESS; |
5553 | 0 | } |
5554 | 0 | CHECK(result); |
5555 | |
|
5556 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_rrsig, type, |
5557 | 0 | (isc_stdtime_t)0, &rdataset, NULL); |
5558 | 0 | dns_db_detachnode(&node); |
5559 | |
|
5560 | 0 | if (result == ISC_R_NOTFOUND) { |
5561 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
5562 | 0 | return ISC_R_SUCCESS; |
5563 | 0 | } |
5564 | 0 | if (result != ISC_R_SUCCESS) { |
5565 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
5566 | 0 | goto cleanup; |
5567 | 0 | } |
5568 | | |
5569 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
5570 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
5571 | 0 | dst_algorithm_t algorithm; |
5572 | |
|
5573 | 0 | dns_rdataset_current(&rdataset, &rdata); |
5574 | 0 | result = dns_rdata_tostruct(&rdata, &rrsig, NULL); |
5575 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
5576 | 0 | algorithm = dst_algorithm_fromdata( |
5577 | 0 | rrsig.algorithm, rrsig.signature, rrsig.siglen); |
5578 | |
|
5579 | 0 | if (!dns_rdatatype_iskeymaterial(type)) { |
5580 | 0 | bool warn = false, deleted = false; |
5581 | 0 | if (delsig_ok(&rrsig, keys, nkeys, kasp != NULL, &warn)) |
5582 | 0 | { |
5583 | 0 | result = update_one_rr(db, ver, zonediff->diff, |
5584 | 0 | DNS_DIFFOP_DELRESIGN, |
5585 | 0 | name, rdataset.ttl, |
5586 | 0 | &rdata); |
5587 | 0 | if (result != ISC_R_SUCCESS) { |
5588 | 0 | break; |
5589 | 0 | } |
5590 | 0 | deleted = true; |
5591 | 0 | } |
5592 | 0 | if (warn && !deleted) { |
5593 | | /* |
5594 | | * At this point, we've got an RRSIG, |
5595 | | * which is signed by an inactive key. |
5596 | | * An administrator needs to provide a new |
5597 | | * key/alg, but until that time, we want to |
5598 | | * keep the old RRSIG. Marking the key as |
5599 | | * offline will prevent us spinning waiting |
5600 | | * for the private part. |
5601 | | */ |
5602 | 0 | if (incremental) { |
5603 | 0 | result = offline(db, ver, zonediff, |
5604 | 0 | name, rdataset.ttl, |
5605 | 0 | &rdata); |
5606 | 0 | if (result != ISC_R_SUCCESS) { |
5607 | 0 | break; |
5608 | 0 | } |
5609 | 0 | } |
5610 | | |
5611 | | /* |
5612 | | * Log the key id and algorithm of |
5613 | | * the inactive key with no replacement |
5614 | | */ |
5615 | 0 | if (zone->log_key_expired_timer <= now) { |
5616 | 0 | char origin[DNS_NAME_FORMATSIZE]; |
5617 | 0 | char algbuf[DNS_NAME_FORMATSIZE]; |
5618 | 0 | dns_name_format(&zone->origin, origin, |
5619 | 0 | sizeof(origin)); |
5620 | 0 | dst_algorithm_format(algorithm, algbuf, |
5621 | 0 | sizeof(algbuf)); |
5622 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
5623 | 0 | "Key %s/%s/%d " |
5624 | 0 | "missing or inactive " |
5625 | 0 | "and has no replacement: " |
5626 | 0 | "retaining signatures.", |
5627 | 0 | origin, algbuf, |
5628 | 0 | rrsig.keyid); |
5629 | 0 | zone->log_key_expired_timer = now + |
5630 | 0 | 3600; |
5631 | 0 | } |
5632 | 0 | } |
5633 | 0 | continue; |
5634 | 0 | } |
5635 | | |
5636 | | /* |
5637 | | * KSK RRSIGs requires special processing. |
5638 | | */ |
5639 | 0 | found = false; |
5640 | 0 | for (i = 0; i < nkeys; i++) { |
5641 | 0 | if (algorithm == dst_key_alg(keys[i]) && |
5642 | 0 | rrsig.keyid == dst_key_id(keys[i])) |
5643 | 0 | { |
5644 | 0 | found = true; |
5645 | | /* |
5646 | | * Mark offline DNSKEY. |
5647 | | * We want the earliest offline expire time |
5648 | | * iff there is a new offline signature. |
5649 | | */ |
5650 | 0 | if (!dst_key_inactive(keys[i]) && |
5651 | 0 | !dst_key_isprivate(keys[i]) && !offlineksk) |
5652 | 0 | { |
5653 | 0 | int64_t timeexpire = dns_time64_from32( |
5654 | 0 | rrsig.timeexpire); |
5655 | 0 | if (timewarn != 0 && |
5656 | 0 | timewarn > timeexpire) |
5657 | 0 | { |
5658 | 0 | timewarn = timeexpire; |
5659 | 0 | } |
5660 | 0 | if (rdata.flags & DNS_RDATA_OFFLINE) { |
5661 | 0 | if (timemaybe == 0 || |
5662 | 0 | timemaybe > timeexpire) |
5663 | 0 | { |
5664 | 0 | timemaybe = timeexpire; |
5665 | 0 | } |
5666 | 0 | break; |
5667 | 0 | } |
5668 | 0 | if (timewarn == 0) { |
5669 | 0 | timewarn = timemaybe; |
5670 | 0 | } |
5671 | 0 | if (timewarn == 0 || |
5672 | 0 | timewarn > timeexpire) |
5673 | 0 | { |
5674 | 0 | timewarn = timeexpire; |
5675 | 0 | } |
5676 | 0 | result = offline(db, ver, zonediff, |
5677 | 0 | name, rdataset.ttl, |
5678 | 0 | &rdata); |
5679 | 0 | break; |
5680 | 0 | } |
5681 | 0 | result = update_one_rr(db, ver, zonediff->diff, |
5682 | 0 | DNS_DIFFOP_DELRESIGN, |
5683 | 0 | name, rdataset.ttl, |
5684 | 0 | &rdata); |
5685 | 0 | break; |
5686 | 0 | } |
5687 | 0 | } |
5688 | | |
5689 | | /* |
5690 | | * If there is not a matching DNSKEY then |
5691 | | * delete the RRSIG. |
5692 | | */ |
5693 | 0 | if (!found) { |
5694 | 0 | result = update_one_rr(db, ver, zonediff->diff, |
5695 | 0 | DNS_DIFFOP_DELRESIGN, name, |
5696 | 0 | rdataset.ttl, &rdata); |
5697 | 0 | } |
5698 | 0 | if (result != ISC_R_SUCCESS) { |
5699 | 0 | break; |
5700 | 0 | } |
5701 | 0 | } |
5702 | |
|
5703 | 0 | dns_rdataset_disassociate(&rdataset); |
5704 | 0 | if (timewarn > 0) { |
5705 | 0 | isc_stdtime_t stdwarn = (isc_stdtime_t)timewarn; |
5706 | 0 | if (timewarn == stdwarn) { |
5707 | 0 | set_key_expiry_warning(zone, (isc_stdtime_t)timewarn, |
5708 | 0 | now); |
5709 | 0 | } else { |
5710 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
5711 | 0 | "key expiry warning time out of range"); |
5712 | 0 | } |
5713 | 0 | } |
5714 | 0 | cleanup: |
5715 | 0 | if (node != NULL) { |
5716 | 0 | dns_db_detachnode(&node); |
5717 | 0 | } |
5718 | 0 | return result; |
5719 | 0 | } |
5720 | | |
5721 | | static isc_result_t |
5722 | | add_sigs(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, dns_zone_t *zone, |
5723 | | dns_rdatatype_t type, dns_diff_t *diff, dst_key_t **keys, |
5724 | | unsigned int nkeys, isc_mem_t *mctx, isc_stdtime_t now, |
5725 | 0 | isc_stdtime_t inception, isc_stdtime_t expire) { |
5726 | 0 | isc_result_t result; |
5727 | 0 | dns_dbnode_t *node = NULL; |
5728 | 0 | dns_stats_t *dnssecsignstats; |
5729 | 0 | dns_rdataset_t rdataset; |
5730 | 0 | dns_rdata_t sig_rdata = DNS_RDATA_INIT; |
5731 | 0 | unsigned char data[DNS_RDATA_MAXLENGTH]; |
5732 | 0 | isc_buffer_t buffer; |
5733 | 0 | unsigned int i; |
5734 | 0 | bool use_kasp = false; |
5735 | 0 | bool offlineksk = false; |
5736 | |
|
5737 | 0 | if (zone->kasp != NULL) { |
5738 | 0 | use_kasp = true; |
5739 | 0 | offlineksk = dns_kasp_offlineksk(zone->kasp); |
5740 | 0 | } |
5741 | |
|
5742 | 0 | dns_rdataset_init(&rdataset); |
5743 | 0 | isc_buffer_init(&buffer, data, sizeof(data)); |
5744 | |
|
5745 | 0 | if (type == dns_rdatatype_nsec3) { |
5746 | 0 | result = dns_db_findnsec3node(db, name, false, &node); |
5747 | 0 | } else { |
5748 | 0 | result = dns_db_findnode(db, name, false, &node); |
5749 | 0 | } |
5750 | 0 | if (result == ISC_R_NOTFOUND) { |
5751 | 0 | return ISC_R_SUCCESS; |
5752 | 0 | } |
5753 | 0 | CHECK(result); |
5754 | |
|
5755 | 0 | result = dns_db_findrdataset(db, node, ver, type, 0, (isc_stdtime_t)0, |
5756 | 0 | &rdataset, NULL); |
5757 | 0 | dns_db_detachnode(&node); |
5758 | 0 | if (result == ISC_R_NOTFOUND) { |
5759 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
5760 | 0 | return ISC_R_SUCCESS; |
5761 | 0 | } |
5762 | 0 | if (result != ISC_R_SUCCESS) { |
5763 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
5764 | 0 | goto cleanup; |
5765 | 0 | } |
5766 | | |
5767 | 0 | for (i = 0; i < nkeys; i++) { |
5768 | | /* Don't add signatures for offline or inactive keys */ |
5769 | 0 | if (!dst_key_isprivate(keys[i]) && !offlineksk) { |
5770 | 0 | continue; |
5771 | 0 | } |
5772 | 0 | if (dst_key_inactive(keys[i]) && !offlineksk) { |
5773 | 0 | continue; |
5774 | 0 | } |
5775 | | |
5776 | 0 | if (use_kasp) { |
5777 | | /* |
5778 | | * A dnssec-policy is found. Check what RRsets this |
5779 | | * key should sign. |
5780 | | */ |
5781 | 0 | isc_result_t kresult; |
5782 | 0 | isc_stdtime_t when; |
5783 | 0 | bool ksk = false; |
5784 | 0 | bool zsk = false; |
5785 | 0 | bool have_zsk = false; |
5786 | |
|
5787 | 0 | kresult = dst_key_getbool(keys[i], DST_BOOL_KSK, &ksk); |
5788 | 0 | if (kresult != ISC_R_SUCCESS) { |
5789 | 0 | if (KSK(keys[i])) { |
5790 | 0 | ksk = true; |
5791 | 0 | } |
5792 | 0 | } |
5793 | 0 | kresult = dst_key_getbool(keys[i], DST_BOOL_ZSK, &zsk); |
5794 | 0 | if (kresult != ISC_R_SUCCESS) { |
5795 | 0 | if (!KSK(keys[i])) { |
5796 | 0 | zsk = true; |
5797 | 0 | } |
5798 | 0 | } |
5799 | | |
5800 | | /* |
5801 | | * Don't consider inactive keys or offline keys. |
5802 | | */ |
5803 | 0 | if (!dst_key_isprivate(keys[i]) && offlineksk && zsk) { |
5804 | 0 | continue; |
5805 | 0 | } |
5806 | 0 | if (dst_key_inactive(keys[i]) && offlineksk && zsk) { |
5807 | 0 | continue; |
5808 | 0 | } |
5809 | | |
5810 | 0 | if (offlineksk) { |
5811 | 0 | have_zsk = true; |
5812 | 0 | } else { |
5813 | 0 | (void)dst_key_have_ksk_and_zsk(keys, nkeys, i, |
5814 | 0 | true, ksk, zsk, |
5815 | 0 | NULL, &have_zsk); |
5816 | 0 | } |
5817 | |
|
5818 | 0 | if (dns_rdatatype_iskeymaterial(type)) { |
5819 | | /* |
5820 | | * DNSKEY RRset is signed with KSK. |
5821 | | * CDS and CDNSKEY RRsets too (RFC 7344, 4.1). |
5822 | | */ |
5823 | 0 | if (!ksk) { |
5824 | 0 | continue; |
5825 | 0 | } |
5826 | 0 | } else if (!zsk) { |
5827 | | /* |
5828 | | * Other RRsets are signed with ZSK. |
5829 | | */ |
5830 | 0 | if (type != dns_rdatatype_soa && |
5831 | 0 | type != zone->privatetype) |
5832 | 0 | { |
5833 | 0 | continue; |
5834 | 0 | } |
5835 | 0 | if (have_zsk) { |
5836 | 0 | continue; |
5837 | 0 | } |
5838 | 0 | } else if (!dst_key_is_signing(keys[i], DST_BOOL_ZSK, |
5839 | 0 | now, &when)) |
5840 | 0 | { |
5841 | | /* |
5842 | | * This key is not active for zone-signing. |
5843 | | */ |
5844 | 0 | continue; |
5845 | 0 | } |
5846 | 0 | } else if (!REVOKE(keys[i])) { |
5847 | | /* |
5848 | | * Don't consider inactive keys, however the KSK may be |
5849 | | * temporary offline, so do consider keys which private |
5850 | | * key files are unavailable. |
5851 | | */ |
5852 | 0 | bool both = dst_key_have_ksk_and_zsk( |
5853 | 0 | keys, nkeys, i, false, KSK(keys[i]), |
5854 | 0 | !KSK(keys[i]), NULL, NULL); |
5855 | 0 | if (both) { |
5856 | | /* |
5857 | | * CDS and CDNSKEY are signed with KSK (RFC |
5858 | | * 7344, 4.1). |
5859 | | */ |
5860 | 0 | if (dns_rdatatype_iskeymaterial(type)) { |
5861 | 0 | if (!KSK(keys[i])) { |
5862 | 0 | continue; |
5863 | 0 | } |
5864 | 0 | } else if (KSK(keys[i])) { |
5865 | 0 | continue; |
5866 | 0 | } |
5867 | 0 | } |
5868 | 0 | } |
5869 | | |
5870 | | /* |
5871 | | * If this key is revoked, it may only sign the DNSKEY RRset. |
5872 | | */ |
5873 | 0 | if (REVOKE(keys[i]) && type != dns_rdatatype_dnskey) { |
5874 | 0 | continue; |
5875 | 0 | } |
5876 | | |
5877 | | /* Calculate the signature, creating a RRSIG RDATA. */ |
5878 | 0 | isc_buffer_clear(&buffer); |
5879 | |
|
5880 | 0 | if (offlineksk && dns_rdatatype_iskeymaterial(type)) { |
5881 | | /* Look up the signature in the SKR bundle */ |
5882 | 0 | dns_skrbundle_t *bundle = dns_zone_getskrbundle(zone); |
5883 | 0 | if (bundle == NULL) { |
5884 | 0 | CLEANUP(DNS_R_NOSKRBUNDLE); |
5885 | 0 | } |
5886 | 0 | CHECK(dns_skrbundle_getsig(bundle, keys[i], type, |
5887 | 0 | &sig_rdata)); |
5888 | 0 | } else { |
5889 | 0 | CHECK(dns_dnssec_sign(name, &rdataset, keys[i], |
5890 | 0 | &inception, &expire, mctx, |
5891 | 0 | &buffer, &sig_rdata)); |
5892 | 0 | } |
5893 | | |
5894 | | /* Update the database and journal with the RRSIG. */ |
5895 | | /* XXX inefficient - will cause dataset merging */ |
5896 | 0 | CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADDRESIGN, name, |
5897 | 0 | rdataset.ttl, &sig_rdata)); |
5898 | 0 | dns_rdata_reset(&sig_rdata); |
5899 | 0 | isc_buffer_init(&buffer, data, sizeof(data)); |
5900 | | |
5901 | | /* Update DNSSEC sign statistics. */ |
5902 | 0 | dnssecsignstats = dns_zone_getdnssecsignstats(zone); |
5903 | 0 | if (dnssecsignstats != NULL) { |
5904 | | /* Generated a new signature. */ |
5905 | 0 | dns_dnssecsignstats_increment(dnssecsignstats, |
5906 | 0 | ID(keys[i]), |
5907 | 0 | (uint8_t)ALG(keys[i]), |
5908 | 0 | dns_dnssecsignstats_sign); |
5909 | | /* This is a refresh. */ |
5910 | 0 | dns_dnssecsignstats_increment( |
5911 | 0 | dnssecsignstats, ID(keys[i]), |
5912 | 0 | (uint8_t)ALG(keys[i]), |
5913 | 0 | dns_dnssecsignstats_refresh); |
5914 | 0 | } |
5915 | 0 | } |
5916 | | |
5917 | 0 | cleanup: |
5918 | 0 | dns_rdataset_cleanup(&rdataset); |
5919 | 0 | if (node != NULL) { |
5920 | 0 | dns_db_detachnode(&node); |
5921 | 0 | } |
5922 | 0 | return result; |
5923 | 0 | } |
5924 | | |
5925 | | static void |
5926 | | calculate_rrsig_validity(dns_zone_t *zone, isc_stdtime_t now, |
5927 | | isc_stdtime_t *inception, isc_stdtime_t *soaexpire, |
5928 | 0 | isc_stdtime_t *expire, isc_stdtime_t *fullexpire) { |
5929 | 0 | REQUIRE(inception != NULL); |
5930 | 0 | REQUIRE(soaexpire != NULL); |
5931 | | /* expire and fullexpire are optional */ |
5932 | |
|
5933 | 0 | isc_stdtime_t jitter = DEFAULT_JITTER; |
5934 | 0 | isc_stdtime_t sigvalidity = dns_zone_getsigvalidityinterval(zone); |
5935 | 0 | isc_stdtime_t shortjitter = 0, fulljitter = 0; |
5936 | |
|
5937 | 0 | if (zone->kasp != NULL) { |
5938 | 0 | jitter = dns_kasp_sigjitter(zone->kasp); |
5939 | 0 | sigvalidity = dns_kasp_sigvalidity(zone->kasp); |
5940 | 0 | INSIST(jitter <= sigvalidity); |
5941 | 0 | } |
5942 | |
|
5943 | 0 | if (jitter > sigvalidity) { |
5944 | 0 | jitter = sigvalidity; |
5945 | 0 | } |
5946 | |
|
5947 | 0 | *inception = now - 3600; /* Allow for clock skew. */ |
5948 | 0 | *soaexpire = now + sigvalidity; |
5949 | | |
5950 | | /* |
5951 | | * Spread out signatures over time if they happen to be |
5952 | | * clumped. We don't do this for each add_sigs() call as |
5953 | | * we still want some clustering to occur. In normal operations |
5954 | | * the records should be re-signed as they fall due and they should |
5955 | | * already be spread out. However if the server is off for a |
5956 | | * period we need to ensure that the clusters don't become |
5957 | | * synchronised by using the full jitter range. |
5958 | | */ |
5959 | 0 | if (sigvalidity >= 3600U) { |
5960 | 0 | if (sigvalidity > 7200U) { |
5961 | 0 | shortjitter = isc_random_uniform(3600); |
5962 | 0 | fulljitter = isc_random_uniform(jitter); |
5963 | 0 | } else { |
5964 | 0 | shortjitter = fulljitter = isc_random_uniform(1200); |
5965 | 0 | } |
5966 | 0 | } |
5967 | |
|
5968 | 0 | SET_IF_NOT_NULL(expire, *soaexpire - shortjitter - 1); |
5969 | 0 | SET_IF_NOT_NULL(fullexpire, *soaexpire - fulljitter - 1); |
5970 | 0 | } |
5971 | | |
5972 | | static void |
5973 | 0 | zone_resigninc(dns_zone_t *zone) { |
5974 | 0 | dns_db_t *db = NULL; |
5975 | 0 | dns_dbversion_t *version = NULL; |
5976 | 0 | dns_diff_t _sig_diff; |
5977 | 0 | dns__zonediff_t zonediff; |
5978 | 0 | dns_fixedname_t fixed; |
5979 | 0 | dns_name_t *name; |
5980 | 0 | dns_typepair_t typepair; |
5981 | 0 | dst_key_t *zone_keys[DNS_MAXZONEKEYS]; |
5982 | 0 | isc_result_t result; |
5983 | 0 | isc_stdtime_t now, inception, soaexpire, expire, fullexpire, stop; |
5984 | 0 | unsigned int i; |
5985 | 0 | unsigned int nkeys = 0; |
5986 | 0 | isc_stdtime_t resign; |
5987 | |
|
5988 | 0 | ENTER; |
5989 | |
|
5990 | 0 | dns_diff_init(zone->mctx, &_sig_diff); |
5991 | 0 | zonediff_init(&zonediff, &_sig_diff); |
5992 | | |
5993 | | /* |
5994 | | * Zone is frozen. Pause for 5 minutes. |
5995 | | */ |
5996 | 0 | if (zone->update_disabled) { |
5997 | 0 | CLEANUP(ISC_R_FAILURE); |
5998 | 0 | } |
5999 | | |
6000 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
6001 | 0 | if (zone->db != NULL) { |
6002 | 0 | dns_db_attach(zone->db, &db); |
6003 | 0 | } |
6004 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
6005 | 0 | if (db == NULL) { |
6006 | 0 | CLEANUP(ISC_R_FAILURE); |
6007 | 0 | } |
6008 | | |
6009 | 0 | result = dns_db_newversion(db, &version); |
6010 | 0 | if (result != ISC_R_SUCCESS) { |
6011 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
6012 | 0 | "zone_resigninc:dns_db_newversion -> %s", |
6013 | 0 | isc_result_totext(result)); |
6014 | 0 | goto cleanup; |
6015 | 0 | } |
6016 | | |
6017 | 0 | now = isc_stdtime_now(); |
6018 | |
|
6019 | 0 | result = dns_zone_findkeys(zone, db, version, now, zone->mctx, |
6020 | 0 | DNS_MAXZONEKEYS, zone_keys, &nkeys); |
6021 | 0 | if (result != ISC_R_SUCCESS) { |
6022 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
6023 | 0 | "zone_resigninc:dns_zone_findkeys -> %s", |
6024 | 0 | isc_result_totext(result)); |
6025 | 0 | goto cleanup; |
6026 | 0 | } |
6027 | | |
6028 | 0 | calculate_rrsig_validity(zone, now, &inception, &soaexpire, &expire, |
6029 | 0 | &fullexpire); |
6030 | |
|
6031 | 0 | stop = now + 5; |
6032 | |
|
6033 | 0 | name = dns_fixedname_initname(&fixed); |
6034 | 0 | result = dns_db_getsigningtime(db, &resign, name, &typepair); |
6035 | 0 | if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) { |
6036 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
6037 | 0 | "zone_resigninc:dns_db_getsigningtime -> %s", |
6038 | 0 | isc_result_totext(result)); |
6039 | 0 | } |
6040 | |
|
6041 | 0 | i = 0; |
6042 | 0 | while (result == ISC_R_SUCCESS) { |
6043 | 0 | dns_rdatatype_t covers = DNS_TYPEPAIR_COVERS(typepair); |
6044 | |
|
6045 | 0 | resign -= dns_zone_getsigresigninginterval(zone); |
6046 | | |
6047 | | /* |
6048 | | * Stop if we hit the SOA as that means we have walked the |
6049 | | * entire zone. The SOA record should always be the most |
6050 | | * recent signature. |
6051 | | */ |
6052 | | /* XXXMPA increase number of RRsets signed pre call */ |
6053 | 0 | if ((covers == dns_rdatatype_soa && |
6054 | 0 | dns_name_equal(name, &zone->origin)) || |
6055 | 0 | i++ > zone->signatures || resign > stop) |
6056 | 0 | { |
6057 | 0 | break; |
6058 | 0 | } |
6059 | | |
6060 | 0 | result = del_sigs(zone, db, version, name, covers, &zonediff, |
6061 | 0 | zone_keys, nkeys, now, true); |
6062 | 0 | if (result != ISC_R_SUCCESS) { |
6063 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
6064 | 0 | "zone_resigninc:del_sigs -> %s", |
6065 | 0 | isc_result_totext(result)); |
6066 | 0 | break; |
6067 | 0 | } |
6068 | | |
6069 | | /* |
6070 | | * If re-signing is over 5 minutes late use 'fullexpire' |
6071 | | * to redistribute the signature over the complete |
6072 | | * re-signing window, otherwise only add a small amount |
6073 | | * of jitter. |
6074 | | */ |
6075 | 0 | result = add_sigs(db, version, name, zone, covers, |
6076 | 0 | zonediff.diff, zone_keys, nkeys, zone->mctx, |
6077 | 0 | now, inception, |
6078 | 0 | resign > (now - 300) ? expire : fullexpire); |
6079 | 0 | if (result != ISC_R_SUCCESS) { |
6080 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
6081 | 0 | "zone_resigninc:add_sigs -> %s", |
6082 | 0 | isc_result_totext(result)); |
6083 | 0 | break; |
6084 | 0 | } |
6085 | 0 | result = dns_db_getsigningtime(db, &resign, name, &typepair); |
6086 | 0 | if (nkeys == 0 && result == ISC_R_NOTFOUND) { |
6087 | 0 | result = ISC_R_SUCCESS; |
6088 | 0 | break; |
6089 | 0 | } |
6090 | 0 | if (result != ISC_R_SUCCESS) { |
6091 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
6092 | 0 | "zone_resigninc:dns_db_getsigningtime -> " |
6093 | 0 | "%s", |
6094 | 0 | isc_result_totext(result)); |
6095 | 0 | } |
6096 | 0 | } |
6097 | |
|
6098 | 0 | if (result != ISC_R_NOMORE) { |
6099 | 0 | CHECK(result); |
6100 | 0 | } |
6101 | | |
6102 | 0 | result = del_sigs(zone, db, version, &zone->origin, dns_rdatatype_soa, |
6103 | 0 | &zonediff, zone_keys, nkeys, now, true); |
6104 | 0 | if (result != ISC_R_SUCCESS) { |
6105 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
6106 | 0 | "zone_resigninc:del_sigs -> %s", |
6107 | 0 | isc_result_totext(result)); |
6108 | 0 | goto cleanup; |
6109 | 0 | } |
6110 | | |
6111 | | /* |
6112 | | * Did we change anything in the zone? |
6113 | | */ |
6114 | 0 | if (ISC_LIST_EMPTY(zonediff.diff->tuples)) { |
6115 | | /* |
6116 | | * Commit the changes if any key has been marked as offline. |
6117 | | */ |
6118 | 0 | if (zonediff.offline) { |
6119 | 0 | dns_db_closeversion(db, &version, true); |
6120 | 0 | } |
6121 | 0 | goto cleanup; |
6122 | 0 | } |
6123 | | |
6124 | | /* Increment SOA serial if we have made changes */ |
6125 | 0 | result = update_soa_serial(zone, db, version, zonediff.diff, zone->mctx, |
6126 | 0 | zone->updatemethod); |
6127 | 0 | if (result != ISC_R_SUCCESS) { |
6128 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
6129 | 0 | "zone_resigninc:update_soa_serial -> %s", |
6130 | 0 | isc_result_totext(result)); |
6131 | 0 | goto cleanup; |
6132 | 0 | } |
6133 | | |
6134 | | /* |
6135 | | * Generate maximum life time signatures so that the above loop |
6136 | | * termination is sensible. |
6137 | | */ |
6138 | 0 | result = add_sigs(db, version, &zone->origin, zone, dns_rdatatype_soa, |
6139 | 0 | zonediff.diff, zone_keys, nkeys, zone->mctx, now, |
6140 | 0 | inception, soaexpire); |
6141 | 0 | if (result != ISC_R_SUCCESS) { |
6142 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
6143 | 0 | "zone_resigninc:add_sigs -> %s", |
6144 | 0 | isc_result_totext(result)); |
6145 | 0 | goto cleanup; |
6146 | 0 | } |
6147 | | |
6148 | | /* Write changes to journal file. */ |
6149 | 0 | CHECK(zone_journal(zone, zonediff.diff, NULL, "zone_resigninc")); |
6150 | | |
6151 | | /* Everything has succeeded. Commit the changes. */ |
6152 | 0 | dns_db_closeversion(db, &version, true); |
6153 | |
|
6154 | 0 | cleanup: |
6155 | 0 | dns_diff_clear(&_sig_diff); |
6156 | 0 | for (i = 0; i < nkeys; i++) { |
6157 | 0 | dst_key_free(&zone_keys[i]); |
6158 | 0 | } |
6159 | 0 | if (version != NULL) { |
6160 | 0 | dns_db_closeversion(db, &version, false); |
6161 | 0 | dns_db_detach(&db); |
6162 | 0 | } else if (db != NULL) { |
6163 | 0 | dns_db_detach(&db); |
6164 | 0 | } |
6165 | |
|
6166 | 0 | LOCK_ZONE(zone); |
6167 | 0 | if (result == ISC_R_SUCCESS) { |
6168 | 0 | dns__zone_set_resigntime(zone); |
6169 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
6170 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY); |
6171 | 0 | } else { |
6172 | | /* |
6173 | | * Something failed. Retry in 5 minutes. |
6174 | | */ |
6175 | 0 | isc_interval_t ival; |
6176 | 0 | isc_interval_set(&ival, 300, 0); |
6177 | 0 | isc_time_nowplusinterval(&zone->resigntime, &ival); |
6178 | 0 | } |
6179 | 0 | UNLOCK_ZONE(zone); |
6180 | |
|
6181 | 0 | INSIST(version == NULL); |
6182 | 0 | } |
6183 | | |
6184 | | static isc_result_t |
6185 | | next_active(dns_db_t *db, dns_dbversion_t *version, dns_name_t *oldname, |
6186 | 0 | dns_name_t *newname, bool bottom) { |
6187 | 0 | isc_result_t result; |
6188 | 0 | dns_dbiterator_t *dbit = NULL; |
6189 | 0 | dns_rdatasetiter_t *rdsit = NULL; |
6190 | 0 | dns_dbnode_t *node = NULL; |
6191 | |
|
6192 | 0 | CHECK(dns_db_createiterator(db, DNS_DB_NONSEC3, &dbit)); |
6193 | 0 | CHECK(dns_dbiterator_seek(dbit, oldname)); |
6194 | 0 | do { |
6195 | 0 | result = dns_dbiterator_next(dbit); |
6196 | 0 | if (result == ISC_R_NOMORE) { |
6197 | 0 | CHECK(dns_dbiterator_first(dbit)); |
6198 | 0 | } |
6199 | 0 | CHECK(dns_dbiterator_current(dbit, &node, newname)); |
6200 | 0 | if (bottom && dns_name_issubdomain(newname, oldname) && |
6201 | 0 | !dns_name_equal(newname, oldname)) |
6202 | 0 | { |
6203 | 0 | dns_db_detachnode(&node); |
6204 | 0 | continue; |
6205 | 0 | } |
6206 | | /* |
6207 | | * Is this node empty? |
6208 | | */ |
6209 | 0 | CHECK(dns_db_allrdatasets(db, node, version, 0, 0, &rdsit)); |
6210 | 0 | result = dns_rdatasetiter_first(rdsit); |
6211 | 0 | dns_db_detachnode(&node); |
6212 | 0 | dns_rdatasetiter_destroy(&rdsit); |
6213 | 0 | if (result != ISC_R_NOMORE) { |
6214 | 0 | break; |
6215 | 0 | } |
6216 | 0 | } while (1); |
6217 | 0 | cleanup: |
6218 | 0 | if (node != NULL) { |
6219 | 0 | dns_db_detachnode(&node); |
6220 | 0 | } |
6221 | 0 | if (dbit != NULL) { |
6222 | 0 | dns_dbiterator_destroy(&dbit); |
6223 | 0 | } |
6224 | 0 | return result; |
6225 | 0 | } |
6226 | | |
6227 | | static bool |
6228 | | signed_with_good_key(dns_zone_t *zone, dns_db_t *db, dns_dbnode_t *node, |
6229 | | dns_dbversion_t *version, dns_rdatatype_t type, |
6230 | 0 | dst_key_t *key, bool fullsign) { |
6231 | 0 | isc_result_t result; |
6232 | 0 | dns_rdataset_t rdataset; |
6233 | 0 | dns_rdata_rrsig_t rrsig; |
6234 | 0 | int count = 0; |
6235 | 0 | dns_kasp_t *kasp = zone->kasp; |
6236 | |
|
6237 | 0 | dns_rdataset_init(&rdataset); |
6238 | 0 | result = dns_db_findrdataset(db, node, version, dns_rdatatype_rrsig, |
6239 | 0 | type, 0, &rdataset, NULL); |
6240 | 0 | if (result != ISC_R_SUCCESS) { |
6241 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
6242 | 0 | return false; |
6243 | 0 | } |
6244 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
6245 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
6246 | 0 | dns_rdataset_current(&rdataset, &rdata); |
6247 | 0 | result = dns_rdata_tostruct(&rdata, &rrsig, NULL); |
6248 | 0 | INSIST(result == ISC_R_SUCCESS); |
6249 | 0 | dst_algorithm_t algorithm; |
6250 | 0 | algorithm = dst_algorithm_fromdata( |
6251 | 0 | rrsig.algorithm, rrsig.signature, rrsig.siglen); |
6252 | 0 | if (algorithm == dst_key_alg(key) && |
6253 | 0 | rrsig.keyid == dst_key_id(key)) |
6254 | 0 | { |
6255 | 0 | dns_rdataset_disassociate(&rdataset); |
6256 | 0 | return true; |
6257 | 0 | } |
6258 | 0 | if (algorithm == dst_key_alg(key)) { |
6259 | 0 | count++; |
6260 | 0 | } |
6261 | 0 | } |
6262 | | |
6263 | 0 | if (zone->kasp != NULL && !fullsign) { |
6264 | 0 | int zsk_count = 0; |
6265 | 0 | bool approved; |
6266 | |
|
6267 | 0 | KASP_LOCK(kasp); |
6268 | 0 | ISC_LIST_FOREACH(dns_kasp_keys(kasp), kkey, link) { |
6269 | 0 | if (dns_kasp_key_algorithm(kkey) != dst_key_alg(key)) { |
6270 | 0 | continue; |
6271 | 0 | } |
6272 | 0 | if (dns_kasp_key_zsk(kkey)) { |
6273 | 0 | zsk_count++; |
6274 | 0 | } |
6275 | 0 | } |
6276 | 0 | KASP_UNLOCK(kasp); |
6277 | |
|
6278 | 0 | if (dns_rdatatype_iskeymaterial(type)) { |
6279 | | /* |
6280 | | * CDS and CDNSKEY are signed with KSK like DNSKEY. |
6281 | | * (RFC 7344, section 4.1 specifies that they must |
6282 | | * be signed with a key in the current DS RRset, |
6283 | | * which would only include KSK's.) |
6284 | | */ |
6285 | 0 | approved = false; |
6286 | 0 | } else { |
6287 | 0 | approved = (zsk_count == count); |
6288 | 0 | } |
6289 | |
|
6290 | 0 | dns_rdataset_disassociate(&rdataset); |
6291 | 0 | return approved; |
6292 | 0 | } |
6293 | | |
6294 | 0 | dns_rdataset_disassociate(&rdataset); |
6295 | 0 | return false; |
6296 | 0 | } |
6297 | | |
6298 | | static isc_result_t |
6299 | | add_nsec(dns_db_t *db, dns_dbversion_t *version, dns_name_t *name, |
6300 | 0 | dns_dbnode_t *node, dns_ttl_t ttl, bool bottom, dns_diff_t *diff) { |
6301 | 0 | dns_fixedname_t fixed; |
6302 | 0 | dns_name_t *next; |
6303 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
6304 | 0 | isc_result_t result; |
6305 | 0 | unsigned char nsecbuffer[DNS_NSEC_BUFFERSIZE]; |
6306 | |
|
6307 | 0 | next = dns_fixedname_initname(&fixed); |
6308 | |
|
6309 | 0 | CHECK(next_active(db, version, name, next, bottom)); |
6310 | 0 | CHECK(dns_nsec_buildrdata(db, version, node, next, nsecbuffer, &rdata)); |
6311 | 0 | CHECK(update_one_rr(db, version, diff, DNS_DIFFOP_ADD, name, ttl, |
6312 | 0 | &rdata)); |
6313 | 0 | cleanup: |
6314 | 0 | return result; |
6315 | 0 | } |
6316 | | |
6317 | | static isc_result_t |
6318 | | check_if_bottom_of_zone(dns_db_t *db, dns_dbnode_t *node, |
6319 | 0 | dns_dbversion_t *version, bool *is_bottom_of_zone) { |
6320 | 0 | isc_result_t result; |
6321 | 0 | dns_rdatasetiter_t *iterator = NULL; |
6322 | 0 | bool seen_soa = false, seen_ns = false, seen_dname = false; |
6323 | |
|
6324 | 0 | REQUIRE(is_bottom_of_zone != NULL); |
6325 | |
|
6326 | 0 | result = dns_db_allrdatasets(db, node, version, 0, 0, &iterator); |
6327 | 0 | if (result != ISC_R_SUCCESS) { |
6328 | 0 | if (result == ISC_R_NOTFOUND) { |
6329 | 0 | result = ISC_R_SUCCESS; |
6330 | 0 | } |
6331 | 0 | return result; |
6332 | 0 | } |
6333 | | |
6334 | 0 | DNS_RDATASETITER_FOREACH(iterator) { |
6335 | 0 | dns_rdataset_t rdataset = DNS_RDATASET_INIT; |
6336 | 0 | dns_rdatasetiter_current(iterator, &rdataset); |
6337 | 0 | switch (rdataset.type) { |
6338 | 0 | case dns_rdatatype_soa: |
6339 | 0 | seen_soa = true; |
6340 | 0 | break; |
6341 | 0 | case dns_rdatatype_ns: |
6342 | 0 | seen_ns = true; |
6343 | 0 | break; |
6344 | 0 | case dns_rdatatype_dname: |
6345 | 0 | seen_dname = true; |
6346 | 0 | break; |
6347 | 0 | } |
6348 | 0 | dns_rdataset_disassociate(&rdataset); |
6349 | 0 | } |
6350 | | |
6351 | 0 | if ((seen_ns && !seen_soa) || seen_dname) { |
6352 | 0 | *is_bottom_of_zone = true; |
6353 | 0 | } |
6354 | |
|
6355 | 0 | dns_rdatasetiter_destroy(&iterator); |
6356 | 0 | return ISC_R_SUCCESS; |
6357 | 0 | } |
6358 | | |
6359 | | typedef struct seen { |
6360 | | bool rr; |
6361 | | bool soa; |
6362 | | bool ns; |
6363 | | bool nsec; |
6364 | | bool nsec3; |
6365 | | bool ds; |
6366 | | bool dname; |
6367 | | } seen_t; |
6368 | | |
6369 | | static isc_result_t |
6370 | | allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version, |
6371 | 0 | dns_rdatasetiter_t **iterp, seen_t *seen) { |
6372 | 0 | dns_rdataset_t rdataset = DNS_RDATASET_INIT; |
6373 | |
|
6374 | 0 | *seen = (seen_t){}; |
6375 | |
|
6376 | 0 | RETERR(dns_db_allrdatasets(db, node, version, 0, 0, iterp)); |
6377 | |
|
6378 | 0 | DNS_RDATASETITER_FOREACH(*iterp) { |
6379 | 0 | dns_rdatasetiter_current(*iterp, &rdataset); |
6380 | |
|
6381 | 0 | if (rdataset.type == dns_rdatatype_rrsig) { |
6382 | 0 | dns_rdataset_disassociate(&rdataset); |
6383 | 0 | continue; |
6384 | 0 | } |
6385 | | |
6386 | 0 | (*seen).rr = true; |
6387 | |
|
6388 | 0 | if (rdataset.type == dns_rdatatype_soa) { |
6389 | 0 | (*seen).soa = true; |
6390 | 0 | } else if (rdataset.type == dns_rdatatype_ns) { |
6391 | 0 | (*seen).ns = true; |
6392 | 0 | } else if (rdataset.type == dns_rdatatype_ds) { |
6393 | 0 | (*seen).ds = true; |
6394 | 0 | } else if (rdataset.type == dns_rdatatype_dname) { |
6395 | 0 | (*seen).dname = true; |
6396 | 0 | } else if (rdataset.type == dns_rdatatype_nsec) { |
6397 | 0 | (*seen).nsec = true; |
6398 | 0 | } else if (rdataset.type == dns_rdatatype_nsec3) { |
6399 | 0 | (*seen).nsec3 = true; |
6400 | 0 | } |
6401 | |
|
6402 | 0 | dns_rdataset_disassociate(&rdataset); |
6403 | 0 | } |
6404 | |
|
6405 | 0 | return ISC_R_SUCCESS; |
6406 | 0 | } |
6407 | | |
6408 | | static isc_result_t |
6409 | | sign_a_node(dns_db_t *db, dns_zone_t *zone, dns_name_t *name, |
6410 | | dns_dbnode_t *node, dns_dbversion_t *version, bool build_nsec3, |
6411 | | bool build_nsec, dst_key_t *key, isc_stdtime_t now, |
6412 | | isc_stdtime_t inception, isc_stdtime_t expire, dns_ttl_t nsecttl, |
6413 | | bool both, bool is_ksk, bool is_zsk, bool fullsign, |
6414 | | bool is_bottom_of_zone, dns_diff_t *diff, int32_t *signatures, |
6415 | 0 | isc_mem_t *mctx) { |
6416 | 0 | isc_result_t result; |
6417 | 0 | dns_rdatasetiter_t *iterator = NULL; |
6418 | 0 | dns_rdataset_t rdataset = DNS_RDATASET_INIT; |
6419 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
6420 | 0 | dns_stats_t *dnssecsignstats; |
6421 | 0 | bool offlineksk = false; |
6422 | 0 | isc_buffer_t buffer; |
6423 | 0 | unsigned char data[DNS_RDATA_MAXLENGTH]; |
6424 | 0 | seen_t seen; |
6425 | |
|
6426 | 0 | if (zone->kasp != NULL) { |
6427 | 0 | offlineksk = dns_kasp_offlineksk(zone->kasp); |
6428 | 0 | } |
6429 | |
|
6430 | 0 | result = allrdatasets(db, node, version, &iterator, &seen); |
6431 | 0 | if (result != ISC_R_SUCCESS) { |
6432 | 0 | if (result == ISC_R_NOTFOUND) { |
6433 | 0 | result = ISC_R_SUCCESS; |
6434 | 0 | } |
6435 | 0 | return result; |
6436 | 0 | } |
6437 | | |
6438 | 0 | isc_buffer_init(&buffer, data, sizeof(data)); |
6439 | | |
6440 | | /* |
6441 | | * Going from insecure to NSEC3. |
6442 | | * Don't generate NSEC3 records for NSEC3 records. |
6443 | | */ |
6444 | 0 | if (build_nsec3 && !seen.nsec3 && seen.rr) { |
6445 | 0 | bool unsecure = !seen.ds && seen.ns && !seen.soa; |
6446 | 0 | CHECK(dns_nsec3_addnsec3s(db, version, name, nsecttl, unsecure, |
6447 | 0 | diff)); |
6448 | 0 | (*signatures)--; |
6449 | 0 | } |
6450 | | /* |
6451 | | * Going from insecure to NSEC. |
6452 | | * Don't generate NSEC records for NSEC3 records. |
6453 | | */ |
6454 | 0 | if (build_nsec && !seen.nsec3 && !seen.nsec && seen.rr) { |
6455 | | /* |
6456 | | * Build a NSEC record except at the origin. |
6457 | | */ |
6458 | 0 | if (!dns_name_equal(name, dns_db_origin(db))) { |
6459 | 0 | CHECK(add_nsec(db, version, name, node, nsecttl, |
6460 | 0 | is_bottom_of_zone, diff)); |
6461 | | /* Count a NSEC generation as a signature generation. */ |
6462 | 0 | (*signatures)--; |
6463 | 0 | } |
6464 | 0 | } |
6465 | | |
6466 | 0 | DNS_RDATASETITER_FOREACH(iterator) { |
6467 | 0 | isc_stdtime_t when; |
6468 | |
|
6469 | 0 | dns_rdataset_cleanup(&rdataset); |
6470 | |
|
6471 | 0 | dns_rdatasetiter_current(iterator, &rdataset); |
6472 | 0 | if (rdataset.type == dns_rdatatype_soa || |
6473 | 0 | rdataset.type == dns_rdatatype_rrsig) |
6474 | 0 | { |
6475 | 0 | continue; |
6476 | 0 | } |
6477 | 0 | if (dns_rdatatype_iskeymaterial(rdataset.type)) { |
6478 | | /* |
6479 | | * CDS and CDNSKEY are signed with KSK like DNSKEY. |
6480 | | * (RFC 7344, section 4.1 specifies that they must |
6481 | | * be signed with a key in the current DS RRset, |
6482 | | * which would only include KSK's.) |
6483 | | */ |
6484 | 0 | if (!is_ksk && both) { |
6485 | 0 | continue; |
6486 | 0 | } |
6487 | 0 | } else if (!is_zsk && both) { |
6488 | 0 | continue; |
6489 | 0 | } else if (is_zsk && |
6490 | 0 | !dst_key_is_signing(key, DST_BOOL_ZSK, now, &when)) |
6491 | 0 | { |
6492 | | /* Only applies to dnssec-policy. */ |
6493 | 0 | if (zone->kasp != NULL) { |
6494 | 0 | continue; |
6495 | 0 | } |
6496 | 0 | } |
6497 | | |
6498 | 0 | if (seen.ns && !seen.soa && rdataset.type != dns_rdatatype_ds && |
6499 | 0 | rdataset.type != dns_rdatatype_nsec) |
6500 | 0 | { |
6501 | 0 | continue; |
6502 | 0 | } |
6503 | 0 | if (signed_with_good_key(zone, db, node, version, rdataset.type, |
6504 | 0 | key, fullsign)) |
6505 | 0 | { |
6506 | 0 | continue; |
6507 | 0 | } |
6508 | | |
6509 | | /* Calculate the signature, creating a RRSIG RDATA. */ |
6510 | 0 | isc_buffer_clear(&buffer); |
6511 | 0 | if (offlineksk && dns_rdatatype_iskeymaterial(rdataset.type)) { |
6512 | | /* Look up the signature in the SKR bundle */ |
6513 | 0 | dns_skrbundle_t *bundle = dns_zone_getskrbundle(zone); |
6514 | 0 | if (bundle == NULL) { |
6515 | 0 | CLEANUP(DNS_R_NOSKRBUNDLE); |
6516 | 0 | } |
6517 | 0 | CHECK(dns_skrbundle_getsig(bundle, key, rdataset.type, |
6518 | 0 | &rdata)); |
6519 | 0 | } else { |
6520 | 0 | CHECK(dns_dnssec_sign(name, &rdataset, key, &inception, |
6521 | 0 | &expire, mctx, &buffer, &rdata)); |
6522 | 0 | } |
6523 | | |
6524 | | /* Update the database and journal with the RRSIG. */ |
6525 | | /* XXX inefficient - will cause dataset merging */ |
6526 | 0 | CHECK(update_one_rr(db, version, diff, DNS_DIFFOP_ADDRESIGN, |
6527 | 0 | name, rdataset.ttl, &rdata)); |
6528 | 0 | dns_rdata_reset(&rdata); |
6529 | | |
6530 | | /* Update DNSSEC sign statistics. */ |
6531 | 0 | dnssecsignstats = dns_zone_getdnssecsignstats(zone); |
6532 | 0 | if (dnssecsignstats != NULL) { |
6533 | | /* Generated a new signature. */ |
6534 | 0 | dns_dnssecsignstats_increment(dnssecsignstats, ID(key), |
6535 | 0 | ALG(key), |
6536 | 0 | dns_dnssecsignstats_sign); |
6537 | | /* This is a refresh. */ |
6538 | 0 | dns_dnssecsignstats_increment( |
6539 | 0 | dnssecsignstats, ID(key), ALG(key), |
6540 | 0 | dns_dnssecsignstats_refresh); |
6541 | 0 | } |
6542 | |
|
6543 | 0 | (*signatures)--; |
6544 | 0 | } |
6545 | | |
6546 | 0 | cleanup: |
6547 | 0 | dns_rdataset_cleanup(&rdataset); |
6548 | 0 | if (iterator != NULL) { |
6549 | 0 | dns_rdatasetiter_destroy(&iterator); |
6550 | 0 | } |
6551 | 0 | return result; |
6552 | 0 | } |
6553 | | |
6554 | | /* |
6555 | | * If 'update_only' is set then don't create a NSEC RRset if it doesn't exist. |
6556 | | */ |
6557 | | static isc_result_t |
6558 | | updatesecure(dns_db_t *db, dns_dbversion_t *version, dns_name_t *name, |
6559 | 0 | dns_ttl_t nsecttl, bool update_only, dns_diff_t *diff) { |
6560 | 0 | isc_result_t result; |
6561 | 0 | dns_rdataset_t rdataset; |
6562 | 0 | dns_dbnode_t *node = NULL; |
6563 | |
|
6564 | 0 | CHECK(dns_db_getoriginnode(db, &node)); |
6565 | 0 | if (update_only) { |
6566 | 0 | dns_rdataset_init(&rdataset); |
6567 | 0 | result = dns_db_findrdataset( |
6568 | 0 | db, node, version, dns_rdatatype_nsec, |
6569 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
6570 | 0 | dns_rdataset_cleanup(&rdataset); |
6571 | 0 | if (result == ISC_R_NOTFOUND) { |
6572 | 0 | goto success; |
6573 | 0 | } |
6574 | 0 | CHECK(result); |
6575 | 0 | } |
6576 | 0 | CHECK(delete_nsec(db, version, node, name, diff)); |
6577 | 0 | CHECK(add_nsec(db, version, name, node, nsecttl, false, diff)); |
6578 | 0 | success: |
6579 | 0 | result = ISC_R_SUCCESS; |
6580 | 0 | cleanup: |
6581 | 0 | if (node != NULL) { |
6582 | 0 | dns_db_detachnode(&node); |
6583 | 0 | } |
6584 | 0 | return result; |
6585 | 0 | } |
6586 | | |
6587 | | static isc_result_t |
6588 | | updatesignwithkey(dns_zone_t *zone, dns_signing_t *signing, |
6589 | | dns_dbversion_t *version, bool build_nsec3, dns_ttl_t nsecttl, |
6590 | 0 | dns_diff_t *diff) { |
6591 | 0 | isc_result_t result; |
6592 | 0 | dns_dbnode_t *node = NULL; |
6593 | 0 | dns_rdataset_t rdataset; |
6594 | 0 | bool seen_done = false; |
6595 | 0 | bool have_rr = false; |
6596 | |
|
6597 | 0 | dns_rdataset_init(&rdataset); |
6598 | 0 | CHECK(dns_db_getoriginnode(signing->db, &node)); |
6599 | |
|
6600 | 0 | result = dns_db_findrdataset(signing->db, node, version, |
6601 | 0 | zone->privatetype, dns_rdatatype_none, 0, |
6602 | 0 | &rdataset, NULL); |
6603 | 0 | if (result == ISC_R_NOTFOUND) { |
6604 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
6605 | 0 | result = ISC_R_SUCCESS; |
6606 | 0 | goto cleanup; |
6607 | 0 | } |
6608 | 0 | if (result != ISC_R_SUCCESS) { |
6609 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
6610 | 0 | goto cleanup; |
6611 | 0 | } |
6612 | | |
6613 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
6614 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
6615 | 0 | unsigned char alg = dst_algorithm_tosecalg(signing->algorithm); |
6616 | |
|
6617 | 0 | dns_rdataset_current(&rdataset, &rdata); |
6618 | | /* |
6619 | | * If we don't match the algorithm or keyid skip the record. |
6620 | | */ |
6621 | 0 | if ((rdata.length != SIGNING_RECORD_SIZE && |
6622 | 0 | rdata.length != OLD_SIGNING_RECORD_SIZE) || |
6623 | 0 | rdata.data[0] == 0 || rdata.data[0] != alg || |
6624 | 0 | rdata.data[1] != ((signing->keyid >> 8) & 0xff) || |
6625 | 0 | rdata.data[2] != (signing->keyid & 0xff) || |
6626 | 0 | (rdata.length == SIGNING_RECORD_SIZE && |
6627 | 0 | (rdata.data[5] != (signing->algorithm >> 8 & 0xff) || |
6628 | 0 | rdata.data[6] != (signing->algorithm & 0xff)))) |
6629 | 0 | { |
6630 | 0 | have_rr = true; |
6631 | 0 | continue; |
6632 | 0 | } |
6633 | | /* |
6634 | | * We have a match. If we were signing (!signing->deleteit) |
6635 | | * and we already have a record indicating that we have |
6636 | | * finished signing (rdata.data[4] != 0) then keep it. |
6637 | | * Otherwise it needs to be deleted as we have removed all |
6638 | | * the signatures (signing->deleteit), so any record indicating |
6639 | | * completion is now out of date, or we have finished signing |
6640 | | * with the new record so we no longer need to remember that |
6641 | | * we need to sign the zone with the matching key across a |
6642 | | * nameserver re-start. |
6643 | | */ |
6644 | 0 | if (!signing->deleteit && rdata.data[4] != 0) { |
6645 | 0 | seen_done = true; |
6646 | 0 | have_rr = true; |
6647 | 0 | } else { |
6648 | 0 | CHECK(update_one_rr(signing->db, version, diff, |
6649 | 0 | DNS_DIFFOP_DEL, &zone->origin, |
6650 | 0 | rdataset.ttl, &rdata)); |
6651 | 0 | } |
6652 | 0 | } |
6653 | | |
6654 | 0 | if (!signing->deleteit && !seen_done) { |
6655 | | /* |
6656 | | * If we were signing then we need to indicate that we have |
6657 | | * finished signing the zone with this key. If it is already |
6658 | | * there we don't need to add it a second time. |
6659 | | */ |
6660 | 0 | unsigned char data[SIGNING_RECORD_SIZE] = { |
6661 | 0 | dst_algorithm_tosecalg(signing->algorithm), |
6662 | 0 | (signing->keyid >> 8) & 0xff, |
6663 | 0 | signing->keyid & 0xff, |
6664 | 0 | 0, |
6665 | 0 | 1, |
6666 | 0 | (signing->algorithm >> 8) & 0xff, |
6667 | 0 | signing->algorithm & 0xff, |
6668 | 0 | }; |
6669 | 0 | dns_rdata_t rdata = (dns_rdata_t){ |
6670 | 0 | .length = signing->algorithm < 256 |
6671 | 0 | ? OLD_SIGNING_RECORD_SIZE |
6672 | 0 | : sizeof(data), |
6673 | 0 | .data = data, |
6674 | 0 | .type = zone->privatetype, |
6675 | 0 | .rdclass = dns_db_class(signing->db), |
6676 | 0 | .link = ISC_LINK_INITIALIZER, |
6677 | 0 | }; |
6678 | | /* |
6679 | | * data[0] can't be 0 as that is used to signal that the |
6680 | | * record is being used to for NSEC/NSEC3 chains generation. |
6681 | | * Set it to 255 instead. |
6682 | | */ |
6683 | 0 | if (data[0] == 0) { |
6684 | 0 | data[0] = 255; |
6685 | 0 | } |
6686 | 0 | CHECK(update_one_rr(signing->db, version, diff, DNS_DIFFOP_ADD, |
6687 | 0 | &zone->origin, rdataset.ttl, &rdata)); |
6688 | 0 | } else if (!have_rr) { |
6689 | 0 | dns_name_t *origin = dns_db_origin(signing->db); |
6690 | | /* |
6691 | | * Rebuild the NSEC/NSEC3 record for the origin as we no |
6692 | | * longer have any private records. |
6693 | | */ |
6694 | 0 | if (build_nsec3) { |
6695 | 0 | CHECK(dns_nsec3_addnsec3s(signing->db, version, origin, |
6696 | 0 | nsecttl, false, diff)); |
6697 | 0 | } |
6698 | 0 | CHECK(updatesecure(signing->db, version, origin, nsecttl, true, |
6699 | 0 | diff)); |
6700 | 0 | } |
6701 | | |
6702 | 0 | cleanup: |
6703 | 0 | dns_rdataset_cleanup(&rdataset); |
6704 | 0 | if (node != NULL) { |
6705 | 0 | dns_db_detachnode(&node); |
6706 | 0 | } |
6707 | 0 | return result; |
6708 | 0 | } |
6709 | | |
6710 | | /* |
6711 | | * Called from zone_nsec3chain() in order to update zone records indicating |
6712 | | * processing status of given NSEC3 chain: |
6713 | | * |
6714 | | * - If the supplied dns_nsec3chain_t structure has been fully processed |
6715 | | * (which is indicated by "active" being set to false): |
6716 | | * |
6717 | | * - remove all NSEC3PARAM records matching the relevant NSEC3 chain, |
6718 | | * |
6719 | | * - remove all private-type records containing NSEC3PARAM RDATA matching |
6720 | | * the relevant NSEC3 chain. |
6721 | | * |
6722 | | * - If the supplied dns_nsec3chain_t structure has not been fully processed |
6723 | | * (which is indicated by "active" being set to true), only remove the |
6724 | | * NSEC3PARAM record which matches the relevant NSEC3 chain and has the |
6725 | | * "flags" field set to 0. |
6726 | | * |
6727 | | * - If given NSEC3 chain is being added, add an NSEC3PARAM record contained |
6728 | | * in the relevant private-type record, but with the "flags" field set to |
6729 | | * 0, indicating that this NSEC3 chain is now complete for this zone. |
6730 | | * |
6731 | | * Note that this function is called at different processing stages for NSEC3 |
6732 | | * chain additions vs. removals and needs to handle all cases properly. |
6733 | | */ |
6734 | | static isc_result_t |
6735 | | fixup_nsec3param(dns_db_t *db, dns_dbversion_t *ver, dns_nsec3chain_t *chain, |
6736 | 0 | bool active, dns_rdatatype_t privatetype, dns_diff_t *diff) { |
6737 | 0 | dns_dbnode_t *node = NULL; |
6738 | 0 | dns_name_t *name = dns_db_origin(db); |
6739 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
6740 | 0 | dns_rdataset_t rdataset; |
6741 | 0 | dns_rdata_nsec3param_t nsec3param; |
6742 | 0 | dns_rdata_soa_t soa; |
6743 | 0 | isc_result_t result; |
6744 | 0 | isc_buffer_t buffer; |
6745 | 0 | unsigned char parambuf[DNS_NSEC3PARAM_BUFFERSIZE]; |
6746 | 0 | dns_ttl_t ttl = 0; |
6747 | 0 | bool nseconly = false, nsec3ok = false; |
6748 | |
|
6749 | 0 | dns_rdataset_init(&rdataset); |
6750 | |
|
6751 | 0 | result = dns_db_getoriginnode(db, &node); |
6752 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
6753 | | |
6754 | | /* Default TTL is SOA MINIMUM */ |
6755 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0, 0, |
6756 | 0 | &rdataset, NULL); |
6757 | 0 | if (result == ISC_R_SUCCESS) { |
6758 | 0 | CHECK(dns_rdataset_first(&rdataset)); |
6759 | 0 | dns_rdataset_current(&rdataset, &rdata); |
6760 | 0 | CHECK(dns_rdata_tostruct(&rdata, &soa, NULL)); |
6761 | 0 | ttl = soa.minimum; |
6762 | 0 | dns_rdata_reset(&rdata); |
6763 | 0 | } |
6764 | 0 | dns_rdataset_cleanup(&rdataset); |
6765 | |
|
6766 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec3param, 0, |
6767 | 0 | 0, &rdataset, NULL); |
6768 | 0 | if (result == ISC_R_NOTFOUND) { |
6769 | 0 | goto try_private; |
6770 | 0 | } |
6771 | 0 | if (result != ISC_R_SUCCESS) { |
6772 | 0 | goto cleanup; |
6773 | 0 | } |
6774 | | |
6775 | | /* |
6776 | | * Delete all NSEC3PARAM records which match that in nsec3chain. |
6777 | | */ |
6778 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
6779 | 0 | dns_rdataset_current(&rdataset, &rdata); |
6780 | 0 | CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL)); |
6781 | |
|
6782 | 0 | if (nsec3param.hash != chain->nsec3param.hash || |
6783 | 0 | (active && nsec3param.flags != 0) || |
6784 | 0 | nsec3param.iterations != chain->nsec3param.iterations || |
6785 | 0 | nsec3param.salt.length != chain->nsec3param.salt.length || |
6786 | 0 | memcmp(nsec3param.salt.base, chain->nsec3param.salt.base, |
6787 | 0 | nsec3param.salt.length)) |
6788 | 0 | { |
6789 | | /* |
6790 | | * If the SOA minimum is different to the current TTL, |
6791 | | * delete the record. We will re-add it with the new |
6792 | | * TTL below. |
6793 | | */ |
6794 | 0 | if (rdataset.ttl != ttl) { |
6795 | 0 | CHECK(update_one_rr(db, ver, diff, |
6796 | 0 | DNS_DIFFOP_DEL, name, |
6797 | 0 | rdataset.ttl, &rdata)); |
6798 | 0 | } |
6799 | 0 | dns_rdata_reset(&rdata); |
6800 | 0 | continue; |
6801 | 0 | } |
6802 | | |
6803 | 0 | CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name, |
6804 | 0 | rdataset.ttl, &rdata)); |
6805 | 0 | dns_rdata_reset(&rdata); |
6806 | 0 | } |
6807 | | |
6808 | | /* |
6809 | | * Restore any NSEC3PARAM records that we deleted to change the TTL. |
6810 | | */ |
6811 | 0 | if (rdataset.ttl != ttl) { |
6812 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
6813 | 0 | dns_rdataset_current(&rdataset, &rdata); |
6814 | 0 | CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL)); |
6815 | |
|
6816 | 0 | if (nsec3param.hash != chain->nsec3param.hash || |
6817 | 0 | (active && nsec3param.flags != 0) || |
6818 | 0 | nsec3param.iterations != |
6819 | 0 | chain->nsec3param.iterations || |
6820 | 0 | nsec3param.salt.length != |
6821 | 0 | chain->nsec3param.salt.length || |
6822 | 0 | memcmp(nsec3param.salt.base, |
6823 | 0 | chain->nsec3param.salt.base, |
6824 | 0 | nsec3param.salt.length)) |
6825 | 0 | { |
6826 | 0 | CHECK(update_one_rr(db, ver, diff, |
6827 | 0 | DNS_DIFFOP_ADD, name, ttl, |
6828 | 0 | &rdata)); |
6829 | 0 | } |
6830 | 0 | dns_rdata_reset(&rdata); |
6831 | 0 | } |
6832 | 0 | } |
6833 | | |
6834 | 0 | dns_rdataset_disassociate(&rdataset); |
6835 | |
|
6836 | 0 | try_private: |
6837 | |
|
6838 | 0 | if (active) { |
6839 | 0 | goto add; |
6840 | 0 | } |
6841 | | |
6842 | 0 | result = dns_nsec_nseconly(db, ver, diff, &nseconly); |
6843 | 0 | nsec3ok = (result == ISC_R_SUCCESS && !nseconly); |
6844 | | |
6845 | | /* |
6846 | | * Delete all private records which match that in nsec3chain. |
6847 | | */ |
6848 | 0 | result = dns_db_findrdataset(db, node, ver, privatetype, 0, 0, |
6849 | 0 | &rdataset, NULL); |
6850 | 0 | if (result == ISC_R_NOTFOUND) { |
6851 | 0 | goto add; |
6852 | 0 | } |
6853 | 0 | CHECK(result); |
6854 | |
|
6855 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
6856 | 0 | dns_rdata_t private = DNS_RDATA_INIT; |
6857 | 0 | unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE]; |
6858 | |
|
6859 | 0 | dns_rdataset_current(&rdataset, &private); |
6860 | 0 | if (!dns_nsec3param_fromprivate(&private, &rdata, buf, |
6861 | 0 | sizeof(buf))) |
6862 | 0 | { |
6863 | 0 | continue; |
6864 | 0 | } |
6865 | 0 | CHECK(dns_rdata_tostruct(&rdata, &nsec3param, NULL)); |
6866 | 0 | dns_rdata_reset(&rdata); |
6867 | |
|
6868 | 0 | if ((!nsec3ok && |
6869 | 0 | (nsec3param.flags & DNS_NSEC3FLAG_INITIAL) != 0) || |
6870 | 0 | nsec3param.hash != chain->nsec3param.hash || |
6871 | 0 | nsec3param.iterations != chain->nsec3param.iterations || |
6872 | 0 | nsec3param.salt.length != chain->nsec3param.salt.length || |
6873 | 0 | memcmp(nsec3param.salt.base, chain->nsec3param.salt.base, |
6874 | 0 | nsec3param.salt.length)) |
6875 | 0 | { |
6876 | 0 | continue; |
6877 | 0 | } |
6878 | | |
6879 | 0 | CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name, |
6880 | 0 | rdataset.ttl, &private)); |
6881 | 0 | } |
6882 | | |
6883 | 0 | add: |
6884 | 0 | if ((chain->nsec3param.flags & DNS_NSEC3FLAG_REMOVE) != 0) { |
6885 | 0 | result = ISC_R_SUCCESS; |
6886 | 0 | goto cleanup; |
6887 | 0 | } |
6888 | | |
6889 | | /* |
6890 | | * Add a NSEC3PARAM record which matches that in nsec3chain but |
6891 | | * with all flags bits cleared. |
6892 | | * |
6893 | | * Note: we do not clear chain->nsec3param.flags as this change |
6894 | | * may be reversed. |
6895 | | */ |
6896 | 0 | isc_buffer_init(&buffer, ¶mbuf, sizeof(parambuf)); |
6897 | 0 | CHECK(dns_rdata_fromstruct(&rdata, dns_db_class(db), |
6898 | 0 | dns_rdatatype_nsec3param, &chain->nsec3param, |
6899 | 0 | &buffer)); |
6900 | 0 | rdata.data[1] = 0; /* Clear flag bits. */ |
6901 | 0 | CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_ADD, name, ttl, &rdata)); |
6902 | |
|
6903 | 0 | cleanup: |
6904 | 0 | dns_db_detachnode(&node); |
6905 | 0 | dns_rdataset_cleanup(&rdataset); |
6906 | 0 | return result; |
6907 | 0 | } |
6908 | | |
6909 | | static isc_result_t |
6910 | | delete_nsec(dns_db_t *db, dns_dbversion_t *ver, dns_dbnode_t *node, |
6911 | 0 | dns_name_t *name, dns_diff_t *diff) { |
6912 | 0 | dns_rdataset_t rdataset; |
6913 | 0 | isc_result_t result; |
6914 | |
|
6915 | 0 | dns_rdataset_init(&rdataset); |
6916 | |
|
6917 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec, 0, 0, |
6918 | 0 | &rdataset, NULL); |
6919 | 0 | if (result == ISC_R_NOTFOUND) { |
6920 | 0 | return ISC_R_SUCCESS; |
6921 | 0 | } |
6922 | 0 | if (result != ISC_R_SUCCESS) { |
6923 | 0 | return result; |
6924 | 0 | } |
6925 | | |
6926 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
6927 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
6928 | |
|
6929 | 0 | dns_rdataset_current(&rdataset, &rdata); |
6930 | 0 | CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name, |
6931 | 0 | rdataset.ttl, &rdata)); |
6932 | 0 | } |
6933 | | |
6934 | 0 | cleanup: |
6935 | 0 | dns_rdataset_disassociate(&rdataset); |
6936 | 0 | return result; |
6937 | 0 | } |
6938 | | |
6939 | | static isc_result_t |
6940 | | deletematchingnsec3(dns_db_t *db, dns_dbversion_t *ver, dns_dbnode_t *node, |
6941 | | dns_name_t *name, const dns_rdata_nsec3param_t *param, |
6942 | 0 | dns_diff_t *diff) { |
6943 | 0 | dns_rdataset_t rdataset; |
6944 | 0 | dns_rdata_nsec3_t nsec3; |
6945 | 0 | isc_result_t result; |
6946 | |
|
6947 | 0 | dns_rdataset_init(&rdataset); |
6948 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec3, 0, 0, |
6949 | 0 | &rdataset, NULL); |
6950 | 0 | if (result == ISC_R_NOTFOUND) { |
6951 | 0 | return ISC_R_SUCCESS; |
6952 | 0 | } |
6953 | 0 | if (result != ISC_R_SUCCESS) { |
6954 | 0 | return result; |
6955 | 0 | } |
6956 | | |
6957 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
6958 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
6959 | |
|
6960 | 0 | dns_rdataset_current(&rdataset, &rdata); |
6961 | 0 | CHECK(dns_rdata_tostruct(&rdata, &nsec3, NULL)); |
6962 | 0 | if (nsec3.hash != param->hash || |
6963 | 0 | nsec3.iterations != param->iterations || |
6964 | 0 | nsec3.salt.length != param->salt.length || |
6965 | 0 | memcmp(nsec3.salt.base, param->salt.base, |
6966 | 0 | nsec3.salt.length)) |
6967 | 0 | { |
6968 | 0 | continue; |
6969 | 0 | } |
6970 | 0 | CHECK(update_one_rr(db, ver, diff, DNS_DIFFOP_DEL, name, |
6971 | 0 | rdataset.ttl, &rdata)); |
6972 | 0 | } |
6973 | | |
6974 | 0 | cleanup: |
6975 | 0 | dns_rdataset_disassociate(&rdataset); |
6976 | 0 | return result; |
6977 | 0 | } |
6978 | | |
6979 | | static isc_result_t |
6980 | | need_nsec_chain(dns_db_t *db, dns_dbversion_t *ver, |
6981 | 0 | const dns_rdata_nsec3param_t *param, bool *answer) { |
6982 | 0 | dns_dbnode_t *node = NULL; |
6983 | 0 | dns_rdata_nsec3param_t myparam; |
6984 | 0 | dns_rdataset_t rdataset; |
6985 | 0 | isc_result_t result; |
6986 | |
|
6987 | 0 | *answer = false; |
6988 | |
|
6989 | 0 | result = dns_db_getoriginnode(db, &node); |
6990 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
6991 | |
|
6992 | 0 | dns_rdataset_init(&rdataset); |
6993 | |
|
6994 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec, 0, 0, |
6995 | 0 | &rdataset, NULL); |
6996 | 0 | if (result == ISC_R_SUCCESS) { |
6997 | 0 | dns_rdataset_disassociate(&rdataset); |
6998 | 0 | dns_db_detachnode(&node); |
6999 | 0 | return result; |
7000 | 0 | } |
7001 | 0 | if (result != ISC_R_NOTFOUND) { |
7002 | 0 | dns_db_detachnode(&node); |
7003 | 0 | return result; |
7004 | 0 | } |
7005 | | |
7006 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_nsec3param, 0, |
7007 | 0 | 0, &rdataset, NULL); |
7008 | 0 | if (result == ISC_R_NOTFOUND) { |
7009 | 0 | *answer = true; |
7010 | 0 | dns_db_detachnode(&node); |
7011 | 0 | return ISC_R_SUCCESS; |
7012 | 0 | } |
7013 | 0 | if (result != ISC_R_SUCCESS) { |
7014 | 0 | dns_db_detachnode(&node); |
7015 | 0 | return result; |
7016 | 0 | } |
7017 | | |
7018 | 0 | bool active = false; |
7019 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
7020 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
7021 | 0 | dns_rdataset_current(&rdataset, &rdata); |
7022 | 0 | CHECK(dns_rdata_tostruct(&rdata, &myparam, NULL)); |
7023 | | |
7024 | | /* |
7025 | | * Ignore any NSEC3PARAM removals. |
7026 | | */ |
7027 | 0 | if (NSEC3REMOVE(myparam.flags)) { |
7028 | 0 | continue; |
7029 | 0 | } |
7030 | | /* |
7031 | | * Ignore the chain that we are in the process of deleting. |
7032 | | */ |
7033 | 0 | if (myparam.hash == param->hash && |
7034 | 0 | myparam.iterations == param->iterations && |
7035 | 0 | myparam.salt.length == param->salt.length && |
7036 | 0 | !memcmp(myparam.salt.base, param->salt.base, |
7037 | 0 | myparam.salt.length)) |
7038 | 0 | { |
7039 | 0 | continue; |
7040 | 0 | } |
7041 | | |
7042 | | /* |
7043 | | * Found an active NSEC3 chain. |
7044 | | */ |
7045 | 0 | active = true; |
7046 | 0 | break; |
7047 | 0 | } |
7048 | | |
7049 | 0 | *answer = !active; |
7050 | |
|
7051 | 0 | cleanup: |
7052 | 0 | dns_rdataset_cleanup(&rdataset); |
7053 | 0 | dns_db_detachnode(&node); |
7054 | 0 | return result; |
7055 | 0 | } |
7056 | | |
7057 | | /*% |
7058 | | * Given a tuple which is part of a diff, return a pointer to the next tuple in |
7059 | | * that diff which has the same name and type (or NULL if no such tuple is |
7060 | | * found). |
7061 | | */ |
7062 | | static dns_difftuple_t * |
7063 | 0 | find_next_matching_tuple(dns_difftuple_t *cur) { |
7064 | 0 | dns_difftuple_t *next = cur; |
7065 | |
|
7066 | 0 | while ((next = ISC_LIST_NEXT(next, link)) != NULL) { |
7067 | 0 | if (cur->rdata.type == next->rdata.type && |
7068 | 0 | dns_name_equal(&cur->name, &next->name)) |
7069 | 0 | { |
7070 | 0 | return next; |
7071 | 0 | } |
7072 | 0 | } |
7073 | | |
7074 | 0 | return NULL; |
7075 | 0 | } |
7076 | | |
7077 | | /*% |
7078 | | * Remove all tuples with the same name and type as 'cur' from 'src' and append |
7079 | | * them to 'dst'. |
7080 | | */ |
7081 | | static void |
7082 | 0 | move_matching_tuples(dns_difftuple_t *cur, dns_diff_t *src, dns_diff_t *dst) { |
7083 | 0 | do { |
7084 | 0 | dns_difftuple_t *next = find_next_matching_tuple(cur); |
7085 | 0 | ISC_LIST_UNLINK(src->tuples, cur, link); |
7086 | 0 | dns_diff_appendminimal(dst, &cur); |
7087 | 0 | cur = next; |
7088 | 0 | } while (cur != NULL); |
7089 | 0 | } |
7090 | | |
7091 | | /*% |
7092 | | * Add/remove DNSSEC signatures for the list of "raw" zone changes supplied in |
7093 | | * 'diff'. Gradually remove tuples from 'diff' and append them to 'zonediff' |
7094 | | * along with tuples representing relevant signature changes. |
7095 | | */ |
7096 | | isc_result_t |
7097 | | dns__zone_updatesigs(dns_diff_t *diff, dns_db_t *db, dns_dbversion_t *version, |
7098 | | dst_key_t *zone_keys[], unsigned int nkeys, |
7099 | | dns_zone_t *zone, isc_stdtime_t inception, |
7100 | | isc_stdtime_t expire, isc_stdtime_t keyexpire, |
7101 | 0 | isc_stdtime_t now, dns__zonediff_t *zonediff) { |
7102 | 0 | dns_difftuple_t *tuple; |
7103 | 0 | isc_result_t result; |
7104 | |
|
7105 | 0 | while ((tuple = ISC_LIST_HEAD(diff->tuples)) != NULL) { |
7106 | 0 | isc_stdtime_t exp = expire; |
7107 | |
|
7108 | 0 | if (keyexpire != 0 && |
7109 | 0 | dns_rdatatype_iskeymaterial(tuple->rdata.type)) |
7110 | 0 | { |
7111 | 0 | exp = keyexpire; |
7112 | 0 | } |
7113 | |
|
7114 | 0 | result = del_sigs(zone, db, version, &tuple->name, |
7115 | 0 | tuple->rdata.type, zonediff, zone_keys, nkeys, |
7116 | 0 | now, false); |
7117 | 0 | if (result != ISC_R_SUCCESS) { |
7118 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
7119 | 0 | "dns__zone_updatesigs:del_sigs -> %s", |
7120 | 0 | isc_result_totext(result)); |
7121 | 0 | return result; |
7122 | 0 | } |
7123 | 0 | result = add_sigs(db, version, &tuple->name, zone, |
7124 | 0 | tuple->rdata.type, zonediff->diff, zone_keys, |
7125 | 0 | nkeys, zone->mctx, now, inception, exp); |
7126 | 0 | if (result != ISC_R_SUCCESS) { |
7127 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
7128 | 0 | "dns__zone_updatesigs:add_sigs -> %s", |
7129 | 0 | isc_result_totext(result)); |
7130 | 0 | return result; |
7131 | 0 | } |
7132 | | |
7133 | | /* |
7134 | | * Signature changes for all RRs with name tuple->name and type |
7135 | | * tuple->rdata.type were appended to zonediff->diff. Now we |
7136 | | * remove all the "raw" changes with the same name and type |
7137 | | * from diff (so that they are not processed by this loop |
7138 | | * again) and append them to zonediff so that they get applied. |
7139 | | */ |
7140 | 0 | move_matching_tuples(tuple, diff, zonediff->diff); |
7141 | 0 | } |
7142 | 0 | return ISC_R_SUCCESS; |
7143 | 0 | } |
7144 | | |
7145 | | /* |
7146 | | * Incrementally build and sign a new NSEC3 chain using the parameters |
7147 | | * requested. |
7148 | | */ |
7149 | | static void |
7150 | 0 | zone_nsec3chain(dns_zone_t *zone) { |
7151 | 0 | dns_db_t *db = NULL; |
7152 | 0 | dns_dbnode_t *node = NULL; |
7153 | 0 | dns_dbversion_t *version = NULL; |
7154 | 0 | dns_diff_t _sig_diff; |
7155 | 0 | dns_diff_t nsec_diff; |
7156 | 0 | dns_diff_t nsec3_diff; |
7157 | 0 | dns_diff_t param_diff; |
7158 | 0 | dns__zonediff_t zonediff; |
7159 | 0 | dns_fixedname_t fixed; |
7160 | 0 | dns_fixedname_t nextfixed; |
7161 | 0 | dns_name_t *name = NULL, *nextname = NULL; |
7162 | 0 | dns_nsec3chain_t *nsec3chain = NULL; |
7163 | 0 | dns_nsec3chainlist_t cleanup; |
7164 | 0 | dst_key_t *zone_keys[DNS_MAXZONEKEYS]; |
7165 | 0 | int32_t signatures; |
7166 | 0 | bool delegation; |
7167 | 0 | bool first; |
7168 | 0 | isc_result_t result; |
7169 | 0 | isc_stdtime_t now, inception, soaexpire, expire; |
7170 | 0 | unsigned int i; |
7171 | 0 | unsigned int nkeys = 0; |
7172 | 0 | uint32_t nodes; |
7173 | 0 | bool unsecure = false; |
7174 | 0 | seen_t seen; |
7175 | 0 | dns_rdatasetiter_t *iterator = NULL; |
7176 | 0 | bool buildnsecchain; |
7177 | 0 | bool updatensec = false; |
7178 | 0 | dns_rdatatype_t privatetype = zone->privatetype; |
7179 | |
|
7180 | 0 | ENTER; |
7181 | |
|
7182 | 0 | name = dns_fixedname_initname(&fixed); |
7183 | 0 | nextname = dns_fixedname_initname(&nextfixed); |
7184 | 0 | dns_diff_init(zone->mctx, ¶m_diff); |
7185 | 0 | dns_diff_init(zone->mctx, &nsec3_diff); |
7186 | 0 | dns_diff_init(zone->mctx, &nsec_diff); |
7187 | 0 | dns_diff_init(zone->mctx, &_sig_diff); |
7188 | 0 | zonediff_init(&zonediff, &_sig_diff); |
7189 | 0 | ISC_LIST_INIT(cleanup); |
7190 | | |
7191 | | /* |
7192 | | * Updates are disabled. Pause for 5 minutes. |
7193 | | */ |
7194 | 0 | if (zone->update_disabled) { |
7195 | 0 | CLEANUP(ISC_R_FAILURE); |
7196 | 0 | } |
7197 | | |
7198 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
7199 | | /* |
7200 | | * This function is called when zone timer fires, after the latter gets |
7201 | | * set by zone_addnsec3chain(). If the action triggering the call to |
7202 | | * zone_addnsec3chain() is closely followed by a zone deletion request, |
7203 | | * it might turn out that the timer thread will not be woken up until |
7204 | | * after the zone is deleted by rmzone(), which calls dns_db_detach() |
7205 | | * for zone->db, causing the latter to become NULL. Return immediately |
7206 | | * if that happens. |
7207 | | */ |
7208 | 0 | if (zone->db != NULL) { |
7209 | 0 | dns_db_attach(zone->db, &db); |
7210 | 0 | } |
7211 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
7212 | 0 | if (db == NULL) { |
7213 | 0 | return; |
7214 | 0 | } |
7215 | | |
7216 | 0 | result = dns_db_newversion(db, &version); |
7217 | 0 | if (result != ISC_R_SUCCESS) { |
7218 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7219 | 0 | "zone_nsec3chain:dns_db_newversion -> %s", |
7220 | 0 | isc_result_totext(result)); |
7221 | 0 | goto cleanup; |
7222 | 0 | } |
7223 | | |
7224 | 0 | now = isc_stdtime_now(); |
7225 | |
|
7226 | 0 | result = dns_zone_findkeys(zone, db, version, now, zone->mctx, |
7227 | 0 | DNS_MAXZONEKEYS, zone_keys, &nkeys); |
7228 | 0 | if (result != ISC_R_SUCCESS) { |
7229 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7230 | 0 | "zone_nsec3chain:dns_zone_findkeys -> %s", |
7231 | 0 | isc_result_totext(result)); |
7232 | 0 | goto cleanup; |
7233 | 0 | } |
7234 | | |
7235 | 0 | calculate_rrsig_validity(zone, now, &inception, &soaexpire, NULL, |
7236 | 0 | &expire); |
7237 | | |
7238 | | /* |
7239 | | * We keep pulling nodes off each iterator in turn until |
7240 | | * we have no more nodes to pull off or we reach the limits |
7241 | | * for this quantum. |
7242 | | */ |
7243 | 0 | nodes = zone->nodes; |
7244 | 0 | signatures = zone->signatures; |
7245 | 0 | LOCK_ZONE(zone); |
7246 | 0 | nsec3chain = ISC_LIST_HEAD(zone->nsec3chain); |
7247 | 0 | UNLOCK_ZONE(zone); |
7248 | 0 | first = true; |
7249 | |
|
7250 | 0 | if (nsec3chain != NULL) { |
7251 | 0 | nsec3chain->save_delete_nsec = nsec3chain->delete_nsec; |
7252 | 0 | } |
7253 | | /* |
7254 | | * Generate new NSEC3 chains first. |
7255 | | * |
7256 | | * The following while loop iterates over nodes in the zone database, |
7257 | | * updating the NSEC3 chain by calling dns_nsec3_addnsec3() for each of |
7258 | | * them. Once all nodes are processed, the "delete_nsec" field is |
7259 | | * consulted to check whether we are supposed to remove NSEC records |
7260 | | * from the zone database; if so, the database iterator is reset to |
7261 | | * point to the first node and the loop traverses all of them again, |
7262 | | * this time removing NSEC records. If we hit a node which is obscured |
7263 | | * by a delegation or a DNAME, nodes are skipped over until we find one |
7264 | | * that is not obscured by the same obscuring name and then normal |
7265 | | * processing is resumed. |
7266 | | * |
7267 | | * The above is repeated until all requested NSEC3 chain changes are |
7268 | | * applied or when we reach the limits for this quantum, whichever |
7269 | | * happens first. |
7270 | | * |
7271 | | * Note that the "signatures" variable is only used here to limit the |
7272 | | * amount of work performed. Actual DNSSEC signatures are only |
7273 | | * generated by dns__zone_updatesigs() calls later in this function. |
7274 | | */ |
7275 | 0 | while (nsec3chain != NULL && nodes-- > 0 && signatures > 0) { |
7276 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7277 | |
|
7278 | 0 | LOCK_ZONE(zone); |
7279 | 0 | dns_nsec3chain_t *nextnsec3chain = ISC_LIST_NEXT(nsec3chain, |
7280 | 0 | link); |
7281 | |
|
7282 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
7283 | 0 | if (nsec3chain->done || nsec3chain->db != zone->db) { |
7284 | 0 | ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain, link); |
7285 | 0 | ISC_LIST_APPEND(cleanup, nsec3chain, link); |
7286 | 0 | } |
7287 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
7288 | 0 | UNLOCK_ZONE(zone); |
7289 | 0 | if (ISC_LIST_TAIL(cleanup) == nsec3chain) { |
7290 | 0 | goto next_addchain; |
7291 | 0 | } |
7292 | | |
7293 | | /* |
7294 | | * Possible future db. |
7295 | | */ |
7296 | 0 | if (nsec3chain->db != db) { |
7297 | 0 | goto next_addchain; |
7298 | 0 | } |
7299 | | |
7300 | 0 | if (NSEC3REMOVE(nsec3chain->nsec3param.flags)) { |
7301 | 0 | goto next_addchain; |
7302 | 0 | } |
7303 | | |
7304 | 0 | dns_dbiterator_current(nsec3chain->dbiterator, &node, name); |
7305 | |
|
7306 | 0 | if (nsec3chain->delete_nsec) { |
7307 | 0 | delegation = false; |
7308 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7309 | 0 | CHECK(delete_nsec(db, version, node, name, &nsec_diff)); |
7310 | 0 | goto next_addnode; |
7311 | 0 | } |
7312 | | /* |
7313 | | * On the first pass we need to check if the current node |
7314 | | * has not been obscured. |
7315 | | */ |
7316 | 0 | delegation = false; |
7317 | 0 | unsecure = false; |
7318 | 0 | if (first) { |
7319 | 0 | dns_fixedname_t ffound; |
7320 | 0 | dns_name_t *found; |
7321 | 0 | found = dns_fixedname_initname(&ffound); |
7322 | 0 | result = dns_db_find( |
7323 | 0 | db, name, version, dns_rdatatype_soa, |
7324 | 0 | DNS_DBFIND_NOWILD, 0, found, NULL, NULL); |
7325 | 0 | if ((result == DNS_R_DELEGATION || |
7326 | 0 | result == DNS_R_DNAME) && |
7327 | 0 | !dns_name_equal(name, found)) |
7328 | 0 | { |
7329 | | /* |
7330 | | * Remember the obscuring name so that |
7331 | | * we skip all obscured names. |
7332 | | */ |
7333 | 0 | dns_name_copy(found, name); |
7334 | 0 | delegation = true; |
7335 | 0 | goto next_addnode; |
7336 | 0 | } |
7337 | 0 | } |
7338 | | |
7339 | | /* |
7340 | | * Check to see if this is a bottom of zone node. |
7341 | | */ |
7342 | 0 | result = allrdatasets(db, node, version, &iterator, &seen); |
7343 | 0 | if (result == ISC_R_NOTFOUND) { |
7344 | | /* Empty node? */ |
7345 | 0 | goto next_addnode; |
7346 | 0 | } |
7347 | 0 | CHECK(result); |
7348 | |
|
7349 | 0 | INSIST(!seen.nsec3); |
7350 | |
|
7351 | 0 | dns_rdatasetiter_destroy(&iterator); |
7352 | | /* |
7353 | | * Is there a NSEC chain than needs to be cleaned up? |
7354 | | */ |
7355 | 0 | if (seen.nsec) { |
7356 | 0 | nsec3chain->seen_nsec = true; |
7357 | 0 | } |
7358 | |
|
7359 | 0 | if (seen.ns && !seen.soa && !seen.ds) { |
7360 | 0 | unsecure = true; |
7361 | 0 | } |
7362 | 0 | if ((seen.ns && !seen.soa) || seen.dname) { |
7363 | 0 | delegation = true; |
7364 | 0 | } |
7365 | | |
7366 | | /* |
7367 | | * Process one node. |
7368 | | */ |
7369 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7370 | 0 | result = dns_nsec3_addnsec3( |
7371 | 0 | db, version, name, &nsec3chain->nsec3param, |
7372 | 0 | zone_nsecttl(zone), unsecure, &nsec3_diff); |
7373 | 0 | if (result != ISC_R_SUCCESS) { |
7374 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7375 | 0 | "zone_nsec3chain:" |
7376 | 0 | "dns_nsec3_addnsec3 -> %s", |
7377 | 0 | isc_result_totext(result)); |
7378 | 0 | goto cleanup; |
7379 | 0 | } |
7380 | | |
7381 | | /* |
7382 | | * Treat each call to dns_nsec3_addnsec3() as if it's cost is |
7383 | | * two signatures. Additionally there will, in general, be |
7384 | | * two signature generated below. |
7385 | | * |
7386 | | * If we are only changing the optout flag the cost is half |
7387 | | * that of the cost of generating a completely new chain. |
7388 | | */ |
7389 | 0 | signatures -= 4; |
7390 | | |
7391 | | /* |
7392 | | * Go onto next node. |
7393 | | */ |
7394 | 0 | next_addnode: |
7395 | 0 | first = false; |
7396 | 0 | dns_db_detachnode(&node); |
7397 | 0 | do { |
7398 | 0 | result = dns_dbiterator_next(nsec3chain->dbiterator); |
7399 | |
|
7400 | 0 | if (result == ISC_R_NOMORE && nsec3chain->delete_nsec) { |
7401 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7402 | 0 | CHECK(fixup_nsec3param(db, version, nsec3chain, |
7403 | 0 | false, privatetype, |
7404 | 0 | ¶m_diff)); |
7405 | 0 | LOCK_ZONE(zone); |
7406 | 0 | ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain, |
7407 | 0 | link); |
7408 | 0 | UNLOCK_ZONE(zone); |
7409 | 0 | ISC_LIST_APPEND(cleanup, nsec3chain, link); |
7410 | 0 | goto next_addchain; |
7411 | 0 | } |
7412 | 0 | if (result == ISC_R_NOMORE) { |
7413 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7414 | 0 | if (nsec3chain->seen_nsec) { |
7415 | 0 | CHECK(fixup_nsec3param( |
7416 | 0 | db, version, nsec3chain, true, |
7417 | 0 | privatetype, ¶m_diff)); |
7418 | 0 | nsec3chain->delete_nsec = true; |
7419 | 0 | goto same_addchain; |
7420 | 0 | } |
7421 | 0 | CHECK(fixup_nsec3param(db, version, nsec3chain, |
7422 | 0 | false, privatetype, |
7423 | 0 | ¶m_diff)); |
7424 | 0 | LOCK_ZONE(zone); |
7425 | 0 | ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain, |
7426 | 0 | link); |
7427 | 0 | UNLOCK_ZONE(zone); |
7428 | 0 | ISC_LIST_APPEND(cleanup, nsec3chain, link); |
7429 | 0 | goto next_addchain; |
7430 | 0 | } else if (result != ISC_R_SUCCESS) { |
7431 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7432 | 0 | "zone_nsec3chain:" |
7433 | 0 | "dns_dbiterator_next -> %s", |
7434 | 0 | isc_result_totext(result)); |
7435 | 0 | goto cleanup; |
7436 | 0 | } else if (delegation) { |
7437 | 0 | dns_dbiterator_current(nsec3chain->dbiterator, |
7438 | 0 | &node, nextname); |
7439 | 0 | dns_db_detachnode(&node); |
7440 | 0 | if (!dns_name_issubdomain(nextname, name)) { |
7441 | 0 | break; |
7442 | 0 | } |
7443 | 0 | } else { |
7444 | 0 | break; |
7445 | 0 | } |
7446 | 0 | } while (1); |
7447 | 0 | continue; |
7448 | | |
7449 | 0 | same_addchain: |
7450 | 0 | CHECK(dns_dbiterator_first(nsec3chain->dbiterator)); |
7451 | 0 | first = true; |
7452 | 0 | continue; |
7453 | | |
7454 | 0 | next_addchain: |
7455 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7456 | 0 | nsec3chain = nextnsec3chain; |
7457 | 0 | first = true; |
7458 | 0 | if (nsec3chain != NULL) { |
7459 | 0 | nsec3chain->save_delete_nsec = nsec3chain->delete_nsec; |
7460 | 0 | } |
7461 | 0 | } |
7462 | | |
7463 | 0 | if (nsec3chain != NULL) { |
7464 | 0 | goto skip_removals; |
7465 | 0 | } |
7466 | | |
7467 | | /* |
7468 | | * Process removals. |
7469 | | * |
7470 | | * This is a counterpart of the above while loop which takes care of |
7471 | | * removing an NSEC3 chain. It starts with determining whether the |
7472 | | * zone needs to switch from NSEC3 to NSEC; if so, it first builds an |
7473 | | * NSEC chain by iterating over all nodes in the zone database and only |
7474 | | * then goes on to remove NSEC3 records be iterating over all nodes |
7475 | | * again and calling deletematchingnsec3() for each of them; otherwise, |
7476 | | * it starts removing NSEC3 records immediately. Rules for processing |
7477 | | * obscured nodes and interrupting work are the same as for the while |
7478 | | * loop above. |
7479 | | */ |
7480 | 0 | LOCK_ZONE(zone); |
7481 | 0 | nsec3chain = ISC_LIST_HEAD(zone->nsec3chain); |
7482 | 0 | UNLOCK_ZONE(zone); |
7483 | 0 | first = true; |
7484 | 0 | buildnsecchain = false; |
7485 | 0 | while (nsec3chain != NULL && nodes-- > 0 && signatures > 0) { |
7486 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7487 | |
|
7488 | 0 | LOCK_ZONE(zone); |
7489 | 0 | dns_nsec3chain_t *nextnsec3chain = ISC_LIST_NEXT(nsec3chain, |
7490 | 0 | link); |
7491 | 0 | UNLOCK_ZONE(zone); |
7492 | |
|
7493 | 0 | if (nsec3chain->db != db) { |
7494 | 0 | goto next_removechain; |
7495 | 0 | } |
7496 | | |
7497 | 0 | if (!NSEC3REMOVE(nsec3chain->nsec3param.flags)) { |
7498 | 0 | goto next_removechain; |
7499 | 0 | } |
7500 | | |
7501 | | /* |
7502 | | * Work out if we need to build a NSEC chain as a consequence |
7503 | | * of removing this NSEC3 chain. |
7504 | | */ |
7505 | 0 | if (first && !updatensec && |
7506 | 0 | (nsec3chain->nsec3param.flags & DNS_NSEC3FLAG_NONSEC) == 0) |
7507 | 0 | { |
7508 | 0 | result = need_nsec_chain(db, version, |
7509 | 0 | &nsec3chain->nsec3param, |
7510 | 0 | &buildnsecchain); |
7511 | 0 | if (result != ISC_R_SUCCESS) { |
7512 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7513 | 0 | "zone_nsec3chain:" |
7514 | 0 | "need_nsec_chain -> %s", |
7515 | 0 | isc_result_totext(result)); |
7516 | 0 | goto cleanup; |
7517 | 0 | } |
7518 | 0 | } |
7519 | | |
7520 | 0 | if (first) { |
7521 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
7522 | 0 | "zone_nsec3chain:buildnsecchain = %u", |
7523 | 0 | buildnsecchain); |
7524 | 0 | } |
7525 | |
|
7526 | 0 | dns_dbiterator_current(nsec3chain->dbiterator, &node, name); |
7527 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7528 | 0 | delegation = false; |
7529 | |
|
7530 | 0 | if (!buildnsecchain) { |
7531 | | /* |
7532 | | * Delete the NSEC3PARAM record matching this chain. |
7533 | | */ |
7534 | 0 | if (first) { |
7535 | 0 | result = fixup_nsec3param( |
7536 | 0 | db, version, nsec3chain, true, |
7537 | 0 | privatetype, ¶m_diff); |
7538 | 0 | if (result != ISC_R_SUCCESS) { |
7539 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7540 | 0 | "zone_nsec3chain:" |
7541 | 0 | "fixup_nsec3param -> %s", |
7542 | 0 | isc_result_totext(result)); |
7543 | 0 | goto cleanup; |
7544 | 0 | } |
7545 | 0 | } |
7546 | | |
7547 | | /* |
7548 | | * Delete the NSEC3 records. |
7549 | | */ |
7550 | 0 | result = deletematchingnsec3(db, version, node, name, |
7551 | 0 | &nsec3chain->nsec3param, |
7552 | 0 | &nsec3_diff); |
7553 | 0 | if (result != ISC_R_SUCCESS) { |
7554 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7555 | 0 | "zone_nsec3chain:" |
7556 | 0 | "deletematchingnsec3 -> %s", |
7557 | 0 | isc_result_totext(result)); |
7558 | 0 | goto cleanup; |
7559 | 0 | } |
7560 | 0 | goto next_removenode; |
7561 | 0 | } |
7562 | | |
7563 | 0 | if (first) { |
7564 | 0 | dns_fixedname_t ffound; |
7565 | 0 | dns_name_t *found; |
7566 | 0 | found = dns_fixedname_initname(&ffound); |
7567 | 0 | result = dns_db_find( |
7568 | 0 | db, name, version, dns_rdatatype_soa, |
7569 | 0 | DNS_DBFIND_NOWILD, 0, found, NULL, NULL); |
7570 | 0 | if ((result == DNS_R_DELEGATION || |
7571 | 0 | result == DNS_R_DNAME) && |
7572 | 0 | !dns_name_equal(name, found)) |
7573 | 0 | { |
7574 | | /* |
7575 | | * Remember the obscuring name so that |
7576 | | * we skip all obscured names. |
7577 | | */ |
7578 | 0 | dns_name_copy(found, name); |
7579 | 0 | delegation = true; |
7580 | 0 | goto next_removenode; |
7581 | 0 | } |
7582 | 0 | } |
7583 | | |
7584 | | /* |
7585 | | * Check to see if this is a bottom of zone node. |
7586 | | */ |
7587 | 0 | result = allrdatasets(db, node, version, &iterator, &seen); |
7588 | 0 | if (result == ISC_R_NOTFOUND) { |
7589 | | /* Empty node? */ |
7590 | 0 | goto next_removenode; |
7591 | 0 | } |
7592 | 0 | CHECK(result); |
7593 | |
|
7594 | 0 | dns_rdatasetiter_destroy(&iterator); |
7595 | |
|
7596 | 0 | if (!seen.rr || seen.nsec3 || seen.nsec) { |
7597 | 0 | goto next_removenode; |
7598 | 0 | } |
7599 | 0 | if ((seen.ns && !seen.soa) || seen.dname) { |
7600 | 0 | delegation = true; |
7601 | 0 | } |
7602 | | |
7603 | | /* |
7604 | | * Add a NSEC record except at the origin. |
7605 | | */ |
7606 | 0 | if (!dns_name_equal(name, dns_db_origin(db))) { |
7607 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7608 | 0 | CHECK(add_nsec(db, version, name, node, |
7609 | 0 | zone_nsecttl(zone), delegation, |
7610 | 0 | &nsec_diff)); |
7611 | 0 | signatures--; |
7612 | 0 | } |
7613 | | |
7614 | 0 | next_removenode: |
7615 | 0 | first = false; |
7616 | 0 | dns_db_detachnode(&node); |
7617 | 0 | do { |
7618 | 0 | result = dns_dbiterator_next(nsec3chain->dbiterator); |
7619 | 0 | if (result == ISC_R_NOMORE && buildnsecchain) { |
7620 | | /* |
7621 | | * The NSEC chain should now be built. |
7622 | | * We can now remove the NSEC3 chain. |
7623 | | */ |
7624 | 0 | updatensec = true; |
7625 | 0 | goto same_removechain; |
7626 | 0 | } |
7627 | 0 | if (result == ISC_R_NOMORE) { |
7628 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7629 | 0 | LOCK_ZONE(zone); |
7630 | 0 | ISC_LIST_UNLINK(zone->nsec3chain, nsec3chain, |
7631 | 0 | link); |
7632 | 0 | UNLOCK_ZONE(zone); |
7633 | 0 | ISC_LIST_APPEND(cleanup, nsec3chain, link); |
7634 | 0 | result = fixup_nsec3param( |
7635 | 0 | db, version, nsec3chain, false, |
7636 | 0 | privatetype, ¶m_diff); |
7637 | 0 | if (result != ISC_R_SUCCESS) { |
7638 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7639 | 0 | "zone_nsec3chain:" |
7640 | 0 | "fixup_nsec3param -> %s", |
7641 | 0 | isc_result_totext(result)); |
7642 | 0 | goto cleanup; |
7643 | 0 | } |
7644 | 0 | goto next_removechain; |
7645 | 0 | } else if (result != ISC_R_SUCCESS) { |
7646 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7647 | 0 | "zone_nsec3chain:" |
7648 | 0 | "dns_dbiterator_next -> %s", |
7649 | 0 | isc_result_totext(result)); |
7650 | 0 | goto cleanup; |
7651 | 0 | } else if (delegation) { |
7652 | 0 | dns_dbiterator_current(nsec3chain->dbiterator, |
7653 | 0 | &node, nextname); |
7654 | 0 | dns_db_detachnode(&node); |
7655 | 0 | if (!dns_name_issubdomain(nextname, name)) { |
7656 | 0 | break; |
7657 | 0 | } |
7658 | 0 | } else { |
7659 | 0 | break; |
7660 | 0 | } |
7661 | 0 | } while (1); |
7662 | 0 | continue; |
7663 | | |
7664 | 0 | same_removechain: |
7665 | 0 | CHECK(dns_dbiterator_first(nsec3chain->dbiterator)); |
7666 | 0 | buildnsecchain = false; |
7667 | 0 | first = true; |
7668 | 0 | continue; |
7669 | | |
7670 | 0 | next_removechain: |
7671 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7672 | 0 | nsec3chain = nextnsec3chain; |
7673 | 0 | first = true; |
7674 | 0 | } |
7675 | | |
7676 | 0 | skip_removals: |
7677 | | /* |
7678 | | * We may need to update the NSEC/NSEC3 records for the zone apex. |
7679 | | */ |
7680 | 0 | if (!ISC_LIST_EMPTY(param_diff.tuples)) { |
7681 | 0 | bool rebuild_nsec = false, rebuild_nsec3 = false; |
7682 | 0 | result = dns_db_getoriginnode(db, &node); |
7683 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
7684 | 0 | result = dns_db_allrdatasets(db, node, version, 0, 0, |
7685 | 0 | &iterator); |
7686 | 0 | if (result != ISC_R_SUCCESS) { |
7687 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7688 | 0 | "zone_nsec3chain:dns_db_allrdatasets -> %s", |
7689 | 0 | isc_result_totext(result)); |
7690 | 0 | goto cleanup; |
7691 | 0 | } |
7692 | 0 | DNS_RDATASETITER_FOREACH(iterator) { |
7693 | 0 | dns_rdataset_t rdataset = DNS_RDATASET_INIT; |
7694 | 0 | dns_rdatasetiter_current(iterator, &rdataset); |
7695 | 0 | if (rdataset.type == dns_rdatatype_nsec) { |
7696 | 0 | rebuild_nsec = true; |
7697 | 0 | } else if (rdataset.type == dns_rdatatype_nsec3param) { |
7698 | 0 | rebuild_nsec3 = true; |
7699 | 0 | } |
7700 | 0 | dns_rdataset_disassociate(&rdataset); |
7701 | 0 | } |
7702 | 0 | dns_rdatasetiter_destroy(&iterator); |
7703 | 0 | dns_db_detachnode(&node); |
7704 | |
|
7705 | 0 | if (rebuild_nsec) { |
7706 | 0 | if (nsec3chain != NULL) { |
7707 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7708 | 0 | } |
7709 | |
|
7710 | 0 | result = updatesecure(db, version, &zone->origin, |
7711 | 0 | zone_nsecttl(zone), true, |
7712 | 0 | &nsec_diff); |
7713 | 0 | if (result != ISC_R_SUCCESS) { |
7714 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7715 | 0 | "zone_nsec3chain:updatesecure -> %s", |
7716 | 0 | isc_result_totext(result)); |
7717 | 0 | goto cleanup; |
7718 | 0 | } |
7719 | 0 | } |
7720 | | |
7721 | 0 | if (rebuild_nsec3) { |
7722 | 0 | if (nsec3chain != NULL) { |
7723 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7724 | 0 | } |
7725 | |
|
7726 | 0 | result = dns_nsec3_addnsec3s( |
7727 | 0 | db, version, dns_db_origin(db), |
7728 | 0 | zone_nsecttl(zone), false, &nsec3_diff); |
7729 | 0 | if (result != ISC_R_SUCCESS) { |
7730 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7731 | 0 | "zone_nsec3chain:" |
7732 | 0 | "dns_nsec3_addnsec3s -> %s", |
7733 | 0 | isc_result_totext(result)); |
7734 | 0 | goto cleanup; |
7735 | 0 | } |
7736 | 0 | } |
7737 | 0 | } |
7738 | | |
7739 | | /* |
7740 | | * Add / update signatures for the NSEC3 records. |
7741 | | */ |
7742 | 0 | if (nsec3chain != NULL) { |
7743 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7744 | 0 | } |
7745 | 0 | result = dns__zone_updatesigs(&nsec3_diff, db, version, zone_keys, |
7746 | 0 | nkeys, zone, inception, expire, 0, now, |
7747 | 0 | &zonediff); |
7748 | 0 | if (result != ISC_R_SUCCESS) { |
7749 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7750 | 0 | "zone_nsec3chain:dns__zone_updatesigs -> %s", |
7751 | 0 | isc_result_totext(result)); |
7752 | 0 | goto cleanup; |
7753 | 0 | } |
7754 | | |
7755 | | /* |
7756 | | * We have changed the NSEC3PARAM or private RRsets |
7757 | | * above so we need to update the signatures. |
7758 | | */ |
7759 | 0 | result = dns__zone_updatesigs(¶m_diff, db, version, zone_keys, |
7760 | 0 | nkeys, zone, inception, expire, 0, now, |
7761 | 0 | &zonediff); |
7762 | 0 | if (result != ISC_R_SUCCESS) { |
7763 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7764 | 0 | "zone_nsec3chain:dns__zone_updatesigs -> %s", |
7765 | 0 | isc_result_totext(result)); |
7766 | 0 | goto cleanup; |
7767 | 0 | } |
7768 | | |
7769 | 0 | if (updatensec) { |
7770 | 0 | result = updatesecure(db, version, &zone->origin, |
7771 | 0 | zone_nsecttl(zone), false, &nsec_diff); |
7772 | 0 | if (result != ISC_R_SUCCESS) { |
7773 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7774 | 0 | "zone_nsec3chain:updatesecure -> %s", |
7775 | 0 | isc_result_totext(result)); |
7776 | 0 | goto cleanup; |
7777 | 0 | } |
7778 | 0 | } |
7779 | | |
7780 | 0 | result = dns__zone_updatesigs(&nsec_diff, db, version, zone_keys, nkeys, |
7781 | 0 | zone, inception, expire, 0, now, |
7782 | 0 | &zonediff); |
7783 | 0 | if (result != ISC_R_SUCCESS) { |
7784 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7785 | 0 | "zone_nsec3chain:dns__zone_updatesigs -> %s", |
7786 | 0 | isc_result_totext(result)); |
7787 | 0 | goto cleanup; |
7788 | 0 | } |
7789 | | |
7790 | | /* |
7791 | | * If we made no effective changes to the zone then we can just |
7792 | | * cleanup otherwise we need to increment the serial. |
7793 | | */ |
7794 | 0 | if (ISC_LIST_EMPTY(zonediff.diff->tuples)) { |
7795 | | /* |
7796 | | * No need to call dns_db_closeversion() here as it is |
7797 | | * called with commit = true below. |
7798 | | */ |
7799 | 0 | goto closeversion; |
7800 | 0 | } |
7801 | | |
7802 | 0 | result = del_sigs(zone, db, version, &zone->origin, dns_rdatatype_soa, |
7803 | 0 | &zonediff, zone_keys, nkeys, now, false); |
7804 | 0 | if (result != ISC_R_SUCCESS) { |
7805 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7806 | 0 | "zone_nsec3chain:del_sigs -> %s", |
7807 | 0 | isc_result_totext(result)); |
7808 | 0 | goto cleanup; |
7809 | 0 | } |
7810 | | |
7811 | 0 | result = update_soa_serial(zone, db, version, zonediff.diff, zone->mctx, |
7812 | 0 | zone->updatemethod); |
7813 | 0 | if (result != ISC_R_SUCCESS) { |
7814 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7815 | 0 | "zone_nsec3chain:update_soa_serial -> %s", |
7816 | 0 | isc_result_totext(result)); |
7817 | 0 | goto cleanup; |
7818 | 0 | } |
7819 | | |
7820 | 0 | result = add_sigs(db, version, &zone->origin, zone, dns_rdatatype_soa, |
7821 | 0 | zonediff.diff, zone_keys, nkeys, zone->mctx, now, |
7822 | 0 | inception, soaexpire); |
7823 | 0 | if (result != ISC_R_SUCCESS) { |
7824 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
7825 | 0 | "zone_nsec3chain:add_sigs -> %s", |
7826 | 0 | isc_result_totext(result)); |
7827 | 0 | goto cleanup; |
7828 | 0 | } |
7829 | | |
7830 | | /* Write changes to journal file. */ |
7831 | 0 | CHECK(zone_journal(zone, zonediff.diff, NULL, "zone_nsec3chain")); |
7832 | |
|
7833 | 0 | LOCK_ZONE(zone); |
7834 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
7835 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY); |
7836 | 0 | UNLOCK_ZONE(zone); |
7837 | |
|
7838 | 0 | closeversion: |
7839 | | /* |
7840 | | * Pause all iterators so that dns_db_closeversion() can succeed. |
7841 | | */ |
7842 | 0 | LOCK_ZONE(zone); |
7843 | 0 | ISC_LIST_FOREACH(zone->nsec3chain, chain, link) { |
7844 | 0 | dns_dbiterator_pause(chain->dbiterator); |
7845 | 0 | } |
7846 | 0 | UNLOCK_ZONE(zone); |
7847 | | |
7848 | | /* |
7849 | | * Everything has succeeded. Commit the changes. |
7850 | | * Unconditionally commit as zonediff.offline not checked above. |
7851 | | */ |
7852 | 0 | dns_db_closeversion(db, &version, true); |
7853 | | |
7854 | | /* |
7855 | | * Everything succeeded so we can clean these up now. |
7856 | | */ |
7857 | 0 | ISC_LIST_FOREACH(cleanup, chain, link) { |
7858 | 0 | ISC_LIST_UNLINK(cleanup, chain, link); |
7859 | 0 | dns_db_detach(&chain->db); |
7860 | 0 | dns_dbiterator_destroy(&chain->dbiterator); |
7861 | 0 | isc_mem_put(zone->mctx, chain, sizeof *chain); |
7862 | 0 | } |
7863 | |
|
7864 | 0 | LOCK_ZONE(zone); |
7865 | 0 | dns__zone_set_resigntime(zone); |
7866 | 0 | UNLOCK_ZONE(zone); |
7867 | |
|
7868 | 0 | cleanup: |
7869 | 0 | if (result != ISC_R_SUCCESS) { |
7870 | 0 | dnssec_log(zone, ISC_LOG_ERROR, "zone_nsec3chain: %s", |
7871 | 0 | isc_result_totext(result)); |
7872 | 0 | } |
7873 | | |
7874 | | /* |
7875 | | * On error roll back the current nsec3chain. |
7876 | | */ |
7877 | 0 | if (result != ISC_R_SUCCESS && nsec3chain != NULL) { |
7878 | 0 | if (nsec3chain->done) { |
7879 | 0 | dns_db_detach(&nsec3chain->db); |
7880 | 0 | dns_dbiterator_destroy(&nsec3chain->dbiterator); |
7881 | 0 | isc_mem_put(zone->mctx, nsec3chain, sizeof *nsec3chain); |
7882 | 0 | } else { |
7883 | 0 | result = dns_dbiterator_first(nsec3chain->dbiterator); |
7884 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
7885 | 0 | dns_dbiterator_pause(nsec3chain->dbiterator); |
7886 | 0 | nsec3chain->delete_nsec = nsec3chain->save_delete_nsec; |
7887 | 0 | } |
7888 | 0 | } |
7889 | | |
7890 | | /* |
7891 | | * Rollback the cleanup list. |
7892 | | */ |
7893 | 0 | ISC_LIST_FOREACH_REV(cleanup, chain, link) { |
7894 | 0 | ISC_LIST_UNLINK(cleanup, chain, link); |
7895 | 0 | if (chain->done) { |
7896 | 0 | dns_db_detach(&chain->db); |
7897 | 0 | dns_dbiterator_destroy(&chain->dbiterator); |
7898 | 0 | isc_mem_put(zone->mctx, chain, sizeof *chain); |
7899 | 0 | } else { |
7900 | 0 | LOCK_ZONE(zone); |
7901 | 0 | ISC_LIST_PREPEND(zone->nsec3chain, chain, link); |
7902 | 0 | UNLOCK_ZONE(zone); |
7903 | 0 | result = dns_dbiterator_first(chain->dbiterator); |
7904 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
7905 | 0 | dns_dbiterator_pause(chain->dbiterator); |
7906 | 0 | chain->delete_nsec = chain->save_delete_nsec; |
7907 | 0 | } |
7908 | 0 | } |
7909 | |
|
7910 | 0 | LOCK_ZONE(zone); |
7911 | 0 | ISC_LIST_FOREACH(zone->nsec3chain, chain, link) { |
7912 | 0 | dns_dbiterator_pause(chain->dbiterator); |
7913 | 0 | } |
7914 | 0 | UNLOCK_ZONE(zone); |
7915 | |
|
7916 | 0 | dns_diff_clear(¶m_diff); |
7917 | 0 | dns_diff_clear(&nsec3_diff); |
7918 | 0 | dns_diff_clear(&nsec_diff); |
7919 | 0 | dns_diff_clear(&_sig_diff); |
7920 | |
|
7921 | 0 | if (iterator != NULL) { |
7922 | 0 | dns_rdatasetiter_destroy(&iterator); |
7923 | 0 | } |
7924 | |
|
7925 | 0 | for (i = 0; i < nkeys; i++) { |
7926 | 0 | dst_key_free(&zone_keys[i]); |
7927 | 0 | } |
7928 | |
|
7929 | 0 | if (node != NULL) { |
7930 | 0 | dns_db_detachnode(&node); |
7931 | 0 | } |
7932 | 0 | if (version != NULL) { |
7933 | 0 | dns_db_closeversion(db, &version, false); |
7934 | 0 | dns_db_detach(&db); |
7935 | 0 | } else if (db != NULL) { |
7936 | 0 | dns_db_detach(&db); |
7937 | 0 | } |
7938 | |
|
7939 | 0 | LOCK_ZONE(zone); |
7940 | 0 | if (ISC_LIST_HEAD(zone->nsec3chain) != NULL) { |
7941 | 0 | isc_interval_t interval; |
7942 | 0 | if (zone->update_disabled || result != ISC_R_SUCCESS) { |
7943 | 0 | isc_interval_set(&interval, 60, 0); /* 1 minute */ |
7944 | 0 | } else { |
7945 | 0 | isc_interval_set(&interval, 0, 10000000); /* 10 ms */ |
7946 | 0 | } |
7947 | 0 | isc_time_nowplusinterval(&zone->nsec3chaintime, &interval); |
7948 | 0 | } else { |
7949 | 0 | isc_time_settoepoch(&zone->nsec3chaintime); |
7950 | 0 | } |
7951 | 0 | UNLOCK_ZONE(zone); |
7952 | |
|
7953 | 0 | INSIST(version == NULL); |
7954 | 0 | } |
7955 | | |
7956 | | /*% |
7957 | | * Delete all RRSIG records with the given algorithm and keyid. |
7958 | | * Remove the NSEC record and RRSIGs if nkeys is zero. |
7959 | | * If all remaining RRsets are signed with the given algorithm |
7960 | | * set *has_algp to true. |
7961 | | */ |
7962 | | static isc_result_t |
7963 | | del_sig(dns_db_t *db, dns_dbversion_t *version, dns_name_t *name, |
7964 | | dns_dbnode_t *node, unsigned int nkeys, dst_algorithm_t algorithm, |
7965 | 0 | uint16_t keyid, bool *has_algp, dns_diff_t *diff) { |
7966 | 0 | dns_rdata_rrsig_t rrsig; |
7967 | 0 | dns_rdataset_t rdataset; |
7968 | 0 | dns_rdatasetiter_t *iterator = NULL; |
7969 | 0 | isc_result_t result; |
7970 | 0 | bool alg_missed = false; |
7971 | 0 | bool alg_found = false; |
7972 | |
|
7973 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
7974 | 0 | dns_name_format(name, namebuf, sizeof(namebuf)); |
7975 | |
|
7976 | 0 | result = dns_db_allrdatasets(db, node, version, 0, 0, &iterator); |
7977 | 0 | if (result != ISC_R_SUCCESS) { |
7978 | 0 | if (result == ISC_R_NOTFOUND) { |
7979 | 0 | result = ISC_R_SUCCESS; |
7980 | 0 | } |
7981 | 0 | return result; |
7982 | 0 | } |
7983 | | |
7984 | 0 | dns_rdataset_init(&rdataset); |
7985 | 0 | DNS_RDATASETITER_FOREACH(iterator) { |
7986 | 0 | bool has_alg = false; |
7987 | 0 | dns_rdatasetiter_current(iterator, &rdataset); |
7988 | 0 | if (nkeys == 0 && rdataset.type == dns_rdatatype_nsec) { |
7989 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
7990 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
7991 | 0 | dns_rdataset_current(&rdataset, &rdata); |
7992 | 0 | CHECK(update_one_rr(db, version, diff, |
7993 | 0 | DNS_DIFFOP_DEL, name, |
7994 | 0 | rdataset.ttl, &rdata)); |
7995 | 0 | } |
7996 | 0 | dns_rdataset_disassociate(&rdataset); |
7997 | 0 | continue; |
7998 | 0 | } |
7999 | 0 | if (rdataset.type != dns_rdatatype_rrsig) { |
8000 | 0 | dns_rdataset_disassociate(&rdataset); |
8001 | 0 | continue; |
8002 | 0 | } |
8003 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
8004 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
8005 | 0 | dst_algorithm_t sigalg; |
8006 | |
|
8007 | 0 | dns_rdataset_current(&rdataset, &rdata); |
8008 | 0 | CHECK(dns_rdata_tostruct(&rdata, &rrsig, NULL)); |
8009 | |
|
8010 | 0 | sigalg = dst_algorithm_fromdata( |
8011 | 0 | rrsig.algorithm, rrsig.signature, rrsig.siglen); |
8012 | 0 | if (nkeys != 0 && |
8013 | 0 | (sigalg != algorithm || rrsig.keyid != keyid)) |
8014 | 0 | { |
8015 | 0 | if (sigalg == algorithm) { |
8016 | 0 | has_alg = true; |
8017 | 0 | } |
8018 | 0 | continue; |
8019 | 0 | } |
8020 | 0 | CHECK(update_one_rr(db, version, diff, |
8021 | 0 | DNS_DIFFOP_DELRESIGN, name, |
8022 | 0 | rdataset.ttl, &rdata)); |
8023 | 0 | } |
8024 | 0 | dns_rdataset_disassociate(&rdataset); |
8025 | | |
8026 | | /* |
8027 | | * After deleting, if there's still a signature for |
8028 | | * 'algorithm', set alg_found; if not, set alg_missed. |
8029 | | */ |
8030 | 0 | if (has_alg) { |
8031 | 0 | alg_found = true; |
8032 | 0 | } else { |
8033 | 0 | alg_missed = true; |
8034 | 0 | } |
8035 | 0 | } |
8036 | | |
8037 | | /* |
8038 | | * Set `has_algp` if the algorithm was found in every RRset: |
8039 | | * i.e., found in at least one, and not missing from any. |
8040 | | */ |
8041 | 0 | *has_algp = (alg_found && !alg_missed); |
8042 | 0 | cleanup: |
8043 | 0 | dns_rdataset_cleanup(&rdataset); |
8044 | 0 | dns_rdatasetiter_destroy(&iterator); |
8045 | 0 | return result; |
8046 | 0 | } |
8047 | | |
8048 | | /* |
8049 | | * Prevent the zone entering a inconsistent state where |
8050 | | * NSEC only DNSKEYs are present with NSEC3 chains. |
8051 | | */ |
8052 | | bool |
8053 | | dns_zone_check_dnskey_nsec3(dns_zone_t *zone, dns_db_t *db, |
8054 | | dns_dbversion_t *ver, dns_diff_t *diff, |
8055 | 0 | dst_key_t **keys, unsigned int numkeys) { |
8056 | 0 | uint8_t alg; |
8057 | 0 | dns_rdatatype_t privatetype; |
8058 | 0 | bool nseconly = false, nsec3 = false; |
8059 | 0 | isc_result_t result; |
8060 | |
|
8061 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
8062 | 0 | REQUIRE(db != NULL); |
8063 | |
|
8064 | 0 | privatetype = dns_zone_getprivatetype(zone); |
8065 | | |
8066 | | /* Scan the tuples for an NSEC-only DNSKEY */ |
8067 | 0 | if (diff != NULL) { |
8068 | 0 | ISC_LIST_FOREACH(diff->tuples, tuple, link) { |
8069 | 0 | if (nseconly && nsec3) { |
8070 | 0 | break; |
8071 | 0 | } |
8072 | | |
8073 | 0 | if (tuple->op != DNS_DIFFOP_ADD) { |
8074 | 0 | continue; |
8075 | 0 | } |
8076 | | |
8077 | 0 | if (tuple->rdata.type == dns_rdatatype_nsec3param) { |
8078 | 0 | nsec3 = true; |
8079 | 0 | } |
8080 | |
|
8081 | 0 | if (tuple->rdata.type != dns_rdatatype_dnskey) { |
8082 | 0 | continue; |
8083 | 0 | } |
8084 | | |
8085 | 0 | alg = tuple->rdata.data[3]; |
8086 | 0 | if (alg == DNS_KEYALG_RSAMD5 || alg == DNS_KEYALG_DSA || |
8087 | 0 | alg == DNS_KEYALG_RSASHA1) |
8088 | 0 | { |
8089 | 0 | nseconly = true; |
8090 | 0 | } |
8091 | 0 | } |
8092 | 0 | } |
8093 | | /* Scan the zone keys for an NSEC-only DNSKEY */ |
8094 | 0 | if (keys != NULL && !nseconly) { |
8095 | 0 | for (unsigned int i = 0; i < numkeys; i++) { |
8096 | 0 | alg = dst_key_alg(keys[i]); |
8097 | 0 | if (alg == DNS_KEYALG_RSAMD5 || alg == DNS_KEYALG_DSA || |
8098 | 0 | alg == DNS_KEYALG_RSASHA1) |
8099 | 0 | { |
8100 | 0 | nseconly = true; |
8101 | 0 | break; |
8102 | 0 | } |
8103 | 0 | } |
8104 | 0 | } |
8105 | | |
8106 | | /* Check DB for NSEC-only DNSKEY */ |
8107 | 0 | if (!nseconly) { |
8108 | 0 | result = dns_nsec_nseconly(db, ver, diff, &nseconly); |
8109 | | /* |
8110 | | * Adding an NSEC3PARAM record can proceed without a |
8111 | | * DNSKEY (it will trigger a delayed change), so we can |
8112 | | * ignore ISC_R_NOTFOUND here. |
8113 | | */ |
8114 | 0 | if (result == ISC_R_NOTFOUND) { |
8115 | 0 | result = ISC_R_SUCCESS; |
8116 | 0 | } |
8117 | 0 | CHECK(result); |
8118 | 0 | } |
8119 | | |
8120 | | /* Check existing DB for NSEC3 */ |
8121 | 0 | if (!nsec3) { |
8122 | 0 | CHECK(dns_nsec3_activex(db, ver, false, privatetype, &nsec3)); |
8123 | 0 | } |
8124 | | |
8125 | | /* Check kasp for NSEC3PARAM settings */ |
8126 | 0 | if (!nsec3) { |
8127 | 0 | dns_kasp_t *kasp = zone->kasp; |
8128 | 0 | if (kasp != NULL) { |
8129 | 0 | nsec3 = dns_kasp_nsec3(kasp); |
8130 | 0 | } |
8131 | 0 | } |
8132 | | |
8133 | | /* Refuse to allow NSEC3 with NSEC-only keys */ |
8134 | 0 | if (nseconly && nsec3) { |
8135 | 0 | goto cleanup; |
8136 | 0 | } |
8137 | | |
8138 | 0 | return true; |
8139 | | |
8140 | 0 | cleanup: |
8141 | 0 | return false; |
8142 | 0 | } |
8143 | | |
8144 | | /* |
8145 | | * Incrementally sign the zone using the keys requested. |
8146 | | * Builds the NSEC chain if required. |
8147 | | */ |
8148 | | static void |
8149 | 0 | zone_sign(dns_zone_t *zone) { |
8150 | 0 | dns_db_t *db = NULL; |
8151 | 0 | dns_dbnode_t *node = NULL; |
8152 | 0 | dns_dbversion_t *version = NULL; |
8153 | 0 | dns_diff_t _sig_diff; |
8154 | 0 | dns_diff_t post_diff; |
8155 | 0 | dns__zonediff_t zonediff; |
8156 | 0 | dns_fixedname_t fixed; |
8157 | 0 | dns_fixedname_t nextfixed; |
8158 | 0 | dns_kasp_t *kasp = NULL; |
8159 | 0 | dns_name_t *name = NULL, *nextname = NULL; |
8160 | 0 | dns_rdataset_t rdataset; |
8161 | 0 | dns_signing_t *signing = NULL; |
8162 | 0 | dns_signinglist_t cleanup; |
8163 | 0 | dst_key_t *zone_keys[DNS_MAXZONEKEYS]; |
8164 | 0 | int32_t signatures; |
8165 | 0 | bool is_ksk, is_zsk; |
8166 | 0 | bool with_ksk, with_zsk; |
8167 | 0 | bool commit = false; |
8168 | 0 | bool is_bottom_of_zone; |
8169 | 0 | bool build_nsec = false; |
8170 | 0 | bool build_nsec3 = false; |
8171 | 0 | bool use_kasp = false; |
8172 | 0 | bool first; |
8173 | 0 | isc_result_t result; |
8174 | 0 | isc_stdtime_t now, inception, soaexpire, expire; |
8175 | 0 | unsigned int i, j; |
8176 | 0 | unsigned int nkeys = 0; |
8177 | 0 | uint32_t nodes; |
8178 | |
|
8179 | 0 | ENTER; |
8180 | |
|
8181 | 0 | dns_rdataset_init(&rdataset); |
8182 | 0 | name = dns_fixedname_initname(&fixed); |
8183 | 0 | nextname = dns_fixedname_initname(&nextfixed); |
8184 | 0 | dns_diff_init(zone->mctx, &_sig_diff); |
8185 | 0 | dns_diff_init(zone->mctx, &post_diff); |
8186 | 0 | zonediff_init(&zonediff, &_sig_diff); |
8187 | 0 | ISC_LIST_INIT(cleanup); |
8188 | | |
8189 | | /* |
8190 | | * Updates are disabled. Pause for 1 minute. |
8191 | | */ |
8192 | 0 | if (zone->update_disabled) { |
8193 | 0 | result = ISC_R_FAILURE; |
8194 | 0 | goto done; |
8195 | 0 | } |
8196 | | |
8197 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
8198 | 0 | if (zone->db != NULL) { |
8199 | 0 | dns_db_attach(zone->db, &db); |
8200 | 0 | } |
8201 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
8202 | 0 | if (db == NULL) { |
8203 | 0 | result = ISC_R_FAILURE; |
8204 | 0 | goto done; |
8205 | 0 | } |
8206 | | |
8207 | 0 | result = dns_db_newversion(db, &version); |
8208 | 0 | if (result != ISC_R_SUCCESS) { |
8209 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
8210 | 0 | "zone_sign:dns_db_newversion -> %s", |
8211 | 0 | isc_result_totext(result)); |
8212 | 0 | goto done; |
8213 | 0 | } |
8214 | | |
8215 | 0 | now = isc_stdtime_now(); |
8216 | |
|
8217 | 0 | result = dns_zone_findkeys(zone, db, version, now, zone->mctx, |
8218 | 0 | DNS_MAXZONEKEYS, zone_keys, &nkeys); |
8219 | 0 | if (result != ISC_R_SUCCESS) { |
8220 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
8221 | 0 | "zone_sign:dns_zone_findkeys -> %s", |
8222 | 0 | isc_result_totext(result)); |
8223 | 0 | goto done; |
8224 | 0 | } |
8225 | | |
8226 | 0 | kasp = zone->kasp; |
8227 | |
|
8228 | 0 | calculate_rrsig_validity(zone, now, &inception, &soaexpire, NULL, |
8229 | 0 | &expire); |
8230 | | |
8231 | | /* |
8232 | | * We keep pulling nodes off each iterator in turn until |
8233 | | * we have no more nodes to pull off or we reach the limits |
8234 | | * for this quantum. |
8235 | | */ |
8236 | 0 | nodes = zone->nodes; |
8237 | 0 | signatures = zone->signatures; |
8238 | 0 | signing = ISC_LIST_HEAD(zone->signing); |
8239 | 0 | first = true; |
8240 | |
|
8241 | 0 | if (kasp != NULL) { |
8242 | 0 | use_kasp = true; |
8243 | 0 | } |
8244 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), "zone_sign:use kasp -> %s", |
8245 | 0 | use_kasp ? "yes" : "no"); |
8246 | | |
8247 | | /* Determine which type of chain to build */ |
8248 | 0 | CHECK(dns_private_chains(db, version, zone->privatetype, &build_nsec, |
8249 | 0 | &build_nsec3)); |
8250 | 0 | if (!build_nsec && !build_nsec3) { |
8251 | 0 | if (use_kasp) { |
8252 | 0 | build_nsec3 = dns_kasp_nsec3(kasp); |
8253 | 0 | if (!dns_zone_check_dnskey_nsec3( |
8254 | 0 | zone, db, version, NULL, |
8255 | 0 | (dst_key_t **)&zone_keys, nkeys)) |
8256 | 0 | { |
8257 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
8258 | 0 | "wait building NSEC3 chain until " |
8259 | 0 | "NSEC only DNSKEYs are removed"); |
8260 | 0 | build_nsec3 = false; |
8261 | 0 | } |
8262 | 0 | build_nsec = !build_nsec3; |
8263 | 0 | } else { |
8264 | | /* If neither chain is found, default to NSEC */ |
8265 | 0 | build_nsec = true; |
8266 | 0 | } |
8267 | 0 | } |
8268 | |
|
8269 | 0 | while (signing != NULL && nodes-- > 0 && signatures > 0) { |
8270 | 0 | dns_signing_t *nextsigning = ISC_LIST_NEXT(signing, link); |
8271 | 0 | bool has_alg = false; |
8272 | |
|
8273 | 0 | dns_dbiterator_pause(signing->dbiterator); |
8274 | |
|
8275 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
8276 | 0 | if (signing->done || signing->db != zone->db) { |
8277 | | /* |
8278 | | * The zone has been reloaded. We will have to |
8279 | | * created new signings as part of the reload |
8280 | | * process so we can destroy this one. |
8281 | | */ |
8282 | 0 | ISC_LIST_UNLINK(zone->signing, signing, link); |
8283 | 0 | ISC_LIST_APPEND(cleanup, signing, link); |
8284 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
8285 | 0 | goto next_signing; |
8286 | 0 | } |
8287 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
8288 | |
|
8289 | 0 | if (signing->db != db) { |
8290 | 0 | goto next_signing; |
8291 | 0 | } |
8292 | | |
8293 | 0 | is_bottom_of_zone = false; |
8294 | |
|
8295 | 0 | if (first && signing->deleteit) { |
8296 | | /* |
8297 | | * Remove the key we are deleting from consideration. |
8298 | | */ |
8299 | 0 | for (i = 0, j = 0; i < nkeys; i++) { |
8300 | | /* |
8301 | | * Find the key we want to remove. |
8302 | | */ |
8303 | 0 | if (ALG(zone_keys[i]) == signing->algorithm && |
8304 | 0 | dst_key_id(zone_keys[i]) == signing->keyid) |
8305 | 0 | { |
8306 | 0 | dst_key_free(&zone_keys[i]); |
8307 | 0 | continue; |
8308 | 0 | } |
8309 | 0 | zone_keys[j] = zone_keys[i]; |
8310 | 0 | j++; |
8311 | 0 | } |
8312 | 0 | for (i = j; i < nkeys; i++) { |
8313 | 0 | zone_keys[i] = NULL; |
8314 | 0 | } |
8315 | 0 | nkeys = j; |
8316 | 0 | } |
8317 | |
|
8318 | 0 | dns_dbiterator_current(signing->dbiterator, &node, name); |
8319 | |
|
8320 | 0 | if (signing->deleteit) { |
8321 | 0 | dns_dbiterator_pause(signing->dbiterator); |
8322 | 0 | CHECK(del_sig(db, version, name, node, nkeys, |
8323 | 0 | signing->algorithm, signing->keyid, |
8324 | 0 | &has_alg, zonediff.diff)); |
8325 | 0 | } |
8326 | | |
8327 | | /* |
8328 | | * On the first pass we need to check if the current node |
8329 | | * has not been obscured. |
8330 | | */ |
8331 | 0 | if (first) { |
8332 | 0 | dns_fixedname_t ffound; |
8333 | 0 | dns_name_t *found; |
8334 | 0 | found = dns_fixedname_initname(&ffound); |
8335 | 0 | result = dns_db_find( |
8336 | 0 | db, name, version, dns_rdatatype_soa, |
8337 | 0 | DNS_DBFIND_NOWILD, 0, found, NULL, NULL); |
8338 | 0 | if ((result == DNS_R_DELEGATION || |
8339 | 0 | result == DNS_R_DNAME) && |
8340 | 0 | !dns_name_equal(name, found)) |
8341 | 0 | { |
8342 | | /* |
8343 | | * Remember the obscuring name so that |
8344 | | * we skip all obscured names. |
8345 | | */ |
8346 | 0 | dns_name_copy(found, name); |
8347 | 0 | is_bottom_of_zone = true; |
8348 | 0 | goto next_node; |
8349 | 0 | } |
8350 | 0 | } |
8351 | | |
8352 | | /* |
8353 | | * Process one node. |
8354 | | */ |
8355 | 0 | with_ksk = false; |
8356 | 0 | with_zsk = false; |
8357 | 0 | dns_dbiterator_pause(signing->dbiterator); |
8358 | |
|
8359 | 0 | CHECK(check_if_bottom_of_zone(db, node, version, |
8360 | 0 | &is_bottom_of_zone)); |
8361 | |
|
8362 | 0 | for (i = 0; !has_alg && i < nkeys; i++) { |
8363 | 0 | bool both = false; |
8364 | | /* |
8365 | | * Find the keys we want to sign with. |
8366 | | */ |
8367 | 0 | if (!dst_key_isprivate(zone_keys[i])) { |
8368 | 0 | continue; |
8369 | 0 | } |
8370 | 0 | if (dst_key_inactive(zone_keys[i])) { |
8371 | 0 | continue; |
8372 | 0 | } |
8373 | | |
8374 | | /* |
8375 | | * When adding look for the specific key. |
8376 | | */ |
8377 | 0 | if (!signing->deleteit && |
8378 | 0 | (ALG(zone_keys[i]) != signing->algorithm || |
8379 | 0 | dst_key_id(zone_keys[i]) != signing->keyid)) |
8380 | 0 | { |
8381 | 0 | continue; |
8382 | 0 | } |
8383 | | |
8384 | | /* |
8385 | | * When deleting make sure we are properly signed |
8386 | | * with the algorithm that was being removed. |
8387 | | */ |
8388 | 0 | if (signing->deleteit && |
8389 | 0 | ALG(zone_keys[i]) != signing->algorithm) |
8390 | 0 | { |
8391 | 0 | continue; |
8392 | 0 | } |
8393 | | |
8394 | | /* |
8395 | | * We do KSK processing. |
8396 | | */ |
8397 | 0 | if (use_kasp) { |
8398 | | /* |
8399 | | * A dnssec-policy is found. Check what |
8400 | | * RRsets this key can sign. |
8401 | | */ |
8402 | 0 | isc_result_t kresult; |
8403 | 0 | is_ksk = false; |
8404 | 0 | kresult = dst_key_getbool( |
8405 | 0 | zone_keys[i], DST_BOOL_KSK, &is_ksk); |
8406 | 0 | if (kresult != ISC_R_SUCCESS) { |
8407 | 0 | if (KSK(zone_keys[i])) { |
8408 | 0 | is_ksk = true; |
8409 | 0 | } |
8410 | 0 | } |
8411 | |
|
8412 | 0 | is_zsk = false; |
8413 | 0 | kresult = dst_key_getbool( |
8414 | 0 | zone_keys[i], DST_BOOL_ZSK, &is_zsk); |
8415 | 0 | if (kresult != ISC_R_SUCCESS) { |
8416 | 0 | if (!KSK(zone_keys[i])) { |
8417 | 0 | is_zsk = true; |
8418 | 0 | } |
8419 | 0 | } |
8420 | 0 | both = true; |
8421 | 0 | } else { |
8422 | 0 | is_ksk = KSK(zone_keys[i]); |
8423 | 0 | is_zsk = !is_ksk; |
8424 | | |
8425 | | /* |
8426 | | * Don't consider inactive keys, however the key |
8427 | | * may be temporary offline, so do consider KSKs |
8428 | | * which private key files are unavailable. |
8429 | | */ |
8430 | 0 | both = dst_key_have_ksk_and_zsk( |
8431 | 0 | zone_keys, nkeys, i, false, is_ksk, |
8432 | 0 | is_zsk, NULL, NULL); |
8433 | 0 | if (both || REVOKE(zone_keys[i])) { |
8434 | 0 | is_ksk = KSK(zone_keys[i]); |
8435 | 0 | is_zsk = !KSK(zone_keys[i]); |
8436 | 0 | } else { |
8437 | 0 | is_ksk = false; |
8438 | 0 | is_zsk = false; |
8439 | 0 | } |
8440 | 0 | } |
8441 | | |
8442 | | /* |
8443 | | * If deleting signatures, we need to ensure that |
8444 | | * the RRset is still signed at least once by a |
8445 | | * KSK and a ZSK. |
8446 | | */ |
8447 | 0 | if (signing->deleteit && is_zsk && with_zsk) { |
8448 | 0 | continue; |
8449 | 0 | } |
8450 | | |
8451 | 0 | if (signing->deleteit && is_ksk && with_ksk) { |
8452 | 0 | continue; |
8453 | 0 | } |
8454 | | |
8455 | 0 | CHECK(sign_a_node( |
8456 | 0 | db, zone, name, node, version, build_nsec3, |
8457 | 0 | build_nsec, zone_keys[i], now, inception, |
8458 | 0 | expire, zone_nsecttl(zone), both, is_ksk, |
8459 | 0 | is_zsk, signing->fullsign, is_bottom_of_zone, |
8460 | 0 | zonediff.diff, &signatures, zone->mctx)); |
8461 | | /* |
8462 | | * If we are adding we are done. Look for other keys |
8463 | | * of the same algorithm if deleting. |
8464 | | */ |
8465 | 0 | if (!signing->deleteit) { |
8466 | 0 | break; |
8467 | 0 | } |
8468 | 0 | if (is_zsk) { |
8469 | 0 | with_zsk = true; |
8470 | 0 | } |
8471 | 0 | if (is_ksk) { |
8472 | 0 | with_ksk = true; |
8473 | 0 | } |
8474 | 0 | } |
8475 | | |
8476 | | /* |
8477 | | * Go onto next node. |
8478 | | */ |
8479 | 0 | next_node: |
8480 | 0 | first = false; |
8481 | 0 | dns_db_detachnode(&node); |
8482 | 0 | do { |
8483 | 0 | result = dns_dbiterator_next(signing->dbiterator); |
8484 | 0 | if (result == ISC_R_NOMORE) { |
8485 | 0 | ISC_LIST_UNLINK(zone->signing, signing, link); |
8486 | 0 | ISC_LIST_APPEND(cleanup, signing, link); |
8487 | 0 | dns_dbiterator_pause(signing->dbiterator); |
8488 | 0 | if (nkeys != 0 && build_nsec) { |
8489 | | /* |
8490 | | * We have finished regenerating the |
8491 | | * zone with a zone signing key. |
8492 | | * The NSEC chain is now complete and |
8493 | | * there is a full set of signatures |
8494 | | * for the zone. We can now clear the |
8495 | | * OPT bit from the NSEC record. |
8496 | | */ |
8497 | 0 | result = updatesecure( |
8498 | 0 | db, version, &zone->origin, |
8499 | 0 | zone_nsecttl(zone), false, |
8500 | 0 | &post_diff); |
8501 | 0 | if (result != ISC_R_SUCCESS) { |
8502 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
8503 | 0 | "updatesecure -> %s", |
8504 | 0 | isc_result_totext( |
8505 | 0 | result)); |
8506 | 0 | goto done; |
8507 | 0 | } |
8508 | 0 | } |
8509 | 0 | result = updatesignwithkey( |
8510 | 0 | zone, signing, version, build_nsec3, |
8511 | 0 | zone_nsecttl(zone), &post_diff); |
8512 | 0 | if (result != ISC_R_SUCCESS) { |
8513 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
8514 | 0 | "updatesignwithkey -> %s", |
8515 | 0 | isc_result_totext(result)); |
8516 | 0 | goto done; |
8517 | 0 | } |
8518 | 0 | build_nsec = false; |
8519 | 0 | goto next_signing; |
8520 | 0 | } else if (result != ISC_R_SUCCESS) { |
8521 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
8522 | 0 | "zone_sign:" |
8523 | 0 | "dns_dbiterator_next -> %s", |
8524 | 0 | isc_result_totext(result)); |
8525 | 0 | goto done; |
8526 | 0 | } else if (is_bottom_of_zone) { |
8527 | 0 | dns_dbiterator_current(signing->dbiterator, |
8528 | 0 | &node, nextname); |
8529 | 0 | dns_db_detachnode(&node); |
8530 | 0 | if (!dns_name_issubdomain(nextname, name)) { |
8531 | 0 | break; |
8532 | 0 | } |
8533 | 0 | } else { |
8534 | 0 | break; |
8535 | 0 | } |
8536 | 0 | } while (1); |
8537 | 0 | continue; |
8538 | | |
8539 | 0 | next_signing: |
8540 | 0 | dns_dbiterator_pause(signing->dbiterator); |
8541 | 0 | signing = nextsigning; |
8542 | 0 | first = true; |
8543 | 0 | } |
8544 | | |
8545 | 0 | if (ISC_LIST_HEAD(post_diff.tuples) != NULL) { |
8546 | 0 | result = dns__zone_updatesigs(&post_diff, db, version, |
8547 | 0 | zone_keys, nkeys, zone, inception, |
8548 | 0 | expire, 0, now, &zonediff); |
8549 | 0 | if (result != ISC_R_SUCCESS) { |
8550 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
8551 | 0 | "zone_sign:dns__zone_updatesigs -> %s", |
8552 | 0 | isc_result_totext(result)); |
8553 | 0 | goto done; |
8554 | 0 | } |
8555 | 0 | } |
8556 | | |
8557 | | /* |
8558 | | * Have we changed anything? |
8559 | | */ |
8560 | 0 | if (ISC_LIST_EMPTY(zonediff.diff->tuples)) { |
8561 | 0 | if (zonediff.offline) { |
8562 | 0 | commit = true; |
8563 | 0 | } |
8564 | 0 | result = ISC_R_SUCCESS; |
8565 | 0 | goto pauseall; |
8566 | 0 | } |
8567 | | |
8568 | 0 | commit = true; |
8569 | |
|
8570 | 0 | result = del_sigs(zone, db, version, &zone->origin, dns_rdatatype_soa, |
8571 | 0 | &zonediff, zone_keys, nkeys, now, false); |
8572 | 0 | if (result != ISC_R_SUCCESS) { |
8573 | 0 | dnssec_log(zone, ISC_LOG_ERROR, "zone_sign:del_sigs -> %s", |
8574 | 0 | isc_result_totext(result)); |
8575 | 0 | goto done; |
8576 | 0 | } |
8577 | | |
8578 | 0 | result = update_soa_serial(zone, db, version, zonediff.diff, zone->mctx, |
8579 | 0 | zone->updatemethod); |
8580 | 0 | if (result != ISC_R_SUCCESS) { |
8581 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
8582 | 0 | "zone_sign:update_soa_serial -> %s", |
8583 | 0 | isc_result_totext(result)); |
8584 | 0 | goto done; |
8585 | 0 | } |
8586 | | |
8587 | | /* |
8588 | | * Generate maximum life time signatures so that the above loop |
8589 | | * termination is sensible. |
8590 | | */ |
8591 | 0 | result = add_sigs(db, version, &zone->origin, zone, dns_rdatatype_soa, |
8592 | 0 | zonediff.diff, zone_keys, nkeys, zone->mctx, now, |
8593 | 0 | inception, soaexpire); |
8594 | 0 | if (result != ISC_R_SUCCESS) { |
8595 | 0 | dnssec_log(zone, ISC_LOG_ERROR, "zone_sign:add_sigs -> %s", |
8596 | 0 | isc_result_totext(result)); |
8597 | 0 | goto done; |
8598 | 0 | } |
8599 | | |
8600 | | /* |
8601 | | * Write changes to journal file. |
8602 | | */ |
8603 | 0 | CHECK(zone_journal(zone, zonediff.diff, NULL, "zone_sign")); |
8604 | |
|
8605 | 0 | pauseall: |
8606 | | /* |
8607 | | * Pause all iterators so that dns_db_closeversion() can succeed. |
8608 | | */ |
8609 | 0 | ISC_LIST_FOREACH(zone->signing, s, link) { |
8610 | 0 | dns_dbiterator_pause(s->dbiterator); |
8611 | 0 | } |
8612 | |
|
8613 | 0 | ISC_LIST_FOREACH(cleanup, s, link) { |
8614 | 0 | dns_dbiterator_pause(s->dbiterator); |
8615 | 0 | } |
8616 | | |
8617 | | /* |
8618 | | * Everything has succeeded. Commit the changes. |
8619 | | */ |
8620 | 0 | dns_db_closeversion(db, &version, commit); |
8621 | | |
8622 | | /* |
8623 | | * Everything succeeded so we can clean these up now. |
8624 | | */ |
8625 | 0 | ISC_LIST_FOREACH(cleanup, s, link) { |
8626 | 0 | ISC_LIST_UNLINK(cleanup, s, link); |
8627 | 0 | dns_db_detach(&s->db); |
8628 | 0 | dns_dbiterator_destroy(&s->dbiterator); |
8629 | 0 | isc_mem_put(zone->mctx, s, sizeof *s); |
8630 | 0 | } |
8631 | |
|
8632 | 0 | LOCK_ZONE(zone); |
8633 | 0 | dns__zone_set_resigntime(zone); |
8634 | 0 | if (commit) { |
8635 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY); |
8636 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
8637 | 0 | } |
8638 | 0 | UNLOCK_ZONE(zone); |
8639 | |
|
8640 | 0 | cleanup: |
8641 | 0 | if (result != ISC_R_SUCCESS) { |
8642 | 0 | dnssec_log(zone, ISC_LOG_ERROR, "zone_sign: failed: %s", |
8643 | 0 | isc_result_totext(result)); |
8644 | 0 | } |
8645 | |
|
8646 | 0 | done: |
8647 | | /* |
8648 | | * Pause all dbiterators. |
8649 | | */ |
8650 | 0 | ISC_LIST_FOREACH(zone->signing, s, link) { |
8651 | 0 | dns_dbiterator_pause(s->dbiterator); |
8652 | 0 | } |
8653 | | |
8654 | | /* |
8655 | | * Rollback the cleanup list. |
8656 | | */ |
8657 | 0 | ISC_LIST_FOREACH(cleanup, s, link) { |
8658 | 0 | ISC_LIST_UNLINK(cleanup, s, link); |
8659 | 0 | ISC_LIST_PREPEND(zone->signing, s, link); |
8660 | 0 | dns_dbiterator_first(s->dbiterator); |
8661 | 0 | dns_dbiterator_pause(s->dbiterator); |
8662 | 0 | } |
8663 | |
|
8664 | 0 | dns_diff_clear(&_sig_diff); |
8665 | 0 | dns_diff_clear(&post_diff); |
8666 | |
|
8667 | 0 | for (i = 0; i < nkeys; i++) { |
8668 | 0 | dst_key_free(&zone_keys[i]); |
8669 | 0 | } |
8670 | |
|
8671 | 0 | if (node != NULL) { |
8672 | 0 | dns_db_detachnode(&node); |
8673 | 0 | } |
8674 | |
|
8675 | 0 | if (version != NULL) { |
8676 | 0 | dns_db_closeversion(db, &version, false); |
8677 | 0 | dns_db_detach(&db); |
8678 | 0 | } else if (db != NULL) { |
8679 | 0 | dns_db_detach(&db); |
8680 | 0 | } |
8681 | |
|
8682 | 0 | LOCK_ZONE(zone); |
8683 | 0 | if (ISC_LIST_HEAD(zone->signing) != NULL) { |
8684 | 0 | isc_interval_t interval; |
8685 | 0 | if (zone->update_disabled || result != ISC_R_SUCCESS) { |
8686 | 0 | isc_interval_set(&interval, 60, 0); /* 1 minute */ |
8687 | 0 | } else { |
8688 | 0 | isc_interval_set(&interval, 0, 10000000); /* 10 ms */ |
8689 | 0 | } |
8690 | 0 | isc_time_nowplusinterval(&zone->signingtime, &interval); |
8691 | 0 | } else { |
8692 | 0 | isc_time_settoepoch(&zone->signingtime); |
8693 | 0 | } |
8694 | 0 | UNLOCK_ZONE(zone); |
8695 | |
|
8696 | 0 | INSIST(version == NULL); |
8697 | 0 | } |
8698 | | |
8699 | | static isc_result_t |
8700 | | normalize_key(dns_rdata_t *rr, dns_rdata_t *target, unsigned char *data, |
8701 | 0 | int size) { |
8702 | 0 | dns_rdata_dnskey_t dnskey; |
8703 | 0 | dns_rdata_keydata_t keydata; |
8704 | 0 | isc_buffer_t buf; |
8705 | 0 | isc_result_t result = ISC_R_SUCCESS; |
8706 | |
|
8707 | 0 | dns_rdata_reset(target); |
8708 | 0 | isc_buffer_init(&buf, data, size); |
8709 | |
|
8710 | 0 | switch (rr->type) { |
8711 | 0 | case dns_rdatatype_dnskey: |
8712 | 0 | result = dns_rdata_tostruct(rr, &dnskey, NULL); |
8713 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
8714 | 0 | dnskey.flags &= ~DNS_KEYFLAG_REVOKE; |
8715 | 0 | result = dns_rdata_fromstruct(target, rr->rdclass, |
8716 | 0 | dns_rdatatype_dnskey, &dnskey, |
8717 | 0 | &buf); |
8718 | 0 | break; |
8719 | 0 | case dns_rdatatype_keydata: |
8720 | 0 | result = dns_rdata_tostruct(rr, &keydata, NULL); |
8721 | 0 | if (result == ISC_R_UNEXPECTEDEND) { |
8722 | 0 | return result; |
8723 | 0 | } |
8724 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
8725 | 0 | dns_keydata_todnskey(&keydata, &dnskey, NULL); |
8726 | 0 | result = dns_rdata_fromstruct(target, rr->rdclass, |
8727 | 0 | dns_rdatatype_dnskey, &dnskey, |
8728 | 0 | &buf); |
8729 | 0 | break; |
8730 | 0 | default: |
8731 | 0 | UNREACHABLE(); |
8732 | 0 | } |
8733 | | |
8734 | 0 | return result; |
8735 | 0 | } |
8736 | | |
8737 | | /* |
8738 | | * 'rdset' contains either a DNSKEY rdataset from the zone apex, or |
8739 | | * a KEYDATA rdataset from the key zone. |
8740 | | * |
8741 | | * 'rr' contains either a DNSKEY record, or a KEYDATA record |
8742 | | * |
8743 | | * After normalizing keys to the same format (DNSKEY, with revoke bit |
8744 | | * cleared), return true if a key that matches 'rr' is found in |
8745 | | * 'rdset', or false if not. |
8746 | | */ |
8747 | | |
8748 | | static bool |
8749 | 0 | matchkey(dns_rdataset_t *rdset, dns_rdata_t *rr) { |
8750 | 0 | unsigned char data1[4096]; |
8751 | 0 | dns_rdata_t rdata1 = DNS_RDATA_INIT; |
8752 | 0 | isc_result_t result; |
8753 | |
|
8754 | 0 | result = normalize_key(rr, &rdata1, data1, sizeof(data1)); |
8755 | 0 | if (result != ISC_R_SUCCESS) { |
8756 | 0 | return false; |
8757 | 0 | } |
8758 | | |
8759 | 0 | DNS_RDATASET_FOREACH(rdset) { |
8760 | 0 | unsigned char data2[4096]; |
8761 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
8762 | 0 | dns_rdata_t rdata2 = DNS_RDATA_INIT; |
8763 | |
|
8764 | 0 | dns_rdataset_current(rdset, &rdata); |
8765 | 0 | result = normalize_key(&rdata, &rdata2, data2, sizeof(data2)); |
8766 | 0 | if (result != ISC_R_SUCCESS) { |
8767 | 0 | continue; |
8768 | 0 | } |
8769 | 0 | if (dns_rdata_compare(&rdata1, &rdata2) == 0) { |
8770 | 0 | return true; |
8771 | 0 | } |
8772 | 0 | } |
8773 | | |
8774 | 0 | return false; |
8775 | 0 | } |
8776 | | |
8777 | | /* |
8778 | | * Calculate the refresh interval for a keydata zone, per |
8779 | | * RFC5011: MAX(1 hr, |
8780 | | * MIN(15 days, |
8781 | | * 1/2 * OrigTTL, |
8782 | | * 1/2 * RRSigExpirationInterval)) |
8783 | | * or for retries: MAX(1 hr, |
8784 | | * MIN(1 day, |
8785 | | * 1/10 * OrigTTL, |
8786 | | * 1/10 * RRSigExpirationInterval)) |
8787 | | */ |
8788 | | static isc_stdtime_t |
8789 | 0 | refresh_time(dns_zonefetch_t *fetch, bool retry) { |
8790 | 0 | isc_result_t result; |
8791 | 0 | uint32_t t; |
8792 | 0 | dns_rdataset_t *sigset; |
8793 | 0 | dns_rdata_t sigrr = DNS_RDATA_INIT; |
8794 | 0 | dns_rdata_sig_t sig; |
8795 | 0 | isc_stdtime_t now; |
8796 | |
|
8797 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_KEY); |
8798 | |
|
8799 | 0 | now = isc_stdtime_now(); |
8800 | |
|
8801 | 0 | if (dns_rdataset_isassociated(&fetch->sigset)) { |
8802 | 0 | sigset = &fetch->sigset; |
8803 | 0 | } else { |
8804 | 0 | return now + dns_zone_mkey_hour; |
8805 | 0 | } |
8806 | | |
8807 | 0 | result = dns_rdataset_first(sigset); |
8808 | 0 | if (result != ISC_R_SUCCESS) { |
8809 | 0 | return now + dns_zone_mkey_hour; |
8810 | 0 | } |
8811 | | |
8812 | 0 | dns_rdataset_current(sigset, &sigrr); |
8813 | 0 | result = dns_rdata_tostruct(&sigrr, &sig, NULL); |
8814 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
8815 | |
|
8816 | 0 | if (!retry) { |
8817 | 0 | t = sig.originalttl / 2; |
8818 | |
|
8819 | 0 | if (isc_serial_gt(sig.timeexpire, now)) { |
8820 | 0 | uint32_t exp = (sig.timeexpire - now) / 2; |
8821 | 0 | if (t > exp) { |
8822 | 0 | t = exp; |
8823 | 0 | } |
8824 | 0 | } |
8825 | |
|
8826 | 0 | if (t > (15 * dns_zone_mkey_day)) { |
8827 | 0 | t = (15 * dns_zone_mkey_day); |
8828 | 0 | } |
8829 | |
|
8830 | 0 | if (t < dns_zone_mkey_hour) { |
8831 | 0 | t = dns_zone_mkey_hour; |
8832 | 0 | } |
8833 | 0 | } else { |
8834 | 0 | t = sig.originalttl / 10; |
8835 | |
|
8836 | 0 | if (isc_serial_gt(sig.timeexpire, now)) { |
8837 | 0 | uint32_t exp = (sig.timeexpire - now) / 10; |
8838 | 0 | if (t > exp) { |
8839 | 0 | t = exp; |
8840 | 0 | } |
8841 | 0 | } |
8842 | |
|
8843 | 0 | if (t > dns_zone_mkey_day) { |
8844 | 0 | t = dns_zone_mkey_day; |
8845 | 0 | } |
8846 | |
|
8847 | 0 | if (t < dns_zone_mkey_hour) { |
8848 | 0 | t = dns_zone_mkey_hour; |
8849 | 0 | } |
8850 | 0 | } |
8851 | |
|
8852 | 0 | return now + t; |
8853 | 0 | } |
8854 | | |
8855 | | /* |
8856 | | * This routine is called when no changes are needed in a KEYDATA |
8857 | | * record except to simply update the refresh timer. Caller should |
8858 | | * hold zone lock. |
8859 | | */ |
8860 | | static isc_result_t |
8861 | 0 | minimal_update(dns_zonefetch_t *fetch, dns_dbversion_t *ver, dns_diff_t *diff) { |
8862 | 0 | dns_keyfetch_t *kfetch; |
8863 | 0 | isc_result_t result; |
8864 | 0 | isc_buffer_t keyb; |
8865 | 0 | unsigned char key_buf[4096]; |
8866 | 0 | dns_rdata_keydata_t keydata; |
8867 | 0 | dns_name_t *name; |
8868 | 0 | dns_zone_t *zone; |
8869 | 0 | isc_stdtime_t now; |
8870 | |
|
8871 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_KEY); |
8872 | |
|
8873 | 0 | now = isc_stdtime_now(); |
8874 | 0 | zone = fetch->zone; |
8875 | 0 | name = dns_fixedname_name(&fetch->name); |
8876 | 0 | kfetch = &fetch->fetchdata.keyfetch; |
8877 | |
|
8878 | 0 | DNS_RDATASET_FOREACH(&kfetch->keydataset) { |
8879 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
8880 | 0 | dns_rdataset_current(&kfetch->keydataset, &rdata); |
8881 | | |
8882 | | /* Delete old version */ |
8883 | 0 | CHECK(update_one_rr(kfetch->db, ver, diff, DNS_DIFFOP_DEL, name, |
8884 | 0 | 0, &rdata)); |
8885 | | |
8886 | | /* Update refresh timer */ |
8887 | 0 | result = dns_rdata_tostruct(&rdata, &keydata, NULL); |
8888 | 0 | if (result == ISC_R_UNEXPECTEDEND) { |
8889 | 0 | continue; |
8890 | 0 | } |
8891 | 0 | CHECK(result); |
8892 | |
|
8893 | 0 | keydata.refresh = refresh_time(fetch, true); |
8894 | 0 | set_refreshkeytimer(zone, &keydata, now, false); |
8895 | |
|
8896 | 0 | dns_rdata_reset(&rdata); |
8897 | 0 | isc_buffer_init(&keyb, key_buf, sizeof(key_buf)); |
8898 | 0 | CHECK(dns_rdata_fromstruct(&rdata, zone->rdclass, |
8899 | 0 | dns_rdatatype_keydata, &keydata, |
8900 | 0 | &keyb)); |
8901 | | |
8902 | | /* Insert updated version */ |
8903 | 0 | CHECK(update_one_rr(kfetch->db, ver, diff, DNS_DIFFOP_ADD, name, |
8904 | 0 | 0, &rdata)); |
8905 | 0 | } |
8906 | 0 | result = ISC_R_SUCCESS; |
8907 | 0 | cleanup: |
8908 | 0 | return result; |
8909 | 0 | } |
8910 | | |
8911 | | /* |
8912 | | * Verify that DNSKEY set is signed by the key specified in 'keydata'. |
8913 | | */ |
8914 | | static bool |
8915 | 0 | revocable(dns_zonefetch_t *fetch, dns_rdata_keydata_t *keydata) { |
8916 | 0 | isc_result_t result; |
8917 | 0 | dns_name_t *keyname; |
8918 | 0 | isc_mem_t *mctx; |
8919 | 0 | dns_rdata_t rr = DNS_RDATA_INIT; |
8920 | 0 | dns_rdata_rrsig_t sig; |
8921 | 0 | dns_rdata_dnskey_t dnskey; |
8922 | 0 | dst_key_t *dstkey = NULL; |
8923 | 0 | unsigned char key_buf[4096]; |
8924 | 0 | isc_buffer_t keyb; |
8925 | 0 | bool answer = false; |
8926 | 0 | dst_algorithm_t algorithm; |
8927 | |
|
8928 | 0 | REQUIRE(fetch != NULL && keydata != NULL); |
8929 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_KEY); |
8930 | 0 | REQUIRE(dns_rdataset_isassociated(&fetch->sigset)); |
8931 | |
|
8932 | 0 | keyname = dns_fixedname_name(&fetch->name); |
8933 | 0 | mctx = fetch->zone->view->mctx; |
8934 | | |
8935 | | /* Generate a key from keydata */ |
8936 | 0 | isc_buffer_init(&keyb, key_buf, sizeof(key_buf)); |
8937 | 0 | dns_keydata_todnskey(keydata, &dnskey, NULL); |
8938 | |
|
8939 | 0 | result = dns_rdata_fromstruct(&rr, keydata->common.rdclass, |
8940 | 0 | dns_rdatatype_dnskey, &dnskey, &keyb); |
8941 | 0 | if (result != ISC_R_SUCCESS) { |
8942 | 0 | return false; |
8943 | 0 | } |
8944 | | |
8945 | 0 | result = dns_dnssec_keyfromrdata(keyname, &rr, mctx, &dstkey); |
8946 | 0 | if (result != ISC_R_SUCCESS) { |
8947 | 0 | return false; |
8948 | 0 | } |
8949 | | |
8950 | | /* See if that key generated any of the signatures */ |
8951 | 0 | DNS_RDATASET_FOREACH(&fetch->sigset) { |
8952 | 0 | dns_rdata_t sigrr = DNS_RDATA_INIT; |
8953 | |
|
8954 | 0 | dns_rdataset_current(&fetch->sigset, &sigrr); |
8955 | 0 | result = dns_rdata_tostruct(&sigrr, &sig, NULL); |
8956 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
8957 | |
|
8958 | 0 | algorithm = dst_algorithm_fromdata(sig.algorithm, sig.signature, |
8959 | 0 | sig.siglen); |
8960 | 0 | if (dst_key_alg(dstkey) == algorithm && |
8961 | 0 | dst_key_rid(dstkey) == sig.keyid) |
8962 | 0 | { |
8963 | 0 | result = dns_dnssec_verify(keyname, &fetch->rrset, |
8964 | 0 | dstkey, false, mctx, &sigrr, |
8965 | 0 | NULL, NULL); |
8966 | |
|
8967 | 0 | dnssec_log(fetch->zone, ISC_LOG_DEBUG(3), |
8968 | 0 | "Confirm revoked DNSKEY is self-signed: %s", |
8969 | 0 | isc_result_totext(result)); |
8970 | |
|
8971 | 0 | if (result == ISC_R_SUCCESS) { |
8972 | 0 | answer = true; |
8973 | 0 | break; |
8974 | 0 | } |
8975 | 0 | } |
8976 | 0 | } |
8977 | |
|
8978 | 0 | dst_key_free(&dstkey); |
8979 | 0 | return answer; |
8980 | 0 | } |
8981 | | |
8982 | | /* |
8983 | | * Fetch DNSKEY records at the trust anchor name. |
8984 | | */ |
8985 | | static isc_result_t |
8986 | 0 | keyfetch_start(dns_zonefetch_t *fetch) { |
8987 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_KEY); |
8988 | |
|
8989 | 0 | fetch->qname = dns_fixedname_name(&fetch->name); |
8990 | 0 | fetch->qtype = dns_rdatatype_dnskey; |
8991 | |
|
8992 | 0 | return ISC_R_SUCCESS; |
8993 | 0 | } |
8994 | | |
8995 | | static void |
8996 | 0 | keyfetch_continue(dns_zonefetch_t *fetch) { |
8997 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_KEY); |
8998 | | /* No continue path for keyfetch exists. */ |
8999 | 0 | REQUIRE(0); |
9000 | 0 | } |
9001 | | |
9002 | | static void |
9003 | 0 | keyfetch_cancel(dns_zonefetch_t *fetch) { |
9004 | 0 | dns_keyfetch_t *kfetch; |
9005 | 0 | dns_zone_t *zone; |
9006 | |
|
9007 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_KEY); |
9008 | 0 | REQUIRE(DNS_ZONE_VALID(fetch->zone)); |
9009 | 0 | REQUIRE(LOCKED_ZONE(fetch->zone)); |
9010 | |
|
9011 | 0 | kfetch = &fetch->fetchdata.keyfetch; |
9012 | 0 | zone = fetch->zone; |
9013 | | |
9014 | | /* |
9015 | | * Error during a key fetch; cancel and retry in an hour. |
9016 | | */ |
9017 | 0 | zone->fetchcount[ZONEFETCHTYPE_KEY]--; |
9018 | |
|
9019 | 0 | dns_db_detach(&kfetch->db); |
9020 | 0 | dns_rdataset_disassociate(&kfetch->keydataset); |
9021 | |
|
9022 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
9023 | | /* Don't really retry if we are exiting */ |
9024 | 0 | isc_time_t timenow, timethen; |
9025 | 0 | char timebuf[80]; |
9026 | |
|
9027 | 0 | timenow = isc_time_now(); |
9028 | 0 | DNS_ZONE_TIME_ADD(&timenow, dns_zone_mkey_hour, &timethen); |
9029 | 0 | zone->refreshkeytime = timethen; |
9030 | 0 | dns__zone_settimer(zone, timenow); |
9031 | |
|
9032 | 0 | isc_time_formattimestamp(&zone->refreshkeytime, timebuf, 80); |
9033 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(1), "retry key refresh: %s", |
9034 | 0 | timebuf); |
9035 | 0 | } |
9036 | 0 | } |
9037 | | |
9038 | | static void |
9039 | 0 | keyfetch_cleanup(dns_zonefetch_t *fetch) { |
9040 | 0 | dns_keyfetch_t *kfetch = NULL; |
9041 | |
|
9042 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_KEY); |
9043 | |
|
9044 | 0 | kfetch = &fetch->fetchdata.keyfetch; |
9045 | |
|
9046 | 0 | dns_db_detach(&kfetch->db); |
9047 | |
|
9048 | 0 | dns_rdataset_cleanup(&kfetch->keydataset); |
9049 | 0 | } |
9050 | | |
9051 | | /* |
9052 | | * A DNSKEY set has been fetched from the zone apex of a zone whose trust |
9053 | | * anchors are being managed; scan the keyset, and update the key zone and the |
9054 | | * local trust anchors according to RFC5011. |
9055 | | */ |
9056 | | static isc_result_t |
9057 | 0 | keyfetch_done(dns_zonefetch_t *fetch, isc_result_t eresult) { |
9058 | 0 | isc_result_t result; |
9059 | 0 | dns_keyfetch_t *kfetch = NULL; |
9060 | 0 | dns_zone_t *zone = NULL; |
9061 | 0 | isc_mem_t *mctx = NULL; |
9062 | 0 | dns_keytable_t *secroots = NULL; |
9063 | 0 | dns_dbversion_t *ver = NULL; |
9064 | 0 | dns_diff_t diff; |
9065 | 0 | bool alldone = false; |
9066 | 0 | bool commit = false; |
9067 | 0 | dns_name_t *keyname = NULL; |
9068 | 0 | dns_rdata_t keydatarr = DNS_RDATA_INIT; |
9069 | 0 | dns_rdata_rrsig_t sig; |
9070 | 0 | dns_rdata_dnskey_t dnskey; |
9071 | 0 | dns_rdata_keydata_t keydata; |
9072 | 0 | bool initializing; |
9073 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
9074 | 0 | unsigned char key_buf[4096]; |
9075 | 0 | isc_buffer_t keyb; |
9076 | 0 | dst_key_t *dstkey = NULL; |
9077 | 0 | isc_stdtime_t now; |
9078 | 0 | int pending = 0; |
9079 | 0 | bool secure = false, initial = false; |
9080 | 0 | dns_keynode_t *keynode = NULL; |
9081 | 0 | dns_rdataset_t *dnskeys = NULL, *dnskeysigs = NULL; |
9082 | 0 | dns_rdataset_t *keydataset = NULL, dsset; |
9083 | |
|
9084 | 0 | REQUIRE(fetch != NULL); |
9085 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_KEY); |
9086 | 0 | REQUIRE(DNS_ZONE_VALID(fetch->zone)); |
9087 | 0 | REQUIRE(LOCKED_ZONE(fetch->zone)); |
9088 | |
|
9089 | 0 | kfetch = &fetch->fetchdata.keyfetch; |
9090 | 0 | zone = fetch->zone; |
9091 | 0 | mctx = fetch->mctx; |
9092 | 0 | keyname = dns_fixedname_name(&fetch->name); |
9093 | 0 | dnskeys = &fetch->rrset; |
9094 | 0 | dnskeysigs = &fetch->sigset; |
9095 | |
|
9096 | 0 | keydataset = &kfetch->keydataset; |
9097 | |
|
9098 | 0 | now = isc_stdtime_now(); |
9099 | 0 | dns_name_format(keyname, namebuf, sizeof(namebuf)); |
9100 | |
|
9101 | 0 | result = dns_view_getsecroots(zone->view, &secroots); |
9102 | 0 | INSIST(result == ISC_R_SUCCESS); |
9103 | |
|
9104 | 0 | dns_diff_init(mctx, &diff); |
9105 | |
|
9106 | 0 | CHECK(dns_db_newversion(kfetch->db, &ver)); |
9107 | |
|
9108 | 0 | zone->fetchcount[ZONEFETCHTYPE_KEY]--; |
9109 | 0 | alldone = (zone->fetchcount[ZONEFETCHTYPE_KEY] == 0); |
9110 | |
|
9111 | 0 | if (alldone) { |
9112 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESHING); |
9113 | 0 | } |
9114 | |
|
9115 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
9116 | 0 | "Returned from key fetch in keyfetch_done() for '%s': %s", |
9117 | 0 | namebuf, isc_result_totext(eresult)); |
9118 | |
|
9119 | 0 | result = dns_zonefetch_verify(fetch, eresult, dns_trust_none); |
9120 | 0 | if (result != ISC_R_SUCCESS) { |
9121 | 0 | CHECK(minimal_update(fetch, ver, &diff)); |
9122 | 0 | goto done; |
9123 | 0 | } |
9124 | | |
9125 | | /* |
9126 | | * Clear any cached trust level, as we need to run validation |
9127 | | * over again; trusted keys might have changed. |
9128 | | */ |
9129 | 0 | dnskeys->trust = dnskeysigs->trust = dns_trust_none; |
9130 | | |
9131 | | /* Look up the trust anchor */ |
9132 | 0 | result = dns_keytable_find(secroots, keyname, &keynode); |
9133 | 0 | if (result != ISC_R_SUCCESS) { |
9134 | 0 | goto anchors_done; |
9135 | 0 | } |
9136 | | |
9137 | | /* |
9138 | | * If the keynode has a DS trust anchor, use it for verification. |
9139 | | */ |
9140 | 0 | dns_rdataset_init(&dsset); |
9141 | 0 | if (dns_keynode_dsset(keynode, &dsset)) { |
9142 | 0 | DNS_RDATASET_FOREACH(dnskeysigs) { |
9143 | 0 | isc_result_t tresult = ISC_R_NOTFOUND; |
9144 | 0 | dns_rdata_t keyrdata = DNS_RDATA_INIT; |
9145 | 0 | dns_rdata_t sigrr = DNS_RDATA_INIT; |
9146 | |
|
9147 | 0 | dns_rdataset_current(dnskeysigs, &sigrr); |
9148 | 0 | dns_rdata_tostruct(&sigrr, &sig, NULL); |
9149 | |
|
9150 | 0 | DNS_RDATASET_FOREACH(&dsset) { |
9151 | 0 | dns_rdata_t dsrdata = DNS_RDATA_INIT; |
9152 | 0 | dns_rdata_ds_t ds; |
9153 | |
|
9154 | 0 | dns_rdata_reset(&dsrdata); |
9155 | 0 | dns_rdataset_current(&dsset, &dsrdata); |
9156 | 0 | dns_rdata_tostruct(&dsrdata, &ds, NULL); |
9157 | |
|
9158 | 0 | if (ds.key_tag != sig.keyid || |
9159 | 0 | ds.algorithm != sig.algorithm) |
9160 | 0 | { |
9161 | 0 | continue; |
9162 | 0 | } |
9163 | | |
9164 | 0 | tresult = dns_dnssec_matchdskey( |
9165 | 0 | keyname, &dsrdata, dnskeys, &keyrdata); |
9166 | 0 | if (tresult == ISC_R_SUCCESS) { |
9167 | 0 | break; |
9168 | 0 | } |
9169 | 0 | } |
9170 | |
|
9171 | 0 | if (tresult == ISC_R_NOTFOUND) { |
9172 | 0 | continue; |
9173 | 0 | } |
9174 | | |
9175 | 0 | result = dns_dnssec_keyfromrdata(keyname, &keyrdata, |
9176 | 0 | mctx, &dstkey); |
9177 | 0 | if (result != ISC_R_SUCCESS) { |
9178 | 0 | continue; |
9179 | 0 | } |
9180 | | |
9181 | 0 | result = dns_dnssec_verify(keyname, dnskeys, dstkey, |
9182 | 0 | false, mctx, &sigrr, NULL, |
9183 | 0 | NULL); |
9184 | 0 | dst_key_free(&dstkey); |
9185 | |
|
9186 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
9187 | 0 | "Verifying DNSKEY set for zone " |
9188 | 0 | "'%s' using DS %d/%d: %s", |
9189 | 0 | namebuf, sig.keyid, sig.algorithm, |
9190 | 0 | isc_result_totext(result)); |
9191 | |
|
9192 | 0 | if (result == ISC_R_SUCCESS) { |
9193 | 0 | dnskeys->trust = dns_trust_secure; |
9194 | 0 | dnskeysigs->trust = dns_trust_secure; |
9195 | 0 | initial = dns_keynode_initial(keynode); |
9196 | 0 | dns_keynode_trust(keynode); |
9197 | 0 | secure = true; |
9198 | 0 | break; |
9199 | 0 | } |
9200 | 0 | } |
9201 | 0 | dns_rdataset_disassociate(&dsset); |
9202 | 0 | } |
9203 | |
|
9204 | 0 | anchors_done: |
9205 | 0 | if (keynode != NULL) { |
9206 | 0 | dns_keynode_detach(&keynode); |
9207 | 0 | } |
9208 | | |
9209 | | /* |
9210 | | * If we were not able to verify the answer using the current |
9211 | | * trusted keys then all we can do is look at any revoked keys. |
9212 | | */ |
9213 | 0 | if (!secure) { |
9214 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9215 | 0 | "DNSKEY set for zone '%s' could not be verified " |
9216 | 0 | "with current keys", |
9217 | 0 | namebuf); |
9218 | 0 | } |
9219 | | |
9220 | | /* |
9221 | | * First scan keydataset to find keys that are not in dnskeyset |
9222 | | * - Missing keys which are not scheduled for removal, |
9223 | | * log a warning |
9224 | | * - Missing keys which are scheduled for removal and |
9225 | | * the remove hold-down timer has completed should |
9226 | | * be removed from the key zone |
9227 | | * - Missing keys whose acceptance timers have not yet |
9228 | | * completed, log a warning and reset the acceptance |
9229 | | * timer to 30 days in the future |
9230 | | * - All keys not being removed have their refresh timers |
9231 | | * updated |
9232 | | */ |
9233 | 0 | initializing = true; |
9234 | 0 | DNS_RDATASET_FOREACH(keydataset) { |
9235 | 0 | dns_keytag_t keytag; |
9236 | |
|
9237 | 0 | dns_rdata_reset(&keydatarr); |
9238 | 0 | dns_rdataset_current(keydataset, &keydatarr); |
9239 | 0 | result = dns_rdata_tostruct(&keydatarr, &keydata, NULL); |
9240 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
9241 | |
|
9242 | 0 | dns_keydata_todnskey(&keydata, &dnskey, NULL); |
9243 | 0 | result = compute_tag(keyname, &dnskey, mctx, &keytag); |
9244 | 0 | if (result != ISC_R_SUCCESS) { |
9245 | | /* |
9246 | | * Skip if we cannot compute the key tag. |
9247 | | * This may happen if the algorithm is unsupported |
9248 | | */ |
9249 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
9250 | 0 | "Cannot compute tag for key in zone %s: " |
9251 | 0 | "%s " |
9252 | 0 | "(skipping)", |
9253 | 0 | namebuf, isc_result_totext(result)); |
9254 | 0 | continue; |
9255 | 0 | } |
9256 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
9257 | | |
9258 | | /* |
9259 | | * If any keydata record has a nonzero add holddown, then |
9260 | | * there was a pre-existing trust anchor for this domain; |
9261 | | * that means we are *not* initializing it and shouldn't |
9262 | | * automatically trust all the keys we find at the zone apex. |
9263 | | */ |
9264 | 0 | initializing = initializing && (keydata.addhd == 0); |
9265 | |
|
9266 | 0 | if (!matchkey(dnskeys, &keydatarr)) { |
9267 | 0 | bool deletekey = false; |
9268 | |
|
9269 | 0 | if (!secure) { |
9270 | 0 | if (keydata.removehd != 0 && |
9271 | 0 | keydata.removehd <= now) |
9272 | 0 | { |
9273 | 0 | deletekey = true; |
9274 | 0 | } |
9275 | 0 | } else if (keydata.addhd == 0) { |
9276 | 0 | deletekey = true; |
9277 | 0 | } else if (keydata.addhd > now) { |
9278 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9279 | 0 | "Pending key %d for zone %s " |
9280 | 0 | "unexpectedly missing from DNSKEY " |
9281 | 0 | "RRset: restarting 30-day " |
9282 | 0 | "acceptance timer", |
9283 | 0 | keytag, namebuf); |
9284 | 0 | if (keydata.addhd < now + dns_zone_mkey_month) { |
9285 | 0 | keydata.addhd = now + |
9286 | 0 | dns_zone_mkey_month; |
9287 | 0 | } |
9288 | 0 | keydata.refresh = refresh_time(fetch, false); |
9289 | 0 | } else if (keydata.removehd == 0) { |
9290 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9291 | 0 | "Active key %d for zone %s " |
9292 | 0 | "unexpectedly missing from DNSKEY " |
9293 | 0 | "RRset", |
9294 | 0 | keytag, namebuf); |
9295 | 0 | keydata.refresh = now + dns_zone_mkey_hour; |
9296 | 0 | } else if (keydata.removehd <= now) { |
9297 | 0 | deletekey = true; |
9298 | 0 | dnssec_log( |
9299 | 0 | zone, ISC_LOG_INFO, |
9300 | 0 | "Revoked key %d for zone %s no longer " |
9301 | 0 | "present in DNSKEY RRset: deleting " |
9302 | 0 | "from managed keys database", |
9303 | 0 | keytag, namebuf); |
9304 | 0 | } else { |
9305 | 0 | keydata.refresh = refresh_time(fetch, false); |
9306 | 0 | } |
9307 | |
|
9308 | 0 | if (secure || deletekey) { |
9309 | | /* Delete old version */ |
9310 | 0 | CHECK(update_one_rr(kfetch->db, ver, &diff, |
9311 | 0 | DNS_DIFFOP_DEL, keyname, 0, |
9312 | 0 | &keydatarr)); |
9313 | 0 | } |
9314 | | |
9315 | 0 | if (!secure || deletekey) { |
9316 | 0 | continue; |
9317 | 0 | } |
9318 | | |
9319 | 0 | dns_rdata_reset(&keydatarr); |
9320 | 0 | isc_buffer_init(&keyb, key_buf, sizeof(key_buf)); |
9321 | 0 | result = dns_rdata_fromstruct(&keydatarr, zone->rdclass, |
9322 | 0 | dns_rdatatype_keydata, |
9323 | 0 | &keydata, &keyb); |
9324 | 0 | if (result != ISC_R_SUCCESS) { |
9325 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
9326 | 0 | "dns_rdata_fromstruct failed: " |
9327 | 0 | "KEYDATA %d: %s", |
9328 | 0 | keytag, isc_result_totext(result)); |
9329 | 0 | continue; |
9330 | 0 | } |
9331 | | |
9332 | | /* Insert updated version */ |
9333 | 0 | CHECK(update_one_rr(kfetch->db, ver, &diff, |
9334 | 0 | DNS_DIFFOP_ADD, keyname, 0, |
9335 | 0 | &keydatarr)); |
9336 | |
|
9337 | 0 | set_refreshkeytimer(zone, &keydata, now, false); |
9338 | 0 | } |
9339 | 0 | } |
9340 | | |
9341 | | /* |
9342 | | * Next scan dnskeyset: |
9343 | | * - If new keys are found (i.e., lacking a match in keydataset) |
9344 | | * add them to the key zone and set the acceptance timer |
9345 | | * to 30 days in the future (or to immediately if we've |
9346 | | * determined that we're initializing the zone for the |
9347 | | * first time) |
9348 | | * - Previously-known keys that have been revoked |
9349 | | * must be scheduled for removal from the key zone (or, |
9350 | | * if they hadn't been accepted as trust anchors yet |
9351 | | * anyway, removed at once) |
9352 | | * - Previously-known unrevoked keys whose acceptance timers |
9353 | | * have completed are promoted to trust anchors |
9354 | | * - All keys not being removed have their refresh |
9355 | | * timers updated |
9356 | | */ |
9357 | 0 | DNS_RDATASET_FOREACH(dnskeys) { |
9358 | 0 | dns_rdata_t dnskeyrr = DNS_RDATA_INIT; |
9359 | 0 | bool revoked = false; |
9360 | 0 | bool newkey = false; |
9361 | 0 | bool updatekey = false; |
9362 | 0 | bool deletekey = false; |
9363 | 0 | bool trustkey = false; |
9364 | 0 | dns_keytag_t keytag; |
9365 | |
|
9366 | 0 | dns_rdataset_current(dnskeys, &dnskeyrr); |
9367 | 0 | result = dns_rdata_tostruct(&dnskeyrr, &dnskey, NULL); |
9368 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
9369 | | |
9370 | | /* Skip ZSK's */ |
9371 | 0 | if ((dnskey.flags & DNS_KEYFLAG_KSK) == 0) { |
9372 | 0 | continue; |
9373 | 0 | } |
9374 | | |
9375 | 0 | result = compute_tag(keyname, &dnskey, mctx, &keytag); |
9376 | 0 | if (result != ISC_R_SUCCESS) { |
9377 | | /* |
9378 | | * Skip if we cannot compute the key tag. |
9379 | | * This may happen if the algorithm is unsupported |
9380 | | */ |
9381 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
9382 | 0 | "Cannot compute tag for key in zone %s: " |
9383 | 0 | "%s " |
9384 | 0 | "(skipping)", |
9385 | 0 | namebuf, isc_result_totext(result)); |
9386 | 0 | continue; |
9387 | 0 | } |
9388 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
9389 | |
|
9390 | 0 | revoked = ((dnskey.flags & DNS_KEYFLAG_REVOKE) != 0); |
9391 | |
|
9392 | 0 | if (matchkey(keydataset, &dnskeyrr)) { |
9393 | 0 | dns_rdata_reset(&keydatarr); |
9394 | 0 | dns_rdataset_current(keydataset, &keydatarr); |
9395 | 0 | result = dns_rdata_tostruct(&keydatarr, &keydata, NULL); |
9396 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
9397 | |
|
9398 | 0 | if (revoked && revocable(fetch, &keydata)) { |
9399 | 0 | if (keydata.addhd > now) { |
9400 | | /* |
9401 | | * Key wasn't trusted yet, and now |
9402 | | * it's been revoked? Just remove it |
9403 | | */ |
9404 | 0 | deletekey = true; |
9405 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9406 | 0 | "Pending key %d for " |
9407 | 0 | "zone %s is now revoked: " |
9408 | 0 | "deleting from the " |
9409 | 0 | "managed keys database", |
9410 | 0 | keytag, namebuf); |
9411 | 0 | } else if (keydata.removehd == 0) { |
9412 | | /* |
9413 | | * Remove key from secroots. |
9414 | | */ |
9415 | 0 | dns_view_untrust(zone->view, keyname, |
9416 | 0 | &dnskey); |
9417 | | |
9418 | | /* If initializing, delete now */ |
9419 | 0 | if (keydata.addhd == 0) { |
9420 | 0 | deletekey = true; |
9421 | 0 | } else { |
9422 | 0 | keydata.removehd = |
9423 | 0 | now + |
9424 | 0 | dns_zone_mkey_month; |
9425 | 0 | keydata.flags |= |
9426 | 0 | DNS_KEYFLAG_REVOKE; |
9427 | 0 | } |
9428 | |
|
9429 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9430 | 0 | "Trusted key %d for " |
9431 | 0 | "zone %s is now revoked", |
9432 | 0 | keytag, namebuf); |
9433 | 0 | } else if (keydata.removehd < now) { |
9434 | | /* Scheduled for removal */ |
9435 | 0 | deletekey = true; |
9436 | |
|
9437 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9438 | 0 | "Revoked key %d for " |
9439 | 0 | "zone %s removal timer " |
9440 | 0 | "complete: deleting from " |
9441 | 0 | "the managed keys database", |
9442 | 0 | keytag, namebuf); |
9443 | 0 | } |
9444 | 0 | } else if (revoked && keydata.removehd == 0) { |
9445 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
9446 | 0 | "Active key %d for zone " |
9447 | 0 | "%s is revoked but " |
9448 | 0 | "did not self-sign; " |
9449 | 0 | "ignoring", |
9450 | 0 | keytag, namebuf); |
9451 | 0 | continue; |
9452 | 0 | } else if (secure) { |
9453 | 0 | if (keydata.removehd != 0) { |
9454 | | /* |
9455 | | * Key isn't revoked--but it |
9456 | | * seems it used to be. |
9457 | | * Remove it now and add it |
9458 | | * back as if it were a fresh key, |
9459 | | * with a 30-day acceptance timer. |
9460 | | */ |
9461 | 0 | deletekey = true; |
9462 | 0 | newkey = true; |
9463 | 0 | keydata.removehd = 0; |
9464 | 0 | keydata.addhd = now + |
9465 | 0 | dns_zone_mkey_month; |
9466 | |
|
9467 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9468 | 0 | "Revoked key %d for " |
9469 | 0 | "zone %s has returned: " |
9470 | 0 | "starting 30-day " |
9471 | 0 | "acceptance timer", |
9472 | 0 | keytag, namebuf); |
9473 | 0 | } else if (keydata.addhd > now) { |
9474 | 0 | pending++; |
9475 | 0 | } else if (keydata.addhd == 0) { |
9476 | 0 | keydata.addhd = now; |
9477 | 0 | } |
9478 | |
|
9479 | 0 | if (keydata.addhd <= now) { |
9480 | 0 | trustkey = true; |
9481 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9482 | 0 | "Key %d for zone %s " |
9483 | 0 | "is now trusted (%s)", |
9484 | 0 | keytag, namebuf, |
9485 | 0 | initial ? "initializing key " |
9486 | 0 | "verified" |
9487 | 0 | : "acceptance timer " |
9488 | 0 | "complete"); |
9489 | 0 | } |
9490 | 0 | } else if (keydata.addhd > now) { |
9491 | | /* |
9492 | | * Not secure, and key is pending: |
9493 | | * reset the acceptance timer |
9494 | | */ |
9495 | 0 | pending++; |
9496 | 0 | keydata.addhd = now + dns_zone_mkey_month; |
9497 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9498 | 0 | "Pending key %d " |
9499 | 0 | "for zone %s was " |
9500 | 0 | "not validated: restarting " |
9501 | 0 | "30-day acceptance timer", |
9502 | 0 | keytag, namebuf); |
9503 | 0 | } |
9504 | | |
9505 | 0 | if (!deletekey && !newkey) { |
9506 | 0 | updatekey = true; |
9507 | 0 | } |
9508 | 0 | } else if (secure) { |
9509 | | /* |
9510 | | * Key wasn't in the key zone but it's |
9511 | | * revoked now anyway, so just skip it |
9512 | | */ |
9513 | 0 | if (revoked) { |
9514 | 0 | continue; |
9515 | 0 | } |
9516 | | |
9517 | | /* Key wasn't in the key zone: add it */ |
9518 | 0 | newkey = true; |
9519 | |
|
9520 | 0 | if (initializing) { |
9521 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
9522 | 0 | "Initializing automatic trust " |
9523 | 0 | "anchor management for zone '%s'; " |
9524 | 0 | "DNSKEY ID %d is now trusted, " |
9525 | 0 | "waiving the normal 30-day " |
9526 | 0 | "waiting period.", |
9527 | 0 | namebuf, keytag); |
9528 | 0 | trustkey = true; |
9529 | 0 | } else { |
9530 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
9531 | 0 | "New key %d observed " |
9532 | 0 | "for zone '%s': " |
9533 | 0 | "starting 30-day " |
9534 | 0 | "acceptance timer", |
9535 | 0 | keytag, namebuf); |
9536 | 0 | } |
9537 | 0 | } else { |
9538 | | /* |
9539 | | * No previously known key, and the key is not |
9540 | | * secure, so skip it. |
9541 | | */ |
9542 | 0 | continue; |
9543 | 0 | } |
9544 | | |
9545 | | /* Delete old version */ |
9546 | 0 | if (deletekey || !newkey) { |
9547 | 0 | CHECK(update_one_rr(kfetch->db, ver, &diff, |
9548 | 0 | DNS_DIFFOP_DEL, keyname, 0, |
9549 | 0 | &keydatarr)); |
9550 | 0 | } |
9551 | | |
9552 | 0 | if (updatekey) { |
9553 | | /* Set refresh timer */ |
9554 | 0 | keydata.refresh = refresh_time(fetch, false); |
9555 | 0 | dns_rdata_reset(&keydatarr); |
9556 | 0 | isc_buffer_init(&keyb, key_buf, sizeof(key_buf)); |
9557 | 0 | result = dns_rdata_fromstruct(&keydatarr, zone->rdclass, |
9558 | 0 | dns_rdatatype_keydata, |
9559 | 0 | &keydata, &keyb); |
9560 | 0 | if (result != ISC_R_SUCCESS) { |
9561 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
9562 | 0 | "dns_rdata_fromstruct failed: " |
9563 | 0 | "KEYDATA %d: %s", |
9564 | 0 | keytag, isc_result_totext(result)); |
9565 | 0 | continue; |
9566 | 0 | } |
9567 | | |
9568 | | /* Insert updated version */ |
9569 | 0 | CHECK(update_one_rr(kfetch->db, ver, &diff, |
9570 | 0 | DNS_DIFFOP_ADD, keyname, 0, |
9571 | 0 | &keydatarr)); |
9572 | 0 | } else if (newkey) { |
9573 | | /* Convert DNSKEY to KEYDATA */ |
9574 | 0 | result = dns_rdata_tostruct(&dnskeyrr, &dnskey, NULL); |
9575 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
9576 | 0 | dns_keydata_fromdnskey(&keydata, &dnskey, 0, 0, 0, |
9577 | 0 | NULL); |
9578 | 0 | keydata.addhd = initializing |
9579 | 0 | ? now |
9580 | 0 | : now + dns_zone_mkey_month; |
9581 | 0 | keydata.refresh = refresh_time(fetch, false); |
9582 | 0 | dns_rdata_reset(&keydatarr); |
9583 | 0 | isc_buffer_init(&keyb, key_buf, sizeof(key_buf)); |
9584 | 0 | CHECK(dns_rdata_fromstruct(&keydatarr, zone->rdclass, |
9585 | 0 | dns_rdatatype_keydata, |
9586 | 0 | &keydata, &keyb)); |
9587 | | |
9588 | | /* Insert into key zone */ |
9589 | 0 | CHECK(update_one_rr(kfetch->db, ver, &diff, |
9590 | 0 | DNS_DIFFOP_ADD, keyname, 0, |
9591 | 0 | &keydatarr)); |
9592 | 0 | } |
9593 | | |
9594 | 0 | if (trustkey) { |
9595 | | /* Trust this key. */ |
9596 | 0 | result = dns_rdata_tostruct(&dnskeyrr, &dnskey, NULL); |
9597 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
9598 | 0 | trust_key(zone, keyname, &dnskey, false); |
9599 | 0 | } |
9600 | |
|
9601 | 0 | if (secure && !deletekey) { |
9602 | 0 | INSIST(newkey || updatekey); |
9603 | 0 | set_refreshkeytimer(zone, &keydata, now, false); |
9604 | 0 | } |
9605 | 0 | } |
9606 | | |
9607 | | /* |
9608 | | * RFC5011 says, "A trust point that has all of its trust anchors |
9609 | | * revoked is considered deleted and is treated as if the trust |
9610 | | * point was never configured." But if someone revoked their |
9611 | | * active key before the standby was trusted, that would mean the |
9612 | | * zone would suddenly be nonsecured. We avoid this by checking to |
9613 | | * see if there's pending keydata. If so, we put a null key in |
9614 | | * the security roots; then all queries to the zone will fail. |
9615 | | */ |
9616 | 0 | if (pending != 0) { |
9617 | 0 | fail_secure(zone, keyname); |
9618 | 0 | } |
9619 | |
|
9620 | 0 | done: |
9621 | 0 | if (!ISC_LIST_EMPTY(diff.tuples)) { |
9622 | | /* Write changes to journal file. */ |
9623 | 0 | CHECK(update_soa_serial(zone, kfetch->db, ver, &diff, mctx, |
9624 | 0 | zone->updatemethod)); |
9625 | 0 | CHECK(zone_journal(zone, &diff, NULL, "keyfetch_done")); |
9626 | 0 | commit = true; |
9627 | |
|
9628 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED); |
9629 | 0 | zone_needdump(zone, 30); |
9630 | 0 | } else if (result == ISC_R_NOMORE) { |
9631 | | /* |
9632 | | * If "updatekey" was true for all keys found in the DNSKEY |
9633 | | * response and the previous update of those keys happened |
9634 | | * during the same second (only possible if a key refresh was |
9635 | | * externally triggered), it may happen that all relevant |
9636 | | * update_one_rr() calls will return ISC_R_SUCCESS, but |
9637 | | * diff.tuples will remain empty. Reset result to |
9638 | | * ISC_R_SUCCESS to prevent a bogus warning from being logged. |
9639 | | */ |
9640 | 0 | result = ISC_R_SUCCESS; |
9641 | 0 | } |
9642 | | |
9643 | 0 | cleanup: |
9644 | 0 | if (result != ISC_R_SUCCESS) { |
9645 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
9646 | 0 | "error during trust anchor processing (%s): " |
9647 | 0 | "DNSSEC validation may be at risk", |
9648 | 0 | isc_result_totext(result)); |
9649 | 0 | } |
9650 | |
|
9651 | 0 | dns_diff_clear(&diff); |
9652 | |
|
9653 | 0 | if (ver != NULL) { |
9654 | 0 | dns_db_closeversion(kfetch->db, &ver, commit); |
9655 | 0 | } |
9656 | |
|
9657 | 0 | if (secroots != NULL) { |
9658 | 0 | dns_keytable_detach(&secroots); |
9659 | 0 | } |
9660 | |
|
9661 | 0 | INSIST(ver == NULL); |
9662 | |
|
9663 | 0 | return result; |
9664 | 0 | } |
9665 | | |
9666 | | /* |
9667 | | * Refresh the data in the key zone. Initiate a fetch to look up |
9668 | | * DNSKEY records at the trust anchor name. |
9669 | | */ |
9670 | | static void |
9671 | 0 | zone_refreshkeys(dns_zone_t *zone) { |
9672 | 0 | isc_result_t result; |
9673 | 0 | dns_rriterator_t rrit; |
9674 | 0 | dns_db_t *db = NULL; |
9675 | 0 | dns_dbversion_t *ver = NULL; |
9676 | 0 | dns_diff_t diff; |
9677 | 0 | dns_rdata_keydata_t kd; |
9678 | 0 | isc_stdtime_t now = isc_stdtime_now(); |
9679 | 0 | bool commit = false; |
9680 | 0 | bool fetching = false; |
9681 | 0 | bool timerset = false; |
9682 | |
|
9683 | 0 | ENTER; |
9684 | 0 | REQUIRE(zone->db != NULL); |
9685 | |
|
9686 | 0 | LOCK_ZONE(zone); |
9687 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
9688 | 0 | isc_time_settoepoch(&zone->refreshkeytime); |
9689 | 0 | UNLOCK_ZONE(zone); |
9690 | 0 | return; |
9691 | 0 | } |
9692 | | |
9693 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
9694 | 0 | dns_db_attach(zone->db, &db); |
9695 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
9696 | |
|
9697 | 0 | dns_diff_init(zone->mctx, &diff); |
9698 | |
|
9699 | 0 | CHECK(dns_db_newversion(db, &ver)); |
9700 | |
|
9701 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESHING); |
9702 | |
|
9703 | 0 | dns_rriterator_init(&rrit, db, ver, 0); |
9704 | 0 | for (result = dns_rriterator_first(&rrit); result == ISC_R_SUCCESS; |
9705 | 0 | result = dns_rriterator_nextrrset(&rrit)) |
9706 | 0 | { |
9707 | 0 | isc_stdtime_t timer = 0xffffffff; |
9708 | 0 | dns_name_t *name = NULL; |
9709 | 0 | dns_rdataset_t *kdset = NULL; |
9710 | 0 | uint32_t ttl; |
9711 | |
|
9712 | 0 | dns_rriterator_current(&rrit, &name, &ttl, &kdset, NULL); |
9713 | 0 | if (kdset == NULL || kdset->type != dns_rdatatype_keydata || |
9714 | 0 | !dns_rdataset_isassociated(kdset)) |
9715 | 0 | { |
9716 | 0 | continue; |
9717 | 0 | } |
9718 | | |
9719 | | /* |
9720 | | * Scan the stored keys looking for ones that need |
9721 | | * removal or refreshing |
9722 | | */ |
9723 | 0 | DNS_RDATASET_FOREACH(kdset) { |
9724 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
9725 | 0 | dns_rdataset_current(kdset, &rdata); |
9726 | 0 | result = dns_rdata_tostruct(&rdata, &kd, NULL); |
9727 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
9728 | | |
9729 | | /* Removal timer expired? */ |
9730 | 0 | if (kd.removehd != 0 && kd.removehd < now) { |
9731 | 0 | dns_rriterator_pause(&rrit); |
9732 | 0 | CHECK(update_one_rr(db, ver, &diff, |
9733 | 0 | DNS_DIFFOP_DEL, name, ttl, |
9734 | 0 | &rdata)); |
9735 | 0 | continue; |
9736 | 0 | } |
9737 | | |
9738 | | /* Acceptance timer expired? */ |
9739 | 0 | if (kd.addhd <= now) { |
9740 | 0 | timer = kd.addhd; |
9741 | 0 | } |
9742 | | |
9743 | | /* Or do we just need to refresh the keyset? */ |
9744 | 0 | if (timer > kd.refresh) { |
9745 | 0 | timer = kd.refresh; |
9746 | 0 | } |
9747 | |
|
9748 | 0 | dns_rriterator_pause(&rrit); |
9749 | 0 | set_refreshkeytimer(zone, &kd, now, false); |
9750 | 0 | timerset = true; |
9751 | 0 | } |
9752 | | |
9753 | 0 | if (timer > now) { |
9754 | 0 | continue; |
9755 | 0 | } |
9756 | | |
9757 | 0 | dns_rriterator_pause(&rrit); |
9758 | |
|
9759 | | #ifdef ENABLE_AFL |
9760 | | if (!dns_fuzzing_resolver) { |
9761 | | #endif /* ifdef ENABLE_AFL */ |
9762 | 0 | dns_zonefetch_t *fetch = NULL; |
9763 | 0 | dns_keyfetch_t *kfetch = NULL; |
9764 | | |
9765 | | /* |
9766 | | * This is a special query for RFC5011 maintenance |
9767 | | * of a trust anchor. We will be validating it |
9768 | | * in keyfetch_done() against a previously-known |
9769 | | * trust anchor; we do not want the normal |
9770 | | * validation process to occur. We set |
9771 | | * DNS_FETCHOPT_NOVALIDATE to suppress validation |
9772 | | * in the resolver, and DNS_FETCHOPT_UNSHARED so |
9773 | | * this fetch isn't combined with another one that |
9774 | | * might be validating. |
9775 | | * |
9776 | | * We must also use DNS_FETCHOPT_NOCACHED, because |
9777 | | * if it was not set and the cache still held a |
9778 | | * non-expired, validated version of the DNSKEY, |
9779 | | * then we'd receive the old, cached version |
9780 | | * instead of the new response - the old version |
9781 | | * would have a higher trust level. |
9782 | | */ |
9783 | 0 | fetch = isc_mem_get(zone->mctx, |
9784 | 0 | sizeof(dns_zonefetch_t)); |
9785 | 0 | *fetch = (dns_zonefetch_t){ |
9786 | 0 | .zone = zone, |
9787 | 0 | .options = DNS_FETCHOPT_NOVALIDATE | |
9788 | 0 | DNS_FETCHOPT_UNSHARED | |
9789 | 0 | DNS_FETCHOPT_NOCACHED, |
9790 | 0 | .fetchtype = ZONEFETCHTYPE_KEY, |
9791 | 0 | .fetchmethods = |
9792 | 0 | (dns_zonefetch_methods_t){ |
9793 | 0 | .start_fetch = keyfetch_start, |
9794 | 0 | .continue_fetch = |
9795 | 0 | keyfetch_continue, |
9796 | 0 | .cancel_fetch = keyfetch_cancel, |
9797 | 0 | .cleanup_fetch = |
9798 | 0 | keyfetch_cleanup, |
9799 | 0 | .done_fetch = keyfetch_done, |
9800 | 0 | }, |
9801 | 0 | }; |
9802 | 0 | isc_mem_attach(zone->mctx, &fetch->mctx); |
9803 | |
|
9804 | 0 | zone->fetchcount[ZONEFETCHTYPE_KEY]++; |
9805 | |
|
9806 | 0 | kfetch = &fetch->fetchdata.keyfetch; |
9807 | 0 | dns_rdataset_init(&kfetch->keydataset); |
9808 | 0 | dns_rdataset_clone(kdset, &kfetch->keydataset); |
9809 | 0 | dns_db_attach(db, &kfetch->db); |
9810 | |
|
9811 | 0 | dns_zonefetch_schedule(fetch, name); |
9812 | |
|
9813 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
9814 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
9815 | 0 | dns_name_format(name, namebuf, sizeof(namebuf)); |
9816 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
9817 | 0 | "Creating key fetch in " |
9818 | 0 | "zone_refreshkeys() for '%s'", |
9819 | 0 | namebuf); |
9820 | 0 | } |
9821 | 0 | fetching = true; |
9822 | | #ifdef ENABLE_AFL |
9823 | | } |
9824 | | #endif /* ifdef ENABLE_AFL */ |
9825 | 0 | } |
9826 | 0 | if (!ISC_LIST_EMPTY(diff.tuples)) { |
9827 | 0 | CHECK(update_soa_serial(zone, db, ver, &diff, zone->mctx, |
9828 | 0 | zone->updatemethod)); |
9829 | 0 | CHECK(zone_journal(zone, &diff, NULL, "zone_refreshkeys")); |
9830 | 0 | commit = true; |
9831 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED); |
9832 | 0 | zone_needdump(zone, 30); |
9833 | 0 | } |
9834 | | |
9835 | 0 | cleanup: |
9836 | 0 | if (!timerset) { |
9837 | 0 | isc_time_settoepoch(&zone->refreshkeytime); |
9838 | 0 | } |
9839 | |
|
9840 | 0 | if (!fetching) { |
9841 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESHING); |
9842 | 0 | } |
9843 | |
|
9844 | 0 | dns_diff_clear(&diff); |
9845 | 0 | if (ver != NULL) { |
9846 | 0 | dns_rriterator_destroy(&rrit); |
9847 | 0 | dns_db_closeversion(db, &ver, commit); |
9848 | 0 | } |
9849 | 0 | dns_db_detach(&db); |
9850 | |
|
9851 | 0 | UNLOCK_ZONE(zone); |
9852 | |
|
9853 | 0 | INSIST(ver == NULL); |
9854 | 0 | } |
9855 | | |
9856 | | typedef enum inline_sync_action { |
9857 | | inline_sync_none = 0, |
9858 | | inline_sync_bootstrap, |
9859 | | inline_sync_incremental_start, |
9860 | | inline_sync_incremental_continue, |
9861 | | } inline_sync_action_t; |
9862 | | |
9863 | | /* |
9864 | | * Inline-signing secure zones pull raw-zone changes during maintenance. |
9865 | | * Raw-zone updates queue immediate secure maintenance. |
9866 | | */ |
9867 | | static inline_sync_action_t |
9868 | 0 | zone_inline_sync_action(dns_zone_t *zone) { |
9869 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
9870 | |
|
9871 | 0 | if (!dns__zone_inline_secure(zone)) { |
9872 | 0 | return inline_sync_none; |
9873 | 0 | } |
9874 | | |
9875 | 0 | if (zone->inline_sync_phase == inline_sync_full) { |
9876 | 0 | zone->inline_sync_phase = inline_sync_idle; |
9877 | | /* |
9878 | | * A full rebuild replaces any parked incremental sync state; |
9879 | | * zone_maintenance() cancels zone->iss before bootstrapping. |
9880 | | */ |
9881 | 0 | return inline_sync_bootstrap; |
9882 | 0 | } |
9883 | | |
9884 | 0 | if (zone->iss != NULL) { |
9885 | 0 | return inline_sync_incremental_continue; |
9886 | 0 | } |
9887 | | |
9888 | 0 | if (zone->inline_sync_phase == inline_sync_incremental) { |
9889 | | /* |
9890 | | * An incremental sync is a one-shot raw-to-secure request. |
9891 | | * Parked incremental syncs are continued before this request is |
9892 | | * consumed, so raw updates that arrive during signing are not |
9893 | | * collapsed into continuation wakes. |
9894 | | */ |
9895 | 0 | zone->inline_sync_phase = inline_sync_idle; |
9896 | 0 | if (zone->db == NULL) { |
9897 | 0 | return inline_sync_bootstrap; |
9898 | 0 | } |
9899 | 0 | return inline_sync_incremental_start; |
9900 | 0 | } |
9901 | | |
9902 | 0 | INSIST(zone->inline_sync_phase == inline_sync_idle); |
9903 | 0 | return inline_sync_none; |
9904 | 0 | } |
9905 | | |
9906 | | static bool |
9907 | 0 | zone_inline_sync_pending(dns_zone_t *zone) { |
9908 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
9909 | |
|
9910 | 0 | return dns__zone_inline_secure(zone) && |
9911 | 0 | (zone->iss != NULL || |
9912 | 0 | zone->inline_sync_phase == inline_sync_incremental || |
9913 | 0 | zone->inline_sync_phase == inline_sync_full); |
9914 | 0 | } |
9915 | | |
9916 | | static void |
9917 | 0 | zone_maintenance(dns_zone_t *zone) { |
9918 | 0 | isc_time_t now; |
9919 | 0 | isc_result_t result; |
9920 | 0 | bool load_pending, exiting, dumping, viewok = false, notify; |
9921 | 0 | bool refreshkeys, rekey; |
9922 | 0 | bool sign = false, resign = false, chain = false, warn_expire = false; |
9923 | 0 | inline_sync_action_t inline_sync = inline_sync_none; |
9924 | |
|
9925 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
9926 | 0 | ENTER; |
9927 | | |
9928 | | /* |
9929 | | * Are we pending load/reload, exiting, or unconfigured |
9930 | | * (e.g. because of a syntax failure in the config file)? |
9931 | | * If so, don't attempt maintenance. |
9932 | | */ |
9933 | 0 | LOCK_ZONE(zone); |
9934 | 0 | load_pending = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING); |
9935 | 0 | exiting = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING); |
9936 | 0 | if (!load_pending && !exiting && zone->view != NULL) { |
9937 | 0 | dns_adb_t *adb = NULL; |
9938 | 0 | dns_view_getadb(zone->view, &adb); |
9939 | 0 | if (adb != NULL) { |
9940 | 0 | dns_adb_detach(&adb); |
9941 | 0 | viewok = true; |
9942 | 0 | } |
9943 | 0 | } |
9944 | 0 | UNLOCK_ZONE(zone); |
9945 | |
|
9946 | 0 | if (load_pending || exiting || !viewok) { |
9947 | 0 | return; |
9948 | 0 | } |
9949 | | |
9950 | 0 | now = isc_time_now(); |
9951 | | |
9952 | | /* |
9953 | | * Expire check. |
9954 | | */ |
9955 | 0 | switch (zone->type) { |
9956 | 0 | case dns_zone_redirect: |
9957 | 0 | if (dns_remote_addresses(&zone->primaries) == NULL) { |
9958 | 0 | break; |
9959 | 0 | } |
9960 | 0 | FALLTHROUGH; |
9961 | 0 | case dns_zone_secondary: |
9962 | 0 | case dns_zone_mirror: |
9963 | 0 | case dns_zone_stub: |
9964 | 0 | LOCK_ZONE(zone); |
9965 | 0 | if (isc_time_compare(&now, &zone->expiretime) >= 0 && |
9966 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) |
9967 | 0 | { |
9968 | 0 | zone_expire(zone); |
9969 | 0 | zone->refreshtime = now; |
9970 | 0 | } |
9971 | 0 | UNLOCK_ZONE(zone); |
9972 | 0 | break; |
9973 | 0 | default: |
9974 | 0 | break; |
9975 | 0 | } |
9976 | | |
9977 | | /* |
9978 | | * Up to date check. |
9979 | | */ |
9980 | 0 | switch (zone->type) { |
9981 | 0 | case dns_zone_redirect: |
9982 | 0 | if (dns_remote_addresses(&zone->primaries) == NULL) { |
9983 | 0 | break; |
9984 | 0 | } |
9985 | 0 | FALLTHROUGH; |
9986 | 0 | case dns_zone_secondary: |
9987 | 0 | case dns_zone_mirror: |
9988 | 0 | case dns_zone_stub: |
9989 | 0 | LOCK_ZONE(zone); |
9990 | 0 | if (isc_time_compare(&now, &zone->refreshtime) >= 0) { |
9991 | 0 | zone_refresh(zone); |
9992 | 0 | } |
9993 | 0 | UNLOCK_ZONE(zone); |
9994 | 0 | break; |
9995 | 0 | default: |
9996 | 0 | break; |
9997 | 0 | } |
9998 | | |
9999 | | /* |
10000 | | * Secondaries send notifies before backing up to disk, |
10001 | | * primaries after. |
10002 | | */ |
10003 | 0 | LOCK_ZONE(zone); |
10004 | 0 | if (zone->notifysoa.notifydefer != 0 && |
10005 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOTIFYNODEFER) && |
10006 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOTIFYDEFERRED)) |
10007 | 0 | { |
10008 | 0 | if (isc_time_compare(&now, &zone->notifytime) > 0) { |
10009 | 0 | zone->notifytime = now; |
10010 | 0 | } |
10011 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOTIFYDEFERRED); |
10012 | 0 | DNS_ZONE_TIME_ADD(&zone->notifytime, |
10013 | 0 | zone->notifysoa.notifydefer, |
10014 | 0 | &zone->notifytime); |
10015 | 0 | } |
10016 | 0 | notify = (zone->type == dns_zone_secondary || |
10017 | 0 | zone->type == dns_zone_mirror) && |
10018 | 0 | (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY) || |
10019 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDSTARTUPNOTIFY)) && |
10020 | 0 | isc_time_compare(&now, &zone->notifytime) >= 0; |
10021 | 0 | UNLOCK_ZONE(zone); |
10022 | |
|
10023 | 0 | if (notify) { |
10024 | 0 | zone_notify(zone, &now); |
10025 | 0 | } |
10026 | | |
10027 | | /* |
10028 | | * Do we need to consolidate the backing store? |
10029 | | */ |
10030 | 0 | switch (zone->type) { |
10031 | 0 | case dns_zone_primary: |
10032 | 0 | case dns_zone_secondary: |
10033 | 0 | case dns_zone_mirror: |
10034 | 0 | case dns_zone_key: |
10035 | 0 | case dns_zone_redirect: |
10036 | 0 | case dns_zone_stub: |
10037 | 0 | LOCK_ZONE(zone); |
10038 | 0 | if (zone->masterfile != NULL && |
10039 | 0 | isc_time_compare(&now, &zone->dumptime) >= 0 && |
10040 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) && |
10041 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP)) |
10042 | 0 | { |
10043 | 0 | dumping = was_dumping(zone); |
10044 | 0 | } else { |
10045 | 0 | dumping = true; |
10046 | 0 | } |
10047 | 0 | UNLOCK_ZONE(zone); |
10048 | 0 | if (!dumping) { |
10049 | 0 | result = zone_dump(zone, true); /* loop locked */ |
10050 | 0 | if (result != ISC_R_SUCCESS) { |
10051 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
10052 | 0 | "dump failed: %s", |
10053 | 0 | isc_result_totext(result)); |
10054 | 0 | } |
10055 | 0 | } |
10056 | 0 | break; |
10057 | 0 | default: |
10058 | 0 | break; |
10059 | 0 | } |
10060 | | |
10061 | | /* |
10062 | | * Primary/redirect zones send notifies now, if needed |
10063 | | */ |
10064 | 0 | switch (zone->type) { |
10065 | 0 | case dns_zone_primary: |
10066 | 0 | case dns_zone_redirect: |
10067 | 0 | LOCK_ZONE(zone); |
10068 | 0 | notify = (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY) || |
10069 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDSTARTUPNOTIFY)) && |
10070 | 0 | isc_time_compare(&now, &zone->notifytime) >= 0; |
10071 | 0 | UNLOCK_ZONE(zone); |
10072 | 0 | if (notify) { |
10073 | 0 | zone_notify(zone, &now); |
10074 | 0 | } |
10075 | 0 | default: |
10076 | 0 | break; |
10077 | 0 | } |
10078 | | |
10079 | 0 | LOCK_ZONE(zone); |
10080 | 0 | inline_sync = zone_inline_sync_action(zone); |
10081 | 0 | UNLOCK_ZONE(zone); |
10082 | |
|
10083 | 0 | switch (inline_sync) { |
10084 | 0 | case inline_sync_bootstrap: |
10085 | 0 | zone_clear_inline_sync(zone); |
10086 | 0 | inline_secure_bootstrap(zone); |
10087 | 0 | break; |
10088 | 0 | case inline_sync_incremental_start: |
10089 | 0 | inline_sync_run(zone); |
10090 | 0 | break; |
10091 | 0 | case inline_sync_incremental_continue: |
10092 | 0 | inline_sync_resume(zone); |
10093 | 0 | break; |
10094 | 0 | case inline_sync_none: |
10095 | 0 | break; |
10096 | 0 | } |
10097 | | |
10098 | | /* |
10099 | | * Apply one queued DB mutation before snapshotting DNSSEC maintenance |
10100 | | * timers, so chain/sign work observes the resulting zone state. |
10101 | | */ |
10102 | 0 | zone_process_maintenance_request(zone); |
10103 | |
|
10104 | 0 | LOCK_ZONE(zone); |
10105 | 0 | if (zone_inline_sync_pending(zone)) { |
10106 | 0 | dns__zone_settimer(zone, now); |
10107 | 0 | UNLOCK_ZONE(zone); |
10108 | 0 | return; |
10109 | 0 | } |
10110 | 0 | UNLOCK_ZONE(zone); |
10111 | |
|
10112 | 0 | switch (zone->type) { |
10113 | 0 | case dns_zone_primary: |
10114 | 0 | case dns_zone_redirect: |
10115 | 0 | case dns_zone_secondary: |
10116 | 0 | LOCK_ZONE(zone); |
10117 | 0 | sign = time_greater_equal(now, zone->signingtime); |
10118 | 0 | resign = time_greater_equal(now, zone->resigntime); |
10119 | 0 | chain = time_greater_equal(now, zone->nsec3chaintime); |
10120 | 0 | warn_expire = time_greater_equal(now, zone->keywarntime); |
10121 | 0 | UNLOCK_ZONE(zone); |
10122 | 0 | break; |
10123 | | |
10124 | 0 | default: |
10125 | 0 | break; |
10126 | 0 | } |
10127 | | |
10128 | | /* |
10129 | | * Do we need to refresh keys? |
10130 | | */ |
10131 | 0 | switch (zone->type) { |
10132 | 0 | case dns_zone_key: |
10133 | 0 | LOCK_ZONE(zone); |
10134 | 0 | refreshkeys = isc_time_compare(&now, &zone->refreshkeytime) >= |
10135 | 0 | 0 && |
10136 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) && |
10137 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESHING); |
10138 | 0 | UNLOCK_ZONE(zone); |
10139 | 0 | if (refreshkeys) { |
10140 | 0 | zone_refreshkeys(zone); |
10141 | 0 | } |
10142 | 0 | break; |
10143 | 0 | case dns_zone_primary: |
10144 | 0 | LOCK_ZONE(zone); |
10145 | 0 | rekey = time_greater_equal(now, zone->refreshkeytime); |
10146 | 0 | UNLOCK_ZONE(zone); |
10147 | 0 | if (rekey) { |
10148 | 0 | zone_rekey(zone); |
10149 | 0 | } |
10150 | 0 | default: |
10151 | 0 | break; |
10152 | 0 | } |
10153 | | |
10154 | 0 | switch (zone->type) { |
10155 | 0 | case dns_zone_primary: |
10156 | 0 | case dns_zone_redirect: |
10157 | 0 | case dns_zone_secondary: |
10158 | | /* |
10159 | | * Do the DNSSEC work that was due before key maintenance. |
10160 | | */ |
10161 | 0 | if (chain) { |
10162 | 0 | zone_nsec3chain(zone); |
10163 | 0 | } else if (sign) { |
10164 | 0 | zone_sign(zone); |
10165 | 0 | } else if (resign) { |
10166 | 0 | zone_resigninc(zone); |
10167 | 0 | } |
10168 | | |
10169 | | /* |
10170 | | * Do we need to issue a key expiry warning? |
10171 | | */ |
10172 | 0 | if (warn_expire) { |
10173 | 0 | set_key_expiry_warning(zone, zone->key_expiry, |
10174 | 0 | isc_time_seconds(&now)); |
10175 | 0 | } |
10176 | 0 | break; |
10177 | | |
10178 | 0 | default: |
10179 | 0 | break; |
10180 | 0 | } |
10181 | 0 | LOCK_ZONE(zone); |
10182 | 0 | dns__zone_settimer(zone, now); |
10183 | 0 | UNLOCK_ZONE(zone); |
10184 | 0 | } |
10185 | | |
10186 | | void |
10187 | 0 | dns_zone_markdirty(dns_zone_t *zone) { |
10188 | 0 | dns_zone_t *secure = NULL; |
10189 | | |
10190 | | /* |
10191 | | * Obtaining a lock on zone->secure could result in a deadlock due to |
10192 | | * a LOR, so spin if both locks cannot be obtained. |
10193 | | */ |
10194 | 0 | again: |
10195 | 0 | LOCK_ZONE(zone); |
10196 | 0 | if (zone->type == dns_zone_primary) { |
10197 | 0 | if (dns__zone_inline_raw(zone)) { |
10198 | 0 | isc_result_t result; |
10199 | |
|
10200 | 0 | secure = zone->secure; |
10201 | 0 | INSIST(secure != zone); |
10202 | 0 | TRYLOCK_ZONE(result, secure); |
10203 | 0 | if (result != ISC_R_SUCCESS) { |
10204 | 0 | UNLOCK_ZONE(zone); |
10205 | 0 | secure = NULL; |
10206 | 0 | isc_thread_yield(); |
10207 | 0 | goto again; |
10208 | 0 | } |
10209 | | |
10210 | 0 | zone_schedule_inline_sync(secure, |
10211 | 0 | inline_sync_incremental); |
10212 | 0 | } |
10213 | | |
10214 | | /* XXXMPA make separate call back */ |
10215 | 0 | dns__zone_set_resigntime(zone); |
10216 | 0 | if (zone->loop != NULL) { |
10217 | 0 | dns__zone_settimer(zone, isc_time_now()); |
10218 | 0 | } |
10219 | 0 | } |
10220 | 0 | if (secure != NULL) { |
10221 | 0 | UNLOCK_ZONE(secure); |
10222 | 0 | } |
10223 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
10224 | 0 | UNLOCK_ZONE(zone); |
10225 | 0 | } |
10226 | | |
10227 | | static void |
10228 | 0 | zone_expire(dns_zone_t *zone) { |
10229 | 0 | dns_db_t *db = NULL; |
10230 | | |
10231 | | /* |
10232 | | * 'zone' locked by caller. |
10233 | | */ |
10234 | |
|
10235 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
10236 | |
|
10237 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_EXPIRED); |
10238 | 0 | zone->refresh = DNS_ZONE_DEFAULTREFRESH; |
10239 | 0 | zone->retry = DNS_ZONE_DEFAULTRETRY; |
10240 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HAVETIMERS); |
10241 | |
|
10242 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, "expired"); |
10243 | | |
10244 | | /* |
10245 | | * An RPZ zone has expired; before unloading it, we must |
10246 | | * first remove it from the RPZ summary database. The |
10247 | | * easiest way to do this is "update" it with an empty |
10248 | | * database so that the update callback synchronizes |
10249 | | * the diff automatically. |
10250 | | */ |
10251 | 0 | if (zone->rpzs != NULL && zone->rpz_num != DNS_RPZ_INVALID_NUM) { |
10252 | 0 | isc_result_t result; |
10253 | 0 | dns_rpz_zone_t *rpz = zone->rpzs->zones[zone->rpz_num]; |
10254 | |
|
10255 | 0 | CHECK(dns_db_create(zone->mctx, ZONEDB_DEFAULT, &zone->origin, |
10256 | 0 | dns_dbtype_zone, zone->rdclass, 0, NULL, |
10257 | 0 | &db)); |
10258 | 0 | CHECK(dns_rpz_dbupdate_callback(db, rpz)); |
10259 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
10260 | 0 | "response-policy zone expired; " |
10261 | 0 | "policies unloaded"); |
10262 | 0 | } |
10263 | | |
10264 | 0 | cleanup: |
10265 | 0 | if (db != NULL) { |
10266 | 0 | dns_db_detach(&db); |
10267 | 0 | } |
10268 | |
|
10269 | 0 | zone_unload(zone); |
10270 | 0 | } |
10271 | | |
10272 | | static void |
10273 | 0 | zone_refresh(dns_zone_t *zone) { |
10274 | 0 | isc_interval_t i; |
10275 | 0 | uint32_t oldflags; |
10276 | 0 | isc_result_t result; |
10277 | |
|
10278 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10279 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
10280 | |
|
10281 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
10282 | 0 | return; |
10283 | 0 | } |
10284 | | |
10285 | | /* |
10286 | | * Set DNS_ZONEFLG_REFRESH so that there is only one refresh operation |
10287 | | * in progress at a time. |
10288 | | */ |
10289 | | |
10290 | 0 | oldflags = atomic_load(&zone->flags); |
10291 | 0 | if (dns_remote_addresses(&zone->primaries) == NULL) { |
10292 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOPRIMARIES); |
10293 | 0 | if ((oldflags & DNS_ZONEFLG_NOPRIMARIES) == 0) { |
10294 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
10295 | 0 | ISC_LOG_ERROR, |
10296 | 0 | "cannot refresh: no primaries"); |
10297 | 0 | } |
10298 | 0 | return; |
10299 | 0 | } |
10300 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESH); |
10301 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOEDNS); |
10302 | 0 | if ((oldflags & (DNS_ZONEFLG_REFRESH | DNS_ZONEFLG_LOADING)) != 0) { |
10303 | 0 | return; |
10304 | 0 | } |
10305 | | |
10306 | | /* |
10307 | | * Set the next refresh time as if refresh check has failed. |
10308 | | * Setting this to the retry time will do that. XXXMLG |
10309 | | * If we are successful it will be reset using zone->refresh. |
10310 | | */ |
10311 | 0 | isc_interval_set(&i, zone->retry - isc_random_uniform(zone->retry / 4), |
10312 | 0 | 0); |
10313 | 0 | result = isc_time_nowplusinterval(&zone->refreshtime, &i); |
10314 | 0 | if (result != ISC_R_SUCCESS) { |
10315 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_WARNING, |
10316 | 0 | "isc_time_nowplusinterval() failed: %s", |
10317 | 0 | isc_result_totext(result)); |
10318 | 0 | } |
10319 | | |
10320 | | /* |
10321 | | * When lacking user-specified timer values from the SOA, |
10322 | | * do exponential backoff of the retry time up to a |
10323 | | * maximum of six hours. |
10324 | | */ |
10325 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HAVETIMERS)) { |
10326 | 0 | zone->retry = ISC_MIN(zone->retry * 2, 6 * 3600); |
10327 | 0 | } |
10328 | |
|
10329 | 0 | dns_remote_reset(&zone->primaries, true); |
10330 | | |
10331 | | /* initiate soa query */ |
10332 | 0 | queue_soa_query(zone); |
10333 | 0 | } |
10334 | | |
10335 | | static void |
10336 | 0 | zone_refresh_async(void *arg) { |
10337 | 0 | dns_zone_t *zone = arg; |
10338 | |
|
10339 | 0 | LOCK_ZONE(zone); |
10340 | 0 | zone_refresh(zone); |
10341 | 0 | UNLOCK_ZONE(zone); |
10342 | |
|
10343 | 0 | dns_zone_detach(&zone); |
10344 | 0 | } |
10345 | | |
10346 | | void |
10347 | 0 | dns_zone_refresh(dns_zone_t *zone) { |
10348 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10349 | |
|
10350 | 0 | dns_zone_ref(zone); |
10351 | 0 | isc_async_run(zone->loop, zone_refresh_async, zone); |
10352 | 0 | } |
10353 | | |
10354 | | static isc_result_t |
10355 | | zone_journal_rollforward(dns_zone_t *zone, dns_db_t *db, bool *needdump, |
10356 | 2 | bool *fixjournal) { |
10357 | 2 | dns_journal_t *journal = NULL; |
10358 | 2 | unsigned int options; |
10359 | 2 | isc_result_t result; |
10360 | | |
10361 | 2 | if (zone->type == dns_zone_primary && |
10362 | 2 | (dns__zone_inline_secure(zone) || |
10363 | 2 | (zone->update_acl != NULL || zone->ssutable != NULL))) |
10364 | 0 | { |
10365 | 0 | options = DNS_JOURNALOPT_RESIGN; |
10366 | 2 | } else { |
10367 | 2 | options = 0; |
10368 | 2 | } |
10369 | | |
10370 | 2 | result = dns_journal_open(zone->mctx, zone->journal, DNS_JOURNAL_READ, |
10371 | 2 | &journal); |
10372 | 2 | if (result == ISC_R_NOTFOUND) { |
10373 | 2 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(3), |
10374 | 2 | "no journal file, but that's OK "); |
10375 | 2 | return ISC_R_SUCCESS; |
10376 | 2 | } else if (result != ISC_R_SUCCESS) { |
10377 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR, |
10378 | 0 | "journal open failed: %s", |
10379 | 0 | isc_result_totext(result)); |
10380 | 0 | return result; |
10381 | 0 | } |
10382 | | |
10383 | 0 | if (dns_journal_empty(journal)) { |
10384 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_DEBUG(1), |
10385 | 0 | "journal empty"); |
10386 | 0 | dns_journal_destroy(&journal); |
10387 | 0 | return ISC_R_SUCCESS; |
10388 | 0 | } |
10389 | | |
10390 | 0 | result = dns_journal_rollforward(journal, db, options); |
10391 | 0 | switch (result) { |
10392 | 0 | case ISC_R_SUCCESS: |
10393 | 0 | *needdump = true; |
10394 | 0 | FALLTHROUGH; |
10395 | 0 | case DNS_R_UPTODATE: |
10396 | 0 | if (dns_journal_recovered(journal)) { |
10397 | 0 | *fixjournal = true; |
10398 | 0 | dns_zone_logc( |
10399 | 0 | zone, DNS_LOGCATEGORY_ZONELOAD, |
10400 | 0 | ISC_LOG_DEBUG(1), |
10401 | 0 | "journal rollforward completed successfully " |
10402 | 0 | "using old journal format: %s", |
10403 | 0 | isc_result_totext(result)); |
10404 | 0 | } else { |
10405 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, |
10406 | 0 | ISC_LOG_DEBUG(1), |
10407 | 0 | "journal rollforward completed " |
10408 | 0 | "successfully: %s", |
10409 | 0 | isc_result_totext(result)); |
10410 | 0 | } |
10411 | |
|
10412 | 0 | dns_journal_destroy(&journal); |
10413 | 0 | return ISC_R_SUCCESS; |
10414 | 0 | case ISC_R_NOTFOUND: |
10415 | 0 | case ISC_R_RANGE: |
10416 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR, |
10417 | 0 | "journal rollforward failed: journal out of sync " |
10418 | 0 | "with zone"); |
10419 | 0 | dns_journal_destroy(&journal); |
10420 | 0 | return result; |
10421 | 0 | default: |
10422 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_ZONELOAD, ISC_LOG_ERROR, |
10423 | 0 | "journal rollforward failed: %s", |
10424 | 0 | isc_result_totext(result)); |
10425 | 0 | dns_journal_destroy(&journal); |
10426 | 0 | return result; |
10427 | 0 | } |
10428 | 0 | } |
10429 | | |
10430 | | static void |
10431 | 0 | zone_journal_compact(dns_zone_t *zone, dns_db_t *db, uint32_t serial) { |
10432 | 0 | isc_result_t result; |
10433 | 0 | int32_t journalsize; |
10434 | 0 | dns_dbversion_t *ver = NULL; |
10435 | 0 | uint64_t dbsize; |
10436 | 0 | uint32_t options = 0; |
10437 | |
|
10438 | 0 | INSIST(LOCKED_ZONE(zone)); |
10439 | 0 | if (dns__zone_inline_raw(zone)) { |
10440 | 0 | INSIST(LOCKED_ZONE(zone->secure)); |
10441 | 0 | } |
10442 | |
|
10443 | 0 | journalsize = zone->journalsize; |
10444 | 0 | if (journalsize == -1) { |
10445 | 0 | journalsize = DNS_JOURNAL_SIZE_MAX; |
10446 | 0 | dns_db_currentversion(db, &ver); |
10447 | 0 | result = dns_db_getsize(db, ver, NULL, &dbsize); |
10448 | 0 | dns_db_closeversion(db, &ver, false); |
10449 | 0 | if (result != ISC_R_SUCCESS) { |
10450 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
10451 | 0 | "zone_journal_compact: " |
10452 | 0 | "could not get zone size: %s", |
10453 | 0 | isc_result_totext(result)); |
10454 | 0 | } else if (dbsize < DNS_JOURNAL_SIZE_MAX / 2) { |
10455 | 0 | journalsize = (int32_t)dbsize * 2; |
10456 | 0 | } |
10457 | 0 | } |
10458 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FIXJOURNAL)) { |
10459 | 0 | options |= DNS_JOURNAL_COMPACTALL; |
10460 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FIXJOURNAL); |
10461 | 0 | zone_debuglog(zone, __func__, 1, "repair full journal"); |
10462 | 0 | } else { |
10463 | 0 | zone_debuglog(zone, __func__, 1, "target journal size %d", |
10464 | 0 | journalsize); |
10465 | 0 | } |
10466 | 0 | result = dns_journal_compact(zone->mctx, zone->journal, serial, options, |
10467 | 0 | journalsize); |
10468 | 0 | switch (result) { |
10469 | 0 | case ISC_R_SUCCESS: |
10470 | 0 | case ISC_R_NOSPACE: |
10471 | 0 | case ISC_R_NOTFOUND: |
10472 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), "dns_journal_compact: %s", |
10473 | 0 | isc_result_totext(result)); |
10474 | 0 | break; |
10475 | 0 | default: |
10476 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
10477 | 0 | "dns_journal_compact failed: %s", |
10478 | 0 | isc_result_totext(result)); |
10479 | 0 | break; |
10480 | 0 | } |
10481 | 0 | } |
10482 | | |
10483 | | isc_result_t |
10484 | 0 | dns_zone_flush(dns_zone_t *zone) { |
10485 | 0 | isc_result_t result = ISC_R_SUCCESS; |
10486 | 0 | bool dumping; |
10487 | |
|
10488 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10489 | |
|
10490 | 0 | LOCK_ZONE(zone); |
10491 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FLUSH); |
10492 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) && |
10493 | 0 | zone->masterfile != NULL) |
10494 | 0 | { |
10495 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDCOMPACT); |
10496 | 0 | result = ISC_R_ALREADYRUNNING; |
10497 | 0 | dumping = was_dumping(zone); |
10498 | 0 | } else { |
10499 | 0 | dumping = true; |
10500 | 0 | } |
10501 | 0 | UNLOCK_ZONE(zone); |
10502 | 0 | if (!dumping) { |
10503 | 0 | result = zone_dump(zone, true); |
10504 | 0 | } |
10505 | 0 | return result; |
10506 | 0 | } |
10507 | | |
10508 | | static void |
10509 | 0 | zone_needdump(dns_zone_t *zone, unsigned int delay) { |
10510 | 0 | isc_time_t dumptime; |
10511 | 0 | isc_time_t now; |
10512 | | |
10513 | | /* |
10514 | | * 'zone' locked by caller |
10515 | | */ |
10516 | |
|
10517 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10518 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
10519 | 0 | ENTER; |
10520 | | |
10521 | | /* |
10522 | | * Do we have a place to dump to and are we loaded? |
10523 | | */ |
10524 | 0 | if (zone->masterfile == NULL || |
10525 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) == 0) |
10526 | 0 | { |
10527 | 0 | return; |
10528 | 0 | } |
10529 | | |
10530 | 0 | now = isc_time_now(); |
10531 | | /* add some noise */ |
10532 | 0 | DNS_ZONE_JITTER_ADD(&now, delay, &dumptime); |
10533 | |
|
10534 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDDUMP); |
10535 | 0 | if (isc_time_isepoch(&zone->dumptime) || |
10536 | 0 | isc_time_compare(&zone->dumptime, &dumptime) > 0) |
10537 | 0 | { |
10538 | 0 | zone->dumptime = dumptime; |
10539 | 0 | } |
10540 | 0 | if (zone->loop != NULL) { |
10541 | 0 | dns__zone_settimer(zone, now); |
10542 | 0 | } |
10543 | 0 | } |
10544 | | |
10545 | | static void |
10546 | 0 | dump_done(void *arg, isc_result_t result) { |
10547 | 0 | dns_zone_t *zone = arg; |
10548 | 0 | dns_zone_t *secure = NULL; |
10549 | 0 | bool again = false; |
10550 | 0 | bool compact = false; |
10551 | 0 | uint32_t serial; |
10552 | 0 | isc_result_t tresult = ISC_R_UNSET; |
10553 | |
|
10554 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10555 | |
|
10556 | 0 | ENTER; |
10557 | | |
10558 | | /* |
10559 | | * Adjust modification time of zone file to preserve expire timing. |
10560 | | */ |
10561 | 0 | if ((zone->type == dns_zone_secondary || |
10562 | 0 | zone->type == dns_zone_mirror || |
10563 | 0 | zone->type == dns_zone_redirect) && |
10564 | 0 | result == ISC_R_SUCCESS) |
10565 | 0 | { |
10566 | 0 | LOCK_ZONE(zone); |
10567 | 0 | isc_time_t when; |
10568 | 0 | isc_interval_t i; |
10569 | 0 | isc_interval_set(&i, zone->expire, 0); |
10570 | 0 | result = isc_time_subtract(&zone->expiretime, &i, &when); |
10571 | 0 | if (result == ISC_R_SUCCESS) { |
10572 | 0 | (void)isc_file_settime(zone->masterfile, &when); |
10573 | 0 | } else { |
10574 | 0 | result = ISC_R_SUCCESS; |
10575 | 0 | } |
10576 | 0 | UNLOCK_ZONE(zone); |
10577 | 0 | } |
10578 | |
|
10579 | 0 | if (result == ISC_R_SUCCESS && zone->journal != NULL) { |
10580 | 0 | tresult = dns_dumpctx_serial(zone->dumpctx, &serial); |
10581 | 0 | } |
10582 | |
|
10583 | 0 | if (tresult == ISC_R_SUCCESS) { |
10584 | | /* |
10585 | | * Handle lock order inversion. |
10586 | | */ |
10587 | 0 | again: |
10588 | 0 | LOCK_ZONE(zone); |
10589 | 0 | if (dns__zone_inline_raw(zone)) { |
10590 | 0 | secure = zone->secure; |
10591 | 0 | INSIST(secure != zone); |
10592 | 0 | TRYLOCK_ZONE(result, secure); |
10593 | 0 | if (result != ISC_R_SUCCESS) { |
10594 | 0 | UNLOCK_ZONE(zone); |
10595 | 0 | secure = NULL; |
10596 | 0 | isc_thread_yield(); |
10597 | 0 | goto again; |
10598 | 0 | } |
10599 | 0 | } |
10600 | | |
10601 | | /* |
10602 | | * If there is a secure version of this zone |
10603 | | * use its serial if it is less than ours. |
10604 | | */ |
10605 | 0 | if (secure != NULL) { |
10606 | 0 | uint32_t sserial; |
10607 | 0 | isc_result_t mresult; |
10608 | |
|
10609 | 0 | ZONEDB_LOCK(&secure->dblock, isc_rwlocktype_read); |
10610 | 0 | if (secure->db != NULL) { |
10611 | 0 | mresult = dns_db_getsoaserial(zone->secure->db, |
10612 | 0 | NULL, &sserial); |
10613 | 0 | if (mresult == ISC_R_SUCCESS && |
10614 | 0 | isc_serial_lt(sserial, serial)) |
10615 | 0 | { |
10616 | 0 | serial = sserial; |
10617 | 0 | } |
10618 | 0 | } |
10619 | 0 | ZONEDB_UNLOCK(&secure->dblock, isc_rwlocktype_read); |
10620 | 0 | } |
10621 | 0 | if (zone->xfr == NULL) { |
10622 | 0 | dns_db_t *zdb = NULL; |
10623 | 0 | if (dns_zone_getdb(zone, &zdb) == ISC_R_SUCCESS) { |
10624 | 0 | zone_journal_compact(zone, zdb, serial); |
10625 | 0 | dns_db_detach(&zdb); |
10626 | 0 | } |
10627 | 0 | } else { |
10628 | 0 | compact = true; |
10629 | 0 | zone->compact_serial = serial; |
10630 | 0 | } |
10631 | 0 | if (secure != NULL) { |
10632 | 0 | UNLOCK_ZONE(secure); |
10633 | 0 | } |
10634 | 0 | UNLOCK_ZONE(zone); |
10635 | 0 | } |
10636 | | |
10637 | 0 | LOCK_ZONE(zone); |
10638 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_DUMPING); |
10639 | 0 | if (compact) { |
10640 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDCOMPACT); |
10641 | 0 | } |
10642 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_SHUTDOWN)) { |
10643 | | /* |
10644 | | * If DNS_ZONEFLG_SHUTDOWN is set, all external references to |
10645 | | * the zone are gone, which means it is in the process of being |
10646 | | * cleaned up, so do not reschedule dumping. |
10647 | | * |
10648 | | * Detach from the raw version of the zone in case this |
10649 | | * operation has been deferred in zone_shutdown(). |
10650 | | */ |
10651 | 0 | if (zone->raw != NULL) { |
10652 | 0 | dns_zone_detach(&zone->raw); |
10653 | 0 | } |
10654 | 0 | if (result == ISC_R_SUCCESS) { |
10655 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FLUSH); |
10656 | 0 | } |
10657 | 0 | } else if (result != ISC_R_SUCCESS && result != ISC_R_CANCELED) { |
10658 | | /* |
10659 | | * Try again in a short while. |
10660 | | */ |
10661 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
10662 | 0 | } else if (result == ISC_R_SUCCESS && |
10663 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) && |
10664 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) && |
10665 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) |
10666 | 0 | { |
10667 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP); |
10668 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DUMPING); |
10669 | 0 | isc_time_settoepoch(&zone->dumptime); |
10670 | 0 | again = true; |
10671 | 0 | } else if (result == ISC_R_SUCCESS) { |
10672 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FLUSH); |
10673 | 0 | } |
10674 | |
|
10675 | 0 | if (zone->dumpctx != NULL) { |
10676 | 0 | dns_dumpctx_detach(&zone->dumpctx); |
10677 | 0 | } |
10678 | 0 | UNLOCK_ZONE(zone); |
10679 | 0 | if (again) { |
10680 | 0 | (void)zone_dump(zone, false); |
10681 | 0 | } |
10682 | 0 | dns_zone_idetach(&zone); |
10683 | 0 | } |
10684 | | |
10685 | | static isc_result_t |
10686 | 0 | zone_dump(dns_zone_t *zone, bool compact) { |
10687 | 0 | isc_result_t result; |
10688 | 0 | dns_dbversion_t *version = NULL; |
10689 | 0 | bool again = false; |
10690 | 0 | dns_db_t *db = NULL; |
10691 | 0 | char *masterfile = NULL; |
10692 | 0 | dns_masterformat_t masterformat = dns_masterformat_none; |
10693 | 0 | const dns_master_style_t *masterstyle = NULL; |
10694 | 0 | dns_masterrawheader_t rawdata; |
10695 | 0 | bool inline_secure; |
10696 | | |
10697 | | /* |
10698 | | * 'compact' MUST only be set if we are loop locked. |
10699 | | */ |
10700 | |
|
10701 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10702 | 0 | ENTER; |
10703 | |
|
10704 | 0 | redo: |
10705 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
10706 | 0 | if (zone->db != NULL) { |
10707 | 0 | dns_db_attach(zone->db, &db); |
10708 | 0 | } |
10709 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
10710 | 0 | LOCK_ZONE(zone); |
10711 | 0 | if (zone->masterfile != NULL) { |
10712 | 0 | masterfile = isc_mem_strdup(zone->mctx, zone->masterfile); |
10713 | 0 | masterformat = zone->masterformat; |
10714 | 0 | } |
10715 | 0 | if (zone->type == dns_zone_key) { |
10716 | 0 | masterstyle = &dns_master_style_keyzone; |
10717 | 0 | } else if (zone->masterstyle != NULL) { |
10718 | 0 | masterstyle = zone->masterstyle; |
10719 | 0 | } else { |
10720 | 0 | masterstyle = &dns_master_style_default; |
10721 | 0 | } |
10722 | 0 | UNLOCK_ZONE(zone); |
10723 | 0 | if (db == NULL) { |
10724 | 0 | result = DNS_R_NOTLOADED; |
10725 | 0 | goto fail; |
10726 | 0 | } |
10727 | 0 | if (masterfile == NULL) { |
10728 | 0 | result = DNS_R_NOMASTERFILE; |
10729 | 0 | goto fail; |
10730 | 0 | } |
10731 | | |
10732 | 0 | dns_db_currentversion(db, &version); |
10733 | |
|
10734 | 0 | dns_master_initrawheader(&rawdata); |
10735 | |
|
10736 | 0 | LOCK_ZONE(zone); |
10737 | 0 | inline_secure = dns__zone_inline_secure(zone); |
10738 | 0 | UNLOCK_ZONE(zone); |
10739 | |
|
10740 | 0 | if (inline_secure) { |
10741 | 0 | get_raw_serial(zone->raw, &rawdata); |
10742 | 0 | } |
10743 | |
|
10744 | 0 | if (compact && zone->type != dns_zone_stub) { |
10745 | 0 | LOCK_ZONE(zone); |
10746 | 0 | zone_iattach(zone, &(dns_zone_t *){ NULL }); |
10747 | |
|
10748 | 0 | INSIST(zone != zone->raw); |
10749 | |
|
10750 | 0 | result = dns_master_dumpasync( |
10751 | 0 | zone->mctx, db, version, masterstyle, masterfile, |
10752 | 0 | zone->loop, dump_done, zone, &zone->dumpctx, |
10753 | 0 | masterformat, &rawdata); |
10754 | |
|
10755 | 0 | UNLOCK_ZONE(zone); |
10756 | 0 | if (result != ISC_R_SUCCESS) { |
10757 | 0 | dns_zone_idetach(&(dns_zone_t *){ zone }); |
10758 | 0 | goto fail; |
10759 | 0 | } |
10760 | 0 | result = DNS_R_CONTINUE; |
10761 | 0 | } else { |
10762 | 0 | result = dns_master_dump(zone->mctx, db, version, masterstyle, |
10763 | 0 | masterfile, masterformat, &rawdata); |
10764 | 0 | if ((zone->type == dns_zone_secondary || |
10765 | 0 | zone->type == dns_zone_mirror || |
10766 | 0 | zone->type == dns_zone_redirect) && |
10767 | 0 | result == ISC_R_SUCCESS) |
10768 | 0 | { |
10769 | 0 | isc_time_t when; |
10770 | 0 | isc_interval_t i; |
10771 | 0 | isc_interval_set(&i, zone->expire, 0); |
10772 | 0 | result = isc_time_subtract(&zone->expiretime, &i, |
10773 | 0 | &when); |
10774 | 0 | if (result == ISC_R_SUCCESS) { |
10775 | 0 | (void)isc_file_settime(zone->masterfile, &when); |
10776 | 0 | } else { |
10777 | 0 | result = ISC_R_SUCCESS; |
10778 | 0 | } |
10779 | 0 | } |
10780 | 0 | } |
10781 | 0 | fail: |
10782 | 0 | if (version != NULL) { |
10783 | 0 | dns_db_closeversion(db, &version, false); |
10784 | 0 | } |
10785 | 0 | if (db != NULL) { |
10786 | 0 | dns_db_detach(&db); |
10787 | 0 | } |
10788 | 0 | if (masterfile != NULL) { |
10789 | 0 | isc_mem_free(zone->mctx, masterfile); |
10790 | 0 | } |
10791 | |
|
10792 | 0 | if (result == DNS_R_CONTINUE) { |
10793 | | /* |
10794 | | * Asyncronous write is in progress. Zone flags will get |
10795 | | * updated on completion. Cleanup is complete. We are done. |
10796 | | */ |
10797 | 0 | return ISC_R_SUCCESS; |
10798 | 0 | } |
10799 | | |
10800 | 0 | again = false; |
10801 | 0 | LOCK_ZONE(zone); |
10802 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_DUMPING); |
10803 | 0 | if (result != ISC_R_SUCCESS) { |
10804 | | /* |
10805 | | * Try again in a short while. |
10806 | | */ |
10807 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
10808 | 0 | } else if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) && |
10809 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) && |
10810 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) |
10811 | 0 | { |
10812 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP); |
10813 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_DUMPING); |
10814 | 0 | isc_time_settoepoch(&zone->dumptime); |
10815 | 0 | again = true; |
10816 | 0 | } else { |
10817 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FLUSH); |
10818 | 0 | } |
10819 | 0 | UNLOCK_ZONE(zone); |
10820 | 0 | if (again) { |
10821 | 0 | goto redo; |
10822 | 0 | } |
10823 | | |
10824 | 0 | return result; |
10825 | 0 | } |
10826 | | |
10827 | | static isc_result_t |
10828 | | dumptostream(dns_zone_t *zone, FILE *fd, const dns_master_style_t *style, |
10829 | 0 | dns_masterformat_t format, const uint32_t rawversion) { |
10830 | 0 | isc_result_t result; |
10831 | 0 | dns_dbversion_t *version = NULL; |
10832 | 0 | dns_db_t *db = NULL; |
10833 | 0 | dns_masterrawheader_t rawdata; |
10834 | 0 | bool inline_secure; |
10835 | |
|
10836 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10837 | |
|
10838 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
10839 | 0 | if (zone->db != NULL) { |
10840 | 0 | dns_db_attach(zone->db, &db); |
10841 | 0 | } |
10842 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
10843 | 0 | if (db == NULL) { |
10844 | 0 | return DNS_R_NOTLOADED; |
10845 | 0 | } |
10846 | | |
10847 | 0 | LOCK_ZONE(zone); |
10848 | 0 | inline_secure = dns__zone_inline_secure(zone); |
10849 | 0 | UNLOCK_ZONE(zone); |
10850 | |
|
10851 | 0 | dns_db_currentversion(db, &version); |
10852 | 0 | dns_master_initrawheader(&rawdata); |
10853 | 0 | if (rawversion == 0) { |
10854 | 0 | rawdata.flags |= DNS_MASTERRAW_COMPAT; |
10855 | 0 | } else if (inline_secure) { |
10856 | 0 | get_raw_serial(zone->raw, &rawdata); |
10857 | 0 | } else if (zone->sourceserialset) { |
10858 | 0 | rawdata.flags = DNS_MASTERRAW_SOURCESERIALSET; |
10859 | 0 | rawdata.sourceserial = zone->sourceserial; |
10860 | 0 | } |
10861 | 0 | result = dns_master_dumptostream(zone->mctx, db, version, style, format, |
10862 | 0 | &rawdata, fd); |
10863 | 0 | dns_db_closeversion(db, &version, false); |
10864 | 0 | dns_db_detach(&db); |
10865 | 0 | return result; |
10866 | 0 | } |
10867 | | |
10868 | | isc_result_t |
10869 | | dns_zone_dumptostream(dns_zone_t *zone, FILE *fd, dns_masterformat_t format, |
10870 | | const dns_master_style_t *style, |
10871 | 0 | const uint32_t rawversion) { |
10872 | 0 | return dumptostream(zone, fd, style, format, rawversion); |
10873 | 0 | } |
10874 | | |
10875 | | void |
10876 | 0 | dns_zone_unload(dns_zone_t *zone) { |
10877 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10878 | |
|
10879 | 0 | LOCK_ZONE(zone); |
10880 | 0 | zone_unload(zone); |
10881 | 0 | UNLOCK_ZONE(zone); |
10882 | 0 | } |
10883 | | |
10884 | | static void |
10885 | 0 | checkds_cancel(dns_zone_t *zone) { |
10886 | | /* |
10887 | | * 'zone' locked by caller. |
10888 | | */ |
10889 | |
|
10890 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
10891 | |
|
10892 | 0 | ISC_LIST_FOREACH(zone->checkds_requests, checkds, link) { |
10893 | 0 | if (checkds->find != NULL) { |
10894 | 0 | dns_adb_cancelfind(checkds->find); |
10895 | 0 | } |
10896 | 0 | if (checkds->request != NULL) { |
10897 | 0 | dns_request_cancel(checkds->request); |
10898 | 0 | } |
10899 | 0 | } |
10900 | 0 | } |
10901 | | |
10902 | | void |
10903 | 0 | dns__zone_forward_cancel(dns_zone_t *zone) { |
10904 | | /* |
10905 | | * 'zone' locked by caller. |
10906 | | */ |
10907 | |
|
10908 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
10909 | |
|
10910 | 0 | ISC_LIST_FOREACH(zone->forwards, forward, link) { |
10911 | 0 | if (forward->request != NULL) { |
10912 | 0 | dns_request_cancel(forward->request); |
10913 | 0 | } |
10914 | 0 | } |
10915 | 0 | } |
10916 | | |
10917 | | static void |
10918 | 0 | zone_unload(dns_zone_t *zone) { |
10919 | | /* |
10920 | | * 'zone' locked by caller. |
10921 | | */ |
10922 | |
|
10923 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
10924 | |
|
10925 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) || |
10926 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) |
10927 | 0 | { |
10928 | 0 | if (zone->dumpctx != NULL) { |
10929 | 0 | dns_dumpctx_cancel(zone->dumpctx); |
10930 | 0 | } |
10931 | 0 | } |
10932 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write); |
10933 | 0 | zone_detachdb(zone); |
10934 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write); |
10935 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADED); |
10936 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDDUMP); |
10937 | |
|
10938 | 0 | if (zone->type == dns_zone_mirror) { |
10939 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
10940 | 0 | "mirror zone is no longer in use; " |
10941 | 0 | "reverting to normal recursion"); |
10942 | 0 | } |
10943 | 0 | } |
10944 | | |
10945 | | void |
10946 | 0 | dns_zone_notify(dns_zone_t *zone, bool nodefer) { |
10947 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10948 | |
|
10949 | 0 | LOCK_ZONE(zone); |
10950 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY); |
10951 | 0 | if (nodefer) { |
10952 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOTIFYDEFERRED)) { |
10953 | | /* |
10954 | | * We have previously deferred the notify, but we have a |
10955 | | * new request not to defer it. Reverse the deferring |
10956 | | * operation. |
10957 | | */ |
10958 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOTIFYDEFERRED); |
10959 | 0 | DNS_ZONE_TIME_SUBTRACT(&zone->notifytime, |
10960 | 0 | zone->notifysoa.notifydefer, |
10961 | 0 | &zone->notifytime); |
10962 | 0 | } |
10963 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOTIFYNODEFER); |
10964 | 0 | } |
10965 | 0 | dns__zone_settimer(zone, isc_time_now()); |
10966 | 0 | UNLOCK_ZONE(zone); |
10967 | 0 | } |
10968 | | |
10969 | | static void |
10970 | 0 | zone_notify(dns_zone_t *zone, isc_time_t *now) { |
10971 | 0 | dns_dbnode_t *node = NULL; |
10972 | 0 | dns_db_t *zonedb = NULL; |
10973 | 0 | dns_dbversion_t *version = NULL; |
10974 | 0 | dns_name_t *origin = NULL; |
10975 | 0 | dns_name_t primary; |
10976 | 0 | dns_rdata_ns_t ns; |
10977 | 0 | dns_rdata_soa_t soa; |
10978 | 0 | dns_rdata_t soardata = DNS_RDATA_INIT; |
10979 | 0 | uint32_t serial; |
10980 | 0 | dns_rdataset_t nsrdset; |
10981 | 0 | dns_rdataset_t soardset; |
10982 | 0 | isc_result_t result; |
10983 | 0 | isc_sockaddr_t src; |
10984 | 0 | isc_sockaddr_t dst; |
10985 | 0 | bool isqueued; |
10986 | 0 | dns_notifytype_t notifytype; |
10987 | 0 | unsigned int flags = 0; |
10988 | 0 | bool loggednotify = false; |
10989 | 0 | bool startup; |
10990 | |
|
10991 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
10992 | |
|
10993 | 0 | LOCK_ZONE(zone); |
10994 | 0 | startup = !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY); |
10995 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY | |
10996 | 0 | DNS_ZONEFLG_NEEDSTARTUPNOTIFY | |
10997 | 0 | DNS_ZONEFLG_NOTIFYNODEFER | |
10998 | 0 | DNS_ZONEFLG_NOTIFYDEFERRED); |
10999 | 0 | notifytype = zone->notifysoa.notifytype; |
11000 | 0 | DNS_ZONE_TIME_ADD(now, zone->notifysoa.notifydelay, &zone->notifytime); |
11001 | 0 | UNLOCK_ZONE(zone); |
11002 | |
|
11003 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING) || |
11004 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) |
11005 | 0 | { |
11006 | 0 | return; |
11007 | 0 | } |
11008 | | |
11009 | 0 | if (notifytype == dns_notifytype_no) { |
11010 | 0 | return; |
11011 | 0 | } |
11012 | | |
11013 | 0 | if (notifytype == dns_notifytype_masteronly && |
11014 | 0 | zone->type != dns_zone_primary) |
11015 | 0 | { |
11016 | 0 | return; |
11017 | 0 | } |
11018 | | |
11019 | 0 | origin = &zone->origin; |
11020 | | |
11021 | | /* |
11022 | | * Record that this was a notify due to starting up. |
11023 | | */ |
11024 | 0 | if (startup) { |
11025 | 0 | flags |= DNS_NOTIFY_STARTUP; |
11026 | 0 | } |
11027 | | |
11028 | | /* |
11029 | | * Get SOA RRset. |
11030 | | */ |
11031 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
11032 | 0 | if (zone->db != NULL) { |
11033 | 0 | dns_db_attach(zone->db, &zonedb); |
11034 | 0 | } |
11035 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
11036 | 0 | if (zonedb == NULL) { |
11037 | 0 | return; |
11038 | 0 | } |
11039 | 0 | dns_db_currentversion(zonedb, &version); |
11040 | 0 | result = dns_db_findnode(zonedb, origin, false, &node); |
11041 | 0 | if (result != ISC_R_SUCCESS) { |
11042 | 0 | goto cleanup1; |
11043 | 0 | } |
11044 | | |
11045 | 0 | dns_rdataset_init(&soardset); |
11046 | 0 | result = dns_db_findrdataset(zonedb, node, version, dns_rdatatype_soa, |
11047 | 0 | dns_rdatatype_none, 0, &soardset, NULL); |
11048 | 0 | if (result != ISC_R_SUCCESS) { |
11049 | 0 | goto cleanup2; |
11050 | 0 | } |
11051 | | |
11052 | | /* |
11053 | | * Find serial and primary server's name. |
11054 | | */ |
11055 | 0 | dns_name_init(&primary); |
11056 | 0 | result = dns_rdataset_first(&soardset); |
11057 | 0 | if (result != ISC_R_SUCCESS) { |
11058 | 0 | goto cleanup3; |
11059 | 0 | } |
11060 | 0 | dns_rdataset_current(&soardset, &soardata); |
11061 | 0 | result = dns_rdata_tostruct(&soardata, &soa, NULL); |
11062 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
11063 | 0 | dns_name_dup(&soa.origin, zone->mctx, &primary); |
11064 | 0 | serial = soa.serial; |
11065 | 0 | dns_rdataset_disassociate(&soardset); |
11066 | | |
11067 | | /* |
11068 | | * Enqueue notify requests for 'also-notify' servers. |
11069 | | */ |
11070 | 0 | LOCK_ZONE(zone); |
11071 | |
|
11072 | 0 | dns_remote_reset(&zone->alsonotify, false); |
11073 | 0 | while (!dns_remote_done(&zone->alsonotify)) { |
11074 | 0 | dns_tsigkey_t *key = NULL; |
11075 | 0 | dns_transport_t *transport = NULL; |
11076 | 0 | dns_notify_t *notify = NULL; |
11077 | 0 | dns_view_t *view = dns_zone_getview(zone); |
11078 | |
|
11079 | 0 | if (dns_remote_keyname(&zone->alsonotify) != NULL) { |
11080 | 0 | dns_name_t *keyname = |
11081 | 0 | dns_remote_keyname(&zone->alsonotify); |
11082 | 0 | (void)dns_view_gettsig(view, keyname, &key); |
11083 | 0 | } |
11084 | |
|
11085 | 0 | if (dns_remote_tlsname(&zone->alsonotify) != NULL) { |
11086 | 0 | dns_name_t *tlsname = |
11087 | 0 | dns_remote_tlsname(&zone->alsonotify); |
11088 | 0 | result = dns_view_gettransport(view, DNS_TRANSPORT_TLS, |
11089 | 0 | tlsname, &transport); |
11090 | |
|
11091 | 0 | if (result == ISC_R_SUCCESS) { |
11092 | 0 | dns_zone_logc( |
11093 | 0 | zone, DNS_LOGCATEGORY_NOTIFY, |
11094 | 0 | ISC_LOG_INFO, |
11095 | 0 | "got TLS configuration for a notify"); |
11096 | 0 | } else { |
11097 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
11098 | 0 | ISC_LOG_ERROR, |
11099 | 0 | "could not get TLS configuration " |
11100 | 0 | "for zone transfer: %s", |
11101 | 0 | isc_result_totext(result)); |
11102 | 0 | if (key != NULL) { |
11103 | 0 | dns_tsigkey_detach(&key); |
11104 | 0 | } |
11105 | 0 | goto next; |
11106 | 0 | } |
11107 | | |
11108 | 0 | flags |= DNS_NOTIFY_TCP; |
11109 | 0 | } |
11110 | | |
11111 | | /* TODO: glue the transport to the notify */ |
11112 | | |
11113 | 0 | dst = dns_remote_curraddr(&zone->alsonotify); |
11114 | 0 | src = dns_remote_sourceaddr(&zone->alsonotify); |
11115 | 0 | INSIST(isc_sockaddr_pf(&src) == isc_sockaddr_pf(&dst)); |
11116 | |
|
11117 | 0 | if (isc_sockaddr_disabled(&dst)) { |
11118 | 0 | if (key != NULL) { |
11119 | 0 | dns_tsigkey_detach(&key); |
11120 | 0 | } |
11121 | 0 | if (transport != NULL) { |
11122 | 0 | dns_transport_detach(&transport); |
11123 | 0 | } |
11124 | 0 | goto next; |
11125 | 0 | } |
11126 | | |
11127 | 0 | if (dns_notify_isqueued(&zone->notifysoa, dns_rdatatype_soa, |
11128 | 0 | zone->view->dstport, flags, NULL, &dst, |
11129 | 0 | key, transport)) |
11130 | 0 | { |
11131 | 0 | if (key != NULL) { |
11132 | 0 | dns_tsigkey_detach(&key); |
11133 | 0 | } |
11134 | 0 | if (transport != NULL) { |
11135 | 0 | dns_transport_detach(&transport); |
11136 | 0 | } |
11137 | 0 | goto next; |
11138 | 0 | } |
11139 | | |
11140 | 0 | dns_notify_create(zone->mctx, dns_rdatatype_soa, |
11141 | 0 | zone->view->dstport, flags, ¬ify); |
11142 | 0 | zone_iattach(zone, ¬ify->zone); |
11143 | 0 | notify->src = src; |
11144 | 0 | notify->dst = dst; |
11145 | |
|
11146 | 0 | INSIST(notify->key == NULL); |
11147 | |
|
11148 | 0 | if (key != NULL) { |
11149 | 0 | notify->key = key; |
11150 | 0 | key = NULL; |
11151 | 0 | } |
11152 | |
|
11153 | 0 | INSIST(notify->transport == NULL); |
11154 | 0 | if (transport != NULL) { |
11155 | 0 | notify->transport = transport; |
11156 | 0 | transport = NULL; |
11157 | 0 | } |
11158 | |
|
11159 | 0 | ISC_LIST_APPEND(zone->notifysoa.notifies, notify, link); |
11160 | 0 | result = dns_notify_queue(notify, startup); |
11161 | 0 | if (result != ISC_R_SUCCESS) { |
11162 | 0 | dns_notify_destroy(notify, true); |
11163 | 0 | } |
11164 | 0 | if (!loggednotify) { |
11165 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_NOTIFY, |
11166 | 0 | ISC_LOG_INFO, |
11167 | 0 | "sending notifies (serial %u)", serial); |
11168 | 0 | loggednotify = true; |
11169 | 0 | } |
11170 | 0 | next: |
11171 | 0 | flags &= ~DNS_NOTIFY_TCP; |
11172 | 0 | dns_remote_next(&zone->alsonotify, false); |
11173 | 0 | } |
11174 | 0 | UNLOCK_ZONE(zone); |
11175 | |
|
11176 | 0 | if (notifytype == dns_notifytype_explicit) { |
11177 | 0 | goto cleanup3; |
11178 | 0 | } |
11179 | | |
11180 | | /* |
11181 | | * Process NS RRset to generate notifies. |
11182 | | */ |
11183 | | |
11184 | 0 | dns_rdataset_init(&nsrdset); |
11185 | 0 | result = dns_db_findrdataset(zonedb, node, version, dns_rdatatype_ns, |
11186 | 0 | dns_rdatatype_none, 0, &nsrdset, NULL); |
11187 | 0 | if (result != ISC_R_SUCCESS) { |
11188 | 0 | goto cleanup3; |
11189 | 0 | } |
11190 | | |
11191 | 0 | DNS_RDATASET_FOREACH(&nsrdset) { |
11192 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
11193 | 0 | dns_rdataset_current(&nsrdset, &rdata); |
11194 | |
|
11195 | 0 | dns_notify_t *notify = NULL; |
11196 | |
|
11197 | 0 | result = dns_rdata_tostruct(&rdata, &ns, NULL); |
11198 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
11199 | | |
11200 | | /* |
11201 | | * Don't notify the primary server unless explicitly |
11202 | | * configured to do so. |
11203 | | */ |
11204 | 0 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_NOTIFYTOSOA) && |
11205 | 0 | dns_name_compare(&primary, &ns.name) == 0) |
11206 | 0 | { |
11207 | 0 | continue; |
11208 | 0 | } |
11209 | | |
11210 | 0 | if (!loggednotify) { |
11211 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_NOTIFY, |
11212 | 0 | ISC_LOG_INFO, |
11213 | 0 | "sending notifies (serial %u)", serial); |
11214 | 0 | loggednotify = true; |
11215 | 0 | } |
11216 | |
|
11217 | 0 | LOCK_ZONE(zone); |
11218 | 0 | isqueued = dns_notify_isqueued( |
11219 | 0 | &zone->notifysoa, dns_rdatatype_soa, |
11220 | 0 | zone->view->dstport, flags, &ns.name, NULL, NULL, NULL); |
11221 | 0 | UNLOCK_ZONE(zone); |
11222 | 0 | if (isqueued) { |
11223 | 0 | continue; |
11224 | 0 | } |
11225 | 0 | dns_notify_create(zone->mctx, dns_rdatatype_soa, |
11226 | 0 | zone->view->dstport, flags, ¬ify); |
11227 | 0 | dns_zone_iattach(zone, ¬ify->zone); |
11228 | 0 | dns_name_dup(&ns.name, zone->mctx, ¬ify->ns); |
11229 | 0 | LOCK_ZONE(zone); |
11230 | 0 | ISC_LIST_APPEND(zone->notifysoa.notifies, notify, link); |
11231 | 0 | UNLOCK_ZONE(zone); |
11232 | 0 | dns_notify_find_address(notify); |
11233 | 0 | } |
11234 | 0 | dns_rdataset_disassociate(&nsrdset); |
11235 | |
|
11236 | 0 | cleanup3: |
11237 | 0 | if (dns_name_dynamic(&primary)) { |
11238 | 0 | dns_name_free(&primary, zone->mctx); |
11239 | 0 | } |
11240 | 0 | cleanup2: |
11241 | 0 | dns_db_detachnode(&node); |
11242 | 0 | cleanup1: |
11243 | 0 | dns_db_closeversion(zonedb, &version, false); |
11244 | 0 | dns_db_detach(&zonedb); |
11245 | 0 | } |
11246 | | |
11247 | | /*** |
11248 | | *** Private |
11249 | | ***/ |
11250 | | static void |
11251 | | create_query(dns_zone_t *zone, dns_rdatatype_t rdtype, dns_name_t *name, |
11252 | 0 | dns_message_t **messagep) { |
11253 | 0 | dns_message_t *message = NULL; |
11254 | 0 | dns_name_t *qname = NULL; |
11255 | 0 | dns_rdataset_t *qrdataset = NULL; |
11256 | |
|
11257 | 0 | dns_message_create(zone->mctx, NULL, NULL, DNS_MESSAGE_INTENTRENDER, |
11258 | 0 | &message); |
11259 | |
|
11260 | 0 | message->opcode = dns_opcode_query; |
11261 | 0 | message->rdclass = zone->rdclass; |
11262 | |
|
11263 | 0 | dns_message_gettempname(message, &qname); |
11264 | |
|
11265 | 0 | dns_message_gettemprdataset(message, &qrdataset); |
11266 | | |
11267 | | /* |
11268 | | * Make question. |
11269 | | */ |
11270 | 0 | dns_name_clone(name, qname); |
11271 | 0 | dns_rdataset_makequestion(qrdataset, zone->rdclass, rdtype); |
11272 | 0 | ISC_LIST_APPEND(qname->list, qrdataset, link); |
11273 | 0 | dns_message_addname(message, qname, DNS_SECTION_QUESTION); |
11274 | |
|
11275 | 0 | *messagep = message; |
11276 | 0 | } |
11277 | | |
11278 | | static isc_result_t |
11279 | | add_opt(dns_message_t *message, uint16_t udpsize, bool reqnsid, |
11280 | 0 | bool reqexpire) { |
11281 | 0 | dns_message_ednsinit(message, 0, udpsize, 0, 0); |
11282 | | |
11283 | | /* Set EDNS options if applicable. */ |
11284 | 0 | if (reqnsid) { |
11285 | 0 | dns_ednsopt_t option = { .code = DNS_OPT_NSID }; |
11286 | 0 | RETERR(dns_message_ednsaddopt(message, &option)); |
11287 | 0 | } |
11288 | 0 | if (reqexpire) { |
11289 | 0 | dns_ednsopt_t option = { .code = DNS_OPT_EXPIRE }; |
11290 | 0 | RETERR(dns_message_ednsaddopt(message, &option)); |
11291 | 0 | } |
11292 | | |
11293 | 0 | return dns_message_setopt(message); |
11294 | 0 | } |
11295 | | |
11296 | | /* |
11297 | | * Called when stub zone update is finished. |
11298 | | * Update zone refresh, retry, expire values accordingly with |
11299 | | * SOA received from primary, sync database to file, restart |
11300 | | * zone management timer. |
11301 | | */ |
11302 | | static void |
11303 | 0 | stub_finish_zone_update(dns_stub_t *stub, isc_time_t now) { |
11304 | 0 | uint32_t refresh, retry, expire; |
11305 | 0 | isc_result_t result; |
11306 | 0 | isc_interval_t i; |
11307 | 0 | unsigned int soacount; |
11308 | 0 | dns_zone_t *zone = stub->zone; |
11309 | | |
11310 | | /* |
11311 | | * Tidy up. |
11312 | | */ |
11313 | 0 | dns_db_closeversion(stub->db, &stub->version, true); |
11314 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write); |
11315 | 0 | if (zone->db == NULL) { |
11316 | 0 | zone_attachdb(zone, stub->db); |
11317 | 0 | } |
11318 | 0 | result = zone_get_from_db(zone, zone->db, NULL, &soacount, NULL, NULL, |
11319 | 0 | &refresh, &retry, &expire, NULL, NULL); |
11320 | 0 | if (result == ISC_R_SUCCESS && soacount > 0U) { |
11321 | 0 | zone->refresh = RANGE(refresh, zone->minrefresh, |
11322 | 0 | zone->maxrefresh); |
11323 | 0 | zone->retry = RANGE(retry, zone->minretry, zone->maxretry); |
11324 | 0 | zone->expire = RANGE(expire, zone->refresh + zone->retry, |
11325 | 0 | DNS_MAX_EXPIRE); |
11326 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HAVETIMERS); |
11327 | 0 | } |
11328 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write); |
11329 | 0 | dns_db_detach(&stub->db); |
11330 | |
|
11331 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH); |
11332 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED); |
11333 | 0 | DNS_ZONE_JITTER_ADD(&now, zone->refresh, &zone->refreshtime); |
11334 | 0 | isc_interval_set(&i, zone->expire, 0); |
11335 | 0 | DNS_ZONE_TIME_ADD(&now, zone->expire, &zone->expiretime); |
11336 | |
|
11337 | 0 | if (zone->masterfile != NULL) { |
11338 | 0 | zone_needdump(zone, 0); |
11339 | 0 | } |
11340 | |
|
11341 | 0 | dns__zone_settimer(zone, now); |
11342 | 0 | } |
11343 | | |
11344 | | /* |
11345 | | * Process answers for A and AAAA queries when |
11346 | | * resolving nameserver addresses for which glue |
11347 | | * was missing in a previous answer for a NS query. |
11348 | | */ |
11349 | | static void |
11350 | 0 | stub_glue_response(void *arg) { |
11351 | 0 | dns_request_t *request = (dns_request_t *)arg; |
11352 | 0 | struct stub_glue_request *sgr = dns_request_getarg(request); |
11353 | 0 | struct stub_cb_args *cb_args = sgr->args; |
11354 | 0 | dns_stub_t *stub = cb_args->stub; |
11355 | 0 | dns_message_t *msg = NULL; |
11356 | 0 | dns_zone_t *zone = NULL; |
11357 | 0 | char primary[ISC_SOCKADDR_FORMATSIZE]; |
11358 | 0 | char source[ISC_SOCKADDR_FORMATSIZE]; |
11359 | 0 | uint32_t addr_count, cnamecnt; |
11360 | 0 | isc_result_t result; |
11361 | 0 | isc_sockaddr_t curraddr; |
11362 | 0 | dns_rdataset_t *addr_rdataset = NULL; |
11363 | 0 | dns_dbnode_t *node = NULL; |
11364 | |
|
11365 | 0 | INSIST(DNS_STUB_VALID(stub)); |
11366 | |
|
11367 | 0 | zone = stub->zone; |
11368 | |
|
11369 | 0 | ENTER; |
11370 | |
|
11371 | 0 | LOCK_ZONE(zone); |
11372 | |
|
11373 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
11374 | 0 | zone_debuglog(zone, __func__, 1, "exiting"); |
11375 | 0 | goto cleanup; |
11376 | 0 | } |
11377 | | |
11378 | 0 | curraddr = dns_remote_curraddr(&zone->primaries); |
11379 | 0 | isc_sockaddr_format(&curraddr, primary, sizeof(primary)); |
11380 | 0 | isc_sockaddr_format(&zone->sourceaddr, source, sizeof(source)); |
11381 | |
|
11382 | 0 | if (dns_request_getresult(request) != ISC_R_SUCCESS) { |
11383 | 0 | dns_unreachcache_add(zone->view->unreachcache, &curraddr, |
11384 | 0 | &zone->sourceaddr); |
11385 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11386 | 0 | "could not refresh stub from primary %s" |
11387 | 0 | " (source %s): %s", |
11388 | 0 | primary, source, |
11389 | 0 | isc_result_totext(dns_request_getresult(request))); |
11390 | 0 | goto cleanup; |
11391 | 0 | } |
11392 | | |
11393 | 0 | dns_message_create(zone->mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, |
11394 | 0 | &msg); |
11395 | 0 | result = dns_request_getresponse(request, msg, 0); |
11396 | 0 | if (result != ISC_R_SUCCESS) { |
11397 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11398 | 0 | "refreshing stub: unable to parse response (%s)", |
11399 | 0 | isc_result_totext(result)); |
11400 | 0 | goto cleanup; |
11401 | 0 | } |
11402 | | |
11403 | | /* |
11404 | | * Unexpected opcode. |
11405 | | */ |
11406 | 0 | if (msg->opcode != dns_opcode_query) { |
11407 | 0 | char opcode[128]; |
11408 | 0 | isc_buffer_t rb; |
11409 | |
|
11410 | 0 | isc_buffer_init(&rb, opcode, sizeof(opcode)); |
11411 | 0 | (void)dns_opcode_totext(msg->opcode, &rb); |
11412 | |
|
11413 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11414 | 0 | "refreshing stub: " |
11415 | 0 | "unexpected opcode (%.*s) from %s (source %s)", |
11416 | 0 | (int)rb.used, opcode, primary, source); |
11417 | 0 | goto cleanup; |
11418 | 0 | } |
11419 | | |
11420 | | /* |
11421 | | * Unexpected rcode. |
11422 | | */ |
11423 | 0 | if (msg->rcode != dns_rcode_noerror) { |
11424 | 0 | char rcode[128]; |
11425 | 0 | isc_buffer_t rb; |
11426 | |
|
11427 | 0 | isc_buffer_init(&rb, rcode, sizeof(rcode)); |
11428 | 0 | (void)dns_rcode_totext(msg->rcode, &rb); |
11429 | |
|
11430 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11431 | 0 | "refreshing stub: " |
11432 | 0 | "unexpected rcode (%.*s) from %s (source %s)", |
11433 | 0 | (int)rb.used, rcode, primary, source); |
11434 | 0 | goto cleanup; |
11435 | 0 | } |
11436 | | |
11437 | | /* |
11438 | | * We need complete messages. |
11439 | | */ |
11440 | 0 | if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) { |
11441 | 0 | if (dns_request_usedtcp(request)) { |
11442 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11443 | 0 | "refreshing stub: truncated TCP " |
11444 | 0 | "response from primary %s (source %s)", |
11445 | 0 | primary, source); |
11446 | 0 | } |
11447 | 0 | goto cleanup; |
11448 | 0 | } |
11449 | | |
11450 | | /* |
11451 | | * If non-auth log. |
11452 | | */ |
11453 | 0 | if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0) { |
11454 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11455 | 0 | "refreshing stub: " |
11456 | 0 | "non-authoritative answer from " |
11457 | 0 | "primary %s (source %s)", |
11458 | 0 | primary, source); |
11459 | 0 | goto cleanup; |
11460 | 0 | } |
11461 | | |
11462 | | /* |
11463 | | * Sanity checks. |
11464 | | */ |
11465 | 0 | cnamecnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_cname); |
11466 | 0 | addr_count = message_count(msg, DNS_SECTION_ANSWER, |
11467 | 0 | sgr->ipv4 ? dns_rdatatype_a |
11468 | 0 | : dns_rdatatype_aaaa); |
11469 | |
|
11470 | 0 | if (cnamecnt != 0) { |
11471 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11472 | 0 | "refreshing stub: unexpected CNAME response " |
11473 | 0 | "from primary %s (source %s)", |
11474 | 0 | primary, source); |
11475 | 0 | goto cleanup; |
11476 | 0 | } |
11477 | | |
11478 | 0 | if (addr_count == 0) { |
11479 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11480 | 0 | "refreshing stub: no %s records in response " |
11481 | 0 | "from primary %s (source %s)", |
11482 | 0 | sgr->ipv4 ? "A" : "AAAA", primary, source); |
11483 | 0 | goto cleanup; |
11484 | 0 | } |
11485 | | /* |
11486 | | * Extract A or AAAA RRset from message. |
11487 | | */ |
11488 | 0 | result = dns_message_findname(msg, DNS_SECTION_ANSWER, &sgr->name, |
11489 | 0 | sgr->ipv4 ? dns_rdatatype_a |
11490 | 0 | : dns_rdatatype_aaaa, |
11491 | 0 | dns_rdatatype_none, NULL, &addr_rdataset); |
11492 | 0 | if (result != ISC_R_SUCCESS) { |
11493 | 0 | if (result != DNS_R_NXDOMAIN && result != DNS_R_NXRRSET) { |
11494 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
11495 | 0 | dns_name_format(&sgr->name, namebuf, sizeof(namebuf)); |
11496 | 0 | dns_zone_log( |
11497 | 0 | zone, ISC_LOG_INFO, |
11498 | 0 | "refreshing stub: dns_message_findname(%s/%s) " |
11499 | 0 | "failed (%s)", |
11500 | 0 | namebuf, sgr->ipv4 ? "A" : "AAAA", |
11501 | 0 | isc_result_totext(result)); |
11502 | 0 | } |
11503 | 0 | goto cleanup; |
11504 | 0 | } |
11505 | | |
11506 | 0 | result = dns_db_findnode(stub->db, &sgr->name, true, &node); |
11507 | 0 | if (result != ISC_R_SUCCESS) { |
11508 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11509 | 0 | "refreshing stub: " |
11510 | 0 | "dns_db_findnode() failed: %s", |
11511 | 0 | isc_result_totext(result)); |
11512 | 0 | goto cleanup; |
11513 | 0 | } |
11514 | | |
11515 | 0 | result = dns_db_addrdataset(stub->db, node, stub->version, 0, |
11516 | 0 | addr_rdataset, 0, NULL); |
11517 | 0 | if (result != ISC_R_SUCCESS) { |
11518 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11519 | 0 | "refreshing stub: " |
11520 | 0 | "dns_db_addrdataset() failed: %s", |
11521 | 0 | isc_result_totext(result)); |
11522 | 0 | } |
11523 | 0 | dns_db_detachnode(&node); |
11524 | |
|
11525 | 0 | cleanup: |
11526 | 0 | if (msg != NULL) { |
11527 | 0 | dns_message_detach(&msg); |
11528 | 0 | } |
11529 | |
|
11530 | 0 | dns_name_free(&sgr->name, zone->mctx); |
11531 | 0 | dns_request_destroy(&sgr->request); |
11532 | 0 | isc_mem_put(zone->mctx, sgr, sizeof(*sgr)); |
11533 | | |
11534 | | /* If last request, release all related resources */ |
11535 | 0 | if (atomic_fetch_sub_release(&stub->pending_requests, 1) == 1) { |
11536 | 0 | isc_mem_put(zone->mctx, cb_args, sizeof(*cb_args)); |
11537 | 0 | stub_finish_zone_update(stub, isc_time_now()); |
11538 | 0 | UNLOCK_ZONE(zone); |
11539 | 0 | stub->magic = 0; |
11540 | 0 | dns_zone_idetach(&stub->zone); |
11541 | 0 | INSIST(stub->db == NULL); |
11542 | 0 | INSIST(stub->version == NULL); |
11543 | 0 | isc_mem_put(stub->mctx, stub, sizeof(*stub)); |
11544 | 0 | } else { |
11545 | 0 | UNLOCK_ZONE(zone); |
11546 | 0 | } |
11547 | 0 | } |
11548 | | |
11549 | | /* |
11550 | | * Create and send an A or AAAA query to the primary |
11551 | | * server of the stub zone given. |
11552 | | */ |
11553 | | static isc_result_t |
11554 | | stub_request_nameserver_address(struct stub_cb_args *args, bool ipv4, |
11555 | 0 | const dns_name_t *name) { |
11556 | 0 | dns_message_t *message = NULL; |
11557 | 0 | dns_zone_t *zone; |
11558 | 0 | isc_result_t result; |
11559 | 0 | struct stub_glue_request *sgr; |
11560 | 0 | isc_sockaddr_t curraddr; |
11561 | |
|
11562 | 0 | zone = args->stub->zone; |
11563 | 0 | sgr = isc_mem_get(zone->mctx, sizeof(*sgr)); |
11564 | 0 | *sgr = (struct stub_glue_request){ |
11565 | 0 | .args = args, |
11566 | 0 | .name = (dns_name_t)DNS_NAME_INITEMPTY, |
11567 | 0 | .ipv4 = ipv4, |
11568 | 0 | }; |
11569 | |
|
11570 | 0 | dns_name_dup(name, zone->mctx, &sgr->name); |
11571 | |
|
11572 | 0 | create_query(zone, ipv4 ? dns_rdatatype_a : dns_rdatatype_aaaa, |
11573 | 0 | &sgr->name, &message); |
11574 | |
|
11575 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) { |
11576 | 0 | result = add_opt(message, args->udpsize, args->reqnsid, false); |
11577 | 0 | if (result != ISC_R_SUCCESS) { |
11578 | 0 | zone_debuglog(zone, __func__, 1, |
11579 | 0 | "unable to add opt record: %s", |
11580 | 0 | isc_result_totext(result)); |
11581 | 0 | goto fail; |
11582 | 0 | } |
11583 | 0 | } |
11584 | | |
11585 | 0 | atomic_fetch_add_release(&args->stub->pending_requests, 1); |
11586 | |
|
11587 | 0 | curraddr = dns_remote_curraddr(&zone->primaries); |
11588 | 0 | result = dns_request_create( |
11589 | 0 | zone->view->requestmgr, message, &zone->sourceaddr, &curraddr, |
11590 | 0 | NULL, NULL, DNS_REQUESTOPT_TCP, args->tsig_key, |
11591 | 0 | args->connect_timeout, args->timeout, UDP_REQUEST_TIMEOUT, |
11592 | 0 | UDP_REQUEST_RETRIES, zone->loop, stub_glue_response, sgr, |
11593 | 0 | &sgr->request); |
11594 | |
|
11595 | 0 | if (result != ISC_R_SUCCESS) { |
11596 | 0 | uint_fast32_t pr; |
11597 | 0 | pr = atomic_fetch_sub_release(&args->stub->pending_requests, 1); |
11598 | 0 | INSIST(pr > 1); |
11599 | 0 | zone_debuglog(zone, __func__, 1, |
11600 | 0 | "dns_request_create() failed: %s", |
11601 | 0 | isc_result_totext(result)); |
11602 | 0 | goto fail; |
11603 | 0 | } |
11604 | | |
11605 | 0 | dns_message_detach(&message); |
11606 | |
|
11607 | 0 | return ISC_R_SUCCESS; |
11608 | | |
11609 | 0 | fail: |
11610 | 0 | dns_name_free(&sgr->name, zone->mctx); |
11611 | 0 | isc_mem_put(zone->mctx, sgr, sizeof(*sgr)); |
11612 | |
|
11613 | 0 | if (message != NULL) { |
11614 | 0 | dns_message_detach(&message); |
11615 | 0 | } |
11616 | |
|
11617 | 0 | return result; |
11618 | 0 | } |
11619 | | |
11620 | | static isc_result_t |
11621 | | save_nsrrset(dns_message_t *message, dns_name_t *name, |
11622 | | struct stub_cb_args *cb_args, dns_db_t *db, |
11623 | 0 | dns_dbversion_t *version) { |
11624 | 0 | dns_rdataset_t *nsrdataset = NULL; |
11625 | 0 | dns_rdataset_t *rdataset = NULL; |
11626 | 0 | dns_dbnode_t *node = NULL; |
11627 | 0 | dns_rdata_ns_t ns; |
11628 | 0 | isc_result_t result; |
11629 | 0 | bool has_glue = false; |
11630 | | |
11631 | | /* |
11632 | | * List of NS entries in answer, keep names that will be used |
11633 | | * to resolve missing A/AAAA glue for each entry. |
11634 | | */ |
11635 | 0 | dns_namelist_t ns_list; |
11636 | 0 | ISC_LIST_INIT(ns_list); |
11637 | | |
11638 | | /* |
11639 | | * Extract NS RRset from message. |
11640 | | */ |
11641 | 0 | result = dns_message_findname(message, DNS_SECTION_ANSWER, name, |
11642 | 0 | dns_rdatatype_ns, dns_rdatatype_none, |
11643 | 0 | NULL, &nsrdataset); |
11644 | 0 | if (result != ISC_R_SUCCESS) { |
11645 | 0 | goto done; |
11646 | 0 | } |
11647 | | |
11648 | | /* |
11649 | | * Add NS rdataset. |
11650 | | */ |
11651 | 0 | result = dns_db_findnode(db, name, true, &node); |
11652 | 0 | if (result != ISC_R_SUCCESS) { |
11653 | 0 | goto done; |
11654 | 0 | } |
11655 | 0 | result = dns_db_addrdataset(db, node, version, 0, nsrdataset, 0, NULL); |
11656 | 0 | dns_db_detachnode(&node); |
11657 | 0 | if (result != ISC_R_SUCCESS) { |
11658 | 0 | goto done; |
11659 | 0 | } |
11660 | | /* |
11661 | | * Add glue rdatasets. |
11662 | | */ |
11663 | 0 | DNS_RDATASET_FOREACH(nsrdataset) { |
11664 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
11665 | 0 | dns_rdataset_current(nsrdataset, &rdata); |
11666 | 0 | result = dns_rdata_tostruct(&rdata, &ns, NULL); |
11667 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
11668 | |
|
11669 | 0 | if (!dns_name_issubdomain(&ns.name, name)) { |
11670 | 0 | continue; |
11671 | 0 | } |
11672 | 0 | rdataset = NULL; |
11673 | 0 | result = dns_message_findname(message, DNS_SECTION_ADDITIONAL, |
11674 | 0 | &ns.name, dns_rdatatype_aaaa, |
11675 | 0 | dns_rdatatype_none, NULL, |
11676 | 0 | &rdataset); |
11677 | 0 | if (result == ISC_R_SUCCESS) { |
11678 | 0 | has_glue = true; |
11679 | 0 | result = dns_db_findnode(db, &ns.name, true, &node); |
11680 | 0 | if (result != ISC_R_SUCCESS) { |
11681 | 0 | goto done; |
11682 | 0 | } |
11683 | 0 | result = dns_db_addrdataset(db, node, version, 0, |
11684 | 0 | rdataset, 0, NULL); |
11685 | 0 | dns_db_detachnode(&node); |
11686 | 0 | if (result != ISC_R_SUCCESS) { |
11687 | 0 | goto done; |
11688 | 0 | } |
11689 | 0 | } |
11690 | | |
11691 | 0 | rdataset = NULL; |
11692 | 0 | result = dns_message_findname( |
11693 | 0 | message, DNS_SECTION_ADDITIONAL, &ns.name, |
11694 | 0 | dns_rdatatype_a, dns_rdatatype_none, NULL, &rdataset); |
11695 | 0 | if (result == ISC_R_SUCCESS) { |
11696 | 0 | has_glue = true; |
11697 | 0 | result = dns_db_findnode(db, &ns.name, true, &node); |
11698 | 0 | if (result != ISC_R_SUCCESS) { |
11699 | 0 | goto done; |
11700 | 0 | } |
11701 | 0 | result = dns_db_addrdataset(db, node, version, 0, |
11702 | 0 | rdataset, 0, NULL); |
11703 | 0 | dns_db_detachnode(&node); |
11704 | 0 | if (result != ISC_R_SUCCESS) { |
11705 | 0 | goto done; |
11706 | 0 | } |
11707 | 0 | } |
11708 | | |
11709 | | /* |
11710 | | * If no glue is found so far, we add the name to the list to |
11711 | | * resolve the A/AAAA glue later. If any glue is found in any |
11712 | | * iteration step, this list will be discarded and only the glue |
11713 | | * provided in this message will be used. |
11714 | | */ |
11715 | 0 | if (!has_glue && dns_name_issubdomain(&ns.name, name)) { |
11716 | 0 | dns_name_t *tmp_name; |
11717 | 0 | tmp_name = isc_mem_get(cb_args->stub->mctx, |
11718 | 0 | sizeof(*tmp_name)); |
11719 | 0 | dns_name_init(tmp_name); |
11720 | 0 | dns_name_dup(&ns.name, cb_args->stub->mctx, tmp_name); |
11721 | 0 | ISC_LIST_APPEND(ns_list, tmp_name, link); |
11722 | 0 | } |
11723 | 0 | } |
11724 | | |
11725 | | /* |
11726 | | * If no glue records were found, we attempt to resolve A/AAAA |
11727 | | * for each NS entry found in the answer. |
11728 | | */ |
11729 | 0 | if (!has_glue) { |
11730 | 0 | ISC_LIST_FOREACH(ns_list, ns_name, link) { |
11731 | | /* |
11732 | | * Resolve NS IPv4 address/A. |
11733 | | */ |
11734 | 0 | result = stub_request_nameserver_address(cb_args, true, |
11735 | 0 | ns_name); |
11736 | 0 | if (result != ISC_R_SUCCESS) { |
11737 | 0 | goto done; |
11738 | 0 | } |
11739 | | /* |
11740 | | * Resolve NS IPv6 address/AAAA. |
11741 | | */ |
11742 | 0 | result = stub_request_nameserver_address(cb_args, false, |
11743 | 0 | ns_name); |
11744 | 0 | if (result != ISC_R_SUCCESS) { |
11745 | 0 | goto done; |
11746 | 0 | } |
11747 | 0 | } |
11748 | 0 | } |
11749 | | |
11750 | 0 | result = ISC_R_SUCCESS; |
11751 | |
|
11752 | 0 | done: |
11753 | 0 | ISC_LIST_FOREACH(ns_list, ns_name, link) { |
11754 | 0 | ISC_LIST_UNLINK(ns_list, ns_name, link); |
11755 | 0 | dns_name_free(ns_name, cb_args->stub->mctx); |
11756 | 0 | isc_mem_put(cb_args->stub->mctx, ns_name, sizeof(*ns_name)); |
11757 | 0 | } |
11758 | 0 | return result; |
11759 | 0 | } |
11760 | | |
11761 | | static void |
11762 | 0 | stub_callback(void *arg) { |
11763 | 0 | dns_request_t *request = (dns_request_t *)arg; |
11764 | 0 | struct stub_cb_args *cb_args = dns_request_getarg(request); |
11765 | 0 | dns_stub_t *stub = cb_args->stub; |
11766 | 0 | dns_message_t *msg = NULL; |
11767 | 0 | dns_zone_t *zone = NULL; |
11768 | 0 | char primary[ISC_SOCKADDR_FORMATSIZE]; |
11769 | 0 | char source[ISC_SOCKADDR_FORMATSIZE]; |
11770 | 0 | uint32_t nscnt, cnamecnt; |
11771 | 0 | isc_result_t result; |
11772 | 0 | isc_sockaddr_t curraddr; |
11773 | 0 | isc_time_t now; |
11774 | 0 | bool exiting = false; |
11775 | |
|
11776 | 0 | INSIST(DNS_STUB_VALID(stub)); |
11777 | |
|
11778 | 0 | zone = stub->zone; |
11779 | |
|
11780 | 0 | ENTER; |
11781 | |
|
11782 | 0 | now = isc_time_now(); |
11783 | |
|
11784 | 0 | LOCK_ZONE(zone); |
11785 | |
|
11786 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
11787 | 0 | goto exiting; |
11788 | 0 | } |
11789 | | |
11790 | 0 | curraddr = dns_remote_curraddr(&zone->primaries); |
11791 | 0 | isc_sockaddr_format(&curraddr, primary, sizeof(primary)); |
11792 | 0 | isc_sockaddr_format(&zone->sourceaddr, source, sizeof(source)); |
11793 | |
|
11794 | 0 | result = dns_request_getresult(request); |
11795 | 0 | switch (result) { |
11796 | 0 | case ISC_R_SUCCESS: |
11797 | 0 | break; |
11798 | 0 | case ISC_R_SHUTTINGDOWN: |
11799 | 0 | case ISC_R_CANCELED: |
11800 | 0 | goto exiting; |
11801 | 0 | case ISC_R_TIMEDOUT: |
11802 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) { |
11803 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS); |
11804 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), |
11805 | 0 | "refreshing stub: timeout retrying " |
11806 | 0 | "without EDNS primary %s (source %s)", |
11807 | 0 | primary, source); |
11808 | 0 | goto same_primary; |
11809 | 0 | } |
11810 | 0 | FALLTHROUGH; |
11811 | 0 | default: |
11812 | 0 | dns_unreachcache_add(zone->view->unreachcache, &curraddr, |
11813 | 0 | &zone->sourceaddr); |
11814 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11815 | 0 | "could not refresh stub from primary " |
11816 | 0 | "%s (source %s): %s", |
11817 | 0 | primary, source, isc_result_totext(result)); |
11818 | 0 | goto next_primary; |
11819 | 0 | } |
11820 | | |
11821 | 0 | dns_message_create(zone->mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, |
11822 | 0 | &msg); |
11823 | |
|
11824 | 0 | result = dns_request_getresponse(request, msg, 0); |
11825 | 0 | if (result != ISC_R_SUCCESS) { |
11826 | 0 | goto next_primary; |
11827 | 0 | } |
11828 | | |
11829 | | /* |
11830 | | * Unexpected opcode. |
11831 | | */ |
11832 | 0 | if (msg->opcode != dns_opcode_query) { |
11833 | 0 | char opcode[128]; |
11834 | 0 | isc_buffer_t rb; |
11835 | |
|
11836 | 0 | isc_buffer_init(&rb, opcode, sizeof(opcode)); |
11837 | 0 | (void)dns_opcode_totext(msg->opcode, &rb); |
11838 | |
|
11839 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11840 | 0 | "refreshing stub: " |
11841 | 0 | "unexpected opcode (%.*s) from %s (source %s)", |
11842 | 0 | (int)rb.used, opcode, primary, source); |
11843 | 0 | goto next_primary; |
11844 | 0 | } |
11845 | | |
11846 | | /* |
11847 | | * Unexpected rcode. |
11848 | | */ |
11849 | 0 | if (msg->rcode != dns_rcode_noerror) { |
11850 | 0 | char rcode[128]; |
11851 | 0 | isc_buffer_t rb; |
11852 | |
|
11853 | 0 | isc_buffer_init(&rb, rcode, sizeof(rcode)); |
11854 | 0 | (void)dns_rcode_totext(msg->rcode, &rb); |
11855 | |
|
11856 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS) && |
11857 | 0 | (msg->rcode == dns_rcode_servfail || |
11858 | 0 | msg->rcode == dns_rcode_notimp || |
11859 | 0 | (msg->rcode == dns_rcode_formerr && msg->opt == NULL))) |
11860 | 0 | { |
11861 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), |
11862 | 0 | "refreshing stub: rcode (%.*s) retrying " |
11863 | 0 | "without EDNS primary %s (source %s)", |
11864 | 0 | (int)rb.used, rcode, primary, source); |
11865 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS); |
11866 | 0 | goto same_primary; |
11867 | 0 | } |
11868 | | |
11869 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11870 | 0 | "refreshing stub: " |
11871 | 0 | "unexpected rcode (%.*s) from %s (source %s)", |
11872 | 0 | (int)rb.used, rcode, primary, source); |
11873 | 0 | goto next_primary; |
11874 | 0 | } |
11875 | | |
11876 | | /* |
11877 | | * We need complete messages. |
11878 | | */ |
11879 | 0 | if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) { |
11880 | 0 | if (dns_request_usedtcp(request)) { |
11881 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11882 | 0 | "refreshing stub: truncated TCP " |
11883 | 0 | "response from primary %s (source %s)", |
11884 | 0 | primary, source); |
11885 | 0 | goto next_primary; |
11886 | 0 | } |
11887 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_USEVC); |
11888 | 0 | goto same_primary; |
11889 | 0 | } |
11890 | | |
11891 | | /* |
11892 | | * If non-auth log and next primary. |
11893 | | */ |
11894 | 0 | if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0) { |
11895 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11896 | 0 | "refreshing stub: " |
11897 | 0 | "non-authoritative answer from " |
11898 | 0 | "primary %s (source %s)", |
11899 | 0 | primary, source); |
11900 | 0 | goto next_primary; |
11901 | 0 | } |
11902 | | |
11903 | | /* |
11904 | | * Sanity checks. |
11905 | | */ |
11906 | 0 | cnamecnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_cname); |
11907 | 0 | nscnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_ns); |
11908 | |
|
11909 | 0 | if (cnamecnt != 0) { |
11910 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11911 | 0 | "refreshing stub: unexpected CNAME response " |
11912 | 0 | "from primary %s (source %s)", |
11913 | 0 | primary, source); |
11914 | 0 | goto next_primary; |
11915 | 0 | } |
11916 | | |
11917 | 0 | if (nscnt == 0) { |
11918 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11919 | 0 | "refreshing stub: no NS records in response " |
11920 | 0 | "from primary %s (source %s)", |
11921 | 0 | primary, source); |
11922 | 0 | goto next_primary; |
11923 | 0 | } |
11924 | | |
11925 | 0 | atomic_fetch_add(&stub->pending_requests, 1); |
11926 | | |
11927 | | /* |
11928 | | * Save answer. |
11929 | | */ |
11930 | 0 | result = save_nsrrset(msg, &zone->origin, cb_args, stub->db, |
11931 | 0 | stub->version); |
11932 | 0 | if (result != ISC_R_SUCCESS) { |
11933 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
11934 | 0 | "refreshing stub: unable to save NS records " |
11935 | 0 | "from primary %s (source %s)", |
11936 | 0 | primary, source); |
11937 | 0 | goto next_primary; |
11938 | 0 | } |
11939 | | |
11940 | 0 | dns_message_detach(&msg); |
11941 | 0 | dns_request_destroy(&zone->request); |
11942 | | |
11943 | | /* |
11944 | | * Check to see if there are no outstanding requests and |
11945 | | * finish off if that is so. |
11946 | | */ |
11947 | 0 | if (atomic_fetch_sub(&stub->pending_requests, 1) == 1) { |
11948 | 0 | isc_mem_put(zone->mctx, cb_args, sizeof(*cb_args)); |
11949 | 0 | stub_finish_zone_update(stub, now); |
11950 | 0 | goto free_stub; |
11951 | 0 | } |
11952 | | |
11953 | 0 | UNLOCK_ZONE(zone); |
11954 | 0 | return; |
11955 | | |
11956 | 0 | exiting: |
11957 | 0 | zone_debuglog(zone, __func__, 1, "exiting"); |
11958 | 0 | exiting = true; |
11959 | |
|
11960 | 0 | next_primary: |
11961 | 0 | isc_mem_put(zone->mctx, cb_args, sizeof(*cb_args)); |
11962 | 0 | if (stub->version != NULL) { |
11963 | 0 | dns_db_closeversion(stub->db, &stub->version, false); |
11964 | 0 | } |
11965 | 0 | if (stub->db != NULL) { |
11966 | 0 | dns_db_detach(&stub->db); |
11967 | 0 | } |
11968 | 0 | if (msg != NULL) { |
11969 | 0 | dns_message_detach(&msg); |
11970 | 0 | } |
11971 | 0 | dns_request_destroy(&zone->request); |
11972 | | /* |
11973 | | * Skip to next failed / untried primary. |
11974 | | */ |
11975 | 0 | dns_remote_next(&zone->primaries, true); |
11976 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOEDNS); |
11977 | 0 | if (exiting || dns_remote_done(&zone->primaries)) { |
11978 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH); |
11979 | 0 | dns__zone_settimer(zone, now); |
11980 | 0 | goto free_stub; |
11981 | 0 | } |
11982 | 0 | queue_soa_query(zone); |
11983 | 0 | goto free_stub; |
11984 | | |
11985 | 0 | same_primary: |
11986 | 0 | isc_mem_put(zone->mctx, cb_args, sizeof(*cb_args)); |
11987 | 0 | if (msg != NULL) { |
11988 | 0 | dns_message_detach(&msg); |
11989 | 0 | } |
11990 | 0 | dns_request_destroy(&zone->request); |
11991 | 0 | ns_query(zone, NULL, stub); |
11992 | 0 | UNLOCK_ZONE(zone); |
11993 | 0 | return; |
11994 | | |
11995 | 0 | free_stub: |
11996 | 0 | UNLOCK_ZONE(zone); |
11997 | 0 | stub->magic = 0; |
11998 | 0 | dns_zone_idetach(&stub->zone); |
11999 | 0 | INSIST(stub->db == NULL); |
12000 | 0 | INSIST(stub->version == NULL); |
12001 | 0 | isc_mem_put(stub->mctx, stub, sizeof(*stub)); |
12002 | 0 | } |
12003 | | |
12004 | | /* |
12005 | | * Get the EDNS EXPIRE option from the response and if it exists trim |
12006 | | * expire to be not more than it. |
12007 | | */ |
12008 | | static void |
12009 | 0 | get_edns_expire(dns_zone_t *zone, dns_message_t *message, uint32_t *expirep) { |
12010 | 0 | isc_result_t result; |
12011 | 0 | uint32_t expire; |
12012 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
12013 | 0 | isc_buffer_t optbuf; |
12014 | 0 | uint16_t optcode; |
12015 | 0 | uint16_t optlen; |
12016 | |
|
12017 | 0 | REQUIRE(expirep != NULL); |
12018 | 0 | REQUIRE(message != NULL); |
12019 | |
|
12020 | 0 | if (message->opt == NULL) { |
12021 | 0 | return; |
12022 | 0 | } |
12023 | | |
12024 | 0 | result = dns_rdataset_first(message->opt); |
12025 | 0 | if (result == ISC_R_SUCCESS) { |
12026 | 0 | dns_rdataset_current(message->opt, &rdata); |
12027 | 0 | isc_buffer_init(&optbuf, rdata.data, rdata.length); |
12028 | 0 | isc_buffer_add(&optbuf, rdata.length); |
12029 | 0 | while (isc_buffer_remaininglength(&optbuf) >= 4) { |
12030 | 0 | optcode = isc_buffer_getuint16(&optbuf); |
12031 | 0 | optlen = isc_buffer_getuint16(&optbuf); |
12032 | | /* |
12033 | | * A EDNS EXPIRE response has a length of 4. |
12034 | | */ |
12035 | 0 | if (optcode != DNS_OPT_EXPIRE || optlen != 4) { |
12036 | 0 | isc_buffer_forward(&optbuf, optlen); |
12037 | 0 | continue; |
12038 | 0 | } |
12039 | 0 | expire = isc_buffer_getuint32(&optbuf); |
12040 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), |
12041 | 0 | "got EDNS EXPIRE of %u", expire); |
12042 | | /* |
12043 | | * Trim *expirep? |
12044 | | */ |
12045 | 0 | if (expire < *expirep) { |
12046 | 0 | *expirep = expire; |
12047 | 0 | } |
12048 | 0 | break; |
12049 | 0 | } |
12050 | 0 | } |
12051 | 0 | } |
12052 | | |
12053 | | /* |
12054 | | * Set the file modification time zone->expire seconds before expiretime. |
12055 | | */ |
12056 | | static void |
12057 | 0 | setmodtime(dns_zone_t *zone, isc_time_t *expiretime) { |
12058 | 0 | isc_result_t result; |
12059 | 0 | isc_time_t when; |
12060 | 0 | isc_interval_t i; |
12061 | |
|
12062 | 0 | isc_interval_set(&i, zone->expire, 0); |
12063 | 0 | result = isc_time_subtract(expiretime, &i, &when); |
12064 | 0 | if (result != ISC_R_SUCCESS) { |
12065 | 0 | return; |
12066 | 0 | } |
12067 | | |
12068 | 0 | result = ISC_R_FAILURE; |
12069 | 0 | if (zone->journal != NULL) { |
12070 | 0 | result = isc_file_settime(zone->journal, &when); |
12071 | 0 | } |
12072 | 0 | if (result == ISC_R_SUCCESS && |
12073 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) && |
12074 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) |
12075 | 0 | { |
12076 | 0 | result = isc_file_settime(zone->masterfile, &when); |
12077 | 0 | } else if (result != ISC_R_SUCCESS) { |
12078 | 0 | result = isc_file_settime(zone->masterfile, &when); |
12079 | 0 | } |
12080 | | |
12081 | | /* |
12082 | | * Someone removed the file from underneath us! |
12083 | | */ |
12084 | 0 | if (result == ISC_R_FILENOTFOUND) { |
12085 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
12086 | 0 | } else if (result != ISC_R_SUCCESS) { |
12087 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
12088 | 0 | "refresh: could not set " |
12089 | 0 | "file modification time of '%s': %s", |
12090 | 0 | zone->masterfile, isc_result_totext(result)); |
12091 | 0 | } |
12092 | 0 | } |
12093 | | |
12094 | | /* |
12095 | | * An SOA query has finished (successfully or not). |
12096 | | */ |
12097 | | static void |
12098 | 0 | refresh_callback(void *arg) { |
12099 | 0 | dns_request_t *request = (dns_request_t *)arg; |
12100 | 0 | dns_zone_t *zone = dns_request_getarg(request); |
12101 | 0 | dns_message_t *msg = NULL; |
12102 | 0 | uint32_t soacnt, cnamecnt, soacount, nscount; |
12103 | 0 | isc_time_t now; |
12104 | 0 | char primary[ISC_SOCKADDR_FORMATSIZE]; |
12105 | 0 | char source[ISC_SOCKADDR_FORMATSIZE]; |
12106 | 0 | dns_rdataset_t *rdataset = NULL; |
12107 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
12108 | 0 | dns_rdata_soa_t soa; |
12109 | 0 | isc_result_t result; |
12110 | 0 | const isc_result_t eresult = dns_request_getresult(request); |
12111 | 0 | isc_sockaddr_t curraddr; |
12112 | 0 | uint32_t serial, oldserial = 0; |
12113 | 0 | bool do_queue_xfrin = false; |
12114 | |
|
12115 | 0 | INSIST(DNS_ZONE_VALID(zone)); |
12116 | |
|
12117 | 0 | ENTER; |
12118 | |
|
12119 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
12120 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_DEBUG(3), |
12121 | 0 | "refresh: request result: %s", |
12122 | 0 | isc_result_totext(eresult)); |
12123 | 0 | } |
12124 | |
|
12125 | 0 | now = isc_time_now(); |
12126 | |
|
12127 | 0 | LOCK_ZONE(zone); |
12128 | |
|
12129 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
12130 | 0 | goto exiting; |
12131 | 0 | } |
12132 | | |
12133 | | /* |
12134 | | * If timeout, log and try the next primary |
12135 | | */ |
12136 | 0 | curraddr = dns_remote_curraddr(&zone->primaries); |
12137 | 0 | isc_sockaddr_format(&curraddr, primary, sizeof(primary)); |
12138 | 0 | isc_sockaddr_format(&zone->sourceaddr, source, sizeof(source)); |
12139 | |
|
12140 | 0 | switch (eresult) { |
12141 | 0 | case ISC_R_SUCCESS: |
12142 | 0 | break; |
12143 | 0 | case ISC_R_SHUTTINGDOWN: |
12144 | 0 | case ISC_R_CANCELED: |
12145 | 0 | goto exiting; |
12146 | 0 | case ISC_R_TIMEDOUT: |
12147 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) { |
12148 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS); |
12149 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12150 | 0 | ISC_LOG_DEBUG(1), |
12151 | 0 | "refresh: timeout retrying without EDNS " |
12152 | 0 | "primary %s (source %s)", |
12153 | 0 | primary, source); |
12154 | 0 | goto same_primary; |
12155 | 0 | } else if (!dns_request_usedtcp(request)) { |
12156 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12157 | 0 | ISC_LOG_INFO, |
12158 | 0 | "refresh: retry limit for " |
12159 | 0 | "primary %s exceeded (source %s)", |
12160 | 0 | primary, source); |
12161 | | /* Try with secondary with TCP. */ |
12162 | 0 | if ((zone->type == dns_zone_secondary || |
12163 | 0 | zone->type == dns_zone_mirror || |
12164 | 0 | zone->type == dns_zone_redirect) && |
12165 | 0 | DNS_ZONE_OPTION(zone, DNS_ZONEOPT_TRYTCPREFRESH)) |
12166 | 0 | { |
12167 | 0 | if (dns_unreachcache_find( |
12168 | 0 | zone->view->unreachcache, &curraddr, |
12169 | 0 | &zone->sourceaddr) != ISC_R_SUCCESS) |
12170 | 0 | { |
12171 | 0 | DNS_ZONE_SETFLAG( |
12172 | 0 | zone, |
12173 | 0 | DNS_ZONEFLG_SOABEFOREAXFR); |
12174 | 0 | goto tcp_transfer; |
12175 | 0 | } |
12176 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12177 | 0 | ISC_LOG_DEBUG(1), |
12178 | 0 | "refresh: skipped tcp fallback " |
12179 | 0 | "as primary %s (source %s) is " |
12180 | 0 | "unreachable (cached)", |
12181 | 0 | primary, source); |
12182 | 0 | } |
12183 | 0 | goto next_primary; |
12184 | 0 | } |
12185 | 0 | FALLTHROUGH; |
12186 | 0 | default: |
12187 | 0 | result = eresult; |
12188 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12189 | 0 | "refresh: failure trying primary " |
12190 | 0 | "%s (source %s): %s", |
12191 | 0 | primary, source, isc_result_totext(result)); |
12192 | 0 | goto next_primary; |
12193 | 0 | } |
12194 | | |
12195 | 0 | dns_message_create(zone->mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, |
12196 | 0 | &msg); |
12197 | 0 | result = dns_request_getresponse(request, msg, 0); |
12198 | 0 | if (result != ISC_R_SUCCESS) { |
12199 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12200 | 0 | "refresh: failure trying primary " |
12201 | 0 | "%s (source %s): %s", |
12202 | 0 | primary, source, isc_result_totext(result)); |
12203 | 0 | goto next_primary; |
12204 | 0 | } |
12205 | | |
12206 | | /* |
12207 | | * Unexpected opcode. |
12208 | | */ |
12209 | 0 | if (msg->opcode != dns_opcode_query) { |
12210 | 0 | char opcode[128]; |
12211 | 0 | isc_buffer_t rb; |
12212 | |
|
12213 | 0 | isc_buffer_init(&rb, opcode, sizeof(opcode)); |
12214 | 0 | (void)dns_opcode_totext(msg->opcode, &rb); |
12215 | |
|
12216 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12217 | 0 | "refresh: " |
12218 | 0 | "unexpected opcode (%.*s) from %s (source %s)", |
12219 | 0 | (int)rb.used, opcode, primary, source); |
12220 | 0 | goto next_primary; |
12221 | 0 | } |
12222 | | |
12223 | | /* |
12224 | | * Unexpected rcode. |
12225 | | */ |
12226 | 0 | if (msg->rcode != dns_rcode_noerror) { |
12227 | 0 | char rcode[128]; |
12228 | 0 | isc_buffer_t rb; |
12229 | |
|
12230 | 0 | isc_buffer_init(&rb, rcode, sizeof(rcode)); |
12231 | 0 | (void)dns_rcode_totext(msg->rcode, &rb); |
12232 | |
|
12233 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS) && |
12234 | 0 | (msg->rcode == dns_rcode_servfail || |
12235 | 0 | msg->rcode == dns_rcode_notimp || |
12236 | 0 | (msg->rcode == dns_rcode_formerr && msg->opt == NULL))) |
12237 | 0 | { |
12238 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12239 | 0 | ISC_LOG_DEBUG(1), |
12240 | 0 | "refresh: rcode (%.*s) retrying without " |
12241 | 0 | "EDNS primary %s (source %s)", |
12242 | 0 | (int)rb.used, rcode, primary, source); |
12243 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS); |
12244 | 0 | goto same_primary; |
12245 | 0 | } |
12246 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS) && |
12247 | 0 | msg->rcode == dns_rcode_badvers) |
12248 | 0 | { |
12249 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12250 | 0 | ISC_LOG_DEBUG(1), |
12251 | 0 | "refresh: rcode (%.*s) retrying without " |
12252 | 0 | "EDNS EXPIRE OPTION primary %s " |
12253 | 0 | "(source %s)", |
12254 | 0 | (int)rb.used, rcode, primary, source); |
12255 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS); |
12256 | 0 | goto same_primary; |
12257 | 0 | } |
12258 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12259 | 0 | "refresh: unexpected rcode (%.*s) from " |
12260 | 0 | "primary %s (source %s)", |
12261 | 0 | (int)rb.used, rcode, primary, source); |
12262 | | /* |
12263 | | * Perhaps AXFR/IXFR is allowed even if SOA queries aren't. |
12264 | | */ |
12265 | 0 | if (msg->rcode == dns_rcode_refused && |
12266 | 0 | (zone->type == dns_zone_secondary || |
12267 | 0 | zone->type == dns_zone_mirror || |
12268 | 0 | zone->type == dns_zone_redirect)) |
12269 | 0 | { |
12270 | 0 | goto tcp_transfer; |
12271 | 0 | } |
12272 | 0 | goto next_primary; |
12273 | 0 | } |
12274 | | |
12275 | | /* |
12276 | | * If truncated punt to zone transfer which will query again. |
12277 | | */ |
12278 | 0 | if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) { |
12279 | 0 | if (zone->type == dns_zone_secondary || |
12280 | 0 | zone->type == dns_zone_mirror || |
12281 | 0 | zone->type == dns_zone_redirect) |
12282 | 0 | { |
12283 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12284 | 0 | ISC_LOG_INFO, |
12285 | 0 | "refresh: truncated UDP answer, " |
12286 | 0 | "initiating TCP zone xfer " |
12287 | 0 | "for primary %s (source %s)", |
12288 | 0 | primary, source); |
12289 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_SOABEFOREAXFR); |
12290 | 0 | goto tcp_transfer; |
12291 | 0 | } else { |
12292 | 0 | INSIST(zone->type == dns_zone_stub); |
12293 | 0 | if (dns_request_usedtcp(request)) { |
12294 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12295 | 0 | ISC_LOG_INFO, |
12296 | 0 | "refresh: truncated TCP response " |
12297 | 0 | "from primary %s (source %s)", |
12298 | 0 | primary, source); |
12299 | 0 | goto next_primary; |
12300 | 0 | } |
12301 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_USEVC); |
12302 | 0 | goto same_primary; |
12303 | 0 | } |
12304 | 0 | } |
12305 | | |
12306 | | /* |
12307 | | * If non-auth, log and try the next primary |
12308 | | */ |
12309 | 0 | if ((msg->flags & DNS_MESSAGEFLAG_AA) == 0) { |
12310 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12311 | 0 | "refresh: non-authoritative answer from " |
12312 | 0 | "primary %s (source %s)", |
12313 | 0 | primary, source); |
12314 | 0 | goto next_primary; |
12315 | 0 | } |
12316 | | |
12317 | 0 | cnamecnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_cname); |
12318 | 0 | soacnt = message_count(msg, DNS_SECTION_ANSWER, dns_rdatatype_soa); |
12319 | 0 | nscount = message_count(msg, DNS_SECTION_AUTHORITY, dns_rdatatype_ns); |
12320 | 0 | soacount = message_count(msg, DNS_SECTION_AUTHORITY, dns_rdatatype_soa); |
12321 | | |
12322 | | /* |
12323 | | * There should not be a CNAME record at top of zone. |
12324 | | */ |
12325 | 0 | if (cnamecnt != 0) { |
12326 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12327 | 0 | "refresh: CNAME at top of zone " |
12328 | 0 | "in primary %s (source %s)", |
12329 | 0 | primary, source); |
12330 | 0 | goto next_primary; |
12331 | 0 | } |
12332 | | |
12333 | | /* |
12334 | | * If referral, log and try the next primary; |
12335 | | */ |
12336 | 0 | if (soacnt == 0 && soacount == 0 && nscount != 0) { |
12337 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12338 | 0 | "refresh: referral response " |
12339 | 0 | "from primary %s (source %s)", |
12340 | 0 | primary, source); |
12341 | 0 | goto next_primary; |
12342 | 0 | } |
12343 | | |
12344 | | /* |
12345 | | * If nodata, log and try the next primary; |
12346 | | */ |
12347 | 0 | if (soacnt == 0 && (nscount == 0 || soacount != 0)) { |
12348 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12349 | 0 | "refresh: NODATA response " |
12350 | 0 | "from primary %s (source %s)", |
12351 | 0 | primary, source); |
12352 | 0 | goto next_primary; |
12353 | 0 | } |
12354 | | |
12355 | | /* |
12356 | | * Only one soa at top of zone. |
12357 | | */ |
12358 | 0 | if (soacnt != 1) { |
12359 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12360 | 0 | "refresh: answer SOA count (%d) != 1 " |
12361 | 0 | "from primary %s (source %s)", |
12362 | 0 | soacnt, primary, source); |
12363 | 0 | goto next_primary; |
12364 | 0 | } |
12365 | | |
12366 | | /* |
12367 | | * Extract serial |
12368 | | */ |
12369 | 0 | rdataset = NULL; |
12370 | 0 | result = dns_message_findname(msg, DNS_SECTION_ANSWER, &zone->origin, |
12371 | 0 | dns_rdatatype_soa, dns_rdatatype_none, |
12372 | 0 | NULL, &rdataset); |
12373 | 0 | if (result != ISC_R_SUCCESS) { |
12374 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12375 | 0 | "refresh: unable to get SOA record " |
12376 | 0 | "from primary %s (source %s)", |
12377 | 0 | primary, source); |
12378 | 0 | goto next_primary; |
12379 | 0 | } |
12380 | | |
12381 | 0 | result = dns_rdataset_first(rdataset); |
12382 | 0 | if (result != ISC_R_SUCCESS) { |
12383 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
12384 | 0 | "refresh: dns_rdataset_first() failed"); |
12385 | 0 | goto next_primary; |
12386 | 0 | } |
12387 | | |
12388 | 0 | dns_rdataset_current(rdataset, &rdata); |
12389 | 0 | result = dns_rdata_tostruct(&rdata, &soa, NULL); |
12390 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
12391 | |
|
12392 | 0 | serial = soa.serial; |
12393 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) { |
12394 | 0 | unsigned int dbsoacount; |
12395 | 0 | result = zone_get_from_db(zone, zone->db, NULL, &dbsoacount, |
12396 | 0 | NULL, &oldserial, NULL, NULL, NULL, |
12397 | 0 | NULL, NULL); |
12398 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
12399 | 0 | RUNTIME_CHECK(dbsoacount > 0U); |
12400 | 0 | zone_debuglogc(zone, DNS_LOGCATEGORY_XFER_IN, __func__, 1, |
12401 | 0 | "serial: new %u, old %u", serial, oldserial); |
12402 | 0 | } else { |
12403 | 0 | zone_debuglogc(zone, DNS_LOGCATEGORY_XFER_IN, __func__, 1, |
12404 | 0 | "serial: new %u, old not loaded", serial); |
12405 | 0 | } |
12406 | |
|
12407 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) || |
12408 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER) || |
12409 | 0 | isc_serial_gt(serial, oldserial)) |
12410 | 0 | { |
12411 | 0 | if (dns_unreachcache_find(zone->view->unreachcache, &curraddr, |
12412 | 0 | &zone->sourceaddr) == ISC_R_SUCCESS) |
12413 | 0 | { |
12414 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12415 | 0 | ISC_LOG_INFO, |
12416 | 0 | "refresh: skipping %s as primary %s " |
12417 | 0 | "(source %s) is unreachable (cached)", |
12418 | 0 | (zone->type == dns_zone_secondary || |
12419 | 0 | zone->type == dns_zone_mirror || |
12420 | 0 | zone->type == dns_zone_redirect) |
12421 | 0 | ? "zone transfer" |
12422 | 0 | : "NS query", |
12423 | 0 | primary, source); |
12424 | 0 | goto next_primary; |
12425 | 0 | } |
12426 | 0 | tcp_transfer: |
12427 | 0 | dns_request_destroy(&zone->request); |
12428 | 0 | if (zone->type == dns_zone_secondary || |
12429 | 0 | zone->type == dns_zone_mirror || |
12430 | 0 | zone->type == dns_zone_redirect) |
12431 | 0 | { |
12432 | 0 | do_queue_xfrin = true; |
12433 | 0 | } else { |
12434 | 0 | INSIST(zone->type == dns_zone_stub); |
12435 | 0 | ns_query(zone, rdataset, NULL); |
12436 | 0 | } |
12437 | 0 | if (msg != NULL) { |
12438 | 0 | dns_message_detach(&msg); |
12439 | 0 | } |
12440 | 0 | } else if (isc_serial_eq(soa.serial, oldserial)) { |
12441 | 0 | isc_time_t expiretime; |
12442 | 0 | uint32_t expire; |
12443 | | |
12444 | | /* |
12445 | | * Compute the new expire time based on this response. |
12446 | | */ |
12447 | 0 | expire = zone->expire; |
12448 | 0 | get_edns_expire(zone, msg, &expire); |
12449 | 0 | DNS_ZONE_TIME_ADD(&now, expire, &expiretime); |
12450 | | |
12451 | | /* |
12452 | | * Has the expire time improved? |
12453 | | */ |
12454 | 0 | if (isc_time_compare(&expiretime, &zone->expiretime) > 0) { |
12455 | 0 | zone->expiretime = expiretime; |
12456 | 0 | if (zone->masterfile != NULL) { |
12457 | 0 | setmodtime(zone, &expiretime); |
12458 | 0 | } |
12459 | 0 | } |
12460 | |
|
12461 | 0 | DNS_ZONE_JITTER_ADD(&now, zone->refresh, &zone->refreshtime); |
12462 | 0 | dns_remote_mark(&zone->primaries, true); |
12463 | 0 | goto next_primary; |
12464 | 0 | } else { |
12465 | 0 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_MULTIMASTER)) { |
12466 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12467 | 0 | ISC_LOG_INFO, |
12468 | 0 | "serial number (%u) " |
12469 | 0 | "received from primary %s < ours (%u)", |
12470 | 0 | soa.serial, primary, oldserial); |
12471 | 0 | } else { |
12472 | 0 | zone_debuglogc(zone, DNS_LOGCATEGORY_XFER_IN, __func__, |
12473 | 0 | 1, "ahead"); |
12474 | 0 | } |
12475 | 0 | dns_remote_mark(&zone->primaries, true); |
12476 | 0 | goto next_primary; |
12477 | 0 | } |
12478 | 0 | if (msg != NULL) { |
12479 | 0 | dns_message_detach(&msg); |
12480 | 0 | } |
12481 | 0 | goto detach; |
12482 | | |
12483 | 0 | next_primary: |
12484 | 0 | if (msg != NULL) { |
12485 | 0 | dns_message_detach(&msg); |
12486 | 0 | } |
12487 | 0 | dns_request_destroy(&zone->request); |
12488 | | /* |
12489 | | * Skip to next failed / untried primary. |
12490 | | */ |
12491 | 0 | dns_remote_next(&zone->primaries, true); |
12492 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NOEDNS); |
12493 | 0 | if (dns_remote_done(&zone->primaries)) { |
12494 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH); |
12495 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDREFRESH)) { |
12496 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDREFRESH); |
12497 | 0 | zone->refreshtime = now; |
12498 | 0 | } |
12499 | 0 | dns__zone_settimer(zone, now); |
12500 | 0 | goto detach; |
12501 | 0 | } |
12502 | | |
12503 | 0 | queue_soa_query(zone); |
12504 | 0 | goto detach; |
12505 | | |
12506 | 0 | exiting: |
12507 | | /* |
12508 | | * We can get here not only during shutdown, but also when the refresh |
12509 | | * is canceled during reconfiguration. In that case, make sure to clear |
12510 | | * the DNS_ZONEFLG_REFRESH flag so that future zone refreshes don't get |
12511 | | * stuck, and make sure a new refresh attempt is made again soon after |
12512 | | * the reconfiguration is complete. |
12513 | | */ |
12514 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH); |
12515 | 0 | zone->refreshtime = now; |
12516 | 0 | dns__zone_settimer(zone, now); |
12517 | |
|
12518 | 0 | dns_request_destroy(&zone->request); |
12519 | 0 | goto detach; |
12520 | | |
12521 | 0 | same_primary: |
12522 | 0 | if (msg != NULL) { |
12523 | 0 | dns_message_detach(&msg); |
12524 | 0 | } |
12525 | 0 | dns_request_destroy(&zone->request); |
12526 | 0 | queue_soa_query(zone); |
12527 | |
|
12528 | 0 | detach: |
12529 | 0 | if (do_queue_xfrin) { |
12530 | | /* Shows in the statistics channel the duration of the step. */ |
12531 | 0 | zone->xfrintime = isc_time_now(); |
12532 | 0 | } |
12533 | 0 | UNLOCK_ZONE(zone); |
12534 | 0 | if (do_queue_xfrin) { |
12535 | 0 | queue_xfrin(zone); |
12536 | 0 | } |
12537 | 0 | dns_zone_idetach(&zone); |
12538 | 0 | return; |
12539 | 0 | } |
12540 | | |
12541 | | struct soaquery { |
12542 | | dns_zone_t *zone; |
12543 | | isc_rlevent_t *rlevent; |
12544 | | }; |
12545 | | |
12546 | | static void |
12547 | 0 | queue_soa_query(dns_zone_t *zone) { |
12548 | 0 | isc_result_t result; |
12549 | 0 | struct soaquery *sq = NULL; |
12550 | |
|
12551 | 0 | ENTER; |
12552 | | /* |
12553 | | * Locked by caller |
12554 | | */ |
12555 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
12556 | |
|
12557 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
12558 | 0 | cancel_refresh(zone); |
12559 | 0 | return; |
12560 | 0 | } |
12561 | | |
12562 | 0 | sq = isc_mem_get(zone->mctx, sizeof(*sq)); |
12563 | 0 | *sq = (struct soaquery){ .zone = NULL }; |
12564 | | |
12565 | | /* Shows in the statistics channel the duration of the current step. */ |
12566 | 0 | zone->xfrintime = isc_time_now(); |
12567 | | |
12568 | | /* |
12569 | | * Attach so that we won't clean up until the event is delivered. |
12570 | | */ |
12571 | 0 | zone_iattach(zone, &sq->zone); |
12572 | 0 | result = isc_ratelimiter_enqueue(zone->zmgr->refreshrl, zone->loop, |
12573 | 0 | soa_query, sq, &sq->rlevent); |
12574 | 0 | if (result != ISC_R_SUCCESS) { |
12575 | 0 | zone_idetach(&sq->zone); |
12576 | 0 | isc_mem_put(zone->mctx, sq, sizeof(*sq)); |
12577 | 0 | cancel_refresh(zone); |
12578 | 0 | } |
12579 | 0 | } |
12580 | | |
12581 | | static void |
12582 | 0 | soa_query(void *arg) { |
12583 | 0 | struct soaquery *sq = (struct soaquery *)arg; |
12584 | 0 | dns_zone_t *zone = sq->zone; |
12585 | 0 | isc_result_t result = ISC_R_FAILURE; |
12586 | 0 | dns_message_t *message = NULL; |
12587 | 0 | isc_netaddr_t primaryip; |
12588 | 0 | dns_tsigkey_t *key = NULL; |
12589 | 0 | dns_transport_t *transport = NULL; |
12590 | 0 | uint32_t options; |
12591 | 0 | bool cancel = true; |
12592 | 0 | bool have_xfrsource = false, reqnsid, reqexpire; |
12593 | 0 | uint16_t udpsize = SEND_BUFFER_SIZE; |
12594 | 0 | isc_sockaddr_t curraddr, sourceaddr; |
12595 | 0 | bool do_queue_xfrin = false; |
12596 | |
|
12597 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
12598 | |
|
12599 | 0 | ENTER; |
12600 | |
|
12601 | 0 | LOCK_ZONE(zone); |
12602 | 0 | if (sq->rlevent->canceled || DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING) || |
12603 | 0 | zone->view->requestmgr == NULL) |
12604 | 0 | { |
12605 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
12606 | 0 | cancel = false; |
12607 | 0 | } |
12608 | 0 | goto cleanup; |
12609 | 0 | } |
12610 | | |
12611 | 0 | again: |
12612 | 0 | dns_zone_logc( |
12613 | 0 | zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_DEBUG(3), |
12614 | 0 | "soa_query: remote server current address index %d count %d", |
12615 | 0 | zone->primaries.curraddr, zone->primaries.addrcnt); |
12616 | 0 | INSIST(dns_remote_count(&zone->primaries) > 0); |
12617 | 0 | INSIST(!dns_remote_done(&zone->primaries)); |
12618 | |
|
12619 | 0 | sourceaddr = dns_remote_sourceaddr(&zone->primaries); |
12620 | 0 | curraddr = dns_remote_curraddr(&zone->primaries); |
12621 | 0 | isc_netaddr_fromsockaddr(&primaryip, &curraddr); |
12622 | |
|
12623 | 0 | if (isc_sockaddr_disabled(&curraddr)) { |
12624 | 0 | goto skip_primary; |
12625 | 0 | } |
12626 | | |
12627 | | /* |
12628 | | * First, look for a tsig key in the primaries statement, then |
12629 | | * try for a server key. |
12630 | | */ |
12631 | 0 | if (dns_remote_keyname(&zone->primaries) != NULL) { |
12632 | 0 | dns_view_t *view = dns_zone_getview(zone); |
12633 | 0 | dns_name_t *keyname = dns_remote_keyname(&zone->primaries); |
12634 | 0 | result = dns_view_gettsig(view, keyname, &key); |
12635 | 0 | if (result != ISC_R_SUCCESS) { |
12636 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
12637 | 0 | dns_name_format(keyname, namebuf, sizeof(namebuf)); |
12638 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12639 | 0 | ISC_LOG_ERROR, "unable to find key: %s", |
12640 | 0 | namebuf); |
12641 | 0 | goto skip_primary; |
12642 | 0 | } |
12643 | 0 | } |
12644 | 0 | if (key == NULL) { |
12645 | 0 | result = dns_view_getpeertsig(zone->view, &primaryip, &key); |
12646 | 0 | if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) { |
12647 | 0 | char addrbuf[ISC_NETADDR_FORMATSIZE]; |
12648 | 0 | isc_netaddr_format(&primaryip, addrbuf, |
12649 | 0 | sizeof(addrbuf)); |
12650 | 0 | dns_zone_logc( |
12651 | 0 | zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_ERROR, |
12652 | 0 | "unable to find TSIG key for %s", addrbuf); |
12653 | 0 | goto skip_primary; |
12654 | 0 | } |
12655 | 0 | } |
12656 | | |
12657 | 0 | if (dns_remote_tlsname(&zone->primaries) != NULL) { |
12658 | 0 | dns_view_t *view = dns_zone_getview(zone); |
12659 | 0 | dns_name_t *tlsname = dns_remote_tlsname(&zone->primaries); |
12660 | 0 | result = dns_view_gettransport(view, DNS_TRANSPORT_TLS, tlsname, |
12661 | 0 | &transport); |
12662 | 0 | if (result != ISC_R_SUCCESS) { |
12663 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
12664 | 0 | dns_name_format(tlsname, namebuf, sizeof(namebuf)); |
12665 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
12666 | 0 | ISC_LOG_ERROR, |
12667 | 0 | "unable to find TLS configuration: %s", |
12668 | 0 | namebuf); |
12669 | 0 | goto skip_primary; |
12670 | 0 | } |
12671 | 0 | } |
12672 | | |
12673 | 0 | options = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_USEVC) ? DNS_REQUESTOPT_TCP |
12674 | 0 | : 0; |
12675 | 0 | reqnsid = zone->view->requestnsid; |
12676 | 0 | reqexpire = zone->requestexpire; |
12677 | 0 | if (zone->view->peers != NULL) { |
12678 | 0 | dns_peer_t *peer = NULL; |
12679 | 0 | bool edns, usetcp; |
12680 | 0 | result = dns_peerlist_peerbyaddr(zone->view->peers, &primaryip, |
12681 | 0 | &peer); |
12682 | 0 | if (result == ISC_R_SUCCESS) { |
12683 | 0 | result = dns_peer_getsupportedns(peer, &edns); |
12684 | 0 | if (result == ISC_R_SUCCESS && !edns) { |
12685 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS); |
12686 | 0 | } |
12687 | 0 | result = dns_peer_gettransfersource(peer, |
12688 | 0 | &zone->sourceaddr); |
12689 | 0 | if (result == ISC_R_SUCCESS) { |
12690 | 0 | have_xfrsource = true; |
12691 | 0 | } |
12692 | 0 | udpsize = dns_view_getudpsize(zone->view); |
12693 | 0 | (void)dns_peer_getudpsize(peer, &udpsize); |
12694 | 0 | (void)dns_peer_getrequestnsid(peer, &reqnsid); |
12695 | 0 | (void)dns_peer_getrequestexpire(peer, &reqexpire); |
12696 | 0 | result = dns_peer_getforcetcp(peer, &usetcp); |
12697 | 0 | if (result == ISC_R_SUCCESS && usetcp) { |
12698 | 0 | options |= DNS_REQUESTOPT_TCP; |
12699 | 0 | } |
12700 | 0 | } |
12701 | 0 | } |
12702 | |
|
12703 | 0 | switch (isc_sockaddr_pf(&curraddr)) { |
12704 | 0 | case PF_INET: |
12705 | 0 | if (!have_xfrsource) { |
12706 | 0 | isc_sockaddr_t any; |
12707 | 0 | isc_sockaddr_any(&any); |
12708 | |
|
12709 | 0 | zone->sourceaddr = sourceaddr; |
12710 | 0 | if (isc_sockaddr_equal(&sourceaddr, &any)) { |
12711 | 0 | zone->sourceaddr = zone->xfrsource4; |
12712 | 0 | } |
12713 | 0 | } |
12714 | 0 | break; |
12715 | 0 | case PF_INET6: |
12716 | 0 | if (!have_xfrsource) { |
12717 | 0 | isc_sockaddr_t any; |
12718 | 0 | isc_sockaddr_any6(&any); |
12719 | |
|
12720 | 0 | zone->sourceaddr = sourceaddr; |
12721 | 0 | if (isc_sockaddr_equal(&zone->sourceaddr, &any)) { |
12722 | 0 | zone->sourceaddr = zone->xfrsource6; |
12723 | 0 | } |
12724 | 0 | } |
12725 | 0 | break; |
12726 | 0 | default: |
12727 | 0 | CLEANUP(ISC_R_NOTIMPLEMENTED); |
12728 | 0 | } |
12729 | | |
12730 | | /* |
12731 | | * FIXME(OS): This is a bit hackish, but it enforces the SOA query to go |
12732 | | * through the XFR channel instead of doing dns_request that doesn't |
12733 | | * have DoT support yet. |
12734 | | */ |
12735 | 0 | if (transport != NULL) { |
12736 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_SOABEFOREAXFR); |
12737 | 0 | do_queue_xfrin = true; |
12738 | 0 | cancel = false; |
12739 | 0 | result = ISC_R_SUCCESS; |
12740 | 0 | goto cleanup; |
12741 | 0 | } |
12742 | | |
12743 | 0 | create_query(zone, dns_rdatatype_soa, &zone->origin, &message); |
12744 | |
|
12745 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) { |
12746 | 0 | result = add_opt(message, udpsize, reqnsid, reqexpire); |
12747 | 0 | if (result != ISC_R_SUCCESS) { |
12748 | 0 | zone_debuglogc(zone, DNS_LOGCATEGORY_XFER_IN, __func__, |
12749 | 0 | 1, "unable to add opt record: %s", |
12750 | 0 | isc_result_totext(result)); |
12751 | 0 | } |
12752 | 0 | } |
12753 | |
|
12754 | 0 | zone_iattach(zone, &(dns_zone_t *){ NULL }); |
12755 | 0 | const unsigned int connect_timeout = isc_nm_getprimariestimeout() / |
12756 | 0 | MS_PER_SEC; |
12757 | 0 | result = dns_request_create( |
12758 | 0 | zone->view->requestmgr, message, &zone->sourceaddr, &curraddr, |
12759 | 0 | NULL, NULL, options, key, connect_timeout, TCP_REQUEST_TIMEOUT, |
12760 | 0 | UDP_REQUEST_TIMEOUT, UDP_REQUEST_RETRIES, zone->loop, |
12761 | 0 | refresh_callback, zone, &zone->request); |
12762 | 0 | if (result != ISC_R_SUCCESS) { |
12763 | 0 | zone_idetach(&(dns_zone_t *){ zone }); |
12764 | 0 | zone_debuglogc(zone, DNS_LOGCATEGORY_XFER_IN, __func__, 1, |
12765 | 0 | "dns_request_create() failed: %s", |
12766 | 0 | isc_result_totext(result)); |
12767 | 0 | goto skip_primary; |
12768 | 0 | } else { |
12769 | | /* Shows in the statistics channel the duration of the query. */ |
12770 | 0 | zone->xfrintime = isc_time_now(); |
12771 | |
|
12772 | 0 | if (isc_sockaddr_pf(&curraddr) == PF_INET) { |
12773 | 0 | dns__zone_stats_increment( |
12774 | 0 | zone, dns_zonestatscounter_soaoutv4); |
12775 | 0 | } else { |
12776 | 0 | dns__zone_stats_increment( |
12777 | 0 | zone, dns_zonestatscounter_soaoutv6); |
12778 | 0 | } |
12779 | 0 | } |
12780 | 0 | cancel = false; |
12781 | 0 | cleanup: |
12782 | 0 | if (transport != NULL) { |
12783 | 0 | dns_transport_detach(&transport); |
12784 | 0 | } |
12785 | 0 | if (key != NULL) { |
12786 | 0 | dns_tsigkey_detach(&key); |
12787 | 0 | } |
12788 | 0 | if (result != ISC_R_SUCCESS) { |
12789 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH); |
12790 | 0 | } |
12791 | 0 | if (message != NULL) { |
12792 | 0 | dns_message_detach(&message); |
12793 | 0 | } |
12794 | 0 | if (cancel) { |
12795 | 0 | cancel_refresh(zone); |
12796 | 0 | } |
12797 | 0 | if (do_queue_xfrin) { |
12798 | | /* Shows in the statistics channel the duration of the step. */ |
12799 | 0 | zone->xfrintime = isc_time_now(); |
12800 | 0 | } |
12801 | 0 | UNLOCK_ZONE(zone); |
12802 | 0 | if (do_queue_xfrin) { |
12803 | 0 | queue_xfrin(zone); |
12804 | 0 | } |
12805 | 0 | isc_rlevent_free(&sq->rlevent); |
12806 | 0 | isc_mem_put(zone->mctx, sq, sizeof(*sq)); |
12807 | 0 | dns_zone_idetach(&zone); |
12808 | 0 | return; |
12809 | | |
12810 | 0 | skip_primary: |
12811 | 0 | if (transport != NULL) { |
12812 | 0 | dns_transport_detach(&transport); |
12813 | 0 | } |
12814 | 0 | if (key != NULL) { |
12815 | 0 | dns_tsigkey_detach(&key); |
12816 | 0 | } |
12817 | 0 | if (message != NULL) { |
12818 | 0 | dns_message_detach(&message); |
12819 | 0 | } |
12820 | | /* |
12821 | | * Skip to next failed / untried primary. |
12822 | | */ |
12823 | 0 | dns_remote_next(&zone->primaries, true); |
12824 | 0 | if (!dns_remote_done(&zone->primaries)) { |
12825 | 0 | goto again; |
12826 | 0 | } |
12827 | 0 | dns_remote_reset(&zone->primaries, false); |
12828 | 0 | goto cleanup; |
12829 | 0 | } |
12830 | | |
12831 | | static void |
12832 | 0 | ns_query(dns_zone_t *zone, dns_rdataset_t *soardataset, dns_stub_t *stub) { |
12833 | 0 | isc_result_t result; |
12834 | 0 | dns_message_t *message = NULL; |
12835 | 0 | isc_netaddr_t primaryip; |
12836 | 0 | dns_tsigkey_t *key = NULL; |
12837 | 0 | dns_dbnode_t *node = NULL; |
12838 | 0 | bool have_xfrsource = false; |
12839 | 0 | bool reqnsid; |
12840 | 0 | uint16_t udpsize = SEND_BUFFER_SIZE; |
12841 | 0 | isc_sockaddr_t curraddr, sourceaddr; |
12842 | 0 | struct stub_cb_args *cb_args = NULL; |
12843 | |
|
12844 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
12845 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
12846 | 0 | REQUIRE((soardataset != NULL && stub == NULL) || |
12847 | 0 | (soardataset == NULL && stub != NULL)); |
12848 | 0 | REQUIRE(stub == NULL || DNS_STUB_VALID(stub)); |
12849 | |
|
12850 | 0 | ENTER; |
12851 | |
|
12852 | 0 | if (stub == NULL) { |
12853 | 0 | stub = isc_mem_get(zone->mctx, sizeof(*stub)); |
12854 | 0 | stub->magic = STUB_MAGIC; |
12855 | 0 | stub->mctx = zone->mctx; |
12856 | 0 | stub->zone = NULL; |
12857 | 0 | stub->db = NULL; |
12858 | 0 | stub->version = NULL; |
12859 | 0 | atomic_init(&stub->pending_requests, 0); |
12860 | | |
12861 | | /* |
12862 | | * Attach so that the zone won't disappear from under us. |
12863 | | */ |
12864 | 0 | zone_iattach(zone, &stub->zone); |
12865 | | |
12866 | | /* |
12867 | | * If a db exists we will update it, otherwise we create a |
12868 | | * new one and attach it to the zone once we have the NS |
12869 | | * RRset and glue. |
12870 | | */ |
12871 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
12872 | 0 | if (zone->db != NULL) { |
12873 | 0 | dns_db_attach(zone->db, &stub->db); |
12874 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
12875 | 0 | } else { |
12876 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
12877 | |
|
12878 | 0 | INSIST(zone->db_argc >= 1); |
12879 | 0 | result = dns_db_create(zone->mctx, zone->db_argv[0], |
12880 | 0 | &zone->origin, dns_dbtype_stub, |
12881 | 0 | zone->rdclass, zone->db_argc - 1, |
12882 | 0 | zone->db_argv + 1, &stub->db); |
12883 | 0 | if (result != ISC_R_SUCCESS) { |
12884 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
12885 | 0 | "refreshing stub: " |
12886 | 0 | "could not create " |
12887 | 0 | "database: %s", |
12888 | 0 | isc_result_totext(result)); |
12889 | 0 | goto cleanup; |
12890 | 0 | } |
12891 | 0 | dns_db_setmaxrrperset(stub->db, zone->maxrrperset); |
12892 | 0 | dns_db_setmaxtypepername(stub->db, |
12893 | 0 | zone->maxtypepername); |
12894 | 0 | } |
12895 | | |
12896 | 0 | result = dns_db_newversion(stub->db, &stub->version); |
12897 | 0 | if (result != ISC_R_SUCCESS) { |
12898 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
12899 | 0 | "refreshing stub: " |
12900 | 0 | "dns_db_newversion() failed: %s", |
12901 | 0 | isc_result_totext(result)); |
12902 | 0 | goto cleanup; |
12903 | 0 | } |
12904 | | |
12905 | | /* |
12906 | | * Update SOA record. |
12907 | | */ |
12908 | 0 | result = dns_db_findnode(stub->db, &zone->origin, true, &node); |
12909 | 0 | if (result != ISC_R_SUCCESS) { |
12910 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
12911 | 0 | "refreshing stub: " |
12912 | 0 | "dns_db_findnode() failed: %s", |
12913 | 0 | isc_result_totext(result)); |
12914 | 0 | goto cleanup; |
12915 | 0 | } |
12916 | | |
12917 | 0 | result = dns_db_addrdataset(stub->db, node, stub->version, 0, |
12918 | 0 | soardataset, 0, NULL); |
12919 | 0 | dns_db_detachnode(&node); |
12920 | 0 | if (result != ISC_R_SUCCESS) { |
12921 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
12922 | 0 | "refreshing stub: " |
12923 | 0 | "dns_db_addrdataset() failed: %s", |
12924 | 0 | isc_result_totext(result)); |
12925 | 0 | goto cleanup; |
12926 | 0 | } |
12927 | 0 | } |
12928 | | |
12929 | | /* |
12930 | | * XXX Optimisation: Create message when zone is setup and reuse. |
12931 | | */ |
12932 | 0 | create_query(zone, dns_rdatatype_ns, &zone->origin, &message); |
12933 | |
|
12934 | 0 | INSIST(dns_remote_count(&zone->primaries) > 0); |
12935 | 0 | INSIST(!dns_remote_done(&zone->primaries)); |
12936 | |
|
12937 | 0 | sourceaddr = dns_remote_sourceaddr(&zone->primaries); |
12938 | 0 | curraddr = dns_remote_curraddr(&zone->primaries); |
12939 | 0 | isc_netaddr_fromsockaddr(&primaryip, &curraddr); |
12940 | | /* |
12941 | | * First, look for a tsig key in the primaries statement, then |
12942 | | * try for a server key. |
12943 | | */ |
12944 | 0 | if (dns_remote_keyname(&zone->primaries) != NULL) { |
12945 | 0 | dns_view_t *view = dns_zone_getview(zone); |
12946 | 0 | dns_name_t *keyname = dns_remote_keyname(&zone->primaries); |
12947 | 0 | result = dns_view_gettsig(view, keyname, &key); |
12948 | 0 | if (result != ISC_R_SUCCESS) { |
12949 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
12950 | 0 | dns_name_format(keyname, namebuf, sizeof(namebuf)); |
12951 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
12952 | 0 | "unable to find key: %s", namebuf); |
12953 | 0 | } |
12954 | 0 | } |
12955 | 0 | if (key == NULL) { |
12956 | 0 | (void)dns_view_getpeertsig(zone->view, &primaryip, &key); |
12957 | 0 | } |
12958 | | |
12959 | | /* FIXME(OS): Do we need the transport here too? Most probably yes */ |
12960 | |
|
12961 | 0 | reqnsid = zone->view->requestnsid; |
12962 | 0 | if (zone->view->peers != NULL) { |
12963 | 0 | dns_peer_t *peer = NULL; |
12964 | 0 | bool edns; |
12965 | 0 | result = dns_peerlist_peerbyaddr(zone->view->peers, &primaryip, |
12966 | 0 | &peer); |
12967 | 0 | if (result == ISC_R_SUCCESS) { |
12968 | 0 | result = dns_peer_getsupportedns(peer, &edns); |
12969 | 0 | if (result == ISC_R_SUCCESS && !edns) { |
12970 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOEDNS); |
12971 | 0 | } |
12972 | 0 | result = dns_peer_gettransfersource(peer, |
12973 | 0 | &zone->sourceaddr); |
12974 | 0 | if (result == ISC_R_SUCCESS) { |
12975 | 0 | have_xfrsource = true; |
12976 | 0 | } |
12977 | 0 | udpsize = dns_view_getudpsize(zone->view); |
12978 | 0 | (void)dns_peer_getudpsize(peer, &udpsize); |
12979 | 0 | (void)dns_peer_getrequestnsid(peer, &reqnsid); |
12980 | 0 | } |
12981 | 0 | } |
12982 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOEDNS)) { |
12983 | 0 | result = add_opt(message, udpsize, reqnsid, false); |
12984 | 0 | if (result != ISC_R_SUCCESS) { |
12985 | 0 | zone_debuglog(zone, __func__, 1, |
12986 | 0 | "unable to add opt record: %s", |
12987 | 0 | isc_result_totext(result)); |
12988 | 0 | } |
12989 | 0 | } |
12990 | | |
12991 | | /* |
12992 | | * Always use TCP so that we shouldn't truncate in additional section. |
12993 | | */ |
12994 | 0 | switch (isc_sockaddr_pf(&curraddr)) { |
12995 | 0 | case PF_INET: |
12996 | 0 | if (!have_xfrsource) { |
12997 | 0 | isc_sockaddr_t any; |
12998 | 0 | isc_sockaddr_any(&any); |
12999 | |
|
13000 | 0 | zone->sourceaddr = sourceaddr; |
13001 | 0 | if (isc_sockaddr_equal(&zone->sourceaddr, &any)) { |
13002 | 0 | zone->sourceaddr = zone->xfrsource4; |
13003 | 0 | } |
13004 | 0 | } |
13005 | 0 | break; |
13006 | 0 | case PF_INET6: |
13007 | 0 | if (!have_xfrsource) { |
13008 | 0 | isc_sockaddr_t any; |
13009 | 0 | isc_sockaddr_any6(&any); |
13010 | |
|
13011 | 0 | zone->sourceaddr = sourceaddr; |
13012 | 0 | if (isc_sockaddr_equal(&zone->sourceaddr, &any)) { |
13013 | 0 | zone->sourceaddr = zone->xfrsource6; |
13014 | 0 | } |
13015 | 0 | } |
13016 | 0 | break; |
13017 | 0 | default: |
13018 | 0 | result = ISC_R_NOTIMPLEMENTED; |
13019 | 0 | POST(result); |
13020 | 0 | goto cleanup; |
13021 | 0 | } |
13022 | | |
13023 | | /* |
13024 | | * Save request parameters so we can reuse them later on |
13025 | | * for resolving missing glue A/AAAA records. |
13026 | | */ |
13027 | 0 | cb_args = isc_mem_get(zone->mctx, sizeof(*cb_args)); |
13028 | 0 | cb_args->stub = stub; |
13029 | 0 | cb_args->tsig_key = key; |
13030 | 0 | cb_args->udpsize = udpsize; |
13031 | 0 | cb_args->connect_timeout = isc_nm_getprimariestimeout() / MS_PER_SEC; |
13032 | 0 | cb_args->timeout = TCP_REQUEST_TIMEOUT; |
13033 | 0 | cb_args->reqnsid = reqnsid; |
13034 | |
|
13035 | 0 | result = dns_request_create( |
13036 | 0 | zone->view->requestmgr, message, &zone->sourceaddr, &curraddr, |
13037 | 0 | NULL, NULL, DNS_REQUESTOPT_TCP, key, cb_args->connect_timeout, |
13038 | 0 | cb_args->timeout, UDP_REQUEST_TIMEOUT, UDP_REQUEST_RETRIES, |
13039 | 0 | zone->loop, stub_callback, cb_args, &zone->request); |
13040 | 0 | if (result != ISC_R_SUCCESS) { |
13041 | 0 | zone_debuglog(zone, __func__, 1, |
13042 | 0 | "dns_request_create() failed: %s", |
13043 | 0 | isc_result_totext(result)); |
13044 | 0 | goto cleanup; |
13045 | 0 | } |
13046 | 0 | dns_message_detach(&message); |
13047 | 0 | goto unlock; |
13048 | | |
13049 | 0 | cleanup: |
13050 | 0 | cancel_refresh(zone); |
13051 | 0 | stub->magic = 0; |
13052 | 0 | if (stub->version != NULL) { |
13053 | 0 | dns_db_closeversion(stub->db, &stub->version, false); |
13054 | 0 | } |
13055 | 0 | if (stub->db != NULL) { |
13056 | 0 | dns_db_detach(&stub->db); |
13057 | 0 | } |
13058 | 0 | if (stub->zone != NULL) { |
13059 | 0 | zone_idetach(&stub->zone); |
13060 | 0 | } |
13061 | 0 | if (cb_args != NULL) { |
13062 | 0 | isc_mem_put(zone->mctx, cb_args, sizeof(*cb_args)); |
13063 | 0 | } |
13064 | 0 | isc_mem_put(stub->mctx, stub, sizeof(*stub)); |
13065 | 0 | if (message != NULL) { |
13066 | 0 | dns_message_detach(&message); |
13067 | 0 | } |
13068 | 0 | unlock: |
13069 | 0 | if (key != NULL) { |
13070 | 0 | dns_tsigkey_detach(&key); |
13071 | 0 | } |
13072 | 0 | return; |
13073 | 0 | } |
13074 | | |
13075 | | /* |
13076 | | * Shut the zone down. |
13077 | | */ |
13078 | | static void |
13079 | 0 | zone_shutdown(void *arg) { |
13080 | 0 | dns_zone_t *zone = (dns_zone_t *)arg; |
13081 | 0 | bool free_needed, linked = false; |
13082 | 0 | dns_zone_t *raw = NULL, *secure = NULL; |
13083 | 0 | dns_view_t *view = NULL, *prev_view = NULL; |
13084 | |
|
13085 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13086 | 0 | INSIST(isc_refcount_current(&zone->references) == 0); |
13087 | |
|
13088 | 0 | zone_debuglog(zone, __func__, 3, "shutting down"); |
13089 | | |
13090 | | /* |
13091 | | * If we were waiting for xfrin quota, step out of |
13092 | | * the queue. |
13093 | | * If there's no zone manager, we can't be waiting for the |
13094 | | * xfrin quota |
13095 | | */ |
13096 | 0 | if (zone->zmgr != NULL) { |
13097 | 0 | RWLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write); |
13098 | 0 | if (zone->statelist == &zone->zmgr->waiting_for_xfrin) { |
13099 | 0 | ISC_LIST_UNLINK(zone->zmgr->waiting_for_xfrin, zone, |
13100 | 0 | statelink); |
13101 | 0 | linked = true; |
13102 | 0 | zone->statelist = NULL; |
13103 | 0 | } |
13104 | 0 | if (zone->statelist == &zone->zmgr->xfrin_in_progress) { |
13105 | 0 | ISC_LIST_UNLINK(zone->zmgr->xfrin_in_progress, zone, |
13106 | 0 | statelink); |
13107 | 0 | zone->statelist = NULL; |
13108 | 0 | dns__zonemgr_resume_xfrs(zone->zmgr, false); |
13109 | 0 | } |
13110 | 0 | RWUNLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write); |
13111 | 0 | } |
13112 | | |
13113 | | /* |
13114 | | * In loop context, no locking required. See dns__zone_xfrdone(). |
13115 | | */ |
13116 | 0 | if (zone->xfr != NULL) { |
13117 | | /* The final detach will happen in dns__zone_xfrdone() */ |
13118 | 0 | dns_xfrin_shutdown(zone->xfr); |
13119 | 0 | } |
13120 | | |
13121 | | /* Safe to release the zone now */ |
13122 | 0 | if (zone->zmgr != NULL) { |
13123 | 0 | dns_zonemgr_releasezone(zone->zmgr, zone); |
13124 | 0 | } |
13125 | | |
13126 | | /* Detach the zone configuration pointer */ |
13127 | 0 | dns_zone_setcfg(zone, NULL); |
13128 | |
|
13129 | 0 | zone_clear_inline_sync(zone); |
13130 | |
|
13131 | 0 | LOCK_ZONE(zone); |
13132 | 0 | INSIST(zone != zone->raw); |
13133 | | |
13134 | | /* |
13135 | | * Detach the views early, we don't need them anymore. However, we need |
13136 | | * to detach them outside of the zone lock to break the lock loop |
13137 | | * between view, adb and zone locks. |
13138 | | */ |
13139 | 0 | view = zone->view; |
13140 | 0 | zone->view = NULL; |
13141 | 0 | prev_view = zone->prev_view; |
13142 | 0 | zone->prev_view = NULL; |
13143 | |
|
13144 | 0 | if (linked) { |
13145 | 0 | isc_refcount_decrement(&zone->irefs); |
13146 | 0 | } |
13147 | 0 | if (zone->request != NULL) { |
13148 | 0 | dns_request_cancel(zone->request); |
13149 | 0 | } |
13150 | |
|
13151 | 0 | if (zone->loadctx != NULL) { |
13152 | 0 | dns_loadctx_cancel(zone->loadctx); |
13153 | 0 | } |
13154 | |
|
13155 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FLUSH) || |
13156 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) |
13157 | 0 | { |
13158 | 0 | if (zone->dumpctx != NULL) { |
13159 | 0 | dns_dumpctx_cancel(zone->dumpctx); |
13160 | 0 | } |
13161 | 0 | } |
13162 | |
|
13163 | 0 | checkds_cancel(zone); |
13164 | |
|
13165 | 0 | dns_notify_cancel(&zone->notifysoa); |
13166 | 0 | dns_notify_cancel(&zone->notifycds); |
13167 | |
|
13168 | 0 | dns__zone_forward_cancel(zone); |
13169 | |
|
13170 | 0 | if (zone->timer != NULL) { |
13171 | 0 | isc_refcount_decrement(&zone->irefs); |
13172 | 0 | isc_timer_destroy(&zone->timer); |
13173 | 0 | } |
13174 | | |
13175 | | /* |
13176 | | * We have now canceled everything set the flag to allow |
13177 | | * dns__zone_free_check() to succeed. We must not unlock between |
13178 | | * setting this flag and calling dns__zone_free_check(). |
13179 | | */ |
13180 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_SHUTDOWN); |
13181 | 0 | free_needed = dns__zone_free_check(zone); |
13182 | | /* |
13183 | | * If a dump is in progress for the secure zone, defer detaching from |
13184 | | * the raw zone as it may prevent the unsigned serial number from being |
13185 | | * stored in the raw-format dump of the secure zone. In this scenario, |
13186 | | * dump_done() takes care of cleaning up the zone->raw reference. |
13187 | | */ |
13188 | 0 | if (dns__zone_inline_secure(zone) && |
13189 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) |
13190 | 0 | { |
13191 | 0 | raw = zone->raw; |
13192 | 0 | zone->raw = NULL; |
13193 | 0 | } |
13194 | 0 | if (dns__zone_inline_raw(zone)) { |
13195 | 0 | secure = zone->secure; |
13196 | 0 | zone->secure = NULL; |
13197 | 0 | } |
13198 | 0 | UNLOCK_ZONE(zone); |
13199 | |
|
13200 | 0 | if (view != NULL) { |
13201 | 0 | dns_view_weakdetach(&view); |
13202 | 0 | } |
13203 | 0 | if (prev_view != NULL) { |
13204 | 0 | dns_view_weakdetach(&prev_view); |
13205 | 0 | } |
13206 | |
|
13207 | 0 | if (raw != NULL) { |
13208 | 0 | dns_zone_detach(&raw); |
13209 | 0 | } |
13210 | 0 | if (secure != NULL) { |
13211 | 0 | dns_zone_idetach(&secure); |
13212 | 0 | } |
13213 | 0 | if (free_needed) { |
13214 | 0 | dns__zone_free(zone); |
13215 | 0 | } |
13216 | 0 | } |
13217 | | |
13218 | | static void |
13219 | 0 | zone_timer(void *arg) { |
13220 | 0 | dns_zone_t *zone = (dns_zone_t *)arg; |
13221 | |
|
13222 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13223 | |
|
13224 | 0 | zone_maintenance(zone); |
13225 | 0 | } |
13226 | | |
13227 | | static void |
13228 | 0 | zone_timer_stop(dns_zone_t *zone) { |
13229 | 0 | zone_debuglog(zone, __func__, 10, "stop zone timer"); |
13230 | 0 | if (zone->timer != NULL) { |
13231 | 0 | isc_timer_stop(zone->timer); |
13232 | 0 | } |
13233 | 0 | } |
13234 | | |
13235 | | static void |
13236 | 0 | zone_timer_set(dns_zone_t *zone, isc_time_t *next, isc_time_t *now) { |
13237 | 0 | isc_interval_t interval; |
13238 | |
|
13239 | 0 | if (isc_time_compare(next, now) <= 0) { |
13240 | 0 | isc_interval_set(&interval, 0, 0); |
13241 | 0 | } else { |
13242 | 0 | isc_time_subtract(next, now, &interval); |
13243 | 0 | } |
13244 | |
|
13245 | 0 | if (zone->loop == NULL) { |
13246 | 0 | zone_debuglog(zone, __func__, 10, "zone is not managed"); |
13247 | 0 | } else if (zone->timer == NULL) { |
13248 | 0 | isc_refcount_increment0(&zone->irefs); |
13249 | 0 | isc_timer_create(zone->loop, zone_timer, zone, &zone->timer); |
13250 | 0 | } |
13251 | 0 | if (zone->timer != NULL) { |
13252 | 0 | isc_timer_start(zone->timer, isc_timertype_once, &interval); |
13253 | 0 | } |
13254 | 0 | } |
13255 | | |
13256 | | static void |
13257 | 0 | zone__settimer(void *arg) { |
13258 | 0 | zone_settimer_t *data = arg; |
13259 | 0 | dns_zone_t *zone = data->zone; |
13260 | 0 | isc_time_t now = data->now; |
13261 | 0 | isc_time_t next; |
13262 | 0 | bool free_needed = false; |
13263 | |
|
13264 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13265 | 0 | ENTER; |
13266 | |
|
13267 | 0 | LOCK_ZONE(zone); |
13268 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
13269 | 0 | goto free; |
13270 | 0 | } |
13271 | 0 | isc_time_settoepoch(&next); |
13272 | |
|
13273 | 0 | switch (zone->type) { |
13274 | 0 | case dns_zone_redirect: |
13275 | 0 | if (dns_remote_addresses(&zone->primaries) != NULL) { |
13276 | 0 | goto treat_as_secondary; |
13277 | 0 | } |
13278 | 0 | FALLTHROUGH; |
13279 | 0 | case dns_zone_primary: |
13280 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY) || |
13281 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDSTARTUPNOTIFY)) |
13282 | 0 | { |
13283 | 0 | next = zone->notifytime; |
13284 | 0 | } |
13285 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) && |
13286 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) |
13287 | 0 | { |
13288 | 0 | INSIST(!isc_time_isepoch(&zone->dumptime)); |
13289 | 0 | next = time_min(next, zone->dumptime); |
13290 | 0 | } |
13291 | 0 | if (zone->type == dns_zone_redirect) { |
13292 | 0 | break; |
13293 | 0 | } |
13294 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESHING)) { |
13295 | 0 | next = time_min(next, zone->refreshkeytime); |
13296 | 0 | } |
13297 | 0 | next = time_min(next, zone->resigntime); |
13298 | 0 | next = time_min(next, zone->keywarntime); |
13299 | 0 | next = time_min(next, zone->signingtime); |
13300 | 0 | if (zone_inline_sync_pending(zone)) { |
13301 | 0 | next = time_min(next, now); |
13302 | 0 | } |
13303 | 0 | if (zone_maintenance_request_pending(zone)) { |
13304 | 0 | next = time_min(next, now); |
13305 | 0 | } |
13306 | 0 | next = time_min(next, zone->nsec3chaintime); |
13307 | 0 | break; |
13308 | | |
13309 | 0 | case dns_zone_secondary: |
13310 | 0 | case dns_zone_mirror: |
13311 | 0 | treat_as_secondary: |
13312 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDNOTIFY) || |
13313 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDSTARTUPNOTIFY)) |
13314 | 0 | { |
13315 | 0 | next = zone->notifytime; |
13316 | 0 | } |
13317 | 0 | FALLTHROUGH; |
13318 | 0 | case dns_zone_stub: |
13319 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH) && |
13320 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NOPRIMARIES) && |
13321 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADING) && |
13322 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING)) |
13323 | 0 | { |
13324 | 0 | next = time_min(next, zone->refreshtime); |
13325 | 0 | } |
13326 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) { |
13327 | 0 | next = time_min(next, zone->expiretime); |
13328 | 0 | } |
13329 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) && |
13330 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) |
13331 | 0 | { |
13332 | 0 | INSIST(!isc_time_isepoch(&zone->dumptime)); |
13333 | 0 | next = time_min(next, zone->dumptime); |
13334 | 0 | } |
13335 | 0 | break; |
13336 | | |
13337 | 0 | case dns_zone_key: |
13338 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDDUMP) && |
13339 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_DUMPING)) |
13340 | 0 | { |
13341 | 0 | INSIST(!isc_time_isepoch(&zone->dumptime)); |
13342 | 0 | next = time_min(next, zone->dumptime); |
13343 | 0 | } |
13344 | 0 | if (!DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESHING)) { |
13345 | 0 | next = time_min(next, zone->refreshkeytime); |
13346 | 0 | } |
13347 | 0 | break; |
13348 | | |
13349 | 0 | default: |
13350 | 0 | break; |
13351 | 0 | } |
13352 | | |
13353 | 0 | if (isc_time_isepoch(&next)) { |
13354 | 0 | zone_timer_stop(zone); |
13355 | 0 | } else { |
13356 | 0 | zone_timer_set(zone, &next, &now); |
13357 | 0 | } |
13358 | |
|
13359 | 0 | free: |
13360 | 0 | isc_mem_put(zone->mctx, data, sizeof(*data)); |
13361 | 0 | isc_refcount_decrement(&zone->irefs); |
13362 | 0 | free_needed = dns__zone_free_check(zone); |
13363 | 0 | UNLOCK_ZONE(zone); |
13364 | 0 | if (free_needed) { |
13365 | 0 | dns__zone_free(zone); |
13366 | 0 | } |
13367 | 0 | } |
13368 | | |
13369 | | void |
13370 | 0 | dns__zone_settimer(dns_zone_t *zone, isc_time_t now) { |
13371 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
13372 | 0 | return; |
13373 | 0 | } |
13374 | | |
13375 | 0 | zone_settimer_t *arg = isc_mem_get(zone->mctx, sizeof(*arg)); |
13376 | 0 | *arg = (zone_settimer_t){ |
13377 | 0 | .zone = zone, |
13378 | 0 | .now = now, |
13379 | 0 | }; |
13380 | 0 | isc_refcount_increment0(&zone->irefs); |
13381 | 0 | isc_async_run(zone->loop, zone__settimer, arg); |
13382 | 0 | } |
13383 | | |
13384 | | static void |
13385 | 0 | cancel_refresh(dns_zone_t *zone) { |
13386 | | /* |
13387 | | * 'zone' locked by caller. |
13388 | | */ |
13389 | |
|
13390 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13391 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
13392 | |
|
13393 | 0 | ENTER; |
13394 | |
|
13395 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH); |
13396 | 0 | dns__zone_settimer(zone, isc_time_now()); |
13397 | 0 | } |
13398 | | |
13399 | | isc_result_t |
13400 | | dns_zone_notifyreceive(dns_zone_t *zone, isc_sockaddr_t *from, |
13401 | 0 | isc_sockaddr_t *to, dns_message_t *msg) { |
13402 | 0 | unsigned int i; |
13403 | 0 | dns_rdata_soa_t soa; |
13404 | 0 | dns_rdataset_t *rdataset = NULL; |
13405 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
13406 | 0 | isc_result_t result; |
13407 | 0 | char fromtext[ISC_SOCKADDR_FORMATSIZE]; |
13408 | 0 | int match = 0; |
13409 | 0 | isc_netaddr_t netaddr; |
13410 | 0 | uint32_t serial = 0; |
13411 | 0 | bool have_serial = false; |
13412 | 0 | dns_tsigkey_t *tsigkey; |
13413 | 0 | const dns_name_t *tsig; |
13414 | |
|
13415 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13416 | | |
13417 | | /* |
13418 | | * If type != T_SOA return DNS_R_NOTIMP. We don't yet support |
13419 | | * ROLLOVER. |
13420 | | * |
13421 | | * SOA: RFC1996 |
13422 | | * Check that 'from' is a valid notify source, (zone->primaries). |
13423 | | * Return DNS_R_REFUSED if not. |
13424 | | * |
13425 | | * If the notify message contains a serial number check it |
13426 | | * against the zones serial and return if <= current serial |
13427 | | * |
13428 | | * If a refresh check is progress, if so just record the |
13429 | | * fact we received a NOTIFY and from where and return. |
13430 | | * We will perform a new refresh check when the current one |
13431 | | * completes. Return ISC_R_SUCCESS. |
13432 | | * |
13433 | | * Otherwise initiate a refresh check using 'from' as the |
13434 | | * first address to check. Return ISC_R_SUCCESS. |
13435 | | */ |
13436 | |
|
13437 | 0 | isc_sockaddr_format(from, fromtext, sizeof(fromtext)); |
13438 | | |
13439 | | /* |
13440 | | * Notify messages are processed by the raw zone. |
13441 | | */ |
13442 | 0 | LOCK_ZONE(zone); |
13443 | 0 | INSIST(zone != zone->raw); |
13444 | 0 | if (dns__zone_inline_secure(zone)) { |
13445 | 0 | result = dns_zone_notifyreceive(zone->raw, from, to, msg); |
13446 | 0 | UNLOCK_ZONE(zone); |
13447 | 0 | return result; |
13448 | 0 | } |
13449 | | /* |
13450 | | * We only handle NOTIFY (SOA) at the present. |
13451 | | */ |
13452 | 0 | if (isc_sockaddr_pf(from) == PF_INET) { |
13453 | 0 | dns__zone_stats_increment(zone, |
13454 | 0 | dns_zonestatscounter_notifyinv4); |
13455 | 0 | } else { |
13456 | 0 | dns__zone_stats_increment(zone, |
13457 | 0 | dns_zonestatscounter_notifyinv6); |
13458 | 0 | } |
13459 | 0 | if (msg->counts[DNS_SECTION_QUESTION] == 0 || |
13460 | 0 | dns_message_findname(msg, DNS_SECTION_QUESTION, &zone->origin, |
13461 | 0 | dns_rdatatype_soa, dns_rdatatype_none, NULL, |
13462 | 0 | NULL) != ISC_R_SUCCESS) |
13463 | 0 | { |
13464 | 0 | UNLOCK_ZONE(zone); |
13465 | 0 | if (msg->counts[DNS_SECTION_QUESTION] == 0) { |
13466 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
13467 | 0 | ISC_LOG_NOTICE, |
13468 | 0 | "NOTIFY with no question " |
13469 | 0 | "section from: %s", |
13470 | 0 | fromtext); |
13471 | 0 | return DNS_R_FORMERR; |
13472 | 0 | } |
13473 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_NOTICE, |
13474 | 0 | "NOTIFY zone does not match"); |
13475 | 0 | return DNS_R_NOTIMP; |
13476 | 0 | } |
13477 | | |
13478 | | /* |
13479 | | * If we are a primary zone just succeed. |
13480 | | */ |
13481 | 0 | if (zone->type == dns_zone_primary) { |
13482 | 0 | UNLOCK_ZONE(zone); |
13483 | 0 | return ISC_R_SUCCESS; |
13484 | 0 | } |
13485 | | |
13486 | 0 | isc_netaddr_fromsockaddr(&netaddr, from); |
13487 | 0 | for (i = 0; i < dns_remote_count(&zone->primaries); i++) { |
13488 | 0 | isc_sockaddr_t sockaddr = dns_remote_addr(&zone->primaries, i); |
13489 | 0 | if (isc_sockaddr_eqaddr(from, &sockaddr)) { |
13490 | 0 | break; |
13491 | 0 | } |
13492 | 0 | if (zone->view->aclenv->match_mapped && |
13493 | 0 | IN6_IS_ADDR_V4MAPPED(&from->type.sin6.sin6_addr) && |
13494 | 0 | isc_sockaddr_pf(&sockaddr) == AF_INET) |
13495 | 0 | { |
13496 | 0 | isc_netaddr_t na1, na2; |
13497 | 0 | isc_netaddr_fromv4mapped(&na1, &netaddr); |
13498 | 0 | isc_netaddr_fromsockaddr(&na2, &sockaddr); |
13499 | 0 | if (isc_netaddr_equal(&na1, &na2)) { |
13500 | 0 | break; |
13501 | 0 | } |
13502 | 0 | } |
13503 | 0 | } |
13504 | | |
13505 | | /* |
13506 | | * Accept notify requests from non primaries if they are on |
13507 | | * 'zone->notifysoa.notify_acl'. |
13508 | | */ |
13509 | 0 | tsigkey = dns_message_gettsigkey(msg); |
13510 | 0 | tsig = dns_tsigkey_identity(tsigkey); |
13511 | 0 | if (i >= dns_remote_count(&zone->primaries) && |
13512 | 0 | zone->notifysoa.notify_acl != NULL && |
13513 | 0 | (dns_acl_match(&netaddr, tsig, zone->notifysoa.notify_acl, |
13514 | 0 | zone->view->aclenv, &match, |
13515 | 0 | NULL) == ISC_R_SUCCESS) && |
13516 | 0 | match > 0) |
13517 | 0 | { |
13518 | | /* Accept notify. */ |
13519 | 0 | } else if (i >= dns_remote_count(&zone->primaries)) { |
13520 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
13521 | 0 | "refused notify from non-primary: %s", fromtext); |
13522 | 0 | dns__zone_stats_increment(zone, dns_zonestatscounter_notifyrej); |
13523 | 0 | UNLOCK_ZONE(zone); |
13524 | 0 | return DNS_R_REFUSED; |
13525 | 0 | } |
13526 | | |
13527 | | /* |
13528 | | * If the zone is loaded and there are answers check the serial |
13529 | | * to see if we need to do a refresh. |
13530 | | */ |
13531 | 0 | if (msg->counts[DNS_SECTION_ANSWER] > 0 && |
13532 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED)) |
13533 | 0 | { |
13534 | 0 | result = dns_message_findname( |
13535 | 0 | msg, DNS_SECTION_ANSWER, &zone->origin, |
13536 | 0 | dns_rdatatype_soa, dns_rdatatype_none, NULL, &rdataset); |
13537 | 0 | if (result == ISC_R_SUCCESS) { |
13538 | 0 | result = dns_rdataset_first(rdataset); |
13539 | 0 | } |
13540 | 0 | if (result == ISC_R_SUCCESS) { |
13541 | 0 | uint32_t oldserial; |
13542 | 0 | unsigned int soacount; |
13543 | |
|
13544 | 0 | dns_rdataset_current(rdataset, &rdata); |
13545 | 0 | result = dns_rdata_tostruct(&rdata, &soa, NULL); |
13546 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
13547 | 0 | serial = soa.serial; |
13548 | 0 | have_serial = true; |
13549 | | /* |
13550 | | * The following should safely be performed without DB |
13551 | | * lock and succeed in this context. |
13552 | | */ |
13553 | 0 | result = zone_get_from_db(zone, zone->db, NULL, |
13554 | 0 | &soacount, NULL, &oldserial, |
13555 | 0 | NULL, NULL, NULL, NULL, NULL); |
13556 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
13557 | 0 | RUNTIME_CHECK(soacount > 0U); |
13558 | 0 | if (isc_serial_le(serial, oldserial)) { |
13559 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
13560 | 0 | ISC_LOG_INFO, |
13561 | 0 | "notify from %s: " |
13562 | 0 | "zone is up to date", |
13563 | 0 | fromtext); |
13564 | 0 | UNLOCK_ZONE(zone); |
13565 | 0 | return ISC_R_SUCCESS; |
13566 | 0 | } |
13567 | 0 | } |
13568 | 0 | } |
13569 | | |
13570 | | /* |
13571 | | * If we got this far and there was a refresh in progress just |
13572 | | * let it complete. Record where we got the notify from so we |
13573 | | * can perform a refresh check when the current one completes |
13574 | | */ |
13575 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH)) { |
13576 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDREFRESH); |
13577 | 0 | zone->notifysoa.notifyfrom = *from; |
13578 | 0 | UNLOCK_ZONE(zone); |
13579 | 0 | if (have_serial) { |
13580 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
13581 | 0 | ISC_LOG_INFO, |
13582 | 0 | "notify from %s: " |
13583 | 0 | "serial %u: refresh in progress, " |
13584 | 0 | "refresh check queued", |
13585 | 0 | fromtext, serial); |
13586 | 0 | } else { |
13587 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
13588 | 0 | ISC_LOG_INFO, |
13589 | 0 | "notify from %s: " |
13590 | 0 | "refresh in progress, " |
13591 | 0 | "refresh check queued", |
13592 | 0 | fromtext); |
13593 | 0 | } |
13594 | 0 | return ISC_R_SUCCESS; |
13595 | 0 | } |
13596 | 0 | if (have_serial) { |
13597 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
13598 | 0 | "notify from %s: serial %u", fromtext, serial); |
13599 | 0 | } else { |
13600 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
13601 | 0 | "notify from %s: no serial", fromtext); |
13602 | 0 | } |
13603 | 0 | zone->notifysoa.notifyfrom = *from; |
13604 | 0 | UNLOCK_ZONE(zone); |
13605 | |
|
13606 | 0 | if (to != NULL) { |
13607 | 0 | dns_unreachcache_remove(zone->view->unreachcache, from, to); |
13608 | 0 | } |
13609 | 0 | dns_zone_refresh(zone); |
13610 | 0 | return ISC_R_SUCCESS; |
13611 | 0 | } |
13612 | | |
13613 | | void |
13614 | | dns_zone_logv(dns_zone_t *zone, isc_logcategory_t category, int level, |
13615 | 16 | const char *prefix, const char *fmt, va_list ap) { |
13616 | 16 | char message[4096]; |
13617 | 16 | const char *zstr; |
13618 | | |
13619 | 16 | REQUIRE(DNS_ZONE_VALID(zone)); |
13620 | | |
13621 | 16 | if (!isc_log_wouldlog(level)) { |
13622 | 14 | return; |
13623 | 14 | } |
13624 | | |
13625 | 2 | vsnprintf(message, sizeof(message), fmt, ap); |
13626 | | |
13627 | 2 | switch (zone->type) { |
13628 | 0 | case dns_zone_key: |
13629 | 0 | zstr = "managed-keys-zone"; |
13630 | 0 | break; |
13631 | 0 | case dns_zone_redirect: |
13632 | 0 | zstr = "redirect-zone"; |
13633 | 0 | break; |
13634 | 2 | default: |
13635 | 2 | zstr = "zone "; |
13636 | 2 | } |
13637 | | |
13638 | 2 | isc_log_write(category, DNS_LOGMODULE_ZONE, level, "%s%s%s%s: %s", |
13639 | 2 | prefix != NULL ? prefix : "", prefix != NULL ? ": " : "", |
13640 | 2 | zstr, zone->strnamerd, message); |
13641 | 2 | } |
13642 | | |
13643 | | void |
13644 | | dns_zone_logc(dns_zone_t *zone, isc_logcategory_t category, int level, |
13645 | 10 | const char *fmt, ...) { |
13646 | 10 | va_list ap; |
13647 | | |
13648 | 10 | va_start(ap, fmt); |
13649 | 10 | dns_zone_logv(zone, category, level, NULL, fmt, ap); |
13650 | 10 | va_end(ap); |
13651 | 10 | } |
13652 | | |
13653 | | void |
13654 | 0 | dns_zone_log(dns_zone_t *zone, int level, const char *fmt, ...) { |
13655 | 0 | va_list ap; |
13656 | |
|
13657 | 0 | va_start(ap, fmt); |
13658 | 0 | dns_zone_logv(zone, DNS_LOGCATEGORY_GENERAL, level, NULL, fmt, ap); |
13659 | 0 | va_end(ap); |
13660 | 0 | } |
13661 | | |
13662 | | static void |
13663 | | zone_debuglogc(dns_zone_t *zone, isc_logcategory_t category, const char *me, |
13664 | 0 | int debuglevel, const char *fmt, ...) { |
13665 | 0 | int level = ISC_LOG_DEBUG(debuglevel); |
13666 | 0 | va_list ap; |
13667 | |
|
13668 | 0 | va_start(ap, fmt); |
13669 | 0 | dns_zone_logv(zone, category, level, me, fmt, ap); |
13670 | 0 | va_end(ap); |
13671 | 0 | } |
13672 | | |
13673 | | static void |
13674 | | zone_debuglog(dns_zone_t *zone, const char *me, int debuglevel, const char *fmt, |
13675 | 6 | ...) { |
13676 | 6 | int level = ISC_LOG_DEBUG(debuglevel); |
13677 | 6 | va_list ap; |
13678 | | |
13679 | 6 | va_start(ap, fmt); |
13680 | 6 | dns_zone_logv(zone, DNS_LOGCATEGORY_GENERAL, level, me, fmt, ap); |
13681 | 6 | va_end(ap); |
13682 | 6 | } |
13683 | | |
13684 | | static void |
13685 | 0 | dnssec_log(dns_zone_t *zone, int level, const char *fmt, ...) { |
13686 | 0 | va_list ap; |
13687 | |
|
13688 | 0 | va_start(ap, fmt); |
13689 | 0 | dns_zone_logv(zone, DNS_LOGCATEGORY_DNSSEC, level, NULL, fmt, ap); |
13690 | 0 | va_end(ap); |
13691 | 0 | } |
13692 | | |
13693 | | static int |
13694 | 0 | message_count(dns_message_t *msg, dns_section_t section, dns_rdatatype_t type) { |
13695 | 0 | int count = 0; |
13696 | |
|
13697 | 0 | MSG_SECTION_FOREACH(msg, section, name) { |
13698 | 0 | ISC_LIST_FOREACH_REV(name->list, curr, link) { |
13699 | 0 | if (curr->type == type) { |
13700 | 0 | count++; |
13701 | 0 | } |
13702 | 0 | } |
13703 | 0 | } |
13704 | |
|
13705 | 0 | return count; |
13706 | 0 | } |
13707 | | |
13708 | | const char * |
13709 | 2 | dns_zonetype_name(dns_zonetype_t type) { |
13710 | 2 | switch (type) { |
13711 | 0 | case dns_zone_none: |
13712 | 0 | return "none"; |
13713 | 2 | case dns_zone_primary: |
13714 | 2 | return "primary"; |
13715 | 0 | case dns_zone_secondary: |
13716 | 0 | return "secondary"; |
13717 | 0 | case dns_zone_mirror: |
13718 | 0 | return "mirror"; |
13719 | 0 | case dns_zone_stub: |
13720 | 0 | return "stub"; |
13721 | 0 | case dns_zone_staticstub: |
13722 | 0 | return "static-stub"; |
13723 | 0 | case dns_zone_key: |
13724 | 0 | return "key"; |
13725 | 0 | case dns_zone_dlz: |
13726 | 0 | return "dlz"; |
13727 | 0 | case dns_zone_redirect: |
13728 | 0 | return "redirect"; |
13729 | 0 | default: |
13730 | 0 | return "unknown"; |
13731 | 2 | } |
13732 | 2 | } |
13733 | | |
13734 | | dns_zonetype_t |
13735 | 0 | dns_zone_getredirecttype(dns_zone_t *zone) { |
13736 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13737 | 0 | REQUIRE(zone->type == dns_zone_redirect); |
13738 | |
|
13739 | 0 | return dns_remote_addresses(&zone->primaries) == NULL |
13740 | 0 | ? dns_zone_primary |
13741 | 0 | : dns_zone_secondary; |
13742 | 0 | } |
13743 | | |
13744 | | dns_notifyctx_t * |
13745 | 0 | dns__zone_getnotifyctx(dns_zone_t *zone, dns_rdatatype_t type) { |
13746 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13747 | |
|
13748 | 0 | switch (type) { |
13749 | 0 | case dns_rdatatype_soa: |
13750 | 0 | return &zone->notifysoa; |
13751 | 0 | case dns_rdatatype_cds: |
13752 | 0 | return &zone->notifycds; |
13753 | 0 | default: |
13754 | 0 | UNREACHABLE(); |
13755 | 0 | } |
13756 | 0 | return NULL; |
13757 | 0 | } |
13758 | | |
13759 | | void |
13760 | 0 | dns__zone_stats_increment(dns_zone_t *zone, isc_statscounter_t counter) { |
13761 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13762 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
13763 | 0 | inc_stats(zone, counter); |
13764 | 0 | } |
13765 | | |
13766 | | void |
13767 | 0 | dns__zone_lock(dns_zone_t *zone) { |
13768 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13769 | 0 | LOCK_ZONE(zone); |
13770 | 0 | } |
13771 | | |
13772 | | void |
13773 | 0 | dns__zone_unlock(dns_zone_t *zone) { |
13774 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13775 | 0 | UNLOCK_ZONE(zone); |
13776 | 0 | } |
13777 | | |
13778 | | bool |
13779 | 0 | dns__zone_locked(dns_zone_t *zone) { |
13780 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13781 | 0 | return LOCKED_ZONE(zone); |
13782 | 0 | } |
13783 | | |
13784 | | bool |
13785 | 0 | dns__zone_loaded(dns_zone_t *zone) { |
13786 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13787 | 0 | return DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) != 0; |
13788 | 0 | } |
13789 | | |
13790 | | bool |
13791 | 0 | dns__zone_exiting(dns_zone_t *zone) { |
13792 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13793 | 0 | return DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING) != 0; |
13794 | 0 | } |
13795 | | |
13796 | | static void |
13797 | 0 | update_log_cb(void *arg, dns_zone_t *zone, int level, const char *message) { |
13798 | 0 | UNUSED(arg); |
13799 | 0 | dns_zone_log(zone, level, "%s", message); |
13800 | 0 | } |
13801 | | |
13802 | | static isc_result_t |
13803 | | dnskey_inuse(dns_zone_t *zone, dns_rdata_t *rdata, isc_mem_t *mctx, |
13804 | 0 | dns_dnsseckeylist_t *keylist, bool *inuse) { |
13805 | 0 | isc_result_t result; |
13806 | 0 | dst_key_t *dstkey = NULL; |
13807 | |
|
13808 | 0 | result = dns_dnssec_keyfromrdata(dns_zone_getorigin(zone), rdata, mctx, |
13809 | 0 | &dstkey); |
13810 | 0 | if (result != ISC_R_SUCCESS) { |
13811 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
13812 | 0 | "dns_dnssec_keyfromrdata() failed: %s", |
13813 | 0 | isc_result_totext(result)); |
13814 | 0 | return result; |
13815 | 0 | } |
13816 | | |
13817 | 0 | ISC_LIST_FOREACH(*keylist, k, link) { |
13818 | 0 | if (dst_key_pubcompare(k->key, dstkey, false)) { |
13819 | 0 | *inuse = true; |
13820 | 0 | break; |
13821 | 0 | } |
13822 | 0 | } |
13823 | |
|
13824 | 0 | dst_key_free(&dstkey); |
13825 | 0 | return ISC_R_SUCCESS; |
13826 | 0 | } |
13827 | | |
13828 | | static isc_result_t |
13829 | | cdnskey_inuse(dns_zone_t *zone, dns_rdata_t *rdata, |
13830 | 0 | dns_dnsseckeylist_t *keylist, bool *inuse) { |
13831 | 0 | isc_result_t result; |
13832 | 0 | dns_rdata_cdnskey_t cdnskey; |
13833 | |
|
13834 | 0 | result = dns_rdata_tostruct(rdata, &cdnskey, NULL); |
13835 | 0 | if (result != ISC_R_SUCCESS) { |
13836 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
13837 | 0 | "dns_rdata_tostruct(cdnskey) failed: %s", |
13838 | 0 | isc_result_totext(result)); |
13839 | 0 | return result; |
13840 | 0 | } |
13841 | | |
13842 | 0 | ISC_LIST_FOREACH(*keylist, k, link) { |
13843 | 0 | dns_rdata_t cdnskeyrdata = DNS_RDATA_INIT; |
13844 | 0 | unsigned char keybuf[DNS_RDATA_MAXLENGTH]; |
13845 | |
|
13846 | 0 | result = dns_dnssec_make_dnskey(k->key, keybuf, sizeof(keybuf), |
13847 | 0 | &cdnskeyrdata); |
13848 | 0 | if (result != ISC_R_SUCCESS) { |
13849 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
13850 | 0 | "dns_dnssec_make_dnskey() failed: %s", |
13851 | 0 | isc_result_totext(result)); |
13852 | 0 | return result; |
13853 | 0 | } |
13854 | | |
13855 | 0 | cdnskeyrdata.type = dns_rdatatype_cdnskey; |
13856 | 0 | if (dns_rdata_compare(rdata, &cdnskeyrdata) == 0) { |
13857 | 0 | *inuse = true; |
13858 | 0 | break; |
13859 | 0 | } |
13860 | 0 | } |
13861 | | |
13862 | 0 | return ISC_R_SUCCESS; |
13863 | 0 | } |
13864 | | |
13865 | | static isc_result_t |
13866 | | cds_inuse(dns_zone_t *zone, dns_rdata_t *rdata, dns_dnsseckeylist_t *keylist, |
13867 | 0 | bool *inuse) { |
13868 | 0 | isc_result_t result; |
13869 | 0 | dns_rdata_ds_t cds; |
13870 | |
|
13871 | 0 | result = dns_rdata_tostruct(rdata, &cds, NULL); |
13872 | 0 | if (result != ISC_R_SUCCESS) { |
13873 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
13874 | 0 | "dns_rdata_tostruct(cds) failed: %s", |
13875 | 0 | isc_result_totext(result)); |
13876 | 0 | return result; |
13877 | 0 | } |
13878 | | |
13879 | 0 | ISC_LIST_FOREACH(*keylist, k, link) { |
13880 | 0 | dns_rdata_t dnskey = DNS_RDATA_INIT; |
13881 | 0 | dns_rdata_t cdsrdata = DNS_RDATA_INIT; |
13882 | 0 | unsigned char keybuf[DNS_RDATA_MAXLENGTH]; |
13883 | 0 | unsigned char cdsbuf[DNS_DS_BUFFERSIZE]; |
13884 | |
|
13885 | 0 | if (dst_key_id(k->key) != cds.key_tag || |
13886 | 0 | dst_algorithm_tosecalg(dst_key_alg(k->key)) != |
13887 | 0 | cds.algorithm) |
13888 | 0 | { |
13889 | 0 | continue; |
13890 | 0 | } |
13891 | 0 | result = dns_dnssec_make_dnskey(k->key, keybuf, sizeof(keybuf), |
13892 | 0 | &dnskey); |
13893 | 0 | if (result != ISC_R_SUCCESS) { |
13894 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
13895 | 0 | "dns_dnssec_make_dnskey() failed: %s", |
13896 | 0 | isc_result_totext(result)); |
13897 | 0 | return result; |
13898 | 0 | } |
13899 | 0 | result = dns_ds_buildrdata(dns_zone_getorigin(zone), &dnskey, |
13900 | 0 | cds.digest_type, cdsbuf, |
13901 | 0 | sizeof(cdsbuf), &cdsrdata); |
13902 | 0 | if (result != ISC_R_SUCCESS) { |
13903 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
13904 | 0 | "dns_ds_buildrdata(keytag=%d, algo=%d, " |
13905 | 0 | "digest=%d) failed: %s", |
13906 | 0 | cds.key_tag, cds.algorithm, |
13907 | 0 | cds.digest_type, |
13908 | 0 | isc_result_totext(result)); |
13909 | 0 | return result; |
13910 | 0 | } |
13911 | | |
13912 | 0 | cdsrdata.type = dns_rdatatype_cds; |
13913 | 0 | if (dns_rdata_compare(rdata, &cdsrdata) == 0) { |
13914 | 0 | *inuse = true; |
13915 | 0 | break; |
13916 | 0 | } |
13917 | 0 | } |
13918 | | |
13919 | 0 | return ISC_R_SUCCESS; |
13920 | 0 | } |
13921 | | |
13922 | | isc_result_t |
13923 | 0 | dns_zone_dnskey_inuse(dns_zone_t *zone, dns_rdata_t *rdata, bool *inuse) { |
13924 | 0 | dns_dnsseckeylist_t keylist; |
13925 | 0 | isc_result_t result = ISC_R_SUCCESS; |
13926 | 0 | isc_stdtime_t now = isc_stdtime_now(); |
13927 | 0 | isc_mem_t *mctx; |
13928 | 0 | dns_kasp_t *kasp; |
13929 | 0 | dns_keystorelist_t *keystores; |
13930 | 0 | const char *keydir; |
13931 | |
|
13932 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
13933 | 0 | REQUIRE(dns_rdatatype_iskeymaterial(rdata->type)); |
13934 | |
|
13935 | 0 | mctx = zone->mctx; |
13936 | |
|
13937 | 0 | ISC_LIST_INIT(keylist); |
13938 | |
|
13939 | 0 | *inuse = false; |
13940 | |
|
13941 | 0 | kasp = dns_zone_getkasp(zone); |
13942 | 0 | keydir = dns_zone_getkeydirectory(zone); |
13943 | 0 | keystores = dns_zone_getkeystores(zone); |
13944 | |
|
13945 | 0 | if (kasp == NULL) { |
13946 | 0 | return ISC_R_SUCCESS; |
13947 | 0 | } |
13948 | | |
13949 | 0 | dns_zone_lock_keyfiles(zone); |
13950 | 0 | result = dns_dnssec_findmatchingkeys(dns_zone_getorigin(zone), kasp, |
13951 | 0 | keydir, keystores, now, false, |
13952 | 0 | mctx, &keylist); |
13953 | 0 | dns_zone_unlock_keyfiles(zone); |
13954 | 0 | if (result == ISC_R_NOTFOUND) { |
13955 | 0 | return ISC_R_SUCCESS; |
13956 | 0 | } else if (result != ISC_R_SUCCESS) { |
13957 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
13958 | 0 | "dns_dnssec_findmatchingkeys() failed: %s", |
13959 | 0 | isc_result_totext(result)); |
13960 | 0 | return result; |
13961 | 0 | } |
13962 | | |
13963 | 0 | switch (rdata->type) { |
13964 | 0 | case dns_rdatatype_dnskey: |
13965 | 0 | result = dnskey_inuse(zone, rdata, mctx, &keylist, inuse); |
13966 | 0 | break; |
13967 | 0 | case dns_rdatatype_cdnskey: |
13968 | 0 | result = cdnskey_inuse(zone, rdata, &keylist, inuse); |
13969 | 0 | break; |
13970 | 0 | case dns_rdatatype_cds: |
13971 | 0 | result = cds_inuse(zone, rdata, &keylist, inuse); |
13972 | 0 | break; |
13973 | 0 | default: |
13974 | 0 | UNREACHABLE(); |
13975 | 0 | break; |
13976 | 0 | } |
13977 | | |
13978 | 0 | ISC_LIST_FOREACH(keylist, key, link) { |
13979 | 0 | ISC_LIST_UNLINK(keylist, key, link); |
13980 | 0 | dns_dnsseckey_destroy(mctx, &key); |
13981 | 0 | } |
13982 | 0 | return result; |
13983 | 0 | } |
13984 | | |
13985 | | static isc_result_t |
13986 | | sync_secure_journal(dns_zone_t *zone, dns_zone_t *raw, dns_journal_t *journal, |
13987 | | uint32_t start, uint32_t end, dns_difftuple_t **soatuplep, |
13988 | 0 | dns_diff_t *diff) { |
13989 | 0 | isc_result_t result; |
13990 | 0 | dns_difftuple_t *tuple = NULL; |
13991 | 0 | dns_diffop_t op = DNS_DIFFOP_ADD; |
13992 | 0 | int n_soa = 0; |
13993 | |
|
13994 | 0 | REQUIRE(soatuplep != NULL); |
13995 | |
|
13996 | 0 | if (start == end) { |
13997 | 0 | return DNS_R_UNCHANGED; |
13998 | 0 | } |
13999 | | |
14000 | 0 | CHECK(dns_journal_iter_init(journal, start, end, NULL)); |
14001 | 0 | for (result = dns_journal_first_rr(journal); result == ISC_R_SUCCESS; |
14002 | 0 | result = dns_journal_next_rr(journal)) |
14003 | 0 | { |
14004 | 0 | dns_name_t *name = NULL; |
14005 | 0 | uint32_t ttl; |
14006 | 0 | dns_rdata_t *rdata = NULL; |
14007 | 0 | dns_journal_current_rr(journal, &name, &ttl, &rdata); |
14008 | |
|
14009 | 0 | if (rdata->type == dns_rdatatype_soa) { |
14010 | 0 | n_soa++; |
14011 | 0 | if (n_soa == 2) { |
14012 | | /* |
14013 | | * Save the latest raw SOA record. |
14014 | | */ |
14015 | 0 | if (*soatuplep != NULL) { |
14016 | 0 | dns_difftuple_free(soatuplep); |
14017 | 0 | } |
14018 | 0 | dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, |
14019 | 0 | name, ttl, rdata, |
14020 | 0 | soatuplep); |
14021 | 0 | } |
14022 | 0 | if (n_soa == 3) { |
14023 | 0 | n_soa = 1; |
14024 | 0 | } |
14025 | 0 | continue; |
14026 | 0 | } |
14027 | | |
14028 | | /* Sanity. */ |
14029 | 0 | if (n_soa == 0) { |
14030 | 0 | dns_zone_log(raw, ISC_LOG_ERROR, |
14031 | 0 | "corrupt journal file: '%s'\n", |
14032 | 0 | raw->journal); |
14033 | 0 | return ISC_R_FAILURE; |
14034 | 0 | } |
14035 | | |
14036 | 0 | if (zone->privatetype != 0 && rdata->type == zone->privatetype) |
14037 | 0 | { |
14038 | 0 | continue; |
14039 | 0 | } |
14040 | | |
14041 | | /* |
14042 | | * Skip DNSSEC records that BIND maintains with inline-signing. |
14043 | | */ |
14044 | 0 | if (rdata->type == dns_rdatatype_nsec || |
14045 | 0 | rdata->type == dns_rdatatype_rrsig || |
14046 | 0 | rdata->type == dns_rdatatype_nsec3 || |
14047 | 0 | rdata->type == dns_rdatatype_nsec3param) |
14048 | 0 | { |
14049 | 0 | continue; |
14050 | 0 | } |
14051 | | /* |
14052 | | * Allow DNSKEY, CDNSKEY, CDS because users should be able to |
14053 | | * update the zone with these records from a different provider, |
14054 | | * but skip records that are under our control. |
14055 | | */ |
14056 | 0 | if (dns_rdatatype_iskeymaterial(rdata->type)) { |
14057 | 0 | bool inuse = false; |
14058 | 0 | isc_result_t r = dns_zone_dnskey_inuse(zone, rdata, |
14059 | 0 | &inuse); |
14060 | 0 | if (r == ISC_R_SUCCESS && inuse) { |
14061 | 0 | continue; |
14062 | 0 | } |
14063 | 0 | } |
14064 | | |
14065 | 0 | op = (n_soa == 1) ? DNS_DIFFOP_DEL : DNS_DIFFOP_ADD; |
14066 | |
|
14067 | 0 | dns_difftuple_create(diff->mctx, op, name, ttl, rdata, &tuple); |
14068 | 0 | dns_diff_appendminimal(diff, &tuple); |
14069 | 0 | } |
14070 | 0 | if (result == ISC_R_NOMORE) { |
14071 | 0 | result = ISC_R_SUCCESS; |
14072 | 0 | } |
14073 | |
|
14074 | 0 | cleanup: |
14075 | 0 | return result; |
14076 | 0 | } |
14077 | | |
14078 | | /* |
14079 | | * Filter the key material preserving TTL changes. If kasp in effect honour the |
14080 | | * existing ttl. The lists returned by sync_secure_db/dns_db_diffx should be |
14081 | | * DNSSEC RRset order so we can process 'del' and 'add' in parallel rather than |
14082 | | * searching for TTL only changes first and processing them, then checking the |
14083 | | * 'in use' status on a subsequent pass. |
14084 | | */ |
14085 | | |
14086 | | static void |
14087 | | filter_keymaterial(dns_zone_t *zone, dns_difftuplelist_t *del, |
14088 | 0 | dns_difftuplelist_t *add, bool kasp, dns_ttl_t ttl) { |
14089 | 0 | dns_difftuple_t *deltuple = ISC_LIST_HEAD(*del); |
14090 | 0 | dns_difftuple_t *addtuple = ISC_LIST_HEAD(*add); |
14091 | 0 | isc_result_t result; |
14092 | |
|
14093 | 0 | while (deltuple != NULL || addtuple != NULL) { |
14094 | 0 | dns_difftuple_t *delnext = NULL, *addnext = NULL; |
14095 | 0 | bool inuse = false; |
14096 | 0 | if (deltuple != NULL) { |
14097 | 0 | delnext = ISC_LIST_NEXT(deltuple, link); |
14098 | 0 | } |
14099 | 0 | if (addtuple != NULL) { |
14100 | 0 | addnext = ISC_LIST_NEXT(addtuple, link); |
14101 | 0 | } |
14102 | 0 | if (deltuple != NULL && addtuple != NULL) { |
14103 | 0 | int n = dns_rdata_compare(&deltuple->rdata, |
14104 | 0 | &addtuple->rdata); |
14105 | 0 | if (n == 0) { |
14106 | | /* |
14107 | | * If the rdata is equal then the only |
14108 | | * difference will be a TTL change. |
14109 | | */ |
14110 | 0 | if (kasp) { |
14111 | | /* TTL is managed by dnssec-policy */ |
14112 | 0 | ISC_LIST_UNLINK(*del, deltuple, link); |
14113 | 0 | dns_difftuple_free(&deltuple); |
14114 | 0 | ISC_LIST_UNLINK(*add, addtuple, link); |
14115 | 0 | dns_difftuple_free(&addtuple); |
14116 | 0 | } |
14117 | 0 | deltuple = delnext; |
14118 | 0 | addtuple = addnext; |
14119 | 0 | continue; |
14120 | 0 | } |
14121 | 0 | if (n < 0) { |
14122 | 0 | goto checkdel; |
14123 | 0 | } |
14124 | 0 | goto checkadd; |
14125 | 0 | } else if (deltuple != NULL) { |
14126 | 0 | checkdel: |
14127 | 0 | result = dns_zone_dnskey_inuse(zone, &deltuple->rdata, |
14128 | 0 | &inuse); |
14129 | 0 | if (result == ISC_R_SUCCESS && inuse) { |
14130 | 0 | ISC_LIST_UNLINK(*del, deltuple, link); |
14131 | 0 | dns_difftuple_free(&deltuple); |
14132 | 0 | } |
14133 | 0 | deltuple = delnext; |
14134 | 0 | } else { |
14135 | 0 | checkadd: |
14136 | 0 | result = dns_zone_dnskey_inuse(zone, &addtuple->rdata, |
14137 | 0 | &inuse); |
14138 | 0 | if (result == ISC_R_SUCCESS && inuse) { |
14139 | 0 | ISC_LIST_UNLINK(*add, addtuple, link); |
14140 | 0 | dns_difftuple_free(&addtuple); |
14141 | 0 | } else if (kasp) { |
14142 | 0 | addtuple->ttl = ttl; |
14143 | 0 | } |
14144 | 0 | addtuple = addnext; |
14145 | 0 | } |
14146 | 0 | } |
14147 | 0 | } |
14148 | | |
14149 | | static isc_result_t |
14150 | | sync_secure_db(dns_zone_t *seczone, dns_zone_t *raw, dns_db_t *secdb, |
14151 | | dns_dbversion_t *secver, dns_difftuple_t **soatuple, |
14152 | 0 | dns_diff_t *diff) { |
14153 | 0 | isc_result_t result; |
14154 | 0 | dns_db_t *rawdb = NULL; |
14155 | 0 | dns_dbversion_t *rawver = NULL; |
14156 | 0 | dns_difftuple_t *oldtuple = NULL, *newtuple = NULL; |
14157 | 0 | dns_rdata_soa_t oldsoa, newsoa; |
14158 | 0 | dns_difftuplelist_t add = ISC_LIST_INITIALIZER; |
14159 | 0 | dns_difftuplelist_t del = ISC_LIST_INITIALIZER; |
14160 | 0 | dns_difftuplelist_t keyadd = ISC_LIST_INITIALIZER; |
14161 | 0 | dns_difftuplelist_t keydel = ISC_LIST_INITIALIZER; |
14162 | 0 | dns_difftuplelist_t ckeyadd = ISC_LIST_INITIALIZER; |
14163 | 0 | dns_difftuplelist_t ckeydel = ISC_LIST_INITIALIZER; |
14164 | 0 | dns_difftuplelist_t cdsadd = ISC_LIST_INITIALIZER; |
14165 | 0 | dns_difftuplelist_t cdsdel = ISC_LIST_INITIALIZER; |
14166 | 0 | dns_kasp_t *kasp = NULL; |
14167 | 0 | dns_ttl_t keyttl = 0, ckeyttl = 0, cdsttl = 0; |
14168 | |
|
14169 | 0 | REQUIRE(DNS_ZONE_VALID(seczone)); |
14170 | 0 | REQUIRE(soatuple != NULL && *soatuple == NULL); |
14171 | |
|
14172 | 0 | if (!seczone->sourceserialset) { |
14173 | 0 | return DNS_R_UNCHANGED; |
14174 | 0 | } |
14175 | | |
14176 | 0 | dns_db_attach(raw->db, &rawdb); |
14177 | 0 | dns_db_currentversion(rawdb, &rawver); |
14178 | 0 | result = dns_db_diffx(diff, rawdb, rawver, secdb, secver, NULL); |
14179 | 0 | dns_db_closeversion(rawdb, &rawver, false); |
14180 | 0 | dns_db_detach(&rawdb); |
14181 | |
|
14182 | 0 | if (result != ISC_R_SUCCESS) { |
14183 | 0 | return result; |
14184 | 0 | } |
14185 | | |
14186 | | /* |
14187 | | * If kasp is in effect honour the existing DNSKEY, CDNSKEY and CDS |
14188 | | * TTLs. |
14189 | | */ |
14190 | 0 | kasp = seczone->kasp; |
14191 | 0 | if (kasp != NULL) { |
14192 | 0 | dns_rdataset_t rdataset; |
14193 | 0 | dns_dbnode_t *node = NULL; |
14194 | 0 | dns_ttl_t ttl = dns_kasp_dnskeyttl(kasp); |
14195 | |
|
14196 | 0 | dns_rdataset_init(&rdataset); |
14197 | |
|
14198 | 0 | result = dns_db_getoriginnode(secdb, &node); |
14199 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
14200 | |
|
14201 | 0 | result = dns_db_findrdataset( |
14202 | 0 | secdb, node, secver, dns_rdatatype_dnskey, |
14203 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
14204 | 0 | keyttl = (result == ISC_R_SUCCESS) ? rdataset.ttl : ttl; |
14205 | 0 | dns_rdataset_cleanup(&rdataset); |
14206 | |
|
14207 | 0 | result = dns_db_findrdataset( |
14208 | 0 | secdb, node, secver, dns_rdatatype_cdnskey, |
14209 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
14210 | 0 | ckeyttl = (result == ISC_R_SUCCESS) ? rdataset.ttl : ttl; |
14211 | 0 | dns_rdataset_cleanup(&rdataset); |
14212 | |
|
14213 | 0 | result = dns_db_findrdataset( |
14214 | 0 | secdb, node, secver, dns_rdatatype_cds, |
14215 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
14216 | 0 | cdsttl = (result == ISC_R_SUCCESS) ? rdataset.ttl : ttl; |
14217 | 0 | dns_rdataset_cleanup(&rdataset); |
14218 | 0 | dns_db_detachnode(&node); |
14219 | 0 | } |
14220 | |
|
14221 | 0 | ISC_LIST_FOREACH(diff->tuples, tuple, link) { |
14222 | 0 | dns_difftuplelist_t *al = &add, *dl = &del; |
14223 | | |
14224 | | /* |
14225 | | * Skip private records that BIND maintains with inline-signing. |
14226 | | */ |
14227 | 0 | if (seczone->privatetype != 0 && |
14228 | 0 | tuple->rdata.type == seczone->privatetype) |
14229 | 0 | { |
14230 | 0 | ISC_LIST_UNLINK(diff->tuples, tuple, link); |
14231 | 0 | dns_difftuple_free(&tuple); |
14232 | 0 | continue; |
14233 | 0 | } |
14234 | | |
14235 | | /* |
14236 | | * Skip DNSSEC records that BIND maintains with inline-signing. |
14237 | | */ |
14238 | 0 | if (tuple->rdata.type == dns_rdatatype_nsec || |
14239 | 0 | tuple->rdata.type == dns_rdatatype_rrsig || |
14240 | 0 | tuple->rdata.type == dns_rdatatype_nsec3 || |
14241 | 0 | tuple->rdata.type == dns_rdatatype_nsec3param) |
14242 | 0 | { |
14243 | 0 | ISC_LIST_UNLINK(diff->tuples, tuple, link); |
14244 | 0 | dns_difftuple_free(&tuple); |
14245 | 0 | continue; |
14246 | 0 | } |
14247 | | |
14248 | | /* |
14249 | | * Apex DNSKEY, CDNSKEY and CDS need special processing so |
14250 | | * split them out. |
14251 | | */ |
14252 | 0 | if (dns_rdatatype_iskeymaterial(tuple->rdata.type) && |
14253 | 0 | dns_name_equal(&tuple->name, &seczone->origin)) |
14254 | 0 | { |
14255 | 0 | switch (tuple->rdata.type) { |
14256 | 0 | case dns_rdatatype_dnskey: |
14257 | 0 | al = &keyadd; |
14258 | 0 | dl = &keydel; |
14259 | 0 | break; |
14260 | 0 | case dns_rdatatype_cdnskey: |
14261 | 0 | al = &ckeyadd; |
14262 | 0 | dl = &ckeydel; |
14263 | 0 | break; |
14264 | 0 | case dns_rdatatype_cds: |
14265 | 0 | al = &cdsadd; |
14266 | 0 | dl = &cdsdel; |
14267 | 0 | break; |
14268 | 0 | default: |
14269 | 0 | UNREACHABLE(); |
14270 | 0 | } |
14271 | 0 | } |
14272 | | |
14273 | 0 | if (tuple->rdata.type == dns_rdatatype_soa) { |
14274 | 0 | if (tuple->op == DNS_DIFFOP_DEL) { |
14275 | 0 | INSIST(oldtuple == NULL); |
14276 | 0 | oldtuple = tuple; |
14277 | 0 | } |
14278 | 0 | if (tuple->op == DNS_DIFFOP_ADD) { |
14279 | 0 | INSIST(newtuple == NULL); |
14280 | 0 | newtuple = tuple; |
14281 | 0 | } |
14282 | 0 | } |
14283 | | |
14284 | | /* |
14285 | | * Split into deletions and additions. |
14286 | | */ |
14287 | 0 | ISC_LIST_UNLINK(diff->tuples, tuple, link); |
14288 | 0 | switch (tuple->op) { |
14289 | 0 | case DNS_DIFFOP_DEL: |
14290 | 0 | case DNS_DIFFOP_DELRESIGN: |
14291 | 0 | ISC_LIST_APPEND(*dl, tuple, link); |
14292 | 0 | break; |
14293 | 0 | case DNS_DIFFOP_ADD: |
14294 | 0 | case DNS_DIFFOP_ADDRESIGN: |
14295 | 0 | ISC_LIST_APPEND(*al, tuple, link); |
14296 | 0 | break; |
14297 | 0 | default: |
14298 | 0 | UNREACHABLE(); |
14299 | 0 | } |
14300 | 0 | } |
14301 | | |
14302 | 0 | if (oldtuple != NULL && newtuple != NULL) { |
14303 | 0 | result = dns_rdata_tostruct(&oldtuple->rdata, &oldsoa, NULL); |
14304 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
14305 | |
|
14306 | 0 | result = dns_rdata_tostruct(&newtuple->rdata, &newsoa, NULL); |
14307 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
14308 | | |
14309 | | /* |
14310 | | * If the SOA records are the same except for the serial |
14311 | | * remove them from the diff. |
14312 | | */ |
14313 | 0 | if (oldtuple->ttl == newtuple->ttl && |
14314 | 0 | oldsoa.refresh == newsoa.refresh && |
14315 | 0 | oldsoa.retry == newsoa.retry && |
14316 | 0 | oldsoa.minimum == newsoa.minimum && |
14317 | 0 | oldsoa.expire == newsoa.expire && |
14318 | 0 | dns_name_equal(&oldsoa.origin, &newsoa.origin) && |
14319 | 0 | dns_name_equal(&oldsoa.contact, &newsoa.contact)) |
14320 | 0 | { |
14321 | 0 | ISC_LIST_UNLINK(del, oldtuple, link); |
14322 | 0 | dns_difftuple_free(&oldtuple); |
14323 | 0 | ISC_LIST_UNLINK(add, newtuple, link); |
14324 | 0 | dns_difftuple_free(&newtuple); |
14325 | 0 | } |
14326 | 0 | } |
14327 | | |
14328 | | /* |
14329 | | * Filter out keys we manage but still allow TTL changes. |
14330 | | */ |
14331 | 0 | filter_keymaterial(seczone, &keydel, &keyadd, kasp != NULL, keyttl); |
14332 | 0 | filter_keymaterial(seczone, &ckeydel, &ckeyadd, kasp != NULL, ckeyttl); |
14333 | 0 | filter_keymaterial(seczone, &cdsdel, &cdsadd, kasp != NULL, cdsttl); |
14334 | | |
14335 | | /* |
14336 | | * Rebuild the diff now that we have filtered it |
14337 | | */ |
14338 | 0 | ISC_LIST_APPENDLIST(diff->tuples, del, link); |
14339 | 0 | ISC_LIST_APPENDLIST(diff->tuples, keydel, link); |
14340 | 0 | ISC_LIST_APPENDLIST(diff->tuples, ckeydel, link); |
14341 | 0 | ISC_LIST_APPENDLIST(diff->tuples, cdsdel, link); |
14342 | 0 | ISC_LIST_APPENDLIST(diff->tuples, add, link); |
14343 | 0 | ISC_LIST_APPENDLIST(diff->tuples, keyadd, link); |
14344 | 0 | ISC_LIST_APPENDLIST(diff->tuples, ckeyadd, link); |
14345 | 0 | ISC_LIST_APPENDLIST(diff->tuples, cdsadd, link); |
14346 | |
|
14347 | 0 | if (ISC_LIST_EMPTY(diff->tuples)) { |
14348 | 0 | return DNS_R_UNCHANGED; |
14349 | 0 | } |
14350 | | |
14351 | | /* |
14352 | | * If there are still SOA records in the diff they can now be removed |
14353 | | * saving the new SOA record. |
14354 | | */ |
14355 | 0 | if (oldtuple != NULL) { |
14356 | 0 | ISC_LIST_UNLINK(diff->tuples, oldtuple, link); |
14357 | 0 | dns_difftuple_free(&oldtuple); |
14358 | 0 | } |
14359 | |
|
14360 | 0 | if (newtuple != NULL) { |
14361 | 0 | ISC_LIST_UNLINK(diff->tuples, newtuple, link); |
14362 | 0 | *soatuple = newtuple; |
14363 | 0 | } |
14364 | |
|
14365 | 0 | return ISC_R_SUCCESS; |
14366 | 0 | } |
14367 | | |
14368 | | static isc_result_t |
14369 | 0 | zone_get_raw_serial(dns_zone_t *raw, uint32_t *serialp) { |
14370 | 0 | isc_result_t result; |
14371 | |
|
14372 | 0 | ZONEDB_LOCK(&raw->dblock, isc_rwlocktype_read); |
14373 | 0 | if (raw->db != NULL) { |
14374 | 0 | result = dns_db_getsoaserial(raw->db, NULL, serialp); |
14375 | 0 | } else { |
14376 | 0 | result = DNS_R_NOTLOADED; |
14377 | 0 | } |
14378 | 0 | ZONEDB_UNLOCK(&raw->dblock, isc_rwlocktype_read); |
14379 | |
|
14380 | 0 | return result; |
14381 | 0 | } |
14382 | | |
14383 | | static zone_inline_sync_t * |
14384 | 0 | inline_sync_pop(dns_zone_t *zone) { |
14385 | 0 | zone_inline_sync_t *iss = NULL; |
14386 | |
|
14387 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
14388 | |
|
14389 | 0 | LOCK_ZONE(zone); |
14390 | 0 | if (zone->iss != NULL) { |
14391 | | /* |
14392 | | * inline_sync_run() is only called from zone maintenance, so |
14393 | | * zone->iss can only be parked between maintenance passes here. |
14394 | | */ |
14395 | 0 | iss = MOVE_OWNERSHIP(zone->iss); |
14396 | 0 | } |
14397 | 0 | UNLOCK_ZONE(zone); |
14398 | |
|
14399 | 0 | return iss; |
14400 | 0 | } |
14401 | | |
14402 | | static void |
14403 | 0 | inline_sync_destroy(zone_inline_sync_t **issp) { |
14404 | 0 | zone_inline_sync_t *iss = NULL; |
14405 | |
|
14406 | 0 | REQUIRE(issp != NULL); |
14407 | |
|
14408 | 0 | iss = *issp; |
14409 | 0 | if (iss == NULL) { |
14410 | 0 | return; |
14411 | 0 | } |
14412 | 0 | *issp = NULL; |
14413 | |
|
14414 | 0 | INSIST(iss->mctx != NULL); |
14415 | |
|
14416 | 0 | dns_diff_clear(&iss->diff); |
14417 | 0 | dns_update_state_clear(&iss->state); |
14418 | 0 | if (iss->db != NULL) { |
14419 | 0 | if (iss->oldver != NULL) { |
14420 | 0 | dns_db_closeversion(iss->db, &iss->oldver, false); |
14421 | 0 | } |
14422 | 0 | if (iss->newver != NULL) { |
14423 | 0 | dns_db_closeversion(iss->db, &iss->newver, false); |
14424 | 0 | } |
14425 | 0 | dns_db_detach(&iss->db); |
14426 | 0 | } |
14427 | 0 | if (iss->raw != NULL) { |
14428 | 0 | dns_zone_detach(&iss->raw); |
14429 | 0 | } |
14430 | 0 | isc_mem_putanddetach(&iss->mctx, iss, sizeof(*iss)); |
14431 | 0 | } |
14432 | | |
14433 | | static isc_result_t |
14434 | | inline_sync_create(dns_zone_t *zone, uint32_t *endp, |
14435 | 0 | zone_inline_sync_t **issp) { |
14436 | 0 | isc_result_t result = ISC_R_SUCCESS; |
14437 | 0 | zone_inline_sync_t *iss = NULL; |
14438 | 0 | uint32_t end = 0; |
14439 | |
|
14440 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
14441 | 0 | REQUIRE(endp != NULL); |
14442 | 0 | REQUIRE(issp != NULL && *issp == NULL); |
14443 | |
|
14444 | 0 | iss = isc_mem_cget(zone->mctx, 1, sizeof(*iss)); |
14445 | 0 | isc_mem_attach(zone->mctx, &iss->mctx); |
14446 | |
|
14447 | 0 | LOCK_ZONE(zone); |
14448 | | |
14449 | | /* |
14450 | | * The inline_sync_run() is loop-serialized for the zone. Install the |
14451 | | * transaction while attaching its resources so zone->iss stays the |
14452 | | * single marker for sync-in-flight. |
14453 | | */ |
14454 | 0 | INSIST(zone->iss == NULL); |
14455 | 0 | zone->iss = iss; |
14456 | 0 | dns_diff_init(iss->mctx, &iss->diff); |
14457 | |
|
14458 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
14459 | 0 | if (zone->db != NULL) { |
14460 | 0 | dns_db_attach(zone->db, &iss->db); |
14461 | 0 | } else { |
14462 | 0 | result = ISC_R_FAILURE; |
14463 | 0 | } |
14464 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
14465 | |
|
14466 | 0 | if (zone->raw != NULL) { |
14467 | 0 | dns_zone_attach(zone->raw, &iss->raw); |
14468 | 0 | } else { |
14469 | 0 | result = ISC_R_FAILURE; |
14470 | 0 | } |
14471 | |
|
14472 | 0 | UNLOCK_ZONE(zone); |
14473 | |
|
14474 | 0 | CHECK(result); |
14475 | 0 | CHECK(zone_get_raw_serial(iss->raw, &end)); |
14476 | |
|
14477 | 0 | LOCK_ZONE(zone); |
14478 | 0 | if (zone->sourceserialset && end == zone->sourceserial) { |
14479 | 0 | UNLOCK_ZONE(zone); |
14480 | 0 | result = DNS_R_UNCHANGED; |
14481 | 0 | goto cleanup; |
14482 | 0 | } |
14483 | 0 | iss->end = end; |
14484 | 0 | UNLOCK_ZONE(zone); |
14485 | |
|
14486 | 0 | dns_db_currentversion(iss->db, &iss->oldver); |
14487 | 0 | CHECK(dns_db_newversion(iss->db, &iss->newver)); |
14488 | |
|
14489 | 0 | *endp = end; |
14490 | 0 | *issp = iss; |
14491 | 0 | return ISC_R_SUCCESS; |
14492 | | |
14493 | 0 | cleanup: |
14494 | 0 | iss = inline_sync_pop(zone); |
14495 | 0 | inline_sync_destroy(&iss); |
14496 | 0 | return result; |
14497 | 0 | } |
14498 | | |
14499 | | static void |
14500 | 0 | zone_clear_inline_sync(dns_zone_t *zone) { |
14501 | 0 | zone_inline_sync_t *iss = NULL; |
14502 | |
|
14503 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
14504 | |
|
14505 | 0 | iss = inline_sync_pop(zone); |
14506 | 0 | inline_sync_destroy(&iss); |
14507 | 0 | } |
14508 | | |
14509 | | static void |
14510 | 0 | inline_secure_bootstrap(dns_zone_t *zone) { |
14511 | 0 | isc_result_t result = ISC_R_SUCCESS; |
14512 | 0 | dns_db_t *rawdb = NULL, *newdb = NULL; |
14513 | 0 | uint32_t end = 0; |
14514 | |
|
14515 | 0 | ENTER; |
14516 | |
|
14517 | 0 | LOCK_ZONE(zone); |
14518 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING) || |
14519 | 0 | !dns__zone_inline_secure(zone)) |
14520 | 0 | { |
14521 | 0 | CHECK(ISC_R_SHUTTINGDOWN); |
14522 | 0 | } |
14523 | | |
14524 | 0 | ZONEDB_LOCK(&zone->raw->dblock, isc_rwlocktype_read); |
14525 | 0 | if (zone->raw->db != NULL) { |
14526 | 0 | dns_db_attach(zone->raw->db, &rawdb); |
14527 | 0 | result = dns_db_getsoaserial(rawdb, NULL, &end); |
14528 | 0 | } else { |
14529 | 0 | result = DNS_R_NOTLOADED; |
14530 | 0 | } |
14531 | 0 | ZONEDB_UNLOCK(&zone->raw->dblock, isc_rwlocktype_read); |
14532 | 0 | CHECK(result); |
14533 | |
|
14534 | 0 | CHECK(secure_db_create_from_raw(zone, rawdb, &newdb)); |
14535 | |
|
14536 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY); |
14537 | 0 | CHECK(zone_postload(zone, newdb, isc_time_now(), ISC_R_SUCCESS)); |
14538 | |
|
14539 | 0 | zone->sourceserial = end; |
14540 | 0 | zone->sourceserialset = true; |
14541 | 0 | zone->inline_sync_phase = inline_sync_idle; |
14542 | 0 | zone_needdump(zone, 0); |
14543 | |
|
14544 | 0 | cleanup: |
14545 | 0 | UNLOCK_ZONE(zone); |
14546 | |
|
14547 | 0 | if (newdb != NULL) { |
14548 | 0 | dns_db_detach(&newdb); |
14549 | 0 | } |
14550 | 0 | if (rawdb != NULL) { |
14551 | 0 | dns_db_detach(&rawdb); |
14552 | 0 | } |
14553 | 0 | if (result != ISC_R_SUCCESS && result != DNS_R_NOTLOADED) { |
14554 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, "inline secure bootstrap: %s", |
14555 | 0 | isc_result_totext(result)); |
14556 | 0 | } |
14557 | 0 | } |
14558 | | |
14559 | | static isc_result_t |
14560 | 0 | inline_sync_finalize(dns_zone_t *zone, uint32_t newserial, uint32_t desired) { |
14561 | 0 | isc_result_t result = ISC_R_SUCCESS; |
14562 | 0 | dns_journal_t *rjournal = NULL; |
14563 | 0 | dns_update_log_t log = { update_log_cb, NULL }; |
14564 | 0 | zone_inline_sync_t *iss = NULL; |
14565 | 0 | uint32_t end = 0; |
14566 | |
|
14567 | 0 | ENTER; |
14568 | |
|
14569 | 0 | LOCK_ZONE(zone); |
14570 | 0 | iss = zone->iss; |
14571 | 0 | INSIST(iss != NULL); |
14572 | 0 | end = iss->end; |
14573 | 0 | UNLOCK_ZONE(zone); |
14574 | |
|
14575 | 0 | result = dns_update_signaturesinc( |
14576 | 0 | &log, zone, iss->db, iss->oldver, iss->newver, &iss->diff, |
14577 | 0 | zone->sigvalidityinterval, &iss->state); |
14578 | 0 | if (result == DNS_R_CONTINUE) { |
14579 | 0 | LOCK_ZONE(zone); |
14580 | 0 | zone_schedule_inline_sync(zone, inline_sync_idle); |
14581 | 0 | UNLOCK_ZONE(zone); |
14582 | 0 | return result; |
14583 | 0 | } |
14584 | | |
14585 | | /* |
14586 | | * If something went wrong while trying to update the secure zone and |
14587 | | * the latter was already signed before, do not apply raw zone deltas to |
14588 | | * it as that would break existing DNSSEC signatures. However, if the |
14589 | | * secure zone was not yet signed (e.g. because no signing keys were |
14590 | | * created for it), commence applying raw zone deltas to it so that |
14591 | | * contents of the raw zone and the secure zone are kept in sync. |
14592 | | */ |
14593 | 0 | if (result != ISC_R_SUCCESS && dns_db_issecure(iss->db)) { |
14594 | 0 | goto cleanup; |
14595 | 0 | } |
14596 | | |
14597 | 0 | CHECK(dns_journal_open(iss->raw->mctx, iss->raw->journal, |
14598 | 0 | DNS_JOURNAL_WRITE, &rjournal)); |
14599 | 0 | CHECK(zone_journal(zone, &iss->diff, &end, "inline_sync")); |
14600 | |
|
14601 | 0 | dns_journal_set_sourceserial(rjournal, end); |
14602 | 0 | dns_journal_commit(rjournal); |
14603 | |
|
14604 | 0 | LOCK_ZONE(zone); |
14605 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY); |
14606 | |
|
14607 | 0 | zone->sourceserial = end; |
14608 | 0 | zone->sourceserialset = true; |
14609 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
14610 | | |
14611 | | /* |
14612 | | * Set resign time to make sure it is set to the earliest signature |
14613 | | * expiration. |
14614 | | */ |
14615 | 0 | dns__zone_set_resigntime(zone); |
14616 | 0 | dns__zone_settimer(zone, isc_time_now()); |
14617 | 0 | UNLOCK_ZONE(zone); |
14618 | |
|
14619 | 0 | dns_db_closeversion(iss->db, &iss->oldver, false); |
14620 | 0 | dns_db_closeversion(iss->db, &iss->newver, true); |
14621 | |
|
14622 | 0 | if (newserial != 0) { |
14623 | 0 | dns_zone_log(zone, ISC_LOG_INFO, "serial %u (unsigned %u)", |
14624 | 0 | newserial, desired); |
14625 | 0 | } |
14626 | |
|
14627 | 0 | cleanup: |
14628 | 0 | if (rjournal != NULL) { |
14629 | 0 | dns_journal_destroy(&rjournal); |
14630 | 0 | } |
14631 | |
|
14632 | 0 | return result; |
14633 | 0 | } |
14634 | | |
14635 | | static void |
14636 | 0 | inline_sync_run(dns_zone_t *zone) { |
14637 | 0 | isc_result_t result = ISC_R_SUCCESS; |
14638 | 0 | dns_journal_t *rjournal = NULL; |
14639 | 0 | dns_journal_t *sjournal = NULL; |
14640 | 0 | uint32_t start = 0, end = 0; |
14641 | 0 | dns_difftuple_t *tuple = NULL, *soatuple = NULL; |
14642 | 0 | uint32_t newserial = 0, desired = 0; |
14643 | 0 | zone_inline_sync_t *iss = NULL; |
14644 | |
|
14645 | 0 | ENTER; |
14646 | |
|
14647 | 0 | CHECK(inline_sync_create(zone, &end, &iss)); |
14648 | | |
14649 | | /* |
14650 | | * We first attempt to sync the raw zone to the secure zone by using the |
14651 | | * raw zone's journal, applying all the deltas from the latest |
14652 | | * source-serial of the secure zone up to the current serial number of |
14653 | | * the raw zone. |
14654 | | * |
14655 | | * If that fails, then we'll fall back to a direct comparison between |
14656 | | * raw and secure zones. |
14657 | | */ |
14658 | 0 | CHECK(dns_journal_open(iss->raw->mctx, iss->raw->journal, |
14659 | 0 | DNS_JOURNAL_WRITE, &rjournal)); |
14660 | |
|
14661 | 0 | result = dns_journal_open(zone->mctx, zone->journal, DNS_JOURNAL_READ, |
14662 | 0 | &sjournal); |
14663 | 0 | if (result != ISC_R_NOTFOUND) { |
14664 | 0 | CHECK(result); |
14665 | 0 | } |
14666 | | |
14667 | 0 | if (!dns_journal_get_sourceserial(rjournal, &start)) { |
14668 | 0 | start = dns_journal_first_serial(rjournal); |
14669 | 0 | dns_journal_set_sourceserial(rjournal, start); |
14670 | 0 | } |
14671 | 0 | if (sjournal != NULL) { |
14672 | 0 | uint32_t serial; |
14673 | | /* |
14674 | | * We read the secure journal first, if that exists use its |
14675 | | * value provided it is greater that from the raw journal. |
14676 | | */ |
14677 | 0 | if (dns_journal_get_sourceserial(sjournal, &serial) && |
14678 | 0 | isc_serial_gt(serial, start)) |
14679 | 0 | { |
14680 | 0 | start = serial; |
14681 | 0 | } |
14682 | 0 | dns_journal_destroy(&sjournal); |
14683 | 0 | } |
14684 | | |
14685 | | /* |
14686 | | * Try to apply diffs from the raw zone's journal to the secure zone. If |
14687 | | * that fails, we recover by syncing up the databases directly. |
14688 | | */ |
14689 | 0 | result = sync_secure_journal(zone, iss->raw, rjournal, start, end, |
14690 | 0 | &soatuple, &iss->diff); |
14691 | 0 | if (result == DNS_R_UNCHANGED) { |
14692 | 0 | LOCK_ZONE(zone); |
14693 | 0 | zone->sourceserial = end; |
14694 | 0 | zone->sourceserialset = true; |
14695 | 0 | UNLOCK_ZONE(zone); |
14696 | |
|
14697 | 0 | goto cleanup; |
14698 | 0 | } else if (result != ISC_R_SUCCESS) { |
14699 | 0 | result = sync_secure_db(zone, iss->raw, iss->db, iss->oldver, |
14700 | 0 | &soatuple, &iss->diff); |
14701 | 0 | if (result == DNS_R_UNCHANGED) { |
14702 | 0 | LOCK_ZONE(zone); |
14703 | 0 | zone->sourceserial = end; |
14704 | 0 | zone->sourceserialset = true; |
14705 | 0 | UNLOCK_ZONE(zone); |
14706 | |
|
14707 | 0 | goto cleanup; |
14708 | 0 | } |
14709 | 0 | CHECK(result); |
14710 | 0 | } |
14711 | | |
14712 | 0 | CHECK(dns_diff_apply(&iss->diff, iss->db, iss->newver)); |
14713 | |
|
14714 | 0 | if (soatuple != NULL) { |
14715 | 0 | uint32_t oldserial; |
14716 | |
|
14717 | 0 | CHECK(dns_db_createsoatuple(iss->db, iss->oldver, |
14718 | 0 | iss->diff.mctx, DNS_DIFFOP_DEL, |
14719 | 0 | &tuple)); |
14720 | 0 | oldserial = dns_soa_getserial(&tuple->rdata); |
14721 | 0 | newserial = desired = dns_soa_getserial(&soatuple->rdata); |
14722 | 0 | if (!isc_serial_gt(newserial, oldserial)) { |
14723 | 0 | newserial = oldserial + 1; |
14724 | 0 | if (newserial == 0) { |
14725 | 0 | newserial++; |
14726 | 0 | } |
14727 | 0 | dns_soa_setserial(newserial, &soatuple->rdata); |
14728 | 0 | } |
14729 | 0 | CHECK(do_one_tuple(&tuple, iss->db, iss->newver, &iss->diff)); |
14730 | 0 | CHECK(do_one_tuple(&soatuple, iss->db, iss->newver, |
14731 | 0 | &iss->diff)); |
14732 | 0 | } else { |
14733 | 0 | CHECK(update_soa_serial(zone, iss->db, iss->newver, &iss->diff, |
14734 | 0 | zone->mctx, zone->updatemethod)); |
14735 | 0 | } |
14736 | | |
14737 | 0 | cleanup: |
14738 | 0 | if (sjournal != NULL) { |
14739 | 0 | dns_journal_destroy(&sjournal); |
14740 | 0 | } |
14741 | 0 | if (rjournal != NULL) { |
14742 | 0 | dns_journal_destroy(&rjournal); |
14743 | 0 | } |
14744 | 0 | if (tuple != NULL) { |
14745 | 0 | dns_difftuple_free(&tuple); |
14746 | 0 | } |
14747 | 0 | if (soatuple != NULL) { |
14748 | 0 | dns_difftuple_free(&soatuple); |
14749 | 0 | } |
14750 | 0 | if (result == ISC_R_SUCCESS) { |
14751 | 0 | result = inline_sync_finalize(zone, newserial, desired); |
14752 | 0 | } |
14753 | 0 | if (result != DNS_R_CONTINUE) { |
14754 | 0 | zone_clear_inline_sync(zone); |
14755 | | /* |
14756 | | * In the pull model, DNS_R_UNCHANGED and DNS_R_NOTLOADED are |
14757 | | * idle results. Treating them like errors here would reset the |
14758 | | * maintenance timer to "now" and either spin on an already |
14759 | | * current raw serial or retry before raw loading finishes. |
14760 | | */ |
14761 | 0 | if (result != ISC_R_SUCCESS && result != DNS_R_UNCHANGED && |
14762 | 0 | result != DNS_R_NOTLOADED) |
14763 | 0 | { |
14764 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, "inline_sync: %s", |
14765 | 0 | isc_result_totext(result)); |
14766 | 0 | } |
14767 | 0 | } |
14768 | 0 | } |
14769 | | |
14770 | | static void |
14771 | 0 | inline_sync_resume(dns_zone_t *zone) { |
14772 | 0 | isc_result_t result = ISC_R_SUCCESS; |
14773 | |
|
14774 | 0 | ENTER; |
14775 | |
|
14776 | 0 | LOCK_ZONE(zone); |
14777 | 0 | INSIST(zone->iss != NULL); |
14778 | 0 | UNLOCK_ZONE(zone); |
14779 | |
|
14780 | 0 | result = inline_sync_finalize(zone, 0, 0); |
14781 | 0 | if (result != DNS_R_CONTINUE) { |
14782 | 0 | zone_clear_inline_sync(zone); |
14783 | 0 | if (result != ISC_R_SUCCESS && result != DNS_R_UNCHANGED && |
14784 | 0 | result != DNS_R_NOTLOADED) |
14785 | 0 | { |
14786 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, "inline_sync: %s", |
14787 | 0 | isc_result_totext(result)); |
14788 | 0 | } |
14789 | 0 | } |
14790 | 0 | } |
14791 | | |
14792 | | STATIC_ASSERT(inline_sync_idle < inline_sync_incremental && |
14793 | | inline_sync_incremental < inline_sync_full, |
14794 | | "inline sync states must be ordered by priority"); |
14795 | | |
14796 | | static void |
14797 | 0 | zone_schedule_inline_sync(dns_zone_t *zone, inline_sync_phase_t state) { |
14798 | 0 | INSIST(LOCKED_ZONE(zone)); |
14799 | 0 | INSIST(dns__zone_inline_secure(zone)); |
14800 | 0 | INSIST(state == inline_sync_idle || state == inline_sync_incremental || |
14801 | 0 | state == inline_sync_full); |
14802 | |
|
14803 | 0 | zone->inline_sync_phase = ISC_MAX(zone->inline_sync_phase, state); |
14804 | 0 | dns__zone_settimer(zone, isc_time_now()); |
14805 | 0 | } |
14806 | | |
14807 | | static isc_result_t |
14808 | | checkandaddsoa(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version, |
14809 | 0 | dns_name_t *name, dns_rdataset_t *rdataset, uint32_t oldserial) { |
14810 | 0 | dns_rdata_soa_t soa; |
14811 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
14812 | 0 | dns_rdatalist_t temprdatalist; |
14813 | 0 | dns_rdataset_t temprdataset; |
14814 | 0 | isc_buffer_t b; |
14815 | 0 | isc_result_t result; |
14816 | 0 | unsigned char buf[DNS_SOA_BUFFERSIZE]; |
14817 | |
|
14818 | 0 | result = dns_rdataset_first(rdataset); |
14819 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
14820 | 0 | dns_rdataset_current(rdataset, &rdata); |
14821 | 0 | result = dns_rdata_tostruct(&rdata, &soa, NULL); |
14822 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
14823 | |
|
14824 | 0 | if (isc_serial_gt(soa.serial, oldserial)) { |
14825 | 0 | return dns_db_addrdataset(db, node, version, 0, rdataset, 0, |
14826 | 0 | NULL); |
14827 | 0 | } |
14828 | | /* |
14829 | | * Always bump the serial. |
14830 | | */ |
14831 | 0 | oldserial++; |
14832 | 0 | if (oldserial == 0) { |
14833 | 0 | oldserial++; |
14834 | 0 | } |
14835 | 0 | soa.serial = oldserial; |
14836 | | |
14837 | | /* |
14838 | | * Construct a replacement rdataset. |
14839 | | */ |
14840 | 0 | dns_rdata_reset(&rdata); |
14841 | 0 | isc_buffer_init(&b, buf, sizeof(buf)); |
14842 | 0 | result = dns_rdata_fromstruct(&rdata, rdataset->rdclass, |
14843 | 0 | dns_rdatatype_soa, &soa, &b); |
14844 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
14845 | 0 | dns_rdatalist_init(&temprdatalist); |
14846 | 0 | temprdatalist.rdclass = rdata.rdclass; |
14847 | 0 | temprdatalist.type = rdata.type; |
14848 | 0 | temprdatalist.ttl = rdataset->ttl; |
14849 | 0 | ISC_LIST_APPEND(temprdatalist.rdata, &rdata, link); |
14850 | |
|
14851 | 0 | dns_rdataset_init(&temprdataset); |
14852 | 0 | dns_rdatalist_tordataset(&temprdatalist, &temprdataset); |
14853 | |
|
14854 | 0 | dns_rdataset_getownercase(rdataset, name); |
14855 | 0 | dns_rdataset_setownercase(&temprdataset, name); |
14856 | 0 | return dns_db_addrdataset(db, node, version, 0, &temprdataset, 0, NULL); |
14857 | 0 | } |
14858 | | |
14859 | | /* |
14860 | | * This function should populate an nsec3paramlist_t with the |
14861 | | * nsecparam_t data from a zone. |
14862 | | */ |
14863 | | static isc_result_t |
14864 | 0 | save_nsec3param(dns_zone_t *zone, nsec3paramlist_t *nsec3list) { |
14865 | 0 | isc_result_t result; |
14866 | 0 | dns_dbnode_t *node = NULL; |
14867 | 0 | dns_rdataset_t rdataset, prdataset; |
14868 | 0 | dns_dbversion_t *version = NULL; |
14869 | 0 | nsec3param_t *nsec3param = NULL; |
14870 | 0 | dns_db_t *db = NULL; |
14871 | 0 | unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE]; |
14872 | |
|
14873 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
14874 | 0 | REQUIRE(nsec3list != NULL); |
14875 | 0 | REQUIRE(ISC_LIST_EMPTY(*nsec3list)); |
14876 | |
|
14877 | 0 | dns_rdataset_init(&rdataset); |
14878 | 0 | dns_rdataset_init(&prdataset); |
14879 | |
|
14880 | 0 | dns_db_attach(zone->db, &db); |
14881 | 0 | CHECK(dns_db_getoriginnode(db, &node)); |
14882 | |
|
14883 | 0 | dns_db_currentversion(db, &version); |
14884 | 0 | result = dns_db_findrdataset(db, node, version, |
14885 | 0 | dns_rdatatype_nsec3param, |
14886 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
14887 | |
|
14888 | 0 | if (result != ISC_R_SUCCESS) { |
14889 | 0 | goto getprivate; |
14890 | 0 | } |
14891 | | |
14892 | | /* |
14893 | | * Walk nsec3param rdataset making a list of parameters (note that |
14894 | | * multiple simultaneous nsec3 chains are annoyingly legal -- this |
14895 | | * is why we use an nsec3list, even though we will usually only |
14896 | | * have one). |
14897 | | */ |
14898 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
14899 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
14900 | 0 | dns_rdata_t private = DNS_RDATA_INIT; |
14901 | |
|
14902 | 0 | dns_rdataset_current(&rdataset, &rdata); |
14903 | 0 | isc_log_write(DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE, |
14904 | 0 | ISC_LOG_DEBUG(3), |
14905 | 0 | "looping through nsec3param data"); |
14906 | 0 | nsec3param = isc_mem_get(zone->mctx, sizeof(nsec3param_t)); |
14907 | 0 | ISC_LINK_INIT(nsec3param, link); |
14908 | | |
14909 | | /* |
14910 | | * now transfer the data from the rdata to |
14911 | | * the nsec3param |
14912 | | */ |
14913 | 0 | dns_nsec3param_toprivate(&rdata, &private, zone->privatetype, |
14914 | 0 | nsec3param->data, |
14915 | 0 | sizeof(nsec3param->data)); |
14916 | 0 | nsec3param->length = private.length; |
14917 | 0 | ISC_LIST_APPEND(*nsec3list, nsec3param, link); |
14918 | 0 | } |
14919 | |
|
14920 | 0 | getprivate: |
14921 | 0 | result = dns_db_findrdataset(db, node, version, zone->privatetype, |
14922 | 0 | dns_rdatatype_none, 0, &prdataset, NULL); |
14923 | 0 | if (result != ISC_R_SUCCESS) { |
14924 | 0 | goto done; |
14925 | 0 | } |
14926 | | |
14927 | | /* |
14928 | | * walk private type records, converting them to nsec3 parameters |
14929 | | * using dns_nsec3param_fromprivate(), do the right thing based on |
14930 | | * CREATE and REMOVE flags |
14931 | | */ |
14932 | 0 | DNS_RDATASET_FOREACH(&prdataset) { |
14933 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
14934 | 0 | dns_rdata_t private = DNS_RDATA_INIT; |
14935 | |
|
14936 | 0 | dns_rdataset_current(&prdataset, &private); |
14937 | 0 | isc_log_write(DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_ZONE, |
14938 | 0 | ISC_LOG_DEBUG(3), |
14939 | 0 | "looping through nsec3param private data"); |
14940 | | |
14941 | | /* |
14942 | | * Do we have a valid private record? |
14943 | | */ |
14944 | 0 | if (!dns_nsec3param_fromprivate(&private, &rdata, buf, |
14945 | 0 | sizeof(buf))) |
14946 | 0 | { |
14947 | 0 | continue; |
14948 | 0 | } |
14949 | | |
14950 | | /* |
14951 | | * Remove any NSEC3PARAM records scheduled to be removed. |
14952 | | */ |
14953 | 0 | if (NSEC3REMOVE(rdata.data[1])) { |
14954 | | /* |
14955 | | * Zero out the flags. |
14956 | | */ |
14957 | 0 | rdata.data[1] = 0; |
14958 | |
|
14959 | 0 | ISC_LIST_FOREACH(*nsec3list, nsec3p, link) { |
14960 | 0 | if (nsec3p->length == |
14961 | 0 | (unsigned int)rdata.length + 1 && |
14962 | 0 | memcmp(rdata.data, nsec3p->data + 1, |
14963 | 0 | nsec3p->length - 1) == 0) |
14964 | 0 | { |
14965 | 0 | ISC_LIST_UNLINK(*nsec3list, nsec3p, |
14966 | 0 | link); |
14967 | 0 | isc_mem_put(zone->mctx, nsec3p, |
14968 | 0 | sizeof(nsec3param_t)); |
14969 | 0 | } |
14970 | 0 | } |
14971 | 0 | continue; |
14972 | 0 | } |
14973 | | |
14974 | 0 | nsec3param = isc_mem_get(zone->mctx, sizeof(nsec3param_t)); |
14975 | 0 | ISC_LINK_INIT(nsec3param, link); |
14976 | | |
14977 | | /* |
14978 | | * Copy the remaining private records so the nsec/nsec3 |
14979 | | * chain gets created. |
14980 | | */ |
14981 | 0 | INSIST(private.length <= sizeof(nsec3param->data)); |
14982 | 0 | memmove(nsec3param->data, private.data, private.length); |
14983 | 0 | nsec3param->length = private.length; |
14984 | 0 | ISC_LIST_APPEND(*nsec3list, nsec3param, link); |
14985 | 0 | } |
14986 | |
|
14987 | 0 | done: |
14988 | 0 | if (result == ISC_R_NOTFOUND) { |
14989 | 0 | result = ISC_R_SUCCESS; |
14990 | 0 | } |
14991 | |
|
14992 | 0 | cleanup: |
14993 | 0 | if (node != NULL) { |
14994 | 0 | dns_db_detachnode(&node); |
14995 | 0 | } |
14996 | 0 | if (version != NULL) { |
14997 | 0 | dns_db_closeversion(db, &version, false); |
14998 | 0 | } |
14999 | 0 | if (db != NULL) { |
15000 | 0 | dns_db_detach(&db); |
15001 | 0 | } |
15002 | 0 | dns_rdataset_cleanup(&rdataset); |
15003 | 0 | dns_rdataset_cleanup(&prdataset); |
15004 | 0 | return result; |
15005 | 0 | } |
15006 | | |
15007 | | /* |
15008 | | * Populate new zone db with private type records found by save_nsec3param(). |
15009 | | */ |
15010 | | static isc_result_t |
15011 | | restore_nsec3param(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version, |
15012 | 0 | nsec3paramlist_t *nsec3list) { |
15013 | 0 | isc_result_t result = ISC_R_SUCCESS; |
15014 | 0 | dns_diff_t diff; |
15015 | 0 | dns_rdata_t rdata; |
15016 | |
|
15017 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
15018 | 0 | REQUIRE(!ISC_LIST_EMPTY(*nsec3list)); |
15019 | |
|
15020 | 0 | dns_diff_init(zone->mctx, &diff); |
15021 | | |
15022 | | /* |
15023 | | * Loop through the list of private-type records, set the INITIAL |
15024 | | * and CREATE flags, and the add the record to the apex of the tree |
15025 | | * in db. |
15026 | | */ |
15027 | 0 | ISC_LIST_FOREACH(*nsec3list, nsec3p, link) { |
15028 | 0 | dns_rdata_init(&rdata); |
15029 | 0 | nsec3p->data[2] = DNS_NSEC3FLAG_CREATE | DNS_NSEC3FLAG_INITIAL; |
15030 | 0 | rdata.length = nsec3p->length; |
15031 | 0 | rdata.data = nsec3p->data; |
15032 | 0 | rdata.type = zone->privatetype; |
15033 | 0 | rdata.rdclass = zone->rdclass; |
15034 | 0 | result = update_one_rr(db, version, &diff, DNS_DIFFOP_ADD, |
15035 | 0 | &zone->origin, 0, &rdata); |
15036 | 0 | if (result != ISC_R_SUCCESS) { |
15037 | 0 | break; |
15038 | 0 | } |
15039 | 0 | } |
15040 | |
|
15041 | 0 | dns_diff_clear(&diff); |
15042 | 0 | return result; |
15043 | 0 | } |
15044 | | |
15045 | | static isc_result_t |
15046 | | copy_non_dnssec_records(dns_db_t *db, dns_dbversion_t *version, dns_db_t *rawdb, |
15047 | 0 | dns_dbiterator_t *dbiterator, unsigned int *oldserial) { |
15048 | 0 | dns_dbnode_t *rawnode = NULL, *node = NULL; |
15049 | 0 | dns_fixedname_t fixed; |
15050 | 0 | dns_name_t *name = dns_fixedname_initname(&fixed); |
15051 | 0 | dns_rdatasetiter_t *rdsit = NULL; |
15052 | 0 | isc_result_t result; |
15053 | |
|
15054 | 0 | result = dns_dbiterator_current(dbiterator, &rawnode, name); |
15055 | 0 | if (result != ISC_R_SUCCESS) { |
15056 | 0 | return ISC_R_SUCCESS; |
15057 | 0 | } |
15058 | | |
15059 | 0 | dns_dbiterator_pause(dbiterator); |
15060 | |
|
15061 | 0 | CHECK(dns_db_findnode(db, name, true, &node)); |
15062 | |
|
15063 | 0 | CHECK(dns_db_allrdatasets(rawdb, rawnode, NULL, 0, 0, &rdsit)); |
15064 | |
|
15065 | 0 | DNS_RDATASETITER_FOREACH(rdsit) { |
15066 | 0 | dns_rdataset_t rdataset = DNS_RDATASET_INIT; |
15067 | 0 | dns_rdatasetiter_current(rdsit, &rdataset); |
15068 | 0 | if (rdataset.type == dns_rdatatype_nsec || |
15069 | 0 | rdataset.type == dns_rdatatype_rrsig || |
15070 | 0 | rdataset.type == dns_rdatatype_nsec3 || |
15071 | 0 | rdataset.type == dns_rdatatype_nsec3param) |
15072 | 0 | { |
15073 | 0 | dns_rdataset_disassociate(&rdataset); |
15074 | 0 | continue; |
15075 | 0 | } |
15076 | | /* |
15077 | | * Allow DNSKEY, CDNSKEY, CDS because users should be able to |
15078 | | * update the zone with these records from a different provider, |
15079 | | * and thus they may exist in the raw version of the zone. |
15080 | | */ |
15081 | | |
15082 | 0 | if (rdataset.type == dns_rdatatype_soa && oldserial != NULL) { |
15083 | 0 | result = checkandaddsoa(db, node, version, name, |
15084 | 0 | &rdataset, *oldserial); |
15085 | 0 | } else { |
15086 | 0 | result = dns_db_addrdataset(db, node, version, 0, |
15087 | 0 | &rdataset, 0, NULL); |
15088 | 0 | } |
15089 | 0 | dns_rdataset_disassociate(&rdataset); |
15090 | 0 | if (result != ISC_R_SUCCESS) { |
15091 | 0 | break; |
15092 | 0 | } |
15093 | 0 | } |
15094 | |
|
15095 | 0 | cleanup: |
15096 | 0 | if (rdsit != NULL) { |
15097 | 0 | dns_rdatasetiter_destroy(&rdsit); |
15098 | 0 | } |
15099 | 0 | if (rawnode) { |
15100 | 0 | dns_db_detachnode(&rawnode); |
15101 | 0 | } |
15102 | 0 | if (node) { |
15103 | 0 | dns_db_detachnode(&node); |
15104 | 0 | } |
15105 | 0 | return result; |
15106 | 0 | } |
15107 | | |
15108 | | static isc_result_t |
15109 | 0 | secure_db_create_from_raw(dns_zone_t *zone, dns_db_t *rawdb, dns_db_t **dbp) { |
15110 | 0 | isc_result_t result = ISC_R_SUCCESS; |
15111 | 0 | dns_db_t *db = NULL; |
15112 | 0 | dns_dbiterator_t *dbiterator = NULL; |
15113 | 0 | dns_dbversion_t *version = NULL; |
15114 | 0 | unsigned int oldserial = 0, *oldserialp = NULL; |
15115 | 0 | nsec3paramlist_t nsec3list; |
15116 | |
|
15117 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
15118 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
15119 | 0 | REQUIRE(dbp != NULL && *dbp == NULL); |
15120 | |
|
15121 | 0 | ISC_LIST_INIT(nsec3list); |
15122 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
15123 | 0 | if (zone->db != NULL) { |
15124 | 0 | result = dns_db_getsoaserial(zone->db, NULL, &oldserial); |
15125 | 0 | if (result == ISC_R_SUCCESS) { |
15126 | 0 | oldserialp = &oldserial; |
15127 | 0 | } |
15128 | | |
15129 | | /* |
15130 | | * assemble nsec3parameters from the old zone, and set a flag |
15131 | | * if any are found |
15132 | | */ |
15133 | 0 | result = save_nsec3param(zone, &nsec3list); |
15134 | 0 | if (result != ISC_R_SUCCESS) { |
15135 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
15136 | 0 | goto cleanup; |
15137 | 0 | } |
15138 | 0 | } |
15139 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
15140 | |
|
15141 | 0 | CHECK(dns_db_create(zone->mctx, zone->db_argv[0], &zone->origin, |
15142 | 0 | dns_dbtype_zone, zone->rdclass, zone->db_argc - 1, |
15143 | 0 | zone->db_argv + 1, &db)); |
15144 | |
|
15145 | 0 | result = dns_db_setgluecachestats(db, zone->gluecachestats); |
15146 | 0 | if (result != ISC_R_NOTIMPLEMENTED) { |
15147 | 0 | CHECK(result); |
15148 | 0 | } |
15149 | | |
15150 | 0 | CHECK(dns_db_newversion(db, &version)); |
15151 | 0 | CHECK(dns_db_createiterator(rawdb, DNS_DB_NONSEC3, &dbiterator)); |
15152 | |
|
15153 | 0 | DNS_DBITERATOR_FOREACH(dbiterator) { |
15154 | 0 | CHECK(copy_non_dnssec_records(db, version, rawdb, dbiterator, |
15155 | 0 | oldserialp)); |
15156 | 0 | } |
15157 | 0 | dns_dbiterator_destroy(&dbiterator); |
15158 | | |
15159 | | /* |
15160 | | * Call restore_nsec3param() to create private-type records from |
15161 | | * the old nsec3 parameters and insert them into db |
15162 | | */ |
15163 | 0 | if (!ISC_LIST_EMPTY(nsec3list)) { |
15164 | 0 | CHECK(restore_nsec3param(zone, db, version, &nsec3list)); |
15165 | 0 | } |
15166 | | |
15167 | 0 | dns_db_closeversion(db, &version, true); |
15168 | 0 | *dbp = MOVE_OWNERSHIP(db); |
15169 | |
|
15170 | 0 | cleanup: |
15171 | 0 | if (dbiterator != NULL) { |
15172 | 0 | dns_dbiterator_destroy(&dbiterator); |
15173 | 0 | } |
15174 | 0 | while (!ISC_LIST_EMPTY(nsec3list)) { |
15175 | 0 | nsec3param_t *nsec3p; |
15176 | 0 | nsec3p = ISC_LIST_HEAD(nsec3list); |
15177 | 0 | ISC_LIST_UNLINK(nsec3list, nsec3p, link); |
15178 | 0 | isc_mem_put(zone->mctx, nsec3p, sizeof(nsec3param_t)); |
15179 | 0 | } |
15180 | 0 | if (db != NULL) { |
15181 | 0 | if (version != NULL) { |
15182 | 0 | dns_db_closeversion(db, &version, false); |
15183 | 0 | } |
15184 | 0 | dns_db_detach(&db); |
15185 | 0 | } |
15186 | |
|
15187 | 0 | INSIST(version == NULL); |
15188 | 0 | return result; |
15189 | 0 | } |
15190 | | |
15191 | | isc_result_t |
15192 | 0 | dns_zone_replacedb(dns_zone_t *zone, dns_db_t *db, bool dump) { |
15193 | 0 | isc_result_t result; |
15194 | 0 | dns_zone_t *secure = NULL; |
15195 | |
|
15196 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
15197 | 0 | again: |
15198 | 0 | LOCK_ZONE(zone); |
15199 | 0 | if (dns__zone_inline_raw(zone)) { |
15200 | 0 | secure = zone->secure; |
15201 | 0 | INSIST(secure != zone); |
15202 | 0 | TRYLOCK_ZONE(result, secure); |
15203 | 0 | if (result != ISC_R_SUCCESS) { |
15204 | 0 | UNLOCK_ZONE(zone); |
15205 | 0 | secure = NULL; |
15206 | 0 | isc_thread_yield(); |
15207 | 0 | goto again; |
15208 | 0 | } |
15209 | 0 | } |
15210 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_write); |
15211 | 0 | result = zone_replacedb(zone, db, dump); |
15212 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_write); |
15213 | 0 | if (secure != NULL) { |
15214 | 0 | UNLOCK_ZONE(secure); |
15215 | 0 | } |
15216 | 0 | UNLOCK_ZONE(zone); |
15217 | 0 | return result; |
15218 | 0 | } |
15219 | | |
15220 | | static isc_result_t |
15221 | 0 | zone_replacedb(dns_zone_t *zone, dns_db_t *db, bool dump) { |
15222 | 0 | dns_dbversion_t *ver; |
15223 | 0 | isc_result_t result; |
15224 | 0 | unsigned int soacount = 0; |
15225 | 0 | unsigned int nscount = 0; |
15226 | | |
15227 | | /* |
15228 | | * 'zone' and 'zone->db' locked by caller. |
15229 | | */ |
15230 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
15231 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
15232 | 0 | if (dns__zone_inline_raw(zone)) { |
15233 | 0 | REQUIRE(LOCKED_ZONE(zone->secure)); |
15234 | 0 | } |
15235 | |
|
15236 | 0 | result = zone_get_from_db(zone, db, &nscount, &soacount, NULL, NULL, |
15237 | 0 | NULL, NULL, NULL, NULL, NULL); |
15238 | 0 | if (result == ISC_R_SUCCESS) { |
15239 | 0 | if (soacount != 1) { |
15240 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, "has %d SOA records", |
15241 | 0 | soacount); |
15242 | 0 | result = DNS_R_BADZONE; |
15243 | 0 | } |
15244 | 0 | if (nscount == 0 && zone->type != dns_zone_key) { |
15245 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, "has no NS records"); |
15246 | 0 | result = DNS_R_BADZONE; |
15247 | 0 | } |
15248 | 0 | if (result != ISC_R_SUCCESS) { |
15249 | 0 | return result; |
15250 | 0 | } |
15251 | 0 | } else { |
15252 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
15253 | 0 | "retrieving SOA and NS records failed: %s", |
15254 | 0 | isc_result_totext(result)); |
15255 | 0 | return result; |
15256 | 0 | } |
15257 | | |
15258 | 0 | RETERR(check_nsec3param(zone, db)); |
15259 | |
|
15260 | 0 | ver = NULL; |
15261 | 0 | dns_db_currentversion(db, &ver); |
15262 | | |
15263 | | /* |
15264 | | * The initial version of a secondary zone is always dumped; |
15265 | | * subsequent versions may be journaled instead if this |
15266 | | * is enabled in the configuration. |
15267 | | */ |
15268 | 0 | if (zone->db != NULL && zone->journal != NULL && |
15269 | 0 | DNS_ZONE_OPTION(zone, DNS_ZONEOPT_IXFRFROMDIFFS) && |
15270 | 0 | !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER)) |
15271 | 0 | { |
15272 | 0 | uint32_t serial, oldserial; |
15273 | |
|
15274 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), "generating diffs"); |
15275 | |
|
15276 | 0 | result = dns_db_getsoaserial(db, ver, &serial); |
15277 | 0 | if (result != ISC_R_SUCCESS) { |
15278 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
15279 | 0 | "ixfr-from-differences: unable to get " |
15280 | 0 | "new serial"); |
15281 | 0 | goto fail; |
15282 | 0 | } |
15283 | | |
15284 | | /* |
15285 | | * This is checked in zone_postload() for primary zones. |
15286 | | */ |
15287 | 0 | result = zone_get_from_db(zone, zone->db, NULL, &soacount, NULL, |
15288 | 0 | &oldserial, NULL, NULL, NULL, NULL, |
15289 | 0 | NULL); |
15290 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
15291 | 0 | RUNTIME_CHECK(soacount > 0U); |
15292 | 0 | if ((zone->type == dns_zone_secondary || |
15293 | 0 | (zone->type == dns_zone_redirect && |
15294 | 0 | dns_remote_addresses(&zone->primaries) != NULL)) && |
15295 | 0 | !isc_serial_gt(serial, oldserial)) |
15296 | 0 | { |
15297 | 0 | uint32_t serialmin, serialmax; |
15298 | 0 | serialmin = (oldserial + 1) & 0xffffffffU; |
15299 | 0 | serialmax = (oldserial + 0x7fffffffU) & 0xffffffffU; |
15300 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
15301 | 0 | "ixfr-from-differences: failed: " |
15302 | 0 | "new serial (%u) out of range [%u - %u]", |
15303 | 0 | serial, serialmin, serialmax); |
15304 | 0 | result = ISC_R_RANGE; |
15305 | 0 | goto fail; |
15306 | 0 | } |
15307 | | |
15308 | 0 | result = dns_db_diff(zone->mctx, db, ver, zone->db, NULL, |
15309 | 0 | zone->journal); |
15310 | 0 | if (result != ISC_R_SUCCESS) { |
15311 | 0 | char strbuf[ISC_STRERRORSIZE]; |
15312 | 0 | strerror_r(errno, strbuf, sizeof(strbuf)); |
15313 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
15314 | 0 | "ixfr-from-differences: failed: " |
15315 | 0 | "%s", |
15316 | 0 | strbuf); |
15317 | 0 | goto fallback; |
15318 | 0 | } |
15319 | 0 | if (dump) { |
15320 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
15321 | 0 | } else { |
15322 | 0 | zone_journal_compact(zone, zone->db, serial); |
15323 | 0 | } |
15324 | 0 | if (zone->type == dns_zone_primary && |
15325 | 0 | dns__zone_inline_raw(zone)) |
15326 | 0 | { |
15327 | 0 | zone_schedule_inline_sync(zone->secure, |
15328 | 0 | inline_sync_incremental); |
15329 | 0 | } |
15330 | 0 | } else { |
15331 | 0 | fallback: |
15332 | 0 | if (dump && zone->masterfile != NULL) { |
15333 | | /* |
15334 | | * If DNS_ZONEFLG_FORCEXFER was set we don't want |
15335 | | * to keep the old masterfile. |
15336 | | */ |
15337 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER) && |
15338 | 0 | remove(zone->masterfile) < 0 && errno != ENOENT) |
15339 | 0 | { |
15340 | 0 | char strbuf[ISC_STRERRORSIZE]; |
15341 | 0 | strerror_r(errno, strbuf, sizeof(strbuf)); |
15342 | 0 | isc_log_write(DNS_LOGCATEGORY_GENERAL, |
15343 | 0 | DNS_LOGMODULE_ZONE, |
15344 | 0 | ISC_LOG_WARNING, |
15345 | 0 | "unable to remove masterfile " |
15346 | 0 | "'%s': '%s'", |
15347 | 0 | zone->masterfile, strbuf); |
15348 | 0 | } |
15349 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED) == 0) { |
15350 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NODELAY); |
15351 | 0 | } else { |
15352 | 0 | zone_needdump(zone, 0); |
15353 | 0 | } |
15354 | 0 | } |
15355 | 0 | if (dump && zone->journal != NULL) { |
15356 | | /* |
15357 | | * The in-memory database just changed, and |
15358 | | * because 'dump' is set, it didn't change by |
15359 | | * being loaded from disk. Also, we have not |
15360 | | * journaled diffs for this change. |
15361 | | * Therefore, the on-disk journal is missing |
15362 | | * the deltas for this change. Since it can |
15363 | | * no longer be used to bring the zone |
15364 | | * up-to-date, it is useless and should be |
15365 | | * removed. |
15366 | | */ |
15367 | 0 | isc_log_write(DNS_LOGCATEGORY_GENERAL, |
15368 | 0 | DNS_LOGMODULE_ZONE, ISC_LOG_DEBUG(3), |
15369 | 0 | "removing journal file"); |
15370 | 0 | if (remove(zone->journal) < 0 && errno != ENOENT) { |
15371 | 0 | char strbuf[ISC_STRERRORSIZE]; |
15372 | 0 | strerror_r(errno, strbuf, sizeof(strbuf)); |
15373 | 0 | isc_log_write(DNS_LOGCATEGORY_GENERAL, |
15374 | 0 | DNS_LOGMODULE_ZONE, |
15375 | 0 | ISC_LOG_WARNING, |
15376 | 0 | "unable to remove journal " |
15377 | 0 | "'%s': '%s'", |
15378 | 0 | zone->journal, strbuf); |
15379 | 0 | } |
15380 | 0 | } |
15381 | |
|
15382 | 0 | if (dns__zone_inline_raw(zone)) { |
15383 | 0 | zone_schedule_inline_sync(zone->secure, |
15384 | 0 | inline_sync_full); |
15385 | 0 | } |
15386 | 0 | } |
15387 | | |
15388 | 0 | dns_db_closeversion(db, &ver, false); |
15389 | |
|
15390 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), "replacing zone database"); |
15391 | |
|
15392 | 0 | if (zone->db != NULL) { |
15393 | 0 | zone_detachdb(zone); |
15394 | 0 | } |
15395 | 0 | zone_attachdb(zone, db); |
15396 | 0 | dns_db_setmaxrrperset(zone->db, zone->maxrrperset); |
15397 | 0 | dns_db_setmaxtypepername(zone->db, zone->maxtypepername); |
15398 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED | DNS_ZONEFLG_NEEDNOTIFY); |
15399 | 0 | return ISC_R_SUCCESS; |
15400 | | |
15401 | 0 | fail: |
15402 | 0 | dns_db_closeversion(db, &ver, false); |
15403 | 0 | return result; |
15404 | 0 | } |
15405 | | |
15406 | | /* The caller must hold the dblock as a writer. */ |
15407 | | static void |
15408 | 2 | zone_attachdb(dns_zone_t *zone, dns_db_t *db) { |
15409 | 2 | REQUIRE(zone->db == NULL && db != NULL); |
15410 | | |
15411 | 2 | dns_db_attach(db, &zone->db); |
15412 | 2 | } |
15413 | | |
15414 | | /* The caller must hold the dblock as a writer. */ |
15415 | | static void |
15416 | 0 | zone_detachdb(dns_zone_t *zone) { |
15417 | 0 | REQUIRE(zone->db != NULL); |
15418 | |
|
15419 | 0 | dns_zone_rpz_disable_db(zone, zone->db); |
15420 | 0 | dns_zone_catz_disable_db(zone, zone->db); |
15421 | 0 | dns_db_detach(&zone->db); |
15422 | 0 | } |
15423 | | |
15424 | | void |
15425 | 0 | dns__zone_xfrdone(dns_zone_t *zone, uint32_t *expireopt, isc_result_t result) { |
15426 | 0 | isc_time_t now, expiretime; |
15427 | 0 | bool again = false; |
15428 | 0 | unsigned int soacount; |
15429 | 0 | unsigned int nscount; |
15430 | 0 | uint32_t serial, refresh, retry, expire, minimum, soattl, oldexpire; |
15431 | 0 | isc_result_t xfrresult = result; |
15432 | 0 | bool free_needed; |
15433 | 0 | dns_zone_t *secure = NULL; |
15434 | |
|
15435 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
15436 | |
|
15437 | 0 | dns_zone_logc( |
15438 | 0 | zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_DEBUG(1), |
15439 | 0 | expireopt == NULL ? "zone transfer finished: %s" |
15440 | 0 | : "zone transfer finished: %s, expire=%u", |
15441 | 0 | isc_result_totext(result), expireopt != NULL ? *expireopt : 0); |
15442 | | |
15443 | | /* |
15444 | | * Obtaining a lock on the zone->secure (see zone_schedule_inline_sync) |
15445 | | * could result in a deadlock due to a LOR so we will spin if we |
15446 | | * can't obtain both locks. |
15447 | | */ |
15448 | 0 | again: |
15449 | 0 | LOCK_ZONE(zone); |
15450 | 0 | if (dns__zone_inline_raw(zone)) { |
15451 | 0 | secure = zone->secure; |
15452 | 0 | INSIST(secure != zone); |
15453 | 0 | TRYLOCK_ZONE(result, secure); |
15454 | 0 | if (result != ISC_R_SUCCESS) { |
15455 | 0 | UNLOCK_ZONE(zone); |
15456 | 0 | secure = NULL; |
15457 | 0 | isc_thread_yield(); |
15458 | 0 | goto again; |
15459 | 0 | } |
15460 | 0 | } |
15461 | | |
15462 | 0 | INSIST(DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH)); |
15463 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_REFRESH); |
15464 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_SOABEFOREAXFR); |
15465 | |
|
15466 | 0 | now = isc_time_now(); |
15467 | 0 | switch (xfrresult) { |
15468 | 0 | case ISC_R_SUCCESS: |
15469 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY); |
15470 | 0 | FALLTHROUGH; |
15471 | 0 | case DNS_R_UPTODATE: |
15472 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_FORCEXFER | |
15473 | 0 | DNS_ZONEFLG_FIRSTREFRESH); |
15474 | | /* |
15475 | | * Has the zone expired underneath us? |
15476 | | */ |
15477 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
15478 | 0 | if (zone->db == NULL) { |
15479 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
15480 | 0 | goto same_primary; |
15481 | 0 | } |
15482 | | |
15483 | 0 | oldexpire = zone->expire; |
15484 | | |
15485 | | /* |
15486 | | * Update the zone structure's data from the actual |
15487 | | * SOA received. |
15488 | | */ |
15489 | 0 | nscount = 0; |
15490 | 0 | soacount = 0; |
15491 | 0 | INSIST(zone->db != NULL); |
15492 | 0 | result = zone_get_from_db(zone, zone->db, &nscount, &soacount, |
15493 | 0 | &soattl, &serial, &refresh, &retry, |
15494 | 0 | &expire, &minimum, NULL); |
15495 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
15496 | 0 | if (result == ISC_R_SUCCESS) { |
15497 | 0 | if (soacount != 1) { |
15498 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
15499 | 0 | ISC_LOG_ERROR, |
15500 | 0 | "transferred zone " |
15501 | 0 | "has %d SOA records", |
15502 | 0 | soacount); |
15503 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HAVETIMERS)) |
15504 | 0 | { |
15505 | 0 | zone->refresh = DNS_ZONE_DEFAULTREFRESH; |
15506 | 0 | zone->retry = DNS_ZONE_DEFAULTRETRY; |
15507 | 0 | } |
15508 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HAVETIMERS); |
15509 | 0 | zone_unload(zone); |
15510 | 0 | goto next_primary; |
15511 | 0 | } |
15512 | 0 | if (nscount == 0) { |
15513 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
15514 | 0 | ISC_LOG_ERROR, |
15515 | 0 | "transferred zone " |
15516 | 0 | "has no NS records"); |
15517 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_HAVETIMERS)) |
15518 | 0 | { |
15519 | 0 | zone->refresh = DNS_ZONE_DEFAULTREFRESH; |
15520 | 0 | zone->retry = DNS_ZONE_DEFAULTRETRY; |
15521 | 0 | } |
15522 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_HAVETIMERS); |
15523 | 0 | zone_unload(zone); |
15524 | 0 | goto next_primary; |
15525 | 0 | } |
15526 | 0 | zone->refresh = RANGE(refresh, zone->minrefresh, |
15527 | 0 | zone->maxrefresh); |
15528 | 0 | zone->retry = RANGE(retry, zone->minretry, |
15529 | 0 | zone->maxretry); |
15530 | 0 | zone->expire = RANGE(expire, |
15531 | 0 | zone->refresh + zone->retry, |
15532 | 0 | DNS_MAX_EXPIRE); |
15533 | 0 | zone->soattl = soattl; |
15534 | 0 | zone->minimum = minimum; |
15535 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_HAVETIMERS); |
15536 | 0 | } |
15537 | | |
15538 | | /* |
15539 | | * Set our next refresh time. |
15540 | | */ |
15541 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDREFRESH)) { |
15542 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDREFRESH); |
15543 | 0 | zone->refreshtime = now; |
15544 | 0 | } else { |
15545 | 0 | DNS_ZONE_JITTER_ADD(&now, zone->refresh, |
15546 | 0 | &zone->refreshtime); |
15547 | 0 | } |
15548 | | |
15549 | | /* |
15550 | | * Set our next expire time. If the parent returned |
15551 | | * an EXPIRE option use that to update zone->expiretime. |
15552 | | */ |
15553 | 0 | expire = zone->expire; |
15554 | 0 | if (expireopt != NULL && *expireopt < expire) { |
15555 | 0 | expire = *expireopt; |
15556 | 0 | } |
15557 | 0 | DNS_ZONE_TIME_ADD(&now, expire, &expiretime); |
15558 | 0 | if (oldexpire != zone->expire || |
15559 | 0 | isc_time_compare(&expiretime, &zone->expiretime) > 0) |
15560 | 0 | { |
15561 | 0 | zone->expiretime = expiretime; |
15562 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_EXPIRED); |
15563 | 0 | } |
15564 | | |
15565 | | /* |
15566 | | * Set loadtime. |
15567 | | */ |
15568 | 0 | zone->loadtime = now; |
15569 | |
|
15570 | 0 | if (result == ISC_R_SUCCESS && xfrresult == ISC_R_SUCCESS) { |
15571 | 0 | char buf[DNS_NAME_FORMATSIZE + sizeof(": TSIG ''")]; |
15572 | 0 | if (zone->tsigkey != NULL) { |
15573 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
15574 | 0 | dns_name_format(zone->tsigkey->name, namebuf, |
15575 | 0 | sizeof(namebuf)); |
15576 | 0 | snprintf(buf, sizeof(buf), ": TSIG '%s'", |
15577 | 0 | namebuf); |
15578 | 0 | } else { |
15579 | 0 | buf[0] = '\0'; |
15580 | 0 | } |
15581 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
15582 | 0 | ISC_LOG_INFO, "transferred serial %u%s", |
15583 | 0 | serial, buf); |
15584 | 0 | if (dns__zone_inline_raw(zone)) { |
15585 | 0 | zone_schedule_inline_sync( |
15586 | 0 | secure, inline_sync_incremental); |
15587 | 0 | } |
15588 | 0 | } |
15589 | | |
15590 | | /* |
15591 | | * This is not necessary if we just performed a AXFR |
15592 | | * however it is necessary for an IXFR / UPTODATE and |
15593 | | * won't hurt with an AXFR. |
15594 | | */ |
15595 | 0 | if (zone->masterfile != NULL || zone->journal != NULL) { |
15596 | 0 | unsigned int delay = DNS_DUMP_DELAY; |
15597 | 0 | isc_interval_t i; |
15598 | 0 | isc_time_t when; |
15599 | | |
15600 | | /* |
15601 | | * Compute effective modification time. |
15602 | | */ |
15603 | 0 | isc_interval_set(&i, zone->expire, 0); |
15604 | 0 | result = isc_time_subtract(&zone->expiretime, &i, |
15605 | 0 | &when); |
15606 | 0 | if (result != ISC_R_SUCCESS) { |
15607 | 0 | when = now; |
15608 | 0 | } |
15609 | |
|
15610 | 0 | result = ISC_R_FAILURE; |
15611 | 0 | if (zone->journal != NULL) { |
15612 | 0 | result = isc_file_settime(zone->journal, &when); |
15613 | 0 | } |
15614 | 0 | if (result != ISC_R_SUCCESS && zone->masterfile != NULL) |
15615 | 0 | { |
15616 | 0 | result = isc_file_settime(zone->masterfile, |
15617 | 0 | &when); |
15618 | 0 | } |
15619 | |
|
15620 | 0 | if ((DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NODELAY) != 0) || |
15621 | 0 | result == ISC_R_FILENOTFOUND) |
15622 | 0 | { |
15623 | 0 | delay = 0; |
15624 | 0 | } |
15625 | |
|
15626 | 0 | if ((result == ISC_R_SUCCESS || |
15627 | 0 | result == ISC_R_FILENOTFOUND) && |
15628 | 0 | zone->masterfile != NULL) |
15629 | 0 | { |
15630 | 0 | zone_needdump(zone, delay); |
15631 | 0 | } else if (result != ISC_R_SUCCESS) { |
15632 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, |
15633 | 0 | ISC_LOG_ERROR, |
15634 | 0 | "transfer: could not set file " |
15635 | 0 | "modification time of '%s': %s", |
15636 | 0 | zone->masterfile, |
15637 | 0 | isc_result_totext(result)); |
15638 | 0 | } |
15639 | 0 | } |
15640 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NODELAY); |
15641 | 0 | dns__zone_stats_increment(zone, |
15642 | 0 | dns_zonestatscounter_xfrsuccess); |
15643 | 0 | break; |
15644 | | |
15645 | 0 | case DNS_R_BADIXFR: |
15646 | | /* Force retry with AXFR. */ |
15647 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NOIXFR); |
15648 | 0 | goto same_primary; |
15649 | | |
15650 | 0 | case DNS_R_TOOMANYRECORDS: |
15651 | 0 | case DNS_R_VERIFYFAILURE: |
15652 | 0 | DNS_ZONE_JITTER_ADD(&now, zone->refresh, &zone->refreshtime); |
15653 | 0 | dns__zone_stats_increment(zone, dns_zonestatscounter_xfrfail); |
15654 | 0 | break; |
15655 | | |
15656 | 0 | case ISC_R_SHUTTINGDOWN: |
15657 | 0 | dns_remote_reset(&zone->primaries, true); |
15658 | 0 | break; |
15659 | | |
15660 | 0 | default: |
15661 | 0 | next_primary: |
15662 | | /* |
15663 | | * Skip to next failed / untried primary. |
15664 | | */ |
15665 | 0 | dns_remote_next(&zone->primaries, true); |
15666 | 0 | same_primary: |
15667 | 0 | if (dns_remote_done(&zone->primaries)) { |
15668 | 0 | dns_remote_reset(&zone->primaries, false); |
15669 | 0 | } else { |
15670 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_REFRESH); |
15671 | 0 | again = true; |
15672 | 0 | } |
15673 | 0 | dns__zone_stats_increment(zone, dns_zonestatscounter_xfrfail); |
15674 | 0 | break; |
15675 | 0 | } |
15676 | 0 | dns__zone_settimer(zone, now); |
15677 | | |
15678 | | /* |
15679 | | * We are called as the done callback of a zone |
15680 | | * transfer object that just entered its shutting-down state or |
15681 | | * failed to start. Since we are no longer responsible for shutting |
15682 | | * it down, we can detach our reference. |
15683 | | */ |
15684 | 0 | if (zone->xfr != NULL) { |
15685 | 0 | dns_xfrin_detach(&zone->xfr); |
15686 | 0 | } |
15687 | |
|
15688 | 0 | if (zone->tsigkey != NULL) { |
15689 | 0 | dns_tsigkey_detach(&zone->tsigkey); |
15690 | 0 | } |
15691 | |
|
15692 | 0 | if (zone->transport != NULL) { |
15693 | 0 | dns_transport_detach(&zone->transport); |
15694 | 0 | } |
15695 | | |
15696 | | /* |
15697 | | * Handle any deferred journal compaction. |
15698 | | */ |
15699 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDCOMPACT)) { |
15700 | 0 | dns_db_t *db = NULL; |
15701 | 0 | if (dns_zone_getdb(zone, &db) == ISC_R_SUCCESS) { |
15702 | 0 | zone_journal_compact(zone, db, zone->compact_serial); |
15703 | 0 | dns_db_detach(&db); |
15704 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_NEEDCOMPACT); |
15705 | 0 | } |
15706 | 0 | } |
15707 | |
|
15708 | 0 | if (secure != NULL) { |
15709 | 0 | UNLOCK_ZONE(secure); |
15710 | 0 | } |
15711 | | /* |
15712 | | * This transfer finishing freed up a transfer quota slot. |
15713 | | * Let any other zones waiting for quota have it. |
15714 | | */ |
15715 | 0 | if (zone->zmgr != NULL && |
15716 | 0 | zone->statelist == &zone->zmgr->xfrin_in_progress) |
15717 | 0 | { |
15718 | 0 | UNLOCK_ZONE(zone); |
15719 | 0 | RWLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write); |
15720 | 0 | ISC_LIST_UNLINK(zone->zmgr->xfrin_in_progress, zone, statelink); |
15721 | 0 | zone->statelist = NULL; |
15722 | 0 | dns__zonemgr_resume_xfrs(zone->zmgr, false); |
15723 | 0 | RWUNLOCK(&zone->zmgr->rwlock, isc_rwlocktype_write); |
15724 | 0 | LOCK_ZONE(zone); |
15725 | 0 | } |
15726 | | |
15727 | | /* |
15728 | | * Retry with a different server if necessary. |
15729 | | */ |
15730 | 0 | if (again && !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
15731 | 0 | queue_soa_query(zone); |
15732 | 0 | } |
15733 | |
|
15734 | 0 | isc_refcount_decrement(&zone->irefs); |
15735 | 0 | free_needed = dns__zone_free_check(zone); |
15736 | 0 | UNLOCK_ZONE(zone); |
15737 | 0 | if (free_needed) { |
15738 | 0 | dns__zone_free(zone); |
15739 | 0 | } |
15740 | 0 | } |
15741 | | |
15742 | | static void |
15743 | 0 | zone_loaddone(void *arg, isc_result_t result) { |
15744 | 0 | dns_load_t *load = arg; |
15745 | 0 | dns_zone_t *zone; |
15746 | 0 | isc_result_t postload_result, tresult; |
15747 | 0 | dns_zone_t *secure = NULL; |
15748 | |
|
15749 | 0 | zone = load->zone; |
15750 | |
|
15751 | 0 | ENTER; |
15752 | | |
15753 | | /* |
15754 | | * If zone loading failed, remove the update db callbacks prior |
15755 | | * to calling the list of callbacks in the zone load structure. |
15756 | | */ |
15757 | 0 | if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) { |
15758 | 0 | dns_zone_rpz_disable_db(zone, load->db); |
15759 | 0 | dns_zone_catz_disable_db(zone, load->db); |
15760 | 0 | } |
15761 | |
|
15762 | 0 | tresult = dns_db_endload(load->db, &load->callbacks); |
15763 | 0 | if (tresult != ISC_R_SUCCESS && |
15764 | 0 | (result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE)) |
15765 | 0 | { |
15766 | 0 | result = tresult; |
15767 | 0 | } |
15768 | | |
15769 | | /* |
15770 | | * Lock hierarchy: zmgr, zone, secure. |
15771 | | */ |
15772 | 0 | again: |
15773 | 0 | LOCK_ZONE(zone); |
15774 | 0 | INSIST(zone != zone->raw); |
15775 | 0 | if (dns__zone_inline_raw(zone)) { |
15776 | 0 | secure = zone->secure; |
15777 | 0 | TRYLOCK_ZONE(tresult, secure); |
15778 | 0 | if (tresult != ISC_R_SUCCESS) { |
15779 | 0 | UNLOCK_ZONE(zone); |
15780 | 0 | secure = NULL; |
15781 | 0 | isc_thread_yield(); |
15782 | 0 | goto again; |
15783 | 0 | } |
15784 | 0 | } |
15785 | 0 | postload_result = zone_postload(zone, load->db, load->loadtime, result); |
15786 | 0 | if (postload_result == ISC_R_SUCCESS && dns__zone_inline_secure(zone)) { |
15787 | 0 | zone_schedule_inline_sync(zone, inline_sync_incremental); |
15788 | 0 | } |
15789 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADING); |
15790 | 0 | zone_idetach(&load->callbacks.zone); |
15791 | | /* |
15792 | | * Leave the zone frozen if the reload fails. |
15793 | | */ |
15794 | 0 | if ((result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE) && |
15795 | 0 | DNS_ZONE_FLAG(zone, DNS_ZONEFLG_THAW)) |
15796 | 0 | { |
15797 | 0 | zone->update_disabled = false; |
15798 | 0 | } |
15799 | 0 | DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_THAW); |
15800 | 0 | if (secure != NULL) { |
15801 | 0 | UNLOCK_ZONE(secure); |
15802 | 0 | } |
15803 | 0 | UNLOCK_ZONE(zone); |
15804 | |
|
15805 | 0 | dns_db_detach(&load->db); |
15806 | 0 | if (zone->loadctx != NULL) { |
15807 | 0 | dns_loadctx_detach(&zone->loadctx); |
15808 | 0 | } |
15809 | 0 | isc_mem_put(zone->mctx, load, sizeof(*load)); |
15810 | |
|
15811 | 0 | dns_zone_idetach(&zone); |
15812 | 0 | } |
15813 | | |
15814 | | static void |
15815 | 0 | queue_xfrin(dns_zone_t *zone) { |
15816 | 0 | isc_result_t result; |
15817 | 0 | dns_zonemgr_t *zmgr = zone->zmgr; |
15818 | |
|
15819 | 0 | ENTER; |
15820 | |
|
15821 | 0 | INSIST(zone->statelist == NULL); |
15822 | |
|
15823 | 0 | RWLOCK(&zmgr->rwlock, isc_rwlocktype_write); |
15824 | 0 | ISC_LIST_APPEND(zmgr->waiting_for_xfrin, zone, statelink); |
15825 | 0 | isc_refcount_increment0(&zone->irefs); |
15826 | 0 | zone->statelist = &zmgr->waiting_for_xfrin; |
15827 | 0 | result = dns__zonemgr_start_xfrin_ifquota(zmgr, zone); |
15828 | 0 | RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write); |
15829 | |
|
15830 | 0 | if (result == ISC_R_QUOTA) { |
15831 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
15832 | 0 | "zone transfer deferred due to quota"); |
15833 | 0 | } else if (result != ISC_R_SUCCESS) { |
15834 | 0 | dns_zone_logc(zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_ERROR, |
15835 | 0 | "starting zone transfer: %s", |
15836 | 0 | isc_result_totext(result)); |
15837 | 0 | } |
15838 | 0 | } |
15839 | | |
15840 | | /* |
15841 | | * Update forwarding support. |
15842 | | */ |
15843 | | |
15844 | | static void |
15845 | 0 | forward_destroy(dns_forward_t *forward) { |
15846 | 0 | forward->magic = 0; |
15847 | 0 | if (forward->request != NULL) { |
15848 | 0 | dns_request_destroy(&forward->request); |
15849 | 0 | } |
15850 | 0 | if (forward->msgbuf != NULL) { |
15851 | 0 | isc_buffer_free(&forward->msgbuf); |
15852 | 0 | } |
15853 | 0 | if (forward->transport != NULL) { |
15854 | 0 | dns_transport_detach(&forward->transport); |
15855 | 0 | } |
15856 | 0 | if (forward->zone != NULL) { |
15857 | 0 | LOCK(&forward->zone->lock); |
15858 | 0 | if (ISC_LINK_LINKED(forward, link)) { |
15859 | 0 | ISC_LIST_UNLINK(forward->zone->forwards, forward, link); |
15860 | 0 | } |
15861 | 0 | UNLOCK(&forward->zone->lock); |
15862 | 0 | dns_zone_idetach(&forward->zone); |
15863 | 0 | } |
15864 | 0 | isc_mem_putanddetach(&forward->mctx, forward, sizeof(*forward)); |
15865 | 0 | } |
15866 | | |
15867 | | static isc_result_t |
15868 | 0 | sendtoprimary(dns_forward_t *forward) { |
15869 | 0 | isc_result_t result; |
15870 | 0 | isc_sockaddr_t src, any; |
15871 | 0 | dns_zone_t *zone = forward->zone; |
15872 | 0 | bool tls_transport_invalid = false; |
15873 | 0 | isc_tlsctx_cache_t *zmgr_tlsctx_cache = NULL; |
15874 | |
|
15875 | 0 | LOCK_ZONE(zone); |
15876 | |
|
15877 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
15878 | 0 | UNLOCK_ZONE(zone); |
15879 | 0 | return ISC_R_CANCELED; |
15880 | 0 | } |
15881 | | |
15882 | 0 | next: |
15883 | 0 | if (forward->which >= dns_remote_count(&forward->zone->primaries)) { |
15884 | 0 | UNLOCK_ZONE(zone); |
15885 | 0 | return ISC_R_NOMORE; |
15886 | 0 | } |
15887 | | |
15888 | 0 | forward->addr = dns_remote_addr(&zone->primaries, forward->which); |
15889 | |
|
15890 | 0 | if (isc_sockaddr_disabled(&forward->addr)) { |
15891 | 0 | forward->which++; |
15892 | 0 | goto next; |
15893 | 0 | } |
15894 | | |
15895 | | /* |
15896 | | * Always use TCP regardless of whether the original update used TCP. |
15897 | | */ |
15898 | 0 | switch (isc_sockaddr_pf(&forward->addr)) { |
15899 | 0 | case PF_INET: |
15900 | 0 | isc_sockaddr_any(&any); |
15901 | 0 | src = zone->primaries.sources[forward->which]; |
15902 | 0 | if (isc_sockaddr_equal(&src, &any)) { |
15903 | 0 | src = zone->xfrsource4; |
15904 | 0 | } |
15905 | 0 | break; |
15906 | 0 | case PF_INET6: |
15907 | 0 | isc_sockaddr_any6(&any); |
15908 | 0 | src = zone->primaries.sources[forward->which]; |
15909 | 0 | if (isc_sockaddr_equal(&src, &any)) { |
15910 | 0 | src = zone->xfrsource6; |
15911 | 0 | } |
15912 | 0 | break; |
15913 | 0 | default: |
15914 | 0 | result = ISC_R_NOTIMPLEMENTED; |
15915 | 0 | goto unlock; |
15916 | 0 | } |
15917 | | |
15918 | 0 | if (forward->transport != NULL) { |
15919 | 0 | dns_transport_detach(&forward->transport); |
15920 | 0 | } |
15921 | |
|
15922 | 0 | if (dns_remote_tlsname(&zone->primaries) != NULL && |
15923 | 0 | zone->primaries.tlsnames[forward->which] != NULL) |
15924 | 0 | { |
15925 | 0 | dns_view_t *view = dns_zone_getview(zone); |
15926 | 0 | dns_name_t *tlsname = zone->primaries.tlsnames[forward->which]; |
15927 | |
|
15928 | 0 | result = dns_view_gettransport(view, DNS_TRANSPORT_TLS, tlsname, |
15929 | 0 | &forward->transport); |
15930 | |
|
15931 | 0 | if (result != ISC_R_SUCCESS) { |
15932 | | /* Log the error message when unlocked. */ |
15933 | 0 | tls_transport_invalid = true; |
15934 | 0 | goto unlock; |
15935 | 0 | } |
15936 | 0 | } |
15937 | | |
15938 | 0 | dns__zonemgr_tlsctx_attach(zone->zmgr, &zmgr_tlsctx_cache); |
15939 | 0 | const unsigned int connect_timeout = isc_nm_getprimariestimeout() / |
15940 | 0 | MS_PER_SEC; |
15941 | 0 | result = dns_request_createraw( |
15942 | 0 | forward->zone->view->requestmgr, forward->msgbuf, &src, |
15943 | 0 | &forward->addr, forward->transport, zmgr_tlsctx_cache, |
15944 | 0 | forward->options, connect_timeout, TCP_REQUEST_TIMEOUT, 0, 0, |
15945 | 0 | forward->zone->loop, forward_callback, forward, |
15946 | 0 | &forward->request); |
15947 | |
|
15948 | 0 | isc_tlsctx_cache_detach(&zmgr_tlsctx_cache); |
15949 | |
|
15950 | 0 | if (result == ISC_R_SUCCESS) { |
15951 | 0 | if (!ISC_LINK_LINKED(forward, link)) { |
15952 | 0 | ISC_LIST_APPEND(zone->forwards, forward, link); |
15953 | 0 | } |
15954 | 0 | } |
15955 | |
|
15956 | 0 | unlock: |
15957 | 0 | UNLOCK_ZONE(zone); |
15958 | |
|
15959 | 0 | if (tls_transport_invalid) { |
15960 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
15961 | 0 | "could not get TLS configuration " |
15962 | 0 | "for dynamic update: %s", |
15963 | 0 | isc_result_totext(result)); |
15964 | 0 | } |
15965 | |
|
15966 | 0 | return result; |
15967 | 0 | } |
15968 | | |
15969 | | static void |
15970 | 0 | forward_callback(void *arg) { |
15971 | 0 | dns_request_t *request = (dns_request_t *)arg; |
15972 | 0 | dns_forward_t *forward = dns_request_getarg(request); |
15973 | 0 | dns_message_t *msg = NULL; |
15974 | 0 | char primary[ISC_SOCKADDR_FORMATSIZE]; |
15975 | 0 | isc_result_t result; |
15976 | 0 | dns_zone_t *zone; |
15977 | |
|
15978 | 0 | INSIST(DNS_FORWARD_VALID(forward)); |
15979 | 0 | zone = forward->zone; |
15980 | 0 | INSIST(DNS_ZONE_VALID(zone)); |
15981 | |
|
15982 | 0 | ENTER; |
15983 | |
|
15984 | 0 | isc_sockaddr_format(&forward->addr, primary, sizeof(primary)); |
15985 | |
|
15986 | 0 | result = dns_request_getresult(request); |
15987 | 0 | if (result != ISC_R_SUCCESS) { |
15988 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
15989 | 0 | "could not forward dynamic update to %s: %s", |
15990 | 0 | primary, isc_result_totext(result)); |
15991 | 0 | goto next_primary; |
15992 | 0 | } |
15993 | | |
15994 | 0 | dns_message_create(zone->mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, |
15995 | 0 | &msg); |
15996 | |
|
15997 | 0 | result = dns_request_getresponse(request, msg, |
15998 | 0 | DNS_MESSAGEPARSE_PRESERVEORDER | |
15999 | 0 | DNS_MESSAGEPARSE_CLONEBUFFER); |
16000 | 0 | if (result != ISC_R_SUCCESS) { |
16001 | 0 | goto next_primary; |
16002 | 0 | } |
16003 | | |
16004 | | /* |
16005 | | * Unexpected opcode. |
16006 | | */ |
16007 | 0 | if (msg->opcode != dns_opcode_update) { |
16008 | 0 | char opcode[128]; |
16009 | 0 | isc_buffer_t rb; |
16010 | |
|
16011 | 0 | isc_buffer_init(&rb, opcode, sizeof(opcode)); |
16012 | 0 | (void)dns_opcode_totext(msg->opcode, &rb); |
16013 | |
|
16014 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
16015 | 0 | "forwarding dynamic update: " |
16016 | 0 | "unexpected opcode (%.*s) from %s", |
16017 | 0 | (int)rb.used, opcode, primary); |
16018 | 0 | goto next_primary; |
16019 | 0 | } |
16020 | | |
16021 | 0 | switch (msg->rcode) { |
16022 | | /* |
16023 | | * Pass these rcodes back to client. |
16024 | | */ |
16025 | 0 | case dns_rcode_noerror: |
16026 | 0 | case dns_rcode_yxdomain: |
16027 | 0 | case dns_rcode_yxrrset: |
16028 | 0 | case dns_rcode_nxrrset: |
16029 | 0 | case dns_rcode_refused: |
16030 | 0 | case dns_rcode_nxdomain: { |
16031 | 0 | char rcode[128]; |
16032 | 0 | isc_buffer_t rb; |
16033 | |
|
16034 | 0 | isc_buffer_init(&rb, rcode, sizeof(rcode)); |
16035 | 0 | (void)dns_rcode_totext(msg->rcode, &rb); |
16036 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
16037 | 0 | "forwarded dynamic update: " |
16038 | 0 | "primary %s returned: %.*s", |
16039 | 0 | primary, (int)rb.used, rcode); |
16040 | 0 | break; |
16041 | 0 | } |
16042 | | |
16043 | | /* These should not occur if the primaries/zone are valid. */ |
16044 | 0 | case dns_rcode_notzone: |
16045 | 0 | case dns_rcode_notauth: { |
16046 | 0 | char rcode[128]; |
16047 | 0 | isc_buffer_t rb; |
16048 | |
|
16049 | 0 | isc_buffer_init(&rb, rcode, sizeof(rcode)); |
16050 | 0 | (void)dns_rcode_totext(msg->rcode, &rb); |
16051 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
16052 | 0 | "forwarding dynamic update: " |
16053 | 0 | "unexpected response: primary %s returned: %.*s", |
16054 | 0 | primary, (int)rb.used, rcode); |
16055 | 0 | goto next_primary; |
16056 | 0 | } |
16057 | | |
16058 | | /* Try another server for these rcodes. */ |
16059 | 0 | case dns_rcode_formerr: |
16060 | 0 | case dns_rcode_servfail: |
16061 | 0 | case dns_rcode_notimp: |
16062 | 0 | case dns_rcode_badvers: |
16063 | 0 | default: |
16064 | 0 | goto next_primary; |
16065 | 0 | } |
16066 | | |
16067 | | /* call callback */ |
16068 | 0 | (forward->callback)(forward->callback_arg, ISC_R_SUCCESS, msg); |
16069 | 0 | msg = NULL; |
16070 | 0 | dns_request_destroy(&forward->request); |
16071 | 0 | forward_destroy(forward); |
16072 | 0 | return; |
16073 | | |
16074 | 0 | next_primary: |
16075 | 0 | if (msg != NULL) { |
16076 | 0 | dns_message_detach(&msg); |
16077 | 0 | } |
16078 | 0 | forward->which++; |
16079 | 0 | dns_request_destroy(&forward->request); |
16080 | 0 | result = sendtoprimary(forward); |
16081 | 0 | if (result != ISC_R_SUCCESS) { |
16082 | | /* call callback */ |
16083 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
16084 | 0 | "exhausted dynamic update forwarder list"); |
16085 | 0 | (forward->callback)(forward->callback_arg, result, NULL); |
16086 | 0 | forward_destroy(forward); |
16087 | 0 | } |
16088 | 0 | } |
16089 | | |
16090 | | isc_result_t |
16091 | | dns_zone_forwardupdate(dns_zone_t *zone, dns_message_t *msg, |
16092 | 0 | dns_updatecallback_t callback, void *callback_arg) { |
16093 | 0 | dns_forward_t *forward; |
16094 | 0 | isc_result_t result; |
16095 | 0 | isc_region_t *mr; |
16096 | |
|
16097 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
16098 | 0 | REQUIRE(msg != NULL); |
16099 | 0 | REQUIRE(callback != NULL); |
16100 | |
|
16101 | 0 | forward = isc_mem_get(zone->mctx, sizeof(*forward)); |
16102 | 0 | *forward = (dns_forward_t){ .callback = callback, |
16103 | 0 | .callback_arg = callback_arg, |
16104 | 0 | .options = DNS_REQUESTOPT_TCP }; |
16105 | 0 | ISC_LINK_INIT(forward, link); |
16106 | 0 | forward->magic = FORWARD_MAGIC; |
16107 | | |
16108 | | /* |
16109 | | * If we have a SIG(0) signed message we need to preserve the |
16110 | | * query id as that is included in the SIG(0) computation. |
16111 | | */ |
16112 | 0 | if (msg->sig0 != NULL) { |
16113 | 0 | forward->options |= DNS_REQUESTOPT_FIXEDID; |
16114 | 0 | } |
16115 | |
|
16116 | 0 | mr = dns_message_getrawmessage(msg); |
16117 | 0 | if (mr == NULL) { |
16118 | 0 | CLEANUP(ISC_R_UNEXPECTEDEND); |
16119 | 0 | } |
16120 | | |
16121 | 0 | isc_buffer_allocate(zone->mctx, &forward->msgbuf, mr->length); |
16122 | 0 | CHECK(isc_buffer_copyregion(forward->msgbuf, mr)); |
16123 | |
|
16124 | 0 | isc_mem_attach(zone->mctx, &forward->mctx); |
16125 | 0 | dns_zone_iattach(zone, &forward->zone); |
16126 | 0 | result = sendtoprimary(forward); |
16127 | |
|
16128 | 0 | cleanup: |
16129 | 0 | if (result != ISC_R_SUCCESS) { |
16130 | 0 | forward_destroy(forward); |
16131 | 0 | } |
16132 | 0 | return result; |
16133 | 0 | } |
16134 | | |
16135 | | static isc_mutex_t * |
16136 | 0 | zone_keymgmt_getlock(dns_zone_t *zone) { |
16137 | 0 | uint32_t hash = dns_name_hash(&zone->origin); |
16138 | 0 | return &keymgmt_buckets_g[hash % ARRAY_SIZE(keymgmt_buckets_g)].lock; |
16139 | 0 | } |
16140 | | |
16141 | | void |
16142 | 22 | dns__zone_keymgmt_initialize(void) { |
16143 | 22.5k | for (size_t idx = 0; idx < ARRAY_SIZE(keymgmt_buckets_g); ++idx) { |
16144 | 22.5k | isc_mutex_init(&keymgmt_buckets_g[idx].lock); |
16145 | 22.5k | } |
16146 | 22 | } |
16147 | | |
16148 | | void |
16149 | 0 | dns__zone_keymgmt_shutdown(void) { |
16150 | 0 | for (size_t idx = 0; idx < ARRAY_SIZE(keymgmt_buckets_g); ++idx) { |
16151 | 0 | isc_mutex_destroy(&keymgmt_buckets_g[idx].lock); |
16152 | 0 | } |
16153 | 0 | } |
16154 | | |
16155 | | static void |
16156 | 0 | zone_saveunique(dns_zone_t *zone, const char *path, const char *templat) { |
16157 | 0 | char *buf; |
16158 | 0 | int buflen; |
16159 | 0 | isc_result_t result; |
16160 | |
|
16161 | 0 | buflen = strlen(path) + strlen(templat) + 2; |
16162 | |
|
16163 | 0 | buf = isc_mem_get(zone->mctx, buflen); |
16164 | |
|
16165 | 0 | CHECK(isc_file_template(path, templat, buf, buflen)); |
16166 | |
|
16167 | 0 | CHECK(isc_file_renameunique(path, buf)); |
16168 | |
|
16169 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
16170 | 0 | "unable to load from '%s'; " |
16171 | 0 | "renaming file to '%s' for failure analysis and " |
16172 | 0 | "retransferring.", |
16173 | 0 | path, buf); |
16174 | |
|
16175 | 0 | cleanup: |
16176 | 0 | isc_mem_put(zone->mctx, buf, buflen); |
16177 | 0 | } |
16178 | | |
16179 | | void |
16180 | 0 | dns_zone_stopxfr(dns_zone_t *zone) { |
16181 | 0 | dns_xfrin_t *xfr = NULL; |
16182 | |
|
16183 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
16184 | |
|
16185 | 0 | RWLOCK(&zone->zmgr->rwlock, isc_rwlocktype_read); |
16186 | 0 | LOCK_ZONE(zone); |
16187 | 0 | if (zone->statelist == &zone->zmgr->xfrin_in_progress && |
16188 | 0 | zone->xfr != NULL) |
16189 | 0 | { |
16190 | 0 | dns_xfrin_attach(zone->xfr, &xfr); |
16191 | 0 | } |
16192 | 0 | UNLOCK_ZONE(zone); |
16193 | 0 | RWUNLOCK(&zone->zmgr->rwlock, isc_rwlocktype_read); |
16194 | |
|
16195 | 0 | if (xfr != NULL) { |
16196 | 0 | dns_xfrin_shutdown(xfr); |
16197 | 0 | dns_xfrin_detach(&xfr); |
16198 | 0 | } |
16199 | 0 | } |
16200 | | |
16201 | | void |
16202 | 0 | dns_zone_forcexfr(dns_zone_t *zone) { |
16203 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
16204 | |
|
16205 | 0 | if (zone->type == dns_zone_primary || |
16206 | 0 | (zone->type == dns_zone_redirect && |
16207 | 0 | dns_remote_addresses(&zone->primaries) == NULL)) |
16208 | 0 | { |
16209 | 0 | return; |
16210 | 0 | } |
16211 | | |
16212 | 0 | LOCK_ZONE(zone); |
16213 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_FORCEXFER); |
16214 | 0 | UNLOCK_ZONE(zone); |
16215 | 0 | dns_zone_refresh(zone); |
16216 | 0 | } |
16217 | | |
16218 | | bool |
16219 | 0 | dns_zone_isforced(dns_zone_t *zone) { |
16220 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
16221 | |
|
16222 | 0 | return DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FORCEXFER); |
16223 | 0 | } |
16224 | | |
16225 | | static void |
16226 | 2 | zone_namerd_tostr(dns_zone_t *zone, char *buf, size_t length) { |
16227 | 2 | isc_result_t result = ISC_R_FAILURE; |
16228 | 2 | isc_buffer_t buffer; |
16229 | | |
16230 | 2 | REQUIRE(buf != NULL); |
16231 | 2 | REQUIRE(length > 1U); |
16232 | | |
16233 | | /* |
16234 | | * Leave space for terminating '\0'. |
16235 | | */ |
16236 | 2 | isc_buffer_init(&buffer, buf, (unsigned int)length - 1); |
16237 | 2 | if (zone->type != dns_zone_redirect && zone->type != dns_zone_key) { |
16238 | 2 | if (dns_name_dynamic(&zone->origin)) { |
16239 | 2 | result = dns_name_totext( |
16240 | 2 | &zone->origin, DNS_NAME_OMITFINALDOT, &buffer); |
16241 | 2 | } |
16242 | 2 | if (result != ISC_R_SUCCESS && |
16243 | 0 | isc_buffer_availablelength(&buffer) >= |
16244 | 0 | (sizeof("<UNKNOWN>") - 1)) |
16245 | 0 | { |
16246 | 0 | isc_buffer_putstr(&buffer, "<UNKNOWN>"); |
16247 | 0 | } |
16248 | | |
16249 | 2 | if (isc_buffer_availablelength(&buffer) > 0) { |
16250 | 2 | isc_buffer_putstr(&buffer, "/"); |
16251 | 2 | } |
16252 | 2 | (void)dns_rdataclass_totext(zone->rdclass, &buffer); |
16253 | 2 | } |
16254 | | |
16255 | 2 | if (zone->view != NULL && strcmp(zone->view->name, "_bind") != 0 && |
16256 | 2 | strcmp(zone->view->name, "_default") != 0 && |
16257 | 2 | strlen(zone->view->name) < isc_buffer_availablelength(&buffer)) |
16258 | 2 | { |
16259 | 2 | isc_buffer_putstr(&buffer, "/"); |
16260 | 2 | isc_buffer_putstr(&buffer, zone->view->name); |
16261 | 2 | } |
16262 | 2 | if (dns__zone_inline_secure(zone) && |
16263 | 0 | 9U < isc_buffer_availablelength(&buffer)) |
16264 | 0 | { |
16265 | 0 | isc_buffer_putstr(&buffer, " (signed)"); |
16266 | 0 | } |
16267 | 2 | if (dns__zone_inline_raw(zone) && |
16268 | 0 | 11U < isc_buffer_availablelength(&buffer)) |
16269 | 0 | { |
16270 | 0 | isc_buffer_putstr(&buffer, " (unsigned)"); |
16271 | 0 | } |
16272 | | |
16273 | 2 | buf[isc_buffer_usedlength(&buffer)] = '\0'; |
16274 | 2 | } |
16275 | | |
16276 | | static void |
16277 | 2 | zone_viewname_tostr(dns_zone_t *zone, char *buf, size_t length) { |
16278 | 2 | isc_buffer_t buffer; |
16279 | | |
16280 | 2 | REQUIRE(buf != NULL); |
16281 | 2 | REQUIRE(length > 1U); |
16282 | | |
16283 | | /* |
16284 | | * Leave space for terminating '\0'. |
16285 | | */ |
16286 | 2 | isc_buffer_init(&buffer, buf, (unsigned int)length - 1); |
16287 | | |
16288 | 2 | if (zone->view == NULL) { |
16289 | 0 | isc_buffer_putstr(&buffer, "_none"); |
16290 | 2 | } else if (strlen(zone->view->name) < |
16291 | 2 | isc_buffer_availablelength(&buffer)) |
16292 | 2 | { |
16293 | 2 | isc_buffer_putstr(&buffer, zone->view->name); |
16294 | 2 | } else { |
16295 | 0 | isc_buffer_putstr(&buffer, "_toolong"); |
16296 | 0 | } |
16297 | | |
16298 | 2 | buf[isc_buffer_usedlength(&buffer)] = '\0'; |
16299 | 2 | } |
16300 | | |
16301 | | isc_result_t |
16302 | | dns_zone_getxfr(dns_zone_t *zone, dns_xfrin_t **xfrp, bool *is_firstrefresh, |
16303 | | bool *is_running, bool *is_deferred, bool *is_presoa, |
16304 | 0 | bool *is_pending, bool *needs_refresh) { |
16305 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
16306 | 0 | REQUIRE(xfrp != NULL && *xfrp == NULL); |
16307 | |
|
16308 | 0 | if (zone->zmgr == NULL) { |
16309 | 0 | return ISC_R_FAILURE; |
16310 | 0 | } |
16311 | | |
16312 | | /* Reset. */ |
16313 | 0 | *is_firstrefresh = false; |
16314 | 0 | *is_running = false; |
16315 | 0 | *is_deferred = false; |
16316 | 0 | *is_presoa = false; |
16317 | 0 | *is_pending = false; |
16318 | 0 | *needs_refresh = false; |
16319 | |
|
16320 | 0 | RWLOCK(&zone->zmgr->rwlock, isc_rwlocktype_read); |
16321 | 0 | LOCK_ZONE(zone); |
16322 | 0 | *is_firstrefresh = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_FIRSTREFRESH); |
16323 | 0 | if (zone->xfr != NULL) { |
16324 | 0 | dns_xfrin_attach(zone->xfr, xfrp); |
16325 | 0 | } |
16326 | 0 | if (zone->statelist == &zone->zmgr->xfrin_in_progress) { |
16327 | 0 | *is_running = true; |
16328 | | /* |
16329 | | * The NEEDREFRESH flag is set only when a notify was received |
16330 | | * while the current zone transfer is running. |
16331 | | */ |
16332 | 0 | *needs_refresh = DNS_ZONE_FLAG(zone, DNS_ZONEFLG_NEEDREFRESH); |
16333 | 0 | } else if (zone->statelist == &zone->zmgr->waiting_for_xfrin) { |
16334 | 0 | *is_deferred = true; |
16335 | 0 | } else if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_REFRESH)) { |
16336 | 0 | if (zone->request != NULL) { |
16337 | 0 | *is_presoa = true; |
16338 | 0 | } else { |
16339 | 0 | *is_pending = true; |
16340 | 0 | } |
16341 | 0 | } else { |
16342 | | /* |
16343 | | * No operation is ongoing or pending, just check if the zone |
16344 | | * needs a refresh by looking at the refresh and expire times. |
16345 | | */ |
16346 | 0 | if (zone->type == dns_zone_secondary || |
16347 | 0 | zone->type == dns_zone_mirror || |
16348 | 0 | zone->type == dns_zone_stub) |
16349 | 0 | { |
16350 | 0 | isc_time_t now = isc_time_now(); |
16351 | 0 | if (isc_time_compare(&now, &zone->refreshtime) >= 0 || |
16352 | 0 | isc_time_compare(&now, &zone->expiretime) >= 0) |
16353 | 0 | { |
16354 | 0 | *needs_refresh = true; |
16355 | 0 | } |
16356 | 0 | } |
16357 | 0 | } |
16358 | 0 | UNLOCK_ZONE(zone); |
16359 | 0 | RWUNLOCK(&zone->zmgr->rwlock, isc_rwlocktype_read); |
16360 | |
|
16361 | 0 | return ISC_R_SUCCESS; |
16362 | 0 | } |
16363 | | |
16364 | | void |
16365 | 0 | dns_zone_lock_keyfiles(dns_zone_t *zone) { |
16366 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
16367 | |
|
16368 | 0 | if (zone->kasp == NULL) { |
16369 | | /* No need to lock, nothing is writing key files. */ |
16370 | 0 | return; |
16371 | 0 | } |
16372 | | |
16373 | 0 | isc_mutex_lock(zone_keymgmt_getlock(zone)); |
16374 | 0 | } |
16375 | | |
16376 | | void |
16377 | 0 | dns_zone_unlock_keyfiles(dns_zone_t *zone) { |
16378 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
16379 | |
|
16380 | 0 | if (zone->kasp == NULL) { |
16381 | | /* No need to lock, nothing is writing key files. */ |
16382 | 0 | return; |
16383 | 0 | } |
16384 | | |
16385 | 0 | isc_mutex_unlock(zone_keymgmt_getlock(zone)); |
16386 | 0 | } |
16387 | | |
16388 | | isc_result_t |
16389 | | dns_zone_checknames(dns_zone_t *zone, const dns_name_t *name, |
16390 | 0 | dns_rdata_t *rdata) { |
16391 | 0 | bool ok = true; |
16392 | 0 | bool fail = false; |
16393 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
16394 | 0 | char namebuf2[DNS_NAME_FORMATSIZE]; |
16395 | 0 | char typebuf[DNS_RDATATYPE_FORMATSIZE]; |
16396 | 0 | int level = ISC_LOG_WARNING; |
16397 | 0 | dns_name_t bad; |
16398 | |
|
16399 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
16400 | |
|
16401 | 0 | if (!DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNAMES) && |
16402 | 0 | rdata->type != dns_rdatatype_nsec3) |
16403 | 0 | { |
16404 | 0 | return ISC_R_SUCCESS; |
16405 | 0 | } |
16406 | | |
16407 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKNAMESFAIL) || |
16408 | 0 | rdata->type == dns_rdatatype_nsec3) |
16409 | 0 | { |
16410 | 0 | level = ISC_LOG_ERROR; |
16411 | 0 | fail = true; |
16412 | 0 | } |
16413 | |
|
16414 | 0 | ok = dns_rdata_checkowner(name, rdata->rdclass, rdata->type, true); |
16415 | 0 | if (!ok) { |
16416 | 0 | dns_name_format(name, namebuf, sizeof(namebuf)); |
16417 | 0 | dns_rdatatype_format(rdata->type, typebuf, sizeof(typebuf)); |
16418 | 0 | dns_zone_log(zone, level, "%s/%s: %s", namebuf, typebuf, |
16419 | 0 | isc_result_totext(DNS_R_BADOWNERNAME)); |
16420 | 0 | if (fail) { |
16421 | 0 | return DNS_R_BADOWNERNAME; |
16422 | 0 | } |
16423 | 0 | } |
16424 | | |
16425 | 0 | dns_name_init(&bad); |
16426 | 0 | ok = dns_rdata_checknames(rdata, name, &bad); |
16427 | 0 | if (!ok) { |
16428 | 0 | dns_name_format(name, namebuf, sizeof(namebuf)); |
16429 | 0 | dns_name_format(&bad, namebuf2, sizeof(namebuf2)); |
16430 | 0 | dns_rdatatype_format(rdata->type, typebuf, sizeof(typebuf)); |
16431 | 0 | dns_zone_log(zone, level, "%s/%s: %s: %s ", namebuf, typebuf, |
16432 | 0 | namebuf2, isc_result_totext(DNS_R_BADNAME)); |
16433 | 0 | if (fail) { |
16434 | 0 | return DNS_R_BADNAME; |
16435 | 0 | } |
16436 | 0 | } |
16437 | | |
16438 | 0 | return ISC_R_SUCCESS; |
16439 | 0 | } |
16440 | | |
16441 | | /* |
16442 | | * Called when a dynamic update for an NSEC3PARAM record is received. |
16443 | | * |
16444 | | * If set, transform the NSEC3 salt into human-readable form so that it can be |
16445 | | * logged. Then call zone_addnsec3chain(), passing NSEC3PARAM RDATA to it. |
16446 | | */ |
16447 | | isc_result_t |
16448 | 0 | dns_zone_addnsec3chain(dns_zone_t *zone, dns_rdata_nsec3param_t *nsec3param) { |
16449 | 0 | isc_result_t result; |
16450 | 0 | char salt[255 * 2 + 1]; |
16451 | |
|
16452 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
16453 | |
|
16454 | 0 | result = dns_nsec3param_salttotext(nsec3param, salt, sizeof(salt)); |
16455 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
16456 | 0 | dnssec_log(zone, ISC_LOG_NOTICE, |
16457 | 0 | "dns_zone_addnsec3chain(hash=%u, iterations=%u, salt=%s)", |
16458 | 0 | nsec3param->hash, nsec3param->iterations, salt); |
16459 | 0 | LOCK_ZONE(zone); |
16460 | 0 | result = zone_addnsec3chain(zone, nsec3param); |
16461 | 0 | UNLOCK_ZONE(zone); |
16462 | |
|
16463 | 0 | return result; |
16464 | 0 | } |
16465 | | |
16466 | | static isc_result_t |
16467 | | zone_signwithkey(dns_zone_t *zone, dst_algorithm_t algorithm, uint16_t keyid, |
16468 | 0 | bool deleteit, bool fullsign) { |
16469 | 0 | dns_signing_t *signing = NULL; |
16470 | 0 | isc_result_t result = ISC_R_SUCCESS; |
16471 | 0 | isc_time_t now; |
16472 | 0 | dns_db_t *db = NULL; |
16473 | |
|
16474 | 0 | signing = isc_mem_get(zone->mctx, sizeof *signing); |
16475 | |
|
16476 | 0 | signing->magic = 0; |
16477 | 0 | signing->db = NULL; |
16478 | 0 | signing->dbiterator = NULL; |
16479 | 0 | signing->algorithm = algorithm; |
16480 | 0 | signing->keyid = keyid; |
16481 | 0 | signing->deleteit = deleteit; |
16482 | 0 | signing->fullsign = fullsign; |
16483 | 0 | signing->done = false; |
16484 | |
|
16485 | 0 | now = isc_time_now(); |
16486 | |
|
16487 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
16488 | 0 | if (zone->db != NULL) { |
16489 | 0 | dns_db_attach(zone->db, &db); |
16490 | 0 | } |
16491 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
16492 | |
|
16493 | 0 | if (db == NULL) { |
16494 | 0 | CLEANUP(ISC_R_NOTFOUND); |
16495 | 0 | } |
16496 | | |
16497 | 0 | dns_db_attach(db, &signing->db); |
16498 | |
|
16499 | 0 | ISC_LIST_FOREACH(zone->signing, current, link) { |
16500 | 0 | if (current->db == signing->db && |
16501 | 0 | current->algorithm == signing->algorithm && |
16502 | 0 | current->keyid == signing->keyid) |
16503 | 0 | { |
16504 | 0 | if (current->deleteit != signing->deleteit) { |
16505 | 0 | current->done = true; |
16506 | 0 | } else { |
16507 | 0 | goto cleanup; |
16508 | 0 | } |
16509 | 0 | } |
16510 | 0 | } |
16511 | | |
16512 | 0 | result = dns_db_createiterator(signing->db, 0, &signing->dbiterator); |
16513 | |
|
16514 | 0 | if (result == ISC_R_SUCCESS) { |
16515 | 0 | result = dns_dbiterator_first(signing->dbiterator); |
16516 | 0 | } |
16517 | 0 | if (result == ISC_R_SUCCESS) { |
16518 | 0 | dns_dbiterator_pause(signing->dbiterator); |
16519 | 0 | ISC_LIST_INITANDAPPEND(zone->signing, signing, link); |
16520 | 0 | signing = NULL; |
16521 | 0 | if (isc_time_isepoch(&zone->signingtime)) { |
16522 | 0 | zone->signingtime = now; |
16523 | 0 | if (zone->loop != NULL) { |
16524 | 0 | dns__zone_settimer(zone, now); |
16525 | 0 | } |
16526 | 0 | } |
16527 | 0 | } |
16528 | |
|
16529 | 0 | cleanup: |
16530 | 0 | if (signing != NULL) { |
16531 | 0 | if (signing->db != NULL) { |
16532 | 0 | dns_db_detach(&signing->db); |
16533 | 0 | } |
16534 | 0 | if (signing->dbiterator != NULL) { |
16535 | 0 | dns_dbiterator_destroy(&signing->dbiterator); |
16536 | 0 | } |
16537 | 0 | isc_mem_put(zone->mctx, signing, sizeof *signing); |
16538 | 0 | } |
16539 | 0 | if (db != NULL) { |
16540 | 0 | dns_db_detach(&db); |
16541 | 0 | } |
16542 | 0 | return result; |
16543 | 0 | } |
16544 | | |
16545 | | /* Called once; *timep should be set to the current time. */ |
16546 | | static isc_result_t |
16547 | 0 | next_keyevent(dst_key_t *key, isc_stdtime_t *timep) { |
16548 | 0 | isc_result_t result; |
16549 | 0 | isc_stdtime_t now, then = 0, event; |
16550 | 0 | int i; |
16551 | |
|
16552 | 0 | now = *timep; |
16553 | |
|
16554 | 0 | for (i = 0; i < DST_MAX_TIMES; i++) { |
16555 | 0 | result = dst_key_gettime(key, i, &event); |
16556 | 0 | if (result == ISC_R_SUCCESS && event > now && |
16557 | 0 | (then == 0 || event < then)) |
16558 | 0 | { |
16559 | 0 | then = event; |
16560 | 0 | } |
16561 | 0 | } |
16562 | |
|
16563 | 0 | if (then != 0) { |
16564 | 0 | *timep = then; |
16565 | 0 | return ISC_R_SUCCESS; |
16566 | 0 | } |
16567 | | |
16568 | 0 | return ISC_R_NOTFOUND; |
16569 | 0 | } |
16570 | | |
16571 | | static isc_result_t |
16572 | | rr_exists(dns_db_t *db, dns_dbversion_t *ver, dns_name_t *name, |
16573 | 0 | const dns_rdata_t *rdata, bool *flag) { |
16574 | 0 | dns_rdataset_t rdataset; |
16575 | 0 | dns_dbnode_t *node = NULL; |
16576 | 0 | isc_result_t result; |
16577 | |
|
16578 | 0 | dns_rdataset_init(&rdataset); |
16579 | 0 | if (rdata->type == dns_rdatatype_nsec3) { |
16580 | 0 | CHECK(dns_db_findnsec3node(db, name, false, &node)); |
16581 | 0 | } else { |
16582 | 0 | CHECK(dns_db_findnode(db, name, false, &node)); |
16583 | 0 | } |
16584 | 0 | result = dns_db_findrdataset(db, node, ver, rdata->type, 0, |
16585 | 0 | (isc_stdtime_t)0, &rdataset, NULL); |
16586 | 0 | if (result == ISC_R_NOTFOUND) { |
16587 | 0 | *flag = false; |
16588 | 0 | result = ISC_R_SUCCESS; |
16589 | 0 | goto cleanup; |
16590 | 0 | } |
16591 | | |
16592 | 0 | bool matched = false; |
16593 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
16594 | 0 | dns_rdata_t myrdata = DNS_RDATA_INIT; |
16595 | 0 | dns_rdataset_current(&rdataset, &myrdata); |
16596 | 0 | if (dns_rdata_compare(&myrdata, rdata) == 0) { |
16597 | 0 | matched = true; |
16598 | 0 | break; |
16599 | 0 | } |
16600 | 0 | } |
16601 | 0 | dns_rdataset_disassociate(&rdataset); |
16602 | 0 | *flag = matched; |
16603 | |
|
16604 | 0 | cleanup: |
16605 | 0 | if (node != NULL) { |
16606 | 0 | dns_db_detachnode(&node); |
16607 | 0 | } |
16608 | 0 | return result; |
16609 | 0 | } |
16610 | | |
16611 | | /* |
16612 | | * Add records to signal the state of signing or of key removal. |
16613 | | */ |
16614 | | static isc_result_t |
16615 | | add_signing_records(dns_db_t *db, dns_rdatatype_t privatetype, |
16616 | 0 | dns_dbversion_t *ver, dns_diff_t *diff, bool sign_all) { |
16617 | 0 | dns_difftuple_t *newtuple = NULL; |
16618 | 0 | dns_rdata_dnskey_t dnskey; |
16619 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
16620 | 0 | bool flag; |
16621 | 0 | isc_region_t r; |
16622 | 0 | isc_result_t result = ISC_R_SUCCESS; |
16623 | 0 | uint16_t keyid; |
16624 | 0 | unsigned char data[SIGNING_RECORD_SIZE]; |
16625 | 0 | dns_name_t *name = dns_db_origin(db); |
16626 | 0 | dns_difftuplelist_t add = ISC_LIST_INITIALIZER; |
16627 | 0 | dns_difftuplelist_t del = ISC_LIST_INITIALIZER; |
16628 | 0 | dns_difftuplelist_t tuples = ISC_LIST_INITIALIZER; |
16629 | | |
16630 | | /* |
16631 | | * Move non DNSKEY and not DNSSEC DNSKEY records to tuples |
16632 | | * and sort the remaining DNSKEY records to add and del. |
16633 | | */ |
16634 | 0 | ISC_LIST_FOREACH(diff->tuples, tuple, link) { |
16635 | 0 | if (tuple->rdata.type != dns_rdatatype_dnskey) { |
16636 | 0 | ISC_LIST_UNLINK(diff->tuples, tuple, link); |
16637 | 0 | ISC_LIST_APPEND(tuples, tuple, link); |
16638 | 0 | continue; |
16639 | 0 | } |
16640 | | |
16641 | 0 | result = dns_rdata_tostruct(&tuple->rdata, &dnskey, NULL); |
16642 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
16643 | 0 | if ((dnskey.flags & DNS_KEYOWNER_ZONE) == 0) { |
16644 | 0 | ISC_LIST_UNLINK(diff->tuples, tuple, link); |
16645 | 0 | ISC_LIST_APPEND(tuples, tuple, link); |
16646 | 0 | continue; |
16647 | 0 | } |
16648 | | |
16649 | 0 | ISC_LIST_UNLINK(diff->tuples, tuple, link); |
16650 | 0 | switch (tuple->op) { |
16651 | 0 | case DNS_DIFFOP_DEL: |
16652 | 0 | case DNS_DIFFOP_DELRESIGN: |
16653 | 0 | ISC_LIST_APPEND(del, tuple, link); |
16654 | 0 | break; |
16655 | 0 | case DNS_DIFFOP_ADD: |
16656 | 0 | case DNS_DIFFOP_ADDRESIGN: |
16657 | 0 | ISC_LIST_APPEND(add, tuple, link); |
16658 | 0 | break; |
16659 | 0 | default: |
16660 | 0 | UNREACHABLE(); |
16661 | 0 | } |
16662 | 0 | } |
16663 | | |
16664 | | /* |
16665 | | * Put the tuples that don't need more processing back onto |
16666 | | * diff->tuples. |
16667 | | */ |
16668 | 0 | ISC_LIST_APPENDLIST(diff->tuples, tuples, link); |
16669 | | |
16670 | | /* |
16671 | | * Filter out DNSKEY TTL changes and put them back onto diff->tuples. |
16672 | | */ |
16673 | 0 | ISC_LIST_FOREACH(del, deltuple, link) { |
16674 | 0 | ISC_LIST_FOREACH(add, addtuple, link) { |
16675 | 0 | int n = dns_rdata_compare(&deltuple->rdata, |
16676 | 0 | &addtuple->rdata); |
16677 | 0 | if (n == 0) { |
16678 | 0 | ISC_LIST_UNLINK(del, deltuple, link); |
16679 | 0 | ISC_LIST_APPEND(diff->tuples, deltuple, link); |
16680 | 0 | ISC_LIST_UNLINK(add, addtuple, link); |
16681 | 0 | ISC_LIST_APPEND(diff->tuples, addtuple, link); |
16682 | 0 | break; |
16683 | 0 | } |
16684 | 0 | } |
16685 | 0 | } |
16686 | | |
16687 | | /* |
16688 | | * Combine any remaining DNSKEY changes together. |
16689 | | */ |
16690 | 0 | ISC_LIST_APPENDLIST(tuples, add, link); |
16691 | 0 | ISC_LIST_APPENDLIST(tuples, del, link); |
16692 | | |
16693 | | /* |
16694 | | * Add private records for keys that have been removed |
16695 | | * or added. |
16696 | | */ |
16697 | 0 | ISC_LIST_FOREACH(tuples, tuple, link) { |
16698 | 0 | dst_algorithm_t algorithm; |
16699 | 0 | dns_rdata_toregion(&tuple->rdata, &r); |
16700 | |
|
16701 | 0 | keyid = dst_region_computeid(&r); |
16702 | |
|
16703 | 0 | algorithm = dst_algorithm_fromdata(dnskey.algorithm, |
16704 | 0 | dnskey.data, dnskey.datalen); |
16705 | 0 | data[0] = dnskey.algorithm; |
16706 | 0 | data[1] = (keyid & 0xff00) >> 8; |
16707 | 0 | data[2] = (keyid & 0xff); |
16708 | 0 | data[3] = (tuple->op == DNS_DIFFOP_ADD) ? 0 : 1; |
16709 | 0 | data[4] = 0; |
16710 | 0 | data[5] = (algorithm & 0xff00) >> 8; |
16711 | 0 | data[6] = (algorithm & 0xff); |
16712 | 0 | rdata.data = data; |
16713 | 0 | rdata.length = algorithm < 256 ? OLD_SIGNING_RECORD_SIZE |
16714 | 0 | : sizeof(data); |
16715 | 0 | rdata.type = privatetype; |
16716 | 0 | rdata.rdclass = tuple->rdata.rdclass; |
16717 | |
|
16718 | 0 | if (sign_all || tuple->op == DNS_DIFFOP_DEL) { |
16719 | 0 | CHECK(rr_exists(db, ver, name, &rdata, &flag)); |
16720 | 0 | if (flag) { |
16721 | 0 | continue; |
16722 | 0 | } |
16723 | | |
16724 | 0 | dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, |
16725 | 0 | 0, &rdata, &newtuple); |
16726 | 0 | CHECK(do_one_tuple(&newtuple, db, ver, diff)); |
16727 | 0 | INSIST(newtuple == NULL); |
16728 | 0 | } |
16729 | | |
16730 | | /* |
16731 | | * Remove any record which says this operation has already |
16732 | | * completed. |
16733 | | */ |
16734 | 0 | data[4] = 1; |
16735 | 0 | CHECK(rr_exists(db, ver, name, &rdata, &flag)); |
16736 | 0 | if (flag) { |
16737 | 0 | dns_difftuple_create(diff->mctx, DNS_DIFFOP_DEL, name, |
16738 | 0 | 0, &rdata, &newtuple); |
16739 | 0 | CHECK(do_one_tuple(&newtuple, db, ver, diff)); |
16740 | 0 | INSIST(newtuple == NULL); |
16741 | 0 | } |
16742 | 0 | } |
16743 | | |
16744 | 0 | cleanup: |
16745 | | /* |
16746 | | * Put the DNSKEY changes we cared about back on diff->tuples. |
16747 | | */ |
16748 | 0 | ISC_LIST_APPENDLIST(diff->tuples, tuples, link); |
16749 | 0 | INSIST(ISC_LIST_EMPTY(add)); |
16750 | 0 | INSIST(ISC_LIST_EMPTY(del)); |
16751 | 0 | INSIST(ISC_LIST_EMPTY(tuples)); |
16752 | 0 | return result; |
16753 | 0 | } |
16754 | | |
16755 | | /* |
16756 | | * See if dns__zone_updatesigs() will update signature for RRset 'rrtype' at |
16757 | | * the apex, and if not tickle them and cause to sign so that newly activated |
16758 | | * keys are used. |
16759 | | */ |
16760 | | static isc_result_t |
16761 | | tickle_apex_rrset(dns_rdatatype_t rrtype, dns_zone_t *zone, dns_db_t *db, |
16762 | | dns_dbversion_t *ver, isc_stdtime_t now, dns_diff_t *diff, |
16763 | | dns__zonediff_t *zonediff, dst_key_t **keys, |
16764 | | unsigned int nkeys, isc_stdtime_t inception, |
16765 | 0 | isc_stdtime_t keyexpire) { |
16766 | 0 | isc_result_t result; |
16767 | 0 | bool apexsig = false; |
16768 | |
|
16769 | 0 | ISC_LIST_FOREACH(diff->tuples, tuple, link) { |
16770 | 0 | if (tuple->rdata.type == rrtype && |
16771 | 0 | dns_name_equal(&tuple->name, &zone->origin)) |
16772 | 0 | { |
16773 | 0 | apexsig = true; |
16774 | 0 | break; |
16775 | 0 | } |
16776 | 0 | } |
16777 | |
|
16778 | 0 | if (!apexsig) { |
16779 | 0 | result = del_sigs(zone, db, ver, &zone->origin, rrtype, |
16780 | 0 | zonediff, keys, nkeys, now, false); |
16781 | 0 | if (result != ISC_R_SUCCESS) { |
16782 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
16783 | 0 | "sign_apex:del_sigs -> %s", |
16784 | 0 | isc_result_totext(result)); |
16785 | 0 | return result; |
16786 | 0 | } |
16787 | 0 | result = add_sigs(db, ver, &zone->origin, zone, rrtype, |
16788 | 0 | zonediff->diff, keys, nkeys, zone->mctx, now, |
16789 | 0 | inception, keyexpire); |
16790 | 0 | if (result != ISC_R_SUCCESS) { |
16791 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
16792 | 0 | "sign_apex:add_sigs -> %s", |
16793 | 0 | isc_result_totext(result)); |
16794 | 0 | return result; |
16795 | 0 | } |
16796 | 0 | } |
16797 | | |
16798 | 0 | return ISC_R_SUCCESS; |
16799 | 0 | } |
16800 | | |
16801 | | static isc_result_t |
16802 | | sign_apex(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, |
16803 | 0 | isc_stdtime_t now, dns_diff_t *diff, dns__zonediff_t *zonediff) { |
16804 | 0 | isc_result_t result; |
16805 | 0 | isc_stdtime_t inception, soaexpire, keyexpire; |
16806 | 0 | dst_key_t *zone_keys[DNS_MAXZONEKEYS]; |
16807 | 0 | unsigned int nkeys = 0, i; |
16808 | |
|
16809 | 0 | result = dns_zone_findkeys(zone, db, ver, now, zone->mctx, |
16810 | 0 | DNS_MAXZONEKEYS, zone_keys, &nkeys); |
16811 | 0 | if (result != ISC_R_SUCCESS) { |
16812 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
16813 | 0 | "sign_apex:dns_zone_findkeys -> %s", |
16814 | 0 | isc_result_totext(result)); |
16815 | 0 | return result; |
16816 | 0 | } |
16817 | | |
16818 | 0 | inception = now - 3600; /* Allow for clock skew. */ |
16819 | 0 | soaexpire = now + dns_zone_getsigvalidityinterval(zone); |
16820 | |
|
16821 | 0 | keyexpire = dns_zone_getkeyvalidityinterval(zone); |
16822 | 0 | if (keyexpire == 0) { |
16823 | 0 | keyexpire = soaexpire - 1; |
16824 | 0 | } else { |
16825 | 0 | keyexpire += now; |
16826 | 0 | } |
16827 | | |
16828 | | /* |
16829 | | * See if dns__zone_updatesigs() will update DNSKEY/CDS/CDNSKEY |
16830 | | * signature and if not cause them to sign so that newly activated |
16831 | | * keys are used. |
16832 | | */ |
16833 | 0 | CHECK(tickle_apex_rrset(dns_rdatatype_dnskey, zone, db, ver, now, diff, |
16834 | 0 | zonediff, zone_keys, nkeys, inception, |
16835 | 0 | keyexpire)); |
16836 | 0 | CHECK(tickle_apex_rrset(dns_rdatatype_cds, zone, db, ver, now, diff, |
16837 | 0 | zonediff, zone_keys, nkeys, inception, |
16838 | 0 | keyexpire)); |
16839 | 0 | CHECK(tickle_apex_rrset(dns_rdatatype_cdnskey, zone, db, ver, now, diff, |
16840 | 0 | zonediff, zone_keys, nkeys, inception, |
16841 | 0 | keyexpire)); |
16842 | |
|
16843 | 0 | result = dns__zone_updatesigs(diff, db, ver, zone_keys, nkeys, zone, |
16844 | 0 | inception, soaexpire, keyexpire, now, |
16845 | 0 | zonediff); |
16846 | 0 | if (result != ISC_R_SUCCESS) { |
16847 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
16848 | 0 | "sign_apex:dns__zone_updatesigs -> %s", |
16849 | 0 | isc_result_totext(result)); |
16850 | 0 | } |
16851 | |
|
16852 | 0 | cleanup: |
16853 | 0 | for (i = 0; i < nkeys; i++) { |
16854 | 0 | dst_key_free(&zone_keys[i]); |
16855 | 0 | } |
16856 | 0 | return result; |
16857 | 0 | } |
16858 | | |
16859 | | static isc_result_t |
16860 | | clean_nsec3param(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, |
16861 | 0 | dns_diff_t *diff) { |
16862 | 0 | isc_result_t result; |
16863 | 0 | dns_dbnode_t *node = NULL; |
16864 | 0 | dns_rdataset_t rdataset; |
16865 | |
|
16866 | 0 | dns_rdataset_init(&rdataset); |
16867 | 0 | CHECK(dns_db_getoriginnode(db, &node)); |
16868 | |
|
16869 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey, |
16870 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
16871 | 0 | dns_rdataset_cleanup(&rdataset); |
16872 | 0 | if (result != ISC_R_NOTFOUND) { |
16873 | 0 | goto cleanup; |
16874 | 0 | } |
16875 | | |
16876 | 0 | result = dns_nsec3param_deletechains(db, ver, zone, true, diff); |
16877 | |
|
16878 | 0 | cleanup: |
16879 | 0 | if (node != NULL) { |
16880 | 0 | dns_db_detachnode(&node); |
16881 | 0 | } |
16882 | 0 | return result; |
16883 | 0 | } |
16884 | | |
16885 | | /* |
16886 | | * Given an RRSIG rdataset and an algorithm, determine whether there |
16887 | | * are any signatures using that algorithm. |
16888 | | */ |
16889 | | static bool |
16890 | 0 | signed_with_alg(dns_rdataset_t *rdataset, dst_algorithm_t alg) { |
16891 | 0 | dst_algorithm_t sigalg; |
16892 | |
|
16893 | 0 | REQUIRE(rdataset == NULL || rdataset->type == dns_rdatatype_rrsig); |
16894 | 0 | if (rdataset == NULL || !dns_rdataset_isassociated(rdataset)) { |
16895 | 0 | return false; |
16896 | 0 | } |
16897 | | |
16898 | 0 | DNS_RDATASET_FOREACH(rdataset) { |
16899 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
16900 | 0 | dns_rdata_rrsig_t rrsig; |
16901 | |
|
16902 | 0 | dns_rdataset_current(rdataset, &rdata); |
16903 | 0 | dns_rdata_tostruct(&rdata, &rrsig, NULL); |
16904 | 0 | sigalg = dst_algorithm_fromdata(rrsig.algorithm, |
16905 | 0 | rrsig.signature, rrsig.siglen); |
16906 | 0 | if (sigalg == alg) { |
16907 | 0 | return true; |
16908 | 0 | } |
16909 | 0 | } |
16910 | | |
16911 | 0 | return false; |
16912 | 0 | } |
16913 | | |
16914 | | static isc_result_t |
16915 | | add_chains(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver, |
16916 | 0 | dns_diff_t *diff) { |
16917 | 0 | dns_name_t *origin; |
16918 | 0 | bool build_nsec3; |
16919 | 0 | isc_result_t result; |
16920 | |
|
16921 | 0 | origin = dns_db_origin(db); |
16922 | 0 | CHECK(dns_private_chains(db, ver, zone->privatetype, NULL, |
16923 | 0 | &build_nsec3)); |
16924 | 0 | if (build_nsec3) { |
16925 | 0 | CHECK(dns_nsec3_addnsec3sx(db, ver, origin, zone_nsecttl(zone), |
16926 | 0 | false, zone->privatetype, diff)); |
16927 | 0 | } |
16928 | 0 | CHECK(updatesecure(db, ver, origin, zone_nsecttl(zone), true, diff)); |
16929 | |
|
16930 | 0 | cleanup: |
16931 | 0 | return result; |
16932 | 0 | } |
16933 | | |
16934 | | static void |
16935 | 0 | dnssec_report(const char *format, ...) { |
16936 | 0 | va_list args; |
16937 | 0 | va_start(args, format); |
16938 | 0 | isc_log_vwrite(DNS_LOGCATEGORY_DNSSEC, DNS_LOGMODULE_ZONE, ISC_LOG_INFO, |
16939 | 0 | format, args); |
16940 | 0 | va_end(args); |
16941 | 0 | } |
16942 | | |
16943 | | static void |
16944 | 0 | checkds_destroy(dns_checkds_t *checkds, bool locked) { |
16945 | 0 | REQUIRE(DNS_CHECKDS_VALID(checkds)); |
16946 | |
|
16947 | 0 | dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3), |
16948 | 0 | "checkds: destroy DS query"); |
16949 | |
|
16950 | 0 | if (checkds->zone != NULL) { |
16951 | 0 | if (!locked) { |
16952 | 0 | LOCK_ZONE(checkds->zone); |
16953 | 0 | } |
16954 | 0 | REQUIRE(LOCKED_ZONE(checkds->zone)); |
16955 | 0 | if (ISC_LINK_LINKED(checkds, link)) { |
16956 | 0 | ISC_LIST_UNLINK(checkds->zone->checkds_requests, |
16957 | 0 | checkds, link); |
16958 | 0 | } |
16959 | 0 | if (!locked) { |
16960 | 0 | UNLOCK_ZONE(checkds->zone); |
16961 | 0 | } |
16962 | 0 | if (locked) { |
16963 | 0 | zone_idetach(&checkds->zone); |
16964 | 0 | } else { |
16965 | 0 | dns_zone_idetach(&checkds->zone); |
16966 | 0 | } |
16967 | 0 | } |
16968 | 0 | if (checkds->find != NULL) { |
16969 | 0 | dns_adb_destroyfind(&checkds->find); |
16970 | 0 | } |
16971 | 0 | if (checkds->request != NULL) { |
16972 | 0 | dns_request_destroy(&checkds->request); |
16973 | 0 | } |
16974 | 0 | if (dns_name_dynamic(&checkds->ns)) { |
16975 | 0 | dns_name_free(&checkds->ns, checkds->mctx); |
16976 | 0 | } |
16977 | 0 | if (checkds->key != NULL) { |
16978 | 0 | dns_tsigkey_detach(&checkds->key); |
16979 | 0 | } |
16980 | 0 | if (checkds->transport != NULL) { |
16981 | 0 | dns_transport_detach(&checkds->transport); |
16982 | 0 | } |
16983 | 0 | INSIST(checkds->rlevent == NULL); |
16984 | 0 | isc_mem_putanddetach(&checkds->mctx, checkds, sizeof(*checkds)); |
16985 | 0 | } |
16986 | | |
16987 | | static isc_result_t |
16988 | | make_dnskey(dst_key_t *key, unsigned char *buf, int bufsize, |
16989 | 0 | dns_rdata_t *target) { |
16990 | 0 | isc_buffer_t b; |
16991 | 0 | isc_region_t r; |
16992 | |
|
16993 | 0 | isc_buffer_init(&b, buf, bufsize); |
16994 | 0 | RETERR(dst_key_todns(key, &b)); |
16995 | |
|
16996 | 0 | dns_rdata_reset(target); |
16997 | 0 | isc_buffer_usedregion(&b, &r); |
16998 | 0 | dns_rdata_fromregion(target, dst_key_class(key), dns_rdatatype_dnskey, |
16999 | 0 | &r); |
17000 | 0 | return ISC_R_SUCCESS; |
17001 | 0 | } |
17002 | | |
17003 | | static bool |
17004 | | do_checkds(dns_zone_t *zone, dst_key_t *key, isc_stdtime_t now, |
17005 | 0 | bool dspublish) { |
17006 | 0 | dns_kasp_t *kasp = zone->kasp; |
17007 | 0 | isc_result_t result; |
17008 | 0 | uint32_t count = 0; |
17009 | 0 | uint32_t num; |
17010 | |
|
17011 | 0 | switch (zone->checkdstype) { |
17012 | 0 | case dns_checkdstype_yes: |
17013 | 0 | num = zone->parent_nscount; |
17014 | 0 | break; |
17015 | 0 | case dns_checkdstype_explicit: |
17016 | 0 | num = dns_remote_count(&zone->parentals); |
17017 | 0 | break; |
17018 | 0 | case dns_checkdstype_no: |
17019 | 0 | default: |
17020 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
17021 | 0 | "checkds: option is disabled"); |
17022 | 0 | return false; |
17023 | 0 | } |
17024 | | |
17025 | 0 | if (dspublish) { |
17026 | 0 | (void)dst_key_getnum(key, DST_NUM_DSPUBCOUNT, &count); |
17027 | 0 | count += 1; |
17028 | 0 | dst_key_setnum(key, DST_NUM_DSPUBCOUNT, count); |
17029 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17030 | 0 | "checkds: %u DS published " |
17031 | 0 | "for key %u", |
17032 | 0 | count, dst_key_id(key)); |
17033 | |
|
17034 | 0 | if (count != num) { |
17035 | 0 | return false; |
17036 | 0 | } |
17037 | 0 | } else { |
17038 | 0 | (void)dst_key_getnum(key, DST_NUM_DSDELCOUNT, &count); |
17039 | 0 | count += 1; |
17040 | 0 | dst_key_setnum(key, DST_NUM_DSDELCOUNT, count); |
17041 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17042 | 0 | "checkds: %u DS withdrawn " |
17043 | 0 | "for key %u", |
17044 | 0 | count, dst_key_id(key)); |
17045 | |
|
17046 | 0 | if (count != num) { |
17047 | 0 | return false; |
17048 | 0 | } |
17049 | 0 | } |
17050 | | |
17051 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17052 | 0 | "checkds: checkds %s for key " |
17053 | 0 | "%u", |
17054 | 0 | dspublish ? "published" : "withdrawn", dst_key_id(key)); |
17055 | |
|
17056 | 0 | dns_zone_lock_keyfiles(zone); |
17057 | 0 | result = dns_keymgr_checkds_id(kasp, &zone->checkds_ok, now, now, |
17058 | 0 | dspublish, dst_key_id(key), |
17059 | 0 | dst_key_alg(key)); |
17060 | 0 | dns_zone_unlock_keyfiles(zone); |
17061 | |
|
17062 | 0 | if (result != ISC_R_SUCCESS) { |
17063 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
17064 | 0 | "checkds: checkds for key %u failed: %s", |
17065 | 0 | dst_key_id(key), isc_result_totext(result)); |
17066 | 0 | return false; |
17067 | 0 | } |
17068 | | |
17069 | 0 | return true; |
17070 | 0 | } |
17071 | | |
17072 | | static isc_result_t |
17073 | 0 | validate_ds(dns_zone_t *zone, dns_message_t *message) { |
17074 | 0 | UNUSED(zone); |
17075 | 0 | UNUSED(message); |
17076 | | |
17077 | | /* Get closest trust anchor */ |
17078 | | |
17079 | | /* Check that trust anchor is (grand)parent of zone. */ |
17080 | | |
17081 | | /* Find the DNSKEY signing the message. */ |
17082 | | |
17083 | | /* Check that DNSKEY is in chain of trust. */ |
17084 | | |
17085 | | /* Validate DS RRset. */ |
17086 | |
|
17087 | 0 | return ISC_R_SUCCESS; |
17088 | 0 | } |
17089 | | |
17090 | | static void |
17091 | 0 | checkds_done(void *arg) { |
17092 | 0 | dns_request_t *request = (dns_request_t *)arg; |
17093 | 0 | dns_checkds_t *checkds = dns_request_getarg(request); |
17094 | 0 | char addrbuf[ISC_SOCKADDR_FORMATSIZE]; |
17095 | 0 | char rcode[128]; |
17096 | 0 | dns_zone_t *zone = NULL; |
17097 | 0 | dns_db_t *db = NULL; |
17098 | 0 | dns_dbversion_t *version = NULL; |
17099 | 0 | dns_dnsseckeylist_t keys; |
17100 | 0 | dns_kasp_t *kasp = NULL; |
17101 | 0 | dns_message_t *message = NULL; |
17102 | 0 | dns_rdataset_t *ds_rrset = NULL; |
17103 | 0 | isc_buffer_t buf; |
17104 | 0 | isc_result_t result; |
17105 | 0 | isc_stdtime_t now; |
17106 | 0 | isc_time_t timenow; |
17107 | 0 | bool rekey = false; |
17108 | 0 | bool empty = false; |
17109 | |
|
17110 | 0 | REQUIRE(DNS_CHECKDS_VALID(checkds)); |
17111 | |
|
17112 | 0 | zone = checkds->zone; |
17113 | |
|
17114 | 0 | ISC_LIST_INIT(keys); |
17115 | |
|
17116 | 0 | kasp = zone->kasp; |
17117 | 0 | INSIST(kasp != NULL); |
17118 | |
|
17119 | 0 | isc_buffer_init(&buf, rcode, sizeof(rcode)); |
17120 | 0 | isc_sockaddr_format(&checkds->dst, addrbuf, sizeof(addrbuf)); |
17121 | |
|
17122 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), "checkds: DS query to %s: done", |
17123 | 0 | addrbuf); |
17124 | |
|
17125 | 0 | dns_message_create(zone->mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE, |
17126 | 0 | &message); |
17127 | 0 | INSIST(message != NULL); |
17128 | |
|
17129 | 0 | CHECK(dns_request_getresult(request)); |
17130 | 0 | CHECK(dns_request_getresponse(request, message, |
17131 | 0 | DNS_MESSAGEPARSE_PRESERVEORDER)); |
17132 | 0 | CHECK(dns_rcode_totext(message->rcode, &buf)); |
17133 | |
|
17134 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17135 | 0 | "checkds: DS response from %s: %.*s", addrbuf, |
17136 | 0 | (int)buf.used, rcode); |
17137 | | |
17138 | | /* Validate response. */ |
17139 | 0 | CHECK(validate_ds(zone, message)); |
17140 | | |
17141 | | /* Check RCODE. */ |
17142 | 0 | if (message->rcode != dns_rcode_noerror) { |
17143 | 0 | dns_zone_log(zone, ISC_LOG_NOTICE, |
17144 | 0 | "checkds: bad DS response from %s: %.*s", addrbuf, |
17145 | 0 | (int)buf.used, rcode); |
17146 | 0 | goto cleanup; |
17147 | 0 | } |
17148 | | |
17149 | | /* Make sure that either AA or RA bit is set. */ |
17150 | 0 | if ((message->flags & DNS_MESSAGEFLAG_AA) == 0 && |
17151 | 0 | (message->flags & DNS_MESSAGEFLAG_RA) == 0) |
17152 | 0 | { |
17153 | 0 | dns_zone_log(zone, ISC_LOG_NOTICE, |
17154 | 0 | "checkds: bad DS response from %s: expected AA or " |
17155 | 0 | "RA bit set", |
17156 | 0 | addrbuf); |
17157 | 0 | goto cleanup; |
17158 | 0 | } |
17159 | | |
17160 | | /* Lookup DS RRset. */ |
17161 | | |
17162 | 0 | MSG_SECTION_FOREACH(message, DNS_SECTION_ANSWER, name) { |
17163 | 0 | if (dns_name_compare(&zone->origin, name) != 0) { |
17164 | 0 | continue; |
17165 | 0 | } |
17166 | | |
17167 | 0 | ISC_LIST_FOREACH(name->list, rdataset, link) { |
17168 | 0 | if (rdataset->type != dns_rdatatype_ds) { |
17169 | 0 | goto next; |
17170 | 0 | } |
17171 | | |
17172 | 0 | ds_rrset = rdataset; |
17173 | 0 | break; |
17174 | 0 | } |
17175 | | |
17176 | 0 | if (ds_rrset != NULL) { |
17177 | 0 | break; |
17178 | 0 | } |
17179 | | |
17180 | 0 | next:; |
17181 | 0 | } |
17182 | | |
17183 | 0 | if (ds_rrset == NULL) { |
17184 | 0 | empty = true; |
17185 | 0 | dns_zone_log(zone, ISC_LOG_NOTICE, |
17186 | 0 | "checkds: empty DS response from %s", addrbuf); |
17187 | 0 | } |
17188 | |
|
17189 | 0 | timenow = isc_time_now(); |
17190 | 0 | now = isc_time_seconds(&timenow); |
17191 | |
|
17192 | 0 | CHECK(dns_zone_getdb(zone, &db)); |
17193 | 0 | dns_db_currentversion(db, &version); |
17194 | |
|
17195 | 0 | KASP_LOCK(kasp); |
17196 | 0 | LOCK_ZONE(zone); |
17197 | 0 | ISC_LIST_FOREACH(zone->checkds_ok, key, link) { |
17198 | 0 | bool alldone = false, found = false; |
17199 | 0 | bool checkdspub = false, checkdsdel = false, ksk = false; |
17200 | 0 | dst_key_state_t ds_state = DST_KEY_STATE_NA; |
17201 | 0 | isc_stdtime_t published = 0, withdrawn = 0; |
17202 | | |
17203 | | /* Is this key have the KSK role? */ |
17204 | 0 | (void)dst_key_role(key->key, &ksk, NULL); |
17205 | 0 | if (!ksk) { |
17206 | 0 | continue; |
17207 | 0 | } |
17208 | | |
17209 | | /* Do we need to check the DS RRset for this key? */ |
17210 | 0 | (void)dst_key_getstate(key->key, DST_KEY_DS, &ds_state); |
17211 | 0 | (void)dst_key_gettime(key->key, DST_TIME_DSPUBLISH, &published); |
17212 | 0 | (void)dst_key_gettime(key->key, DST_TIME_DSDELETE, &withdrawn); |
17213 | |
|
17214 | 0 | if (ds_state == DST_KEY_STATE_RUMOURED && published == 0) { |
17215 | 0 | checkdspub = true; |
17216 | 0 | } else if (ds_state == DST_KEY_STATE_UNRETENTIVE && |
17217 | 0 | withdrawn == 0) |
17218 | 0 | { |
17219 | 0 | checkdsdel = true; |
17220 | 0 | } |
17221 | 0 | if (!checkdspub && !checkdsdel) { |
17222 | 0 | continue; |
17223 | 0 | } |
17224 | | |
17225 | 0 | if (empty) { |
17226 | 0 | goto dswithdrawn; |
17227 | 0 | } |
17228 | | |
17229 | | /* Find the appropriate DS record. */ |
17230 | 0 | DNS_RDATASET_FOREACH(ds_rrset) { |
17231 | 0 | dns_rdata_ds_t ds; |
17232 | 0 | dns_rdata_t dnskey = DNS_RDATA_INIT; |
17233 | 0 | dns_rdata_t dsrdata = DNS_RDATA_INIT; |
17234 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
17235 | 0 | isc_result_t r; |
17236 | 0 | unsigned char dsbuf[DNS_DS_BUFFERSIZE]; |
17237 | 0 | unsigned char keybuf[DNS_RDATA_MAXLENGTH]; |
17238 | |
|
17239 | 0 | dns_rdataset_current(ds_rrset, &rdata); |
17240 | 0 | r = dns_rdata_tostruct(&rdata, &ds, NULL); |
17241 | 0 | if (r != ISC_R_SUCCESS) { |
17242 | 0 | continue; |
17243 | 0 | } |
17244 | | /* Check key tag and algorithm. */ |
17245 | 0 | if (dst_key_id(key->key) != ds.key_tag) { |
17246 | 0 | continue; |
17247 | 0 | } |
17248 | 0 | if (dst_algorithm_tosecalg(dst_key_alg(key->key)) != |
17249 | 0 | ds.algorithm) |
17250 | 0 | { |
17251 | 0 | continue; |
17252 | 0 | } |
17253 | | /* Derive DS from DNSKEY, see if the rdata is equal. */ |
17254 | 0 | make_dnskey(key->key, keybuf, sizeof(keybuf), &dnskey); |
17255 | 0 | r = dns_ds_buildrdata(&zone->origin, &dnskey, |
17256 | 0 | ds.digest_type, dsbuf, |
17257 | 0 | sizeof(dsbuf), &dsrdata); |
17258 | 0 | if (r != ISC_R_SUCCESS) { |
17259 | 0 | continue; |
17260 | 0 | } |
17261 | 0 | if (dns_rdata_compare(&rdata, &dsrdata) == 0) { |
17262 | 0 | found = true; |
17263 | 0 | if (checkdspub) { |
17264 | | /* DS Published. */ |
17265 | 0 | alldone = do_checkds(zone, key->key, |
17266 | 0 | now, true); |
17267 | 0 | if (alldone) { |
17268 | 0 | rekey = true; |
17269 | 0 | } |
17270 | 0 | } |
17271 | 0 | } |
17272 | 0 | } |
17273 | |
|
17274 | 0 | dswithdrawn: |
17275 | | /* DS withdrawn. */ |
17276 | 0 | if (checkdsdel && !found) { |
17277 | 0 | alldone = do_checkds(zone, key->key, now, false); |
17278 | 0 | if (alldone) { |
17279 | 0 | rekey = true; |
17280 | 0 | } |
17281 | 0 | } |
17282 | 0 | } |
17283 | 0 | UNLOCK_ZONE(zone); |
17284 | 0 | KASP_UNLOCK(kasp); |
17285 | | |
17286 | | /* Rekey after checkds. */ |
17287 | 0 | if (rekey) { |
17288 | 0 | dns_zone_rekey(zone, false, false); |
17289 | 0 | } |
17290 | |
|
17291 | 0 | cleanup: |
17292 | 0 | if (result != ISC_R_SUCCESS) { |
17293 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17294 | 0 | "checkds: DS request failed: %s", |
17295 | 0 | isc_result_totext(result)); |
17296 | 0 | } |
17297 | |
|
17298 | 0 | if (version != NULL) { |
17299 | 0 | dns_db_closeversion(db, &version, false); |
17300 | 0 | } |
17301 | 0 | if (db != NULL) { |
17302 | 0 | dns_db_detach(&db); |
17303 | 0 | } |
17304 | |
|
17305 | 0 | ISC_LIST_FOREACH(keys, key, link) { |
17306 | 0 | ISC_LIST_UNLINK(keys, key, link); |
17307 | 0 | dns_dnsseckey_destroy(dns_zone_getmctx(zone), &key); |
17308 | 0 | } |
17309 | |
|
17310 | 0 | checkds_destroy(checkds, false); |
17311 | 0 | dns_message_detach(&message); |
17312 | 0 | } |
17313 | | |
17314 | | static bool |
17315 | | checkds_isqueued(dns_zone_t *zone, dns_name_t *name, isc_sockaddr_t *addr, |
17316 | 0 | dns_tsigkey_t *key, dns_transport_t *transport) { |
17317 | 0 | ISC_LIST_FOREACH(zone->checkds_requests, checkds, link) { |
17318 | 0 | if (checkds->request != NULL) { |
17319 | 0 | continue; |
17320 | 0 | } |
17321 | 0 | if (name != NULL && dns_name_equal(name, &checkds->ns)) { |
17322 | 0 | return true; |
17323 | 0 | } |
17324 | 0 | if (addr != NULL && isc_sockaddr_equal(addr, &checkds->dst) && |
17325 | 0 | checkds->key == key && checkds->transport == transport) |
17326 | 0 | { |
17327 | 0 | return true; |
17328 | 0 | } |
17329 | 0 | } |
17330 | 0 | return false; |
17331 | 0 | } |
17332 | | |
17333 | | static void |
17334 | 0 | checkds_create(isc_mem_t *mctx, unsigned int flags, dns_checkds_t **checkdsp) { |
17335 | 0 | dns_checkds_t *checkds; |
17336 | |
|
17337 | 0 | REQUIRE(checkdsp != NULL && *checkdsp == NULL); |
17338 | |
|
17339 | 0 | checkds = isc_mem_get(mctx, sizeof(*checkds)); |
17340 | 0 | *checkds = (dns_checkds_t){ |
17341 | 0 | .magic = CHECKDS_MAGIC, |
17342 | 0 | .flags = flags, |
17343 | 0 | .link = ISC_LINK_INITIALIZER, |
17344 | 0 | .mctx = isc_mem_ref(mctx), |
17345 | 0 | .ns = DNS_NAME_INITEMPTY, |
17346 | 0 | }; |
17347 | |
|
17348 | 0 | isc_sockaddr_any(&checkds->dst); |
17349 | |
|
17350 | 0 | *checkdsp = checkds; |
17351 | 0 | } |
17352 | | |
17353 | | static void |
17354 | 0 | checkds_createmessage(dns_zone_t *zone, dns_message_t **messagep) { |
17355 | 0 | dns_message_t *message = NULL; |
17356 | |
|
17357 | 0 | dns_name_t *tempname = NULL; |
17358 | 0 | dns_rdataset_t *temprdataset = NULL; |
17359 | |
|
17360 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
17361 | 0 | REQUIRE(messagep != NULL && *messagep == NULL); |
17362 | |
|
17363 | 0 | dns_message_create(zone->mctx, NULL, NULL, DNS_MESSAGE_INTENTRENDER, |
17364 | 0 | &message); |
17365 | |
|
17366 | 0 | message->opcode = dns_opcode_query; |
17367 | 0 | message->rdclass = zone->rdclass; |
17368 | 0 | message->flags |= DNS_MESSAGEFLAG_RD; |
17369 | |
|
17370 | 0 | dns_message_gettempname(message, &tempname); |
17371 | |
|
17372 | 0 | dns_message_gettemprdataset(message, &temprdataset); |
17373 | | |
17374 | | /* |
17375 | | * Make question. |
17376 | | */ |
17377 | 0 | dns_name_init(tempname); |
17378 | 0 | dns_name_clone(&zone->origin, tempname); |
17379 | 0 | dns_rdataset_makequestion(temprdataset, zone->rdclass, |
17380 | 0 | dns_rdatatype_ds); |
17381 | 0 | ISC_LIST_APPEND(tempname->list, temprdataset, link); |
17382 | 0 | dns_message_addname(message, tempname, DNS_SECTION_QUESTION); |
17383 | 0 | tempname = NULL; |
17384 | 0 | temprdataset = NULL; |
17385 | |
|
17386 | 0 | *messagep = message; |
17387 | 0 | } |
17388 | | |
17389 | | /* |
17390 | | * XXXAG should check for DNS_ZONEFLG_EXITING |
17391 | | */ |
17392 | | static void |
17393 | 0 | process_checkds_adb_event(void *arg) { |
17394 | 0 | dns_adbfind_t *find = (dns_adbfind_t *)arg; |
17395 | 0 | dns_checkds_t *checkds = (dns_checkds_t *)find->cbarg; |
17396 | 0 | dns_adbstatus_t astat = find->status; |
17397 | |
|
17398 | 0 | REQUIRE(DNS_CHECKDS_VALID(checkds)); |
17399 | 0 | REQUIRE(find == checkds->find); |
17400 | |
|
17401 | 0 | switch (astat) { |
17402 | 0 | case DNS_ADB_MOREADDRESSES: |
17403 | 0 | dns_adb_destroyfind(&checkds->find); |
17404 | 0 | checkds_find_address(checkds); |
17405 | 0 | return; |
17406 | | |
17407 | 0 | case DNS_ADB_NOMOREADDRESSES: |
17408 | 0 | LOCK_ZONE(checkds->zone); |
17409 | 0 | checkds_send_tons(checkds); |
17410 | 0 | UNLOCK_ZONE(checkds->zone); |
17411 | 0 | break; |
17412 | | |
17413 | 0 | default: |
17414 | 0 | break; |
17415 | 0 | } |
17416 | | |
17417 | 0 | checkds_destroy(checkds, false); |
17418 | 0 | } |
17419 | | |
17420 | | static void |
17421 | 0 | checkds_find_address(dns_checkds_t *checkds) { |
17422 | 0 | isc_result_t result; |
17423 | 0 | unsigned int options; |
17424 | 0 | dns_adb_t *adb = NULL; |
17425 | 0 | dns_view_t *view = NULL; |
17426 | |
|
17427 | 0 | REQUIRE(DNS_CHECKDS_VALID(checkds)); |
17428 | |
|
17429 | 0 | view = checkds->zone->view; |
17430 | 0 | options = DNS_ADBFIND_WANTEVENT; |
17431 | 0 | if (isc_net_probeipv4() != ISC_R_DISABLED) { |
17432 | 0 | options |= DNS_ADBFIND_INET; |
17433 | 0 | } |
17434 | 0 | if (isc_net_probeipv6() != ISC_R_DISABLED) { |
17435 | 0 | options |= DNS_ADBFIND_INET6; |
17436 | 0 | } |
17437 | |
|
17438 | 0 | dns_view_getadb(view, &adb); |
17439 | 0 | if (adb == NULL) { |
17440 | 0 | goto destroy; |
17441 | 0 | } |
17442 | | |
17443 | 0 | result = dns_adb_createfind( |
17444 | 0 | adb, checkds->zone->loop, process_checkds_adb_event, checkds, |
17445 | 0 | &checkds->ns, options, 0, checkds->zone->view->dstport, 0, NULL, |
17446 | 0 | NULL, NULL, view->max_delegation_servers, &checkds->find, NULL); |
17447 | 0 | dns_adb_detach(&adb); |
17448 | | |
17449 | | /* Something failed? */ |
17450 | 0 | if (result != ISC_R_SUCCESS) { |
17451 | 0 | goto destroy; |
17452 | 0 | } |
17453 | | |
17454 | | /* More addresses pending? */ |
17455 | 0 | if ((checkds->find->options & DNS_ADBFIND_WANTEVENT) != 0) { |
17456 | 0 | return; |
17457 | 0 | } |
17458 | | |
17459 | | /* We have as many addresses as we can get. */ |
17460 | 0 | LOCK_ZONE(checkds->zone); |
17461 | 0 | checkds_send_tons(checkds); |
17462 | 0 | UNLOCK_ZONE(checkds->zone); |
17463 | |
|
17464 | 0 | destroy: |
17465 | 0 | checkds_destroy(checkds, false); |
17466 | 0 | } |
17467 | | |
17468 | | static void |
17469 | 0 | checkds_send_toaddr(void *arg) { |
17470 | 0 | dns_checkds_t *checkds = (dns_checkds_t *)arg; |
17471 | 0 | isc_result_t result; |
17472 | 0 | dns_message_t *message = NULL; |
17473 | 0 | isc_netaddr_t dstip; |
17474 | 0 | dns_tsigkey_t *key = NULL; |
17475 | 0 | char addrbuf[ISC_SOCKADDR_FORMATSIZE]; |
17476 | 0 | isc_sockaddr_t src; |
17477 | 0 | unsigned int options; |
17478 | 0 | bool have_checkdssource = false; |
17479 | 0 | bool canceled = checkds->rlevent->canceled; |
17480 | |
|
17481 | 0 | REQUIRE(DNS_CHECKDS_VALID(checkds)); |
17482 | |
|
17483 | 0 | isc_rlevent_free(&checkds->rlevent); |
17484 | |
|
17485 | 0 | LOCK_ZONE(checkds->zone); |
17486 | |
|
17487 | 0 | if (DNS_ZONE_FLAG(checkds->zone, DNS_ZONEFLG_LOADED) == 0 || canceled || |
17488 | 0 | DNS_ZONE_FLAG(checkds->zone, DNS_ZONEFLG_EXITING) || |
17489 | 0 | checkds->zone->view->requestmgr == NULL || |
17490 | 0 | checkds->zone->db == NULL) |
17491 | 0 | { |
17492 | 0 | CLEANUP(ISC_R_CANCELED); |
17493 | 0 | } |
17494 | | |
17495 | | /* |
17496 | | * The raw IPv4 address should also exist. Don't send to the |
17497 | | * mapped form. |
17498 | | */ |
17499 | 0 | if (isc_sockaddr_pf(&checkds->dst) == PF_INET6 && |
17500 | 0 | IN6_IS_ADDR_V4MAPPED(&checkds->dst.type.sin6.sin6_addr)) |
17501 | 0 | { |
17502 | 0 | isc_sockaddr_format(&checkds->dst, addrbuf, sizeof(addrbuf)); |
17503 | 0 | dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3), |
17504 | 0 | "checkds: ignoring IPv6 mapped IPV4 address: %s", |
17505 | 0 | addrbuf); |
17506 | 0 | CLEANUP(ISC_R_CANCELED); |
17507 | 0 | } |
17508 | | |
17509 | 0 | checkds_createmessage(checkds->zone, &message); |
17510 | |
|
17511 | 0 | isc_sockaddr_format(&checkds->dst, addrbuf, sizeof(addrbuf)); |
17512 | 0 | if (checkds->key != NULL) { |
17513 | | /* Transfer ownership of key */ |
17514 | 0 | key = checkds->key; |
17515 | 0 | checkds->key = NULL; |
17516 | 0 | } else { |
17517 | 0 | isc_netaddr_fromsockaddr(&dstip, &checkds->dst); |
17518 | 0 | result = dns_view_getpeertsig(checkds->zone->view, &dstip, |
17519 | 0 | &key); |
17520 | 0 | if (result != ISC_R_SUCCESS && result != ISC_R_NOTFOUND) { |
17521 | 0 | dns_zone_log(checkds->zone, ISC_LOG_ERROR, |
17522 | 0 | "checkds: DS query to %s not sent. " |
17523 | 0 | "Peer TSIG key lookup failure.", |
17524 | 0 | addrbuf); |
17525 | 0 | goto cleanup_message; |
17526 | 0 | } |
17527 | 0 | } |
17528 | | |
17529 | 0 | if (key != NULL) { |
17530 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
17531 | |
|
17532 | 0 | dns_name_format(key->name, namebuf, sizeof(namebuf)); |
17533 | 0 | dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3), |
17534 | 0 | "checkds: sending DS query to %s : TSIG (%s)", |
17535 | 0 | addrbuf, namebuf); |
17536 | 0 | } else { |
17537 | 0 | dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3), |
17538 | 0 | "checkds: sending DS query to %s", addrbuf); |
17539 | 0 | } |
17540 | 0 | options = 0; |
17541 | 0 | if (checkds->zone->view->peers != NULL) { |
17542 | 0 | dns_peer_t *peer = NULL; |
17543 | 0 | bool usetcp = false; |
17544 | 0 | result = dns_peerlist_peerbyaddr(checkds->zone->view->peers, |
17545 | 0 | &dstip, &peer); |
17546 | 0 | if (result == ISC_R_SUCCESS) { |
17547 | 0 | result = dns_peer_getquerysource(peer, &src); |
17548 | 0 | if (result == ISC_R_SUCCESS) { |
17549 | 0 | have_checkdssource = true; |
17550 | 0 | } |
17551 | 0 | result = dns_peer_getforcetcp(peer, &usetcp); |
17552 | 0 | if (result == ISC_R_SUCCESS && usetcp) { |
17553 | 0 | options |= DNS_FETCHOPT_TCP; |
17554 | 0 | } |
17555 | 0 | } |
17556 | 0 | } |
17557 | 0 | switch (isc_sockaddr_pf(&checkds->dst)) { |
17558 | 0 | case PF_INET: |
17559 | 0 | if (!have_checkdssource) { |
17560 | 0 | isc_sockaddr_t any; |
17561 | 0 | isc_sockaddr_any(&any); |
17562 | |
|
17563 | 0 | src = checkds->src; |
17564 | 0 | if (isc_sockaddr_equal(&src, &any)) { |
17565 | 0 | src = checkds->zone->parentalsrc4; |
17566 | 0 | } |
17567 | 0 | } |
17568 | 0 | break; |
17569 | 0 | case PF_INET6: |
17570 | 0 | if (!have_checkdssource) { |
17571 | 0 | isc_sockaddr_t any; |
17572 | 0 | isc_sockaddr_any6(&any); |
17573 | |
|
17574 | 0 | src = checkds->src; |
17575 | 0 | if (isc_sockaddr_equal(&src, &any)) { |
17576 | 0 | src = checkds->zone->parentalsrc6; |
17577 | 0 | } |
17578 | 0 | } |
17579 | 0 | break; |
17580 | 0 | default: |
17581 | 0 | result = ISC_R_NOTIMPLEMENTED; |
17582 | 0 | goto cleanup_key; |
17583 | 0 | } |
17584 | | |
17585 | 0 | dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3), |
17586 | 0 | "checkds: create request for DS query to %s", addrbuf); |
17587 | |
|
17588 | 0 | options |= DNS_REQUESTOPT_TCP; |
17589 | 0 | const unsigned int connect_timeout = isc_nm_getinitialtimeout() / |
17590 | 0 | MS_PER_SEC; |
17591 | 0 | result = dns_request_create( |
17592 | 0 | checkds->zone->view->requestmgr, message, &src, &checkds->dst, |
17593 | 0 | NULL, NULL, options, key, connect_timeout, TCP_REQUEST_TIMEOUT, |
17594 | 0 | UDP_REQUEST_TIMEOUT, UDP_REQUEST_RETRIES, checkds->zone->loop, |
17595 | 0 | checkds_done, checkds, &checkds->request); |
17596 | 0 | if (result != ISC_R_SUCCESS) { |
17597 | 0 | dns_zone_log(checkds->zone, ISC_LOG_DEBUG(3), |
17598 | 0 | "checkds: dns_request_create() to %s failed: %s", |
17599 | 0 | addrbuf, isc_result_totext(result)); |
17600 | 0 | } |
17601 | |
|
17602 | 0 | cleanup_key: |
17603 | 0 | if (key != NULL) { |
17604 | 0 | dns_tsigkey_detach(&key); |
17605 | 0 | } |
17606 | 0 | cleanup_message: |
17607 | 0 | dns_message_detach(&message); |
17608 | 0 | cleanup: |
17609 | 0 | UNLOCK_ZONE(checkds->zone); |
17610 | 0 | if (result != ISC_R_SUCCESS) { |
17611 | 0 | checkds_destroy(checkds, false); |
17612 | 0 | } |
17613 | 0 | } |
17614 | | |
17615 | | static void |
17616 | 0 | checkds_send_tons(dns_checkds_t *checkds) { |
17617 | 0 | isc_sockaddr_t dst; |
17618 | 0 | isc_result_t result; |
17619 | 0 | dns_checkds_t *newcheckds = NULL; |
17620 | 0 | dns_zone_t *zone = NULL; |
17621 | | |
17622 | | /* |
17623 | | * Zone lock held by caller. |
17624 | | */ |
17625 | 0 | REQUIRE(DNS_CHECKDS_VALID(checkds)); |
17626 | 0 | REQUIRE(LOCKED_ZONE(checkds->zone)); |
17627 | |
|
17628 | 0 | zone = checkds->zone; |
17629 | |
|
17630 | 0 | if (DNS_ZONE_FLAG(checkds->zone, DNS_ZONEFLG_EXITING)) { |
17631 | 0 | return; |
17632 | 0 | } |
17633 | | |
17634 | 0 | ISC_LIST_FOREACH(checkds->find->list, ai, publink) { |
17635 | 0 | dst = ai->sockaddr; |
17636 | 0 | if (checkds_isqueued(zone, NULL, &dst, NULL, NULL)) { |
17637 | 0 | continue; |
17638 | 0 | } |
17639 | | |
17640 | 0 | newcheckds = NULL; |
17641 | 0 | checkds_create(checkds->mctx, 0, &newcheckds); |
17642 | 0 | zone_iattach(zone, &newcheckds->zone); |
17643 | 0 | ISC_LIST_APPEND(newcheckds->zone->checkds_requests, newcheckds, |
17644 | 0 | link); |
17645 | 0 | newcheckds->dst = dst; |
17646 | 0 | dns_name_dup(&checkds->ns, checkds->mctx, &newcheckds->ns); |
17647 | 0 | switch (isc_sockaddr_pf(&newcheckds->dst)) { |
17648 | 0 | case PF_INET: |
17649 | 0 | isc_sockaddr_any(&newcheckds->src); |
17650 | 0 | break; |
17651 | 0 | case PF_INET6: |
17652 | 0 | isc_sockaddr_any6(&newcheckds->src); |
17653 | 0 | break; |
17654 | 0 | default: |
17655 | 0 | UNREACHABLE(); |
17656 | 0 | } |
17657 | | /* |
17658 | | * XXXWMM: Should we attach key and transport here? |
17659 | | * Probably not, because we expect the name servers to be |
17660 | | * publicly available on the default transport protocol. |
17661 | | */ |
17662 | | |
17663 | 0 | CHECK(isc_ratelimiter_enqueue(newcheckds->zone->zmgr->checkdsrl, |
17664 | 0 | newcheckds->zone->loop, |
17665 | 0 | checkds_send_toaddr, newcheckds, |
17666 | 0 | &newcheckds->rlevent)); |
17667 | 0 | newcheckds = NULL; |
17668 | 0 | } |
17669 | | |
17670 | 0 | cleanup: |
17671 | 0 | if (newcheckds != NULL) { |
17672 | 0 | checkds_destroy(newcheckds, true); |
17673 | 0 | } |
17674 | 0 | } |
17675 | | |
17676 | | static void |
17677 | 0 | checkds_send(dns_zone_t *zone) { |
17678 | 0 | dns_view_t *view = dns_zone_getview(zone); |
17679 | 0 | isc_result_t result; |
17680 | 0 | unsigned int flags = 0; |
17681 | 0 | unsigned int i = 0; |
17682 | | |
17683 | | /* |
17684 | | * Zone lock held by caller. |
17685 | | */ |
17686 | 0 | REQUIRE(LOCKED_ZONE(zone)); |
17687 | |
|
17688 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17689 | 0 | "checkds: start sending DS queries to %u parentals", |
17690 | 0 | dns_remote_count(&zone->parentals)); |
17691 | |
|
17692 | 0 | if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXITING)) { |
17693 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17694 | 0 | "checkds: abort, named exiting"); |
17695 | 0 | return; |
17696 | 0 | } |
17697 | | |
17698 | 0 | dns_remote_reset(&zone->parentals, false); |
17699 | 0 | while (!dns_remote_done(&zone->parentals)) { |
17700 | 0 | dns_tsigkey_t *key = NULL; |
17701 | 0 | dns_transport_t *transport = NULL; |
17702 | 0 | isc_sockaddr_t src, dst; |
17703 | 0 | dns_checkds_t *checkds = NULL; |
17704 | |
|
17705 | 0 | i++; |
17706 | |
|
17707 | 0 | if (dns_remote_keyname(&zone->parentals) != NULL) { |
17708 | 0 | dns_name_t *keyname = |
17709 | 0 | dns_remote_keyname(&zone->parentals); |
17710 | 0 | (void)dns_view_gettsig(view, keyname, &key); |
17711 | 0 | } |
17712 | |
|
17713 | 0 | if (dns_remote_tlsname(&zone->parentals) != NULL) { |
17714 | 0 | dns_name_t *tlsname = |
17715 | 0 | dns_remote_tlsname(&zone->parentals); |
17716 | 0 | (void)dns_view_gettransport(view, DNS_TRANSPORT_TLS, |
17717 | 0 | tlsname, &transport); |
17718 | 0 | dns_zone_logc( |
17719 | 0 | zone, DNS_LOGCATEGORY_XFER_IN, ISC_LOG_INFO, |
17720 | 0 | "got TLS configuration for zone transfer"); |
17721 | 0 | } |
17722 | |
|
17723 | 0 | dst = dns_remote_curraddr(&zone->parentals); |
17724 | 0 | src = dns_remote_sourceaddr(&zone->parentals); |
17725 | 0 | INSIST(isc_sockaddr_pf(&src) == isc_sockaddr_pf(&dst)); |
17726 | |
|
17727 | 0 | if (isc_sockaddr_disabled(&dst)) { |
17728 | 0 | if (key != NULL) { |
17729 | 0 | dns_tsigkey_detach(&key); |
17730 | 0 | } |
17731 | 0 | if (transport != NULL) { |
17732 | 0 | dns_transport_detach(&transport); |
17733 | 0 | } |
17734 | 0 | goto next; |
17735 | 0 | } |
17736 | | |
17737 | | /* TODO: glue the transport to the checkds request */ |
17738 | | |
17739 | 0 | if (checkds_isqueued(zone, NULL, &dst, key, transport)) { |
17740 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17741 | 0 | "checkds: DS query to parent " |
17742 | 0 | "%d is queued", |
17743 | 0 | i); |
17744 | 0 | if (key != NULL) { |
17745 | 0 | dns_tsigkey_detach(&key); |
17746 | 0 | } |
17747 | 0 | if (transport != NULL) { |
17748 | 0 | dns_transport_detach(&transport); |
17749 | 0 | } |
17750 | 0 | goto next; |
17751 | 0 | } |
17752 | | |
17753 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17754 | 0 | "checkds: create DS query for " |
17755 | 0 | "parent %d", |
17756 | 0 | i); |
17757 | |
|
17758 | 0 | checkds_create(zone->mctx, flags, &checkds); |
17759 | 0 | zone_iattach(zone, &checkds->zone); |
17760 | 0 | dns_name_dup(dns_rootname, checkds->mctx, &checkds->ns); |
17761 | 0 | checkds->src = src; |
17762 | 0 | checkds->dst = dst; |
17763 | |
|
17764 | 0 | INSIST(checkds->key == NULL); |
17765 | 0 | if (key != NULL) { |
17766 | 0 | checkds->key = key; |
17767 | 0 | key = NULL; |
17768 | 0 | } |
17769 | |
|
17770 | 0 | INSIST(checkds->transport == NULL); |
17771 | 0 | if (transport != NULL) { |
17772 | 0 | checkds->transport = transport; |
17773 | 0 | transport = NULL; |
17774 | 0 | } |
17775 | |
|
17776 | 0 | ISC_LIST_APPEND(zone->checkds_requests, checkds, link); |
17777 | 0 | result = isc_ratelimiter_enqueue( |
17778 | 0 | checkds->zone->zmgr->checkdsrl, checkds->zone->loop, |
17779 | 0 | checkds_send_toaddr, checkds, &checkds->rlevent); |
17780 | 0 | if (result != ISC_R_SUCCESS) { |
17781 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17782 | 0 | "checkds: send DS query to " |
17783 | 0 | "parent %d failed", |
17784 | 0 | i); |
17785 | 0 | checkds_destroy(checkds, true); |
17786 | 0 | } |
17787 | |
|
17788 | 0 | next: |
17789 | 0 | dns_remote_next(&zone->parentals, false); |
17790 | 0 | } |
17791 | 0 | } |
17792 | | |
17793 | | /* |
17794 | | * Fetch NS records from parent zone. |
17795 | | */ |
17796 | | static isc_result_t |
17797 | 0 | nsfetch_start(dns_zonefetch_t *fetch) { |
17798 | 0 | dns_nsfetch_t *nsfetch; |
17799 | |
|
17800 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_NS); |
17801 | |
|
17802 | 0 | nsfetch = &fetch->fetchdata.nsfetch; |
17803 | | |
17804 | | /* Derive parent domain. Check for root domain. */ |
17805 | 0 | if (dns_name_countlabels(&nsfetch->pname) <= 1U) { |
17806 | 0 | return ISC_R_NOTFOUND; |
17807 | 0 | } |
17808 | | |
17809 | 0 | dns_name_split(&nsfetch->pname, |
17810 | 0 | dns_name_countlabels(&nsfetch->pname) - 1U, NULL, |
17811 | 0 | &nsfetch->pname); |
17812 | |
|
17813 | 0 | fetch->qtype = dns_rdatatype_ns; |
17814 | 0 | fetch->qname = &nsfetch->pname; |
17815 | |
|
17816 | 0 | return ISC_R_SUCCESS; |
17817 | 0 | } |
17818 | | |
17819 | | /* |
17820 | | * Retry an NS RRset lookup, one level up. In other words, this function should |
17821 | | * be called on an dns_nsfetch structure where the response yielded in a NODATA |
17822 | | * response. This must be because there is an empty non-terminal inbetween the |
17823 | | * child and parent zone. |
17824 | | */ |
17825 | | static void |
17826 | 0 | nsfetch_continue(dns_zonefetch_t *fetch) { |
17827 | 0 | dns_zone_t *zone = fetch->zone; |
17828 | |
|
17829 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_NS); |
17830 | |
|
17831 | | #ifdef ENABLE_AFL |
17832 | | if (!dns_fuzzing_resolver) { |
17833 | | #endif /* ifdef ENABLE_AFL */ |
17834 | 0 | LOCK_ZONE(zone); |
17835 | 0 | zone->fetchcount[ZONEFETCHTYPE_NS]++; |
17836 | |
|
17837 | 0 | dns_zonefetch_reschedule(fetch); |
17838 | |
|
17839 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
17840 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
17841 | 0 | "Creating parent NS fetch in " |
17842 | 0 | "nsfetch_continue()"); |
17843 | 0 | } |
17844 | 0 | UNLOCK_ZONE(zone); |
17845 | | #ifdef ENABLE_AFL |
17846 | | } |
17847 | | #endif /* ifdef ENABLE_AFL */ |
17848 | 0 | } |
17849 | | |
17850 | | static void |
17851 | 0 | nsfetch_cancel(dns_zonefetch_t *fetch) { |
17852 | 0 | dns_zone_t *zone; |
17853 | |
|
17854 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_NS); |
17855 | 0 | REQUIRE(DNS_ZONE_VALID(fetch->zone)); |
17856 | 0 | REQUIRE(LOCKED_ZONE(fetch->zone)); |
17857 | |
|
17858 | 0 | zone = fetch->zone; |
17859 | |
|
17860 | 0 | zone->fetchcount[ZONEFETCHTYPE_NS]--; |
17861 | 0 | } |
17862 | | |
17863 | | static void |
17864 | 0 | nsfetch_cleanup(dns_zonefetch_t *fetch) { |
17865 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_NS); |
17866 | 0 | } |
17867 | | |
17868 | | /* |
17869 | | * An NS RRset has been fetched from the parent of a zone whose DS RRset needs |
17870 | | * to be checked; scan the RRset and start sending queries to the parental |
17871 | | * agents. |
17872 | | */ |
17873 | | static isc_result_t |
17874 | 0 | nsfetch_checkds(dns_zonefetch_t *fetch, isc_result_t eresult) { |
17875 | 0 | dns_nsfetch_t *nsfetch; |
17876 | 0 | isc_result_t result = ISC_R_NOMORE; |
17877 | 0 | dns_zone_t *zone = NULL; |
17878 | 0 | dns_name_t *pname = NULL; |
17879 | 0 | char pnamebuf[DNS_NAME_FORMATSIZE]; |
17880 | 0 | dns_rdataset_t *nsrrset = NULL; |
17881 | |
|
17882 | 0 | REQUIRE(fetch != NULL); |
17883 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_NS); |
17884 | 0 | REQUIRE(DNS_ZONE_VALID(fetch->zone)); |
17885 | 0 | REQUIRE(LOCKED_ZONE(fetch->zone)); |
17886 | |
|
17887 | 0 | nsfetch = &fetch->fetchdata.nsfetch; |
17888 | 0 | zone = fetch->zone; |
17889 | 0 | nsrrset = &fetch->rrset; |
17890 | 0 | pname = &nsfetch->pname; |
17891 | |
|
17892 | 0 | zone->fetchcount[ZONEFETCHTYPE_NS]--; |
17893 | |
|
17894 | 0 | dns_name_format(pname, pnamebuf, sizeof(pnamebuf)); |
17895 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
17896 | 0 | "Returned from '%s' NS fetch in nsfetch_checkds(): %s", |
17897 | 0 | pnamebuf, isc_result_totext(eresult)); |
17898 | |
|
17899 | 0 | if (eresult == DNS_R_NCACHENXRRSET || eresult == DNS_R_NXRRSET) { |
17900 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
17901 | 0 | "NODATA response for NS '%s', level up", pnamebuf); |
17902 | 0 | return DNS_R_CONTINUE; |
17903 | 0 | } |
17904 | | |
17905 | 0 | CHECK(dns_zonefetch_verify(fetch, eresult, dns_trust_secure)); |
17906 | | |
17907 | | /* Record the number of NS records we found. */ |
17908 | 0 | zone->parent_nscount = dns_rdataset_count(nsrrset); |
17909 | |
|
17910 | 0 | UNLOCK_ZONE(zone); |
17911 | | |
17912 | | /* Look up the addresses for the found parental name servers. */ |
17913 | 0 | DNS_RDATASET_FOREACH(nsrrset) { |
17914 | 0 | dns_checkds_t *checkds = NULL; |
17915 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
17916 | 0 | dns_rdata_ns_t ns; |
17917 | 0 | bool isqueued; |
17918 | |
|
17919 | 0 | dns_rdataset_current(nsrrset, &rdata); |
17920 | 0 | result = dns_rdata_tostruct(&rdata, &ns, NULL); |
17921 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
17922 | |
|
17923 | 0 | dns_rdata_reset(&rdata); |
17924 | |
|
17925 | 0 | LOCK_ZONE(zone); |
17926 | 0 | isqueued = checkds_isqueued(zone, &ns.name, NULL, NULL, NULL); |
17927 | 0 | UNLOCK_ZONE(zone); |
17928 | 0 | if (isqueued) { |
17929 | 0 | continue; |
17930 | 0 | } |
17931 | 0 | checkds_create(zone->mctx, 0, &checkds); |
17932 | |
|
17933 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
17934 | 0 | char nsnamebuf[DNS_NAME_FORMATSIZE]; |
17935 | 0 | dns_name_format(&ns.name, nsnamebuf, sizeof(nsnamebuf)); |
17936 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
17937 | 0 | "checkds: send DS query to NS %s", |
17938 | 0 | nsnamebuf); |
17939 | 0 | } |
17940 | |
|
17941 | 0 | LOCK_ZONE(zone); |
17942 | 0 | zone_iattach(zone, &checkds->zone); |
17943 | 0 | dns_name_dup(&ns.name, zone->mctx, &checkds->ns); |
17944 | 0 | ISC_LIST_APPEND(zone->checkds_requests, checkds, link); |
17945 | 0 | UNLOCK_ZONE(zone); |
17946 | |
|
17947 | 0 | checkds_find_address(checkds); |
17948 | 0 | } |
17949 | |
|
17950 | 0 | LOCK_ZONE(zone); |
17951 | |
|
17952 | 0 | cleanup: |
17953 | 0 | if (result != ISC_R_SUCCESS) { |
17954 | 0 | dnssec_log( |
17955 | 0 | zone, ISC_LOG_ERROR, |
17956 | 0 | "checkds: error during parental-agents processing: %s", |
17957 | 0 | isc_result_totext(result)); |
17958 | 0 | } |
17959 | |
|
17960 | 0 | return result; |
17961 | 0 | } |
17962 | | |
17963 | | static void |
17964 | 0 | zone_checkds(dns_zone_t *zone) { |
17965 | 0 | bool cdscheck = false; |
17966 | 0 | dns_checkdstype_t checkdstype = zone->checkdstype; |
17967 | |
|
17968 | 0 | if (checkdstype == dns_checkdstype_no) { |
17969 | 0 | return; |
17970 | 0 | } |
17971 | | |
17972 | 0 | ISC_LIST_FOREACH(zone->checkds_ok, key, link) { |
17973 | 0 | dst_key_state_t ds_state = DST_KEY_STATE_NA; |
17974 | 0 | bool ksk = false; |
17975 | 0 | isc_stdtime_t published = 0, withdrawn = 0; |
17976 | | |
17977 | | /* Is this key have the KSK role? */ |
17978 | 0 | (void)dst_key_role(key->key, &ksk, NULL); |
17979 | 0 | if (!ksk) { |
17980 | 0 | continue; |
17981 | 0 | } |
17982 | | |
17983 | | /* Do we need to check the DS RRset? */ |
17984 | 0 | (void)dst_key_getstate(key->key, DST_KEY_DS, &ds_state); |
17985 | 0 | (void)dst_key_gettime(key->key, DST_TIME_DSPUBLISH, &published); |
17986 | 0 | (void)dst_key_gettime(key->key, DST_TIME_DSDELETE, &withdrawn); |
17987 | |
|
17988 | 0 | if (ds_state == DST_KEY_STATE_RUMOURED && published == 0) { |
17989 | 0 | dst_key_setnum(key->key, DST_NUM_DSPUBCOUNT, 0); |
17990 | 0 | cdscheck = true; |
17991 | 0 | } else if (ds_state == DST_KEY_STATE_UNRETENTIVE && |
17992 | 0 | withdrawn == 0) |
17993 | 0 | { |
17994 | 0 | dst_key_setnum(key->key, DST_NUM_DSDELCOUNT, 0); |
17995 | 0 | cdscheck = true; |
17996 | 0 | } |
17997 | 0 | } |
17998 | |
|
17999 | 0 | if (!cdscheck) { |
18000 | 0 | return; |
18001 | 0 | } |
18002 | | |
18003 | 0 | if (checkdstype == dns_checkdstype_explicit) { |
18004 | | /* Request the DS RRset. */ |
18005 | 0 | LOCK_ZONE(zone); |
18006 | 0 | checkds_send(zone); |
18007 | 0 | UNLOCK_ZONE(zone); |
18008 | 0 | return; |
18009 | 0 | } |
18010 | | |
18011 | 0 | INSIST(checkdstype == dns_checkdstype_yes); |
18012 | |
|
18013 | | #ifdef ENABLE_AFL |
18014 | | if (!dns_fuzzing_resolver) { |
18015 | | #endif /* ifdef ENABLE_AFL */ |
18016 | 0 | dns_zonefetch_t *fetch = NULL; |
18017 | 0 | dns_nsfetch_t *nsfetch = NULL; |
18018 | |
|
18019 | 0 | fetch = isc_mem_get(zone->mctx, sizeof(dns_zonefetch_t)); |
18020 | 0 | *fetch = (dns_zonefetch_t){ |
18021 | 0 | .zone = zone, |
18022 | 0 | .fetchtype = ZONEFETCHTYPE_NS, |
18023 | 0 | .fetchmethods = |
18024 | 0 | (dns_zonefetch_methods_t){ |
18025 | 0 | .start_fetch = nsfetch_start, |
18026 | 0 | .continue_fetch = nsfetch_continue, |
18027 | 0 | .cancel_fetch = nsfetch_cancel, |
18028 | 0 | .cleanup_fetch = nsfetch_cleanup, |
18029 | 0 | .done_fetch = nsfetch_checkds, |
18030 | 0 | }, |
18031 | 0 | }; |
18032 | 0 | isc_mem_attach(zone->mctx, &fetch->mctx); |
18033 | |
|
18034 | 0 | LOCK_ZONE(zone); |
18035 | 0 | zone->fetchcount[ZONEFETCHTYPE_NS]++; |
18036 | |
|
18037 | 0 | nsfetch = &fetch->fetchdata.nsfetch; |
18038 | 0 | dns_name_init(&nsfetch->pname); |
18039 | 0 | dns_name_clone(&zone->origin, &nsfetch->pname); |
18040 | |
|
18041 | 0 | dns_zonefetch_schedule(fetch, &zone->origin); |
18042 | |
|
18043 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
18044 | 0 | dnssec_log( |
18045 | 0 | zone, ISC_LOG_DEBUG(3), |
18046 | 0 | "Creating parent NS fetch in zone_checkds()"); |
18047 | 0 | } |
18048 | 0 | UNLOCK_ZONE(zone); |
18049 | | #ifdef ENABLE_AFL |
18050 | | } |
18051 | | #endif /* ifdef ENABLE_AFL */ |
18052 | 0 | } |
18053 | | |
18054 | | static unsigned char _dsync_data[] = "\x06_dsync"; |
18055 | | static dns_name_t _dsync = DNS_NAME_INITNONABSOLUTE(_dsync_data); |
18056 | | |
18057 | | static isc_result_t |
18058 | 0 | dsyncfetch_start(dns_zonefetch_t *fetch) { |
18059 | 0 | dns_dsyncfetch_t *dsyncfetch; |
18060 | 0 | dns_zone_t *zone; |
18061 | 0 | dns_name_t *dsyncname, prefix; |
18062 | 0 | unsigned int nlabels; |
18063 | 0 | isc_result_t result; |
18064 | |
|
18065 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_DSYNC); |
18066 | 0 | REQUIRE(DNS_ZONE_VALID(fetch->zone)); |
18067 | |
|
18068 | 0 | dsyncfetch = &fetch->fetchdata.dsyncfetch; |
18069 | 0 | zone = fetch->zone; |
18070 | | |
18071 | | /* |
18072 | | * The dsync owner name is build up of <prefix>._dsync.<parent-name>. |
18073 | | * The prefix is the relative domain name of the child consisting of |
18074 | | * the labels under the zonecut. |
18075 | | */ |
18076 | 0 | dsyncname = dns_fixedname_initname(&dsyncfetch->dsyncname); |
18077 | |
|
18078 | 0 | nlabels = dns_name_countlabels(&dsyncfetch->pname); |
18079 | 0 | dns_name_init(&prefix); |
18080 | 0 | dns_name_split(dns_fixedname_name(&fetch->name), nlabels, &prefix, |
18081 | 0 | NULL); |
18082 | |
|
18083 | 0 | result = dns_name_concatenate(&prefix, &_dsync, dsyncname); |
18084 | 0 | if (result != ISC_R_SUCCESS) { |
18085 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
18086 | 0 | "dsyncfetch: failed to create parent DSYNC fetch " |
18087 | 0 | "(child part): %s", |
18088 | 0 | isc_result_totext(result)); |
18089 | 0 | return result; |
18090 | 0 | } |
18091 | | |
18092 | 0 | result = dns_name_concatenate(dsyncname, &dsyncfetch->pname, dsyncname); |
18093 | 0 | if (result != ISC_R_SUCCESS) { |
18094 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
18095 | 0 | "dsyncfetch: failed to create parent DSYNC fetch " |
18096 | 0 | "(parent part): %s", |
18097 | 0 | isc_result_totext(result)); |
18098 | 0 | return result; |
18099 | 0 | } |
18100 | | |
18101 | 0 | fetch->qtype = dns_rdatatype_dsync; |
18102 | 0 | fetch->qname = dsyncname; |
18103 | |
|
18104 | 0 | return ISC_R_SUCCESS; |
18105 | 0 | } |
18106 | | |
18107 | | /* |
18108 | | * Retry an DSYNC RRset lookup. |
18109 | | */ |
18110 | | static void |
18111 | 0 | dsyncfetch_continue(dns_zonefetch_t *fetch) { |
18112 | 0 | dns_zone_t *zone; |
18113 | |
|
18114 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_DSYNC); |
18115 | 0 | REQUIRE(DNS_ZONE_VALID(fetch->zone)); |
18116 | |
|
18117 | 0 | zone = fetch->zone; |
18118 | |
|
18119 | | #ifdef ENABLE_AFL |
18120 | | if (!dns_fuzzing_resolver) { |
18121 | | #endif /* ifdef ENABLE_AFL */ |
18122 | 0 | LOCK_ZONE(zone); |
18123 | 0 | zone->fetchcount[ZONEFETCHTYPE_DSYNC]++; |
18124 | |
|
18125 | 0 | dns_zonefetch_reschedule(fetch); |
18126 | |
|
18127 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
18128 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
18129 | 0 | "Creating parent DSYNC fetch in " |
18130 | 0 | "dsyncfetch_continue()"); |
18131 | 0 | } |
18132 | 0 | UNLOCK_ZONE(zone); |
18133 | | #ifdef ENABLE_AFL |
18134 | | } |
18135 | | #endif /* ifdef ENABLE_AFL */ |
18136 | 0 | } |
18137 | | |
18138 | | static void |
18139 | 0 | dsyncfetch_cancel(dns_zonefetch_t *fetch) { |
18140 | 0 | dns_zone_t *zone; |
18141 | |
|
18142 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_DSYNC); |
18143 | 0 | REQUIRE(DNS_ZONE_VALID(fetch->zone)); |
18144 | 0 | REQUIRE(LOCKED_ZONE(fetch->zone)); |
18145 | |
|
18146 | 0 | zone = fetch->zone; |
18147 | |
|
18148 | 0 | zone->fetchcount[ZONEFETCHTYPE_DSYNC]--; |
18149 | 0 | } |
18150 | | |
18151 | | static void |
18152 | 0 | dsyncfetch_cleanup(dns_zonefetch_t *fetch) { |
18153 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_DSYNC); |
18154 | 0 | } |
18155 | | |
18156 | | /* |
18157 | | * A DSYNC RRset has been fetched; scan the RRset and start sending |
18158 | | * NOTIFY(CDS) queries to them. |
18159 | | */ |
18160 | | static isc_result_t |
18161 | 0 | dsyncfetch_done(dns_zonefetch_t *fetch, isc_result_t eresult) { |
18162 | 0 | dns_dsyncfetch_t *dsyncfetch; |
18163 | 0 | isc_result_t result = ISC_R_NOMORE; |
18164 | 0 | dns_notify_t *notify = NULL; |
18165 | 0 | dns_zone_t *zone = NULL; |
18166 | 0 | dns_name_t *dsyncname = NULL; |
18167 | 0 | char dsyncnamebuf[DNS_NAME_FORMATSIZE]; |
18168 | 0 | dns_rdataset_t *rrset = NULL; |
18169 | 0 | in_port_t port = 0; |
18170 | 0 | dns_fixedname_t fixed; |
18171 | 0 | dns_name_t *target = dns_fixedname_initname(&fixed); |
18172 | |
|
18173 | 0 | REQUIRE(fetch != NULL); |
18174 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_DSYNC); |
18175 | 0 | REQUIRE(DNS_ZONE_VALID(fetch->zone)); |
18176 | 0 | REQUIRE(LOCKED_ZONE(fetch->zone)); |
18177 | |
|
18178 | 0 | dsyncfetch = &fetch->fetchdata.dsyncfetch; |
18179 | 0 | zone = fetch->zone; |
18180 | 0 | rrset = &fetch->rrset; |
18181 | 0 | dsyncname = dns_fixedname_name(&dsyncfetch->dsyncname); |
18182 | |
|
18183 | 0 | zone->fetchcount[ZONEFETCHTYPE_DSYNC]--; |
18184 | |
|
18185 | 0 | dns_name_format(dsyncname, dsyncnamebuf, sizeof(dsyncnamebuf)); |
18186 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
18187 | 0 | "dsyncfetch: Returned from '%s' DSYNC fetch in " |
18188 | 0 | "dsyncfetch_done(): %s", |
18189 | 0 | dsyncnamebuf, isc_result_totext(eresult)); |
18190 | |
|
18191 | 0 | result = dns_zonefetch_verify(fetch, eresult, dns_trust_secure); |
18192 | 0 | if (result != ISC_R_SUCCESS) { |
18193 | 0 | goto done; |
18194 | 0 | } |
18195 | | |
18196 | 0 | UNLOCK_ZONE(zone); |
18197 | | |
18198 | | /* Notify targets. */ |
18199 | 0 | dns_rdata_dsync_t dsync; |
18200 | 0 | unsigned int count = 0; |
18201 | 0 | for (result = dns_rdataset_first(rrset); result == ISC_R_SUCCESS; |
18202 | 0 | result = dns_rdataset_next(rrset)) |
18203 | 0 | { |
18204 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
18205 | |
|
18206 | 0 | dns_rdataset_current(rrset, &rdata); |
18207 | 0 | result = dns_rdata_tostruct(&rdata, &dsync, NULL); |
18208 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
18209 | |
|
18210 | 0 | dns_rdata_reset(&rdata); |
18211 | 0 | if (dsync.scheme != DNS_DSYNCSCHEME_NOTIFY) { |
18212 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), |
18213 | 0 | "dsyncfetch: unsupported DSYNC scheme %u, " |
18214 | 0 | "ignoring", |
18215 | 0 | dsync.scheme); |
18216 | 0 | continue; |
18217 | 0 | } |
18218 | | |
18219 | 0 | if (dsync.type != dns_rdatatype_cds) { |
18220 | 0 | char typebuf[DNS_RDATATYPE_FORMATSIZE]; |
18221 | 0 | dns_rdatatype_format(dsync.type, typebuf, |
18222 | 0 | sizeof(typebuf)); |
18223 | |
|
18224 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(1), |
18225 | 0 | "dsyncfetch: DSYNC RRtype %s not " |
18226 | 0 | "supported, ignoring", |
18227 | 0 | result == ISC_R_SUCCESS ? typebuf |
18228 | 0 | : "UNKNOWN"); |
18229 | 0 | continue; |
18230 | 0 | } |
18231 | | |
18232 | | /* Save matched DSYNC record. */ |
18233 | 0 | if (count == 0) { |
18234 | 0 | port = dsync.port; |
18235 | 0 | dns_name_copy(&dsync.target, target); |
18236 | 0 | } |
18237 | |
|
18238 | 0 | count++; |
18239 | 0 | if (count > 1) { |
18240 | 0 | dns_zone_log(zone, ISC_LOG_WARNING, |
18241 | 0 | "dsyncfetch: multiple DSYNC records " |
18242 | 0 | "matching NOTIFY scheme and CDS RRtype, " |
18243 | 0 | "dropping response"); |
18244 | 0 | result = DNS_R_INVALIDDSYNC; |
18245 | 0 | break; |
18246 | 0 | } |
18247 | 0 | } |
18248 | |
|
18249 | 0 | LOCK_ZONE(zone); |
18250 | |
|
18251 | 0 | if (result == ISC_R_NOMORE) { |
18252 | 0 | result = ISC_R_SUCCESS; |
18253 | 0 | } else { |
18254 | | /* We didn't scan the complete RRset. */ |
18255 | 0 | goto done; |
18256 | 0 | } |
18257 | | |
18258 | | /* |
18259 | | * Only one NOTIFY can be queued per type. |
18260 | | * Currently the only supported scheme/type is NOTIFY CDS. |
18261 | | * The port and target from the first matched DSYNC record are saved in |
18262 | | * 'port' and 'target'. |
18263 | | */ |
18264 | 0 | if (count != 1) { |
18265 | | /* |
18266 | | * Either count equals zero, and there is nothing to be queued. |
18267 | | * Or count > 1, then this is an invalid DSYNC RRset. |
18268 | | */ |
18269 | 0 | goto done; |
18270 | 0 | } |
18271 | | |
18272 | 0 | bool isqueued = dns_notify_isqueued(&zone->notifycds, dns_rdatatype_cds, |
18273 | 0 | port, 0, target, NULL, NULL, NULL); |
18274 | |
|
18275 | 0 | UNLOCK_ZONE(zone); |
18276 | |
|
18277 | 0 | if (!isqueued) { |
18278 | 0 | dns_notify_create(zone->mctx, dns_rdatatype_cds, port, |
18279 | 0 | DNS_NOTIFY_NOSOA, ¬ify); |
18280 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
18281 | 0 | char tbuf[DNS_NAME_FORMATSIZE]; |
18282 | 0 | dns_name_format(target, tbuf, sizeof(tbuf)); |
18283 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
18284 | 0 | "dsyncfetch: send NOTIFY(CDS) query to %s", |
18285 | 0 | tbuf); |
18286 | 0 | } |
18287 | 0 | dns_zone_iattach(zone, ¬ify->zone); |
18288 | 0 | dns_name_dup(target, zone->mctx, ¬ify->ns); |
18289 | 0 | LOCK_ZONE(zone); |
18290 | 0 | ISC_LIST_APPEND(zone->notifycds.notifies, notify, link); |
18291 | 0 | UNLOCK_ZONE(zone); |
18292 | 0 | dns_notify_find_address(notify); |
18293 | 0 | } |
18294 | |
|
18295 | 0 | LOCK_ZONE(zone); |
18296 | |
|
18297 | 0 | done: |
18298 | 0 | if (result != ISC_R_SUCCESS) { |
18299 | 0 | dns_zone_log(zone, ISC_LOG_DEBUG(3), |
18300 | 0 | "dsyncfetch: error processing DSYNC RRset: %s", |
18301 | 0 | isc_result_totext(result)); |
18302 | 0 | } |
18303 | |
|
18304 | 0 | return result; |
18305 | 0 | } |
18306 | | |
18307 | | /* |
18308 | | * An NS RRset has been fetched from the parent of a zone whose DSYNC RRset |
18309 | | * needs to be queried; scan the RRset and start resolving those queries. |
18310 | | */ |
18311 | | static isc_result_t |
18312 | 0 | nsfetch_dsync(dns_zonefetch_t *fetch, isc_result_t eresult) { |
18313 | 0 | dns_nsfetch_t *nsfetch; |
18314 | 0 | isc_result_t result = ISC_R_NOMORE; |
18315 | 0 | dns_zone_t *zone = NULL; |
18316 | 0 | dns_name_t *pname = NULL; |
18317 | 0 | char pnamebuf[DNS_NAME_FORMATSIZE]; |
18318 | |
|
18319 | 0 | REQUIRE(fetch != NULL); |
18320 | 0 | REQUIRE(fetch->fetchtype == ZONEFETCHTYPE_NS); |
18321 | 0 | REQUIRE(DNS_ZONE_VALID(fetch->zone)); |
18322 | 0 | REQUIRE(LOCKED_ZONE(fetch->zone)); |
18323 | |
|
18324 | 0 | nsfetch = &fetch->fetchdata.nsfetch; |
18325 | 0 | zone = fetch->zone; |
18326 | 0 | pname = &nsfetch->pname; |
18327 | |
|
18328 | 0 | zone->fetchcount[ZONEFETCHTYPE_NS]--; |
18329 | |
|
18330 | 0 | dns_name_format(pname, pnamebuf, sizeof(pnamebuf)); |
18331 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
18332 | 0 | "Returned from '%s' NS fetch in nsfetch_dsync(): %s", |
18333 | 0 | pnamebuf, isc_result_totext(eresult)); |
18334 | |
|
18335 | 0 | if (eresult == DNS_R_NCACHENXRRSET || eresult == DNS_R_NXRRSET) { |
18336 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
18337 | 0 | "NODATA response for NS '%s', level up", pnamebuf); |
18338 | 0 | return DNS_R_CONTINUE; |
18339 | 0 | } |
18340 | | |
18341 | 0 | result = dns_zonefetch_verify(fetch, eresult, dns_trust_secure); |
18342 | 0 | if (result != ISC_R_SUCCESS) { |
18343 | 0 | goto done; |
18344 | 0 | } |
18345 | | |
18346 | | #ifdef ENABLE_AFL |
18347 | | if (!dns_fuzzing_resolver) { |
18348 | | #endif /* ifdef ENABLE_AFL */ |
18349 | 0 | dns_zonefetch_t *zfetch = NULL; |
18350 | 0 | dns_dsyncfetch_t *dsyncfetch; |
18351 | |
|
18352 | 0 | zfetch = isc_mem_get(zone->mctx, sizeof(dns_zonefetch_t)); |
18353 | 0 | *zfetch = (dns_zonefetch_t){ |
18354 | 0 | .zone = zone, |
18355 | 0 | .fetchtype = ZONEFETCHTYPE_DSYNC, |
18356 | 0 | .fetchmethods = |
18357 | 0 | (dns_zonefetch_methods_t){ |
18358 | 0 | .start_fetch = dsyncfetch_start, |
18359 | 0 | .continue_fetch = dsyncfetch_continue, |
18360 | 0 | .cancel_fetch = dsyncfetch_cancel, |
18361 | 0 | .cleanup_fetch = dsyncfetch_cleanup, |
18362 | 0 | .done_fetch = dsyncfetch_done, |
18363 | 0 | }, |
18364 | 0 | }; |
18365 | 0 | isc_mem_attach(zone->mctx, &zfetch->mctx); |
18366 | |
|
18367 | 0 | zone->fetchcount[ZONEFETCHTYPE_DSYNC]++; |
18368 | |
|
18369 | 0 | dsyncfetch = &zfetch->fetchdata.dsyncfetch; |
18370 | 0 | dns_name_init(&dsyncfetch->pname); |
18371 | 0 | dns_name_clone(pname, &dsyncfetch->pname); |
18372 | |
|
18373 | 0 | dns_zonefetch_schedule(zfetch, &zone->origin); |
18374 | |
|
18375 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
18376 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
18377 | 0 | "Creating parent DSYNC fetch in " |
18378 | 0 | "nsfetch_dsync()"); |
18379 | 0 | } |
18380 | | #ifdef ENABLE_AFL |
18381 | | } |
18382 | | #endif /* ifdef ENABLE_AFL */ |
18383 | |
|
18384 | 0 | done: |
18385 | 0 | return result; |
18386 | 0 | } |
18387 | | |
18388 | | static void |
18389 | 0 | zone_notifycds(dns_zone_t *zone) { |
18390 | 0 | dns_notifytype_t notifytype = zone->notifycds.notifytype; |
18391 | |
|
18392 | 0 | if (notifytype == dns_notifytype_no) { |
18393 | 0 | return; |
18394 | 0 | } |
18395 | | |
18396 | 0 | INSIST(notifytype == dns_notifytype_yes); |
18397 | |
|
18398 | | #ifdef ENABLE_AFL |
18399 | | if (!dns_fuzzing_resolver) { |
18400 | | #endif /* ifdef ENABLE_AFL */ |
18401 | 0 | dns_zonefetch_t *fetch = NULL; |
18402 | 0 | dns_nsfetch_t *nsfetch; |
18403 | |
|
18404 | 0 | fetch = isc_mem_get(zone->mctx, sizeof(dns_zonefetch_t)); |
18405 | 0 | *fetch = (dns_zonefetch_t){ |
18406 | 0 | .zone = zone, |
18407 | 0 | .fetchtype = ZONEFETCHTYPE_NS, |
18408 | 0 | .fetchmethods = |
18409 | 0 | (dns_zonefetch_methods_t){ |
18410 | 0 | .start_fetch = nsfetch_start, |
18411 | 0 | .continue_fetch = nsfetch_continue, |
18412 | 0 | .cancel_fetch = nsfetch_cancel, |
18413 | 0 | .cleanup_fetch = nsfetch_cleanup, |
18414 | 0 | .done_fetch = nsfetch_dsync, |
18415 | 0 | }, |
18416 | 0 | }; |
18417 | 0 | isc_mem_attach(zone->mctx, &fetch->mctx); |
18418 | |
|
18419 | 0 | LOCK_ZONE(zone); |
18420 | 0 | zone->fetchcount[ZONEFETCHTYPE_NS]++; |
18421 | |
|
18422 | 0 | nsfetch = &fetch->fetchdata.nsfetch; |
18423 | 0 | dns_name_init(&nsfetch->pname); |
18424 | 0 | dns_name_clone(&zone->origin, &nsfetch->pname); |
18425 | |
|
18426 | 0 | dns_zonefetch_schedule(fetch, &zone->origin); |
18427 | |
|
18428 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
18429 | 0 | dnssec_log( |
18430 | 0 | zone, ISC_LOG_DEBUG(3), |
18431 | 0 | "Creating parent NS fetch in zone_notifyds()"); |
18432 | 0 | } |
18433 | 0 | UNLOCK_ZONE(zone); |
18434 | | #ifdef ENABLE_AFL |
18435 | | } |
18436 | | #endif /* ifdef ENABLE_AFL */ |
18437 | 0 | } |
18438 | | |
18439 | | static void |
18440 | | update_ttl(dns_rdataset_t *rdataset, dns_name_t *name, dns_ttl_t ttl, |
18441 | 0 | dns_diff_t *diff) { |
18442 | | /* |
18443 | | * Delete everything using the existing TTL. |
18444 | | */ |
18445 | 0 | DNS_RDATASET_FOREACH(rdataset) { |
18446 | 0 | dns_difftuple_t *tuple = NULL; |
18447 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
18448 | |
|
18449 | 0 | dns_rdataset_current(rdataset, &rdata); |
18450 | 0 | dns_difftuple_create(diff->mctx, DNS_DIFFOP_DEL, name, |
18451 | 0 | rdataset->ttl, &rdata, &tuple); |
18452 | 0 | dns_diff_appendminimal(diff, &tuple); |
18453 | 0 | } |
18454 | | |
18455 | | /* |
18456 | | * Add everything using the new TTL. |
18457 | | */ |
18458 | 0 | DNS_RDATASET_FOREACH(rdataset) { |
18459 | 0 | dns_difftuple_t *tuple = NULL; |
18460 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
18461 | |
|
18462 | 0 | dns_rdataset_current(rdataset, &rdata); |
18463 | 0 | dns_difftuple_create(diff->mctx, DNS_DIFFOP_ADD, name, ttl, |
18464 | 0 | &rdata, &tuple); |
18465 | 0 | dns_diff_appendminimal(diff, &tuple); |
18466 | 0 | } |
18467 | 0 | } |
18468 | | |
18469 | | static isc_result_t |
18470 | | zone_verifykeys(dns_zone_t *zone, dns_dnsseckeylist_t *newkeys, |
18471 | 0 | uint32_t purgeval, isc_stdtime_t now) { |
18472 | | /* |
18473 | | * Make sure that the existing keys are also present in the new keylist. |
18474 | | */ |
18475 | 0 | ISC_LIST_FOREACH(zone->keyring, key1, link) { |
18476 | 0 | bool found = false; |
18477 | |
|
18478 | 0 | if (dst_key_is_unused(key1->key)) { |
18479 | 0 | continue; |
18480 | 0 | } |
18481 | 0 | if (dns_keymgr_key_may_be_purged(key1->key, purgeval, now)) { |
18482 | 0 | continue; |
18483 | 0 | } |
18484 | 0 | if (key1->purge) { |
18485 | 0 | continue; |
18486 | 0 | } |
18487 | | |
18488 | 0 | ISC_LIST_FOREACH(*newkeys, key2, link) { |
18489 | 0 | if (dst_key_compare(key1->key, key2->key)) { |
18490 | 0 | found = true; |
18491 | 0 | break; |
18492 | 0 | } |
18493 | 0 | } |
18494 | |
|
18495 | 0 | if (!found) { |
18496 | 0 | char keystr[DST_KEY_FORMATSIZE]; |
18497 | 0 | dst_key_format(key1->key, keystr, sizeof(keystr)); |
18498 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(1), |
18499 | 0 | "verifykeys: key %s - not available", |
18500 | 0 | keystr); |
18501 | 0 | return ISC_R_NOTFOUND; |
18502 | 0 | } |
18503 | 0 | } |
18504 | | |
18505 | | /* All good. */ |
18506 | 0 | return ISC_R_SUCCESS; |
18507 | 0 | } |
18508 | | |
18509 | | static void |
18510 | 0 | remove_rdataset(dns_zone_t *zone, dns_diff_t *diff, dns_rdataset_t *rdataset) { |
18511 | 0 | if (!dns_rdataset_isassociated(rdataset)) { |
18512 | 0 | return; |
18513 | 0 | } |
18514 | | |
18515 | 0 | DNS_RDATASET_FOREACH(rdataset) { |
18516 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
18517 | 0 | dns_difftuple_t *tuple = NULL; |
18518 | |
|
18519 | 0 | dns_rdataset_current(rdataset, &rdata); |
18520 | 0 | dns_difftuple_create(zone->mctx, DNS_DIFFOP_DEL, &zone->origin, |
18521 | 0 | rdataset->ttl, &rdata, &tuple); |
18522 | 0 | dns_diff_append(diff, &tuple); |
18523 | 0 | } |
18524 | 0 | return; |
18525 | 0 | } |
18526 | | |
18527 | | static void |
18528 | 0 | add_tuple(dns_diff_t *diff, dns_difftuple_t *tuple) { |
18529 | 0 | dns_difftuple_t *copy = NULL; |
18530 | |
|
18531 | 0 | dns_difftuple_copy(tuple, ©); |
18532 | 0 | dns_diff_appendminimal(diff, ©); |
18533 | 0 | } |
18534 | | |
18535 | | static void |
18536 | | zone_apply_skrbundle(dns_zone_t *zone, dns_skrbundle_t *bundle, |
18537 | | dns_rdataset_t *dnskeyset, dns_rdataset_t *cdsset, |
18538 | 0 | dns_rdataset_t *cdnskeyset, dns_diff_t *diff) { |
18539 | 0 | dns_kasp_t *kasp = zone->kasp; |
18540 | |
|
18541 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
18542 | 0 | REQUIRE(DNS_KASP_VALID(kasp)); |
18543 | 0 | REQUIRE(DNS_SKRBUNDLE_VALID(bundle)); |
18544 | | |
18545 | | /* Remove existing DNSKEY, CDS, and CDNSKEY records. */ |
18546 | 0 | remove_rdataset(zone, diff, dnskeyset); |
18547 | 0 | remove_rdataset(zone, diff, cdsset); |
18548 | 0 | remove_rdataset(zone, diff, cdnskeyset); |
18549 | | |
18550 | | /* Add the records from the bundle. */ |
18551 | 0 | ISC_LIST_FOREACH(bundle->diff.tuples, tuple, link) { |
18552 | 0 | switch (tuple->rdata.type) { |
18553 | 0 | case dns_rdatatype_dnskey: |
18554 | 0 | add_tuple(diff, tuple); |
18555 | 0 | break; |
18556 | 0 | case dns_rdatatype_cdnskey: |
18557 | 0 | case dns_rdatatype_cds: |
18558 | 0 | add_tuple(diff, tuple); |
18559 | 0 | break; |
18560 | 0 | case dns_rdatatype_rrsig: |
18561 | | /* Not interested in right now */ |
18562 | 0 | break; |
18563 | 0 | default: |
18564 | 0 | INSIST(0); |
18565 | 0 | } |
18566 | 0 | } |
18567 | 0 | } |
18568 | | |
18569 | | static void |
18570 | 0 | zone_rekey(dns_zone_t *zone) { |
18571 | 0 | isc_result_t result; |
18572 | 0 | dns_db_t *db = NULL; |
18573 | 0 | dns_dbnode_t *node = NULL; |
18574 | 0 | dns_dbversion_t *ver = NULL; |
18575 | 0 | dns_rdataset_t cdsset, soaset, soasigs, keyset, keysigs, cdnskeyset; |
18576 | 0 | dns_dnsseckeylist_t dnskeys, keys, rmkeys; |
18577 | 0 | dns_diff_t diff, _sig_diff; |
18578 | 0 | dns_kasp_t *kasp; |
18579 | 0 | dns_skrbundle_t *bundle = NULL; |
18580 | 0 | dns__zonediff_t zonediff; |
18581 | 0 | bool commit = false, newactive = false; |
18582 | 0 | bool newalg = false; |
18583 | 0 | bool fullsign; |
18584 | 0 | bool notifycds = false; |
18585 | 0 | bool offlineksk = false; |
18586 | 0 | bool keymgr_done = false; |
18587 | 0 | bool kasp_change = false; |
18588 | 0 | uint8_t options = 0; |
18589 | 0 | uint32_t sigval = 0; |
18590 | 0 | dns_ttl_t ttl = 3600; |
18591 | 0 | const char *dir = NULL; |
18592 | 0 | isc_mem_t *mctx = NULL; |
18593 | 0 | isc_stdtime_t now, nexttime = 0; |
18594 | 0 | isc_time_t timenow; |
18595 | 0 | isc_interval_t ival; |
18596 | 0 | char timebuf[80]; |
18597 | |
|
18598 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
18599 | |
|
18600 | 0 | ISC_LIST_INIT(dnskeys); |
18601 | 0 | ISC_LIST_INIT(keys); |
18602 | 0 | ISC_LIST_INIT(rmkeys); |
18603 | 0 | dns_rdataset_init(&soaset); |
18604 | 0 | dns_rdataset_init(&soasigs); |
18605 | 0 | dns_rdataset_init(&keyset); |
18606 | 0 | dns_rdataset_init(&keysigs); |
18607 | 0 | dns_rdataset_init(&cdsset); |
18608 | 0 | dns_rdataset_init(&cdnskeyset); |
18609 | 0 | mctx = zone->mctx; |
18610 | 0 | dns_diff_init(mctx, &diff); |
18611 | 0 | dns_diff_init(mctx, &_sig_diff); |
18612 | 0 | zonediff_init(&zonediff, &_sig_diff); |
18613 | |
|
18614 | 0 | CHECK(dns_zone_getdb(zone, &db)); |
18615 | 0 | CHECK(dns_db_newversion(db, &ver)); |
18616 | 0 | CHECK(dns_db_getoriginnode(db, &node)); |
18617 | |
|
18618 | 0 | timenow = isc_time_now(); |
18619 | 0 | now = isc_time_seconds(&timenow); |
18620 | |
|
18621 | 0 | kasp = zone->kasp; |
18622 | 0 | dir = dns_zone_getkeydirectory(zone); |
18623 | |
|
18624 | 0 | dnssec_log(zone, ISC_LOG_INFO, "reconfiguring zone keys"); |
18625 | | |
18626 | | /* Get the SOA record's TTL */ |
18627 | 0 | CHECK(dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, |
18628 | 0 | dns_rdatatype_none, 0, &soaset, &soasigs)); |
18629 | 0 | ttl = soaset.ttl; |
18630 | 0 | dns_rdataset_disassociate(&soaset); |
18631 | |
|
18632 | 0 | if (kasp != NULL) { |
18633 | 0 | ttl = dns_kasp_dnskeyttl(kasp); |
18634 | 0 | offlineksk = dns_kasp_offlineksk(kasp); |
18635 | 0 | sigval = dns_kasp_sigvalidity_dnskey(kasp); |
18636 | 0 | } |
18637 | | |
18638 | | /* Get the current DNSKEY rdataset */ |
18639 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_dnskey, |
18640 | 0 | dns_rdatatype_none, 0, &keyset, &keysigs); |
18641 | 0 | if (result == ISC_R_SUCCESS) { |
18642 | | /* |
18643 | | * If we don't have a policy then use the DNSKEY ttl |
18644 | | * if it exists. Otherwise update the DNSKEY ttl if |
18645 | | * needed. |
18646 | | */ |
18647 | 0 | if (kasp == NULL) { |
18648 | 0 | ttl = keyset.ttl; |
18649 | 0 | } else if (ttl != keyset.ttl && !offlineksk) { |
18650 | 0 | update_ttl(&keyset, &zone->origin, ttl, &diff); |
18651 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
18652 | 0 | "Updating DNSKEY TTL from %u to %u", |
18653 | 0 | keyset.ttl, ttl); |
18654 | 0 | keyset.ttl = ttl; |
18655 | 0 | } |
18656 | |
|
18657 | 0 | dns_zone_lock_keyfiles(zone); |
18658 | |
|
18659 | 0 | result = dns_dnssec_keylistfromrdataset( |
18660 | 0 | &zone->origin, kasp, dir, mctx, &keyset, &keysigs, |
18661 | 0 | &soasigs, false, false, &dnskeys); |
18662 | |
|
18663 | 0 | dns_zone_unlock_keyfiles(zone); |
18664 | |
|
18665 | 0 | CHECK(result); |
18666 | 0 | } else if (result != ISC_R_NOTFOUND) { |
18667 | 0 | goto cleanup; |
18668 | 0 | } |
18669 | | |
18670 | | /* Get the current CDS rdataset */ |
18671 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_cds, |
18672 | 0 | dns_rdatatype_none, 0, &cdsset, NULL); |
18673 | 0 | if (result != ISC_R_SUCCESS) { |
18674 | 0 | dns_rdataset_cleanup(&cdsset); |
18675 | 0 | } else if (kasp != NULL && ttl != cdsset.ttl && !offlineksk) { |
18676 | 0 | update_ttl(&cdsset, &zone->origin, ttl, &diff); |
18677 | 0 | dnssec_log(zone, ISC_LOG_INFO, "Updating CDS TTL from %u to %u", |
18678 | 0 | cdsset.ttl, ttl); |
18679 | 0 | cdsset.ttl = ttl; |
18680 | 0 | } |
18681 | | |
18682 | | /* Get the current CDNSKEY rdataset */ |
18683 | 0 | result = dns_db_findrdataset(db, node, ver, dns_rdatatype_cdnskey, |
18684 | 0 | dns_rdatatype_none, 0, &cdnskeyset, NULL); |
18685 | 0 | if (result != ISC_R_SUCCESS) { |
18686 | 0 | dns_rdataset_cleanup(&cdnskeyset); |
18687 | 0 | } else if (kasp != NULL && ttl != cdnskeyset.ttl && !offlineksk) { |
18688 | 0 | update_ttl(&cdnskeyset, &zone->origin, ttl, &diff); |
18689 | 0 | dnssec_log(zone, ISC_LOG_INFO, |
18690 | 0 | "Updating CDNSKEY TTL from %u to %u", cdnskeyset.ttl, |
18691 | 0 | ttl); |
18692 | 0 | cdnskeyset.ttl = ttl; |
18693 | 0 | } |
18694 | | |
18695 | | /* |
18696 | | * True when called from "rndc sign". Indicates the zone should be |
18697 | | * fully signed now. |
18698 | | */ |
18699 | 0 | fullsign = DNS_ZONE_OPTION(zone, DNS_ZONEOPT_FULLSIGN); |
18700 | 0 | if (fullsign) { |
18701 | 0 | options |= DNS_KEYMGRATTR_FULLSIGN; |
18702 | 0 | } |
18703 | | |
18704 | | /* |
18705 | | * True when called from "rndc dnssec -step". Indicates the zone |
18706 | | * is allowed to do the next step(s) in the keymgr process. |
18707 | | */ |
18708 | 0 | if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_FORCEKEYMGR)) { |
18709 | 0 | options |= DNS_KEYMGRATTR_FORCESTEP; |
18710 | 0 | } |
18711 | |
|
18712 | 0 | if (offlineksk) { |
18713 | | /* Lookup the correct bundle in the SKR. */ |
18714 | 0 | LOCK_ZONE(zone); |
18715 | 0 | if (zone->skr == NULL) { |
18716 | 0 | UNLOCK_ZONE(zone); |
18717 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(1), |
18718 | 0 | "zone_rekey:dns_skr_lookup failed: " |
18719 | 0 | "no SKR available"); |
18720 | 0 | CLEANUP(DNS_R_NOSKRFILE); |
18721 | 0 | } |
18722 | 0 | bundle = dns_skr_lookup(zone->skr, now, sigval); |
18723 | 0 | zone->skrbundle = bundle; |
18724 | 0 | UNLOCK_ZONE(zone); |
18725 | |
|
18726 | 0 | if (bundle == NULL) { |
18727 | 0 | char nowstr[26]; /* Minimal buf per ctime_r() spec. */ |
18728 | 0 | char utc[sizeof("YYYYMMDDHHSSMM")]; |
18729 | 0 | isc_buffer_t b; |
18730 | 0 | isc_region_t r; |
18731 | 0 | isc_buffer_init(&b, utc, sizeof(utc)); |
18732 | |
|
18733 | 0 | isc_stdtime_tostring(now, nowstr, sizeof(nowstr)); |
18734 | 0 | (void)dns_time32_totext(now, &b); |
18735 | 0 | isc_buffer_usedregion(&b, &r); |
18736 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(1), |
18737 | 0 | "zone_rekey:dns_skr_lookup failed: " |
18738 | 0 | "no available SKR bundle for time " |
18739 | 0 | "%.*s (%s)", |
18740 | 0 | (int)r.length, r.base, nowstr); |
18741 | 0 | CLEANUP(DNS_R_NOSKRBUNDLE); |
18742 | 0 | } |
18743 | | |
18744 | 0 | zone_apply_skrbundle(zone, bundle, &keyset, &cdsset, |
18745 | 0 | &cdnskeyset, &diff); |
18746 | |
|
18747 | 0 | dns_skrbundle_t *next = ISC_LIST_NEXT(bundle, link); |
18748 | 0 | if (next != NULL) { |
18749 | 0 | if (nexttime == 0) { |
18750 | 0 | nexttime = next->inception; |
18751 | 0 | } |
18752 | 0 | } else { |
18753 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
18754 | 0 | "zone_rekey: last bundle in skr, please " |
18755 | 0 | "import new skr file"); |
18756 | 0 | } |
18757 | 0 | } |
18758 | | |
18759 | | /* |
18760 | | * DNSSEC Key and Signing Policy |
18761 | | */ |
18762 | | |
18763 | 0 | KASP_LOCK(kasp); |
18764 | |
|
18765 | 0 | dns_zone_lock_keyfiles(zone); |
18766 | 0 | result = dns_dnssec_findmatchingkeys(&zone->origin, kasp, dir, |
18767 | 0 | dns_zone_getkeystores(zone), now, |
18768 | 0 | false, mctx, &keys); |
18769 | 0 | dns_zone_unlock_keyfiles(zone); |
18770 | |
|
18771 | 0 | if (result != ISC_R_SUCCESS) { |
18772 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(1), |
18773 | 0 | "zone_rekey:dns_dnssec_findmatchingkeys failed: %s", |
18774 | 0 | isc_result_totext(result)); |
18775 | 0 | } |
18776 | |
|
18777 | 0 | if (kasp != NULL && !offlineksk) { |
18778 | | /* Verify new keys. */ |
18779 | 0 | isc_result_t ret = zone_verifykeys( |
18780 | 0 | zone, &keys, dns_kasp_purgekeys(kasp), now); |
18781 | 0 | if (ret != ISC_R_SUCCESS) { |
18782 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
18783 | 0 | "zone_rekey:zone_verifykeys failed: " |
18784 | 0 | "some key files are missing"); |
18785 | 0 | KASP_UNLOCK(kasp); |
18786 | 0 | goto cleanup; |
18787 | 0 | } |
18788 | | |
18789 | | /* |
18790 | | * Check DS at parental agents. Clear ongoing checks. |
18791 | | */ |
18792 | 0 | LOCK_ZONE(zone); |
18793 | 0 | checkds_cancel(zone); |
18794 | 0 | clear_keylist(&zone->checkds_ok, zone->mctx); |
18795 | 0 | ISC_LIST_INIT(zone->checkds_ok); |
18796 | 0 | UNLOCK_ZONE(zone); |
18797 | |
|
18798 | 0 | ret = dns_zone_getdnsseckeys(zone, db, ver, now, |
18799 | 0 | &zone->checkds_ok); |
18800 | 0 | if (ret == ISC_R_SUCCESS) { |
18801 | 0 | zone_checkds(zone); |
18802 | 0 | } else { |
18803 | 0 | dnssec_log(zone, |
18804 | 0 | (ret == ISC_R_NOTFOUND) ? ISC_LOG_DEBUG(1) |
18805 | 0 | : ISC_LOG_ERROR, |
18806 | 0 | "zone_rekey:dns_zone_getdnsseckeys failed: " |
18807 | 0 | "%s", |
18808 | 0 | isc_result_totext(ret)); |
18809 | 0 | } |
18810 | | |
18811 | | /* Run keymgr. */ |
18812 | 0 | if (result == ISC_R_SUCCESS || result == ISC_R_NOTFOUND) { |
18813 | 0 | dns_zone_lock_keyfiles(zone); |
18814 | 0 | result = dns_keymgr_run(&zone->origin, zone->rdclass, |
18815 | 0 | mctx, &keys, &dnskeys, dir, |
18816 | 0 | kasp, options, now, &nexttime); |
18817 | 0 | dns_zone_unlock_keyfiles(zone); |
18818 | |
|
18819 | 0 | if (result == ISC_R_SUCCESS) { |
18820 | 0 | kasp_change = true; |
18821 | 0 | keymgr_done = true; |
18822 | 0 | } else if (result == DNS_R_UNCHANGED) { |
18823 | 0 | result = ISC_R_SUCCESS; |
18824 | 0 | keymgr_done = true; |
18825 | 0 | } else { |
18826 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
18827 | 0 | "zone_rekey:dns_keymgr_run " |
18828 | 0 | "failed: %s", |
18829 | 0 | isc_result_totext(result)); |
18830 | 0 | KASP_UNLOCK(kasp); |
18831 | 0 | goto cleanup; |
18832 | 0 | } |
18833 | 0 | } |
18834 | 0 | } else if (offlineksk) { |
18835 | | /* |
18836 | | * With offline-ksk enabled we don't run the keymgr. |
18837 | | * Instead we derive the states from the timing metadata. |
18838 | | */ |
18839 | 0 | dns_zone_lock_keyfiles(zone); |
18840 | 0 | result = dns_keymgr_offline(&zone->origin, &keys, kasp, now, |
18841 | 0 | &nexttime); |
18842 | 0 | dns_zone_unlock_keyfiles(zone); |
18843 | |
|
18844 | 0 | if (result == ISC_R_SUCCESS) { |
18845 | 0 | keymgr_done = true; |
18846 | 0 | } else { |
18847 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
18848 | 0 | "zone_rekey:dns_keymgr_offline " |
18849 | 0 | "failed: %s", |
18850 | 0 | isc_result_totext(result)); |
18851 | 0 | } |
18852 | 0 | } |
18853 | | |
18854 | 0 | KASP_UNLOCK(kasp); |
18855 | | |
18856 | | /* |
18857 | | * Update CDS, CDNSKEY and DNSKEY record sets if the keymgr ran |
18858 | | * successfully (dns_keymgr_run returned ISC_R_SUCCESS), or in |
18859 | | * case of DNSSEC management without dnssec-policy if we have keys |
18860 | | * (dns_dnssec_findmatchingkeys returned ISC_R_SUCCESS). |
18861 | | */ |
18862 | 0 | if (result == ISC_R_SUCCESS) { |
18863 | 0 | dns_kasp_digestlist_t digests; |
18864 | 0 | bool cdsdel = false; |
18865 | 0 | bool cdnskeydel = false; |
18866 | 0 | bool cdnskeypub = true; |
18867 | 0 | bool sane_diff, sane_dnskey; |
18868 | 0 | isc_stdtime_t when; |
18869 | |
|
18870 | 0 | result = dns_dnssec_updatekeys(&dnskeys, &keys, &rmkeys, |
18871 | 0 | &zone->origin, ttl, &diff, mctx, |
18872 | 0 | dnssec_report); |
18873 | | /* |
18874 | | * Keys couldn't be updated for some reason; |
18875 | | * try again later. |
18876 | | */ |
18877 | 0 | if (result != ISC_R_SUCCESS) { |
18878 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
18879 | 0 | "zone_rekey:couldn't update zone keys: %s", |
18880 | 0 | isc_result_totext(result)); |
18881 | 0 | goto cleanup; |
18882 | 0 | } |
18883 | | |
18884 | 0 | if (offlineksk) { |
18885 | | /* We can skip a lot of things */ |
18886 | 0 | goto post_sync; |
18887 | 0 | } |
18888 | | |
18889 | | /* |
18890 | | * Publish CDS/CDNSKEY DELETE records if the zone is |
18891 | | * transitioning from secure to insecure. |
18892 | | */ |
18893 | 0 | if (kasp != NULL) { |
18894 | 0 | if (strcmp(dns_kasp_getname(kasp), "insecure") == 0) { |
18895 | 0 | cdsdel = true; |
18896 | 0 | cdnskeydel = true; |
18897 | 0 | } |
18898 | 0 | digests = dns_kasp_digests(kasp); |
18899 | 0 | cdnskeypub = dns_kasp_cdnskey(kasp); |
18900 | 0 | } else { |
18901 | | /* Check if there is a CDS DELETE record. */ |
18902 | 0 | if (dns_rdataset_isassociated(&cdsset)) { |
18903 | 0 | DNS_RDATASET_FOREACH(&cdsset) { |
18904 | 0 | dns_rdata_t crdata = DNS_RDATA_INIT; |
18905 | 0 | dns_rdataset_current(&cdsset, &crdata); |
18906 | | /* |
18907 | | * CDS deletion record has this form |
18908 | | * "0 0 0 00" which is 5 zero octets. |
18909 | | */ |
18910 | 0 | if (crdata.length == 5U && |
18911 | 0 | memcmp(crdata.data, |
18912 | 0 | (unsigned char[5]){ 0, 0, 0, |
18913 | 0 | 0, 0 }, |
18914 | 0 | 5) == 0) |
18915 | 0 | { |
18916 | 0 | cdsdel = true; |
18917 | 0 | break; |
18918 | 0 | } |
18919 | 0 | } |
18920 | 0 | } |
18921 | | |
18922 | | /* Check if there is a CDNSKEY DELETE record. */ |
18923 | 0 | if (dns_rdataset_isassociated(&cdnskeyset)) { |
18924 | 0 | DNS_RDATASET_FOREACH(&cdnskeyset) { |
18925 | 0 | dns_rdata_t crdata = DNS_RDATA_INIT; |
18926 | 0 | dns_rdataset_current(&cdnskeyset, |
18927 | 0 | &crdata); |
18928 | | /* |
18929 | | * CDNSKEY deletion record has this form |
18930 | | * "0 3 0 AA==" which is 2 zero octets, |
18931 | | * a 3, and 2 zero octets. |
18932 | | */ |
18933 | 0 | if (crdata.length == 5U && |
18934 | 0 | memcmp(crdata.data, |
18935 | 0 | (unsigned char[5]){ 0, 0, 3, |
18936 | 0 | 0, 0 }, |
18937 | 0 | 5) == 0) |
18938 | 0 | { |
18939 | 0 | cdnskeydel = true; |
18940 | 0 | break; |
18941 | 0 | } |
18942 | 0 | } |
18943 | 0 | } |
18944 | |
|
18945 | 0 | digests = dns_kasp_digests(zone->defaultkasp); |
18946 | 0 | } |
18947 | | |
18948 | | /* |
18949 | | * Update CDS / CDNSKEY records. |
18950 | | */ |
18951 | 0 | result = dns_dnssec_syncupdate(&dnskeys, &rmkeys, &cdsset, |
18952 | 0 | &cdnskeyset, now, &digests, |
18953 | 0 | cdnskeypub, ttl, &diff, mctx); |
18954 | 0 | if (result == ISC_R_SUCCESS) { |
18955 | 0 | notifycds = true; |
18956 | 0 | } else if (result != DNS_R_UNCHANGED) { |
18957 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
18958 | 0 | "zone_rekey:couldn't update CDS/CDNSKEY: %s", |
18959 | 0 | isc_result_totext(result)); |
18960 | 0 | goto cleanup; |
18961 | 0 | } |
18962 | | |
18963 | 0 | if (cdsdel || cdnskeydel) { |
18964 | | /* |
18965 | | * Only publish CDS/CDNSKEY DELETE records if there is |
18966 | | * a KSK that can be used to verify the RRset. This |
18967 | | * means there must be a key with the KSK role that is |
18968 | | * published and is used for signing. |
18969 | | */ |
18970 | 0 | bool allow = false; |
18971 | 0 | ISC_LIST_FOREACH(dnskeys, key, link) { |
18972 | 0 | dst_key_t *dstk = key->key; |
18973 | |
|
18974 | 0 | if (dst_key_is_published(dstk, now, &when) && |
18975 | 0 | dst_key_is_signing(dstk, DST_BOOL_KSK, now, |
18976 | 0 | &when)) |
18977 | 0 | { |
18978 | 0 | allow = true; |
18979 | 0 | break; |
18980 | 0 | } |
18981 | 0 | } |
18982 | 0 | if (cdsdel) { |
18983 | 0 | cdsdel = allow; |
18984 | 0 | } |
18985 | 0 | if (cdnskeydel) { |
18986 | 0 | cdnskeydel = allow; |
18987 | 0 | } |
18988 | 0 | } |
18989 | 0 | result = dns_dnssec_syncdelete( |
18990 | 0 | &cdsset, &cdnskeyset, &zone->origin, zone->rdclass, ttl, |
18991 | 0 | &diff, mctx, cdsdel, cdnskeydel); |
18992 | 0 | if (result == ISC_R_SUCCESS) { |
18993 | 0 | notifycds = true; |
18994 | 0 | } else if (result != DNS_R_UNCHANGED) { |
18995 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
18996 | 0 | "zone_rekey:couldn't update CDS/CDNSKEY " |
18997 | 0 | "DELETE records: %s", |
18998 | 0 | isc_result_totext(result)); |
18999 | 0 | goto cleanup; |
19000 | 0 | } |
19001 | | |
19002 | 0 | post_sync: |
19003 | | /* |
19004 | | * See if any pre-existing keys have newly become active; |
19005 | | * also, see if any new key is for a new algorithm, as in that |
19006 | | * event, we need to sign the zone fully. (If there's a new |
19007 | | * key, but it's for an already-existing algorithm, then |
19008 | | * the zone signing can be handled incrementally.) |
19009 | | */ |
19010 | 0 | ISC_LIST_FOREACH(dnskeys, key, link) { |
19011 | 0 | if (!key->first_sign) { |
19012 | 0 | continue; |
19013 | 0 | } |
19014 | | |
19015 | 0 | newactive = true; |
19016 | |
|
19017 | 0 | if (!dns_rdataset_isassociated(&keysigs)) { |
19018 | 0 | newalg = true; |
19019 | 0 | break; |
19020 | 0 | } |
19021 | | |
19022 | 0 | if (signed_with_alg(&keysigs, dst_key_alg(key->key))) { |
19023 | | /* |
19024 | | * This isn't a new algorithm; clear |
19025 | | * first_sign so we won't sign the |
19026 | | * whole zone with this key later. |
19027 | | */ |
19028 | 0 | key->first_sign = false; |
19029 | 0 | } else { |
19030 | 0 | newalg = true; |
19031 | 0 | break; |
19032 | 0 | } |
19033 | 0 | } |
19034 | | |
19035 | | /* |
19036 | | * A sane diff is one that is not empty, and that does not |
19037 | | * introduce a zone with NSEC only DNSKEYs along with NSEC3 |
19038 | | * chains. |
19039 | | */ |
19040 | 0 | sane_dnskey = dns_zone_check_dnskey_nsec3(zone, db, ver, &diff, |
19041 | 0 | NULL, 0); |
19042 | 0 | sane_diff = !ISC_LIST_EMPTY(diff.tuples) && sane_dnskey; |
19043 | 0 | if (!sane_dnskey) { |
19044 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
19045 | 0 | "NSEC only DNSKEYs and NSEC3 chains not " |
19046 | 0 | "allowed"); |
19047 | 0 | } |
19048 | |
|
19049 | 0 | if (newactive || fullsign || sane_diff || kasp_change) { |
19050 | 0 | CHECK(dns_diff_apply(&diff, db, ver)); |
19051 | 0 | CHECK(clean_nsec3param(zone, db, ver, &diff)); |
19052 | 0 | CHECK(add_signing_records(db, zone->privatetype, ver, |
19053 | 0 | &diff, newalg || fullsign)); |
19054 | 0 | CHECK(update_soa_serial(zone, db, ver, &diff, mctx, |
19055 | 0 | zone->updatemethod)); |
19056 | 0 | CHECK(add_chains(zone, db, ver, &diff)); |
19057 | 0 | CHECK(sign_apex(zone, db, ver, now, &diff, &zonediff)); |
19058 | 0 | CHECK(zone_journal(zone, zonediff.diff, NULL, |
19059 | 0 | "zone_rekey")); |
19060 | 0 | commit = true; |
19061 | 0 | } |
19062 | 0 | } |
19063 | | |
19064 | 0 | dns_db_closeversion(db, &ver, true); |
19065 | |
|
19066 | 0 | LOCK_ZONE(zone); |
19067 | |
|
19068 | 0 | if (commit) { |
19069 | 0 | dns_stats_t *dnssecsignstats = |
19070 | 0 | dns_zone_getdnssecsignstats(zone); |
19071 | |
|
19072 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_NEEDNOTIFY); |
19073 | |
|
19074 | 0 | zone_needdump(zone, DNS_DUMP_DELAY); |
19075 | |
|
19076 | 0 | dns__zone_settimer(zone, timenow); |
19077 | | |
19078 | | /* Remove any signatures from removed keys. */ |
19079 | 0 | ISC_LIST_FOREACH(rmkeys, key, link) { |
19080 | 0 | result = zone_signwithkey(zone, dst_key_alg(key->key), |
19081 | 0 | dst_key_id(key->key), true, |
19082 | 0 | false); |
19083 | 0 | if (result != ISC_R_SUCCESS) { |
19084 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
19085 | 0 | "zone_signwithkey failed: " |
19086 | 0 | "%s", |
19087 | 0 | isc_result_totext(result)); |
19088 | 0 | } |
19089 | | |
19090 | | /* Clear DNSSEC sign statistics. */ |
19091 | 0 | if (dnssecsignstats != NULL) { |
19092 | 0 | dns_dnssecsignstats_clear( |
19093 | 0 | dnssecsignstats, dst_key_id(key->key), |
19094 | 0 | dst_key_alg(key->key)); |
19095 | | /* |
19096 | | * Also clear the dnssec-sign |
19097 | | * statistics of the revoked key id. |
19098 | | */ |
19099 | 0 | dns_dnssecsignstats_clear( |
19100 | 0 | dnssecsignstats, dst_key_rid(key->key), |
19101 | 0 | dst_key_alg(key->key)); |
19102 | 0 | } |
19103 | 0 | } |
19104 | |
|
19105 | 0 | if (fullsign) { |
19106 | | /* |
19107 | | * "rndc sign" was called, so we now sign the zone |
19108 | | * with all active keys, whether they're new or not. |
19109 | | */ |
19110 | 0 | ISC_LIST_FOREACH(dnskeys, key, link) { |
19111 | 0 | if (key->force_sign || key->hint_sign) { |
19112 | 0 | result = zone_signwithkey( |
19113 | 0 | zone, dst_key_alg(key->key), |
19114 | 0 | dst_key_id(key->key), false, |
19115 | 0 | true); |
19116 | 0 | if (result != ISC_R_SUCCESS) { |
19117 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
19118 | 0 | "zone_signwithkey " |
19119 | 0 | "failed: " |
19120 | 0 | "%s", |
19121 | 0 | isc_result_totext( |
19122 | 0 | result)); |
19123 | 0 | } |
19124 | 0 | } |
19125 | 0 | } |
19126 | | /* |
19127 | | * ...and remove signatures for all inactive keys. |
19128 | | */ |
19129 | 0 | ISC_LIST_FOREACH(dnskeys, key, link) { |
19130 | 0 | if (!key->force_sign && !key->hint_sign) { |
19131 | 0 | result = zone_signwithkey( |
19132 | 0 | zone, dst_key_alg(key->key), |
19133 | 0 | dst_key_id(key->key), true, |
19134 | 0 | false); |
19135 | 0 | if (result != ISC_R_SUCCESS) { |
19136 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
19137 | 0 | "zone_signwithkey " |
19138 | 0 | "failed: " |
19139 | 0 | "%s", |
19140 | 0 | isc_result_totext( |
19141 | 0 | result)); |
19142 | 0 | } |
19143 | 0 | } |
19144 | 0 | } |
19145 | |
|
19146 | 0 | } else if (newalg) { |
19147 | | /* |
19148 | | * We haven't been told to sign fully, but a new |
19149 | | * algorithm was added to the DNSKEY. We sign |
19150 | | * the full zone, but only with newly active |
19151 | | * keys. |
19152 | | */ |
19153 | 0 | ISC_LIST_FOREACH(dnskeys, key, link) { |
19154 | 0 | if (!key->first_sign) { |
19155 | 0 | continue; |
19156 | 0 | } |
19157 | | |
19158 | 0 | result = zone_signwithkey( |
19159 | 0 | zone, dst_key_alg(key->key), |
19160 | 0 | dst_key_id(key->key), false, false); |
19161 | 0 | if (result != ISC_R_SUCCESS) { |
19162 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
19163 | 0 | "zone_signwithkey failed: " |
19164 | 0 | "%s", |
19165 | 0 | isc_result_totext(result)); |
19166 | 0 | } |
19167 | 0 | } |
19168 | 0 | } |
19169 | | |
19170 | | /* |
19171 | | * Clear fullsign flag, if it was set, so we don't do |
19172 | | * another full signing next time. |
19173 | | */ |
19174 | 0 | DNS_ZONE_CLROPTION(zone, DNS_ZONEOPT_FULLSIGN); |
19175 | | |
19176 | | /* |
19177 | | * Cause the zone to add/delete NSEC3 chains for the |
19178 | | * deferred NSEC3PARAM changes. |
19179 | | */ |
19180 | 0 | ISC_LIST_FOREACH(zonediff.diff->tuples, tuple, link) { |
19181 | 0 | unsigned char buf[DNS_NSEC3PARAM_BUFFERSIZE]; |
19182 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
19183 | 0 | dns_rdata_nsec3param_t nsec3param; |
19184 | |
|
19185 | 0 | if (tuple->rdata.type != zone->privatetype || |
19186 | 0 | tuple->op != DNS_DIFFOP_ADD) |
19187 | 0 | { |
19188 | 0 | continue; |
19189 | 0 | } |
19190 | | |
19191 | 0 | if (!dns_nsec3param_fromprivate(&tuple->rdata, &rdata, |
19192 | 0 | buf, sizeof(buf))) |
19193 | 0 | { |
19194 | 0 | continue; |
19195 | 0 | } |
19196 | | |
19197 | 0 | result = dns_rdata_tostruct(&rdata, &nsec3param, NULL); |
19198 | 0 | RUNTIME_CHECK(result == ISC_R_SUCCESS); |
19199 | 0 | if (nsec3param.flags == 0) { |
19200 | 0 | continue; |
19201 | 0 | } |
19202 | | |
19203 | 0 | result = zone_addnsec3chain(zone, &nsec3param); |
19204 | 0 | if (result != ISC_R_SUCCESS) { |
19205 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
19206 | 0 | "zone_addnsec3chain failed: %s", |
19207 | 0 | isc_result_totext(result)); |
19208 | 0 | } |
19209 | 0 | } |
19210 | | |
19211 | | /* |
19212 | | * Activate any NSEC3 chain updates that may have |
19213 | | * been scheduled before this rekey. |
19214 | | */ |
19215 | 0 | if (fullsign || newalg) { |
19216 | 0 | resume_addnsec3chain(zone); |
19217 | 0 | } |
19218 | | |
19219 | | /* |
19220 | | * Schedule the next resigning event |
19221 | | */ |
19222 | 0 | dns__zone_set_resigntime(zone); |
19223 | 0 | } |
19224 | |
|
19225 | 0 | isc_time_settoepoch(&zone->refreshkeytime); |
19226 | | |
19227 | | /* |
19228 | | * If keymgr provided a next time, use the calculated next rekey time. |
19229 | | */ |
19230 | 0 | if (kasp != NULL) { |
19231 | 0 | isc_time_t timenext; |
19232 | 0 | uint32_t nexttime_seconds; |
19233 | | |
19234 | | /* |
19235 | | * Set the key refresh timer to the next scheduled key event |
19236 | | * or to 'dnssec-loadkeys-interval' seconds in the future |
19237 | | * if no next key event is scheduled (nexttime == 0). |
19238 | | */ |
19239 | 0 | if (nexttime > 0) { |
19240 | 0 | nexttime_seconds = nexttime - now; |
19241 | 0 | } else { |
19242 | 0 | nexttime_seconds = zone->refreshkeyinterval; |
19243 | 0 | } |
19244 | |
|
19245 | 0 | DNS_ZONE_TIME_ADD(&timenow, nexttime_seconds, &timenext); |
19246 | 0 | zone->refreshkeytime = timenext; |
19247 | 0 | dns__zone_settimer(zone, timenow); |
19248 | 0 | isc_time_formattimestamp(&zone->refreshkeytime, timebuf, 80); |
19249 | |
|
19250 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
19251 | 0 | "next key event in %u seconds", nexttime_seconds); |
19252 | 0 | dnssec_log(zone, ISC_LOG_INFO, "next key event: %s", timebuf); |
19253 | 0 | } else { |
19254 | | /* |
19255 | | * If we're doing key maintenance, set the key refresh timer to |
19256 | | * the next scheduled key event or to 'dnssec-loadkeys-interval' |
19257 | | * seconds in the future, whichever is sooner. |
19258 | | */ |
19259 | 0 | isc_time_t timethen; |
19260 | 0 | isc_stdtime_t then; |
19261 | |
|
19262 | 0 | DNS_ZONE_TIME_ADD(&timenow, zone->refreshkeyinterval, |
19263 | 0 | &timethen); |
19264 | 0 | zone->refreshkeytime = timethen; |
19265 | |
|
19266 | 0 | ISC_LIST_FOREACH(dnskeys, key, link) { |
19267 | 0 | then = now; |
19268 | 0 | result = next_keyevent(key->key, &then); |
19269 | 0 | if (result != ISC_R_SUCCESS) { |
19270 | 0 | continue; |
19271 | 0 | } |
19272 | | |
19273 | 0 | DNS_ZONE_TIME_ADD(&timenow, then - now, &timethen); |
19274 | 0 | if (isc_time_compare(&timethen, &zone->refreshkeytime) < |
19275 | 0 | 0) |
19276 | 0 | { |
19277 | 0 | zone->refreshkeytime = timethen; |
19278 | 0 | } |
19279 | 0 | } |
19280 | |
|
19281 | 0 | dns__zone_settimer(zone, timenow); |
19282 | |
|
19283 | 0 | isc_time_formattimestamp(&zone->refreshkeytime, timebuf, 80); |
19284 | 0 | dnssec_log(zone, ISC_LOG_INFO, "next key event: %s", timebuf); |
19285 | 0 | } |
19286 | 0 | UNLOCK_ZONE(zone); |
19287 | | |
19288 | | /* |
19289 | | * Remember which keys have been used. |
19290 | | */ |
19291 | 0 | if (!ISC_LIST_EMPTY(zone->keyring)) { |
19292 | 0 | clear_keylist(&zone->keyring, zone->mctx); |
19293 | 0 | } |
19294 | |
|
19295 | 0 | ISC_LIST_FOREACH(dnskeys, key, link) { |
19296 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
19297 | | /* This debug log is used in the kasp system test */ |
19298 | 0 | char algbuf[DNS_SECALG_FORMATSIZE]; |
19299 | 0 | dns_secalg_format(dst_key_alg(key->key), algbuf, |
19300 | 0 | sizeof(algbuf)); |
19301 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
19302 | 0 | "zone_rekey done: key %d/%s", |
19303 | 0 | dst_key_id(key->key), algbuf); |
19304 | 0 | } |
19305 | 0 | ISC_LIST_UNLINK(dnskeys, key, link); |
19306 | 0 | ISC_LIST_APPEND(zone->keyring, key, link); |
19307 | 0 | } |
19308 | |
|
19309 | 0 | if (keymgr_done && isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
19310 | 0 | char namebuf[DNS_NAME_FORMATSIZE]; |
19311 | |
|
19312 | 0 | dns_name_format(&zone->origin, namebuf, sizeof(namebuf)); |
19313 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), "keymgr: %s done", namebuf); |
19314 | 0 | } |
19315 | | |
19316 | | /* |
19317 | | * If the CDS/CDNSKEY RRset has changed, send NOTIFY(CDS) to endpoints. |
19318 | | */ |
19319 | 0 | if (notifycds) { |
19320 | 0 | zone_notifycds(zone); |
19321 | 0 | } |
19322 | |
|
19323 | 0 | result = ISC_R_SUCCESS; |
19324 | |
|
19325 | 0 | cleanup: |
19326 | 0 | LOCK_ZONE(zone); |
19327 | 0 | if (result != ISC_R_SUCCESS) { |
19328 | | /* |
19329 | | * Something went wrong; try again in ten minutes or |
19330 | | * after a key refresh interval, whichever is shorter. |
19331 | | */ |
19332 | 0 | int loglevel = ISC_LOG_DEBUG(3); |
19333 | 0 | if (result != DNS_R_NOTLOADED) { |
19334 | 0 | loglevel = ISC_LOG_ERROR; |
19335 | 0 | } |
19336 | 0 | dnssec_log(zone, loglevel, |
19337 | 0 | "zone_rekey failure: %s (retry in %u seconds)", |
19338 | 0 | isc_result_totext(result), |
19339 | 0 | ISC_MIN(zone->refreshkeyinterval, 600)); |
19340 | 0 | isc_interval_set(&ival, ISC_MIN(zone->refreshkeyinterval, 600), |
19341 | 0 | 0); |
19342 | 0 | isc_time_nowplusinterval(&zone->refreshkeytime, &ival); |
19343 | 0 | } |
19344 | | |
19345 | | /* |
19346 | | * Clear forcekeymgr flag, if it was set, so we don't do |
19347 | | * another force next time. |
19348 | | */ |
19349 | 0 | DNS_ZONE_CLROPTION(zone, DNS_ZONEOPT_FORCEKEYMGR); |
19350 | |
|
19351 | 0 | UNLOCK_ZONE(zone); |
19352 | |
|
19353 | 0 | dns_diff_clear(&diff); |
19354 | 0 | dns_diff_clear(&_sig_diff); |
19355 | |
|
19356 | 0 | clear_keylist(&dnskeys, mctx); |
19357 | 0 | clear_keylist(&keys, mctx); |
19358 | 0 | clear_keylist(&rmkeys, mctx); |
19359 | |
|
19360 | 0 | if (ver != NULL) { |
19361 | 0 | dns_db_closeversion(db, &ver, false); |
19362 | 0 | } |
19363 | 0 | dns_rdataset_cleanup(&cdsset); |
19364 | 0 | dns_rdataset_cleanup(&keyset); |
19365 | 0 | dns_rdataset_cleanup(&keysigs); |
19366 | 0 | dns_rdataset_cleanup(&soasigs); |
19367 | 0 | dns_rdataset_cleanup(&cdnskeyset); |
19368 | 0 | if (node != NULL) { |
19369 | 0 | dns_db_detachnode(&node); |
19370 | 0 | } |
19371 | 0 | if (db != NULL) { |
19372 | 0 | dns_db_detach(&db); |
19373 | 0 | } |
19374 | |
|
19375 | 0 | INSIST(ver == NULL); |
19376 | 0 | } |
19377 | | |
19378 | | void |
19379 | 0 | dns_zone_rekey(dns_zone_t *zone, bool fullsign, bool forcekeymgr) { |
19380 | 0 | isc_time_t now; |
19381 | |
|
19382 | 0 | if (zone->type == dns_zone_primary && zone->loop != NULL) { |
19383 | 0 | LOCK_ZONE(zone); |
19384 | |
|
19385 | 0 | if (fullsign) { |
19386 | 0 | DNS_ZONE_SETOPTION(zone, DNS_ZONEOPT_FULLSIGN); |
19387 | 0 | } |
19388 | 0 | if (forcekeymgr) { |
19389 | 0 | DNS_ZONE_SETOPTION(zone, DNS_ZONEOPT_FORCEKEYMGR); |
19390 | 0 | } |
19391 | |
|
19392 | 0 | now = isc_time_now(); |
19393 | 0 | zone->refreshkeytime = now; |
19394 | 0 | dns__zone_settimer(zone, now); |
19395 | |
|
19396 | 0 | UNLOCK_ZONE(zone); |
19397 | 0 | } |
19398 | 0 | } |
19399 | | |
19400 | | isc_result_t |
19401 | | dns_zone_dnssecstatus(dns_zone_t *zone, dns_kasp_t *kasp, |
19402 | | dns_dnsseckeylist_t *keys, isc_stdtime_t now, |
19403 | 0 | bool verbose, char *out, size_t out_len) { |
19404 | 0 | isc_result_t result; |
19405 | 0 | isc_buffer_t buf; |
19406 | 0 | isc_time_t refreshkeytime; |
19407 | 0 | isc_stdtime_t refresh; |
19408 | 0 | char timestr[26]; |
19409 | |
|
19410 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
19411 | 0 | REQUIRE(out != NULL); |
19412 | |
|
19413 | 0 | isc_buffer_init(&buf, out, out_len); |
19414 | |
|
19415 | 0 | RETERR(isc_buffer_printf( |
19416 | 0 | &buf, "DNSSEC status for zone '%s' using policy '%s':\n", |
19417 | 0 | zone->strname, dns_kasp_getname(kasp))); |
19418 | |
|
19419 | 0 | isc_stdtime_tostring(now, timestr, sizeof(timestr)); |
19420 | 0 | RETERR(isc_buffer_printf(&buf, "Current time: %s\n", timestr)); |
19421 | |
|
19422 | 0 | dns_zone_getrefreshkeytime(zone, &refreshkeytime); |
19423 | 0 | refresh = isc_time_seconds(&refreshkeytime); |
19424 | 0 | isc_stdtime_tostring(refresh, timestr, sizeof(timestr)); |
19425 | 0 | RETERR(isc_buffer_printf(&buf, "Next key event: %s\n", timestr)); |
19426 | |
|
19427 | 0 | bool checkds = zone->checkdstype != dns_checkdstype_no; |
19428 | 0 | LOCK(&kasp->lock); |
19429 | 0 | result = dns_keymgr_status(kasp, keys, &buf, now, verbose, checkds); |
19430 | 0 | UNLOCK(&kasp->lock); |
19431 | |
|
19432 | 0 | return result; |
19433 | 0 | } |
19434 | | |
19435 | | isc_result_t |
19436 | | dns_zone_nscheck(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version, |
19437 | 0 | unsigned int *errors) { |
19438 | 0 | isc_result_t result; |
19439 | 0 | dns_dbnode_t *node = NULL; |
19440 | |
|
19441 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
19442 | 0 | REQUIRE(errors != NULL); |
19443 | |
|
19444 | 0 | RETERR(dns_db_getoriginnode(db, &node)); |
19445 | 0 | result = zone_count_ns_rr(zone, db, node, version, NULL, errors, false); |
19446 | 0 | dns_db_detachnode(&node); |
19447 | 0 | return result; |
19448 | 0 | } |
19449 | | |
19450 | | isc_result_t |
19451 | 2 | dns_zone_cdscheck(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version) { |
19452 | 2 | isc_result_t result; |
19453 | 2 | dns_dbnode_t *node = NULL; |
19454 | 2 | dns_rdataset_t dnskey, cds, cdnskey; |
19455 | 2 | unsigned char algorithms[DST_MAX_ALGS]; |
19456 | 2 | unsigned int i; |
19457 | 2 | bool empty = false; |
19458 | | |
19459 | 2 | enum { notexpected = 0, expected = 1, found = 2 }; |
19460 | | |
19461 | 2 | REQUIRE(DNS_ZONE_VALID(zone)); |
19462 | | |
19463 | 2 | RETERR(dns_db_getoriginnode(db, &node)); |
19464 | | |
19465 | 2 | dns_rdataset_init(&cds); |
19466 | 2 | dns_rdataset_init(&dnskey); |
19467 | 2 | dns_rdataset_init(&cdnskey); |
19468 | | |
19469 | 2 | result = dns_db_findrdataset(db, node, version, dns_rdatatype_cds, |
19470 | 2 | dns_rdatatype_none, 0, &cds, NULL); |
19471 | 2 | if (result != ISC_R_NOTFOUND) { |
19472 | 0 | CHECK(result); |
19473 | 0 | } |
19474 | | |
19475 | 2 | result = dns_db_findrdataset(db, node, version, dns_rdatatype_cdnskey, |
19476 | 2 | dns_rdatatype_none, 0, &cdnskey, NULL); |
19477 | 2 | if (result != ISC_R_NOTFOUND) { |
19478 | 0 | CHECK(result); |
19479 | 0 | } |
19480 | | |
19481 | 2 | if (!dns_rdataset_isassociated(&cds) && |
19482 | 2 | !dns_rdataset_isassociated(&cdnskey)) |
19483 | 2 | { |
19484 | 2 | result = ISC_R_SUCCESS; |
19485 | 2 | goto cleanup; |
19486 | 2 | } |
19487 | | |
19488 | 0 | result = dns_db_findrdataset(db, node, version, dns_rdatatype_dnskey, |
19489 | 0 | dns_rdatatype_none, 0, &dnskey, NULL); |
19490 | 0 | if (result == ISC_R_NOTFOUND) { |
19491 | 0 | empty = true; |
19492 | 0 | } else { |
19493 | 0 | CHECK(result); |
19494 | 0 | } |
19495 | | |
19496 | | /* |
19497 | | * For each DNSSEC algorithm in the CDS RRset there must be |
19498 | | * a matching DNSKEY record with the exception of a CDS deletion |
19499 | | * record which must be by itself. |
19500 | | */ |
19501 | 0 | if (dns_rdataset_isassociated(&cds)) { |
19502 | 0 | bool logged_digest_type[DNS_DSDIGEST_MAX + 1] = { 0 }; |
19503 | 0 | bool delete = false; |
19504 | 0 | memset(algorithms, notexpected, sizeof(algorithms)); |
19505 | 0 | DNS_RDATASET_FOREACH(&cds) { |
19506 | 0 | dns_rdata_t crdata = DNS_RDATA_INIT; |
19507 | 0 | dns_rdata_cds_t structcds; |
19508 | |
|
19509 | 0 | dns_rdataset_current(&cds, &crdata); |
19510 | | /* |
19511 | | * CDS deletion record has this form "0 0 0 00" which |
19512 | | * is 5 zero octets. |
19513 | | */ |
19514 | 0 | if (crdata.length == 5U && |
19515 | 0 | memcmp(crdata.data, |
19516 | 0 | (unsigned char[5]){ 0, 0, 0, 0, 0 }, 5) == 0) |
19517 | 0 | { |
19518 | 0 | delete = true; |
19519 | 0 | continue; |
19520 | 0 | } |
19521 | | |
19522 | 0 | if (empty) { |
19523 | 0 | CLEANUP(DNS_R_BADCDS); |
19524 | 0 | } |
19525 | | |
19526 | 0 | CHECK(dns_rdata_tostruct(&crdata, &structcds, NULL)); |
19527 | | |
19528 | | /* |
19529 | | * Log deprecated CDS digest types. |
19530 | | */ |
19531 | 0 | switch (structcds.digest_type) { |
19532 | 0 | case DNS_DSDIGEST_SHA1: |
19533 | 0 | case DNS_DSDIGEST_GOST: |
19534 | 0 | if (!logged_digest_type[structcds.digest_type]) |
19535 | 0 | { |
19536 | 0 | char algbuf[DNS_DSDIGEST_FORMATSIZE]; |
19537 | 0 | dns_dsdigest_format( |
19538 | 0 | structcds.digest_type, algbuf, |
19539 | 0 | sizeof(algbuf)); |
19540 | 0 | dnssec_log(zone, ISC_LOG_WARNING, |
19541 | 0 | "deprecated CDS digest type " |
19542 | 0 | "%u (%s)", |
19543 | 0 | structcds.digest_type, |
19544 | 0 | algbuf); |
19545 | 0 | logged_digest_type[structcds.digest_type] = |
19546 | 0 | true; |
19547 | 0 | } |
19548 | 0 | break; |
19549 | 0 | } |
19550 | | |
19551 | 0 | if (structcds.algorithm != DNS_KEYALG_PRIVATEDNS && |
19552 | 0 | structcds.algorithm != DNS_KEYALG_PRIVATEOID) |
19553 | 0 | { |
19554 | 0 | if (algorithms[structcds.algorithm] == 0) { |
19555 | 0 | algorithms[structcds.algorithm] = |
19556 | 0 | expected; |
19557 | 0 | } |
19558 | 0 | DNS_RDATASET_FOREACH(&dnskey) { |
19559 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
19560 | 0 | dns_rdata_dnskey_t structdnskey; |
19561 | |
|
19562 | 0 | dns_rdataset_current(&dnskey, &rdata); |
19563 | 0 | dns_rdata_tostruct(&rdata, |
19564 | 0 | &structdnskey, NULL); |
19565 | |
|
19566 | 0 | if (structdnskey.algorithm == |
19567 | 0 | structcds.algorithm) |
19568 | 0 | { |
19569 | 0 | algorithms[structcds.algorithm] = |
19570 | 0 | found; |
19571 | 0 | } |
19572 | 0 | } |
19573 | 0 | } else { |
19574 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
19575 | 0 | dns_rdata_dnskey_t structdnskey; |
19576 | 0 | dst_algorithm_t dnskeyalg; |
19577 | | |
19578 | | /* Convert CDS to DS */ |
19579 | 0 | crdata.type = dns_rdatatype_ds; |
19580 | 0 | result = dns_dnssec_matchdskey(&zone->origin, |
19581 | 0 | &crdata, &dnskey, |
19582 | 0 | &rdata); |
19583 | 0 | if (result != ISC_R_SUCCESS) { |
19584 | 0 | CLEANUP(DNS_R_BADCDS); |
19585 | 0 | } |
19586 | 0 | CHECK(dns_rdata_tostruct(&rdata, &structdnskey, |
19587 | 0 | NULL)); |
19588 | 0 | dnskeyalg = dst_algorithm_fromdata( |
19589 | 0 | structdnskey.algorithm, |
19590 | 0 | structdnskey.data, |
19591 | 0 | structdnskey.datalen); |
19592 | 0 | algorithms[dnskeyalg] = found; |
19593 | 0 | } |
19594 | 0 | } |
19595 | 0 | for (i = 0; i < sizeof(algorithms); i++) { |
19596 | 0 | if (delete) { |
19597 | 0 | if (algorithms[i] != notexpected) { |
19598 | 0 | CLEANUP(DNS_R_BADCDS); |
19599 | 0 | } |
19600 | 0 | } else if (algorithms[i] == expected) { |
19601 | 0 | CLEANUP(DNS_R_BADCDS); |
19602 | 0 | } |
19603 | 0 | } |
19604 | 0 | } |
19605 | | |
19606 | | /* |
19607 | | * For each DNSSEC algorithm in the CDNSKEY RRset there must be |
19608 | | * a matching DNSKEY record with the exception of a CDNSKEY deletion |
19609 | | * record which must be by itself. |
19610 | | */ |
19611 | 0 | if (dns_rdataset_isassociated(&cdnskey)) { |
19612 | 0 | bool delete = false; |
19613 | 0 | memset(algorithms, notexpected, sizeof(algorithms)); |
19614 | 0 | DNS_RDATASET_FOREACH(&cdnskey) { |
19615 | 0 | dns_rdata_t crdata = DNS_RDATA_INIT; |
19616 | 0 | dns_rdata_cdnskey_t structcdnskey; |
19617 | 0 | dst_algorithm_t cdnskeyalg; |
19618 | |
|
19619 | 0 | dns_rdataset_current(&cdnskey, &crdata); |
19620 | | /* |
19621 | | * CDNSKEY deletion record has this form |
19622 | | * "0 3 0 AA==" which is 2 zero octets, a 3, |
19623 | | * and 2 zero octets. |
19624 | | */ |
19625 | 0 | if (crdata.length == 5U && |
19626 | 0 | memcmp(crdata.data, |
19627 | 0 | (unsigned char[5]){ 0, 0, 3, 0, 0 }, 5) == 0) |
19628 | 0 | { |
19629 | 0 | delete = true; |
19630 | 0 | continue; |
19631 | 0 | } |
19632 | | |
19633 | 0 | if (empty) { |
19634 | 0 | CLEANUP(DNS_R_BADCDNSKEY); |
19635 | 0 | } |
19636 | | |
19637 | 0 | CHECK(dns_rdata_tostruct(&crdata, &structcdnskey, |
19638 | 0 | NULL)); |
19639 | 0 | cdnskeyalg = dst_algorithm_fromdata( |
19640 | 0 | structcdnskey.algorithm, structcdnskey.data, |
19641 | 0 | structcdnskey.datalen); |
19642 | 0 | if (algorithms[cdnskeyalg] == 0) { |
19643 | 0 | algorithms[cdnskeyalg] = expected; |
19644 | 0 | } |
19645 | 0 | DNS_RDATASET_FOREACH(&dnskey) { |
19646 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
19647 | 0 | dns_rdata_dnskey_t structdnskey; |
19648 | 0 | dst_algorithm_t dnskeyalg; |
19649 | |
|
19650 | 0 | dns_rdataset_current(&dnskey, &rdata); |
19651 | 0 | CHECK(dns_rdata_tostruct(&rdata, &structdnskey, |
19652 | 0 | NULL)); |
19653 | 0 | dnskeyalg = dst_algorithm_fromdata( |
19654 | 0 | structdnskey.algorithm, |
19655 | 0 | structdnskey.data, |
19656 | 0 | structdnskey.datalen); |
19657 | |
|
19658 | 0 | if (dnskeyalg == cdnskeyalg) { |
19659 | 0 | algorithms[cdnskeyalg] = found; |
19660 | 0 | } |
19661 | 0 | } |
19662 | 0 | } |
19663 | 0 | for (i = 0; i < sizeof(algorithms); i++) { |
19664 | 0 | if (delete) { |
19665 | 0 | if (algorithms[i] != notexpected) { |
19666 | 0 | CLEANUP(DNS_R_BADCDNSKEY); |
19667 | 0 | } |
19668 | 0 | } else if (algorithms[i] == expected) { |
19669 | 0 | CLEANUP(DNS_R_BADCDNSKEY); |
19670 | 0 | } |
19671 | 0 | } |
19672 | 0 | } |
19673 | 0 | result = ISC_R_SUCCESS; |
19674 | |
|
19675 | 2 | cleanup: |
19676 | 2 | dns_rdataset_cleanup(&cds); |
19677 | 2 | dns_rdataset_cleanup(&dnskey); |
19678 | 2 | dns_rdataset_cleanup(&cdnskey); |
19679 | 2 | dns_db_detachnode(&node); |
19680 | 2 | return result; |
19681 | 0 | } |
19682 | | |
19683 | | isc_result_t |
19684 | 0 | dns_zone_dlzpostload(dns_zone_t *zone, dns_db_t *db) { |
19685 | 0 | isc_time_t loadtime; |
19686 | 0 | isc_result_t result; |
19687 | 0 | dns_zone_t *secure = NULL; |
19688 | |
|
19689 | 0 | loadtime = isc_time_now(); |
19690 | | |
19691 | | /* |
19692 | | * Lock hierarchy: zmgr, zone, secure. |
19693 | | */ |
19694 | 0 | again: |
19695 | 0 | LOCK_ZONE(zone); |
19696 | 0 | INSIST(zone != zone->raw); |
19697 | 0 | if (dns__zone_inline_raw(zone)) { |
19698 | 0 | secure = zone->secure; |
19699 | 0 | TRYLOCK_ZONE(result, secure); |
19700 | 0 | if (result != ISC_R_SUCCESS) { |
19701 | 0 | UNLOCK_ZONE(zone); |
19702 | 0 | secure = NULL; |
19703 | 0 | isc_thread_yield(); |
19704 | 0 | goto again; |
19705 | 0 | } |
19706 | 0 | } |
19707 | 0 | result = zone_postload(zone, db, loadtime, ISC_R_SUCCESS); |
19708 | 0 | if (result == ISC_R_SUCCESS && dns__zone_inline_secure(zone)) { |
19709 | 0 | zone_schedule_inline_sync(zone, inline_sync_incremental); |
19710 | 0 | } |
19711 | 0 | if (secure != NULL) { |
19712 | 0 | UNLOCK_ZONE(secure); |
19713 | 0 | } |
19714 | 0 | UNLOCK_ZONE(zone); |
19715 | 0 | return result; |
19716 | 0 | } |
19717 | | |
19718 | | /* |
19719 | | * Lock hierarchy: zmgr, zone, raw. |
19720 | | */ |
19721 | | isc_result_t |
19722 | 0 | dns_zone_link(dns_zone_t *zone, dns_zone_t *raw) { |
19723 | 0 | dns_zonemgr_t *zmgr; |
19724 | |
|
19725 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
19726 | 0 | REQUIRE(zone->zmgr != NULL); |
19727 | 0 | REQUIRE(zone->loop != NULL); |
19728 | 0 | REQUIRE(zone->raw == NULL); |
19729 | |
|
19730 | 0 | REQUIRE(DNS_ZONE_VALID(raw)); |
19731 | 0 | REQUIRE(raw->zmgr == NULL); |
19732 | 0 | REQUIRE(raw->loop == NULL); |
19733 | 0 | REQUIRE(raw->secure == NULL); |
19734 | |
|
19735 | 0 | REQUIRE(zone != raw); |
19736 | | |
19737 | | /* |
19738 | | * Lock hierarchy: zmgr, zone, raw. |
19739 | | */ |
19740 | 0 | zmgr = zone->zmgr; |
19741 | 0 | RWLOCK(&zmgr->rwlock, isc_rwlocktype_write); |
19742 | 0 | LOCK_ZONE(zone); |
19743 | 0 | LOCK_ZONE(raw); |
19744 | |
|
19745 | 0 | isc_loop_attach(zone->loop, &raw->loop); |
19746 | | |
19747 | | /* dns_zone_attach(raw, &zone->raw); */ |
19748 | 0 | isc_refcount_increment(&raw->references); |
19749 | 0 | zone->raw = raw; |
19750 | | |
19751 | | /* dns_zone_iattach(zone, &raw->secure); */ |
19752 | 0 | zone_iattach(zone, &raw->secure); |
19753 | |
|
19754 | 0 | ISC_LIST_APPEND(zmgr->zones, raw, link); |
19755 | 0 | raw->zmgr = zmgr; |
19756 | 0 | isc_refcount_increment(&zmgr->refs); |
19757 | |
|
19758 | 0 | UNLOCK_ZONE(raw); |
19759 | 0 | UNLOCK_ZONE(zone); |
19760 | 0 | RWUNLOCK(&zmgr->rwlock, isc_rwlocktype_write); |
19761 | 0 | return ISC_R_SUCCESS; |
19762 | 0 | } |
19763 | | |
19764 | | void |
19765 | 0 | dns_zone_getraw(dns_zone_t *zone, dns_zone_t **raw) { |
19766 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
19767 | 0 | REQUIRE(raw != NULL && *raw == NULL); |
19768 | |
|
19769 | 0 | LOCK(&zone->lock); |
19770 | 0 | INSIST(zone != zone->raw); |
19771 | 0 | if (zone->raw != NULL) { |
19772 | 0 | dns_zone_attach(zone->raw, raw); |
19773 | 0 | } |
19774 | 0 | UNLOCK(&zone->lock); |
19775 | 0 | } |
19776 | | |
19777 | | bool |
19778 | 0 | dns_zone_israw(dns_zone_t *zone) { |
19779 | 0 | bool israw; |
19780 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
19781 | 0 | LOCK(&zone->lock); |
19782 | 0 | israw = zone->secure != NULL; |
19783 | 0 | UNLOCK(&zone->lock); |
19784 | 0 | return israw; |
19785 | 0 | } |
19786 | | |
19787 | | bool |
19788 | 0 | dns_zone_issecure(dns_zone_t *zone) { |
19789 | 0 | bool issecure; |
19790 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
19791 | 0 | LOCK(&zone->lock); |
19792 | 0 | issecure = zone->raw != NULL; |
19793 | 0 | UNLOCK(&zone->lock); |
19794 | 0 | return issecure; |
19795 | 0 | } |
19796 | | |
19797 | 0 | #define PENDINGFLAGS (DNS_NSEC3FLAG_CREATE | DNS_NSEC3FLAG_INITIAL) |
19798 | | |
19799 | | static void |
19800 | | zone_process_keydone(dns_zone_t *zone, |
19801 | 0 | const zone_maintenance_request_t *request) { |
19802 | 0 | bool commit = false; |
19803 | 0 | isc_result_t result; |
19804 | 0 | dns_dbversion_t *oldver = NULL, *newver = NULL; |
19805 | 0 | dns_db_t *db = NULL; |
19806 | 0 | dns_dbnode_t *node = NULL; |
19807 | 0 | dns_rdataset_t rdataset; |
19808 | 0 | dns_diff_t diff; |
19809 | 0 | dns_update_log_t log = { update_log_cb, NULL }; |
19810 | 0 | bool clear_pending = false; |
19811 | |
|
19812 | 0 | INSIST(DNS_ZONE_VALID(zone)); |
19813 | |
|
19814 | 0 | ENTER; |
19815 | |
|
19816 | 0 | dns_rdataset_init(&rdataset); |
19817 | 0 | dns_diff_init(zone->mctx, &diff); |
19818 | |
|
19819 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
19820 | 0 | if (zone->db != NULL) { |
19821 | 0 | dns_db_attach(zone->db, &db); |
19822 | 0 | } |
19823 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
19824 | 0 | if (db == NULL) { |
19825 | 0 | goto cleanup; |
19826 | 0 | } |
19827 | | |
19828 | 0 | dns_db_currentversion(db, &oldver); |
19829 | 0 | result = dns_db_newversion(db, &newver); |
19830 | 0 | if (result != ISC_R_SUCCESS) { |
19831 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
19832 | 0 | "keydone:dns_db_newversion -> %s", |
19833 | 0 | isc_result_totext(result)); |
19834 | 0 | goto cleanup; |
19835 | 0 | } |
19836 | | |
19837 | 0 | CHECK(dns_db_getoriginnode(db, &node)); |
19838 | |
|
19839 | 0 | result = dns_db_findrdataset(db, node, newver, zone->privatetype, |
19840 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
19841 | 0 | if (result != ISC_R_SUCCESS) { |
19842 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
19843 | 0 | goto cleanup; |
19844 | 0 | } |
19845 | | |
19846 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
19847 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
19848 | 0 | bool found = false; |
19849 | |
|
19850 | 0 | dns_rdataset_current(&rdataset, &rdata); |
19851 | |
|
19852 | 0 | if (request->u.keydone.all) { |
19853 | | /* Old (5) and new (7) forms */ |
19854 | 0 | if ((rdata.length == OLD_SIGNING_RECORD_SIZE || |
19855 | 0 | rdata.length == SIGNING_RECORD_SIZE) && |
19856 | 0 | rdata.data[0] != 0 && rdata.data[3] == 0 && |
19857 | 0 | rdata.data[4] == 1) |
19858 | 0 | { |
19859 | 0 | found = true; |
19860 | 0 | } else if (rdata.data[0] == 0 && |
19861 | 0 | (rdata.data[2] & PENDINGFLAGS) != 0) |
19862 | 0 | { |
19863 | 0 | found = true; |
19864 | 0 | clear_pending = true; |
19865 | 0 | } |
19866 | 0 | } else if (rdata.length == OLD_SIGNING_RECORD_SIZE && |
19867 | 0 | memcmp(rdata.data, request->u.keydone.data, |
19868 | 0 | OLD_SIGNING_RECORD_SIZE) == 0) |
19869 | 0 | { |
19870 | 0 | found = true; |
19871 | 0 | } else if (rdata.length == SIGNING_RECORD_SIZE && |
19872 | 0 | memcmp(rdata.data, request->u.keydone.data, |
19873 | 0 | SIGNING_RECORD_SIZE) == 0) |
19874 | 0 | { |
19875 | 0 | found = true; |
19876 | 0 | } |
19877 | |
|
19878 | 0 | if (found) { |
19879 | 0 | CHECK(update_one_rr(db, newver, &diff, DNS_DIFFOP_DEL, |
19880 | 0 | &zone->origin, rdataset.ttl, |
19881 | 0 | &rdata)); |
19882 | 0 | } |
19883 | 0 | } |
19884 | | |
19885 | 0 | if (!ISC_LIST_EMPTY(diff.tuples)) { |
19886 | | /* Write changes to journal file. */ |
19887 | 0 | CHECK(update_soa_serial(zone, db, newver, &diff, zone->mctx, |
19888 | 0 | zone->updatemethod)); |
19889 | |
|
19890 | 0 | result = dns_update_signatures(&log, zone, db, oldver, newver, |
19891 | 0 | &diff, |
19892 | 0 | zone->sigvalidityinterval); |
19893 | 0 | if (!clear_pending) { |
19894 | 0 | CHECK(result); |
19895 | 0 | } |
19896 | | |
19897 | 0 | CHECK(zone_journal(zone, &diff, NULL, "keydone")); |
19898 | 0 | commit = true; |
19899 | |
|
19900 | 0 | LOCK_ZONE(zone); |
19901 | 0 | DNS_ZONE_SETFLAG(zone, |
19902 | 0 | DNS_ZONEFLG_LOADED | DNS_ZONEFLG_NEEDNOTIFY); |
19903 | 0 | zone_needdump(zone, 30); |
19904 | 0 | UNLOCK_ZONE(zone); |
19905 | 0 | } |
19906 | | |
19907 | 0 | cleanup: |
19908 | 0 | dns_rdataset_cleanup(&rdataset); |
19909 | 0 | if (db != NULL) { |
19910 | 0 | if (node != NULL) { |
19911 | 0 | dns_db_detachnode(&node); |
19912 | 0 | } |
19913 | 0 | if (oldver != NULL) { |
19914 | 0 | dns_db_closeversion(db, &oldver, false); |
19915 | 0 | } |
19916 | 0 | if (newver != NULL) { |
19917 | 0 | dns_db_closeversion(db, &newver, commit); |
19918 | 0 | } |
19919 | 0 | dns_db_detach(&db); |
19920 | 0 | } |
19921 | 0 | dns_diff_clear(&diff); |
19922 | |
|
19923 | 0 | INSIST(oldver == NULL); |
19924 | 0 | INSIST(newver == NULL); |
19925 | 0 | } |
19926 | | |
19927 | | isc_result_t |
19928 | 0 | dns_zone_keydone(dns_zone_t *zone, const char *keystr) { |
19929 | 0 | isc_result_t result = ISC_R_SUCCESS; |
19930 | 0 | zone_maintenance_request_t *request = NULL; |
19931 | 0 | isc_buffer_t b; |
19932 | 0 | bool all = false; |
19933 | |
|
19934 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
19935 | |
|
19936 | 0 | LOCK_ZONE(zone); |
19937 | |
|
19938 | 0 | all = strcasecmp(keystr, "all") == 0; |
19939 | 0 | request = isc_mem_get(zone->mctx, sizeof(*request)); |
19940 | 0 | *request = (zone_maintenance_request_t){ |
19941 | 0 | .link = ISC_LINK_INITIALIZER, |
19942 | 0 | .type = zone_maintenance_request_keydone, |
19943 | 0 | .u.keydone = { |
19944 | 0 | .all = all, |
19945 | 0 | }, |
19946 | 0 | }; |
19947 | |
|
19948 | 0 | if (!all) { |
19949 | 0 | isc_textregion_t r; |
19950 | 0 | const char *algstr = NULL; |
19951 | 0 | dns_keytag_t keyid; |
19952 | 0 | dst_algorithm_t alg; |
19953 | 0 | size_t n; |
19954 | |
|
19955 | 0 | n = sscanf(keystr, "%hu/", &keyid); |
19956 | 0 | if (n == 0U) { |
19957 | 0 | CLEANUP(ISC_R_FAILURE); |
19958 | 0 | } |
19959 | | |
19960 | 0 | algstr = strchr(keystr, '/'); |
19961 | 0 | if (algstr != NULL) { |
19962 | 0 | algstr++; |
19963 | 0 | } else { |
19964 | 0 | CLEANUP(ISC_R_FAILURE); |
19965 | 0 | } |
19966 | | |
19967 | 0 | n = sscanf(algstr, "%u", &alg); |
19968 | 0 | if (n == 0U) { |
19969 | 0 | r.base = UNCONST(algstr); |
19970 | 0 | r.length = strlen(algstr); |
19971 | 0 | CHECK(dst_algorithm_fromtext(&alg, &r)); |
19972 | 0 | } |
19973 | | |
19974 | | /* construct a private-type rdata */ |
19975 | 0 | isc_buffer_init(&b, request->u.keydone.data, |
19976 | 0 | sizeof(request->u.keydone.data)); |
19977 | 0 | isc_buffer_putuint8(&b, dst_algorithm_tosecalg(alg)); |
19978 | 0 | isc_buffer_putuint16(&b, keyid); |
19979 | 0 | isc_buffer_putuint8(&b, 0); |
19980 | 0 | isc_buffer_putuint8(&b, 1); |
19981 | 0 | isc_buffer_putuint16(&b, alg); |
19982 | 0 | } |
19983 | | |
19984 | 0 | ISC_LIST_APPEND(zone->maintenance_queue, request, link); |
19985 | 0 | if (zone->loop != NULL && zone_maintenance_request_pending(zone)) { |
19986 | 0 | isc_time_t now = isc_time_now(); |
19987 | 0 | dns__zone_settimer(zone, now); |
19988 | 0 | } |
19989 | 0 | request = NULL; |
19990 | |
|
19991 | 0 | cleanup: |
19992 | 0 | if (request != NULL) { |
19993 | 0 | isc_mem_put(zone->mctx, request, sizeof(*request)); |
19994 | 0 | } |
19995 | 0 | UNLOCK_ZONE(zone); |
19996 | 0 | return result; |
19997 | 0 | } |
19998 | | |
19999 | | static void |
20000 | | salt2text(unsigned char *salt, uint8_t saltlen, unsigned char *text, |
20001 | 0 | unsigned int textlen) { |
20002 | 0 | isc_region_t r; |
20003 | 0 | isc_buffer_t buf; |
20004 | 0 | isc_result_t result; |
20005 | |
|
20006 | 0 | r.base = salt; |
20007 | 0 | r.length = (unsigned int)saltlen; |
20008 | |
|
20009 | 0 | isc_buffer_init(&buf, text, textlen); |
20010 | 0 | result = isc_hex_totext(&r, 2, "", &buf); |
20011 | 0 | if (result == ISC_R_SUCCESS) { |
20012 | 0 | text[saltlen * 2] = 0; |
20013 | 0 | } else { |
20014 | 0 | text[0] = 0; |
20015 | 0 | } |
20016 | 0 | } |
20017 | | |
20018 | | /* |
20019 | | * Check whether NSEC3 chain addition or removal specified by the private-type |
20020 | | * record passed with the event was already queued (or even fully performed). |
20021 | | * If not, modify the relevant private-type records at the zone apex and call |
20022 | | * resume_addnsec3chain(). |
20023 | | */ |
20024 | | static void |
20025 | 0 | rss_post(dns_zone_t *zone, nsec3param_t *np) { |
20026 | 0 | bool commit = false; |
20027 | 0 | isc_result_t result; |
20028 | 0 | dns_dbversion_t *oldver = NULL, *newver = NULL; |
20029 | 0 | dns_db_t *db = NULL; |
20030 | 0 | dns_dbnode_t *node = NULL; |
20031 | 0 | dns_rdataset_t prdataset, nrdataset; |
20032 | 0 | dns_diff_t diff; |
20033 | 0 | dns_update_log_t log = { update_log_cb, NULL }; |
20034 | 0 | bool nseconly; |
20035 | 0 | bool exists = false; |
20036 | |
|
20037 | 0 | ENTER; |
20038 | |
|
20039 | 0 | dns_rdataset_init(&prdataset); |
20040 | 0 | dns_rdataset_init(&nrdataset); |
20041 | 0 | dns_diff_init(zone->mctx, &diff); |
20042 | |
|
20043 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
20044 | 0 | if (zone->db != NULL) { |
20045 | 0 | dns_db_attach(zone->db, &db); |
20046 | 0 | } |
20047 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
20048 | 0 | if (db == NULL) { |
20049 | 0 | goto cleanup; |
20050 | 0 | } |
20051 | | |
20052 | 0 | dns_db_currentversion(db, &oldver); |
20053 | 0 | result = dns_db_newversion(db, &newver); |
20054 | 0 | if (result != ISC_R_SUCCESS) { |
20055 | 0 | dnssec_log(zone, ISC_LOG_ERROR, |
20056 | 0 | "setnsec3param:dns_db_newversion -> %s", |
20057 | 0 | isc_result_totext(result)); |
20058 | 0 | goto cleanup; |
20059 | 0 | } |
20060 | | |
20061 | 0 | CHECK(dns_db_getoriginnode(db, &node)); |
20062 | | |
20063 | | /* |
20064 | | * Do we need to look up the NSEC3 parameters? |
20065 | | */ |
20066 | 0 | if (np->lookup) { |
20067 | 0 | dns_rdata_nsec3param_t param; |
20068 | 0 | dns_rdata_t nrdata = DNS_RDATA_INIT; |
20069 | 0 | dns_rdata_t prdata = DNS_RDATA_INIT; |
20070 | 0 | unsigned char nbuf[DNS_NSEC3PARAM_BUFFERSIZE]; |
20071 | 0 | unsigned char saltbuf[255]; |
20072 | 0 | isc_buffer_t b; |
20073 | |
|
20074 | 0 | param.salt = (isc_region_t){ .base = NULL }; |
20075 | 0 | result = dns__zone_lookup_nsec3param(zone, &np->rdata, ¶m, |
20076 | 0 | saltbuf, np->resalt); |
20077 | 0 | if (result == ISC_R_SUCCESS) { |
20078 | | /* |
20079 | | * Success because the NSEC3PARAM already exists, but |
20080 | | * function returns void, so goto cleanup. |
20081 | | */ |
20082 | 0 | goto cleanup; |
20083 | 0 | } |
20084 | 0 | if (result != DNS_R_NSEC3RESALT && result != ISC_R_NOTFOUND) { |
20085 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
20086 | 0 | "setnsec3param:lookup nsec3param -> %s", |
20087 | 0 | isc_result_totext(result)); |
20088 | 0 | goto cleanup; |
20089 | 0 | } |
20090 | | |
20091 | 0 | INSIST(param.salt.base != NULL); |
20092 | | |
20093 | | /* Update NSEC3 parameters. */ |
20094 | 0 | np->rdata.hash = param.hash; |
20095 | 0 | np->rdata.flags = param.flags; |
20096 | 0 | np->rdata.iterations = param.iterations; |
20097 | 0 | np->rdata.salt.length = param.salt.length; |
20098 | 0 | np->rdata.salt = param.salt; |
20099 | |
|
20100 | 0 | isc_buffer_init(&b, nbuf, sizeof(nbuf)); |
20101 | 0 | CHECK(dns_rdata_fromstruct(&nrdata, zone->rdclass, |
20102 | 0 | dns_rdatatype_nsec3param, &np->rdata, |
20103 | 0 | &b)); |
20104 | 0 | dns_nsec3param_toprivate(&nrdata, &prdata, zone->privatetype, |
20105 | 0 | np->data, sizeof(np->data)); |
20106 | 0 | np->length = prdata.length; |
20107 | 0 | np->nsec = false; |
20108 | 0 | } |
20109 | | |
20110 | | /* |
20111 | | * Does a private-type record already exist for this chain? |
20112 | | */ |
20113 | 0 | result = dns_db_findrdataset(db, node, newver, zone->privatetype, |
20114 | 0 | dns_rdatatype_none, 0, &prdataset, NULL); |
20115 | 0 | if (result == ISC_R_SUCCESS) { |
20116 | 0 | DNS_RDATASET_FOREACH(&prdataset) { |
20117 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
20118 | 0 | dns_rdataset_current(&prdataset, &rdata); |
20119 | |
|
20120 | 0 | if (np->length == rdata.length && |
20121 | 0 | memcmp(rdata.data, np->data, np->length) == 0) |
20122 | 0 | { |
20123 | 0 | exists = true; |
20124 | 0 | break; |
20125 | 0 | } |
20126 | 0 | } |
20127 | 0 | } else if (result != ISC_R_NOTFOUND) { |
20128 | 0 | INSIST(!dns_rdataset_isassociated(&prdataset)); |
20129 | 0 | goto cleanup; |
20130 | 0 | } |
20131 | | |
20132 | | /* |
20133 | | * Does the chain already exist? |
20134 | | */ |
20135 | 0 | result = dns_db_findrdataset(db, node, newver, dns_rdatatype_nsec3param, |
20136 | 0 | dns_rdatatype_none, 0, &nrdataset, NULL); |
20137 | 0 | if (result == ISC_R_SUCCESS) { |
20138 | 0 | DNS_RDATASET_FOREACH(&nrdataset) { |
20139 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
20140 | 0 | dns_rdataset_current(&nrdataset, &rdata); |
20141 | |
|
20142 | 0 | if (np->length == ((unsigned int)rdata.length + 1) && |
20143 | 0 | memcmp(rdata.data, np->data + 1, np->length - 1) == |
20144 | 0 | 0) |
20145 | 0 | { |
20146 | 0 | exists = true; |
20147 | 0 | break; |
20148 | 0 | } |
20149 | 0 | } |
20150 | 0 | } else if (result != ISC_R_NOTFOUND) { |
20151 | 0 | INSIST(!dns_rdataset_isassociated(&nrdataset)); |
20152 | 0 | goto cleanup; |
20153 | 0 | } |
20154 | | |
20155 | | /* |
20156 | | * We need to remove any existing NSEC3 chains if the supplied NSEC3 |
20157 | | * parameters are supposed to replace the current ones or if we are |
20158 | | * switching to NSEC. |
20159 | | */ |
20160 | 0 | if (!exists && np->replace && (np->length != 0 || np->nsec)) { |
20161 | 0 | CHECK(dns_nsec3param_deletechains(db, newver, zone, !np->nsec, |
20162 | 0 | &diff)); |
20163 | 0 | } |
20164 | | |
20165 | 0 | if (!exists && np->length != 0) { |
20166 | | /* |
20167 | | * We're creating an NSEC3 chain. Add the private-type record |
20168 | | * passed in the request parameters to the zone apex. |
20169 | | * |
20170 | | * If the zone is not currently capable of supporting an NSEC3 |
20171 | | * chain (due to the DNSKEY RRset at the zone apex not existing |
20172 | | * or containing at least one key using an NSEC-only |
20173 | | * algorithm), add the INITIAL flag, so these parameters can be |
20174 | | * used later when NSEC3 becomes available. |
20175 | | */ |
20176 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
20177 | |
|
20178 | 0 | np->data[2] |= DNS_NSEC3FLAG_CREATE; |
20179 | 0 | result = dns_nsec_nseconly(db, newver, NULL, &nseconly); |
20180 | 0 | if (result == ISC_R_NOTFOUND || nseconly) { |
20181 | 0 | np->data[2] |= DNS_NSEC3FLAG_INITIAL; |
20182 | 0 | } |
20183 | |
|
20184 | 0 | rdata.length = np->length; |
20185 | 0 | rdata.data = np->data; |
20186 | 0 | rdata.type = zone->privatetype; |
20187 | 0 | rdata.rdclass = zone->rdclass; |
20188 | 0 | CHECK(update_one_rr(db, newver, &diff, DNS_DIFFOP_ADD, |
20189 | 0 | &zone->origin, 0, &rdata)); |
20190 | 0 | } |
20191 | | |
20192 | | /* |
20193 | | * If we changed anything in the zone, write changes to journal file |
20194 | | * and set commit to true so that resume_addnsec3chain() will be |
20195 | | * called below in order to kick off adding/removing relevant NSEC3 |
20196 | | * records. |
20197 | | */ |
20198 | 0 | if (!ISC_LIST_EMPTY(diff.tuples)) { |
20199 | 0 | CHECK(update_soa_serial(zone, db, newver, &diff, zone->mctx, |
20200 | 0 | zone->updatemethod)); |
20201 | 0 | result = dns_update_signatures(&log, zone, db, oldver, newver, |
20202 | 0 | &diff, |
20203 | 0 | zone->sigvalidityinterval); |
20204 | 0 | if (result != ISC_R_NOTFOUND) { |
20205 | 0 | CHECK(result); |
20206 | 0 | } |
20207 | 0 | CHECK(zone_journal(zone, &diff, NULL, "setnsec3param")); |
20208 | 0 | commit = true; |
20209 | 0 | } |
20210 | | |
20211 | 0 | cleanup: |
20212 | 0 | dns_rdataset_cleanup(&prdataset); |
20213 | 0 | dns_rdataset_cleanup(&nrdataset); |
20214 | 0 | if (node != NULL) { |
20215 | 0 | dns_db_detachnode(&node); |
20216 | 0 | } |
20217 | 0 | if (oldver != NULL) { |
20218 | 0 | dns_db_closeversion(db, &oldver, false); |
20219 | 0 | } |
20220 | 0 | if (newver != NULL) { |
20221 | 0 | dns_db_closeversion(db, &newver, commit); |
20222 | 0 | } |
20223 | 0 | if (db != NULL) { |
20224 | 0 | dns_db_detach(&db); |
20225 | 0 | } |
20226 | 0 | if (commit) { |
20227 | 0 | LOCK_ZONE(zone); |
20228 | 0 | DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADED); |
20229 | 0 | zone_needdump(zone, 30); |
20230 | 0 | resume_addnsec3chain(zone); |
20231 | 0 | UNLOCK_ZONE(zone); |
20232 | 0 | } |
20233 | 0 | dns_diff_clear(&diff); |
20234 | |
|
20235 | 0 | INSIST(oldver == NULL); |
20236 | 0 | INSIST(newver == NULL); |
20237 | 0 | } |
20238 | | |
20239 | | /* |
20240 | | * Check if zone has NSEC3PARAM (and thus a chain) with the right parameters. |
20241 | | * |
20242 | | * If 'salt' is NULL, a match is found if the salt has the requested length, |
20243 | | * otherwise the NSEC3 salt must match the requested salt value too. |
20244 | | * |
20245 | | * Returns ISC_R_SUCCESS, if a match is found, or an error if no match is |
20246 | | * found, or if the db lookup failed. |
20247 | | */ |
20248 | | isc_result_t |
20249 | | dns__zone_lookup_nsec3param(dns_zone_t *zone, dns_rdata_nsec3param_t *lookup, |
20250 | | dns_rdata_nsec3param_t *param, |
20251 | 0 | unsigned char saltbuf[255], bool resalt) { |
20252 | 0 | isc_result_t result = ISC_R_UNEXPECTED; |
20253 | 0 | dns_dbnode_t *node = NULL; |
20254 | 0 | dns_db_t *db = NULL; |
20255 | 0 | dns_dbversion_t *version = NULL; |
20256 | 0 | dns_rdataset_t rdataset; |
20257 | 0 | dns_rdata_nsec3param_t nsec3param; |
20258 | |
|
20259 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
20260 | |
|
20261 | 0 | dns_rdataset_init(&rdataset); |
20262 | |
|
20263 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
20264 | 0 | if (zone->db != NULL) { |
20265 | 0 | dns_db_attach(zone->db, &db); |
20266 | 0 | } |
20267 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
20268 | 0 | if (db == NULL) { |
20269 | 0 | result = ISC_R_FAILURE; |
20270 | 0 | goto setparam; |
20271 | 0 | } |
20272 | | |
20273 | 0 | result = dns_db_findnode(db, &zone->origin, false, &node); |
20274 | 0 | if (result != ISC_R_SUCCESS) { |
20275 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
20276 | 0 | "dns__zone_lookup_nsec3param:" |
20277 | 0 | "dns_db_findnode -> %s", |
20278 | 0 | isc_result_totext(result)); |
20279 | 0 | result = ISC_R_FAILURE; |
20280 | 0 | goto setparam; |
20281 | 0 | } |
20282 | 0 | dns_db_currentversion(db, &version); |
20283 | |
|
20284 | 0 | result = dns_db_findrdataset(db, node, version, |
20285 | 0 | dns_rdatatype_nsec3param, |
20286 | 0 | dns_rdatatype_none, 0, &rdataset, NULL); |
20287 | 0 | if (result != ISC_R_SUCCESS) { |
20288 | 0 | INSIST(!dns_rdataset_isassociated(&rdataset)); |
20289 | 0 | if (result != ISC_R_NOTFOUND) { |
20290 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
20291 | 0 | "dns__zone_lookup_nsec3param:" |
20292 | 0 | "dns_db_findrdataset -> %s", |
20293 | 0 | isc_result_totext(result)); |
20294 | 0 | } |
20295 | 0 | goto setparam; |
20296 | 0 | } |
20297 | | |
20298 | 0 | result = ISC_R_NOTFOUND; |
20299 | 0 | DNS_RDATASET_FOREACH(&rdataset) { |
20300 | 0 | dns_rdata_t rdata = DNS_RDATA_INIT; |
20301 | 0 | dns_rdataset_current(&rdataset, &rdata); |
20302 | 0 | dns_rdata_tostruct(&rdata, &nsec3param, NULL); |
20303 | | |
20304 | | /* Check parameters. */ |
20305 | 0 | if (nsec3param.hash != lookup->hash) { |
20306 | 0 | continue; |
20307 | 0 | } |
20308 | 0 | if (nsec3param.iterations != lookup->iterations) { |
20309 | 0 | continue; |
20310 | 0 | } |
20311 | 0 | if (nsec3param.salt.length != lookup->salt.length) { |
20312 | 0 | continue; |
20313 | 0 | } |
20314 | 0 | if (lookup->salt.base != NULL) { |
20315 | 0 | if (memcmp(nsec3param.salt.base, lookup->salt.base, |
20316 | 0 | lookup->salt.length) != 0) |
20317 | 0 | { |
20318 | 0 | continue; |
20319 | 0 | } |
20320 | 0 | } |
20321 | | /* Found a match. */ |
20322 | 0 | result = ISC_R_SUCCESS; |
20323 | 0 | param->hash = nsec3param.hash; |
20324 | 0 | param->flags = nsec3param.flags; |
20325 | 0 | param->iterations = nsec3param.iterations; |
20326 | 0 | param->salt = nsec3param.salt; |
20327 | 0 | break; |
20328 | 0 | } |
20329 | |
|
20330 | 0 | setparam: |
20331 | 0 | if (result != ISC_R_SUCCESS) { |
20332 | | /* Found no match. */ |
20333 | 0 | param->hash = lookup->hash; |
20334 | 0 | param->flags = lookup->flags; |
20335 | 0 | param->iterations = lookup->iterations; |
20336 | 0 | param->salt = lookup->salt; |
20337 | 0 | } |
20338 | |
|
20339 | 0 | if (result != ISC_R_NOTFOUND) { |
20340 | 0 | CHECK(result); |
20341 | 0 | } |
20342 | | |
20343 | 0 | if (param->salt.length == 0) { |
20344 | 0 | param->salt.base = (unsigned char *)"-"; |
20345 | 0 | } else if (resalt || param->salt.base == NULL) { |
20346 | 0 | unsigned char *newsalt; |
20347 | 0 | unsigned char salttext[255 * 2 + 1]; |
20348 | 0 | do { |
20349 | | /* Generate a new salt. */ |
20350 | 0 | result = dns_nsec3_generate_salt(saltbuf, |
20351 | 0 | param->salt.length); |
20352 | 0 | if (result != ISC_R_SUCCESS) { |
20353 | 0 | break; |
20354 | 0 | } |
20355 | 0 | newsalt = saltbuf; |
20356 | 0 | salt2text(newsalt, param->salt.length, salttext, |
20357 | 0 | sizeof(salttext)); |
20358 | 0 | dnssec_log(zone, ISC_LOG_INFO, "generated salt: %s", |
20359 | 0 | salttext); |
20360 | | /* Check for salt conflict. */ |
20361 | 0 | if (param->salt.base != NULL && |
20362 | 0 | memcmp(newsalt, param->salt.base, |
20363 | 0 | param->salt.length) == 0) |
20364 | 0 | { |
20365 | 0 | result = ISC_R_SUCCESS; |
20366 | 0 | } else { |
20367 | 0 | param->salt.base = newsalt; |
20368 | 0 | result = DNS_R_NSEC3RESALT; |
20369 | 0 | } |
20370 | 0 | } while (result == ISC_R_SUCCESS); |
20371 | |
|
20372 | 0 | INSIST(result != ISC_R_SUCCESS); |
20373 | 0 | } |
20374 | |
|
20375 | 0 | cleanup: |
20376 | 0 | dns_rdataset_cleanup(&rdataset); |
20377 | 0 | if (node != NULL) { |
20378 | 0 | dns_db_detachnode(&node); |
20379 | 0 | } |
20380 | 0 | if (version != NULL) { |
20381 | 0 | dns_db_closeversion(db, &version, false); |
20382 | 0 | } |
20383 | 0 | if (db != NULL) { |
20384 | 0 | dns_db_detach(&db); |
20385 | 0 | } |
20386 | |
|
20387 | 0 | return result; |
20388 | 0 | } |
20389 | | |
20390 | | /* |
20391 | | * Called when an "rndc signing -nsec3param ..." command is received, or the |
20392 | | * 'dnssec-policy' has changed. |
20393 | | * |
20394 | | * Allocate and prepare an nsec3param_t structure which holds information about |
20395 | | * the NSEC3 changes requested for the zone: |
20396 | | * |
20397 | | * - if NSEC3 is to be disabled ("-nsec3param none"), only set the "nsec" |
20398 | | * field of the structure to true and the "replace" field to the value |
20399 | | * of the "replace" argument, leaving other fields initialized to zeros, to |
20400 | | * signal that the zone should be signed using NSEC instead of NSEC3, |
20401 | | * |
20402 | | * - otherwise, prepare NSEC3PARAM RDATA that will eventually be inserted at |
20403 | | * the zone apex, convert it to a private-type record and store the latter |
20404 | | * in the "data" field of the nsec3param_t structure. |
20405 | | * |
20406 | | * Once the nsec3param_t structure is prepared, queue it for zone maintenance. |
20407 | | * The request is processed once the zone DB is loaded and no inline-signing |
20408 | | * transaction is active. |
20409 | | */ |
20410 | | isc_result_t |
20411 | | dns_zone_setnsec3param(dns_zone_t *zone, uint8_t hash, uint8_t flags, |
20412 | | uint16_t iter, isc_region_t *salt, bool replace, |
20413 | 0 | bool resalt) { |
20414 | 0 | isc_result_t result = ISC_R_SUCCESS; |
20415 | 0 | dns_rdata_nsec3param_t param, lookup; |
20416 | 0 | dns_rdata_t nrdata = DNS_RDATA_INIT; |
20417 | 0 | dns_rdata_t prdata = DNS_RDATA_INIT; |
20418 | 0 | unsigned char nbuf[DNS_NSEC3PARAM_BUFFERSIZE]; |
20419 | 0 | unsigned char saltbuf[255]; |
20420 | 0 | zone_maintenance_request_t *request = NULL; |
20421 | 0 | nsec3param_t *np = NULL; |
20422 | 0 | isc_buffer_t b; |
20423 | 0 | bool do_lookup = false; |
20424 | |
|
20425 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
20426 | |
|
20427 | 0 | LOCK_ZONE(zone); |
20428 | | |
20429 | | /* |
20430 | | * First check if the requested NSEC3 parameters are already |
20431 | | * set, if so, no need to set again. |
20432 | | */ |
20433 | 0 | if (hash != 0) { |
20434 | 0 | lookup.hash = hash; |
20435 | 0 | lookup.flags = flags; |
20436 | 0 | lookup.iterations = iter; |
20437 | 0 | lookup.salt = *salt; |
20438 | 0 | param.salt = (isc_region_t){ .base = NULL }; |
20439 | 0 | result = dns__zone_lookup_nsec3param(zone, &lookup, ¶m, |
20440 | 0 | saltbuf, resalt); |
20441 | 0 | if (result == ISC_R_SUCCESS) { |
20442 | 0 | UNLOCK_ZONE(zone); |
20443 | 0 | return ISC_R_SUCCESS; |
20444 | 0 | } |
20445 | | /* |
20446 | | * Schedule lookup if lookup above failed (may happen if |
20447 | | * zone db is NULL for example). |
20448 | | */ |
20449 | 0 | do_lookup = (param.salt.base == NULL) ? true : false; |
20450 | 0 | } |
20451 | | |
20452 | 0 | request = isc_mem_get(zone->mctx, sizeof(*request)); |
20453 | 0 | *request = (zone_maintenance_request_t){ |
20454 | 0 | .link = ISC_LINK_INITIALIZER, |
20455 | 0 | .type = zone_maintenance_request_setnsec3param, |
20456 | 0 | .u.nsec3param = { |
20457 | 0 | .replace = replace, |
20458 | 0 | .resalt = resalt, |
20459 | 0 | .lookup = do_lookup, |
20460 | 0 | }, |
20461 | 0 | }; |
20462 | |
|
20463 | 0 | np = &request->u.nsec3param; |
20464 | 0 | if (hash == 0) { |
20465 | 0 | np->nsec = true; |
20466 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), "setnsec3param:nsec"); |
20467 | 0 | } else { |
20468 | 0 | param.common.rdclass = zone->rdclass; |
20469 | 0 | param.common.rdtype = dns_rdatatype_nsec3param; |
20470 | 0 | param.mctx = NULL; |
20471 | | /* |
20472 | | * nsec3 specific param set in |
20473 | | * dns__zone_lookup_nsec3param() |
20474 | | */ |
20475 | 0 | isc_buffer_init(&b, nbuf, sizeof(nbuf)); |
20476 | |
|
20477 | 0 | if (param.salt.base != NULL) { |
20478 | 0 | CHECK(dns_rdata_fromstruct(&nrdata, zone->rdclass, |
20479 | 0 | dns_rdatatype_nsec3param, |
20480 | 0 | ¶m, &b)); |
20481 | 0 | dns_nsec3param_toprivate(&nrdata, &prdata, |
20482 | 0 | zone->privatetype, np->data, |
20483 | 0 | sizeof(np->data)); |
20484 | 0 | np->length = prdata.length; |
20485 | 0 | } |
20486 | | |
20487 | 0 | np->rdata = param; |
20488 | |
|
20489 | 0 | if (isc_log_wouldlog(ISC_LOG_DEBUG(3))) { |
20490 | 0 | unsigned char salttext[255 * 2 + 1]; |
20491 | 0 | if (param.salt.base != NULL) { |
20492 | 0 | salt2text(param.salt.base, param.salt.length, |
20493 | 0 | salttext, sizeof(salttext)); |
20494 | 0 | } |
20495 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(3), |
20496 | 0 | "setnsec3param:nsec3 %u %u %u %u:%s", |
20497 | 0 | param.hash, param.flags, param.iterations, |
20498 | 0 | param.salt.length, |
20499 | 0 | param.salt.base == NULL ? "unknown" |
20500 | 0 | : (char *)salttext); |
20501 | 0 | } |
20502 | 0 | } |
20503 | | |
20504 | | /* |
20505 | | * Queue the request and let zone maintenance process it once the zone |
20506 | | * DB is loaded and no inline-signing transaction is active. |
20507 | | */ |
20508 | 0 | ISC_LIST_APPEND(zone->maintenance_queue, request, link); |
20509 | 0 | if (zone->loop != NULL && zone_maintenance_request_pending(zone)) { |
20510 | 0 | isc_time_t now = isc_time_now(); |
20511 | 0 | dns__zone_settimer(zone, now); |
20512 | 0 | } |
20513 | 0 | request = NULL; |
20514 | |
|
20515 | 0 | result = ISC_R_SUCCESS; |
20516 | |
|
20517 | 0 | cleanup: |
20518 | 0 | if (request != NULL) { |
20519 | 0 | isc_mem_put(zone->mctx, request, sizeof(*request)); |
20520 | 0 | } |
20521 | 0 | UNLOCK_ZONE(zone); |
20522 | 0 | return result; |
20523 | 0 | } |
20524 | | |
20525 | | unsigned int |
20526 | 0 | dns_zone_getincludes(dns_zone_t *zone, char ***includesp) { |
20527 | 0 | char **array = NULL; |
20528 | 0 | unsigned int n = 0; |
20529 | |
|
20530 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
20531 | 0 | REQUIRE(includesp != NULL && *includesp == NULL); |
20532 | |
|
20533 | 0 | LOCK_ZONE(zone); |
20534 | 0 | if (zone->nincludes == 0) { |
20535 | 0 | goto done; |
20536 | 0 | } |
20537 | | |
20538 | 0 | array = isc_mem_allocate(zone->mctx, sizeof(char *) * zone->nincludes); |
20539 | 0 | ISC_LIST_FOREACH(zone->includes, include, link) { |
20540 | 0 | INSIST(n < zone->nincludes); |
20541 | 0 | array[n++] = isc_mem_strdup(zone->mctx, include->name); |
20542 | 0 | } |
20543 | 0 | INSIST(n == zone->nincludes); |
20544 | 0 | *includesp = array; |
20545 | |
|
20546 | 0 | done: |
20547 | 0 | UNLOCK_ZONE(zone); |
20548 | 0 | return n; |
20549 | 0 | } |
20550 | | |
20551 | | static void |
20552 | | zone_process_setserial(dns_zone_t *zone, |
20553 | 0 | const zone_maintenance_request_t *request) { |
20554 | 0 | uint32_t oldserial, desired; |
20555 | 0 | bool commit = false; |
20556 | 0 | isc_result_t result; |
20557 | 0 | dns_dbversion_t *oldver = NULL, *newver = NULL; |
20558 | 0 | dns_db_t *db = NULL; |
20559 | 0 | dns_diff_t diff; |
20560 | 0 | dns_update_log_t log = { update_log_cb, NULL }; |
20561 | 0 | dns_difftuple_t *oldtuple = NULL, *newtuple = NULL; |
20562 | |
|
20563 | 0 | INSIST(DNS_ZONE_VALID(zone)); |
20564 | |
|
20565 | 0 | ENTER; |
20566 | |
|
20567 | 0 | if (zone->update_disabled) { |
20568 | 0 | goto disabled; |
20569 | 0 | } |
20570 | | |
20571 | 0 | desired = request->u.setserial.serial; |
20572 | |
|
20573 | 0 | dns_diff_init(zone->mctx, &diff); |
20574 | |
|
20575 | 0 | ZONEDB_LOCK(&zone->dblock, isc_rwlocktype_read); |
20576 | 0 | if (zone->db != NULL) { |
20577 | 0 | dns_db_attach(zone->db, &db); |
20578 | 0 | } |
20579 | 0 | ZONEDB_UNLOCK(&zone->dblock, isc_rwlocktype_read); |
20580 | 0 | if (db == NULL) { |
20581 | 0 | goto cleanup; |
20582 | 0 | } |
20583 | | |
20584 | 0 | dns_db_currentversion(db, &oldver); |
20585 | 0 | result = dns_db_newversion(db, &newver); |
20586 | 0 | if (result != ISC_R_SUCCESS) { |
20587 | 0 | dns_zone_log(zone, ISC_LOG_ERROR, |
20588 | 0 | "setserial:dns_db_newversion -> %s", |
20589 | 0 | isc_result_totext(result)); |
20590 | 0 | goto cleanup; |
20591 | 0 | } |
20592 | | |
20593 | 0 | CHECK(dns_db_createsoatuple(db, oldver, diff.mctx, DNS_DIFFOP_DEL, |
20594 | 0 | &oldtuple)); |
20595 | 0 | dns_difftuple_copy(oldtuple, &newtuple); |
20596 | 0 | newtuple->op = DNS_DIFFOP_ADD; |
20597 | |
|
20598 | 0 | oldserial = dns_soa_getserial(&oldtuple->rdata); |
20599 | 0 | if (desired == 0U) { |
20600 | 0 | desired = 1; |
20601 | 0 | } |
20602 | 0 | if (!isc_serial_gt(desired, oldserial)) { |
20603 | 0 | if (desired != oldserial) { |
20604 | 0 | dns_zone_log(zone, ISC_LOG_INFO, |
20605 | 0 | "setserial: desired serial (%u) " |
20606 | 0 | "out of range (%u-%u)", |
20607 | 0 | desired, oldserial + 1, |
20608 | 0 | oldserial + 0x7fffffff); |
20609 | 0 | } |
20610 | 0 | goto cleanup; |
20611 | 0 | } |
20612 | | |
20613 | 0 | dns_soa_setserial(desired, &newtuple->rdata); |
20614 | 0 | CHECK(do_one_tuple(&oldtuple, db, newver, &diff)); |
20615 | 0 | CHECK(do_one_tuple(&newtuple, db, newver, &diff)); |
20616 | 0 | result = dns_update_signatures(&log, zone, db, oldver, newver, &diff, |
20617 | 0 | zone->sigvalidityinterval); |
20618 | 0 | if (result != ISC_R_NOTFOUND) { |
20619 | 0 | CHECK(result); |
20620 | 0 | } |
20621 | | |
20622 | | /* Write changes to journal file. */ |
20623 | 0 | CHECK(zone_journal(zone, &diff, NULL, "setserial")); |
20624 | 0 | commit = true; |
20625 | |
|
20626 | 0 | LOCK_ZONE(zone); |
20627 | 0 | zone_needdump(zone, 30); |
20628 | 0 | UNLOCK_ZONE(zone); |
20629 | |
|
20630 | 0 | cleanup: |
20631 | 0 | if (oldtuple != NULL) { |
20632 | 0 | dns_difftuple_free(&oldtuple); |
20633 | 0 | } |
20634 | 0 | if (newtuple != NULL) { |
20635 | 0 | dns_difftuple_free(&newtuple); |
20636 | 0 | } |
20637 | 0 | if (oldver != NULL) { |
20638 | 0 | dns_db_closeversion(db, &oldver, false); |
20639 | 0 | } |
20640 | 0 | if (newver != NULL) { |
20641 | 0 | dns_db_closeversion(db, &newver, commit); |
20642 | 0 | } |
20643 | 0 | if (db != NULL) { |
20644 | 0 | dns_db_detach(&db); |
20645 | 0 | } |
20646 | 0 | dns_diff_clear(&diff); |
20647 | |
|
20648 | 0 | disabled: |
20649 | 0 | INSIST(oldver == NULL); |
20650 | 0 | INSIST(newver == NULL); |
20651 | 0 | } |
20652 | | |
20653 | | isc_result_t |
20654 | 0 | dns_zone_setserial(dns_zone_t *zone, uint32_t serial) { |
20655 | 0 | isc_result_t result = ISC_R_SUCCESS; |
20656 | 0 | zone_maintenance_request_t *request = NULL; |
20657 | |
|
20658 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
20659 | |
|
20660 | 0 | LOCK_ZONE(zone); |
20661 | |
|
20662 | 0 | if (!dns__zone_inline_secure(zone)) { |
20663 | 0 | if (!dns_zone_isdynamic(zone, true)) { |
20664 | 0 | CLEANUP(DNS_R_NOTDYNAMIC); |
20665 | 0 | } |
20666 | 0 | } |
20667 | | |
20668 | 0 | if (zone->update_disabled) { |
20669 | 0 | CLEANUP(DNS_R_FROZEN); |
20670 | 0 | } |
20671 | | |
20672 | 0 | request = isc_mem_get(zone->mctx, sizeof(*request)); |
20673 | 0 | *request = (zone_maintenance_request_t){ |
20674 | 0 | .link = ISC_LINK_INITIALIZER, |
20675 | 0 | .type = zone_maintenance_request_setserial, |
20676 | 0 | .u.setserial = { |
20677 | 0 | .serial = serial, |
20678 | 0 | }, |
20679 | 0 | }; |
20680 | 0 | ISC_LIST_APPEND(zone->maintenance_queue, request, link); |
20681 | 0 | if (zone->loop != NULL && zone_maintenance_request_pending(zone)) { |
20682 | 0 | isc_time_t now = isc_time_now(); |
20683 | 0 | dns__zone_settimer(zone, now); |
20684 | 0 | } |
20685 | 0 | request = NULL; |
20686 | |
|
20687 | 0 | cleanup: |
20688 | 0 | if (request != NULL) { |
20689 | 0 | isc_mem_put(zone->mctx, request, sizeof(*request)); |
20690 | 0 | } |
20691 | 0 | UNLOCK_ZONE(zone); |
20692 | 0 | return result; |
20693 | 0 | } |
20694 | | |
20695 | | static void |
20696 | 0 | zone_process_maintenance_request(dns_zone_t *zone) { |
20697 | 0 | zone_maintenance_request_t *request = NULL; |
20698 | |
|
20699 | 0 | LOCK_ZONE(zone); |
20700 | 0 | if (zone_maintenance_request_pending(zone)) { |
20701 | 0 | request = ISC_LIST_HEAD(zone->maintenance_queue); |
20702 | 0 | ISC_LIST_UNLINK(zone->maintenance_queue, request, link); |
20703 | 0 | } |
20704 | 0 | UNLOCK_ZONE(zone); |
20705 | |
|
20706 | 0 | if (request == NULL) { |
20707 | 0 | return; |
20708 | 0 | } |
20709 | | |
20710 | 0 | switch (request->type) { |
20711 | 0 | case zone_maintenance_request_setnsec3param: |
20712 | 0 | rss_post(zone, &request->u.nsec3param); |
20713 | 0 | break; |
20714 | 0 | case zone_maintenance_request_keydone: |
20715 | 0 | zone_process_keydone(zone, request); |
20716 | 0 | break; |
20717 | 0 | case zone_maintenance_request_setserial: |
20718 | 0 | zone_process_setserial(zone, request); |
20719 | 0 | break; |
20720 | 0 | default: |
20721 | 0 | UNREACHABLE(); |
20722 | 0 | } |
20723 | | |
20724 | 0 | isc_mem_put(zone->mctx, request, sizeof(*request)); |
20725 | 0 | } |
20726 | | |
20727 | | bool |
20728 | 0 | dns_zone_isloaded(dns_zone_t *zone) { |
20729 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
20730 | |
|
20731 | 0 | return DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADED); |
20732 | 0 | } |
20733 | | |
20734 | | isc_result_t |
20735 | 2 | dns_zone_verifydb(dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *ver) { |
20736 | 2 | dns_dbversion_t *version = NULL; |
20737 | 2 | dns_keytable_t *secroots = NULL; |
20738 | 2 | isc_result_t result; |
20739 | 2 | dns_name_t *origin; |
20740 | | |
20741 | 2 | REQUIRE(DNS_ZONE_VALID(zone)); |
20742 | 2 | REQUIRE(db != NULL); |
20743 | | |
20744 | 2 | ENTER; |
20745 | | |
20746 | 2 | if (dns_zone_gettype(zone) != dns_zone_mirror) { |
20747 | 2 | return ISC_R_SUCCESS; |
20748 | 2 | } |
20749 | | |
20750 | 0 | if (ver == NULL) { |
20751 | 0 | dns_db_currentversion(db, &version); |
20752 | 0 | } else { |
20753 | 0 | version = ver; |
20754 | 0 | } |
20755 | |
|
20756 | 0 | if (zone->view != NULL) { |
20757 | 0 | result = dns_view_getsecroots(zone->view, &secroots); |
20758 | 0 | CHECK(result); |
20759 | 0 | } |
20760 | | |
20761 | 0 | origin = dns_db_origin(db); |
20762 | 0 | result = dns_zoneverify_dnssec(zone, db, version, origin, secroots, |
20763 | 0 | zone->mctx, true, false, dnssec_report); |
20764 | |
|
20765 | 0 | cleanup: |
20766 | 0 | if (secroots != NULL) { |
20767 | 0 | dns_keytable_detach(&secroots); |
20768 | 0 | } |
20769 | |
|
20770 | 0 | if (ver == NULL) { |
20771 | 0 | dns_db_closeversion(db, &version, false); |
20772 | 0 | } |
20773 | |
|
20774 | 0 | if (result != ISC_R_SUCCESS) { |
20775 | 0 | dnssec_log(zone, ISC_LOG_ERROR, "zone verification failed: %s", |
20776 | 0 | isc_result_totext(result)); |
20777 | 0 | result = DNS_R_VERIFYFAILURE; |
20778 | 0 | } |
20779 | |
|
20780 | 0 | return result; |
20781 | 0 | } |
20782 | | |
20783 | | static dns_ttl_t |
20784 | 0 | zone_nsecttl(dns_zone_t *zone) { |
20785 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
20786 | |
|
20787 | 0 | return ISC_MIN(zone->minimum, zone->soattl); |
20788 | 0 | } |
20789 | | |
20790 | | void |
20791 | | dns_zonemgr_set_tlsctx_cache(dns_zonemgr_t *zmgr, |
20792 | 0 | isc_tlsctx_cache_t *tlsctx_cache) { |
20793 | 0 | REQUIRE(DNS_ZONEMGR_VALID(zmgr)); |
20794 | 0 | REQUIRE(tlsctx_cache != NULL); |
20795 | |
|
20796 | 0 | RWLOCK(&zmgr->tlsctx_cache_rwlock, isc_rwlocktype_write); |
20797 | |
|
20798 | 0 | if (zmgr->tlsctx_cache != NULL) { |
20799 | 0 | isc_tlsctx_cache_detach(&zmgr->tlsctx_cache); |
20800 | 0 | } |
20801 | |
|
20802 | 0 | isc_tlsctx_cache_attach(tlsctx_cache, &zmgr->tlsctx_cache); |
20803 | |
|
20804 | 0 | RWUNLOCK(&zmgr->tlsctx_cache_rwlock, isc_rwlocktype_write); |
20805 | 0 | } |
20806 | | |
20807 | | void |
20808 | | dns__zonemgr_tlsctx_attach(dns_zonemgr_t *zmgr, |
20809 | 0 | isc_tlsctx_cache_t **ptlsctx_cache) { |
20810 | 0 | REQUIRE(DNS_ZONEMGR_VALID(zmgr)); |
20811 | 0 | REQUIRE(ptlsctx_cache != NULL && *ptlsctx_cache == NULL); |
20812 | |
|
20813 | 0 | RWLOCK(&zmgr->tlsctx_cache_rwlock, isc_rwlocktype_read); |
20814 | |
|
20815 | 0 | INSIST(zmgr->tlsctx_cache != NULL); |
20816 | 0 | isc_tlsctx_cache_attach(zmgr->tlsctx_cache, ptlsctx_cache); |
20817 | |
|
20818 | 0 | RWUNLOCK(&zmgr->tlsctx_cache_rwlock, isc_rwlocktype_read); |
20819 | 0 | } |
20820 | | |
20821 | | isc_result_t |
20822 | 2 | dns_zone_makedb(dns_zone_t *zone, dns_db_t **dbp) { |
20823 | 2 | REQUIRE(DNS_ZONE_VALID(zone)); |
20824 | 2 | REQUIRE(dbp != NULL && *dbp == NULL); |
20825 | | |
20826 | 2 | dns_db_t *db = NULL; |
20827 | | |
20828 | 2 | isc_result_t result = dns_db_create( |
20829 | 2 | zone->mctx, zone->db_argv[0], &zone->origin, |
20830 | 2 | (zone->type == dns_zone_stub) ? dns_dbtype_stub |
20831 | 2 | : dns_dbtype_zone, |
20832 | 2 | zone->rdclass, zone->db_argc - 1, zone->db_argv + 1, &db); |
20833 | 2 | if (result != ISC_R_SUCCESS) { |
20834 | 0 | return result; |
20835 | 0 | } |
20836 | | |
20837 | 2 | switch (zone->type) { |
20838 | 2 | case dns_zone_primary: |
20839 | 2 | case dns_zone_secondary: |
20840 | 2 | case dns_zone_mirror: |
20841 | 2 | result = dns_db_setgluecachestats(db, zone->gluecachestats); |
20842 | 2 | if (result == ISC_R_NOTIMPLEMENTED) { |
20843 | 0 | result = ISC_R_SUCCESS; |
20844 | 0 | } |
20845 | 2 | if (result != ISC_R_SUCCESS) { |
20846 | 0 | dns_db_detach(&db); |
20847 | 0 | return result; |
20848 | 0 | } |
20849 | 2 | break; |
20850 | 2 | default: |
20851 | 0 | break; |
20852 | 2 | } |
20853 | | |
20854 | 2 | dns_db_setmaxrrperset(db, zone->maxrrperset); |
20855 | 2 | dns_db_setmaxtypepername(db, zone->maxtypepername); |
20856 | | |
20857 | 2 | *dbp = db; |
20858 | | |
20859 | 2 | return ISC_R_SUCCESS; |
20860 | 2 | } |
20861 | | |
20862 | | isc_result_t |
20863 | 0 | dns_zone_import_skr(dns_zone_t *zone, const char *file) { |
20864 | 0 | dns_skr_t *skr = NULL; |
20865 | 0 | isc_result_t result; |
20866 | |
|
20867 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
20868 | 0 | REQUIRE(zone->kasp != NULL); |
20869 | 0 | REQUIRE(file != NULL); |
20870 | |
|
20871 | 0 | dns_skr_create(zone->mctx, file, &zone->origin, zone->rdclass, &skr); |
20872 | |
|
20873 | 0 | CHECK(dns_skr_read(zone->mctx, file, &zone->origin, zone->rdclass, |
20874 | 0 | dns_kasp_dnskeyttl(zone->kasp), &skr)); |
20875 | |
|
20876 | 0 | dns_zone_setskr(zone, skr); |
20877 | 0 | dnssec_log(zone, ISC_LOG_DEBUG(1), "imported skr file %s", file); |
20878 | |
|
20879 | 0 | cleanup: |
20880 | 0 | dns_skr_detach(&skr); |
20881 | |
|
20882 | 0 | return result; |
20883 | 0 | } |
20884 | | |
20885 | | void |
20886 | | dns_zone_setplugins(dns_zone_t *zone, void *plugins, |
20887 | 0 | void (*plugins_free)(isc_mem_t *, void **)) { |
20888 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
20889 | 0 | REQUIRE(zone->plugins == NULL); |
20890 | 0 | REQUIRE(zone->plugins_free == NULL); |
20891 | |
|
20892 | 0 | zone->plugins = plugins; |
20893 | 0 | zone->plugins_free = plugins_free; |
20894 | 0 | } |
20895 | | |
20896 | | void |
20897 | 0 | dns_zone_unloadplugins(dns_zone_t *zone) { |
20898 | 0 | if (zone->hooktable != NULL) { |
20899 | 0 | INSIST(zone->hooktable_free); |
20900 | 0 | zone->hooktable_free(zone->mctx, &zone->hooktable); |
20901 | 0 | INSIST(zone->hooktable == NULL); |
20902 | 0 | zone->hooktable_free = NULL; |
20903 | 0 | } |
20904 | |
|
20905 | 0 | if (zone->plugins != NULL) { |
20906 | 0 | INSIST(zone->plugins_free); |
20907 | 0 | zone->plugins_free(zone->mctx, &zone->plugins); |
20908 | 0 | INSIST(zone->plugins == NULL); |
20909 | 0 | zone->plugins_free = NULL; |
20910 | 0 | } |
20911 | 0 | } |
20912 | | |
20913 | | bool |
20914 | 0 | dns_zone_isexpired(dns_zone_t *zone) { |
20915 | 0 | REQUIRE(DNS_ZONE_VALID(zone)); |
20916 | |
|
20917 | | return DNS_ZONE_FLAG(zone, DNS_ZONEFLG_EXPIRED); |
20918 | 0 | } |