100d72ed03a46de3035347a348765f28aa47f1c1
[openssl.git] / crypto / conf / conf_mod.c
1 /* conf_mod.c */
2 /* Written by Stephen Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 2001.
4  */
5 /* ====================================================================
6  * Copyright (c) 2001 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 #include <stdio.h>
60 #include <ctype.h>
61 #include <openssl/crypto.h>
62 #include "cryptlib.h"
63 #include <openssl/conf.h>
64 #include <openssl/dso.h>
65 #include <openssl/x509.h>
66
67
68 #define DSO_mod_init_name "OPENSSL_init"
69 #define DSO_mod_finish_name "OPENSSL_finish"
70
71
72 /* This structure contains a data about supported modules.
73  * entries in this table correspond to either dynamic or
74  * static modules.
75  */
76
77 struct conf_module_st
78         {
79         /* DSO of this module or NULL if static */
80         DSO *dso;
81         /* Name of the module */
82         char *name;
83         /* Init function */
84         conf_init_func *init; 
85         /* Finish function */
86         conf_finish_func *finish;
87         /* Number of successfully initialized modules */
88         int links;
89         void *usr_data;
90         };
91
92
93 /* This structure contains information about modules that have been
94  * successfully initialized. There may be more than one entry for a
95  * given module.
96  */
97
98 struct conf_imodule_st
99         {
100         CONF_MODULE *pmod;
101         char *name;
102         char *value;
103         unsigned long flags;
104         void *usr_data;
105         };
106
107 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
108 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
109
110 static void module_free(CONF_MODULE *md);
111 static void module_finish(CONF_IMODULE *imod);
112 static int module_run(const CONF *cnf, char *name, char *value,
113                                           unsigned long flags);
114 static CONF_MODULE *module_add(DSO *dso, const char *name,
115                         conf_init_func *ifunc, conf_finish_func *ffunc);
116 static CONF_MODULE *module_find(char *name);
117 static int module_init(CONF_MODULE *pmod, char *name, char *value,
118                                            const CONF *cnf);
119 static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
120                                                                         unsigned long flags);
121
122 /* Main function: load modules from a CONF structure */
123
124 int CONF_modules_load(const CONF *cnf, const char *appname,
125                       unsigned long flags)
126         {
127         STACK_OF(CONF_VALUE) *values;
128         CONF_VALUE *vl;
129         char *vsection;
130
131         int ret, i;
132
133         if (!cnf || !appname)
134                 return 1;
135
136
137         vsection = NCONF_get_string(cnf, NULL, appname); 
138
139         if (!vsection)
140                 {
141                 ERR_clear_error();
142                 return 1;
143                 }
144
145         values = NCONF_get_section(cnf, vsection);
146
147         if (!values)
148                 return 0;
149
150         for (i = 0; i < sk_CONF_VALUE_num(values); i++)
151                 {
152                 vl = sk_CONF_VALUE_value(values, i);
153                 ret = module_run(cnf, vl->name, vl->value, flags);
154                 if (ret <= 0)
155                         if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
156                                 return ret;
157                 }
158
159         return 1;
160
161         }
162
163 int CONF_modules_load_file(const char *filename, const char *appname,
164                            unsigned long flags)
165         {
166         CONF *conf = NULL;
167         int ret = 0;
168         conf = NCONF_new(NULL);
169         if (!conf)
170                 goto err;
171
172         if (NCONF_load(conf, filename, NULL) <= 0)
173                 goto err;
174
175         ret = CONF_modules_load(conf, appname, flags);
176
177         err:
178         NCONF_free(conf);
179
180         return ret;
181         }
182
183 static int module_run(const CONF *cnf, char *name, char *value,
184                       unsigned long flags)
185         {
186         CONF_MODULE *md;
187         int ret;
188
189         md = module_find(name);
190
191         /* Module not found: try to load DSO */
192         if (!md)
193                 md = module_load_dso(cnf, name, value, flags);
194
195         if (!md)
196                 {
197                 if (!(flags & CONF_MFLAGS_SILENT))
198                         {
199                         CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
200                         ERR_add_error_data(2, "module=", name);
201                         }
202                 return -1;
203                 }
204
205         ret = module_init(md, name, value, cnf);
206
207         if (ret <= 0)
208                 {
209                 if (!(flags & CONF_MFLAGS_SILENT))
210                         {
211                         char rcode[10];
212                         CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
213                         sprintf(rcode, "%-8d", ret);
214                         ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
215                         }
216                 }
217
218         return ret;
219         }
220
221 /* Load a module from a DSO */
222 static CONF_MODULE *module_load_dso(const CONF *cnf, char *name, char *value,
223                                     unsigned long flags)
224         {
225         DSO *dso = NULL;
226         conf_init_func *ifunc;
227         conf_finish_func *ffunc;
228         char *path = NULL;
229         int errcode = 0;
230         CONF_MODULE *md;
231         /* Look for alternative path in module section */
232         path = NCONF_get_string(cnf, value, "path");
233         if (!path)
234                 {
235                 ERR_get_error();
236                 path = name;
237                 }
238         dso = DSO_load(NULL, path, NULL, 0);
239         if (!dso)
240                 {
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)
246                 {
247                 errcode = CONF_R_MISSING_INIT_FUNCTION;
248                 goto err;
249                 }
250         ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
251         if (!ffunc)
252                 {
253                 errcode = CONF_R_MISSING_FINISH_FUNCTION;
254                 goto err;
255                 }
256         /* All OK, add module */
257         md = module_add(dso, name, ifunc, ffunc);
258
259         if (!md)
260                 goto err;
261
262         return md;
263
264         err:
265         if (dso)
266                 DSO_free(dso);
267         CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
268         ERR_add_error_data(4, "module=", name, ", path=", path);
269         return NULL;
270         }
271
272 /* add module to list */
273 static CONF_MODULE *module_add(DSO *dso, const char *name,
274                                conf_init_func *ifunc, conf_finish_func *ffunc)
275         {
276         CONF_MODULE *tmod = NULL;
277         if (supported_modules == NULL)
278                 supported_modules = sk_CONF_MODULE_new_null();
279         if (supported_modules == NULL)
280                 return NULL;
281         tmod = OPENSSL_malloc(sizeof(CONF_MODULE));
282         if (tmod == NULL)
283                 return NULL;
284
285         tmod->dso = dso;
286         tmod->name = BUF_strdup(name);
287         tmod->init = ifunc;
288         tmod->finish = ffunc;
289         tmod->links = 0;
290
291         if (!sk_CONF_MODULE_push(supported_modules, tmod))
292                 {
293                 OPENSSL_free(tmod);
294                 return NULL;
295                 }
296
297         return tmod;
298         }
299
300 /* Find a module from the list. We allow module names of the
301  * form modname.XXXX to just search for modname to allow the
302  * same module to be initialized more than once.
303  */
304
305 static CONF_MODULE *module_find(char *name)
306         {
307         CONF_MODULE *tmod;
308         int i, nchar;
309         char *p;
310         p = strrchr(name, '.');
311
312         if (p)
313                 nchar = p - name;
314         else 
315                 nchar = strlen(name);
316
317         for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++)
318                 {
319                 tmod = sk_CONF_MODULE_value(supported_modules, i);
320                 if (!strncmp(tmod->name, name, nchar))
321                         return tmod;
322                 }
323
324         return NULL;
325
326         }
327
328 /* initialize a module */
329 static int module_init(CONF_MODULE *pmod, char *name, char *value,
330                        const CONF *cnf)
331         {
332         int ret = 1;
333         int init_called = 0;
334         CONF_IMODULE *imod = NULL;
335
336         /* Otherwise add initialized module to list */
337         imod = OPENSSL_malloc(sizeof(CONF_IMODULE));
338         if (!imod)
339                 goto err;
340
341         imod->pmod = pmod;
342         imod->name = BUF_strdup(name);
343         imod->value = BUF_strdup(value);
344         imod->usr_data = NULL;
345
346         if (!imod->name || !imod->value)
347                 goto memerr;
348
349         /* Try to initialize module */
350         if(pmod->init)
351                 {
352                 ret = pmod->init(imod, cnf);
353                 init_called = 1;
354                 /* Error occurred, exit */
355                 if (ret <= 0)
356                         goto err;
357                 }
358
359         if (initialized_modules == NULL)
360                 {
361                 initialized_modules = sk_CONF_IMODULE_new_null();
362                 if (!initialized_modules)
363                         {
364                         CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
365                         goto err;
366                         }
367                 }
368
369         if (!sk_CONF_IMODULE_push(initialized_modules, imod))
370                 {
371                 CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
372                 goto err;
373                 }
374
375         pmod->links++;
376
377         return ret;
378
379         err:
380
381         /* We've started the module so we'd better finish it */
382         if (pmod->finish && init_called)
383                 pmod->finish(imod);
384
385         memerr:
386         if (imod)
387                 {
388                 if (imod->name)
389                         OPENSSL_free(imod->name);
390                 if (imod->value)
391                         OPENSSL_free(imod->value);
392                 OPENSSL_free(imod);
393                 }
394
395         return -1;
396
397         }
398
399 /* Unload any dynamic modules that have a link count of zero:
400  * i.e. have no active initialized modules. If 'all' is set
401  * then all modules are unloaded including static ones.
402  */
403
404 void CONF_modules_unload(int all)
405         {
406         int i;
407         CONF_MODULE *md;
408         /* unload modules in reverse order */
409         for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
410                 {
411                 md = sk_CONF_MODULE_value(supported_modules, i);
412                 /* If static or in use and 'all' not set ignore it */
413                 if (((md->links > 0) || !md->dso) && !all)
414                         continue;
415                 /* Since we're working in reverse this is OK */
416                 sk_CONF_MODULE_delete(supported_modules, i);
417                 module_free(md);
418                 }
419         if (sk_CONF_MODULE_num(supported_modules) == 0)
420                 {
421                 sk_CONF_MODULE_free(supported_modules);
422                 supported_modules = NULL;
423                 }
424         }
425
426 /* unload a single module */
427 static void module_free(CONF_MODULE *md)
428         {
429         if (md->dso)
430                 DSO_free(md->dso);
431         OPENSSL_free(md->name);
432         OPENSSL_free(md);
433         }
434
435 /* finish and free up all modules instances */
436
437 void CONF_modules_finish(void)
438         {
439         CONF_IMODULE *imod;
440         while (sk_CONF_IMODULE_num(initialized_modules) > 0)
441                 {
442                 imod = sk_CONF_IMODULE_pop(initialized_modules);
443                 module_finish(imod);
444                 }
445         sk_CONF_IMODULE_free(initialized_modules);
446         initialized_modules = NULL;
447         }
448
449 /* finish a module instance */
450
451 static void module_finish(CONF_IMODULE *imod)
452         {
453         imod->pmod->finish(imod);
454         imod->pmod->links--;
455         OPENSSL_free(imod->name);
456         OPENSSL_free(imod->value);
457         OPENSSL_free(imod);
458         }
459
460 /* Add a static module to OpenSSL */
461
462 int CONF_module_add(const char *name, conf_init_func *ifunc, 
463                     conf_finish_func *ffunc)
464         {
465         if (module_add(NULL, name, ifunc, ffunc))
466                 return 1;
467         else
468                 return 0;
469         }
470
471 void CONF_modules_free(void)
472         {
473         CONF_modules_finish();
474         CONF_modules_unload(1);
475         }
476
477 /* Utility functions */
478
479 const char *CONF_imodule_get_name(const CONF_IMODULE *md)
480         {
481         return md->name;
482         }
483
484 const char *CONF_imodule_get_value(const CONF_IMODULE *md)
485         {
486         return md->value;
487         }
488
489 void *CONF_imodule_get_usr_data(const CONF_IMODULE *md)
490         {
491         return md->usr_data;
492         }
493
494 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
495         {
496         md->usr_data = usr_data;
497         }
498
499 CONF_MODULE *CONF_imodule_get_module(const CONF_IMODULE *md)
500         {
501         return md->pmod;
502         }
503
504 unsigned long CONF_imodule_get_flags(const CONF_IMODULE *md)
505         {
506         return md->flags;
507         }
508
509 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
510         {
511         md->flags = flags;
512         }
513
514 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
515         {
516         return pmod->usr_data;
517         }
518
519 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
520         {
521         pmod->usr_data = usr_data;
522         }
523
524 /* Return default config file name */
525
526 char *CONF_get1_default_config_file(void)
527         {
528         char *file;
529         int len;
530
531         file = getenv("OPENSSL_CONF");
532         if (file) 
533                 return BUF_strdup(file);
534
535         len = strlen(X509_get_default_cert_area());
536 #ifndef OPENSSL_SYS_VMS
537         len++;
538 #endif
539         len += strlen(OPENSSL_CONF);
540
541         file = OPENSSL_malloc(len + 1);
542
543         if (!file)
544                 return NULL;
545         strcpy(file,X509_get_default_cert_area());
546 #ifndef OPENSSL_SYS_VMS
547         strcat(file,"/");
548 #endif
549         strcat(file,OPENSSL_CONF);
550
551         return file;
552         }
553
554 /* This function takes a list separated by 'sep' and calls the
555  * callback function giving the start and length of each member
556  * optionally stripping leading and trailing whitespace. This can
557  * be used to parse comma separated lists for example.
558  */
559
560 int CONF_parse_list(char *list, int sep, int nospc,
561                 int (*list_cb)(char *elem, int len, void *usr), void *arg)
562         {
563         int ret;
564         char *lstart, *tmpend, *p;
565         lstart = list;
566
567         for(;;)
568                 {
569                 if (nospc)
570                         {
571                         while(*lstart && isspace((unsigned char)*lstart))
572                                 lstart++;
573                         }
574                 p = strchr(lstart, sep);
575                 if (p == lstart || !*lstart)
576                         ret = list_cb(NULL, 0, arg);
577                 else
578                         {
579                         if (p)
580                                 tmpend = p - 1;
581                         else 
582                                 tmpend = lstart + strlen(lstart) - 1;
583                         if (nospc)
584                                 {
585                                 while(isspace((unsigned char)*tmpend))
586                                         tmpend--;
587                                 }
588                         ret = list_cb(lstart, tmpend - lstart + 1, arg);
589                         }
590                 if (ret <= 0)
591                         return ret;
592                 if (p == NULL)
593                         return 1;
594                 lstart = p + 1;
595                 }
596         }
597