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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
* Copyright (c) 2021 One Identity
* Copyright (c) 2021 Xiaoyu Qiu
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation, or (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; 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 "regexp-parser.h"
#include "parser/parser-expr.h"
#include "scratch-buffers.h"
#include "string-list.h"
#include <string.h>
typedef struct _RegexpParser
{
LogParser super;
gchar *prefix;
GList *patterns;
LogMatcherOptions matcher_options;
GList *matchers;
} RegexpParser;
LogMatcherOptions *
regexp_parser_get_matcher_options(LogParser *s)
{
RegexpParser *self = (RegexpParser *) s;
return &self->matcher_options;
}
void
regexp_parser_set_prefix(LogParser *s, const gchar *prefix)
{
RegexpParser *self = (RegexpParser *) s;
g_free(self->prefix);
if (prefix)
self->prefix = g_strdup(prefix);
else
self->prefix = NULL;
}
void
regexp_parser_set_patterns(LogParser *s, GList *patterns)
{
RegexpParser *self = (RegexpParser *) s;
string_list_free(self->patterns);
self->patterns = patterns;
}
gboolean
regexp_parser_compile(LogParser *s, GError **error)
{
RegexpParser *self = (RegexpParser *) s;
log_matcher_options_init(&self->matcher_options);
gboolean result = TRUE;
for (GList *item = self->patterns; item; item = item->next)
{
self->matchers = g_list_prepend(self->matchers, log_matcher_new(&self->matcher_options));
log_matcher_pcre_set_nv_prefix((LogMatcher *)self->matchers->data, self->prefix);
if(!log_matcher_compile((LogMatcher *)self->matchers->data, item->data, error))
{
result = FALSE;
break;
}
}
if (result)
self->matchers = g_list_reverse(self->matchers);
else
g_list_free_full(self->matchers, (GDestroyNotify) log_matcher_unref);
return result;
}
static gboolean
regexp_parser_process(LogParser *s, LogMessage **pmsg, const LogPathOptions *path_options, const gchar *input,
gsize input_len)
{
RegexpParser *self = (RegexpParser *) s;
log_msg_make_writable(pmsg, path_options);
msg_trace("regexp-parser message processing started",
evt_tag_str("input", input),
evt_tag_str("prefix", self->prefix),
evt_tag_msg_reference(*pmsg));
gboolean result = FALSE;
for (GList *item = self->matchers; item; item = item->next)
{
msg_trace("regexp-parser message processing for",
evt_tag_str("input", input),
evt_tag_str("pattern", ((LogMatcher *)item->data)->pattern));
gint value_handle = LM_V_MESSAGE;
if (G_UNLIKELY(self->super.template_obj))
value_handle = LM_V_NONE;
if (log_matcher_match((LogMatcher *)item->data, *pmsg, value_handle, input, input_len))
{
result = TRUE;
break;
}
}
return result;
}
static void
regexp_parser_free(LogPipe *s)
{
RegexpParser *self = (RegexpParser *) s;
g_list_free_full(self->matchers, (GDestroyNotify) log_matcher_unref);
log_matcher_options_destroy(&self->matcher_options);
g_free(self->prefix);
string_list_free(self->patterns);
log_parser_free_method(s);
}
static LogPipe *
regexp_parser_clone(LogPipe *s)
{
RegexpParser *self = (RegexpParser *)s;
RegexpParser *cloned;
cloned = (RegexpParser *)regexp_parser_new(s->cfg);
log_parser_clone_settings(&self->super, &cloned->super);
regexp_parser_set_prefix(&cloned->super, self->prefix);
regexp_parser_set_patterns(&cloned->super, string_list_clone(self->patterns));
for (GList *item = self->matchers; item; item = item->next)
cloned->matchers = g_list_append(cloned->matchers, log_matcher_ref((LogMatcher *)item->data));
return &cloned->super.super;
}
LogParser *
regexp_parser_new(GlobalConfig *cfg)
{
RegexpParser *self = g_new0(RegexpParser, 1);
log_parser_init_instance(&self->super, cfg);
self->super.super.free_fn = regexp_parser_free;
self->super.super.clone = regexp_parser_clone;
self->super.process = regexp_parser_process;
log_matcher_options_defaults(&self->matcher_options);
self->matcher_options.flags |= LMF_STORE_MATCHES;
self->patterns = NULL;
return &self->super;
}
|