From 02669cbfd81136d5a381100dd7ca01a5e80d3eb5 Mon Sep 17 00:00:00 2001 From: david Date: Sat, 26 Mar 2011 06:48:17 +0000 Subject: [PATCH] Defer the calculation of the default XSL stylesheet until it's needed. This is going to depend on argv[0], so it can't happen in the NmapOps constructor, which runs even before main. --- NmapOps.cc | 44 +++++++++++++++++++++++++++++--------------- NmapOps.h | 3 ++- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/NmapOps.cc b/NmapOps.cc index 1052160ed..5f1f08e11 100644 --- a/NmapOps.cc +++ b/NmapOps.cc @@ -195,8 +195,6 @@ static char *filename_to_url(const char *filename) { } void NmapOps::Initialize() { - char tmpxsl[MAXPATHLEN]; - setaf(AF_INET); #if defined WIN32 || defined __amigaos__ isr00t = 1; @@ -272,20 +270,9 @@ void NmapOps::Initialize() { adler32 = false; if (datadir) free(datadir); datadir = NULL; + xsl_stylesheet_set = false; if (xsl_stylesheet) free(xsl_stylesheet); - if (nmap_fetchfile(tmpxsl, sizeof(tmpxsl), "nmap.xsl") == 1) { - xsl_stylesheet = filename_to_url(tmpxsl); - } else { -#if WIN32 - /* Use a relative URL on Windows if nmap_fetchfile failed. It won't work, - but it gives a clue that there is an nmap.xsl somewhere. */ - Strncpy(tmpxsl, "nmap.xsl", sizeof(tmpxsl)); - xsl_stylesheet = strdup(tmpxsl); -#else - Snprintf(tmpxsl, sizeof(tmpxsl), "%s/nmap.xsl", NMAPDATADIR); - xsl_stylesheet = filename_to_url(tmpxsl); -#endif - } + xsl_stylesheet = NULL; spoof_mac_set = false; mass_dns = true; log_errors = false; @@ -572,6 +559,33 @@ void NmapOps::setMaxHostGroupSz(unsigned int sz) { void NmapOps::setXSLStyleSheet(const char *xslname) { if (xsl_stylesheet) free(xsl_stylesheet); xsl_stylesheet = xslname? strdup(xslname) : NULL; + xsl_stylesheet_set = true; +} + +/* Returns the full path or URL that should be printed in the XML + output xml-stylesheet element. Returns NULL if the whole element + should be skipped */ +char *NmapOps::XSLStyleSheet() { + char tmpxsl[MAXPATHLEN]; + + if (xsl_stylesheet_set) + return xsl_stylesheet; + + if (nmap_fetchfile(tmpxsl, sizeof(tmpxsl), "nmap.xsl") == 1) { + xsl_stylesheet = filename_to_url(tmpxsl); + } else { +#if WIN32 + /* Use a relative URL on Windows if nmap_fetchfile failed. It won't work, + but it gives a clue that there is an nmap.xsl somewhere. */ + Strncpy(tmpxsl, "nmap.xsl", sizeof(tmpxsl)); + xsl_stylesheet = strdup(tmpxsl); +#else + Snprintf(tmpxsl, sizeof(tmpxsl), "%s/nmap.xsl", NMAPDATADIR); + xsl_stylesheet = filename_to_url(tmpxsl); +#endif + } + + return xsl_stylesheet; } void NmapOps::setSpoofMACAddress(u8 *mac_data) { diff --git a/NmapOps.h b/NmapOps.h index 9933a5ab4..76a255de2 100644 --- a/NmapOps.h +++ b/NmapOps.h @@ -235,7 +235,7 @@ class NmapOps { /* Returns the full path or URL that should be printed in the XML output xml-stylesheet element. Returns NULL if the whole element should be skipped */ - char *XSLStyleSheet() { return xsl_stylesheet; } + char *XSLStyleSheet(); /* Sets the spoofed MAC address */ void setSpoofMACAddress(u8 *mac_data); @@ -362,6 +362,7 @@ class NmapOps { struct timeval start_time; bool pTrace; // Whether packet tracing has been enabled bool vTrace; // Whether version tracing has been enabled + bool xsl_stylesheet_set; char *xsl_stylesheet; u8 spoof_mac[6]; bool spoof_mac_set;