diff --git a/Makefile.in b/Makefile.in index 5a319fffe..fd4b37867 100644 --- a/Makefile.in +++ b/Makefile.in @@ -68,7 +68,7 @@ UNINSTALLZENMAP=@UNINSTALLZENMAP@ ifneq (@LIBLUA_LIBS@,) NSE_SRC=nse_main.cc nse_nsock.cc nse_fs.cc nse_nmaplib.cc nse_debug.cc nse_pcrelib.cc nse_binlib.cc nse_bit.cc -NSE_HDRS=nse_main.h nse_nsock.h nse_fs.h nse_nmaplib.h nse_debug.h nse_macros.h nse_pcrelib.h nse_binlib.h nse_bit.h +NSE_HDRS=nse_main.h nse_nsock.h nse_fs.h nse_nmaplib.h nse_debug.h nse_pcrelib.h nse_binlib.h nse_bit.h NSE_OBJS=nse_main.o nse_nsock.o nse_fs.o nse_nmaplib.o nse_debug.o nse_pcrelib.o nse_binlib.o nse_bit.o ifneq (@OPENSSL_LIBS@,) NSE_SRC+=nse_openssl.cc diff --git a/mswin32/nmap.vcproj b/mswin32/nmap.vcproj index 4548235cc..0da20e3ad 100644 --- a/mswin32/nmap.vcproj +++ b/mswin32/nmap.vcproj @@ -463,10 +463,6 @@ RelativePath="..\nse_fs.h" > - - diff --git a/nse_fs.cc b/nse_fs.cc index 3ebcf1edb..e146f9b7e 100644 --- a/nse_fs.cc +++ b/nse_fs.cc @@ -13,12 +13,13 @@ extern "C" { #endif #include "errno.h" -#include "nse_macros.h" #include "nse_fs.h" #include "nmap.h" #include "nmap_error.h" #include "NmapOps.h" +#define MAX_FILENAME_LEN 4096 + extern NmapOps o; static bool filename_is_absolute(const char *file) { @@ -89,13 +90,13 @@ int nse_scandir (lua_State *L) { if (dir == INVALID_HANDLE_VALUE) { error("%s: No files in '%s\\*'", SCRIPT_ENGINE, dirname); - return SCRIPT_ENGINE_ERROR; + return 0; } while(!(morefiles == FALSE && GetLastError() == ERROR_NO_MORE_FILES)) { // if we are looking for files and this file doesn't end with .nse or // is a directory, then we don't look further at it - if(files_or_dirs == FILES) { + if(files_or_dirs == NSE_FILES) { if(!((nse_check_extension(SCRIPT_ENGINE_EXTENSION, entry.cFileName)) && !(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )) { @@ -105,7 +106,7 @@ int nse_scandir (lua_State *L) { // if we are looking for dirs and this dir // isn't a directory, then we don't look further at it - } else if(files_or_dirs == DIRS) { + } else if(files_or_dirs == NSE_DIRS) { if(!(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { morefiles = FindNextFile(dir, &entry); continue; @@ -143,7 +144,7 @@ int nse_scandir (lua_State *L) { dir = opendir(dirname); if(dir == NULL) { error("%s: Could not open directory '%s'.", SCRIPT_ENGINE, dirname); - return SCRIPT_ENGINE_ERROR; + return 0; } // note that if there is a symlink in the dir, we have to rely on @@ -159,7 +160,7 @@ int nse_scandir (lua_State *L) { // if we are looking for files and this file doesn't end with .nse and // isn't a file or a link, then we don't look further at it - if(files_or_dirs == FILES) { + if(files_or_dirs == NSE_FILES) { if(!(nse_check_extension(SCRIPT_ENGINE_EXTENSION, entry->d_name) && (S_ISREG(stat_entry.st_mode) || S_ISLNK(stat_entry.st_mode)) @@ -169,7 +170,7 @@ int nse_scandir (lua_State *L) { // if we are looking for dirs and this dir // isn't a dir or a link, then we don't look further at it - } else if(files_or_dirs == DIRS) { + } else if(files_or_dirs == NSE_DIRS) { if(!(S_ISDIR(stat_entry.st_mode) || S_ISLNK(stat_entry.st_mode) )) { diff --git a/nse_fs.h b/nse_fs.h index 9a7c3d373..e5fe55b4b 100644 --- a/nse_fs.h +++ b/nse_fs.h @@ -9,4 +9,7 @@ int nse_fetchfile_absolute(char *path, size_t path_len, const char *file); int nse_scandir (lua_State *L); +#define NSE_FILES 1 +#define NSE_DIRS 2 + #endif diff --git a/nse_macros.h b/nse_macros.h deleted file mode 100644 index 2e8b52c2d..000000000 --- a/nse_macros.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef NSE_MACROS -#define NSE_MACROS - -#define HOSTRULE "hostrule" -#define HOSTTESTS "hosttests" -#define PORTRULE "portrule" -#define PORTTESTS "porttests" -#define SCRIPTFILES "scriptfiles" -#define ACTION "action" -#define HOST "host" -#define PORT "port" -#define PORT_U "Port" -#define DESCRIPTION "description" -#define AUTHOR "author" -#define LICENSE "license" -#define RUNLEVEL "runlevel" -#define TARGET_CLASS "Target Class" -#define TARGET "target" -#define TYPE "type" -#define ID "id" -#define FILENAME "filename" -#define CATEGORY "category" -#define WAITING "nse_waiting" -#define FILES 1 -#define DIRS 2 - -#define SCRIPT_ENGINE "NSE" -#define SCRIPT_ENGINE_LUA "LUA INTERPRETER" -#define SCRIPT_ENGINE_SUCCESS 0 -#define SCRIPT_ENGINE_ERROR 2 -#define SCRIPT_ENGINE_LUA_ERROR 3 - -#ifdef WIN32 - #define SCRIPT_ENGINE_LUA_DIR "scripts\\" -#else - #define SCRIPT_ENGINE_LUA_DIR "scripts/" -#endif - -#define SCRIPT_ENGINE_LIB_DIR "nselib/" - -#define SCRIPT_ENGINE_DATABASE "script.db" -#define SCRIPT_ENGINE_EXTENSION ".nse" - -#define SCRIPT_ENGINE_LUA_TRY(func) if (func != 0) {\ - error("LUA INTERPRETER in %s:%d: %s", __FILE__, __LINE__, (char *)lua_tostring(L, -1));\ - return SCRIPT_ENGINE_LUA_ERROR;\ -} - -#define SCRIPT_ENGINE_TRY(func) if (func != 0) {\ - return SCRIPT_ENGINE_ERROR;\ -} - -#define ARRAY_LEN(a) ((int)(sizeof(a) / sizeof(a[0]))) - -#define SCRIPT_ENGINE_VERBOSE(msg) if (o.debugging || o.verbose > 0) {msg}; -#define SCRIPT_ENGINE_DEBUGGING(msg) if (o.debugging) {msg}; - -#define MAX_FILENAME_LEN 4096 - -#endif - diff --git a/nse_main.cc b/nse_main.cc index 9da5c496c..98347a2da 100644 --- a/nse_main.cc +++ b/nse_main.cc @@ -9,7 +9,6 @@ #include "nse_pcrelib.h" #include "nse_openssl.h" #include "nse_debug.h" -#include "nse_macros.h" #include "nmap.h" #include "nmap_error.h" @@ -27,6 +26,8 @@ #define NSE_WAITING_TO_RUNNING "NSE_WAITING_TO_RUNNING" #define NSE_DESTRUCTOR "NSE_DESTRUCTOR" +#define MAX_FILENAME_LEN 4096 + extern NmapOps o; int current_hosts = LUA_NOREF; @@ -146,7 +147,7 @@ static int dump_dir (lua_State *L) luaL_checkstring(L, 1); lua_pushcclosure(L, nse_scandir, 0); lua_pushvalue(L, 1); - lua_pushinteger(L, FILES); + lua_pushinteger(L, NSE_FILES); lua_call(L, 2, 1); return 1; } @@ -179,7 +180,7 @@ static int unref (lua_State *L) static int updatedb (lua_State *L) { - lua_pushboolean(L, script_updatedb() == SCRIPT_ENGINE_SUCCESS); + lua_pushboolean(L, script_updatedb()); return 1; } @@ -347,7 +348,7 @@ int script_updatedb (void) " db:write(' } }\\n')\n" "end\n" "db:close()\n"; - int status = SCRIPT_ENGINE_SUCCESS; + int status = 1; lua_State *L; log_write(LOG_STDOUT, "%s: Updating rule database.\n", SCRIPT_ENGINE); @@ -371,7 +372,7 @@ int script_updatedb (void) { error("%s: error while updating Script Database:\n%s\n", SCRIPT_ENGINE, lua_tostring(L, -1)); - status = SCRIPT_ENGINE_ERROR; + status = 0; } else log_write(LOG_STDOUT, "NSE script database updated successfully.\n"); @@ -508,22 +509,21 @@ void nse_destructor (lua_State *L, char what) static lua_State *L_NSE = NULL; -int open_nse (void) +void open_nse (void) { - if (L_NSE != NULL) return SCRIPT_ENGINE_SUCCESS; + if (L_NSE == NULL) + { + if ((L_NSE = luaL_newstate()) == NULL) + fatal("%s: failed to open a Lua state!", SCRIPT_ENGINE); + lua_atpanic(L_NSE, panic); - if ((L_NSE = luaL_newstate()) == NULL) - fatal("%s: failed to open a Lua state!", SCRIPT_ENGINE); - lua_atpanic(L_NSE, panic); - - if (lua_cpcall(L_NSE, init_main, (void *) &o.chosenScripts) != 0) - fatal("%s: failed to initialize the script engine:\n%s\n", SCRIPT_ENGINE, - lua_tostring(L_NSE, -1)); - - return SCRIPT_ENGINE_SUCCESS; + if (lua_cpcall(L_NSE, init_main, (void *) &o.chosenScripts) != 0) + fatal("%s: failed to initialize the script engine:\n%s\n", SCRIPT_ENGINE, + lua_tostring(L_NSE, -1)); + } } -int script_scan (std::vector &targets) +void script_scan (std::vector &targets) { o.current_scantype = SCRIPT_SCAN; @@ -534,10 +534,7 @@ int script_scan (std::vector &targets) { error("%s: Script Engine Scan Aborted.\nAn error was thrown by the " "engine: %s", SCRIPT_ENGINE, lua_tostring(L_NSE, -1)); - return SCRIPT_ENGINE_ERROR; } - else - return SCRIPT_ENGINE_SUCCESS; } void close_nse (void) diff --git a/nse_main.h b/nse_main.h index b834fc21f..7978ed8cf 100644 --- a/nse_main.h +++ b/nse_main.h @@ -29,15 +29,28 @@ typedef std::vector ScriptResults; class Target; -int script_updatedb(); -void script_scan_free(); /* API */ void nse_restore (lua_State *, int); void nse_destructor (lua_State *, char); -int open_nse (void); -int script_scan(std::vector &targets); +void open_nse (void); +void script_scan (std::vector &targets); void close_nse (void); +int script_updatedb (void); + +#define SCRIPT_ENGINE "NSE" + +#ifdef WIN32 +# define SCRIPT_ENGINE_LUA_DIR "scripts\\" +# define SCRIPT_ENGINE_LIB_DIR "nselib\\" +#else +# define SCRIPT_ENGINE_LUA_DIR "scripts/" +# define SCRIPT_ENGINE_LIB_DIR "nselib/" +#endif + +#define SCRIPT_ENGINE_DATABASE "script.db" +#define SCRIPT_ENGINE_EXTENSION ".nse" + #endif diff --git a/nse_nmaplib.cc b/nse_nmaplib.cc index 2733e46a7..e2f0391f9 100644 --- a/nse_nmaplib.cc +++ b/nse_nmaplib.cc @@ -20,7 +20,6 @@ extern "C" { #include "nse_nmaplib.h" #include "nse_nsock.h" -#include "nse_macros.h" #define SCRIPT_ENGINE_PUSHSTRING_NOTNULL(c_str, str) if(c_str != NULL) {\ lua_pushstring(L, c_str); \ @@ -604,7 +603,6 @@ int luaopen_nmap (lua_State *L) lua_pushcclosure(L, luaopen_nsock, 0); lua_pushliteral(L, "nsock"); lua_call(L, 1, 0); - SCRIPT_ENGINE_TRY(l_dnet_open(L)); lua_settop(L, 1); // just nmap lib on stack diff --git a/nse_nsock.cc b/nse_nsock.cc index 1e1b87ab0..8c487d678 100644 --- a/nse_nsock.cc +++ b/nse_nsock.cc @@ -13,7 +13,6 @@ extern "C" #include "nse_nsock.h" #include "nse_main.h" -#include "nse_macros.h" #include "nsock.h" #include "nmap_error.h" @@ -83,6 +82,8 @@ int l_nsock_checkstatus(lua_State * L, nsock_event nse); void l_nsock_trace(nsock_iod nsiod, const char *message, int direction); +static void l_dnet_open(lua_State * L); /* open dnet metatable */ + const char *inet_ntop_both(int af, const void *v_addr, char *ipstring); unsigned short inet_port_both(int af, const void *v_addr); @@ -445,6 +446,8 @@ int luaopen_nsock(lua_State * L) nsp_ssl_init_max_speed(nsp); #endif + l_dnet_open(L); /* open dnet metatable */ + return 0; } @@ -1744,7 +1747,7 @@ static luaL_reg l_dnet[] = { {NULL, NULL} }; -int l_dnet_open(lua_State * L) +void l_dnet_open(lua_State * L) { luaL_newmetatable(L, "dnet"); lua_createtable(L, 0, 5); @@ -1753,7 +1756,6 @@ int l_dnet_open(lua_State * L) lua_pushliteral(L, ""); lua_setfield(L, -2, "__metatable"); // protect metatable lua_pop(L, 1); - return NSOCK_WRAPPER_SUCCESS; } struct l_dnet_udata diff --git a/nse_nsock.h b/nse_nsock.h index 684ee6d65..a49a0e019 100644 --- a/nse_nsock.h +++ b/nse_nsock.h @@ -7,7 +7,6 @@ int l_nsock_loop(int tout); int l_nsock_sleep(lua_State *L); int l_dnet_new(lua_State *); -int l_dnet_open(lua_State *); int l_dnet_get_interface_link(lua_State *); #endif