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