-
Notifications
You must be signed in to change notification settings - Fork 494
Add new rewrite function: set-severity() #3115
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
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ff45e15
cfg-grammar: use LOG_PRIMASK
Kokan 9386a6a
rewrite: set-level()
Kokan c534b51
cisco-parser: update LEVEL hard macro
Kokan 5526fca
set-level: accept text format
Kokan b919194
set-level: use scratch buffer
Kokan 9f875e2
set-level: Fix build after rebase
smortex bdc619e
set-level: Rename to set-severity
smortex 1264c6b
set-severity: lower the severity of malformed severity logging
smortex 926b4c3
set-severity: Move setting severity to cisco-parser()
smortex 3fffd0f
set-severity: Avoid calling _convert_severity() twice
smortex c39184e
set-severity: Prove behaviour with invalid severity
smortex f694910
syslog-names: Add more severity names
smortex a48c6d1
set-severity: Fix setting severity to emergency
smortex 353ea3c
set-severity: Avoid reading an unallocated message
smortex a552d62
set-severity: Fix setting a wrong severity with empty template
smortex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Outdated
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.
Outdated
Show resolved
Hide resolved
|
||
|
||
severity = _convert_severity_as_text(severity_text); | ||
if (severity >= 0) | ||
return severity; | ||
smortex marked this conversation as resolved.
Outdated
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.