netcmd plugin: Implemented a fgets(3)-like wrapper around gnutls_record_recv(3).
[collectd.git] / src / netcmd.c
1 /**
2  * collectd - src/netcmd.c
3  * Copyright (C) 2007-2011  Florian octo Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Author:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28 #include "common.h"
29 #include "plugin.h"
30 #include "configfile.h"
31
32 #include "utils_cmd_flush.h"
33 #include "utils_cmd_getval.h"
34 #include "utils_cmd_listval.h"
35 #include "utils_cmd_putval.h"
36 #include "utils_cmd_putnotif.h"
37
38 /* Folks without pthread will need to disable this plugin. */
39 #include <pthread.h>
40
41 #include <sys/socket.h>
42 #include <sys/poll.h>
43 #include <netdb.h>
44 #include <sys/stat.h>
45 #include <sys/un.h>
46
47 #include <grp.h>
48
49 #include <gnutls/gnutls.h>
50
51 #define NC_DEFAULT_SERVICE "25826"
52 #define NC_TLS_DH_BITS 1024
53
54 /*
55  * Private data structures
56  */
57 struct nc_peer_s
58 {
59   char *node;
60   char *service;
61   int *fds;
62   size_t fds_num;
63
64   char *tls_cert_file;
65   char *tls_key_file;
66   char *tls_ca_file;
67   char *tls_crl_file;
68   _Bool tls_verify_peer;
69
70   gnutls_certificate_credentials_t tls_credentials;
71   gnutls_dh_params_t tls_dh_params;
72   gnutls_priority_t tls_priority;
73
74 };
75 typedef struct nc_peer_s nc_peer_t;
76
77 #if defined(PAGESIZE)
78 # define NC_READ_BUFFER_SIZE PAGESIZE
79 #elif defined(PAGE_SIZE)
80 # define NC_READ_BUFFER_SIZE PAGE_SIZE
81 #else
82 # define NC_READ_BUFFER_SIZE 4096
83 #endif
84
85 struct nc_connection_s
86 {
87   /* TLS fields */
88   int fd;
89   char *read_buffer;
90   size_t read_buffer_fill;
91
92   /* non-TLS fields */
93   FILE *fh_in;
94   FILE *fh_out;
95
96   gnutls_session_t tls_session;
97   _Bool have_tls_session;
98 };
99 typedef struct nc_connection_s nc_connection_t;
100
101 /*
102  * Private variables
103  */
104
105 /* socket configuration */
106 static nc_peer_t *peers = NULL;
107 static size_t     peers_num;
108
109 static struct pollfd  *pollfd = NULL;
110 static size_t          pollfd_num;
111
112 static int       listen_thread_loop = 0;
113 static int       listen_thread_running = 0;
114 static pthread_t listen_thread;
115
116 /*
117  * Functions
118  */
119 static nc_peer_t *nc_fd_to_peer (int fd) /* {{{ */
120 {
121   size_t i;
122
123   for (i = 0; i < peers_num; i++)
124   {
125     size_t j;
126
127     for (j = 0; j < peers[i].fds_num; j++)
128       if (peers[i].fds[j] == fd)
129         return (peers + i);
130   }
131
132   return (NULL);
133 } /* }}} nc_peer_t *nc_fd_to_peer */
134
135 static int nc_register_fd (nc_peer_t *peer, int fd) /* {{{ */
136 {
137   struct pollfd *poll_ptr;
138   int *fd_ptr;
139
140   poll_ptr = realloc (pollfd, (pollfd_num + 1) * sizeof (*pollfd));
141   if (poll_ptr == NULL)
142   {
143     ERROR ("netcmd plugin: realloc failed.");
144     return (-1);
145   }
146   pollfd = poll_ptr;
147
148   memset (&pollfd[pollfd_num], 0, sizeof (pollfd[pollfd_num]));
149   pollfd[pollfd_num].fd = fd;
150   pollfd[pollfd_num].events = POLLIN | POLLPRI;
151   pollfd[pollfd_num].revents = 0;
152   pollfd_num++;
153
154   if (peer == NULL)
155     return (0);
156
157   fd_ptr = realloc (peer->fds, (peer->fds_num + 1) * sizeof (*peer->fds));
158   if (fd_ptr == NULL)
159   {
160     ERROR ("netcmd plugin: realloc failed.");
161     return (-1);
162   }
163   peer->fds = fd_ptr;
164   peer->fds[peer->fds_num] = fd;
165   peer->fds_num++;
166
167   return (0);
168 } /* }}} int nc_register_fd */
169
170 static int nc_tls_init (nc_peer_t *peer) /* {{{ */
171 {
172   if (peer == NULL)
173     return (EINVAL);
174
175   if ((peer->tls_cert_file == NULL)
176       || (peer->tls_key_file == NULL))
177     return (0);
178
179   /* Initialize the structure holding our certificate information. */
180   gnutls_certificate_allocate_credentials (&peer->tls_credentials);
181
182   /* Set up the configured certificates. */
183   if (peer->tls_ca_file != NULL)
184     gnutls_certificate_set_x509_trust_file (peer->tls_credentials,
185         peer->tls_ca_file, GNUTLS_X509_FMT_PEM);
186   if (peer->tls_crl_file != NULL)
187       gnutls_certificate_set_x509_crl_file (peer->tls_credentials,
188           peer->tls_crl_file, GNUTLS_X509_FMT_PEM);
189   gnutls_certificate_set_x509_key_file (peer->tls_credentials,
190       peer->tls_cert_file, peer->tls_key_file, GNUTLS_X509_FMT_PEM);
191
192   /* Initialize Diffie-Hellman parameters. */
193   gnutls_dh_params_init (&peer->tls_dh_params);
194   gnutls_dh_params_generate2 (peer->tls_dh_params, NC_TLS_DH_BITS);
195   gnutls_certificate_set_dh_params (peer->tls_credentials,
196       peer->tls_dh_params);
197
198   /* Initialize a "priority cache". This will tell GNUTLS which algorithms to
199    * use and which to avoid. We use the "NORMAL" method for now. */
200   gnutls_priority_init (&peer->tls_priority,
201      /* priority = */ "NORMAL", /* errpos = */ NULL);
202
203   return (0);
204 } /* }}} int nc_tls_init */
205
206 static gnutls_session_t nc_tls_get_session (nc_peer_t *peer) /* {{{ */
207 {
208   gnutls_session_t session;
209
210   if (peer->tls_credentials == NULL)
211     return (NULL);
212
213   /* Initialize new session. */
214   gnutls_init (&session, GNUTLS_SERVER);
215
216   /* Set cipher priority and credentials based on the information stored with
217    * the peer. */
218   gnutls_priority_set (session, peer->tls_priority);
219   gnutls_credentials_set (session,
220       GNUTLS_CRD_CERTIFICATE, peer->tls_credentials);
221
222   /* Request the client certificate. */
223   gnutls_certificate_server_set_request (session, GNUTLS_CERT_REQUEST);
224
225   return (session);
226 } /* }}} gnutls_session_t nc_tls_get_session */
227
228 static int nc_open_socket (nc_peer_t *peer) /* {{{ */
229 {
230   struct addrinfo ai_hints;
231   struct addrinfo *ai_list;
232   struct addrinfo *ai_ptr;
233   int status;
234
235   const char *node = NULL;
236   const char *service = NULL;
237
238   if (peer != NULL)
239   {
240     node = peer->node;
241     service = peer->service;
242   }
243
244   if (service == NULL)
245     service = NC_DEFAULT_SERVICE;
246
247   memset (&ai_hints, 0, sizeof (ai_hints));
248 #ifdef AI_PASSIVE
249   ai_hints.ai_flags |= AI_PASSIVE;
250 #endif
251 #ifdef AI_ADDRCONFIG
252   ai_hints.ai_flags |= AI_ADDRCONFIG;
253 #endif
254   ai_hints.ai_family = AF_UNSPEC;
255   ai_hints.ai_socktype = SOCK_STREAM;
256
257   ai_list = NULL;
258
259   if (service == NULL)
260     service = NC_DEFAULT_SERVICE;
261
262   status = getaddrinfo (node, service, &ai_hints, &ai_list);
263   if (status != 0)
264   {
265     ERROR ("netcmd plugin: getaddrinfo failed: %s",
266         gai_strerror (status));
267     return (-1);
268   }
269
270   for (ai_ptr = ai_list; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
271   {
272     char errbuf[1024];
273     int fd;
274
275     fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype,
276         ai_ptr->ai_protocol);
277     if (fd < 0)
278     {
279       ERROR ("netcmd plugin: socket(2) failed: %s",
280           sstrerror (errno, errbuf, sizeof (errbuf)));
281       continue;
282     }
283
284     status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
285     if (status != 0)
286     {
287       close (fd);
288       ERROR ("netcmd plugin: bind(2) failed: %s",
289           sstrerror (errno, errbuf, sizeof (errbuf)));
290       continue;
291     }
292
293     status = listen (fd, /* backlog = */ 8);
294     if (status != 0)
295     {
296       close (fd);
297       ERROR ("netcmd plugin: listen(2) failed: %s",
298           sstrerror (errno, errbuf, sizeof (errbuf)));
299       continue;
300     }
301
302     status = nc_register_fd (peer, fd);
303     if (status != 0)
304     {
305       close (fd);
306       continue;
307     }
308   } /* for (ai_next) */
309
310   freeaddrinfo (ai_list);
311
312   return (nc_tls_init (peer));
313 } /* }}} int nc_open_socket */
314
315 static void nc_connection_close (nc_connection_t *conn) /* {{{ */
316 {
317   if (conn == NULL)
318     return;
319
320   if (conn->fd >= 0)
321   {
322     close (conn->fd);
323     conn->fd = -1;
324   }
325
326   if (conn->fh_in != NULL)
327   {
328     fclose (conn->fh_in);
329     conn->fh_in = NULL;
330   }
331
332   if (conn->fh_out != NULL)
333   {
334     fclose (conn->fh_out);
335     conn->fh_out = NULL;
336   }
337
338   if (conn->have_tls_session)
339   {
340     gnutls_deinit (conn->tls_session);
341     conn->have_tls_session = 0;
342   }
343
344   sfree (conn);
345 } /* }}} void nc_connection_close */
346
347 static int nc_connection_init (nc_connection_t *conn) /* {{{ */
348 {
349   int fd_copy;
350   char errbuf[1024];
351
352   if (conn->have_tls_session)
353   {
354     conn->read_buffer = malloc (NC_READ_BUFFER_SIZE);
355     if (conn->read_buffer == NULL)
356       return (ENOMEM);
357     memset (conn->read_buffer, 0, NC_READ_BUFFER_SIZE);
358
359     gnutls_transport_set_ptr (conn->tls_session, &conn->fd);
360     return (0);
361   }
362
363   /* Duplicate the file descriptor. We need two file descriptors, because we
364    * create two FILE* objects. If they pointed to the same FD and we called
365    * fclose() on each, that would call close() twice on the same FD. If
366    * another file is opened in between those two calls, it could get assigned
367    * that FD and weird stuff would happen. */
368   fd_copy = dup (conn->fd);
369   if (fd_copy < 0)
370   {
371     ERROR ("netcmd plugin: dup(2) failed: %s",
372         sstrerror (errno, errbuf, sizeof (errbuf)));
373     return (-1);
374   }
375
376   conn->fh_in  = fdopen (conn->fd, "r");
377   if (conn->fh_in == NULL)
378   {
379     ERROR ("netcmd plugin: fdopen failed: %s",
380         sstrerror (errno, errbuf, sizeof (errbuf)));
381     return (-1);
382   }
383   /* Prevent other code from using the FD directly. */
384   conn->fd = -1;
385
386   conn->fh_out = fdopen (fd_copy, "w");
387   /* Prevent nc_connection_close from calling close(2) on this fd. */
388   if (conn->fh_out == NULL)
389   {
390     ERROR ("netcmd plugin: fdopen failed: %s",
391         sstrerror (errno, errbuf, sizeof (errbuf)));
392     return (-1);
393   }
394
395   /* change output buffer to line buffered mode */
396   if (setvbuf (conn->fh_out, NULL, _IOLBF, 0) != 0)
397   {
398     ERROR ("netcmd plugin: setvbuf failed: %s",
399         sstrerror (errno, errbuf, sizeof (errbuf)));
400     nc_connection_close (conn);
401     return (-1);
402   }
403
404   return (0);
405 } /* }}} int nc_connection_init */
406
407 static char *nc_connection_gets (nc_connection_t *conn, /* {{{ */
408     char *buffer, size_t buffer_size)
409 {
410   ssize_t status;
411   char *orig_buffer = buffer;
412
413   if (conn == NULL)
414   {
415     errno = EINVAL;
416     return (NULL);
417   }
418
419   if (!conn->have_tls_session)
420     return (fgets (buffer, (int) buffer_size, conn->fh_in));
421
422   if ((buffer == NULL) || (buffer_size < 2))
423   {
424     errno = EINVAL;
425     return (NULL);
426   }
427
428   /* ensure null termination */
429   memset (buffer, 0, buffer_size);
430   buffer_size--;
431
432   while (42)
433   {
434     size_t max_copy_bytes;
435     size_t newline_pos;
436     _Bool found_newline;
437     size_t i;
438
439     /* If there's no more data in the read buffer, read another chunk from the
440      * socket. */
441     if (conn->read_buffer_fill < 1)
442     {
443       status = gnutls_record_recv (conn->tls_session,
444           conn->read_buffer, NC_READ_BUFFER_SIZE);
445       if (status < 0) /* error */
446       {
447         ERROR ("netcmd plugin: Error while reading from TLS stream.");
448         return (NULL);
449       }
450       else if (status == 0) /* we reached end of file */
451       {
452         if (orig_buffer == buffer) /* nothing has been written to the buffer yet */
453           return (NULL); /* end of file */
454         else
455           return (orig_buffer);
456       }
457       else
458       {
459         conn->read_buffer_fill = (size_t) status;
460       }
461     }
462     assert (conn->read_buffer_fill > 0);
463
464     /* Determine where the first newline character is in the buffer. We're not
465      * using strcspn(3) here, becaus the buffer is possibly not
466      * null-terminated. */
467     newline_pos = conn->read_buffer_fill;
468     found_newline = 0;
469     for (i = 0; i < conn->read_buffer_fill; i++)
470     {
471       if (conn->read_buffer[i] == '\n')
472       {
473         newline_pos = i;
474         found_newline = 1;
475         break;
476       }
477     }
478
479     /* Determine how many bytes to copy at most. This is MIN(buffer available,
480      * read buffer size, characters to newline). */
481     max_copy_bytes = buffer_size;
482     if (max_copy_bytes > conn->read_buffer_fill)
483       max_copy_bytes = conn->read_buffer_fill;
484     if (max_copy_bytes > (newline_pos + 1))
485       max_copy_bytes = newline_pos + 1;
486     assert (max_copy_bytes > 0);
487
488     /* Copy bytes to the output buffer. */
489     memcpy (buffer, conn->read_buffer, max_copy_bytes);
490     buffer += max_copy_bytes;
491     assert (buffer_size >= max_copy_bytes);
492     buffer_size -= max_copy_bytes;
493
494     /* If there is data left in the read buffer, move it to the front of the
495      * buffer. */
496     if (max_copy_bytes < conn->read_buffer_fill)
497     {
498       size_t data_left_size = conn->read_buffer_fill - max_copy_bytes;
499       memmove (conn->read_buffer, conn->read_buffer + max_copy_bytes,
500           data_left_size);
501       conn->read_buffer_fill -= max_copy_bytes;
502     }
503     else
504     {
505       assert (max_copy_bytes == conn->read_buffer_fill);
506       conn->read_buffer_fill = 0;
507     }
508
509     if (found_newline)
510       break;
511
512     if (buffer_size == 0) /* no more space in the output buffer */
513       break;
514   }
515
516   return (orig_buffer);
517 } /* }}} char *nc_connection_gets */
518
519 static void *nc_handle_client (void *arg) /* {{{ */
520 {
521   nc_connection_t *conn;
522   char errbuf[1024];
523   int status;
524
525   conn = arg;
526
527   DEBUG ("netcmd plugin: nc_handle_client: Reading from fd #%i", conn->fd);
528
529   status = nc_connection_init (conn);
530   if (status != 0)
531   {
532     nc_connection_close (conn);
533     pthread_exit ((void *) 1);
534   }
535
536   while (42)
537   {
538     char buffer[1024];
539     char buffer_copy[1024];
540     char *fields[128];
541     int   fields_num;
542     int   len;
543
544     errno = 0;
545     if (nc_connection_gets (conn, buffer, sizeof (buffer)) == NULL)
546     {
547       if (errno != 0)
548       {
549         WARNING ("netcmd plugin: failed to read from socket #%i: %s",
550             fileno (conn->fh_in),
551             sstrerror (errno, errbuf, sizeof (errbuf)));
552       }
553       break;
554     }
555
556     len = strlen (buffer);
557     while ((len > 0)
558         && ((buffer[len - 1] == '\n') || (buffer[len - 1] == '\r')))
559       buffer[--len] = '\0';
560
561     if (len == 0)
562       continue;
563
564     sstrncpy (buffer_copy, buffer, sizeof (buffer_copy));
565
566     fields_num = strsplit (buffer_copy, fields,
567         sizeof (fields) / sizeof (fields[0]));
568
569     if (fields_num < 1)
570     {
571       nc_connection_close (conn);
572       break;
573     }
574
575     if (strcasecmp (fields[0], "getval") == 0)
576     {
577       handle_getval (conn->fh_out, buffer);
578     }
579     else if (strcasecmp (fields[0], "putval") == 0)
580     {
581       handle_putval (conn->fh_out, buffer);
582     }
583     else if (strcasecmp (fields[0], "listval") == 0)
584     {
585       handle_listval (conn->fh_out, buffer);
586     }
587     else if (strcasecmp (fields[0], "putnotif") == 0)
588     {
589       handle_putnotif (conn->fh_out, buffer);
590     }
591     else if (strcasecmp (fields[0], "flush") == 0)
592     {
593       handle_flush (conn->fh_out, buffer);
594     }
595     else
596     {
597       if (fprintf (conn->fh_out, "-1 Unknown command: %s\n", fields[0]) < 0)
598       {
599         WARNING ("netcmd plugin: failed to write to socket #%i: %s",
600             fileno (conn->fh_out),
601             sstrerror (errno, errbuf, sizeof (errbuf)));
602         break;
603       }
604     }
605   } /* while (fgets) */
606
607   DEBUG ("netcmd plugin: nc_handle_client: Exiting..");
608   nc_connection_close (conn);
609
610   pthread_exit ((void *) 0);
611   return ((void *) 0);
612 } /* }}} void *nc_handle_client */
613
614 static void *nc_server_thread (void __attribute__((unused)) *arg) /* {{{ */
615 {
616   int  status;
617   pthread_t th;
618   pthread_attr_t th_attr;
619   char errbuf[1024];
620   size_t i;
621
622   for (i = 0; i < peers_num; i++)
623     nc_open_socket (peers + i);
624
625   if (peers_num == 0)
626     nc_open_socket (NULL);
627
628   if (pollfd_num == 0)
629   {
630     ERROR ("netcmd plugin: No sockets could be opened.");
631     pthread_exit ((void *) -1);
632   }
633
634   while (listen_thread_loop != 0)
635   {
636     status = poll (pollfd, (nfds_t) pollfd_num, /* timeout = */ -1);
637     if (status < 0)
638     {
639       if ((errno == EINTR) || (errno == EAGAIN))
640         continue;
641
642       ERROR ("netcmd plugin: poll(2) failed: %s",
643           sstrerror (errno, errbuf, sizeof (errbuf)));
644       listen_thread_loop = 0;
645       continue;
646     }
647
648     for (i = 0; i < pollfd_num; i++)
649     {
650       nc_peer_t *peer;
651       nc_connection_t *conn;
652
653       if (pollfd[i].revents == 0)
654       {
655         continue;
656       }
657       else if ((pollfd[i].revents & (POLLERR | POLLHUP | POLLNVAL))
658           != 0)
659       {
660         WARNING ("netcmd plugin: File descriptor %i failed.",
661             pollfd[i].fd);
662         close (pollfd[i].fd);
663         pollfd[i].fd = -1;
664         pollfd[i].events = 0;
665         pollfd[i].revents = 0;
666         continue;
667       }
668       pollfd[i].revents = 0;
669
670       peer = nc_fd_to_peer (pollfd[i].fd);
671       if (peer == NULL)
672       {
673         ERROR ("netcmd plugin: Unable to find peer structure for file "
674             "descriptor #%i.", pollfd[i].fd);
675         continue;
676       }
677
678       status = accept (pollfd[i].fd,
679           /* sockaddr = */ NULL,
680           /* sockaddr_len = */ NULL);
681       if (status < 0)
682       {
683         if (errno != EINTR)
684           ERROR ("netcmd plugin: accept failed: %s",
685               sstrerror (errno, errbuf, sizeof (errbuf)));
686         continue;
687       }
688
689       conn = malloc (sizeof (*conn));
690       if (conn == NULL)
691       {
692         ERROR ("netcmd plugin: malloc failed.");
693         close (status);
694         continue;
695       }
696       memset (conn, 0, sizeof (*conn));
697       conn->fh_in = NULL;
698       conn->fh_out = NULL;
699
700       conn->fd = status;
701       if ((peer != NULL)
702           && (peer->tls_cert_file != NULL))
703       {
704         DEBUG ("netcmd plugin: Starting TLS session on [%s]:%s",
705             (peer->node != NULL) ? peer->node : "any",
706             (peer->service != NULL) ? peer->service : NC_DEFAULT_SERVICE);
707         conn->tls_session = nc_tls_get_session (peer);
708         conn->have_tls_session = 1;
709       }
710
711       DEBUG ("Spawning child to handle connection on fd %i", conn->fd);
712
713       pthread_attr_init (&th_attr);
714       pthread_attr_setdetachstate (&th_attr, PTHREAD_CREATE_DETACHED);
715
716       status = pthread_create (&th, &th_attr, nc_handle_client,
717           conn);
718       if (status != 0)
719       {
720         WARNING ("netcmd plugin: pthread_create failed: %s",
721             sstrerror (errno, errbuf, sizeof (errbuf)));
722         nc_connection_close (conn);
723         continue;
724       }
725     }
726   } /* while (listen_thread_loop) */
727
728   for (i = 0; i < pollfd_num; i++)
729   {
730     if (pollfd[i].fd < 0)
731       continue;
732
733     close (pollfd[i].fd);
734     pollfd[i].fd = -1;
735     pollfd[i].events = 0;
736     pollfd[i].revents = 0;
737   }
738
739   sfree (pollfd);
740   pollfd_num = 0;
741
742   return ((void *) 0);
743 } /* }}} void *nc_server_thread */
744
745 /*
746  * <Plugin netcmd>
747  *   <Listen>
748  *     Address "::1"
749  *     Port "1234"
750  *     TLSCertFile "/path/to/cert"
751  *     TLSKeyFile  "/path/to/key"
752  *     TLSCAFile   "/path/to/ca"
753  *     TLSCRLFile  "/path/to/crl"
754  *     TLSVerifyPeer yes|no
755  *   </Listen>
756  * </Plugin>
757  */
758 static int nc_config_peer (const oconfig_item_t *ci) /* {{{ */
759 {
760   nc_peer_t *p;
761   int i;
762
763   p = realloc (peers, sizeof (*peers) * (peers_num + 1));
764   if (p == NULL)
765   {
766     ERROR ("netcmd plugin: realloc failed.");
767     return (ENOMEM);
768   }
769   peers = p;
770   p = peers + peers_num;
771   memset (p, 0, sizeof (*p));
772   p->node = NULL;
773   p->service = NULL;
774   p->tls_cert_file = NULL;
775   p->tls_key_file = NULL;
776   p->tls_ca_file = NULL;
777   p->tls_crl_file = NULL;
778   p->tls_verify_peer = 1;
779
780   for (i = 0; i < ci->children_num; i++)
781   {
782     oconfig_item_t *child = ci->children + i;
783
784     if (strcasecmp ("Address", child->key) == 0)
785       cf_util_get_string (child, &p->node);
786     else if (strcasecmp ("Port", child->key) == 0)
787       cf_util_get_string (child, &p->service);
788     else if (strcasecmp ("TLSCertFile", child->key) == 0)
789       cf_util_get_string (child, &p->tls_cert_file);
790     else if (strcasecmp ("TLSKeyFile", child->key) == 0)
791       cf_util_get_string (child, &p->tls_key_file);
792     else if (strcasecmp ("TLSCAFile", child->key) == 0)
793       cf_util_get_string (child, &p->tls_ca_file);
794     else if (strcasecmp ("TLSCRLFile", child->key) == 0)
795       cf_util_get_string (child, &p->tls_crl_file);
796     else
797       WARNING ("netcmd plugin: The option \"%s\" is not recognized within "
798           "a \"%s\" block.", child->key, ci->key);
799   }
800
801   DEBUG ("netcmd plugin: node = \"%s\"; service = \"%s\";", p->node, p->service);
802
803   peers_num++;
804
805   return (0);
806 } /* }}} int nc_config_peer */
807
808 static int nc_config (oconfig_item_t *ci)
809 {
810   int i;
811
812   for (i = 0; i < ci->children_num; i++)
813   {
814     oconfig_item_t *child = ci->children + i;
815
816     if (strcasecmp ("Listen", child->key) == 0)
817       nc_config_peer (child);
818     else
819       WARNING ("netcmd plugin: The option \"%s\" is not recognized.",
820           child->key);
821   }
822
823   return (0);
824 } /* int nc_config */
825
826 static int nc_init (void)
827 {
828   static int have_init = 0;
829
830   int status;
831
832   /* Initialize only once. */
833   if (have_init != 0)
834     return (0);
835   have_init = 1;
836
837   listen_thread_loop = 1;
838
839   status = pthread_create (&listen_thread, NULL, nc_server_thread, NULL);
840   if (status != 0)
841   {
842     char errbuf[1024];
843     listen_thread_loop = 0;
844     listen_thread_running = 0;
845     ERROR ("netcmd plugin: pthread_create failed: %s",
846         sstrerror (errno, errbuf, sizeof (errbuf)));
847     return (-1);
848   }
849
850   listen_thread_running = 1;
851   return (0);
852 } /* int nc_init */
853
854 static int nc_shutdown (void)
855 {
856   void *ret;
857
858   listen_thread_loop = 0;
859
860   if (listen_thread != (pthread_t) 0)
861   {
862     pthread_kill (listen_thread, SIGTERM);
863     pthread_join (listen_thread, &ret);
864     listen_thread = (pthread_t) 0;
865   }
866
867   plugin_unregister_init ("netcmd");
868   plugin_unregister_shutdown ("netcmd");
869
870   return (0);
871 } /* int nc_shutdown */
872
873 void module_register (void)
874 {
875   plugin_register_complex_config ("netcmd", nc_config);
876   plugin_register_init ("netcmd", nc_init);
877   plugin_register_shutdown ("netcmd", nc_shutdown);
878 } /* void module_register (void) */
879
880 /* vim: set sw=2 sts=2 tw=78 et fdm=marker : */