File: lua_parser_api.cpp

package info (click to toggle)
sysdig 0.24.1-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,408 kB
  • sloc: cpp: 63,382; ansic: 31,517; sh: 1,100; python: 349; makefile: 49
file content (303 lines) | stat: -rw-r--r-- 6,313 bytes parent folder | download
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
/*
Copyright (C) 2013-2018 Draios Inc dba Sysdig.

This file is part of sysdig.

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 "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.
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, "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_getglobal(ls, "siparser");

	lua_parser* parser = (lua_parser*)lua_touserdata(ls, -1);
	lua_pop(ls, 1);

	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()";
		fprintf(stderr, "%s\n", err.c_str());
		throw sinsp_exception(err);
	}

	sinsp_filter* filter = parser->m_filter;

	filter->push_expression(parser->m_last_boolop);
	parser->m_nest_level++;

	parser->m_last_boolop = BO_NONE;
	parser->m_have_rel_expr = false;

	return 0;
}

int lua_parser_cbacks::unnest(lua_State *ls)
{
	lua_getglobal(ls, "siparser");

	lua_parser* parser = (lua_parser*)lua_touserdata(ls, -1);
	lua_pop(ls, 1);

	if (parser->m_nest_level < 1)
	{
		string err = "filter.unnest() called without being nested";
		fprintf(stderr, "%s\n", err.c_str());
		throw sinsp_exception(err);
	}

	sinsp_filter* filter = parser->m_filter;

	filter->pop_expression();
	parser->m_nest_level--;

	return 0;
}

int lua_parser_cbacks::bool_op(lua_State *ls)
{
	lua_getglobal(ls, "siparser");

	lua_parser* parser = (lua_parser*)lua_touserdata(ls, -1);
	lua_pop(ls, 1);

	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() ";
			fprintf(stderr, "%s\n", err.c_str());
			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";
			fprintf(stderr, "%s\n", err.c_str());
			throw sinsp_exception(err);
		}
	}
	parser->m_last_boolop = op;
	return 0;

}

int lua_parser_cbacks::rel_expr(lua_State *ls)
{
	lua_getglobal(ls, "siparser");

	lua_parser* parser = (lua_parser*)lua_touserdata(ls, -1);
	lua_pop(ls, 1);

	if (parser->m_have_rel_expr && parser->m_last_boolop == BO_NONE)
	{
		string err = "filter.rel_expr() called twice in a row";
		fprintf(stderr, "%s\n", err.c_str());
		throw sinsp_exception(err);
	}

	parser->m_have_rel_expr = true;
	sinsp* inspector = parser->m_inspector;
	sinsp_filter* filter = parser->m_filter;

	const char* fld = luaL_checkstring(ls, 1);
	sinsp_filter_check* chk = g_filterlist.new_filter_check_from_fldname(fld,
									     inspector,
									     true);
	if(chk == NULL)
	{
		string err = "filter_check called with nonexistent field " + string(fld);
		fprintf(stderr, "%s\n", err.c_str());
		throw sinsp_exception("parser API error");
	}

	try
	{
		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, 2);
		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, "pmatch") == 0)
			{
				if (!lua_istable(ls, 3))
				{
					string err = "Got non-table as in-expression operand\n";
					fprintf(stderr, "%s\n", err.c_str());
					throw sinsp_exception("parser API error");
				}
				int n = (int)lua_objlen(ls, 3);  /* get size of table */
				for (i=1; i<=n; i++)
				{
					lua_rawgeti(ls, 3, i);
					const char* value = luaL_checkstring(ls, 5);
					chk->add_filter_value(value, strlen(value), i - 1);
					lua_pop(ls, 1);
				}
			}
			else
			{
				const char* value = luaL_checkstring(ls, 3);
				chk->add_filter_value(value, strlen(value));
			}

			if (lua_isnumber(ls, 4))
			{
				rule_index = (int) luaL_checkinteger(ls, 4);
			}
		}
		else
		{
			if (lua_isnumber(ls, 3))
			{
				rule_index = (int) luaL_checkinteger(ls, 3);
			}
		}

		if (rule_index)
		{
			chk->set_check_id(rule_index);
		}
	}
	catch(sinsp_exception& e)
	{
		fprintf(stderr, "Error in filter.rel_expr() %s\n\n", e.what());
		throw e;
	}

	filter->add_check(chk);

	return 0;
}