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