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.]
59 /* Part of the code in here was originally in conf.c, which is now removed */
62 #include <openssl/stack.h>
63 #include <openssl/lhash.h>
64 #include <openssl/conf.h>
65 #include <openssl/conf_api.h>
67 #include <openssl/buffer.h>
68 #include <openssl/err.h>
70 static char *eat_ws(CONF
*conf
, char *p
);
71 static char *eat_alpha_numeric(CONF
*conf
, char *p
);
72 static void clear_comments(CONF
*conf
, char *p
);
73 static int str_copy(CONF
*conf
,char *section
,char **to
, char *from
);
74 static char *scan_quote(CONF
*conf
, char *p
);
75 static char *scan_dquote(CONF
*conf
, char *p
);
76 #define scan_esc(p) (((IS_EOF((conf),(p)[1]))?(p+=1):(p+=2)))
78 static CONF
*def_create(CONF_METHOD
*meth
);
79 static int def_init_default(CONF
*conf
);
80 static int def_init_WIN32(CONF
*conf
);
81 static int def_destroy(CONF
*conf
);
82 static int def_destroy_data(CONF
*conf
);
83 static int def_load(CONF
*conf
, BIO
*bp
, long *eline
);
84 static int def_dump(CONF
*conf
, BIO
*bp
);
85 static int def_is_number(CONF
*conf
, char c
);
86 static int def_to_int(CONF
*conf
, char c
);
88 const char *CONF_def_version
="CONF_def" OPENSSL_VERSION_PTEXT
;
90 static CONF_METHOD default_method
= {
102 static CONF_METHOD WIN32_method
= {
114 CONF_METHOD
*NCONF_default()
116 return &default_method
;
118 CONF_METHOD
*NCONF_WIN32()
120 return &WIN32_method
;
123 static CONF
*def_create(CONF_METHOD
*meth
)
127 ret
= (CONF
*)OPENSSL_malloc(sizeof(CONF
) + sizeof(unsigned short *));
129 if (meth
->init(ret
) == 0)
137 static int def_init_default(CONF
*conf
)
142 conf
->meth
= &default_method
;
143 conf
->meth_data
= (void *)CONF_type_default
;
149 static int def_init_WIN32(CONF
*conf
)
154 conf
->meth
= &WIN32_method
;
155 conf
->meth_data
= (void *)CONF_type_win32
;
161 static int def_destroy(CONF
*conf
)
163 if (def_destroy_data(conf
))
171 static int def_destroy_data(CONF
*conf
)
175 _CONF_free_data(conf
);
179 static int def_load(CONF
*conf
, BIO
*in
, long *line
)
188 CONF_VALUE
*v
=NULL
,*tv
;
190 char *section
=NULL
,*buf
;
191 STACK_OF(CONF_VALUE
) *section_sk
=NULL
,*ts
;
192 char *start
,*psection
,*pname
;
193 void *h
= (void *)(conf
->data
);
195 if ((buff
=BUF_MEM_new()) == NULL
)
197 CONFerr(CONF_F_CONF_LOAD_BIO
,ERR_R_BUF_LIB
);
201 section
=(char *)OPENSSL_malloc(10);
204 CONFerr(CONF_F_CONF_LOAD_BIO
,ERR_R_MALLOC_FAILURE
);
207 strcpy(section
,"default");
209 if (_CONF_new_data(conf
) == 0)
211 CONFerr(CONF_F_CONF_LOAD_BIO
,ERR_R_MALLOC_FAILURE
);
215 sv
=_CONF_new_section(conf
,section
);
218 CONFerr(CONF_F_CONF_LOAD_BIO
,
219 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
222 section_sk
=(STACK_OF(CONF_VALUE
) *)sv
->value
;
228 if (!BUF_MEM_grow(buff
,bufnum
+BUFSIZE
))
230 CONFerr(CONF_F_CONF_LOAD_BIO
,ERR_R_BUF_LIB
);
233 p
= &(buff
->data
[bufnum
]);
235 BIO_gets(in
, p
, BUFSIZE
-1);
241 if ((p
[i
-1] != '\r') && (p
[i
-1] != '\n'))
246 /* we removed some trailing stuff so there is a new
247 * line on the end. */
249 again
=1; /* long line */
253 eline
++; /* another input line */
256 /* we now have a line with trailing \r\n removed */
258 /* i is the number of bytes */
262 /* check for line continuation */
265 /* If we have bytes and the last char '\\' and
266 * second last char is not '\\' */
267 p
= &(buff
->data
[bufnum
-1]);
268 if (IS_ESC(conf
,p
[0]) &&
269 ((bufnum
<= 1) || !IS_ESC(conf
,p
[-1])))
279 clear_comments(conf
, buf
);
282 if (IS_EOF(conf
,*s
)) continue; /* blank line */
288 start
=eat_ws(conf
, s
);
291 end
=eat_alpha_numeric(conf
, ss
);
300 CONFerr(CONF_F_CONF_LOAD_BIO
,
301 CONF_R_MISSING_CLOSE_SQUARE_BRACKET
);
305 if (!str_copy(conf
,NULL
,§ion
,start
)) goto err
;
306 if ((sv
=_CONF_get_section(conf
,section
)) == NULL
)
307 sv
=_CONF_new_section(conf
,section
);
310 CONFerr(CONF_F_CONF_LOAD_BIO
,
311 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
314 section_sk
=(STACK_OF(CONF_VALUE
) *)sv
->value
;
321 end
=eat_alpha_numeric(conf
, s
);
322 if ((end
[0] == ':') && (end
[1] == ':'))
328 end
=eat_alpha_numeric(conf
, end
);
333 CONFerr(CONF_F_CONF_LOAD_BIO
,
334 CONF_R_MISSING_EQUAL_SIGN
);
339 start
=eat_ws(conf
, p
);
340 while (!IS_EOF(conf
,*p
))
343 while ((p
!= start
) && (IS_WS(conf
,*p
)))
348 if (!(v
=(CONF_VALUE
*)OPENSSL_malloc(sizeof(CONF_VALUE
))))
350 CONFerr(CONF_F_CONF_LOAD_BIO
,
351 ERR_R_MALLOC_FAILURE
);
354 if (psection
== NULL
) psection
=section
;
355 v
->name
=(char *)OPENSSL_malloc(strlen(pname
)+1);
359 CONFerr(CONF_F_CONF_LOAD_BIO
,
360 ERR_R_MALLOC_FAILURE
);
363 strcpy(v
->name
,pname
);
364 if (!str_copy(conf
,psection
,&(v
->value
),start
)) goto err
;
366 if (strcmp(psection
,section
) != 0)
368 if ((tv
=_CONF_get_section(conf
,psection
))
370 tv
=_CONF_new_section(conf
,psection
);
373 CONFerr(CONF_F_CONF_LOAD_BIO
,
374 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
377 ts
=(STACK_OF(CONF_VALUE
) *)tv
->value
;
385 if (_CONF_add_string(conf
, tv
, v
) == 0)
387 CONFerr(CONF_F_CONF_LOAD_BIO
,
388 ERR_R_MALLOC_FAILURE
);
392 v
->section
=tv
->section
;
393 if (!sk_CONF_VALUE_push(ts
,v
))
395 CONFerr(CONF_F_CONF_LOAD_BIO
,
396 ERR_R_MALLOC_FAILURE
);
399 vv
=(CONF_VALUE
*)lh_insert(conf
->data
,v
);
402 sk_CONF_VALUE_delete_ptr(ts
,vv
);
403 OPENSSL_free(vv
->name
);
404 OPENSSL_free(vv
->value
);
411 if (buff
!= NULL
) BUF_MEM_free(buff
);
412 if (section
!= NULL
) OPENSSL_free(section
);
415 if (buff
!= NULL
) BUF_MEM_free(buff
);
416 if (section
!= NULL
) OPENSSL_free(section
);
417 if (line
!= NULL
) *line
=eline
;
418 sprintf(btmp
,"%ld",eline
);
419 ERR_add_error_data(2,"line ",btmp
);
420 if ((h
!= conf
->data
) && (conf
->data
!= NULL
)) CONF_free(conf
->data
);
423 if (v
->name
!= NULL
) OPENSSL_free(v
->name
);
424 if (v
->value
!= NULL
) OPENSSL_free(v
->value
);
425 if (v
!= NULL
) OPENSSL_free(v
);
430 static void clear_comments(CONF
*conf
, char *p
)
437 if (IS_FCOMMENT(conf
,*p
))
451 if (IS_COMMENT(conf
,*p
))
456 if (IS_DQUOTE(conf
,*p
))
458 p
=scan_dquote(conf
, p
);
461 if (IS_QUOTE(conf
,*p
))
463 p
=scan_quote(conf
, p
);
478 static int str_copy(CONF
*conf
, char *section
, char **pto
, char *from
)
480 int q
,r
,rr
=0,to
=0,len
=0;
481 char *s
,*e
,*rp
,*p
,*rrp
,*np
,*cp
,v
;
484 if ((buf
=BUF_MEM_new()) == NULL
) return(0);
487 if (!BUF_MEM_grow(buf
,len
)) goto err
;
491 if (IS_QUOTE(conf
,*from
))
495 while (!IS_EOF(conf
,*from
) && (*from
!= q
))
497 if (IS_ESC(conf
,*from
))
500 if (IS_EOF(conf
,*from
)) break;
502 buf
->data
[to
++]= *(from
++);
504 if (*from
== q
) from
++;
506 else if (IS_DQUOTE(conf
,*from
))
510 while (!IS_EOF(conf
,*from
))
523 buf
->data
[to
++]= *(from
++);
525 if (*from
== q
) from
++;
527 else if (IS_ESC(conf
,*from
))
531 if (IS_EOF(conf
,v
)) break;
532 else if (v
== 'r') v
='\r';
533 else if (v
== 'n') v
='\n';
534 else if (v
== 'b') v
='\b';
535 else if (v
== 't') v
='\t';
538 else if (IS_EOF(conf
,*from
))
540 else if (*from
== '$')
542 /* try to expand it */
554 while (IS_ALPHA_NUMERIC(conf
,*e
))
556 if ((e
[0] == ':') && (e
[1] == ':'))
564 while (IS_ALPHA_NUMERIC(conf
,*e
))
574 CONFerr(CONF_F_STR_COPY
,CONF_R_NO_CLOSE_BRACE
);
579 /* So at this point we have
580 * ns which is the start of the name string which is
582 * cs which is the start of the section string which is
584 * e is the 'next point after'.
585 * r and s are the chars replaced by the '\0'
586 * rp and sp is where 'r' and 's' came from.
588 p
=_CONF_get_string(conf
,cp
,np
);
589 if (rrp
!= NULL
) *rrp
=rr
;
593 CONFerr(CONF_F_STR_COPY
,CONF_R_VARIABLE_HAS_NO_VALUE
);
596 BUF_MEM_grow(buf
,(strlen(p
)+len
-(e
-from
)));
598 buf
->data
[to
++]= *(p
++);
602 buf
->data
[to
++]= *(from
++);
605 if (*pto
!= NULL
) OPENSSL_free(*pto
);
610 if (buf
!= NULL
) BUF_MEM_free(buf
);
614 static char *eat_ws(CONF
*conf
, char *p
)
616 while (IS_WS(conf
,*p
) && (!IS_EOF(conf
,*p
)))
621 static char *eat_alpha_numeric(CONF
*conf
, char *p
)
630 if (!IS_ALPHA_NUMERIC_PUNCT(conf
,*p
))
636 static char *scan_quote(CONF
*conf
, char *p
)
641 while (!(IS_EOF(conf
,*p
)) && (*p
!= q
))
646 if (IS_EOF(conf
,*p
)) return(p
);
655 static char *scan_dquote(CONF
*conf
, char *p
)
660 while (!(IS_EOF(conf
,*p
)))
679 static void dump_value(CONF_VALUE
*a
, BIO
*out
)
682 BIO_printf(out
, "[%s] %s=%s\n", a
->section
, a
->name
, a
->value
);
684 BIO_printf(out
, "[[%s]]\n", a
->section
);
687 static int def_dump(CONF
*conf
, BIO
*out
)
689 lh_doall_arg(conf
->data
, (void (*)())dump_value
, out
);
693 static int def_is_number(CONF
*conf
, char c
)
695 return IS_NUMBER(conf
,c
);
698 static int def_to_int(CONF
*conf
, char c
)