Skip to content

Commit

Permalink
Large recode of nse_init.cc
Browse files Browse the repository at this point in the history
Now does most of it's work through Lua:

From Nmap-dev: "Many of the changes consist of changing how Nmap interfaces
with Lua that were sometimes awkward or inflexible. Most of the functions 
have been made to be callable directly by Lua which offers many technical
advantages: stack management is alleviated, errors are handled cleanly and
are more descriptive, and there is increased reusability."

Additionally:
   -- Moved all lua_State * symbols from "l" to "L". This is to maintain
      consistency with other Lua libraries (convention) and to make our macros portable.
   -- Moved file system manipulation over to nse_fs.cc (from nse_init.cc)
  • Loading branch information
batrick committed May 31, 2008
1 parent 742ff67 commit d0bc640
Show file tree
Hide file tree
Showing 14 changed files with 1,568 additions and 1,393 deletions.
6 changes: 3 additions & 3 deletions Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ INSTALLZENMAP=@INSTALLZENMAP@
UNINSTALLZENMAP=@UNINSTALLZENMAP@

ifneq (@LIBLUA_LIBS@,)
NSE_SRC=nse_main.cc nse_nsock.cc nse_init.cc nse_nmaplib.cc nse_debug.cc nse_pcrelib.cc nse_string.cc
NSE_HDRS=nse_main.h nse_nsock.h nse_init.h nse_nmaplib.h nse_debug.h nse_macros.h nse_pcrelib.h nse_string.h
NSE_OBJS=nse_main.o nse_nsock.o nse_init.o nse_nmaplib.o nse_debug.o nse_pcrelib.o nse_string.o
NSE_SRC=nse_main.cc nse_nsock.cc nse_init.cc nse_fs.cc nse_nmaplib.cc nse_debug.cc nse_pcrelib.cc nse_string.cc
NSE_HDRS=nse_main.h nse_nsock.h nse_init.h nse_fs.h nse_nmaplib.h nse_debug.h nse_macros.h nse_pcrelib.h nse_string.h
NSE_OBJS=nse_main.o nse_nsock.o nse_init.o nse_fs.o nse_nmaplib.o nse_debug.o nse_pcrelib.o nse_string.o
NSESTDLIB=nsestdlib
endif

Expand Down
10 changes: 7 additions & 3 deletions mswin32/nmap.vcproj
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@
RelativePath="..\nse_init.cc"
>
</File>
<File
RelativePath="..\nse_fs.cc"
>
</File>
<File
RelativePath="..\nse_main.cc"
>
Expand Down Expand Up @@ -433,15 +437,15 @@
>
</File>
<File
RelativePath="..\nse_auxiliar.h"
RelativePath="..\nse_debug.h"
>
</File>
<File
RelativePath="..\nse_debug.h"
RelativePath="..\nse_init.h"
>
</File>
<File
RelativePath="..\nse_init.h"
RelativePath="..\nse_fs.h"
>
</File>
<File
Expand Down
4 changes: 2 additions & 2 deletions nmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2645,7 +2645,7 @@ void sigdie(int signo) {
* and is a directory. Otherwise returns 0.
*/

int fileexistsandisreadable(char *pathname) {
int fileexistsandisreadable(const char *pathname) {
char *pathname_buf = strdup(pathname);
int status = 0;

Expand All @@ -2671,7 +2671,7 @@ int fileexistsandisreadable(char *pathname) {
return status;
}

int nmap_fileexistsandisreadable(char* pathname) {
int nmap_fileexistsandisreadable(const char* pathname) {
return fileexistsandisreadable(pathname);
}

Expand Down
2 changes: 1 addition & 1 deletion nmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ const char *tsseqclass2ascii(int seqclass);
const char *seqidx2difficultystr(unsigned long idx);
const char *seqidx2difficultystr1(unsigned long idx);
int nmap_fetchfile(char *filename_returned, int bufferlen, const char *file);
int nmap_fileexistsandisreadable(char* pathname);
int nmap_fileexistsandisreadable(const char* pathname);
int gather_logfile_resumption_state(char *fname, int *myargc, char ***myargv);

#endif /* NMAP_H */
40 changes: 20 additions & 20 deletions nse_debug.cc
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
#include "nse_debug.h"
#include "output.h"

void l_dumpStack(lua_State* l) {
int stack_height = lua_gettop(l);
void l_dumpStack(lua_State *L) {
int stack_height = lua_gettop(L);
int i;

log_write(LOG_PLAIN, "-== Stack Dump Begin ==-\n");
for(i = -1; i >= 0 - stack_height; i--) {
log_write(LOG_PLAIN, "%d: ", i);
l_dumpValue(l, i);
l_dumpValue(L, i);
}

log_write(LOG_PLAIN, "-== Stack Dump End ==-\n");
}

void l_dumpValue(lua_State* l, int i) {
switch (lua_type(l, i))
void l_dumpValue(lua_State *L, int i) {
switch (lua_type(L, i))
{
case LUA_TTABLE:
l_dumpTable(l, i);
l_dumpTable(L, i);
break;
case LUA_TFUNCTION:
l_dumpFunction(l, i);
l_dumpFunction(L, i);
break;
case LUA_TSTRING:
log_write(LOG_PLAIN, "string '%s'\n", lua_tostring(l, i));
log_write(LOG_PLAIN, "string '%s'\n", lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
log_write(LOG_PLAIN, "boolean: %s\n",
lua_toboolean(l, i) ? "true" : "false");
lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
log_write(LOG_PLAIN, "number: %g\n", lua_tonumber(l, i));
log_write(LOG_PLAIN, "number: %g\n", lua_tonumber(L, i));
break;
default:
log_write(LOG_PLAIN, "%s\n", lua_typename(l, lua_type(l, i)));
log_write(LOG_PLAIN, "%s\n", lua_typename(L, lua_type(L, i)));
}
}

void l_dumpTable(lua_State *l, int index) {
void l_dumpTable(lua_State *L, int index) {
log_write(LOG_PLAIN, "table\n");
lua_pushnil(l);
lua_pushnil(L);

if (index<0) --index;
while(lua_next(l, index) != 0)
while(lua_next(L, index) != 0)
{
l_dumpValue(l, -2);
l_dumpValue(l, -1);
lua_pop(l, 1);
l_dumpValue(L, -2);
l_dumpValue(L, -1);
lua_pop(L, 1);
}
}

void l_dumpFunction(lua_State* l, int index) {
void l_dumpFunction(lua_State *L, int index) {
// lua_Debug ar;

log_write(LOG_PLAIN, "function\n");

// lua_pushvalue(l, index);
// lua_getinfo(l, ">n", &ar);
// lua_pushvalue(L, index);
// lua_getinfo(L, ">n", &ar);
//
// log_write(LOG_PLAIN, "\tname: %s %s\n", ar.namewhat, ar.name);
fflush(stdout);
Expand Down
190 changes: 190 additions & 0 deletions nse_fs.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
#ifndef WIN32
#include "dirent.h"
#endif

#include "errno.h"
#include "nse_macros.h"
#include "nse_fs.h"
#include "nmap.h"
#include "nmap_error.h"
#include "NmapOps.h"

extern NmapOps o;

static bool filename_is_absolute(const char *file) {
if (file[0] == '/')
return true;
#ifdef WIN32
if ((file[0] != '\0' && file[1] == ':') || file[0] == '\\')
return true;
#endif
return false;
}

/* This is simply the most portable way to check
* if a file has a given extension.
* The portability comes at the price of reduced
* flexibility.
*/
int nse_check_extension (const char* ext, const char* path)
{
int pathlen = strlen(path);
int extlen = strlen(ext);
if (extlen > pathlen || pathlen > MAX_FILENAME_LEN)
return 0;
else
return strcmp(path + pathlen - extlen, ext) == 0;
}

int nse_fetchfile(char *path, size_t path_len, const char *file) {
int type = nmap_fetchfile(path, path_len, file);

// lets look in <nmap>/scripts too
if(type == 0) {
std::string alt_path = std::string(SCRIPT_ENGINE_LUA_DIR) + std::string(file);
type = nmap_fetchfile(path, path_len, alt_path.c_str());
}

return type;
}

/* This is a modification of nse_fetchfile that first looks for an
* absolute file name.
*/
int nse_fetchfile_absolute(char *path, size_t path_len, const char *file) {
if (filename_is_absolute(file)) {
if (o.debugging > 1)
log_write(LOG_STDOUT, "%s: Trying absolute path %s\n", SCRIPT_ENGINE, file);
Strncpy(path, file, path_len);
return nmap_fileexistsandisreadable(file);
}

return nse_fetchfile(path, path_len, file);
}

#ifdef WIN32

int nse_scandir (lua_State *L)
HANDLE dir;
WIN32_FIND_DATA entry;
std::string path;
BOOL morefiles = FALSE;
char *dirname = luaL_checkstring(L, 1);
int files_or_dirs = luaL_checkint(L, 2);

lua_createtable(L, 100, 0); // 100 files average

dir = FindFirstFile((std::string(dirname) + "\\*").c_str(), &entry);

if (dir == INVALID_HANDLE_VALUE)
{
error("%s: No files in '%s\\*'", SCRIPT_ENGINE, dirname);
return SCRIPT_ENGINE_ERROR;
}

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(!(
(check_extension(SCRIPT_ENGINE_EXTENSION, entry.cFileName) == MATCH)
&& !(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
)) {
morefiles = FindNextFile(dir, &entry);
continue;
}

// 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) {
if(!(
(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
)) {
morefiles = FindNextFile(dir, &entry);
continue;
}

// they have passed an invalid value for files_or_dirs
} else {
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
}

// otherwise we add it to the results
// we assume that dirname ends with a directory separator of some kind
path = std::string(dirname) + "\\" + std::string(entry.cFileName);
lua_pushstring(L, path.c_str());
lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
morefiles = FindNextFile(dir, &entry);
}


return 1;
}

#else

int nse_scandir (lua_State *L) {
DIR* dir;
struct dirent* entry;
struct stat stat_entry;
const char *dirname = luaL_checkstring(L, 1);
int files_or_dirs = luaL_checkint(L, 2);

lua_createtable(L, 100, 0); // 100 files average

dir = opendir(dirname);
if(dir == NULL) {
error("%s: Could not open directory '%s'.", SCRIPT_ENGINE, dirname);
return SCRIPT_ENGINE_ERROR;
}

// note that if there is a symlink in the dir, we have to rely on
// the .nse extension
// if they provide a symlink to a dir which ends with .nse, things
// break :/
while((entry = readdir(dir)) != NULL) {
std::string path = std::string(dirname) + "/" + std::string(entry->d_name);

if(stat(path.c_str(), &stat_entry) != 0)
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);

// 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(!(
(nse_check_extension(SCRIPT_ENGINE_EXTENSION, entry->d_name))
&& (S_ISREG(stat_entry.st_mode)
|| S_ISLNK(stat_entry.st_mode))
)) {
continue;
}

// 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) {
if(!(
(S_ISDIR(stat_entry.st_mode)
|| S_ISLNK(stat_entry.st_mode))
)) {
continue;
}

// they have passed an invalid value for files_or_dirs
} else {
fatal("%s: In: %s:%i This should never happen.",
SCRIPT_ENGINE, __FILE__, __LINE__);
}

// otherwise we add it to the results
lua_pushstring(L, path.c_str());
lua_rawseti(L, -2, lua_objlen(L, -2) + 1);
}

closedir(dir);

return 1;
}

#endif
22 changes: 22 additions & 0 deletions nse_fs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef NSE_FS
#define NSE_FS

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

#include <vector>
#include <string>
#include <string.h>

int nse_check_extension (const char* ext, const char* path);

int nse_fetchfile(char *path, size_t path_len, const char *file);

int nse_fetchfile_absolute(char *path, size_t path_len, const char *file);

int nse_scandir (lua_State *L);

#endif
Loading

0 comments on commit d0bc640

Please sign in to comment.