conf_lib.c 9.0 KB

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