cbbaf64f3cf54b4433deb63acced0bfff402288a
[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 <openssl/crypto.h>
61 #include "cryptlib.h"
62 #include <openssl/conf.h>
63 #include <openssl/dso.h>
64 #include <openssl/x509.h>
65
66
67 #define DSO_mod_init_name "OPENSSL_init"
68 #define DSO_mod_finish_name "OPENSSL_finish"
69
70
71 /* This structure contains a data about supported modules.
72  * entries in this table correspond to either dynamic or
73  * static modules.
74  */
75
76 struct conf_module_st
77         {
78         /* DSO of this module or NULL if static */
79         DSO *dso;
80         /* Name of the module */
81         char *name;
82         /* Init function */
83         conf_init_func *init; 
84         /* Finish function */
85         conf_finish_func *finish;
86         /* Number of successfully initialized modules */
87         int links;
88         void *usr_data;
89         };
90
91
92 /* This structure contains information about modules that have been
93  * successfully initialized. There may be more than one entry for a
94  * given module.
95  */
96
97 struct conf_imodule_st
98         {
99         CONF_MODULE *pmod;
100         char *name;
101         char *value;
102         unsigned long flags;
103         void *usr_data;
104         };
105
106 static STACK_OF(CONF_MODULE) *supported_modules = NULL;
107 static STACK_OF(CONF_IMODULE) *initialized_modules = NULL;
108
109 static void module_free(CONF_MODULE *md);
110 static void module_finish(CONF_IMODULE *imod);
111 static int module_run(CONF *cnf, char *name, char *value, unsigned long flags);
112 static CONF_MODULE *module_add(DSO *dso, char *name,
113                         conf_init_func *ifunc, conf_finish_func *ffunc);
114 static CONF_MODULE *module_find(char *name);
115 static int module_init(CONF_MODULE *pmod, char *name, char *value, CONF *cnf);
116 static CONF_MODULE *module_load_dso(CONF *cnf, char *name, char *value, unsigned long flags);
117
118 /* Main function: load modules from a CONF structure */
119
120 int CONF_modules_load(CONF *cnf, char *appname, unsigned long flags)
121         {
122         STACK_OF(CONF_VALUE) *values;
123         CONF_VALUE *vl;
124         char *vsection;
125
126         int ret, i;
127
128         if (!cnf || !appname)
129                 return 1;
130
131
132         vsection = NCONF_get_string(cnf, NULL, appname); 
133
134         if (!vsection)
135                 {
136                 ERR_clear_error();
137                 return 1;
138                 }
139
140         values = NCONF_get_section(cnf, vsection);
141
142         if (!values)
143                 return 0;
144
145         for (i = 0; i < sk_CONF_VALUE_num(values); i++)
146                 {
147                 vl = sk_CONF_VALUE_value(values, i);
148                 ret = module_run(cnf, vl->name, vl->value, flags);
149                 if (ret <= 0)
150                         if(!(flags & CONF_MFLAGS_IGNORE_ERRORS))
151                                 return ret;
152                 }
153
154         return 1;
155
156         }
157
158 int CONF_modules_load_file(char *filename, char *appname, unsigned long flags)
159         {
160         CONF *conf = NULL;
161         int ret = 0;
162         conf = NCONF_new(NULL);
163         if (!conf)
164                 goto err;
165
166         if (NCONF_load(conf, filename, NULL) <= 0)
167                 goto err;
168
169         ret = CONF_modules_load(conf, appname, flags);
170
171         err:
172         NCONF_free(conf);
173
174         return ret;
175         }
176
177 static int module_run(CONF *cnf, char *name, char *value, unsigned long flags)
178         {
179         CONF_MODULE *md;
180         int ret;
181
182         md = module_find(name);
183
184         /* Module not found: try to load DSO */
185         if (!md)
186                 md = module_load_dso(cnf, name, value, flags);
187
188         if (!md)
189                 {
190                 if (!(flags & CONF_MFLAGS_SILENT))
191                         {
192                         CONFerr(CONF_F_MODULE_RUN, CONF_R_UNKNOWN_MODULE_NAME);
193                         ERR_add_error_data(2, "module=", name);
194                         }
195                 return -1;
196                 }
197
198         ret = module_init(md, name, value, cnf);
199
200         if (ret <= 0)
201                 {
202                 if (!(flags & CONF_MFLAGS_SILENT))
203                         {
204                         char rcode[10];
205                         CONFerr(CONF_F_CONF_MODULES_LOAD, CONF_R_MODULE_INITIALIZATION_ERROR);
206                         sprintf(rcode, "%-8d", ret);
207                         ERR_add_error_data(6, "module=", name, ", value=", value, ", retcode=", rcode);
208                         }
209                 }
210
211         return ret;
212         }
213
214 /* Load a module from a DSO */
215 static CONF_MODULE *module_load_dso(CONF *cnf, char *name, char *value, unsigned long flags)
216         {
217         DSO *dso = NULL;
218         conf_init_func *ifunc;
219         conf_finish_func *ffunc;
220         char *path = NULL;
221         int errcode = 0;
222         CONF_MODULE *md;
223         /* Look for alternative path in module section */
224         path = NCONF_get_string(cnf, value, "path");
225         if (!path)
226                 {
227                 ERR_get_error();
228                 path = name;
229                 }
230         dso = DSO_load(NULL, path, NULL, 0);
231         if (!dso)
232                 {
233                 errcode = CONF_R_ERROR_LOADING_DSO;
234                 goto err;
235                 }
236         ifunc = (conf_init_func *)DSO_bind_func(dso, DSO_mod_init_name);
237         if (!ifunc)
238                 {
239                 errcode = CONF_R_MISSING_INIT_FUNCTION;
240                 goto err;
241                 }
242         ffunc = (conf_finish_func *)DSO_bind_func(dso, DSO_mod_finish_name);
243         if (!ffunc)
244                 {
245                 errcode = CONF_R_MISSING_FINISH_FUNCTION;
246                 goto err;
247                 }
248         /* All OK, add module */
249         md = module_add(dso, name, ifunc, ffunc);
250
251         if (!md)
252                 goto err;
253
254         return md;
255
256         err:
257         if (dso)
258                 DSO_free(dso);
259         CONFerr(CONF_F_MODULE_LOAD_DSO, errcode);
260         ERR_add_error_data(4, "module=", name, ", path=", path);
261         return NULL;
262         }
263
264 /* add module to list */
265 static CONF_MODULE *module_add(DSO *dso, char *name,
266                         conf_init_func *ifunc, conf_finish_func *ffunc)
267         {
268         CONF_MODULE *tmod = NULL;
269         if (supported_modules == NULL)
270                 supported_modules = sk_CONF_MODULE_new_null();
271         if (supported_modules == NULL)
272                 return NULL;
273         tmod = OPENSSL_malloc(sizeof(CONF_MODULE));
274         if (tmod == NULL)
275                 return NULL;
276
277         tmod->dso = dso;
278         tmod->name = BUF_strdup(name);
279         tmod->init = ifunc;
280         tmod->finish = ffunc;
281         tmod->links = 0;
282
283         if (!sk_CONF_MODULE_push(supported_modules, tmod))
284                 {
285                 OPENSSL_free(tmod);
286                 return NULL;
287                 }
288
289         return tmod;
290         }
291
292 /* Find a module from the list. We allow module names of the
293  * form modname.XXXX to just search for modname to allow the
294  * same module to be initialized more than once.
295  */
296
297 static CONF_MODULE *module_find(char *name)
298         {
299         CONF_MODULE *tmod;
300         int i, nchar;
301         char *p;
302         p = strrchr(name, '.');
303
304         if (p)
305                 nchar = p - name;
306         else 
307                 nchar = strlen(name);
308
309         for (i = 0; i < sk_CONF_MODULE_num(supported_modules); i++)
310                 {
311                 tmod = sk_CONF_MODULE_value(supported_modules, i);
312                 if (!strncmp(tmod->name, name, nchar))
313                         return tmod;
314                 }
315
316         return NULL;
317
318         }
319
320 /* initialize a module */
321 static int module_init(CONF_MODULE *pmod, char *name, char *value, CONF *cnf)
322         {
323         int ret, init_called = 0;
324         CONF_IMODULE *imod = NULL;
325
326         /* Otherwise add initialized module to list */
327         imod = OPENSSL_malloc(sizeof(CONF_IMODULE));
328         if (!imod)
329                 goto err;
330
331         imod->pmod = pmod;
332         imod->name = BUF_strdup(name);
333         imod->value = BUF_strdup(value);
334         imod->usr_data = NULL;
335
336         if (!imod->name || !imod->value)
337                 goto memerr;
338
339         /* Try to initialize module */
340         if(pmod->init)
341                 {
342                 ret = pmod->init(imod, cnf);
343                 init_called = 1;
344                 /* Error occurred, exit */
345                 if (ret <= 0)
346                         goto err;
347                 }
348
349         if (initialized_modules == NULL)
350                 initialized_modules = sk_CONF_IMODULE_new_null();
351
352         if (!initialized_modules)
353                 goto err;
354
355         if (!sk_CONF_IMODULE_push(initialized_modules, imod))
356                 goto err;
357
358         pmod->links++;
359
360         return ret;
361
362         err:
363
364         CONFerr(CONF_F_MODULE_INIT, ERR_R_MALLOC_FAILURE);
365
366         /* We've started the module so we'd better finish it */
367         if (pmod->finish && init_called)
368                 pmod->finish(imod);
369
370         memerr:
371         if (imod)
372                 {
373                 if (imod->name)
374                         OPENSSL_free(imod->name);
375                 if (imod->value)
376                         OPENSSL_free(imod->value);
377                 OPENSSL_free(imod);
378                 }
379
380         return -1;
381
382         }
383
384 /* Unload any dynamic modules that have a link count of zero:
385  * i.e. have no active initialized modules. If 'all' is set
386  * then all modules are unloaded including static ones.
387  */
388
389 void CONF_modules_unload(int all)
390         {
391         int i;
392         CONF_MODULE *md;
393         /* unload modules in reverse order */
394         for (i = sk_CONF_MODULE_num(supported_modules) - 1; i >= 0; i--)
395                 {
396                 md = sk_CONF_MODULE_value(supported_modules, i);
397                 /* If static or in use and 'all' not set ignore it */
398                 if (((md->links > 0) || !md->dso) && !all)
399                         continue;
400                 /* Since we're working in reverse this is OK */
401                 sk_CONF_MODULE_delete(supported_modules, i);
402                 module_free(md);
403                 }
404         if (sk_CONF_MODULE_num(supported_modules) == 0)
405                 {
406                 sk_CONF_MODULE_free(supported_modules);
407                 supported_modules = NULL;
408                 }
409         }
410
411 /* unload a single module */
412 static void module_free(CONF_MODULE *md)
413         {
414         if (md->dso)
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                 {
427                 imod = sk_CONF_IMODULE_pop(initialized_modules);
428                 module_finish(imod);
429                 }
430         sk_CONF_IMODULE_free(initialized_modules);
431         initialized_modules = NULL;
432         }
433
434 /* finish a module instance */
435
436 static void module_finish(CONF_IMODULE *imod)
437         {
438         imod->pmod->finish(imod);
439         imod->pmod->links--;
440         OPENSSL_free(imod->name);
441         OPENSSL_free(imod->value);
442         OPENSSL_free(imod);
443         }
444
445 /* Add a static module to OpenSSL */
446
447 int CONF_module_add(char *name, conf_init_func *ifunc, conf_finish_func *ffunc)
448         {
449         if (module_add(NULL, name, ifunc, ffunc))
450                 return 1;
451         else
452                 return 0;
453         }
454
455 void CONF_modules_free(void)
456         {
457         CONF_modules_finish();
458         CONF_modules_unload(1);
459         }
460
461 /* Utility functions */
462
463 char *CONF_imodule_get_name(CONF_IMODULE *md)
464         {
465         return md->name;
466         }
467
468 char *CONF_imodule_get_value(CONF_IMODULE *md)
469         {
470         return md->value;
471         }
472
473 void *CONF_imodule_get_usr_data(CONF_IMODULE *md)
474         {
475         return md->usr_data;
476         }
477
478 void CONF_imodule_set_usr_data(CONF_IMODULE *md, void *usr_data)
479         {
480         md->usr_data = usr_data;
481         }
482
483 CONF_MODULE *CONF_imodule_get_module(CONF_IMODULE *md)
484         {
485         return md->pmod;
486         }
487
488 unsigned long CONF_imodule_get_flags(CONF_IMODULE *md)
489         {
490         return md->flags;
491         }
492
493 void CONF_imodule_set_flags(CONF_IMODULE *md, unsigned long flags)
494         {
495         md->flags = flags;
496         }
497
498 void *CONF_module_get_usr_data(CONF_MODULE *pmod)
499         {
500         return pmod->usr_data;
501         }
502
503 void CONF_module_set_usr_data(CONF_MODULE *pmod, void *usr_data)
504         {
505         pmod->usr_data = usr_data;
506         }
507