aboutsummaryrefslogtreecommitdiffstats
path: root/src/charon/plugins/sql
diff options
context:
space:
mode:
authorMartin Willi <martin@strongswan.org>2008-04-17 15:08:48 +0000
committerMartin Willi <martin@strongswan.org>2008-04-17 15:08:48 +0000
commitc4ec8c9d18ffd0cea68dd63267befc7d8713500e (patch)
treea52b2ffcbc2aa8cae5a1ac1daaab767ec504a50d /src/charon/plugins/sql
parentb360e3933d84c5869d839ccd274fa191dc2daef0 (diff)
downloadstrongswan-c4ec8c9d18ffd0cea68dd63267befc7d8713500e.tar.bz2
strongswan-c4ec8c9d18ffd0cea68dd63267befc7d8713500e.tar.xz
fixed compiler warning
Diffstat (limited to 'src/charon/plugins/sql')
-rw-r--r--src/charon/plugins/sql/Makefile.am3
-rw-r--r--src/charon/plugins/sql/sql_attribute.c226
-rw-r--r--src/charon/plugins/sql/sql_attribute.h51
-rw-r--r--src/charon/plugins/sql/sql_plugin.c9
-rw-r--r--src/charon/plugins/sql/sqlite.sql36
-rw-r--r--src/charon/plugins/sql/test.sql8
6 files changed, 331 insertions, 2 deletions
diff --git a/src/charon/plugins/sql/Makefile.am b/src/charon/plugins/sql/Makefile.am
index af01eac01..787b5e35b 100644
--- a/src/charon/plugins/sql/Makefile.am
+++ b/src/charon/plugins/sql/Makefile.am
@@ -5,6 +5,7 @@ AM_CFLAGS = -rdynamic
plugin_LTLIBRARIES = libcharon-sql.la
libcharon_sql_la_SOURCES = sql_plugin.h sql_plugin.c \
- sql_config.h sql_config.c sql_cred.h sql_cred.c sql_logger.h sql_logger.c
+ sql_config.h sql_config.c sql_cred.h sql_cred.c \
+ sql_attribute.h sql_attribute.c sql_logger.h sql_logger.c
libcharon_sql_la_LDFLAGS = -module
diff --git a/src/charon/plugins/sql/sql_attribute.c b/src/charon/plugins/sql/sql_attribute.c
new file mode 100644
index 000000000..23ec62220
--- /dev/null
+++ b/src/charon/plugins/sql/sql_attribute.c
@@ -0,0 +1,226 @@
+/*
+ * Copyright (C) 2008 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 "sql_attribute.h"
+
+#include <daemon.h>
+
+typedef struct private_sql_attribute_t private_sql_attribute_t;
+
+/**
+ * private data of sql_attribute
+ */
+struct private_sql_attribute_t {
+
+ /**
+ * public functions
+ */
+ sql_attribute_t public;
+
+ /**
+ * database connection
+ */
+ database_t *db;
+};
+
+/**
+ * convert a address blob to an ip of the correct family
+ */
+static host_t *ip_from_chunk(chunk_t address)
+{
+ switch (address.len)
+ {
+ case 4:
+ return host_create_from_chunk(AF_INET, address, 0);
+ case 16:
+ return host_create_from_chunk(AF_INET6, address, 0);
+ default:
+ return NULL;
+ }
+}
+
+/**
+ * increment a chunk, as it would reprensent a network order integer
+ */
+static void increment_chunk(chunk_t chunk)
+{
+ int i;
+
+ for (i = chunk.len - 1; i >= 0; i++)
+ {
+ if (++chunk.ptr[i] != 0)
+ {
+ return;
+ }
+ }
+}
+
+/**
+ * Lookup if we have an existing lease
+ */
+static host_t* get_lease(private_sql_attribute_t *this,
+ char *name, identification_t *id)
+{
+ enumerator_t *e;
+ chunk_t address;
+ host_t *ip = NULL;
+ int lease;
+
+ POS;
+ e = this->db->query(this->db,
+ "SELECT l.id, l.address FROM leases AS l "
+ "JOIN pools AS p ON l.pool = p.id "
+ "JOIN identities AS i ON l.identity = i.id "
+ "WHERE p.name = ? AND i.type = ? AND i.data = ? "
+ "AND (l.release ISNULL OR p.timeout ISNULL "
+ " OR (l.release < (p.timeout + l.acquire))) "
+ "ORDER BY l.acquire LIMIT 1", DB_TEXT, name,
+ DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id),
+ DB_INT, DB_BLOB);
+ if (e)
+ {
+ if (e->enumerate(e, &lease, &address))
+ {
+ if (this->db->execute(this->db, NULL,
+ "UPDATE leases SET release = NULL WHERE id = ?",
+ DB_INT, lease) > 0)
+ {
+ POS;
+ ip = ip_from_chunk(address);
+ }
+ }
+ e->destroy(e);
+ }
+ return ip;
+}
+
+/**
+ * Create a new lease entry for client
+ */
+static host_t* create_lease(private_sql_attribute_t *this,
+ char *name, identification_t *id)
+{
+ enumerator_t *e, *f;
+ chunk_t address;
+ host_t *ip = NULL;
+ int pool, identity = 0;
+ POS;
+
+ e = this->db->query(this->db,
+ "SELECT id, next FROM pools WHERE name = ? AND next <= end",
+ DB_TEXT, name,
+ DB_INT, DB_BLOB);
+ if (!e)
+ {
+ return NULL;
+ }
+ if (e->enumerate(e, &pool, &address))
+ {
+ f = this->db->query(this->db,
+ "SELECT id FROM identities WHERE type = ? AND data = ?",
+ DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id),
+ DB_INT);
+ if (f)
+ {
+ if (!f->enumerate(f, &identity))
+ {
+ this->db->execute(this->db, &identity,
+ "INSERT INTO identities (type, data) VALUES (?, ?)",
+ DB_INT, id->get_type(id), DB_BLOB, id->get_encoding(id));
+ }
+ f->destroy(f);
+ }
+ if (identity)
+ {
+ if (this->db->execute(this->db, NULL,
+ "INSERT INTO leases "
+ "(pool, address, identity) VALUES (?, ?, ?)",
+ DB_INT, pool, DB_BLOB, address, DB_INT, identity) > 0)
+ {
+ POS;
+ ip = ip_from_chunk(address);
+ increment_chunk(address);
+ this->db->execute(this->db, NULL,
+ "UPDATE pools set next = ? WHERE id = ?",
+ DB_BLOB, address, DB_INT, pool);
+ }
+ }
+ }
+ e->destroy(e);
+ return ip;
+}
+
+/**
+ * Implementation of attribute_provider_t.acquire_address
+ */
+static host_t* acquire_address(private_sql_attribute_t *this,
+ char *name, identification_t *id,
+ auth_info_t *auth, host_t *requested)
+{
+ host_t *ip;
+
+ ip = get_lease(this, name, id);
+ if (!ip)
+ {
+ ip = create_lease(this, name, id);
+ }
+ return ip;
+}
+
+/**
+ * Implementation of attribute_provider_t.release_address
+ */
+static bool release_address(private_sql_attribute_t *this,
+ char *name, host_t *address)
+{
+ if (this->db->execute(this->db, NULL,
+ "UPDATE leases SET release = DATE('NOW') WHERE "
+ "pool IN (SELECT id FROM pools WHERE name = ?) AND "
+ "address = ? "
+ "ORDER BY acquire LIMIT 1",
+ DB_TEXT, name, DB_BLOB, address->get_address(address)) > 0)
+ {
+ POS;
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/**
+ * Implementation of sql_attribute_t.destroy
+ */
+static void destroy(private_sql_attribute_t *this)
+{
+ free(this);
+}
+
+/*
+ * see header file
+ */
+sql_attribute_t *sql_attribute_create(database_t *db)
+{
+ private_sql_attribute_t *this = malloc_thing(private_sql_attribute_t);
+
+ this->public.provider.acquire_address = (host_t*(*)(attribute_provider_t *this, char*, identification_t *,auth_info_t *, host_t *))acquire_address;
+ this->public.provider.release_address = (bool(*)(attribute_provider_t *this, char*,host_t *))release_address;
+ this->public.destroy = (void(*)(sql_attribute_t*))destroy;
+
+ this->db = db;
+
+ return &this->public;
+}
+
diff --git a/src/charon/plugins/sql/sql_attribute.h b/src/charon/plugins/sql/sql_attribute.h
new file mode 100644
index 000000000..211204ced
--- /dev/null
+++ b/src/charon/plugins/sql/sql_attribute.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2008 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 sql_attribute sql_attribute
+ * @{ @ingroup sql
+ */
+
+#ifndef SQL_ATTRIBUTE_H_
+#define SQL_ATTRIBUTE_H_
+
+#include <config/attributes/attribute_provider.h>
+
+typedef struct sql_attribute_t sql_attribute_t;
+
+/**
+ * SQL database based IKEv2 cfg attribute provider.
+ */
+struct sql_attribute_t {
+
+ /**
+ * Implements attribute provider interface
+ */
+ attribute_provider_t provider;
+
+ /**
+ * Destroy a sql_attribute instance.
+ */
+ void (*destroy)(sql_attribute_t *this);
+};
+
+/**
+ * Create a sql_attribute instance.
+ */
+sql_attribute_t *sql_attribute_create(database_t *db);
+
+#endif /* SQL_ATTRIBUTE_H_ @}*/
diff --git a/src/charon/plugins/sql/sql_plugin.c b/src/charon/plugins/sql/sql_plugin.c
index 4712c1e9a..d5689363e 100644
--- a/src/charon/plugins/sql/sql_plugin.c
+++ b/src/charon/plugins/sql/sql_plugin.c
@@ -20,6 +20,7 @@
#include <daemon.h>
#include "sql_config.h"
#include "sql_cred.h"
+#include "sql_attribute.h"
#include "sql_logger.h"
typedef struct private_sql_plugin_t private_sql_plugin_t;
@@ -50,6 +51,11 @@ struct private_sql_plugin_t {
sql_cred_t *cred;
/**
+ * CFG attributes
+ */
+ sql_attribute_t *attribute;
+
+ /**
* bus listener/logger
*/
sql_logger_t *logger;
@@ -65,6 +71,7 @@ static void destroy(private_sql_plugin_t *this)
charon->bus->remove_listener(charon->bus, &this->logger->listener);
this->config->destroy(this->config);
this->cred->destroy(this->cred);
+ this->attribute->destroy(this->attribute);
this->logger->destroy(this->logger);
this->db->destroy(this->db);
free(this);
@@ -98,10 +105,12 @@ plugin_t *plugin_create()
}
this->config = sql_config_create(this->db);
this->cred = sql_cred_create(this->db);
+ this->attribute = sql_attribute_create(this->db);
this->logger = sql_logger_create(this->db);
charon->backends->add_backend(charon->backends, &this->config->backend);
charon->credentials->add_set(charon->credentials, &this->cred->set);
+ charon->attributes->add_provider(charon->attributes, &this->attribute->provider);
charon->bus->add_listener(charon->bus, &this->logger->listener);
return &this->public.plugin;
diff --git a/src/charon/plugins/sql/sqlite.sql b/src/charon/plugins/sql/sqlite.sql
index bf2d9f815..a3f527b90 100644
--- a/src/charon/plugins/sql/sqlite.sql
+++ b/src/charon/plugins/sql/sqlite.sql
@@ -150,6 +150,41 @@ CREATE TABLE shared_secret_identity (
PRIMARY KEY (shared_secret, identity)
);
+DROP TABLE IF EXISTS pools;
+CREATE TABLE pools (
+ id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ name TEXT NOT NULL,
+ start BLOB NOT NULL,
+ end BLOB NOT NULL,
+ next BLOB NOT NULL,
+ timeout INTEGER DEFAULT NULL
+);
+DROP INDEX IF EXISTS pools_name;
+CREATE INDEX pools_name ON pools (
+ name
+);
+
+DROP TABLE IF EXISTS leases;
+CREATE TABLE leases (
+ id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+ pool INTEGER NOT NULL,
+ address BLOB NOT NULL,
+ identity INTEGER NOT NULL,
+ acquire INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,
+ release INTEGER DEFAULT NULL
+);
+DROP INDEX IF EXISTS leases_pool;
+CREATE INDEX leases_pool ON leases (
+ pool
+);
+DROP INDEX IF EXISTS leases_identity;
+CREATE INDEX leases_identity ON leases (
+ identity
+);
+DROP INDEX IF EXISTS leases_release;
+CREATE INDEX leases_release ON leases (
+ release
+);
DROP TABLE IF EXISTS ike_sas;
CREATE TABLE ike_sas (
@@ -167,7 +202,6 @@ CREATE TABLE ike_sas (
created INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-
DROP TABLE IF EXISTS logs;
CREATE TABLE logs (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
diff --git a/src/charon/plugins/sql/test.sql b/src/charon/plugins/sql/test.sql
index d706e6da8..f3cc0e8d4 100644
--- a/src/charon/plugins/sql/test.sql
+++ b/src/charon/plugins/sql/test.sql
@@ -116,3 +116,11 @@ INSERT INTO certificate_identity (
) VALUES (
2, 5
);
+
+INSERT INTO pools (
+ name, start, end, next
+) VALUES (
+ 'a', X'0a050000', X'0a05ffff', X'0a050000'
+);
+
+