xref: /freebsd/contrib/unbound/services/modstack.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
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 			char * s = md;
124 			snprintf(md, sizeof(md), "%s", module_conf);
125 			/* Leading spaces are present on errors. */
126 			while (*s && isspace((unsigned char)*s))
127 				s++;
128 			if(strchr(s, ' ')) *(strchr(s, ' ')) = 0;
129 			if(strchr(s, '\t')) *(strchr(s, '\t')) = 0;
130 			log_err("Unknown value in module-config, module: '%s'."
131 				" This module is not present (not compiled in),"
132 				" See the list of linked modules with unbound -V", s);
133 			return 0;
134 		}
135 	}
136 	return 1;
137 }
138 
139 /** The list of module names */
140 const char**
141 module_list_avail(void)
142 {
143 	/* these are the modules available */
144 	static const char* names[] = {
145 		"dns64",
146 #ifdef WITH_PYTHONMODULE
147 		"python",
148 #endif
149 #ifdef WITH_DYNLIBMODULE
150 		"dynlib",
151 #endif
152 #ifdef USE_CACHEDB
153 		"cachedb",
154 #endif
155 #ifdef USE_IPSECMOD
156 		"ipsecmod",
157 #endif
158 #ifdef CLIENT_SUBNET
159 		"subnetcache",
160 #endif
161 #ifdef USE_IPSET
162 		"ipset",
163 #endif
164 		"respip",
165 		"validator",
166 		"iterator",
167 		NULL};
168 	return names;
169 }
170 
171 /** func block get function type */
172 typedef struct module_func_block* (*fbgetfunctype)(void);
173 
174 /** The list of module func blocks */
175 static fbgetfunctype*
176 module_funcs_avail(void)
177 {
178         static struct module_func_block* (*fb[])(void) = {
179 		&dns64_get_funcblock,
180 #ifdef WITH_PYTHONMODULE
181 		&pythonmod_get_funcblock,
182 #endif
183 #ifdef WITH_DYNLIBMODULE
184 		&dynlibmod_get_funcblock,
185 #endif
186 #ifdef USE_CACHEDB
187 		&cachedb_get_funcblock,
188 #endif
189 #ifdef USE_IPSECMOD
190 		&ipsecmod_get_funcblock,
191 #endif
192 #ifdef CLIENT_SUBNET
193 		&subnetmod_get_funcblock,
194 #endif
195 #ifdef USE_IPSET
196 		&ipset_get_funcblock,
197 #endif
198 		&respip_get_funcblock,
199 		&val_get_funcblock,
200 		&iter_get_funcblock,
201 		NULL};
202 	return fb;
203 }
204 
205 struct
206 module_func_block* module_factory(const char** str)
207 {
208         int i = 0;
209         const char* s = *str;
210 	const char** names = module_list_avail();
211 	fbgetfunctype* fb = module_funcs_avail();
212         while(*s && isspace((unsigned char)*s))
213                 s++;
214 	while(names[i]) {
215                 if(strncmp(names[i], s, strlen(names[i])) == 0) {
216                         s += strlen(names[i]);
217                         *str = s;
218                         return (*fb[i])();
219                 }
220 		i++;
221         }
222         return NULL;
223 }
224 
225 int
226 modstack_setup(struct module_stack* stack, const char* module_conf,
227 	struct module_env* env)
228 {
229         int i;
230         if(stack->num != 0)
231                 modstack_desetup(stack, env);
232         /* fixed setup of the modules */
233         if(!modstack_config(stack, module_conf)) {
234 		return 0;
235         }
236         env->need_to_validate = 0; /* set by module init below */
237         for(i=0; i<stack->num; i++) {
238                 verbose(VERB_OPS, "init module %d: %s",
239                         i, stack->mod[i]->name);
240                 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
241                 if(!(*stack->mod[i]->init)(env, i)) {
242                         log_err("module init for module %s failed",
243                                 stack->mod[i]->name);
244 			return 0;
245                 }
246         }
247 	return 1;
248 }
249 
250 void
251 modstack_desetup(struct module_stack* stack, struct module_env* env)
252 {
253         int i;
254         for(i=0; i<stack->num; i++) {
255                 fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
256                 (*stack->mod[i]->deinit)(env, i);
257         }
258         stack->num = 0;
259         free(stack->mod);
260         stack->mod = NULL;
261 }
262 
263 int
264 modstack_find(struct module_stack* stack, const char* name)
265 {
266 	int i;
267 	for(i=0; i<stack->num; i++) {
268 		if(strcmp(stack->mod[i]->name, name) == 0)
269 			return i;
270 	}
271 	return -1;
272 }
273 
274 size_t
275 mod_get_mem(struct module_env* env, const char* name)
276 {
277 	int m = modstack_find(&env->mesh->mods, name);
278 	if(m != -1) {
279 		fptr_ok(fptr_whitelist_mod_get_mem(env->mesh->
280 			mods.mod[m]->get_mem));
281 		return (*env->mesh->mods.mod[m]->get_mem)(env, m);
282 	}
283 	return 0;
284 }
285