File: msg-format.c

package info (click to toggle)
syslog-ng 4.8.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,456 kB
  • sloc: ansic: 177,631; python: 13,035; cpp: 11,611; makefile: 7,012; sh: 5,147; java: 3,651; xml: 3,344; yacc: 1,377; lex: 599; perl: 193; awk: 190; objc: 162
file content (343 lines) | stat: -rw-r--r-- 11,948 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright (c) 2002-2012 Balabit
 * Copyright (c) 1998-2012 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 "msg-format.h"
#include "cfg.h"
#include "plugin.h"
#include "plugin-types.h"
#include "find-crlf.h"
#include "scratch-buffers.h"
#include "utf8utils.h"
#include "hostname.h"

static gsize
_rstripped_message_length(const guchar *data, gsize length)
{
  while (length > 0 && (data[length - 1] == '\n' || data[length - 1] == '\0'))
    length--;
  return length;
}

static void
msg_format_inject_parse_error(MsgFormatOptions *options, LogMessage *msg, const guchar *data, gsize length,
                              gint problem_position)
{
  GString *buf = scratch_buffers_alloc();


  /* overwrite the message as if it was coming from syslog-ng */
  log_msg_clear(msg);

  msg->timestamps[LM_TS_STAMP] = msg->timestamps[LM_TS_RECVD];

  const gchar *hname = options->use_fqdn
                       ? get_local_hostname_fqdn()
                       : get_local_hostname_short();

  log_msg_set_value(msg, LM_V_HOST, hname, -1);

  if (problem_position > 0)
    g_string_printf(buf, "Error processing log message: %.*s>@<%.*s", (gint) problem_position-1,
                    data, (gint) (length-problem_position+1), data+problem_position-1);
  else
    g_string_printf(buf, "Error processing log message: %.*s", (gint) length, data);

  log_msg_set_value(msg, LM_V_MESSAGE, buf->str, buf->len);
  log_msg_set_value(msg, LM_V_PROGRAM, "syslog-ng", 9);
  g_string_printf(buf, "%d", (int) getpid());
  log_msg_set_value(msg, LM_V_PID, buf->str, buf->len);

  msg->flags |= LF_LOCAL;
  msg->pri = LOG_SYSLOG | LOG_ERR;
}

static void
msg_format_preprocess_message(MsgFormatOptions *options, LogMessage *msg,
                              const guchar *data, gsize length)
{
  if (options->flags & LP_STORE_RAW_MESSAGE)
    {
      log_msg_set_value(msg, LM_V_RAWMSG,
                        (gchar *) data, _rstripped_message_length(data, length));
    }
}

static void
msg_format_postprocess_message(MsgFormatOptions *options, LogMessage *msg,
                               const guchar *data, gsize length)
{
  if (options->flags & LP_NO_PARSE_DATE)
    {
      msg->timestamps[LM_TS_STAMP] = msg->timestamps[LM_TS_RECVD];
      unix_time_set_timezone(&msg->timestamps[LM_TS_STAMP],
                             time_zone_info_get_offset(options->recv_time_zone_info,
                                                       msg->timestamps[LM_TS_RECVD].ut_sec));
    }
  if (G_UNLIKELY(options->flags & LP_NO_MULTI_LINE))
    {
      gssize msg_len;
      gchar *msg_text;
      gchar *p;

      p = msg_text = (gchar *) log_msg_get_value(msg, LM_V_MESSAGE, &msg_len);
      while ((p = find_cr_or_lf_or_nul(p, msg_text + msg_len - p)))
        {
          *p = ' ';
          p++;
        }
    }
  if (options->flags & LP_LOCAL)
    msg->flags |= LF_LOCAL;
  if (options->flags & LP_ASSUME_UTF8)
    msg->flags |= LF_UTF8;
}

static gboolean
msg_format_process_message(MsgFormatOptions *options, LogMessage *msg,
                           const guchar *data, gsize length,
                           gsize *problem_position)
{
  if ((options->flags & LP_NOPARSE) == 0)
    {
      return options->format_handler->parse(options, msg, data, length, problem_position);
    }
  else
    {
      msg->pri = options->default_pri;

      log_msg_set_value_to_string(msg, LM_V_MSGFORMAT, "raw");
      if (options->flags & LP_SANITIZE_UTF8)
        {
          if (!g_utf8_validate((gchar *) data, length, NULL))
            {
              gchar buf[SANITIZE_UTF8_BUFFER_SIZE(length)];
              gsize sanitized_length;
              optimized_sanitize_utf8_to_escaped_binary(data, length, &sanitized_length, buf, sizeof(buf));
              log_msg_set_value(msg, LM_V_MESSAGE, buf, _rstripped_message_length((guchar *) buf, sanitized_length));
              log_msg_set_tag_by_id(msg, LM_T_MSG_UTF8_SANITIZED);
              msg->flags |= LF_UTF8;
              return TRUE;
            }
          else
            msg->flags |= LF_UTF8;
        }
      else if ((options->flags & LP_VALIDATE_UTF8) && g_utf8_validate((gchar *) data, length, NULL))
        msg->flags |= LF_UTF8;

      log_msg_set_value(msg, LM_V_MESSAGE, (gchar *) data, _rstripped_message_length(data, length));
      return TRUE;
    }
}

gboolean
msg_format_try_parse_into(MsgFormatOptions *options, LogMessage *msg,
                          const guchar *data, gsize length,
                          gsize *problem_position)
{
  if (G_UNLIKELY(!options->format_handler))
    {
      gchar buf[256];

      g_snprintf(buf, sizeof(buf), "Error parsing message, format module %s is not loaded", options->format);
      log_msg_set_value(msg, LM_V_MESSAGE, buf, -1);
      return FALSE;
    }

  msg_format_preprocess_message(options, msg, data, length);

  if (!msg_format_process_message(options, msg, data, length, problem_position))
    return FALSE;

  msg_format_postprocess_message(options, msg, data, length);
  return TRUE;
}

void
msg_format_parse_into(MsgFormatOptions *options, LogMessage *msg,
                      const guchar *data, gsize length)
{
  gsize problem_position = 0;

  if (!msg_format_try_parse_into(options, msg, data, length, &problem_position))
    {
      if (options->flags & LP_PIGGYBACK_ERRORS)
        msg_format_inject_parse_error(options, msg, data, _rstripped_message_length(data, length), problem_position);
      else
        log_msg_set_value(msg, LM_V_MESSAGE, (gchar *) data, length);

      /* the injected error message needs to be postprocessed too */
      msg_format_postprocess_message(options, msg, data, length);

      gchar buf[256];
      gsize len = g_snprintf(buf, sizeof(buf), "%s-error", options->format);
      log_msg_set_value(msg, LM_V_MSGFORMAT, buf, len);
    }
}

static gsize
_determine_payload_size(MsgFormatOptions *parse_options, const guchar *data, gsize length)
{
  gsize payload_size;

  if ((parse_options->flags & LP_STORE_RAW_MESSAGE))
    payload_size = length * 4;
  else
    payload_size = length * 2;

  return MAX(payload_size, 256);
}

LogMessage *
msg_format_construct_message(MsgFormatOptions *options, const guchar *data, gsize length)
{
  LogMessage *msg = log_msg_sized_new(_determine_payload_size(options, data, length));
  return msg;
}

LogMessage *
msg_format_parse(MsgFormatOptions *options, const guchar *data, gsize length)
{
  LogMessage *msg = msg_format_construct_message(options, data, length);

  msg_trace("Initial message parsing follows");
  msg_format_parse_into(options, msg, data, length);
  return msg;
}

gboolean
msg_format_options_set_sdata_prefix(MsgFormatOptions *options, const gchar *prefix)
{
  if (prefix && strlen(prefix) > 128)
    return FALSE;

  g_free(options->sdata_prefix);
  options->sdata_prefix = g_strdup(prefix);
  return TRUE;
}

void
msg_format_options_defaults(MsgFormatOptions *options)
{
  options->flags = LP_EXPECT_HOSTNAME | LP_STORE_LEGACY_MSGHDR | LP_PIGGYBACK_ERRORS;
  options->recv_time_zone = NULL;
  options->recv_time_zone_info = NULL;
  options->bad_hostname = NULL;
  options->default_pri = 0xFFFF;
  options->sdata_param_value_max = 65535;
  options->sdata_prefix = NULL;
  options->sdata_prefix_len = 0;
}

/* NOTE: _init needs to be idempotent when called multiple times w/o invoking _destroy */
void
msg_format_options_init(MsgFormatOptions *options, GlobalConfig *cfg)
{
  Plugin *p;

  if (options->initialized)
    return;

  if (cfg->bad_hostname_compiled)
    options->bad_hostname = &cfg->bad_hostname;
  if (options->recv_time_zone == NULL)
    options->recv_time_zone = g_strdup(cfg->recv_time_zone);
  if (options->recv_time_zone_info == NULL)
    options->recv_time_zone_info = time_zone_info_new(options->recv_time_zone);

  if (!options->format)
    options->format = g_strdup("syslog");

  p = cfg_find_plugin(cfg, LL_CONTEXT_FORMAT, options->format);
  if (p)
    options->format_handler = plugin_construct(p);

  if (!options->sdata_prefix)
    options->sdata_prefix = g_strdup(logmsg_sd_prefix);
  options->sdata_prefix_len = strlen(options->sdata_prefix);
  options->use_fqdn = cfg->host_resolve_options.use_fqdn;
  options->initialized = TRUE;
}

void
msg_format_options_copy(MsgFormatOptions *options, const MsgFormatOptions *source)
{
  g_assert(!options->initialized);

  options->format = g_strdup(source->format);
  options->flags = source->flags;
  options->default_pri = source->default_pri;
  options->recv_time_zone = g_strdup(source->recv_time_zone);
  options->sdata_param_value_max = source->sdata_param_value_max;
  options->sdata_prefix = g_strdup(source->sdata_prefix);
}

void
msg_format_options_destroy(MsgFormatOptions *options)
{
  if (options->format)
    {
      g_free(options->format);
      options->format = NULL;
    }
  if (options->recv_time_zone)
    {
      g_free(options->recv_time_zone);
      options->recv_time_zone = NULL;
    }
  if (options->recv_time_zone_info)
    {
      time_zone_info_free(options->recv_time_zone_info);
      options->recv_time_zone_info = NULL;
    }
  g_free(options->sdata_prefix);
  options->initialized = FALSE;
}

CfgFlagHandler msg_format_flag_handlers[] =
{
  { "no-parse",                   CFH_SET, offsetof(MsgFormatOptions, flags), LP_NOPARSE },
  { "check-hostname",             CFH_SET, offsetof(MsgFormatOptions, flags), LP_CHECK_HOSTNAME },
  { "syslog-protocol",            CFH_SET, offsetof(MsgFormatOptions, flags), LP_SYSLOG_PROTOCOL },
  { "assume-utf8",                CFH_SET, offsetof(MsgFormatOptions, flags), LP_ASSUME_UTF8 },
  { "validate-utf8",              CFH_SET, offsetof(MsgFormatOptions, flags), LP_VALIDATE_UTF8 },
  { "sanitize-utf8",              CFH_SET, offsetof(MsgFormatOptions, flags), LP_SANITIZE_UTF8 },
  { "no-multi-line",              CFH_SET, offsetof(MsgFormatOptions, flags), LP_NO_MULTI_LINE },
  { "store-legacy-msghdr",        CFH_SET, offsetof(MsgFormatOptions, flags), LP_STORE_LEGACY_MSGHDR },
  { "store-raw-message",          CFH_SET, offsetof(MsgFormatOptions, flags), LP_STORE_RAW_MESSAGE },
  { "dont-store-legacy-msghdr", CFH_CLEAR, offsetof(MsgFormatOptions, flags), LP_STORE_LEGACY_MSGHDR },
  { "expect-hostname",            CFH_SET, offsetof(MsgFormatOptions, flags), LP_EXPECT_HOSTNAME },
  { "no-hostname",              CFH_CLEAR, offsetof(MsgFormatOptions, flags), LP_EXPECT_HOSTNAME },
  { "guess-timezone",             CFH_SET, offsetof(MsgFormatOptions, flags), LP_GUESS_TIMEZONE },
  { "no-header",                  CFH_SET, offsetof(MsgFormatOptions, flags), LP_NO_HEADER },
  { "no-rfc3164-fallback",        CFH_SET, offsetof(MsgFormatOptions, flags), LP_NO_RFC3164_FALLBACK },
  { "piggyback-errors",           CFH_SET, offsetof(MsgFormatOptions, flags), LP_PIGGYBACK_ERRORS },
  { "no-piggyback-errors",      CFH_CLEAR, offsetof(MsgFormatOptions, flags), LP_PIGGYBACK_ERRORS },
  { NULL },
};

gboolean
msg_format_options_process_flag(MsgFormatOptions *options, const gchar *flag)
{
  return cfg_process_flag(msg_format_flag_handlers, options, flag);
}