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 */
63 #include "internal/cryptlib.h"
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 static CONF_METHOD default_method = {
104 static CONF_METHOD WIN32_method = {
117 CONF_METHOD *NCONF_default()
119 return &default_method;
122 CONF_METHOD *NCONF_WIN32()
124 return &WIN32_method;
127 static CONF *def_create(CONF_METHOD *meth)
131 ret = OPENSSL_malloc(sizeof(*ret));
133 if (meth->init(ret) == 0) {
140 static int def_init_default(CONF *conf)
145 conf->meth = &default_method;
146 conf->meth_data = (void *)CONF_type_default;
152 static int def_init_WIN32(CONF *conf)
157 conf->meth = &WIN32_method;
158 conf->meth_data = (void *)CONF_type_win32;
164 static int def_destroy(CONF *conf)
166 if (def_destroy_data(conf)) {
173 static int def_destroy_data(CONF *conf)
177 _CONF_free_data(conf);
181 static int def_load(CONF *conf, const char *name, long *line)
186 #ifdef OPENSSL_SYS_VMS
187 in = BIO_new_file(name, "r");
189 in = BIO_new_file(name, "rb");
192 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
193 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
195 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
199 ret = def_load_bio(conf, in, line);
205 static int def_load_bio(CONF *conf, BIO *in, long *line)
207 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
208 #define CONFBUFSIZE 512
209 int bufnum = 0, i, ii;
210 BUF_MEM *buff = NULL;
214 char btmp[DECIMAL_SIZE(eline) + 1];
215 CONF_VALUE *v = NULL, *tv;
216 CONF_VALUE *sv = NULL;
217 char *section = NULL, *buf;
218 char *start, *psection, *pname;
219 void *h = (void *)(conf->data);
221 if ((buff = BUF_MEM_new()) == NULL) {
222 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
226 section = OPENSSL_malloc(10);
227 if (section == NULL) {
228 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
231 BUF_strlcpy(section, "default", 10);
233 if (_CONF_new_data(conf) == 0) {
234 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
238 sv = _CONF_new_section(conf, section);
240 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
247 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
248 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
251 p = &(buff->data[bufnum]);
253 BIO_gets(in, p, CONFBUFSIZE - 1);
254 p[CONFBUFSIZE - 1] = '\0';
256 if (i == 0 && !again)
260 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
266 * we removed some trailing stuff so there is a new line on the end.
269 again = 1; /* long line */
272 eline++; /* another input line */
275 /* we now have a line with trailing \r\n removed */
277 /* i is the number of bytes */
281 /* check for line continuation */
284 * If we have bytes and the last char '\\' and second last char
287 p = &(buff->data[bufnum - 1]);
288 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
298 clear_comments(conf, buf);
299 s = eat_ws(conf, buf);
300 if (IS_EOF(conf, *s))
301 continue; /* blank line */
306 start = eat_ws(conf, s);
309 end = eat_alpha_numeric(conf, ss);
310 p = eat_ws(conf, end);
312 if (*p != '\0' && ss != p) {
316 CONFerr(CONF_F_DEF_LOAD_BIO,
317 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
321 if (!str_copy(conf, NULL, §ion, start))
323 if ((sv = _CONF_get_section(conf, section)) == NULL)
324 sv = _CONF_new_section(conf, section);
326 CONFerr(CONF_F_DEF_LOAD_BIO,
327 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
334 end = eat_alpha_numeric(conf, s);
335 if ((end[0] == ':') && (end[1] == ':')) {
340 end = eat_alpha_numeric(conf, end);
342 p = eat_ws(conf, end);
344 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
349 start = eat_ws(conf, p);
350 while (!IS_EOF(conf, *p))
353 while ((p != start) && (IS_WS(conf, *p)))
358 if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
359 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
362 if (psection == NULL)
364 v->name = OPENSSL_malloc(strlen(pname) + 1);
366 if (v->name == NULL) {
367 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
370 BUF_strlcpy(v->name, pname, strlen(pname) + 1);
371 if (!str_copy(conf, psection, &(v->value), start))
374 if (strcmp(psection, section) != 0) {
375 if ((tv = _CONF_get_section(conf, psection))
377 tv = _CONF_new_section(conf, psection);
379 CONFerr(CONF_F_DEF_LOAD_BIO,
380 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
385 if (_CONF_add_string(conf, tv, v) == 0) {
386 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
393 OPENSSL_free(section);
397 OPENSSL_free(section);
400 BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
401 ERR_add_error_data(2, "line ", btmp);
402 if (h != conf->data) {
403 CONF_free(conf->data);
407 OPENSSL_free(v->name);
408 OPENSSL_free(v->value);
414 static void clear_comments(CONF *conf, char *p)
417 if (IS_FCOMMENT(conf, *p)) {
421 if (!IS_WS(conf, *p)) {
428 if (IS_COMMENT(conf, *p)) {
432 if (IS_DQUOTE(conf, *p)) {
433 p = scan_dquote(conf, p);
436 if (IS_QUOTE(conf, *p)) {
437 p = scan_quote(conf, p);
440 if (IS_ESC(conf, *p)) {
441 p = scan_esc(conf, p);
444 if (IS_EOF(conf, *p))
451 static int str_copy(CONF *conf, char *section, char **pto, char *from)
453 int q, r, rr = 0, to = 0, len = 0;
454 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
457 if ((buf = BUF_MEM_new()) == NULL)
460 len = strlen(from) + 1;
461 if (!BUF_MEM_grow(buf, len))
465 if (IS_QUOTE(conf, *from)) {
468 while (!IS_EOF(conf, *from) && (*from != q)) {
469 if (IS_ESC(conf, *from)) {
471 if (IS_EOF(conf, *from))
474 buf->data[to++] = *(from++);
478 } else if (IS_DQUOTE(conf, *from)) {
481 while (!IS_EOF(conf, *from)) {
483 if (*(from + 1) == q) {
489 buf->data[to++] = *(from++);
493 } else if (IS_ESC(conf, *from)) {
507 } else if (IS_EOF(conf, *from))
509 else if (*from == '$') {
510 /* try to expand it */
524 while (IS_ALPHA_NUMERIC(conf, *e))
526 if ((e[0] == ':') && (e[1] == ':')) {
533 while (IS_ALPHA_NUMERIC(conf, *e))
541 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
547 * So at this point we have
548 * np which is the start of the name string which is
550 * cp which is the start of the section string which is
552 * e is the 'next point after'.
553 * r and rr are the chars replaced by the '\0'
554 * rp and rrp is where 'r' and 'rr' came from.
556 p = _CONF_get_string(conf, cp, np);
561 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
564 if (!BUF_MEM_grow_clean(buf,
565 (strlen(p) + buf->length - (e - from)))) {
566 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
570 buf->data[to++] = *(p++);
573 * Since we change the pointer 'from', we also have to change the
574 * perceived length of the string it points at. /RL
580 * In case there were no braces or parenthesis around the
581 * variable reference, we have to put back the character that was
582 * replaced with a '\0'. /RL
586 buf->data[to++] = *(from++);
588 buf->data[to] = '\0';
598 static char *eat_ws(CONF *conf, char *p)
600 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
605 static char *eat_alpha_numeric(CONF *conf, char *p)
608 if (IS_ESC(conf, *p)) {
609 p = scan_esc(conf, p);
612 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
618 static char *scan_quote(CONF *conf, char *p)
623 while (!(IS_EOF(conf, *p)) && (*p != q)) {
624 if (IS_ESC(conf, *p)) {
626 if (IS_EOF(conf, *p))
636 static char *scan_dquote(CONF *conf, char *p)
641 while (!(IS_EOF(conf, *p))) {
656 static void dump_value_doall_arg(CONF_VALUE *a, BIO *out)
659 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
661 BIO_printf(out, "[[%s]]\n", a->section);
664 static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE, BIO)
666 static int def_dump(const CONF *conf, BIO *out)
668 lh_CONF_VALUE_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value),
673 static int def_is_number(const CONF *conf, char c)
675 return IS_NUMBER(conf, c);
678 static int def_to_int(const CONF *conf, char c)