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