Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/conf.h>
  57. #include <string.h>
  58. #include <ctype.h>
  59. #include <openssl/bio.h>
  60. #include <openssl/buf.h>
  61. #include <openssl/err.h>
  62. #include <openssl/mem.h>
  63. #include "conf_def.h"
  64. static uint32_t conf_value_hash(const CONF_VALUE *v) {
  65. return (lh_strhash(v->section) << 2) ^ lh_strhash(v->name);
  66. }
  67. static int conf_value_cmp(const CONF_VALUE *a, const CONF_VALUE *b) {
  68. int i;
  69. if (a->section != b->section) {
  70. i = strcmp(a->section, b->section);
  71. if (i) {
  72. return i;
  73. }
  74. }
  75. if (a->name != NULL && b->name != NULL) {
  76. return strcmp(a->name, b->name);
  77. } else if (a->name == b->name) {
  78. return 0;
  79. } else {
  80. return (a->name == NULL) ? -1 : 1;
  81. }
  82. }
  83. CONF *NCONF_new(void *method) {
  84. CONF *conf;
  85. if (method != NULL) {
  86. return NULL;
  87. }
  88. conf = OPENSSL_malloc(sizeof(CONF));
  89. if (conf == NULL) {
  90. return NULL;
  91. }
  92. conf->data = lh_CONF_VALUE_new(conf_value_hash, conf_value_cmp);
  93. if (conf->data == NULL) {
  94. OPENSSL_free(conf);
  95. return NULL;
  96. }
  97. return conf;
  98. }
  99. static void value_free_contents(CONF_VALUE *value) {
  100. if (value->section) {
  101. OPENSSL_free(value->section);
  102. }
  103. if (value->name) {
  104. OPENSSL_free(value->name);
  105. if (value->value) {
  106. OPENSSL_free(value->value);
  107. }
  108. } else {
  109. if (value->value) {
  110. sk_CONF_VALUE_free((STACK_OF(CONF_VALUE)*)value->value);
  111. }
  112. }
  113. }
  114. static void value_free(CONF_VALUE *value) {
  115. value_free_contents(value);
  116. OPENSSL_free(value);
  117. }
  118. void NCONF_free(CONF *conf) {
  119. if (conf == NULL || conf->data == NULL) {
  120. return;
  121. }
  122. lh_CONF_VALUE_doall(conf->data, value_free_contents);
  123. lh_CONF_VALUE_free(conf->data);
  124. OPENSSL_free(conf);
  125. }
  126. CONF_VALUE *NCONF_new_section(const CONF *conf, const char *section) {
  127. STACK_OF(CONF_VALUE) *sk = NULL;
  128. int ok = 0, i;
  129. CONF_VALUE *v = NULL, *old_value;
  130. sk = sk_CONF_VALUE_new_null();
  131. v = OPENSSL_malloc(sizeof(CONF_VALUE));
  132. if (sk == NULL || v == NULL) {
  133. goto err;
  134. }
  135. i = strlen(section) + 1;
  136. v->section = OPENSSL_malloc(i);
  137. if (v->section == NULL) {
  138. goto err;
  139. }
  140. memcpy(v->section, section, i);
  141. v->section[i-1] = 0;
  142. v->name = NULL;
  143. v->value = (char *)sk;
  144. if (!lh_CONF_VALUE_insert(conf->data, &old_value, v)) {
  145. goto err;
  146. }
  147. if (old_value) {
  148. value_free(old_value);
  149. }
  150. ok = 1;
  151. err:
  152. if (!ok) {
  153. if (sk != NULL) {
  154. sk_CONF_VALUE_free(sk);
  155. }
  156. if (v != NULL) {
  157. OPENSSL_free(v);
  158. }
  159. v = NULL;
  160. }
  161. return v;
  162. }
  163. static int str_copy(CONF *conf, char *section, char **pto, char *from) {
  164. int q, r, rr = 0, to = 0, len = 0;
  165. char *s, *e, *rp, *rrp, *np, *cp, v;
  166. const char *p;
  167. BUF_MEM *buf;
  168. buf = BUF_MEM_new();
  169. if (buf == NULL) {
  170. return 0;
  171. }
  172. len = strlen(from) + 1;
  173. if (!BUF_MEM_grow(buf, len)) {
  174. goto err;
  175. }
  176. for (;;) {
  177. if (IS_QUOTE(conf, *from)) {
  178. q = *from;
  179. from++;
  180. while (!IS_EOF(conf, *from) && (*from != q)) {
  181. if (IS_ESC(conf, *from)) {
  182. from++;
  183. if (IS_EOF(conf, *from)) {
  184. break;
  185. }
  186. }
  187. buf->data[to++] = *(from++);
  188. }
  189. if (*from == q) {
  190. from++;
  191. }
  192. } else if (IS_DQUOTE(conf, *from)) {
  193. q = *from;
  194. from++;
  195. while (!IS_EOF(conf, *from)) {
  196. if (*from == q) {
  197. if (*(from + 1) == q) {
  198. from++;
  199. } else {
  200. break;
  201. }
  202. }
  203. buf->data[to++] = *(from++);
  204. }
  205. if (*from == q) {
  206. from++;
  207. }
  208. } else if (IS_ESC(conf, *from)) {
  209. from++;
  210. v = *(from++);
  211. if (IS_EOF(conf, v)) {
  212. break;
  213. } else if (v == 'r') {
  214. v = '\r';
  215. } else if (v == 'n') {
  216. v = '\n';
  217. } else if (v == 'b') {
  218. v = '\b';
  219. } else if (v == 't') {
  220. v = '\t';
  221. }
  222. buf->data[to++] = v;
  223. } else if (IS_EOF(conf, *from)) {
  224. break;
  225. } else if (*from == '$') {
  226. /* try to expand it */
  227. rrp = NULL;
  228. s = &(from[1]);
  229. if (*s == '{') {
  230. q = '}';
  231. } else if (*s == '(') {
  232. q = ')';
  233. } else {
  234. q = 0;
  235. }
  236. if (q) {
  237. s++;
  238. }
  239. cp = section;
  240. e = np = s;
  241. while (IS_ALPHA_NUMERIC(conf, *e)) {
  242. e++;
  243. }
  244. if (e[0] == ':' && e[1] == ':') {
  245. cp = np;
  246. rrp = e;
  247. rr = *e;
  248. *rrp = '\0';
  249. e += 2;
  250. np = e;
  251. while (IS_ALPHA_NUMERIC(conf, *e)) {
  252. e++;
  253. }
  254. }
  255. r = *e;
  256. *e = '\0';
  257. rp = e;
  258. if (q) {
  259. if (r != q) {
  260. OPENSSL_PUT_ERROR(CONF, str_copy, CONF_R_NO_CLOSE_BRACE);
  261. goto err;
  262. }
  263. e++;
  264. }
  265. /* So at this point we have
  266. * np which is the start of the name string which is
  267. * '\0' terminated.
  268. * cp which is the start of the section string which is
  269. * '\0' terminated.
  270. * e is the 'next point after'.
  271. * r and rr are the chars replaced by the '\0'
  272. * rp and rrp is where 'r' and 'rr' came from. */
  273. p = NCONF_get_string(conf, cp, np);
  274. if (rrp != NULL) {
  275. *rrp = rr;
  276. }
  277. *rp = r;
  278. if (p == NULL) {
  279. OPENSSL_PUT_ERROR(CONF, str_copy, CONF_R_VARIABLE_HAS_NO_VALUE);
  280. goto err;
  281. }
  282. BUF_MEM_grow_clean(buf, (strlen(p) + buf->length - (e - from)));
  283. while (*p) {
  284. buf->data[to++] = *(p++);
  285. }
  286. /* Since we change the pointer 'from', we also have
  287. to change the perceived length of the string it
  288. points at. /RL */
  289. len -= e - from;
  290. from = e;
  291. /* In case there were no braces or parenthesis around
  292. the variable reference, we have to put back the
  293. character that was replaced with a '\0'. /RL */
  294. *rp = r;
  295. } else {
  296. buf->data[to++] = *(from++);
  297. }
  298. }
  299. buf->data[to] = '\0';
  300. if (*pto != NULL) {
  301. OPENSSL_free(*pto);
  302. }
  303. *pto = buf->data;
  304. OPENSSL_free(buf);
  305. return 1;
  306. err:
  307. if (buf != NULL) {
  308. BUF_MEM_free(buf);
  309. }
  310. return 0;
  311. }
  312. static CONF_VALUE *get_section(const CONF *conf, const char *section) {
  313. CONF_VALUE template;
  314. memset(&template, 0, sizeof(template));
  315. template.section = (char *) section;
  316. return lh_CONF_VALUE_retrieve(conf->data, &template);
  317. }
  318. STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section) {
  319. CONF_VALUE *section_value = get_section(conf, section);
  320. if (section_value == NULL) {
  321. return NULL;
  322. }
  323. return (STACK_OF(CONF_VALUE)*) section_value->value;
  324. }
  325. const char *NCONF_get_string(const CONF *conf, const char *section,
  326. const char *name) {
  327. CONF_VALUE template, *value;
  328. memset(&template, 0, sizeof(template));
  329. template.section = (char *) section;
  330. template.name = (char *) name;
  331. value = lh_CONF_VALUE_retrieve(conf->data, &template);
  332. if (value == NULL) {
  333. return NULL;
  334. }
  335. return value->value;
  336. }
  337. int add_string(const CONF *conf, CONF_VALUE *section, CONF_VALUE *value) {
  338. STACK_OF(CONF_VALUE) *section_stack = (STACK_OF(CONF_VALUE)*) section->value;
  339. CONF_VALUE *old_value;
  340. value->section = section->section;
  341. if (!sk_CONF_VALUE_push(section_stack, value)) {
  342. return 0;
  343. }
  344. if (!lh_CONF_VALUE_insert(conf->data, &old_value, value)) {
  345. return 0;
  346. }
  347. if (old_value != NULL) {
  348. (void)sk_CONF_VALUE_delete_ptr(section_stack, old_value);
  349. value_free(old_value);
  350. }
  351. return 1;
  352. }
  353. static char *eat_ws(CONF *conf, char *p) {
  354. while (IS_WS(conf, *p) && !IS_EOF(conf, *p)) {
  355. p++;
  356. }
  357. return p;
  358. }
  359. #define scan_esc(conf, p) (((IS_EOF((conf), (p)[1])) ? ((p) + 1) : ((p) + 2)))
  360. static char *eat_alpha_numeric(CONF *conf, char *p) {
  361. for (;;) {
  362. if (IS_ESC(conf, *p)) {
  363. p = scan_esc(conf, p);
  364. continue;
  365. }
  366. if (!IS_ALPHA_NUMERIC_PUNCT(conf, *p)) {
  367. return p;
  368. }
  369. p++;
  370. }
  371. }
  372. static char *scan_quote(CONF *conf, char *p) {
  373. int q = *p;
  374. p++;
  375. while (!IS_EOF(conf, *p) && *p != q) {
  376. if (IS_ESC(conf, *p)) {
  377. p++;
  378. if (IS_EOF(conf, *p)) {
  379. return p;
  380. }
  381. }
  382. p++;
  383. }
  384. if (*p == q) {
  385. p++;
  386. }
  387. return p;
  388. }
  389. static char *scan_dquote(CONF *conf, char *p) {
  390. int q = *p;
  391. p++;
  392. while (!(IS_EOF(conf, *p))) {
  393. if (*p == q) {
  394. if (*(p + 1) == q) {
  395. p++;
  396. } else {
  397. break;
  398. }
  399. }
  400. p++;
  401. }
  402. if (*p == q) {
  403. p++;
  404. }
  405. return p;
  406. }
  407. static void clear_comments(CONF *conf, char *p) {
  408. for (;;) {
  409. if (IS_FCOMMENT(conf, *p)) {
  410. *p = '\0';
  411. return;
  412. }
  413. if (!IS_WS(conf, *p)) {
  414. break;
  415. }
  416. p++;
  417. }
  418. for (;;) {
  419. if (IS_COMMENT(conf, *p)) {
  420. *p = '\0';
  421. return;
  422. }
  423. if (IS_DQUOTE(conf, *p)) {
  424. p = scan_dquote(conf, p);
  425. continue;
  426. }
  427. if (IS_QUOTE(conf, *p)) {
  428. p = scan_quote(conf, p);
  429. continue;
  430. }
  431. if (IS_ESC(conf, *p)) {
  432. p = scan_esc(conf, p);
  433. continue;
  434. }
  435. if (IS_EOF(conf, *p)) {
  436. return;
  437. } else {
  438. p++;
  439. }
  440. }
  441. }
  442. static int def_load_bio(CONF *conf, BIO *in, long *out_error_line) {
  443. static const size_t CONFBUFSIZE = 512;
  444. int bufnum = 0, i, ii;
  445. BUF_MEM *buff = NULL;
  446. char *s, *p, *end;
  447. int again;
  448. long eline = 0;
  449. char btmp[DECIMAL_SIZE(eline) + 1];
  450. CONF_VALUE *v = NULL, *tv;
  451. CONF_VALUE *sv = NULL;
  452. char *section = NULL, *buf;
  453. char *start, *psection, *pname;
  454. if ((buff = BUF_MEM_new()) == NULL) {
  455. OPENSSL_PUT_ERROR(CONF, def_load_bio, ERR_R_BUF_LIB);
  456. goto err;
  457. }
  458. section = (char *)OPENSSL_malloc(10);
  459. if (section == NULL) {
  460. OPENSSL_PUT_ERROR(CONF, def_load_bio, ERR_R_MALLOC_FAILURE);
  461. goto err;
  462. }
  463. BUF_strlcpy(section, "default", 10);
  464. sv = NCONF_new_section(conf, section);
  465. if (sv == NULL) {
  466. OPENSSL_PUT_ERROR(CONF, def_load_bio, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  467. goto err;
  468. }
  469. bufnum = 0;
  470. again = 0;
  471. for (;;) {
  472. if (!BUF_MEM_grow(buff, bufnum + CONFBUFSIZE)) {
  473. OPENSSL_PUT_ERROR(CONF, def_load_bio, ERR_R_BUF_LIB);
  474. goto err;
  475. }
  476. p = &(buff->data[bufnum]);
  477. *p = '\0';
  478. BIO_gets(in, p, CONFBUFSIZE - 1);
  479. p[CONFBUFSIZE - 1] = '\0';
  480. ii = i = strlen(p);
  481. if (i == 0 && !again) {
  482. break;
  483. }
  484. again = 0;
  485. while (i > 0) {
  486. if ((p[i - 1] != '\r') && (p[i - 1] != '\n')) {
  487. break;
  488. } else {
  489. i--;
  490. }
  491. }
  492. /* we removed some trailing stuff so there is a new
  493. * line on the end. */
  494. if (ii && i == ii) {
  495. again = 1; /* long line */
  496. } else {
  497. p[i] = '\0';
  498. eline++; /* another input line */
  499. }
  500. /* we now have a line with trailing \r\n removed */
  501. /* i is the number of bytes */
  502. bufnum += i;
  503. v = NULL;
  504. /* check for line continuation */
  505. if (bufnum >= 1) {
  506. /* If we have bytes and the last char '\\' and
  507. * second last char is not '\\' */
  508. p = &(buff->data[bufnum - 1]);
  509. if (IS_ESC(conf, p[0]) && ((bufnum <= 1) || !IS_ESC(conf, p[-1]))) {
  510. bufnum--;
  511. again = 1;
  512. }
  513. }
  514. if (again) {
  515. continue;
  516. }
  517. bufnum = 0;
  518. buf = buff->data;
  519. clear_comments(conf, buf);
  520. s = eat_ws(conf, buf);
  521. if (IS_EOF(conf, *s)) {
  522. continue; /* blank line */
  523. }
  524. if (*s == '[') {
  525. char *ss;
  526. s++;
  527. start = eat_ws(conf, s);
  528. ss = start;
  529. again:
  530. end = eat_alpha_numeric(conf, ss);
  531. p = eat_ws(conf, end);
  532. if (*p != ']') {
  533. if (*p != '\0' && ss != p) {
  534. ss = p;
  535. goto again;
  536. }
  537. OPENSSL_PUT_ERROR(CONF, def_load_bio, CONF_R_MISSING_CLOSE_SQUARE_BRACKET);
  538. goto err;
  539. }
  540. *end = '\0';
  541. if (!str_copy(conf, NULL, &section, start)) {
  542. goto err;
  543. }
  544. if ((sv = get_section(conf, section)) == NULL) {
  545. sv = NCONF_new_section(conf, section);
  546. }
  547. if (sv == NULL) {
  548. OPENSSL_PUT_ERROR(CONF, def_load_bio, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  549. goto err;
  550. }
  551. continue;
  552. } else {
  553. pname = s;
  554. psection = NULL;
  555. end = eat_alpha_numeric(conf, s);
  556. if ((end[0] == ':') && (end[1] == ':')) {
  557. *end = '\0';
  558. end += 2;
  559. psection = pname;
  560. pname = end;
  561. end = eat_alpha_numeric(conf, end);
  562. }
  563. p = eat_ws(conf, end);
  564. if (*p != '=') {
  565. OPENSSL_PUT_ERROR(CONF, def_load_bio, CONF_R_MISSING_EQUAL_SIGN);
  566. goto err;
  567. }
  568. *end = '\0';
  569. p++;
  570. start = eat_ws(conf, p);
  571. while (!IS_EOF(conf, *p)) {
  572. p++;
  573. }
  574. p--;
  575. while ((p != start) && (IS_WS(conf, *p))) {
  576. p--;
  577. }
  578. p++;
  579. *p = '\0';
  580. if (!(v = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) {
  581. OPENSSL_PUT_ERROR(CONF, def_load_bio, ERR_R_MALLOC_FAILURE);
  582. goto err;
  583. }
  584. if (psection == NULL) {
  585. psection = section;
  586. }
  587. v->name = (char *)OPENSSL_malloc(strlen(pname) + 1);
  588. v->value = NULL;
  589. if (v->name == NULL) {
  590. OPENSSL_PUT_ERROR(CONF, def_load_bio, ERR_R_MALLOC_FAILURE);
  591. goto err;
  592. }
  593. BUF_strlcpy(v->name, pname, strlen(pname) + 1);
  594. if (!str_copy(conf, psection, &(v->value), start)) {
  595. goto err;
  596. }
  597. if (strcmp(psection, section) != 0) {
  598. if ((tv = get_section(conf, psection)) == NULL) {
  599. tv = NCONF_new_section(conf, psection);
  600. }
  601. if (tv == NULL) {
  602. OPENSSL_PUT_ERROR(CONF, def_load_bio, CONF_R_UNABLE_TO_CREATE_NEW_SECTION);
  603. goto err;
  604. }
  605. } else {
  606. tv = sv;
  607. }
  608. if (add_string(conf, tv, v) == 0) {
  609. OPENSSL_PUT_ERROR(CONF, def_load_bio, ERR_R_MALLOC_FAILURE);
  610. goto err;
  611. }
  612. v = NULL;
  613. }
  614. }
  615. if (buff != NULL) {
  616. BUF_MEM_free(buff);
  617. }
  618. if (section != NULL) {
  619. OPENSSL_free(section);
  620. }
  621. return 1;
  622. err:
  623. if (buff != NULL) {
  624. BUF_MEM_free(buff);
  625. }
  626. if (section != NULL) {
  627. OPENSSL_free(section);
  628. }
  629. if (out_error_line != NULL) {
  630. *out_error_line = eline;
  631. }
  632. BIO_snprintf(btmp, sizeof btmp, "%ld", eline);
  633. ERR_add_error_data(2, "line ", btmp);
  634. if (v != NULL) {
  635. if (v->name != NULL) {
  636. OPENSSL_free(v->name);
  637. }
  638. if (v->value != NULL) {
  639. OPENSSL_free(v->value);
  640. }
  641. if (v != NULL) {
  642. OPENSSL_free(v);
  643. }
  644. }
  645. return 0;
  646. }
  647. int NCONF_load(CONF *conf, const char *filename, long *out_error_line) {
  648. BIO *in = BIO_new_file(filename, "rb");
  649. int ret;
  650. if (in == NULL) {
  651. OPENSSL_PUT_ERROR(CONF, NCONF_load, ERR_R_SYS_LIB);
  652. return 0;
  653. }
  654. ret = def_load_bio(conf, in, out_error_line);
  655. BIO_free(in);
  656. return ret;
  657. }
  658. int NCONF_load_bio(CONF *conf, BIO *bio, long *out_error_line) {
  659. return def_load_bio(conf, bio, out_error_line);
  660. }
  661. int CONF_parse_list(const char *list, char sep, int remove_whitespace,
  662. int (*list_cb)(const char *elem, int len, void *usr),
  663. void *arg) {
  664. int ret;
  665. const char *lstart, *tmpend, *p;
  666. if (list == NULL) {
  667. OPENSSL_PUT_ERROR(CONF, CONF_parse_list, CONF_R_LIST_CANNOT_BE_NULL);
  668. return 0;
  669. }
  670. lstart = list;
  671. for (;;) {
  672. if (remove_whitespace) {
  673. while (*lstart && isspace((unsigned char)*lstart)) {
  674. lstart++;
  675. }
  676. }
  677. p = strchr(lstart, sep);
  678. if (p == lstart || !*lstart) {
  679. ret = list_cb(NULL, 0, arg);
  680. } else {
  681. if (p) {
  682. tmpend = p - 1;
  683. } else {
  684. tmpend = lstart + strlen(lstart) - 1;
  685. }
  686. if (remove_whitespace) {
  687. while (isspace((unsigned char)*tmpend)) {
  688. tmpend--;
  689. }
  690. }
  691. ret = list_cb(lstart, tmpend - lstart + 1, arg);
  692. }
  693. if (ret <= 0) {
  694. return ret;
  695. }
  696. if (p == NULL) {
  697. return 1;
  698. }
  699. lstart = p + 1;
  700. }
  701. }