suricata
conf-yaml-loader.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2023 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Endace Technology Limited - Jason Ish <jason.ish@endace.com>
22  *
23  * YAML configuration loader.
24  */
25 
26 #include "suricata-common.h"
27 #include "conf.h"
28 #include "conf-yaml-loader.h"
29 #include <yaml.h>
30 #include "util-path.h"
31 #include "util-debug.h"
32 #include "util-unittest.h"
33 
34 #define YAML_VERSION_MAJOR 1
35 #define YAML_VERSION_MINOR 1
36 
37 /* The maximum level of recursion allowed while parsing the YAML
38  * file. */
39 #define RECURSION_LIMIT 128
40 
41 /* Sometimes we'll have to create a node name on the fly (integer
42  * conversion, etc), so this is a default length to allocate that will
43  * work most of the time. */
44 #define DEFAULT_NAME_LEN 16
45 
46 #define MANGLE_ERRORS_MAX 10
47 static int mangle_errors = 0;
48 
49 static char *conf_dirname = NULL;
50 
51 static int ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel, int state);
52 
53 /* Configuration processing states. */
54 enum conf_state {
55  CONF_KEY = 0,
58 };
59 
60 /**
61  * \brief Mangle unsupported characters.
62  *
63  * \param string A pointer to an null terminated string.
64  *
65  * \retval none
66  */
67 static void
68 Mangle(char *string)
69 {
70  char *c;
71 
72  while ((c = strchr(string, '_')))
73  *c = '-';
74 }
75 
76 /**
77  * \brief Set the directory name of the configuration file.
78  *
79  * \param filename The configuration filename.
80  */
81 static void
82 ConfYamlSetConfDirname(const char *filename)
83 {
84  char *ep;
85 
86  ep = strrchr(filename, '\\');
87  if (ep == NULL)
88  ep = strrchr(filename, '/');
89 
90  if (ep == NULL) {
91  conf_dirname = SCStrdup(".");
92  if (conf_dirname == NULL) {
93  FatalError("ERROR: Failed to allocate memory while loading configuration.");
94  }
95  }
96  else {
97  conf_dirname = SCStrdup(filename);
98  if (conf_dirname == NULL) {
99  FatalError("ERROR: Failed to allocate memory while loading configuration.");
100  }
101  conf_dirname[ep - filename] = '\0';
102  }
103 }
104 
105 /**
106  * \brief Include a file in the configuration.
107  *
108  * \param parent The configuration node the included configuration will be
109  * placed at.
110  * \param filename The filename to include.
111  *
112  * \retval 0 on success, -1 on failure.
113  */
114 int ConfYamlHandleInclude(ConfNode *parent, const char *filename)
115 {
116  yaml_parser_t parser;
117  char include_filename[PATH_MAX];
118  FILE *file = NULL;
119  int ret = -1;
120 
121  if (yaml_parser_initialize(&parser) != 1) {
122  SCLogError("Failed to initialize YAML parser");
123  return -1;
124  }
125 
126  if (PathIsAbsolute(filename)) {
127  strlcpy(include_filename, filename, sizeof(include_filename));
128  }
129  else {
130  snprintf(include_filename, sizeof(include_filename), "%s/%s",
131  conf_dirname, filename);
132  }
133 
134  file = fopen(include_filename, "r");
135  if (file == NULL) {
136  SCLogError("Failed to open configuration include file %s: %s", include_filename,
137  strerror(errno));
138  goto done;
139  }
140 
141  yaml_parser_set_input_file(&parser, file);
142 
143  if (ConfYamlParse(&parser, parent, 0, 0, 0) != 0) {
144  SCLogError("Failed to include configuration file %s", filename);
145  goto done;
146  }
147 
148  ret = 0;
149 
150 done:
151  yaml_parser_delete(&parser);
152  if (file != NULL) {
153  fclose(file);
154  }
155 
156  return ret;
157 }
158 
159 /**
160  * \brief Parse a YAML layer.
161  *
162  * \param parser A pointer to an active yaml_parser_t.
163  * \param parent The parent configuration node.
164  *
165  * \retval 0 on success, -1 on failure.
166  */
167 static int ConfYamlParse(yaml_parser_t *parser, ConfNode *parent, int inseq, int rlevel, int state)
168 {
169  ConfNode *node = parent;
170  yaml_event_t event;
171  memset(&event, 0, sizeof(event));
172  int done = 0;
173  int seq_idx = 0;
174  int retval = 0;
175  int was_empty = -1;
176  int include_count = 0;
177 
178  if (rlevel++ > RECURSION_LIMIT) {
179  SCLogError("Recursion limit reached while parsing "
180  "configuration file, aborting.");
181  return -1;
182  }
183 
184  while (!done) {
185  if (!yaml_parser_parse(parser, &event)) {
186  SCLogError("Failed to parse configuration file at line %" PRIuMAX ": %s",
187  (uintmax_t)parser->problem_mark.line, parser->problem);
188  retval = -1;
189  break;
190  }
191 
192  if (event.type == YAML_DOCUMENT_START_EVENT) {
193  SCLogDebug("event.type=YAML_DOCUMENT_START_EVENT; state=%d", state);
194  /* Verify YAML version - its more likely to be a valid
195  * Suricata configuration file if the version is
196  * correct. */
197  yaml_version_directive_t *ver =
198  event.data.document_start.version_directive;
199  if (ver == NULL) {
200  SCLogError("ERROR: Invalid configuration file.");
201  SCLogError("The configuration file must begin with the following two lines: %%YAML "
202  "1.1 and ---");
203  goto fail;
204  }
205  int major = ver->major;
206  int minor = ver->minor;
207  if (!(major == YAML_VERSION_MAJOR && minor == YAML_VERSION_MINOR)) {
208  SCLogError("ERROR: Invalid YAML version. Must be 1.1");
209  goto fail;
210  }
211  }
212  else if (event.type == YAML_SCALAR_EVENT) {
213  char *value = (char *)event.data.scalar.value;
214  char *tag = (char *)event.data.scalar.tag;
215  SCLogDebug("event.type=YAML_SCALAR_EVENT; state=%d; value=%s; "
216  "tag=%s; inseq=%d", state, value, tag, inseq);
217 
218  /* Skip over empty scalar values while in KEY state. This
219  * tends to only happen on an empty file, where a scalar
220  * event probably shouldn't fire anyways. */
221  if (state == CONF_KEY && strlen(value) == 0) {
222  goto next;
223  }
224 
225  /* If the value is unquoted, certain strings in YAML represent NULL. */
226  if ((inseq || state == CONF_VAL) &&
227  event.data.scalar.style == YAML_PLAIN_SCALAR_STYLE) {
228  if (strlen(value) == 0 || strcmp(value, "~") == 0 || strcmp(value, "null") == 0 ||
229  strcmp(value, "Null") == 0 || strcmp(value, "NULL") == 0) {
230  value = NULL;
231  }
232  }
233 
234  if (inseq) {
235  if (state == CONF_INCLUDE) {
236  if (value != NULL) {
237  SCLogInfo("Including configuration file %s.", value);
238  if (ConfYamlHandleInclude(parent, value) != 0) {
239  goto fail;
240  }
241  }
242  goto next;
243  }
244  char sequence_node_name[DEFAULT_NAME_LEN];
245  snprintf(sequence_node_name, DEFAULT_NAME_LEN, "%d", seq_idx++);
246  ConfNode *seq_node = NULL;
247  if (was_empty < 0) {
248  // initialize was_empty
249  if (TAILQ_EMPTY(&parent->head)) {
250  was_empty = 1;
251  } else {
252  was_empty = 0;
253  }
254  }
255  // we only check if the node's list was not empty at first
256  if (was_empty == 0) {
257  seq_node = ConfNodeLookupChild(parent, sequence_node_name);
258  }
259  if (seq_node != NULL) {
260  /* The sequence node has already been set, probably
261  * from the command line. Remove it so it gets
262  * re-added in the expected order for iteration.
263  */
264  TAILQ_REMOVE(&parent->head, seq_node, next);
265  }
266  else {
267  seq_node = ConfNodeNew();
268  if (unlikely(seq_node == NULL)) {
269  goto fail;
270  }
271  seq_node->name = SCStrdup(sequence_node_name);
272  if (unlikely(seq_node->name == NULL)) {
273  SCFree(seq_node);
274  goto fail;
275  }
276  if (value != NULL) {
277  seq_node->val = SCStrdup(value);
278  if (unlikely(seq_node->val == NULL)) {
279  SCFree(seq_node->name);
280  goto fail;
281  }
282  } else {
283  seq_node->val = NULL;
284  }
285  }
286  TAILQ_INSERT_TAIL(&parent->head, seq_node, next);
287  }
288  else {
289  if (state == CONF_INCLUDE) {
290  SCLogInfo("Including configuration file %s.", value);
291  if (ConfYamlHandleInclude(parent, value) != 0) {
292  goto fail;
293  }
294  state = CONF_KEY;
295  }
296  else if (state == CONF_KEY) {
297 
298  if (strcmp(value, "include") == 0) {
299  state = CONF_INCLUDE;
300  if (++include_count > 1) {
301  SCLogWarning("Multipline \"include\" fields at the same level are "
302  "deprecated and will not work in Suricata 8, please move "
303  "to an array of include files: line: %zu",
304  parser->mark.line);
305  }
306  goto next;
307  }
308 
309  if (parent->is_seq) {
310  if (parent->val == NULL) {
311  parent->val = SCStrdup(value);
312  if (parent->val && strchr(parent->val, '_'))
313  Mangle(parent->val);
314  }
315  }
316 
317  if (strchr(value, '.') != NULL) {
318  node = ConfNodeGetNodeOrCreate(parent, value, 0);
319  if (node == NULL) {
320  /* Error message already logged. */
321  goto fail;
322  }
323  } else {
324  ConfNode *existing = ConfNodeLookupChild(parent, value);
325  if (existing != NULL) {
326  if (!existing->final) {
327  SCLogInfo("Configuration node '%s' redefined.", existing->name);
328  ConfNodePrune(existing);
329  }
330  node = existing;
331  } else {
332  node = ConfNodeNew();
333  node->name = SCStrdup(value);
334  node->parent = parent;
335  if (node->name && strchr(node->name, '_')) {
336  if (!(parent->name &&
337  ((strcmp(parent->name, "address-groups") == 0) ||
338  (strcmp(parent->name, "port-groups") == 0)))) {
339  Mangle(node->name);
340  if (mangle_errors < MANGLE_ERRORS_MAX) {
341  SCLogWarning(
342  "%s is deprecated. Please use %s on line %" PRIuMAX
343  ".",
344  value, node->name,
345  (uintmax_t)parser->mark.line + 1);
346  mangle_errors++;
347  if (mangle_errors >= MANGLE_ERRORS_MAX)
348  SCLogWarning("not showing more "
349  "parameter name warnings.");
350  }
351  }
352  }
353  TAILQ_INSERT_TAIL(&parent->head, node, next);
354  }
355  }
356  state = CONF_VAL;
357  }
358  else {
359  if (value != NULL && (tag != NULL) && (strcmp(tag, "!include") == 0)) {
360  SCLogInfo("Including configuration file %s at "
361  "parent node %s.", value, node->name);
362  if (ConfYamlHandleInclude(node, value) != 0)
363  goto fail;
364  } else if (!node->final && value != NULL) {
365  if (node->val != NULL)
366  SCFree(node->val);
367  node->val = SCStrdup(value);
368  }
369  state = CONF_KEY;
370  }
371  }
372  }
373  else if (event.type == YAML_SEQUENCE_START_EVENT) {
374  SCLogDebug("event.type=YAML_SEQUENCE_START_EVENT; state=%d", state);
375  /* If we're processing a list of includes, use the current parent. */
376  if (ConfYamlParse(parser, state == CONF_INCLUDE ? parent : node, 1, rlevel,
377  state == CONF_INCLUDE ? CONF_INCLUDE : 0) != 0)
378  goto fail;
379  node->is_seq = 1;
380  state = CONF_KEY;
381  }
382  else if (event.type == YAML_SEQUENCE_END_EVENT) {
383  SCLogDebug("event.type=YAML_SEQUENCE_END_EVENT; state=%d", state);
384  done = 1;
385  }
386  else if (event.type == YAML_MAPPING_START_EVENT) {
387  SCLogDebug("event.type=YAML_MAPPING_START_EVENT; state=%d", state);
388  if (state == CONF_INCLUDE) {
389  SCLogError("Include fields cannot be a mapping: line %zu", parser->mark.line);
390  goto fail;
391  }
392  if (inseq) {
393  char sequence_node_name[DEFAULT_NAME_LEN];
394  snprintf(sequence_node_name, DEFAULT_NAME_LEN, "%d", seq_idx++);
395  ConfNode *seq_node = NULL;
396  if (was_empty < 0) {
397  // initialize was_empty
398  if (TAILQ_EMPTY(&node->head)) {
399  was_empty = 1;
400  } else {
401  was_empty = 0;
402  }
403  }
404  // we only check if the node's list was not empty at first
405  if (was_empty == 0) {
406  seq_node = ConfNodeLookupChild(node, sequence_node_name);
407  }
408  if (seq_node != NULL) {
409  /* The sequence node has already been set, probably
410  * from the command line. Remove it so it gets
411  * re-added in the expected order for iteration.
412  */
413  TAILQ_REMOVE(&node->head, seq_node, next);
414  }
415  else {
416  seq_node = ConfNodeNew();
417  if (unlikely(seq_node == NULL)) {
418  goto fail;
419  }
420  seq_node->name = SCStrdup(sequence_node_name);
421  if (unlikely(seq_node->name == NULL)) {
422  SCFree(seq_node);
423  goto fail;
424  }
425  }
426  seq_node->is_seq = 1;
427  TAILQ_INSERT_TAIL(&node->head, seq_node, next);
428  if (ConfYamlParse(parser, seq_node, 0, rlevel, 0) != 0)
429  goto fail;
430  }
431  else {
432  if (ConfYamlParse(parser, node, inseq, rlevel, 0) != 0)
433  goto fail;
434  }
435  state = CONF_KEY;
436  }
437  else if (event.type == YAML_MAPPING_END_EVENT) {
438  SCLogDebug("event.type=YAML_MAPPING_END_EVENT; state=%d", state);
439  done = 1;
440  }
441  else if (event.type == YAML_STREAM_END_EVENT) {
442  SCLogDebug("event.type=YAML_STREAM_END_EVENT; state=%d", state);
443  done = 1;
444  }
445 
446  next:
447  yaml_event_delete(&event);
448  continue;
449 
450  fail:
451  yaml_event_delete(&event);
452  retval = -1;
453  break;
454  }
455 
456  rlevel--;
457  return retval;
458 }
459 
460 /**
461  * \brief Load configuration from a YAML file.
462  *
463  * This function will load a configuration file. On failure -1 will
464  * be returned and it is suggested that the program then exit. Any
465  * errors while loading the configuration file will have already been
466  * logged.
467  *
468  * \param filename Filename of configuration file to load.
469  *
470  * \retval 0 on success, -1 on failure.
471  */
472 int
473 ConfYamlLoadFile(const char *filename)
474 {
475  FILE *infile;
476  yaml_parser_t parser;
477  int ret;
478  ConfNode *root = ConfGetRootNode();
479 
480  if (yaml_parser_initialize(&parser) != 1) {
481  SCLogError("failed to initialize yaml parser.");
482  return -1;
483  }
484 
485  struct stat stat_buf;
486  if (stat(filename, &stat_buf) == 0) {
487  if (stat_buf.st_mode & S_IFDIR) {
488  SCLogError("yaml argument is not a file but a directory: %s. "
489  "Please specify the yaml file in your -c option.",
490  filename);
491  yaml_parser_delete(&parser);
492  return -1;
493  }
494  }
495 
496  // coverity[toctou : FALSE]
497  infile = fopen(filename, "r");
498  if (infile == NULL) {
499  SCLogError("failed to open file: %s: %s", filename, strerror(errno));
500  yaml_parser_delete(&parser);
501  return -1;
502  }
503 
504  if (conf_dirname == NULL) {
505  ConfYamlSetConfDirname(filename);
506  }
507 
508  yaml_parser_set_input_file(&parser, infile);
509  ret = ConfYamlParse(&parser, root, 0, 0, 0);
510  yaml_parser_delete(&parser);
511  fclose(infile);
512 
513  return ret;
514 }
515 
516 /**
517  * \brief Load configuration from a YAML string.
518  */
519 int
520 ConfYamlLoadString(const char *string, size_t len)
521 {
522  ConfNode *root = ConfGetRootNode();
523  yaml_parser_t parser;
524  int ret;
525 
526  if (yaml_parser_initialize(&parser) != 1) {
527  fprintf(stderr, "Failed to initialize yaml parser.\n");
528  exit(EXIT_FAILURE);
529  }
530  yaml_parser_set_input_string(&parser, (const unsigned char *)string, len);
531  ret = ConfYamlParse(&parser, root, 0, 0, 0);
532  yaml_parser_delete(&parser);
533 
534  return ret;
535 }
536 
537 /**
538  * \brief Load configuration from a YAML file, insert in tree at 'prefix'
539  *
540  * This function will load a configuration file and insert it into the
541  * config tree at 'prefix'. This means that if this is called with prefix
542  * "abc" and the file contains a parameter "def", it will be loaded as
543  * "abc.def".
544  *
545  * \param filename Filename of configuration file to load.
546  * \param prefix Name prefix to use.
547  *
548  * \retval 0 on success, -1 on failure.
549  */
550 int
551 ConfYamlLoadFileWithPrefix(const char *filename, const char *prefix)
552 {
553  FILE *infile;
554  yaml_parser_t parser;
555  int ret;
556  ConfNode *root = ConfGetNode(prefix);
557 
558  if (yaml_parser_initialize(&parser) != 1) {
559  SCLogError("failed to initialize yaml parser.");
560  return -1;
561  }
562 
563  struct stat stat_buf;
564  /* coverity[toctou] */
565  if (stat(filename, &stat_buf) == 0) {
566  if (stat_buf.st_mode & S_IFDIR) {
567  SCLogError("yaml argument is not a file but a directory: %s. "
568  "Please specify the yaml file in your -c option.",
569  filename);
570  return -1;
571  }
572  }
573 
574  /* coverity[toctou] */
575  infile = fopen(filename, "r");
576  if (infile == NULL) {
577  SCLogError("failed to open file: %s: %s", filename, strerror(errno));
578  yaml_parser_delete(&parser);
579  return -1;
580  }
581 
582  if (conf_dirname == NULL) {
583  ConfYamlSetConfDirname(filename);
584  }
585 
586  if (root == NULL) {
587  /* if node at 'prefix' doesn't yet exist, add a place holder */
588  ConfSet(prefix, "<prefix root node>");
589  root = ConfGetNode(prefix);
590  if (root == NULL) {
591  fclose(infile);
592  yaml_parser_delete(&parser);
593  return -1;
594  }
595  }
596  yaml_parser_set_input_file(&parser, infile);
597  ret = ConfYamlParse(&parser, root, 0, 0, 0);
598  yaml_parser_delete(&parser);
599  fclose(infile);
600 
601  return ret;
602 }
603 
604 #ifdef UNITTESTS
605 
606 static int
607 ConfYamlSequenceTest(void)
608 {
609  char input[] = "\
610 %YAML 1.1\n\
611 ---\n\
612 rule-files:\n\
613  - netbios.rules\n\
614  - x11.rules\n\
615 \n\
616 default-log-dir: /tmp\n\
617 ";
618 
620  ConfInit();
621 
622  ConfYamlLoadString(input, strlen(input));
623 
624  ConfNode *node;
625  node = ConfGetNode("rule-files");
626  FAIL_IF_NULL(node);
628  FAIL_IF(TAILQ_EMPTY(&node->head));
629  int i = 0;
630  ConfNode *filename;
631  TAILQ_FOREACH(filename, &node->head, next) {
632  if (i == 0) {
633  FAIL_IF(strcmp(filename->val, "netbios.rules") != 0);
634  FAIL_IF(ConfNodeIsSequence(filename));
635  FAIL_IF(filename->is_seq != 0);
636  }
637  else if (i == 1) {
638  FAIL_IF(strcmp(filename->val, "x11.rules") != 0);
639  FAIL_IF(ConfNodeIsSequence(filename));
640  }
641  FAIL_IF(i > 1);
642  i++;
643  }
644 
645  ConfDeInit();
647  PASS;
648 }
649 
650 static int
651 ConfYamlLoggingOutputTest(void)
652 {
653  char input[] = "\
654 %YAML 1.1\n\
655 ---\n\
656 logging:\n\
657  output:\n\
658  - interface: console\n\
659  log-level: error\n\
660  - interface: syslog\n\
661  facility: local4\n\
662  log-level: info\n\
663 ";
664 
666  ConfInit();
667 
668  ConfYamlLoadString(input, strlen(input));
669 
670  ConfNode *outputs;
671  outputs = ConfGetNode("logging.output");
672  FAIL_IF_NULL(outputs);
673 
674  ConfNode *output;
675  ConfNode *output_param;
676 
677  output = TAILQ_FIRST(&outputs->head);
678  FAIL_IF_NULL(output);
679  FAIL_IF(strcmp(output->name, "0") != 0);
680 
681  output_param = TAILQ_FIRST(&output->head);
682  FAIL_IF_NULL(output_param);
683  FAIL_IF(strcmp(output_param->name, "interface") != 0);
684  FAIL_IF(strcmp(output_param->val, "console") != 0);
685 
686  output_param = TAILQ_NEXT(output_param, next);
687  FAIL_IF(strcmp(output_param->name, "log-level") != 0);
688  FAIL_IF(strcmp(output_param->val, "error") != 0);
689 
690  output = TAILQ_NEXT(output, next);
691  FAIL_IF_NULL(output);
692  FAIL_IF(strcmp(output->name, "1") != 0);
693 
694  output_param = TAILQ_FIRST(&output->head);
695  FAIL_IF_NULL(output_param);
696  FAIL_IF(strcmp(output_param->name, "interface") != 0);
697  FAIL_IF(strcmp(output_param->val, "syslog") != 0);
698 
699  output_param = TAILQ_NEXT(output_param, next);
700  FAIL_IF(strcmp(output_param->name, "facility") != 0);
701  FAIL_IF(strcmp(output_param->val, "local4") != 0);
702 
703  output_param = TAILQ_NEXT(output_param, next);
704  FAIL_IF(strcmp(output_param->name, "log-level") != 0);
705  FAIL_IF(strcmp(output_param->val, "info") != 0);
706 
707  ConfDeInit();
709 
710  PASS;
711 }
712 
713 /**
714  * Try to load something that is not a valid YAML file.
715  */
716 static int
717 ConfYamlNonYamlFileTest(void)
718 {
720  ConfInit();
721 
722  FAIL_IF(ConfYamlLoadFile("/etc/passwd") != -1);
723 
724  ConfDeInit();
726 
727  PASS;
728 }
729 
730 static int
731 ConfYamlBadYamlVersionTest(void)
732 {
733  char input[] = "\
734 %YAML 9.9\n\
735 ---\n\
736 logging:\n\
737  output:\n\
738  - interface: console\n\
739  log-level: error\n\
740  - interface: syslog\n\
741  facility: local4\n\
742  log-level: info\n\
743 ";
744 
746  ConfInit();
747 
748  FAIL_IF(ConfYamlLoadString(input, strlen(input)) != -1);
749 
750  ConfDeInit();
752 
753  PASS;
754 }
755 
756 static int
757 ConfYamlSecondLevelSequenceTest(void)
758 {
759  char input[] = "\
760 %YAML 1.1\n\
761 ---\n\
762 libhtp:\n\
763  server-config:\n\
764  - apache-php:\n\
765  address: [\"192.168.1.0/24\"]\n\
766  personality: [\"Apache_2_2\", \"PHP_5_3\"]\n\
767  path-parsing: [\"compress_separators\", \"lowercase\"]\n\
768  - iis-php:\n\
769  address:\n\
770  - 192.168.0.0/24\n\
771 \n\
772  personality:\n\
773  - IIS_7_0\n\
774  - PHP_5_3\n\
775 \n\
776  path-parsing:\n\
777  - compress_separators\n\
778 ";
779 
781  ConfInit();
782 
783  FAIL_IF(ConfYamlLoadString(input, strlen(input)) != 0);
784 
785  ConfNode *outputs;
786  outputs = ConfGetNode("libhtp.server-config");
787  FAIL_IF_NULL(outputs);
788 
789  ConfNode *node;
790 
791  node = TAILQ_FIRST(&outputs->head);
792  FAIL_IF_NULL(node);
793  FAIL_IF(strcmp(node->name, "0") != 0);
794 
795  node = TAILQ_FIRST(&node->head);
796  FAIL_IF_NULL(node);
797  FAIL_IF(strcmp(node->name, "apache-php") != 0);
798 
799  node = ConfNodeLookupChild(node, "address");
800  FAIL_IF_NULL(node);
801 
802  node = TAILQ_FIRST(&node->head);
803  FAIL_IF_NULL(node);
804  FAIL_IF(strcmp(node->name, "0") != 0);
805  FAIL_IF(strcmp(node->val, "192.168.1.0/24") != 0);
806 
807  ConfDeInit();
809 
810  PASS;
811 }
812 
813 /**
814  * Test file inclusion support.
815  */
816 static int
817 ConfYamlFileIncludeTest(void)
818 {
819  FILE *config_file;
820 
821  const char config_filename[] = "ConfYamlFileIncludeTest-config.yaml";
822  const char config_file_contents[] =
823  "%YAML 1.1\n"
824  "---\n"
825  "# Include something at the root level.\n"
826  "include: ConfYamlFileIncludeTest-include.yaml\n"
827  "# Test including under a mapping.\n"
828  "mapping: !include ConfYamlFileIncludeTest-include.yaml\n";
829 
830  const char include_filename[] = "ConfYamlFileIncludeTest-include.yaml";
831  const char include_file_contents[] =
832  "%YAML 1.1\n"
833  "---\n"
834  "host-mode: auto\n"
835  "unix-command:\n"
836  " enabled: no\n";
837 
839  ConfInit();
840 
841  /* Write out the test files. */
842  FAIL_IF_NULL((config_file = fopen(config_filename, "w")));
843  FAIL_IF(fwrite(config_file_contents, strlen(config_file_contents), 1, config_file) != 1);
844  fclose(config_file);
845 
846  FAIL_IF_NULL((config_file = fopen(include_filename, "w")));
847  FAIL_IF(fwrite(include_file_contents, strlen(include_file_contents), 1, config_file) != 1);
848  fclose(config_file);
849 
850  /* Reset conf_dirname. */
851  if (conf_dirname != NULL) {
852  SCFree(conf_dirname);
853  conf_dirname = NULL;
854  }
855 
856  FAIL_IF(ConfYamlLoadFile("ConfYamlFileIncludeTest-config.yaml") != 0);
857 
858  /* Check values that should have been loaded into the root of the
859  * configuration. */
860  ConfNode *node;
861  node = ConfGetNode("host-mode");
862  FAIL_IF_NULL(node);
863  FAIL_IF(strcmp(node->val, "auto") != 0);
864 
865  node = ConfGetNode("unix-command.enabled");
866  FAIL_IF_NULL(node);
867  FAIL_IF(strcmp(node->val, "no") != 0);
868 
869  /* Check for values that were included under a mapping. */
870  node = ConfGetNode("mapping.host-mode");
871  FAIL_IF_NULL(node);
872  FAIL_IF(strcmp(node->val, "auto") != 0);
873 
874  node = ConfGetNode("mapping.unix-command.enabled");
875  FAIL_IF_NULL(node);
876  FAIL_IF(strcmp(node->val, "no") != 0);
877 
878  ConfDeInit();
880 
881  unlink(config_filename);
882  unlink(include_filename);
883 
884  PASS;
885 }
886 
887 /**
888  * Test that a configuration section is overridden but subsequent
889  * occurrences.
890  */
891 static int
892 ConfYamlOverrideTest(void)
893 {
894  char config[] = "%YAML 1.1\n"
895  "---\n"
896  "some-log-dir: /var/log\n"
897  "some-log-dir: /tmp\n"
898  "\n"
899  "parent:\n"
900  " child0:\n"
901  " key: value\n"
902  "parent:\n"
903  " child1:\n"
904  " key: value\n"
905  "vars:\n"
906  " address-groups:\n"
907  " HOME_NET: \"[192.168.0.0/16,10.0.0.0/8,172.16.0.0/12]\"\n"
908  " EXTERNAL_NET: any\n"
909  "vars.address-groups.HOME_NET: \"10.10.10.10/32\"\n";
910  const char *value;
911 
913  ConfInit();
914 
915  FAIL_IF(ConfYamlLoadString(config, strlen(config)) != 0);
916  FAIL_IF_NOT(ConfGet("some-log-dir", &value));
917  FAIL_IF(strcmp(value, "/tmp") != 0);
918 
919  /* Test that parent.child0 does not exist, but child1 does. */
920  FAIL_IF_NOT_NULL(ConfGetNode("parent.child0"));
921  FAIL_IF_NOT(ConfGet("parent.child1.key", &value));
922  FAIL_IF(strcmp(value, "value") != 0);
923 
924  /* First check that vars.address-groups.EXTERNAL_NET has the
925  * expected parent of vars.address-groups and save this
926  * pointer. We want to make sure that the overrided value has the
927  * same parent later on. */
928  ConfNode *vars_address_groups = ConfGetNode("vars.address-groups");
929  FAIL_IF_NULL(vars_address_groups);
930  ConfNode *vars_address_groups_external_net = ConfGetNode("vars.address-groups.EXTERNAL_NET");
931  FAIL_IF_NULL(vars_address_groups_external_net);
932  FAIL_IF_NOT(vars_address_groups_external_net->parent == vars_address_groups);
933 
934  /* Now check that HOME_NET has the overrided value. */
935  ConfNode *vars_address_groups_home_net = ConfGetNode("vars.address-groups.HOME_NET");
936  FAIL_IF_NULL(vars_address_groups_home_net);
937  FAIL_IF(strcmp(vars_address_groups_home_net->val, "10.10.10.10/32") != 0);
938 
939  /* And check that it has the correct parent. */
940  FAIL_IF_NOT(vars_address_groups_home_net->parent == vars_address_groups);
941 
942  ConfDeInit();
944 
945  PASS;
946 }
947 
948 /**
949  * Test that a configuration parameter loaded from YAML doesn't
950  * override a 'final' value that may be set on the command line.
951  */
952 static int
953 ConfYamlOverrideFinalTest(void)
954 {
956  ConfInit();
957 
958  char config[] =
959  "%YAML 1.1\n"
960  "---\n"
961  "default-log-dir: /var/log\n";
962 
963  /* Set the log directory as if it was set on the command line. */
964  FAIL_IF_NOT(ConfSetFinal("default-log-dir", "/tmp"));
965  FAIL_IF(ConfYamlLoadString(config, strlen(config)) != 0);
966 
967  const char *default_log_dir;
968 
969  FAIL_IF_NOT(ConfGet("default-log-dir", &default_log_dir));
970  FAIL_IF(strcmp(default_log_dir, "/tmp") != 0);
971 
972  ConfDeInit();
974 
975  PASS;
976 }
977 
978 static int ConfYamlNull(void)
979 {
981  ConfInit();
982 
983  char config[] = "%YAML 1.1\n"
984  "---\n"
985  "quoted-tilde: \"~\"\n"
986  "unquoted-tilde: ~\n"
987  "quoted-null: \"null\"\n"
988  "unquoted-null: null\n"
989  "quoted-Null: \"Null\"\n"
990  "unquoted-Null: Null\n"
991  "quoted-NULL: \"NULL\"\n"
992  "unquoted-NULL: NULL\n"
993  "empty-quoted: \"\"\n"
994  "empty-unquoted: \n"
995  "list: [\"null\", null, \"Null\", Null, \"NULL\", NULL, \"~\", ~]\n";
996  FAIL_IF(ConfYamlLoadString(config, strlen(config)) != 0);
997 
998  const char *val;
999 
1000  FAIL_IF_NOT(ConfGet("quoted-tilde", &val));
1001  FAIL_IF_NULL(val);
1002  FAIL_IF_NOT(ConfGet("unquoted-tilde", &val));
1003  FAIL_IF_NOT_NULL(val);
1004 
1005  FAIL_IF_NOT(ConfGet("quoted-null", &val));
1006  FAIL_IF_NULL(val);
1007  FAIL_IF_NOT(ConfGet("unquoted-null", &val));
1008  FAIL_IF_NOT_NULL(val);
1009 
1010  FAIL_IF_NOT(ConfGet("quoted-Null", &val));
1011  FAIL_IF_NULL(val);
1012  FAIL_IF_NOT(ConfGet("unquoted-Null", &val));
1013  FAIL_IF_NOT_NULL(val);
1014 
1015  FAIL_IF_NOT(ConfGet("quoted-NULL", &val));
1016  FAIL_IF_NULL(val);
1017  FAIL_IF_NOT(ConfGet("unquoted-NULL", &val));
1018  FAIL_IF_NOT_NULL(val);
1019 
1020  FAIL_IF_NOT(ConfGet("empty-quoted", &val));
1021  FAIL_IF_NULL(val);
1022  FAIL_IF_NOT(ConfGet("empty-unquoted", &val));
1023  FAIL_IF_NOT_NULL(val);
1024 
1025  FAIL_IF_NOT(ConfGet("list.0", &val));
1026  FAIL_IF_NULL(val);
1027  FAIL_IF_NOT(ConfGet("list.1", &val));
1028  FAIL_IF_NOT_NULL(val);
1029 
1030  FAIL_IF_NOT(ConfGet("list.2", &val));
1031  FAIL_IF_NULL(val);
1032  FAIL_IF_NOT(ConfGet("list.3", &val));
1033  FAIL_IF_NOT_NULL(val);
1034 
1035  FAIL_IF_NOT(ConfGet("list.4", &val));
1036  FAIL_IF_NULL(val);
1037  FAIL_IF_NOT(ConfGet("list.5", &val));
1038  FAIL_IF_NOT_NULL(val);
1039 
1040  FAIL_IF_NOT(ConfGet("list.6", &val));
1041  FAIL_IF_NULL(val);
1042  FAIL_IF_NOT(ConfGet("list.7", &val));
1043  FAIL_IF_NOT_NULL(val);
1044 
1045  ConfDeInit();
1047 
1048  PASS;
1049 }
1050 
1051 #endif /* UNITTESTS */
1052 
1053 void
1055 {
1056 #ifdef UNITTESTS
1057  UtRegisterTest("ConfYamlSequenceTest", ConfYamlSequenceTest);
1058  UtRegisterTest("ConfYamlLoggingOutputTest", ConfYamlLoggingOutputTest);
1059  UtRegisterTest("ConfYamlNonYamlFileTest", ConfYamlNonYamlFileTest);
1060  UtRegisterTest("ConfYamlBadYamlVersionTest", ConfYamlBadYamlVersionTest);
1061  UtRegisterTest("ConfYamlSecondLevelSequenceTest",
1062  ConfYamlSecondLevelSequenceTest);
1063  UtRegisterTest("ConfYamlFileIncludeTest", ConfYamlFileIncludeTest);
1064  UtRegisterTest("ConfYamlOverrideTest", ConfYamlOverrideTest);
1065  UtRegisterTest("ConfYamlOverrideFinalTest", ConfYamlOverrideFinalTest);
1066  UtRegisterTest("ConfYamlNull", ConfYamlNull);
1067 #endif /* UNITTESTS */
1068 }
len
uint8_t len
Definition: app-layer-dnp3.h:2
FAIL_IF_NULL
#define FAIL_IF_NULL(expr)
Fail a test if expression evaluates to NULL.
Definition: util-unittest.h:89
ConfNode_::val
char * val
Definition: conf.h:34
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:103
YAML_VERSION_MAJOR
#define YAML_VERSION_MAJOR
Definition: conf-yaml-loader.c:34
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
ConfGetNode
ConfNode * ConfGetNode(const char *name)
Get a ConfNode by name.
Definition: conf.c:181
ConfNodeNew
ConfNode * ConfNodeNew(void)
Allocate a new configuration node.
Definition: conf.c:139
ConfYamlLoadFileWithPrefix
int ConfYamlLoadFileWithPrefix(const char *filename, const char *prefix)
Load configuration from a YAML file, insert in tree at 'prefix'.
Definition: conf-yaml-loader.c:551
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:248
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
ConfYamlHandleInclude
int ConfYamlHandleInclude(ConfNode *parent, const char *filename)
Include a file in the configuration.
Definition: conf-yaml-loader.c:114
ConfSetFinal
int ConfSetFinal(const char *name, const char *val)
Set a final configuration value.
Definition: conf.c:303
TAILQ_INSERT_TAIL
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:294
YAML_VERSION_MINOR
#define YAML_VERSION_MINOR
Definition: conf-yaml-loader.c:35
conf_state
conf_state
Definition: conf-yaml-loader.c:54
util-unittest.h
ConfNode_::is_seq
int is_seq
Definition: conf.h:36
FAIL_IF_NOT
#define FAIL_IF_NOT(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:82
strlcpy
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: util-strlcpyu.c:43
tag
uint32_t tag
Definition: decode-vntag.h:0
ConfGet
int ConfGet(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:335
ConfGetRootNode
ConfNode * ConfGetRootNode(void)
Get the root configuration node.
Definition: conf.c:207
TAILQ_REMOVE
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:312
FAIL_IF_NOT_NULL
#define FAIL_IF_NOT_NULL(expr)
Fail a test if expression evaluates to non-NULL.
Definition: util-unittest.h:96
util-debug.h
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
CONF_KEY
@ CONF_KEY
Definition: conf-yaml-loader.c:55
ConfNodePrune
void ConfNodePrune(ConfNode *node)
Create the path for an include entry.
Definition: conf.c:879
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
ConfYamlLoadFile
int ConfYamlLoadFile(const char *filename)
Load configuration from a YAML file.
Definition: conf-yaml-loader.c:473
ConfNode_::parent
struct ConfNode_ * parent
Definition: conf.h:41
RECURSION_LIMIT
#define RECURSION_LIMIT
Definition: conf-yaml-loader.c:39
ConfNode_::final
int final
Definition: conf.h:39
ConfYamlLoadString
int ConfYamlLoadString(const char *string, size_t len)
Load configuration from a YAML string.
Definition: conf-yaml-loader.c:520
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:249
conf-yaml-loader.h
conf.h
ConfCreateContextBackup
void ConfCreateContextBackup(void)
Creates a backup of the conf_hash hash_table used by the conf API.
Definition: conf.c:669
MANGLE_ERRORS_MAX
#define MANGLE_ERRORS_MAX
Definition: conf-yaml-loader.c:46
SCLogInfo
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:224
ConfNodeLookupChild
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
Definition: conf.c:781
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
ConfYamlRegisterTests
void ConfYamlRegisterTests(void)
Definition: conf-yaml-loader.c:1054
suricata-common.h
util-path.h
ConfNode_::name
char * name
Definition: conf.h:33
TAILQ_NEXT
#define TAILQ_NEXT(elm, field)
Definition: queue.h:307
PathIsAbsolute
int PathIsAbsolute(const char *path)
Check if a path is absolute.
Definition: util-path.c:44
ConfNodeIsSequence
int ConfNodeIsSequence(const ConfNode *node)
Check if a node is a sequence or node.
Definition: conf.c:911
ConfRestoreContextBackup
void ConfRestoreContextBackup(void)
Restores the backup of the hash_table present in backup_conf_hash back to conf_hash.
Definition: conf.c:679
SCStrdup
#define SCStrdup(s)
Definition: util-mem.h:56
FatalError
#define FatalError(...)
Definition: util-debug.h:502
ConfInit
void ConfInit(void)
Initialize the configuration system.
Definition: conf.c:120
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
SCFree
#define SCFree(p)
Definition: util-mem.h:61
ConfNode_
Definition: conf.h:32
ConfNodeGetNodeOrCreate
ConfNode * ConfNodeGetNodeOrCreate(ConfNode *parent, const char *name, int final)
Helper function to get a node, creating it if it does not exist.
Definition: conf.c:66
ConfDeInit
void ConfDeInit(void)
De-initializes the configuration system.
Definition: conf.c:688
ConfSet
int ConfSet(const char *name, const char *val)
Set a configuration value.
Definition: conf.c:224
CONF_VAL
@ CONF_VAL
Definition: conf-yaml-loader.c:56
DEFAULT_NAME_LEN
#define DEFAULT_NAME_LEN
Definition: conf-yaml-loader.c:44
CONF_INCLUDE
@ CONF_INCLUDE
Definition: conf-yaml-loader.c:57