Merge branch 'check-caps'
[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/debug.h>
22 #include <threading/thread.h>
23 #include <utils/identification.h>
24 #include <networking/host.h>
25 #include <collections/hashtable.h>
26 #include <utils/backtrace.h>
27 #include <selectors/traffic_selector.h>
28
29 #define CHECKSUM_LIBRARY IPSEC_LIB_DIR"/libchecksum.so"
30
31 typedef struct private_library_t private_library_t;
32
33 /**
34 * private data of library
35 */
36 struct private_library_t {
37
38 /**
39 * public functions
40 */
41 library_t public;
42
43 /**
44 * Hashtable with registered objects (name => object)
45 */
46 hashtable_t *objects;
47
48 /**
49 * Integrity check failed?
50 */
51 bool integrity_failed;
52
53 /**
54 * Number of times we have been initialized
55 */
56 refcount_t ref;
57 };
58
59 /**
60 * library instance
61 */
62 library_t *lib = NULL;
63
64 /**
65 * Deinitialize library
66 */
67 void library_deinit()
68 {
69 private_library_t *this = (private_library_t*)lib;
70 bool detailed;
71
72 if (!this || !ref_put(&this->ref))
73 { /* have more users */
74 return;
75 }
76
77 detailed = lib->settings->get_bool(lib->settings,
78 "libstrongswan.leak_detective.detailed", TRUE);
79
80 /* make sure the cache is clear before unloading plugins */
81 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
82
83 this->public.scheduler->destroy(this->public.scheduler);
84 this->public.processor->destroy(this->public.processor);
85 this->public.plugins->destroy(this->public.plugins);
86 this->public.hosts->destroy(this->public.hosts);
87 this->public.settings->destroy(this->public.settings);
88 this->public.credmgr->destroy(this->public.credmgr);
89 this->public.creds->destroy(this->public.creds);
90 this->public.encoding->destroy(this->public.encoding);
91 this->public.crypto->destroy(this->public.crypto);
92 this->public.caps->destroy(this->public.caps);
93 this->public.proposal->destroy(this->public.proposal);
94 this->public.fetcher->destroy(this->public.fetcher);
95 this->public.resolver->destroy(this->public.resolver);
96 this->public.db->destroy(this->public.db);
97 this->public.printf_hook->destroy(this->public.printf_hook);
98 this->objects->destroy(this->objects);
99 if (this->public.integrity)
100 {
101 this->public.integrity->destroy(this->public.integrity);
102 }
103
104 if (lib->leak_detective)
105 {
106 lib->leak_detective->report(lib->leak_detective, detailed);
107 lib->leak_detective->destroy(lib->leak_detective);
108 }
109
110 threads_deinit();
111 backtrace_deinit();
112
113 free(this);
114 lib = NULL;
115 }
116
117 METHOD(library_t, get, void*,
118 private_library_t *this, char *name)
119 {
120 return this->objects->get(this->objects, name);
121 }
122
123 METHOD(library_t, set, bool,
124 private_library_t *this, char *name, void *object)
125 {
126 if (object)
127 {
128 if (this->objects->get(this->objects, name))
129 {
130 return FALSE;
131 }
132 this->objects->put(this->objects, name, object);
133 return TRUE;
134 }
135 return this->objects->remove(this->objects, name) != NULL;
136 }
137
138 /**
139 * Hashtable hash function
140 */
141 static u_int hash(char *key)
142 {
143 return chunk_hash(chunk_create(key, strlen(key)));
144 }
145
146 /**
147 * Hashtable equals function
148 */
149 static bool equals(char *a, char *b)
150 {
151 return streq(a, b);
152 }
153
154 /**
155 * Number of words we write and memwipe() in memwipe check
156 */
157 #define MEMWIPE_WIPE_WORDS 16
158
159 /**
160 * Write magic to memory, and try to clear it with memwipe()
161 */
162 __attribute__((noinline))
163 static void do_magic(int *magic, int **out)
164 {
165 int buf[MEMWIPE_WIPE_WORDS], i;
166
167 *out = buf;
168 for (i = 0; i < countof(buf); i++)
169 {
170 buf[i] = *magic;
171 }
172 /* passing buf to dbg should make sure the compiler can't optimize out buf.
173 * we use directly dbg(3), as DBG3() might be stripped with DEBUG_LEVEL. */
174 dbg(DBG_LIB, 3, "memwipe() pre: %b", buf, sizeof(buf));
175 memwipe(buf, sizeof(buf));
176 }
177
178 /**
179 * Check if memwipe works as expected
180 */
181 static bool check_memwipe()
182 {
183 int magic = 0xCAFEBABE, *buf, i;
184
185 do_magic(&magic, &buf);
186
187 for (i = 0; i < MEMWIPE_WIPE_WORDS; i++)
188 {
189 if (buf[i] == magic)
190 {
191 DBG1(DBG_LIB, "memwipe() check failed: stackdir: %b",
192 buf, MEMWIPE_WIPE_WORDS * sizeof(int));
193 return FALSE;
194 }
195 }
196 return TRUE;
197 }
198
199 /*
200 * see header file
201 */
202 bool library_init(char *settings)
203 {
204 private_library_t *this;
205 printf_hook_t *pfh;
206
207 if (lib)
208 { /* already initialized, increase refcount */
209 this = (private_library_t*)lib;
210 ref_get(&this->ref);
211 return !this->integrity_failed;
212 }
213
214 INIT(this,
215 .public = {
216 .get = _get,
217 .set = _set,
218 },
219 .ref = 1,
220 );
221 lib = &this->public;
222
223 backtrace_init();
224 threads_init();
225
226 #ifdef LEAK_DETECTIVE
227 lib->leak_detective = leak_detective_create();
228 #endif /* LEAK_DETECTIVE */
229
230 pfh = printf_hook_create();
231 this->public.printf_hook = pfh;
232
233 pfh->add_handler(pfh, 'b', mem_printf_hook,
234 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
235 PRINTF_HOOK_ARGTYPE_END);
236 pfh->add_handler(pfh, 'B', chunk_printf_hook,
237 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
238 pfh->add_handler(pfh, 'H', host_printf_hook,
239 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
240 pfh->add_handler(pfh, 'N', enum_printf_hook,
241 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
242 PRINTF_HOOK_ARGTYPE_END);
243 pfh->add_handler(pfh, 'T', time_printf_hook,
244 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
245 PRINTF_HOOK_ARGTYPE_END);
246 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
247 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
248 PRINTF_HOOK_ARGTYPE_END);
249 pfh->add_handler(pfh, 'Y', identification_printf_hook,
250 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
251 pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
252 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
253
254 this->objects = hashtable_create((hashtable_hash_t)hash,
255 (hashtable_equals_t)equals, 4);
256 this->public.settings = settings_create(settings);
257 this->public.hosts = host_resolver_create();
258 this->public.proposal = proposal_keywords_create();
259 this->public.caps = capabilities_create();
260 this->public.crypto = crypto_factory_create();
261 this->public.creds = credential_factory_create();
262 this->public.credmgr = credential_manager_create();
263 this->public.encoding = cred_encoding_create();
264 this->public.fetcher = fetcher_manager_create();
265 this->public.resolver = resolver_manager_create();
266 this->public.db = database_factory_create();
267 this->public.processor = processor_create();
268 this->public.scheduler = scheduler_create();
269 this->public.plugins = plugin_loader_create();
270
271 if (!check_memwipe())
272 {
273 return FALSE;
274 }
275
276 if (lib->settings->get_bool(lib->settings,
277 "libstrongswan.integrity_test", FALSE))
278 {
279 #ifdef INTEGRITY_TEST
280 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
281 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
282 {
283 DBG1(DBG_LIB, "integrity check of libstrongswan failed");
284 this->integrity_failed = TRUE;
285 }
286 #else /* !INTEGRITY_TEST */
287 DBG1(DBG_LIB, "integrity test enabled, but not supported");
288 this->integrity_failed = TRUE;
289 #endif /* INTEGRITY_TEST */
290 }
291
292 return !this->integrity_failed;
293 }