Go to the documentation of this file.
42 #define MODULE_NAME "LuaLog"
70 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data);
81 static int LuaTxLogger(
ThreadVars *
tv,
void *thread_data,
const Packet *p,
Flow *f,
void *alstate,
void *txptr, uint64_t tx_id)
114 static int LuaStreamingLogger(
ThreadVars *
tv,
void *thread_data,
const Flow *f,
115 const uint8_t *data, uint32_t data_len, uint64_t tx_id, uint8_t
flags)
174 if (!(PacketIsIPv4(p)) && !(PacketIsIPv6(p))) {
236 if ((!(PacketIsIPv4(p))) && (!(PacketIsIPv6(p)))) {
275 void *tx,
const uint64_t tx_id, uint8_t dir)
280 if ((!(PacketIsIPv4(p))) && (!(PacketIsIPv6(p))))
355 lua_newtable(luastate);
357 for (; u < st->
nstats; u++) {
358 lua_pushinteger(luastate, u + 1);
360 lua_newtable(luastate);
362 lua_pushstring(luastate,
"name");
363 lua_pushstring(luastate, st->
stats[u].
name);
364 lua_settable(luastate, -3);
366 lua_pushstring(luastate,
"tmname");
368 lua_settable(luastate, -3);
370 lua_pushstring(luastate,
"value");
371 lua_pushinteger(luastate, st->
stats[u].
value);
372 lua_settable(luastate, -3);
374 lua_pushstring(luastate,
"pvalue");
376 lua_settable(luastate, -3);
378 lua_settable(luastate, -3);
409 lua_getglobal(L,
"package");
411 if (strlen(
ctx->path) > 0) {
412 lua_pushstring(L,
ctx->path);
414 lua_pushstring(L,
"");
416 lua_setfield(L, -2,
"path");
418 if (strlen(
ctx->cpath) > 0) {
419 lua_pushstring(L,
ctx->cpath);
421 lua_pushstring(L,
"");
423 lua_setfield(L, -2,
"cpath");
442 if (luastate == NULL)
444 luaL_openlibs(luastate);
446 LuaSetPaths(luastate,
ctx);
448 int status = luaL_loadfile(luastate, filename);
450 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
455 if (lua_pcall(luastate, 0, 0, 0) != 0) {
456 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
460 lua_getglobal(luastate,
"init");
461 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
466 if (lua_pcall(luastate, 0, 1, 0) != 0) {
467 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
472 if (lua_gettop(luastate) == 0) {
473 SCLogError(
"init function in script should return table, nothing returned");
476 if (lua_type(luastate, 1) != LUA_TTABLE) {
477 SCLogError(
"init function in script should return table, returned is not table");
481 lua_pushnil(luastate);
483 while (lua_next(luastate, -2)) {
484 k = lua_tostring(luastate, -2);
488 v = lua_tostring(luastate, -1);
489 lua_pop(luastate, 1);
495 if (strcmp(k,
"protocol") == 0 && strcmp(v,
"http") == 0)
497 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"dns") == 0)
499 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"tls") == 0)
501 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"ssh") == 0)
503 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"smtp") == 0)
505 else if (strcmp(k,
"type") == 0 && strcmp(v,
"packet") == 0)
507 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"alerts") == 0)
509 else if (strcmp(k,
"type") == 0 && strcmp(v,
"file") == 0)
511 else if (strcmp(k,
"type") == 0 && strcmp(v,
"streaming") == 0)
513 else if (strcmp(k,
"type") == 0 && strcmp(v,
"flow") == 0)
515 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"tcp") == 0)
517 else if (strcmp(k,
"type") == 0 && strcmp(v,
"stats") == 0)
520 SCLogError(
"unknown key and/or value: k='%s', v='%s'", k, v);
526 SCLogError(
"invalid combination of 'needs' in the script");
530 lua_getglobal(luastate,
"setup");
531 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
536 lua_getglobal(luastate,
"log");
537 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
542 lua_getglobal(luastate,
"deinit");
543 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
565 if (luastate == NULL) {
570 luaL_openlibs(luastate);
572 LuaSetPaths(luastate,
ctx);
574 int status = luaL_loadfile(luastate, filename);
576 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
581 if (lua_pcall(luastate, 0, 0, 0) != 0) {
582 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
586 lua_getglobal(luastate,
"setup");
592 if (lua_pcall(luastate, 0, 0, 0) != 0) {
593 SCLogError(
"couldn't run script 'setup' function: %s", lua_tostring(luastate, -1));
597 SCLogDebug(
"lua_State %p is set up", luastate);
605 static void LogLuaSubFree(
OutputCtx *oc) {
633 BUG_ON(parent_ctx == NULL);
638 char path[PATH_MAX] =
"";
639 int ret = snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", conf->
val);
640 if (ret < 0 || ret ==
sizeof(path)) {
641 SCLogError(
"failed to construct lua script path");
647 lua_ctx->
luastate = LuaScriptSetup(path, mc);
654 output_ctx->
data = lua_ctx;
655 output_ctx->
DeInit = LogLuaSubFree;
657 result.
ctx = output_ctx;
667 static void LogLuaMasterFree(
OutputCtx *oc)
694 if (scripts == NULL) {
705 output_ctx->
DeInit = LogLuaMasterFree;
715 if (lua_path && strlen(lua_path) > 0) {
720 if (lua_cpath && strlen(lua_cpath) > 0) {
731 memset(&opts, 0x00,
sizeof(opts));
733 char path[PATH_MAX] =
"";
734 snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", script->
val);
737 int r = LuaScriptInit(path, &opts, master_config);
799 }
else if (opts.
file) {
805 }
else if (opts.
flow) {
807 }
else if (opts.
stats) {
818 result.
ctx = output_ctx;
824 output_ctx->
DeInit(output_ctx);
826 int failure_fatal = 0;
827 if (
SCConfGetBool(
"engine.init-failure-fatal", &failure_fatal) != 1) {
828 SCLogDebug(
"ConfGetBool could not load the value.");
831 FatalError(
"Error during setup of lua output. Details should be "
832 "described in previous error messages. Shutting down...");
841 static void OutputLuaLogDoDeinit(
LogLuaCtx *lua_ctx)
845 lua_getglobal(luastate,
"deinit");
846 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
852 if (lua_pcall(luastate, 0, 0, 0) != 0) {
853 SCLogError(
"couldn't run script 'deinit' function: %s", lua_tostring(luastate, -1));
864 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
870 if (initdata == NULL) {
871 SCLogDebug(
"Error getting context for LuaLog. \"initdata\" argument NULL");
897 OutputLuaLogDoDeinit(td->
lua_ctx);
903 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
#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
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
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)
#define PACKET_ALERT_FLAG_TX
struct LogLuaCtx_ LogLuaCtx
@ TLS_STATE_SERVER_HANDSHAKE_DONE
AppProto alproto
application level protocol
char script_dir[PATH_MAX]
Path to script directory.
int LuaRegisterFunctions(lua_State *luastate)
void CreateTimeString(const SCTime_t ts, char *str, size_t size)
void LuaReturnState(lua_State *s)