File: syslog-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 (1197 lines) | stat: -rw-r--r-- 34,788 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
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
/*
 * Copyright (c) 2002-2012 Balabit
 * Copyright (c) 1998-2012 Balázs Scheidler
 *
 * 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 "syslog-format.h"
#include "timeutils/scan-timestamp.h"
#include "timeutils/conv.h"
#include "logmsg/logmsg.h"
#include "messages.h"
#include "timeutils/cache.h"
#include "find-crlf.h"
#include "cfg.h"
#include "str-format.h"
#include "utf8utils.h"
#include "str-utils.h"
#include "syslog-names.h"

#include "logproto/logproto.h"

#include <regex.h>
#include <ctype.h>
#include <string.h>

#define SD_NAME_SIZE 256

static const char aix_fwd_string[] = "Message forwarded from ";
static const char repeat_msg_string[] = "last message repeated";
static struct
{
  gboolean initialized;
  NVHandle is_synced;
  NVHandle cisco_seqid;
} handles;

static inline gboolean
_skip_char(const guchar **data, gint *left)
{
  if (*left < 1)
    return FALSE;

  (*data)++;
  (*left)--;

  return TRUE;
}

static gint
_skip_chars(const guchar **data, gint *length, const gchar *chars, gint max_len)
{
  const guchar *src = *data;
  gint left = *length;
  gint num_skipped = 0;

  while (max_len && left && _strchr_optimized_for_single_char_haystack(chars, *src))
    {
      _skip_char(&src, &left);
      num_skipped++;
      if (max_len >= 0)
        max_len--;
    }
  *data = src;
  *length = left;
  return num_skipped;
}

static gboolean
_skip_space(const guchar **data, gint *length)
{
  const guchar *src = *data;
  gint left = *length;

  if (left > 0 && *src == ' ')
    {
      _skip_char(&src, &left);
    }
  else
    {
      return FALSE;
    }

  *data = src;
  *length = left;
  return TRUE;
}

static gint
_skip_chars_until(const guchar **data, gint *length, const gchar *delims)
{
  const guchar *src = *data;
  gint left = *length;
  gint num_skipped = 0;

  while (left && _strchr_optimized_for_single_char_haystack(delims, *src) == 0)
    {
      _skip_char(&src, &left);
      num_skipped++;
    }
  *data = src;
  *length = left;
  return num_skipped;
}

static gboolean
_syslog_format_parse_pri(LogMessage *msg, const guchar **data, gint *length, guint flags, guint16 default_pri)
{
  int pri;
  gboolean success = TRUE;
  const guchar *src = *data;
  gint left = *length;

  if (left && src[0] == '<')
    {
      _skip_char(&src, &left);
      pri = 0;
      while (left && *src != '>')
        {
          if (isdigit(*src))
            {
              pri = pri * 10 + ((*src) - '0');
            }
          else
            {
              return FALSE;
            }
          _skip_char(&src, &left);
        }
      msg->pri = pri;
      if (left)
        {
          _skip_char(&src, &left);
        }
    }
  /* No priority info in the buffer? Just assign a default. */
  else
    {
      msg->pri = default_pri != 0xFFFF ? default_pri : (EVT_FAC_USER | EVT_PRI_NOTICE);
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_MISSING_PRI);
    }

  *data = src;
  *length = left;
  return success;
}

static void
_syslog_format_parse_column(LogMessage *msg, NVHandle handle, const guchar **data, gint *length, gint max_length)
{
  const guchar *src, *space;
  gint left;

  src = *data;
  left = *length;
  space = memchr(src, ' ', left);
  if (space)
    {
      left -= space - src;
      src = space;
    }
  else
    {
      src = src + left;
      left = 0;
    }
  if (left)
    {
      if ((*length - left) > 1 || (*data)[0] != '-')
        {
          gint len = (*length - left) > max_length ? max_length : (*length - left);
          log_msg_set_value(msg, handle, (gchar *) *data, len);
        }
    }
  *data = src;
  *length = left;
}

static void
_syslog_format_parse_cisco_sequence_id(LogMessage *msg, const guchar **data, gint *length)
{
  const guchar *src = *data;
  gint left = *length;

  while (left && *src != ':')
    {
      if (!isdigit(*src))
        return;
      if (!_skip_char(&src, &left))
        return;
    }
  if (!_skip_char(&src, &left))
    return;

  /* if the next char is not space, then we may try to read a date */

  if (!left || *src != ' ')
    return;

  log_msg_set_value(msg, handles.cisco_seqid, (gchar *) *data, *length - left - 1);

  *data = src;
  *length = left;
  return;
}

static void
_syslog_format_parse_cisco_timestamp_attributes(LogMessage *msg, const guchar **data, gint *length, gint parse_flags)
{
  const guchar *src = *data;
  gint left = *length;

  if (!left)
    return;

  /* Cisco timestamp extensions, the first '*' indicates that the clock is
   * unsynced, '.' if it is known to be synced */
  if (G_UNLIKELY(src[0] == '*'))
    {
      if (!(parse_flags & LP_NO_PARSE_DATE))
        log_msg_set_value(msg, handles.is_synced, "0", 1);
      _skip_char(&src, &left);
    }
  else if (G_UNLIKELY(src[0] == '.'))
    {
      if (!(parse_flags & LP_NO_PARSE_DATE))
        log_msg_set_value(msg, handles.is_synced, "1", 1);
      _skip_char(&src, &left);
    }
  *data = src;
  *length = left;
}

static gboolean
_syslog_format_parse_timestamp(LogMessage *msg, UnixTime *stamp,
                               const guchar **data, gint *length,
                               guint parse_flags, glong recv_timezone_ofs)
{
  gboolean result;
  WallClockTime wct = WALL_CLOCK_TIME_INIT;

  if ((parse_flags & LP_SYSLOG_PROTOCOL) == 0)
    result = scan_rfc3164_timestamp(data, length, &wct);
  else
    {
      if (G_UNLIKELY(*length >= 1 && (*data)[0] == '-'))
        {
          log_msg_set_tag_by_id(msg, LM_T_SYSLOG_MISSING_TIMESTAMP);
          unix_time_set_now(stamp);
          (*data)++;
          (*length)--;
          return TRUE;
        }
      result = scan_rfc5424_timestamp(data, length, &wct);
    }

  if ((parse_flags & LP_NO_PARSE_DATE) == 0)
    {
      convert_and_normalize_wall_clock_time_to_unix_time_with_tz_hint(&wct, stamp, recv_timezone_ofs);

      if ((parse_flags & LP_GUESS_TIMEZONE) != 0)
        unix_time_fix_timezone_assuming_the_time_matches_real_time(stamp);
    }

  return result;
}

static gboolean
_syslog_format_parse_date(LogMessage *msg, const guchar **data, gint *length, guint parse_flags,
                          glong recv_timezone_ofs)
{
  UnixTime *stamp = &msg->timestamps[LM_TS_STAMP];

  unix_time_unset(stamp);
  if (!_syslog_format_parse_timestamp(msg, stamp, data, length, parse_flags, recv_timezone_ofs))
    {
      *stamp = msg->timestamps[LM_TS_RECVD];
      unix_time_set_timezone(stamp, recv_timezone_ofs);
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_MISSING_TIMESTAMP);
      return FALSE;
    }

  return TRUE;
}

static gboolean
_syslog_format_parse_version(LogMessage *msg, const guchar **data, gint *length)
{
  const guchar *src = *data;
  gint left = *length;
  gint version = 0;

  while (left && *src != ' ')
    {
      if (isdigit(*src))
        {
          version = version * 10 + ((*src) - '0');
        }
      else
        {
          return FALSE;
        }
      _skip_char(&src, &left);
    }
  if (version != 1)
    return FALSE;

  *data = src;
  *length = left;
  return TRUE;
}

static void
_syslog_format_parse_legacy_program_name(LogMessage *msg, const guchar **data, gint *length, guint flags)
{
  /* the data pointer will not change */
  const guchar *src, *prog_start;
  gint left;

  src = *data;
  left = *length;
  prog_start = src;
  while (left && *src != ' ' && *src != '[' && *src != ':')
    {
      _skip_char(&src, &left);
    }
  log_msg_set_value(msg, LM_V_PROGRAM, (gchar *) prog_start, src - prog_start);
  if (left > 0 && *src == '[')
    {
      const guchar *pid_start = src + 1;
      while (left && *src != ' ' && *src != ']' && *src != ':')
        {
          _skip_char(&src, &left);
        }
      if (left)
        {
          log_msg_set_value(msg, LM_V_PID, (gchar *) pid_start, src - pid_start);
        }
      if (left > 0 && *src == ']')
        {
          _skip_char(&src, &left);
        }
    }
  if (left > 0 && *src == ':')
    {
      _skip_char(&src, &left);
    }
  if (left > 0 && *src == ' ')
    {
      _skip_char(&src, &left);
    }
  if ((flags & LP_STORE_LEGACY_MSGHDR))
    {
      log_msg_set_value(msg, LM_V_LEGACY_MSGHDR, (gchar *) *data, *length - left);
    }
  *data = src;
  *length = left;
}

static guint8 invalid_chars[32];

static void
_init_parse_hostname_invalid_chars(void)
{
  if ((invalid_chars[0] & 0x1) == 0)
    {
      gint i;
      /* we use a bit string to represent valid/invalid characters  when check_hostname is enabled */

      /* not yet initialized */
      for (i = 0; i < 256; i++)
        {
          if (!((i >= 'A' && i <= 'Z') ||
                (i >= 'a' && i <= 'z') ||
                (i >= '0' && i <= '9') ||
                i == '-' || i == '_' ||
                i == '.' || i == ':' ||
                i == '@' || i == '/'))
            {
              invalid_chars[i / 8] |= 1 << (i % 8);
            }
        }
      invalid_chars[0] |= 0x1;
    }
}

static inline gboolean
_is_invalid_hostname_char(guchar c)
{
  return invalid_chars[c / 8] & (1 << (c % 8));
}

typedef struct _IPv6Heuristics
{
  gint8 current_segment;
  gint8 digits_in_segment;
  gboolean heuristic_failed;
} IPv6Heuristics;

static gboolean
ipv6_heuristics_feed_gchar(IPv6Heuristics *self, gchar c)
{
  if (self->heuristic_failed)
    return FALSE;

  if (c != ':' && !g_ascii_isxdigit(c))
    {
      self->heuristic_failed = TRUE;
      return FALSE;
    }

  if (g_ascii_isxdigit(c))
    {
      if (++self->digits_in_segment > 4)
        {
          self->heuristic_failed = TRUE;
          return FALSE;
        }
    }

  if (c == ':')
    {
      self->digits_in_segment = 0;
      if (++self->current_segment >= 8)
        {
          self->heuristic_failed = TRUE;
          return FALSE;
        }
    }

  return TRUE;
}

static void
_syslog_format_parse_hostname(LogMessage *msg, const guchar **data, gint *length,
                              const guchar **hostname_start, int *hostname_len,
                              guint flags, regex_t *bad_hostname)
{
  /* FIXME: support nil value support  with new protocol*/
  const guchar *src, *oldsrc;
  gint left, oldleft;
  gchar hostname_buf[256];
  gint dst = 0;

  IPv6Heuristics ipv6_heuristics = {0};

  src = *data;
  left = *length;

  /* If we haven't already found the original hostname,
     look for it now. */

  oldsrc = src;
  oldleft = left;

  while (left && *src != ' ' && *src != '[' && dst < sizeof(hostname_buf) - 1)
    {
      ipv6_heuristics_feed_gchar(&ipv6_heuristics, *src);

      if (*src == ':' && ipv6_heuristics.heuristic_failed)
        {
          break;
        }

      if (G_UNLIKELY((flags & LP_CHECK_HOSTNAME) && _is_invalid_hostname_char(*src)))
        {
          break;
        }
      hostname_buf[dst++] = *src;
      _skip_char(&src, &left);
    }
  hostname_buf[dst] = 0;

  if (left && *src == ' ' &&
      (!bad_hostname || regexec(bad_hostname, hostname_buf, 0, NULL, 0)))
    {
      /* This was a hostname. It came from a
         syslog-ng, since syslogd doesn't send
         hostnames. It's even better then the one
         we got from the AIX fwd message, if we
         did. */
      *hostname_start = oldsrc;
      *hostname_len = oldleft - left;
    }
  else
    {
      *hostname_start = NULL;
      *hostname_len = 0;

      src = oldsrc;
      left = oldleft;
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_INVALID_HOSTNAME);
    }

  if (*hostname_len > 255)
    *hostname_len = 255;

  *data = src;
  *length = left;
}

/**
 * _syslog_format_parse:
 * @msg: LogMessage instance to store parsed information into
 * @data: message
 * @length: length of the message pointed to by @data
 * @flags: value affecting how the message is parsed (bits from LP_*)
 *
 * Parse an http://www.syslog.cc/ietf/drafts/draft-ietf-syslog-protocol-23.txt formatted log
 * message for structured data elements and store the parsed information
 * in @msg.values and dup the SD string. Parsing is affected by the bits set @flags argument.
 **/
gboolean
_syslog_format_parse_sd(LogMessage *msg, const guchar **data, gint *length, const MsgFormatOptions *options)
{
  /*
   * STRUCTURED-DATA = NILVALUE / 1*SD-ELEMENT
   * SD-ELEMENT      = "[" SD-ID *(SP SD-PARAM) "]"
   * SD-PARAM        = PARAM-NAME "=" %d34 PARAM-VALUE %d34
   * SD-ID           = SD-NAME
   * PARAM-NAME      = SD-NAME
   * PARAM-VALUE     = UTF-8-STRING ; characters '"', '\' and
   *                                ; ']' MUST be escaped.
   * SD-NAME         = 1*32PRINTUSASCII ; except '=', SP, ']', %d34 (")
   *
   * Example Structured Data string:
   *
   *   [exampleSDID@0 iut="3" eventSource="Application" eventID="1011"][examplePriority@0 class="high"]
   *
   * NOTE: To increase compatibility, we modified the parser to accept longer than 32 character
   * SD-ID's and SD-PARAM's. However there is still a practical limit (255 characters) for the
   * values, since currently NVPairs can not store longer ID's.
   */

  gboolean ret = FALSE;
  const guchar *src = *data;
  /* ASCII string */
  gchar sd_id_name[SD_NAME_SIZE];
  gsize sd_id_len;
  gchar sd_param_name[SD_NAME_SIZE];

  /* UTF-8 string */
  gchar sd_param_value[options->sdata_param_value_max + 1];
  gsize sd_param_value_len;
  gchar sd_value_name[SD_NAME_SIZE];

  g_assert(options->sdata_prefix_len < SD_NAME_SIZE);

  guint open_sd = 0;
  gint left = *length, pos;

  if (left && src[0] == '-')
    {
      /* Nothing to do here */
      _skip_char(&src, &left);
    }
  else if (left && src[0] == '[')
    {
      _skip_char(&src, &left);
      open_sd++;
      do
        {
          if (!left || !isascii(*src) || *src == '=' || *src == ' ' || *src == ']' || *src == '"')
            goto error;
          /* read sd_id */
          pos = 0;
          while (left && *src != ' ' && *src != ']')
            {
              if (pos < sizeof(sd_id_name) - 1 - options->sdata_prefix_len)
                {
                  if (isascii(*src) && *src != '=' && *src != ' ' && *src != ']' && *src != '"')
                    {
                      sd_id_name[pos] = *src;
                      pos++;
                    }
                  else
                    {
                      goto error;
                    }
                }
              else
                {
                  goto error;
                }
              _skip_char(&src, &left);
            }

          if (pos == 0)
            goto error;

          sd_id_name[pos] = 0;
          sd_id_len = pos;
          strcpy(sd_value_name, options->sdata_prefix);
          g_strlcpy(sd_value_name + options->sdata_prefix_len, sd_id_name, sizeof(sd_value_name) - options->sdata_prefix_len);

          if (left && *src == ']')
            {
              log_msg_set_value_by_name(msg, sd_value_name, "", 0);
            }
          else
            {
              if (options->sdata_prefix_len + pos + 1 >= sizeof(sd_value_name))
                goto error;

              sd_value_name[options->sdata_prefix_len + pos] = '.';
              sd_value_name[options->sdata_prefix_len + pos + 1] = 0;
            }

          g_assert(sd_id_len < sizeof(sd_param_name));

          /* read sd-element */
          while (left && *src != ']')
            {
              if (left && *src == ' ') /* skip the ' ' before the parameter name */
                _skip_char(&src, &left);
              else
                goto error;

              if (!left || !isascii(*src) || *src == '=' || *src == ' ' || *src == ']' || *src == '"')
                goto error;

              /* read sd-param */
              pos = 0;
              while (left && *src != '=')
                {
                  if (pos < sizeof(sd_param_name) - 1 - sd_id_len)
                    {
                      if (isascii(*src) && *src != '=' && *src != ' ' && *src != ']' && *src != '"')
                        {
                          sd_param_name[pos] = *src;
                          pos++;
                        }
                      else
                        goto error;
                    }
                  else
                    {
                      goto error;
                    }
                  _skip_char(&src, &left);
                }
              sd_param_name[pos] = 0;
              gsize sd_param_name_len = g_strlcpy(&sd_value_name[options->sdata_prefix_len + 1 + sd_id_len],
                                                  sd_param_name,
                                                  sizeof(sd_value_name) - options->sdata_prefix_len - 1 - sd_id_len);

              if (sd_param_name_len >= sizeof(sd_value_name) - options->sdata_prefix_len - 1 - sd_id_len)
                goto error;

              if (left && *src == '=')
                _skip_char(&src, &left);
              else
                goto error;

              /* read sd-param-value */

              if (left && *src == '"')
                {
                  gboolean quote = FALSE;
                  /* opening quote */
                  _skip_char(&src, &left);
                  pos = 0;

                  while (left && (*src != '"' || quote))
                    {
                      if (!quote && *src == '\\')
                        {
                          quote = TRUE;
                        }
                      else
                        {
                          if (quote && *src != '"' && *src != ']' && *src != '\\' && pos < sizeof(sd_param_value) - 1)
                            {
                              sd_param_value[pos] = '\\';
                              pos++;
                            }
                          else if (!quote &&  *src == ']')
                            {
                              _skip_char(&src, &left);
                              goto error;
                            }
                          if (pos < sizeof(sd_param_value) - 1)
                            {
                              sd_param_value[pos] = *src;
                              pos++;
                            }
                          quote = FALSE;
                        }
                      _skip_char(&src, &left);
                    }
                  sd_param_value[pos] = 0;
                  sd_param_value_len = pos;

                  if (left && *src == '"')/* closing quote */
                    _skip_char(&src, &left);
                  else
                    goto error;
                }
              else if (left)
                {
                  pos = 0;

                  while (left && (*src != ' ' && *src != ']'))
                    {
                      if (pos < sizeof(sd_param_value) - 1)
                        {
                          sd_param_value[pos] = *src;
                          pos++;
                        }
                      _skip_char(&src, &left);
                    }
                  sd_param_value[pos] = 0;
                  sd_param_value_len = pos;

                }
              else
                {
                  goto error;
                }

              log_msg_set_value_by_name(msg, sd_value_name, sd_param_value, sd_param_value_len);
            }

          if (left && *src == ']')
            {
              _skip_char(&src, &left);
              open_sd--;
            }
          else
            {
              goto error;
            }

          /* if any other sd then continue*/
          if (left && *src == '[')
            {
              /* new structured data begins, thus continue iteration */
              _skip_char(&src, &left);
              open_sd++;
            }
        }
      while (left && open_sd != 0);
    }
  else
    {
      goto error;
    }
  ret = TRUE;
error:
  /* FIXME: what happens if an error occurs? there's no way to return a
   * failure from here, but nevertheless we should do something sane, e.g.
   * don't parse the SD string, but skip to the end so that the $MSG
   * contents are correctly parsed. */

  *data = src;
  *length = left;
  return ret;
}

gboolean
_syslog_format_parse_sd_column(LogMessage *msg, const guchar **data, gint *length, const MsgFormatOptions *options)
{
  if (*length == 0)
    return TRUE;

  guchar first_char = (*data)[0];
  if (first_char == '-' || first_char == '[')
    return _syslog_format_parse_sd(msg, data, length, options);

  /* the SDATA block is not there, skip parsing it as this is how we have
   * processed SDATA blocks since we added RFC5424.  This is more forgiving
   * than strict RFC5424 but apps have a bad history of conforming to it
   * anyway.  */
  return TRUE;
}

gboolean
_syslog_format_parse_message_column(LogMessage *msg,
                                    const guchar **data, gint *length,
                                    const MsgFormatOptions *parse_options)
{
  const guchar *src = (guchar *) *data;
  gint left = *length;

  /* checking if there are remaining data in log message */
  if (left != 0)
    {
      /* optional part of the log message [SP MSG] */
      if (!_skip_space(&src, &left))
        {
          return FALSE;
        }

      if (left >= 3 && memcmp(src, "\xEF\xBB\xBF", 3) == 0)
        {
          /* we have a BOM, this is UTF8 */
          msg->flags |= LF_UTF8;
          src += 3;
          left -= 3;

          log_msg_set_value(msg, LM_V_MESSAGE, (gchar *) src, left);
          return TRUE;
        }

      if ((parse_options->flags & LP_SANITIZE_UTF8))
        {
          if (!g_utf8_validate((gchar *) src, left, NULL))
            {
              gchar buf[SANITIZE_UTF8_BUFFER_SIZE(left)];
              gsize sanitized_length;
              optimized_sanitize_utf8_to_escaped_binary(src, left, &sanitized_length, buf, sizeof(buf));
              log_msg_set_value(msg, LM_V_MESSAGE, 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 ((parse_options->flags & LP_VALIDATE_UTF8) && g_utf8_validate((gchar *) src, left, NULL))
        msg->flags |= LF_UTF8;
    }
  log_msg_set_value(msg, LM_V_MESSAGE, (gchar *) src, left);
  return TRUE;
}

static gboolean
_syslog_format_parse_legacy_header(LogMessage *msg, const guchar **data, gint *length,
                                   const MsgFormatOptions *parse_options)
{
  const guchar *src = *data;
  gint left = *length;
  time_t now;

  _syslog_format_parse_cisco_sequence_id(msg, &src, &left);
  _skip_chars(&src, &left, " ", -1);
  _syslog_format_parse_cisco_timestamp_attributes(msg, &src, &left, parse_options->flags);

  now = get_cached_realtime_sec();
  if (_syslog_format_parse_date(msg, &src, &left, parse_options->flags & ~LP_SYSLOG_PROTOCOL,
                                time_zone_info_get_offset(parse_options->recv_time_zone_info, now)))
    {
      /* Expected format: hostname program[pid]: */
      /* Possibly: Message forwarded from hostname: ... */
      const guchar *hostname_start = NULL;
      int hostname_len = 0;

      _skip_chars(&src, &left, " ", -1);

      /* Detect funny AIX syslogd forwarded message. */
      if (G_UNLIKELY(left >= (sizeof(aix_fwd_string) - 1) &&
                     !memcmp(src, aix_fwd_string, sizeof(aix_fwd_string) - 1)))
        {
          src += sizeof(aix_fwd_string) - 1;
          left -= sizeof(aix_fwd_string) - 1;
          hostname_start = src;
          hostname_len = _skip_chars_until(&src, &left, ":");
          _skip_chars(&src, &left, " :", -1);
        }

      /* Now, try to tell if it's a "last message repeated" line */
      if (G_UNLIKELY(left >= sizeof(repeat_msg_string) &&
                     !memcmp(src, repeat_msg_string, sizeof(repeat_msg_string) - 1)))
        {
          ;     /* It is. Do nothing since there's no hostname or program name coming. */
        }
      else
        {
          if (!hostname_start && (parse_options->flags & LP_EXPECT_HOSTNAME))
            {
              /* Don't parse a hostname if it is local */
              /* It's a regular ol' message. */
              _syslog_format_parse_hostname(msg, &src, &left, &hostname_start, &hostname_len, parse_options->flags,
                                            parse_options->bad_hostname);

              /* Skip whitespace. */
              _skip_chars(&src, &left, " ", -1);
            }

          /* Try to extract a program name */
          _syslog_format_parse_legacy_program_name(msg, &src, &left, parse_options->flags);
        }

      /* If we did manage to find a hostname, store it. */
      if (hostname_start)
        {
          log_msg_set_value(msg, LM_V_HOST, (gchar *) hostname_start, hostname_len);
        }
    }
  else
    {
      /* no timestamp, format is expected to be "program[pid] message" */
      /* Different format */

      /* A kernel message? Use 'kernel' as the program name. */
      if (((msg->pri & SYSLOG_FACMASK) == LOG_KERN && (parse_options->flags & LP_LOCAL) != 0))
        {
          log_msg_set_value(msg, LM_V_PROGRAM, "kernel", 6);
        }
      /* No, not a kernel message. */
      else
        {
          log_msg_set_tag_by_id(msg, LM_T_SYSLOG_RFC3164_MISSING_HEADER);
          /* Capture the program name */
          _syslog_format_parse_legacy_program_name(msg, &src, &left, parse_options->flags);
        }
    }
  *data = src;
  *length = left;
  return TRUE;
}

/* validate that we did not receive an RFC5425 style octet count, which
 * should have already been processed by the time we got here, unless the
 * transport is incorrectly configured */
static void
_syslog_format_check_framing(LogMessage *msg, const guchar **data, gint *length)
{
  const guchar *src = *data;
  gint left = *length;
  gint i = 0;

  while (left > 0 && isdigit(*src))
    {
      if (!_skip_char(&src, &left))
        return;

      i++;

      if (i > RFC6587_MAX_FRAME_LEN_DIGITS)
        return;
    }

  if (i == 0 || *src != ' ')
    return;

  /* we did indeed find a series of digits that look like framing, that's
   * probably not what was intended. */
  msg_debug("RFC5425 style octet count was found at the start of the message, this is probably not what was intended",
            evt_tag_mem("data", data, src - (*data)),
            evt_tag_msg_reference(msg));
  log_msg_set_tag_by_id(msg, LM_T_SYSLOG_UNEXPECTED_FRAMING);
  *data = src;
  *length = left;
}

static void
_syslog_format_parse_legacy_message(LogMessage *msg,
                                    const guchar **data, gint *length,
                                    const MsgFormatOptions *parse_options)
{
  const guchar *src = (const guchar *) *data;
  gint left = *length;

  if (parse_options->flags & LP_SANITIZE_UTF8)
    {
      if (!g_utf8_validate((gchar *) src, left, NULL))
        {
          /* invalid utf8, sanitize it and then remember it is now utf8 clean */
          gchar buf[SANITIZE_UTF8_BUFFER_SIZE(left)];
          gsize sanitized_length;
          optimized_sanitize_utf8_to_escaped_binary(src, left, &sanitized_length, buf, sizeof(buf));
          log_msg_set_value(msg, LM_V_MESSAGE, buf, sanitized_length);
          log_msg_set_tag_by_id(msg, LM_T_MSG_UTF8_SANITIZED);
          msg->flags |= LF_UTF8;
          return;
        }
      else
        {
          /* valid utf8, no need to sanitize, store it and mark it as utf8 clean */
          msg->flags |= LF_UTF8;
        }
    }
  else if ((parse_options->flags & LP_VALIDATE_UTF8) && g_utf8_validate((gchar *) src, left, NULL))
    {
      /* valid utf8, mark it as utf8 clean */
      msg->flags |= LF_UTF8;
    }

  log_msg_set_value(msg, LM_V_MESSAGE, (gchar *) src, left);
}

/**
 * _syslog_format_parse_legacy:
 * @msg: LogMessage instance to store parsed information into
 * @data: message
 * @length: length of the message pointed to by @data
 * @flags: value affecting how the message is parsed (bits from LP_*)
 *
 * Parse an RFC3164 formatted log message and store the parsed information
 * in @msg. Parsing is affected by the bits set @flags argument.
 *
 * This parser is _very_ forgiving, it basically accepts anything any device
 * would barf on the line.
 **/
static gboolean
_syslog_format_parse_legacy(const MsgFormatOptions *parse_options,
                            const guchar *data, gint length,
                            LogMessage *msg, gsize *position)
{
  const guchar *src;
  gint left;

  src = (const guchar *) data;
  left = length;

  _syslog_format_check_framing(msg, &src, &left);
  if (!_syslog_format_parse_pri(msg, &src, &left, parse_options->flags, parse_options->default_pri))
    {
      /* invalid <pri> value, that's really difficult to do, as it needs to
       * start with an opening bracket and then no number OR no closing bracket
       * follows. A missing <pri> value would be accepted.
       *
       * This is a very rare case, but it's best handled like all the other
       * formatting errors, accept it and shove the entire line into $MSG.
       * This basically disables error piggybacking for RFC3164 inputs.  */

      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_INVALID_PRI);
      _syslog_format_parse_legacy_message(msg, &src, &left, parse_options);
    }
  else
    {
      if ((parse_options->flags & LP_NO_HEADER) == 0)
        _syslog_format_parse_legacy_header(msg, &src, &left, parse_options);

      _syslog_format_parse_legacy_message(msg, &src, &left, parse_options);
    }

  log_msg_set_value_to_string(msg, LM_V_MSGFORMAT, "rfc3164");
  return TRUE;
}

/**
 * _syslog_format_parse_syslog_proto:
 *
 * Parse a message according to the latest syslog-protocol drafts.
 **/
static gboolean
_syslog_format_parse_syslog_proto(const MsgFormatOptions *parse_options, const guchar *data, gint length,
                                  LogMessage *msg,
                                  gsize *position)
{
  /**
   *  SYSLOG-MSG      = HEADER SP STRUCTURED-DATA [SP MSG]
   *  HEADER          = PRI VERSION SP TIMESTAMP SP HOSTNAME
   *                        SP APP-NAME SP PROCID SP MSGID
   *    SP              = ' ' (space)
   *
   *    <165>1 2003-10-11T22:14:15.003Z mymachine.example.com evntslog - ID47 [exampleSDID@0 iut="3" eventSource="Application" eventID="1011"] BOMAn application
   *    event log entry...
   **/

  const guchar *src;
  gint left;
  const guchar *hostname_start = NULL;
  gint hostname_len = 0;

  src = (guchar *) data;
  left = length;

  _syslog_format_check_framing(msg, &src, &left);

  if (!_syslog_format_parse_pri(msg, &src, &left, parse_options->flags, parse_options->default_pri) ||
      !_syslog_format_parse_version(msg, &src, &left))
    {
      if ((parse_options->flags & LP_NO_RFC3164_FALLBACK) == 0)
        return _syslog_format_parse_legacy(parse_options, data, length, msg, position);
      return FALSE;
    }

  if (!_skip_space(&src, &left))
    {
      goto error;
    }

  /* ISO time format */
  time_t now = get_cached_realtime_sec();
  if (!_syslog_format_parse_date(msg, &src, &left, parse_options->flags,
                                 time_zone_info_get_offset(parse_options->recv_time_zone_info, now)))
    goto error;

  if (!_skip_space(&src, &left))
    {
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_RFC5424_MISSING_HOSTNAME);
      goto error;
    }

  /* hostname 255 ascii */
  _syslog_format_parse_hostname(msg, &src, &left, &hostname_start, &hostname_len, parse_options->flags, NULL);
  if (!_skip_space(&src, &left))
    {
      src++;
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_RFC5424_MISSING_APP_NAME);
      goto error;
    }
  /* If we did manage to find a hostname, store it. */
  if (hostname_start && hostname_len == 1 && *hostname_start == '-')
    ;
  else if (hostname_start)
    {
      log_msg_set_value(msg, LM_V_HOST, (gchar *) hostname_start, hostname_len);
    }

  /* application name 48 ascii*/
  _syslog_format_parse_column(msg, LM_V_PROGRAM, &src, &left, 48);
  if (!_skip_space(&src, &left))
    {
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_RFC5424_MISSING_PROCID);
      goto error;
    }

  /* process id 128 ascii */
  _syslog_format_parse_column(msg, LM_V_PID, &src, &left, 128);
  if (!_skip_space(&src, &left))
    {
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_RFC5424_MISSING_MSGID);
      goto error;
    }

  /* message id 32 ascii */
  _syslog_format_parse_column(msg, LM_V_MSGID, &src, &left, 32);
  if (!_skip_space(&src, &left))
    {
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_RFC5424_MISSING_SDATA);
      goto error;
    }

  /* structured data part */
  if (!_syslog_format_parse_sd_column(msg, &src, &left, parse_options))
    {
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_RFC5424_INVALID_SDATA);
      goto error;
    }

  if (!_syslog_format_parse_message_column(msg, &src, &left, parse_options))
    {
      log_msg_set_tag_by_id(msg, LM_T_SYSLOG_MISSING_MESSAGE);
      goto error;
    }

  log_msg_set_value_to_string(msg, LM_V_MSGFORMAT, "rfc5424");

  return TRUE;
error:
  *position = src - data;
  return FALSE;
}

gboolean
syslog_format_handler(const MsgFormatOptions *parse_options,
                      LogMessage *msg,
                      const guchar *data, gsize length,
                      gsize *problem_position)
{
  gboolean success;

  while (length > 0 && (data[length - 1] == '\n' || data[length - 1] == '\0'))
    length--;

  msg->initial_parse = TRUE;
  if (parse_options->flags & LP_SYSLOG_PROTOCOL)
    success = _syslog_format_parse_syslog_proto(parse_options, data, length, msg, problem_position);
  else
    success = _syslog_format_parse_legacy(parse_options, data, length, msg, problem_position);
  msg->initial_parse = FALSE;

  return success;
}

void
syslog_format_init(void)
{
  if (!handles.initialized)
    {
      handles.is_synced = log_msg_get_value_handle(".SDATA.timeQuality.isSynced");
      handles.cisco_seqid = log_msg_get_value_handle(".SDATA.meta.sequenceId");
      handles.initialized = TRUE;
    }

  _init_parse_hostname_invalid_chars();
}