#!/usr/bin/perl

# script to update mcafee automatically by Sam Pratt
# based on sophos update script by jkf

############################################################
# 11/08/2000: 	ftp problem has been fixed to use Net::FTP
# 		.netrc entry no longer required
#
# 21/09/2000:	Modified to work in delphi test mailscanner
#
# 05/06/2002:   Modified to use uvscan instead of mcafee/dat
#
############################################################

use strict;
use Net::FTP;
my($LockFile) = "/tmp/McAfeeBusy.lock";

my($LOCK_SH) = 1;
my($LOCK_EX) = 2;
my($LOCK_NB) = 4;
my($LOCK_UN) = 8;

my($ftp);
my($tar) = '/bin/tar';
my($rm) = '/bin/rm';
#my($ftpsite) = 'ftpeur.nai.com'; # Use faster European mirror instead of
my($ftpsite) = 'ftp.nai.com'; # busy US site
my($ftplogin) = 'anonymous';
my($ftppassword) = "user123\@ecs.soton.ac.uk";

my($mcafeeroot) = '/usr/local/uvscan';
my($datfile);
my($mcafee) = "$mcafeeroot/uvscan";

#umask(0022);

chdir($mcafeeroot) or die("Cannot cd $mcafeeroot $!");

# get the update file from fpt.nai.com
# this script assumes that there is only one file
# to download I think this is the case but the documentation is
# not very clear on the matter

$ftp = Net::FTP->new($ftpsite) or BailOut('McAfee update failed: cannnot connect to ftp site');
$ftp->login($ftplogin,$ftppassword ) or BailOut('McAfee update failed: cannot login to ftp site');
$ftp->cwd('/pub/antivirus/datfiles/4.x/') or BailOut('McAfee update failed: cannot cd ftp update dir');
my(@files)= $ftp->ls();
my($file);
foreach $file (@files){
	if ($file =~ /dat-.*\.tar/){
		$datfile = $file;
	}
}

BailOut('McAfee update failed: cannot find the update file') if (!($datfile=~ /dat-.*\.tar/));

$ftp->binary() or BailOut('McAfee update failed: cannot download update files');
$ftp->get($datfile) or BailOut('McAfee update failed: cannot download update files');

&LockMcafee();
system("$tar \-xf $datfile");
unlink $datfile;

# to see if the new dat's are o.k attempt to run mcafee with them and 
# check for errors
open(MCAFEETEST, "$mcafee -d $mcafeeroot --version . | ");
while(<MCAFEETEST>){
	chomp;
	print "$_\n";
	if (/Missing or invalid DAT/){
		BailOut('Mcafee update failed due to a problem with the dat files');
	}
	if (/Data file not found/){
		BailOut('Mcafee update failed due to a problem with the dat files');
	}
	if (/Removal datafile clean.dat not found/){
		BailOut('Mcafee update failed due to a problem with the dat files');
	}
	if (/Unable to remove viruses/){
		BailOut('Mcafee update failed due to a problem with the dat files');
	}
}

close(MCAFEETEST);
&UnlockMcafee();

chdir $mcafeeroot or &BailOut("Cannot cd $mcafeeroot, $!");
exit 0;

sub BailOut {
        warn "@_, $!";
        chdir $mcafeeroot or die "Cannot cd $mcafeeroot, $!";
        exit 1;
}

sub LockMcafee {
        open(LOCK, ">$LockFile") or return;
        flock(LOCK, $LOCK_EX);
        print LOCK "Locked for updating Mcafee DAT files by $$\n";
}

sub UnlockMcafee {
        print LOCK "Unlocked after updating Mcafee DAT files by $$\n";
        flock(LOCK, $LOCK_UN);
        close LOCK;
}
