Import bind 9.5.2 vendor sources.
[dragonfly.git] / contrib / bind-9.5.2 / 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.17.2.7 2009/01/19 23:47:03 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 {
173         const cfg_listelt_t *elt;
174         const cfg_obj_t *cacl = NULL;
175         isc_result_t result;
176         int n = 0;
177
178         for (elt = cfg_list_first(caml);
179              elt != NULL;
180              elt = cfg_list_next(elt)) {
181                 const cfg_obj_t *ce = cfg_listelt_value(elt);
182
183                 /* negated element; just get the value. */
184                 if (cfg_obj_istuple(ce))
185                         ce = cfg_tuple_get(ce, "value");
186
187                 if (cfg_obj_istype(ce, &cfg_type_keyref)) {
188                         n++;
189                 } else if (cfg_obj_islist(ce)) {
190                         n += count_acl_elements(ce, cctx);
191                 } else if (cfg_obj_isstring(ce)) {
192                         const char *name = cfg_obj_asstring(ce);
193                         if (strcasecmp(name, "localhost") == 0 ||
194                             strcasecmp(name, "localnets") == 0) {
195                                 n++;
196                         } else if (strcasecmp(name, "any") != 0 &&
197                                    strcasecmp(name, "none") != 0) {
198                                 result = get_acl_def(cctx, name, &cacl);
199                                 if (result == ISC_R_SUCCESS)
200                                         n += count_acl_elements(cacl, cctx) + 1;
201                         }
202                 }
203         }
204
205         return n;
206 }
207
208 isc_result_t
209 cfg_acl_fromconfig(const cfg_obj_t *caml,
210                    const cfg_obj_t *cctx,
211                    isc_log_t *lctx,
212                    cfg_aclconfctx_t *ctx,
213                    isc_mem_t *mctx,
214                    unsigned int nest_level,
215                    dns_acl_t **target)
216 {
217         isc_result_t result;
218         dns_acl_t *dacl = NULL, *inneracl = NULL;
219         dns_aclelement_t *de;
220         const cfg_listelt_t *elt;
221         dns_iptable_t *iptab;
222         int new_nest_level = 0;
223
224         if (nest_level != 0)
225                 new_nest_level = nest_level - 1;
226
227         REQUIRE(target != NULL);
228         REQUIRE(*target == NULL || DNS_ACL_VALID(*target));
229
230         if (*target != NULL) {
231                 /*
232                  * If target already points to an ACL, then we're being
233                  * called recursively to configure a nested ACL.  The
234                  * nested ACL's contents should just be absorbed into its
235                  * parent ACL.
236                  */
237                 dns_acl_attach(*target, &dacl);
238                 dns_acl_detach(target);
239         } else {
240                 /*
241                  * Need to allocate a new ACL structure.  Count the items
242                  * in the ACL definition that will require space in the
243                  * elements table.  (Note that if nest_level is nonzero,
244                  * *everything* goes in the elements table.)
245                  */
246                 int nelem;
247
248                 if (nest_level == 0)
249                         nelem = count_acl_elements(caml, cctx);
250                 else
251                         nelem = cfg_list_length(caml, ISC_FALSE);
252
253                 result = dns_acl_create(mctx, nelem, &dacl);
254                 if (result != ISC_R_SUCCESS)
255                         return (result);
256         }
257
258         de = dacl->elements;
259         for (elt = cfg_list_first(caml);
260              elt != NULL;
261              elt = cfg_list_next(elt)) {
262                 const cfg_obj_t *ce = cfg_listelt_value(elt);
263                 isc_boolean_t   neg;
264
265                 if (cfg_obj_istuple(ce)) {
266                         /* This must be a negated element. */
267                         ce = cfg_tuple_get(ce, "value");
268                         neg = ISC_TRUE;
269                         dacl->has_negatives = ISC_TRUE;
270                 } else
271                         neg = ISC_FALSE;
272
273                 /*
274                  * If nest_level is nonzero, then every element is
275                  * to be stored as a separate, nested ACL rather than
276                  * merged into the main iptable.
277                  */
278                 iptab = dacl->iptable;
279
280                 if (nest_level != 0) {
281                         result = dns_acl_create(mctx,
282                                                 cfg_list_length(ce, ISC_FALSE),
283                                                 &de->nestedacl);
284                         if (result != ISC_R_SUCCESS)
285                                 goto cleanup;
286                         iptab = de->nestedacl->iptable;
287                 }
288
289                 if (cfg_obj_isnetprefix(ce)) {
290                         /* Network prefix */
291                         isc_netaddr_t   addr;
292                         unsigned int    bitlen;
293
294                         cfg_obj_asnetprefix(ce, &addr, &bitlen);
295
296                         /*
297                          * If nesting ACLs (nest_level != 0), we negate
298                          * the nestedacl element, not the iptable entry.
299                          */
300                         result = dns_iptable_addprefix(iptab, &addr, bitlen,
301                                               ISC_TF(nest_level != 0 || !neg));
302                         if (result != ISC_R_SUCCESS)
303                                 goto cleanup;
304
305                         if (nest_level > 0) {
306                                 de->type = dns_aclelementtype_nestedacl;
307                                 de->negative = neg;
308                         } else
309                                 continue;
310                 } else if (cfg_obj_islist(ce)) {
311                         /*
312                          * If we're nesting ACLs, put the nested
313                          * ACL onto the elements list; otherwise
314                          * merge it into *this* ACL.  We nest ACLs
315                          * in two cases: 1) sortlist, 2) if the
316                          * nested ACL contains negated members.
317                          */
318                         if (inneracl != NULL)
319                                 dns_acl_detach(&inneracl);
320                         result = cfg_acl_fromconfig(ce, cctx, lctx,
321                                                     ctx, mctx, new_nest_level,
322                                                     &inneracl);
323                         if (result != ISC_R_SUCCESS)
324                                 goto cleanup;
325 nested_acl:
326                         if (nest_level > 0 || inneracl->has_negatives) {
327                                 de->type = dns_aclelementtype_nestedacl;
328                                 de->negative = neg;
329                                 if (de->nestedacl != NULL)
330                                         dns_acl_detach(&de->nestedacl);
331                                 dns_acl_attach(inneracl,
332                                                &de->nestedacl);
333                                 dns_acl_detach(&inneracl);
334                                 /* Fall through. */
335                         } else {
336                                 dns_acl_merge(dacl, inneracl,
337                                               ISC_TF(!neg));
338                                 de += inneracl->length;  /* elements added */
339                                 dns_acl_detach(&inneracl);
340                                 continue;
341                         }
342                 } else if (cfg_obj_istype(ce, &cfg_type_keyref)) {
343                         /* Key name. */
344                         de->type = dns_aclelementtype_keyname;
345                         de->negative = neg;
346                         dns_name_init(&de->keyname, NULL);
347                         result = convert_keyname(ce, lctx, mctx,
348                                                  &de->keyname);
349                         if (result != ISC_R_SUCCESS)
350                                 goto cleanup;
351                 } else if (cfg_obj_isstring(ce)) {
352                         /* ACL name. */
353                         const char *name = cfg_obj_asstring(ce);
354                         if (strcasecmp(name, "any") == 0) {
355                                 /* Iptable entry with zero bit length. */
356                                 result = dns_iptable_addprefix(iptab, NULL, 0,
357                                               ISC_TF(nest_level != 0 || !neg));
358                                 if (result != ISC_R_SUCCESS)
359                                         goto cleanup;
360
361                                 if (nest_level != 0) {
362                                         de->type = dns_aclelementtype_nestedacl;
363                                         de->negative = neg;
364                                 } else
365                                         continue;
366                         } else if (strcasecmp(name, "none") == 0) {
367                                 /* none == !any */
368                                 /*
369                                  * We don't unconditional set
370                                  * dacl->has_negatives and
371                                  * de->negative to true so we can handle
372                                  * "!none;".
373                                  */
374                                 result = dns_iptable_addprefix(iptab, NULL, 0,
375                                               ISC_TF(nest_level != 0 || neg));
376                                 if (result != ISC_R_SUCCESS)
377                                         goto cleanup;
378
379                                 if (!neg)
380                                         dacl->has_negatives = !neg;
381
382                                 if (nest_level != 0) {
383                                         de->type = dns_aclelementtype_nestedacl;
384                                         de->negative = !neg;
385                                 } else
386                                         continue;
387                         } else if (strcasecmp(name, "localhost") == 0) {
388                                 de->type = dns_aclelementtype_localhost;
389                                 de->negative = neg;
390                         } else if (strcasecmp(name, "localnets") == 0) {
391                                 de->type = dns_aclelementtype_localnets;
392                                 de->negative = neg;
393                         } else {
394                                 if (inneracl != NULL)
395                                         dns_acl_detach(&inneracl);
396                                 result = convert_named_acl(ce, cctx, lctx, ctx,
397                                                            mctx, new_nest_level,
398                                                            &inneracl);
399                                 if (result != ISC_R_SUCCESS)
400                                         goto cleanup;
401
402                                 goto nested_acl;
403                         }
404                 } else {
405                         cfg_obj_log(ce, lctx, ISC_LOG_WARNING,
406                                     "address match list contains "
407                                     "unsupported element type");
408                         result = ISC_R_FAILURE;
409                         goto cleanup;
410                 }
411
412                 /*
413                  * This should only be reached for localhost, localnets
414                  * and keyname elements, and nested ACLs if nest_level is
415                  * nonzero (i.e., in sortlists).
416                  */
417                 if (de->nestedacl != NULL &&
418                     de->type != dns_aclelementtype_nestedacl)
419                         dns_acl_detach(&de->nestedacl);
420
421                 dacl->node_count++;
422                 de->node_num = dacl->node_count;
423
424                 dacl->length++;
425                 de++;
426                 INSIST(dacl->length <= dacl->alloc);
427         }
428
429         dns_acl_attach(dacl, target);
430         result = ISC_R_SUCCESS;
431
432  cleanup:
433         if (inneracl != NULL)
434                 dns_acl_detach(&inneracl);
435         dns_acl_detach(&dacl);
436         return (result);
437 }