From b642be63c5c3c3c26b6ddf04025ab3d753169d35 Mon Sep 17 00:00:00 2001 From: fyodor Date: Sat, 11 Aug 2007 03:31:22 +0000 Subject: [PATCH] merge soc07 r4803 - Implement the --services option using a filename translation map and some additional logic in nmap_fetchfile. --- NmapOps.h | 5 +++++ nmap.cc | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/NmapOps.h b/NmapOps.h index 9f86f5966..cb9ef08de 100644 --- a/NmapOps.h +++ b/NmapOps.h @@ -299,6 +299,11 @@ class NmapOps { int ttl; // Time to live int badsum; char *datadir; + /* A map from abstract data file names like "nmap-services" and "nmap-os-db" + to paths which have been requested by the user. nmap_fetchfile will return + the file names defined in this map instead of searching for a matching + file. */ + std::map requested_data_files; bool mass_dns; int resolve_all; char *dns_servers; diff --git a/nmap.cc b/nmap.cc index 3c98b7e51..404bbf403 100644 --- a/nmap.cc +++ b/nmap.cc @@ -506,6 +506,7 @@ int nmap_main(int argc, char *argv[]) { {"version", no_argument, 0, 'V'}, {"verbose", no_argument, 0, 'v'}, {"datadir", required_argument, 0, 0}, + {"services", required_argument, 0, 0}, {"debug", optional_argument, 0, 'd'}, {"help", no_argument, 0, 'h'}, {"iflist", no_argument, 0, 0}, @@ -718,6 +719,9 @@ int nmap_main(int argc, char *argv[]) { } } else if (strcmp(long_options[option_index].name, "datadir") == 0) { o.datadir = strdup(optarg); + } else if (strcmp(long_options[option_index].name, "services") == 0) { + o.requested_data_files["nmap-services"] = optarg; + o.fastscan++; } else if (optcmp(long_options[option_index].name, "append-output") == 0) { o.append_output = 1; } else if (strcmp(long_options[option_index].name, "noninteractive") == 0) { @@ -2580,8 +2584,11 @@ int nmap_fetchfile(char *filename_returned, int bufferlen, char *file) { struct passwd *pw; char dot_buffer[512]; static int warningcount = 0; + std::map::iterator iter; - /* First we try [--datadir]/file, then $NMAPDIR/file + /* First, check the map of requested data file names. If there's an entry for + file, use it and return. + Otherwise, we try [--datadir]/file, then $NMAPDIR/file next we try ~user/nmap/file then we try NMAPDATADIR/file <--NMAPDATADIR finally we try ./file @@ -2591,6 +2598,17 @@ int nmap_fetchfile(char *filename_returned, int bufferlen, char *file) { --datadir -> $NMAPDIR -> nmap.exe directory -> NMAPDATADIR -> . */ + /* Check the map of requested data file names. */ + iter = o.requested_data_files.find(file); + if (iter != o.requested_data_files.end()) { + Strncpy(filename_returned, iter->second.c_str(), bufferlen); + /* If a special file name was requested, we must not return any other file + name. Return a positive result even if the file doesn't exist or is not + readable. It is the caller's responsibility to report the error if the + file can't be accessed. */ + return fileexistsandisreadable(filename_returned) || 1; + } + if (o.datadir) { res = snprintf(filename_returned, bufferlen, "%s/%s", o.datadir, file); if (res > 0 && res < bufferlen) {