#!/bin/bash
#
# SAVget Version 0.2.6
# http://www.pizzle.org/~kylus/savget.html
# An SGET-like script to update Sophos Antivirus IDE files on UNIX
# Copyright (C) 2002 Patrick Boyne <pjb@arangoth.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
########################### CONFIGURATION ###################################
LANG=C

# Set this to the directory SAVI's vdl.dat is located. If you change this, 
# make sure you leave off the last /. The default is /usr/local/sav.
SAVDIR="/var/lib64/sav"

# Run quietly all the time? If so, set this to 'true'
QUIET=false

# Set this to true to log all actions to Syslog
SYSLOG=false

# Set this to a temp directory 
TMP=/tmp

# The location of wget on the system, and any options (EXCEPT -O) you wish to
# use with the program. 
WGET="/usr/bin/wget -q"

# Specify how long, in days, IDE files should be allowed to remain on the 
# system. To my knowledge, Sophos is usually able to incorporate an IDE in
# the main vdl.dat two releases after it is created.
IDELIFE=60

# The following options need only be set if you plan on downloading registered
# products from Sophos. These are NOT needed to download virus IDE files.

# Set the following two variables to the username and password that was 
# provided to you by Sophos for downloading products. Note that these are
# NOT required to download IDE files. Also note that anyone in the 'sav' group
# will have the ability to see these.
SAVUSER=
SAVPASS=

# Set this to a COMMA-DELIMITED list of REGISTERED products you would like to 
# download. Note that you do NOT need to fill this out for IDE files.
SAVPROD=linux.intel.libc6.tar.Z

# If you use Vanja Hrustic's Sophie project, specify the location of the 
# pid file when it is running in the backgound.
SOPHIEPID="/var/run/sophie.pid"

######################### END OF CONFIGURATION ##############################

Print_Usage () {

    echo -e "\nSAVget 0.2.6"
    echo -e "Copyright (C) 2002 Patrick Boyne <pjb@arangoth.org>"
    echo -e "SAVget comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under the terms of the GNU General Public License.\n"
    echo "Usage: $0 [-ahklmqrv] [-d <dir>] [-g <ide>] [-p <p1,...>] [-u <u,p>]"
    echo "  -a          Update ALL IDEs for the current SAV version."
    echo "  -c          Clean old IDE files from \$SAVDIR"
    echo "  -d <dir>    Set local directory (override \$SAVDIR)."
    echo "  -g <ide>    Get a single IDE file (leave off the .ide)."
    echo "  -h          Print this help message."
    echo "  -k          Use Sophos' UK mirror (default is the US site)."
    echo "  -l          Log actions to syslog."
    echo "  -m          Use with Mutt to scan emails and download new IDE files."
    echo "  -p <p1,..>  Comma-delimited list of Registered Products to download."
    echo "  -q          Run quietly (only output errors)."
    echo "  -r          Retrieve default Registred Products."
    echo "  -u <u,p>    Specify username and password (username,pass)."
    echo "  -v          Version information."
    echo "  -z          Restart sophie (must be root!)."

}

IDEURL="http://www.sophos.com/downloads/ide"
SAVURL="http://downloads.sophos.com/sophos/products/full"
SAVZIP=$(/usr/bin/sweep -v | gawk '/^Product version / { print gensub("\\.","","",$4) }' | gawk -F. '{print $1}' | sed -e 's/[A-Za-z]//g')_ides.zip
TMPFILE=$TMP/.savget-$USER-$$-$RANDOM

# Simple function to parse the command line options
MODE=null
while getopts "acd:g:hklmp:qru:vz" OPT
do
    case $OPT in
        a)
            MODE=all
            ;;
        c)
            MODE=clean
            ;;
        d)
            SAVDIR=$OPTARG
            ;;
        g)
            MODE=get
            ARG=$OPTARG
            ;;

        h)
            MODE=help
            ;;
        
        k)
            IDEURL="http://www.sophos.co.uk/downloads/ide"
            SAVURL="http://downloads.sophos.co.uk/sophos/products/full"
            ;;

        l)
            SYSLOG=true
            ;;

        m)
            MODE=mail
            ;;
        p)
            MODE=prod
            SAVPROD=$OPTARG
            ;;
            
        q)
            QUIET=true
            ;;
            
        r) 
            MODE=retr
            ;;

        u)
            echo "$OPTARG" > $TMPFILE
            SAVUSER=`cut -d , -f 1 $TMPFILE`
            SAVPASS=`cut -d , -f 2 $TMPFILE`
            rm -rf $TMPFILE
            ;;
        v)
            echo -e "\nSAVget 0.2.6"
            echo -e "Copyright (C) 2002 Patrick Boyne <pjb@arangoth.org>"
            echo -e "http://www.pizzle.org/~kylus/savget.html"
            echo -e "SAVget comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under the terms of the GNU General Public License.\n" 
            exit 0
            ;;
        z)
            MODE=sophie
            ;;
    esac
done

if [ $# -eq 0 ]; then
    Print_Usage
    exit 2
fi

case $MODE in
    all)
        $WGET -O $SAVDIR/$SAVZIP $IDEURL/$SAVZIP
        if [ ! -s $SAVDIR/$SAVZIP ]; then
            echo "Error retrieving IDEs. Check network connection or URL."
            if [ "$SYSLOG" == "true" ]; then
                logger -i "SAVget: Error retrieving IDEs."
            fi
            rm -rf $SAVDIR/$SAVZIP
            exit 1
        else
            rm -rf $SAVDIR/*.ide
            unzip -d $SAVDIR $SAVDIR/$SAVZIP 1>/dev/null 2>&1
            if [ "$QUIET" != "true" ]; then
                echo "IDEs Updated."
            fi
            if [ "$SYSLOG" == "true" ]; then
                logger -i "SAVget: IDEs Updated."
            fi
            exit 0
        fi
        ;;

    clean)
        OLDIDE=`find $SAVDIR -name \*.ide -a -mtime +$IDELIFE`
        declare -a OLD
        if [ "$OLDIDE" == "" ]; then
            echo "No IDE Files in $SAVDIR are >= $IDELIFE days."
        else
            OLD=(${OLDIDE// / /})
            IDX=0
            ancient=${OLD[$IDX]}
            until [ "$ancient" == "" ]
            do
                rm -rf $ancient
                if [ "$QUIET" == "false" ]; then
                    echo "Removing old IDE File $ancient"
                fi
                if [ "$SYSLOG" == "true" ]; then
                    logger -i "SAVget: Removed old IDE $ancient."
                fi
                IDX=`expr $IDX + 1`
                ancient=${OLD[$IDX]}
            done
        fi
        exit 0
        ;;

    get)
        if [ -e $SAVDIR/$ARG.ide ]; then
            mv $SAVDIR/$ARG.ide $TMP/$ARG.ide.old
        fi
        $WGET -O $SAVDIR/$ARG.ide $IDEURL/$ARG.ide
        if [ ! -s $SAVDIR/$ARG.ide ]; then
            echo "Error retrieving $ARG.ide. Check network connection or filename."
            if [ "$SYSLOG" == "true" ]; then
                logger -i "SAVget: Error retrieving $ARG.ide."
            fi
            rm -rf $SAVDIR/$ARG.ide
            if [ -e $TMP/$ARG.ide.old ]; then
                mv $TMP/$ARG.ide.old $SAVDIR/$ARG.ide
            fi
            exit 1
        else
            chmod 644 $SAVDIR/$ARG.ide
            if [ "$QUIET" != "true" ]; then
                echo "Successfully retrieved $ARG.ide."
            fi
            if [ "$SYSLOG" == "true" ]; then
                logger -i "SAVget: Successfully retrieved $ARG.ide."
            fi
            exit 0
        fi
        ;;

    help)
        Print_Usage
        exit 0
        ;;

    mail)
        LEN=`expr length $IDEURL + 2`
        IDE=`grep -F .ide | cut -c $LEN-`
        if [ -e $SAVDIR/$IDE ]; then
            mv $SAVDIR/$IDE $TMP/$IDE.old
        fi
        $WGET -O $SAVDIR/$IDE $IDEURL/$IDE
        if [ ! -s $SAVDIR/$IDE ]; then
            echo "Error retrieving $IDE. Check network connection."
            if [ "$SYSLOG" == "true" ]; then
                logger -i "SAVget: Error retrieving $IDE."
            fi
            rm -rf $SAVDIR/$IDE
            if [ -e $TMP/$IDE.old ]; then
                mv $TMP/$IDE.old $SAVDIR/$IDE
            fi
            exit 1
        else 
            chmod 644 $SAVDIR/$IDE
            if [ "$QUIET" != "true" ]; then
                echo "Successfully retrieved $IDE."
            fi
            if [ "$SYSLOG" == "true" ]; then
                logger -i "Successfully retrieved $IDE."
            fi
            exit 0
        fi
        ;;

    prod | retr)
        if [ "$SAVUSER" == "" -o "$SAVPASS" == "" ]; then
            echo "Username and password needed to download registered products. Check configuration or use the -u option."
            exit 3
        fi
        if [ "$SAVPROD" == "" ]; then
            echo "No products selected! Check configuration or use the -p option."
            exit 4
        fi
    	declare -a SPA
	    SPA=(${SAVPROD//,/ /})
        INDEX=0
    	product=${SPA[$INDEX]}
        until [ "$product" == "" ] 
        do
	    $WGET -O $SAVDIR/$product --http-user $SAVUSER --http-passwd $SAVPASS $SAVURL/$product
            if [ ! -s $SAVDIR/$product ]; then
                echo "Error downloading $product. Check network connection, login information, and name of file."
                if [ "$SYSLOG" == "true" ]; then
                    logger -i "SAVget: Error retrieving registered product $product."
                fi
                exit 1
            else
                if [ "$QUIET" != "true" ]; then
                    echo "Successfully retrieved $product."
                fi
                INDEX=`expr $INDEX + 1`
                product=${SPA[$INDEX]}
            fi        
        done
        if [ "$SYSLOG" == "true" ]; then
            logger -i "SAVget: Successfully retrieved Registered Products."
        fi
        exit 0
        ;;

    sophie)
        if [ $UID -eq 0 ]; then
            if [ -e $SOPHIEPID ]; then
                kill -HUP `cat $SOPHIEPID`
				if [ "$QUIET" == "false" ]; then
	                echo "Sophie configuration reloaded."
				fi
				if [ "$SYSLOG" == "true" ]; then
					logger -i "Sophie configuration reloaded."
				fi
                exit 0
            else
                echo "Error! $SOPHIEPID does not exist. Check your configuration!"
                exit 1
            fi
        else
            echo "You must be root to restart Sophie."
            exit 1
        fi
        ;;

    *)
        Print_Usage
        exit 2    
esac
