logging to SQL database
[strongswan.git] / src / charon / plugins / sql / sql_plugin.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "sql_plugin.h"
19
20 #include <daemon.h>
21 #include "sql_config.h"
22 #include "sql_cred.h"
23 #include "sql_logger.h"
24
25 typedef struct private_sql_plugin_t private_sql_plugin_t;
26
27 /**
28 * private data of sql plugin
29 */
30 struct private_sql_plugin_t {
31
32 /**
33 * implements plugin interface
34 */
35 sql_plugin_t public;
36
37 /**
38 * database connection instance
39 */
40 database_t *db;
41
42 /**
43 * configuration backend
44 */
45 sql_config_t *config;
46
47 /**
48 * credential set
49 */
50 sql_cred_t *cred;
51
52 /**
53 * bus listener/logger
54 */
55 sql_logger_t *logger;
56 };
57
58 /**
59 * Implementation of plugin_t.destroy
60 */
61 static void destroy(private_sql_plugin_t *this)
62 {
63 charon->backends->remove_backend(charon->backends, &this->config->backend);
64 charon->credentials->remove_set(charon->credentials, &this->cred->set);
65 charon->bus->remove_listener(charon->bus, &this->logger->listener);
66 this->config->destroy(this->config);
67 this->cred->destroy(this->cred);
68 this->logger->destroy(this->logger);
69 this->db->destroy(this->db);
70 free(this);
71 }
72
73 /*
74 * see header file
75 */
76 plugin_t *plugin_create()
77 {
78 char *uri;
79 private_sql_plugin_t *this;
80
81 uri = lib->settings->get_str(lib->settings, "charon.plugins.sql.database", NULL);
82 if (!uri)
83 {
84 DBG1(DBG_CFG, "SQL plugin database URI not set");
85 return NULL;
86 }
87
88 this = malloc_thing(private_sql_plugin_t);
89
90 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
91
92 this->db = lib->db->create(lib->db, uri);
93 if (!this->db)
94 {
95 DBG1(DBG_CFG, "SQL plugin failed to connect to database");
96 free(this);
97 return NULL;
98 }
99 this->config = sql_config_create(this->db);
100 this->cred = sql_cred_create(this->db);
101 this->logger = sql_logger_create(this->db);
102
103 charon->backends->add_backend(charon->backends, &this->config->backend);
104 charon->credentials->add_set(charon->credentials, &this->cred->set);
105 charon->bus->add_listener(charon->bus, &this->logger->listener);
106
107 return &this->public.plugin;
108 }
109