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>
73 * The maximum length we can grow a value to after variable expansion. 64k
74 * should be more than enough for all reasonable uses.
76 #define MAX_CONF_VALUE_LENGTH 65536
78 static char *eat_ws(CONF *conf, char *p);
79 static char *eat_alpha_numeric(CONF *conf, char *p);
80 static void clear_comments(CONF *conf, char *p);
81 static int str_copy(CONF *conf, char *section, char **to, char *from);
82 static char *scan_quote(CONF *conf, char *p);
83 static char *scan_dquote(CONF *conf, char *p);
84 #define scan_esc(conf,p) (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
86 static CONF *def_create(CONF_METHOD *meth);
87 static int def_init_default(CONF *conf);
88 static int def_init_WIN32(CONF *conf);
89 static int def_destroy(CONF *conf);
90 static int def_destroy_data(CONF *conf);
91 static int def_load(CONF *conf, const char *name, long *eline);
92 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
93 static int def_dump(const CONF *conf, BIO *bp);
94 static int def_is_number(const CONF *conf, char c);
95 static int def_to_int(const CONF *conf, char c);
97 const char CONF_def_version[] = "CONF_def" OPENSSL_VERSION_PTEXT;
99 static CONF_METHOD default_method = {
112 static CONF_METHOD WIN32_method = {
125 CONF_METHOD *NCONF_default()
127 return &default_method;
130 CONF_METHOD *NCONF_WIN32()
132 return &WIN32_method;
135 static CONF *def_create(CONF_METHOD *meth)
139 ret = OPENSSL_malloc(sizeof(CONF) + sizeof(unsigned short *));
141 if (meth->init(ret) == 0) {
148 static int def_init_default(CONF *conf)
153 conf->meth = &default_method;
154 conf->meth_data = CONF_type_default;
160 static int def_init_WIN32(CONF *conf)
165 conf->meth = &WIN32_method;
166 conf->meth_data = (void *)CONF_type_win32;
172 static int def_destroy(CONF *conf)
174 if (def_destroy_data(conf)) {
181 static int def_destroy_data(CONF *conf)
185 _CONF_free_data(conf);
189 static int def_load(CONF *conf, const char *name, long *line)
194 #ifdef OPENSSL_SYS_VMS
195 in = BIO_new_file(name, "r");
197 in = BIO_new_file(name, "rb");
200 if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
201 CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
203 CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
207 ret = def_load_bio(conf, in, line);
213 static int def_load_bio(CONF *conf, BIO *in, long *line)
215 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
216 #define CONFBUFSIZE 512
217 int bufnum = 0, i, ii;
218 BUF_MEM *buff = NULL;
222 char btmp[DECIMAL_SIZE(eline) + 1];
223 CONF_VALUE *v = NULL, *tv;
224 CONF_VALUE *sv = NULL;
225 char *section = NULL, *buf;
226 char *start, *psection, *pname;
227 void *h = (void *)(conf->data);
229 if ((buff = BUF_MEM_new()) == NULL) {
230 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
234 section = BUF_strdup("default");
235 if (section == NULL) {
236 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
240 if (_CONF_new_data(conf) == 0) {
241 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
245 sv = _CONF_new_section(conf, section);
247 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
254 if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
255 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
258 p = &(buff->data[bufnum]);
260 BIO_gets(in, p, CONFBUFSIZE - 1);
261 p[CONFBUFSIZE - 1] = '\0';
263 if (i == 0 && !again)
267 if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
273 * we removed some trailing stuff so there is a new line on the end.
276 again = 1; /* long line */
279 eline++; /* another input line */
282 /* we now have a line with trailing \r\n removed */
284 /* i is the number of bytes */
288 /* check for line continuation */
291 * If we have bytes and the last char '\\' and second last char
294 p = &(buff->data[bufnum - 1]);
295 if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
305 clear_comments(conf, buf);
306 s = eat_ws(conf, buf);
307 if (IS_EOF(conf, *s))
308 continue; /* blank line */
313 start = eat_ws(conf, s);
316 end = eat_alpha_numeric(conf, ss);
317 p = eat_ws(conf, end);
319 if (*p != '\0' && ss != p) {
323 CONFerr(CONF_F_DEF_LOAD_BIO,
324 CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
328 if (!str_copy(conf, NULL, §ion, start))
330 if ((sv = _CONF_get_section(conf, section)) == NULL)
331 sv = _CONF_new_section(conf, section);
333 CONFerr(CONF_F_DEF_LOAD_BIO,
334 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
341 end = eat_alpha_numeric(conf, s);
342 if ((end[0] == ':') && (end[1] == ':')) {
347 end = eat_alpha_numeric(conf, end);
349 p = eat_ws(conf, end);
351 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
356 start = eat_ws(conf, p);
357 while (!IS_EOF(conf, *p))
360 while ((p != start) && (IS_WS(conf, *p)))
365 if (!(v = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) {
366 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
369 if (psection == NULL)
371 v->name = (char *)OPENSSL_malloc(strlen(pname) + 1);
373 if (v->name == NULL) {
374 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
377 BUF_strlcpy(v->name, pname, strlen(pname) + 1);
378 if (!str_copy(conf, psection, &(v->value), start))
381 if (strcmp(psection, section) != 0) {
382 if ((tv = _CONF_get_section(conf, psection))
384 tv = _CONF_new_section(conf, psection);
386 CONFerr(CONF_F_DEF_LOAD_BIO,
387 CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
393 if (_CONF_add_string(conf, tv, v) == 0) {
394 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
398 v->section = tv->section;
399 if (!sk_CONF_VALUE_push(ts, v)) {
400 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
403 vv = (CONF_VALUE *)lh_insert(conf->data, v);
405 sk_CONF_VALUE_delete_ptr(ts, vv);
406 OPENSSL_free(vv->name);
407 OPENSSL_free(vv->value);
417 OPENSSL_free(section);
423 OPENSSL_free(section);
426 BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
427 ERR_add_error_data(2, "line ", btmp);
428 if ((h != conf->data) && (conf->data != NULL)) {
429 CONF_free(conf->data);
434 OPENSSL_free(v->name);
435 if (v->value != NULL)
436 OPENSSL_free(v->value);
443 static void clear_comments(CONF *conf, char *p)
446 if (IS_FCOMMENT(conf, *p)) {
450 if (!IS_WS(conf, *p)) {
457 if (IS_COMMENT(conf, *p)) {
461 if (IS_DQUOTE(conf, *p)) {
462 p = scan_dquote(conf, p);
465 if (IS_QUOTE(conf, *p)) {
466 p = scan_quote(conf, p);
469 if (IS_ESC(conf, *p)) {
470 p = scan_esc(conf, p);
473 if (IS_EOF(conf, *p))
480 static int str_copy(CONF *conf, char *section, char **pto, char *from)
482 int q, r, rr = 0, to = 0, len = 0;
483 char *s, *e, *rp, *p, *rrp, *np, *cp, v;
486 if ((buf = BUF_MEM_new()) == NULL)
489 len = strlen(from) + 1;
490 if (!BUF_MEM_grow(buf, len))
494 if (IS_QUOTE(conf, *from)) {
497 while (!IS_EOF(conf, *from) && (*from != q)) {
498 if (IS_ESC(conf, *from)) {
500 if (IS_EOF(conf, *from))
503 buf->data[to++] = *(from++);
507 } else if (IS_DQUOTE(conf, *from)) {
510 while (!IS_EOF(conf, *from)) {
512 if (*(from + 1) == q) {
518 buf->data[to++] = *(from++);
522 } else if (IS_ESC(conf, *from)) {
536 } else if (IS_EOF(conf, *from))
538 else if (*from == '$') {
541 /* try to expand it */
555 while (IS_ALPHA_NUMERIC(conf, *e))
557 if ((e[0] == ':') && (e[1] == ':')) {
564 while (IS_ALPHA_NUMERIC(conf, *e))
572 CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
578 * So at this point we have
579 * np which is the start of the name string which is
581 * cp which is the start of the section string which is
583 * e is the 'next point after'.
584 * r and rr are the chars replaced by the '\0'
585 * rp and rrp is where 'r' and 'rr' came from.
587 p = _CONF_get_string(conf, cp, np);
592 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
595 newsize = strlen(p) + buf->length - (e - from);
596 if (newsize > MAX_CONF_VALUE_LENGTH) {
597 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
600 if (!BUF_MEM_grow_clean(buf, newsize)) {
601 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
605 buf->data[to++] = *(p++);
608 * Since we change the pointer 'from', we also have to change the
609 * perceived length of the string it points at. /RL
615 * In case there were no braces or parenthesis around the
616 * variable reference, we have to put back the character that was
617 * replaced with a '\0'. /RL
621 buf->data[to++] = *(from++);
623 buf->data[to] = '\0';
635 static char *eat_ws(CONF *conf, char *p)
637 while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
642 static char *eat_alpha_numeric(CONF *conf, char *p)
645 if (IS_ESC(conf, *p)) {
646 p = scan_esc(conf, p);
649 if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p))
655 static char *scan_quote(CONF *conf, char *p)
660 while (!(IS_EOF(conf, *p)) && (*p != q)) {
661 if (IS_ESC(conf, *p)) {
663 if (IS_EOF(conf, *p))
673 static char *scan_dquote(CONF *conf, char *p)
678 while (!(IS_EOF(conf, *p))) {
693 static void dump_value_doall_arg(CONF_VALUE *a, BIO *out)
696 BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
698 BIO_printf(out, "[[%s]]\n", a->section);
701 static IMPLEMENT_LHASH_DOALL_ARG_FN(dump_value, CONF_VALUE, BIO)
703 static int def_dump(const CONF *conf, BIO *out)
705 lh_CONF_VALUE_doall_arg(conf->data, LHASH_DOALL_ARG_FN(dump_value),
710 static int def_is_number(const CONF *conf, char c)
712 return IS_NUMBER(conf, c);
715 static int def_to_int(const CONF *conf, char c)