File: filter-re.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (249 lines) | stat: -rw-r--r-- 6,991 bytes parent folder | download | duplicates (2)
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * 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 "filter-re.h"
#include "str-utils.h"
#include "messages.h"
#include "scratch-buffers.h"
#include <string.h>

typedef struct _FilterRE
{
  FilterExprNode super;
  NVHandle value_handle;
  LogMatcherOptions matcher_options;
  LogMatcher *matcher;
} FilterRE;

static gboolean
filter_re_eval(FilterExprNode *s, LogMessage **msgs, gint num_msg, LogTemplateEvalOptions *options)
{
  FilterRE *self = (FilterRE *) s;
  LogMessage *msg = msgs[num_msg - 1];
  gboolean result;

  msg_trace("match() evaluation started against a name-value pair",
            evt_tag_msg_value_name("name", self->value_handle),
            evt_tag_msg_value("value", msg, self->value_handle),
            evt_tag_str("pattern", self->matcher->pattern),
            evt_tag_msg_reference(msg));
  result = log_matcher_match_value(self->matcher, msg, self->value_handle);
  return result ^ s->comp;
}

static void
filter_re_free(FilterExprNode *s)
{
  FilterRE *self = (FilterRE *) s;

  log_matcher_unref(self->matcher);
  log_matcher_options_destroy(&self->matcher_options);
}

static gboolean
filter_re_init(FilterExprNode *s, GlobalConfig *cfg)
{
  FilterRE *self = (FilterRE *) s;

  if (self->matcher_options.flags & LMF_STORE_MATCHES)
    self->super.modify = TRUE;

  return TRUE;
}

LogMatcherOptions *
filter_re_get_matcher_options(FilterExprNode *s)
{
  FilterRE *self = (FilterRE *) s;

  return &self->matcher_options;
}

gboolean
filter_re_compile_pattern(FilterExprNode *s, const gchar *re, GError **error)
{
  FilterRE *self = (FilterRE *) s;

  log_matcher_options_init(&self->matcher_options);
  self->matcher = log_matcher_new(&self->matcher_options);
  return log_matcher_compile(self->matcher, re, error);
}

static void
filter_re_init_instance(FilterRE *self, NVHandle value_handle)
{
  filter_expr_node_init_instance(&self->super);
  self->value_handle = value_handle;
  self->super.init = filter_re_init;
  self->super.eval = filter_re_eval;
  self->super.free_fn = filter_re_free;
  self->super.type = "regexp";
  log_matcher_options_defaults(&self->matcher_options);
  self->matcher_options.flags |= LMF_MATCH_ONLY;
}

FilterExprNode *
filter_re_new(NVHandle value_handle)
{
  FilterRE *self = g_new0(FilterRE, 1);

  filter_re_init_instance(self, value_handle);
  return &self->super;
}

FilterExprNode *
filter_source_new(void)
{
  FilterRE *self = (FilterRE *) filter_re_new(LM_V_SOURCE);

  if (!log_matcher_options_set_type(&self->matcher_options, "string"))
    {
      /* this can only happen if the plain text string matcher will cease to exist */
      g_assert_not_reached();
    }
  return &self->super;
}

typedef struct _FilterMatch
{
  FilterRE super;
  LogTemplate *template;
} FilterMatch;

gboolean
filter_match_is_usage_obsolete(FilterExprNode *s)
{
  FilterMatch *self = (FilterMatch *) s;

  return self->super.value_handle == 0 && self->template == NULL;
}

void
filter_match_set_value_handle(FilterExprNode *s, NVHandle value_handle)
{
  FilterMatch *self = (FilterMatch *) s;

  self->super.value_handle = value_handle;
}

void
filter_match_set_template_ref(FilterExprNode *s, LogTemplate *template)
{
  FilterMatch *self = (FilterMatch *) s;

  log_template_unref(self->template);
  self->template = template;
}

static gboolean
filter_match_eval_against_program_pid_msg(FilterExprNode *s, LogMessage **msgs, gint num_msg,
                                          LogTemplateEvalOptions *options)
{
  FilterMatch *self = (FilterMatch *) s;

  const gchar *pid;
  gssize pid_len;
  gchar *str;
  gboolean result;
  LogMessage *msg = msgs[num_msg - 1];

  pid = log_msg_get_value(msg, LM_V_PID, &pid_len);

  /* compatibility mode */
  str = g_strdup_printf("%s%s%s%s: %s",
                        log_msg_get_value(msg, LM_V_PROGRAM, NULL),
                        pid_len > 0 ? "[" : "",
                        pid,
                        pid_len > 0 ? "]" : "",
                        log_msg_get_value(msg, LM_V_MESSAGE, NULL));

  msg_trace("match() evaluation started against constructed $PROGRAM[$PID]: $MESSAGE string for compatibility",
            evt_tag_printf("input", "%s", str),
            evt_tag_str("pattern", self->super.matcher->pattern),
            evt_tag_msg_reference(msg));

  result = log_matcher_match_buffer(self->super.matcher, msg, str, -1);

  g_free(str);
  return result ^ s->comp;
}

static gboolean
filter_match_eval_against_template(FilterExprNode *s, LogMessage **msgs, gint num_msg, LogTemplateEvalOptions *options)
{
  FilterMatch *self = (FilterMatch *) s;
  LogMessage *msg = msgs[num_msg - 1];

  msg_trace("match() evaluation started against template",
            evt_tag_template("input", self->template, msg, options),
            evt_tag_str("pattern", self->super.matcher->pattern),
            evt_tag_str("template", self->template->template_str),
            evt_tag_msg_reference(msg));
  gboolean result = log_matcher_match_template(self->super.matcher, msg, self->template, options);
  return result ^ s->comp;
}

static void
filter_match_determine_eval_function(FilterMatch *self)
{
  if (self->super.value_handle)
    self->super.super.eval = filter_re_eval;
  else if (self->template)
    self->super.super.eval = filter_match_eval_against_template;
  else
    self->super.super.eval = filter_match_eval_against_program_pid_msg;
}

static gboolean
filter_match_init(FilterExprNode *s, GlobalConfig *cfg)
{
  FilterMatch *self = (FilterMatch *) s;

  if (!filter_re_init(s, cfg))
    return FALSE;

  filter_match_determine_eval_function(self);

  return TRUE;
}

static void
filter_match_free(FilterExprNode *s)
{
  FilterMatch *self = (FilterMatch *) s;

  log_template_unref(self->template);
  filter_re_free(&self->super.super);
}

FilterExprNode *
filter_match_new(void)
{
  FilterMatch *self = g_new0(FilterMatch, 1);

  filter_re_init_instance(&self->super, 0);
  self->super.super.init = filter_match_init;
  self->super.super.free_fn = filter_match_free;
  return &self->super.super;
}