sl@0: /* conf_api.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: /* Part of the code in here was originally in conf.c, which is now removed */ sl@0: sl@0: #ifndef CONF_DEBUG sl@0: # undef NDEBUG /* avoid conflicting definitions */ sl@0: # define NDEBUG sl@0: #endif sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include "e_os.h" sl@0: sl@0: static void value_free_hash(CONF_VALUE *a, LHASH *conf); sl@0: static void value_free_stack(CONF_VALUE *a,LHASH *conf); sl@0: static IMPLEMENT_LHASH_DOALL_ARG_FN(value_free_hash, CONF_VALUE *, LHASH *) sl@0: static IMPLEMENT_LHASH_DOALL_ARG_FN(value_free_stack, CONF_VALUE *, LHASH *) sl@0: /* We don't use function pointer casting or wrapper functions - but cast each sl@0: * callback parameter inside the callback functions. */ sl@0: /* static unsigned long hash(CONF_VALUE *v); */ sl@0: static unsigned long hash(const void *v_void); sl@0: /* static int cmp_conf(CONF_VALUE *a,CONF_VALUE *b); */ sl@0: static int cmp_conf(const void *a_void,const void *b_void); sl@0: sl@0: /* Up until OpenSSL 0.9.5a, this was get_section */ sl@0: EXPORT_C CONF_VALUE *_CONF_get_section(const CONF *conf, const char *section) sl@0: { sl@0: CONF_VALUE *v,vv; sl@0: sl@0: if ((conf == NULL) || (section == NULL)) return(NULL); sl@0: vv.name=NULL; sl@0: vv.section=(char *)section; sl@0: v=(CONF_VALUE *)lh_retrieve(conf->data,&vv); sl@0: return(v); sl@0: } sl@0: sl@0: /* Up until OpenSSL 0.9.5a, this was CONF_get_section */ sl@0: EXPORT_C STACK_OF(CONF_VALUE) *_CONF_get_section_values(const CONF *conf, sl@0: const char *section) sl@0: { sl@0: CONF_VALUE *v; sl@0: sl@0: v=_CONF_get_section(conf,section); sl@0: if (v != NULL) sl@0: return((STACK_OF(CONF_VALUE) *)v->value); sl@0: else sl@0: return(NULL); sl@0: } sl@0: sl@0: EXPORT_C int _CONF_add_string(CONF *conf, CONF_VALUE *section, CONF_VALUE *value) sl@0: { sl@0: CONF_VALUE *v = NULL; sl@0: STACK_OF(CONF_VALUE) *ts; sl@0: sl@0: ts = (STACK_OF(CONF_VALUE) *)section->value; sl@0: sl@0: value->section=section->section; sl@0: if (!sk_CONF_VALUE_push(ts,value)) sl@0: { sl@0: return 0; sl@0: } sl@0: sl@0: v = (CONF_VALUE *)lh_insert(conf->data, value); sl@0: if (v != NULL) sl@0: { sl@0: (void)sk_CONF_VALUE_delete_ptr(ts,v); sl@0: OPENSSL_free(v->name); sl@0: OPENSSL_free(v->value); sl@0: OPENSSL_free(v); sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C char *_CONF_get_string(const CONF *conf, const char *section, const char *name) sl@0: { sl@0: CONF_VALUE *v,vv; sl@0: char *p; sl@0: sl@0: if (name == NULL) return(NULL); sl@0: if (conf != NULL) sl@0: { sl@0: if (section != NULL) sl@0: { sl@0: vv.name=(char *)name; sl@0: vv.section=(char *)section; sl@0: v=(CONF_VALUE *)lh_retrieve(conf->data,&vv); sl@0: if (v != NULL) return(v->value); sl@0: if (strcmp(section,"ENV") == 0) sl@0: { sl@0: p=Getenv(name); sl@0: if (p != NULL) return(p); sl@0: } sl@0: } sl@0: vv.section="default"; sl@0: vv.name=(char *)name; sl@0: v=(CONF_VALUE *)lh_retrieve(conf->data,&vv); sl@0: if (v != NULL) sl@0: return(v->value); sl@0: else sl@0: return(NULL); sl@0: } sl@0: else sl@0: return(Getenv(name)); sl@0: } sl@0: sl@0: #if 0 /* There's no way to provide error checking with this function, so sl@0: force implementors of the higher levels to get a string and read sl@0: the number themselves. */ sl@0: EXPORT_C long _CONF_get_number(CONF *conf, char *section, char *name) sl@0: { sl@0: char *str; sl@0: long ret=0; sl@0: sl@0: str=_CONF_get_string(conf,section,name); sl@0: if (str == NULL) return(0); sl@0: for (;;) sl@0: { sl@0: if (conf->meth->is_number(conf, *str)) sl@0: ret=ret*10+conf->meth->to_int(conf, *str); sl@0: else sl@0: return(ret); sl@0: str++; sl@0: } sl@0: } sl@0: #endif sl@0: sl@0: EXPORT_C int _CONF_new_data(CONF *conf) sl@0: { sl@0: if (conf == NULL) sl@0: { sl@0: return 0; sl@0: } sl@0: if (conf->data == NULL) sl@0: if ((conf->data = lh_new(hash, cmp_conf)) == NULL) sl@0: { sl@0: return 0; sl@0: } sl@0: return 1; sl@0: } sl@0: sl@0: EXPORT_C void _CONF_free_data(CONF *conf) sl@0: { sl@0: if (conf == NULL || conf->data == NULL) return; sl@0: sl@0: conf->data->down_load=0; /* evil thing to make sure the 'OPENSSL_free()' sl@0: * works as expected */ sl@0: lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(value_free_hash), sl@0: conf->data); sl@0: sl@0: /* We now have only 'section' entries in the hash table. sl@0: * Due to problems with */ sl@0: sl@0: lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(value_free_stack), sl@0: conf->data); sl@0: lh_free(conf->data); sl@0: } sl@0: sl@0: static void value_free_hash(CONF_VALUE *a, LHASH *conf) sl@0: { sl@0: if (a->name != NULL) sl@0: { sl@0: a=(CONF_VALUE *)lh_delete(conf,a); sl@0: } sl@0: } sl@0: sl@0: static void value_free_stack(CONF_VALUE *a, LHASH *conf) sl@0: { sl@0: CONF_VALUE *vv; sl@0: STACK *sk; sl@0: int i; sl@0: sl@0: if (a->name != NULL) return; sl@0: sl@0: sk=(STACK *)a->value; sl@0: for (i=sk_num(sk)-1; i>=0; i--) sl@0: { sl@0: vv=(CONF_VALUE *)sk_value(sk,i); sl@0: OPENSSL_free(vv->value); sl@0: OPENSSL_free(vv->name); sl@0: OPENSSL_free(vv); sl@0: } sl@0: if (sk != NULL) sk_free(sk); sl@0: OPENSSL_free(a->section); sl@0: OPENSSL_free(a); sl@0: } sl@0: sl@0: /* static unsigned long hash(CONF_VALUE *v) */ sl@0: static unsigned long hash(const void *v_void) sl@0: { sl@0: CONF_VALUE *v = (CONF_VALUE *)v_void; sl@0: return((lh_strhash(v->section)<<2)^lh_strhash(v->name)); sl@0: } sl@0: sl@0: /* static int cmp_conf(CONF_VALUE *a, CONF_VALUE *b) */ sl@0: static int cmp_conf(const void *a_void,const void *b_void) sl@0: { sl@0: int i; sl@0: CONF_VALUE *a = (CONF_VALUE *)a_void; sl@0: CONF_VALUE *b = (CONF_VALUE *)b_void; sl@0: sl@0: if (a->section != b->section) sl@0: { sl@0: i=strcmp(a->section,b->section); sl@0: if (i) return(i); sl@0: } sl@0: sl@0: if ((a->name != NULL) && (b->name != NULL)) sl@0: { sl@0: i=strcmp(a->name,b->name); sl@0: return(i); sl@0: } sl@0: else if (a->name == b->name) sl@0: return(0); sl@0: else sl@0: return((a->name == NULL)?-1:1); sl@0: } sl@0: sl@0: /* Up until OpenSSL 0.9.5a, this was new_section */ sl@0: EXPORT_C CONF_VALUE *_CONF_new_section(CONF *conf, const char *section) sl@0: { sl@0: STACK *sk=NULL; sl@0: int ok=0,i; sl@0: CONF_VALUE *v=NULL,*vv; sl@0: sl@0: if ((sk=sk_new_null()) == NULL) sl@0: goto err; sl@0: if ((v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))) == NULL) sl@0: goto err; sl@0: i=strlen(section)+1; sl@0: if ((v->section=(char *)OPENSSL_malloc(i)) == NULL) sl@0: goto err; sl@0: sl@0: memcpy(v->section,section,i); sl@0: v->name=NULL; sl@0: v->value=(char *)sk; sl@0: sl@0: vv=(CONF_VALUE *)lh_insert(conf->data,v); sl@0: assert(vv == NULL); sl@0: ok=1; sl@0: err: sl@0: if (!ok) sl@0: { sl@0: if (sk != NULL) sk_free(sk); sl@0: if (v != NULL) OPENSSL_free(v); sl@0: v=NULL; sl@0: } sl@0: return(v); sl@0: } sl@0: sl@0: IMPLEMENT_STACK_OF(CONF_VALUE)