]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/conf/conf_mod.c
Remove unused parameters from internal functions
[thirdparty/openssl.git] / crypto / conf / conf_mod.c
CommitLineData
0f113f3e
MC
1/*
2 * Written by Stephen Henson (steve@openssl.org) for the OpenSSL project
3 * 2001.
bc37d996
DSH
4 */
5/* ====================================================================
6 * Copyright (c) 2001 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
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
bc37d996
DSH
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>
df5eaa8a 60#include <ctype.h>
bc37d996 61#include <openssl/crypto.h>
b39fc560 62#include "internal/cryptlib.h"
bc37d996
DSH
63#include <openssl/conf.h>
64#include <openssl/dso.h>
65#include <openssl/x509.h>
66
bc37d996
DSH
67#define DSO_mod_init_name "OPENSSL_init"
68#define DSO_mod_finish_name "OPENSSL_finish"
69
0f113f3e
MC
70/*
71 * This structure contains a data about supported modules. entries in this
72 * table correspond to either dynamic or static modules.
bc37d996
DSH
73 */
74
0f113f3e
MC
75struct conf_module_st {
76 /* DSO of this module or NULL if static */
77 DSO *dso;
78 /* Name of the module */
79 char *name;
80 /* Init function */
81 conf_init_func *init;
82 /* Finish function */
83 conf_finish_func *finish;
84 /* Number of successfully initialized modules */
85 int links;
86 void *usr_data;
87};
88
89/*
90 * This structure contains information about modules that have been
91 * successfully initialized. There may be more than one entry for a given
92 * module.
bc37d996
DSH
93 */
94
0f113f3e
MC
95struct conf_imodule_st {
96 CONF_MODULE *pmod;
97 char *name;
98 char *value;
99 unsigned long flags;
100 void *usr_data;
101};
bc37d996
DSH
102
103static STACK_OF(CONF_MODULE) *supported_modules = NULL;
104static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
105
106static void module_free(CONF_MODULE *md);
107static void module_finish(CONF_IMODULE *imod);
9dd5ae65 108static int module_run(const CONF *cnf, char *name, char *value,
0f113f3e 109 unsigned long flags);
cca28b29 110static CONF_MODULE *module_add(DSO *dso, const char *name,
0f113f3e
MC
111 conf_init_func *ifunc,
112 conf_finish_func *ffunc);
bc37d996 113static CONF_MODULE *module_find(char *name);
9dd5ae65 114static int module_init(CONF_MODULE *pmod, char *name, char *value,
0f113f3e 115 const CONF *cnf);
a773b52a 116static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value);
bc37d996
DSH
117
118/* Main function: load modules from a CONF structure */
119
9dd5ae65 120int CONF_modules_load(const CONF *cnf, const char *appname,
0f113f3e
MC
121 unsigned long flags)
122{
123 STACK_OF(CONF_VALUE) *values;
124 CONF_VALUE *vl;
125 char *vsection = NULL;
bc37d996 126
0f113f3e 127 int ret, i;
bc37d996 128
0f113f3e
MC
129 if (!cnf)
130 return 1;
bc37d996 131
0f113f3e
MC
132 if (appname)
133 vsection = NCONF_get_string(cnf, NULL, appname);
bc37d996 134
0f113f3e
MC
135 if (!appname || (!vsection && (flags & CONF_MFLAGS_DEFAULT_SECTION)))
136 vsection = NCONF_get_string(cnf, NULL, "openssl_conf");
bc37d996 137
0f113f3e
MC
138 if (!vsection) {
139 ERR_clear_error();
140 return 1;
141 }
bc37d996 142
0f113f3e 143 values = NCONF_get_section(cnf, vsection);
bc37d996 144
0f113f3e
MC
145 if (!values)
146 return 0;
bc37d996 147
0f113f3e
MC
148 for (i = 0; i < sk_CONF_VALUE_num(values); i++) {
149 vl = sk_CONF_VALUE_value(values, i);
150 ret = module_run(cnf, vl->name, vl->value, flags);
151 if (ret <= 0)
152 if (!(flags & CONF_MFLAGS_IGNORE_ERRORS))
153 return ret;
154 }
bc37d996 155
0f113f3e 156 return 1;
bc37d996 157
0f113f3e 158}
bc37d996 159
9dd5ae65 160int CONF_modules_load_file(const char *filename, const char *appname,
0f113f3e
MC
161 unsigned long flags)
162{
163 char *file = NULL;
164 CONF *conf = NULL;
165 int ret = 0;
166 conf = NCONF_new(NULL);
90945fa3 167 if (conf == NULL)
0f113f3e
MC
168 goto err;
169
170 if (filename == NULL) {
171 file = CONF_get1_default_config_file();
172 if (!file)
173 goto err;
174 } else
175 file = (char *)filename;
176
177 if (NCONF_load(conf, file, NULL) <= 0) {
178 if ((flags & CONF_MFLAGS_IGNORE_MISSING_FILE) &&
179 (ERR_GET_REASON(ERR_peek_last_error()) == CONF_R_NO_SUCH_FILE)) {
180 ERR_clear_error();
181 ret = 1;
182 }
183 goto err;
184 }
185
186 ret = CONF_modules_load(conf, appname, flags);
187
188 err:
189 if (filename == NULL)
190 OPENSSL_free(file);
191 NCONF_free(conf);
192
193 return ret;
194}
bc37d996 195
9dd5ae65 196static int module_run(const CONF *cnf, char *name, char *value,
0f113f3e
MC
197 unsigned long flags)
198{
199 CONF_MODULE *md;
200 int ret;
201
202 md = module_find(name);
203
204 /* Module not found: try to load DSO */
205 if (!md && !(flags & CONF_MFLAGS_NO_DSO))
a773b52a 206 md = module_load_dso(cnf, name, value);
0f113f3e
MC
207
208 if (!md) {
209 if (!(flags & CONF_MFLAGS_SILENT)) {
210 CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
211 ERR_add_error_data(2, "module=", name);
212 }
213 return -1;
214 }
215
216 ret = module_init(md, name, value, cnf);
217
218 if (ret <= 0) {
219 if (!(flags & CONF_MFLAGS_SILENT)) {
220 char rcode[DECIMAL_SIZE(ret) + 1];
221 CONFerr(CONF_F_MODULE_RUN, CONF_R_MODULE_INITIALIZATION_ERROR);
222 BIO_snprintf(rcode, sizeof rcode, "%-8d", ret);
223 ERR_add_error_data(6, "module=", name, ", value=", value,
224 ", retcode=", rcode);
225 }
226 }
227
228 return ret;
229}
bc37d996
DSH
230
231/* Load a module from a DSO */
a773b52a 232static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value)
0f113f3e
MC
233{
234 DSO *dso = NULL;
235 conf_init_func *ifunc;
236 conf_finish_func *ffunc;
237 char *path = NULL;
238 int errcode = 0;
239 CONF_MODULE *md;
240 /* Look for alternative path in module section */
241 path = NCONF_get_string(cnf, value, "path");
242 if (!path) {
243 ERR_clear_error();
244 path = name;
245 }
246 dso = DSO_load(NULL, path, NULL, 0);
247 if (!dso) {
248 errcode = CONF_R_ERROR_LOADING_DSO;
249 goto err;
250 }
251 ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
252 if (!ifunc) {
253 errcode = CONF_R_MISSING_INIT_FUNCTION;
254 goto err;
255 }
256 ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
257 /* All OK, add module */
258 md = module_add(dso, name, ifunc, ffunc);
259
260 if (!md)
261 goto err;
262
263 return md;
264
265 err:
efa7dd64 266 DSO_free(dso);
0f113f3e
MC
267 CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
268 ERR_add_error_data(4, "module=", name, ", path=", path);
269 return NULL;
270}
bc37d996
DSH
271
272/* add module to list */
cca28b29 273static CONF_MODULE *module_add(DSO *dso, const char *name,
0f113f3e
MC
274 conf_init_func *ifunc, conf_finish_func *ffunc)
275{
276 CONF_MODULE *tmod = NULL;
277 if (supported_modules == NULL)
278 supported_modules = sk_CONF_MODULE_new_null();
279 if (supported_modules == NULL)
280 return NULL;
64b25758 281 tmod = OPENSSL_zalloc(sizeof(*tmod));
0f113f3e
MC
282 if (tmod == NULL)
283 return NULL;
284
285 tmod->dso = dso;
7644a9ae 286 tmod->name = OPENSSL_strdup(name);
0f113f3e
MC
287 tmod->init = ifunc;
288 tmod->finish = ffunc;
0f113f3e
MC
289
290 if (!sk_CONF_MODULE_push(supported_modules, tmod)) {
291 OPENSSL_free(tmod);
292 return NULL;
293 }
294
295 return tmod;
296}
297
298/*
299 * Find a module from the list. We allow module names of the form
300 * modname.XXXX to just search for modname to allow the same module to be
301 * initialized more than once.
bc37d996
DSH
302 */
303
304static CONF_MODULE *module_find(char *name)
0f113f3e
MC
305{
306 CONF_MODULE *tmod;
307 int i, nchar;
308 char *p;
309 p = strrchr(name, '.');
bc37d996 310
0f113f3e
MC
311 if (p)
312 nchar = p - name;
313 else
314 nchar = strlen(name);
bc37d996 315
0f113f3e
MC
316 for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++) {
317 tmod = sk_CONF_MODULE_value(supported_modules, i);
86885c28 318 if (strncmp(tmod->name, name, nchar) == 0)
0f113f3e
MC
319 return tmod;
320 }
bc37d996 321
0f113f3e 322 return NULL;
bc37d996 323
0f113f3e 324}
bc37d996
DSH
325
326/* initialize a module */
9dd5ae65 327static int module_init(CONF_MODULE *pmod, char *name, char *value,
0f113f3e
MC
328 const CONF *cnf)
329{
330 int ret = 1;
331 int init_called = 0;
332 CONF_IMODULE *imod = NULL;
333
334 /* Otherwise add initialized module to list */
b4faea50 335 imod = OPENSSL_malloc(sizeof(*imod));
90945fa3 336 if (imod == NULL)
0f113f3e
MC
337 goto err;
338
339 imod->pmod = pmod;
7644a9ae
RS
340 imod->name = OPENSSL_strdup(name);
341 imod->value = OPENSSL_strdup(value);
0f113f3e
MC
342 imod->usr_data = NULL;
343
344 if (!imod->name || !imod->value)
345 goto memerr;
346
347 /* Try to initialize module */
348 if (pmod->init) {
349 ret = pmod->init(imod, cnf);
350 init_called = 1;
351 /* Error occurred, exit */
352 if (ret <= 0)
353 goto err;
354 }
355
356 if (initialized_modules == NULL) {
357 initialized_modules = sk_CONF_IMODULE_new_null();
358 if (!initialized_modules) {
359 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
360 goto err;
361 }
362 }
363
364 if (!sk_CONF_IMODULE_push(initialized_modules, imod)) {
365 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
366 goto err;
367 }
368
369 pmod->links++;
370
371 return ret;
372
373 err:
374
375 /* We've started the module so we'd better finish it */
376 if (pmod->finish && init_called)
377 pmod->finish(imod);
378
379 memerr:
380 if (imod) {
b548a1f1
RS
381 OPENSSL_free(imod->name);
382 OPENSSL_free(imod->value);
0f113f3e
MC
383 OPENSSL_free(imod);
384 }
385
386 return -1;
387
388}
389
390/*
391 * Unload any dynamic modules that have a link count of zero: i.e. have no
392 * active initialized modules. If 'all' is set then all modules are unloaded
393 * including static ones.
bc37d996
DSH
394 */
395
396void CONF_modules_unload(int all)
0f113f3e
MC
397{
398 int i;
399 CONF_MODULE *md;
400 CONF_modules_finish();
401 /* unload modules in reverse order */
402 for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--) {
403 md = sk_CONF_MODULE_value(supported_modules, i);
404 /* If static or in use and 'all' not set ignore it */
405 if (((md->links > 0) || !md->dso) && !all)
406 continue;
407 /* Since we're working in reverse this is OK */
408 (void)sk_CONF_MODULE_delete(supported_modules, i);
409 module_free(md);
410 }
411 if (sk_CONF_MODULE_num(supported_modules) == 0) {
412 sk_CONF_MODULE_free(supported_modules);
413 supported_modules = NULL;
414 }
415}
bc37d996
DSH
416
417/* unload a single module */
418static void module_free(CONF_MODULE *md)
0f113f3e 419{
efa7dd64 420 DSO_free(md->dso);
0f113f3e
MC
421 OPENSSL_free(md->name);
422 OPENSSL_free(md);
423}
bc37d996
DSH
424
425/* finish and free up all modules instances */
426
427void CONF_modules_finish(void)
0f113f3e
MC
428{
429 CONF_IMODULE *imod;
430 while (sk_CONF_IMODULE_num(initialized_modules) > 0) {
431 imod = sk_CONF_IMODULE_pop(initialized_modules);
432 module_finish(imod);
433 }
434 sk_CONF_IMODULE_free(initialized_modules);
435 initialized_modules = NULL;
436}
bc37d996
DSH
437
438/* finish a module instance */
439
440static void module_finish(CONF_IMODULE *imod)
0f113f3e 441{
efa7dd64
RS
442 if (!imod)
443 return;
0f113f3e
MC
444 if (imod->pmod->finish)
445 imod->pmod->finish(imod);
446 imod->pmod->links--;
447 OPENSSL_free(imod->name);
448 OPENSSL_free(imod->value);
449 OPENSSL_free(imod);
450}
bc37d996
DSH
451
452/* Add a static module to OpenSSL */
453
0f113f3e
MC
454int CONF_module_add(const char *name, conf_init_func *ifunc,
455 conf_finish_func *ffunc)
456{
457 if (module_add(NULL, name, ifunc, ffunc))
458 return 1;
459 else
460 return 0;
461}
bc37d996
DSH
462
463void CONF_modules_free(void)
0f113f3e
MC
464{
465 CONF_modules_finish();
466 CONF_modules_unload(1);
467}
bc37d996
DSH
468
469/* Utility functions */
470
9dd5ae65 471const char *CONF_imodule_get_name(const CONF_IMODULE *md)
0f113f3e
MC
472{
473 return md->name;
474}
bc37d996 475
9dd5ae65 476const char *CONF_imodule_get_value(const CONF_IMODULE *md)
0f113f3e
MC
477{
478 return md->value;
479}
bc37d996 480
9dd5ae65 481void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
0f113f3e
MC
482{
483 return md->usr_data;
484}
bc37d996
DSH
485
486void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
0f113f3e
MC
487{
488 md->usr_data = usr_data;
489}
bc37d996 490
9dd5ae65 491CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
0f113f3e
MC
492{
493 return md->pmod;
494}
bc37d996 495
9dd5ae65 496unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
0f113f3e
MC
497{
498 return md->flags;
499}
bc37d996
DSH
500
501void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
0f113f3e
MC
502{
503 md->flags = flags;
504}
bc37d996
DSH
505
506void *CONF_module_get_usr_data(CONF_MODULE *pmod)
0f113f3e
MC
507{
508 return pmod->usr_data;
509}
bc37d996
DSH
510
511void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
0f113f3e
MC
512{
513 pmod->usr_data = usr_data;
514}
bc37d996 515
c9501c22
DSH
516/* Return default config file name */
517
518char *CONF_get1_default_config_file(void)
0f113f3e
MC
519{
520 char *file;
521 int len;
c9501c22 522
0f113f3e
MC
523 file = getenv("OPENSSL_CONF");
524 if (file)
7644a9ae 525 return OPENSSL_strdup(file);
c9501c22 526
0f113f3e 527 len = strlen(X509_get_default_cert_area());
c9501c22 528#ifndef OPENSSL_SYS_VMS
0f113f3e 529 len++;
c9501c22 530#endif
0f113f3e 531 len += strlen(OPENSSL_CONF);
c9501c22 532
0f113f3e 533 file = OPENSSL_malloc(len + 1);
c9501c22 534
90945fa3 535 if (file == NULL)
0f113f3e 536 return NULL;
7644a9ae 537 OPENSSL_strlcpy(file, X509_get_default_cert_area(), len + 1);
c9501c22 538#ifndef OPENSSL_SYS_VMS
7644a9ae 539 OPENSSL_strlcat(file, "/", len + 1);
c9501c22 540#endif
7644a9ae 541 OPENSSL_strlcat(file, OPENSSL_CONF, len + 1);
c9501c22 542
0f113f3e
MC
543 return file;
544}
df5eaa8a 545
0f113f3e
MC
546/*
547 * This function takes a list separated by 'sep' and calls the callback
548 * function giving the start and length of each member optionally stripping
549 * leading and trailing whitespace. This can be used to parse comma separated
550 * lists for example.
df5eaa8a
DSH
551 */
552
3822740c 553int CONF_parse_list(const char *list_, int sep, int nospc,
0f113f3e
MC
554 int (*list_cb) (const char *elem, int len, void *usr),
555 void *arg)
556{
557 int ret;
558 const char *lstart, *tmpend, *p;
559
560 if (list_ == NULL) {
561 CONFerr(CONF_F_CONF_PARSE_LIST, CONF_R_LIST_CANNOT_BE_NULL);
562 return 0;
563 }
564
565 lstart = list_;
566 for (;;) {
567 if (nospc) {
568 while (*lstart && isspace((unsigned char)*lstart))
569 lstart++;
570 }
571 p = strchr(lstart, sep);
572 if (p == lstart || !*lstart)
573 ret = list_cb(NULL, 0, arg);
574 else {
575 if (p)
576 tmpend = p - 1;
577 else
578 tmpend = lstart + strlen(lstart) - 1;
579 if (nospc) {
580 while (isspace((unsigned char)*tmpend))
581 tmpend--;
582 }
583 ret = list_cb(lstart, tmpend - lstart + 1, arg);
584 }
585 if (ret <= 0)
586 return ret;
587 if (p == NULL)
588 return 1;
589 lstart = p + 1;
590 }
591}