snort plugin: Add a bounady check when reading from the metrics array.
[collectd.git] / src / snort.c
1 /**
2  * collectd - src/snort.c
3  * Copyright (C) 2013 Kris Nielander
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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Kris Nielander <nielander@fox-it.com>
20  *
21  * This plugin is based on the snmp plugin by Florian octo Forster.
22  *
23  **/
24
25 #include "collectd.h"
26 #include "plugin.h" /* plugin_register_*, plugin_dispatch_values */
27 #include "common.h" /* auxiliary functions */
28 #include <sys/mman.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 struct metric_definition_s {
35     char *name;
36     char *type;
37     int data_source_type;
38     int index;
39     struct metric_definition_s *next;
40 };
41 typedef struct metric_definition_s metric_definition_t;
42
43 struct instance_definition_s {
44     char *name;
45     char *path;
46     metric_definition_t **metric_list;
47     int metric_list_len;
48     cdtime_t last;
49     cdtime_t interval;
50     struct instance_definition_s *next;
51 };
52 typedef struct instance_definition_s instance_definition_t;
53
54 /* Private */
55 static metric_definition_t *metric_head = NULL;
56
57 static int snort_read_submit(instance_definition_t *id, metric_definition_t *md,
58     const char *buf){
59
60     /* Registration variables */
61     value_t value;
62     value_list_t vl = VALUE_LIST_INIT;
63
64     DEBUG("snort plugin: plugin_instance=%s type=%s value=%s", id->name,
65         md->type, buf);
66
67     if (buf == NULL)
68         return (-1);
69
70     /* Parse value */
71     parse_value(buf, &value, md->data_source_type);
72
73     /* Register */
74     vl.values_len = 1;
75     vl.values = &value;
76
77     sstrncpy(vl.host, hostname_g, sizeof (vl.host));
78     sstrncpy(vl.plugin, "snort", sizeof(vl.plugin));
79     sstrncpy(vl.plugin_instance, id->name, sizeof(vl.plugin_instance));
80     sstrncpy(vl.type, md->type, sizeof(vl.type));
81
82     vl.time = id->last;
83     vl.interval = id->interval;
84
85     DEBUG("snort plugin: -> plugin_dispatch_values (&vl);");
86     plugin_dispatch_values(&vl);
87
88     return (0);
89 }
90
91 static int snort_read(user_data_t *ud){
92     instance_definition_t *id;
93     metric_definition_t *md;
94
95     int i;
96     int fd;
97     int metrics_num;
98
99     char **metrics;
100     char **metrics_t;
101
102     struct stat sb;
103     char *buf, *buf_t;
104
105     /* mmap, char pointers */
106     char *p_start;
107     char *p_end;
108
109     id = ud->data;
110     DEBUG("snort plugin: snort_read (instance = %s)", id->name);
111
112     fd = open(id->path, O_RDONLY);
113     if (fd == -1){
114         ERROR("snort plugin: Unable to open `%s'.", id->path);
115         return (-1);
116     }
117
118     if ((fstat(fd, &sb) != 0) || (!S_ISREG(sb.st_mode))){
119         ERROR("snort plugin: `%s' is not a file.", id->path);
120         return (-1);
121     }
122
123     if (sb.st_size == 0){
124         ERROR("snort plugin: `%s' is empty.", id->path);
125         return (-1);
126     }
127
128     p_start = mmap(/* addr = */ NULL, sb.st_size, PROT_READ, MAP_SHARED, fd,
129         /* offset = */ 0);
130     if (p_start == MAP_FAILED){
131         ERROR("snort plugin: mmap error");
132         return (-1);
133     }
134
135     /* Set the start value count. */
136     metrics_num = 1;
137
138     /* Set the pointer to the last line of the file and count the fields.
139      (Skip the last two characters of the buffer: `\n' and `\0') */
140     for (p_end = (p_start + sb.st_size) - 2; p_end > p_start; --p_end){
141         if (*p_end == ','){
142             ++metrics_num;
143         } else if (*p_end == '\n'){
144             ++p_end;
145             break;
146         }
147     }
148
149     if (metrics_num == 1){
150         ERROR("snort plugin: last line of `%s' does not contain enough values.", id->path);
151         return (-1);
152     }
153
154     if (*p_end == '#'){
155         ERROR("snort plugin: last line of `%s' is a comment.", id->path);
156         return (-1);
157     }
158
159     /* Copy the line to the buffer */
160     buf_t = buf = strdup(p_end);
161
162     /* Done with mmap and file pointer */
163     close(fd);
164     munmap(p_start, sb.st_size);
165
166     /* Create a list of all values */
167     metrics = calloc (metrics_num, sizeof (*metrics));
168     if (metrics == NULL) {
169         ERROR ("snort plugin: calloc failed.");
170         return (-1);
171     }
172
173     for (metrics_t = metrics; (*metrics_t = strsep(&buf_t, ",")) != NULL;)
174         if (**metrics_t != '\0')
175             if (++metrics_t >= &metrics[metrics_num])
176                 break;
177
178     /* Set last time */
179     id->last = TIME_T_TO_CDTIME_T(strtol(*metrics, NULL, 0));
180
181     /* Register values */
182     for (i = 0; i < id->metric_list_len; ++i){
183         md = id->metric_list[i];
184
185         if (md->index >= metrics_num) {
186             ERROR ("snort plugin: Metric \"%s\": Request for index %i when "
187                     "only %i fields are available.",
188                     md->name, md->index, metrics_num);
189             continue;
190         }
191
192         snort_read_submit(id, md, metrics[md->index]);
193     }
194
195     /* Free up resources */
196     free(metrics);
197     free(buf);
198     return (0);
199 }
200
201 static void snort_metric_definition_destroy(void *arg){
202     metric_definition_t *md;
203
204     md = arg;
205     if (md == NULL)
206         return;
207
208     if (md->name != NULL)
209         DEBUG("snort plugin: Destroying metric definition `%s'.", md->name);
210
211     sfree(md->name);
212     sfree(md->type);
213     sfree(md);
214 }
215
216 static int snort_config_add_metric_index(metric_definition_t *md, oconfig_item_t *ci){
217     if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)){
218         WARNING("snort plugin: `Index' needs exactly one integer argument.");
219         return (-1);
220     }
221
222     md->index = (int)ci->values[0].value.number;
223     if (md->index <= 0){
224         WARNING("snort plugin: `Index' must be higher than 0.");
225         return (-1);
226     }
227
228     return (0);
229 }
230
231 /* Parse metric  */
232 static int snort_config_add_metric(oconfig_item_t *ci){
233     metric_definition_t *md;
234     const data_set_t *ds;
235     int status = 0;
236     int i;
237
238     if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)){
239         WARNING("snort plugin: The `Metric' config option needs exactly one string argument.");
240         return (-1);
241     }
242
243     md = (metric_definition_t *)malloc(sizeof(*md));
244     if (md == NULL)
245         return (-1);
246     memset(md, 0, sizeof(*md));
247
248     md->name = strdup(ci->values[0].value.string);
249     if (md->name == NULL){
250         free(md);
251         return (-1);
252     }
253
254     for (i = 0; i < ci->children_num; ++i){
255         oconfig_item_t *option = ci->children + i;
256         status = 0;
257
258         if (strcasecmp("Type", option->key) == 0)
259             status = cf_util_get_string(option, &md->type);
260         else if (strcasecmp("Index", option->key) == 0)
261             status = snort_config_add_metric_index(md, option);
262         else {
263             WARNING("snort plugin: Option `%s' not allowed here.", option->key);
264             status = -1;
265         }
266
267         if (status != 0)
268             break;
269     }
270
271     if (status != 0){
272         snort_metric_definition_destroy(md);
273         return (-1);
274     }
275
276     /* Verify all necessary options have been set. */
277     if (md->type == NULL){
278         WARNING("snort plugin: Option `Type' must be set.");
279         status = -1;
280     } else if (md->index == 0){
281         WARNING("snort plugin: Option `Index' must be set.");
282         status = -1;
283     }
284
285     if (status != 0){
286         snort_metric_definition_destroy(md);
287         return (-1);
288     }
289
290     /* Retrieve the data source type from the types db. */
291     ds = plugin_get_ds(md->type);
292     if (ds == NULL){
293         WARNING("snort plugin: `Type' must be defined in `types.db'.");
294         snort_metric_definition_destroy(md);
295         return (-1);
296     } else {
297         md->data_source_type = ds->ds->type;
298     }
299
300     DEBUG("snort plugin: md = { name = %s, type = %s, data_source_type = %d, index = %d }",
301         md->name, md->type, md->data_source_type, md->index);
302
303     if (metric_head == NULL)
304         metric_head = md;
305     else {
306         metric_definition_t *last;
307         last = metric_head;
308         while (last->next != NULL)
309             last = last->next;
310         last->next = md;
311     }
312
313     return (0);
314 }
315
316 static void snort_instance_definition_destroy(void *arg){
317     instance_definition_t *id;
318
319     id = arg;
320     if (id == NULL)
321         return;
322
323     if (id->name != NULL)
324         DEBUG("snort plugin: Destroying instance definition `%s'.", id->name);
325
326     sfree(id->name);
327     sfree(id->path);
328     sfree(id->metric_list);
329     sfree(id);
330 }
331
332 static int snort_config_add_instance_collect(instance_definition_t *id, oconfig_item_t *ci){
333     metric_definition_t *metric;
334     int i;
335
336     if (ci->values_num < 1){
337         WARNING("snort plugin: The `Collect' config option needs at least one argument.");
338         return (-1);
339     }
340
341     /* Verify string arguments */
342     for (i = 0; i < ci->values_num; ++i)
343         if (ci->values[i].type != OCONFIG_TYPE_STRING){
344             WARNING("snort plugin: All arguments to `Collect' must be strings.");
345             return (-1);
346         }
347
348     id->metric_list = (metric_definition_t **)malloc(sizeof(metric_definition_t *) * ci->values_num);
349     if (id->metric_list == NULL)
350         return (-1);
351
352     for (i = 0; i < ci->values_num; ++i){
353         for (metric = metric_head; metric != NULL; metric = metric->next)
354             if (strcasecmp(ci->values[i].value.string, metric->name) == 0)
355                 break;
356
357         if (metric == NULL){
358             WARNING("snort plugin: `Collect' argument not found `%s'.", ci->values[i].value.string);
359             return (-1);
360         }
361
362         DEBUG("snort plugin: id { name=%s md->name=%s }", id->name, metric->name);
363
364         id->metric_list[i] = metric;
365         id->metric_list_len++;
366     }
367
368     return (0);
369 }
370
371 /* Parse instance  */
372 static int snort_config_add_instance(oconfig_item_t *ci){
373
374     instance_definition_t* id;
375     int status = 0;
376     int i;
377
378     /* Registration variables */
379     char cb_name[DATA_MAX_NAME_LEN];
380     user_data_t cb_data;
381     struct timespec cb_interval;
382
383     if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)){
384         WARNING("snort plugin: The `Instance' config option needs exactly one string argument.");
385         return (-1);
386     }
387
388     id = (instance_definition_t *)malloc(sizeof(*id));
389     if (id == NULL)
390         return (-1);
391     memset(id, 0, sizeof(*id));
392
393     id->name = strdup(ci->values[0].value.string);
394     if (id->name == NULL){
395         free(id);
396         return (-1);
397     }
398
399     /* Use default interval. */
400     id->interval = plugin_get_interval();
401
402     for (i = 0; i < ci->children_num; ++i){
403         oconfig_item_t *option = ci->children + i;
404         status = 0;
405
406         if (strcasecmp("Path", option->key) == 0)
407             status = cf_util_get_string(option, &id->path);
408         else if (strcasecmp("Collect", option->key) == 0)
409             status = snort_config_add_instance_collect(id, option);
410         else if (strcasecmp("Interval", option->key) == 0)
411             cf_util_get_cdtime(option, &id->interval);
412         else {
413             WARNING("snort plugin: Option `%s' not allowed here.", option->key);
414             status = -1;
415         }
416
417         if (status != 0)
418             break;
419     }
420
421     if (status != 0){
422         snort_instance_definition_destroy(id);
423         return (-1);
424     }
425
426     /* Verify all necessary options have been set. */
427     if (id->path == NULL){
428         WARNING("snort plugin: Option `Path' must be set.");
429         status = -1;
430     } else if (id->metric_list == NULL){
431         WARNING("snort plugin: Option `Collect' must be set.");
432         status = -1;
433    }
434
435     if (status != 0){
436         snort_instance_definition_destroy(id);
437         return (-1);
438     }
439
440     DEBUG("snort plugin: id = { name = %s, path = %s }", id->name, id->path);
441
442     ssnprintf (cb_name, sizeof (cb_name), "snort-%s", id->name);
443     memset(&cb_data, 0, sizeof(cb_data));
444     cb_data.data = id;
445     cb_data.free_func = snort_instance_definition_destroy;
446     CDTIME_T_TO_TIMESPEC(id->interval, &cb_interval);
447     status = plugin_register_complex_read(NULL, cb_name, snort_read, &cb_interval, &cb_data);
448
449     if (status != 0){
450         ERROR("snort plugin: Registering complex read function failed.");
451         snort_instance_definition_destroy(id);
452         return (-1);
453     }
454
455     return (0);
456 }
457
458 /* Parse blocks */
459 static int snort_config(oconfig_item_t *ci){
460     int i;
461     for (i = 0; i < ci->children_num; ++i){
462         oconfig_item_t *child = ci->children + i;
463         if (strcasecmp("Metric", child->key) == 0)
464             snort_config_add_metric(child);
465         else if (strcasecmp("Instance", child->key) == 0)
466             snort_config_add_instance(child);
467         else
468             WARNING("snort plugin: Ignore unknown config option `%s'.", child->key);
469     }
470
471     return (0);
472 } /* int snort_config */
473
474 static int snort_shutdown(void){
475     metric_definition_t *metric_this;
476     metric_definition_t *metric_next;
477
478     metric_this = metric_head;
479     metric_head = NULL;
480
481     while (metric_this != NULL){
482         metric_next = metric_this->next;
483         snort_metric_definition_destroy(metric_this);
484         metric_this = metric_next;
485     }
486
487     return (0);
488 }
489
490 void module_register(void){
491     plugin_register_complex_config("snort", snort_config);
492     plugin_register_shutdown("snort", snort_shutdown);
493 }
494