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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/* Copyright (c) 2015, Vsevolod Stakhov
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "lua_common.h"
#include "task.h"
#include "main.h"
#include "cfg_rcl.h"
#include "tokenizers/tokenizers.h"
/***
* @function util.create_event_base()
* Creates new event base for processing asynchronous events
* @return {ev_base} new event processing base
*/
LUA_FUNCTION_DEF (util, create_event_base);
/***
* @function util.load_rspamd_config(filename)
* Load rspamd config from the specified file
* @return {confg} new configuration object suitable for access
*/
LUA_FUNCTION_DEF (util, load_rspamd_config);
/***
* @function util.config_from_ucl(any)
* Load rspamd config from ucl reperesented by any lua table
* @return {confg} new configuration object suitable for access
*/
LUA_FUNCTION_DEF (util, config_from_ucl);
/***
* @function util.encode_base64(input[, str_len])
* Encodes data in base64 breaking lines if needed
* @param {text or string} input input data
* @param {number} str_len optional size of lines or 0 if split is not needed
* @return {rspamd_text} encoded data chunk
*/
LUA_FUNCTION_DEF (util, encode_base64);
/***
* @function util.decode_base64(input)
* Decodes data from base64 ignoring whitespace characters
* @param {text or string} input data to decode; if `rspamd{text}` is used then the string is modified **in-place**
* @return {rspamd_text} decoded data chunk
*/
LUA_FUNCTION_DEF (util, decode_base64);
/***
* @function util.tokenize_text(input[, exceptions])
* Create tokens from a text using optional exceptions list
* @param {text/string} input input data
* @param {table} exceptions, a table of pairs containing <start_pos,lenght> of exceptions in the input
* @return {table/strings} list of strings representing words in the text
*/
LUA_FUNCTION_DEF (util, tokenize_text);
LUA_FUNCTION_DEF (util, process_message);
static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, create_event_base),
LUA_INTERFACE_DEF (util, load_rspamd_config),
LUA_INTERFACE_DEF (util, config_from_ucl),
LUA_INTERFACE_DEF (util, process_message),
LUA_INTERFACE_DEF (util, encode_base64),
LUA_INTERFACE_DEF (util, decode_base64),
LUA_INTERFACE_DEF (util, tokenize_text),
{NULL, NULL}
};
static gint
lua_util_create_event_base (lua_State *L)
{
struct event_base **pev_base;
pev_base = lua_newuserdata (L, sizeof (struct event_base *));
rspamd_lua_setclass (L, "rspamd{ev_base}", -1);
*pev_base = event_init ();
return 1;
}
static gint
lua_util_load_rspamd_config (lua_State *L)
{
struct rspamd_config *cfg, **pcfg;
const gchar *cfg_name;
cfg_name = luaL_checkstring (L, 1);
if (cfg_name) {
cfg = g_malloc0 (sizeof (struct rspamd_config));
rspamd_init_cfg (cfg, FALSE);
if (rspamd_config_read (cfg, cfg_name, NULL, NULL, NULL)) {
msg_err ("cannot load config from %s", cfg_name);
lua_pushnil (L);
}
else {
rspamd_config_post_load (cfg);
init_symbols_cache (cfg->cfg_pool, cfg->cache, cfg, NULL, TRUE);
pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
rspamd_lua_setclass (L, "rspamd{config}", -1);
*pcfg = cfg;
}
}
return 1;
}
static gint
lua_util_config_from_ucl (lua_State *L)
{
struct rspamd_config *cfg, **pcfg;
struct rspamd_rcl_section *top;
GError *err = NULL;
ucl_object_t *obj;
obj = ucl_object_lua_import (L, 1);
if (obj) {
cfg = g_malloc0 (sizeof (struct rspamd_config));
rspamd_init_cfg (cfg, FALSE);
cfg->lua_state = L;
cfg->rcl_obj = obj;
top = rspamd_rcl_config_init ();
if (!rspamd_rcl_parse (top, cfg, cfg->cfg_pool, cfg->rcl_obj, &err)) {
msg_err ("rcl parse error: %s", err->message);
ucl_object_unref (obj);
lua_pushnil (L);
}
else {
rspamd_config_post_load (cfg);
init_symbols_cache (cfg->cfg_pool, cfg->cache, cfg, NULL, TRUE);
pcfg = lua_newuserdata (L, sizeof (struct rspamd_config *));
rspamd_lua_setclass (L, "rspamd{config}", -1);
*pcfg = cfg;
}
}
return 1;
}
static gboolean
lua_util_task_fin (struct rspamd_task *task, void *ud)
{
ucl_object_t **target = ud;
*target = rspamd_protocol_write_ucl (task, NULL);
rdns_resolver_release (task->resolver->r);
return TRUE;
}
static gint
lua_util_process_message (lua_State *L)
{
struct rspamd_config *cfg = lua_check_config (L, 1);
const gchar *message;
gsize mlen;
struct rspamd_task *task;
struct event_base *base;
ucl_object_t *res = NULL;
message = luaL_checklstring (L, 2, &mlen);
if (cfg != NULL && message != NULL) {
base = event_init ();
rspamd_init_filters (cfg, FALSE);
task = rspamd_task_new (NULL);
task->cfg = cfg;
task->ev_base = base;
task->msg.start = rspamd_mempool_alloc (task->task_pool, mlen + 1);
rspamd_strlcpy ((gpointer)task->msg.start, message, mlen + 1);
task->msg.len = mlen;
task->fin_callback = lua_util_task_fin;
task->fin_arg = &res;
task->resolver = dns_resolver_init (NULL, base, cfg);
task->s = new_async_session (task->task_pool, rspamd_task_fin,
rspamd_task_restore, rspamd_task_free_hard, task);
if (rspamd_task_process (task, NULL, message, mlen, NULL, TRUE)) {
event_base_loop (base, 0);
if (res != NULL) {
ucl_object_push_lua (L, res, true);
ucl_object_unref (res);
}
else {
ucl_object_push_lua (L, rspamd_protocol_write_ucl (task, NULL),
true);
rdns_resolver_release (task->resolver->r);
rspamd_task_free_hard (task);
}
}
else {
lua_pushnil (L);
}
event_base_free (base);
}
else {
lua_pushnil (L);
}
return 1;
}
static gint
lua_util_encode_base64 (lua_State *L)
{
struct rspamd_lua_text *t;
const gchar *s = NULL;
gchar *out;
gsize inlen, outlen;
guint str_lim = 0;
if (lua_type (L, 1) == LUA_TSTRING) {
s = luaL_checklstring (L, 1, &inlen);
}
else if (lua_type (L, 1) == LUA_TUSERDATA) {
t = lua_check_text (L, 1);
if (t != NULL) {
s = t->start;
inlen = t->len;
}
}
if (lua_gettop (L) > 1) {
str_lim = luaL_checknumber (L, 2);
}
if (s == NULL) {
lua_pushnil (L);
}
else {
out = rspamd_encode_base64 (s, inlen, str_lim, &outlen);
if (out != NULL) {
t = lua_newuserdata (L, sizeof (*t));
rspamd_lua_setclass (L, "rspamd{text}", -1);
t->start = out;
t->len = outlen;
/* Need destruction */
t->own = TRUE;
}
else {
lua_pushnil (L);
}
}
return 1;
}
static gint
lua_util_decode_base64 (lua_State *L)
{
struct rspamd_lua_text *t;
const gchar *s = NULL;
gsize inlen, outlen;
gboolean zero_copy = FALSE, grab_own = FALSE;
gint state = 0;
guint save = 0;
if (lua_type (L, 1) == LUA_TSTRING) {
s = luaL_checklstring (L, 1, &inlen);
}
else if (lua_type (L, 1) == LUA_TUSERDATA) {
t = lua_check_text (L, 1);
if (t != NULL) {
s = t->start;
inlen = t->len;
zero_copy = TRUE;
if (t->own) {
t->own = FALSE;
grab_own = TRUE;
}
}
}
if (s != NULL) {
if (zero_copy) {
/* Decode in place */
outlen = g_base64_decode_step (s, inlen, (guchar *)s, &state, &save);
t = lua_newuserdata (L, sizeof (*t));
rspamd_lua_setclass (L, "rspamd{text}", -1);
t->start = s;
t->len = outlen;
t->own = grab_own;
}
else {
t = lua_newuserdata (L, sizeof (*t));
rspamd_lua_setclass (L, "rspamd{text}", -1);
t->len = (inlen / 4) * 3 + 3;
t->start = g_malloc (t->len);
outlen = g_base64_decode_step (s, inlen, (guchar *)t->start,
&state, &save);
t->len = outlen;
t->own = TRUE;
}
}
else {
lua_pushnil (L);
}
return 1;
}
static gint
lua_util_tokenize_text (lua_State *L)
{
const gchar *in = NULL;
gsize len, pos, ex_len, i;
GList *exceptions = NULL, *cur;
struct rspamd_lua_text *t;
struct process_exception *ex;
GArray *res;
rspamd_fstring_t *w;
gboolean compat = FALSE;
if (lua_type (L, 1) == LUA_TSTRING) {
in = luaL_checklstring (L, 1, &len);
}
else if (lua_type (L, 1) == LUA_TTABLE) {
t = lua_check_text (L, 1);
if (t) {
in = t->start;
len = t->len;
}
}
if (in == NULL) {
lua_pushnil (L);
return 1;
}
if (lua_gettop (L) > 1 && lua_type (L, 2) == LUA_TTABLE) {
lua_pushvalue (L, 2);
lua_pushnil (L);
while (lua_next (L, -2) != 0) {
if (lua_type (L, -1) == LUA_TTABLE) {
lua_rawgeti (L, -1, 1);
pos = luaL_checknumber (L, -1);
lua_pop (L, 1);
lua_rawgeti (L, -1, 2);
ex_len = luaL_checknumber (L, -1);
lua_pop (L, 1);
if (ex_len > 0) {
ex = g_slice_alloc (sizeof (*ex));
ex->pos = pos;
ex->len = ex_len;
exceptions = g_list_prepend (exceptions, ex);
}
}
lua_pop (L, 1);
}
lua_pop (L, 1);
}
if (lua_gettop (L) > 2 && lua_type (L, 3) == LUA_TBOOLEAN) {
compat = lua_toboolean (L, 3);
}
if (exceptions) {
exceptions = g_list_reverse (exceptions);
}
res = rspamd_tokenize_text ((gchar *)in, len, TRUE, 0, exceptions, compat);
if (res == NULL) {
lua_pushnil (L);
}
else {
lua_newtable (L);
for (i = 0; i < res->len; i ++) {
w = &g_array_index (res, rspamd_fstring_t, i);
lua_pushlstring (L, w->begin, w->len);
lua_rawseti (L, -2, i + 1);
}
}
cur = exceptions;
while (cur) {
ex = cur->data;
g_slice_free1 (sizeof (*ex), ex);
cur = g_list_next (cur);
}
g_list_free (exceptions);
return 1;
}
static gint
lua_load_util (lua_State * L)
{
lua_newtable (L);
luaL_register (L, NULL, utillib_f);
return 1;
}
void
luaopen_util (lua_State * L)
{
rspamd_lua_add_preload (L, "rspamd_util", lua_load_util);
}
|