os/ossrv/ssl/libcrypto/src/crypto/conf/conf_mod.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* conf_mod.c */
sl@0
     2
/* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL
sl@0
     3
 * project 2001.
sl@0
     4
 */
sl@0
     5
/* ====================================================================
sl@0
     6
 * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
sl@0
     7
 *
sl@0
     8
 * Redistribution and use in source and binary forms, with or without
sl@0
     9
 * modification, are permitted provided that the following conditions
sl@0
    10
 * are met:
sl@0
    11
 *
sl@0
    12
 * 1. Redistributions of source code must retain the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    14
 *
sl@0
    15
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    16
 *    notice, this list of conditions and the following disclaimer in
sl@0
    17
 *    the documentation and/or other materials provided with the
sl@0
    18
 *    distribution.
sl@0
    19
 *
sl@0
    20
 * 3. All advertising materials mentioning features or use of this
sl@0
    21
 *    software must display the following acknowledgment:
sl@0
    22
 *    "This product includes software developed by the OpenSSL Project
sl@0
    23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0
    24
 *
sl@0
    25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    26
 *    endorse or promote products derived from this software without
sl@0
    27
 *    prior written permission. For written permission, please contact
sl@0
    28
 *    licensing@OpenSSL.org.
sl@0
    29
 *
sl@0
    30
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    31
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    32
 *    permission of the OpenSSL Project.
sl@0
    33
 *
sl@0
    34
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    35
 *    acknowledgment:
sl@0
    36
 *    "This product includes software developed by the OpenSSL Project
sl@0
    37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0
    38
 *
sl@0
    39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    51
 * ====================================================================
sl@0
    52
 *
sl@0
    53
 * This product includes cryptographic software written by Eric Young
sl@0
    54
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    55
 * Hudson (tjh@cryptsoft.com).
sl@0
    56
 *
sl@0
    57
 */
sl@0
    58
/*
sl@0
    59
 © Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
sl@0
    60
 */
sl@0
    61
#include <stdio.h>
sl@0
    62
#include <ctype.h>
sl@0
    63
#include <openssl/crypto.h>
sl@0
    64
#include "cryptlib.h"
sl@0
    65
#include <openssl/conf.h>
sl@0
    66
#include <openssl/dso.h>
sl@0
    67
#include <openssl/x509.h>
sl@0
    68
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
sl@0
    69
#include "libcrypto_wsd_macros.h"
sl@0
    70
#include "libcrypto_wsd.h"
sl@0
    71
#endif
sl@0
    72
sl@0
    73
sl@0
    74
#define DSO_mod_init_name "OPENSSL_init"
sl@0
    75
#define DSO_mod_finish_name "OPENSSL_finish"
sl@0
    76
sl@0
    77
sl@0
    78
/* This structure contains a data about supported modules.
sl@0
    79
 * entries in this table correspond to either dynamic or
sl@0
    80
 * static modules.
sl@0
    81
 */
sl@0
    82
sl@0
    83
struct conf_module_st
sl@0
    84
	{
sl@0
    85
	/* DSO of this module or NULL if static */
sl@0
    86
	DSO *dso;
sl@0
    87
	/* Name of the module */
sl@0
    88
	char *name;
sl@0
    89
	/* Init function */
sl@0
    90
	conf_init_func *init; 
sl@0
    91
	/* Finish function */
sl@0
    92
	conf_finish_func *finish;
sl@0
    93
	/* Number of successfully initialized modules */
sl@0
    94
	int links;
sl@0
    95
	void *usr_data;
sl@0
    96
	};
sl@0
    97
sl@0
    98
sl@0
    99
/* This structure contains information about modules that have been
sl@0
   100
 * successfully initialized. There may be more than one entry for a
sl@0
   101
 * given module.
sl@0
   102
 */
sl@0
   103
sl@0
   104
struct conf_imodule_st
sl@0
   105
	{
sl@0
   106
	CONF_MODULE *pmod;
sl@0
   107
	char *name;
sl@0
   108
	char *value;
sl@0
   109
	unsigned long flags;
sl@0
   110
	void *usr_data;
sl@0
   111
	};
sl@0
   112
#ifndef EMULATOR
sl@0
   113
static STACK_OF(CONF_MODULE) *supported_modules = NULL;
sl@0
   114
static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
sl@0
   115
#else
sl@0
   116
  GET_STATIC_VAR_FROM_TLS(supported_modules,conf_mod,STACK_OF(CONF_MODULE) *)
sl@0
   117
  #define supported_modules (*GET_WSD_VAR_NAME(supported_modules,conf_mod, s)())
sl@0
   118
  
sl@0
   119
  GET_STATIC_VAR_FROM_TLS(initialized_modules,conf_mod,STACK_OF(CONF_MODULE) *)
sl@0
   120
  #define initialized_modules (*GET_WSD_VAR_NAME(initialized_modules,conf_mod, s)())
sl@0
   121
#endif
sl@0
   122
sl@0
   123
static void module_free(CONF_MODULE *md);
sl@0
   124
static void module_finish(CONF_IMODULE *imod);
sl@0
   125
static int module_run(const CONF *cnf, char *name, char *value,
sl@0
   126
					  unsigned long flags);
sl@0
   127
static CONF_MODULE *module_add(DSO *dso, const char *name,
sl@0
   128
			conf_init_func *ifunc, conf_finish_func *ffunc);
sl@0
   129
static CONF_MODULE *module_find(char *name);
sl@0
   130
static int module_init(CONF_MODULE *pmod, char *name, char *value,
sl@0
   131
					   const CONF *cnf);
sl@0
   132
static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
sl@0
   133
									unsigned long flags);
sl@0
   134
sl@0
   135
/* Main function: load modules from a CONF structure */
sl@0
   136
sl@0
   137
EXPORT_C int CONF_modules_load(const CONF *cnf, const char *appname,
sl@0
   138
		      unsigned long flags)
sl@0
   139
	{
sl@0
   140
	STACK_OF(CONF_VALUE) *values;
sl@0
   141
	CONF_VALUE *vl;
sl@0
   142
	char *vsection = NULL;
sl@0
   143
sl@0
   144
	int ret, i;
sl@0
   145
sl@0
   146
	if (!cnf)
sl@0
   147
		return 1;
sl@0
   148
sl@0
   149
	if (appname)
sl@0
   150
sl@0
   151
	vsection = NCONF_get_string(cnf, NULL, appname); 
sl@0
   152
	if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
sl@0
   153
		vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
sl@0
   154
sl@0
   155
	if (!vsection)
sl@0
   156
		{
sl@0
   157
		ERR_clear_error();
sl@0
   158
		return 1;
sl@0
   159
		}
sl@0
   160
sl@0
   161
	values = NCONF_get_section(cnf, vsection);
sl@0
   162
sl@0
   163
	if (!values)
sl@0
   164
		return 0;
sl@0
   165
sl@0
   166
	for (i = 0; i < sk_CONF_VALUE_num(values); i++)
sl@0
   167
		{
sl@0
   168
		vl = sk_CONF_VALUE_value(values, i);
sl@0
   169
		ret = module_run(cnf, vl->name, vl->value, flags);
sl@0
   170
		if (ret <= 0)
sl@0
   171
			if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
sl@0
   172
				return ret;
sl@0
   173
		}
sl@0
   174
sl@0
   175
	return 1;
sl@0
   176
sl@0
   177
	}
sl@0
   178
sl@0
   179
EXPORT_C int CONF_modules_load_file(const char *filename, const char *appname,
sl@0
   180
			   unsigned long flags)
sl@0
   181
	{
sl@0
   182
	char *file = NULL;
sl@0
   183
	CONF *conf = NULL;
sl@0
   184
	int ret = 0;
sl@0
   185
	conf = NCONF_new(NULL);
sl@0
   186
	if (!conf)
sl@0
   187
		goto err;
sl@0
   188
sl@0
   189
	if (filename == NULL)
sl@0
   190
		{
sl@0
   191
		file = CONF_get1_default_config_file();
sl@0
   192
		if (!file)
sl@0
   193
			goto err;
sl@0
   194
		}
sl@0
   195
	else
sl@0
   196
		file = (char *)filename;
sl@0
   197
sl@0
   198
	if (NCONF_load(conf, file, NULL) <= 0)
sl@0
   199
		{
sl@0
   200
		if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
sl@0
   201
		  (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE))
sl@0
   202
			{
sl@0
   203
			ERR_clear_error();
sl@0
   204
			ret = 1;
sl@0
   205
			}
sl@0
   206
		goto err;
sl@0
   207
		}
sl@0
   208
sl@0
   209
	ret = CONF_modules_load(conf, appname, flags);
sl@0
   210
sl@0
   211
	err:
sl@0
   212
	if (filename == NULL)
sl@0
   213
		OPENSSL_free(file);
sl@0
   214
	NCONF_free(conf);
sl@0
   215
sl@0
   216
	return ret;
sl@0
   217
	}
sl@0
   218
sl@0
   219
static int module_run(const CONF *cnf, char *name, char *value,
sl@0
   220
		      unsigned long flags)
sl@0
   221
	{
sl@0
   222
	CONF_MODULE *md;
sl@0
   223
	int ret;
sl@0
   224
sl@0
   225
	md = module_find(name);
sl@0
   226
sl@0
   227
	/* Module not found: try to load DSO */
sl@0
   228
	if (!md && !(flags & CONF_MFLAGS_NO_DSO))
sl@0
   229
		md = module_load_dso(cnf, name, value, flags);
sl@0
   230
sl@0
   231
	if (!md)
sl@0
   232
		{
sl@0
   233
		if (!(flags & CONF_MFLAGS_SILENT))
sl@0
   234
			{
sl@0
   235
			CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
sl@0
   236
			ERR_add_error_data(2, "module=", name);
sl@0
   237
			}
sl@0
   238
		return -1;
sl@0
   239
		}
sl@0
   240
sl@0
   241
	ret = module_init(md, name, value, cnf);
sl@0
   242
sl@0
   243
	if (ret <= 0)
sl@0
   244
		{
sl@0
   245
		if (!(flags & CONF_MFLAGS_SILENT))
sl@0
   246
			{
sl@0
   247
			char rcode[DECIMAL_SIZE(ret)+1];
sl@0
   248
			CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
sl@0
   249
			BIO_snprintf(rcode, sizeof rcode, "%-8d", ret);
sl@0
   250
			ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
sl@0
   251
			}
sl@0
   252
		}
sl@0
   253
sl@0
   254
	return ret;
sl@0
   255
	}
sl@0
   256
sl@0
   257
/* Load a module from a DSO */
sl@0
   258
static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
sl@0
   259
				    unsigned long flags)
sl@0
   260
	{
sl@0
   261
	DSO *dso = NULL;
sl@0
   262
	conf_init_func *ifunc;
sl@0
   263
	conf_finish_func *ffunc;
sl@0
   264
	char *path = NULL;
sl@0
   265
	int errcode = 0;
sl@0
   266
	CONF_MODULE *md;
sl@0
   267
	/* Look for alternative path in module section */
sl@0
   268
	path = NCONF_get_string(cnf, value, "path");
sl@0
   269
	if (!path)
sl@0
   270
		{
sl@0
   271
		ERR_clear_error();
sl@0
   272
		path = name;
sl@0
   273
		}
sl@0
   274
	dso = DSO_load(NULL, path, NULL, 0);
sl@0
   275
	if (!dso)
sl@0
   276
		{
sl@0
   277
		errcode = CONF_R_ERROR_LOADING_DSO;
sl@0
   278
		goto err;
sl@0
   279
		}
sl@0
   280
        ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
sl@0
   281
	if (!ifunc)
sl@0
   282
		{
sl@0
   283
		errcode = CONF_R_MISSING_INIT_FUNCTION;
sl@0
   284
		goto err;
sl@0
   285
		}
sl@0
   286
        ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
sl@0
   287
	/* All OK, add module */
sl@0
   288
	md = module_add(dso, name, ifunc, ffunc);
sl@0
   289
sl@0
   290
	if (!md)
sl@0
   291
		goto err;
sl@0
   292
sl@0
   293
	return md;
sl@0
   294
sl@0
   295
	err:
sl@0
   296
	if (dso)
sl@0
   297
		DSO_free(dso);
sl@0
   298
	CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
sl@0
   299
	ERR_add_error_data(4, "module=", name, ", path=", path);
sl@0
   300
	return NULL;
sl@0
   301
	}
sl@0
   302
sl@0
   303
/* add module to list */
sl@0
   304
static CONF_MODULE *module_add(DSO *dso, const char *name,
sl@0
   305
			       conf_init_func *ifunc, conf_finish_func *ffunc)
sl@0
   306
	{
sl@0
   307
	CONF_MODULE *tmod = NULL;
sl@0
   308
	if (supported_modules == NULL)
sl@0
   309
		supported_modules = sk_CONF_MODULE_new_null();
sl@0
   310
	if (supported_modules == NULL)
sl@0
   311
		return NULL;
sl@0
   312
	tmod = OPENSSL_malloc(sizeof(CONF_MODULE));
sl@0
   313
	if (tmod == NULL)
sl@0
   314
		return NULL;
sl@0
   315
sl@0
   316
	tmod->dso = dso;
sl@0
   317
	tmod->name = BUF_strdup(name);
sl@0
   318
	tmod->init = ifunc;
sl@0
   319
	tmod->finish = ffunc;
sl@0
   320
	tmod->links = 0;
sl@0
   321
sl@0
   322
	if (!sk_CONF_MODULE_push(supported_modules, tmod))
sl@0
   323
		{
sl@0
   324
		OPENSSL_free(tmod);
sl@0
   325
		return NULL;
sl@0
   326
		}
sl@0
   327
sl@0
   328
	return tmod;
sl@0
   329
	}
sl@0
   330
sl@0
   331
/* Find a module from the list. We allow module names of the
sl@0
   332
 * form modname.XXXX to just search for modname to allow the
sl@0
   333
 * same module to be initialized more than once.
sl@0
   334
 */
sl@0
   335
sl@0
   336
static CONF_MODULE *module_find(char *name)
sl@0
   337
	{
sl@0
   338
	CONF_MODULE *tmod;
sl@0
   339
	int i, nchar;
sl@0
   340
	char *p;
sl@0
   341
	p = strrchr(name, '.');
sl@0
   342
sl@0
   343
	if (p)
sl@0
   344
		nchar = p - name;
sl@0
   345
	else 
sl@0
   346
		nchar = strlen(name);
sl@0
   347
sl@0
   348
	for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++)
sl@0
   349
		{
sl@0
   350
		tmod = sk_CONF_MODULE_value(supported_modules, i);
sl@0
   351
		if (!strncmp(tmod->name, name, nchar))
sl@0
   352
			return tmod;
sl@0
   353
		}
sl@0
   354
sl@0
   355
	return NULL;
sl@0
   356
sl@0
   357
	}
sl@0
   358
sl@0
   359
/* initialize a module */
sl@0
   360
static int module_init(CONF_MODULE *pmod, char *name, char *value,
sl@0
   361
		       const CONF *cnf)
sl@0
   362
	{
sl@0
   363
	int ret = 1;
sl@0
   364
	int init_called = 0;
sl@0
   365
	CONF_IMODULE *imod = NULL;
sl@0
   366
sl@0
   367
	/* Otherwise add initialized module to list */
sl@0
   368
	imod = OPENSSL_malloc(sizeof(CONF_IMODULE));
sl@0
   369
	if (!imod)
sl@0
   370
		goto err;
sl@0
   371
sl@0
   372
	imod->pmod = pmod;
sl@0
   373
	imod->name = BUF_strdup(name);
sl@0
   374
	imod->value = BUF_strdup(value);
sl@0
   375
	imod->usr_data = NULL;
sl@0
   376
sl@0
   377
	if (!imod->name || !imod->value)
sl@0
   378
		goto memerr;
sl@0
   379
sl@0
   380
	/* Try to initialize module */
sl@0
   381
	if(pmod->init)
sl@0
   382
		{
sl@0
   383
		ret = pmod->init(imod, cnf);
sl@0
   384
		init_called = 1;
sl@0
   385
		/* Error occurred, exit */
sl@0
   386
		if (ret <= 0)
sl@0
   387
			goto err;
sl@0
   388
		}
sl@0
   389
sl@0
   390
	if (initialized_modules == NULL)
sl@0
   391
		{
sl@0
   392
		initialized_modules = sk_CONF_IMODULE_new_null();
sl@0
   393
		if (!initialized_modules)
sl@0
   394
			{
sl@0
   395
			CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
sl@0
   396
			goto err;
sl@0
   397
			}
sl@0
   398
		}
sl@0
   399
sl@0
   400
	if (!sk_CONF_IMODULE_push(initialized_modules, imod))
sl@0
   401
		{
sl@0
   402
		CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
sl@0
   403
		goto err;
sl@0
   404
		}
sl@0
   405
sl@0
   406
	pmod->links++;
sl@0
   407
sl@0
   408
	return ret;
sl@0
   409
sl@0
   410
	err:
sl@0
   411
sl@0
   412
	/* We've started the module so we'd better finish it */
sl@0
   413
	if (pmod->finish && init_called)
sl@0
   414
		pmod->finish(imod);
sl@0
   415
sl@0
   416
	memerr:
sl@0
   417
	if (imod)
sl@0
   418
		{
sl@0
   419
		if (imod->name)
sl@0
   420
			OPENSSL_free(imod->name);
sl@0
   421
		if (imod->value)
sl@0
   422
			OPENSSL_free(imod->value);
sl@0
   423
		OPENSSL_free(imod);
sl@0
   424
		}
sl@0
   425
sl@0
   426
	return -1;
sl@0
   427
sl@0
   428
	}
sl@0
   429
sl@0
   430
/* Unload any dynamic modules that have a link count of zero:
sl@0
   431
 * i.e. have no active initialized modules. If 'all' is set
sl@0
   432
 * then all modules are unloaded including static ones.
sl@0
   433
 */
sl@0
   434
sl@0
   435
EXPORT_C void CONF_modules_unload(int all)
sl@0
   436
	{
sl@0
   437
	int i;
sl@0
   438
	CONF_MODULE *md;
sl@0
   439
	CONF_modules_finish();
sl@0
   440
	/* unload modules in reverse order */
sl@0
   441
	for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
sl@0
   442
		{
sl@0
   443
		md = sk_CONF_MODULE_value(supported_modules, i);
sl@0
   444
		/* If static or in use and 'all' not set ignore it */
sl@0
   445
		if (((md->links > 0) || !md->dso) && !all)
sl@0
   446
			continue;
sl@0
   447
		/* Since we're working in reverse this is OK */
sl@0
   448
		(void)sk_CONF_MODULE_delete(supported_modules, i);
sl@0
   449
		module_free(md);
sl@0
   450
		}
sl@0
   451
	if (sk_CONF_MODULE_num(supported_modules) == 0)
sl@0
   452
		{
sl@0
   453
		sk_CONF_MODULE_free(supported_modules);
sl@0
   454
		supported_modules = NULL;
sl@0
   455
		}
sl@0
   456
	}
sl@0
   457
sl@0
   458
/* unload a single module */
sl@0
   459
static void module_free(CONF_MODULE *md)
sl@0
   460
	{
sl@0
   461
	if (md->dso)
sl@0
   462
		DSO_free(md->dso);
sl@0
   463
	OPENSSL_free(md->name);
sl@0
   464
	OPENSSL_free(md);
sl@0
   465
	}
sl@0
   466
sl@0
   467
/* finish and free up all modules instances */
sl@0
   468
sl@0
   469
EXPORT_C void CONF_modules_finish(void)
sl@0
   470
	{
sl@0
   471
	CONF_IMODULE *imod;
sl@0
   472
	while (sk_CONF_IMODULE_num(initialized_modules) > 0)
sl@0
   473
		{
sl@0
   474
		imod = sk_CONF_IMODULE_pop(initialized_modules);
sl@0
   475
		module_finish(imod);
sl@0
   476
		}
sl@0
   477
	sk_CONF_IMODULE_free(initialized_modules);
sl@0
   478
	initialized_modules = NULL;
sl@0
   479
	}
sl@0
   480
sl@0
   481
/* finish a module instance */
sl@0
   482
sl@0
   483
static void module_finish(CONF_IMODULE *imod)
sl@0
   484
	{
sl@0
   485
	if (imod->pmod->finish)
sl@0
   486
		imod->pmod->finish(imod);
sl@0
   487
	imod->pmod->links--;
sl@0
   488
	OPENSSL_free(imod->name);
sl@0
   489
	OPENSSL_free(imod->value);
sl@0
   490
	OPENSSL_free(imod);
sl@0
   491
	}
sl@0
   492
sl@0
   493
/* Add a static module to OpenSSL */
sl@0
   494
sl@0
   495
EXPORT_C int CONF_module_add(const char *name, conf_init_func *ifunc, 
sl@0
   496
		    conf_finish_func *ffunc)
sl@0
   497
	{
sl@0
   498
	if (module_add(NULL, name, ifunc, ffunc))
sl@0
   499
		return 1;
sl@0
   500
	else
sl@0
   501
		return 0;
sl@0
   502
	}
sl@0
   503
sl@0
   504
EXPORT_C void CONF_modules_free(void)
sl@0
   505
	{
sl@0
   506
	CONF_modules_finish();
sl@0
   507
	CONF_modules_unload(1);
sl@0
   508
	}
sl@0
   509
sl@0
   510
/* Utility functions */
sl@0
   511
sl@0
   512
EXPORT_C const char *CONF_imodule_get_name(const CONF_IMODULE *md)
sl@0
   513
	{
sl@0
   514
	return md->name;
sl@0
   515
	}
sl@0
   516
sl@0
   517
EXPORT_C const char *CONF_imodule_get_value(const CONF_IMODULE *md)
sl@0
   518
	{
sl@0
   519
	return md->value;
sl@0
   520
	}
sl@0
   521
sl@0
   522
EXPORT_C void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
sl@0
   523
	{
sl@0
   524
	return md->usr_data;
sl@0
   525
	}
sl@0
   526
sl@0
   527
EXPORT_C void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
sl@0
   528
	{
sl@0
   529
	md->usr_data = usr_data;
sl@0
   530
	}
sl@0
   531
sl@0
   532
EXPORT_C CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
sl@0
   533
	{
sl@0
   534
	return md->pmod;
sl@0
   535
	}
sl@0
   536
sl@0
   537
EXPORT_C unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
sl@0
   538
	{
sl@0
   539
	return md->flags;
sl@0
   540
	}
sl@0
   541
sl@0
   542
EXPORT_C void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
sl@0
   543
	{
sl@0
   544
	md->flags = flags;
sl@0
   545
	}
sl@0
   546
sl@0
   547
EXPORT_C void *CONF_module_get_usr_data(CONF_MODULE *pmod)
sl@0
   548
	{
sl@0
   549
	return pmod->usr_data;
sl@0
   550
	}
sl@0
   551
sl@0
   552
EXPORT_C void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
sl@0
   553
	{
sl@0
   554
	pmod->usr_data = usr_data;
sl@0
   555
	}
sl@0
   556
sl@0
   557
/* Return default config file name */
sl@0
   558
sl@0
   559
EXPORT_C char *CONF_get1_default_config_file(void)
sl@0
   560
	{
sl@0
   561
	char *file;
sl@0
   562
	int len;
sl@0
   563
sl@0
   564
	file = getenv("OPENSSL_CONF");
sl@0
   565
	if (file) 
sl@0
   566
		return BUF_strdup(file);
sl@0
   567
sl@0
   568
	len = strlen(X509_get_default_cert_area());
sl@0
   569
#ifndef OPENSSL_SYS_VMS
sl@0
   570
	len++;
sl@0
   571
#endif
sl@0
   572
	len += strlen(OPENSSL_CONF);
sl@0
   573
sl@0
   574
	file = OPENSSL_malloc(len + 1);
sl@0
   575
sl@0
   576
	if (!file)
sl@0
   577
		return NULL;
sl@0
   578
	BUF_strlcpy(file,X509_get_default_cert_area(),len + 1);
sl@0
   579
#ifndef OPENSSL_SYS_VMS
sl@0
   580
	BUF_strlcat(file,"/",len + 1);
sl@0
   581
#endif
sl@0
   582
	BUF_strlcat(file,OPENSSL_CONF,len + 1);
sl@0
   583
sl@0
   584
	return file;
sl@0
   585
	}
sl@0
   586
sl@0
   587
/* This function takes a list separated by 'sep' and calls the
sl@0
   588
 * callback function giving the start and length of each member
sl@0
   589
 * optionally stripping leading and trailing whitespace. This can
sl@0
   590
 * be used to parse comma separated lists for example.
sl@0
   591
 */
sl@0
   592
sl@0
   593
EXPORT_C int CONF_parse_list(const char *list_, int sep, int nospc,
sl@0
   594
	int (*list_cb)(const char *elem, int len, void *usr), void *arg)
sl@0
   595
	{
sl@0
   596
	int ret;
sl@0
   597
	const char *lstart, *tmpend, *p;
sl@0
   598
	lstart = list_;
sl@0
   599
sl@0
   600
	for(;;)
sl@0
   601
		{
sl@0
   602
		if (nospc)
sl@0
   603
			{
sl@0
   604
			while(*lstart && isspace((unsigned char)*lstart))
sl@0
   605
				lstart++;
sl@0
   606
			}
sl@0
   607
		p = strchr(lstart, sep);
sl@0
   608
		if (p == lstart || !*lstart)
sl@0
   609
			ret = list_cb(NULL, 0, arg);
sl@0
   610
		else
sl@0
   611
			{
sl@0
   612
			if (p)
sl@0
   613
				tmpend = p - 1;
sl@0
   614
			else 
sl@0
   615
				tmpend = lstart + strlen(lstart) - 1;
sl@0
   616
			if (nospc)
sl@0
   617
				{
sl@0
   618
				while(isspace((unsigned char)*tmpend))
sl@0
   619
					tmpend--;
sl@0
   620
				}
sl@0
   621
			ret = list_cb(lstart, tmpend - lstart + 1, arg);
sl@0
   622
			}
sl@0
   623
		if (ret <= 0)
sl@0
   624
			return ret;
sl@0
   625
		if (p == NULL)
sl@0
   626
			return 1;
sl@0
   627
		lstart = p + 1;
sl@0
   628
		}
sl@0
   629
	}
sl@0
   630