#! /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 [ "${NETWORKING}" = "no" ]; then
	echo "WARNING: Networking is down. Portsentry service can't be runed."
	exit 1
fi

[ -f /usr/bin/portsentry ] || exit 0

# Check the modes we're running in...
unset allmodes;
is_yes "$PORTSENTRY_ATCP"; [ "$?" = 1 ] || {
    PORTSENTRY_TCP="0";
    PORTSENTRY_STCP="0";
    allmodes=" -atcp";
    }
is_yes "$PORTSENTRY_AUDP"; [ "$?" = 1 ] || {
    PORTSENTRY_UDP="0";
    PORTSENTRY_SUDP="0";
    allmodes="$allmodes -audp";
    }
is_yes $PORTSENTRY_TCP; [ "$?" = 1 ] || allmodes="$allmodes -tcp";
is_yes $PORTSENTRY_UDP; [ "$?" = 1 ] || allmodes="$allmodes -udp";
is_yes $PORTSENTRY_STCP; [ "$?" = 1 ] || allmodes="$allmodes -stcp";
is_yes $PORTSENTRY_SUDP; [ "$?" = 1 ] || allmodes="$allmodes -sudp";

case "$1" in
  start)
	# Check if the service is already running?
	for mode in $allmodes; do
	    if [ ! -f /var/lock/subsys/portsentry$mode ]; then
		msg_starting "portsentry$mode"
		daemon "portsentry $mode"
	    	RETVAL=$?
    		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/portsentry$mode
	    else
        	msg_Already_Running portsentry
	        exit 1
	    fi
	    done
	;;

  stop)
        # Stop daemons.
	cd /var/lock/subsys/;
	ls --color=none /var/lock/subsys/portsentry* >/dev/null 2>&1
	[ $? -eq "0" ] || {
	    msg_Not_Running portsentry
	    exit 1
	    }
	msg_stopping portsentry
        killproc portsentry
        rm -f /var/lock/subsys/portsentry* >/dev/null 2>&1	
	rm -f /var/run/portsentry.pid /var/run/portsentry.loc* >/dev/null 2>&1
	;;
  status)
	status portsentry
	;;
  restart|reload)
	$0 stop
	$0 start
	;;
  *)
	echo "Usage: inet {start|stop|status|restart|reload}"
	exit 1
esac

exit 0
