1 /* crypto/conf/conf.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
62 #include <openssl/stack.h>
63 #include <openssl/lhash.h>
64 #include <openssl/conf.h>
65 #include <openssl/buffer.h>
66 #include <openssl/err.h>
70 static void value_free_hash(CONF_VALUE *a, LHASH *conf);
71 static void value_free_stack(CONF_VALUE *a,LHASH *conf);
72 static unsigned long hash(CONF_VALUE *v);
73 static int cmp_conf(CONF_VALUE *a,CONF_VALUE *b);
74 static char *eat_ws(char *p);
75 static char *eat_alpha_numeric(char *p);
76 static void clear_comments(char *p);
77 static int str_copy(LHASH *conf,char *section,char **to, char *from);
78 static char *scan_quote(char *p);
79 static CONF_VALUE *new_section(LHASH *conf,char *section);
80 static CONF_VALUE *get_section(LHASH *conf,char *section);
81 #define scan_esc(p) ((((p)[1] == '\0')?(p++):(p+=2)),p)
83 const char *CONF_version="CONF" OPENSSL_VERSION_PTEXT;
86 LHASH *CONF_load(LHASH *h, const char *file, long *line)
92 in=BIO_new_file(file, "r");
94 in=BIO_new_file(file, "rb");
98 CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB);
102 ltmp = CONF_load_bio(h, in, line);
108 LHASH *CONF_load_fp(LHASH *h, FILE *in, long *line)
112 if(!(btmp = BIO_new_fp(in, BIO_NOCLOSE))) {
113 CONFerr(CONF_F_CONF_LOAD_FP,ERR_R_BUF_LIB);
116 ltmp = CONF_load_bio(h, btmp, line);
122 LHASH *CONF_load_bio(LHASH *h, BIO *in, long *line)
132 CONF_VALUE *v=NULL,*vv,*tv;
134 char *section=NULL,*buf;
135 STACK_OF(CONF_VALUE) *section_sk=NULL,*ts;
136 char *start,*psection,*pname;
138 if ((buff=BUF_MEM_new()) == NULL)
140 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
144 section=(char *)Malloc(10);
147 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
150 strcpy(section,"default");
154 if ((ret=lh_new(hash,cmp_conf)) == NULL)
156 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_MALLOC_FAILURE);
163 sv=new_section(ret,section);
166 CONFerr(CONF_F_CONF_LOAD_BIO,
167 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
170 section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
176 if (!BUF_MEM_grow(buff,bufnum+BUFSIZE))
178 CONFerr(CONF_F_CONF_LOAD_BIO,ERR_R_BUF_LIB);
181 p= &(buff->data[bufnum]);
183 BIO_gets(in, p, BUFSIZE-1);
189 if ((p[i-1] != '\r') && (p[i-1] != '\n'))
194 /* we removed some trailing stuff so there is a new
195 * line on the end. */
197 again=1; /* long line */
201 eline++; /* another input line */
204 /* we now have a line with trailing \r\n removed */
206 /* i is the number of bytes */
210 /* check for line continuation */
213 /* If we have bytes and the last char '\\' and
214 * second last char is not '\\' */
215 p= &(buff->data[bufnum-1]);
217 ((bufnum <= 1) || !IS_ESC(p[-1])))
230 if (IS_EOF(*s)) continue; /* blank line */
239 end=eat_alpha_numeric(ss);
248 CONFerr(CONF_F_CONF_LOAD_BIO,
249 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
253 if (!str_copy(ret,NULL,§ion,start)) goto err;
254 if ((sv=get_section(ret,section)) == NULL)
255 sv=new_section(ret,section);
258 CONFerr(CONF_F_CONF_LOAD_BIO,
259 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
262 section_sk=(STACK_OF(CONF_VALUE) *)sv->value;
269 end=eat_alpha_numeric(s);
270 if ((end[0] == ':') && (end[1] == ':'))
276 end=eat_alpha_numeric(end);
281 CONFerr(CONF_F_CONF_LOAD_BIO,
282 CONF_R_MISSING_EQUAL_SIGN);
291 while ((p != start) && (IS_WS(*p)))
296 if (!(v=(CONF_VALUE *)Malloc(sizeof(CONF_VALUE))))
298 CONFerr(CONF_F_CONF_LOAD_BIO,
299 ERR_R_MALLOC_FAILURE);
302 if (psection == NULL) psection=section;
303 v->name=(char *)Malloc(strlen(pname)+1);
307 CONFerr(CONF_F_CONF_LOAD_BIO,
308 ERR_R_MALLOC_FAILURE);
311 strcpy(v->name,pname);
312 if (!str_copy(ret,psection,&(v->value),start)) goto err;
314 if (strcmp(psection,section) != 0)
316 if ((tv=get_section(ret,psection))
318 tv=new_section(ret,psection);
321 CONFerr(CONF_F_CONF_LOAD_BIO,
322 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
325 ts=(STACK_OF(CONF_VALUE) *)tv->value;
332 v->section=tv->section;
333 if (!sk_CONF_VALUE_push(ts,v))
335 CONFerr(CONF_F_CONF_LOAD_BIO,
336 ERR_R_MALLOC_FAILURE);
339 vv=(CONF_VALUE *)lh_insert(ret,v);
342 sk_CONF_VALUE_delete_ptr(ts,vv);
350 if (buff != NULL) BUF_MEM_free(buff);
351 if (section != NULL) Free(section);
354 if (buff != NULL) BUF_MEM_free(buff);
355 if (section != NULL) Free(section);
356 if (line != NULL) *line=eline;
357 sprintf(btmp,"%ld",eline);
358 ERR_add_error_data(2,"line ",btmp);
359 if ((h != ret) && (ret != NULL)) CONF_free(ret);
362 if (v->name != NULL) Free(v->name);
363 if (v->value != NULL) Free(v->value);
364 if (v != NULL) Free(v);
369 char *CONF_get_string(LHASH *conf, char *section, char *name)
374 if (name == NULL) return(NULL);
381 v=(CONF_VALUE *)lh_retrieve(conf,&vv);
382 if (v != NULL) return(v->value);
383 if (strcmp(section,"ENV") == 0)
386 if (p != NULL) return(p);
389 vv.section="default";
391 v=(CONF_VALUE *)lh_retrieve(conf,&vv);
398 return(Getenv(name));
401 static CONF_VALUE *get_section(LHASH *conf, char *section)
405 if ((conf == NULL) || (section == NULL)) return(NULL);
408 v=(CONF_VALUE *)lh_retrieve(conf,&vv);
412 STACK_OF(CONF_VALUE) *CONF_get_section(LHASH *conf, char *section)
416 v=get_section(conf,section);
418 return((STACK_OF(CONF_VALUE) *)v->value);
423 long CONF_get_number(LHASH *conf, char *section, char *name)
428 str=CONF_get_string(conf,section,name);
429 if (str == NULL) return(0);
433 ret=ret*10+(*str -'0');
440 void CONF_free(LHASH *conf)
442 if (conf == NULL) return;
444 conf->down_load=0; /* evil thing to make sure the 'Free()'
445 * works as expected */
446 lh_doall_arg(conf,(void (*)())value_free_hash,conf);
448 /* We now have only 'section' entries in the hash table.
449 * Due to problems with */
451 lh_doall_arg(conf,(void (*)())value_free_stack,conf);
455 static void value_free_hash(CONF_VALUE *a, LHASH *conf)
459 a=(CONF_VALUE *)lh_delete(conf,a);
463 static void value_free_stack(CONF_VALUE *a, LHASH *conf)
469 if (a->name != NULL) return;
471 sk=(STACK *)a->value;
472 for (i=sk_num(sk)-1; i>=0; i--)
474 vv=(CONF_VALUE *)sk_value(sk,i);
479 if (sk != NULL) sk_free(sk);
484 static void clear_comments(char *p)
513 static int str_copy(LHASH *conf, char *section, char **pto, char *from)
515 int q,r,rr=0,to=0,len=0;
516 char *s,*e,*rp,*p,*rrp,*np,*cp,v;
519 if ((buf=BUF_MEM_new()) == NULL) return(0);
522 if (!BUF_MEM_grow(buf,len)) goto err;
530 while ((*from != '\0') && (*from != q))
535 if (*from == '\0') break;
537 buf->data[to++]= *(from++);
540 else if (*from == '\\')
544 if (v == '\0') break;
545 else if (v == 'r') v='\r';
546 else if (v == 'n') v='\n';
547 else if (v == 'b') v='\b';
548 else if (v == 't') v='\t';
551 else if (*from == '\0')
553 else if (*from == '$')
555 /* try to expand it */
567 while (IS_ALPHA_NUMERIC(*e))
569 if ((e[0] == ':') && (e[1] == ':'))
577 while (IS_ALPHA_NUMERIC(*e))
587 CONFerr(CONF_F_STR_COPY,CONF_R_NO_CLOSE_BRACE);
592 /* So at this point we have
593 * ns which is the start of the name string which is
595 * cs which is the start of the section string which is
597 * e is the 'next point after'.
598 * r and s are the chars replaced by the '\0'
599 * rp and sp is where 'r' and 's' came from.
601 p=CONF_get_string(conf,cp,np);
602 if (rrp != NULL) *rrp=rr;
606 CONFerr(CONF_F_STR_COPY,CONF_R_VARIABLE_HAS_NO_VALUE);
609 BUF_MEM_grow(buf,(strlen(p)+len-(e-from)));
611 buf->data[to++]= *(p++);
615 buf->data[to++]= *(from++);
618 if (*pto != NULL) Free(*pto);
623 if (buf != NULL) BUF_MEM_free(buf);
627 static char *eat_ws(char *p)
629 while (IS_WS(*p) && (!IS_EOF(*p)))
634 static char *eat_alpha_numeric(char *p)
643 if (!IS_ALPHA_NUMERIC_PUNCT(*p))
649 static unsigned long hash(CONF_VALUE *v)
651 return((lh_strhash(v->section)<<2)^lh_strhash(v->name));
654 static int cmp_conf(CONF_VALUE *a, CONF_VALUE *b)
658 if (a->section != b->section)
660 i=strcmp(a->section,b->section);
664 if ((a->name != NULL) && (b->name != NULL))
666 i=strcmp(a->name,b->name);
669 else if (a->name == b->name)
672 return((a->name == NULL)?-1:1);
675 static char *scan_quote(char *p)
680 while (!(IS_EOF(*p)) && (*p != q))
685 if (IS_EOF(*p)) return(p);
693 static CONF_VALUE *new_section(LHASH *conf, char *section)
697 CONF_VALUE *v=NULL,*vv;
699 if ((sk=sk_new_null()) == NULL)
701 if ((v=(CONF_VALUE *)Malloc(sizeof(CONF_VALUE))) == NULL)
704 if ((v->section=(char *)Malloc(i)) == NULL)
707 memcpy(v->section,section,i);
711 vv=(CONF_VALUE *)lh_insert(conf,v);
714 #if !defined(NO_STDIO) && !defined(WIN16)
715 fprintf(stderr,"internal fault\n");
723 if (sk != NULL) sk_free(sk);
724 if (v != NULL) Free(v);
730 IMPLEMENT_STACK_OF(CONF_VALUE)