summaryrefslogtreecommitdiffstats
path: root/client/privsep.lua
blob: 432f4727be3bfd0b51a2ba2161998d97f1d5f7b5 (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

ipcmsg = require("cmsgpack")
ipcmsg.encode = ipcmsg.pack
ipcmsg.decode = ipcmsg.unpack

client = require("client")

local ml = require("ml")

local modules_path = "./modules"

local privsep = {}

local function call(conn, req)
	local n = client.write(conn.fd, ipcmsg.encode(req))
	retmsg, errmsg = client.recv(conn.fd)
	if retmsg then
		local data = ipcmsg.decode(retmsg)
		return unpack(data.result or {})
	end
	return nil, errmsg
end

local function wrap(conn, modname)
	local mod = dofile(modules_path.."/"..modname..".lua")
	local f = {}
	local i = #conn.mods + 1
	local ret = conn:call{ type = "load", modidx = i,
		                    modname = modname }
	if ret == nil then
		return nil
	end
	for k,v in pairs(mod) do
		f[k] = function(...)
			return conn:call{ type = "call", modidx = i,
					       func = k, args = {...}}
		end
	end
	conn[i] = f
	return f
end

function privsep.connect(token)
	return {
		["fd"] = client.connect(),
		["call"] = call,
		["wrap"] = wrap,
		["close"] = function(self)
				return client.close(self.fd)
			end,
		["mods"] = {},
	}
end


return privsep