2 * Copyright (C) 2004-2009 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.22.34.2 2009/01/18 23:47:41 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_destroy(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 * Find the definition of the named acl whose name is "name".
58 get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
60 const cfg_obj_t *acls = NULL;
61 const cfg_listelt_t *elt;
63 result = cfg_map_get(cctx, "acl", &acls);
64 if (result != ISC_R_SUCCESS)
66 for (elt = cfg_list_first(acls);
68 elt = cfg_list_next(elt)) {
69 const cfg_obj_t *acl = cfg_listelt_value(elt);
70 const char *aclname = cfg_obj_asstring(cfg_tuple_get(acl, "name"));
71 if (strcasecmp(aclname, name) == 0) {
73 *ret = cfg_tuple_get(acl, "value");
75 return (ISC_R_SUCCESS);
78 return (ISC_R_NOTFOUND);
82 convert_named_acl(const cfg_obj_t *nameobj, const cfg_obj_t *cctx,
83 isc_log_t *lctx, cfg_aclconfctx_t *ctx,
84 isc_mem_t *mctx, unsigned int nest_level,
88 const cfg_obj_t *cacl = NULL;
91 const char *aclname = cfg_obj_asstring(nameobj);
93 /* Look for an already-converted version. */
94 for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
96 dacl = ISC_LIST_NEXT(dacl, nextincache))
98 if (strcasecmp(aclname, dacl->name) == 0) {
99 if (ISC_MAGIC_VALID(dacl, LOOP_MAGIC)) {
100 cfg_obj_log(nameobj, lctx, ISC_LOG_ERROR,
101 "acl loop detected: %s", aclname);
102 return (ISC_R_FAILURE);
104 dns_acl_attach(dacl, target);
105 return (ISC_R_SUCCESS);
108 /* Not yet converted. Convert now. */
109 result = get_acl_def(cctx, aclname, &cacl);
110 if (result != ISC_R_SUCCESS) {
111 cfg_obj_log(nameobj, lctx, ISC_LOG_WARNING,
112 "undefined ACL '%s'", aclname);
116 * Add a loop detection element.
118 memset(&loop, 0, sizeof(loop));
119 ISC_LINK_INIT(&loop, nextincache);
120 DE_CONST(aclname, loop.name);
121 loop.magic = LOOP_MAGIC;
122 ISC_LIST_APPEND(ctx->named_acl_cache, &loop, nextincache);
123 result = cfg_acl_fromconfig(cacl, cctx, lctx, ctx, mctx,
125 ISC_LIST_UNLINK(ctx->named_acl_cache, &loop, nextincache);
128 if (result != ISC_R_SUCCESS)
130 dacl->name = isc_mem_strdup(dacl->mctx, aclname);
131 if (dacl->name == NULL)
132 return (ISC_R_NOMEMORY);
133 ISC_LIST_APPEND(ctx->named_acl_cache, dacl, nextincache);
134 dns_acl_attach(dacl, target);
135 return (ISC_R_SUCCESS);
139 convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
144 dns_fixedname_t fixname;
146 const char *txtname = cfg_obj_asstring(keyobj);
148 keylen = strlen(txtname);
149 isc_buffer_init(&buf, txtname, keylen);
150 isc_buffer_add(&buf, keylen);
151 dns_fixedname_init(&fixname);
152 result = dns_name_fromtext(dns_fixedname_name(&fixname), &buf,
153 dns_rootname, ISC_FALSE, NULL);
154 if (result != ISC_R_SUCCESS) {
155 cfg_obj_log(keyobj, lctx, ISC_LOG_WARNING,
156 "key name '%s' is not a valid domain name",
160 return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
164 * Recursively pre-parse an ACL definition to find the total number
165 * of non-IP-prefix elements (localhost, localnets, key) in all nested
166 * ACLs, so that the parent will have enough space allocated for the
167 * elements table after all the nested ACLs have been merged in to the
171 count_acl_elements(const cfg_obj_t *caml, const cfg_obj_t *cctx)
173 const cfg_listelt_t *elt;
174 const cfg_obj_t *cacl = NULL;
178 for (elt = cfg_list_first(caml);
180 elt = cfg_list_next(elt)) {
181 const cfg_obj_t *ce = cfg_listelt_value(elt);
183 /* negated element; just get the value. */
184 if (cfg_obj_istuple(ce))
185 ce = cfg_tuple_get(ce, "value");
187 if (cfg_obj_istype(ce, &cfg_type_keyref)) {
189 } else if (cfg_obj_islist(ce)) {
190 n += count_acl_elements(ce, cctx);
191 } else if (cfg_obj_isstring(ce)) {
192 const char *name = cfg_obj_asstring(ce);
193 if (strcasecmp(name, "localhost") == 0 ||
194 strcasecmp(name, "localnets") == 0) {
196 } else if (strcasecmp(name, "any") != 0 &&
197 strcasecmp(name, "none") != 0) {
198 result = get_acl_def(cctx, name, &cacl);
199 if (result == ISC_R_SUCCESS)
200 n += count_acl_elements(cacl, cctx) + 1;
209 cfg_acl_fromconfig(const cfg_obj_t *caml,
210 const cfg_obj_t *cctx,
212 cfg_aclconfctx_t *ctx,
214 unsigned int nest_level,
218 dns_acl_t *dacl = NULL, *inneracl = NULL;
219 dns_aclelement_t *de;
220 const cfg_listelt_t *elt;
221 dns_iptable_t *iptab;
222 int new_nest_level = 0;
225 new_nest_level = nest_level - 1;
227 REQUIRE(target != NULL);
228 REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
230 if (*target != NULL) {
232 * If target already points to an ACL, then we're being
233 * called recursively to configure a nested ACL. The
234 * nested ACL's contents should just be absorbed into its
237 dns_acl_attach(*target, &dacl);
238 dns_acl_detach(target);
241 * Need to allocate a new ACL structure. Count the items
242 * in the ACL definition that will require space in the
243 * elements table. (Note that if nest_level is nonzero,
244 * *everything* goes in the elements table.)
249 nelem = count_acl_elements(caml, cctx);
251 nelem = cfg_list_length(caml, ISC_FALSE);
253 result = dns_acl_create(mctx, nelem, &dacl);
254 if (result != ISC_R_SUCCESS)
259 for (elt = cfg_list_first(caml);
261 elt = cfg_list_next(elt)) {
262 const cfg_obj_t *ce = cfg_listelt_value(elt);
265 if (cfg_obj_istuple(ce)) {
266 /* This must be a negated element. */
267 ce = cfg_tuple_get(ce, "value");
269 dacl->has_negatives = ISC_TRUE;
274 * If nest_level is nonzero, then every element is
275 * to be stored as a separate, nested ACL rather than
276 * merged into the main iptable.
278 iptab = dacl->iptable;
280 if (nest_level != 0) {
281 result = dns_acl_create(mctx,
282 cfg_list_length(ce, ISC_FALSE),
284 if (result != ISC_R_SUCCESS)
286 iptab = de->nestedacl->iptable;
289 if (cfg_obj_isnetprefix(ce)) {
294 cfg_obj_asnetprefix(ce, &addr, &bitlen);
297 * If nesting ACLs (nest_level != 0), we negate
298 * the nestedacl element, not the iptable entry.
300 result = dns_iptable_addprefix(iptab, &addr, bitlen,
301 ISC_TF(nest_level != 0 || !neg));
302 if (result != ISC_R_SUCCESS)
305 if (nest_level > 0) {
306 de->type = dns_aclelementtype_nestedacl;
310 } else if (cfg_obj_islist(ce)) {
312 * If we're nesting ACLs, put the nested
313 * ACL onto the elements list; otherwise
314 * merge it into *this* ACL. We nest ACLs
315 * in two cases: 1) sortlist, 2) if the
316 * nested ACL contains negated members.
318 if (inneracl != NULL)
319 dns_acl_detach(&inneracl);
320 result = cfg_acl_fromconfig(ce, cctx, lctx,
321 ctx, mctx, new_nest_level,
323 if (result != ISC_R_SUCCESS)
326 if (nest_level > 0 || inneracl->has_negatives) {
327 de->type = dns_aclelementtype_nestedacl;
329 if (de->nestedacl != NULL)
330 dns_acl_detach(&de->nestedacl);
331 dns_acl_attach(inneracl,
333 dns_acl_detach(&inneracl);
336 dns_acl_merge(dacl, inneracl,
338 de += inneracl->length; /* elements added */
339 dns_acl_detach(&inneracl);
342 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
344 de->type = dns_aclelementtype_keyname;
346 dns_name_init(&de->keyname, NULL);
347 result = convert_keyname(ce, lctx, mctx,
349 if (result != ISC_R_SUCCESS)
351 } else if (cfg_obj_isstring(ce)) {
353 const char *name = cfg_obj_asstring(ce);
354 if (strcasecmp(name, "any") == 0) {
355 /* Iptable entry with zero bit length. */
356 result = dns_iptable_addprefix(iptab, NULL, 0,
357 ISC_TF(nest_level != 0 || !neg));
358 if (result != ISC_R_SUCCESS)
361 if (nest_level != 0) {
362 de->type = dns_aclelementtype_nestedacl;
366 } else if (strcasecmp(name, "none") == 0) {
369 * We don't unconditional set
370 * dacl->has_negatives and
371 * de->negative to true so we can handle
374 result = dns_iptable_addprefix(iptab, NULL, 0,
375 ISC_TF(nest_level != 0 || neg));
376 if (result != ISC_R_SUCCESS)
380 dacl->has_negatives = !neg;
382 if (nest_level != 0) {
383 de->type = dns_aclelementtype_nestedacl;
387 } else if (strcasecmp(name, "localhost") == 0) {
388 de->type = dns_aclelementtype_localhost;
390 } else if (strcasecmp(name, "localnets") == 0) {
391 de->type = dns_aclelementtype_localnets;
394 if (inneracl != NULL)
395 dns_acl_detach(&inneracl);
396 result = convert_named_acl(ce, cctx, lctx, ctx,
397 mctx, new_nest_level,
399 if (result != ISC_R_SUCCESS)
405 cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
406 "address match list contains "
407 "unsupported element type");
408 result = ISC_R_FAILURE;
413 * This should only be reached for localhost, localnets
414 * and keyname elements, and nested ACLs if nest_level is
415 * nonzero (i.e., in sortlists).
417 if (de->nestedacl != NULL &&
418 de->type != dns_aclelementtype_nestedacl)
419 dns_acl_detach(&de->nestedacl);
422 de->node_num = dacl->node_count;
426 INSIST(dacl->length <= dacl->alloc);
429 dns_acl_attach(dacl, target);
430 result = ISC_R_SUCCESS;
433 if (inneracl != NULL)
434 dns_acl_detach(&inneracl);
435 dns_acl_detach(&dacl);