From 502339c92cb2485504dc017109ad10a0c65f6e7e Mon Sep 17 00:00:00 2001 From: david Date: Fri, 6 Sep 2013 06:16:13 +0000 Subject: [PATCH] Revert to r32212. My recent changes are causing problems when I try to integrate them into the ncat-sa-take2 branch. --- ncat/ncat_core.c | 4 ---- ncat/ncat_core.h | 8 -------- ncat/ncat_lua.c | 42 ++++++++++++++++++------------------------ ncat/ncat_lua.h | 7 ++----- ncat/ncat_main.c | 10 ++++------ ncat/ncat_posix.c | 2 +- 6 files changed, 25 insertions(+), 48 deletions(-) diff --git a/ncat/ncat_core.c b/ncat/ncat_core.c index b7177d193..006386f36 100644 --- a/ncat/ncat_core.c +++ b/ncat/ncat_core.c @@ -204,10 +204,6 @@ 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 f86fa5b58..0dca7ef8c 100644 --- a/ncat/ncat_core.h +++ b/ncat/ncat_core.h @@ -126,10 +126,6 @@ #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 @@ -202,10 +198,6 @@ 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 71dd73549..5416973de 100644 --- a/ncat/ncat_lua.c +++ b/ncat/ncat_lua.c @@ -124,7 +124,10 @@ #include "ncat.h" #include "ncat_lua.h" -static void report(lua_State *L, char *prefix) +static lua_State *L; +static int last_function_number; + +static void report(char *prefix) { const char *errormsg; errormsg = lua_tostring(L, -1); @@ -148,41 +151,32 @@ static int traceback (lua_State *L) return 1; } -lua_State *lua_setup(const char *filename) +void lua_setup(void) { - lua_State *L; - - ncat_assert(filename != NULL); + ncat_assert(o.cmdexec != NULL); L = luaL_newstate(); luaL_openlibs(L); - if (luaL_loadfile(L, filename) != 0) - report(L, "Error loading the Lua script"); + if (luaL_loadfile(L, o.cmdexec) != 0) + report("Error loading the Lua script"); - return L; + /* install the traceback function */ + last_function_number = lua_gettop(L); + lua_pushcfunction(L, traceback); + lua_insert(L, last_function_number); } -void lua_run(lua_State *L) +void lua_run(void) { - if (lua_call_traceback(L, 0, 0) != LUA_OK && !lua_isnil(L, -1)) { - report(L, "Error running the Lua script"); + if (lua_pcall(L, 0, 0, last_function_number) != LUA_OK && !lua_isnil(L, -1)) { + /* handle the error; the code below is taken from lua.c, Lua source code */ + lua_remove(L, last_function_number); + report("Error running the Lua script"); } else { if (o.debug) - logdebug("Lua script returned successfully.\n"); + logdebug("%s returned successfully.\n", o.cmdexec); lua_close(L); exit(EXIT_SUCCESS); } } - - -/* Returns the value of a lua_pcall of the chunk on top of the stack, with an - error handler that prints a traceback. */ -int lua_call_traceback(lua_State *L, int nargs, int nresults) -{ - /* The chunk to run is on top of the stack. Put the traceback function - before it and run it. */ - lua_pushcfunction(L, traceback); - lua_insert(L, -2); - return lua_pcall(L, nargs, nresults, -2); -} diff --git a/ncat/ncat_lua.h b/ncat/ncat_lua.h index c74680de5..6c16cf5d0 100644 --- a/ncat/ncat_lua.h +++ b/ncat/ncat_lua.h @@ -124,8 +124,6 @@ #ifndef _NCAT_LUA_H #define _NCAT_LUA_H -#include "ncat.h" - #ifdef __cplusplus extern "C" { #endif @@ -138,8 +136,7 @@ extern "C" { } #endif -lua_State *lua_setup(const char *filename); -void lua_run(lua_State *L); -int lua_call_traceback(lua_State *L, int nargs, int nresults); +void lua_setup(void); +void lua_run(void); #endif diff --git a/ncat/ncat_main.c b/ncat/ncat_main.c index 1e6bd1ffc..d6c2bdbe7 100644 --- a/ncat/ncat_main.c +++ b/ncat/ncat_main.c @@ -535,8 +535,6 @@ 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"); @@ -545,9 +543,9 @@ int main(int argc, char *argv[]) perror("Cannot set mode"); #endif ncat_assert(argc == 3); - L = lua_setup(argv[2]); - ncat_assert(L != NULL); - lua_run(L); + o.cmdexec = argv[2]; + lua_setup(); + lua_run(); } #endif break; @@ -871,7 +869,7 @@ connection brokering should work."); #ifdef HAVE_LUA if (o.execmode == EXEC_LUA) - o.lua_exec_state = lua_setup(o.cmdexec); + lua_setup(); #endif if (o.listen) diff --git a/ncat/ncat_posix.c b/ncat/ncat_posix.c index 16ddb271b..9946b20b6 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(o.lua_exec_state); + lua_run(); break; #endif default: