2 /* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
60 #include <openssl/crypto.h>
62 #include <openssl/conf.h>
63 #include <openssl/dso.h>
64 #include <openssl/x509.h>
67 #define DSO_mod_init_name "OPENSSL_init"
68 #define DSO_mod_finish_name "OPENSSL_finish"
71 /* This structure contains a data about supported modules.
72 * entries in this table correspond to either dynamic or
78 /* DSO of this module or NULL if static */
80 /* Name of the module */
85 conf_finish_func *finish;
86 /* Number of successfully initialized modules */
92 /* This structure contains information about modules that have been
93 * successfully initialized. There may be more than one entry for a
97 struct conf_imodule_st
106 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
107 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
109 static void module_free(CONF_MODULE *md);
110 static void module_finish(CONF_IMODULE *imod);
111 static int module_run(CONF *cnf, char *name, char *value, unsigned long flags);
112 static CONF_MODULE *module_add(DSO *dso, const char *name,
113 conf_init_func *ifunc, conf_finish_func *ffunc);
114 static CONF_MODULE *module_find(char *name);
115 static int module_init(CONF_MODULE *pmod, char *name, char *value, CONF *cnf);
116 static CONF_MODULE *module_load_dso(CONF *cnf, char *name, char *value, unsigned long flags);
118 /* Main function: load modules from a CONF structure */
120 int CONF_modules_load(CONF *cnf, char *appname, unsigned long flags)
122 STACK_OF(CONF_VALUE) *values;
128 if (!cnf || !appname)
132 vsection = NCONF_get_string(cnf, NULL, appname);
140 values = NCONF_get_section(cnf, vsection);
145 for (i = 0; i < sk_CONF_VALUE_num(values); i++)
147 vl = sk_CONF_VALUE_value(values, i);
148 ret = module_run(cnf, vl->name, vl->value, flags);
150 if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
158 int CONF_modules_load_file(char *filename, char *appname, unsigned long flags)
162 conf = NCONF_new(NULL);
166 if (NCONF_load(conf, filename, NULL) <= 0)
169 ret = CONF_modules_load(conf, appname, flags);
177 static int module_run(CONF *cnf, char *name, char *value, unsigned long flags)
182 md = module_find(name);
184 /* Module not found: try to load DSO */
186 md = module_load_dso(cnf, name, value, flags);
190 if (!(flags & CONF_MFLAGS_SILENT))
192 CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
193 ERR_add_error_data(2, "module=", name);
198 ret = module_init(md, name, value, cnf);
202 if (!(flags & CONF_MFLAGS_SILENT))
205 CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
206 sprintf(rcode, "%-8d", ret);
207 ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
214 /* Load a module from a DSO */
215 static CONF_MODULE *module_load_dso(CONF *cnf, char *name, char *value, unsigned long flags)
218 conf_init_func *ifunc;
219 conf_finish_func *ffunc;
223 /* Look for alternative path in module section */
224 path = NCONF_get_string(cnf, value, "path");
230 dso = DSO_load(NULL, path, NULL, 0);
233 errcode = CONF_R_ERROR_LOADING_DSO;
236 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
239 errcode = CONF_R_MISSING_INIT_FUNCTION;
242 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
245 errcode = CONF_R_MISSING_FINISH_FUNCTION;
248 /* All OK, add module */
249 md = module_add(dso, name, ifunc, ffunc);
259 CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
260 ERR_add_error_data(4, "module=", name, ", path=", path);
264 /* add module to list */
265 static CONF_MODULE *module_add(DSO *dso, const char *name,
266 conf_init_func *ifunc, conf_finish_func *ffunc)
268 CONF_MODULE *tmod = NULL;
269 if (supported_modules == NULL)
270 supported_modules = sk_CONF_MODULE_new_null();
271 if (supported_modules == NULL)
273 tmod = OPENSSL_malloc(sizeof(CONF_MODULE));
278 tmod->name = BUF_strdup(name);
280 tmod->finish = ffunc;
283 if (!sk_CONF_MODULE_push(supported_modules, tmod))
292 /* Find a module from the list. We allow module names of the
293 * form modname.XXXX to just search for modname to allow the
294 * same module to be initialized more than once.
297 static CONF_MODULE *module_find(char *name)
302 p = strrchr(name, '.');
307 nchar = strlen(name);
309 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++)
311 tmod = sk_CONF_MODULE_value(supported_modules, i);
312 if (!strncmp(tmod->name, name, nchar))
320 /* initialize a module */
321 static int module_init(CONF_MODULE *pmod, char *name, char *value, CONF *cnf)
325 CONF_IMODULE *imod = NULL;
327 /* Otherwise add initialized module to list */
328 imod = OPENSSL_malloc(sizeof(CONF_IMODULE));
333 imod->name = BUF_strdup(name);
334 imod->value = BUF_strdup(value);
335 imod->usr_data = NULL;
337 if (!imod->name || !imod->value)
340 /* Try to initialize module */
343 ret = pmod->init(imod, cnf);
345 /* Error occurred, exit */
350 if (initialized_modules == NULL)
351 initialized_modules = sk_CONF_IMODULE_new_null();
353 if (!initialized_modules)
356 if (!sk_CONF_IMODULE_push(initialized_modules, imod))
365 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
367 /* We've started the module so we'd better finish it */
368 if (pmod->finish && init_called)
375 OPENSSL_free(imod->name);
377 OPENSSL_free(imod->value);
385 /* Unload any dynamic modules that have a link count of zero:
386 * i.e. have no active initialized modules. If 'all' is set
387 * then all modules are unloaded including static ones.
390 void CONF_modules_unload(int all)
394 /* unload modules in reverse order */
395 for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
397 md = sk_CONF_MODULE_value(supported_modules, i);
398 /* If static or in use and 'all' not set ignore it */
399 if (((md->links > 0) || !md->dso) && !all)
401 /* Since we're working in reverse this is OK */
402 sk_CONF_MODULE_delete(supported_modules, i);
405 if (sk_CONF_MODULE_num(supported_modules) == 0)
407 sk_CONF_MODULE_free(supported_modules);
408 supported_modules = NULL;
412 /* unload a single module */
413 static void module_free(CONF_MODULE *md)
417 OPENSSL_free(md->name);
421 /* finish and free up all modules instances */
423 void CONF_modules_finish(void)
426 while (sk_CONF_IMODULE_num(initialized_modules) > 0)
428 imod = sk_CONF_IMODULE_pop(initialized_modules);
431 sk_CONF_IMODULE_free(initialized_modules);
432 initialized_modules = NULL;
435 /* finish a module instance */
437 static void module_finish(CONF_IMODULE *imod)
439 imod->pmod->finish(imod);
441 OPENSSL_free(imod->name);
442 OPENSSL_free(imod->value);
446 /* Add a static module to OpenSSL */
448 int CONF_module_add(const char *name, conf_init_func *ifunc,
449 conf_finish_func *ffunc)
451 if (module_add(NULL, name, ifunc, ffunc))
457 void CONF_modules_free(void)
459 CONF_modules_finish();
460 CONF_modules_unload(1);
463 /* Utility functions */
465 char *CONF_imodule_get_name(CONF_IMODULE *md)
470 char *CONF_imodule_get_value(CONF_IMODULE *md)
475 void *CONF_imodule_get_usr_data(CONF_IMODULE *md)
480 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
482 md->usr_data = usr_data;
485 CONF_MODULE *CONF_imodule_get_module(CONF_IMODULE *md)
490 unsigned long CONF_imodule_get_flags(CONF_IMODULE *md)
495 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
500 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
502 return pmod->usr_data;
505 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
507 pmod->usr_data = usr_data;