conf_mod.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/cryptlib.h"
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <openssl/crypto.h>
  13. #include "internal/conf.h"
  14. #include "internal/dso.h"
  15. #include <openssl/x509.h>
  16. #include <openssl/trace.h>
  17. #define DSO_mod_init_name "OPENSSL_init"
  18. #define DSO_mod_finish_name "OPENSSL_finish"
  19. /*
  20. * This structure contains a data about supported modules. entries in this
  21. * table correspond to either dynamic or static modules.
  22. */
  23. struct conf_module_st {
  24. /* DSO of this module or NULL if static */
  25. DSO *dso;
  26. /* Name of the module */
  27. char *name;
  28. /* Init function */
  29. conf_init_func *init;
  30. /* Finish function */
  31. conf_finish_func *finish;
  32. /* Number of successfully initialized modules */
  33. int links;
  34. void *usr_data;
  35. };
  36. /*
  37. * This structure contains information about modules that have been
  38. * successfully initialized. There may be more than one entry for a given
  39. * module.
  40. */
  41. struct conf_imodule_st {
  42. CONF_MODULE *pmod;
  43. char *name;
  44. char *value;
  45. unsigned long flags;
  46. void *usr_data;
  47. };
  48. static STACK_OF(CONF_MODULE) *supported_modules = NULL;
  49. static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
  50. static void module_free(CONF_MODULE *md);
  51. static void module_finish(CONF_IMODULE *imod);
  52. static int module_run(const CONF *cnf, const char *name, const char *value,
  53. unsigned long flags);
  54. static CONF_MODULE *module_add(DSO *dso, const char *name,
  55. conf_init_func *ifunc,
  56. conf_finish_func *ffunc);
  57. static CONF_MODULE *module_find(const char *name);
  58. static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
  59. const CONF *cnf);
  60. static CONF_MODULE *module_load_dso(const CONF *cnf, const char *name,
  61. const char *value);
  62. /* Main function: load modules from a CONF structure */
  63. int CONF_modules_load(const CONF *cnf, const char *appname,
  64. unsigned long flags)
  65. {
  66. STACK_OF(CONF_VALUE) *values;
  67. CONF_VALUE *vl;
  68. char *vsection = NULL;
  69. int ret, i;
  70. if (!cnf)
  71. return 1;
  72. if (appname)
  73. vsection = NCONF_get_string(cnf, NULL, appname);
  74. if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
  75. vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
  76. if (!vsection) {
  77. ERR_clear_error();
  78. return 1;
  79. }
  80. OSSL_TRACE1(CONF, "Configuration in section %s\n", vsection);
  81. values = NCONF_get_section(cnf, vsection);
  82. if (!values)
  83. return 0;
  84. for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
  85. vl = sk_CONF_VALUE_value(values, i);
  86. ret = module_run(cnf, vl->name, vl->value, flags);
  87. OSSL_TRACE3(CONF, "Running module %s (%s) returned %d\n",
  88. vl->name, vl->value, ret);
  89. if (ret <= 0)
  90. if (!(flags & CONF_MFLAGS_IGNORE_ERRORS))
  91. return ret;
  92. }
  93. return 1;
  94. }
  95. int CONF_modules_load_file(const char *filename, const char *appname,
  96. unsigned long flags)
  97. {
  98. char *file = NULL;
  99. CONF *conf = NULL;
  100. int ret = 0;
  101. conf = NCONF_new(NULL);
  102. if (conf == NULL)
  103. goto err;
  104. if (filename == NULL) {
  105. file = CONF_get1_default_config_file();
  106. if (!file)
  107. goto err;
  108. } else
  109. file = (char *)filename;
  110. if (NCONF_load(conf, file, NULL) <= 0) {
  111. if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
  112. (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
  113. ERR_clear_error();
  114. ret = 1;
  115. }
  116. goto err;
  117. }
  118. ret = CONF_modules_load(conf, appname, flags);
  119. err:
  120. if (filename == NULL)
  121. OPENSSL_free(file);
  122. NCONF_free(conf);
  123. if (flags & CONF_MFLAGS_IGNORE_RETURN_CODES)
  124. return 1;
  125. return ret;
  126. }
  127. static int module_run(const CONF *cnf, const char *name, const char *value,
  128. unsigned long flags)
  129. {
  130. CONF_MODULE *md;
  131. int ret;
  132. md = module_find(name);
  133. /* Module not found: try to load DSO */
  134. if (!md && !(flags & CONF_MFLAGS_NO_DSO))
  135. md = module_load_dso(cnf, name, value);
  136. if (!md) {
  137. if (!(flags & CONF_MFLAGS_SILENT)) {
  138. CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
  139. ERR_add_error_data(2, "module=", name);
  140. }
  141. return -1;
  142. }
  143. ret = module_init(md, name, value, cnf);
  144. if (ret <= 0) {
  145. if (!(flags & CONF_MFLAGS_SILENT)) {
  146. char rcode[DECIMAL_SIZE(ret) + 1];
  147. CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
  148. BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
  149. ERR_add_error_data(6, "module=", name, ", value=", value,
  150. ", retcode=", rcode);
  151. }
  152. }
  153. return ret;
  154. }
  155. /* Load a module from a DSO */
  156. static CONF_MODULE *module_load_dso(const CONF *cnf,
  157. const char *name, const char *value)
  158. {
  159. DSO *dso = NULL;
  160. conf_init_func *ifunc;
  161. conf_finish_func *ffunc;
  162. const char *path = NULL;
  163. int errcode = 0;
  164. CONF_MODULE *md;
  165. /* Look for alternative path in module section */
  166. path = NCONF_get_string(cnf, value, "path");
  167. if (path == NULL) {
  168. ERR_clear_error();
  169. path = name;
  170. }
  171. dso = DSO_load(NULL, path, NULL, 0);
  172. if (dso == NULL) {
  173. errcode = CONF_R_ERROR_LOADING_DSO;
  174. goto err;
  175. }
  176. ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
  177. if (ifunc == NULL) {
  178. errcode = CONF_R_MISSING_INIT_FUNCTION;
  179. goto err;
  180. }
  181. ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
  182. /* All OK, add module */
  183. md = module_add(dso, name, ifunc, ffunc);
  184. if (md == NULL)
  185. goto err;
  186. return md;
  187. err:
  188. DSO_free(dso);
  189. CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
  190. ERR_add_error_data(4, "module=", name, ", path=", path);
  191. return NULL;
  192. }
  193. /* add module to list */
  194. static CONF_MODULE *module_add(DSO *dso, const char *name,
  195. conf_init_func *ifunc, conf_finish_func *ffunc)
  196. {
  197. CONF_MODULE *tmod = NULL;
  198. if (supported_modules == NULL)
  199. supported_modules = sk_CONF_MODULE_new_null();
  200. if (supported_modules == NULL)
  201. return NULL;
  202. if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
  203. CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
  204. return NULL;
  205. }
  206. tmod->dso = dso;
  207. tmod->name = OPENSSL_strdup(name);
  208. tmod->init = ifunc;
  209. tmod->finish = ffunc;
  210. if (tmod->name == NULL) {
  211. OPENSSL_free(tmod);
  212. return NULL;
  213. }
  214. if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
  215. OPENSSL_free(tmod->name);
  216. OPENSSL_free(tmod);
  217. return NULL;
  218. }
  219. return tmod;
  220. }
  221. /*
  222. * Find a module from the list. We allow module names of the form
  223. * modname.XXXX to just search for modname to allow the same module to be
  224. * initialized more than once.
  225. */
  226. static CONF_MODULE *module_find(const char *name)
  227. {
  228. CONF_MODULE *tmod;
  229. int i, nchar;
  230. char *p;
  231. p = strrchr(name, '.');
  232. if (p)
  233. nchar = p - name;
  234. else
  235. nchar = strlen(name);
  236. for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
  237. tmod = sk_CONF_MODULE_value(supported_modules, i);
  238. if (strncmp(tmod->name, name, nchar) == 0)
  239. return tmod;
  240. }
  241. return NULL;
  242. }
  243. /* initialize a module */
  244. static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
  245. const CONF *cnf)
  246. {
  247. int ret = 1;
  248. int init_called = 0;
  249. CONF_IMODULE *imod = NULL;
  250. /* Otherwise add initialized module to list */
  251. imod = OPENSSL_malloc(sizeof(*imod));
  252. if (imod == NULL)
  253. goto err;
  254. imod->pmod = pmod;
  255. imod->name = OPENSSL_strdup(name);
  256. imod->value = OPENSSL_strdup(value);
  257. imod->usr_data = NULL;
  258. if (!imod->name || !imod->value)
  259. goto memerr;
  260. /* Try to initialize module */
  261. if (pmod->init) {
  262. ret = pmod->init(imod, cnf);
  263. init_called = 1;
  264. /* Error occurred, exit */
  265. if (ret <= 0)
  266. goto err;
  267. }
  268. if (initialized_modules == NULL) {
  269. initialized_modules = sk_CONF_IMODULE_new_null();
  270. if (!initialized_modules) {
  271. CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
  272. goto err;
  273. }
  274. }
  275. if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
  276. CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
  277. goto err;
  278. }
  279. pmod->links++;
  280. return ret;
  281. err:
  282. /* We've started the module so we'd better finish it */
  283. if (pmod->finish && init_called)
  284. pmod->finish(imod);
  285. memerr:
  286. if (imod) {
  287. OPENSSL_free(imod->name);
  288. OPENSSL_free(imod->value);
  289. OPENSSL_free(imod);
  290. }
  291. return -1;
  292. }
  293. /*
  294. * Unload any dynamic modules that have a link count of zero: i.e. have no
  295. * active initialized modules. If 'all' is set then all modules are unloaded
  296. * including static ones.
  297. */
  298. void CONF_modules_unload(int all)
  299. {
  300. int i;
  301. CONF_MODULE *md;
  302. CONF_modules_finish();
  303. /* unload modules in reverse order */
  304. for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
  305. md = sk_CONF_MODULE_value(supported_modules, i);
  306. /* If static or in use and 'all' not set ignore it */
  307. if (((md->links > 0) || !md->dso) && !all)
  308. continue;
  309. /* Since we're working in reverse this is OK */
  310. (void)sk_CONF_MODULE_delete(supported_modules, i);
  311. module_free(md);
  312. }
  313. if (sk_CONF_MODULE_num(supported_modules) == 0) {
  314. sk_CONF_MODULE_free(supported_modules);
  315. supported_modules = NULL;
  316. }
  317. }
  318. /* unload a single module */
  319. static void module_free(CONF_MODULE *md)
  320. {
  321. DSO_free(md->dso);
  322. OPENSSL_free(md->name);
  323. OPENSSL_free(md);
  324. }
  325. /* finish and free up all modules instances */
  326. void CONF_modules_finish(void)
  327. {
  328. CONF_IMODULE *imod;
  329. while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
  330. imod = sk_CONF_IMODULE_pop(initialized_modules);
  331. module_finish(imod);
  332. }
  333. sk_CONF_IMODULE_free(initialized_modules);
  334. initialized_modules = NULL;
  335. }
  336. /* finish a module instance */
  337. static void module_finish(CONF_IMODULE *imod)
  338. {
  339. if (!imod)
  340. return;
  341. if (imod->pmod->finish)
  342. imod->pmod->finish(imod);
  343. imod->pmod->links--;
  344. OPENSSL_free(imod->name);
  345. OPENSSL_free(imod->value);
  346. OPENSSL_free(imod);
  347. }
  348. /* Add a static module to OpenSSL */
  349. int CONF_module_add(const char *name, conf_init_func *ifunc,
  350. conf_finish_func *ffunc)
  351. {
  352. if (module_add(NULL, name, ifunc, ffunc))
  353. return 1;
  354. else
  355. return 0;
  356. }
  357. void conf_modules_free_int(void)
  358. {
  359. CONF_modules_finish();
  360. CONF_modules_unload(1);
  361. }
  362. /* Utility functions */
  363. const char *CONF_imodule_get_name(const CONF_IMODULE *md)
  364. {
  365. return md->name;
  366. }
  367. const char *CONF_imodule_get_value(const CONF_IMODULE *md)
  368. {
  369. return md->value;
  370. }
  371. void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
  372. {
  373. return md->usr_data;
  374. }
  375. void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
  376. {
  377. md->usr_data = usr_data;
  378. }
  379. CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
  380. {
  381. return md->pmod;
  382. }
  383. unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
  384. {
  385. return md->flags;
  386. }
  387. void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
  388. {
  389. md->flags = flags;
  390. }
  391. void *CONF_module_get_usr_data(CONF_MODULE *pmod)
  392. {
  393. return pmod->usr_data;
  394. }
  395. void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
  396. {
  397. pmod->usr_data = usr_data;
  398. }
  399. /* Return default config file name */
  400. char *CONF_get1_default_config_file(void)
  401. {
  402. char *file, *sep = "";
  403. int len;
  404. if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
  405. return OPENSSL_strdup(file);
  406. len = strlen(X509_get_default_cert_area());
  407. #ifndef OPENSSL_SYS_VMS
  408. len++;
  409. sep = "/";
  410. #endif
  411. len += strlen(OPENSSL_CONF);
  412. file = OPENSSL_malloc(len + 1);
  413. if (file == NULL)
  414. return NULL;
  415. BIO_snprintf(file, len + 1, "%s%s%s", X509_get_default_cert_area(),
  416. sep, OPENSSL_CONF);
  417. return file;
  418. }
  419. /*
  420. * This function takes a list separated by 'sep' and calls the callback
  421. * function giving the start and length of each member optionally stripping
  422. * leading and trailing whitespace. This can be used to parse comma separated
  423. * lists for example.
  424. */
  425. int CONF_parse_list(const char *list_, int sep, int nospc,
  426. int (*list_cb) (const char *elem, int len, void *usr),
  427. void *arg)
  428. {
  429. int ret;
  430. const char *lstart, *tmpend, *p;
  431. if (list_ == NULL) {
  432. CONFerr(CONF_F_CONF_PARSE_LIST, CONF_R_LIST_CANNOT_BE_NULL);
  433. return 0;
  434. }
  435. lstart = list_;
  436. for (;;) {
  437. if (nospc) {
  438. while (*lstart && isspace((unsigned char)*lstart))
  439. lstart++;
  440. }
  441. p = strchr(lstart, sep);
  442. if (p == lstart || *lstart == '\0')
  443. ret = list_cb(NULL, 0, arg);
  444. else {
  445. if (p)
  446. tmpend = p - 1;
  447. else
  448. tmpend = lstart + strlen(lstart) - 1;
  449. if (nospc) {
  450. while (isspace((unsigned char)*tmpend))
  451. tmpend--;
  452. }
  453. ret = list_cb(lstart, tmpend - lstart + 1, arg);
  454. }
  455. if (ret <= 0)
  456. return ret;
  457. if (p == NULL)
  458. return 1;
  459. lstart = p + 1;
  460. }
  461. }