#!/usr/bin/perl
# SARA Support Scanning (SSS) Optional Module
# Agent that supports self scans through sss.html
# and bootup scans through sss.cgi.  Review the
# acoompanying README file for details.
# You can start the SARA agent by typing
# nohup .sss/sss.pl > /dev/null &
#
$sara_in = "/tmp/sara-in";
$which="";
$which=`which mailx 2> /dev/null | grep "^\/"`;
if ($which) {
  $mailx = $which;
} else {
  $which=`which mail | grep "^\/"`;
  if ($which) {
    $mailx = $which;
  }
}
$mailx =~ s/\n//;

$uuencode="/usr/bin/uuencode";
while ( $sara_in) {
  die "SSS agent data directory ($sara_in) not found, aborting"unless (-d $sara_in);
  @SARA_IN = `ls $sara_in/* 2> /dev/null`;
  for (@SARA_IN) {
    $tmpfile = $_;
    $tmpfile =~ s/\n//g;
    ($dummy,$ip,$email,$org) = split(/\_/,$tmpfile);
    unlink "$tmpfile";
sleep 5;
    $date = `date`;
    open (LOG, ">> sss.log");
    print LOG "$date $ip $email $org\n";
    if (! $mailx) {
     print LOG "No mail agent\n";
    }
    close(LOG);
    if ("$email" =~ /\@/) {
      $ipdir=$ip;
      $ipdir = $org if $org;
      system("rm -rf results/$ipdir") if !$org;
       
      system("./sara -a3 -c \"option=default\" -d $ipdir $ip;cat results/$ipdir/report.html|$uuencode \"$ipdir-report.html\"| $mailx -s \"SARA REPORT for $ipdir\" $email"); 
      system("rm -rf results/$ipdir") if ! $org;
    } else {
      system("rm -rf results/$ip");
      system("./sara -a3 -c \"option=default\" -d $ip $ip");
      @_results = `cat results/$ip/report.csv`;
      for $tmp (@_results) {
       (@logger_data) = split/,/,$tmp;
       `logger  -p mail.warning "SSS Report: $logger_data[0] $logger_data[8] $logger_data[10]\n"`; 
    }
  }
}
sleep 60;
}
