#  This software was derived from SATAN 1.1.1 by Dan Farmer and Wietse Venema
#  (http://www.porcupine.org/satan).
#
#  This code update is provided by the Advanced Research Corporation (r) to
#  further the utility of the SARA Report Writer.  We request that the
#  developers do not modify or reverse engineer this code for
#  their own gain.
#
#  Developed and distributed by the Advanced Research Corporation.
#
#
# Generate a report for a list of hosts
#
($_host) = split(/,/, $html_script_args);
$_ip = $all_hosts{$_host}{IP};
($_type = $hosttype{$_host}) =~ tr / \//+!/;
($_hosttype = $hosttype{$_host});

#
# Refresh the host type and service tables.
#
&make_hosttype_info();
&make_service_info();
&make_subnet_info();
&make_severity_info();
$html_txt = "";
$html_cor = "";
#
#  Could not figure out Dan's code, so here is a Bob kludge
$vulnerable="FALSE";
$win_vulnerable="FALSE";
for (keys %{$severity_host_type_info{$_host}}) {
                ($_tutorials = $_) =~ tr / /_/;
                for (sort split(/\n/, $severity_host_type_info{$_host}{$_})) {
                        &sara_split($_) if ($system eq "sara");
                        &saint_split($_) if ($system eq "saint");
                        &satan_split($_) if ($system eq "satan");

                  $_severity= $severity_brown_type_info{$service_output};
                  if($_severity) {
                    if ($pro_filter) {
                      $_pro_filter="FALSE";
                      for (split(/\,/, $pro_filter)){
                        $_tmp = $_;
                        $_tmp =~ s/\ //;
                        if ( $_severity =~ "$_tmp") {
                          $_pro_filter = "TRUE";
                        }
                    }
                    }
                    if ($_pro_filter eq "FALSE") {
                     $vulnerable="TRUE" if sans10($service_output) == 1;
#                     $vulnerable="FALSE" if correction("$_host|$text") == 1;
                   }
                  }
                  if($severity_yellow_type_info{$service_output}) {
	             $win_vulnerable="TRUE" if sans10($service_output) == 1;;
                     $vulnerable="TRUE" if sans10($service_output) == 1;
#                     $vulnerable="FALSE" if correction("$_host|$text") == 1;
                  }
                  if($severity_red_type_info{$service_output}) {
                     $win_vulnerable="TRUE" if sans10($service_output) == 1;
                     $vulnerable="TRUE" if sans10($service_output) == 1;
#                     $vulnerable="FALSE" if correction("$_host|$text") == 1;
                  }
}
}

if ( $_type eq "not+scanned" ) {
return;
}

#Kludge so that it will import into MS Word
$hostname = $_host;
$hostname =~ s/\./\_/g;
$hostname =~ s/\-/\_/g;
$linkname = substr($hostname,0,30);
# Filter if Report Writer is activated
    $todo = "print";
#  Check and see if filters are active
    if ($option eq "selected"){
#  Filter out hosts without vulnerabilities
    if ($novul eq "on" || $sans eq "on") {
     if ($vulnerable eq "FALSE") {
       $todo = "noprint";
     }
}
#  Filter out all Windows
    if ( $hosttype{$_host} =~ /Windows/ ) {
     if ($nowin eq "on" || $nowin1 eq "on"){
      $todo = "noprint";
     }
      if ($nowin1 eq "on"){
        if ($win_vulnerable eq "TRUE") {
          $todo="print";
        }
      }
    }
}
if ($todo eq "print"){

print <<EOF;
<PRE>

</PRE>
<A NAME='BM$linkname'>
<B>Host: $_host </B></CENTER>
<BR>
<B>General host information:</B>
<UL>
EOF

#
# Refresh the host type and service tables.
#

$_exists = 0;
$_dot = "blackdot";
$_alt = "BLACK";
if ($_hosttype) {
	print <<EOF;
        <li>Host type: $_hosttype
EOF
	$_exists = 1;
}
if (exists($client_info{$_host})) {
        for (sort split(/\n/, $client_info{$_host})) {
                # Mask blanks and slashes
                ($_service = $_) =~ tr / \//+!/;
                print <<EOF;
        <li>$_ client
EOF
        }
        $_exists = 1;
}

if (exists($all_hosts{$_host})) {
        ($_subnet = $all_hosts{$_host}{IP}) =~ s/\.[^.]*$//;
        $_dot = "blackdot";
        print <<EOF;
        <LI>Subnet  $_subnet
EOF
        if ($get_admins == 1) {
          $admin = &get_administrator();
          print "<LI>Administrator: $admin<BR>\n";
        }
        $_exists = 1;
}


if (exists($server_info{$_host})) {
	for (sort split(/\n/, $server_info{$_host})) {
		# Mask blanks and slashes
		($_service = $_) =~ tr / \//+!/;
                $_dot = "greendot";
		$_alt = "GREEN";
                $nbname = "";
                if ($_service =~ "SMB") {
                  open (NB, $facts_file);
                  while (<NB>) {
                    chop;
                    if (/Netbios Name/i && /$_host/) {
                     @nb=split(/\|/,$_);
                     if ($nb[7]) {
                       $nbname="($nb[7])";
                     }
                    }
                  }
                  close(NB);
                  $_="SMB";
                }
		print <<EOF;
		<li><Font Color="$_alt"> $_  server $nbname($_alt)</Font>
EOF
	}
	$_exists = 1;
}

if (exists($severity_host_type_info{$_host})) {
	for (keys %{$severity_host_type_info{$_host}}) {
		($_tutorials = $_) =~ tr / /_/;
		for (sort split(/\n/, $severity_host_type_info{$_host}{$_})) {
                        &sara_split($_);
                         if($severity_red_type_info{$service_output}) {
                            $_dot = "reddot";
                            $_alt = "RED";
			    $_service = $service_output;
                            $_service =~ s/ /_/g;
                            if (sans10($service_output) == 1) {
                              if (correction("$_host|$text") == 0 ) {
                                $html_txt .= "<li><FONT COLOR=\"$_alt\">";
                                $html_txt .= "<a href=\"#$_service\">";
                                $html_txt .= "$text</A>($_alt)</Font>";
                                $html_txt .= "$sans_label\n";
                              } else {
                                $html_cor .= "<li><FONT COLOR=\"GRAY\">";
                                $html_cor .= "<a href=\"#$_service\">";
                                $html_cor .= "$text</A>($_alt)</Font>";
                                $html_cor .= "sans_label\n";
                            }
                           }
                         }
		}
	}
	$_exists = 1;

        for (keys %{$severity_host_type_info{$_host}}) {
                ($_tutorials = $_) =~ tr / /_/;
                for (sort split(/\n/, $severity_host_type_info{$_host}{$_})) {
                        &sara_split($_);

                         if ($severity_yellow_type_info{$service_output}) {
                            $_dot = "yellowdot";
                            $_alt = "YELLOW";
			    $_service = $service_output;
                            $_service =~ s/ /_/g;

                            if (sans10($service_output) == 1) {
                              if (correction("$_host|$text") == 0 ) {
                                $html_txt .= "<li><FONT COLOR=\"$_alt\">";
                                $html_txt .= "<a href=\"#$_service\">";
                                $html_txt .= "$text</A>($_alt)</Font>";
                                $html_txt .= "$sans_label\n";
                              } else {
                                $html_cor .= "<li><FONT COLOR=\"GRAY\">";
                                $html_cor .= "<a href=\"#$_service\">";
                                $html_cor .= "$text</A>($_alt)</Font>";
                                $html_cor .= "$sans_label\n";
                            }
                          }

}
}
}
$exists = 1;

        for (keys %{$severity_host_type_info{$_host}}) {
                ($_tutorials = $_) =~ tr / /_/;
                for (sort split(/\n/, $severity_host_type_info{$_host}{$_})) {
                        &sara_split($_);

                  $_severity=$severity_brown_type_info{$service_output};
                  if ($_severity) {
                       $_dot = "browndot";
                       $_alt = "BROWN";
                       $_service = $service_output;
  		       $_service =~ s/ /_/g;
                       if ($pro_filter) {
                         $_pro_filter="FALSE";
                         for (split(/\,/, $pro_filter)){
                           $_tmp = $_;
                           $_tmp =~ s/\ //;
                           if ( $_severity =~ "$_tmp") {
                             $_pro_filter = "TRUE";
                           }
                         }
                       }
                       if (sans10($service_output) == 1) {
                         if ($_pro_filter eq "FALSE") {
                          if(correction("$_host|$text") == 0) {
                            $html_txt .= "<li><FONT COLOR=\"$_alt\">";
                            $html_txt .= "<a href=\"#$_service\">";
                            $html_txt .= "$text</A>($_alt)</Font>";
                            $html_txt .= "$sans_label\n";
                          } else {
                            $html_cor .= "<li><FONT COLOR=\"GRAY\">";
                            $html_cor .= "<a href=\"#$_service\">";
                            $html_cor .= "$text</A>($_alt)</Font>";
                            $html_cor .= "$sans_label\n";
                          }
                        }
                       }
}
}
}
  if ("$html_txt") {
    print <<EOF;
    </UL>
    <PRE>
    </PRE>
    <B>Vulnerability information:</B>
    <UL>
    $html_txt
    </UL>
EOF
  }
  if ("$html_cor") {
    print <<EOF;
    </UL>
    <PRE>
    </PRE>
    <B>Administrator Identified False Positive</B>
    <UL>
    $html_cor
    </UL>
EOF
  }
$exists=1;
}
print <<EOF;
EOF

# List host service info

if ($_exists) {
} else {
	print <<EOF;
	<B>No information found on host $_host.</B>
EOF
}

print <<EOF;
</UL>
<HR>
EOF
}
