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);
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)))) {
342 "%s is deprecated. Please use %s on line %" PRIuMAX
345 (uintmax_t)parser->mark.line + 1);
349 "parameter name warnings.");
359 if (value != NULL && (
tag != NULL) && (strcmp(
tag,
"!include") == 0)) {
360 SCLogInfo(
"Including configuration file %s at "
361 "parent node %s.", value, node->
name);
364 }
else if (!node->
final && value != NULL) {
365 if (node->
val != NULL)
373 else if (event.type == YAML_SEQUENCE_START_EVENT) {
374 SCLogDebug(
"event.type=YAML_SEQUENCE_START_EVENT; state=%d", state);
376 if (ConfYamlParse(parser, state ==
CONF_INCLUDE ? parent : node, 1, rlevel,
382 else if (event.type == YAML_SEQUENCE_END_EVENT) {
383 SCLogDebug(
"event.type=YAML_SEQUENCE_END_EVENT; state=%d", state);
386 else if (event.type == YAML_MAPPING_START_EVENT) {
387 SCLogDebug(
"event.type=YAML_MAPPING_START_EVENT; state=%d", state);
389 SCLogError(
"Include fields cannot be a mapping: line %zu", parser->mark.line);
405 if (was_empty == 0) {
408 if (seq_node != NULL) {
428 if (ConfYamlParse(parser, seq_node, 0, rlevel, 0) != 0)
432 if (ConfYamlParse(parser, node, inseq, rlevel, 0) != 0)
437 else if (event.type == YAML_MAPPING_END_EVENT) {
438 SCLogDebug(
"event.type=YAML_MAPPING_END_EVENT; state=%d", state);
441 else if (event.type == YAML_STREAM_END_EVENT) {
442 SCLogDebug(
"event.type=YAML_STREAM_END_EVENT; state=%d", state);
447 yaml_event_delete(&event);
451 yaml_event_delete(&event);
476 yaml_parser_t parser;
480 if (yaml_parser_initialize(&parser) != 1) {
481 SCLogError(
"failed to initialize yaml parser.");
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.",
491 yaml_parser_delete(&parser);
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);
504 if (conf_dirname == NULL) {
505 ConfYamlSetConfDirname(filename);
508 yaml_parser_set_input_file(&parser, infile);
509 ret = ConfYamlParse(&parser, root, 0, 0, 0);
510 yaml_parser_delete(&parser);
523 yaml_parser_t parser;
526 if (yaml_parser_initialize(&parser) != 1) {
527 fprintf(stderr,
"Failed to initialize yaml parser.\n");
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);
554 yaml_parser_t parser;
558 if (yaml_parser_initialize(&parser) != 1) {
559 SCLogError(
"failed to initialize yaml parser.");
563 struct stat stat_buf;
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.",
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);
582 if (conf_dirname == NULL) {
583 ConfYamlSetConfDirname(filename);
588 ConfSet(prefix,
"<prefix root node>");
592 yaml_parser_delete(&parser);
596 yaml_parser_set_input_file(&parser, infile);
597 ret = ConfYamlParse(&parser, root, 0, 0, 0);
598 yaml_parser_delete(&parser);
607 ConfYamlSequenceTest(
void)
616 default-log-dir: /tmp\n\
633 FAIL_IF(strcmp(filename->
val,
"netbios.rules") != 0);
638 FAIL_IF(strcmp(filename->
val,
"x11.rules") != 0);
651 ConfYamlLoggingOutputTest(
void)
658 - interface: console\n\
660 - interface: syslog\n\
683 FAIL_IF(strcmp(output_param->
name,
"interface") != 0);
684 FAIL_IF(strcmp(output_param->
val,
"console") != 0);
687 FAIL_IF(strcmp(output_param->
name,
"log-level") != 0);
688 FAIL_IF(strcmp(output_param->
val,
"error") != 0);
696 FAIL_IF(strcmp(output_param->
name,
"interface") != 0);
697 FAIL_IF(strcmp(output_param->
val,
"syslog") != 0);
700 FAIL_IF(strcmp(output_param->
name,
"facility") != 0);
701 FAIL_IF(strcmp(output_param->
val,
"local4") != 0);
704 FAIL_IF(strcmp(output_param->
name,
"log-level") != 0);
705 FAIL_IF(strcmp(output_param->
val,
"info") != 0);
717 ConfYamlNonYamlFileTest(
void)
731 ConfYamlBadYamlVersionTest(
void)
738 - interface: console\n\
740 - interface: syslog\n\
757 ConfYamlSecondLevelSequenceTest(
void)
765 address: [\"192.168.1.0/24\"]\n\
766 personality: [\"Apache_2_2\", \"PHP_5_3\"]\n\
767 path-parsing: [\"compress_separators\", \"lowercase\"]\n\
777 - compress_separators\n\
805 FAIL_IF(strcmp(node->
val,
"192.168.1.0/24") != 0);
817 ConfYamlFileIncludeTest(
void)
821 const char config_filename[] =
"ConfYamlFileIncludeTest-config.yaml";
822 const char config_file_contents[] =
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";
830 const char include_filename[] =
"ConfYamlFileIncludeTest-include.yaml";
831 const char include_file_contents[] =
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);
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);
851 if (conf_dirname != NULL) {
874 node =
ConfGetNode(
"mapping.unix-command.enabled");
881 unlink(config_filename);
882 unlink(include_filename);
892 ConfYamlOverrideTest(
void)
894 char config[] =
"%YAML 1.1\n"
896 "some-log-dir: /var/log\n"
897 "some-log-dir: /tmp\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";
917 FAIL_IF(strcmp(value,
"/tmp") != 0);
922 FAIL_IF(strcmp(value,
"value") != 0);
930 ConfNode *vars_address_groups_external_net =
ConfGetNode(
"vars.address-groups.EXTERNAL_NET");
937 FAIL_IF(strcmp(vars_address_groups_home_net->
val,
"10.10.10.10/32") != 0);
953 ConfYamlOverrideFinalTest(
void)
961 "default-log-dir: /var/log\n";
967 const char *default_log_dir;
970 FAIL_IF(strcmp(default_log_dir,
"/tmp") != 0);
978 static int ConfYamlNull(
void)
983 char config[] =
"%YAML 1.1\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"
995 "list: [\"null\", null, \"Null\", Null, \"NULL\", NULL, \"~\", ~]\n";
1058 UtRegisterTest(
"ConfYamlLoggingOutputTest", ConfYamlLoggingOutputTest);
1059 UtRegisterTest(
"ConfYamlNonYamlFileTest", ConfYamlNonYamlFileTest);
1060 UtRegisterTest(
"ConfYamlBadYamlVersionTest", ConfYamlBadYamlVersionTest);
1062 ConfYamlSecondLevelSequenceTest);
1063 UtRegisterTest(
"ConfYamlFileIncludeTest", ConfYamlFileIncludeTest);
1065 UtRegisterTest(
"ConfYamlOverrideFinalTest", ConfYamlOverrideFinalTest);