#!/bin/sh
INSPECT_DIR=/tmp #Каталог куда будут сохраняться письма для сканирования
SENDMAIL="/usr/sbin/sendmail -i"
VIRUSADMIN="info@unix.specialist.ru" # адрес для уведомлениий
EX_TEMPFAIL=75
EX_UNAVAILABLE=69
# строка для запуска spamassassin
FILTER_SPAMC="/usr/bin/spamc -u spamfilter -U /var/run/spamd.sock"
trap "rm -f $INSPECT_DIR/in.$$ $INSPECT_DIR/vr.$$ $INSPECT_DIR/vr1.$$" 0 1 2 3 15
# Проверка на спам
cat | $FILTER_SPAMC > $INSPECT_DIR/in.$$ || { echo Cannot save mail to file; exit
$EX_TEMPFAIL; }
# Проверка на вирусы
/usr/bin/clamscan -v -r --no-summary --stdout ${INSPECT_DIR}/in.$$>$INSPECT_DIR/vr.$$
# Результат проверки
AV_RESULT=$?
case "$AV_RESULT" in
0)
   # Проверено. Мин нет :)
   $SENDMAIL "$@" <${INSPECT_DIR}/in.$$
   exit 0
   ;;
1)
   # Обнаружен вирус. Посылаем уведомление админу
   echo "Subject: VIRUS FOUND" >> $INSPECT_DIR/vr1.$$
   echo >> $INSPECT_DIR/vr1.$$
   echo "************************************************" >> $INSPECT_DIR/vr1.$$
   echo "* MAIL                                            *" >> $INSPECT_DIR/vr1.$$
   echo "************************************************" >> $INSPECT_DIR/vr1.$$
   echo >> $INSPECT_DIR/vr1.$$
   # Включаем в отчет реальные адреса релеев
   grep Received $INSPECT_DIR/in.$$ >> $INSPECT_DIR/vr1.$$
   echo "Mail from: $2 (may be forget)" >> $INSPECT_DIR/vr1.$$
   echo "To: $4" >> $INSPECT_DIR/vr1.$$
   grep Subject $INSPECT_DIR/in.$$ >> $INSPECT_DIR/vr1.$$
   echo >> $INSPECT_DIR/vr1.$$
   echo "************************************************" >> $INSPECT_DIR/vr1.$$
   echo "* Virus(es)                                       *" >> $INSPECT_DIR/vr1.$$
   echo "************************************************" >> $INSPECT_DIR/vr1.$$
   # Включаем в отчет список вирусов
   cat $INSPECT_DIR/vr.$$ >> $INSPECT_DIR/vr1.$$
   $SENDMAIL -f $VIRUSADMIN -r $VIRUSADMIN -F "Antivirus" $VIRUSADMIN < $INSPECT_DIR/vr1.$$
   exit 0
   ;;
*)
   # Произошла ошибка в работе антивируса. Сообщим об ошибке админу
   echo "Subject: ANTIVIRUS FAILED" >> $INSPECT_DIR/vr1.$$
   echo >> $INSPECT_DIR/vr1.$$
   echo "************************************************" >> $INSPECT_DIR/vr1.$$
   echo "* Antivirus Failed with next problem              *" >> $INSPECT_DIR/vr1.$$
   echo "************************************************" >> $INSPECT_DIR/vr1.$$
   case "$AV_RESULT" in
      40)
          echo "* Unknown option passed.                        *" >> $INSPECT_DIR/vr1.$$
          ;;
      50)
          echo "* Database initialization error.                *" >> $INSPECT_DIR/vr1.$$
          ;;
      52)
          echo "* Not supported file type.                      *" >> $INSPECT_DIR/vr1.$$
          ;;
      53)
          echo "* Can't open directory.                         *" >> $INSPECT_DIR/vr1.$$
          ;;
      54)
          echo "* Can't open file. (ofm)                        *" >> $INSPECT_DIR/vr1.$$
          ;;
      55)
          echo "* Error reading file. (ofm)                     *" >> $INSPECT_DIR/vr1.$$
          ;;
      56)
          echo "* Can't stat input file / directory.            *" >> $INSPECT_DIR/vr1.$$
          ;;
      57)
          echo "* Can't get absolute path name of current       *" >> $INSPECT_DIR/vr1.$$
          echo "* working directory.                            *" >> $INSPECT_DIR/vr1.$$
          ;;
      58)
          echo "* I/O error, please check your filesystem.      *" >> $INSPECT_DIR/vr1.$$
          ;;
      59)
          echo "* Can't get information about current user      *" >> $INSPECT_DIR/vr1.$$
          echo "* from /etc/passwd.                             *" >> $INSPECT_DIR/vr1.$$
          ;;
      60)
          echo "* Can't get information about user              *" >> $INSPECT_DIR/vr1.$$
          echo "* clamav (default name) from /etc/passwd.       *" >> $INSPECT_DIR/vr1.$$
          ;;
      61)
          echo "* Can't fork.                                   *" >> $INSPECT_DIR/vr1.$$
          ;;
      63)
          echo "* Can't create temporary files/directories      *" >> $INSPECT_DIR/vr1.$$
          echo "* (check permissions).                          *" >> $INSPECT_DIR/vr1.$$
          ;;
      64)
          echo "* Can't write to temporary directory (please    *" >> $INSPECT_DIR/vr1.$$
          echo "* specify another one).                         *" >> $INSPECT_DIR/vr1.$$
          ;;
      70)
          echo "* Can't allocate and clear memory (calloc).     *" >> $INSPECT_DIR/vr1.$$
          ;;
     71)
         echo "* Can't allocate memory (malloc).              *" >> $INSPECT_DIR/vr1.$$
         ;;
     *)
         echo "Unknown error $AV_RESULT" >> $INSPECT_DIR/vr1.$$
         ;;
  esac
         echo "************************************************" >> $INSPECT_DIR/vr1.$$
  $SENDMAIL -f $VIRADMIN -r $VIRADMIN -F "Antivirus" "$VIRADMIN" < $INSPECT_DIR/vr1.$$
  exit $EX_TEMPFAIL
  ;;
esac
exit 0

