#  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.
#
#
($_host) = split(/,/, $html_script_args);
$_ip = $all_hosts{$_host}{IP};
($_type = $hosttype{$_host}) =~ tr / \//+!/;
$_hosttype=substr($hosttype{$_host},0,22);

#
# Refresh the host type and service tables.
#
&make_hosttype_info();
&make_service_info();
&make_subnet_info();
&make_severity_info();
$_red_cnt=0;
$_yellow_cnt=0;
$_brown_cnt=0;
$_green_cnt=0;
$_fp_cnt   =0;
	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");

                         if($severity_red_type_info{$service_output}) {
                          if (correction("$_host|$text") == 0 ) {
			    $_red_cnt=$_red_cnt + 1 if sans10($service_output);
                          } else {
                            $_fp_cnt=$_fp_cnt + 1 if sans10($service_output);
                          }
		}
	    }
	}

        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");

                         if ($severity_yellow_type_info{$service_output}) {
                          if (correction("$_host|$text") == 0 ) {
                            $_yellow_cnt = $_yellow_cnt + 1 if sans10($service_output);
                          } else {
                            $_fp_cnt=$_fp_cnt + 1 if sans10($service_output);
                          }
			}
	    }
	}

        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)) {
                    $_pro_filter="FALSE";
                    if ($pro_filter) {
                      for (split(/\,/, $pro_filter)){
                        $_tmp = $_;
                        $_tmp =~ s/\ //;
                        if ( $_severity =~ "$_tmp") {
                          $_pro_filter = "TRUE";
                        }
                     }
                    }
                    if ($_pro_filter eq "FALSE") {
                     if (correction("$_host|$text") == 0 ) {
                       $_brown_cnt = $_brown_cnt + 1 if sans10($service_output);
                       } else {
                         $_fp_cnt=$_fp_cnt + 1 if sans10($service_output);
                     }
                    }
                   }
             }
	}
if (exists($server_info{$_host})) {
        for (sort split(/\n/, $server_info{$_host})) {
	$_green_cnt = $_green_cnt + 1;
        }
}
if ( $hosttype{$_host} ne "not scanned") 
{
     $_ip = $all_hosts{$_host}{IP};

#Kludge so that it will import into MS Word
    $hostname = $_host;
    $hostname =~ s/\./\_/g;
    $hostname =~ s/\-/\_/g;
    $linkname = substr($hostname,0,30);


    $todo = "print";
#  Check and see if filters are active
    if ($option ne "default"){
#  Filter out hosts without services
#  Filter out hosts without vulnerabilities or SANS10 mode is on
    if ($novul eq "on" || $sans eq "on") {
      if ( ( $_red_cnt+$_yellow_cnt+$_brown_cnt+$_fp_cnt) == 0) {
        $todo="noprint";
      }
    }
#  Filter out all Windows
    if ( $hosttype{$_host} =~ /Windows/ ) {
      if ($nowin1 eq "on"){
        if (($_red_cnt+$_yellow_cnt+$_fp_cnt) == 0) {
          $todo="noprint";
        }
      }elsif ($nowin eq "on"){
        $todo="noprint";
      }
    }
   }
    if ( $todo eq "print" ) {
    if ( $start_report == 1 ) {
     $start_report = 0;
print <<EOF;
   <CENTER><TABLE BORDER=1><TR><TD width=140><Font Size=2><B>Host Name</B></Font></TD><TD width=70><Font size=2><B>IP Address</B></Font></TD><TD width=80><Font size=2><B>Host Type</B></Font></TD><TD width=40><Font size=2><B>Green</B></Font></TD><TD width=30><Font size=2><B>Red</B></Font></TD><TD width=45><Font size=2><B>Yellow</B></Font></TD><TD width=45><Font size=2><B>Brown</B></Font></TD><TD width=25><Font Size=2><B>FP</B></Font size=2></TD></TR>

EOF
}
    print <<EOF;
    <TR><TD><Font size=1><A HREF='#BM$linkname'>$_host</A></Font></TD><TD><Font size=1>$_ip</Font></TD><TD><Font size=1>$_hosttype</Font></TD><TD><CENTER><Font size=1>$_green_cnt</Font></Center></TD><TD><CENTER><Font size=1>$_red_cnt</Font></center></TD><TD><CENTER><Font size=1>$_yellow_cnt</Font></center></TD><TD><CENTER><Font size=1>$_brown_cnt</Font></center></TD><TD><Font size=1>$_fp_cnt</Font></TD></TR>
EOF
}
}
