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_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
)
98 SYSerr(SYS_F_FOPEN
,get_last_sys_error());
99 ERR_set_error_data(BUF_strdup(file
),
100 ERR_TXT_MALLOCED
|ERR_TXT_STRING
);
101 CONFerr(CONF_F_CONF_LOAD
,ERR_R_SYS_LIB
);
105 ltmp
= CONF_load_fp(h
, in
, line
);
111 LHASH
*CONF_load_fp(LHASH
*h
, FILE *in
, long *line
)
115 if(!(btmp
= BIO_new_fp(in
, BIO_NOCLOSE
))) {
116 CONFerr(CONF_F_CONF_LOAD_FP
,ERR_R_BUF_LIB
);
119 ltmp
= CONF_load_bio(h
, btmp
, line
);
124 LHASH
*CONF_load_bio(LHASH
*h
, BIO
*in
, long *line
)
134 CONF_VALUE
*v
=NULL
,*vv
,*tv
;
136 char *section
=NULL
,*buf
;
137 STACK
*section_sk
=NULL
,*ts
;
138 char *start
,*psection
,*pname
;
140 if ((buff
=BUF_MEM_new()) == NULL
)
142 CONFerr(CONF_F_CONF_LOAD_BIO
,ERR_R_BUF_LIB
);
146 section
=(char *)Malloc(10);
149 CONFerr(CONF_F_CONF_LOAD_BIO
,ERR_R_MALLOC_FAILURE
);
152 strcpy(section
,"default");
156 if ((ret
=lh_new(hash
,cmp
)) == NULL
)
158 CONFerr(CONF_F_CONF_LOAD_BIO
,ERR_R_MALLOC_FAILURE
);
165 sv
=new_section(ret
,section
);
168 CONFerr(CONF_F_CONF_LOAD_BIO
,
169 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
172 section_sk
=(STACK
*)sv
->value
;
178 if (!BUF_MEM_grow(buff
,bufnum
+BUFSIZE
))
180 CONFerr(CONF_F_CONF_LOAD_BIO
,ERR_R_BUF_LIB
);
183 p
= &(buff
->data
[bufnum
]);
185 BIO_gets(in
, p
, BUFSIZE
-1);
191 if ((p
[i
-1] != '\r') && (p
[i
-1] != '\n'))
196 /* we removed some trailing stuff so there is a new
197 * line on the end. */
199 again
=1; /* long line */
203 eline
++; /* another input line */
206 /* we now have a line with trailing \r\n removed */
208 /* i is the number of bytes */
212 /* check for line continuation */
215 /* If we have bytes and the last char '\\' and
216 * second last char is not '\\' */
217 p
= &(buff
->data
[bufnum
-1]);
219 ((bufnum
<= 1) || !IS_ESC(p
[-1])))
232 if (IS_EOF(*s
)) continue; /* blank line */
241 end
=eat_alpha_numeric(ss
);
250 CONFerr(CONF_F_CONF_LOAD_BIO
,
251 CONF_R_MISSING_CLOSE_SQUARE_BRACKET
);
255 if (!str_copy(ret
,NULL
,§ion
,start
)) goto err
;
256 if ((sv
=get_section(ret
,section
)) == NULL
)
257 sv
=new_section(ret
,section
);
260 CONFerr(CONF_F_CONF_LOAD_BIO
,
261 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
264 section_sk
=(STACK
*)sv
->value
;
271 end
=eat_alpha_numeric(s
);
272 if ((end
[0] == ':') && (end
[1] == ':'))
278 end
=eat_alpha_numeric(end
);
283 CONFerr(CONF_F_CONF_LOAD_BIO
,
284 CONF_R_MISSING_EQUAL_SIGN
);
293 while ((p
!= start
) && (IS_WS(*p
)))
298 if ((v
=(CONF_VALUE
*)Malloc(sizeof(CONF_VALUE
))) == NULL
)
300 CONFerr(CONF_F_CONF_LOAD_BIO
,
301 ERR_R_MALLOC_FAILURE
);
304 if (psection
== NULL
) psection
=section
;
305 v
->name
=(char *)Malloc(strlen(pname
)+1);
309 CONFerr(CONF_F_CONF_LOAD_BIO
,
310 ERR_R_MALLOC_FAILURE
);
313 strcpy(v
->name
,pname
);
314 if (!str_copy(ret
,psection
,&(v
->value
),start
)) goto err
;
316 if (strcmp(psection
,section
) != 0)
318 if ((tv
=get_section(ret
,psection
))
320 tv
=new_section(ret
,psection
);
323 CONFerr(CONF_F_CONF_LOAD_BIO
,
324 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
327 ts
=(STACK
*)tv
->value
;
334 v
->section
=tv
->section
;
335 if (!sk_push(ts
,(char *)v
))
337 CONFerr(CONF_F_CONF_LOAD_BIO
,
338 ERR_R_MALLOC_FAILURE
);
341 vv
=(CONF_VALUE
*)lh_insert(ret
,(char *)v
);
344 sk_delete_ptr(ts
,(char *)vv
);
352 if (buff
!= NULL
) BUF_MEM_free(buff
);
353 if (section
!= NULL
) Free(section
);
356 if (buff
!= NULL
) BUF_MEM_free(buff
);
357 if (section
!= NULL
) Free(section
);
358 if (line
!= NULL
) *line
=eline
;
359 sprintf(btmp
,"%ld",eline
);
360 ERR_add_error_data(2,"line ",btmp
);
361 if ((h
!= ret
) && (ret
!= NULL
)) CONF_free(ret
);
364 if (v
->name
!= NULL
) Free(v
->name
);
365 if (v
->value
!= NULL
) Free(v
->value
);
366 if (v
!= NULL
) Free(v
);
371 char *CONF_get_string(LHASH
*conf
, char *section
, char *name
)
376 if (name
== NULL
) return(NULL
);
383 v
=(CONF_VALUE
*)lh_retrieve(conf
,(char *)&vv
);
384 if (v
!= NULL
) return(v
->value
);
385 if (strcmp(section
,"ENV") == 0)
388 if (p
!= NULL
) return(p
);
391 vv
.section
="default";
393 v
=(CONF_VALUE
*)lh_retrieve(conf
,(char *)&vv
);
400 return(Getenv(name
));
403 static CONF_VALUE
*get_section(LHASH
*conf
, char *section
)
407 if ((conf
== NULL
) || (section
== NULL
)) return(NULL
);
410 v
=(CONF_VALUE
*)lh_retrieve(conf
,(char *)&vv
);
414 STACK
*CONF_get_section(LHASH
*conf
, char *section
)
418 v
=get_section(conf
,section
);
420 return((STACK
*)v
->value
);
425 long CONF_get_number(LHASH
*conf
, char *section
, char *name
)
430 str
=CONF_get_string(conf
,section
,name
);
431 if (str
== NULL
) return(0);
435 ret
=ret
*10+(*str
-'0');
442 void CONF_free(LHASH
*conf
)
444 if (conf
== NULL
) return;
446 conf
->down_load
=0; /* evil thing to make sure the 'Free()'
447 * works as expected */
448 lh_doall_arg(conf
,(void (*)())value_free_hash
,(char *)conf
);
450 /* We now have only 'section' entries in the hash table.
451 * Due to problems with */
453 lh_doall_arg(conf
,(void (*)())value_free_stack
,(char *)conf
);
457 static void value_free_hash(CONF_VALUE
*a
, LHASH
*conf
)
461 a
=(CONF_VALUE
*)lh_delete(conf
,(char *)a
);
465 static void value_free_stack(CONF_VALUE
*a
, LHASH
*conf
)
471 if (a
->name
!= NULL
) return;
473 sk
=(STACK
*)a
->value
;
474 for (i
=sk_num(sk
)-1; i
>=0; i
--)
476 vv
=(CONF_VALUE
*)sk_value(sk
,i
);
481 if (sk
!= NULL
) sk_free(sk
);
486 static void clear_comments(char *p
)
515 static int str_copy(LHASH
*conf
, char *section
, char **pto
, char *from
)
517 int q
,r
,rr
=0,to
=0,len
=0;
518 char *s
,*e
,*rp
,*p
,*rrp
,*np
,*cp
,v
;
521 if ((buf
=BUF_MEM_new()) == NULL
) return(0);
524 if (!BUF_MEM_grow(buf
,len
)) goto err
;
532 while ((*from
!= '\0') && (*from
!= q
))
537 if (*from
== '\0') break;
539 buf
->data
[to
++]= *(from
++);
542 else if (*from
== '\\')
546 if (v
== '\0') break;
547 else if (v
== 'r') v
='\r';
548 else if (v
== 'n') v
='\n';
549 else if (v
== 'b') v
='\b';
550 else if (v
== 't') v
='\t';
553 else if (*from
== '\0')
555 else if (*from
== '$')
557 /* try to expand it */
569 while (IS_ALPHA_NUMERIC(*e
))
571 if ((e
[0] == ':') && (e
[1] == ':'))
579 while (IS_ALPHA_NUMERIC(*e
))
589 CONFerr(CONF_F_STR_COPY
,CONF_R_NO_CLOSE_BRACE
);
594 /* So at this point we have
595 * ns which is the start of the name string which is
597 * cs which is the start of the section string which is
599 * e is the 'next point after'.
600 * r and s are the chars replaced by the '\0'
601 * rp and sp is where 'r' and 's' came from.
603 p
=CONF_get_string(conf
,cp
,np
);
604 if (rrp
!= NULL
) *rrp
=rr
;
608 CONFerr(CONF_F_STR_COPY
,CONF_R_VARIABLE_HAS_NO_VALUE
);
611 BUF_MEM_grow(buf
,(strlen(p
)+len
-(e
-from
)));
613 buf
->data
[to
++]= *(p
++);
617 buf
->data
[to
++]= *(from
++);
620 if (*pto
!= NULL
) Free(*pto
);
625 if (buf
!= NULL
) BUF_MEM_free(buf
);
629 static char *eat_ws(char *p
)
631 while (IS_WS(*p
) && (!IS_EOF(*p
)))
636 static char *eat_alpha_numeric(char *p
)
645 if (!IS_ALPHA_NUMERIC_PUNCT(*p
))
651 static unsigned long hash(CONF_VALUE
*v
)
653 return((lh_strhash(v
->section
)<<2)^lh_strhash(v
->name
));
656 static int cmp(CONF_VALUE
*a
, CONF_VALUE
*b
)
660 if (a
->section
!= b
->section
)
662 i
=strcmp(a
->section
,b
->section
);
666 if ((a
->name
!= NULL
) && (b
->name
!= NULL
))
668 i
=strcmp(a
->name
,b
->name
);
671 else if (a
->name
== b
->name
)
674 return((a
->name
== NULL
)?-1:1);
677 static char *scan_quote(char *p
)
682 while (!(IS_EOF(*p
)) && (*p
!= q
))
687 if (IS_EOF(*p
)) return(p
);
695 static CONF_VALUE
*new_section(LHASH
*conf
, char *section
)
699 CONF_VALUE
*v
=NULL
,*vv
;
701 if ((sk
=sk_new_null()) == NULL
)
703 if ((v
=(CONF_VALUE
*)Malloc(sizeof(CONF_VALUE
))) == NULL
)
706 if ((v
->section
=(char *)Malloc(i
)) == NULL
)
709 memcpy(v
->section
,section
,i
);
713 vv
=(CONF_VALUE
*)lh_insert(conf
,(char *)v
);
716 #if !defined(NO_STDIO) && !defined(WIN16)
717 fprintf(stderr
,"internal fault\n");
725 if (sk
!= NULL
) sk_free(sk
);
726 if (v
!= NULL
) Free(v
);