#!/bin/sh
# $Owl: Owl/packages/openssh/sshd.init,v 1.7 2005/11/16 13:21:53 solar Exp $
#
# chkconfig: 2345 55 25
# description: \
#	sshd (Secure Shell Daemon) is the daemon program for ssh(1). \
#	Together these programs replace rlogin and rsh, and provide \
#	secure encrypted communications between two untrusted hosts \
#	over an insecure network.
# processname: sshd
# config: /etc/ssh/sshd_config
# pidfile: /var/run/sshd.pid

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

OWL_STARTUP_ENABLE=1

function keygen_1()
{
	local TYPE=$1 FILE=$2
	if [ ! -s $FILE ]; then
		ssh-keygen -t $TYPE -C $TYPE -N '' -f $FILE
	fi
}

function keygen()
{
	keygen_1 rsa1 /etc/ssh/ssh_host_key &&
		keygen_1 dsa /etc/ssh/ssh_host_dsa_key &&
		keygen_1 rsa /etc/ssh/ssh_host_rsa_key
}

case "$1" in
start)
	keygen || exit
	if [ "0$OWL_STARTUP_VERSION" -ge 3 ]; then
		daemon --pidfile /var/run/sshd.pid --expect-user root sshd
	else
# Red Hat's implementation of daemon() would get confused by multiple
# sshd processes.  We don't bother echo'ing a nice message if this is
# installed on Red Hat; this is just sufficient to ensure the machine
# remains accessible.
		test ! -f /var/run/sshd.pid && /usr/sbin/sshd
	fi
	;;
stop)
	if [ "0$OWL_STARTUP_VERSION" -ge 3 ]; then
		killproc --pidfile /var/run/sshd.pid --expect-user root sshd
	else
		test -f /var/run/sshd.pid && killproc sshd
	fi
	;;
status)
	status --pidfile /var/run/sshd.pid --expect-user root sshd
	;;
restart)
	sshd -t || exit
	$0 stop
	$0 start
	;;
reload)
	sshd -t || exit
	if [ "0$OWL_STARTUP_VERSION" -ge 3 ]; then
		killproc --pidfile /var/run/sshd.pid --expect-user root \
			-HUP sshd
	else
		killproc sshd -HUP
	fi
	;;
keygen)
	keygen
	;;
*)
	echo "Usage: sshd {start|stop|status|restart|reload|keygen}"
	exit 1
esac

exit $?
