#!/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
QUARDIR=/var/spool/MailScanner/quarantine
INQDIR=/var/spool/mqueue.in
RUNAS=root
INPID=/var/run/sendmail.in.pid
OUTPID=/var/run/sendmail.out.pid
SMPID=/var/run/sm-client.pid
MSPID=/var/run/MailScanner.pid
MSPUSER=smmsp
MSPGROUP=smmsp
SENDMAIL=/usr/sbin/sendmail
POSTFIX=/usr/sbin/postfix
POSTFIXINCF=/etc/postfix.in
POSTFIXOUTCF=/etc/postfix
POSTFIXWORKOWNER=postfix
POSTFIXQUAROWNER=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 QUARDIR
export INQDIR
export RUNAS
export INPID
export OUTPID
export SMPID
export MSPID
export MSPUSER
export MSPGROUP
export SENDMAIL
export POSTFIX
export POSTFIXINCF
export POSTFIXOUTCF
export POSTFIXWORKOWNER
export POSTFIXQUAROWNER
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
    gprintf "Missing ZMailer configuration file: %s\n" "$ZMAILERCF"
    exit 1
  fi
  if [ ! -f $MAILBIN/zmailer  ]; then
    gprintf "Missing main ZMailer script: %s/zmailer\n" "$MAILBIN"
  fi
  if [ -z $POSTOFFICEIN ]; then
    gprintf "Incoming postoffice not defined in %s - patch zmailer configuration\n" "$ZMAILERCF"
    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
          success
          echo
          gprintf "Assuming you are using a single Postfix instance (hold queue method)\n"
        fi
      elif [ $MTA = 'exim' ]; then
        if test -x $EXIM ; then
          $EXIM -C $EXIMINCF -bd 2> /dev/null
          success
          echo
        else
          failure
          echo
          gprintf "Could not find Exim installation, see /etc/sysconfig/MailScanner\n"
        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
        gprintf "Invalid MTA in /etc/sysconfig/MailScanner\n"
      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
          gprintf "Could not find Postfix installation, see /etc/sysconfig/MailScanner\n"
        fi
      elif [ $MTA = 'exim' ]; then
        if test -x $EXIM ; then
          $EXIM -C $EXIMSENDCF -q15m 2> /dev/null
          success
          echo
        else
          failure
          echo
          gprintf "Could not find Exim installation, see /etc/sysconfig/MailScanner\n"
        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
        gprintf "Invalid MTA in /etc/sysconfig/MailScanner\n"
      fi
}

RETVAL=0

# See how we were called.
case "$1" in
  startin)
	# Start just incoming sendmail
	gprintf "Starting incoming %s only:\n" "$MTA"
	echo -n '         incoming' $MTA': '
	StartInSendmail
	;;
  startout)
	# Start just outgoing sendmail
	gprintf "Starting outgoing %s only:\n" "$MTA"
	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:       '
	if [ $MTA = 'postfix' ]; then
	  :> $MSPID
	  chown $RUNAS $MSPID
	  chown $POSTFIXWORKOWNER $WORKDIR
	  #chown $POSTFIXQUAROWNER $QUARDIR
	fi
	/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
	;;
  stopms)
	# Just kill MailScanner itself
	echo    'Shutting down MailScanner daemon:'
	echo -n '         MailScanner:       '
	killproc MailScanner -15
        touch /var/lock/subsys/MailScanner.off >/dev/null 2>&1
	rm -f /var/lock/subsys/MailScanner
	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
          [ -r $INPID ] && kill `head -1 $INPID` 2>/dev/null
          [ -r /var/run/sm-client.pid ] && \
          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
          gprintf "Invalid MTA in /etc/sysconfig/MailScanner\n"
        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
          [ -r $OUTPID ] && 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
          gprintf "Invalid MTA in /etc/sysconfig/MailScanner\n"
        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
          if [ -f $POSTFIXINCF/main.cf ]; then
            echo -n '    Incoming postfix:       '
            $POSTFIX -c $POSTFIXINCF reload 2>/dev/null
            success
            echo
          fi
          echo -n '    Outgoing postfix:       '
          $POSTFIX -c $POSTFIXOUTCF reload 2>/dev/null
          success
          echo
        fi
	;;
  restart)
	$0 stop
        #sleep $RESTART_DELAY
        echo -n 'Waiting for MailScanner to die gracefully '
        while ( ps ax | grep -q '[M]ailScanner:' )
        do
          echo -n .
          sleep 1
        done
        echo ' dead.'
        sleep 1
	$0 start
	RETVAL=$?
	;;
  *)
	 gprintf "Usage: service MailScanner {start|stop|status|restart|reload|startin|startout|stopms}\n"
	exit 1
esac

exit $RETVAL
