Add plugin constructor registration for all libraries that provide plugins
[strongswan.git] / src / libstrongswan / library.c
1 /*
2 * Copyright (C) 2009-2016 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/array.h>
26 #include <collections/hashtable.h>
27 #include <utils/backtrace.h>
28 #include <selectors/traffic_selector.h>
29
30 #define CHECKSUM_LIBRARY IPSEC_LIB_DIR"/libchecksum.so"
31
32 #ifndef STRONGSWAN_CONF
33 #define STRONGSWAN_CONF NULL
34 #endif
35
36 typedef struct private_library_t private_library_t;
37
38 /**
39 * private data of library
40 */
41 struct private_library_t {
42
43 /**
44 * public functions
45 */
46 library_t public;
47
48 /**
49 * Hashtable with registered objects (name => object)
50 */
51 hashtable_t *objects;
52
53 /**
54 * Integrity check failed?
55 */
56 bool integrity_failed;
57
58 #ifdef LEAK_DETECTIVE
59 /**
60 * Where to write leak detective output to
61 */
62 FILE *ld_out;
63 #endif
64
65 /**
66 * Number of times we have been initialized
67 */
68 refcount_t ref;
69 };
70
71 #define MAX_NAMESPACES 5
72
73 /**
74 * Additional namespaces registered using __atrribute__((constructor))
75 */
76 static char *namespaces[MAX_NAMESPACES];
77 static int ns_count;
78
79 /**
80 * Described in header
81 */
82 void library_add_namespace(char *ns)
83 {
84 if (ns_count < MAX_NAMESPACES - 1)
85 {
86 namespaces[ns_count] = ns;
87 ns_count++;
88 }
89 else
90 {
91 fprintf(stderr, "failed to register additional namespace alias, please "
92 "increase MAX_NAMESPACES");
93 }
94 }
95
96 /**
97 * Register plugins if built statically
98 */
99 #ifdef STATIC_PLUGIN_CONSTRUCTORS
100 #include "plugin_constructors.c"
101 #endif
102
103 /**
104 * library instance
105 */
106 library_t *lib = NULL;
107
108 #ifdef LEAK_DETECTIVE
109 /**
110 * Default leak report callback
111 */
112 CALLBACK(report_leaks, void,
113 private_library_t *this, int count, size_t bytes, backtrace_t *bt,
114 bool detailed)
115 {
116 fprintf(this->ld_out, "%zu bytes total, %d allocations, %zu bytes average:\n",
117 bytes, count, bytes / count);
118 bt->log(bt, this->ld_out, detailed);
119 }
120
121 /**
122 * Default leak report summary callback
123 */
124 CALLBACK(sum_leaks, void,
125 private_library_t *this, int count, size_t bytes, int whitelisted)
126 {
127 switch (count)
128 {
129 case 0:
130 fprintf(this->ld_out, "No leaks detected");
131 break;
132 case 1:
133 fprintf(this->ld_out, "One leak detected");
134 break;
135 default:
136 fprintf(this->ld_out, "%d leaks detected, %zu bytes", count, bytes);
137 break;
138 }
139 fprintf(this->ld_out, ", %d suppressed by whitelist\n", whitelisted);
140 }
141 #endif /* LEAK_DETECTIVE */
142
143 /**
144 * Deinitialize library
145 */
146 void library_deinit()
147 {
148 private_library_t *this = (private_library_t*)lib;
149 bool detailed;
150
151 if (!this || !ref_put(&this->ref))
152 { /* have more users */
153 return;
154 }
155
156 detailed = lib->settings->get_bool(lib->settings,
157 "%s.leak_detective.detailed", TRUE, lib->ns);
158
159 /* make sure the cache is clear before unloading plugins */
160 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
161
162 this->public.streams->destroy(this->public.streams);
163 this->public.watcher->destroy(this->public.watcher);
164 this->public.scheduler->destroy(this->public.scheduler);
165 this->public.processor->destroy(this->public.processor);
166 this->public.plugins->destroy(this->public.plugins);
167 this->public.hosts->destroy(this->public.hosts);
168 this->public.settings->destroy(this->public.settings);
169 this->public.credmgr->destroy(this->public.credmgr);
170 this->public.creds->destroy(this->public.creds);
171 this->public.encoding->destroy(this->public.encoding);
172 this->public.crypto->destroy(this->public.crypto);
173 this->public.caps->destroy(this->public.caps);
174 this->public.proposal->destroy(this->public.proposal);
175 this->public.fetcher->destroy(this->public.fetcher);
176 this->public.resolver->destroy(this->public.resolver);
177 this->public.db->destroy(this->public.db);
178 this->public.printf_hook->destroy(this->public.printf_hook);
179 this->objects->destroy(this->objects);
180 if (this->public.integrity)
181 {
182 this->public.integrity->destroy(this->public.integrity);
183 }
184
185 if (lib->leak_detective)
186 {
187 lib->leak_detective->report(lib->leak_detective, detailed);
188 lib->leak_detective->destroy(lib->leak_detective);
189 lib->leak_detective = NULL;
190 }
191 #ifdef LEAK_DETECTIVE
192 if (this->ld_out && this->ld_out != stderr)
193 {
194 fclose(this->ld_out);
195 }
196 #endif /* LEAK_DETECTIVE */
197
198 backtrace_deinit();
199 arrays_deinit();
200 utils_deinit();
201 threads_deinit();
202
203 free(this->public.conf);
204 free((void*)this->public.ns);
205 free(this);
206 lib = NULL;
207 }
208
209 METHOD(library_t, get, void*,
210 private_library_t *this, char *name)
211 {
212 return this->objects->get(this->objects, name);
213 }
214
215 METHOD(library_t, set, bool,
216 private_library_t *this, char *name, void *object)
217 {
218 if (object)
219 {
220 if (this->objects->get(this->objects, name))
221 {
222 return FALSE;
223 }
224 this->objects->put(this->objects, name, object);
225 return TRUE;
226 }
227 return this->objects->remove(this->objects, name) != NULL;
228 }
229
230 /**
231 * Hashtable hash function
232 */
233 static u_int hash(char *key)
234 {
235 return chunk_hash(chunk_create(key, strlen(key)));
236 }
237
238 /**
239 * Hashtable equals function
240 */
241 static bool equals(char *a, char *b)
242 {
243 return streq(a, b);
244 }
245
246 /**
247 * Number of words we write and memwipe() in memwipe check
248 */
249 #define MEMWIPE_WIPE_WORDS 16
250
251 #ifndef NO_CHECK_MEMWIPE
252
253 /**
254 * Write magic to memory, and try to clear it with memwipe()
255 */
256 __attribute__((noinline))
257 static void do_magic(int *magic, int **out)
258 {
259 int buf[MEMWIPE_WIPE_WORDS], i;
260
261 *out = buf;
262 for (i = 0; i < countof(buf); i++)
263 {
264 buf[i] = *magic;
265 }
266 /* passing buf to dbg should make sure the compiler can't optimize out buf.
267 * we use directly dbg(3), as DBG3() might be stripped with DEBUG_LEVEL. */
268 dbg(DBG_LIB, 3, "memwipe() pre: %b", buf, sizeof(buf));
269 memwipe(buf, sizeof(buf));
270 }
271
272 /**
273 * Check if memwipe works as expected
274 */
275 static bool check_memwipe()
276 {
277 int magic = 0xCAFEBABE, *buf, i;
278
279 do_magic(&magic, &buf);
280
281 for (i = 0; i < MEMWIPE_WIPE_WORDS; i++)
282 {
283 if (buf[i] == magic)
284 {
285 DBG1(DBG_LIB, "memwipe() check failed: stackdir: %b",
286 buf, MEMWIPE_WIPE_WORDS * sizeof(int));
287 return FALSE;
288 }
289 }
290 return TRUE;
291 }
292
293 #endif
294
295 /*
296 * see header file
297 */
298 bool library_init(char *settings, const char *namespace)
299 {
300 private_library_t *this;
301 printf_hook_t *pfh;
302 int i;
303
304 if (lib)
305 { /* already initialized, increase refcount */
306 this = (private_library_t*)lib;
307 ref_get(&this->ref);
308 return !this->integrity_failed;
309 }
310
311 chunk_hash_seed();
312
313 INIT(this,
314 .public = {
315 .get = _get,
316 .set = _set,
317 .ns = strdup(namespace ?: "libstrongswan"),
318 .conf = strdupnull(settings ?: (getenv("STRONGSWAN_CONF") ?: STRONGSWAN_CONF)),
319 },
320 .ref = 1,
321 );
322 lib = &this->public;
323
324 threads_init();
325 utils_init();
326 arrays_init();
327 backtrace_init();
328
329 #ifdef LEAK_DETECTIVE
330 {
331 FILE *out = NULL;
332 char *log;
333
334 log = getenv("LEAK_DETECTIVE_LOG");
335 if (log)
336 {
337 out = fopen(log, "a");
338 }
339 this->ld_out = out ?: stderr;
340 }
341 lib->leak_detective = leak_detective_create();
342 if (lib->leak_detective)
343 {
344 lib->leak_detective->set_report_cb(lib->leak_detective,
345 report_leaks, sum_leaks, this);
346 }
347 #endif /* LEAK_DETECTIVE */
348
349 pfh = printf_hook_create();
350 this->public.printf_hook = pfh;
351
352 pfh->add_handler(pfh, 'b', mem_printf_hook,
353 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
354 PRINTF_HOOK_ARGTYPE_END);
355 pfh->add_handler(pfh, 'B', chunk_printf_hook,
356 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
357 pfh->add_handler(pfh, 'H', host_printf_hook,
358 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
359 pfh->add_handler(pfh, 'N', enum_printf_hook,
360 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
361 PRINTF_HOOK_ARGTYPE_END);
362 pfh->add_handler(pfh, 'T', time_printf_hook,
363 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
364 PRINTF_HOOK_ARGTYPE_END);
365 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
366 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
367 PRINTF_HOOK_ARGTYPE_END);
368 pfh->add_handler(pfh, 'Y', identification_printf_hook,
369 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
370 pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
371 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
372
373 this->objects = hashtable_create((hashtable_hash_t)hash,
374 (hashtable_equals_t)equals, 4);
375
376 this->public.settings = settings_create(this->public.conf);
377 /* add registered aliases */
378 for (i = 0; i < ns_count; ++i)
379 {
380 lib->settings->add_fallback(lib->settings, lib->ns, namespaces[i]);
381 }
382 /* all namespace settings may fall back to libstrongswan */
383 lib->settings->add_fallback(lib->settings, lib->ns, "libstrongswan");
384
385 this->public.hosts = host_resolver_create();
386 this->public.proposal = proposal_keywords_create();
387 this->public.caps = capabilities_create();
388 this->public.crypto = crypto_factory_create();
389 this->public.creds = credential_factory_create();
390 this->public.credmgr = credential_manager_create();
391 this->public.encoding = cred_encoding_create();
392 this->public.fetcher = fetcher_manager_create();
393 this->public.resolver = resolver_manager_create();
394 this->public.db = database_factory_create();
395 this->public.processor = processor_create();
396 this->public.scheduler = scheduler_create();
397 this->public.watcher = watcher_create();
398 this->public.streams = stream_manager_create();
399 this->public.plugins = plugin_loader_create();
400
401 #ifndef NO_CHECK_MEMWIPE
402 if (!check_memwipe())
403 {
404 return FALSE;
405 }
406 #endif
407
408 if (lib->settings->get_bool(lib->settings,
409 "%s.integrity_test", FALSE, lib->ns))
410 {
411 #ifdef INTEGRITY_TEST
412 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
413 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
414 {
415 DBG1(DBG_LIB, "integrity check of libstrongswan failed");
416 this->integrity_failed = TRUE;
417 }
418 #else /* !INTEGRITY_TEST */
419 DBG1(DBG_LIB, "integrity test enabled, but not supported");
420 this->integrity_failed = TRUE;
421 #endif /* INTEGRITY_TEST */
422 }
423
424 diffie_hellman_init();
425
426 return !this->integrity_failed;
427 }