1 
2 /***************************************************************************
3  * NmapOps.cc -- The NmapOps class contains global options, mostly based   *
4  * on user-provided command-line settings.                                 *
5  *                                                                         *
6  ***********************IMPORTANT NMAP LICENSE TERMS************************
7  *                                                                         *
8  * The Nmap Security Scanner is (C) 1996-2020 Insecure.Com LLC ("The Nmap  *
9  * Project"). Nmap is also a registered trademark of the Nmap Project.     *
10  *                                                                         *
11  * This program is distributed under the terms of the Nmap Public Source   *
12  * License (NPSL). The exact license text applying to a particular Nmap    *
13  * release or source code control revision is contained in the LICENSE     *
14  * file distributed with that version of Nmap or source code control       *
15  * revision. More Nmap copyright/legal information is available from       *
16  * https://nmap.org/book/man-legal.html, and further information on the    *
17  * NPSL license itself can be found at https://nmap.org/npsl. This header  *
18  * summarizes some key points from the Nmap license, but is no substitute  *
19  * for the actual license text.                                            *
20  *                                                                         *
21  * Nmap is generally free for end users to download and use themselves,    *
22  * including commercial use. It is available from https://nmap.org.        *
23  *                                                                         *
24  * The Nmap license generally prohibits companies from using and           *
25  * redistributing Nmap in commercial products, but we sell a special Nmap  *
26  * OEM Edition with a more permissive license and special features for     *
27  * this purpose. See https://nmap.org/oem                                  *
28  *                                                                         *
29  * If you have received a written Nmap license agreement or contract       *
30  * stating terms other than these (such as an Nmap OEM license), you may   *
31  * choose to use and redistribute Nmap under those terms instead.          *
32  *                                                                         *
33  * The official Nmap Windows builds include the Npcap software             *
34  * (https://npcap.org) for packet capture and transmission. It is under    *
35  * separate license terms which forbid redistribution without special      *
36  * permission. So the official Nmap Windows builds may not be              *
37  * redistributed without special permission (such as an Nmap OEM           *
38  * license).                                                               *
39  *                                                                         *
40  * Source is provided to this software because we believe users have a     *
41  * right to know exactly what a program is going to do before they run it. *
42  * This also allows you to audit the software for security holes.          *
43  *                                                                         *
44  * Source code also allows you to port Nmap to new platforms, fix bugs,    *
45  * and add new features.  You are highly encouraged to submit your         *
46  * changes as a Github PR or by email to the dev@nmap.org mailing list     *
47  * for possible incorporation into the main distribution. Unless you       *
48  * specify otherwise, it is understood that you are offering us very       *
49  * broad rights to use your submissions as described in the Nmap Public    *
50  * Source License Contributor Agreement. This is important because we      *
51  * fund the project by selling licenses with various terms, and also       *
52  * because the inability to relicense code has caused devastating          *
53  * problems for other Free Software projects (such as KDE and NASM).       *
54  *                                                                         *
55  * The free version of Nmap is distributed in the hope that it will be     *
56  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of  *
57  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Warranties,        *
58  * indemnification and commercial support are all available through the    *
59  * Npcap OEM program--see https://nmap.org/oem.                            *
60  *                                                                         *
61  ***************************************************************************/
62 
63 /* $Id: NmapOps.cc 38078 2020-10-02 16:12:22Z dmiller $ */
64 #ifdef WIN32
65 #include "winfix.h"
66 #endif
67 #include "nmap.h"
68 #include "nbase.h"
69 #include "NmapOps.h"
70 #include "osscan.h"
71 #include "nmap_error.h"
72 
73 NmapOps o;
74 
NmapOps()75 NmapOps::NmapOps() {
76   datadir = NULL;
77   xsl_stylesheet = NULL;
78   Initialize();
79 }
80 
~NmapOps()81 NmapOps::~NmapOps() {
82   if (xsl_stylesheet) {
83     free(xsl_stylesheet);
84     xsl_stylesheet = NULL;
85   }
86 
87   if (reference_FPs) {
88     delete reference_FPs;
89     reference_FPs = NULL;
90   }
91 
92   if (dns_servers) {
93     free(dns_servers);
94     dns_servers = NULL;
95   }
96   if (extra_payload) {
97     free(extra_payload);
98     extra_payload = NULL;
99   }
100   if (ipoptions) {
101     free(ipoptions);
102     ipoptions = NULL;
103   }
104   if (portlist) {
105     free(portlist);
106     portlist = NULL;
107   }
108   if (exclude_portlist) {
109     free(exclude_portlist);
110     exclude_portlist = NULL;
111   }
112   if (proxy_chain) {
113     nsock_proxychain_delete(proxy_chain);
114     proxy_chain = NULL;
115   }
116   if (exclude_spec) {
117     free(exclude_spec);
118     exclude_spec = NULL;
119   }
120   if (idleProxy) {
121     free(idleProxy);
122     idleProxy = NULL;
123   }
124   if (datadir) {
125     free(datadir);
126     datadir = NULL;
127   }
128 
129 #ifndef NOLUA
130   if (scriptversion || script)
131     close_nse();
132   if (scriptargs) {
133     free(scriptargs);
134     scriptargs = NULL;
135   }
136 #endif
137 }
138 
ReInit()139 void NmapOps::ReInit() {
140   Initialize();
141 }
142 
143 // no setpf() because it is based on setaf() values
pf()144 int NmapOps::pf() {
145   return (af() == AF_INET)? PF_INET : PF_INET6;
146 }
147 
SourceSockAddr(struct sockaddr_storage * ss,size_t * ss_len)148 int NmapOps::SourceSockAddr(struct sockaddr_storage *ss, size_t *ss_len) {
149   if (sourcesocklen <= 0)
150     return 1;
151   assert(sourcesocklen <= sizeof(*ss));
152   if (ss)
153     memcpy(ss, &sourcesock, sourcesocklen);
154   if (ss_len)
155     *ss_len = sourcesocklen;
156   return 0;
157 }
158 
159 /* Returns a const pointer to the source address if set, or NULL if unset. */
SourceSockAddr() const160 const struct sockaddr_storage *NmapOps::SourceSockAddr() const {
161   if (sourcesock.ss_family == AF_UNSPEC)
162     return NULL;
163   else
164     return &sourcesock;
165 }
166 
167 /* Note that it is OK to pass in a sockaddr_in or sockaddr_in6 casted
168      to sockaddr_storage */
setSourceSockAddr(struct sockaddr_storage * ss,size_t ss_len)169 void NmapOps::setSourceSockAddr(struct sockaddr_storage *ss, size_t ss_len) {
170   assert(ss_len > 0 && ss_len <= sizeof(*ss));
171   memcpy(&sourcesock, ss, ss_len);
172   sourcesocklen = ss_len;
173 }
174 
175 // Number of seconds since getStartTime().  The current time is an
176 // optional argument to avoid an extra gettimeofday() call.
TimeSinceStart(const struct timeval * now)177 float NmapOps::TimeSinceStart(const struct timeval *now) {
178   struct timeval tv;
179   if (!now)
180     gettimeofday(&tv, NULL);
181   else tv = *now;
182 
183   return TIMEVAL_FSEC_SUBTRACT(tv, start_time);
184 }
185 
186 // Convert a filename to a file:// URL. The return value must be freed.
filename_to_url(const char * filename)187 static char *filename_to_url(const char *filename) {
188   std::string url(filename);
189   char percent_buffer[10];
190 
191 #if WIN32
192   for (std::string::iterator p = url.begin(); p != url.end(); p++) {
193     if (*p == '\\')
194       *p = '/';
195   }
196   /* Put a pseudo-root directory before "C:/" or whatever. */
197   url = "/" + url;
198 #endif
199 
200   /* Percent-encode any troublesome characters. */
201   std::string::size_type i = 0;
202   /* See RFC 3986, section 3.3 "Path" for allowed characters. */
203   while ((i = url.find_first_of("?#[]%", i)) != std::string::npos) {
204     Snprintf(percent_buffer, sizeof(percent_buffer), "%%%02X", url[i]);
205     url.replace(i, 1, percent_buffer);
206     i += strlen(percent_buffer);
207   }
208 
209   url = "file://" + url;
210 
211   return strdup(url.c_str());
212 }
213 
Initialize()214 void NmapOps::Initialize() {
215   setaf(AF_INET);
216 #if defined WIN32 || defined __amigaos__
217   isr00t = 1;
218 #else
219   if (getenv("NMAP_PRIVILEGED"))
220     isr00t = 1;
221   else if (getenv("NMAP_UNPRIVILEGED"))
222     isr00t = 0;
223   else
224     isr00t = !(geteuid());
225 #endif
226   have_pcap = true;
227   debugging = 0;
228   verbose = 0;
229   min_packet_send_rate = 0.0; /* Unset. */
230   max_packet_send_rate = 0.0; /* Unset. */
231   stats_interval = 0.0; /* Unset. */
232   randomize_hosts = false;
233   randomize_ports = true;
234   sendpref = PACKET_SEND_NOPREF;
235   spoofsource = false;
236   fastscan = false;
237   device[0] = '\0';
238   ping_group_sz = PING_GROUP_SZ;
239   nogcc = false;
240   generate_random_ips = false;
241   reference_FPs = NULL;
242   magic_port = 33000 + (get_random_uint() % 31000);
243   magic_port_set = false;
244   timing_level = 3;
245   max_parallelism = 0;
246   min_parallelism = 0;
247   max_os_tries = 5;
248   max_rtt_timeout = MAX_RTT_TIMEOUT;
249   min_rtt_timeout = MIN_RTT_TIMEOUT;
250   initial_rtt_timeout = INITIAL_RTT_TIMEOUT;
251   max_retransmissions = MAX_RETRANSMISSIONS;
252   min_host_group_sz = 1;
253   max_host_group_sz = 100000; // don't want to be restrictive unless user sets
254   max_tcp_scan_delay = MAX_TCP_SCAN_DELAY;
255   max_udp_scan_delay = MAX_UDP_SCAN_DELAY;
256   max_sctp_scan_delay = MAX_SCTP_SCAN_DELAY;
257   max_ips_to_scan = 0;
258   extra_payload_length = 0;
259   extra_payload = NULL;
260   scan_delay = 0;
261   open_only = false;
262   scanflags = -1;
263   defeat_rst_ratelimit = false;
264   defeat_icmp_ratelimit = false;
265   resume_ip.ss_family = AF_UNSPEC;
266   osscan_limit = false;
267   osscan_guess = false;
268   numdecoys = 0;
269   decoyturn = -1;
270   osscan = false;
271   servicescan = false;
272   override_excludeports = false;
273   version_intensity = 7;
274   pingtype = PINGTYPE_UNKNOWN;
275   listscan = ackscan = bouncescan = connectscan = 0;
276   nullscan = xmasscan = fragscan = synscan = windowscan = 0;
277   maimonscan = idlescan = finscan = udpscan = ipprotscan = 0;
278   noportscan = noresolve = false;
279   sctpinitscan = 0;
280   sctpcookieechoscan = 0;
281   append_output = false;
282   memset(logfd, 0, sizeof(FILE *) * LOG_NUM_FILES);
283   ttl = -1;
284   badsum = false;
285   nmap_stdout = stdout;
286   gettimeofday(&start_time, NULL);
287   pTrace = vTrace = false;
288   reason = false;
289   adler32 = false;
290   if (datadir) free(datadir);
291   datadir = NULL;
292   xsl_stylesheet_set = false;
293   if (xsl_stylesheet) free(xsl_stylesheet);
294   xsl_stylesheet = NULL;
295   spoof_mac_set = false;
296   mass_dns = true;
297   deprecated_xml_osclass = false;
298   always_resolve = false;
299   resolve_all = false;
300   dns_servers = NULL;
301   implicitARPPing = true;
302   numhosts_scanned = 0;
303   numhosts_up = 0;
304   numhosts_scanning = 0;
305   noninteractive = false;
306   current_scantype = STYPE_UNKNOWN;
307   ipoptions = NULL;
308   ipoptionslen = 0;
309   ipopt_firsthop = 0;
310   ipopt_lasthop  = 0;
311   release_memory = false;
312   topportlevel = -1;
313 #ifndef NOLUA
314   script = false;
315   scriptargs = NULL;
316   scriptversion = false;
317   scripttrace = false;
318   scriptupdatedb = false;
319   scripthelp = false;
320   scripttimeout = 0;
321   chosenScripts.clear();
322 #endif
323   memset(&sourcesock, 0, sizeof(sourcesock));
324   sourcesocklen = 0;
325   excludefd = NULL;
326   exclude_spec = NULL;
327   inputfd = NULL;
328   idleProxy = NULL;
329   portlist = NULL;
330   exclude_portlist = NULL;
331   proxy_chain = NULL;
332   resuming = false;
333   discovery_ignore_rst = false;
334 }
335 
SCTPScan()336 bool NmapOps::SCTPScan() {
337   return sctpinitscan|sctpcookieechoscan;
338 }
339 
TCPScan()340 bool NmapOps::TCPScan() {
341   return ackscan|bouncescan|connectscan|finscan|idlescan|maimonscan|nullscan|synscan|windowscan|xmasscan;
342 }
343 
UDPScan()344 bool NmapOps::UDPScan() {
345   return udpscan;
346 }
347 
RawScan()348 bool NmapOps::RawScan() {
349   if (ackscan||finscan||idlescan||ipprotscan||maimonscan||nullscan||osscan||synscan||udpscan||windowscan||xmasscan||sctpinitscan||sctpcookieechoscan||traceroute)
350     return true;
351   if (pingtype & (PINGTYPE_ICMP_PING|PINGTYPE_ICMP_MASK|PINGTYPE_ICMP_TS|PINGTYPE_TCP_USE_ACK|PINGTYPE_UDP|PINGTYPE_PROTO|PINGTYPE_SCTP_INIT))
352     return true;
353   /* A SYN scan will only generate raw packets if nmap is running as root.
354      Otherwise, it becomes a connect scan. */
355   if ((pingtype & PINGTYPE_TCP_USE_SYN) && isr00t)
356     return true;
357 
358    return false;
359 }
360 
361 
ValidateOptions()362 void NmapOps::ValidateOptions() {
363         const char *privreq = "root privileges.";
364 #ifdef WIN32
365         if (!have_pcap)
366           privreq = "Npcap, but it seems to be missing.\n\
367 Npcap is available from https://npcap.org. The Npcap driver service must\n\
368 be started by an administrator before Npcap can be used. Running nmap.exe\n\
369 will open a UAC dialog where you can start the service if you have\n\
370 administrator privileges.";
371 #endif
372 
373 
374   /* Insure that at least one scantype is selected */
375   if (!noportscan && !(TCPScan() || UDPScan() || SCTPScan() || ipprotscan)) {
376     if (isr00t)
377       synscan++;
378     else connectscan++;
379     //    if (verbose) error("No TCP, UDP, SCTP or ICMP scantype specified, assuming %s scan. Use -sn if you really don't want to portscan (and just want to see what hosts are up).", synscan? "SYN Stealth" : "vanilla tcp connect()");
380   }
381 
382   if (pingtype != PINGTYPE_NONE && spoofsource) {
383     error("WARNING: If -S is being used to fake your source address, you may also have to use -e <interface> and -Pn .  If you are using it to specify your real source address, you can ignore this warning.");
384   }
385 
386   if (pingtype != PINGTYPE_NONE && idlescan) {
387     error("WARNING: Many people use -Pn w/Idlescan to prevent pings from their true IP.  On the other hand, timing info Nmap gains from pings can allow for faster, more reliable scans.");
388     sleep(2); /* Give ppl a chance for ^C :) */
389   }
390 
391  if (numdecoys > 1 && idlescan) {
392     error("WARNING: Your decoys won't be used in the Idlescan portion of your scanning (although all packets sent to the target are spoofed anyway");
393   }
394 
395  if (connectscan && spoofsource) {
396     error("WARNING: -S will only affect the source address used in a connect() scan if you specify one of your own addresses.  Use -sS or another raw scan if you want to completely spoof your source address, but then you need to know what you're doing to obtain meaningful results.");
397   }
398 
399  if ((pingtype & PINGTYPE_UDP) && (!isr00t)) {
400    fatal("Sorry, UDP Ping (-PU) only works if you are root (because we need to read raw responses off the wire)");
401  }
402 
403  if ((pingtype & PINGTYPE_SCTP_INIT) && (!isr00t)) {
404    fatal("Sorry, SCTP INIT Ping (-PY) only works if you are root (because we need to read raw responses off the wire)");
405   }
406 
407  if ((pingtype & PINGTYPE_PROTO) && (!isr00t)) {
408    fatal("Sorry, IPProto Ping (-PO) only works if you are root (because we need to read raw responses off the wire)");
409  }
410 
411  if (ipprotscan && (TCPScan() || UDPScan() || SCTPScan())) {
412    fatal("Sorry, the IPProtoscan (-sO) must currently be used alone rather than combined with other scan types.");
413  }
414 
415  if (noportscan && (TCPScan() || UDPScan() || SCTPScan() || ipprotscan)) {
416    fatal("-sL and -sn (skip port scan) are not valid with any other scan types");
417  }
418 
419  if (af() == AF_INET6 && (pingtype & (PINGTYPE_ICMP_MASK|PINGTYPE_ICMP_TS))) {
420    fatal("ICMP Timestamp and Address Mask pings are only valid for IPv4.");
421  }
422 
423  if (sendpref == PACKET_SEND_NOPREF) {
424 #ifdef WIN32
425    sendpref = PACKET_SEND_ETH_STRONG;
426 #else
427    sendpref = PACKET_SEND_IP_WEAK;
428 #endif
429  }
430 /* We start with stuff users should not do if they are not root */
431   if (!isr00t) {
432 
433     if (ackscan|finscan|idlescan|ipprotscan|maimonscan|nullscan|synscan|udpscan|windowscan|xmasscan|sctpinitscan|sctpcookieechoscan) {
434       fatal("You requested a scan type which requires %s", privreq);
435     }
436 
437     if (numdecoys > 0) {
438       fatal("Sorry, but decoys (-D) require %s", privreq);
439     }
440 
441     if (fragscan) {
442       fatal("Sorry, but fragscan requires %s", privreq);
443     }
444 
445     if (osscan) {
446       fatal("TCP/IP fingerprinting (for OS scan) requires %s", privreq);
447     }
448   }
449 
450 
451   if (bouncescan && pingtype != PINGTYPE_NONE)
452     log_write(LOG_STDOUT, "Hint: if your bounce scan target hosts aren't reachable from here, remember to use -Pn so we don't try and ping them prior to the scan\n");
453 
454   if (ackscan+bouncescan+connectscan+finscan+idlescan+maimonscan+nullscan+synscan+windowscan+xmasscan > 1)
455     fatal("You specified more than one type of TCP scan.  Please choose only one of -sA, -b, -sT, -sF, -sI, -sM, -sN, -sS, -sW, and -sX");
456 
457   if (numdecoys > 0 && (bouncescan || connectscan)) {
458     error("WARNING: Decoys are irrelevant to the bounce or connect scans");
459   }
460 
461   if (fragscan && !(ackscan|finscan|maimonscan|nullscan|synscan|windowscan|xmasscan) && \
462       !(pingtype&(PINGTYPE_ICMP_TS|PINGTYPE_TCP)) && !(fragscan == 8 && pingtype&PINGTYPE_ICMP_MASK) && \
463       !(extra_payload_length + 8 > fragscan)) {
464     fatal("Fragscan only works with TCP, ICMP Timestamp or ICMP Mask (mtu=8) ping types or ACK, FIN, Maimon, NULL, SYN, Window, and XMAS scan types");
465   }
466 
467   if (osscan && bouncescan)
468     error("Combining bounce scan with OS scan seems silly, but I will let you do whatever you want!");
469 
470 #if !defined(LINUX) && !defined(OPENBSD) && !defined(FREEBSD) && !defined(NETBSD)
471   if (fragscan) {
472     error("Warning: Packet fragmentation selected on a host other than Linux, OpenBSD, FreeBSD, or NetBSD.  This may or may not work.");
473   }
474 #endif
475 
476   if (osscan && noportscan) {
477     fatal("WARNING: OS Scan is unreliable without a port scan.  You need to use a scan type along with it, such as -sS, -sT, -sF, etc instead of -sn");
478   }
479 
480   if (osscan && ipprotscan) {
481     error("WARNING: Disabling OS Scan (-O) as it is incompatible with the IPProto Scan (-sO)");
482     osscan = false;
483   }
484 
485   if (servicescan && ipprotscan) {
486     error("WARNING: Disabling Service Scan (-sV) as it is incompatible with the IPProto Scan (-sO)");
487     servicescan = false;
488   }
489 
490   if (servicescan && noportscan)
491     servicescan = false;
492 
493   if (defeat_rst_ratelimit && !synscan && !openOnly()) {
494     fatal("Option --defeat-rst-ratelimit works only with a SYN scan (-sS)");
495   }
496 
497   if (defeat_icmp_ratelimit && !udpscan) {
498     fatal("Option --defeat-icmp-ratelimit works only with a UDP scan (-sU)");
499   }
500 
501   if (resume_ip.ss_family != AF_UNSPEC && generate_random_ips)
502     resume_ip.ss_family = AF_UNSPEC;
503 
504   if (magic_port_set && connectscan) {
505     error("WARNING: -g is incompatible with the default connect() scan (-sT).  Use a raw scan such as -sS if you want to set the source port.");
506   }
507 
508   if (max_parallelism && min_parallelism && (min_parallelism > max_parallelism)) {
509     fatal("--min-parallelism=%i must be less than or equal to --max-parallelism=%i",min_parallelism,max_parallelism);
510   }
511 
512   if (min_packet_send_rate != 0.0 && max_packet_send_rate != 0.0 && min_packet_send_rate > max_packet_send_rate) {
513     fatal("--min-rate=%g must be less than or equal to --max-rate=%g", min_packet_send_rate, max_packet_send_rate);
514   }
515 
516   if (af() == AF_INET6 && (generate_random_ips||bouncescan||fragscan)) {
517     fatal("Random targets, FTP bounce scan, and fragmentation are not supported with IPv6.");
518   }
519 
520   if(ipoptions && osscan)
521     error("WARNING: IP options are NOT used while OS scanning!");
522 
523 #ifndef NOLUA
524   /* Make sure nmap.registry.args is available (even if it's empty) */
525   if (!scriptargs)
526     scriptargs = strdup("");
527 #endif
528 }
529 
setMaxOSTries(int mot)530 void NmapOps::setMaxOSTries(int mot) {
531   if (mot <= 0)
532     fatal("%s: value must be at least 1", __func__);
533   max_os_tries = mot;
534 }
535 
setMaxRttTimeout(int rtt)536 void NmapOps::setMaxRttTimeout(int rtt)
537 {
538   if (rtt <= 0) fatal("%s: maximum round trip time must be greater than 0", __func__);
539   max_rtt_timeout = rtt;
540   if (rtt < min_rtt_timeout) min_rtt_timeout = rtt;
541   if (rtt < initial_rtt_timeout) initial_rtt_timeout = rtt;
542 }
543 
setMinRttTimeout(int rtt)544 void NmapOps::setMinRttTimeout(int rtt)
545 {
546   if (rtt < 0) fatal("%s: minimum round trip time must be at least 0", __func__);
547   min_rtt_timeout = rtt;
548   if (rtt > max_rtt_timeout) max_rtt_timeout = rtt;
549   if (rtt > initial_rtt_timeout) initial_rtt_timeout = rtt;
550 }
551 
setInitialRttTimeout(int rtt)552 void NmapOps::setInitialRttTimeout(int rtt)
553 {
554   if (rtt <= 0) fatal("%s: initial round trip time must be greater than 0", __func__);
555   initial_rtt_timeout = rtt;
556   if (rtt > max_rtt_timeout) max_rtt_timeout = rtt;
557   if (rtt < min_rtt_timeout) min_rtt_timeout = rtt;
558 }
559 
setMaxRetransmissions(int max_retransmit)560 void NmapOps::setMaxRetransmissions(int max_retransmit)
561 {
562     if (max_retransmit < 0)
563         fatal("%s: must be positive", __func__);
564     max_retransmissions = max_retransmit;
565 }
566 
567 
setMinHostGroupSz(unsigned int sz)568 void NmapOps::setMinHostGroupSz(unsigned int sz) {
569   if (sz > max_host_group_sz)
570     fatal("Minimum host group size may not be set to greater than maximum size (currently %d)\n", max_host_group_sz);
571   min_host_group_sz = sz;
572 }
573 
setMaxHostGroupSz(unsigned int sz)574 void NmapOps::setMaxHostGroupSz(unsigned int sz) {
575   if (sz < min_host_group_sz)
576     fatal("Maximum host group size may not be set to less than the minimum size (currently %d)\n", min_host_group_sz);
577   if (sz <= 0)
578     fatal("Max host size must be at least 1");
579   max_host_group_sz = sz;
580 }
581 
582   /* Sets the Name of the XML stylesheet to be printed in XML output.
583      If this is never called, a default stylesheet distributed with
584      Nmap is used.  If you call it with NULL as the xslname, no
585      stylesheet line is printed. */
setXSLStyleSheet(const char * xslname)586 void NmapOps::setXSLStyleSheet(const char *xslname) {
587   if (xsl_stylesheet) free(xsl_stylesheet);
588   xsl_stylesheet = xslname? strdup(xslname) : NULL;
589   xsl_stylesheet_set = true;
590 }
591 
592 /* Returns the full path or URL that should be printed in the XML
593    output xml-stylesheet element.  Returns NULL if the whole element
594    should be skipped */
XSLStyleSheet()595 char *NmapOps::XSLStyleSheet() {
596   char tmpxsl[MAXPATHLEN];
597 
598   if (xsl_stylesheet_set)
599     return xsl_stylesheet;
600 
601   if (nmap_fetchfile(tmpxsl, sizeof(tmpxsl), "nmap.xsl") == 1) {
602     xsl_stylesheet = filename_to_url(tmpxsl);
603   } else {
604     /* Use a relative URL if nmap_fetchfile failed. It won't work,
605        but it gives a clue that there is an nmap.xsl somewhere. */
606     xsl_stylesheet = strdup("nmap.xsl");
607   }
608 
609   return xsl_stylesheet;
610 }
611 
setSpoofMACAddress(u8 * mac_data)612 void NmapOps::setSpoofMACAddress(u8 *mac_data) {
613   memcpy(spoof_mac, mac_data, 6);
614   spoof_mac_set = true;
615 }
616 
617 #ifndef NOLUA
chooseScripts(char * argument)618 void NmapOps::chooseScripts(char* argument) {
619         char *p;
620 
621         for (;;) {
622                 p = strchr(argument, ',');
623                 if (p == NULL) {
624                         chosenScripts.push_back(std::string(argument));
625                         break;
626                 } else {
627                         chosenScripts.push_back(std::string(argument, p - argument));
628                         argument = p + 1;
629                 }
630         }
631 }
632 #endif
633