]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/bind9/lib/isccfg/aclconf.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / bind9 / lib / isccfg / aclconf.c
1 /*
2  * Copyright (C) 2004-2006  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2002  Internet Software Consortium.
4  *
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.
8  *
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.
16  */
17
18 /* $Id: aclconf.c,v 1.2.2.6 2006/03/02 00:37:22 marka Exp $ */
19
20 #include <config.h>
21
22 #include <isc/mem.h>
23 #include <isc/string.h>         /* Required for HP/UX (and others?) */
24 #include <isc/util.h>
25
26 #include <isccfg/namedconf.h>
27 #include <isccfg/aclconf.h>
28
29 #include <dns/acl.h>
30 #include <dns/fixedname.h>
31 #include <dns/log.h>
32
33 #define LOOP_MAGIC ISC_MAGIC('L','O','O','P') 
34
35 void
36 cfg_aclconfctx_init(cfg_aclconfctx_t *ctx) {
37         ISC_LIST_INIT(ctx->named_acl_cache);
38 }
39
40 void
41 cfg_aclconfctx_destroy(cfg_aclconfctx_t *ctx) {
42         dns_acl_t *dacl, *next;
43         for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
44              dacl != NULL;
45              dacl = next)
46         {
47                 next = ISC_LIST_NEXT(dacl, nextincache);
48                 dns_acl_detach(&dacl);
49         }
50 }
51
52 /*
53  * Find the definition of the named acl whose name is "name".
54  */
55 static isc_result_t
56 get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
57         isc_result_t result;
58         const cfg_obj_t *acls = NULL;
59         const cfg_listelt_t *elt;
60         
61         result = cfg_map_get(cctx, "acl", &acls);
62         if (result != ISC_R_SUCCESS)
63                 return (result);
64         for (elt = cfg_list_first(acls);
65              elt != NULL;
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);
72                 }
73         }
74         return (ISC_R_NOTFOUND);
75 }
76
77 static isc_result_t
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)
81 {
82         isc_result_t result;
83         const cfg_obj_t *cacl = NULL;
84         dns_acl_t *dacl;
85         dns_acl_t loop;
86         const char *aclname = cfg_obj_asstring(nameobj);
87
88         /* Look for an already-converted version. */
89         for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
90              dacl != NULL;
91              dacl = ISC_LIST_NEXT(dacl, nextincache))
92         {
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);
98                         }
99                         dns_acl_attach(dacl, target);
100                         return (ISC_R_SUCCESS);
101                 }
102         }
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);
108                 return (result);
109         }
110         /*
111          * Add a loop detection element.
112          */
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);
120         loop.magic = 0;
121         loop.name = NULL;
122         if (result != ISC_R_SUCCESS)
123                 return (result);
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);
130 }
131
132 static isc_result_t
133 convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
134                 dns_name_t *dnsname)
135 {
136         isc_result_t result;
137         isc_buffer_t buf;
138         dns_fixedname_t fixname;
139         unsigned int keylen;
140         const char *txtname = cfg_obj_asstring(keyobj);
141
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",
151                             txtname);
152                 return (result);
153         }
154         return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
155 }
156
157 isc_result_t
158 cfg_acl_fromconfig(const cfg_obj_t *caml,
159                    const cfg_obj_t *cctx,
160                    isc_log_t *lctx,
161                    cfg_aclconfctx_t *ctx,
162                    isc_mem_t *mctx,
163                    dns_acl_t **target)
164 {
165         isc_result_t result;
166         unsigned int count;
167         dns_acl_t *dacl = NULL;
168         dns_aclelement_t *de;
169         const cfg_listelt_t *elt;
170
171         REQUIRE(target != NULL && *target == NULL);
172
173         count = 0;
174         for (elt = cfg_list_first(caml);
175              elt != NULL;
176              elt = cfg_list_next(elt))
177                 count++;
178
179         result = dns_acl_create(mctx, count, &dacl);
180         if (result != ISC_R_SUCCESS)
181                 return (result);
182
183         de = dacl->elements;
184         for (elt = cfg_list_first(caml);
185              elt != NULL;
186              elt = cfg_list_next(elt))
187         {
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;
193                 } else {
194                         de->negative = ISC_FALSE;
195                 }
196
197                 if (cfg_obj_isnetprefix(ce)) {
198                         /* Network prefix */
199                         de->type = dns_aclelementtype_ipprefix;
200
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)) {
205                         /* Key name */
206                         de->type = dns_aclelementtype_keyname;
207                         dns_name_init(&de->u.keyname, NULL);
208                         result = convert_keyname(ce, lctx, mctx,
209                                                  &de->u.keyname);
210                         if (result != ISC_R_SUCCESS)
211                                 goto cleanup;
212                 } else if (cfg_obj_islist(ce)) {
213                         /* Nested ACL */
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)
218                                 goto cleanup;
219                 } else if (cfg_obj_isstring(ce)) {
220                         /* ACL name */
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);
231                         } else {
232                                 de->type = dns_aclelementtype_nestedacl;
233                                 result = convert_named_acl(ce, cctx, lctx,
234                                                            ctx, mctx,
235                                                            &de->u.nestedacl);
236                                 if (result != ISC_R_SUCCESS)
237                                         goto cleanup;
238                         }
239                 } else {
240                         cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
241                                     "address match list contains "
242                                     "unsupported element type");
243                         result = ISC_R_FAILURE;
244                         goto cleanup;
245                 }
246                 de++;
247                 dacl->length++;
248         }
249
250         *target = dacl;
251         return (ISC_R_SUCCESS);
252
253  cleanup:
254         dns_acl_detach(&dacl);
255         return (result);
256 }