2009-09-02 15:26:36 +00:00
|
|
|
/*
|
2012-04-19 11:32:51 +00:00
|
|
|
* Copyright (C) 2006-2012 Tobias Brunner
|
2009-05-06 17:48:21 +00:00
|
|
|
* Copyright (C) 2005-2009 Martin Willi
|
2007-10-03 15:10:41 +00:00
|
|
|
* Copyright (C) 2006 Daniel Roethlisberger
|
2006-07-07 08:49:06 +00:00
|
|
|
* 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>
|
2006-03-14 13:22:48 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2006-07-27 12:18:40 +00:00
|
|
|
#include <time.h>
|
2010-06-15 17:40:44 +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>
|
2012-06-27 09:27:36 +00:00
|
|
|
#include <plugins/plugin_feature.h>
|
2008-06-12 11:42:19 +00:00
|
|
|
#include <config/proposal.h>
|
2010-07-06 09:36:58 +00:00
|
|
|
#include <kernel/kernel_handler.h>
|
2011-02-09 21:27:04 +00:00
|
|
|
#include <processing/jobs/start_action_job.h>
|
2005-11-03 17:26:49 +00:00
|
|
|
|
2012-07-27 12:45:15 +00:00
|
|
|
#ifndef CAP_NET_ADMIN
|
|
|
|
#define CAP_NET_ADMIN 12
|
|
|
|
#endif
|
|
|
|
|
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;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-07-06 09:36:58 +00:00
|
|
|
/**
|
|
|
|
* Handler for kernel events
|
|
|
|
*/
|
|
|
|
kernel_handler_t *kernel_handler;
|
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
|
|
|
|
*/
|
2010-03-31 15:26:39 +00:00
|
|
|
extern void (*dbg) (debug_t group, level_t level, char *fmt, ...);
|
2006-10-18 11:46:13 +00:00
|
|
|
|
2010-03-12 15:56:54 +00:00
|
|
|
/**
|
|
|
|
* we store the previous debug function so we can reset it
|
|
|
|
*/
|
2010-03-31 15:26:39 +00:00
|
|
|
static void (*dbg_old) (debug_t group, level_t level, char *fmt, ...);
|
2010-03-12 15:56:54 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
/**
|
|
|
|
* Logging hook for library logs, spreads debug message over bus
|
|
|
|
*/
|
2010-03-31 15:26:39 +00:00
|
|
|
static void dbg_bus(debug_t group, level_t level, char *fmt, ...)
|
2006-10-18 11:46:13 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
va_start(args, fmt);
|
2010-03-31 15:26:39 +00:00
|
|
|
charon->bus->vlog(charon->bus, group, level, fmt, args);
|
2006-10-18 11:46:13 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2005-11-16 12:06:34 +00:00
|
|
|
/**
|
2006-09-27 14:14:44 +00:00
|
|
|
* Clean up all daemon resources
|
|
|
|
*/
|
|
|
|
static void destroy(private_daemon_t *this)
|
|
|
|
{
|
2007-06-11 10:57:19 +00:00
|
|
|
/* terminate all idle threads */
|
2010-07-15 12:49:41 +00:00
|
|
|
lib->processor->set_threads(lib->processor, 0);
|
2010-07-05 11:46:04 +00:00
|
|
|
|
2007-06-11 10:57:19 +00:00
|
|
|
/* close all IKE_SAs */
|
2008-04-17 10:46:25 +00:00
|
|
|
if (this->public.ike_sa_manager)
|
|
|
|
{
|
|
|
|
this->public.ike_sa_manager->flush(this->public.ike_sa_manager);
|
|
|
|
}
|
2011-06-29 10:08:24 +00:00
|
|
|
if (this->public.traps)
|
|
|
|
{
|
|
|
|
this->public.traps->flush(this->public.traps);
|
|
|
|
}
|
2012-05-02 07:03:23 +00:00
|
|
|
if (this->public.sender)
|
|
|
|
{
|
|
|
|
this->public.sender->flush(this->public.sender);
|
|
|
|
}
|
2012-06-19 11:29:09 +00:00
|
|
|
|
|
|
|
/* cancel all threads and wait for their termination */
|
|
|
|
lib->processor->cancel(lib->processor);
|
|
|
|
|
2011-12-14 13:24:15 +00:00
|
|
|
#ifdef ME
|
|
|
|
DESTROY_IF(this->public.connect_manager);
|
|
|
|
DESTROY_IF(this->public.mediation_manager);
|
|
|
|
#endif /* ME */
|
2011-12-15 11:20:09 +00:00
|
|
|
/* make sure the cache is clear before unloading plugins */
|
|
|
|
lib->credmgr->flush_cache(lib->credmgr, CERT_ANY);
|
2008-05-15 14:01:26 +00:00
|
|
|
lib->plugins->unload(lib->plugins);
|
2010-07-12 08:57:46 +00:00
|
|
|
DESTROY_IF(this->kernel_handler);
|
2009-05-06 17:48:21 +00:00
|
|
|
DESTROY_IF(this->public.traps);
|
2011-06-28 17:42:54 +00:00
|
|
|
DESTROY_IF(this->public.shunts);
|
2006-09-27 14:14:44 +00:00
|
|
|
DESTROY_IF(this->public.ike_sa_manager);
|
2008-03-13 14:14:44 +00:00
|
|
|
DESTROY_IF(this->public.controller);
|
|
|
|
DESTROY_IF(this->public.eap);
|
2011-12-01 13:08:24 +00:00
|
|
|
DESTROY_IF(this->public.xauth);
|
2007-04-27 14:25:08 +00:00
|
|
|
DESTROY_IF(this->public.backends);
|
2006-09-27 14:14:44 +00:00
|
|
|
DESTROY_IF(this->public.socket);
|
2012-07-03 11:07:24 +00:00
|
|
|
DESTROY_IF(this->public.caps);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2007-06-11 10:57:19 +00:00
|
|
|
/* rehook library logging, shutdown logging */
|
2010-03-12 15:56:54 +00:00
|
|
|
dbg = dbg_old;
|
2006-10-18 11:46:13 +00:00
|
|
|
DESTROY_IF(this->public.bus);
|
2008-11-11 10:52:37 +00:00
|
|
|
this->public.file_loggers->destroy_offset(this->public.file_loggers,
|
|
|
|
offsetof(file_logger_t, destroy));
|
|
|
|
this->public.sys_loggers->destroy_offset(this->public.sys_loggers,
|
|
|
|
offsetof(sys_logger_t, destroy));
|
2012-05-15 09:37:07 +00:00
|
|
|
free((void*)this->public.name);
|
2006-09-27 14:14:44 +00:00
|
|
|
free(this);
|
|
|
|
}
|
|
|
|
|
2010-03-12 15:56:54 +00:00
|
|
|
METHOD(daemon_t, start, void,
|
|
|
|
private_daemon_t *this)
|
|
|
|
{
|
|
|
|
/* start the engine, go multithreaded */
|
2010-07-15 12:49:41 +00:00
|
|
|
lib->processor->set_threads(lib->processor,
|
2012-04-24 12:10:06 +00:00
|
|
|
lib->settings->get_int(lib->settings, "%s.threads",
|
|
|
|
DEFAULT_THREADS, charon->name));
|
2010-03-12 15:56:54 +00:00
|
|
|
}
|
|
|
|
|
2012-07-12 14:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize/deinitialize sender and receiver
|
|
|
|
*/
|
|
|
|
static bool sender_receiver_cb(void *plugin, plugin_feature_t *feature,
|
|
|
|
bool reg, private_daemon_t *this)
|
|
|
|
{
|
|
|
|
if (reg)
|
|
|
|
{
|
|
|
|
this->public.receiver = receiver_create();
|
|
|
|
if (!this->public.receiver)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
this->public.sender = sender_create();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DESTROY_IF(this->public.receiver);
|
|
|
|
DESTROY_IF(this->public.sender);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-03-12 15:56:54 +00:00
|
|
|
METHOD(daemon_t, initialize, bool,
|
2012-04-19 11:32:51 +00:00
|
|
|
private_daemon_t *this, char *plugins)
|
2008-11-11 10:52:37 +00:00
|
|
|
{
|
2012-07-12 14:56:35 +00:00
|
|
|
plugin_feature_t features[] = {
|
2012-06-27 09:27:36 +00:00
|
|
|
PLUGIN_PROVIDE(CUSTOM, "libcharon"),
|
|
|
|
PLUGIN_DEPENDS(NONCE_GEN),
|
2012-07-12 14:56:35 +00:00
|
|
|
PLUGIN_DEPENDS(CUSTOM, "libcharon-receiver"),
|
2012-06-27 10:14:16 +00:00
|
|
|
PLUGIN_DEPENDS(CUSTOM, "kernel-ipsec"),
|
|
|
|
PLUGIN_DEPENDS(CUSTOM, "kernel-net"),
|
2012-07-12 14:56:35 +00:00
|
|
|
PLUGIN_CALLBACK((plugin_feature_callback_t)sender_receiver_cb, this),
|
|
|
|
PLUGIN_PROVIDE(CUSTOM, "libcharon-receiver"),
|
|
|
|
PLUGIN_DEPENDS(HASHER, HASH_SHA1),
|
|
|
|
PLUGIN_DEPENDS(RNG, RNG_STRONG),
|
|
|
|
PLUGIN_DEPENDS(CUSTOM, "socket"),
|
2012-06-27 09:27:36 +00:00
|
|
|
};
|
|
|
|
lib->plugins->add_static_features(lib->plugins, charon->name, features,
|
|
|
|
countof(features), TRUE);
|
|
|
|
|
2008-05-15 14:01:26 +00:00
|
|
|
/* load plugins, further infrastructure may need it */
|
2012-04-19 11:32:51 +00:00
|
|
|
if (!lib->plugins->load(lib->plugins, NULL, plugins))
|
2009-09-01 14:20:45 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2012-01-19 11:27:56 +00:00
|
|
|
DBG1(DBG_DMN, "loaded plugins: %s",
|
|
|
|
lib->plugins->loaded_plugins(lib->plugins));
|
2008-08-19 18:51:30 +00:00
|
|
|
|
2008-05-15 14:01:26 +00:00
|
|
|
this->public.ike_sa_manager = ike_sa_manager_create();
|
|
|
|
if (this->public.ike_sa_manager == NULL)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2011-02-09 21:27:04 +00:00
|
|
|
/* Queue start_action job */
|
|
|
|
lib->processor->queue_job(lib->processor, (job_t*)start_action_job_create());
|
|
|
|
|
2008-03-26 18:40:19 +00:00
|
|
|
#ifdef ME
|
2007-10-03 15:10:41 +00:00
|
|
|
this->public.connect_manager = connect_manager_create();
|
2008-03-13 14:14:44 +00:00
|
|
|
if (this->public.connect_manager == NULL)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2007-10-03 15:10:41 +00:00
|
|
|
this->public.mediation_manager = mediation_manager_create();
|
2008-03-26 18:40:19 +00:00
|
|
|
#endif /* ME */
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2007-08-29 00:37:10 +00:00
|
|
|
return TRUE;
|
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
|
|
|
*/
|
2012-04-24 09:07:56 +00:00
|
|
|
private_daemon_t *daemon_create(const char *name)
|
2009-09-02 15:26:36 +00:00
|
|
|
{
|
2010-03-12 15:56:54 +00:00
|
|
|
private_daemon_t *this;
|
|
|
|
|
|
|
|
INIT(this,
|
|
|
|
.public = {
|
|
|
|
.initialize = _initialize,
|
|
|
|
.start = _start,
|
2010-10-29 07:39:19 +00:00
|
|
|
.bus = bus_create(),
|
2010-03-12 15:56:54 +00:00
|
|
|
.file_loggers = linked_list_create(),
|
|
|
|
.sys_loggers = linked_list_create(),
|
2012-04-24 09:07:56 +00:00
|
|
|
.name = strdup(name ?: "libcharon"),
|
2010-03-12 15:56:54 +00:00
|
|
|
},
|
|
|
|
);
|
2010-11-23 09:50:36 +00:00
|
|
|
charon = &this->public;
|
2012-07-03 11:07:24 +00:00
|
|
|
this->public.caps = capabilities_create();
|
2010-11-23 09:50:36 +00:00
|
|
|
this->public.controller = controller_create();
|
|
|
|
this->public.eap = eap_manager_create();
|
2011-12-01 13:08:24 +00:00
|
|
|
this->public.xauth = xauth_manager_create();
|
2010-11-23 09:50:36 +00:00
|
|
|
this->public.backends = backend_manager_create();
|
|
|
|
this->public.socket = socket_manager_create();
|
|
|
|
this->public.traps = trap_manager_create();
|
2011-06-28 17:42:54 +00:00
|
|
|
this->public.shunts = shunt_manager_create();
|
2010-11-23 09:50:36 +00:00
|
|
|
this->kernel_handler = kernel_handler_create();
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2012-07-03 11:07:24 +00:00
|
|
|
this->public.caps->keep(this->public.caps, CAP_NET_ADMIN);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2005-11-29 10:25:07 +00:00
|
|
|
return this;
|
2005-11-16 12:06:34 +00:00
|
|
|
}
|
|
|
|
|
2009-06-09 12:56:31 +00:00
|
|
|
/**
|
2010-03-12 15:56:54 +00:00
|
|
|
* Described in header.
|
2009-06-09 12:56:31 +00:00
|
|
|
*/
|
2010-03-12 15:56:54 +00:00
|
|
|
void libcharon_deinit()
|
2009-06-09 12:56:31 +00:00
|
|
|
{
|
2010-03-12 15:56:54 +00:00
|
|
|
destroy((private_daemon_t*)charon);
|
|
|
|
charon = NULL;
|
2006-06-14 12:42:36 +00:00
|
|
|
}
|
|
|
|
|
2005-11-16 12:06:34 +00:00
|
|
|
/**
|
2010-03-12 15:56:54 +00:00
|
|
|
* Described in header.
|
2005-11-16 12:06:34 +00:00
|
|
|
*/
|
2012-04-24 09:07:56 +00:00
|
|
|
bool libcharon_init(const char *name)
|
2006-10-18 11:46:13 +00:00
|
|
|
{
|
2012-04-24 09:07:56 +00:00
|
|
|
daemon_create(name);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-10-29 07:39:19 +00:00
|
|
|
/* for uncritical pseudo random numbers */
|
|
|
|
srandom(time(NULL) + getpid());
|
|
|
|
|
|
|
|
/* set up hook to log dbg message in library via charons message bus */
|
|
|
|
dbg_old = dbg;
|
|
|
|
dbg = dbg_bus;
|
|
|
|
|
2008-06-12 11:42:19 +00:00
|
|
|
lib->printf_hook->add_handler(lib->printf_hook, 'P',
|
2009-03-12 18:07:32 +00:00
|
|
|
proposal_printf_hook,
|
|
|
|
PRINTF_HOOK_ARGTYPE_POINTER,
|
|
|
|
PRINTF_HOOK_ARGTYPE_END);
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-03-12 15:56:54 +00:00
|
|
|
if (lib->integrity &&
|
|
|
|
!lib->integrity->check(lib->integrity, "libcharon", libcharon_init))
|
2010-02-23 16:59:52 +00:00
|
|
|
{
|
2010-03-31 15:26:39 +00:00
|
|
|
dbg(DBG_DMN, 1, "integrity check of libcharon failed");
|
2010-03-12 15:56:54 +00:00
|
|
|
return FALSE;
|
2010-02-23 16:59:52 +00:00
|
|
|
}
|
2009-09-04 11:46:09 +00:00
|
|
|
|
2010-03-12 15:56:54 +00:00
|
|
|
return TRUE;
|
2005-11-03 08:22:10 +00:00
|
|
|
}
|