2 * Copyright (C) 2004-2012 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_init(&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_boolean_t *has_negative)
220 const cfg_listelt_t *elt;
221 const cfg_obj_t *cacl = NULL;
225 if (has_negative != NULL)
226 *has_negative = ISC_FALSE;
228 for (elt = cfg_list_first(caml);
230 elt = cfg_list_next(elt)) {
231 const cfg_obj_t *ce = cfg_listelt_value(elt);
233 /* negated element; just get the value. */
234 if (cfg_obj_istuple(ce)) {
235 ce = cfg_tuple_get(ce, "value");
236 if (has_negative != NULL)
237 *has_negative = ISC_TRUE;
240 if (cfg_obj_istype(ce, &cfg_type_keyref)) {
242 } else if (cfg_obj_islist(ce)) {
243 isc_boolean_t negative;
244 n += count_acl_elements(ce, cctx, &negative);
247 } else if (cfg_obj_isstring(ce)) {
248 const char *name = cfg_obj_asstring(ce);
249 if (strcasecmp(name, "localhost") == 0 ||
250 strcasecmp(name, "localnets") == 0) {
252 } else if (strcasecmp(name, "any") != 0 &&
253 strcasecmp(name, "none") != 0) {
254 result = get_acl_def(cctx, name, &cacl);
255 if (result == ISC_R_SUCCESS)
256 n += count_acl_elements(cacl, cctx,
266 cfg_acl_fromconfig(const cfg_obj_t *caml,
267 const cfg_obj_t *cctx,
269 cfg_aclconfctx_t *ctx,
271 unsigned int nest_level,
275 dns_acl_t *dacl = NULL, *inneracl = NULL;
276 dns_aclelement_t *de;
277 const cfg_listelt_t *elt;
278 dns_iptable_t *iptab;
279 int new_nest_level = 0;
282 new_nest_level = nest_level - 1;
284 REQUIRE(target != NULL);
285 REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
287 if (*target != NULL) {
289 * If target already points to an ACL, then we're being
290 * called recursively to configure a nested ACL. The
291 * nested ACL's contents should just be absorbed into its
294 dns_acl_attach(*target, &dacl);
295 dns_acl_detach(target);
298 * Need to allocate a new ACL structure. Count the items
299 * in the ACL definition that will require space in the
300 * elements table. (Note that if nest_level is nonzero,
301 * *everything* goes in the elements table.)
306 nelem = count_acl_elements(caml, cctx, NULL);
308 nelem = cfg_list_length(caml, ISC_FALSE);
310 result = dns_acl_create(mctx, nelem, &dacl);
311 if (result != ISC_R_SUCCESS)
316 for (elt = cfg_list_first(caml);
318 elt = cfg_list_next(elt)) {
319 const cfg_obj_t *ce = cfg_listelt_value(elt);
322 if (cfg_obj_istuple(ce)) {
323 /* This must be a negated element. */
324 ce = cfg_tuple_get(ce, "value");
326 dacl->has_negatives = ISC_TRUE;
331 * If nest_level is nonzero, then every element is
332 * to be stored as a separate, nested ACL rather than
333 * merged into the main iptable.
335 iptab = dacl->iptable;
337 if (nest_level != 0) {
338 result = dns_acl_create(mctx,
339 cfg_list_length(ce, ISC_FALSE),
341 if (result != ISC_R_SUCCESS)
343 iptab = de->nestedacl->iptable;
346 if (cfg_obj_isnetprefix(ce)) {
351 cfg_obj_asnetprefix(ce, &addr, &bitlen);
354 * If nesting ACLs (nest_level != 0), we negate
355 * the nestedacl element, not the iptable entry.
357 result = dns_iptable_addprefix(iptab, &addr, bitlen,
358 ISC_TF(nest_level != 0 || !neg));
359 if (result != ISC_R_SUCCESS)
362 if (nest_level > 0) {
363 de->type = dns_aclelementtype_nestedacl;
367 } else if (cfg_obj_islist(ce)) {
369 * If we're nesting ACLs, put the nested
370 * ACL onto the elements list; otherwise
371 * merge it into *this* ACL. We nest ACLs
372 * in two cases: 1) sortlist, 2) if the
373 * nested ACL contains negated members.
375 if (inneracl != NULL)
376 dns_acl_detach(&inneracl);
377 result = cfg_acl_fromconfig(ce, cctx, lctx,
378 ctx, mctx, new_nest_level,
380 if (result != ISC_R_SUCCESS)
383 if (nest_level > 0 || inneracl->has_negatives) {
384 de->type = dns_aclelementtype_nestedacl;
386 if (de->nestedacl != NULL)
387 dns_acl_detach(&de->nestedacl);
388 dns_acl_attach(inneracl,
390 dns_acl_detach(&inneracl);
393 dns_acl_merge(dacl, inneracl,
395 de += inneracl->length; /* elements added */
396 dns_acl_detach(&inneracl);
399 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
401 de->type = dns_aclelementtype_keyname;
403 dns_name_init(&de->keyname, NULL);
404 result = convert_keyname(ce, lctx, mctx,
406 if (result != ISC_R_SUCCESS)
408 } else if (cfg_obj_isstring(ce)) {
410 const char *name = cfg_obj_asstring(ce);
411 if (strcasecmp(name, "any") == 0) {
412 /* Iptable entry with zero bit length. */
413 result = dns_iptable_addprefix(iptab, NULL, 0,
414 ISC_TF(nest_level != 0 || !neg));
415 if (result != ISC_R_SUCCESS)
418 if (nest_level != 0) {
419 de->type = dns_aclelementtype_nestedacl;
423 } else if (strcasecmp(name, "none") == 0) {
426 * We don't unconditional set
427 * dacl->has_negatives and
428 * de->negative to true so we can handle
431 result = dns_iptable_addprefix(iptab, NULL, 0,
432 ISC_TF(nest_level != 0 || neg));
433 if (result != ISC_R_SUCCESS)
437 dacl->has_negatives = !neg;
439 if (nest_level != 0) {
440 de->type = dns_aclelementtype_nestedacl;
444 } else if (strcasecmp(name, "localhost") == 0) {
445 de->type = dns_aclelementtype_localhost;
447 } else if (strcasecmp(name, "localnets") == 0) {
448 de->type = dns_aclelementtype_localnets;
451 if (inneracl != NULL)
452 dns_acl_detach(&inneracl);
453 result = convert_named_acl(ce, cctx, lctx, ctx,
454 mctx, new_nest_level,
456 if (result != ISC_R_SUCCESS)
462 cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
463 "address match list contains "
464 "unsupported element type");
465 result = ISC_R_FAILURE;
470 * This should only be reached for localhost, localnets
471 * and keyname elements, and nested ACLs if nest_level is
472 * nonzero (i.e., in sortlists).
474 if (de->nestedacl != NULL &&
475 de->type != dns_aclelementtype_nestedacl)
476 dns_acl_detach(&de->nestedacl);
479 de->node_num = dacl->node_count;
483 INSIST(dacl->length <= dacl->alloc);
486 dns_acl_attach(dacl, target);
487 result = ISC_R_SUCCESS;
490 if (inneracl != NULL)
491 dns_acl_detach(&inneracl);
492 dns_acl_detach(&dacl);