mirror of
https://github.com/nmap/nmap.git
synced 2026-01-10 00:19:02 +00:00
Fixed file and directory retrieval problems under windows
This commit is contained in:
31
nmap.cc
31
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) {
|
||||
|
||||
19
nse_init.cc
19
nse_init.cc
@@ -30,6 +30,7 @@ int init_updatedb(lua_State* l);
|
||||
int init_pick_default_categories(std::vector<std::string>& 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<std::string>& 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user