#!/usr/bin/perl use Sys::Syslog; # # Place version number (in both forms) here # $SophosRoot = "/usr/local/Sophos"; $IDELink = "/usr/local/Sophos/ide"; $VDLDir = "../lib"; $Lynx = "/usr/bin/lynx -dump"; $Unzip = "/usr/bin/unzip -joqq"; $rm = "/bin/rm"; $LockFile = "/tmp/SophosBusy.lock"; $LOCK_SH = 1; $LOCK_EX = 2; $LOCK_NB = 4; $LOCK_UN = 8; Sys::Syslog::openlog("Sophos-autoupdate", 'pid, nowait', 'mail'); # Work out the current VDL (and hence Sophos Sweep) version number chdir "$SophosRoot/bin/$VDLDir"; opendir(LIBDIR, ".") || &BailOut("Cannot open Sophos/lib directory"); foreach $vdlname (sort readdir(LIBDIR)) { next unless $vdlname =~ /^vdl-(\d+)\.(\d+)\.dat$/; $MajorVer = $1; $MinorVer = $2; } closedir(LIBDIR); &BailOut("Could not calculate Sophos version number") unless defined($MajorVer) && defined($MinorVer); $SophosVersion = "$MajorVer$MinorVer"; $VDLVersion = "$MajorVer.$MinorVer"; # Derive other filenames and URLs from this $ZipName = $SophosVersion . "_ides.zip"; $URL = "http://www.sophos.com/downloads/ide/$ZipName"; ($min,$hour,$date,$month,$year) = (localtime)[1,2,3,4,5]; $month++; $year+=1900; $IDEDir = "$SophosRoot/$SophosVersion." . sprintf("%04d%02d%02d%02d%02d", $year, $month, $date, $hour, $min); #print "Sophos Version is $SophosVersion\n"; #print "VDL Version is $VDLVersion\n"; # If the directory already exists, then we have already done the update # for today, so quietly exit. Sys::Syslog::syslog('info', "Sophos already up-to-date"),exit 0 if -d $IDEDir; # Create the IDE directory for today umask 0022; mkdir $IDEDir, 0755; chdir $IDEDir or &BailOut("Cannot cd $IDEDir, $!"); # Fetch and unpack the IDE zip file from Sophos $result = system("$Lynx $URL > $ZipName"); &BailOut("Lynx failed with error return " . ($result>>8) . "\n") if $result>>8; $result = system("$Unzip $ZipName"); &BailOut("Unzip failed with error return " . ($result>>8) . "\n") if $result>>8; symlink("$VDLDir/vdl-$VDLVersion.dat", "vdl.dat"); # Link in this new directory to Sophos chdir $SophosRoot or &BailOut("Cannot cd $SophosRoot, $!"); $OldLinkTarget = readlink $IDELink; &LockSophos(); unlink $IDELink if -l $IDELink; symlink $IDEDir, $IDELink; &UnlockSophos(); system("$rm -rf $OldLinkTarget") if defined $OldLinkTarget && -e $OldLinkTarget; Sys::Syslog::syslog('info', "Sophos successfully updated in $IDEDir"); Sys::Syslog::closelog(); exit 0; sub BailOut { Sys::Syslog::syslog('err', @_); Sys::Syslog::closelog(); warn "@_, $!"; chdir $SophosRoot or die "Cannot cd $SophosRoot, $!"; system("$rm -rf $IDEDir") if -d $IDEDir; exit 1; } sub LockSophos { open(LOCK, ">$LockFile") or return; flock(LOCK, $LOCK_EX); print LOCK "Locked for updating Sophos IDE files by $$\n"; } sub UnlockSophos { print LOCK "Unlocked after updating Sophos IDE files by $$\n"; flock(LOCK, $LOCK_UN); close LOCK; }