|
|
|
|
@ -30,13 +30,14 @@ TmEcode AlertUnifiedAlert (ThreadVars *, Packet *, void *, PacketQueue *);
|
|
|
|
|
TmEcode AlertUnifiedAlertThreadInit(ThreadVars *, void *, void **);
|
|
|
|
|
TmEcode AlertUnifiedAlertThreadDeinit(ThreadVars *, void *);
|
|
|
|
|
int AlertUnifiedAlertOpenFileCtx(LogFileCtx *, char *);
|
|
|
|
|
void AlertUnifiedAlertRegisterTests (void);
|
|
|
|
|
|
|
|
|
|
void TmModuleAlertUnifiedAlertRegister (void) {
|
|
|
|
|
tmm_modules[TMM_ALERTUNIFIEDALERT].name = "AlertUnifiedAlert";
|
|
|
|
|
tmm_modules[TMM_ALERTUNIFIEDALERT].ThreadInit = AlertUnifiedAlertThreadInit;
|
|
|
|
|
tmm_modules[TMM_ALERTUNIFIEDALERT].Func = AlertUnifiedAlert;
|
|
|
|
|
tmm_modules[TMM_ALERTUNIFIEDALERT].ThreadDeinit = AlertUnifiedAlertThreadDeinit;
|
|
|
|
|
tmm_modules[TMM_ALERTUNIFIEDALERT].RegisterTests = NULL;
|
|
|
|
|
tmm_modules[TMM_ALERTUNIFIEDALERT].RegisterTests = AlertUnifiedAlertRegisterTests;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef struct AlertUnifiedAlertThread_ {
|
|
|
|
|
@ -102,8 +103,13 @@ int AlertUnifiedAlertWriteFileHeader(ThreadVars *t, AlertUnifiedAlertThread *aun
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AlertUnifiedAlertCloseFile(ThreadVars *t, AlertUnifiedAlertThread *aun) {
|
|
|
|
|
if (aun->file_ctx->fp != NULL)
|
|
|
|
|
if (aun->file_ctx->fp != NULL) {
|
|
|
|
|
fclose(aun->file_ctx->fp);
|
|
|
|
|
if (aun->file_ctx->filename != NULL) {
|
|
|
|
|
free(aun->file_ctx->filename);
|
|
|
|
|
aun->file_ctx->filename = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
@ -203,6 +209,7 @@ TmEcode AlertUnifiedAlertThreadInit(ThreadVars *t, void *initdata, void **data)
|
|
|
|
|
}
|
|
|
|
|
/** Use the Ouptut Context (file pointer and mutex) */
|
|
|
|
|
aun->file_ctx = (LogFileCtx*) initdata;
|
|
|
|
|
aun->size_limit = 30;
|
|
|
|
|
|
|
|
|
|
/** Write Unified header */
|
|
|
|
|
int ret = AlertUnifiedAlertWriteFileHeader(t, aun);
|
|
|
|
|
@ -246,24 +253,23 @@ error:
|
|
|
|
|
* */
|
|
|
|
|
LogFileCtx *AlertUnifiedAlertInitCtx(char *config_file)
|
|
|
|
|
{
|
|
|
|
|
int ret=0;
|
|
|
|
|
LogFileCtx* file_ctx=LogFileNewCtx();
|
|
|
|
|
int ret = 0;
|
|
|
|
|
LogFileCtx *file_ctx = LogFileNewCtx();
|
|
|
|
|
|
|
|
|
|
if(file_ctx == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (file_ctx == NULL) {
|
|
|
|
|
printf("AlertUnifiedAlertInitCtx: Couldn't create new file_ctx\n");
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** fill the new LogFileCtx with the specific AlertUnifiedAlert configuration */
|
|
|
|
|
ret=AlertUnifiedAlertOpenFileCtx(file_ctx, config_file);
|
|
|
|
|
ret = AlertUnifiedAlertOpenFileCtx(file_ctx, config_file);
|
|
|
|
|
|
|
|
|
|
if(ret < 0)
|
|
|
|
|
if (ret < 0)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
/** In AlertUnifiedAlertOpenFileCtx the second parameter should be the configuration file to use
|
|
|
|
|
* but it's not implemented yet, so passing NULL to load the default
|
|
|
|
|
* configuration
|
|
|
|
|
/** In AlertUnifiedAlertOpenFileCtx the second parameter should be
|
|
|
|
|
* the configuration file to use but it's not implemented yet, so
|
|
|
|
|
* passing NULL to load the default configuration
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
return file_ctx;
|
|
|
|
|
@ -276,10 +282,9 @@ LogFileCtx *AlertUnifiedAlertInitCtx(char *config_file)
|
|
|
|
|
* */
|
|
|
|
|
int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, char *config_file)
|
|
|
|
|
{
|
|
|
|
|
char filename[PATH_MAX]; /* XXX some sane default? */
|
|
|
|
|
char *filename = malloc(PATH_MAX); /* XXX some sane default? */
|
|
|
|
|
|
|
|
|
|
if(config_file == NULL)
|
|
|
|
|
{
|
|
|
|
|
if (config_file == NULL) {
|
|
|
|
|
/** Separate config files not implemented at the moment,
|
|
|
|
|
* but it must be able to load from separate config file.
|
|
|
|
|
* Load the default configuration.
|
|
|
|
|
@ -297,7 +302,8 @@ int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, char *config_file)
|
|
|
|
|
char *log_dir;
|
|
|
|
|
if (ConfGet("default-log-dir", &log_dir) != 1)
|
|
|
|
|
log_dir = DEFAULT_LOG_DIR;
|
|
|
|
|
snprintf(filename, sizeof(filename), "%s/%s.%" PRIu32, log_dir, "unified.alert", (uint32_t)ts.tv_sec);
|
|
|
|
|
|
|
|
|
|
snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32, log_dir, "unified.alert", (uint32_t)ts.tv_sec);
|
|
|
|
|
|
|
|
|
|
/* XXX filename & location */
|
|
|
|
|
file_ctx->fp = fopen(filename, "wb");
|
|
|
|
|
@ -305,14 +311,65 @@ int AlertUnifiedAlertOpenFileCtx(LogFileCtx *file_ctx, char *config_file)
|
|
|
|
|
printf("Error: fopen %s failed: %s\n", filename, strerror(errno)); /* XXX errno threadsafety? */
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(file_ctx->config_file == NULL)
|
|
|
|
|
file_ctx->config_file = strdup("configfile.aua");
|
|
|
|
|
/** Remember the config file (or NULL if not indicated) */
|
|
|
|
|
|
|
|
|
|
file_ctx->filename = filename;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef UNITTESTS
|
|
|
|
|
/**
|
|
|
|
|
* \test Test the Rotate process
|
|
|
|
|
*
|
|
|
|
|
* \retval 1 on succces
|
|
|
|
|
* \retval 0 on failure
|
|
|
|
|
*/
|
|
|
|
|
static int AlertUnifiedAlertTestRotate01(void)
|
|
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
int r = 0;
|
|
|
|
|
ThreadVars tv;
|
|
|
|
|
LogFileCtx *lf;
|
|
|
|
|
void *data = NULL;
|
|
|
|
|
|
|
|
|
|
lf = AlertUnifiedAlertInitCtx(NULL);
|
|
|
|
|
char *filename = strdup(lf->filename);
|
|
|
|
|
|
|
|
|
|
memset(&tv, 0, sizeof(ThreadVars));
|
|
|
|
|
|
|
|
|
|
if (lf == NULL)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
ret = AlertUnifiedAlertThreadInit(&tv, lf, &data);
|
|
|
|
|
if (ret == TM_ECODE_FAILED) {
|
|
|
|
|
LogFileFreeCtx(lf);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sleep(1);
|
|
|
|
|
ret = AlertUnifiedAlertRotateFile(&tv, data);
|
|
|
|
|
if (ret == -1)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
if (strcmp(filename, lf->filename) == 0)
|
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
|
|
r = 1;
|
|
|
|
|
|
|
|
|
|
error:
|
|
|
|
|
AlertUnifiedAlertThreadDeinit(&tv, data);
|
|
|
|
|
if (lf != NULL) LogFileFreeCtx(lf);
|
|
|
|
|
if (filename != NULL) free(filename);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
#endif /* UNITTESTS */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* \brief this function registers unit tests for Unified2
|
|
|
|
|
*/
|
|
|
|
|
void AlertUnifiedAlertRegisterTests (void) {
|
|
|
|
|
#ifdef UNITTESTS
|
|
|
|
|
UtRegisterTest("UnifiedAlertTestRotate01 -- Rotate File",
|
|
|
|
|
AlertUnifiedAlertTestRotate01, 1);
|
|
|
|
|
#endif /* UNITTESTS */
|
|
|
|
|
}
|
|
|
|
|
|