os/ossrv/ssl/libcrypto/src/crypto/conf/conf_api.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* conf_api.c */
sl@0
     2
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
sl@0
     3
 * All rights reserved.
sl@0
     4
 *
sl@0
     5
 * This package is an SSL implementation written
sl@0
     6
 * by Eric Young (eay@cryptsoft.com).
sl@0
     7
 * The implementation was written so as to conform with Netscapes SSL.
sl@0
     8
 * 
sl@0
     9
 * This library is free for commercial and non-commercial use as long as
sl@0
    10
 * the following conditions are aheared to.  The following conditions
sl@0
    11
 * apply to all code found in this distribution, be it the RC4, RSA,
sl@0
    12
 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
sl@0
    13
 * included with this distribution is covered by the same copyright terms
sl@0
    14
 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
sl@0
    15
 * 
sl@0
    16
 * Copyright remains Eric Young's, and as such any Copyright notices in
sl@0
    17
 * the code are not to be removed.
sl@0
    18
 * If this package is used in a product, Eric Young should be given attribution
sl@0
    19
 * as the author of the parts of the library used.
sl@0
    20
 * This can be in the form of a textual message at program startup or
sl@0
    21
 * in documentation (online or textual) provided with the package.
sl@0
    22
 * 
sl@0
    23
 * Redistribution and use in source and binary forms, with or without
sl@0
    24
 * modification, are permitted provided that the following conditions
sl@0
    25
 * are met:
sl@0
    26
 * 1. Redistributions of source code must retain the copyright
sl@0
    27
 *    notice, this list of conditions and the following disclaimer.
sl@0
    28
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    29
 *    notice, this list of conditions and the following disclaimer in the
sl@0
    30
 *    documentation and/or other materials provided with the distribution.
sl@0
    31
 * 3. All advertising materials mentioning features or use of this software
sl@0
    32
 *    must display the following acknowledgement:
sl@0
    33
 *    "This product includes cryptographic software written by
sl@0
    34
 *     Eric Young (eay@cryptsoft.com)"
sl@0
    35
 *    The word 'cryptographic' can be left out if the rouines from the library
sl@0
    36
 *    being used are not cryptographic related :-).
sl@0
    37
 * 4. If you include any Windows specific code (or a derivative thereof) from 
sl@0
    38
 *    the apps directory (application code) you must include an acknowledgement:
sl@0
    39
 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
sl@0
    40
 * 
sl@0
    41
 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
sl@0
    42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
sl@0
    44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
sl@0
    45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sl@0
    46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sl@0
    47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
sl@0
    49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
sl@0
    50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
sl@0
    51
 * SUCH DAMAGE.
sl@0
    52
 * 
sl@0
    53
 * The licence and distribution terms for any publically available version or
sl@0
    54
 * derivative of this code cannot be changed.  i.e. this code cannot simply be
sl@0
    55
 * copied and put under another distribution licence
sl@0
    56
 * [including the GNU Public Licence.]
sl@0
    57
 */
sl@0
    58
sl@0
    59
/* Part of the code in here was originally in conf.c, which is now removed */
sl@0
    60
sl@0
    61
#ifndef CONF_DEBUG
sl@0
    62
# undef NDEBUG /* avoid conflicting definitions */
sl@0
    63
# define NDEBUG
sl@0
    64
#endif
sl@0
    65
sl@0
    66
#include <assert.h>
sl@0
    67
#include <string.h>
sl@0
    68
#include <openssl/conf.h>
sl@0
    69
#include <openssl/conf_api.h>
sl@0
    70
#include "e_os.h"
sl@0
    71
sl@0
    72
static void value_free_hash(CONF_VALUE *a, LHASH *conf);
sl@0
    73
static void value_free_stack(CONF_VALUE *a,LHASH *conf);
sl@0
    74
static IMPLEMENT_LHASH_DOALL_ARG_FN(value_free_hash, CONF_VALUE *, LHASH *)
sl@0
    75
static IMPLEMENT_LHASH_DOALL_ARG_FN(value_free_stack, CONF_VALUE *, LHASH *)
sl@0
    76
/* We don't use function pointer casting or wrapper functions - but cast each
sl@0
    77
 * callback parameter inside the callback functions. */
sl@0
    78
/* static unsigned long hash(CONF_VALUE *v); */
sl@0
    79
static unsigned long hash(const void *v_void);
sl@0
    80
/* static int cmp_conf(CONF_VALUE *a,CONF_VALUE *b); */
sl@0
    81
static int cmp_conf(const void *a_void,const void *b_void);
sl@0
    82
sl@0
    83
/* Up until OpenSSL 0.9.5a, this was get_section */
sl@0
    84
EXPORT_C CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section)
sl@0
    85
	{
sl@0
    86
	CONF_VALUE *v,vv;
sl@0
    87
sl@0
    88
	if ((conf == NULL) || (section == NULL)) return(NULL);
sl@0
    89
	vv.name=NULL;
sl@0
    90
	vv.section=(char *)section;
sl@0
    91
	v=(CONF_VALUE *)lh_retrieve(conf->data,&vv);
sl@0
    92
	return(v);
sl@0
    93
	}
sl@0
    94
sl@0
    95
/* Up until OpenSSL 0.9.5a, this was CONF_get_section */
sl@0
    96
EXPORT_C STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf,
sl@0
    97
					       const char *section)
sl@0
    98
	{
sl@0
    99
	CONF_VALUE *v;
sl@0
   100
sl@0
   101
	v=_CONF_get_section(conf,section);
sl@0
   102
	if (v != NULL)
sl@0
   103
		return((STACK_OF(CONF_VALUE) *)v->value);
sl@0
   104
	else
sl@0
   105
		return(NULL);
sl@0
   106
	}
sl@0
   107
sl@0
   108
EXPORT_C int _CONF_add_string(CONF *conf, CONF_VALUE *section, CONF_VALUE *value)
sl@0
   109
	{
sl@0
   110
	CONF_VALUE *v = NULL;
sl@0
   111
	STACK_OF(CONF_VALUE) *ts;
sl@0
   112
sl@0
   113
	ts = (STACK_OF(CONF_VALUE) *)section->value;
sl@0
   114
sl@0
   115
	value->section=section->section;	
sl@0
   116
	if (!sk_CONF_VALUE_push(ts,value))
sl@0
   117
		{
sl@0
   118
		return 0;
sl@0
   119
		}
sl@0
   120
sl@0
   121
	v = (CONF_VALUE *)lh_insert(conf->data, value);
sl@0
   122
	if (v != NULL)
sl@0
   123
		{
sl@0
   124
		(void)sk_CONF_VALUE_delete_ptr(ts,v);
sl@0
   125
		OPENSSL_free(v->name);
sl@0
   126
		OPENSSL_free(v->value);
sl@0
   127
		OPENSSL_free(v);
sl@0
   128
		}
sl@0
   129
	return 1;
sl@0
   130
	}
sl@0
   131
sl@0
   132
EXPORT_C char *_CONF_get_string(const CONF *conf, const char *section, const char *name)
sl@0
   133
	{
sl@0
   134
	CONF_VALUE *v,vv;
sl@0
   135
	char *p;
sl@0
   136
sl@0
   137
	if (name == NULL) return(NULL);
sl@0
   138
	if (conf != NULL)
sl@0
   139
		{
sl@0
   140
		if (section != NULL)
sl@0
   141
			{
sl@0
   142
			vv.name=(char *)name;
sl@0
   143
			vv.section=(char *)section;
sl@0
   144
			v=(CONF_VALUE *)lh_retrieve(conf->data,&vv);
sl@0
   145
			if (v != NULL) return(v->value);
sl@0
   146
			if (strcmp(section,"ENV") == 0)
sl@0
   147
				{
sl@0
   148
				p=Getenv(name);
sl@0
   149
				if (p != NULL) return(p);
sl@0
   150
				}
sl@0
   151
			}
sl@0
   152
		vv.section="default";
sl@0
   153
		vv.name=(char *)name;
sl@0
   154
		v=(CONF_VALUE *)lh_retrieve(conf->data,&vv);
sl@0
   155
		if (v != NULL)
sl@0
   156
			return(v->value);
sl@0
   157
		else
sl@0
   158
			return(NULL);
sl@0
   159
		}
sl@0
   160
	else
sl@0
   161
		return(Getenv(name));
sl@0
   162
	}
sl@0
   163
sl@0
   164
#if 0 /* There's no way to provide error checking with this function, so
sl@0
   165
	 force implementors of the higher levels to get a string and read
sl@0
   166
	 the number themselves. */
sl@0
   167
EXPORT_C long _CONF_get_number(CONF *conf, char *section, char *name)
sl@0
   168
	{
sl@0
   169
	char *str;
sl@0
   170
	long ret=0;
sl@0
   171
sl@0
   172
	str=_CONF_get_string(conf,section,name);
sl@0
   173
	if (str == NULL) return(0);
sl@0
   174
	for (;;)
sl@0
   175
		{
sl@0
   176
		if (conf->meth->is_number(conf, *str))
sl@0
   177
			ret=ret*10+conf->meth->to_int(conf, *str);
sl@0
   178
		else
sl@0
   179
			return(ret);
sl@0
   180
		str++;
sl@0
   181
		}
sl@0
   182
	}
sl@0
   183
#endif
sl@0
   184
sl@0
   185
EXPORT_C int _CONF_new_data(CONF *conf)
sl@0
   186
	{
sl@0
   187
	if (conf == NULL)
sl@0
   188
		{
sl@0
   189
		return 0;
sl@0
   190
		}
sl@0
   191
	if (conf->data == NULL)
sl@0
   192
		if ((conf->data = lh_new(hash, cmp_conf)) == NULL)
sl@0
   193
			{
sl@0
   194
			return 0;
sl@0
   195
			}
sl@0
   196
	return 1;
sl@0
   197
	}
sl@0
   198
sl@0
   199
EXPORT_C void _CONF_free_data(CONF *conf)
sl@0
   200
	{
sl@0
   201
	if (conf == NULL || conf->data == NULL) return;
sl@0
   202
sl@0
   203
	conf->data->down_load=0; /* evil thing to make sure the 'OPENSSL_free()'
sl@0
   204
				  * works as expected */
sl@0
   205
	lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(value_free_hash),
sl@0
   206
			conf->data);
sl@0
   207
sl@0
   208
	/* We now have only 'section' entries in the hash table.
sl@0
   209
	 * Due to problems with */
sl@0
   210
sl@0
   211
	lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(value_free_stack),
sl@0
   212
			conf->data);
sl@0
   213
	lh_free(conf->data);
sl@0
   214
	}
sl@0
   215
sl@0
   216
static void value_free_hash(CONF_VALUE *a, LHASH *conf)
sl@0
   217
	{
sl@0
   218
	if (a->name != NULL)
sl@0
   219
		{
sl@0
   220
		a=(CONF_VALUE *)lh_delete(conf,a);
sl@0
   221
		}
sl@0
   222
	}
sl@0
   223
sl@0
   224
static void value_free_stack(CONF_VALUE *a, LHASH *conf)
sl@0
   225
	{
sl@0
   226
	CONF_VALUE *vv;
sl@0
   227
	STACK *sk;
sl@0
   228
	int i;
sl@0
   229
sl@0
   230
	if (a->name != NULL) return;
sl@0
   231
sl@0
   232
	sk=(STACK *)a->value;
sl@0
   233
	for (i=sk_num(sk)-1; i>=0; i--)
sl@0
   234
		{
sl@0
   235
		vv=(CONF_VALUE *)sk_value(sk,i);
sl@0
   236
		OPENSSL_free(vv->value);
sl@0
   237
		OPENSSL_free(vv->name);
sl@0
   238
		OPENSSL_free(vv);
sl@0
   239
		}
sl@0
   240
	if (sk != NULL) sk_free(sk);
sl@0
   241
	OPENSSL_free(a->section);
sl@0
   242
	OPENSSL_free(a);
sl@0
   243
	}
sl@0
   244
sl@0
   245
/* static unsigned long hash(CONF_VALUE *v) */
sl@0
   246
static unsigned long hash(const void *v_void)
sl@0
   247
	{
sl@0
   248
	CONF_VALUE *v = (CONF_VALUE *)v_void;
sl@0
   249
	return((lh_strhash(v->section)<<2)^lh_strhash(v->name));
sl@0
   250
	}
sl@0
   251
sl@0
   252
/* static int cmp_conf(CONF_VALUE *a, CONF_VALUE *b) */
sl@0
   253
static int cmp_conf(const void *a_void,const  void *b_void)
sl@0
   254
	{
sl@0
   255
	int i;
sl@0
   256
	CONF_VALUE *a = (CONF_VALUE *)a_void;
sl@0
   257
	CONF_VALUE *b = (CONF_VALUE *)b_void;
sl@0
   258
sl@0
   259
	if (a->section != b->section)
sl@0
   260
		{
sl@0
   261
		i=strcmp(a->section,b->section);
sl@0
   262
		if (i) return(i);
sl@0
   263
		}
sl@0
   264
sl@0
   265
	if ((a->name != NULL) && (b->name != NULL))
sl@0
   266
		{
sl@0
   267
		i=strcmp(a->name,b->name);
sl@0
   268
		return(i);
sl@0
   269
		}
sl@0
   270
	else if (a->name == b->name)
sl@0
   271
		return(0);
sl@0
   272
	else
sl@0
   273
		return((a->name == NULL)?-1:1);
sl@0
   274
	}
sl@0
   275
sl@0
   276
/* Up until OpenSSL 0.9.5a, this was new_section */
sl@0
   277
EXPORT_C CONF_VALUE *_CONF_new_section(CONF *conf, const char *section)
sl@0
   278
	{
sl@0
   279
	STACK *sk=NULL;
sl@0
   280
	int ok=0,i;
sl@0
   281
	CONF_VALUE *v=NULL,*vv;
sl@0
   282
sl@0
   283
	if ((sk=sk_new_null()) == NULL)
sl@0
   284
		goto err;
sl@0
   285
	if ((v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))) == NULL)
sl@0
   286
		goto err;
sl@0
   287
	i=strlen(section)+1;
sl@0
   288
	if ((v->section=(char *)OPENSSL_malloc(i)) == NULL)
sl@0
   289
		goto err;
sl@0
   290
sl@0
   291
	memcpy(v->section,section,i);
sl@0
   292
	v->name=NULL;
sl@0
   293
	v->value=(char *)sk;
sl@0
   294
	
sl@0
   295
	vv=(CONF_VALUE *)lh_insert(conf->data,v);
sl@0
   296
	assert(vv == NULL);
sl@0
   297
	ok=1;
sl@0
   298
err:
sl@0
   299
	if (!ok)
sl@0
   300
		{
sl@0
   301
		if (sk != NULL) sk_free(sk);
sl@0
   302
		if (v != NULL) OPENSSL_free(v);
sl@0
   303
		v=NULL;
sl@0
   304
		}
sl@0
   305
	return(v);
sl@0
   306
	}
sl@0
   307
sl@0
   308
IMPLEMENT_STACK_OF(CONF_VALUE)