1*0Sstevel@tonic-gate /* crypto/conf/conf.c */ 2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3*0Sstevel@tonic-gate * All rights reserved. 4*0Sstevel@tonic-gate * 5*0Sstevel@tonic-gate * This package is an SSL implementation written 6*0Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com). 7*0Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as 10*0Sstevel@tonic-gate * the following conditions are aheared to. The following conditions 11*0Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA, 12*0Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13*0Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms 14*0Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15*0Sstevel@tonic-gate * 16*0Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in 17*0Sstevel@tonic-gate * the code are not to be removed. 18*0Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution 19*0Sstevel@tonic-gate * as the author of the parts of the library used. 20*0Sstevel@tonic-gate * This can be in the form of a textual message at program startup or 21*0Sstevel@tonic-gate * in documentation (online or textual) provided with the package. 22*0Sstevel@tonic-gate * 23*0Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without 24*0Sstevel@tonic-gate * modification, are permitted provided that the following conditions 25*0Sstevel@tonic-gate * are met: 26*0Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright 27*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer. 28*0Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright 29*0Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the 30*0Sstevel@tonic-gate * documentation and/or other materials provided with the distribution. 31*0Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software 32*0Sstevel@tonic-gate * must display the following acknowledgement: 33*0Sstevel@tonic-gate * "This product includes cryptographic software written by 34*0Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)" 35*0Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library 36*0Sstevel@tonic-gate * being used are not cryptographic related :-). 37*0Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from 38*0Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement: 39*0Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40*0Sstevel@tonic-gate * 41*0Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42*0Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43*0Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44*0Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45*0Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46*0Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47*0Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48*0Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49*0Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50*0Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51*0Sstevel@tonic-gate * SUCH DAMAGE. 52*0Sstevel@tonic-gate * 53*0Sstevel@tonic-gate * The licence and distribution terms for any publically available version or 54*0Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be 55*0Sstevel@tonic-gate * copied and put under another distribution licence 56*0Sstevel@tonic-gate * [including the GNU Public Licence.] 57*0Sstevel@tonic-gate */ 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate /* Part of the code in here was originally in conf.c, which is now removed */ 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate #include <stdio.h> 62*0Sstevel@tonic-gate #include <string.h> 63*0Sstevel@tonic-gate #include <openssl/stack.h> 64*0Sstevel@tonic-gate #include <openssl/lhash.h> 65*0Sstevel@tonic-gate #include <openssl/conf.h> 66*0Sstevel@tonic-gate #include <openssl/conf_api.h> 67*0Sstevel@tonic-gate #include "conf_def.h" 68*0Sstevel@tonic-gate #include <openssl/buffer.h> 69*0Sstevel@tonic-gate #include <openssl/err.h> 70*0Sstevel@tonic-gate #include "cryptlib.h" 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate static char *eat_ws(CONF *conf, char *p); 73*0Sstevel@tonic-gate static char *eat_alpha_numeric(CONF *conf, char *p); 74*0Sstevel@tonic-gate static void clear_comments(CONF *conf, char *p); 75*0Sstevel@tonic-gate static int str_copy(CONF *conf,char *section,char **to, char *from); 76*0Sstevel@tonic-gate static char *scan_quote(CONF *conf, char *p); 77*0Sstevel@tonic-gate static char *scan_dquote(CONF *conf, char *p); 78*0Sstevel@tonic-gate #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2))) 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate static CONF *def_create(CONF_METHOD *meth); 81*0Sstevel@tonic-gate static int def_init_default(CONF *conf); 82*0Sstevel@tonic-gate static int def_init_WIN32(CONF *conf); 83*0Sstevel@tonic-gate static int def_destroy(CONF *conf); 84*0Sstevel@tonic-gate static int def_destroy_data(CONF *conf); 85*0Sstevel@tonic-gate static int def_load(CONF *conf, const char *name, long *eline); 86*0Sstevel@tonic-gate static int def_load_bio(CONF *conf, BIO *bp, long *eline); 87*0Sstevel@tonic-gate static int def_dump(const CONF *conf, BIO *bp); 88*0Sstevel@tonic-gate static int def_is_number(const CONF *conf, char c); 89*0Sstevel@tonic-gate static int def_to_int(const CONF *conf, char c); 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate const char *CONF_def_version="CONF_def" OPENSSL_VERSION_PTEXT; 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate static CONF_METHOD default_method = { 94*0Sstevel@tonic-gate "OpenSSL default", 95*0Sstevel@tonic-gate def_create, 96*0Sstevel@tonic-gate def_init_default, 97*0Sstevel@tonic-gate def_destroy, 98*0Sstevel@tonic-gate def_destroy_data, 99*0Sstevel@tonic-gate def_load_bio, 100*0Sstevel@tonic-gate def_dump, 101*0Sstevel@tonic-gate def_is_number, 102*0Sstevel@tonic-gate def_to_int, 103*0Sstevel@tonic-gate def_load 104*0Sstevel@tonic-gate }; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate static CONF_METHOD WIN32_method = { 107*0Sstevel@tonic-gate "WIN32", 108*0Sstevel@tonic-gate def_create, 109*0Sstevel@tonic-gate def_init_WIN32, 110*0Sstevel@tonic-gate def_destroy, 111*0Sstevel@tonic-gate def_destroy_data, 112*0Sstevel@tonic-gate def_load_bio, 113*0Sstevel@tonic-gate def_dump, 114*0Sstevel@tonic-gate def_is_number, 115*0Sstevel@tonic-gate def_to_int, 116*0Sstevel@tonic-gate def_load 117*0Sstevel@tonic-gate }; 118*0Sstevel@tonic-gate 119*0Sstevel@tonic-gate CONF_METHOD *NCONF_default() 120*0Sstevel@tonic-gate { 121*0Sstevel@tonic-gate return &default_method; 122*0Sstevel@tonic-gate } 123*0Sstevel@tonic-gate CONF_METHOD *NCONF_WIN32() 124*0Sstevel@tonic-gate { 125*0Sstevel@tonic-gate return &WIN32_method; 126*0Sstevel@tonic-gate } 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate static CONF *def_create(CONF_METHOD *meth) 129*0Sstevel@tonic-gate { 130*0Sstevel@tonic-gate CONF *ret; 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gate ret = (CONF *)OPENSSL_malloc(sizeof(CONF) + sizeof(unsigned short *)); 133*0Sstevel@tonic-gate if (ret) 134*0Sstevel@tonic-gate if (meth->init(ret) == 0) 135*0Sstevel@tonic-gate { 136*0Sstevel@tonic-gate OPENSSL_free(ret); 137*0Sstevel@tonic-gate ret = NULL; 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate return ret; 140*0Sstevel@tonic-gate } 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate static int def_init_default(CONF *conf) 143*0Sstevel@tonic-gate { 144*0Sstevel@tonic-gate if (conf == NULL) 145*0Sstevel@tonic-gate return 0; 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gate conf->meth = &default_method; 148*0Sstevel@tonic-gate conf->meth_data = (void *)CONF_type_default; 149*0Sstevel@tonic-gate conf->data = NULL; 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate return 1; 152*0Sstevel@tonic-gate } 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate static int def_init_WIN32(CONF *conf) 155*0Sstevel@tonic-gate { 156*0Sstevel@tonic-gate if (conf == NULL) 157*0Sstevel@tonic-gate return 0; 158*0Sstevel@tonic-gate 159*0Sstevel@tonic-gate conf->meth = &WIN32_method; 160*0Sstevel@tonic-gate conf->meth_data = (void *)CONF_type_win32; 161*0Sstevel@tonic-gate conf->data = NULL; 162*0Sstevel@tonic-gate 163*0Sstevel@tonic-gate return 1; 164*0Sstevel@tonic-gate } 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate static int def_destroy(CONF *conf) 167*0Sstevel@tonic-gate { 168*0Sstevel@tonic-gate if (def_destroy_data(conf)) 169*0Sstevel@tonic-gate { 170*0Sstevel@tonic-gate OPENSSL_free(conf); 171*0Sstevel@tonic-gate return 1; 172*0Sstevel@tonic-gate } 173*0Sstevel@tonic-gate return 0; 174*0Sstevel@tonic-gate } 175*0Sstevel@tonic-gate 176*0Sstevel@tonic-gate static int def_destroy_data(CONF *conf) 177*0Sstevel@tonic-gate { 178*0Sstevel@tonic-gate if (conf == NULL) 179*0Sstevel@tonic-gate return 0; 180*0Sstevel@tonic-gate _CONF_free_data(conf); 181*0Sstevel@tonic-gate return 1; 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate static int def_load(CONF *conf, const char *name, long *line) 185*0Sstevel@tonic-gate { 186*0Sstevel@tonic-gate int ret; 187*0Sstevel@tonic-gate BIO *in=NULL; 188*0Sstevel@tonic-gate 189*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS 190*0Sstevel@tonic-gate in=BIO_new_file(name, "r"); 191*0Sstevel@tonic-gate #else 192*0Sstevel@tonic-gate in=BIO_new_file(name, "rb"); 193*0Sstevel@tonic-gate #endif 194*0Sstevel@tonic-gate if (in == NULL) 195*0Sstevel@tonic-gate { 196*0Sstevel@tonic-gate if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE) 197*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD,CONF_R_NO_SUCH_FILE); 198*0Sstevel@tonic-gate else 199*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB); 200*0Sstevel@tonic-gate return 0; 201*0Sstevel@tonic-gate } 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate ret = def_load_bio(conf, in, line); 204*0Sstevel@tonic-gate BIO_free(in); 205*0Sstevel@tonic-gate 206*0Sstevel@tonic-gate return ret; 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate static int def_load_bio(CONF *conf, BIO *in, long *line) 210*0Sstevel@tonic-gate { 211*0Sstevel@tonic-gate /* The macro BUFSIZE conflicts with a system macro in VxWorks */ 212*0Sstevel@tonic-gate #define CONFBUFSIZE 512 213*0Sstevel@tonic-gate int bufnum=0,i,ii; 214*0Sstevel@tonic-gate BUF_MEM *buff=NULL; 215*0Sstevel@tonic-gate char *s,*p,*end; 216*0Sstevel@tonic-gate int again,n; 217*0Sstevel@tonic-gate long eline=0; 218*0Sstevel@tonic-gate char btmp[DECIMAL_SIZE(eline)+1]; 219*0Sstevel@tonic-gate CONF_VALUE *v=NULL,*tv; 220*0Sstevel@tonic-gate CONF_VALUE *sv=NULL; 221*0Sstevel@tonic-gate char *section=NULL,*buf; 222*0Sstevel@tonic-gate STACK_OF(CONF_VALUE) *section_sk=NULL,*ts; 223*0Sstevel@tonic-gate char *start,*psection,*pname; 224*0Sstevel@tonic-gate void *h = (void *)(conf->data); 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate if ((buff=BUF_MEM_new()) == NULL) 227*0Sstevel@tonic-gate { 228*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB); 229*0Sstevel@tonic-gate goto err; 230*0Sstevel@tonic-gate } 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gate section=(char *)OPENSSL_malloc(10); 233*0Sstevel@tonic-gate if (section == NULL) 234*0Sstevel@tonic-gate { 235*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE); 236*0Sstevel@tonic-gate goto err; 237*0Sstevel@tonic-gate } 238*0Sstevel@tonic-gate BUF_strlcpy(section,"default",10); 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gate if (_CONF_new_data(conf) == 0) 241*0Sstevel@tonic-gate { 242*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE); 243*0Sstevel@tonic-gate goto err; 244*0Sstevel@tonic-gate } 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate sv=_CONF_new_section(conf,section); 247*0Sstevel@tonic-gate if (sv == NULL) 248*0Sstevel@tonic-gate { 249*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO, 250*0Sstevel@tonic-gate CONF_R_UNABLE_TO_CREATE_NEW_SECTION); 251*0Sstevel@tonic-gate goto err; 252*0Sstevel@tonic-gate } 253*0Sstevel@tonic-gate section_sk=(STACK_OF(CONF_VALUE) *)sv->value; 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate bufnum=0; 256*0Sstevel@tonic-gate again=0; 257*0Sstevel@tonic-gate for (;;) 258*0Sstevel@tonic-gate { 259*0Sstevel@tonic-gate if (!BUF_MEM_grow(buff,bufnum+CONFBUFSIZE)) 260*0Sstevel@tonic-gate { 261*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB); 262*0Sstevel@tonic-gate goto err; 263*0Sstevel@tonic-gate } 264*0Sstevel@tonic-gate p= &(buff->data[bufnum]); 265*0Sstevel@tonic-gate *p='\0'; 266*0Sstevel@tonic-gate BIO_gets(in, p, CONFBUFSIZE-1); 267*0Sstevel@tonic-gate p[CONFBUFSIZE-1]='\0'; 268*0Sstevel@tonic-gate ii=i=strlen(p); 269*0Sstevel@tonic-gate if (i == 0 && !again) break; 270*0Sstevel@tonic-gate again=0; 271*0Sstevel@tonic-gate while (i > 0) 272*0Sstevel@tonic-gate { 273*0Sstevel@tonic-gate if ((p[i-1] != '\r') && (p[i-1] != '\n')) 274*0Sstevel@tonic-gate break; 275*0Sstevel@tonic-gate else 276*0Sstevel@tonic-gate i--; 277*0Sstevel@tonic-gate } 278*0Sstevel@tonic-gate /* we removed some trailing stuff so there is a new 279*0Sstevel@tonic-gate * line on the end. */ 280*0Sstevel@tonic-gate if (ii && i == ii) 281*0Sstevel@tonic-gate again=1; /* long line */ 282*0Sstevel@tonic-gate else 283*0Sstevel@tonic-gate { 284*0Sstevel@tonic-gate p[i]='\0'; 285*0Sstevel@tonic-gate eline++; /* another input line */ 286*0Sstevel@tonic-gate } 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate /* we now have a line with trailing \r\n removed */ 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gate /* i is the number of bytes */ 291*0Sstevel@tonic-gate bufnum+=i; 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate v=NULL; 294*0Sstevel@tonic-gate /* check for line continuation */ 295*0Sstevel@tonic-gate if (bufnum >= 1) 296*0Sstevel@tonic-gate { 297*0Sstevel@tonic-gate /* If we have bytes and the last char '\\' and 298*0Sstevel@tonic-gate * second last char is not '\\' */ 299*0Sstevel@tonic-gate p= &(buff->data[bufnum-1]); 300*0Sstevel@tonic-gate if (IS_ESC(conf,p[0]) && 301*0Sstevel@tonic-gate ((bufnum <= 1) || !IS_ESC(conf,p[-1]))) 302*0Sstevel@tonic-gate { 303*0Sstevel@tonic-gate bufnum--; 304*0Sstevel@tonic-gate again=1; 305*0Sstevel@tonic-gate } 306*0Sstevel@tonic-gate } 307*0Sstevel@tonic-gate if (again) continue; 308*0Sstevel@tonic-gate bufnum=0; 309*0Sstevel@tonic-gate buf=buff->data; 310*0Sstevel@tonic-gate 311*0Sstevel@tonic-gate clear_comments(conf, buf); 312*0Sstevel@tonic-gate n=strlen(buf); 313*0Sstevel@tonic-gate s=eat_ws(conf, buf); 314*0Sstevel@tonic-gate if (IS_EOF(conf,*s)) continue; /* blank line */ 315*0Sstevel@tonic-gate if (*s == '[') 316*0Sstevel@tonic-gate { 317*0Sstevel@tonic-gate char *ss; 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate s++; 320*0Sstevel@tonic-gate start=eat_ws(conf, s); 321*0Sstevel@tonic-gate ss=start; 322*0Sstevel@tonic-gate again: 323*0Sstevel@tonic-gate end=eat_alpha_numeric(conf, ss); 324*0Sstevel@tonic-gate p=eat_ws(conf, end); 325*0Sstevel@tonic-gate if (*p != ']') 326*0Sstevel@tonic-gate { 327*0Sstevel@tonic-gate if (*p != '\0') 328*0Sstevel@tonic-gate { 329*0Sstevel@tonic-gate ss=p; 330*0Sstevel@tonic-gate goto again; 331*0Sstevel@tonic-gate } 332*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO, 333*0Sstevel@tonic-gate CONF_R_MISSING_CLOSE_SQUARE_BRACKET); 334*0Sstevel@tonic-gate goto err; 335*0Sstevel@tonic-gate } 336*0Sstevel@tonic-gate *end='\0'; 337*0Sstevel@tonic-gate if (!str_copy(conf,NULL,§ion,start)) goto err; 338*0Sstevel@tonic-gate if ((sv=_CONF_get_section(conf,section)) == NULL) 339*0Sstevel@tonic-gate sv=_CONF_new_section(conf,section); 340*0Sstevel@tonic-gate if (sv == NULL) 341*0Sstevel@tonic-gate { 342*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO, 343*0Sstevel@tonic-gate CONF_R_UNABLE_TO_CREATE_NEW_SECTION); 344*0Sstevel@tonic-gate goto err; 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate section_sk=(STACK_OF(CONF_VALUE) *)sv->value; 347*0Sstevel@tonic-gate continue; 348*0Sstevel@tonic-gate } 349*0Sstevel@tonic-gate else 350*0Sstevel@tonic-gate { 351*0Sstevel@tonic-gate pname=s; 352*0Sstevel@tonic-gate psection=NULL; 353*0Sstevel@tonic-gate end=eat_alpha_numeric(conf, s); 354*0Sstevel@tonic-gate if ((end[0] == ':') && (end[1] == ':')) 355*0Sstevel@tonic-gate { 356*0Sstevel@tonic-gate *end='\0'; 357*0Sstevel@tonic-gate end+=2; 358*0Sstevel@tonic-gate psection=pname; 359*0Sstevel@tonic-gate pname=end; 360*0Sstevel@tonic-gate end=eat_alpha_numeric(conf, end); 361*0Sstevel@tonic-gate } 362*0Sstevel@tonic-gate p=eat_ws(conf, end); 363*0Sstevel@tonic-gate if (*p != '=') 364*0Sstevel@tonic-gate { 365*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO, 366*0Sstevel@tonic-gate CONF_R_MISSING_EQUAL_SIGN); 367*0Sstevel@tonic-gate goto err; 368*0Sstevel@tonic-gate } 369*0Sstevel@tonic-gate *end='\0'; 370*0Sstevel@tonic-gate p++; 371*0Sstevel@tonic-gate start=eat_ws(conf, p); 372*0Sstevel@tonic-gate while (!IS_EOF(conf,*p)) 373*0Sstevel@tonic-gate p++; 374*0Sstevel@tonic-gate p--; 375*0Sstevel@tonic-gate while ((p != start) && (IS_WS(conf,*p))) 376*0Sstevel@tonic-gate p--; 377*0Sstevel@tonic-gate p++; 378*0Sstevel@tonic-gate *p='\0'; 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gate if (!(v=(CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) 381*0Sstevel@tonic-gate { 382*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO, 383*0Sstevel@tonic-gate ERR_R_MALLOC_FAILURE); 384*0Sstevel@tonic-gate goto err; 385*0Sstevel@tonic-gate } 386*0Sstevel@tonic-gate if (psection == NULL) psection=section; 387*0Sstevel@tonic-gate v->name=(char *)OPENSSL_malloc(strlen(pname)+1); 388*0Sstevel@tonic-gate v->value=NULL; 389*0Sstevel@tonic-gate if (v->name == NULL) 390*0Sstevel@tonic-gate { 391*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO, 392*0Sstevel@tonic-gate ERR_R_MALLOC_FAILURE); 393*0Sstevel@tonic-gate goto err; 394*0Sstevel@tonic-gate } 395*0Sstevel@tonic-gate BUF_strlcpy(v->name,pname,strlen(pname)+1); 396*0Sstevel@tonic-gate if (!str_copy(conf,psection,&(v->value),start)) goto err; 397*0Sstevel@tonic-gate 398*0Sstevel@tonic-gate if (strcmp(psection,section) != 0) 399*0Sstevel@tonic-gate { 400*0Sstevel@tonic-gate if ((tv=_CONF_get_section(conf,psection)) 401*0Sstevel@tonic-gate == NULL) 402*0Sstevel@tonic-gate tv=_CONF_new_section(conf,psection); 403*0Sstevel@tonic-gate if (tv == NULL) 404*0Sstevel@tonic-gate { 405*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO, 406*0Sstevel@tonic-gate CONF_R_UNABLE_TO_CREATE_NEW_SECTION); 407*0Sstevel@tonic-gate goto err; 408*0Sstevel@tonic-gate } 409*0Sstevel@tonic-gate ts=(STACK_OF(CONF_VALUE) *)tv->value; 410*0Sstevel@tonic-gate } 411*0Sstevel@tonic-gate else 412*0Sstevel@tonic-gate { 413*0Sstevel@tonic-gate tv=sv; 414*0Sstevel@tonic-gate ts=section_sk; 415*0Sstevel@tonic-gate } 416*0Sstevel@tonic-gate #if 1 417*0Sstevel@tonic-gate if (_CONF_add_string(conf, tv, v) == 0) 418*0Sstevel@tonic-gate { 419*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO, 420*0Sstevel@tonic-gate ERR_R_MALLOC_FAILURE); 421*0Sstevel@tonic-gate goto err; 422*0Sstevel@tonic-gate } 423*0Sstevel@tonic-gate #else 424*0Sstevel@tonic-gate v->section=tv->section; 425*0Sstevel@tonic-gate if (!sk_CONF_VALUE_push(ts,v)) 426*0Sstevel@tonic-gate { 427*0Sstevel@tonic-gate CONFerr(CONF_F_CONF_LOAD_BIO, 428*0Sstevel@tonic-gate ERR_R_MALLOC_FAILURE); 429*0Sstevel@tonic-gate goto err; 430*0Sstevel@tonic-gate } 431*0Sstevel@tonic-gate vv=(CONF_VALUE *)lh_insert(conf->data,v); 432*0Sstevel@tonic-gate if (vv != NULL) 433*0Sstevel@tonic-gate { 434*0Sstevel@tonic-gate sk_CONF_VALUE_delete_ptr(ts,vv); 435*0Sstevel@tonic-gate OPENSSL_free(vv->name); 436*0Sstevel@tonic-gate OPENSSL_free(vv->value); 437*0Sstevel@tonic-gate OPENSSL_free(vv); 438*0Sstevel@tonic-gate } 439*0Sstevel@tonic-gate #endif 440*0Sstevel@tonic-gate v=NULL; 441*0Sstevel@tonic-gate } 442*0Sstevel@tonic-gate } 443*0Sstevel@tonic-gate if (buff != NULL) BUF_MEM_free(buff); 444*0Sstevel@tonic-gate if (section != NULL) OPENSSL_free(section); 445*0Sstevel@tonic-gate return(1); 446*0Sstevel@tonic-gate err: 447*0Sstevel@tonic-gate if (buff != NULL) BUF_MEM_free(buff); 448*0Sstevel@tonic-gate if (section != NULL) OPENSSL_free(section); 449*0Sstevel@tonic-gate if (line != NULL) *line=eline; 450*0Sstevel@tonic-gate BIO_snprintf(btmp,sizeof btmp,"%ld",eline); 451*0Sstevel@tonic-gate ERR_add_error_data(2,"line ",btmp); 452*0Sstevel@tonic-gate if ((h != conf->data) && (conf->data != NULL)) 453*0Sstevel@tonic-gate { 454*0Sstevel@tonic-gate CONF_free(conf->data); 455*0Sstevel@tonic-gate conf->data=NULL; 456*0Sstevel@tonic-gate } 457*0Sstevel@tonic-gate if (v != NULL) 458*0Sstevel@tonic-gate { 459*0Sstevel@tonic-gate if (v->name != NULL) OPENSSL_free(v->name); 460*0Sstevel@tonic-gate if (v->value != NULL) OPENSSL_free(v->value); 461*0Sstevel@tonic-gate if (v != NULL) OPENSSL_free(v); 462*0Sstevel@tonic-gate } 463*0Sstevel@tonic-gate return(0); 464*0Sstevel@tonic-gate } 465*0Sstevel@tonic-gate 466*0Sstevel@tonic-gate static void clear_comments(CONF *conf, char *p) 467*0Sstevel@tonic-gate { 468*0Sstevel@tonic-gate char *to; 469*0Sstevel@tonic-gate 470*0Sstevel@tonic-gate to=p; 471*0Sstevel@tonic-gate for (;;) 472*0Sstevel@tonic-gate { 473*0Sstevel@tonic-gate if (IS_FCOMMENT(conf,*p)) 474*0Sstevel@tonic-gate { 475*0Sstevel@tonic-gate *p='\0'; 476*0Sstevel@tonic-gate return; 477*0Sstevel@tonic-gate } 478*0Sstevel@tonic-gate if (!IS_WS(conf,*p)) 479*0Sstevel@tonic-gate { 480*0Sstevel@tonic-gate break; 481*0Sstevel@tonic-gate } 482*0Sstevel@tonic-gate p++; 483*0Sstevel@tonic-gate } 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate for (;;) 486*0Sstevel@tonic-gate { 487*0Sstevel@tonic-gate if (IS_COMMENT(conf,*p)) 488*0Sstevel@tonic-gate { 489*0Sstevel@tonic-gate *p='\0'; 490*0Sstevel@tonic-gate return; 491*0Sstevel@tonic-gate } 492*0Sstevel@tonic-gate if (IS_DQUOTE(conf,*p)) 493*0Sstevel@tonic-gate { 494*0Sstevel@tonic-gate p=scan_dquote(conf, p); 495*0Sstevel@tonic-gate continue; 496*0Sstevel@tonic-gate } 497*0Sstevel@tonic-gate if (IS_QUOTE(conf,*p)) 498*0Sstevel@tonic-gate { 499*0Sstevel@tonic-gate p=scan_quote(conf, p); 500*0Sstevel@tonic-gate continue; 501*0Sstevel@tonic-gate } 502*0Sstevel@tonic-gate if (IS_ESC(conf,*p)) 503*0Sstevel@tonic-gate { 504*0Sstevel@tonic-gate p=scan_esc(conf,p); 505*0Sstevel@tonic-gate continue; 506*0Sstevel@tonic-gate } 507*0Sstevel@tonic-gate if (IS_EOF(conf,*p)) 508*0Sstevel@tonic-gate return; 509*0Sstevel@tonic-gate else 510*0Sstevel@tonic-gate p++; 511*0Sstevel@tonic-gate } 512*0Sstevel@tonic-gate } 513*0Sstevel@tonic-gate 514*0Sstevel@tonic-gate static int str_copy(CONF *conf, char *section, char **pto, char *from) 515*0Sstevel@tonic-gate { 516*0Sstevel@tonic-gate int q,r,rr=0,to=0,len=0; 517*0Sstevel@tonic-gate char *s,*e,*rp,*p,*rrp,*np,*cp,v; 518*0Sstevel@tonic-gate BUF_MEM *buf; 519*0Sstevel@tonic-gate 520*0Sstevel@tonic-gate if ((buf=BUF_MEM_new()) == NULL) return(0); 521*0Sstevel@tonic-gate 522*0Sstevel@tonic-gate len=strlen(from)+1; 523*0Sstevel@tonic-gate if (!BUF_MEM_grow(buf,len)) goto err; 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate for (;;) 526*0Sstevel@tonic-gate { 527*0Sstevel@tonic-gate if (IS_QUOTE(conf,*from)) 528*0Sstevel@tonic-gate { 529*0Sstevel@tonic-gate q= *from; 530*0Sstevel@tonic-gate from++; 531*0Sstevel@tonic-gate while (!IS_EOF(conf,*from) && (*from != q)) 532*0Sstevel@tonic-gate { 533*0Sstevel@tonic-gate if (IS_ESC(conf,*from)) 534*0Sstevel@tonic-gate { 535*0Sstevel@tonic-gate from++; 536*0Sstevel@tonic-gate if (IS_EOF(conf,*from)) break; 537*0Sstevel@tonic-gate } 538*0Sstevel@tonic-gate buf->data[to++]= *(from++); 539*0Sstevel@tonic-gate } 540*0Sstevel@tonic-gate if (*from == q) from++; 541*0Sstevel@tonic-gate } 542*0Sstevel@tonic-gate else if (IS_DQUOTE(conf,*from)) 543*0Sstevel@tonic-gate { 544*0Sstevel@tonic-gate q= *from; 545*0Sstevel@tonic-gate from++; 546*0Sstevel@tonic-gate while (!IS_EOF(conf,*from)) 547*0Sstevel@tonic-gate { 548*0Sstevel@tonic-gate if (*from == q) 549*0Sstevel@tonic-gate { 550*0Sstevel@tonic-gate if (*(from+1) == q) 551*0Sstevel@tonic-gate { 552*0Sstevel@tonic-gate from++; 553*0Sstevel@tonic-gate } 554*0Sstevel@tonic-gate else 555*0Sstevel@tonic-gate { 556*0Sstevel@tonic-gate break; 557*0Sstevel@tonic-gate } 558*0Sstevel@tonic-gate } 559*0Sstevel@tonic-gate buf->data[to++]= *(from++); 560*0Sstevel@tonic-gate } 561*0Sstevel@tonic-gate if (*from == q) from++; 562*0Sstevel@tonic-gate } 563*0Sstevel@tonic-gate else if (IS_ESC(conf,*from)) 564*0Sstevel@tonic-gate { 565*0Sstevel@tonic-gate from++; 566*0Sstevel@tonic-gate v= *(from++); 567*0Sstevel@tonic-gate if (IS_EOF(conf,v)) break; 568*0Sstevel@tonic-gate else if (v == 'r') v='\r'; 569*0Sstevel@tonic-gate else if (v == 'n') v='\n'; 570*0Sstevel@tonic-gate else if (v == 'b') v='\b'; 571*0Sstevel@tonic-gate else if (v == 't') v='\t'; 572*0Sstevel@tonic-gate buf->data[to++]= v; 573*0Sstevel@tonic-gate } 574*0Sstevel@tonic-gate else if (IS_EOF(conf,*from)) 575*0Sstevel@tonic-gate break; 576*0Sstevel@tonic-gate else if (*from == '$') 577*0Sstevel@tonic-gate { 578*0Sstevel@tonic-gate /* try to expand it */ 579*0Sstevel@tonic-gate rrp=NULL; 580*0Sstevel@tonic-gate s= &(from[1]); 581*0Sstevel@tonic-gate if (*s == '{') 582*0Sstevel@tonic-gate q='}'; 583*0Sstevel@tonic-gate else if (*s == '(') 584*0Sstevel@tonic-gate q=')'; 585*0Sstevel@tonic-gate else q=0; 586*0Sstevel@tonic-gate 587*0Sstevel@tonic-gate if (q) s++; 588*0Sstevel@tonic-gate cp=section; 589*0Sstevel@tonic-gate e=np=s; 590*0Sstevel@tonic-gate while (IS_ALPHA_NUMERIC(conf,*e)) 591*0Sstevel@tonic-gate e++; 592*0Sstevel@tonic-gate if ((e[0] == ':') && (e[1] == ':')) 593*0Sstevel@tonic-gate { 594*0Sstevel@tonic-gate cp=np; 595*0Sstevel@tonic-gate rrp=e; 596*0Sstevel@tonic-gate rr= *e; 597*0Sstevel@tonic-gate *rrp='\0'; 598*0Sstevel@tonic-gate e+=2; 599*0Sstevel@tonic-gate np=e; 600*0Sstevel@tonic-gate while (IS_ALPHA_NUMERIC(conf,*e)) 601*0Sstevel@tonic-gate e++; 602*0Sstevel@tonic-gate } 603*0Sstevel@tonic-gate r= *e; 604*0Sstevel@tonic-gate *e='\0'; 605*0Sstevel@tonic-gate rp=e; 606*0Sstevel@tonic-gate if (q) 607*0Sstevel@tonic-gate { 608*0Sstevel@tonic-gate if (r != q) 609*0Sstevel@tonic-gate { 610*0Sstevel@tonic-gate CONFerr(CONF_F_STR_COPY,CONF_R_NO_CLOSE_BRACE); 611*0Sstevel@tonic-gate goto err; 612*0Sstevel@tonic-gate } 613*0Sstevel@tonic-gate e++; 614*0Sstevel@tonic-gate } 615*0Sstevel@tonic-gate /* So at this point we have 616*0Sstevel@tonic-gate * ns which is the start of the name string which is 617*0Sstevel@tonic-gate * '\0' terminated. 618*0Sstevel@tonic-gate * cs which is the start of the section string which is 619*0Sstevel@tonic-gate * '\0' terminated. 620*0Sstevel@tonic-gate * e is the 'next point after'. 621*0Sstevel@tonic-gate * r and s are the chars replaced by the '\0' 622*0Sstevel@tonic-gate * rp and sp is where 'r' and 's' came from. 623*0Sstevel@tonic-gate */ 624*0Sstevel@tonic-gate p=_CONF_get_string(conf,cp,np); 625*0Sstevel@tonic-gate if (rrp != NULL) *rrp=rr; 626*0Sstevel@tonic-gate *rp=r; 627*0Sstevel@tonic-gate if (p == NULL) 628*0Sstevel@tonic-gate { 629*0Sstevel@tonic-gate CONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE); 630*0Sstevel@tonic-gate goto err; 631*0Sstevel@tonic-gate } 632*0Sstevel@tonic-gate BUF_MEM_grow_clean(buf,(strlen(p)+len-(e-from))); 633*0Sstevel@tonic-gate while (*p) 634*0Sstevel@tonic-gate buf->data[to++]= *(p++); 635*0Sstevel@tonic-gate from=e; 636*0Sstevel@tonic-gate } 637*0Sstevel@tonic-gate else 638*0Sstevel@tonic-gate buf->data[to++]= *(from++); 639*0Sstevel@tonic-gate } 640*0Sstevel@tonic-gate buf->data[to]='\0'; 641*0Sstevel@tonic-gate if (*pto != NULL) OPENSSL_free(*pto); 642*0Sstevel@tonic-gate *pto=buf->data; 643*0Sstevel@tonic-gate OPENSSL_free(buf); 644*0Sstevel@tonic-gate return(1); 645*0Sstevel@tonic-gate err: 646*0Sstevel@tonic-gate if (buf != NULL) BUF_MEM_free(buf); 647*0Sstevel@tonic-gate return(0); 648*0Sstevel@tonic-gate } 649*0Sstevel@tonic-gate 650*0Sstevel@tonic-gate static char *eat_ws(CONF *conf, char *p) 651*0Sstevel@tonic-gate { 652*0Sstevel@tonic-gate while (IS_WS(conf,*p) && (!IS_EOF(conf,*p))) 653*0Sstevel@tonic-gate p++; 654*0Sstevel@tonic-gate return(p); 655*0Sstevel@tonic-gate } 656*0Sstevel@tonic-gate 657*0Sstevel@tonic-gate static char *eat_alpha_numeric(CONF *conf, char *p) 658*0Sstevel@tonic-gate { 659*0Sstevel@tonic-gate for (;;) 660*0Sstevel@tonic-gate { 661*0Sstevel@tonic-gate if (IS_ESC(conf,*p)) 662*0Sstevel@tonic-gate { 663*0Sstevel@tonic-gate p=scan_esc(conf,p); 664*0Sstevel@tonic-gate continue; 665*0Sstevel@tonic-gate } 666*0Sstevel@tonic-gate if (!IS_ALPHA_NUMERIC_PUNCT(conf,*p)) 667*0Sstevel@tonic-gate return(p); 668*0Sstevel@tonic-gate p++; 669*0Sstevel@tonic-gate } 670*0Sstevel@tonic-gate } 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate static char *scan_quote(CONF *conf, char *p) 673*0Sstevel@tonic-gate { 674*0Sstevel@tonic-gate int q= *p; 675*0Sstevel@tonic-gate 676*0Sstevel@tonic-gate p++; 677*0Sstevel@tonic-gate while (!(IS_EOF(conf,*p)) && (*p != q)) 678*0Sstevel@tonic-gate { 679*0Sstevel@tonic-gate if (IS_ESC(conf,*p)) 680*0Sstevel@tonic-gate { 681*0Sstevel@tonic-gate p++; 682*0Sstevel@tonic-gate if (IS_EOF(conf,*p)) return(p); 683*0Sstevel@tonic-gate } 684*0Sstevel@tonic-gate p++; 685*0Sstevel@tonic-gate } 686*0Sstevel@tonic-gate if (*p == q) p++; 687*0Sstevel@tonic-gate return(p); 688*0Sstevel@tonic-gate } 689*0Sstevel@tonic-gate 690*0Sstevel@tonic-gate 691*0Sstevel@tonic-gate static char *scan_dquote(CONF *conf, char *p) 692*0Sstevel@tonic-gate { 693*0Sstevel@tonic-gate int q= *p; 694*0Sstevel@tonic-gate 695*0Sstevel@tonic-gate p++; 696*0Sstevel@tonic-gate while (!(IS_EOF(conf,*p))) 697*0Sstevel@tonic-gate { 698*0Sstevel@tonic-gate if (*p == q) 699*0Sstevel@tonic-gate { 700*0Sstevel@tonic-gate if (*(p+1) == q) 701*0Sstevel@tonic-gate { 702*0Sstevel@tonic-gate p++; 703*0Sstevel@tonic-gate } 704*0Sstevel@tonic-gate else 705*0Sstevel@tonic-gate { 706*0Sstevel@tonic-gate break; 707*0Sstevel@tonic-gate } 708*0Sstevel@tonic-gate } 709*0Sstevel@tonic-gate p++; 710*0Sstevel@tonic-gate } 711*0Sstevel@tonic-gate if (*p == q) p++; 712*0Sstevel@tonic-gate return(p); 713*0Sstevel@tonic-gate } 714*0Sstevel@tonic-gate 715*0Sstevel@tonic-gate static void dump_value(CONF_VALUE *a, BIO *out) 716*0Sstevel@tonic-gate { 717*0Sstevel@tonic-gate if (a->name) 718*0Sstevel@tonic-gate BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value); 719*0Sstevel@tonic-gate else 720*0Sstevel@tonic-gate BIO_printf(out, "[[%s]]\n", a->section); 721*0Sstevel@tonic-gate } 722*0Sstevel@tonic-gate 723*0Sstevel@tonic-gate static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE *, BIO *) 724*0Sstevel@tonic-gate 725*0Sstevel@tonic-gate static int def_dump(const CONF *conf, BIO *out) 726*0Sstevel@tonic-gate { 727*0Sstevel@tonic-gate lh_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value), out); 728*0Sstevel@tonic-gate return 1; 729*0Sstevel@tonic-gate } 730*0Sstevel@tonic-gate 731*0Sstevel@tonic-gate static int def_is_number(const CONF *conf, char c) 732*0Sstevel@tonic-gate { 733*0Sstevel@tonic-gate return IS_NUMBER(conf,c); 734*0Sstevel@tonic-gate } 735*0Sstevel@tonic-gate 736*0Sstevel@tonic-gate static int def_to_int(const CONF *conf, char c) 737*0Sstevel@tonic-gate { 738*0Sstevel@tonic-gate return c - '0'; 739*0Sstevel@tonic-gate } 740*0Sstevel@tonic-gate 741