conf_lib.c 9.7 KB

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