ed7c743c7ade1a7d2cf19b1cb7a4794ecadcab87
[strongswan.git] / src / libcharon / plugins / vici / vici_plugin.c
1 /*
2 * Copyright (C) 2014 Martin Willi
3 * Copyright (C) 2014 revosec AG
4 *
5 * Copyright (C) 2015 Andreas Steffen
6 * HSR Hochschule fuer Technik Rapperswil
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /*
20 * Copyright (C) 2014 Timo Teräs <timo.teras@iki.fi>
21 *
22 * Permission is hereby granted, free of charge, to any person obtaining a copy
23 * of this software and associated documentation files (the "Software"), to deal
24 * in the Software without restriction, including without limitation the rights
25 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
26 * copies of the Software, and to permit persons to whom the Software is
27 * furnished to do so, subject to the following conditions:
28 *
29 * The above copyright notice and this permission notice shall be included in
30 * all copies or substantial portions of the Software.
31 *
32 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
38 * THE SOFTWARE.
39 */
40
41 #include "vici_plugin.h"
42 #include "vici_dispatcher.h"
43 #include "vici_query.h"
44 #include "vici_control.h"
45 #include "vici_cred.h"
46 #include "vici_config.h"
47 #include "vici_attribute.h"
48 #include "vici_authority.h"
49 #include "vici_logger.h"
50
51 #include <library.h>
52 #include <daemon.h>
53
54 typedef struct private_vici_plugin_t private_vici_plugin_t;
55
56 /**
57 * Private members of vici_plugin_t
58 */
59 struct private_vici_plugin_t {
60
61 /**
62 * public functions
63 */
64 vici_plugin_t public;
65
66 /**
67 * Dispatcher, creating socket
68 */
69 vici_dispatcher_t *dispatcher;
70
71 /**
72 * Query commands
73 */
74 vici_query_t *query;
75
76 /**
77 * Control commands
78 */
79 vici_control_t *control;
80
81 /**
82 * Credential backend
83 */
84 vici_cred_t *cred;
85
86 /**
87 * Certification Authority backend
88 */
89 vici_authority_t *authority;
90
91 /**
92 * Configuration backend
93 */
94 vici_config_t *config;
95
96 /**
97 * IKE attribute backend
98 */
99 vici_attribute_t *attrs;
100
101 /**
102 * Generic debug logger
103 */
104 vici_logger_t *logger;
105 };
106
107 METHOD(plugin_t, get_name, char*,
108 private_vici_plugin_t *this)
109 {
110 return "vici";
111 }
112
113 /**
114 * Register vici plugin features
115 */
116 static bool register_vici(private_vici_plugin_t *this,
117 plugin_feature_t *feature, bool reg, void *data)
118 {
119 if (reg)
120 {
121 char *uri;
122
123 uri = lib->settings->get_str(lib->settings, "%s.plugins.vici.socket",
124 VICI_DEFAULT_URI, lib->ns);
125 this->dispatcher = vici_dispatcher_create(uri);
126 if (this->dispatcher)
127 {
128 this->query = vici_query_create(this->dispatcher);
129 this->control = vici_control_create(this->dispatcher);
130 this->cred = vici_cred_create(this->dispatcher);
131 this->authority = vici_authority_create(this->dispatcher,
132 this->cred);
133 lib->credmgr->add_set(lib->credmgr, &this->authority->set);
134 this->config = vici_config_create(this->dispatcher, this->authority,
135 this->cred);
136 this->attrs = vici_attribute_create(this->dispatcher);
137 this->logger = vici_logger_create(this->dispatcher);
138
139 charon->backends->add_backend(charon->backends,
140 &this->config->backend);
141 charon->attributes->add_provider(charon->attributes,
142 &this->attrs->provider);
143 charon->bus->add_logger(charon->bus, &this->logger->logger);
144 charon->bus->add_listener(charon->bus, &this->query->listener);
145 return TRUE;
146 }
147 return FALSE;
148 }
149 else
150 {
151 charon->bus->remove_listener(charon->bus, &this->query->listener);
152 charon->bus->remove_logger(charon->bus, &this->logger->logger);
153 charon->attributes->remove_provider(charon->attributes,
154 &this->attrs->provider);
155 charon->backends->remove_backend(charon->backends,
156 &this->config->backend);
157
158 this->logger->destroy(this->logger);
159 this->attrs->destroy(this->attrs);
160 this->config->destroy(this->config);
161 lib->credmgr->remove_set(lib->credmgr, &this->authority->set);
162 this->authority->destroy(this->authority);
163 this->cred->destroy(this->cred);
164 this->control->destroy(this->control);
165 this->query->destroy(this->query);
166 this->dispatcher->destroy(this->dispatcher);
167 }
168 return TRUE;
169 }
170
171 METHOD(plugin_t, get_features, int,
172 private_vici_plugin_t *this, plugin_feature_t *features[])
173 {
174 static plugin_feature_t f[] = {
175 PLUGIN_CALLBACK((plugin_feature_callback_t)register_vici, NULL),
176 PLUGIN_PROVIDE(CUSTOM, "vici"),
177 };
178 *features = f;
179 return countof(f);
180 }
181
182 METHOD(plugin_t, destroy, void,
183 private_vici_plugin_t *this)
184 {
185 free(this);
186 }
187
188 /*
189 * see header file
190 */
191 plugin_t *vici_plugin_create()
192 {
193 private_vici_plugin_t *this;
194
195 INIT(this,
196 .public = {
197 .plugin = {
198 .get_name = _get_name,
199 .reload = (void*)return_false,
200 .get_features = _get_features,
201 .destroy = _destroy,
202 },
203 },
204 );
205
206 return &this->public.plugin;
207 }