4 * @brief Implementation of daemon_t and main of IKEv2-Daemon.
9 * Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
10 * Copyright (C) 2005-2006 Martin Willi
11 * Copyright (C) 2005 Jan Hutter
12 * Hochschule fuer Technik Rapperswil
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 #include <sys/types.h>
36 # include <execinfo.h>
37 #endif /* HAVE_BACKTRACE */
42 #include <crypto/ca.h>
43 #include <utils/fetcher.h>
44 #include <config/credentials/local_credential_store.h>
45 #include <config/backends/local_backend.h>
46 #include <sa/authenticators/eap/eap_method.h>
49 typedef struct private_daemon_t private_daemon_t
;
52 * Private additions to daemon_t, contains threads and internal functions.
54 struct private_daemon_t
{
56 * Public members of daemon_t.
61 * Signal set used for signal handling.
66 * The thread_id of main-thread.
68 pthread_t main_thread_id
;
72 * One and only instance of the daemon.
77 * hook in library for debugging messages
79 extern void (*dbg
) (int level
, char *fmt
, ...);
82 * Logging hook for library logs, spreads debug message over bus
84 static void dbg_bus(int level
, char *fmt
, ...)
89 charon
->bus
->vsignal(charon
->bus
, DBG_LIB
, level
, fmt
, args
);
94 * Logging hook for library logs, using stderr output
96 static void dbg_stderr(int level
, char *fmt
, ...)
101 fprintf(stderr
, "00[LIB] ");
102 vfprintf(stderr
, fmt
, args
);
103 fprintf(stderr
, "\n");
108 * Run the daemon and handle unix signals
110 static void run(private_daemon_t
*this)
112 /* reselect signals for this thread */
113 sigemptyset(&(this->signal_set
));
114 sigaddset(&(this->signal_set
), SIGINT
);
115 sigaddset(&(this->signal_set
), SIGHUP
);
116 sigaddset(&(this->signal_set
), SIGTERM
);
117 pthread_sigmask(SIG_BLOCK
, &(this->signal_set
), 0);
124 error
= sigwait(&(this->signal_set
), &signal_number
);
127 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
130 switch (signal_number
)
134 DBG1(DBG_DMN
, "signal of type SIGHUP received. Ignored");
139 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
143 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
147 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", signal_number
);
155 * Clean up all daemon resources
157 static void destroy(private_daemon_t
*this)
159 /* destruction is a non trivial task, we need to follow
160 * a strict order to prevent threading issues!
161 * Kill active threads first, except the sender, as
162 * the killed IKE_SA want to send delete messages.
164 /* we don't want to receive anything anymore... */
165 DESTROY_IF(this->public.receiver
);
166 /* ignore all incoming user requests */
167 DESTROY_IF(this->public.stroke
);
168 DESTROY_IF(this->public.controller
);
169 /* stop scheduing jobs */
170 DESTROY_IF(this->public.scheduler
);
171 /* stop processing jobs */
172 DESTROY_IF(this->public.thread_pool
);
173 /* shut down manager with all IKE SAs */
174 DESTROY_IF(this->public.ike_sa_manager
);
175 /* all child SAs should be down now, so kill kernel interface */
176 DESTROY_IF(this->public.kernel_interface
);
177 /* destroy other infrastructure */
178 DESTROY_IF(this->public.job_queue
);
179 DESTROY_IF(this->public.event_queue
);
180 DESTROY_IF(this->public.credentials
);
181 DESTROY_IF(this->public.cfg_store
);
182 DESTROY_IF(this->public.local_backend
);
184 /* we hope the sender could send the outstanding deletes, but
185 * we shut down here at any cost */
186 DESTROY_IF(this->public.sender
);
187 DESTROY_IF(this->public.socket
);
188 /* before destroying bus with its listeners, rehook library logs */
190 DESTROY_IF(this->public.bus
);
191 DESTROY_IF(this->public.outlog
);
192 DESTROY_IF(this->public.syslog
);
193 DESTROY_IF(this->public.authlog
);
198 * Enforce daemon shutdown, with a given reason to do so.
200 static void kill_daemon(private_daemon_t
*this, char *reason
)
202 /* we send SIGTERM, so the daemon can cleanly shut down */
203 DBG1(DBG_DMN
, "killing daemon: %s", reason
);
204 if (this->main_thread_id
== pthread_self())
206 /* initialization failed, terminate daemon */
213 DBG1(DBG_DMN
, "sending SIGTERM to ourself");
215 /* thread must die, since he produced a ciritcal failure and can't continue */
221 * Initialize the daemon, optional with a strict crl policy
223 static void initialize(private_daemon_t
*this, bool strict
, bool syslog
,
226 credential_store_t
* credentials
;
229 /* for uncritical pseudo random numbers */
230 srandom(time(NULL
) + getpid());
232 /* setup bus and it's listeners first to enable log output */
233 this->public.bus
= bus_create();
234 this->public.outlog
= file_logger_create(stdout
);
235 this->public.syslog
= sys_logger_create(LOG_DAEMON
);
236 this->public.authlog
= sys_logger_create(LOG_AUTHPRIV
);
237 this->public.bus
->add_listener(this->public.bus
, &this->public.syslog
->listener
);
238 this->public.bus
->add_listener(this->public.bus
, &this->public.outlog
->listener
);
239 this->public.bus
->add_listener(this->public.bus
, &this->public.authlog
->listener
);
240 this->public.authlog
->set_level(this->public.authlog
, SIG_ANY
, LEVEL_AUDIT
);
241 /* set up hook to log dbg message in library via charons message bus */
244 /* apply loglevels */
245 for (signal
= 0; signal
< DBG_MAX
; signal
++)
249 this->public.syslog
->set_level(this->public.syslog
,
250 signal
, levels
[signal
]);
254 this->public.outlog
->set_level(this->public.outlog
,
255 signal
, levels
[signal
]);
259 DBG1(DBG_DMN
, "starting charon (strongSwan Version %s)", VERSION
);
261 this->public.socket
= socket_create(IKEV2_UDP_PORT
, IKEV2_NATT_PORT
);
262 this->public.ike_sa_manager
= ike_sa_manager_create();
263 this->public.job_queue
= job_queue_create();
264 this->public.event_queue
= event_queue_create();
265 this->public.credentials
= (credential_store_t
*)local_credential_store_create(strict
);
266 this->public.cfg_store
= cfg_store_create();
267 this->public.local_backend
= local_backend_create();
268 this->public.cfg_store
->register_backend(this->public.cfg_store
,
269 &this->public.local_backend
->backend
);
271 /* initialize fetcher_t class */
272 fetcher_initialize();
274 /* load secrets, ca certificates and crls */
275 credentials
= this->public.credentials
;
276 credentials
->load_ca_certificates(credentials
);
277 credentials
->load_aa_certificates(credentials
);
278 credentials
->load_attr_certificates(credentials
);
279 credentials
->load_ocsp_certificates(credentials
);
280 credentials
->load_crls(credentials
);
281 credentials
->load_secrets(credentials
);
283 /* start building threads, we are multi-threaded NOW */
284 this->public.controller
= controller_create();
285 this->public.stroke
= stroke_create(this->public.local_backend
);
286 this->public.sender
= sender_create();
287 this->public.receiver
= receiver_create();
288 this->public.scheduler
= scheduler_create();
289 this->public.kernel_interface
= kernel_interface_create();
290 this->public.thread_pool
= thread_pool_create(NUMBER_OF_WORKING_THREADS
);
294 * Handle SIGSEGV/SIGILL signals raised by threads
296 void signal_handler(int signal
)
298 #ifdef HAVE_BACKTRACE
304 size
= backtrace(array
, 20);
305 strings
= backtrace_symbols(array
, size
);
307 DBG1(DBG_DMN
, "thread %u received %s. Dumping %d frames from stack:",
308 pthread_self(), signal
== SIGSEGV ?
"SIGSEGV" : "SIGILL", size
);
310 for (i
= 0; i
< size
; i
++)
312 DBG1(DBG_DMN
, " %s", strings
[i
]);
315 #else /* !HAVE_BACKTRACE */
316 DBG1(DBG_DMN
, "thread %u received %s",
317 pthread_self(), signal
== SIGSEGV ?
"SIGSEGV" : "SIGILL");
318 #endif /* HAVE_BACKTRACE */
319 DBG1(DBG_DMN
, "killing ourself hard after SIGSEGV");
326 private_daemon_t
*daemon_create(void)
328 private_daemon_t
*this = malloc_thing(private_daemon_t
);
329 struct sigaction action
;
332 this->public.kill
= (void (*) (daemon_t
*,char*))kill_daemon
;
334 /* NULL members for clean destruction */
335 this->public.socket
= NULL
;
336 this->public.ike_sa_manager
= NULL
;
337 this->public.job_queue
= NULL
;
338 this->public.event_queue
= NULL
;
339 this->public.credentials
= NULL
;
340 this->public.cfg_store
= NULL
;
341 this->public.local_backend
= NULL
;
342 this->public.sender
= NULL
;
343 this->public.receiver
= NULL
;
344 this->public.scheduler
= NULL
;
345 this->public.kernel_interface
= NULL
;
346 this->public.thread_pool
= NULL
;
347 this->public.controller
= NULL
;
348 this->public.stroke
= NULL
;
349 this->public.bus
= NULL
;
350 this->public.outlog
= NULL
;
351 this->public.syslog
= NULL
;
352 this->public.authlog
= NULL
;
354 this->main_thread_id
= pthread_self();
356 /* setup signal handling for all threads */
357 sigemptyset(&(this->signal_set
));
358 sigaddset(&(this->signal_set
), SIGSEGV
);
359 sigaddset(&(this->signal_set
), SIGINT
);
360 sigaddset(&(this->signal_set
), SIGHUP
);
361 sigaddset(&(this->signal_set
), SIGTERM
);
362 pthread_sigmask(SIG_BLOCK
, &(this->signal_set
), 0);
364 /* setup SIGSEGV handler for all threads */
365 action
.sa_handler
= signal_handler
;
366 action
.sa_mask
= this->signal_set
;
368 sigaction(SIGSEGV
, &action
, NULL
);
369 sigaction(SIGILL
, &action
, NULL
);
374 * print command line usage and exit
376 static void usage(const char *msg
)
378 if (msg
!= NULL
&& *msg
!= '\0')
380 fprintf(stderr
, "%s\n", msg
);
382 fprintf(stderr
, "Usage: charon\n"
385 " [--strictcrlpolicy]\n"
387 " [--crlcheckinterval <interval>]\n"
388 " [--eapdir <dir>]\n"
390 " [--debug-<type> <level>]\n"
391 " <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
392 " <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
393 " 2 = controlmore, 3 = raw, 4 = private)\n"
396 exit(msg
== NULL?
0 : 1);
400 * Main function, manages the daemon.
402 int main(int argc
, char *argv
[])
404 u_int crl_check_interval
= 0;
405 bool strict_crl_policy
= FALSE
;
406 bool cache_crls
= FALSE
;
407 bool use_syslog
= FALSE
;
408 char *eapdir
= IPSEC_EAPDIR
;
410 private_daemon_t
*private_charon
;
415 level_t levels
[DBG_MAX
];
418 /* use CTRL loglevel for default */
419 for (signal
= 0; signal
< DBG_MAX
; signal
++)
421 levels
[signal
] = LEVEL_CTRL
;
424 /* handle arguments */
427 struct option long_opts
[] = {
428 { "help", no_argument
, NULL
, 'h' },
429 { "version", no_argument
, NULL
, 'v' },
430 { "use-syslog", no_argument
, NULL
, 'l' },
431 { "strictcrlpolicy", no_argument
, NULL
, 'r' },
432 { "cachecrls", no_argument
, NULL
, 'C' },
433 { "crlcheckinterval", required_argument
, NULL
, 'x' },
434 { "eapdir", required_argument
, NULL
, 'e' },
435 /* TODO: handle "debug-all" */
436 { "debug-dmn", required_argument
, &signal
, DBG_DMN
},
437 { "debug-mgr", required_argument
, &signal
, DBG_MGR
},
438 { "debug-ike", required_argument
, &signal
, DBG_IKE
},
439 { "debug-chd", required_argument
, &signal
, DBG_CHD
},
440 { "debug-job", required_argument
, &signal
, DBG_JOB
},
441 { "debug-cfg", required_argument
, &signal
, DBG_CFG
},
442 { "debug-knl", required_argument
, &signal
, DBG_KNL
},
443 { "debug-net", required_argument
, &signal
, DBG_NET
},
444 { "debug-enc", required_argument
, &signal
, DBG_ENC
},
445 { "debug-lib", required_argument
, &signal
, DBG_LIB
},
449 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
458 printf("Linux strongSwan %s\n", VERSION
);
464 strict_crl_policy
= TRUE
;
470 crl_check_interval
= atoi(optarg
);
476 /* option is in signal */
477 levels
[signal
] = atoi(optarg
);
486 private_charon
= daemon_create();
487 charon
= (daemon_t
*)private_charon
;
489 /* initialize daemon */
490 initialize(private_charon
, strict_crl_policy
, use_syslog
, levels
);
492 /* load pluggable EAP modules */
493 eap_method_load(eapdir
);
495 /* set cache_crls and crl_check_interval options */
496 ca_info_set_options(cache_crls
, crl_check_interval
);
498 /* check/setup PID file */
499 if (stat(PID_FILE
, &stb
) == 0)
501 DBG1(DBG_DMN
, "charon already running (\""PID_FILE
"\" exists)");
502 destroy(private_charon
);
505 pid_file
= fopen(PID_FILE
, "w");
508 fprintf(pid_file
, "%d\n", getpid());
512 /* log socket info */
513 list
= charon
->kernel_interface
->create_address_list(charon
->kernel_interface
);
514 DBG1(DBG_NET
, "listening on %d addresses:", list
->get_count(list
));
515 while (list
->remove_first(list
, (void**)&host
) == SUCCESS
)
517 DBG1(DBG_NET
, " %H", host
);
527 /* normal termination, cleanup and exit */
528 destroy(private_charon
);