#  This software was derived from SATAN 1.1.1 by Dan Farmer and Wietse Venema
#  (http://www.porcupine.org/satan).
#
#  Added start time logic   06/01/00 by todd
#  Updated start time logic 09/24/00 by todd
#  Updated to fix problem with ill defined addresses (dns-wise)
#
require 'perl/sara-data.pl';
require 'perl/misc.pl';
require 'perl/getfqdn.pl';
require 'perl/get_host.pl';
require 'perl/suser.pl';
require 'perl/status.pl';
require 'perl/xml.pl';
require 'perl/htm.pl';

sub run_sara {
#foreach (keys %ENV) {
   $hostfile=$ENV{SARA_HOSTS};
#}

$now_day = &hhmmss(time());
$now_year= &yymmdd(time());
$start_dtg = "$now_year $now_day";
if ($hostfile) {
        $i = 0;
        print "Hosts are $hostfile\n" if $debug;
	die "SARA needs root privileges for data collection.\n" if $>;
	%host_is_alive = ();
        open (HOSTS, $hostfile) || die "can't open $hostfile\n";
        while (<HOSTS>) {
	  chomp;
	  $primary_targets[$i++] = $_;
        }
        close (HOSTS);
	for $primary_target (@primary_targets) {
          $primary_target =~ s/\s+//g;
          if ($primary_target){
            if ($primary_target =~ /^\d+\.\d+\.\d+\.\d+$/) {
               $primary_target = $target
               if ($target = &getfqdn(&get_host_name(
                  &get_host_addr($primary_target))));
            }

                # schedule probe assignments...
                &add_primary_target($primary_target, 0);
          }
	  &fix_primary_targets();
	  while(sizeof(*new_targets)||sizeof(*new_todos)||sizeof(*new_facts)) {
		&process_targets();
		&process_todos();
		&process_facts();
		&save_sara_data();
	  }
        }

        $xml_file = "results/$sara_data/$report_xml";
        $csv_file = "results/$sara_data/$report_csv";
        $htm_file = "results/$sara_data/$report_htm";
        unlink ($xml_file);
        unlink ($csv_file);
        unlink ($htm_file);
        if ($reportwriter == 1) {
          &save_xml($xml_file);
          &save_csv($csv_file);
          &save_htm($htm_file);
        }
	&update_status("SARA run completed");
        $now_day = &hhmmss(time());
        $now_year= &yymmdd(time());
        $stop_dtg = "$now_year $now_day";
        open (HISTORY,">>results/$sara_data/history");
        print HISTORY "$start_dtg|$stop_dtg|$attack_level|$primaries|$sara_version\n";
        close(HISTORY);

} else {
	local($primaries) = @_;
	local($target);
	#
	# Don't die silently when tools cannot be run with sufficient privilege.
	#
	die "SARA needs root privileges for data collection.\n" if $>;

	#
	# Clear the table with alive hosts.
	#
	%host_is_alive = ();

	#
	# Set up a list of primary target hosts.
	#
	die "no primary target was specified" if $primaries eq "";
	for $primary_target (split(/\s+/, $primaries)) {
		#
		# Jump hoops to get at the official host name in case foo.com is
		# in reality called bar.foo.com.
		#
		if ($primary_target =~ /^\d+\.\d+\.\d+\.\d+$/) {
                        $primary_target = $target
			    if ($target = &getfqdn(&get_host_name(
				    &get_host_addr($primary_target))));
		}
		# schedule probe assignments...
                print "Adding a primary target\n" if $debug;
		&add_primary_target($primary_target, 0);
#	}
	#
	# In case we must do something special with primary target hosts.
	#
	&fix_primary_targets();
	#
	# Switch control between the data collection engine and the
	# inference engine until both run out of new ideas. Checkpoint
	# after each iteration.
	#
	while(sizeof(*new_targets)||sizeof(*new_todos)||sizeof(*new_facts)) {
		&process_targets();
		&process_todos();
		&process_facts();
		&save_sara_data();
	}
}
        $xml_file = "results/$sara_data/$report_xml";
        $csv_file = "results/$sara_data/$report_csv";
        $htm_file = "results/$sara_data/$report_htm";
        unlink ($xml_file);
        unlink ($csv_file);
        unlink ($htm_file);
        if ($reportwriter == 1) {
          &save_xml($xml_file);
          &save_csv($csv_file);
          &save_htm($htm_file);
	  &update_status("SARA run completed");
        }
        $now_day = &hhmmss(time());
        $now_year= &yymmdd(time());
        $stop_dtg = "$now_year $now_day";
        open (HISTORY,">>results/$sara_data/history");
        print HISTORY "$start_dtg|$stop_dtg|$attack_level|$primaries|$sara_version\n";
        close(HISTORY);
  }
}
1;
