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.]
71 static void value_free_hash(CONF_VALUE
*a
, LHASH
*conf
);
72 static void value_free_stack(CONF_VALUE
*a
,LHASH
*conf
);
73 static unsigned long hash(CONF_VALUE
*v
);
74 static int cmp(CONF_VALUE
*a
,CONF_VALUE
*b
);
75 static char *eat_ws(char *p
);
76 static char *eat_alpha_numeric(char *p
);
77 static void clear_comments(char *p
);
78 static int str_copy(LHASH
*conf
,char *section
,char **to
, char *from
);
79 static char *scan_quote(char *p
);
80 static CONF_VALUE
*new_section(LHASH
*conf
,char *section
);
81 static CONF_VALUE
*get_section(LHASH
*conf
,char *section
);
83 static void value_free_hash();
84 static void value_free_stack();
85 static unsigned long hash();
87 static char *eat_ws();
88 static char *eat_alpha_numeric();
89 static void clear_comments();
90 static int str_copy();
91 static char *scan_quote();
92 static CONF_VALUE
*new_section();
93 static CONF_VALUE
*get_section();
96 #define scan_esc(p) ((((p)[1] == '\0')?(p++):(p+=2)),p)
98 char *CONF_version
="CONF part of OpenSSL 0.9.2 31-Dec-1998";
100 LHASH
*CONF_load(h
,file
,line
)
114 CONF_VALUE
*v
=NULL
,*vv
,*tv
;
116 char *section
=NULL
,*buf
;
117 STACK
*section_sk
=NULL
,*ts
;
118 char *start
,*psection
,*pname
;
120 if ((buff
=BUF_MEM_new()) == NULL
)
122 CONFerr(CONF_F_CONF_LOAD
,ERR_R_BUF_LIB
);
129 SYSerr(SYS_F_FOPEN
,get_last_sys_error());
130 ERR_set_error_data(BUF_strdup(file
),
131 ERR_TXT_MALLOCED
|ERR_TXT_STRING
);
132 CONFerr(CONF_F_CONF_LOAD
,ERR_R_SYS_LIB
);
136 section
=(char *)Malloc(10);
139 CONFerr(CONF_F_CONF_LOAD
,ERR_R_MALLOC_FAILURE
);
142 strcpy(section
,"default");
146 if ((ret
=lh_new(hash
,cmp
)) == NULL
)
148 CONFerr(CONF_F_CONF_LOAD
,ERR_R_MALLOC_FAILURE
);
155 sv
=new_section(ret
,section
);
158 CONFerr(CONF_F_CONF_LOAD
,CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
161 section_sk
=(STACK
*)sv
->value
;
167 if (!BUF_MEM_grow(buff
,bufnum
+BUFSIZE
))
169 CONFerr(CONF_F_CONF_LOAD
,ERR_R_BUF_LIB
);
172 p
= &(buff
->data
[bufnum
]);
174 fgets(p
,BUFSIZE
-1,in
);
180 if ((p
[i
-1] != '\r') && (p
[i
-1] != '\n'))
185 /* we removed some trailing stuff so there is a new
186 * line on the end. */
188 again
=1; /* long line */
192 eline
++; /* another input line */
195 /* we now have a line with trailing \r\n removed */
197 /* i is the number of bytes */
201 /* check for line continuation */
204 /* If we have bytes and the last char '\\' and
205 * second last char is not '\\' */
206 p
= &(buff
->data
[bufnum
-1]);
208 ((bufnum
<= 1) || !IS_ESC(p
[-1])))
221 if (IS_EOF(*s
)) continue; /* blank line */
230 end
=eat_alpha_numeric(ss
);
239 CONFerr(CONF_F_CONF_LOAD
,CONF_R_MISSING_CLOSE_SQUARE_BRACKET
);
243 if (!str_copy(ret
,NULL
,§ion
,start
)) goto err
;
244 if ((sv
=get_section(ret
,section
)) == NULL
)
245 sv
=new_section(ret
,section
);
248 CONFerr(CONF_F_CONF_LOAD
,CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
251 section_sk
=(STACK
*)sv
->value
;
258 end
=eat_alpha_numeric(s
);
259 if ((end
[0] == ':') && (end
[1] == ':'))
265 end
=eat_alpha_numeric(end
);
270 CONFerr(CONF_F_CONF_LOAD
,CONF_R_MISSING_EQUAL_SIGN
);
279 while ((p
!= start
) && (IS_WS(*p
)))
284 if ((v
=(CONF_VALUE
*)Malloc(sizeof(CONF_VALUE
))) == NULL
)
286 CONFerr(CONF_F_CONF_LOAD
,ERR_R_MALLOC_FAILURE
);
289 if (psection
== NULL
) psection
=section
;
290 v
->name
=(char *)Malloc(strlen(pname
)+1);
294 CONFerr(CONF_F_CONF_LOAD
,ERR_R_MALLOC_FAILURE
);
297 strcpy(v
->name
,pname
);
298 if (!str_copy(ret
,psection
,&(v
->value
),start
)) goto err
;
300 if (strcmp(psection
,section
) != 0)
302 if ((tv
=get_section(ret
,psection
))
304 tv
=new_section(ret
,psection
);
307 CONFerr(CONF_F_CONF_LOAD
,CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
310 ts
=(STACK
*)tv
->value
;
317 v
->section
=tv
->section
;
318 if (!sk_push(ts
,(char *)v
))
320 CONFerr(CONF_F_CONF_LOAD
,ERR_R_MALLOC_FAILURE
);
323 vv
=(CONF_VALUE
*)lh_insert(ret
,(char *)v
);
326 sk_delete_ptr(ts
,(char *)vv
);
334 if (buff
!= NULL
) BUF_MEM_free(buff
);
335 if (section
!= NULL
) Free(section
);
336 if (in
!= NULL
) fclose(in
);
339 if (buff
!= NULL
) BUF_MEM_free(buff
);
340 if (section
!= NULL
) Free(section
);
341 if (line
!= NULL
) *line
=eline
;
342 sprintf(btmp
,"%ld",eline
);
343 ERR_add_error_data(2,"line ",btmp
);
344 if (in
!= NULL
) fclose(in
);
345 if ((h
!= ret
) && (ret
!= NULL
)) CONF_free(ret
);
348 if (v
->name
!= NULL
) Free(v
->name
);
349 if (v
->value
!= NULL
) Free(v
->value
);
350 if (v
!= NULL
) Free(v
);
355 char *CONF_get_string(conf
,section
,name
)
363 if (name
== NULL
) return(NULL
);
370 v
=(CONF_VALUE
*)lh_retrieve(conf
,(char *)&vv
);
371 if (v
!= NULL
) return(v
->value
);
372 if (strcmp(section
,"ENV") == 0)
375 if (p
!= NULL
) return(p
);
378 vv
.section
="default";
380 v
=(CONF_VALUE
*)lh_retrieve(conf
,(char *)&vv
);
387 return(Getenv(name
));
390 static CONF_VALUE
*get_section(conf
,section
)
396 if ((conf
== NULL
) || (section
== NULL
)) return(NULL
);
399 v
=(CONF_VALUE
*)lh_retrieve(conf
,(char *)&vv
);
403 STACK
*CONF_get_section(conf
,section
)
409 v
=get_section(conf
,section
);
411 return((STACK
*)v
->value
);
416 long CONF_get_number(conf
,section
,name
)
424 str
=CONF_get_string(conf
,section
,name
);
425 if (str
== NULL
) return(0);
429 ret
=ret
*10+(*str
-'0');
439 if (conf
== NULL
) return;
441 conf
->down_load
=0; /* evil thing to make sure the 'Free()'
442 * works as expected */
443 lh_doall_arg(conf
,(void (*)())value_free_hash
,(char *)conf
);
445 /* We now have only 'section' entries in the hash table.
446 * Due to problems with */
448 lh_doall_arg(conf
,(void (*)())value_free_stack
,(char *)conf
);
452 static void value_free_hash(a
,conf
)
458 a
=(CONF_VALUE
*)lh_delete(conf
,(char *)a
);
462 static void value_free_stack(a
,conf
)
470 if (a
->name
!= NULL
) return;
472 sk
=(STACK
*)a
->value
;
473 for (i
=sk_num(sk
)-1; i
>=0; i
--)
475 vv
=(CONF_VALUE
*)sk_value(sk
,i
);
480 if (sk
!= NULL
) sk_free(sk
);
485 static void clear_comments(p
)
515 static int str_copy(conf
,section
,pto
,from
)
520 int q
,r
,rr
=0,to
=0,len
=0;
521 char *s
,*e
,*rp
,*p
,*rrp
,*np
,*cp
,v
;
524 if ((buf
=BUF_MEM_new()) == NULL
) return(0);
527 if (!BUF_MEM_grow(buf
,len
)) goto err
;
535 while ((*from
!= '\0') && (*from
!= q
))
540 if (*from
== '\0') break;
542 buf
->data
[to
++]= *(from
++);
545 else if (*from
== '\\')
549 if (v
== '\0') break;
550 else if (v
== 'r') v
='\r';
551 else if (v
== 'n') v
='\n';
552 else if (v
== 'b') v
='\b';
553 else if (v
== 't') v
='\t';
556 else if (*from
== '\0')
558 else if (*from
== '$')
560 /* try to expand it */
572 while (IS_ALPHA_NUMERIC(*e
))
574 if ((e
[0] == ':') && (e
[1] == ':'))
582 while (IS_ALPHA_NUMERIC(*e
))
592 CONFerr(CONF_F_STR_COPY
,CONF_R_NO_CLOSE_BRACE
);
597 /* So at this point we have
598 * ns which is the start of the name string which is
600 * cs which is the start of the section string which is
602 * e is the 'next point after'.
603 * r and s are the chars replaced by the '\0'
604 * rp and sp is where 'r' and 's' came from.
606 p
=CONF_get_string(conf
,cp
,np
);
607 if (rrp
!= NULL
) *rrp
=rr
;
611 CONFerr(CONF_F_STR_COPY
,CONF_R_VARIABLE_HAS_NO_VALUE
);
614 BUF_MEM_grow(buf
,(strlen(p
)+len
-(e
-from
)));
616 buf
->data
[to
++]= *(p
++);
620 buf
->data
[to
++]= *(from
++);
623 if (*pto
!= NULL
) Free(*pto
);
628 if (buf
!= NULL
) BUF_MEM_free(buf
);
632 static char *eat_ws(p
)
635 while (IS_WS(*p
) && (!IS_EOF(*p
)))
640 static char *eat_alpha_numeric(p
)
650 if (!IS_ALPHA_NUMERIC_PUNCT(*p
))
656 static unsigned long hash(v
)
659 return((lh_strhash(v
->section
)<<2)^lh_strhash(v
->name
));
667 if (a
->section
!= b
->section
)
669 i
=strcmp(a
->section
,b
->section
);
673 if ((a
->name
!= NULL
) && (b
->name
!= NULL
))
675 i
=strcmp(a
->name
,b
->name
);
678 else if (a
->name
== b
->name
)
681 return((a
->name
== NULL
)?-1:1);
684 static char *scan_quote(p
)
690 while (!(IS_EOF(*p
)) && (*p
!= q
))
695 if (IS_EOF(*p
)) return(p
);
703 static CONF_VALUE
*new_section(conf
,section
)
709 CONF_VALUE
*v
=NULL
,*vv
;
711 if ((sk
=sk_new_null()) == NULL
)
713 if ((v
=(CONF_VALUE
*)Malloc(sizeof(CONF_VALUE
))) == NULL
)
716 if ((v
->section
=(char *)Malloc(i
)) == NULL
)
719 memcpy(v
->section
,section
,i
);
723 vv
=(CONF_VALUE
*)lh_insert(conf
,(char *)v
);
726 #if !defined(NO_STDIO) && !defined(WIN16)
727 fprintf(stderr
,"internal fault\n");
735 if (sk
!= NULL
) sk_free(sk
);
736 if (v
!= NULL
) Free(v
);