conf_mod.c 16 KB

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