conf_lib.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /* conf_lib.c */
  2. /* Written by Richard Levitte (richard@levitte.org) for the OpenSSL
  3. * project 2000.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include <openssl/crypto.h>
  60. #include <openssl/err.h>
  61. #include <openssl/conf.h>
  62. #include <openssl/conf_api.h>
  63. #include <openssl/lhash.h>
  64. const char CONF_version[]="CONF" OPENSSL_VERSION_PTEXT;
  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. /* The following section contains the "CONF classic" functions,
  75. rewritten in terms of the new CONF interface. */
  76. int CONF_set_default_method(CONF_METHOD *meth)
  77. {
  78. default_CONF_method = meth;
  79. return 1;
  80. }
  81. LHASH_OF(CONF_VALUE) *CONF_load(LHASH_OF(CONF_VALUE) *conf, const char *file,
  82. long *eline)
  83. {
  84. LHASH_OF(CONF_VALUE) *ltmp;
  85. BIO *in=NULL;
  86. #ifdef OPENSSL_SYS_VMS
  87. in=BIO_new_file(file, "r");
  88. #else
  89. in=BIO_new_file(file, "rb");
  90. #endif
  91. if (in == NULL)
  92. {
  93. CONFerr(CONF_F_CONF_LOAD,ERR_R_SYS_LIB);
  94. return NULL;
  95. }
  96. ltmp = CONF_load_bio(conf, in, eline);
  97. BIO_free(in);
  98. return ltmp;
  99. }
  100. #ifndef OPENSSL_NO_FP_API
  101. LHASH_OF(CONF_VALUE) *CONF_load_fp(LHASH_OF(CONF_VALUE) *conf, FILE *fp,
  102. long *eline)
  103. {
  104. BIO *btmp;
  105. LHASH_OF(CONF_VALUE) *ltmp;
  106. if(!(btmp = BIO_new_fp(fp, BIO_NOCLOSE))) {
  107. CONFerr(CONF_F_CONF_LOAD_FP,ERR_R_BUF_LIB);
  108. return NULL;
  109. }
  110. ltmp = CONF_load_bio(conf, btmp, eline);
  111. BIO_free(btmp);
  112. return ltmp;
  113. }
  114. #endif
  115. LHASH_OF(CONF_VALUE) *CONF_load_bio(LHASH_OF(CONF_VALUE) *conf, BIO *bp,
  116. long *eline)
  117. {
  118. CONF ctmp;
  119. int ret;
  120. CONF_set_nconf(&ctmp, conf);
  121. ret = NCONF_load_bio(&ctmp, bp, eline);
  122. if (ret)
  123. return ctmp.data;
  124. return NULL;
  125. }
  126. STACK_OF(CONF_VALUE) *CONF_get_section(LHASH_OF(CONF_VALUE) *conf,
  127. const char *section)
  128. {
  129. if (conf == NULL)
  130. {
  131. return NULL;
  132. }
  133. else
  134. {
  135. CONF ctmp;
  136. CONF_set_nconf(&ctmp, conf);
  137. return NCONF_get_section(&ctmp, section);
  138. }
  139. }
  140. char *CONF_get_string(LHASH_OF(CONF_VALUE) *conf,const char *group,
  141. const char *name)
  142. {
  143. if (conf == NULL)
  144. {
  145. return NCONF_get_string(NULL, group, name);
  146. }
  147. else
  148. {
  149. CONF ctmp;
  150. CONF_set_nconf(&ctmp, conf);
  151. return NCONF_get_string(&ctmp, group, name);
  152. }
  153. }
  154. long CONF_get_number(LHASH_OF(CONF_VALUE) *conf,const char *group,
  155. const char *name)
  156. {
  157. int status;
  158. long result = 0;
  159. if (conf == NULL)
  160. {
  161. status = NCONF_get_number_e(NULL, group, name, &result);
  162. }
  163. else
  164. {
  165. CONF ctmp;
  166. CONF_set_nconf(&ctmp, conf);
  167. status = NCONF_get_number_e(&ctmp, group, name, &result);
  168. }
  169. if (status == 0)
  170. {
  171. /* This function does not believe in errors... */
  172. ERR_clear_error();
  173. }
  174. return result;
  175. }
  176. void CONF_free(LHASH_OF(CONF_VALUE) *conf)
  177. {
  178. CONF ctmp;
  179. CONF_set_nconf(&ctmp, conf);
  180. NCONF_free_data(&ctmp);
  181. }
  182. #ifndef OPENSSL_NO_FP_API
  183. int CONF_dump_fp(LHASH_OF(CONF_VALUE) *conf, FILE *out)
  184. {
  185. BIO *btmp;
  186. int ret;
  187. if(!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
  188. CONFerr(CONF_F_CONF_DUMP_FP,ERR_R_BUF_LIB);
  189. return 0;
  190. }
  191. ret = CONF_dump_bio(conf, btmp);
  192. BIO_free(btmp);
  193. return ret;
  194. }
  195. #endif
  196. int CONF_dump_bio(LHASH_OF(CONF_VALUE) *conf, BIO *out)
  197. {
  198. CONF ctmp;
  199. CONF_set_nconf(&ctmp, conf);
  200. return NCONF_dump_bio(&ctmp, out);
  201. }
  202. /* The following section contains the "New CONF" functions. They are
  203. completely centralised around a new CONF structure that may contain
  204. basically anything, but at least a method pointer and a table of data.
  205. These functions are also written in terms of the bridge functions used
  206. by the "CONF classic" functions, for consistency. */
  207. CONF *NCONF_new(CONF_METHOD *meth)
  208. {
  209. CONF *ret;
  210. if (meth == NULL)
  211. meth = NCONF_default();
  212. ret = meth->create(meth);
  213. if (ret == NULL)
  214. {
  215. CONFerr(CONF_F_NCONF_NEW,ERR_R_MALLOC_FAILURE);
  216. return(NULL);
  217. }
  218. return ret;
  219. }
  220. void NCONF_free(CONF *conf)
  221. {
  222. if (conf == NULL)
  223. return;
  224. conf->meth->destroy(conf);
  225. }
  226. void NCONF_free_data(CONF *conf)
  227. {
  228. if (conf == NULL)
  229. return;
  230. conf->meth->destroy_data(conf);
  231. }
  232. int NCONF_load(CONF *conf, const char *file, long *eline)
  233. {
  234. if (conf == NULL)
  235. {
  236. CONFerr(CONF_F_NCONF_LOAD,CONF_R_NO_CONF);
  237. return 0;
  238. }
  239. return conf->meth->load(conf, file, eline);
  240. }
  241. #ifndef OPENSSL_NO_FP_API
  242. int NCONF_load_fp(CONF *conf, FILE *fp,long *eline)
  243. {
  244. BIO *btmp;
  245. int ret;
  246. if(!(btmp = BIO_new_fp(fp, BIO_NOCLOSE)))
  247. {
  248. CONFerr(CONF_F_NCONF_LOAD_FP,ERR_R_BUF_LIB);
  249. return 0;
  250. }
  251. ret = NCONF_load_bio(conf, btmp, eline);
  252. BIO_free(btmp);
  253. return ret;
  254. }
  255. #endif
  256. int NCONF_load_bio(CONF *conf, BIO *bp,long *eline)
  257. {
  258. if (conf == NULL)
  259. {
  260. CONFerr(CONF_F_NCONF_LOAD_BIO,CONF_R_NO_CONF);
  261. return 0;
  262. }
  263. return conf->meth->load_bio(conf, bp, eline);
  264. }
  265. STACK_OF(CONF_VALUE) *NCONF_get_section(const CONF *conf,const char *section)
  266. {
  267. if (conf == NULL)
  268. {
  269. CONFerr(CONF_F_NCONF_GET_SECTION,CONF_R_NO_CONF);
  270. return NULL;
  271. }
  272. if (section == NULL)
  273. {
  274. CONFerr(CONF_F_NCONF_GET_SECTION,CONF_R_NO_SECTION);
  275. return NULL;
  276. }
  277. return _CONF_get_section_values(conf, section);
  278. }
  279. char *NCONF_get_string(const CONF *conf,const char *group,const char *name)
  280. {
  281. char *s = _CONF_get_string(conf, group, name);
  282. /* Since we may get a value from an environment variable even
  283. if conf is NULL, let's check the value first */
  284. if (s) return s;
  285. if (conf == NULL)
  286. {
  287. CONFerr(CONF_F_NCONF_GET_STRING,
  288. CONF_R_NO_CONF_OR_ENVIRONMENT_VARIABLE);
  289. return NULL;
  290. }
  291. CONFerr(CONF_F_NCONF_GET_STRING,
  292. CONF_R_NO_VALUE);
  293. ERR_add_error_data(4,"group=",group," name=",name);
  294. return NULL;
  295. }
  296. int NCONF_get_number_e(const CONF *conf,const char *group,const char *name,
  297. long *result)
  298. {
  299. char *str;
  300. if (result == NULL)
  301. {
  302. CONFerr(CONF_F_NCONF_GET_NUMBER_E,ERR_R_PASSED_NULL_PARAMETER);
  303. return 0;
  304. }
  305. str = NCONF_get_string(conf,group,name);
  306. if (str == NULL)
  307. return 0;
  308. for (*result = 0;conf->meth->is_number(conf, *str);)
  309. {
  310. *result = (*result)*10 + conf->meth->to_int(conf, *str);
  311. str++;
  312. }
  313. return 1;
  314. }
  315. #ifndef OPENSSL_NO_FP_API
  316. int NCONF_dump_fp(const CONF *conf, FILE *out)
  317. {
  318. BIO *btmp;
  319. int ret;
  320. if(!(btmp = BIO_new_fp(out, BIO_NOCLOSE))) {
  321. CONFerr(CONF_F_NCONF_DUMP_FP,ERR_R_BUF_LIB);
  322. return 0;
  323. }
  324. ret = NCONF_dump_bio(conf, btmp);
  325. BIO_free(btmp);
  326. return ret;
  327. }
  328. #endif
  329. int NCONF_dump_bio(const CONF *conf, BIO *out)
  330. {
  331. if (conf == NULL)
  332. {
  333. CONFerr(CONF_F_NCONF_DUMP_BIO,CONF_R_NO_CONF);
  334. return 0;
  335. }
  336. return conf->meth->dump(conf, out);
  337. }
  338. /* This function should be avoided */
  339. #if 0
  340. long NCONF_get_number(CONF *conf,char *group,char *name)
  341. {
  342. int status;
  343. long ret=0;
  344. status = NCONF_get_number_e(conf, group, name, &ret);
  345. if (status == 0)
  346. {
  347. /* This function does not believe in errors... */
  348. ERR_get_error();
  349. }
  350. return ret;
  351. }
  352. #endif