#! /bin/sh
#
# portsenry	Start the portsentry Port Scan Detector
#
# chkconfig:	345 98 05
# description:	Portsentry Port Scan Detector is part of the Abacus Project \
#		suite of tools. The Abacus Project is an initiative to release \
#		low-maintenance, generic, and reliable host based intrusion \
#		detection software to the Internet community.
# processname:	portsentry
# pidfile:	/var/run/portsentry.pid


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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/portsentry ] && . /etc/sysconfig/portsentry

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down Portsentry
		exit 1
	fi
else
	exit 0
fi

# Check the modes we're running in...
allmodes=""
if is_yes "$PORTSENTRY_ATCP"; then
	PORTSENTRY_TCP="0"
	PORTSENTRY_STCP="0"
	allmodes="$allmodes atcp"
fi

if is_yes "$PORTSENTRY_AUDP"; then
	PORTSENTRY_UDP="0"
	PORTSENTRY_SUDP="0"
	allmodes="$allmodes audp"
fi
if is_yes $PORTSENTRY_TCP; then
	allmodes="$allmodes tcp"
fi
if is_yes $PORTSENTRY_UDP; then
	allmodes="$allmodes udp"
fi
if is_yes $PORTSENTRY_STCP; then
	allmodes="$allmodes stcp"
fi
if is_yes $PORTSENTRY_SUDP; then
	allmodes="$allmodes sudp"
fi

RETVAL=0
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/portsentry ]; then
		for mode in $allmodes; do
			if [ ! -f /var/lock/subsys/portsentry-$mode ]; then
				msg_starting "portsentry ($mode)"
				daemon "portsentry -$mode"
				RET=$?
				[ $RET = 0 ] && touch /var/lock/subsys/portsentry-$mode
				[ $RETVAL -gt 0 ] && RETVAL=$RET
			else
				msg_already_running "portsentry ($mode)"
			fi
		done
		touch /var/lock/subsys/portsentry
	else
		msg_already_running portsentry
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/portsentry ]; then
		# Stop daemons.
		msg_stopping portsentry
		killproc portsentry
		rm -f /var/lock/subsys/portsentry*
	else
		msg_not_running portsentry
	fi
	;;
  status)
	status portsentry
	exit $?
	;;
  restart|force-reload)
	$0 stop
	$0 start
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
