1 /**
2 * collectd - src/network.c
3 * Copyright (C) 2005-2010 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; only version 2.1 of the License is
9 * applicable.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * Authors:
21 * Florian octo Forster <octo at verplant.org>
22 * Aman Gupta <aman at tmm1.net>
23 **/
25 #define _BSD_SOURCE /* For struct ip_mreq */
27 #include "collectd.h"
28 #include "plugin.h"
29 #include "common.h"
30 #include "configfile.h"
31 #include "utils_fbhash.h"
32 #include "utils_avltree.h"
33 #include "utils_cache.h"
34 #include "utils_complain.h"
36 #include "network.h"
38 #if HAVE_PTHREAD_H
39 # include <pthread.h>
40 #endif
41 #if HAVE_SYS_SOCKET_H
42 # include <sys/socket.h>
43 #endif
44 #if HAVE_NETDB_H
45 # include <netdb.h>
46 #endif
47 #if HAVE_NETINET_IN_H
48 # include <netinet/in.h>
49 #endif
50 #if HAVE_ARPA_INET_H
51 # include <arpa/inet.h>
52 #endif
53 #if HAVE_POLL_H
54 # include <poll.h>
55 #endif
56 #if HAVE_NET_IF_H
57 # include <net/if.h>
58 #endif
60 #if HAVE_LIBGCRYPT
61 # include <gcrypt.h>
62 GCRY_THREAD_OPTION_PTHREAD_IMPL;
63 #endif
65 #ifndef IPV6_ADD_MEMBERSHIP
66 # ifdef IPV6_JOIN_GROUP
67 # define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
68 # else
69 # error "Neither IP_ADD_MEMBERSHIP nor IPV6_JOIN_GROUP is defined"
70 # endif
71 #endif /* !IP_ADD_MEMBERSHIP */
73 /*
74 * Maximum size required for encryption / signing:
75 *
76 * 42 bytes for the encryption header
77 * + 64 bytes for the username
78 * -----------
79 * = 106 bytes
80 */
81 #define BUFF_SIG_SIZE 106
83 /*
84 * Private data types
85 */
86 #define SECURITY_LEVEL_NONE 0
87 #if HAVE_LIBGCRYPT
88 # define SECURITY_LEVEL_SIGN 1
89 # define SECURITY_LEVEL_ENCRYPT 2
90 #endif
91 struct sockent_client
92 {
93 int fd;
94 struct sockaddr_storage *addr;
95 socklen_t addrlen;
96 #if HAVE_LIBGCRYPT
97 int security_level;
98 char *username;
99 char *password;
100 gcry_cipher_hd_t cypher;
101 unsigned char password_hash[32];
102 #endif
103 };
105 struct sockent_server
106 {
107 int *fd;
108 size_t fd_num;
109 #if HAVE_LIBGCRYPT
110 int security_level;
111 char *auth_file;
112 fbhash_t *userdb;
113 gcry_cipher_hd_t cypher;
114 #endif
115 };
117 typedef struct sockent
118 {
119 #define SOCKENT_TYPE_CLIENT 1
120 #define SOCKENT_TYPE_SERVER 2
121 int type;
123 char *node;
124 char *service;
125 int interface;
127 union
128 {
129 struct sockent_client client;
130 struct sockent_server server;
131 } data;
133 struct sockent *next;
134 } sockent_t;
136 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
137 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
138 * +-------+-----------------------+-------------------------------+
139 * ! Ver. ! ! Length !
140 * +-------+-----------------------+-------------------------------+
141 */
142 struct part_header_s
143 {
144 uint16_t type;
145 uint16_t length;
146 };
147 typedef struct part_header_s part_header_t;
149 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
150 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
151 * +-------------------------------+-------------------------------+
152 * ! Type ! Length !
153 * +-------------------------------+-------------------------------+
154 * : (Length - 4) Bytes :
155 * +---------------------------------------------------------------+
156 */
157 struct part_string_s
158 {
159 part_header_t *head;
160 char *value;
161 };
162 typedef struct part_string_s part_string_t;
164 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
165 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
166 * +-------------------------------+-------------------------------+
167 * ! Type ! Length !
168 * +-------------------------------+-------------------------------+
169 * : (Length - 4 == 2 || 4 || 8) Bytes :
170 * +---------------------------------------------------------------+
171 */
172 struct part_number_s
173 {
174 part_header_t *head;
175 uint64_t *value;
176 };
177 typedef struct part_number_s part_number_t;
179 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
180 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
181 * +-------------------------------+-------------------------------+
182 * ! Type ! Length !
183 * +-------------------------------+---------------+---------------+
184 * ! Num of values ! Type0 ! Type1 !
185 * +-------------------------------+---------------+---------------+
186 * ! Value0 !
187 * ! !
188 * +---------------------------------------------------------------+
189 * ! Value1 !
190 * ! !
191 * +---------------------------------------------------------------+
192 */
193 struct part_values_s
194 {
195 part_header_t *head;
196 uint16_t *num_values;
197 uint8_t *values_types;
198 value_t *values;
199 };
200 typedef struct part_values_s part_values_t;
202 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
203 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
204 * +-------------------------------+-------------------------------+
205 * ! Type ! Length !
206 * +-------------------------------+-------------------------------+
207 * ! Hash (Bits 0 - 31) !
208 * : : :
209 * ! Hash (Bits 224 - 255) !
210 * +---------------------------------------------------------------+
211 */
212 /* Minimum size */
213 #define PART_SIGNATURE_SHA256_SIZE 36
214 struct part_signature_sha256_s
215 {
216 part_header_t head;
217 unsigned char hash[32];
218 char *username;
219 };
220 typedef struct part_signature_sha256_s part_signature_sha256_t;
222 /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 3 3
223 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
224 * +-------------------------------+-------------------------------+
225 * ! Type ! Length !
226 * +-------------------------------+-------------------------------+
227 * ! Original length ! Padding (0 - 15 bytes) !
228 * +-------------------------------+-------------------------------+
229 * ! Hash (Bits 0 - 31) !
230 * : : :
231 * ! Hash (Bits 128 - 159) !
232 * +---------------------------------------------------------------+
233 */
234 /* Minimum size */
235 #define PART_ENCRYPTION_AES256_SIZE 42
236 struct part_encryption_aes256_s
237 {
238 part_header_t head;
239 uint16_t username_length;
240 char *username;
241 unsigned char iv[16];
242 /* <encrypted> */
243 unsigned char hash[20];
244 /* <payload /> */
245 /* </encrypted> */
246 };
247 typedef struct part_encryption_aes256_s part_encryption_aes256_t;
249 struct receive_list_entry_s
250 {
251 char *data;
252 int data_len;
253 int fd;
254 struct receive_list_entry_s *next;
255 };
256 typedef struct receive_list_entry_s receive_list_entry_t;
258 /*
259 * Private variables
260 */
261 static int network_config_ttl = 0;
262 static size_t network_config_packet_size = 1452;
263 static int network_config_forward = 0;
264 static int network_config_stats = 0;
266 static sockent_t *sending_sockets = NULL;
268 static receive_list_entry_t *receive_list_head = NULL;
269 static receive_list_entry_t *receive_list_tail = NULL;
270 static pthread_mutex_t receive_list_lock = PTHREAD_MUTEX_INITIALIZER;
271 static pthread_cond_t receive_list_cond = PTHREAD_COND_INITIALIZER;
272 static uint64_t receive_list_length = 0;
274 static sockent_t *listen_sockets = NULL;
275 static struct pollfd *listen_sockets_pollfd = NULL;
276 static size_t listen_sockets_num = 0;
278 /* The receive and dispatch threads will run as long as `listen_loop' is set to
279 * zero. */
280 static int listen_loop = 0;
281 static int receive_thread_running = 0;
282 static pthread_t receive_thread_id;
283 static int dispatch_thread_running = 0;
284 static pthread_t dispatch_thread_id;
286 /* Buffer in which to-be-sent network packets are constructed. */
287 static char *send_buffer;
288 static char *send_buffer_ptr;
289 static int send_buffer_fill;
290 static value_list_t send_buffer_vl = VALUE_LIST_STATIC;
291 static pthread_mutex_t send_buffer_lock = PTHREAD_MUTEX_INITIALIZER;
293 /* XXX: These counters are incremented from one place only. The spot in which
294 * the values are incremented is either only reachable by one thread (the
295 * dispatch thread, for example) or locked by some lock (send_buffer_lock for
296 * example). Only if neither is true, the stats_lock is acquired. The counters
297 * are always read without holding a lock in the hope that writing 8 bytes to
298 * memory is an atomic operation. */
299 static derive_t stats_octets_rx = 0;
300 static derive_t stats_octets_tx = 0;
301 static derive_t stats_packets_rx = 0;
302 static derive_t stats_packets_tx = 0;
303 static derive_t stats_values_dispatched = 0;
304 static derive_t stats_values_not_dispatched = 0;
305 static derive_t stats_values_sent = 0;
306 static derive_t stats_values_not_sent = 0;
307 static pthread_mutex_t stats_lock = PTHREAD_MUTEX_INITIALIZER;
309 /*
310 * Private functions
311 */
312 static _Bool check_receive_okay (const value_list_t *vl) /* {{{ */
313 {
314 uint64_t time_sent = 0;
315 int status;
317 status = uc_meta_data_get_unsigned_int (vl,
318 "network:time_sent", &time_sent);
320 /* This is a value we already sent. Don't allow it to be received again in
321 * order to avoid looping. */
322 if ((status == 0) && (time_sent >= ((uint64_t) vl->time)))
323 return (0);
325 return (1);
326 } /* }}} _Bool check_receive_okay */
328 static _Bool check_send_okay (const value_list_t *vl) /* {{{ */
329 {
330 _Bool received = 0;
331 int status;
333 if (network_config_forward != 0)
334 return (1);
336 if (vl->meta == NULL)
337 return (1);
339 status = meta_data_get_boolean (vl->meta, "network:received", &received);
340 if (status == -ENOENT)
341 return (1);
342 else if (status != 0)
343 {
344 ERROR ("network plugin: check_send_okay: meta_data_get_boolean failed "
345 "with status %i.", status);
346 return (1);
347 }
349 /* By default, only *send* value lists that were not *received* by the
350 * network plugin. */
351 return (!received);
352 } /* }}} _Bool check_send_okay */
354 static int network_dispatch_values (value_list_t *vl, /* {{{ */
355 const char *username)
356 {
357 int status;
359 if ((vl->time <= 0)
360 || (strlen (vl->host) <= 0)
361 || (strlen (vl->plugin) <= 0)
362 || (strlen (vl->type) <= 0))
363 return (-EINVAL);
365 if (!check_receive_okay (vl))
366 {
367 #if COLLECT_DEBUG
368 char name[6*DATA_MAX_NAME_LEN];
369 FORMAT_VL (name, sizeof (name), vl);
370 name[sizeof (name) - 1] = 0;
371 DEBUG ("network plugin: network_dispatch_values: "
372 "NOT dispatching %s.", name);
373 #endif
374 stats_values_not_dispatched++;
375 return (0);
376 }
378 assert (vl->meta == NULL);
380 vl->meta = meta_data_create ();
381 if (vl->meta == NULL)
382 {
383 ERROR ("network plugin: meta_data_create failed.");
384 return (-ENOMEM);
385 }
387 status = meta_data_add_boolean (vl->meta, "network:received", 1);
388 if (status != 0)
389 {
390 ERROR ("network plugin: meta_data_add_boolean failed.");
391 meta_data_destroy (vl->meta);
392 vl->meta = NULL;
393 return (status);
394 }
396 if (username != NULL)
397 {
398 status = meta_data_add_string (vl->meta, "network:username", username);
399 if (status != 0)
400 {
401 ERROR ("network plugin: meta_data_add_string failed.");
402 meta_data_destroy (vl->meta);
403 vl->meta = NULL;
404 return (status);
405 }
406 }
408 plugin_dispatch_values (vl);
409 stats_values_dispatched++;
411 meta_data_destroy (vl->meta);
412 vl->meta = NULL;
414 return (0);
415 } /* }}} int network_dispatch_values */
417 #if HAVE_LIBGCRYPT
418 static gcry_cipher_hd_t network_get_aes256_cypher (sockent_t *se, /* {{{ */
419 const void *iv, size_t iv_size, const char *username)
420 {
421 gcry_error_t err;
422 gcry_cipher_hd_t *cyper_ptr;
423 unsigned char password_hash[32];
425 if (se->type == SOCKENT_TYPE_CLIENT)
426 {
427 cyper_ptr = &se->data.client.cypher;
428 memcpy (password_hash, se->data.client.password_hash,
429 sizeof (password_hash));
430 }
431 else
432 {
433 char *secret;
435 cyper_ptr = &se->data.server.cypher;
437 if (username == NULL)
438 return (NULL);
440 secret = fbh_get (se->data.server.userdb, username);
441 if (secret == NULL)
442 return (NULL);
444 gcry_md_hash_buffer (GCRY_MD_SHA256,
445 password_hash,
446 secret, strlen (secret));
448 sfree (secret);
449 }
451 if (*cyper_ptr == NULL)
452 {
453 err = gcry_cipher_open (cyper_ptr,
454 GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_OFB, /* flags = */ 0);
455 if (err != 0)
456 {
457 ERROR ("network plugin: gcry_cipher_open returned: %s",
458 gcry_strerror (err));
459 *cyper_ptr = NULL;
460 return (NULL);
461 }
462 }
463 else
464 {
465 gcry_cipher_reset (*cyper_ptr);
466 }
467 assert (*cyper_ptr != NULL);
469 err = gcry_cipher_setkey (*cyper_ptr,
470 password_hash, sizeof (password_hash));
471 if (err != 0)
472 {
473 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
474 gcry_strerror (err));
475 gcry_cipher_close (*cyper_ptr);
476 *cyper_ptr = NULL;
477 return (NULL);
478 }
480 err = gcry_cipher_setiv (*cyper_ptr, iv, iv_size);
481 if (err != 0)
482 {
483 ERROR ("network plugin: gcry_cipher_setkey returned: %s",
484 gcry_strerror (err));
485 gcry_cipher_close (*cyper_ptr);
486 *cyper_ptr = NULL;
487 return (NULL);
488 }
490 return (*cyper_ptr);
491 } /* }}} int network_get_aes256_cypher */
492 #endif /* HAVE_LIBGCRYPT */
494 static int write_part_values (char **ret_buffer, int *ret_buffer_len,
495 const data_set_t *ds, const value_list_t *vl)
496 {
497 char *packet_ptr;
498 int packet_len;
499 int num_values;
501 part_header_t pkg_ph;
502 uint16_t pkg_num_values;
503 uint8_t *pkg_values_types;
504 value_t *pkg_values;
506 int offset;
507 int i;
509 num_values = vl->values_len;
510 packet_len = sizeof (part_header_t) + sizeof (uint16_t)
511 + (num_values * sizeof (uint8_t))
512 + (num_values * sizeof (value_t));
514 if (*ret_buffer_len < packet_len)
515 return (-1);
517 pkg_values_types = (uint8_t *) malloc (num_values * sizeof (uint8_t));
518 if (pkg_values_types == NULL)
519 {
520 ERROR ("network plugin: write_part_values: malloc failed.");
521 return (-1);
522 }
524 pkg_values = (value_t *) malloc (num_values * sizeof (value_t));
525 if (pkg_values == NULL)
526 {
527 free (pkg_values_types);
528 ERROR ("network plugin: write_part_values: malloc failed.");
529 return (-1);
530 }
532 pkg_ph.type = htons (TYPE_VALUES);
533 pkg_ph.length = htons (packet_len);
535 pkg_num_values = htons ((uint16_t) vl->values_len);
537 for (i = 0; i < num_values; i++)
538 {
539 pkg_values_types[i] = (uint8_t) ds->ds[i].type;
540 switch (ds->ds[i].type)
541 {
542 case DS_TYPE_COUNTER:
543 pkg_values[i].counter = htonll (vl->values[i].counter);
544 break;
546 case DS_TYPE_GAUGE:
547 pkg_values[i].gauge = htond (vl->values[i].gauge);
548 break;
550 case DS_TYPE_DERIVE:
551 pkg_values[i].derive = htonll (vl->values[i].derive);
552 break;
554 case DS_TYPE_ABSOLUTE:
555 pkg_values[i].absolute = htonll (vl->values[i].absolute);
556 break;
558 default:
559 free (pkg_values_types);
560 free (pkg_values);
561 ERROR ("network plugin: write_part_values: "
562 "Unknown data source type: %i",
563 ds->ds[i].type);
564 return (-1);
565 } /* switch (ds->ds[i].type) */
566 } /* for (num_values) */
568 /*
569 * Use `memcpy' to write everything to the buffer, because the pointer
570 * may be unaligned and some architectures, such as SPARC, can't handle
571 * that.
572 */
573 packet_ptr = *ret_buffer;
574 offset = 0;
575 memcpy (packet_ptr + offset, &pkg_ph, sizeof (pkg_ph));
576 offset += sizeof (pkg_ph);
577 memcpy (packet_ptr + offset, &pkg_num_values, sizeof (pkg_num_values));
578 offset += sizeof (pkg_num_values);
579 memcpy (packet_ptr + offset, pkg_values_types, num_values * sizeof (uint8_t));
580 offset += num_values * sizeof (uint8_t);
581 memcpy (packet_ptr + offset, pkg_values, num_values * sizeof (value_t));
582 offset += num_values * sizeof (value_t);
584 assert (offset == packet_len);
586 *ret_buffer = packet_ptr + packet_len;
587 *ret_buffer_len -= packet_len;
589 free (pkg_values_types);
590 free (pkg_values);
592 return (0);
593 } /* int write_part_values */
595 static int write_part_number (char **ret_buffer, int *ret_buffer_len,
596 int type, uint64_t value)
597 {
598 char *packet_ptr;
599 int packet_len;
601 part_header_t pkg_head;
602 uint64_t pkg_value;
604 int offset;
606 packet_len = sizeof (pkg_head) + sizeof (pkg_value);
608 if (*ret_buffer_len < packet_len)
609 return (-1);
611 pkg_head.type = htons (type);
612 pkg_head.length = htons (packet_len);
613 pkg_value = htonll (value);
615 packet_ptr = *ret_buffer;
616 offset = 0;
617 memcpy (packet_ptr + offset, &pkg_head, sizeof (pkg_head));
618 offset += sizeof (pkg_head);
619 memcpy (packet_ptr + offset, &pkg_value, sizeof (pkg_value));
620 offset += sizeof (pkg_value);
622 assert (offset == packet_len);
624 *ret_buffer = packet_ptr + packet_len;
625 *ret_buffer_len -= packet_len;
627 return (0);
628 } /* int write_part_number */
630 static int write_part_string (char **ret_buffer, int *ret_buffer_len,
631 int type, const char *str, int str_len)
632 {
633 char *buffer;
634 int buffer_len;
636 uint16_t pkg_type;
637 uint16_t pkg_length;
639 int offset;
641 buffer_len = 2 * sizeof (uint16_t) + str_len + 1;
642 if (*ret_buffer_len < buffer_len)
643 return (-1);
645 pkg_type = htons (type);
646 pkg_length = htons (buffer_len);
648 buffer = *ret_buffer;
649 offset = 0;
650 memcpy (buffer + offset, (void *) &pkg_type, sizeof (pkg_type));
651 offset += sizeof (pkg_type);
652 memcpy (buffer + offset, (void *) &pkg_length, sizeof (pkg_length));
653 offset += sizeof (pkg_length);
654 memcpy (buffer + offset, str, str_len);
655 offset += str_len;
656 memset (buffer + offset, '\0', 1);
657 offset += 1;
659 assert (offset == buffer_len);
661 *ret_buffer = buffer + buffer_len;
662 *ret_buffer_len -= buffer_len;
664 return (0);
665 } /* int write_part_string */
667 static int parse_part_values (void **ret_buffer, size_t *ret_buffer_len,
668 value_t **ret_values, int *ret_num_values)
669 {
670 char *buffer = *ret_buffer;
671 size_t buffer_len = *ret_buffer_len;
673 uint16_t tmp16;
674 size_t exp_size;
675 int i;
677 uint16_t pkg_length;
678 uint16_t pkg_type;
679 uint16_t pkg_numval;
681 uint8_t *pkg_types;
682 value_t *pkg_values;
684 if (buffer_len < 15)
685 {
686 NOTICE ("network plugin: packet is too short: "
687 "buffer_len = %zu", buffer_len);
688 return (-1);
689 }
691 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
692 buffer += sizeof (tmp16);
693 pkg_type = ntohs (tmp16);
695 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
696 buffer += sizeof (tmp16);
697 pkg_length = ntohs (tmp16);
699 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
700 buffer += sizeof (tmp16);
701 pkg_numval = ntohs (tmp16);
703 assert (pkg_type == TYPE_VALUES);
705 exp_size = 3 * sizeof (uint16_t)
706 + pkg_numval * (sizeof (uint8_t) + sizeof (value_t));
707 if ((buffer_len < 0) || (buffer_len < exp_size))
708 {
709 WARNING ("network plugin: parse_part_values: "
710 "Packet too short: "
711 "Chunk of size %zu expected, "
712 "but buffer has only %zu bytes left.",
713 exp_size, buffer_len);
714 return (-1);
715 }
717 if (pkg_length != exp_size)
718 {
719 WARNING ("network plugin: parse_part_values: "
720 "Length and number of values "
721 "in the packet don't match.");
722 return (-1);
723 }
725 pkg_types = (uint8_t *) malloc (pkg_numval * sizeof (uint8_t));
726 pkg_values = (value_t *) malloc (pkg_numval * sizeof (value_t));
727 if ((pkg_types == NULL) || (pkg_values == NULL))
728 {
729 sfree (pkg_types);
730 sfree (pkg_values);
731 ERROR ("network plugin: parse_part_values: malloc failed.");
732 return (-1);
733 }
735 memcpy ((void *) pkg_types, (void *) buffer, pkg_numval * sizeof (uint8_t));
736 buffer += pkg_numval * sizeof (uint8_t);
737 memcpy ((void *) pkg_values, (void *) buffer, pkg_numval * sizeof (value_t));
738 buffer += pkg_numval * sizeof (value_t);
740 for (i = 0; i < pkg_numval; i++)
741 {
742 switch (pkg_types[i])
743 {
744 case DS_TYPE_COUNTER:
745 pkg_values[i].counter = (counter_t) ntohll (pkg_values[i].counter);
746 break;
748 case DS_TYPE_GAUGE:
749 pkg_values[i].gauge = (gauge_t) ntohd (pkg_values[i].gauge);
750 break;
752 case DS_TYPE_DERIVE:
753 pkg_values[i].derive = (derive_t) ntohll (pkg_values[i].derive);
754 break;
756 case DS_TYPE_ABSOLUTE:
757 pkg_values[i].absolute = (absolute_t) ntohll (pkg_values[i].absolute);
758 break;
760 default:
761 NOTICE ("network plugin: parse_part_values: "
762 "Don't know how to handle data source type %"PRIu8,
763 pkg_types[i]);
764 sfree (pkg_types);
765 sfree (pkg_values);
766 return (-1);
767 } /* switch (pkg_types[i]) */
768 }
770 *ret_buffer = buffer;
771 *ret_buffer_len = buffer_len - pkg_length;
772 *ret_num_values = pkg_numval;
773 *ret_values = pkg_values;
775 sfree (pkg_types);
777 return (0);
778 } /* int parse_part_values */
780 static int parse_part_number (void **ret_buffer, size_t *ret_buffer_len,
781 uint64_t *value)
782 {
783 char *buffer = *ret_buffer;
784 size_t buffer_len = *ret_buffer_len;
786 uint16_t tmp16;
787 uint64_t tmp64;
788 size_t exp_size = 2 * sizeof (uint16_t) + sizeof (uint64_t);
790 uint16_t pkg_length;
791 uint16_t pkg_type;
793 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
794 {
795 WARNING ("network plugin: parse_part_number: "
796 "Packet too short: "
797 "Chunk of size %zu expected, "
798 "but buffer has only %zu bytes left.",
799 exp_size, buffer_len);
800 return (-1);
801 }
803 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
804 buffer += sizeof (tmp16);
805 pkg_type = ntohs (tmp16);
807 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
808 buffer += sizeof (tmp16);
809 pkg_length = ntohs (tmp16);
811 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
812 buffer += sizeof (tmp64);
813 *value = ntohll (tmp64);
815 *ret_buffer = buffer;
816 *ret_buffer_len = buffer_len - pkg_length;
818 return (0);
819 } /* int parse_part_number */
821 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
822 char *output, int output_len)
823 {
824 char *buffer = *ret_buffer;
825 size_t buffer_len = *ret_buffer_len;
827 uint16_t tmp16;
828 size_t header_size = 2 * sizeof (uint16_t);
830 uint16_t pkg_length;
831 uint16_t pkg_type;
833 if ((buffer_len < 0) || (buffer_len < header_size))
834 {
835 WARNING ("network plugin: parse_part_string: "
836 "Packet too short: "
837 "Chunk of at least size %zu expected, "
838 "but buffer has only %zu bytes left.",
839 header_size, buffer_len);
840 return (-1);
841 }
843 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
844 buffer += sizeof (tmp16);
845 pkg_type = ntohs (tmp16);
847 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
848 buffer += sizeof (tmp16);
849 pkg_length = ntohs (tmp16);
851 /* Check that packet fits in the input buffer */
852 if (pkg_length > buffer_len)
853 {
854 WARNING ("network plugin: parse_part_string: "
855 "Packet too big: "
856 "Chunk of size %"PRIu16" received, "
857 "but buffer has only %zu bytes left.",
858 pkg_length, buffer_len);
859 return (-1);
860 }
862 /* Check that pkg_length is in the valid range */
863 if (pkg_length <= header_size)
864 {
865 WARNING ("network plugin: parse_part_string: "
866 "Packet too short: "
867 "Header claims this packet is only %hu "
868 "bytes long.", pkg_length);
869 return (-1);
870 }
872 /* Check that the package data fits into the output buffer.
873 * The previous if-statement ensures that:
874 * `pkg_length > header_size' */
875 if ((output_len < 0)
876 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
877 {
878 WARNING ("network plugin: parse_part_string: "
879 "Output buffer too small.");
880 return (-1);
881 }
883 /* All sanity checks successfull, let's copy the data over */
884 output_len = pkg_length - header_size;
885 memcpy ((void *) output, (void *) buffer, output_len);
886 buffer += output_len;
888 /* For some very weird reason '\0' doesn't do the trick on SPARC in
889 * this statement. */
890 if (output[output_len - 1] != 0)
891 {
892 WARNING ("network plugin: parse_part_string: "
893 "Received string does not end "
894 "with a NULL-byte.");
895 return (-1);
896 }
898 *ret_buffer = buffer;
899 *ret_buffer_len = buffer_len - pkg_length;
901 return (0);
902 } /* int parse_part_string */
904 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
905 * parse_packet and vice versa. */
906 #define PP_SIGNED 0x01
907 #define PP_ENCRYPTED 0x02
908 static int parse_packet (sockent_t *se,
909 void *buffer, size_t buffer_size, int flags,
910 const char *username);
912 #define BUFFER_READ(p,s) do { \
913 memcpy ((p), buffer + buffer_offset, (s)); \
914 buffer_offset += (s); \
915 } while (0)
917 #if HAVE_LIBGCRYPT
918 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
919 void **ret_buffer, size_t *ret_buffer_len, int flags)
920 {
921 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
923 char *buffer;
924 size_t buffer_len;
925 size_t buffer_offset;
927 size_t username_len;
928 char *secret;
930 part_signature_sha256_t pss;
931 uint16_t pss_head_length;
932 char hash[sizeof (pss.hash)];
934 gcry_md_hd_t hd;
935 gcry_error_t err;
936 unsigned char *hash_ptr;
938 buffer = *ret_buffer;
939 buffer_len = *ret_buffer_len;
940 buffer_offset = 0;
942 if (se->data.server.userdb == NULL)
943 {
944 c_complain (LOG_NOTICE, &complain_no_users,
945 "network plugin: Received signed network packet but can't verify it "
946 "because no user DB has been configured. Will accept it.");
947 return (0);
948 }
950 /* Check if the buffer has enough data for this structure. */
951 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
952 return (-ENOMEM);
954 /* Read type and length header */
955 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
956 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
957 pss_head_length = ntohs (pss.head.length);
959 /* Check if the `pss_head_length' is within bounds. */
960 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
961 || (pss_head_length > buffer_len))
962 {
963 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
964 return (-1);
965 }
967 /* Copy the hash. */
968 BUFFER_READ (pss.hash, sizeof (pss.hash));
970 /* Calculate username length (without null byte) and allocate memory */
971 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
972 pss.username = malloc (username_len + 1);
973 if (pss.username == NULL)
974 return (-ENOMEM);
976 /* Read the username */
977 BUFFER_READ (pss.username, username_len);
978 pss.username[username_len] = 0;
980 assert (buffer_offset == pss_head_length);
982 /* Query the password */
983 secret = fbh_get (se->data.server.userdb, pss.username);
984 if (secret == NULL)
985 {
986 ERROR ("network plugin: Unknown user: %s", pss.username);
987 sfree (pss.username);
988 return (-ENOENT);
989 }
991 /* Create a hash device and check the HMAC */
992 hd = NULL;
993 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
994 if (err != 0)
995 {
996 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
997 gcry_strerror (err));
998 sfree (secret);
999 sfree (pss.username);
1000 return (-1);
1001 }
1003 err = gcry_md_setkey (hd, secret, strlen (secret));
1004 if (err != 0)
1005 {
1006 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1007 gcry_md_close (hd);
1008 sfree (secret);
1009 sfree (pss.username);
1010 return (-1);
1011 }
1013 gcry_md_write (hd,
1014 buffer + PART_SIGNATURE_SHA256_SIZE,
1015 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1016 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1017 if (hash_ptr == NULL)
1018 {
1019 ERROR ("network plugin: gcry_md_read failed.");
1020 gcry_md_close (hd);
1021 sfree (secret);
1022 sfree (pss.username);
1023 return (-1);
1024 }
1025 memcpy (hash, hash_ptr, sizeof (hash));
1027 /* Clean up */
1028 gcry_md_close (hd);
1029 hd = NULL;
1031 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1032 {
1033 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1034 "Hash mismatch.");
1035 }
1036 else
1037 {
1038 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1039 flags | PP_SIGNED, pss.username);
1040 }
1042 sfree (secret);
1043 sfree (pss.username);
1045 *ret_buffer = buffer + buffer_len;
1046 *ret_buffer_len = 0;
1048 return (0);
1049 } /* }}} int parse_part_sign_sha256 */
1050 /* #endif HAVE_LIBGCRYPT */
1052 #else /* if !HAVE_LIBGCRYPT */
1053 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1054 void **ret_buffer, size_t *ret_buffer_size, int flags)
1055 {
1056 static int warning_has_been_printed = 0;
1058 char *buffer;
1059 size_t buffer_size;
1060 size_t buffer_offset;
1061 uint16_t part_len;
1063 part_signature_sha256_t pss;
1065 buffer = *ret_buffer;
1066 buffer_size = *ret_buffer_size;
1067 buffer_offset = 0;
1069 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1070 return (-ENOMEM);
1072 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1073 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1074 part_len = ntohs (pss.head.length);
1076 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1077 || (part_len > buffer_size))
1078 return (-EINVAL);
1080 if (warning_has_been_printed == 0)
1081 {
1082 WARNING ("network plugin: Received signed packet, but the network "
1083 "plugin was not linked with libgcrypt, so I cannot "
1084 "verify the signature. The packet will be accepted.");
1085 warning_has_been_printed = 1;
1086 }
1088 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1089 /* username = */ NULL);
1091 *ret_buffer = buffer + buffer_size;
1092 *ret_buffer_size = 0;
1094 return (0);
1095 } /* }}} int parse_part_sign_sha256 */
1096 #endif /* !HAVE_LIBGCRYPT */
1098 #if HAVE_LIBGCRYPT
1099 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1100 void **ret_buffer, size_t *ret_buffer_len,
1101 int flags)
1102 {
1103 char *buffer = *ret_buffer;
1104 size_t buffer_len = *ret_buffer_len;
1105 size_t payload_len;
1106 size_t part_size;
1107 size_t buffer_offset;
1108 uint16_t username_len;
1109 part_encryption_aes256_t pea;
1110 unsigned char hash[sizeof (pea.hash)];
1112 gcry_cipher_hd_t cypher;
1113 gcry_error_t err;
1115 /* Make sure at least the header if available. */
1116 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1117 {
1118 NOTICE ("network plugin: parse_part_encr_aes256: "
1119 "Discarding short packet.");
1120 return (-1);
1121 }
1123 buffer_offset = 0;
1125 /* Copy the unencrypted information into `pea'. */
1126 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1127 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1129 /* Check the `part size'. */
1130 part_size = ntohs (pea.head.length);
1131 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1132 || (part_size > buffer_len))
1133 {
1134 NOTICE ("network plugin: parse_part_encr_aes256: "
1135 "Discarding part with invalid size.");
1136 return (-1);
1137 }
1139 /* Read the username */
1140 BUFFER_READ (&username_len, sizeof (username_len));
1141 username_len = ntohs (username_len);
1143 if ((username_len <= 0)
1144 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1145 {
1146 NOTICE ("network plugin: parse_part_encr_aes256: "
1147 "Discarding part with invalid username length.");
1148 return (-1);
1149 }
1151 assert (username_len > 0);
1152 pea.username = malloc (username_len + 1);
1153 if (pea.username == NULL)
1154 return (-ENOMEM);
1155 BUFFER_READ (pea.username, username_len);
1156 pea.username[username_len] = 0;
1158 /* Last but not least, the initialization vector */
1159 BUFFER_READ (pea.iv, sizeof (pea.iv));
1161 /* Make sure we are at the right position */
1162 assert (buffer_offset == (username_len +
1163 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1165 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1166 pea.username);
1167 if (cypher == NULL)
1168 {
1169 sfree (pea.username);
1170 return (-1);
1171 }
1173 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1174 assert (payload_len > 0);
1176 /* Decrypt the packet in-place */
1177 err = gcry_cipher_decrypt (cypher,
1178 buffer + buffer_offset,
1179 part_size - buffer_offset,
1180 /* in = */ NULL, /* in len = */ 0);
1181 if (err != 0)
1182 {
1183 sfree (pea.username);
1184 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1185 gcry_strerror (err));
1186 return (-1);
1187 }
1189 /* Read the hash */
1190 BUFFER_READ (pea.hash, sizeof (pea.hash));
1192 /* Make sure we're at the right position - again */
1193 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1194 assert (buffer_offset == (part_size - payload_len));
1196 /* Check hash sum */
1197 memset (hash, 0, sizeof (hash));
1198 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1199 buffer + buffer_offset, payload_len);
1200 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1201 {
1202 sfree (pea.username);
1203 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1204 return (-1);
1205 }
1207 parse_packet (se, buffer + buffer_offset, payload_len,
1208 flags | PP_ENCRYPTED, pea.username);
1210 /* XXX: Free pea.username?!? */
1212 /* Update return values */
1213 *ret_buffer = buffer + part_size;
1214 *ret_buffer_len = buffer_len - part_size;
1216 sfree (pea.username);
1218 return (0);
1219 } /* }}} int parse_part_encr_aes256 */
1220 /* #endif HAVE_LIBGCRYPT */
1222 #else /* if !HAVE_LIBGCRYPT */
1223 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1224 void **ret_buffer, size_t *ret_buffer_size, int flags)
1225 {
1226 static int warning_has_been_printed = 0;
1228 char *buffer;
1229 size_t buffer_size;
1230 size_t buffer_offset;
1232 part_header_t ph;
1233 size_t ph_length;
1235 buffer = *ret_buffer;
1236 buffer_size = *ret_buffer_size;
1237 buffer_offset = 0;
1239 /* parse_packet assures this minimum size. */
1240 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1242 BUFFER_READ (&ph.type, sizeof (ph.type));
1243 BUFFER_READ (&ph.length, sizeof (ph.length));
1244 ph_length = ntohs (ph.length);
1246 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1247 || (ph_length > buffer_size))
1248 {
1249 ERROR ("network plugin: AES-256 encrypted part "
1250 "with invalid length received.");
1251 return (-1);
1252 }
1254 if (warning_has_been_printed == 0)
1255 {
1256 WARNING ("network plugin: Received encrypted packet, but the network "
1257 "plugin was not linked with libgcrypt, so I cannot "
1258 "decrypt it. The part will be discarded.");
1259 warning_has_been_printed = 1;
1260 }
1262 *ret_buffer += ph_length;
1263 *ret_buffer_size -= ph_length;
1265 return (0);
1266 } /* }}} int parse_part_encr_aes256 */
1267 #endif /* !HAVE_LIBGCRYPT */
1269 #undef BUFFER_READ
1271 static int parse_packet (sockent_t *se, /* {{{ */
1272 void *buffer, size_t buffer_size, int flags,
1273 const char *username)
1274 {
1275 int status;
1277 value_list_t vl = VALUE_LIST_INIT;
1278 notification_t n;
1280 #if HAVE_LIBGCRYPT
1281 int packet_was_signed = (flags & PP_SIGNED);
1282 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1283 int printed_ignore_warning = 0;
1284 #endif /* HAVE_LIBGCRYPT */
1287 memset (&vl, '\0', sizeof (vl));
1288 memset (&n, '\0', sizeof (n));
1289 status = 0;
1291 while ((status == 0) && (0 < buffer_size)
1292 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1293 {
1294 uint16_t pkg_length;
1295 uint16_t pkg_type;
1297 memcpy ((void *) &pkg_type,
1298 (void *) buffer,
1299 sizeof (pkg_type));
1300 memcpy ((void *) &pkg_length,
1301 (void *) (buffer + sizeof (pkg_type)),
1302 sizeof (pkg_length));
1304 pkg_length = ntohs (pkg_length);
1305 pkg_type = ntohs (pkg_type);
1307 if (pkg_length > buffer_size)
1308 break;
1309 /* Ensure that this loop terminates eventually */
1310 if (pkg_length < (2 * sizeof (uint16_t)))
1311 break;
1313 if (pkg_type == TYPE_ENCR_AES256)
1314 {
1315 status = parse_part_encr_aes256 (se,
1316 &buffer, &buffer_size, flags);
1317 if (status != 0)
1318 {
1319 ERROR ("network plugin: Decrypting AES256 "
1320 "part failed "
1321 "with status %i.", status);
1322 break;
1323 }
1324 }
1325 #if HAVE_LIBGCRYPT
1326 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1327 && (packet_was_encrypted == 0))
1328 {
1329 if (printed_ignore_warning == 0)
1330 {
1331 INFO ("network plugin: Unencrypted packet or "
1332 "part has been ignored.");
1333 printed_ignore_warning = 1;
1334 }
1335 buffer = ((char *) buffer) + pkg_length;
1336 continue;
1337 }
1338 #endif /* HAVE_LIBGCRYPT */
1339 else if (pkg_type == TYPE_SIGN_SHA256)
1340 {
1341 status = parse_part_sign_sha256 (se,
1342 &buffer, &buffer_size, flags);
1343 if (status != 0)
1344 {
1345 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1346 "signature failed "
1347 "with status %i.", status);
1348 break;
1349 }
1350 }
1351 #if HAVE_LIBGCRYPT
1352 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1353 && (packet_was_encrypted == 0)
1354 && (packet_was_signed == 0))
1355 {
1356 if (printed_ignore_warning == 0)
1357 {
1358 INFO ("network plugin: Unsigned packet or "
1359 "part has been ignored.");
1360 printed_ignore_warning = 1;
1361 }
1362 buffer = ((char *) buffer) + pkg_length;
1363 continue;
1364 }
1365 #endif /* HAVE_LIBGCRYPT */
1366 else if (pkg_type == TYPE_VALUES)
1367 {
1368 status = parse_part_values (&buffer, &buffer_size,
1369 &vl.values, &vl.values_len);
1370 if (status != 0)
1371 break;
1373 network_dispatch_values (&vl, username);
1375 sfree (vl.values);
1376 }
1377 else if (pkg_type == TYPE_TIME)
1378 {
1379 uint64_t tmp = 0;
1380 status = parse_part_number (&buffer, &buffer_size,
1381 &tmp);
1382 if (status == 0)
1383 {
1384 vl.time = TIME_T_TO_CDTIME_T (tmp);
1385 n.time = TIME_T_TO_CDTIME_T (tmp);
1386 }
1387 }
1388 else if (pkg_type == TYPE_TIME_HR)
1389 {
1390 uint64_t tmp = 0;
1391 status = parse_part_number (&buffer, &buffer_size,
1392 &tmp);
1393 if (status == 0)
1394 {
1395 vl.time = (cdtime_t) tmp;
1396 n.time = (cdtime_t) tmp;
1397 }
1398 }
1399 else if (pkg_type == TYPE_INTERVAL)
1400 {
1401 uint64_t tmp = 0;
1402 status = parse_part_number (&buffer, &buffer_size,
1403 &tmp);
1404 if (status == 0)
1405 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1406 }
1407 else if (pkg_type == TYPE_INTERVAL_HR)
1408 {
1409 uint64_t tmp = 0;
1410 status = parse_part_number (&buffer, &buffer_size,
1411 &tmp);
1412 if (status == 0)
1413 vl.interval = (cdtime_t) tmp;
1414 }
1415 else if (pkg_type == TYPE_HOST)
1416 {
1417 status = parse_part_string (&buffer, &buffer_size,
1418 vl.host, sizeof (vl.host));
1419 if (status == 0)
1420 sstrncpy (n.host, vl.host, sizeof (n.host));
1421 }
1422 else if (pkg_type == TYPE_PLUGIN)
1423 {
1424 status = parse_part_string (&buffer, &buffer_size,
1425 vl.plugin, sizeof (vl.plugin));
1426 if (status == 0)
1427 sstrncpy (n.plugin, vl.plugin,
1428 sizeof (n.plugin));
1429 }
1430 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1431 {
1432 status = parse_part_string (&buffer, &buffer_size,
1433 vl.plugin_instance,
1434 sizeof (vl.plugin_instance));
1435 if (status == 0)
1436 sstrncpy (n.plugin_instance,
1437 vl.plugin_instance,
1438 sizeof (n.plugin_instance));
1439 }
1440 else if (pkg_type == TYPE_TYPE)
1441 {
1442 status = parse_part_string (&buffer, &buffer_size,
1443 vl.type, sizeof (vl.type));
1444 if (status == 0)
1445 sstrncpy (n.type, vl.type, sizeof (n.type));
1446 }
1447 else if (pkg_type == TYPE_TYPE_INSTANCE)
1448 {
1449 status = parse_part_string (&buffer, &buffer_size,
1450 vl.type_instance,
1451 sizeof (vl.type_instance));
1452 if (status == 0)
1453 sstrncpy (n.type_instance, vl.type_instance,
1454 sizeof (n.type_instance));
1455 }
1456 else if (pkg_type == TYPE_MESSAGE)
1457 {
1458 status = parse_part_string (&buffer, &buffer_size,
1459 n.message, sizeof (n.message));
1461 if (status != 0)
1462 {
1463 /* do nothing */
1464 }
1465 else if ((n.severity != NOTIF_FAILURE)
1466 && (n.severity != NOTIF_WARNING)
1467 && (n.severity != NOTIF_OKAY))
1468 {
1469 INFO ("network plugin: "
1470 "Ignoring notification with "
1471 "unknown severity %i.",
1472 n.severity);
1473 }
1474 else if (n.time <= 0)
1475 {
1476 INFO ("network plugin: "
1477 "Ignoring notification with "
1478 "time == 0.");
1479 }
1480 else if (strlen (n.message) <= 0)
1481 {
1482 INFO ("network plugin: "
1483 "Ignoring notification with "
1484 "an empty message.");
1485 }
1486 else
1487 {
1488 plugin_dispatch_notification (&n);
1489 }
1490 }
1491 else if (pkg_type == TYPE_SEVERITY)
1492 {
1493 uint64_t tmp = 0;
1494 status = parse_part_number (&buffer, &buffer_size,
1495 &tmp);
1496 if (status == 0)
1497 n.severity = (int) tmp;
1498 }
1499 else
1500 {
1501 DEBUG ("network plugin: parse_packet: Unknown part"
1502 " type: 0x%04hx", pkg_type);
1503 buffer = ((char *) buffer) + pkg_length;
1504 }
1505 } /* while (buffer_size > sizeof (part_header_t)) */
1507 if (status == 0 && buffer_size > 0)
1508 WARNING ("network plugin: parse_packet: Received truncated "
1509 "packet, try increasing `MaxPacketSize'");
1511 return (status);
1512 } /* }}} int parse_packet */
1514 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1515 {
1516 if (sec->fd >= 0)
1517 {
1518 close (sec->fd);
1519 sec->fd = -1;
1520 }
1521 sfree (sec->addr);
1522 #if HAVE_LIBGCRYPT
1523 sfree (sec->username);
1524 sfree (sec->password);
1525 if (sec->cypher != NULL)
1526 gcry_cipher_close (sec->cypher);
1527 #endif
1528 } /* }}} void free_sockent_client */
1530 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1531 {
1532 size_t i;
1534 for (i = 0; i < ses->fd_num; i++)
1535 {
1536 if (ses->fd[i] >= 0)
1537 {
1538 close (ses->fd[i]);
1539 ses->fd[i] = -1;
1540 }
1541 }
1543 sfree (ses->fd);
1544 #if HAVE_LIBGCRYPT
1545 sfree (ses->auth_file);
1546 fbh_destroy (ses->userdb);
1547 if (ses->cypher != NULL)
1548 gcry_cipher_close (ses->cypher);
1549 #endif
1550 } /* }}} void free_sockent_server */
1552 static void sockent_destroy (sockent_t *se) /* {{{ */
1553 {
1554 sockent_t *next;
1556 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1558 while (se != NULL)
1559 {
1560 next = se->next;
1562 sfree (se->node);
1563 sfree (se->service);
1565 if (se->type == SOCKENT_TYPE_CLIENT)
1566 free_sockent_client (&se->data.client);
1567 else
1568 free_sockent_server (&se->data.server);
1570 sfree (se);
1571 se = next;
1572 }
1573 } /* }}} void sockent_destroy */
1575 /*
1576 * int network_set_ttl
1577 *
1578 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1579 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1580 *
1581 * The `struct addrinfo' is used to destinguish between unicast and multicast
1582 * sockets.
1583 */
1584 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1585 {
1586 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1587 network_config_ttl);
1589 assert (se->type == SOCKENT_TYPE_CLIENT);
1591 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1592 return (-1);
1594 if (ai->ai_family == AF_INET)
1595 {
1596 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1597 int optname;
1599 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1600 optname = IP_MULTICAST_TTL;
1601 else
1602 optname = IP_TTL;
1604 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1605 &network_config_ttl,
1606 sizeof (network_config_ttl)) != 0)
1607 {
1608 char errbuf[1024];
1609 ERROR ("setsockopt: %s",
1610 sstrerror (errno, errbuf, sizeof (errbuf)));
1611 return (-1);
1612 }
1613 }
1614 else if (ai->ai_family == AF_INET6)
1615 {
1616 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1617 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1618 int optname;
1620 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1621 optname = IPV6_MULTICAST_HOPS;
1622 else
1623 optname = IPV6_UNICAST_HOPS;
1625 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1626 &network_config_ttl,
1627 sizeof (network_config_ttl)) != 0)
1628 {
1629 char errbuf[1024];
1630 ERROR ("setsockopt: %s",
1631 sstrerror (errno, errbuf,
1632 sizeof (errbuf)));
1633 return (-1);
1634 }
1635 }
1637 return (0);
1638 } /* int network_set_ttl */
1640 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1641 {
1642 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1643 se->interface);
1645 assert (se->type == SOCKENT_TYPE_CLIENT);
1647 if (ai->ai_family == AF_INET)
1648 {
1649 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1651 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1652 {
1653 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1654 /* If possible, use the "ip_mreqn" structure which has
1655 * an "interface index" member. Using the interface
1656 * index is preferred here, because of its similarity
1657 * to the way IPv6 handles this. Unfortunately, it
1658 * appears not to be portable. */
1659 struct ip_mreqn mreq;
1661 memset (&mreq, 0, sizeof (mreq));
1662 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1663 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1664 mreq.imr_ifindex = se->interface;
1665 #else
1666 struct ip_mreq mreq;
1668 memset (&mreq, 0, sizeof (mreq));
1669 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1670 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1671 #endif
1673 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1674 &mreq, sizeof (mreq)) != 0)
1675 {
1676 char errbuf[1024];
1677 ERROR ("setsockopt: %s",
1678 sstrerror (errno, errbuf, sizeof (errbuf)));
1679 return (-1);
1680 }
1682 return (0);
1683 }
1684 }
1685 else if (ai->ai_family == AF_INET6)
1686 {
1687 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1689 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1690 {
1691 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1692 &se->interface,
1693 sizeof (se->interface)) != 0)
1694 {
1695 char errbuf[1024];
1696 ERROR ("setsockopt: %s",
1697 sstrerror (errno, errbuf,
1698 sizeof (errbuf)));
1699 return (-1);
1700 }
1702 return (0);
1703 }
1704 }
1706 /* else: Not a multicast interface. */
1707 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1708 if (se->interface != 0)
1709 {
1710 char interface_name[IFNAMSIZ];
1712 if (if_indextoname (se->interface, interface_name) == NULL)
1713 return (-1);
1715 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1717 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1718 interface_name,
1719 sizeof(interface_name)) == -1 )
1720 {
1721 char errbuf[1024];
1722 ERROR ("setsockopt: %s",
1723 sstrerror (errno, errbuf, sizeof (errbuf)));
1724 return (-1);
1725 }
1726 }
1727 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1729 #else
1730 WARNING ("network plugin: Cannot set the interface on a unicast "
1731 "socket because "
1732 # if !defined(SO_BINDTODEVICE)
1733 "the the \"SO_BINDTODEVICE\" socket option "
1734 # else
1735 "the \"if_indextoname\" function "
1736 # endif
1737 "is not available on your system.");
1738 #endif
1740 return (0);
1741 } /* }}} network_set_interface */
1743 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1744 {
1745 int loop = 0;
1746 int yes = 1;
1748 /* allow multiple sockets to use the same PORT number */
1749 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1750 &yes, sizeof(yes)) == -1) {
1751 char errbuf[1024];
1752 ERROR ("setsockopt: %s",
1753 sstrerror (errno, errbuf, sizeof (errbuf)));
1754 return (-1);
1755 }
1757 DEBUG ("fd = %i; calling `bind'", fd);
1759 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1760 {
1761 char errbuf[1024];
1762 ERROR ("bind: %s",
1763 sstrerror (errno, errbuf, sizeof (errbuf)));
1764 return (-1);
1765 }
1767 if (ai->ai_family == AF_INET)
1768 {
1769 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1770 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1771 {
1772 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1773 struct ip_mreqn mreq;
1774 #else
1775 struct ip_mreq mreq;
1776 #endif
1778 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1780 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1781 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1782 /* Set the interface using the interface index if
1783 * possible (available). Unfortunately, the struct
1784 * ip_mreqn is not portable. */
1785 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1786 mreq.imr_ifindex = interface_idx;
1787 #else
1788 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1789 #endif
1791 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1792 &loop, sizeof (loop)) == -1)
1793 {
1794 char errbuf[1024];
1795 ERROR ("setsockopt: %s",
1796 sstrerror (errno, errbuf,
1797 sizeof (errbuf)));
1798 return (-1);
1799 }
1801 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1802 &mreq, sizeof (mreq)) == -1)
1803 {
1804 char errbuf[1024];
1805 ERROR ("setsockopt: %s",
1806 sstrerror (errno, errbuf,
1807 sizeof (errbuf)));
1808 return (-1);
1809 }
1811 return (0);
1812 }
1813 }
1814 else if (ai->ai_family == AF_INET6)
1815 {
1816 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1817 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1818 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1819 {
1820 struct ipv6_mreq mreq;
1822 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1824 memcpy (&mreq.ipv6mr_multiaddr,
1825 &addr->sin6_addr,
1826 sizeof (addr->sin6_addr));
1828 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1829 * ipv6mr_interface may be set to zeroes to
1830 * choose the default multicast interface or to
1831 * the index of a particular multicast-capable
1832 * interface if the host is multihomed.
1833 * Membership is associ-associated with a
1834 * single interface; programs running on
1835 * multihomed hosts may need to join the same
1836 * group on more than one interface.*/
1837 mreq.ipv6mr_interface = interface_idx;
1839 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1840 &loop, sizeof (loop)) == -1)
1841 {
1842 char errbuf[1024];
1843 ERROR ("setsockopt: %s",
1844 sstrerror (errno, errbuf,
1845 sizeof (errbuf)));
1846 return (-1);
1847 }
1849 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1850 &mreq, sizeof (mreq)) == -1)
1851 {
1852 char errbuf[1024];
1853 ERROR ("setsockopt: %s",
1854 sstrerror (errno, errbuf,
1855 sizeof (errbuf)));
1856 return (-1);
1857 }
1859 return (0);
1860 }
1861 }
1863 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1864 /* if a specific interface was set, bind the socket to it. But to avoid
1865 * possible problems with multicast routing, only do that for non-multicast
1866 * addresses */
1867 if (interface_idx != 0)
1868 {
1869 char interface_name[IFNAMSIZ];
1871 if (if_indextoname (interface_idx, interface_name) == NULL)
1872 return (-1);
1874 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1876 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1877 interface_name,
1878 sizeof(interface_name)) == -1 )
1879 {
1880 char errbuf[1024];
1881 ERROR ("setsockopt: %s",
1882 sstrerror (errno, errbuf, sizeof (errbuf)));
1883 return (-1);
1884 }
1885 }
1886 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1888 return (0);
1889 } /* int network_bind_socket */
1891 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1892 * or `SOCKENT_TYPE_SERVER' */
1893 static int sockent_init (sockent_t *se, int type) /* {{{ */
1894 {
1895 if (se == NULL)
1896 return (-1);
1898 memset (se, 0, sizeof (*se));
1900 se->type = SOCKENT_TYPE_CLIENT;
1901 se->node = NULL;
1902 se->service = NULL;
1903 se->interface = 0;
1904 se->next = NULL;
1906 if (type == SOCKENT_TYPE_SERVER)
1907 {
1908 se->type = SOCKENT_TYPE_SERVER;
1909 se->data.server.fd = NULL;
1910 #if HAVE_LIBGCRYPT
1911 se->data.server.security_level = SECURITY_LEVEL_NONE;
1912 se->data.server.auth_file = NULL;
1913 se->data.server.userdb = NULL;
1914 se->data.server.cypher = NULL;
1915 #endif
1916 }
1917 else
1918 {
1919 se->data.client.fd = -1;
1920 se->data.client.addr = NULL;
1921 #if HAVE_LIBGCRYPT
1922 se->data.client.security_level = SECURITY_LEVEL_NONE;
1923 se->data.client.username = NULL;
1924 se->data.client.password = NULL;
1925 se->data.client.cypher = NULL;
1926 #endif
1927 }
1929 return (0);
1930 } /* }}} int sockent_init */
1932 /* Open the file descriptors for a initialized sockent structure. */
1933 static int sockent_open (sockent_t *se) /* {{{ */
1934 {
1935 struct addrinfo ai_hints;
1936 struct addrinfo *ai_list, *ai_ptr;
1937 int ai_return;
1939 const char *node;
1940 const char *service;
1942 if (se == NULL)
1943 return (-1);
1945 /* Set up the security structures. */
1946 #if HAVE_LIBGCRYPT /* {{{ */
1947 if (se->type == SOCKENT_TYPE_CLIENT)
1948 {
1949 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1950 {
1951 if ((se->data.client.username == NULL)
1952 || (se->data.client.password == NULL))
1953 {
1954 ERROR ("network plugin: Client socket with "
1955 "security requested, but no "
1956 "credentials are configured.");
1957 return (-1);
1958 }
1959 gcry_md_hash_buffer (GCRY_MD_SHA256,
1960 se->data.client.password_hash,
1961 se->data.client.password,
1962 strlen (se->data.client.password));
1963 }
1964 }
1965 else /* (se->type == SOCKENT_TYPE_SERVER) */
1966 {
1967 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1968 {
1969 if (se->data.server.auth_file == NULL)
1970 {
1971 ERROR ("network plugin: Server socket with "
1972 "security requested, but no "
1973 "password file is configured.");
1974 return (-1);
1975 }
1976 }
1977 if (se->data.server.auth_file != NULL)
1978 {
1979 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1980 if (se->data.server.userdb == NULL)
1981 {
1982 ERROR ("network plugin: Reading password file "
1983 "`%s' failed.",
1984 se->data.server.auth_file);
1985 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1986 return (-1);
1987 }
1988 }
1989 }
1990 #endif /* }}} HAVE_LIBGCRYPT */
1992 node = se->node;
1993 service = se->service;
1995 if (service == NULL)
1996 service = NET_DEFAULT_PORT;
1998 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1999 node, service);
2001 memset (&ai_hints, 0, sizeof (ai_hints));
2002 ai_hints.ai_flags = 0;
2003 #ifdef AI_PASSIVE
2004 ai_hints.ai_flags |= AI_PASSIVE;
2005 #endif
2006 #ifdef AI_ADDRCONFIG
2007 ai_hints.ai_flags |= AI_ADDRCONFIG;
2008 #endif
2009 ai_hints.ai_family = AF_UNSPEC;
2010 ai_hints.ai_socktype = SOCK_DGRAM;
2011 ai_hints.ai_protocol = IPPROTO_UDP;
2013 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
2014 if (ai_return != 0)
2015 {
2016 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2017 (se->node == NULL) ? "(null)" : se->node,
2018 (se->service == NULL) ? "(null)" : se->service,
2019 gai_strerror (ai_return));
2020 return (-1);
2021 }
2023 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2024 {
2025 int status;
2027 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
2028 {
2029 int *tmp;
2031 tmp = realloc (se->data.server.fd,
2032 sizeof (*tmp) * (se->data.server.fd_num + 1));
2033 if (tmp == NULL)
2034 {
2035 ERROR ("network plugin: realloc failed.");
2036 continue;
2037 }
2038 se->data.server.fd = tmp;
2039 tmp = se->data.server.fd + se->data.server.fd_num;
2041 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2042 ai_ptr->ai_protocol);
2043 if (*tmp < 0)
2044 {
2045 char errbuf[1024];
2046 ERROR ("network plugin: socket(2) failed: %s",
2047 sstrerror (errno, errbuf,
2048 sizeof (errbuf)));
2049 continue;
2050 }
2052 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2053 if (status != 0)
2054 {
2055 close (*tmp);
2056 *tmp = -1;
2057 continue;
2058 }
2060 se->data.server.fd_num++;
2061 continue;
2062 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
2063 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
2064 {
2065 se->data.client.fd = socket (ai_ptr->ai_family,
2066 ai_ptr->ai_socktype,
2067 ai_ptr->ai_protocol);
2068 if (se->data.client.fd < 0)
2069 {
2070 char errbuf[1024];
2071 ERROR ("network plugin: socket(2) failed: %s",
2072 sstrerror (errno, errbuf,
2073 sizeof (errbuf)));
2074 continue;
2075 }
2077 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
2078 if (se->data.client.addr == NULL)
2079 {
2080 ERROR ("network plugin: malloc failed.");
2081 close (se->data.client.fd);
2082 se->data.client.fd = -1;
2083 continue;
2084 }
2086 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
2087 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
2088 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2089 se->data.client.addrlen = ai_ptr->ai_addrlen;
2091 network_set_ttl (se, ai_ptr);
2092 network_set_interface (se, ai_ptr);
2094 /* We don't open more than one write-socket per
2095 * node/service pair.. */
2096 break;
2097 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
2098 } /* for (ai_list) */
2100 freeaddrinfo (ai_list);
2102 /* Check if all went well. */
2103 if (se->type == SOCKENT_TYPE_SERVER)
2104 {
2105 if (se->data.server.fd_num <= 0)
2106 return (-1);
2107 }
2108 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2109 {
2110 if (se->data.client.fd < 0)
2111 return (-1);
2112 }
2114 return (0);
2115 } /* }}} int sockent_open */
2117 /* Add a sockent to the global list of sockets */
2118 static int sockent_add (sockent_t *se) /* {{{ */
2119 {
2120 sockent_t *last_ptr;
2122 if (se == NULL)
2123 return (-1);
2125 if (se->type == SOCKENT_TYPE_SERVER)
2126 {
2127 struct pollfd *tmp;
2128 size_t i;
2130 tmp = realloc (listen_sockets_pollfd,
2131 sizeof (*tmp) * (listen_sockets_num
2132 + se->data.server.fd_num));
2133 if (tmp == NULL)
2134 {
2135 ERROR ("network plugin: realloc failed.");
2136 return (-1);
2137 }
2138 listen_sockets_pollfd = tmp;
2139 tmp = listen_sockets_pollfd + listen_sockets_num;
2141 for (i = 0; i < se->data.server.fd_num; i++)
2142 {
2143 memset (tmp + i, 0, sizeof (*tmp));
2144 tmp[i].fd = se->data.server.fd[i];
2145 tmp[i].events = POLLIN | POLLPRI;
2146 tmp[i].revents = 0;
2147 }
2149 listen_sockets_num += se->data.server.fd_num;
2151 if (listen_sockets == NULL)
2152 {
2153 listen_sockets = se;
2154 return (0);
2155 }
2156 last_ptr = listen_sockets;
2157 }
2158 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2159 {
2160 if (sending_sockets == NULL)
2161 {
2162 sending_sockets = se;
2163 return (0);
2164 }
2165 last_ptr = sending_sockets;
2166 }
2168 while (last_ptr->next != NULL)
2169 last_ptr = last_ptr->next;
2170 last_ptr->next = se;
2172 return (0);
2173 } /* }}} int sockent_add */
2175 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2176 {
2177 while (42)
2178 {
2179 receive_list_entry_t *ent;
2180 sockent_t *se;
2182 /* Lock and wait for more data to come in */
2183 pthread_mutex_lock (&receive_list_lock);
2184 while ((listen_loop == 0)
2185 && (receive_list_head == NULL))
2186 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2188 /* Remove the head entry and unlock */
2189 ent = receive_list_head;
2190 if (ent != NULL)
2191 receive_list_head = ent->next;
2192 receive_list_length--;
2193 pthread_mutex_unlock (&receive_list_lock);
2195 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2196 * because we dispatch all missing packets before shutting down. */
2197 if (ent == NULL)
2198 break;
2200 /* Look for the correct `sockent_t' */
2201 se = listen_sockets;
2202 while (se != NULL)
2203 {
2204 size_t i;
2206 for (i = 0; i < se->data.server.fd_num; i++)
2207 if (se->data.server.fd[i] == ent->fd)
2208 break;
2210 if (i < se->data.server.fd_num)
2211 break;
2213 se = se->next;
2214 }
2216 if (se == NULL)
2217 {
2218 ERROR ("network plugin: Got packet from FD %i, but can't "
2219 "find an appropriate socket entry.",
2220 ent->fd);
2221 sfree (ent->data);
2222 sfree (ent);
2223 continue;
2224 }
2226 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2227 /* username = */ NULL);
2228 sfree (ent->data);
2229 sfree (ent);
2230 } /* while (42) */
2232 return (NULL);
2233 } /* }}} void *dispatch_thread */
2235 static int network_receive (void) /* {{{ */
2236 {
2237 char buffer[network_config_packet_size];
2238 int buffer_len;
2240 int i;
2241 int status;
2243 receive_list_entry_t *private_list_head;
2244 receive_list_entry_t *private_list_tail;
2245 uint64_t private_list_length;
2247 assert (listen_sockets_num > 0);
2249 private_list_head = NULL;
2250 private_list_tail = NULL;
2251 private_list_length = 0;
2253 while (listen_loop == 0)
2254 {
2255 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2257 if (status <= 0)
2258 {
2259 char errbuf[1024];
2260 if (errno == EINTR)
2261 continue;
2262 ERROR ("poll failed: %s",
2263 sstrerror (errno, errbuf, sizeof (errbuf)));
2264 return (-1);
2265 }
2267 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2268 {
2269 receive_list_entry_t *ent;
2271 if ((listen_sockets_pollfd[i].revents
2272 & (POLLIN | POLLPRI)) == 0)
2273 continue;
2274 status--;
2276 buffer_len = recv (listen_sockets_pollfd[i].fd,
2277 buffer, sizeof (buffer),
2278 0 /* no flags */);
2279 if (buffer_len < 0)
2280 {
2281 char errbuf[1024];
2282 ERROR ("recv failed: %s",
2283 sstrerror (errno, errbuf,
2284 sizeof (errbuf)));
2285 return (-1);
2286 }
2288 stats_octets_rx += ((uint64_t) buffer_len);
2289 stats_packets_rx++;
2291 /* TODO: Possible performance enhancement: Do not free
2292 * these entries in the dispatch thread but put them in
2293 * another list, so we don't have to allocate more and
2294 * more of these structures. */
2295 ent = malloc (sizeof (receive_list_entry_t));
2296 if (ent == NULL)
2297 {
2298 ERROR ("network plugin: malloc failed.");
2299 return (-1);
2300 }
2301 memset (ent, 0, sizeof (receive_list_entry_t));
2302 ent->data = malloc (network_config_packet_size);
2303 if (ent->data == NULL)
2304 {
2305 sfree (ent);
2306 ERROR ("network plugin: malloc failed.");
2307 return (-1);
2308 }
2309 ent->fd = listen_sockets_pollfd[i].fd;
2310 ent->next = NULL;
2312 memcpy (ent->data, buffer, buffer_len);
2313 ent->data_len = buffer_len;
2315 if (private_list_head == NULL)
2316 private_list_head = ent;
2317 else
2318 private_list_tail->next = ent;
2319 private_list_tail = ent;
2320 private_list_length++;
2322 /* Do not block here. Blocking here has led to
2323 * insufficient performance in the past. */
2324 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2325 {
2326 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2327 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2329 if (receive_list_head == NULL)
2330 receive_list_head = private_list_head;
2331 else
2332 receive_list_tail->next = private_list_head;
2333 receive_list_tail = private_list_tail;
2334 receive_list_length += private_list_length;
2336 pthread_cond_signal (&receive_list_cond);
2337 pthread_mutex_unlock (&receive_list_lock);
2339 private_list_head = NULL;
2340 private_list_tail = NULL;
2341 private_list_length = 0;
2342 }
2343 } /* for (listen_sockets_pollfd) */
2344 } /* while (listen_loop == 0) */
2346 /* Make sure everything is dispatched before exiting. */
2347 if (private_list_head != NULL)
2348 {
2349 pthread_mutex_lock (&receive_list_lock);
2351 if (receive_list_head == NULL)
2352 receive_list_head = private_list_head;
2353 else
2354 receive_list_tail->next = private_list_head;
2355 receive_list_tail = private_list_tail;
2356 receive_list_length += private_list_length;
2358 private_list_head = NULL;
2359 private_list_tail = NULL;
2360 private_list_length = 0;
2362 pthread_cond_signal (&receive_list_cond);
2363 pthread_mutex_unlock (&receive_list_lock);
2364 }
2366 return (0);
2367 } /* }}} int network_receive */
2369 static void *receive_thread (void __attribute__((unused)) *arg)
2370 {
2371 return (network_receive () ? (void *) 1 : (void *) 0);
2372 } /* void *receive_thread */
2374 static void network_init_buffer (void)
2375 {
2376 memset (send_buffer, 0, network_config_packet_size);
2377 send_buffer_ptr = send_buffer;
2378 send_buffer_fill = 0;
2380 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2381 } /* int network_init_buffer */
2383 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2384 const char *buffer, size_t buffer_size)
2385 {
2386 int status;
2388 while (42)
2389 {
2390 status = sendto (se->data.client.fd, buffer, buffer_size,
2391 /* flags = */ 0,
2392 (struct sockaddr *) se->data.client.addr,
2393 se->data.client.addrlen);
2394 if (status < 0)
2395 {
2396 char errbuf[1024];
2397 if (errno == EINTR)
2398 continue;
2399 ERROR ("network plugin: sendto failed: %s",
2400 sstrerror (errno, errbuf,
2401 sizeof (errbuf)));
2402 break;
2403 }
2405 break;
2406 } /* while (42) */
2407 } /* }}} void networt_send_buffer_plain */
2409 #if HAVE_LIBGCRYPT
2410 #define BUFFER_ADD(p,s) do { \
2411 memcpy (buffer + buffer_offset, (p), (s)); \
2412 buffer_offset += (s); \
2413 } while (0)
2415 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2416 const char *in_buffer, size_t in_buffer_size)
2417 {
2418 part_signature_sha256_t ps;
2419 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2420 size_t buffer_offset;
2421 size_t username_len;
2423 gcry_md_hd_t hd;
2424 gcry_error_t err;
2425 unsigned char *hash;
2427 hd = NULL;
2428 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2429 if (err != 0)
2430 {
2431 ERROR ("network plugin: Creating HMAC object failed: %s",
2432 gcry_strerror (err));
2433 return;
2434 }
2436 err = gcry_md_setkey (hd, se->data.client.password,
2437 strlen (se->data.client.password));
2438 if (err != 0)
2439 {
2440 ERROR ("network plugin: gcry_md_setkey failed: %s",
2441 gcry_strerror (err));
2442 gcry_md_close (hd);
2443 return;
2444 }
2446 username_len = strlen (se->data.client.username);
2447 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2448 {
2449 ERROR ("network plugin: Username too long: %s",
2450 se->data.client.username);
2451 return;
2452 }
2454 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2455 se->data.client.username, username_len);
2456 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2457 in_buffer, in_buffer_size);
2459 /* Initialize the `ps' structure. */
2460 memset (&ps, 0, sizeof (ps));
2461 ps.head.type = htons (TYPE_SIGN_SHA256);
2462 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2464 /* Calculate the hash value. */
2465 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2466 username_len + in_buffer_size);
2467 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2468 if (hash == NULL)
2469 {
2470 ERROR ("network plugin: gcry_md_read failed.");
2471 gcry_md_close (hd);
2472 return;
2473 }
2474 memcpy (ps.hash, hash, sizeof (ps.hash));
2476 /* Add the header */
2477 buffer_offset = 0;
2479 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2480 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2481 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2483 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2485 gcry_md_close (hd);
2486 hd = NULL;
2488 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2489 networt_send_buffer_plain (se, buffer, buffer_offset);
2490 } /* }}} void networt_send_buffer_signed */
2492 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2493 const char *in_buffer, size_t in_buffer_size)
2494 {
2495 part_encryption_aes256_t pea;
2496 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2497 size_t buffer_size;
2498 size_t buffer_offset;
2499 size_t header_size;
2500 size_t username_len;
2501 gcry_error_t err;
2502 gcry_cipher_hd_t cypher;
2504 /* Initialize the header fields */
2505 memset (&pea, 0, sizeof (pea));
2506 pea.head.type = htons (TYPE_ENCR_AES256);
2508 pea.username = se->data.client.username;
2510 username_len = strlen (pea.username);
2511 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2512 {
2513 ERROR ("network plugin: Username too long: %s", pea.username);
2514 return;
2515 }
2517 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2518 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2519 - sizeof (pea.hash);
2521 assert (buffer_size <= sizeof (buffer));
2522 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2523 "buffer_size = %zu;", buffer_size);
2525 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2526 + username_len + in_buffer_size));
2527 pea.username_length = htons ((uint16_t) username_len);
2529 /* Chose a random initialization vector. */
2530 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2532 /* Create hash of the payload */
2533 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2535 /* Initialize the buffer */
2536 buffer_offset = 0;
2537 memset (buffer, 0, sizeof (buffer));
2540 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2541 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2542 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2543 BUFFER_ADD (pea.username, username_len);
2544 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2545 assert (buffer_offset == header_size);
2546 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2547 BUFFER_ADD (in_buffer, in_buffer_size);
2549 assert (buffer_offset == buffer_size);
2551 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2552 se->data.client.password);
2553 if (cypher == NULL)
2554 return;
2556 /* Encrypt the buffer in-place */
2557 err = gcry_cipher_encrypt (cypher,
2558 buffer + header_size,
2559 buffer_size - header_size,
2560 /* in = */ NULL, /* in len = */ 0);
2561 if (err != 0)
2562 {
2563 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2564 gcry_strerror (err));
2565 return;
2566 }
2568 /* Send it out without further modifications */
2569 networt_send_buffer_plain (se, buffer, buffer_size);
2570 } /* }}} void networt_send_buffer_encrypted */
2571 #undef BUFFER_ADD
2572 #endif /* HAVE_LIBGCRYPT */
2574 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2575 {
2576 sockent_t *se;
2578 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2580 for (se = sending_sockets; se != NULL; se = se->next)
2581 {
2582 #if HAVE_LIBGCRYPT
2583 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2584 networt_send_buffer_encrypted (se, buffer, buffer_len);
2585 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2586 networt_send_buffer_signed (se, buffer, buffer_len);
2587 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2588 #endif /* HAVE_LIBGCRYPT */
2589 networt_send_buffer_plain (se, buffer, buffer_len);
2590 } /* for (sending_sockets) */
2591 } /* }}} void network_send_buffer */
2593 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2594 value_list_t *vl_def,
2595 const data_set_t *ds, const value_list_t *vl)
2596 {
2597 char *buffer_orig = buffer;
2599 if (strcmp (vl_def->host, vl->host) != 0)
2600 {
2601 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2602 vl->host, strlen (vl->host)) != 0)
2603 return (-1);
2604 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2605 }
2607 if (vl_def->time != vl->time)
2608 {
2609 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2610 (uint64_t) vl->time))
2611 return (-1);
2612 vl_def->time = vl->time;
2613 }
2615 if (vl_def->interval != vl->interval)
2616 {
2617 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2618 (uint64_t) vl->interval))
2619 return (-1);
2620 vl_def->interval = vl->interval;
2621 }
2623 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2624 {
2625 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2626 vl->plugin, strlen (vl->plugin)) != 0)
2627 return (-1);
2628 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2629 }
2631 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2632 {
2633 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2634 vl->plugin_instance,
2635 strlen (vl->plugin_instance)) != 0)
2636 return (-1);
2637 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2638 }
2640 if (strcmp (vl_def->type, vl->type) != 0)
2641 {
2642 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2643 vl->type, strlen (vl->type)) != 0)
2644 return (-1);
2645 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2646 }
2648 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2649 {
2650 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2651 vl->type_instance,
2652 strlen (vl->type_instance)) != 0)
2653 return (-1);
2654 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2655 }
2657 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2658 return (-1);
2660 return (buffer - buffer_orig);
2661 } /* }}} int add_to_buffer */
2663 static void flush_buffer (void)
2664 {
2665 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2666 send_buffer_fill);
2668 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2670 stats_octets_tx += ((uint64_t) send_buffer_fill);
2671 stats_packets_tx++;
2673 network_init_buffer ();
2674 }
2676 static int network_write (const data_set_t *ds, const value_list_t *vl,
2677 user_data_t __attribute__((unused)) *user_data)
2678 {
2679 int status;
2681 if (!check_send_okay (vl))
2682 {
2683 #if COLLECT_DEBUG
2684 char name[6*DATA_MAX_NAME_LEN];
2685 FORMAT_VL (name, sizeof (name), vl);
2686 name[sizeof (name) - 1] = 0;
2687 DEBUG ("network plugin: network_write: "
2688 "NOT sending %s.", name);
2689 #endif
2690 /* Counter is not protected by another lock and may be reached by
2691 * multiple threads */
2692 pthread_mutex_lock (&stats_lock);
2693 stats_values_not_sent++;
2694 pthread_mutex_unlock (&stats_lock);
2695 return (0);
2696 }
2698 uc_meta_data_add_unsigned_int (vl,
2699 "network:time_sent", (uint64_t) vl->time);
2701 pthread_mutex_lock (&send_buffer_lock);
2703 status = add_to_buffer (send_buffer_ptr,
2704 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2705 &send_buffer_vl,
2706 ds, vl);
2707 if (status >= 0)
2708 {
2709 /* status == bytes added to the buffer */
2710 send_buffer_fill += status;
2711 send_buffer_ptr += status;
2713 stats_values_sent++;
2714 }
2715 else
2716 {
2717 flush_buffer ();
2719 status = add_to_buffer (send_buffer_ptr,
2720 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2721 &send_buffer_vl,
2722 ds, vl);
2724 if (status >= 0)
2725 {
2726 send_buffer_fill += status;
2727 send_buffer_ptr += status;
2729 stats_values_sent++;
2730 }
2731 }
2733 if (status < 0)
2734 {
2735 ERROR ("network plugin: Unable to append to the "
2736 "buffer for some weird reason");
2737 }
2738 else if ((network_config_packet_size - send_buffer_fill) < 15)
2739 {
2740 flush_buffer ();
2741 }
2743 pthread_mutex_unlock (&send_buffer_lock);
2745 return ((status < 0) ? -1 : 0);
2746 } /* int network_write */
2748 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2749 int *retval)
2750 {
2751 if ((ci->values_num != 1)
2752 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2753 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2754 {
2755 ERROR ("network plugin: The `%s' config option needs "
2756 "exactly one boolean argument.", ci->key);
2757 return (-1);
2758 }
2760 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2761 {
2762 if (ci->values[0].value.boolean)
2763 *retval = 1;
2764 else
2765 *retval = 0;
2766 }
2767 else
2768 {
2769 char *str = ci->values[0].value.string;
2771 if (IS_TRUE (str))
2772 *retval = 1;
2773 else if (IS_FALSE (str))
2774 *retval = 0;
2775 else
2776 {
2777 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2778 "option as boolean value.",
2779 str, ci->key);
2780 return (-1);
2781 }
2782 }
2784 return (0);
2785 } /* }}} int network_config_set_boolean */
2787 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2788 {
2789 int tmp;
2790 if ((ci->values_num != 1)
2791 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2792 {
2793 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2794 "one numeric argument.");
2795 return (-1);
2796 }
2798 tmp = (int) ci->values[0].value.number;
2799 if ((tmp > 0) && (tmp <= 255))
2800 network_config_ttl = tmp;
2802 return (0);
2803 } /* }}} int network_config_set_ttl */
2805 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2806 int *interface)
2807 {
2808 if ((ci->values_num != 1)
2809 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2810 {
2811 WARNING ("network plugin: The `Interface' config option needs exactly "
2812 "one string argument.");
2813 return (-1);
2814 }
2816 if (interface == NULL)
2817 return (-1);
2819 *interface = if_nametoindex (ci->values[0].value.string);
2821 return (0);
2822 } /* }}} int network_config_set_interface */
2824 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2825 {
2826 int tmp;
2827 if ((ci->values_num != 1)
2828 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2829 {
2830 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
2831 "one numeric argument.");
2832 return (-1);
2833 }
2835 tmp = (int) ci->values[0].value.number;
2836 if ((tmp >= 1024) && (tmp <= 65535))
2837 network_config_packet_size = tmp;
2839 return (0);
2840 } /* }}} int network_config_set_buffer_size */
2842 #if HAVE_LIBGCRYPT
2843 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2844 char **ret_string)
2845 {
2846 char *tmp;
2847 if ((ci->values_num != 1)
2848 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2849 {
2850 WARNING ("network plugin: The `%s' config option needs exactly "
2851 "one string argument.", ci->key);
2852 return (-1);
2853 }
2855 tmp = strdup (ci->values[0].value.string);
2856 if (tmp == NULL)
2857 return (-1);
2859 sfree (*ret_string);
2860 *ret_string = tmp;
2862 return (0);
2863 } /* }}} int network_config_set_string */
2864 #endif /* HAVE_LIBGCRYPT */
2866 #if HAVE_LIBGCRYPT
2867 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2868 int *retval)
2869 {
2870 char *str;
2871 if ((ci->values_num != 1)
2872 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2873 {
2874 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2875 "one string argument.");
2876 return (-1);
2877 }
2879 str = ci->values[0].value.string;
2880 if (strcasecmp ("Encrypt", str) == 0)
2881 *retval = SECURITY_LEVEL_ENCRYPT;
2882 else if (strcasecmp ("Sign", str) == 0)
2883 *retval = SECURITY_LEVEL_SIGN;
2884 else if (strcasecmp ("None", str) == 0)
2885 *retval = SECURITY_LEVEL_NONE;
2886 else
2887 {
2888 WARNING ("network plugin: Unknown security level: %s.", str);
2889 return (-1);
2890 }
2892 return (0);
2893 } /* }}} int network_config_set_security_level */
2894 #endif /* HAVE_LIBGCRYPT */
2896 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2897 {
2898 sockent_t *se;
2899 int status;
2900 int i;
2902 if ((ci->values_num < 1) || (ci->values_num > 2)
2903 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2904 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2905 {
2906 ERROR ("network plugin: The `%s' config option needs "
2907 "one or two string arguments.", ci->key);
2908 return (-1);
2909 }
2911 se = malloc (sizeof (*se));
2912 if (se == NULL)
2913 {
2914 ERROR ("network plugin: malloc failed.");
2915 return (-1);
2916 }
2917 sockent_init (se, SOCKENT_TYPE_SERVER);
2919 se->node = strdup (ci->values[0].value.string);
2920 if (ci->values_num >= 2)
2921 se->service = strdup (ci->values[1].value.string);
2923 for (i = 0; i < ci->children_num; i++)
2924 {
2925 oconfig_item_t *child = ci->children + i;
2927 #if HAVE_LIBGCRYPT
2928 if (strcasecmp ("AuthFile", child->key) == 0)
2929 network_config_set_string (child, &se->data.server.auth_file);
2930 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2931 network_config_set_security_level (child,
2932 &se->data.server.security_level);
2933 else
2934 #endif /* HAVE_LIBGCRYPT */
2935 if (strcasecmp ("Interface", child->key) == 0)
2936 network_config_set_interface (child,
2937 &se->interface);
2938 else
2939 {
2940 WARNING ("network plugin: Option `%s' is not allowed here.",
2941 child->key);
2942 }
2943 }
2945 #if HAVE_LIBGCRYPT
2946 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2947 && (se->data.server.auth_file == NULL))
2948 {
2949 ERROR ("network plugin: A security level higher than `none' was "
2950 "requested, but no AuthFile option was given. Cowardly refusing to "
2951 "open this socket!");
2952 sockent_destroy (se);
2953 return (-1);
2954 }
2955 #endif /* HAVE_LIBGCRYPT */
2957 status = sockent_open (se);
2958 if (status != 0)
2959 {
2960 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2961 sockent_destroy (se);
2962 return (-1);
2963 }
2965 status = sockent_add (se);
2966 if (status != 0)
2967 {
2968 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2969 sockent_destroy (se);
2970 return (-1);
2971 }
2973 return (0);
2974 } /* }}} int network_config_add_listen */
2976 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2977 {
2978 sockent_t *se;
2979 int status;
2980 int i;
2982 if ((ci->values_num < 1) || (ci->values_num > 2)
2983 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2984 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2985 {
2986 ERROR ("network plugin: The `%s' config option needs "
2987 "one or two string arguments.", ci->key);
2988 return (-1);
2989 }
2991 se = malloc (sizeof (*se));
2992 if (se == NULL)
2993 {
2994 ERROR ("network plugin: malloc failed.");
2995 return (-1);
2996 }
2997 sockent_init (se, SOCKENT_TYPE_CLIENT);
2999 se->node = strdup (ci->values[0].value.string);
3000 if (ci->values_num >= 2)
3001 se->service = strdup (ci->values[1].value.string);
3003 for (i = 0; i < ci->children_num; i++)
3004 {
3005 oconfig_item_t *child = ci->children + i;
3007 #if HAVE_LIBGCRYPT
3008 if (strcasecmp ("Username", child->key) == 0)
3009 network_config_set_string (child, &se->data.client.username);
3010 else if (strcasecmp ("Password", child->key) == 0)
3011 network_config_set_string (child, &se->data.client.password);
3012 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3013 network_config_set_security_level (child,
3014 &se->data.client.security_level);
3015 else
3016 #endif /* HAVE_LIBGCRYPT */
3017 if (strcasecmp ("Interface", child->key) == 0)
3018 network_config_set_interface (child,
3019 &se->interface);
3020 else
3021 {
3022 WARNING ("network plugin: Option `%s' is not allowed here.",
3023 child->key);
3024 }
3025 }
3027 #if HAVE_LIBGCRYPT
3028 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3029 && ((se->data.client.username == NULL)
3030 || (se->data.client.password == NULL)))
3031 {
3032 ERROR ("network plugin: A security level higher than `none' was "
3033 "requested, but no Username or Password option was given. "
3034 "Cowardly refusing to open this socket!");
3035 sockent_destroy (se);
3036 return (-1);
3037 }
3038 #endif /* HAVE_LIBGCRYPT */
3040 status = sockent_open (se);
3041 if (status != 0)
3042 {
3043 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
3044 sockent_destroy (se);
3045 return (-1);
3046 }
3048 status = sockent_add (se);
3049 if (status != 0)
3050 {
3051 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3052 sockent_destroy (se);
3053 return (-1);
3054 }
3056 return (0);
3057 } /* }}} int network_config_add_server */
3059 static int network_config (oconfig_item_t *ci) /* {{{ */
3060 {
3061 int i;
3063 for (i = 0; i < ci->children_num; i++)
3064 {
3065 oconfig_item_t *child = ci->children + i;
3067 if (strcasecmp ("Listen", child->key) == 0)
3068 network_config_add_listen (child);
3069 else if (strcasecmp ("Server", child->key) == 0)
3070 network_config_add_server (child);
3071 else if (strcasecmp ("TimeToLive", child->key) == 0)
3072 network_config_set_ttl (child);
3073 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3074 network_config_set_buffer_size (child);
3075 else if (strcasecmp ("Forward", child->key) == 0)
3076 network_config_set_boolean (child, &network_config_forward);
3077 else if (strcasecmp ("ReportStats", child->key) == 0)
3078 network_config_set_boolean (child, &network_config_stats);
3079 else
3080 {
3081 WARNING ("network plugin: Option `%s' is not allowed here.",
3082 child->key);
3083 }
3084 }
3086 return (0);
3087 } /* }}} int network_config */
3089 static int network_notification (const notification_t *n,
3090 user_data_t __attribute__((unused)) *user_data)
3091 {
3092 char buffer[network_config_packet_size];
3093 char *buffer_ptr = buffer;
3094 int buffer_free = sizeof (buffer);
3095 int status;
3097 memset (buffer, '\0', sizeof (buffer));
3099 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3100 (uint64_t) n->time);
3101 if (status != 0)
3102 return (-1);
3104 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3105 (uint64_t) n->severity);
3106 if (status != 0)
3107 return (-1);
3109 if (strlen (n->host) > 0)
3110 {
3111 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3112 n->host, strlen (n->host));
3113 if (status != 0)
3114 return (-1);
3115 }
3117 if (strlen (n->plugin) > 0)
3118 {
3119 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3120 n->plugin, strlen (n->plugin));
3121 if (status != 0)
3122 return (-1);
3123 }
3125 if (strlen (n->plugin_instance) > 0)
3126 {
3127 status = write_part_string (&buffer_ptr, &buffer_free,
3128 TYPE_PLUGIN_INSTANCE,
3129 n->plugin_instance, strlen (n->plugin_instance));
3130 if (status != 0)
3131 return (-1);
3132 }
3134 if (strlen (n->type) > 0)
3135 {
3136 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3137 n->type, strlen (n->type));
3138 if (status != 0)
3139 return (-1);
3140 }
3142 if (strlen (n->type_instance) > 0)
3143 {
3144 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3145 n->type_instance, strlen (n->type_instance));
3146 if (status != 0)
3147 return (-1);
3148 }
3150 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3151 n->message, strlen (n->message));
3152 if (status != 0)
3153 return (-1);
3155 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3157 return (0);
3158 } /* int network_notification */
3160 static int network_shutdown (void)
3161 {
3162 listen_loop++;
3164 /* Kill the listening thread */
3165 if (receive_thread_running != 0)
3166 {
3167 INFO ("network plugin: Stopping receive thread.");
3168 pthread_kill (receive_thread_id, SIGTERM);
3169 pthread_join (receive_thread_id, NULL /* no return value */);
3170 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3171 receive_thread_running = 0;
3172 }
3174 /* Shutdown the dispatching thread */
3175 if (dispatch_thread_running != 0)
3176 {
3177 INFO ("network plugin: Stopping dispatch thread.");
3178 pthread_mutex_lock (&receive_list_lock);
3179 pthread_cond_broadcast (&receive_list_cond);
3180 pthread_mutex_unlock (&receive_list_lock);
3181 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3182 dispatch_thread_running = 0;
3183 }
3185 sockent_destroy (listen_sockets);
3187 if (send_buffer_fill > 0)
3188 flush_buffer ();
3190 sfree (send_buffer);
3192 /* TODO: Close `sending_sockets' */
3194 plugin_unregister_config ("network");
3195 plugin_unregister_init ("network");
3196 plugin_unregister_write ("network");
3197 plugin_unregister_shutdown ("network");
3199 return (0);
3200 } /* int network_shutdown */
3202 static int network_stats_read (void) /* {{{ */
3203 {
3204 derive_t copy_octets_rx;
3205 derive_t copy_octets_tx;
3206 derive_t copy_packets_rx;
3207 derive_t copy_packets_tx;
3208 derive_t copy_values_dispatched;
3209 derive_t copy_values_not_dispatched;
3210 derive_t copy_values_sent;
3211 derive_t copy_values_not_sent;
3212 derive_t copy_receive_list_length;
3213 value_list_t vl = VALUE_LIST_INIT;
3214 value_t values[2];
3216 copy_octets_rx = stats_octets_rx;
3217 copy_octets_tx = stats_octets_tx;
3218 copy_packets_rx = stats_packets_rx;
3219 copy_packets_tx = stats_packets_tx;
3220 copy_values_dispatched = stats_values_dispatched;
3221 copy_values_not_dispatched = stats_values_not_dispatched;
3222 copy_values_sent = stats_values_sent;
3223 copy_values_not_sent = stats_values_not_sent;
3224 copy_receive_list_length = receive_list_length;
3226 /* Initialize `vl' */
3227 vl.values = values;
3228 vl.values_len = 2;
3229 vl.time = 0;
3230 vl.interval = interval_g;
3231 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3232 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3234 /* Octets received / sent */
3235 vl.values[0].derive = (derive_t) copy_octets_rx;
3236 vl.values[1].derive = (derive_t) copy_octets_tx;
3237 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3238 plugin_dispatch_values (&vl);
3240 /* Packets received / send */
3241 vl.values[0].derive = (derive_t) copy_packets_rx;
3242 vl.values[1].derive = (derive_t) copy_packets_tx;
3243 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3244 plugin_dispatch_values (&vl);
3246 /* Values (not) dispatched and (not) send */
3247 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3248 vl.values_len = 1;
3250 vl.values[0].derive = (derive_t) copy_values_dispatched;
3251 sstrncpy (vl.type_instance, "dispatch-accepted",
3252 sizeof (vl.type_instance));
3253 plugin_dispatch_values (&vl);
3255 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3256 sstrncpy (vl.type_instance, "dispatch-rejected",
3257 sizeof (vl.type_instance));
3258 plugin_dispatch_values (&vl);
3260 vl.values[0].derive = (derive_t) copy_values_sent;
3261 sstrncpy (vl.type_instance, "send-accepted",
3262 sizeof (vl.type_instance));
3263 plugin_dispatch_values (&vl);
3265 vl.values[0].derive = (derive_t) copy_values_not_sent;
3266 sstrncpy (vl.type_instance, "send-rejected",
3267 sizeof (vl.type_instance));
3268 plugin_dispatch_values (&vl);
3270 /* Receive queue length */
3271 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3272 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3273 vl.type_instance[0] = 0;
3274 plugin_dispatch_values (&vl);
3276 return (0);
3277 } /* }}} int network_stats_read */
3279 static int network_init (void)
3280 {
3281 static _Bool have_init = 0;
3283 /* Check if we were already initialized. If so, just return - there's
3284 * nothing more to do (for now, that is). */
3285 if (have_init)
3286 return (0);
3287 have_init = 1;
3289 #if HAVE_LIBGCRYPT
3290 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
3291 gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
3292 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
3293 #endif
3295 if (network_config_stats != 0)
3296 plugin_register_read ("network", network_stats_read);
3298 plugin_register_shutdown ("network", network_shutdown);
3300 send_buffer = malloc (network_config_packet_size);
3301 if (send_buffer == NULL)
3302 {
3303 ERROR ("network plugin: malloc failed.");
3304 return (-1);
3305 }
3306 network_init_buffer ();
3308 /* setup socket(s) and so on */
3309 if (sending_sockets != NULL)
3310 {
3311 plugin_register_write ("network", network_write,
3312 /* user_data = */ NULL);
3313 plugin_register_notification ("network", network_notification,
3314 /* user_data = */ NULL);
3315 }
3317 /* If no threads need to be started, return here. */
3318 if ((listen_sockets_num == 0)
3319 || ((dispatch_thread_running != 0)
3320 && (receive_thread_running != 0)))
3321 return (0);
3323 if (dispatch_thread_running == 0)
3324 {
3325 int status;
3326 status = pthread_create (&dispatch_thread_id,
3327 NULL /* no attributes */,
3328 dispatch_thread,
3329 NULL /* no argument */);
3330 if (status != 0)
3331 {
3332 char errbuf[1024];
3333 ERROR ("network: pthread_create failed: %s",
3334 sstrerror (errno, errbuf,
3335 sizeof (errbuf)));
3336 }
3337 else
3338 {
3339 dispatch_thread_running = 1;
3340 }
3341 }
3343 if (receive_thread_running == 0)
3344 {
3345 int status;
3346 status = pthread_create (&receive_thread_id,
3347 NULL /* no attributes */,
3348 receive_thread,
3349 NULL /* no argument */);
3350 if (status != 0)
3351 {
3352 char errbuf[1024];
3353 ERROR ("network: pthread_create failed: %s",
3354 sstrerror (errno, errbuf,
3355 sizeof (errbuf)));
3356 }
3357 else
3358 {
3359 receive_thread_running = 1;
3360 }
3361 }
3363 return (0);
3364 } /* int network_init */
3366 /*
3367 * The flush option of the network plugin cannot flush individual identifiers.
3368 * All the values are added to a buffer and sent when the buffer is full, the
3369 * requested value may or may not be in there, it's not worth finding out. We
3370 * just send the buffer if `flush' is called - if the requested value was in
3371 * there, good. If not, well, then there is nothing to flush.. -octo
3372 */
3373 static int network_flush (__attribute__((unused)) cdtime_t timeout,
3374 __attribute__((unused)) const char *identifier,
3375 __attribute__((unused)) user_data_t *user_data)
3376 {
3377 pthread_mutex_lock (&send_buffer_lock);
3379 if (send_buffer_fill > 0)
3380 flush_buffer ();
3382 pthread_mutex_unlock (&send_buffer_lock);
3384 return (0);
3385 } /* int network_flush */
3387 void module_register (void)
3388 {
3389 plugin_register_complex_config ("network", network_config);
3390 plugin_register_init ("network", network_init);
3391 plugin_register_flush ("network", network_flush,
3392 /* user_data = */ NULL);
3393 } /* void module_register */
3395 /* vim: set fdm=marker : */