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 */
64 #include <openssl/stack.h>
65 #include <openssl/lhash.h>
66 #include <openssl/conf.h>
67 #include <openssl/conf_api.h>
69 #include <openssl/buffer.h>
70 #include <openssl/err.h>
72 static char *eat_ws(CONF
*conf
, char *p
);
73 static char *eat_alpha_numeric(CONF
*conf
, char *p
);
74 static void clear_comments(CONF
*conf
, char *p
);
75 static int str_copy(CONF
*conf
, char *section
, char **to
, char *from
);
76 static char *scan_quote(CONF
*conf
, char *p
);
77 static char *scan_dquote(CONF
*conf
, char *p
);
78 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
80 static CONF
*def_create(CONF_METHOD
*meth
);
81 static int def_init_default(CONF
*conf
);
82 static int def_init_WIN32(CONF
*conf
);
83 static int def_destroy(CONF
*conf
);
84 static int def_destroy_data(CONF
*conf
);
85 static int def_load(CONF
*conf
, const char *name
, long *eline
);
86 static int def_load_bio(CONF
*conf
, BIO
*bp
, long *eline
);
87 static int def_dump(const CONF
*conf
, BIO
*bp
);
88 static int def_is_number(const CONF
*conf
, char c
);
89 static int def_to_int(const CONF
*conf
, char c
);
91 const char CONF_def_version
[] = "CONF_def" OPENSSL_VERSION_PTEXT
;
93 static CONF_METHOD default_method
= {
106 static CONF_METHOD WIN32_method
= {
119 CONF_METHOD
*NCONF_default()
121 return &default_method
;
124 CONF_METHOD
*NCONF_WIN32()
126 return &WIN32_method
;
129 static CONF
*def_create(CONF_METHOD
*meth
)
133 ret
= OPENSSL_malloc(sizeof(CONF
) + sizeof(unsigned short *));
135 if (meth
->init(ret
) == 0) {
142 static int def_init_default(CONF
*conf
)
147 conf
->meth
= &default_method
;
148 conf
->meth_data
= CONF_type_default
;
154 static int def_init_WIN32(CONF
*conf
)
159 conf
->meth
= &WIN32_method
;
160 conf
->meth_data
= (void *)CONF_type_win32
;
166 static int def_destroy(CONF
*conf
)
168 if (def_destroy_data(conf
)) {
175 static int def_destroy_data(CONF
*conf
)
179 _CONF_free_data(conf
);
183 static int def_load(CONF
*conf
, const char *name
, long *line
)
188 #ifdef OPENSSL_SYS_VMS
189 in
= BIO_new_file(name
, "r");
191 in
= BIO_new_file(name
, "rb");
194 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE
)
195 CONFerr(CONF_F_DEF_LOAD
, CONF_R_NO_SUCH_FILE
);
197 CONFerr(CONF_F_DEF_LOAD
, ERR_R_SYS_LIB
);
201 ret
= def_load_bio(conf
, in
, line
);
207 static int def_load_bio(CONF
*conf
, BIO
*in
, long *line
)
209 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
210 #define CONFBUFSIZE 512
211 int bufnum
= 0, i
, ii
;
212 BUF_MEM
*buff
= NULL
;
216 char btmp
[DECIMAL_SIZE(eline
) + 1];
217 CONF_VALUE
*v
= NULL
, *tv
;
218 CONF_VALUE
*sv
= NULL
;
219 char *section
= NULL
, *buf
;
220 char *start
, *psection
, *pname
;
221 void *h
= (void *)(conf
->data
);
223 if ((buff
= BUF_MEM_new()) == NULL
) {
224 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_BUF_LIB
);
228 section
= BUF_strdup("default");
229 if (section
== NULL
) {
230 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
234 if (_CONF_new_data(conf
) == 0) {
235 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
239 sv
= _CONF_new_section(conf
, section
);
241 CONFerr(CONF_F_DEF_LOAD_BIO
, CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
248 if (!BUF_MEM_grow(buff
, bufnum
+ CONFBUFSIZE
)) {
249 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_BUF_LIB
);
252 p
= &(buff
->data
[bufnum
]);
254 BIO_gets(in
, p
, CONFBUFSIZE
- 1);
255 p
[CONFBUFSIZE
- 1] = '\0';
257 if (i
== 0 && !again
)
261 if ((p
[i
- 1] != '\r') && (p
[i
- 1] != '\n'))
267 * we removed some trailing stuff so there is a new line on the end.
270 again
= 1; /* long line */
273 eline
++; /* another input line */
276 /* we now have a line with trailing \r\n removed */
278 /* i is the number of bytes */
282 /* check for line continuation */
285 * If we have bytes and the last char '\\' and second last char
288 p
= &(buff
->data
[bufnum
- 1]);
289 if (IS_ESC(conf
, p
[0]) && ((bufnum
<= 1) || !IS_ESC(conf
, p
[-1]))) {
299 clear_comments(conf
, buf
);
300 s
= eat_ws(conf
, buf
);
301 if (IS_EOF(conf
, *s
))
302 continue; /* blank line */
307 start
= eat_ws(conf
, s
);
310 end
= eat_alpha_numeric(conf
, ss
);
311 p
= eat_ws(conf
, end
);
313 if (*p
!= '\0' && ss
!= p
) {
317 CONFerr(CONF_F_DEF_LOAD_BIO
,
318 CONF_R_MISSING_CLOSE_SQUARE_BRACKET
);
322 if (!str_copy(conf
, NULL
, §ion
, start
))
324 if ((sv
= _CONF_get_section(conf
, section
)) == NULL
)
325 sv
= _CONF_new_section(conf
, section
);
327 CONFerr(CONF_F_DEF_LOAD_BIO
,
328 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
335 end
= eat_alpha_numeric(conf
, s
);
336 if ((end
[0] == ':') && (end
[1] == ':')) {
341 end
= eat_alpha_numeric(conf
, end
);
343 p
= eat_ws(conf
, end
);
345 CONFerr(CONF_F_DEF_LOAD_BIO
, CONF_R_MISSING_EQUAL_SIGN
);
350 start
= eat_ws(conf
, p
);
351 while (!IS_EOF(conf
, *p
))
354 while ((p
!= start
) && (IS_WS(conf
, *p
)))
359 if (!(v
= (CONF_VALUE
*)OPENSSL_malloc(sizeof(CONF_VALUE
)))) {
360 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
363 if (psection
== NULL
)
365 v
->name
= (char *)OPENSSL_malloc(strlen(pname
) + 1);
367 if (v
->name
== NULL
) {
368 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
371 BUF_strlcpy(v
->name
, pname
, strlen(pname
) + 1);
372 if (!str_copy(conf
, psection
, &(v
->value
), start
))
375 if (strcmp(psection
, section
) != 0) {
376 if ((tv
= _CONF_get_section(conf
, psection
))
378 tv
= _CONF_new_section(conf
, psection
);
380 CONFerr(CONF_F_DEF_LOAD_BIO
,
381 CONF_R_UNABLE_TO_CREATE_NEW_SECTION
);
387 if (_CONF_add_string(conf
, tv
, v
) == 0) {
388 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
392 v
->section
= tv
->section
;
393 if (!sk_CONF_VALUE_push(ts
, v
)) {
394 CONFerr(CONF_F_DEF_LOAD_BIO
, ERR_R_MALLOC_FAILURE
);
397 vv
= (CONF_VALUE
*)lh_insert(conf
->data
, v
);
399 sk_CONF_VALUE_delete_ptr(ts
, vv
);
400 OPENSSL_free(vv
->name
);
401 OPENSSL_free(vv
->value
);
411 OPENSSL_free(section
);
417 OPENSSL_free(section
);
420 BIO_snprintf(btmp
, sizeof btmp
, "%ld", eline
);
421 ERR_add_error_data(2, "line ", btmp
);
422 if ((h
!= conf
->data
) && (conf
->data
!= NULL
)) {
423 CONF_free(conf
->data
);
428 OPENSSL_free(v
->name
);
429 if (v
->value
!= NULL
)
430 OPENSSL_free(v
->value
);
437 static void clear_comments(CONF
*conf
, char *p
)
440 if (IS_FCOMMENT(conf
, *p
)) {
444 if (!IS_WS(conf
, *p
)) {
451 if (IS_COMMENT(conf
, *p
)) {
455 if (IS_DQUOTE(conf
, *p
)) {
456 p
= scan_dquote(conf
, p
);
459 if (IS_QUOTE(conf
, *p
)) {
460 p
= scan_quote(conf
, p
);
463 if (IS_ESC(conf
, *p
)) {
464 p
= scan_esc(conf
, p
);
467 if (IS_EOF(conf
, *p
))
474 static int str_copy(CONF
*conf
, char *section
, char **pto
, char *from
)
476 int q
, r
, rr
= 0, to
= 0, len
= 0;
477 char *s
, *e
, *rp
, *p
, *rrp
, *np
, *cp
, v
;
480 if ((buf
= BUF_MEM_new()) == NULL
)
483 len
= strlen(from
) + 1;
484 if (!BUF_MEM_grow(buf
, len
))
488 if (IS_QUOTE(conf
, *from
)) {
491 while (!IS_EOF(conf
, *from
) && (*from
!= q
)) {
492 if (IS_ESC(conf
, *from
)) {
494 if (IS_EOF(conf
, *from
))
497 buf
->data
[to
++] = *(from
++);
501 } else if (IS_DQUOTE(conf
, *from
)) {
504 while (!IS_EOF(conf
, *from
)) {
506 if (*(from
+ 1) == q
) {
512 buf
->data
[to
++] = *(from
++);
516 } else if (IS_ESC(conf
, *from
)) {
530 } else if (IS_EOF(conf
, *from
))
532 else if (*from
== '$') {
533 /* try to expand it */
547 while (IS_ALPHA_NUMERIC(conf
, *e
))
549 if ((e
[0] == ':') && (e
[1] == ':')) {
556 while (IS_ALPHA_NUMERIC(conf
, *e
))
564 CONFerr(CONF_F_STR_COPY
, CONF_R_NO_CLOSE_BRACE
);
570 * So at this point we have
571 * np which is the start of the name string which is
573 * cp which is the start of the section string which is
575 * e is the 'next point after'.
576 * r and rr are the chars replaced by the '\0'
577 * rp and rrp is where 'r' and 'rr' came from.
579 p
= _CONF_get_string(conf
, cp
, np
);
584 CONFerr(CONF_F_STR_COPY
, CONF_R_VARIABLE_HAS_NO_VALUE
);
587 if (!BUF_MEM_grow_clean(buf
,
588 (strlen(p
) + buf
->length
- (e
- from
)))) {
589 CONFerr(CONF_F_STR_COPY
, ERR_R_MALLOC_FAILURE
);
593 buf
->data
[to
++] = *(p
++);
596 * Since we change the pointer 'from', we also have to change the
597 * perceived length of the string it points at. /RL
603 * In case there were no braces or parenthesis around the
604 * variable reference, we have to put back the character that was
605 * replaced with a '\0'. /RL
609 buf
->data
[to
++] = *(from
++);
611 buf
->data
[to
] = '\0';
623 static char *eat_ws(CONF
*conf
, char *p
)
625 while (IS_WS(conf
, *p
) && (!IS_EOF(conf
, *p
)))
630 static char *eat_alpha_numeric(CONF
*conf
, char *p
)
633 if (IS_ESC(conf
, *p
)) {
634 p
= scan_esc(conf
, p
);
637 if (!IS_ALPHA_NUMERIC_PUNCT(conf
, *p
))
643 static char *scan_quote(CONF
*conf
, char *p
)
648 while (!(IS_EOF(conf
, *p
)) && (*p
!= q
)) {
649 if (IS_ESC(conf
, *p
)) {
651 if (IS_EOF(conf
, *p
))
661 static char *scan_dquote(CONF
*conf
, char *p
)
666 while (!(IS_EOF(conf
, *p
))) {
681 static void dump_value_doall_arg(CONF_VALUE
*a
, BIO
*out
)
684 BIO_printf(out
, "[%s] %s=%s\n", a
->section
, a
->name
, a
->value
);
686 BIO_printf(out
, "[[%s]]\n", a
->section
);
689 static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value
, CONF_VALUE
, BIO
)
691 static int def_dump(const CONF
*conf
, BIO
*out
)
693 lh_CONF_VALUE_doall_arg(conf
->data
, LHASH_DOALL_ARG_FN(dump_value
),
698 static int def_is_number(const CONF
*conf
, char c
)
700 return IS_NUMBER(conf
, c
);
703 static int def_to_int(const CONF
*conf
, char c
)