1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* Copyright (c) 2002-2013 Balabit
* Copyright (c) 1998-2013 Balázs Scheidler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "rewrite-subst.h"
/* LogRewriteSubst
*
* This class implements the "subst" expression in a rewrite rule.
*/
typedef struct _LogRewriteSubst LogRewriteSubst;
struct _LogRewriteSubst
{
LogRewrite super;
LogMatcherOptions matcher_options;
LogMatcher *matcher;
LogTemplate *replacement;
};
LogMatcherOptions *
log_rewrite_subst_get_matcher_options(LogRewrite *s)
{
LogRewriteSubst *self = (LogRewriteSubst *) s;
return &self->matcher_options;
}
void
log_rewrite_subst_process(LogRewrite *s, LogMessage **pmsg, const LogPathOptions *path_options)
{
LogRewriteSubst *self = (LogRewriteSubst *) s;
LogMessage *msg;
NVTable *nvtable;
const gchar *value;
gchar *new_value;
gssize length;
gssize new_length = -1;
msg = log_msg_make_writable(pmsg, path_options);
nvtable = nv_table_ref(msg->payload);
value = log_msg_get_value(msg, self->super.value_handle, &length);
new_value = log_matcher_replace(self->matcher, msg, self->super.value_handle, value, length, self->replacement,
&new_length);
if (new_value)
{
msg_trace("Performing subst() rewrite",
evt_tag_str("rule", s->name),
evt_tag_str("value", log_msg_get_value_name(s->value_handle, NULL)),
evt_tag_mem("input", value, length),
evt_tag_str("type", self->matcher_options.type),
evt_tag_str("pattern", self->matcher->pattern),
evt_tag_str("replacement", self->replacement->template_str),
log_pipe_location_tag(&s->super));
log_msg_set_value(msg, self->super.value_handle, new_value, new_length);
}
else
{
msg_trace("Performing subst() rewrite failed, pattern did not match",
evt_tag_str("rule", s->name),
evt_tag_str("value", log_msg_get_value_name(s->value_handle, NULL)),
evt_tag_mem("input", value, length),
evt_tag_str("type", self->matcher_options.type),
evt_tag_str("pattern", self->matcher->pattern),
evt_tag_str("replacement", self->replacement->template_str),
log_pipe_location_tag(&s->super));
}
nv_table_unref(nvtable);
g_free(new_value);
}
gboolean
log_rewrite_subst_compile_pattern(LogRewrite *s, const gchar *regexp, GError **error)
{
LogRewriteSubst *self = (LogRewriteSubst *) s;
log_matcher_options_init(&self->matcher_options);
self->matcher = log_matcher_new(&self->matcher_options);
if (!log_matcher_is_replace_supported(self->matcher))
{
g_set_error(error, LOG_MATCHER_ERROR, 0,
"subst() only supports matchers that allow replacement, glob is not one of these");
return FALSE;
}
return log_matcher_compile(self->matcher, regexp, error);
}
static LogPipe *
log_rewrite_subst_clone(LogPipe *s)
{
LogRewriteSubst *self = (LogRewriteSubst *) s;
LogRewriteSubst *cloned;
cloned = (LogRewriteSubst *) log_rewrite_subst_new(self->replacement, s->cfg);
log_rewrite_clone_method(&cloned->super, &self->super);
cloned->matcher = log_matcher_ref(self->matcher);
return &cloned->super.super;
}
void
log_rewrite_subst_free(LogPipe *s)
{
LogRewriteSubst *self = (LogRewriteSubst *) s;
log_matcher_unref(self->matcher);
log_template_unref(self->replacement);
log_matcher_options_destroy(&self->matcher_options);
log_rewrite_free_method(s);
}
LogRewrite *
log_rewrite_subst_new(LogTemplate *replacement, GlobalConfig *cfg)
{
LogRewriteSubst *self = g_new0(LogRewriteSubst, 1);
log_rewrite_init_instance(&self->super, cfg);
self->super.super.free_fn = log_rewrite_subst_free;
self->super.super.clone = log_rewrite_subst_clone;
self->super.process = log_rewrite_subst_process;
self->replacement = log_template_ref(replacement);
log_matcher_options_defaults(&self->matcher_options);
return &self->super;
}
|