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 bool LuaTlsTxLogCondition(
87 static int LuaTxLogger(
ThreadVars *
tv,
void *thread_data,
const Packet *
p,
Flow *
f,
void *alstate,
void *txptr, uint64_t tx_id)
121 const uint8_t *data, uint32_t data_len, uint64_t tx_id, uint8_t
flags)
146 lua_getglobal(luastate,
"log");
147 lua_newtable(luastate);
153 lua_pushstring(luastate,
"stream");
154 lua_newtable(luastate);
163 lua_settable(luastate, -3);
165 int retval = lua_pcall(luastate, 1, 0, 0);
193 if (!(PacketIsIPv4(
p)) && !(PacketIsIPv6(
p))) {
255 if ((!(PacketIsIPv4(
p))) && (!(PacketIsIPv6(
p)))) {
294 void *tx,
const uint64_t tx_id, uint8_t dir)
299 if ((!(PacketIsIPv4(
p))) && (!(PacketIsIPv6(
p))))
374 lua_newtable(luastate);
376 for (; u < st->
nstats; u++) {
377 lua_pushinteger(luastate, u + 1);
379 lua_newtable(luastate);
381 lua_pushstring(luastate,
"name");
382 lua_pushstring(luastate, st->
stats[u].
name);
383 lua_settable(luastate, -3);
385 lua_pushstring(luastate,
"tmname");
387 lua_settable(luastate, -3);
389 lua_pushstring(luastate,
"value");
390 lua_pushinteger(luastate, st->
stats[u].
value);
391 lua_settable(luastate, -3);
393 lua_pushstring(luastate,
"pvalue");
395 lua_settable(luastate, -3);
397 lua_settable(luastate, -3);
428 lua_getglobal(L,
"package");
430 if (strlen(
ctx->path) > 0) {
431 lua_pushstring(L,
ctx->path);
433 lua_pushstring(L,
"");
435 lua_setfield(L, -2,
"path");
437 if (strlen(
ctx->cpath) > 0) {
438 lua_pushstring(L,
ctx->cpath);
440 lua_pushstring(L,
"");
442 lua_setfield(L, -2,
"cpath");
461 if (luastate == NULL)
463 luaL_openlibs(luastate);
465 LuaSetPaths(luastate,
ctx);
467 int status = luaL_loadfile(luastate, filename);
469 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
474 if (lua_pcall(luastate, 0, 0, 0) != 0) {
475 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
479 lua_getglobal(luastate,
"init");
480 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
485 if (lua_pcall(luastate, 0, 1, 0) != 0) {
486 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
491 if (lua_gettop(luastate) == 0) {
492 SCLogError(
"init function in script should return table, nothing returned");
495 if (lua_type(luastate, 1) != LUA_TTABLE) {
496 SCLogError(
"init function in script should return table, returned is not table");
500 lua_pushnil(luastate);
502 while (lua_next(luastate, -2)) {
503 k = lua_tostring(luastate, -2);
507 v = lua_tostring(luastate, -1);
508 lua_pop(luastate, 1);
514 if (strcmp(k,
"streaming") == 0) {
516 if (strcmp(v,
"http") == 0) {
518 }
else if (strcmp(v,
"tcp") == 0) {
521 SCLogError(
"unsupported streaming argument: %s", v);
524 }
else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"http") == 0)
526 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"dns") == 0)
528 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"tls") == 0)
530 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"ssh") == 0)
532 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"smtp") == 0)
534 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"ntp") == 0)
536 else if (strcmp(k,
"type") == 0 && strcmp(v,
"packet") == 0)
538 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"alerts") == 0)
540 else if (strcmp(k,
"type") == 0 && strcmp(v,
"file") == 0)
542 else if (strcmp(k,
"type") == 0 && strcmp(v,
"streaming") == 0)
544 else if (strcmp(k,
"type") == 0 && strcmp(v,
"flow") == 0)
546 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"tcp") == 0)
548 else if (strcmp(k,
"type") == 0 && strcmp(v,
"stats") == 0)
551 SCLogError(
"unknown key and/or value: k='%s', v='%s'", k, v);
557 SCLogError(
"invalid combination of 'needs' in the script");
561 lua_getglobal(luastate,
"setup");
562 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
567 lua_getglobal(luastate,
"log");
568 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
573 lua_getglobal(luastate,
"deinit");
574 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
596 if (luastate == NULL) {
601 luaL_openlibs(luastate);
603 LuaSetPaths(luastate,
ctx);
605 int status = luaL_loadfile(luastate, filename);
607 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
612 if (lua_pcall(luastate, 0, 0, 0) != 0) {
613 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
617 lua_getglobal(luastate,
"setup");
619 if (lua_pcall(luastate, 0, 0, 0) != 0) {
620 SCLogError(
"couldn't run script 'setup' function: %s", lua_tostring(luastate, -1));
624 SCLogDebug(
"lua_State %p is set up", luastate);
632 static void LogLuaSubFree(
OutputCtx *oc) {
660 BUG_ON(parent_ctx == NULL);
665 char path[PATH_MAX] =
"";
666 if (strlen(dir) > 0) {
667 if (
PathMerge(path,
sizeof(path), dir, conf->
val) < 0) {
668 SCLogError(
"failed to construct lua script path");
677 lua_ctx->
luastate = LuaScriptSetup(path, mc);
684 output_ctx->
data = lua_ctx;
685 output_ctx->
DeInit = LogLuaSubFree;
687 result.
ctx = output_ctx;
697 static void LogLuaMasterFree(
OutputCtx *oc)
724 if (scripts == NULL) {
735 output_ctx->
DeInit = LogLuaMasterFree;
745 if (lua_path && strlen(lua_path) > 0) {
750 if (lua_cpath && strlen(lua_cpath) > 0) {
761 memset(&opts, 0x00,
sizeof(opts));
763 char path[PATH_MAX] =
"";
764 snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", script->
val);
767 int r = LuaScriptInit(path, &opts, master_config);
834 }
else if (opts.
file) {
840 }
else if (opts.
flow) {
842 }
else if (opts.
stats) {
853 result.
ctx = output_ctx;
859 output_ctx->
DeInit(output_ctx);
861 int failure_fatal = 0;
862 if (
SCConfGetBool(
"engine.init-failure-fatal", &failure_fatal) != 1) {
863 SCLogDebug(
"ConfGetBool could not load the value.");
866 FatalError(
"Error during setup of lua output. Details should be "
867 "described in previous error messages. Shutting down...");
876 static void OutputLuaLogDoDeinit(
LogLuaCtx *lua_ctx)
880 lua_getglobal(luastate,
"deinit");
881 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
887 if (lua_pcall(luastate, 0, 0, 0) != 0) {
888 SCLogError(
"couldn't run script 'deinit' function: %s", lua_tostring(luastate, -1));
899 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
905 if (initdata == NULL) {
906 SCLogDebug(
"Error getting context for LuaLog. \"initdata\" argument NULL");
932 OutputLuaLogDoDeinit(td->
lua_ctx);
938 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)
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 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
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)