e6a8a8981fab309c2bb5c29a120f2227b5a89647
[strongswan.git] / src / libcharon / plugins / medcli / medcli_plugin.c
1 /*
2 * Copyright (C) 2013 Tobias Brunner
3 * Copyright (C) 2008 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 #include "medcli_plugin.h"
18
19 #include "medcli_creds.h"
20 #include "medcli_config.h"
21 #include "medcli_listener.h"
22
23 #include <daemon.h>
24
25 typedef struct private_medcli_plugin_t private_medcli_plugin_t;
26
27 /**
28 * private data of medcli plugin
29 */
30 struct private_medcli_plugin_t {
31
32 /**
33 * implements plugin interface
34 */
35 medcli_plugin_t public;
36
37 /**
38 * database connection instance
39 */
40 database_t *db;
41
42 /**
43 * medcli credential set instance
44 */
45 medcli_creds_t *creds;
46
47 /**
48 * medcli config database
49 */
50 medcli_config_t *config;
51
52 /**
53 * Listener to update database connection state
54 */
55 medcli_listener_t *listener;
56 };
57
58 METHOD(plugin_t, get_name, char*,
59 private_medcli_plugin_t *this)
60 {
61 return "medcli";
62 }
63
64 /**
65 * Connect to database
66 */
67 static bool open_database(private_medcli_plugin_t *this,
68 plugin_feature_t *feature, bool reg, void *cb_data)
69 {
70 if (reg)
71 {
72 char *uri;
73
74 uri = lib->settings->get_str(lib->settings,
75 "medcli.database", NULL);
76 if (!uri)
77 {
78 DBG1(DBG_CFG, "mediation client database URI not defined, skipped");
79 return FALSE;
80 }
81
82 this->db = lib->db->create(lib->db, uri);
83 if (this->db == NULL)
84 {
85 DBG1(DBG_CFG, "opening mediation client database failed");
86 return FALSE;
87 }
88
89 this->creds = medcli_creds_create(this->db);
90 this->config = medcli_config_create(this->db);
91 this->listener = medcli_listener_create(this->db);
92
93 lib->credmgr->add_set(lib->credmgr, &this->creds->set);
94 charon->backends->add_backend(charon->backends, &this->config->backend);
95 charon->bus->add_listener(charon->bus, &this->listener->listener);
96 }
97 else
98 {
99 charon->bus->remove_listener(charon->bus, &this->listener->listener);
100 charon->backends->remove_backend(charon->backends, &this->config->backend);
101 lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
102 this->listener->destroy(this->listener);
103 this->config->destroy(this->config);
104 this->creds->destroy(this->creds);
105 this->db->destroy(this->db);
106 }
107 return TRUE;
108 }
109
110 METHOD(plugin_t, get_features, int,
111 private_medcli_plugin_t *this, plugin_feature_t *features[])
112 {
113 static plugin_feature_t f[] = {
114 PLUGIN_CALLBACK((plugin_feature_callback_t)open_database, NULL),
115 PLUGIN_PROVIDE(CUSTOM, "medcli"),
116 PLUGIN_DEPENDS(DATABASE, DB_ANY),
117 };
118 *features = f;
119 return countof(f);
120 }
121
122 METHOD(plugin_t, destroy, void,
123 private_medcli_plugin_t *this)
124 {
125 free(this);
126 }
127
128 /*
129 * see header file
130 */
131 plugin_t *medcli_plugin_create()
132 {
133 private_medcli_plugin_t *this;
134
135 INIT(this,
136 .public = {
137 .plugin = {
138 .get_name = _get_name,
139 .get_features = _get_features,
140 .destroy = _destroy,
141 },
142 },
143 );
144
145 return &this->public.plugin;
146 }