aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_config.c
blob: 92eaf0b2051e9d7f2573566ff4b9aa8e49fc00ce (plain)
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
/*
 * Copyright (c) 2009, Rambler media
 * 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 BY Rambler media ''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 Rambler 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 "../expressions.h"
#include "../map.h"
#include "../message.h"
#include "../radix.h"
#include "../trie.h"
#include "../classifiers/classifiers.h"

/* Config file methods */
LUA_FUNCTION_DEF (config, get_module_opt);
LUA_FUNCTION_DEF (config, get_all_opt);
LUA_FUNCTION_DEF (config, register_function);
LUA_FUNCTION_DEF (config, add_radix_map);
LUA_FUNCTION_DEF (config, add_hash_map);
LUA_FUNCTION_DEF (config, get_classifier);
LUA_FUNCTION_DEF (config, register_symbol);
LUA_FUNCTION_DEF (config, register_post_filter);

static const struct luaL_reg    configlib_m[] = {
	LUA_INTERFACE_DEF (config, get_module_opt),
	LUA_INTERFACE_DEF (config, get_all_opt),
	LUA_INTERFACE_DEF (config, register_function),
	LUA_INTERFACE_DEF (config, add_radix_map),
	LUA_INTERFACE_DEF (config, add_hash_map),
	LUA_INTERFACE_DEF (config, get_classifier),
	LUA_INTERFACE_DEF (config, register_symbol),
	LUA_INTERFACE_DEF (config, register_post_filter),
	{"__tostring", lua_class_tostring},
	{NULL, NULL}
};


/* Radix tree */
LUA_FUNCTION_DEF (radix, get_key);

static const struct luaL_reg    radixlib_m[] = {
	LUA_INTERFACE_DEF (radix, get_key),
	{"__tostring", lua_class_tostring},
	{NULL, NULL}
};

/* Hash table */
LUA_FUNCTION_DEF (hash_table, get_key);

static const struct luaL_reg    hashlib_m[] = {
	LUA_INTERFACE_DEF (hash_table, get_key),
	{"__tostring", lua_class_tostring},
	{NULL, NULL}
};

/* Suffix trie */
LUA_FUNCTION_DEF (trie, create);
LUA_FUNCTION_DEF (trie, add_pattern);
LUA_FUNCTION_DEF (trie, search_text);
LUA_FUNCTION_DEF (trie, search_task);

static const struct luaL_reg    trielib_m[] = {
	LUA_INTERFACE_DEF (trie, create),
	LUA_INTERFACE_DEF (trie, add_pattern),
	LUA_INTERFACE_DEF (trie, search_task),
	LUA_INTERFACE_DEF (trie, search_task),
	{"__tostring", lua_class_tostring},
	{NULL, NULL}
};

static struct config_file      *
lua_check_config (lua_State * L)
{
	void                           *ud = luaL_checkudata (L, 1, "rspamd{config}");
	luaL_argcheck (L, ud != NULL, 1, "'config' expected");
	return *((struct config_file **)ud);
}

static radix_tree_t           *
lua_check_radix (lua_State * L)
{
	void                           *ud = luaL_checkudata (L, 1, "rspamd{radix}");
	luaL_argcheck (L, ud != NULL, 1, "'radix' expected");
	return **((radix_tree_t ***)ud);
}

static GHashTable           *
lua_check_hash_table (lua_State * L)
{
	void                           *ud = luaL_checkudata (L, 1, "rspamd{hash_table}");
	luaL_argcheck (L, ud != NULL, 1, "'hash_table' expected");
	return **((GHashTable ***)ud);
}

static rspamd_trie_t          *
lua_check_trie (lua_State * L)
{
	void                           *ud = luaL_checkudata (L, 1, "rspamd{trie}");
	luaL_argcheck (L, ud != NULL, 1, "'trie' expected");
	return **((rspamd_trie_t ***)ud);
}

/*** Config functions ***/
static gint
lua_config_get_module_opt (lua_State * L)
{
	struct config_file             *cfg = lua_check_config (L);
	const gchar                     *mname, *optname, *val;

	if (cfg) {
		mname = luaL_checkstring (L, 2);
		optname = luaL_checkstring (L, 3);

		if (mname && optname) {
			val = get_module_opt (cfg, (gchar *)mname, (gchar *)optname);
			if (val) {
				lua_pushstring (L, val);
				return 1;
			}
		}
	}
	lua_pushnil (L);
	return 1;
}

static gint
opt_compare (gconstpointer a, gconstpointer b)
{
	const struct module_opt        *o1 = a,
								   *o2 = b;
	
	return g_ascii_strcasecmp (o1->param, o2->param);
}

static gint
lua_config_get_all_opt (lua_State * L)
{
	struct config_file             *cfg = lua_check_config (L);
	const gchar                     *mname;
	GList                          *cur_opt, *next_opt;
	struct module_opt              *opt, *tmp;
	gint                            i;

	if (cfg) {
		mname = luaL_checkstring (L, 2);

		if (mname) {
			cur_opt = g_hash_table_lookup (cfg->modules_opts, mname);
			if (cur_opt == NULL) {
				lua_pushnil (L);
				return 1;
			}
			/* Sort options in alphabet order by param name */
			cur_opt = g_list_sort (cur_opt, opt_compare);
			g_hash_table_insert (cfg->modules_opts, (gpointer)mname, cur_opt);

			lua_newtable (L);
			while (cur_opt) {
				opt = cur_opt->data;
				next_opt = g_list_next (cur_opt);
				if (next_opt) {
					tmp = next_opt->data;
					if (g_ascii_strcasecmp (tmp->param, opt->param) == 0) {
						/* We have some common values */
						lua_pushstring (L, opt->param);
						lua_newtable (L);
						/* Now stack looks like:
						 * table - parent associated table of options
						 * key - string key of this option
						 * table - array of values, beginig from 1
						 */
						
						for (i = 1; ; i++) {
							lua_pushinteger (L, i);
							lua_pushstring (L, opt->value);
							lua_settable (L, -3);

							cur_opt = g_list_next (cur_opt);
							if (!cur_opt) {
								break;
							}
							tmp = cur_opt->data;
							if (g_ascii_strcasecmp (tmp->param, opt->param) != 0) {
								break;
							}
							opt = tmp;
						}
						/* Now set index in parent table */
						lua_settable (L, -3);
						/* Now continue in outter cycle */
						continue;
					}
					else {
						lua_set_table_index (L, opt->param, opt->value);
					}
				}
				else {
					lua_set_table_index (L, opt->param, opt->value);
				}
				cur_opt = next_opt;
			}
			return 1;
		}
	}
	lua_pushnil (L);
	return 1;
}


static gint
lua_config_get_classifier (lua_State * L)
{
	struct config_file             *cfg = lua_check_config (L);
	struct classifier_config       *clc = NULL, **pclc = NULL;
	const gchar                     *name;
	GList                          *cur;

	if (cfg) {
		name = luaL_checkstring (L, 2);

		cur = g_list_first (cfg->classifiers);
		while (cur) {
			clc = cur->data;
			if (g_ascii_strcasecmp (clc->classifier->name, name) == 0) {
				pclc = &clc;
				break;
			}
			cur = g_list_next (cur);
		}
		if (pclc) {
			pclc = lua_newuserdata (L, sizeof (struct classifier_config *));
			lua_setclass (L, "rspamd{classifier}", -1);
			*pclc = clc;
			return 1;
		}
	}

	lua_pushnil (L);
	return 1;

}

struct lua_callback_data {
	const gchar                     *name;
	lua_State                      *L;
};

static gboolean
lua_config_function_callback (struct worker_task *task, GList *args, void *user_data)
{
	struct lua_callback_data       *cd = user_data;
	struct worker_task            **ptask;
	gint                            i = 1;
	struct expression_argument     *arg;
	GList                          *cur;
	gboolean                        res = FALSE;

	lua_getglobal (cd->L, cd->name);
	ptask = lua_newuserdata (cd->L, sizeof (struct worker_task *));
	lua_setclass (cd->L, "rspamd{task}", -1);
	*ptask = task;
	/* Now push all arguments */
	cur = args;
	while (cur) {
		arg = get_function_arg (cur->data, task, TRUE);
		lua_pushstring (cd->L, (const gchar *)arg->data);
		cur = g_list_next (cur);
		i ++;
	}

	if (lua_pcall (cd->L, i, 1, 0) != 0) {
		msg_warn ("error running function %s: %s", cd->name, lua_tostring (cd->L, -1));
	}
	else {
		if (lua_isboolean (cd->L, 1)) {
			res = lua_toboolean (cd->L, 1);
		}
	}

	return res;
}

static gint
lua_config_register_function (lua_State *L)
{
	struct config_file             *cfg = lua_check_config (L);
	const gchar                     *name, *callback;
	struct lua_callback_data       *cd;
	
	if (cfg) {
		name = g_strdup (luaL_checkstring (L, 2));
	
		callback = luaL_checkstring (L, 3);
		if (name) {
			cd = g_malloc (sizeof (struct lua_callback_data));
			cd->name = g_strdup (callback);
			cd->L = L;
			register_expression_function (name, lua_config_function_callback, cd);
		}
	}
	return 1;
}

void
lua_call_post_filters (struct worker_task *task)
{
	struct lua_callback_data       *cd;
	struct worker_task            **ptask;
	GList                          *cur;

	cur = task->cfg->post_filters;
	while (cur) {
		cd = cur->data;
		lua_getglobal (cd->L, cd->name);
		ptask = lua_newuserdata (cd->L, sizeof (struct worker_task *));
		lua_setclass (cd->L, "rspamd{task}", -1);
		*ptask = task;

		if (lua_pcall (cd->L, 1, 1, 0) != 0) {
			msg_warn ("error running function %s: %s", cd->name, lua_tostring (cd->L, -1));
		}
		cur = g_list_next (cur);
	}
}

static gint
lua_config_register_post_filter (lua_State *L)
{
	struct config_file             *cfg = lua_check_config (L);
	const gchar                     *callback;
	struct lua_callback_data       *cd;

	if (cfg) {

		callback = luaL_checkstring (L, 2);
		if (callback) {
			cd = g_malloc (sizeof (struct lua_callback_data));
			cd->name = g_strdup (callback);
			cd->L = L;
			cfg->post_filters = g_list_prepend (cfg->post_filters, cd);
		}
	}
	return 1;
}

static gint
lua_config_add_radix_map (lua_State *L)
{
	struct config_file             *cfg = lua_check_config (L);
	const gchar                     *map_line;
	radix_tree_t                   **r, ***ud;

	if (cfg) {
		map_line = luaL_checkstring (L, 2);
		r = g_malloc (sizeof (radix_tree_t *));
		*r = radix_tree_create ();
		if (!add_map (map_line, read_radix_list, fin_radix_list, (void **)r)) {
			msg_warn ("invalid radix map %s", map_line);
			radix_tree_free (*r);
			g_free (r);
			lua_pushnil (L);
			return 1;
		}
		ud = lua_newuserdata (L, sizeof (radix_tree_t *));
		*ud = r;
		lua_setclass (L, "rspamd{radix}", -1);

		return 1;
	}

	lua_pushnil (L);
	return 1;

}

static gint
lua_config_add_hash_map (lua_State *L)
{
	struct config_file             *cfg = lua_check_config (L);
	const gchar                     *map_line;
	GHashTable                    **r, ***ud;

	if (cfg) {
		map_line = luaL_checkstring (L, 2);
		r = g_malloc (sizeof (GHashTable *));
		*r = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
		if (!add_map (map_line, read_host_list, fin_host_list, (void **)r)) {
			msg_warn ("invalid hash map %s", map_line);
			g_hash_table_destroy (*r);
			g_free (r);
			lua_pushnil (L);
			return 1;
		}
		ud = lua_newuserdata (L, sizeof (GHashTable *));
		*ud = r;
		lua_setclass (L, "rspamd{hash_table}", -1);

		return 1;
	}

	lua_pushnil (L);
	return 1;

}

/*** Metric functions ***/


static void
lua_metric_symbol_callback (struct worker_task *task, gpointer ud)
{
	struct lua_callback_data       *cd = ud;
	struct worker_task            **ptask;

	lua_getglobal (cd->L, cd->name);
	ptask = lua_newuserdata (cd->L, sizeof (struct worker_task *));
	lua_setclass (cd->L, "rspamd{task}", -1);
	*ptask = task;

	if (lua_pcall (cd->L, 1, 1, 0) != 0) {
		msg_warn ("error running function %s: %s", cd->name, lua_tostring (cd->L, -1));
	}
}

static gint
lua_config_register_symbol (lua_State * L)
{
	struct config_file             *cfg = lua_check_config (L);
	const gchar                     *name, *callback;
	double                          weight;
	struct lua_callback_data       *cd;

	if (cfg) {
		name = g_strdup (luaL_checkstring (L, 2));
		weight = luaL_checknumber (L, 3);
		callback = luaL_checkstring (L, 4);
		if (name) {
			cd = g_malloc (sizeof (struct lua_callback_data));
			cd->name = g_strdup (callback);
			cd->L = L;
			register_symbol (&cfg->cache, name, weight, lua_metric_symbol_callback, cd);
		}
	}
	return 1;
}

/* Radix and hash table functions */
static gint
lua_radix_get_key (lua_State * L)
{
	radix_tree_t                  *radix = lua_check_radix (L);
	guint32                         key;

	if (radix) {
		key = luaL_checkint (L, 2);

		if (radix32tree_find (radix, key) != RADIX_NO_VALUE) {
			lua_pushboolean (L, 1);
			return 1;
		}
	}

	lua_pushboolean (L, 0);
	return 1;
}

static gint
lua_hash_table_get_key (lua_State * L)
{
	GHashTable                    *tbl = lua_check_hash_table (L);
	const gchar                    *key;

	if (tbl) {
		key = luaL_checkstring (L, 2);

		if (g_hash_table_lookup (tbl, key) != NULL) {
			lua_pushboolean (L, 1);
			return 1;
		}
	}

	lua_pushboolean (L, 0);
	return 1;
}

/* Trie functions */
static gint
lua_trie_create (lua_State *L)
{
	rspamd_trie_t                 *trie, **ptrie;
	gboolean                       icase = FALSE;

	if (lua_gettop (L) == 1) {
		icase = lua_toboolean (L, 1);
	}

	trie = rspamd_trie_create (icase);

	ptrie = lua_newuserdata (L, sizeof (rspamd_trie_t *));
	lua_setclass (L, "rspamd{trie}", -1);
	*ptrie = trie;

	return 1;
}

static gint
lua_trie_add_pattern (lua_State *L)
{
	rspamd_trie_t                 *trie = lua_check_trie (L);
	const gchar                   *pattern;
	gint                           id;

	if (trie) {
		pattern  = luaL_checkstring (L, 2);
		id = luaL_checknumber (L, 3);

		if (pattern != NULL) {
			rspamd_trie_insert (trie, pattern, id);
			lua_pushboolean (L, 1);
		}
	}

	lua_pushboolean (L, 0);

	return 1;
}

static gint
lua_trie_search_text (lua_State *L)
{
	rspamd_trie_t                 *trie = lua_check_trie (L);
	const gchar                   *text, *pos;
	gint                           id, i = 1;
	gsize                          len;

	if (trie) {
		text = luaL_checkstring (L, 2);
		len = strlen (text);
		if (text) {
			lua_newtable (L);
			pos = text;
			while (pos < text + len && (pos = rspamd_trie_lookup (trie, pos, len, &id)) != NULL) {
				lua_pushinteger (L, i);
				lua_pushinteger (L, id);
				lua_settable (L, -3);
				i ++;
			}
			return 1;
		}
	}

	lua_pushnil (L);
	return 1;
}

static gint
lua_trie_search_task (lua_State *L)
{
	rspamd_trie_t                 *trie = lua_check_trie (L);
	struct worker_task            *task;
	struct mime_text_part         *part;
	GList                         *cur;
	const gchar                   *pos, *end;
	gint                           id, i = 1;
	void                           *ud;

	if (trie) {
		ud = luaL_checkudata (L, 2, "rspamd{task}");
		luaL_argcheck (L, ud != NULL, 1, "'task' expected");
		task = *((struct worker_task **)ud);
		if (task) {
			lua_newtable (L);
			cur = task->text_parts;
			while (cur) {
				part = cur->data;
				if (!part->is_empty && part->content != NULL) {
					pos = (const gchar *)part->content->data;
					end = pos + part->content->len;
					while (pos < end && (pos = rspamd_trie_lookup (trie, pos, part->content->len, &id)) != NULL) {
						lua_pushinteger (L, i);
						lua_pushinteger (L, id);
						lua_settable (L, -3);
						i ++;
					}
				}
				cur = g_list_next (cur);
			}
			return 1;
		}
	}

	lua_pushnil (L);
	return 1;
}
/* Init functions */

gint
luaopen_config (lua_State * L)
{
	lua_newclass (L, "rspamd{config}", configlib_m);
	luaL_openlib (L, "rspamd_config", null_reg, 0);

	return 1;
}

gint
luaopen_radix (lua_State * L)
{
	lua_newclass (L, "rspamd{radix}", radixlib_m);
	luaL_openlib (L, "rspamd_radix", null_reg, 0);

	return 1;
}

gint
luaopen_hash_table (lua_State * L)
{
	lua_newclass (L, "rspamd{hash_table}", hashlib_m);
	luaL_openlib (L, "rspamd_hash_table", null_reg, 0);

	return 1;
}

gint
luaopen_trie (lua_State * L)
{
	lua_newclass (L, "rspamd{trie}", trielib_m);
	luaL_openlib (L, "rspamd_trie", null_reg, 0);

	return 1;
}