logging to SQL database
authorMartin Willi <martin@strongswan.org>
Sat, 15 Mar 2008 14:17:09 +0000 (14:17 -0000)
committerMartin Willi <martin@strongswan.org>
Sat, 15 Mar 2008 14:17:09 +0000 (14:17 -0000)
src/charon/plugins/sql/Makefile.am
src/charon/plugins/sql/mysql.sql
src/charon/plugins/sql/sql_logger.c [new file with mode: 0644]
src/charon/plugins/sql/sql_logger.h [new file with mode: 0644]
src/charon/plugins/sql/sql_plugin.c
src/charon/plugins/sql/sqlite.sql

index 3ead79c..af01eac 100644 (file)
@@ -5,6 +5,6 @@ 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_config.h sql_config.c sql_cred.h sql_cred.c sql_logger.h sql_logger.c
 libcharon_sql_la_LDFLAGS = -module
 
index 4fee18d..72a7750 100644 (file)
@@ -114,3 +114,28 @@ CREATE TABLE private_keys (
   PRIMARY KEY  (`id`)
 ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
 
+
+DROP TABLE IF EXISTS ike_sas;
+CREATE TABLE ike_sas (
+  `local_spi` BLOB(8) NOT NULL,
+  `remote_spi` BLOB(8) NOT NULL,
+  `id` int(10) unsigned NOT NULL,
+  `initiator` tinyint(1) NOT NULL,
+  `local_id` varchar(64) collate utf8_unicode_ci default NULL,
+  `remote_id` varchar(64) collate utf8_unicode_ci default NULL,
+  `local` varchar(64) collate utf8_unicode_ci NOT NULL,
+  `remote` varchar(64) collate utf8_unicode_ci NOT NULL,
+  PRIMARY KEY  (local_spi(8))
+) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+
+DROP TABLE IF EXISTS logs;
+CREATE TABLE logs (
+  `id` int(10) unsigned NOT NULL auto_increment,
+  `local_spi` BLOB(8) NOT NULL,
+  `signal` tinyint(3) NOT NULL,
+  `level` tinyint(3) NOT NULL,
+  `msg`varchar(256) NOT NULL,
+  PRIMARY KEY  (`id`)
+) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
+
+
diff --git a/src/charon/plugins/sql/sql_logger.c b/src/charon/plugins/sql/sql_logger.c
new file mode 100644 (file)
index 0000000..a55777e
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * 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: sql_logger.c 3589 2008-03-13 14:14:44Z martin $
+ */
+
+#include <string.h>
+
+#include "sql_logger.h"
+
+#include <daemon.h>
+
+typedef struct private_sql_logger_t private_sql_logger_t;
+
+/**
+ * Private data of an sql_logger_t object
+ */
+struct private_sql_logger_t {
+
+       /**
+        * Public part
+        */
+       sql_logger_t public;
+       
+       /**
+        * database connection
+        */
+       database_t *db;
+       
+       /**
+        * logging level
+        */
+       int level;
+};
+
+
+/**
+ * Implementation of bus_listener_t.signal.
+ */
+static bool signal_(private_sql_logger_t *this, signal_t signal, level_t level,
+                                       int thread, ike_sa_t* ike_sa, char *format, va_list args)
+{
+       if (ike_sa && level <= this->level)
+       {
+               char buffer[8192], local_id[64], remote_id[64], local[40], remote[40];
+               char *current = buffer, *next;
+               chunk_t local_spi, remote_spi;
+               u_int64_t ispi, rspi;
+               bool initiator;
+               ike_sa_id_t *id;
+       
+               id = ike_sa->get_id(ike_sa);
+               initiator = id->is_initiator(id);
+               ispi = id->get_initiator_spi(id);
+               rspi = id->get_responder_spi(id);
+               if (initiator)
+               {
+                       local_spi.ptr = (char*)&ispi;
+                       remote_spi.ptr = (char*)&rspi;
+               }
+               else
+               {
+                       local_spi.ptr = (char*)&rspi;
+                       remote_spi.ptr = (char*)&ispi;
+               }
+               local_spi.len = remote_spi.len = sizeof(ispi);
+               snprintf(local_id, sizeof(local_id), "%D", ike_sa->get_my_id(ike_sa));
+               snprintf(remote_id, sizeof(remote_id), "%D", ike_sa->get_other_id(ike_sa));
+               snprintf(local, sizeof(local), "%H", ike_sa->get_my_host(ike_sa));
+               snprintf(remote, sizeof(remote), "%H", ike_sa->get_other_host(ike_sa));
+               
+               /* write in memory buffer first */
+               vsnprintf(buffer, sizeof(buffer), format, args);
+       
+               this->db->execute(this->db, NULL, "REPLACE INTO ike_sas ("
+                                                 "local_spi, remote_spi, id, initiator, "
+                                                 "local_id, remote_id, local, remote) "
+                                                 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
+                                                 DB_BLOB, local_spi, DB_BLOB, remote_spi,
+                                                 DB_INT, ike_sa->get_unique_id(ike_sa),
+                                                 DB_INT, initiator,
+                                                 DB_TEXT, local_id, DB_TEXT, remote_id, 
+                                                 DB_TEXT, local, DB_TEXT, remote);
+               /* do a log with every line */
+               while (current)
+               {
+                       next = strchr(current, '\n');
+                       if (next)
+                       {
+                               *(next++) = '\0';
+                       }
+                       this->db->execute(this->db, NULL,
+                                                         "INSERT INTO logs (local_spi, signal, level, msg) "
+                                                         "VALUES (?, ?, ?, ?)",
+                                                         DB_BLOB, local_spi, DB_INT, signal, DB_INT, level,
+                                                         DB_TEXT, current);
+                       current = next;
+               }
+       }
+       /* always stay registered */
+       return TRUE;
+}
+
+/**
+ * Implementation of sql_logger_t.destroy.
+ */
+static void destroy(private_sql_logger_t *this)
+{
+       free(this);
+}
+
+/**
+ * Described in header.
+ */
+sql_logger_t *sql_logger_create(database_t *db)
+{
+       private_sql_logger_t *this = malloc_thing(private_sql_logger_t);
+       
+       this->public.listener.signal = (bool(*)(bus_listener_t*,signal_t,level_t,int,ike_sa_t*,char*,va_list))signal_;
+       this->public.destroy = (void(*)(sql_logger_t*))destroy;
+       
+       this->db = db;
+       
+       this->level = lib->settings->get_int(lib->settings,
+                                                                                "charon.plugins.sql.loglevel", 1);
+       
+       return &this->public;
+}
+
diff --git a/src/charon/plugins/sql/sql_logger.h b/src/charon/plugins/sql/sql_logger.h
new file mode 100644 (file)
index 0000000..30507bc
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * 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: sql_logger.h 3594 2008-03-13 14:53:57Z martin $
+ */
+
+/**
+ * @defgroup sql_logger_i sql_logger
+ * @{ @ingroup sql
+ */
+
+#ifndef SQL_LOGGER_H_
+#define SQL_LOGGER_H_
+
+#include <bus/bus.h>
+#include <database/database.h>
+
+typedef struct sql_logger_t sql_logger_t;
+
+/**
+ * SQL database logger.
+ */
+struct sql_logger_t {
+
+       /**
+        * Implements bus_listener_t interface
+        */
+       bus_listener_t listener;
+       
+       /**
+        * Destry the backend.
+        */
+       void (*destroy)(sql_logger_t *this);    
+};
+
+/**
+ * Create a sql_logger instance.
+ *
+ * @param db           underlying database
+ * @return                     logger instance
+ */
+sql_logger_t *sql_logger_create(database_t *db);
+
+#endif /* SQL_LOGGER_H_ @}*/
index c006731..f1d34c2 100644 (file)
@@ -20,6 +20,7 @@
 #include <daemon.h>
 #include "sql_config.h"
 #include "sql_cred.h"
+#include "sql_logger.h"
 
 typedef struct private_sql_plugin_t private_sql_plugin_t;
 
@@ -47,6 +48,11 @@ struct private_sql_plugin_t {
         * credential set
         */
        sql_cred_t *cred;
+       
+       /**
+        * bus listener/logger
+        */
+       sql_logger_t *logger;
 };
 
 /**
@@ -56,8 +62,10 @@ static void destroy(private_sql_plugin_t *this)
 {
        charon->backends->remove_backend(charon->backends, &this->config->backend);
        charon->credentials->remove_set(charon->credentials, &this->cred->set);
+       charon->bus->remove_listener(charon->bus, &this->logger->listener);
        this->config->destroy(this->config);
        this->cred->destroy(this->cred);
+       this->logger->destroy(this->logger);
        this->db->destroy(this->db);
        free(this);
 }
@@ -90,9 +98,11 @@ plugin_t *plugin_create()
        }
        this->config = sql_config_create(this->db);
        this->cred = sql_cred_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->bus->add_listener(charon->bus, &this->logger->listener);
        
        return &this->public.plugin;
 }
index f004534..8ea1d90 100644 (file)
@@ -99,3 +99,27 @@ CREATE TABLE private_keys (
        keyid BLOB NOT NULL,
        data BLOB NOT NULL
 );
+
+DROP TABLE IF EXISTS ike_sas;
+CREATE TABLE ike_sas (
+  local_spi BLOB NOT NULL PRIMARY KEY,
+  remote_spi BLOB NOT NULL,
+  id INTEGER NOT NULL,
+  initiator INTEGER NOT NULL,
+  local_id TEXT NOT NULL,
+  remote_id TEXT NOT NULL,
+  local TEXT NOT NULL,
+  remote TEXT NOT NULL
+);
+
+DROP TABLE IF EXISTS logs;
+CREATE TABLE logs (
+  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
+  local_spi BLOB NOT NULL,
+  signal INTEGER NOT NULL,
+  level INTEGER NOT NULL,
+  msg TEXT NOT NULL
+);
+
+
+