conf_lib.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright 2000-2016 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 "e_os.h"
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "internal/conf.h"
  13. #include <openssl/crypto.h>
  14. #include <openssl/err.h>
  15. #include <openssl/conf.h>
  16. #include <openssl/conf_api.h>
  17. #include <openssl/lhash.h>
  18. static CONF_METHOD *default_CONF_method = NULL;
  19. /* Init a 'CONF' structure from an old LHASH */
  20. void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
  21. {
  22. if (default_CONF_method == NULL)
  23. default_CONF_method = NCONF_default();
  24. default_CONF_method->init(conf);
  25. conf->data = hash;
  26. }
  27. /*
  28. * The following section contains the "CONF classic" functions, rewritten in
  29. * terms of the new CONF interface.
  30. */
  31. int CONF_set_default_method(CONF_METHOD *meth)
  32. {
  33. default_CONF_method = meth;
  34. return 1;
  35. }
  36. LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
  37. long *eline)
  38. {
  39. LHASH_OF(CONF_VALUE) *ltmp;
  40. BIO *in = NULL;
  41. #ifdef OPENSSL_SYS_VMS
  42. in = BIO_new_file(file, "r");
  43. #else
  44. in = BIO_new_file(file, "rb");
  45. #endif
  46. if (in == NULL) {
  47. CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB);
  48. return NULL;
  49. }
  50. ltmp = CONF_load_bio(conf, in, eline);
  51. BIO_free(in);
  52. return ltmp;
  53. }
  54. #ifndef OPENSSL_NO_STDIO
  55. LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
  56. long *eline)
  57. {
  58. BIO *btmp;
  59. LHASH_OF(CONF_VALUE) *ltmp;
  60. if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  61. CONFerr(CONF_F_CONF_LOAD_FP, ERR_R_BUF_LIB);
  62. return NULL;
  63. }
  64. ltmp = CONF_load_bio(conf, btmp, eline);
  65. BIO_free(btmp);
  66. return ltmp;
  67. }
  68. #endif
  69. LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
  70. long *eline)
  71. {
  72. CONF ctmp;
  73. int ret;
  74. CONF_set_nconf(&ctmp, conf);
  75. ret = NCONF_load_bio(&ctmp, bp, eline);
  76. if (ret)
  77. return ctmp.data;
  78. return NULL;
  79. }
  80. STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
  81. const char *section)
  82. {
  83. if (conf == NULL) {
  84. return NULL;
  85. } else {
  86. CONF ctmp;
  87. CONF_set_nconf(&ctmp, conf);
  88. return NCONF_get_section(&ctmp, section);
  89. }
  90. }
  91. char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
  92. const char *name)
  93. {
  94. if (conf == NULL) {
  95. return NCONF_get_string(NULL, group, name);
  96. } else {
  97. CONF ctmp;
  98. CONF_set_nconf(&ctmp, conf);
  99. return NCONF_get_string(&ctmp, group, name);
  100. }
  101. }
  102. long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
  103. const char *name)
  104. {
  105. int status;
  106. long result = 0;
  107. if (conf == NULL) {
  108. status = NCONF_get_number_e(NULL, group, name, &result);
  109. } else {
  110. CONF ctmp;
  111. CONF_set_nconf(&ctmp, conf);
  112. status = NCONF_get_number_e(&ctmp, group, name, &result);
  113. }
  114. if (status == 0) {
  115. /* This function does not believe in errors... */
  116. ERR_clear_error();
  117. }
  118. return result;
  119. }
  120. void CONF_free(LHASH_OF(CONF_VALUE) *conf)
  121. {
  122. CONF ctmp;
  123. CONF_set_nconf(&ctmp, conf);
  124. NCONF_free_data(&ctmp);
  125. }
  126. #ifndef OPENSSL_NO_STDIO
  127. int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
  128. {
  129. BIO *btmp;
  130. int ret;
  131. if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
  132. CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB);
  133. return 0;
  134. }
  135. ret = CONF_dump_bio(conf, btmp);
  136. BIO_free(btmp);
  137. return ret;
  138. }
  139. #endif
  140. int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
  141. {
  142. CONF ctmp;
  143. CONF_set_nconf(&ctmp, conf);
  144. return NCONF_dump_bio(&ctmp, out);
  145. }
  146. /*
  147. * The following section contains the "New CONF" functions. They are
  148. * completely centralised around a new CONF structure that may contain
  149. * basically anything, but at least a method pointer and a table of data.
  150. * These functions are also written in terms of the bridge functions used by
  151. * the "CONF classic" functions, for consistency.
  152. */
  153. CONF *NCONF_new(CONF_METHOD *meth)
  154. {
  155. CONF *ret;
  156. if (meth == NULL)
  157. meth = NCONF_default();
  158. ret = meth->create(meth);
  159. if (ret == NULL) {
  160. CONFerr(CONF_F_NCONF_NEW, ERR_R_MALLOC_FAILURE);
  161. return NULL;
  162. }
  163. return ret;
  164. }
  165. void NCONF_free(CONF *conf)
  166. {
  167. if (conf == NULL)
  168. return;
  169. conf->meth->destroy(conf);
  170. }
  171. void NCONF_free_data(CONF *conf)
  172. {
  173. if (conf == NULL)
  174. return;
  175. conf->meth->destroy_data(conf);
  176. }
  177. int NCONF_load(CONF *conf, const char *file, long *eline)
  178. {
  179. if (conf == NULL) {
  180. CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF);
  181. return 0;
  182. }
  183. return conf->meth->load(conf, file, eline);
  184. }
  185. #ifndef OPENSSL_NO_STDIO
  186. int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
  187. {
  188. BIO *btmp;
  189. int ret;
  190. if ((btmp = BIO_new_fp(fp, BIO_NOCLOSE)) == NULL) {
  191. CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB);
  192. return 0;
  193. }
  194. ret = NCONF_load_bio(conf, btmp, eline);
  195. BIO_free(btmp);
  196. return ret;
  197. }
  198. #endif
  199. int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
  200. {
  201. if (conf == NULL) {
  202. CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF);
  203. return 0;
  204. }
  205. return conf->meth->load_bio(conf, bp, eline);
  206. }
  207. STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
  208. {
  209. if (conf == NULL) {
  210. CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);
  211. return NULL;
  212. }
  213. if (section == NULL) {
  214. CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);
  215. return NULL;
  216. }
  217. return _CONF_get_section_values(conf, section);
  218. }
  219. char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
  220. {
  221. char *s = _CONF_get_string(conf, group, name);
  222. /*
  223. * Since we may get a value from an environment variable even if conf is
  224. * NULL, let's check the value first
  225. */
  226. if (s)
  227. return s;
  228. if (conf == NULL) {
  229. CONFerr(CONF_F_NCONF_GET_STRING,
  230. CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
  231. return NULL;
  232. }
  233. CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE);
  234. ERR_add_error_data(4, "group=", group, " name=", name);
  235. return NULL;
  236. }
  237. int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
  238. long *result)
  239. {
  240. char *str;
  241. if (result == NULL) {
  242. CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER);
  243. return 0;
  244. }
  245. str = NCONF_get_string(conf, group, name);
  246. if (str == NULL)
  247. return 0;
  248. for (*result = 0; conf->meth->is_number(conf, *str);) {
  249. *result = (*result) * 10 + conf->meth->to_int(conf, *str);
  250. str++;
  251. }
  252. return 1;
  253. }
  254. #ifndef OPENSSL_NO_STDIO
  255. int NCONF_dump_fp(const CONF *conf, FILE *out)
  256. {
  257. BIO *btmp;
  258. int ret;
  259. if ((btmp = BIO_new_fp(out, BIO_NOCLOSE)) == NULL) {
  260. CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB);
  261. return 0;
  262. }
  263. ret = NCONF_dump_bio(conf, btmp);
  264. BIO_free(btmp);
  265. return ret;
  266. }
  267. #endif
  268. int NCONF_dump_bio(const CONF *conf, BIO *out)
  269. {
  270. if (conf == NULL) {
  271. CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF);
  272. return 0;
  273. }
  274. return conf->meth->dump(conf, out);
  275. }
  276. /*
  277. * These routines call the C malloc/free, to avoid intermixing with
  278. * OpenSSL function pointers before the library is initialized.
  279. */
  280. OPENSSL_INIT_SETTINGS *OPENSSL_INIT_new(void)
  281. {
  282. OPENSSL_INIT_SETTINGS *ret = malloc(sizeof(*ret));
  283. if (ret != NULL)
  284. memset(ret, 0, sizeof(*ret));
  285. return ret;
  286. }
  287. #ifndef OPENSSL_NO_STDIO
  288. int OPENSSL_INIT_set_config_appname(OPENSSL_INIT_SETTINGS *settings,
  289. const char *appname)
  290. {
  291. char *newappname = NULL;
  292. if (appname != NULL) {
  293. newappname = strdup(appname);
  294. if (newappname == NULL)
  295. return 0;
  296. }
  297. free(settings->appname);
  298. settings->appname = newappname;
  299. return 1;
  300. }
  301. #endif
  302. void OPENSSL_INIT_free(OPENSSL_INIT_SETTINGS *settings)
  303. {
  304. free(settings->appname);
  305. free(settings);
  306. }