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