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