469915476f19feffb32afc7039e4bcf30c6afdfb
[strongswan.git] / src / libcharon / plugins / medcli / medcli_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 "medcli_plugin.h"
17
18 #include "medcli_creds.h"
19 #include "medcli_config.h"
20 #include "medcli_listener.h"
21
22 #include <daemon.h>
23
24 typedef struct private_medcli_plugin_t private_medcli_plugin_t;
25
26 /**
27 * private data of medcli plugin
28 */
29 struct private_medcli_plugin_t {
30
31 /**
32 * implements plugin interface
33 */
34 medcli_plugin_t public;
35
36 /**
37 * database connection instance
38 */
39 database_t *db;
40
41 /**
42 * medcli credential set instance
43 */
44 medcli_creds_t *creds;
45
46 /**
47 * medcli config database
48 */
49 medcli_config_t *config;
50
51 /**
52 * Listener to update database connection state
53 */
54 medcli_listener_t *listener;
55 };
56
57 METHOD(plugin_t, get_name, char*,
58 private_medcli_plugin_t *this)
59 {
60 return "medcli";
61 }
62
63 METHOD(plugin_t, destroy, void,
64 private_medcli_plugin_t *this)
65 {
66 charon->bus->remove_listener(charon->bus, &this->listener->listener);
67 charon->backends->remove_backend(charon->backends, &this->config->backend);
68 lib->credmgr->remove_set(lib->credmgr, &this->creds->set);
69 this->listener->destroy(this->listener);
70 this->config->destroy(this->config);
71 this->creds->destroy(this->creds);
72 this->db->destroy(this->db);
73 free(this);
74 }
75
76 /*
77 * see header file
78 */
79 plugin_t *medcli_plugin_create()
80 {
81 char *uri;
82 private_medcli_plugin_t *this;
83
84 INIT(this,
85 .public = {
86 .plugin = {
87 .get_name = _get_name,
88 .reload = (void*)return_false,
89 .destroy = _destroy,
90 },
91 },
92 );
93
94 uri = lib->settings->get_str(lib->settings,
95 "medcli.database", NULL);
96 if (!uri)
97 {
98 DBG1(DBG_CFG, "mediation client database URI not defined, skipped");
99 free(this);
100 return NULL;
101 }
102
103 this->db = lib->db->create(lib->db, uri);
104 if (this->db == NULL)
105 {
106 DBG1(DBG_CFG, "opening mediation client database failed");
107 free(this);
108 return NULL;
109 }
110
111 this->creds = medcli_creds_create(this->db);
112 this->config = medcli_config_create(this->db);
113 this->listener = medcli_listener_create(this->db);
114
115 lib->credmgr->add_set(lib->credmgr, &this->creds->set);
116 charon->backends->add_backend(charon->backends, &this->config->backend);
117 charon->bus->add_listener(charon->bus, &this->listener->listener);
118
119 return &this->public.plugin;
120 }
121