9b6af22bb9657761840f8f81b2ee5a81a0b9a816
[strongswan.git] / src / libcharon / daemon.c
1 /*
2 * Copyright (C) 2006-2010 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2006 Daniel Roethlisberger
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include <stdio.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #include <time.h>
23
24 #ifdef CAPABILITIES
25 # ifdef HAVE_SYS_CAPABILITY_H
26 # include <sys/capability.h>
27 # elif defined(CAPABILITIES_NATIVE)
28 # include <linux/capability.h>
29 # endif /* CAPABILITIES_NATIVE */
30 #endif /* CAPABILITIES */
31
32 #include "daemon.h"
33
34 #include <library.h>
35 #include <plugins/plugin.h>
36 #include <config/proposal.h>
37 #include <kernel/kernel_handler.h>
38 #include <processing/jobs/start_action_job.h>
39
40 typedef struct private_daemon_t private_daemon_t;
41
42 /**
43 * Private additions to daemon_t, contains threads and internal functions.
44 */
45 struct private_daemon_t {
46 /**
47 * Public members of daemon_t.
48 */
49 daemon_t public;
50
51 /**
52 * Handler for kernel events
53 */
54 kernel_handler_t *kernel_handler;
55
56 /**
57 * capabilities to keep
58 */
59 #ifdef CAPABILITIES_LIBCAP
60 cap_t caps;
61 #endif /* CAPABILITIES_LIBCAP */
62 #ifdef CAPABILITIES_NATIVE
63 struct __user_cap_data_struct caps[2];
64 #endif /* CAPABILITIES_NATIVE */
65
66 };
67
68 /**
69 * One and only instance of the daemon.
70 */
71 daemon_t *charon;
72
73 /**
74 * hook in library for debugging messages
75 */
76 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
77
78 /**
79 * we store the previous debug function so we can reset it
80 */
81 static void (*dbg_old) (debug_t group, level_t level, char *fmt, ...);
82
83 /**
84 * Logging hook for library logs, spreads debug message over bus
85 */
86 static void dbg_bus(debug_t group, level_t level, char *fmt, ...)
87 {
88 va_list args;
89
90 va_start(args, fmt);
91 charon->bus->vlog(charon->bus, group, level, fmt, args);
92 va_end(args);
93 }
94
95 /**
96 * Clean up all daemon resources
97 */
98 static void destroy(private_daemon_t *this)
99 {
100 /* terminate all idle threads */
101 lib->processor->set_threads(lib->processor, 0);
102
103 /* close all IKE_SAs */
104 if (this->public.ike_sa_manager)
105 {
106 this->public.ike_sa_manager->flush(this->public.ike_sa_manager);
107 }
108 if (this->public.traps)
109 {
110 this->public.traps->flush(this->public.traps);
111 }
112 DESTROY_IF(this->public.receiver);
113 DESTROY_IF(this->public.sender);
114 /* unload plugins to release threads */
115 lib->plugins->unload(lib->plugins);
116 #ifdef CAPABILITIES_LIBCAP
117 cap_free(this->caps);
118 #endif /* CAPABILITIES_LIBCAP */
119 DESTROY_IF(this->kernel_handler);
120 DESTROY_IF(this->public.traps);
121 DESTROY_IF(this->public.shunts);
122 DESTROY_IF(this->public.ike_sa_manager);
123 DESTROY_IF(this->public.controller);
124 DESTROY_IF(this->public.eap);
125 #ifdef ME
126 DESTROY_IF(this->public.connect_manager);
127 DESTROY_IF(this->public.mediation_manager);
128 #endif /* ME */
129 DESTROY_IF(this->public.backends);
130 DESTROY_IF(this->public.socket);
131
132 /* rehook library logging, shutdown logging */
133 dbg = dbg_old;
134 DESTROY_IF(this->public.bus);
135 this->public.file_loggers->destroy_offset(this->public.file_loggers,
136 offsetof(file_logger_t, destroy));
137 this->public.sys_loggers->destroy_offset(this->public.sys_loggers,
138 offsetof(sys_logger_t, destroy));
139 free(this);
140 }
141
142 METHOD(daemon_t, keep_cap, void,
143 private_daemon_t *this, u_int cap)
144 {
145 #ifdef CAPABILITIES_LIBCAP
146 cap_set_flag(this->caps, CAP_EFFECTIVE, 1, &cap, CAP_SET);
147 cap_set_flag(this->caps, CAP_INHERITABLE, 1, &cap, CAP_SET);
148 cap_set_flag(this->caps, CAP_PERMITTED, 1, &cap, CAP_SET);
149 #endif /* CAPABILITIES_LIBCAP */
150 #ifdef CAPABILITIES_NATIVE
151 int i = 0;
152
153 if (cap >= 32)
154 {
155 i++;
156 cap -= 32;
157 }
158 this->caps[i].effective |= 1 << cap;
159 this->caps[i].permitted |= 1 << cap;
160 this->caps[i].inheritable |= 1 << cap;
161 #endif /* CAPABILITIES_NATIVE */
162 }
163
164 METHOD(daemon_t, drop_capabilities, bool,
165 private_daemon_t *this)
166 {
167 #ifdef CAPABILITIES_LIBCAP
168 if (cap_set_proc(this->caps) != 0)
169 {
170 return FALSE;
171 }
172 #endif /* CAPABILITIES_LIBCAP */
173 #ifdef CAPABILITIES_NATIVE
174 struct __user_cap_header_struct header = {
175 #if defined(_LINUX_CAPABILITY_VERSION_3)
176 .version = _LINUX_CAPABILITY_VERSION_3,
177 #elif defined(_LINUX_CAPABILITY_VERSION_2)
178 .version = _LINUX_CAPABILITY_VERSION_2,
179 #elif defined(_LINUX_CAPABILITY_VERSION_1)
180 .version = _LINUX_CAPABILITY_VERSION_1,
181 #else
182 .version = _LINUX_CAPABILITY_VERSION,
183 #endif
184 };
185 if (capset(&header, this->caps) != 0)
186 {
187 return FALSE;
188 }
189 #endif /* CAPABILITIES_NATIVE */
190 return TRUE;
191 }
192
193 METHOD(daemon_t, start, void,
194 private_daemon_t *this)
195 {
196 /* start the engine, go multithreaded */
197 lib->processor->set_threads(lib->processor,
198 lib->settings->get_int(lib->settings, "charon.threads",
199 DEFAULT_THREADS));
200 }
201
202 /**
203 * Log loaded plugins
204 */
205 static void print_plugins()
206 {
207 char buf[512];
208 int len = 0;
209 enumerator_t *enumerator;
210 plugin_t *plugin;
211
212 buf[0] = '\0';
213 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
214 while (len < sizeof(buf) && enumerator->enumerate(enumerator, &plugin, NULL))
215 {
216 len += snprintf(&buf[len], sizeof(buf)-len, "%s ",
217 plugin->get_name(plugin));
218 }
219 enumerator->destroy(enumerator);
220 DBG1(DBG_DMN, "loaded plugins: %s", buf);
221 }
222
223 METHOD(daemon_t, initialize, bool,
224 private_daemon_t *this)
225 {
226 DBG1(DBG_DMN, "Starting IKEv2 charon daemon (strongSwan "VERSION")");
227
228 if (lib->integrity)
229 {
230 DBG1(DBG_DMN, "integrity tests enabled:");
231 DBG1(DBG_DMN, "lib 'libstrongswan': passed file and segment integrity tests");
232 DBG1(DBG_DMN, "lib 'libhydra': passed file and segment integrity tests");
233 DBG1(DBG_DMN, "lib 'libcharon': passed file and segment integrity tests");
234 DBG1(DBG_DMN, "daemon 'charon': passed file integrity test");
235 }
236
237 /* load plugins, further infrastructure may need it */
238 if (!lib->plugins->load(lib->plugins, NULL,
239 lib->settings->get_str(lib->settings, "charon.load", PLUGINS)))
240 {
241 return FALSE;
242 }
243
244 print_plugins();
245
246 this->public.ike_sa_manager = ike_sa_manager_create();
247 if (this->public.ike_sa_manager == NULL)
248 {
249 return FALSE;
250 }
251 this->public.sender = sender_create();
252 this->public.receiver = receiver_create();
253 if (this->public.receiver == NULL)
254 {
255 return FALSE;
256 }
257
258 /* Queue start_action job */
259 lib->processor->queue_job(lib->processor, (job_t*)start_action_job_create());
260
261 #ifdef ME
262 this->public.connect_manager = connect_manager_create();
263 if (this->public.connect_manager == NULL)
264 {
265 return FALSE;
266 }
267 this->public.mediation_manager = mediation_manager_create();
268 #endif /* ME */
269
270 return TRUE;
271 }
272
273 /**
274 * Create the daemon.
275 */
276 private_daemon_t *daemon_create()
277 {
278 private_daemon_t *this;
279
280 INIT(this,
281 .public = {
282 .keep_cap = _keep_cap,
283 .drop_capabilities = _drop_capabilities,
284 .initialize = _initialize,
285 .start = _start,
286 .bus = bus_create(),
287 .file_loggers = linked_list_create(),
288 .sys_loggers = linked_list_create(),
289 },
290 );
291 charon = &this->public;
292 this->public.controller = controller_create();
293 this->public.eap = eap_manager_create();
294 this->public.backends = backend_manager_create();
295 this->public.socket = socket_manager_create();
296 this->public.traps = trap_manager_create();
297 this->public.shunts = shunt_manager_create();
298 this->kernel_handler = kernel_handler_create();
299
300 #ifdef CAPABILITIES
301 #ifdef CAPABILITIES_LIBCAP
302 this->caps = cap_init();
303 #endif /* CAPABILITIES_LIBCAP */
304 keep_cap(this, CAP_NET_ADMIN);
305 if (lib->leak_detective)
306 {
307 keep_cap(this, CAP_SYS_NICE);
308 }
309 #endif /* CAPABILITIES */
310
311 return this;
312 }
313
314 /**
315 * Described in header.
316 */
317 void libcharon_deinit()
318 {
319 destroy((private_daemon_t*)charon);
320 charon = NULL;
321 }
322
323 /**
324 * Described in header.
325 */
326 bool libcharon_init()
327 {
328 daemon_create();
329
330 /* for uncritical pseudo random numbers */
331 srandom(time(NULL) + getpid());
332
333 /* set up hook to log dbg message in library via charons message bus */
334 dbg_old = dbg;
335 dbg = dbg_bus;
336
337 lib->printf_hook->add_handler(lib->printf_hook, 'P',
338 proposal_printf_hook,
339 PRINTF_HOOK_ARGTYPE_POINTER,
340 PRINTF_HOOK_ARGTYPE_END);
341
342 if (lib->integrity &&
343 !lib->integrity->check(lib->integrity, "libcharon", libcharon_init))
344 {
345 dbg(DBG_DMN, 1, "integrity check of libcharon failed");
346 return FALSE;
347 }
348
349 return TRUE;
350 }