summaryrefslogtreecommitdiffstats
path: root/privileged-main.lua
blob: 6aa9681a21257ba3828906eb81abce36a76b678f (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
msg = ...

--print("DEBUG: got message:", msg)

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


function ret_error(errmsg)
	io.stderr:write("ERROR: "..tostring(errmsg).."\n")
	return ipcmsg.encode{ status = false, errmsg = errmsg}
end

function ret_success(result)
	return ipcmsg.encode{ status = true, errmsg = "success", result = result}
end

req = ipcmsg.decode(msg)

--print("DEBUG: msg decoded")

-- path must be absolute for production so users cannot load scripts from
-- non secured dirs
modules_path = "./modules/"

if type(req.mod) ~= "string" then
	return ret_error("mod is missing in message or is bad format")
end

if type(req.func) ~= "string" then
	return ret_error("func is missing in message or is wrong format")
end

-- make sure we dont have any path elements in modname so we cannot pass
-- modnames like '../myevilmod'
mfile = modules_path..string.gsub(req.mod, ".*/", "")..".lua"

-- load the module
m = dofile(mfile)
--print("DEBUG: mfile:", mfile)
--print("DEBUG: '"..req.func.."' type:", type(m[req.func]))

-- check that the func we want exists
if type(m[req.func]) ~= "function" then
	ret_error(func..": not a function in '".. mfile .."'")
end

-- TODO: check permissions here
print("Calling '"..req.mod.."."..req.func.."'")

--print("DEBUG: args:", req.args)
-- execute the func and pack the return values into a table
result = { m[req.func](unpack(req.args)) }
--result = { m[func](unpack(req.args or {})) }

--print("DEBUG: result:", result)

return ret_success(result)