zone.c 212 KB
Newer Older
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004-2007  Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 * Copyright (C) 1999-2003  Internet Software Consortium.
4
 *
Automatic Updater's avatar
Automatic Updater committed
5
 * Permission to use, copy, modify, and/or distribute this software for any
6 7
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
8
 *
Mark Andrews's avatar
Mark Andrews committed
9 10 11 12 13 14 15
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
16 17
 */

18
/* $Id: zone.c,v 1.471 2008/01/24 02:00:44 jinmei Exp $ */
19 20

/*! \file */
21 22 23

#include <config.h>

24
#include <isc/file.h>
25
#include <isc/mutex.h>
Mark Andrews's avatar
Mark Andrews committed
26
#include <isc/print.h>
27
#include <isc/random.h>
Mark Andrews's avatar
Mark Andrews committed
28
#include <isc/ratelimiter.h>
29
#include <isc/refcount.h>
30
#include <isc/rwlock.h>
31
#include <isc/serial.h>
32
#include <isc/string.h>
33
#include <isc/taskpool.h>
34
#include <isc/timer.h>
Michael Graff's avatar
Michael Graff committed
35
#include <isc/util.h>
36
#include <isc/xml.h>
37

38
#include <dns/acache.h>
39
#include <dns/acl.h>
Mark Andrews's avatar
Mark Andrews committed
40
#include <dns/adb.h>
41
#include <dns/callbacks.h>
42
#include <dns/db.h>
43
#include <dns/dbiterator.h>
44
#include <dns/events.h>
45
#include <dns/journal.h>
46
#include <dns/log.h>
47
#include <dns/master.h>
Mark Andrews's avatar
Mark Andrews committed
48
#include <dns/masterdump.h>
49
#include <dns/message.h>
50
#include <dns/name.h>
51
#include <dns/peer.h>
52
#include <dns/rcode.h>
53
#include <dns/rdataclass.h>
54
#include <dns/rdatalist.h>
55
#include <dns/rdataset.h>
Brian Wellington's avatar
Brian Wellington committed
56
#include <dns/rdatastruct.h>
57
#include <dns/rdatatype.h>
Mark Andrews's avatar
Mark Andrews committed
58
#include <dns/request.h>
59 60
#include <dns/resolver.h>
#include <dns/result.h>
61
#include <dns/stats.h>
62
#include <dns/ssu.h>
63
#include <dns/tsig.h>
64
#include <dns/xfrin.h>
65
#include <dns/zone.h>
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
#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)

88
/*%
89 90 91 92
 * Ensure 'a' is at least 'min' but not more than 'max'.
 */
#define RANGE(a, min, max) \
		(((a) < (min)) ? (min) : ((a) < (max) ? (a) : (max)))
93

94 95 96
/*
 * Default values.
 */
97 98 99
#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 */
Mark Andrews's avatar
Mark Andrews committed
100

101
#ifndef DNS_MAX_EXPIRE
102
#define DNS_MAX_EXPIRE	14515200	/*%< 24 weeks */
103
#endif
Mark Andrews's avatar
Mark Andrews committed
104

105
#ifndef DNS_DUMP_DELAY
106
#define DNS_DUMP_DELAY 900		/*%< 15 minutes */
107 108
#endif

109 110
typedef struct dns_notify dns_notify_t;
typedef struct dns_stub dns_stub_t;
111
typedef struct dns_load dns_load_t;
112
typedef struct dns_forward dns_forward_t;
113 114
typedef struct dns_io dns_io_t;
typedef ISC_LIST(dns_io_t) dns_iolist_t;
115

116 117 118
#define DNS_ZONE_CHECKLOCK
#ifdef DNS_ZONE_CHECKLOCK
#define LOCK_ZONE(z) \
Mark Andrews's avatar
Mark Andrews committed
119 120 121 122
	 do { LOCK(&(z)->lock); \
	      INSIST((z)->locked == ISC_FALSE); \
	     (z)->locked = ISC_TRUE; \
		} while (0)
123 124
#define UNLOCK_ZONE(z) \
	do { (z)->locked = ISC_FALSE; UNLOCK(&(z)->lock); } while (0)
Mark Andrews's avatar
Mark Andrews committed
125
#define LOCKED_ZONE(z) ((z)->locked)
126 127 128 129 130 131
#else
#define LOCK_ZONE(z) LOCK(&(z)->lock)
#define UNLOCK_ZONE(z) UNLOCK(&(z)->lock)
#define LOCKED_ZONE(z) ISC_TRUE
#endif

132 133 134 135 136 137 138 139 140 141 142 143
#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

144 145 146 147
struct dns_zone {
	/* Unlocked */
	unsigned int		magic;
	isc_mutex_t		lock;
148 149 150
#ifdef DNS_ZONE_CHECKLOCK
	isc_boolean_t		locked;
#endif
151
	isc_mem_t		*mctx;
152
	isc_refcount_t		erefs;
153

154 155 156 157 158 159 160
#ifdef ISC_RWLOCK_USEATOMIC
	isc_rwlock_t		dblock;
#else
	isc_mutex_t		dblock;
#endif
	dns_db_t		*db;		/* Locked by dblock */

161
	/* Locked */
162 163
	dns_zonemgr_t		*zmgr;
	ISC_LINK(dns_zone_t)	link;		/* Used by zmgr. */
164
	isc_timer_t		*timer;
165
	unsigned int		irefs;
166
	dns_name_t		origin;
Mark Andrews's avatar
Mark Andrews committed
167
	char			*masterfile;
168
	dns_masterformat_t	masterformat;
169
	char			*journal;
170
	isc_int32_t		journalsize;
171 172 173 174 175
	dns_rdataclass_t	rdclass;
	dns_zonetype_t		type;
	unsigned int		flags;
	unsigned int		options;
	unsigned int		db_argc;
176
	char			**db_argv;
177 178 179
	isc_time_t		expiretime;
	isc_time_t		refreshtime;
	isc_time_t		dumptime;
180
	isc_time_t		loadtime;
181
	isc_time_t		notifytime;
182 183 184 185 186
	isc_uint32_t		serial;
	isc_uint32_t		refresh;
	isc_uint32_t		retry;
	isc_uint32_t		expire;
	isc_uint32_t		minimum;
187
	char			*keydirectory;
188 189 190 191 192 193

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

194
	isc_sockaddr_t		*masters;
David Lawrence's avatar
tabify  
David Lawrence committed
195
	dns_name_t		**masterkeynames;
196
	isc_boolean_t		*mastersok;
197 198
	unsigned int		masterscnt;
	unsigned int		curmaster;
Mark Andrews's avatar
Mark Andrews committed
199
	isc_sockaddr_t		masteraddr;
200
	dns_notifytype_t	notifytype;
201
	isc_sockaddr_t		*notify;
202 203
	unsigned int		notifycnt;
	isc_sockaddr_t		notifyfrom;
204
	isc_task_t		*task;
205 206 207 208 209 210 211
	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;
Mark Andrews's avatar
Mark Andrews committed
212
	dns_xfrin_ctx_t		*xfr;		/* task locked */
213
	dns_tsigkey_t		*tsigkey;	/* key used for xfr */
214
	/* Access Control Lists */
215
	dns_acl_t		*update_acl;
216
	dns_acl_t		*forward_acl;
217
	dns_acl_t		*notify_acl;
218
	dns_acl_t		*query_acl;
219
	dns_acl_t		*queryon_acl;
220
	dns_acl_t		*xfr_acl;
221
	isc_boolean_t		update_disabled;
222
	isc_boolean_t		zero_no_soa_ttl;
223
	dns_severity_t		check_names;
224
	ISC_LIST(dns_notify_t)	notifies;
Mark Andrews's avatar
Mark Andrews committed
225
	dns_request_t		*request;
226
	dns_loadctx_t		*lctx;
227
	dns_io_t		*readio;
228 229
	dns_dumpctx_t		*dctx;
	dns_io_t		*writeio;
Mark Andrews's avatar
Mark Andrews committed
230 231
	isc_uint32_t		maxxfrin;
	isc_uint32_t		maxxfrout;
232 233
	isc_uint32_t		idlein;
	isc_uint32_t		idleout;
234
	isc_event_t		ctlevent;
235
	dns_ssutable_t		*ssutable;
236
	isc_uint32_t		sigvalidityinterval;
237
	dns_view_t		*view;
238
	dns_acache_t		*acache;
239 240 241
	dns_checkmxfunc_t	checkmx;
	dns_checksrvfunc_t	checksrv;
	dns_checknsfunc_t	checkns;
242
	/*%
243
	 * Zones in certain states such as "waiting for zone transfer"
244 245 246 247 248
	 * 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.
	 */
249
	ISC_LINK(dns_zone_t)	statelink;
David Lawrence's avatar
tabify  
David Lawrence committed
250
	dns_zonelist_t		*statelist;
251
	/*%
252
	 * Optional per-zone statistics counters (NULL if not present).
253
	 */
254
	dns_stats_t	    	*counters;
255
	isc_uint32_t		notifydelay;
256 257
	dns_isselffunc_t	isself;
	void			*isselfarg;
258 259 260 261 262

	char *			strnamerd;
	char *			strname;
	char *			strrdclass;
	char *			strviewname;
263 264 265 266 267

	/*%
	 * Serial number for deferred journal compaction.
	 */
	isc_uint32_t		compact_serial;
268 269
};

270
#define DNS_ZONE_FLAG(z,f) (ISC_TF(((z)->flags & (f)) != 0))
271 272 273 274 275 276 277 278
#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)
279
	/* XXX MPA these may need to go back into zone.h */
280 281 282 283 284 285 286 287 288 289
#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
290
						 * uptodate */
291
#define DNS_ZONEFLG_NEEDNOTIFY	0x00000400U	/*%< need to send out notify
Mark Andrews's avatar
Mark Andrews committed
292
						 * messages */
293
#define DNS_ZONEFLG_DIFFONRELOAD 0x00000800U	/*%< generate a journal diff on
294
						 * reload */
295
#define DNS_ZONEFLG_NOMASTERS	0x00001000U	/*%< an attempt to refresh a
296 297
						 * zone with no masters
						 * occured */
298 299
#define DNS_ZONEFLG_LOADING	0x00002000U	/*%< load from disk in progress*/
#define DNS_ZONEFLG_HAVETIMERS	0x00004000U	/*%< timer values have been set
300 301 302
						 * from SOA (if not set, we
						 * are still using
						 * default timer values) */
303
#define DNS_ZONEFLG_FORCEXFER   0x00008000U     /*%< Force a zone xfer */
Mark Andrews's avatar
Mark Andrews committed
304 305 306
#define DNS_ZONEFLG_NOREFRESH	0x00010000U
#define DNS_ZONEFLG_DIALNOTIFY	0x00020000U
#define DNS_ZONEFLG_DIALREFRESH	0x00040000U
307
#define DNS_ZONEFLG_SHUTDOWN	0x00080000U
308
#define DNS_ZONEFLAG_NOIXFR	0x00100000U	/*%< IXFR failed, force AXFR */
309
#define DNS_ZONEFLG_FLUSH	0x00200000U
310
#define DNS_ZONEFLG_NOEDNS	0x00400000U
311
#define DNS_ZONEFLG_USEALTXFRSRC 0x00800000U
312
#define DNS_ZONEFLG_SOABEFOREAXFR 0x01000000U
313
#define DNS_ZONEFLG_NEEDCOMPACT 0x02000000U
314

Mark Andrews's avatar
Mark Andrews committed
315
#define DNS_ZONE_OPTION(z,o) (((z)->options & (o)) != 0)
316

317 318 319
/* Flags for zone_load() */
#define DNS_ZONELOADFLAG_NOSTAT	0x00000001U	/* Do not stat() master files */

320 321 322 323 324 325 326 327 328 329
#define UNREACH_CHACHE_SIZE	10U
#define UNREACH_HOLD_TIME	600	/* 10 minutes */

struct dns_unreachable {
	isc_sockaddr_t	remote;
	isc_sockaddr_t	local;
	isc_uint32_t	expire;
	isc_uint32_t	last;
};

330
struct dns_zonemgr {
331
	unsigned int		magic;
332
	isc_mem_t *		mctx;
Mark Andrews's avatar
Mark Andrews committed
333
	int			refs;		/* Locked by rwlock */
334 335
	isc_taskmgr_t *		taskmgr;
	isc_timermgr_t *	timermgr;
336
	isc_socketmgr_t *	socketmgr;
337
	isc_taskpool_t *	zonetasks;
338
	isc_task_t *		task;
339
	isc_ratelimiter_t *	rl;
340
	isc_rwlock_t		rwlock;
341 342
	isc_mutex_t		iolock;

343
	/* Locked by rwlock. */
344 345 346
	dns_zonelist_t		zones;
	dns_zonelist_t		waiting_for_xfrin;
	dns_zonelist_t		xfrin_in_progress;
347

348
	/* Configuration data. */
349 350
	isc_uint32_t		transfersin;
	isc_uint32_t		transfersperns;
351
	unsigned int		serialqueryrate;
352 353 354 355 356 357

	/* Locked by iolock */
	isc_uint32_t		iolimit;
	isc_uint32_t		ioactive;
	dns_iolist_t		high;
	dns_iolist_t		low;
358 359 360 361
	
	/* Locked by rwlock. */
	/* LRU cache */
	struct dns_unreachable	unreachable[UNREACH_CHACHE_SIZE];
362 363
};

364
/*%
Mark Andrews's avatar
Mark Andrews committed
365 366
 * Hold notify state.
 */
367
struct dns_notify {
368
	unsigned int		magic;
Mark Andrews's avatar
Mark Andrews committed
369
	unsigned int		flags;
370
	isc_mem_t		*mctx;
Mark Andrews's avatar
Mark Andrews committed
371 372 373 374
	dns_zone_t		*zone;
	dns_adbfind_t		*find;
	dns_request_t		*request;
	dns_name_t		ns;
375
	isc_sockaddr_t		dst;
376 377 378
	ISC_LINK(dns_notify_t)	link;
};

Mark Andrews's avatar
Mark Andrews committed
379 380
#define DNS_NOTIFY_NOSOA	0x0001U

381
/*%
382 383 384 385 386 387
 *	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 {
388
	unsigned int		magic;
David Lawrence's avatar
tabify  
David Lawrence committed
389
	isc_mem_t		*mctx;
390 391 392
	dns_zone_t		*zone;
	dns_db_t		*db;
	dns_dbversion_t		*version;
Mark Andrews's avatar
Mark Andrews committed
393 394
};

395
/*%
396 397 398
 *	Hold load state.
 */
struct dns_load {
399
	unsigned int		magic;
400 401 402 403 404 405 406
	isc_mem_t		*mctx;
	dns_zone_t		*zone;
	dns_db_t		*db;
	isc_time_t		loadtime;
	dns_rdatacallbacks_t	callbacks;
};

407
/*%
408 409 410
 *	Hold forward state.
 */
struct dns_forward {
411
	unsigned int		magic;
412 413 414 415 416 417 418
	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
419
	void			*callback_arg;
420 421
};

422
/*%
423 424 425
 *	Hold IO request state.
 */
struct dns_io {
426
	unsigned int	magic;
427 428 429 430 431 432 433
	dns_zonemgr_t	*zmgr;
	isc_boolean_t	high;
	isc_task_t	*task;
	ISC_LINK(dns_io_t) link;
	isc_event_t	*event;
};

434 435
#define SEND_BUFFER_SIZE 2048

436
static void zone_settimer(dns_zone_t *, isc_time_t *);
437
static void cancel_refresh(dns_zone_t *);
438
static void zone_debuglog(dns_zone_t *zone, const char *, int debuglevel,
439 440 441
			  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);
442
static void queue_xfrin(dns_zone_t *zone);
Mark Andrews's avatar
Mark Andrews committed
443 444
static void zone_unload(dns_zone_t *zone);
static void zone_expire(dns_zone_t *zone);
445
static void zone_iattach(dns_zone_t *source, dns_zone_t **target);
446
static void zone_idetach(dns_zone_t **zonep);
Mark Andrews's avatar
Mark Andrews committed
447
static isc_result_t zone_replacedb(dns_zone_t *zone, dns_db_t *db,
Brian Wellington's avatar
Brian Wellington committed
448
				   isc_boolean_t dump);
449 450
static inline void zone_attachdb(dns_zone_t *zone, dns_db_t *db);
static inline void zone_detachdb(dns_zone_t *zone);
451
static isc_result_t default_journal(dns_zone_t *zone);
452
static void zone_xfrdone(dns_zone_t *zone, isc_result_t result);
453 454
static isc_result_t zone_postload(dns_zone_t *zone, dns_db_t *db,
				  isc_time_t loadtime, isc_result_t result);
455
static void zone_needdump(dns_zone_t *zone, unsigned int delay);
456
static void zone_shutdown(isc_task_t *, isc_event_t *);
457 458 459
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);
460 461 462 463
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);
464 465 466 467 468 469

#if 0
/* ondestroy example */
static void dns_zonemgr_dbdestroyed(isc_task_t *task, isc_event_t *event);
#endif

470
static void refresh_callback(isc_task_t *, isc_event_t *);
471
static void stub_callback(isc_task_t *, isc_event_t *);
472 473
static void queue_soa_query(dns_zone_t *zone);
static void soa_query(isc_task_t *, isc_event_t *);
474 475
static void ns_query(dns_zone_t *zone, dns_rdataset_t *soardataset,
		     dns_stub_t *stub);
476 477
static int message_count(dns_message_t *msg, dns_section_t section,
			 dns_rdatatype_t type);
478
static void notify_cancel(dns_zone_t *zone);
479 480
static void notify_find_address(dns_notify_t *notify);
static void notify_send(dns_notify_t *notify);
Mark Andrews's avatar
Mark Andrews committed
481 482 483
static isc_result_t notify_createmessage(dns_zone_t *zone,
					 unsigned int flags,
					 dns_message_t **messagep);
Mark Andrews's avatar
Mark Andrews committed
484
static void notify_done(isc_task_t *task, isc_event_t *event);
485
static void notify_send_toaddr(isc_task_t *task, isc_event_t *event);
486
static isc_result_t zone_dump(dns_zone_t *, isc_boolean_t);
487
static void got_transfer_quota(isc_task_t *task, isc_event_t *event);
David Lawrence's avatar
David Lawrence committed
488 489
static isc_result_t zmgr_start_xfrin_ifquota(dns_zonemgr_t *zmgr,
					     dns_zone_t *zone);
490
static void zmgr_resume_xfrs(dns_zonemgr_t *zmgr, isc_boolean_t multi);
491
static void zonemgr_free(dns_zonemgr_t *zmgr);
492 493 494 495 496
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);
497

498
static isc_result_t
499
zone_get_from_db(dns_zone_t *zone, dns_db_t *db, unsigned int *nscount,
500 501
		 unsigned int *soacount, isc_uint32_t *serial,
		 isc_uint32_t *refresh, isc_uint32_t *retry,
502
		 isc_uint32_t *expire, isc_uint32_t *minimum,
503
		 unsigned int *errors);
504

505
static void zone_freedbargs(dns_zone_t *zone);
506
static void forward_callback(isc_task_t *task, isc_event_t *event);
507 508
static void zone_saveunique(dns_zone_t *zone, const char *path,
			    const char *templat);
509
static void zone_maintenance(dns_zone_t *zone);
510
static void zone_notify(dns_zone_t *zone, isc_time_t *now);
511
static void dump_done(void *arg, isc_result_t result);
512 513 514 515
static isc_boolean_t dns_zonemgr_unreachable(dns_zonemgr_t *zmgr,
					     isc_sockaddr_t *remote,
					     isc_sockaddr_t *local,
					     isc_time_t *now);
516

517
#define ENTER zone_debuglog(zone, me, 1, "enter")
518

519 520
static const unsigned int dbargc_default = 1;
static const char *dbargv_default[] = { "rbt" };
521

522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
#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)

550 551 552 553
/***
 ***	Public functions.
 ***/

554
isc_result_t
555
dns_zone_create(dns_zone_t **zonep, isc_mem_t *mctx) {
Andreas Gustafsson's avatar
Andreas Gustafsson committed
556
	isc_result_t result;
557
	dns_zone_t *zone;
558
	isc_time_t now;
559

560 561 562
	REQUIRE(zonep != NULL && *zonep == NULL);
	REQUIRE(mctx != NULL);

563
	TIME_NOW(&now);
Andreas Gustafsson's avatar
Andreas Gustafsson committed
564
	zone = isc_mem_get(mctx, sizeof(*zone));
565
	if (zone == NULL)
566
		return (ISC_R_NOMEMORY);
567

568 569 570
	zone->mctx = NULL;
	isc_mem_attach(mctx, &zone->mctx);

Andreas Gustafsson's avatar
Andreas Gustafsson committed
571
	result = isc_mutex_init(&zone->lock);
572
	if (result != ISC_R_SUCCESS)
573 574 575
		goto free_zone;

	result = ZONEDB_INITLOCK(&zone->dblock);
576
	if (result != ISC_R_SUCCESS)
577
		goto free_mutex;
578 579

	/* XXX MPA check that all elements are initialised */
580 581 582
#ifdef DNS_ZONE_CHECKLOCK
	zone->locked = ISC_FALSE;
#endif
Mark Andrews's avatar
Mark Andrews committed
583
	zone->db = NULL;
584 585
	zone->zmgr = NULL;
	ISC_LINK_INIT(zone, link);
586 587 588
	result = isc_refcount_init(&zone->erefs, 1);	/* Implicit attach. */
	if (result != ISC_R_SUCCESS)
		goto free_dblock;
589
	zone->irefs = 0;
590
	dns_name_init(&zone->origin, NULL);
591 592 593 594
	zone->strnamerd = NULL;
	zone->strname = NULL;
	zone->strrdclass = NULL;
	zone->strviewname = NULL;
595
	zone->masterfile = NULL;
596
	zone->masterformat =  dns_masterformat_none;
597
	zone->keydirectory = NULL;
598
	zone->journalsize = -1;
599
	zone->journal = NULL;
600 601 602
	zone->rdclass = dns_rdataclass_none;
	zone->type = dns_zone_none;
	zone->flags = 0;
603
	zone->options = 0;
604 605
	zone->db_argc = 0;
	zone->db_argv = NULL;
606 607 608
	isc_time_settoepoch(&zone->expiretime);
	isc_time_settoepoch(&zone->refreshtime);
	isc_time_settoepoch(&zone->dumptime);
609
	isc_time_settoepoch(&zone->loadtime);
610
	zone->notifytime = now;
611
	zone->serial = 0;
612 613
	zone->refresh = DNS_ZONE_DEFAULTREFRESH;
	zone->retry = DNS_ZONE_DEFAULTRETRY;
614 615
	zone->expire = 0;
	zone->minimum = 0;
616 617 618 619
	zone->maxrefresh = DNS_ZONE_MAXREFRESH;
	zone->minrefresh = DNS_ZONE_MINREFRESH;
	zone->maxretry = DNS_ZONE_MAXRETRY;
	zone->minretry = DNS_ZONE_MINRETRY;
620
	zone->masters = NULL;
Michael Sawyer's avatar
Michael Sawyer committed
621
	zone->masterkeynames = NULL;
622
	zone->mastersok = NULL;
623 624 625
	zone->masterscnt = 0;
	zone->curmaster = 0;
	zone->notify = NULL;
626
	zone->notifytype = dns_notifytype_yes;
627 628 629
	zone->notifycnt = 0;
	zone->task = NULL;
	zone->update_acl = NULL;
630
	zone->forward_acl = NULL;
631
	zone->notify_acl = NULL;
632
	zone->query_acl = NULL;
633
	zone->queryon_acl = NULL;
634
	zone->xfr_acl = NULL;
635
	zone->update_disabled = ISC_FALSE;
636
	zone->zero_no_soa_ttl = ISC_TRUE;
637
	zone->check_names = dns_severity_ignore;
Mark Andrews's avatar
Mark Andrews committed
638
	zone->request = NULL;
639
	zone->lctx = NULL;
640
	zone->readio = NULL;
641 642
	zone->dctx = NULL;
	zone->writeio = NULL;
643
	zone->timer = NULL;
644 645
	zone->idlein = DNS_DEFAULT_IDLEIN;
	zone->idleout = DNS_DEFAULT_IDLEOUT;
Mark Andrews's avatar
Mark Andrews committed
646
	ISC_LIST_INIT(zone->notifies);
647 648
	isc_sockaddr_any(&zone->notifysrc4);
	isc_sockaddr_any6(&zone->notifysrc6);
649 650
	isc_sockaddr_any(&zone->xfrsource4);
	isc_sockaddr_any6(&zone->xfrsource6);
651 652
	isc_sockaddr_any(&zone->altxfrsource4);
	isc_sockaddr_any6(&zone->altxfrsource6);
653
	zone->xfr = NULL;
654
	zone->tsigkey = NULL;
Mark Andrews's avatar
Mark Andrews committed
655 656
	zone->maxxfrin = MAX_XFER_TIME;
	zone->maxxfrout = MAX_XFER_TIME;
657
	zone->ssutable = NULL;
658
	zone->sigvalidityinterval = 30 * 24 * 3600;
659
	zone->view = NULL;
660
	zone->acache = NULL;
661 662 663
	zone->checkmx = NULL;
	zone->checksrv = NULL;
	zone->checkns = NULL;
664 665
	ISC_LINK_INIT(zone, statelink);
	zone->statelist = NULL;
666
	zone->counters = NULL;
667
	zone->notifydelay = 5;
668 669
	zone->isself = NULL;
	zone->isselfarg = NULL;
670

671
	zone->magic = ZONE_MAGIC;
672 673

	/* Must be after magic is set. */
674
	result = dns_zone_setdbtype(zone, dbargc_default, dbargv_default);
675
	if (result != ISC_R_SUCCESS)
676
		goto free_erefs;
Mark Andrews's avatar
Mark Andrews committed
677

678 679 680
	ISC_EVENT_INIT(&zone->ctlevent, sizeof(zone->ctlevent), 0, NULL,
		       DNS_EVENT_ZONECONTROL, zone_shutdown, zone, zone,
		       NULL, NULL);
681
	*zonep = zone;
682
	return (ISC_R_SUCCESS);
683

684 685 686 687
 free_erefs:
	isc_refcount_decrement(&zone->erefs, NULL);
	isc_refcount_destroy(&zone->erefs);

688 689 690
 free_dblock:
	ZONEDB_DESTROYLOCK(&zone->dblock);

691
 free_mutex:
692
	DESTROYLOCK(&zone->lock);
693 694

 free_zone:
695 696
	isc_mem_putanddetach(&zone->mctx, zone, sizeof(*zone));
	return (result);
697 698
}

699 700 701 702
/*
 * Free a zone.  Because we require that there be no more
 * outstanding events or references, no locking is necessary.
 */
703 704
static void
zone_free(dns_zone_t *zone) {
Mark Andrews's avatar
Mark Andrews committed
705
	isc_mem_t *mctx = NULL;
706

707
	REQUIRE(DNS_ZONE_VALID(zone));
708
	REQUIRE(isc_refcount_current(&zone->erefs) == 0);
709
	REQUIRE(zone->irefs == 0);
710
	REQUIRE(!LOCKED_ZONE(zone));
711
	REQUIRE(zone->timer == NULL);
712

713 714 715
	/*
	 * Managed objects.  Order is important.
	 */
716 717
	if (zone->request != NULL)
		dns_request_destroy(&zone->request); /* XXXMPA */
718
	INSIST(zone->readio == NULL);
719
	INSIST(zone->statelist == NULL);
720
	INSIST(zone->writeio == NULL);
721

722
	if (zone->task != NULL)
723
		isc_task_detach(&zone->task);
724
	if (zone->zmgr != NULL)
725
		dns_zonemgr_releasezone(zone->zmgr, zone);
726

727
	/* Unmanaged objects */
728 729 730
	if (zone->masterfile != NULL)
		isc_mem_free(zone->mctx, zone->masterfile);
	zone->masterfile = NULL;
731 732 733
	if (zone->keydirectory != NULL)
		isc_mem_free(zone->mctx, zone->keydirectory);
	zone->keydirectory = NULL;
734
	zone->journalsize = -1;
735 736 737
	if (zone->journal != NULL)
		isc_mem_free(zone->mctx, zone->journal);
	zone->journal = NULL;
738
	if (zone->counters != NULL)
739
		dns_stats_destroy(zone->mctx, &zone->counters);
Mark Andrews's avatar
Mark Andrews committed
740
	if (zone->db != NULL)
741 742 743
		zone_detachdb(zone);
	if (zone->acache != NULL)
		dns_acache_detach(&zone->acache);
744
	zone_freedbargs(zone);
745 746 747 748
	RUNTIME_CHECK(dns_zone_setmasterswithkeys(zone, NULL, NULL, 0)
		      == ISC_R_SUCCESS);
	RUNTIME_CHECK(dns_zone_setalsonotify(zone, NULL, 0)
		      == ISC_R_SUCCESS);
749
	zone->check_names = dns_severity_ignore;
750
	if (zone->update_acl != NULL)
751
		dns_acl_detach(&zone->update_acl);
752 753
	if (zone->forward_acl != NULL)
		dns_acl_detach(&zone->forward_acl);
754 755
	if (zone->notify_acl != NULL)
		dns_acl_detach(&zone->notify_acl);
756
	if (zone->query_acl != NULL)
757
		dns_acl_detach(&zone->query_acl);
758 759
	if (zone->queryon_acl != NULL)
		dns_acl_detach(&zone->queryon_acl);
760
	if (zone->xfr_acl != NULL)
761
		dns_acl_detach(&zone->xfr_acl);
762 763
	if (dns_name_dynamic(&zone->origin))
		dns_name_free(&zone->origin, zone->mctx);
764 765 766 767 768 769 770 771
	if (zone->strnamerd != NULL)
		isc_mem_free(zone->mctx, zone->strnamerd);
	if (zone->strname != NULL)
		isc_mem_free(zone->mctx, zone->strname);
	if (zone->strrdclass != NULL)
		isc_mem_free(zone->mctx, zone->strrdclass);
	if (zone->strviewname != NULL)
		isc_mem_free(zone->mctx, zone->strviewname);
772
	if (zone->ssutable != NULL)
773
		dns_ssutable_detach(&zone->ssutable);
774 775

	/* last stuff */
776
	ZONEDB_DESTROYLOCK(&zone->dblock);
777
	DESTROYLOCK(&zone->lock);
778
	isc_refcount_destroy(&zone->erefs);
779
	zone->magic = 0;
Mark Andrews's avatar
Mark Andrews committed
780
	mctx = zone->mctx;
Andreas Gustafsson's avatar
Andreas Gustafsson committed
781
	isc_mem_put(mctx, zone, sizeof(*zone));
Mark Andrews's avatar
Mark Andrews committed
782
	isc_mem_detach(&mctx);
783 784 785 786 787 788 789
}

/*
 *	Single shot.
 */
void
dns_zone_setclass(dns_zone_t *zone, dns_rdataclass_t rdclass) {
790
	char namebuf[1024];
791 792

	REQUIRE(DNS_ZONE_VALID(zone));
793
	REQUIRE(rdclass != dns_rdataclass_none);
794

795 796 797
	/*
	 * Test and set.
	 */
798
	LOCK_ZONE(zone);
799 800 801
	REQUIRE(zone->rdclass == dns_rdataclass_none ||
		zone->rdclass == rdclass);
	zone->rdclass = rdclass;
802 803 804 805 806 807 808 809 810 811 812

	if (zone->strnamerd != NULL)
		isc_mem_free(zone->mctx, zone->strnamerd);
	if (zone->strrdclass != NULL)
		isc_mem_free(zone->mctx, zone->strrdclass);

	zone_namerd_tostr(zone, namebuf, sizeof namebuf);
	zone->strnamerd = isc_mem_strdup(zone->mctx, namebuf);
	zone_rdclass_tostr(zone, namebuf, sizeof namebuf);
	zone->strrdclass = isc_mem_strdup(zone->mctx, namebuf);

813
	UNLOCK_ZONE(zone);
814 815
}

816 817 818 819 820 821 822
dns_rdataclass_t
dns_zone_getclass(dns_zone_t *zone){
	REQUIRE(DNS_ZONE_VALID(zone));

	return (zone->rdclass);
}

823 824 825 826
void
dns_zone_setnotifytype(dns_zone_t *zone, dns_notifytype_t notifytype) {
	REQUIRE(DNS_ZONE_VALID(zone));

827
	LOCK_ZONE(zone);
828
	zone->notifytype = notifytype;
829
	UNLOCK_ZONE(zone);