suricata
util-lua-common.c
Go to the documentation of this file.
1 /* Copyright (C) 2014-2021 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 /**
19  * \file
20  *
21  * \author Victor Julien <victor@inliniac.net>
22  *
23  * Common function for Lua Output
24  */
25 
26 #include "suricata-common.h"
27 
28 #include "threads.h"
29 #include "threadvars.h"
30 
31 #include "output.h"
32 #include "util-conf.h"
33 
34 #include "lua.h"
35 
36 #include "util-lua.h"
37 #include "util-lua-common.h"
38 
39 int LuaCallbackError(lua_State *luastate, const char *msg)
40 {
41  lua_pushnil(luastate);
42  lua_pushstring(luastate, msg);
43  return 2;
44 }
45 
46 const char *LuaGetStringArgument(lua_State *luastate, int idx)
47 {
48  /* get argument */
49  if (!lua_isstring(luastate, idx))
50  return NULL;
51  const char *str = lua_tostring(luastate, idx);
52  if (str == NULL)
53  return NULL;
54  if (strlen(str) == 0)
55  return NULL;
56  return str;
57 }
58 
59 void LuaPushTableKeyValueInt(lua_State *luastate, const char *key, int value)
60 {
61  lua_pushstring(luastate, key);
62  lua_pushnumber(luastate, value);
63  lua_settable(luastate, -3);
64 }
65 
66 /** \brief Push a key plus string value to the stack
67  *
68  * If value is NULL, string "(null")" will be put on the stack.
69  */
70 void LuaPushTableKeyValueString(lua_State *luastate, const char *key, const char *value)
71 {
72  lua_pushstring(luastate, key);
73  lua_pushstring(luastate, value ? value : "(null)");
74  lua_settable(luastate, -3);
75 }
76 
77 void LuaPushTableKeyValueArray(lua_State *luastate, const char *key, const uint8_t *value, size_t len)
78 {
79  lua_pushstring(luastate, key);
80  LuaPushStringBuffer(luastate, value, len);
81  lua_settable(luastate, -3);
82 }
83 
84 /** \internal
85  * \brief fill lua stack with payload
86  * \param luastate the lua state
87  * \param p packet
88  * \retval cnt number of data items placed on the stack
89  *
90  * Places: payload (string), open (bool), close (bool), toserver (bool), toclient (bool)
91  */
92 static int LuaCallbackStreamingBufferPushToStack(lua_State *luastate, const LuaStreamingBuffer *b)
93 {
94  //PrintRawDataFp(stdout, (uint8_t *)b->data, b->data_len);
95  lua_pushlstring (luastate, (const char *)b->data, b->data_len);
96  lua_pushboolean (luastate, (b->flags & OUTPUT_STREAMING_FLAG_OPEN));
97  lua_pushboolean (luastate, (b->flags & OUTPUT_STREAMING_FLAG_CLOSE));
98  lua_pushboolean (luastate, (b->flags & OUTPUT_STREAMING_FLAG_TOSERVER));
99  lua_pushboolean (luastate, (b->flags & OUTPUT_STREAMING_FLAG_TOCLIENT));
100  return 5;
101 }
102 
103 /** \internal
104  * \brief Wrapper for getting payload into a lua script
105  * \retval cnt number of items placed on the stack
106  */
107 static int LuaCallbackStreamingBuffer(lua_State *luastate)
108 {
109  const LuaStreamingBuffer *b = LuaStateGetStreamingBuffer(luastate);
110  if (b == NULL)
111  return LuaCallbackError(luastate, "internal error: no buffer");
112 
113  return LuaCallbackStreamingBufferPushToStack(luastate, b);
114 }
115 
116 static int LuaCallbackLogPath(lua_State *luastate)
117 {
118  const char *ld = SCConfigGetLogDirectory();
119  if (ld == NULL)
120  return LuaCallbackError(luastate, "internal error: no log dir");
121 
122  return LuaPushStringBuffer(luastate, (const uint8_t *)ld, strlen(ld));
123 }
124 
125 /** \internal
126  * \brief fill lua stack with thread info
127  * \param luastate the lua state
128  * \param pa pointer to packet alert struct
129  * \retval cnt number of data items placed on the stack
130  *
131  * Places: thread id (number), thread name (string, thread group name (string)
132  */
133 static int LuaCallbackThreadInfoPushToStackFromThreadVars(lua_State *luastate, const ThreadVars *tv)
134 {
135  unsigned long tid = SCGetThreadIdLong();
136  lua_pushinteger (luastate, (lua_Integer)tid);
137  lua_pushstring (luastate, tv->name);
138  lua_pushstring (luastate, tv->thread_group_name);
139  return 3;
140 }
141 
142 /** \internal
143  * \brief Wrapper for getting tuple info into a lua script
144  * \retval cnt number of items placed on the stack
145  */
146 static int LuaCallbackThreadInfo(lua_State *luastate)
147 {
148  const ThreadVars *tv = LuaStateGetThreadVars(luastate);
149  if (tv == NULL)
150  return LuaCallbackError(luastate, "internal error: no tv");
151 
152  return LuaCallbackThreadInfoPushToStackFromThreadVars(luastate, tv);
153 }
154 
156 {
157  /* registration of the callbacks */
158  lua_pushcfunction(luastate, LuaCallbackStreamingBuffer);
159  lua_setglobal(luastate, "SCStreamingBuffer");
160 
161  lua_pushcfunction(luastate, LuaCallbackLogPath);
162  lua_setglobal(luastate, "SCLogPath");
163 
164  lua_pushcfunction(luastate, LuaCallbackThreadInfo);
165  lua_setglobal(luastate, "SCThreadInfo");
166  return 0;
167 }
168 
169 int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
170 {
171  AppProto flow_alproto = 0;
172  Flow *flow = LuaStateGetFlow(luastate);
173  if (flow == NULL)
174  return LuaCallbackError(luastate, "internal error: no flow");
175 
176  flow_alproto = flow->alproto;
177 
178  return (alproto == flow_alproto);
179 }
len
uint8_t len
Definition: app-layer-dnp3.h:2
LuaStateGetStreamingBuffer
LuaStreamingBuffer * LuaStateGetStreamingBuffer(lua_State *luastate)
Definition: util-lua.c:254
ThreadVars_::name
char name[16]
Definition: threadvars.h:65
util-lua-common.h
LuaPushTableKeyValueInt
void LuaPushTableKeyValueInt(lua_State *luastate, const char *key, int value)
Definition: util-lua-common.c:59
LuaStreamingBuffer_
Definition: util-lua.h:34
util-lua.h
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:86
LuaCallbackError
int LuaCallbackError(lua_State *luastate, const char *msg)
Definition: util-lua-common.c:39
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
LuaGetStringArgument
const char * LuaGetStringArgument(lua_State *luastate, int idx)
Definition: util-lua-common.c:46
lua_State
struct lua_State lua_State
Definition: suricata-common.h:523
LuaStateGetThreadVars
ThreadVars * LuaStateGetThreadVars(lua_State *luastate)
get tv pointer from the lua state
Definition: util-lua.c:102
ThreadVars_
Per thread variable structure.
Definition: threadvars.h:58
SCConfigGetLogDirectory
const char * SCConfigGetLogDirectory(void)
Definition: util-conf.c:38
ThreadVars_::thread_group_name
char * thread_group_name
Definition: threadvars.h:67
SCGetThreadIdLong
#define SCGetThreadIdLong(...)
Definition: threads.h:255
LuaPushTableKeyValueArray
void LuaPushTableKeyValueArray(lua_State *luastate, const char *key, const uint8_t *value, size_t len)
Definition: util-lua-common.c:77
util-conf.h
suricata-common.h
OUTPUT_STREAMING_FLAG_OPEN
#define OUTPUT_STREAMING_FLAG_OPEN
Definition: output-streaming.h:29
LuaStreamingBuffer_::data_len
uint32_t data_len
Definition: util-lua.h:36
LuaStreamingBuffer_::data
const uint8_t * data
Definition: util-lua.h:35
tv
ThreadVars * tv
Definition: fuzz_decodepcapfile.c:32
threadvars.h
OUTPUT_STREAMING_FLAG_TOSERVER
#define OUTPUT_STREAMING_FLAG_TOSERVER
Definition: output-streaming.h:31
str
#define str(s)
Definition: suricata-common.h:308
LuaStateGetFlow
Flow * LuaStateGetFlow(lua_State *luastate)
get flow pointer from lua state
Definition: util-lua.c:161
OUTPUT_STREAMING_FLAG_CLOSE
#define OUTPUT_STREAMING_FLAG_CLOSE
Definition: output-streaming.h:30
OUTPUT_STREAMING_FLAG_TOCLIENT
#define OUTPUT_STREAMING_FLAG_TOCLIENT
Definition: output-streaming.h:32
LuaPushTableKeyValueString
void LuaPushTableKeyValueString(lua_State *luastate, const char *key, const char *value)
Push a key plus string value to the stack.
Definition: util-lua-common.c:70
Flow_::alproto
AppProto alproto
application level protocol
Definition: flow.h:450
LuaRegisterFunctions
int LuaRegisterFunctions(lua_State *luastate)
Definition: util-lua-common.c:155
LuaStateNeedProto
int LuaStateNeedProto(lua_State *luastate, AppProto alproto)
Definition: util-lua-common.c:169
LuaStreamingBuffer_::flags
uint8_t flags
Definition: util-lua.h:37
output.h
LuaPushStringBuffer
int LuaPushStringBuffer(lua_State *luastate, const uint8_t *input, size_t input_len)
Definition: util-lua.c:319