mirror of
https://github.com/OISF/suricata.git
synced 2026-02-03 20:41:46 -05:00
lua: create suricata.config lua lib
Currently only provides "log_path" as a replacement for SCLogPath.
This commit is contained in:
parent
16fee33368
commit
ce7cdd6f9a
10 changed files with 115 additions and 27 deletions
25
doc/userguide/lua/libs/config.rst
Normal file
25
doc/userguide/lua/libs/config.rst
Normal file
|
|
@ -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
|
||||
|
|
@ -9,6 +9,7 @@ environment without access to additional modules.
|
|||
.. toctree::
|
||||
|
||||
base64
|
||||
config
|
||||
dns
|
||||
file
|
||||
flowlib
|
||||
|
|
|
|||
|
|
@ -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
|
||||
~~~~~~~~~~~~
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 \
|
||||
|
|
|
|||
|
|
@ -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 },
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
56
src/util-lua-config.c
Normal file
56
src/util-lua-config.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
25
src/util-lua-config.h
Normal file
25
src/util-lua-config.h
Normal file
|
|
@ -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 */
|
||||
Loading…
Reference in a new issue