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
|
/*
* Copyright (c) 2002-2013 Balabit
* Copyright (c) 1998-2013 Balázs Scheidler
*
* 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 "template/templates.h"
#include "template/repr.h"
#include "template/compiler.h"
#include "template/macros.h"
#include "template/escaping.h"
#include "template/repr.h"
#include "timeutils/format.h"
#include "cfg.h"
gboolean
log_template_is_literal_string(const LogTemplate *self)
{
return self->literal;
}
const gchar *
log_template_get_literal_value(const LogTemplate *self, gssize *value_len)
{
g_assert(self->literal);
if (!self->compiled_template)
return "";
LogTemplateElem *e = (LogTemplateElem *) self->compiled_template->data;
if (value_len)
*value_len = e->text_len;
return e->text;
}
gboolean
log_template_is_trivial(LogTemplate *self)
{
return self->trivial;
}
NVHandle
log_template_get_trivial_value_handle(LogTemplate *self)
{
g_assert(self->trivial);
if (self->literal)
return LM_V_NONE;
LogTemplateElem *e = (LogTemplateElem *) self->compiled_template->data;
switch (e->type)
{
case LTE_MACRO:
if (e->macro == M_MESSAGE)
return LM_V_MESSAGE;
else if (e->macro == M_HOST)
return LM_V_HOST;
else
g_assert_not_reached();
break;
case LTE_VALUE:
return e->value_handle;
default:
g_assert_not_reached();
}
}
const gchar *
log_template_get_trivial_value_and_type(LogTemplate *self, LogMessage *msg, gssize *value_len,
LogMessageValueType *type)
{
LogMessageValueType t = LM_VT_STRING;
const gchar *result = "";
gssize result_len = 0;
g_assert(self->trivial);
if (self->literal)
{
result = log_template_get_literal_value(self, &result_len);
}
else
{
NVHandle handle = log_template_get_trivial_value_handle(self);
g_assert(handle != LM_V_NONE);
result = log_msg_get_value_with_type(msg, handle, &result_len, &t);
}
if (type)
{
*type = self->type_hint == LM_VT_NONE ? t : self->type_hint;
}
if (value_len)
*value_len = result_len;
return result;
}
const gchar *
log_template_get_trivial_value(LogTemplate *self, LogMessage *msg, gssize *value_len)
{
return log_template_get_trivial_value_and_type(self, msg, value_len, NULL);
}
static gboolean
_calculate_if_literal(LogTemplate *self)
{
if (!self->compiled_template)
return TRUE;
if (self->compiled_template->next)
return FALSE;
return log_template_elem_is_literal_string((LogTemplateElem *) self->compiled_template->data);
}
static gboolean
_calculate_if_trivial(LogTemplate *self)
{
/* if we need to escape, that's not trivial */
if (self->escape)
return FALSE;
/* empty templates are trivial */
if (self->compiled_template == NULL)
return TRUE;
/* more than one element */
if (self->compiled_template->next != NULL)
return FALSE;
const LogTemplateElem *e = (LogTemplateElem *) self->compiled_template->data;
/* reference to non-last element of the context, that's not trivial */
if (e->msg_ref > 0)
return FALSE;
if (log_template_elem_is_literal_string(e))
return TRUE;
switch (e->type)
{
case LTE_FUNC:
/* functions are never trivial */
return FALSE;
case LTE_MACRO:
if (e->text_len > 0)
return FALSE;
/* we have macros for MESSAGE and HOST for compatibility reasons, but
* they should be considered trivial */
if (e->macro == M_MESSAGE || e->macro == M_HOST)
return TRUE;
return FALSE;
case LTE_VALUE:
/* values are trivial if they don't contain text */
return e->text_len == 0;
default:
g_assert_not_reached();
}
}
static void
log_template_reset_compiled(LogTemplate *self)
{
log_template_elem_free_list(self->compiled_template);
self->compiled_template = NULL;
self->trivial = FALSE;
}
gboolean
log_template_compile(LogTemplate *self, const gchar *template, GError **error)
{
LogTemplateCompiler compiler;
gboolean result;
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
log_template_reset_compiled(self);
if (self->template_str)
g_free(self->template_str);
self->template_str = g_strdup(template);
log_template_compiler_init(&compiler, self);
result = log_template_compiler_compile(&compiler, &self->compiled_template, error);
log_template_compiler_clear(&compiler);
self->literal = _calculate_if_literal(self);
self->trivial = _calculate_if_trivial(self);
return result;
}
void
log_template_forget_template_string(LogTemplate *self)
{
g_free(self->template_str);
self->template_str = NULL;
}
static void
_split_type_and_template(gchar *spec, gchar **value, gchar **type)
{
char *sp, *ep;
*type = NULL;
sp = spec;
while (g_ascii_isalnum(*sp) || (*sp) == '_')
sp++;
while (*sp == ' ' || *sp == '\t')
sp++;
if (*sp != '(' ||
!((g_ascii_toupper(spec[0]) >= 'A' &&
g_ascii_toupper(spec[0]) <= 'Z') ||
spec[0] == '_'))
{
*value = spec;
return;
}
ep = strrchr(sp, ')');
if (ep == NULL || ep[1] != '\0')
{
*value = spec;
return;
}
*value = sp + 1;
*type = spec;
sp[0] = '\0';
ep[0] = '\0';
}
gboolean
log_template_compile_with_type_hint(LogTemplate *self, const gchar *template_and_typehint, GError **error)
{
gchar *buf = g_strdup(template_and_typehint);
gchar *template_string = NULL;
gchar *typehint_string = NULL;
gboolean result = FALSE;
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
_split_type_and_template(buf, &template_string, &typehint_string);
if (!log_template_compile(self, template_string, error))
goto exit;
if (!log_template_set_type_hint(self, typehint_string, error))
goto exit;
result = TRUE;
exit:
g_free(buf);
return result;
}
void
log_template_compile_literal_string(LogTemplate *self, const gchar *literal)
{
log_template_reset_compiled(self);
g_free(self->template_str);
self->template_str = g_strdup(literal);
self->compiled_template = g_list_append(self->compiled_template,
log_template_elem_new_macro(literal, M_NONE, NULL, 0));
/* double check that the representation here is actually considered trivial. It should be. */
g_assert(_calculate_if_trivial(self));
self->literal = TRUE;
self->trivial = TRUE;
}
void
log_template_set_escape(LogTemplate *self, gboolean enable)
{
self->escape = enable;
}
gboolean
log_template_set_type_hint(LogTemplate *self, const gchar *type_hint, GError **error)
{
g_return_val_if_fail(error == NULL || *error == NULL, FALSE);
gboolean result;
if (!type_hint)
{
self->explicit_type_hint = LM_VT_NONE;
result = TRUE;
}
else if (!type_hint_parse(type_hint, &self->explicit_type_hint, error))
{
self->explicit_type_hint = LM_VT_NONE;
result = FALSE;
}
else
{
result = TRUE;
}
self->type_hint = self->explicit_type_hint;
return result;
}
void
log_template_set_type_hint_value(LogTemplate *self, LogMessageValueType type_hint)
{
self->explicit_type_hint = self->type_hint = type_hint;
}
/* NOTE: we should completely get rid off the name property of templates,
* we basically use it at two locations:
*
* 1) dbparser uses it to store SyntheticMessages, there the "name" of a
* value is stored here
*
* 2) we reuse the LogTemplate structure (which represents a compiled
* template) to store a template {} statement in the configuration,
* which apart from a compiled template, also sports a name. This was
* the original reason the name attribute was introduced. This basically
* blends two unrelated purposes in the same struct.
*
* Other call sites pass a dummy value, which is probably never used in any
* meaningful way.
*
* Both usages and the dummy call-sites should be removed, and the entire
* thing replaced by another struct that contains a LogTemplate.
*
* I saw this to cause confusion numerous times already.
* --
* Bazsi.
*/
void
log_template_set_name(LogTemplate *self, const gchar *name)
{
if (self->name)
g_free(self->name);
self->name = g_strdup(name);
}
/* NOTE: the name parameter should not be used, please pass a NULL until it is eliminated */
LogTemplate *
log_template_new(GlobalConfig *cfg, const gchar *name)
{
LogTemplate *self = g_new0(LogTemplate, 1);
log_template_set_name(self, name);
g_atomic_counter_set(&self->ref_cnt, 1);
self->cfg = cfg;
if (cfg_is_config_version_older(cfg, VERSION_VALUE_4_0))
self->type_hint = LM_VT_STRING;
else
self->type_hint = LM_VT_NONE;
self->explicit_type_hint = LM_VT_NONE;
self->top_level = TRUE;
return self;
}
LogTemplate *
log_template_new_embedded(GlobalConfig *cfg)
{
LogTemplate *self = log_template_new(cfg, NULL);
self->top_level = FALSE;
return self;
}
static void
log_template_free(LogTemplate *self)
{
log_template_reset_compiled(self);
g_free(self->name);
g_free(self->template_str);
g_free(self);
}
LogTemplate *
log_template_ref(LogTemplate *s)
{
if (s)
g_atomic_counter_inc(&s->ref_cnt);
return s;
}
void
log_template_unref(LogTemplate *s)
{
if (s && g_atomic_counter_dec_and_test(&s->ref_cnt))
log_template_free(s);
}
/* NOTE: _init needs to be idempotent when called multiple times w/o invoking _destroy */
void
log_template_options_init(LogTemplateOptions *options, GlobalConfig *cfg)
{
gint i;
if (options->initialized)
return;
if (options->ts_format == -1)
options->ts_format = cfg->template_options.ts_format;
for (i = 0; i < LTZ_MAX; i++)
{
if (options->time_zone[i] == NULL)
options->time_zone[i] = g_strdup(cfg->template_options.time_zone[i]);
if (options->time_zone_info[i] == NULL)
options->time_zone_info[i] = time_zone_info_new(options->time_zone[i]);
}
if (options->frac_digits == -1)
options->frac_digits = cfg->template_options.frac_digits;
if (options->on_error == -1)
options->on_error = cfg->template_options.on_error;
options->use_fqdn = cfg->host_resolve_options.use_fqdn;
options->initialized = TRUE;
}
void
log_template_options_clone(LogTemplateOptions *source, LogTemplateOptions *dest)
{
dest->ts_format = source->ts_format;
for (gint i = 0; i < LTZ_MAX; i++)
{
if (source->time_zone[i])
dest->time_zone[i] = g_strdup(source->time_zone[i]);
}
dest->frac_digits = source->frac_digits;
dest->on_error = source->on_error;
dest->use_fqdn = source->use_fqdn;
/* NOTE: this still needs to be initialized by the owner as clone results
* in an uninitialized state. */
dest->initialized = FALSE;
}
void
log_template_options_destroy(LogTemplateOptions *options)
{
gint i;
for (i = 0; i < LTZ_MAX; i++)
{
if (options->time_zone[i])
g_free(options->time_zone[i]);
if (options->time_zone_info[i])
time_zone_info_free(options->time_zone_info[i]);
}
options->initialized = FALSE;
}
void
log_template_options_defaults(LogTemplateOptions *options)
{
memset(options, 0, sizeof(LogTemplateOptions));
options->frac_digits = -1;
options->ts_format = -1;
options->on_error = -1;
options->use_fqdn = FALSE;
}
void
log_template_options_global_defaults(LogTemplateOptions *options)
{
log_template_options_defaults(options);
options->ts_format = TS_FMT_BSD;
options->frac_digits = 0;
options->on_error = ON_ERROR_DROP_MESSAGE;
}
GQuark
log_template_error_quark(void)
{
return g_quark_from_static_string("log-template-error-quark");
}
gboolean
log_template_on_error_parse(const gchar *strictness, gint *out)
{
return on_error_parse(strictness, out);
}
void
log_template_options_set_on_error(LogTemplateOptions *options, gint on_error)
{
options->on_error = on_error;
}
EVTTAG *
evt_tag_template(const gchar *name, LogTemplate *template_obj, LogMessage *msg, LogTemplateEvalOptions *options)
{
/* trying to avoid scratch-buffers here, this is only meant to be used in trace messages */
GString *buf = g_string_sized_new(256);
log_template_format(template_obj, msg, options, buf);
EVTTAG *result = evt_tag_str(name, buf->str);
g_string_free(buf, TRUE);
return result;
}
|