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
count_modules(const char * s)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
modstack_init(struct module_stack * stack)92 modstack_init(struct module_stack* stack)
93 {
94 stack->num = 0;
95 stack->mod = NULL;
96 }
97
98 void
modstack_free(struct module_stack * stack)99 modstack_free(struct module_stack* stack)
100 {
101 if(!stack)
102 return;
103 stack->num = 0;
104 free(stack->mod);
105 stack->mod = NULL;
106 }
107
108 int
modstack_config(struct module_stack * stack,const char * module_conf)109 modstack_config(struct module_stack* stack, const char* module_conf)
110 {
111 int i;
112 verbose(VERB_QUERY, "module config: \"%s\"", module_conf);
113 stack->num = count_modules(module_conf);
114 if(stack->num == 0) {
115 log_err("error: no modules specified");
116 return 0;
117 }
118 if(stack->num > MAX_MODULE) {
119 log_err("error: too many modules (%d max %d)",
120 stack->num, MAX_MODULE);
121 return 0;
122 }
123 stack->mod = (struct module_func_block**)calloc((size_t)
124 stack->num, sizeof(struct module_func_block*));
125 if(!stack->mod) {
126 log_err("out of memory");
127 return 0;
128 }
129 for(i=0; i<stack->num; i++) {
130 stack->mod[i] = module_factory(&module_conf);
131 if(!stack->mod[i]) {
132 char md[256];
133 char * s = md;
134 snprintf(md, sizeof(md), "%s", module_conf);
135 /* Leading spaces are present on errors. */
136 while (*s && isspace((unsigned char)*s))
137 s++;
138 if(strchr(s, ' ')) *(strchr(s, ' ')) = 0;
139 if(strchr(s, '\t')) *(strchr(s, '\t')) = 0;
140 log_err("Unknown value in module-config, module: '%s'."
141 " This module is not present (not compiled in),"
142 " See the list of linked modules with unbound -V", s);
143 return 0;
144 }
145 }
146 return 1;
147 }
148
149 /** The list of module names */
150 const char**
module_list_avail(void)151 module_list_avail(void)
152 {
153 /* these are the modules available */
154 static const char* names[] = {
155 "dns64",
156 #ifdef WITH_PYTHONMODULE
157 "python",
158 #endif
159 #ifdef WITH_DYNLIBMODULE
160 "dynlib",
161 #endif
162 #ifdef USE_CACHEDB
163 "cachedb",
164 #endif
165 #ifdef USE_IPSECMOD
166 "ipsecmod",
167 #endif
168 #ifdef CLIENT_SUBNET
169 "subnetcache",
170 #endif
171 #ifdef USE_IPSET
172 "ipset",
173 #endif
174 "respip",
175 "validator",
176 "iterator",
177 NULL};
178 return names;
179 }
180
181 /** func block get function type */
182 typedef struct module_func_block* (*fbgetfunctype)(void);
183
184 /** The list of module func blocks */
185 static fbgetfunctype*
module_funcs_avail(void)186 module_funcs_avail(void)
187 {
188 static struct module_func_block* (*fb[])(void) = {
189 &dns64_get_funcblock,
190 #ifdef WITH_PYTHONMODULE
191 &pythonmod_get_funcblock,
192 #endif
193 #ifdef WITH_DYNLIBMODULE
194 &dynlibmod_get_funcblock,
195 #endif
196 #ifdef USE_CACHEDB
197 &cachedb_get_funcblock,
198 #endif
199 #ifdef USE_IPSECMOD
200 &ipsecmod_get_funcblock,
201 #endif
202 #ifdef CLIENT_SUBNET
203 &subnetmod_get_funcblock,
204 #endif
205 #ifdef USE_IPSET
206 &ipset_get_funcblock,
207 #endif
208 &respip_get_funcblock,
209 &val_get_funcblock,
210 &iter_get_funcblock,
211 NULL};
212 return fb;
213 }
214
215 struct
module_factory(const char ** str)216 module_func_block* module_factory(const char** str)
217 {
218 int i = 0;
219 const char* s = *str;
220 const char** names = module_list_avail();
221 fbgetfunctype* fb = module_funcs_avail();
222 while(*s && isspace((unsigned char)*s))
223 s++;
224 while(names[i]) {
225 if(strncmp(names[i], s, strlen(names[i])) == 0) {
226 s += strlen(names[i]);
227 *str = s;
228 return (*fb[i])();
229 }
230 i++;
231 }
232 return NULL;
233 }
234
235 int
modstack_call_startup(struct module_stack * stack,const char * module_conf,struct module_env * env)236 modstack_call_startup(struct module_stack* stack, const char* module_conf,
237 struct module_env* env)
238 {
239 int i;
240 if(stack->num != 0)
241 fatal_exit("unexpected already initialised modules");
242 /* fixed setup of the modules */
243 if(!modstack_config(stack, module_conf)) {
244 return 0;
245 }
246 for(i=0; i<stack->num; i++) {
247 if(stack->mod[i]->startup == NULL)
248 continue;
249 verbose(VERB_OPS, "startup module %d: %s",
250 i, stack->mod[i]->name);
251 fptr_ok(fptr_whitelist_mod_startup(stack->mod[i]->startup));
252 if(!(*stack->mod[i]->startup)(env, i)) {
253 log_err("module startup for module %s failed",
254 stack->mod[i]->name);
255 return 0;
256 }
257 }
258 return 1;
259 }
260
261 int
modstack_call_init(struct module_stack * stack,const char * module_conf,struct module_env * env)262 modstack_call_init(struct module_stack* stack, const char* module_conf,
263 struct module_env* env)
264 {
265 int i, changed = 0;
266 env->need_to_validate = 0; /* set by module init below */
267 for(i=0; i<stack->num; i++) {
268 while(*module_conf && isspace((unsigned char)*module_conf))
269 module_conf++;
270 if(strncmp(stack->mod[i]->name, module_conf,
271 strlen(stack->mod[i]->name))) {
272 if(stack->mod[i]->startup || stack->mod[i]->destartup) {
273 log_err("changed module ordering during reload not supported, for module that needs startup");
274 return 0;
275 } else {
276 changed = 1;
277 }
278 }
279 module_conf += strlen(stack->mod[i]->name);
280 }
281 if(changed) {
282 modstack_free(stack);
283 if(!modstack_config(stack, module_conf)) {
284 return 0;
285 }
286 }
287
288 for(i=0; i<stack->num; i++) {
289 verbose(VERB_OPS, "init module %d: %s",
290 i, stack->mod[i]->name);
291 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
292 if(!(*stack->mod[i]->init)(env, i)) {
293 log_err("module init for module %s failed",
294 stack->mod[i]->name);
295 return 0;
296 }
297 }
298 return 1;
299 }
300
301 void
modstack_call_deinit(struct module_stack * stack,struct module_env * env)302 modstack_call_deinit(struct module_stack* stack, struct module_env* env)
303 {
304 int i;
305 for(i=0; i<stack->num; i++) {
306 fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
307 (*stack->mod[i]->deinit)(env, i);
308 }
309 }
310
311 void
modstack_call_destartup(struct module_stack * stack,struct module_env * env)312 modstack_call_destartup(struct module_stack* stack, struct module_env* env)
313 {
314 int i;
315 for(i=0; i<stack->num; i++) {
316 if(stack->mod[i]->destartup == NULL)
317 continue;
318 fptr_ok(fptr_whitelist_mod_destartup(stack->mod[i]->destartup));
319 (*stack->mod[i]->destartup)(env, i);
320 }
321 }
322
323 int
modstack_find(struct module_stack * stack,const char * name)324 modstack_find(struct module_stack* stack, const char* name)
325 {
326 int i;
327 for(i=0; i<stack->num; i++) {
328 if(strcmp(stack->mod[i]->name, name) == 0)
329 return i;
330 }
331 return -1;
332 }
333
334 size_t
mod_get_mem(struct module_env * env,const char * name)335 mod_get_mem(struct module_env* env, const char* name)
336 {
337 int m = modstack_find(&env->mesh->mods, name);
338 if(m != -1) {
339 fptr_ok(fptr_whitelist_mod_get_mem(env->mesh->
340 mods.mod[m]->get_mem));
341 return (*env->mesh->mods.mod[m]->get_mem)(env, m);
342 }
343 return 0;
344 }
345