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)
173 if (!(PacketIsIPv4(p)) && !(PacketIsIPv6(p))) {
235 if ((!(PacketIsIPv4(p))) && (!(PacketIsIPv6(p)))) {
274 void *tx,
const uint64_t tx_id, uint8_t dir)
279 if ((!(PacketIsIPv4(p))) && (!(PacketIsIPv6(p))))
354 lua_newtable(luastate);
356 for (; u < st->
nstats; u++) {
357 lua_pushinteger(luastate, u + 1);
359 lua_newtable(luastate);
361 lua_pushstring(luastate,
"name");
362 lua_pushstring(luastate, st->
stats[u].
name);
363 lua_settable(luastate, -3);
365 lua_pushstring(luastate,
"tmname");
367 lua_settable(luastate, -3);
369 lua_pushstring(luastate,
"value");
370 lua_pushinteger(luastate, st->
stats[u].
value);
371 lua_settable(luastate, -3);
373 lua_pushstring(luastate,
"pvalue");
375 lua_settable(luastate, -3);
377 lua_settable(luastate, -3);
408 lua_getglobal(L,
"package");
410 if (strlen(
ctx->path) > 0) {
411 lua_pushstring(L,
ctx->path);
413 lua_pushstring(L,
"");
415 lua_setfield(L, -2,
"path");
417 if (strlen(
ctx->cpath) > 0) {
418 lua_pushstring(L,
ctx->cpath);
420 lua_pushstring(L,
"");
422 lua_setfield(L, -2,
"cpath");
441 if (luastate == NULL)
443 luaL_openlibs(luastate);
445 LuaSetPaths(luastate,
ctx);
447 int status = luaL_loadfile(luastate, filename);
449 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
454 if (lua_pcall(luastate, 0, 0, 0) != 0) {
455 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
459 lua_getglobal(luastate,
"init");
460 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
465 if (lua_pcall(luastate, 0, 1, 0) != 0) {
466 SCLogError(
"couldn't run script 'init' function: %s", lua_tostring(luastate, -1));
471 if (lua_gettop(luastate) == 0) {
472 SCLogError(
"init function in script should return table, nothing returned");
475 if (lua_type(luastate, 1) != LUA_TTABLE) {
476 SCLogError(
"init function in script should return table, returned is not table");
480 lua_pushnil(luastate);
482 while (lua_next(luastate, -2)) {
483 k = lua_tostring(luastate, -2);
487 v = lua_tostring(luastate, -1);
488 lua_pop(luastate, 1);
494 if (strcmp(k,
"protocol") == 0 && strcmp(v,
"http") == 0)
496 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"dns") == 0)
498 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"tls") == 0)
500 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"ssh") == 0)
502 else if (strcmp(k,
"protocol") == 0 && strcmp(v,
"smtp") == 0)
504 else if (strcmp(k,
"type") == 0 && strcmp(v,
"packet") == 0)
506 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"alerts") == 0)
508 else if (strcmp(k,
"type") == 0 && strcmp(v,
"file") == 0)
510 else if (strcmp(k,
"type") == 0 && strcmp(v,
"streaming") == 0)
512 else if (strcmp(k,
"type") == 0 && strcmp(v,
"flow") == 0)
514 else if (strcmp(k,
"filter") == 0 && strcmp(v,
"tcp") == 0)
516 else if (strcmp(k,
"type") == 0 && strcmp(v,
"stats") == 0)
519 SCLogError(
"unknown key and/or value: k='%s', v='%s'", k, v);
525 SCLogError(
"invalid combination of 'needs' in the script");
529 lua_getglobal(luastate,
"setup");
530 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
535 lua_getglobal(luastate,
"log");
536 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
541 lua_getglobal(luastate,
"deinit");
542 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
564 if (luastate == NULL) {
569 luaL_openlibs(luastate);
571 LuaSetPaths(luastate,
ctx);
573 int status = luaL_loadfile(luastate, filename);
575 SCLogError(
"couldn't load file: %s", lua_tostring(luastate, -1));
580 if (lua_pcall(luastate, 0, 0, 0) != 0) {
581 SCLogError(
"couldn't prime file: %s", lua_tostring(luastate, -1));
585 lua_getglobal(luastate,
"setup");
590 if (lua_pcall(luastate, 0, 0, 0) != 0) {
591 SCLogError(
"couldn't run script 'setup' function: %s", lua_tostring(luastate, -1));
595 SCLogDebug(
"lua_State %p is set up", luastate);
603 static void LogLuaSubFree(
OutputCtx *oc) {
631 BUG_ON(parent_ctx == NULL);
636 char path[PATH_MAX] =
"";
637 int ret = snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", conf->
val);
638 if (ret < 0 || ret ==
sizeof(path)) {
639 SCLogError(
"failed to construct lua script path");
645 lua_ctx->
luastate = LuaScriptSetup(path, mc);
652 output_ctx->
data = lua_ctx;
653 output_ctx->
DeInit = LogLuaSubFree;
655 result.
ctx = output_ctx;
665 static void LogLuaMasterFree(
OutputCtx *oc)
692 if (scripts == NULL) {
703 output_ctx->
DeInit = LogLuaMasterFree;
713 if (lua_path && strlen(lua_path) > 0) {
718 if (lua_cpath && strlen(lua_cpath) > 0) {
729 memset(&opts, 0x00,
sizeof(opts));
731 char path[PATH_MAX] =
"";
732 snprintf(path,
sizeof(path),
"%s%s%s", dir, strlen(dir) ?
"/" :
"", script->
val);
735 int r = LuaScriptInit(path, &opts, master_config);
797 }
else if (opts.
file) {
803 }
else if (opts.
flow) {
805 }
else if (opts.
stats) {
816 result.
ctx = output_ctx;
822 output_ctx->
DeInit(output_ctx);
824 int failure_fatal = 0;
825 if (
SCConfGetBool(
"engine.init-failure-fatal", &failure_fatal) != 1) {
826 SCLogDebug(
"ConfGetBool could not load the value.");
829 FatalError(
"Error during setup of lua output. Details should be "
830 "described in previous error messages. Shutting down...");
839 static void OutputLuaLogDoDeinit(
LogLuaCtx *lua_ctx)
843 lua_getglobal(luastate,
"deinit");
844 if (lua_type(luastate, -1) != LUA_TFUNCTION) {
850 if (lua_pcall(luastate, 0, 0, 0) != 0) {
851 SCLogError(
"couldn't run script 'deinit' function: %s", lua_tostring(luastate, -1));
862 static TmEcode LuaLogThreadInit(
ThreadVars *t,
const void *initdata,
void **data)
868 if (initdata == NULL) {
869 SCLogDebug(
"Error getting context for LuaLog. \"initdata\" argument NULL");
895 OutputLuaLogDoDeinit(td->
lua_ctx);
901 memset(td, 0,
sizeof(*td));