]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
lua: create suricata.config lua lib
authorJason Ish <jason.ish@oisf.net>
Fri, 30 May 2025 22:54:29 +0000 (16:54 -0600)
committerVictor Julien <victor@inliniac.net>
Wed, 4 Jun 2025 07:39:51 +0000 (09:39 +0200)
Currently only provides "log_path" as a replacement for SCLogPath.

doc/userguide/lua/libs/config.rst [new file with mode: 0644]
doc/userguide/lua/libs/index.rst
doc/userguide/lua/lua-functions.rst
doc/userguide/output/lua-output.rst
lua/fast.lua
src/Makefile.am
src/util-lua-builtins.c
src/util-lua-common.c
src/util-lua-config.c [new file with mode: 0644]
src/util-lua-config.h [new file with mode: 0644]

diff --git a/doc/userguide/lua/libs/config.rst b/doc/userguide/lua/libs/config.rst
new file mode 100644 (file)
index 0000000..8b09a3b
--- /dev/null
@@ -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
index 0778b63440a2451e13bab13ff8ae02a672a570c0..cc63fb99c8fc18551bc05932cf675efbc8752b79 100644 (file)
@@ -9,6 +9,7 @@ environment without access to additional modules.
 .. toctree::
 
    base64
+   config
    dns
    file
    flowlib
index dce63ced6d77fe64f6edab41986fab3ed5afa98f..8a23957e4171d1e4e4f3fa29c3b377233a3221e7 100644 (file)
@@ -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
 ~~~~~~~~~~~~
index d21ae7de761418f2d7d44389034f6277ddf5e02b..597de6c561ed833398771fbde4b8a38f03bb6051 100644 (file)
@@ -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
index f72283ef00c90694cfbd98b1b965951194e10bde..abeb9d055a79b2a88516163bc075f0aca3c43381 100644 (file)
@@ -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
index 4501243b924a5e27c5aa3d98b89664a231e0e44f..44ba243ff924058432634db72700923dc0ee083b 100755 (executable)
@@ -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 \
index 5b45ab7130ad886c9cd09c9dce0ff5e2dba06c8a..f0898c13b55320d868c719120b4f2c4728ac5dc3 100644 (file)
@@ -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 },
index 33498b088f1c848db6b9eee7ad0144742673df20..ca9b9915fd69cc9f74710f4e567f2183f2285ed2 100644 (file)
@@ -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 (file)
index 0000000..e5632b3
--- /dev/null
@@ -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 (file)
index 0000000..d06dea7
--- /dev/null
@@ -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