2 * Copyright (C) 2004-2010 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.27.62.2 2010/08/13 23:46:28 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_init(cfg_aclconfctx_t *ctx) {
38 ISC_LIST_INIT(ctx->named_acl_cache);
42 cfg_aclconfctx_clear(cfg_aclconfctx_t *ctx) {
43 dns_acl_t *dacl, *next;
45 for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
49 next = ISC_LIST_NEXT(dacl, nextincache);
50 dns_acl_detach(&dacl);
55 cfg_aclconfctx_clone(cfg_aclconfctx_t *src, cfg_aclconfctx_t *dest) {
56 dns_acl_t *dacl, *next;
57 REQUIRE(src != NULL && dest != NULL);
59 cfg_aclconfctx_init(dest);
60 for (dacl = ISC_LIST_HEAD(src->named_acl_cache);
65 next = ISC_LIST_NEXT(dacl, nextincache);
66 dns_acl_attach(dacl, ©);
67 ISC_LIST_APPEND(dest->named_acl_cache, copy, nextincache);
72 * Find the definition of the named acl whose name is "name".
75 get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
77 const cfg_obj_t *acls = NULL;
78 const cfg_listelt_t *elt;
80 result = cfg_map_get(cctx, "acl", &acls);
81 if (result != ISC_R_SUCCESS)
83 for (elt = cfg_list_first(acls);
85 elt = cfg_list_next(elt)) {
86 const cfg_obj_t *acl = cfg_listelt_value(elt);
87 const char *aclname = cfg_obj_asstring(cfg_tuple_get(acl, "name"));
88 if (strcasecmp(aclname, name) == 0) {
90 *ret = cfg_tuple_get(acl, "value");
92 return (ISC_R_SUCCESS);
95 return (ISC_R_NOTFOUND);
99 convert_named_acl(const cfg_obj_t *nameobj, const cfg_obj_t *cctx,
100 isc_log_t *lctx, cfg_aclconfctx_t *ctx,
101 isc_mem_t *mctx, unsigned int nest_level,
105 const cfg_obj_t *cacl = NULL;
108 const char *aclname = cfg_obj_asstring(nameobj);
110 /* Look for an already-converted version. */
111 for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
113 dacl = ISC_LIST_NEXT(dacl, nextincache))
115 if (strcasecmp(aclname, dacl->name) == 0) {
116 if (ISC_MAGIC_VALID(dacl, LOOP_MAGIC)) {
117 cfg_obj_log(nameobj, lctx, ISC_LOG_ERROR,
118 "acl loop detected: %s", aclname);
119 return (ISC_R_FAILURE);
121 dns_acl_attach(dacl, target);
122 return (ISC_R_SUCCESS);
125 /* Not yet converted. Convert now. */
126 result = get_acl_def(cctx, aclname, &cacl);
127 if (result != ISC_R_SUCCESS) {
128 cfg_obj_log(nameobj, lctx, ISC_LOG_WARNING,
129 "undefined ACL '%s'", aclname);
133 * Add a loop detection element.
135 memset(&loop, 0, sizeof(loop));
136 ISC_LINK_INIT(&loop, nextincache);
137 DE_CONST(aclname, loop.name);
138 loop.magic = LOOP_MAGIC;
139 ISC_LIST_APPEND(ctx->named_acl_cache, &loop, nextincache);
140 result = cfg_acl_fromconfig(cacl, cctx, lctx, ctx, mctx,
142 ISC_LIST_UNLINK(ctx->named_acl_cache, &loop, nextincache);
145 if (result != ISC_R_SUCCESS)
147 dacl->name = isc_mem_strdup(dacl->mctx, aclname);
148 if (dacl->name == NULL)
149 return (ISC_R_NOMEMORY);
150 ISC_LIST_APPEND(ctx->named_acl_cache, dacl, nextincache);
151 dns_acl_attach(dacl, target);
152 return (ISC_R_SUCCESS);
156 convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
161 dns_fixedname_t fixname;
163 const char *txtname = cfg_obj_asstring(keyobj);
165 keylen = strlen(txtname);
166 isc_buffer_init(&buf, txtname, keylen);
167 isc_buffer_add(&buf, keylen);
168 dns_fixedname_init(&fixname);
169 result = dns_name_fromtext(dns_fixedname_name(&fixname), &buf,
170 dns_rootname, 0, NULL);
171 if (result != ISC_R_SUCCESS) {
172 cfg_obj_log(keyobj, lctx, ISC_LOG_WARNING,
173 "key name '%s' is not a valid domain name",
177 return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
181 * Recursively pre-parse an ACL definition to find the total number
182 * of non-IP-prefix elements (localhost, localnets, key) in all nested
183 * ACLs, so that the parent will have enough space allocated for the
184 * elements table after all the nested ACLs have been merged in to the
188 count_acl_elements(const cfg_obj_t *caml, const cfg_obj_t *cctx,
189 isc_boolean_t *has_negative)
191 const cfg_listelt_t *elt;
192 const cfg_obj_t *cacl = NULL;
196 if (has_negative != NULL)
197 *has_negative = ISC_FALSE;
199 for (elt = cfg_list_first(caml);
201 elt = cfg_list_next(elt)) {
202 const cfg_obj_t *ce = cfg_listelt_value(elt);
204 /* negated element; just get the value. */
205 if (cfg_obj_istuple(ce)) {
206 ce = cfg_tuple_get(ce, "value");
207 if (has_negative != NULL)
208 *has_negative = ISC_TRUE;
211 if (cfg_obj_istype(ce, &cfg_type_keyref)) {
213 } else if (cfg_obj_islist(ce)) {
214 isc_boolean_t negative;
215 n += count_acl_elements(ce, cctx, &negative);
218 } else if (cfg_obj_isstring(ce)) {
219 const char *name = cfg_obj_asstring(ce);
220 if (strcasecmp(name, "localhost") == 0 ||
221 strcasecmp(name, "localnets") == 0) {
223 } else if (strcasecmp(name, "any") != 0 &&
224 strcasecmp(name, "none") != 0) {
225 result = get_acl_def(cctx, name, &cacl);
226 if (result == ISC_R_SUCCESS)
227 n += count_acl_elements(cacl, cctx,
237 cfg_acl_fromconfig(const cfg_obj_t *caml,
238 const cfg_obj_t *cctx,
240 cfg_aclconfctx_t *ctx,
242 unsigned int nest_level,
246 dns_acl_t *dacl = NULL, *inneracl = NULL;
247 dns_aclelement_t *de;
248 const cfg_listelt_t *elt;
249 dns_iptable_t *iptab;
250 int new_nest_level = 0;
253 new_nest_level = nest_level - 1;
255 REQUIRE(target != NULL);
256 REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
258 if (*target != NULL) {
260 * If target already points to an ACL, then we're being
261 * called recursively to configure a nested ACL. The
262 * nested ACL's contents should just be absorbed into its
265 dns_acl_attach(*target, &dacl);
266 dns_acl_detach(target);
269 * Need to allocate a new ACL structure. Count the items
270 * in the ACL definition that will require space in the
271 * elements table. (Note that if nest_level is nonzero,
272 * *everything* goes in the elements table.)
277 nelem = count_acl_elements(caml, cctx, NULL);
279 nelem = cfg_list_length(caml, ISC_FALSE);
281 result = dns_acl_create(mctx, nelem, &dacl);
282 if (result != ISC_R_SUCCESS)
287 for (elt = cfg_list_first(caml);
289 elt = cfg_list_next(elt)) {
290 const cfg_obj_t *ce = cfg_listelt_value(elt);
293 if (cfg_obj_istuple(ce)) {
294 /* This must be a negated element. */
295 ce = cfg_tuple_get(ce, "value");
297 dacl->has_negatives = ISC_TRUE;
302 * If nest_level is nonzero, then every element is
303 * to be stored as a separate, nested ACL rather than
304 * merged into the main iptable.
306 iptab = dacl->iptable;
308 if (nest_level != 0) {
309 result = dns_acl_create(mctx,
310 cfg_list_length(ce, ISC_FALSE),
312 if (result != ISC_R_SUCCESS)
314 iptab = de->nestedacl->iptable;
317 if (cfg_obj_isnetprefix(ce)) {
322 cfg_obj_asnetprefix(ce, &addr, &bitlen);
325 * If nesting ACLs (nest_level != 0), we negate
326 * the nestedacl element, not the iptable entry.
328 result = dns_iptable_addprefix(iptab, &addr, bitlen,
329 ISC_TF(nest_level != 0 || !neg));
330 if (result != ISC_R_SUCCESS)
333 if (nest_level > 0) {
334 de->type = dns_aclelementtype_nestedacl;
338 } else if (cfg_obj_islist(ce)) {
340 * If we're nesting ACLs, put the nested
341 * ACL onto the elements list; otherwise
342 * merge it into *this* ACL. We nest ACLs
343 * in two cases: 1) sortlist, 2) if the
344 * nested ACL contains negated members.
346 if (inneracl != NULL)
347 dns_acl_detach(&inneracl);
348 result = cfg_acl_fromconfig(ce, cctx, lctx,
349 ctx, mctx, new_nest_level,
351 if (result != ISC_R_SUCCESS)
354 if (nest_level > 0 || inneracl->has_negatives) {
355 de->type = dns_aclelementtype_nestedacl;
357 if (de->nestedacl != NULL)
358 dns_acl_detach(&de->nestedacl);
359 dns_acl_attach(inneracl,
361 dns_acl_detach(&inneracl);
364 dns_acl_merge(dacl, inneracl,
366 de += inneracl->length; /* elements added */
367 dns_acl_detach(&inneracl);
370 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
372 de->type = dns_aclelementtype_keyname;
374 dns_name_init(&de->keyname, NULL);
375 result = convert_keyname(ce, lctx, mctx,
377 if (result != ISC_R_SUCCESS)
379 } else if (cfg_obj_isstring(ce)) {
381 const char *name = cfg_obj_asstring(ce);
382 if (strcasecmp(name, "any") == 0) {
383 /* Iptable entry with zero bit length. */
384 result = dns_iptable_addprefix(iptab, NULL, 0,
385 ISC_TF(nest_level != 0 || !neg));
386 if (result != ISC_R_SUCCESS)
389 if (nest_level != 0) {
390 de->type = dns_aclelementtype_nestedacl;
394 } else if (strcasecmp(name, "none") == 0) {
397 * We don't unconditional set
398 * dacl->has_negatives and
399 * de->negative to true so we can handle
402 result = dns_iptable_addprefix(iptab, NULL, 0,
403 ISC_TF(nest_level != 0 || neg));
404 if (result != ISC_R_SUCCESS)
408 dacl->has_negatives = !neg;
410 if (nest_level != 0) {
411 de->type = dns_aclelementtype_nestedacl;
415 } else if (strcasecmp(name, "localhost") == 0) {
416 de->type = dns_aclelementtype_localhost;
418 } else if (strcasecmp(name, "localnets") == 0) {
419 de->type = dns_aclelementtype_localnets;
422 if (inneracl != NULL)
423 dns_acl_detach(&inneracl);
424 result = convert_named_acl(ce, cctx, lctx, ctx,
425 mctx, new_nest_level,
427 if (result != ISC_R_SUCCESS)
433 cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
434 "address match list contains "
435 "unsupported element type");
436 result = ISC_R_FAILURE;
441 * This should only be reached for localhost, localnets
442 * and keyname elements, and nested ACLs if nest_level is
443 * nonzero (i.e., in sortlists).
445 if (de->nestedacl != NULL &&
446 de->type != dns_aclelementtype_nestedacl)
447 dns_acl_detach(&de->nestedacl);
450 de->node_num = dacl->node_count;
454 INSIST(dacl->length <= dacl->alloc);
457 dns_acl_attach(dacl, target);
458 result = ISC_R_SUCCESS;
461 if (inneracl != NULL)
462 dns_acl_detach(&inneracl);
463 dns_acl_detach(&dacl);