2 * Copyright (C) 2004-2011 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.
18 /* $Id: aclconf.c,v 1.29.72.2 2011-06-17 23:47:11 tbox Exp $ */
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;
80 REQUIRE(actxp != NULL && *actxp != NULL);
85 isc_refcount_decrement(&actx->references, &refs);
87 for (dacl = ISC_LIST_HEAD(actx->named_acl_cache);
91 next = ISC_LIST_NEXT(dacl, nextincache);
92 ISC_LIST_UNLINK(actx->named_acl_cache, dacl,
94 dns_acl_detach(&dacl);
96 isc_mem_putanddetach(&actx->mctx, actx, sizeof(*actx));
103 * Find the definition of the named acl whose name is "name".
106 get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
108 const cfg_obj_t *acls = NULL;
109 const cfg_listelt_t *elt;
111 result = cfg_map_get(cctx, "acl", &acls);
112 if (result != ISC_R_SUCCESS)
114 for (elt = cfg_list_first(acls);
116 elt = cfg_list_next(elt)) {
117 const cfg_obj_t *acl = cfg_listelt_value(elt);
118 const char *aclname = cfg_obj_asstring(cfg_tuple_get(acl, "name"));
119 if (strcasecmp(aclname, name) == 0) {
121 *ret = cfg_tuple_get(acl, "value");
123 return (ISC_R_SUCCESS);
126 return (ISC_R_NOTFOUND);
130 convert_named_acl(const cfg_obj_t *nameobj, const cfg_obj_t *cctx,
131 isc_log_t *lctx, cfg_aclconfctx_t *ctx,
132 isc_mem_t *mctx, unsigned int nest_level,
136 const cfg_obj_t *cacl = NULL;
139 const char *aclname = cfg_obj_asstring(nameobj);
141 /* Look for an already-converted version. */
142 for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
144 dacl = ISC_LIST_NEXT(dacl, nextincache))
146 if (strcasecmp(aclname, dacl->name) == 0) {
147 if (ISC_MAGIC_VALID(dacl, LOOP_MAGIC)) {
148 cfg_obj_log(nameobj, lctx, ISC_LOG_ERROR,
149 "acl loop detected: %s", aclname);
150 return (ISC_R_FAILURE);
152 dns_acl_attach(dacl, target);
153 return (ISC_R_SUCCESS);
156 /* Not yet converted. Convert now. */
157 result = get_acl_def(cctx, aclname, &cacl);
158 if (result != ISC_R_SUCCESS) {
159 cfg_obj_log(nameobj, lctx, ISC_LOG_WARNING,
160 "undefined ACL '%s'", aclname);
164 * Add a loop detection element.
166 memset(&loop, 0, sizeof(loop));
167 ISC_LINK_INIT(&loop, nextincache);
168 DE_CONST(aclname, loop.name);
169 loop.magic = LOOP_MAGIC;
170 ISC_LIST_APPEND(ctx->named_acl_cache, &loop, nextincache);
171 result = cfg_acl_fromconfig(cacl, cctx, lctx, ctx, mctx,
173 ISC_LIST_UNLINK(ctx->named_acl_cache, &loop, nextincache);
176 if (result != ISC_R_SUCCESS)
178 dacl->name = isc_mem_strdup(dacl->mctx, aclname);
179 if (dacl->name == NULL)
180 return (ISC_R_NOMEMORY);
181 ISC_LIST_APPEND(ctx->named_acl_cache, dacl, nextincache);
182 dns_acl_attach(dacl, target);
183 return (ISC_R_SUCCESS);
187 convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
192 dns_fixedname_t fixname;
194 const char *txtname = cfg_obj_asstring(keyobj);
196 keylen = strlen(txtname);
197 isc_buffer_init(&buf, txtname, keylen);
198 isc_buffer_add(&buf, keylen);
199 dns_fixedname_init(&fixname);
200 result = dns_name_fromtext(dns_fixedname_name(&fixname), &buf,
201 dns_rootname, 0, NULL);
202 if (result != ISC_R_SUCCESS) {
203 cfg_obj_log(keyobj, lctx, ISC_LOG_WARNING,
204 "key name '%s' is not a valid domain name",
208 return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
212 * Recursively pre-parse an ACL definition to find the total number
213 * of non-IP-prefix elements (localhost, localnets, key) in all nested
214 * ACLs, so that the parent will have enough space allocated for the
215 * elements table after all the nested ACLs have been merged in to the
219 count_acl_elements(const cfg_obj_t *caml, const cfg_obj_t *cctx,
220 isc_boolean_t *has_negative)
222 const cfg_listelt_t *elt;
223 const cfg_obj_t *cacl = 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;
246 n += count_acl_elements(ce, cctx, &negative);
249 } else if (cfg_obj_isstring(ce)) {
250 const char *name = cfg_obj_asstring(ce);
251 if (strcasecmp(name, "localhost") == 0 ||
252 strcasecmp(name, "localnets") == 0) {
254 } else if (strcasecmp(name, "any") != 0 &&
255 strcasecmp(name, "none") != 0) {
256 result = get_acl_def(cctx, name, &cacl);
257 if (result == ISC_R_SUCCESS)
258 n += count_acl_elements(cacl, cctx,
268 cfg_acl_fromconfig(const cfg_obj_t *caml,
269 const cfg_obj_t *cctx,
271 cfg_aclconfctx_t *ctx,
273 unsigned int nest_level,
277 dns_acl_t *dacl = NULL, *inneracl = NULL;
278 dns_aclelement_t *de;
279 const cfg_listelt_t *elt;
280 dns_iptable_t *iptab;
281 int new_nest_level = 0;
284 new_nest_level = nest_level - 1;
286 REQUIRE(target != NULL);
287 REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
289 if (*target != NULL) {
291 * If target already points to an ACL, then we're being
292 * called recursively to configure a nested ACL. The
293 * nested ACL's contents should just be absorbed into its
296 dns_acl_attach(*target, &dacl);
297 dns_acl_detach(target);
300 * Need to allocate a new ACL structure. Count the items
301 * in the ACL definition that will require space in the
302 * elements table. (Note that if nest_level is nonzero,
303 * *everything* goes in the elements table.)
308 nelem = count_acl_elements(caml, cctx, NULL);
310 nelem = cfg_list_length(caml, ISC_FALSE);
312 result = dns_acl_create(mctx, nelem, &dacl);
313 if (result != ISC_R_SUCCESS)
318 for (elt = cfg_list_first(caml);
320 elt = cfg_list_next(elt)) {
321 const cfg_obj_t *ce = cfg_listelt_value(elt);
324 if (cfg_obj_istuple(ce)) {
325 /* This must be a negated element. */
326 ce = cfg_tuple_get(ce, "value");
328 dacl->has_negatives = ISC_TRUE;
333 * If nest_level is nonzero, then every element is
334 * to be stored as a separate, nested ACL rather than
335 * merged into the main iptable.
337 iptab = dacl->iptable;
339 if (nest_level != 0) {
340 result = dns_acl_create(mctx,
341 cfg_list_length(ce, ISC_FALSE),
343 if (result != ISC_R_SUCCESS)
345 iptab = de->nestedacl->iptable;
348 if (cfg_obj_isnetprefix(ce)) {
353 cfg_obj_asnetprefix(ce, &addr, &bitlen);
356 * If nesting ACLs (nest_level != 0), we negate
357 * the nestedacl element, not the iptable entry.
359 result = dns_iptable_addprefix(iptab, &addr, bitlen,
360 ISC_TF(nest_level != 0 || !neg));
361 if (result != ISC_R_SUCCESS)
364 if (nest_level > 0) {
365 de->type = dns_aclelementtype_nestedacl;
369 } else if (cfg_obj_islist(ce)) {
371 * If we're nesting ACLs, put the nested
372 * ACL onto the elements list; otherwise
373 * merge it into *this* ACL. We nest ACLs
374 * in two cases: 1) sortlist, 2) if the
375 * nested ACL contains negated members.
377 if (inneracl != NULL)
378 dns_acl_detach(&inneracl);
379 result = cfg_acl_fromconfig(ce, cctx, lctx,
380 ctx, mctx, new_nest_level,
382 if (result != ISC_R_SUCCESS)
385 if (nest_level > 0 || inneracl->has_negatives) {
386 de->type = dns_aclelementtype_nestedacl;
388 if (de->nestedacl != NULL)
389 dns_acl_detach(&de->nestedacl);
390 dns_acl_attach(inneracl,
392 dns_acl_detach(&inneracl);
395 dns_acl_merge(dacl, inneracl,
397 de += inneracl->length; /* elements added */
398 dns_acl_detach(&inneracl);
401 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
403 de->type = dns_aclelementtype_keyname;
405 dns_name_init(&de->keyname, NULL);
406 result = convert_keyname(ce, lctx, mctx,
408 if (result != ISC_R_SUCCESS)
410 } else if (cfg_obj_isstring(ce)) {
412 const char *name = cfg_obj_asstring(ce);
413 if (strcasecmp(name, "any") == 0) {
414 /* Iptable entry with zero bit length. */
415 result = dns_iptable_addprefix(iptab, NULL, 0,
416 ISC_TF(nest_level != 0 || !neg));
417 if (result != ISC_R_SUCCESS)
420 if (nest_level != 0) {
421 de->type = dns_aclelementtype_nestedacl;
425 } else if (strcasecmp(name, "none") == 0) {
428 * We don't unconditional set
429 * dacl->has_negatives and
430 * de->negative to true so we can handle
433 result = dns_iptable_addprefix(iptab, NULL, 0,
434 ISC_TF(nest_level != 0 || neg));
435 if (result != ISC_R_SUCCESS)
439 dacl->has_negatives = !neg;
441 if (nest_level != 0) {
442 de->type = dns_aclelementtype_nestedacl;
446 } else if (strcasecmp(name, "localhost") == 0) {
447 de->type = dns_aclelementtype_localhost;
449 } else if (strcasecmp(name, "localnets") == 0) {
450 de->type = dns_aclelementtype_localnets;
453 if (inneracl != NULL)
454 dns_acl_detach(&inneracl);
455 result = convert_named_acl(ce, cctx, lctx, ctx,
456 mctx, new_nest_level,
458 if (result != ISC_R_SUCCESS)
464 cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
465 "address match list contains "
466 "unsupported element type");
467 result = ISC_R_FAILURE;
472 * This should only be reached for localhost, localnets
473 * and keyname elements, and nested ACLs if nest_level is
474 * nonzero (i.e., in sortlists).
476 if (de->nestedacl != NULL &&
477 de->type != dns_aclelementtype_nestedacl)
478 dns_acl_detach(&de->nestedacl);
481 de->node_num = dacl->node_count;
485 INSIST(dacl->length <= dacl->alloc);
488 dns_acl_attach(dacl, target);
489 result = ISC_R_SUCCESS;
492 if (inneracl != NULL)
493 dns_acl_detach(&inneracl);
494 dns_acl_detach(&dacl);