X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fiptables.c;h=5fa1f4086a8aa23dd5bfff14e289f0e80845d65c;hb=add941762db8b575003b050c3c59fed121ccbdbb;hp=d534d37863784d394827b41f79fe780a31404b10;hpb=8de91034a0186feba7079b92711b0d140278ce7a;p=collectd.git diff --git a/src/iptables.c b/src/iptables.c index d534d378..5fa1f408 100644 --- a/src/iptables.c +++ b/src/iptables.c @@ -29,194 +29,318 @@ # include #endif -#define MODULE_NAME "iptables" -#define BUFSIZE 512 - /* * (Module-)Global variables */ /* - * Files will go into iptables-chain/comment.rrd files - */ -static char *file_template = "iptables-%s.rrd"; - -/* * Config format should be `Chain table chainname', * e. g. `Chain mangle incoming' */ -static char *config_keys[] = +static const char *config_keys[] = { "Chain", NULL }; static int config_keys_num = 1; /* - Removed packet count for now, should have config option if you want to save them - Although other collectd models don't seem to care much for options eitherway for what to log -*/ -static char *ds_def[] = -{ -// "DS:packets:COUNTER:"COLLECTD_HEARTBEAT":0:U", - "DS:bytes:DERIVE:"COLLECTD_HEARTBEAT":0:U", - NULL -}; -static int ds_num = 1; - -/* Each table/chain combo that will be queried goes into this list */ +#ifndef XT_TABLE_MAXNAMELEN +# define XT_TABLE_MAXNAMELEN 32 +#endif typedef struct { - char table[16]; - char name[32]; + char table[XT_TABLE_MAXNAMELEN]; + char chain[XT_TABLE_MAXNAMELEN]; + union + { + int num; + char *comment; + } rule; + enum + { + RTYPE_NUM, + RTYPE_COMMENT, + RTYPE_COMMENT_ALL + } rule_type; + char name[64]; } ip_chain_t; static ip_chain_t **chain_list = NULL; static int chain_num = 0; -static int iptables_config (char *key, char *value) +static int iptables_config (const char *key, const char *value) { if (strcasecmp (key, "Chain") == 0) { ip_chain_t temp, *final, **list; + char *table; + int table_len; char *chain; - int tLen; + int chain_len; + + char *value_copy; + char *fields[4]; + int fields_num; - memset( &temp, 0, sizeof( temp )); - - /* simple parsing, only allow a space... */ - chain = rindex(value, ' ' ); - if (!chain) + memset (&temp, 0, sizeof (temp)); + + value_copy = strdup (value); + if (value_copy == NULL) + { + char errbuf[1024]; + ERROR ("strdup failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (1); + } + + /* Chain [ [name]] */ + fields_num = strsplit (value_copy, fields, 4); + if (fields_num < 2) + { + free (value_copy); + return (1); + } + + table = fields[0]; + chain = fields[1]; + + table_len = strlen (table); + if (table_len >= sizeof(temp.table)) { - syslog (LOG_EMERG, "missing chain." ); + ERROR ("Table `%s' too long.", table); + free (value_copy); return (1); } - tLen = (int)(chain - value); - if ( tLen > sizeof( temp.table )) + strncpy (temp.table, table, table_len); + temp.table[table_len] = '\0'; + + chain_len = strlen (chain); + if (chain_len >= sizeof(temp.chain)) { - syslog (LOG_EMERG, "table too long." ); + ERROR ("Chain `%s' too long.", chain); + free (value_copy); return (1); } - memcpy( temp.table, value, tLen ); - temp.table[tLen] = 0; - chain++; - strncpy( temp.name, chain, sizeof( temp.name )); - + strncpy (temp.chain, chain, chain_len); + temp.chain[chain_len] = '\0'; + + if (fields_num >= 3) + { + char *comment = fields[2]; + int rule = atoi (comment); + + if (rule) + { + temp.rule.num = rule; + temp.rule_type = RTYPE_NUM; + } + else + { + temp.rule.comment = strdup (comment); + if (temp.rule.comment == NULL) + { + free (value_copy); + return (1); + } + temp.rule_type = RTYPE_COMMENT; + } + } + else + { + temp.rule_type = RTYPE_COMMENT_ALL; + } + + if (fields_num >= 4) + strncpy (temp.name, fields[3], sizeof (temp.name) - 1); + + free (value_copy); + value_copy = NULL; + table = NULL; + chain = NULL; + list = (ip_chain_t **) realloc (chain_list, (chain_num + 1) * sizeof (ip_chain_t *)); - if ( list == NULL ) + if (list == NULL) { - syslog (LOG_EMERG, "Cannot allocate more memory."); - return (1); + char errbuf[1024]; + ERROR ("realloc failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (1); } + chain_list = list; final = (ip_chain_t *) malloc( sizeof(temp) ); if (final == NULL) { - syslog (LOG_EMERG, "Cannot allocate memory."); - return (1); + char errbuf[1024]; + ERROR ("malloc failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); + return (1); } - *final = temp; - chain_list[chain_num++] = final; - } else + memcpy (final, &temp, sizeof (temp)); + chain_list[chain_num] = final; + chain_num++; + + DEBUG ("Chain #%i: table = %s; chain = %s;", chain_num, final->table, final->chain); + } + else { return (-1); } return (0); -} - -static void iptables_init (void) -{ - return; -} - -static void iptables_write (char *host, char *inst, char *val) -{ - char file[BUFSIZE]; - int status; - - status = snprintf (file, BUFSIZE, file_template, inst); - if (status < 1) - return; - else if (status >= BUFSIZE) - return; - - rrd_update_file (host, file, val, ds_def, ds_num); -} - +} /* int iptables_config */ +/* This needs to return `int' for IPT_MATCH_ITERATE to work. */ static int submit_match (const struct ipt_entry_match *match, - const struct ipt_entry *entry, const ip_chain_t *chain) + const struct ipt_entry *entry, + const ip_chain_t *chain, + int rule_num) { - char name[BUFSIZE]; - char buf[BUFSIZE]; int status; + value_t values[1]; + value_list_t vl = VALUE_LIST_INIT; - /* Only log rules that have a comment, although could probably also do numerical targets sometime */ - if ( strcmp( match->u.user.name, "comment" ) ) - return 0; + /* Select the rules to collect */ + if (chain->rule_type == RTYPE_NUM) + { + if (chain->rule.num != rule_num) + return (0); + } + else + { + if (strcmp (match->u.user.name, "comment") != 0) + return (0); + if ((chain->rule_type == RTYPE_COMMENT) + && (strcmp (chain->rule.comment, (char *) match->data) != 0)) + return (0); + } -/* - This would also add the table name to the name, but seems a bit overkill - status = snprintf (name, BUFSIZE, "%s-%s/%s", - table->table, table->chain, match->data ); -*/ - status = snprintf (name, BUFSIZE, "%s/%s", chain->name, match->data ); + vl.values = values; + vl.values_len = 1; + vl.time = time (NULL); + strcpy (vl.host, hostname_g); + strcpy (vl.plugin, "iptables"); + + status = snprintf (vl.plugin_instance, sizeof (vl.plugin_instance), + "%s-%s", chain->table, chain->chain); + if ((status >= sizeof (vl.plugin_instance)) || (status < 1)) + return (0); - if ((status >= BUFSIZE) || (status < 1)) - return 0; + if (chain->name[0] != '\0') + { + strncpy (vl.type_instance, chain->name, sizeof (vl.type_instance)); + } + else + { + if (chain->rule_type == RTYPE_NUM) + snprintf (vl.type_instance, sizeof (vl.type_instance), + "%i", chain->rule.num); + else + strncpy (vl.type_instance, (char *) match->data, + sizeof (vl.type_instance)); + } + vl.type_instance[sizeof (vl.type_instance) - 1] = '\0'; - status = snprintf (buf, BUFSIZE, "%u:%lld", /* ":lld", */ - (unsigned int) curtime, - /* entry->counters.pcnt, */ - entry->counters.bcnt ); - if ((status >= BUFSIZE) || (status < 1)) - return 0; + values[0].counter = (counter_t) entry->counters.bcnt; + plugin_dispatch_values ("ipt_bytes", &vl); - plugin_submit (MODULE_NAME, name, buf); + values[0].counter = (counter_t) entry->counters.pcnt; + plugin_dispatch_values ("ipt_packets", &vl); - return 0; -} /* int submit_match */ + return (0); +} /* void submit_match */ static void submit_chain( iptc_handle_t *handle, ip_chain_t *chain ) { const struct ipt_entry *entry; + int rule_num; /* Find first rule for chain and use the iterate macro */ - entry = iptc_first_rule( chain->name, handle ); - while ( entry ) { - IPT_MATCH_ITERATE( entry, submit_match, entry, chain ); - entry = iptc_next_rule( entry, handle ); + entry = iptc_first_rule( chain->chain, handle ); + if (entry == NULL) + { + DEBUG ("iptc_first_rule failed: %s", iptc_strerror (errno)); + return; } + + rule_num = 1; + while (entry) + { + if (chain->rule_type == RTYPE_NUM) + { + submit_match (NULL, entry, chain, rule_num); + } + else + { + IPT_MATCH_ITERATE( entry, submit_match, entry, chain, rule_num ); + } + + entry = iptc_next_rule( entry, handle ); + rule_num++; + } /* while (entry) */ } -static void iptables_read (void) { +static int iptables_read (void) +{ int i; + static complain_t complaint; /* Init the iptc handle structure and query the correct table */ - for( i = 0; i < chain_num; i++) { + for (i = 0; i < chain_num; i++) + { iptc_handle_t handle; ip_chain_t *chain; chain = chain_list[i]; if (!chain) + { + DEBUG ("chain == NULL"); continue; + } + handle = iptc_init( chain->table ); if (!handle) + { + DEBUG ("iptc_init (%s) failed: %s", chain->table, iptc_strerror (errno)); + plugin_complain (LOG_ERR, &complaint, "iptc_init (%s) failed: %s", + chain->table, iptc_strerror (errno)); continue; - submit_chain( &handle, chain ); - iptc_free( &handle ); + } + plugin_relief (LOG_INFO, &complaint, "iptc_init (%s) succeeded", + chain->table); + + submit_chain (&handle, chain); + iptc_free (&handle); } -} -void module_register (void) + return (0); +} /* int iptables_read */ + +static int iptables_shutdown (void) { - plugin_register (MODULE_NAME, iptables_init, iptables_read, iptables_write); - cf_register (MODULE_NAME, iptables_config, config_keys, config_keys_num); -} + int i; + + for (i = 0; i < chain_num; i++) + { + if ((chain_list[i] != NULL) && (chain_list[i]->rule_type == RTYPE_COMMENT)) + { + sfree (chain_list[i]->rule.comment); + } + sfree (chain_list[i]); + } + sfree (chain_list); + + return (0); +} /* int iptables_shutdown */ -#undef BUFSIZE -#undef MODULE_NAME +void module_register (void) +{ + plugin_register_config ("iptables", iptables_config, + config_keys, config_keys_num); + plugin_register_read ("iptables", iptables_read); + plugin_register_shutdown ("iptables", iptables_shutdown); +} /* void module_register */ /* * vim:shiftwidth=4:softtabstop=4:tabstop=8