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