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.4 2009/10/01 23:47:17 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,
172 isc_boolean_t *has_negative)
174 const cfg_listelt_t *elt;
175 const cfg_obj_t *cacl = NULL;
179 if (has_negative != NULL)
180 *has_negative = ISC_FALSE;
182 for (elt = cfg_list_first(caml);
184 elt = cfg_list_next(elt)) {
185 const cfg_obj_t *ce = cfg_listelt_value(elt);
187 /* negated element; just get the value. */
188 if (cfg_obj_istuple(ce)) {
189 ce = cfg_tuple_get(ce, "value");
190 if (has_negative != NULL)
191 *has_negative = ISC_TRUE;
194 if (cfg_obj_istype(ce, &cfg_type_keyref)) {
196 } else if (cfg_obj_islist(ce)) {
197 isc_boolean_t negative;
198 n += count_acl_elements(ce, cctx, &negative);
201 } else if (cfg_obj_isstring(ce)) {
202 const char *name = cfg_obj_asstring(ce);
203 if (strcasecmp(name, "localhost") == 0 ||
204 strcasecmp(name, "localnets") == 0) {
206 } else if (strcasecmp(name, "any") != 0 &&
207 strcasecmp(name, "none") != 0) {
208 result = get_acl_def(cctx, name, &cacl);
209 if (result == ISC_R_SUCCESS)
210 n += count_acl_elements(cacl, cctx,
220 cfg_acl_fromconfig(const cfg_obj_t *caml,
221 const cfg_obj_t *cctx,
223 cfg_aclconfctx_t *ctx,
225 unsigned int nest_level,
229 dns_acl_t *dacl = NULL, *inneracl = NULL;
230 dns_aclelement_t *de;
231 const cfg_listelt_t *elt;
232 dns_iptable_t *iptab;
233 int new_nest_level = 0;
236 new_nest_level = nest_level - 1;
238 REQUIRE(target != NULL);
239 REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
241 if (*target != NULL) {
243 * If target already points to an ACL, then we're being
244 * called recursively to configure a nested ACL. The
245 * nested ACL's contents should just be absorbed into its
248 dns_acl_attach(*target, &dacl);
249 dns_acl_detach(target);
252 * Need to allocate a new ACL structure. Count the items
253 * in the ACL definition that will require space in the
254 * elements table. (Note that if nest_level is nonzero,
255 * *everything* goes in the elements table.)
260 nelem = count_acl_elements(caml, cctx, NULL);
262 nelem = cfg_list_length(caml, ISC_FALSE);
264 result = dns_acl_create(mctx, nelem, &dacl);
265 if (result != ISC_R_SUCCESS)
270 for (elt = cfg_list_first(caml);
272 elt = cfg_list_next(elt)) {
273 const cfg_obj_t *ce = cfg_listelt_value(elt);
276 if (cfg_obj_istuple(ce)) {
277 /* This must be a negated element. */
278 ce = cfg_tuple_get(ce, "value");
280 dacl->has_negatives = ISC_TRUE;
285 * If nest_level is nonzero, then every element is
286 * to be stored as a separate, nested ACL rather than
287 * merged into the main iptable.
289 iptab = dacl->iptable;
291 if (nest_level != 0) {
292 result = dns_acl_create(mctx,
293 cfg_list_length(ce, ISC_FALSE),
295 if (result != ISC_R_SUCCESS)
297 iptab = de->nestedacl->iptable;
300 if (cfg_obj_isnetprefix(ce)) {
305 cfg_obj_asnetprefix(ce, &addr, &bitlen);
308 * If nesting ACLs (nest_level != 0), we negate
309 * the nestedacl element, not the iptable entry.
311 result = dns_iptable_addprefix(iptab, &addr, bitlen,
312 ISC_TF(nest_level != 0 || !neg));
313 if (result != ISC_R_SUCCESS)
316 if (nest_level > 0) {
317 de->type = dns_aclelementtype_nestedacl;
321 } else if (cfg_obj_islist(ce)) {
323 * If we're nesting ACLs, put the nested
324 * ACL onto the elements list; otherwise
325 * merge it into *this* ACL. We nest ACLs
326 * in two cases: 1) sortlist, 2) if the
327 * nested ACL contains negated members.
329 if (inneracl != NULL)
330 dns_acl_detach(&inneracl);
331 result = cfg_acl_fromconfig(ce, cctx, lctx,
332 ctx, mctx, new_nest_level,
334 if (result != ISC_R_SUCCESS)
337 if (nest_level > 0 || inneracl->has_negatives) {
338 de->type = dns_aclelementtype_nestedacl;
340 if (de->nestedacl != NULL)
341 dns_acl_detach(&de->nestedacl);
342 dns_acl_attach(inneracl,
344 dns_acl_detach(&inneracl);
347 dns_acl_merge(dacl, inneracl,
349 de += inneracl->length; /* elements added */
350 dns_acl_detach(&inneracl);
353 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
355 de->type = dns_aclelementtype_keyname;
357 dns_name_init(&de->keyname, NULL);
358 result = convert_keyname(ce, lctx, mctx,
360 if (result != ISC_R_SUCCESS)
362 } else if (cfg_obj_isstring(ce)) {
364 const char *name = cfg_obj_asstring(ce);
365 if (strcasecmp(name, "any") == 0) {
366 /* Iptable entry with zero bit length. */
367 result = dns_iptable_addprefix(iptab, NULL, 0,
368 ISC_TF(nest_level != 0 || !neg));
369 if (result != ISC_R_SUCCESS)
372 if (nest_level != 0) {
373 de->type = dns_aclelementtype_nestedacl;
377 } else if (strcasecmp(name, "none") == 0) {
380 * We don't unconditional set
381 * dacl->has_negatives and
382 * de->negative to true so we can handle
385 result = dns_iptable_addprefix(iptab, NULL, 0,
386 ISC_TF(nest_level != 0 || neg));
387 if (result != ISC_R_SUCCESS)
391 dacl->has_negatives = !neg;
393 if (nest_level != 0) {
394 de->type = dns_aclelementtype_nestedacl;
398 } else if (strcasecmp(name, "localhost") == 0) {
399 de->type = dns_aclelementtype_localhost;
401 } else if (strcasecmp(name, "localnets") == 0) {
402 de->type = dns_aclelementtype_localnets;
405 if (inneracl != NULL)
406 dns_acl_detach(&inneracl);
407 result = convert_named_acl(ce, cctx, lctx, ctx,
408 mctx, new_nest_level,
410 if (result != ISC_R_SUCCESS)
416 cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
417 "address match list contains "
418 "unsupported element type");
419 result = ISC_R_FAILURE;
424 * This should only be reached for localhost, localnets
425 * and keyname elements, and nested ACLs if nest_level is
426 * nonzero (i.e., in sortlists).
428 if (de->nestedacl != NULL &&
429 de->type != dns_aclelementtype_nestedacl)
430 dns_acl_detach(&de->nestedacl);
433 de->node_num = dacl->node_count;
437 INSIST(dacl->length <= dacl->alloc);
440 dns_acl_attach(dacl, target);
441 result = ISC_R_SUCCESS;
444 if (inneracl != NULL)
445 dns_acl_detach(&inneracl);
446 dns_acl_detach(&dacl);