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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* Copyright (c) 2017 Balabit
*
* 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 "snmptrapd-parser.h"
#include "snmptrapd-header-parser.h"
#include "snmptrapd-nv-context.h"
#include "varbindlist-scanner.h"
#include "scratch-buffers.h"
#include "utf8utils.h"
#include "str-utils.h"
typedef struct _SnmpTrapdParser
{
LogParser super;
GString *prefix;
gboolean set_message_macro;
} SnmpTrapdParser;
void
snmptrapd_parser_set_prefix(LogParser *s, const gchar *prefix)
{
SnmpTrapdParser *self = (SnmpTrapdParser *) s;
if (!prefix)
g_string_truncate(self->prefix, 0);
else
g_string_assign(self->prefix, prefix);
}
void
snmptrapd_parser_set_set_message_macro(LogParser *s, gboolean set_message_macro)
{
SnmpTrapdParser *self = (SnmpTrapdParser *) s;
self->set_message_macro = set_message_macro;
}
static inline gboolean
_is_unwanted_key_char(gchar c)
{
return (c == ':');
}
static inline void
_move_and_replace_unwanted_key_chars(GString *key, gsize unwanted_key_count, gchar **curr_char_addr)
{
gsize number_of_removed_chars = unwanted_key_count - 1;
gchar *curr_pos = *curr_char_addr;
gchar *end_of_str = key->str + key->len;
gchar *replace_pos = curr_pos - unwanted_key_count;
if (unwanted_key_count > 1)
{
memmove(replace_pos, curr_pos - 1, end_of_str - (curr_pos - 1));
g_string_truncate(key, key->len - number_of_removed_chars);
}
*replace_pos = '_';
*curr_char_addr -= unwanted_key_count;
}
static const gchar *
_normalize_key(GString *key)
{
gsize unwanted_key_count = 0;
gchar *curr_char = key->str;
while (*curr_char)
{
if (_is_unwanted_key_char(*curr_char))
{
unwanted_key_count++;
}
else if (unwanted_key_count > 0)
{
_move_and_replace_unwanted_key_chars(key, unwanted_key_count, &curr_char);
unwanted_key_count = 0;
}
curr_char++;
}
if (unwanted_key_count)
_move_and_replace_unwanted_key_chars(key, unwanted_key_count, &curr_char);
return key->str;
}
static const gchar *
_get_formatted_key(const gchar *key, const GString *prefix, GString *formatted_key)
{
g_string_truncate(formatted_key, 0);
if (prefix->len > 0)
g_string_assign(formatted_key, prefix->str);
g_string_append(formatted_key, key);
_normalize_key(formatted_key);
return formatted_key->str;
}
static void
_append_name_value_to_generated_message(GString *generated_message, const gchar *key,
const gchar *value, gsize value_length)
{
ScratchBuffersMarker marker;
GString *escaped_value = scratch_buffers_alloc_and_mark(&marker);
if (generated_message->len > 0)
g_string_append(generated_message, ", ");
append_unsafe_utf8_as_escaped_text(escaped_value, value, value_length, "'");
g_string_append_printf(generated_message, "%s='%s'", key, escaped_value->str);
scratch_buffers_reclaim_marked(marker);
}
static void
_add_name_value(SnmpTrapdNVContext *nv_context, const gchar *key,
const gchar *value, gsize value_length)
{
ScratchBuffersMarker marker;
GString *formatted_key = scratch_buffers_alloc_and_mark(&marker);
const gchar *prefixed_key = _get_formatted_key(key, nv_context->key_prefix, formatted_key);
log_msg_set_value_by_name(nv_context->msg, prefixed_key, value, value_length);
if (nv_context->generated_message)
_append_name_value_to_generated_message(nv_context->generated_message, key, value, value_length);
scratch_buffers_reclaim_marked(marker);
}
static gboolean
_parse_varbindlist(SnmpTrapdNVContext *nv_context, const gchar **input, gsize *input_len)
{
VarBindListScanner varbindlist_scanner;
const gchar *key, *value;
varbindlist_scanner_init(&varbindlist_scanner);
varbindlist_scanner_input(&varbindlist_scanner, *input);
while (varbindlist_scanner_scan_next(&varbindlist_scanner))
{
key = varbindlist_scanner_get_current_key(&varbindlist_scanner);
value = varbindlist_scanner_get_current_value(&varbindlist_scanner);
snmptrapd_nv_context_add_name_value(nv_context, key, value, -1);
}
varbindlist_scanner_deinit(&varbindlist_scanner);
return TRUE;
}
static gboolean
snmptrapd_parser_process(LogParser *s, LogMessage **pmsg, const LogPathOptions *path_options,
const gchar *input, gsize input_len)
{
SnmpTrapdParser *self = (SnmpTrapdParser *) s;
ScratchBuffersMarker marker;
log_msg_make_writable(pmsg, path_options);
msg_trace("snmptrapd-parser message processing started",
evt_tag_str ("input", input),
evt_tag_str ("prefix", self->prefix->str),
evt_tag_printf("msg", "%p", *pmsg));
APPEND_ZERO(input, input, input_len);
GString *generated_message = NULL;
if (self->set_message_macro)
generated_message = scratch_buffers_alloc_and_mark(&marker);
SnmpTrapdNVContext nv_context =
{
.key_prefix = self->prefix,
.msg = *pmsg,
.generated_message = generated_message,
.add_name_value = _add_name_value
};
log_msg_set_value(nv_context.msg, LM_V_PROGRAM, "snmptrapd", -1);
if (!snmptrapd_header_parser_parse(&nv_context, &input, &input_len))
{
msg_debug("snmptrapd-parser failed",
evt_tag_str ("error", "cannot parse snmptrapd header"),
evt_tag_str ("input", input));
return FALSE;
};
if (!_parse_varbindlist(&nv_context, &input, &input_len))
{
msg_debug("snmptrapd-parser failed",
evt_tag_str ("error", "can not parse name-value pairs in the input"),
evt_tag_str ("input", input));
return FALSE;
};
if (self->set_message_macro)
{
log_msg_set_value(nv_context.msg, LM_V_MESSAGE, nv_context.generated_message->str, -1);
scratch_buffers_reclaim_marked(marker);
}
else
{
log_msg_unset_value(nv_context.msg, LM_V_MESSAGE);
}
return TRUE;
}
static LogPipe *
snmptrapd_parser_clone(LogPipe *s)
{
SnmpTrapdParser *self = (SnmpTrapdParser *) s;
SnmpTrapdParser *cloned = (SnmpTrapdParser *) snmptrapd_parser_new(s->cfg);
snmptrapd_parser_set_prefix(&cloned->super, self->prefix->str);
snmptrapd_parser_set_set_message_macro(&cloned->super, self->set_message_macro);
/* log_parser_clone_method() is missing.. */
log_parser_set_template(&cloned->super, log_template_ref(self->super.template));
return &cloned->super.super;
}
static void
snmptrapd_parser_free(LogPipe *s)
{
SnmpTrapdParser *self = (SnmpTrapdParser *) s;
g_string_free(self->prefix, TRUE);
log_parser_free_method(s);
}
LogParser *
snmptrapd_parser_new(GlobalConfig *cfg)
{
SnmpTrapdParser *self = g_new0(SnmpTrapdParser, 1);
log_parser_init_instance(&self->super, cfg);
self->super.super.free_fn = snmptrapd_parser_free;
self->super.super.clone = snmptrapd_parser_clone;
self->super.process = snmptrapd_parser_process;
self->prefix = g_string_new(".snmp.");
self->set_message_macro = TRUE;
return &self->super;
}
|