xref: /freebsd/contrib/unbound/services/modstack.c (revision 5956d97f4b3204318ceb6aa9c77bd0bc6ea87a41)
1 /*
2  * services/modstack.c - stack of modules
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions to help maintain a stack of modules.
40  */
41 #include "config.h"
42 #include <ctype.h>
43 #include "services/modstack.h"
44 #include "util/module.h"
45 #include "util/fptr_wlist.h"
46 #include "dns64/dns64.h"
47 #include "iterator/iterator.h"
48 #include "validator/validator.h"
49 #include "respip/respip.h"
50 
51 #ifdef WITH_PYTHONMODULE
52 #include "pythonmod/pythonmod.h"
53 #endif
54 #ifdef WITH_DYNLIBMODULE
55 #include "dynlibmod/dynlibmod.h"
56 #endif
57 #ifdef USE_CACHEDB
58 #include "cachedb/cachedb.h"
59 #endif
60 #ifdef USE_IPSECMOD
61 #include "ipsecmod/ipsecmod.h"
62 #endif
63 #ifdef CLIENT_SUBNET
64 #include "edns-subnet/subnetmod.h"
65 #endif
66 #ifdef USE_IPSET
67 #include "ipset/ipset.h"
68 #endif
69 
70 /** count number of modules (words) in the string */
71 static int
72 count_modules(const char* s)
73 {
74         int num = 0;
75         if(!s)
76                 return 0;
77         while(*s) {
78                 /* skip whitespace */
79                 while(*s && isspace((unsigned char)*s))
80                         s++;
81                 if(*s && !isspace((unsigned char)*s)) {
82                         /* skip identifier */
83                         num++;
84                         while(*s && !isspace((unsigned char)*s))
85                                 s++;
86                 }
87         }
88         return num;
89 }
90 
91 void
92 modstack_init(struct module_stack* stack)
93 {
94 	stack->num = 0;
95 	stack->mod = NULL;
96 }
97 
98 int
99 modstack_config(struct module_stack* stack, const char* module_conf)
100 {
101 	int i;
102 	verbose(VERB_QUERY, "module config: \"%s\"", module_conf);
103 	stack->num = count_modules(module_conf);
104 	if(stack->num == 0) {
105 		log_err("error: no modules specified");
106 		return 0;
107 	}
108 	if(stack->num > MAX_MODULE) {
109 		log_err("error: too many modules (%d max %d)",
110 			stack->num, MAX_MODULE);
111 		return 0;
112 	}
113 	stack->mod = (struct module_func_block**)calloc((size_t)
114 		stack->num, sizeof(struct module_func_block*));
115 	if(!stack->mod) {
116 		log_err("out of memory");
117 		return 0;
118 	}
119 	for(i=0; i<stack->num; i++) {
120 		stack->mod[i] = module_factory(&module_conf);
121 		if(!stack->mod[i]) {
122 			char md[256];
123 			snprintf(md, sizeof(md), "%s", module_conf);
124 			if(strchr(md, ' ')) *(strchr(md, ' ')) = 0;
125 			if(strchr(md, '\t')) *(strchr(md, '\t')) = 0;
126 			log_err("Unknown value in module-config, module: '%s'."
127 				" This module is not present (not compiled in),"
128 				" See the list of linked modules with unbound -V", md);
129 			return 0;
130 		}
131 	}
132 	return 1;
133 }
134 
135 /** The list of module names */
136 const char**
137 module_list_avail(void)
138 {
139 	/* these are the modules available */
140 	static const char* names[] = {
141 		"dns64",
142 #ifdef WITH_PYTHONMODULE
143 		"python",
144 #endif
145 #ifdef WITH_DYNLIBMODULE
146 		"dynlib",
147 #endif
148 #ifdef USE_CACHEDB
149 		"cachedb",
150 #endif
151 #ifdef USE_IPSECMOD
152 		"ipsecmod",
153 #endif
154 #ifdef CLIENT_SUBNET
155 		"subnetcache",
156 #endif
157 #ifdef USE_IPSET
158 		"ipset",
159 #endif
160 		"respip",
161 		"validator",
162 		"iterator",
163 		NULL};
164 	return names;
165 }
166 
167 /** func block get function type */
168 typedef struct module_func_block* (*fbgetfunctype)(void);
169 
170 /** The list of module func blocks */
171 static fbgetfunctype*
172 module_funcs_avail(void)
173 {
174         static struct module_func_block* (*fb[])(void) = {
175 		&dns64_get_funcblock,
176 #ifdef WITH_PYTHONMODULE
177 		&pythonmod_get_funcblock,
178 #endif
179 #ifdef WITH_DYNLIBMODULE
180 		&dynlibmod_get_funcblock,
181 #endif
182 #ifdef USE_CACHEDB
183 		&cachedb_get_funcblock,
184 #endif
185 #ifdef USE_IPSECMOD
186 		&ipsecmod_get_funcblock,
187 #endif
188 #ifdef CLIENT_SUBNET
189 		&subnetmod_get_funcblock,
190 #endif
191 #ifdef USE_IPSET
192 		&ipset_get_funcblock,
193 #endif
194 		&respip_get_funcblock,
195 		&val_get_funcblock,
196 		&iter_get_funcblock,
197 		NULL};
198 	return fb;
199 }
200 
201 struct
202 module_func_block* module_factory(const char** str)
203 {
204         int i = 0;
205         const char* s = *str;
206 	const char** names = module_list_avail();
207 	fbgetfunctype* fb = module_funcs_avail();
208         while(*s && isspace((unsigned char)*s))
209                 s++;
210 	while(names[i]) {
211                 if(strncmp(names[i], s, strlen(names[i])) == 0) {
212                         s += strlen(names[i]);
213                         *str = s;
214                         return (*fb[i])();
215                 }
216 		i++;
217         }
218         return NULL;
219 }
220 
221 int
222 modstack_setup(struct module_stack* stack, const char* module_conf,
223 	struct module_env* env)
224 {
225         int i;
226         if(stack->num != 0)
227                 modstack_desetup(stack, env);
228         /* fixed setup of the modules */
229         if(!modstack_config(stack, module_conf)) {
230 		return 0;
231         }
232         env->need_to_validate = 0; /* set by module init below */
233         for(i=0; i<stack->num; i++) {
234                 verbose(VERB_OPS, "init module %d: %s",
235                         i, stack->mod[i]->name);
236                 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
237                 if(!(*stack->mod[i]->init)(env, i)) {
238                         log_err("module init for module %s failed",
239                                 stack->mod[i]->name);
240 			return 0;
241                 }
242         }
243 	return 1;
244 }
245 
246 void
247 modstack_desetup(struct module_stack* stack, struct module_env* env)
248 {
249         int i;
250         for(i=0; i<stack->num; i++) {
251                 fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
252                 (*stack->mod[i]->deinit)(env, i);
253         }
254         stack->num = 0;
255         free(stack->mod);
256         stack->mod = NULL;
257 }
258 
259 int
260 modstack_find(struct module_stack* stack, const char* name)
261 {
262 	int i;
263 	for(i=0; i<stack->num; i++) {
264 		if(strcmp(stack->mod[i]->name, name) == 0)
265 			return i;
266 	}
267 	return -1;
268 }
269 
270 size_t
271 mod_get_mem(struct module_env* env, const char* name)
272 {
273 	int m = modstack_find(&env->mesh->mods, name);
274 	if(m != -1) {
275 		fptr_ok(fptr_whitelist_mod_get_mem(env->mesh->
276 			mods.mod[m]->get_mem));
277 		return (*env->mesh->mods.mod[m]->get_mem)(env, m);
278 	}
279 	return 0;
280 }
281