1 /**
2  * collectd - src/target_replace.c
3  * Copyright (C) 2008       Florian Forster
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *   Florian Forster <octo at collectd.org>
25  **/
26 
27 #include "collectd.h"
28 
29 #include "filter_chain.h"
30 #include "utils/common/common.h"
31 #include "utils_subst.h"
32 
33 #include <regex.h>
34 
35 struct tr_action_s;
36 typedef struct tr_action_s tr_action_t;
37 struct tr_action_s {
38   regex_t re;
39   char *replacement;
40   bool may_be_empty;
41 
42   tr_action_t *next;
43 };
44 
45 struct tr_meta_data_action_s;
46 typedef struct tr_meta_data_action_s tr_meta_data_action_t;
47 struct tr_meta_data_action_s {
48   char *key;
49   regex_t re;
50   char *replacement;
51 
52   tr_meta_data_action_t *next;
53 };
54 
55 struct tr_data_s {
56   tr_action_t *host;
57   tr_action_t *plugin;
58   tr_action_t *plugin_instance;
59   /* tr_action_t *type; */
60   tr_action_t *type_instance;
61   tr_meta_data_action_t *meta;
62 };
63 typedef struct tr_data_s tr_data_t;
64 
tr_strdup(const char * orig)65 static char *tr_strdup(const char *orig) /* {{{ */
66 {
67   size_t sz;
68   char *dest;
69 
70   if (orig == NULL)
71     return NULL;
72 
73   sz = strlen(orig) + 1;
74   dest = malloc(sz);
75   if (dest == NULL)
76     return NULL;
77 
78   memcpy(dest, orig, sz);
79 
80   return dest;
81 } /* }}} char *tr_strdup */
82 
tr_action_destroy(tr_action_t * act)83 static void tr_action_destroy(tr_action_t *act) /* {{{ */
84 {
85   if (act == NULL)
86     return;
87 
88   regfree(&act->re);
89   sfree(act->replacement);
90 
91   if (act->next != NULL)
92     tr_action_destroy(act->next);
93 
94   sfree(act);
95 } /* }}} void tr_action_destroy */
96 
tr_meta_data_action_destroy(tr_meta_data_action_t * act)97 static void tr_meta_data_action_destroy(tr_meta_data_action_t *act) /* {{{ */
98 {
99   if (act == NULL)
100     return;
101 
102   sfree(act->key);
103   regfree(&act->re);
104   sfree(act->replacement);
105 
106   if (act->next != NULL)
107     tr_meta_data_action_destroy(act->next);
108 
109   sfree(act);
110 } /* }}} void tr_meta_data_action_destroy */
111 
tr_config_add_action(tr_action_t ** dest,const oconfig_item_t * ci,bool may_be_empty)112 static int tr_config_add_action(tr_action_t **dest, /* {{{ */
113                                 const oconfig_item_t *ci, bool may_be_empty) {
114   tr_action_t *act;
115   int status;
116 
117   if (dest == NULL)
118     return -EINVAL;
119 
120   if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
121       (ci->values[1].type != OCONFIG_TYPE_STRING)) {
122     ERROR("Target `replace': The `%s' option requires exactly two string "
123           "arguments.",
124           ci->key);
125     return -1;
126   }
127 
128   act = calloc(1, sizeof(*act));
129   if (act == NULL) {
130     ERROR("tr_config_add_action: calloc failed.");
131     return -ENOMEM;
132   }
133 
134   act->replacement = NULL;
135   act->may_be_empty = may_be_empty;
136 
137   status = regcomp(&act->re, ci->values[0].value.string, REG_EXTENDED);
138   if (status != 0) {
139     char errbuf[1024] = "";
140 
141     /* regerror assures null termination. */
142     regerror(status, &act->re, errbuf, sizeof(errbuf));
143     ERROR("Target `replace': Compiling the regular expression `%s' "
144           "failed: %s.",
145           ci->values[0].value.string, errbuf);
146     sfree(act);
147     return -EINVAL;
148   }
149 
150   act->replacement = tr_strdup(ci->values[1].value.string);
151   if (act->replacement == NULL) {
152     ERROR("tr_config_add_action: tr_strdup failed.");
153     tr_action_destroy(act);
154     return -ENOMEM;
155   }
156 
157   /* Insert action at end of list. */
158   if (*dest == NULL)
159     *dest = act;
160   else {
161     tr_action_t *prev;
162 
163     prev = *dest;
164     while (prev->next != NULL)
165       prev = prev->next;
166 
167     prev->next = act;
168   }
169 
170   return 0;
171 } /* }}} int tr_config_add_action */
172 
tr_config_add_meta_action(tr_meta_data_action_t ** dest,const oconfig_item_t * ci,bool should_delete)173 static int tr_config_add_meta_action(tr_meta_data_action_t **dest, /* {{{ */
174                                      const oconfig_item_t *ci,
175                                      bool should_delete) {
176   tr_meta_data_action_t *act;
177   int status;
178 
179   if (dest == NULL)
180     return -EINVAL;
181 
182   if (should_delete) {
183     if ((ci->values_num != 2) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
184         (ci->values[1].type != OCONFIG_TYPE_STRING)) {
185       ERROR("Target `replace': The `%s' option requires exactly two string "
186             "arguments.",
187             ci->key);
188       return -1;
189     }
190   } else {
191     if ((ci->values_num != 3) || (ci->values[0].type != OCONFIG_TYPE_STRING) ||
192         (ci->values[1].type != OCONFIG_TYPE_STRING) ||
193         (ci->values[2].type != OCONFIG_TYPE_STRING)) {
194       ERROR("Target `replace': The `%s' option requires exactly three string "
195             "arguments.",
196             ci->key);
197       return -1;
198     }
199   }
200 
201   if (strlen(ci->values[0].value.string) == 0) {
202     ERROR("Target `replace': The `%s' option does not accept empty string as "
203           "first argument.",
204           ci->key);
205     return -1;
206   }
207 
208   act = calloc(1, sizeof(*act));
209   if (act == NULL) {
210     ERROR("tr_config_add_meta_action: calloc failed.");
211     return -ENOMEM;
212   }
213 
214   act->key = NULL;
215   act->replacement = NULL;
216 
217   status = regcomp(&act->re, ci->values[1].value.string, REG_EXTENDED);
218   if (status != 0) {
219     char errbuf[1024] = "";
220 
221     /* regerror assures null termination. */
222     regerror(status, &act->re, errbuf, sizeof(errbuf));
223     ERROR("Target `replace': Compiling the regular expression `%s' "
224           "failed: %s.",
225           ci->values[1].value.string, errbuf);
226     sfree(act->key);
227     sfree(act);
228     return -EINVAL;
229   }
230 
231   act->key = tr_strdup(ci->values[0].value.string);
232   if (act->key == NULL) {
233     ERROR("tr_config_add_meta_action: tr_strdup failed.");
234     tr_meta_data_action_destroy(act);
235     return -ENOMEM;
236   }
237 
238   if (!should_delete) {
239     act->replacement = tr_strdup(ci->values[2].value.string);
240     if (act->replacement == NULL) {
241       ERROR("tr_config_add_meta_action: tr_strdup failed.");
242       tr_meta_data_action_destroy(act);
243       return -ENOMEM;
244     }
245   }
246 
247   /* Insert action at end of list. */
248   if (*dest == NULL)
249     *dest = act;
250   else {
251     tr_meta_data_action_t *prev;
252 
253     prev = *dest;
254     while (prev->next != NULL)
255       prev = prev->next;
256 
257     prev->next = act;
258   }
259 
260   return 0;
261 } /* }}} int tr_config_add_meta_action */
262 
tr_action_invoke(tr_action_t * act_head,char * buffer_in,size_t buffer_in_size,bool may_be_empty)263 static int tr_action_invoke(tr_action_t *act_head, /* {{{ */
264                             char *buffer_in, size_t buffer_in_size,
265                             bool may_be_empty) {
266   int status;
267   char buffer[DATA_MAX_NAME_LEN];
268   regmatch_t matches[8] = {[0] = {0}};
269 
270   if (act_head == NULL)
271     return -EINVAL;
272 
273   sstrncpy(buffer, buffer_in, sizeof(buffer));
274 
275   DEBUG("target_replace plugin: tr_action_invoke: <- buffer = %s;", buffer);
276 
277   for (tr_action_t *act = act_head; act != NULL; act = act->next) {
278     char temp[DATA_MAX_NAME_LEN];
279     char *subst_status;
280 
281     status = regexec(&act->re, buffer, STATIC_ARRAY_SIZE(matches), matches,
282                      /* flags = */ 0);
283     if (status == REG_NOMATCH)
284       continue;
285     else if (status != 0) {
286       char errbuf[1024] = "";
287 
288       regerror(status, &act->re, errbuf, sizeof(errbuf));
289       ERROR("Target `replace': Executing a regular expression failed: %s.",
290             errbuf);
291       continue;
292     }
293 
294     subst_status = subst(temp, sizeof(temp), buffer, (size_t)matches[0].rm_so,
295                          (size_t)matches[0].rm_eo, act->replacement);
296     if (subst_status == NULL) {
297       ERROR("Target `replace': subst (buffer = %s, start = %" PRIsz
298             ", end = %" PRIsz ", "
299             "replacement = %s) failed.",
300             buffer, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo,
301             act->replacement);
302       continue;
303     }
304     sstrncpy(buffer, temp, sizeof(buffer));
305 
306     DEBUG("target_replace plugin: tr_action_invoke: -- buffer = %s;", buffer);
307   } /* for (act = act_head; act != NULL; act = act->next) */
308 
309   if ((may_be_empty == false) && (buffer[0] == 0)) {
310     WARNING("Target `replace': Replacement resulted in an empty string, "
311             "which is not allowed for this buffer (`host' or `plugin').");
312     return 0;
313   }
314 
315   DEBUG("target_replace plugin: tr_action_invoke: -> buffer = %s;", buffer);
316   sstrncpy(buffer_in, buffer, buffer_in_size);
317 
318   return 0;
319 } /* }}} int tr_action_invoke */
320 
tr_meta_data_action_invoke(tr_meta_data_action_t * act_head,meta_data_t ** dest)321 static int tr_meta_data_action_invoke(/* {{{ */
322                                       tr_meta_data_action_t *act_head,
323                                       meta_data_t **dest) {
324   int status;
325   regmatch_t matches[8] = {[0] = {0}};
326 
327   if (act_head == NULL)
328     return -EINVAL;
329 
330   if ((*dest) == NULL) /* nothing to do */
331     return 0;
332 
333   for (tr_meta_data_action_t *act = act_head; act != NULL; act = act->next) {
334     char temp[DATA_MAX_NAME_LEN];
335     char *subst_status;
336     int value_type;
337     int meta_data_status;
338     char *value;
339     meta_data_t *result;
340 
341     value_type = meta_data_type(*dest, act->key);
342     if (value_type == 0) /* not found */
343       continue;
344     if (value_type != MD_TYPE_STRING) {
345       WARNING("Target `replace': Attempting replace on metadata key `%s', "
346               "which isn't a string.",
347               act->key);
348       continue;
349     }
350 
351     meta_data_status = meta_data_get_string(*dest, act->key, &value);
352     if (meta_data_status != 0) {
353       ERROR("Target `replace': Unable to retrieve metadata value for `%s'.",
354             act->key);
355       return meta_data_status;
356     }
357 
358     DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
359           "old value = `%s'",
360           act->key, value);
361 
362     status = regexec(&act->re, value, STATIC_ARRAY_SIZE(matches), matches,
363                      /* flags = */ 0);
364     if (status == REG_NOMATCH) {
365       sfree(value);
366       continue;
367     } else if (status != 0) {
368       char errbuf[1024] = "";
369 
370       regerror(status, &act->re, errbuf, sizeof(errbuf));
371       ERROR("Target `replace': Executing a regular expression failed: %s.",
372             errbuf);
373       sfree(value);
374       continue;
375     }
376 
377     if (act->replacement == NULL) {
378       /* no replacement; delete the key */
379       DEBUG("target_replace plugin: tr_meta_data_action_invoke: "
380             "deleting `%s'",
381             act->key);
382       meta_data_delete(*dest, act->key);
383       sfree(value);
384       continue;
385     }
386 
387     subst_status = subst(temp, sizeof(temp), value, (size_t)matches[0].rm_so,
388                          (size_t)matches[0].rm_eo, act->replacement);
389     if (subst_status == NULL) {
390       ERROR("Target `replace': subst (value = %s, start = %" PRIsz
391             ", end = %" PRIsz ", "
392             "replacement = %s) failed.",
393             value, (size_t)matches[0].rm_so, (size_t)matches[0].rm_eo,
394             act->replacement);
395       sfree(value);
396       continue;
397     }
398 
399     DEBUG("target_replace plugin: tr_meta_data_action_invoke: `%s' "
400           "value `%s' -> `%s'",
401           act->key, value, temp);
402 
403     if ((result = meta_data_create()) == NULL) {
404       ERROR("Target `replace': failed to create metadata for `%s'.", act->key);
405       sfree(value);
406       return -ENOMEM;
407     }
408 
409     meta_data_status = meta_data_add_string(result, act->key, temp);
410     if (meta_data_status != 0) {
411       ERROR("Target `replace': Unable to set metadata value for `%s'.",
412             act->key);
413       meta_data_destroy(result);
414       sfree(value);
415       return meta_data_status;
416     }
417 
418     meta_data_clone_merge(dest, result);
419     meta_data_destroy(result);
420     sfree(value);
421   } /* for (act = act_head; act != NULL; act = act->next) */
422 
423   return 0;
424 } /* }}} int tr_meta_data_action_invoke */
425 
tr_destroy(void ** user_data)426 static int tr_destroy(void **user_data) /* {{{ */
427 {
428   tr_data_t *data;
429 
430   if (user_data == NULL)
431     return -EINVAL;
432 
433   data = *user_data;
434   if (data == NULL)
435     return 0;
436 
437   tr_action_destroy(data->host);
438   tr_action_destroy(data->plugin);
439   tr_action_destroy(data->plugin_instance);
440   /* tr_action_destroy (data->type); */
441   tr_action_destroy(data->type_instance);
442   tr_meta_data_action_destroy(data->meta);
443   sfree(data);
444 
445   return 0;
446 } /* }}} int tr_destroy */
447 
tr_create(const oconfig_item_t * ci,void ** user_data)448 static int tr_create(const oconfig_item_t *ci, void **user_data) /* {{{ */
449 {
450   tr_data_t *data;
451   int status;
452 
453   data = calloc(1, sizeof(*data));
454   if (data == NULL) {
455     ERROR("tr_create: calloc failed.");
456     return -ENOMEM;
457   }
458 
459   data->host = NULL;
460   data->plugin = NULL;
461   data->plugin_instance = NULL;
462   /* data->type = NULL; */
463   data->type_instance = NULL;
464   data->meta = NULL;
465 
466   status = 0;
467   for (int i = 0; i < ci->children_num; i++) {
468     oconfig_item_t *child = ci->children + i;
469 
470     if ((strcasecmp("Host", child->key) == 0) ||
471         (strcasecmp("Hostname", child->key) == 0))
472       status = tr_config_add_action(&data->host, child,
473                                     /* may be empty = */ false);
474     else if (strcasecmp("Plugin", child->key) == 0)
475       status = tr_config_add_action(&data->plugin, child,
476                                     /* may be empty = */ false);
477     else if (strcasecmp("PluginInstance", child->key) == 0)
478       status = tr_config_add_action(&data->plugin_instance, child,
479                                     /* may be empty = */ true);
480 #if 0
481     else if (strcasecmp ("Type", child->key) == 0)
482       status = tr_config_add_action (&data->type, child,
483           /* may be empty = */ 0);
484 #endif
485     else if (strcasecmp("TypeInstance", child->key) == 0)
486       status = tr_config_add_action(&data->type_instance, child,
487                                     /* may be empty = */ true);
488     else if (strcasecmp("MetaData", child->key) == 0)
489       status = tr_config_add_meta_action(&data->meta, child,
490                                          /* should delete = */ false);
491     else if (strcasecmp("DeleteMetaData", child->key) == 0)
492       status = tr_config_add_meta_action(&data->meta, child,
493                                          /* should delete = */ true);
494     else {
495       ERROR("Target `replace': The `%s' configuration option is not understood "
496             "and will be ignored.",
497             child->key);
498       status = 0;
499     }
500 
501     if (status != 0)
502       break;
503   }
504 
505   /* Additional sanity-checking */
506   while (status == 0) {
507     if ((data->host == NULL) && (data->plugin == NULL) &&
508         (data->plugin_instance == NULL)
509         /* && (data->type == NULL) */
510         && (data->type_instance == NULL) && (data->meta == NULL)) {
511       ERROR("Target `replace': You need to set at least one of `Host', "
512             "`Plugin', `PluginInstance' or `TypeInstance'.");
513       status = -1;
514     }
515 
516     break;
517   }
518 
519   if (status != 0) {
520     tr_destroy((void *)&data);
521     return status;
522   }
523 
524   *user_data = data;
525   return 0;
526 } /* }}} int tr_create */
527 
tr_invoke(const data_set_t * ds,value_list_t * vl,notification_meta_t ** meta,void ** user_data)528 static int tr_invoke(const data_set_t *ds, value_list_t *vl, /* {{{ */
529                      notification_meta_t __attribute__((unused)) * *meta,
530                      void **user_data) {
531   tr_data_t *data;
532 
533   if ((ds == NULL) || (vl == NULL) || (user_data == NULL))
534     return -EINVAL;
535 
536   data = *user_data;
537   if (data == NULL) {
538     ERROR("Target `replace': Invoke: `data' is NULL.");
539     return -EINVAL;
540   }
541 
542   if (data->meta != NULL) {
543     tr_meta_data_action_invoke(data->meta, &(vl->meta));
544   }
545 
546 #define HANDLE_FIELD(f, e)                                                     \
547   if (data->f != NULL)                                                         \
548   tr_action_invoke(data->f, vl->f, sizeof(vl->f), e)
549   HANDLE_FIELD(host, false);
550   HANDLE_FIELD(plugin, false);
551   HANDLE_FIELD(plugin_instance, true);
552   /* HANDLE_FIELD (type, false); */
553   HANDLE_FIELD(type_instance, true);
554 
555   return FC_TARGET_CONTINUE;
556 } /* }}} int tr_invoke */
557 
module_register(void)558 void module_register(void) {
559   target_proc_t tproc = {0};
560 
561   tproc.create = tr_create;
562   tproc.destroy = tr_destroy;
563   tproc.invoke = tr_invoke;
564   fc_register_target("replace", tproc);
565 } /* module_register */
566