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