Moving charon to libcharon.
[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 /**
52 * Implementation of plugin_t.destroy
53 */
54 static void destroy(private_medsrv_plugin_t *this)
55 {
56 charon->backends->remove_backend(charon->backends, &this->config->backend);
57 charon->credentials->remove_set(charon->credentials, &this->creds->set);
58 this->config->destroy(this->config);
59 this->creds->destroy(this->creds);
60 this->db->destroy(this->db);
61 free(this);
62 }
63
64 /*
65 * see header file
66 */
67 plugin_t *medsrv_plugin_create()
68 {
69 char *uri;
70 private_medsrv_plugin_t *this = malloc_thing(private_medsrv_plugin_t);
71
72 this->public.plugin.destroy = (void(*)(plugin_t*))destroy;
73
74 uri = lib->settings->get_str(lib->settings,
75 "medsrv.database", NULL);
76 if (!uri)
77 {
78 DBG1(DBG_CFG, "mediation database URI not defined, skipped");
79 free(this);
80 return NULL;
81 }
82
83 this->db = lib->db->create(lib->db, uri);
84 if (this->db == NULL)
85 {
86 DBG1(DBG_CFG, "opening mediation server database failed");
87 free(this);
88 return NULL;
89 }
90
91 this->creds = medsrv_creds_create(this->db);
92 this->config = medsrv_config_create(this->db);
93
94 charon->credentials->add_set(charon->credentials, &this->creds->set);
95 charon->backends->add_backend(charon->backends, &this->config->backend);
96
97 return &this->public.plugin;
98 }
99