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