watcher: add a centralized an generic facility to monitor file descriptors
[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.watcher->destroy(this->public.watcher);
84 this->public.scheduler->destroy(this->public.scheduler);
85 this->public.processor->destroy(this->public.processor);
86 this->public.plugins->destroy(this->public.plugins);
87 this->public.hosts->destroy(this->public.hosts);
88 this->public.settings->destroy(this->public.settings);
89 this->public.credmgr->destroy(this->public.credmgr);
90 this->public.creds->destroy(this->public.creds);
91 this->public.encoding->destroy(this->public.encoding);
92 this->public.crypto->destroy(this->public.crypto);
93 this->public.caps->destroy(this->public.caps);
94 this->public.proposal->destroy(this->public.proposal);
95 this->public.fetcher->destroy(this->public.fetcher);
96 this->public.resolver->destroy(this->public.resolver);
97 this->public.db->destroy(this->public.db);
98 this->public.printf_hook->destroy(this->public.printf_hook);
99 this->objects->destroy(this->objects);
100 if (this->public.integrity)
101 {
102 this->public.integrity->destroy(this->public.integrity);
103 }
104
105 if (lib->leak_detective)
106 {
107 lib->leak_detective->report(lib->leak_detective, detailed);
108 lib->leak_detective->destroy(lib->leak_detective);
109 }
110
111 threads_deinit();
112 backtrace_deinit();
113
114 free(this);
115 lib = NULL;
116 }
117
118 METHOD(library_t, get, void*,
119 private_library_t *this, char *name)
120 {
121 return this->objects->get(this->objects, name);
122 }
123
124 METHOD(library_t, set, bool,
125 private_library_t *this, char *name, void *object)
126 {
127 if (object)
128 {
129 if (this->objects->get(this->objects, name))
130 {
131 return FALSE;
132 }
133 this->objects->put(this->objects, name, object);
134 return TRUE;
135 }
136 return this->objects->remove(this->objects, name) != NULL;
137 }
138
139 /**
140 * Hashtable hash function
141 */
142 static u_int hash(char *key)
143 {
144 return chunk_hash(chunk_create(key, strlen(key)));
145 }
146
147 /**
148 * Hashtable equals function
149 */
150 static bool equals(char *a, char *b)
151 {
152 return streq(a, b);
153 }
154
155 /**
156 * Number of words we write and memwipe() in memwipe check
157 */
158 #define MEMWIPE_WIPE_WORDS 16
159
160 /**
161 * Write magic to memory, and try to clear it with memwipe()
162 */
163 __attribute__((noinline))
164 static void do_magic(int *magic, int **out)
165 {
166 int buf[MEMWIPE_WIPE_WORDS], i;
167
168 *out = buf;
169 for (i = 0; i < countof(buf); i++)
170 {
171 buf[i] = *magic;
172 }
173 /* passing buf to dbg should make sure the compiler can't optimize out buf.
174 * we use directly dbg(3), as DBG3() might be stripped with DEBUG_LEVEL. */
175 dbg(DBG_LIB, 3, "memwipe() pre: %b", buf, sizeof(buf));
176 memwipe(buf, sizeof(buf));
177 }
178
179 /**
180 * Check if memwipe works as expected
181 */
182 static bool check_memwipe()
183 {
184 int magic = 0xCAFEBABE, *buf, i;
185
186 do_magic(&magic, &buf);
187
188 for (i = 0; i < MEMWIPE_WIPE_WORDS; i++)
189 {
190 if (buf[i] == magic)
191 {
192 DBG1(DBG_LIB, "memwipe() check failed: stackdir: %b",
193 buf, MEMWIPE_WIPE_WORDS * sizeof(int));
194 return FALSE;
195 }
196 }
197 return TRUE;
198 }
199
200 /*
201 * see header file
202 */
203 bool library_init(char *settings)
204 {
205 private_library_t *this;
206 printf_hook_t *pfh;
207
208 if (lib)
209 { /* already initialized, increase refcount */
210 this = (private_library_t*)lib;
211 ref_get(&this->ref);
212 return !this->integrity_failed;
213 }
214
215 INIT(this,
216 .public = {
217 .get = _get,
218 .set = _set,
219 },
220 .ref = 1,
221 );
222 lib = &this->public;
223
224 backtrace_init();
225 threads_init();
226
227 #ifdef LEAK_DETECTIVE
228 lib->leak_detective = leak_detective_create();
229 #endif /* LEAK_DETECTIVE */
230
231 pfh = printf_hook_create();
232 this->public.printf_hook = pfh;
233
234 pfh->add_handler(pfh, 'b', mem_printf_hook,
235 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
236 PRINTF_HOOK_ARGTYPE_END);
237 pfh->add_handler(pfh, 'B', chunk_printf_hook,
238 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
239 pfh->add_handler(pfh, 'H', host_printf_hook,
240 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
241 pfh->add_handler(pfh, 'N', enum_printf_hook,
242 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
243 PRINTF_HOOK_ARGTYPE_END);
244 pfh->add_handler(pfh, 'T', time_printf_hook,
245 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
246 PRINTF_HOOK_ARGTYPE_END);
247 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
248 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
249 PRINTF_HOOK_ARGTYPE_END);
250 pfh->add_handler(pfh, 'Y', identification_printf_hook,
251 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
252 pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
253 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
254
255 this->objects = hashtable_create((hashtable_hash_t)hash,
256 (hashtable_equals_t)equals, 4);
257 this->public.settings = settings_create(settings);
258 this->public.hosts = host_resolver_create();
259 this->public.proposal = proposal_keywords_create();
260 this->public.caps = capabilities_create();
261 this->public.crypto = crypto_factory_create();
262 this->public.creds = credential_factory_create();
263 this->public.credmgr = credential_manager_create();
264 this->public.encoding = cred_encoding_create();
265 this->public.fetcher = fetcher_manager_create();
266 this->public.resolver = resolver_manager_create();
267 this->public.db = database_factory_create();
268 this->public.processor = processor_create();
269 this->public.scheduler = scheduler_create();
270 this->public.watcher = watcher_create();
271 this->public.plugins = plugin_loader_create();
272
273 if (!check_memwipe())
274 {
275 return FALSE;
276 }
277
278 if (lib->settings->get_bool(lib->settings,
279 "libstrongswan.integrity_test", FALSE))
280 {
281 #ifdef INTEGRITY_TEST
282 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
283 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
284 {
285 DBG1(DBG_LIB, "integrity check of libstrongswan failed");
286 this->integrity_failed = TRUE;
287 }
288 #else /* !INTEGRITY_TEST */
289 DBG1(DBG_LIB, "integrity test enabled, but not supported");
290 this->integrity_failed = TRUE;
291 #endif /* INTEGRITY_TEST */
292 }
293
294 return !this->integrity_failed;
295 }