| 1 |
/** @file yamlconf.c |
| 2 |
* @brief YAML configuration file loader for rsyslog. |
| 3 |
* |
| 4 |
* Parses a .yaml/.yml rsyslog config file and drives the same processing |
| 5 |
* pipeline that the RainerScript lex/bison parser uses: |
| 6 |
* - builds struct cnfobj + struct nvlst for each declarative block |
| 7 |
* - calls cnfDoObj() to let rsconf.c dispatch to the right processor |
| 8 |
* - feeds ruleset script: strings back into the existing parser via |
| 9 |
* cnfAddConfigBuffer() so RainerScript logic is fully supported |
| 10 |
* |
| 11 |
* The YAML schema mirrors the RainerScript object model: |
| 12 |
* |
| 13 |
* global: { key: value, ... } |
| 14 |
* modules: [ { load: name, param: val, ... }, ... ] |
| 15 |
* inputs: [ { type: name, param: val, ... }, ... ] |
| 16 |
* templates: [ { name: n, type: t, string: s, ... }, ... ] |
| 17 |
* rulesets: [ { name: n, script: |rainerscript..., ... }, ... ] |
| 18 |
* or [ { name: n, statements: [{if:...,action:...}, ...] }, ... ] |
| 19 |
* mainqueue: { type: linkedlist, size: 100000, ... } |
| 20 |
* include: [ { path: glob, optional: on }, ... ] |
| 21 |
* parser: [ { name: n, type: t, ... }, ... ] |
| 22 |
* lookup_table: [ { name: n, file: f, ... }, ... ] |
| 23 |
* dyn_stats: [ { name: n, ... }, ... ] |
| 24 |
* timezone: [ { id: z, offset: o }, ... ] |
| 25 |
* ratelimit: [ { name: n, interval: i, burst: b }, ... ] |
| 26 |
* |
| 27 |
* Parameter names within each block are identical to their RainerScript |
| 28 |
* counterparts; all type conversion and validation happens downstream in |
| 29 |
* nvlstGetParams() as usual. |
| 30 |
* |
| 31 |
* Concurrency & Locking: this code runs only during config load, which is |
| 32 |
* single-threaded. No synchronisation primitives are needed. |
| 33 |
* |
| 34 |
* Copyright 2025 Rainer Gerhards and Adiscon GmbH. |
| 35 |
* |
| 36 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 37 |
* you may not use this file except in compliance with the License. |
| 38 |
* You may obtain a copy of the License at |
| 39 |
* |
| 40 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 41 |
* -or- |
| 42 |
* see COPYING.ASL20 in the source distribution |
| 43 |
* |
| 44 |
* Unless required by applicable law or agreed to in writing, software |
| 45 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 46 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 47 |
* See the License for the specific language governing permissions and |
| 48 |
* limitations under the License. |
| 49 |
*/ |
| 50 |
#include "config.h" |
| 51 |
|
| 52 |
#ifdef HAVE_LIBYAML |
| 53 |
|
| 54 |
#include <stdio.h> |
| 55 |
#include <stdlib.h> |
| 56 |
#include <string.h> |
| 57 |
#include <errno.h> |
| 58 |
#include <yaml.h> |
| 59 |
#include <libestr.h> |
| 60 |
|
| 61 |
#include "rsyslog.h" |
| 62 |
#include "errmsg.h" |
| 63 |
#include "yamlconf.h" |
| 64 |
#include "grammar/rainerscript.h" |
| 65 |
#include "grammar/parserif.h" |
| 66 |
|
| 67 |
/* Maximum nesting depth tracked during YAML node skipping. |
| 68 |
* rsyslog config objects are inherently shallow (2-3 levels); 16 guards |
| 69 |
* against malformed or adversarial YAML without needing a full DFS. */ |
| 70 |
/** @brief Maximum YAML nesting depth allowed when skipping unknown nodes. */ |
| 71 |
#define YAMLCONF_MAX_DEPTH 16 |
| 72 |
|
| 73 |
/* -------------------------------------------------------------------------- |
| 74 |
* Forward declarations |
| 75 |
* -------------------------------------------------------------------------- */ |
| 76 |
static rsRetVal process_top_level(yaml_parser_t *parser, const char *key, const char *fname); |
| 77 |
static rsRetVal parse_singleton_obj(yaml_parser_t *parser, enum cnfobjType objType, const char *fname); |
| 78 |
static rsRetVal parse_obj_sequence(yaml_parser_t *parser, enum cnfobjType objType, const char *fname); |
| 79 |
static rsRetVal parse_template_sequence(yaml_parser_t *parser, const char *fname); |
| 80 |
static rsRetVal parse_elements_sequence(yaml_parser_t *parser, struct objlst **out, const char *fname); |
| 81 |
static rsRetVal parse_ruleset_sequence(yaml_parser_t *parser, const char *fname); |
| 82 |
static rsRetVal parse_actions_sequence(yaml_parser_t *parser, struct cnfstmt **head_out, const char *fname); |
| 83 |
static rsRetVal parse_include_sequence(yaml_parser_t *parser, const char *fname); |
| 84 |
static rsRetVal build_nvlst_from_mapping(yaml_parser_t *parser, struct nvlst **out, const char *fname); |
| 85 |
static rsRetVal build_array_from_sequence(yaml_parser_t *parser, struct cnfarray **out, const char *fname); |
| 86 |
static rsRetVal yaml_value_to_nvlst_node( |
| 87 |
yaml_parser_t *parser, const char *fname, yaml_event_t *ev, const char *key, struct nvlst **out); |
| 88 |
static rsRetVal build_stmts_rs(yaml_parser_t *parser, es_str_t **s, const char *fname); |
| 89 |
static rsRetVal expect_event( |
| 90 |
yaml_parser_t *parser, yaml_event_t *ev, yaml_event_type_t expected, const char *ctx, const char *fname); |
| 91 |
static void skip_node(yaml_parser_t *parser); |
| 92 |
|
| 93 |
/* -------------------------------------------------------------------------- |
| 94 |
* Helpers |
| 95 |
* -------------------------------------------------------------------------- */ |
| 96 |
|
| 97 |
/* Convert a YAML scalar value (C string) into a heap-allocated es_str_t. |
| 98 |
* The caller owns the returned pointer. */ |
| 99 |
static es_str_t *scalar_to_estr(const char *val) { |
| 100 |
return es_newStrFromCStr(val, strlen(val)); |
| 101 |
} |
| 102 |
|
| 103 |
/* Append a C string to an es_str_t, growing as needed. |
| 104 |
* Returns 0 on success, -1 on error. */ |
| 105 |
static int estr_append_cstr(es_str_t **s, const char *buf, size_t len) { |
| 106 |
if (len > (size_t)INT_MAX) { |
| 107 |
LogError(0, RS_RET_OUT_OF_MEMORY, "yamlconf: string length %zu exceeds maximum supported size", len); |
| 108 |
return -1; |
| 109 |
} |
| 110 |
return es_addBuf(s, buf, (es_size_t)len); |
| 111 |
} |
| 112 |
|
| 113 |
/* Append a single character to an es_str_t. */ |
| 114 |
static int estr_append_char(es_str_t **s, char c) { |
| 115 |
return es_addChar(s, (unsigned char)c); |
| 116 |
} |
| 117 |
|
| 118 |
/* Append a string value enclosed in double-quotes, escaping backslash and |
| 119 |
* double-quote characters with a preceding backslash. This produces valid |
| 120 |
* RainerScript string literals. */ |
| 121 |
static int estr_append_quoted(es_str_t **s, const char *val, size_t len) { |
| 122 |
if (estr_append_char(s, '"') != 0) return -1; |
| 123 |
for (size_t i = 0; i < len; i++) { |
| 124 |
char c = val[i]; |
| 125 |
if (c == '"' || c == '\\') { |
| 126 |
if (estr_append_char(s, '\\') != 0) return -1; |
| 127 |
} |
| 128 |
if (estr_append_char(s, c) != 0) return -1; |
| 129 |
} |
| 130 |
return estr_append_char(s, '"'); |
| 131 |
} |
| 132 |
|
| 133 |
/* Synthesise a complete RainerScript ruleset block: |
| 134 |
* |
| 135 |
* ruleset(name="<n>" [key="val" ...]) { |
| 136 |
* <script_str> |
| 137 |
* } |
| 138 |
* |
| 139 |
* All entries from 'lst' (the nvlst collected from the YAML mapping) are |
| 140 |
* serialised as key="value" pairs inside the parentheses. String values are |
| 141 |
* double-quoted and escaped. Array values use ["v1","v2"] syntax. |
| 142 |
* |
| 143 |
* The resulting es_str_t has two trailing NUL bytes appended so it is safe |
| 144 |
* to pass directly to cnfAddConfigBuffer() / yy_scan_buffer(). |
| 145 |
* |
| 146 |
* Returns RS_RET_OK on success, RS_RET_OUT_OF_MEMORY on allocation failure. */ |
| 147 |
static rsRetVal build_ruleset_rainerscript(struct nvlst *lst, const char *script_str, es_str_t **out) { |
| 148 |
es_str_t *s = es_newStr(256); |
| 149 |
DEFiRet; |
| 150 |
|
| 151 |
if (s == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 152 |
|
| 153 |
if (estr_append_cstr(&s, "ruleset(", 8) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 154 |
|
| 155 |
for (struct nvlst *n = lst; n != NULL; n = n->next) { |
| 156 |
if (n != lst) { |
| 157 |
if (estr_append_char(&s, ' ') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 158 |
} |
| 159 |
/* key name */ |
| 160 |
size_t klen = es_strlen(n->name); |
| 161 |
if (estr_append_cstr(&s, (char *)es_getBufAddr(n->name), klen) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 162 |
if (estr_append_char(&s, '=') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 163 |
|
| 164 |
if (n->val.datatype == 'S') { |
| 165 |
/* String value → "escaped" */ |
| 166 |
size_t vlen = es_strlen(n->val.d.estr); |
| 167 |
if (estr_append_quoted(&s, (char *)es_getBufAddr(n->val.d.estr), vlen) != 0) |
| 168 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 169 |
} else if (n->val.datatype == 'A') { |
| 170 |
/* Array value → ["v1","v2",...] */ |
| 171 |
struct cnfarray *ar = n->val.d.ar; |
| 172 |
if (estr_append_char(&s, '[') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 173 |
for (int i = 0; i < ar->nmemb; i++) { |
| 174 |
if (i > 0 && estr_append_char(&s, ',') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 175 |
size_t elen = es_strlen(ar->arr[i]); |
| 176 |
if (estr_append_quoted(&s, (char *)es_getBufAddr(ar->arr[i]), elen) != 0) |
| 177 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 178 |
} |
| 179 |
if (estr_append_char(&s, ']') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 180 |
} else { |
| 181 |
/* Unexpected type; skip with a warning (should not happen for rulesets) */ |
| 182 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 183 |
"yamlconf: unexpected nvlst value datatype '%c' in ruleset " |
| 184 |
"parameter serialisation, skipping", |
| 185 |
n->val.datatype); |
| 186 |
if (estr_append_cstr(&s, "\"\"", 2) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
/* Close parameter list, open script body */ |
| 191 |
if (estr_append_cstr(&s, ") {\n", 4) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 192 |
|
| 193 |
/* Script body */ |
| 194 |
size_t slen = strlen(script_str); |
| 195 |
if (estr_append_cstr(&s, script_str, slen) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 196 |
|
| 197 |
/* Ensure the script ends with a newline before the closing brace */ |
| 198 |
if (slen == 0 || script_str[slen - 1] != '\n') { |
| 199 |
if (estr_append_char(&s, '\n') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 200 |
} |
| 201 |
|
| 202 |
if (estr_append_cstr(&s, "}\n", 2) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 203 |
|
| 204 |
/* Two trailing NUL bytes required by yy_scan_buffer() */ |
| 205 |
if (estr_append_char(&s, '\0') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 206 |
if (estr_append_char(&s, '\0') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 207 |
|
| 208 |
*out = s; |
| 209 |
s = NULL; /* caller owns it */ |
| 210 |
iRet = RS_RET_OK; |
| 211 |
|
| 212 |
finalize_it: |
| 213 |
if (s != NULL) es_deleteStr(s); |
| 214 |
RETiRet; |
| 215 |
} |
| 216 |
|
| 217 |
/* Emit a parse-error message that includes the YAML source location. */ |
| 218 |
static void yaml_errmsg(yaml_parser_t *parser, const char *fname, const char *extra) { |
| 219 |
if (parser->problem != NULL) { |
| 220 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: %s (line %lu col %lu)%s%s", fname, parser->problem, |
| 221 |
(unsigned long)parser->problem_mark.line + 1, (unsigned long)parser->problem_mark.column + 1, |
| 222 |
extra ? " " : "", extra ? extra : ""); |
| 223 |
} else { |
| 224 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: parse error%s%s", fname, extra ? " " : "", |
| 225 |
extra ? extra : ""); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
/* Pull the next event from the parser; on error log and return RS_RET_CONF_PARSE_ERROR. */ |
| 230 |
static rsRetVal next_event(yaml_parser_t *parser, yaml_event_t *ev, const char *fname) { |
| 231 |
DEFiRet; |
| 232 |
if (!yaml_parser_parse(parser, ev)) { |
| 233 |
yaml_errmsg(parser, fname, NULL); |
| 234 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 235 |
} |
| 236 |
finalize_it: |
| 237 |
RETiRet; |
| 238 |
} |
| 239 |
|
| 240 |
/* Pull the next event and assert it has the expected type. */ |
| 241 |
static rsRetVal expect_event( |
| 242 |
yaml_parser_t *parser, yaml_event_t *ev, yaml_event_type_t expected, const char *ctx, const char *fname) { |
| 243 |
DEFiRet; |
| 244 |
CHKiRet(next_event(parser, ev, fname)); |
| 245 |
if (ev->type != expected) { |
| 246 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: expected event %d got %d in %s", fname, (int)expected, |
| 247 |
(int)ev->type, ctx); |
| 248 |
yaml_event_delete(ev); |
| 249 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 250 |
} |
| 251 |
finalize_it: |
| 252 |
RETiRet; |
| 253 |
} |
| 254 |
|
| 255 |
/* Skip over the current node (scalar, sequence, or mapping) and all its |
| 256 |
* children. Assumes the opening event for this node has already been consumed |
| 257 |
* (i.e. the caller has just read MAPPING_START or SEQUENCE_START; for a |
| 258 |
* scalar the caller passes the scalar event and we do nothing). */ |
| 259 |
static void skip_node(yaml_parser_t *parser) { |
| 260 |
yaml_event_t ev; |
| 261 |
int depth = 1; |
| 262 |
while (depth > 0) { |
| 263 |
if (!yaml_parser_parse(parser, &ev)) break; |
| 264 |
PRAGMA_DIAGNOSTIC_PUSH |
| 265 |
PRAGMA_IGNORE_Wswitch_enum switch (ev.type) { |
| 266 |
case YAML_MAPPING_START_EVENT: |
| 267 |
case YAML_SEQUENCE_START_EVENT: |
| 268 |
depth++; |
| 269 |
if (depth > YAMLCONF_MAX_DEPTH) { |
| 270 |
yaml_event_delete(&ev); |
| 271 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 272 |
"yamlconf: YAML nesting exceeds maximum depth %d; " |
| 273 |
"skipping remainder", |
| 274 |
YAMLCONF_MAX_DEPTH); |
| 275 |
return; |
| 276 |
} |
| 277 |
break; |
| 278 |
case YAML_MAPPING_END_EVENT: |
| 279 |
case YAML_SEQUENCE_END_EVENT: |
| 280 |
depth--; |
| 281 |
break; |
| 282 |
default: |
| 283 |
break; |
| 284 |
} |
| 285 |
PRAGMA_DIAGNOSTIC_POP |
| 286 |
yaml_event_delete(&ev); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/* -------------------------------------------------------------------------- |
| 291 |
* nvlst / cnfarray builders |
| 292 |
* -------------------------------------------------------------------------- */ |
| 293 |
|
| 294 |
/* Build a struct cnfarray* from the remaining items of a YAML sequence. |
| 295 |
* The SEQUENCE_START event must already have been consumed by the caller. |
| 296 |
* Consumes up to and including the matching SEQUENCE_END event. */ |
| 297 |
static rsRetVal build_array_from_sequence(yaml_parser_t *parser, struct cnfarray **out, const char *fname) { |
| 298 |
yaml_event_t ev; |
| 299 |
struct cnfarray *arr = NULL; |
| 300 |
es_str_t *estr; |
| 301 |
DEFiRet; |
| 302 |
|
| 303 |
*out = NULL; |
| 304 |
while (1) { |
| 305 |
CHKiRet(next_event(parser, &ev, fname)); |
| 306 |
if (ev.type == YAML_SEQUENCE_END_EVENT) { |
| 307 |
yaml_event_delete(&ev); |
| 308 |
break; |
| 309 |
} |
| 310 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 311 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: array elements must be scalars", fname); |
| 312 |
yaml_event_delete(&ev); |
| 313 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 314 |
} |
| 315 |
estr = scalar_to_estr((char *)ev.data.scalar.value); |
| 316 |
yaml_event_delete(&ev); |
| 317 |
if (estr == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 318 |
|
| 319 |
if (arr == NULL) |
| 320 |
arr = cnfarrayNew(estr); |
| 321 |
else |
| 322 |
arr = cnfarrayAdd(arr, estr); |
| 323 |
|
| 324 |
if (arr == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 325 |
} |
| 326 |
*out = arr; |
| 327 |
|
| 328 |
finalize_it: |
| 329 |
if (iRet != RS_RET_OK && arr != NULL) { |
| 330 |
cnfarrayContentDestruct(arr); |
| 331 |
free(arr); |
| 332 |
} |
| 333 |
RETiRet; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* @brief Converts one YAML value event into a named nvlst node. |
| 338 |
* |
| 339 |
* The event @p ev must already have been pulled from the parser by the |
| 340 |
* caller (the value event following a key scalar). The event is always |
| 341 |
* consumed before returning, regardless of success or failure. |
| 342 |
* |
| 343 |
* Callers that need to handle YAML_MAPPING_START_EVENT specially (e.g. to |
| 344 |
* skip nested mappings with a warning) should check ev->type before calling |
| 345 |
* and only invoke this function for SCALAR and SEQUENCE_START events. |
| 346 |
* |
| 347 |
* @param parser libyaml parser; read-from only when ev is SEQUENCE_START. |
| 348 |
* @param fname Config file name used in diagnostic messages. |
| 349 |
* @param ev Pre-read value event; always consumed. |
| 350 |
* @param key Parameter key (C string); copied into the node's name field. |
| 351 |
* @param out Set to the new nvlst node on success; NULL on any error. |
| 352 |
* @return RS_RET_OK, RS_RET_OUT_OF_MEMORY, or RS_RET_CONF_PARSE_ERROR. |
| 353 |
*/ |
| 354 |
static rsRetVal yaml_value_to_nvlst_node( |
| 355 |
yaml_parser_t *parser, const char *fname, yaml_event_t *ev, const char *key, struct nvlst **out) { |
| 356 |
es_str_t *keyestr = NULL; |
| 357 |
struct nvlst *node = NULL; |
| 358 |
DEFiRet; |
| 359 |
|
| 360 |
*out = NULL; |
| 361 |
keyestr = scalar_to_estr(key); |
| 362 |
if (keyestr == NULL) { |
| 363 |
yaml_event_delete(ev); |
| 364 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 365 |
} |
| 366 |
|
| 367 |
if (ev->type == YAML_SCALAR_EVENT) { |
| 368 |
es_str_t *val = scalar_to_estr((char *)ev->data.scalar.value); |
| 369 |
yaml_event_delete(ev); |
| 370 |
if (val == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 371 |
node = nvlstNewStr(val); |
| 372 |
if (node == NULL) { |
| 373 |
es_deleteStr(val); |
| 374 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 375 |
} |
| 376 |
} else if (ev->type == YAML_SEQUENCE_START_EVENT) { |
| 377 |
struct cnfarray *arr = NULL; |
| 378 |
yaml_event_delete(ev); |
| 379 |
CHKiRet(build_array_from_sequence(parser, &arr, fname)); |
| 380 |
node = nvlstNewArray(arr); |
| 381 |
if (node == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 382 |
} else { |
| 383 |
yaml_event_delete(ev); |
| 384 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: unsupported value type for parameter '%s'", fname, key); |
| 385 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 386 |
} |
| 387 |
|
| 388 |
nvlstSetName(node, keyestr); |
| 389 |
keyestr = NULL; /* owned by node */ |
| 390 |
*out = node; |
| 391 |
node = NULL; |
| 392 |
|
| 393 |
finalize_it: |
| 394 |
es_deleteStr(keyestr); |
| 395 |
if (node != NULL) nvlstDestruct(node); |
| 396 |
RETiRet; |
| 397 |
} |
| 398 |
|
| 399 |
/* Build a struct nvlst* from a YAML mapping. |
| 400 |
* The MAPPING_START event must already have been consumed by the caller. |
| 401 |
* Consumes up to and including the matching MAPPING_END event. |
| 402 |
* |
| 403 |
* Each key/value pair becomes one nvlst node: |
| 404 |
* scalar value → nvlstNewStr(estr) |
| 405 |
* sequence → nvlstNewArray(cnfarray) |
| 406 |
* nested mapping → logged as unsupported and skipped (rsyslog params are flat) |
| 407 |
*/ |
| 408 |
static rsRetVal build_nvlst_from_mapping(yaml_parser_t *parser, struct nvlst **out, const char *fname) { |
| 409 |
yaml_event_t ev; |
| 410 |
struct nvlst *lst = NULL; |
| 411 |
struct nvlst *node; |
| 412 |
char *key = NULL; |
| 413 |
DEFiRet; |
| 414 |
|
| 415 |
*out = NULL; |
| 416 |
|
| 417 |
while (1) { |
| 418 |
/* Expect: key scalar or mapping end */ |
| 419 |
CHKiRet(next_event(parser, &ev, fname)); |
| 420 |
|
| 421 |
if (ev.type == YAML_MAPPING_END_EVENT) { |
| 422 |
yaml_event_delete(&ev); |
| 423 |
break; |
| 424 |
} |
| 425 |
|
| 426 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 427 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: expected scalar key in mapping", fname); |
| 428 |
yaml_event_delete(&ev); |
| 429 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 430 |
} |
| 431 |
|
| 432 |
free(key); |
| 433 |
key = strdup((char *)ev.data.scalar.value); |
| 434 |
yaml_event_delete(&ev); |
| 435 |
if (key == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 436 |
|
| 437 |
/* Now read the value */ |
| 438 |
CHKiRet(next_event(parser, &ev, fname)); |
| 439 |
|
| 440 |
if (ev.type == YAML_MAPPING_START_EVENT) { |
| 441 |
/* Nested mappings are not part of the rsyslog parameter model. |
| 442 |
* Skip and warn so the user knows the key was ignored. */ |
| 443 |
yaml_event_delete(&ev); |
| 444 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 445 |
"yamlconf: %s: nested mapping for key '%s' is not supported – ignoring", fname, key); |
| 446 |
skip_node(parser); |
| 447 |
continue; |
| 448 |
} |
| 449 |
CHKiRet(yaml_value_to_nvlst_node(parser, fname, &ev, key, &node)); |
| 450 |
/* Prepend; cnfDoObj() does not depend on order */ |
| 451 |
node->next = lst; |
| 452 |
lst = node; |
| 453 |
} |
| 454 |
|
| 455 |
*out = lst; |
| 456 |
|
| 457 |
finalize_it: |
| 458 |
free(key); |
| 459 |
if (iRet != RS_RET_OK && lst != NULL) { |
| 460 |
nvlstDestruct(lst); |
| 461 |
} |
| 462 |
RETiRet; |
| 463 |
} |
| 464 |
|
| 465 |
/* -------------------------------------------------------------------------- |
| 466 |
* Object section parsers |
| 467 |
* -------------------------------------------------------------------------- */ |
| 468 |
|
| 469 |
/* Parse a singleton mapping section (e.g. global:, mainqueue:) and emit one |
| 470 |
* cnfobj of the given type. |
| 471 |
* The MAPPING_START event for the section body must already be consumed. */ |
| 472 |
static rsRetVal parse_singleton_obj(yaml_parser_t *parser, enum cnfobjType objType, const char *fname) { |
| 473 |
struct nvlst *lst = NULL; |
| 474 |
struct cnfobj *obj = NULL; |
| 475 |
DEFiRet; |
| 476 |
|
| 477 |
CHKiRet(build_nvlst_from_mapping(parser, &lst, fname)); |
| 478 |
obj = cnfobjNew(objType, lst); |
| 479 |
if (obj == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 480 |
lst = NULL; /* owned by obj now */ |
| 481 |
cnfDoObj(obj); |
| 482 |
|
| 483 |
finalize_it: |
| 484 |
if (lst != NULL) nvlstDestruct(lst); |
| 485 |
RETiRet; |
| 486 |
} |
| 487 |
|
| 488 |
/* Parse a sequence of mapping items, each becoming one cnfobj. |
| 489 |
* The SEQUENCE_START event for the list must already be consumed. |
| 490 |
* |
| 491 |
* Used for: modules, inputs, templates, parser, lookup_table, dyn_stats, |
| 492 |
* ratelimit, timezone. */ |
| 493 |
static rsRetVal parse_obj_sequence(yaml_parser_t *parser, enum cnfobjType objType, const char *fname) { |
| 494 |
yaml_event_t ev; |
| 495 |
DEFiRet; |
| 496 |
|
| 497 |
while (1) { |
| 498 |
CHKiRet(next_event(parser, &ev, fname)); |
| 499 |
|
| 500 |
if (ev.type == YAML_SEQUENCE_END_EVENT) { |
| 501 |
yaml_event_delete(&ev); |
| 502 |
break; |
| 503 |
} |
| 504 |
if (ev.type != YAML_MAPPING_START_EVENT) { |
| 505 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: sequence item must be a mapping", fname); |
| 506 |
yaml_event_delete(&ev); |
| 507 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 508 |
} |
| 509 |
yaml_event_delete(&ev); |
| 510 |
|
| 511 |
struct nvlst *lst = NULL; |
| 512 |
CHKiRet(build_nvlst_from_mapping(parser, &lst, fname)); |
| 513 |
|
| 514 |
struct cnfobj *obj = cnfobjNew(objType, lst); |
| 515 |
if (obj == NULL) { |
| 516 |
nvlstDestruct(lst); |
| 517 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 518 |
} |
| 519 |
cnfDoObj(obj); |
| 520 |
} |
| 521 |
|
| 522 |
finalize_it: |
| 523 |
RETiRet; |
| 524 |
} |
| 525 |
|
| 526 |
/* Parse the elements: sequence of a list template. |
| 527 |
* |
| 528 |
* Each item is a mapping with exactly one key, "property" or "constant", |
| 529 |
* whose value is a mapping of parameters: |
| 530 |
* |
| 531 |
* elements: |
| 532 |
* - property: |
| 533 |
* name: msg |
| 534 |
* - constant: |
| 535 |
* value: "\n" |
| 536 |
* |
| 537 |
* Builds a struct objlst of CNFOBJ_PROPERTY / CNFOBJ_CONSTANT nodes that |
| 538 |
* mirrors what the bison grammar constructs for: |
| 539 |
* template(name="t" type="list") { property(name="msg") constant(value="\n") } |
| 540 |
* |
| 541 |
* The SEQUENCE_START event must already have been consumed by the caller. |
| 542 |
* Ownership of the returned objlst passes to the caller. */ |
| 543 |
static rsRetVal parse_elements_sequence(yaml_parser_t *parser, struct objlst **out, const char *fname) { |
| 544 |
yaml_event_t ev; |
| 545 |
struct objlst *subobjs = NULL; |
| 546 |
char *elemtype = NULL; |
| 547 |
DEFiRet; |
| 548 |
|
| 549 |
*out = NULL; |
| 550 |
|
| 551 |
while (1) { |
| 552 |
CHKiRet(next_event(parser, &ev, fname)); |
| 553 |
if (ev.type == YAML_SEQUENCE_END_EVENT) { |
| 554 |
yaml_event_delete(&ev); |
| 555 |
break; |
| 556 |
} |
| 557 |
if (ev.type != YAML_MAPPING_START_EVENT) { |
| 558 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 559 |
"yamlconf: %s: each elements: item must be a mapping with a " |
| 560 |
"'property' or 'constant' key", |
| 561 |
fname); |
| 562 |
yaml_event_delete(&ev); |
| 563 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 564 |
} |
| 565 |
yaml_event_delete(&ev); |
| 566 |
|
| 567 |
/* Read the element type key: "property" or "constant" */ |
| 568 |
CHKiRet(next_event(parser, &ev, fname)); |
| 569 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 570 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: elements: item key must be 'property' or 'constant'", |
| 571 |
fname); |
| 572 |
yaml_event_delete(&ev); |
| 573 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 574 |
} |
| 575 |
free(elemtype); |
| 576 |
elemtype = strdup((char *)ev.data.scalar.value); |
| 577 |
yaml_event_delete(&ev); |
| 578 |
if (elemtype == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 579 |
|
| 580 |
enum cnfobjType etype; |
| 581 |
if (!strcmp(elemtype, "property")) { |
| 582 |
etype = CNFOBJ_PROPERTY; |
| 583 |
} else if (!strcmp(elemtype, "constant")) { |
| 584 |
etype = CNFOBJ_CONSTANT; |
| 585 |
} else { |
| 586 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 587 |
"yamlconf: %s: elements: item key must be 'property' or 'constant', got '%s'", fname, elemtype); |
| 588 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 589 |
} |
| 590 |
|
| 591 |
/* Read MAPPING_START for the element parameters */ |
| 592 |
CHKiRet(expect_event(parser, &ev, YAML_MAPPING_START_EVENT, "element params mapping", fname)); |
| 593 |
yaml_event_delete(&ev); |
| 594 |
|
| 595 |
struct nvlst *elst = NULL; |
| 596 |
CHKiRet(build_nvlst_from_mapping(parser, &elst, fname)); |
| 597 |
|
| 598 |
struct cnfobj *eobj = cnfobjNew(etype, elst); |
| 599 |
elst = NULL; /* owned by eobj */ |
| 600 |
if (eobj == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 601 |
|
| 602 |
subobjs = objlstAdd(subobjs, eobj); |
| 603 |
|
| 604 |
/* Consume the MAPPING_END of the outer element item mapping */ |
| 605 |
CHKiRet(expect_event(parser, &ev, YAML_MAPPING_END_EVENT, "element item end", fname)); |
| 606 |
yaml_event_delete(&ev); |
| 607 |
} |
| 608 |
|
| 609 |
*out = subobjs; |
| 610 |
subobjs = NULL; |
| 611 |
|
| 612 |
finalize_it: |
| 613 |
free(elemtype); |
| 614 |
if (subobjs != NULL) objlstDestruct(subobjs); |
| 615 |
RETiRet; |
| 616 |
} |
| 617 |
|
| 618 |
/* Parse the templates: sequence. |
| 619 |
* |
| 620 |
* Simple templates (type=string/subtree/plugin) use the flat nvlst path |
| 621 |
* identical to other object sequences. |
| 622 |
* |
| 623 |
* List templates (type=list) need sub-objects: the "elements:" key takes |
| 624 |
* a sequence of property:/constant: items that are built as |
| 625 |
* CNFOBJ_PROPERTY/CNFOBJ_CONSTANT nodes and attached to the template |
| 626 |
* cnfobj->subobjs — exactly what the bison grammar does for: |
| 627 |
* template(name="t" type="list") { property(...) constant(...) } |
| 628 |
* |
| 629 |
* YAML schema summary: |
| 630 |
* # string template |
| 631 |
* - name: t1 |
| 632 |
* type: string |
| 633 |
* string: "%msg%\n" |
| 634 |
* |
| 635 |
* # subtree template |
| 636 |
* - name: t2 |
| 637 |
* type: subtree |
| 638 |
* subtree: "$!all-json" |
| 639 |
* |
| 640 |
* # list template |
| 641 |
* - name: t3 |
| 642 |
* type: list |
| 643 |
* elements: |
| 644 |
* - property: |
| 645 |
* name: msg |
| 646 |
* - constant: |
| 647 |
* value: "\n" |
| 648 |
* |
| 649 |
* SEQUENCE_START must already be consumed by the caller. */ |
| 650 |
static rsRetVal parse_template_sequence(yaml_parser_t *parser, const char *fname) { |
| 651 |
yaml_event_t ev; |
| 652 |
struct nvlst *lst = NULL; |
| 653 |
struct objlst *subobjs = NULL; |
| 654 |
char *kname = NULL; |
| 655 |
DEFiRet; |
| 656 |
|
| 657 |
while (1) { |
| 658 |
CHKiRet(next_event(parser, &ev, fname)); |
| 659 |
if (ev.type == YAML_SEQUENCE_END_EVENT) { |
| 660 |
yaml_event_delete(&ev); |
| 661 |
break; |
| 662 |
} |
| 663 |
if (ev.type != YAML_MAPPING_START_EVENT) { |
| 664 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: templates sequence item must be a mapping", fname); |
| 665 |
yaml_event_delete(&ev); |
| 666 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 667 |
} |
| 668 |
yaml_event_delete(&ev); |
| 669 |
|
| 670 |
/* Reset per-template state */ |
| 671 |
lst = NULL; |
| 672 |
subobjs = NULL; |
| 673 |
|
| 674 |
/* Walk the template mapping manually to intercept "elements:" */ |
| 675 |
while (1) { |
| 676 |
CHKiRet(next_event(parser, &ev, fname)); |
| 677 |
if (ev.type == YAML_MAPPING_END_EVENT) { |
| 678 |
yaml_event_delete(&ev); |
| 679 |
break; |
| 680 |
} |
| 681 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 682 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: expected scalar key in template mapping", fname); |
| 683 |
yaml_event_delete(&ev); |
| 684 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 685 |
} |
| 686 |
free(kname); |
| 687 |
kname = strdup((char *)ev.data.scalar.value); |
| 688 |
yaml_event_delete(&ev); |
| 689 |
if (kname == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 690 |
|
| 691 |
CHKiRet(next_event(parser, &ev, fname)); |
| 692 |
|
| 693 |
if (!strcmp(kname, "elements")) { |
| 694 |
/* elements: [...] – sub-objects for list template */ |
| 695 |
if (ev.type != YAML_SEQUENCE_START_EVENT) { |
| 696 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 697 |
"yamlconf: %s: 'elements' value must be a sequence of " |
| 698 |
"property/constant items", |
| 699 |
fname); |
| 700 |
yaml_event_delete(&ev); |
| 701 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 702 |
} |
| 703 |
yaml_event_delete(&ev); |
| 704 |
if (subobjs != NULL) objlstDestruct(subobjs); |
| 705 |
subobjs = NULL; |
| 706 |
CHKiRet(parse_elements_sequence(parser, &subobjs, fname)); |
| 707 |
} else { |
| 708 |
/* Regular template param: add to nvlst */ |
| 709 |
struct nvlst *node = NULL; |
| 710 |
CHKiRet(yaml_value_to_nvlst_node(parser, fname, &ev, kname, &node)); |
| 711 |
node->next = lst; |
| 712 |
lst = node; |
| 713 |
} |
| 714 |
} /* end per-template key loop */ |
| 715 |
|
| 716 |
struct cnfobj *obj = cnfobjNew(CNFOBJ_TPL, lst); |
| 717 |
lst = NULL; /* owned by obj */ |
| 718 |
if (obj == NULL) { |
| 719 |
if (subobjs != NULL) objlstDestruct(subobjs); |
| 720 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 721 |
} |
| 722 |
obj->subobjs = subobjs; /* NULL for non-list templates */ |
| 723 |
subobjs = NULL; |
| 724 |
cnfDoObj(obj); |
| 725 |
} /* end template sequence loop */ |
| 726 |
|
| 727 |
finalize_it: |
| 728 |
free(kname); |
| 729 |
nvlstDestruct(lst); |
| 730 |
if (subobjs != NULL) objlstDestruct(subobjs); |
| 731 |
RETiRet; |
| 732 |
} |
| 733 |
|
| 734 |
/* Parse the actions: sequence. |
| 735 |
* |
| 736 |
* Each item is a YAML mapping that mirrors an action() object: the "type" |
| 737 |
* key names the output module and all other keys are passed as parameters. |
| 738 |
* We build an nvlst for each item and call cnfstmtNewAct() directly, then |
| 739 |
* chain the resulting cnfstmt nodes via ->next. |
| 740 |
* |
| 741 |
* The SEQUENCE_START event must already have been consumed by the caller. |
| 742 |
* On success *head_out points to the first node of the chain (possibly NULL |
| 743 |
* if the sequence was empty). Ownership passes to the caller. */ |
| 744 |
static rsRetVal parse_actions_sequence(yaml_parser_t *parser, struct cnfstmt **head_out, const char *fname) { |
| 745 |
yaml_event_t ev; |
| 746 |
struct cnfstmt *head = NULL, *tail = NULL; |
| 747 |
DEFiRet; |
| 748 |
|
| 749 |
*head_out = NULL; |
| 750 |
|
| 751 |
while (1) { |
| 752 |
CHKiRet(next_event(parser, &ev, fname)); |
| 753 |
if (ev.type == YAML_SEQUENCE_END_EVENT) { |
| 754 |
yaml_event_delete(&ev); |
| 755 |
break; |
| 756 |
} |
| 757 |
if (ev.type != YAML_MAPPING_START_EVENT) { |
| 758 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: actions: item must be a mapping", fname); |
| 759 |
yaml_event_delete(&ev); |
| 760 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 761 |
} |
| 762 |
yaml_event_delete(&ev); |
| 763 |
|
| 764 |
struct nvlst *alst = NULL; |
| 765 |
CHKiRet(build_nvlst_from_mapping(parser, &alst, fname)); |
| 766 |
|
| 767 |
/* cnfstmtNewAct() takes ownership of alst and frees it internally. */ |
| 768 |
struct cnfstmt *act = cnfstmtNewAct(alst); |
| 769 |
if (act == NULL) { |
| 770 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 771 |
} |
| 772 |
|
| 773 |
if (head == NULL) { |
| 774 |
head = tail = act; |
| 775 |
} else { |
| 776 |
tail->next = act; |
| 777 |
tail = act; |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
*head_out = head; |
| 782 |
head = NULL; |
| 783 |
|
| 784 |
finalize_it: |
| 785 |
if (head != NULL) cnfstmtDestructLst(head); |
| 786 |
RETiRet; |
| 787 |
} |
| 788 |
|
| 789 |
/* -------------------------------------------------------------------------- |
| 790 |
* YAML-native statements: block |
| 791 |
* |
| 792 |
* Converts a YAML statements: sequence into a RainerScript body string that |
| 793 |
* is then injected via cnfAddConfigBuffer(), following the same proven path |
| 794 |
* as the raw script: key. |
| 795 |
* |
| 796 |
* Supported statement types in a statements: sequence item: |
| 797 |
* |
| 798 |
* Unconditional action (same schema as actions: items): |
| 799 |
* - type: omfile |
| 800 |
* file: /path |
| 801 |
* template: outfmt |
| 802 |
* |
| 803 |
* Conditional (single-action shorthand): |
| 804 |
* - if: '$msg contains "msgnum:"' |
| 805 |
* action: |
| 806 |
* type: omfile |
| 807 |
* file: /path |
| 808 |
* else: # optional |
| 809 |
* - stop: true |
| 810 |
* |
| 811 |
* Conditional (multi-statement then/else): |
| 812 |
* - if: '$msg contains "msgnum:"' |
| 813 |
* then: |
| 814 |
* - type: omfile |
| 815 |
* file: /path |
| 816 |
* else: # optional |
| 817 |
* - stop: true |
| 818 |
* |
| 819 |
* Control flow: |
| 820 |
* - stop: true # or stop: |
| 821 |
* - continue: true # or continue: |
| 822 |
* - call: rulesetname |
| 823 |
* |
| 824 |
* Variable assignment: |
| 825 |
* - set: |
| 826 |
* var: "$.nbr" |
| 827 |
* expr: 'field($msg, 58, 2)' |
| 828 |
* - unset: "$.var" |
| 829 |
* |
| 830 |
* foreach loop: |
| 831 |
* - foreach: |
| 832 |
* var: "$.item" |
| 833 |
* in: "$!array" |
| 834 |
* do: |
| 835 |
* - type: omfile |
| 836 |
* file: /tmp/out |
| 837 |
* |
| 838 |
* Indirect ruleset call: |
| 839 |
* - call_indirect: "$!rulesetname" |
| 840 |
* |
| 841 |
* Reload a lookup table (triggers an asynchronous reload of a previously |
| 842 |
* loaded lookup table). Unlike action() parameters, reload_lookup_table |
| 843 |
* takes positional string arguments in RainerScript, so the YAML keys |
| 844 |
* "table" and "stub_value" are extracted and emitted as quoted literals: |
| 845 |
* - reload_lookup_table: |
| 846 |
* table: myLookupTable |
| 847 |
* stub_value: unknown # optional; returned on reload failure |
| 848 |
* |
| 849 |
* -------------------------------------------------------------------------- */ |
| 850 |
|
| 851 |
/* Append all entries of 'lst' as key="val" pairs to the RainerScript |
| 852 |
* string *s. Used to serialise action() parameter lists. |
| 853 |
* Reuses the same quoting logic as build_ruleset_rainerscript(). |
| 854 |
* Returns 0 on success, -1 on allocation failure (int, not rsRetVal). */ |
| 855 |
static int append_nvlst_as_rs_params(es_str_t **s, struct nvlst *lst) { |
| 856 |
for (struct nvlst *n = lst; n != NULL; n = n->next) { |
| 857 |
if (n != lst) { |
| 858 |
if (estr_append_char(s, ' ') != 0) return -1; |
| 859 |
} |
| 860 |
size_t klen = es_strlen(n->name); |
| 861 |
if (estr_append_cstr(s, (char *)es_getBufAddr(n->name), klen) != 0) return -1; |
| 862 |
if (estr_append_char(s, '=') != 0) return -1; |
| 863 |
if (n->val.datatype == 'S') { |
| 864 |
size_t vlen = es_strlen(n->val.d.estr); |
| 865 |
if (estr_append_quoted(s, (char *)es_getBufAddr(n->val.d.estr), vlen) != 0) return -1; |
| 866 |
} else if (n->val.datatype == 'A') { |
| 867 |
struct cnfarray *ar = n->val.d.ar; |
| 868 |
if (estr_append_char(s, '[') != 0) return -1; |
| 869 |
for (int i = 0; i < ar->nmemb; i++) { |
| 870 |
if (i > 0 && estr_append_char(s, ',') != 0) return -1; |
| 871 |
size_t elen = es_strlen(ar->arr[i]); |
| 872 |
if (estr_append_quoted(s, (char *)es_getBufAddr(ar->arr[i]), elen) != 0) return -1; |
| 873 |
} |
| 874 |
if (estr_append_char(s, ']') != 0) return -1; |
| 875 |
} else { |
| 876 |
if (estr_append_cstr(s, "\"\"", 2) != 0) return -1; |
| 877 |
} |
| 878 |
} |
| 879 |
return 0; |
| 880 |
} |
| 881 |
|
| 882 |
/* Parse the sub-mapping for a foreach: statement. |
| 883 |
* MAPPING_START already consumed. Fills *p_var, *p_in, *p_body (all |
| 884 |
* owned by caller; freed via the finalize_it: of build_one_stmt_rs). */ |
| 885 |
static rsRetVal parse_foreach_mapping( |
| 886 |
yaml_parser_t *parser, const char *fname, char **p_var, char **p_in, es_str_t **p_body) { |
| 887 |
yaml_event_t ev; |
| 888 |
DEFiRet; |
| 889 |
|
| 890 |
while (1) { |
| 891 |
CHKiRet(next_event(parser, &ev, fname)); |
| 892 |
if (ev.type == YAML_MAPPING_END_EVENT) { |
| 893 |
yaml_event_delete(&ev); |
| 894 |
break; |
| 895 |
} |
| 896 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 897 |
yaml_event_delete(&ev); |
| 898 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 899 |
} |
| 900 |
char *fk = strdup((char *)ev.data.scalar.value); |
| 901 |
yaml_event_delete(&ev); |
| 902 |
if (fk == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 903 |
|
| 904 |
if (!strcmp(fk, "var") || !strcmp(fk, "in")) { |
| 905 |
yaml_event_t fv; |
| 906 |
rsRetVal fret = next_event(parser, &fv, fname); |
| 907 |
if (fret != RS_RET_OK) { |
| 908 |
free(fk); |
| 909 |
ABORT_FINALIZE(fret); |
| 910 |
} |
| 911 |
if (fv.type == YAML_SCALAR_EVENT) { |
| 912 |
char **dst = !strcmp(fk, "var") ? p_var : p_in; |
| 913 |
free(*dst); |
| 914 |
*dst = strdup((char *)fv.data.scalar.value); |
| 915 |
if (*dst == NULL) { |
| 916 |
yaml_event_delete(&fv); |
| 917 |
free(fk); |
| 918 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 919 |
} |
| 920 |
} |
| 921 |
yaml_event_delete(&fv); |
| 922 |
} else if (!strcmp(fk, "do")) { |
| 923 |
yaml_event_t sev; |
| 924 |
rsRetVal sret = next_event(parser, &sev, fname); |
| 925 |
if (sret != RS_RET_OK) { |
| 926 |
free(fk); |
| 927 |
ABORT_FINALIZE(sret); |
| 928 |
} |
| 929 |
if (sev.type != YAML_SEQUENCE_START_EVENT) { |
| 930 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: foreach 'do' must be a sequence of statements", |
| 931 |
fname); |
| 932 |
yaml_event_delete(&sev); |
| 933 |
free(fk); |
| 934 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 935 |
} |
| 936 |
yaml_event_delete(&sev); |
| 937 |
es_deleteStr(*p_body); |
| 938 |
*p_body = es_newStr(64); |
| 939 |
if (*p_body == NULL) { |
| 940 |
free(fk); |
| 941 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 942 |
} |
| 943 |
rsRetVal bret = build_stmts_rs(parser, p_body, fname); |
| 944 |
if (bret != RS_RET_OK) { |
| 945 |
free(fk); |
| 946 |
ABORT_FINALIZE(bret); |
| 947 |
} |
| 948 |
} else { |
| 949 |
/* skip unknown foreach sub-key */ |
| 950 |
yaml_event_t fv; |
| 951 |
rsRetVal fret = next_event(parser, &fv, fname); |
| 952 |
if (fret == RS_RET_OK) yaml_event_delete(&fv); |
| 953 |
} |
| 954 |
free(fk); |
| 955 |
} |
| 956 |
|
| 957 |
finalize_it: |
| 958 |
RETiRet; |
| 959 |
} |
| 960 |
|
| 961 |
/* Parse the sub-mapping for a set: statement. |
| 962 |
* MAPPING_START already consumed. Fills *p_var and *p_expr. */ |
| 963 |
static rsRetVal parse_set_subkeys(yaml_parser_t *parser, const char *fname, char **p_var, char **p_expr) { |
| 964 |
yaml_event_t ev; |
| 965 |
DEFiRet; |
| 966 |
|
| 967 |
while (1) { |
| 968 |
CHKiRet(next_event(parser, &ev, fname)); |
| 969 |
if (ev.type == YAML_MAPPING_END_EVENT) { |
| 970 |
yaml_event_delete(&ev); |
| 971 |
break; |
| 972 |
} |
| 973 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 974 |
yaml_event_delete(&ev); |
| 975 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 976 |
} |
| 977 |
char *sk = strdup((char *)ev.data.scalar.value); |
| 978 |
yaml_event_delete(&ev); |
| 979 |
if (sk == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 980 |
|
| 981 |
yaml_event_t sv; |
| 982 |
rsRetVal sret = next_event(parser, &sv, fname); |
| 983 |
if (sret != RS_RET_OK) { |
| 984 |
free(sk); |
| 985 |
ABORT_FINALIZE(sret); |
| 986 |
} |
| 987 |
if (sv.type == YAML_SCALAR_EVENT) { |
| 988 |
char **dst = !strcmp(sk, "var") ? p_var : (!strcmp(sk, "expr") ? p_expr : NULL); |
| 989 |
if (dst != NULL) { |
| 990 |
free(*dst); |
| 991 |
*dst = strdup((char *)sv.data.scalar.value); |
| 992 |
if (*dst == NULL) { |
| 993 |
yaml_event_delete(&sv); |
| 994 |
free(sk); |
| 995 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 996 |
} |
| 997 |
} |
| 998 |
} |
| 999 |
yaml_event_delete(&sv); |
| 1000 |
free(sk); |
| 1001 |
} |
| 1002 |
|
| 1003 |
finalize_it: |
| 1004 |
RETiRet; |
| 1005 |
} |
| 1006 |
|
| 1007 |
/* Build RainerScript for one statement mapping. MAPPING_START already consumed. |
| 1008 |
* Appends to *s. |
| 1009 |
* |
| 1010 |
* Design note: this function intentionally uses a two-phase collect-then-emit |
| 1011 |
* pattern rather than being split further. The statement type cannot be |
| 1012 |
* determined until all keys in the YAML mapping have been read (e.g. an |
| 1013 |
* item with only 'type:' is a flat action, but one with 'if:' and 'action:' |
| 1014 |
* is an if/action shorthand). Splitting collect from emit would require a |
| 1015 |
* ~15-field context struct with its own cleanup, adding boilerplate without |
| 1016 |
* clarity. The foreach and set sub-mapping parsers are extracted into |
| 1017 |
* parse_foreach_mapping() and parse_set_subkeys() to keep nesting shallow. */ |
| 1018 |
static rsRetVal build_one_stmt_rs(yaml_parser_t *parser, es_str_t **s, const char *fname) { |
| 1019 |
yaml_event_t ev; |
| 1020 |
DEFiRet; |
| 1021 |
|
| 1022 |
/* Collect all top-level keys from this mapping so we can determine the |
| 1023 |
* statement type. Some values (then:/else:/action:/set:) are nested |
| 1024 |
* sequences or mappings that we handle inline. */ |
| 1025 |
char *if_expr = NULL; /* value of if: */ |
| 1026 |
char *call_name = NULL; /* value of call: */ |
| 1027 |
char *call_indirect_expr = NULL; /* value of call_indirect: */ |
| 1028 |
char *set_var = NULL; /* value of set.var */ |
| 1029 |
char *set_expr = NULL; /* value of set.expr */ |
| 1030 |
char *unset_var = NULL; /* value of unset: */ |
| 1031 |
char *foreach_var = NULL; /* value of foreach.var */ |
| 1032 |
char *foreach_in = NULL; /* value of foreach.in */ |
| 1033 |
es_str_t *foreach_body = NULL; /* compiled body of foreach.do */ |
| 1034 |
es_str_t *then_rs = NULL; |
| 1035 |
es_str_t *else_rs = NULL; |
| 1036 |
es_str_t *action_rs = NULL; /* action: nested mapping → serialised RS */ |
| 1037 |
struct nvlst *act_lst = NULL; /* for flat action (type: key at top level) */ |
| 1038 |
int has_stop = 0; |
| 1039 |
int has_continue = 0; |
| 1040 |
int has_if = 0; |
| 1041 |
int has_call = 0; |
| 1042 |
int has_call_indirect = 0; |
| 1043 |
int has_set = 0; |
| 1044 |
int has_unset = 0; |
| 1045 |
int has_foreach = 0; |
| 1046 |
int has_type = 0; /* flat action form */ |
| 1047 |
/* reload_lookup_table: uses positional args: ("tableName"[, "stubValue"]) */ |
| 1048 |
char *rlt_table = NULL; |
| 1049 |
char *rlt_stub = NULL; |
| 1050 |
int has_rlt = 0; |
| 1051 |
|
| 1052 |
while (1) { |
| 1053 |
CHKiRet(next_event(parser, &ev, fname)); |
| 1054 |
if (ev.type == YAML_MAPPING_END_EVENT) { |
| 1055 |
yaml_event_delete(&ev); |
| 1056 |
break; |
| 1057 |
} |
| 1058 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 1059 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: expected scalar key in statements item", fname); |
| 1060 |
yaml_event_delete(&ev); |
| 1061 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1062 |
} |
| 1063 |
char *kname = strdup((char *)ev.data.scalar.value); |
| 1064 |
yaml_event_delete(&ev); |
| 1065 |
if (kname == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1066 |
|
| 1067 |
if (!strcmp(kname, "if")) { |
| 1068 |
/* value: scalar expression string */ |
| 1069 |
yaml_event_t vev; |
| 1070 |
CHKiRet_Hdlr(next_event(parser, &vev, fname)) { |
| 1071 |
free(kname); |
| 1072 |
FINALIZE; |
| 1073 |
} |
| 1074 |
if (vev.type != YAML_SCALAR_EVENT) { |
| 1075 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: 'if' value must be a scalar expression", fname); |
| 1076 |
yaml_event_delete(&vev); |
| 1077 |
free(kname); |
| 1078 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1079 |
} |
| 1080 |
free(if_expr); |
| 1081 |
if_expr = strdup((char *)vev.data.scalar.value); |
| 1082 |
yaml_event_delete(&vev); |
| 1083 |
has_if = 1; |
| 1084 |
free(kname); |
| 1085 |
if (if_expr == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1086 |
|
| 1087 |
} else if (!strcmp(kname, "then") || !strcmp(kname, "else")) { |
| 1088 |
/* value: sequence of statement items */ |
| 1089 |
int is_else = (kname[0] == 'e'); |
| 1090 |
free(kname); |
| 1091 |
yaml_event_t sev; |
| 1092 |
CHKiRet(next_event(parser, &sev, fname)); |
| 1093 |
if (sev.type != YAML_SEQUENCE_START_EVENT) { |
| 1094 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1095 |
"yamlconf: %s: 'then'/'else' value must be a sequence of statements", fname); |
| 1096 |
yaml_event_delete(&sev); |
| 1097 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1098 |
} |
| 1099 |
yaml_event_delete(&sev); |
| 1100 |
es_str_t *branch_rs = es_newStr(64); |
| 1101 |
if (branch_rs == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1102 |
rsRetVal bret = build_stmts_rs(parser, &branch_rs, fname); |
| 1103 |
if (bret != RS_RET_OK) { |
| 1104 |
es_deleteStr(branch_rs); |
| 1105 |
ABORT_FINALIZE(bret); |
| 1106 |
} |
| 1107 |
if (is_else) { |
| 1108 |
es_deleteStr(else_rs); |
| 1109 |
else_rs = branch_rs; |
| 1110 |
} else { |
| 1111 |
es_deleteStr(then_rs); |
| 1112 |
then_rs = branch_rs; |
| 1113 |
} |
| 1114 |
|
| 1115 |
} else if (!strcmp(kname, "action")) { |
| 1116 |
/* value: mapping with action params (type: + others) */ |
| 1117 |
free(kname); |
| 1118 |
yaml_event_t mev; |
| 1119 |
CHKiRet(next_event(parser, &mev, fname)); |
| 1120 |
if (mev.type != YAML_MAPPING_START_EVENT) { |
| 1121 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1122 |
"yamlconf: %s: 'action' value must be a mapping of action parameters", fname); |
| 1123 |
yaml_event_delete(&mev); |
| 1124 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1125 |
} |
| 1126 |
yaml_event_delete(&mev); |
| 1127 |
struct nvlst *alst = NULL; |
| 1128 |
rsRetVal aret = build_nvlst_from_mapping(parser, &alst, fname); |
| 1129 |
if (aret != RS_RET_OK) ABORT_FINALIZE(aret); |
| 1130 |
/* serialise to action(...) string */ |
| 1131 |
es_deleteStr(action_rs); |
| 1132 |
action_rs = es_newStr(128); |
| 1133 |
if (action_rs == NULL) { |
| 1134 |
nvlstDestruct(alst); |
| 1135 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1136 |
} |
| 1137 |
if (estr_append_cstr(&action_rs, "action(", 7) != 0 || append_nvlst_as_rs_params(&action_rs, alst) != 0 || |
| 1138 |
estr_append_cstr(&action_rs, ")\n", 2) != 0) { |
| 1139 |
nvlstDestruct(alst); |
| 1140 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1141 |
} |
| 1142 |
nvlstDestruct(alst); |
| 1143 |
|
| 1144 |
} else if (!strcmp(kname, "stop")) { |
| 1145 |
free(kname); |
| 1146 |
/* Only emit stop if value is truthy. Canonical YAML falsy values |
| 1147 |
* (false/no/off/0) must NOT trigger the statement — stop: false is |
| 1148 |
* a valid way to conditionally disable a stop. Non-scalar values |
| 1149 |
* are a config error. */ |
| 1150 |
yaml_event_t vev; |
| 1151 |
CHKiRet(next_event(parser, &vev, fname)); |
| 1152 |
if (vev.type != YAML_SCALAR_EVENT) { |
| 1153 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: 'stop' value must be a scalar (true/false)", fname); |
| 1154 |
yaml_event_delete(&vev); |
| 1155 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1156 |
} |
| 1157 |
if (strcasecmp((char *)vev.data.scalar.value, "false") != 0 && |
| 1158 |
strcasecmp((char *)vev.data.scalar.value, "no") != 0 && |
| 1159 |
strcasecmp((char *)vev.data.scalar.value, "off") != 0 && |
| 1160 |
strcmp((char *)vev.data.scalar.value, "0") != 0) { |
| 1161 |
has_stop = 1; |
| 1162 |
} |
| 1163 |
yaml_event_delete(&vev); |
| 1164 |
|
| 1165 |
} else if (!strcmp(kname, "continue")) { |
| 1166 |
free(kname); |
| 1167 |
yaml_event_t vev; |
| 1168 |
CHKiRet(next_event(parser, &vev, fname)); |
| 1169 |
if (vev.type != YAML_SCALAR_EVENT) { |
| 1170 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: 'continue' value must be a scalar (true/false)", |
| 1171 |
fname); |
| 1172 |
yaml_event_delete(&vev); |
| 1173 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1174 |
} |
| 1175 |
if (strcasecmp((char *)vev.data.scalar.value, "false") != 0 && |
| 1176 |
strcasecmp((char *)vev.data.scalar.value, "no") != 0 && |
| 1177 |
strcasecmp((char *)vev.data.scalar.value, "off") != 0 && |
| 1178 |
strcmp((char *)vev.data.scalar.value, "0") != 0) { |
| 1179 |
has_continue = 1; |
| 1180 |
} |
| 1181 |
yaml_event_delete(&vev); |
| 1182 |
|
| 1183 |
} else if (!strcmp(kname, "call")) { |
| 1184 |
free(kname); |
| 1185 |
yaml_event_t vev; |
| 1186 |
CHKiRet(next_event(parser, &vev, fname)); |
| 1187 |
if (vev.type != YAML_SCALAR_EVENT) { |
| 1188 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: 'call' value must be a ruleset name", fname); |
| 1189 |
yaml_event_delete(&vev); |
| 1190 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1191 |
} |
| 1192 |
free(call_name); |
| 1193 |
call_name = strdup((char *)vev.data.scalar.value); |
| 1194 |
yaml_event_delete(&vev); |
| 1195 |
has_call = 1; |
| 1196 |
if (call_name == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1197 |
|
| 1198 |
} else if (!strcmp(kname, "call_indirect")) { |
| 1199 |
free(kname); |
| 1200 |
yaml_event_t vev; |
| 1201 |
CHKiRet(next_event(parser, &vev, fname)); |
| 1202 |
if (vev.type != YAML_SCALAR_EVENT) { |
| 1203 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1204 |
"yamlconf: %s: 'call_indirect' value must be a variable expression", fname); |
| 1205 |
yaml_event_delete(&vev); |
| 1206 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1207 |
} |
| 1208 |
free(call_indirect_expr); |
| 1209 |
call_indirect_expr = strdup((char *)vev.data.scalar.value); |
| 1210 |
yaml_event_delete(&vev); |
| 1211 |
has_call_indirect = 1; |
| 1212 |
if (call_indirect_expr == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1213 |
|
| 1214 |
} else if (!strcmp(kname, "unset")) { |
| 1215 |
free(kname); |
| 1216 |
yaml_event_t vev; |
| 1217 |
CHKiRet(next_event(parser, &vev, fname)); |
| 1218 |
if (vev.type != YAML_SCALAR_EVENT) { |
| 1219 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: 'unset' value must be a variable name", fname); |
| 1220 |
yaml_event_delete(&vev); |
| 1221 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1222 |
} |
| 1223 |
free(unset_var); |
| 1224 |
unset_var = strdup((char *)vev.data.scalar.value); |
| 1225 |
yaml_event_delete(&vev); |
| 1226 |
has_unset = 1; |
| 1227 |
if (unset_var == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1228 |
|
| 1229 |
} else if (!strcmp(kname, "foreach")) { |
| 1230 |
free(kname); |
| 1231 |
has_foreach = 1; |
| 1232 |
yaml_event_t mev; |
| 1233 |
CHKiRet(next_event(parser, &mev, fname)); |
| 1234 |
if (mev.type != YAML_MAPPING_START_EVENT) { |
| 1235 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1236 |
"yamlconf: %s: 'foreach' value must be a mapping with var:, in:, and do: keys", fname); |
| 1237 |
yaml_event_delete(&mev); |
| 1238 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1239 |
} |
| 1240 |
yaml_event_delete(&mev); |
| 1241 |
CHKiRet(parse_foreach_mapping(parser, fname, &foreach_var, &foreach_in, &foreach_body)); |
| 1242 |
|
| 1243 |
} else if (!strcmp(kname, "set")) { |
| 1244 |
free(kname); |
| 1245 |
has_set = 1; |
| 1246 |
yaml_event_t mev; |
| 1247 |
CHKiRet(next_event(parser, &mev, fname)); |
| 1248 |
if (mev.type != YAML_MAPPING_START_EVENT) { |
| 1249 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1250 |
"yamlconf: %s: 'set' value must be a mapping with var: and expr: keys", fname); |
| 1251 |
yaml_event_delete(&mev); |
| 1252 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1253 |
} |
| 1254 |
yaml_event_delete(&mev); |
| 1255 |
CHKiRet(parse_set_subkeys(parser, fname, &set_var, &set_expr)); |
| 1256 |
|
| 1257 |
} else if (!strcmp(kname, "reload_lookup_table")) { |
| 1258 |
free(kname); |
| 1259 |
has_rlt = 1; |
| 1260 |
yaml_event_t mev; |
| 1261 |
CHKiRet(next_event(parser, &mev, fname)); |
| 1262 |
if (mev.type != YAML_MAPPING_START_EVENT) { |
| 1263 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1264 |
"yamlconf: %s: 'reload_lookup_table' value must be a mapping with at " |
| 1265 |
"least a 'table' key", |
| 1266 |
fname); |
| 1267 |
yaml_event_delete(&mev); |
| 1268 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1269 |
} |
| 1270 |
yaml_event_delete(&mev); |
| 1271 |
/* Extract positional args: table (required) and stub_value (optional). |
| 1272 |
* The RainerScript syntax is reload_lookup_table("name"[, "stub"]) |
| 1273 |
* so we cannot use append_nvlst_as_rs_params(). */ |
| 1274 |
struct nvlst *rlt_nl = NULL; |
| 1275 |
rsRetVal rlt_r = build_nvlst_from_mapping(parser, &rlt_nl, fname); |
| 1276 |
if (rlt_r != RS_RET_OK) ABORT_FINALIZE(rlt_r); |
| 1277 |
for (struct nvlst *n = rlt_nl; n != NULL; n = n->next) { |
| 1278 |
if (n->val.datatype != 'S') continue; |
| 1279 |
/* es_getBufAddr() is NOT null-terminated; use es_str2cstr() */ |
| 1280 |
char *nkey = es_str2cstr(n->name, NULL); |
| 1281 |
char *nval = es_str2cstr(n->val.d.estr, NULL); |
| 1282 |
if (nkey != NULL && nval != NULL) { |
| 1283 |
if (!strcmp(nkey, "table")) { |
| 1284 |
free(rlt_table); |
| 1285 |
rlt_table = strdup(nval); |
| 1286 |
} else if (!strcmp(nkey, "stub_value")) { |
| 1287 |
free(rlt_stub); |
| 1288 |
rlt_stub = strdup(nval); |
| 1289 |
} |
| 1290 |
} |
| 1291 |
free(nkey); |
| 1292 |
free(nval); |
| 1293 |
} |
| 1294 |
nvlstDestruct(rlt_nl); |
| 1295 |
if (rlt_table == NULL && has_rlt) { |
| 1296 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: reload_lookup_table: 'table' key is required", |
| 1297 |
fname); |
| 1298 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1299 |
} |
| 1300 |
|
| 1301 |
} else { |
| 1302 |
/* Unknown key at statement level: treat as action param (flat action form). |
| 1303 |
* Collect into act_lst so we can emit action(...) at the end. */ |
| 1304 |
has_type = has_type || (!strcmp(kname, "type") ? 1 : 0); |
| 1305 |
yaml_event_t vev; |
| 1306 |
rsRetVal next_ret = next_event(parser, &vev, fname); |
| 1307 |
if (next_ret != RS_RET_OK) { |
| 1308 |
free(kname); |
| 1309 |
ABORT_FINALIZE(next_ret); |
| 1310 |
} |
| 1311 |
struct nvlst *node = NULL; |
| 1312 |
rsRetVal nret = yaml_value_to_nvlst_node(parser, fname, &vev, kname, &node); |
| 1313 |
free(kname); |
| 1314 |
kname = NULL; |
| 1315 |
if (nret != RS_RET_OK) ABORT_FINALIZE(nret); |
| 1316 |
node->next = act_lst; |
| 1317 |
act_lst = node; |
| 1318 |
} |
| 1319 |
} /* end mapping loop */ |
| 1320 |
|
| 1321 |
/* ---- Generate RainerScript from collected fields ---- */ |
| 1322 |
|
| 1323 |
if (has_if) { |
| 1324 |
/* if (<expr>) then { ... } [else { ... }] */ |
| 1325 |
if (estr_append_cstr(s, "if (", 4) != 0 || estr_append_cstr(s, if_expr, strlen(if_expr)) != 0 || |
| 1326 |
estr_append_cstr(s, ") then {\n", 9) != 0) |
| 1327 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1328 |
|
| 1329 |
/* then branch: either action_rs (from action: key) or then_rs (from then: key) */ |
| 1330 |
if (action_rs != NULL) { |
| 1331 |
if (es_addStr(s, action_rs) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1332 |
} else if (then_rs != NULL) { |
| 1333 |
if (es_addStr(s, then_rs) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1334 |
} |
| 1335 |
|
| 1336 |
if (estr_append_char(s, '}') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1337 |
|
| 1338 |
if (else_rs != NULL) { |
| 1339 |
if (estr_append_cstr(s, " else {\n", 8) != 0 || es_addStr(s, else_rs) != 0 || estr_append_char(s, '}') != 0) |
| 1340 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1341 |
} |
| 1342 |
if (estr_append_char(s, '\n') != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1343 |
|
| 1344 |
} else if (has_stop) { |
| 1345 |
if (estr_append_cstr(s, "stop\n", 5) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1346 |
|
| 1347 |
} else if (has_continue) { |
| 1348 |
if (estr_append_cstr(s, "continue\n", 9) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1349 |
|
| 1350 |
} else if (has_call) { |
| 1351 |
if (estr_append_cstr(s, "call ", 5) != 0 || estr_append_cstr(s, call_name, strlen(call_name)) != 0 || |
| 1352 |
estr_append_char(s, '\n') != 0) |
| 1353 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1354 |
|
| 1355 |
} else if (has_call_indirect) { |
| 1356 |
if (estr_append_cstr(s, "call_indirect ", 14) != 0 || |
| 1357 |
estr_append_cstr(s, call_indirect_expr, strlen(call_indirect_expr)) != 0 || |
| 1358 |
estr_append_cstr(s, ";\n", 2) != 0) |
| 1359 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1360 |
|
| 1361 |
} else if (has_unset) { |
| 1362 |
if (estr_append_cstr(s, "unset ", 6) != 0 || estr_append_cstr(s, unset_var, strlen(unset_var)) != 0 || |
| 1363 |
estr_append_cstr(s, ";\n", 2) != 0) |
| 1364 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1365 |
|
| 1366 |
} else if (has_foreach) { |
| 1367 |
if (foreach_var == NULL || foreach_in == NULL || foreach_body == NULL) { |
| 1368 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: foreach: requires 'var', 'in', and 'do' keys", fname); |
| 1369 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1370 |
} |
| 1371 |
if (estr_append_cstr(s, "foreach (", 9) != 0 || estr_append_cstr(s, foreach_var, strlen(foreach_var)) != 0 || |
| 1372 |
estr_append_cstr(s, " in ", 4) != 0 || estr_append_cstr(s, foreach_in, strlen(foreach_in)) != 0 || |
| 1373 |
estr_append_cstr(s, ") do {\n", 7) != 0 || es_addStr(s, foreach_body) != 0 || |
| 1374 |
estr_append_cstr(s, "}\n", 2) != 0) |
| 1375 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1376 |
|
| 1377 |
} else if (has_set) { |
| 1378 |
if (set_var == NULL || set_expr == NULL) { |
| 1379 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: set: requires both 'var' and 'expr' keys", fname); |
| 1380 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1381 |
} |
| 1382 |
if (estr_append_cstr(s, "set ", 4) != 0 || estr_append_cstr(s, set_var, strlen(set_var)) != 0 || |
| 1383 |
estr_append_cstr(s, " = ", 3) != 0 || estr_append_cstr(s, set_expr, strlen(set_expr)) != 0 || |
| 1384 |
estr_append_cstr(s, ";\n", 2) != 0) |
| 1385 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1386 |
|
| 1387 |
} else if (has_type || act_lst != NULL) { |
| 1388 |
/* Flat action: item with type: key directly at statement level */ |
| 1389 |
if (estr_append_cstr(s, "action(", 7) != 0 || append_nvlst_as_rs_params(s, act_lst) != 0 || |
| 1390 |
estr_append_cstr(s, ")\n", 2) != 0) |
| 1391 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1392 |
|
| 1393 |
} else if (has_rlt) { |
| 1394 |
/* Emit: reload_lookup_table("tableName"[, "stubValue"]) */ |
| 1395 |
if (estr_append_cstr(s, "reload_lookup_table(", 20) != 0 || |
| 1396 |
estr_append_quoted(s, rlt_table, strlen(rlt_table)) != 0) |
| 1397 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1398 |
if (rlt_stub != NULL) { |
| 1399 |
if (estr_append_cstr(s, ", ", 2) != 0 || estr_append_quoted(s, rlt_stub, strlen(rlt_stub)) != 0) |
| 1400 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1401 |
} |
| 1402 |
if (estr_append_cstr(s, ")\n", 2) != 0) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1403 |
|
| 1404 |
} else { |
| 1405 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1406 |
"yamlconf: %s: unrecognised statement item in statements: " |
| 1407 |
"(expected if/type/stop/continue/call/call_indirect/unset/set/foreach/reload_lookup_table)", |
| 1408 |
fname); |
| 1409 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1410 |
} |
| 1411 |
|
| 1412 |
finalize_it: |
| 1413 |
free(if_expr); |
| 1414 |
free(call_name); |
| 1415 |
free(call_indirect_expr); |
| 1416 |
free(unset_var); |
| 1417 |
free(foreach_var); |
| 1418 |
free(foreach_in); |
| 1419 |
free(set_var); |
| 1420 |
free(set_expr); |
| 1421 |
es_deleteStr(then_rs); |
| 1422 |
es_deleteStr(else_rs); |
| 1423 |
es_deleteStr(action_rs); |
| 1424 |
es_deleteStr(foreach_body); |
| 1425 |
nvlstDestruct(act_lst); |
| 1426 |
free(rlt_table); |
| 1427 |
free(rlt_stub); |
| 1428 |
RETiRet; |
| 1429 |
} |
| 1430 |
|
| 1431 |
/* Parse a statements: sequence and append the equivalent RainerScript to *s. |
| 1432 |
* SEQUENCE_START must already be consumed by the caller. |
| 1433 |
* |
| 1434 |
* This function is RECURSIVE: build_one_stmt_rs() calls build_stmts_rs() for |
| 1435 |
* then:/else: branch bodies, and parse_foreach_mapping() calls it for do: |
| 1436 |
* bodies. Recursion depth is bounded by YAML nesting depth which is shallow |
| 1437 |
* in practice (foreach inside if is the deepest realistic case). */ |
| 1438 |
static rsRetVal build_stmts_rs(yaml_parser_t *parser, es_str_t **s, const char *fname) { |
| 1439 |
yaml_event_t ev; |
| 1440 |
DEFiRet; |
| 1441 |
|
| 1442 |
while (1) { |
| 1443 |
CHKiRet(next_event(parser, &ev, fname)); |
| 1444 |
if (ev.type == YAML_SEQUENCE_END_EVENT) { |
| 1445 |
yaml_event_delete(&ev); |
| 1446 |
break; |
| 1447 |
} |
| 1448 |
if (ev.type != YAML_MAPPING_START_EVENT) { |
| 1449 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: each item in a statements: sequence must be a mapping", |
| 1450 |
fname); |
| 1451 |
yaml_event_delete(&ev); |
| 1452 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1453 |
} |
| 1454 |
yaml_event_delete(&ev); |
| 1455 |
CHKiRet(build_one_stmt_rs(parser, s, fname)); |
| 1456 |
} |
| 1457 |
|
| 1458 |
finalize_it: |
| 1459 |
RETiRet; |
| 1460 |
} |
| 1461 |
|
| 1462 |
/* Parse the rulesets: sequence. |
| 1463 |
* |
| 1464 |
* Each item is a mapping with at least a "name" key. The special "script" |
| 1465 |
* key causes a complete ruleset(params) { script } RainerScript block to |
| 1466 |
* be synthesised and pushed onto the flex buffer stack via |
| 1467 |
* cnfAddConfigBuffer(); the ongoing yyparse() then processes it naturally. |
| 1468 |
* |
| 1469 |
* The "statements" key is the YAML-native alternative to "script". It takes |
| 1470 |
* a sequence of statement mappings (if/action/stop/continue/call/set) that are |
| 1471 |
* translated to an equivalent RainerScript body and then processed via the |
| 1472 |
* same cnfAddConfigBuffer() path. Only the filter expression in "if:" remains |
| 1473 |
* as a RainerScript expression string; action parameters and control-flow are |
| 1474 |
* expressed as YAML keys. script:/statements: and filter:/actions: are all |
| 1475 |
* mutually exclusive. |
| 1476 |
* |
| 1477 |
* ORDERING NOTE: The synthesised ruleset buffer is pushed AFTER any .conf |
| 1478 |
* files that were included via the include: section (which are also pushed |
| 1479 |
* via cnfAddConfigBuffer / cnfSetLexFile). Because the flex buffer stack is |
| 1480 |
* LIFO, the ruleset script is processed BEFORE those included .conf files. |
| 1481 |
* Consequently, any templates or objects that a ruleset script references |
| 1482 |
* must be defined in the YAML file's own templates:/global:/etc. sections |
| 1483 |
* (processed immediately via cnfDoObj()), not in included .conf fragments. |
| 1484 |
* |
| 1485 |
* Structured filter shortcut (Phase 2): |
| 1486 |
* filter: "<filter-string>" # optional; "*.info" (PRI) or ":prop,op,val" (property) |
| 1487 |
* actions: # mutually exclusive with script: |
| 1488 |
* - type: omfile |
| 1489 |
* file: /var/log/messages |
| 1490 |
* When filter: + actions: are used, cnfstmt nodes are built directly via |
| 1491 |
* cnfstmtNewPRIFILT / cnfstmtNewPROPFILT / cnfstmtNewAct and set on the |
| 1492 |
* cnfobj->script field before calling cnfDoObj() — no text buffer needed. */ |
| 1493 |
static rsRetVal parse_ruleset_sequence(yaml_parser_t *parser, const char *fname) { |
| 1494 |
yaml_event_t ev; |
| 1495 |
/* Hoisted to function scope so finalize_it: can clean up on error */ |
| 1496 |
struct nvlst *lst = NULL; |
| 1497 |
char *script_str = NULL; |
| 1498 |
char *filter_str = NULL; |
| 1499 |
struct cnfstmt *actions = NULL; |
| 1500 |
char *kname = NULL; |
| 1501 |
DEFiRet; |
| 1502 |
|
| 1503 |
while (1) { |
| 1504 |
CHKiRet(next_event(parser, &ev, fname)); |
| 1505 |
if (ev.type == YAML_SEQUENCE_END_EVENT) { |
| 1506 |
yaml_event_delete(&ev); |
| 1507 |
break; |
| 1508 |
} |
| 1509 |
if (ev.type != YAML_MAPPING_START_EVENT) { |
| 1510 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: rulesets sequence item must be a mapping", fname); |
| 1511 |
yaml_event_delete(&ev); |
| 1512 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1513 |
} |
| 1514 |
yaml_event_delete(&ev); |
| 1515 |
|
| 1516 |
/* Reset per-ruleset state (all NULL at function entry or after prior iteration) */ |
| 1517 |
lst = NULL; |
| 1518 |
script_str = NULL; |
| 1519 |
filter_str = NULL; |
| 1520 |
actions = NULL; |
| 1521 |
|
| 1522 |
/* We parse the mapping manually here so we can intercept special keys */ |
| 1523 |
while (1) { |
| 1524 |
CHKiRet(next_event(parser, &ev, fname)); |
| 1525 |
if (ev.type == YAML_MAPPING_END_EVENT) { |
| 1526 |
yaml_event_delete(&ev); |
| 1527 |
break; |
| 1528 |
} |
| 1529 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 1530 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: expected scalar key in ruleset mapping", fname); |
| 1531 |
yaml_event_delete(&ev); |
| 1532 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1533 |
} |
| 1534 |
|
| 1535 |
free(kname); |
| 1536 |
kname = strdup((char *)ev.data.scalar.value); |
| 1537 |
yaml_event_delete(&ev); |
| 1538 |
if (kname == NULL) { |
| 1539 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1540 |
} |
| 1541 |
|
| 1542 |
CHKiRet(next_event(parser, &ev, fname)); |
| 1543 |
|
| 1544 |
if (!strcmp(kname, "script")) { |
| 1545 |
/* script: must be a scalar (RainerScript text) */ |
| 1546 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 1547 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1548 |
"yamlconf: %s: 'script' value must be a scalar" |
| 1549 |
" (use a YAML block scalar '|' for multi-line)", |
| 1550 |
fname); |
| 1551 |
yaml_event_delete(&ev); |
| 1552 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1553 |
} |
| 1554 |
free(script_str); |
| 1555 |
script_str = strdup((char *)ev.data.scalar.value); |
| 1556 |
yaml_event_delete(&ev); |
| 1557 |
if (script_str == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1558 |
|
| 1559 |
} else if (!strcmp(kname, "statements")) { |
| 1560 |
/* statements: sequence — YAML-native scripting. |
| 1561 |
* Translates to RainerScript and stored in script_str. */ |
| 1562 |
if (ev.type != YAML_SEQUENCE_START_EVENT) { |
| 1563 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: 'statements' value must be a sequence", fname); |
| 1564 |
yaml_event_delete(&ev); |
| 1565 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1566 |
} |
| 1567 |
yaml_event_delete(&ev); |
| 1568 |
es_str_t *stmts_estr = es_newStr(256); |
| 1569 |
if (stmts_estr == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1570 |
rsRetVal sret = build_stmts_rs(parser, &stmts_estr, fname); |
| 1571 |
if (sret != RS_RET_OK) { |
| 1572 |
es_deleteStr(stmts_estr); |
| 1573 |
ABORT_FINALIZE(sret); |
| 1574 |
} |
| 1575 |
free(script_str); |
| 1576 |
script_str = es_str2cstr(stmts_estr, NULL); |
| 1577 |
es_deleteStr(stmts_estr); |
| 1578 |
if (script_str == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1579 |
|
| 1580 |
} else if (!strcmp(kname, "filter")) { |
| 1581 |
/* filter: "<pri-filter>" or ":<property-filter>" */ |
| 1582 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 1583 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: 'filter' value must be a scalar string", fname); |
| 1584 |
yaml_event_delete(&ev); |
| 1585 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1586 |
} |
| 1587 |
free(filter_str); |
| 1588 |
filter_str = strdup((char *)ev.data.scalar.value); |
| 1589 |
yaml_event_delete(&ev); |
| 1590 |
if (filter_str == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1591 |
} else if (!strcmp(kname, "actions")) { |
| 1592 |
/* actions: [ {type: ..., ...}, ... ] */ |
| 1593 |
if (ev.type != YAML_SEQUENCE_START_EVENT) { |
| 1594 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: 'actions' value must be a sequence", fname); |
| 1595 |
yaml_event_delete(&ev); |
| 1596 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1597 |
} |
| 1598 |
yaml_event_delete(&ev); |
| 1599 |
if (actions) { |
| 1600 |
cnfstmtDestructLst(actions); |
| 1601 |
actions = NULL; |
| 1602 |
} |
| 1603 |
CHKiRet(parse_actions_sequence(parser, &actions, fname)); |
| 1604 |
} else { |
| 1605 |
/* All other keys go into the header nvlst */ |
| 1606 |
struct nvlst *node = NULL; |
| 1607 |
CHKiRet(yaml_value_to_nvlst_node(parser, fname, &ev, kname, &node)); |
| 1608 |
node->next = lst; |
| 1609 |
lst = node; |
| 1610 |
} |
| 1611 |
} /* end per-ruleset key loop */ |
| 1612 |
|
| 1613 |
/* Sanity: script:/statements: and actions: are mutually exclusive */ |
| 1614 |
if (script_str != NULL && actions != NULL) { |
| 1615 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1616 |
"yamlconf: %s: ruleset cannot have both 'script'/'statements' and 'actions';" |
| 1617 |
" use one or the other", |
| 1618 |
fname); |
| 1619 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1620 |
} |
| 1621 |
|
| 1622 |
if (script_str != NULL && *script_str != '\0') { |
| 1623 |
/* Synthesise a complete RainerScript ruleset(...) { script } |
| 1624 |
* block and push it onto the flex buffer stack. The bison parser |
| 1625 |
* will call cnfDoObj() itself after parsing the whole block, |
| 1626 |
* setting cnfobj->script correctly via rulesetProcessCnf(). */ |
| 1627 |
free(filter_str); |
| 1628 |
filter_str = NULL; |
| 1629 |
es_str_t *estr = NULL; |
| 1630 |
rsRetVal br = build_ruleset_rainerscript(lst, script_str, &estr); |
| 1631 |
nvlstDestruct(lst); |
| 1632 |
lst = NULL; |
| 1633 |
free(script_str); |
| 1634 |
script_str = NULL; |
| 1635 |
if (br != RS_RET_OK) ABORT_FINALIZE(br); |
| 1636 |
/* cnfAddConfigBuffer() takes ownership of estr */ |
| 1637 |
cnfAddConfigBuffer(estr, fname); |
| 1638 |
} else if (actions != NULL) { |
| 1639 |
/* Phase 2 structured shortcut: build cnfstmt chain directly. |
| 1640 |
* If filter: was given, wrap the action chain in a filter node. |
| 1641 |
* Otherwise the actions run unconditionally (equivalent to *.*). */ |
| 1642 |
struct cnfstmt *body = actions; |
| 1643 |
actions = NULL; |
| 1644 |
if (filter_str != NULL && *filter_str != '\0') { |
| 1645 |
struct cnfstmt *filt; |
| 1646 |
/* Property filters start with ':', PRI filters do not */ |
| 1647 |
if (filter_str[0] == ':') { |
| 1648 |
filt = cnfstmtNewPROPFILT(filter_str, body); |
| 1649 |
} else { |
| 1650 |
filt = cnfstmtNewPRIFILT(filter_str, body); |
| 1651 |
} |
| 1652 |
if (filt == NULL) { |
| 1653 |
cnfstmtDestructLst(body); |
| 1654 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1655 |
} |
| 1656 |
/* filter_str ownership taken by cnfstmt (printable field) */ |
| 1657 |
filter_str = NULL; |
| 1658 |
body = filt; |
| 1659 |
} |
| 1660 |
free(filter_str); |
| 1661 |
filter_str = NULL; |
| 1662 |
struct cnfobj *obj = cnfobjNew(CNFOBJ_RULESET, lst); |
| 1663 |
lst = NULL; |
| 1664 |
if (obj == NULL) { |
| 1665 |
cnfstmtDestructLst(body); |
| 1666 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1667 |
} |
| 1668 |
obj->script = body; |
| 1669 |
cnfDoObj(obj); |
| 1670 |
} else { |
| 1671 |
/* No script or actions — emit just the ruleset header. */ |
| 1672 |
free(script_str); |
| 1673 |
script_str = NULL; |
| 1674 |
free(filter_str); |
| 1675 |
filter_str = NULL; |
| 1676 |
struct cnfobj *obj = cnfobjNew(CNFOBJ_RULESET, lst); |
| 1677 |
lst = NULL; /* cnfobjNew takes ownership */ |
| 1678 |
if (obj == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1679 |
cnfDoObj(obj); |
| 1680 |
} |
| 1681 |
} /* end ruleset sequence loop */ |
| 1682 |
|
| 1683 |
finalize_it: |
| 1684 |
free(kname); |
| 1685 |
free(script_str); |
| 1686 |
free(filter_str); |
| 1687 |
if (actions != NULL) cnfstmtDestructLst(actions); |
| 1688 |
nvlstDestruct(lst); |
| 1689 |
RETiRet; |
| 1690 |
} |
| 1691 |
|
| 1692 |
/* Parse the include: sequence and trigger file inclusion. |
| 1693 |
* Each item is a mapping with "path" (required) and "optional" (optional). |
| 1694 |
* |
| 1695 |
* Processing order matters because the flex buffer stack is LIFO: |
| 1696 |
* - .yaml/.yml paths: cnfDoInclude() → yamlconf_load() immediately and |
| 1697 |
* recursively, so they must be processed in forward (document) order. |
| 1698 |
* - .conf / other paths: cnfDoInclude() → cnfSetLexFile() which pushes |
| 1699 |
* onto the LIFO flex buffer stack. To ensure the bison parser later |
| 1700 |
* sees them in document order (A then B then C), we must push them in |
| 1701 |
* REVERSE order (C, B, A → stack top to bottom A, B, C → pop A first). |
| 1702 |
* |
| 1703 |
* We therefore collect all items first, then do a forward pass for YAML |
| 1704 |
* files and a reverse pass for non-YAML files. |
| 1705 |
* |
| 1706 |
* ORDERING LIMITATION: when the include: list mixes .yaml and non-YAML files |
| 1707 |
* (e.g. [a.conf, b.yaml, c.conf]), YAML files are always processed before |
| 1708 |
* non-YAML files, regardless of their relative position in the document. |
| 1709 |
* This is a fundamental architectural constraint — the LIFO flex buffer stack |
| 1710 |
* cannot interleave with synchronous YAML loads. A warning is emitted when |
| 1711 |
* such a mixed list is detected. If strict order is required, use separate |
| 1712 |
* include: blocks or consolidate to a single file type. */ |
| 1713 |
static rsRetVal parse_include_sequence(yaml_parser_t *parser, const char *fname) { |
| 1714 |
yaml_event_t ev; |
| 1715 |
char *k = NULL; |
| 1716 |
char *path = NULL; /* current item path; function-scope so finalize_it can free it */ |
| 1717 |
/* Dynamic array of collected items */ |
| 1718 |
struct inc_item { |
| 1719 |
char *path; |
| 1720 |
int optional; |
| 1721 |
} *items = NULL; |
| 1722 |
int nitems = 0, cap = 0; |
| 1723 |
DEFiRet; |
| 1724 |
|
| 1725 |
/* ---- Phase 1: collect all (path, optional) pairs ---- */ |
| 1726 |
while (1) { |
| 1727 |
free(path); /* free any leftover from previous iteration */ |
| 1728 |
path = NULL; |
| 1729 |
CHKiRet(next_event(parser, &ev, fname)); |
| 1730 |
if (ev.type == YAML_SEQUENCE_END_EVENT) { |
| 1731 |
yaml_event_delete(&ev); |
| 1732 |
break; |
| 1733 |
} |
| 1734 |
if (ev.type != YAML_MAPPING_START_EVENT) { |
| 1735 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: include sequence item must be a mapping", fname); |
| 1736 |
yaml_event_delete(&ev); |
| 1737 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1738 |
} |
| 1739 |
yaml_event_delete(&ev); |
| 1740 |
|
| 1741 |
int optional = 0; |
| 1742 |
|
| 1743 |
while (1) { |
| 1744 |
CHKiRet(next_event(parser, &ev, fname)); |
| 1745 |
if (ev.type == YAML_MAPPING_END_EVENT) { |
| 1746 |
yaml_event_delete(&ev); |
| 1747 |
break; |
| 1748 |
} |
| 1749 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 1750 |
yaml_event_delete(&ev); |
| 1751 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1752 |
} |
| 1753 |
k = strdup((char *)ev.data.scalar.value); |
| 1754 |
yaml_event_delete(&ev); |
| 1755 |
if (k == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1756 |
|
| 1757 |
yaml_event_t vev; |
| 1758 |
CHKiRet(next_event(parser, &vev, fname)); |
| 1759 |
if (vev.type == YAML_SCALAR_EVENT) { |
| 1760 |
const char *v = (char *)vev.data.scalar.value; |
| 1761 |
if (!strcmp(k, "path")) { |
| 1762 |
free(path); |
| 1763 |
path = strdup(v); |
| 1764 |
} else if (!strcmp(k, "optional")) { |
| 1765 |
optional = (!strcasecmp(v, "on") || !strcasecmp(v, "yes") || !strcmp(v, "1")) ? 1 : 0; |
| 1766 |
} |
| 1767 |
yaml_event_delete(&vev); |
| 1768 |
} else { |
| 1769 |
/* Non-scalar value for an include key is a config error. |
| 1770 |
* Consume the nested structure so the parser stays in sync. */ |
| 1771 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1772 |
"yamlconf: %s: include item key '%s' must have a scalar value, skipping", fname, k); |
| 1773 |
if (vev.type == YAML_MAPPING_START_EVENT || vev.type == YAML_SEQUENCE_START_EVENT) { |
| 1774 |
yaml_event_delete(&vev); |
| 1775 |
skip_node(parser); |
| 1776 |
} else { |
| 1777 |
yaml_event_delete(&vev); |
| 1778 |
} |
| 1779 |
} |
| 1780 |
free(k); |
| 1781 |
k = NULL; |
| 1782 |
} |
| 1783 |
|
| 1784 |
if (path != NULL) { |
| 1785 |
if (nitems >= cap) { |
| 1786 |
int newcap = cap == 0 ? 8 : cap * 2; |
| 1787 |
struct inc_item *tmp = realloc(items, newcap * sizeof(*items)); |
| 1788 |
if (tmp == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1789 |
items = tmp; |
| 1790 |
cap = newcap; |
| 1791 |
} |
| 1792 |
items[nitems].path = path; |
| 1793 |
path = NULL; /* ownership transferred to items[]; finalize_it must not free */ |
| 1794 |
items[nitems].optional = optional; |
| 1795 |
nitems++; |
| 1796 |
} else { |
| 1797 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: include item missing required 'path' key", fname); |
| 1798 |
/* non-fatal: skip this item */ |
| 1799 |
} |
| 1800 |
} |
| 1801 |
|
| 1802 |
/* ---- Phase 2: warn on mixed list, then forward pass for YAML files ---- */ |
| 1803 |
/* Detect mixed .yaml / non-YAML to warn about ordering limitation. */ |
| 1804 |
int has_yaml_item = 0, has_conf_item = 0; |
| 1805 |
for (int i = 0; i < nitems; i++) { |
| 1806 |
const char *ext = strrchr(items[i].path, '.'); |
| 1807 |
if (ext != NULL && (!strcmp(ext, ".yaml") || !strcmp(ext, ".yml"))) |
| 1808 |
has_yaml_item = 1; |
| 1809 |
else |
| 1810 |
has_conf_item = 1; |
| 1811 |
} |
| 1812 |
if (has_yaml_item && has_conf_item) { |
| 1813 |
LogError(0, RS_RET_OK, |
| 1814 |
"yamlconf: %s: include: list mixes .yaml and non-YAML files; " |
| 1815 |
"YAML files are loaded first regardless of document order — " |
| 1816 |
"use separate include: blocks if strict ordering is required", |
| 1817 |
fname); |
| 1818 |
} |
| 1819 |
|
| 1820 |
for (int i = 0; i < nitems; i++) { |
| 1821 |
const char *ext = strrchr(items[i].path, '.'); |
| 1822 |
int is_yaml = (ext != NULL && (!strcmp(ext, ".yaml") || !strcmp(ext, ".yml"))); |
| 1823 |
if (!is_yaml) continue; |
| 1824 |
int iret = cnfDoInclude(items[i].path, items[i].optional); |
| 1825 |
if (iret != 0 && !items[i].optional) { |
| 1826 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: include of '%s' failed (non-optional)", fname, |
| 1827 |
items[i].path); |
| 1828 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1829 |
} |
| 1830 |
} |
| 1831 |
|
| 1832 |
/* ---- Phase 3: reverse pass — non-YAML files → LIFO stack ---- */ |
| 1833 |
/* Pushing in reverse order ensures the bison parser processes them in |
| 1834 |
* the same forward (document) order as they appear in the YAML list. */ |
| 1835 |
for (int i = nitems - 1; i >= 0; i--) { |
| 1836 |
const char *ext = strrchr(items[i].path, '.'); |
| 1837 |
int is_yaml = (ext != NULL && (!strcmp(ext, ".yaml") || !strcmp(ext, ".yml"))); |
| 1838 |
if (is_yaml) continue; |
| 1839 |
int iret = cnfDoInclude(items[i].path, items[i].optional); |
| 1840 |
if (iret != 0 && !items[i].optional) { |
| 1841 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: include of '%s' failed (non-optional)", fname, |
| 1842 |
items[i].path); |
| 1843 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1844 |
} |
| 1845 |
} |
| 1846 |
|
| 1847 |
finalize_it: |
| 1848 |
free(k); |
| 1849 |
free(path); /* NULL-safe; only non-NULL if ABORT_FINALIZE before item was stored */ |
| 1850 |
for (int i = 0; i < nitems; i++) free(items[i].path); |
| 1851 |
free(items); |
| 1852 |
RETiRet; |
| 1853 |
} |
| 1854 |
|
| 1855 |
/* -------------------------------------------------------------------------- |
| 1856 |
* Top-level dispatcher |
| 1857 |
* -------------------------------------------------------------------------- */ |
| 1858 |
|
| 1859 |
/* Map a top-level YAML key to its cnfobjType and call the right parser. |
| 1860 |
* The opening event for the section value (MAPPING_START or SEQUENCE_START) |
| 1861 |
* has already been consumed when this function is called. */ |
| 1862 |
static rsRetVal process_top_level(yaml_parser_t *parser, const char *key, const char *fname) { |
| 1863 |
DEFiRet; |
| 1864 |
|
| 1865 |
if (!strcmp(key, "global")) { |
| 1866 |
CHKiRet(parse_singleton_obj(parser, CNFOBJ_GLOBAL, fname)); |
| 1867 |
} else if (!strcmp(key, "mainqueue") || !strcmp(key, "main_queue")) { |
| 1868 |
CHKiRet(parse_singleton_obj(parser, CNFOBJ_MAINQ, fname)); |
| 1869 |
} else if (!strcmp(key, "modules") || !strcmp(key, "testbench_modules")) { |
| 1870 |
CHKiRet(parse_obj_sequence(parser, CNFOBJ_MODULE, fname)); |
| 1871 |
} else if (!strcmp(key, "inputs")) { |
| 1872 |
CHKiRet(parse_obj_sequence(parser, CNFOBJ_INPUT, fname)); |
| 1873 |
} else if (!strcmp(key, "templates")) { |
| 1874 |
CHKiRet(parse_template_sequence(parser, fname)); |
| 1875 |
} else if (!strcmp(key, "rulesets")) { |
| 1876 |
CHKiRet(parse_ruleset_sequence(parser, fname)); |
| 1877 |
} else if (!strcmp(key, "parsers")) { |
| 1878 |
CHKiRet(parse_obj_sequence(parser, CNFOBJ_PARSER, fname)); |
| 1879 |
} else if (!strcmp(key, "lookup_tables")) { |
| 1880 |
CHKiRet(parse_obj_sequence(parser, CNFOBJ_LOOKUP_TABLE, fname)); |
| 1881 |
} else if (!strcmp(key, "dyn_stats")) { |
| 1882 |
CHKiRet(parse_obj_sequence(parser, CNFOBJ_DYN_STATS, fname)); |
| 1883 |
} else if (!strcmp(key, "perctile_stats")) { |
| 1884 |
CHKiRet(parse_obj_sequence(parser, CNFOBJ_PERCTILE_STATS, fname)); |
| 1885 |
} else if (!strcmp(key, "ratelimits")) { |
| 1886 |
CHKiRet(parse_obj_sequence(parser, CNFOBJ_RATELIMIT, fname)); |
| 1887 |
} else if (!strcmp(key, "timezones")) { |
| 1888 |
CHKiRet(parse_obj_sequence(parser, CNFOBJ_TIMEZONE, fname)); |
| 1889 |
} else if (!strcmp(key, "include")) { |
| 1890 |
CHKiRet(parse_include_sequence(parser, fname)); |
| 1891 |
} else if (!strcmp(key, "version")) { |
| 1892 |
/* Informational; skip the scalar value */ |
| 1893 |
yaml_event_t ev; |
| 1894 |
if (next_event(parser, &ev, fname) == RS_RET_OK) yaml_event_delete(&ev); |
| 1895 |
} else { |
| 1896 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: unknown top-level key '%s' – ignoring", fname, key); |
| 1897 |
/* Skip the value node so the parser stays in sync */ |
| 1898 |
yaml_event_t ev; |
| 1899 |
if (next_event(parser, &ev, fname) == RS_RET_OK) { |
| 1900 |
if (ev.type == YAML_MAPPING_START_EVENT || ev.type == YAML_SEQUENCE_START_EVENT) { |
| 1901 |
yaml_event_delete(&ev); |
| 1902 |
skip_node(parser); |
| 1903 |
} else { |
| 1904 |
yaml_event_delete(&ev); |
| 1905 |
} |
| 1906 |
} |
| 1907 |
} |
| 1908 |
|
| 1909 |
finalize_it: |
| 1910 |
RETiRet; |
| 1911 |
} |
| 1912 |
|
| 1913 |
/* -------------------------------------------------------------------------- |
| 1914 |
* Public entry point |
| 1915 |
* -------------------------------------------------------------------------- */ |
| 1916 |
|
| 1917 |
/** |
| 1918 |
* @brief Load and process a YAML rsyslog configuration file. |
| 1919 |
* |
| 1920 |
* Opens @p fname, drives the libyaml event parser over the top-level |
| 1921 |
* mapping, and dispatches each section to the appropriate handler. |
| 1922 |
* All resulting config objects are committed to rsconf via cnfDoObj() or |
| 1923 |
* cnfAddConfigBuffer() before this function returns. |
| 1924 |
* |
| 1925 |
* @param fname Absolute or relative path to the .yaml/.yml config file. |
| 1926 |
* @return RS_RET_OK on success; RS_RET_FILE_NOT_FOUND if @p fname cannot |
| 1927 |
* be opened; RS_RET_CONF_PARSE_ERROR on any YAML or semantic error. |
| 1928 |
*/ |
| 1929 |
rsRetVal yamlconf_load(const char *fname) { |
| 1930 |
yaml_parser_t parser; |
| 1931 |
yaml_event_t ev; |
| 1932 |
int parserInit = 0; |
| 1933 |
FILE *fh = NULL; |
| 1934 |
#define YAMLCONF_MAX_TOPKEYS 32 |
| 1935 |
char *seen_keys[YAMLCONF_MAX_TOPKEYS]; |
| 1936 |
int seen_count = 0; |
| 1937 |
DEFiRet; |
| 1938 |
char *prev_cnfcurrfn = cnfcurrfn; |
| 1939 |
|
| 1940 |
cnfcurrfn = (char *)fname; |
| 1941 |
|
| 1942 |
fh = fopen(fname, "r"); |
| 1943 |
if (fh == NULL) { |
| 1944 |
LogError(errno, RS_RET_CONF_FILE_NOT_FOUND, "yamlconf: cannot open config file '%s'", fname); |
| 1945 |
ABORT_FINALIZE(RS_RET_CONF_FILE_NOT_FOUND); |
| 1946 |
} |
| 1947 |
|
| 1948 |
if (!yaml_parser_initialize(&parser)) { |
| 1949 |
LogError(0, RS_RET_INTERNAL_ERROR, "yamlconf: failed to initialize YAML parser"); |
| 1950 |
ABORT_FINALIZE(RS_RET_INTERNAL_ERROR); |
| 1951 |
} |
| 1952 |
parserInit = 1; |
| 1953 |
yaml_parser_set_input_file(&parser, fh); |
| 1954 |
|
| 1955 |
/* Consume stream/document start */ |
| 1956 |
CHKiRet(expect_event(&parser, &ev, YAML_STREAM_START_EVENT, "stream start", fname)); |
| 1957 |
yaml_event_delete(&ev); |
| 1958 |
CHKiRet(expect_event(&parser, &ev, YAML_DOCUMENT_START_EVENT, "document start", fname)); |
| 1959 |
yaml_event_delete(&ev); |
| 1960 |
|
| 1961 |
/* The top-level node must be a mapping */ |
| 1962 |
CHKiRet(expect_event(&parser, &ev, YAML_MAPPING_START_EVENT, "top-level mapping", fname)); |
| 1963 |
yaml_event_delete(&ev); |
| 1964 |
|
| 1965 |
/* Track seen top-level keys to warn on duplicates. Duplicate keys are |
| 1966 |
* undefined behaviour in the YAML spec and unsupported by rsyslog; use |
| 1967 |
* sequences for multiple items or include: for file-level composition. */ |
| 1968 |
|
| 1969 |
/* Walk the top-level key/value pairs */ |
| 1970 |
while (1) { |
| 1971 |
CHKiRet(next_event(&parser, &ev, fname)); |
| 1972 |
|
| 1973 |
if (ev.type == YAML_MAPPING_END_EVENT) { |
| 1974 |
yaml_event_delete(&ev); |
| 1975 |
break; |
| 1976 |
} |
| 1977 |
if (ev.type != YAML_SCALAR_EVENT) { |
| 1978 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: expected scalar key at top level", fname); |
| 1979 |
yaml_event_delete(&ev); |
| 1980 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 1981 |
} |
| 1982 |
|
| 1983 |
char *topkey = strdup((char *)ev.data.scalar.value); |
| 1984 |
yaml_event_delete(&ev); |
| 1985 |
if (topkey == NULL) ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 1986 |
|
| 1987 |
/* Warn on duplicate top-level key. */ |
| 1988 |
for (int ki = 0; ki < seen_count; ++ki) { |
| 1989 |
if (!strcmp(seen_keys[ki], topkey)) { |
| 1990 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 1991 |
"yamlconf: %s: duplicate top-level key '%s' - " |
| 1992 |
"this is unsupported; use sequences for multiple items " |
| 1993 |
"or include: for file-level composition", |
| 1994 |
fname, topkey); |
| 1995 |
break; |
| 1996 |
} |
| 1997 |
} |
| 1998 |
int topkey_owned = 0; /* 1 if seen_keys owns topkey, 0 if we must free it */ |
| 1999 |
if (seen_count < YAMLCONF_MAX_TOPKEYS) { |
| 2000 |
seen_keys[seen_count++] = topkey; /* ownership transferred to seen_keys */ |
| 2001 |
topkey_owned = 1; |
| 2002 |
} |
| 2003 |
/* else: array full (>32 distinct top-level keys — highly unusual); |
| 2004 |
* duplicate detection skipped for this key; topkey freed below. */ |
| 2005 |
|
| 2006 |
/* Consume the opening event of the value node before dispatching */ |
| 2007 |
yaml_event_t vev; |
| 2008 |
rsRetVal vret = next_event(&parser, &vev, fname); |
| 2009 |
if (vret != RS_RET_OK) { |
| 2010 |
if (!topkey_owned) free(topkey); |
| 2011 |
ABORT_FINALIZE(vret); |
| 2012 |
} |
| 2013 |
|
| 2014 |
/* For sections that expect a sequence we need SEQUENCE_START; |
| 2015 |
* for singleton sections we need MAPPING_START. |
| 2016 |
* process_top_level() takes responsibility from here, but we already |
| 2017 |
* consumed the opening event – so verify it makes sense. */ |
| 2018 |
if (vev.type != YAML_MAPPING_START_EVENT && vev.type != YAML_SEQUENCE_START_EVENT && |
| 2019 |
vev.type != YAML_SCALAR_EVENT) { |
| 2020 |
LogError(0, RS_RET_CONF_PARSE_ERROR, "yamlconf: %s: unexpected value type for key '%s'", fname, topkey); |
| 2021 |
yaml_event_delete(&vev); |
| 2022 |
if (!topkey_owned) free(topkey); |
| 2023 |
ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR); |
| 2024 |
} |
| 2025 |
|
| 2026 |
/* For scalar values (e.g. version: 2) put the event back via a small |
| 2027 |
* detour: handle scalars inline here before calling process_top_level. */ |
| 2028 |
if (vev.type == YAML_SCALAR_EVENT) { |
| 2029 |
if (!strcmp(topkey, "version")) { |
| 2030 |
/* silently accept */ |
| 2031 |
} else { |
| 2032 |
LogError(0, RS_RET_CONF_PARSE_ERROR, |
| 2033 |
"yamlconf: %s: key '%s' has a scalar value but expects" |
| 2034 |
" a mapping or sequence", |
| 2035 |
fname, topkey); |
| 2036 |
/* non-fatal */ |
| 2037 |
} |
| 2038 |
yaml_event_delete(&vev); |
| 2039 |
if (!topkey_owned) free(topkey); |
| 2040 |
continue; |
| 2041 |
} |
| 2042 |
|
| 2043 |
yaml_event_delete(&vev); |
| 2044 |
/* process_top_level() will now call the right parser which will consume |
| 2045 |
* the rest of the value node (up to and including its END event). */ |
| 2046 |
rsRetVal pret = process_top_level(&parser, topkey, fname); |
| 2047 |
if (!topkey_owned) free(topkey); |
| 2048 |
if (pret != RS_RET_OK) ABORT_FINALIZE(pret); |
| 2049 |
} |
| 2050 |
|
| 2051 |
finalize_it: |
| 2052 |
for (int ki = 0; ki < seen_count; ++ki) free(seen_keys[ki]); |
| 2053 |
if (parserInit) yaml_parser_delete(&parser); |
| 2054 |
if (fh) fclose(fh); |
| 2055 |
cnfcurrfn = prev_cnfcurrfn; |
| 2056 |
RETiRet; |
| 2057 |
} |
| 2058 |
|
| 2059 |
#endif /* HAVE_LIBYAML */ |