Go to the documentation of this file.
41 #define MODULE_NAME "LuaLog"
69 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data);
80 static int LuaTxLogger(
ThreadVars *
tv,
void *thread_data,
const Packet *p,
Flow *f,
void *alstate,
void *txptr, uint64_t tx_id)
113 static int LuaStreamingLogger(
ThreadVars *
tv,
void *thread_data,
const Flow *f,
114 const uint8_t *data, uint32_t data_len, uint64_t tx_id, uint8_t
flags)
139 lua_getglobal(luastate,
"log");
140 lua_newtable(luastate);
146 lua_pushstring(luastate,
"stream");
147 lua_newtable(luastate);
156 lua_settable(luastate, -3);
158 int retval = lua_pcall(luastate, 1, 0, 0);
186 if (!(PacketIsIPv4(p)) && !(PacketIsIPv6(p))) {
248 if ((!(PacketIsIPv4(p))) && (!(PacketIsIPv6(p)))) {
287 void *tx,
const uint64_t tx_id, uint8_t dir)
292 if ((!(PacketIsIPv4(p))) && (!(PacketIsIPv6(p))))
367 lua_newtable(luastate);
369 for (; u < st->
nstats; u++) {
370 lua_pushinteger(luastate, u + 1);
372 lua_newtable(luastate);
374 lua_pushstring(luastate,
"name");
375 lua_pushstring(luastate, st->
stats[u].
name);
376 lua_settable(luastate, -3);
378 lua_pushstring(luastate,
"tmname");
380 lua_settable(luastate, -3);
382 lua_pushstring(luastate,
"value");
383 lua_pushinteger(luastate, st->
stats[u].
value);
384 lua_settable(luastate, -3);
386 lua_pushstring(luastate,
"pvalue");
388 lua_settable(luastate, -3);
390 lua_settable(luastate, -3);
421 lua_getglobal(L,
"package");
423 if (strlen(
ctx->path) > 0) {
424 lua_pushstring(L,
ctx->path);
426 lua_pushstring(L,
"");
428 lua_setfield(L, -2,
"path");
430 if (strlen(
ctx->cpath) > 0) {
431 lua_pushstring(L,
ctx->cpath);
433 lua_pushstring(L,
"");
435 lua_setfield(L, -2,
"cpath");
454 if (luastate == NULL)
456 luaL_openlibs(luastate);
458 LuaSetPaths(luastate,
ctx);
460 int status = luaL_loadfile(luastate, filename);
462 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
467 if (lua_pcall(luastate, 0, 0, 0) != 0) {
468 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
472 lua_getglobal(luastate,
"init");
473 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
478 if (lua_pcall(luastate, 0, 1, 0) != 0) {
479 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
484 if (lua_gettop(luastate) == 0) {
485 SCLogError(
"init function in script should return table, nothing returned");
488 if (lua_type(luastate, 1) != LUA_TTABLE) {
489 SCLogError(
"init function in script should return table, returned is not table");
493 lua_pushnil(luastate);
495 while (lua_next(luastate, -2)) {
496 k = lua_tostring(luastate, -2);
500 v = lua_tostring(luastate, -1);
501 lua_pop(luastate, 1);
507 if (strcmp(k,
"streaming") == 0) {
509 if (strcmp(v,
"http") == 0) {
511 }
else if (strcmp(v,
"tcp") == 0) {
514 SCLogError(
"unsupported streaming argument: %s", v);
517 }
else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"http") == 0)
519 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"dns") == 0)
521 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"tls") == 0)
523 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"ssh") == 0)
525 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"smtp") == 0)
527 else if (strcmp(k,
"type") == 0 && strcmp(v,
"packet") == 0)
529 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"alerts") == 0)
531 else if (strcmp(k,
"type") == 0 && strcmp(v,
"file") == 0)
533 else if (strcmp(k,
"type") == 0 && strcmp(v,
"streaming") == 0)
535 else if (strcmp(k,
"type") == 0 && strcmp(v,
"flow") == 0)
537 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"tcp") == 0)
539 else if (strcmp(k,
"type") == 0 && strcmp(v,
"stats") == 0)
542 SCLogError(
"unknown key and/or value: k='%s', v='%s'", k, v);
548 SCLogError(
"invalid combination of 'needs' in the script");
552 lua_getglobal(luastate,
"setup");
553 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
558 lua_getglobal(luastate,
"log");
559 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
564 lua_getglobal(luastate,
"deinit");
565 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
587 if (luastate == NULL) {
592 luaL_openlibs(luastate);
594 LuaSetPaths(luastate,
ctx);
596 int status = luaL_loadfile(luastate, filename);
598 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
603 if (lua_pcall(luastate, 0, 0, 0) != 0) {
604 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
608 lua_getglobal(luastate,
"setup");
610 if (lua_pcall(luastate, 0, 0, 0) != 0) {
611 SCLogError(
"couldn't run script 'setup' function: %s", lua_tostring(luastate, -1));
615 SCLogDebug(
"lua_State %p is set up", luastate);
623 static void LogLuaSubFree(
OutputCtx *oc) {
651 BUG_ON(parent_ctx == NULL);
656 char path[PATH_MAX] =
"";
657 int ret = snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", conf->
val);
658 if (ret < 0 || ret ==
sizeof(path)) {
659 SCLogError(
"failed to construct lua script path");
665 lua_ctx->
luastate = LuaScriptSetup(path, mc);
672 output_ctx->
data = lua_ctx;
673 output_ctx->
DeInit = LogLuaSubFree;
675 result.
ctx = output_ctx;
685 static void LogLuaMasterFree(
OutputCtx *oc)
712 if (scripts == NULL) {
723 output_ctx->
DeInit = LogLuaMasterFree;
733 if (lua_path && strlen(lua_path) > 0) {
738 if (lua_cpath && strlen(lua_cpath) > 0) {
749 memset(&opts, 0x00,
sizeof(opts));
751 char path[PATH_MAX] =
"";
752 snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", script->
val);
755 int r = LuaScriptInit(path, &opts, master_config);
817 }
else if (opts.
file) {
823 }
else if (opts.
flow) {
825 }
else if (opts.
stats) {
836 result.
ctx = output_ctx;
842 output_ctx->
DeInit(output_ctx);
844 int failure_fatal = 0;
845 if (
SCConfGetBool(
"engine.init-failure-fatal", &failure_fatal) != 1) {
846 SCLogDebug(
"ConfGetBool could not load the value.");
849 FatalError(
"Error during setup of lua output. Details should be "
850 "described in previous error messages. Shutting down...");
859 static void OutputLuaLogDoDeinit(
LogLuaCtx *lua_ctx)
863 lua_getglobal(luastate,
"deinit");
864 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
870 if (lua_pcall(luastate, 0, 0, 0) != 0) {
871 SCLogError(
"couldn't run script 'deinit' function: %s", lua_tostring(luastate, -1));
882 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
888 if (initdata == NULL) {
889 SCLogDebug(
"Error getting context for LuaLog. \"initdata\" argument NULL");
915 OutputLuaLogDoDeinit(td->
lua_ctx);
921 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 LuaPushTableKeyValueInt(lua_State *luastate, const char *key, int value)
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
@ TLS_STATE_CLIENT_HANDSHAKE_DONE
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)
void LuaLogRegister(void)
PacketLogCondition PacketConditionFunc
#define OUTPUT_STREAMING_FLAG_TOSERVER
#define SCLogError(...)
Macro used to log ERROR messages.
void LuaStateSetStreamingBuffer(lua_State *luastate, LuaStreamingBuffer *b)
#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
@ TLS_STATE_SERVER_HANDSHAKE_DONE
AppProto alproto
application level protocol
char script_dir[PATH_MAX]
Path to script directory.
void CreateTimeString(const SCTime_t ts, char *str, size_t size)
void LuaReturnState(lua_State *s)