| 1 |
/* conf_lib.c */ |
| 2 |
/* |
| 3 |
* Written by Richard Levitte ([email protected]) for the OpenSSL project |
| 4 |
* 2000. |
| 5 |
*/ |
| 6 |
/* ==================================================================== |
| 7 |
* Copyright (c) 2000 The OpenSSL Project. All rights reserved. |
| 8 |
* |
| 9 |
* Redistribution and use in source and binary forms, with or without |
| 10 |
* modification, are permitted provided that the following conditions |
| 11 |
* are met: |
| 12 |
* |
| 13 |
* 1. Redistributions of source code must retain the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer. |
| 15 |
* |
| 16 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 17 |
* notice, this list of conditions and the following disclaimer in |
| 18 |
* the documentation and/or other materials provided with the |
| 19 |
* distribution. |
| 20 |
* |
| 21 |
* 3. All advertising materials mentioning features or use of this |
| 22 |
* software must display the following acknowledgment: |
| 23 |
* "This product includes software developed by the OpenSSL Project |
| 24 |
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 25 |
* |
| 26 |
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 27 |
* endorse or promote products derived from this software without |
| 28 |
* prior written permission. For written permission, please contact |
| 29 |
* [email protected]. |
| 30 |
* |
| 31 |
* 5. Products derived from this software may not be called "OpenSSL" |
| 32 |
* nor may "OpenSSL" appear in their names without prior written |
| 33 |
* permission of the OpenSSL Project. |
| 34 |
* |
| 35 |
* 6. Redistributions of any form whatsoever must retain the following |
| 36 |
* acknowledgment: |
| 37 |
* "This product includes software developed by the OpenSSL Project |
| 38 |
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 39 |
* |
| 40 |
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 41 |
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 43 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 44 |
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 45 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 46 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 47 |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 49 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 50 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 51 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 52 |
* ==================================================================== |
| 53 |
* |
| 54 |
* This product includes cryptographic software written by Eric Young |
| 55 |
* ([email protected]). This product includes software written by Tim |
| 56 |
* Hudson ([email protected]). |
| 57 |
* |
| 58 |
*/ |
| 59 |
|
| 60 |
#include <stdio.h> |
| 61 |
#include <openssl/crypto.h> |
| 62 |
#include <openssl/err.h> |
| 63 |
#include <openssl/conf.h> |
| 64 |
#include <openssl/conf_api.h> |
| 65 |
#include <openssl/lhash.h> |
| 66 |
|
| 67 |
const char CONF_version[] = "CONF" OPENSSL_VERSION_PTEXT; |
| 68 |
|
| 69 |
static CONF_METHOD *default_CONF_method = NULL; |
| 70 |
|
| 71 |
/* Init a 'CONF' structure from an old LHASH */ |
| 72 |
|
| 73 |
void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash) |
| 74 |
{ |
| 75 |
if (default_CONF_method == NULL) |
| 76 |
default_CONF_method = NCONF_default(); |
| 77 |
|
| 78 |
default_CONF_method->init(conf); |
| 79 |
conf->data = hash; |
| 80 |
} |
| 81 |
|
| 82 |
/* |
| 83 |
* The following section contains the "CONF classic" functions, rewritten in |
| 84 |
* terms of the new CONF interface. |
| 85 |
*/ |
| 86 |
|
| 87 |
int CONF_set_default_method(CONF_METHOD *meth) |
| 88 |
{ |
| 89 |
default_CONF_method = meth; |
| 90 |
return 1; |
| 91 |
} |
| 92 |
|
| 93 |
#ifndef OPENSSL_NO_STDIO |
| 94 |
LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file, |
| 95 |
long *eline) |
| 96 |
{ |
| 97 |
LHASH_OF(CONF_VALUE) *ltmp; |
| 98 |
BIO *in = NULL; |
| 99 |
|
| 100 |
#ifdef OPENSSL_SYS_VMS |
| 101 |
in = BIO_new_file(file, "r"); |
| 102 |
#else |
| 103 |
in = BIO_new_file(file, "rb"); |
| 104 |
#endif |
| 105 |
if (in == NULL) { |
| 106 |
CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB); |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
|
| 110 |
ltmp = CONF_load_bio(conf, in, eline); |
| 111 |
BIO_free(in); |
| 112 |
|
| 113 |
return ltmp; |
| 114 |
} |
| 115 |
#endif |
| 116 |
|
| 117 |
#ifndef OPENSSL_NO_FP_API |
| 118 |
LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp, |
| 119 |
long *eline) |
| 120 |
{ |
| 121 |
BIO *btmp; |
| 122 |
LHASH_OF(CONF_VALUE) *ltmp; |
| 123 |
if (!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) { |
| 124 |
CONFerr(CONF_F_CONF_LOAD_FP, ERR_R_BUF_LIB); |
| 125 |
return NULL; |
| 126 |
} |
| 127 |
ltmp = CONF_load_bio(conf, btmp, eline); |
| 128 |
BIO_free(btmp); |
| 129 |
return ltmp; |
| 130 |
} |
| 131 |
#endif |
| 132 |
|
| 133 |
LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp, |
| 134 |
long *eline) |
| 135 |
{ |
| 136 |
CONF ctmp; |
| 137 |
int ret; |
| 138 |
|
| 139 |
CONF_set_nconf(&ctmp, conf); |
| 140 |
|
| 141 |
ret = NCONF_load_bio(&ctmp, bp, eline); |
| 142 |
if (ret) |
| 143 |
return ctmp.data; |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
|
| 147 |
STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf, |
| 148 |
const char *section) |
| 149 |
{ |
| 150 |
if (conf == NULL) { |
| 151 |
return NULL; |
| 152 |
} else { |
| 153 |
CONF ctmp; |
| 154 |
CONF_set_nconf(&ctmp, conf); |
| 155 |
return NCONF_get_section(&ctmp, section); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group, |
| 160 |
const char *name) |
| 161 |
{ |
| 162 |
if (conf == NULL) { |
| 163 |
return NCONF_get_string(NULL, group, name); |
| 164 |
} else { |
| 165 |
CONF ctmp; |
| 166 |
CONF_set_nconf(&ctmp, conf); |
| 167 |
return NCONF_get_string(&ctmp, group, name); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group, |
| 172 |
const char *name) |
| 173 |
{ |
| 174 |
int status; |
| 175 |
long result = 0; |
| 176 |
|
| 177 |
if (conf == NULL) { |
| 178 |
status = NCONF_get_number_e(NULL, group, name, &result); |
| 179 |
} else { |
| 180 |
CONF ctmp; |
| 181 |
CONF_set_nconf(&ctmp, conf); |
| 182 |
status = NCONF_get_number_e(&ctmp, group, name, &result); |
| 183 |
} |
| 184 |
|
| 185 |
if (status == 0) { |
| 186 |
/* This function does not believe in errors... */ |
| 187 |
ERR_clear_error(); |
| 188 |
} |
| 189 |
return result; |
| 190 |
} |
| 191 |
|
| 192 |
void CONF_free(LHASH_OF(CONF_VALUE) *conf) |
| 193 |
{ |
| 194 |
CONF ctmp; |
| 195 |
CONF_set_nconf(&ctmp, conf); |
| 196 |
NCONF_free_data(&ctmp); |
| 197 |
} |
| 198 |
|
| 199 |
#ifndef OPENSSL_NO_FP_API |
| 200 |
int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out) |
| 201 |
{ |
| 202 |
BIO *btmp; |
| 203 |
int ret; |
| 204 |
|
| 205 |
if (!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) { |
| 206 |
CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB); |
| 207 |
return 0; |
| 208 |
} |
| 209 |
ret = CONF_dump_bio(conf, btmp); |
| 210 |
BIO_free(btmp); |
| 211 |
return ret; |
| 212 |
} |
| 213 |
#endif |
| 214 |
|
| 215 |
int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out) |
| 216 |
{ |
| 217 |
CONF ctmp; |
| 218 |
CONF_set_nconf(&ctmp, conf); |
| 219 |
return NCONF_dump_bio(&ctmp, out); |
| 220 |
} |
| 221 |
|
| 222 |
/* |
| 223 |
* The following section contains the "New CONF" functions. They are |
| 224 |
* completely centralised around a new CONF structure that may contain |
| 225 |
* basically anything, but at least a method pointer and a table of data. |
| 226 |
* These functions are also written in terms of the bridge functions used by |
| 227 |
* the "CONF classic" functions, for consistency. |
| 228 |
*/ |
| 229 |
|
| 230 |
CONF *NCONF_new(CONF_METHOD *meth) |
| 231 |
{ |
| 232 |
CONF *ret; |
| 233 |
|
| 234 |
if (meth == NULL) |
| 235 |
meth = NCONF_default(); |
| 236 |
|
| 237 |
ret = meth->create(meth); |
| 238 |
if (ret == NULL) { |
| 239 |
CONFerr(CONF_F_NCONF_NEW, ERR_R_MALLOC_FAILURE); |
| 240 |
return (NULL); |
| 241 |
} |
| 242 |
|
| 243 |
return ret; |
| 244 |
} |
| 245 |
|
| 246 |
void NCONF_free(CONF *conf) |
| 247 |
{ |
| 248 |
if (conf == NULL) |
| 249 |
return; |
| 250 |
conf->meth->destroy(conf); |
| 251 |
} |
| 252 |
|
| 253 |
void NCONF_free_data(CONF *conf) |
| 254 |
{ |
| 255 |
if (conf == NULL) |
| 256 |
return; |
| 257 |
conf->meth->destroy_data(conf); |
| 258 |
} |
| 259 |
|
| 260 |
#ifndef OPENSSL_NO_STDIO |
| 261 |
int NCONF_load(CONF *conf, const char *file, long *eline) |
| 262 |
{ |
| 263 |
if (conf == NULL) { |
| 264 |
CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF); |
| 265 |
return 0; |
| 266 |
} |
| 267 |
|
| 268 |
return conf->meth->load(conf, file, eline); |
| 269 |
} |
| 270 |
#endif |
| 271 |
|
| 272 |
#ifndef OPENSSL_NO_FP_API |
| 273 |
int NCONF_load_fp(CONF *conf, FILE *fp, long *eline) |
| 274 |
{ |
| 275 |
BIO *btmp; |
| 276 |
int ret; |
| 277 |
if (!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) { |
| 278 |
CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB); |
| 279 |
return 0; |
| 280 |
} |
| 281 |
ret = NCONF_load_bio(conf, btmp, eline); |
| 282 |
BIO_free(btmp); |
| 283 |
return ret; |
| 284 |
} |
| 285 |
#endif |
| 286 |
|
| 287 |
int NCONF_load_bio(CONF *conf, BIO *bp, long *eline) |
| 288 |
{ |
| 289 |
if (conf == NULL) { |
| 290 |
CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF); |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
return conf->meth->load_bio(conf, bp, eline); |
| 295 |
} |
| 296 |
|
| 297 |
STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section) |
| 298 |
{ |
| 299 |
if (conf == NULL) { |
| 300 |
CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF); |
| 301 |
return NULL; |
| 302 |
} |
| 303 |
|
| 304 |
if (section == NULL) { |
| 305 |
CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION); |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
|
| 309 |
return _CONF_get_section_values(conf, section); |
| 310 |
} |
| 311 |
|
| 312 |
char *NCONF_get_string(const CONF *conf, const char *group, const char *name) |
| 313 |
{ |
| 314 |
char *s = _CONF_get_string(conf, group, name); |
| 315 |
|
| 316 |
/* |
| 317 |
* Since we may get a value from an environment variable even if conf is |
| 318 |
* NULL, let's check the value first |
| 319 |
*/ |
| 320 |
if (s) |
| 321 |
return s; |
| 322 |
|
| 323 |
if (conf == NULL) { |
| 324 |
CONFerr(CONF_F_NCONF_GET_STRING, |
| 325 |
CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE); |
| 326 |
return NULL; |
| 327 |
} |
| 328 |
CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE); |
| 329 |
ERR_add_error_data(4, "group=", group, " name=", name); |
| 330 |
return NULL; |
| 331 |
} |
| 332 |
|
| 333 |
int NCONF_get_number_e(const CONF *conf, const char *group, const char *name, |
| 334 |
long *result) |
| 335 |
{ |
| 336 |
char *str; |
| 337 |
|
| 338 |
if (result == NULL) { |
| 339 |
CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER); |
| 340 |
return 0; |
| 341 |
} |
| 342 |
|
| 343 |
if (conf == NULL) |
| 344 |
return 0; |
| 345 |
|
| 346 |
str = NCONF_get_string(conf, group, name); |
| 347 |
|
| 348 |
if (str == NULL) |
| 349 |
return 0; |
| 350 |
|
| 351 |
for (*result = 0; conf->meth->is_number(conf, *str);) { |
| 352 |
*result = (*result) * 10 + conf->meth->to_int(conf, *str); |
| 353 |
str++; |
| 354 |
} |
| 355 |
|
| 356 |
return 1; |
| 357 |
} |
| 358 |
|
| 359 |
#ifndef OPENSSL_NO_FP_API |
| 360 |
int NCONF_dump_fp(const CONF *conf, FILE *out) |
| 361 |
{ |
| 362 |
BIO *btmp; |
| 363 |
int ret; |
| 364 |
if (!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) { |
| 365 |
CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
ret = NCONF_dump_bio(conf, btmp); |
| 369 |
BIO_free(btmp); |
| 370 |
return ret; |
| 371 |
} |
| 372 |
#endif |
| 373 |
|
| 374 |
int NCONF_dump_bio(const CONF *conf, BIO *out) |
| 375 |
{ |
| 376 |
if (conf == NULL) { |
| 377 |
CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF); |
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
return conf->meth->dump(conf, out); |
| 382 |
} |
| 383 |
|
| 384 |
/* This function should be avoided */ |
| 385 |
#if 0 |
| 386 |
long NCONF_get_number(CONF *conf, char *group, char *name) |
| 387 |
{ |
| 388 |
int status; |
| 389 |
long ret = 0; |
| 390 |
|
| 391 |
status = NCONF_get_number_e(conf, group, name, &ret); |
| 392 |
if (status == 0) { |
| 393 |
/* This function does not believe in errors... */ |
| 394 |
ERR_get_error(); |
| 395 |
} |
| 396 |
return ret; |
| 397 |
} |
| 398 |
#endif |