conf_mod.c 13 KB

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