conf_lib.c 9.3 KB

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