Do not prepend $OPENSSL_CONF_INCLUDE to absolute include paths
[openssl.git] / crypto / conf / conf_def.c
1 /*
2  * Copyright 1995-2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 /* Part of the code in here was originally in conf.c, which is now removed */
11
12 #include <stdio.h>
13 #include <string.h>
14 #ifdef __TANDEM
15 # include <strings.h> /* strcasecmp */
16 #endif
17 #include "internal/cryptlib.h"
18 #include "internal/o_dir.h"
19 #include <openssl/lhash.h>
20 #include <openssl/conf.h>
21 #include <openssl/conf_api.h>
22 #include "conf_def.h"
23 #include <openssl/buffer.h>
24 #include <openssl/err.h>
25 #ifndef OPENSSL_NO_POSIX_IO
26 # include <sys/stat.h>
27 # ifdef _WIN32
28 #  define stat    _stat
29 #  define strcasecmp _stricmp
30 # endif
31 #endif
32
33 #ifndef S_ISDIR
34 # define S_ISDIR(a) (((a) & S_IFMT) == S_IFDIR)
35 #endif
36
37 /*
38  * The maximum length we can grow a value to after variable expansion. 64k
39  * should be more than enough for all reasonable uses.
40  */
41 #define MAX_CONF_VALUE_LENGTH       65536
42
43 static int is_keytype(const CONF *conf, char c, unsigned short type);
44 static char *eat_ws(CONF *conf, char *p);
45 static void trim_ws(CONF *conf, char *start);
46 static char *eat_alpha_numeric(CONF *conf, char *p);
47 static void clear_comments(CONF *conf, char *p);
48 static int str_copy(CONF *conf, char *section, char **to, char *from);
49 static char *scan_quote(CONF *conf, char *p);
50 static char *scan_dquote(CONF *conf, char *p);
51 #define scan_esc(conf,p)        (((IS_EOF((conf),(p)[1]))?((p)+1):((p)+2)))
52 #ifndef OPENSSL_NO_POSIX_IO
53 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
54                             char **dirpath);
55 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx);
56 #endif
57
58 static CONF *def_create(CONF_METHOD *meth);
59 static int def_init_default(CONF *conf);
60 #ifndef OPENSSL_NO_DEPRECATED_3_0
61 static int def_init_WIN32(CONF *conf);
62 #endif
63 static int def_destroy(CONF *conf);
64 static int def_destroy_data(CONF *conf);
65 static int def_load(CONF *conf, const char *name, long *eline);
66 static int def_load_bio(CONF *conf, BIO *bp, long *eline);
67 static int def_dump(const CONF *conf, BIO *bp);
68 static int def_is_number(const CONF *conf, char c);
69 static int def_to_int(const CONF *conf, char c);
70
71 static CONF_METHOD default_method = {
72     "OpenSSL default",
73     def_create,
74     def_init_default,
75     def_destroy,
76     def_destroy_data,
77     def_load_bio,
78     def_dump,
79     def_is_number,
80     def_to_int,
81     def_load
82 };
83
84 CONF_METHOD *NCONF_default(void)
85 {
86     return &default_method;
87 }
88
89 #ifndef OPENSSL_NO_DEPRECATED_3_0
90 static CONF_METHOD WIN32_method = {
91     "WIN32",
92     def_create,
93     def_init_WIN32,
94     def_destroy,
95     def_destroy_data,
96     def_load_bio,
97     def_dump,
98     def_is_number,
99     def_to_int,
100     def_load
101 };
102
103 CONF_METHOD *NCONF_WIN32(void)
104 {
105     return &WIN32_method;
106 }
107 #endif
108
109 static CONF *def_create(CONF_METHOD *meth)
110 {
111     CONF *ret;
112
113     ret = OPENSSL_malloc(sizeof(*ret));
114     if (ret != NULL)
115         if (meth->init(ret) == 0) {
116             OPENSSL_free(ret);
117             ret = NULL;
118         }
119     return ret;
120 }
121
122 static int def_init_default(CONF *conf)
123 {
124     if (conf == NULL)
125         return 0;
126
127     memset(conf, 0, sizeof(*conf));
128     conf->meth = &default_method;
129     conf->meth_data = (void *)CONF_type_default;
130
131     return 1;
132 }
133
134 #ifndef OPENSSL_NO_DEPRECATED_3_0
135 static int def_init_WIN32(CONF *conf)
136 {
137     if (conf == NULL)
138         return 0;
139
140     memset(conf, 0, sizeof(*conf));
141     conf->meth = &WIN32_method;
142     conf->meth_data = (void *)CONF_type_win32;
143
144     return 1;
145 }
146 #endif
147
148 static int def_destroy(CONF *conf)
149 {
150     if (def_destroy_data(conf)) {
151         OPENSSL_free(conf);
152         return 1;
153     }
154     return 0;
155 }
156
157 static int def_destroy_data(CONF *conf)
158 {
159     if (conf == NULL)
160         return 0;
161     _CONF_free_data(conf);
162     return 1;
163 }
164
165 static int def_load(CONF *conf, const char *name, long *line)
166 {
167     int ret;
168     BIO *in = NULL;
169
170 #ifdef OPENSSL_SYS_VMS
171     in = BIO_new_file(name, "r");
172 #else
173     in = BIO_new_file(name, "rb");
174 #endif
175     if (in == NULL) {
176         if (ERR_GET_REASON(ERR_peek_last_error()) == BIO_R_NO_SUCH_FILE)
177             CONFerr(CONF_F_DEF_LOAD, CONF_R_NO_SUCH_FILE);
178         else
179             CONFerr(CONF_F_DEF_LOAD, ERR_R_SYS_LIB);
180         return 0;
181     }
182
183     ret = def_load_bio(conf, in, line);
184     BIO_free(in);
185
186     return ret;
187 }
188
189 static int def_load_bio(CONF *conf, BIO *in, long *line)
190 {
191 /* The macro BUFSIZE conflicts with a system macro in VxWorks */
192 #define CONFBUFSIZE     512
193     int bufnum = 0, i, ii;
194     BUF_MEM *buff = NULL;
195     char *s, *p, *end;
196     int again;
197     long eline = 0;
198     char btmp[DECIMAL_SIZE(eline) + 1];
199     CONF_VALUE *v = NULL, *tv;
200     CONF_VALUE *sv = NULL;
201     char *section = NULL, *buf;
202     char *start, *psection, *pname;
203     void *h = (void *)(conf->data);
204     STACK_OF(BIO) *biosk = NULL;
205 #ifndef OPENSSL_NO_POSIX_IO
206     char *dirpath = NULL;
207     OPENSSL_DIR_CTX *dirctx = NULL;
208 #endif
209
210     if ((buff = BUF_MEM_new()) == NULL) {
211         CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
212         goto err;
213     }
214
215     section = OPENSSL_strdup("default");
216     if (section == NULL) {
217         CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
218         goto err;
219     }
220
221     if (_CONF_new_data(conf) == 0) {
222         CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
223         goto err;
224     }
225
226     sv = _CONF_new_section(conf, section);
227     if (sv == NULL) {
228         CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
229         goto err;
230     }
231
232     bufnum = 0;
233     again = 0;
234     for (;;) {
235         if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
236             CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_BUF_LIB);
237             goto err;
238         }
239         p = &(buff->data[bufnum]);
240         *p = '\0';
241  read_retry:
242         BIO_gets(in, p, CONFBUFSIZE - 1);
243         p[CONFBUFSIZE - 1] = '\0';
244         ii = i = strlen(p);
245         if (i == 0 && !again) {
246             /* the currently processed BIO is at EOF */
247             BIO *parent;
248
249 #ifndef OPENSSL_NO_POSIX_IO
250             /* continue processing with the next file from directory */
251             if (dirctx != NULL) {
252                 BIO *next;
253
254                 if ((next = get_next_file(dirpath, &dirctx)) != NULL) {
255                     BIO_vfree(in);
256                     in = next;
257                     goto read_retry;
258                 } else {
259                     OPENSSL_free(dirpath);
260                     dirpath = NULL;
261                 }
262             }
263 #endif
264             /* no more files in directory, continue with processing parent */
265             if ((parent = sk_BIO_pop(biosk)) == NULL) {
266                 /* everything processed get out of the loop */
267                 break;
268             } else {
269                 BIO_vfree(in);
270                 in = parent;
271                 goto read_retry;
272             }
273         }
274         again = 0;
275         while (i > 0) {
276             if ((p[i - 1] != '\r') && (p[i - 1] != '\n'))
277                 break;
278             else
279                 i--;
280         }
281         /*
282          * we removed some trailing stuff so there is a new line on the end.
283          */
284         if (ii && i == ii)
285             again = 1;          /* long line */
286         else {
287             p[i] = '\0';
288             eline++;            /* another input line */
289         }
290
291         /* we now have a line with trailing \r\n removed */
292
293         /* i is the number of bytes */
294         bufnum += i;
295
296         v = NULL;
297         /* check for line continuation */
298         if (bufnum >= 1) {
299             /*
300              * If we have bytes and the last char '\\' and second last char
301              * is not '\\'
302              */
303             p = &(buff->data[bufnum - 1]);
304             if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
305                 bufnum--;
306                 again = 1;
307             }
308         }
309         if (again)
310             continue;
311         bufnum = 0;
312         buf = buff->data;
313
314         clear_comments(conf, buf);
315         s = eat_ws(conf, buf);
316         if (IS_EOF(conf, *s))
317             continue;           /* blank line */
318         if (*s == '[') {
319             char *ss;
320
321             s++;
322             start = eat_ws(conf, s);
323             ss = start;
324  again:
325             end = eat_alpha_numeric(conf, ss);
326             p = eat_ws(conf, end);
327             if (*p != ']') {
328                 if (*p != '\0' && ss != p) {
329                     ss = p;
330                     goto again;
331                 }
332                 CONFerr(CONF_F_DEF_LOAD_BIO,
333                         CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
334                 goto err;
335             }
336             *end = '\0';
337             if (!str_copy(conf, NULL, &section, start))
338                 goto err;
339             if ((sv = _CONF_get_section(conf, section)) == NULL)
340                 sv = _CONF_new_section(conf, section);
341             if (sv == NULL) {
342                 CONFerr(CONF_F_DEF_LOAD_BIO,
343                         CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
344                 goto err;
345             }
346             continue;
347         } else {
348             pname = s;
349             end = eat_alpha_numeric(conf, s);
350             if ((end[0] == ':') && (end[1] == ':')) {
351                 *end = '\0';
352                 end += 2;
353                 psection = pname;
354                 pname = end;
355                 end = eat_alpha_numeric(conf, end);
356             } else {
357                 psection = section;
358             }
359             p = eat_ws(conf, end);
360             if (strncmp(pname, ".pragma", 7) == 0
361                 && (p != pname + 7 || *p == '=')) {
362                 char *pval;
363
364                 if (*p == '=') {
365                     p++;
366                     p = eat_ws(conf, p);
367                 }
368                 trim_ws(conf, p);
369
370                 /* Pragma values take the form keyword:value */
371                 pval = strchr(p, ':');
372                 if (pval == NULL || pval == p || pval[1] == '\0') {
373                     CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_INVALID_PRAGMA);
374                     goto err;
375                 }
376
377                 *pval++ = '\0';
378                 trim_ws(conf, p);
379                 pval = eat_ws(conf, pval);
380
381                 /*
382                  * Known pragmas:
383                  *
384                  * dollarid     takes "on", "true or "off", "false"
385                  */
386                 if (strcmp(p, "dollarid") == 0) {
387                     if (strcmp(pval, "on") == 0
388                         || strcmp(pval, "true") == 0) {
389                         conf->flag_dollarid = 1;
390                     } else if (strcmp(pval, "off") == 0
391                                || strcmp(pval, "false") == 0) {
392                         conf->flag_dollarid = 0;
393                     } else {
394                         CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_INVALID_PRAGMA);
395                         goto err;
396                     }
397                 }
398                 /*
399                  * We *ignore* any unknown pragma.
400                  */
401                 continue;
402             } else if (strncmp(pname, ".include", 8) == 0
403                 && (p != pname + 8 || *p == '=')) {
404                 char *include = NULL;
405                 BIO *next;
406                 const char *include_dir = ossl_safe_getenv("OPENSSL_CONF_INCLUDE");
407                 char *include_path = NULL;
408
409                 if (*p == '=') {
410                     p++;
411                     p = eat_ws(conf, p);
412                 }
413                 trim_ws(conf, p);
414                 if (!str_copy(conf, psection, &include, p))
415                     goto err;
416
417                 if (include_dir != NULL && !ossl_is_absolute_path(include)) {
418                     size_t newlen = strlen(include_dir) + strlen(include) + 2;
419
420                     include_path = OPENSSL_malloc(newlen);
421                     if (include_path == NULL) {
422                         CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
423                         OPENSSL_free(include);
424                         goto err;
425                     }
426
427                     OPENSSL_strlcpy(include_path, include_dir, newlen);
428                     if (!ossl_ends_with_dirsep(include_path))
429                         OPENSSL_strlcat(include_path, "/", newlen);
430                     OPENSSL_strlcat(include_path, include, newlen);
431                     OPENSSL_free(include);
432                 } else {
433                     include_path = include;
434                 }
435
436                 /* get the BIO of the included file */
437 #ifndef OPENSSL_NO_POSIX_IO
438                 next = process_include(include_path, &dirctx, &dirpath);
439                 if (include_path != dirpath) {
440                     /* dirpath will contain include in case of a directory */
441                     OPENSSL_free(include_path);
442                 }
443 #else
444                 next = BIO_new_file(include_path, "r");
445                 OPENSSL_free(include_path);
446 #endif
447
448                 if (next != NULL) {
449                     /* push the currently processing BIO onto stack */
450                     if (biosk == NULL) {
451                         if ((biosk = sk_BIO_new_null()) == NULL) {
452                             CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
453                             BIO_free(next);
454                             goto err;
455                         }
456                     }
457                     if (!sk_BIO_push(biosk, in)) {
458                         CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
459                         BIO_free(next);
460                         goto err;
461                     }
462                     /* continue with reading from the included BIO */
463                     in = next;
464                 }
465                 continue;
466             } else if (*p != '=') {
467                 CONFerr(CONF_F_DEF_LOAD_BIO, CONF_R_MISSING_EQUAL_SIGN);
468                 ERR_add_error_data(2, "HERE-->", p);
469                 goto err;
470             }
471             *end = '\0';
472             p++;
473             start = eat_ws(conf, p);
474             trim_ws(conf, start);
475
476             if ((v = OPENSSL_malloc(sizeof(*v))) == NULL) {
477                 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
478                 goto err;
479             }
480             v->name = OPENSSL_strdup(pname);
481             v->value = NULL;
482             if (v->name == NULL) {
483                 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
484                 goto err;
485             }
486             if (!str_copy(conf, psection, &(v->value), start))
487                 goto err;
488
489             if (strcmp(psection, section) != 0) {
490                 if ((tv = _CONF_get_section(conf, psection))
491                     == NULL)
492                     tv = _CONF_new_section(conf, psection);
493                 if (tv == NULL) {
494                     CONFerr(CONF_F_DEF_LOAD_BIO,
495                             CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
496                     goto err;
497                 }
498             } else
499                 tv = sv;
500             if (_CONF_add_string(conf, tv, v) == 0) {
501                 CONFerr(CONF_F_DEF_LOAD_BIO, ERR_R_MALLOC_FAILURE);
502                 goto err;
503             }
504             v = NULL;
505         }
506     }
507     BUF_MEM_free(buff);
508     OPENSSL_free(section);
509     /*
510      * No need to pop, since we only get here if the stack is empty.
511      * If this causes a BIO leak, THE ISSUE IS SOMEWHERE ELSE!
512      */
513     sk_BIO_free(biosk);
514     return 1;
515  err:
516     BUF_MEM_free(buff);
517     OPENSSL_free(section);
518     /*
519      * Since |in| is the first element of the stack and should NOT be freed
520      * here, we cannot use sk_BIO_pop_free().  Instead, we pop and free one
521      * BIO at a time, making sure that the last one popped isn't.
522      */
523     while (sk_BIO_num(biosk) > 0) {
524         BIO *popped = sk_BIO_pop(biosk);
525         BIO_vfree(in);
526         in = popped;
527     }
528     sk_BIO_free(biosk);
529 #ifndef OPENSSL_NO_POSIX_IO
530     OPENSSL_free(dirpath);
531     if (dirctx != NULL)
532         OPENSSL_DIR_end(&dirctx);
533 #endif
534     if (line != NULL)
535         *line = eline;
536     BIO_snprintf(btmp, sizeof(btmp), "%ld", eline);
537     ERR_add_error_data(2, "line ", btmp);
538     if (h != conf->data) {
539         CONF_free(conf->data);
540         conf->data = NULL;
541     }
542     if (v != NULL) {
543         OPENSSL_free(v->name);
544         OPENSSL_free(v->value);
545         OPENSSL_free(v);
546     }
547     return 0;
548 }
549
550 static void clear_comments(CONF *conf, char *p)
551 {
552     for (;;) {
553         if (IS_FCOMMENT(conf, *p)) {
554             *p = '\0';
555             return;
556         }
557         if (!IS_WS(conf, *p)) {
558             break;
559         }
560         p++;
561     }
562
563     for (;;) {
564         if (IS_COMMENT(conf, *p)) {
565             *p = '\0';
566             return;
567         }
568         if (IS_DQUOTE(conf, *p)) {
569             p = scan_dquote(conf, p);
570             continue;
571         }
572         if (IS_QUOTE(conf, *p)) {
573             p = scan_quote(conf, p);
574             continue;
575         }
576         if (IS_ESC(conf, *p)) {
577             p = scan_esc(conf, p);
578             continue;
579         }
580         if (IS_EOF(conf, *p))
581             return;
582         else
583             p++;
584     }
585 }
586
587 static int str_copy(CONF *conf, char *section, char **pto, char *from)
588 {
589     int q, r, rr = 0, to = 0, len = 0;
590     char *s, *e, *rp, *p, *rrp, *np, *cp, v;
591     BUF_MEM *buf;
592
593     if ((buf = BUF_MEM_new()) == NULL)
594         return 0;
595
596     len = strlen(from) + 1;
597     if (!BUF_MEM_grow(buf, len))
598         goto err;
599
600     for (;;) {
601         if (IS_QUOTE(conf, *from)) {
602             q = *from;
603             from++;
604             while (!IS_EOF(conf, *from) && (*from != q)) {
605                 if (IS_ESC(conf, *from)) {
606                     from++;
607                     if (IS_EOF(conf, *from))
608                         break;
609                 }
610                 buf->data[to++] = *(from++);
611             }
612             if (*from == q)
613                 from++;
614         } else if (IS_DQUOTE(conf, *from)) {
615             q = *from;
616             from++;
617             while (!IS_EOF(conf, *from)) {
618                 if (*from == q) {
619                     if (*(from + 1) == q) {
620                         from++;
621                     } else {
622                         break;
623                     }
624                 }
625                 buf->data[to++] = *(from++);
626             }
627             if (*from == q)
628                 from++;
629         } else if (IS_ESC(conf, *from)) {
630             from++;
631             v = *(from++);
632             if (IS_EOF(conf, v))
633                 break;
634             else if (v == 'r')
635                 v = '\r';
636             else if (v == 'n')
637                 v = '\n';
638             else if (v == 'b')
639                 v = '\b';
640             else if (v == 't')
641                 v = '\t';
642             buf->data[to++] = v;
643         } else if (IS_EOF(conf, *from))
644             break;
645         else if (*from == '$'
646                  && (!conf->flag_dollarid
647                      || from[1] == '{'
648                      || from[1] == '(')) {
649             size_t newsize;
650
651             /* try to expand it */
652             rrp = NULL;
653             s = &(from[1]);
654             if (*s == '{')
655                 q = '}';
656             else if (*s == '(')
657                 q = ')';
658             else
659                 q = 0;
660
661             if (q)
662                 s++;
663             cp = section;
664             e = np = s;
665             while (IS_ALNUM(conf, *e)
666                    || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
667                 e++;
668             if ((e[0] == ':') && (e[1] == ':')) {
669                 cp = np;
670                 rrp = e;
671                 rr = *e;
672                 *rrp = '\0';
673                 e += 2;
674                 np = e;
675                 while (IS_ALNUM(conf, *e)
676                        || (conf->flag_dollarid && IS_DOLLAR(conf, *e)))
677                     e++;
678             }
679             r = *e;
680             *e = '\0';
681             rp = e;
682             if (q) {
683                 if (r != q) {
684                     CONFerr(CONF_F_STR_COPY, CONF_R_NO_CLOSE_BRACE);
685                     goto err;
686                 }
687                 e++;
688             }
689             /*-
690              * So at this point we have
691              * np which is the start of the name string which is
692              *   '\0' terminated.
693              * cp which is the start of the section string which is
694              *   '\0' terminated.
695              * e is the 'next point after'.
696              * r and rr are the chars replaced by the '\0'
697              * rp and rrp is where 'r' and 'rr' came from.
698              */
699             p = _CONF_get_string(conf, cp, np);
700             if (rrp != NULL)
701                 *rrp = rr;
702             *rp = r;
703             if (p == NULL) {
704                 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_HAS_NO_VALUE);
705                 goto err;
706             }
707             newsize = strlen(p) + buf->length - (e - from);
708             if (newsize > MAX_CONF_VALUE_LENGTH) {
709                 CONFerr(CONF_F_STR_COPY, CONF_R_VARIABLE_EXPANSION_TOO_LONG);
710                 goto err;
711             }
712             if (!BUF_MEM_grow_clean(buf, newsize)) {
713                 CONFerr(CONF_F_STR_COPY, ERR_R_MALLOC_FAILURE);
714                 goto err;
715             }
716             while (*p)
717                 buf->data[to++] = *(p++);
718
719             /*
720              * Since we change the pointer 'from', we also have to change the
721              * perceived length of the string it points at.  /RL
722              */
723             len -= e - from;
724             from = e;
725
726             /*
727              * In case there were no braces or parenthesis around the
728              * variable reference, we have to put back the character that was
729              * replaced with a '\0'.  /RL
730              */
731             *rp = r;
732         } else
733             buf->data[to++] = *(from++);
734     }
735     buf->data[to] = '\0';
736     OPENSSL_free(*pto);
737     *pto = buf->data;
738     OPENSSL_free(buf);
739     return 1;
740  err:
741     BUF_MEM_free(buf);
742     return 0;
743 }
744
745 #ifndef OPENSSL_NO_POSIX_IO
746 /*
747  * Check whether included path is a directory.
748  * Returns next BIO to process and in case of a directory
749  * also an opened directory context and the include path.
750  */
751 static BIO *process_include(char *include, OPENSSL_DIR_CTX **dirctx,
752                             char **dirpath)
753 {
754     struct stat st;
755     BIO *next;
756
757     if (stat(include, &st) < 0) {
758         ERR_raise_data(ERR_LIB_SYS, errno,
759                        "calling stat(%s)",
760                        include);
761         /* missing include file is not fatal error */
762         return NULL;
763     }
764
765     if (S_ISDIR(st.st_mode)) {
766         if (*dirctx != NULL) {
767             CONFerr(CONF_F_PROCESS_INCLUDE,
768                     CONF_R_RECURSIVE_DIRECTORY_INCLUDE);
769             ERR_add_error_data(1, include);
770             return NULL;
771         }
772         /* a directory, load its contents */
773         if ((next = get_next_file(include, dirctx)) != NULL)
774             *dirpath = include;
775         return next;
776     }
777
778     next = BIO_new_file(include, "r");
779     return next;
780 }
781
782 /*
783  * Get next file from the directory path.
784  * Returns BIO of the next file to read and updates dirctx.
785  */
786 static BIO *get_next_file(const char *path, OPENSSL_DIR_CTX **dirctx)
787 {
788     const char *filename;
789     size_t pathlen;
790
791     pathlen = strlen(path);
792     while ((filename = OPENSSL_DIR_read(dirctx, path)) != NULL) {
793         size_t namelen;
794
795         namelen = strlen(filename);
796
797
798         if ((namelen > 5 && strcasecmp(filename + namelen - 5, ".conf") == 0)
799             || (namelen > 4 && strcasecmp(filename + namelen - 4, ".cnf") == 0)) {
800             size_t newlen;
801             char *newpath;
802             BIO *bio;
803
804             newlen = pathlen + namelen + 2;
805             newpath = OPENSSL_zalloc(newlen);
806             if (newpath == NULL) {
807                 CONFerr(CONF_F_GET_NEXT_FILE, ERR_R_MALLOC_FAILURE);
808                 break;
809             }
810 #ifdef OPENSSL_SYS_VMS
811             /*
812              * If the given path isn't clear VMS syntax,
813              * we treat it as on Unix.
814              */
815             if (path[pathlen - 1] == ']'
816                 || path[pathlen - 1] == '>'
817                 || path[pathlen - 1] == ':') {
818                 /* Clear VMS directory syntax, just copy as is */
819                 OPENSSL_strlcpy(newpath, path, newlen);
820             }
821 #endif
822             if (newpath[0] == '\0') {
823                 OPENSSL_strlcpy(newpath, path, newlen);
824                 OPENSSL_strlcat(newpath, "/", newlen);
825             }
826             OPENSSL_strlcat(newpath, filename, newlen);
827
828             bio = BIO_new_file(newpath, "r");
829             OPENSSL_free(newpath);
830             /* Errors when opening files are non-fatal. */
831             if (bio != NULL)
832                 return bio;
833         }
834     }
835     OPENSSL_DIR_end(dirctx);
836     *dirctx = NULL;
837     return NULL;
838 }
839 #endif
840
841 static int is_keytype(const CONF *conf, char c, unsigned short type)
842 {
843     const unsigned short * keytypes = (const unsigned short *) conf->meth_data;
844     unsigned char key = (unsigned char)c;
845
846 #ifdef CHARSET_EBCDIC
847 # if CHAR_BIT > 8
848     if (key > 255) {
849         /* key is out of range for os_toascii table */
850         return 0;
851     }
852 # endif
853     /* convert key from ebcdic to ascii */
854     key = os_toascii[key];
855 #endif
856
857     if (key > 127) {
858         /* key is not a seven bit ascii character */
859         return 0;
860     }
861
862     return (keytypes[key] & type) ? 1 : 0;
863 }
864
865 static char *eat_ws(CONF *conf, char *p)
866 {
867     while (IS_WS(conf, *p) && (!IS_EOF(conf, *p)))
868         p++;
869     return p;
870 }
871
872 static void trim_ws(CONF *conf, char *start)
873 {
874     char *p = start;
875
876     while (!IS_EOF(conf, *p))
877         p++;
878     p--;
879     while ((p >= start) && IS_WS(conf, *p))
880         p--;
881     p++;
882     *p = '\0';
883 }
884
885 static char *eat_alpha_numeric(CONF *conf, char *p)
886 {
887     for (;;) {
888         if (IS_ESC(conf, *p)) {
889             p = scan_esc(conf, p);
890             continue;
891         }
892         if (!(IS_ALNUM_PUNCT(conf, *p)
893               || (conf->flag_dollarid && IS_DOLLAR(conf, *p))))
894             return p;
895         p++;
896     }
897 }
898
899 static char *scan_quote(CONF *conf, char *p)
900 {
901     int q = *p;
902
903     p++;
904     while (!(IS_EOF(conf, *p)) && (*p != q)) {
905         if (IS_ESC(conf, *p)) {
906             p++;
907             if (IS_EOF(conf, *p))
908                 return p;
909         }
910         p++;
911     }
912     if (*p == q)
913         p++;
914     return p;
915 }
916
917 static char *scan_dquote(CONF *conf, char *p)
918 {
919     int q = *p;
920
921     p++;
922     while (!(IS_EOF(conf, *p))) {
923         if (*p == q) {
924             if (*(p + 1) == q) {
925                 p++;
926             } else {
927                 break;
928             }
929         }
930         p++;
931     }
932     if (*p == q)
933         p++;
934     return p;
935 }
936
937 static void dump_value_doall_arg(const CONF_VALUE *a, BIO *out)
938 {
939     if (a->name)
940         BIO_printf(out, "[%s] %s=%s\n", a->section, a->name, a->value);
941     else
942         BIO_printf(out, "[[%s]]\n", a->section);
943 }
944
945 IMPLEMENT_LHASH_DOALL_ARG_CONST(CONF_VALUE, BIO);
946
947 static int def_dump(const CONF *conf, BIO *out)
948 {
949     lh_CONF_VALUE_doall_BIO(conf->data, dump_value_doall_arg, out);
950     return 1;
951 }
952
953 static int def_is_number(const CONF *conf, char c)
954 {
955     return IS_NUMBER(conf, c);
956 }
957
958 static int def_to_int(const CONF *conf, char c)
959 {
960     return c - '0';
961 }