34 #define YAML_VERSION_MAJOR 1
35 #define YAML_VERSION_MINOR 1
39 #define RECURSION_LIMIT 128
44 #define DEFAULT_NAME_LEN 16
46 #define MANGLE_ERRORS_MAX 10
47 static int mangle_errors = 0;
49 static char *conf_dirname = NULL;
51 static int ConfYamlParse(yaml_parser_t *parser,
ConfNode *parent,
int inseq,
int rlevel,
int state);
72 while ((c = strchr(
string,
'_')))
82 ConfYamlSetConfDirname(
const char *filename)
86 ep = strrchr(filename,
'\\');
88 ep = strrchr(filename,
'/');
92 if (conf_dirname == NULL) {
93 FatalError(
"ERROR: Failed to allocate memory while loading configuration.");
98 if (conf_dirname == NULL) {
99 FatalError(
"ERROR: Failed to allocate memory while loading configuration.");
101 conf_dirname[ep - filename] =
'\0';
116 yaml_parser_t parser;
117 char include_filename[PATH_MAX];
121 if (yaml_parser_initialize(&parser) != 1) {
122 SCLogError(
"Failed to initialize YAML parser");
127 strlcpy(include_filename, filename,
sizeof(include_filename));
130 snprintf(include_filename,
sizeof(include_filename),
"%s/%s",
131 conf_dirname, filename);
134 file = fopen(include_filename,
"r");
136 SCLogError(
"Failed to open configuration include file %s: %s", include_filename,
141 yaml_parser_set_input_file(&parser, file);
143 if (ConfYamlParse(&parser, parent, 0, 0, 0) != 0) {
144 SCLogError(
"Failed to include configuration file %s", filename);
151 yaml_parser_delete(&parser);
167 static int ConfYamlParse(yaml_parser_t *parser,
ConfNode *parent,
int inseq,
int rlevel,
int state)
171 memset(&event, 0,
sizeof(event));
176 int include_count = 0;
179 SCLogError(
"Recursion limit reached while parsing "
180 "configuration file, aborting.");
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);
192 if (event.type == YAML_DOCUMENT_START_EVENT) {
193 SCLogDebug(
"event.type=YAML_DOCUMENT_START_EVENT; state=%d", state);
197 yaml_version_directive_t *ver =
198 event.data.document_start.version_directive;
200 SCLogError(
"ERROR: Invalid configuration file.");
201 SCLogError(
"The configuration file must begin with the following two lines: %%YAML "
205 int major = ver->major;
206 int minor = ver->minor;
208 SCLogError(
"ERROR: Invalid YAML version. Must be 1.1");
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);
221 if (state ==
CONF_KEY && strlen(value) == 0) {
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) {
237 SCLogInfo(
"Including configuration file %s.", value);
256 if (was_empty == 0) {
259 if (seq_node != NULL) {
283 seq_node->
val = NULL;
290 SCLogInfo(
"Including configuration file %s.", value);
298 if (strcmp(value,
"include") == 0) {
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",
310 if (parent->
val == NULL) {
312 if (parent->
val && strchr(parent->
val,
'_'))
317 if (strchr(value,
'.') != NULL) {
325 if (existing != NULL) {
326 if (!existing->
final) {
327 SCLogInfo(
"Configuration node '%s' redefined.", existing->
name);
338 if (node->
name && strchr(node->
name,
'_')) {
339 if (!(parent->
name &&
340 ((strcmp(parent->
name,
"address-groups") == 0) ||
341 (strcmp(parent->
name,
"port-groups") == 0)))) {
345 "%s is deprecated. Please use %s on line %" PRIuMAX
348 (uintmax_t)parser->mark.line + 1);
352 "parameter name warnings.");
362 if (value != NULL && (
tag != NULL) && (strcmp(
tag,
"!include") == 0)) {
363 SCLogInfo(
"Including configuration file %s at "
364 "parent node %s.", value, node->
name);
367 }
else if (!node->
final && value != NULL) {
368 if (node->
val != NULL)
376 else if (event.type == YAML_SEQUENCE_START_EVENT) {
377 SCLogDebug(
"event.type=YAML_SEQUENCE_START_EVENT; state=%d", state);
379 if (ConfYamlParse(parser, state ==
CONF_INCLUDE ? parent : node, 1, rlevel,
385 else if (event.type == YAML_SEQUENCE_END_EVENT) {
386 SCLogDebug(
"event.type=YAML_SEQUENCE_END_EVENT; state=%d", state);
389 else if (event.type == YAML_MAPPING_START_EVENT) {
390 SCLogDebug(
"event.type=YAML_MAPPING_START_EVENT; state=%d", state);
392 SCLogError(
"Include fields cannot be a mapping: line %zu", parser->mark.line);
408 if (was_empty == 0) {
411 if (seq_node != NULL) {
431 if (ConfYamlParse(parser, seq_node, 0, rlevel, 0) != 0)
435 if (ConfYamlParse(parser, node, inseq, rlevel, 0) != 0)
440 else if (event.type == YAML_MAPPING_END_EVENT) {
441 SCLogDebug(
"event.type=YAML_MAPPING_END_EVENT; state=%d", state);
444 else if (event.type == YAML_STREAM_END_EVENT) {
445 SCLogDebug(
"event.type=YAML_STREAM_END_EVENT; state=%d", state);
450 yaml_event_delete(&event);
454 yaml_event_delete(&event);
479 yaml_parser_t parser;
483 if (yaml_parser_initialize(&parser) != 1) {
484 SCLogError(
"failed to initialize yaml parser.");
488 struct stat stat_buf;
489 if (stat(filename, &stat_buf) == 0) {
490 if (stat_buf.st_mode & S_IFDIR) {
491 SCLogError(
"yaml argument is not a file but a directory: %s. "
492 "Please specify the yaml file in your -c option.",
494 yaml_parser_delete(&parser);
500 infile = fopen(filename,
"r");
501 if (infile == NULL) {
502 SCLogError(
"failed to open file: %s: %s", filename, strerror(errno));
503 yaml_parser_delete(&parser);
507 if (conf_dirname == NULL) {
508 ConfYamlSetConfDirname(filename);
511 yaml_parser_set_input_file(&parser, infile);
512 ret = ConfYamlParse(&parser, root, 0, 0, 0);
513 yaml_parser_delete(&parser);
526 yaml_parser_t parser;
529 if (yaml_parser_initialize(&parser) != 1) {
530 fprintf(stderr,
"Failed to initialize yaml parser.\n");
533 yaml_parser_set_input_string(&parser, (
const unsigned char *)
string,
len);
534 ret = ConfYamlParse(&parser, root, 0, 0, 0);
535 yaml_parser_delete(&parser);
557 yaml_parser_t parser;
561 struct stat stat_buf;
563 if (stat(filename, &stat_buf) == 0) {
564 if (stat_buf.st_mode & S_IFDIR) {
565 SCLogError(
"yaml argument is not a file but a directory: %s. "
566 "Please specify the yaml file in your -c option.",
572 if (yaml_parser_initialize(&parser) != 1) {
573 SCLogError(
"failed to initialize yaml parser.");
578 infile = fopen(filename,
"r");
579 if (infile == NULL) {
580 SCLogError(
"failed to open file: %s: %s", filename, strerror(errno));
581 yaml_parser_delete(&parser);
585 if (conf_dirname == NULL) {
586 ConfYamlSetConfDirname(filename);
591 ConfSet(prefix,
"<prefix root node>");
595 yaml_parser_delete(&parser);
599 yaml_parser_set_input_file(&parser, infile);
600 ret = ConfYamlParse(&parser, root, 0, 0, 0);
601 yaml_parser_delete(&parser);
610 ConfYamlSequenceTest(
void)
619 default-log-dir: /tmp\n\
636 FAIL_IF(strcmp(filename->
val,
"netbios.rules") != 0);
641 FAIL_IF(strcmp(filename->
val,
"x11.rules") != 0);
654 ConfYamlLoggingOutputTest(
void)
661 - interface: console\n\
663 - interface: syslog\n\
686 FAIL_IF(strcmp(output_param->
name,
"interface") != 0);
687 FAIL_IF(strcmp(output_param->
val,
"console") != 0);
690 FAIL_IF(strcmp(output_param->
name,
"log-level") != 0);
691 FAIL_IF(strcmp(output_param->
val,
"error") != 0);
699 FAIL_IF(strcmp(output_param->
name,
"interface") != 0);
700 FAIL_IF(strcmp(output_param->
val,
"syslog") != 0);
703 FAIL_IF(strcmp(output_param->
name,
"facility") != 0);
704 FAIL_IF(strcmp(output_param->
val,
"local4") != 0);
707 FAIL_IF(strcmp(output_param->
name,
"log-level") != 0);
708 FAIL_IF(strcmp(output_param->
val,
"info") != 0);
720 ConfYamlNonYamlFileTest(
void)
734 ConfYamlBadYamlVersionTest(
void)
741 - interface: console\n\
743 - interface: syslog\n\
760 ConfYamlSecondLevelSequenceTest(
void)
768 address: [\"192.168.1.0/24\"]\n\
769 personality: [\"Apache_2_2\", \"PHP_5_3\"]\n\
770 path-parsing: [\"compress_separators\", \"lowercase\"]\n\
780 - compress_separators\n\
808 FAIL_IF(strcmp(node->
val,
"192.168.1.0/24") != 0);
820 ConfYamlFileIncludeTest(
void)
824 const char config_filename[] =
"ConfYamlFileIncludeTest-config.yaml";
825 const char config_file_contents[] =
828 "# Include something at the root level.\n"
829 "include: ConfYamlFileIncludeTest-include.yaml\n"
830 "# Test including under a mapping.\n"
831 "mapping: !include ConfYamlFileIncludeTest-include.yaml\n";
833 const char include_filename[] =
"ConfYamlFileIncludeTest-include.yaml";
834 const char include_file_contents[] =
845 FAIL_IF_NULL((config_file = fopen(config_filename,
"w")));
846 FAIL_IF(fwrite(config_file_contents, strlen(config_file_contents), 1, config_file) != 1);
849 FAIL_IF_NULL((config_file = fopen(include_filename,
"w")));
850 FAIL_IF(fwrite(include_file_contents, strlen(include_file_contents), 1, config_file) != 1);
854 if (conf_dirname != NULL) {
877 node =
ConfGetNode(
"mapping.unix-command.enabled");
884 unlink(config_filename);
885 unlink(include_filename);
895 ConfYamlOverrideTest(
void)
897 char config[] =
"%YAML 1.1\n"
899 "some-log-dir: /var/log\n"
900 "some-log-dir: /tmp\n"
910 " HOME_NET: \"[192.168.0.0/16,10.0.0.0/8,172.16.0.0/12]\"\n"
911 " EXTERNAL_NET: any\n"
912 "vars.address-groups.HOME_NET: \"10.10.10.10/32\"\n";
920 FAIL_IF(strcmp(value,
"/tmp") != 0);
925 FAIL_IF(strcmp(value,
"value") != 0);
933 ConfNode *vars_address_groups_external_net =
ConfGetNode(
"vars.address-groups.EXTERNAL_NET");
940 FAIL_IF(strcmp(vars_address_groups_home_net->
val,
"10.10.10.10/32") != 0);
956 ConfYamlOverrideFinalTest(
void)
964 "default-log-dir: /var/log\n";
970 const char *default_log_dir;
973 FAIL_IF(strcmp(default_log_dir,
"/tmp") != 0);
981 static int ConfYamlNull(
void)
986 char config[] =
"%YAML 1.1\n"
988 "quoted-tilde: \"~\"\n"
989 "unquoted-tilde: ~\n"
990 "quoted-null: \"null\"\n"
991 "unquoted-null: null\n"
992 "quoted-Null: \"Null\"\n"
993 "unquoted-Null: Null\n"
994 "quoted-NULL: \"NULL\"\n"
995 "unquoted-NULL: NULL\n"
996 "empty-quoted: \"\"\n"
998 "list: [\"null\", null, \"Null\", Null, \"NULL\", NULL, \"~\", ~]\n";
1061 UtRegisterTest(
"ConfYamlLoggingOutputTest", ConfYamlLoggingOutputTest);
1062 UtRegisterTest(
"ConfYamlNonYamlFileTest", ConfYamlNonYamlFileTest);
1063 UtRegisterTest(
"ConfYamlBadYamlVersionTest", ConfYamlBadYamlVersionTest);
1065 ConfYamlSecondLevelSequenceTest);
1066 UtRegisterTest(
"ConfYamlFileIncludeTest", ConfYamlFileIncludeTest);
1068 UtRegisterTest(
"ConfYamlOverrideFinalTest", ConfYamlOverrideFinalTest);