#  This software was derived from SATAN 1.1.1 by Dan Farmer and Wietse Venema
#  (http://www.porcupine.org/satan).
#
#
# Collect data and keep the user informed.
#
# Check for firewall flag
if ($firewall_flag eq 1) {
   foreach (keys %ENV) {
       $ENV{SARA_FW}="ON" 
   }
}
# Set attack level
foreach (keys %ENV) {
  $ENV{SARA_LEVEL}=$attack_level;
}
# See if we need to set subnet mode automatically
$tmp_proximate_subnets=$attack_proximate_subnets;
if ($primary_target !~ /[A-Za-z]/) {
  if ($primary_target =~ /\// || $primary_target =~ /\-/) {
    $attack_proximate_subnets = 1;
  }
}

#
# Make sure they specified a host at all.
#
if ($primary_target eq "") {
	print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>Error - Missing input </TITLE>
<LINK REV="made" HREF="mailto:sara\@arc.com">
</HEAD>
<BODY>
<H1><IMG WIDTH="150" SRC=$HTML_ROOT/images/sara.gif> Error - Missing input </H1>
<hr>
No primary host or network was specified.
</BODY>
</HTML>
EOF
;
	die "\n";
}

#
# If a host name was specified look up the official name.
#
$primary_target =~ s/\+/ /g;
$primary_target =~ s/\,/ /g;
if ($primary_target !~ /^\d+\./ && $primary_target !~ /\s+/) {
	$tmp_target = &getfqdn(&get_host_name(&get_host_addr($primary_target)));
	if ($tmp_target eq "") {
		print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>Error - Unknown host</TITLE>
<LINK REV="made" HREF="mailto:sara\@fish.com">
</HEAD>
<BODY>
<H1><IMG WIDTH="150" SRC=$HTML_ROOT/images/sara.gif> Error - Unknown host</H1>
<hr>
Unable to look up host <TT> $primary_target </TT>
</BODY>
</HTML>
EOF
;	
		die "\n";
	}
	$primary_target = $tmp_target;
}
#
# Primary target is OK, start data collection.
#
print CLIENT <<EOF
<HTML>
<HEAD>
<TITLE>SARA data collection </TITLE>
<LINK REV="made" HREF="mailto:sara\@fish.com">
</HEAD>
<BODY>
<H1><IMG WIDTH="150" SRC=$HTML_ROOT/images/sara.gif> SARA data collection</H1>
<hr>
<B>Data collection in progress...</B>

<P>
<PRE>

EOF
;

$_live_hosts = $live_hosts;

if ($daemon && $primary_target =~ /\//) {
print CLIENT <<EOF;
You can not scan more than one subnet while in daemon mode
EOF
} else {
$debug_flag=$debug;
$debug="ON" if  ! $daemon;
$debug=""   if  $daemon;
select CLIENT;
&run_sara($primary_target);
select STDOUT;
$debug=$debug_flag;
$attack_proximate_subnets=$tmp_proximate_subnets;

$_live_hosts = $live_hosts - $_live_hosts;

print CLIENT <<EOF;
</PRE>
<P>

<B>Data collection completed ($_live_hosts host(s) visited).</B>
<hr> <a href=/sara.html> Back to the SARA start page </a> |
<a href=../reporting/analysis.pl>Continue with report and analysis</a>
EOF
if (&getfqdn($tmp_target)) {
	print CLIENT <<EOF;
| <a href="../reporting/sara_info_host.pl,$tmp_target,">
View primary target results</a>
EOF
}
}
print CLIENT <<EOF
</BODY>
</HTML>
EOF
