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