2 * Copyright (C) 2004-2012, 2014, 2016 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
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.
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.
23 #include <isc/string.h> /* Required for HP/UX (and others?) */
26 #include <isccfg/namedconf.h>
27 #include <isccfg/aclconf.h>
30 #include <dns/iptable.h>
31 #include <dns/fixedname.h>
34 #define LOOP_MAGIC ISC_MAGIC('L','O','O','P')
37 cfg_aclconfctx_create(isc_mem_t *mctx, cfg_aclconfctx_t **ret) {
39 cfg_aclconfctx_t *actx;
41 REQUIRE(mctx != NULL);
42 REQUIRE(ret != NULL && *ret == NULL);
44 actx = isc_mem_get(mctx, sizeof(*actx));
46 return (ISC_R_NOMEMORY);
48 result = isc_refcount_init(&actx->references, 1);
49 if (result != ISC_R_SUCCESS)
53 isc_mem_attach(mctx, &actx->mctx);
54 ISC_LIST_INIT(actx->named_acl_cache);
57 return (ISC_R_SUCCESS);
60 isc_mem_put(mctx, actx, sizeof(*actx));
65 cfg_aclconfctx_attach(cfg_aclconfctx_t *src, cfg_aclconfctx_t **dest) {
67 REQUIRE(dest != NULL && *dest == NULL);
69 isc_refcount_increment(&src->references, NULL);
74 cfg_aclconfctx_detach(cfg_aclconfctx_t **actxp) {
75 cfg_aclconfctx_t *actx;
76 dns_acl_t *dacl, *next;
79 REQUIRE(actxp != NULL && *actxp != NULL);
83 isc_refcount_decrement(&actx->references, &refs);
85 for (dacl = ISC_LIST_HEAD(actx->named_acl_cache);
89 next = ISC_LIST_NEXT(dacl, nextincache);
90 ISC_LIST_UNLINK(actx->named_acl_cache, dacl,
92 dns_acl_detach(&dacl);
94 isc_mem_putanddetach(&actx->mctx, actx, sizeof(*actx));
101 * Find the definition of the named acl whose name is "name".
104 get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
106 const cfg_obj_t *acls = NULL;
107 const cfg_listelt_t *elt;
109 result = cfg_map_get(cctx, "acl", &acls);
110 if (result != ISC_R_SUCCESS)
112 for (elt = cfg_list_first(acls);
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) {
119 *ret = cfg_tuple_get(acl, "value");
121 return (ISC_R_SUCCESS);
124 return (ISC_R_NOTFOUND);
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,
134 const cfg_obj_t *cacl = NULL;
137 const char *aclname = cfg_obj_asstring(nameobj);
139 /* Look for an already-converted version. */
140 for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
142 dacl = ISC_LIST_NEXT(dacl, nextincache))
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);
150 dns_acl_attach(dacl, target);
151 return (ISC_R_SUCCESS);
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);
162 * Add a loop detection element.
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,
171 ISC_LIST_UNLINK(ctx->named_acl_cache, &loop, nextincache);
174 if (result != ISC_R_SUCCESS)
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);
185 convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
190 dns_fixedname_t fixname;
192 const char *txtname = cfg_obj_asstring(keyobj);
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",
206 return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
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
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)
221 const cfg_listelt_t *elt;
225 REQUIRE(count != NULL);
227 if (has_negative != NULL)
228 *has_negative = ISC_FALSE;
230 for (elt = cfg_list_first(caml);
232 elt = cfg_list_next(elt)) {
233 const cfg_obj_t *ce = cfg_listelt_value(elt);
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;
242 if (cfg_obj_istype(ce, &cfg_type_keyref)) {
244 } else if (cfg_obj_islist(ce)) {
245 isc_boolean_t negative;
247 result = count_acl_elements(ce, cctx, lctx, ctx, mctx,
249 if (result != ISC_R_SUCCESS)
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)
261 } else if (strcasecmp(name, "any") != 0) {
262 dns_acl_t *inneracl = NULL;
264 * Convert any named acls we reference now if
265 * they have not already been converted.
267 result = convert_named_acl(ce, cctx, lctx, ctx,
269 if (result == ISC_R_SUCCESS) {
270 if (inneracl->has_negatives)
273 n += inneracl->length;
274 dns_acl_detach(&inneracl);
282 return (ISC_R_SUCCESS);
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,
291 return (cfg_acl_fromconfig2(caml, cctx, lctx, ctx, mctx,
292 nest_level, 0, 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)
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;
309 new_nest_level = nest_level - 1;
311 REQUIRE(target != NULL);
312 REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
314 if (*target != NULL) {
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
321 dns_acl_attach(*target, &dacl);
322 dns_acl_detach(target);
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.)
332 if (nest_level == 0) {
333 result = count_acl_elements(caml, cctx, lctx, ctx,
335 if (result != ISC_R_SUCCESS)
338 nelem = cfg_list_length(caml, ISC_FALSE);
340 result = dns_acl_create(mctx, nelem, &dacl);
341 if (result != ISC_R_SUCCESS)
346 for (elt = cfg_list_first(caml);
348 elt = cfg_list_next(elt)) {
349 const cfg_obj_t *ce = cfg_listelt_value(elt);
352 INSIST(dacl->length <= dacl->alloc);
354 if (cfg_obj_istuple(ce)) {
355 /* This must be a negated element. */
356 ce = cfg_tuple_get(ce, "value");
358 dacl->has_negatives = ISC_TRUE;
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.
367 iptab = dacl->iptable;
369 if (nest_level != 0) {
370 result = dns_acl_create(mctx,
371 cfg_list_length(ce, ISC_FALSE),
373 if (result != ISC_R_SUCCESS)
375 iptab = de->nestedacl->iptable;
378 if (cfg_obj_isnetprefix(ce)) {
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; "
391 dns_acl_detach(&de->nestedacl);
396 * If nesting ACLs (nest_level != 0), we negate
397 * the nestedacl element, not the iptable entry.
399 result = dns_iptable_addprefix(iptab, &addr, bitlen,
400 ISC_TF(nest_level != 0 || !neg));
401 if (result != ISC_R_SUCCESS)
404 if (nest_level > 0) {
405 INSIST(dacl->length < dacl->alloc);
406 de->type = dns_aclelementtype_nestedacl;
410 } else if (cfg_obj_islist(ce)) {
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.
418 if (inneracl != NULL)
419 dns_acl_detach(&inneracl);
420 result = cfg_acl_fromconfig(ce, cctx, lctx,
421 ctx, mctx, new_nest_level,
423 if (result != ISC_R_SUCCESS)
426 if (nest_level > 0 || inneracl->has_negatives) {
427 INSIST(dacl->length < dacl->alloc);
428 de->type = dns_aclelementtype_nestedacl;
430 if (de->nestedacl != NULL)
431 dns_acl_detach(&de->nestedacl);
432 dns_acl_attach(inneracl,
434 dns_acl_detach(&inneracl);
437 INSIST(dacl->length + inneracl->length
439 dns_acl_merge(dacl, inneracl,
441 de += inneracl->length; /* elements added */
442 dns_acl_detach(&inneracl);
443 INSIST(dacl->length <= dacl->alloc);
446 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
448 INSIST(dacl->length < dacl->alloc);
449 de->type = dns_aclelementtype_keyname;
451 dns_name_init(&de->keyname, NULL);
452 result = convert_keyname(ce, lctx, mctx,
454 if (result != ISC_R_SUCCESS)
456 } else if (cfg_obj_isstring(ce)) {
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)
466 if (nest_level != 0) {
467 INSIST(dacl->length < dacl->alloc);
468 de->type = dns_aclelementtype_nestedacl;
472 } else if (strcasecmp(name, "none") == 0) {
475 * We don't unconditional set
476 * dacl->has_negatives and
477 * de->negative to true so we can handle
480 result = dns_iptable_addprefix(iptab, NULL, 0,
481 ISC_TF(nest_level != 0 || neg));
482 if (result != ISC_R_SUCCESS)
486 dacl->has_negatives = !neg;
488 if (nest_level != 0) {
489 INSIST(dacl->length < dacl->alloc);
490 de->type = dns_aclelementtype_nestedacl;
494 } else if (strcasecmp(name, "localhost") == 0) {
495 INSIST(dacl->length < dacl->alloc);
496 de->type = dns_aclelementtype_localhost;
498 } else if (strcasecmp(name, "localnets") == 0) {
499 INSIST(dacl->length < dacl->alloc);
500 de->type = dns_aclelementtype_localnets;
503 if (inneracl != NULL)
504 dns_acl_detach(&inneracl);
506 * This call should just find the cached
509 result = convert_named_acl(ce, cctx, lctx, ctx,
510 mctx, new_nest_level,
512 if (result != ISC_R_SUCCESS)
518 cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
519 "address match list contains "
520 "unsupported element type");
521 result = ISC_R_FAILURE;
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).
530 if (de->nestedacl != NULL &&
531 de->type != dns_aclelementtype_nestedacl)
532 dns_acl_detach(&de->nestedacl);
535 de->node_num = dacl->node_count;
539 INSIST(dacl->length <= dacl->alloc);
542 dns_acl_attach(dacl, target);
543 result = ISC_R_SUCCESS;
546 if (inneracl != NULL)
547 dns_acl_detach(&inneracl);
548 dns_acl_detach(&dacl);