1 /*
2  * Copyright (c) 2002-2012 Balabit
3  * Copyright (c) 1998-2012 Balázs Scheidler
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * As an additional exemption you are allowed to compile & link against the
20  * OpenSSL libraries as published by the OpenSSL project. See the file
21  * COPYING for details.
22  *
23  */
24 
25 #include "rewrite/rewrite-expr.h"
26 
27 /* rewrite */
28 
29 void
log_rewrite_set_condition(LogRewrite * self,FilterExprNode * condition)30 log_rewrite_set_condition(LogRewrite *self, FilterExprNode *condition)
31 {
32   self->condition = condition;
33 }
34 
35 static void
log_rewrite_queue(LogPipe * s,LogMessage * msg,const LogPathOptions * path_options)36 log_rewrite_queue(LogPipe *s, LogMessage *msg, const LogPathOptions *path_options)
37 {
38   LogRewrite *self = (LogRewrite *) s;
39 
40   msg_trace(">>>>>> rewrite rule evaluation begin",
41             evt_tag_str("rule", self->name),
42             log_pipe_location_tag(s),
43             evt_tag_printf("msg", "%p", msg));
44   if (self->condition && !filter_expr_eval_root(self->condition, &msg, path_options))
45     {
46       msg_trace("Rewrite condition unmatched, skipping rewrite",
47                 evt_tag_str("value", log_msg_get_value_name(self->value_handle, NULL)),
48                 evt_tag_str("rule", self->name),
49                 log_pipe_location_tag(s),
50                 evt_tag_printf("msg", "%p", msg));
51     }
52   else
53     {
54       self->process(self, &msg, path_options);
55     }
56   msg_trace("<<<<<< rewrite rule evaluation finished",
57             evt_tag_str("rule", self->name),
58             log_pipe_location_tag(s),
59             evt_tag_printf("msg", "%p", msg));
60   log_pipe_forward_msg(s, msg, path_options);
61 }
62 
63 gboolean
log_rewrite_init_method(LogPipe * s)64 log_rewrite_init_method(LogPipe *s)
65 {
66   LogRewrite *self = (LogRewrite *) s;
67   GlobalConfig *cfg = log_pipe_get_config(s);
68 
69   if (self->condition)
70     filter_expr_init(self->condition, cfg);
71 
72   if (!self->name)
73     self->name = cfg_tree_get_rule_name(&cfg->tree, ENC_REWRITE, s->expr_node);
74   return TRUE;
75 }
76 
77 void
log_rewrite_free_method(LogPipe * s)78 log_rewrite_free_method(LogPipe *s)
79 {
80   LogRewrite *self = (LogRewrite *) s;
81 
82   if (self->condition)
83     filter_expr_unref(self->condition);
84   g_free(self->name);
85   log_pipe_free_method(s);
86 }
87 
88 void
log_rewrite_init_instance(LogRewrite * self,GlobalConfig * cfg)89 log_rewrite_init_instance(LogRewrite *self, GlobalConfig *cfg)
90 {
91   log_pipe_init_instance(&self->super, cfg);
92   /* indicate that the rewrite rule is changing the message */
93   self->super.free_fn = log_rewrite_free_method;
94   self->super.queue = log_rewrite_queue;
95   self->super.init = log_rewrite_init_method;
96   self->value_handle = LM_V_MESSAGE;
97 }
98