2
0

conf_def.c 22 KB

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