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