File: contextual-data-record-scanner.c

package info (click to toggle)
syslog-ng 3.28.1-2%2Bdeb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,028 kB
  • sloc: ansic: 132,531; python: 5,838; makefile: 5,195; sh: 4,580; java: 3,555; xml: 3,344; yacc: 1,209; lex: 493; perl: 193; awk: 184
file content (199 lines) | stat: -rw-r--r-- 6,481 bytes parent folder | download
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
/*
 * Copyright (c) 2016 Balabit
 *
 * 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 "contextual-data-record-scanner.h"
#include "scanner/csv-scanner/csv-scanner.h"
#include "string-list.h"
#include "messages.h"
#include "cfg.h"

#include <string.h>

struct _ContextualDataRecordScanner
{
  ContextualDataRecord last_record;
  GlobalConfig *cfg;
  CSVScanner scanner;
  CSVScannerOptions options;
  gchar *filename;
  gchar *name_prefix;
};

static gboolean
_fetch_next(ContextualDataRecordScanner *self)
{
  if (!csv_scanner_scan_next(&self->scanner))
    {
      msg_error("add-contextual-data(): error parsing CSV file, expecting an additional column which was not found. Expecting (selector, name, value) triplets",
                evt_tag_str("target", csv_scanner_get_current_name(&self->scanner)));
      return FALSE;
    }

  return TRUE;
}

static gboolean
_is_whole_record_parsed(ContextualDataRecordScanner *self)
{
  if (!csv_scanner_scan_next(&self->scanner) &&
      csv_scanner_is_scan_complete(&self->scanner))
    return TRUE;

  msg_error("add-contextual-data(): extra data found at the end of line, expecting (selector, name, value) triplets");
  return FALSE;
}

static gboolean
_fetch_selector(ContextualDataRecordScanner *self, ContextualDataRecord *record)
{
  if (!_fetch_next(self))
    return FALSE;
  record->selector = g_string_new(csv_scanner_get_current_value(&self->scanner));
  return TRUE;
}

static gboolean
_fetch_name(ContextualDataRecordScanner *self, ContextualDataRecord *record)
{
  if (!_fetch_next(self))
    return FALSE;

  gchar *name = g_strdup_printf("%s%s", self->name_prefix ? : "", csv_scanner_get_current_value(&self->scanner));
  record->value_handle = log_msg_get_value_handle(name);
  g_free(name);

  return TRUE;
}

static gboolean
_fetch_value(ContextualDataRecordScanner *self, ContextualDataRecord *record)
{
  if (!_fetch_next(self))
    return FALSE;

  const gchar *value_template = csv_scanner_get_current_value(&self->scanner);

  record->value = log_template_new(self->cfg, NULL);

  if (cfg_is_config_version_older(self->cfg, VERSION_VALUE_3_21) &&
      strchr(value_template, '$') != NULL)
    {
      msg_warning("WARNING: the value field in add-contextual-data() CSV files has been changed "
                  "to be a template starting with " VERSION_3_21 ". You are using an older config "
                  "version and your CSV file contains a '$' character in this field, which needs "
                  "to be escaped as '$$' once you change your @version declaration in the "
                  "configuration. This message means that this string is now assumed to be a "
                  "literal (non-template) string for compatibility",
                  evt_tag_str("selector", record->selector->str),
                  evt_tag_str("name", log_msg_get_value_name(record->value_handle, NULL)),
                  evt_tag_str("value", value_template));
      log_template_compile_literal_string(record->value, value_template);
    }
  else
    {
      GError *error = NULL;

      if (!log_template_compile(record->value, value_template, &error))
        {
          msg_error("add-contextual-data(): error compiling template",
                    evt_tag_str("selector", record->selector->str),
                    evt_tag_str("name", log_msg_get_value_name(record->value_handle, NULL)),
                    evt_tag_str("value", value_template),
                    evt_tag_str("error", error->message));
          g_clear_error(&error);
          return FALSE;
        }
    }
  return TRUE;
}

static gboolean
_get_next_record(ContextualDataRecordScanner *self, const gchar *input, ContextualDataRecord *record)
{
  gboolean result = FALSE;

  csv_scanner_init(&self->scanner, &self->options, input);

  if (!_fetch_selector(self, record))
    goto error;

  if (!_fetch_name(self, record))
    goto error;

  if (!_fetch_value(self, record))
    goto error;

  if (!_is_whole_record_parsed(self))
    goto error;

  result = TRUE;

error:
  csv_scanner_deinit(&self->scanner);
  return result;
}

ContextualDataRecord *
contextual_data_record_scanner_get_next(ContextualDataRecordScanner *self,
                                        const gchar *input,
                                        const gchar *filename, gint lineno)
{
  contextual_data_record_init(&self->last_record);
  if (!_get_next_record(self, input, &self->last_record))
    {
      contextual_data_record_clean(&self->last_record);
      msg_error("add-contextual-data(): the failing line is",
                evt_tag_str("input", input),
                evt_tag_printf("filename", "%s:%d", filename, lineno));
      return NULL;
    }

  return &self->last_record;
}

void
contextual_data_record_scanner_free(ContextualDataRecordScanner *self)
{
  csv_scanner_options_clean(&self->options);
  g_free(self->name_prefix);
  g_free(self);
}

ContextualDataRecordScanner *
contextual_data_record_scanner_new(GlobalConfig *cfg, const gchar *name_prefix)
{
  ContextualDataRecordScanner *self =
    g_new0(ContextualDataRecordScanner, 1);

  self->cfg = cfg;

  csv_scanner_options_set_delimiters(&self->options, ",");
  csv_scanner_options_set_quote_pairs(&self->options, "\"\"''");
  const gchar *column_array[] = { "selector", "name", "value", NULL };
  csv_scanner_options_set_columns(&self->options,
                                  string_array_to_list(column_array));
  csv_scanner_options_set_flags(&self->options, CSV_SCANNER_STRIP_WHITESPACE);
  csv_scanner_options_set_dialect(&self->options, CSV_SCANNER_ESCAPE_DOUBLE_CHAR);
  self->name_prefix = g_strdup(name_prefix);

  return self;
}