1
0
mirror of https://github.com/nmap/nmap.git synced 2025-12-06 04:31:29 +00:00

[NSE] Applied change to remove the old nse_macros.h header file.

Here is a mostly exhaustive list of the changes:

o Removes the SCRIPT_ENGINE_* status defines and replaces the
  instances with regular boolean integer returns or changes the
  procedure to return void. The latter case is better generally because
  the caller ignores any status return (e.g. nmap.cc calling open_nse)
  and/or the procedure raises a fatal error when unsuccessful.

o Moves the SCRIPT_ENGINE_LUA_DIR and the like to the nse_main.h header file.

o Removes the use of the SCRIPT_ENGINE_TRY (there was only one left)
  and thus changes the call to l_dnet_open to a void function called
  directly by luaopen_nsock (in nse_nsock.cc) instead of luaopen_nmap
  (in nse_nmaplib.cc). I felt moving the function was also an
  appropriate (but somewhat unrelated to the intent of the patch) change
  as opening the dnet metatable is very related to opening up the
  nsock library. This confines errors in opening the nsock library, including
  opening the dnet metatable, to the call to luaopen_nsock.

o The FILES and DIRS defines are moved in to nse_fs.h where they are
  more appropriate and localalized.
This commit is contained in:
batrick
2009-06-07 01:25:53 +00:00
parent 63d0132758
commit 68bf664db6
10 changed files with 51 additions and 103 deletions

View File

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

View File

@@ -463,10 +463,6 @@
RelativePath="..\nse_fs.h"
>
</File>
<File
RelativePath="..\nse_macros.h"
>
</File>
<File
RelativePath="..\nse_main.h"
>

View File

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

View File

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

View File

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

View File

@@ -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<Target *> &targets)
void script_scan (std::vector<Target *> &targets)
{
o.current_scantype = SCRIPT_SCAN;
@@ -534,10 +534,7 @@ int script_scan (std::vector<Target *> &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)

View File

@@ -29,15 +29,28 @@ typedef std::vector<ScriptResult> 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<Target *> &targets);
void open_nse (void);
void script_scan (std::vector<Target *> &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

View File

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

View File

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

View File

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