From 5e3bb361f2fa3874655b3a132e77f3d800216975 Mon Sep 17 00:00:00 2001 From: fyodor Date: Sat, 11 Aug 2007 03:59:18 +0000 Subject: [PATCH] merge soc07 r4860 - Add verbose data file path reporting. Some more changes might be coming, for example to change the conditions under which this information is displayed. --- MACLookup.cc | 5 +++ NmapOps.h | 3 ++ nmap.cc | 3 ++ nmap_rpc.cc | 2 ++ osscan.cc | 2 ++ output.cc | 95 +++++++++++++++++++++++++++++++++++++++++++++++++ output.h | 4 +++ protocols.cc | 2 ++ service_scan.cc | 2 ++ services.cc | 2 ++ 10 files changed, 120 insertions(+) diff --git a/MACLookup.cc b/MACLookup.cc index 20b547ade..3345b9f28 100644 --- a/MACLookup.cc +++ b/MACLookup.cc @@ -103,10 +103,13 @@ /* Character pool memory allocation */ #include "MACLookup.h" +#include "NmapOps.h" #include "nmap.h" #include "nmap_error.h" #include "charpool.h" +extern NmapOps o; + struct MAC_entry { int prefix; /* -1 means none set */ char *vendor; @@ -156,6 +159,8 @@ static void mac_prefix_init() { error("Unable to open %s. Ethernet vendor correlation will not be performed ", filename); return; } + /* Record where this data file was found. */ + o.loaded_data_files["nmap-mac-prefixes"] = filename; while(fgets(line, sizeof(line), fp)) { lineno++; diff --git a/NmapOps.h b/NmapOps.h index a49c557d9..f9258ba15 100644 --- a/NmapOps.h +++ b/NmapOps.h @@ -309,6 +309,9 @@ class NmapOps { the file names defined in this map instead of searching for a matching file. */ std::map requested_data_files; + /* A map from data file names to the paths at which they were actually found. + Only files that were actually read should be in this map. */ + std::map loaded_data_files; bool mass_dns; int resolve_all; char *dns_servers; diff --git a/nmap.cc b/nmap.cc index d6d68709b..6c039919e 100644 --- a/nmap.cc +++ b/nmap.cc @@ -1793,6 +1793,9 @@ int nmap_main(int argc, char *argv[]) { num_host_exp_groups = 0; free(host_exp_group); + if (o.verbose) + printdatafilepaths(); + printfinaloutput(); free_scan_lists(ports); diff --git a/nmap_rpc.cc b/nmap_rpc.cc index 1cbb97a9c..a3b491f15 100644 --- a/nmap_rpc.cc +++ b/nmap_rpc.cc @@ -147,6 +147,8 @@ static void rpc_services_init() { if (!fp) { fatal("Unable to open %s for reading rpc information", filename); } + /* Record where this data file was found. */ + o.loaded_data_files["nmap-rpc"] = filename; while(fgets(line, sizeof(line), fp)) { lineno++; diff --git a/osscan.cc b/osscan.cc index 2021b026f..65456047a 100644 --- a/osscan.cc +++ b/osscan.cc @@ -2138,6 +2138,8 @@ char filename[256]; if (nmap_fetchfile(filename, sizeof(filename), dbname) != 1){ fatal("OS scan requested but I cannot find %s file. It should be in %s, ~/.nmap/ or .", dbname, NMAPDATADIR); } +/* Record where this data file was found. */ +o.loaded_data_files[dbname] = filename; return parse_fingerprint_file(filename); } diff --git a/output.cc b/output.cc index 166dc1d5a..febc9c53d 100644 --- a/output.cc +++ b/output.cc @@ -1807,3 +1807,98 @@ void printfinaloutput() { log_flush_all(); } +/* Returns the position of the last directory separator (slash, also backslash + on Win32) in a path. Returns -1 if none was found. */ +static int find_last_path_separator(const std::string& path) { +#ifndef WIN32 + const char *PATH_SEPARATORS = "/"; +#else + const char *PATH_SEPARATORS = "\\/"; +#endif + + return path.find_last_of(PATH_SEPARATORS); +} + +/* Returns the directory name part of a path (everything up to the last + directory separator. If there is no separator, returns ".". If there is only + one separator and it is the first character, returns "/". */ +static std::string get_dirname(const std::string& path) { + int i; + + i = find_last_path_separator(path); + if (i == -1) + return "."; + else if (i == 0) + return "/"; + else + return path.substr(0, i); +} + +/* Returns the file name part of a path (everything after the last directory + separator). */ +static std::string get_filename(const std::string& path) { + int i; + + i = find_last_path_separator(path); + if (i == -1) + return path; + else + return path.substr(i + 1); +} + +/* A record consisting of a data file name ("nmap-services", "nmap-os-db", + etc.), and the directory and file in which is was found. This is a + broken-down version of what is stored in o.loaded_data_files. It is used in + printdatafilepaths. */ +struct data_file_record { + std::string data_file; + std::string dir; + std::string file; + /* Compares this record to another. First, compare the directory names, then + compare the file names. */ + bool operator<(const struct data_file_record& other) { + int cmp; + + cmp = dir.compare(other.dir); + if (cmp == 0) + cmp = file.compare(other.file); + + return cmp < 0; + } +}; + +/* Prints the names of data files that were loaded and the paths at which they + were found. */ +void printdatafilepaths() { + std::list df; + std::list::iterator iter; + std::map::iterator map_iter; + + /* Copy the elements of o.loaded_data_files (each a (data file, path) pair) to + a list of data_file_records to make them easier to manipulate. */ + for (map_iter = o.loaded_data_files.begin(); map_iter != o.loaded_data_files.end(); map_iter++) { + struct data_file_record r; + r.data_file = map_iter->first; + r.dir = get_dirname(map_iter->second); + r.file = get_filename(map_iter->second); + df.push_back(r); + } + + /* Sort the list, first by directory name, then by file name. This ensures + that records with the same directory name are contiguous. */ + df.sort(); + + /* Iterate over the list and display file names. */ + iter = df.begin(); + while (iter != df.end()) { + std::string dir = iter->dir; + /* Write the directory name. */ + log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT, "Read from %s:", dir.c_str()); + /* Write files in that directory on the same line. */ + while (iter != df.end() && iter->dir == dir) { + log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT, " %s", iter->file.c_str()); + iter++; + } + log_write(LOG_NORMAL|LOG_SKID|LOG_STDOUT, ".\n"); + } +} diff --git a/output.h b/output.h index 2248d7ef8..c4653213e 100644 --- a/output.h +++ b/output.h @@ -205,5 +205,9 @@ void printStatusMessage(); of an Nmap run */ void printfinaloutput(); +/* Prints the names of data files that were loaded and the paths at which they + were found. */ +void printdatafilepaths(); + char* xml_convert (const char* str); #endif /* OUTPUT_H */ diff --git a/protocols.cc b/protocols.cc index 9cfb4099b..e5e9b8ce0 100644 --- a/protocols.cc +++ b/protocols.cc @@ -133,6 +133,8 @@ static int nmap_protocols_init() { if (!fp) { fatal("Unable to open %s for reading protocol information", filename); } + /* Record where this data file was found. */ + o.loaded_data_files["nmap-protocols"] = filename; memset(protocol_table, 0, sizeof(protocol_table)); diff --git a/service_scan.cc b/service_scan.cc index dcbfb4c5a..37431e2fb 100644 --- a/service_scan.cc +++ b/service_scan.cc @@ -1147,6 +1147,8 @@ static void parse_nmap_service_probes(AllProbes *AP) { } parse_nmap_service_probe_file(AP, filename); + /* Record where this data file was found. */ + o.loaded_data_files["nmap-service-probes"] = filename; } AllProbes *AllProbes::global_AP; diff --git a/services.cc b/services.cc index c71622fae..bf4e69cfe 100644 --- a/services.cc +++ b/services.cc @@ -156,6 +156,8 @@ static int nmap_services_init() { if (!fp) { fatal("Unable to open %s for reading service information", filename); } + /* Record where this data file was found. */ + o.loaded_data_files["nmap-services"] = filename; memset(service_table, 0, sizeof(service_table));