# Copyright (c) 1995-2000 SuSE GmbH Nuernberg, Germany.
#
# Author: Kurt Garloff <feedback@suse.de>
#
# etc/init.d/lshd
#
#   and symbolic its link
#
# /usr/sbin/rclshd
#
# System startup script for the sshd2 replacement lshd
#
### BEGIN INIT INFO
# Provides: lshd
# Required-Start: $network 
# X-SuSE-Should-Start: $syslog
# Required-Stop:  $network 
# X-SuSE-Should-Stop: $syslog
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    Start lshd to allow lsh/ssh2 connections to this host
### END INIT INFO

# Source SuSE config
test -e /etc/sysconfig/lshd || exit 6
. /etc/sysconfig/lshd

LSHD_BIN=/usr/sbin/lshd
test -x $LSHD_BIN || exit 5

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.

case "$1" in
    start)
	if ! test -f /var/lib/lsh/yarrow-seed-file; then
	    echo "Generating random number generator seed file"
	    echo " /var/lib/lsh/yarrow-seed-file."
	    echo "This may take a while."
	    echo "You may need to generate entropy by typing on your keyboard."
	    lsh-make-seed --server
	fi
        if ! test -f /etc/lsh_host_key ; then
	    echo "Generating /etc/lsh_host_key."
	    lsh-keygen -l 8 --server | lsh-writekey --server
	    # Everybody should be allow to verify the public key! 
	    # This improves security, whatever you think about it. (KG)
	    chmod 644 /etc/lsh_host_key.pub
        fi
	echo -n "Starting LSH daemon "
	if echo $LSHD_OPTS | grep -- "--daemonic" 2>&1 >/dev/null; then
	  startproc $LSHD_BIN $LSHD_OPTS
	  if rc_check; then
	    echo -n "(daemonic) "
	    usleep 200000;
	    if ! [ -e /var/run/lshd.pid ] || ! ps -h --pid `cat /var/run/lshd.pid` | grep "$LSHD_BIN" 2>&1 >/dev/null; then
	      rc_failed 1
	      rm -f /var/run/lshd.pid
	    fi
	  fi
	else
	  startproc $LSHD_BIN $LSHD_OPTS
	fi
	rc_status -v
	;;
    stop)
	echo -n "Shutting down LSH daemon:"
	killproc -TERM $LSHD_BIN
	rc_status -v
	rm -f /var/run/lshd.pid
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    try-restart)
	$0 stop >/dev/null && $0 start
	rc_status
	;;
    reload|force-reload)
        echo -n "Reload service lshd"
        killproc -HUP $LSHD_BIN
	rc_status -v
        ;;
    status)
	echo -n "Checking for service lshd: "
	checkproc $LSHD_BIN #&& echo OK || echo No process
	rc_status -v
	;;
    probe)
	# lshd is only configured by command line options
        test /etc/lsh_host_key -nt /var/run/lshd.pid && echo reload
        ;;
    *)
	echo "Usage: $0 {start|stop|status|restart|reload|probe}"
	exit 1
esac

# Return with approp. exit status
rc_exit
