#!/bin/sh -e
#
# /etc/init.d/honeyd
#
# Originally written by Miquel van Smoorenburg <miquels@drinkel.ow.org>.
# Modified for Debian GNU/Linux by Ian Murdock <imurdock@gnu.ai.mit.edu>.
# Modified for honeyd by Javier Fernandez-Sanguino <jfs@debian.org> 

PATH=/bin:/usr/bin:/sbin:/usr/sbin
# Daemon locations
DAEMON=/usr/bin/honeyd
# Daemon names
NAME=honeyd
# Pidfiles
PIDFILE=/var/run/honeyd.pid
# Labels
LABEL="Honeyd daemon"
DEFAULT=/etc/default/honeyd
LOGDIR="/var/log/honeypot"
DAEMONLOG="$LOGDIR/daemon.log"
# time to wait for daemons death, in seconds
DODTIME=5
# Users to run the daemons as
DAEMONUSER=honeyd

# Defaults (should be changed in the 
# /etc/default/honeyd file, not here)
RUN="no"
OPTIONS=""
INTERFACE=""

# Note: You should not need to modify anything below this.
test -x $DAEMON || exit 0
if [ "$(id -u)" != "0" ]
then
  echo "You must be root to start, stop or restart \"$LABEL\"."
  exit 1
fi

is_alive () {
# Check the status of the honeyd daemon
    ret=1
    if [ -f "$PIDFILE" ] ; then
    	pid=`cat $PIDFILE`
	if [ -n "$pid" ] && [ -e /proc/$pid ] ; then
		procname=`/bin/ps h -p $pid -C $DAEMON -U $DAEMONUSER`
		[ -n "$procname" ] && ret=0
	fi
    else
        procname=`/bin/ps h -C $DAEMON -U $DAEMONUSER`
	[ -n "$procname" ] && ret=0
    fi
    return $ret
}

not_configured () {
        echo "ERROR: $LABEL will not be started unless it is configured"
        if [ "$1" != "stop" ]
        then
                echo ""
                echo "Please configure its configuration and then edit $DEFAULT"
                echo "and set the \"RUN\" variable to \"yes\" in order to allow"
                echo "$LABEL to start."
        fi
        exit 0
}


# Read config (will override defaults)
# and check if it is configured
if [ -f "$DEFAULT" ] ; then
        . $DEFAULT
fi

trap "" 1
trap "" 15

# This is the network in which honeyd will work
DAEMONOPTS="-f /etc/honeypot/honeyd.conf -l $LOGDIR/honeyd.log"
DAEMONOPTS="$DAEMONOPTS -p /etc/honeypot/nmap.prints"
DAEMONOPTS="$DAEMONOPTS -a /etc/honeypot/nmap.assoc"
DAEMONOPTS="$DAEMONOPTS -0 /etc/honeypot/pf.os"
DAEMONOPTS="$DAEMONOPTS -x /etc/honeypot/xprobe2.conf"
# Does the user exist?
if getent passwd | grep -q "^$DAEMONUSER:"; then
        DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'`
        DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'`
	DAEMONOPTS="$DAEMONOPTS -u $DAEMONUID -g $DAEMONGID"
fi


# Extra options (given by the user in /etc/default files)
DAEMONOPTS="$DAEMONOPTS $OPTIONS"

# Have we defined an interface?
if [ "x$INTERFACE" != "x" ];
then
	INTERFACE="-i $INTERFACE"
fi



start()
{
    if [ "x$RUN" != "xyes" ] ; then
                not_configured
    fi
    date=`date -R`
    echo "$date - Starting honeyd" >>$LOGDIR/daemon.log
    start-stop-daemon --quiet --start --pidfile "$PIDFILE" --exec $DAEMON \
    	-- $DAEMONOPTS $INTERFACE $NETWORK >>$LOGDIR/daemon.log 2>&1
}

stop()
{
    date=`date -R`
    echo "$date - Stopping honeyd" >>$LOGDIR/daemon.log
    start-stop-daemon --quiet --stop --pidfile $PIDFILE --oknodo \
    	--exec $DAEMON >>$DAEMONLOG 2>&1
}

case "$1" in
  start)
    echo -n "Starting $LABEL: "
    start
    if is_alive ; then
	    echo "$NAME."
    else
    	    echo "already started."
    fi
    ;;
  stop)
    echo -n "Stopping $LABEL: "
    stop
    echo "$NAME."
    ;;
  restart)
    echo -n "Restarting $LABEL: "
    stop
    sleep "$DODTIME"s
    start
    if is_alive ; then
	    echo "$NAME."
    else
    	    echo "ERROR (please check $DAEMONLOG)"
    fi
    ;;
  status)
    echo -n "Status of $LABEL: "
    if [ "x$RUN" != "xyes" ] ; then
    		echo "not configured."
		exit 1
    fi
    if is_alive ; then
	    echo "alive."
    else
    	    echo "dead."
	    exit 1
    fi
    ;;
  reload|force-reload)
    if [ "x$RUN" != "xyes" ] ; then
                not_configured
    fi
    if is_alive ; then
    	echo -n "Reloading $LABEL configuration files"
	start-stop-daemon --stop --pidfile $PIDFILE --signal 1 --exec $DAEMON
	if is_alive ; then
	    echo "done."
	 else
    	    echo "ERROR (please check $DAEMONLOG)"
	    exit 1
	 fi
    else
    	echo "Cannot reload $LABEL as it is not alive."
	    exit 1
    fi
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload|status}"
    exit 1
    ;;
esac

exit 0
