#!/usr/bin/perl
$sara_in="/tmp/sara-in";
$ping="./fping";
print "Content-type: text/html\n\n";
print <<EOF;
<TITLE>SARA Self Scan</TITLE>
<TABLE width=640>
<TR><TD><img width="100" height="150" src="/sss/lighthouse.gif">
<img width=140  src="/sss/sara.gif"></TD>
<TD><Center><H1>SARA Self Scan</H1></Center>
EOF
$ssspl=`ps -ef | grep "sss\.pl" | grep -v grep`;
if (! $ssspl) {
  print "<H2>SARA Support Agent, sss.pl, not running<BR>\n";
  print "Contact the System Administrator<BR>\n";
  print "Aborting processing</H2>\n";
  exit;
}
if (! -d "$sara_in") {
  print "SARA Support Agent fifo directory, /tmp/sara-in, does not exist\n";
  print "Aborting process\n";
}
foreach (keys %ENV) {
     $querystring=$ENV{QUERY_STRING};
     $remotehost=$ENV{REMOTE_HOST};
     $remoteaddr=$ENV{REMOTE_ADDR}; 
}
if (! "$remotehost" ) {
  $remotehost = $remoteaddr;
}
if ($remotehost) {
  $results = `$ping $remotehost`;
  if ($results !~ /alive/) {
    print "Your system, $remotehost, appears to be behind a firewall. ";
    print "SARA will not scan this host.  If you wish a SARA Self Scan,";
    print "please enable full access for the SARA host,";
    print "<P>\n";
    exit;
  }
  ($email0,$email1,$org) =split(/\&/,$querystring);
  ($dummy,$email0)  = split(/\=/,$email0);
  ($dummy,$email1)  = split(/\=/,$email1);
  ($dummy,$org)     = split(/\=/,$org);
  if (! $email0 && ! $email1) {
    `touch $sara_in/\`date +%Y%j%S\`_$remotehost`;
    print "Queuing SARA scan for $remotehost\n"
  } else {
    if ($confirm =~ /no/i) {
      print "<H3> Self Scan was Cancelled by User</H3>\n";
      print "<P><A HREF=\"/sara-if.html\">Return to Self Audit</A>\n";
      exit;
    }
  
    if ("$email0" ne "$email1") {
      print "<H3> Email Names do not Match, Try Again</H3>\n";
      print '<P><A HREF="/sara-if.html">Return to Self Audit</A>\n';
      exit;
    }
    $email0 =~ s/%40/@/;
    print "<H3>Scan Request Submitted to SARA</H3>\n";
    print "<P>\n";
    $fifo_out = "$sara_in/\`date +%Y%j%S\`_$remotehost";
    $fifo_out .= "_$email0";
    $fifo_out .= "_$org" if $org;
    `touch "$fifo_out"`;
    print "A SARA Scan on $remotehost ($remoteaddr) will be sent to $email0\n"; 
    print "<P><A HREF=\"/sss/sss.html\">Return to Self Audit</A>\n";
    print "</TD></TR></TABLE>\n";
    }
}

