]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/bind9/lib/isccfg/aclconf.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / bind9 / lib / isccfg / aclconf.c
1 /*
2  * Copyright (C) 2004-2011  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.29.72.2 2011-06-17 23:47:11 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 isc_result_t
37 cfg_aclconfctx_create(isc_mem_t *mctx, cfg_aclconfctx_t **ret) {
38         isc_result_t result;
39         cfg_aclconfctx_t *actx;
40
41         REQUIRE(mctx != NULL);
42         REQUIRE(ret != NULL && *ret == NULL);
43
44         actx = isc_mem_get(mctx, sizeof(*actx));
45         if (actx == NULL)
46                 return (ISC_R_NOMEMORY);
47
48         result = isc_refcount_init(&actx->references, 1);
49         if (result != ISC_R_SUCCESS)
50                 goto cleanup;
51
52         actx->mctx = NULL;
53         isc_mem_attach(mctx, &actx->mctx);
54         ISC_LIST_INIT(actx->named_acl_cache);
55
56         *ret = actx;
57         return (ISC_R_SUCCESS);
58
59  cleanup:
60         isc_mem_put(mctx, actx, sizeof(*actx));
61         return (result);
62 }
63
64 void
65 cfg_aclconfctx_attach(cfg_aclconfctx_t *src, cfg_aclconfctx_t **dest) {
66         REQUIRE(src != NULL);
67         REQUIRE(dest != NULL && *dest == NULL);
68
69         isc_refcount_increment(&src->references, NULL);
70         *dest = src;
71 }
72
73 void
74 cfg_aclconfctx_detach(cfg_aclconfctx_t **actxp) {
75         cfg_aclconfctx_t *actx;
76         dns_acl_t *dacl, *next;
77         isc_mem_t *mctx;
78         unsigned int refs;
79
80         REQUIRE(actxp != NULL && *actxp != NULL);
81
82         actx = *actxp;
83         mctx = actx->mctx;
84
85         isc_refcount_decrement(&actx->references, &refs);
86         if (refs == 0) {
87                 for (dacl = ISC_LIST_HEAD(actx->named_acl_cache);
88                      dacl != NULL;
89                      dacl = next)
90                 {
91                         next = ISC_LIST_NEXT(dacl, nextincache);
92                         ISC_LIST_UNLINK(actx->named_acl_cache, dacl,
93                                         nextincache);
94                         dns_acl_detach(&dacl);
95                 }
96                 isc_mem_putanddetach(&actx->mctx, actx, sizeof(*actx));
97         }
98
99         *actxp = NULL;
100 }
101
102 /*
103  * Find the definition of the named acl whose name is "name".
104  */
105 static isc_result_t
106 get_acl_def(const cfg_obj_t *cctx, const char *name, const cfg_obj_t **ret) {
107         isc_result_t result;
108         const cfg_obj_t *acls = NULL;
109         const cfg_listelt_t *elt;
110
111         result = cfg_map_get(cctx, "acl", &acls);
112         if (result != ISC_R_SUCCESS)
113                 return (result);
114         for (elt = cfg_list_first(acls);
115              elt != NULL;
116              elt = cfg_list_next(elt)) {
117                 const cfg_obj_t *acl = cfg_listelt_value(elt);
118                 const char *aclname = cfg_obj_asstring(cfg_tuple_get(acl, "name"));
119                 if (strcasecmp(aclname, name) == 0) {
120                         if (ret != NULL) {
121                                 *ret = cfg_tuple_get(acl, "value");
122                         }
123                         return (ISC_R_SUCCESS);
124                 }
125         }
126         return (ISC_R_NOTFOUND);
127 }
128
129 static isc_result_t
130 convert_named_acl(const cfg_obj_t *nameobj, const cfg_obj_t *cctx,
131                   isc_log_t *lctx, cfg_aclconfctx_t *ctx,
132                   isc_mem_t *mctx, unsigned int nest_level,
133                   dns_acl_t **target)
134 {
135         isc_result_t result;
136         const cfg_obj_t *cacl = NULL;
137         dns_acl_t *dacl;
138         dns_acl_t loop;
139         const char *aclname = cfg_obj_asstring(nameobj);
140
141         /* Look for an already-converted version. */
142         for (dacl = ISC_LIST_HEAD(ctx->named_acl_cache);
143              dacl != NULL;
144              dacl = ISC_LIST_NEXT(dacl, nextincache))
145         {
146                 if (strcasecmp(aclname, dacl->name) == 0) {
147                         if (ISC_MAGIC_VALID(dacl, LOOP_MAGIC)) {
148                                 cfg_obj_log(nameobj, lctx, ISC_LOG_ERROR,
149                                             "acl loop detected: %s", aclname);
150                                 return (ISC_R_FAILURE);
151                         }
152                         dns_acl_attach(dacl, target);
153                         return (ISC_R_SUCCESS);
154                 }
155         }
156         /* Not yet converted.  Convert now. */
157         result = get_acl_def(cctx, aclname, &cacl);
158         if (result != ISC_R_SUCCESS) {
159                 cfg_obj_log(nameobj, lctx, ISC_LOG_WARNING,
160                             "undefined ACL '%s'", aclname);
161                 return (result);
162         }
163         /*
164          * Add a loop detection element.
165          */
166         memset(&loop, 0, sizeof(loop));
167         ISC_LINK_INIT(&loop, nextincache);
168         DE_CONST(aclname, loop.name);
169         loop.magic = LOOP_MAGIC;
170         ISC_LIST_APPEND(ctx->named_acl_cache, &loop, nextincache);
171         result = cfg_acl_fromconfig(cacl, cctx, lctx, ctx, mctx,
172                                     nest_level, &dacl);
173         ISC_LIST_UNLINK(ctx->named_acl_cache, &loop, nextincache);
174         loop.magic = 0;
175         loop.name = NULL;
176         if (result != ISC_R_SUCCESS)
177                 return (result);
178         dacl->name = isc_mem_strdup(dacl->mctx, aclname);
179         if (dacl->name == NULL)
180                 return (ISC_R_NOMEMORY);
181         ISC_LIST_APPEND(ctx->named_acl_cache, dacl, nextincache);
182         dns_acl_attach(dacl, target);
183         return (ISC_R_SUCCESS);
184 }
185
186 static isc_result_t
187 convert_keyname(const cfg_obj_t *keyobj, isc_log_t *lctx, isc_mem_t *mctx,
188                 dns_name_t *dnsname)
189 {
190         isc_result_t result;
191         isc_buffer_t buf;
192         dns_fixedname_t fixname;
193         unsigned int keylen;
194         const char *txtname = cfg_obj_asstring(keyobj);
195
196         keylen = strlen(txtname);
197         isc_buffer_init(&buf, txtname, keylen);
198         isc_buffer_add(&buf, keylen);
199         dns_fixedname_init(&fixname);
200         result = dns_name_fromtext(dns_fixedname_name(&fixname), &buf,
201                                    dns_rootname, 0, NULL);
202         if (result != ISC_R_SUCCESS) {
203                 cfg_obj_log(keyobj, lctx, ISC_LOG_WARNING,
204                             "key name '%s' is not a valid domain name",
205                             txtname);
206                 return (result);
207         }
208         return (dns_name_dup(dns_fixedname_name(&fixname), mctx, dnsname));
209 }
210
211 /*
212  * Recursively pre-parse an ACL definition to find the total number
213  * of non-IP-prefix elements (localhost, localnets, key) in all nested
214  * ACLs, so that the parent will have enough space allocated for the
215  * elements table after all the nested ACLs have been merged in to the
216  * parent.
217  */
218 static int
219 count_acl_elements(const cfg_obj_t *caml, const cfg_obj_t *cctx,
220                    isc_boolean_t *has_negative)
221 {
222         const cfg_listelt_t *elt;
223         const cfg_obj_t *cacl = NULL;
224         isc_result_t result;
225         int n = 0;
226
227         if (has_negative != NULL)
228                 *has_negative = ISC_FALSE;
229
230         for (elt = cfg_list_first(caml);
231              elt != NULL;
232              elt = cfg_list_next(elt)) {
233                 const cfg_obj_t *ce = cfg_listelt_value(elt);
234
235                 /* negated element; just get the value. */
236                 if (cfg_obj_istuple(ce)) {
237                         ce = cfg_tuple_get(ce, "value");
238                         if (has_negative != NULL)
239                                 *has_negative = ISC_TRUE;
240                 }
241
242                 if (cfg_obj_istype(ce, &cfg_type_keyref)) {
243                         n++;
244                 } else if (cfg_obj_islist(ce)) {
245                         isc_boolean_t negative;
246                         n += count_acl_elements(ce, cctx, &negative);
247                         if (negative)
248                                 n++;
249                 } else if (cfg_obj_isstring(ce)) {
250                         const char *name = cfg_obj_asstring(ce);
251                         if (strcasecmp(name, "localhost") == 0 ||
252                             strcasecmp(name, "localnets") == 0) {
253                                 n++;
254                         } else if (strcasecmp(name, "any") != 0 &&
255                                    strcasecmp(name, "none") != 0) {
256                                 result = get_acl_def(cctx, name, &cacl);
257                                 if (result == ISC_R_SUCCESS)
258                                         n += count_acl_elements(cacl, cctx,
259                                                                 NULL) + 1;
260                         }
261                 }
262         }
263
264         return n;
265 }
266
267 isc_result_t
268 cfg_acl_fromconfig(const cfg_obj_t *caml,
269                    const cfg_obj_t *cctx,
270                    isc_log_t *lctx,
271                    cfg_aclconfctx_t *ctx,
272                    isc_mem_t *mctx,
273                    unsigned int nest_level,
274                    dns_acl_t **target)
275 {
276         isc_result_t result;
277         dns_acl_t *dacl = NULL, *inneracl = NULL;
278         dns_aclelement_t *de;
279         const cfg_listelt_t *elt;
280         dns_iptable_t *iptab;
281         int new_nest_level = 0;
282
283         if (nest_level != 0)
284                 new_nest_level = nest_level - 1;
285
286         REQUIRE(target != NULL);
287         REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
288
289         if (*target != NULL) {
290                 /*
291                  * If target already points to an ACL, then we're being
292                  * called recursively to configure a nested ACL.  The
293                  * nested ACL's contents should just be absorbed into its
294                  * parent ACL.
295                  */
296                 dns_acl_attach(*target, &dacl);
297                 dns_acl_detach(target);
298         } else {
299                 /*
300                  * Need to allocate a new ACL structure.  Count the items
301                  * in the ACL definition that will require space in the
302                  * elements table.  (Note that if nest_level is nonzero,
303                  * *everything* goes in the elements table.)
304                  */
305                 int nelem;
306
307                 if (nest_level == 0)
308                         nelem = count_acl_elements(caml, cctx, NULL);
309                 else
310                         nelem = cfg_list_length(caml, ISC_FALSE);
311
312                 result = dns_acl_create(mctx, nelem, &dacl);
313                 if (result != ISC_R_SUCCESS)
314                         return (result);
315         }
316
317         de = dacl->elements;
318         for (elt = cfg_list_first(caml);
319              elt != NULL;
320              elt = cfg_list_next(elt)) {
321                 const cfg_obj_t *ce = cfg_listelt_value(elt);
322                 isc_boolean_t   neg;
323
324                 if (cfg_obj_istuple(ce)) {
325                         /* This must be a negated element. */
326                         ce = cfg_tuple_get(ce, "value");
327                         neg = ISC_TRUE;
328                         dacl->has_negatives = ISC_TRUE;
329                 } else
330                         neg = ISC_FALSE;
331
332                 /*
333                  * If nest_level is nonzero, then every element is
334                  * to be stored as a separate, nested ACL rather than
335                  * merged into the main iptable.
336                  */
337                 iptab = dacl->iptable;
338
339                 if (nest_level != 0) {
340                         result = dns_acl_create(mctx,
341                                                 cfg_list_length(ce, ISC_FALSE),
342                                                 &de->nestedacl);
343                         if (result != ISC_R_SUCCESS)
344                                 goto cleanup;
345                         iptab = de->nestedacl->iptable;
346                 }
347
348                 if (cfg_obj_isnetprefix(ce)) {
349                         /* Network prefix */
350                         isc_netaddr_t   addr;
351                         unsigned int    bitlen;
352
353                         cfg_obj_asnetprefix(ce, &addr, &bitlen);
354
355                         /*
356                          * If nesting ACLs (nest_level != 0), we negate
357                          * the nestedacl element, not the iptable entry.
358                          */
359                         result = dns_iptable_addprefix(iptab, &addr, bitlen,
360                                               ISC_TF(nest_level != 0 || !neg));
361                         if (result != ISC_R_SUCCESS)
362                                 goto cleanup;
363
364                         if (nest_level > 0) {
365                                 de->type = dns_aclelementtype_nestedacl;
366                                 de->negative = neg;
367                         } else
368                                 continue;
369                 } else if (cfg_obj_islist(ce)) {
370                         /*
371                          * If we're nesting ACLs, put the nested
372                          * ACL onto the elements list; otherwise
373                          * merge it into *this* ACL.  We nest ACLs
374                          * in two cases: 1) sortlist, 2) if the
375                          * nested ACL contains negated members.
376                          */
377                         if (inneracl != NULL)
378                                 dns_acl_detach(&inneracl);
379                         result = cfg_acl_fromconfig(ce, cctx, lctx,
380                                                     ctx, mctx, new_nest_level,
381                                                     &inneracl);
382                         if (result != ISC_R_SUCCESS)
383                                 goto cleanup;
384 nested_acl:
385                         if (nest_level > 0 || inneracl->has_negatives) {
386                                 de->type = dns_aclelementtype_nestedacl;
387                                 de->negative = neg;
388                                 if (de->nestedacl != NULL)
389                                         dns_acl_detach(&de->nestedacl);
390                                 dns_acl_attach(inneracl,
391                                                &de->nestedacl);
392                                 dns_acl_detach(&inneracl);
393                                 /* Fall through. */
394                         } else {
395                                 dns_acl_merge(dacl, inneracl,
396                                               ISC_TF(!neg));
397                                 de += inneracl->length;  /* elements added */
398                                 dns_acl_detach(&inneracl);
399                                 continue;
400                         }
401                 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
402                         /* Key name. */
403                         de->type = dns_aclelementtype_keyname;
404                         de->negative = neg;
405                         dns_name_init(&de->keyname, NULL);
406                         result = convert_keyname(ce, lctx, mctx,
407                                                  &de->keyname);
408                         if (result != ISC_R_SUCCESS)
409                                 goto cleanup;
410                 } else if (cfg_obj_isstring(ce)) {
411                         /* ACL name. */
412                         const char *name = cfg_obj_asstring(ce);
413                         if (strcasecmp(name, "any") == 0) {
414                                 /* Iptable entry with zero bit length. */
415                                 result = dns_iptable_addprefix(iptab, NULL, 0,
416                                               ISC_TF(nest_level != 0 || !neg));
417                                 if (result != ISC_R_SUCCESS)
418                                         goto cleanup;
419
420                                 if (nest_level != 0) {
421                                         de->type = dns_aclelementtype_nestedacl;
422                                         de->negative = neg;
423                                 } else
424                                         continue;
425                         } else if (strcasecmp(name, "none") == 0) {
426                                 /* none == !any */
427                                 /*
428                                  * We don't unconditional set
429                                  * dacl->has_negatives and
430                                  * de->negative to true so we can handle
431                                  * "!none;".
432                                  */
433                                 result = dns_iptable_addprefix(iptab, NULL, 0,
434                                               ISC_TF(nest_level != 0 || neg));
435                                 if (result != ISC_R_SUCCESS)
436                                         goto cleanup;
437
438                                 if (!neg)
439                                         dacl->has_negatives = !neg;
440
441                                 if (nest_level != 0) {
442                                         de->type = dns_aclelementtype_nestedacl;
443                                         de->negative = !neg;
444                                 } else
445                                         continue;
446                         } else if (strcasecmp(name, "localhost") == 0) {
447                                 de->type = dns_aclelementtype_localhost;
448                                 de->negative = neg;
449                         } else if (strcasecmp(name, "localnets") == 0) {
450                                 de->type = dns_aclelementtype_localnets;
451                                 de->negative = neg;
452                         } else {
453                                 if (inneracl != NULL)
454                                         dns_acl_detach(&inneracl);
455                                 result = convert_named_acl(ce, cctx, lctx, ctx,
456                                                            mctx, new_nest_level,
457                                                            &inneracl);
458                                 if (result != ISC_R_SUCCESS)
459                                         goto cleanup;
460
461                                 goto nested_acl;
462                         }
463                 } else {
464                         cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
465                                     "address match list contains "
466                                     "unsupported element type");
467                         result = ISC_R_FAILURE;
468                         goto cleanup;
469                 }
470
471                 /*
472                  * This should only be reached for localhost, localnets
473                  * and keyname elements, and nested ACLs if nest_level is
474                  * nonzero (i.e., in sortlists).
475                  */
476                 if (de->nestedacl != NULL &&
477                     de->type != dns_aclelementtype_nestedacl)
478                         dns_acl_detach(&de->nestedacl);
479
480                 dacl->node_count++;
481                 de->node_num = dacl->node_count;
482
483                 dacl->length++;
484                 de++;
485                 INSIST(dacl->length <= dacl->alloc);
486         }
487
488         dns_acl_attach(dacl, target);
489         result = ISC_R_SUCCESS;
490
491  cleanup:
492         if (inneracl != NULL)
493                 dns_acl_detach(&inneracl);
494         dns_acl_detach(&dacl);
495         return (result);
496 }