medsrv: Use plugin features with dependency on database implementation
[strongswan.git] / src / libcharon / plugins / medsrv / medsrv_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 "medsrv_plugin.h"
18
19 #include "medsrv_creds.h"
20 #include "medsrv_config.h"
21
22 #include <daemon.h>
23
24 typedef struct private_medsrv_plugin_t private_medsrv_plugin_t;
25
26 /**
27 * private data of medsrv plugin
28 */
29 struct private_medsrv_plugin_t {
30
31 /**
32 * implements plugin interface
33 */
34 medsrv_plugin_t public;
35
36 /**
37 * database connection instance
38 */
39 database_t *db;
40
41 /**
42 * medsrv credential set instance
43 */
44 medsrv_creds_t *creds;
45
46 /**
47 * medsrv config database
48 */
49 medsrv_config_t *config;
50 };
51
52 METHOD(plugin_t, get_name, char*,
53 private_medsrv_plugin_t *this)
54 {
55 return "medsrv";
56 }
57
58 /**
59 * Connect to database
60 */
61 static bool open_database(private_medsrv_plugin_t *this,
62 plugin_feature_t *feature, bool reg, void *cb_data)
63 {
64 if (reg)
65 {
66 char *uri;
67
68 uri = lib->settings->get_str(lib->settings,
69 "medsrv.database", NULL);
70 if (!uri)
71 {
72 DBG1(DBG_CFG, "mediation database URI not defined, skipped");
73 return FALSE;
74 }
75
76 this->db = lib->db->create(lib->db, uri);
77 if (this->db == NULL)
78 {
79 DBG1(DBG_CFG, "opening mediation server database failed");
80 return FALSE;
81 }
82
83 this->creds = medsrv_creds_create(this->db);
84 this->config = medsrv_config_create(this->db);
85
86 lib->credmgr->add_set(lib->credmgr, &this->creds->set);
87 charon->backends->add_backend(charon->backends, &this->config->backend);
88 }
89 else
90 {
91 charon->backends->remove_backend(charon->backends, &this->config->backend);
92 lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
93 this->config->destroy(this->config);
94 this->creds->destroy(this->creds);
95 this->db->destroy(this->db);
96 }
97 return TRUE;
98 }
99
100 METHOD(plugin_t, get_features, int,
101 private_medsrv_plugin_t *this, plugin_feature_t *features[])
102 {
103 static plugin_feature_t f[] = {
104 PLUGIN_CALLBACK((plugin_feature_callback_t)open_database, NULL),
105 PLUGIN_PROVIDE(CUSTOM, "medsrv"),
106 PLUGIN_DEPENDS(DATABASE, DB_ANY),
107 };
108 *features = f;
109 return countof(f);
110 }
111
112 METHOD(plugin_t, destroy, void,
113 private_medsrv_plugin_t *this)
114 {
115 free(this);
116 }
117
118 /*
119 * see header file
120 */
121 plugin_t *medsrv_plugin_create()
122 {
123 private_medsrv_plugin_t *this;
124
125 INIT(this,
126 .public = {
127 .plugin = {
128 .get_name = _get_name,
129 .get_features = _get_features,
130 .destroy = _destroy,
131 },
132 },
133 );
134
135 return &this->public.plugin;
136 }