conf_lib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* conf_lib.c */
  2. /*
  3. * Written by Richard Levitte (richard@levitte.org) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000 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 <openssl/crypto.h>
  61. #include <openssl/err.h>
  62. #include <openssl/conf.h>
  63. #include <openssl/conf_api.h>
  64. #include <openssl/lhash.h>
  65. const char CONF_version[] = "CONF" OPENSSL_VERSION_PTEXT;
  66. static CONF_METHOD *default_CONF_method = NULL;
  67. /* Init a 'CONF' structure from an old LHASH */
  68. void CONF_set_nconf(CONF *conf, LHASH_OF(CONF_VALUE) *hash)
  69. {
  70. if (default_CONF_method == NULL)
  71. default_CONF_method = NCONF_default();
  72. default_CONF_method->init(conf);
  73. conf->data = hash;
  74. }
  75. /*
  76. * The following section contains the "CONF classic" functions, rewritten in
  77. * terms of the new CONF interface.
  78. */
  79. int CONF_set_default_method(CONF_METHOD *meth)
  80. {
  81. default_CONF_method = meth;
  82. return 1;
  83. }
  84. LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
  85. long *eline)
  86. {
  87. LHASH_OF(CONF_VALUE) *ltmp;
  88. BIO *in = NULL;
  89. #ifdef OPENSSL_SYS_VMS
  90. in = BIO_new_file(file, "r");
  91. #else
  92. in = BIO_new_file(file, "rb");
  93. #endif
  94. if (in == NULL) {
  95. CONFerr(CONF_F_CONF_LOAD, ERR_R_SYS_LIB);
  96. return NULL;
  97. }
  98. ltmp = CONF_load_bio(conf, in, eline);
  99. BIO_free(in);
  100. return ltmp;
  101. }
  102. #ifndef OPENSSL_NO_FP_API
  103. LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
  104. long *eline)
  105. {
  106. BIO *btmp;
  107. LHASH_OF(CONF_VALUE) *ltmp;
  108. if (!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) {
  109. CONFerr(CONF_F_CONF_LOAD_FP, ERR_R_BUF_LIB);
  110. return NULL;
  111. }
  112. ltmp = CONF_load_bio(conf, btmp, eline);
  113. BIO_free(btmp);
  114. return ltmp;
  115. }
  116. #endif
  117. LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
  118. long *eline)
  119. {
  120. CONF ctmp;
  121. int ret;
  122. CONF_set_nconf(&ctmp, conf);
  123. ret = NCONF_load_bio(&ctmp, bp, eline);
  124. if (ret)
  125. return ctmp.data;
  126. return NULL;
  127. }
  128. STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
  129. const char *section)
  130. {
  131. if (conf == NULL) {
  132. return NULL;
  133. } else {
  134. CONF ctmp;
  135. CONF_set_nconf(&ctmp, conf);
  136. return NCONF_get_section(&ctmp, section);
  137. }
  138. }
  139. char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf, const char *group,
  140. const char *name)
  141. {
  142. if (conf == NULL) {
  143. return NCONF_get_string(NULL, group, name);
  144. } else {
  145. CONF ctmp;
  146. CONF_set_nconf(&ctmp, conf);
  147. return NCONF_get_string(&ctmp, group, name);
  148. }
  149. }
  150. long CONF_get_number(LHASH_OF(CONF_VALUE) *conf, const char *group,
  151. const char *name)
  152. {
  153. int status;
  154. long result = 0;
  155. if (conf == NULL) {
  156. status = NCONF_get_number_e(NULL, group, name, &result);
  157. } else {
  158. CONF ctmp;
  159. CONF_set_nconf(&ctmp, conf);
  160. status = NCONF_get_number_e(&ctmp, group, name, &result);
  161. }
  162. if (status == 0) {
  163. /* This function does not believe in errors... */
  164. ERR_clear_error();
  165. }
  166. return result;
  167. }
  168. void CONF_free(LHASH_OF(CONF_VALUE) *conf)
  169. {
  170. CONF ctmp;
  171. CONF_set_nconf(&ctmp, conf);
  172. NCONF_free_data(&ctmp);
  173. }
  174. #ifndef OPENSSL_NO_FP_API
  175. int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
  176. {
  177. BIO *btmp;
  178. int ret;
  179. if (!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
  180. CONFerr(CONF_F_CONF_DUMP_FP, ERR_R_BUF_LIB);
  181. return 0;
  182. }
  183. ret = CONF_dump_bio(conf, btmp);
  184. BIO_free(btmp);
  185. return ret;
  186. }
  187. #endif
  188. int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
  189. {
  190. CONF ctmp;
  191. CONF_set_nconf(&ctmp, conf);
  192. return NCONF_dump_bio(&ctmp, out);
  193. }
  194. /*
  195. * The following section contains the "New CONF" functions. They are
  196. * completely centralised around a new CONF structure that may contain
  197. * basically anything, but at least a method pointer and a table of data.
  198. * These functions are also written in terms of the bridge functions used by
  199. * the "CONF classic" functions, for consistency.
  200. */
  201. CONF *NCONF_new(CONF_METHOD *meth)
  202. {
  203. CONF *ret;
  204. if (meth == NULL)
  205. meth = NCONF_default();
  206. ret = meth->create(meth);
  207. if (ret == NULL) {
  208. CONFerr(CONF_F_NCONF_NEW, ERR_R_MALLOC_FAILURE);
  209. return (NULL);
  210. }
  211. return ret;
  212. }
  213. void NCONF_free(CONF *conf)
  214. {
  215. if (conf == NULL)
  216. return;
  217. conf->meth->destroy(conf);
  218. }
  219. void NCONF_free_data(CONF *conf)
  220. {
  221. if (conf == NULL)
  222. return;
  223. conf->meth->destroy_data(conf);
  224. }
  225. int NCONF_load(CONF *conf, const char *file, long *eline)
  226. {
  227. if (conf == NULL) {
  228. CONFerr(CONF_F_NCONF_LOAD, CONF_R_NO_CONF);
  229. return 0;
  230. }
  231. return conf->meth->load(conf, file, eline);
  232. }
  233. #ifndef OPENSSL_NO_FP_API
  234. int NCONF_load_fp(CONF *conf, FILE *fp, long *eline)
  235. {
  236. BIO *btmp;
  237. int ret;
  238. if (!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) {
  239. CONFerr(CONF_F_NCONF_LOAD_FP, ERR_R_BUF_LIB);
  240. return 0;
  241. }
  242. ret = NCONF_load_bio(conf, btmp, eline);
  243. BIO_free(btmp);
  244. return ret;
  245. }
  246. #endif
  247. int NCONF_load_bio(CONF *conf, BIO *bp, long *eline)
  248. {
  249. if (conf == NULL) {
  250. CONFerr(CONF_F_NCONF_LOAD_BIO, CONF_R_NO_CONF);
  251. return 0;
  252. }
  253. return conf->meth->load_bio(conf, bp, eline);
  254. }
  255. STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf, const char *section)
  256. {
  257. if (conf == NULL) {
  258. CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_CONF);
  259. return NULL;
  260. }
  261. if (section == NULL) {
  262. CONFerr(CONF_F_NCONF_GET_SECTION, CONF_R_NO_SECTION);
  263. return NULL;
  264. }
  265. return _CONF_get_section_values(conf, section);
  266. }
  267. char *NCONF_get_string(const CONF *conf, const char *group, const char *name)
  268. {
  269. char *s = _CONF_get_string(conf, group, name);
  270. /*
  271. * Since we may get a value from an environment variable even if conf is
  272. * NULL, let's check the value first
  273. */
  274. if (s)
  275. return s;
  276. if (conf == NULL) {
  277. CONFerr(CONF_F_NCONF_GET_STRING,
  278. CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
  279. return NULL;
  280. }
  281. CONFerr(CONF_F_NCONF_GET_STRING, CONF_R_NO_VALUE);
  282. ERR_add_error_data(4, "group=", group, " name=", name);
  283. return NULL;
  284. }
  285. int NCONF_get_number_e(const CONF *conf, const char *group, const char *name,
  286. long *result)
  287. {
  288. char *str;
  289. if (result == NULL) {
  290. CONFerr(CONF_F_NCONF_GET_NUMBER_E, ERR_R_PASSED_NULL_PARAMETER);
  291. return 0;
  292. }
  293. str = NCONF_get_string(conf, group, name);
  294. if (str == NULL)
  295. return 0;
  296. for (*result = 0; conf->meth->is_number(conf, *str);) {
  297. *result = (*result) * 10 + conf->meth->to_int(conf, *str);
  298. str++;
  299. }
  300. return 1;
  301. }
  302. #ifndef OPENSSL_NO_FP_API
  303. int NCONF_dump_fp(const CONF *conf, FILE *out)
  304. {
  305. BIO *btmp;
  306. int ret;
  307. if (!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
  308. CONFerr(CONF_F_NCONF_DUMP_FP, ERR_R_BUF_LIB);
  309. return 0;
  310. }
  311. ret = NCONF_dump_bio(conf, btmp);
  312. BIO_free(btmp);
  313. return ret;
  314. }
  315. #endif
  316. int NCONF_dump_bio(const CONF *conf, BIO *out)
  317. {
  318. if (conf == NULL) {
  319. CONFerr(CONF_F_NCONF_DUMP_BIO, CONF_R_NO_CONF);
  320. return 0;
  321. }
  322. return conf->meth->dump(conf, out);
  323. }
  324. /* This function should be avoided */
  325. #if 0
  326. long NCONF_get_number(CONF *conf, char *group, char *name)
  327. {
  328. int status;
  329. long ret = 0;
  330. status = NCONF_get_number_e(conf, group, name, &ret);
  331. if (status == 0) {
  332. /* This function does not believe in errors... */
  333. ERR_get_error();
  334. }
  335. return ret;
  336. }
  337. #endif