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