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).
61 #include <openssl/crypto.h>
63 #include <openssl/conf.h>
64 #include <openssl/dso.h>
65 #include <openssl/x509.h>
68 #define DSO_mod_init_name "OPENSSL_init"
69 #define DSO_mod_finish_name "OPENSSL_finish"
72 /* This structure contains a data about supported modules.
73 * entries in this table correspond to either dynamic or
79 /* DSO of this module or NULL if static */
81 /* Name of the module */
86 conf_finish_func *finish;
87 /* Number of successfully initialized modules */
93 /* This structure contains information about modules that have been
94 * successfully initialized. There may be more than one entry for a
98 struct conf_imodule_st
107 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
108 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
110 static void module_free(CONF_MODULE *md);
111 static void module_finish(CONF_IMODULE *imod);
112 static int module_run(const CONF *cnf, char *name, char *value,
113 unsigned long flags);
114 static CONF_MODULE *module_add(DSO *dso, const char *name,
115 conf_init_func *ifunc, conf_finish_func *ffunc);
116 static CONF_MODULE *module_find(char *name);
117 static int module_init(CONF_MODULE *pmod, char *name, char *value,
119 static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
120 unsigned long flags);
122 /* Main function: load modules from a CONF structure */
124 int CONF_modules_load(const CONF *cnf, const char *appname,
127 STACK_OF(CONF_VALUE) *values;
137 appname = "openssl_conf";
139 vsection = NCONF_get_string(cnf, NULL, appname);
147 values = NCONF_get_section(cnf, vsection);
152 for (i = 0; i < sk_CONF_VALUE_num(values); i++)
154 vl = sk_CONF_VALUE_value(values, i);
155 ret = module_run(cnf, vl->name, vl->value, flags);
157 if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
165 int CONF_modules_load_file(const char *filename, const char *appname,
171 conf = NCONF_new(NULL);
175 if (filename == NULL)
177 file = CONF_get1_default_config_file();
182 file = (char *)filename;
184 if (NCONF_load(conf, file, NULL) <= 0)
186 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
187 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE))
195 ret = CONF_modules_load(conf, appname, flags);
198 if (filename == NULL)
205 static int module_run(const CONF *cnf, char *name, char *value,
211 md = module_find(name);
213 /* Module not found: try to load DSO */
214 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
215 md = module_load_dso(cnf, name, value, flags);
219 if (!(flags & CONF_MFLAGS_SILENT))
221 CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
222 ERR_add_error_data(2, "module=", name);
227 ret = module_init(md, name, value, cnf);
231 if (!(flags & CONF_MFLAGS_SILENT))
233 char rcode[DECIMAL_SIZE(ret)+1];
234 CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
235 sprintf(rcode, "%-8d", ret);
236 ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
243 /* Load a module from a DSO */
244 static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
248 conf_init_func *ifunc;
249 conf_finish_func *ffunc;
253 /* Look for alternative path in module section */
254 path = NCONF_get_string(cnf, value, "path");
260 dso = DSO_load(NULL, path, NULL, 0);
263 errcode = CONF_R_ERROR_LOADING_DSO;
266 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
269 errcode = CONF_R_MISSING_INIT_FUNCTION;
272 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
273 /* All OK, add module */
274 md = module_add(dso, name, ifunc, ffunc);
284 CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
285 ERR_add_error_data(4, "module=", name, ", path=", path);
289 /* add module to list */
290 static CONF_MODULE *module_add(DSO *dso, const char *name,
291 conf_init_func *ifunc, conf_finish_func *ffunc)
293 CONF_MODULE *tmod = NULL;
294 if (supported_modules == NULL)
295 supported_modules = sk_CONF_MODULE_new_null();
296 if (supported_modules == NULL)
298 tmod = OPENSSL_malloc(sizeof(CONF_MODULE));
303 tmod->name = BUF_strdup(name);
305 tmod->finish = ffunc;
308 if (!sk_CONF_MODULE_push(supported_modules, tmod))
317 /* Find a module from the list. We allow module names of the
318 * form modname.XXXX to just search for modname to allow the
319 * same module to be initialized more than once.
322 static CONF_MODULE *module_find(char *name)
327 p = strrchr(name, '.');
332 nchar = strlen(name);
334 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++)
336 tmod = sk_CONF_MODULE_value(supported_modules, i);
337 if (!strncmp(tmod->name, name, nchar))
345 /* initialize a module */
346 static int module_init(CONF_MODULE *pmod, char *name, char *value,
351 CONF_IMODULE *imod = NULL;
353 /* Otherwise add initialized module to list */
354 imod = OPENSSL_malloc(sizeof(CONF_IMODULE));
359 imod->name = BUF_strdup(name);
360 imod->value = BUF_strdup(value);
361 imod->usr_data = NULL;
363 if (!imod->name || !imod->value)
366 /* Try to initialize module */
369 ret = pmod->init(imod, cnf);
371 /* Error occurred, exit */
376 if (initialized_modules == NULL)
378 initialized_modules = sk_CONF_IMODULE_new_null();
379 if (!initialized_modules)
381 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
386 if (!sk_CONF_IMODULE_push(initialized_modules, imod))
388 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
398 /* We've started the module so we'd better finish it */
399 if (pmod->finish && init_called)
406 OPENSSL_free(imod->name);
408 OPENSSL_free(imod->value);
416 /* Unload any dynamic modules that have a link count of zero:
417 * i.e. have no active initialized modules. If 'all' is set
418 * then all modules are unloaded including static ones.
421 void CONF_modules_unload(int all)
425 CONF_modules_finish();
426 /* unload modules in reverse order */
427 for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
429 md = sk_CONF_MODULE_value(supported_modules, i);
430 /* If static or in use and 'all' not set ignore it */
431 if (((md->links > 0) || !md->dso) && !all)
433 /* Since we're working in reverse this is OK */
434 sk_CONF_MODULE_delete(supported_modules, i);
437 if (sk_CONF_MODULE_num(supported_modules) == 0)
439 sk_CONF_MODULE_free(supported_modules);
440 supported_modules = NULL;
444 /* unload a single module */
445 static void module_free(CONF_MODULE *md)
449 OPENSSL_free(md->name);
453 /* finish and free up all modules instances */
455 void CONF_modules_finish(void)
458 while (sk_CONF_IMODULE_num(initialized_modules) > 0)
460 imod = sk_CONF_IMODULE_pop(initialized_modules);
463 sk_CONF_IMODULE_free(initialized_modules);
464 initialized_modules = NULL;
467 /* finish a module instance */
469 static void module_finish(CONF_IMODULE *imod)
471 if (imod->pmod->finish)
472 imod->pmod->finish(imod);
474 OPENSSL_free(imod->name);
475 OPENSSL_free(imod->value);
479 /* Add a static module to OpenSSL */
481 int CONF_module_add(const char *name, conf_init_func *ifunc,
482 conf_finish_func *ffunc)
484 if (module_add(NULL, name, ifunc, ffunc))
490 void CONF_modules_free(void)
492 CONF_modules_finish();
493 CONF_modules_unload(1);
496 /* Utility functions */
498 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
503 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
508 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
513 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
515 md->usr_data = usr_data;
518 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
523 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
528 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
533 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
535 return pmod->usr_data;
538 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
540 pmod->usr_data = usr_data;
543 /* Return default config file name */
545 char *CONF_get1_default_config_file(void)
550 file = getenv("OPENSSL_CONF");
552 return BUF_strdup(file);
554 len = strlen(X509_get_default_cert_area());
555 #ifndef OPENSSL_SYS_VMS
558 len += strlen(OPENSSL_CONF);
560 file = OPENSSL_malloc(len + 1);
564 strcpy(file,X509_get_default_cert_area());
565 #ifndef OPENSSL_SYS_VMS
568 strcat(file,OPENSSL_CONF);
573 /* This function takes a list separated by 'sep' and calls the
574 * callback function giving the start and length of each member
575 * optionally stripping leading and trailing whitespace. This can
576 * be used to parse comma separated lists for example.
579 int CONF_parse_list(const char *list, int sep, int nospc,
580 int (*list_cb)(const char *elem, int len, void *usr), void *arg)
583 const char *lstart, *tmpend, *p;
590 while(*lstart && isspace((unsigned char)*lstart))
593 p = strchr(lstart, sep);
594 if (p == lstart || !*lstart)
595 ret = list_cb(NULL, 0, arg);
601 tmpend = lstart + strlen(lstart) - 1;
604 while(isspace((unsigned char)*tmpend))
607 ret = list_cb(lstart, tmpend - lstart + 1, arg);