Cover V10, I02
Article
Listing 1

feb2001.tar


Listing 1 antivirus.pl

#!/usr/bin/perl 

use CGI;
use Mail::Sendmail;
use Socket;
use Sys::Hostname;

$query=new CGI;

print $query->header();
print $query->start_html("Antivirus service");
print '<meta HTTP-EQUIV="EXPIRES" CONTENT="0">';
print '<BODY BGCOLOR="#FFFFFF">';

my $hostname = gethostbyaddr(inet_aton($query->remote_host()),AF_INET);

$hostname=~s/.enpl.es//;
$hostname=uc($hostname);

print $query->h1("<CENTER>Antivirus ($hostname)</CENTER>");
print $query->hr;

# show antivirus info
if (! -x "/usr/local/bin/uvscan")
 {
  # Doesn't find the executable
   print "<B>Error</B>: Can't find uvscan: $!";
   exit(-1);
 }
open (FILE, "/usr/local/bin/uvscan --version |");

while (<FILE>)
{
 print $_ . "<P>";
}
close FILE;
                                                                                
print $query->hr;
print $query->start_form;


if($query->param())
# We come from the submit button, we start the whole process
  {
  # We've got $hostname, and $query->param('Shares'), 
  # $query->param('email')
  # We don't need anything more to start the job
  
  # We have to use smbmount to mount /mnt/smb/$hostname/$share
my $share;
my $email;
my $time;

 $share=$query->param('Shares');
 $share=~s/^( |\t)+//g;
 chop($share);
 $share=~s/ //g;

 $email=$query->param('email');

 if (!($email=~/[a-z-A-Z-0-1]+\@enpl.es/))
  {
   print "<B>Error:</B> Invalid e-mail address: $email";
   exit(-1);
  }
  
  # We create the dir. where we'll mount the client share

 
 
  mkdir "/mnt/smb/$hostname",500;
  mkdir "/mnt/smb/$hostname/$share",500;
  
  open FD, ">/tmp/atjob.$hostname.$share" || die "Can't create temp file";

  
  print FD "/usr/bin/smbmount //$hostname/$share \
    /mnt/smb/$hostname/$share -N > /dev/null \n";
  print FD  "/usr/local/bin/uvscan -c -r  /mnt/smb/$hostname/$share \
    |mail -s \"Antivirus Report\" $email \n";
  print FD "/usr/bin/smbumount /mnt/smb/$hostname/$share\n";
  
# We remove the directory in two steps, just in case

  print FD "rmdir /mnt/smb/$hostname/$share\n";
  print FD "rmdir /mnt/smb/$hostname\n";

  close FD;
  chmod 500,"/tmp/atjob.$hostname.$share";
  if ($query->param('ScanNow') eq 'on') 
   {
    $time='now';
    print "<B>You'll receive an e-mail report in a while.</B>";
   }
  else
   {
    $time='23:00';
    print "<B>Ready<P>Remember to leave the computer on tonight...</B>";
   }
  system ("at $time -f /tmp/atjob.$hostname.$share");

#  sleep(5);

 
print $query->end_form;
print $query->end_html;



}
else

{
 # No parameters  

my @shares;

open (FILE,"/usr/local/samba/bin/smbclient -L $hostname -U \
  nobody%nopass |grep Disk| cut -f 1 -d \" \" |");
while (<FILE>)
{
 $_=~s/^\s+//;              # Cut spaces
 $_=~s/Disk//;
 if (! ($_=~/PRINTER\$/))   # This resource appears as a directory when \
   the user has a shared printer
  {push @shares, $_;}
}
close FILE;

if ($#shares==-1)
 {
  print "<B>No shared resources to scan<B><P>";
  print '<a href="http://calix:85/help.html"><img SRC="/help.gif" \
    BORDER=0 height=40 width=200></a>';
 }
else
 {

  print "Select the resource: " .  $query->popup_menu('Shares',\@shares) \
    . "</P>";
  print "Send e-mail report to: " . $query->textfield(-name=>'email',
          -size=>30) . "</P>";
  print $query->checkbox(-name=>'ScanNow',-label=>'Search for viruses now');
  print '<a href="http://calix:85/help.html"><img SRC="/help.gif" \
    BORDER=0 height=40 width=200></a>';
  print "<P>" . $query->submit(-name=>'Start');
 }

print $query->end_form;
print $query->end_html;

}