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 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
|
/*
* Copyright (c) 2002-2014 Balabit
* Copyright (c) 1998-2012 Balázs Scheidler
* Copyright (c) 2014 Viktor Tusa <tusavik@gmail.com>
*
* 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 <string.h>
#include <ctype.h>
static void
tf_echo(LogMessage *msg, gint argc, GString *argv[], GString *result, LogMessageValueType *type)
{
*type = LM_VT_STRING;
_append_args_with_separator(argc, argv, result, ' ');
}
TEMPLATE_FUNCTION_SIMPLE(tf_echo);
static void
tf_length(LogMessage *msg, gint argc, GString *argv[], GString *result, LogMessageValueType *type)
{
gint i;
*type = LM_VT_INTEGER;
for (i = 0; i < argc; i++)
{
format_uint32_padded(result, 0, 0, 10, argv[i]->len);
if (i < argc - 1)
g_string_append_c(result, ' ');
}
}
TEMPLATE_FUNCTION_SIMPLE(tf_length);
/*
* $(substr $arg START [LEN])
*/
static void
tf_substr(LogMessage *msg, gint argc, GString *argv[], GString *result, LogMessageValueType *type)
{
gint64 start, len;
*type = LM_VT_STRING;
/*
* We need to cast argv[0]->len, which is gsize (so unsigned) type, to a
* signed type, so we can compare negative offsets/lengths with string
* length. But if argv[0]->len is bigger than G_MAXLONG, this can lead to
* completely wrong calculations, so we'll just return nothing (alternative
* would be to return original string and perhaps print an error...)
*/
if (argv[0]->len >= G_MAXLONG)
{
msg_error("$(substr) error: string is too long");
return;
}
/* check number of arguments */
if (argc < 2 || argc > 3)
return;
/* get offset position from second argument */
if (!parse_int64(argv[1]->str, &start))
{
msg_error("$(substr) parsing failed, start could not be parsed",
evt_tag_str("start", argv[1]->str));
return;
}
/* if we were called with >2 arguments, third was desired length */
if (argc > 2)
{
if (!parse_int64(argv[2]->str, &len))
{
msg_error("$(substr) parsing failed, length could not be parsed",
evt_tag_str("length", argv[2]->str));
return;
}
}
else
len = (glong)argv[0]->len;
/*
* if requested substring length is negative and beyond string start, do nothing;
* also if it is greater than string length, limit it to string length
*/
if (len < 0 && -(len) > (glong)argv[0]->len)
return;
else if (len > (glong)argv[0]->len)
len = (glong)argv[0]->len;
/*
* if requested offset is beyond string length, do nothing;
* also, if offset is negative and "before" string beginning, do nothing
* (or should we perhaps align start with the beginning instead?)
*/
if (start >= (glong)argv[0]->len)
return;
else if (start < 0 && -(start) > (glong)argv[0]->len)
return;
/* with negative length, see if we don't end up with start > end */
if (len < 0 && ((start < 0 && start > len) ||
(start >= 0 && (len + ((glong)argv[0]->len) - start) < 0)))
return;
/* if requested offset is negative, move start it accordingly */
if (start < 0)
{
start = start + (glong)argv[0]->len;
/*
* this shouldn't actually happen, as earlier we tested for
* (start < 0 && -start > argv0len), but better safe than sorry
*/
if (start < 0)
start = 0;
}
/*
* if requested length is negative, "resize" len to include exactly as many
* characters as needed from the end of the string, given our start position.
* (start is always non-negative here already)
*/
if (len < 0)
{
len = ((glong)argv[0]->len) - start + len;
/* this also shouldn't happen, but - again - better safe than sorry */
if (len < 0)
return;
}
/* if we're beyond string end, do nothing */
if (start >= (glong)argv[0]->len)
return;
/* if we want something beyond string end, do it only till the end */
if (start + len > (glong)argv[0]->len)
len = ((glong)argv[0]->len) - start;
/* skip g_string_append_len if we ended up having to extract 0 chars */
if (len == 0)
return;
g_string_append_len(result, argv[0]->str + start, len);
}
TEMPLATE_FUNCTION_SIMPLE(tf_substr);
/*
* $(strip $arg1 $arg2 ...)
*/
static void
tf_strip(LogMessage *msg, gint argc, GString *argv[], GString *result, LogMessageValueType *type)
{
gsize initial_len = result->len;
gint i;
*type = LM_VT_STRING;
for (i = 0; i < argc; i++)
{
if (argv[i]->len == 0)
continue;
gint spaces_end = 0;
while (isspace(argv[i]->str[argv[i]->len - spaces_end - 1]) && spaces_end < argv[i]->len)
spaces_end++;
if (argv[i]->len == spaces_end)
continue;
gint spaces_start = 0;
while (isspace(argv[i]->str[spaces_start]))
spaces_start++;
if (result->len > initial_len)
g_string_append_c(result, ' ');
g_string_append_len(result, &argv[i]->str[spaces_start], argv[i]->len - spaces_end - spaces_start);
}
}
TEMPLATE_FUNCTION_SIMPLE(tf_strip);
/*
* $(sanitize [opts] $arg1 $arg2 ...)
*
* Options:
* --ctrl-chars or -c Filter control characters (default)
* --no-ctrl-chars or -C Don't filter control characters
* --invalid-chars <set> or -i Set of characters to be translated, default "/"
* --replacement <replace> or -r Single character replacement for invalid chars.
*/
typedef struct _TFSanitizeState
{
TFSimpleFuncState super;
gboolean ctrl_chars;
gchar replacement;
gchar *invalid_chars;
} TFSanitizeState;
static gboolean
tf_sanitize_prepare(LogTemplateFunction *self, gpointer s, LogTemplate *parent, gint argc, gchar *argv[],
GError **error)
{
TFSanitizeState *state = (TFSanitizeState *) s;
gboolean ctrl_chars = TRUE;
gchar *invalid_chars = NULL;
gchar *replacement = NULL;
GOptionContext *ctx;
GOptionEntry stize_options[] =
{
{ "ctrl-chars", 'c', 0, G_OPTION_ARG_NONE, &ctrl_chars, NULL, NULL },
{ "no-ctrl-chars", 'C', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &ctrl_chars, NULL, NULL },
{ "invalid-chars", 'i', 0, G_OPTION_ARG_STRING, &invalid_chars, NULL, NULL },
{ "replacement", 'r', 0, G_OPTION_ARG_STRING, &replacement, NULL, NULL },
{ NULL }
};
gboolean result = FALSE;
ctx = g_option_context_new("sanitize-file");
g_option_context_add_main_entries(ctx, stize_options, NULL);
if (!g_option_context_parse(ctx, &argc, &argv, error))
{
g_option_context_free(ctx);
goto exit;
}
g_option_context_free(ctx);
invalid_chars = invalid_chars ? : g_strdup("/");
replacement = replacement ? : g_strdup("_");
if (!tf_simple_func_prepare(self, state, parent, argc, argv, error))
{
goto exit;
}
state->ctrl_chars = ctrl_chars;
state->invalid_chars = g_strdup(invalid_chars);
state->replacement = replacement[0];
result = TRUE;
exit:
/* glib supplies us with duplicated strings that we are responsible for! */
g_free(invalid_chars);
g_free(replacement);
return result;
}
static void
tf_sanitize_call(LogTemplateFunction *self, gpointer s, const LogTemplateInvokeArgs *args, GString *result,
LogMessageValueType *type)
{
TFSanitizeState *state = (TFSanitizeState *) s;
gint argc;
gint i, pos;
*type = LM_VT_STRING;
argc = state->super.argc;
for (i = 0; i < argc; i++)
{
for (pos = 0; pos < args->argv[i]->len; pos++)
{
guchar last_char = args->argv[i]->str[pos];
if ((state->ctrl_chars && last_char < 32) ||
(strchr(state->invalid_chars, (gchar) last_char) != NULL))
g_string_append_c(result, state->replacement);
else
g_string_append_c(result, last_char);
}
if (i < argc - 1)
g_string_append_c(result, '/');
}
}
static void
tf_sanitize_free_state(gpointer s)
{
TFSanitizeState *state = (TFSanitizeState *) s;
g_free(state->invalid_chars);
tf_simple_func_free_state(&state->super);
}
TEMPLATE_FUNCTION(TFSanitizeState, tf_sanitize, tf_sanitize_prepare, tf_simple_func_eval, tf_sanitize_call,
tf_sanitize_free_state, NULL);
void
tf_indent_multi_line(LogMessage *msg, gint argc, GString *argv[], GString *text, LogMessageValueType *type)
{
gchar *p, *new_line;
*type = LM_VT_STRING;
/* append the message text(s) to the template string */
_append_args_with_separator(argc, argv, text, ' ');
/* look up the \n-s and insert a \t after them */
p = text->str;
new_line = memchr(p, '\n', text->len);
while(new_line)
{
if (*(gchar *)(new_line + 1) != '\t')
{
g_string_insert_c(text, new_line - p + 1, '\t');
}
new_line = memchr(new_line + 1, '\n', p + text->len - new_line);
}
}
TEMPLATE_FUNCTION_SIMPLE(tf_indent_multi_line);
void
tf_lowercase(LogMessage *msg, gint argc, GString *argv[], GString *result, LogMessageValueType *type)
{
gint i;
*type = LM_VT_STRING;
for (i = 0; i < argc; i++)
{
gchar *new = g_utf8_strdown(argv[i]->str, argv[i]->len);
g_string_append(result, new);
if (i < argc - 1)
g_string_append_c(result, ' ');
g_free(new);
}
}
TEMPLATE_FUNCTION_SIMPLE(tf_lowercase);
void
tf_uppercase(LogMessage *msg, gint argc, GString *argv[], GString *result, LogMessageValueType *type)
{
gint i;
*type = LM_VT_STRING;
for (i = 0; i < argc; i++)
{
gchar *new = g_utf8_strup(argv[i]->str, argv[i]->len);
g_string_append(result, new);
if (i < argc - 1)
g_string_append_c(result, ' ');
g_free(new);
}
}
TEMPLATE_FUNCTION_SIMPLE(tf_uppercase);
void
tf_replace_delimiter(LogMessage *msg, gint argc, GString *argv[], GString *result, LogMessageValueType *type)
{
gchar *haystack, *delimiters, new_delimiter;
*type = LM_VT_STRING;
if (argc != 3)
{
msg_error("$(replace-delimiter) parsing failed, wrong number of arguments");
return;
}
delimiters = argv[0]->str;
new_delimiter = argv[1]->str[0];
haystack = g_strdup(argv[2]->str);
g_string_append(result, g_strdelimit(haystack, delimiters, new_delimiter));
g_free(haystack);
}
TEMPLATE_FUNCTION_SIMPLE(tf_replace_delimiter);
typedef struct _TFStringPaddingState
{
TFSimpleFuncState super;
GString *padding_pattern;
gint64 width;
} TFStringPaddingState;
static gboolean
_padding_prepare_parse_state(TFStringPaddingState *state, gint argc, gchar **argv, GError **error)
{
if (argc < 3)
{
g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
"$(padding) Not enough arguments, usage $(padding <input> <width> [padding string])");
return FALSE;
}
if (!parse_int64(argv[2], &state->width))
{
g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
"Padding template function requires a number as second argument!");
return FALSE;
}
return TRUE;
}
static void
_padding_prepare_fill_padding_pattern(TFStringPaddingState *state, gint argc, gchar **argv)
{
state->padding_pattern = g_string_sized_new(state->width);
if (argc < 4)
{
g_string_printf(state->padding_pattern, "%*s", (int)(state->width), "");
}
else
{
gint len = strlen(argv[3]);
if (len < 1)
{
g_string_printf(state->padding_pattern, "%*s", (int)(state->width), "");
}
else
{
gint repeat = state->width / len; // integer division!
for (gint i = 0; i < repeat; i++)
{
g_string_append_len(state->padding_pattern, argv[3], len);
}
g_string_append_len(state->padding_pattern, argv[3], state->width - (repeat * len));
}
}
}
static gboolean
tf_string_padding_prepare(LogTemplateFunction *self, gpointer s, LogTemplate *parent,
gint argc, gchar *argv[], GError **error)
{
TFStringPaddingState *state = (TFStringPaddingState *) s;
if (!_padding_prepare_parse_state(state, argc, argv, error))
return FALSE;
_padding_prepare_fill_padding_pattern(state, argc, argv);
if (!tf_simple_func_prepare(self, state, parent, 2, argv, error))
{
g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
"padding: prepare failed");
return FALSE;
}
return TRUE;
}
static void
tf_string_padding_call(LogTemplateFunction *self, gpointer s, const LogTemplateInvokeArgs *args, GString *result,
LogMessageValueType *type)
{
TFStringPaddingState *state = (TFStringPaddingState *) s;
*type = LM_VT_STRING;
if (args->argv[0]->len > state->width)
{
g_string_append_len(result, args->argv[0]->str, args->argv[0]->len);
}
else
{
g_string_append_len(result, state->padding_pattern->str, state->width - args->argv[0]->len);
g_string_append_len(result, args->argv[0]->str, args->argv[0]->len);
}
}
static void
tf_string_padding_free_state(gpointer s)
{
TFStringPaddingState *state = (TFStringPaddingState *) s;
if (state->padding_pattern)
g_string_free(state->padding_pattern, TRUE);
tf_simple_func_free_state(&state->super);
}
TEMPLATE_FUNCTION(TFStringPaddingState, tf_string_padding, tf_string_padding_prepare,
tf_simple_func_eval, tf_string_padding_call, tf_string_padding_free_state, NULL);
typedef struct _TFBinaryState
{
TFSimpleFuncState super;
GString *octets;
} TFBinaryState;
static void
tf_binary_call(LogTemplateFunction *self, gpointer s, const LogTemplateInvokeArgs *args, GString *result,
LogMessageValueType *type)
{
TFBinaryState *state = (TFBinaryState *) s;
*type = LM_VT_STRING;
g_string_append_len(result, state->octets->str, state->octets->len);
}
static gboolean
tf_binary_prepare(LogTemplateFunction *self, gpointer s, LogTemplate *parent, gint argc, gchar *argv[], GError **error)
{
TFBinaryState *state = (TFBinaryState *) s;
GString *octets = g_string_new("");
if (argc < 2)
{
g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
"$(binary) Incorrect parameters, usage $(binary <number> <number> ...)");
goto error;
}
for (gint i = 1; i < argc; i++)
{
gint64 number;
gchar *token = argv[i];
if (!parse_int64_base_any(token, &number))
{
gchar *base = "dec";
if (token[0] == '0')
{
base = token[1] == 'x' ? "hex" : "oct";
}
g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
"$(binary) template function requires list of dec/hex/oct numbers as arguments, unable to parse %s as a %s number",
token, base);
goto error;
}
if (number > 0xFF)
{
g_set_error(error, LOG_TEMPLATE_ERROR, LOG_TEMPLATE_ERROR_COMPILE,
"$(binary) template function only supports 8 bit values as characters, %" G_GUINT64_FORMAT " is above 255", number);
goto error;
}
g_string_append_c(octets, (gchar) number);
}
if (!tf_simple_func_prepare(self, state, parent, argc, argv, error))
{
goto error;
}
state->octets = octets;
return TRUE;
error:
g_string_free(octets, TRUE);
return FALSE;
}
static void
tf_binary_free_state(gpointer s)
{
TFBinaryState *state = (TFBinaryState *) s;
if (state->octets)
g_string_free(state->octets, TRUE);
tf_simple_func_free_state(&state->super);
}
TEMPLATE_FUNCTION(TFBinaryState, tf_binary, tf_binary_prepare, tf_simple_func_eval, tf_binary_call,
tf_binary_free_state, NULL);
static inline gsize
_get_base64_encoded_size(gsize len)
{
return (len / 3 + 1) * 4 + 4;
}
static void
tf_base64encode(LogMessage *msg, gint argc, GString *argv[], GString *result, LogMessageValueType *type)
{
gint i;
gint state = 0;
gint save = 0;
gsize out_len = 0;
gsize init_len = result->len;
*type = LM_VT_STRING;
for (i = 0; i < argc; i++)
{
/* expand the buffer and add space for the base64 encoded string */
g_string_set_size(result, init_len + out_len + _get_base64_encoded_size(argv[i]->len));
out_len +=
g_base64_encode_step((guchar *) argv[i]->str, argv[i]->len,
FALSE /* break_lines */,
result->str + init_len + out_len,
&state, &save);
}
g_string_set_size(result, init_len + out_len + _get_base64_encoded_size(0));
#if !GLIB_CHECK_VERSION(2, 54, 0)
/* NOTE: this is an ugly workaround for glib versions < 2.54 (which is
* pretty recent and not widely available yet) to fix an encoding issue.
*
* This is the bug:
* https://bugzilla.gnome.org/show_bug.cgi?id=780066
*
* This is the fix:
* https://gitlab.gnome.org/GNOME/glib/commits/35c0dd2755dbcea2539117cf33959a1a9e497f12
*
* We basically set the c2 byte used in a base64 encode to zero, if only 1
* remaining byte is there. Read the bugreport for reasons.
*
* Yes, I've actually stumbled into this in the unit test and could easily
* anyone.
*/
if (((unsigned char *) &save)[0] == 1)
((unsigned char *) &save)[2] = 0;
#endif
out_len += g_base64_encode_close(FALSE, result->str + init_len + out_len, &state, &save);
g_string_set_size(result, init_len + out_len);
};
TEMPLATE_FUNCTION_SIMPLE(tf_base64encode);
|