config/ref: Raise errors for ref.config parsing

This commit raises an error in configuration test mode if there was an
error parsing reference.config.

Issue: 4659
pull/6894/head
Jeff Lucovsky 5 years ago committed by Victor Julien
parent 8d615842f9
commit be2155b4ed

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2021 Open Information Security Foundation /* Copyright (C) 2007-2022 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -2364,7 +2364,10 @@ static DetectEngineCtx *DetectEngineCtxInitReal(enum DetectEngineType type, cons
(void)SRepInit(de_ctx); (void)SRepInit(de_ctx);
SCClassConfLoadClassficationConfigFile(de_ctx, NULL); SCClassConfLoadClassficationConfigFile(de_ctx, NULL);
SCRConfLoadReferenceConfigFile(de_ctx, NULL); if (SCRConfLoadReferenceConfigFile(de_ctx, NULL) < 0) {
if (RunmodeGetCurrent() == RUNMODE_CONF_TEST)
goto error;
}
if (ActionInitConfig() < 0) { if (ActionInitConfig() < 0) {
goto error; goto error;

@ -1,4 +1,4 @@
/* Copyright (C) 2007-2019 Open Information Security Foundation /* Copyright (C) 2007-2022 Open Information Security Foundation
* *
* You can copy, redistribute or modify this Program under the terms of * You can copy, redistribute or modify this Program under the terms of
* the GNU General Public License version 2 as published by the Free * the GNU General Public License version 2 as published by the Free
@ -122,8 +122,9 @@ static FILE *SCRConfInitContextAndLocalResources(DetectEngineCtx *de_ctx, FILE *
const char *filename = SCRConfGetConfFilename(de_ctx); const char *filename = SCRConfGetConfFilename(de_ctx);
if ((fd = fopen(filename, "r")) == NULL) { if ((fd = fopen(filename, "r")) == NULL) {
#ifdef UNITTESTS #ifdef UNITTESTS
if (RunmodeIsUnittests()) if (RunmodeIsUnittests()) {
return NULL; // silently fail return NULL; // silently fail
}
#endif #endif
SCLogError(SC_ERR_FOPEN, "Error opening file: \"%s\": %s", filename, SCLogError(SC_ERR_FOPEN, "Error opening file: \"%s\": %s", filename,
strerror(errno)); strerror(errno));
@ -319,16 +320,22 @@ static int SCRConfIsLineBlankOrComment(char *line)
* *
* \param de_ctx Pointer to the Detection Engine Context. * \param de_ctx Pointer to the Detection Engine Context.
*/ */
static void SCRConfParseFile(DetectEngineCtx *de_ctx, FILE *fd) static bool SCRConfParseFile(DetectEngineCtx *de_ctx, FILE *fd)
{ {
char line[1024]; char line[1024];
uint8_t i = 1; uint8_t i = 1;
int runmode = RunmodeGetCurrent();
bool is_conf_test_mode = runmode == RUNMODE_CONF_TEST;
while (fgets(line, sizeof(line), fd) != NULL) { while (fgets(line, sizeof(line), fd) != NULL) {
if (SCRConfIsLineBlankOrComment(line)) if (SCRConfIsLineBlankOrComment(line))
continue; continue;
SCRConfAddReference(de_ctx, line); if (SCRConfAddReference(de_ctx, line) != 0) {
if (is_conf_test_mode) {
return false;
}
}
i++; i++;
} }
@ -336,7 +343,7 @@ static void SCRConfParseFile(DetectEngineCtx *de_ctx, FILE *fd)
SCLogInfo("Added \"%d\" reference types from the reference.config file", SCLogInfo("Added \"%d\" reference types from the reference.config file",
de_ctx->reference_conf_ht->count); de_ctx->reference_conf_ht->count);
#endif /* UNITTESTS */ #endif /* UNITTESTS */
return; return true;
} }
/** /**
@ -492,7 +499,7 @@ int SCRConfLoadReferenceConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
fd = SCRConfInitContextAndLocalResources(de_ctx, fd); fd = SCRConfInitContextAndLocalResources(de_ctx, fd);
if (fd == NULL) { if (fd == NULL) {
#ifdef UNITTESTS #ifdef UNITTESTS
if (RunmodeIsUnittests() && fd == NULL) { if (RunmodeIsUnittests()) {
return -1; return -1;
} }
#endif #endif
@ -501,10 +508,10 @@ int SCRConfLoadReferenceConfigFile(DetectEngineCtx *de_ctx, FILE *fd)
return -1; return -1;
} }
SCRConfParseFile(de_ctx, fd); bool rc = SCRConfParseFile(de_ctx, fd);
SCRConfDeInitLocalResources(de_ctx, fd); SCRConfDeInitLocalResources(de_ctx, fd);
return 0; return rc ? 0 : -1;
} }
/** /**

Loading…
Cancel
Save