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_secure (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;
792 if ((buffer_len < 0) || ((size_t) buffer_len < exp_size))
793 {
794 WARNING ("network plugin: parse_part_number: "
795 "Packet too short: "
796 "Chunk of size %zu expected, "
797 "but buffer has only %zu bytes left.",
798 exp_size, buffer_len);
799 return (-1);
800 }
802 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
803 buffer += sizeof (tmp16);
804 /* pkg_type = ntohs (tmp16); */
806 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
807 buffer += sizeof (tmp16);
808 pkg_length = ntohs (tmp16);
810 memcpy ((void *) &tmp64, buffer, sizeof (tmp64));
811 buffer += sizeof (tmp64);
812 *value = ntohll (tmp64);
814 *ret_buffer = buffer;
815 *ret_buffer_len = buffer_len - pkg_length;
817 return (0);
818 } /* int parse_part_number */
820 static int parse_part_string (void **ret_buffer, size_t *ret_buffer_len,
821 char *output, int output_len)
822 {
823 char *buffer = *ret_buffer;
824 size_t buffer_len = *ret_buffer_len;
826 uint16_t tmp16;
827 size_t header_size = 2 * sizeof (uint16_t);
829 uint16_t pkg_length;
831 if ((buffer_len < 0) || (buffer_len < header_size))
832 {
833 WARNING ("network plugin: parse_part_string: "
834 "Packet too short: "
835 "Chunk of at least size %zu expected, "
836 "but buffer has only %zu bytes left.",
837 header_size, buffer_len);
838 return (-1);
839 }
841 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
842 buffer += sizeof (tmp16);
843 /* pkg_type = ntohs (tmp16); */
845 memcpy ((void *) &tmp16, buffer, sizeof (tmp16));
846 buffer += sizeof (tmp16);
847 pkg_length = ntohs (tmp16);
849 /* Check that packet fits in the input buffer */
850 if (pkg_length > buffer_len)
851 {
852 WARNING ("network plugin: parse_part_string: "
853 "Packet too big: "
854 "Chunk of size %"PRIu16" received, "
855 "but buffer has only %zu bytes left.",
856 pkg_length, buffer_len);
857 return (-1);
858 }
860 /* Check that pkg_length is in the valid range */
861 if (pkg_length <= header_size)
862 {
863 WARNING ("network plugin: parse_part_string: "
864 "Packet too short: "
865 "Header claims this packet is only %hu "
866 "bytes long.", pkg_length);
867 return (-1);
868 }
870 /* Check that the package data fits into the output buffer.
871 * The previous if-statement ensures that:
872 * `pkg_length > header_size' */
873 if ((output_len < 0)
874 || ((size_t) output_len < ((size_t) pkg_length - header_size)))
875 {
876 WARNING ("network plugin: parse_part_string: "
877 "Output buffer too small.");
878 return (-1);
879 }
881 /* All sanity checks successfull, let's copy the data over */
882 output_len = pkg_length - header_size;
883 memcpy ((void *) output, (void *) buffer, output_len);
884 buffer += output_len;
886 /* For some very weird reason '\0' doesn't do the trick on SPARC in
887 * this statement. */
888 if (output[output_len - 1] != 0)
889 {
890 WARNING ("network plugin: parse_part_string: "
891 "Received string does not end "
892 "with a NULL-byte.");
893 return (-1);
894 }
896 *ret_buffer = buffer;
897 *ret_buffer_len = buffer_len - pkg_length;
899 return (0);
900 } /* int parse_part_string */
902 /* Forward declaration: parse_part_sign_sha256 and parse_part_encr_aes256 call
903 * parse_packet and vice versa. */
904 #define PP_SIGNED 0x01
905 #define PP_ENCRYPTED 0x02
906 static int parse_packet (sockent_t *se,
907 void *buffer, size_t buffer_size, int flags,
908 const char *username);
910 #define BUFFER_READ(p,s) do { \
911 memcpy ((p), buffer + buffer_offset, (s)); \
912 buffer_offset += (s); \
913 } while (0)
915 #if HAVE_LIBGCRYPT
916 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
917 void **ret_buffer, size_t *ret_buffer_len, int flags)
918 {
919 static c_complain_t complain_no_users = C_COMPLAIN_INIT_STATIC;
921 char *buffer;
922 size_t buffer_len;
923 size_t buffer_offset;
925 size_t username_len;
926 char *secret;
928 part_signature_sha256_t pss;
929 uint16_t pss_head_length;
930 char hash[sizeof (pss.hash)];
932 gcry_md_hd_t hd;
933 gcry_error_t err;
934 unsigned char *hash_ptr;
936 buffer = *ret_buffer;
937 buffer_len = *ret_buffer_len;
938 buffer_offset = 0;
940 if (se->data.server.userdb == NULL)
941 {
942 c_complain (LOG_NOTICE, &complain_no_users,
943 "network plugin: Received signed network packet but can't verify it "
944 "because no user DB has been configured. Will accept it.");
945 return (0);
946 }
948 /* Check if the buffer has enough data for this structure. */
949 if (buffer_len <= PART_SIGNATURE_SHA256_SIZE)
950 return (-ENOMEM);
952 /* Read type and length header */
953 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
954 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
955 pss_head_length = ntohs (pss.head.length);
957 /* Check if the `pss_head_length' is within bounds. */
958 if ((pss_head_length <= PART_SIGNATURE_SHA256_SIZE)
959 || (pss_head_length > buffer_len))
960 {
961 ERROR ("network plugin: HMAC-SHA-256 with invalid length received.");
962 return (-1);
963 }
965 /* Copy the hash. */
966 BUFFER_READ (pss.hash, sizeof (pss.hash));
968 /* Calculate username length (without null byte) and allocate memory */
969 username_len = pss_head_length - PART_SIGNATURE_SHA256_SIZE;
970 pss.username = malloc (username_len + 1);
971 if (pss.username == NULL)
972 return (-ENOMEM);
974 /* Read the username */
975 BUFFER_READ (pss.username, username_len);
976 pss.username[username_len] = 0;
978 assert (buffer_offset == pss_head_length);
980 /* Query the password */
981 secret = fbh_get (se->data.server.userdb, pss.username);
982 if (secret == NULL)
983 {
984 ERROR ("network plugin: Unknown user: %s", pss.username);
985 sfree (pss.username);
986 return (-ENOENT);
987 }
989 /* Create a hash device and check the HMAC */
990 hd = NULL;
991 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
992 if (err != 0)
993 {
994 ERROR ("network plugin: Creating HMAC-SHA-256 object failed: %s",
995 gcry_strerror (err));
996 sfree (secret);
997 sfree (pss.username);
998 return (-1);
999 }
1001 err = gcry_md_setkey (hd, secret, strlen (secret));
1002 if (err != 0)
1003 {
1004 ERROR ("network plugin: gcry_md_setkey failed: %s", gcry_strerror (err));
1005 gcry_md_close (hd);
1006 sfree (secret);
1007 sfree (pss.username);
1008 return (-1);
1009 }
1011 gcry_md_write (hd,
1012 buffer + PART_SIGNATURE_SHA256_SIZE,
1013 buffer_len - PART_SIGNATURE_SHA256_SIZE);
1014 hash_ptr = gcry_md_read (hd, GCRY_MD_SHA256);
1015 if (hash_ptr == NULL)
1016 {
1017 ERROR ("network plugin: gcry_md_read failed.");
1018 gcry_md_close (hd);
1019 sfree (secret);
1020 sfree (pss.username);
1021 return (-1);
1022 }
1023 memcpy (hash, hash_ptr, sizeof (hash));
1025 /* Clean up */
1026 gcry_md_close (hd);
1027 hd = NULL;
1029 if (memcmp (pss.hash, hash, sizeof (pss.hash)) != 0)
1030 {
1031 WARNING ("network plugin: Verifying HMAC-SHA-256 signature failed: "
1032 "Hash mismatch.");
1033 }
1034 else
1035 {
1036 parse_packet (se, buffer + buffer_offset, buffer_len - buffer_offset,
1037 flags | PP_SIGNED, pss.username);
1038 }
1040 sfree (secret);
1041 sfree (pss.username);
1043 *ret_buffer = buffer + buffer_len;
1044 *ret_buffer_len = 0;
1046 return (0);
1047 } /* }}} int parse_part_sign_sha256 */
1048 /* #endif HAVE_LIBGCRYPT */
1050 #else /* if !HAVE_LIBGCRYPT */
1051 static int parse_part_sign_sha256 (sockent_t *se, /* {{{ */
1052 void **ret_buffer, size_t *ret_buffer_size, int flags)
1053 {
1054 static int warning_has_been_printed = 0;
1056 char *buffer;
1057 size_t buffer_size;
1058 size_t buffer_offset;
1059 uint16_t part_len;
1061 part_signature_sha256_t pss;
1063 buffer = *ret_buffer;
1064 buffer_size = *ret_buffer_size;
1065 buffer_offset = 0;
1067 if (buffer_size <= PART_SIGNATURE_SHA256_SIZE)
1068 return (-ENOMEM);
1070 BUFFER_READ (&pss.head.type, sizeof (pss.head.type));
1071 BUFFER_READ (&pss.head.length, sizeof (pss.head.length));
1072 part_len = ntohs (pss.head.length);
1074 if ((part_len <= PART_SIGNATURE_SHA256_SIZE)
1075 || (part_len > buffer_size))
1076 return (-EINVAL);
1078 if (warning_has_been_printed == 0)
1079 {
1080 WARNING ("network plugin: Received signed packet, but the network "
1081 "plugin was not linked with libgcrypt, so I cannot "
1082 "verify the signature. The packet will be accepted.");
1083 warning_has_been_printed = 1;
1084 }
1086 parse_packet (se, buffer + part_len, buffer_size - part_len, flags,
1087 /* username = */ NULL);
1089 *ret_buffer = buffer + buffer_size;
1090 *ret_buffer_size = 0;
1092 return (0);
1093 } /* }}} int parse_part_sign_sha256 */
1094 #endif /* !HAVE_LIBGCRYPT */
1096 #if HAVE_LIBGCRYPT
1097 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1098 void **ret_buffer, size_t *ret_buffer_len,
1099 int flags)
1100 {
1101 char *buffer = *ret_buffer;
1102 size_t buffer_len = *ret_buffer_len;
1103 size_t payload_len;
1104 size_t part_size;
1105 size_t buffer_offset;
1106 uint16_t username_len;
1107 part_encryption_aes256_t pea;
1108 unsigned char hash[sizeof (pea.hash)];
1110 gcry_cipher_hd_t cypher;
1111 gcry_error_t err;
1113 /* Make sure at least the header if available. */
1114 if (buffer_len <= PART_ENCRYPTION_AES256_SIZE)
1115 {
1116 NOTICE ("network plugin: parse_part_encr_aes256: "
1117 "Discarding short packet.");
1118 return (-1);
1119 }
1121 buffer_offset = 0;
1123 /* Copy the unencrypted information into `pea'. */
1124 BUFFER_READ (&pea.head.type, sizeof (pea.head.type));
1125 BUFFER_READ (&pea.head.length, sizeof (pea.head.length));
1127 /* Check the `part size'. */
1128 part_size = ntohs (pea.head.length);
1129 if ((part_size <= PART_ENCRYPTION_AES256_SIZE)
1130 || (part_size > buffer_len))
1131 {
1132 NOTICE ("network plugin: parse_part_encr_aes256: "
1133 "Discarding part with invalid size.");
1134 return (-1);
1135 }
1137 /* Read the username */
1138 BUFFER_READ (&username_len, sizeof (username_len));
1139 username_len = ntohs (username_len);
1141 if ((username_len <= 0)
1142 || (username_len > (part_size - (PART_ENCRYPTION_AES256_SIZE + 1))))
1143 {
1144 NOTICE ("network plugin: parse_part_encr_aes256: "
1145 "Discarding part with invalid username length.");
1146 return (-1);
1147 }
1149 assert (username_len > 0);
1150 pea.username = malloc (username_len + 1);
1151 if (pea.username == NULL)
1152 return (-ENOMEM);
1153 BUFFER_READ (pea.username, username_len);
1154 pea.username[username_len] = 0;
1156 /* Last but not least, the initialization vector */
1157 BUFFER_READ (pea.iv, sizeof (pea.iv));
1159 /* Make sure we are at the right position */
1160 assert (buffer_offset == (username_len +
1161 PART_ENCRYPTION_AES256_SIZE - sizeof (pea.hash)));
1163 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
1164 pea.username);
1165 if (cypher == NULL)
1166 {
1167 sfree (pea.username);
1168 return (-1);
1169 }
1171 payload_len = part_size - (PART_ENCRYPTION_AES256_SIZE + username_len);
1172 assert (payload_len > 0);
1174 /* Decrypt the packet in-place */
1175 err = gcry_cipher_decrypt (cypher,
1176 buffer + buffer_offset,
1177 part_size - buffer_offset,
1178 /* in = */ NULL, /* in len = */ 0);
1179 if (err != 0)
1180 {
1181 sfree (pea.username);
1182 ERROR ("network plugin: gcry_cipher_decrypt returned: %s",
1183 gcry_strerror (err));
1184 return (-1);
1185 }
1187 /* Read the hash */
1188 BUFFER_READ (pea.hash, sizeof (pea.hash));
1190 /* Make sure we're at the right position - again */
1191 assert (buffer_offset == (username_len + PART_ENCRYPTION_AES256_SIZE));
1192 assert (buffer_offset == (part_size - payload_len));
1194 /* Check hash sum */
1195 memset (hash, 0, sizeof (hash));
1196 gcry_md_hash_buffer (GCRY_MD_SHA1, hash,
1197 buffer + buffer_offset, payload_len);
1198 if (memcmp (hash, pea.hash, sizeof (hash)) != 0)
1199 {
1200 sfree (pea.username);
1201 ERROR ("network plugin: Decryption failed: Checksum mismatch.");
1202 return (-1);
1203 }
1205 parse_packet (se, buffer + buffer_offset, payload_len,
1206 flags | PP_ENCRYPTED, pea.username);
1208 /* XXX: Free pea.username?!? */
1210 /* Update return values */
1211 *ret_buffer = buffer + part_size;
1212 *ret_buffer_len = buffer_len - part_size;
1214 sfree (pea.username);
1216 return (0);
1217 } /* }}} int parse_part_encr_aes256 */
1218 /* #endif HAVE_LIBGCRYPT */
1220 #else /* if !HAVE_LIBGCRYPT */
1221 static int parse_part_encr_aes256 (sockent_t *se, /* {{{ */
1222 void **ret_buffer, size_t *ret_buffer_size, int flags)
1223 {
1224 static int warning_has_been_printed = 0;
1226 char *buffer;
1227 size_t buffer_size;
1228 size_t buffer_offset;
1230 part_header_t ph;
1231 size_t ph_length;
1233 buffer = *ret_buffer;
1234 buffer_size = *ret_buffer_size;
1235 buffer_offset = 0;
1237 /* parse_packet assures this minimum size. */
1238 assert (buffer_size >= (sizeof (ph.type) + sizeof (ph.length)));
1240 BUFFER_READ (&ph.type, sizeof (ph.type));
1241 BUFFER_READ (&ph.length, sizeof (ph.length));
1242 ph_length = ntohs (ph.length);
1244 if ((ph_length <= PART_ENCRYPTION_AES256_SIZE)
1245 || (ph_length > buffer_size))
1246 {
1247 ERROR ("network plugin: AES-256 encrypted part "
1248 "with invalid length received.");
1249 return (-1);
1250 }
1252 if (warning_has_been_printed == 0)
1253 {
1254 WARNING ("network plugin: Received encrypted packet, but the network "
1255 "plugin was not linked with libgcrypt, so I cannot "
1256 "decrypt it. The part will be discarded.");
1257 warning_has_been_printed = 1;
1258 }
1260 *ret_buffer += ph_length;
1261 *ret_buffer_size -= ph_length;
1263 return (0);
1264 } /* }}} int parse_part_encr_aes256 */
1265 #endif /* !HAVE_LIBGCRYPT */
1267 #undef BUFFER_READ
1269 static int parse_packet (sockent_t *se, /* {{{ */
1270 void *buffer, size_t buffer_size, int flags,
1271 const char *username)
1272 {
1273 int status;
1275 value_list_t vl = VALUE_LIST_INIT;
1276 notification_t n;
1278 #if HAVE_LIBGCRYPT
1279 int packet_was_signed = (flags & PP_SIGNED);
1280 int packet_was_encrypted = (flags & PP_ENCRYPTED);
1281 int printed_ignore_warning = 0;
1282 #endif /* HAVE_LIBGCRYPT */
1285 memset (&vl, '\0', sizeof (vl));
1286 memset (&n, '\0', sizeof (n));
1287 status = 0;
1289 while ((status == 0) && (0 < buffer_size)
1290 && ((unsigned int) buffer_size > sizeof (part_header_t)))
1291 {
1292 uint16_t pkg_length;
1293 uint16_t pkg_type;
1295 memcpy ((void *) &pkg_type,
1296 (void *) buffer,
1297 sizeof (pkg_type));
1298 memcpy ((void *) &pkg_length,
1299 (void *) (buffer + sizeof (pkg_type)),
1300 sizeof (pkg_length));
1302 pkg_length = ntohs (pkg_length);
1303 pkg_type = ntohs (pkg_type);
1305 if (pkg_length > buffer_size)
1306 break;
1307 /* Ensure that this loop terminates eventually */
1308 if (pkg_length < (2 * sizeof (uint16_t)))
1309 break;
1311 if (pkg_type == TYPE_ENCR_AES256)
1312 {
1313 status = parse_part_encr_aes256 (se,
1314 &buffer, &buffer_size, flags);
1315 if (status != 0)
1316 {
1317 ERROR ("network plugin: Decrypting AES256 "
1318 "part failed "
1319 "with status %i.", status);
1320 break;
1321 }
1322 }
1323 #if HAVE_LIBGCRYPT
1324 else if ((se->data.server.security_level == SECURITY_LEVEL_ENCRYPT)
1325 && (packet_was_encrypted == 0))
1326 {
1327 if (printed_ignore_warning == 0)
1328 {
1329 INFO ("network plugin: Unencrypted packet or "
1330 "part has been ignored.");
1331 printed_ignore_warning = 1;
1332 }
1333 buffer = ((char *) buffer) + pkg_length;
1334 continue;
1335 }
1336 #endif /* HAVE_LIBGCRYPT */
1337 else if (pkg_type == TYPE_SIGN_SHA256)
1338 {
1339 status = parse_part_sign_sha256 (se,
1340 &buffer, &buffer_size, flags);
1341 if (status != 0)
1342 {
1343 ERROR ("network plugin: Verifying HMAC-SHA-256 "
1344 "signature failed "
1345 "with status %i.", status);
1346 break;
1347 }
1348 }
1349 #if HAVE_LIBGCRYPT
1350 else if ((se->data.server.security_level == SECURITY_LEVEL_SIGN)
1351 && (packet_was_encrypted == 0)
1352 && (packet_was_signed == 0))
1353 {
1354 if (printed_ignore_warning == 0)
1355 {
1356 INFO ("network plugin: Unsigned packet or "
1357 "part has been ignored.");
1358 printed_ignore_warning = 1;
1359 }
1360 buffer = ((char *) buffer) + pkg_length;
1361 continue;
1362 }
1363 #endif /* HAVE_LIBGCRYPT */
1364 else if (pkg_type == TYPE_VALUES)
1365 {
1366 status = parse_part_values (&buffer, &buffer_size,
1367 &vl.values, &vl.values_len);
1368 if (status != 0)
1369 break;
1371 network_dispatch_values (&vl, username);
1373 sfree (vl.values);
1374 }
1375 else if (pkg_type == TYPE_TIME)
1376 {
1377 uint64_t tmp = 0;
1378 status = parse_part_number (&buffer, &buffer_size,
1379 &tmp);
1380 if (status == 0)
1381 {
1382 vl.time = TIME_T_TO_CDTIME_T (tmp);
1383 n.time = TIME_T_TO_CDTIME_T (tmp);
1384 }
1385 }
1386 else if (pkg_type == TYPE_TIME_HR)
1387 {
1388 uint64_t tmp = 0;
1389 status = parse_part_number (&buffer, &buffer_size,
1390 &tmp);
1391 if (status == 0)
1392 {
1393 vl.time = (cdtime_t) tmp;
1394 n.time = (cdtime_t) tmp;
1395 }
1396 }
1397 else if (pkg_type == TYPE_INTERVAL)
1398 {
1399 uint64_t tmp = 0;
1400 status = parse_part_number (&buffer, &buffer_size,
1401 &tmp);
1402 if (status == 0)
1403 vl.interval = TIME_T_TO_CDTIME_T (tmp);
1404 }
1405 else if (pkg_type == TYPE_INTERVAL_HR)
1406 {
1407 uint64_t tmp = 0;
1408 status = parse_part_number (&buffer, &buffer_size,
1409 &tmp);
1410 if (status == 0)
1411 vl.interval = (cdtime_t) tmp;
1412 }
1413 else if (pkg_type == TYPE_HOST)
1414 {
1415 status = parse_part_string (&buffer, &buffer_size,
1416 vl.host, sizeof (vl.host));
1417 if (status == 0)
1418 sstrncpy (n.host, vl.host, sizeof (n.host));
1419 }
1420 else if (pkg_type == TYPE_PLUGIN)
1421 {
1422 status = parse_part_string (&buffer, &buffer_size,
1423 vl.plugin, sizeof (vl.plugin));
1424 if (status == 0)
1425 sstrncpy (n.plugin, vl.plugin,
1426 sizeof (n.plugin));
1427 }
1428 else if (pkg_type == TYPE_PLUGIN_INSTANCE)
1429 {
1430 status = parse_part_string (&buffer, &buffer_size,
1431 vl.plugin_instance,
1432 sizeof (vl.plugin_instance));
1433 if (status == 0)
1434 sstrncpy (n.plugin_instance,
1435 vl.plugin_instance,
1436 sizeof (n.plugin_instance));
1437 }
1438 else if (pkg_type == TYPE_TYPE)
1439 {
1440 status = parse_part_string (&buffer, &buffer_size,
1441 vl.type, sizeof (vl.type));
1442 if (status == 0)
1443 sstrncpy (n.type, vl.type, sizeof (n.type));
1444 }
1445 else if (pkg_type == TYPE_TYPE_INSTANCE)
1446 {
1447 status = parse_part_string (&buffer, &buffer_size,
1448 vl.type_instance,
1449 sizeof (vl.type_instance));
1450 if (status == 0)
1451 sstrncpy (n.type_instance, vl.type_instance,
1452 sizeof (n.type_instance));
1453 }
1454 else if (pkg_type == TYPE_MESSAGE)
1455 {
1456 status = parse_part_string (&buffer, &buffer_size,
1457 n.message, sizeof (n.message));
1459 if (status != 0)
1460 {
1461 /* do nothing */
1462 }
1463 else if ((n.severity != NOTIF_FAILURE)
1464 && (n.severity != NOTIF_WARNING)
1465 && (n.severity != NOTIF_OKAY))
1466 {
1467 INFO ("network plugin: "
1468 "Ignoring notification with "
1469 "unknown severity %i.",
1470 n.severity);
1471 }
1472 else if (n.time <= 0)
1473 {
1474 INFO ("network plugin: "
1475 "Ignoring notification with "
1476 "time == 0.");
1477 }
1478 else if (strlen (n.message) <= 0)
1479 {
1480 INFO ("network plugin: "
1481 "Ignoring notification with "
1482 "an empty message.");
1483 }
1484 else
1485 {
1486 plugin_dispatch_notification (&n);
1487 }
1488 }
1489 else if (pkg_type == TYPE_SEVERITY)
1490 {
1491 uint64_t tmp = 0;
1492 status = parse_part_number (&buffer, &buffer_size,
1493 &tmp);
1494 if (status == 0)
1495 n.severity = (int) tmp;
1496 }
1497 else
1498 {
1499 DEBUG ("network plugin: parse_packet: Unknown part"
1500 " type: 0x%04hx", pkg_type);
1501 buffer = ((char *) buffer) + pkg_length;
1502 }
1503 } /* while (buffer_size > sizeof (part_header_t)) */
1505 if (status == 0 && buffer_size > 0)
1506 WARNING ("network plugin: parse_packet: Received truncated "
1507 "packet, try increasing `MaxPacketSize'");
1509 return (status);
1510 } /* }}} int parse_packet */
1512 static void free_sockent_client (struct sockent_client *sec) /* {{{ */
1513 {
1514 if (sec->fd >= 0)
1515 {
1516 close (sec->fd);
1517 sec->fd = -1;
1518 }
1519 sfree (sec->addr);
1520 #if HAVE_LIBGCRYPT
1521 sfree (sec->username);
1522 sfree (sec->password);
1523 if (sec->cypher != NULL)
1524 gcry_cipher_close (sec->cypher);
1525 #endif
1526 } /* }}} void free_sockent_client */
1528 static void free_sockent_server (struct sockent_server *ses) /* {{{ */
1529 {
1530 size_t i;
1532 for (i = 0; i < ses->fd_num; i++)
1533 {
1534 if (ses->fd[i] >= 0)
1535 {
1536 close (ses->fd[i]);
1537 ses->fd[i] = -1;
1538 }
1539 }
1541 sfree (ses->fd);
1542 #if HAVE_LIBGCRYPT
1543 sfree (ses->auth_file);
1544 fbh_destroy (ses->userdb);
1545 if (ses->cypher != NULL)
1546 gcry_cipher_close (ses->cypher);
1547 #endif
1548 } /* }}} void free_sockent_server */
1550 static void sockent_destroy (sockent_t *se) /* {{{ */
1551 {
1552 sockent_t *next;
1554 DEBUG ("network plugin: sockent_destroy (se = %p);", (void *) se);
1556 while (se != NULL)
1557 {
1558 next = se->next;
1560 sfree (se->node);
1561 sfree (se->service);
1563 if (se->type == SOCKENT_TYPE_CLIENT)
1564 free_sockent_client (&se->data.client);
1565 else
1566 free_sockent_server (&se->data.server);
1568 sfree (se);
1569 se = next;
1570 }
1571 } /* }}} void sockent_destroy */
1573 /*
1574 * int network_set_ttl
1575 *
1576 * Set the `IP_MULTICAST_TTL', `IP_TTL', `IPV6_MULTICAST_HOPS' or
1577 * `IPV6_UNICAST_HOPS', depending on which option is applicable.
1578 *
1579 * The `struct addrinfo' is used to destinguish between unicast and multicast
1580 * sockets.
1581 */
1582 static int network_set_ttl (const sockent_t *se, const struct addrinfo *ai)
1583 {
1584 DEBUG ("network plugin: network_set_ttl: network_config_ttl = %i;",
1585 network_config_ttl);
1587 assert (se->type == SOCKENT_TYPE_CLIENT);
1589 if ((network_config_ttl < 1) || (network_config_ttl > 255))
1590 return (-1);
1592 if (ai->ai_family == AF_INET)
1593 {
1594 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1595 int optname;
1597 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1598 optname = IP_MULTICAST_TTL;
1599 else
1600 optname = IP_TTL;
1602 if (setsockopt (se->data.client.fd, IPPROTO_IP, optname,
1603 &network_config_ttl,
1604 sizeof (network_config_ttl)) != 0)
1605 {
1606 char errbuf[1024];
1607 ERROR ("setsockopt: %s",
1608 sstrerror (errno, errbuf, sizeof (errbuf)));
1609 return (-1);
1610 }
1611 }
1612 else if (ai->ai_family == AF_INET6)
1613 {
1614 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1615 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1616 int optname;
1618 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1619 optname = IPV6_MULTICAST_HOPS;
1620 else
1621 optname = IPV6_UNICAST_HOPS;
1623 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, optname,
1624 &network_config_ttl,
1625 sizeof (network_config_ttl)) != 0)
1626 {
1627 char errbuf[1024];
1628 ERROR ("setsockopt: %s",
1629 sstrerror (errno, errbuf,
1630 sizeof (errbuf)));
1631 return (-1);
1632 }
1633 }
1635 return (0);
1636 } /* int network_set_ttl */
1638 static int network_set_interface (const sockent_t *se, const struct addrinfo *ai) /* {{{ */
1639 {
1640 DEBUG ("network plugin: network_set_interface: interface index = %i;",
1641 se->interface);
1643 assert (se->type == SOCKENT_TYPE_CLIENT);
1645 if (ai->ai_family == AF_INET)
1646 {
1647 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1649 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1650 {
1651 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1652 /* If possible, use the "ip_mreqn" structure which has
1653 * an "interface index" member. Using the interface
1654 * index is preferred here, because of its similarity
1655 * to the way IPv6 handles this. Unfortunately, it
1656 * appears not to be portable. */
1657 struct ip_mreqn mreq;
1659 memset (&mreq, 0, sizeof (mreq));
1660 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1661 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1662 mreq.imr_ifindex = se->interface;
1663 #else
1664 struct ip_mreq mreq;
1666 memset (&mreq, 0, sizeof (mreq));
1667 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1668 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1669 #endif
1671 if (setsockopt (se->data.client.fd, IPPROTO_IP, IP_MULTICAST_IF,
1672 &mreq, sizeof (mreq)) != 0)
1673 {
1674 char errbuf[1024];
1675 ERROR ("setsockopt: %s",
1676 sstrerror (errno, errbuf, sizeof (errbuf)));
1677 return (-1);
1678 }
1680 return (0);
1681 }
1682 }
1683 else if (ai->ai_family == AF_INET6)
1684 {
1685 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1687 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1688 {
1689 if (setsockopt (se->data.client.fd, IPPROTO_IPV6, IPV6_MULTICAST_IF,
1690 &se->interface,
1691 sizeof (se->interface)) != 0)
1692 {
1693 char errbuf[1024];
1694 ERROR ("setsockopt: %s",
1695 sstrerror (errno, errbuf,
1696 sizeof (errbuf)));
1697 return (-1);
1698 }
1700 return (0);
1701 }
1702 }
1704 /* else: Not a multicast interface. */
1705 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1706 if (se->interface != 0)
1707 {
1708 char interface_name[IFNAMSIZ];
1710 if (if_indextoname (se->interface, interface_name) == NULL)
1711 return (-1);
1713 DEBUG ("network plugin: Binding socket to interface %s", interface_name);
1715 if (setsockopt (se->data.client.fd, SOL_SOCKET, SO_BINDTODEVICE,
1716 interface_name,
1717 sizeof(interface_name)) == -1 )
1718 {
1719 char errbuf[1024];
1720 ERROR ("setsockopt: %s",
1721 sstrerror (errno, errbuf, sizeof (errbuf)));
1722 return (-1);
1723 }
1724 }
1725 /* #endif HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1727 #else
1728 WARNING ("network plugin: Cannot set the interface on a unicast "
1729 "socket because "
1730 # if !defined(SO_BINDTODEVICE)
1731 "the the \"SO_BINDTODEVICE\" socket option "
1732 # else
1733 "the \"if_indextoname\" function "
1734 # endif
1735 "is not available on your system.");
1736 #endif
1738 return (0);
1739 } /* }}} network_set_interface */
1741 static int network_bind_socket (int fd, const struct addrinfo *ai, const int interface_idx)
1742 {
1743 int loop = 0;
1744 int yes = 1;
1746 /* allow multiple sockets to use the same PORT number */
1747 if (setsockopt (fd, SOL_SOCKET, SO_REUSEADDR,
1748 &yes, sizeof(yes)) == -1) {
1749 char errbuf[1024];
1750 ERROR ("setsockopt: %s",
1751 sstrerror (errno, errbuf, sizeof (errbuf)));
1752 return (-1);
1753 }
1755 DEBUG ("fd = %i; calling `bind'", fd);
1757 if (bind (fd, ai->ai_addr, ai->ai_addrlen) == -1)
1758 {
1759 char errbuf[1024];
1760 ERROR ("bind: %s",
1761 sstrerror (errno, errbuf, sizeof (errbuf)));
1762 return (-1);
1763 }
1765 if (ai->ai_family == AF_INET)
1766 {
1767 struct sockaddr_in *addr = (struct sockaddr_in *) ai->ai_addr;
1768 if (IN_MULTICAST (ntohl (addr->sin_addr.s_addr)))
1769 {
1770 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1771 struct ip_mreqn mreq;
1772 #else
1773 struct ip_mreq mreq;
1774 #endif
1776 DEBUG ("fd = %i; IPv4 multicast address found", fd);
1778 mreq.imr_multiaddr.s_addr = addr->sin_addr.s_addr;
1779 #if HAVE_STRUCT_IP_MREQN_IMR_IFINDEX
1780 /* Set the interface using the interface index if
1781 * possible (available). Unfortunately, the struct
1782 * ip_mreqn is not portable. */
1783 mreq.imr_address.s_addr = ntohl (INADDR_ANY);
1784 mreq.imr_ifindex = interface_idx;
1785 #else
1786 mreq.imr_interface.s_addr = ntohl (INADDR_ANY);
1787 #endif
1789 if (setsockopt (fd, IPPROTO_IP, IP_MULTICAST_LOOP,
1790 &loop, sizeof (loop)) == -1)
1791 {
1792 char errbuf[1024];
1793 ERROR ("setsockopt: %s",
1794 sstrerror (errno, errbuf,
1795 sizeof (errbuf)));
1796 return (-1);
1797 }
1799 if (setsockopt (fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
1800 &mreq, sizeof (mreq)) == -1)
1801 {
1802 char errbuf[1024];
1803 ERROR ("setsockopt: %s",
1804 sstrerror (errno, errbuf,
1805 sizeof (errbuf)));
1806 return (-1);
1807 }
1809 return (0);
1810 }
1811 }
1812 else if (ai->ai_family == AF_INET6)
1813 {
1814 /* Useful example: http://gsyc.escet.urjc.es/~eva/IPv6-web/examples/mcast.html */
1815 struct sockaddr_in6 *addr = (struct sockaddr_in6 *) ai->ai_addr;
1816 if (IN6_IS_ADDR_MULTICAST (&addr->sin6_addr))
1817 {
1818 struct ipv6_mreq mreq;
1820 DEBUG ("fd = %i; IPv6 multicast address found", fd);
1822 memcpy (&mreq.ipv6mr_multiaddr,
1823 &addr->sin6_addr,
1824 sizeof (addr->sin6_addr));
1826 /* http://developer.apple.com/documentation/Darwin/Reference/ManPages/man4/ip6.4.html
1827 * ipv6mr_interface may be set to zeroes to
1828 * choose the default multicast interface or to
1829 * the index of a particular multicast-capable
1830 * interface if the host is multihomed.
1831 * Membership is associ-associated with a
1832 * single interface; programs running on
1833 * multihomed hosts may need to join the same
1834 * group on more than one interface.*/
1835 mreq.ipv6mr_interface = interface_idx;
1837 if (setsockopt (fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
1838 &loop, sizeof (loop)) == -1)
1839 {
1840 char errbuf[1024];
1841 ERROR ("setsockopt: %s",
1842 sstrerror (errno, errbuf,
1843 sizeof (errbuf)));
1844 return (-1);
1845 }
1847 if (setsockopt (fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP,
1848 &mreq, sizeof (mreq)) == -1)
1849 {
1850 char errbuf[1024];
1851 ERROR ("setsockopt: %s",
1852 sstrerror (errno, errbuf,
1853 sizeof (errbuf)));
1854 return (-1);
1855 }
1857 return (0);
1858 }
1859 }
1861 #if defined(HAVE_IF_INDEXTONAME) && HAVE_IF_INDEXTONAME && defined(SO_BINDTODEVICE)
1862 /* if a specific interface was set, bind the socket to it. But to avoid
1863 * possible problems with multicast routing, only do that for non-multicast
1864 * addresses */
1865 if (interface_idx != 0)
1866 {
1867 char interface_name[IFNAMSIZ];
1869 if (if_indextoname (interface_idx, interface_name) == NULL)
1870 return (-1);
1872 DEBUG ("fd = %i; Binding socket to interface %s", fd, interface_name);
1874 if (setsockopt (fd, SOL_SOCKET, SO_BINDTODEVICE,
1875 interface_name,
1876 sizeof(interface_name)) == -1 )
1877 {
1878 char errbuf[1024];
1879 ERROR ("setsockopt: %s",
1880 sstrerror (errno, errbuf, sizeof (errbuf)));
1881 return (-1);
1882 }
1883 }
1884 #endif /* HAVE_IF_INDEXTONAME && SO_BINDTODEVICE */
1886 return (0);
1887 } /* int network_bind_socket */
1889 /* Initialize a sockent structure. `type' must be either `SOCKENT_TYPE_CLIENT'
1890 * or `SOCKENT_TYPE_SERVER' */
1891 static int sockent_init (sockent_t *se, int type) /* {{{ */
1892 {
1893 if (se == NULL)
1894 return (-1);
1896 memset (se, 0, sizeof (*se));
1898 se->type = SOCKENT_TYPE_CLIENT;
1899 se->node = NULL;
1900 se->service = NULL;
1901 se->interface = 0;
1902 se->next = NULL;
1904 if (type == SOCKENT_TYPE_SERVER)
1905 {
1906 se->type = SOCKENT_TYPE_SERVER;
1907 se->data.server.fd = NULL;
1908 #if HAVE_LIBGCRYPT
1909 se->data.server.security_level = SECURITY_LEVEL_NONE;
1910 se->data.server.auth_file = NULL;
1911 se->data.server.userdb = NULL;
1912 se->data.server.cypher = NULL;
1913 #endif
1914 }
1915 else
1916 {
1917 se->data.client.fd = -1;
1918 se->data.client.addr = NULL;
1919 #if HAVE_LIBGCRYPT
1920 se->data.client.security_level = SECURITY_LEVEL_NONE;
1921 se->data.client.username = NULL;
1922 se->data.client.password = NULL;
1923 se->data.client.cypher = NULL;
1924 #endif
1925 }
1927 return (0);
1928 } /* }}} int sockent_init */
1930 /* Open the file descriptors for a initialized sockent structure. */
1931 static int sockent_open (sockent_t *se) /* {{{ */
1932 {
1933 struct addrinfo ai_hints;
1934 struct addrinfo *ai_list, *ai_ptr;
1935 int ai_return;
1937 const char *node;
1938 const char *service;
1940 if (se == NULL)
1941 return (-1);
1943 /* Set up the security structures. */
1944 #if HAVE_LIBGCRYPT /* {{{ */
1945 if (se->type == SOCKENT_TYPE_CLIENT)
1946 {
1947 if (se->data.client.security_level > SECURITY_LEVEL_NONE)
1948 {
1949 if ((se->data.client.username == NULL)
1950 || (se->data.client.password == NULL))
1951 {
1952 ERROR ("network plugin: Client socket with "
1953 "security requested, but no "
1954 "credentials are configured.");
1955 return (-1);
1956 }
1957 gcry_md_hash_buffer (GCRY_MD_SHA256,
1958 se->data.client.password_hash,
1959 se->data.client.password,
1960 strlen (se->data.client.password));
1961 }
1962 }
1963 else /* (se->type == SOCKENT_TYPE_SERVER) */
1964 {
1965 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1966 {
1967 if (se->data.server.auth_file == NULL)
1968 {
1969 ERROR ("network plugin: Server socket with "
1970 "security requested, but no "
1971 "password file is configured.");
1972 return (-1);
1973 }
1974 }
1975 if (se->data.server.auth_file != NULL)
1976 {
1977 se->data.server.userdb = fbh_create (se->data.server.auth_file);
1978 if (se->data.server.userdb == NULL)
1979 {
1980 ERROR ("network plugin: Reading password file "
1981 "`%s' failed.",
1982 se->data.server.auth_file);
1983 if (se->data.server.security_level > SECURITY_LEVEL_NONE)
1984 return (-1);
1985 }
1986 }
1987 }
1988 #endif /* }}} HAVE_LIBGCRYPT */
1990 node = se->node;
1991 service = se->service;
1993 if (service == NULL)
1994 service = NET_DEFAULT_PORT;
1996 DEBUG ("network plugin: sockent_open: node = %s; service = %s;",
1997 node, service);
1999 memset (&ai_hints, 0, sizeof (ai_hints));
2000 ai_hints.ai_flags = 0;
2001 #ifdef AI_PASSIVE
2002 ai_hints.ai_flags |= AI_PASSIVE;
2003 #endif
2004 #ifdef AI_ADDRCONFIG
2005 ai_hints.ai_flags |= AI_ADDRCONFIG;
2006 #endif
2007 ai_hints.ai_family = AF_UNSPEC;
2008 ai_hints.ai_socktype = SOCK_DGRAM;
2009 ai_hints.ai_protocol = IPPROTO_UDP;
2011 ai_return = getaddrinfo (node, service, &ai_hints, &ai_list);
2012 if (ai_return != 0)
2013 {
2014 ERROR ("network plugin: getaddrinfo (%s, %s) failed: %s",
2015 (se->node == NULL) ? "(null)" : se->node,
2016 (se->service == NULL) ? "(null)" : se->service,
2017 gai_strerror (ai_return));
2018 return (-1);
2019 }
2021 for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
2022 {
2023 int status;
2025 if (se->type == SOCKENT_TYPE_SERVER) /* {{{ */
2026 {
2027 int *tmp;
2029 tmp = realloc (se->data.server.fd,
2030 sizeof (*tmp) * (se->data.server.fd_num + 1));
2031 if (tmp == NULL)
2032 {
2033 ERROR ("network plugin: realloc failed.");
2034 continue;
2035 }
2036 se->data.server.fd = tmp;
2037 tmp = se->data.server.fd + se->data.server.fd_num;
2039 *tmp = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
2040 ai_ptr->ai_protocol);
2041 if (*tmp < 0)
2042 {
2043 char errbuf[1024];
2044 ERROR ("network plugin: socket(2) failed: %s",
2045 sstrerror (errno, errbuf,
2046 sizeof (errbuf)));
2047 continue;
2048 }
2050 status = network_bind_socket (*tmp, ai_ptr, se->interface);
2051 if (status != 0)
2052 {
2053 close (*tmp);
2054 *tmp = -1;
2055 continue;
2056 }
2058 se->data.server.fd_num++;
2059 continue;
2060 } /* }}} if (se->type == SOCKENT_TYPE_SERVER) */
2061 else /* if (se->type == SOCKENT_TYPE_CLIENT) {{{ */
2062 {
2063 se->data.client.fd = socket (ai_ptr->ai_family,
2064 ai_ptr->ai_socktype,
2065 ai_ptr->ai_protocol);
2066 if (se->data.client.fd < 0)
2067 {
2068 char errbuf[1024];
2069 ERROR ("network plugin: socket(2) failed: %s",
2070 sstrerror (errno, errbuf,
2071 sizeof (errbuf)));
2072 continue;
2073 }
2075 se->data.client.addr = malloc (sizeof (*se->data.client.addr));
2076 if (se->data.client.addr == NULL)
2077 {
2078 ERROR ("network plugin: malloc failed.");
2079 close (se->data.client.fd);
2080 se->data.client.fd = -1;
2081 continue;
2082 }
2084 memset (se->data.client.addr, 0, sizeof (*se->data.client.addr));
2085 assert (sizeof (*se->data.client.addr) >= ai_ptr->ai_addrlen);
2086 memcpy (se->data.client.addr, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
2087 se->data.client.addrlen = ai_ptr->ai_addrlen;
2089 network_set_ttl (se, ai_ptr);
2090 network_set_interface (se, ai_ptr);
2092 /* We don't open more than one write-socket per
2093 * node/service pair.. */
2094 break;
2095 } /* }}} if (se->type == SOCKENT_TYPE_CLIENT) */
2096 } /* for (ai_list) */
2098 freeaddrinfo (ai_list);
2100 /* Check if all went well. */
2101 if (se->type == SOCKENT_TYPE_SERVER)
2102 {
2103 if (se->data.server.fd_num <= 0)
2104 return (-1);
2105 }
2106 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2107 {
2108 if (se->data.client.fd < 0)
2109 return (-1);
2110 }
2112 return (0);
2113 } /* }}} int sockent_open */
2115 /* Add a sockent to the global list of sockets */
2116 static int sockent_add (sockent_t *se) /* {{{ */
2117 {
2118 sockent_t *last_ptr;
2120 if (se == NULL)
2121 return (-1);
2123 if (se->type == SOCKENT_TYPE_SERVER)
2124 {
2125 struct pollfd *tmp;
2126 size_t i;
2128 tmp = realloc (listen_sockets_pollfd,
2129 sizeof (*tmp) * (listen_sockets_num
2130 + se->data.server.fd_num));
2131 if (tmp == NULL)
2132 {
2133 ERROR ("network plugin: realloc failed.");
2134 return (-1);
2135 }
2136 listen_sockets_pollfd = tmp;
2137 tmp = listen_sockets_pollfd + listen_sockets_num;
2139 for (i = 0; i < se->data.server.fd_num; i++)
2140 {
2141 memset (tmp + i, 0, sizeof (*tmp));
2142 tmp[i].fd = se->data.server.fd[i];
2143 tmp[i].events = POLLIN | POLLPRI;
2144 tmp[i].revents = 0;
2145 }
2147 listen_sockets_num += se->data.server.fd_num;
2149 if (listen_sockets == NULL)
2150 {
2151 listen_sockets = se;
2152 return (0);
2153 }
2154 last_ptr = listen_sockets;
2155 }
2156 else /* if (se->type == SOCKENT_TYPE_CLIENT) */
2157 {
2158 if (sending_sockets == NULL)
2159 {
2160 sending_sockets = se;
2161 return (0);
2162 }
2163 last_ptr = sending_sockets;
2164 }
2166 while (last_ptr->next != NULL)
2167 last_ptr = last_ptr->next;
2168 last_ptr->next = se;
2170 return (0);
2171 } /* }}} int sockent_add */
2173 static void *dispatch_thread (void __attribute__((unused)) *arg) /* {{{ */
2174 {
2175 while (42)
2176 {
2177 receive_list_entry_t *ent;
2178 sockent_t *se;
2180 /* Lock and wait for more data to come in */
2181 pthread_mutex_lock (&receive_list_lock);
2182 while ((listen_loop == 0)
2183 && (receive_list_head == NULL))
2184 pthread_cond_wait (&receive_list_cond, &receive_list_lock);
2186 /* Remove the head entry and unlock */
2187 ent = receive_list_head;
2188 if (ent != NULL)
2189 receive_list_head = ent->next;
2190 receive_list_length--;
2191 pthread_mutex_unlock (&receive_list_lock);
2193 /* Check whether we are supposed to exit. We do NOT check `listen_loop'
2194 * because we dispatch all missing packets before shutting down. */
2195 if (ent == NULL)
2196 break;
2198 /* Look for the correct `sockent_t' */
2199 se = listen_sockets;
2200 while (se != NULL)
2201 {
2202 size_t i;
2204 for (i = 0; i < se->data.server.fd_num; i++)
2205 if (se->data.server.fd[i] == ent->fd)
2206 break;
2208 if (i < se->data.server.fd_num)
2209 break;
2211 se = se->next;
2212 }
2214 if (se == NULL)
2215 {
2216 ERROR ("network plugin: Got packet from FD %i, but can't "
2217 "find an appropriate socket entry.",
2218 ent->fd);
2219 sfree (ent->data);
2220 sfree (ent);
2221 continue;
2222 }
2224 parse_packet (se, ent->data, ent->data_len, /* flags = */ 0,
2225 /* username = */ NULL);
2226 sfree (ent->data);
2227 sfree (ent);
2228 } /* while (42) */
2230 return (NULL);
2231 } /* }}} void *dispatch_thread */
2233 static int network_receive (void) /* {{{ */
2234 {
2235 char buffer[network_config_packet_size];
2236 int buffer_len;
2238 int i;
2239 int status;
2241 receive_list_entry_t *private_list_head;
2242 receive_list_entry_t *private_list_tail;
2243 uint64_t private_list_length;
2245 assert (listen_sockets_num > 0);
2247 private_list_head = NULL;
2248 private_list_tail = NULL;
2249 private_list_length = 0;
2251 while (listen_loop == 0)
2252 {
2253 status = poll (listen_sockets_pollfd, listen_sockets_num, -1);
2255 if (status <= 0)
2256 {
2257 char errbuf[1024];
2258 if (errno == EINTR)
2259 continue;
2260 ERROR ("poll failed: %s",
2261 sstrerror (errno, errbuf, sizeof (errbuf)));
2262 return (-1);
2263 }
2265 for (i = 0; (i < listen_sockets_num) && (status > 0); i++)
2266 {
2267 receive_list_entry_t *ent;
2269 if ((listen_sockets_pollfd[i].revents
2270 & (POLLIN | POLLPRI)) == 0)
2271 continue;
2272 status--;
2274 buffer_len = recv (listen_sockets_pollfd[i].fd,
2275 buffer, sizeof (buffer),
2276 0 /* no flags */);
2277 if (buffer_len < 0)
2278 {
2279 char errbuf[1024];
2280 ERROR ("recv failed: %s",
2281 sstrerror (errno, errbuf,
2282 sizeof (errbuf)));
2283 return (-1);
2284 }
2286 stats_octets_rx += ((uint64_t) buffer_len);
2287 stats_packets_rx++;
2289 /* TODO: Possible performance enhancement: Do not free
2290 * these entries in the dispatch thread but put them in
2291 * another list, so we don't have to allocate more and
2292 * more of these structures. */
2293 ent = malloc (sizeof (receive_list_entry_t));
2294 if (ent == NULL)
2295 {
2296 ERROR ("network plugin: malloc failed.");
2297 return (-1);
2298 }
2299 memset (ent, 0, sizeof (receive_list_entry_t));
2300 ent->data = malloc (network_config_packet_size);
2301 if (ent->data == NULL)
2302 {
2303 sfree (ent);
2304 ERROR ("network plugin: malloc failed.");
2305 return (-1);
2306 }
2307 ent->fd = listen_sockets_pollfd[i].fd;
2308 ent->next = NULL;
2310 memcpy (ent->data, buffer, buffer_len);
2311 ent->data_len = buffer_len;
2313 if (private_list_head == NULL)
2314 private_list_head = ent;
2315 else
2316 private_list_tail->next = ent;
2317 private_list_tail = ent;
2318 private_list_length++;
2320 /* Do not block here. Blocking here has led to
2321 * insufficient performance in the past. */
2322 if (pthread_mutex_trylock (&receive_list_lock) == 0)
2323 {
2324 assert (((receive_list_head == NULL) && (receive_list_length == 0))
2325 || ((receive_list_head != NULL) && (receive_list_length != 0)));
2327 if (receive_list_head == NULL)
2328 receive_list_head = private_list_head;
2329 else
2330 receive_list_tail->next = private_list_head;
2331 receive_list_tail = private_list_tail;
2332 receive_list_length += private_list_length;
2334 pthread_cond_signal (&receive_list_cond);
2335 pthread_mutex_unlock (&receive_list_lock);
2337 private_list_head = NULL;
2338 private_list_tail = NULL;
2339 private_list_length = 0;
2340 }
2341 } /* for (listen_sockets_pollfd) */
2342 } /* while (listen_loop == 0) */
2344 /* Make sure everything is dispatched before exiting. */
2345 if (private_list_head != NULL)
2346 {
2347 pthread_mutex_lock (&receive_list_lock);
2349 if (receive_list_head == NULL)
2350 receive_list_head = private_list_head;
2351 else
2352 receive_list_tail->next = private_list_head;
2353 receive_list_tail = private_list_tail;
2354 receive_list_length += private_list_length;
2356 private_list_head = NULL;
2357 private_list_tail = NULL;
2358 private_list_length = 0;
2360 pthread_cond_signal (&receive_list_cond);
2361 pthread_mutex_unlock (&receive_list_lock);
2362 }
2364 return (0);
2365 } /* }}} int network_receive */
2367 static void *receive_thread (void __attribute__((unused)) *arg)
2368 {
2369 return (network_receive () ? (void *) 1 : (void *) 0);
2370 } /* void *receive_thread */
2372 static void network_init_buffer (void)
2373 {
2374 memset (send_buffer, 0, network_config_packet_size);
2375 send_buffer_ptr = send_buffer;
2376 send_buffer_fill = 0;
2378 memset (&send_buffer_vl, 0, sizeof (send_buffer_vl));
2379 } /* int network_init_buffer */
2381 static void networt_send_buffer_plain (const sockent_t *se, /* {{{ */
2382 const char *buffer, size_t buffer_size)
2383 {
2384 int status;
2386 while (42)
2387 {
2388 status = sendto (se->data.client.fd, buffer, buffer_size,
2389 /* flags = */ 0,
2390 (struct sockaddr *) se->data.client.addr,
2391 se->data.client.addrlen);
2392 if (status < 0)
2393 {
2394 char errbuf[1024];
2395 if (errno == EINTR)
2396 continue;
2397 ERROR ("network plugin: sendto failed: %s",
2398 sstrerror (errno, errbuf,
2399 sizeof (errbuf)));
2400 break;
2401 }
2403 break;
2404 } /* while (42) */
2405 } /* }}} void networt_send_buffer_plain */
2407 #if HAVE_LIBGCRYPT
2408 #define BUFFER_ADD(p,s) do { \
2409 memcpy (buffer + buffer_offset, (p), (s)); \
2410 buffer_offset += (s); \
2411 } while (0)
2413 static void networt_send_buffer_signed (const sockent_t *se, /* {{{ */
2414 const char *in_buffer, size_t in_buffer_size)
2415 {
2416 part_signature_sha256_t ps;
2417 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2418 size_t buffer_offset;
2419 size_t username_len;
2421 gcry_md_hd_t hd;
2422 gcry_error_t err;
2423 unsigned char *hash;
2425 hd = NULL;
2426 err = gcry_md_open (&hd, GCRY_MD_SHA256, GCRY_MD_FLAG_HMAC);
2427 if (err != 0)
2428 {
2429 ERROR ("network plugin: Creating HMAC object failed: %s",
2430 gcry_strerror (err));
2431 return;
2432 }
2434 err = gcry_md_setkey (hd, se->data.client.password,
2435 strlen (se->data.client.password));
2436 if (err != 0)
2437 {
2438 ERROR ("network plugin: gcry_md_setkey failed: %s",
2439 gcry_strerror (err));
2440 gcry_md_close (hd);
2441 return;
2442 }
2444 username_len = strlen (se->data.client.username);
2445 if (username_len > (BUFF_SIG_SIZE - PART_SIGNATURE_SHA256_SIZE))
2446 {
2447 ERROR ("network plugin: Username too long: %s",
2448 se->data.client.username);
2449 return;
2450 }
2452 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE,
2453 se->data.client.username, username_len);
2454 memcpy (buffer + PART_SIGNATURE_SHA256_SIZE + username_len,
2455 in_buffer, in_buffer_size);
2457 /* Initialize the `ps' structure. */
2458 memset (&ps, 0, sizeof (ps));
2459 ps.head.type = htons (TYPE_SIGN_SHA256);
2460 ps.head.length = htons (PART_SIGNATURE_SHA256_SIZE + username_len);
2462 /* Calculate the hash value. */
2463 gcry_md_write (hd, buffer + PART_SIGNATURE_SHA256_SIZE,
2464 username_len + in_buffer_size);
2465 hash = gcry_md_read (hd, GCRY_MD_SHA256);
2466 if (hash == NULL)
2467 {
2468 ERROR ("network plugin: gcry_md_read failed.");
2469 gcry_md_close (hd);
2470 return;
2471 }
2472 memcpy (ps.hash, hash, sizeof (ps.hash));
2474 /* Add the header */
2475 buffer_offset = 0;
2477 BUFFER_ADD (&ps.head.type, sizeof (ps.head.type));
2478 BUFFER_ADD (&ps.head.length, sizeof (ps.head.length));
2479 BUFFER_ADD (ps.hash, sizeof (ps.hash));
2481 assert (buffer_offset == PART_SIGNATURE_SHA256_SIZE);
2483 gcry_md_close (hd);
2484 hd = NULL;
2486 buffer_offset = PART_SIGNATURE_SHA256_SIZE + username_len + in_buffer_size;
2487 networt_send_buffer_plain (se, buffer, buffer_offset);
2488 } /* }}} void networt_send_buffer_signed */
2490 static void networt_send_buffer_encrypted (sockent_t *se, /* {{{ */
2491 const char *in_buffer, size_t in_buffer_size)
2492 {
2493 part_encryption_aes256_t pea;
2494 char buffer[BUFF_SIG_SIZE + in_buffer_size];
2495 size_t buffer_size;
2496 size_t buffer_offset;
2497 size_t header_size;
2498 size_t username_len;
2499 gcry_error_t err;
2500 gcry_cipher_hd_t cypher;
2502 /* Initialize the header fields */
2503 memset (&pea, 0, sizeof (pea));
2504 pea.head.type = htons (TYPE_ENCR_AES256);
2506 pea.username = se->data.client.username;
2508 username_len = strlen (pea.username);
2509 if ((PART_ENCRYPTION_AES256_SIZE + username_len) > BUFF_SIG_SIZE)
2510 {
2511 ERROR ("network plugin: Username too long: %s", pea.username);
2512 return;
2513 }
2515 buffer_size = PART_ENCRYPTION_AES256_SIZE + username_len + in_buffer_size;
2516 header_size = PART_ENCRYPTION_AES256_SIZE + username_len
2517 - sizeof (pea.hash);
2519 assert (buffer_size <= sizeof (buffer));
2520 DEBUG ("network plugin: networt_send_buffer_encrypted: "
2521 "buffer_size = %zu;", buffer_size);
2523 pea.head.length = htons ((uint16_t) (PART_ENCRYPTION_AES256_SIZE
2524 + username_len + in_buffer_size));
2525 pea.username_length = htons ((uint16_t) username_len);
2527 /* Chose a random initialization vector. */
2528 gcry_randomize ((void *) &pea.iv, sizeof (pea.iv), GCRY_STRONG_RANDOM);
2530 /* Create hash of the payload */
2531 gcry_md_hash_buffer (GCRY_MD_SHA1, pea.hash, in_buffer, in_buffer_size);
2533 /* Initialize the buffer */
2534 buffer_offset = 0;
2535 memset (buffer, 0, sizeof (buffer));
2538 BUFFER_ADD (&pea.head.type, sizeof (pea.head.type));
2539 BUFFER_ADD (&pea.head.length, sizeof (pea.head.length));
2540 BUFFER_ADD (&pea.username_length, sizeof (pea.username_length));
2541 BUFFER_ADD (pea.username, username_len);
2542 BUFFER_ADD (pea.iv, sizeof (pea.iv));
2543 assert (buffer_offset == header_size);
2544 BUFFER_ADD (pea.hash, sizeof (pea.hash));
2545 BUFFER_ADD (in_buffer, in_buffer_size);
2547 assert (buffer_offset == buffer_size);
2549 cypher = network_get_aes256_cypher (se, pea.iv, sizeof (pea.iv),
2550 se->data.client.password);
2551 if (cypher == NULL)
2552 return;
2554 /* Encrypt the buffer in-place */
2555 err = gcry_cipher_encrypt (cypher,
2556 buffer + header_size,
2557 buffer_size - header_size,
2558 /* in = */ NULL, /* in len = */ 0);
2559 if (err != 0)
2560 {
2561 ERROR ("network plugin: gcry_cipher_encrypt returned: %s",
2562 gcry_strerror (err));
2563 return;
2564 }
2566 /* Send it out without further modifications */
2567 networt_send_buffer_plain (se, buffer, buffer_size);
2568 } /* }}} void networt_send_buffer_encrypted */
2569 #undef BUFFER_ADD
2570 #endif /* HAVE_LIBGCRYPT */
2572 static void network_send_buffer (char *buffer, size_t buffer_len) /* {{{ */
2573 {
2574 sockent_t *se;
2576 DEBUG ("network plugin: network_send_buffer: buffer_len = %zu", buffer_len);
2578 for (se = sending_sockets; se != NULL; se = se->next)
2579 {
2580 #if HAVE_LIBGCRYPT
2581 if (se->data.client.security_level == SECURITY_LEVEL_ENCRYPT)
2582 networt_send_buffer_encrypted (se, buffer, buffer_len);
2583 else if (se->data.client.security_level == SECURITY_LEVEL_SIGN)
2584 networt_send_buffer_signed (se, buffer, buffer_len);
2585 else /* if (se->data.client.security_level == SECURITY_LEVEL_NONE) */
2586 #endif /* HAVE_LIBGCRYPT */
2587 networt_send_buffer_plain (se, buffer, buffer_len);
2588 } /* for (sending_sockets) */
2589 } /* }}} void network_send_buffer */
2591 static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */
2592 value_list_t *vl_def,
2593 const data_set_t *ds, const value_list_t *vl)
2594 {
2595 char *buffer_orig = buffer;
2597 if (strcmp (vl_def->host, vl->host) != 0)
2598 {
2599 if (write_part_string (&buffer, &buffer_size, TYPE_HOST,
2600 vl->host, strlen (vl->host)) != 0)
2601 return (-1);
2602 sstrncpy (vl_def->host, vl->host, sizeof (vl_def->host));
2603 }
2605 if (vl_def->time != vl->time)
2606 {
2607 if (write_part_number (&buffer, &buffer_size, TYPE_TIME_HR,
2608 (uint64_t) vl->time))
2609 return (-1);
2610 vl_def->time = vl->time;
2611 }
2613 if (vl_def->interval != vl->interval)
2614 {
2615 if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL_HR,
2616 (uint64_t) vl->interval))
2617 return (-1);
2618 vl_def->interval = vl->interval;
2619 }
2621 if (strcmp (vl_def->plugin, vl->plugin) != 0)
2622 {
2623 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN,
2624 vl->plugin, strlen (vl->plugin)) != 0)
2625 return (-1);
2626 sstrncpy (vl_def->plugin, vl->plugin, sizeof (vl_def->plugin));
2627 }
2629 if (strcmp (vl_def->plugin_instance, vl->plugin_instance) != 0)
2630 {
2631 if (write_part_string (&buffer, &buffer_size, TYPE_PLUGIN_INSTANCE,
2632 vl->plugin_instance,
2633 strlen (vl->plugin_instance)) != 0)
2634 return (-1);
2635 sstrncpy (vl_def->plugin_instance, vl->plugin_instance, sizeof (vl_def->plugin_instance));
2636 }
2638 if (strcmp (vl_def->type, vl->type) != 0)
2639 {
2640 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE,
2641 vl->type, strlen (vl->type)) != 0)
2642 return (-1);
2643 sstrncpy (vl_def->type, ds->type, sizeof (vl_def->type));
2644 }
2646 if (strcmp (vl_def->type_instance, vl->type_instance) != 0)
2647 {
2648 if (write_part_string (&buffer, &buffer_size, TYPE_TYPE_INSTANCE,
2649 vl->type_instance,
2650 strlen (vl->type_instance)) != 0)
2651 return (-1);
2652 sstrncpy (vl_def->type_instance, vl->type_instance, sizeof (vl_def->type_instance));
2653 }
2655 if (write_part_values (&buffer, &buffer_size, ds, vl) != 0)
2656 return (-1);
2658 return (buffer - buffer_orig);
2659 } /* }}} int add_to_buffer */
2661 static void flush_buffer (void)
2662 {
2663 DEBUG ("network plugin: flush_buffer: send_buffer_fill = %i",
2664 send_buffer_fill);
2666 network_send_buffer (send_buffer, (size_t) send_buffer_fill);
2668 stats_octets_tx += ((uint64_t) send_buffer_fill);
2669 stats_packets_tx++;
2671 network_init_buffer ();
2672 }
2674 static int network_write (const data_set_t *ds, const value_list_t *vl,
2675 user_data_t __attribute__((unused)) *user_data)
2676 {
2677 int status;
2679 if (!check_send_okay (vl))
2680 {
2681 #if COLLECT_DEBUG
2682 char name[6*DATA_MAX_NAME_LEN];
2683 FORMAT_VL (name, sizeof (name), vl);
2684 name[sizeof (name) - 1] = 0;
2685 DEBUG ("network plugin: network_write: "
2686 "NOT sending %s.", name);
2687 #endif
2688 /* Counter is not protected by another lock and may be reached by
2689 * multiple threads */
2690 pthread_mutex_lock (&stats_lock);
2691 stats_values_not_sent++;
2692 pthread_mutex_unlock (&stats_lock);
2693 return (0);
2694 }
2696 uc_meta_data_add_unsigned_int (vl,
2697 "network:time_sent", (uint64_t) vl->time);
2699 pthread_mutex_lock (&send_buffer_lock);
2701 status = add_to_buffer (send_buffer_ptr,
2702 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2703 &send_buffer_vl,
2704 ds, vl);
2705 if (status >= 0)
2706 {
2707 /* status == bytes added to the buffer */
2708 send_buffer_fill += status;
2709 send_buffer_ptr += status;
2711 stats_values_sent++;
2712 }
2713 else
2714 {
2715 flush_buffer ();
2717 status = add_to_buffer (send_buffer_ptr,
2718 network_config_packet_size - (send_buffer_fill + BUFF_SIG_SIZE),
2719 &send_buffer_vl,
2720 ds, vl);
2722 if (status >= 0)
2723 {
2724 send_buffer_fill += status;
2725 send_buffer_ptr += status;
2727 stats_values_sent++;
2728 }
2729 }
2731 if (status < 0)
2732 {
2733 ERROR ("network plugin: Unable to append to the "
2734 "buffer for some weird reason");
2735 }
2736 else if ((network_config_packet_size - send_buffer_fill) < 15)
2737 {
2738 flush_buffer ();
2739 }
2741 pthread_mutex_unlock (&send_buffer_lock);
2743 return ((status < 0) ? -1 : 0);
2744 } /* int network_write */
2746 static int network_config_set_boolean (const oconfig_item_t *ci, /* {{{ */
2747 int *retval)
2748 {
2749 if ((ci->values_num != 1)
2750 || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN)
2751 && (ci->values[0].type != OCONFIG_TYPE_STRING)))
2752 {
2753 ERROR ("network plugin: The `%s' config option needs "
2754 "exactly one boolean argument.", ci->key);
2755 return (-1);
2756 }
2758 if (ci->values[0].type == OCONFIG_TYPE_BOOLEAN)
2759 {
2760 if (ci->values[0].value.boolean)
2761 *retval = 1;
2762 else
2763 *retval = 0;
2764 }
2765 else
2766 {
2767 char *str = ci->values[0].value.string;
2769 if (IS_TRUE (str))
2770 *retval = 1;
2771 else if (IS_FALSE (str))
2772 *retval = 0;
2773 else
2774 {
2775 ERROR ("network plugin: Cannot parse string value `%s' of the `%s' "
2776 "option as boolean value.",
2777 str, ci->key);
2778 return (-1);
2779 }
2780 }
2782 return (0);
2783 } /* }}} int network_config_set_boolean */
2785 static int network_config_set_ttl (const oconfig_item_t *ci) /* {{{ */
2786 {
2787 int tmp;
2788 if ((ci->values_num != 1)
2789 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2790 {
2791 WARNING ("network plugin: The `TimeToLive' config option needs exactly "
2792 "one numeric argument.");
2793 return (-1);
2794 }
2796 tmp = (int) ci->values[0].value.number;
2797 if ((tmp > 0) && (tmp <= 255))
2798 network_config_ttl = tmp;
2800 return (0);
2801 } /* }}} int network_config_set_ttl */
2803 static int network_config_set_interface (const oconfig_item_t *ci, /* {{{ */
2804 int *interface)
2805 {
2806 if ((ci->values_num != 1)
2807 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2808 {
2809 WARNING ("network plugin: The `Interface' config option needs exactly "
2810 "one string argument.");
2811 return (-1);
2812 }
2814 if (interface == NULL)
2815 return (-1);
2817 *interface = if_nametoindex (ci->values[0].value.string);
2819 return (0);
2820 } /* }}} int network_config_set_interface */
2822 static int network_config_set_buffer_size (const oconfig_item_t *ci) /* {{{ */
2823 {
2824 int tmp;
2825 if ((ci->values_num != 1)
2826 || (ci->values[0].type != OCONFIG_TYPE_NUMBER))
2827 {
2828 WARNING ("network plugin: The `MaxPacketSize' config option needs exactly "
2829 "one numeric argument.");
2830 return (-1);
2831 }
2833 tmp = (int) ci->values[0].value.number;
2834 if ((tmp >= 1024) && (tmp <= 65535))
2835 network_config_packet_size = tmp;
2837 return (0);
2838 } /* }}} int network_config_set_buffer_size */
2840 #if HAVE_LIBGCRYPT
2841 static int network_config_set_string (const oconfig_item_t *ci, /* {{{ */
2842 char **ret_string)
2843 {
2844 char *tmp;
2845 if ((ci->values_num != 1)
2846 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2847 {
2848 WARNING ("network plugin: The `%s' config option needs exactly "
2849 "one string argument.", ci->key);
2850 return (-1);
2851 }
2853 tmp = strdup (ci->values[0].value.string);
2854 if (tmp == NULL)
2855 return (-1);
2857 sfree (*ret_string);
2858 *ret_string = tmp;
2860 return (0);
2861 } /* }}} int network_config_set_string */
2862 #endif /* HAVE_LIBGCRYPT */
2864 #if HAVE_LIBGCRYPT
2865 static int network_config_set_security_level (oconfig_item_t *ci, /* {{{ */
2866 int *retval)
2867 {
2868 char *str;
2869 if ((ci->values_num != 1)
2870 || (ci->values[0].type != OCONFIG_TYPE_STRING))
2871 {
2872 WARNING ("network plugin: The `SecurityLevel' config option needs exactly "
2873 "one string argument.");
2874 return (-1);
2875 }
2877 str = ci->values[0].value.string;
2878 if (strcasecmp ("Encrypt", str) == 0)
2879 *retval = SECURITY_LEVEL_ENCRYPT;
2880 else if (strcasecmp ("Sign", str) == 0)
2881 *retval = SECURITY_LEVEL_SIGN;
2882 else if (strcasecmp ("None", str) == 0)
2883 *retval = SECURITY_LEVEL_NONE;
2884 else
2885 {
2886 WARNING ("network plugin: Unknown security level: %s.", str);
2887 return (-1);
2888 }
2890 return (0);
2891 } /* }}} int network_config_set_security_level */
2892 #endif /* HAVE_LIBGCRYPT */
2894 static int network_config_add_listen (const oconfig_item_t *ci) /* {{{ */
2895 {
2896 sockent_t *se;
2897 int status;
2898 int i;
2900 if ((ci->values_num < 1) || (ci->values_num > 2)
2901 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2902 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2903 {
2904 ERROR ("network plugin: The `%s' config option needs "
2905 "one or two string arguments.", ci->key);
2906 return (-1);
2907 }
2909 se = malloc (sizeof (*se));
2910 if (se == NULL)
2911 {
2912 ERROR ("network plugin: malloc failed.");
2913 return (-1);
2914 }
2915 sockent_init (se, SOCKENT_TYPE_SERVER);
2917 se->node = strdup (ci->values[0].value.string);
2918 if (ci->values_num >= 2)
2919 se->service = strdup (ci->values[1].value.string);
2921 for (i = 0; i < ci->children_num; i++)
2922 {
2923 oconfig_item_t *child = ci->children + i;
2925 #if HAVE_LIBGCRYPT
2926 if (strcasecmp ("AuthFile", child->key) == 0)
2927 network_config_set_string (child, &se->data.server.auth_file);
2928 else if (strcasecmp ("SecurityLevel", child->key) == 0)
2929 network_config_set_security_level (child,
2930 &se->data.server.security_level);
2931 else
2932 #endif /* HAVE_LIBGCRYPT */
2933 if (strcasecmp ("Interface", child->key) == 0)
2934 network_config_set_interface (child,
2935 &se->interface);
2936 else
2937 {
2938 WARNING ("network plugin: Option `%s' is not allowed here.",
2939 child->key);
2940 }
2941 }
2943 #if HAVE_LIBGCRYPT
2944 if ((se->data.server.security_level > SECURITY_LEVEL_NONE)
2945 && (se->data.server.auth_file == NULL))
2946 {
2947 ERROR ("network plugin: A security level higher than `none' was "
2948 "requested, but no AuthFile option was given. Cowardly refusing to "
2949 "open this socket!");
2950 sockent_destroy (se);
2951 return (-1);
2952 }
2953 #endif /* HAVE_LIBGCRYPT */
2955 status = sockent_open (se);
2956 if (status != 0)
2957 {
2958 ERROR ("network plugin: network_config_add_listen: sockent_open failed.");
2959 sockent_destroy (se);
2960 return (-1);
2961 }
2963 status = sockent_add (se);
2964 if (status != 0)
2965 {
2966 ERROR ("network plugin: network_config_add_listen: sockent_add failed.");
2967 sockent_destroy (se);
2968 return (-1);
2969 }
2971 return (0);
2972 } /* }}} int network_config_add_listen */
2974 static int network_config_add_server (const oconfig_item_t *ci) /* {{{ */
2975 {
2976 sockent_t *se;
2977 int status;
2978 int i;
2980 if ((ci->values_num < 1) || (ci->values_num > 2)
2981 || (ci->values[0].type != OCONFIG_TYPE_STRING)
2982 || ((ci->values_num > 1) && (ci->values[1].type != OCONFIG_TYPE_STRING)))
2983 {
2984 ERROR ("network plugin: The `%s' config option needs "
2985 "one or two string arguments.", ci->key);
2986 return (-1);
2987 }
2989 se = malloc (sizeof (*se));
2990 if (se == NULL)
2991 {
2992 ERROR ("network plugin: malloc failed.");
2993 return (-1);
2994 }
2995 sockent_init (se, SOCKENT_TYPE_CLIENT);
2997 se->node = strdup (ci->values[0].value.string);
2998 if (ci->values_num >= 2)
2999 se->service = strdup (ci->values[1].value.string);
3001 for (i = 0; i < ci->children_num; i++)
3002 {
3003 oconfig_item_t *child = ci->children + i;
3005 #if HAVE_LIBGCRYPT
3006 if (strcasecmp ("Username", child->key) == 0)
3007 network_config_set_string (child, &se->data.client.username);
3008 else if (strcasecmp ("Password", child->key) == 0)
3009 network_config_set_string (child, &se->data.client.password);
3010 else if (strcasecmp ("SecurityLevel", child->key) == 0)
3011 network_config_set_security_level (child,
3012 &se->data.client.security_level);
3013 else
3014 #endif /* HAVE_LIBGCRYPT */
3015 if (strcasecmp ("Interface", child->key) == 0)
3016 network_config_set_interface (child,
3017 &se->interface);
3018 else
3019 {
3020 WARNING ("network plugin: Option `%s' is not allowed here.",
3021 child->key);
3022 }
3023 }
3025 #if HAVE_LIBGCRYPT
3026 if ((se->data.client.security_level > SECURITY_LEVEL_NONE)
3027 && ((se->data.client.username == NULL)
3028 || (se->data.client.password == NULL)))
3029 {
3030 ERROR ("network plugin: A security level higher than `none' was "
3031 "requested, but no Username or Password option was given. "
3032 "Cowardly refusing to open this socket!");
3033 sockent_destroy (se);
3034 return (-1);
3035 }
3036 #endif /* HAVE_LIBGCRYPT */
3038 status = sockent_open (se);
3039 if (status != 0)
3040 {
3041 ERROR ("network plugin: network_config_add_server: sockent_open failed.");
3042 sockent_destroy (se);
3043 return (-1);
3044 }
3046 status = sockent_add (se);
3047 if (status != 0)
3048 {
3049 ERROR ("network plugin: network_config_add_server: sockent_add failed.");
3050 sockent_destroy (se);
3051 return (-1);
3052 }
3054 return (0);
3055 } /* }}} int network_config_add_server */
3057 static int network_config (oconfig_item_t *ci) /* {{{ */
3058 {
3059 int i;
3061 for (i = 0; i < ci->children_num; i++)
3062 {
3063 oconfig_item_t *child = ci->children + i;
3065 if (strcasecmp ("Listen", child->key) == 0)
3066 network_config_add_listen (child);
3067 else if (strcasecmp ("Server", child->key) == 0)
3068 network_config_add_server (child);
3069 else if (strcasecmp ("TimeToLive", child->key) == 0)
3070 network_config_set_ttl (child);
3071 else if (strcasecmp ("MaxPacketSize", child->key) == 0)
3072 network_config_set_buffer_size (child);
3073 else if (strcasecmp ("Forward", child->key) == 0)
3074 network_config_set_boolean (child, &network_config_forward);
3075 else if (strcasecmp ("ReportStats", child->key) == 0)
3076 network_config_set_boolean (child, &network_config_stats);
3077 else
3078 {
3079 WARNING ("network plugin: Option `%s' is not allowed here.",
3080 child->key);
3081 }
3082 }
3084 return (0);
3085 } /* }}} int network_config */
3087 static int network_notification (const notification_t *n,
3088 user_data_t __attribute__((unused)) *user_data)
3089 {
3090 char buffer[network_config_packet_size];
3091 char *buffer_ptr = buffer;
3092 int buffer_free = sizeof (buffer);
3093 int status;
3095 memset (buffer, '\0', sizeof (buffer));
3097 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_TIME_HR,
3098 (uint64_t) n->time);
3099 if (status != 0)
3100 return (-1);
3102 status = write_part_number (&buffer_ptr, &buffer_free, TYPE_SEVERITY,
3103 (uint64_t) n->severity);
3104 if (status != 0)
3105 return (-1);
3107 if (strlen (n->host) > 0)
3108 {
3109 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_HOST,
3110 n->host, strlen (n->host));
3111 if (status != 0)
3112 return (-1);
3113 }
3115 if (strlen (n->plugin) > 0)
3116 {
3117 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_PLUGIN,
3118 n->plugin, strlen (n->plugin));
3119 if (status != 0)
3120 return (-1);
3121 }
3123 if (strlen (n->plugin_instance) > 0)
3124 {
3125 status = write_part_string (&buffer_ptr, &buffer_free,
3126 TYPE_PLUGIN_INSTANCE,
3127 n->plugin_instance, strlen (n->plugin_instance));
3128 if (status != 0)
3129 return (-1);
3130 }
3132 if (strlen (n->type) > 0)
3133 {
3134 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE,
3135 n->type, strlen (n->type));
3136 if (status != 0)
3137 return (-1);
3138 }
3140 if (strlen (n->type_instance) > 0)
3141 {
3142 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_TYPE_INSTANCE,
3143 n->type_instance, strlen (n->type_instance));
3144 if (status != 0)
3145 return (-1);
3146 }
3148 status = write_part_string (&buffer_ptr, &buffer_free, TYPE_MESSAGE,
3149 n->message, strlen (n->message));
3150 if (status != 0)
3151 return (-1);
3153 network_send_buffer (buffer, sizeof (buffer) - buffer_free);
3155 return (0);
3156 } /* int network_notification */
3158 static int network_shutdown (void)
3159 {
3160 listen_loop++;
3162 /* Kill the listening thread */
3163 if (receive_thread_running != 0)
3164 {
3165 INFO ("network plugin: Stopping receive thread.");
3166 pthread_kill (receive_thread_id, SIGTERM);
3167 pthread_join (receive_thread_id, NULL /* no return value */);
3168 memset (&receive_thread_id, 0, sizeof (receive_thread_id));
3169 receive_thread_running = 0;
3170 }
3172 /* Shutdown the dispatching thread */
3173 if (dispatch_thread_running != 0)
3174 {
3175 INFO ("network plugin: Stopping dispatch thread.");
3176 pthread_mutex_lock (&receive_list_lock);
3177 pthread_cond_broadcast (&receive_list_cond);
3178 pthread_mutex_unlock (&receive_list_lock);
3179 pthread_join (dispatch_thread_id, /* ret = */ NULL);
3180 dispatch_thread_running = 0;
3181 }
3183 sockent_destroy (listen_sockets);
3185 if (send_buffer_fill > 0)
3186 flush_buffer ();
3188 sfree (send_buffer);
3190 /* TODO: Close `sending_sockets' */
3192 plugin_unregister_config ("network");
3193 plugin_unregister_init ("network");
3194 plugin_unregister_write ("network");
3195 plugin_unregister_shutdown ("network");
3197 return (0);
3198 } /* int network_shutdown */
3200 static int network_stats_read (void) /* {{{ */
3201 {
3202 derive_t copy_octets_rx;
3203 derive_t copy_octets_tx;
3204 derive_t copy_packets_rx;
3205 derive_t copy_packets_tx;
3206 derive_t copy_values_dispatched;
3207 derive_t copy_values_not_dispatched;
3208 derive_t copy_values_sent;
3209 derive_t copy_values_not_sent;
3210 derive_t copy_receive_list_length;
3211 value_list_t vl = VALUE_LIST_INIT;
3212 value_t values[2];
3214 copy_octets_rx = stats_octets_rx;
3215 copy_octets_tx = stats_octets_tx;
3216 copy_packets_rx = stats_packets_rx;
3217 copy_packets_tx = stats_packets_tx;
3218 copy_values_dispatched = stats_values_dispatched;
3219 copy_values_not_dispatched = stats_values_not_dispatched;
3220 copy_values_sent = stats_values_sent;
3221 copy_values_not_sent = stats_values_not_sent;
3222 copy_receive_list_length = receive_list_length;
3224 /* Initialize `vl' */
3225 vl.values = values;
3226 vl.values_len = 2;
3227 vl.time = 0;
3228 vl.interval = interval_g;
3229 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
3230 sstrncpy (vl.plugin, "network", sizeof (vl.plugin));
3232 /* Octets received / sent */
3233 vl.values[0].derive = (derive_t) copy_octets_rx;
3234 vl.values[1].derive = (derive_t) copy_octets_tx;
3235 sstrncpy (vl.type, "if_octets", sizeof (vl.type));
3236 plugin_dispatch_values_secure (&vl);
3238 /* Packets received / send */
3239 vl.values[0].derive = (derive_t) copy_packets_rx;
3240 vl.values[1].derive = (derive_t) copy_packets_tx;
3241 sstrncpy (vl.type, "if_packets", sizeof (vl.type));
3242 plugin_dispatch_values_secure (&vl);
3244 /* Values (not) dispatched and (not) send */
3245 sstrncpy (vl.type, "total_values", sizeof (vl.type));
3246 vl.values_len = 1;
3248 vl.values[0].derive = (derive_t) copy_values_dispatched;
3249 sstrncpy (vl.type_instance, "dispatch-accepted",
3250 sizeof (vl.type_instance));
3251 plugin_dispatch_values_secure (&vl);
3253 vl.values[0].derive = (derive_t) copy_values_not_dispatched;
3254 sstrncpy (vl.type_instance, "dispatch-rejected",
3255 sizeof (vl.type_instance));
3256 plugin_dispatch_values_secure (&vl);
3258 vl.values[0].derive = (derive_t) copy_values_sent;
3259 sstrncpy (vl.type_instance, "send-accepted",
3260 sizeof (vl.type_instance));
3261 plugin_dispatch_values_secure (&vl);
3263 vl.values[0].derive = (derive_t) copy_values_not_sent;
3264 sstrncpy (vl.type_instance, "send-rejected",
3265 sizeof (vl.type_instance));
3266 plugin_dispatch_values_secure (&vl);
3268 /* Receive queue length */
3269 vl.values[0].gauge = (gauge_t) copy_receive_list_length;
3270 sstrncpy (vl.type, "queue_length", sizeof (vl.type));
3271 vl.type_instance[0] = 0;
3272 plugin_dispatch_values_secure (&vl);
3274 return (0);
3275 } /* }}} int network_stats_read */
3277 static int network_init (void)
3278 {
3279 static _Bool have_init = 0;
3281 /* Check if we were already initialized. If so, just return - there's
3282 * nothing more to do (for now, that is). */
3283 if (have_init)
3284 return (0);
3285 have_init = 1;
3287 #if HAVE_LIBGCRYPT
3288 gcry_control (GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
3289 gcry_control (GCRYCTL_INIT_SECMEM, 32768, 0);
3290 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
3291 #endif
3293 if (network_config_stats != 0)
3294 plugin_register_read ("network", network_stats_read);
3296 plugin_register_shutdown ("network", network_shutdown);
3298 send_buffer = malloc (network_config_packet_size);
3299 if (send_buffer == NULL)
3300 {
3301 ERROR ("network plugin: malloc failed.");
3302 return (-1);
3303 }
3304 network_init_buffer ();
3306 /* setup socket(s) and so on */
3307 if (sending_sockets != NULL)
3308 {
3309 plugin_register_write ("network", network_write,
3310 /* user_data = */ NULL);
3311 plugin_register_notification ("network", network_notification,
3312 /* user_data = */ NULL);
3313 }
3315 /* If no threads need to be started, return here. */
3316 if ((listen_sockets_num == 0)
3317 || ((dispatch_thread_running != 0)
3318 && (receive_thread_running != 0)))
3319 return (0);
3321 if (dispatch_thread_running == 0)
3322 {
3323 int status;
3324 status = pthread_create (&dispatch_thread_id,
3325 NULL /* no attributes */,
3326 dispatch_thread,
3327 NULL /* no argument */);
3328 if (status != 0)
3329 {
3330 char errbuf[1024];
3331 ERROR ("network: pthread_create failed: %s",
3332 sstrerror (errno, errbuf,
3333 sizeof (errbuf)));
3334 }
3335 else
3336 {
3337 dispatch_thread_running = 1;
3338 }
3339 }
3341 if (receive_thread_running == 0)
3342 {
3343 int status;
3344 status = pthread_create (&receive_thread_id,
3345 NULL /* no attributes */,
3346 receive_thread,
3347 NULL /* no argument */);
3348 if (status != 0)
3349 {
3350 char errbuf[1024];
3351 ERROR ("network: pthread_create failed: %s",
3352 sstrerror (errno, errbuf,
3353 sizeof (errbuf)));
3354 }
3355 else
3356 {
3357 receive_thread_running = 1;
3358 }
3359 }
3361 return (0);
3362 } /* int network_init */
3364 /*
3365 * The flush option of the network plugin cannot flush individual identifiers.
3366 * All the values are added to a buffer and sent when the buffer is full, the
3367 * requested value may or may not be in there, it's not worth finding out. We
3368 * just send the buffer if `flush' is called - if the requested value was in
3369 * there, good. If not, well, then there is nothing to flush.. -octo
3370 */
3371 static int network_flush (__attribute__((unused)) cdtime_t timeout,
3372 __attribute__((unused)) const char *identifier,
3373 __attribute__((unused)) user_data_t *user_data)
3374 {
3375 pthread_mutex_lock (&send_buffer_lock);
3377 if (send_buffer_fill > 0)
3378 flush_buffer ();
3380 pthread_mutex_unlock (&send_buffer_lock);
3382 return (0);
3383 } /* int network_flush */
3385 void module_register (void)
3386 {
3387 plugin_register_complex_config ("network", network_config);
3388 plugin_register_init ("network", network_init);
3389 plugin_register_flush ("network", network_flush,
3390 /* user_data = */ NULL);
3391 } /* void module_register */
3393 /* vim: set fdm=marker : */