#!/sbin/sh
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
# All rights reserved.
#
# ident	"@(#)sshd	1.1	01/09/19 SMI"
#
# If sshd is configured (/etc/ssh/sshd_config exists and is readable),
# the start it up.
# Checks to see if RSA, and DSA host keys are available
# if any of these keys are not present, the respective keys are created.

# this script is for openssh. this script is originally from Solaris 9. 

LD_LIBRARY_PATH=/usr/lib:/usr/local/lib
export LD_LIBRARY_PATH

# KEYDIR=/etc/ssh
KEYDIR=/etc/openssh
# KEYGEN="/usr/bin/ssh-keygen -q"
KEYGEN="/usr/local/bin/ssh-keygen -q"
PIDFILE=/var/run/sshd.pid

case $1 in 
'start')
	if [ -x /usr/local/bin/ssh-keygen ]; then
		if [ ! -f "$KEYDIR/ssh_host_rsa_key" ]; then
	     		echo "Creating new RSA public/private host key pair"
	     		$KEYGEN -f $KEYDIR/ssh_host_rsa_key -t rsa -N ''
		fi

		if [ ! -f "$KEYDIR/ssh_host_dsa_key" ]; then
	     		echo "Creating new DSA public/private host key pair"
	     		$KEYGEN -f $KEYDIR/ssh_host_dsa_key -t dsa -N ''
		fi     
	fi

	[ -x /usr/local/sbin/sshd ] && /usr/local/sbin/sshd  -f /etc/openssh/sshd_config &
	;;
'stop')
	#
	# If we are switching Run level downwards then we disconnect
	# all connections.
	#
	# Otherwise we just kill the master daemon that is listening
	# and leave the connections active
	if [ -z "$_INIT_RUN_LEVEL" ]; then
		set -- `/usr/bin/who -r`
		_INIT_RUN_LEVEL="$7"
		_INIT_PREV_LEVEL="$9"
	fi
	
	if [ $_INIT_RUN_LEVEL -lt $_INIT_PREV_LEVEL ]; then
		/usr/bin/pkill -u 0 -x sshd
	fi
	if [ -f "$PIDFILE" ]; then
		/usr/bin/kill -TERM `/usr/bin/cat $PIDFILE`
	fi
	;;

'restart')
	if [ -f "$PIDFILE" ]; then
		/usr/bin/kill -HUP `/usr/bin/cat $PIDFILE`
	fi
	;;
*)
	echo "Usage: $0 { start | stop }"
	exit 1
	;;
esac	
