2 * Copyright (C) 2006-2007 Tobias Brunner
3 * Copyright (C) 2006 Daniel Roethlisberger
4 * Copyright (C) 2005-2008 Martin Willi
5 * Copyright (C) 2005 Jan Hutter
6 * Hochschule fuer Technik Rapperswil
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>.
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
22 #endif /* HAVE_DLADDR */
25 #include <sys/prctl.h>
29 #include <sys/types.h>
38 # include <execinfo.h>
39 #endif /* HAVE_BACKTRACE */
41 #include <sys/capability.h>
42 #endif /* CAPABILITIES */
47 #include <config/traffic_selector.h>
48 #include <config/proposal.h>
51 #include <fips/fips.h>
52 #include <fips/fips_signature.h>
53 #endif /* INTEGRITY_TEST */
55 typedef struct private_daemon_t private_daemon_t
;
58 * Private additions to daemon_t, contains threads and internal functions.
60 struct private_daemon_t
{
62 * Public members of daemon_t.
67 * Signal set used for signal handling.
72 * The thread_id of main-thread.
74 pthread_t main_thread_id
;
78 * capabilities to keep
81 #endif /* CAPABILITIES */
85 * One and only instance of the daemon.
90 * hook in library for debugging messages
92 extern void (*dbg
) (int level
, char *fmt
, ...);
95 * Logging hook for library logs, spreads debug message over bus
97 static void dbg_bus(int level
, char *fmt
, ...)
102 charon
->bus
->vsignal(charon
->bus
, DBG_LIB
, level
, NULL
, fmt
, args
);
107 * Logging hook for library logs, using stderr output
109 static void dbg_stderr(int level
, char *fmt
, ...)
116 fprintf(stderr
, "00[LIB] ");
117 vfprintf(stderr
, fmt
, args
);
118 fprintf(stderr
, "\n");
124 * Run the daemon and handle unix signals
126 static void run(private_daemon_t
*this)
130 /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
132 sigaddset(&set
, SIGINT
);
133 sigaddset(&set
, SIGHUP
);
134 sigaddset(&set
, SIGTERM
);
141 error
= sigwait(&set
, &sig
);
144 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
151 DBG1(DBG_DMN
, "signal of type SIGHUP received. Ignored");
156 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
161 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
166 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", sig
);
174 * Clean up all daemon resources
176 static void destroy(private_daemon_t
*this)
178 /* terminate all idle threads */
179 if (this->public.processor
)
181 this->public.processor
->set_threads(this->public.processor
, 0);
183 /* close all IKE_SAs */
184 if (this->public.ike_sa_manager
)
186 this->public.ike_sa_manager
->flush(this->public.ike_sa_manager
);
188 /* unload plugins to release threads */
189 lib
->plugins
->unload(lib
->plugins
);
191 cap_free(this->caps
);
192 #endif /* CAPABILITIES */
193 DESTROY_IF(this->public.ike_sa_manager
);
194 DESTROY_IF(this->public.kernel_interface
);
195 DESTROY_IF(this->public.scheduler
);
196 DESTROY_IF(this->public.controller
);
197 DESTROY_IF(this->public.eap
);
198 DESTROY_IF(this->public.sim
);
200 DESTROY_IF(this->public.connect_manager
);
201 DESTROY_IF(this->public.mediation_manager
);
203 DESTROY_IF(this->public.backends
);
204 DESTROY_IF(this->public.attributes
);
205 DESTROY_IF(this->public.credentials
);
206 DESTROY_IF(this->public.sender
);
207 DESTROY_IF(this->public.receiver
);
208 DESTROY_IF(this->public.socket
);
209 /* wait until all threads are gone */
210 DESTROY_IF(this->public.processor
);
212 /* rehook library logging, shutdown logging */
214 DESTROY_IF(this->public.bus
);
215 DESTROY_IF(this->public.outlog
);
216 DESTROY_IF(this->public.syslog
);
217 DESTROY_IF(this->public.authlog
);
222 * Enforce daemon shutdown, with a given reason to do so.
224 static void kill_daemon(private_daemon_t
*this, char *reason
)
226 /* we send SIGTERM, so the daemon can cleanly shut down */
227 if (this->public.bus
)
229 DBG1(DBG_DMN
, "killing daemon: %s", reason
);
233 fprintf(stderr
, "killing daemon: %s\n", reason
);
235 if (this->main_thread_id
== pthread_self())
237 /* initialization failed, terminate daemon */
243 DBG1(DBG_DMN
, "sending SIGTERM to ourself");
245 /* thread must die, since he produced a ciritcal failure and can't continue */
251 * drop daemon capabilities
253 static void drop_capabilities(private_daemon_t
*this)
255 prctl(PR_SET_KEEPCAPS
, 1);
257 if (setgid(charon
->gid
) != 0)
259 kill_daemon(this, "change to unprivileged group failed");
261 if (setuid(charon
->uid
) != 0)
263 kill_daemon(this, "change to unprivileged user failed");
267 if (cap_set_proc(this->caps
) != 0)
269 kill_daemon(this, "unable to drop daemon capabilities");
271 #endif /* CAPABILITIES */
275 * Implementation of daemon_t.keep_cap
277 static void keep_cap(private_daemon_t
*this, u_int cap
)
280 cap_set_flag(this->caps
, CAP_EFFECTIVE
, 1, &cap
, CAP_SET
);
281 cap_set_flag(this->caps
, CAP_INHERITABLE
, 1, &cap
, CAP_SET
);
282 cap_set_flag(this->caps
, CAP_PERMITTED
, 1, &cap
, CAP_SET
);
283 #endif /* CAPABILITIES */
289 static void lookup_uid_gid(private_daemon_t
*this)
294 struct passwd passwd
, *pwp
;
296 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) != 0 ||
299 kill_daemon(this, "resolving user '"IPSEC_USER
"' failed");
301 charon
->uid
= pwp
->pw_uid
;
307 struct group group
, *grp
;
309 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) != 0 ||
312 kill_daemon(this, "reslvoing group '"IPSEC_GROUP
"' failed");
314 charon
->gid
= grp
->gr_gid
;
320 * Initialize the daemon
322 static bool initialize(private_daemon_t
*this, bool syslog
, level_t levels
[])
326 /* for uncritical pseudo random numbers */
327 srandom(time(NULL
) + getpid());
329 /* setup bus and it's listeners first to enable log output */
330 this->public.bus
= bus_create();
331 this->public.outlog
= file_logger_create(stdout
);
332 this->public.syslog
= sys_logger_create(LOG_DAEMON
);
333 this->public.authlog
= sys_logger_create(LOG_AUTHPRIV
);
334 this->public.bus
->add_listener(this->public.bus
, &this->public.syslog
->listener
);
335 this->public.bus
->add_listener(this->public.bus
, &this->public.outlog
->listener
);
336 this->public.bus
->add_listener(this->public.bus
, &this->public.authlog
->listener
);
337 this->public.authlog
->set_level(this->public.authlog
, SIG_ANY
, LEVEL_AUDIT
);
338 /* set up hook to log dbg message in library via charons message bus */
341 /* apply loglevels */
342 for (signal
= 0; signal
< DBG_MAX
; signal
++)
344 this->public.syslog
->set_level(this->public.syslog
,
345 signal
, levels
[signal
]);
348 this->public.outlog
->set_level(this->public.outlog
,
349 signal
, levels
[signal
]);
353 DBG1(DBG_DMN
, "starting charon (strongSwan Version %s)", VERSION
);
355 /* load secrets, ca certificates and crls */
356 this->public.processor
= processor_create();
357 this->public.scheduler
= scheduler_create();
358 this->public.credentials
= credential_manager_create();
359 this->public.controller
= controller_create();
360 this->public.eap
= eap_manager_create();
361 this->public.sim
= sim_manager_create();
362 this->public.backends
= backend_manager_create();
363 this->public.attributes
= attribute_manager_create();
364 this->public.kernel_interface
= kernel_interface_create();
365 this->public.socket
= socket_create();
367 /* load plugins, further infrastructure may need it */
368 lib
->plugins
->load(lib
->plugins
, IPSEC_PLUGINDIR
,
369 lib
->settings
->get_str(lib
->settings
, "charon.load", PLUGINS
));
371 /* create the kernel interfaces */
372 this->public.kernel_interface
->create_interfaces(this->public.kernel_interface
);
374 #ifdef INTEGRITY_TEST
375 DBG1(DBG_DMN
, "integrity test of libstrongswan code");
376 if (fips_verify_hmac_signature(hmac_key
, hmac_signature
))
378 DBG1(DBG_DMN
, " integrity test passed");
382 DBG1(DBG_DMN
, " integrity test failed");
385 #endif /* INTEGRITY_TEST */
387 this->public.ike_sa_manager
= ike_sa_manager_create();
388 if (this->public.ike_sa_manager
== NULL
)
392 this->public.sender
= sender_create();
393 this->public.receiver
= receiver_create();
394 if (this->public.receiver
== NULL
)
400 this->public.connect_manager
= connect_manager_create();
401 if (this->public.connect_manager
== NULL
)
405 this->public.mediation_manager
= mediation_manager_create();
412 * Handle SIGSEGV/SIGILL signals raised by threads
414 static void segv_handler(int signal
)
416 #ifdef HAVE_BACKTRACE
422 size
= backtrace(array
, 20);
423 strings
= backtrace_symbols(array
, size
);
425 DBG1(DBG_JOB
, "thread %u received %s. Dumping %d frames from stack:",
426 pthread_self(), signal
== SIGSEGV ?
"SIGSEGV" : "SIGILL", size
);
428 for (i
= 0; i
< size
; i
++)
433 if (dladdr(array
[i
], &info
))
435 void *ptr
= array
[i
];
436 if (strstr(info
.dli_fname
, ".so"))
438 ptr
= (void*)(array
[i
] - info
.dli_fbase
);
440 DBG1(DBG_DMN
, " %s [%p]", info
.dli_fname
, ptr
);
444 #endif /* HAVE_DLADDR */
445 DBG1(DBG_DMN
, " %s", strings
[i
]);
448 #endif /* HAVE_DLADDR */
451 #else /* !HAVE_BACKTRACE */
452 DBG1(DBG_DMN
, "thread %u received %s",
453 pthread_self(), signal
== SIGSEGV ?
"SIGSEGV" : "SIGILL");
454 #endif /* HAVE_BACKTRACE */
455 DBG1(DBG_DMN
, "killing ourself, received critical signal");
462 private_daemon_t
*daemon_create(void)
464 struct sigaction action
;
465 private_daemon_t
*this = malloc_thing(private_daemon_t
);
468 this->public.kill
= (void (*) (daemon_t
*,char*))kill_daemon
;
469 this->public.keep_cap
= (void(*)(daemon_t
*, u_int cap
))keep_cap
;
471 /* NULL members for clean destruction */
472 this->public.socket
= NULL
;
473 this->public.ike_sa_manager
= NULL
;
474 this->public.credentials
= NULL
;
475 this->public.backends
= NULL
;
476 this->public.attributes
= NULL
;
477 this->public.sender
= NULL
;
478 this->public.receiver
= NULL
;
479 this->public.scheduler
= NULL
;
480 this->public.kernel_interface
= NULL
;
481 this->public.processor
= NULL
;
482 this->public.controller
= NULL
;
483 this->public.eap
= NULL
;
484 this->public.sim
= NULL
;
485 this->public.bus
= NULL
;
486 this->public.outlog
= NULL
;
487 this->public.syslog
= NULL
;
488 this->public.authlog
= NULL
;
490 this->public.connect_manager
= NULL
;
491 this->public.mediation_manager
= NULL
;
493 this->public.uid
= 0;
494 this->public.gid
= 0;
496 this->main_thread_id
= pthread_self();
498 this->caps
= cap_init();
499 keep_cap(this, CAP_NET_ADMIN
);
500 if (lib
->leak_detective
)
502 keep_cap(this, CAP_SYS_NICE
);
504 #endif /* CAPABILITIES */
506 /* add handler for SEGV and ILL,
507 * add handler for USR1 (cancellation).
508 * INT, TERM and HUP are handled by sigwait() in run() */
509 action
.sa_handler
= segv_handler
;
511 sigemptyset(&action
.sa_mask
);
512 sigaddset(&action
.sa_mask
, SIGINT
);
513 sigaddset(&action
.sa_mask
, SIGTERM
);
514 sigaddset(&action
.sa_mask
, SIGHUP
);
515 sigaction(SIGSEGV
, &action
, NULL
);
516 sigaction(SIGILL
, &action
, NULL
);
517 action
.sa_handler
= SIG_IGN
;
518 sigaction(SIGPIPE
, &action
, NULL
);
520 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, 0);
526 * print command line usage and exit
528 static void usage(const char *msg
)
530 if (msg
!= NULL
&& *msg
!= '\0')
532 fprintf(stderr
, "%s\n", msg
);
534 fprintf(stderr
, "Usage: charon\n"
538 " [--debug-<type> <level>]\n"
539 " <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
540 " <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
541 " 2 = controlmore, 3 = raw, 4 = private)\n"
544 exit(msg
== NULL?
0 : 1);
548 * Main function, manages the daemon.
550 int main(int argc
, char *argv
[])
552 bool use_syslog
= FALSE
;
554 private_daemon_t
*private_charon
;
557 level_t levels
[DBG_MAX
];
560 /* logging for library during initialization, as we have no bus yet */
563 /* initialize library */
564 library_init(STRONGSWAN_CONF
);
565 lib
->printf_hook
->add_handler(lib
->printf_hook
, 'R',
566 traffic_selector_get_printf_hooks());
567 lib
->printf_hook
->add_handler(lib
->printf_hook
, 'P',
568 proposal_get_printf_hooks());
569 private_charon
= daemon_create();
570 charon
= (daemon_t
*)private_charon
;
572 lookup_uid_gid(private_charon
);
574 /* use CTRL loglevel for default */
575 for (signal
= 0; signal
< DBG_MAX
; signal
++)
577 levels
[signal
] = LEVEL_CTRL
;
580 /* handle arguments */
583 struct option long_opts
[] = {
584 { "help", no_argument
, NULL
, 'h' },
585 { "version", no_argument
, NULL
, 'v' },
586 { "use-syslog", no_argument
, NULL
, 'l' },
587 /* TODO: handle "debug-all" */
588 { "debug-dmn", required_argument
, &signal
, DBG_DMN
},
589 { "debug-mgr", required_argument
, &signal
, DBG_MGR
},
590 { "debug-ike", required_argument
, &signal
, DBG_IKE
},
591 { "debug-chd", required_argument
, &signal
, DBG_CHD
},
592 { "debug-job", required_argument
, &signal
, DBG_JOB
},
593 { "debug-cfg", required_argument
, &signal
, DBG_CFG
},
594 { "debug-knl", required_argument
, &signal
, DBG_KNL
},
595 { "debug-net", required_argument
, &signal
, DBG_NET
},
596 { "debug-enc", required_argument
, &signal
, DBG_ENC
},
597 { "debug-lib", required_argument
, &signal
, DBG_LIB
},
601 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
610 printf("Linux strongSwan %s\n", VERSION
);
616 /* option is in signal */
617 levels
[signal
] = atoi(optarg
);
626 /* initialize daemon */
627 if (!initialize(private_charon
, use_syslog
, levels
))
629 DBG1(DBG_DMN
, "initialization failed - aborting charon");
630 destroy(private_charon
);
634 /* check/setup PID file */
635 if (stat(PID_FILE
, &stb
) == 0)
637 DBG1(DBG_DMN
, "charon already running (\""PID_FILE
"\" exists)");
638 destroy(private_charon
);
641 pid_file
= fopen(PID_FILE
, "w");
644 fprintf(pid_file
, "%d\n", getpid());
645 fchown(fileno(pid_file
), charon
->uid
, charon
->gid
);
649 /* drop the capabilities we won't need */
650 drop_capabilities(private_charon
);
652 /* start the engine, go multithreaded */
653 charon
->processor
->set_threads(charon
->processor
,
654 lib
->settings
->get_int(lib
->settings
, "charon.threads",
660 /* normal termination, cleanup and exit */
661 destroy(private_charon
);