summaryrefslogtreecommitdiffstats
path: root/bin/lua-privsep.c
blob: d6d750ccb27cc251216ef533f65782186d99f1ee (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
#include <stdio.h>

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#include "conn.h"
#include "lua-privsep.h"

#ifndef PRIVSEP_PATH
#define PRIVSEP_PATH "./"
#endif

static int traceback (lua_State *L) {
	if (!lua_isstring(L, 1))  /* 'message' not a string? */
		return 1;  /* keep it intact */
	fprintf(stderr, "traceback\n");
	lua_getfield(L, LUA_GLOBALSINDEX, "debug");
	if (!lua_istable(L, -1)) {
		fprintf(stderr, "traceback: debug\n");
		lua_pop(L, 1);
		return 1;
	}

	lua_getfield(L, -1, "traceback");
	if (!lua_isfunction(L, -1)) {
		fprintf(stderr, "traceback: traceback\n");
		lua_pop(L, 2);
		return 1;
	}

	lua_pushvalue(L, 1);  /* pass error message */
	lua_pushinteger(L, 2);  /* skip this function and traceback */
	lua_call(L, 2, 1);  /* call debug.traceback */
	return 1;
}

int call_lua(int fd, const char *msg, size_t msglen)
{
	const char *luamain = PRIVSEP_PATH "privileged-main.lua";
	int traceback_index;
	const char *retbuf;
	size_t retsize;

	lua_State *L = luaL_newstate();
	luaL_openlibs(L);

	lua_pushcfunction(L, traceback);
	traceback_index = lua_gettop(L);

	if (luaL_loadfile(L, luamain))
		return luaL_error(L, "%s", luamain);

	lua_pushlstring(L, msg, msglen);


	if (lua_pcall(L, 1, 1, traceback_index))
		return luaL_error(L, "error");

	if (!lua_isstring(L, -1))
		error(L, "function must return string");
	
	retbuf = lua_tolstring(L, -1, &retsize);
	return write(fd, retbuf, retsize);
}