conf_mod.c revision 127128
1109998Smarkm/* conf_mod.c */
2109998Smarkm/* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL
3109998Smarkm * project 2001.
4109998Smarkm */
5109998Smarkm/* ====================================================================
6109998Smarkm * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7109998Smarkm *
8109998Smarkm * Redistribution and use in source and binary forms, with or without
9109998Smarkm * modification, are permitted provided that the following conditions
10109998Smarkm * are met:
11109998Smarkm *
12109998Smarkm * 1. Redistributions of source code must retain the above copyright
13109998Smarkm *    notice, this list of conditions and the following disclaimer.
14109998Smarkm *
15109998Smarkm * 2. Redistributions in binary form must reproduce the above copyright
16109998Smarkm *    notice, this list of conditions and the following disclaimer in
17109998Smarkm *    the documentation and/or other materials provided with the
18109998Smarkm *    distribution.
19109998Smarkm *
20109998Smarkm * 3. All advertising materials mentioning features or use of this
21109998Smarkm *    software must display the following acknowledgment:
22109998Smarkm *    "This product includes software developed by the OpenSSL Project
23109998Smarkm *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24109998Smarkm *
25109998Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26109998Smarkm *    endorse or promote products derived from this software without
27109998Smarkm *    prior written permission. For written permission, please contact
28109998Smarkm *    licensing@OpenSSL.org.
29109998Smarkm *
30109998Smarkm * 5. Products derived from this software may not be called "OpenSSL"
31109998Smarkm *    nor may "OpenSSL" appear in their names without prior written
32109998Smarkm *    permission of the OpenSSL Project.
33109998Smarkm *
34109998Smarkm * 6. Redistributions of any form whatsoever must retain the following
35109998Smarkm *    acknowledgment:
36109998Smarkm *    "This product includes software developed by the OpenSSL Project
37109998Smarkm *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38109998Smarkm *
39109998Smarkm * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40109998Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41109998Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42109998Smarkm * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43109998Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44109998Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45109998Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46109998Smarkm * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47109998Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48109998Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49109998Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50109998Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE.
51109998Smarkm * ====================================================================
52109998Smarkm *
53109998Smarkm * This product includes cryptographic software written by Eric Young
54109998Smarkm * (eay@cryptsoft.com).  This product includes software written by Tim
55109998Smarkm * Hudson (tjh@cryptsoft.com).
56109998Smarkm *
57109998Smarkm */
58109998Smarkm
59109998Smarkm#include <stdio.h>
60109998Smarkm#include <ctype.h>
61109998Smarkm#include <openssl/crypto.h>
62109998Smarkm#include "cryptlib.h"
63109998Smarkm#include <openssl/conf.h>
64109998Smarkm#include <openssl/dso.h>
65109998Smarkm#include <openssl/x509.h>
66109998Smarkm
67109998Smarkm
68109998Smarkm#define DSO_mod_init_name "OPENSSL_init"
69109998Smarkm#define DSO_mod_finish_name "OPENSSL_finish"
70109998Smarkm
71109998Smarkm
72109998Smarkm/* This structure contains a data about supported modules.
73109998Smarkm * entries in this table correspond to either dynamic or
74109998Smarkm * static modules.
75109998Smarkm */
76109998Smarkm
77109998Smarkmstruct conf_module_st
78109998Smarkm	{
79109998Smarkm	/* DSO of this module or NULL if static */
80109998Smarkm	DSO *dso;
81109998Smarkm	/* Name of the module */
82109998Smarkm	char *name;
83109998Smarkm	/* Init function */
84109998Smarkm	conf_init_func *init;
85109998Smarkm	/* Finish function */
86109998Smarkm	conf_finish_func *finish;
87109998Smarkm	/* Number of successfully initialized modules */
88109998Smarkm	int links;
89109998Smarkm	void *usr_data;
90109998Smarkm	};
91109998Smarkm
92109998Smarkm
93109998Smarkm/* This structure contains information about modules that have been
94109998Smarkm * successfully initialized. There may be more than one entry for a
95109998Smarkm * given module.
96109998Smarkm */
97109998Smarkm
98109998Smarkmstruct conf_imodule_st
99109998Smarkm	{
100109998Smarkm	CONF_MODULE *pmod;
101109998Smarkm	char *name;
102109998Smarkm	char *value;
103109998Smarkm	unsigned long flags;
104109998Smarkm	void *usr_data;
105109998Smarkm	};
106109998Smarkm
107109998Smarkmstatic STACK_OF(CONF_MODULE) *supported_modules = NULL;
108109998Smarkmstatic STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
109109998Smarkm
110109998Smarkmstatic void module_free(CONF_MODULE *md);
111109998Smarkmstatic void module_finish(CONF_IMODULE *imod);
112109998Smarkmstatic int module_run(const CONF *cnf, char *name, char *value,
113109998Smarkm					  unsigned long flags);
114109998Smarkmstatic CONF_MODULE *module_add(DSO *dso, const char *name,
115109998Smarkm			conf_init_func *ifunc, conf_finish_func *ffunc);
116109998Smarkmstatic CONF_MODULE *module_find(char *name);
117109998Smarkmstatic int module_init(CONF_MODULE *pmod, char *name, char *value,
118109998Smarkm					   const CONF *cnf);
119109998Smarkmstatic CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
120109998Smarkm									unsigned long flags);
121109998Smarkm
122109998Smarkm/* Main function: load modules from a CONF structure */
123109998Smarkm
124109998Smarkmint CONF_modules_load(const CONF *cnf, const char *appname,
125109998Smarkm		      unsigned long flags)
126109998Smarkm	{
127109998Smarkm	STACK_OF(CONF_VALUE) *values;
128109998Smarkm	CONF_VALUE *vl;
129109998Smarkm	char *vsection;
130109998Smarkm
131109998Smarkm	int ret, i;
132109998Smarkm
133109998Smarkm	if (!cnf)
134109998Smarkm		return 1;
135109998Smarkm
136109998Smarkm	if (appname == NULL)
137109998Smarkm		appname = "openssl_conf";
138109998Smarkm
139109998Smarkm	vsection = NCONF_get_string(cnf, NULL, appname);
140109998Smarkm
141109998Smarkm	if (!vsection)
142109998Smarkm		{
143109998Smarkm		ERR_clear_error();
144109998Smarkm		return 1;
145109998Smarkm		}
146109998Smarkm
147109998Smarkm	values = NCONF_get_section(cnf, vsection);
148109998Smarkm
149109998Smarkm	if (!values)
150109998Smarkm		return 0;
151109998Smarkm
152109998Smarkm	for (i = 0; i < sk_CONF_VALUE_num(values); i++)
153109998Smarkm		{
154109998Smarkm		vl = sk_CONF_VALUE_value(values, i);
155109998Smarkm		ret = module_run(cnf, vl->name, vl->value, flags);
156109998Smarkm		if (ret <= 0)
157109998Smarkm			if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
158109998Smarkm				return ret;
159109998Smarkm		}
160109998Smarkm
161109998Smarkm	return 1;
162109998Smarkm
163109998Smarkm	}
164109998Smarkm
165109998Smarkmint CONF_modules_load_file(const char *filename, const char *appname,
166109998Smarkm			   unsigned long flags)
167109998Smarkm	{
168109998Smarkm	char *file = NULL;
169109998Smarkm	CONF *conf = NULL;
170109998Smarkm	int ret = 0;
171109998Smarkm	conf = NCONF_new(NULL);
172109998Smarkm	if (!conf)
173109998Smarkm		goto err;
174109998Smarkm
175109998Smarkm	if (filename == NULL)
176109998Smarkm		{
177109998Smarkm		file = CONF_get1_default_config_file();
178109998Smarkm		if (!file)
179109998Smarkm			goto err;
180109998Smarkm		}
181109998Smarkm	else
182109998Smarkm		file = (char *)filename;
183109998Smarkm
184109998Smarkm	if (NCONF_load(conf, file, NULL) <= 0)
185109998Smarkm		{
186109998Smarkm		if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
187109998Smarkm		  (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE))
188109998Smarkm			{
189109998Smarkm			ERR_clear_error();
190109998Smarkm			ret = 1;
191109998Smarkm			}
192109998Smarkm		goto err;
193109998Smarkm		}
194109998Smarkm
195109998Smarkm	ret = CONF_modules_load(conf, appname, flags);
196109998Smarkm
197109998Smarkm	err:
198109998Smarkm	if (filename == NULL)
199109998Smarkm		OPENSSL_free(file);
200109998Smarkm	NCONF_free(conf);
201109998Smarkm
202109998Smarkm	return ret;
203109998Smarkm	}
204109998Smarkm
205109998Smarkmstatic int module_run(const CONF *cnf, char *name, char *value,
206109998Smarkm		      unsigned long flags)
207109998Smarkm	{
208109998Smarkm	CONF_MODULE *md;
209109998Smarkm	int ret;
210109998Smarkm
211109998Smarkm	md = module_find(name);
212109998Smarkm
213109998Smarkm	/* Module not found: try to load DSO */
214109998Smarkm	if (!md && !(flags & CONF_MFLAGS_NO_DSO))
215109998Smarkm		md = module_load_dso(cnf, name, value, flags);
216109998Smarkm
217109998Smarkm	if (!md)
218109998Smarkm		{
219109998Smarkm		if (!(flags & CONF_MFLAGS_SILENT))
220109998Smarkm			{
221109998Smarkm			CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
222109998Smarkm			ERR_add_error_data(2, "module=", name);
223109998Smarkm			}
224109998Smarkm		return -1;
225109998Smarkm		}
226109998Smarkm
227109998Smarkm	ret = module_init(md, name, value, cnf);
228109998Smarkm
229109998Smarkm	if (ret <= 0)
230109998Smarkm		{
231109998Smarkm		if (!(flags & CONF_MFLAGS_SILENT))
232109998Smarkm			{
233109998Smarkm			char rcode[DECIMAL_SIZE(ret)+1];
234109998Smarkm			CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
235127128Snectar			BIO_snprintf(rcode, sizeof rcode, "%-8d", ret);
236109998Smarkm			ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
237109998Smarkm			}
238109998Smarkm		}
239109998Smarkm
240109998Smarkm	return ret;
241109998Smarkm	}
242109998Smarkm
243109998Smarkm/* Load a module from a DSO */
244109998Smarkmstatic CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
245109998Smarkm				    unsigned long flags)
246109998Smarkm	{
247109998Smarkm	DSO *dso = NULL;
248109998Smarkm	conf_init_func *ifunc;
249109998Smarkm	conf_finish_func *ffunc;
250109998Smarkm	char *path = NULL;
251109998Smarkm	int errcode = 0;
252109998Smarkm	CONF_MODULE *md;
253109998Smarkm	/* Look for alternative path in module section */
254109998Smarkm	path = NCONF_get_string(cnf, value, "path");
255109998Smarkm	if (!path)
256109998Smarkm		{
257109998Smarkm		ERR_get_error();
258109998Smarkm		path = name;
259109998Smarkm		}
260109998Smarkm	dso = DSO_load(NULL, path, NULL, 0);
261109998Smarkm	if (!dso)
262109998Smarkm		{
263109998Smarkm		errcode = CONF_R_ERROR_LOADING_DSO;
264109998Smarkm		goto err;
265109998Smarkm		}
266109998Smarkm        ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
267109998Smarkm	if (!ifunc)
268109998Smarkm		{
269109998Smarkm		errcode = CONF_R_MISSING_INIT_FUNCTION;
270109998Smarkm		goto err;
271109998Smarkm		}
272109998Smarkm        ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
273109998Smarkm	/* All OK, add module */
274109998Smarkm	md = module_add(dso, name, ifunc, ffunc);
275109998Smarkm
276109998Smarkm	if (!md)
277109998Smarkm		goto err;
278109998Smarkm
279109998Smarkm	return md;
280109998Smarkm
281109998Smarkm	err:
282109998Smarkm	if (dso)
283109998Smarkm		DSO_free(dso);
284109998Smarkm	CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
285109998Smarkm	ERR_add_error_data(4, "module=", name, ", path=", path);
286109998Smarkm	return NULL;
287109998Smarkm	}
288109998Smarkm
289109998Smarkm/* add module to list */
290109998Smarkmstatic CONF_MODULE *module_add(DSO *dso, const char *name,
291109998Smarkm			       conf_init_func *ifunc, conf_finish_func *ffunc)
292109998Smarkm	{
293109998Smarkm	CONF_MODULE *tmod = NULL;
294109998Smarkm	if (supported_modules == NULL)
295109998Smarkm		supported_modules = sk_CONF_MODULE_new_null();
296109998Smarkm	if (supported_modules == NULL)
297109998Smarkm		return NULL;
298109998Smarkm	tmod = OPENSSL_malloc(sizeof(CONF_MODULE));
299109998Smarkm	if (tmod == NULL)
300109998Smarkm		return NULL;
301109998Smarkm
302109998Smarkm	tmod->dso = dso;
303109998Smarkm	tmod->name = BUF_strdup(name);
304109998Smarkm	tmod->init = ifunc;
305109998Smarkm	tmod->finish = ffunc;
306109998Smarkm	tmod->links = 0;
307109998Smarkm
308109998Smarkm	if (!sk_CONF_MODULE_push(supported_modules, tmod))
309109998Smarkm		{
310109998Smarkm		OPENSSL_free(tmod);
311109998Smarkm		return NULL;
312109998Smarkm		}
313109998Smarkm
314109998Smarkm	return tmod;
315109998Smarkm	}
316109998Smarkm
317109998Smarkm/* Find a module from the list. We allow module names of the
318109998Smarkm * form modname.XXXX to just search for modname to allow the
319109998Smarkm * same module to be initialized more than once.
320109998Smarkm */
321109998Smarkm
322109998Smarkmstatic CONF_MODULE *module_find(char *name)
323109998Smarkm	{
324109998Smarkm	CONF_MODULE *tmod;
325109998Smarkm	int i, nchar;
326109998Smarkm	char *p;
327109998Smarkm	p = strrchr(name, '.');
328109998Smarkm
329109998Smarkm	if (p)
330109998Smarkm		nchar = p - name;
331109998Smarkm	else
332109998Smarkm		nchar = strlen(name);
333109998Smarkm
334109998Smarkm	for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++)
335109998Smarkm		{
336109998Smarkm		tmod = sk_CONF_MODULE_value(supported_modules, i);
337109998Smarkm		if (!strncmp(tmod->name, name, nchar))
338109998Smarkm			return tmod;
339109998Smarkm		}
340109998Smarkm
341109998Smarkm	return NULL;
342109998Smarkm
343109998Smarkm	}
344109998Smarkm
345109998Smarkm/* initialize a module */
346109998Smarkmstatic int module_init(CONF_MODULE *pmod, char *name, char *value,
347109998Smarkm		       const CONF *cnf)
348109998Smarkm	{
349109998Smarkm	int ret = 1;
350109998Smarkm	int init_called = 0;
351109998Smarkm	CONF_IMODULE *imod = NULL;
352109998Smarkm
353109998Smarkm	/* Otherwise add initialized module to list */
354109998Smarkm	imod = OPENSSL_malloc(sizeof(CONF_IMODULE));
355109998Smarkm	if (!imod)
356109998Smarkm		goto err;
357109998Smarkm
358109998Smarkm	imod->pmod = pmod;
359109998Smarkm	imod->name = BUF_strdup(name);
360109998Smarkm	imod->value = BUF_strdup(value);
361109998Smarkm	imod->usr_data = NULL;
362109998Smarkm
363109998Smarkm	if (!imod->name || !imod->value)
364109998Smarkm		goto memerr;
365109998Smarkm
366109998Smarkm	/* Try to initialize module */
367109998Smarkm	if(pmod->init)
368109998Smarkm		{
369109998Smarkm		ret = pmod->init(imod, cnf);
370109998Smarkm		init_called = 1;
371109998Smarkm		/* Error occurred, exit */
372109998Smarkm		if (ret <= 0)
373109998Smarkm			goto err;
374109998Smarkm		}
375109998Smarkm
376109998Smarkm	if (initialized_modules == NULL)
377109998Smarkm		{
378109998Smarkm		initialized_modules = sk_CONF_IMODULE_new_null();
379109998Smarkm		if (!initialized_modules)
380109998Smarkm			{
381109998Smarkm			CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
382109998Smarkm			goto err;
383109998Smarkm			}
384109998Smarkm		}
385109998Smarkm
386109998Smarkm	if (!sk_CONF_IMODULE_push(initialized_modules, imod))
387109998Smarkm		{
388109998Smarkm		CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
389109998Smarkm		goto err;
390109998Smarkm		}
391109998Smarkm
392109998Smarkm	pmod->links++;
393109998Smarkm
394109998Smarkm	return ret;
395109998Smarkm
396109998Smarkm	err:
397109998Smarkm
398109998Smarkm	/* We've started the module so we'd better finish it */
399109998Smarkm	if (pmod->finish && init_called)
400109998Smarkm		pmod->finish(imod);
401109998Smarkm
402109998Smarkm	memerr:
403109998Smarkm	if (imod)
404109998Smarkm		{
405109998Smarkm		if (imod->name)
406109998Smarkm			OPENSSL_free(imod->name);
407109998Smarkm		if (imod->value)
408109998Smarkm			OPENSSL_free(imod->value);
409109998Smarkm		OPENSSL_free(imod);
410109998Smarkm		}
411109998Smarkm
412109998Smarkm	return -1;
413109998Smarkm
414109998Smarkm	}
415109998Smarkm
416109998Smarkm/* Unload any dynamic modules that have a link count of zero:
417109998Smarkm * i.e. have no active initialized modules. If 'all' is set
418109998Smarkm * then all modules are unloaded including static ones.
419109998Smarkm */
420109998Smarkm
421109998Smarkmvoid CONF_modules_unload(int all)
422109998Smarkm	{
423109998Smarkm	int i;
424109998Smarkm	CONF_MODULE *md;
425109998Smarkm	CONF_modules_finish();
426109998Smarkm	/* unload modules in reverse order */
427109998Smarkm	for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
428109998Smarkm		{
429109998Smarkm		md = sk_CONF_MODULE_value(supported_modules, i);
430109998Smarkm		/* If static or in use and 'all' not set ignore it */
431109998Smarkm		if (((md->links > 0) || !md->dso) && !all)
432109998Smarkm			continue;
433109998Smarkm		/* Since we're working in reverse this is OK */
434109998Smarkm		sk_CONF_MODULE_delete(supported_modules, i);
435109998Smarkm		module_free(md);
436109998Smarkm		}
437109998Smarkm	if (sk_CONF_MODULE_num(supported_modules) == 0)
438109998Smarkm		{
439109998Smarkm		sk_CONF_MODULE_free(supported_modules);
440109998Smarkm		supported_modules = NULL;
441109998Smarkm		}
442109998Smarkm	}
443109998Smarkm
444109998Smarkm/* unload a single module */
445109998Smarkmstatic void module_free(CONF_MODULE *md)
446109998Smarkm	{
447109998Smarkm	if (md->dso)
448109998Smarkm		DSO_free(md->dso);
449109998Smarkm	OPENSSL_free(md->name);
450109998Smarkm	OPENSSL_free(md);
451109998Smarkm	}
452109998Smarkm
453109998Smarkm/* finish and free up all modules instances */
454109998Smarkm
455109998Smarkmvoid CONF_modules_finish(void)
456109998Smarkm	{
457109998Smarkm	CONF_IMODULE *imod;
458109998Smarkm	while (sk_CONF_IMODULE_num(initialized_modules) > 0)
459109998Smarkm		{
460109998Smarkm		imod = sk_CONF_IMODULE_pop(initialized_modules);
461109998Smarkm		module_finish(imod);
462109998Smarkm		}
463109998Smarkm	sk_CONF_IMODULE_free(initialized_modules);
464109998Smarkm	initialized_modules = NULL;
465109998Smarkm	}
466109998Smarkm
467109998Smarkm/* finish a module instance */
468109998Smarkm
469109998Smarkmstatic void module_finish(CONF_IMODULE *imod)
470109998Smarkm	{
471109998Smarkm	if (imod->pmod->finish)
472109998Smarkm		imod->pmod->finish(imod);
473109998Smarkm	imod->pmod->links--;
474109998Smarkm	OPENSSL_free(imod->name);
475109998Smarkm	OPENSSL_free(imod->value);
476109998Smarkm	OPENSSL_free(imod);
477109998Smarkm	}
478109998Smarkm
479109998Smarkm/* Add a static module to OpenSSL */
480109998Smarkm
481109998Smarkmint CONF_module_add(const char *name, conf_init_func *ifunc,
482109998Smarkm		    conf_finish_func *ffunc)
483109998Smarkm	{
484109998Smarkm	if (module_add(NULL, name, ifunc, ffunc))
485109998Smarkm		return 1;
486109998Smarkm	else
487109998Smarkm		return 0;
488109998Smarkm	}
489109998Smarkm
490109998Smarkmvoid CONF_modules_free(void)
491109998Smarkm	{
492109998Smarkm	CONF_modules_finish();
493109998Smarkm	CONF_modules_unload(1);
494109998Smarkm	}
495109998Smarkm
496109998Smarkm/* Utility functions */
497109998Smarkm
498109998Smarkmconst char *CONF_imodule_get_name(const CONF_IMODULE *md)
499109998Smarkm	{
500109998Smarkm	return md->name;
501109998Smarkm	}
502109998Smarkm
503109998Smarkmconst char *CONF_imodule_get_value(const CONF_IMODULE *md)
504109998Smarkm	{
505109998Smarkm	return md->value;
506109998Smarkm	}
507109998Smarkm
508109998Smarkmvoid *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
509109998Smarkm	{
510109998Smarkm	return md->usr_data;
511109998Smarkm	}
512109998Smarkm
513109998Smarkmvoid CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
514109998Smarkm	{
515109998Smarkm	md->usr_data = usr_data;
516109998Smarkm	}
517109998Smarkm
518109998SmarkmCONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
519109998Smarkm	{
520109998Smarkm	return md->pmod;
521109998Smarkm	}
522109998Smarkm
523109998Smarkmunsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
524109998Smarkm	{
525109998Smarkm	return md->flags;
526109998Smarkm	}
527109998Smarkm
528109998Smarkmvoid CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
529109998Smarkm	{
530109998Smarkm	md->flags = flags;
531109998Smarkm	}
532109998Smarkm
533109998Smarkmvoid *CONF_module_get_usr_data(CONF_MODULE *pmod)
534109998Smarkm	{
535109998Smarkm	return pmod->usr_data;
536109998Smarkm	}
537109998Smarkm
538109998Smarkmvoid CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
539109998Smarkm	{
540109998Smarkm	pmod->usr_data = usr_data;
541109998Smarkm	}
542109998Smarkm
543109998Smarkm/* Return default config file name */
544109998Smarkm
545109998Smarkmchar *CONF_get1_default_config_file(void)
546109998Smarkm	{
547109998Smarkm	char *file;
548109998Smarkm	int len;
549109998Smarkm
550109998Smarkm	file = getenv("OPENSSL_CONF");
551109998Smarkm	if (file)
552109998Smarkm		return BUF_strdup(file);
553109998Smarkm
554109998Smarkm	len = strlen(X509_get_default_cert_area());
555109998Smarkm#ifndef OPENSSL_SYS_VMS
556109998Smarkm	len++;
557109998Smarkm#endif
558109998Smarkm	len += strlen(OPENSSL_CONF);
559109998Smarkm
560109998Smarkm	file = OPENSSL_malloc(len + 1);
561109998Smarkm
562109998Smarkm	if (!file)
563109998Smarkm		return NULL;
564127128Snectar	BUF_strlcpy(file,X509_get_default_cert_area(),len + 1);
565109998Smarkm#ifndef OPENSSL_SYS_VMS
566127128Snectar	BUF_strlcat(file,"/",len + 1);
567109998Smarkm#endif
568127128Snectar	BUF_strlcat(file,OPENSSL_CONF,len + 1);
569109998Smarkm
570109998Smarkm	return file;
571109998Smarkm	}
572109998Smarkm
573109998Smarkm/* This function takes a list separated by 'sep' and calls the
574109998Smarkm * callback function giving the start and length of each member
575109998Smarkm * optionally stripping leading and trailing whitespace. This can
576109998Smarkm * be used to parse comma separated lists for example.
577109998Smarkm */
578109998Smarkm
579127128Snectarint CONF_parse_list(const char *list_, int sep, int nospc,
580109998Smarkm	int (*list_cb)(const char *elem, int len, void *usr), void *arg)
581109998Smarkm	{
582109998Smarkm	int ret;
583109998Smarkm	const char *lstart, *tmpend, *p;
584127128Snectar	lstart = list_;
585109998Smarkm
586109998Smarkm	for(;;)
587109998Smarkm		{
588109998Smarkm		if (nospc)
589109998Smarkm			{
590109998Smarkm			while(*lstart && isspace((unsigned char)*lstart))
591109998Smarkm				lstart++;
592109998Smarkm			}
593109998Smarkm		p = strchr(lstart, sep);
594109998Smarkm		if (p == lstart || !*lstart)
595109998Smarkm			ret = list_cb(NULL, 0, arg);
596109998Smarkm		else
597109998Smarkm			{
598109998Smarkm			if (p)
599109998Smarkm				tmpend = p - 1;
600109998Smarkm			else
601109998Smarkm				tmpend = lstart + strlen(lstart) - 1;
602109998Smarkm			if (nospc)
603109998Smarkm				{
604109998Smarkm				while(isspace((unsigned char)*tmpend))
605109998Smarkm					tmpend--;
606109998Smarkm				}
607109998Smarkm			ret = list_cb(lstart, tmpend - lstart + 1, arg);
608109998Smarkm			}
609109998Smarkm		if (ret <= 0)
610109998Smarkm			return ret;
611109998Smarkm		if (p == NULL)
612109998Smarkm			return 1;
613109998Smarkm		lstart = p + 1;
614109998Smarkm		}
615109998Smarkm	}
616109998Smarkm
617