65691cc00c5c6e5969ecf28842373f07fccf50ec
[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
16 #include "sql_plugin.h"
17
18 #include <daemon.h>
19 #include "sql_config.h"
20 #include "sql_cred.h"
21 #include "sql_attribute.h"
22 #include "sql_logger.h"
23
24 typedef struct private_sql_plugin_t private_sql_plugin_t;
25
26 /**
27 * private data of sql plugin
28 */
29 struct private_sql_plugin_t {
30
31 /**
32 * implements plugin interface
33 */
34 sql_plugin_t public;
35
36 /**
37 * database connection instance
38 */
39 database_t *db;
40
41 /**
42 * configuration backend
43 */
44 sql_config_t *config;
45
46 /**
47 * credential set
48 */
49 sql_cred_t *cred;
50
51 /**
52 * CFG attributes
53 */
54 sql_attribute_t *attribute;
55
56 /**
57 * bus listener/logger
58 */
59 sql_logger_t *logger;
60 };
61
62 /**
63 * Implementation of plugin_t.destroy
64 */
65 static void destroy(private_sql_plugin_t *this)
66 {
67 charon->backends->remove_backend(charon->backends, &this->config->backend);
68 charon->credentials->remove_set(charon->credentials, &this->cred->set);
69 charon->attributes->remove_provider(charon->attributes, &this->attribute->provider);
70 charon->bus->remove_listener(charon->bus, &this->logger->listener);
71 this->config->destroy(this->config);
72 this->cred->destroy(this->cred);
73 this->attribute->destroy(this->attribute);
74 this->logger->destroy(this->logger);
75 this->db->destroy(this->db);
76 free(this);
77 }
78
79 /*
80 * see header file
81 */
82 plugin_t *plugin_create()
83 {
84 char *uri;
85 private_sql_plugin_t *this;
86
87 uri = lib->settings->get_str(lib->settings, "charon.plugins.sql.database", NULL);
88 if (!uri)
89 {
90 DBG1(DBG_CFG, "sql plugin: database URI not set");
91 return NULL;
92 }
93
94 this = malloc_thing(private_sql_plugin_t);
95
96 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
97
98 this->db = lib->db->create(lib->db, uri);
99 if (!this->db)
100 {
101 DBG1(DBG_CFG, "sql plugin failed to connect to database");
102 free(this);
103 return NULL;
104 }
105 this->config = sql_config_create(this->db);
106 this->cred = sql_cred_create(this->db);
107 this->attribute = sql_attribute_create(this->db);
108 this->logger = sql_logger_create(this->db);
109
110 charon->backends->add_backend(charon->backends, &this->config->backend);
111 charon->credentials->add_set(charon->credentials, &this->cred->set);
112 charon->attributes->add_provider(charon->attributes, &this->attribute->provider);
113 charon->bus->add_listener(charon->bus, &this->logger->listener);
114
115 return &this->public.plugin;
116 }
117