Moved TLS stack to its own library
[strongswan.git] / src / libcharon / 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_logger.h"
22
23 typedef struct private_sql_plugin_t private_sql_plugin_t;
24
25 /**
26 * private data of sql plugin
27 */
28 struct private_sql_plugin_t {
29
30 /**
31 * implements plugin interface
32 */
33 sql_plugin_t public;
34
35 /**
36 * database connection instance
37 */
38 database_t *db;
39
40 /**
41 * configuration backend
42 */
43 sql_config_t *config;
44
45 /**
46 * credential set
47 */
48 sql_cred_t *cred;
49
50 /**
51 * bus listener/logger
52 */
53 sql_logger_t *logger;
54 };
55
56 /**
57 * Implementation of plugin_t.destroy
58 */
59 static void destroy(private_sql_plugin_t *this)
60 {
61 charon->backends->remove_backend(charon->backends, &this->config->backend);
62 lib->credmgr->remove_set(lib->credmgr, &this->cred->set);
63 charon->bus->remove_listener(charon->bus, &this->logger->listener);
64 this->config->destroy(this->config);
65 this->cred->destroy(this->cred);
66 this->logger->destroy(this->logger);
67 this->db->destroy(this->db);
68 free(this);
69 }
70
71 /*
72 * see header file
73 */
74 plugin_t *sql_plugin_create()
75 {
76 char *uri;
77 private_sql_plugin_t *this;
78
79 uri = lib->settings->get_str(lib->settings, "charon.plugins.sql.database", NULL);
80 if (!uri)
81 {
82 DBG1(DBG_CFG, "sql plugin: database URI not set");
83 return NULL;
84 }
85
86 this = malloc_thing(private_sql_plugin_t);
87
88 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
89
90 this->db = lib->db->create(lib->db, uri);
91 if (!this->db)
92 {
93 DBG1(DBG_CFG, "sql plugin failed to connect to database");
94 free(this);
95 return NULL;
96 }
97 this->config = sql_config_create(this->db);
98 this->cred = sql_cred_create(this->db);
99 this->logger = sql_logger_create(this->db);
100
101 charon->backends->add_backend(charon->backends, &this->config->backend);
102 lib->credmgr->add_set(lib->credmgr, &this->cred->set);
103 charon->bus->add_listener(charon->bus, &this->logger->listener);
104
105 return &this->public.plugin;
106 }
107