Go to the documentation of this file.
52 #define MODULE_NAME "LuaLog"
73 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data);
84 static int LuaTxLogger(
ThreadVars *
tv,
void *thread_data,
const Packet *p,
Flow *f,
void *alstate,
void *txptr, uint64_t tx_id)
117 static int LuaStreamingLogger(
ThreadVars *
tv,
void *thread_data,
const Flow *f,
118 const uint8_t *data, uint32_t data_len, uint64_t tx_id, uint8_t
flags)
177 if (!(PacketIsIPv4(p)) && !(PacketIsIPv6(p))) {
239 if ((!(PacketIsIPv4(p))) && (!(PacketIsIPv6(p)))) {
278 void *tx,
const uint64_t tx_id, uint8_t dir)
283 if ((!(PacketIsIPv4(p))) && (!(PacketIsIPv6(p))))
358 lua_newtable(luastate);
360 for (; u < st->
nstats; u++) {
361 lua_pushinteger(luastate, u + 1);
363 lua_newtable(luastate);
365 lua_pushstring(luastate,
"name");
366 lua_pushstring(luastate, st->
stats[u].
name);
367 lua_settable(luastate, -3);
369 lua_pushstring(luastate,
"tmname");
371 lua_settable(luastate, -3);
373 lua_pushstring(luastate,
"value");
374 lua_pushinteger(luastate, st->
stats[u].
value);
375 lua_settable(luastate, -3);
377 lua_pushstring(luastate,
"pvalue");
379 lua_settable(luastate, -3);
381 lua_settable(luastate, -3);
417 if (luastate == NULL)
419 luaL_openlibs(luastate);
421 int status = luaL_loadfile(luastate, filename);
423 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
428 if (lua_pcall(luastate, 0, 0, 0) != 0) {
429 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
433 lua_getglobal(luastate,
"init");
434 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
439 lua_newtable(luastate);
440 if (lua_gettop(luastate) == 0 || lua_type(luastate, 2) != LUA_TTABLE) {
445 lua_pushliteral(luastate,
"script_api_ver");
446 lua_pushnumber (luastate, 1);
447 lua_settable(luastate, -3);
449 if (lua_pcall(luastate, 1, 1, 0) != 0) {
450 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
455 if (lua_gettop(luastate) == 0) {
456 SCLogError(
"init function in script should return table, nothing returned");
459 if (lua_type(luastate, 1) != LUA_TTABLE) {
460 SCLogError(
"init function in script should return table, returned is not table");
464 lua_pushnil(luastate);
466 while (lua_next(luastate, -2)) {
467 k = lua_tostring(luastate, -2);
471 v = lua_tostring(luastate, -1);
472 lua_pop(luastate, 1);
478 if (strcmp(k,
"protocol") == 0 && strcmp(v,
"http") == 0)
480 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"dns") == 0)
482 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"tls") == 0)
484 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"ssh") == 0)
486 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"smtp") == 0)
488 else if (strcmp(k,
"type") == 0 && strcmp(v,
"packet") == 0)
490 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"alerts") == 0)
492 else if (strcmp(k,
"type") == 0 && strcmp(v,
"file") == 0)
494 else if (strcmp(k,
"type") == 0 && strcmp(v,
"streaming") == 0)
496 else if (strcmp(k,
"type") == 0 && strcmp(v,
"flow") == 0)
498 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"tcp") == 0)
500 else if (strcmp(k,
"type") == 0 && strcmp(v,
"stats") == 0)
503 SCLogError(
"unknown key and/or value: k='%s', v='%s'", k, v);
509 SCLogError(
"invalid combination of 'needs' in the script");
513 lua_getglobal(luastate,
"setup");
514 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
519 lua_getglobal(luastate,
"log");
520 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
525 lua_getglobal(luastate,
"deinit");
526 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
545 static lua_State *LuaScriptSetup(
const char *filename)
548 if (luastate == NULL) {
553 luaL_openlibs(luastate);
555 int status = luaL_loadfile(luastate, filename);
557 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
562 if (lua_pcall(luastate, 0, 0, 0) != 0) {
563 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
567 lua_getglobal(luastate,
"setup");
581 if (lua_pcall(luastate, 0, 0, 0) != 0) {
582 SCLogError(
"couldn't run script 'setup' function: %s", lua_tostring(luastate, -1));
586 SCLogDebug(
"lua_State %p is set up", luastate);
594 static void LogLuaSubFree(
OutputCtx *oc) {
622 const char *dir =
"";
623 if (parent_ctx && parent_ctx->
data) {
628 char path[PATH_MAX] =
"";
629 int ret = snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", conf->
val);
630 if (ret < 0 || ret ==
sizeof(path)) {
631 SCLogError(
"failed to construct lua script path");
637 lua_ctx->
luastate = LuaScriptSetup(path);
644 output_ctx->
data = lua_ctx;
645 output_ctx->
DeInit = LogLuaSubFree;
647 result.
ctx = output_ctx;
657 static void LogLuaMasterFree(
OutputCtx *oc)
684 if (scripts == NULL) {
695 output_ctx->
DeInit = LogLuaMasterFree;
710 memset(&opts, 0x00,
sizeof(opts));
712 char path[PATH_MAX] =
"";
713 snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", script->
val);
716 int r = LuaScriptInit(path, &opts);
778 }
else if (opts.
file) {
784 }
else if (opts.
flow) {
786 }
else if (opts.
stats) {
797 result.
ctx = output_ctx;
803 output_ctx->
DeInit(output_ctx);
805 int failure_fatal = 0;
806 if (
ConfGetBool(
"engine.init-failure-fatal", &failure_fatal) != 1) {
807 SCLogDebug(
"ConfGetBool could not load the value.");
810 FatalError(
"Error during setup of lua output. Details should be "
811 "described in previous error messages. Shutting down...");
820 static void OutputLuaLogDoDeinit(
LogLuaCtx *lua_ctx)
824 lua_getglobal(luastate,
"deinit");
825 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
831 if (lua_pcall(luastate, 0, 0, 0) != 0) {
832 SCLogError(
"couldn't run script 'deinit' function: %s", lua_tostring(luastate, -1));
843 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
849 if (initdata == NULL) {
850 SCLogDebug(
"Error getting context for LuaLog. \"initdata\" argument NULL");
876 OutputLuaLogDoDeinit(td->
lua_ctx);
882 memset(td, 0,
sizeof(*td));
void LuaStateSetTX(lua_State *luastate, void *txptr, const uint64_t tx_id)
struct LogLuaThreadCtx_ LogLuaThreadCtx
void LuaStateSetPacket(lua_State *luastate, Packet *p)
structure containing global config The OutputLuaLogInitSub which is run per script can access this to...
void AppLayerHtpNeedFileInspection(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request file...
void LuaStateSetThreadVars(lua_State *luastate, ThreadVars *tv)
const struct Signature_ * s
#define PACKET_ALERT_FLAG_TX
void LuaPushTableKeyValueInt(lua_State *luastate, const char *key, int value)
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
struct HtpBodyChunk_ * next
lua_State * LuaGetState(void)
int LuaRegisterDnsFunctions(lua_State *luastate)
register http lua extensions in a luastate
#define TAILQ_FOREACH(var, head, field)
bool SSHTxLogCondition(ThreadVars *tv, const Packet *p, void *state, void *tx, uint64_t tx_id)
int LuaRegisterSshFunctions(lua_State *luastate)
register ssh lua extensions in a luastate
void LuaStateSetFlow(lua_State *luastate, Flow *f)
set a flow pointer in the lua state
#define TAILQ_INSERT_TAIL(head, elm, field)
struct LogLuaScriptOptions_ LogLuaScriptOptions
OutputInitSubFunc InitSubFunc
PacketLogger PacketLogFunc
#define OUTPUT_STREAMING_FLAG_TRANSACTION
size_t strlcpy(char *dst, const char *src, size_t siz)
struct lua_State lua_State
struct LogLuaMasterCtx_ LogLuaMasterCtx
structure containing global config The OutputLuaLogInitSub which is run per script can access this to...
enum SCOutputStreamingType stream_type
ThreadInitFunc ThreadInit
int LuaRegisterHttpFunctions(lua_State *luastate)
register http lua extensions in a luastate
int LuaRegisterHasshFunctions(lua_State *luastate)
Register Hassh Lua extensions.
ThreadDeinitFunc ThreadDeinit
#define SCMutexUnlock(mut)
void LuaStateSetFile(lua_State *luastate, File *file)
void AppLayerHtpEnableRequestBodyCallback(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request body...
void AppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
void OutputRegisterModule(const char *, const char *, OutputInitFunc)
Per thread variable structure.
SCStreamingLogger StreamingLogFunc
void AppLayerHtpEnableResponseBodyCallback(void)
Sets a flag that informs the HTP app layer that some module in the engine needs the http request body...
int LuaRegisterTlsFunctions(lua_State *luastate)
register tls lua extensions in a luastate
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
void * AppLayerParserGetTx(uint8_t ipproto, AppProto alproto, void *alstate, uint64_t tx_id)
#define SCMutexInit(mut, mutattrs)
int LuaRegisterJa3Functions(lua_State *luastate)
Register JA3 Lua extensions.
ConfNode * ConfNodeLookupChild(const ConfNode *node, const char *name)
Lookup a child configuration node by name.
TxLoggerCondition TxLogCondition
void(* DeInit)(struct OutputCtx_ *)
void LuaStateSetPacketAlert(lua_State *luastate, PacketAlert *pa)
void LuaLogRegister(void)
PacketLogCondition PacketConditionFunc
#define SCLogError(...)
Macro used to log ERROR messages.
void LuaStateSetStreamingBuffer(lua_State *luastate, LuaStreamingBuffer *b)
struct LogLuaCtx_ LogLuaCtx
int LuaRegisterSmtpFunctions(lua_State *luastate)
AppProto alproto
application level protocol
int LuaRegisterFunctions(lua_State *luastate)
void CreateTimeString(const SCTime_t ts, char *str, size_t size)
void LuaReturnState(lua_State *s)
const char * ConfNodeLookupChildValue(const ConfNode *node, const char *name)
Lookup the value of a child configuration node by name.