Go to the documentation of this file.
41 #define MODULE_NAME "LuaLog"
73 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data);
84 static bool LuaTlsTxLogCondition(
92 void *txptr, uint64_t tx_id)
132 static int LuaStreamingLogger(
ThreadVars *
tv,
void *thread_data,
const Flow *
f,
const uint8_t *data,
133 uint32_t data_len, uint64_t tx_id, uint8_t
flags)
162 lua_getglobal(luastate,
"log");
163 lua_newtable(luastate);
169 lua_pushstring(luastate,
"stream");
170 lua_newtable(luastate);
179 lua_settable(luastate, -3);
181 int retval = lua_pcall(luastate, 1, 0, 0);
211 if (!(PacketIsIPv4(
p)) && !(PacketIsIPv6(
p))) {
280 if ((!(PacketIsIPv4(
p))) && (!(PacketIsIPv6(
p)))) {
326 void *tx,
const uint64_t tx_id, uint8_t dir)
331 if ((!(PacketIsIPv4(
p))) && (!(PacketIsIPv6(
p))))
426 lua_newtable(luastate);
428 for (; u < st->
nstats; u++) {
429 lua_pushinteger(luastate, u + 1);
431 lua_newtable(luastate);
433 lua_pushstring(luastate,
"name");
434 lua_pushstring(luastate, st->
stats[u].
name);
435 lua_settable(luastate, -3);
437 lua_pushstring(luastate,
"tmname");
439 lua_settable(luastate, -3);
441 lua_pushstring(luastate,
"value");
442 lua_pushinteger(luastate, st->
stats[u].
value);
443 lua_settable(luastate, -3);
445 lua_pushstring(luastate,
"pvalue");
447 lua_settable(luastate, -3);
449 lua_settable(luastate, -3);
481 lua_getglobal(L,
"package");
483 if (strlen(
ctx->path) > 0) {
484 lua_pushstring(L,
ctx->path);
486 lua_pushstring(L,
"");
488 lua_setfield(L, -2,
"path");
490 if (strlen(
ctx->cpath) > 0) {
491 lua_pushstring(L,
ctx->cpath);
493 lua_pushstring(L,
"");
495 lua_setfield(L, -2,
"cpath");
514 if (luastate == NULL)
516 luaL_openlibs(luastate);
518 LuaSetPaths(luastate,
ctx);
520 int status = luaL_loadfile(luastate, filename);
522 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
527 if (lua_pcall(luastate, 0, 0, 0) != 0) {
528 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
532 lua_getglobal(luastate,
"init");
533 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
538 if (lua_pcall(luastate, 0, 1, 0) != 0) {
539 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
544 if (lua_gettop(luastate) == 0) {
545 SCLogError(
"init function in script should return table, nothing returned");
548 if (lua_type(luastate, 1) != LUA_TTABLE) {
549 SCLogError(
"init function in script should return table, returned is not table");
553 lua_pushnil(luastate);
555 while (lua_next(luastate, -2)) {
559 if (lua_type(luastate, -2) != LUA_TSTRING) {
560 lua_pop(luastate, 1);
563 k = lua_tostring(luastate, -2);
565 v = lua_tostring(luastate, -1);
566 lua_pop(luastate, 1);
572 if (strcmp(k,
"streaming") == 0) {
574 if (strcmp(v,
"http") == 0) {
576 }
else if (strcmp(v,
"tcp") == 0) {
579 SCLogError(
"unsupported streaming argument: %s", v);
582 }
else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"http") == 0)
584 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"dns") == 0)
586 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"tls") == 0)
588 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"ssh") == 0)
590 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"smtp") == 0)
592 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"ntp") == 0)
594 else if (strcmp(k,
"type") == 0 && strcmp(v,
"packet") == 0)
596 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"alerts") == 0)
598 else if (strcmp(k,
"type") == 0 && strcmp(v,
"file") == 0)
600 else if (strcmp(k,
"type") == 0 && strcmp(v,
"streaming") == 0)
602 else if (strcmp(k,
"type") == 0 && strcmp(v,
"flow") == 0)
604 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"tcp") == 0)
606 else if (strcmp(k,
"type") == 0 && strcmp(v,
"stats") == 0)
609 SCLogError(
"unknown key and/or value: k='%s', v='%s'", k, v);
615 SCLogError(
"invalid combination of 'needs' in the script");
619 lua_getglobal(luastate,
"setup");
620 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
625 lua_getglobal(luastate,
"log");
626 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
631 lua_getglobal(luastate,
"deinit");
632 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
654 if (luastate == NULL) {
659 luaL_openlibs(luastate);
661 LuaSetPaths(luastate,
ctx);
663 int status = luaL_loadfile(luastate, filename);
665 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
670 if (lua_pcall(luastate, 0, 0, 0) != 0) {
671 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
675 lua_getglobal(luastate,
"setup");
677 if (lua_pcall(luastate, 0, 0, 0) != 0) {
678 SCLogError(
"couldn't run script 'setup' function: %s", lua_tostring(luastate, -1));
682 SCLogDebug(
"lua_State %p is set up", luastate);
719 BUG_ON(parent_ctx == NULL);
724 char path[PATH_MAX] =
"";
725 if (strlen(dir) > 0) {
726 if (
PathMerge(path,
sizeof(path), dir, conf->
val) < 0) {
727 SCLogError(
"failed to construct lua script path");
736 lua_ctx->
luastate = LuaScriptSetup(path, mc);
746 output_ctx->
data = lua_ctx;
747 output_ctx->
DeInit = LogLuaSubFree;
749 result.
ctx = output_ctx;
759 static void LogLuaMasterFree(
OutputCtx *oc)
786 if (scripts == NULL) {
797 output_ctx->
DeInit = LogLuaMasterFree;
807 if (lua_path && strlen(lua_path) > 0) {
812 if (lua_cpath && strlen(lua_cpath) > 0) {
823 memset(&opts, 0x00,
sizeof(opts));
825 char path[PATH_MAX] =
"";
826 snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", script->
val);
829 int r = LuaScriptInit(path, &opts, master_config);
896 }
else if (opts.
file) {
902 }
else if (opts.
flow) {
904 }
else if (opts.
stats) {
915 result.
ctx = output_ctx;
921 output_ctx->
DeInit(output_ctx);
923 int failure_fatal = 0;
924 if (
SCConfGetBool(
"engine.init-failure-fatal", &failure_fatal) != 1) {
925 SCLogDebug(
"ConfGetBool could not load the value.");
928 FatalError(
"Error during setup of lua output. Details should be "
929 "described in previous error messages. Shutting down...");
938 static void OutputLuaLogDoDeinit(
LogLuaCtx *lua_ctx)
956 lua_getglobal(luastate,
"deinit");
957 if (lua_type(luastate, -1) == LUA_TFUNCTION) {
958 if (lua_pcall(luastate, 0, 0, 0) != 0) {
959 SCLogError(
"couldn't run script 'deinit' function: %s", lua_tostring(luastate, -1));
960 lua_pop(luastate, 1);
964 lua_pop(luastate, 1);
978 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
984 if (initdata == NULL) {
985 SCLogDebug(
"Error getting context for LuaLog. \"initdata\" argument NULL");
1011 OutputLuaLogDoDeinit(td->
lua_ctx);
1017 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
void LuaStateSetInvocationGen(lua_State *luastate, uint64_t *gen)
publish the state's invocation counter (once, at state init)
void LuaPushTableKeyValueInt(lua_State *luastate, const char *key, int value)
int PathMerge(char *out_buf, size_t buf_size, const char *const dir, const char *const fname)
struct HtpBodyChunk_ * next
lua_State * LuaGetState(void)
#define TAILQ_FOREACH(var, head, field)
bool SSHTxLogCondition(ThreadVars *tv, const Packet *p, void *state, void *tx, uint64_t tx_id)
int SCConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
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
const char * SCConfNodeLookupChildValue(const SCConfNode *node, const char *name)
Lookup the value of a child configuration node by name.
PacketLogger PacketLogFunc
#define OUTPUT_STREAMING_FLAG_TRANSACTION
char path[PATH_MAX]
Lua search path for Lua modules.
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
ThreadDeinitFunc ThreadDeinit
void SCAppLayerParserRegisterLogger(uint8_t ipproto, AppProto alproto)
#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 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...
void SCLuaRequirefBuiltIns(lua_State *L)
Register Suricata built-in modules for loading in a non-sandboxed environment.
#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)
SCConfNode * SCConfNodeLookupChild(const SCConfNode *node, const char *name)
Lookup a child configuration node by name.
char cpath[PATH_MAX]
Lua search path for C modules.
TxLoggerCondition TxLogCondition
#define OUTPUT_STREAMING_FLAG_OPEN
void(* DeInit)(struct OutputCtx_ *)
void LuaStateSetPacketAlert(lua_State *luastate, PacketAlert *pa)
bool SSLV3TxLogReady(void *tx)
void LuaLogRegister(void)
PacketLogCondition PacketConditionFunc
#define OUTPUT_STREAMING_FLAG_TOSERVER
#define SCLogError(...)
Macro used to log ERROR messages.
void LuaStateSetDirection(lua_State *luastate, int direction)
#define PACKET_ALERT_FLAG_TX
void LuaPushTableKeyValueBoolean(lua_State *luastate, const char *key, bool value)
#define OUTPUT_STREAMING_FLAG_CLOSE
#define OUTPUT_STREAMING_FLAG_TOCLIENT
void LuaPushTableKeyValueLString(lua_State *luastate, const char *key, const char *value, size_t len)
Push a key plus string value with length to the stack.
struct LogLuaCtx_ LogLuaCtx
AppProto alproto
application level protocol
char script_dir[PATH_MAX]
Path to script directory.
#define DEBUG_VALIDATE_BUG_ON(exp)
void CreateTimeString(const SCTime_t ts, char *str, size_t size)
void LuaReturnState(lua_State *s)