From 5d362169f22a69a1f0e87fcc5d32e43d5b437f4b Mon Sep 17 00:00:00 2001 From: david Date: Tue, 20 Dec 2011 21:11:21 +0000 Subject: [PATCH] o On Windows, the directory \AppData\Roaming\nmap is now searched for data files. This is the equivalent of $HOME/.nmap on POSIX. --- CHANGELOG | 4 +++ docs/refguide.xml | 6 +++- nmap.cc | 74 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 62 insertions(+), 22 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 7dc79508f..906a6a1a2 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,9 @@ # Nmap Changelog ($Id$); -*-text-*- +o On Windows, the directory \AppData\Roaming\nmap is now + searched for data files. This is the equivalent of $HOME/.nmap on + POSIX. [David] + o [NSE] Applied patch to add additional version information to Mongodb scripts [Martin Swende] diff --git a/docs/refguide.xml b/docs/refguide.xml index b5609831d..0cedeb4c3 100644 --- a/docs/refguide.xml +++ b/docs/refguide.xml @@ -2274,6 +2274,7 @@ found: $NMAPDIRNMAPDIR environment variable ~/.nmap (not searched on Windows).nmap directory +HOME\AppData\Roaming\nmap (only on Windows).nmap directory the directory containing the nmap executable the directory containing the nmap @@ -4100,7 +4101,10 @@ hosts with at least one environment variable. Next comes ~/.nmap.nmap directory - for real and effective UIDs (POSIX systems only). This is + for real and effective UIDs; or on Windows, + HOME\AppData\Roaming\nmap + (where HOME is the user's home directory, + like C:\Users\user). This is followed by the location of the nmap executable and the same location with ../share/nmap appended. Then a compiled-in diff --git a/nmap.cc b/nmap.cc index 62de99cc7..46148809c 100644 --- a/nmap.cc +++ b/nmap.cc @@ -117,6 +117,7 @@ #ifdef WIN32 #include "winfix.h" +#include #endif #if HAVE_OPENSSL @@ -2809,6 +2810,7 @@ static int nmap_fetchfile_sub(char *filename_returned, int bufferlen, const char * --datadir * $NMAPDIR * [Non-Windows only] ~/.nmap + * [Windows only] ...\Users\\AppData\Roaming\nmap * The directory containing the nmap binary * [Non-Windows only] The directory containing the nmap binary plus "/../share/nmap" @@ -2841,11 +2843,58 @@ int nmap_fetchfile(char *filename_returned, int bufferlen, const char *file) { return res; } +#ifdef WIN32 +static int nmap_fetchfile_userdir(char *buf, size_t buflen, + const char *file) { + char appdata[MAX_PATH]; + int res; + + if (SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, appdata) != S_OK) + return 0; + res = Snprintf(buf, buflen, "%s\\nmap\\%s", appdata, file); + if (res <= 0 || res >= buflen) + return 0; + + return fileexistsandisreadable(buf); +} +#else +static int nmap_fetchfile_userdir_uid(char *buf, size_t buflen, + const char *file, int uid) { + struct passwd *pw; + int res; + + pw = getpwuid(uid); + if (pw == NULL) + return 0; + res = Snprintf(buf, buflen, "%s/.nmap/%s", ps->pw_dir, file); + if (res <= 0 || res >= buflen) + return 0; + + return fileexistsandisreadable(buf); +} + +static int nmap_fetchfile_userdir(char *buf, size_t buflen, + const char *file) { + int res; + + res = nmap_fetchfile_userdir_uid(buf, buflen, getuid()); + if (res != 0) + return res; + + if (getuid() != geteuid()) { + res = nmap_fetchfile_userdir_uid(buf, buflen, geteuid()); + if (res != 0) + return res; + } + + return 0; +} +#endif + static int nmap_fetchfile_sub(char *filename_returned, int bufferlen, const char *file) { char *dirptr; int res; int foundsomething = 0; - struct passwd *pw; char dot_buffer[512]; static int warningcount = 0; @@ -2862,26 +2911,9 @@ static int nmap_fetchfile_sub(char *filename_returned, int bufferlen, const char foundsomething = fileexistsandisreadable(filename_returned); } } -#ifndef WIN32 - if (!foundsomething) { - pw = getpwuid(getuid()); - if (pw) { - res = Snprintf(filename_returned, bufferlen, "%s/.nmap/%s", pw->pw_dir, file); - if (res > 0 && res < bufferlen) { - foundsomething = fileexistsandisreadable(filename_returned); - } - } - if (!foundsomething && getuid() != geteuid()) { - pw = getpwuid(geteuid()); - if (pw) { - res = Snprintf(filename_returned, bufferlen, "%s/.nmap/%s", pw->pw_dir, file); - if (res > 0 && res < bufferlen) { - foundsomething = fileexistsandisreadable(filename_returned); - } - } - } - } -#endif + + if (!foundsomething) + foundsomething = nmap_fetchfile_userdir(filename_returned, bufferlen, file); const char *argv0; char *dir;