sandbox.lua 4.63 KB
Newer Older
1 2
-- Units
kB = 1024
3 4
MB = 1024*kB
GB = 1024*MB
5 6 7 8
-- Time
sec = 1000
minute = 60 * sec
hour = 60 * minute
9
day = 24 * hour
10

11 12
-- Resolver bindings
kres = require('kres')
13
trust_anchors = require('trust_anchors')
14
resolve = worker.resolve
15

16 17 18 19 20 21 22 23 24
-- Function aliases
-- `env.VAR returns os.getenv(VAR)`
env = {}
setmetatable(env, {
	__index = function (t, k) return os.getenv(k) end
})

-- Quick access to interfaces
-- `net.<iface>` => `net.interfaces()[iface]`
25
-- `net = {addr1, ..}` => `net.listen(name, addr1)`
26
-- `net.ipv{4,6} = {true, false}` => enable/disable IPv{4,6}
27 28 29 30
setmetatable(net, {
	__index = function (t, k)
		local v = rawget(t, k)
		if v then return v
31 32
		elseif k == 'ipv6' then return not option('NO_IPV6')
		elseif k == 'ipv4' then return not option('NO_IPV4')
33 34
		else return net.interfaces()[k]
		end
35 36
	end,
	__newindex = function (t,k,v)
37 38 39 40 41 42 43
		if     k == 'ipv6' then return option('NO_IPV6', not v)
		elseif k == 'ipv4' then return option('NO_IPV4', not v)
		else
			local iname = rawget(net.interfaces(), v)
			if iname then t.listen(iname)
			else t.listen(v)
			end
44
		end
45 46 47
	end
})

48 49
-- Syntactic sugar for module loading
-- `modules.<name> = <config>`
50
setmetatable(modules, {
51
	__newindex = function (t,k,v)
52
		if type(k) == 'number' then k = v end
53 54
		if not rawget(_G, k) then
			modules.load(k)
55
			local mod = _G[k]
Marek Vavruša's avatar
Marek Vavruša committed
56 57 58 59
			local config = rawget(mod, 'config')
			if mod and config then
				if k ~= v then config(v)
				else           config()
60
				end
61 62
			end
		end
63 64 65 66 67 68 69
	end
})

-- Syntactic sugar for cache
-- `cache.{size|storage} = value`
setmetatable(cache, {
	__newindex = function (t,k,v)
70 71 72 73 74 75 76 77
		-- Defaults
		local storage = rawget(t, 'current_storage')
		if not storage then storage = 'lmdb://' end
		local size = rawget(t, 'current_size')
		if not size then size = 10*MB end
		-- Declarative interface for cache
		if     k == 'size'    then t.open(v, storage)
		elseif k == 'storage' then t.open(size, v)
78
		else   rawset(t, k, v) end
79
	end
80 81
})

82 83 84 85
-- Syntactic sugar for TA store
setmetatable(trust_anchors, {
	__newindex = function (t,k,v)
	if     k == 'file' then t.config(v)
86
	elseif k == 'negative' then t.set_insecure(v)
87 88 89 90
	else   rawset(t, k, v) end
	end,
})

91 92 93 94 95 96 97
-- Register module in Lua environment
function modules_register(module)
	-- Syntactic sugar for get() and set() properties
	setmetatable(module, {
		__index = function (t, k)
			local  v = rawget(t, k)
			if     v     then return v
98
			elseif rawget(t, 'get') then return t.get(k)
99 100 101 102
			end
		end,
		__newindex = function (t, k, v)
			local  old_v = rawget(t, k)
103
			if not old_v and rawget(t, 'set') then
104 105 106 107 108 109
				t.set(k..' '..v)
			end
		end
	})
end

110
-- Make sandboxed environment
111
local function make_sandbox(defined)
112
	local __protected = { modules = true, cache = true, net = true, trust_anchors = true }
113 114 115 116 117 118 119 120 121 122 123 124 125
	return setmetatable({}, {
		__index = defined,
		__newindex = function (t, k, v)
			if __protected[k] then
				for k2,v2 in pairs(v) do
					defined[k][k2] = v2
				end
			else
				defined[k] = v
			end
		end
	})
end
126

127
-- Compatibility sandbox
128 129 130 131 132 133
if setfenv then -- Lua 5.1 and less
	_G = make_sandbox(getfenv(0))
	setfenv(0, _G)
else -- Lua 5.2+
	_SANDBOX = make_sandbox(_ENV)
end
134

135 136 137 138 139 140 141 142 143 144 145
-- Interactive command evaluation
function eval_cmd(line)
	-- Compatibility sandbox code loading
	local function load_code(code)
	    if getfenv then -- Lua 5.1
	        return loadstring(code)
	    else            -- Lua 5.2+
	        return load(code, nil, 't', _ENV)
	    end
	end
	local status, err, chunk
146
	chunk, err = load_code('return table_print('..line..')')
147 148 149 150
	if err then
		chunk, err = load_code(line)
	end
	if not err then
151 152 153
		return chunk()
	else
		error(err)
154 155 156
	end
end

157 158 159 160
-- Pretty printing
function table_print (tt, indent, done)
	done = done or {}
	indent = indent or 0
161
	result = ""
162 163 164 165 166 167 168 169 170 171 172 173 174
	-- Convert to printable string (escape unprintable)
	local function printable(value)
		value = tostring(value)
		local bytes = {}
		for i = 1, #value do
			local c = string.byte(value, i)
			if c >= 0x20 and c < 0x7f then table.insert(bytes, string.char(c))
			else                           table.insert(bytes, '\\'..tostring(c))
			end
			if i > 50 then table.insert(bytes, '...') break end
		end
		return table.concat(bytes)
	end
175 176
	if type(tt) == "table" then
		for key, value in pairs (tt) do
177
			result = result .. string.rep (" ", indent)
178 179
			if type (value) == "table" and not done [value] then
				done [value] = true
180
				result = result .. string.format("[%s] => {\n", printable (key))
181
				result = result .. table_print (value, indent + 4, done)
182 183
				result = result .. string.rep (" ", indent)
				result = result .. "}\n"
184
			else
185
				result = result .. string.format("[%s] => %s\n",
186
				         tostring (key), printable(value))
187 188 189
			end
		end
	else
190
		result = result .. tostring(tt) .. "\n"
191
	end
192
	return result
193
end