#!/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=/usr/bin/honeyd
DAEMON2=/usr/sbin/farpd
NAME=honeyd
NAME2=farpd
PIDFILE=/var/run/honeyd.pid
PIDFILE2=/var/run/farpd.pid
LABEL="Honeyd daemon"
LABEL2="Farpd daemon"
DEFAULT=/etc/default/honeyd
LOGDIR="/var/log/honeypot"
# time to wait for daemons death, in seconds
DODTIME=5

# 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.

not_configured () {
        echo "ERROR: $LABEL will not be started/stopped 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
	if [ "x$RUN" != "xyes" ] ; then
                not_configured
        fi
else
        not_configured
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 $OPTIONS"
# Have we defined an interface?
if [ "x$INTERFACE" != "x" ];
then
	INTERFACE="-i $INTERFACE"
fi


test -x $DAEMON || exit 0
if [ "$(id -u)" != "0" ]
then
  echo "You must be root to start, stop or restart \"$LABEL\"."
  exit 1
fi

start()
{
    if [ "x$RUN_ARPD" = "xyes" ] ; then
        echo -n "(starting $LABEL2 as requested: "
	if [ ! -x $DAEMON2 ] ; then
		echo "ERR: Cannot find $NAME2, aborting"
		exit 1
	fi
    	date=`date -R`
	echo "$date - Starting farpd" >>$LOGDIR/daemon.log
        start-stop-daemon --quiet --start --exec $DAEMON2 \
		-- $INTERFACE $NETWORK 
        echo "$NAME2 ) "
    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
    echo "$NAME."
}

stop()
{
    if [ "x$RUN_ARPD" = "xyes" ] ; then
        echo -n "(stopping also $LABEL2: "
    	date=`date -R`
	echo "$date - Stopping farpd" >>$LOGDIR/daemon.log
        start-stop-daemon --quiet --stop --oknodo --exec $DAEMON2 
        echo "$NAME2 ) "
    fi
    date=`date -R`
    echo "$date - Stopping honeyd" >>$LOGDIR/daemon.log
    start-stop-daemon --quiet --stop --pidfile $PIDFILE --oknodo --exec $DAEMON >>$LOGDIR/daemon.log 2>&1
    echo "$NAME."
}

case "$1" in
  start)
    echo -n "Starting $LABEL: "
    start
    ;;
  stop)
    echo -n "Stopping $LABEL: "
    stop
    ;;
  restart)
    echo -n "Restarting $LABEL: "
    stop
    sleep "$DODTIME"s
    start
    ;;
  reload|force-reload)
    echo "Reloading $LABEL configuration files"
    start-stop-daemon --stop --pidfile $PIDFILE --signal 1 --exec $DAEMON
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload}"
    exit 1
    ;;
esac

exit 0
