#!/bin/sh
#
# spfmilter    This shell script takes care of starting and stopping
#              spfmilter.
#
# chkconfig: 2345 79 31
# description: spfmilter is a mail filter that runs in conjunction with \
#              sendmail to check for forged sender addresses in incoming \
#              mail.
# processname: spfmilter
# config: /etc/mail/sendmail.cf /etc/sysconfig/spfmilter

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

# Source networking configuration.
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network

# Source spfmilter configuration.
SPFMILTER_OPTIONS=""
SPFMILTER_SOCKET=unix:/var/run/spfmilter/spfmilter.sock
SPFMILTER_PIDFILE=/var/run/spfmilter/spfmilter.pid
SPFMILTER_WHITELIST=/etc/mail/spfmilter-whitelist
SPFMILTER_FALLBACK=/etc/mail/spfmilter-fallback
export SPFMILTER_OPTIONS SPFMILTER_SOCKET SPFMILTER_PIDFILE SPFMILTER_WHITELIST SPFMILTER_FALLBACK
[ -f /etc/sysconfig/spfmilter ] && . /etc/sysconfig/spfmilter

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

[ -f /usr/sbin/sendmail ] || exit 0
[ -f /usr/sbin/spfmilter ] || exit 0

RETVAL=0

# Local version of "daemon" that uses pid file in spfmilter directory
spfdaemon() {

	# See if it's already running. Look *only* at the pid file.
	pid=
	if [ -f "$SPFMILTER_PIDFILE" ]; then
		p=`cat $SPFMILTER_PIDFILE`
		[ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid=$p
	fi
	[ -n "${pid:-}" ] && return

	# make sure it doesn't core dump anywhere; while this could mask
	# problems with the daemon, it also closes some security problems
	ulimit -S -c 0 >/dev/null 2>&1

	# if NICELEVEL is set in /etc/sysconfig/spfmilter, honor it
	if [ -n "$NICELEVEL" ]; then
		nice="nice -n $NICELEVEL"
	else
		nice=
	fi

	# Echo daemon
        [ "${BOOTUP:-}" = "verbose" -a -z "$LSB" ] && echo -n " spfmilter"

	# And start it up.
	$nice initlog $INITLOG_ARGS -c "$*"
	[ "$?" -eq 0 ] && success $"spfmilter startup" || failure $"spfmilter startup"
}

start() {
	if [ ! -f "$SPFMILTER_PIDFILE" ]; then
		echo -n $"Starting spfmilter: "
		spfdaemon /usr/sbin/spfmilter \
			--user spfmilt \
			--pidfile "$SPFMILTER_PIDFILE" \
			--fallback "$SPFMILTER_FALLBACK" \
			--whitelist "$SPFMILTER_WHITELIST" \
			$SPFMILTER_OPTIONS $SPFMILTER_SOCKET
		RETVAL=$?
		echo
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/spfmilter
	fi

	return $RETVAL
}

stop() {
	# See if it's already running. Look *only* at the pid file.
	pid=
	if [ -f "$SPFMILTER_PIDFILE" ]; then
		p=`cat $SPFMILTER_PIDFILE`
		[ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid=$p
	fi
	[ -z "${pid:-}" ] && rm -f "$SPFMILTER_PIDFILE" /var/lock/subsys/spfmilter && return

	echo -n $"Shutting down spfmilter: "
	# TERM first, then KILL if not dead
	kill -TERM $pid
	usleep 100000
	if checkpid $pid && sleep 1 &&
	   checkpid $pid && sleep 3 &&
	   checkpid $pid ; then
		kill -KILL $pid
		usleep 100000
	fi
 
	if checkpid $pid; then
		failure $"spfmilter shutdown"
		RETVAL=1
	else
		success $"spfmilter shutdown"
		rm -f "$SPFMILTER_PIDFILE"
		rm -f /var/lock/subsys/spfmilter
		RETVAL=0
	fi
	echo
	return $RETVAL
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	stop
	start
	RETVAL=$?
	;;
  condrestart)
	if [ -f /var/lock/subsys/spfmilter ]; then
	    stop
	    start
	    RETVAL=$?
	fi
	;;
  status)
	pid=
	if [ -f "$SPFMILTER_PIDFILE" ]; then
		p=`cat $SPFMILTER_PIDFILE`
		[ -z "${p//[0-9]/}" -a -d "/proc/$p" ] && pid=$p
		if [ -n "${pid:-}" ]; then
			echo $"spfmilter (pid $pid) is running..."
			exit 0
		else
			echo $"spfmilter dead but pid file exists"
			exit 1
		fi
	fi
	if [ -f /var/lock/subsys/spfmilter ]; then
		echo $"spfmilter dead but subsys locked"
		exit 2
	fi
	echo $"spfmilter is stopped"
	exit 3
	;;
  *)
	echo $"Usage: $0 {start|stop|restart|condrestart|status}"
	exit 1
	;;
esac

exit $RETVAL

