#!/bin/sh
# Download and install the EICAR Anti-Virus test file from the Internet
#
# (C) 2003 Marc Haber
# This script has been derived from update-ms-fonts by Eric Sharkey
# You may freely distribute this file under the terms of the GNU General
# Public License, version 2 or later.

#abort if anything goes wrong
set -e

if [ `id -u` != 0 ] ; then
  echo "update-eicar can only be run as root."
  exit -1
fi

for x in $* ; do
  if [ `echo $x | awk '{print substr($1,1,1)}'` = '-' ] ; then
    case `echo $x | awk '{print substr($1,2,1)}'` in
      q) QUIET_MODE=1;;
      c) CHECK_ONLY=1;;
    esac
  else
    LOCALCOPY=$x
  fi
done

if [ `echo $LOCALCOPY | tr [:upper:] [:lower:] `x = "nonex" ] ; then
  exit 0
fi

EXITCODE=0

URLROOT="http://www.eicar.org/download"
LOCALTARGETDIR="/var/lib/clamav-getfiles"
SCRATCHDIR=/tmp/clamav-getfiles

if [ "$SCRATCHDIR" = "$LOCALCOPY" ] ; then
  SCRATCHDIR=/tmp/clamav-getfiles-scratch
fi

mkdir -p -m 0755 $SCRATCHDIR
if [ -O $SCRATCHDIR ] ; then
  if [ `ls -ld $SCRATCHDIR | awk '{print substr($1,6,1) substr($1,9,1)}'` != '--' ] ; then
    echo "
update-eicar: Error: $SCRATCHDIR must have mode 755 permissions.

The directory $SCRATCHDIR is writable by non-root users.  Using this
directory would be a security risk.  Please remove this directory.
" 1>&2
    exit -3;
  fi
else
  echo "
update-eicar: Error: $SCRATCHDIR is not owned by root.

The directory $SCRATCHDIR exists but is not owned by root with group
root.  Using this directory would be a security risk.  Please remove it.
" 1>&2
  exit -2;
fi
cd $SCRATCHDIR

INFOFILE="eicar.info"

cat <<EOF > $INFOFILE
44d88612fea8a8f36de82e1278abb02f  eicar.com
EOF

for file in `awk '{print $2}' $INFOFILE` ; do
  if [ ! -e $LOCALTARGETDIR/$file ] || [ `md5sum $LOCALTARGETDIR/$file | awk '{print $1}'` != `grep $file $INFOFILE | awk '{print $1}'` ] ; then
    THISFILE=`grep $file $INFOFILE | awk '{print $2}'`
    echo "$file md5sum mismatch, file needs downloading"
    if ! echo $DWNLDFILES | grep -q $THISFILE ; then
      DWNLDFILES="$DWNLDFILES $THISFILE"
    fi
  fi
done

mkdir -p $LOCALTARGETDIR

if [ -n "$CHECK_ONLY" ] ; then
  if [ -n "$DWNLDFILES" ] ; then
    EXITCODE=1
  fi
elif [ -n "$DWNLDFILES" ] ; then 
  for ff in $DWNLDFILES; do
    if [ ! -e $ff.done ] || [ ! -e $ff ] ; then
      if [ -z "$LOCALCOPY" ] ; then
        if [ -n "$QUIET_MODE" ] ; then
          curl --quiet --remote-name $URLROOT/$ff
        else
	  echo curl --remote-name $URLROOT/$ff
          curl --remote-name $URLROOT/$ff
        fi
      else
        cp $LOCALCOPY/$ff .
      fi
      cp $ff $LOCALTARGETDIR
      touch $ff.done
    fi
  done
  #Add some level of predictability by folding everything to lower case
  for x in *; do
    y=`echo $x | tr '[A-Z]' '[a-z]'`
    if [ "$x" != "$y" ]; then
       mv "$x" "$y"
    fi
  done

  chmod 644 *

  awk '{print $2}'  $INFOFILE > /var/lib/clamav-getfiles/installedfiles
fi

rm -rf $SCRATCHDIR

if [ -z "$QUIETMODE" ] ; then
  if [ $EXITCODE = 0 ] ; then
    echo "EICAR Anti-Virus Test File downloaded and installed."
  else
    echo "EICAR Anti-Virus Test File needs to be updated."
  fi
fi

exit $EXITCODE
