suricata
util-lua-filelib.c
Go to the documentation of this file.
1/* Copyright (C) 2025 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18#include "suricata-common.h"
19#include "util-lua.h"
20#include "util-lua-common.h"
21#include "util-lua-filelib.h"
22
23#include "lua.h"
24#include "lauxlib.h"
25
26static const char file_mt[] = "suricata:file:mt";
27
28struct LuaFile {
30};
31
32static int LuaFileGetFile(lua_State *L)
33{
34 File *file = LuaStateGetFile(L);
35 if (file == NULL) {
36 return LuaCallbackError(L, "error: no file found");
37 }
38
39 struct LuaFile *lua_file = (struct LuaFile *)lua_newuserdata(L, sizeof(*lua_file));
40 if (lua_file == NULL) {
41 return LuaCallbackError(L, "error: fail to allocate user data");
42 }
43 lua_file->file = file;
44
45 luaL_getmetatable(L, file_mt);
46 lua_setmetatable(L, -2);
47
48 return 1;
49}
50
51static int LuaFileGetFileId(lua_State *L)
52{
53 struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
54 const File *file = lua_file->file;
55 lua_pushinteger(L, file->file_store_id);
56
57 return 1;
58}
59
60static int LuaFileGetTxId(lua_State *L)
61{
62 lua_Integer tx_id = LuaStateGetTxId(L);
63 lua_pushinteger(L, tx_id);
64
65 return 1;
66}
67
68static int LuaFileGetName(lua_State *L)
69{
70 struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
71 const File *file = lua_file->file;
72 lua_pushlstring(L, (char *)file->name, file->name_len);
73
74 return 1;
75}
76
77static int LuaFileGetSize(lua_State *L)
78{
79 struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
80 const File *file = lua_file->file;
81 lua_pushinteger(L, FileTrackedSize(file));
82
83 return 1;
84}
85
86static int LuaFileGetMagic(lua_State *L)
87{
88#ifdef HAVE_MAGIC
89 struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
90 const File *file = lua_file->file;
91 if (file->magic != NULL) {
92 lua_pushstring(L, file->magic);
93 } else {
94 lua_pushnil(L);
95 }
96#else
97 lua_pushnil(L);
98#endif
99
100 return 1;
101}
102
103static void PushHex(lua_State *L, const uint8_t *buf, size_t len)
104{
105 /* Large enough for sha256. */
106 char hex[65] = "";
107 for (size_t i = 0; i < len; i++) {
108 char one[3] = "";
109 snprintf(one, sizeof(one), "%02x", buf[i]);
110 strlcat(hex, one, sizeof(hex));
111 }
112
113 lua_pushstring(L, hex);
114}
115
116static int LuaFileGetMd5(lua_State *L)
117{
118 struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
119 const File *file = lua_file->file;
120
121 if (file->flags & FILE_MD5) {
122 PushHex(L, file->md5, sizeof(file->md5));
123 } else {
124 lua_pushnil(L);
125 }
126
127 return 1;
128}
129
130static int LuaFileGetSha1(lua_State *L)
131{
132 struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
133 const File *file = lua_file->file;
134
135 if (file->flags & FILE_SHA1) {
136 PushHex(L, file->sha1, sizeof(file->sha1));
137 } else {
138 lua_pushnil(L);
139 }
140
141 return 1;
142}
143
144static int LuaFileGetSha256(lua_State *L)
145{
146 struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
147 const File *file = lua_file->file;
148
149 if (file->flags & FILE_SHA256) {
150 PushHex(L, file->sha256, sizeof(file->sha256));
151 } else {
152 lua_pushnil(L);
153 }
154
155 return 1;
156}
157
158static int LuaFileGetState(lua_State *L)
159{
160 struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
161 const File *file = lua_file->file;
162
163 const char *state = "UNKNOWN";
164 switch (file->state) {
166 state = "CLOSED";
167 break;
169 state = "TRUNCATED";
170 break;
171 case FILE_STATE_ERROR:
172 state = "ERROR";
173 break;
175 state = "OPENED";
176 break;
177 case FILE_STATE_NONE:
178 state = "NONE";
179 break;
180 case FILE_STATE_MAX:
181 break;
182 }
183
184 lua_pushstring(L, state);
185
186 return 1;
187}
188
189static int LuaFileIsStored(lua_State *L)
190{
191 struct LuaFile *lua_file = luaL_checkudata(L, 1, file_mt);
192 const File *file = lua_file->file;
193 lua_pushboolean(L, file->flags & FILE_STORED);
194
195 return 1;
196}
197
198static const struct luaL_Reg filelib[] = {
199 { "get_state", LuaFileGetState },
200 { "is_stored", LuaFileIsStored },
201 { "file_id", LuaFileGetFileId },
202 { "tx_id", LuaFileGetTxId },
203 { "name", LuaFileGetName },
204 { "size", LuaFileGetSize },
205 { "magic", LuaFileGetMagic },
206 { "md5", LuaFileGetMd5 },
207 { "sha1", LuaFileGetSha1 },
208 { "sha256", LuaFileGetSha256 },
209 { NULL, NULL },
210};
211
212static const struct luaL_Reg filemodlib[] = {
213 { "get_file", LuaFileGetFile },
214 { NULL, NULL },
215};
216
218{
219 luaL_newmetatable(L, file_mt);
220 lua_pushvalue(L, -1);
221 lua_setfield(L, -2, "__index");
222 luaL_setfuncs(L, filelib, 0);
223
224 luaL_newlib(L, filemodlib);
225
226 return 1;
227}
uint8_t len
uint8_t * name
Definition util-file.h:88
uint16_t name_len
Definition util-file.h:81
uint8_t md5[SC_MD5_LEN]
Definition util-file.h:94
uint16_t flags
Definition util-file.h:80
FileState state
Definition util-file.h:82
uint8_t sha256[SC_SHA256_LEN]
Definition util-file.h:98
uint8_t sha1[SC_SHA1_LEN]
Definition util-file.h:96
uint32_t file_store_id
Definition util-file.h:85
struct lua_State lua_State
size_t strlcat(char *, const char *src, size_t siz)
uint64_t FileTrackedSize(const File *file)
get the size of the file
Definition util-file.c:326
#define FILE_SHA256
Definition util-file.h:52
#define FILE_MD5
Definition util-file.h:48
#define FILE_STORED
Definition util-file.h:56
@ FILE_STATE_OPENED
Definition util-file.h:70
@ FILE_STATE_TRUNCATED
Definition util-file.h:73
@ FILE_STATE_MAX
Definition util-file.h:76
@ FILE_STATE_ERROR
Definition util-file.h:75
@ FILE_STATE_NONE
Definition util-file.h:69
@ FILE_STATE_CLOSED
Definition util-file.h:71
#define FILE_SHA1
Definition util-file.h:50
int LuaCallbackError(lua_State *luastate, const char *msg)
int SCLuaLoadFileLib(lua_State *L)
File * LuaStateGetFile(lua_State *luastate)
get file pointer from the lua state
Definition util-lua.c:223
uint64_t LuaStateGetTxId(lua_State *luastate)
get tx id from the lua state
Definition util-lua.c:143