From: Jason Ish Date: Fri, 30 May 2025 22:54:29 +0000 (-0600) Subject: lua: create suricata.config lua lib X-Git-Tag: suricata-8.0.0-rc1~195 X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=ce7cdd6f9a0658d2c868678094ab92947a72e98f;p=thirdparty%2Fsuricata.git lua: create suricata.config lua lib Currently only provides "log_path" as a replacement for SCLogPath. --- diff --git a/doc/userguide/lua/libs/config.rst b/doc/userguide/lua/libs/config.rst new file mode 100644 index 0000000000..8b09a3bef0 --- /dev/null +++ b/doc/userguide/lua/libs/config.rst @@ -0,0 +1,25 @@ +Config Library +############## + +The config library provides access to Suricata configuration settings. + +To use this library, you must require it:: + + local config = require("suricata.config") + +Functions +********* + +``log_path()`` +============== + +Returns the configured log directory path. + +Example:: + + local config = require("suricata.config") + + local log_path, err = config.log_path() + if log_path == nil then + print("failed to get log path " .. err) + end diff --git a/doc/userguide/lua/libs/index.rst b/doc/userguide/lua/libs/index.rst index 0778b63440..cc63fb99c8 100644 --- a/doc/userguide/lua/libs/index.rst +++ b/doc/userguide/lua/libs/index.rst @@ -9,6 +9,7 @@ environment without access to additional modules. .. toctree:: base64 + config dns file flowlib diff --git a/doc/userguide/lua/lua-functions.rst b/doc/userguide/lua/lua-functions.rst index dce63ced6d..8a23957e41 100644 --- a/doc/userguide/lua/lua-functions.rst +++ b/doc/userguide/lua/lua-functions.rst @@ -315,19 +315,6 @@ SCThreadInfo It gives: tid (integer), tname (string), tgroup (string) -SCLogPath -~~~~~~~~~ - -Expose the log path. - -:: - - - name = "fast_lua.log" - function setup (args) - filename = SCLogPath() .. "/" .. name - file = assert(io.open(filename, "a")) - end SCByteVarGet ~~~~~~~~~~~~ diff --git a/doc/userguide/output/lua-output.rst b/doc/userguide/output/lua-output.rst index d21ae7de76..597de6c561 100644 --- a/doc/userguide/output/lua-output.rst +++ b/doc/userguide/output/lua-output.rst @@ -27,6 +27,7 @@ Example: :: + local config = require("suricata.config") local logger = require("suricata.log") function init (args) @@ -36,7 +37,7 @@ Example: end function setup (args) - filename = SCLogPath() .. "/" .. name + filename = config.log_path() .. "/" .. name file = assert(io.open(filename, "a")) logger.info("HTTP Log Filename " .. filename) http = 0 diff --git a/lua/fast.lua b/lua/fast.lua index f72283ef00..abeb9d055a 100644 --- a/lua/fast.lua +++ b/lua/fast.lua @@ -18,6 +18,7 @@ local packet = require("suricata.packet") local rule = require("suricata.rule") +local config = require("suricata.config") function init() local needs = {} @@ -27,7 +28,7 @@ function init() end function setup() - filename = SCLogPath() .. "/fast.log" + filename = config.log_path() .. "/fast.log" file = assert(io.open(filename, "a")) alert_count = 0 end diff --git a/src/Makefile.am b/src/Makefile.am index 4501243b92..44ba243ff9 100755 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -536,6 +536,7 @@ noinst_HEADERS = \ util-lua-base64lib.h \ util-lua-builtins.h \ util-lua-common.h \ + util-lua-config.h \ util-lua-dataset.h \ util-lua-dnp3-objects.h \ util-lua-dnp3.h \ @@ -1114,6 +1115,7 @@ libsuricata_c_a_SOURCES = \ util-lua-base64lib.c \ util-lua-builtins.c \ util-lua-common.c \ + util-lua-config.c \ util-lua-dataset.c \ util-lua-dnp3-objects.c \ util-lua-dnp3.c \ diff --git a/src/util-lua-builtins.c b/src/util-lua-builtins.c index 5b45ab7130..f0898c13b5 100644 --- a/src/util-lua-builtins.c +++ b/src/util-lua-builtins.c @@ -18,6 +18,7 @@ #include "suricata-common.h" #include "util-lua-builtins.h" #include "util-lua-base64lib.h" +#include "util-lua-config.h" #include "util-lua-dataset.h" #include "util-lua-dnp3.h" #include "util-lua-flowintlib.h" @@ -39,6 +40,7 @@ static const luaL_Reg builtins[] = { { "suricata.base64", SCLuaLoadBase64Lib }, + { "suricata.config", SCLuaLoadConfigLib }, { "suricata.dataset", LuaLoadDatasetLib }, { "suricata.dnp3", SCLuaLoadDnp3Lib }, { "suricata.dns", SCLuaLoadDnsLib }, diff --git a/src/util-lua-common.c b/src/util-lua-common.c index 33498b088f..ca9b9915fd 100644 --- a/src/util-lua-common.c +++ b/src/util-lua-common.c @@ -113,15 +113,6 @@ static int LuaCallbackStreamingBuffer(lua_State *luastate) return LuaCallbackStreamingBufferPushToStack(luastate, b); } -static int LuaCallbackLogPath(lua_State *luastate) -{ - const char *ld = SCConfigGetLogDirectory(); - if (ld == NULL) - return LuaCallbackError(luastate, "internal error: no log dir"); - - return LuaPushStringBuffer(luastate, (const uint8_t *)ld, strlen(ld)); -} - /** \internal * \brief fill lua stack with thread info * \param luastate the lua state @@ -158,9 +149,6 @@ int LuaRegisterFunctions(lua_State *luastate) lua_pushcfunction(luastate, LuaCallbackStreamingBuffer); lua_setglobal(luastate, "SCStreamingBuffer"); - lua_pushcfunction(luastate, LuaCallbackLogPath); - lua_setglobal(luastate, "SCLogPath"); - lua_pushcfunction(luastate, LuaCallbackThreadInfo); lua_setglobal(luastate, "SCThreadInfo"); return 0; diff --git a/src/util-lua-config.c b/src/util-lua-config.c new file mode 100644 index 0000000000..e5632b392b --- /dev/null +++ b/src/util-lua-config.c @@ -0,0 +1,56 @@ +/* Copyright (C) 2025 Open Information Security Foundation + * + * You can copy, redistribute or modify this Program under the terms of + * the GNU General Public License version 2 as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +/** + * \file + * + * Configuration API for Lua. + * + * local config = require("suricata.config") + */ + +#include "suricata-common.h" +#include "util-lua-config.h" +#include "conf.h" +#include "util-conf.h" +#include "app-layer-protos.h" +#include "util-lua-common.h" +#include "util-lua.h" + +#include "lauxlib.h" + +static int LuaConfigLogPath(lua_State *L) +{ + const char *ld = SCConfigGetLogDirectory(); + if (ld == NULL) + return LuaCallbackError(L, "internal error: no log dir"); + + return LuaPushStringBuffer(L, (const uint8_t *)ld, strlen(ld)); +} + +static const luaL_Reg configlib[] = { + // clang-format off + { "log_path", LuaConfigLogPath }, + { NULL, NULL }, + // clang-format on +}; + +int SCLuaLoadConfigLib(lua_State *L) +{ + luaL_newlib(L, configlib); + return 1; +} diff --git a/src/util-lua-config.h b/src/util-lua-config.h new file mode 100644 index 0000000000..d06dea75a9 --- /dev/null +++ b/src/util-lua-config.h @@ -0,0 +1,25 @@ +/* Copyright (C) 2025 Open Information Security Foundation + * + * You can copy, redistribute or modify this Program under the terms of + * the GNU General Public License version 2 as published by the Free + * Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * version 2 along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +#ifndef UTIL_LUA_CONFIG_H +#define UTIL_LUA_CONFIG_H + +#include "lua.h" + +int SCLuaLoadConfigLib(lua_State *luastate); + +#endif /* UTIL_LUA_CONFIG_H */ \ No newline at end of file