diff -urNb nmap-5.51/NmapOps.cc nmap-5.51-noclobber-unique/NmapOps.cc --- nmap-5.51/NmapOps.cc 2011-01-27 23:49:15.000000000 +0200 +++ nmap-5.51-noclobber-unique/NmapOps.cc 2011-09-13 14:17:31.000000000 +0200 @@ -262,6 +262,7 @@ sctpinitscan = 0; sctpcookieechoscan = 0; append_output = 0; + no_clobber = 0; memset(logfd, 0, sizeof(FILE *) * LOG_NUM_FILES); ttl = -1; badsum = 0; diff -urNb nmap-5.51/NmapOps.h nmap-5.51-noclobber-unique/NmapOps.h --- nmap-5.51/NmapOps.h 2011-01-27 23:49:15.000000000 +0200 +++ nmap-5.51-noclobber-unique/NmapOps.h 2011-09-13 14:17:31.000000000 +0200 @@ -299,6 +299,7 @@ int noresolve; int noportscan; int append_output; /* Append to any output files rather than overwrite */ + int no_clobber; /* Exit if an attempt is made to write to an existing output file */ FILE *logfd[LOG_NUM_FILES]; FILE *nmap_stdout; /* Nmap standard output */ int ttl; // Time to live diff -urNb nmap-5.51/nmap.cc nmap-5.51-noclobber-unique/nmap.cc --- nmap-5.51/nmap.cc 2011-02-09 04:37:53.000000000 +0200 +++ nmap-5.51-noclobber-unique/nmap.cc 2011-09-13 14:17:31.000000000 +0200 @@ -298,6 +298,7 @@ " --iflist: Print host interfaces and routes (for debugging)\n" " --log-errors: Log errors/warnings to the normal-format output file\n" " --append-output: Append to rather than clobber specified output files\n" + " --no-clobber: Exit if nmap would overwrite the specified output file\n" " --resume : Resume an aborted scan\n" " --stylesheet : XSL stylesheet to transform XML output to HTML\n" " --webxml: Reference stylesheet from Nmap.Org for more portable XML\n" @@ -569,6 +570,7 @@ {"mtu", required_argument, 0, 0}, {"append_output", no_argument, 0, 0}, {"append-output", no_argument, 0, 0}, + {"no-clobber", no_argument, 0, 0}, {"noninteractive", no_argument, 0, 0}, {"spoof_mac", required_argument, 0, 0}, {"spoof-mac", required_argument, 0, 0}, @@ -739,6 +741,8 @@ o.requested_data_files["nmap-service-probes"] = optarg; } else if (optcmp(long_options[option_index].name, "append-output") == 0) { o.append_output = 1; + } else if (optcmp(long_options[option_index].name, "no-clobber") == 0) { + o.no_clobber = 1; } else if (strcmp(long_options[option_index].name, "noninteractive") == 0) { o.noninteractive = true; } else if (optcmp(long_options[option_index].name, "spoof-mac") == 0) { @@ -1287,22 +1291,21 @@ format_ip_options(o.ipoptions, o.ipoptionslen)); } - /* Open the log files, now that we know whether the user wants them appended - or overwritten */ + // Open the log files if (normalfilename) { - log_open(LOG_NORMAL, o.append_output, normalfilename); + log_open(LOG_NORMAL, normalfilename); free(normalfilename); } if (machinefilename) { - log_open(LOG_MACHINE, o.append_output, machinefilename); + log_open(LOG_MACHINE, machinefilename); free(machinefilename); } if (kiddiefilename) { - log_open(LOG_SKID, o.append_output, kiddiefilename); + log_open(LOG_SKID, kiddiefilename); free(kiddiefilename); } if (xmlfilename) { - log_open(LOG_XML, o.append_output, xmlfilename); + log_open(LOG_XML, xmlfilename); free(xmlfilename); } diff -urNb nmap-5.51/output.cc nmap-5.51-noclobber-unique/output.cc --- nmap-5.51/output.cc 2011-01-21 02:04:16.000000000 +0200 +++ nmap-5.51-noclobber-unique/output.cc 2011-09-13 14:17:31.000000000 +0200 @@ -1061,10 +1061,14 @@ fflush(stderr); } -/* Open a log descriptor of the type given to the filename given. If - append is nonzero, the file will be appended instead of clobbered if - it already exists. If the file does not exist, it will be created */ -int log_open(int logt, int append, char *filename) { +/* Open a log descriptor of the type given to the filename given. If the file + does not exist, it will be created. If --append or --no-clobber have been + specified on the command line and the output file already exists, this + function will either append to that file in the case of "append" or create a + unique file by appending the first available integer to the end e.g. + foo.nmap.1 +*/ +int log_open(int logt, char *filename) { int i = 0; if (logt <= 0 || logt > LOG_FILE_MASK) return -1; @@ -1080,8 +1084,29 @@ if (!o.nmap_stdout) fatal("Could not assign %s to stdout for writing", DEVNULL); } else { + // Append to existing file if (o.append_output) o.logfd[i] = fopen(filename, "a"); + // Create unique file to prevent overwrite of existing + else if (o.no_clobber) { + o.logfd[i] = fopen(filename, "wx"); + if (o.logfd[i] == NULL && errno == EEXIST) { + char unique_name [sizeof(filename)+9]; + int j = 0; + while (o.logfd[i] == NULL && errno == EEXIST && j < 99) { + j++; + snprintf(unique_name,sizeof(filename)+9,"%s.%d",filename,j); + o.logfd[i] = fopen(unique_name, "wx"); + } + if (!o.logfd[i]) { + fatal("Failed to open %s output file %s for writing, possibly because \ +you have too many same named output files.", logtypes[i], unique_name); + } else { + error("warning: The %s output file exists and --no-clobber was specified, using \ +%s instead.", filename, unique_name); + } + } + } else o.logfd[i] = fopen(filename, "w"); if (!o.logfd[i]) diff -urNb nmap-5.51/output.h nmap-5.51-noclobber-unique/output.h --- nmap-5.51/output.h 2011-01-21 02:04:16.000000000 +0200 +++ nmap-5.51-noclobber-unique/output.h 2011-09-13 14:17:31.000000000 +0200 @@ -180,10 +180,14 @@ corresponding logs immediately */ void log_flush_all(); -/* Open a log descriptor of the type given to the filename given. If - append is nonzero, the file will be appended instead of clobbered if - it already exists. If the file does not exist, it will be created */ -int log_open(int logt, int append, char *filename); +/* Open a log descriptor of the type given to the filename given. If the file + does not exist, it will be created. If --append or --no-clobber have been + specified on the command line and the output file already exists, this + function will either append to that file in the case of "append" or create a + unique file by appending the first available integer to the end e.g. + foo.nmap.1 +*/ +int log_open(int logt, char *filename); /* Output the list of ports scanned to the top of machine parseable logs (in a comment, unfortunately). The items in ports should be