aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_cfg_file.c
blob: 6ec48ebef0d2c9aae6a31ffe0999bfcc2980e6a0 (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
/*
 * 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"
#ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif

/* 
 * This is implementation of lua routines to handle config file params 
 */


/* Check element with specified name in list, and append it to list if no element with such name was found */
static void
lua_check_element (memory_pool_t *pool, const gchar *name, GList **options, struct module_opt **opt) 
{
	struct module_opt                   *cur;
	GList                               *cur_opt;
	gboolean                             found = FALSE;

	cur_opt = *options;

	while (cur_opt) {
		cur = cur_opt->data;

		if (g_ascii_strcasecmp (cur->param, name) == 0) {
			found = TRUE;
			break;
		}
		cur_opt = g_list_next (cur_opt);
	}
	
	if (found) {
		*opt = cur;
		cur->is_lua = TRUE;
	}
	else {
		/* New option */
		*opt = memory_pool_alloc0 (pool, sizeof (struct module_opt));
		(*opt)->is_lua = TRUE;
		(*opt)->param = memory_pool_strdup (pool, name);
		*options = g_list_prepend (*options, *opt);
	}
}

/* Process a single item in 'config' table */
static void
lua_process_module (lua_State *L, const gchar *param, struct config_file *cfg)
{
	GList                               *cur_opt;
	struct module_opt                   *cur;
	const gchar                          *name;
	gboolean                             new_module = FALSE;

	/* Get module opt structure */
	if ((cur_opt = g_hash_table_lookup (cfg->modules_opts, param)) == NULL) {
		new_module = TRUE;
	}

	/* Now iterate throught module table */
	for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
		/* key - -2, value - -1 */
		name = luaL_checkstring (L, -2);
		if (name != NULL) {
			lua_check_element (cfg->cfg_pool, name, &cur_opt, &cur);
			lua_process_element (cfg, name, cur, -1);
			g_hash_table_insert (cfg->modules_opts, (gpointer)param, cur_opt);
		}
	}
	
	if (new_module && cur_opt != NULL) {
		/* Insert new list into a hash */
		g_hash_table_insert (cfg->modules_opts, memory_pool_strdup (cfg->cfg_pool, param), cur_opt);
	}
}

/* Process a single item in 'metrics' table */
static void
lua_process_metric (lua_State *L, const gchar *name, struct config_file *cfg)
{
	GList                               *metric_list;
	gchar                               *symbol;
	struct metric                       *metric;
	gdouble                             *score;

	/* Get module opt structure */
	if ((metric = g_hash_table_lookup (cfg->metrics, name)) == NULL) {
		metric = check_metric_conf (cfg, metric);
		metric->name = memory_pool_strdup (cfg->cfg_pool, name);
	}

	/* Now iterate throught module table */
	for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
		/* key - -2, value - -1 */
		symbol = memory_pool_strdup (cfg->cfg_pool, luaL_checkstring (L, -2));
		if (symbol != NULL) {
			if (lua_istable (L, -1)) {
				/* We got a table, so extract individual attributes */
				lua_pushstring (L, "weight");
				lua_gettable (L, -1);
				if (lua_isnumber (L, -1)) {
					score = memory_pool_alloc (cfg->cfg_pool, sizeof (double));
					*score = lua_tonumber (L, -1);
				}
				else {
					msg_warn ("cannot get weight of symbol: %s", symbol);
					continue;
				}
			}
			else if (lua_isnumber (L, -1)) {
				/* Just got weight */
				score = memory_pool_alloc (cfg->cfg_pool, sizeof (double));
				*score = lua_tonumber (L, -1);
			}
			else {
				msg_warn ("cannot get weight of symbol: %s", symbol);
				continue;
			}
			/* Insert symbol */
			g_hash_table_insert (metric->symbols, symbol, score);

			if ((metric_list = g_hash_table_lookup (cfg->metrics_symbols, symbol)) == NULL) {
				metric_list = g_list_prepend (NULL, metric);
				memory_pool_add_destructor (cfg->cfg_pool, (pool_destruct_func)g_list_free, metric_list);
				g_hash_table_insert (cfg->metrics_symbols, symbol, metric_list);
			}
			else {
				/* Slow but keep start element of list in safe */
				metric_list = g_list_append (metric_list, metric);
			}
		}
	}
}

/* Process single element */
void
lua_process_element (struct config_file *cfg, const gchar *name, struct module_opt *opt, gint idx) 
{
	lua_State                            *L = cfg->lua_state;
	gint                            t;
	double                               *num;
	gboolean                             *flag;
	
	t = lua_type (L, idx);
	/* Handle type */
	switch (t) {
		case LUA_TNUMBER:
			opt->actual_data = memory_pool_alloc (cfg->cfg_pool, sizeof (double));
			num = (double *)opt->actual_data;
			*num = lua_tonumber (L, idx);
			opt->lua_type = LUA_VAR_NUM;
			break;
		case LUA_TBOOLEAN: 
			opt->actual_data = memory_pool_alloc (cfg->cfg_pool, sizeof (gboolean));
			flag = (gboolean *)opt->actual_data;
			*flag = lua_toboolean (L, idx);
			opt->lua_type = LUA_VAR_BOOLEAN;
			break;
		case LUA_TSTRING: 
			opt->actual_data = memory_pool_strdup (cfg->cfg_pool, lua_tostring (L, idx));
			opt->lua_type = LUA_VAR_STRING;
			break;
		case LUA_TFUNCTION:
			opt->actual_data = memory_pool_strdup (cfg->cfg_pool, name);
			opt->lua_type = LUA_VAR_FUNCTION;
			break;
		case LUA_TNIL:
		case LUA_TTABLE: 
		case LUA_TUSERDATA:
		case LUA_TTHREAD:
		case LUA_TLIGHTUSERDATA:
			msg_warn ("cannot handle variables of type %s as there is nothing to do with them", lua_typename (L, t));
			opt->lua_type = LUA_VAR_UNKNOWN;
			break;
	}
}


static void
lua_module_callback (gpointer key, gpointer value, gpointer ud)
{
	struct config_file                  *cfg = ud;
	lua_State                           *L = cfg->lua_state;
	GList                               *cur;
	struct module_opt                   *opt;

	cur = value;
	while (cur) {
		opt = cur->data;
		if (opt->is_lua && opt->actual_data == NULL) {
			/* Try to extract variable name from config table first */
			lua_getglobal (L, "config");
			if (lua_istable (L, -1)) {
				lua_pushstring (L, opt->param);
				lua_gettable (L, -2);
				if (lua_isnil (L, -1)) {
					/* Try to get global variable */
					lua_getglobal (L, opt->param);
				}
			}
			else {
				/* Try to get global variable */
				lua_getglobal (L, opt->param);
			}
			lua_process_element (cfg, opt->param, opt, -1);
		}
		cur = g_list_next (cur);
	}

}

/* Do post load initialization based on lua */
void
lua_post_load_config (struct config_file *cfg)
{
	lua_State                            *L = cfg->lua_state;
	const gchar                          *name;

	/* First check all module options that may be overriden in 'config' global */
	lua_getglobal (L, "config");

	if (lua_istable (L, -1)) {
		/* Iterate */
		for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
			/* 'key' is at index -2 and 'value' is at index -1 */
			/* Key must be a string and value must be a table */
			name = luaL_checkstring (L, -2);
			if (name != NULL && lua_istable (L, -1)) {
				lua_process_module (L, name, cfg);
			}
		}
	}

	/* First check all module options that may be overriden in 'config' global */
	lua_getglobal (L, "metrics");

	if (lua_istable (L, -1)) {
		/* Iterate */
		for (lua_pushnil(L); lua_next(L, -2); lua_pop(L, 1)) {
			/* 'key' is at index -2 and 'value' is at index -1 */
			/* Key must be a string and value must be a table */
			name = luaL_checkstring (L, -2);
			if (name != NULL && lua_istable (L, -1)) {
				lua_process_metric (L, name, cfg);
			}
		}
	}

	/* Now parse all lua params */
	g_hash_table_foreach (cfg->modules_opts, lua_module_callback, cfg);
}

/* Handle lua dynamic config param */
gboolean
lua_handle_param (struct worker_task *task, gchar *mname, gchar *optname, enum lua_var_type expected_type, gpointer *res)
{
	lua_State                            *L = task->cfg->lua_state;
	GList                                *cur;
	struct module_opt                    *opt;
	struct worker_task                  **ptask;
	double                                num_res;
	gboolean                              bool_res;
	gchar                                *str_res;

	if ((cur = g_hash_table_lookup (task->cfg->modules_opts, mname)) == NULL) {
		*res = NULL;
		return FALSE;
	}
	
	/* Search for specified option */
	while (cur) {
		opt = cur->data;
		if (opt->is_lua && g_ascii_strcasecmp (opt->param, optname) == 0) {
			if (opt->lua_type == expected_type) {
				/* Just push pointer to res */
				*res = opt->actual_data;
				return TRUE;
			}
			else if (opt->lua_type == LUA_VAR_FUNCTION) {
				/* Call specified function and expect result of given expected_type */
				/* First check function in config table */
				lua_getglobal (L, "config");
				if (lua_istable (L, -1)) {
					lua_pushstring (L, mname);
					lua_gettable (L, -2);
					if (lua_isnil (L, -1)) {
						/* Try to get global variable */
						lua_getglobal (L, opt->actual_data);
					}
					else {
						/* Call local function in table */
						lua_pushstring (L, opt->actual_data);
						lua_gettable (L, -2);
					}
				}
				else {
					/* Try to get global variable */
					lua_getglobal (L, opt->actual_data);
				}
				if (lua_isnil (L, -1)) {
					msg_err ("function with name %s is not defined", (gchar *)opt->actual_data);
					return FALSE;
				}
				/* Now we got function in top of stack */
				ptask = lua_newuserdata (L, sizeof (struct worker_task *));
				lua_setclass (L, "rspamd{task}", -1);
				*ptask = task;
				/* Call function */
				if (lua_pcall (L, 1, 1, 0) != 0) {
					msg_info ("call to %s failed: %s", (gchar *)opt->actual_data, lua_tostring (L, -1));
					*res = NULL;
					return FALSE;
				}
				/* Get result of specified type */
				switch (expected_type) {
					case LUA_VAR_NUM:
						if (!lua_isnumber (L, -1)) {
							*res = NULL;
							return FALSE;
						}
						num_res = lua_tonumber (L, -1);
						*res = memory_pool_alloc (task->task_pool, sizeof (double));
						**(double **)res = num_res;
						return TRUE;
					case LUA_VAR_BOOLEAN:
						if (!lua_isboolean (L, -1)) {
							*res = NULL;
							return FALSE;
						}
						bool_res = lua_toboolean (L, -1);
						*res = memory_pool_alloc (task->task_pool, sizeof (gboolean));
						**(gboolean **)res = bool_res;
						return TRUE;
					case LUA_VAR_STRING:
						if (!lua_isstring (L, -1)) {
							*res = NULL;
							return FALSE;
						}
						str_res = memory_pool_strdup (task->task_pool, lua_tostring (L, -1));
						*res = str_res;
						return TRUE;
					case LUA_VAR_FUNCTION:
					case LUA_VAR_UNKNOWN:
						msg_err ("cannot expect function or unknown types");
						*res = NULL;
						return FALSE;
				}
			}
		}
		cur = g_list_next (cur);
	}

	/* Option not found */
	*res = NULL;
	return FALSE;
}

#define FAKE_RES_VAR "rspamd_res"
gboolean
lua_check_condition (struct config_file *cfg, const gchar *condition)
{
	lua_State                            *L = cfg->lua_state;
	gchar                                *hostbuf, *condbuf;
	gsize                                 hostlen;
	gboolean                              res;
#ifdef HAVE_SYS_UTSNAME_H
	struct utsname                        uts;
#endif

	/* Set some globals for condition */
	/* XXX: think what other variables can be useful */
	hostlen = sysconf (_SC_HOST_NAME_MAX) + 1;
	hostbuf = alloca (hostlen);
	gethostname (hostbuf, hostlen);
	hostbuf[hostlen - 1] = '\0';

	/* Hostname */
	lua_pushstring (L, hostbuf);
	lua_setglobal (L, "hostname");
	/* Config file name */
	lua_pushstring (L, cfg->cfg_name);
	lua_setglobal (L, "cfg_name");
	/* Check for uname */
#ifdef HAVE_SYS_UTSNAME_H
	uname (&uts);
	lua_pushstring (L, uts.sysname);
	lua_setglobal (L, "osname");
	lua_pushstring (L, uts.release);
	lua_setglobal (L, "osrelease");
#else
	lua_pushstring (L, "unknown");
	lua_setglobal (L, "osname");
	lua_pushstring (L, "");
	lua_setglobal (L, "osrelease");
#endif
	/* Make fake string */
	hostlen = sizeof (FAKE_RES_VAR "=") + strlen (condition);
	condbuf = g_malloc (hostlen);
	rspamd_strlcpy (condbuf, FAKE_RES_VAR "=", sizeof (FAKE_RES_VAR "="));
	g_strlcat (condbuf, condition, hostlen);
	/* Evaluate condition */
	if (luaL_dostring (L, condbuf) != 0) {
		msg_err ("eval of '%s' failed: '%s'", condition, lua_tostring (L, -1));
		g_free (condbuf);
		return FALSE;
	}
	/* Get global variable res to get result */
	lua_getglobal (L, FAKE_RES_VAR);
	if (! lua_isboolean (L, -1)) {
		msg_err ("bad string evaluated: %s, type: %s", condbuf, lua_typename (L, lua_type (L, -1)));
		g_free (condbuf);
			return FALSE;
	}

	res = lua_toboolean (L, -1);
	g_free (condbuf);

	return res;
}