Redbean fix lua panic (#300)

* Fix Lua panic in redbean when calling request/connection functions from .init.lua

The error calls were triggered appropriately, but used the global Lua
state instead of the current Lua state (within protected call), which
triggered Lua panic. This executes the error in the proper context.

* Rename global Lua state object to make its (mis-)usage easier to detect
This commit is contained in:
Paul Kulchenko 2021-10-25 14:54:56 -07:00 committed by GitHub
parent c00755f921
commit 660ff56d40
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -376,7 +376,7 @@ static int messageshandled;
static int sslticketlifetime; static int sslticketlifetime;
static uint32_t clientaddrsize; static uint32_t clientaddrsize;
static lua_State *L; static lua_State *GL;
static size_t zsize; static size_t zsize;
static char *outbuf; static char *outbuf;
static char *content; static char *content;
@ -1035,6 +1035,7 @@ static bool LuaOnClientConnection(void) {
bool dropit; bool dropit;
uint32_t ip, serverip; uint32_t ip, serverip;
uint16_t port, serverport; uint16_t port, serverport;
lua_State *L = GL;
lua_getglobal(L, "OnClientConnection"); lua_getglobal(L, "OnClientConnection");
GetClientAddr(&ip, &port); GetClientAddr(&ip, &port);
GetServerAddr(&serverip, &serverport); GetServerAddr(&serverip, &serverport);
@ -1055,6 +1056,7 @@ static bool LuaOnClientConnection(void) {
static void LuaOnProcessCreate(int pid) { static void LuaOnProcessCreate(int pid) {
uint32_t ip, serverip; uint32_t ip, serverip;
uint16_t port, serverport; uint16_t port, serverport;
lua_State *L = GL;
lua_getglobal(L, "OnProcessCreate"); lua_getglobal(L, "OnProcessCreate");
GetClientAddr(&ip, &port); GetClientAddr(&ip, &port);
GetServerAddr(&serverip, &serverport); GetServerAddr(&serverip, &serverport);
@ -1070,6 +1072,7 @@ static void LuaOnProcessCreate(int pid) {
} }
static void LuaOnProcessDestroy(int pid) { static void LuaOnProcessDestroy(int pid) {
lua_State *L = GL;
lua_getglobal(L, "OnProcessDestroy"); lua_getglobal(L, "OnProcessDestroy");
lua_pushinteger(L, pid); lua_pushinteger(L, pid);
if (LuaCallWithTrace(L, 1, 0) != LUA_OK) { if (LuaCallWithTrace(L, 1, 0) != LUA_OK) {
@ -1080,6 +1083,7 @@ static void LuaOnProcessDestroy(int pid) {
static inline bool IsHookDefined(const char *s) { static inline bool IsHookDefined(const char *s) {
#ifndef STATIC #ifndef STATIC
lua_State *L = GL;
bool res = !!lua_getglobal(L, s); bool res = !!lua_getglobal(L, s);
lua_pop(L, 1); lua_pop(L, 1);
return res; return res;
@ -1089,6 +1093,7 @@ static inline bool IsHookDefined(const char *s) {
} }
static void CallSimpleHook(const char *s) { static void CallSimpleHook(const char *s) {
lua_State *L = GL;
lua_getglobal(L, s); lua_getglobal(L, s);
if (LuaCallWithTrace(L, 0, 0) != LUA_OK) { if (LuaCallWithTrace(L, 0, 0) != LUA_OK) {
LogLuaError(s, lua_tostring(L, -1)); LogLuaError(s, lua_tostring(L, -1));
@ -2819,6 +2824,7 @@ static char *ServeStatusz(void) {
AppendLong1("workers", shared->workers); AppendLong1("workers", shared->workers);
AppendLong1("assets.n", assets.n); AppendLong1("assets.n", assets.n);
#ifndef STATIC #ifndef STATIC
lua_State *L = GL;
AppendLong1("lua.memory", AppendLong1("lua.memory",
lua_gc(L, LUA_GCCOUNT) * 1024 + lua_gc(L, LUA_GCCOUNTB)); lua_gc(L, LUA_GCCOUNT) * 1024 + lua_gc(L, LUA_GCCOUNTB));
#endif #endif
@ -2872,6 +2878,7 @@ static bool IsLoopbackClient() {
} }
static char *LuaOnHttpRequest(void) { static char *LuaOnHttpRequest(void) {
lua_State *L = GL;
effectivepath.p = url.path.p; effectivepath.p = url.path.p;
effectivepath.n = url.path.n; effectivepath.n = url.path.n;
lua_getglobal(L, "OnHttpRequest"); lua_getglobal(L, "OnHttpRequest");
@ -2891,6 +2898,7 @@ static char *LuaOnHttpRequest(void) {
static char *ServeLua(struct Asset *a, const char *s, size_t n) { static char *ServeLua(struct Asset *a, const char *s, size_t n) {
char *code; char *code;
size_t codelen; size_t codelen;
lua_State *L = GL;
LockInc(&shared->c.dynamicrequests); LockInc(&shared->c.dynamicrequests);
effectivepath.p = s; effectivepath.p = s;
effectivepath.n = n; effectivepath.n = n;
@ -2995,7 +3003,7 @@ static const char *LuaCheckHost(lua_State *L, int idx, size_t *hostlen) {
return host; return host;
} }
static void OnlyCallFromInitLua(const char *api) { static void OnlyCallFromInitLua(lua_State *L, const char *api) {
if (isinitialized) { if (isinitialized) {
luaL_error(L, "%s() should be called from the global scope of .init.lua", luaL_error(L, "%s() should be called from the global scope of .init.lua",
api); api);
@ -3003,21 +3011,21 @@ static void OnlyCallFromInitLua(const char *api) {
} }
} }
static void DontCallFromInitLua(const char *api) { static void DontCallFromInitLua(lua_State *L, const char *api) {
if (!isinitialized) { if (!isinitialized) {
luaL_error(L, "%s() can't be called from .init.lua", api); luaL_error(L, "%s() can't be called from .init.lua", api);
unreachable; unreachable;
} }
} }
static void OnlyCallDuringConnection(const char *api) { static void OnlyCallDuringConnection(lua_State *L, const char *api) {
if (!ishandlingconnection) { if (!ishandlingconnection) {
luaL_error(L, "%s() can only be called while handling a connection", api); luaL_error(L, "%s() can only be called while handling a connection", api);
unreachable; unreachable;
} }
} }
static void OnlyCallDuringRequest(const char *api) { static void OnlyCallDuringRequest(lua_State *L, const char *api) {
if (!ishandlingrequest) { if (!ishandlingrequest) {
luaL_error(L, "%s() can only be called while handling a request", api); luaL_error(L, "%s() can only be called while handling a request", api);
unreachable; unreachable;
@ -3025,7 +3033,7 @@ static void OnlyCallDuringRequest(const char *api) {
} }
static int LuaServe(lua_State *L, const char *api, char *impl(void)) { static int LuaServe(lua_State *L, const char *api, char *impl(void)) {
OnlyCallDuringRequest(api); OnlyCallDuringRequest(L, api);
luaheaderp = impl(); luaheaderp = impl();
return 0; return 0;
} }
@ -3042,7 +3050,7 @@ static int LuaServeAsset(lua_State *L) {
size_t pathlen; size_t pathlen;
struct Asset *a; struct Asset *a;
const char *path; const char *path;
OnlyCallDuringRequest("ServeAsset"); OnlyCallDuringRequest(L, "ServeAsset");
path = LuaCheckPath(L, 1, &pathlen); path = LuaCheckPath(L, 1, &pathlen);
if ((a = GetAsset(path, pathlen)) && !S_ISDIR(GetMode(a))) { if ((a = GetAsset(path, pathlen)) && !S_ISDIR(GetMode(a))) {
luaheaderp = ServeAsset(a, path, pathlen); luaheaderp = ServeAsset(a, path, pathlen);
@ -3056,7 +3064,7 @@ static int LuaServeAsset(lua_State *L) {
static int LuaServeIndex(lua_State *L) { static int LuaServeIndex(lua_State *L) {
size_t pathlen; size_t pathlen;
const char *path; const char *path;
OnlyCallDuringRequest("ServeIndex"); OnlyCallDuringRequest(L, "ServeIndex");
path = LuaCheckPath(L, 1, &pathlen); path = LuaCheckPath(L, 1, &pathlen);
lua_pushboolean(L, !!(luaheaderp = ServeIndex(path, pathlen))); lua_pushboolean(L, !!(luaheaderp = ServeIndex(path, pathlen)));
return 1; return 1;
@ -3066,7 +3074,7 @@ static int LuaServeRedirect(lua_State *L) {
size_t loclen; size_t loclen;
const char *location, *eval; const char *location, *eval;
int code; int code;
OnlyCallDuringRequest("ServeRedirect"); OnlyCallDuringRequest(L, "ServeRedirect");
code = luaL_checkinteger(L, 1); code = luaL_checkinteger(L, 1);
if (!(300 <= code && code <= 399)) { if (!(300 <= code && code <= 399)) {
@ -3094,7 +3102,7 @@ static int LuaServeRedirect(lua_State *L) {
static int LuaRoutePath(lua_State *L) { static int LuaRoutePath(lua_State *L) {
size_t pathlen; size_t pathlen;
const char *path; const char *path;
OnlyCallDuringRequest("RoutePath"); OnlyCallDuringRequest(L, "RoutePath");
path = LuaCheckPath(L, 1, &pathlen); path = LuaCheckPath(L, 1, &pathlen);
lua_pushboolean(L, !!(luaheaderp = RoutePath(path, pathlen))); lua_pushboolean(L, !!(luaheaderp = RoutePath(path, pathlen)));
return 1; return 1;
@ -3103,7 +3111,7 @@ static int LuaRoutePath(lua_State *L) {
static int LuaRouteHost(lua_State *L) { static int LuaRouteHost(lua_State *L) {
size_t hostlen, pathlen; size_t hostlen, pathlen;
const char *host, *path; const char *host, *path;
OnlyCallDuringRequest("RouteHost"); OnlyCallDuringRequest(L, "RouteHost");
host = LuaCheckHost(L, 1, &hostlen); host = LuaCheckHost(L, 1, &hostlen);
path = LuaCheckPath(L, 2, &pathlen); path = LuaCheckPath(L, 2, &pathlen);
lua_pushboolean(L, !!(luaheaderp = RouteHost(host, hostlen, path, pathlen))); lua_pushboolean(L, !!(luaheaderp = RouteHost(host, hostlen, path, pathlen)));
@ -3113,7 +3121,7 @@ static int LuaRouteHost(lua_State *L) {
static int LuaRoute(lua_State *L) { static int LuaRoute(lua_State *L) {
size_t hostlen, pathlen; size_t hostlen, pathlen;
const char *host, *path; const char *host, *path;
OnlyCallDuringRequest("Route"); OnlyCallDuringRequest(L, "Route");
host = LuaCheckHost(L, 1, &hostlen); host = LuaCheckHost(L, 1, &hostlen);
path = LuaCheckPath(L, 2, &pathlen); path = LuaCheckPath(L, 2, &pathlen);
lua_pushboolean(L, !!(luaheaderp = Route(host, hostlen, path, pathlen))); lua_pushboolean(L, !!(luaheaderp = Route(host, hostlen, path, pathlen)));
@ -3125,7 +3133,7 @@ static int LuaRespond(lua_State *L, char *R(unsigned, const char *)) {
int code; int code;
size_t reasonlen; size_t reasonlen;
const char *reason; const char *reason;
OnlyCallDuringRequest("Respond"); OnlyCallDuringRequest(L, "Respond");
code = luaL_checkinteger(L, 1); code = luaL_checkinteger(L, 1);
if (!(100 <= code && code <= 999)) { if (!(100 <= code && code <= 999)) {
luaL_argerror(L, 1, "bad status code"); luaL_argerror(L, 1, "bad status code");
@ -3885,7 +3893,7 @@ static int LuaGetDate(lua_State *L) {
} }
static int LuaGetHttpVersion(lua_State *L) { static int LuaGetHttpVersion(lua_State *L) {
OnlyCallDuringRequest("GetHttpVersion"); OnlyCallDuringRequest(L, "GetHttpVersion");
lua_pushinteger(L, msg.version); lua_pushinteger(L, msg.version);
return 1; return 1;
} }
@ -3896,7 +3904,7 @@ static int LuaGetRedbeanVersion(lua_State *L) {
} }
static int LuaGetMethod(lua_State *L) { static int LuaGetMethod(lua_State *L) {
OnlyCallDuringRequest("GetMethod"); OnlyCallDuringRequest(L, "GetMethod");
if (msg.method) { if (msg.method) {
lua_pushstring(L, kHttpMethod[msg.method]); lua_pushstring(L, kHttpMethod[msg.method]);
} else { } else {
@ -3915,17 +3923,17 @@ static int LuaGetAddr(lua_State *L, void GetAddr(uint32_t *, uint16_t *)) {
} }
static int LuaGetServerAddr(lua_State *L) { static int LuaGetServerAddr(lua_State *L) {
OnlyCallDuringConnection("GetServerAddr"); OnlyCallDuringConnection(L, "GetServerAddr");
return LuaGetAddr(L, GetServerAddr); return LuaGetAddr(L, GetServerAddr);
} }
static int LuaGetClientAddr(lua_State *L) { static int LuaGetClientAddr(lua_State *L) {
OnlyCallDuringConnection("GetClientAddr"); OnlyCallDuringConnection(L, "GetClientAddr");
return LuaGetAddr(L, GetClientAddr); return LuaGetAddr(L, GetClientAddr);
} }
static int LuaGetRemoteAddr(lua_State *L) { static int LuaGetRemoteAddr(lua_State *L) {
OnlyCallDuringRequest("GetRemoteAddr"); OnlyCallDuringRequest(L, "GetRemoteAddr");
return LuaGetAddr(L, GetRemoteAddr); return LuaGetAddr(L, GetRemoteAddr);
} }
@ -3971,7 +3979,7 @@ static int LuaCategorizeIp(lua_State *L) {
static int LuaGetUrl(lua_State *L) { static int LuaGetUrl(lua_State *L) {
char *p; char *p;
size_t n; size_t n;
OnlyCallDuringRequest("GetUrl"); OnlyCallDuringRequest(L, "GetUrl");
p = EncodeUrl(&url, &n); p = EncodeUrl(&url, &n);
lua_pushlstring(L, p, n); lua_pushlstring(L, p, n);
free(p); free(p);
@ -3987,25 +3995,25 @@ static void LuaPushUrlView(lua_State *L, struct UrlView *v) {
} }
static int LuaGetScheme(lua_State *L) { static int LuaGetScheme(lua_State *L) {
OnlyCallDuringRequest("GetScheme"); OnlyCallDuringRequest(L, "GetScheme");
LuaPushUrlView(L, &url.scheme); LuaPushUrlView(L, &url.scheme);
return 1; return 1;
} }
static int LuaGetPath(lua_State *L) { static int LuaGetPath(lua_State *L) {
OnlyCallDuringRequest("GetPath"); OnlyCallDuringRequest(L, "GetPath");
LuaPushUrlView(L, &url.path); LuaPushUrlView(L, &url.path);
return 1; return 1;
} }
static int LuaGetEffectivePath(lua_State *L) { static int LuaGetEffectivePath(lua_State *L) {
OnlyCallDuringRequest("GetEffectivePath"); OnlyCallDuringRequest(L, "GetEffectivePath");
lua_pushlstring(L, effectivepath.p, effectivepath.n); lua_pushlstring(L, effectivepath.p, effectivepath.n);
return 1; return 1;
} }
static int LuaGetFragment(lua_State *L) { static int LuaGetFragment(lua_State *L) {
OnlyCallDuringRequest("GetFragment"); OnlyCallDuringRequest(L, "GetFragment");
LuaPushUrlView(L, &url.fragment); LuaPushUrlView(L, &url.fragment);
return 1; return 1;
} }
@ -4013,7 +4021,7 @@ static int LuaGetFragment(lua_State *L) {
static int LuaGetUser(lua_State *L) { static int LuaGetUser(lua_State *L) {
size_t n; size_t n;
const char *p, *q; const char *p, *q;
OnlyCallDuringRequest("GetUser"); OnlyCallDuringRequest(L, "GetUser");
if (url.user.p) { if (url.user.p) {
LuaPushUrlView(L, &url.user); LuaPushUrlView(L, &url.user);
} else if ((p = GetBasicAuthorization(&n))) { } else if ((p = GetBasicAuthorization(&n))) {
@ -4029,7 +4037,7 @@ static int LuaGetUser(lua_State *L) {
static int LuaGetPass(lua_State *L) { static int LuaGetPass(lua_State *L) {
size_t n; size_t n;
const char *p, *q; const char *p, *q;
OnlyCallDuringRequest("GetPass"); OnlyCallDuringRequest(L, "GetPass");
if (url.user.p) { if (url.user.p) {
LuaPushUrlView(L, &url.pass); LuaPushUrlView(L, &url.pass);
} else if ((p = GetBasicAuthorization(&n))) { } else if ((p = GetBasicAuthorization(&n))) {
@ -4047,7 +4055,7 @@ static int LuaGetPass(lua_State *L) {
static int LuaGetHost(lua_State *L) { static int LuaGetHost(lua_State *L) {
char b[16]; char b[16];
OnlyCallDuringRequest("GetHost"); OnlyCallDuringRequest(L, "GetHost");
if (url.host.n) { if (url.host.n) {
lua_pushlstring(L, url.host.p, url.host.n); lua_pushlstring(L, url.host.p, url.host.n);
} else { } else {
@ -4059,7 +4067,7 @@ static int LuaGetHost(lua_State *L) {
static int LuaGetPort(lua_State *L) { static int LuaGetPort(lua_State *L) {
int i, x = 0; int i, x = 0;
OnlyCallDuringRequest("GetPort"); OnlyCallDuringRequest(L, "GetPort");
for (i = 0; i < url.port.n; ++i) x = url.port.p[i] - '0' + x * 10; for (i = 0; i < url.port.n; ++i) x = url.port.p[i] - '0' + x * 10;
if (!x) x = ntohs(serveraddr->sin_port); if (!x) x = ntohs(serveraddr->sin_port);
lua_pushinteger(L, x); lua_pushinteger(L, x);
@ -4089,7 +4097,7 @@ static int LuaGetHeader(lua_State *L) {
int h; int h;
const char *key; const char *key;
size_t i, keylen; size_t i, keylen;
OnlyCallDuringRequest("GetHeader"); OnlyCallDuringRequest(L, "GetHeader");
key = luaL_checklstring(L, 1, &keylen); key = luaL_checklstring(L, 1, &keylen);
if ((h = GetHttpHeader(key, keylen)) != -1) { if ((h = GetHttpHeader(key, keylen)) != -1) {
if (msg.headers[h].a) { if (msg.headers[h].a) {
@ -4110,7 +4118,7 @@ static int LuaGetHeader(lua_State *L) {
} }
static int LuaGetHeaders(lua_State *L) { static int LuaGetHeaders(lua_State *L) {
OnlyCallDuringRequest("GetHeaders"); OnlyCallDuringRequest(L, "GetHeaders");
return LuaPushHeaders(L, &msg, inbuf.p); return LuaPushHeaders(L, &msg, inbuf.p);
} }
@ -4120,7 +4128,7 @@ static int LuaSetHeader(lua_State *L) {
char *p, *q; char *p, *q;
const char *key, *val, *eval; const char *key, *val, *eval;
size_t i, keylen, vallen, evallen; size_t i, keylen, vallen, evallen;
OnlyCallDuringRequest("SetHeader"); OnlyCallDuringRequest(L, "SetHeader");
key = luaL_checklstring(L, 1, &keylen); key = luaL_checklstring(L, 1, &keylen);
val = luaL_checklstring(L, 2, &vallen); val = luaL_checklstring(L, 2, &vallen);
if ((h = GetHttpHeader(key, keylen)) == -1) { if ((h = GetHttpHeader(key, keylen)) == -1) {
@ -4169,7 +4177,7 @@ static int LuaSetHeader(lua_State *L) {
static int LuaGetCookie(lua_State *L) { static int LuaGetCookie(lua_State *L) {
char *cookie = 0, *cookietmpl, *cookieval; char *cookie = 0, *cookietmpl, *cookieval;
OnlyCallDuringRequest("GetCookie"); OnlyCallDuringRequest(L, "GetCookie");
cookietmpl = gc(xasprintf(" %s=", luaL_checkstring(L, 1))); cookietmpl = gc(xasprintf(" %s=", luaL_checkstring(L, 1)));
if (HasHeader(kHttpCookie)) { if (HasHeader(kHttpCookie)) {
@ -4195,7 +4203,7 @@ static int LuaSetCookie(lua_State *L) {
const char *hostpref = "__Host-"; const char *hostpref = "__Host-";
const char *securepref = "__Secure-"; const char *securepref = "__Secure-";
OnlyCallDuringRequest("SetCookie"); OnlyCallDuringRequest(L, "SetCookie");
key = luaL_checklstring(L, 1, &keylen); key = luaL_checklstring(L, 1, &keylen);
val = luaL_checklstring(L, 2, &vallen); val = luaL_checklstring(L, 2, &vallen);
@ -4295,7 +4303,7 @@ static int LuaSetCookie(lua_State *L) {
static int LuaHasParam(lua_State *L) { static int LuaHasParam(lua_State *L) {
size_t i, n; size_t i, n;
const char *s; const char *s;
OnlyCallDuringRequest("HasParam"); OnlyCallDuringRequest(L, "HasParam");
s = luaL_checklstring(L, 1, &n); s = luaL_checklstring(L, 1, &n);
for (i = 0; i < url.params.n; ++i) { for (i = 0; i < url.params.n; ++i) {
if (SlicesEqual(s, n, url.params.p[i].key.p, url.params.p[i].key.n)) { if (SlicesEqual(s, n, url.params.p[i].key.p, url.params.p[i].key.n)) {
@ -4310,7 +4318,7 @@ static int LuaHasParam(lua_State *L) {
static int LuaGetParam(lua_State *L) { static int LuaGetParam(lua_State *L) {
size_t i, n; size_t i, n;
const char *s; const char *s;
OnlyCallDuringRequest("GetParam"); OnlyCallDuringRequest(L, "GetParam");
s = luaL_checklstring(L, 1, &n); s = luaL_checklstring(L, 1, &n);
for (i = 0; i < url.params.n; ++i) { for (i = 0; i < url.params.n; ++i) {
if (SlicesEqual(s, n, url.params.p[i].key.p, url.params.p[i].key.n)) { if (SlicesEqual(s, n, url.params.p[i].key.p, url.params.p[i].key.n)) {
@ -4342,7 +4350,7 @@ static void LuaPushUrlParams(lua_State *L, struct UrlParams *h) {
} }
static int LuaGetParams(lua_State *L) { static int LuaGetParams(lua_State *L) {
OnlyCallDuringRequest("GetParams"); OnlyCallDuringRequest(L, "GetParams");
LuaPushUrlParams(L, &url.params); LuaPushUrlParams(L, &url.params);
return 1; return 1;
} }
@ -4458,7 +4466,7 @@ static int LuaEncodeUrl(lua_State *L) {
static int LuaWrite(lua_State *L) { static int LuaWrite(lua_State *L) {
size_t size; size_t size;
const char *data; const char *data;
OnlyCallDuringRequest("Write"); OnlyCallDuringRequest(L, "Write");
if (!lua_isnil(L, 1)) { if (!lua_isnil(L, 1)) {
data = luaL_checklstring(L, 1, &size); data = luaL_checklstring(L, 1, &size);
appendd(&outbuf, data, size); appendd(&outbuf, data, size);
@ -4757,32 +4765,32 @@ static noinline int LuaProgramInt(lua_State *L, void P(long)) {
} }
static int LuaProgramPort(lua_State *L) { static int LuaProgramPort(lua_State *L) {
OnlyCallFromInitLua("ProgramPort"); OnlyCallFromInitLua(L, "ProgramPort");
return LuaProgramInt(L, ProgramPort); return LuaProgramInt(L, ProgramPort);
} }
static int LuaProgramCache(lua_State *L) { static int LuaProgramCache(lua_State *L) {
OnlyCallFromInitLua("ProgramCache"); OnlyCallFromInitLua(L, "ProgramCache");
return LuaProgramInt(L, ProgramCache); return LuaProgramInt(L, ProgramCache);
} }
static int LuaProgramTimeout(lua_State *L) { static int LuaProgramTimeout(lua_State *L) {
OnlyCallFromInitLua("ProgramTimeout"); OnlyCallFromInitLua(L, "ProgramTimeout");
return LuaProgramInt(L, ProgramTimeout); return LuaProgramInt(L, ProgramTimeout);
} }
static int LuaProgramUid(lua_State *L) { static int LuaProgramUid(lua_State *L) {
OnlyCallFromInitLua("ProgramUid"); OnlyCallFromInitLua(L, "ProgramUid");
return LuaProgramInt(L, ProgramUid); return LuaProgramInt(L, ProgramUid);
} }
static int LuaProgramGid(lua_State *L) { static int LuaProgramGid(lua_State *L) {
OnlyCallFromInitLua("ProgramGid"); OnlyCallFromInitLua(L, "ProgramGid");
return LuaProgramInt(L, ProgramGid); return LuaProgramInt(L, ProgramGid);
} }
static int LuaProgramSslTicketLifetime(lua_State *L) { static int LuaProgramSslTicketLifetime(lua_State *L) {
OnlyCallFromInitLua("ProgramSslTicketLifetime"); OnlyCallFromInitLua(L, "ProgramSslTicketLifetime");
return LuaProgramInt(L, ProgramSslTicketLifetime); return LuaProgramInt(L, ProgramSslTicketLifetime);
} }
@ -4792,12 +4800,12 @@ static noinline int LuaProgramString(lua_State *L, void P(const char *)) {
} }
static int LuaProgramAddr(lua_State *L) { static int LuaProgramAddr(lua_State *L) {
OnlyCallFromInitLua("ProgramAddr"); OnlyCallFromInitLua(L, "ProgramAddr");
return LuaProgramString(L, ProgramAddr); return LuaProgramString(L, ProgramAddr);
} }
static int LuaProgramBrand(lua_State *L) { static int LuaProgramBrand(lua_State *L) {
OnlyCallFromInitLua("ProgramBrand"); OnlyCallFromInitLua(L, "ProgramBrand");
return LuaProgramString(L, ProgramBrand); return LuaProgramString(L, ProgramBrand);
} }
@ -4806,12 +4814,12 @@ static int LuaProgramDirectory(lua_State *L) {
} }
static int LuaProgramLogPath(lua_State *L) { static int LuaProgramLogPath(lua_State *L) {
OnlyCallFromInitLua("ProgramLogPath"); OnlyCallFromInitLua(L, "ProgramLogPath");
return LuaProgramString(L, ProgramLogPath); return LuaProgramString(L, ProgramLogPath);
} }
static int LuaProgramPidPath(lua_State *L) { static int LuaProgramPidPath(lua_State *L) {
OnlyCallFromInitLua("ProgramPidPath"); OnlyCallFromInitLua(L, "ProgramPidPath");
return LuaProgramString(L, ProgramPidPath); return LuaProgramString(L, ProgramPidPath);
} }
@ -4820,7 +4828,7 @@ static int LuaProgramSslPresharedKey(lua_State *L) {
struct Psk psk; struct Psk psk;
size_t n1, n2, i; size_t n1, n2, i;
const char *p1, *p2; const char *p1, *p2;
OnlyCallFromInitLua("ProgramSslPresharedKey"); OnlyCallFromInitLua(L, "ProgramSslPresharedKey");
p1 = luaL_checklstring(L, 1, &n1); p1 = luaL_checklstring(L, 1, &n1);
p2 = luaL_checklstring(L, 2, &n2); p2 = luaL_checklstring(L, 2, &n2);
if (!n1 || n1 > MBEDTLS_PSK_MAX_LEN || !n2) { if (!n1 || n1 > MBEDTLS_PSK_MAX_LEN || !n2) {
@ -4863,7 +4871,7 @@ static int LuaProgramPrivateKey(lua_State *L) {
#ifndef UNSECURE #ifndef UNSECURE
size_t n; size_t n;
const char *p; const char *p;
OnlyCallFromInitLua("ProgramPrivateKey"); OnlyCallFromInitLua(L, "ProgramPrivateKey");
p = luaL_checklstring(L, 1, &n); p = luaL_checklstring(L, 1, &n);
ProgramPrivateKey(p, n); ProgramPrivateKey(p, n);
#endif #endif
@ -4874,7 +4882,7 @@ static int LuaProgramCertificate(lua_State *L) {
#ifndef UNSECURE #ifndef UNSECURE
size_t n; size_t n;
const char *p; const char *p;
OnlyCallFromInitLua("ProgramCertificate"); OnlyCallFromInitLua(L, "ProgramCertificate");
p = luaL_checklstring(L, 1, &n); p = luaL_checklstring(L, 1, &n);
ProgramCertificate(p, n); ProgramCertificate(p, n);
#endif #endif
@ -4903,12 +4911,12 @@ static noinline int LuaProgramBool(lua_State *L, bool *b) {
} }
static int LuaProgramSslClientVerify(lua_State *L) { static int LuaProgramSslClientVerify(lua_State *L) {
OnlyCallFromInitLua("ProgramSslClientVerify"); OnlyCallFromInitLua(L, "ProgramSslClientVerify");
return LuaProgramBool(L, &sslclientverify); return LuaProgramBool(L, &sslclientverify);
} }
static int LuaProgramSslFetchVerify(lua_State *L) { static int LuaProgramSslFetchVerify(lua_State *L) {
OnlyCallFromInitLua("ProgramSslFetchVerify"); OnlyCallFromInitLua(L, "ProgramSslFetchVerify");
return LuaProgramBool(L, &sslfetchverify); return LuaProgramBool(L, &sslfetchverify);
} }
@ -4921,13 +4929,13 @@ static int LuaProgramLogBodies(lua_State *L) {
} }
static int LuaEvadeDragnetSurveillance(lua_State *L) { static int LuaEvadeDragnetSurveillance(lua_State *L) {
OnlyCallFromInitLua("EvadeDragnetSurveillance"); OnlyCallFromInitLua(L, "EvadeDragnetSurveillance");
return LuaProgramBool(L, &evadedragnetsurveillance); return LuaProgramBool(L, &evadedragnetsurveillance);
} }
static int LuaProgramSslCompression(lua_State *L) { static int LuaProgramSslCompression(lua_State *L) {
#ifndef UNSECURE #ifndef UNSECURE
OnlyCallFromInitLua("ProgramSslCompression"); OnlyCallFromInitLua(L, "ProgramSslCompression");
conf.disable_compression = confcli.disable_compression = !lua_toboolean(L, 1); conf.disable_compression = confcli.disable_compression = !lua_toboolean(L, 1);
#endif #endif
return 0; return 0;
@ -5121,7 +5129,7 @@ static void LuaSetIntField(lua_State *L, const char *k, lua_Integer v) {
} }
static int LuaLaunchBrowser(lua_State *L) { static int LuaLaunchBrowser(lua_State *L) {
OnlyCallFromInitLua("LaunchBrowser"); OnlyCallFromInitLua(L, "LaunchBrowser");
launchbrowser = strdup(luaL_optstring(L, 1, "/")); launchbrowser = strdup(luaL_optstring(L, 1, "/"));
return 0; return 0;
} }
@ -5267,6 +5275,7 @@ static bool LuaRun(const char *path, bool mandatory) {
pathlen = strlen(path); pathlen = strlen(path);
if ((a = GetAsset(path, pathlen))) { if ((a = GetAsset(path, pathlen))) {
if ((code = FreeLater(LoadAsset(a, &codelen)))) { if ((code = FreeLater(LoadAsset(a, &codelen)))) {
lua_State *L = GL;
effectivepath.p = path; effectivepath.p = path;
effectivepath.n = pathlen; effectivepath.n = pathlen;
DEBUGF("(lua) LuaRun(%`'s)", path); DEBUGF("(lua) LuaRun(%`'s)", path);
@ -5461,8 +5470,8 @@ static char *GetDefaultLuaPath(void) {
static void LuaInit(void) { static void LuaInit(void) {
#ifndef STATIC #ifndef STATIC
size_t i; size_t i;
lua_State *L = GL = luaL_newstate();
g_lua_path_default = GetDefaultLuaPath(); g_lua_path_default = GetDefaultLuaPath();
L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);
for (i = 0; i < ARRAYLEN(kLuaLibs); ++i) { for (i = 0; i < ARRAYLEN(kLuaLibs); ++i) {
luaL_requiref(L, kLuaLibs[i].name, kLuaLibs[i].func, 1); luaL_requiref(L, kLuaLibs[i].name, kLuaLibs[i].func, 1);
@ -5502,6 +5511,7 @@ static void LuaReload(void) {
static void LuaDestroy(void) { static void LuaDestroy(void) {
#ifndef STATIC #ifndef STATIC
lua_State *L = GL;
lua_close(L); lua_close(L);
#endif #endif
} }