conf_mod.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 "internal/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 == NULL)
  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. DSO_free(dso);
  233. CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
  234. ERR_add_error_data(4, "module=", name, ", path=", path);
  235. return NULL;
  236. }
  237. /* add module to list */
  238. static CONF_MODULE *module_add(DSO *dso, const char *name,
  239. conf_init_func *ifunc, conf_finish_func *ffunc)
  240. {
  241. CONF_MODULE *tmod = NULL;
  242. if (supported_modules == NULL)
  243. supported_modules = sk_CONF_MODULE_new_null();
  244. if (supported_modules == NULL)
  245. return NULL;
  246. tmod = OPENSSL_zalloc(sizeof(*tmod));
  247. if (tmod == NULL)
  248. return NULL;
  249. tmod->dso = dso;
  250. tmod->name = OPENSSL_strdup(name);
  251. tmod->init = ifunc;
  252. tmod->finish = ffunc;
  253. if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
  254. OPENSSL_free(tmod);
  255. return NULL;
  256. }
  257. return tmod;
  258. }
  259. /*
  260. * Find a module from the list. We allow module names of the form
  261. * modname.XXXX to just search for modname to allow the same module to be
  262. * initialized more than once.
  263. */
  264. static CONF_MODULE *module_find(char *name)
  265. {
  266. CONF_MODULE *tmod;
  267. int i, nchar;
  268. char *p;
  269. p = strrchr(name, '.');
  270. if (p)
  271. nchar = p - name;
  272. else
  273. nchar = strlen(name);
  274. for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
  275. tmod = sk_CONF_MODULE_value(supported_modules, i);
  276. if (strncmp(tmod->name, name, nchar) == 0)
  277. return tmod;
  278. }
  279. return NULL;
  280. }
  281. /* initialize a module */
  282. static int module_init(CONF_MODULE *pmod, char *name, char *value,
  283. const CONF *cnf)
  284. {
  285. int ret = 1;
  286. int init_called = 0;
  287. CONF_IMODULE *imod = NULL;
  288. /* Otherwise add initialized module to list */
  289. imod = OPENSSL_malloc(sizeof(*imod));
  290. if (imod == NULL)
  291. goto err;
  292. imod->pmod = pmod;
  293. imod->name = OPENSSL_strdup(name);
  294. imod->value = OPENSSL_strdup(value);
  295. imod->usr_data = NULL;
  296. if (!imod->name || !imod->value)
  297. goto memerr;
  298. /* Try to initialize module */
  299. if (pmod->init) {
  300. ret = pmod->init(imod, cnf);
  301. init_called = 1;
  302. /* Error occurred, exit */
  303. if (ret <= 0)
  304. goto err;
  305. }
  306. if (initialized_modules == NULL) {
  307. initialized_modules = sk_CONF_IMODULE_new_null();
  308. if (!initialized_modules) {
  309. CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
  310. goto err;
  311. }
  312. }
  313. if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
  314. CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
  315. goto err;
  316. }
  317. pmod->links++;
  318. return ret;
  319. err:
  320. /* We've started the module so we'd better finish it */
  321. if (pmod->finish && init_called)
  322. pmod->finish(imod);
  323. memerr:
  324. if (imod) {
  325. OPENSSL_free(imod->name);
  326. OPENSSL_free(imod->value);
  327. OPENSSL_free(imod);
  328. }
  329. return -1;
  330. }
  331. /*
  332. * Unload any dynamic modules that have a link count of zero: i.e. have no
  333. * active initialized modules. If 'all' is set then all modules are unloaded
  334. * including static ones.
  335. */
  336. void CONF_modules_unload(int all)
  337. {
  338. int i;
  339. CONF_MODULE *md;
  340. CONF_modules_finish();
  341. /* unload modules in reverse order */
  342. for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
  343. md = sk_CONF_MODULE_value(supported_modules, i);
  344. /* If static or in use and 'all' not set ignore it */
  345. if (((md->links > 0) || !md->dso) && !all)
  346. continue;
  347. /* Since we're working in reverse this is OK */
  348. (void)sk_CONF_MODULE_delete(supported_modules, i);
  349. module_free(md);
  350. }
  351. if (sk_CONF_MODULE_num(supported_modules) == 0) {
  352. sk_CONF_MODULE_free(supported_modules);
  353. supported_modules = NULL;
  354. }
  355. }
  356. /* unload a single module */
  357. static void module_free(CONF_MODULE *md)
  358. {
  359. DSO_free(md->dso);
  360. OPENSSL_free(md->name);
  361. OPENSSL_free(md);
  362. }
  363. /* finish and free up all modules instances */
  364. void CONF_modules_finish(void)
  365. {
  366. CONF_IMODULE *imod;
  367. while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
  368. imod = sk_CONF_IMODULE_pop(initialized_modules);
  369. module_finish(imod);
  370. }
  371. sk_CONF_IMODULE_free(initialized_modules);
  372. initialized_modules = NULL;
  373. }
  374. /* finish a module instance */
  375. static void module_finish(CONF_IMODULE *imod)
  376. {
  377. if (!imod)
  378. return;
  379. if (imod->pmod->finish)
  380. imod->pmod->finish(imod);
  381. imod->pmod->links--;
  382. OPENSSL_free(imod->name);
  383. OPENSSL_free(imod->value);
  384. OPENSSL_free(imod);
  385. }
  386. /* Add a static module to OpenSSL */
  387. int CONF_module_add(const char *name, conf_init_func *ifunc,
  388. conf_finish_func *ffunc)
  389. {
  390. if (module_add(NULL, name, ifunc, ffunc))
  391. return 1;
  392. else
  393. return 0;
  394. }
  395. void CONF_modules_free(void)
  396. {
  397. CONF_modules_finish();
  398. CONF_modules_unload(1);
  399. }
  400. /* Utility functions */
  401. const char *CONF_imodule_get_name(const CONF_IMODULE *md)
  402. {
  403. return md->name;
  404. }
  405. const char *CONF_imodule_get_value(const CONF_IMODULE *md)
  406. {
  407. return md->value;
  408. }
  409. void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
  410. {
  411. return md->usr_data;
  412. }
  413. void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
  414. {
  415. md->usr_data = usr_data;
  416. }
  417. CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
  418. {
  419. return md->pmod;
  420. }
  421. unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
  422. {
  423. return md->flags;
  424. }
  425. void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
  426. {
  427. md->flags = flags;
  428. }
  429. void *CONF_module_get_usr_data(CONF_MODULE *pmod)
  430. {
  431. return pmod->usr_data;
  432. }
  433. void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
  434. {
  435. pmod->usr_data = usr_data;
  436. }
  437. /* Return default config file name */
  438. char *CONF_get1_default_config_file(void)
  439. {
  440. char *file;
  441. int len;
  442. file = getenv("OPENSSL_CONF");
  443. if (file)
  444. return OPENSSL_strdup(file);
  445. len = strlen(X509_get_default_cert_area());
  446. #ifndef OPENSSL_SYS_VMS
  447. len++;
  448. #endif
  449. len += strlen(OPENSSL_CONF);
  450. file = OPENSSL_malloc(len + 1);
  451. if (file == NULL)
  452. return NULL;
  453. OPENSSL_strlcpy(file, X509_get_default_cert_area(), len + 1);
  454. #ifndef OPENSSL_SYS_VMS
  455. OPENSSL_strlcat(file, "/", len + 1);
  456. #endif
  457. OPENSSL_strlcat(file, OPENSSL_CONF, len + 1);
  458. return file;
  459. }
  460. /*
  461. * This function takes a list separated by 'sep' and calls the callback
  462. * function giving the start and length of each member optionally stripping
  463. * leading and trailing whitespace. This can be used to parse comma separated
  464. * lists for example.
  465. */
  466. int CONF_parse_list(const char *list_, int sep, int nospc,
  467. int (*list_cb) (const char *elem, int len, void *usr),
  468. void *arg)
  469. {
  470. int ret;
  471. const char *lstart, *tmpend, *p;
  472. if (list_ == NULL) {
  473. CONFerr(CONF_F_CONF_PARSE_LIST, CONF_R_LIST_CANNOT_BE_NULL);
  474. return 0;
  475. }
  476. lstart = list_;
  477. for (;;) {
  478. if (nospc) {
  479. while (*lstart && isspace((unsigned char)*lstart))
  480. lstart++;
  481. }
  482. p = strchr(lstart, sep);
  483. if (p == lstart || !*lstart)
  484. ret = list_cb(NULL, 0, arg);
  485. else {
  486. if (p)
  487. tmpend = p - 1;
  488. else
  489. tmpend = lstart + strlen(lstart) - 1;
  490. if (nospc) {
  491. while (isspace((unsigned char)*tmpend))
  492. tmpend--;
  493. }
  494. ret = list_cb(lstart, tmpend - lstart + 1, arg);
  495. }
  496. if (ret <= 0)
  497. return ret;
  498. if (p == NULL)
  499. return 1;
  500. lstart = p + 1;
  501. }
  502. }