xref: /freebsd/contrib/unbound/services/modstack.c (revision 7453645f2a9411a3f9d982b768bcc323f41cf906)
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 
50 #ifdef WITH_PYTHONMODULE
51 #include "pythonmod/pythonmod.h"
52 #endif
53 #ifdef USE_CACHEDB
54 #include "cachedb/cachedb.h"
55 #endif
56 
57 /** count number of modules (words) in the string */
58 static int
59 count_modules(const char* s)
60 {
61         int num = 0;
62         if(!s)
63                 return 0;
64         while(*s) {
65                 /* skip whitespace */
66                 while(*s && isspace((unsigned char)*s))
67                         s++;
68                 if(*s && !isspace((unsigned char)*s)) {
69                         /* skip identifier */
70                         num++;
71                         while(*s && !isspace((unsigned char)*s))
72                                 s++;
73                 }
74         }
75         return num;
76 }
77 
78 void
79 modstack_init(struct module_stack* stack)
80 {
81 	stack->num = 0;
82 	stack->mod = NULL;
83 }
84 
85 int
86 modstack_config(struct module_stack* stack, const char* module_conf)
87 {
88         int i;
89         verbose(VERB_QUERY, "module config: \"%s\"", module_conf);
90         stack->num = count_modules(module_conf);
91         if(stack->num == 0) {
92                 log_err("error: no modules specified");
93                 return 0;
94         }
95         if(stack->num > MAX_MODULE) {
96                 log_err("error: too many modules (%d max %d)",
97                         stack->num, MAX_MODULE);
98                 return 0;
99         }
100         stack->mod = (struct module_func_block**)calloc((size_t)
101                 stack->num, sizeof(struct module_func_block*));
102         if(!stack->mod) {
103                 log_err("out of memory");
104                 return 0;
105         }
106         for(i=0; i<stack->num; i++) {
107                 stack->mod[i] = module_factory(&module_conf);
108                 if(!stack->mod[i]) {
109                         log_err("Unknown value for next module: '%s'",
110                                 module_conf);
111                         return 0;
112                 }
113         }
114         return 1;
115 }
116 
117 /** The list of module names */
118 const char**
119 module_list_avail(void)
120 {
121         /* these are the modules available */
122         static const char* names[] = {
123 		"dns64",
124 #ifdef WITH_PYTHONMODULE
125 		"python",
126 #endif
127 #ifdef USE_CACHEDB
128 		"cachedb",
129 #endif
130 		"validator",
131 		"iterator",
132 		NULL};
133 	return names;
134 }
135 
136 /** func block get function type */
137 typedef struct module_func_block* (*fbgetfunctype)(void);
138 
139 /** The list of module func blocks */
140 static fbgetfunctype*
141 module_funcs_avail(void)
142 {
143         static struct module_func_block* (*fb[])(void) = {
144 		&dns64_get_funcblock,
145 #ifdef WITH_PYTHONMODULE
146 		&pythonmod_get_funcblock,
147 #endif
148 #ifdef USE_CACHEDB
149 		&cachedb_get_funcblock,
150 #endif
151 		&val_get_funcblock,
152 		&iter_get_funcblock,
153 		NULL};
154 	return fb;
155 }
156 
157 struct
158 module_func_block* module_factory(const char** str)
159 {
160         int i = 0;
161         const char* s = *str;
162 	const char** names = module_list_avail();
163 	fbgetfunctype* fb = module_funcs_avail();
164         while(*s && isspace((unsigned char)*s))
165                 s++;
166 	while(names[i]) {
167                 if(strncmp(names[i], s, strlen(names[i])) == 0) {
168                         s += strlen(names[i]);
169                         *str = s;
170                         return (*fb[i])();
171                 }
172 		i++;
173         }
174         return NULL;
175 }
176 
177 int
178 modstack_setup(struct module_stack* stack, const char* module_conf,
179 	struct module_env* env)
180 {
181         int i;
182         if(stack->num != 0)
183                 modstack_desetup(stack, env);
184         /* fixed setup of the modules */
185         if(!modstack_config(stack, module_conf)) {
186 		return 0;
187         }
188         env->need_to_validate = 0; /* set by module init below */
189         for(i=0; i<stack->num; i++) {
190                 verbose(VERB_OPS, "init module %d: %s",
191                         i, stack->mod[i]->name);
192                 fptr_ok(fptr_whitelist_mod_init(stack->mod[i]->init));
193                 if(!(*stack->mod[i]->init)(env, i)) {
194                         log_err("module init for module %s failed",
195                                 stack->mod[i]->name);
196 			return 0;
197                 }
198         }
199 	return 1;
200 }
201 
202 void
203 modstack_desetup(struct module_stack* stack, struct module_env* env)
204 {
205         int i;
206         for(i=0; i<stack->num; i++) {
207                 fptr_ok(fptr_whitelist_mod_deinit(stack->mod[i]->deinit));
208                 (*stack->mod[i]->deinit)(env, i);
209         }
210         stack->num = 0;
211         free(stack->mod);
212         stack->mod = NULL;
213 }
214 
215 int
216 modstack_find(struct module_stack* stack, const char* name)
217 {
218 	int i;
219         for(i=0; i<stack->num; i++) {
220 		if(strcmp(stack->mod[i]->name, name) == 0)
221 			return i;
222 	}
223 	return -1;
224 }
225