2 * Copyright (C) 2004-2006 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.2.2.6 2006/03/02 00:37:22 marka 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/fixedname.h>
33 #define LOOP_MAGIC ISC_MAGIC('L','O','O','P')
36 cfg_aclconfctx_init(cfg_aclconfctx_t *ctx) {
37 ISC_LIST_INIT(ctx->named_acl_cache);
41 cfg_aclconfctx_destroy(cfg_aclconfctx_t *ctx) {
42 dns_acl_t *dacl, *next;
43 for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
47 next = ISC_LIST_NEXT(dacl, nextincache);
48 dns_acl_detach(&dacl);
53 * Find the definition of the named acl whose name is "name".
56 get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
58 const cfg_obj_t *acls = NULL;
59 const cfg_listelt_t *elt;
61 result = cfg_map_get(cctx, "acl", &acls);
62 if (result != ISC_R_SUCCESS)
64 for (elt = cfg_list_first(acls);
66 elt = cfg_list_next(elt)) {
67 const cfg_obj_t *acl = cfg_listelt_value(elt);
68 const char *aclname = cfg_obj_asstring(cfg_tuple_get(acl, "name"));
69 if (strcasecmp(aclname, name) == 0) {
70 *ret = cfg_tuple_get(acl, "value");
71 return (ISC_R_SUCCESS);
74 return (ISC_R_NOTFOUND);
78 convert_named_acl(const cfg_obj_t *nameobj, const cfg_obj_t *cctx,
79 isc_log_t *lctx, cfg_aclconfctx_t *ctx,
80 isc_mem_t *mctx, dns_acl_t **target)
83 const cfg_obj_t *cacl = NULL;
86 const char *aclname = cfg_obj_asstring(nameobj);
88 /* Look for an already-converted version. */
89 for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
91 dacl = ISC_LIST_NEXT(dacl, nextincache))
93 if (strcasecmp(aclname, dacl->name) == 0) {
94 if (ISC_MAGIC_VALID(dacl, LOOP_MAGIC)) {
95 cfg_obj_log(nameobj, lctx, ISC_LOG_ERROR,
96 "acl loop detected: %s", aclname);
97 return (ISC_R_FAILURE);
99 dns_acl_attach(dacl, target);
100 return (ISC_R_SUCCESS);
103 /* Not yet converted. Convert now. */
104 result = get_acl_def(cctx, aclname, &cacl);
105 if (result != ISC_R_SUCCESS) {
106 cfg_obj_log(nameobj, lctx, ISC_LOG_WARNING,
107 "undefined ACL '%s'", aclname);
111 * Add a loop detection element.
113 memset(&loop, 0, sizeof(loop));
114 ISC_LINK_INIT(&loop, nextincache);
115 DE_CONST(aclname, loop.name);
116 loop.magic = LOOP_MAGIC;
117 ISC_LIST_APPEND(ctx->named_acl_cache, &loop, nextincache);
118 result = cfg_acl_fromconfig(cacl, cctx, lctx, ctx, mctx, &dacl);
119 ISC_LIST_UNLINK(ctx->named_acl_cache, &loop, nextincache);
122 if (result != ISC_R_SUCCESS)
124 dacl->name = isc_mem_strdup(dacl->mctx, aclname);
125 if (dacl->name == NULL)
126 return (ISC_R_NOMEMORY);
127 ISC_LIST_APPEND(ctx->named_acl_cache, dacl, nextincache);
128 dns_acl_attach(dacl, target);
129 return (ISC_R_SUCCESS);
133 convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
138 dns_fixedname_t fixname;
140 const char *txtname = cfg_obj_asstring(keyobj);
142 keylen = strlen(txtname);
143 isc_buffer_init(&buf, txtname, keylen);
144 isc_buffer_add(&buf, keylen);
145 dns_fixedname_init(&fixname);
146 result = dns_name_fromtext(dns_fixedname_name(&fixname), &buf,
147 dns_rootname, ISC_FALSE, NULL);
148 if (result != ISC_R_SUCCESS) {
149 cfg_obj_log(keyobj, lctx, ISC_LOG_WARNING,
150 "key name '%s' is not a valid domain name",
154 return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
158 cfg_acl_fromconfig(const cfg_obj_t *caml,
159 const cfg_obj_t *cctx,
161 cfg_aclconfctx_t *ctx,
167 dns_acl_t *dacl = NULL;
168 dns_aclelement_t *de;
169 const cfg_listelt_t *elt;
171 REQUIRE(target != NULL && *target == NULL);
174 for (elt = cfg_list_first(caml);
176 elt = cfg_list_next(elt))
179 result = dns_acl_create(mctx, count, &dacl);
180 if (result != ISC_R_SUCCESS)
184 for (elt = cfg_list_first(caml);
186 elt = cfg_list_next(elt))
188 const cfg_obj_t *ce = cfg_listelt_value(elt);
189 if (cfg_obj_istuple(ce)) {
190 /* This must be a negated element. */
191 ce = cfg_tuple_get(ce, "value");
192 de->negative = ISC_TRUE;
194 de->negative = ISC_FALSE;
197 if (cfg_obj_isnetprefix(ce)) {
199 de->type = dns_aclelementtype_ipprefix;
201 cfg_obj_asnetprefix(ce,
202 &de->u.ip_prefix.address,
203 &de->u.ip_prefix.prefixlen);
204 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
206 de->type = dns_aclelementtype_keyname;
207 dns_name_init(&de->u.keyname, NULL);
208 result = convert_keyname(ce, lctx, mctx,
210 if (result != ISC_R_SUCCESS)
212 } else if (cfg_obj_islist(ce)) {
214 de->type = dns_aclelementtype_nestedacl;
215 result = cfg_acl_fromconfig(ce, cctx, lctx, ctx,
216 mctx, &de->u.nestedacl);
217 if (result != ISC_R_SUCCESS)
219 } else if (cfg_obj_isstring(ce)) {
221 const char *name = cfg_obj_asstring(ce);
222 if (strcasecmp(name, "localhost") == 0) {
223 de->type = dns_aclelementtype_localhost;
224 } else if (strcasecmp(name, "localnets") == 0) {
225 de->type = dns_aclelementtype_localnets;
226 } else if (strcasecmp(name, "any") == 0) {
227 de->type = dns_aclelementtype_any;
228 } else if (strcasecmp(name, "none") == 0) {
229 de->type = dns_aclelementtype_any;
230 de->negative = ISC_TF(! de->negative);
232 de->type = dns_aclelementtype_nestedacl;
233 result = convert_named_acl(ce, cctx, lctx,
236 if (result != ISC_R_SUCCESS)
240 cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
241 "address match list contains "
242 "unsupported element type");
243 result = ISC_R_FAILURE;
251 return (ISC_R_SUCCESS);
254 dns_acl_detach(&dacl);