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