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