2 * ipsecmod/ipsecmod-whitelist.h - White listed domains for the ipsecmod to
5 * Copyright (c) 2017, NLnet Labs. All rights reserved.
7 * This software is open source.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
20 * Neither the name of the NLNET LABS nor the names of its contributors may
21 * be used to endorse or promote products derived from this software without
22 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * Keep track of the white listed domains for ipsecmod.
45 #include "ipsecmod/ipsecmod.h"
46 #include "ipsecmod/ipsecmod-whitelist.h"
47 #include "util/regional.h"
49 #include "util/config_file.h"
50 #include "util/rbtree.h"
51 #include "util/data/dname.h"
52 #include "util/storage/dnstree.h"
53 #include "sldns/str2wire.h"
55 /** Apply ipsecmod-whitelist string. */
57 whitelist_str_cfg(rbtree_type* whitelist, const char* name)
59 struct name_tree_node* n;
61 uint8_t* nm = sldns_str2wire_dname(name, &len);
63 log_err("ipsecmod: could not parse %s for whitelist.", name);
66 n = (struct name_tree_node*)calloc(1, sizeof(*n));
68 log_err("ipsecmod: out of memory while creating whitelist.");
75 n->labs = dname_count_labels(nm);
76 n->dclass = LDNS_RR_CLASS_IN;
77 if(!name_tree_insert(whitelist, n, nm, len, n->labs, n->dclass)) {
78 /* duplicate element ignored, idempotent */
85 /** Read ipsecmod-whitelist config. */
87 read_whitelist(rbtree_type* whitelist, struct config_file* cfg)
89 struct config_strlist* p;
90 for(p = cfg->ipsecmod_whitelist; p; p = p->next) {
92 if(!whitelist_str_cfg(whitelist, p->str))
99 ipsecmod_whitelist_apply_cfg(struct ipsecmod_env* ie,
100 struct config_file* cfg)
102 ie->whitelist = rbtree_create(name_tree_compare);
103 if(!read_whitelist(ie->whitelist, cfg))
105 name_tree_init_parents(ie->whitelist);
109 /** Delete ipsecmod_env->whitelist element. */
111 whitelist_free(struct rbnode_type* n, void* ATTR_UNUSED(d))
114 free(((struct name_tree_node*)n)->name);
119 /** Get memory usage of ipsecmod_env->whitelist element. */
121 whitelist_get_mem(struct rbnode_type* n, void* arg)
123 struct name_tree_node* node = (struct name_tree_node*)n;
124 size_t* size = (size_t*) arg;
126 *size += sizeof(node) + node->len;
131 ipsecmod_whitelist_delete(rbtree_type* whitelist)
134 traverse_postorder(whitelist, whitelist_free, NULL);
140 ipsecmod_domain_is_whitelisted(struct ipsecmod_env* ie, uint8_t* dname,
141 size_t dname_len, uint16_t qclass)
143 if(!ie->whitelist) return 1; /* No whitelist, treat as whitelisted. */
144 return name_tree_lookup(ie->whitelist, dname, dname_len,
145 dname_count_labels(dname), qclass) != NULL;
149 ipsecmod_whitelist_get_mem(rbtree_type* whitelist)
153 traverse_postorder(whitelist, whitelist_get_mem, &size);
158 #endif /* USE_IPSECMOD */