attr-sql: Use plugin features with dependency to database backend
[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 METHOD(plugin_t, get_name, char*,
57 private_sql_plugin_t *this)
58 {
59 return "sql";
60 }
61
62 METHOD(plugin_t, destroy, void,
63 private_sql_plugin_t *this)
64 {
65 charon->backends->remove_backend(charon->backends, &this->config->backend);
66 lib->credmgr->remove_set(lib->credmgr, &this->cred->set);
67 charon->bus->remove_logger(charon->bus, &this->logger->logger);
68 this->config->destroy(this->config);
69 this->cred->destroy(this->cred);
70 this->logger->destroy(this->logger);
71 this->db->destroy(this->db);
72 free(this);
73 }
74
75 /*
76 * see header file
77 */
78 plugin_t *sql_plugin_create()
79 {
80 char *uri;
81 private_sql_plugin_t *this;
82
83 uri = lib->settings->get_str(lib->settings, "%s.plugins.sql.database",
84 NULL, charon->name);
85 if (!uri)
86 {
87 DBG1(DBG_CFG, "sql plugin: database URI not set");
88 return NULL;
89 }
90
91 INIT(this,
92 .public = {
93 .plugin = {
94 .get_name = _get_name,
95 .reload = (void*)return_false,
96 .destroy = _destroy,
97 },
98 },
99 .db = lib->db->create(lib->db, uri),
100 );
101
102 if (!this->db)
103 {
104 DBG1(DBG_CFG, "sql plugin failed to connect to database");
105 free(this);
106 return NULL;
107 }
108 this->config = sql_config_create(this->db);
109 this->cred = sql_cred_create(this->db);
110 this->logger = sql_logger_create(this->db);
111
112 charon->backends->add_backend(charon->backends, &this->config->backend);
113 lib->credmgr->add_set(lib->credmgr, &this->cred->set);
114 charon->bus->add_logger(charon->bus, &this->logger->logger);
115
116 return &this->public.plugin;
117 }
118