conf_mod.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * Copyright 2002-2019 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. if (flags & CONF_MFLAGS_IGNORE_RETURN_CODES)
  120. return 1;
  121. return ret;
  122. }
  123. static int module_run(const CONF *cnf, const char *name, const char *value,
  124. unsigned long flags)
  125. {
  126. CONF_MODULE *md;
  127. int ret;
  128. md = module_find(name);
  129. /* Module not found: try to load DSO */
  130. if (!md && !(flags & CONF_MFLAGS_NO_DSO))
  131. md = module_load_dso(cnf, name, value);
  132. if (!md) {
  133. if (!(flags & CONF_MFLAGS_SILENT)) {
  134. CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
  135. ERR_add_error_data(2, "module=", name);
  136. }
  137. return -1;
  138. }
  139. ret = module_init(md, name, value, cnf);
  140. if (ret <= 0) {
  141. if (!(flags & CONF_MFLAGS_SILENT)) {
  142. char rcode[DECIMAL_SIZE(ret) + 1];
  143. CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
  144. BIO_snprintf(rcode, sizeof(rcode), "%-8d", ret);
  145. ERR_add_error_data(6, "module=", name, ", value=", value,
  146. ", retcode=", rcode);
  147. }
  148. }
  149. return ret;
  150. }
  151. /* Load a module from a DSO */
  152. static CONF_MODULE *module_load_dso(const CONF *cnf,
  153. const char *name, const char *value)
  154. {
  155. DSO *dso = NULL;
  156. conf_init_func *ifunc;
  157. conf_finish_func *ffunc;
  158. const char *path = NULL;
  159. int errcode = 0;
  160. CONF_MODULE *md;
  161. /* Look for alternative path in module section */
  162. path = NCONF_get_string(cnf, value, "path");
  163. if (!path) {
  164. ERR_clear_error();
  165. path = name;
  166. }
  167. dso = DSO_load(NULL, path, NULL, 0);
  168. if (!dso) {
  169. errcode = CONF_R_ERROR_LOADING_DSO;
  170. goto err;
  171. }
  172. ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
  173. if (!ifunc) {
  174. errcode = CONF_R_MISSING_INIT_FUNCTION;
  175. goto err;
  176. }
  177. ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
  178. /* All OK, add module */
  179. md = module_add(dso, name, ifunc, ffunc);
  180. if (!md)
  181. goto err;
  182. return md;
  183. err:
  184. DSO_free(dso);
  185. CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
  186. ERR_add_error_data(4, "module=", name, ", path=", path);
  187. return NULL;
  188. }
  189. /* add module to list */
  190. static CONF_MODULE *module_add(DSO *dso, const char *name,
  191. conf_init_func *ifunc, conf_finish_func *ffunc)
  192. {
  193. CONF_MODULE *tmod = NULL;
  194. if (supported_modules == NULL)
  195. supported_modules = sk_CONF_MODULE_new_null();
  196. if (supported_modules == NULL)
  197. return NULL;
  198. if ((tmod = OPENSSL_zalloc(sizeof(*tmod))) == NULL) {
  199. CONFerr(CONF_F_MODULE_ADD, ERR_R_MALLOC_FAILURE);
  200. return NULL;
  201. }
  202. tmod->dso = dso;
  203. tmod->name = OPENSSL_strdup(name);
  204. tmod->init = ifunc;
  205. tmod->finish = ffunc;
  206. if (tmod->name == NULL) {
  207. OPENSSL_free(tmod);
  208. return NULL;
  209. }
  210. if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
  211. OPENSSL_free(tmod->name);
  212. OPENSSL_free(tmod);
  213. return NULL;
  214. }
  215. return tmod;
  216. }
  217. /*
  218. * Find a module from the list. We allow module names of the form
  219. * modname.XXXX to just search for modname to allow the same module to be
  220. * initialized more than once.
  221. */
  222. static CONF_MODULE *module_find(const char *name)
  223. {
  224. CONF_MODULE *tmod;
  225. int i, nchar;
  226. char *p;
  227. p = strrchr(name, '.');
  228. if (p)
  229. nchar = p - name;
  230. else
  231. nchar = strlen(name);
  232. for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
  233. tmod = sk_CONF_MODULE_value(supported_modules, i);
  234. if (strncmp(tmod->name, name, nchar) == 0)
  235. return tmod;
  236. }
  237. return NULL;
  238. }
  239. /* initialize a module */
  240. static int module_init(CONF_MODULE *pmod, const char *name, const char *value,
  241. const CONF *cnf)
  242. {
  243. int ret = 1;
  244. int init_called = 0;
  245. CONF_IMODULE *imod = NULL;
  246. /* Otherwise add initialized module to list */
  247. imod = OPENSSL_malloc(sizeof(*imod));
  248. if (imod == NULL)
  249. goto err;
  250. imod->pmod = pmod;
  251. imod->name = OPENSSL_strdup(name);
  252. imod->value = OPENSSL_strdup(value);
  253. imod->usr_data = NULL;
  254. if (!imod->name || !imod->value)
  255. goto memerr;
  256. /* Try to initialize module */
  257. if (pmod->init) {
  258. ret = pmod->init(imod, cnf);
  259. init_called = 1;
  260. /* Error occurred, exit */
  261. if (ret <= 0)
  262. goto err;
  263. }
  264. if (initialized_modules == NULL) {
  265. initialized_modules = sk_CONF_IMODULE_new_null();
  266. if (!initialized_modules) {
  267. CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
  268. goto err;
  269. }
  270. }
  271. if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
  272. CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
  273. goto err;
  274. }
  275. pmod->links++;
  276. return ret;
  277. err:
  278. /* We've started the module so we'd better finish it */
  279. if (pmod->finish && init_called)
  280. pmod->finish(imod);
  281. memerr:
  282. if (imod) {
  283. OPENSSL_free(imod->name);
  284. OPENSSL_free(imod->value);
  285. OPENSSL_free(imod);
  286. }
  287. return -1;
  288. }
  289. /*
  290. * Unload any dynamic modules that have a link count of zero: i.e. have no
  291. * active initialized modules. If 'all' is set then all modules are unloaded
  292. * including static ones.
  293. */
  294. void CONF_modules_unload(int all)
  295. {
  296. int i;
  297. CONF_MODULE *md;
  298. CONF_modules_finish();
  299. /* unload modules in reverse order */
  300. for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
  301. md = sk_CONF_MODULE_value(supported_modules, i);
  302. /* If static or in use and 'all' not set ignore it */
  303. if (((md->links > 0) || !md->dso) && !all)
  304. continue;
  305. /* Since we're working in reverse this is OK */
  306. (void)sk_CONF_MODULE_delete(supported_modules, i);
  307. module_free(md);
  308. }
  309. if (sk_CONF_MODULE_num(supported_modules) == 0) {
  310. sk_CONF_MODULE_free(supported_modules);
  311. supported_modules = NULL;
  312. }
  313. }
  314. /* unload a single module */
  315. static void module_free(CONF_MODULE *md)
  316. {
  317. DSO_free(md->dso);
  318. OPENSSL_free(md->name);
  319. OPENSSL_free(md);
  320. }
  321. /* finish and free up all modules instances */
  322. void CONF_modules_finish(void)
  323. {
  324. CONF_IMODULE *imod;
  325. while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
  326. imod = sk_CONF_IMODULE_pop(initialized_modules);
  327. module_finish(imod);
  328. }
  329. sk_CONF_IMODULE_free(initialized_modules);
  330. initialized_modules = NULL;
  331. }
  332. /* finish a module instance */
  333. static void module_finish(CONF_IMODULE *imod)
  334. {
  335. if (!imod)
  336. return;
  337. if (imod->pmod->finish)
  338. imod->pmod->finish(imod);
  339. imod->pmod->links--;
  340. OPENSSL_free(imod->name);
  341. OPENSSL_free(imod->value);
  342. OPENSSL_free(imod);
  343. }
  344. /* Add a static module to OpenSSL */
  345. int CONF_module_add(const char *name, conf_init_func *ifunc,
  346. conf_finish_func *ffunc)
  347. {
  348. if (module_add(NULL, name, ifunc, ffunc))
  349. return 1;
  350. else
  351. return 0;
  352. }
  353. void conf_modules_free_int(void)
  354. {
  355. CONF_modules_finish();
  356. CONF_modules_unload(1);
  357. }
  358. /* Utility functions */
  359. const char *CONF_imodule_get_name(const CONF_IMODULE *md)
  360. {
  361. return md->name;
  362. }
  363. const char *CONF_imodule_get_value(const CONF_IMODULE *md)
  364. {
  365. return md->value;
  366. }
  367. void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
  368. {
  369. return md->usr_data;
  370. }
  371. void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
  372. {
  373. md->usr_data = usr_data;
  374. }
  375. CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
  376. {
  377. return md->pmod;
  378. }
  379. unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
  380. {
  381. return md->flags;
  382. }
  383. void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
  384. {
  385. md->flags = flags;
  386. }
  387. void *CONF_module_get_usr_data(CONF_MODULE *pmod)
  388. {
  389. return pmod->usr_data;
  390. }
  391. void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
  392. {
  393. pmod->usr_data = usr_data;
  394. }
  395. /* Return default config file name */
  396. char *CONF_get1_default_config_file(void)
  397. {
  398. char *file, *sep = "";
  399. int len;
  400. if ((file = ossl_safe_getenv("OPENSSL_CONF")) != NULL)
  401. return OPENSSL_strdup(file);
  402. len = strlen(X509_get_default_cert_area());
  403. #ifndef OPENSSL_SYS_VMS
  404. len++;
  405. sep = "/";
  406. #endif
  407. len += strlen(OPENSSL_CONF);
  408. file = OPENSSL_malloc(len + 1);
  409. if (file == NULL)
  410. return NULL;
  411. BIO_snprintf(file, len + 1, "%s%s%s", X509_get_default_cert_area(),
  412. sep, OPENSSL_CONF);
  413. return file;
  414. }
  415. /*
  416. * This function takes a list separated by 'sep' and calls the callback
  417. * function giving the start and length of each member optionally stripping
  418. * leading and trailing whitespace. This can be used to parse comma separated
  419. * lists for example.
  420. */
  421. int CONF_parse_list(const char *list_, int sep, int nospc,
  422. int (*list_cb) (const char *elem, int len, void *usr),
  423. void *arg)
  424. {
  425. int ret;
  426. const char *lstart, *tmpend, *p;
  427. if (list_ == NULL) {
  428. CONFerr(CONF_F_CONF_PARSE_LIST, CONF_R_LIST_CANNOT_BE_NULL);
  429. return 0;
  430. }
  431. lstart = list_;
  432. for (;;) {
  433. if (nospc) {
  434. while (*lstart && isspace((unsigned char)*lstart))
  435. lstart++;
  436. }
  437. p = strchr(lstart, sep);
  438. if (p == lstart || !*lstart)
  439. ret = list_cb(NULL, 0, arg);
  440. else {
  441. if (p)
  442. tmpend = p - 1;
  443. else
  444. tmpend = lstart + strlen(lstart) - 1;
  445. if (nospc) {
  446. while (isspace((unsigned char)*tmpend))
  447. tmpend--;
  448. }
  449. ret = list_cb(lstart, tmpend - lstart + 1, arg);
  450. }
  451. if (ret <= 0)
  452. return ret;
  453. if (p == NULL)
  454. return 1;
  455. lstart = p + 1;
  456. }
  457. }