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