From 35432086701e7d6e7d325ab6ea081dfbc8a99e23 Mon Sep 17 00:00:00 2001 From: diman Date: Fri, 29 Dec 2006 00:44:49 +0000 Subject: [PATCH] Fixed file and directory retrieval problems under windows --- nmap.cc | 31 ++++++++++++++++++++++--------- nse_init.cc | 19 ++++++++++++++++--- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/nmap.cc b/nmap.cc index fd34d957c..e24cf9a0b 100644 --- a/nmap.cc +++ b/nmap.cc @@ -2315,18 +2315,31 @@ void sigdie(int signo) { */ int fileexistsandisreadable(char *pathname) { + char *pathname_buf = strdup(pathname); + int status = -1; + +#ifdef WIN32 + // stat on windows only works for "dir_name" not for "dir_name/" or "dir_name\\" + int pathname_len = strlen(pathname_buf); + char last_char = pathname_buf[pathname_len - 1]; + + if( last_char == '/' + || last_char == '\\') + pathname_buf[pathname_len - 1] = '\0'; + +#endif + struct stat st; - if (stat(pathname, &st) == -1) - return 0; + if (stat(pathname_buf, &st) == -1) + status = 0; + else if (!S_ISDIR(st.st_mode) && (access(pathname_buf, R_OK) != -1)) + status = 1; + else if ((st.st_mode & S_IFDIR) && (access(pathname_buf, R_OK) != -1)) + status = 2; - if (!S_ISDIR(st.st_mode) && (access(pathname, R_OK) != -1)) - return 1; - - if ((st.st_mode & S_IFDIR) && (access(pathname, R_OK) != -1)) - return 2; - - return 0; + free(pathname_buf); + return status; } int nmap_fileexistsandisreadable(char* pathname) { diff --git a/nse_init.cc b/nse_init.cc index 89d6b48d3..fcb3e9553 100644 --- a/nse_init.cc +++ b/nse_init.cc @@ -30,6 +30,7 @@ int init_updatedb(lua_State* l); int init_pick_default_categories(std::vector& chosenScripts); int check_extension(const char* ext, const char* path); +std::string get_filename(std::string str); extern NmapOps o; @@ -242,7 +243,7 @@ int init_updatedb(lua_State* l) { fprintf(scriptdb, "Entry{ category = \"%s\", filename = \"%s\" }\n", lua_tostring(l, -1), - c_iter); + get_filename(std::string(c_iter)).c_str()); lua_pop(l, 1); } lua_pop(l, 1); // pop the categories table @@ -345,12 +346,14 @@ end\n"; for(iter = files.begin(); iter != files.end(); iter++) { c_iter = strdup((*iter).c_str()); type = init_fetchfile(script_path, sizeof(script_path), c_iter); - free(c_iter); if(type != 1) { error("%s: %s is not a file.", SCRIPT_ENGINE, c_iter); return SCRIPT_ENGINE_ERROR; } + + free(c_iter); + SCRIPT_ENGINE_TRY(init_loadfile(l, script_path)); } @@ -373,6 +376,15 @@ int init_fetchfile(char *path, size_t path_len, char* file) { return type; } +std::string get_filename(std::string str) { + int pos = MAX( (int) str.rfind('\\'), (int) str.rfind('/')); + + if(pos > -1) + return str.substr(pos + 1); + else + return str; +} + /* This is simply the most portable way to check * if a file has a given extension. * The portability comes at the price of reduced @@ -456,7 +468,8 @@ int init_scandir(char* dirname, std::vector& result, int files_or_d } // otherwise we add it to the results - path = std::string(dirname) + "\\" + std::string(entry.cFileName); + // we assume that dirname ends with a directory separator of some kind + path = std::string(dirname) + std::string(entry.cFileName); result.push_back(path); morefiles = FindNextFile(dir, &entry); }