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