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