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 <linux/capability.h>
26 #include <sys/prctl.h>
30 #include <sys/types.h>
39 # include <execinfo.h>
40 #endif /* HAVE_BACKTRACE */
45 #include <config/traffic_selector.h>
46 #include <config/proposal.h>
48 /* on some distros, a capset definition is missing */
49 #ifdef NO_CAPSET_DEFINED
50 extern int capset(cap_user_header_t hdrp
, const cap_user_data_t datap
);
51 #endif /* NO_CAPSET_DEFINED */
54 #include <fips/fips.h>
55 #include <fips_signature.h>
56 #endif /* INTEGRITY_TEST */
58 typedef struct private_daemon_t private_daemon_t
;
61 * Private additions to daemon_t, contains threads and internal functions.
63 struct private_daemon_t
{
65 * Public members of daemon_t.
70 * Signal set used for signal handling.
75 * The thread_id of main-thread.
77 pthread_t main_thread_id
;
81 * One and only instance of the daemon.
86 * hook in library for debugging messages
88 extern void (*dbg
) (int level
, char *fmt
, ...);
91 * Logging hook for library logs, spreads debug message over bus
93 static void dbg_bus(int level
, char *fmt
, ...)
98 charon
->bus
->vsignal(charon
->bus
, DBG_LIB
, level
, fmt
, args
);
103 * Logging hook for library logs, using stderr output
105 static void dbg_stderr(int level
, char *fmt
, ...)
112 fprintf(stderr
, "00[LIB] ");
113 vfprintf(stderr
, fmt
, args
);
114 fprintf(stderr
, "\n");
120 * Run the daemon and handle unix signals
122 static void run(private_daemon_t
*this)
126 /* handle SIGINT, SIGHUP ans SIGTERM in this handler */
128 sigaddset(&set
, SIGINT
);
129 sigaddset(&set
, SIGHUP
);
130 sigaddset(&set
, SIGTERM
);
137 error
= sigwait(&set
, &sig
);
140 DBG1(DBG_DMN
, "error %d while waiting for a signal", error
);
147 DBG1(DBG_DMN
, "signal of type SIGHUP received. Ignored");
152 DBG1(DBG_DMN
, "signal of type SIGINT received. Shutting down");
157 DBG1(DBG_DMN
, "signal of type SIGTERM received. Shutting down");
162 DBG1(DBG_DMN
, "unknown signal %d received. Ignored", sig
);
170 * Clean up all daemon resources
172 static void destroy(private_daemon_t
*this)
174 /* terminate all idle threads */
175 if (this->public.processor
)
177 this->public.processor
->set_threads(this->public.processor
, 0);
179 /* close all IKE_SAs */
180 if (this->public.ike_sa_manager
)
182 this->public.ike_sa_manager
->flush(this->public.ike_sa_manager
);
184 /* unload plugins to release threads */
185 lib
->plugins
->unload(lib
->plugins
);
186 DESTROY_IF(this->public.ike_sa_manager
);
187 DESTROY_IF(this->public.kernel_interface
);
188 DESTROY_IF(this->public.scheduler
);
189 DESTROY_IF(this->public.controller
);
190 DESTROY_IF(this->public.eap
);
192 DESTROY_IF(this->public.connect_manager
);
193 DESTROY_IF(this->public.mediation_manager
);
195 DESTROY_IF(this->public.backends
);
196 DESTROY_IF(this->public.attributes
);
197 DESTROY_IF(this->public.credentials
);
198 DESTROY_IF(this->public.sender
);
199 DESTROY_IF(this->public.receiver
);
200 DESTROY_IF(this->public.socket
);
201 /* wait until all threads are gone */
202 DESTROY_IF(this->public.processor
);
204 /* rehook library logging, shutdown logging */
206 DESTROY_IF(this->public.bus
);
207 DESTROY_IF(this->public.outlog
);
208 DESTROY_IF(this->public.syslog
);
209 DESTROY_IF(this->public.authlog
);
214 * Enforce daemon shutdown, with a given reason to do so.
216 static void kill_daemon(private_daemon_t
*this, char *reason
)
218 /* we send SIGTERM, so the daemon can cleanly shut down */
219 if (this->public.bus
)
221 DBG1(DBG_DMN
, "killing daemon: %s", reason
);
225 fprintf(stderr
, "killing daemon: %s\n", reason
);
227 if (this->main_thread_id
== pthread_self())
229 /* initialization failed, terminate daemon */
235 DBG1(DBG_DMN
, "sending SIGTERM to ourself");
237 /* thread must die, since he produced a ciritcal failure and can't continue */
243 * drop daemon capabilities
245 static void drop_capabilities(private_daemon_t
*this, bool full
)
247 struct __user_cap_header_struct hdr
;
248 struct __user_cap_data_struct data
;
250 /* CAP_NET_ADMIN is needed to use netlink */
251 u_int32_t keep
= (1<<CAP_NET_ADMIN
) | (1<<CAP_SYS_NICE
);
255 if (setgid(charon
->gid
) != 0)
257 kill_daemon(this, "change to unprivileged group failed");
259 if (setuid(charon
->uid
) != 0)
261 kill_daemon(this, "change to unprivileged user failed");
266 /* CAP_NET_BIND_SERVICE to bind services below port 1024 */
267 keep
|= (1<<CAP_NET_BIND_SERVICE
);
268 /* CAP_NET_RAW to create RAW sockets */
269 keep
|= (1<<CAP_NET_RAW
);
270 /* CAP_DAC_READ_SEARCH to read ipsec.secrets */
271 keep
|= (1<<CAP_DAC_READ_SEARCH
);
272 /* CAP_CHOWN to change file permissions (socket permissions) */
273 keep
|= (1<<CAP_CHOWN
);
274 /* CAP_SETUID to call setuid() */
275 keep
|= (1<<CAP_SETUID
);
276 /* CAP_SETGID to call setgid() */
277 keep
|= (1<<CAP_SETGID
);
280 /* we use the old capset version for now. For systems with version 2
281 * available, we specifiy version 1 excplicitly. */
282 #ifdef _LINUX_CAPABILITY_VERSION_1
283 hdr
.version
= _LINUX_CAPABILITY_VERSION_1
;
285 hdr
.version
= _LINUX_CAPABILITY_VERSION
;
288 data
.inheritable
= data
.effective
= data
.permitted
= keep
;
290 if (capset(&hdr
, &data
))
292 kill_daemon(this, "unable to drop daemon capabilities");
299 static void lookup_uid_gid(private_daemon_t
*this)
304 struct passwd passwd
, *pwp
;
306 if (getpwnam_r(IPSEC_USER
, &passwd
, buf
, sizeof(buf
), &pwp
) != 0 ||
309 kill_daemon(this, "resolving user '"IPSEC_USER
"' failed");
311 charon
->uid
= pwp
->pw_uid
;
317 struct group group
, *grp
;
319 if (getgrnam_r(IPSEC_GROUP
, &group
, buf
, sizeof(buf
), &grp
) != 0 ||
322 kill_daemon(this, "reslvoing group '"IPSEC_GROUP
"' failed");
324 charon
->gid
= grp
->gr_gid
;
330 * Initialize the daemon
332 static bool initialize(private_daemon_t
*this, bool syslog
, level_t levels
[])
336 /* for uncritical pseudo random numbers */
337 srandom(time(NULL
) + getpid());
339 /* setup bus and it's listeners first to enable log output */
340 this->public.bus
= bus_create();
341 this->public.outlog
= file_logger_create(stdout
);
342 this->public.syslog
= sys_logger_create(LOG_DAEMON
);
343 this->public.authlog
= sys_logger_create(LOG_AUTHPRIV
);
344 this->public.bus
->add_listener(this->public.bus
, &this->public.syslog
->listener
);
345 this->public.bus
->add_listener(this->public.bus
, &this->public.outlog
->listener
);
346 this->public.bus
->add_listener(this->public.bus
, &this->public.authlog
->listener
);
347 this->public.authlog
->set_level(this->public.authlog
, SIG_ANY
, LEVEL_AUDIT
);
348 /* set up hook to log dbg message in library via charons message bus */
351 /* apply loglevels */
352 for (signal
= 0; signal
< DBG_MAX
; signal
++)
354 this->public.syslog
->set_level(this->public.syslog
,
355 signal
, levels
[signal
]);
358 this->public.outlog
->set_level(this->public.outlog
,
359 signal
, levels
[signal
]);
363 DBG1(DBG_DMN
, "starting charon (strongSwan Version %s)", VERSION
);
365 #ifdef INTEGRITY_TEST
366 DBG1(DBG_DMN
, "integrity test of libstrongswan code");
367 if (fips_verify_hmac_signature(hmac_key
, hmac_signature
))
369 DBG1(DBG_DMN
, " integrity test passed");
373 DBG1(DBG_DMN
, " integrity test failed");
376 #endif /* INTEGRITY_TEST */
378 /* load secrets, ca certificates and crls */
379 this->public.processor
= processor_create();
380 this->public.scheduler
= scheduler_create();
381 this->public.credentials
= credential_manager_create();
382 this->public.controller
= controller_create();
383 this->public.eap
= eap_manager_create();
384 this->public.backends
= backend_manager_create();
385 this->public.attributes
= attribute_manager_create();
386 this->public.kernel_interface
= kernel_interface_create();
387 this->public.socket
= socket_create();
389 /* load plugins, further infrastructure may need it */
390 lib
->plugins
->load(lib
->plugins
, IPSEC_PLUGINDIR
,
391 lib
->settings
->get_str(lib
->settings
, "charon.load", PLUGINS
));
393 this->public.ike_sa_manager
= ike_sa_manager_create();
394 if (this->public.ike_sa_manager
== NULL
)
398 this->public.sender
= sender_create();
399 this->public.receiver
= receiver_create();
400 if (this->public.receiver
== NULL
)
406 this->public.connect_manager
= connect_manager_create();
407 if (this->public.connect_manager
== NULL
)
411 this->public.mediation_manager
= mediation_manager_create();
418 * Handle SIGSEGV/SIGILL signals raised by threads
420 static void segv_handler(int signal
)
422 #ifdef HAVE_BACKTRACE
428 size
= backtrace(array
, 20);
429 strings
= backtrace_symbols(array
, size
);
431 DBG1(DBG_JOB
, "thread %u received %s. Dumping %d frames from stack:",
432 pthread_self(), signal
== SIGSEGV ?
"SIGSEGV" : "SIGILL", size
);
434 for (i
= 0; i
< size
; i
++)
439 if (dladdr(array
[i
], &info
))
441 void *ptr
= array
[i
];
442 if (strstr(info
.dli_fname
, ".so"))
444 ptr
= (void*)(array
[i
] - info
.dli_fbase
);
446 DBG1(DBG_DMN
, " %s [%p]", info
.dli_fname
, ptr
);
450 #endif /* HAVE_DLADDR */
451 DBG1(DBG_DMN
, " %s", strings
[i
]);
454 #endif /* HAVE_DLADDR */
457 #else /* !HAVE_BACKTRACE */
458 DBG1(DBG_DMN
, "thread %u received %s",
459 pthread_self(), signal
== SIGSEGV ?
"SIGSEGV" : "SIGILL");
460 #endif /* HAVE_BACKTRACE */
461 DBG1(DBG_DMN
, "killing ourself, received critical signal");
468 private_daemon_t
*daemon_create(void)
470 struct sigaction action
;
471 private_daemon_t
*this = malloc_thing(private_daemon_t
);
474 this->public.kill
= (void (*) (daemon_t
*,char*))kill_daemon
;
476 /* NULL members for clean destruction */
477 this->public.socket
= NULL
;
478 this->public.ike_sa_manager
= NULL
;
479 this->public.credentials
= NULL
;
480 this->public.backends
= NULL
;
481 this->public.attributes
= NULL
;
482 this->public.sender
= NULL
;
483 this->public.receiver
= NULL
;
484 this->public.scheduler
= NULL
;
485 this->public.kernel_interface
= NULL
;
486 this->public.processor
= NULL
;
487 this->public.controller
= NULL
;
488 this->public.eap
= NULL
;
489 this->public.bus
= NULL
;
490 this->public.outlog
= NULL
;
491 this->public.syslog
= NULL
;
492 this->public.authlog
= NULL
;
494 this->public.connect_manager
= NULL
;
495 this->public.mediation_manager
= NULL
;
497 this->public.uid
= 0;
498 this->public.gid
= 0;
500 this->main_thread_id
= pthread_self();
502 /* add handler for SEGV and ILL,
503 * add handler for USR1 (cancellation).
504 * INT, TERM and HUP are handled by sigwait() in run() */
505 action
.sa_handler
= segv_handler
;
507 sigemptyset(&action
.sa_mask
);
508 sigaddset(&action
.sa_mask
, SIGINT
);
509 sigaddset(&action
.sa_mask
, SIGTERM
);
510 sigaddset(&action
.sa_mask
, SIGHUP
);
511 sigaction(SIGSEGV
, &action
, NULL
);
512 sigaction(SIGILL
, &action
, NULL
);
513 action
.sa_handler
= SIG_IGN
;
514 sigaction(SIGPIPE
, &action
, NULL
);
516 pthread_sigmask(SIG_SETMASK
, &action
.sa_mask
, 0);
522 * print command line usage and exit
524 static void usage(const char *msg
)
526 if (msg
!= NULL
&& *msg
!= '\0')
528 fprintf(stderr
, "%s\n", msg
);
530 fprintf(stderr
, "Usage: charon\n"
534 " [--debug-<type> <level>]\n"
535 " <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
536 " <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
537 " 2 = controlmore, 3 = raw, 4 = private)\n"
540 exit(msg
== NULL?
0 : 1);
544 * Main function, manages the daemon.
546 int main(int argc
, char *argv
[])
548 bool use_syslog
= FALSE
;
550 private_daemon_t
*private_charon
;
553 level_t levels
[DBG_MAX
];
556 /* logging for library during initialization, as we have no bus yet */
559 /* initialize library */
560 library_init(STRONGSWAN_CONF
);
561 lib
->printf_hook
->add_handler(lib
->printf_hook
, 'R',
562 traffic_selector_get_printf_hooks());
563 lib
->printf_hook
->add_handler(lib
->printf_hook
, 'P',
564 proposal_get_printf_hooks());
565 private_charon
= daemon_create();
566 charon
= (daemon_t
*)private_charon
;
568 lookup_uid_gid(private_charon
);
570 /* drop the capabilities we won't need for initialization */
571 prctl(PR_SET_KEEPCAPS
, 1);
572 drop_capabilities(private_charon
, FALSE
);
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 additional capabilites (bind & root) */
650 drop_capabilities(private_charon
, TRUE
);
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
);