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.
|
|
|
|
*/
|
|
|
|
|
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-04-10 08:07:38 +00:00
|
|
|
#include <execinfo.h>
|
2006-04-20 07:06:39 +00:00
|
|
|
#include <string.h>
|
2006-06-14 12:42:36 +00:00
|
|
|
#include <getopt.h>
|
2005-11-16 12:06:34 +00:00
|
|
|
|
|
|
|
#include "daemon.h"
|
|
|
|
|
2005-11-23 09:43:14 +00:00
|
|
|
#include <types.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>
|
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
|
|
|
* A logger_t object assigned for daemon things.
|
2005-11-29 10:25:07 +00:00
|
|
|
*/
|
|
|
|
logger_t *logger;
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
/**
|
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 11:27:25 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Main loop function.
|
|
|
|
*
|
|
|
|
* @param this calling object
|
2005-11-29 11:27:25 +00:00
|
|
|
*/
|
2005-11-29 10:25:07 +00:00
|
|
|
void (*run) (private_daemon_t *this);
|
2005-11-29 11:27:25 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Initialize the daemon.
|
|
|
|
*
|
2006-06-14 12:42:36 +00:00
|
|
|
* @param this calling object
|
|
|
|
* @param strict enforce a strict crl policy
|
2005-11-29 11:27:25 +00:00
|
|
|
*/
|
2006-06-14 12:42:36 +00:00
|
|
|
void (*initialize) (private_daemon_t *this, bool strict);
|
2005-11-29 11:27:25 +00:00
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Destroy the daemon.
|
|
|
|
*
|
|
|
|
* @param this calling object
|
2005-11-29 11:27:25 +00:00
|
|
|
*/
|
|
|
|
void (*destroy) (private_daemon_t *this);
|
2005-11-29 10:25:07 +00:00
|
|
|
};
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2005-11-29 10:25:07 +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;
|
|
|
|
|
2005-11-16 12:06:34 +00:00
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Implementation of private_daemon_t.run.
|
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)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, "Error %d when waiting for signal", error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
switch (signal_number)
|
|
|
|
{
|
2005-11-16 12:06:34 +00:00
|
|
|
case SIGHUP:
|
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "Signal of type SIGHUP received. Do nothing");
|
2005-11-16 12:06:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SIGINT:
|
|
|
|
{
|
2005-12-06 12:54:34 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "Signal of type SIGINT received. Exit main loop");
|
2005-11-16 12:06:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
case SIGTERM:
|
2005-12-06 12:54:34 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "Signal of type SIGTERM received. Exit main loop");
|
2005-11-16 12:06:34 +00:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
{
|
2005-11-29 10:25:07 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "Unknown signal %d received. Do nothing", signal_number);
|
2005-11-16 12:06:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Implementation of daemon_t.kill.
|
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 */
|
2005-12-06 12:54:34 +00:00
|
|
|
this->logger->log(this->logger, CONTROL, "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 */
|
|
|
|
this->destroy(this);
|
2006-03-14 13:22:48 +00:00
|
|
|
unlink(PID_FILE);
|
2005-11-29 11:27:25 +00:00
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, CONTROL, "sending SIGTERM to ourself", reason);
|
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
|
|
|
|
|
|
|
/**
|
2005-12-06 12:54:34 +00:00
|
|
|
* Implementation of private_daemon_t.initialize.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2006-06-14 12:42:36 +00:00
|
|
|
static void initialize(private_daemon_t *this, bool strict)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2006-06-20 06:05:01 +00:00
|
|
|
credential_store_t* credentials;
|
2006-04-25 10:06:30 +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);
|
|
|
|
this->public.interfaces = interfaces_create(IKEV2_UDP_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();
|
|
|
|
this->public.send_queue = send_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
|
|
|
|
|
|
|
/* load keys, ca certificates and crls */
|
|
|
|
credentials = this->public.credentials;
|
2006-06-20 08:43:57 +00:00
|
|
|
credentials->load_ca_certificates(credentials);
|
|
|
|
credentials->load_crls(credentials);
|
|
|
|
credentials->load_private_keys(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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-11-29 10:25:07 +00:00
|
|
|
* Destory all initiated objects
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2005-11-29 11:27:25 +00:00
|
|
|
static void destroy(private_daemon_t *this)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2006-05-30 06:14:23 +00:00
|
|
|
/* 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.
|
|
|
|
*/
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.receiver != NULL)
|
2006-05-30 06:14:23 +00:00
|
|
|
{ /* we don't want to receive anything... */
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.receiver->destroy(this->public.receiver);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2006-05-30 06:14:23 +00:00
|
|
|
if (this->public.stroke != NULL)
|
|
|
|
{ /* ignore all incoming user requests */
|
|
|
|
this->public.stroke->destroy(this->public.stroke);
|
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.scheduler != NULL)
|
2006-05-30 06:14:23 +00:00
|
|
|
{ /* stop scheduing jobs */
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.scheduler->destroy(this->public.scheduler);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.thread_pool != NULL)
|
2006-05-30 06:14:23 +00:00
|
|
|
{ /* stop processing jobs */
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.thread_pool->destroy(this->public.thread_pool);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2006-05-30 06:14:23 +00:00
|
|
|
if (this->public.ike_sa_manager != NULL)
|
|
|
|
{ /* shut down manager with all IKE SAs */
|
|
|
|
this->public.ike_sa_manager->destroy(this->public.ike_sa_manager);
|
|
|
|
}
|
|
|
|
if (this->public.kernel_interface != NULL)
|
|
|
|
{ /* all child SAs should be down now, so kill kernel interface */
|
|
|
|
this->public.kernel_interface->destroy(this->public.kernel_interface);
|
|
|
|
}
|
|
|
|
/* destroy other infrastructure */
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.job_queue != NULL)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.job_queue->destroy(this->public.job_queue);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2005-11-29 11:27:25 +00:00
|
|
|
if (this->public.event_queue != NULL)
|
2005-11-17 11:24:03 +00:00
|
|
|
{
|
2005-11-29 11:27:25 +00:00
|
|
|
this->public.event_queue->destroy(this->public.event_queue);
|
2005-11-17 11:24:03 +00:00
|
|
|
}
|
2006-06-22 06:36:28 +00:00
|
|
|
if (this->public.interfaces != NULL)
|
|
|
|
{
|
|
|
|
this->public.interfaces->destroy(this->public.interfaces);
|
|
|
|
}
|
2006-03-07 09:42:15 +00:00
|
|
|
if (this->public.configuration != NULL)
|
2005-11-16 12:06:34 +00:00
|
|
|
{
|
2006-03-07 09:42:15 +00:00
|
|
|
this->public.configuration->destroy(this->public.configuration);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
2006-03-16 15:25:06 +00:00
|
|
|
if (this->public.credentials != NULL)
|
|
|
|
{
|
|
|
|
this->public.credentials->destroy(this->public.credentials);
|
|
|
|
}
|
|
|
|
if (this->public.connections != NULL)
|
|
|
|
{
|
|
|
|
this->public.connections->destroy(this->public.connections);
|
|
|
|
}
|
|
|
|
if (this->public.policies != NULL)
|
|
|
|
{
|
|
|
|
this->public.policies->destroy(this->public.policies);
|
|
|
|
}
|
2006-05-30 06:14:23 +00:00
|
|
|
/* we hope the sender could send the outstanding deletes, but
|
|
|
|
* we shut down here at any cost */
|
|
|
|
if (this->public.sender != NULL)
|
2006-03-16 15:25:06 +00:00
|
|
|
{
|
2006-05-30 06:14:23 +00:00
|
|
|
this->public.sender->destroy(this->public.sender);
|
|
|
|
}
|
|
|
|
if (this->public.send_queue != NULL)
|
|
|
|
{
|
|
|
|
this->public.send_queue->destroy(this->public.send_queue);
|
|
|
|
}
|
|
|
|
if (this->public.socket != NULL)
|
|
|
|
{
|
|
|
|
this->public.socket->destroy(this->public.socket);
|
2006-03-16 15:25:06 +00:00
|
|
|
}
|
2006-04-10 08:07:38 +00:00
|
|
|
free(this);
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
void signal_handler(int signal)
|
|
|
|
{
|
|
|
|
void *array[20];
|
|
|
|
size_t size;
|
|
|
|
char **strings;
|
|
|
|
size_t i;
|
|
|
|
logger_t *logger;
|
|
|
|
|
|
|
|
size = backtrace(array, 20);
|
|
|
|
strings = backtrace_symbols(array, size);
|
|
|
|
logger = logger_manager->get_logger(logger_manager, DAEMON);
|
|
|
|
|
|
|
|
logger->log(logger, ERROR, "Thread %u received SIGSEGV. Dumping %d frames from stack:", pthread_self(), size);
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
for (i = 0; i < size; i++)
|
|
|
|
{
|
2006-04-13 13:24:45 +00:00
|
|
|
logger->log(logger, ERROR, " %s", strings[i]);
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
free (strings);
|
2006-04-19 11:40:48 +00:00
|
|
|
logger->log(logger, ERROR, "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
|
|
|
/**
|
2005-11-29 10:25:07 +00:00
|
|
|
* @brief Create the daemon.
|
2005-11-16 12:06:34 +00:00
|
|
|
*
|
2005-11-29 10:25:07 +00:00
|
|
|
* @return created daemon_t
|
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 */
|
|
|
|
this->run = run;
|
2005-11-29 11:27:25 +00:00
|
|
|
this->destroy = destroy;
|
|
|
|
this->initialize = initialize;
|
|
|
|
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;
|
2006-06-22 06:36:28 +00:00
|
|
|
this->public.interfaces = NULL;
|
2005-11-29 10:25:07 +00:00
|
|
|
this->public.ike_sa_manager = NULL;
|
|
|
|
this->public.job_queue = NULL;
|
|
|
|
this->public.event_queue = NULL;
|
|
|
|
this->public.send_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;
|
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;
|
|
|
|
if (sigaction(SIGSEGV, &action, NULL) == -1)
|
|
|
|
{
|
|
|
|
this->logger->log(this->logger, ERROR, "signal handler setup for SIGSEGV failed");
|
|
|
|
}
|
2005-11-29 10:25:07 +00:00
|
|
|
return this;
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
|
|
|
|
2006-06-14 12:42:36 +00:00
|
|
|
static void usage(const char *msg)
|
|
|
|
{
|
|
|
|
if (msg != NULL && *msg != '\0')
|
|
|
|
fprintf(stderr, "%s\n", msg);
|
|
|
|
fprintf(stderr, "Usage: charon"
|
|
|
|
" [--help]"
|
|
|
|
" [--version]"
|
|
|
|
" [--use-syslog]"
|
|
|
|
" [--strictcrlpolicy]"
|
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
exit(msg == NULL? 0 : 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-04-10 08:07:38 +00:00
|
|
|
{
|
2006-06-14 12:42:36 +00:00
|
|
|
bool strict_crl_policy = FALSE;
|
|
|
|
|
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-04-20 07:06:39 +00:00
|
|
|
|
2006-06-14 12:42:36 +00:00
|
|
|
/* handle arguments */
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
static const struct option long_opts[] = {
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "version", no_argument, NULL, 'v' },
|
|
|
|
{ "use-syslog", no_argument, NULL, 'l' },
|
|
|
|
{ "strictcrlpolicy", no_argument, NULL, 'r' },
|
|
|
|
{ 0,0,0,0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
int c = getopt_long(argc, argv, "", long_opts, NULL);
|
|
|
|
|
|
|
|
/* Note: "breaking" from case terminates loop */
|
|
|
|
switch (c)
|
2006-04-20 07:06:39 +00:00
|
|
|
{
|
2006-06-14 12:42:36 +00:00
|
|
|
case EOF: /* end of flags */
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
usage(NULL);
|
|
|
|
break; /* not actually reached */
|
|
|
|
case 'v':
|
|
|
|
printf("Linux strongSwan %s\n", VERSION);
|
|
|
|
exit(0);
|
|
|
|
case 'l':
|
|
|
|
logger_manager->set_output(logger_manager, ALL_LOGGERS, NULL);
|
|
|
|
continue;
|
|
|
|
case 'r':
|
|
|
|
strict_crl_policy = TRUE;
|
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
usage("");
|
|
|
|
break; /* not actually reached */
|
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
|
|
|
|
|
|
|
private_charon->logger = logger_manager->get_logger(logger_manager, DAEMON);
|
2006-05-31 05:48:32 +00:00
|
|
|
|
|
|
|
private_charon->logger->log(private_charon->logger, CONTROL,
|
|
|
|
"Starting Charon (strongSwan Version %s)", VERSION);
|
2006-03-14 13:22:48 +00:00
|
|
|
|
2006-04-26 07:50:06 +00:00
|
|
|
/* initialize daemon */
|
2006-06-14 12:42:36 +00:00
|
|
|
private_charon->initialize(private_charon, strict_crl_policy);
|
2006-04-26 07:50:06 +00:00
|
|
|
|
2006-03-14 13:22:48 +00:00
|
|
|
/* check/setup PID file */
|
|
|
|
if (stat(PID_FILE, &stb) == 0)
|
|
|
|
{
|
|
|
|
private_charon->logger->log(private_charon->logger, ERROR,
|
|
|
|
"charon already running (\""PID_FILE"\" exists)");
|
|
|
|
private_charon->destroy(private_charon);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
pid_file = fopen(PID_FILE, "w");
|
|
|
|
if (pid_file)
|
|
|
|
{
|
|
|
|
fprintf(pid_file, "%d\n", getpid());
|
|
|
|
fclose(pid_file);
|
|
|
|
}
|
2005-11-29 10:25:07 +00:00
|
|
|
|
2006-04-26 07:50:06 +00:00
|
|
|
/* run daemon */
|
2005-11-29 10:25:07 +00:00
|
|
|
private_charon->run(private_charon);
|
|
|
|
|
2006-03-14 13:22:48 +00:00
|
|
|
/* normal termination, cleanup and exit */
|
2005-11-29 11:27:25 +00:00
|
|
|
private_charon->destroy(private_charon);
|
2006-03-14 13:22:48 +00:00
|
|
|
unlink(PID_FILE);
|
2005-11-16 12:06:34 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
return 0;
|
2005-11-03 08:22:10 +00:00
|
|
|
}
|
2005-11-29 10:25:07 +00:00
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
|