2005-11-03 07:18:16 +00:00
|
|
|
/**
|
|
|
|
* @file daemon.c
|
|
|
|
*
|
2005-12-06 12:54:34 +00:00
|
|
|
* @brief Implementation of daemon_t and main of IKEv2-Daemon.
|
2005-11-03 07:18:16 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2006-06-22 06:36:28 +00:00
|
|
|
* Copyright (C) 2006 Tobias Brunner, Daniel Roethlisberger
|
2006-07-07 08:49:06 +00:00
|
|
|
* Copyright (C) 2005-2006 Martin Willi
|
|
|
|
* Copyright (C) 2005 Jan Hutter
|
2005-11-03 07:18:16 +00:00
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
2006-10-18 11:46:13 +00:00
|
|
|
|
2005-11-03 08:56:04 +00:00
|
|
|
#include <stdio.h>
|
2005-11-16 12:06:34 +00:00
|
|
|
#include <signal.h>
|
|
|
|
#include <pthread.h>
|
2006-03-14 13:22:48 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2006-07-27 12:18:40 +00:00
|
|
|
#include <time.h>
|
2006-04-20 07:06:39 +00:00
|
|
|
#include <string.h>
|
2006-06-14 12:42:36 +00:00
|
|
|
#include <getopt.h>
|
2006-07-28 09:45:18 +00:00
|
|
|
#include <errno.h>
|
2006-12-11 09:29:34 +00:00
|
|
|
#ifdef HAVE_BACKTRACE
|
|
|
|
# include <execinfo.h>
|
|
|
|
#endif /* HAVE_BACKTRACE */
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
#include "daemon.h"
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2006-10-31 12:27:59 +00:00
|
|
|
#include <library.h>
|
2007-04-04 07:49:05 +00:00
|
|
|
#include <crypto/ca.h>
|
2007-03-07 19:28:03 +00:00
|
|
|
#include <utils/fetcher.h>
|
2006-07-05 10:53:20 +00:00
|
|
|
#include <config/credentials/local_credential_store.h>
|
2006-04-25 10:06:30 +00:00
|
|
|
#include <config/connections/local_connection_store.h>
|
|
|
|
#include <config/policies/local_policy_store.h>
|
2007-02-12 15:56:47 +00:00
|
|
|
#include <sa/authenticators/eap/eap_method.h>
|
2005-11-03 17:26:49 +00:00
|
|
|
|
2005-11-03 08:56:04 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
typedef struct private_daemon_t private_daemon_t;
|
2005-11-16 12:06:34 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Private additions to daemon_t, contains threads and internal functions.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
struct private_daemon_t {
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Public members of daemon_t.
|
2005-11-29 10:25:07 +00:00
|
|
|
*/
|
|
|
|
daemon_t public;
|
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Signal set used for signal handling.
|
2005-11-29 10:25:07 +00:00
|
|
|
*/
|
|
|
|
sigset_t signal_set;
|
|
|
|
|
2005-11-29 11:27:25 +00:00
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* The thread_id of main-thread.
|
2005-11-29 11:27:25 +00:00
|
|
|
*/
|
2005-12-04 11:46:58 +00:00
|
|
|
pthread_t main_thread_id;
|
2005-11-29 10:25:07 +00:00
|
|
|
};
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2006-09-27 14:14:44 +00:00
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* One and only instance of the daemon.
|
2005-11-29 10:25:07 +00:00
|
|
|
*/
|
|
|
|
daemon_t *charon;
|
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
/**
|
|
|
|
* hook in library for debugging messages
|
|
|
|
*/
|
|
|
|
extern void (*dbg) (int level, char *fmt, ...);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logging hook for library logs, spreads debug message over bus
|
|
|
|
*/
|
|
|
|
static void dbg_bus(int level, char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
2006-10-26 09:46:56 +00:00
|
|
|
charon->bus->vsignal(charon->bus, DBG_LIB, level, fmt, args);
|
2006-10-18 11:46:13 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logging hook for library logs, using stderr output
|
|
|
|
*/
|
|
|
|
static void dbg_stderr(int level, char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, fmt);
|
|
|
|
fprintf(stderr, "00[LIB] ");
|
|
|
|
vfprintf(stderr, fmt, args);
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2005-11-16 12:06:34 +00:00
|
|
|
/**
|
2006-09-27 14:14:44 +00:00
|
|
|
* Run the daemon and handle unix signals
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
static void run(private_daemon_t *this)
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
|
|
|
/* reselect signals for this thread */
|
|
|
|
sigemptyset(&(this->signal_set));
|
|
|
|
sigaddset(&(this->signal_set), SIGINT);
|
|
|
|
sigaddset(&(this->signal_set), SIGHUP);
|
|
|
|
sigaddset(&(this->signal_set), SIGTERM);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
|
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
while(TRUE)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
int signal_number;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
error = sigwait(&(this->signal_set), &signal_number);
|
|
|
|
if(error)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "error %d while waiting for a signal", error);
|
2005-11-29 10:25:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (signal_number)
|
|
|
|
{
|
2005-11-16 12:06:34 +00:00
|
|
|
case SIGHUP:
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "signal of type SIGHUP received. Ignored");
|
2005-11-16 12:06:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SIGINT:
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "signal of type SIGINT received. Shutting down");
|
2005-11-16 12:06:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case SIGTERM:
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "signal of type SIGTERM received. Shutting down");
|
2005-11-16 12:06:34 +00:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "unknown signal %d received. Ignored", signal_number);
|
2005-11-16 12:06:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-09-27 14:14:44 +00:00
|
|
|
* Clean up all daemon resources
|
|
|
|
*/
|
|
|
|
static void destroy(private_daemon_t *this)
|
|
|
|
{
|
|
|
|
/* destruction is a non trivial task, we need to follow
|
|
|
|
* a strict order to prevent threading issues!
|
|
|
|
* Kill active threads first, except the sender, as
|
|
|
|
* the killed IKE_SA want to send delete messages.
|
|
|
|
*/
|
|
|
|
/* we don't want to receive anything anymore... */
|
|
|
|
DESTROY_IF(this->public.receiver);
|
|
|
|
/* ignore all incoming user requests */
|
|
|
|
DESTROY_IF(this->public.stroke);
|
|
|
|
/* stop scheduing jobs */
|
|
|
|
DESTROY_IF(this->public.scheduler);
|
|
|
|
/* stop processing jobs */
|
|
|
|
DESTROY_IF(this->public.thread_pool);
|
|
|
|
/* shut down manager with all IKE SAs */
|
|
|
|
DESTROY_IF(this->public.ike_sa_manager);
|
|
|
|
/* all child SAs should be down now, so kill kernel interface */
|
|
|
|
DESTROY_IF(this->public.kernel_interface);
|
|
|
|
/* destroy other infrastructure */
|
|
|
|
DESTROY_IF(this->public.job_queue);
|
|
|
|
DESTROY_IF(this->public.event_queue);
|
|
|
|
DESTROY_IF(this->public.configuration);
|
|
|
|
DESTROY_IF(this->public.credentials);
|
|
|
|
DESTROY_IF(this->public.connections);
|
|
|
|
DESTROY_IF(this->public.policies);
|
2006-10-18 11:46:13 +00:00
|
|
|
sched_yield();
|
2006-09-27 14:14:44 +00:00
|
|
|
/* we hope the sender could send the outstanding deletes, but
|
2006-10-18 11:46:13 +00:00
|
|
|
* we shut down here at any cost */
|
2006-09-27 14:14:44 +00:00
|
|
|
DESTROY_IF(this->public.sender);
|
|
|
|
DESTROY_IF(this->public.socket);
|
2006-10-18 11:46:13 +00:00
|
|
|
/* before destroying bus with its listeners, rehook library logs */
|
|
|
|
dbg = dbg_stderr;
|
|
|
|
DESTROY_IF(this->public.bus);
|
|
|
|
DESTROY_IF(this->public.outlog);
|
|
|
|
DESTROY_IF(this->public.syslog);
|
|
|
|
DESTROY_IF(this->public.authlog);
|
2006-09-27 14:14:44 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enforce daemon shutdown, with a given reason to do so.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 11:27:25 +00:00
|
|
|
static void kill_daemon(private_daemon_t *this, char *reason)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
/* we send SIGTERM, so the daemon can cleanly shut down */
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "killing daemon: %s", reason);
|
2005-12-04 11:46:58 +00:00
|
|
|
if (this->main_thread_id == pthread_self())
|
2005-11-29 11:27:25 +00:00
|
|
|
{
|
|
|
|
/* initialization failed, terminate daemon */
|
2006-09-27 14:14:44 +00:00
|
|
|
destroy(this);
|
2006-03-14 13:22:48 +00:00
|
|
|
unlink(PID_FILE);
|
2005-11-29 11:27:25 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "sending SIGTERM to ourself");
|
2006-06-20 10:05:56 +00:00
|
|
|
raise(SIGTERM);
|
2005-11-29 11:27:25 +00:00
|
|
|
/* thread must die, since he produced a ciritcal failure and can't continue */
|
|
|
|
pthread_exit(NULL);
|
|
|
|
}
|
2005-11-29 10:25:07 +00:00
|
|
|
}
|
2005-11-16 12:06:34 +00:00
|
|
|
|
|
|
|
/**
|
2006-09-27 14:14:44 +00:00
|
|
|
* Initialize the daemon, optional with a strict crl policy
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2006-10-18 11:46:13 +00:00
|
|
|
static void initialize(private_daemon_t *this, bool strict, bool syslog,
|
|
|
|
level_t levels[])
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2006-06-20 06:05:01 +00:00
|
|
|
credential_store_t* credentials;
|
2006-10-18 11:46:13 +00:00
|
|
|
signal_t signal;
|
2006-04-25 10:06:30 +00:00
|
|
|
|
2006-07-27 12:18:40 +00:00
|
|
|
/* for uncritical pseudo random numbers */
|
|
|
|
srandom(time(NULL) + getpid());
|
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
/* setup bus and it's listeners first to enable log output */
|
|
|
|
this->public.bus = bus_create();
|
|
|
|
this->public.outlog = file_logger_create(stdout);
|
|
|
|
this->public.syslog = sys_logger_create(LOG_DAEMON);
|
|
|
|
this->public.authlog = sys_logger_create(LOG_AUTHPRIV);
|
|
|
|
this->public.bus->add_listener(this->public.bus, &this->public.syslog->listener);
|
|
|
|
this->public.bus->add_listener(this->public.bus, &this->public.outlog->listener);
|
|
|
|
this->public.bus->add_listener(this->public.bus, &this->public.authlog->listener);
|
|
|
|
this->public.authlog->set_level(this->public.authlog, SIG_ANY, LEVEL_AUDIT);
|
|
|
|
/* set up hook to log dbg message in library via charons message bus */
|
|
|
|
dbg = dbg_bus;
|
|
|
|
|
|
|
|
/* apply loglevels */
|
2006-10-26 09:46:56 +00:00
|
|
|
for (signal = 0; signal < DBG_MAX; signal++)
|
2006-10-18 11:46:13 +00:00
|
|
|
{
|
|
|
|
if (syslog)
|
|
|
|
{
|
|
|
|
this->public.syslog->set_level(this->public.syslog,
|
|
|
|
signal, levels[signal]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->public.outlog->set_level(this->public.outlog,
|
|
|
|
signal, levels[signal]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "starting charon (strongSwan Version %s)", VERSION);
|
2006-10-18 11:46:13 +00:00
|
|
|
|
2006-03-16 15:25:06 +00:00
|
|
|
this->public.configuration = configuration_create();
|
2006-06-22 06:36:28 +00:00
|
|
|
this->public.socket = socket_create(IKEV2_UDP_PORT, IKEV2_NATT_PORT);
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.ike_sa_manager = ike_sa_manager_create();
|
|
|
|
this->public.job_queue = job_queue_create();
|
|
|
|
this->public.event_queue = event_queue_create();
|
2006-04-25 10:06:30 +00:00
|
|
|
this->public.connections = (connection_store_t*)local_connection_store_create();
|
|
|
|
this->public.policies = (policy_store_t*)local_policy_store_create();
|
2006-06-20 08:43:57 +00:00
|
|
|
this->public.credentials = (credential_store_t*)local_credential_store_create(strict);
|
2006-06-20 06:05:01 +00:00
|
|
|
|
2007-03-07 19:28:03 +00:00
|
|
|
/* initialize fetcher_t class */
|
|
|
|
fetcher_initialize();
|
|
|
|
|
2006-09-18 07:42:57 +00:00
|
|
|
/* load secrets, ca certificates and crls */
|
2006-06-20 06:05:01 +00:00
|
|
|
credentials = this->public.credentials;
|
2006-06-20 08:43:57 +00:00
|
|
|
credentials->load_ca_certificates(credentials);
|
2007-03-08 16:47:18 +00:00
|
|
|
credentials->load_ocsp_certificates(credentials);
|
2006-06-20 08:43:57 +00:00
|
|
|
credentials->load_crls(credentials);
|
2006-09-18 07:42:57 +00:00
|
|
|
credentials->load_secrets(credentials);
|
2006-04-25 10:06:30 +00:00
|
|
|
|
|
|
|
/* start building threads, we are multi-threaded NOW */
|
|
|
|
this->public.stroke = stroke_create();
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.sender = sender_create();
|
|
|
|
this->public.receiver = receiver_create();
|
|
|
|
this->public.scheduler = scheduler_create();
|
2006-02-09 16:25:02 +00:00
|
|
|
this->public.kernel_interface = kernel_interface_create();
|
|
|
|
this->public.thread_pool = thread_pool_create(NUMBER_OF_WORKING_THREADS);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-09-27 14:14:44 +00:00
|
|
|
* Handle SIGSEGV/SIGILL signals raised by threads
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2006-04-10 08:07:38 +00:00
|
|
|
void signal_handler(int signal)
|
|
|
|
{
|
2006-12-11 09:29:34 +00:00
|
|
|
#ifdef HAVE_BACKTRACE
|
2006-04-10 08:07:38 +00:00
|
|
|
void *array[20];
|
|
|
|
size_t size;
|
|
|
|
char **strings;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
size = backtrace(array, 20);
|
|
|
|
strings = backtrace_symbols(array, size);
|
|
|
|
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "thread %u received %s. Dumping %d frames from stack:",
|
2006-12-06 13:59:13 +00:00
|
|
|
pthread_self(), signal == SIGSEGV ? "SIGSEGV" : "SIGILL", size);
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, " %s", strings[i]);
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
free (strings);
|
2006-12-06 13:59:13 +00:00
|
|
|
#else /* !HAVE_BACKTRACE */
|
|
|
|
DBG1(DBG_DMN, "thread %u received %s",
|
|
|
|
pthread_self(), signal == SIGSEGV ? "SIGSEGV" : "SIGILL");
|
|
|
|
#endif /* HAVE_BACKTRACE */
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "killing ourself hard after SIGSEGV");
|
2006-06-20 10:05:56 +00:00
|
|
|
raise(SIGKILL);
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
2005-11-29 10:25:07 +00:00
|
|
|
|
2005-11-16 12:06:34 +00:00
|
|
|
/**
|
2006-09-27 14:14:44 +00:00
|
|
|
* Create the daemon.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2006-05-09 07:34:25 +00:00
|
|
|
private_daemon_t *daemon_create(void)
|
2005-12-04 01:30:35 +00:00
|
|
|
{
|
2006-04-10 08:07:38 +00:00
|
|
|
private_daemon_t *this = malloc_thing(private_daemon_t);
|
|
|
|
struct sigaction action;
|
2005-11-29 10:25:07 +00:00
|
|
|
|
|
|
|
/* assign methods */
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.kill = (void (*) (daemon_t*,char*))kill_daemon;
|
2005-11-29 10:25:07 +00:00
|
|
|
|
|
|
|
/* NULL members for clean destruction */
|
|
|
|
this->public.socket = NULL;
|
|
|
|
this->public.ike_sa_manager = NULL;
|
|
|
|
this->public.job_queue = NULL;
|
|
|
|
this->public.event_queue = NULL;
|
2006-03-07 09:42:15 +00:00
|
|
|
this->public.configuration = NULL;
|
2006-03-16 15:25:06 +00:00
|
|
|
this->public.credentials = NULL;
|
|
|
|
this->public.connections = NULL;
|
|
|
|
this->public.policies = NULL;
|
2005-11-29 10:25:07 +00:00
|
|
|
this->public.sender= NULL;
|
|
|
|
this->public.receiver = NULL;
|
|
|
|
this->public.scheduler = NULL;
|
2006-02-09 16:25:02 +00:00
|
|
|
this->public.kernel_interface = NULL;
|
2005-11-29 10:25:07 +00:00
|
|
|
this->public.thread_pool = NULL;
|
2006-03-16 15:25:06 +00:00
|
|
|
this->public.stroke = NULL;
|
2006-09-27 14:14:44 +00:00
|
|
|
this->public.bus = NULL;
|
|
|
|
this->public.outlog = NULL;
|
|
|
|
this->public.syslog = NULL;
|
2006-10-18 11:46:13 +00:00
|
|
|
this->public.authlog = NULL;
|
2005-11-29 10:25:07 +00:00
|
|
|
|
2005-12-04 11:46:58 +00:00
|
|
|
this->main_thread_id = pthread_self();
|
2005-11-29 11:27:25 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
/* setup signal handling for all threads */
|
2005-11-29 10:25:07 +00:00
|
|
|
sigemptyset(&(this->signal_set));
|
2006-04-10 08:07:38 +00:00
|
|
|
sigaddset(&(this->signal_set), SIGSEGV);
|
2005-11-29 10:25:07 +00:00
|
|
|
sigaddset(&(this->signal_set), SIGINT);
|
|
|
|
sigaddset(&(this->signal_set), SIGHUP);
|
|
|
|
sigaddset(&(this->signal_set), SIGTERM);
|
|
|
|
pthread_sigmask(SIG_BLOCK, &(this->signal_set), 0);
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
/* setup SIGSEGV handler for all threads */
|
|
|
|
action.sa_handler = signal_handler;
|
|
|
|
action.sa_mask = this->signal_set;
|
|
|
|
action.sa_flags = 0;
|
2006-10-18 11:46:13 +00:00
|
|
|
sigaction(SIGSEGV, &action, NULL);
|
|
|
|
sigaction(SIGILL, &action, NULL);
|
2005-11-29 10:25:07 +00:00
|
|
|
return this;
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
|
|
|
|
2006-09-27 14:14:44 +00:00
|
|
|
/**
|
|
|
|
* print command line usage and exit
|
|
|
|
*/
|
2006-06-14 12:42:36 +00:00
|
|
|
static void usage(const char *msg)
|
|
|
|
{
|
|
|
|
if (msg != NULL && *msg != '\0')
|
2006-10-18 11:46:13 +00:00
|
|
|
{
|
2006-06-14 12:42:36 +00:00
|
|
|
fprintf(stderr, "%s\n", msg);
|
2006-10-18 11:46:13 +00:00
|
|
|
}
|
|
|
|
fprintf(stderr, "Usage: charon\n"
|
|
|
|
" [--help]\n"
|
|
|
|
" [--version]\n"
|
|
|
|
" [--strictcrlpolicy]\n"
|
2007-04-05 17:07:14 +00:00
|
|
|
" [--cachecrls]\n"
|
2007-04-04 07:49:05 +00:00
|
|
|
" [--crlcheckinterval <interval>]\n"
|
|
|
|
" [--eapdir <dir>]\n"
|
2006-10-18 11:46:13 +00:00
|
|
|
" [--use-syslog]\n"
|
|
|
|
" [--debug-<type> <level>]\n"
|
|
|
|
" <type>: log context type (dmn|mgr|ike|chd|job|cfg|knl|net|enc|lib)\n"
|
|
|
|
" <level>: log verbosity (-1 = silent, 0 = audit, 1 = control,\n"
|
|
|
|
" 2 = controlmore, 3 = raw, 4 = private)\n"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
exit(msg == NULL? 0 : 1);
|
2006-06-14 12:42:36 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 12:06:34 +00:00
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Main function, manages the daemon.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
int main(int argc, char *argv[])
|
2006-10-18 11:46:13 +00:00
|
|
|
{
|
2007-04-04 07:49:05 +00:00
|
|
|
u_int crl_check_interval = 0;
|
2006-06-14 12:42:36 +00:00
|
|
|
bool strict_crl_policy = FALSE;
|
2007-04-05 17:07:14 +00:00
|
|
|
bool cache_crls = FALSE;
|
2006-10-18 11:46:13 +00:00
|
|
|
bool use_syslog = FALSE;
|
2007-02-12 15:56:47 +00:00
|
|
|
char *eapdir = IPSEC_EAPDIR;
|
2006-06-14 12:42:36 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
private_daemon_t *private_charon;
|
2006-03-14 13:22:48 +00:00
|
|
|
FILE *pid_file;
|
|
|
|
struct stat stb;
|
2006-08-28 08:45:22 +00:00
|
|
|
linked_list_t *list;
|
|
|
|
host_t *host;
|
2006-10-26 09:46:56 +00:00
|
|
|
level_t levels[DBG_MAX];
|
2006-10-18 11:46:13 +00:00
|
|
|
int signal;
|
|
|
|
|
|
|
|
/* use CTRL loglevel for default */
|
2006-10-26 09:46:56 +00:00
|
|
|
for (signal = 0; signal < DBG_MAX; signal++)
|
2006-10-18 11:46:13 +00:00
|
|
|
{
|
|
|
|
levels[signal] = LEVEL_CTRL;
|
|
|
|
}
|
2006-04-20 07:06:39 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
/* handle arguments */
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
struct option long_opts[] = {
|
2006-06-14 12:42:36 +00:00
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'v' },
|
|
|
|
{ "use-syslog", no_argument, NULL, 'l' },
|
|
|
|
{ "strictcrlpolicy", no_argument, NULL, 'r' },
|
2007-04-05 17:07:14 +00:00
|
|
|
{ "cachecrls", no_argument, NULL, 'C' },
|
2007-04-04 07:49:05 +00:00
|
|
|
{ "crlcheckinterval", required_argument, NULL, 'x' },
|
2007-02-12 15:56:47 +00:00
|
|
|
{ "eapdir", required_argument, NULL, 'e' },
|
2006-10-26 09:46:56 +00:00
|
|
|
/* TODO: handle "debug-all" */
|
|
|
|
{ "debug-dmn", required_argument, &signal, DBG_DMN },
|
|
|
|
{ "debug-mgr", required_argument, &signal, DBG_MGR },
|
|
|
|
{ "debug-ike", required_argument, &signal, DBG_IKE },
|
|
|
|
{ "debug-chd", required_argument, &signal, DBG_CHD },
|
|
|
|
{ "debug-job", required_argument, &signal, DBG_JOB },
|
|
|
|
{ "debug-cfg", required_argument, &signal, DBG_CFG },
|
|
|
|
{ "debug-knl", required_argument, &signal, DBG_KNL },
|
|
|
|
{ "debug-net", required_argument, &signal, DBG_NET },
|
|
|
|
{ "debug-enc", required_argument, &signal, DBG_ENC },
|
|
|
|
{ "debug-lib", required_argument, &signal, DBG_LIB },
|
2006-06-14 12:42:36 +00:00
|
|
|
{ 0,0,0,0 }
|
|
|
|
};
|
2006-10-18 11:46:13 +00:00
|
|
|
|
2006-06-14 12:42:36 +00:00
|
|
|
int c = getopt_long(argc, argv, "", long_opts, NULL);
|
|
|
|
switch (c)
|
2006-04-20 07:06:39 +00:00
|
|
|
{
|
2006-10-18 11:46:13 +00:00
|
|
|
case EOF:
|
2006-06-14 12:42:36 +00:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(NULL);
|
2006-10-18 11:46:13 +00:00
|
|
|
break;
|
2006-06-14 12:42:36 +00:00
|
|
|
case 'v':
|
|
|
|
printf("Linux strongSwan %s\n", VERSION);
|
|
|
|
exit(0);
|
|
|
|
case 'l':
|
2006-10-18 11:46:13 +00:00
|
|
|
use_syslog = TRUE;
|
2006-06-14 12:42:36 +00:00
|
|
|
continue;
|
|
|
|
case 'r':
|
|
|
|
strict_crl_policy = TRUE;
|
|
|
|
continue;
|
2007-04-05 17:07:14 +00:00
|
|
|
case 'C':
|
|
|
|
cache_crls = TRUE;
|
|
|
|
continue;
|
2007-04-04 07:49:05 +00:00
|
|
|
case 'x':
|
|
|
|
crl_check_interval = atoi(optarg);
|
|
|
|
continue;
|
2007-02-12 15:56:47 +00:00
|
|
|
case 'e':
|
|
|
|
eapdir = optarg;
|
|
|
|
continue;
|
2006-10-18 11:46:13 +00:00
|
|
|
case 0:
|
|
|
|
/* option is in signal */
|
|
|
|
levels[signal] = atoi(optarg);
|
|
|
|
continue;
|
2006-06-14 12:42:36 +00:00
|
|
|
default:
|
|
|
|
usage("");
|
2006-10-18 11:46:13 +00:00
|
|
|
break;
|
2006-04-20 07:06:39 +00:00
|
|
|
}
|
2006-06-14 12:42:36 +00:00
|
|
|
break;
|
2006-04-20 07:06:39 +00:00
|
|
|
}
|
2006-06-14 12:42:36 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
private_charon = daemon_create();
|
|
|
|
charon = (daemon_t*)private_charon;
|
2006-04-10 08:07:38 +00:00
|
|
|
|
2006-04-26 07:50:06 +00:00
|
|
|
/* initialize daemon */
|
2006-10-18 11:46:13 +00:00
|
|
|
initialize(private_charon, strict_crl_policy, use_syslog, levels);
|
2007-04-04 07:49:05 +00:00
|
|
|
|
2007-02-12 15:56:47 +00:00
|
|
|
/* load pluggable EAP modules */
|
|
|
|
eap_method_load(eapdir);
|
2006-04-26 07:50:06 +00:00
|
|
|
|
2007-04-05 17:07:14 +00:00
|
|
|
/* set cache_crls and crl_check_interval options */
|
|
|
|
ca_info_set_options(cache_crls, crl_check_interval);
|
2007-04-04 07:49:05 +00:00
|
|
|
|
2006-03-14 13:22:48 +00:00
|
|
|
/* check/setup PID file */
|
|
|
|
if (stat(PID_FILE, &stb) == 0)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_DMN, "charon already running (\""PID_FILE"\" exists)");
|
2006-09-27 14:14:44 +00:00
|
|
|
destroy(private_charon);
|
2006-03-14 13:22:48 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
pid_file = fopen(PID_FILE, "w");
|
|
|
|
if (pid_file)
|
|
|
|
{
|
|
|
|
fprintf(pid_file, "%d\n", getpid());
|
|
|
|
fclose(pid_file);
|
|
|
|
}
|
2007-02-12 15:56:47 +00:00
|
|
|
|
2006-08-28 08:45:22 +00:00
|
|
|
/* log socket info */
|
2007-03-03 14:56:24 +00:00
|
|
|
list = charon->kernel_interface->create_address_list(charon->kernel_interface);
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_NET, "listening on %d addresses:", list->get_count(list));
|
2006-08-28 08:45:22 +00:00
|
|
|
while (list->remove_first(list, (void**)&host) == SUCCESS)
|
|
|
|
{
|
2006-10-26 09:46:56 +00:00
|
|
|
DBG1(DBG_NET, " %H", host);
|
2006-08-28 08:45:22 +00:00
|
|
|
host->destroy(host);
|
|
|
|
}
|
|
|
|
list->destroy(list);
|
|
|
|
|
2006-04-26 07:50:06 +00:00
|
|
|
/* run daemon */
|
2006-09-27 14:14:44 +00:00
|
|
|
run(private_charon);
|
2005-11-29 10:25:07 +00:00
|
|
|
|
2007-02-12 15:56:47 +00:00
|
|
|
eap_method_unload();
|
2007-03-08 17:00:32 +00:00
|
|
|
fetcher_finalize();
|
2006-03-14 13:22:48 +00:00
|
|
|
/* normal termination, cleanup and exit */
|
2006-09-27 14:14:44 +00:00
|
|
|
destroy(private_charon);
|
2006-03-14 13:22:48 +00:00
|
|
|
unlink(PID_FILE);
|
2006-10-18 11:46:13 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
return 0;
|
2005-11-03 08:22:10 +00:00
|
|
|
}
|