conf_def.c 15 KB

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