Merge pull request #3329 from efuss/fix-3311
[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 "plugin.h"
30 #include "utils/common/common.h"
31
32 #include <libiptc/libip6tc.h>
33 #include <libiptc/libiptc.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[] = {"Chain", "Chain6"};
67 static int config_keys_num = STATIC_ARRAY_SIZE(config_keys);
68 enum protocol_version_e { IPV4, IPV6 };
69 typedef enum protocol_version_e protocol_version_t;
70
71 /*
72  * Each table/chain combo that will be queried goes into this list
73  */
74 #ifndef XT_TABLE_MAXNAMELEN
75 #define XT_TABLE_MAXNAMELEN 32
76 #endif
77 typedef struct {
78   protocol_version_t ip_version;
79   char table[XT_TABLE_MAXNAMELEN];
80   char chain[XT_TABLE_MAXNAMELEN];
81   union {
82     int num;
83     char *comment;
84   } rule;
85   enum { RTYPE_NUM, RTYPE_COMMENT, RTYPE_COMMENT_ALL } rule_type;
86   char name[64];
87 } ip_chain_t;
88
89 static ip_chain_t **chain_list;
90 static int chain_num;
91
92 static int iptables_config(const char *key, const char *value) {
93   /* int ip_value; */
94   protocol_version_t ip_version = 0;
95
96   if (strcasecmp(key, "Chain") == 0)
97     ip_version = IPV4;
98   else if (strcasecmp(key, "Chain6") == 0)
99     ip_version = IPV6;
100   else
101     return 1;
102
103   ip_chain_t temp = {0};
104   ip_chain_t *final, **list;
105   char *table;
106   char *chain;
107
108   char *value_copy;
109   char *fields[4];
110   int fields_num;
111
112   value_copy = strdup(value);
113   if (value_copy == NULL) {
114     ERROR("strdup failed: %s", STRERRNO);
115     return 1;
116   }
117
118   /*
119    *  Time to fill the temp element
120    *  Examine value string, it should look like:
121    *  Chain[6] <table> <chain> [<comment|num> [name]]
122    */
123
124   /* set IPv4 or IPv6 */
125   temp.ip_version = ip_version;
126
127   /* Chain <table> <chain> [<comment|num> [name]] */
128   fields_num = strsplit(value_copy, fields, 4);
129   if (fields_num < 2) {
130     free(value_copy);
131     return 1;
132   }
133
134   table = fields[0];
135   chain = fields[1];
136
137   size_t table_len = strlen(table) + 1;
138   if (table_len > sizeof(temp.table)) {
139     ERROR("Table `%s' too long.", table);
140     free(value_copy);
141     return 1;
142   }
143   sstrncpy(temp.table, table, table_len);
144
145   size_t chain_len = strlen(chain) + 1;
146   if (chain_len > sizeof(temp.chain)) {
147     ERROR("Chain `%s' too long.", chain);
148     free(value_copy);
149     return 1;
150   }
151   sstrncpy(temp.chain, chain, chain_len);
152
153   if (fields_num >= 3) {
154     char *comment = fields[2];
155     int rule = atoi(comment);
156
157     if (rule) {
158       temp.rule.num = rule;
159       temp.rule_type = RTYPE_NUM;
160     } else {
161       temp.rule.comment = strdup(comment);
162       if (temp.rule.comment == NULL) {
163         free(value_copy);
164         return 1;
165       }
166       temp.rule_type = RTYPE_COMMENT;
167     }
168   } else {
169     temp.rule_type = RTYPE_COMMENT_ALL;
170   }
171
172   if (fields_num >= 4)
173     sstrncpy(temp.name, fields[3], sizeof(temp.name));
174
175   free(value_copy);
176   value_copy = NULL;
177   table = NULL;
178   chain = NULL;
179
180   list = realloc(chain_list, (chain_num + 1) * sizeof(ip_chain_t *));
181   if (list == NULL) {
182     ERROR("realloc failed: %s", STRERRNO);
183     sfree(temp.rule.comment);
184     return 1;
185   }
186
187   chain_list = list;
188   final = malloc(sizeof(*final));
189   if (final == NULL) {
190     ERROR("malloc failed: %s", STRERRNO);
191     sfree(temp.rule.comment);
192     return 1;
193   }
194   memcpy(final, &temp, sizeof(temp));
195   chain_list[chain_num] = final;
196   chain_num++;
197
198   DEBUG("Chain #%i: table = %s; chain = %s;", chain_num, final->table,
199         final->chain);
200
201   return 0;
202 } /* int iptables_config */
203
204 static int submit6_match(const struct ip6t_entry_match *match,
205                          const struct ip6t_entry *entry,
206                          const ip_chain_t *chain, int rule_num) {
207   int status;
208   value_list_t vl = VALUE_LIST_INIT;
209
210   /* Select the rules to collect */
211   if (chain->rule_type == RTYPE_NUM) {
212     if (chain->rule.num != rule_num)
213       return 0;
214   } else {
215     if (strcmp(match->u.user.name, "comment") != 0)
216       return 0;
217     if ((chain->rule_type == RTYPE_COMMENT) &&
218         (strcmp(chain->rule.comment, (char *)match->data) != 0))
219       return 0;
220   }
221
222   sstrncpy(vl.plugin, "ip6tables", sizeof(vl.plugin));
223
224   status = ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
225                      chain->table, chain->chain);
226   if ((status < 1) || ((unsigned int)status >= sizeof(vl.plugin_instance)))
227     return 0;
228
229   if (chain->name[0] != '\0') {
230     sstrncpy(vl.type_instance, chain->name, sizeof(vl.type_instance));
231   } else {
232     if (chain->rule_type == RTYPE_NUM)
233       ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%i",
234                 chain->rule.num);
235     else
236       sstrncpy(vl.type_instance, (char *)match->data, sizeof(vl.type_instance));
237   }
238
239   sstrncpy(vl.type, "ipt_bytes", sizeof(vl.type));
240   vl.values = &(value_t){.derive = (derive_t)entry->counters.bcnt};
241   vl.values_len = 1;
242   plugin_dispatch_values(&vl);
243
244   sstrncpy(vl.type, "ipt_packets", sizeof(vl.type));
245   vl.values = &(value_t){.derive = (derive_t)entry->counters.pcnt};
246   plugin_dispatch_values(&vl);
247
248   return 0;
249 } /* int submit6_match */
250
251 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
252 static int submit_match(const struct ipt_entry_match *match,
253                         const struct ipt_entry *entry, const ip_chain_t *chain,
254                         int rule_num) {
255   int status;
256   value_list_t vl = VALUE_LIST_INIT;
257
258   /* Select the rules to collect */
259   if (chain->rule_type == RTYPE_NUM) {
260     if (chain->rule.num != rule_num)
261       return 0;
262   } else {
263     if (strcmp(match->u.user.name, "comment") != 0)
264       return 0;
265     if ((chain->rule_type == RTYPE_COMMENT) &&
266         (strcmp(chain->rule.comment, (char *)match->data) != 0))
267       return 0;
268   }
269
270   sstrncpy(vl.plugin, "iptables", sizeof(vl.plugin));
271
272   status = ssnprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%s-%s",
273                      chain->table, chain->chain);
274   if ((status < 1) || ((unsigned int)status >= sizeof(vl.plugin_instance)))
275     return 0;
276
277   if (chain->name[0] != '\0') {
278     sstrncpy(vl.type_instance, chain->name, sizeof(vl.type_instance));
279   } else {
280     if (chain->rule_type == RTYPE_NUM)
281       ssnprintf(vl.type_instance, sizeof(vl.type_instance), "%i",
282                 chain->rule.num);
283     else
284       sstrncpy(vl.type_instance, (char *)match->data, sizeof(vl.type_instance));
285   }
286
287   sstrncpy(vl.type, "ipt_bytes", sizeof(vl.type));
288   vl.values = &(value_t){.derive = (derive_t)entry->counters.bcnt};
289   vl.values_len = 1;
290   plugin_dispatch_values(&vl);
291
292   sstrncpy(vl.type, "ipt_packets", sizeof(vl.type));
293   vl.values = &(value_t){.derive = (derive_t)entry->counters.pcnt};
294   plugin_dispatch_values(&vl);
295
296   return 0;
297 } /* int submit_match */
298
299 /* ipv6 submit_chain */
300 static void submit6_chain(ip6tc_handle_t *handle, ip_chain_t *chain) {
301   const struct ip6t_entry *entry;
302   int rule_num;
303
304   /* Find first rule for chain and use the iterate macro */
305   entry = ip6tc_first_rule(chain->chain, handle);
306   if (entry == NULL) {
307     DEBUG("ip6tc_first_rule failed: %s", ip6tc_strerror(errno));
308     return;
309   }
310
311   rule_num = 1;
312   while (entry) {
313     if (chain->rule_type == RTYPE_NUM) {
314       submit6_match(NULL, entry, chain, rule_num);
315     } else {
316       IP6T_MATCH_ITERATE(entry, submit6_match, entry, chain, rule_num);
317     }
318
319     entry = ip6tc_next_rule(entry, handle);
320     rule_num++;
321   } /* while (entry) */
322 }
323
324 /* ipv4 submit_chain */
325 static void submit_chain(iptc_handle_t *handle, ip_chain_t *chain) {
326   const struct ipt_entry *entry;
327   int rule_num;
328
329   /* Find first rule for chain and use the iterate macro */
330   entry = iptc_first_rule(chain->chain, handle);
331   if (entry == NULL) {
332     DEBUG("iptc_first_rule failed: %s", iptc_strerror(errno));
333     return;
334   }
335
336   rule_num = 1;
337   while (entry) {
338     if (chain->rule_type == RTYPE_NUM) {
339       submit_match(NULL, entry, chain, rule_num);
340     } else {
341       IPT_MATCH_ITERATE(entry, submit_match, entry, chain, rule_num);
342     }
343
344     entry = iptc_next_rule(entry, handle);
345     rule_num++;
346   } /* while (entry) */
347 }
348
349 static int iptables_read(void) {
350   int num_failures = 0;
351   ip_chain_t *chain;
352
353   /* Init the iptc handle structure and query the correct table */
354   for (int i = 0; i < chain_num; i++) {
355     chain = chain_list[i];
356
357     if (!chain) {
358       DEBUG("iptables plugin: chain == NULL");
359       continue;
360     }
361
362     if (chain->ip_version == IPV4) {
363 #ifdef HAVE_IPTC_HANDLE_T
364       iptc_handle_t _handle;
365       iptc_handle_t *handle = &_handle;
366
367       *handle = iptc_init(chain->table);
368 #else
369       iptc_handle_t *handle;
370       handle = iptc_init(chain->table);
371 #endif
372
373       if (!handle) {
374         ERROR("iptables plugin: iptc_init (%s) failed: %s", chain->table,
375               iptc_strerror(errno));
376         num_failures++;
377         continue;
378       }
379
380       submit_chain(handle, chain);
381       iptc_free(handle);
382     } else if (chain->ip_version == IPV6) {
383 #ifdef HAVE_IP6TC_HANDLE_T
384       ip6tc_handle_t _handle;
385       ip6tc_handle_t *handle = &_handle;
386
387       *handle = ip6tc_init(chain->table);
388 #else
389       ip6tc_handle_t *handle;
390       handle = ip6tc_init(chain->table);
391 #endif
392       if (!handle) {
393         ERROR("iptables plugin: ip6tc_init (%s) failed: %s", chain->table,
394               ip6tc_strerror(errno));
395         num_failures++;
396         continue;
397       }
398
399       submit6_chain(handle, chain);
400       ip6tc_free(handle);
401     } else
402       num_failures++;
403   } /* for (i = 0 .. chain_num) */
404
405   return (num_failures < chain_num) ? 0 : -1;
406 } /* int iptables_read */
407
408 static int iptables_shutdown(void) {
409   for (int i = 0; i < chain_num; i++) {
410     if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
411       sfree(chain_list[i]->rule.comment);
412     sfree(chain_list[i]);
413   }
414   sfree(chain_list);
415
416   return 0;
417 } /* int iptables_shutdown */
418
419 static int iptables_init(void) {
420 #if defined(HAVE_SYS_CAPABILITY_H) && defined(CAP_NET_ADMIN)
421   if (check_capability(CAP_NET_ADMIN) != 0) {
422     if (getuid() == 0)
423       WARNING("iptables plugin: Running collectd as root, but the "
424               "CAP_NET_ADMIN capability is missing. The plugin's read "
425               "function will probably fail. Is your init system dropping "
426               "capabilities?");
427     else
428       WARNING("iptables plugin: collectd doesn't have the CAP_NET_ADMIN "
429               "capability. If you don't want to run collectd as root, try "
430               "running \"setcap cap_net_admin=ep\" on the collectd binary.");
431   }
432 #endif
433   return 0;
434 } /* int iptables_init */
435
436 void module_register(void) {
437   plugin_register_config("iptables", iptables_config, config_keys,
438                          config_keys_num);
439   plugin_register_init("iptables", iptables_init);
440   plugin_register_read("iptables", iptables_read);
441   plugin_register_shutdown("iptables", iptables_shutdown);
442 } /* void module_register */