8881feca9042ef2bcabf795275e62cbca9194c2c
[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 #include "vici_plugin.h"
17 #include "vici_dispatcher.h"
18 #include "vici_query.h"
19 #include "vici_control.h"
20 #include "vici_cred.h"
21 #include "vici_config.h"
22 #include "vici_attribute.h"
23 #include "vici_logger.h"
24
25 #include <library.h>
26 #include <hydra.h>
27 #include <daemon.h>
28
29 typedef struct private_vici_plugin_t private_vici_plugin_t;
30
31 /**
32 * Private members of vici_plugin_t
33 */
34 struct private_vici_plugin_t {
35
36 /**
37 * public functions
38 */
39 vici_plugin_t public;
40
41 /**
42 * Dispatcher, creating socket
43 */
44 vici_dispatcher_t *dispatcher;
45
46 /**
47 * Query commands
48 */
49 vici_query_t *query;
50
51 /**
52 * Control commands
53 */
54 vici_control_t *control;
55
56 /**
57 * Credential backend
58 */
59 vici_cred_t *cred;
60
61 /**
62 * Configuration backend
63 */
64 vici_config_t *config;
65
66 /**
67 * IKE attribute backend
68 */
69 vici_attribute_t *attrs;
70
71 /**
72 * Generic debug logger
73 */
74 vici_logger_t *logger;
75 };
76
77 METHOD(plugin_t, get_name, char*,
78 private_vici_plugin_t *this)
79 {
80 return "vici";
81 }
82
83 /**
84 * Register vici plugin features
85 */
86 static bool register_vici(private_vici_plugin_t *this,
87 plugin_feature_t *feature, bool reg, void *data)
88 {
89 if (reg)
90 {
91 char *uri;
92
93 uri = lib->settings->get_str(lib->settings, "%s.plugins.vici.socket",
94 VICI_DEFAULT_URI, lib->ns);
95 this->dispatcher = vici_dispatcher_create(uri);
96 if (this->dispatcher)
97 {
98 this->query = vici_query_create(this->dispatcher);
99 this->control = vici_control_create(this->dispatcher);
100 this->cred = vici_cred_create(this->dispatcher);
101 this->config = vici_config_create(this->dispatcher);
102 this->attrs = vici_attribute_create(this->dispatcher);
103 this->logger = vici_logger_create(this->dispatcher);
104
105 charon->backends->add_backend(charon->backends,
106 &this->config->backend);
107 hydra->attributes->add_provider(hydra->attributes,
108 &this->attrs->provider);
109 charon->bus->add_logger(charon->bus, &this->logger->logger);
110 return TRUE;
111 }
112 return FALSE;
113 }
114 else
115 {
116 charon->bus->remove_logger(charon->bus, &this->logger->logger);
117 hydra->attributes->remove_provider(hydra->attributes,
118 &this->attrs->provider);
119 charon->backends->remove_backend(charon->backends,
120 &this->config->backend);
121
122 this->logger->destroy(this->logger);
123 this->attrs->destroy(this->attrs);
124 this->config->destroy(this->config);
125 this->cred->destroy(this->cred);
126 this->control->destroy(this->control);
127 this->query->destroy(this->query);
128 this->dispatcher->destroy(this->dispatcher);
129 }
130 return TRUE;
131 }
132
133 METHOD(plugin_t, get_features, int,
134 private_vici_plugin_t *this, plugin_feature_t *features[])
135 {
136 static plugin_feature_t f[] = {
137 PLUGIN_CALLBACK((plugin_feature_callback_t)register_vici, NULL),
138 PLUGIN_PROVIDE(CUSTOM, "vici"),
139 };
140 *features = f;
141 return countof(f);
142 }
143
144 METHOD(plugin_t, destroy, void,
145 private_vici_plugin_t *this)
146 {
147 free(this);
148 }
149
150 /*
151 * see header file
152 */
153 plugin_t *vici_plugin_create()
154 {
155 private_vici_plugin_t *this;
156
157 INIT(this,
158 .public = {
159 .plugin = {
160 .get_name = _get_name,
161 .reload = (void*)return_false,
162 .get_features = _get_features,
163 .destroy = _destroy,
164 },
165 },
166 );
167
168 return &this->public.plugin;
169 }