conf_mod.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. /* conf_mod.c */
  2. /* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL
  3. * project 2001.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <ctype.h>
  60. #include <openssl/crypto.h>
  61. #include "cryptlib.h"
  62. #include <openssl/conf.h>
  63. #include <openssl/dso.h>
  64. #include <openssl/x509.h>
  65. #define DSO_mod_init_name "OPENSSL_init"
  66. #define DSO_mod_finish_name "OPENSSL_finish"
  67. /* This structure contains a data about supported modules.
  68. * entries in this table correspond to either dynamic or
  69. * static modules.
  70. */
  71. struct conf_module_st
  72. {
  73. /* DSO of this module or NULL if static */
  74. DSO *dso;
  75. /* Name of the module */
  76. char *name;
  77. /* Init function */
  78. conf_init_func *init;
  79. /* Finish function */
  80. conf_finish_func *finish;
  81. /* Number of successfully initialized modules */
  82. int links;
  83. void *usr_data;
  84. };
  85. /* This structure contains information about modules that have been
  86. * successfully initialized. There may be more than one entry for a
  87. * given module.
  88. */
  89. struct conf_imodule_st
  90. {
  91. CONF_MODULE *pmod;
  92. char *name;
  93. char *value;
  94. unsigned long flags;
  95. void *usr_data;
  96. };
  97. static STACK_OF(CONF_MODULE) *supported_modules = NULL;
  98. static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
  99. static void module_free(CONF_MODULE *md);
  100. static void module_finish(CONF_IMODULE *imod);
  101. static int module_run(const CONF *cnf, char *name, char *value,
  102. unsigned long flags);
  103. static CONF_MODULE *module_add(DSO *dso, const char *name,
  104. conf_init_func *ifunc, conf_finish_func *ffunc);
  105. static CONF_MODULE *module_find(char *name);
  106. static int module_init(CONF_MODULE *pmod, char *name, char *value,
  107. const CONF *cnf);
  108. static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
  109. unsigned long flags);
  110. /* Main function: load modules from a CONF structure */
  111. int CONF_modules_load(const CONF *cnf, const char *appname,
  112. unsigned long flags)
  113. {
  114. STACK_OF(CONF_VALUE) *values;
  115. CONF_VALUE *vl;
  116. char *vsection;
  117. int ret, i;
  118. if (!cnf)
  119. return 1;
  120. if (appname == NULL)
  121. appname = "openssl_conf";
  122. vsection = NCONF_get_string(cnf, NULL, appname);
  123. if (!vsection)
  124. {
  125. ERR_clear_error();
  126. return 1;
  127. }
  128. values = NCONF_get_section(cnf, vsection);
  129. if (!values)
  130. return 0;
  131. for (i = 0; i < sk_CONF_VALUE_num(values); i++)
  132. {
  133. vl = sk_CONF_VALUE_value(values, i);
  134. ret = module_run(cnf, vl->name, vl->value, flags);
  135. if (ret <= 0)
  136. if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
  137. return ret;
  138. }
  139. return 1;
  140. }
  141. int CONF_modules_load_file(const char *filename, const char *appname,
  142. unsigned long flags)
  143. {
  144. char *file = NULL;
  145. CONF *conf = NULL;
  146. int ret = 0;
  147. conf = NCONF_new(NULL);
  148. if (!conf)
  149. goto err;
  150. if (filename == NULL)
  151. {
  152. file = CONF_get1_default_config_file();
  153. if (!file)
  154. goto err;
  155. }
  156. else
  157. file = (char *)filename;
  158. if (NCONF_load(conf, file, NULL) <= 0)
  159. {
  160. if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
  161. (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE))
  162. {
  163. ERR_clear_error();
  164. ret = 1;
  165. }
  166. goto err;
  167. }
  168. ret = CONF_modules_load(conf, appname, flags);
  169. err:
  170. if (filename == NULL)
  171. OPENSSL_free(file);
  172. NCONF_free(conf);
  173. return ret;
  174. }
  175. static int module_run(const CONF *cnf, char *name, char *value,
  176. unsigned long flags)
  177. {
  178. CONF_MODULE *md;
  179. int ret;
  180. md = module_find(name);
  181. /* Module not found: try to load DSO */
  182. if (!md && !(flags & CONF_MFLAGS_NO_DSO))
  183. md = module_load_dso(cnf, name, value, flags);
  184. if (!md)
  185. {
  186. if (!(flags & CONF_MFLAGS_SILENT))
  187. {
  188. CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
  189. ERR_add_error_data(2, "module=", name);
  190. }
  191. return -1;
  192. }
  193. ret = module_init(md, name, value, cnf);
  194. if (ret <= 0)
  195. {
  196. if (!(flags & CONF_MFLAGS_SILENT))
  197. {
  198. char rcode[DECIMAL_SIZE(ret)+1];
  199. CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
  200. sprintf(rcode, "%-8d", ret);
  201. ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
  202. }
  203. }
  204. return ret;
  205. }
  206. /* Load a module from a DSO */
  207. static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
  208. unsigned long flags)
  209. {
  210. DSO *dso = NULL;
  211. conf_init_func *ifunc;
  212. conf_finish_func *ffunc;
  213. char *path = NULL;
  214. int errcode = 0;
  215. CONF_MODULE *md;
  216. /* Look for alternative path in module section */
  217. path = NCONF_get_string(cnf, value, "path");
  218. if (!path)
  219. {
  220. ERR_get_error();
  221. path = name;
  222. }
  223. dso = DSO_load(NULL, path, NULL, 0);
  224. if (!dso)
  225. {
  226. errcode = CONF_R_ERROR_LOADING_DSO;
  227. goto err;
  228. }
  229. ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
  230. if (!ifunc)
  231. {
  232. errcode = CONF_R_MISSING_INIT_FUNCTION;
  233. goto err;
  234. }
  235. ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
  236. /* All OK, add module */
  237. md = module_add(dso, name, ifunc, ffunc);
  238. if (!md)
  239. goto err;
  240. return md;
  241. err:
  242. if (dso)
  243. DSO_free(dso);
  244. CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
  245. ERR_add_error_data(4, "module=", name, ", path=", path);
  246. return NULL;
  247. }
  248. /* add module to list */
  249. static CONF_MODULE *module_add(DSO *dso, const char *name,
  250. conf_init_func *ifunc, conf_finish_func *ffunc)
  251. {
  252. CONF_MODULE *tmod = NULL;
  253. if (supported_modules == NULL)
  254. supported_modules = sk_CONF_MODULE_new_null();
  255. if (supported_modules == NULL)
  256. return NULL;
  257. tmod = OPENSSL_malloc(sizeof(CONF_MODULE));
  258. if (tmod == NULL)
  259. return NULL;
  260. tmod->dso = dso;
  261. tmod->name = BUF_strdup(name);
  262. tmod->init = ifunc;
  263. tmod->finish = ffunc;
  264. tmod->links = 0;
  265. if (!sk_CONF_MODULE_push(supported_modules, tmod))
  266. {
  267. OPENSSL_free(tmod);
  268. return NULL;
  269. }
  270. return tmod;
  271. }
  272. /* Find a module from the list. We allow module names of the
  273. * form modname.XXXX to just search for modname to allow the
  274. * same module to be initialized more than once.
  275. */
  276. static CONF_MODULE *module_find(char *name)
  277. {
  278. CONF_MODULE *tmod;
  279. int i, nchar;
  280. char *p;
  281. p = strrchr(name, '.');
  282. if (p)
  283. nchar = p - name;
  284. else
  285. nchar = strlen(name);
  286. for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++)
  287. {
  288. tmod = sk_CONF_MODULE_value(supported_modules, i);
  289. if (!strncmp(tmod->name, name, nchar))
  290. return tmod;
  291. }
  292. return NULL;
  293. }
  294. /* initialize a module */
  295. static int module_init(CONF_MODULE *pmod, char *name, char *value,
  296. const CONF *cnf)
  297. {
  298. int ret = 1;
  299. int init_called = 0;
  300. CONF_IMODULE *imod = NULL;
  301. /* Otherwise add initialized module to list */
  302. imod = OPENSSL_malloc(sizeof(CONF_IMODULE));
  303. if (!imod)
  304. goto err;
  305. imod->pmod = pmod;
  306. imod->name = BUF_strdup(name);
  307. imod->value = BUF_strdup(value);
  308. imod->usr_data = NULL;
  309. if (!imod->name || !imod->value)
  310. goto memerr;
  311. /* Try to initialize module */
  312. if(pmod->init)
  313. {
  314. ret = pmod->init(imod, cnf);
  315. init_called = 1;
  316. /* Error occurred, exit */
  317. if (ret <= 0)
  318. goto err;
  319. }
  320. if (initialized_modules == NULL)
  321. {
  322. initialized_modules = sk_CONF_IMODULE_new_null();
  323. if (!initialized_modules)
  324. {
  325. CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
  326. goto err;
  327. }
  328. }
  329. if (!sk_CONF_IMODULE_push(initialized_modules, imod))
  330. {
  331. CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
  332. goto err;
  333. }
  334. pmod->links++;
  335. return ret;
  336. err:
  337. /* We've started the module so we'd better finish it */
  338. if (pmod->finish && init_called)
  339. pmod->finish(imod);
  340. memerr:
  341. if (imod)
  342. {
  343. if (imod->name)
  344. OPENSSL_free(imod->name);
  345. if (imod->value)
  346. OPENSSL_free(imod->value);
  347. OPENSSL_free(imod);
  348. }
  349. return -1;
  350. }
  351. /* Unload any dynamic modules that have a link count of zero:
  352. * i.e. have no active initialized modules. If 'all' is set
  353. * then all modules are unloaded including static ones.
  354. */
  355. void CONF_modules_unload(int all)
  356. {
  357. int i;
  358. CONF_MODULE *md;
  359. CONF_modules_finish();
  360. /* unload modules in reverse order */
  361. for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
  362. {
  363. md = sk_CONF_MODULE_value(supported_modules, i);
  364. /* If static or in use and 'all' not set ignore it */
  365. if (((md->links > 0) || !md->dso) && !all)
  366. continue;
  367. /* Since we're working in reverse this is OK */
  368. sk_CONF_MODULE_delete(supported_modules, i);
  369. module_free(md);
  370. }
  371. if (sk_CONF_MODULE_num(supported_modules) == 0)
  372. {
  373. sk_CONF_MODULE_free(supported_modules);
  374. supported_modules = NULL;
  375. }
  376. }
  377. /* unload a single module */
  378. static void module_free(CONF_MODULE *md)
  379. {
  380. if (md->dso)
  381. DSO_free(md->dso);
  382. OPENSSL_free(md->name);
  383. OPENSSL_free(md);
  384. }
  385. /* finish and free up all modules instances */
  386. void CONF_modules_finish(void)
  387. {
  388. CONF_IMODULE *imod;
  389. while (sk_CONF_IMODULE_num(initialized_modules) > 0)
  390. {
  391. imod = sk_CONF_IMODULE_pop(initialized_modules);
  392. module_finish(imod);
  393. }
  394. sk_CONF_IMODULE_free(initialized_modules);
  395. initialized_modules = NULL;
  396. }
  397. /* finish a module instance */
  398. static void module_finish(CONF_IMODULE *imod)
  399. {
  400. if (imod->pmod->finish)
  401. imod->pmod->finish(imod);
  402. imod->pmod->links--;
  403. OPENSSL_free(imod->name);
  404. OPENSSL_free(imod->value);
  405. OPENSSL_free(imod);
  406. }
  407. /* Add a static module to OpenSSL */
  408. int CONF_module_add(const char *name, conf_init_func *ifunc,
  409. conf_finish_func *ffunc)
  410. {
  411. if (module_add(NULL, name, ifunc, ffunc))
  412. return 1;
  413. else
  414. return 0;
  415. }
  416. void CONF_modules_free(void)
  417. {
  418. CONF_modules_finish();
  419. CONF_modules_unload(1);
  420. }
  421. /* Utility functions */
  422. const char *CONF_imodule_get_name(const CONF_IMODULE *md)
  423. {
  424. return md->name;
  425. }
  426. const char *CONF_imodule_get_value(const CONF_IMODULE *md)
  427. {
  428. return md->value;
  429. }
  430. void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
  431. {
  432. return md->usr_data;
  433. }
  434. void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
  435. {
  436. md->usr_data = usr_data;
  437. }
  438. CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
  439. {
  440. return md->pmod;
  441. }
  442. unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
  443. {
  444. return md->flags;
  445. }
  446. void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
  447. {
  448. md->flags = flags;
  449. }
  450. void *CONF_module_get_usr_data(CONF_MODULE *pmod)
  451. {
  452. return pmod->usr_data;
  453. }
  454. void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
  455. {
  456. pmod->usr_data = usr_data;
  457. }
  458. /* Return default config file name */
  459. char *CONF_get1_default_config_file(void)
  460. {
  461. char *file;
  462. int len;
  463. file = getenv("OPENSSL_CONF");
  464. if (file)
  465. return BUF_strdup(file);
  466. len = strlen(X509_get_default_cert_area());
  467. #ifndef OPENSSL_SYS_VMS
  468. len++;
  469. #endif
  470. len += strlen(OPENSSL_CONF);
  471. file = OPENSSL_malloc(len + 1);
  472. if (!file)
  473. return NULL;
  474. strcpy(file,X509_get_default_cert_area());
  475. #ifndef OPENSSL_SYS_VMS
  476. strcat(file,"/");
  477. #endif
  478. strcat(file,OPENSSL_CONF);
  479. return file;
  480. }
  481. /* This function takes a list separated by 'sep' and calls the
  482. * callback function giving the start and length of each member
  483. * optionally stripping leading and trailing whitespace. This can
  484. * be used to parse comma separated lists for example.
  485. */
  486. int CONF_parse_list(const char *list, int sep, int nospc,
  487. int (*list_cb)(const char *elem, int len, void *usr), void *arg)
  488. {
  489. int ret;
  490. const char *lstart, *tmpend, *p;
  491. lstart = list;
  492. for(;;)
  493. {
  494. if (nospc)
  495. {
  496. while(*lstart && isspace((unsigned char)*lstart))
  497. lstart++;
  498. }
  499. p = strchr(lstart, sep);
  500. if (p == lstart || !*lstart)
  501. ret = list_cb(NULL, 0, arg);
  502. else
  503. {
  504. if (p)
  505. tmpend = p - 1;
  506. else
  507. tmpend = lstart + strlen(lstart) - 1;
  508. if (nospc)
  509. {
  510. while(isspace((unsigned char)*tmpend))
  511. tmpend--;
  512. }
  513. ret = list_cb(lstart, tmpend - lstart + 1, arg);
  514. }
  515. if (ret <= 0)
  516. return ret;
  517. if (p == NULL)
  518. return 1;
  519. lstart = p + 1;
  520. }
  521. }