check integrity of plugins before loading
[strongswan.git] / src / libstrongswan / library.c
1 /*
2 * Copyright (C) 2009 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "library.h"
18
19 #include <stdlib.h>
20
21 #include <utils.h>
22 #include <chunk.h>
23 #include <debug.h>
24 #include <utils/identification.h>
25 #include <utils/host.h>
26 #ifdef LEAK_DETECTIVE
27 #include <utils/leak_detective.h>
28 #endif
29
30 typedef struct private_library_t private_library_t;
31
32 /**
33 * private data of library
34 */
35 struct private_library_t {
36
37 /**
38 * public functions
39 */
40 library_t public;
41
42 #ifdef LEAK_DETECTIVE
43 /**
44 * Memory leak detective, if enabled
45 */
46 leak_detective_t *detective;
47 #endif /* LEAK_DETECTIVE */
48 };
49
50 /**
51 * library instance
52 */
53 library_t *lib;
54
55 /**
56 * Implementation of library_t.destroy
57 */
58 void library_deinit()
59 {
60 private_library_t *this = (private_library_t*)lib;
61
62 this->public.plugins->destroy(this->public.plugins);
63 this->public.settings->destroy(this->public.settings);
64 this->public.creds->destroy(this->public.creds);
65 this->public.crypto->destroy(this->public.crypto);
66 this->public.fetcher->destroy(this->public.fetcher);
67 this->public.db->destroy(this->public.db);
68 this->public.printf_hook->destroy(this->public.printf_hook);
69 if (this->public.integrity)
70 {
71 this->public.integrity->destroy(this->public.integrity);
72 }
73
74 #ifdef LEAK_DETECTIVE
75 if (this->detective)
76 {
77 this->detective->destroy(this->detective);
78 }
79 #endif /* LEAK_DETECTIVE */
80 free(this);
81 lib = NULL;
82 }
83
84 /*
85 * see header file
86 */
87 void library_init(char *settings)
88 {
89 printf_hook_t *pfh;
90 private_library_t *this = malloc_thing(private_library_t);
91 lib = &this->public;
92
93 lib->leak_detective = FALSE;
94
95 #ifdef LEAK_DETECTIVE
96 this->detective = leak_detective_create();
97 #endif /* LEAK_DETECTIVE */
98
99 pfh = printf_hook_create();
100 this->public.printf_hook = pfh;
101
102 pfh->add_handler(pfh, 'b', mem_printf_hook,
103 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
104 PRINTF_HOOK_ARGTYPE_END);
105 pfh->add_handler(pfh, 'B', chunk_printf_hook,
106 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
107 pfh->add_handler(pfh, 'H', host_printf_hook,
108 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
109 pfh->add_handler(pfh, 'N', enum_printf_hook,
110 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
111 PRINTF_HOOK_ARGTYPE_END);
112 pfh->add_handler(pfh, 'T', time_printf_hook,
113 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
114 PRINTF_HOOK_ARGTYPE_END);
115 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
116 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
117 PRINTF_HOOK_ARGTYPE_END);
118 pfh->add_handler(pfh, 'Y', identification_printf_hook,
119 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
120
121 this->public.settings = settings_create(settings);
122 this->public.crypto = crypto_factory_create();
123 this->public.creds = credential_factory_create();
124 this->public.fetcher = fetcher_manager_create();
125 this->public.db = database_factory_create();
126 this->public.plugins = plugin_loader_create();
127 this->public.integrity = NULL;
128
129 if (lib->settings->get_bool(lib->settings,
130 "libstrongswan.integrity_test", FALSE))
131 {
132 this->public.integrity = integrity_checker_create();
133 }
134 }
135