zone.c 502 KB
Newer Older
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3
 *
4
5
6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8
9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
10
11
 */

12
/*! \file */
13
14

#include <config.h>
Mark Andrews's avatar
Mark Andrews committed
15
#include <errno.h>
16

17
#include <isc/file.h>
18
#include <isc/hex.h>
19
#include <isc/mutex.h>
20
#include <isc/pool.h>
Mark Andrews's avatar
Mark Andrews committed
21
#include <isc/print.h>
22
#include <isc/random.h>
Mark Andrews's avatar
Mark Andrews committed
23
#include <isc/ratelimiter.h>
24
#include <isc/refcount.h>
25
#include <isc/rwlock.h>
26
#include <isc/serial.h>
27
#include <isc/stats.h>
28
#include <isc/stdtime.h>
29
#include <isc/strerror.h>
30
#include <isc/string.h>
31
#include <isc/taskpool.h>
32
#include <isc/thread.h>
33
#include <isc/timer.h>
Michael Graff's avatar
Michael Graff committed
34
#include <isc/util.h>
35

36
#include <dns/acl.h>
Mark Andrews's avatar
Mark Andrews committed
37
#include <dns/adb.h>
38
#include <dns/callbacks.h>
39
#include <dns/catz.h>
40
#include <dns/db.h>
41
#include <dns/dbiterator.h>
Evan Hunt's avatar
Evan Hunt committed
42
#include <dns/dlz.h>
43
#include <dns/dnssec.h>
44
#include <dns/events.h>
45
#include <dns/journal.h>
46
47
#include <dns/keydata.h>
#include <dns/keytable.h>
48
#include <dns/keyvalues.h>
49
#include <dns/log.h>
50
#include <dns/master.h>
Mark Andrews's avatar
Mark Andrews committed
51
#include <dns/masterdump.h>
52
#include <dns/message.h>
53
#include <dns/name.h>
54
#include <dns/nsec.h>
55
#include <dns/nsec3.h>
56
#include <dns/peer.h>
57
#include <dns/private.h>
58
#include <dns/rcode.h>
59
#include <dns/rdata.h>
60
#include <dns/rdataclass.h>
61
#include <dns/rdatalist.h>
62
#include <dns/rdataset.h>
63
#include <dns/rdatasetiter.h>
Brian Wellington's avatar
Brian Wellington committed
64
#include <dns/rdatastruct.h>
65
#include <dns/rdatatype.h>
Mark Andrews's avatar
Mark Andrews committed
66
#include <dns/request.h>
67
68
#include <dns/resolver.h>
#include <dns/result.h>
69
#include <dns/rriterator.h>
70
#include <dns/soa.h>
71
#include <dns/ssu.h>
72
#include <dns/stats.h>
73
#include <dns/time.h>
74
#include <dns/tsig.h>
75
#include <dns/update.h>
76
#include <dns/xfrin.h>
77
#include <dns/zone.h>
78
#include <dns/zt.h>
79

80
81
#include <dst/dst.h>

82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#define ZONE_MAGIC			ISC_MAGIC('Z', 'O', 'N', 'E')
#define DNS_ZONE_VALID(zone)		ISC_MAGIC_VALID(zone, ZONE_MAGIC)

#define NOTIFY_MAGIC			ISC_MAGIC('N', 't', 'f', 'y')
#define DNS_NOTIFY_VALID(notify)	ISC_MAGIC_VALID(notify, NOTIFY_MAGIC)

#define STUB_MAGIC			ISC_MAGIC('S', 't', 'u', 'b')
#define DNS_STUB_VALID(stub)		ISC_MAGIC_VALID(stub, STUB_MAGIC)

#define ZONEMGR_MAGIC			ISC_MAGIC('Z', 'm', 'g', 'r')
#define DNS_ZONEMGR_VALID(stub)		ISC_MAGIC_VALID(stub, ZONEMGR_MAGIC)

#define LOAD_MAGIC			ISC_MAGIC('L', 'o', 'a', 'd')
#define DNS_LOAD_VALID(load)		ISC_MAGIC_VALID(load, LOAD_MAGIC)

#define FORWARD_MAGIC			ISC_MAGIC('F', 'o', 'r', 'w')
#define DNS_FORWARD_VALID(load)		ISC_MAGIC_VALID(load, FORWARD_MAGIC)

#define IO_MAGIC			ISC_MAGIC('Z', 'm', 'I', 'O')
#define DNS_IO_VALID(load)		ISC_MAGIC_VALID(load, IO_MAGIC)

103
/*%
104
105
106
107
 * Ensure 'a' is at least 'min' but not more than 'max'.
 */
#define RANGE(a, min, max) \
		(((a) < (min)) ? (min) : ((a) < (max) ? (a) : (max)))
108

109
110
#define NSEC3REMOVE(x) (((x) & DNS_NSEC3FLAG_REMOVE) != 0)

Scott Mann's avatar
Scott Mann committed
111
112
113
114
115
116
117
/*%
 * Key flags
 */
#define REVOKE(x) ((dst_key_flags(x) & DNS_KEYFLAG_REVOKE) != 0)
#define KSK(x) ((dst_key_flags(x) & DNS_KEYFLAG_KSK) != 0)
#define ALG(x) dst_key_alg(x)

118
119
120
/*
 * Default values.
 */
121
122
123
#define DNS_DEFAULT_IDLEIN 3600		/*%< 1 hour */
#define DNS_DEFAULT_IDLEOUT 3600	/*%< 1 hour */
#define MAX_XFER_TIME (2*3600)		/*%< Documented default is 2 hours */
Scott Mann's avatar
Scott Mann committed
124
#define RESIGN_DELAY 3600		/*%< 1 hour */
Mark Andrews's avatar
Mark Andrews committed
125

126
#ifndef DNS_MAX_EXPIRE
127
#define DNS_MAX_EXPIRE	14515200	/*%< 24 weeks */
128
#endif
Mark Andrews's avatar
Mark Andrews committed
129

130
#ifndef DNS_DUMP_DELAY
131
#define DNS_DUMP_DELAY 900		/*%< 15 minutes */
132
133
#endif

134
135
typedef struct dns_notify dns_notify_t;
typedef struct dns_stub dns_stub_t;
136
typedef struct dns_load dns_load_t;
137
typedef struct dns_forward dns_forward_t;
138
typedef ISC_LIST(dns_forward_t) dns_forwardlist_t;
139
140
typedef struct dns_io dns_io_t;
typedef ISC_LIST(dns_io_t) dns_iolist_t;
141
142
typedef struct dns_signing dns_signing_t;
typedef ISC_LIST(dns_signing_t) dns_signinglist_t;
143
144
typedef struct dns_nsec3chain dns_nsec3chain_t;
typedef ISC_LIST(dns_nsec3chain_t) dns_nsec3chainlist_t;
145
typedef struct dns_keyfetch dns_keyfetch_t;
146
typedef struct dns_asyncload dns_asyncload_t;
147
typedef struct dns_include dns_include_t;
148

149
150
151
#define DNS_ZONE_CHECKLOCK
#ifdef DNS_ZONE_CHECKLOCK
#define LOCK_ZONE(z) \
Mark Andrews's avatar
Mark Andrews committed
152
153
154
155
	 do { LOCK(&(z)->lock); \
	      INSIST((z)->locked == ISC_FALSE); \
	     (z)->locked = ISC_TRUE; \
		} while (0)
156
157
#define UNLOCK_ZONE(z) \
	do { (z)->locked = ISC_FALSE; UNLOCK(&(z)->lock); } while (0)
Mark Andrews's avatar
Mark Andrews committed
158
#define LOCKED_ZONE(z) ((z)->locked)
159
160
#define TRYLOCK_ZONE(result, z) \
	do { \
161
	      result = isc_mutex_trylock(&(z)->lock); \
162
163
164
165
166
	      if (result == ISC_R_SUCCESS) {  \
		     INSIST((z)->locked == ISC_FALSE); \
		     (z)->locked = ISC_TRUE; \
	      } \
	} while (0)
167
168
169
170
#else
#define LOCK_ZONE(z) LOCK(&(z)->lock)
#define UNLOCK_ZONE(z) UNLOCK(&(z)->lock)
#define LOCKED_ZONE(z) ISC_TRUE
171
#define TRYLOCK_ZONE(result, z) \
172
	do { result = isc_mutex_trylock(&(z)->lock); } while (0)
173
174
#endif

175
176
177
178
179
180
181
182
183
184
185
186
#ifdef ISC_RWLOCK_USEATOMIC
#define ZONEDB_INITLOCK(l)	isc_rwlock_init((l), 0, 0)
#define ZONEDB_DESTROYLOCK(l)	isc_rwlock_destroy(l)
#define ZONEDB_LOCK(l, t)	RWLOCK((l), (t))
#define ZONEDB_UNLOCK(l, t)	RWUNLOCK((l), (t))
#else
#define ZONEDB_INITLOCK(l)	isc_mutex_init(l)
#define ZONEDB_DESTROYLOCK(l)	DESTROYLOCK(l)
#define ZONEDB_LOCK(l, t)	LOCK(l)
#define ZONEDB_UNLOCK(l, t)	UNLOCK(l)
#endif

187
188
189
190
struct dns_zone {
	/* Unlocked */
	unsigned int		magic;
	isc_mutex_t		lock;
191
192
193
#ifdef DNS_ZONE_CHECKLOCK
	isc_boolean_t		locked;
#endif
194
	isc_mem_t		*mctx;
195
	isc_refcount_t		erefs;
196

197
198
199
200
201
202
203
#ifdef ISC_RWLOCK_USEATOMIC
	isc_rwlock_t		dblock;
#else
	isc_mutex_t		dblock;
#endif
	dns_db_t		*db;		/* Locked by dblock */

204
	/* Locked */
205
206
	dns_zonemgr_t		*zmgr;
	ISC_LINK(dns_zone_t)	link;		/* Used by zmgr. */
207
	isc_timer_t		*timer;
208
	unsigned int		irefs;
209
	dns_name_t		origin;
Mark Andrews's avatar
Mark Andrews committed
210
	char			*masterfile;
211
	ISC_LIST(dns_include_t)	includes;	/* Include files */
212
	ISC_LIST(dns_include_t)	newincludes;	/* Loading */
213
	unsigned int		nincludes;
214
	dns_masterformat_t	masterformat;
Evan Hunt's avatar
Evan Hunt committed
215
	const dns_master_style_t *masterstyle;
216
	char			*journal;
217
	isc_int32_t		journalsize;
218
219
220
221
	dns_rdataclass_t	rdclass;
	dns_zonetype_t		type;
	unsigned int		flags;
	unsigned int		options;
Evan Hunt's avatar
Evan Hunt committed
222
	unsigned int		options2;
223
	unsigned int		db_argc;
224
	char			**db_argv;
225
226
227
	isc_time_t		expiretime;
	isc_time_t		refreshtime;
	isc_time_t		dumptime;
228
	isc_time_t		loadtime;
229
	isc_time_t		notifytime;
230
231
232
	isc_time_t		resigntime;
	isc_time_t		keywarntime;
	isc_time_t		signingtime;
233
	isc_time_t		nsec3chaintime;
234
	isc_time_t		refreshkeytime;
235
	isc_uint32_t		refreshkeyinterval;
236
	isc_uint32_t		refreshkeycount;
237
238
239
240
	isc_uint32_t		refresh;
	isc_uint32_t		retry;
	isc_uint32_t		expire;
	isc_uint32_t		minimum;
241
	isc_stdtime_t		key_expiry;
Scott Mann's avatar
Scott Mann committed
242
	isc_stdtime_t		log_key_expired_timer;
243
	char			*keydirectory;
244
245
246
247
248
249

	isc_uint32_t		maxrefresh;
	isc_uint32_t		minrefresh;
	isc_uint32_t		maxretry;
	isc_uint32_t		minretry;

250
251
	isc_uint32_t		maxrecords;

252
	isc_sockaddr_t		*masters;
Evan Hunt's avatar
Evan Hunt committed
253
	isc_dscp_t		*masterdscps;
David Lawrence's avatar
tabify    
David Lawrence committed
254
	dns_name_t		**masterkeynames;
255
	isc_boolean_t		*mastersok;
256
257
	unsigned int		masterscnt;
	unsigned int		curmaster;
Mark Andrews's avatar
Mark Andrews committed
258
	isc_sockaddr_t		masteraddr;
259
	dns_notifytype_t	notifytype;
260
	isc_sockaddr_t		*notify;
261
	dns_name_t		**notifykeynames;
Tinderbox User's avatar
Tinderbox User committed
262
	isc_dscp_t		*notifydscp;
263
264
	unsigned int		notifycnt;
	isc_sockaddr_t		notifyfrom;
265
	isc_task_t		*task;
266
	isc_task_t		*loadtask;
267
268
269
270
271
272
273
	isc_sockaddr_t		notifysrc4;
	isc_sockaddr_t		notifysrc6;
	isc_sockaddr_t		xfrsource4;
	isc_sockaddr_t		xfrsource6;
	isc_sockaddr_t		altxfrsource4;
	isc_sockaddr_t		altxfrsource6;
	isc_sockaddr_t		sourceaddr;
Evan Hunt's avatar
Evan Hunt committed
274
275
276
277
278
279
	isc_dscp_t		notifysrc4dscp;
	isc_dscp_t		notifysrc6dscp;
	isc_dscp_t		xfrsource4dscp;
	isc_dscp_t		xfrsource6dscp;
	isc_dscp_t		altxfrsource4dscp;
	isc_dscp_t		altxfrsource6dscp;
Mark Andrews's avatar
Mark Andrews committed
280
	dns_xfrin_ctx_t		*xfr;		/* task locked */
281
	dns_tsigkey_t		*tsigkey;	/* key used for xfr */
282
	/* Access Control Lists */
283
	dns_acl_t		*update_acl;
284
	dns_acl_t		*forward_acl;
285
	dns_acl_t		*notify_acl;
286
	dns_acl_t		*query_acl;
287
	dns_acl_t		*queryon_acl;
288
	dns_acl_t		*xfr_acl;
289
	isc_boolean_t		update_disabled;
290
	isc_boolean_t		zero_no_soa_ttl;
291
	dns_severity_t		check_names;
292
	ISC_LIST(dns_notify_t)	notifies;
Mark Andrews's avatar
Mark Andrews committed
293
	dns_request_t		*request;
294
	dns_loadctx_t		*lctx;
295
	dns_io_t		*readio;
296
297
	dns_dumpctx_t		*dctx;
	dns_io_t		*writeio;
Mark Andrews's avatar
Mark Andrews committed
298
299
	isc_uint32_t		maxxfrin;
	isc_uint32_t		maxxfrout;
300
301
	isc_uint32_t		idlein;
	isc_uint32_t		idleout;
302
	isc_event_t		ctlevent;
303
	dns_ssutable_t		*ssutable;
304
	isc_uint32_t		sigvalidityinterval;
305
	isc_uint32_t		sigresigninginterval;
306
	dns_view_t		*view;
307
	dns_view_t		*prev_view;
308
309
310
	dns_checkmxfunc_t	checkmx;
	dns_checksrvfunc_t	checksrv;
	dns_checknsfunc_t	checkns;
311
	/*%
312
	 * Zones in certain states such as "waiting for zone transfer"
313
314
315
316
317
	 * or "zone transfer in progress" are kept on per-state linked lists
	 * in the zone manager using the 'statelink' field.  The 'statelist'
	 * field points at the list the zone is currently on.  It the zone
	 * is not on any such list, statelist is NULL.
	 */
318
	ISC_LINK(dns_zone_t)	statelink;
David Lawrence's avatar
tabify    
David Lawrence committed
319
	dns_zonelist_t		*statelist;
320
	/*%
321
	 * Statistics counters about zone management.
322
	 */
323
	isc_stats_t		*stats;
324
325
326
327
	/*%
	 * Optional per-zone statistics counters.  Counted outside of this
	 * module.
	 */
328
	dns_zonestat_level_t	statlevel;
329
330
	isc_boolean_t		requeststats_on;
	isc_stats_t		*requeststats;
331
	dns_stats_t		*rcvquerystats;
332
	isc_uint32_t		notifydelay;
333
334
	dns_isselffunc_t	isself;
	void			*isselfarg;
335
336
337
338
339

	char *			strnamerd;
	char *			strname;
	char *			strrdclass;
	char *			strviewname;
340
341
342
343
344

	/*%
	 * Serial number for deferred journal compaction.
	 */
	isc_uint32_t		compact_serial;
345
346
347
	/*%
	 * Keys that are signing the zone for the first time.
	 */
348
349
	dns_signinglist_t	signing;
	dns_nsec3chainlist_t	nsec3chain;
350
351
352
353
354
355
	/*%
	 * Signing / re-signing quantum stopping parameters.
	 */
	isc_uint32_t		signatures;
	isc_uint32_t		nodes;
	dns_rdatatype_t		privatetype;
356
357
358
359
360

	/*%
	 * Autosigning/key-maintenance options
	 */
	isc_uint32_t		keyopts;
361
362
363
364
365

	/*%
	 * True if added by "rndc addzone"
	 */
	isc_boolean_t           added;
366

367
368
369
370
371
	/*%
	 * True if added by automatically by named.
	 */
	isc_boolean_t           automatic;

372
	/*%
373
	 * response policy data to be relayed to the database
374
	 */
375
376
	dns_rpz_zones_t		*rpzs;
	dns_rpz_num_t		rpz_num;
377

378
379
380
	/*%
	 * catalog zone data
	 */
381
382
383
384
385
386
	dns_catz_zones_t	*catzs;

	/*%
	 * parent catalog zone
	 */
	dns_catz_zone_t		*parentcatz;
387

388
389
390
391
	/*%
	 * Serial number update method.
	 */
	dns_updatemethod_t	updatemethod;
392

393
394
395
396
397
	/*%
	 * whether ixfr is requested
	 */
	isc_boolean_t		requestixfr;

398
399
400
401
402
	/*%
	 * whether EDNS EXPIRE is requested
	 */
	isc_boolean_t		requestexpire;

403
404
405
406
407
	/*%
	 * Outstanding forwarded UPDATE requests.
	 */
	dns_forwardlist_t	forwards;

408
409
	dns_zone_t		*raw;
	dns_zone_t		*secure;
410

411
412
	isc_boolean_t		sourceserialset;
	isc_uint32_t		sourceserial;
Tinderbox User's avatar
Tinderbox User committed
413

Evan Hunt's avatar
Evan Hunt committed
414
415
416
417
	/*%
	 * maximum zone ttl
	 */
	dns_ttl_t		maxttl;
Tinderbox User's avatar
Tinderbox User committed
418

419
420
421
422
423
424
425
426
427
428
429
	/*
	 * Inline zone signing state.
	 */
	dns_diff_t		rss_diff;
	isc_eventlist_t		rss_events;
	dns_dbversion_t		*rss_newver;
	dns_dbversion_t		*rss_oldver;
	dns_db_t		*rss_db;
	dns_zone_t		*rss_raw;
	isc_event_t		*rss_event;
	dns_update_state_t      *rss_state;
430
431

	isc_stats_t             *gluecachestats;
432
433
};

434
435
436
437
438
439
440
441
442
443
444
445
typedef struct {
	dns_diff_t	*diff;
	isc_boolean_t	offline;
} zonediff_t;

#define zonediff_init(z, d) \
	do { \
		zonediff_t *_z = (z); \
		(_z)->diff = (d); \
		(_z)->offline = ISC_FALSE; \
	} while (0)

446
#define DNS_ZONE_FLAG(z,f) (ISC_TF(((z)->flags & (f)) != 0))
447
448
449
450
451
452
453
454
#define DNS_ZONE_SETFLAG(z,f) do { \
		INSIST(LOCKED_ZONE(z)); \
		(z)->flags |= (f); \
		} while (0)
#define DNS_ZONE_CLRFLAG(z,f) do { \
		INSIST(LOCKED_ZONE(z)); \
		(z)->flags &= ~(f); \
		} while (0)
455
	/* XXX MPA these may need to go back into zone.h */
456
457
458
459
460
461
462
463
464
465
#define DNS_ZONEFLG_REFRESH	0x00000001U	/*%< refresh check in progress */
#define DNS_ZONEFLG_NEEDDUMP	0x00000002U	/*%< zone need consolidation */
#define DNS_ZONEFLG_USEVC	0x00000004U	/*%< use tcp for refresh query */
#define DNS_ZONEFLG_DUMPING	0x00000008U	/*%< a dump is in progress */
#define DNS_ZONEFLG_HASINCLUDE	0x00000010U	/*%< $INCLUDE in zone file */
#define DNS_ZONEFLG_LOADED	0x00000020U	/*%< database has loaded */
#define DNS_ZONEFLG_EXITING	0x00000040U	/*%< zone is being destroyed */
#define DNS_ZONEFLG_EXPIRED	0x00000080U	/*%< zone has expired */
#define DNS_ZONEFLG_NEEDREFRESH	0x00000100U	/*%< refresh check needed */
#define DNS_ZONEFLG_UPTODATE	0x00000200U	/*%< zone contents are
466
						 * uptodate */
467
#define DNS_ZONEFLG_NEEDNOTIFY	0x00000400U	/*%< need to send out notify
Mark Andrews's avatar
Mark Andrews committed
468
						 * messages */
469
#define DNS_ZONEFLG_DIFFONRELOAD 0x00000800U	/*%< generate a journal diff on
470
						 * reload */
471
#define DNS_ZONEFLG_NOMASTERS	0x00001000U	/*%< an attempt to refresh a
472
						 * zone with no masters
Francis Dupont's avatar
Francis Dupont committed
473
						 * occurred */
474
475
#define DNS_ZONEFLG_LOADING	0x00002000U	/*%< load from disk in progress*/
#define DNS_ZONEFLG_HAVETIMERS	0x00004000U	/*%< timer values have been set
476
477
478
						 * from SOA (if not set, we
						 * are still using
						 * default timer values) */
479
#define DNS_ZONEFLG_FORCEXFER	0x00008000U	/*%< Force a zone xfer */
Mark Andrews's avatar
Mark Andrews committed
480
481
482
#define DNS_ZONEFLG_NOREFRESH	0x00010000U
#define DNS_ZONEFLG_DIALNOTIFY	0x00020000U
#define DNS_ZONEFLG_DIALREFRESH	0x00040000U
483
#define DNS_ZONEFLG_SHUTDOWN	0x00080000U
484
#define DNS_ZONEFLAG_NOIXFR	0x00100000U	/*%< IXFR failed, force AXFR */
485
#define DNS_ZONEFLG_FLUSH	0x00200000U
486
#define DNS_ZONEFLG_NOEDNS	0x00400000U
487
#define DNS_ZONEFLG_USEALTXFRSRC 0x00800000U
488
#define DNS_ZONEFLG_SOABEFOREAXFR 0x01000000U
489
#define DNS_ZONEFLG_NEEDCOMPACT 0x02000000U
490
491
#define DNS_ZONEFLG_REFRESHING	0x04000000U	/*%< Refreshing keydata */
#define DNS_ZONEFLG_THAW	0x08000000U
492
#define DNS_ZONEFLG_LOADPENDING	0x10000000U	/*%< Loading scheduled */
493
#define DNS_ZONEFLG_NODELAY	0x20000000U
494
#define DNS_ZONEFLG_SENDSECURE  0x40000000U
495
496
497
498
#define DNS_ZONEFLG_NEEDSTARTUPNOTIFY 0x80000000U /*%< need to send out notify
						   *   due to the zone just
						   *   being loaded for the
						   *   first time.  */
499

Mark Andrews's avatar
Mark Andrews committed
500
#define DNS_ZONE_OPTION(z,o) (((z)->options & (o)) != 0)
Evan Hunt's avatar
Evan Hunt committed
501
#define DNS_ZONE_OPTION2(z,o) (((z)->options2 & (o)) != 0)
502
#define DNS_ZONEKEY_OPTION(z,o) (((z)->keyopts & (o)) != 0)
503

504
505
/* Flags for zone_load() */
#define DNS_ZONELOADFLAG_NOSTAT	0x00000001U	/* Do not stat() master files */
506
507
#define DNS_ZONELOADFLAG_THAW	0x00000002U	/* Thaw the zone on successful
						   load. */
508

509
510
511
#define UNREACH_CHACHE_SIZE	10U
#define UNREACH_HOLD_TIME	600	/* 10 minutes */

512
513
514
515
516
#define CHECK(op) \
	do { result = (op); \
		if (result != ISC_R_SUCCESS) goto failure; \
	} while (0)

517
518
519
520
521
struct dns_unreachable {
	isc_sockaddr_t	remote;
	isc_sockaddr_t	local;
	isc_uint32_t	expire;
	isc_uint32_t	last;
522
	isc_uint32_t	count;
523
524
};

525
struct dns_zonemgr {
526
	unsigned int		magic;
527
	isc_mem_t *		mctx;
Mark Andrews's avatar
Mark Andrews committed
528
	int			refs;		/* Locked by rwlock */
529
530
	isc_taskmgr_t *		taskmgr;
	isc_timermgr_t *	timermgr;
531
	isc_socketmgr_t *	socketmgr;
532
	isc_taskpool_t *	zonetasks;
533
	isc_taskpool_t *	loadtasks;
534
	isc_task_t *		task;
535
	isc_pool_t *		mctxpool;
536
537
	isc_ratelimiter_t *	notifyrl;
	isc_ratelimiter_t *	refreshrl;
538
539
	isc_ratelimiter_t *	startupnotifyrl;
	isc_ratelimiter_t *	startuprefreshrl;
540
	isc_rwlock_t		rwlock;
541
	isc_mutex_t		iolock;
542
	isc_rwlock_t		urlock;
543

544
	/* Locked by rwlock. */
545
546
547
	dns_zonelist_t		zones;
	dns_zonelist_t		waiting_for_xfrin;
	dns_zonelist_t		xfrin_in_progress;
548

549
	/* Configuration data. */
550
551
	isc_uint32_t		transfersin;
	isc_uint32_t		transfersperns;
552
553
	unsigned int		notifyrate;
	unsigned int		startupnotifyrate;
554
	unsigned int		serialqueryrate;
555
	unsigned int		startupserialqueryrate;
556
557
558
559
560
561

	/* Locked by iolock */
	isc_uint32_t		iolimit;
	isc_uint32_t		ioactive;
	dns_iolist_t		high;
	dns_iolist_t		low;
Automatic Updater's avatar
Automatic Updater committed
562

563
	/* Locked by urlock. */
564
565
	/* LRU cache */
	struct dns_unreachable	unreachable[UNREACH_CHACHE_SIZE];
566
567
};

568
/*%
Mark Andrews's avatar
Mark Andrews committed
569
570
 * Hold notify state.
 */
571
struct dns_notify {
572
	unsigned int		magic;
Mark Andrews's avatar
Mark Andrews committed
573
	unsigned int		flags;
574
	isc_mem_t		*mctx;
Mark Andrews's avatar
Mark Andrews committed
575
576
577
578
	dns_zone_t		*zone;
	dns_adbfind_t		*find;
	dns_request_t		*request;
	dns_name_t		ns;
579
	isc_sockaddr_t		dst;
580
	dns_tsigkey_t		*key;
Tinderbox User's avatar
Tinderbox User committed
581
	isc_dscp_t		dscp;
582
	ISC_LINK(dns_notify_t)	link;
583
	isc_event_t		*event;
584
585
};

Mark Andrews's avatar
Mark Andrews committed
586
#define DNS_NOTIFY_NOSOA	0x0001U
587
#define DNS_NOTIFY_STARTUP	0x0002U
Mark Andrews's avatar
Mark Andrews committed
588

589
/*%
590
591
592
593
594
595
 *	dns_stub holds state while performing a 'stub' transfer.
 *	'db' is the zone's 'db' or a new one if this is the initial
 *	transfer.
 */

struct dns_stub {
596
	unsigned int		magic;
David Lawrence's avatar
tabify    
David Lawrence committed
597
	isc_mem_t		*mctx;
598
599
600
	dns_zone_t		*zone;
	dns_db_t		*db;
	dns_dbversion_t		*version;
Mark Andrews's avatar
Mark Andrews committed
601
602
};

603
/*%
604
605
606
 *	Hold load state.
 */
struct dns_load {
607
	unsigned int		magic;
608
609
610
611
612
613
614
	isc_mem_t		*mctx;
	dns_zone_t		*zone;
	dns_db_t		*db;
	isc_time_t		loadtime;
	dns_rdatacallbacks_t	callbacks;
};

615
/*%
616
617
618
 *	Hold forward state.
 */
struct dns_forward {
619
	unsigned int		magic;
620
621
622
623
624
625
626
	isc_mem_t		*mctx;
	dns_zone_t		*zone;
	isc_buffer_t		*msgbuf;
	dns_request_t		*request;
	isc_uint32_t		which;
	isc_sockaddr_t		addr;
	dns_updatecallback_t	callback;
Mark Andrews's avatar
Mark Andrews committed
627
	void			*callback_arg;
628
	unsigned int		options;
629
	ISC_LINK(dns_forward_t)	link;
630
631
};

632
/*%
633
634
635
 *	Hold IO request state.
 */
struct dns_io {
636
	unsigned int	magic;
637
638
639
640
641
642
643
	dns_zonemgr_t	*zmgr;
	isc_boolean_t	high;
	isc_task_t	*task;
	ISC_LINK(dns_io_t) link;
	isc_event_t	*event;
};

644
645
/*%
 *	Hold state for when we are signing a zone with a new
646
 *	DNSKEY as result of an update.
647
648
 */
struct dns_signing {
649
	unsigned int		magic;
650
651
652
653
	dns_db_t		*db;
	dns_dbiterator_t	*dbiterator;
	dns_secalg_t		algorithm;
	isc_uint16_t		keyid;
654
	isc_boolean_t		deleteit;
655
	isc_boolean_t		done;
656
657
658
	ISC_LINK(dns_signing_t)	link;
};

659
struct dns_nsec3chain {
660
	unsigned int			magic;
661
662
663
664
665
	dns_db_t			*db;
	dns_dbiterator_t		*dbiterator;
	dns_rdata_nsec3param_t		nsec3param;
	unsigned char			salt[255];
	isc_boolean_t			done;
666
667
668
	isc_boolean_t			seen_nsec;
	isc_boolean_t			delete_nsec;
	isc_boolean_t			save_delete_nsec;
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
	ISC_LINK(dns_nsec3chain_t)	link;
};
/*%<
 * 'dbiterator' contains a iterator for the database.  If we are creating
 * a NSEC3 chain only the non-NSEC3 nodes will be iterated.  If we are
 * removing a NSEC3 chain then both NSEC3 and non-NSEC3 nodes will be
 * iterated.
 *
 * 'nsec3param' contains the parameters of the NSEC3 chain being created
 * or removed.
 *
 * 'salt' is buffer space and is referenced via 'nsec3param.salt'.
 *
 * 'seen_nsec' will be set to true if, while iterating the zone to create a
 * NSEC3 chain, a NSEC record is seen.
 *
 * 'delete_nsec' will be set to true if, at the completion of the creation
 * of a NSEC3 chain, 'seen_nsec' is true.  If 'delete_nsec' is true then we
 * are in the process of deleting the NSEC chain.
 *
 * 'save_delete_nsec' is used to store the initial state of 'delete_nsec'
 * so it can be recovered in the event of a error.
 */

693
694
695
696
697
698
699
700
701
702
struct dns_keyfetch {
	dns_fixedname_t name;
	dns_rdataset_t keydataset;
	dns_rdataset_t dnskeyset;
	dns_rdataset_t dnskeysigset;
	dns_zone_t *zone;
	dns_db_t *db;
	dns_fetch_t *fetch;
};

703
704
705
706
707
708
709
710
711
/*%
 * Hold state for an asynchronous load
 */
struct dns_asyncload {
	dns_zone_t *zone;
	dns_zt_zoneloaded_t loaded;
	void *loaded_arg;
};

712
713
714
715
716
/*%
 * Reference to an include file encountered during loading
 */
struct dns_include {
	char *name;
Evan Hunt's avatar
Evan Hunt committed
717
	isc_time_t filetime;
718
719
720
	ISC_LINK(dns_include_t)	link;
};

Evan Hunt's avatar
Evan Hunt committed
721
722
723
724
725
/*
 * These can be overridden by the -T mkeytimers option on the command
 * line, so that we can test with shorter periods than specified in
 * RFC 5011.
 */
Evan Hunt's avatar
Evan Hunt committed
726
727
728
#define HOUR 3600
#define DAY (24*HOUR)
#define MONTH (30*DAY)
729
730
731
LIBDNS_EXTERNAL_DATA unsigned int dns_zone_mkey_hour = HOUR;
LIBDNS_EXTERNAL_DATA unsigned int dns_zone_mkey_day = DAY;
LIBDNS_EXTERNAL_DATA unsigned int dns_zone_mkey_month = MONTH;
Evan Hunt's avatar
Evan Hunt committed
732

733
734
#define SEND_BUFFER_SIZE 2048

735
static void zone_settimer(dns_zone_t *, isc_time_t *);
736
static void cancel_refresh(dns_zone_t *);
737
static void zone_debuglog(dns_zone_t *zone, const char *, int debuglevel,
738
739
740
			  const char *msg, ...) ISC_FORMAT_PRINTF(4, 5);
static void notify_log(dns_zone_t *zone, int level, const char *fmt, ...)
     ISC_FORMAT_PRINTF(3, 4);
741
static void queue_xfrin(dns_zone_t *zone);
742
743
744
745
static isc_result_t update_one_rr(dns_db_t *db, dns_dbversion_t *ver,
				  dns_diff_t *diff, dns_diffop_t op,
				  dns_name_t *name, dns_ttl_t ttl,
				  dns_rdata_t *rdata);
Mark Andrews's avatar
Mark Andrews committed
746
747
static void zone_unload(dns_zone_t *zone);
static void zone_expire(dns_zone_t *zone);
748
static void zone_iattach(dns_zone_t *source, dns_zone_t **target);
749
static void zone_idetach(dns_zone_t **zonep);
Mark Andrews's avatar
Mark Andrews committed
750
static isc_result_t zone_replacedb(dns_zone_t *zone, dns_db_t *db,
Brian Wellington's avatar
Brian Wellington committed
751
				   isc_boolean_t dump);
752
753
static inline void zone_attachdb(dns_zone_t *zone, dns_db_t *db);
static inline void zone_detachdb(dns_zone_t *zone);
754
static isc_result_t default_journal(dns_zone_t *zone);
755
static void zone_xfrdone(dns_zone_t *zone, isc_result_t result);
756
757
static isc_result_t zone_postload(dns_zone_t *zone, dns_db_t *db,
				  isc_time_t loadtime, isc_result_t result);
758
static void zone_needdump(dns_zone_t *zone, unsigned int delay);
759
static void zone_shutdown(isc_task_t *, isc_event_t *);
760
761
762
static void zone_loaddone(void *arg, isc_result_t result);
static isc_result_t zone_startload(dns_db_t *db, dns_zone_t *zone,
				   isc_time_t loadtime);
763
764
765
766
static void zone_namerd_tostr(dns_zone_t *zone, char *buf, size_t length);
static void zone_name_tostr(dns_zone_t *zone, char *buf, size_t length);
static void zone_rdclass_tostr(dns_zone_t *zone, char *buf, size_t length);
static void zone_viewname_tostr(dns_zone_t *zone, char *buf, size_t length);
767
768
static isc_result_t zone_send_secureserial(dns_zone_t *zone,
					   isc_uint32_t serial);
769
static void refresh_callback(isc_task_t *, isc_event_t *);
770
static void stub_callback(isc_task_t *, isc_event_t *);
771
772
static void queue_soa_query(dns_zone_t *zone);
static void soa_query(isc_task_t *, isc_event_t *);
773
774
static void ns_query(dns_zone_t *zone, dns_rdataset_t *soardataset,
		     dns_stub_t *stub);
775
776
static int message_count(dns_message_t *msg, dns_section_t section,
			 dns_rdatatype_t type);
777
static void notify_cancel(dns_zone_t *zone);
778
779
static void notify_find_address(dns_notify_t *notify);
static void notify_send(dns_notify_t *notify);
Mark Andrews's avatar
Mark Andrews committed
780
781
782
static isc_result_t notify_createmessage(dns_zone_t *zone,
					 unsigned int flags,
					 dns_message_t **messagep);
Mark Andrews's avatar
Mark Andrews committed
783
static void notify_done(isc_task_t *task, isc_event_t *event);
784
static void notify_send_toaddr(isc_task_t *task, isc_event_t *event);
785
static isc_result_t zone_dump(dns_zone_t *, isc_boolean_t);
786
static void got_transfer_quota(isc_task_t *task, isc_event_t *event);
David Lawrence's avatar
David Lawrence committed
787
788
static isc_result_t zmgr_start_xfrin_ifquota(dns_zonemgr_t *zmgr,
					     dns_zone_t *zone);
789
static void zmgr_resume_xfrs(dns_zonemgr_t *zmgr, isc_boolean_t multi);
790
static void zonemgr_free(dns_zonemgr_t *zmgr);
791
792
793
794
795
static isc_result_t zonemgr_getio(dns_zonemgr_t *zmgr, isc_boolean_t high,
				  isc_task_t *task, isc_taskaction_t action,
				  void *arg, dns_io_t **iop);
static void zonemgr_putio(dns_io_t **iop);
static void zonemgr_cancelio(dns_io_t *io);
796

797
static isc_result_t
798
zone_get_from_db(dns_zone_t *zone, dns_db_t *db, unsigned int *nscount,
799
800
		 unsigned int *soacount, isc_uint32_t *serial,
		 isc_uint32_t *refresh, isc_uint32_t *retry,
801
		 isc_uint32_t *expire, isc_uint32_t *minimum,
802
		 unsigned int *errors);
803

804
static void zone_freedbargs(dns_zone_t *zone);
805
static void forward_callback(isc_task_t *task, isc_event_t *event);
806
807
static void zone_saveunique(dns_zone_t *zone, const char *path,
			    const char *templat);
808
static void zone_maintenance(dns_zone_t *zone);
809
static void zone_notify(dns_zone_t *zone, isc_time_t *now);
810
static void dump_done(void *arg, isc_result_t result);
811
static isc_result_t zone_signwithkey(dns_zone_t *zone, dns_secalg_t algorithm,
812
				     isc_uint16_t keyid, isc_boolean_t deleteit);
813
814
815
static isc_result_t delete_nsec(dns_db_t *db, dns_dbversion_t *ver,
				dns_dbnode_t *node, dns_name_t *name,
				dns_diff_t *diff);
816
static void zone_rekey(dns_zone_t *zone);
817
static isc_result_t zone_send_securedb(dns_zone_t *zone, dns_db_t *db);
818
819
static void setrl(isc_ratelimiter_t *rl, unsigned int *rate,
		  unsigned int value);
820

821
#define ENTER zone_debuglog(zone, me, 1, "enter")
822

823
824
static const unsigned int dbargc_default = 1;
static const char *dbargv_default[] = { "rbt" };
825

826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
#define DNS_ZONE_JITTER_ADD(a, b, c) \
	do { \
		isc_interval_t _i; \
		isc_uint32_t _j; \
		_j = isc_random_jitter((b), (b)/4); \
		isc_interval_set(&_i, _j, 0); \
		if (isc_time_add((a), &_i, (c)) != ISC_R_SUCCESS) { \
			dns_zone_log(zone, ISC_LOG_WARNING, \
				     "epoch approaching: upgrade required: " \
				     "now + %s failed", #b); \
			isc_interval_set(&_i, _j/2, 0); \
			(void)isc_time_add((a), &_i, (c)); \
		} \
	} while (0)

#define DNS_ZONE_TIME_ADD(a, b, c) \
	do { \
		isc_interval_t _i; \
		isc_interval_set(&_i, (b), 0); \
		if (isc_time_add((a), &_i, (c)) != ISC_R_SUCCESS) { \
			dns_zone_log(zone, ISC_LOG_WARNING, \
				     "epoch approaching: upgrade required: " \
				     "now + %s failed", #b); \
			isc_interval_set(&_i, (b)/2, 0); \
			(void)isc_time_add((a), &_i, (c)); \
		} \
	} while (0)

854
855
856
857
858
859
860
861
862
863
864
865
866
867
typedef struct nsec3param nsec3param_t;
struct nsec3param {
	unsigned char data[DNS_NSEC3PARAM_BUFFERSIZE + 1];
	unsigned int length;
	isc_boolean_t nsec;
	isc_boolean_t replace;
	ISC_LINK(nsec3param_t)	link;
};
typedef ISC_LIST(nsec3param_t) nsec3paramlist_t;
struct np3event {
	isc_event_t event;
	nsec3param_t params;
};

868
869
870
871
872
struct ssevent {
	isc_event_t event;
	isc_uint32_t serial;
};

873
874
875
876
/*%
 * Increment resolver-related statistics counters.  Zone must be locked.
 */
static inline void
877
inc_stats(dns_zone_t *zone, isc_statscounter_t counter) {
878
	if (zone->stats != NULL)
879
		isc_stats_increment(zone->stats, counter);
880
881
}

882
883
884
885
/***
 ***	Public functions.
 ***/

886
isc_result_t
887
dns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) {
Andreas Gustafsson's avatar
Andreas Gustafsson committed
888
	isc_result_t result;
889
	dns_zone_t *zone;
890
	isc_time_t now;
891

892
893
894
	REQUIRE(zonep != NULL && *zonep == NULL);
	REQUIRE(mctx != NULL);

895
	TIME_NOW(&now);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
896
	zone = isc_mem_get(mctx, sizeof(*zone));
897
	if (zone == NULL) {
898
		return (ISC_R_NOMEMORY);
899
	}
900

901
902
903
	zone->mctx = NULL;
	isc_mem_attach(mctx, &zone->mctx);

Andreas Gustafsson's avatar
Andreas Gustafsson committed
904
	result = isc_mutex_init(&zone->lock);
905
	if (result != ISC_R_SUCCESS) {
906
		goto free_zone;
907
	}
908
909

	result = ZONEDB_INITLOCK(&zone->dblock);
910
	if (result != ISC_R_SUCCESS) {
911
		goto free_mutex;
912
	}
913
914

	/* XXX MPA check that all elements are initialised */
915
916
917
#ifdef DNS_ZONE_CHECKLOCK
	zone->locked = ISC_FALSE;
#endif
Mark Andrews's avatar
Mark Andrews committed
918
	zone->db = NULL;
919
920
	zone->zmgr = NULL;
	ISC_LINK_INIT(zone, link);
921
	result = isc_refcount_init(&zone->erefs, 1);	/* Implicit attach. */
922
	if (result != ISC_R_SUCCESS) {
923
		goto free_dblock;
924
	}
925
	zone->irefs = 0;
926
	dns_name_init(&zone->origin, NULL);
927
928
929
930
	zone->strnamerd = NULL;
	zone->strname = NULL;
	zone->strrdclass = NULL;
	zone->strviewname = NULL;
931
	zone->masterfile = NULL;
932
	ISC_LIST_INIT(zone->includes);
933
	ISC_LIST_INIT(zone->newincludes);
934
	zone->nincludes = 0;
935
	zone->masterformat = dns_masterformat_none;
Evan Hunt's avatar
Evan Hunt committed
936
	zone->masterstyle = NULL;
937
	zone->keydirectory = NULL;
938
	zone->journalsize = -1;
939
	zone->journal = NULL;
940
941
942
	zone->rdclass = dns_rdataclass_none;
	zone->type = dns_zone_none;
	zone->flags = 0;
943
	zone->options = 0;
944
	zone->options2 = 0;
945
	zone->keyopts = 0;
946
947
	zone->db_argc = 0;
	zone->db_argv = NULL;
948
949
950
	isc_time_settoepoch(&zone->expiretime);
	isc_time_settoepoch(&zone->refreshtime);
	isc_time_settoepoch(&zone->dumptime);
951
	isc_time_settoepoch(&zone->loadtime);
952
	zone->notifytime = now;
953
954
955
	isc_time_settoepoch(&zone->resigntime);
	isc_time_settoepoch(&zone->keywarntime);
	isc_time_settoepoch(&zone->signingtime);
956
	isc_time_settoepoch(&zone->nsec3chaintime);
957
	isc_time_settoepoch(&zone->refreshkeytime);
958
	zone->refreshkeyinterval = 0;
959
	zone->refreshkeycount = 0;
960
961
	zone->refresh = DNS_ZONE_DEFAULTREFRESH;
	zone->retry = DNS_ZONE_DEFAULTRETRY;
962
963
	zone->expire = 0;
	zone->minimum = 0;
964
965
966
967
	zone->maxrefresh = DNS_ZONE_MAXREFRESH;
	zone->minrefresh = DNS_ZONE_MINREFRESH;
	zone->maxretry = DNS_ZONE_MAXRETRY;
	zone->minretry = DNS_ZONE_MINRETRY;
968
	zone->masters = NULL;
Evan Hunt's avatar
Evan Hunt committed
969
	zone->masterdscps = NULL;
Michael Sawyer's avatar
Michael Sawyer committed
970
	zone->masterkeynames = NULL;
971
	zone->mastersok = NULL;
972
973
	zone->masterscnt = 0;
	zone->curmaster = 0;
Evan Hunt's avatar
Evan Hunt committed
974
	zone->maxttl = 0;
975
	zone->notify = NULL;
976
	zone->notifykeynames = NULL;
Tinderbox User's avatar
Tinderbox User committed
977
	zone->notifydscp = NULL;
978
	zone->notifytype = dns_notifytype_yes;
979
980
	zone->notifycnt = 0;
	zone->task = NULL;
981
	zone->loadtask = NULL;
982
	zone->update_acl = NULL;
983
	zone->forward_acl = NULL;
984
	zone->notify_acl = NULL;
985
	zone->query_acl = NULL;
986
	zone->queryon_acl = NULL;
987
	zone->xfr_acl = NULL;
988
	zone->update_disabled = ISC_FALSE;
989
	zone->zero_no_soa_ttl = ISC_TRUE;
990
	zone->check_names = dns_severity_ignore;
Mark Andrews's avatar
Mark Andrews committed
991
	zone->request = NULL;
992
	zone->lctx = NULL;
993
	zone->readio = NULL;
994
995
	zone->dctx = NULL;
	zone->writeio = NULL;
996
	zone->timer = NULL;
997
998
	zone->idlein = DNS_DEFAULT_IDLEIN;
	zone->idleout = DNS_DEFAULT_IDLEOUT;
Scott Mann's avatar
Scott Mann committed
999
	zone->log_key_expired_timer = 0;
Mark Andrews's avatar
Mark Andrews committed
1000
	ISC_LIST_INIT(zone->notifies);
1001
1002
	isc_sockaddr_any(&zone->notifysrc4);
	isc_sockaddr_any6(&zone->notifysrc6);
1003
1004
	isc_sockaddr_any(&zone->xfrsource4);
	isc_sockaddr_any6(&zone->xfrsource6);
1005
1006
	isc_sockaddr_any(&zone->altxfrsource4);
	isc_sockaddr_any6(&zone->altxfrsource6);
Evan Hunt's avatar
Evan Hunt committed
1007