Home | History | Annotate | Line # | Download | only in named
zoneconf.c revision 1.12
      1  1.10    rillig /*	$NetBSD: zoneconf.c,v 1.12 2022/09/23 12:15:21 christos Exp $	*/
      2   1.1  christos 
      3   1.1  christos /*
      4   1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5   1.1  christos  *
      6  1.12  christos  * SPDX-License-Identifier: MPL-2.0
      7  1.12  christos  *
      8   1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      9   1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10   1.9  christos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11   1.1  christos  *
     12   1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     13   1.1  christos  * information regarding copyright ownership.
     14   1.1  christos  */
     15   1.1  christos 
     16   1.3  christos #include <inttypes.h>
     17   1.3  christos #include <stdbool.h>
     18   1.3  christos 
     19   1.1  christos #include <isc/buffer.h>
     20   1.1  christos #include <isc/file.h>
     21   1.1  christos #include <isc/mem.h>
     22   1.1  christos #include <isc/print.h>
     23   1.1  christos #include <isc/stats.h>
     24   1.7  christos #include <isc/string.h> /* Required for HP/UX (and others?) */
     25   1.1  christos #include <isc/util.h>
     26   1.1  christos 
     27   1.1  christos #include <dns/acl.h>
     28   1.1  christos #include <dns/db.h>
     29   1.7  christos #include <dns/fixedname.h>
     30   1.1  christos #include <dns/ipkeylist.h>
     31   1.1  christos #include <dns/journal.h>
     32   1.7  christos #include <dns/kasp.h>
     33   1.1  christos #include <dns/log.h>
     34   1.7  christos #include <dns/masterdump.h>
     35   1.1  christos #include <dns/name.h>
     36   1.9  christos #include <dns/nsec3.h>
     37   1.1  christos #include <dns/rdata.h>
     38   1.7  christos #include <dns/rdatalist.h>
     39   1.7  christos #include <dns/rdataset.h>
     40   1.1  christos #include <dns/rdatatype.h>
     41   1.1  christos #include <dns/result.h>
     42   1.1  christos #include <dns/sdlz.h>
     43   1.1  christos #include <dns/ssu.h>
     44   1.1  christos #include <dns/stats.h>
     45   1.1  christos #include <dns/tsig.h>
     46   1.1  christos #include <dns/view.h>
     47   1.1  christos #include <dns/zone.h>
     48   1.1  christos 
     49   1.1  christos #include <ns/client.h>
     50   1.1  christos 
     51   1.1  christos #include <named/config.h>
     52   1.1  christos #include <named/globals.h>
     53   1.1  christos #include <named/log.h>
     54   1.1  christos #include <named/server.h>
     55   1.1  christos #include <named/zoneconf.h>
     56   1.1  christos 
     57   1.1  christos /* ACLs associated with zone */
     58   1.1  christos typedef enum {
     59   1.1  christos 	allow_notify,
     60   1.1  christos 	allow_query,
     61   1.1  christos 	allow_query_on,
     62   1.1  christos 	allow_transfer,
     63   1.1  christos 	allow_update,
     64   1.1  christos 	allow_update_forwarding
     65   1.1  christos } acl_type_t;
     66   1.1  christos 
     67   1.7  christos #define RETERR(x)                        \
     68   1.7  christos 	do {                             \
     69   1.7  christos 		isc_result_t _r = (x);   \
     70   1.7  christos 		if (_r != ISC_R_SUCCESS) \
     71   1.7  christos 			return ((_r));   \
     72  1.10    rillig 	} while (0)
     73   1.1  christos 
     74   1.7  christos #define CHECK(x)                             \
     75   1.7  christos 	do {                                 \
     76   1.7  christos 		result = (x);                \
     77   1.7  christos 		if (result != ISC_R_SUCCESS) \
     78   1.7  christos 			goto cleanup;        \
     79  1.10    rillig 	} while (0)
     80   1.1  christos 
     81   1.1  christos /*%
     82   1.1  christos  * Convenience function for configuring a single zone ACL.
     83   1.1  christos  */
     84   1.1  christos static isc_result_t
     85   1.1  christos configure_zone_acl(const cfg_obj_t *zconfig, const cfg_obj_t *vconfig,
     86   1.1  christos 		   const cfg_obj_t *config, acl_type_t acltype,
     87   1.1  christos 		   cfg_aclconfctx_t *actx, dns_zone_t *zone,
     88   1.1  christos 		   void (*setzacl)(dns_zone_t *, dns_acl_t *),
     89   1.7  christos 		   void (*clearzacl)(dns_zone_t *)) {
     90   1.1  christos 	isc_result_t result;
     91   1.7  christos 	const cfg_obj_t *maps[5] = { NULL, NULL, NULL, NULL, NULL };
     92   1.1  christos 	const cfg_obj_t *aclobj = NULL;
     93   1.1  christos 	int i = 0;
     94   1.1  christos 	dns_acl_t **aclp = NULL, *acl = NULL;
     95   1.1  christos 	const char *aclname;
     96   1.1  christos 	dns_view_t *view;
     97   1.1  christos 
     98   1.1  christos 	view = dns_zone_getview(zone);
     99   1.1  christos 
    100   1.1  christos 	switch (acltype) {
    101   1.7  christos 	case allow_notify:
    102   1.7  christos 		if (view != NULL) {
    103   1.1  christos 			aclp = &view->notifyacl;
    104   1.7  christos 		}
    105   1.1  christos 		aclname = "allow-notify";
    106   1.1  christos 		break;
    107   1.7  christos 	case allow_query:
    108   1.7  christos 		if (view != NULL) {
    109   1.1  christos 			aclp = &view->queryacl;
    110   1.7  christos 		}
    111   1.1  christos 		aclname = "allow-query";
    112   1.1  christos 		break;
    113   1.7  christos 	case allow_query_on:
    114   1.7  christos 		if (view != NULL) {
    115   1.1  christos 			aclp = &view->queryonacl;
    116   1.7  christos 		}
    117   1.1  christos 		aclname = "allow-query-on";
    118   1.1  christos 		break;
    119   1.7  christos 	case allow_transfer:
    120   1.7  christos 		if (view != NULL) {
    121   1.1  christos 			aclp = &view->transferacl;
    122   1.7  christos 		}
    123   1.1  christos 		aclname = "allow-transfer";
    124   1.1  christos 		break;
    125   1.7  christos 	case allow_update:
    126   1.7  christos 		if (view != NULL) {
    127   1.1  christos 			aclp = &view->updateacl;
    128   1.7  christos 		}
    129   1.1  christos 		aclname = "allow-update";
    130   1.1  christos 		break;
    131   1.7  christos 	case allow_update_forwarding:
    132   1.7  christos 		if (view != NULL) {
    133   1.1  christos 			aclp = &view->upfwdacl;
    134   1.7  christos 		}
    135   1.1  christos 		aclname = "allow-update-forwarding";
    136   1.1  christos 		break;
    137   1.7  christos 	default:
    138  1.12  christos 		UNREACHABLE();
    139   1.1  christos 	}
    140   1.1  christos 
    141   1.1  christos 	/* First check to see if ACL is defined within the zone */
    142   1.1  christos 	if (zconfig != NULL) {
    143   1.1  christos 		maps[0] = cfg_tuple_get(zconfig, "options");
    144   1.1  christos 		(void)named_config_get(maps, aclname, &aclobj);
    145   1.1  christos 		if (aclobj != NULL) {
    146   1.1  christos 			aclp = NULL;
    147   1.1  christos 			goto parse_acl;
    148   1.1  christos 		}
    149   1.1  christos 	}
    150   1.1  christos 
    151   1.1  christos 	/* Failing that, see if there's a default ACL already in the view */
    152   1.1  christos 	if (aclp != NULL && *aclp != NULL) {
    153   1.1  christos 		(*setzacl)(zone, *aclp);
    154   1.1  christos 		return (ISC_R_SUCCESS);
    155   1.1  christos 	}
    156   1.1  christos 
    157   1.1  christos 	/* Check for default ACLs that haven't been parsed yet */
    158   1.1  christos 	if (vconfig != NULL) {
    159   1.1  christos 		const cfg_obj_t *options = cfg_tuple_get(vconfig, "options");
    160   1.7  christos 		if (options != NULL) {
    161   1.1  christos 			maps[i++] = options;
    162   1.7  christos 		}
    163   1.1  christos 	}
    164   1.1  christos 	if (config != NULL) {
    165   1.1  christos 		const cfg_obj_t *options = NULL;
    166   1.1  christos 		(void)cfg_map_get(config, "options", &options);
    167   1.7  christos 		if (options != NULL) {
    168   1.1  christos 			maps[i++] = options;
    169   1.7  christos 		}
    170   1.1  christos 	}
    171   1.1  christos 	maps[i++] = named_g_defaults;
    172   1.1  christos 	maps[i] = NULL;
    173   1.1  christos 
    174   1.1  christos 	(void)named_config_get(maps, aclname, &aclobj);
    175   1.1  christos 	if (aclobj == NULL) {
    176   1.1  christos 		(*clearzacl)(zone);
    177   1.1  christos 		return (ISC_R_SUCCESS);
    178   1.1  christos 	}
    179   1.1  christos 
    180   1.1  christos parse_acl:
    181   1.1  christos 	result = cfg_acl_fromconfig(aclobj, config, named_g_lctx, actx,
    182   1.9  christos 				    named_g_mctx, 0, &acl);
    183   1.7  christos 	if (result != ISC_R_SUCCESS) {
    184   1.1  christos 		return (result);
    185   1.7  christos 	}
    186   1.1  christos 	(*setzacl)(zone, acl);
    187   1.1  christos 
    188   1.1  christos 	/* Set the view default now */
    189   1.7  christos 	if (aclp != NULL) {
    190   1.1  christos 		dns_acl_attach(acl, aclp);
    191   1.7  christos 	}
    192   1.1  christos 
    193   1.1  christos 	dns_acl_detach(&acl);
    194   1.1  christos 	return (ISC_R_SUCCESS);
    195   1.1  christos }
    196   1.1  christos 
    197   1.1  christos /*%
    198   1.1  christos  * Parse the zone update-policy statement.
    199   1.1  christos  */
    200   1.1  christos static isc_result_t
    201   1.1  christos configure_zone_ssutable(const cfg_obj_t *zconfig, dns_zone_t *zone,
    202   1.7  christos 			const char *zname) {
    203   1.1  christos 	const cfg_obj_t *updatepolicy = NULL;
    204   1.1  christos 	const cfg_listelt_t *element, *element2;
    205   1.1  christos 	dns_ssutable_t *table = NULL;
    206   1.1  christos 	isc_mem_t *mctx = dns_zone_getmctx(zone);
    207   1.3  christos 	bool autoddns = false;
    208   1.1  christos 	isc_result_t result;
    209   1.1  christos 
    210   1.1  christos 	(void)cfg_map_get(zconfig, "update-policy", &updatepolicy);
    211   1.1  christos 
    212   1.1  christos 	if (updatepolicy == NULL) {
    213   1.1  christos 		dns_zone_setssutable(zone, NULL);
    214   1.1  christos 		return (ISC_R_SUCCESS);
    215   1.1  christos 	}
    216   1.1  christos 
    217   1.1  christos 	if (cfg_obj_isstring(updatepolicy) &&
    218   1.7  christos 	    strcmp("local", cfg_obj_asstring(updatepolicy)) == 0)
    219   1.7  christos 	{
    220   1.3  christos 		autoddns = true;
    221   1.1  christos 		updatepolicy = NULL;
    222   1.1  christos 	}
    223   1.1  christos 
    224   1.1  christos 	result = dns_ssutable_create(mctx, &table);
    225   1.7  christos 	if (result != ISC_R_SUCCESS) {
    226   1.1  christos 		return (result);
    227   1.7  christos 	}
    228   1.1  christos 
    229   1.7  christos 	for (element = cfg_list_first(updatepolicy); element != NULL;
    230   1.1  christos 	     element = cfg_list_next(element))
    231   1.1  christos 	{
    232   1.1  christos 		const cfg_obj_t *stmt = cfg_listelt_value(element);
    233   1.1  christos 		const cfg_obj_t *mode = cfg_tuple_get(stmt, "mode");
    234   1.1  christos 		const cfg_obj_t *identity = cfg_tuple_get(stmt, "identity");
    235   1.1  christos 		const cfg_obj_t *matchtype = cfg_tuple_get(stmt, "matchtype");
    236   1.1  christos 		const cfg_obj_t *dname = cfg_tuple_get(stmt, "name");
    237   1.1  christos 		const cfg_obj_t *typelist = cfg_tuple_get(stmt, "types");
    238   1.1  christos 		const char *str;
    239   1.3  christos 		bool grant = false;
    240   1.3  christos 		bool usezone = false;
    241   1.3  christos 		dns_ssumatchtype_t mtype = dns_ssumatchtype_name;
    242   1.1  christos 		dns_fixedname_t fname, fident;
    243   1.1  christos 		isc_buffer_t b;
    244   1.1  christos 		dns_rdatatype_t *types;
    245   1.1  christos 		unsigned int i, n;
    246   1.1  christos 
    247   1.1  christos 		str = cfg_obj_asstring(mode);
    248   1.3  christos 		if (strcasecmp(str, "grant") == 0) {
    249   1.3  christos 			grant = true;
    250   1.3  christos 		} else if (strcasecmp(str, "deny") == 0) {
    251   1.3  christos 			grant = false;
    252   1.3  christos 		} else {
    253  1.12  christos 			UNREACHABLE();
    254   1.3  christos 		}
    255   1.1  christos 
    256   1.1  christos 		str = cfg_obj_asstring(matchtype);
    257   1.1  christos 		CHECK(dns_ssu_mtypefromstring(str, &mtype));
    258   1.9  christos 		if (mtype == dns_ssumatchtype_subdomain &&
    259   1.9  christos 		    strcasecmp(str, "zonesub") == 0) {
    260   1.3  christos 			usezone = true;
    261   1.1  christos 		}
    262   1.1  christos 
    263   1.1  christos 		dns_fixedname_init(&fident);
    264   1.1  christos 		str = cfg_obj_asstring(identity);
    265   1.1  christos 		isc_buffer_constinit(&b, str, strlen(str));
    266   1.1  christos 		isc_buffer_add(&b, strlen(str));
    267   1.1  christos 		result = dns_name_fromtext(dns_fixedname_name(&fident), &b,
    268   1.1  christos 					   dns_rootname, 0, NULL);
    269   1.1  christos 		if (result != ISC_R_SUCCESS) {
    270   1.1  christos 			cfg_obj_log(identity, named_g_lctx, ISC_LOG_ERROR,
    271   1.1  christos 				    "'%s' is not a valid name", str);
    272   1.1  christos 			goto cleanup;
    273   1.1  christos 		}
    274   1.1  christos 
    275   1.1  christos 		dns_fixedname_init(&fname);
    276   1.1  christos 		if (usezone) {
    277   1.6  christos 			dns_name_copynf(dns_zone_getorigin(zone),
    278   1.7  christos 					dns_fixedname_name(&fname));
    279   1.1  christos 		} else {
    280   1.1  christos 			str = cfg_obj_asstring(dname);
    281   1.1  christos 			isc_buffer_constinit(&b, str, strlen(str));
    282   1.1  christos 			isc_buffer_add(&b, strlen(str));
    283   1.1  christos 			result = dns_name_fromtext(dns_fixedname_name(&fname),
    284   1.1  christos 						   &b, dns_rootname, 0, NULL);
    285   1.1  christos 			if (result != ISC_R_SUCCESS) {
    286   1.1  christos 				cfg_obj_log(identity, named_g_lctx,
    287   1.1  christos 					    ISC_LOG_ERROR,
    288   1.1  christos 					    "'%s' is not a valid name", str);
    289   1.1  christos 				goto cleanup;
    290   1.1  christos 			}
    291   1.1  christos 		}
    292   1.1  christos 
    293   1.1  christos 		n = named_config_listcount(typelist);
    294   1.7  christos 		if (n == 0) {
    295   1.1  christos 			types = NULL;
    296   1.7  christos 		} else {
    297   1.1  christos 			types = isc_mem_get(mctx, n * sizeof(dns_rdatatype_t));
    298   1.1  christos 		}
    299   1.1  christos 
    300   1.1  christos 		i = 0;
    301   1.7  christos 		for (element2 = cfg_list_first(typelist); element2 != NULL;
    302   1.1  christos 		     element2 = cfg_list_next(element2))
    303   1.1  christos 		{
    304   1.1  christos 			const cfg_obj_t *typeobj;
    305   1.1  christos 			isc_textregion_t r;
    306   1.1  christos 
    307   1.1  christos 			INSIST(i < n);
    308   1.1  christos 
    309   1.1  christos 			typeobj = cfg_listelt_value(element2);
    310   1.1  christos 			str = cfg_obj_asstring(typeobj);
    311   1.1  christos 			DE_CONST(str, r.base);
    312   1.1  christos 			r.length = strlen(str);
    313   1.1  christos 
    314   1.1  christos 			result = dns_rdatatype_fromtext(&types[i++], &r);
    315   1.1  christos 			if (result != ISC_R_SUCCESS) {
    316   1.1  christos 				cfg_obj_log(identity, named_g_lctx,
    317   1.1  christos 					    ISC_LOG_ERROR,
    318   1.1  christos 					    "'%s' is not a valid type", str);
    319   1.1  christos 				isc_mem_put(mctx, types,
    320   1.1  christos 					    n * sizeof(dns_rdatatype_t));
    321   1.1  christos 				goto cleanup;
    322   1.1  christos 			}
    323   1.1  christos 		}
    324   1.1  christos 		INSIST(i == n);
    325   1.1  christos 
    326   1.7  christos 		result = dns_ssutable_addrule(
    327   1.7  christos 			table, grant, dns_fixedname_name(&fident), mtype,
    328   1.7  christos 			dns_fixedname_name(&fname), n, types);
    329   1.7  christos 		if (types != NULL) {
    330   1.1  christos 			isc_mem_put(mctx, types, n * sizeof(dns_rdatatype_t));
    331   1.7  christos 		}
    332   1.1  christos 		if (result != ISC_R_SUCCESS) {
    333   1.1  christos 			goto cleanup;
    334   1.1  christos 		}
    335   1.1  christos 	}
    336   1.1  christos 
    337   1.1  christos 	/*
    338   1.1  christos 	 * If "update-policy local;" and a session key exists,
    339   1.1  christos 	 * then use the default policy, which is equivalent to:
    340   1.1  christos 	 * update-policy { grant <session-keyname> zonesub any; };
    341   1.1  christos 	 */
    342   1.1  christos 	if (autoddns) {
    343   1.1  christos 		dns_rdatatype_t any = dns_rdatatype_any;
    344   1.1  christos 
    345   1.1  christos 		if (named_g_server->session_keyname == NULL) {
    346   1.1  christos 			isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
    347   1.1  christos 				      NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,
    348   1.1  christos 				      "failed to enable auto DDNS policy "
    349   1.1  christos 				      "for zone %s: session key not found",
    350   1.1  christos 				      zname);
    351   1.1  christos 			result = ISC_R_NOTFOUND;
    352   1.1  christos 			goto cleanup;
    353   1.1  christos 		}
    354   1.1  christos 
    355   1.7  christos 		result = dns_ssutable_addrule(
    356   1.7  christos 			table, true, named_g_server->session_keyname,
    357   1.7  christos 			dns_ssumatchtype_local, dns_zone_getorigin(zone), 1,
    358   1.7  christos 			&any);
    359   1.1  christos 
    360   1.7  christos 		if (result != ISC_R_SUCCESS) {
    361   1.1  christos 			goto cleanup;
    362   1.7  christos 		}
    363   1.1  christos 	}
    364   1.1  christos 
    365   1.1  christos 	result = ISC_R_SUCCESS;
    366   1.1  christos 	dns_zone_setssutable(zone, table);
    367   1.1  christos 
    368   1.7  christos cleanup:
    369   1.1  christos 	dns_ssutable_detach(&table);
    370   1.1  christos 	return (result);
    371   1.1  christos }
    372   1.1  christos 
    373   1.1  christos /*
    374   1.1  christos  * This is the TTL used for internally generated RRsets for static-stub zones.
    375   1.1  christos  * The value doesn't matter because the mapping is static, but needs to be
    376   1.1  christos  * defined for the sake of implementation.
    377   1.1  christos  */
    378   1.1  christos #define STATICSTUB_SERVER_TTL 86400
    379   1.1  christos 
    380   1.1  christos /*%
    381   1.1  christos  * Configure an apex NS with glues for a static-stub zone.
    382   1.1  christos  * For example, for the zone named "example.com", the following RRs will be
    383   1.1  christos  * added to the zone DB:
    384   1.1  christos  * example.com. NS example.com.
    385   1.1  christos  * example.com. A 192.0.2.1
    386   1.1  christos  * example.com. AAAA 2001:db8::1
    387   1.1  christos  */
    388   1.1  christos static isc_result_t
    389   1.1  christos configure_staticstub_serveraddrs(const cfg_obj_t *zconfig, dns_zone_t *zone,
    390   1.1  christos 				 dns_rdatalist_t *rdatalist_ns,
    391   1.1  christos 				 dns_rdatalist_t *rdatalist_a,
    392   1.7  christos 				 dns_rdatalist_t *rdatalist_aaaa) {
    393   1.1  christos 	const cfg_listelt_t *element;
    394   1.1  christos 	isc_mem_t *mctx = dns_zone_getmctx(zone);
    395   1.1  christos 	isc_region_t region, sregion;
    396   1.1  christos 	dns_rdata_t *rdata;
    397   1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
    398   1.1  christos 
    399   1.7  christos 	for (element = cfg_list_first(zconfig); element != NULL;
    400   1.1  christos 	     element = cfg_list_next(element))
    401   1.1  christos 	{
    402   1.7  christos 		const isc_sockaddr_t *sa;
    403   1.1  christos 		isc_netaddr_t na;
    404   1.1  christos 		const cfg_obj_t *address = cfg_listelt_value(element);
    405   1.1  christos 		dns_rdatalist_t *rdatalist;
    406   1.1  christos 
    407   1.1  christos 		sa = cfg_obj_assockaddr(address);
    408   1.1  christos 		if (isc_sockaddr_getport(sa) != 0) {
    409   1.1  christos 			cfg_obj_log(zconfig, named_g_lctx, ISC_LOG_ERROR,
    410   1.1  christos 				    "port is not configurable for "
    411   1.1  christos 				    "static stub server-addresses");
    412   1.1  christos 			return (ISC_R_FAILURE);
    413   1.1  christos 		}
    414   1.1  christos 		isc_netaddr_fromsockaddr(&na, sa);
    415   1.1  christos 		if (isc_netaddr_getzone(&na) != 0) {
    416   1.1  christos 			cfg_obj_log(zconfig, named_g_lctx, ISC_LOG_ERROR,
    417   1.7  christos 				    "scoped address is not allowed "
    418   1.7  christos 				    "for static stub "
    419   1.7  christos 				    "server-addresses");
    420   1.1  christos 			return (ISC_R_FAILURE);
    421   1.1  christos 		}
    422   1.1  christos 
    423   1.1  christos 		switch (na.family) {
    424   1.1  christos 		case AF_INET:
    425   1.1  christos 			region.length = sizeof(na.type.in);
    426   1.1  christos 			rdatalist = rdatalist_a;
    427   1.1  christos 			break;
    428   1.1  christos 		default:
    429   1.1  christos 			INSIST(na.family == AF_INET6);
    430   1.1  christos 			region.length = sizeof(na.type.in6);
    431   1.1  christos 			rdatalist = rdatalist_aaaa;
    432   1.1  christos 			break;
    433   1.1  christos 		}
    434   1.1  christos 
    435   1.1  christos 		rdata = isc_mem_get(mctx, sizeof(*rdata) + region.length);
    436   1.1  christos 		region.base = (unsigned char *)(rdata + 1);
    437   1.1  christos 		memmove(region.base, &na.type, region.length);
    438   1.1  christos 		dns_rdata_init(rdata);
    439   1.1  christos 		dns_rdata_fromregion(rdata, dns_zone_getclass(zone),
    440   1.1  christos 				     rdatalist->type, &region);
    441   1.1  christos 		ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
    442   1.1  christos 	}
    443   1.1  christos 
    444   1.1  christos 	/*
    445   1.1  christos 	 * If no address is specified (unlikely in this context, but possible),
    446   1.1  christos 	 * there's nothing to do anymore.
    447   1.1  christos 	 */
    448   1.1  christos 	if (ISC_LIST_EMPTY(rdatalist_a->rdata) &&
    449   1.1  christos 	    ISC_LIST_EMPTY(rdatalist_aaaa->rdata)) {
    450   1.1  christos 		return (ISC_R_SUCCESS);
    451   1.1  christos 	}
    452   1.1  christos 
    453   1.1  christos 	/* Add to the list an apex NS with the ns name being the origin name */
    454   1.1  christos 	dns_name_toregion(dns_zone_getorigin(zone), &sregion);
    455   1.1  christos 	rdata = isc_mem_get(mctx, sizeof(*rdata) + sregion.length);
    456   1.1  christos 	region.length = sregion.length;
    457   1.1  christos 	region.base = (unsigned char *)(rdata + 1);
    458   1.1  christos 	memmove(region.base, sregion.base, region.length);
    459   1.1  christos 	dns_rdata_init(rdata);
    460   1.7  christos 	dns_rdata_fromregion(rdata, dns_zone_getclass(zone), dns_rdatatype_ns,
    461   1.7  christos 			     &region);
    462   1.1  christos 	ISC_LIST_APPEND(rdatalist_ns->rdata, rdata, link);
    463   1.1  christos 
    464   1.1  christos 	return (result);
    465   1.1  christos }
    466   1.1  christos 
    467   1.1  christos /*%
    468   1.1  christos  * Configure an apex NS with an out-of-zone NS names for a static-stub zone.
    469   1.1  christos  * For example, for the zone named "example.com", something like the following
    470   1.1  christos  * RRs will be added to the zone DB:
    471   1.1  christos  * example.com. NS ns.example.net.
    472   1.1  christos  */
    473   1.1  christos static isc_result_t
    474   1.1  christos configure_staticstub_servernames(const cfg_obj_t *zconfig, dns_zone_t *zone,
    475   1.7  christos 				 dns_rdatalist_t *rdatalist,
    476   1.7  christos 				 const char *zname) {
    477   1.1  christos 	const cfg_listelt_t *element;
    478   1.1  christos 	isc_mem_t *mctx = dns_zone_getmctx(zone);
    479   1.1  christos 	dns_rdata_t *rdata;
    480   1.1  christos 	isc_region_t sregion, region;
    481   1.1  christos 	isc_result_t result = ISC_R_SUCCESS;
    482   1.1  christos 
    483   1.7  christos 	for (element = cfg_list_first(zconfig); element != NULL;
    484   1.1  christos 	     element = cfg_list_next(element))
    485   1.1  christos 	{
    486   1.1  christos 		const cfg_obj_t *obj;
    487   1.1  christos 		const char *str;
    488   1.1  christos 		dns_fixedname_t fixed_name;
    489   1.1  christos 		dns_name_t *nsname;
    490   1.1  christos 		isc_buffer_t b;
    491   1.1  christos 
    492   1.1  christos 		obj = cfg_listelt_value(element);
    493   1.1  christos 		str = cfg_obj_asstring(obj);
    494   1.1  christos 
    495   1.1  christos 		nsname = dns_fixedname_initname(&fixed_name);
    496   1.1  christos 
    497   1.1  christos 		isc_buffer_constinit(&b, str, strlen(str));
    498   1.1  christos 		isc_buffer_add(&b, strlen(str));
    499   1.1  christos 		result = dns_name_fromtext(nsname, &b, dns_rootname, 0, NULL);
    500   1.1  christos 		if (result != ISC_R_SUCCESS) {
    501   1.1  christos 			cfg_obj_log(zconfig, named_g_lctx, ISC_LOG_ERROR,
    502   1.7  christos 				    "server-name '%s' is not a valid "
    503   1.7  christos 				    "name",
    504   1.7  christos 				    str);
    505   1.1  christos 			return (result);
    506   1.1  christos 		}
    507   1.1  christos 		if (dns_name_issubdomain(nsname, dns_zone_getorigin(zone))) {
    508   1.1  christos 			cfg_obj_log(zconfig, named_g_lctx, ISC_LOG_ERROR,
    509   1.1  christos 				    "server-name '%s' must not be a "
    510   1.1  christos 				    "subdomain of zone name '%s'",
    511   1.1  christos 				    str, zname);
    512   1.1  christos 			return (ISC_R_FAILURE);
    513   1.1  christos 		}
    514   1.1  christos 
    515   1.1  christos 		dns_name_toregion(nsname, &sregion);
    516   1.1  christos 		rdata = isc_mem_get(mctx, sizeof(*rdata) + sregion.length);
    517   1.1  christos 		region.length = sregion.length;
    518   1.1  christos 		region.base = (unsigned char *)(rdata + 1);
    519   1.1  christos 		memmove(region.base, sregion.base, region.length);
    520   1.1  christos 		dns_rdata_init(rdata);
    521   1.1  christos 		dns_rdata_fromregion(rdata, dns_zone_getclass(zone),
    522   1.1  christos 				     dns_rdatatype_ns, &region);
    523   1.1  christos 		ISC_LIST_APPEND(rdatalist->rdata, rdata, link);
    524   1.1  christos 	}
    525   1.1  christos 
    526   1.1  christos 	return (result);
    527   1.1  christos }
    528   1.1  christos 
    529   1.1  christos /*%
    530   1.1  christos  * Configure static-stub zone.
    531   1.1  christos  */
    532   1.1  christos static isc_result_t
    533   1.1  christos configure_staticstub(const cfg_obj_t *zconfig, dns_zone_t *zone,
    534   1.7  christos 		     const char *zname, const char *dbtype) {
    535   1.1  christos 	int i = 0;
    536   1.1  christos 	const cfg_obj_t *obj;
    537   1.1  christos 	isc_mem_t *mctx = dns_zone_getmctx(zone);
    538   1.1  christos 	dns_db_t *db = NULL;
    539   1.1  christos 	dns_dbversion_t *dbversion = NULL;
    540   1.1  christos 	dns_dbnode_t *apexnode = NULL;
    541   1.1  christos 	dns_name_t apexname;
    542   1.1  christos 	isc_result_t result;
    543   1.1  christos 	dns_rdataset_t rdataset;
    544   1.1  christos 	dns_rdatalist_t rdatalist_ns, rdatalist_a, rdatalist_aaaa;
    545   1.7  christos 	dns_rdatalist_t *rdatalists[] = { &rdatalist_ns, &rdatalist_a,
    546   1.7  christos 					  &rdatalist_aaaa, NULL };
    547   1.1  christos 	dns_rdata_t *rdata;
    548   1.1  christos 	isc_region_t region;
    549   1.1  christos 
    550   1.1  christos 	/* Create the DB beforehand */
    551   1.1  christos 	RETERR(dns_db_create(mctx, dbtype, dns_zone_getorigin(zone),
    552   1.7  christos 			     dns_dbtype_stub, dns_zone_getclass(zone), 0, NULL,
    553   1.7  christos 			     &db));
    554   1.5  christos 
    555   1.5  christos 	dns_rdataset_init(&rdataset);
    556   1.1  christos 
    557   1.1  christos 	dns_rdatalist_init(&rdatalist_ns);
    558   1.1  christos 	rdatalist_ns.rdclass = dns_zone_getclass(zone);
    559   1.1  christos 	rdatalist_ns.type = dns_rdatatype_ns;
    560   1.1  christos 	rdatalist_ns.ttl = STATICSTUB_SERVER_TTL;
    561   1.1  christos 
    562   1.1  christos 	dns_rdatalist_init(&rdatalist_a);
    563   1.1  christos 	rdatalist_a.rdclass = dns_zone_getclass(zone);
    564   1.1  christos 	rdatalist_a.type = dns_rdatatype_a;
    565   1.1  christos 	rdatalist_a.ttl = STATICSTUB_SERVER_TTL;
    566   1.1  christos 
    567   1.1  christos 	dns_rdatalist_init(&rdatalist_aaaa);
    568   1.1  christos 	rdatalist_aaaa.rdclass = dns_zone_getclass(zone);
    569   1.1  christos 	rdatalist_aaaa.type = dns_rdatatype_aaaa;
    570   1.1  christos 	rdatalist_aaaa.ttl = STATICSTUB_SERVER_TTL;
    571   1.1  christos 
    572   1.1  christos 	/* Prepare zone RRs from the configuration */
    573   1.1  christos 	obj = NULL;
    574   1.1  christos 	result = cfg_map_get(zconfig, "server-addresses", &obj);
    575   1.1  christos 	if (result == ISC_R_SUCCESS) {
    576   1.1  christos 		INSIST(obj != NULL);
    577   1.7  christos 		CHECK(configure_staticstub_serveraddrs(obj, zone, &rdatalist_ns,
    578   1.5  christos 						       &rdatalist_a,
    579   1.5  christos 						       &rdatalist_aaaa));
    580   1.1  christos 	}
    581   1.1  christos 
    582   1.1  christos 	obj = NULL;
    583   1.1  christos 	result = cfg_map_get(zconfig, "server-names", &obj);
    584   1.1  christos 	if (result == ISC_R_SUCCESS) {
    585   1.1  christos 		INSIST(obj != NULL);
    586   1.7  christos 		CHECK(configure_staticstub_servernames(obj, zone, &rdatalist_ns,
    587   1.5  christos 						       zname));
    588   1.1  christos 	}
    589   1.1  christos 
    590   1.1  christos 	/*
    591   1.1  christos 	 * Sanity check: there should be at least one NS RR at the zone apex
    592   1.1  christos 	 * to trigger delegation.
    593   1.1  christos 	 */
    594   1.1  christos 	if (ISC_LIST_EMPTY(rdatalist_ns.rdata)) {
    595   1.1  christos 		isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
    596   1.1  christos 			      NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,
    597   1.1  christos 			      "No NS record is configured for a "
    598   1.7  christos 			      "static-stub zone '%s'",
    599   1.7  christos 			      zname);
    600   1.1  christos 		result = ISC_R_FAILURE;
    601   1.1  christos 		goto cleanup;
    602   1.1  christos 	}
    603   1.1  christos 
    604   1.1  christos 	/*
    605   1.1  christos 	 * Now add NS and glue A/AAAA RRsets to the zone DB.
    606   1.1  christos 	 * First open a new version for the add operation and get a pointer
    607   1.1  christos 	 * to the apex node (all RRs are of the apex name).
    608   1.1  christos 	 */
    609   1.5  christos 	CHECK(dns_db_newversion(db, &dbversion));
    610   1.5  christos 
    611   1.1  christos 	dns_name_init(&apexname, NULL);
    612   1.1  christos 	dns_name_clone(dns_zone_getorigin(zone), &apexname);
    613   1.5  christos 	CHECK(dns_db_findnode(db, &apexname, false, &apexnode));
    614   1.1  christos 
    615   1.1  christos 	/* Add NS RRset */
    616   1.7  christos 	RUNTIME_CHECK(dns_rdatalist_tordataset(&rdatalist_ns, &rdataset) ==
    617   1.7  christos 		      ISC_R_SUCCESS);
    618   1.7  christos 	CHECK(dns_db_addrdataset(db, apexnode, dbversion, 0, &rdataset, 0,
    619   1.7  christos 				 NULL));
    620   1.1  christos 	dns_rdataset_disassociate(&rdataset);
    621   1.1  christos 
    622   1.1  christos 	/* Add glue A RRset, if any */
    623   1.1  christos 	if (!ISC_LIST_EMPTY(rdatalist_a.rdata)) {
    624   1.7  christos 		RUNTIME_CHECK(
    625   1.7  christos 			dns_rdatalist_tordataset(&rdatalist_a, &rdataset) ==
    626   1.7  christos 			ISC_R_SUCCESS);
    627   1.7  christos 		CHECK(dns_db_addrdataset(db, apexnode, dbversion, 0, &rdataset,
    628   1.7  christos 					 0, NULL));
    629   1.1  christos 		dns_rdataset_disassociate(&rdataset);
    630   1.1  christos 	}
    631   1.1  christos 
    632   1.1  christos 	/* Add glue AAAA RRset, if any */
    633   1.1  christos 	if (!ISC_LIST_EMPTY(rdatalist_aaaa.rdata)) {
    634   1.7  christos 		RUNTIME_CHECK(
    635   1.7  christos 			dns_rdatalist_tordataset(&rdatalist_aaaa, &rdataset) ==
    636   1.7  christos 			ISC_R_SUCCESS);
    637   1.7  christos 		CHECK(dns_db_addrdataset(db, apexnode, dbversion, 0, &rdataset,
    638   1.7  christos 					 0, NULL));
    639   1.1  christos 		dns_rdataset_disassociate(&rdataset);
    640   1.1  christos 	}
    641   1.1  christos 
    642   1.5  christos 	dns_db_closeversion(db, &dbversion, true);
    643   1.5  christos 	dns_zone_setdb(zone, db);
    644   1.5  christos 
    645   1.1  christos 	result = ISC_R_SUCCESS;
    646   1.1  christos 
    647   1.7  christos cleanup:
    648   1.5  christos 	if (dns_rdataset_isassociated(&rdataset)) {
    649   1.5  christos 		dns_rdataset_disassociate(&rdataset);
    650   1.5  christos 	}
    651   1.5  christos 	if (apexnode != NULL) {
    652   1.1  christos 		dns_db_detachnode(db, &apexnode);
    653   1.5  christos 	}
    654   1.5  christos 	if (dbversion != NULL) {
    655   1.5  christos 		dns_db_closeversion(db, &dbversion, false);
    656   1.5  christos 	}
    657   1.5  christos 	if (db != NULL) {
    658   1.1  christos 		dns_db_detach(&db);
    659   1.5  christos 	}
    660   1.1  christos 	for (i = 0; rdatalists[i] != NULL; i++) {
    661   1.1  christos 		while ((rdata = ISC_LIST_HEAD(rdatalists[i]->rdata)) != NULL) {
    662   1.1  christos 			ISC_LIST_UNLINK(rdatalists[i]->rdata, rdata, link);
    663   1.1  christos 			dns_rdata_toregion(rdata, &region);
    664   1.1  christos 			isc_mem_put(mctx, rdata,
    665   1.1  christos 				    sizeof(*rdata) + region.length);
    666   1.1  christos 		}
    667   1.1  christos 	}
    668   1.1  christos 
    669   1.1  christos 	INSIST(dbversion == NULL);
    670   1.1  christos 
    671   1.1  christos 	return (result);
    672   1.1  christos }
    673   1.1  christos 
    674   1.1  christos /*%
    675   1.1  christos  * Convert a config file zone type into a server zone type.
    676   1.1  christos  */
    677  1.12  christos static dns_zonetype_t
    678   1.1  christos zonetype_fromconfig(const cfg_obj_t *map) {
    679   1.1  christos 	const cfg_obj_t *obj = NULL;
    680   1.1  christos 	isc_result_t result;
    681   1.1  christos 
    682   1.1  christos 	result = cfg_map_get(map, "type", &obj);
    683   1.1  christos 	INSIST(result == ISC_R_SUCCESS && obj != NULL);
    684   1.1  christos 	return (named_config_getzonetype(obj));
    685   1.1  christos }
    686   1.1  christos 
    687   1.1  christos /*%
    688   1.1  christos  * Helper function for strtoargv().  Pardon the gratuitous recursion.
    689   1.1  christos  */
    690   1.1  christos static isc_result_t
    691   1.7  christos strtoargvsub(isc_mem_t *mctx, char *s, unsigned int *argcp, char ***argvp,
    692   1.7  christos 	     unsigned int n) {
    693   1.1  christos 	isc_result_t result;
    694   1.1  christos 
    695   1.1  christos 	/* Discard leading whitespace. */
    696   1.7  christos 	while (*s == ' ' || *s == '\t') {
    697   1.1  christos 		s++;
    698   1.7  christos 	}
    699   1.1  christos 
    700   1.1  christos 	if (*s == '\0') {
    701   1.1  christos 		/* We have reached the end of the string. */
    702   1.1  christos 		*argcp = n;
    703   1.1  christos 		*argvp = isc_mem_get(mctx, n * sizeof(char *));
    704   1.1  christos 	} else {
    705   1.1  christos 		char *p = s;
    706   1.7  christos 		while (*p != ' ' && *p != '\t' && *p != '\0') {
    707   1.1  christos 			p++;
    708   1.7  christos 		}
    709   1.7  christos 		if (*p != '\0') {
    710   1.1  christos 			*p++ = '\0';
    711   1.7  christos 		}
    712   1.1  christos 
    713   1.1  christos 		result = strtoargvsub(mctx, p, argcp, argvp, n + 1);
    714   1.7  christos 		if (result != ISC_R_SUCCESS) {
    715   1.1  christos 			return (result);
    716   1.7  christos 		}
    717   1.1  christos 		(*argvp)[n] = s;
    718   1.1  christos 	}
    719   1.1  christos 	return (ISC_R_SUCCESS);
    720   1.1  christos }
    721   1.1  christos 
    722   1.1  christos /*%
    723   1.1  christos  * Tokenize the string "s" into whitespace-separated words,
    724   1.1  christos  * return the number of words in '*argcp' and an array
    725   1.1  christos  * of pointers to the words in '*argvp'.  The caller
    726   1.1  christos  * must free the array using isc_mem_put().  The string
    727   1.1  christos  * is modified in-place.
    728   1.1  christos  */
    729   1.1  christos static isc_result_t
    730   1.1  christos strtoargv(isc_mem_t *mctx, char *s, unsigned int *argcp, char ***argvp) {
    731   1.1  christos 	return (strtoargvsub(mctx, s, argcp, argvp, 0));
    732   1.1  christos }
    733   1.1  christos 
    734  1.12  christos static const char *const primary_synonyms[] = { "primary", "master", NULL };
    735  1.12  christos 
    736  1.12  christos static const char *const secondary_synonyms[] = { "secondary", "slave", NULL };
    737  1.12  christos 
    738   1.1  christos static void
    739   1.1  christos checknames(dns_zonetype_t ztype, const cfg_obj_t **maps,
    740   1.7  christos 	   const cfg_obj_t **objp) {
    741   1.1  christos 	isc_result_t result;
    742   1.1  christos 
    743   1.1  christos 	switch (ztype) {
    744  1.12  christos 	case dns_zone_secondary:
    745   1.3  christos 	case dns_zone_mirror:
    746  1.12  christos 		result = named_checknames_get(maps, secondary_synonyms, objp);
    747   1.3  christos 		break;
    748  1.12  christos 	case dns_zone_primary:
    749  1.12  christos 		result = named_checknames_get(maps, primary_synonyms, objp);
    750   1.3  christos 		break;
    751   1.1  christos 	default:
    752  1.12  christos 		UNREACHABLE();
    753   1.1  christos 	}
    754   1.8  christos 
    755   1.1  christos 	INSIST(result == ISC_R_SUCCESS && objp != NULL && *objp != NULL);
    756   1.1  christos }
    757   1.1  christos 
    758   1.1  christos /*
    759   1.1  christos  * Callback to see if a non-recursive query coming from 'srcaddr' to
    760   1.1  christos  * 'destaddr', with optional key 'mykey' for class 'rdclass' would be
    761   1.1  christos  * delivered to 'myview'.
    762   1.1  christos  *
    763   1.1  christos  * We run this unlocked as both the view list and the interface list
    764   1.1  christos  * are updated when the appropriate task has exclusivity.
    765   1.1  christos  */
    766   1.3  christos static bool
    767   1.7  christos isself(dns_view_t *myview, dns_tsigkey_t *mykey, const isc_sockaddr_t *srcaddr,
    768   1.7  christos        const isc_sockaddr_t *dstaddr, dns_rdataclass_t rdclass, void *arg) {
    769   1.7  christos 	ns_interfacemgr_t *interfacemgr = (ns_interfacemgr_t *)arg;
    770   1.1  christos 	dns_aclenv_t *env = ns_interfacemgr_getaclenv(interfacemgr);
    771   1.1  christos 	dns_view_t *view;
    772   1.1  christos 	dns_tsigkey_t *key = NULL;
    773   1.1  christos 	isc_netaddr_t netsrc;
    774   1.1  christos 	isc_netaddr_t netdst;
    775   1.1  christos 
    776   1.7  christos 	if (interfacemgr == NULL) {
    777   1.3  christos 		return (true);
    778   1.7  christos 	}
    779   1.1  christos 
    780   1.7  christos 	if (!ns_interfacemgr_listeningon(interfacemgr, dstaddr)) {
    781   1.3  christos 		return (false);
    782   1.7  christos 	}
    783   1.1  christos 
    784   1.1  christos 	isc_netaddr_fromsockaddr(&netsrc, srcaddr);
    785   1.1  christos 	isc_netaddr_fromsockaddr(&netdst, dstaddr);
    786   1.1  christos 
    787   1.7  christos 	for (view = ISC_LIST_HEAD(named_g_server->viewlist); view != NULL;
    788   1.6  christos 	     view = ISC_LIST_NEXT(view, link))
    789   1.6  christos 	{
    790   1.6  christos 		const dns_name_t *tsig = NULL;
    791   1.1  christos 
    792   1.7  christos 		if (view->matchrecursiveonly) {
    793   1.1  christos 			continue;
    794   1.7  christos 		}
    795   1.1  christos 
    796   1.7  christos 		if (rdclass != view->rdclass) {
    797   1.1  christos 			continue;
    798   1.7  christos 		}
    799   1.1  christos 
    800   1.1  christos 		if (mykey != NULL) {
    801   1.3  christos 			bool match;
    802   1.1  christos 			isc_result_t result;
    803   1.1  christos 
    804   1.1  christos 			result = dns_view_gettsig(view, &mykey->name, &key);
    805   1.7  christos 			if (result != ISC_R_SUCCESS) {
    806   1.1  christos 				continue;
    807   1.7  christos 			}
    808   1.1  christos 			match = dst_key_compare(mykey->key, key->key);
    809   1.1  christos 			dns_tsigkey_detach(&key);
    810   1.7  christos 			if (!match) {
    811   1.1  christos 				continue;
    812   1.7  christos 			}
    813   1.1  christos 			tsig = dns_tsigkey_identity(mykey);
    814   1.1  christos 		}
    815   1.1  christos 
    816   1.3  christos 		if (dns_acl_allowed(&netsrc, tsig, view->matchclients, env) &&
    817   1.3  christos 		    dns_acl_allowed(&netdst, tsig, view->matchdestinations,
    818   1.3  christos 				    env))
    819   1.3  christos 		{
    820   1.1  christos 			break;
    821   1.3  christos 		}
    822   1.1  christos 	}
    823   1.3  christos 	return (view == myview);
    824   1.1  christos }
    825   1.1  christos 
    826   1.3  christos /*%
    827   1.3  christos  * For mirror zones, change "notify yes;" to "notify explicit;", informing the
    828   1.3  christos  * user only if "notify" was explicitly configured rather than inherited from
    829   1.3  christos  * default configuration.
    830   1.3  christos  */
    831   1.3  christos static dns_notifytype_t
    832   1.3  christos process_notifytype(dns_notifytype_t ntype, dns_zonetype_t ztype,
    833   1.7  christos 		   const char *zname, const cfg_obj_t **maps) {
    834   1.3  christos 	const cfg_obj_t *obj = NULL;
    835   1.3  christos 
    836   1.3  christos 	/*
    837   1.3  christos 	 * Return the original setting if this is not a mirror zone or if the
    838   1.3  christos 	 * zone is configured with something else than "notify yes;".
    839   1.3  christos 	 */
    840   1.3  christos 	if (ztype != dns_zone_mirror || ntype != dns_notifytype_yes) {
    841   1.3  christos 		return (ntype);
    842   1.3  christos 	}
    843   1.3  christos 
    844   1.3  christos 	/*
    845   1.3  christos 	 * Only log a message if "notify" was set in the configuration
    846   1.3  christos 	 * hierarchy supplied in 'maps'.
    847   1.3  christos 	 */
    848   1.3  christos 	if (named_config_get(maps, "notify", &obj) == ISC_R_SUCCESS) {
    849   1.3  christos 		cfg_obj_log(obj, named_g_lctx, ISC_LOG_INFO,
    850   1.3  christos 			    "'notify explicit;' will be used for mirror zone "
    851   1.7  christos 			    "'%s'",
    852   1.7  christos 			    zname);
    853   1.3  christos 	}
    854   1.3  christos 
    855   1.3  christos 	return (dns_notifytype_explicit);
    856   1.3  christos }
    857   1.1  christos 
    858   1.1  christos isc_result_t
    859   1.1  christos named_zone_configure(const cfg_obj_t *config, const cfg_obj_t *vconfig,
    860   1.7  christos 		     const cfg_obj_t *zconfig, cfg_aclconfctx_t *ac,
    861   1.7  christos 		     dns_kasplist_t *kasplist, dns_zone_t *zone,
    862   1.7  christos 		     dns_zone_t *raw) {
    863   1.1  christos 	isc_result_t result;
    864   1.1  christos 	const char *zname;
    865   1.1  christos 	dns_rdataclass_t zclass;
    866   1.1  christos 	dns_rdataclass_t vclass;
    867   1.1  christos 	const cfg_obj_t *maps[5];
    868   1.1  christos 	const cfg_obj_t *nodefault[4];
    869   1.1  christos 	const cfg_obj_t *zoptions = NULL;
    870   1.1  christos 	const cfg_obj_t *options = NULL;
    871   1.1  christos 	const cfg_obj_t *obj;
    872   1.1  christos 	const char *filename = NULL;
    873   1.7  christos 	const char *kaspname = NULL;
    874   1.1  christos 	const char *dupcheck;
    875   1.1  christos 	dns_notifytype_t notifytype = dns_notifytype_yes;
    876   1.3  christos 	uint32_t count;
    877   1.1  christos 	unsigned int dbargc;
    878   1.1  christos 	char **dbargv;
    879   1.1  christos 	static char default_dbtype[] = "rbt";
    880   1.1  christos 	static char dlz_dbtype[] = "dlz";
    881   1.1  christos 	char *cpval = default_dbtype;
    882   1.1  christos 	isc_mem_t *mctx = dns_zone_getmctx(zone);
    883   1.1  christos 	dns_dialuptype_t dialup = dns_dialuptype_no;
    884   1.1  christos 	dns_zonetype_t ztype;
    885   1.1  christos 	int i;
    886   1.3  christos 	int32_t journal_size;
    887   1.3  christos 	bool multi;
    888   1.3  christos 	bool alt;
    889   1.7  christos 	dns_view_t *view = NULL;
    890   1.7  christos 	dns_kasp_t *kasp = NULL;
    891   1.3  christos 	bool check = false, fail = false;
    892   1.3  christos 	bool warn = false, ignore = false;
    893   1.3  christos 	bool ixfrdiff;
    894   1.9  christos 	bool use_kasp = false;
    895   1.1  christos 	dns_masterformat_t masterformat;
    896   1.1  christos 	const dns_master_style_t *masterstyle = &dns_master_style_default;
    897   1.1  christos 	isc_stats_t *zoneqrystats;
    898   1.1  christos 	dns_stats_t *rcvquerystats;
    899   1.4  christos 	dns_stats_t *dnssecsignstats;
    900   1.3  christos 	dns_zonestat_level_t statlevel = dns_zonestat_none;
    901   1.1  christos 	int seconds;
    902  1.12  christos 	dns_ttl_t maxttl = 0; /* unlimited */
    903   1.1  christos 	dns_zone_t *mayberaw = (raw != NULL) ? raw : zone;
    904   1.1  christos 	isc_dscp_t dscp;
    905   1.1  christos 
    906   1.1  christos 	i = 0;
    907   1.1  christos 	if (zconfig != NULL) {
    908   1.1  christos 		zoptions = cfg_tuple_get(zconfig, "options");
    909   1.1  christos 		nodefault[i] = maps[i] = zoptions;
    910   1.1  christos 		i++;
    911   1.1  christos 	}
    912   1.1  christos 	if (vconfig != NULL) {
    913   1.1  christos 		nodefault[i] = maps[i] = cfg_tuple_get(vconfig, "options");
    914   1.1  christos 		i++;
    915   1.1  christos 	}
    916   1.1  christos 	if (config != NULL) {
    917   1.1  christos 		(void)cfg_map_get(config, "options", &options);
    918   1.1  christos 		if (options != NULL) {
    919   1.1  christos 			nodefault[i] = maps[i] = options;
    920   1.1  christos 			i++;
    921   1.1  christos 		}
    922   1.1  christos 	}
    923   1.1  christos 	nodefault[i] = NULL;
    924   1.1  christos 	maps[i++] = named_g_defaults;
    925   1.1  christos 	maps[i] = NULL;
    926   1.1  christos 
    927   1.7  christos 	if (vconfig != NULL) {
    928   1.1  christos 		RETERR(named_config_getclass(cfg_tuple_get(vconfig, "class"),
    929   1.7  christos 					     dns_rdataclass_in, &vclass));
    930   1.7  christos 	} else {
    931   1.1  christos 		vclass = dns_rdataclass_in;
    932   1.7  christos 	}
    933   1.1  christos 
    934   1.1  christos 	/*
    935   1.1  christos 	 * Configure values common to all zone types.
    936   1.1  christos 	 */
    937   1.1  christos 
    938   1.1  christos 	zname = cfg_obj_asstring(cfg_tuple_get(zconfig, "name"));
    939   1.1  christos 
    940   1.7  christos 	RETERR(named_config_getclass(cfg_tuple_get(zconfig, "class"), vclass,
    941   1.7  christos 				     &zclass));
    942   1.1  christos 	dns_zone_setclass(zone, zclass);
    943   1.7  christos 	if (raw != NULL) {
    944   1.1  christos 		dns_zone_setclass(raw, zclass);
    945   1.7  christos 	}
    946   1.1  christos 
    947   1.1  christos 	ztype = zonetype_fromconfig(zoptions);
    948   1.1  christos 	if (raw != NULL) {
    949   1.1  christos 		dns_zone_settype(raw, ztype);
    950  1.12  christos 		dns_zone_settype(zone, dns_zone_primary);
    951   1.7  christos 	} else {
    952   1.1  christos 		dns_zone_settype(zone, ztype);
    953   1.7  christos 	}
    954   1.1  christos 
    955   1.1  christos 	obj = NULL;
    956   1.1  christos 	result = cfg_map_get(zoptions, "database", &obj);
    957   1.7  christos 	if (result == ISC_R_SUCCESS) {
    958   1.1  christos 		cpval = isc_mem_strdup(mctx, cfg_obj_asstring(obj));
    959   1.7  christos 	}
    960   1.7  christos 	if (cpval == NULL) {
    961   1.7  christos 		return (ISC_R_NOMEMORY);
    962   1.7  christos 	}
    963   1.1  christos 
    964   1.1  christos 	obj = NULL;
    965   1.1  christos 	result = cfg_map_get(zoptions, "dlz", &obj);
    966   1.1  christos 	if (result == ISC_R_SUCCESS) {
    967   1.1  christos 		const char *dlzname = cfg_obj_asstring(obj);
    968   1.1  christos 		size_t len;
    969   1.1  christos 
    970   1.1  christos 		if (cpval != default_dbtype) {
    971   1.7  christos 			isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
    972   1.7  christos 				      NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,
    973   1.7  christos 				      "zone '%s': both 'database' and 'dlz' "
    974   1.7  christos 				      "specified",
    975   1.7  christos 				      zname);
    976   1.7  christos 			return (ISC_R_FAILURE);
    977   1.1  christos 		}
    978   1.1  christos 
    979   1.1  christos 		len = strlen(dlzname) + 5;
    980   1.1  christos 		cpval = isc_mem_allocate(mctx, len);
    981   1.1  christos 		snprintf(cpval, len, "dlz %s", dlzname);
    982   1.1  christos 	}
    983   1.1  christos 
    984   1.1  christos 	result = strtoargv(mctx, cpval, &dbargc, &dbargv);
    985   1.1  christos 	if (result != ISC_R_SUCCESS && cpval != default_dbtype) {
    986   1.1  christos 		isc_mem_free(mctx, cpval);
    987   1.1  christos 		return (result);
    988   1.1  christos 	}
    989   1.1  christos 
    990   1.1  christos 	/*
    991   1.1  christos 	 * ANSI C is strange here.  There is no logical reason why (char **)
    992   1.1  christos 	 * cannot be promoted automatically to (const char * const *) by the
    993   1.1  christos 	 * compiler w/o generating a warning.
    994   1.1  christos 	 */
    995   1.7  christos 	dns_zone_setdbtype(zone, dbargc, (const char *const *)dbargv);
    996   1.1  christos 	isc_mem_put(mctx, dbargv, dbargc * sizeof(*dbargv));
    997   1.7  christos 	if (cpval != default_dbtype && cpval != dlz_dbtype) {
    998   1.1  christos 		isc_mem_free(mctx, cpval);
    999   1.7  christos 	}
   1000   1.1  christos 
   1001   1.1  christos 	obj = NULL;
   1002   1.1  christos 	result = cfg_map_get(zoptions, "file", &obj);
   1003   1.7  christos 	if (result == ISC_R_SUCCESS) {
   1004   1.1  christos 		filename = cfg_obj_asstring(obj);
   1005   1.7  christos 	}
   1006   1.1  christos 
   1007   1.1  christos 	/*
   1008   1.1  christos 	 * Unless we're using some alternative database, a master zone
   1009   1.1  christos 	 * will be needing a master file.
   1010   1.1  christos 	 */
   1011  1.12  christos 	if (ztype == dns_zone_primary && cpval == default_dbtype &&
   1012   1.1  christos 	    filename == NULL) {
   1013   1.1  christos 		isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
   1014   1.1  christos 			      NAMED_LOGMODULE_SERVER, ISC_LOG_ERROR,
   1015   1.7  christos 			      "zone '%s': 'file' not specified", zname);
   1016   1.1  christos 		return (ISC_R_FAILURE);
   1017   1.1  christos 	}
   1018   1.1  christos 
   1019  1.12  christos 	if (ztype == dns_zone_secondary || ztype == dns_zone_mirror) {
   1020   1.1  christos 		masterformat = dns_masterformat_raw;
   1021   1.7  christos 	} else {
   1022   1.1  christos 		masterformat = dns_masterformat_text;
   1023   1.7  christos 	}
   1024   1.1  christos 	obj = NULL;
   1025   1.1  christos 	result = named_config_get(maps, "masterfile-format", &obj);
   1026   1.1  christos 	if (result == ISC_R_SUCCESS) {
   1027   1.1  christos 		const char *masterformatstr = cfg_obj_asstring(obj);
   1028   1.1  christos 
   1029   1.3  christos 		if (strcasecmp(masterformatstr, "text") == 0) {
   1030   1.1  christos 			masterformat = dns_masterformat_text;
   1031   1.3  christos 		} else if (strcasecmp(masterformatstr, "raw") == 0) {
   1032   1.1  christos 			masterformat = dns_masterformat_raw;
   1033   1.3  christos 		} else if (strcasecmp(masterformatstr, "map") == 0) {
   1034   1.1  christos 			masterformat = dns_masterformat_map;
   1035  1.12  christos 			cfg_obj_log(obj, named_g_lctx, ISC_LOG_WARNING,
   1036  1.12  christos 				    "masterfile-format: format 'map' is "
   1037  1.12  christos 				    "deprecated");
   1038   1.3  christos 		} else {
   1039  1.12  christos 			UNREACHABLE();
   1040   1.3  christos 		}
   1041   1.1  christos 	}
   1042   1.1  christos 
   1043   1.1  christos 	obj = NULL;
   1044   1.1  christos 	result = named_config_get(maps, "masterfile-style", &obj);
   1045   1.1  christos 	if (result == ISC_R_SUCCESS) {
   1046   1.1  christos 		const char *masterstylestr = cfg_obj_asstring(obj);
   1047   1.1  christos 
   1048   1.1  christos 		if (masterformat != dns_masterformat_text) {
   1049   1.1  christos 			cfg_obj_log(obj, named_g_lctx, ISC_LOG_ERROR,
   1050   1.1  christos 				    "zone '%s': 'masterfile-style' "
   1051   1.1  christos 				    "can only be used with "
   1052   1.7  christos 				    "'masterfile-format text'",
   1053   1.7  christos 				    zname);
   1054   1.1  christos 			return (ISC_R_FAILURE);
   1055   1.1  christos 		}
   1056   1.1  christos 
   1057   1.3  christos 		if (strcasecmp(masterstylestr, "full") == 0) {
   1058   1.1  christos 			masterstyle = &dns_master_style_full;
   1059   1.3  christos 		} else if (strcasecmp(masterstylestr, "relative") == 0) {
   1060   1.1  christos 			masterstyle = &dns_master_style_default;
   1061   1.3  christos 		} else {
   1062  1.12  christos 			UNREACHABLE();
   1063   1.7  christos 		}
   1064   1.1  christos 	}
   1065   1.1  christos 
   1066   1.1  christos 	obj = NULL;
   1067   1.1  christos 	result = named_config_get(maps, "max-records", &obj);
   1068   1.1  christos 	INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1069   1.1  christos 	dns_zone_setmaxrecords(mayberaw, cfg_obj_asuint32(obj));
   1070   1.7  christos 	if (zone != mayberaw) {
   1071   1.1  christos 		dns_zone_setmaxrecords(zone, 0);
   1072   1.7  christos 	}
   1073   1.1  christos 
   1074   1.1  christos 	if (raw != NULL && filename != NULL) {
   1075   1.1  christos #define SIGNED ".signed"
   1076   1.1  christos 		size_t signedlen = strlen(filename) + sizeof(SIGNED);
   1077   1.1  christos 		char *signedname;
   1078   1.1  christos 
   1079   1.7  christos 		RETERR(dns_zone_setfile(raw, filename, masterformat,
   1080   1.7  christos 					masterstyle));
   1081   1.1  christos 		signedname = isc_mem_get(mctx, signedlen);
   1082   1.1  christos 
   1083   1.1  christos 		(void)snprintf(signedname, signedlen, "%s" SIGNED, filename);
   1084   1.3  christos 		result = dns_zone_setfile(zone, signedname,
   1085   1.3  christos 					  dns_masterformat_raw, NULL);
   1086   1.1  christos 		isc_mem_put(mctx, signedname, signedlen);
   1087   1.7  christos 		if (result != ISC_R_SUCCESS) {
   1088   1.1  christos 			return (result);
   1089   1.7  christos 		}
   1090   1.7  christos 	} else {
   1091   1.7  christos 		RETERR(dns_zone_setfile(zone, filename, masterformat,
   1092   1.7  christos 					masterstyle));
   1093   1.7  christos 	}
   1094   1.1  christos 
   1095   1.1  christos 	obj = NULL;
   1096   1.1  christos 	result = cfg_map_get(zoptions, "journal", &obj);
   1097   1.7  christos 	if (result == ISC_R_SUCCESS) {
   1098   1.1  christos 		RETERR(dns_zone_setjournal(mayberaw, cfg_obj_asstring(obj)));
   1099   1.7  christos 	}
   1100   1.1  christos 
   1101   1.1  christos 	/*
   1102   1.1  christos 	 * Notify messages are processed by the raw zone if it exists.
   1103   1.1  christos 	 */
   1104  1.12  christos 	if (ztype == dns_zone_secondary || ztype == dns_zone_mirror) {
   1105   1.7  christos 		RETERR(configure_zone_acl(
   1106   1.7  christos 			zconfig, vconfig, config, allow_notify, ac, mayberaw,
   1107   1.7  christos 			dns_zone_setnotifyacl, dns_zone_clearnotifyacl));
   1108   1.7  christos 	}
   1109   1.1  christos 
   1110   1.1  christos 	/*
   1111   1.1  christos 	 * XXXAG This probably does not make sense for stubs.
   1112   1.1  christos 	 */
   1113   1.7  christos 	RETERR(configure_zone_acl(zconfig, vconfig, config, allow_query, ac,
   1114   1.7  christos 				  zone, dns_zone_setqueryacl,
   1115   1.1  christos 				  dns_zone_clearqueryacl));
   1116   1.1  christos 
   1117   1.7  christos 	RETERR(configure_zone_acl(zconfig, vconfig, config, allow_query_on, ac,
   1118   1.7  christos 				  zone, dns_zone_setqueryonacl,
   1119   1.1  christos 				  dns_zone_clearqueryonacl));
   1120   1.1  christos 
   1121   1.1  christos 	obj = NULL;
   1122   1.1  christos 	result = named_config_get(maps, "dialup", &obj);
   1123   1.1  christos 	INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1124   1.1  christos 	if (cfg_obj_isboolean(obj)) {
   1125   1.7  christos 		if (cfg_obj_asboolean(obj)) {
   1126   1.1  christos 			dialup = dns_dialuptype_yes;
   1127   1.7  christos 		} else {
   1128   1.1  christos 			dialup = dns_dialuptype_no;
   1129   1.7  christos 		}
   1130   1.1  christos 	} else {
   1131   1.1  christos 		const char *dialupstr = cfg_obj_asstring(obj);
   1132   1.3  christos 		if (strcasecmp(dialupstr, "notify") == 0) {
   1133   1.1  christos 			dialup = dns_dialuptype_notify;
   1134   1.3  christos 		} else if (strcasecmp(dialupstr, "notify-passive") == 0) {
   1135   1.1  christos 			dialup = dns_dialuptype_notifypassive;
   1136   1.3  christos 		} else if (strcasecmp(dialupstr, "refresh") == 0) {
   1137   1.1  christos 			dialup = dns_dialuptype_refresh;
   1138   1.3  christos 		} else if (strcasecmp(dialupstr, "passive") == 0) {
   1139   1.1  christos 			dialup = dns_dialuptype_passive;
   1140   1.3  christos 		} else {
   1141  1.12  christos 			UNREACHABLE();
   1142   1.3  christos 		}
   1143   1.1  christos 	}
   1144   1.7  christos 	if (raw != NULL) {
   1145   1.1  christos 		dns_zone_setdialup(raw, dialup);
   1146   1.7  christos 	}
   1147   1.1  christos 	dns_zone_setdialup(zone, dialup);
   1148   1.1  christos 
   1149   1.1  christos 	obj = NULL;
   1150   1.1  christos 	result = named_config_get(maps, "zone-statistics", &obj);
   1151   1.1  christos 	INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1152   1.1  christos 	if (cfg_obj_isboolean(obj)) {
   1153   1.7  christos 		if (cfg_obj_asboolean(obj)) {
   1154   1.1  christos 			statlevel = dns_zonestat_full;
   1155   1.7  christos 		} else {
   1156   1.1  christos 			statlevel = dns_zonestat_none;
   1157   1.7  christos 		}
   1158   1.1  christos 	} else {
   1159   1.1  christos 		const char *levelstr = cfg_obj_asstring(obj);
   1160   1.3  christos 		if (strcasecmp(levelstr, "full") == 0) {
   1161   1.1  christos 			statlevel = dns_zonestat_full;
   1162   1.3  christos 		} else if (strcasecmp(levelstr, "terse") == 0) {
   1163   1.1  christos 			statlevel = dns_zonestat_terse;
   1164   1.3  christos 		} else if (strcasecmp(levelstr, "none") == 0) {
   1165   1.1  christos 			statlevel = dns_zonestat_none;
   1166   1.3  christos 		} else {
   1167  1.12  christos 			UNREACHABLE();
   1168   1.3  christos 		}
   1169   1.1  christos 	}
   1170   1.1  christos 	dns_zone_setstatlevel(zone, statlevel);
   1171   1.1  christos 
   1172   1.7  christos 	zoneqrystats = NULL;
   1173   1.1  christos 	rcvquerystats = NULL;
   1174   1.4  christos 	dnssecsignstats = NULL;
   1175   1.1  christos 	if (statlevel == dns_zonestat_full) {
   1176   1.1  christos 		RETERR(isc_stats_create(mctx, &zoneqrystats,
   1177   1.1  christos 					ns_statscounter_max));
   1178   1.4  christos 		RETERR(dns_rdatatypestats_create(mctx, &rcvquerystats));
   1179   1.4  christos 		RETERR(dns_dnssecsignstats_create(mctx, &dnssecsignstats));
   1180   1.1  christos 	}
   1181   1.7  christos 	dns_zone_setrequeststats(zone, zoneqrystats);
   1182   1.1  christos 	dns_zone_setrcvquerystats(zone, rcvquerystats);
   1183   1.4  christos 	dns_zone_setdnssecsignstats(zone, dnssecsignstats);
   1184   1.1  christos 
   1185   1.7  christos 	if (zoneqrystats != NULL) {
   1186   1.1  christos 		isc_stats_detach(&zoneqrystats);
   1187   1.7  christos 	}
   1188   1.1  christos 
   1189   1.7  christos 	if (rcvquerystats != NULL) {
   1190   1.1  christos 		dns_stats_detach(&rcvquerystats);
   1191   1.7  christos 	}
   1192   1.1  christos 
   1193   1.7  christos 	if (dnssecsignstats != NULL) {
   1194   1.4  christos 		dns_stats_detach(&dnssecsignstats);
   1195   1.4  christos 	}
   1196   1.4  christos 
   1197   1.1  christos 	/*
   1198   1.1  christos 	 * Configure master functionality.  This applies
   1199   1.9  christos 	 * to primary servers (type "primary") and secondaries
   1200   1.9  christos 	 * acting as primaries (type "secondary"), but not to stubs.
   1201   1.1  christos 	 */
   1202   1.1  christos 	if (ztype != dns_zone_stub && ztype != dns_zone_staticstub &&
   1203   1.7  christos 	    ztype != dns_zone_redirect)
   1204   1.7  christos 	{
   1205   1.7  christos 		obj = NULL;
   1206   1.7  christos 		result = named_config_get(maps, "dnssec-policy", &obj);
   1207   1.7  christos 		if (result == ISC_R_SUCCESS) {
   1208   1.7  christos 			kaspname = cfg_obj_asstring(obj);
   1209  1.11  christos 			if (strcmp(kaspname, "none") != 0) {
   1210  1.11  christos 				result = dns_kasplist_find(kasplist, kaspname,
   1211  1.11  christos 							   &kasp);
   1212  1.11  christos 				if (result != ISC_R_SUCCESS) {
   1213  1.11  christos 					cfg_obj_log(
   1214  1.11  christos 						obj, named_g_lctx,
   1215  1.11  christos 						ISC_LOG_ERROR,
   1216  1.11  christos 						"dnssec-policy '%s' not found ",
   1217  1.11  christos 						kaspname);
   1218  1.11  christos 					RETERR(result);
   1219  1.11  christos 				}
   1220  1.11  christos 				dns_zone_setkasp(zone, kasp);
   1221  1.11  christos 				use_kasp = true;
   1222   1.7  christos 			}
   1223  1.11  christos 		}
   1224  1.11  christos 		if (!use_kasp) {
   1225  1.11  christos 			dns_zone_setkasp(zone, NULL);
   1226   1.7  christos 		}
   1227   1.7  christos 
   1228   1.1  christos 		obj = NULL;
   1229   1.1  christos 		result = named_config_get(maps, "notify", &obj);
   1230   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1231   1.1  christos 		if (cfg_obj_isboolean(obj)) {
   1232   1.7  christos 			if (cfg_obj_asboolean(obj)) {
   1233   1.1  christos 				notifytype = dns_notifytype_yes;
   1234   1.7  christos 			} else {
   1235   1.1  christos 				notifytype = dns_notifytype_no;
   1236   1.7  christos 			}
   1237   1.1  christos 		} else {
   1238   1.9  christos 			const char *str = cfg_obj_asstring(obj);
   1239   1.9  christos 			if (strcasecmp(str, "explicit") == 0) {
   1240   1.1  christos 				notifytype = dns_notifytype_explicit;
   1241   1.9  christos 			} else if (strcasecmp(str, "master-only") == 0 ||
   1242   1.9  christos 				   strcasecmp(str, "primary-only") == 0)
   1243   1.9  christos 			{
   1244   1.1  christos 				notifytype = dns_notifytype_masteronly;
   1245   1.3  christos 			} else {
   1246  1.12  christos 				UNREACHABLE();
   1247   1.3  christos 			}
   1248   1.1  christos 		}
   1249   1.3  christos 		notifytype = process_notifytype(notifytype, ztype, zname,
   1250   1.3  christos 						nodefault);
   1251   1.7  christos 		if (raw != NULL) {
   1252   1.1  christos 			dns_zone_setnotifytype(raw, dns_notifytype_no);
   1253   1.7  christos 		}
   1254   1.1  christos 		dns_zone_setnotifytype(zone, notifytype);
   1255   1.1  christos 
   1256   1.1  christos 		obj = NULL;
   1257   1.1  christos 		result = named_config_get(maps, "also-notify", &obj);
   1258   1.1  christos 		if (result == ISC_R_SUCCESS &&
   1259   1.1  christos 		    (notifytype == dns_notifytype_yes ||
   1260   1.1  christos 		     notifytype == dns_notifytype_explicit ||
   1261   1.1  christos 		     (notifytype == dns_notifytype_masteronly &&
   1262  1.12  christos 		      ztype == dns_zone_primary)))
   1263   1.1  christos 		{
   1264   1.1  christos 			dns_ipkeylist_t ipkl;
   1265   1.1  christos 			dns_ipkeylist_init(&ipkl);
   1266   1.1  christos 
   1267  1.11  christos 			RETERR(named_config_getipandkeylist(config, "primaries",
   1268  1.11  christos 							    obj, mctx, &ipkl));
   1269   1.7  christos 			result = dns_zone_setalsonotifydscpkeys(
   1270   1.7  christos 				zone, ipkl.addrs, ipkl.dscps, ipkl.keys,
   1271   1.7  christos 				ipkl.count);
   1272   1.1  christos 			dns_ipkeylist_clear(mctx, &ipkl);
   1273   1.1  christos 			RETERR(result);
   1274   1.7  christos 		} else {
   1275   1.1  christos 			RETERR(dns_zone_setalsonotify(zone, NULL, 0));
   1276   1.7  christos 		}
   1277   1.1  christos 
   1278   1.1  christos 		obj = NULL;
   1279  1.11  christos 		result = named_config_get(maps, "parental-source", &obj);
   1280  1.11  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1281  1.11  christos 		RETERR(dns_zone_setparentalsrc4(zone, cfg_obj_assockaddr(obj)));
   1282  1.11  christos 		dscp = cfg_obj_getdscp(obj);
   1283  1.11  christos 		if (dscp == -1) {
   1284  1.11  christos 			dscp = named_g_dscp;
   1285  1.11  christos 		}
   1286  1.11  christos 		RETERR(dns_zone_setparentalsrc4dscp(zone, dscp));
   1287  1.11  christos 		named_add_reserved_dispatch(named_g_server,
   1288  1.11  christos 					    cfg_obj_assockaddr(obj));
   1289  1.11  christos 
   1290  1.11  christos 		obj = NULL;
   1291  1.11  christos 		result = named_config_get(maps, "parental-source-v6", &obj);
   1292  1.11  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1293  1.11  christos 		RETERR(dns_zone_setparentalsrc6(zone, cfg_obj_assockaddr(obj)));
   1294  1.11  christos 		dscp = cfg_obj_getdscp(obj);
   1295  1.11  christos 		if (dscp == -1) {
   1296  1.11  christos 			dscp = named_g_dscp;
   1297  1.11  christos 		}
   1298  1.11  christos 		RETERR(dns_zone_setparentalsrc6dscp(zone, dscp));
   1299  1.11  christos 		named_add_reserved_dispatch(named_g_server,
   1300  1.11  christos 					    cfg_obj_assockaddr(obj));
   1301  1.11  christos 
   1302  1.11  christos 		obj = NULL;
   1303   1.1  christos 		result = named_config_get(maps, "notify-source", &obj);
   1304   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1305   1.1  christos 		RETERR(dns_zone_setnotifysrc4(zone, cfg_obj_assockaddr(obj)));
   1306   1.1  christos 		dscp = cfg_obj_getdscp(obj);
   1307   1.7  christos 		if (dscp == -1) {
   1308   1.1  christos 			dscp = named_g_dscp;
   1309   1.7  christos 		}
   1310   1.1  christos 		RETERR(dns_zone_setnotifysrc4dscp(zone, dscp));
   1311   1.1  christos 		named_add_reserved_dispatch(named_g_server,
   1312   1.1  christos 					    cfg_obj_assockaddr(obj));
   1313   1.1  christos 
   1314   1.1  christos 		obj = NULL;
   1315   1.1  christos 		result = named_config_get(maps, "notify-source-v6", &obj);
   1316   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1317   1.1  christos 		RETERR(dns_zone_setnotifysrc6(zone, cfg_obj_assockaddr(obj)));
   1318   1.1  christos 		dscp = cfg_obj_getdscp(obj);
   1319   1.7  christos 		if (dscp == -1) {
   1320   1.1  christos 			dscp = named_g_dscp;
   1321   1.7  christos 		}
   1322   1.1  christos 		RETERR(dns_zone_setnotifysrc6dscp(zone, dscp));
   1323   1.1  christos 		named_add_reserved_dispatch(named_g_server,
   1324   1.1  christos 					    cfg_obj_assockaddr(obj));
   1325   1.1  christos 
   1326   1.1  christos 		obj = NULL;
   1327   1.1  christos 		result = named_config_get(maps, "notify-to-soa", &obj);
   1328   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1329   1.1  christos 		dns_zone_setoption(zone, DNS_ZONEOPT_NOTIFYTOSOA,
   1330   1.1  christos 				   cfg_obj_asboolean(obj));
   1331   1.1  christos 
   1332   1.1  christos 		dns_zone_setisself(zone, isself, named_g_server->interfacemgr);
   1333   1.1  christos 
   1334   1.7  christos 		RETERR(configure_zone_acl(
   1335   1.7  christos 			zconfig, vconfig, config, allow_transfer, ac, zone,
   1336   1.7  christos 			dns_zone_setxfracl, dns_zone_clearxfracl));
   1337   1.1  christos 
   1338   1.1  christos 		obj = NULL;
   1339   1.1  christos 		result = named_config_get(maps, "max-transfer-time-out", &obj);
   1340   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1341   1.1  christos 		dns_zone_setmaxxfrout(zone, cfg_obj_asuint32(obj) * 60);
   1342   1.1  christos 
   1343   1.1  christos 		obj = NULL;
   1344   1.1  christos 		result = named_config_get(maps, "max-transfer-idle-out", &obj);
   1345   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1346   1.1  christos 		dns_zone_setidleout(zone, cfg_obj_asuint32(obj) * 60);
   1347   1.1  christos 
   1348   1.1  christos 		obj = NULL;
   1349   1.1  christos 		result = named_config_get(maps, "max-journal-size", &obj);
   1350   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1351   1.7  christos 		if (raw != NULL) {
   1352   1.1  christos 			dns_zone_setjournalsize(raw, -1);
   1353   1.7  christos 		}
   1354   1.1  christos 		dns_zone_setjournalsize(zone, -1);
   1355   1.1  christos 		if (cfg_obj_isstring(obj)) {
   1356   1.1  christos 			const char *str = cfg_obj_asstring(obj);
   1357   1.1  christos 			if (strcasecmp(str, "unlimited") == 0) {
   1358   1.1  christos 				journal_size = DNS_JOURNAL_SIZE_MAX;
   1359   1.1  christos 			} else {
   1360   1.1  christos 				INSIST(strcasecmp(str, "default") == 0);
   1361   1.1  christos 				journal_size = -1;
   1362   1.1  christos 			}
   1363   1.1  christos 		} else {
   1364   1.1  christos 			isc_resourcevalue_t value;
   1365   1.1  christos 			value = cfg_obj_asuint64(obj);
   1366   1.1  christos 			if (value > DNS_JOURNAL_SIZE_MAX) {
   1367   1.7  christos 				cfg_obj_log(obj, named_g_lctx, ISC_LOG_ERROR,
   1368   1.1  christos 					    "'max-journal-size "
   1369   1.3  christos 					    "%" PRId64 "' "
   1370   1.1  christos 					    "is too large",
   1371   1.1  christos 					    value);
   1372   1.1  christos 				RETERR(ISC_R_RANGE);
   1373   1.1  christos 			}
   1374   1.3  christos 			journal_size = (uint32_t)value;
   1375   1.1  christos 		}
   1376   1.7  christos 		if (raw != NULL) {
   1377   1.1  christos 			dns_zone_setjournalsize(raw, journal_size);
   1378   1.7  christos 		}
   1379   1.1  christos 		dns_zone_setjournalsize(zone, journal_size);
   1380   1.1  christos 
   1381   1.1  christos 		obj = NULL;
   1382   1.1  christos 		result = named_config_get(maps, "ixfr-from-differences", &obj);
   1383   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1384   1.3  christos 		if (cfg_obj_isboolean(obj)) {
   1385   1.1  christos 			ixfrdiff = cfg_obj_asboolean(obj);
   1386   1.7  christos 		} else if ((strcasecmp(cfg_obj_asstring(obj), "primary") == 0 ||
   1387   1.7  christos 			    strcasecmp(cfg_obj_asstring(obj), "master") == 0) &&
   1388  1.12  christos 			   ztype == dns_zone_primary)
   1389   1.3  christos 		{
   1390   1.3  christos 			ixfrdiff = true;
   1391   1.7  christos 		} else if ((strcasecmp(cfg_obj_asstring(obj), "secondary") ==
   1392   1.7  christos 				    0 ||
   1393   1.7  christos 			    strcasecmp(cfg_obj_asstring(obj), "slave") == 0) &&
   1394  1.12  christos 			   ztype == dns_zone_secondary)
   1395   1.3  christos 		{
   1396   1.3  christos 			ixfrdiff = true;
   1397   1.3  christos 		} else {
   1398   1.3  christos 			ixfrdiff = false;
   1399   1.3  christos 		}
   1400   1.1  christos 		if (raw != NULL) {
   1401   1.1  christos 			dns_zone_setoption(raw, DNS_ZONEOPT_IXFRFROMDIFFS,
   1402   1.3  christos 					   true);
   1403   1.1  christos 			dns_zone_setoption(zone, DNS_ZONEOPT_IXFRFROMDIFFS,
   1404   1.3  christos 					   false);
   1405   1.7  christos 		} else {
   1406   1.1  christos 			dns_zone_setoption(zone, DNS_ZONEOPT_IXFRFROMDIFFS,
   1407   1.1  christos 					   ixfrdiff);
   1408   1.7  christos 		}
   1409   1.1  christos 
   1410   1.1  christos 		obj = NULL;
   1411   1.9  christos 		result = named_config_get(maps, "max-ixfr-ratio", &obj);
   1412   1.9  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1413   1.9  christos 		if (cfg_obj_isstring(obj)) {
   1414   1.9  christos 			dns_zone_setixfrratio(zone, 0);
   1415   1.9  christos 		} else {
   1416   1.9  christos 			dns_zone_setixfrratio(zone, cfg_obj_aspercentage(obj));
   1417   1.9  christos 		}
   1418   1.9  christos 
   1419   1.9  christos 		obj = NULL;
   1420   1.1  christos 		result = named_config_get(maps, "request-expire", &obj);
   1421   1.1  christos 		INSIST(result == ISC_R_SUCCESS);
   1422   1.1  christos 		dns_zone_setrequestexpire(zone, cfg_obj_asboolean(obj));
   1423   1.1  christos 
   1424   1.1  christos 		obj = NULL;
   1425   1.1  christos 		result = named_config_get(maps, "request-ixfr", &obj);
   1426   1.1  christos 		INSIST(result == ISC_R_SUCCESS);
   1427   1.1  christos 		dns_zone_setrequestixfr(zone, cfg_obj_asboolean(obj));
   1428   1.1  christos 
   1429   1.8  christos 		obj = NULL;
   1430   1.1  christos 		checknames(ztype, maps, &obj);
   1431   1.1  christos 		INSIST(obj != NULL);
   1432   1.1  christos 		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
   1433   1.3  christos 			fail = false;
   1434   1.3  christos 			check = true;
   1435   1.1  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
   1436   1.3  christos 			fail = check = true;
   1437   1.1  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
   1438   1.3  christos 			fail = check = false;
   1439   1.3  christos 		} else {
   1440  1.12  christos 			UNREACHABLE();
   1441   1.3  christos 		}
   1442   1.1  christos 		if (raw != NULL) {
   1443   1.7  christos 			dns_zone_setoption(raw, DNS_ZONEOPT_CHECKNAMES, check);
   1444   1.1  christos 			dns_zone_setoption(raw, DNS_ZONEOPT_CHECKNAMESFAIL,
   1445   1.1  christos 					   fail);
   1446   1.7  christos 			dns_zone_setoption(zone, DNS_ZONEOPT_CHECKNAMES, false);
   1447   1.1  christos 			dns_zone_setoption(zone, DNS_ZONEOPT_CHECKNAMESFAIL,
   1448   1.3  christos 					   false);
   1449   1.1  christos 		} else {
   1450   1.7  christos 			dns_zone_setoption(zone, DNS_ZONEOPT_CHECKNAMES, check);
   1451   1.1  christos 			dns_zone_setoption(zone, DNS_ZONEOPT_CHECKNAMESFAIL,
   1452   1.1  christos 					   fail);
   1453   1.1  christos 		}
   1454   1.1  christos 
   1455   1.1  christos 		obj = NULL;
   1456   1.1  christos 		result = named_config_get(maps, "notify-delay", &obj);
   1457   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1458   1.1  christos 		dns_zone_setnotifydelay(zone, cfg_obj_asuint32(obj));
   1459   1.1  christos 
   1460   1.1  christos 		obj = NULL;
   1461   1.1  christos 		result = named_config_get(maps, "check-sibling", &obj);
   1462   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1463   1.1  christos 		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKSIBLING,
   1464   1.1  christos 				   cfg_obj_asboolean(obj));
   1465   1.1  christos 
   1466   1.1  christos 		obj = NULL;
   1467   1.1  christos 		result = named_config_get(maps, "check-spf", &obj);
   1468   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1469   1.1  christos 		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
   1470   1.3  christos 			check = true;
   1471   1.1  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
   1472   1.3  christos 			check = false;
   1473   1.3  christos 		} else {
   1474  1.12  christos 			UNREACHABLE();
   1475   1.3  christos 		}
   1476   1.1  christos 		dns_zone_setoption(zone, DNS_ZONEOPT_CHECKSPF, check);
   1477   1.1  christos 
   1478   1.1  christos 		obj = NULL;
   1479   1.1  christos 		result = named_config_get(maps, "zero-no-soa-ttl", &obj);
   1480   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1481   1.1  christos 		dns_zone_setzeronosoattl(zone, cfg_obj_asboolean(obj));
   1482   1.1  christos 
   1483   1.1  christos 		obj = NULL;
   1484   1.1  christos 		result = named_config_get(maps, "nsec3-test-zone", &obj);
   1485   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1486   1.1  christos 		dns_zone_setoption(zone, DNS_ZONEOPT_NSEC3TESTZONE,
   1487   1.1  christos 				   cfg_obj_asboolean(obj));
   1488   1.1  christos 	} else if (ztype == dns_zone_redirect) {
   1489   1.1  christos 		dns_zone_setnotifytype(zone, dns_notifytype_no);
   1490   1.1  christos 
   1491   1.1  christos 		obj = NULL;
   1492   1.1  christos 		result = named_config_get(maps, "max-journal-size", &obj);
   1493   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1494   1.1  christos 		dns_zone_setjournalsize(zone, -1);
   1495   1.1  christos 		if (cfg_obj_isstring(obj)) {
   1496   1.1  christos 			const char *str = cfg_obj_asstring(obj);
   1497   1.1  christos 			if (strcasecmp(str, "unlimited") == 0) {
   1498   1.1  christos 				journal_size = DNS_JOURNAL_SIZE_MAX;
   1499   1.1  christos 			} else {
   1500   1.1  christos 				INSIST(strcasecmp(str, "default") == 0);
   1501   1.1  christos 				journal_size = -1;
   1502   1.1  christos 			}
   1503   1.1  christos 		} else {
   1504   1.1  christos 			isc_resourcevalue_t value;
   1505   1.1  christos 			value = cfg_obj_asuint64(obj);
   1506   1.1  christos 			if (value > DNS_JOURNAL_SIZE_MAX) {
   1507   1.7  christos 				cfg_obj_log(obj, named_g_lctx, ISC_LOG_ERROR,
   1508   1.1  christos 					    "'max-journal-size "
   1509   1.3  christos 					    "%" PRId64 "' "
   1510   1.1  christos 					    "is too large",
   1511   1.1  christos 					    value);
   1512   1.1  christos 				RETERR(ISC_R_RANGE);
   1513   1.1  christos 			}
   1514   1.3  christos 			journal_size = (uint32_t)value;
   1515   1.1  christos 		}
   1516   1.1  christos 		dns_zone_setjournalsize(zone, journal_size);
   1517   1.1  christos 	}
   1518   1.1  christos 
   1519  1.12  christos 	if (use_kasp) {
   1520  1.12  christos 		maxttl = dns_kasp_zonemaxttl(dns_zone_getkasp(zone));
   1521  1.12  christos 	} else {
   1522  1.12  christos 		obj = NULL;
   1523  1.12  christos 		result = named_config_get(maps, "max-zone-ttl", &obj);
   1524  1.12  christos 		if (result == ISC_R_SUCCESS) {
   1525  1.12  christos 			if (cfg_obj_isduration(obj)) {
   1526  1.12  christos 				maxttl = cfg_obj_asduration(obj);
   1527  1.12  christos 			}
   1528  1.12  christos 		}
   1529  1.12  christos 	}
   1530  1.12  christos 	dns_zone_setmaxttl(zone, maxttl);
   1531  1.12  christos 	if (raw != NULL) {
   1532  1.12  christos 		dns_zone_setmaxttl(raw, maxttl);
   1533  1.12  christos 	}
   1534  1.12  christos 
   1535   1.1  christos 	/*
   1536   1.1  christos 	 * Configure update-related options.  These apply to
   1537   1.9  christos 	 * primary servers only.
   1538   1.1  christos 	 */
   1539  1.12  christos 	if (ztype == dns_zone_primary) {
   1540   1.1  christos 		dns_acl_t *updateacl;
   1541   1.1  christos 
   1542   1.7  christos 		RETERR(configure_zone_acl(
   1543   1.7  christos 			zconfig, vconfig, config, allow_update, ac, mayberaw,
   1544   1.7  christos 			dns_zone_setupdateacl, dns_zone_clearupdateacl));
   1545   1.1  christos 
   1546   1.1  christos 		updateacl = dns_zone_getupdateacl(mayberaw);
   1547   1.7  christos 		if (updateacl != NULL && dns_acl_isinsecure(updateacl)) {
   1548   1.1  christos 			isc_log_write(named_g_lctx, DNS_LOGCATEGORY_SECURITY,
   1549   1.1  christos 				      NAMED_LOGMODULE_SERVER, ISC_LOG_WARNING,
   1550   1.1  christos 				      "zone '%s' allows unsigned updates "
   1551   1.1  christos 				      "from remote hosts, which is insecure",
   1552   1.1  christos 				      zname);
   1553   1.7  christos 		}
   1554   1.1  christos 
   1555   1.1  christos 		RETERR(configure_zone_ssutable(zoptions, mayberaw, zname));
   1556   1.1  christos 	}
   1557   1.1  christos 
   1558  1.12  christos 	/*
   1559  1.12  christos 	 * Configure DNSSEC signing. These apply to primary zones or zones that
   1560  1.12  christos 	 * use inline-signing (raw != NULL).
   1561  1.12  christos 	 */
   1562  1.12  christos 	if (ztype == dns_zone_primary || raw != NULL) {
   1563   1.1  christos 		const cfg_obj_t *validity, *resign;
   1564   1.3  christos 		bool allow = false, maint = false;
   1565   1.3  christos 		bool sigvalinsecs;
   1566   1.3  christos 
   1567   1.9  christos 		if (use_kasp) {
   1568   1.9  christos 			if (dns_kasp_nsec3(kasp)) {
   1569   1.9  christos 				result = dns_zone_setnsec3param(
   1570   1.9  christos 					zone, 1, dns_kasp_nsec3flags(kasp),
   1571   1.9  christos 					dns_kasp_nsec3iter(kasp),
   1572   1.9  christos 					dns_kasp_nsec3saltlen(kasp), NULL, true,
   1573   1.9  christos 					false);
   1574   1.9  christos 			} else {
   1575   1.9  christos 				result = dns_zone_setnsec3param(
   1576   1.9  christos 					zone, 0, 0, 0, 0, NULL, true, false);
   1577   1.9  christos 			}
   1578   1.9  christos 			INSIST(result == ISC_R_SUCCESS);
   1579   1.9  christos 		}
   1580   1.9  christos 
   1581   1.9  christos 		if (use_kasp) {
   1582   1.7  christos 			seconds = (uint32_t)dns_kasp_sigvalidity_dnskey(kasp);
   1583   1.7  christos 		} else {
   1584   1.7  christos 			obj = NULL;
   1585   1.7  christos 			result = named_config_get(maps, "dnskey-sig-validity",
   1586   1.7  christos 						  &obj);
   1587   1.7  christos 			INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1588   1.7  christos 			seconds = cfg_obj_asuint32(obj) * 86400;
   1589   1.7  christos 		}
   1590   1.3  christos 		dns_zone_setkeyvalidityinterval(zone, seconds);
   1591   1.1  christos 
   1592   1.9  christos 		if (use_kasp) {
   1593   1.7  christos 			seconds = (uint32_t)dns_kasp_sigvalidity(kasp);
   1594   1.7  christos 			dns_zone_setsigvalidityinterval(zone, seconds);
   1595   1.7  christos 			seconds = (uint32_t)dns_kasp_sigrefresh(kasp);
   1596   1.7  christos 			dns_zone_setsigresigninginterval(zone, seconds);
   1597   1.7  christos 		} else {
   1598   1.7  christos 			obj = NULL;
   1599   1.7  christos 			result = named_config_get(maps, "sig-validity-interval",
   1600   1.7  christos 						  &obj);
   1601   1.7  christos 			INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1602   1.7  christos 
   1603   1.7  christos 			sigvalinsecs = ns_server_getoption(
   1604   1.7  christos 				named_g_server->sctx, NS_SERVER_SIGVALINSECS);
   1605   1.7  christos 			validity = cfg_tuple_get(obj, "validity");
   1606   1.7  christos 			seconds = cfg_obj_asuint32(validity);
   1607   1.7  christos 			if (!sigvalinsecs) {
   1608   1.7  christos 				seconds *= 86400;
   1609   1.7  christos 			}
   1610   1.7  christos 			dns_zone_setsigvalidityinterval(zone, seconds);
   1611   1.1  christos 
   1612   1.7  christos 			resign = cfg_tuple_get(obj, "re-sign");
   1613   1.7  christos 			if (cfg_obj_isvoid(resign)) {
   1614   1.7  christos 				seconds /= 4;
   1615   1.7  christos 			} else if (!sigvalinsecs) {
   1616   1.9  christos 				uint32_t r = cfg_obj_asuint32(resign);
   1617   1.7  christos 				if (seconds > 7 * 86400) {
   1618   1.9  christos 					seconds = r * 86400;
   1619   1.7  christos 				} else {
   1620   1.9  christos 					seconds = r * 3600;
   1621   1.7  christos 				}
   1622   1.1  christos 			} else {
   1623   1.7  christos 				seconds = cfg_obj_asuint32(resign);
   1624   1.1  christos 			}
   1625   1.7  christos 			dns_zone_setsigresigninginterval(zone, seconds);
   1626   1.1  christos 		}
   1627   1.1  christos 
   1628   1.1  christos 		obj = NULL;
   1629   1.1  christos 		result = named_config_get(maps, "key-directory", &obj);
   1630   1.1  christos 		if (result == ISC_R_SUCCESS) {
   1631   1.1  christos 			filename = cfg_obj_asstring(obj);
   1632   1.1  christos 			RETERR(dns_zone_setkeydirectory(zone, filename));
   1633   1.1  christos 		}
   1634   1.1  christos 
   1635   1.1  christos 		obj = NULL;
   1636   1.1  christos 		result = named_config_get(maps, "sig-signing-signatures", &obj);
   1637   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1638   1.1  christos 		dns_zone_setsignatures(zone, cfg_obj_asuint32(obj));
   1639   1.1  christos 
   1640   1.1  christos 		obj = NULL;
   1641   1.1  christos 		result = named_config_get(maps, "sig-signing-nodes", &obj);
   1642   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1643   1.1  christos 		dns_zone_setnodes(zone, cfg_obj_asuint32(obj));
   1644   1.1  christos 
   1645   1.1  christos 		obj = NULL;
   1646   1.1  christos 		result = named_config_get(maps, "sig-signing-type", &obj);
   1647   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1648   1.1  christos 		dns_zone_setprivatetype(zone, cfg_obj_asuint32(obj));
   1649   1.1  christos 
   1650   1.1  christos 		obj = NULL;
   1651   1.1  christos 		result = named_config_get(maps, "update-check-ksk", &obj);
   1652   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1653   1.1  christos 		dns_zone_setoption(zone, DNS_ZONEOPT_UPDATECHECKKSK,
   1654   1.1  christos 				   cfg_obj_asboolean(obj));
   1655   1.7  christos 		/*
   1656   1.7  christos 		 * This setting will be ignored if dnssec-policy is used.
   1657   1.7  christos 		 * named-checkconf will error if both are configured.
   1658   1.7  christos 		 */
   1659   1.1  christos 
   1660   1.1  christos 		obj = NULL;
   1661   1.1  christos 		result = named_config_get(maps, "dnssec-dnskey-kskonly", &obj);
   1662   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1663   1.1  christos 		dns_zone_setoption(zone, DNS_ZONEOPT_DNSKEYKSKONLY,
   1664   1.1  christos 				   cfg_obj_asboolean(obj));
   1665   1.7  christos 		/*
   1666   1.7  christos 		 * This setting will be ignored if dnssec-policy is used.
   1667   1.7  christos 		 * named-checkconf will error if both are configured.
   1668   1.7  christos 		 */
   1669   1.1  christos 
   1670   1.1  christos 		obj = NULL;
   1671   1.1  christos 		result = named_config_get(maps, "dnssec-loadkeys-interval",
   1672   1.1  christos 					  &obj);
   1673   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1674   1.1  christos 		RETERR(dns_zone_setrefreshkeyinterval(zone,
   1675   1.1  christos 						      cfg_obj_asuint32(obj)));
   1676   1.1  christos 
   1677   1.1  christos 		obj = NULL;
   1678   1.1  christos 		result = cfg_map_get(zoptions, "auto-dnssec", &obj);
   1679  1.11  christos 		if (kasp != NULL) {
   1680  1.11  christos 			bool s2i = (strcmp(dns_kasp_getname(kasp),
   1681  1.11  christos 					   "insecure") != 0);
   1682   1.7  christos 			dns_zone_setkeyopt(zone, DNS_ZONEKEY_ALLOW, true);
   1683  1.11  christos 			dns_zone_setkeyopt(zone, DNS_ZONEKEY_CREATE, !s2i);
   1684   1.7  christos 			dns_zone_setkeyopt(zone, DNS_ZONEKEY_MAINTAIN, true);
   1685   1.7  christos 		} else if (result == ISC_R_SUCCESS) {
   1686   1.1  christos 			const char *arg = cfg_obj_asstring(obj);
   1687   1.3  christos 			if (strcasecmp(arg, "allow") == 0) {
   1688   1.3  christos 				allow = true;
   1689   1.3  christos 			} else if (strcasecmp(arg, "maintain") == 0) {
   1690   1.3  christos 				allow = maint = true;
   1691   1.3  christos 			} else if (strcasecmp(arg, "off") == 0) {
   1692   1.7  christos 				/* Default */
   1693   1.3  christos 			} else {
   1694  1.12  christos 				UNREACHABLE();
   1695   1.3  christos 			}
   1696   1.1  christos 			dns_zone_setkeyopt(zone, DNS_ZONEKEY_ALLOW, allow);
   1697   1.7  christos 			dns_zone_setkeyopt(zone, DNS_ZONEKEY_CREATE, false);
   1698   1.1  christos 			dns_zone_setkeyopt(zone, DNS_ZONEKEY_MAINTAIN, maint);
   1699   1.1  christos 		}
   1700   1.1  christos 	}
   1701   1.1  christos 
   1702  1.12  christos 	if (ztype == dns_zone_secondary || ztype == dns_zone_mirror) {
   1703   1.1  christos 		RETERR(configure_zone_acl(zconfig, vconfig, config,
   1704   1.7  christos 					  allow_update_forwarding, ac, mayberaw,
   1705   1.7  christos 					  dns_zone_setforwardacl,
   1706   1.1  christos 					  dns_zone_clearforwardacl));
   1707   1.1  christos 	}
   1708   1.1  christos 
   1709   1.1  christos 	/*%
   1710  1.11  christos 	 * Configure parental agents, applies to primary and secondary zones.
   1711  1.11  christos 	 */
   1712  1.12  christos 	if (ztype == dns_zone_primary || ztype == dns_zone_secondary) {
   1713  1.11  christos 		obj = NULL;
   1714  1.11  christos 		(void)cfg_map_get(zoptions, "parental-agents", &obj);
   1715  1.11  christos 		if (obj != NULL) {
   1716  1.11  christos 			dns_ipkeylist_t ipkl;
   1717  1.11  christos 			dns_ipkeylist_init(&ipkl);
   1718  1.11  christos 			RETERR(named_config_getipandkeylist(
   1719  1.11  christos 				config, "parental-agents", obj, mctx, &ipkl));
   1720  1.11  christos 			result = dns_zone_setparentals(zone, ipkl.addrs,
   1721  1.11  christos 						       ipkl.keys, ipkl.count);
   1722  1.11  christos 			dns_ipkeylist_clear(mctx, &ipkl);
   1723  1.11  christos 			RETERR(result);
   1724  1.11  christos 		} else {
   1725  1.11  christos 			RETERR(dns_zone_setparentals(zone, NULL, NULL, 0));
   1726  1.11  christos 		}
   1727  1.11  christos 	}
   1728  1.11  christos 
   1729  1.11  christos 	/*%
   1730   1.1  christos 	 * Primary master functionality.
   1731   1.1  christos 	 */
   1732  1.12  christos 	if (ztype == dns_zone_primary) {
   1733   1.1  christos 		obj = NULL;
   1734   1.1  christos 		result = named_config_get(maps, "check-wildcard", &obj);
   1735   1.7  christos 		if (result == ISC_R_SUCCESS) {
   1736   1.1  christos 			check = cfg_obj_asboolean(obj);
   1737   1.7  christos 		} else {
   1738   1.3  christos 			check = false;
   1739   1.7  christos 		}
   1740   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_CHECKWILDCARD, check);
   1741   1.1  christos 
   1742   1.1  christos 		/*
   1743   1.1  christos 		 * With map files, the default is ignore duplicate
   1744   1.1  christos 		 * records.  With other master formats, the default is
   1745   1.1  christos 		 * taken from the global configuration.
   1746   1.1  christos 		 */
   1747   1.1  christos 		obj = NULL;
   1748   1.1  christos 		if (masterformat != dns_masterformat_map) {
   1749   1.1  christos 			result = named_config_get(maps, "check-dup-records",
   1750   1.1  christos 						  &obj);
   1751   1.1  christos 			INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1752   1.1  christos 			dupcheck = cfg_obj_asstring(obj);
   1753   1.1  christos 		} else {
   1754   1.1  christos 			result = named_config_get(nodefault,
   1755   1.7  christos 						  "check-dup-records", &obj);
   1756   1.7  christos 			if (result == ISC_R_SUCCESS) {
   1757   1.1  christos 				dupcheck = cfg_obj_asstring(obj);
   1758   1.7  christos 			} else {
   1759   1.1  christos 				dupcheck = "ignore";
   1760   1.7  christos 			}
   1761   1.1  christos 		}
   1762   1.1  christos 		if (strcasecmp(dupcheck, "warn") == 0) {
   1763   1.3  christos 			fail = false;
   1764   1.3  christos 			check = true;
   1765   1.1  christos 		} else if (strcasecmp(dupcheck, "fail") == 0) {
   1766   1.3  christos 			fail = check = true;
   1767   1.1  christos 		} else if (strcasecmp(dupcheck, "ignore") == 0) {
   1768   1.3  christos 			fail = check = false;
   1769   1.3  christos 		} else {
   1770  1.12  christos 			UNREACHABLE();
   1771   1.3  christos 		}
   1772   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_CHECKDUPRR, check);
   1773   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_CHECKDUPRRFAIL, fail);
   1774   1.1  christos 
   1775   1.1  christos 		obj = NULL;
   1776   1.1  christos 		result = named_config_get(maps, "check-mx", &obj);
   1777   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1778   1.1  christos 		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
   1779   1.3  christos 			fail = false;
   1780   1.3  christos 			check = true;
   1781   1.1  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
   1782   1.3  christos 			fail = check = true;
   1783   1.1  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
   1784   1.3  christos 			fail = check = false;
   1785   1.3  christos 		} else {
   1786  1.12  christos 			UNREACHABLE();
   1787   1.3  christos 		}
   1788   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_CHECKMX, check);
   1789   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_CHECKMXFAIL, fail);
   1790   1.1  christos 
   1791   1.1  christos 		/*
   1792   1.1  christos 		 * With map files, the default is *not* to check
   1793   1.1  christos 		 * integrity.  With other master formats, the default is
   1794   1.1  christos 		 * taken from the global configuration.
   1795   1.1  christos 		 */
   1796   1.1  christos 		obj = NULL;
   1797   1.1  christos 		if (masterformat != dns_masterformat_map) {
   1798   1.1  christos 			result = named_config_get(maps, "check-integrity",
   1799   1.1  christos 						  &obj);
   1800   1.1  christos 			INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1801   1.1  christos 			dns_zone_setoption(mayberaw, DNS_ZONEOPT_CHECKINTEGRITY,
   1802   1.1  christos 					   cfg_obj_asboolean(obj));
   1803   1.1  christos 		} else {
   1804   1.3  christos 			check = false;
   1805   1.1  christos 			result = named_config_get(nodefault, "check-integrity",
   1806   1.7  christos 						  &obj);
   1807   1.7  christos 			if (result == ISC_R_SUCCESS) {
   1808   1.1  christos 				check = cfg_obj_asboolean(obj);
   1809   1.7  christos 			}
   1810   1.1  christos 			dns_zone_setoption(mayberaw, DNS_ZONEOPT_CHECKINTEGRITY,
   1811   1.1  christos 					   check);
   1812   1.1  christos 		}
   1813   1.1  christos 
   1814   1.1  christos 		obj = NULL;
   1815   1.1  christos 		result = named_config_get(maps, "check-mx-cname", &obj);
   1816   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1817   1.1  christos 		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
   1818   1.3  christos 			warn = true;
   1819   1.3  christos 			ignore = false;
   1820   1.1  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
   1821   1.3  christos 			warn = ignore = false;
   1822   1.1  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
   1823   1.3  christos 			warn = ignore = true;
   1824   1.3  christos 		} else {
   1825  1.12  christos 			UNREACHABLE();
   1826   1.3  christos 		}
   1827   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_WARNMXCNAME, warn);
   1828   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_IGNOREMXCNAME, ignore);
   1829   1.1  christos 
   1830   1.1  christos 		obj = NULL;
   1831   1.1  christos 		result = named_config_get(maps, "check-srv-cname", &obj);
   1832   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1833   1.1  christos 		if (strcasecmp(cfg_obj_asstring(obj), "warn") == 0) {
   1834   1.3  christos 			warn = true;
   1835   1.3  christos 			ignore = false;
   1836   1.1  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "fail") == 0) {
   1837   1.3  christos 			warn = ignore = false;
   1838   1.1  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "ignore") == 0) {
   1839   1.3  christos 			warn = ignore = true;
   1840   1.3  christos 		} else {
   1841  1.12  christos 			UNREACHABLE();
   1842   1.3  christos 		}
   1843   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_WARNSRVCNAME, warn);
   1844   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_IGNORESRVCNAME,
   1845   1.1  christos 				   ignore);
   1846   1.1  christos 
   1847   1.1  christos 		obj = NULL;
   1848   1.1  christos 		result = named_config_get(maps, "dnssec-secure-to-insecure",
   1849   1.1  christos 					  &obj);
   1850   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1851   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_SECURETOINSECURE,
   1852   1.1  christos 				   cfg_obj_asboolean(obj));
   1853   1.1  christos 
   1854   1.1  christos 		obj = NULL;
   1855   1.1  christos 		result = cfg_map_get(zoptions, "dnssec-update-mode", &obj);
   1856   1.1  christos 		if (result == ISC_R_SUCCESS) {
   1857   1.1  christos 			const char *arg = cfg_obj_asstring(obj);
   1858   1.3  christos 			if (strcasecmp(arg, "no-resign") == 0) {
   1859   1.1  christos 				dns_zone_setkeyopt(zone, DNS_ZONEKEY_NORESIGN,
   1860   1.3  christos 						   true);
   1861   1.3  christos 			} else if (strcasecmp(arg, "maintain") == 0) {
   1862   1.7  christos 				/* Default */
   1863   1.3  christos 			} else {
   1864  1.12  christos 				UNREACHABLE();
   1865   1.3  christos 			}
   1866   1.1  christos 		}
   1867   1.1  christos 
   1868   1.1  christos 		obj = NULL;
   1869   1.1  christos 		result = named_config_get(maps, "serial-update-method", &obj);
   1870   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1871   1.7  christos 		if (strcasecmp(cfg_obj_asstring(obj), "unixtime") == 0) {
   1872   1.7  christos 			dns_zone_setserialupdatemethod(
   1873   1.7  christos 				zone, dns_updatemethod_unixtime);
   1874   1.7  christos 		} else if (strcasecmp(cfg_obj_asstring(obj), "date") == 0) {
   1875   1.1  christos 			dns_zone_setserialupdatemethod(zone,
   1876   1.1  christos 						       dns_updatemethod_date);
   1877   1.7  christos 		} else {
   1878   1.7  christos 			dns_zone_setserialupdatemethod(
   1879   1.7  christos 				zone, dns_updatemethod_increment);
   1880   1.7  christos 		}
   1881   1.1  christos 	}
   1882   1.1  christos 
   1883   1.1  christos 	/*
   1884   1.1  christos 	 * Configure slave functionality.
   1885   1.1  christos 	 */
   1886   1.1  christos 	switch (ztype) {
   1887   1.3  christos 	case dns_zone_mirror:
   1888   1.3  christos 		/*
   1889   1.3  christos 		 * Disable outgoing zone transfers for mirror zones unless they
   1890   1.3  christos 		 * are explicitly enabled by zone configuration.
   1891   1.3  christos 		 */
   1892   1.3  christos 		obj = NULL;
   1893   1.3  christos 		(void)cfg_map_get(zoptions, "allow-transfer", &obj);
   1894   1.3  christos 		if (obj == NULL) {
   1895   1.3  christos 			dns_acl_t *none;
   1896   1.3  christos 			RETERR(dns_acl_none(mctx, &none));
   1897   1.3  christos 			dns_zone_setxfracl(zone, none);
   1898   1.3  christos 			dns_acl_detach(&none);
   1899   1.3  christos 		}
   1900  1.12  christos 		FALLTHROUGH;
   1901  1.12  christos 	case dns_zone_secondary:
   1902   1.1  christos 	case dns_zone_stub:
   1903   1.1  christos 	case dns_zone_redirect:
   1904   1.1  christos 		count = 0;
   1905   1.1  christos 		obj = NULL;
   1906   1.9  christos 		(void)cfg_map_get(zoptions, "primaries", &obj);
   1907   1.9  christos 		if (obj == NULL) {
   1908   1.9  christos 			(void)cfg_map_get(zoptions, "masters", &obj);
   1909   1.9  christos 		}
   1910   1.9  christos 
   1911   1.3  christos 		/*
   1912   1.9  christos 		 * Use the built-in primary server list if one was not
   1913   1.3  christos 		 * explicitly specified and this is a root zone mirror.
   1914   1.3  christos 		 */
   1915   1.3  christos 		if (obj == NULL && ztype == dns_zone_mirror &&
   1916   1.3  christos 		    dns_name_equal(dns_zone_getorigin(zone), dns_rootname))
   1917   1.3  christos 		{
   1918  1.11  christos 			result = named_config_getremotesdef(
   1919  1.11  christos 				named_g_config, "primaries",
   1920   1.9  christos 				DEFAULT_IANA_ROOT_ZONE_PRIMARIES, &obj);
   1921   1.3  christos 			RETERR(result);
   1922   1.3  christos 		}
   1923   1.1  christos 		if (obj != NULL) {
   1924   1.1  christos 			dns_ipkeylist_t ipkl;
   1925   1.1  christos 			dns_ipkeylist_init(&ipkl);
   1926   1.1  christos 
   1927  1.11  christos 			RETERR(named_config_getipandkeylist(config, "primaries",
   1928  1.11  christos 							    obj, mctx, &ipkl));
   1929   1.9  christos 			result = dns_zone_setprimarieswithkeys(
   1930   1.7  christos 				mayberaw, ipkl.addrs, ipkl.keys, ipkl.count);
   1931   1.1  christos 			count = ipkl.count;
   1932   1.1  christos 			dns_ipkeylist_clear(mctx, &ipkl);
   1933   1.1  christos 			RETERR(result);
   1934   1.7  christos 		} else {
   1935   1.9  christos 			result = dns_zone_setprimaries(mayberaw, NULL, 0);
   1936   1.7  christos 		}
   1937   1.1  christos 		RETERR(result);
   1938   1.1  christos 
   1939   1.3  christos 		multi = false;
   1940   1.1  christos 		if (count > 1) {
   1941   1.1  christos 			obj = NULL;
   1942   1.1  christos 			result = named_config_get(maps, "multi-master", &obj);
   1943   1.1  christos 			INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1944   1.1  christos 			multi = cfg_obj_asboolean(obj);
   1945   1.1  christos 		}
   1946   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_MULTIMASTER, multi);
   1947   1.1  christos 
   1948   1.1  christos 		obj = NULL;
   1949   1.1  christos 		result = named_config_get(maps, "max-transfer-time-in", &obj);
   1950   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1951   1.1  christos 		dns_zone_setmaxxfrin(mayberaw, cfg_obj_asuint32(obj) * 60);
   1952   1.1  christos 
   1953   1.1  christos 		obj = NULL;
   1954   1.1  christos 		result = named_config_get(maps, "max-transfer-idle-in", &obj);
   1955   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1956   1.1  christos 		dns_zone_setidlein(mayberaw, cfg_obj_asuint32(obj) * 60);
   1957   1.1  christos 
   1958   1.1  christos 		obj = NULL;
   1959   1.1  christos 		result = named_config_get(maps, "max-refresh-time", &obj);
   1960   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1961   1.1  christos 		dns_zone_setmaxrefreshtime(mayberaw, cfg_obj_asuint32(obj));
   1962   1.1  christos 
   1963   1.1  christos 		obj = NULL;
   1964   1.1  christos 		result = named_config_get(maps, "min-refresh-time", &obj);
   1965   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1966   1.1  christos 		dns_zone_setminrefreshtime(mayberaw, cfg_obj_asuint32(obj));
   1967   1.1  christos 
   1968   1.1  christos 		obj = NULL;
   1969   1.1  christos 		result = named_config_get(maps, "max-retry-time", &obj);
   1970   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1971   1.1  christos 		dns_zone_setmaxretrytime(mayberaw, cfg_obj_asuint32(obj));
   1972   1.1  christos 
   1973   1.1  christos 		obj = NULL;
   1974   1.1  christos 		result = named_config_get(maps, "min-retry-time", &obj);
   1975   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1976   1.1  christos 		dns_zone_setminretrytime(mayberaw, cfg_obj_asuint32(obj));
   1977   1.1  christos 
   1978   1.1  christos 		obj = NULL;
   1979   1.1  christos 		result = named_config_get(maps, "transfer-source", &obj);
   1980   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1981   1.1  christos 		RETERR(dns_zone_setxfrsource4(mayberaw,
   1982   1.1  christos 					      cfg_obj_assockaddr(obj)));
   1983   1.1  christos 		dscp = cfg_obj_getdscp(obj);
   1984   1.7  christos 		if (dscp == -1) {
   1985   1.1  christos 			dscp = named_g_dscp;
   1986   1.7  christos 		}
   1987   1.1  christos 		RETERR(dns_zone_setxfrsource4dscp(mayberaw, dscp));
   1988   1.1  christos 		named_add_reserved_dispatch(named_g_server,
   1989   1.1  christos 					    cfg_obj_assockaddr(obj));
   1990   1.1  christos 
   1991   1.1  christos 		obj = NULL;
   1992   1.1  christos 		result = named_config_get(maps, "transfer-source-v6", &obj);
   1993   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   1994   1.1  christos 		RETERR(dns_zone_setxfrsource6(mayberaw,
   1995   1.1  christos 					      cfg_obj_assockaddr(obj)));
   1996   1.1  christos 		dscp = cfg_obj_getdscp(obj);
   1997   1.7  christos 		if (dscp == -1) {
   1998   1.1  christos 			dscp = named_g_dscp;
   1999   1.7  christos 		}
   2000   1.1  christos 		RETERR(dns_zone_setxfrsource6dscp(mayberaw, dscp));
   2001   1.1  christos 		named_add_reserved_dispatch(named_g_server,
   2002   1.1  christos 					    cfg_obj_assockaddr(obj));
   2003   1.1  christos 
   2004   1.1  christos 		obj = NULL;
   2005   1.1  christos 		result = named_config_get(maps, "alt-transfer-source", &obj);
   2006   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   2007   1.1  christos 		RETERR(dns_zone_setaltxfrsource4(mayberaw,
   2008   1.1  christos 						 cfg_obj_assockaddr(obj)));
   2009   1.1  christos 		dscp = cfg_obj_getdscp(obj);
   2010   1.7  christos 		if (dscp == -1) {
   2011   1.1  christos 			dscp = named_g_dscp;
   2012   1.7  christos 		}
   2013   1.1  christos 		RETERR(dns_zone_setaltxfrsource4dscp(mayberaw, dscp));
   2014   1.1  christos 
   2015   1.1  christos 		obj = NULL;
   2016   1.1  christos 		result = named_config_get(maps, "alt-transfer-source-v6", &obj);
   2017   1.1  christos 		INSIST(result == ISC_R_SUCCESS && obj != NULL);
   2018   1.1  christos 		RETERR(dns_zone_setaltxfrsource6(mayberaw,
   2019   1.1  christos 						 cfg_obj_assockaddr(obj)));
   2020   1.1  christos 		dscp = cfg_obj_getdscp(obj);
   2021   1.7  christos 		if (dscp == -1) {
   2022   1.1  christos 			dscp = named_g_dscp;
   2023   1.7  christos 		}
   2024   1.1  christos 		RETERR(dns_zone_setaltxfrsource6dscp(mayberaw, dscp));
   2025   1.1  christos 
   2026   1.1  christos 		obj = NULL;
   2027   1.1  christos 		(void)named_config_get(maps, "use-alt-transfer-source", &obj);
   2028   1.1  christos 		if (obj == NULL) {
   2029   1.1  christos 			/*
   2030   1.1  christos 			 * Default off when views are in use otherwise
   2031   1.1  christos 			 * on for BIND 8 compatibility.
   2032   1.1  christos 			 */
   2033   1.1  christos 			view = dns_zone_getview(zone);
   2034   1.1  christos 			if (view != NULL && strcmp(view->name, "_default") == 0)
   2035   1.7  christos 			{
   2036   1.3  christos 				alt = true;
   2037   1.7  christos 			} else {
   2038   1.3  christos 				alt = false;
   2039   1.7  christos 			}
   2040   1.7  christos 		} else {
   2041   1.1  christos 			alt = cfg_obj_asboolean(obj);
   2042   1.7  christos 		}
   2043   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_USEALTXFRSRC, alt);
   2044   1.1  christos 
   2045   1.1  christos 		obj = NULL;
   2046   1.1  christos 		(void)named_config_get(maps, "try-tcp-refresh", &obj);
   2047   1.1  christos 		dns_zone_setoption(mayberaw, DNS_ZONEOPT_TRYTCPREFRESH,
   2048   1.1  christos 				   cfg_obj_asboolean(obj));
   2049   1.1  christos 		break;
   2050   1.1  christos 
   2051   1.1  christos 	case dns_zone_staticstub:
   2052   1.1  christos 		RETERR(configure_staticstub(zoptions, zone, zname,
   2053   1.1  christos 					    default_dbtype));
   2054   1.1  christos 		break;
   2055   1.1  christos 
   2056   1.1  christos 	default:
   2057   1.1  christos 		break;
   2058   1.1  christos 	}
   2059   1.1  christos 
   2060   1.1  christos 	return (ISC_R_SUCCESS);
   2061   1.1  christos }
   2062   1.1  christos 
   2063   1.1  christos /*
   2064   1.1  christos  * Set up a DLZ zone as writeable
   2065   1.1  christos  */
   2066   1.1  christos isc_result_t
   2067   1.7  christos named_zone_configure_writeable_dlz(dns_dlzdb_t *dlzdatabase, dns_zone_t *zone,
   2068   1.7  christos 				   dns_rdataclass_t rdclass, dns_name_t *name) {
   2069   1.1  christos 	dns_db_t *db = NULL;
   2070   1.1  christos 	isc_time_t now;
   2071   1.1  christos 	isc_result_t result;
   2072   1.1  christos 
   2073   1.1  christos 	TIME_NOW(&now);
   2074   1.1  christos 
   2075   1.1  christos 	dns_zone_settype(zone, dns_zone_dlz);
   2076   1.1  christos 	result = dns_sdlz_setdb(dlzdatabase, rdclass, name, &db);
   2077   1.7  christos 	if (result != ISC_R_SUCCESS) {
   2078   1.1  christos 		return (result);
   2079   1.7  christos 	}
   2080   1.1  christos 	result = dns_zone_dlzpostload(zone, db);
   2081   1.1  christos 	dns_db_detach(&db);
   2082   1.1  christos 	return (result);
   2083   1.1  christos }
   2084   1.1  christos 
   2085   1.3  christos bool
   2086  1.12  christos named_zone_reusable(dns_zone_t *zone, const cfg_obj_t *zconfig) {
   2087   1.1  christos 	const cfg_obj_t *zoptions = NULL;
   2088   1.1  christos 	const cfg_obj_t *obj = NULL;
   2089   1.1  christos 	const char *cfilename;
   2090   1.1  christos 	const char *zfilename;
   2091   1.1  christos 	dns_zone_t *raw = NULL;
   2092   1.9  christos 	bool has_raw, inline_signing;
   2093   1.1  christos 	dns_zonetype_t ztype;
   2094   1.1  christos 
   2095   1.1  christos 	zoptions = cfg_tuple_get(zconfig, "options");
   2096   1.1  christos 
   2097   1.1  christos 	/*
   2098   1.1  christos 	 * We always reconfigure a static-stub zone for simplicity, assuming
   2099   1.1  christos 	 * the amount of data to be loaded is small.
   2100   1.1  christos 	 */
   2101   1.1  christos 	if (zonetype_fromconfig(zoptions) == dns_zone_staticstub) {
   2102   1.1  christos 		dns_zone_log(zone, ISC_LOG_DEBUG(1),
   2103   1.1  christos 			     "not reusable: staticstub");
   2104   1.3  christos 		return (false);
   2105   1.1  christos 	}
   2106   1.1  christos 
   2107   1.1  christos 	/* If there's a raw zone, use that for filename and type comparison */
   2108   1.1  christos 	dns_zone_getraw(zone, &raw);
   2109   1.1  christos 	if (raw != NULL) {
   2110   1.1  christos 		zfilename = dns_zone_getfile(raw);
   2111   1.1  christos 		ztype = dns_zone_gettype(raw);
   2112   1.1  christos 		dns_zone_detach(&raw);
   2113   1.3  christos 		has_raw = true;
   2114   1.1  christos 	} else {
   2115   1.1  christos 		zfilename = dns_zone_getfile(zone);
   2116   1.1  christos 		ztype = dns_zone_gettype(zone);
   2117   1.3  christos 		has_raw = false;
   2118   1.1  christos 	}
   2119   1.1  christos 
   2120  1.12  christos 	inline_signing = named_zone_inlinesigning(zconfig);
   2121   1.9  christos 	if (!inline_signing && has_raw) {
   2122   1.1  christos 		dns_zone_log(zone, ISC_LOG_DEBUG(1),
   2123   1.1  christos 			     "not reusable: old zone was inline-signing");
   2124   1.3  christos 		return (false);
   2125   1.9  christos 	} else if (inline_signing && !has_raw) {
   2126   1.1  christos 		dns_zone_log(zone, ISC_LOG_DEBUG(1),
   2127   1.1  christos 			     "not reusable: old zone was not inline-signing");
   2128   1.3  christos 		return (false);
   2129   1.1  christos 	}
   2130   1.1  christos 
   2131   1.1  christos 	if (zonetype_fromconfig(zoptions) != ztype) {
   2132   1.1  christos 		dns_zone_log(zone, ISC_LOG_DEBUG(1),
   2133   1.1  christos 			     "not reusable: type mismatch");
   2134   1.3  christos 		return (false);
   2135   1.1  christos 	}
   2136   1.1  christos 
   2137   1.1  christos 	obj = NULL;
   2138   1.1  christos 	(void)cfg_map_get(zoptions, "file", &obj);
   2139   1.7  christos 	if (obj != NULL) {
   2140   1.1  christos 		cfilename = cfg_obj_asstring(obj);
   2141   1.7  christos 	} else {
   2142   1.1  christos 		cfilename = NULL;
   2143   1.7  christos 	}
   2144   1.1  christos 	if (!((cfilename == NULL && zfilename == NULL) ||
   2145   1.1  christos 	      (cfilename != NULL && zfilename != NULL &&
   2146   1.1  christos 	       strcmp(cfilename, zfilename) == 0)))
   2147   1.1  christos 	{
   2148   1.1  christos 		dns_zone_log(zone, ISC_LOG_DEBUG(1),
   2149   1.1  christos 			     "not reusable: filename mismatch");
   2150   1.3  christos 		return (false);
   2151   1.1  christos 	}
   2152   1.1  christos 
   2153   1.3  christos 	return (true);
   2154   1.1  christos }
   2155   1.9  christos 
   2156   1.9  christos bool
   2157  1.12  christos named_zone_inlinesigning(const cfg_obj_t *zconfig) {
   2158   1.9  christos 	const cfg_obj_t *zoptions = NULL;
   2159   1.9  christos 	const cfg_obj_t *signing = NULL;
   2160   1.9  christos 	bool inline_signing = false;
   2161   1.9  christos 
   2162   1.9  christos 	zoptions = cfg_tuple_get(zconfig, "options");
   2163   1.9  christos 	inline_signing = (cfg_map_get(zoptions, "inline-signing", &signing) ==
   2164   1.9  christos 				  ISC_R_SUCCESS &&
   2165   1.9  christos 			  cfg_obj_asboolean(signing));
   2166   1.9  christos 
   2167   1.9  christos 	return (inline_signing);
   2168   1.9  christos }
   2169