e1694af3475407ae75ba98de73e8b48598e63a56
[collectd.git] / src / iptables.c
1 /**
2  * collectd - src/iptables.c
3  * Copyright (C) 2007 Sjoerd van der Berg
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *  Sjoerd van der Berg <harekiet at users.sourceforge.net>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26 #include "configfile.h"
27
28 #if OWN_LIBIPTC
29 # include "libiptc/libiptc.h"
30 #else
31 # include <libiptc/libiptc.h>
32 #endif
33
34 /*
35  * (Module-)Global variables
36  */
37
38 /*
39  * Config format should be `Chain table chainname',
40  * e. g. `Chain mangle incoming'
41  */
42 static const char *config_keys[] =
43 {
44         "Chain",
45         NULL
46 };
47 static int config_keys_num = 1;
48 /*
49     Each table/chain combo that will be queried goes into this list
50 */
51 #ifndef XT_TABLE_MAXNAMELEN
52 # define XT_TABLE_MAXNAMELEN 32
53 #endif
54 typedef struct {
55     char table[XT_TABLE_MAXNAMELEN];
56     char chain[XT_TABLE_MAXNAMELEN];
57     union
58     {
59         int   num;
60         char *comment;
61     } rule;
62     enum
63     {
64         RTYPE_NUM,
65         RTYPE_COMMENT,
66         RTYPE_COMMENT_ALL
67     } rule_type;
68     char name[64];
69 } ip_chain_t;
70
71 static ip_chain_t **chain_list = NULL;
72 static int chain_num = 0;
73
74 static int iptables_config (const char *key, const char *value)
75 {
76         if (strcasecmp (key, "Chain") == 0)
77         {
78                 ip_chain_t temp, *final, **list;
79                 char *table;
80                 int   table_len;
81                 char *chain;
82                 int   chain_len;
83
84                 char *value_copy;
85                 char *fields[4];
86                 int   fields_num;
87                 
88                 memset (&temp, 0, sizeof (temp));
89
90                 value_copy = strdup (value);
91                 if (value_copy == NULL)
92                 {
93                     char errbuf[1024];
94                     ERROR ("strdup failed: %s",
95                             sstrerror (errno, errbuf, sizeof (errbuf)));
96                     return (1);
97                 }
98
99                 /* Chain <table> <chain> [<comment|num> [name]] */
100                 fields_num = strsplit (value_copy, fields, 4);
101                 if (fields_num < 2)
102                 {
103                     free (value_copy);
104                     return (1);
105                 }
106
107                 table = fields[0];
108                 chain = fields[1];
109
110                 table_len = strlen (table) + 1;
111                 if ((unsigned int)table_len > sizeof(temp.table))
112                 {
113                         ERROR ("Table `%s' too long.", table);
114                         free (value_copy);
115                         return (1);
116                 }
117                 sstrncpy (temp.table, table, table_len);
118
119                 chain_len = strlen (chain) + 1;
120                 if ((unsigned int)chain_len > sizeof(temp.chain))
121                 {
122                         ERROR ("Chain `%s' too long.", chain);
123                         free (value_copy);
124                         return (1);
125                 }
126                 sstrncpy (temp.chain, chain, chain_len);
127
128                 if (fields_num >= 3)
129                 {
130                     char *comment = fields[2];
131                     int   rule = atoi (comment);
132
133                     if (rule)
134                     {
135                         temp.rule.num = rule;
136                         temp.rule_type = RTYPE_NUM;
137                     }
138                     else
139                     {
140                         temp.rule.comment = strdup (comment);
141                         if (temp.rule.comment == NULL)
142                         {
143                             free (value_copy);
144                             return (1);
145                         }
146                         temp.rule_type = RTYPE_COMMENT;
147                     }
148                 }
149                 else
150                 {
151                     temp.rule_type = RTYPE_COMMENT_ALL;
152                 }
153
154                 if (fields_num >= 4)
155                     sstrncpy (temp.name, fields[3], sizeof (temp.name));
156
157                 free (value_copy);
158                 value_copy = NULL;
159                 table = NULL;
160                 chain = NULL;
161
162                 list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *));
163                 if (list == NULL)
164                 {
165                     char errbuf[1024];
166                     ERROR ("realloc failed: %s",
167                             sstrerror (errno, errbuf, sizeof (errbuf)));
168                     return (1);
169                 }
170
171                 chain_list = list;
172                 final = (ip_chain_t *) malloc( sizeof(temp) );
173                 if (final == NULL) 
174                 {
175                     char errbuf[1024];
176                     ERROR ("malloc failed: %s",
177                             sstrerror (errno, errbuf, sizeof (errbuf)));
178                     return (1);
179                 }
180                 memcpy (final, &temp, sizeof (temp));
181                 chain_list[chain_num] = final;
182                 chain_num++;
183
184                 DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain);
185         }
186         else 
187         {
188                 return (-1);
189         }
190
191         return (0);
192 } /* int iptables_config */
193
194 /* This needs to return `int' for IPT_MATCH_ITERATE to work. */
195 static int submit_match (const struct ipt_entry_match *match,
196                 const struct ipt_entry *entry,
197                 const ip_chain_t *chain,
198                 int rule_num) 
199 {
200     int status;
201     value_t values[1];
202     value_list_t vl = VALUE_LIST_INIT;
203
204     /* Select the rules to collect */
205     if (chain->rule_type == RTYPE_NUM)
206     {
207         if (chain->rule.num != rule_num)
208             return (0);
209     }
210     else
211     {
212         if (strcmp (match->u.user.name, "comment") != 0)
213             return (0);
214         if ((chain->rule_type == RTYPE_COMMENT)
215                 && (strcmp (chain->rule.comment, (char *) match->data) != 0))
216             return (0);
217     }
218
219     vl.values = values;
220     vl.values_len = 1;
221     vl.time = time (NULL);
222     sstrncpy (vl.host, hostname_g, sizeof (vl.host));
223     sstrncpy (vl.plugin, "iptables", sizeof (vl.plugin));
224
225     status = ssnprintf (vl.plugin_instance, sizeof (vl.plugin_instance),
226             "%s-%s", chain->table, chain->chain);
227     if ((status < 1) || ((unsigned int)status >= sizeof (vl.plugin_instance)))
228         return (0);
229
230     if (chain->name[0] != '\0')
231     {
232         sstrncpy (vl.type_instance, chain->name, sizeof (vl.type_instance));
233     }
234     else
235     {
236         if (chain->rule_type == RTYPE_NUM)
237             ssnprintf (vl.type_instance, sizeof (vl.type_instance),
238                     "%i", chain->rule.num);
239         else
240             sstrncpy (vl.type_instance, (char *) match->data,
241                     sizeof (vl.type_instance));
242     }
243
244     sstrncpy (vl.type, "ipt_bytes", sizeof (vl.type));
245     values[0].counter = (counter_t) entry->counters.bcnt;
246     plugin_dispatch_values (&vl);
247
248     sstrncpy (vl.type, "ipt_packets", sizeof (vl.type));
249     values[0].counter = (counter_t) entry->counters.pcnt;
250     plugin_dispatch_values (&vl);
251
252     return (0);
253 } /* void submit_match */
254
255 static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) {
256     const struct ipt_entry *entry;
257     int rule_num;
258
259     /* Find first rule for chain and use the iterate macro */    
260     entry = iptc_first_rule( chain->chain, handle );
261     if (entry == NULL)
262     {
263         DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno));
264         return;
265     }
266
267     rule_num = 1;
268     while (entry)
269     {
270         if (chain->rule_type == RTYPE_NUM)
271         {
272             submit_match (NULL, entry, chain, rule_num);
273         }
274         else
275         {
276             IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num );
277         }
278
279         entry = iptc_next_rule( entry, handle );
280         rule_num++;
281     } /* while (entry) */
282 }
283
284
285 static int iptables_read (void)
286 {
287     int i;
288     int num_failures = 0;
289
290     /* Init the iptc handle structure and query the correct table */    
291     for (i = 0; i < chain_num; i++)
292     {
293         iptc_handle_t handle;
294         ip_chain_t *chain;
295         
296         chain = chain_list[i];
297         if (!chain)
298         {
299             DEBUG ("iptables plugin: chain == NULL");
300             continue;
301         }
302
303         handle = iptc_init (chain->table);
304         if (!handle)
305         {
306             ERROR ("iptables plugin: iptc_init (%s) failed: %s",
307                     chain->table, iptc_strerror (errno));
308             num_failures++;
309             continue;
310         }
311
312         submit_chain (&handle, chain);
313         iptc_free (&handle);
314     } /* for (i = 0 .. chain_num) */
315
316     return ((num_failures < chain_num) ? 0 : -1);
317 } /* int iptables_read */
318
319 static int iptables_shutdown (void)
320 {
321     int i;
322
323     for (i = 0; i < chain_num; i++)
324     {
325         if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT))
326         {
327             sfree (chain_list[i]->rule.comment);
328         }
329         sfree (chain_list[i]);
330     }
331     sfree (chain_list);
332
333     return (0);
334 } /* int iptables_shutdown */
335
336 void module_register (void)
337 {
338     plugin_register_config ("iptables", iptables_config,
339             config_keys, config_keys_num);
340     plugin_register_read ("iptables", iptables_read);
341     plugin_register_shutdown ("iptables", iptables_shutdown);
342 } /* void module_register */
343
344 /*
345  * vim:shiftwidth=4:softtabstop=4:tabstop=8
346  */