]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - contrib/bind9/lib/isccfg/aclconf.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / contrib / bind9 / lib / isccfg / aclconf.c
1 /*
2  * Copyright (C) 2004-2009  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2002  Internet Software Consortium.
4  *
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.
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.22.34.4 2009/10/01 23:47:17 tbox 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/iptable.h>
31 #include <dns/fixedname.h>
32 #include <dns/log.h>
33
34 #define LOOP_MAGIC ISC_MAGIC('L','O','O','P')
35
36 void
37 cfg_aclconfctx_init(cfg_aclconfctx_t *ctx) {
38         ISC_LIST_INIT(ctx->named_acl_cache);
39 }
40
41 void
42 cfg_aclconfctx_destroy(cfg_aclconfctx_t *ctx) {
43         dns_acl_t *dacl, *next;
44
45         for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
46              dacl != NULL;
47              dacl = next)
48         {
49                 next = ISC_LIST_NEXT(dacl, nextincache);
50                 dns_acl_detach(&dacl);
51         }
52 }
53
54 /*
55  * Find the definition of the named acl whose name is "name".
56  */
57 static isc_result_t
58 get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
59         isc_result_t result;
60         const cfg_obj_t *acls = NULL;
61         const cfg_listelt_t *elt;
62
63         result = cfg_map_get(cctx, "acl", &acls);
64         if (result != ISC_R_SUCCESS)
65                 return (result);
66         for (elt = cfg_list_first(acls);
67              elt != NULL;
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) {
72                         if (ret != NULL) {
73                                 *ret = cfg_tuple_get(acl, "value");
74                         }
75                         return (ISC_R_SUCCESS);
76                 }
77         }
78         return (ISC_R_NOTFOUND);
79 }
80
81 static isc_result_t
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,
85                   dns_acl_t **target)
86 {
87         isc_result_t result;
88         const cfg_obj_t *cacl = NULL;
89         dns_acl_t *dacl;
90         dns_acl_t loop;
91         const char *aclname = cfg_obj_asstring(nameobj);
92
93         /* Look for an already-converted version. */
94         for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
95              dacl != NULL;
96              dacl = ISC_LIST_NEXT(dacl, nextincache))
97         {
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);
103                         }
104                         dns_acl_attach(dacl, target);
105                         return (ISC_R_SUCCESS);
106                 }
107         }
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);
113                 return (result);
114         }
115         /*
116          * Add a loop detection element.
117          */
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,
124                                     nest_level, &dacl);
125         ISC_LIST_UNLINK(ctx->named_acl_cache, &loop, nextincache);
126         loop.magic = 0;
127         loop.name = NULL;
128         if (result != ISC_R_SUCCESS)
129                 return (result);
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);
136 }
137
138 static isc_result_t
139 convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
140                 dns_name_t *dnsname)
141 {
142         isc_result_t result;
143         isc_buffer_t buf;
144         dns_fixedname_t fixname;
145         unsigned int keylen;
146         const char *txtname = cfg_obj_asstring(keyobj);
147
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",
157                             txtname);
158                 return (result);
159         }
160         return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
161 }
162
163 /*
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
168  * parent.
169  */
170 static int
171 count_acl_elements(const cfg_obj_t *caml, const cfg_obj_t *cctx,
172                    isc_boolean_t *has_negative)
173 {
174         const cfg_listelt_t *elt;
175         const cfg_obj_t *cacl = NULL;
176         isc_result_t result;
177         int n = 0;
178
179         if (has_negative != NULL)
180                 *has_negative = ISC_FALSE;
181
182         for (elt = cfg_list_first(caml);
183              elt != NULL;
184              elt = cfg_list_next(elt)) {
185                 const cfg_obj_t *ce = cfg_listelt_value(elt);
186
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;
192                 }
193
194                 if (cfg_obj_istype(ce, &cfg_type_keyref)) {
195                         n++;
196                 } else if (cfg_obj_islist(ce)) {
197                         isc_boolean_t negative;
198                         n += count_acl_elements(ce, cctx, &negative);
199                         if (negative)
200                                 n++;
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) {
205                                 n++;
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,
211                                                                 NULL) + 1;
212                         }
213                 }
214         }
215
216         return n;
217 }
218
219 isc_result_t
220 cfg_acl_fromconfig(const cfg_obj_t *caml,
221                    const cfg_obj_t *cctx,
222                    isc_log_t *lctx,
223                    cfg_aclconfctx_t *ctx,
224                    isc_mem_t *mctx,
225                    unsigned int nest_level,
226                    dns_acl_t **target)
227 {
228         isc_result_t result;
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;
234
235         if (nest_level != 0)
236                 new_nest_level = nest_level - 1;
237
238         REQUIRE(target != NULL);
239         REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
240
241         if (*target != NULL) {
242                 /*
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
246                  * parent ACL.
247                  */
248                 dns_acl_attach(*target, &dacl);
249                 dns_acl_detach(target);
250         } else {
251                 /*
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.)
256                  */
257                 int nelem;
258
259                 if (nest_level == 0)
260                         nelem = count_acl_elements(caml, cctx, NULL);
261                 else
262                         nelem = cfg_list_length(caml, ISC_FALSE);
263
264                 result = dns_acl_create(mctx, nelem, &dacl);
265                 if (result != ISC_R_SUCCESS)
266                         return (result);
267         }
268
269         de = dacl->elements;
270         for (elt = cfg_list_first(caml);
271              elt != NULL;
272              elt = cfg_list_next(elt)) {
273                 const cfg_obj_t *ce = cfg_listelt_value(elt);
274                 isc_boolean_t   neg;
275
276                 if (cfg_obj_istuple(ce)) {
277                         /* This must be a negated element. */
278                         ce = cfg_tuple_get(ce, "value");
279                         neg = ISC_TRUE;
280                         dacl->has_negatives = ISC_TRUE;
281                 } else
282                         neg = ISC_FALSE;
283
284                 /*
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.
288                  */
289                 iptab = dacl->iptable;
290
291                 if (nest_level != 0) {
292                         result = dns_acl_create(mctx,
293                                                 cfg_list_length(ce, ISC_FALSE),
294                                                 &de->nestedacl);
295                         if (result != ISC_R_SUCCESS)
296                                 goto cleanup;
297                         iptab = de->nestedacl->iptable;
298                 }
299
300                 if (cfg_obj_isnetprefix(ce)) {
301                         /* Network prefix */
302                         isc_netaddr_t   addr;
303                         unsigned int    bitlen;
304
305                         cfg_obj_asnetprefix(ce, &addr, &bitlen);
306
307                         /*
308                          * If nesting ACLs (nest_level != 0), we negate
309                          * the nestedacl element, not the iptable entry.
310                          */
311                         result = dns_iptable_addprefix(iptab, &addr, bitlen,
312                                               ISC_TF(nest_level != 0 || !neg));
313                         if (result != ISC_R_SUCCESS)
314                                 goto cleanup;
315
316                         if (nest_level > 0) {
317                                 de->type = dns_aclelementtype_nestedacl;
318                                 de->negative = neg;
319                         } else
320                                 continue;
321                 } else if (cfg_obj_islist(ce)) {
322                         /*
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.
328                          */
329                         if (inneracl != NULL)
330                                 dns_acl_detach(&inneracl);
331                         result = cfg_acl_fromconfig(ce, cctx, lctx,
332                                                     ctx, mctx, new_nest_level,
333                                                     &inneracl);
334                         if (result != ISC_R_SUCCESS)
335                                 goto cleanup;
336 nested_acl:
337                         if (nest_level > 0 || inneracl->has_negatives) {
338                                 de->type = dns_aclelementtype_nestedacl;
339                                 de->negative = neg;
340                                 if (de->nestedacl != NULL)
341                                         dns_acl_detach(&de->nestedacl);
342                                 dns_acl_attach(inneracl,
343                                                &de->nestedacl);
344                                 dns_acl_detach(&inneracl);
345                                 /* Fall through. */
346                         } else {
347                                 dns_acl_merge(dacl, inneracl,
348                                               ISC_TF(!neg));
349                                 de += inneracl->length;  /* elements added */
350                                 dns_acl_detach(&inneracl);
351                                 continue;
352                         }
353                 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
354                         /* Key name. */
355                         de->type = dns_aclelementtype_keyname;
356                         de->negative = neg;
357                         dns_name_init(&de->keyname, NULL);
358                         result = convert_keyname(ce, lctx, mctx,
359                                                  &de->keyname);
360                         if (result != ISC_R_SUCCESS)
361                                 goto cleanup;
362                 } else if (cfg_obj_isstring(ce)) {
363                         /* ACL name. */
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)
370                                         goto cleanup;
371
372                                 if (nest_level != 0) {
373                                         de->type = dns_aclelementtype_nestedacl;
374                                         de->negative = neg;
375                                 } else
376                                         continue;
377                         } else if (strcasecmp(name, "none") == 0) {
378                                 /* none == !any */
379                                 /*
380                                  * We don't unconditional set
381                                  * dacl->has_negatives and
382                                  * de->negative to true so we can handle
383                                  * "!none;".
384                                  */
385                                 result = dns_iptable_addprefix(iptab, NULL, 0,
386                                               ISC_TF(nest_level != 0 || neg));
387                                 if (result != ISC_R_SUCCESS)
388                                         goto cleanup;
389
390                                 if (!neg)
391                                         dacl->has_negatives = !neg;
392
393                                 if (nest_level != 0) {
394                                         de->type = dns_aclelementtype_nestedacl;
395                                         de->negative = !neg;
396                                 } else
397                                         continue;
398                         } else if (strcasecmp(name, "localhost") == 0) {
399                                 de->type = dns_aclelementtype_localhost;
400                                 de->negative = neg;
401                         } else if (strcasecmp(name, "localnets") == 0) {
402                                 de->type = dns_aclelementtype_localnets;
403                                 de->negative = neg;
404                         } else {
405                                 if (inneracl != NULL)
406                                         dns_acl_detach(&inneracl);
407                                 result = convert_named_acl(ce, cctx, lctx, ctx,
408                                                            mctx, new_nest_level,
409                                                            &inneracl);
410                                 if (result != ISC_R_SUCCESS)
411                                         goto cleanup;
412
413                                 goto nested_acl;
414                         }
415                 } else {
416                         cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
417                                     "address match list contains "
418                                     "unsupported element type");
419                         result = ISC_R_FAILURE;
420                         goto cleanup;
421                 }
422
423                 /*
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).
427                  */
428                 if (de->nestedacl != NULL &&
429                     de->type != dns_aclelementtype_nestedacl)
430                         dns_acl_detach(&de->nestedacl);
431
432                 dacl->node_count++;
433                 de->node_num = dacl->node_count;
434
435                 dacl->length++;
436                 de++;
437                 INSIST(dacl->length <= dacl->alloc);
438         }
439
440         dns_acl_attach(dacl, target);
441         result = ISC_R_SUCCESS;
442
443  cleanup:
444         if (inneracl != NULL)
445                 dns_acl_detach(&inneracl);
446         dns_acl_detach(&dacl);
447         return (result);
448 }