implemented IKE_SA rekeying
[strongswan.git] / src / charon / daemon.c
1 /**
2 * @file daemon.c
3 *
4 * @brief Implementation of daemon_t and main of IKEv2-Daemon.
5 *
6 */
7
8 /*
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
13 *
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>.
18 *
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
22 * for more details.
23 */
24
25 #include <stdio.h>
26 #include <signal.h>
27 #include <pthread.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <time.h>
32 #include <execinfo.h>
33 #include <string.h>
34 #include <getopt.h>
35
36 #include "daemon.h"
37
38 #include <types.h>
39 #include <config/credentials/local_credential_store.h>
40 #include <config/connections/local_connection_store.h>
41 #include <config/policies/local_policy_store.h>
42
43
44 typedef struct private_daemon_t private_daemon_t;
45
46 /**
47 * Private additions to daemon_t, contains threads and internal functions.
48 */
49 struct private_daemon_t {
50 /**
51 * Public members of daemon_t.
52 */
53 daemon_t public;
54
55 /**
56 * A logger_t object assigned for daemon things.
57 */
58 logger_t *logger;
59
60 /**
61 * Signal set used for signal handling.
62 */
63 sigset_t signal_set;
64
65 /**
66 * The thread_id of main-thread.
67 */
68 pthread_t main_thread_id;
69
70 /**
71 * Main loop function.
72 *
73 * @param this calling object
74 */
75 void (*run) (private_daemon_t *this);
76
77 /**
78 * Initialize the daemon.
79 *
80 * @param this calling object
81 * @param strict enforce a strict crl policy
82 */
83 void (*initialize) (private_daemon_t *this, bool strict);
84
85 /**
86 * Destroy the daemon.
87 *
88 * @param this calling object
89 */
90 void (*destroy) (private_daemon_t *this);
91 };
92
93 /**
94 * One and only instance of the daemon.
95 */
96 daemon_t *charon;
97
98 /**
99 * Implementation of private_daemon_t.run.
100 */
101 static void run(private_daemon_t *this)
102 {
103 /* reselect signals for this thread */
104 sigemptyset(&(this->signal_set));
105 sigaddset(&(this->signal_set), SIGINT);
106 sigaddset(&(this->signal_set), SIGHUP);
107 sigaddset(&(this->signal_set), SIGTERM);
108 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
109
110 while(TRUE)
111 {
112 int signal_number;
113 int error;
114
115 error = sigwait(&(this->signal_set), &signal_number);
116 if(error)
117 {
118 this->logger->log(this->logger, ERROR, "Error %d when waiting for signal", error);
119 return;
120 }
121 switch (signal_number)
122 {
123 case SIGHUP:
124 {
125 this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
126 break;
127 }
128 case SIGINT:
129 {
130 this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop");
131 return;
132 }
133 case SIGTERM:
134 this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop");
135 return;
136 default:
137 {
138 this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
139 break;
140 }
141 }
142 }
143 }
144
145 /**
146 * Implementation of daemon_t.kill.
147 */
148 static void kill_daemon(private_daemon_t *this, char *reason)
149 {
150 /* we send SIGTERM, so the daemon can cleanly shut down */
151 this->logger->log(this->logger, CONTROL, "Killing daemon: %s", reason);
152 if (this->main_thread_id == pthread_self())
153 {
154 /* initialization failed, terminate daemon */
155 this->destroy(this);
156 unlink(PID_FILE);
157 exit(-1);
158 }
159 else
160 {
161 this->logger->log(this->logger, CONTROL, "sending SIGTERM to ourself", reason);
162 raise(SIGTERM);
163 /* thread must die, since he produced a ciritcal failure and can't continue */
164 pthread_exit(NULL);
165 }
166 }
167
168 /**
169 * Implementation of private_daemon_t.initialize.
170 */
171 static void initialize(private_daemon_t *this, bool strict)
172 {
173 credential_store_t* credentials;
174
175 /* for uncritical pseudo random numbers */
176 srandom(time(NULL) + getpid());
177
178 this->public.configuration = configuration_create();
179 this->public.socket = socket_create(IKEV2_UDP_PORT, IKEV2_NATT_PORT);
180 this->public.interfaces = interfaces_create(IKEV2_UDP_PORT);
181 this->public.ike_sa_manager = ike_sa_manager_create();
182 this->public.job_queue = job_queue_create();
183 this->public.event_queue = event_queue_create();
184 this->public.send_queue = send_queue_create();
185 this->public.connections = (connection_store_t*)local_connection_store_create();
186 this->public.policies = (policy_store_t*)local_policy_store_create();
187 this->public.credentials = (credential_store_t*)local_credential_store_create(strict);
188
189 /* load keys, ca certificates and crls */
190 credentials = this->public.credentials;
191 credentials->load_ca_certificates(credentials);
192 credentials->load_crls(credentials);
193 credentials->load_private_keys(credentials);
194
195 /* start building threads, we are multi-threaded NOW */
196 this->public.stroke = stroke_create();
197 this->public.sender = sender_create();
198 this->public.receiver = receiver_create();
199 this->public.scheduler = scheduler_create();
200 this->public.kernel_interface = kernel_interface_create();
201 this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
202 }
203
204 /**
205 * Destory all initiated objects
206 */
207 static void destroy(private_daemon_t *this)
208 {
209 /* destruction is a non trivial task, we need to follow
210 * a strict order to prevent threading issues!
211 * Kill active threads first, except the sender, as
212 * the killed IKE_SA want to send delete messages.
213 */
214 if (this->public.receiver != NULL)
215 { /* we don't want to receive anything... */
216 this->public.receiver->destroy(this->public.receiver);
217 }
218 if (this->public.stroke != NULL)
219 { /* ignore all incoming user requests */
220 this->public.stroke->destroy(this->public.stroke);
221 }
222 if (this->public.scheduler != NULL)
223 { /* stop scheduing jobs */
224 this->public.scheduler->destroy(this->public.scheduler);
225 }
226 if (this->public.thread_pool != NULL)
227 { /* stop processing jobs */
228 this->public.thread_pool->destroy(this->public.thread_pool);
229 }
230 if (this->public.ike_sa_manager != NULL)
231 { /* shut down manager with all IKE SAs */
232 this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
233 }
234 if (this->public.kernel_interface != NULL)
235 { /* all child SAs should be down now, so kill kernel interface */
236 this->public.kernel_interface->destroy(this->public.kernel_interface);
237 }
238 /* destroy other infrastructure */
239 if (this->public.job_queue != NULL)
240 {
241 this->public.job_queue->destroy(this->public.job_queue);
242 }
243 if (this->public.event_queue != NULL)
244 {
245 this->public.event_queue->destroy(this->public.event_queue);
246 }
247 if (this->public.interfaces != NULL)
248 {
249 this->public.interfaces->destroy(this->public.interfaces);
250 }
251 if (this->public.configuration != NULL)
252 {
253 this->public.configuration->destroy(this->public.configuration);
254 }
255 if (this->public.credentials != NULL)
256 {
257 this->public.credentials->destroy(this->public.credentials);
258 }
259 if (this->public.connections != NULL)
260 {
261 this->public.connections->destroy(this->public.connections);
262 }
263 if (this->public.policies != NULL)
264 {
265 this->public.policies->destroy(this->public.policies);
266 }
267 /* we hope the sender could send the outstanding deletes, but
268 * we shut down here at any cost */
269 if (this->public.sender != NULL)
270 {
271 this->public.sender->destroy(this->public.sender);
272 }
273 if (this->public.send_queue != NULL)
274 {
275 this->public.send_queue->destroy(this->public.send_queue);
276 }
277 if (this->public.socket != NULL)
278 {
279 this->public.socket->destroy(this->public.socket);
280 }
281 free(this);
282 }
283
284 void signal_handler(int signal)
285 {
286 void *array[20];
287 size_t size;
288 char **strings;
289 size_t i;
290 logger_t *logger;
291
292 size = backtrace(array, 20);
293 strings = backtrace_symbols(array, size);
294 logger = logger_manager->get_logger(logger_manager, DAEMON);
295
296 logger->log(logger, ERROR, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size);
297
298 for (i = 0; i < size; i++)
299 {
300 logger->log(logger, ERROR, " %s", strings[i]);
301 }
302 free (strings);
303 logger->log(logger, ERROR, "Killing ourself hard after SIGSEGV");
304 raise(SIGKILL);
305 }
306
307 /**
308 * @brief Create the daemon.
309 *
310 * @return created daemon_t
311 */
312 private_daemon_t *daemon_create(void)
313 {
314 private_daemon_t *this = malloc_thing(private_daemon_t);
315 struct sigaction action;
316
317 /* assign methods */
318 this->run = run;
319 this->destroy = destroy;
320 this->initialize = initialize;
321 this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
322
323 /* NULL members for clean destruction */
324 this->public.socket = NULL;
325 this->public.interfaces = NULL;
326 this->public.ike_sa_manager = NULL;
327 this->public.job_queue = NULL;
328 this->public.event_queue = NULL;
329 this->public.send_queue = NULL;
330 this->public.configuration = NULL;
331 this->public.credentials = NULL;
332 this->public.connections = NULL;
333 this->public.policies = NULL;
334 this->public.sender= NULL;
335 this->public.receiver = NULL;
336 this->public.scheduler = NULL;
337 this->public.kernel_interface = NULL;
338 this->public.thread_pool = NULL;
339 this->public.stroke = NULL;
340
341 this->main_thread_id = pthread_self();
342
343 /* setup signal handling for all threads */
344 sigemptyset(&(this->signal_set));
345 sigaddset(&(this->signal_set), SIGSEGV);
346 sigaddset(&(this->signal_set), SIGINT);
347 sigaddset(&(this->signal_set), SIGHUP);
348 sigaddset(&(this->signal_set), SIGTERM);
349 pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
350
351 /* setup SIGSEGV handler for all threads */
352 action.sa_handler = signal_handler;
353 action.sa_mask = this->signal_set;
354 action.sa_flags = 0;
355 if (sigaction(SIGSEGV, &action, NULL) == -1)
356 {
357 this->logger->log(this->logger, ERROR, "signal handler setup for SIGSEGV failed");
358 }
359 return this;
360 }
361
362 static void usage(const char *msg)
363 {
364 if (msg != NULL && *msg != '\0')
365 fprintf(stderr, "%s\n", msg);
366 fprintf(stderr, "Usage: charon"
367 " [--help]"
368 " [--version]"
369 " [--use-syslog]"
370 " [--strictcrlpolicy]"
371 "\n"
372 );
373 exit(msg == NULL? 0 : 1);
374 }
375
376
377 /**
378 * Main function, manages the daemon.
379 */
380 int main(int argc, char *argv[])
381 {
382 bool strict_crl_policy = FALSE;
383
384 private_daemon_t *private_charon;
385 FILE *pid_file;
386 struct stat stb;
387
388 /* handle arguments */
389 for (;;)
390 {
391 static const struct option long_opts[] = {
392 { "help", no_argument, NULL, 'h' },
393 { "version", no_argument, NULL, 'v' },
394 { "use-syslog", no_argument, NULL, 'l' },
395 { "strictcrlpolicy", no_argument, NULL, 'r' },
396 { 0,0,0,0 }
397 };
398
399 int c = getopt_long(argc, argv, "", long_opts, NULL);
400
401 /* Note: "breaking" from case terminates loop */
402 switch (c)
403 {
404 case EOF: /* end of flags */
405 break;
406 case 'h':
407 usage(NULL);
408 break; /* not actually reached */
409 case 'v':
410 printf("Linux strongSwan %s\n", VERSION);
411 exit(0);
412 case 'l':
413 logger_manager->set_output(logger_manager, ALL_LOGGERS, NULL);
414 continue;
415 case 'r':
416 strict_crl_policy = TRUE;
417 continue;
418 default:
419 usage("");
420 break; /* not actually reached */
421 }
422 break;
423 }
424
425 private_charon = daemon_create();
426 charon = (daemon_t*)private_charon;
427
428 private_charon->logger = logger_manager->get_logger(logger_manager, DAEMON);
429
430 private_charon->logger->log(private_charon->logger, CONTROL,
431 "Starting Charon (strongSwan Version %s)", VERSION);
432
433 /* initialize daemon */
434 private_charon->initialize(private_charon, strict_crl_policy);
435
436 /* check/setup PID file */
437 if (stat(PID_FILE, &stb) == 0)
438 {
439 private_charon->logger->log(private_charon->logger, ERROR,
440 "charon already running (\""PID_FILE"\" exists)");
441 private_charon->destroy(private_charon);
442 exit(-1);
443 }
444 pid_file = fopen(PID_FILE, "w");
445 if (pid_file)
446 {
447 fprintf(pid_file, "%d\n", getpid());
448 fclose(pid_file);
449 }
450
451 /* run daemon */
452 private_charon->run(private_charon);
453
454 /* normal termination, cleanup and exit */
455 private_charon->destroy(private_charon);
456 unlink(PID_FILE);
457
458 return 0;
459 }
460
461