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