attribute_manager supports attribute_handler's to handle configuration attributes via plugins

moved resolv.conf editing to a separate plugin (resolv_conf)
extended attribute_provider interface to hand out arbitrary attributes
  moved strongswan.conf based dns/nbns configuration to a plugin (attr)
This commit is contained in:
Martin Willi
2009-04-24 14:13:52 +00:00
parent da17b0169a
commit 7f56b49461
20 changed files with 1099 additions and 381 deletions
+9
View File
@@ -0,0 +1,9 @@
INCLUDES = -I$(top_srcdir)/src/libstrongswan -I$(top_srcdir)/src/charon
AM_CFLAGS = -rdynamic
plugin_LTLIBRARIES = libstrongswan-attr.la
libstrongswan_attr_la_SOURCES = attr_plugin.h attr_plugin.c \
attr_provider.h attr_provider.c
libstrongswan_attr_la_LDFLAGS = -module
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2009 Martin Willi
* 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.
*
* $Id$
*/
#include "attr_plugin.h"
#include "attr_provider.h"
#include <daemon.h>
typedef struct private_attr_plugin_t private_attr_plugin_t;
/**
* private data of attr plugin
*/
struct private_attr_plugin_t {
/**
* implements plugin interface
*/
attr_plugin_t public;
/**
* CFG attributes provider
*/
attr_provider_t *provider;
};
/**
* Implementation of plugin_t.destroy
*/
static void destroy(private_attr_plugin_t *this)
{
charon->attributes->remove_provider(charon->attributes, &this->provider->provider);
this->provider->destroy(this->provider);
free(this);
}
/*
* see header file
*/
plugin_t *plugin_create()
{
private_attr_plugin_t *this = malloc_thing(private_attr_plugin_t);
this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
this->provider = attr_provider_create();
charon->attributes->add_provider(charon->attributes, &this->provider->provider);
return &this->public.plugin;
}
+49
View File
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2009 Martin Willi
* 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.
*
* $Id$
*/
/**
* @defgroup attr attr
* @ingroup cplugins
*
* @defgroup attr_plugin attr_plugin
* @{ @ingroup attr
*/
#ifndef ATTR_PLUGIN_H_
#define ATTR_PLUGIN_H_
#include <plugins/plugin.h>
typedef struct attr_plugin_t attr_plugin_t;
/**
* Plugin providing configuration attribute through strongswan.conf.
*/
struct attr_plugin_t {
/**
* implements plugin interface
*/
plugin_t plugin;
};
/**
* Create a attr_plugin instance.
*/
plugin_t *plugin_create();
#endif /** ATTR_PLUGIN_H_ @}*/
+156
View File
@@ -0,0 +1,156 @@
/*
* Copyright (C) 2009 Martin Willi
* 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.
*
* $Id$
*/
#include "attr_provider.h"
#include <time.h>
#include <daemon.h>
#define SERVER_MAX 2
typedef struct private_attr_provider_t private_attr_provider_t;
typedef struct attribute_entry_t attribute_entry_t;
/**
* private data of attr_provider
*/
struct private_attr_provider_t {
/**
* public functions
*/
attr_provider_t public;
/**
* List of attributes, attribute_entry_t
*/
linked_list_t *attributes;
};
struct attribute_entry_t {
/** type of attribute */
configuration_attribute_type_t type;
/** attribute value */
chunk_t value;
};
/**
* convert enumerator value from attribute_entry
*/
static bool attr_enum_filter(void *null, attribute_entry_t **in,
configuration_attribute_type_t *type, void* none, chunk_t *value)
{
*type = (*in)->type;
*value = (*in)->value;
return TRUE;
}
/**
* Implementation of attribute_provider_t.create_attribute_enumerator
*/
static enumerator_t* create_attribute_enumerator(
private_attr_provider_t *this, identification_t *id)
{
return enumerator_create_filter(
this->attributes->create_enumerator(this->attributes),
(void*)attr_enum_filter, NULL, NULL);
}
/**
* Implementation of attr_provider_t.destroy
*/
static void destroy(private_attr_provider_t *this)
{
attribute_entry_t *entry;
while (this->attributes->remove_last(this->attributes,
(void**)&entry) == SUCCESS)
{
free(entry->value.ptr);
free(entry);
}
this->attributes->destroy(this->attributes);
free(this);
}
/**
* Add an attribute entry to the list
*/
static void add_entry(private_attr_provider_t *this, char *key, int nr,
configuration_attribute_type_t type)
{
attribute_entry_t *entry;
host_t *host;
char *str;
str = lib->settings->get_str(lib->settings, "charon.%s%d", NULL, key, nr);
if (str)
{
host = host_create_from_string(str, 0);
if (host)
{
entry = malloc_thing(attribute_entry_t);
if (host->get_family(host) == AF_INET6)
{
switch (type)
{
case INTERNAL_IP4_DNS:
type = INTERNAL_IP6_DNS;
break;
case INTERNAL_IP4_NBNS:
type = INTERNAL_IP6_NBNS;
break;
default:
break;
}
}
entry->type = type;
entry->value = chunk_clone(host->get_address(host));
host->destroy(host);
this->attributes->insert_last(this->attributes, entry);
}
}
}
/*
* see header file
*/
attr_provider_t *attr_provider_create(database_t *db)
{
private_attr_provider_t *this;
int i;
this = malloc_thing(private_attr_provider_t);
this->public.provider.acquire_address = (host_t*(*)(attribute_provider_t *this, char*, identification_t *, host_t *))return_null;
this->public.provider.release_address = (bool(*)(attribute_provider_t *this, char*,host_t *, identification_t*))return_false;
this->public.provider.create_attribute_enumerator = (enumerator_t*(*)(attribute_provider_t*, identification_t *id))create_attribute_enumerator;
this->public.destroy = (void(*)(attr_provider_t*))destroy;
this->attributes = linked_list_create();
for (i = 1; i <= SERVER_MAX; i++)
{
add_entry(this, "dns", i, INTERNAL_IP4_DNS);
add_entry(this, "nbns", i, INTERNAL_IP4_NBNS);
}
return &this->public;
}
+51
View File
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2009 Martin Willi
* 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.
*
* $Id$
*/
/**
* @defgroup attr_provider attr_provider
* @{ @ingroup attr
*/
#ifndef ATTR_PROVIDER_H_
#define ATTR_PROVIDER_H_
#include <config/attributes/attribute_provider.h>
typedef struct attr_provider_t attr_provider_t;
/**
* Provide configuration attributes through static strongswan.conf definition.
*/
struct attr_provider_t {
/**
* Implements attribute provider interface
*/
attribute_provider_t provider;
/**
* Destroy a attr_provider instance.
*/
void (*destroy)(attr_provider_t *this);
};
/**
* Create a attr_provider instance.
*/
attr_provider_t *attr_provider_create();
#endif /** ATTR_PROVIDER @}*/
@@ -0,0 +1,13 @@
INCLUDES = -I$(top_srcdir)/src/libstrongswan -I$(top_srcdir)/src/charon
AM_CFLAGS = -rdynamic \
-DRESOLV_CONF=\"${resolv_conf}\"
plugin_LTLIBRARIES = libstrongswan-resolv-conf.la
libstrongswan_resolv_conf_la_SOURCES = \
resolv_conf_plugin.h resolv_conf_plugin.c \
resolv_conf_handler.h resolv_conf_handler.c
libstrongswan_resolv_conf_la_LDFLAGS = -module
@@ -0,0 +1,194 @@
/*
* Copyright (C) 2009 Martin Willi
* 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.
*
* $Id$
*/
#include "resolv_conf_handler.h"
#include <unistd.h>
#include <daemon.h>
#include <utils/mutex.h>
typedef struct private_resolv_conf_handler_t private_resolv_conf_handler_t;
/**
* Private data of an resolv_conf_handler_t object.
*/
struct private_resolv_conf_handler_t {
/**
* Public resolv_conf_handler_t interface.
*/
resolv_conf_handler_t public;
/**
* resolv.conf file to use
*/
char *file;
/**
* Mutex to access file exclusively
*/
mutex_t *mutex;
};
/**
* Implementation of attribute_handler_t.handle
*/
static bool handle(private_resolv_conf_handler_t *this, ike_sa_t *ike_sa,
configuration_attribute_type_t type, chunk_t data)
{
FILE *in, *out;
char buf[1024];
host_t *addr;
int family;
size_t len;
bool handled = FALSE;
switch (type)
{
case INTERNAL_IP4_DNS:
family = AF_INET;
break;
case INTERNAL_IP6_DNS:
family = AF_INET6;
break;
default:
return FALSE;
}
this->mutex->lock(this->mutex);
in = fopen(this->file, "r");
/* allows us to stream from in to out */
unlink(this->file);
out = fopen(this->file, "w");
if (out)
{
addr = host_create_from_chunk(family, data, 0);
fprintf(out, "nameserver %H # by strongSwan, from %D\n",
addr, ike_sa->get_other_id(ike_sa));
DBG1(DBG_IKE, "installing DNS server %H to %s", addr, this->file);
addr->destroy(addr);
handled = TRUE;
/* copy rest of the file */
if (in)
{
while ((len = fread(buf, 1, sizeof(buf), in)))
{
ignore_result(fwrite(buf, 1, len, out));
}
fclose(in);
}
fclose(out);
}
if (!handled)
{
DBG1(DBG_IKE, "adding DNS server failed", this->file);
}
this->mutex->unlock(this->mutex);
return handled;
}
/**
* Implementation of attribute_handler_t.release
*/
static void release(private_resolv_conf_handler_t *this, ike_sa_t *ike_sa,
configuration_attribute_type_t type, chunk_t data)
{
FILE *in, *out;
char line[1024], matcher[512], *pos;
host_t *addr;
int family;
switch (type)
{
case INTERNAL_IP4_DNS:
family = AF_INET;
break;
case INTERNAL_IP6_DNS:
family = AF_INET6;
break;
default:
return;
}
this->mutex->lock(this->mutex);
in = fopen(this->file, "r");
if (in)
{
/* allows us to stream from in to out */
unlink(this->file);
out = fopen(this->file, "w");
if (out)
{
addr = host_create_from_chunk(family, data, 0);
snprintf(matcher, sizeof(matcher),
"nameserver %H # by strongSwan, from %D\n",
addr, ike_sa->get_other_id(ike_sa));
/* copy all, but matching line */
while ((pos = fgets(line, sizeof(line), in)))
{
if (strneq(line, matcher, strlen(matcher)))
{
DBG1(DBG_IKE, "removing DNS server %H from %s",
addr, this->file);
}
else
{
fputs(line, out);
}
}
addr->destroy(addr);
fclose(out);
}
fclose(in);
}
this->mutex->unlock(this->mutex);
}
/**
* Implementation of resolv_conf_handler_t.destroy.
*/
static void destroy(private_resolv_conf_handler_t *this)
{
this->mutex->destroy(this->mutex);
free(this);
}
/**
* See header
*/
resolv_conf_handler_t *resolv_conf_handler_create()
{
private_resolv_conf_handler_t *this = malloc_thing(private_resolv_conf_handler_t);
this->public.handler.handle = (bool(*)(attribute_handler_t*, ike_sa_t*, configuration_attribute_type_t, chunk_t))handle;
this->public.handler.release = (void(*)(attribute_handler_t*, ike_sa_t*, configuration_attribute_type_t, chunk_t))release;
this->public.destroy = (void(*)(resolv_conf_handler_t*))destroy;
this->mutex = mutex_create(MUTEX_DEFAULT);
this->file = lib->settings->get_str(lib->settings,
"charon.plugins.resolv-conf.file", RESOLV_CONF);
return &this->public;
}
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2009 Martin Willi
* 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.
*
* $Id$
*/
/**
* @defgroup resolv_conf_handler resolv_conf_handler
* @{ @ingroup resolv_conf
*/
#ifndef RESOLV_CONF_HANDLER_H_
#define RESOLV_CONF_HANDLER_H_
#include <config/attributes/attribute_handler.h>
typedef struct resolv_conf_handler_t resolv_conf_handler_t;
/**
* Handle DNS configuration attributes by mangling a resolv.conf file.
*/
struct resolv_conf_handler_t {
/**
* Implements the attribute_handler_t interface
*/
attribute_handler_t handler;
/**
* Destroy a resolv_conf_handler_t.
*/
void (*destroy)(resolv_conf_handler_t *this);
};
/**
* Create a resolv_conf_handler instance.
*/
resolv_conf_handler_t *resolv_conf_handler_create();
#endif /* RESOLV_CONF_HANDLER_ @}*/
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2009 Martin Willi
* 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.
*
* $Id$
*/
#include "resolv_conf_plugin.h"
#include "resolv_conf_handler.h"
#include <daemon.h>
typedef struct private_resolv_conf_plugin_t private_resolv_conf_plugin_t;
/**
* private data of resolv_conf plugin
*/
struct private_resolv_conf_plugin_t {
/**
* implements plugin interface
*/
resolv_conf_plugin_t public;
/**
* The registerd DNS attribute handler
*/
resolv_conf_handler_t *handler;
};
/**
* Implementation of plugin_t.destroy
*/
static void destroy(private_resolv_conf_plugin_t *this)
{
charon->attributes->remove_handler(charon->attributes,
&this->handler->handler);
this->handler->destroy(this->handler);
free(this);
}
/*
* see header file
*/
plugin_t *plugin_create()
{
private_resolv_conf_plugin_t *this = malloc_thing(private_resolv_conf_plugin_t);
this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
this->handler = resolv_conf_handler_create();
charon->attributes->add_handler(charon->attributes, &this->handler->handler);
return &this->public.plugin;
}
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2009 Martin Willi
* 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.
*
* $Id$
*/
/**
* @defgroup resolv_conf resolv_conf
* @ingroup cplugins
*
* @defgroup resolv_conf_plugin resolv_conf_plugin
* @{ @ingroup resolv_conf
*/
#ifndef RESOLV_CONF_PLUGIN_H_
#define RESOLV_CONF_PLUGIN_H_
#include <plugins/plugin.h>
typedef struct resolv_conf_plugin_t resolv_conf_plugin_t;
/**
* Plugin that writes received DNS servers in a resolv.conf file.
*/
struct resolv_conf_plugin_t {
/**
* implements plugin interface
*/
plugin_t plugin;
};
/**
* Create a resolv_conf_plugin instance.
*/
plugin_t *plugin_create();
#endif /** RESOLV_CONF_PLUGIN_H_ @}*/
+1
View File
@@ -265,6 +265,7 @@ sql_attribute_t *sql_attribute_create(database_t *db)
this->public.provider.acquire_address = (host_t*(*)(attribute_provider_t *this, char*, identification_t *, host_t *))acquire_address;
this->public.provider.release_address = (bool(*)(attribute_provider_t *this, char*,host_t *, identification_t*))release_address;
this->public.provider.create_attribute_enumerator = (enumerator_t*(*)(attribute_provider_t*, identification_t *id))enumerator_create_empty;
this->public.destroy = (void(*)(sql_attribute_t*))destroy;
this->db = db;
@@ -532,6 +532,7 @@ stroke_attribute_t *stroke_attribute_create()
this->public.provider.acquire_address = (host_t*(*)(attribute_provider_t *this, char*, identification_t *,host_t *))acquire_address;
this->public.provider.release_address = (bool(*)(attribute_provider_t *this, char*,host_t *, identification_t*))release_address;
this->public.provider.create_attribute_enumerator = (enumerator_t*(*)(attribute_provider_t*, identification_t *id))enumerator_create_empty;
this->public.add_pool = (void(*)(stroke_attribute_t*, stroke_msg_t *msg))add_pool;
this->public.del_pool = (void(*)(stroke_attribute_t*, stroke_msg_t *msg))del_pool;
this->public.create_pool_enumerator = (enumerator_t*(*)(stroke_attribute_t*))create_pool_enumerator;