5df46d04f1c86b0240eaed9ab0e954cf937f0eb0
[strongswan.git] / src / libcharon / plugins / medsrv / medsrv_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 "medsrv_plugin.h"
17
18 #include "medsrv_creds.h"
19 #include "medsrv_config.h"
20
21 #include <daemon.h>
22
23 typedef struct private_medsrv_plugin_t private_medsrv_plugin_t;
24
25 /**
26 * private data of medsrv plugin
27 */
28 struct private_medsrv_plugin_t {
29
30 /**
31 * implements plugin interface
32 */
33 medsrv_plugin_t public;
34
35 /**
36 * database connection instance
37 */
38 database_t *db;
39
40 /**
41 * medsrv credential set instance
42 */
43 medsrv_creds_t *creds;
44
45 /**
46 * medsrv config database
47 */
48 medsrv_config_t *config;
49 };
50
51 METHOD(plugin_t, get_name, char*,
52 private_medsrv_plugin_t *this)
53 {
54 return "medsrv";
55 }
56
57 METHOD(plugin_t, destroy, void,
58 private_medsrv_plugin_t *this)
59 {
60 charon->backends->remove_backend(charon->backends, &this->config->backend);
61 lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
62 this->config->destroy(this->config);
63 this->creds->destroy(this->creds);
64 this->db->destroy(this->db);
65 free(this);
66 }
67
68 /*
69 * see header file
70 */
71 plugin_t *medsrv_plugin_create()
72 {
73 char *uri;
74 private_medsrv_plugin_t *this;
75
76 INIT(this,
77 .public = {
78 .plugin = {
79 .get_name = _get_name,
80 .reload = (void*)return_false,
81 .destroy = _destroy,
82 },
83 },
84 );
85
86 uri = lib->settings->get_str(lib->settings,
87 "medsrv.database", NULL);
88 if (!uri)
89 {
90 DBG1(DBG_CFG, "mediation database URI not defined, skipped");
91 free(this);
92 return NULL;
93 }
94
95 this->db = lib->db->create(lib->db, uri);
96 if (this->db == NULL)
97 {
98 DBG1(DBG_CFG, "opening mediation server database failed");
99 free(this);
100 return NULL;
101 }
102
103 this->creds = medsrv_creds_create(this->db);
104 this->config = medsrv_config_create(this->db);
105
106 lib->credmgr->add_set(lib->credmgr, &this->creds->set);
107 charon->backends->add_backend(charon->backends, &this->config->backend);
108
109 return &this->public.plugin;
110 }
111