zoneconf.c 31.8 KB
Newer Older
1
/*
Automatic Updater's avatar
Automatic Updater committed
2
 * Copyright (C) 2004-2010  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: zoneconf.c,v 1.164 2010/08/11 18:14:18 each Exp $ */
19 20

/*% */
David Lawrence's avatar
David Lawrence committed
21

22 23
#include <config.h>

24
#include <isc/buffer.h>
25
#include <isc/file.h>
26
#include <isc/mem.h>
27
#include <isc/print.h>
28
#include <isc/stats.h>
29
#include <isc/string.h>		/* Required for HP/UX (and others?) */
Bob Halley's avatar
Bob Halley committed
30
#include <isc/util.h>
31

32
#include <dns/acl.h>
33
#include <dns/fixedname.h>
34
#include <dns/log.h>
35 36
#include <dns/name.h>
#include <dns/rdatatype.h>
Mark Andrews's avatar
Mark Andrews committed
37
#include <dns/ssu.h>
38
#include <dns/stats.h>
39
#include <dns/view.h>
40
#include <dns/zone.h>
41

42
#include <named/client.h>
43
#include <named/config.h>
44 45
#include <named/globals.h>
#include <named/log.h>
46
#include <named/server.h>
47
#include <named/zoneconf.h>
48

49 50 51 52 53 54 55 56 57
/* ACLs associated with zone */
typedef enum {
	allow_notify,
	allow_query,
	allow_transfer,
	allow_update,
	allow_update_forwarding
} acl_type_t;

58 59 60 61 62 63
#define RETERR(x) do { \
	isc_result_t _r = (x); \
	if (_r != ISC_R_SUCCESS) \
		return (_r); \
	} while (0)

64 65 66 67 68 69
#define CHECK(x) do { \
	result = (x); \
	if (result != ISC_R_SUCCESS) \
		goto cleanup; \
	} while (0)

70
/*%
71 72 73
 * Convenience function for configuring a single zone ACL.
 */
static isc_result_t
74
configure_zone_acl(const cfg_obj_t *zconfig, const cfg_obj_t *vconfig,
75
		   const cfg_obj_t *config, acl_type_t acltype,
Automatic Updater's avatar
Automatic Updater committed
76
		   cfg_aclconfctx_t *actx, dns_zone_t *zone,
77 78 79 80
		   void (*setzacl)(dns_zone_t *, dns_acl_t *),
		   void (*clearzacl)(dns_zone_t *))
{
	isc_result_t result;
81
	const cfg_obj_t *maps[5] = {NULL, NULL, NULL, NULL, NULL};
82
	const cfg_obj_t *aclobj = NULL;
83
	int i = 0;
84 85
	dns_acl_t **aclp = NULL, *acl = NULL;
	const char *aclname;
Automatic Updater's avatar
Automatic Updater committed
86
	dns_view_t *view;
87

Automatic Updater's avatar
Automatic Updater committed
88
	view = dns_zone_getview(zone);
89 90 91

	switch (acltype) {
	    case allow_notify:
Automatic Updater's avatar
Automatic Updater committed
92 93
		if (view != NULL)
			aclp = &view->notifyacl;
94 95 96
		aclname = "allow-notify";
		break;
	    case allow_query:
Automatic Updater's avatar
Automatic Updater committed
97 98
		if (view != NULL)
			aclp = &view->queryacl;
99 100 101
		aclname = "allow-query";
		break;
	    case allow_transfer:
Automatic Updater's avatar
Automatic Updater committed
102 103
		if (view != NULL)
			aclp = &view->transferacl;
104 105 106
		aclname = "allow-transfer";
		break;
	    case allow_update:
Automatic Updater's avatar
Automatic Updater committed
107 108
		if (view != NULL)
			aclp = &view->updateacl;
109 110 111
		aclname = "allow-update";
		break;
	    case allow_update_forwarding:
Automatic Updater's avatar
Automatic Updater committed
112 113
		if (view != NULL)
			aclp = &view->upfwdacl;
114 115
		aclname = "allow-update-forwarding";
		break;
Automatic Updater's avatar
Automatic Updater committed
116 117 118
	    default:
		INSIST(0);
		return (ISC_R_FAILURE);
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
	}

	/* First check to see if ACL is defined within the zone */
	if (zconfig != NULL) {
		maps[0] = cfg_tuple_get(zconfig, "options");
		ns_config_get(maps, aclname, &aclobj);
		if (aclobj != NULL) {
			aclp = NULL;
			goto parse_acl;
		}
	}

	/* Failing that, see if there's a default ACL already in the view */
	if (aclp != NULL && *aclp != NULL) {
		(*setzacl)(zone, *aclp);
		return (ISC_R_SUCCESS);
	}
136

137
	/* Check for default ACLs that haven't been parsed yet */
138 139 140 141 142
	if (vconfig != NULL) {
		const cfg_obj_t *options = cfg_tuple_get(vconfig, "options");
		if (options != NULL)
			maps[i++] = options;
	}
143
	if (config != NULL) {
144
		const cfg_obj_t *options = NULL;
145 146 147
		(void)cfg_map_get(config, "options", &options);
		if (options != NULL)
			maps[i++] = options;
148
	}
149
	maps[i++] = ns_g_defaults;
150 151 152 153
	maps[i] = NULL;

	result = ns_config_get(maps, aclname, &aclobj);
	if (aclobj == NULL) {
154 155 156
		(*clearzacl)(zone);
		return (ISC_R_SUCCESS);
	}
157

158
parse_acl:
159
	result = cfg_acl_fromconfig(aclobj, config, ns_g_lctx, actx,
160
				    dns_zone_getmctx(zone), 0, &acl);
161 162
	if (result != ISC_R_SUCCESS)
		return (result);
163 164
	(*setzacl)(zone, acl);

Automatic Updater's avatar
Automatic Updater committed
165
	/* Set the view default now */
166 167 168 169
	if (aclp != NULL)
		dns_acl_attach(acl, aclp);

	dns_acl_detach(&acl);
170
	return (ISC_R_SUCCESS);
171 172
}

173
/*%
174
 * Parse the zone update-policy statement.
175
 */
176
static isc_result_t
177
configure_zone_ssutable(const cfg_obj_t *zconfig, dns_zone_t *zone,
178 179
			const char *zname)
{
180 181
	const cfg_obj_t *updatepolicy = NULL;
	const cfg_listelt_t *element, *element2;
182 183
	dns_ssutable_t *table = NULL;
	isc_mem_t *mctx = dns_zone_getmctx(zone);
Automatic Updater's avatar
Automatic Updater committed
184
	isc_boolean_t autoddns = ISC_FALSE;
185 186 187
	isc_result_t result;

	(void)cfg_map_get(zconfig, "update-policy", &updatepolicy);
188

189
	if (updatepolicy == NULL) {
190
		dns_zone_setssutable(zone, NULL);
191
		return (ISC_R_SUCCESS);
192
	}
193

Automatic Updater's avatar
Automatic Updater committed
194 195 196 197 198
	if (cfg_obj_isstring(updatepolicy) &&
	    strcmp("local", cfg_obj_asstring(updatepolicy)) == 0) {
		autoddns = ISC_TRUE;
		updatepolicy = NULL;
	}
199

200 201 202 203 204 205 206 207
	result = dns_ssutable_create(mctx, &table);
	if (result != ISC_R_SUCCESS)
		return (result);

	for (element = cfg_list_first(updatepolicy);
	     element != NULL;
	     element = cfg_list_next(element))
	{
208 209 210 211 212 213
		const cfg_obj_t *stmt = cfg_listelt_value(element);
		const cfg_obj_t *mode = cfg_tuple_get(stmt, "mode");
		const cfg_obj_t *identity = cfg_tuple_get(stmt, "identity");
		const cfg_obj_t *matchtype = cfg_tuple_get(stmt, "matchtype");
		const cfg_obj_t *dname = cfg_tuple_get(stmt, "name");
		const cfg_obj_t *typelist = cfg_tuple_get(stmt, "types");
214
		const char *str;
215
		isc_boolean_t grant = ISC_FALSE;
216
		isc_boolean_t usezone = ISC_FALSE;
217
		unsigned int mtype = DNS_SSUMATCHTYPE_NAME;
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
		dns_fixedname_t fname, fident;
		isc_buffer_t b;
		dns_rdatatype_t *types;
		unsigned int i, n;

		str = cfg_obj_asstring(mode);
		if (strcasecmp(str, "grant") == 0)
			grant = ISC_TRUE;
		else if (strcasecmp(str, "deny") == 0)
			grant = ISC_FALSE;
		else
			INSIST(0);

		str = cfg_obj_asstring(matchtype);
		if (strcasecmp(str, "name") == 0)
			mtype = DNS_SSUMATCHTYPE_NAME;
		else if (strcasecmp(str, "subdomain") == 0)
			mtype = DNS_SSUMATCHTYPE_SUBDOMAIN;
		else if (strcasecmp(str, "wildcard") == 0)
			mtype = DNS_SSUMATCHTYPE_WILDCARD;
		else if (strcasecmp(str, "self") == 0)
			mtype = DNS_SSUMATCHTYPE_SELF;
240 241 242 243
		else if (strcasecmp(str, "selfsub") == 0)
			mtype = DNS_SSUMATCHTYPE_SELFSUB;
		else if (strcasecmp(str, "selfwild") == 0)
			mtype = DNS_SSUMATCHTYPE_SELFWILD;
244 245 246 247 248 249 250 251
		else if (strcasecmp(str, "ms-self") == 0)
			mtype = DNS_SSUMATCHTYPE_SELFMS;
		else if (strcasecmp(str, "krb5-self") == 0)
			mtype = DNS_SSUMATCHTYPE_SELFKRB5;
		else if (strcasecmp(str, "ms-subdomain") == 0)
			mtype = DNS_SSUMATCHTYPE_SUBDOMAINMS;
		else if (strcasecmp(str, "krb5-subdomain") == 0)
			mtype = DNS_SSUMATCHTYPE_SUBDOMAINKRB5;
252 253 254 255
		else if (strcasecmp(str, "tcp-self") == 0)
			mtype = DNS_SSUMATCHTYPE_TCPSELF;
		else if (strcasecmp(str, "6to4-self") == 0)
			mtype = DNS_SSUMATCHTYPE_6TO4SELF;
256 257 258 259
		else if (strcasecmp(str, "zonesub") == 0) {
			mtype = DNS_SSUMATCHTYPE_SUBDOMAIN;
			usezone = ISC_TRUE;
		} else
260 261 262 263 264 265 266
			INSIST(0);

		dns_fixedname_init(&fident);
		str = cfg_obj_asstring(identity);
		isc_buffer_init(&b, str, strlen(str));
		isc_buffer_add(&b, strlen(str));
		result = dns_name_fromtext(dns_fixedname_name(&fident), &b,
267
					   dns_rootname, 0, NULL);
268 269 270 271 272 273 274
		if (result != ISC_R_SUCCESS) {
			cfg_obj_log(identity, ns_g_lctx, ISC_LOG_ERROR,
				    "'%s' is not a valid name", str);
			goto cleanup;
		}

		dns_fixedname_init(&fname);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
		if (usezone) {
			result = dns_name_copy(dns_zone_getorigin(zone),
					       dns_fixedname_name(&fname),
					       NULL);
			if (result != ISC_R_SUCCESS) {
				cfg_obj_log(identity, ns_g_lctx, ISC_LOG_ERROR,
					    "error copying origin: %s",
					    isc_result_totext(result));
				goto cleanup;
			}
		} else {
			str = cfg_obj_asstring(dname);
			isc_buffer_init(&b, str, strlen(str));
			isc_buffer_add(&b, strlen(str));
			result = dns_name_fromtext(dns_fixedname_name(&fname),
290
						   &b, dns_rootname, 0, NULL);
291 292 293 294 295
			if (result != ISC_R_SUCCESS) {
				cfg_obj_log(identity, ns_g_lctx, ISC_LOG_ERROR,
					    "'%s' is not a valid name", str);
				goto cleanup;
			}
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
		}

		n = ns_config_listcount(typelist);
		if (n == 0)
			types = NULL;
		else {
			types = isc_mem_get(mctx, n * sizeof(dns_rdatatype_t));
			if (types == NULL) {
				result = ISC_R_NOMEMORY;
				goto cleanup;
			}
		}

		i = 0;
		for (element2 = cfg_list_first(typelist);
		     element2 != NULL;
		     element2 = cfg_list_next(element2))
		{
314
			const cfg_obj_t *typeobj;
315 316 317 318 319 320
			isc_textregion_t r;

			INSIST(i < n);

			typeobj = cfg_listelt_value(element2);
			str = cfg_obj_asstring(typeobj);
321
			DE_CONST(str, r.base);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
			r.length = strlen(str);

			result = dns_rdatatype_fromtext(&types[i++], &r);
			if (result != ISC_R_SUCCESS) {
				cfg_obj_log(identity, ns_g_lctx, ISC_LOG_ERROR,
					    "'%s' is not a valid type", str);
				isc_mem_put(mctx, types,
					    n * sizeof(dns_rdatatype_t));
				goto cleanup;
			}
		}
		INSIST(i == n);

		result = dns_ssutable_addrule(table, grant,
					      dns_fixedname_name(&fident),
					      mtype,
					      dns_fixedname_name(&fname),
					      n, types);
		if (types != NULL)
			isc_mem_put(mctx, types, n * sizeof(dns_rdatatype_t));
		if (result != ISC_R_SUCCESS) {
			goto cleanup;
		}
345
	}
346

347
	/*
348 349 350
	 * If "update-policy local;" and a session key exists,
	 * then use the default policy, which is equivalent to:
	 * update-policy { grant <session-keyname> zonesub any; };
351 352 353 354
	 */
	if (autoddns) {
		dns_rdatatype_t any = dns_rdatatype_any;

355
		if (ns_g_server->session_keyname == NULL) {
356 357 358 359 360 361 362 363 364 365
			isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
				      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
				      "failed to enable auto DDNS policy "
				      "for zone %s: session key not found",
				      zname);
			result = ISC_R_NOTFOUND;
			goto cleanup;
		}

		result = dns_ssutable_addrule(table, ISC_TRUE,
366
					      ns_g_server->session_keyname,
367 368 369 370 371 372
					      DNS_SSUMATCHTYPE_SUBDOMAIN,
					      dns_zone_getorigin(zone),
					      1, &any);

		if (result != ISC_R_SUCCESS)
			goto cleanup;
373
	}
374 375 376 377 378 379 380 381 382

	result = ISC_R_SUCCESS;
	dns_zone_setssutable(zone, table);

 cleanup:
	dns_ssutable_detach(&table);
	return (result);
}

383
/*%
384 385 386
 * Convert a config file zone type into a server zone type.
 */
static inline dns_zonetype_t
387 388
zonetype_fromconfig(const cfg_obj_t *map) {
	const cfg_obj_t *obj = NULL;
389 390 391 392 393
	isc_result_t result;

	result = cfg_map_get(map, "type", &obj);
	INSIST(result == ISC_R_SUCCESS);
	return (ns_config_getzonetype(obj));
394 395
}

396
/*%
397 398 399
 * Helper function for strtoargv().  Pardon the gratuitous recursion.
 */
static isc_result_t
Andreas Gustafsson's avatar
Andreas Gustafsson committed
400 401 402
strtoargvsub(isc_mem_t *mctx, char *s, unsigned int *argcp,
	     char ***argvp, unsigned int n)
{
403
	isc_result_t result;
Automatic Updater's avatar
Automatic Updater committed
404

405 406 407
	/* Discard leading whitespace. */
	while (*s == ' ' || *s == '\t')
		s++;
Automatic Updater's avatar
Automatic Updater committed
408

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
	if (*s == '\0') {
		/* We have reached the end of the string. */
		*argcp = n;
		*argvp = isc_mem_get(mctx, n * sizeof(char *));
		if (*argvp == NULL)
			return (ISC_R_NOMEMORY);
	} else {
		char *p = s;
		while (*p != ' ' && *p != '\t' && *p != '\0')
			p++;
		if (*p != '\0')
			*p++ = '\0';

		result = strtoargvsub(mctx, p, argcp, argvp, n + 1);
		if (result != ISC_R_SUCCESS)
			return (result);
		(*argvp)[n] = s;
	}
	return (ISC_R_SUCCESS);
}

430
/*%
431 432 433 434 435 436 437
 * Tokenize the string "s" into whitespace-separated words,
 * return the number of words in '*argcp' and an array
 * of pointers to the words in '*argvp'.  The caller
 * must free the array using isc_mem_put().  The string
 * is modified in-place.
 */
static isc_result_t
Andreas Gustafsson's avatar
Andreas Gustafsson committed
438
strtoargv(isc_mem_t *mctx, char *s, unsigned int *argcp, char ***argvp) {
439 440 441
	return (strtoargvsub(mctx, s, argcp, argvp, 0));
}

442
static void
443 444 445
checknames(dns_zonetype_t ztype, const cfg_obj_t **maps,
	   const cfg_obj_t **objp)
{
446
	const char *zone = NULL;
447
	isc_result_t result;
448 449 450 451 452 453 454

	switch (ztype) {
	case dns_zone_slave: zone = "slave"; break;
	case dns_zone_master: zone = "master"; break;
	default:
		INSIST(0);
	}
455 456
	result = ns_checknames_get(maps, zone, objp);
	INSIST(result == ISC_R_SUCCESS);
457 458
}

459
isc_result_t
460 461 462
ns_zone_configure(const cfg_obj_t *config, const cfg_obj_t *vconfig,
		  const cfg_obj_t *zconfig, cfg_aclconfctx_t *ac,
		  dns_zone_t *zone)
463 464
{
	isc_result_t result;
465
	const char *zname;
466
	dns_rdataclass_t zclass;
467
	dns_rdataclass_t vclass;
468 469 470 471
	const cfg_obj_t *maps[5];
	const cfg_obj_t *zoptions = NULL;
	const cfg_obj_t *options = NULL;
	const cfg_obj_t *obj;
472
	const char *filename = NULL;
473
	dns_notifytype_t notifytype = dns_notifytype_yes;
474 475 476
	isc_sockaddr_t *addrs;
	dns_name_t **keynames;
	isc_uint32_t count;
477 478 479 480 481
	char *cpval;
	unsigned int dbargc;
	char **dbargv;
	static char default_dbtype[] = "rbt";
	isc_mem_t *mctx = dns_zone_getmctx(zone);
482
	dns_dialuptype_t dialup = dns_dialuptype_no;
483 484
	dns_zonetype_t ztype;
	int i;
485
	isc_int32_t journal_size;
486
	isc_boolean_t multi;
487 488
	isc_boolean_t alt;
	dns_view_t *view;
489
	isc_boolean_t check = ISC_FALSE, fail = ISC_FALSE;
490
	isc_boolean_t warn = ISC_FALSE, ignore = ISC_FALSE;
491
	isc_boolean_t ixfrdiff;
492
	dns_masterformat_t masterformat;
493
	isc_stats_t *zoneqrystats;
494
	isc_boolean_t zonestats_on;
495
	int seconds;
Michael Graff's avatar
Michael Graff committed
496

497 498 499 500 501 502 503 504 505 506 507 508 509 510
	i = 0;
	if (zconfig != NULL) {
		zoptions = cfg_tuple_get(zconfig, "options");
		maps[i++] = zoptions;
	}
	if (vconfig != NULL)
		maps[i++] = cfg_tuple_get(vconfig, "options");
	if (config != NULL) {
		(void)cfg_map_get(config, "options", &options);
		if (options != NULL)
			maps[i++] = options;
	}
	maps[i++] = ns_g_defaults;
	maps[i++] = NULL;
511 512 513 514 515 516 517

	if (vconfig != NULL)
		RETERR(ns_config_getclass(cfg_tuple_get(vconfig, "class"),
					  dns_rdataclass_in, &vclass));
	else
		vclass = dns_rdataclass_in;

518 519 520
	/*
	 * Configure values common to all zone types.
	 */
521

522
	zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
523

524 525
	RETERR(ns_config_getclass(cfg_tuple_get(zconfig, "class"),
				  vclass, &zclass));
526
	dns_zone_setclass(zone, zclass);
527

528 529 530 531 532 533
	ztype = zonetype_fromconfig(zoptions);
	dns_zone_settype(zone, ztype);

	obj = NULL;
	result = cfg_map_get(zoptions, "database", &obj);
	if (result == ISC_R_SUCCESS)
534
		cpval = isc_mem_strdup(mctx, cfg_obj_asstring(obj));
535
	else
536
		cpval = default_dbtype;
537 538 539 540 541 542 543 544 545 546

	if (cpval == NULL)
		return(ISC_R_NOMEMORY);

	result = strtoargv(mctx, cpval, &dbargc, &dbargv);
	if (result != ISC_R_SUCCESS && cpval != default_dbtype) {
		isc_mem_free(mctx, cpval);
		return (result);
	}

547 548 549 550 551
	/*
	 * ANSI C is strange here.  There is no logical reason why (char **)
	 * cannot be promoted automatically to (const char * const *) by the
	 * compiler w/o generating a warning.
	 */
552
	result = dns_zone_setdbtype(zone, dbargc, (const char * const *)dbargv);
553
	isc_mem_put(mctx, dbargv, dbargc * sizeof(*dbargv));
554 555 556 557
	if (cpval != default_dbtype)
		isc_mem_free(mctx, cpval);
	if (result != ISC_R_SUCCESS)
		return (result);
558

559 560 561 562
	obj = NULL;
	result = cfg_map_get(zoptions, "file", &obj);
	if (result == ISC_R_SUCCESS)
		filename = cfg_obj_asstring(obj);
563

564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	/*
	 * Unless we're using some alternative database, a master zone
	 * will be needing a master file.
	 */
	if (ztype == dns_zone_master && cpval == default_dbtype) {
		if (filename == NULL) {
			isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
				      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
				      "zone '%s': 'file' not specified",
				      zname);
			return (ISC_R_FAILURE);
		}

		if (!isc_file_exists(filename)) {
			isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
				      NS_LOGMODULE_SERVER, ISC_LOG_ERROR,
				      "zone '%s': master file not found",
				      zname);
			return (ISC_R_NOTFOUND);
		}
	}

586 587 588 589
	masterformat = dns_masterformat_text;
	obj = NULL;
	result= ns_config_get(maps, "masterfile-format", &obj);
	if (result == ISC_R_SUCCESS) {
590
		const char *masterformatstr = cfg_obj_asstring(obj);
591 592 593 594 595 596 597 598 599

		if (strcasecmp(masterformatstr, "text") == 0)
			masterformat = dns_masterformat_text;
		else if (strcasecmp(masterformatstr, "raw") == 0)
			masterformat = dns_masterformat_raw;
		else
			INSIST(0);
	}
	RETERR(dns_zone_setfile2(zone, filename, masterformat));
600

601 602 603 604 605
	obj = NULL;
	result = cfg_map_get(zoptions, "journal", &obj);
	if (result == ISC_R_SUCCESS)
		RETERR(dns_zone_setjournal(zone, cfg_obj_asstring(obj)));

606 607
	if (ztype == dns_zone_slave)
		RETERR(configure_zone_acl(zconfig, vconfig, config,
608
					  allow_notify, ac, zone,
609 610
					  dns_zone_setnotifyacl,
					  dns_zone_clearnotifyacl));
611 612 613
	/*
	 * XXXAG This probably does not make sense for stubs.
	 */
614
	RETERR(configure_zone_acl(zconfig, vconfig, config,
615
				  allow_query, ac, zone,
616 617 618
				  dns_zone_setqueryacl,
				  dns_zone_clearqueryacl));

619 620 621 622 623 624 625 626 627
	obj = NULL;
	result = ns_config_get(maps, "dialup", &obj);
	INSIST(result == ISC_R_SUCCESS);
	if (cfg_obj_isboolean(obj)) {
		if (cfg_obj_asboolean(obj))
			dialup = dns_dialuptype_yes;
		else
			dialup = dns_dialuptype_no;
	} else {
628
		const char *dialupstr = cfg_obj_asstring(obj);
629 630 631 632 633 634 635 636 637 638 639
		if (strcasecmp(dialupstr, "notify") == 0)
			dialup = dns_dialuptype_notify;
		else if (strcasecmp(dialupstr, "notify-passive") == 0)
			dialup = dns_dialuptype_notifypassive;
		else if (strcasecmp(dialupstr, "refresh") == 0)
			dialup = dns_dialuptype_refresh;
		else if (strcasecmp(dialupstr, "passive") == 0)
			dialup = dns_dialuptype_passive;
		else
			INSIST(0);
	}
640
	dns_zone_setdialup(zone, dialup);
641

642 643 644
	obj = NULL;
	result = ns_config_get(maps, "zone-statistics", &obj);
	INSIST(result == ISC_R_SUCCESS);
645 646 647
	zonestats_on = cfg_obj_asboolean(obj);
	zoneqrystats = NULL;
	if (zonestats_on) {
648 649
		RETERR(isc_stats_create(mctx, &zoneqrystats,
					dns_nsstatscounter_max));
650 651 652
	}
	dns_zone_setrequeststats(zone, zoneqrystats);
	if (zoneqrystats != NULL)
653
		isc_stats_detach(&zoneqrystats);
654

655 656 657 658 659
	/*
	 * Configure master functionality.  This applies
	 * to primary masters (type "master") and slaves
	 * acting as masters (type "slave"), but not to stubs.
	 */
660 661 662 663 664 665 666 667 668 669
	if (ztype != dns_zone_stub) {
		obj = NULL;
		result = ns_config_get(maps, "notify", &obj);
		INSIST(result == ISC_R_SUCCESS);
		if (cfg_obj_isboolean(obj)) {
			if (cfg_obj_asboolean(obj))
				notifytype = dns_notifytype_yes;
			else
				notifytype = dns_notifytype_no;
		} else {
670
			const char *notifystr = cfg_obj_asstring(obj);
671 672
			if (strcasecmp(notifystr, "explicit") == 0)
				notifytype = dns_notifytype_explicit;
673 674
			else if (strcasecmp(notifystr, "master-only") == 0)
				notifytype = dns_notifytype_masteronly;
675 676 677
			else
				INSIST(0);
		}
678
		dns_zone_setnotifytype(zone, notifytype);
679

680 681
		obj = NULL;
		result = ns_config_get(maps, "also-notify", &obj);
682
		if (result == ISC_R_SUCCESS) {
683
			isc_sockaddr_t *addrs = NULL;
684
			isc_uint32_t addrcount;
685 686 687 688 689 690
			result = ns_config_getiplist(config, obj, 0, mctx,
						     &addrs, &addrcount);
			if (result != ISC_R_SUCCESS)
				return (result);
			result = dns_zone_setalsonotify(zone, addrs,
							addrcount);
691
			ns_config_putiplist(mctx, &addrs, addrcount);
692 693 694
			if (result != ISC_R_SUCCESS)
				return (result);
		} else
695
			RETERR(dns_zone_setalsonotify(zone, NULL, 0));
696

697 698 699
		obj = NULL;
		result = ns_config_get(maps, "notify-source", &obj);
		INSIST(result == ISC_R_SUCCESS);
700
		RETERR(dns_zone_setnotifysrc4(zone, cfg_obj_assockaddr(obj)));
701
		ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
702

703 704 705
		obj = NULL;
		result = ns_config_get(maps, "notify-source-v6", &obj);
		INSIST(result == ISC_R_SUCCESS);
706
		RETERR(dns_zone_setnotifysrc6(zone, cfg_obj_assockaddr(obj)));
707
		ns_add_reserved_dispatch(ns_g_server, cfg_obj_assockaddr(obj));
708

709 710 711 712 713 714
		obj = NULL;
		result = ns_config_get(maps, "notify-to-soa", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setoption(zone, DNS_ZONEOPT_NOTIFYTOSOA,
				   cfg_obj_asboolean(obj));

715 716
		dns_zone_setisself(zone, ns_client_isself, NULL);

717
		RETERR(configure_zone_acl(zconfig, vconfig, config,
718
					  allow_transfer, ac, zone,
719 720
					  dns_zone_setxfracl,
					  dns_zone_clearxfracl));
721

722 723 724
		obj = NULL;
		result = ns_config_get(maps, "max-transfer-time-out", &obj);
		INSIST(result == ISC_R_SUCCESS);
725
		dns_zone_setmaxxfrout(zone, cfg_obj_asuint32(obj) * 60);
726

727 728 729
		obj = NULL;
		result = ns_config_get(maps, "max-transfer-idle-out", &obj);
		INSIST(result == ISC_R_SUCCESS);
730
		dns_zone_setidleout(zone, cfg_obj_asuint32(obj) * 60);
731 732

		obj = NULL;
733
		result =  ns_config_get(maps, "max-journal-size", &obj);
734 735 736 737 738 739 740 741 742 743 744 745 746
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setjournalsize(zone, -1);
		if (cfg_obj_isstring(obj)) {
			const char *str = cfg_obj_asstring(obj);
			INSIST(strcasecmp(str, "unlimited") == 0);
			journal_size = ISC_UINT32_MAX / 2;
		} else {
			isc_resourcevalue_t value;
			value = cfg_obj_asuint64(obj);
			if (value > ISC_UINT32_MAX / 2) {
				cfg_obj_log(obj, ns_g_lctx,
					    ISC_LOG_ERROR,
					    "'max-journal-size "
Andreas Gustafsson's avatar
Andreas Gustafsson committed
747 748
					    "%" ISC_PRINT_QUADFORMAT "d' "
					    "is too large",
749 750
					    value);
				RETERR(ISC_R_RANGE);
751
			}
752 753 754
			journal_size = (isc_uint32_t)value;
		}
		dns_zone_setjournalsize(zone, journal_size);
755 756 757 758

		obj = NULL;
		result = ns_config_get(maps, "ixfr-from-differences", &obj);
		INSIST(result == ISC_R_SUCCESS);
759 760 761 762 763 764 765 766 767 768 769
		if (cfg_obj_isboolean(obj))
			ixfrdiff = cfg_obj_asboolean(obj);
		else if (strcasecmp(cfg_obj_asstring(obj), "master") &&
			 ztype == dns_zone_master)
			ixfrdiff = ISC_TRUE;
		else if (strcasecmp(cfg_obj_asstring(obj), "slave") &&
			ztype == dns_zone_slave)
			ixfrdiff = ISC_TRUE;
		else
			ixfrdiff = ISC_FALSE;
		dns_zone_setoption(zone, DNS_ZONEOPT_IXFRFROMDIFFS, ixfrdiff);
770 771 772 773 774 775 776 777 778 779 780 781 782 783

		checknames(ztype, maps, &obj);
		INSIST(obj != NULL);
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			fail = ISC_FALSE;
			check = ISC_TRUE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			fail = check = ISC_TRUE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			fail = check = ISC_FALSE;
		} else
			INSIST(0);
		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKNAMES, check);
		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKNAMESFAIL, fail);
784 785 786 787 788 789

		obj = NULL;
		result = ns_config_get(maps, "notify-delay", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setnotifydelay(zone, cfg_obj_asuint32(obj));

790 791 792
		obj = NULL;
		result = ns_config_get(maps, "check-sibling", &obj);
		INSIST(result == ISC_R_SUCCESS);
Automatic Updater's avatar
Automatic Updater committed
793
		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKSIBLING,
794
				   cfg_obj_asboolean(obj));
795 796 797 798 799

		obj = NULL;
		result = ns_config_get(maps, "zero-no-soa-ttl", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setzeronosoattl(zone, cfg_obj_asboolean(obj));
800 801 802 803 804 805

		obj = NULL;
		result = ns_config_get(maps, "nsec3-test-zone", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setoption(zone, DNS_ZONEOPT_NSEC3TESTZONE,
				   cfg_obj_asboolean(obj));
806
	}
807

808 809 810 811
	/*
	 * Configure update-related options.  These apply to
	 * primary masters only.
	 */
812
	if (ztype == dns_zone_master) {
813
		dns_acl_t *updateacl;
814

815
		RETERR(configure_zone_acl(zconfig, vconfig, config,
816
					  allow_update, ac, zone,
817 818
					  dns_zone_setupdateacl,
					  dns_zone_clearupdateacl));
Automatic Updater's avatar
Automatic Updater committed
819

820 821 822 823 824 825
		updateacl = dns_zone_getupdateacl(zone);
		if (updateacl != NULL  && dns_acl_isinsecure(updateacl))
			isc_log_write(ns_g_lctx, DNS_LOGCATEGORY_SECURITY,
				      NS_LOGMODULE_SERVER, ISC_LOG_WARNING,
				      "zone '%s' allows updates by IP "
				      "address, which is insecure",
826
				      zname);
Automatic Updater's avatar
Automatic Updater committed
827

828
		RETERR(configure_zone_ssutable(zoptions, zone, zname));
829 830 831 832

		obj = NULL;
		result = ns_config_get(maps, "sig-validity-interval", &obj);
		INSIST(result == ISC_R_SUCCESS);
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852
		{
			const cfg_obj_t *validity, *resign;

			validity = cfg_tuple_get(obj, "validity");
			seconds = cfg_obj_asuint32(validity) * 86400;
			dns_zone_setsigvalidityinterval(zone, seconds);

			resign = cfg_tuple_get(obj, "re-sign");
			if (cfg_obj_isvoid(resign)) {
				seconds /= 4;
			} else {
				if (seconds > 7 * 86400)
					seconds = cfg_obj_asuint32(resign) *
							86400;
				else
					seconds = cfg_obj_asuint32(resign) *
							3600;
			}
			dns_zone_setsigresigninginterval(zone, seconds);
		}
853 854 855 856 857 858 859

		obj = NULL;
		result = ns_config_get(maps, "key-directory", &obj);
		if (result == ISC_R_SUCCESS) {
			filename = cfg_obj_asstring(obj);
			RETERR(dns_zone_setkeydirectory(zone, filename));
		}
860

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
		obj = NULL;
		result = ns_config_get(maps, "sig-signing-signatures", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setsignatures(zone, cfg_obj_asuint32(obj));

		obj = NULL;
		result = ns_config_get(maps, "sig-signing-nodes", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setnodes(zone, cfg_obj_asuint32(obj));

		obj = NULL;
		result = ns_config_get(maps, "sig-signing-type", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setprivatetype(zone, cfg_obj_asuint32(obj));

		obj = NULL;
		result = ns_config_get(maps, "update-check-ksk", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setoption(zone, DNS_ZONEOPT_UPDATECHECKKSK,
				   cfg_obj_asboolean(obj));

882
		obj = NULL;
883
		result = ns_config_get(maps, "dnssec-dnskey-kskonly", &obj);
884 885 886
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setoption(zone, DNS_ZONEOPT_DNSKEYKSKONLY,
				   cfg_obj_asboolean(obj));
887 888
	} else if (ztype == dns_zone_slave) {
		RETERR(configure_zone_acl(zconfig, vconfig, config,
889
					  allow_update_forwarding, ac, zone,
890 891 892 893 894 895 896 897
					  dns_zone_setforwardacl,
					  dns_zone_clearforwardacl));
	}

	/*%
	 * Primary master functionality.
	 */
	if (ztype == dns_zone_master) {
898 899 900
		isc_boolean_t allow = ISC_FALSE, maint = ISC_FALSE;
		isc_boolean_t create = ISC_FALSE;

901 902 903 904 905 906 907
		obj = NULL;
		result = ns_config_get(maps, "check-wildcard", &obj);
		if (result == ISC_R_SUCCESS)
			check = cfg_obj_asboolean(obj);
		else
			check = ISC_FALSE;
		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKWILDCARD, check);
908

909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
		obj = NULL;
		result = ns_config_get(maps, "check-dup-records", &obj);
		INSIST(obj != NULL);
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			fail = ISC_FALSE;
			check = ISC_TRUE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			fail = check = ISC_TRUE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			fail = check = ISC_FALSE;
		} else
			INSIST(0);
		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKDUPRR, check);
		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKDUPRRFAIL, fail);

924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
		obj = NULL;
		result = ns_config_get(maps, "check-mx", &obj);
		INSIST(obj != NULL);
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			fail = ISC_FALSE;
			check = ISC_TRUE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			fail = check = ISC_TRUE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			fail = check = ISC_FALSE;
		} else
			INSIST(0);
		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKMX, check);
		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKMXFAIL, fail);

		obj = NULL;
940
		result = ns_config_get(maps, "check-integrity", &obj);
941
		INSIST(obj != NULL);
Automatic Updater's avatar
Automatic Updater committed
942
		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKINTEGRITY,
943
				   cfg_obj_asboolean(obj));
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973

		obj = NULL;
		result = ns_config_get(maps, "check-mx-cname", &obj);
		INSIST(obj != NULL);
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			warn = ISC_TRUE;
			ignore = ISC_FALSE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			warn = ignore = ISC_FALSE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			warn = ignore = ISC_TRUE;
		} else
			INSIST(0);
		dns_zone_setoption(zone, DNS_ZONEOPT_WARNMXCNAME, warn);
		dns_zone_setoption(zone, DNS_ZONEOPT_IGNOREMXCNAME, ignore);

		obj = NULL;
		result = ns_config_get(maps, "check-srv-cname", &obj);
		INSIST(obj != NULL);
		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
			warn = ISC_TRUE;
			ignore = ISC_FALSE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
			warn = ignore = ISC_FALSE;
		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
			warn = ignore = ISC_TRUE;
		} else
			INSIST(0);
		dns_zone_setoption(zone, DNS_ZONEOPT_WARNSRVCNAME, warn);
		dns_zone_setoption(zone, DNS_ZONEOPT_IGNORESRVCNAME, ignore);
974 975

		obj = NULL;
976
		result = ns_config_get(maps, "dnssec-secure-to-insecure", &obj);
977 978 979
		INSIST(obj != NULL);
		dns_zone_setoption(zone, DNS_ZONEOPT_SECURETOINSECURE,
				   cfg_obj_asboolean(obj));
980

Automatic Updater's avatar
Automatic Updater committed
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
		obj = NULL;
		result = cfg_map_get(zoptions, "auto-dnssec", &obj);
		if (result == ISC_R_SUCCESS) {
			const char *arg = cfg_obj_asstring(obj);
			if (strcasecmp(arg, "allow") == 0)
				allow = ISC_TRUE;
			else if (strcasecmp(arg, "maintain") == 0)
				allow = maint = ISC_TRUE;
			else if (strcasecmp(arg, "create") == 0)
				allow = maint = create = ISC_TRUE;
			else if (strcasecmp(arg, "off") == 0)
				;
			else
				INSIST(0);
			dns_zone_setkeyopt(zone, DNS_ZONEKEY_ALLOW, allow);
			dns_zone_setkeyopt(zone, DNS_ZONEKEY_MAINTAIN, maint);
			dns_zone_setkeyopt(zone, DNS_ZONEKEY_CREATE, create);
		}
999
	}
1000

1001 1002 1003
	/*
	 * Configure slave functionality.
	 */
1004 1005 1006
	switch (ztype) {
	case dns_zone_slave:
	case dns_zone_stub:
1007
		count = 0;
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
		obj = NULL;
		result = cfg_map_get(zoptions, "masters", &obj);
		if (obj != NULL) {
			addrs = NULL;
			keynames = NULL;
			RETERR(ns_config_getipandkeylist(config, obj, mctx,
							 &addrs, &keynames,
							 &count));
			result = dns_zone_setmasterswithkeys(zone, addrs,
							     keynames, count);
			ns_config_putipandkeylist(mctx, &addrs, &keynames,
						  count);
1020
		} else
1021
			result = dns_zone_setmasters(zone, NULL, 0);
1022
		RETERR(result);
1023

1024 1025 1026 1027 1028 1029 1030 1031 1032
		multi = ISC_FALSE;
		if (count > 1) {
			obj = NULL;
			result = ns_config_get(maps, "multi-master", &obj);
			INSIST(result == ISC_R_SUCCESS);
			multi = cfg_obj_asboolean(obj);
		}
		dns_zone_setoption(zone, DNS_ZONEOPT_MULTIMASTER, multi);

1033 1034 1035
		obj = NULL;
		result = ns_config_get(maps, "max-transfer-time-in", &obj);
		INSIST(result == ISC_R_SUCCESS);
1036
		dns_zone_setmaxxfrin(zone, cfg_obj_asuint32(obj) * 60);
1037

1038 1039 1040
		obj = NULL;
		result = ns_config_get(maps, "max-transfer-idle-in", &obj);
		INSIST(result == ISC_R_SUCCESS);
1041
		dns_zone_setidlein(zone, cfg_obj_asuint32(obj) * 60);
1042

1043 1044 1045 1046
		obj = NULL;
		result = ns_config_get(maps, "max-refresh-time", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setmaxrefreshtime(zone, cfg_obj_asuint32(obj));
1047

1048 1049 1050 1051
		obj = NULL;
		result = ns_config_get(maps, "min-refresh-time", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setminrefreshtime(zone, cfg_obj_asuint32(obj));
1052

1053 1054 1055 1056
		obj = NULL;
		result = ns_config_get(maps, "max-retry-time", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setmaxretrytime(zone, cfg_obj_asuint32(obj));
1057

1058 1059 1060 1061
		obj = NULL;
		result = ns_config_get(maps, "min-retry-time", &obj);
		INSIST(result == ISC_R_SUCCESS);
		dns_zone_setminretrytime(zone, cfg_obj_asuint32(obj));
1062