treewide: declare loop variable in loop expression
[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 #include "configfile.h"
32
33 #include <libiptc/libiptc.h>
34 #include <libiptc/libip6tc.h>
35
36 /*
37  * iptc_handle_t was available before libiptc was officially available as a
38  * shared library. Note, that when the shared lib was introduced, the API and
39  * ABI have changed slightly:
40  * 'iptc_handle_t' used to be 'struct iptc_handle *' and most functions used
41  * 'iptc_handle_t *' as an argument. Now, most functions use 'struct
42  * iptc_handle *' (thus removing one level of pointer indirection).
43  *
44  * HAVE_IPTC_HANDLE_T is used to determine which API ought to be used. While
45  * this is somewhat hacky, I didn't find better way to solve that :-/
46  * -tokkee
47  */
48 #ifndef HAVE_IPTC_HANDLE_T
49 typedef struct iptc_handle iptc_handle_t;
50 #endif
51 #ifndef HAVE_IP6TC_HANDLE_T
52 typedef struct ip6tc_handle ip6tc_handle_t;
53 #endif
54
55 /*
56  * (Module-)Global variables
57  */
58
59 /*
60  * Config format should be `Chain table chainname',
61  * e. g. `Chain mangle incoming'
62  */
63 static const char *config_keys[] =
64 {
65     "Chain",
66     "Chain6"
67 };
68 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
69 enum protocol_version_e
70 {
71     IPV4,
72     IPV6
73 };
74 typedef enum protocol_version_e protocol_version_t;
75
76 /*
77  * Each table/chain combo that will be queried goes into this list
78  */
79 #ifndef XT_TABLE_MAXNAMELEN
80 # define XT_TABLE_MAXNAMELEN 32
81 #endif
82 typedef struct {
83     protocol_version_t ip_version;
84     char table[XT_TABLE_MAXNAMELEN];
85     char chain[XT_TABLE_MAXNAMELEN];
86     union
87     {
88         int   num;
89         char *comment;
90     } rule;
91     enum
92     {
93         RTYPE_NUM,
94         RTYPE_COMMENT,
95         RTYPE_COMMENT_ALL
96     } rule_type;
97     char name[64];
98 } ip_chain_t;
99
100 static ip_chain_t **chain_list = NULL;
101 static int chain_num = 0;
102
103 static int iptables_config (const char *key, const char *value)
104 {
105     /* int ip_value; */
106     protocol_version_t ip_version = 0;
107
108     if (strcasecmp (key, "Chain") == 0)
109         ip_version = IPV4;
110     else if (strcasecmp (key, "Chain6") == 0)
111         ip_version = IPV6;
112     else
113         return (1);
114
115     ip_chain_t  temp = { 0 };
116     ip_chain_t *final, **list;
117     char *table;
118     int   table_len;
119     char *chain;
120     int   chain_len;
121
122     char *value_copy;
123     char *fields[4];
124     int   fields_num;
125
126     value_copy = strdup (value);
127     if (value_copy == NULL)
128     {
129         char errbuf[1024];
130         ERROR ("strdup failed: %s",
131                 sstrerror (errno, errbuf, sizeof (errbuf)));
132         return (1);
133     }
134
135     /*
136      *  Time to fill the temp element
137      *  Examine value string, it should look like:
138      *  Chain[6] <table> <chain> [<comment|num> [name]]
139      */
140
141     /* set IPv4 or IPv6 */
142     temp.ip_version = ip_version;
143
144     /* Chain <table> <chain> [<comment|num> [name]] */
145     fields_num = strsplit (value_copy, fields, 4);
146     if (fields_num < 2)
147     {
148         free (value_copy);
149         return (1);
150     }
151
152     table = fields[0];
153     chain = fields[1];
154
155     table_len = strlen (table) + 1;
156     if ((unsigned int)table_len > sizeof(temp.table))
157     {
158         ERROR ("Table `%s' too long.", table);
159         free (value_copy);
160         return (1);
161     }
162     sstrncpy (temp.table, table, table_len);
163
164     chain_len = strlen (chain) + 1;
165     if ((unsigned int)chain_len > sizeof(temp.chain))
166     {
167         ERROR ("Chain `%s' too long.", chain);
168         free (value_copy);
169         return (1);
170     }
171     sstrncpy (temp.chain, chain, chain_len);
172
173     if (fields_num >= 3)
174     {
175         char *comment = fields[2];
176         int   rule = atoi (comment);
177
178         if (rule)
179         {
180             temp.rule.num = rule;
181             temp.rule_type = RTYPE_NUM;
182         }
183         else
184         {
185             temp.rule.comment = strdup (comment);
186             if (temp.rule.comment == NULL)
187             {
188                 free (value_copy);
189                 return (1);
190             }
191             temp.rule_type = RTYPE_COMMENT;
192         }
193     }
194     else
195     {
196         temp.rule_type = RTYPE_COMMENT_ALL;
197     }
198
199     if (fields_num >= 4)
200         sstrncpy (temp.name, fields[3], sizeof (temp.name));
201
202     free (value_copy);
203     value_copy = NULL;
204     table = NULL;
205     chain = NULL;
206
207     list = realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
208     if (list == NULL)
209     {
210         char errbuf[1024];
211         ERROR ("realloc failed: %s",
212                 sstrerror (errno, errbuf, sizeof (errbuf)));
213         sfree (temp.rule.comment);
214         return (1);
215     }
216
217     chain_list = list;
218     final = malloc(sizeof (*final));
219     if (final == NULL)
220     {
221         char errbuf[1024];
222         ERROR ("malloc failed: %s",
223                 sstrerror (errno, errbuf, sizeof (errbuf)));
224         sfree (temp.rule.comment);
225         return (1);
226     }
227     memcpy (final, &temp, sizeof (temp));
228     chain_list[chain_num] = final;
229     chain_num++;
230
231     DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
232
233     return (0);
234 } /* int iptables_config */
235
236 static int submit6_match (const struct ip6t_entry_match *match,
237                           const struct ip6t_entry *entry,
238                           const ip_chain_t *chain,
239                           int rule_num)
240 {
241     int status;
242     value_t values[1];
243     value_list_t vl = VALUE_LIST_INIT;
244
245     /* Select the rules to collect */
246     if (chain->rule_type == RTYPE_NUM)
247     {
248         if (chain->rule.num != rule_num)
249             return (0);
250     }
251     else
252     {
253         if (strcmp (match->u.user.name, "comment") != 0)
254             return (0);
255         if ((chain->rule_type == RTYPE_COMMENT)
256              && (strcmp (chain->rule.comment, (char *) match->data) != 0))
257             return (0);
258     }
259
260     vl.values = values;
261     vl.values_len = 1;
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     values[0].derive = (derive_t) entry->counters.bcnt;
286     plugin_dispatch_values (&vl);
287
288     sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
289     values[0].derive = (derive_t) entry->counters.pcnt;
290     plugin_dispatch_values (&vl);
291
292     return (0);
293 } /* int submit_match */
294
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_t values[1];
304     value_list_t vl = VALUE_LIST_INIT;
305
306     /* Select the rules to collect */
307     if (chain->rule_type == RTYPE_NUM)
308     {
309         if (chain->rule.num != rule_num)
310             return (0);
311     }
312     else
313     {
314         if (strcmp (match->u.user.name, "comment") != 0)
315             return (0);
316         if ((chain->rule_type == RTYPE_COMMENT)
317              && (strcmp (chain->rule.comment, (char *) match->data) != 0))
318             return (0);
319     }
320
321     vl.values = values;
322     vl.values_len = 1;
323     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
324     sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
325
326     status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
327                         "%s-%s", chain->table, chain->chain);
328     if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
329         return (0);
330
331     if (chain->name[0] != '\0')
332     {
333         sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
334     }
335     else
336     {
337         if (chain->rule_type == RTYPE_NUM)
338             ssnprintf (vl.type_instance, sizeof (vl.type_instance),
339                        "%i", chain->rule.num);
340         else
341             sstrncpy (vl.type_instance, (char *) match->data,
342                       sizeof (vl.type_instance));
343     }
344
345     sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
346     values[0].derive = (derive_t) entry->counters.bcnt;
347     plugin_dispatch_values (&vl);
348
349     sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
350     values[0].derive = (derive_t) entry->counters.pcnt;
351     plugin_dispatch_values (&vl);
352
353     return (0);
354 } /* int submit_match */
355
356
357 /* ipv6 submit_chain */
358 static void submit6_chain (ip6tc_handle_t *handle, ip_chain_t *chain)
359 {
360     const struct ip6t_entry *entry;
361     int rule_num;
362
363     /* Find first rule for chain and use the iterate macro */
364     entry = ip6tc_first_rule( chain->chain, handle );
365     if (entry == NULL)
366     {
367         DEBUG ("ip6tc_first_rule failed: %s", ip6tc_strerror (errno));
368         return;
369     }
370
371     rule_num = 1;
372     while (entry)
373     {
374         if (chain->rule_type == RTYPE_NUM)
375         {
376             submit6_match (NULL, entry, chain, rule_num);
377         }
378         else
379         {
380             IP6T_MATCH_ITERATE( entry, submit6_match, entry, chain, rule_num );
381         }
382
383         entry = ip6tc_next_rule( entry, handle );
384         rule_num++;
385     } /* while (entry) */
386 }
387
388
389 /* ipv4 submit_chain */
390 static void submit_chain (iptc_handle_t *handle, ip_chain_t *chain)
391 {
392     const struct ipt_entry *entry;
393     int rule_num;
394
395     /* Find first rule for chain and use the iterate macro */
396     entry = iptc_first_rule( chain->chain, handle );
397     if (entry == NULL)
398     {
399         DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
400         return;
401     }
402
403     rule_num = 1;
404     while (entry)
405     {
406         if (chain->rule_type == RTYPE_NUM)
407         {
408             submit_match (NULL, entry, chain, rule_num);
409         }
410         else
411         {
412             IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
413         }
414
415         entry = iptc_next_rule( entry, handle );
416         rule_num++;
417     } /* while (entry) */
418 }
419
420
421 static int iptables_read (void)
422 {
423     int num_failures = 0;
424     ip_chain_t *chain;
425
426     /* Init the iptc handle structure and query the correct table */
427     for (int i = 0; i < chain_num; i++)
428     {
429         chain = chain_list[i];
430
431         if (!chain)
432         {
433             DEBUG ("iptables plugin: chain == NULL");
434             continue;
435         }
436
437         if ( chain->ip_version == IPV4 )
438         {
439 #ifdef HAVE_IPTC_HANDLE_T
440             iptc_handle_t _handle;
441             iptc_handle_t *handle = &_handle;
442
443             *handle = iptc_init (chain->table);
444 #else
445             iptc_handle_t *handle;
446             handle = iptc_init (chain->table);
447 #endif
448
449             if (!handle)
450             {
451                 ERROR ("iptables plugin: iptc_init (%s) failed: %s",
452                         chain->table, iptc_strerror (errno));
453                 num_failures++;
454                 continue;
455             }
456
457             submit_chain (handle, chain);
458             iptc_free (handle);
459         }
460         else if ( chain->ip_version == IPV6 )
461         {
462 #ifdef HAVE_IP6TC_HANDLE_T
463             ip6tc_handle_t _handle;
464             ip6tc_handle_t *handle = &_handle;
465
466             *handle = ip6tc_init (chain->table);
467 #else
468             ip6tc_handle_t *handle;
469             handle = ip6tc_init (chain->table);
470 #endif
471             if (!handle)
472             {
473                 ERROR ("iptables plugin: ip6tc_init (%s) failed: %s",
474                         chain->table, ip6tc_strerror (errno));
475                 num_failures++;
476                 continue;
477             }
478
479             submit6_chain (handle, chain);
480             ip6tc_free (handle);
481         }
482         else
483             num_failures++;
484     } /* for (i = 0 .. chain_num) */
485
486     return ((num_failures < chain_num) ? 0 : -1);
487 } /* int iptables_read */
488
489 static int iptables_shutdown (void)
490 {
491     for (int i = 0; i < chain_num; i++)
492     {
493         if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
494             sfree (chain_list[i]->rule.comment);
495         sfree (chain_list[i]);
496     }
497     sfree (chain_list);
498
499     return (0);
500 } /* int iptables_shutdown */
501
502 void module_register (void)
503 {
504     plugin_register_config ("iptables", iptables_config,
505                              config_keys, config_keys_num);
506     plugin_register_read ("iptables", iptables_read);
507     plugin_register_shutdown ("iptables", iptables_shutdown);
508 } /* void module_register */
509