1
0
mirror of https://github.com/nmap/nmap.git synced 2026-01-18 20:29:02 +00:00

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.
This commit is contained in:
david
2013-09-05 20:35:49 +00:00
parent 7ab4da3581
commit a16dd65503
6 changed files with 30 additions and 13 deletions

View File

@@ -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;

View File

@@ -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;

View File

@@ -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);

View File

@@ -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

View File

@@ -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)

View File

@@ -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: