Debian Codegraph @

runtime/translate.c

raw
1 /** @file translate.c
2 * @brief Canonical config translation support for rsyslog.
3 *
4 * Captures config objects and script bodies during config load so rsyslogd
5 * can emit canonical RainerScript or YAML during validation runs.
6 *
7 * Copyright 2026 Rainer Gerhards and Adiscon GmbH.
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 * -or-
15 * see COPYING.ASL20 in the source distribution
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "rsyslog.h"
31 #include "translate.h"
32 #include "action.h"
33 #include "errmsg.h"
34 #include "msg.h"
35 #include "grammar/grammar.h"
36
37 static char *translateVasprintf(const char *fmt, va_list ap);
38 static void cnfarrayCloneDestruct(struct cnfarray *ar);
39 static int preferredKeyRank(const struct nvlst *n);
40 static int nvlstSortComesBefore(const struct nvlst *a, const struct nvlst *b);
41 static int estrAppendCstr(es_str_t **s, const char *buf);
42 static int estrAppendChar(es_str_t **s, char c);
43 static int estrAppendQuoted(es_str_t **s, const char *buf);
44 static int nvlstValueToRs(es_str_t **out, const struct nvlst *node);
45
46 struct rsconfTranslateWarning_s {
47 char *msg;
48 struct rsconfTranslateWarning_s *next;
49 };
50
51 static void addWarning(struct rsconfTranslateWarning_s **head, const char *fmt, ...);
52
53 struct rsconfTranslateYamlAction_s {
54 struct nvlst *nvlst;
55 struct rsconfTranslateYamlAction_s *next;
56 };
57
58 struct rsconfTranslateYamlStatement_s {
59 char *if_expr;
60 struct rsconfTranslateYamlAction_s *actions;
61 struct rsconfTranslateYamlStatement_s *next;
62 };
63
64 enum rsconfTranslateYamlRulesetKind_e {
65 RS_TRANSLATE_YAML_RULESET_NONE = 0,
66 RS_TRANSLATE_YAML_RULESET_ACTIONS,
67 RS_TRANSLATE_YAML_RULESET_FILTER_ACTIONS,
68 RS_TRANSLATE_YAML_RULESET_STATEMENTS,
69 };
70
71 struct rsconfTranslateItem_s {
72 enum cnfobjType objType;
73 struct nvlst *nvlst;
74 struct objlst *subobjs;
75 char *script;
76 char *source;
77 int line;
78 struct rsconfTranslateWarning_s *warnings;
79 enum rsconfTranslateYamlRulesetKind_e yaml_ruleset_kind;
80 char *yaml_filter;
81 struct rsconfTranslateYamlAction_s *yaml_actions;
82 struct rsconfTranslateYamlStatement_s *yaml_statements;
83 struct rsconfTranslateItem_s *next;
84 };
85
86 struct rsconfTranslateState_s {
87 enum rsconfTranslateFormat fmt;
88 int fatal;
89 struct rsconfTranslateWarning_s *globalWarnings;
90 struct rsconfTranslateItem_s *globals;
91 struct rsconfTranslateItem_s *mainqueue;
92 struct rsconfTranslateItem_s *modules;
93 struct rsconfTranslateItem_s *inputs;
94 struct rsconfTranslateItem_s *templates;
95 struct rsconfTranslateItem_s *rulesets;
96 struct rsconfTranslateItem_s *lookups;
97 struct rsconfTranslateItem_s *parsers;
98 struct rsconfTranslateItem_s *timezones;
99 struct rsconfTranslateItem_s *dynstats;
100 struct rsconfTranslateItem_s *perctilestats;
101 struct rsconfTranslateItem_s *ratelimits;
102 };
103
104 /* Concurrency & Locking:
105 * Translation capture is only active during single-threaded config load and
106 * validation. g_tx is process-global mutable state with no locking because it
107 * is not accessed concurrently; runtime worker threads are not started while
108 * translation state is being built or emitted.
109 */
110 static struct rsconfTranslateState_s g_tx = {
111 RSCONF_TRANSLATE_NONE, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
112
113 static void cnfarrayCloneDestruct(struct cnfarray *ar) {
114 if (ar == NULL) return;
115 cnfarrayContentDestruct(ar);
116 free(ar);
117 }
118
119 static void warningsDestruct(struct rsconfTranslateWarning_s *w) {
120 struct rsconfTranslateWarning_s *n;
121 while (w != NULL) {
122 n = w->next;
123 free(w->msg);
124 free(w);
125 w = n;
126 }
127 }
128
129 static void yamlActionDestruct(struct rsconfTranslateYamlAction_s *act) {
130 while (act != NULL) {
131 struct rsconfTranslateYamlAction_s *n = act->next;
132 nvlstDestruct(act->nvlst);
133 free(act);
134 act = n;
135 }
136 }
137
138 static void yamlStatementDestruct(struct rsconfTranslateYamlStatement_s *stmt) {
139 while (stmt != NULL) {
140 struct rsconfTranslateYamlStatement_s *n = stmt->next;
141 free(stmt->if_expr);
142 yamlActionDestruct(stmt->actions);
143 free(stmt);
144 stmt = n;
145 }
146 }
147
148 static void itemDestruct(struct rsconfTranslateItem_s *it) {
149 while (it != NULL) {
150 struct rsconfTranslateItem_s *n = it->next;
151 nvlstDestruct(it->nvlst);
152 objlstDestruct(it->subobjs);
153 free(it->script);
154 free(it->source);
155 warningsDestruct(it->warnings);
156 free(it->yaml_filter);
157 yamlActionDestruct(it->yaml_actions);
158 yamlStatementDestruct(it->yaml_statements);
159 free(it);
160 it = n;
161 }
162 }
163
164 void rsconfTranslateCleanup(void) {
165 warningsDestruct(g_tx.globalWarnings);
166 itemDestruct(g_tx.globals);
167 itemDestruct(g_tx.mainqueue);
168 itemDestruct(g_tx.modules);
169 itemDestruct(g_tx.inputs);
170 itemDestruct(g_tx.templates);
171 itemDestruct(g_tx.rulesets);
172 itemDestruct(g_tx.lookups);
173 itemDestruct(g_tx.parsers);
174 itemDestruct(g_tx.timezones);
175 itemDestruct(g_tx.dynstats);
176 itemDestruct(g_tx.perctilestats);
177 itemDestruct(g_tx.ratelimits);
178 memset(&g_tx, 0, sizeof(g_tx));
179 }
180
181 void rsconfTranslateConfigure(enum rsconfTranslateFormat fmt) {
182 rsconfTranslateCleanup();
183 g_tx.fmt = fmt;
184 }
185
186 int rsconfTranslateEnabled(void) {
187 return g_tx.fmt != RSCONF_TRANSLATE_NONE;
188 }
189
190 int rsconfTranslateHasFatal(void) {
191 return g_tx.fatal;
192 }
193
194 PRAGMA_DIAGNOSTIC_PUSH
195 PRAGMA_IGNORE_Wformat_nonliteral static char *translateVasprintf(const char *fmt, va_list ap) {
196 va_list ap_copy;
197 char *buf;
198 int len;
199
200 va_copy(ap_copy, ap);
201 len = vsnprintf(NULL, 0, fmt, ap_copy);
202 va_end(ap_copy);
203 if (len < 0) return NULL;
204 buf = malloc((size_t)len + 1);
205 if (buf == NULL) return NULL;
206 va_copy(ap_copy, ap);
207 if (vsnprintf(buf, (size_t)len + 1, fmt, ap_copy) != len) {
208 va_end(ap_copy);
209 free(buf);
210 return NULL;
211 }
212 va_end(ap_copy);
213 return buf;
214 }
215
216 PRAGMA_IGNORE_Wformat_nonliteral static void addWarning(struct rsconfTranslateWarning_s **head, const char *fmt, ...) {
217 va_list ap;
218 char *buf;
219 struct rsconfTranslateWarning_s *w;
220
221 va_start(ap, fmt);
222 buf = translateVasprintf(fmt, ap);
223 va_end(ap);
224 if (buf == NULL) return;
225
226 w = calloc(1, sizeof(*w));
227 if (w == NULL) {
228 free(buf);
229 return;
230 }
231 w->msg = buf;
232 if (*head == NULL) {
233 *head = w;
234 } else {
235 struct rsconfTranslateWarning_s *cur = *head;
236 while (cur->next != NULL) cur = cur->next;
237 cur->next = w;
238 }
239 }
240
241 void rsconfTranslateAddUnsupported(const char *source, int line, const char *fmt, ...) {
242 va_list ap;
243 char *buf;
244
245 va_start(ap, fmt);
246 buf = translateVasprintf(fmt, ap);
247 va_end(ap);
248 g_tx.fatal = 1;
249 addWarning(&g_tx.globalWarnings, "%s:%d: %s", source == NULL ? "<config>" : source, line,
250 buf == NULL ? "translator: failed to format error message" : buf);
251 free(buf);
252 }
253
254 PRAGMA_DIAGNOSTIC_POP
255
256 static es_str_t *cloneEstr(const es_str_t *in) {
257 if (in == NULL) return NULL;
258 return es_newStrFromCStr((char *)es_getBufAddr((es_str_t *)in), es_strlen((es_str_t *)in));
259 }
260
261 static struct cnfarray *cloneArray(const struct cnfarray *ar) {
262 int i;
263 struct cnfarray *out;
264 if (ar == NULL) return NULL;
265 out = calloc(1, sizeof(*out));
266 if (out == NULL) return NULL;
267 out->nodetype = ar->nodetype;
268 out->nmemb = ar->nmemb;
269 if (ar->nmemb > 0) {
270 out->arr = calloc(ar->nmemb, sizeof(es_str_t *));
271 if (out->arr == NULL) {
272 free(out);
273 return NULL;
274 }
275 for (i = 0; i < ar->nmemb; ++i) {
276 out->arr[i] = cloneEstr(ar->arr[i]);
277 if (out->arr[i] == NULL && ar->arr[i] != NULL) {
278 cnfarrayCloneDestruct(out);
279 return NULL;
280 }
281 }
282 }
283 return out;
284 }
285
286 static struct nvlst *cloneNvlstNode(const struct nvlst *node) {
287 struct nvlst *out;
288 if (node == NULL) return NULL;
289 out = calloc(1, sizeof(*out));
290 if (out == NULL) return NULL;
291 out->name = cloneEstr(node->name);
292 if (out->name == NULL && node->name != NULL) {
293 nvlstDestruct(out);
294 return NULL;
295 }
296 out->val.datatype = node->val.datatype;
297 switch (node->val.datatype) {
298 case 'S':
299 out->val.d.estr = cloneEstr(node->val.d.estr);
300 if (out->val.d.estr == NULL && node->val.d.estr != NULL) {
301 nvlstDestruct(out);
302 return NULL;
303 }
304 break;
305 case 'A':
306 out->val.d.ar = cloneArray(node->val.d.ar);
307 if (out->val.d.ar == NULL && node->val.d.ar != NULL) {
308 nvlstDestruct(out);
309 return NULL;
310 }
311 break;
312 case 'N':
313 out->val.d.n = node->val.d.n;
314 break;
315 default:
316 break;
317 }
318 return out;
319 }
320
321 struct nvlst *rsconfTranslateCloneNvlst(const struct nvlst *lst) {
322 struct nvlst *head = NULL, *n;
323
324 while (lst != NULL) {
325 struct nvlst *prev = NULL, *cur = head;
326 n = cloneNvlstNode(lst);
327 if (n == NULL) {
328 nvlstDestruct(head);
329 return NULL;
330 }
331 while (cur != NULL && !nvlstSortComesBefore(n, cur)) {
332 prev = cur;
333 cur = cur->next;
334 }
335 n->next = cur;
336 if (prev == NULL) {
337 head = n;
338 } else {
339 prev->next = n;
340 }
341 lst = lst->next;
342 }
343 return head;
344 }
345
346 static struct objlst *cloneObjlst(const struct objlst *lst) {
347 struct objlst *head = NULL, *tail = NULL, *n;
348 while (lst != NULL) {
349 struct cnfobj *obj = calloc(1, sizeof(*obj));
350 if (obj == NULL) {
351 objlstDestruct(head);
352 return NULL;
353 }
354 obj->objType = lst->obj->objType;
355 obj->nvlst = rsconfTranslateCloneNvlst(lst->obj->nvlst);
356 if (obj->nvlst == NULL && lst->obj->nvlst != NULL) {
357 cnfobjDestruct(obj);
358 objlstDestruct(head);
359 return NULL;
360 }
361 n = objlstNew(obj);
362 if (n == NULL) {
363 cnfobjDestruct(obj);
364 objlstDestruct(head);
365 return NULL;
366 }
367 if (head == NULL) {
368 head = tail = n;
369 } else {
370 tail->next = n;
371 tail = n;
372 }
373 lst = lst->next;
374 }
375 return head;
376 }
377
378 static int stmtListIsStructuredActionsOnly(const struct cnfstmt *stmt) {
379 while (stmt != NULL) {
380 if (stmt->nodetype != S_ACT) return 0;
381 if (stmt->d.act != NULL && stmt->d.act->pSyntaxLst != NULL) {
382 stmt = stmt->next;
383 continue;
384 }
385 if (stmt->printable != NULL) {
386 const char *s = (const char *)stmt->printable;
387 while (*s == ' ' || *s == '\t') ++s;
388 if (*s == '/' || (*s == '-' && s[1] == '/') || !strncmp(s, ":omusrmsg:", 10)) {
389 stmt = stmt->next;
390 continue;
391 }
392 }
393 return 0;
394 }
395 return 1;
396 }
397
398 static struct nvlst *newStringNode(const char *name, const char *value) {
399 es_str_t *nm = NULL;
400 es_str_t *val = NULL;
401 struct nvlst *node = NULL;
402
403 nm = es_newStrFromCStr(name, strlen(name));
404 if (nm == NULL) goto done;
405 val = es_newStrFromCStr(value, strlen(value));
406 if (val == NULL) {
407 es_deleteStr(nm);
408 goto done;
409 }
410 node = nvlstNewStr(val);
411 if (node == NULL) {
412 es_deleteStr(val);
413 es_deleteStr(nm);
414 goto done;
415 }
416 node = nvlstSetName(node, nm);
417 if (node == NULL) {
418 es_deleteStr(val);
419 es_deleteStr(nm);
420 }
421
422 done:
423 return node;
424 }
425
426 static void yamlActionNodeAppend(struct nvlst **head, struct nvlst *node) {
427 struct nvlst *cur;
428
429 if (node == NULL) return;
430 node->next = NULL;
431 if (*head == NULL) {
432 *head = node;
433 return;
434 }
435 cur = *head;
436 while (cur->next != NULL) cur = cur->next;
437 cur->next = node;
438 }
439
440 static struct nvlst *legacyActionToNvlst(const char *printable, int *oom) {
441 const char *s = printable;
442 struct nvlst *lst = NULL;
443 struct nvlst *node;
444
445 if (printable == NULL) return NULL;
446 while (*s == ' ' || *s == '\t') ++s;
447
448 if (*s == '/' || (*s == '-' && s[1] == '/')) {
449 if (*s == '-') ++s;
450 node = newStringNode("type", "omfile");
451 if (node == NULL) goto oom_fail;
452 yamlActionNodeAppend(&lst, node);
453 node = newStringNode("file", s);
454 if (node == NULL) goto oom_fail;
455 yamlActionNodeAppend(&lst, node);
456 return lst;
457 }
458
459 if (!strncmp(s, ":omusrmsg:", 10)) {
460 node = newStringNode("type", "omusrmsg");
461 if (node == NULL) goto oom_fail;
462 yamlActionNodeAppend(&lst, node);
463 node = newStringNode("users", s + 10);
464 if (node == NULL) goto oom_fail;
465 yamlActionNodeAppend(&lst, node);
466 return lst;
467 }
468
469 return NULL;
470
471 oom_fail:
472 *oom = 1;
473 nvlstDestruct(lst);
474 return NULL;
475 }
476
477 static char *buildPriFilterExpr(const char *selector) {
478 es_str_t *estr;
479 char *out;
480 const char *p;
481 int use_single = 1;
482
483 estr = es_newStr(64);
484 if (estr == NULL) return NULL;
485 for (p = selector; *p != '\0'; ++p) {
486 if (*p == '\'') {
487 use_single = 0;
488 break;
489 }
490 }
491 if (use_single) {
492 if (estrAppendCstr(&estr, "prifilt('") != 0) {
493 es_deleteStr(estr);
494 return NULL;
495 }
496 } else if (estrAppendCstr(&estr, "prifilt(") != 0 || estrAppendQuoted(&estr, selector) != 0 ||
497 estrAppendChar(&estr, ')') != 0) {
498 es_deleteStr(estr);
499 return NULL;
500 }
501 if (!use_single) {
502 out = es_str2cstr(estr, NULL);
503 es_deleteStr(estr);
504 return out;
505 }
506 for (p = selector; *p != '\0'; ++p) {
507 if (estrAppendChar(&estr, *p) != 0) {
508 es_deleteStr(estr);
509 return NULL;
510 }
511 }
512 if (estrAppendCstr(&estr, "')") != 0) {
513 es_deleteStr(estr);
514 return NULL;
515 }
516 out = es_str2cstr(estr, NULL);
517 es_deleteStr(estr);
518 return out;
519 }
520
521 static int appendActionNvlstSingleline(es_str_t **out, const struct nvlst *lst) {
522 const struct nvlst *n;
523 char *name;
524 int first = 1;
525 int rank;
526
527 if (estrAppendCstr(out, "action(") != 0) return -1;
528 for (rank = 0; rank < 4; ++rank) {
529 for (n = lst; n != NULL; n = n->next) {
530 if (preferredKeyRank(n) != rank) continue;
531 name = es_str2cstr(n->name, NULL);
532 if (name == NULL) return -1;
533 if (!first && estrAppendCstr(out, " ") != 0) {
534 free(name);
535 return -1;
536 }
537 first = 0;
538 if (estrAppendCstr(out, name) != 0 || estrAppendChar(out, '=') != 0 || nvlstValueToRs(out, n) != 0) {
539 free(name);
540 return -1;
541 }
542 free(name);
543 }
544 }
545 return estrAppendChar(out, ')');
546 }
547
548 static struct rsconfTranslateYamlAction_s *cloneYamlActions(const struct cnfstmt *stmt, int *oom) {
549 struct rsconfTranslateYamlAction_s *head = NULL, *tail = NULL, *node;
550
551 while (stmt != NULL) {
552 int legacy_oom = 0;
553 node = calloc(1, sizeof(*node));
554 if (node == NULL) {
555 *oom = 1;
556 yamlActionDestruct(head);
557 return NULL;
558 }
559 if (stmt->d.act != NULL && stmt->d.act->pSyntaxLst != NULL) {
560 node->nvlst = rsconfTranslateCloneNvlst(stmt->d.act->pSyntaxLst);
561 } else {
562 node->nvlst = legacyActionToNvlst((const char *)stmt->printable, &legacy_oom);
563 }
564 if (node->nvlst == NULL) {
565 *oom = legacy_oom;
566 free(node);
567 yamlActionDestruct(head);
568 return NULL;
569 }
570 if (head == NULL) {
571 head = tail = node;
572 } else {
573 tail->next = node;
574 tail = node;
575 }
576 stmt = stmt->next;
577 }
578
579 return head;
580 }
581
582 static void yamlActionsAppend(struct rsconfTranslateYamlAction_s **dst, struct rsconfTranslateYamlAction_s *src) {
583 struct rsconfTranslateYamlAction_s *tail;
584 if (*dst == NULL) {
585 *dst = src;
586 return;
587 }
588 tail = *dst;
589 while (tail->next != NULL) tail = tail->next;
590 tail->next = src;
591 }
592
593 static struct rsconfTranslateYamlStatement_s *cloneYamlLegacyStatements(const struct cnfstmt *stmt, int *oom) {
594 struct rsconfTranslateYamlStatement_s *head = NULL, *tail = NULL, *node;
595
596 while (stmt != NULL) {
597 const struct cnfstmt *then_branch = NULL;
598 char *if_expr;
599
600 if (stmt->nodetype != S_PRIFILT || stmt->d.s_prifilt.t_else != NULL || stmt->printable == NULL) {
601 yamlStatementDestruct(head);
602 return NULL;
603 }
604
605 then_branch = stmt->d.s_prifilt.t_then;
606 if (!stmtListIsStructuredActionsOnly(then_branch)) {
607 yamlStatementDestruct(head);
608 return NULL;
609 }
610
611 if_expr = buildPriFilterExpr((const char *)stmt->printable);
612 if (if_expr == NULL) {
613 *oom = 1;
614 yamlStatementDestruct(head);
615 return NULL;
616 }
617
618 node = calloc(1, sizeof(*node));
619 if (node == NULL) {
620 free(if_expr);
621 *oom = 1;
622 yamlStatementDestruct(head);
623 return NULL;
624 }
625 node->if_expr = if_expr;
626 node->actions = cloneYamlActions(then_branch, oom);
627 if (node->actions == NULL) {
628 yamlStatementDestruct(node);
629 yamlStatementDestruct(head);
630 return NULL;
631 }
632
633 if (head == NULL) {
634 head = tail = node;
635 } else {
636 tail->next = node;
637 tail = node;
638 }
639 stmt = stmt->next;
640 }
641
642 return head;
643 }
644
645 static struct rsconfTranslateYamlStatement_s *newYamlStatement(char *if_expr,
646 struct rsconfTranslateYamlAction_s *actions,
647 int *oom) {
648 struct rsconfTranslateYamlStatement_s *node = calloc(1, sizeof(*node));
649 if (node == NULL) {
650 *oom = 1;
651 free(if_expr);
652 yamlActionDestruct(actions);
653 return NULL;
654 }
655 node->if_expr = if_expr;
656 node->actions = actions;
657 return node;
658 }
659
660 static void yamlStatementsAppend(struct rsconfTranslateYamlStatement_s **dst,
661 struct rsconfTranslateYamlStatement_s *src) {
662 struct rsconfTranslateYamlStatement_s *tail;
663 if (*dst == NULL) {
664 *dst = src;
665 return;
666 }
667 tail = *dst;
668 while (tail->next != NULL) tail = tail->next;
669 tail->next = src;
670 }
671
672 static int promoteSimpleYamlToStatements(struct rsconfTranslateItem_s *it, int *oom) {
673 char *if_expr;
674 struct rsconfTranslateYamlStatement_s *stmt;
675
676 if (it->yaml_ruleset_kind == RS_TRANSLATE_YAML_RULESET_STATEMENTS) return 1;
677 if (it->yaml_ruleset_kind != RS_TRANSLATE_YAML_RULESET_FILTER_ACTIONS || it->yaml_filter == NULL ||
678 it->yaml_actions == NULL)
679 return 0;
680
681 if_expr = buildPriFilterExpr(it->yaml_filter);
682 if (if_expr == NULL) {
683 *oom = 1;
684 return 0;
685 }
686 stmt = newYamlStatement(if_expr, it->yaml_actions, oom);
687 if (stmt == NULL) return 0;
688
689 it->yaml_actions = NULL;
690 free(it->yaml_filter);
691 it->yaml_filter = NULL;
692 it->yaml_statements = stmt;
693 it->yaml_ruleset_kind = RS_TRANSLATE_YAML_RULESET_STATEMENTS;
694 return 1;
695 }
696
697 static int appendYamlLegacyStatement(struct rsconfTranslateItem_s *it, const struct cnfstmt *script, int *oom) {
698 const struct cnfstmt *then_branch = NULL;
699 char *if_expr;
700 struct rsconfTranslateYamlAction_s *actions;
701 struct rsconfTranslateYamlStatement_s *stmt;
702
703 if (script == NULL || script->next != NULL || script->nodetype != S_PRIFILT || script->d.s_prifilt.t_else != NULL ||
704 script->printable == NULL)
705 return 0;
706 then_branch = script->d.s_prifilt.t_then;
707 if (!stmtListIsStructuredActionsOnly(then_branch)) return 0;
708
709 if (!promoteSimpleYamlToStatements(it, oom) || it->yaml_ruleset_kind != RS_TRANSLATE_YAML_RULESET_STATEMENTS) {
710 return 0;
711 }
712
713 if_expr = buildPriFilterExpr((const char *)script->printable);
714 if (if_expr == NULL) {
715 *oom = 1;
716 return 0;
717 }
718 actions = cloneYamlActions(then_branch, oom);
719 if (actions == NULL) {
720 free(if_expr);
721 return 0;
722 }
723 stmt = newYamlStatement(if_expr, actions, oom);
724 if (stmt == NULL) return 0;
725 yamlStatementsAppend(&it->yaml_statements, stmt);
726 return 1;
727 }
728
729 static void captureYamlRulesetBody(struct rsconfTranslateItem_s *it, const struct cnfstmt *script) {
730 int oom = 0;
731
732 if (it == NULL || script == NULL) return;
733
734 if (stmtListIsStructuredActionsOnly(script)) {
735 struct rsconfTranslateYamlAction_s *actions = cloneYamlActions(script, &oom);
736 if (oom) {
737 addWarning(&it->warnings, "translator: out of memory when capturing YAML action list");
738 g_tx.fatal = 1;
739 return;
740 }
741 if (actions == NULL) {
742 /* Unsupported legacy action shorthand, fall back below. */
743 } else if (it->yaml_ruleset_kind == RS_TRANSLATE_YAML_RULESET_NONE) {
744 yamlStatementDestruct(it->yaml_statements);
745 it->yaml_statements = NULL;
746 it->yaml_actions = actions;
747 it->yaml_ruleset_kind = RS_TRANSLATE_YAML_RULESET_ACTIONS;
748 return;
749 } else if (it->yaml_ruleset_kind == RS_TRANSLATE_YAML_RULESET_ACTIONS && it->yaml_filter == NULL) {
750 yamlActionsAppend(&it->yaml_actions, actions);
751 return;
752 } else {
753 yamlActionDestruct(actions);
754 free(it->yaml_filter);
755 it->yaml_filter = NULL;
756 yamlActionDestruct(it->yaml_actions);
757 it->yaml_actions = NULL;
758 yamlStatementDestruct(it->yaml_statements);
759 it->yaml_statements = NULL;
760 it->yaml_ruleset_kind = RS_TRANSLATE_YAML_RULESET_NONE;
761 }
762 }
763
764 if (script->next == NULL && script->printable != NULL) {
765 const struct cnfstmt *then_branch = NULL;
766
767 if (script->nodetype == S_PRIFILT && script->d.s_prifilt.t_else == NULL) {
768 then_branch = script->d.s_prifilt.t_then;
769 } else if (script->nodetype == S_PROPFILT && script->d.s_propfilt.t_else == NULL) {
770 then_branch = script->d.s_propfilt.t_then;
771 }
772
773 if (then_branch != NULL && stmtListIsStructuredActionsOnly(then_branch) &&
774 it->yaml_ruleset_kind == RS_TRANSLATE_YAML_RULESET_NONE) {
775 it->yaml_filter = strdup((char *)script->printable);
776 if (it->yaml_filter == NULL) {
777 addWarning(&it->warnings, "translator: out of memory when capturing YAML filter");
778 g_tx.fatal = 1;
779 return;
780 }
781 it->yaml_actions = cloneYamlActions(then_branch, &oom);
782 if (oom) {
783 addWarning(&it->warnings, "translator: out of memory when capturing YAML action list");
784 g_tx.fatal = 1;
785 return;
786 }
787 if (it->yaml_actions != NULL) {
788 yamlStatementDestruct(it->yaml_statements);
789 it->yaml_statements = NULL;
790 it->yaml_ruleset_kind = RS_TRANSLATE_YAML_RULESET_FILTER_ACTIONS;
791 return;
792 }
793 free(it->yaml_filter);
794 it->yaml_filter = NULL;
795 }
796 }
797
798 if (it->yaml_ruleset_kind == RS_TRANSLATE_YAML_RULESET_NONE) {
799 it->yaml_statements = cloneYamlLegacyStatements(script, &oom);
800 if (oom) {
801 addWarning(&it->warnings, "translator: out of memory when capturing YAML statements");
802 g_tx.fatal = 1;
803 return;
804 }
805 if (it->yaml_statements != NULL) {
806 it->yaml_ruleset_kind = RS_TRANSLATE_YAML_RULESET_STATEMENTS;
807 return;
808 }
809 }
810
811 free(it->yaml_filter);
812 it->yaml_filter = NULL;
813 yamlActionDestruct(it->yaml_actions);
814 it->yaml_actions = NULL;
815 yamlStatementDestruct(it->yaml_statements);
816 it->yaml_statements = NULL;
817 it->yaml_ruleset_kind = RS_TRANSLATE_YAML_RULESET_NONE;
818 }
819
820 static int stmtListIsSelectorCompatible(const struct cnfstmt *stmt) {
821 while (stmt != NULL) {
822 switch (stmt->nodetype) {
823 case S_ACT:
824 case S_STOP:
825 case S_CALL:
826 case S_CALL_INDIRECT:
827 case S_NOP:
828 break;
829 default:
830 return 0;
831 }
832 stmt = stmt->next;
833 }
834 return 1;
835 }
836
837 static int estrAppendCstr(es_str_t **s, const char *buf) {
838 return es_addBuf(s, buf, strlen(buf));
839 }
840
841 static int estrAppendChar(es_str_t **s, char c) {
842 return es_addChar(s, (unsigned char)c);
843 }
844
845 static int estrAppendQuoted(es_str_t **s, const char *buf) {
846 size_t i;
847 if (estrAppendChar(s, '"') != 0) return -1;
848 for (i = 0; buf[i] != '\0'; ++i) {
849 switch (buf[i]) {
850 case '"':
851 case '\\':
852 if (estrAppendChar(s, '\\') != 0 || estrAppendChar(s, buf[i]) != 0) return -1;
853 break;
854 case '\n':
855 if (estrAppendCstr(s, "\\n") != 0) return -1;
856 break;
857 case '\r':
858 if (estrAppendCstr(s, "\\r") != 0) return -1;
859 break;
860 case '\t':
861 if (estrAppendCstr(s, "\\t") != 0) return -1;
862 break;
863 default:
864 if (estrAppendChar(s, buf[i]) != 0) return -1;
865 break;
866 }
867 }
868 return estrAppendChar(s, '"');
869 }
870
871 static const char *exprOpToStr(unsigned nodetype) {
872 switch (nodetype) {
873 case CMP_EQ:
874 return "==";
875 case CMP_NE:
876 return "!=";
877 case CMP_LE:
878 return "<=";
879 case CMP_GE:
880 return ">=";
881 case CMP_LT:
882 return "<";
883 case CMP_GT:
884 return ">";
885 case CMP_CONTAINS:
886 return "contains";
887 case CMP_CONTAINSI:
888 return "contains_i";
889 case CMP_STARTSWITH:
890 return "startswith";
891 case CMP_STARTSWITHI:
892 return "startswith_i";
893 case CMP_ENDSWITH:
894 return "endswith";
895 case OR:
896 return "or";
897 case AND:
898 return "and";
899 default:
900 return NULL;
901 }
902 }
903
904 static int exprToString(es_str_t **out, const struct cnfexpr *expr, struct rsconfTranslateWarning_s **warnings);
905
906 static int exprListToString(es_str_t **out,
907 unsigned short nParams,
908 const struct cnfexpr *const *exprs,
909 struct rsconfTranslateWarning_s **warnings) {
910 unsigned short i;
911 for (i = 0; i < nParams; ++i) {
912 if (i > 0 && estrAppendCstr(out, ", ") != 0) return -1;
913 if (exprToString(out, exprs[i], warnings) != 0) return -1;
914 }
915 return 0;
916 }
917
918 static int exprToString(es_str_t **out, const struct cnfexpr *expr, struct rsconfTranslateWarning_s **warnings) {
919 const char *op;
920 char nbuf[64];
921 int i;
922 char *cstr;
923 const struct cnffunc *func;
924 const struct cnfarray *ar;
925
926 if (expr == NULL) return -1;
927 op = exprOpToStr(expr->nodetype);
928 if (op != NULL) {
929 if (estrAppendChar(out, '(') != 0) return -1;
930 if (exprToString(out, expr->l, warnings) != 0) return -1;
931 if (estrAppendCstr(out, " ") != 0 || estrAppendCstr(out, op) != 0 || estrAppendCstr(out, " ") != 0) return -1;
932 if (exprToString(out, expr->r, warnings) != 0) return -1;
933 return estrAppendChar(out, ')');
934 }
935
936 switch (expr->nodetype) {
937 case NOT:
938 if (estrAppendCstr(out, "not ") != 0) return -1;
939 return exprToString(out, expr->r, warnings);
940 case 'N': {
941 int len = snprintf(nbuf, sizeof(nbuf), "%lld", ((const struct cnfnumval *)expr)->val);
942 if (len < 0 || len >= (int)sizeof(nbuf)) return -1;
943 return estrAppendCstr(out, nbuf);
944 }
945 case 'S':
946 cstr = es_str2cstr(((const struct cnfstringval *)expr)->estr, NULL);
947 if (cstr == NULL) return -1;
948 i = estrAppendQuoted(out, cstr);
949 free(cstr);
950 return i;
951 case 'V':
952 return estrAppendCstr(out, ((const struct cnfvar *)expr)->name);
953 case 'A':
954 ar = (const struct cnfarray *)expr;
955 if (estrAppendChar(out, '[') != 0) return -1;
956 for (i = 0; i < ar->nmemb; ++i) {
957 char *aval = es_str2cstr(ar->arr[i], NULL);
958 if (i > 0 && estrAppendCstr(out, ", ") != 0) {
959 free(aval);
960 return -1;
961 }
962 if (aval == NULL || estrAppendQuoted(out, aval) != 0) {
963 free(aval);
964 return -1;
965 }
966 free(aval);
967 }
968 return estrAppendChar(out, ']');
969 case 'F':
970 func = (const struct cnffunc *)expr;
971 cstr = es_str2cstr(func->fname, NULL);
972 if (cstr == NULL) return -1;
973 if (estrAppendCstr(out, cstr) != 0 || estrAppendChar(out, '(') != 0) {
974 free(cstr);
975 return -1;
976 }
977 free(cstr);
978 if (exprListToString(out, func->nParams, (const struct cnfexpr *const *)func->expr, warnings) != 0 ||
979 estrAppendChar(out, ')') != 0)
980 return -1;
981 return 0;
982 case S_FUNC_EXISTS:
983 if (estrAppendCstr(out, "exists(") != 0) return -1;
984 if (estrAppendCstr(out, ((const struct cnffuncexists *)expr)->varname) != 0) return -1;
985 return estrAppendChar(out, ')');
986 case '&':
987 case '+':
988 case '-':
989 case '*':
990 case '/':
991 case '%':
992 case 'M':
993 if (expr->nodetype == 'M') {
994 if (estrAppendChar(out, '-') != 0) return -1;
995 return exprToString(out, expr->r, warnings);
996 }
997 if (estrAppendChar(out, '(') != 0) return -1;
998 if (exprToString(out, expr->l, warnings) != 0) return -1;
999 if (estrAppendCstr(out, " ") != 0 || estrAppendChar(out, (char)expr->nodetype) != 0 ||
1000 estrAppendCstr(out, " ") != 0)
1001 return -1;
1002 if (exprToString(out, expr->r, warnings) != 0) return -1;
1003 return estrAppendChar(out, ')');
1004 default:
1005 addWarning(warnings, "translator: unsupported expression node %u", expr->nodetype);
1006 return -1;
1007 }
1008 }
1009
1010 static int nvlstValueToRs(es_str_t **out, const struct nvlst *node) {
1011 char *cstr;
1012 int i;
1013 struct cnfarray *ar;
1014 char nbuf[64];
1015
1016 switch (node->val.datatype) {
1017 case 'S':
1018 cstr = es_str2cstr(node->val.d.estr, NULL);
1019 if (cstr == NULL) return -1;
1020 i = estrAppendQuoted(out, cstr);
1021 free(cstr);
1022 return i;
1023 case 'A':
1024 ar = node->val.d.ar;
1025 if (estrAppendChar(out, '[') != 0) return -1;
1026 for (i = 0; i < ar->nmemb; ++i) {
1027 cstr = es_str2cstr(ar->arr[i], NULL);
1028 if (cstr == NULL) return -1;
1029 if ((i > 0 && estrAppendCstr(out, ", ") != 0) || estrAppendQuoted(out, cstr) != 0) {
1030 free(cstr);
1031 return -1;
1032 }
1033 free(cstr);
1034 }
1035 return estrAppendChar(out, ']');
1036 case 'N': {
1037 int len = snprintf(nbuf, sizeof(nbuf), "%lld", node->val.d.n);
1038 if (len < 0 || len >= (int)sizeof(nbuf)) return -1;
1039 return estrAppendCstr(out, nbuf);
1040 }
1041 default:
1042 return -1;
1043 }
1044 }
1045
1046 static int emitActionSingleline(es_str_t **out,
1047 const struct cnfstmt *stmt,
1048 struct rsconfTranslateWarning_s **warnings) {
1049 char *name;
1050
1051 switch (stmt->nodetype) {
1052 case S_ACT:
1053 if (stmt->d.act != NULL && stmt->d.act->pSyntaxLst != NULL) {
1054 return appendActionNvlstSingleline(out, stmt->d.act->pSyntaxLst);
1055 }
1056 if (stmt->printable != NULL) {
1057 int oom = 0;
1058 struct nvlst *legacy = legacyActionToNvlst((char *)stmt->printable, &oom);
1059 if (legacy != NULL) {
1060 int ret = appendActionNvlstSingleline(out, legacy);
1061 nvlstDestruct(legacy);
1062 return ret;
1063 }
1064 if (oom) {
1065 addWarning(warnings, "translator: out of memory when converting legacy action syntax");
1066 return -1;
1067 }
1068 addWarning(warnings, "legacy action syntax preserved as script text");
1069 return estrAppendCstr(out, (char *)stmt->printable);
1070 }
1071 addWarning(warnings, "translator: action statement lost original syntax");
1072 return -1;
1073 case S_STOP:
1074 return estrAppendCstr(out, "stop");
1075 case S_CALL: {
1076 int ret;
1077 if (estrAppendCstr(out, "call ") != 0) return -1;
1078 name = es_str2cstr(stmt->d.s_call.name, NULL);
1079 if (name == NULL) return -1;
1080 ret = estrAppendCstr(out, name);
1081 free(name);
1082 return ret;
1083 }
1084 case S_CALL_INDIRECT:
1085 if (estrAppendCstr(out, "call_indirect ") != 0) return -1;
1086 if (exprToString(out, stmt->d.s_call_ind.expr, warnings) != 0) return -1;
1087 return estrAppendChar(out, ';');
1088 case S_NOP:
1089 if (stmt->printable != NULL && !strcmp((char *)stmt->printable, "continue")) {
1090 return estrAppendCstr(out, "continue");
1091 }
1092 addWarning(warnings, "translator: omitted internal NOP statement");
1093 return -1;
1094 default:
1095 return -1;
1096 }
1097 }
1098
1099 static int stmtListToString(es_str_t **out,
1100 const struct cnfstmt *stmt,
1101 int indent,
1102 struct rsconfTranslateWarning_s **warnings);
1103
1104 static int appendIndent(es_str_t **out, int indent) {
1105 while (indent-- > 0) {
1106 if (estrAppendCstr(out, " ") != 0) return -1;
1107 }
1108 return 0;
1109 }
1110
1111 static int emitSelectorBlock(es_str_t **out,
1112 const struct cnfstmt *stmt,
1113 int indent,
1114 const char *selector,
1115 struct rsconfTranslateWarning_s **warnings) {
1116 const struct cnfstmt *cur = stmt;
1117 int first = 1;
1118 while (cur != NULL) {
1119 if (appendIndent(out, indent) != 0) return -1;
1120 if (!first && estrAppendCstr(out, "& ") != 0) return -1;
1121 if (first) {
1122 if (estrAppendCstr(out, selector) != 0 || estrAppendCstr(out, " ") != 0) return -1;
1123 }
1124 first = 0;
1125 if (emitActionSingleline(out, cur, warnings) != 0 || estrAppendChar(out, '\n') != 0) return -1;
1126 cur = cur->next;
1127 }
1128 return 0;
1129 }
1130
1131 static int stmtListToString(es_str_t **out,
1132 const struct cnfstmt *stmt,
1133 int indent,
1134 struct rsconfTranslateWarning_s **warnings) {
1135 while (stmt != NULL) {
1136 switch (stmt->nodetype) {
1137 case S_ACT:
1138 case S_STOP:
1139 case S_CALL:
1140 case S_CALL_INDIRECT:
1141 case S_NOP:
1142 if (appendIndent(out, indent) != 0 || emitActionSingleline(out, stmt, warnings) != 0 ||
1143 estrAppendChar(out, '\n') != 0)
1144 return -1;
1145 break;
1146 case S_SET:
1147 if (appendIndent(out, indent) != 0) return -1;
1148 if (estrAppendCstr(out, stmt->d.s_set.force_reset ? "reset " : "set ") != 0 ||
1149 estrAppendCstr(out, (char *)stmt->d.s_set.varname) != 0 || estrAppendCstr(out, " = ") != 0 ||
1150 exprToString(out, stmt->d.s_set.expr, warnings) != 0 || estrAppendCstr(out, ";\n") != 0)
1151 return -1;
1152 break;
1153 case S_UNSET:
1154 if (appendIndent(out, indent) != 0 || estrAppendCstr(out, "unset ") != 0 ||
1155 estrAppendCstr(out, (char *)stmt->d.s_unset.varname) != 0 || estrAppendCstr(out, ";\n") != 0)
1156 return -1;
1157 break;
1158 case S_IF:
1159 if (appendIndent(out, indent) != 0 || estrAppendCstr(out, "if ") != 0 ||
1160 exprToString(out, stmt->d.s_if.expr, warnings) != 0 || estrAppendCstr(out, " then {\n") != 0 ||
1161 stmtListToString(out, stmt->d.s_if.t_then, indent + 1, warnings) != 0)
1162 return -1;
1163 if (appendIndent(out, indent) != 0) return -1;
1164 if (stmt->d.s_if.t_else != NULL) {
1165 if (estrAppendCstr(out, "} else {\n") != 0 ||
1166 stmtListToString(out, stmt->d.s_if.t_else, indent + 1, warnings) != 0 ||
1167 appendIndent(out, indent) != 0 || estrAppendCstr(out, "}\n") != 0)
1168 return -1;
1169 } else if (estrAppendCstr(out, "}\n") != 0) {
1170 return -1;
1171 }
1172 break;
1173 case S_FOREACH:
1174 if (appendIndent(out, indent) != 0 || estrAppendCstr(out, "foreach (") != 0 ||
1175 estrAppendCstr(out, stmt->d.s_foreach.iter->var) != 0 || estrAppendCstr(out, " in ") != 0 ||
1176 exprToString(out, stmt->d.s_foreach.iter->collection, warnings) != 0 ||
1177 estrAppendCstr(out, ") do {\n") != 0 ||
1178 stmtListToString(out, stmt->d.s_foreach.body, indent + 1, warnings) != 0 ||
1179 appendIndent(out, indent) != 0 || estrAppendCstr(out, "}\n") != 0)
1180 return -1;
1181 break;
1182 case S_PRIFILT:
1183 if (stmt->d.s_prifilt.t_else == NULL && stmtListIsSelectorCompatible(stmt->d.s_prifilt.t_then)) {
1184 if (emitSelectorBlock(out, stmt->d.s_prifilt.t_then, indent, (char *)stmt->printable, warnings) !=
1185 0)
1186 return -1;
1187 } else {
1188 addWarning(warnings, "PRI selector normalized into if/prifilt() form");
1189 if (appendIndent(out, indent) != 0 || estrAppendCstr(out, "if prifilt(") != 0 ||
1190 estrAppendQuoted(out, (char *)stmt->printable) != 0 || estrAppendCstr(out, ") then {\n") != 0 ||
1191 stmtListToString(out, stmt->d.s_prifilt.t_then, indent + 1, warnings) != 0 ||
1192 appendIndent(out, indent) != 0)
1193 return -1;
1194 if (stmt->d.s_prifilt.t_else != NULL) {
1195 if (estrAppendCstr(out, "} else {\n") != 0 ||
1196 stmtListToString(out, stmt->d.s_prifilt.t_else, indent + 1, warnings) != 0 ||
1197 appendIndent(out, indent) != 0 || estrAppendCstr(out, "}\n") != 0)
1198 return -1;
1199 } else if (estrAppendCstr(out, "}\n") != 0) {
1200 return -1;
1201 }
1202 }
1203 break;
1204 case S_PROPFILT:
1205 if (stmt->d.s_propfilt.t_else == NULL && stmtListIsSelectorCompatible(stmt->d.s_propfilt.t_then)) {
1206 if (emitSelectorBlock(out, stmt->d.s_propfilt.t_then, indent, (char *)stmt->printable, warnings) !=
1207 0)
1208 return -1;
1209 } else {
1210 addWarning(warnings, "property selector with else/complex body is not safely translatable");
1211 return -1;
1212 }
1213 break;
1214 case S_RELOAD_LOOKUP_TABLE:
1215 if (appendIndent(out, indent) != 0 || estrAppendCstr(out, "reload_lookup_table(") != 0 ||
1216 estrAppendQuoted(out, (char *)stmt->d.s_reload_lookup_table.table_name) != 0 ||
1217 estrAppendCstr(out, ", stub_value=") != 0 ||
1218 estrAppendQuoted(out, (char *)stmt->d.s_reload_lookup_table.stub_value) != 0 ||
1219 estrAppendCstr(out, ")\n") != 0)
1220 return -1;
1221 break;
1222 default:
1223 addWarning(warnings, "translator: unsupported statement node %u", stmt->nodetype);
1224 return -1;
1225 }
1226 stmt = stmt->next;
1227 }
1228 return 0;
1229 }
1230
1231 static struct rsconfTranslateItem_s **selectList(enum cnfobjType t) {
1232 switch (t) {
1233 case CNFOBJ_GLOBAL:
1234 return &g_tx.globals;
1235 case CNFOBJ_MAINQ:
1236 return &g_tx.mainqueue;
1237 case CNFOBJ_MODULE:
1238 return &g_tx.modules;
1239 case CNFOBJ_INPUT:
1240 return &g_tx.inputs;
1241 case CNFOBJ_TPL:
1242 return &g_tx.templates;
1243 case CNFOBJ_RULESET:
1244 return &g_tx.rulesets;
1245 case CNFOBJ_LOOKUP_TABLE:
1246 return &g_tx.lookups;
1247 case CNFOBJ_PARSER:
1248 return &g_tx.parsers;
1249 case CNFOBJ_TIMEZONE:
1250 return &g_tx.timezones;
1251 case CNFOBJ_DYN_STATS:
1252 return &g_tx.dynstats;
1253 case CNFOBJ_PERCTILE_STATS:
1254 return &g_tx.perctilestats;
1255 case CNFOBJ_RATELIMIT:
1256 return &g_tx.ratelimits;
1257 case CNFOBJ_ACTION:
1258 case CNFOBJ_PROPERTY:
1259 case CNFOBJ_CONSTANT:
1260 default:
1261 return NULL;
1262 }
1263 }
1264
1265 static void mergeSingleton(struct rsconfTranslateItem_s *dst, const struct nvlst *lst) {
1266 const struct nvlst *src;
1267 for (src = lst; src != NULL; src = src->next) {
1268 struct nvlst *prev = NULL;
1269 struct nvlst *cur = dst->nvlst;
1270 while (cur != NULL) {
1271 char *lhs = es_str2cstr(cur->name, NULL);
1272 char *rhs = es_str2cstr(src->name, NULL);
1273 int eq = lhs != NULL && rhs != NULL && !strcasecmp(lhs, rhs);
1274 free(lhs);
1275 free(rhs);
1276 if (eq) break;
1277 prev = cur;
1278 cur = cur->next;
1279 }
1280 if (cur != NULL) {
1281 struct nvlst *rep = cloneNvlstNode(src);
1282 if (rep == NULL) continue;
1283 rep->next = cur->next;
1284 if (prev == NULL)
1285 dst->nvlst = rep;
1286 else
1287 prev->next = rep;
1288 cur->next = NULL;
1289 nvlstDestruct(cur);
1290 } else {
1291 struct nvlst *rep = cloneNvlstNode(src);
1292 struct nvlst *tail;
1293 if (rep == NULL) continue;
1294 rep->next = NULL;
1295 if (dst->nvlst == NULL) {
1296 dst->nvlst = rep;
1297 } else {
1298 tail = dst->nvlst;
1299 while (tail->next != NULL) tail = tail->next;
1300 tail->next = rep;
1301 }
1302 }
1303 }
1304 }
1305
1306 void rsconfTranslateCaptureObj(const struct cnfobj *o, const char *source, int line) {
1307 struct rsconfTranslateItem_s **list;
1308 struct rsconfTranslateItem_s *it;
1309 es_str_t *estr;
1310
1311 if (!rsconfTranslateEnabled() || o == NULL) return;
1312 list = selectList(o->objType);
1313 if (list == NULL) return;
1314
1315 if ((o->objType == CNFOBJ_GLOBAL || o->objType == CNFOBJ_MAINQ) && *list != NULL) {
1316 mergeSingleton(*list, o->nvlst);
1317 return;
1318 }
1319
1320 it = calloc(1, sizeof(*it));
1321 if (it == NULL) {
1322 g_tx.fatal = 1;
1323 return;
1324 }
1325 it->objType = o->objType;
1326 it->nvlst = rsconfTranslateCloneNvlst(o->nvlst);
1327 if (it->nvlst == NULL && o->nvlst != NULL) {
1328 g_tx.fatal = 1;
1329 itemDestruct(it);
1330 return;
1331 }
1332 it->subobjs = cloneObjlst(o->subobjs);
1333 if (it->subobjs == NULL && o->subobjs != NULL) {
1334 g_tx.fatal = 1;
1335 itemDestruct(it);
1336 return;
1337 }
1338 it->source = source ? strdup(source) : NULL;
1339 if (source != NULL && it->source == NULL) {
1340 g_tx.fatal = 1;
1341 itemDestruct(it);
1342 return;
1343 }
1344 it->line = line;
1345 if (o->objType == CNFOBJ_RULESET && o->script != NULL) {
1346 estr = es_newStr(256);
1347 if (estr == NULL || stmtListToString(&estr, o->script, 1, &it->warnings) != 0) {
1348 addWarning(&it->warnings, "translator: failed to serialize ruleset body");
1349 g_tx.fatal = 1;
1350 if (estr != NULL) es_deleteStr(estr);
1351 } else {
1352 it->script = es_str2cstr(estr, NULL);
1353 es_deleteStr(estr);
1354 if (it->script == NULL) {
1355 addWarning(&it->warnings, "translator: out of memory when capturing ruleset body");
1356 g_tx.fatal = 1;
1357 }
1358 }
1359 captureYamlRulesetBody(it, o->script);
1360 }
1361 if (*list == NULL) {
1362 *list = it;
1363 } else {
1364 struct rsconfTranslateItem_s *tail = *list;
1365 while (tail->next != NULL) tail = tail->next;
1366 tail->next = it;
1367 }
1368 }
1369
1370 void rsconfTranslateCaptureScript(const struct cnfstmt *script, const char *source, int line) {
1371 struct rsconfTranslateItem_s *it = g_tx.rulesets;
1372 es_str_t *estr;
1373 struct nvlst *nameNode;
1374 int had_script;
1375
1376 if (!rsconfTranslateEnabled() || script == NULL) return;
1377 while (it != NULL) {
1378 struct nvlst *cur;
1379 for (cur = it->nvlst; cur != NULL; cur = cur->next) {
1380 char *k = es_str2cstr(cur->name, NULL);
1381 int match = k != NULL && !strcasecmp(k, "name");
1382 free(k);
1383 if (match) break;
1384 }
1385 if (cur != NULL) {
1386 char *v = es_str2cstr(cur->val.d.estr, NULL);
1387 int match = v != NULL && !strcmp(v, "RSYSLOG_DefaultRuleset");
1388 free(v);
1389 if (match) break;
1390 }
1391 it = it->next;
1392 }
1393 if (it == NULL) {
1394 es_str_t *nm = es_newStrFromCStr("name", 4);
1395 es_str_t *val = es_newStrFromCStr("RSYSLOG_DefaultRuleset", strlen("RSYSLOG_DefaultRuleset"));
1396 if (nm == NULL || val == NULL) {
1397 es_deleteStr(nm);
1398 es_deleteStr(val);
1399 g_tx.fatal = 1;
1400 return;
1401 }
1402 nameNode = nvlstNewStr(val);
1403 if (nameNode == NULL) {
1404 es_deleteStr(val);
1405 es_deleteStr(nm);
1406 g_tx.fatal = 1;
1407 return;
1408 }
1409 nameNode = nvlstSetName(nameNode, nm);
1410 it = calloc(1, sizeof(*it));
1411 if (it == NULL) {
1412 nvlstDestruct(nameNode);
1413 g_tx.fatal = 1;
1414 return;
1415 }
1416 it->objType = CNFOBJ_RULESET;
1417 it->nvlst = nameNode;
1418 it->source = source ? strdup(source) : NULL;
1419 it->line = line;
1420 addWarning(&it->warnings, "top-level statements normalized into explicit RSYSLOG_DefaultRuleset");
1421 if (g_tx.rulesets == NULL) {
1422 g_tx.rulesets = it;
1423 } else {
1424 struct rsconfTranslateItem_s *tail = g_tx.rulesets;
1425 while (tail->next != NULL) tail = tail->next;
1426 tail->next = it;
1427 }
1428 }
1429 had_script = it->script != NULL;
1430 estr = es_newStr(256);
1431 if (estr == NULL || stmtListToString(&estr, script, 1, &it->warnings) != 0) {
1432 addWarning(&it->warnings, "translator: failed to serialize top-level script");
1433 g_tx.fatal = 1;
1434 if (estr != NULL) es_deleteStr(estr);
1435 return;
1436 }
1437 if (it->script == NULL) {
1438 it->script = es_str2cstr(estr, NULL);
1439 if (it->script == NULL) {
1440 addWarning(&it->warnings, "translator: out of memory when capturing script");
1441 g_tx.fatal = 1;
1442 }
1443 } else {
1444 char *more = es_str2cstr(estr, NULL);
1445 if (more == NULL) {
1446 addWarning(&it->warnings, "translator: out of memory when capturing script");
1447 g_tx.fatal = 1;
1448 es_deleteStr(estr);
1449 return;
1450 }
1451 size_t oldlen = strlen(it->script);
1452 size_t addlen = strlen(more);
1453 char *merged = realloc(it->script, oldlen + addlen + 1);
1454 if (merged == NULL) {
1455 addWarning(&it->warnings, "translator: out of memory when merging script parts");
1456 g_tx.fatal = 1;
1457 } else {
1458 memcpy(merged + oldlen, more, addlen + 1);
1459 it->script = merged;
1460 }
1461 free(more);
1462 }
1463 es_deleteStr(estr);
1464 if (had_script) {
1465 int oom = 0;
1466 if (!appendYamlLegacyStatement(it, script, &oom)) {
1467 if (oom) {
1468 addWarning(&it->warnings, "translator: out of memory when capturing YAML statements");
1469 g_tx.fatal = 1;
1470 } else {
1471 free(it->yaml_filter);
1472 it->yaml_filter = NULL;
1473 yamlActionDestruct(it->yaml_actions);
1474 it->yaml_actions = NULL;
1475 yamlStatementDestruct(it->yaml_statements);
1476 it->yaml_statements = NULL;
1477 it->yaml_ruleset_kind = RS_TRANSLATE_YAML_RULESET_NONE;
1478 }
1479 }
1480 } else {
1481 captureYamlRulesetBody(it, script);
1482 }
1483 }
1484
1485 static void writeWarningComments(FILE *fp, const struct rsconfTranslateWarning_s *w, int indent) {
1486 while (w != NULL) {
1487 int i;
1488 for (i = 0; i < indent; ++i) fputs(" ", fp);
1489 fprintf(fp, "# TRANSLATION WARNING: %s\n", w->msg);
1490 w = w->next;
1491 }
1492 }
1493
1494 static void logWarnings(const struct rsconfTranslateWarning_s *w) {
1495 while (w != NULL) {
1496 LogError(0, RS_RET_CONF_PARSE_ERROR, "%s", w->msg);
1497 w = w->next;
1498 }
1499 }
1500
1501 static void logItemWarnings(const struct rsconfTranslateItem_s *items) {
1502 while (items != NULL) {
1503 logWarnings(items->warnings);
1504 items = items->next;
1505 }
1506 }
1507
1508 static void logAllWarnings(void) {
1509 logWarnings(g_tx.globalWarnings);
1510 logItemWarnings(g_tx.globals);
1511 logItemWarnings(g_tx.mainqueue);
1512 logItemWarnings(g_tx.modules);
1513 logItemWarnings(g_tx.inputs);
1514 logItemWarnings(g_tx.templates);
1515 logItemWarnings(g_tx.rulesets);
1516 logItemWarnings(g_tx.lookups);
1517 logItemWarnings(g_tx.parsers);
1518 logItemWarnings(g_tx.timezones);
1519 logItemWarnings(g_tx.dynstats);
1520 logItemWarnings(g_tx.perctilestats);
1521 logItemWarnings(g_tx.ratelimits);
1522 }
1523
1524 static void writeYamlQuoted(FILE *fp, const char *s) {
1525 fputc('"', fp);
1526 while (*s != '\0') {
1527 switch (*s) {
1528 case '"':
1529 fputs("\\\"", fp);
1530 break;
1531 case '\\':
1532 fputs("\\\\", fp);
1533 break;
1534 case '\n':
1535 fputs("\\n", fp);
1536 break;
1537 case '\r':
1538 fputs("\\r", fp);
1539 break;
1540 case '\t':
1541 fputs("\\t", fp);
1542 break;
1543 default:
1544 fputc(*s, fp);
1545 break;
1546 }
1547 ++s;
1548 }
1549 fputc('"', fp);
1550 }
1551
1552 static void writeYamlValue(FILE *fp, const struct nvlst *n) {
1553 int i;
1554 char *s;
1555 switch (n->val.datatype) {
1556 case 'N':
1557 fprintf(fp, "%lld", n->val.d.n);
1558 break;
1559 case 'A':
1560 fputc('[', fp);
1561 for (i = 0; i < n->val.d.ar->nmemb; ++i) {
1562 s = es_str2cstr(n->val.d.ar->arr[i], NULL);
1563 if (i > 0) fputs(", ", fp);
1564 writeYamlQuoted(fp, s == NULL ? "" : s);
1565 free(s);
1566 }
1567 fputc(']', fp);
1568 break;
1569 case 'S':
1570 default:
1571 s = es_str2cstr(n->val.d.estr, NULL);
1572 writeYamlQuoted(fp, s == NULL ? "" : s);
1573 free(s);
1574 break;
1575 }
1576 }
1577
1578 static int preferredKeyRank(const struct nvlst *n) {
1579 char *name;
1580 int rank = 3;
1581 if (n == NULL) return rank;
1582 name = es_str2cstr(n->name, NULL);
1583 if (name != NULL) {
1584 if (!strcmp(name, "name")) {
1585 rank = 0;
1586 } else if (!strcmp(name, "type")) {
1587 rank = 1;
1588 } else if (!strcmp(name, "load")) {
1589 rank = 2;
1590 }
1591 }
1592 free(name);
1593 return rank;
1594 }
1595
1596 static int nvlstSortComesBefore(const struct nvlst *a, const struct nvlst *b) {
1597 char *nameA;
1598 char *nameB;
1599 int rankA;
1600 int rankB;
1601 int cmp;
1602
1603 if (b == NULL) return 1;
1604 if (a == NULL) return 0;
1605
1606 rankA = preferredKeyRank(a);
1607 rankB = preferredKeyRank(b);
1608 if (rankA != rankB) return rankA < rankB;
1609
1610 nameA = es_str2cstr(a->name, NULL);
1611 nameB = es_str2cstr(b->name, NULL);
1612 if (nameA == NULL || nameB == NULL) {
1613 free(nameA);
1614 free(nameB);
1615 return 0;
1616 }
1617 cmp = strcmp(nameA, nameB);
1618 free(nameA);
1619 free(nameB);
1620 return cmp < 0;
1621 }
1622
1623 static void writeYamlEntry(FILE *fp, const struct nvlst *n, int indent, int list_prefix) {
1624 char *name;
1625 int i;
1626 for (i = 0; i < indent; ++i) fputs(" ", fp);
1627 if (list_prefix) fputs("- ", fp);
1628 name = es_str2cstr(n->name, NULL);
1629 fprintf(fp, "%s: ", name == NULL ? "" : name);
1630 free(name);
1631 writeYamlValue(fp, n);
1632 fputc('\n', fp);
1633 }
1634
1635 static void writeYamlMappingExcept(FILE *fp, const struct nvlst *lst, int indent, const struct nvlst *skip) {
1636 const struct nvlst *n;
1637 int rank;
1638 for (rank = 0; rank < 4; ++rank) {
1639 for (n = lst; n != NULL; n = n->next) {
1640 if (n == skip || preferredKeyRank(n) != rank) continue;
1641 writeYamlEntry(fp, n, indent, 0);
1642 }
1643 }
1644 }
1645
1646 static void writeYamlMapping(FILE *fp, const struct nvlst *lst, int indent) {
1647 writeYamlMappingExcept(fp, lst, indent, NULL);
1648 }
1649
1650 static void writeYamlBlockScalar(FILE *fp, const char *content, int indent) {
1651 const char *cur = content;
1652 int i;
1653
1654 while (cur != NULL && *cur != '\0') {
1655 for (i = 0; i < indent; ++i) fputs(" ", fp);
1656 while (*cur != '\0' && *cur != '\n') {
1657 fputc(*cur++, fp);
1658 }
1659 fputc('\n', fp);
1660 if (*cur == '\n') ++cur;
1661 }
1662 }
1663
1664 static void writeYamlActions(FILE *fp, const struct rsconfTranslateYamlAction_s *actions, int indent) {
1665 const struct rsconfTranslateYamlAction_s *act;
1666 int i;
1667
1668 for (i = 0; i < indent; ++i) fputs(" ", fp);
1669 fputs("actions:\n", fp);
1670 for (act = actions; act != NULL; act = act->next) {
1671 const struct nvlst *n;
1672 const struct nvlst *firstNode = NULL;
1673 int firstRank = 4;
1674 for (n = act->nvlst; n != NULL; n = n->next) {
1675 int rank = preferredKeyRank(n);
1676 if (rank < firstRank) {
1677 firstNode = n;
1678 firstRank = rank;
1679 }
1680 }
1681 if (firstNode != NULL) {
1682 writeYamlEntry(fp, firstNode, indent + 1, 1);
1683 } else {
1684 for (i = 0; i < indent + 1; ++i) fputs(" ", fp);
1685 fputs("-\n", fp);
1686 }
1687 writeYamlMappingExcept(fp, act->nvlst, indent + 2, firstNode);
1688 }
1689 }
1690
1691 static int yamlActionCount(const struct rsconfTranslateYamlAction_s *actions) {
1692 int n = 0;
1693 while (actions != NULL) {
1694 ++n;
1695 actions = actions->next;
1696 }
1697 return n;
1698 }
1699
1700 static void writeYamlActionList(FILE *fp, const struct rsconfTranslateYamlAction_s *actions, int indent) {
1701 const struct rsconfTranslateYamlAction_s *act;
1702 int i;
1703
1704 for (act = actions; act != NULL; act = act->next) {
1705 const struct nvlst *n;
1706 const struct nvlst *firstNode = NULL;
1707 int firstRank = 4;
1708 for (n = act->nvlst; n != NULL; n = n->next) {
1709 int rank = preferredKeyRank(n);
1710 if (rank < firstRank) {
1711 firstNode = n;
1712 firstRank = rank;
1713 }
1714 }
1715 if (firstNode != NULL) {
1716 writeYamlEntry(fp, firstNode, indent, 1);
1717 } else {
1718 for (i = 0; i < indent; ++i) fputs(" ", fp);
1719 fputs("-\n", fp);
1720 }
1721 writeYamlMappingExcept(fp, act->nvlst, indent + 1, firstNode);
1722 }
1723 }
1724
1725 static void writeYamlActionMapping(FILE *fp, const struct nvlst *lst, int indent) {
1726 const struct nvlst *n;
1727 const struct nvlst *firstNode = NULL;
1728 int firstRank = 4;
1729
1730 for (n = lst; n != NULL; n = n->next) {
1731 int rank = preferredKeyRank(n);
1732 if (rank < firstRank) {
1733 firstNode = n;
1734 firstRank = rank;
1735 }
1736 }
1737 if (firstNode != NULL) {
1738 writeYamlEntry(fp, firstNode, indent, 0);
1739 }
1740 writeYamlMappingExcept(fp, lst, indent, firstNode);
1741 }
1742
1743 static void writeYamlStatements(FILE *fp, const struct rsconfTranslateYamlStatement_s *stmts, int indent) {
1744 const struct rsconfTranslateYamlStatement_s *stmt;
1745 int i;
1746
1747 for (i = 0; i < indent; ++i) fputs(" ", fp);
1748 fputs("statements:\n", fp);
1749 for (stmt = stmts; stmt != NULL; stmt = stmt->next) {
1750 for (i = 0; i < indent + 1; ++i) fputs(" ", fp);
1751 fputs("- if: ", fp);
1752 writeYamlQuoted(fp, stmt->if_expr == NULL ? "" : stmt->if_expr);
1753 fputc('\n', fp);
1754 if (yamlActionCount(stmt->actions) == 1) {
1755 for (i = 0; i < indent + 2; ++i) fputs(" ", fp);
1756 fputs("action:\n", fp);
1757 writeYamlActionMapping(fp, stmt->actions->nvlst, indent + 3);
1758 } else {
1759 for (i = 0; i < indent + 2; ++i) fputs(" ", fp);
1760 fputs("then:\n", fp);
1761 writeYamlActionList(fp, stmt->actions, indent + 3);
1762 }
1763 }
1764 }
1765
1766 static void writeYamlListSection(FILE *fp, const char *name, const struct rsconfTranslateItem_s *items) {
1767 const struct rsconfTranslateItem_s *it;
1768 if (items == NULL) return;
1769 fprintf(fp, "%s:\n", name);
1770 for (it = items; it != NULL; it = it->next) {
1771 const struct nvlst *n;
1772 const struct nvlst *firstNode = NULL;
1773 int firstRank = 4;
1774 writeWarningComments(fp, it->warnings, 1);
1775 for (n = it->nvlst; n != NULL; n = n->next) {
1776 int rank = preferredKeyRank(n);
1777 if (rank < firstRank) {
1778 firstNode = n;
1779 firstRank = rank;
1780 }
1781 }
1782 if (it->nvlst == NULL && it->script == NULL) {
1783 fputs(" -\n", fp);
1784 continue;
1785 }
1786 if (firstNode != NULL) {
1787 writeYamlEntry(fp, firstNode, 1, 1);
1788 } else {
1789 fputs(" -\n", fp);
1790 }
1791 writeYamlMappingExcept(fp, it->nvlst, 2, firstNode);
1792 if (it->subobjs != NULL) {
1793 fputs(" elements:\n", fp);
1794 for (const struct objlst *obj = it->subobjs; obj != NULL; obj = obj->next) {
1795 fprintf(fp, " - %s:\n", obj->obj->objType == CNFOBJ_PROPERTY ? "property" : "constant");
1796 writeYamlMapping(fp, obj->obj->nvlst, 4);
1797 }
1798 }
1799 if (it->yaml_ruleset_kind == RS_TRANSLATE_YAML_RULESET_FILTER_ACTIONS && it->yaml_filter != NULL) {
1800 fputs(" filter: ", fp);
1801 writeYamlQuoted(fp, it->yaml_filter);
1802 fputc('\n', fp);
1803 writeYamlActions(fp, it->yaml_actions, 2);
1804 } else if (it->yaml_ruleset_kind == RS_TRANSLATE_YAML_RULESET_ACTIONS) {
1805 writeYamlActions(fp, it->yaml_actions, 2);
1806 } else if (it->yaml_ruleset_kind == RS_TRANSLATE_YAML_RULESET_STATEMENTS) {
1807 writeYamlStatements(fp, it->yaml_statements, 2);
1808 } else if (it->script != NULL) {
1809 fputs(" script: |\n", fp);
1810 writeYamlBlockScalar(fp, it->script, 3);
1811 }
1812 }
1813 }
1814
1815 static void writeRsParams(FILE *fp, const struct nvlst *lst) {
1816 const struct nvlst *n;
1817 char *name;
1818 int first = 1, i, rank;
1819 for (rank = 0; rank < 4; ++rank) {
1820 for (n = lst; n != NULL; n = n->next) {
1821 if (preferredKeyRank(n) != rank) continue;
1822 if (!first) fputc(' ', fp);
1823 first = 0;
1824 name = es_str2cstr(n->name, NULL);
1825 fprintf(fp, "%s=", name == NULL ? "" : name);
1826 free(name);
1827 switch (n->val.datatype) {
1828 case 'N':
1829 fprintf(fp, "%lld", n->val.d.n);
1830 break;
1831 case 'A':
1832 fputc('[', fp);
1833 for (i = 0; i < n->val.d.ar->nmemb; ++i) {
1834 char *aval = es_str2cstr(n->val.d.ar->arr[i], NULL);
1835 if (i > 0) fputs(", ", fp);
1836 writeYamlQuoted(fp, aval == NULL ? "" : aval);
1837 free(aval);
1838 }
1839 fputc(']', fp);
1840 break;
1841 case 'S':
1842 default: {
1843 char *val = es_str2cstr(n->val.d.estr, NULL);
1844 writeYamlQuoted(fp, val == NULL ? "" : val);
1845 free(val);
1846 break;
1847 }
1848 }
1849 }
1850 }
1851 }
1852
1853 static void writeRsTemplate(FILE *fp, const struct rsconfTranslateItem_s *it) {
1854 fprintf(fp, "template(");
1855 writeRsParams(fp, it->nvlst);
1856 if (it->subobjs == NULL) {
1857 fprintf(fp, ")\n\n");
1858 return;
1859 }
1860 fprintf(fp, ") {\n");
1861 for (const struct objlst *obj = it->subobjs; obj != NULL; obj = obj->next) {
1862 fprintf(fp, " %s(", obj->obj->objType == CNFOBJ_PROPERTY ? "property" : "constant");
1863 writeRsParams(fp, obj->obj->nvlst);
1864 fprintf(fp, ")\n");
1865 }
1866 fprintf(fp, "}\n\n");
1867 }
1868
1869 static void writeRsList(FILE *fp, const char *keyword, const struct rsconfTranslateItem_s *items) {
1870 const struct rsconfTranslateItem_s *it;
1871 for (it = items; it != NULL; it = it->next) {
1872 writeWarningComments(fp, it->warnings, 0);
1873 if (it->objType == CNFOBJ_TPL) {
1874 writeRsTemplate(fp, it);
1875 continue;
1876 }
1877 if (it->objType == CNFOBJ_RULESET) {
1878 fprintf(fp, "ruleset(");
1879 writeRsParams(fp, it->nvlst);
1880 if (it->script != NULL) {
1881 fprintf(fp, ") {\n%s}\n\n", it->script);
1882 } else {
1883 fprintf(fp, ") {}\n\n");
1884 }
1885 continue;
1886 }
1887 fprintf(fp, "%s(", keyword);
1888 writeRsParams(fp, it->nvlst);
1889 fprintf(fp, ")\n\n");
1890 }
1891 }
1892
1893 rsRetVal rsconfTranslateWriteFile(const char *path) {
1894 FILE *fp = NULL;
1895 DEFiRet;
1896
1897 if (!rsconfTranslateEnabled()) {
1898 FINALIZE;
1899 }
1900 if (g_tx.fatal) {
1901 logAllWarnings();
1902 ABORT_FINALIZE(RS_RET_CONF_PARSE_ERROR);
1903 }
1904
1905 if (path == NULL || !strcmp(path, "-")) {
1906 fp = stdout;
1907 } else {
1908 fp = fopen(path, "w");
1909 }
1910 if (fp == NULL) {
1911 ABORT_FINALIZE(RS_RET_FILE_NOT_FOUND);
1912 }
1913
1914 if (g_tx.fmt == RSCONF_TRANSLATE_YAML) {
1915 fputs("version: 2\n\n", fp);
1916 writeWarningComments(fp, g_tx.globalWarnings, 0);
1917 if (g_tx.globals != NULL) {
1918 writeWarningComments(fp, g_tx.globals->warnings, 0);
1919 fputs("global:\n", fp);
1920 writeYamlMapping(fp, g_tx.globals->nvlst, 1);
1921 fputc('\n', fp);
1922 }
1923 if (g_tx.mainqueue != NULL) {
1924 writeWarningComments(fp, g_tx.mainqueue->warnings, 0);
1925 fputs("mainqueue:\n", fp);
1926 writeYamlMapping(fp, g_tx.mainqueue->nvlst, 1);
1927 fputc('\n', fp);
1928 }
1929 writeYamlListSection(fp, "modules", g_tx.modules);
1930 writeYamlListSection(fp, "inputs", g_tx.inputs);
1931 writeYamlListSection(fp, "templates", g_tx.templates);
1932 writeYamlListSection(fp, "rulesets", g_tx.rulesets);
1933 writeYamlListSection(fp, "lookup_tables", g_tx.lookups);
1934 writeYamlListSection(fp, "parsers", g_tx.parsers);
1935 writeYamlListSection(fp, "timezones", g_tx.timezones);
1936 writeYamlListSection(fp, "dyn_stats", g_tx.dynstats);
1937 writeYamlListSection(fp, "perctile_stats", g_tx.perctilestats);
1938 writeYamlListSection(fp, "ratelimits", g_tx.ratelimits);
1939 } else {
1940 writeWarningComments(fp, g_tx.globalWarnings, 0);
1941 writeRsList(fp, "global", g_tx.globals);
1942 writeRsList(fp, "main_queue", g_tx.mainqueue);
1943 writeRsList(fp, "module", g_tx.modules);
1944 writeRsList(fp, "input", g_tx.inputs);
1945 writeRsList(fp, "template", g_tx.templates);
1946 writeRsList(fp, "lookup_table", g_tx.lookups);
1947 writeRsList(fp, "parser", g_tx.parsers);
1948 writeRsList(fp, "timezone", g_tx.timezones);
1949 writeRsList(fp, "dyn_stats", g_tx.dynstats);
1950 writeRsList(fp, "perctile_stats", g_tx.perctilestats);
1951 writeRsList(fp, "ratelimit", g_tx.ratelimits);
1952 writeRsList(fp, "ruleset", g_tx.rulesets);
1953 }
1954
1955 finalize_it:
1956 if (fp != NULL && fp != stdout) fclose(fp);
1957 RETiRet;
1958 }