From a16dd6550385cca9faf3f315db4608d4520f9c19 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 5 Sep 2013 20:35:49 +0000 Subject: [PATCH] Get rid of static lua-exec state. Have lua_setup return the state it creates. Store the state created from --lua-exec in the global options table. Use a temporary local for --lua-exec-internal. --- ncat/ncat_core.c | 4 ++++ ncat/ncat_core.h | 8 ++++++++ ncat/ncat_lua.c | 16 +++++++++------- ncat/ncat_lua.h | 4 ++-- ncat/ncat_main.c | 9 ++++++--- ncat/ncat_posix.c | 2 +- 6 files changed, 30 insertions(+), 13 deletions(-) diff --git a/ncat/ncat_core.c b/ncat/ncat_core.c index 006386f36..b7177d193 100644 --- a/ncat/ncat_core.c +++ b/ncat/ncat_core.c @@ -204,6 +204,10 @@ void options_init(void) o.proxy_auth = NULL; o.proxytype = NULL; +#ifdef HAVE_LUA + o.lua_exec_state = NULL; +#endif + #ifdef HAVE_OPENSSL o.ssl = 0; o.sslcert = NULL; diff --git a/ncat/ncat_core.h b/ncat/ncat_core.h index 0dca7ef8c..f86fa5b58 100644 --- a/ncat/ncat_core.h +++ b/ncat/ncat_core.h @@ -126,6 +126,10 @@ #include "util.h" #include "sockaddr_u.h" +#ifdef HAVE_LUA +#include "ncat_lua.h" +#endif + /* Maximum size of the srcaddrs array. In this case two because we can only have a IPV4 INADDR_ANY and a IPV6 in6addr_any at most or a user defined address */ #define NUM_LISTEN_ADDRS 2 @@ -198,6 +202,10 @@ struct options { char *proxy_auth; char *proxytype; +#ifdef HAVE_LUA + lua_State *lua_exec_state; +#endif + int ssl; char *sslcert; char *sslkey; diff --git a/ncat/ncat_lua.c b/ncat/ncat_lua.c index 4bfa5d12b..56895cadb 100644 --- a/ncat/ncat_lua.c +++ b/ncat/ncat_lua.c @@ -124,9 +124,7 @@ #include "ncat.h" #include "ncat_lua.h" -static lua_State *L; - -static void report(char *prefix) +static void report(lua_State *L, char *prefix) { const char *errormsg; errormsg = lua_tostring(L, -1); @@ -150,25 +148,29 @@ static int traceback (lua_State *L) return 1; } -void lua_setup(void) +lua_State *lua_setup(void) { + lua_State *L; + ncat_assert(o.cmdexec != NULL); L = luaL_newstate(); luaL_openlibs(L); if (luaL_loadfile(L, o.cmdexec) != 0) - report("Error loading the Lua script"); + report(L, "Error loading the Lua script"); + + return L; } -void lua_run(void) +void lua_run(lua_State *L) { /* The chunk as read from lua_setup is on top of the stack. Put the traceback function before it and run it. */ lua_pushcfunction(L, traceback); lua_insert(L, -2); if (lua_pcall(L, 0, 0, -2) != LUA_OK && !lua_isnil(L, -1)) { - report("Error running the Lua script"); + report(L, "Error running the Lua script"); } else { if (o.debug) logdebug("%s returned successfully.\n", o.cmdexec); diff --git a/ncat/ncat_lua.h b/ncat/ncat_lua.h index b2e80ce2a..7404b6b8c 100644 --- a/ncat/ncat_lua.h +++ b/ncat/ncat_lua.h @@ -138,7 +138,7 @@ extern "C" { } #endif -void lua_setup(void); -void lua_run(void); +lua_State *lua_setup(void); +void lua_run(lua_State *L); #endif diff --git a/ncat/ncat_main.c b/ncat/ncat_main.c index d6c2bdbe7..6ba38312a 100644 --- a/ncat/ncat_main.c +++ b/ncat/ncat_main.c @@ -535,6 +535,8 @@ int main(int argc, char *argv[]) forking in POSIX builds, Windows does not have the fork() system call and thus requires this workaround. More info here: http://seclists.org/nmap-dev/2013/q2/492 */ + lua_State *L; + #ifdef WIN32 if (o.debug) logdebug("Enabling binary stdout for the Lua output.\n"); @@ -544,8 +546,9 @@ int main(int argc, char *argv[]) #endif ncat_assert(argc == 3); o.cmdexec = argv[2]; - lua_setup(); - lua_run(); + L = lua_setup(); + ncat_assert(L != NULL); + lua_run(L); } #endif break; @@ -869,7 +872,7 @@ connection brokering should work."); #ifdef HAVE_LUA if (o.execmode == EXEC_LUA) - lua_setup(); + o.lua_exec_state = lua_setup(); #endif if (o.listen) diff --git a/ncat/ncat_posix.c b/ncat/ncat_posix.c index 9946b20b6..16ddb271b 100644 --- a/ncat/ncat_posix.c +++ b/ncat/ncat_posix.c @@ -234,7 +234,7 @@ void netexec(struct fdinfo *info, char *cmdexec) break; #ifdef HAVE_LUA case EXEC_LUA: - lua_run(); + lua_run(o.lua_exec_state); break; #endif default: