Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new rewrite function: set-severity() #3115

Merged
merged 15 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/cfg-grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -1333,13 +1333,13 @@ msg_format_option
{
if (last_msg_format_options->default_pri == 0xFFFF)
last_msg_format_options->default_pri = LOG_USER;
last_msg_format_options->default_pri = (last_msg_format_options->default_pri & ~7) | $3;
last_msg_format_options->default_pri = (last_msg_format_options->default_pri & ~LOG_PRIMASK) | $3;
}
| KW_DEFAULT_FACILITY '(' facility_string ')'
{
if (last_msg_format_options->default_pri == 0xFFFF)
last_msg_format_options->default_pri = LOG_NOTICE;
last_msg_format_options->default_pri = (last_msg_format_options->default_pri & 7) | $3;
last_msg_format_options->default_pri = (last_msg_format_options->default_pri & LOG_PRIMASK) | $3;
}
;

Expand Down
2 changes: 2 additions & 0 deletions lib/rewrite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(REWRITE_HEADERS
rewrite/rewrite-expr-parser.h
rewrite/rewrite-groupset.h
rewrite/rewrite-unset.h
rewrite/rewrite-set-severity.h
PARENT_SCOPE
)

Expand All @@ -17,6 +18,7 @@ set(REWRITE_SOURCES
rewrite/rewrite-expr-parser.c
rewrite/rewrite-groupset.c
rewrite/rewrite-unset.c
rewrite/rewrite-set-severity.c
PARENT_SCOPE
)

Expand Down
10 changes: 6 additions & 4 deletions lib/rewrite/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ rewriteinclude_HEADERS = \
lib/rewrite/rewrite-set.h \
lib/rewrite/rewrite-unset.h \
lib/rewrite/rewrite-subst.h \
lib/rewrite/rewrite-expr-parser.h \
lib/rewrite/rewrite-groupset.h
lib/rewrite/rewrite-expr-parser.h \
lib/rewrite/rewrite-groupset.h \
lib/rewrite/rewrite-set-severity.h


rewrite_sources = \
Expand All @@ -19,8 +20,9 @@ rewrite_sources = \
lib/rewrite/rewrite-unset.c \
lib/rewrite/rewrite-subst.c \
lib/rewrite/rewrite-expr-parser.c \
lib/rewrite/rewrite-expr-grammar.y \
lib/rewrite/rewrite-groupset.c
lib/rewrite/rewrite-expr-grammar.y \
lib/rewrite/rewrite-groupset.c \
lib/rewrite/rewrite-set-severity.c

BUILT_SOURCES += \
lib/rewrite/rewrite-expr-grammar.y \
Expand Down
3 changes: 3 additions & 0 deletions lib/rewrite/rewrite-expr-grammar.ym
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "rewrite/rewrite-unset.h"
#include "rewrite/rewrite-subst.h"
#include "rewrite/rewrite-groupset.h"
#include "rewrite/rewrite-set-severity.h"
#include "filter/filter-expr.h"
#include "filter/filter-expr-parser.h"
#include "cfg-grammar.h"
Expand Down Expand Up @@ -65,6 +66,7 @@
%token KW_UNSET
%token KW_SUBST
%token KW_VALUES
%token KW_SET_SEVERITY

%%

Expand Down Expand Up @@ -115,6 +117,7 @@ rewrite_expr
{
last_rewrite = log_rewrite_groupunset_new(configuration);
} rewrite_groupset_opts ')' { $$ = last_rewrite; }
| KW_SET_SEVERITY '(' template_content ')' { $$ = log_rewrite_set_severity_new($3, configuration); }
| LL_IDENTIFIER
{
Plugin *p;
Expand Down
1 change: 1 addition & 0 deletions lib/rewrite/rewrite-expr-parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static CfgLexerKeyword rewrite_expr_keywords[] =
{ "subst", KW_SUBST },
{ "set_tag", KW_SET_TAG },
{ "clear_tag", KW_CLEAR_TAG },
{ "set_severity", KW_SET_SEVERITY },
bazsi marked this conversation as resolved.
Show resolved Hide resolved

{ "groupset", KW_GROUP_SET },
{ "groupunset", KW_GROUP_UNSET },
Expand Down
139 changes: 139 additions & 0 deletions lib/rewrite/rewrite-set-severity.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright (c) 2019 Balabit
* Copyright (c) 2019 Kokan <[email protected]>
*
* 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-set-severity.h"
#include "template/templates.h"
#include "syslog-names.h"
#include "scratch-buffers.h"

#include <stdlib.h>

typedef struct _LogRewriteSetSeverity LogRewriteSetSeverity;

struct _LogRewriteSetSeverity
{
LogRewrite super;
LogTemplate *severity;
};

static gint
_convert_severity_as_number(GString *severity_text)
{
char *endptr;
long int severity = strtol(severity_text->str, &endptr, 10);
if (0 == endptr)
smortex marked this conversation as resolved.
Show resolved Hide resolved
return -1;

if (endptr[0] != '\0' || severity_text->str == endptr)
return -1;

if (severity > 7)
return -1;
MrAnno marked this conversation as resolved.
Show resolved Hide resolved

return severity;
}

static gint
_convert_severity_as_text(GString *severity_text)
{
return syslog_name_lookup_level_by_name(severity_text->str);
}

static gint
_convert_severity(GString *severity_text)
{
gint severity = _convert_severity_as_number(severity_text);
smortex marked this conversation as resolved.
Show resolved Hide resolved
if (severity >= 0)
return severity;
smortex marked this conversation as resolved.
Show resolved Hide resolved

severity = _convert_severity_as_text(severity_text);
if (severity >= 0)
return severity;
smortex marked this conversation as resolved.
Show resolved Hide resolved

return -1;
}

static void
_set_msg_severity(LogMessage *msg, const guint16 severity)
{
msg->pri = (msg->pri & ~LOG_PRIMASK) | severity;
}

static void
log_rewrite_set_severity_process(LogRewrite *s, LogMessage **pmsg, const LogPathOptions *path_options)
{
ScratchBuffersMarker marker;
GString *result = scratch_buffers_alloc_and_mark(&marker);
LogRewriteSetSeverity *self = (LogRewriteSetSeverity *) s;

log_msg_make_writable(pmsg, path_options);

log_template_format(self->severity, *pmsg, NULL, LTZ_LOCAL, 0, NULL, result);

const gint severity = _convert_severity(result);
if (severity < 0)
{
msg_debug("Warning: invalid severity to set", evt_tag_str("severity", result->str), log_pipe_location_tag(&s->super));
goto error;
}

_set_msg_severity(*pmsg, severity);

error:
scratch_buffers_reclaim_marked(marker);
}

static LogPipe *
log_rewrite_set_severity_clone(LogPipe *s)
{
LogRewriteSetSeverity *self = (LogRewriteSetSeverity *) s;
LogRewriteSetSeverity *cloned = (LogRewriteSetSeverity *)log_rewrite_set_severity_new(log_template_ref(self->severity),
s->cfg);

if (self->super.condition)
cloned->super.condition = filter_expr_ref(self->super.condition);

return &cloned->super.super;
}

void
log_rewrite_set_severity_free(LogPipe *s)
{
LogRewriteSetSeverity *self = (LogRewriteSetSeverity *) s;
log_template_unref(self->severity);
log_rewrite_free_method(s);
}

LogRewrite *
log_rewrite_set_severity_new(LogTemplate *severity, GlobalConfig *cfg)
{
LogRewriteSetSeverity *self = g_new0(LogRewriteSetSeverity, 1);

log_rewrite_init_instance(&self->super, cfg);
self->super.super.free_fn = log_rewrite_set_severity_free;
self->super.super.clone = log_rewrite_set_severity_clone;
self->super.process = log_rewrite_set_severity_process;
self->severity = severity;
return &self->super;
}
32 changes: 32 additions & 0 deletions lib/rewrite/rewrite-set-severity.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2019 Balabit
* Copyright (c) 2019 Kokan <[email protected]>
*
* 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.
*
*/

#ifndef REWRITE_SET_SEVERITY_H_INCLUDED
#define REWRITE_SET_SEVERITY_H_INCLUDED

#include "rewrite-expr.h"

LogRewrite *log_rewrite_set_severity_new(LogTemplate *severity, GlobalConfig *cfg);

#endif
1 change: 1 addition & 0 deletions lib/rewrite/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
add_unit_test(LIBTEST CRITERION TARGET test_rewrite)
add_unit_test(LIBTEST CRITERION TARGET test_set_severity)
10 changes: 7 additions & 3 deletions lib/rewrite/tests/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
lib_rewrite_tests_TESTS = \
lib/rewrite/tests/test_rewrite
lib/rewrite/tests/test_rewrite \
lib/rewrite/tests/test_set_severity

EXTRA_DIST += lib/rewrite/tests/CMakeLists.txt

check_PROGRAMS += ${lib_rewrite_tests_TESTS}

lib_rewrite_tests_test_rewrite_CFLAGS = $(TEST_CFLAGS)

lib_rewrite_tests_test_rewrite_LDADD = $(TEST_LDADD) \
$(PREOPEN_SYSLOGFORMAT)

lib_rewrite_tests_test_rewrite_SOURCES = \
lib/rewrite/tests/test_rewrite.c

lib_rewrite_tests_test_set_severity_CFLAGS = $(TEST_CFLAGS)
lib_rewrite_tests_test_set_severity_LDADD = $(TEST_LDADD)
lib_rewrite_tests_test_set_severity_SOURCES = \
lib/rewrite/tests/test_set_severity.c
Loading