Code

8223d389a496f1ed85995ce8f2f318a8a16a9940
[sysdb.git] / src / plugins / store / network.c
1 /*
2  * SysDB - src/plugins/backend/store/network.c
3  * Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif
32 #include "sysdb.h"
33 #include "core/plugin.h"
34 #include "client/sock.h"
35 #include "utils/error.h"
36 #include "utils/proto.h"
37 #include "utils/os.h"
38 #include "utils/ssl.h"
40 #include "liboconfig/utils.h"
42 #include <errno.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <strings.h>
48 SDB_PLUGIN_MAGIC;
50 /*
51  * private data types
52  */
54 typedef struct {
55         sdb_client_t *client;
56         char *addr;
57         char *username;
58         sdb_ssl_options_t ssl_opts;
59 } user_data_t;
60 #define UD(obj) SDB_OBJ_WRAPPER(obj)->data
62 static void
63 user_data_destroy(void *obj)
64 {
65         user_data_t *ud = obj;
67         if (! ud)
68                 return;
70         if (ud->client)
71                 sdb_client_destroy(ud->client);
72         ud->client = NULL;
73         if (ud->addr)
74                 free(ud->addr);
75         ud->addr = NULL;
76         if (ud->username)
77                 free(ud->username);
78         ud->username = NULL;
80         sdb_ssl_free_options(&ud->ssl_opts);
82         free(ud);
83 } /* user_data_destroy */
85 /*
86  * store writer implementation
87  */
89 static int
90 store_rpc(user_data_t *ud, const char *msg, size_t msg_len)
91 {
92         sdb_strbuf_t *buf = sdb_strbuf_create(128);
93         uint32_t rstatus = 0;
94         ssize_t status;
96         if (sdb_client_eof(ud->client)) {
97                 sdb_client_close(ud->client);
98                 if (sdb_client_connect(ud->client, ud->username)) {
99                         sdb_log(SDB_LOG_ERR, "store::network: Failed to reconnect "
100                                         "to SysDB at %s as user %s", ud->addr, ud->username);
101                         return -1;
102                 }
103                 sdb_log(SDB_LOG_INFO, "store::network: Successfully reconnected "
104                                 "to SysDB at %s as user %s", ud->addr, ud->username);
105         }
107         status = sdb_client_rpc(ud->client, SDB_CONNECTION_STORE,
108                         (uint32_t)msg_len, msg, &rstatus, buf);
109         if (status < 0)
110                 sdb_log(SDB_LOG_ERR, "store::network: %s", sdb_strbuf_string(buf));
111         else if (rstatus != SDB_CONNECTION_OK) {
112                 sdb_log(SDB_LOG_ERR, "store::network: Failed to send object: %s",
113                                 sdb_strbuf_string(buf));
114                 status = -1;
115         }
117         sdb_strbuf_destroy(buf);
118         if (status < 0)
119                 return -1;
120         return 0;
121 } /* store_rpc */
123 static int
124 store_host(sdb_store_host_t *host, sdb_object_t *user_data)
126         sdb_proto_host_t h = { host->last_update, host->name };
127         size_t len = sdb_proto_marshal_host(NULL, 0, &h);
128         char buf[len];
130         sdb_proto_marshal_host(buf, len, &h);
131         return store_rpc(UD(user_data), buf, len);
132 } /* store_host */
134 static int
135 store_service(sdb_store_service_t *service, sdb_object_t *user_data)
137         sdb_proto_service_t s = {
138                 service->last_update, service->hostname, service->name,
139         };
140         ssize_t len = sdb_proto_marshal_service(NULL, 0, &s);
141         char buf[len];
143         sdb_proto_marshal_service(buf, len, &s);
144         return store_rpc(UD(user_data), buf, len);
145 } /* store_service */
147 static int
148 store_metric(sdb_store_metric_t *metric, sdb_object_t *user_data)
150         sdb_proto_metric_t m = {
151                 metric->last_update, metric->hostname, metric->name,
152                 metric->store.type, metric->store.id,
153         };
154         size_t len = sdb_proto_marshal_metric(NULL, 0, &m);
155         char buf[len];
157         sdb_proto_marshal_metric(buf, len, &m);
158         return store_rpc(UD(user_data), buf, len);
159 } /* store_metric */
161 static int
162 store_attr(sdb_store_attribute_t *attr, sdb_object_t *user_data)
164         sdb_proto_attribute_t a = {
165                 attr->last_update, attr->parent_type, attr->hostname, attr->parent,
166                 attr->key, attr->value,
167         };
168         size_t len = sdb_proto_marshal_attribute(NULL, 0, &a);
169         char buf[len];
171         sdb_proto_marshal_attribute(buf, len, &a);
172         return store_rpc(UD(user_data), buf, len);
173 } /* store_attr */
175 static sdb_store_writer_t store_impl = {
176         store_host, store_service, store_metric, store_attr,
177 };
179 /*
180  * plugin API
181  */
183 static int
184 store_init(sdb_object_t *user_data)
186         user_data_t *ud;
188         if (! user_data)
189                 return -1;
191         ud = SDB_OBJ_WRAPPER(user_data)->data;
192         if (sdb_client_connect(ud->client, ud->username)) {
193                 sdb_log(SDB_LOG_ERR, "store::network: Failed to connect "
194                                 "to SysDB at %s as user %s", ud->addr, ud->username);
195                 return -1;
196         }
198         sdb_log(SDB_LOG_INFO, "store::network: Successfully connected "
199                         "to SysDB at %s as user %s", ud->addr, ud->username);
200         return 0;
201 } /* store_init */
203 static int
204 store_config_server(oconfig_item_t *ci)
206         sdb_object_t *user_data;
207         user_data_t *ud;
208         int ret = 0;
209         int i;
211         ud = calloc(1, sizeof(*ud));
212         if (! ud) {
213                 char errbuf[1024];
214                 sdb_log(SDB_LOG_ERR, "store::network: Failed to allocate "
215                                 "a user-data object: %s",
216                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
217                 return -1;
218         }
220         if (oconfig_get_string(ci, &ud->addr)) {
221                 sdb_log(SDB_LOG_ERR, "store::network: Server requires "
222                                 "a single string argument\n\tUsage: <Server ADDRESS>");
223                 user_data_destroy(ud);
224                 return -1;
225         }
226         ud->addr = strdup(ud->addr);
227         if (! ud->addr) {
228                 sdb_log(SDB_LOG_ERR, "store::network: Failed to duplicate "
229                                 "a string");
230                 user_data_destroy(ud);
231                 return -1;
232         }
234         ud->client = sdb_client_create(ud->addr);
235         if (! ud->client) {
236                 char errbuf[1024];
237                 sdb_log(SDB_LOG_ERR, "store::network: Failed to create client "
238                                 "connecting to '%s': %s", ud->addr,
239                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
240                 user_data_destroy(ud);
241                 return -1;
242         }
244         for (i = 0; i < ci->children_num; ++i) {
245                 oconfig_item_t *child = ci->children + i;
246                 char *tmp = NULL;
248                 if (! strcasecmp(child->key, "Username")) {
249                         if (oconfig_get_string(child, &tmp)) {
250                                 ret = -1;
251                                 break;
252                         }
253                         ud->username = strdup(tmp);
254                 }
255                 else if (! strcasecmp(child->key, "SSLCertificate")) {
256                         if (oconfig_get_string(child, &tmp)) {
257                                 ret = -1;
258                                 break;
259                         }
260                         ud->ssl_opts.cert_file = strdup(tmp);
261                 }
262                 else if (! strcasecmp(child->key, "SSLCertificateKey")) {
263                         if (oconfig_get_string(child, &tmp)) {
264                                 ret = -1;
265                                 break;
266                         }
267                         ud->ssl_opts.key_file = strdup(tmp);
268                 }
269                 else if (! strcasecmp(child->key, "SSLCACertificates")) {
270                         if (oconfig_get_string(child, &tmp)) {
271                                 ret = -1;
272                                 break;
273                         }
274                         ud->ssl_opts.ca_file = strdup(tmp);
275                 }
276                 else
277                         sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
278                                         "unknown config option '%s' inside <Server %s>.",
279                                         child->key, ud->addr);
280         }
282         if (ret) {
283                 user_data_destroy(ud);
284                 return ret;
285         }
286         if (! ud->username)
287                 ud->username = sdb_get_current_user();
289         if (sdb_client_set_ssl_options(ud->client, &ud->ssl_opts)) {
290                 sdb_log(SDB_LOG_ERR, "store::network: Failed to apply SSL options");
291                 user_data_destroy(ud);
292                 return -1;
293         }
295         user_data = sdb_object_create_wrapper("store-network-userdata", ud,
296                         user_data_destroy);
297         if (! user_data) {
298                 char errbuf[1024];
299                 sdb_log(SDB_LOG_ERR, "store::network: Failed to allocate "
300                                 "a user-data wrapper object: %s",
301                                 sdb_strerror(errno, errbuf, sizeof(errbuf)));
302                 user_data_destroy(ud);
303                 return -1;
304         }
306         sdb_plugin_register_init(ud->addr, store_init, user_data);
307         sdb_plugin_register_writer(ud->addr, &store_impl, user_data);
308         sdb_object_deref(user_data);
309         return 0;
310 } /* store_config_server */
312 static int
313 store_config(oconfig_item_t *ci)
315         int i;
317         if (! ci) /* nothing to do to deconfigure this plugin */
318                 return 0;
320         for (i = 0; i < ci->children_num; ++i) {
321                 oconfig_item_t *child = ci->children + i;
323                 if (! strcasecmp(child->key, "Server"))
324                         store_config_server(child);
325                 else
326                         sdb_log(SDB_LOG_WARNING, "store::network: Ignoring "
327                                         "unknown config option '%s'.", child->key);
328         }
329         return 0;
330 } /* store_config */
332 int
333 sdb_module_init(sdb_plugin_info_t *info)
335         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_DESC,
336                         "send stored objects to a remote SysDB instance");
337         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_COPYRIGHT,
338                         "Copyright (C) 2015 Sebastian 'tokkee' Harl <sh@tokkee.org>");
339         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_LICENSE, "BSD");
340         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_VERSION, SDB_VERSION);
341         sdb_plugin_set_info(info, SDB_PLUGIN_INFO_PLUGIN_VERSION, SDB_VERSION);
343         sdb_plugin_register_config(store_config);
344         return 0;
345 } /* sdb_module_init */
347 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */