Merge branch 'pr/1918'
[collectd.git] / src / iptables.c
1 /**
2  * collectd - src/iptables.c
3  * Copyright (C) 2007       Sjoerd van der Berg
4  * Copyright (C) 2007-2010  Florian octo Forster
5  * Copyright (C) 2009       Marco Chiappero
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  *
21  * Authors:
22  *  Sjoerd van der Berg <harekiet at users.sourceforge.net>
23  *  Florian Forster <octo at collectd.org>
24  *  Marco Chiappero <marco at absence.it>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #include <libiptc/libiptc.h>
33 #include <libiptc/libip6tc.h>
34
35 #ifdef HAVE_SYS_CAPABILITY_H
36 # include <sys/capability.h>
37 #endif
38
39 /*
40  * iptc_handle_t was available before libiptc was officially available as a
41  * shared library. Note, that when the shared lib was introduced, the API and
42  * ABI have changed slightly:
43  * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
44  * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
45  * iptc_handle *' (thus removing one level of pointer indirection).
46  *
47  * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
48  * this is somewhat hacky, I didn't find better way to solve that :-/
49  * -tokkee
50  */
51 #ifndef HAVE_IPTC_HANDLE_T
52 typedef struct iptc_handle iptc_handle_t;
53 #endif
54 #ifndef HAVE_IP6TC_HANDLE_T
55 typedef struct ip6tc_handle ip6tc_handle_t;
56 #endif
57
58 /*
59  * (Module-)Global variables
60  */
61
62 /*
63  * Config format should be `Chain table chainname',
64  * e. g. `Chain mangle incoming'
65  */
66 static const char *config_keys[] =
67 {
68     "Chain",
69     "Chain6"
70 };
71 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
72 enum protocol_version_e
73 {
74     IPV4,
75     IPV6
76 };
77 typedef enum protocol_version_e protocol_version_t;
78
79 /*
80  * Each table/chain combo that will be queried goes into this list
81  */
82 #ifndef XT_TABLE_MAXNAMELEN
83 # define XT_TABLE_MAXNAMELEN 32
84 #endif
85 typedef struct {
86     protocol_version_t ip_version;
87     char table[XT_TABLE_MAXNAMELEN];
88     char chain[XT_TABLE_MAXNAMELEN];
89     union
90     {
91         int   num;
92         char *comment;
93     } rule;
94     enum
95     {
96         RTYPE_NUM,
97         RTYPE_COMMENT,
98         RTYPE_COMMENT_ALL
99     } rule_type;
100     char name[64];
101 } ip_chain_t;
102
103 static ip_chain_t **chain_list = NULL;
104 static int chain_num = 0;
105
106 static int iptables_config (const char *key, const char *value)
107 {
108     /* int ip_value; */
109     protocol_version_t ip_version = 0;
110
111     if (strcasecmp (key, "Chain") == 0)
112         ip_version = IPV4;
113     else if (strcasecmp (key, "Chain6") == 0)
114         ip_version = IPV6;
115     else
116         return (1);
117
118     ip_chain_t  temp = { 0 };
119     ip_chain_t *final, **list;
120     char *table;
121     int   table_len;
122     char *chain;
123     int   chain_len;
124
125     char *value_copy;
126     char *fields[4];
127     int   fields_num;
128
129     value_copy = strdup (value);
130     if (value_copy == NULL)
131     {
132         char errbuf[1024];
133         ERROR ("strdup failed: %s",
134                 sstrerror (errno, errbuf, sizeof (errbuf)));
135         return (1);
136     }
137
138     /*
139      *  Time to fill the temp element
140      *  Examine value string, it should look like:
141      *  Chain[6] <table> <chain> [<comment|num> [name]]
142      */
143
144     /* set IPv4 or IPv6 */
145     temp.ip_version = ip_version;
146
147     /* Chain <table> <chain> [<comment|num> [name]] */
148     fields_num = strsplit (value_copy, fields, 4);
149     if (fields_num < 2)
150     {
151         free (value_copy);
152         return (1);
153     }
154
155     table = fields[0];
156     chain = fields[1];
157
158     table_len = strlen (table) + 1;
159     if ((unsigned int)table_len > sizeof(temp.table))
160     {
161         ERROR ("Table `%s' too long.", table);
162         free (value_copy);
163         return (1);
164     }
165     sstrncpy (temp.table, table, table_len);
166
167     chain_len = strlen (chain) + 1;
168     if ((unsigned int)chain_len > sizeof(temp.chain))
169     {
170         ERROR ("Chain `%s' too long.", chain);
171         free (value_copy);
172         return (1);
173     }
174     sstrncpy (temp.chain, chain, chain_len);
175
176     if (fields_num >= 3)
177     {
178         char *comment = fields[2];
179         int   rule = atoi (comment);
180
181         if (rule)
182         {
183             temp.rule.num = rule;
184             temp.rule_type = RTYPE_NUM;
185         }
186         else
187         {
188             temp.rule.comment = strdup (comment);
189             if (temp.rule.comment == NULL)
190             {
191                 free (value_copy);
192                 return (1);
193             }
194             temp.rule_type = RTYPE_COMMENT;
195         }
196     }
197     else
198     {
199         temp.rule_type = RTYPE_COMMENT_ALL;
200     }
201
202     if (fields_num >= 4)
203         sstrncpy (temp.name, fields[3], sizeof (temp.name));
204
205     free (value_copy);
206     value_copy = NULL;
207     table = NULL;
208     chain = NULL;
209
210     list = realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
211     if (list == NULL)
212     {
213         char errbuf[1024];
214         ERROR ("realloc failed: %s",
215                 sstrerror (errno, errbuf, sizeof (errbuf)));
216         sfree (temp.rule.comment);
217         return (1);
218     }
219
220     chain_list = list;
221     final = malloc(sizeof (*final));
222     if (final == NULL)
223     {
224         char errbuf[1024];
225         ERROR ("malloc failed: %s",
226                 sstrerror (errno, errbuf, sizeof (errbuf)));
227         sfree (temp.rule.comment);
228         return (1);
229     }
230     memcpy (final, &temp, sizeof (temp));
231     chain_list[chain_num] = final;
232     chain_num++;
233
234     DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
235
236     return (0);
237 } /* int iptables_config */
238
239 static int submit6_match (const struct ip6t_entry_match *match,
240                           const struct ip6t_entry *entry,
241                           const ip_chain_t *chain,
242                           int rule_num)
243 {
244     int status;
245     value_list_t vl = VALUE_LIST_INIT;
246
247     /* Select the rules to collect */
248     if (chain->rule_type == RTYPE_NUM)
249     {
250         if (chain->rule.num != rule_num)
251             return (0);
252     }
253     else
254     {
255         if (strcmp (match->u.user.name, "comment") != 0)
256             return (0);
257         if ((chain->rule_type == RTYPE_COMMENT)
258              && (strcmp (chain->rule.comment, (char *) match->data) != 0))
259             return (0);
260     }
261
262     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
263     sstrncpy (vl.plugin, "ip6tables", sizeof (vl.plugin));
264
265     status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
266                         "%s-%s", chain->table, chain->chain);
267     if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
268         return (0);
269
270     if (chain->name[0] != '\0')
271     {
272         sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
273     }
274     else
275     {
276         if (chain->rule_type == RTYPE_NUM)
277             ssnprintf (vl.type_instance, sizeof (vl.type_instance),
278                        "%i", chain->rule.num);
279         else
280             sstrncpy (vl.type_instance, (char *) match->data,
281                       sizeof (vl.type_instance));
282     }
283
284     sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
285     vl.values = &(value_t) { .derive = (derive_t) entry->counters.bcnt };
286     vl.values_len = 1;
287     plugin_dispatch_values (&vl);
288
289     sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
290     vl.values = &(value_t) { .derive = (derive_t) entry->counters.pcnt };
291     plugin_dispatch_values (&vl);
292
293     return (0);
294 } /* int submit6_match */
295
296 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
297 static int submit_match (const struct ipt_entry_match *match,
298                          const struct ipt_entry *entry,
299                          const ip_chain_t *chain,
300                          int rule_num)
301 {
302     int status;
303     value_list_t vl = VALUE_LIST_INIT;
304
305     /* Select the rules to collect */
306     if (chain->rule_type == RTYPE_NUM)
307     {
308         if (chain->rule.num != rule_num)
309             return (0);
310     }
311     else
312     {
313         if (strcmp (match->u.user.name, "comment") != 0)
314             return (0);
315         if ((chain->rule_type == RTYPE_COMMENT)
316              && (strcmp (chain->rule.comment, (char *) match->data) != 0))
317             return (0);
318     }
319
320     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
321     sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
322
323     status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
324                         "%s-%s", chain->table, chain->chain);
325     if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
326         return (0);
327
328     if (chain->name[0] != '\0')
329     {
330         sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
331     }
332     else
333     {
334         if (chain->rule_type == RTYPE_NUM)
335             ssnprintf (vl.type_instance, sizeof (vl.type_instance),
336                        "%i", chain->rule.num);
337         else
338             sstrncpy (vl.type_instance, (char *) match->data,
339                       sizeof (vl.type_instance));
340     }
341
342     sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
343     vl.values = &(value_t) { .derive = (derive_t) entry->counters.bcnt };
344     vl.values_len = 1;
345     plugin_dispatch_values (&vl);
346
347     sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
348     vl.values = &(value_t) { .derive = (derive_t) entry->counters.pcnt };
349     plugin_dispatch_values (&vl);
350
351     return (0);
352 } /* int submit_match */
353
354
355 /* ipv6 submit_chain */
356 static void submit6_chain (ip6tc_handle_t *handle, ip_chain_t *chain)
357 {
358     const struct ip6t_entry *entry;
359     int rule_num;
360
361     /* Find first rule for chain and use the iterate macro */
362     entry = ip6tc_first_rule( chain->chain, handle );
363     if (entry == NULL)
364     {
365         DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
366         return;
367     }
368
369     rule_num = 1;
370     while (entry)
371     {
372         if (chain->rule_type == RTYPE_NUM)
373         {
374             submit6_match (NULL, entry, chain, rule_num);
375         }
376         else
377         {
378             IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
379         }
380
381         entry = ip6tc_next_rule( entry, handle );
382         rule_num++;
383     } /* while (entry) */
384 }
385
386
387 /* ipv4 submit_chain */
388 static void submit_chain (iptc_handle_t *handle, ip_chain_t *chain)
389 {
390     const struct ipt_entry *entry;
391     int rule_num;
392
393     /* Find first rule for chain and use the iterate macro */
394     entry = iptc_first_rule( chain->chain, handle );
395     if (entry == NULL)
396     {
397         DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
398         return;
399     }
400
401     rule_num = 1;
402     while (entry)
403     {
404         if (chain->rule_type == RTYPE_NUM)
405         {
406             submit_match (NULL, entry, chain, rule_num);
407         }
408         else
409         {
410             IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
411         }
412
413         entry = iptc_next_rule( entry, handle );
414         rule_num++;
415     } /* while (entry) */
416 }
417
418
419 static int iptables_read (void)
420 {
421     int num_failures = 0;
422     ip_chain_t *chain;
423
424     /* Init the iptc handle structure and query the correct table */
425     for (int i = 0; i < chain_num; i++)
426     {
427         chain = chain_list[i];
428
429         if (!chain)
430         {
431             DEBUG ("iptables plugin: chain == NULL");
432             continue;
433         }
434
435         if ( chain->ip_version == IPV4 )
436         {
437 #ifdef HAVE_IPTC_HANDLE_T
438             iptc_handle_t _handle;
439             iptc_handle_t *handle = &_handle;
440
441             *handle = iptc_init (chain->table);
442 #else
443             iptc_handle_t *handle;
444             handle = iptc_init (chain->table);
445 #endif
446
447             if (!handle)
448             {
449                 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
450                         chain->table, iptc_strerror (errno));
451                 num_failures++;
452                 continue;
453             }
454
455             submit_chain (handle, chain);
456             iptc_free (handle);
457         }
458         else if ( chain->ip_version == IPV6 )
459         {
460 #ifdef HAVE_IP6TC_HANDLE_T
461             ip6tc_handle_t _handle;
462             ip6tc_handle_t *handle = &_handle;
463
464             *handle = ip6tc_init (chain->table);
465 #else
466             ip6tc_handle_t *handle;
467             handle = ip6tc_init (chain->table);
468 #endif
469             if (!handle)
470             {
471                 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
472                         chain->table, ip6tc_strerror (errno));
473                 num_failures++;
474                 continue;
475             }
476
477             submit6_chain (handle, chain);
478             ip6tc_free (handle);
479         }
480         else
481             num_failures++;
482     } /* for (i = 0 .. chain_num) */
483
484     return ((num_failures < chain_num) ? 0 : -1);
485 } /* int iptables_read */
486
487 static int iptables_shutdown (void)
488 {
489     for (int i = 0; i < chain_num; i++)
490     {
491         if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
492             sfree (chain_list[i]->rule.comment);
493         sfree (chain_list[i]);
494     }
495     sfree (chain_list);
496
497     return (0);
498 } /* int iptables_shutdown */
499
500 static int iptables_init (void)
501 {
502 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_ADMIN)
503     if (check_capability (CAP_NET_ADMIN) != 0)
504     {
505         if (getuid () == 0)
506             WARNING ("iptables plugin: Running collectd as root, but the "
507                   "CAP_NET_ADMIN capability is missing. The plugin's read "
508                   "function will probably fail. Is your init system dropping "
509                   "capabilities?");
510         else
511             WARNING ("iptables plugin: collectd doesn't have the CAP_NET_ADMIN "
512                   "capability. If you don't want to run collectd as root, try "
513                   "running \"setcap cap_net_admin=ep\" on the collectd binary.");
514     }
515 #endif
516     return (0);
517 } /* int iptables_init */
518
519 void module_register (void)
520 {
521     plugin_register_config ("iptables", iptables_config,
522                              config_keys, config_keys_num);
523     plugin_register_init ("iptables", iptables_init);
524     plugin_register_read ("iptables", iptables_read);
525     plugin_register_shutdown ("iptables", iptables_shutdown);
526 } /* void module_register */
527