Make sure the certificate cache is flushed when plugins are unloaded.
[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 <debug.h>
22 #include <threading/thread.h>
23 #include <utils/identification.h>
24 #include <utils/host.h>
25 #include <utils/hashtable.h>
26 #include <selectors/traffic_selector.h>
27
28 #define CHECKSUM_LIBRARY IPSEC_LIB_DIR"/libchecksum.so"
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 /**
43 * Hashtable with registered objects (name => object)
44 */
45 hashtable_t *objects;
46 };
47
48 /**
49 * library instance
50 */
51 library_t *lib;
52
53 /**
54 * Deinitialize library
55 */
56 void library_deinit()
57 {
58 private_library_t *this = (private_library_t*)lib;
59 bool detailed;
60
61 detailed = lib->settings->get_bool(lib->settings,
62 "libstrongswan.leak_detective.detailed", TRUE);
63
64 /* make sure the cache is clear before unloading plugins */
65 lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
66
67 this->public.scheduler->destroy(this->public.scheduler);
68 this->public.processor->destroy(this->public.processor);
69 this->public.plugins->destroy(this->public.plugins);
70 this->public.settings->destroy(this->public.settings);
71 this->public.credmgr->destroy(this->public.credmgr);
72 this->public.creds->destroy(this->public.creds);
73 this->public.encoding->destroy(this->public.encoding);
74 this->public.crypto->destroy(this->public.crypto);
75 this->public.fetcher->destroy(this->public.fetcher);
76 this->public.db->destroy(this->public.db);
77 this->public.printf_hook->destroy(this->public.printf_hook);
78 this->objects->destroy(this->objects);
79 if (this->public.integrity)
80 {
81 this->public.integrity->destroy(this->public.integrity);
82 }
83
84 if (lib->leak_detective)
85 {
86 lib->leak_detective->report(lib->leak_detective, detailed);
87 lib->leak_detective->destroy(lib->leak_detective);
88 }
89
90 threads_deinit();
91
92 free(this);
93 lib = NULL;
94 }
95
96 METHOD(library_t, get, void*,
97 private_library_t *this, char *name)
98 {
99 return this->objects->get(this->objects, name);
100 }
101
102 METHOD(library_t, set, bool,
103 private_library_t *this, char *name, void *object)
104 {
105 if (object)
106 {
107 if (this->objects->get(this->objects, name))
108 {
109 return FALSE;
110 }
111 this->objects->put(this->objects, name, object);
112 return TRUE;
113 }
114 return this->objects->remove(this->objects, name) != NULL;
115 }
116
117 /**
118 * Hashtable hash function
119 */
120 static u_int hash(char *key)
121 {
122 return chunk_hash(chunk_create(key, strlen(key)));
123 }
124
125 /**
126 * Hashtable equals function
127 */
128 static bool equals(char *a, char *b)
129 {
130 return streq(a, b);
131 }
132
133 /*
134 * see header file
135 */
136 bool library_init(char *settings)
137 {
138 private_library_t *this;
139 printf_hook_t *pfh;
140
141 INIT(this,
142 .public = {
143 .get = _get,
144 .set = _set,
145 },
146 );
147 lib = &this->public;
148
149 threads_init();
150
151 #ifdef LEAK_DETECTIVE
152 lib->leak_detective = leak_detective_create();
153 #endif /* LEAK_DETECTIVE */
154
155 pfh = printf_hook_create();
156 this->public.printf_hook = pfh;
157
158 pfh->add_handler(pfh, 'b', mem_printf_hook,
159 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
160 PRINTF_HOOK_ARGTYPE_END);
161 pfh->add_handler(pfh, 'B', chunk_printf_hook,
162 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
163 pfh->add_handler(pfh, 'H', host_printf_hook,
164 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
165 pfh->add_handler(pfh, 'N', enum_printf_hook,
166 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
167 PRINTF_HOOK_ARGTYPE_END);
168 pfh->add_handler(pfh, 'T', time_printf_hook,
169 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
170 PRINTF_HOOK_ARGTYPE_END);
171 pfh->add_handler(pfh, 'V', time_delta_printf_hook,
172 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_POINTER,
173 PRINTF_HOOK_ARGTYPE_END);
174 pfh->add_handler(pfh, 'Y', identification_printf_hook,
175 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
176 pfh->add_handler(pfh, 'R', traffic_selector_printf_hook,
177 PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_END);
178
179 this->objects = hashtable_create((hashtable_hash_t)hash,
180 (hashtable_equals_t)equals, 4);
181 this->public.settings = settings_create(settings);
182 this->public.crypto = crypto_factory_create();
183 this->public.creds = credential_factory_create();
184 this->public.credmgr = credential_manager_create();
185 this->public.encoding = cred_encoding_create();
186 this->public.fetcher = fetcher_manager_create();
187 this->public.db = database_factory_create();
188 this->public.processor = processor_create();
189 this->public.scheduler = scheduler_create();
190 this->public.plugins = plugin_loader_create();
191
192 if (lib->settings->get_bool(lib->settings,
193 "libstrongswan.integrity_test", FALSE))
194 {
195 #ifdef INTEGRITY_TEST
196 this->public.integrity = integrity_checker_create(CHECKSUM_LIBRARY);
197 if (!lib->integrity->check(lib->integrity, "libstrongswan", library_init))
198 {
199 DBG1(DBG_LIB, "integrity check of libstrongswan failed");
200 return FALSE;
201 }
202 #else /* !INTEGRITY_TEST */
203 DBG1(DBG_LIB, "integrity test enabled, but not supported");
204 return FALSE;
205 #endif /* INTEGRITY_TEST */
206 }
207 return TRUE;
208 }
209