conf_mod.c revision 109998
1275970Scy/* conf_mod.c */
2275970Scy/* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL
3275970Scy * project 2001.
4275970Scy */
5275970Scy/* ====================================================================
6275970Scy * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7275970Scy *
8275970Scy * Redistribution and use in source and binary forms, with or without
9275970Scy * modification, are permitted provided that the following conditions
10275970Scy * are met:
11275970Scy *
12275970Scy * 1. Redistributions of source code must retain the above copyright
13275970Scy *    notice, this list of conditions and the following disclaimer.
14275970Scy *
15275970Scy * 2. Redistributions in binary form must reproduce the above copyright
16275970Scy *    notice, this list of conditions and the following disclaimer in
17275970Scy *    the documentation and/or other materials provided with the
18275970Scy *    distribution.
19275970Scy *
20275970Scy * 3. All advertising materials mentioning features or use of this
21275970Scy *    software must display the following acknowledgment:
22275970Scy *    "This product includes software developed by the OpenSSL Project
23275970Scy *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24275970Scy *
25275970Scy * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26275970Scy *    endorse or promote products derived from this software without
27275970Scy *    prior written permission. For written permission, please contact
28275970Scy *    licensing@OpenSSL.org.
29275970Scy *
30275970Scy * 5. Products derived from this software may not be called "OpenSSL"
31275970Scy *    nor may "OpenSSL" appear in their names without prior written
32275970Scy *    permission of the OpenSSL Project.
33275970Scy *
34275970Scy * 6. Redistributions of any form whatsoever must retain the following
35275970Scy *    acknowledgment:
36275970Scy *    "This product includes software developed by the OpenSSL Project
37275970Scy *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38275970Scy *
39275970Scy * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40275970Scy * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41275970Scy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42275970Scy * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43275970Scy * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44275970Scy * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45275970Scy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46275970Scy * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47275970Scy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48275970Scy * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49275970Scy * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50275970Scy * OF THE POSSIBILITY OF SUCH DAMAGE.
51275970Scy * ====================================================================
52275970Scy *
53275970Scy * This product includes cryptographic software written by Eric Young
54275970Scy * (eay@cryptsoft.com).  This product includes software written by Tim
55275970Scy * Hudson (tjh@cryptsoft.com).
56275970Scy *
57275970Scy */
58275970Scy
59275970Scy#include <stdio.h>
60275970Scy#include <ctype.h>
61275970Scy#include <openssl/crypto.h>
62275970Scy#include "cryptlib.h"
63275970Scy#include <openssl/conf.h>
64275970Scy#include <openssl/dso.h>
65275970Scy#include <openssl/x509.h>
66275970Scy
67275970Scy
68275970Scy#define DSO_mod_init_name "OPENSSL_init"
69275970Scy#define DSO_mod_finish_name "OPENSSL_finish"
70275970Scy
71275970Scy
72275970Scy/* This structure contains a data about supported modules.
73275970Scy * entries in this table correspond to either dynamic or
74275970Scy * static modules.
75275970Scy */
76275970Scy
77275970Scystruct conf_module_st
78275970Scy	{
79275970Scy	/* DSO of this module or NULL if static */
80275970Scy	DSO *dso;
81275970Scy	/* Name of the module */
82275970Scy	char *name;
83275970Scy	/* Init function */
84275970Scy	conf_init_func *init;
85275970Scy	/* Finish function */
86275970Scy	conf_finish_func *finish;
87275970Scy	/* Number of successfully initialized modules */
88275970Scy	int links;
89275970Scy	void *usr_data;
90275970Scy	};
91275970Scy
92275970Scy
93275970Scy/* This structure contains information about modules that have been
94275970Scy * successfully initialized. There may be more than one entry for a
95275970Scy * given module.
96275970Scy */
97275970Scy
98275970Scystruct conf_imodule_st
99275970Scy	{
100275970Scy	CONF_MODULE *pmod;
101275970Scy	char *name;
102275970Scy	char *value;
103275970Scy	unsigned long flags;
104275970Scy	void *usr_data;
105275970Scy	};
106275970Scy
107275970Scystatic STACK_OF(CONF_MODULE) *supported_modules = NULL;
108275970Scystatic STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
109275970Scy
110275970Scystatic void module_free(CONF_MODULE *md);
111275970Scystatic void module_finish(CONF_IMODULE *imod);
112275970Scystatic int module_run(const CONF *cnf, char *name, char *value,
113275970Scy					  unsigned long flags);
114275970Scystatic CONF_MODULE *module_add(DSO *dso, const char *name,
115275970Scy			conf_init_func *ifunc, conf_finish_func *ffunc);
116275970Scystatic CONF_MODULE *module_find(char *name);
117275970Scystatic int module_init(CONF_MODULE *pmod, char *name, char *value,
118275970Scy					   const CONF *cnf);
119275970Scystatic CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
120275970Scy									unsigned long flags);
121275970Scy
122275970Scy/* Main function: load modules from a CONF structure */
123275970Scy
124275970Scyint CONF_modules_load(const CONF *cnf, const char *appname,
125275970Scy		      unsigned long flags)
126275970Scy	{
127275970Scy	STACK_OF(CONF_VALUE) *values;
128275970Scy	CONF_VALUE *vl;
129275970Scy	char *vsection;
130275970Scy
131275970Scy	int ret, i;
132275970Scy
133275970Scy	if (!cnf)
134275970Scy		return 1;
135275970Scy
136275970Scy	if (appname == NULL)
137275970Scy		appname = "openssl_conf";
138275970Scy
139275970Scy	vsection = NCONF_get_string(cnf, NULL, appname);
140275970Scy
141275970Scy	if (!vsection)
142275970Scy		{
143275970Scy		ERR_clear_error();
144275970Scy		return 1;
145275970Scy		}
146275970Scy
147275970Scy	values = NCONF_get_section(cnf, vsection);
148275970Scy
149275970Scy	if (!values)
150275970Scy		return 0;
151275970Scy
152275970Scy	for (i = 0; i < sk_CONF_VALUE_num(values); i++)
153275970Scy		{
154275970Scy		vl = sk_CONF_VALUE_value(values, i);
155275970Scy		ret = module_run(cnf, vl->name, vl->value, flags);
156275970Scy		if (ret <= 0)
157275970Scy			if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
158275970Scy				return ret;
159275970Scy		}
160275970Scy
161275970Scy	return 1;
162275970Scy
163275970Scy	}
164275970Scy
165275970Scyint CONF_modules_load_file(const char *filename, const char *appname,
166275970Scy			   unsigned long flags)
167275970Scy	{
168275970Scy	char *file = NULL;
169275970Scy	CONF *conf = NULL;
170275970Scy	int ret = 0;
171275970Scy	conf = NCONF_new(NULL);
172275970Scy	if (!conf)
173275970Scy		goto err;
174275970Scy
175275970Scy	if (filename == NULL)
176275970Scy		{
177275970Scy		file = CONF_get1_default_config_file();
178275970Scy		if (!file)
179275970Scy			goto err;
180275970Scy		}
181275970Scy	else
182275970Scy		file = (char *)filename;
183275970Scy
184275970Scy	if (NCONF_load(conf, file, NULL) <= 0)
185275970Scy		{
186275970Scy		if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
187275970Scy		  (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE))
188275970Scy			{
189275970Scy			ERR_clear_error();
190275970Scy			ret = 1;
191275970Scy			}
192275970Scy		goto err;
193275970Scy		}
194275970Scy
195275970Scy	ret = CONF_modules_load(conf, appname, flags);
196275970Scy
197275970Scy	err:
198275970Scy	if (filename == NULL)
199275970Scy		OPENSSL_free(file);
200275970Scy	NCONF_free(conf);
201275970Scy
202275970Scy	return ret;
203275970Scy	}
204275970Scy
205275970Scystatic int module_run(const CONF *cnf, char *name, char *value,
206275970Scy		      unsigned long flags)
207275970Scy	{
208275970Scy	CONF_MODULE *md;
209275970Scy	int ret;
210275970Scy
211275970Scy	md = module_find(name);
212275970Scy
213275970Scy	/* Module not found: try to load DSO */
214280849Scy	if (!md && !(flags & CONF_MFLAGS_NO_DSO))
215275970Scy		md = module_load_dso(cnf, name, value, flags);
216275970Scy
217275970Scy	if (!md)
218275970Scy		{
219275970Scy		if (!(flags & CONF_MFLAGS_SILENT))
220280849Scy			{
221275970Scy			CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
222275970Scy			ERR_add_error_data(2, "module=", name);
223280849Scy			}
224280849Scy		return -1;
225275970Scy		}
226275970Scy
227275970Scy	ret = module_init(md, name, value, cnf);
228275970Scy
229275970Scy	if (ret <= 0)
230275970Scy		{
231275970Scy		if (!(flags & CONF_MFLAGS_SILENT))
232275970Scy			{
233275970Scy			char rcode[DECIMAL_SIZE(ret)+1];
234275970Scy			CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
235275970Scy			sprintf(rcode, "%-8d", ret);
236275970Scy			ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
237275970Scy			}
238275970Scy		}
239275970Scy
240275970Scy	return ret;
241275970Scy	}
242275970Scy
243275970Scy/* Load a module from a DSO */
244275970Scystatic CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
245275970Scy				    unsigned long flags)
246275970Scy	{
247275970Scy	DSO *dso = NULL;
248275970Scy	conf_init_func *ifunc;
249275970Scy	conf_finish_func *ffunc;
250275970Scy	char *path = NULL;
251275970Scy	int errcode = 0;
252275970Scy	CONF_MODULE *md;
253275970Scy	/* Look for alternative path in module section */
254275970Scy	path = NCONF_get_string(cnf, value, "path");
255275970Scy	if (!path)
256275970Scy		{
257275970Scy		ERR_get_error();
258275970Scy		path = name;
259275970Scy		}
260275970Scy	dso = DSO_load(NULL, path, NULL, 0);
261275970Scy	if (!dso)
262275970Scy		{
263275970Scy		errcode = CONF_R_ERROR_LOADING_DSO;
264275970Scy		goto err;
265275970Scy		}
266275970Scy        ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
267275970Scy	if (!ifunc)
268275970Scy		{
269275970Scy		errcode = CONF_R_MISSING_INIT_FUNCTION;
270275970Scy		goto err;
271275970Scy		}
272275970Scy        ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
273275970Scy	/* All OK, add module */
274275970Scy	md = module_add(dso, name, ifunc, ffunc);
275275970Scy
276275970Scy	if (!md)
277275970Scy		goto err;
278275970Scy
279275970Scy	return md;
280275970Scy
281293894Sglebius	err:
282275970Scy	if (dso)
283275970Scy		DSO_free(dso);
284275970Scy	CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
285275970Scy	ERR_add_error_data(4, "module=", name, ", path=", path);
286	return NULL;
287	}
288
289/* add module to list */
290static CONF_MODULE *module_add(DSO *dso, const char *name,
291			       conf_init_func *ifunc, conf_finish_func *ffunc)
292	{
293	CONF_MODULE *tmod = NULL;
294	if (supported_modules == NULL)
295		supported_modules = sk_CONF_MODULE_new_null();
296	if (supported_modules == NULL)
297		return NULL;
298	tmod = OPENSSL_malloc(sizeof(CONF_MODULE));
299	if (tmod == NULL)
300		return NULL;
301
302	tmod->dso = dso;
303	tmod->name = BUF_strdup(name);
304	tmod->init = ifunc;
305	tmod->finish = ffunc;
306	tmod->links = 0;
307
308	if (!sk_CONF_MODULE_push(supported_modules, tmod))
309		{
310		OPENSSL_free(tmod);
311		return NULL;
312		}
313
314	return tmod;
315	}
316
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.
320 */
321
322static CONF_MODULE *module_find(char *name)
323	{
324	CONF_MODULE *tmod;
325	int i, nchar;
326	char *p;
327	p = strrchr(name, '.');
328
329	if (p)
330		nchar = p - name;
331	else
332		nchar = strlen(name);
333
334	for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++)
335		{
336		tmod = sk_CONF_MODULE_value(supported_modules, i);
337		if (!strncmp(tmod->name, name, nchar))
338			return tmod;
339		}
340
341	return NULL;
342
343	}
344
345/* initialize a module */
346static int module_init(CONF_MODULE *pmod, char *name, char *value,
347		       const CONF *cnf)
348	{
349	int ret = 1;
350	int init_called = 0;
351	CONF_IMODULE *imod = NULL;
352
353	/* Otherwise add initialized module to list */
354	imod = OPENSSL_malloc(sizeof(CONF_IMODULE));
355	if (!imod)
356		goto err;
357
358	imod->pmod = pmod;
359	imod->name = BUF_strdup(name);
360	imod->value = BUF_strdup(value);
361	imod->usr_data = NULL;
362
363	if (!imod->name || !imod->value)
364		goto memerr;
365
366	/* Try to initialize module */
367	if(pmod->init)
368		{
369		ret = pmod->init(imod, cnf);
370		init_called = 1;
371		/* Error occurred, exit */
372		if (ret <= 0)
373			goto err;
374		}
375
376	if (initialized_modules == NULL)
377		{
378		initialized_modules = sk_CONF_IMODULE_new_null();
379		if (!initialized_modules)
380			{
381			CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
382			goto err;
383			}
384		}
385
386	if (!sk_CONF_IMODULE_push(initialized_modules, imod))
387		{
388		CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
389		goto err;
390		}
391
392	pmod->links++;
393
394	return ret;
395
396	err:
397
398	/* We've started the module so we'd better finish it */
399	if (pmod->finish && init_called)
400		pmod->finish(imod);
401
402	memerr:
403	if (imod)
404		{
405		if (imod->name)
406			OPENSSL_free(imod->name);
407		if (imod->value)
408			OPENSSL_free(imod->value);
409		OPENSSL_free(imod);
410		}
411
412	return -1;
413
414	}
415
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.
419 */
420
421void CONF_modules_unload(int all)
422	{
423	int i;
424	CONF_MODULE *md;
425	CONF_modules_finish();
426	/* unload modules in reverse order */
427	for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
428		{
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)
432			continue;
433		/* Since we're working in reverse this is OK */
434		sk_CONF_MODULE_delete(supported_modules, i);
435		module_free(md);
436		}
437	if (sk_CONF_MODULE_num(supported_modules) == 0)
438		{
439		sk_CONF_MODULE_free(supported_modules);
440		supported_modules = NULL;
441		}
442	}
443
444/* unload a single module */
445static void module_free(CONF_MODULE *md)
446	{
447	if (md->dso)
448		DSO_free(md->dso);
449	OPENSSL_free(md->name);
450	OPENSSL_free(md);
451	}
452
453/* finish and free up all modules instances */
454
455void CONF_modules_finish(void)
456	{
457	CONF_IMODULE *imod;
458	while (sk_CONF_IMODULE_num(initialized_modules) > 0)
459		{
460		imod = sk_CONF_IMODULE_pop(initialized_modules);
461		module_finish(imod);
462		}
463	sk_CONF_IMODULE_free(initialized_modules);
464	initialized_modules = NULL;
465	}
466
467/* finish a module instance */
468
469static void module_finish(CONF_IMODULE *imod)
470	{
471	if (imod->pmod->finish)
472		imod->pmod->finish(imod);
473	imod->pmod->links--;
474	OPENSSL_free(imod->name);
475	OPENSSL_free(imod->value);
476	OPENSSL_free(imod);
477	}
478
479/* Add a static module to OpenSSL */
480
481int CONF_module_add(const char *name, conf_init_func *ifunc,
482		    conf_finish_func *ffunc)
483	{
484	if (module_add(NULL, name, ifunc, ffunc))
485		return 1;
486	else
487		return 0;
488	}
489
490void CONF_modules_free(void)
491	{
492	CONF_modules_finish();
493	CONF_modules_unload(1);
494	}
495
496/* Utility functions */
497
498const char *CONF_imodule_get_name(const CONF_IMODULE *md)
499	{
500	return md->name;
501	}
502
503const char *CONF_imodule_get_value(const CONF_IMODULE *md)
504	{
505	return md->value;
506	}
507
508void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
509	{
510	return md->usr_data;
511	}
512
513void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
514	{
515	md->usr_data = usr_data;
516	}
517
518CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
519	{
520	return md->pmod;
521	}
522
523unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
524	{
525	return md->flags;
526	}
527
528void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
529	{
530	md->flags = flags;
531	}
532
533void *CONF_module_get_usr_data(CONF_MODULE *pmod)
534	{
535	return pmod->usr_data;
536	}
537
538void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
539	{
540	pmod->usr_data = usr_data;
541	}
542
543/* Return default config file name */
544
545char *CONF_get1_default_config_file(void)
546	{
547	char *file;
548	int len;
549
550	file = getenv("OPENSSL_CONF");
551	if (file)
552		return BUF_strdup(file);
553
554	len = strlen(X509_get_default_cert_area());
555#ifndef OPENSSL_SYS_VMS
556	len++;
557#endif
558	len += strlen(OPENSSL_CONF);
559
560	file = OPENSSL_malloc(len + 1);
561
562	if (!file)
563		return NULL;
564	strcpy(file,X509_get_default_cert_area());
565#ifndef OPENSSL_SYS_VMS
566	strcat(file,"/");
567#endif
568	strcat(file,OPENSSL_CONF);
569
570	return file;
571	}
572
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.
577 */
578
579int CONF_parse_list(const char *list, int sep, int nospc,
580	int (*list_cb)(const char *elem, int len, void *usr), void *arg)
581	{
582	int ret;
583	const char *lstart, *tmpend, *p;
584	lstart = list;
585
586	for(;;)
587		{
588		if (nospc)
589			{
590			while(*lstart && isspace((unsigned char)*lstart))
591				lstart++;
592			}
593		p = strchr(lstart, sep);
594		if (p == lstart || !*lstart)
595			ret = list_cb(NULL, 0, arg);
596		else
597			{
598			if (p)
599				tmpend = p - 1;
600			else
601				tmpend = lstart + strlen(lstart) - 1;
602			if (nospc)
603				{
604				while(isspace((unsigned char)*tmpend))
605					tmpend--;
606				}
607			ret = list_cb(lstart, tmpend - lstart + 1, arg);
608			}
609		if (ret <= 0)
610			return ret;
611		if (p == NULL)
612			return 1;
613		lstart = p + 1;
614		}
615	}
616
617