1 /*
2  * Copyright (C) 2004-2012, 2014, 2016  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2002  Internet Software Consortium.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15  * PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /* $Id$ */
19 
20 #include <config.h>
21 
22 #include <isc/mem.h>
23 #include <isc/string.h>		/* Required for HP/UX (and others?) */
24 #include <isc/util.h>
25 
26 #include <isccfg/namedconf.h>
27 #include <isccfg/aclconf.h>
28 
29 #include <dns/acl.h>
30 #include <dns/iptable.h>
31 #include <dns/fixedname.h>
32 #include <dns/log.h>
33 
34 #define LOOP_MAGIC ISC_MAGIC('L','O','O','P')
35 
36 isc_result_t
cfg_aclconfctx_create(isc_mem_t * mctx,cfg_aclconfctx_t ** ret)37 cfg_aclconfctx_create(isc_mem_t *mctx, cfg_aclconfctx_t **ret) {
38 	isc_result_t result;
39 	cfg_aclconfctx_t *actx;
40 
41 	REQUIRE(mctx != NULL);
42 	REQUIRE(ret != NULL && *ret == NULL);
43 
44 	actx = isc_mem_get(mctx, sizeof(*actx));
45 	if (actx == NULL)
46 		return (ISC_R_NOMEMORY);
47 
48 	result = isc_refcount_init(&actx->references, 1);
49 	if (result != ISC_R_SUCCESS)
50 		goto cleanup;
51 
52 	actx->mctx = NULL;
53 	isc_mem_attach(mctx, &actx->mctx);
54 	ISC_LIST_INIT(actx->named_acl_cache);
55 
56 	*ret = actx;
57 	return (ISC_R_SUCCESS);
58 
59  cleanup:
60 	isc_mem_put(mctx, actx, sizeof(*actx));
61 	return (result);
62 }
63 
64 void
cfg_aclconfctx_attach(cfg_aclconfctx_t * src,cfg_aclconfctx_t ** dest)65 cfg_aclconfctx_attach(cfg_aclconfctx_t *src, cfg_aclconfctx_t **dest) {
66 	REQUIRE(src != NULL);
67 	REQUIRE(dest != NULL && *dest == NULL);
68 
69 	isc_refcount_increment(&src->references, NULL);
70 	*dest = src;
71 }
72 
73 void
cfg_aclconfctx_detach(cfg_aclconfctx_t ** actxp)74 cfg_aclconfctx_detach(cfg_aclconfctx_t **actxp) {
75 	cfg_aclconfctx_t *actx;
76 	dns_acl_t *dacl, *next;
77 	unsigned int refs;
78 
79 	REQUIRE(actxp != NULL && *actxp != NULL);
80 
81 	actx = *actxp;
82 
83 	isc_refcount_decrement(&actx->references, &refs);
84 	if (refs == 0) {
85 		for (dacl = ISC_LIST_HEAD(actx->named_acl_cache);
86 		     dacl != NULL;
87 		     dacl = next)
88 		{
89 			next = ISC_LIST_NEXT(dacl, nextincache);
90 			ISC_LIST_UNLINK(actx->named_acl_cache, dacl,
91 					nextincache);
92 			dns_acl_detach(&dacl);
93 		}
94 		isc_mem_putanddetach(&actx->mctx, actx, sizeof(*actx));
95 	}
96 
97 	*actxp = NULL;
98 }
99 
100 /*
101  * Find the definition of the named acl whose name is "name".
102  */
103 static isc_result_t
get_acl_def(const cfg_obj_t * cctx,const char * name,const cfg_obj_t ** ret)104 get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
105 	isc_result_t result;
106 	const cfg_obj_t *acls = NULL;
107 	const cfg_listelt_t *elt;
108 
109 	result = cfg_map_get(cctx, "acl", &acls);
110 	if (result != ISC_R_SUCCESS)
111 		return (result);
112 	for (elt = cfg_list_first(acls);
113 	     elt != NULL;
114 	     elt = cfg_list_next(elt)) {
115 		const cfg_obj_t *acl = cfg_listelt_value(elt);
116 		const char *aclname = cfg_obj_asstring(cfg_tuple_get(acl, "name"));
117 		if (strcasecmp(aclname, name) == 0) {
118 			if (ret != NULL) {
119 				*ret = cfg_tuple_get(acl, "value");
120 			}
121 			return (ISC_R_SUCCESS);
122 		}
123 	}
124 	return (ISC_R_NOTFOUND);
125 }
126 
127 static isc_result_t
convert_named_acl(const cfg_obj_t * nameobj,const cfg_obj_t * cctx,isc_log_t * lctx,cfg_aclconfctx_t * ctx,isc_mem_t * mctx,unsigned int nest_level,dns_acl_t ** target)128 convert_named_acl(const cfg_obj_t *nameobj, const cfg_obj_t *cctx,
129 		  isc_log_t *lctx, cfg_aclconfctx_t *ctx,
130 		  isc_mem_t *mctx, unsigned int nest_level,
131 		  dns_acl_t **target)
132 {
133 	isc_result_t result;
134 	const cfg_obj_t *cacl = NULL;
135 	dns_acl_t *dacl;
136 	dns_acl_t loop;
137 	const char *aclname = cfg_obj_asstring(nameobj);
138 
139 	/* Look for an already-converted version. */
140 	for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
141 	     dacl != NULL;
142 	     dacl = ISC_LIST_NEXT(dacl, nextincache))
143 	{
144 		if (strcasecmp(aclname, dacl->name) == 0) {
145 			if (ISC_MAGIC_VALID(dacl, LOOP_MAGIC)) {
146 				cfg_obj_log(nameobj, lctx, ISC_LOG_ERROR,
147 					    "acl loop detected: %s", aclname);
148 				return (ISC_R_FAILURE);
149 			}
150 			dns_acl_attach(dacl, target);
151 			return (ISC_R_SUCCESS);
152 		}
153 	}
154 	/* Not yet converted.  Convert now. */
155 	result = get_acl_def(cctx, aclname, &cacl);
156 	if (result != ISC_R_SUCCESS) {
157 		cfg_obj_log(nameobj, lctx, ISC_LOG_WARNING,
158 			    "undefined ACL '%s'", aclname);
159 		return (result);
160 	}
161 	/*
162 	 * Add a loop detection element.
163 	 */
164 	memset(&loop, 0, sizeof(loop));
165 	ISC_LINK_INIT(&loop, nextincache);
166 	DE_CONST(aclname, loop.name);
167 	loop.magic = LOOP_MAGIC;
168 	ISC_LIST_APPEND(ctx->named_acl_cache, &loop, nextincache);
169 	result = cfg_acl_fromconfig(cacl, cctx, lctx, ctx, mctx,
170 				    nest_level, &dacl);
171 	ISC_LIST_UNLINK(ctx->named_acl_cache, &loop, nextincache);
172 	loop.magic = 0;
173 	loop.name = NULL;
174 	if (result != ISC_R_SUCCESS)
175 		return (result);
176 	dacl->name = isc_mem_strdup(dacl->mctx, aclname);
177 	if (dacl->name == NULL)
178 		return (ISC_R_NOMEMORY);
179 	ISC_LIST_APPEND(ctx->named_acl_cache, dacl, nextincache);
180 	dns_acl_attach(dacl, target);
181 	return (ISC_R_SUCCESS);
182 }
183 
184 static isc_result_t
convert_keyname(const cfg_obj_t * keyobj,isc_log_t * lctx,isc_mem_t * mctx,dns_name_t * dnsname)185 convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
186 		dns_name_t *dnsname)
187 {
188 	isc_result_t result;
189 	isc_buffer_t buf;
190 	dns_fixedname_t fixname;
191 	unsigned int keylen;
192 	const char *txtname = cfg_obj_asstring(keyobj);
193 
194 	keylen = strlen(txtname);
195 	isc_buffer_constinit(&buf, txtname, keylen);
196 	isc_buffer_add(&buf, keylen);
197 	dns_fixedname_init(&fixname);
198 	result = dns_name_fromtext(dns_fixedname_name(&fixname), &buf,
199 				   dns_rootname, 0, NULL);
200 	if (result != ISC_R_SUCCESS) {
201 		cfg_obj_log(keyobj, lctx, ISC_LOG_WARNING,
202 			    "key name '%s' is not a valid domain name",
203 			    txtname);
204 		return (result);
205 	}
206 	return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
207 }
208 
209 /*
210  * Recursively pre-parse an ACL definition to find the total number
211  * of non-IP-prefix elements (localhost, localnets, key) in all nested
212  * ACLs, so that the parent will have enough space allocated for the
213  * elements table after all the nested ACLs have been merged in to the
214  * parent.
215  */
216 static isc_result_t
count_acl_elements(const cfg_obj_t * caml,const cfg_obj_t * cctx,isc_log_t * lctx,cfg_aclconfctx_t * ctx,isc_mem_t * mctx,isc_uint32_t * count,isc_boolean_t * has_negative)217 count_acl_elements(const cfg_obj_t *caml, const cfg_obj_t *cctx,
218 		   isc_log_t *lctx, cfg_aclconfctx_t *ctx, isc_mem_t *mctx,
219 		   isc_uint32_t *count, isc_boolean_t *has_negative)
220 {
221 	const cfg_listelt_t *elt;
222 	isc_result_t result;
223 	isc_uint32_t n = 0;
224 
225 	REQUIRE(count != NULL);
226 
227 	if (has_negative != NULL)
228 		*has_negative = ISC_FALSE;
229 
230 	for (elt = cfg_list_first(caml);
231 	     elt != NULL;
232 	     elt = cfg_list_next(elt)) {
233 		const cfg_obj_t *ce = cfg_listelt_value(elt);
234 
235 		/* negated element; just get the value. */
236 		if (cfg_obj_istuple(ce)) {
237 			ce = cfg_tuple_get(ce, "value");
238 			if (has_negative != NULL)
239 				*has_negative = ISC_TRUE;
240 		}
241 
242 		if (cfg_obj_istype(ce, &cfg_type_keyref)) {
243 			n++;
244 		} else if (cfg_obj_islist(ce)) {
245 			isc_boolean_t negative;
246 			isc_uint32_t sub;
247 			result = count_acl_elements(ce, cctx, lctx, ctx, mctx,
248 						    &sub, &negative);
249 			if (result != ISC_R_SUCCESS)
250 				return (result);
251 			n += sub;
252 			if (negative)
253 				n++;
254 		} else if (cfg_obj_isstring(ce)) {
255 			const char *name = cfg_obj_asstring(ce);
256 			if (strcasecmp(name, "localhost") == 0 ||
257 			    strcasecmp(name, "localnets") == 0 ||
258 			    strcasecmp(name, "none") == 0)
259 			{
260 				n++;
261 			} else if (strcasecmp(name, "any") != 0) {
262 				dns_acl_t *inneracl = NULL;
263 				/*
264 				 * Convert any named acls we reference now if
265 				 * they have not already been converted.
266 				 */
267 				result = convert_named_acl(ce, cctx, lctx, ctx,
268 							   mctx, 0, &inneracl);
269 				if (result == ISC_R_SUCCESS) {
270 					if (inneracl->has_negatives)
271 						n++;
272 					else
273 						n += inneracl->length;
274 					dns_acl_detach(&inneracl);
275 				} else
276 					return (result);
277 			}
278 		}
279 	}
280 
281 	*count = n;
282 	return (ISC_R_SUCCESS);
283 }
284 
285 isc_result_t
cfg_acl_fromconfig(const cfg_obj_t * caml,const cfg_obj_t * cctx,isc_log_t * lctx,cfg_aclconfctx_t * ctx,isc_mem_t * mctx,unsigned int nest_level,dns_acl_t ** target)286 cfg_acl_fromconfig(const cfg_obj_t *caml, const cfg_obj_t *cctx,
287 		   isc_log_t *lctx, cfg_aclconfctx_t *ctx,
288 		   isc_mem_t *mctx, unsigned int nest_level,
289 		   dns_acl_t **target)
290 {
291 	return (cfg_acl_fromconfig2(caml, cctx, lctx, ctx, mctx,
292 				    nest_level, 0, target));
293 }
294 
295 isc_result_t
cfg_acl_fromconfig2(const cfg_obj_t * caml,const cfg_obj_t * cctx,isc_log_t * lctx,cfg_aclconfctx_t * ctx,isc_mem_t * mctx,unsigned int nest_level,isc_uint16_t family,dns_acl_t ** target)296 cfg_acl_fromconfig2(const cfg_obj_t *caml, const cfg_obj_t *cctx,
297 		   isc_log_t *lctx, cfg_aclconfctx_t *ctx,
298 		   isc_mem_t *mctx, unsigned int nest_level,
299 		   isc_uint16_t family, dns_acl_t **target)
300 {
301 	isc_result_t result;
302 	dns_acl_t *dacl = NULL, *inneracl = NULL;
303 	dns_aclelement_t *de;
304 	const cfg_listelt_t *elt;
305 	dns_iptable_t *iptab;
306 	int new_nest_level = 0;
307 
308 	if (nest_level != 0)
309 		new_nest_level = nest_level - 1;
310 
311 	REQUIRE(target != NULL);
312 	REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
313 
314 	if (*target != NULL) {
315 		/*
316 		 * If target already points to an ACL, then we're being
317 		 * called recursively to configure a nested ACL.  The
318 		 * nested ACL's contents should just be absorbed into its
319 		 * parent ACL.
320 		 */
321 		dns_acl_attach(*target, &dacl);
322 		dns_acl_detach(target);
323 	} else {
324 		/*
325 		 * Need to allocate a new ACL structure.  Count the items
326 		 * in the ACL definition that will require space in the
327 		 * elements table.  (Note that if nest_level is nonzero,
328 		 * *everything* goes in the elements table.)
329 		 */
330 		isc_uint32_t nelem;
331 
332 		if (nest_level == 0) {
333 			result = count_acl_elements(caml, cctx, lctx, ctx,
334 						    mctx, &nelem, NULL);
335 			if (result != ISC_R_SUCCESS)
336 				return (result);
337 		} else
338 			nelem = cfg_list_length(caml, ISC_FALSE);
339 
340 		result = dns_acl_create(mctx, nelem, &dacl);
341 		if (result != ISC_R_SUCCESS)
342 			return (result);
343 	}
344 
345 	de = dacl->elements;
346 	for (elt = cfg_list_first(caml);
347 	     elt != NULL;
348 	     elt = cfg_list_next(elt)) {
349 		const cfg_obj_t *ce = cfg_listelt_value(elt);
350 		isc_boolean_t	neg;
351 
352 		INSIST(dacl->length <= dacl->alloc);
353 
354 		if (cfg_obj_istuple(ce)) {
355 			/* This must be a negated element. */
356 			ce = cfg_tuple_get(ce, "value");
357 			neg = ISC_TRUE;
358 			dacl->has_negatives = ISC_TRUE;
359 		} else
360 			neg = ISC_FALSE;
361 
362 		/*
363 		 * If nest_level is nonzero, then every element is
364 		 * to be stored as a separate, nested ACL rather than
365 		 * merged into the main iptable.
366 		 */
367 		iptab = dacl->iptable;
368 
369 		if (nest_level != 0) {
370 			result = dns_acl_create(mctx,
371 						cfg_list_length(ce, ISC_FALSE),
372 						&de->nestedacl);
373 			if (result != ISC_R_SUCCESS)
374 				goto cleanup;
375 			iptab = de->nestedacl->iptable;
376 		}
377 
378 		if (cfg_obj_isnetprefix(ce)) {
379 			/* Network prefix */
380 			isc_netaddr_t	addr;
381 			unsigned int	bitlen;
382 
383 			cfg_obj_asnetprefix(ce, &addr, &bitlen);
384 			if (family != 0 && family != addr.family) {
385 				char buf[ISC_NETADDR_FORMATSIZE + 1];
386 				isc_netaddr_format(&addr, buf, sizeof(buf));
387 				cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
388 					    "'%s': incorrect address family; "
389 					    "ignoring", buf);
390 				if (nest_level != 0)
391 					dns_acl_detach(&de->nestedacl);
392 				continue;
393 			}
394 
395 			/*
396 			 * If nesting ACLs (nest_level != 0), we negate
397 			 * the nestedacl element, not the iptable entry.
398 			 */
399 			result = dns_iptable_addprefix(iptab, &addr, bitlen,
400 					      ISC_TF(nest_level != 0 || !neg));
401 			if (result != ISC_R_SUCCESS)
402 				goto cleanup;
403 
404 			if (nest_level > 0) {
405 				INSIST(dacl->length < dacl->alloc);
406 				de->type = dns_aclelementtype_nestedacl;
407 				de->negative = neg;
408 			} else
409 				continue;
410 		} else if (cfg_obj_islist(ce)) {
411 			/*
412 			 * If we're nesting ACLs, put the nested
413 			 * ACL onto the elements list; otherwise
414 			 * merge it into *this* ACL.  We nest ACLs
415 			 * in two cases: 1) sortlist, 2) if the
416 			 * nested ACL contains negated members.
417 			 */
418 			if (inneracl != NULL)
419 				dns_acl_detach(&inneracl);
420 			result = cfg_acl_fromconfig(ce, cctx, lctx,
421 						    ctx, mctx, new_nest_level,
422 						    &inneracl);
423 			if (result != ISC_R_SUCCESS)
424 				goto cleanup;
425 nested_acl:
426 			if (nest_level > 0 || inneracl->has_negatives) {
427 				INSIST(dacl->length < dacl->alloc);
428 				de->type = dns_aclelementtype_nestedacl;
429 				de->negative = neg;
430 				if (de->nestedacl != NULL)
431 					dns_acl_detach(&de->nestedacl);
432 				dns_acl_attach(inneracl,
433 					       &de->nestedacl);
434 				dns_acl_detach(&inneracl);
435 				/* Fall through. */
436 			} else {
437 				INSIST(dacl->length + inneracl->length
438 				       <= dacl->alloc);
439 				dns_acl_merge(dacl, inneracl,
440 					      ISC_TF(!neg));
441 				de += inneracl->length;  /* elements added */
442 				dns_acl_detach(&inneracl);
443 				INSIST(dacl->length <= dacl->alloc);
444 				continue;
445 			}
446 		} else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
447 			/* Key name. */
448 			INSIST(dacl->length < dacl->alloc);
449 			de->type = dns_aclelementtype_keyname;
450 			de->negative = neg;
451 			dns_name_init(&de->keyname, NULL);
452 			result = convert_keyname(ce, lctx, mctx,
453 						 &de->keyname);
454 			if (result != ISC_R_SUCCESS)
455 				goto cleanup;
456 		} else if (cfg_obj_isstring(ce)) {
457 			/* ACL name. */
458 			const char *name = cfg_obj_asstring(ce);
459 			if (strcasecmp(name, "any") == 0) {
460 				/* Iptable entry with zero bit length. */
461 				result = dns_iptable_addprefix(iptab, NULL, 0,
462 					      ISC_TF(nest_level != 0 || !neg));
463 				if (result != ISC_R_SUCCESS)
464 					goto cleanup;
465 
466 				if (nest_level != 0) {
467 					INSIST(dacl->length < dacl->alloc);
468 					de->type = dns_aclelementtype_nestedacl;
469 					de->negative = neg;
470 				} else
471 					continue;
472 			} else if (strcasecmp(name, "none") == 0) {
473 				/* none == !any */
474 				/*
475 				 * We don't unconditional set
476 				 * dacl->has_negatives and
477 				 * de->negative to true so we can handle
478 				 * "!none;".
479 				 */
480 				result = dns_iptable_addprefix(iptab, NULL, 0,
481 					      ISC_TF(nest_level != 0 || neg));
482 				if (result != ISC_R_SUCCESS)
483 					goto cleanup;
484 
485 				if (!neg)
486 					dacl->has_negatives = !neg;
487 
488 				if (nest_level != 0) {
489 					INSIST(dacl->length < dacl->alloc);
490 					de->type = dns_aclelementtype_nestedacl;
491 					de->negative = !neg;
492 				} else
493 					continue;
494 			} else if (strcasecmp(name, "localhost") == 0) {
495 				INSIST(dacl->length < dacl->alloc);
496 				de->type = dns_aclelementtype_localhost;
497 				de->negative = neg;
498 			} else if (strcasecmp(name, "localnets") == 0) {
499 				INSIST(dacl->length < dacl->alloc);
500 				de->type = dns_aclelementtype_localnets;
501 				de->negative = neg;
502 			} else {
503 				if (inneracl != NULL)
504 					dns_acl_detach(&inneracl);
505 				/*
506 				 * This call should just find the cached
507 				 * of the named acl.
508 				 */
509 				result = convert_named_acl(ce, cctx, lctx, ctx,
510 							   mctx, new_nest_level,
511 							   &inneracl);
512 				if (result != ISC_R_SUCCESS)
513 					goto cleanup;
514 
515 				goto nested_acl;
516 			}
517 		} else {
518 			cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
519 				    "address match list contains "
520 				    "unsupported element type");
521 			result = ISC_R_FAILURE;
522 			goto cleanup;
523 		}
524 
525 		/*
526 		 * This should only be reached for localhost, localnets
527 		 * and keyname elements, and nested ACLs if nest_level is
528 		 * nonzero (i.e., in sortlists).
529 		 */
530 		if (de->nestedacl != NULL &&
531 		    de->type != dns_aclelementtype_nestedacl)
532 			dns_acl_detach(&de->nestedacl);
533 
534 		dacl->node_count++;
535 		de->node_num = dacl->node_count;
536 
537 		dacl->length++;
538 		de++;
539 		INSIST(dacl->length <= dacl->alloc);
540 	}
541 
542 	dns_acl_attach(dacl, target);
543 	result = ISC_R_SUCCESS;
544 
545  cleanup:
546 	if (inneracl != NULL)
547 		dns_acl_detach(&inneracl);
548 	dns_acl_detach(&dacl);
549 	return (result);
550 }
551