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