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