Index: nse_main.h =================================================================== --- nse_main.h (revision 11110) +++ nse_main.h (working copy) @@ -29,6 +29,7 @@ class Target; int script_scan(std::vector &targets); +void script_scan_close(void); int script_updatedb(); //parses the arguments provided to scripts via nmap's --script-args option Index: nmap.cc =================================================================== --- nmap.cc (revision 11110) +++ nmap.cc (working copy) @@ -110,6 +110,7 @@ #include "traceroute.h" #include "nmap_tty.h" #include "nmap_dns.h" +#include "nse_main.h" #include "services.h" #include "protocols.h" #include "targets.h" @@ -1916,6 +1917,7 @@ if (o.extra_payload) free(o.extra_payload); if (o.ipoptions) free(o.ipoptions); #ifndef NOLUA + script_scan_close(); free(o.scriptargs); #endif } Index: nse_main.cc =================================================================== --- nse_main.cc (revision 11111) +++ nse_main.cc (working copy) @@ -219,6 +219,8 @@ return ret; } +static lua_State* L = NULL; + /* open a lua instance * open the lua standard libraries * open all the scripts and prepare them for execution @@ -232,7 +234,6 @@ std::list::iterator thr_iter; std::list torun_threads; std::vector::iterator script_iter; - lua_State* L; o.current_scantype = SCRIPT_SCAN; @@ -251,54 +252,56 @@ SCRIPT_ENGINE, (*targets.begin())->NameIP(targetstr, sizeof(targetstr))); ) - L = luaL_newstate(); - if (L == NULL) { - error("%s: Failed luaL_newstate()", SCRIPT_ENGINE); - return SCRIPT_ENGINE_ERROR; - } - lua_atpanic(L, panic); + if (L == NULL) { + L = luaL_newstate(); + if (L == NULL) { + error("%s: Failed luaL_newstate()", SCRIPT_ENGINE); + return SCRIPT_ENGINE_ERROR; + } + lua_atpanic(L, panic); - status = lua_cpcall(L, init_lua, NULL); - if (status != 0) - { - error("%s: error while initializing Lua State:\n%s\n", - SCRIPT_ENGINE, lua_tostring(L, -1)); - status = SCRIPT_ENGINE_ERROR; - goto finishup; - } + status = lua_cpcall(L, init_lua, NULL); + if (status != 0) + { + error("%s: error while initializing Lua State:\n%s\n", + SCRIPT_ENGINE, lua_tostring(L, -1)); + status = SCRIPT_ENGINE_ERROR; + goto finishup; + } - //set the arguments - if provided - status = lua_cpcall(L, init_setargs, NULL); - if (status != 0) - { - error("%s: error while setting arguments for scripts:\n%s\n", - SCRIPT_ENGINE, lua_tostring(L, -1)); - status = SCRIPT_ENGINE_ERROR; - goto finishup; - } + //set the arguments - if provided + status = lua_cpcall(L, init_setargs, NULL); + if (status != 0) + { + error("%s: error while setting arguments for scripts:\n%s\n", + SCRIPT_ENGINE, lua_tostring(L, -1)); + status = SCRIPT_ENGINE_ERROR; + goto finishup; + } - lua_settop(L, 0); // safety, is 0 anyway - lua_rawgeti(L, LUA_REGISTRYINDEX, errfunc); // index 1 + lua_settop(L, 0); // safety, is 0 anyway + lua_rawgeti(L, LUA_REGISTRYINDEX, errfunc); // index 1 - if (!lua_checkstack(L, o.chosenScripts.size() + 1)) - { - error("%s: stack overflow at %s:%d", SCRIPT_ENGINE, __FILE__, __LINE__); - status = SCRIPT_ENGINE_ERROR; - goto finishup; - } - lua_pushcclosure(L, init_rules, 0); - for (script_iter = o.chosenScripts.begin(); - script_iter != o.chosenScripts.end(); - script_iter++) - lua_pushstring(L, script_iter->c_str()); - status = lua_pcall(L, o.chosenScripts.size(), 0, 1); - if (status != 0) - { - error("%s: error while initializing script rules:\n%s\n", - SCRIPT_ENGINE, lua_tostring(L, -1)); - status = SCRIPT_ENGINE_ERROR; - goto finishup; - } + if (!lua_checkstack(L, o.chosenScripts.size() + 1)) + { + error("%s: stack overflow at %s:%d", SCRIPT_ENGINE, __FILE__, __LINE__); + status = SCRIPT_ENGINE_ERROR; + goto finishup; + } + lua_pushcclosure(L, init_rules, 0); + for (script_iter = o.chosenScripts.begin(); + script_iter != o.chosenScripts.end(); + script_iter++) + lua_pushstring(L, script_iter->c_str()); + status = lua_pcall(L, o.chosenScripts.size(), 0, 1); + if (status != 0) + { + error("%s: error while initializing script rules:\n%s\n", + SCRIPT_ENGINE, lua_tostring(L, -1)); + status = SCRIPT_ENGINE_ERROR; + goto finishup; + } + } SCRIPT_ENGINE_DEBUGGING(log_write(LOG_STDOUT, "%s: Matching rules.\n", SCRIPT_ENGINE);) @@ -350,7 +353,7 @@ SCRIPT_ENGINE_DEBUGGING( log_write(LOG_STDOUT, "%s: Script scanning completed.\n", SCRIPT_ENGINE); ) - lua_close(L); + torun_scripts.clear(); if(status != SCRIPT_ENGINE_SUCCESS) { error("%s: Aborting script scan.", SCRIPT_ENGINE); @@ -360,6 +363,11 @@ } } +void script_scan_close(void) { + if (L != NULL) + lua_close(L); +} + int process_mainloop(lua_State *L) { int state; int unfinished = running_scripts.size() + waiting_scripts.size();