aboutsummaryrefslogtreecommitdiffstats
path: root/extras/filters/filter-lua/filter_lua.c
blob: c2c8781d1b01a146efbbbf6ba2b6ba7865456c84 (plain) (blame)
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
/*
 * Copyright (c) 2013 Eric Faurot <eric@openbsd.org>
 * Copyright (c) 2014 Emmanuel Vadot <manu@bocal.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>

#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM == 501
#define luaL_Reg luaL_reg
void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
  luaL_checkstack(L, nup+1, "too many upvalues");
  for (; l->name != NULL; l++) {  /* fill the table with given functions */
    int i;
    lua_pushstring(L, l->name);
    for (i = 0; i < nup; i++)  /* copy upvalues to the top */
      lua_pushvalue(L, -(nup + 1));
    lua_pushcclosure(L, l->func, nup);  /* closure with those upvalues */
    lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */
  }
  lua_pop(L, nup);  /* remove upvalues */
}
#define luaL_newlib(L, l) \
  (lua_newtable((L)),luaL_setfuncs((L), (l), 0))
#endif

#include <smtpd-api.h>

#define ID_STR_SZ 20

lua_State *L;

static int
l_filter_accept(lua_State *L)
{
	uint64_t	 id;
	const char	*s_hex_id;

	if (lua_gettop(L) != 1)
		return 0;

	s_hex_id = luaL_checklstring(L, 1, NULL);
	id = strtoumax(s_hex_id, NULL, 16);
	filter_api_accept(id);
	return 0;
}

static int
l_filter_reject(lua_State *L)
{
	uint64_t	id;
	const char	*s_hex_id;
	uint32_t	action;

	if (lua_gettop(L) != 2)
		return 0;

	s_hex_id = luaL_checklstring(L, 1, NULL);
	id = strtoumax(s_hex_id, NULL, 16);
	action = luaL_checkinteger(L, 2);
	switch (action) {
	case FILTER_FAIL:
	case FILTER_CLOSE:
		filter_api_reject(id, action);
		break;
	}

	return 0;
}

static int
l_filter_reject_code(lua_State *L)
{
	uint64_t	id;
	const char	*s_hex_id;
	uint32_t	action;
	uint32_t	code;
	const char	*line;

	if (lua_gettop(L) != 4)
		return 0;

	s_hex_id = luaL_checklstring(L, 1, NULL);
	id = strtoumax(s_hex_id, NULL, 16);
	action = luaL_checkinteger(L, 2);
	code = luaL_checkinteger(L, 3);
	line = luaL_checklstring(L, 4, NULL);

	switch (action) {
	case FILTER_FAIL:
	case FILTER_CLOSE:
		filter_api_reject_code(id, action, code, line);
		break;
	}

	return 0;
}

static int
l_filter_writeln(lua_State *L)
{
	uint64_t	id;
	const char	*s_hex_id;
	const char	*line;

	if (lua_gettop(L) != 2)
		return 0;

	s_hex_id = luaL_checklstring(L, 1, NULL);
	id = strtoumax(s_hex_id, NULL, 16);
	line = luaL_checklstring(L, 2, NULL);

	filter_api_writeln(id, line);

	return 0;
}

static const luaL_Reg l_filter [] = {
	{"accept", l_filter_accept},
	{"reject", l_filter_reject},
	{"reject_code", l_filter_reject_code},
	{"writeln", l_filter_writeln},
	{NULL, NULL}
};

static int
on_connect(uint64_t id, struct filter_connect *conn)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);

	lua_getglobal(L, "on_connect");
	lua_pushstring(L, s_id);
	lua_pushstring(L,
	    filter_api_sockaddr_to_text((struct sockaddr *)&conn->local));
	lua_pushstring(L,
	    filter_api_sockaddr_to_text((struct sockaddr *)&conn->remote));
	lua_pushstring(L, conn->hostname);

	if (lua_pcall(L, 4, 0, 0)) {
		log_warnx("warn: on_connect: %s",
		    lua_tostring(L, -1));
		exit(1);
	}

	return 1;
}

static int
on_helo(uint64_t id, const char *helo)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_helo");
	lua_pushstring(L, s_id);
	lua_pushstring(L, helo);

	if (lua_pcall(L, 2, 0, 0)) {
		log_warnx("warn: on_helo: %s",
		    lua_tostring(L, -1));
		exit(1);
	}

	return 1;
}

static int
on_mail(uint64_t id, struct mailaddr *mail)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_mail");
	lua_pushstring(L, s_id);
	lua_pushstring(L, filter_api_mailaddr_to_text(mail));

	if (lua_pcall(L, 2, 0, 0)) {
		log_warnx("warn: on_mail: %s",
		    lua_tostring(L, -1));
		exit(1);
	}

	return 1;
}

static int
on_rcpt(uint64_t id, struct mailaddr *rcpt)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_rcpt");
	lua_pushstring(L, s_id);
	lua_pushstring(L, filter_api_mailaddr_to_text(rcpt));

	if (lua_pcall(L, 2, 0, 0)) {
		log_warnx("warn: on_rcpt: %s",
		    lua_tostring(L, -1));
		exit(1);
	}

	return 1;
}

static int
on_data(uint64_t id)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_data");
	lua_pushstring(L, s_id);

	if (lua_pcall(L, 1, 0, 0)) {
		log_warnx("warn: on_data: %s",
		    lua_tostring(L, -1));
		exit(1);
	}

	return 1;
}

static void
on_dataline(uint64_t id, const char *line)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_dataline");
	lua_pushstring(L, s_id);
	lua_pushstring(L, line);

	if (lua_pcall(L, 2, 0, 0)) {
		log_warnx("warn: on_dataline: %s",
		    lua_tostring(L, -1));
		exit(1);
	}
}

static int
on_eom(uint64_t id, size_t size)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_eom");
	lua_pushstring(L, s_id);

	if (lua_pcall(L, 1, 0, 0)) {
		log_warnx("warn: on_eom: %s", lua_tostring(L, -1));
		exit(1);
	}

	return 1;
}

static void
on_tx_begin(uint64_t id)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_tx_begin");
	lua_pushstring(L, s_id);

	if (lua_pcall(L, 1, 0, 0)) {
		log_warnx("warn: on_tx_begin: %s",
		    lua_tostring(L, -1));
		exit(1);
	}
}

static void
on_tx_commit(uint64_t id)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_tx_commit");
	lua_pushstring(L, s_id);

	if (lua_pcall(L, 1, 0, 0)) {
		log_warnx("warn: on_tx_commit: %s",
		    lua_tostring(L, -1));
		exit(1);
	}
}

static void
on_tx_rollback(uint64_t id)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_tx_rollback");
	lua_pushstring(L, s_id);

	if (lua_pcall(L, 1, 0, 0)) {
		log_warnx("warn: on_tx_rollback: %s",
		    lua_tostring(L, -1));
		exit(1);
	}
}

static void
on_disconnect(uint64_t id)
{
	char	s_id[ID_STR_SZ];

	(void)snprintf(s_id, sizeof(s_id), "%016"PRIx64"", id);
	lua_getglobal(L, "on_disconnect");
	lua_pushstring(L, s_id);

	if (lua_pcall(L, 1, 0, 0)) {
		log_warnx("warn: on_disconnect: %s",
		    lua_tostring(L, -1));
		exit(1);
	}
}

int
main(int argc, char **argv)
{
	int ch, d = 0, v = 0;
	char *c = NULL, *path;

	log_init(1);

	while ((ch = getopt(argc, argv, "c:dv")) != -1) {
		switch (ch) {
		case 'c':
			c = optarg;
			break;
		case 'd':
			d = 1;
			break;
		case 'v':
			v |= TRACE_DEBUG;
			break;
		default:
			log_warnx("warn: bad option");
			return 1;
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;
	if (argc == 0)
		errx(1, "missing path");

	if (c)
		c = strip(c);
	path = argv[0];

	log_init(d);
	log_verbose(v);

	log_debug("debug: starting...");

	if ((L = luaL_newstate()) == NULL) {
		log_warnx("warn: can't create lua state");
		return 1;
	}
	luaL_openlibs(L);
	luaL_newlib(L, l_filter);
#if 0
	luaL_newmetatable(L, "filter");
	lua_setmetatable(L, -2);

	lua_pushnumber(L, FILTER_OK);
	lua_setfield(L, -2, "FILTER_OK");
	lua_pushnumber(L, FILTER_FAIL);
	lua_setfield(L, -2, "FILTER_FAIL");
	lua_pushnumber(L, FILTER_CLOSE);
	lua_setfield(L, -2, "FILTER_CLOSE");
#endif
	lua_setglobal(L, "filter");

	if (luaL_dofile(L, path) != 0) {
		log_warnx("warn: error loading script: %s", path);
		return 1;
	}

	lua_getglobal(L, "on_connect");
	if (lua_isfunction(L, -1)) {
		log_debug("debug: on_connect is present");
		filter_api_on_connect(on_connect);
	}

	lua_getglobal(L, "on_helo");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_helo is present");
		filter_api_on_helo(on_helo);
	}

	lua_getglobal(L, "on_mail");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_mail is present");
		filter_api_on_mail(on_mail);
	}

	lua_getglobal(L, "on_rcpt");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_rcpt is present");
		filter_api_on_rcpt(on_rcpt);
	}

	lua_getglobal(L, "on_data");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_data is present");
		filter_api_on_data(on_data);
	}

	lua_getglobal(L, "on_dataline");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_dataline is present");
		filter_api_on_dataline(on_dataline);
	}

	lua_getglobal(L, "on_eom");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_eom is present");
		filter_api_on_eom(on_eom);
	}

	lua_getglobal(L, "on_tx_begin");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_tx_begin is present");
		filter_api_on_tx_begin(on_tx_begin);
	}

	lua_getglobal(L, "on_tx_commit");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_tx_commit is present");
		filter_api_on_tx_commit(on_tx_commit);
	}

	lua_getglobal(L, "on_tx_rollback");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_tx_rollback is present");
		filter_api_on_tx_rollback(on_tx_rollback);
	}

	lua_getglobal(L, "on_disconnect");
	if (lua_isfunction(L, 1)) {
		log_debug("debug: on_disconnect is present");
		filter_api_on_disconnect(on_disconnect);
	}

	filter_api_no_chroot();
	if (c)
		filter_api_set_chroot(c);

	filter_api_loop();
	log_debug("debug: exiting");

	return 1;
}