#!/bin/bash
#
# mailscanner   This shell script takes care of starting and stopping
#               MailScanner, and its associated copies of sendmail.
#
# chkconfig: 2345 80 30
# description: MailScanner is an open-source E-Mail Gateway Virus Scanner.
# processname: MailScanner
# config: /etc/MailScanner/MailScanner.conf
# pidfile: /var/run/MailScanner.pid

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

#
# If you are using sendmail, Exim or Postfix, please try to avoid editing
# thie file. Edit /etc/sysconfig/MailScanner instead.
#
MTA=sendmail
QUEUETIME=15m
WORKDIR=/var/spool/MailScanner/incoming
INQDIR=/var/spool/mqueue.in
INPID=/var/run/sendmail.in.pid
OUTPID=/var/run/sendmail.out.pid
SMPID=/var/run/sm-client.pid
MSPUSER=smmsp
MSPGROUP=smmsp
SENDMAIL=/usr/sbin/sendmail
POSTFIX=/usr/sbin/postfix
POSTFIXINCF=/etc/postfix.in
POSTFIXOUTCF=/etc/postfix
EXIM=/usr/sbin/exim
EXIMINCF=/etc/exim.conf
EXIMSENDCF=/etc/exim_send.conf
ZMAILER=/usr/lib/zmailer/zmailer
ZMAILERCF=/etc/zmailer/zmailer.conf
RESTART_DELAY=10

# Source mailscanner configuration.
if [ -f /etc/sysconfig/MailScanner ] ; then
        . /etc/sysconfig/MailScanner
fi
export MTA
export QUEUETIME
export WORKDIR
export INQDIR
export INPID
export OUTPID
export SMPID
export MSPUSER
export MSPGROUP
export SENDMAIL
export POSTFIX
export POSTFIXINCF
export POSTFIXOUTCF
export EXIM
export EXIMINCF
export EXIMSENDCF
export ZMAILER
export ZMAILERCF
export RESTART_DELAY

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

[ -f /usr/sbin/check_MailScanner ] || exit 0
[ -f $SENDMAIL ] || exit 0


if [ $MTA = 'zmailer' ]; then
  # Source zmailer configuration.
  if [ -f $ZMAILERCF ]; then
    . $ZMAILERCF
  else
    echo "Missing ZMailer configuration file: $ZMAILERCF"
    exit 1
  fi
  if [ ! -f $MAILBIN/zmailer  ]; then
    echo "Missing main ZMailer script: $MAILBIN/zmailer"
  fi
  if [ -z $POSTOFFICEIN ]; then
    echo "Incoming postoffice not defined in $ZMAILERCF - patch zmailer configuration"
    exit 1
  fi
fi

# Start both the sendmail processes
StartInSendmail() {
      if [ $MTA = 'postfix' ]; then
        if test -x $POSTFIX ; then
          if [ -f $POSTFIXINCF/main.cf ]; then
            $POSTFIX -c $POSTFIXINCF start 2> /dev/null
          fi
          success
          echo
        else
          failure
          echo
          echo "Could not find Postfix installation, see /etc/sysconfig/MailScanner"
        fi
      elif [ $MTA = 'exim' ]; then
        if test -x $EXIM ; then
          $EXIM -C $EXIMINCF -bd 2> /dev/null
          success
          echo
        else
          failure
          echo
          echo "Could not find Exim installation, see /etc/sysconfig/MailScanner"
        fi
      elif [ $MTA = 'sendmail' ]; then
        /usr/bin/newaliases > /dev/null 2>&1
        if test -x /usr/bin/make -a -f /etc/mail/Makefile ; then
            make -C /etc/mail -s
        else
            for i in virtusertable access domaintable mailertable ; do
                if [ -f /etc/mail/$i ] ; then
                    makemap hash /etc/mail/$i < /etc/mail/$i
                fi
            done
        fi
        $SENDMAIL -bd -OPrivacyOptions=noetrn \
                      -ODeliveryMode=queueonly \
                      -OQueueDirectory=$INQDIR \
                      -OPidFile=$INPID
        touch /var/run/sm-client.pid
        chown $MSPUSER:$MSPGROUP /var/run/sm-client.pid 2>/dev/null
        $SENDMAIL -L sm-msp-queue -Ac -q15m -OPidFile=$SMPID 2>/dev/null
        success
        echo
      elif [ $MTA = 'zmailer' ]; then
        $MAILBIN/zmailer smtpserver >/tmp/zmailer_start_output.$$ 2>&1
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
          success
          echo
        else
          failure
          echo
          cat /tmp/zmailer_start_output.$$
        fi
        rm -f /tmp/zmailer_start_output.$$
      else
        failure
        echo
        echo "Invalid MTA in /etc/sysconfig/MailScanner"
      fi
}
StartOutSendmail() {
      if [ $MTA = 'postfix' ]; then
        if test -x $POSTFIX -a -f $POSTFIXOUTCF/main.cf ; then
          $POSTFIX -c $POSTFIXOUTCF start 2> /dev/null
          success
          echo
        else
          failure
          echo
          echo "Could not find Postfix installation, see /etc/sysconfig/MailScanner"
        fi
      elif [ $MTA = 'exim' ]; then
        if test -x $EXIM ; then
          $EXIM -C $EXIMSENDCF -q15m 2> /dev/null
          success
          echo
        else
          failure
          echo
          echo "Could not find Exim installation, see /etc/sysconfig/MailScanner"
        fi
      elif [ $MTA = 'sendmail' ]; then
        $SENDMAIL $([ -n "$QUEUETIME" ] && echo -q$QUEUETIME) \
                  -OPidFile=$OUTPID
        success
        echo
      elif [ $MTA = 'zmailer' ]; then
        $MAILBIN/zmailer router scheduler >/tmp/zmailer_start_output.$$ 2>&1
        RETVAL=$?
        if [ $RETVAL -eq 0 ]; then
          success
          echo
        else
          failure
          echo
          cat /tmp/zmailer_start_output.$$
        fi
        rm -f /tmp/zmailer_start_output.$$
      else
        failure
        echo
        echo "Invalid MTA in /etc/sysconfig/MailScanner"
      fi
}

RETVAL=0

# See how we were called.
case "$1" in
  startin)
	# Start just incoming sendmail
	echo "Starting incoming $MTA only:"
	echo -n '         incoming' $MTA': '
	StartInSendmail
	;;
  startout)
	# Start just outgoing sendmail
	echo "Starting outgoing $MTA only:"
	echo -n '         outgoing' $MTA': '
	StartOutSendmail
	;;
  start)
	# Start daemons.
	echo 'Starting MailScanner daemons:'
        echo -n '         incoming' $MTA': '
	StartInSendmail
        echo -n '         outgoing' $MTA': '
	StartOutSendmail
	echo -n '         MailScanner:       '
	/usr/sbin/check_MailScanner >/dev/null
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/MailScanner
        [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/MailScanner.off
	success
	echo
	;;
  stop)
	# Stop daemons.
	echo    'Shutting down MailScanner daemons:'
	echo -n '         MailScanner:       '
	killproc MailScanner -15
	echo
	echo -n '         incoming' $MTA': '
        if [ $MTA = "postfix" ]; then
          if [ -f $POSTFIXINCF/main.cf ]; then
            $POSTFIX -c $POSTFIXINCF stop 2> /dev/null
          fi
          success
        elif [ $MTA = "exim" ]; then
          killproc exim 2>/dev/null
          success
        elif [ $MTA = "sendmail" ]; then
          #killproc sendmail 2>/dev/null
          kill `head -1 $INPID` 2>/dev/null
          kill `head -1 /var/run/sm-client.pid` 2>/dev/null
          success
        elif [ $MTA = "zmailer" ]; then
          $MAILBIN/zmailer stop smtpserver >/tmp/zmailer_stop_output.$$ 2>&1
          RETVAL=$?
          if [ $RETVAL -eq 0 ]; then
            success
          else
            failure
            echo
            cat /tmp/zmailer_stop_output.$$
          fi
          rm -f /tmp/zmailer_stop_output.$$
	else
          failure
          echo
          echo "Invalid MTA in /etc/sysconfig/MailScanner"
        fi
	echo
	echo -n '         outgoing' $MTA': '
        if [ $MTA = "postfix" ]; then
          $POSTFIX -c $POSTFIXOUTCF stop 2>/dev/null
          success
        elif [ $MTA = "exim" ]; then
          success
        elif [ $MTA = "sendmail" ]; then
          #killproc /usr/sbin/sendmail 2>/dev/null
          kill `head -1 $OUTPID` 2>/dev/null
          success
        elif [ $MTA = "zmailer" ]; then
          $MAILBIN/zmailer stop router scheduler >/tmp/zmailer_stop_output.$$ 2>&1
          RETVAL=$?
          if [ $RETVAL -eq 0 ]; then
            success
          else
            failure
            echo
            cat /tmp/zmailer_stop_output.$$
          fi
          rm -f /tmp/zmailer_stop_output.$$
        else
          failure
          echo
          echo "Invalid MTA in /etc/sysconfig/MailScanner"
        fi
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/MailScanner
        [ $RETVAL -eq 0 ] && touch /var/lock/subsys/MailScanner.off
	# Clear out all the old pid files
	rm -f /var/run/MailScanner.pid
	# Clear out the old incoming dirs
	cd $WORKDIR && ls | egrep '^[0123456789]+$' | xargs /bin/rm -rf 2>/dev/null
	;;
  status)
        # Work out if all of MailScanner is running
	echo    'Checking MailScanner daemons:'
	echo -n '         MailScanner:       '
	pid=`pidofproc MailScanner`
	if [ -z "$pid" ] ; then failure; else success; fi
	echo
	if [ $MTA = "sendmail" ]; then
          # Now the incoming sendmail
          echo -n '         incoming sendmail: '
	  pid=`head -1 $INPID`
	  alive=`ps ax | awk '{ print $1 }' | grep '^'$pid'$'`
          #pid=`ps ax | egrep '\[sendmail\]|sendmai[l]: accepting connections'`
          if [ -z "$alive" ] ; then failure; else success; fi
          echo
          # Now the outgoing sendmail
          echo -n '         outgoing sendmail: '
	  pid=`head -1 $OUTPID`
	  alive=`ps ax | awk '{ print $1 }' | grep '^'$pid'$'`
          # More complex regexp to handle other RedHats
          #pid=`ps ax | egrep '\[sendmail\]|sendmai[l] -q[0-9]*[mhd]|sendmail: Queue runner' | grep -v grep`
          if [ -z "$alive" ] ; then failure; else success; fi
          echo
        elif [ $MTA = "zmailer" ]; then
          echo -n '         incoming zmailer: '
          if ( status smtpserver ) | grep stop > /dev/null ; then
            failure
          else
            success
          fi
          echo
          echo -n '         outgoing zmailer: '
          if ( status scheduler || status router ) | grep stop > /dev/null ; then
            failure
          else
            success
          fi
          echo
        fi
        ;;
  reload)
	echo    'Reloading MailScanner workers:'
        echo -n '         MailScanner:       '
	pid=`pidofproc MailScanner`
	if [ -z "$pid" ] ; then
	  failure
	else
	  /bin/kill -HUP -- -$pid
	  success
	fi
	echo
        if [ $MTA = "postfix" ]; then
          echo -n '    Incoming postfix:       '
          $POSTFIX -c $POSTFIXINCF reload >/dev/null
          success
          echo
          echo -n '    Outgoing postfix:       '
          $POSTFIX -c $POSTFIXOUTCF reload >/dev/null
          success
          echo
        fi
	;;
  restart)
	$0 stop
	sleep $RESTART_DELAY
	$0 start
	RETVAL=$?
	;;
  *)
	 echo "Usage: service MailScanner {start|stop|status|restart|reload|startin|startout}"
	exit 1
esac

exit $RETVAL
