Skip to content

Commit

Permalink
Merge pull request #4666 from bazsi/driver-specific-template-escaping
Browse files Browse the repository at this point in the history
Driver specific template escaping
  • Loading branch information
alltilla authored Oct 19, 2023
2 parents eb1f04a + 2b79680 commit 5e761f8
Show file tree
Hide file tree
Showing 32 changed files with 571 additions and 219 deletions.
2 changes: 1 addition & 1 deletion lib/apphook.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "logsource.h"
#include "logwriter.h"
#include "afinter.h"
#include "template/templates.h"
#include "template/globals.h"
#include "hostname.h"
#include "mainloop-call.h"
#include "service-management.h"
Expand Down
1 change: 0 additions & 1 deletion lib/cfg-grammar-internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ extern LogSchedulerOptions *last_scheduler_options;
extern LogParser *last_parser;
extern FilterExprNode *last_filter_expr;
extern LogTemplateOptions *last_template_options;
extern LogTemplate *last_template;
extern ValuePairs *last_value_pairs;
extern ValuePairsTransformSet *last_vp_transset;
extern LogMatcherOptions *last_matcher_options;
Expand Down
82 changes: 43 additions & 39 deletions lib/cfg-grammar.y
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@
%token LL_CONTEXT_SERVER_PROTO 18
%token LL_CONTEXT_OPTIONS 19
%token LL_CONTEXT_CONFIG 20
%token LL_CONTEXT_TEMPLATE_REF 21

/* this is a placeholder for unit tests, must be the latest & largest */
%token LL_CONTEXT_MAX 21
%token LL_CONTEXT_MAX 22


/* statements */
Expand Down Expand Up @@ -337,6 +338,7 @@
%token <token> LL_TOKEN 10434
%token <cptr> LL_BLOCK 10435
%token <cptr> LL_PLUGIN 10436
%token <cptr> LL_TEMPLATE_REF 10437

%destructor { free($$); } <cptr>

Expand Down Expand Up @@ -388,6 +390,8 @@

%type <ptr> template_content
%type <ptr> template_content_list
%type <ptr> template_name_or_content
%type <ptr> template_name_or_content_tail

%type <ptr> filter_content

Expand Down Expand Up @@ -833,9 +837,9 @@ template_stmt
}
| template_fn
{
user_template_function_register(configuration, last_template->name, last_template);
log_template_unref(last_template);
last_template = NULL;
LogTemplate *template = $1;
user_template_function_register(configuration, template->name, template);
log_template_unref(template);
}
;

Expand All @@ -847,82 +851,95 @@ template_def
template_block
: KW_TEMPLATE string
<ptr>{
$$ = last_template = log_template_new(configuration, $2);
$$ = log_template_new(configuration, $2);
}
'{' template_items '}' { $$ = $3; free($2); }
'{' { $<ptr>$ = $3; } template_items '}' { $$ = $3; free($2); }
;

template_simple
: KW_TEMPLATE string
<ptr>{
$$ = last_template = log_template_new(configuration, $2);
$$ = log_template_new(configuration, $2);
}
template_content_inner { $$ = $3; free($2); }
;

template_fn
: KW_TEMPLATE_FUNCTION string
<ptr>{
$$ = last_template = log_template_new(configuration, $2);
$$ = log_template_new(configuration, $2);
}
template_content_inner { $$ = $3; free($2); }
;

template_items
: template_item semicolons template_items
: { $<ptr>$ = $<ptr>0; } template_item semicolons template_items
|
;

template_item
: KW_TEMPLATE '(' { $<ptr>$ = $<ptr>0; } template_content_inner ')'
| KW_TEMPLATE_ESCAPE '(' yesno ')' { log_template_set_escape($<ptr>0, $3); }
;

/* START_RULES */

/* $0 must be the <ptr> for the LogTemplate to be populated */
template_content_inner
: string
{
GError *error = NULL;

CHECK_ERROR_GERROR(log_template_compile(last_template, $1, &error), @1, error, "Error compiling template");
CHECK_ERROR_GERROR(log_template_compile($<ptr>0, $1, &error), @1, error, "Error compiling template");
free($1);
}
| LL_IDENTIFIER '(' string_or_number ')'
{
GError *error = NULL;

CHECK_ERROR_GERROR(log_template_compile(last_template, $3, &error), @3, error, "Error compiling template");
CHECK_ERROR_GERROR(log_template_compile($<ptr>0, $3, &error), @3, error, "Error compiling template");
free($3);

CHECK_ERROR_GERROR(log_template_set_type_hint(last_template, $1, &error), @1, error, "Error setting the template type-hint \"%s\"", $1);
CHECK_ERROR_GERROR(log_template_set_type_hint($<ptr>0, $1, &error), @1, error, "Error setting the template type-hint \"%s\"", $1);
free($1);
}
| LL_NUMBER
{
gchar decimal[32];

g_snprintf(decimal, sizeof(decimal), "%" G_GINT64_FORMAT, $1);
log_template_compile_literal_string(last_template, decimal);
log_template_set_type_hint(last_template, "int64", NULL);
log_template_compile_literal_string($<ptr>0, decimal);
log_template_set_type_hint($<ptr>0, "int64", NULL);
}
| LL_FLOAT
{
log_template_compile_literal_string(last_template, lexer->token_text->str);
log_template_set_type_hint(last_template, "float", NULL);
log_template_compile_literal_string($<ptr>0, lexer->token_text->str);
log_template_set_type_hint($<ptr>0, "float", NULL);
}
;

template_content
: <ptr>{ $$ = last_template = log_template_new(configuration, NULL); } template_content_inner { $$ = $1; }
: <ptr>{
$$ = log_template_new(configuration, NULL);
} template_content_inner { $$ = $1; }
;

template_content_list
: template_content template_content_list { $$ = g_list_prepend($2, $1); }
| { $$ = NULL; }
;

template_name_or_content
: _template_ref_context_push template_name_or_content_tail { $$ = $2; };
;

template_name_or_content_tail
: LL_TEMPLATE_REF { $$ = cfg_tree_lookup_template(&configuration->tree, $1); free($1); }
| template_content { $$ = $1; };
;

/* END_RULES */

template_item
: KW_TEMPLATE '(' template_content_inner ')'
| KW_TEMPLATE_ESCAPE '(' yesno ')' { log_template_set_escape(last_template, $3); }
;


block_stmt
Expand Down Expand Up @@ -1207,16 +1224,8 @@ facility_string
;

parser_opt
: KW_TEMPLATE '(' string ')' {
LogTemplate *template;
GError *error = NULL;

template = cfg_tree_check_inline_template(&configuration->tree, $3, &error);
CHECK_ERROR_GERROR(template != NULL, @3, error, "Error compiling template");
log_parser_set_template(last_parser, template);
free($3);
}
| KW_INTERNAL '(' yesno ')' { log_pipe_set_internal(&last_parser->super, $3); }
: KW_TEMPLATE '(' template_name_or_content ')' { log_parser_set_template(last_parser, $3); }
| KW_INTERNAL '(' yesno ')' { log_pipe_set_internal(&last_parser->super, $3); }
;

driver_option
Expand Down Expand Up @@ -1422,14 +1431,7 @@ dest_writer_option
| KW_FLUSH_LINES '(' nonnegative_integer ')' { last_writer_options->flush_lines = $3; }
| KW_FLUSH_TIMEOUT '(' positive_integer ')' { }
| KW_SUPPRESS '(' nonnegative_integer ')' { last_writer_options->suppress = $3; }
| KW_TEMPLATE '(' string ')' {
GError *error = NULL;

last_writer_options->template = cfg_tree_check_inline_template(&configuration->tree, $3, &error);
CHECK_ERROR_GERROR(last_writer_options->template != NULL, @3, error, "Error compiling template");
free($3);
}
| KW_TEMPLATE_ESCAPE '(' yesno ')' { log_writer_options_set_template_escape(last_writer_options, $3); }
| KW_TEMPLATE '(' template_name_or_content ')' { last_writer_options->template = $3; }
| KW_PAD_SIZE '(' nonnegative_integer ')' { last_writer_options->padding = $3; }
| KW_TRUNCATE_SIZE '(' nonnegative_integer ')' { last_writer_options->truncate_size = $3; }
| KW_MARK_FREQ '(' nonnegative_integer ')' { last_writer_options->mark_freq = $3; }
Expand Down Expand Up @@ -1470,6 +1472,7 @@ template_option
| KW_TIME_ZONE '(' string ')' { last_template_options->time_zone[LTZ_SEND] = g_strdup($3); free($3); }
| KW_SEND_TIME_ZONE '(' string ')' { last_template_options->time_zone[LTZ_SEND] = g_strdup($3); free($3); }
| KW_LOCAL_TIME_ZONE '(' string ')' { last_template_options->time_zone[LTZ_LOCAL] = g_strdup($3); free($3); }
| KW_TEMPLATE_ESCAPE '(' yesno ')' { last_template_options->escape = $3; }
| KW_ON_ERROR '(' string ')'
{
gint on_error;
Expand Down Expand Up @@ -1628,6 +1631,7 @@ _block_content_context_push: { cfg_lexer_push_context(lexer, LL_CONTEXT_BLOCK_CO
_block_content_context_pop: { cfg_lexer_pop_context(lexer); };
_block_arg_context_push: { cfg_lexer_push_context(lexer, LL_CONTEXT_BLOCK_ARG, NULL, "block argument"); };
_block_arg_context_pop: { cfg_lexer_pop_context(lexer); };
_template_ref_context_push: { cfg_lexer_push_context(lexer, LL_CONTEXT_TEMPLATE_REF, NULL, "template reference"); };
_inner_dest_context_push: { cfg_lexer_push_context(lexer, LL_CONTEXT_INNER_DEST, NULL, "within destination"); };
_inner_dest_context_pop: { cfg_lexer_pop_context(lexer); };
_inner_src_context_push: { cfg_lexer_push_context(lexer, LL_CONTEXT_INNER_SRC, NULL, "within source"); };
Expand Down
17 changes: 17 additions & 0 deletions lib/cfg-lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,22 @@ cfg_lexer_lex(CfgLexer *self, CFG_STYPE *yylval, CFG_LTYPE *yylloc)

tok = cfg_lexer_lex_next_token(self, yylval, yylloc);
cfg_lexer_append_preprocessed_output(self, self->token_pretext->str);

if (cfg_lexer_get_context_type(self) == LL_CONTEXT_TEMPLATE_REF)
{
cfg_lexer_pop_context(self);

if ((tok == LL_IDENTIFIER || tok == LL_STRING))
{

LogTemplate *template = cfg_tree_lookup_template(&configuration->tree, yylval->cptr);
if (template != NULL)
{
tok = LL_TEMPLATE_REF;
log_template_unref(template);
}
}
}
}

preprocess_result = cfg_lexer_preprocess(self, tok, yylval, yylloc);
Expand Down Expand Up @@ -1232,6 +1248,7 @@ static const gchar *lexer_contexts[] =
[LL_CONTEXT_SERVER_PROTO] = "server-proto",
[LL_CONTEXT_OPTIONS] = "options",
[LL_CONTEXT_CONFIG] = "config",
[LL_CONTEXT_TEMPLATE_REF] = "template-ref",
};

gint
Expand Down
13 changes: 0 additions & 13 deletions lib/logwriter.c
Original file line number Diff line number Diff line change
Expand Up @@ -1937,19 +1937,6 @@ log_writer_options_defaults(LogWriterOptions *options)
host_resolve_options_defaults(&options->host_resolve_options);
}

void
log_writer_options_set_template_escape(LogWriterOptions *options, gboolean enable)
{
if (options->template && options->template->def_inline)
{
log_template_set_escape(options->template, enable);
}
else
{
msg_error("Macro escaping can only be specified for inline templates");
}
}

void
log_writer_options_set_mark_mode(LogWriterOptions *options, const gchar *mark_mode)
{
Expand Down
2 changes: 2 additions & 0 deletions lib/template/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(TEMPLATE_HEADERS
template/templates.h
template/macros.h
template/function.h
template/globals.h
template/eval.h
template/simple-function.h
template/repr.h
Expand All @@ -15,6 +16,7 @@ set(TEMPLATE_SOURCES
template/templates.c
template/macros.c
template/eval.c
template/globals.c
template/simple-function.c
template/repr.c
template/compiler.c
Expand Down
2 changes: 2 additions & 0 deletions lib/template/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ templateinclude_HEADERS = \
lib/template/templates.h \
lib/template/macros.h \
lib/template/function.h \
lib/template/globals.h \
lib/template/eval.h \
lib/template/simple-function.h \
lib/template/repr.h \
Expand All @@ -18,6 +19,7 @@ templateinclude_HEADERS = \
template_sources = \
lib/template/templates.c \
lib/template/macros.c \
lib/template/globals.c \
lib/template/eval.c \
lib/template/simple-function.c \
lib/template/repr.c \
Expand Down
28 changes: 28 additions & 0 deletions lib/template/common-template-typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,35 @@
#ifndef COMMON_TYPEDEFS_H_INCLUDED
#define COMMON_TYPEDEFS_H_INCLUDED

#include "timeutils/zoneinfo.h"

#define LTZ_LOCAL 0
#define LTZ_SEND 1
#define LTZ_MAX 2

typedef struct _LogTemplateOptions LogTemplateOptions;
typedef struct _LogTemplate LogTemplate;

/* template expansion options that can be influenced by the user and
* is static throughout the runtime for a given configuration. There
* are call-site specific options too, those are specified as
* arguments to log_template_format() */
struct _LogTemplateOptions
{
gboolean initialized;
/* timestamp format as specified by ts_format() */
gint ts_format;
/* number of digits in the fraction of a second part, specified using frac_digits() */
gint frac_digits;
gboolean use_fqdn;
gboolean escape;

/* timezone for LTZ_LOCAL/LTZ_SEND settings */
gchar *time_zone[LTZ_MAX];
TimeZoneInfo *time_zone_info[LTZ_MAX];

/* Template error handling settings */
gint on_error;
};

#endif
34 changes: 13 additions & 21 deletions lib/template/escaping.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,24 @@
#include <string.h>

void
result_append(GString *result, const gchar *sstr, gssize len, gboolean escape)
log_template_default_escape_method(GString *result, const gchar *sstr, gsize len)
{
gint i;
gsize i;
const guchar *ustr = (const guchar *) sstr;

if (len < 0)
len = strlen(sstr);

if (escape)
for (i = 0; i < len; i++)
{
for (i = 0; i < len; i++)
if (ustr[i] == '\'' || ustr[i] == '"' || ustr[i] == '\\')
{
g_string_append_c(result, '\\');
g_string_append_c(result, ustr[i]);
}
else if (ustr[i] < ' ')
{
if (ustr[i] == '\'' || ustr[i] == '"' || ustr[i] == '\\')
{
g_string_append_c(result, '\\');
g_string_append_c(result, ustr[i]);
}
else if (ustr[i] < ' ')
{
g_string_append_c(result, '\\');
format_uint32_padded(result, 3, '0', 8, ustr[i]);
}
else
g_string_append_c(result, ustr[i]);
g_string_append_c(result, '\\');
format_uint32_padded(result, 3, '0', 8, ustr[i]);
}
else
g_string_append_c(result, ustr[i]);
}
else
g_string_append_len(result, sstr, len);
}
4 changes: 3 additions & 1 deletion lib/template/escaping.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include "syslog-ng.h"

void result_append(GString *result, const gchar *sstr, gssize len, gboolean escape);
typedef void (*LogTemplateEscapeFunction)(GString *target, const gchar *value, gsize value_len);

void log_template_default_escape_method(GString *result, const gchar *sstr, gsize len);

#endif
Loading

0 comments on commit 5e761f8

Please sign in to comment.