Moved scheduler_t to libhydra.
[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 <syslog.h>
23 #include <time.h>
24 #include <errno.h>
25
26 #ifdef CAPABILITIES
27 #ifdef HAVE_SYS_CAPABILITY_H
28 #include <sys/capability.h>
29 #endif /* HAVE_SYS_CAPABILITY_H */
30 #endif /* CAPABILITIES */
31
32 #include "daemon.h"
33
34 #include <library.h>
35 #include <hydra.h>
36 #include <config/proposal.h>
37 #include <kernel/kernel_handler.h>
38
39 #ifndef LOG_AUTHPRIV /* not defined on OpenSolaris */
40 #define LOG_AUTHPRIV LOG_AUTH
41 #endif
42
43 typedef struct private_daemon_t private_daemon_t;
44
45 /**
46 * Private additions to daemon_t, contains threads and internal functions.
47 */
48 struct private_daemon_t {
49 /**
50 * Public members of daemon_t.
51 */
52 daemon_t public;
53
54 /**
55 * Handler for kernel events
56 */
57 kernel_handler_t *kernel_handler;
58
59 /**
60 * capabilities to keep
61 */
62 #ifdef CAPABILITIES_LIBCAP
63 cap_t caps;
64 #endif /* CAPABILITIES_LIBCAP */
65 #ifdef CAPABILITIES_NATIVE
66 struct __user_cap_data_struct caps;
67 #endif /* CAPABILITIES_NATIVE */
68
69 };
70
71 /**
72 * One and only instance of the daemon.
73 */
74 daemon_t *charon;
75
76 /**
77 * hook in library for debugging messages
78 */
79 extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
80
81 /**
82 * we store the previous debug function so we can reset it
83 */
84 static void (*dbg_old) (debug_t group, level_t level, char *fmt, ...);
85
86 /**
87 * Logging hook for library logs, spreads debug message over bus
88 */
89 static void dbg_bus(debug_t group, level_t level, char *fmt, ...)
90 {
91 va_list args;
92
93 va_start(args, fmt);
94 charon->bus->vlog(charon->bus, group, level, fmt, args);
95 va_end(args);
96 }
97
98 /**
99 * Clean up all daemon resources
100 */
101 static void destroy(private_daemon_t *this)
102 {
103 /* terminate all idle threads */
104 hydra->processor->set_threads(hydra->processor, 0);
105
106 /* close all IKE_SAs */
107 if (this->public.ike_sa_manager)
108 {
109 this->public.ike_sa_manager->flush(this->public.ike_sa_manager);
110 }
111 DESTROY_IF(this->public.receiver);
112 DESTROY_IF(this->public.sender);
113 /* unload plugins to release threads */
114 lib->plugins->unload(lib->plugins);
115 #ifdef CAPABILITIES_LIBCAP
116 cap_free(this->caps);
117 #endif /* CAPABILITIES_LIBCAP */
118 DESTROY_IF(this->public.traps);
119 DESTROY_IF(this->public.ike_sa_manager);
120 DESTROY_IF(this->kernel_handler);
121 DESTROY_IF(this->public.kernel_interface);
122 DESTROY_IF(this->public.controller);
123 DESTROY_IF(this->public.eap);
124 DESTROY_IF(this->public.sim);
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 this->caps.effective |= 1 << cap;
152 this->caps.permitted |= 1 << cap;
153 this->caps.inheritable |= 1 << cap;
154 #endif /* CAPABILITIES_NATIVE */
155 }
156
157 METHOD(daemon_t, drop_capabilities, bool,
158 private_daemon_t *this)
159 {
160 #ifdef CAPABILITIES_LIBCAP
161 if (cap_set_proc(this->caps) != 0)
162 {
163 return FALSE;
164 }
165 #endif /* CAPABILITIES_LIBCAP */
166 #ifdef CAPABILITIES_NATIVE
167 struct __user_cap_header_struct header = {
168 .version = _LINUX_CAPABILITY_VERSION,
169 };
170 if (capset(&header, &this->caps) != 0)
171 {
172 return FALSE;
173 }
174 #endif /* CAPABILITIES_NATIVE */
175 return TRUE;
176 }
177
178 METHOD(daemon_t, start, void,
179 private_daemon_t *this)
180 {
181 /* start the engine, go multithreaded */
182 hydra->processor->set_threads(hydra->processor,
183 lib->settings->get_int(lib->settings, "charon.threads",
184 DEFAULT_THREADS));
185 }
186
187 /**
188 * Log loaded plugins
189 */
190 static void print_plugins()
191 {
192 char buf[512], *plugin;
193 int len = 0;
194 enumerator_t *enumerator;
195
196 buf[0] = '\0';
197 enumerator = lib->plugins->create_plugin_enumerator(lib->plugins);
198 while (len < sizeof(buf) && enumerator->enumerate(enumerator, &plugin))
199 {
200 len += snprintf(&buf[len], sizeof(buf)-len, "%s ", plugin);
201 }
202 enumerator->destroy(enumerator);
203 DBG1(DBG_DMN, "loaded plugins: %s", buf);
204 }
205
206 /**
207 * Initialize logging
208 */
209 static void initialize_loggers(private_daemon_t *this, bool use_stderr,
210 level_t levels[])
211 {
212 sys_logger_t *sys_logger;
213 file_logger_t *file_logger;
214 enumerator_t *enumerator;
215 char *facility, *filename;
216 int loggers_defined = 0;
217 debug_t group;
218 level_t def;
219 bool append, ike_name;
220 FILE *file;
221
222 /* setup sysloggers */
223 enumerator = lib->settings->create_section_enumerator(lib->settings,
224 "charon.syslog");
225 while (enumerator->enumerate(enumerator, &facility))
226 {
227 loggers_defined++;
228
229 ike_name = lib->settings->get_bool(lib->settings,
230 "charon.syslog.%s.ike_name", FALSE, facility);
231 if (streq(facility, "daemon"))
232 {
233 sys_logger = sys_logger_create(LOG_DAEMON, ike_name);
234 }
235 else if (streq(facility, "auth"))
236 {
237 sys_logger = sys_logger_create(LOG_AUTHPRIV, ike_name);
238 }
239 else
240 {
241 continue;
242 }
243 def = lib->settings->get_int(lib->settings,
244 "charon.syslog.%s.default", 1, facility);
245 for (group = 0; group < DBG_MAX; group++)
246 {
247 sys_logger->set_level(sys_logger, group,
248 lib->settings->get_int(lib->settings,
249 "charon.syslog.%s.%N", def,
250 facility, debug_lower_names, group));
251 }
252 this->public.sys_loggers->insert_last(this->public.sys_loggers,
253 sys_logger);
254 this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
255 }
256 enumerator->destroy(enumerator);
257
258 /* and file loggers */
259 enumerator = lib->settings->create_section_enumerator(lib->settings,
260 "charon.filelog");
261 while (enumerator->enumerate(enumerator, &filename))
262 {
263 loggers_defined++;
264 if (streq(filename, "stderr"))
265 {
266 file = stderr;
267 }
268 else if (streq(filename, "stdout"))
269 {
270 file = stdout;
271 }
272 else
273 {
274 append = lib->settings->get_bool(lib->settings,
275 "charon.filelog.%s.append", TRUE, filename);
276 file = fopen(filename, append ? "a" : "w");
277 if (file == NULL)
278 {
279 DBG1(DBG_DMN, "opening file %s for logging failed: %s",
280 filename, strerror(errno));
281 continue;
282 }
283 if (lib->settings->get_bool(lib->settings,
284 "charon.filelog.%s.flush_line", FALSE, filename))
285 {
286 setlinebuf(file);
287 }
288 }
289 file_logger = file_logger_create(file,
290 lib->settings->get_str(lib->settings,
291 "charon.filelog.%s.time_format", NULL, filename),
292 lib->settings->get_bool(lib->settings,
293 "charon.filelog.%s.ike_name", FALSE, filename));
294 def = lib->settings->get_int(lib->settings,
295 "charon.filelog.%s.default", 1, filename);
296 for (group = 0; group < DBG_MAX; group++)
297 {
298 file_logger->set_level(file_logger, group,
299 lib->settings->get_int(lib->settings,
300 "charon.filelog.%s.%N", def,
301 filename, debug_lower_names, group));
302 }
303 this->public.file_loggers->insert_last(this->public.file_loggers,
304 file_logger);
305 this->public.bus->add_listener(this->public.bus, &file_logger->listener);
306
307 }
308 enumerator->destroy(enumerator);
309
310 /* set up legacy style default loggers provided via command-line */
311 if (!loggers_defined)
312 {
313 /* set up default stdout file_logger */
314 file_logger = file_logger_create(stdout, NULL, FALSE);
315 this->public.bus->add_listener(this->public.bus, &file_logger->listener);
316 this->public.file_loggers->insert_last(this->public.file_loggers,
317 file_logger);
318 /* set up default daemon sys_logger */
319 sys_logger = sys_logger_create(LOG_DAEMON, FALSE);
320 this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
321 this->public.sys_loggers->insert_last(this->public.sys_loggers,
322 sys_logger);
323 for (group = 0; group < DBG_MAX; group++)
324 {
325 sys_logger->set_level(sys_logger, group, levels[group]);
326 if (use_stderr)
327 {
328 file_logger->set_level(file_logger, group, levels[group]);
329 }
330 }
331
332 /* set up default auth sys_logger */
333 sys_logger = sys_logger_create(LOG_AUTHPRIV, FALSE);
334 this->public.bus->add_listener(this->public.bus, &sys_logger->listener);
335 this->public.sys_loggers->insert_last(this->public.sys_loggers,
336 sys_logger);
337 sys_logger->set_level(sys_logger, DBG_ANY, LEVEL_AUDIT);
338 }
339 }
340
341 METHOD(daemon_t, initialize, bool,
342 private_daemon_t *this, bool syslog, level_t levels[])
343 {
344 /* for uncritical pseudo random numbers */
345 srandom(time(NULL) + getpid());
346
347 /* setup bus and it's listeners first to enable log output */
348 this->public.bus = bus_create();
349 /* set up hook to log dbg message in library via charons message bus */
350 dbg_old = dbg;
351 dbg = dbg_bus;
352
353 initialize_loggers(this, !syslog, levels);
354
355 DBG1(DBG_DMN, "Starting IKEv2 charon daemon (strongSwan "VERSION")");
356
357 if (lib->integrity)
358 {
359 DBG1(DBG_DMN, "integrity tests enabled:");
360 DBG1(DBG_DMN, "lib 'libstrongswan': passed file and segment integrity tests");
361 DBG1(DBG_DMN, "lib 'libhydra': passed file and segment integrity tests");
362 DBG1(DBG_DMN, "lib 'libcharon': passed file and segment integrity tests");
363 DBG1(DBG_DMN, "daemon 'charon': passed file integrity test");
364 }
365
366 /* load secrets, ca certificates and crls */
367 this->public.controller = controller_create();
368 this->public.eap = eap_manager_create();
369 this->public.sim = sim_manager_create();
370 this->public.backends = backend_manager_create();
371 this->public.kernel_interface = kernel_interface_create();
372 this->kernel_handler = kernel_handler_create();
373 this->public.socket = socket_manager_create();
374 this->public.traps = trap_manager_create();
375
376 /* load plugins, further infrastructure may need it */
377 if (!lib->plugins->load(lib->plugins, NULL,
378 lib->settings->get_str(lib->settings, "charon.load", PLUGINS)))
379 {
380 return FALSE;
381 }
382
383 print_plugins();
384
385 this->public.ike_sa_manager = ike_sa_manager_create();
386 if (this->public.ike_sa_manager == NULL)
387 {
388 return FALSE;
389 }
390 this->public.sender = sender_create();
391 this->public.receiver = receiver_create();
392 if (this->public.receiver == NULL)
393 {
394 return FALSE;
395 }
396
397 #ifdef ME
398 this->public.connect_manager = connect_manager_create();
399 if (this->public.connect_manager == NULL)
400 {
401 return FALSE;
402 }
403 this->public.mediation_manager = mediation_manager_create();
404 #endif /* ME */
405
406 return TRUE;
407 }
408
409 /**
410 * Create the daemon.
411 */
412 private_daemon_t *daemon_create()
413 {
414 private_daemon_t *this;
415
416 INIT(this,
417 .public = {
418 .keep_cap = _keep_cap,
419 .drop_capabilities = _drop_capabilities,
420 .initialize = _initialize,
421 .start = _start,
422 .file_loggers = linked_list_create(),
423 .sys_loggers = linked_list_create(),
424 },
425 );
426
427 #ifdef CAPABILITIES
428 #ifdef CAPABILITIES_LIBCAP
429 this->caps = cap_init();
430 #endif /* CAPABILITIES_LIBCAP */
431 keep_cap(this, CAP_NET_ADMIN);
432 if (lib->leak_detective)
433 {
434 keep_cap(this, CAP_SYS_NICE);
435 }
436 #endif /* CAPABILITIES */
437
438 return this;
439 }
440
441 /**
442 * Described in header.
443 */
444 void libcharon_deinit()
445 {
446
447 destroy((private_daemon_t*)charon);
448 charon = NULL;
449 }
450
451 /**
452 * Described in header.
453 */
454 bool libcharon_init()
455 {
456 private_daemon_t *this;
457
458 this = daemon_create();
459 charon = &this->public;
460
461 lib->printf_hook->add_handler(lib->printf_hook, 'P',
462 proposal_printf_hook,
463 PRINTF_HOOK_ARGTYPE_POINTER,
464 PRINTF_HOOK_ARGTYPE_END);
465
466 if (lib->integrity &&
467 !lib->integrity->check(lib->integrity, "libcharon", libcharon_init))
468 {
469 dbg(DBG_DMN, 1, "integrity check of libcharon failed");
470 return FALSE;
471 }
472
473 return TRUE;
474 }