-
Notifications
You must be signed in to change notification settings - Fork 8
/
ex32.c
51 lines (45 loc) · 1.83 KB
/
ex32.c
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
// Example 32. Run user-defined Lua code in a sandbox
//8<----------------------------------------------------------------------------
#include <stdio.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(int argc, char **argv) {
lua_State *L = luaL_newstate();
luaL_openlibs(L);
const char *user_script = "/tmp/foo.lua";
//8<----------------------------------------------------------------------------
// Define and store the sandbox for subsequent use.
const char *safe[] = {
"assert", "error", "ipairs", "math", "next", "pairs",
"pcall", "select", "string", "table", "tonumber",
"tostring", "type", "xpcall", NULL
};
lua_newtable(L); // the sandbox environment
for (const char **p = safe; *p; p++)
lua_getglobal(L, *p), lua_setfield(L, -2, *p);
/* add other safe host functions to sandbox */
int sandbox_ref = luaL_ref(L, LUA_REGISTRYINDEX);
//8<----------------------------------------------------------------------------
lua_rawgeti(L, LUA_REGISTRYINDEX, sandbox_ref), lua_setglobal(L, "sandbox");
//8<----------------------------------------------------------------------------
// Attempt to load the user-defined Lua script
// (text-only) as an anonymous function.
if (luaL_loadfilex(L, user_script, "t") == LUA_OK) {
// Make the sandbox the function's environment.
lua_rawgeti(L, LUA_REGISTRYINDEX, sandbox_ref);
lua_setupvalue(L, -2, 1);
// Execute the script.
if (lua_pcall(L, 0, 0, 0) != LUA_OK) {
/* process and pop error message at index -1 */
}
}
/* ... */
// Finished with the sandbox; delete it.
luaL_unref(L, LUA_REGISTRYINDEX, sandbox_ref);
//8<----------------------------------------------------------------------------
luaL_dostring(L, "for k,v in pairs(sandbox) do print(k,v) end");
lua_close(L);
return 0;
}
//8<----------------------------------------------------------------------------