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