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
20 #include <linux/capability.h>
21 #include <sys/prctl.h>
25 #include <sys/types.h>
34 # include <execinfo.h>
35 #endif /* HAVE_BACKTRACE */
40 #include <config/traffic_selector.h>
42 /* on some distros, a capset definition is missing */
43 #ifdef NO_CAPSET_DEFINED
44 extern int capset(cap_user_header_t hdrp
, const cap_user_data_t datap
);
45 #endif /* NO_CAPSET_DEFINED */
48 #include <fips/fips.h>
49 #include <fips_signature.h>
50 #endif /* INTEGRITY_TEST */
52 typedef struct private_daemon_t private_daemon_t
;
55 * Private additions to daemon_t, contains threads and internal functions.
57 struct private_daemon_t
{
59 * Public members of daemon_t.
64 * Signal set used for signal handling.
69 * The thread_id of main-thread.
71 pthread_t main_thread_id
;
75 * One and only instance of the daemon.
80 * hook in library for debugging messages
82 extern void (*dbg
) (int level
, char *fmt
, ...);
85 * Logging hook for library logs, spreads debug message over bus
87 static void dbg_bus(int level
, char *fmt
, ...)
92 charon
->bus
->vsignal(charon
->bus
, DBG_LIB
, level
, fmt
, args
);
97 * Logging hook for library logs, using stderr output
99 static void dbg_stderr(int level
, char *fmt
, ...)
106 fprintf(stderr
, "00[LIB] ");
107 vfprintf(stderr
, fmt
, args
);
108 fprintf(stderr
, "\n");
114 * Run the daemon and handle unix signals
116 static void run(private_daemon_t
*this)
120 /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
122 sigaddset(&set
, SIGINT
);
123 sigaddset(&set
, SIGHUP
);
124 sigaddset(&set
, SIGTERM
);
131 error
= sigwait(&set
, &sig
);
134 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
141 DBG1(DBG_DMN
, "signal of type SIGHUP received. Ignored");
146 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
151 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
156 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", sig
);
164 * Clean up all daemon resources
166 static void destroy(private_daemon_t
*this)
168 /* terminate all idle threads */
169 if (this->public.processor
)
171 this->public.processor
->set_threads(this->public.processor
, 0);
173 /* close all IKE_SAs */
174 if (this->public.ike_sa_manager
)
176 this->public.ike_sa_manager
->flush(this->public.ike_sa_manager
);
178 DESTROY_IF(this->public.plugins
);
179 DESTROY_IF(this->public.ike_sa_manager
);
180 DESTROY_IF(this->public.kernel_interface
);
181 DESTROY_IF(this->public.scheduler
);
182 DESTROY_IF(this->public.controller
);
183 DESTROY_IF(this->public.eap
);
185 DESTROY_IF(this->public.connect_manager
);
186 DESTROY_IF(this->public.mediation_manager
);
188 DESTROY_IF(this->public.backends
);
189 DESTROY_IF(this->public.attributes
);
190 DESTROY_IF(this->public.credentials
);
191 DESTROY_IF(this->public.sender
);
192 DESTROY_IF(this->public.receiver
);
193 DESTROY_IF(this->public.socket
);
194 /* wait until all threads are gone */
195 DESTROY_IF(this->public.processor
);
197 /* rehook library logging, shutdown logging */
199 DESTROY_IF(this->public.bus
);
200 DESTROY_IF(this->public.outlog
);
201 DESTROY_IF(this->public.syslog
);
202 DESTROY_IF(this->public.authlog
);
207 * Enforce daemon shutdown, with a given reason to do so.
209 static void kill_daemon(private_daemon_t
*this, char *reason
)
211 /* we send SIGTERM, so the daemon can cleanly shut down */
212 if (this->public.bus
)
214 DBG1(DBG_DMN
, "killing daemon: %s", reason
);
218 fprintf(stderr
, "killing daemon: %s\n", reason
);
220 if (this->main_thread_id
== pthread_self())
222 /* initialization failed, terminate daemon */
228 DBG1(DBG_DMN
, "sending SIGTERM to ourself");
230 /* thread must die, since he produced a ciritcal failure and can't continue */
236 * drop daemon capabilities
238 static void drop_capabilities(private_daemon_t
*this, bool full
)
240 struct __user_cap_header_struct hdr
;
241 struct __user_cap_data_struct data
;
243 /* CAP_NET_ADMIN is needed to use netlink */
244 u_int32_t keep
= (1<<CAP_NET_ADMIN
) | (1<<CAP_SYS_NICE
);
248 if (setgid(charon
->gid
) != 0)
250 kill_daemon(this, "change to unprivileged group failed");
252 if (setuid(charon
->uid
) != 0)
254 kill_daemon(this, "change to unprivileged user failed");
259 /* CAP_NET_BIND_SERVICE to bind services below port 1024 */
260 keep
|= (1<<CAP_NET_BIND_SERVICE
);
261 /* CAP_NET_RAW to create RAW sockets */
262 keep
|= (1<<CAP_NET_RAW
);
263 /* CAP_DAC_READ_SEARCH to read ipsec.secrets */
264 keep
|= (1<<CAP_DAC_READ_SEARCH
);
265 /* CAP_CHOWN to change file permissions (socket permissions) */
266 keep
|= (1<<CAP_CHOWN
);
267 /* CAP_SETUID to call setuid() */
268 keep
|= (1<<CAP_SETUID
);
269 /* CAP_SETGID to call setgid() */
270 keep
|= (1<<CAP_SETGID
);
273 /* we use the old capset version for now. For systems with version 2
274 * available, we specifiy version 1 excplicitly. */
275 #ifdef _LINUX_CAPABILITY_VERSION_1
276 hdr
.version
= _LINUX_CAPABILITY_VERSION_1
;
278 hdr
.version
= _LINUX_CAPABILITY_VERSION
;
281 data
.inheritable
= data
.effective
= data
.permitted
= keep
;
283 if (capset(&hdr
, &data
))
285 kill_daemon(this, "unable to drop daemon capabilities");
292 static void lookup_uid_gid(private_daemon_t
*this)
297 struct passwd passwd
, *pwp
;
299 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) != 0 ||
302 kill_daemon(this, "resolving user '"IPSEC_USER
"' failed");
304 charon
->uid
= pwp
->pw_uid
;
310 struct group group
, *grp
;
312 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) != 0 ||
315 kill_daemon(this, "reslvoing group '"IPSEC_GROUP
"' failed");
317 charon
->gid
= grp
->gr_gid
;
323 * Initialize the daemon
325 static bool initialize(private_daemon_t
*this, bool syslog
, level_t levels
[])
329 /* for uncritical pseudo random numbers */
330 srandom(time(NULL
) + getpid());
332 /* setup bus and it's listeners first to enable log output */
333 this->public.bus
= bus_create();
334 this->public.outlog
= file_logger_create(stdout
);
335 this->public.syslog
= sys_logger_create(LOG_DAEMON
);
336 this->public.authlog
= sys_logger_create(LOG_AUTHPRIV
);
337 this->public.bus
->add_listener(this->public.bus
, &this->public.syslog
->listener
);
338 this->public.bus
->add_listener(this->public.bus
, &this->public.outlog
->listener
);
339 this->public.bus
->add_listener(this->public.bus
, &this->public.authlog
->listener
);
340 this->public.authlog
->set_level(this->public.authlog
, SIG_ANY
, LEVEL_AUDIT
);
341 /* set up hook to log dbg message in library via charons message bus */
344 /* apply loglevels */
345 for (signal
= 0; signal
< DBG_MAX
; signal
++)
347 this->public.syslog
->set_level(this->public.syslog
,
348 signal
, levels
[signal
]);
351 this->public.outlog
->set_level(this->public.outlog
,
352 signal
, levels
[signal
]);
356 DBG1(DBG_DMN
, "starting charon (strongSwan Version %s)", VERSION
);
358 #ifdef INTEGRITY_TEST
359 DBG1(DBG_DMN
, "integrity test of libstrongswan code");
360 if (fips_verify_hmac_signature(hmac_key
, hmac_signature
))
362 DBG1(DBG_DMN
, " integrity test passed");
366 DBG1(DBG_DMN
, " integrity test failed");
369 #endif /* INTEGRITY_TEST */
371 this->public.ike_sa_manager
= ike_sa_manager_create();
372 if (this->public.ike_sa_manager
== NULL
)
376 this->public.processor
= processor_create();
377 this->public.scheduler
= scheduler_create();
379 /* load secrets, ca certificates and crls */
380 this->public.credentials
= credential_manager_create();
381 this->public.controller
= controller_create();
382 this->public.eap
= eap_manager_create();
383 this->public.backends
= backend_manager_create();
384 this->public.attributes
= attribute_manager_create();
385 this->public.plugins
= plugin_loader_create();
386 this->public.kernel_interface
= kernel_interface_create();
387 this->public.socket
= socket_create();
388 this->public.sender
= sender_create();
389 this->public.receiver
= receiver_create();
390 if (this->public.receiver
== NULL
)
396 this->public.connect_manager
= connect_manager_create();
397 if (this->public.connect_manager
== NULL
)
401 this->public.mediation_manager
= mediation_manager_create();
404 this->public.plugins
->load(this->public.plugins
, IPSEC_PLUGINDIR
, "libcharon-");
410 * Handle SIGSEGV/SIGILL signals raised by threads
412 static void segv_handler(int signal
)
414 #ifdef HAVE_BACKTRACE
420 size
= backtrace(array
, 20);
421 strings
= backtrace_symbols(array
, size
);
423 DBG1(DBG_JOB
, "thread %u received %s. Dumping %d frames from stack:",
424 pthread_self(), signal
== SIGSEGV ?
"SIGSEGV" : "SIGILL", size
);
426 for (i
= 0; i
< size
; i
++)
428 DBG1(DBG_DMN
, " %s", strings
[i
]);
431 #else /* !HAVE_BACKTRACE */
432 DBG1(DBG_DMN
, "thread %u received %s",
433 pthread_self(), signal
== SIGSEGV ?
"SIGSEGV" : "SIGILL");
434 #endif /* HAVE_BACKTRACE */
435 DBG1(DBG_DMN
, "killing ourself, received critical signal");
442 private_daemon_t
*daemon_create(void)
444 struct sigaction action
;
445 private_daemon_t
*this = malloc_thing(private_daemon_t
);
448 this->public.kill
= (void (*) (daemon_t
*,char*))kill_daemon
;
450 /* NULL members for clean destruction */
451 this->public.socket
= NULL
;
452 this->public.ike_sa_manager
= NULL
;
453 this->public.credentials
= NULL
;
454 this->public.backends
= NULL
;
455 this->public.attributes
= NULL
;
456 this->public.sender
= NULL
;
457 this->public.receiver
= NULL
;
458 this->public.scheduler
= NULL
;
459 this->public.kernel_interface
= NULL
;
460 this->public.processor
= NULL
;
461 this->public.controller
= NULL
;
462 this->public.eap
= NULL
;
463 this->public.plugins
= NULL
;
464 this->public.bus
= NULL
;
465 this->public.outlog
= NULL
;
466 this->public.syslog
= NULL
;
467 this->public.authlog
= NULL
;
468 this->public.uid
= 0;
469 this->public.gid
= 0;
471 this->main_thread_id
= pthread_self();
473 /* add handler for SEGV and ILL,
474 * add handler for USR1 (cancellation).
475 * INT, TERM and HUP are handled by sigwait() in run() */
476 action
.sa_handler
= segv_handler
;
478 sigemptyset(&action
.sa_mask
);
479 sigaddset(&action
.sa_mask
, SIGINT
);
480 sigaddset(&action
.sa_mask
, SIGTERM
);
481 sigaddset(&action
.sa_mask
, SIGHUP
);
482 sigaction(SIGSEGV
, &action
, NULL
);
483 sigaction(SIGILL
, &action
, NULL
);
484 action
.sa_handler
= SIG_IGN
;
485 sigaction(SIGPIPE
, &action
, NULL
);
487 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, 0);
493 * print command line usage and exit
495 static void usage(const char *msg
)
497 if (msg
!= NULL
&& *msg
!= '\0')
499 fprintf(stderr
, "%s\n", msg
);
501 fprintf(stderr
, "Usage: charon\n"
505 " [--debug-<type> <level>]\n"
506 " <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
507 " <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
508 " 2 = controlmore, 3 = raw, 4 = private)\n"
511 exit(msg
== NULL?
0 : 1);
515 * Main function, manages the daemon.
517 int main(int argc
, char *argv
[])
519 bool use_syslog
= FALSE
;
521 private_daemon_t
*private_charon
;
524 level_t levels
[DBG_MAX
];
527 /* logging for library during initialization, as we have no bus yet */
530 /* initialize library */
531 library_init(STRONGSWAN_CONF
);
532 lib
->plugins
->load(lib
->plugins
, IPSEC_PLUGINDIR
, "libstrongswan-");
533 lib
->printf_hook
->add_handler(lib
->printf_hook
, 'R',
534 traffic_selector_get_printf_hooks());
535 private_charon
= daemon_create();
536 charon
= (daemon_t
*)private_charon
;
538 lookup_uid_gid(private_charon
);
540 /* drop the capabilities we won't need for initialization */
541 prctl(PR_SET_KEEPCAPS
, 1);
542 drop_capabilities(private_charon
, FALSE
);
544 /* use CTRL loglevel for default */
545 for (signal
= 0; signal
< DBG_MAX
; signal
++)
547 levels
[signal
] = LEVEL_CTRL
;
550 /* handle arguments */
553 struct option long_opts
[] = {
554 { "help", no_argument
, NULL
, 'h' },
555 { "version", no_argument
, NULL
, 'v' },
556 { "use-syslog", no_argument
, NULL
, 'l' },
557 /* TODO: handle "debug-all" */
558 { "debug-dmn", required_argument
, &signal
, DBG_DMN
},
559 { "debug-mgr", required_argument
, &signal
, DBG_MGR
},
560 { "debug-ike", required_argument
, &signal
, DBG_IKE
},
561 { "debug-chd", required_argument
, &signal
, DBG_CHD
},
562 { "debug-job", required_argument
, &signal
, DBG_JOB
},
563 { "debug-cfg", required_argument
, &signal
, DBG_CFG
},
564 { "debug-knl", required_argument
, &signal
, DBG_KNL
},
565 { "debug-net", required_argument
, &signal
, DBG_NET
},
566 { "debug-enc", required_argument
, &signal
, DBG_ENC
},
567 { "debug-lib", required_argument
, &signal
, DBG_LIB
},
571 int c
= getopt_long(argc
, argv
, "", long_opts
, NULL
);
580 printf("Linux strongSwan %s\n", VERSION
);
586 /* option is in signal */
587 levels
[signal
] = atoi(optarg
);
596 /* initialize daemon */
597 if (!initialize(private_charon
, use_syslog
, levels
))
599 DBG1(DBG_DMN
, "initialization failed - aborting charon");
600 destroy(private_charon
);
604 /* check/setup PID file */
605 if (stat(PID_FILE
, &stb
) == 0)
607 DBG1(DBG_DMN
, "charon already running (\""PID_FILE
"\" exists)");
608 destroy(private_charon
);
611 pid_file
= fopen(PID_FILE
, "w");
614 fprintf(pid_file
, "%d\n", getpid());
615 fchown(fileno(pid_file
), charon
->uid
, charon
->gid
);
619 /* drop additional capabilites (bind & root) */
620 drop_capabilities(private_charon
, TRUE
);
622 /* start the engine, go multithreaded */
623 charon
->processor
->set_threads(charon
->processor
,
624 lib
->settings
->get_int(lib
->settings
, "charon.threads",
630 /* normal termination, cleanup and exit */
631 destroy(private_charon
);