-
Notifications
You must be signed in to change notification settings - Fork 165
/
lua_parser_api.cpp
314 lines (268 loc) · 5.97 KB
/
lua_parser_api.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
Copyright (C) 2021 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <memory>
#include "sinsp.h"
#include "sinsp_int.h"
#include "filterchecks.h"
#include "lua_parser_api.h"
#include "lua_parser.h"
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
extern sinsp_filter_check_list g_filterlist;
// It would be nice to expose this up to Lua so that comparison operator
// parsing/encoding can be done there.
static cmpop string_to_cmpop(const char* str)
{
if(strcmp(str, "=") == 0)
{
return CO_EQ;
}
else if(strcmp(str, "!=") == 0)
{
return CO_NE;
}
else if(strcmp(str, "<=") == 0)
{
return CO_LE;
}
else if(strcmp(str, "<") == 0)
{
return CO_LT;
}
else if(strcmp(str, ">=") == 0)
{
return CO_GE;
}
else if(strcmp(str, ">") == 0)
{
return CO_GT;
}
else if(strcmp(str, "contains") == 0)
{
return CO_CONTAINS;
}
else if(strcmp(str, "icontains") == 0)
{
return CO_ICONTAINS;
}
else if(strcmp(str, "startswith") == 0)
{
return CO_STARTSWITH;
}
else if(strcmp(str, "endswith") == 0)
{
return CO_ENDSWITH;
}
else if(strcmp(str, "in") == 0)
{
return CO_IN;
}
else if(strcmp(str, "intersects") == 0)
{
return CO_INTERSECTS;
}
else if(strcmp(str, "pmatch") == 0)
{
return CO_PMATCH;
}
else if(strcmp(str, "exists") == 0)
{
return CO_EXISTS;
}
else if(strcmp(str, "glob") == 0)
{
return CO_GLOB;
}
else
{
throw sinsp_exception("filter error: invalid comparison operator: " + string(str));
}
}
boolop string_to_boolop(const char* str)
{
if(strcmp(str, "or") == 0)
{
return BO_OR;
}
else if(strcmp(str, "and") == 0)
{
return BO_AND;
}
else if(strcmp(str, "not") == 0)
{
return BO_NOT;
}
else
{
throw sinsp_exception("filter error: invalid boolean operator: " + string(str));
}
}
int lua_parser_cbacks::nest(lua_State *ls)
{
lua_parser* parser = (lua_parser*)lua_topointer(ls, -1);
try {
if (parser->m_have_rel_expr && parser->m_last_boolop == BO_NONE)
{
string err = "filter.nest() called without a preceding call to filter.bool_op()";
throw sinsp_exception(err);
}
parser->filter()->push_expression(parser->m_last_boolop);
parser->m_nest_level++;
parser->m_last_boolop = BO_NONE;
parser->m_have_rel_expr = false;
}
catch (const std::exception& e)
{
lua_pushstring(ls, e.what());
return 1;
}
lua_pushnil(ls);
return 1;
}
int lua_parser_cbacks::unnest(lua_State *ls)
{
lua_parser* parser = (lua_parser*)lua_topointer(ls, -1);
try {
if (parser->m_nest_level < 1)
{
string err = "filter.unnest() called without being nested";
throw sinsp_exception(err);
}
parser->filter()->pop_expression();
parser->m_nest_level--;
}
catch (const std::exception& e)
{
lua_pushstring(ls, e.what());
return 1;
}
lua_pushnil(ls);
return 1;
}
int lua_parser_cbacks::bool_op(lua_State *ls)
{
lua_parser* parser = (lua_parser*)lua_topointer(ls, -2);
try {
const char* opstr = luaL_checkstring(ls, -1);
boolop op = string_to_boolop(opstr);
if (!parser->m_have_rel_expr)
{
if (op == BO_NOT) {
op = (boolop)((uint32_t)parser->m_last_boolop | op);
}
else
{
string err = "filter.bool_op() called without having called rel_expr() ";
throw sinsp_exception(err);
}
}
if (parser->m_last_boolop != BO_NONE)
{
if (op == BO_NOT) {
op = (boolop)((uint32_t)parser->m_last_boolop | op);
}
else
{
string err = "filter.bool_op() called twice in a row";
throw sinsp_exception(err);
}
}
parser->m_last_boolop = op;
}
catch (const std::exception& e)
{
lua_pushstring(ls, e.what());
return 1;
}
lua_pushnil(ls);
return 1;
}
int lua_parser_cbacks::rel_expr(lua_State *ls)
{
lua_parser* parser = (lua_parser*)lua_topointer(ls, 1);
try {
if (parser->m_have_rel_expr && parser->m_last_boolop == BO_NONE)
{
string err = "filter.rel_expr() called twice in a row";
throw sinsp_exception(err);
}
parser->m_have_rel_expr = true;
const char* fld = luaL_checkstring(ls, 2);
gen_event_filter_check *chk = parser->factory()->new_filtercheck(fld);
if(chk == NULL)
{
string err = "filter_check called with nonexistent field " + string(fld);
throw sinsp_exception(err);
}
int i;
int rule_index = 0;
chk->m_boolop = parser->m_last_boolop;
parser->m_last_boolop = BO_NONE;
chk->parse_field_name(fld, true, true);
const char* cmpop = luaL_checkstring(ls, 3);
chk->m_cmpop = string_to_cmpop(cmpop);
// "exists" is the only unary comparison op
if(strcmp(cmpop, "exists"))
{
if (strcmp(cmpop, "in") == 0 ||
strcmp(cmpop, "intersects") == 0 ||
strcmp(cmpop, "pmatch") == 0)
{
if (!lua_istable(ls, 4))
{
string err = "Got non-table as in-expression operand for field " + string(fld);
throw sinsp_exception(err);
}
int n = luaL_getn(ls, 4); /* get size of table */
for (i=1; i<=n; i++)
{
lua_rawgeti(ls, 4, i);
const char* value = luaL_checkstring(ls, 6);
chk->add_filter_value(value, strlen(value), i - 1);
lua_pop(ls, 1);
}
}
else
{
const char* value = luaL_checkstring(ls, 4);
chk->add_filter_value(value, strlen(value));
}
if (lua_isnumber(ls, 5))
{
rule_index = (int) luaL_checkinteger(ls, 5);
}
}
else
{
if (lua_isnumber(ls, 4))
{
rule_index = (int) luaL_checkinteger(ls, 4);
}
}
if (rule_index)
{
chk->set_check_id(rule_index);
}
parser->filter()->add_check(chk);
}
catch (const std::exception& e)
{
lua_pushstring(ls, e.what());
return 1;
}
lua_pushnil(ls);
return 1;
}