ca47f63
#!/bin/bash
ca47f63
#
ca47f63
# Init file for OpenSSH server daemon
ca47f63
#
ca47f63
# chkconfig: 2345 55 25
ca47f63
# description: OpenSSH server daemon
ca47f63
#
ca47f63
# processname: sshd
ca47f63
# config: /etc/ssh/ssh_host_key
ca47f63
# config: /etc/ssh/ssh_host_key.pub
ca47f63
# config: /etc/ssh/ssh_random_seed
ca47f63
# config: /etc/ssh/sshd_config
ca47f63
# pidfile: /var/run/sshd.pid
ca47f63
ca47f63
# source function library
ca47f63
. /etc/rc.d/init.d/functions
ca47f63
ca47f63
# pull in sysconfig settings
ca47f63
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
ca47f63
ca47f63
RETVAL=0
ca47f63
prog="sshd"
ca47f63
ca47f63
# Some functions to make the below more readable
ca47f63
KEYGEN=/usr/bin/ssh-keygen
ca47f63
SSHD=/usr/sbin/sshd
ca47f63
RSA1_KEY=/etc/ssh/ssh_host_key
ca47f63
RSA_KEY=/etc/ssh/ssh_host_rsa_key
ca47f63
DSA_KEY=/etc/ssh/ssh_host_dsa_key
ca47f63
PID_FILE=/var/run/sshd.pid
ca47f63
ca47f63
runlevel=$(set -- $(runlevel); eval "echo \$$#" )
ca47f63
ca47f63
do_rsa1_keygen() {
ca47f63
	if [ ! -s $RSA1_KEY ]; then
ca47f63
		echo -n $"Generating SSH1 RSA host key: "
ca47f63
		if $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
ca47f63
			chmod 600 $RSA1_KEY
ca47f63
			chmod 644 $RSA1_KEY.pub
ca47f63
			if [ -x /sbin/restorecon ]; then
ca47f63
			    /sbin/restorecon $RSA1_KEY.pub
ca47f63
			fi
ca47f63
			success $"RSA1 key generation"
ca47f63
			echo
ca47f63
		else
ca47f63
			failure $"RSA1 key generation"
ca47f63
			echo
ca47f63
			exit 1
ca47f63
		fi
ca47f63
	fi
ca47f63
}
ca47f63
ca47f63
do_rsa_keygen() {
ca47f63
	if [ ! -s $RSA_KEY ]; then
ca47f63
		echo -n $"Generating SSH2 RSA host key: "
ca47f63
		if $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
ca47f63
			chmod 600 $RSA_KEY
ca47f63
			chmod 644 $RSA_KEY.pub
ca47f63
			if [ -x /sbin/restorecon ]; then
ca47f63
			    /sbin/restorecon $RSA_KEY.pub
ca47f63
			fi
ca47f63
			success $"RSA key generation"
ca47f63
			echo
ca47f63
		else
ca47f63
			failure $"RSA key generation"
ca47f63
			echo
ca47f63
			exit 1
ca47f63
		fi
ca47f63
	fi
ca47f63
}
ca47f63
ca47f63
do_dsa_keygen() {
ca47f63
	if [ ! -s $DSA_KEY ]; then
ca47f63
		echo -n $"Generating SSH2 DSA host key: "
ca47f63
		if $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
ca47f63
			chmod 600 $DSA_KEY
ca47f63
			chmod 644 $DSA_KEY.pub
ca47f63
			if [ -x /sbin/restorecon ]; then
ca47f63
			    /sbin/restorecon $DSA_KEY.pub
ca47f63
			fi
ca47f63
			success $"DSA key generation"
ca47f63
			echo
ca47f63
		else
ca47f63
			failure $"DSA key generation"
ca47f63
			echo
ca47f63
			exit 1
ca47f63
		fi
ca47f63
	fi
ca47f63
}
ca47f63
ca47f63
do_restart_sanity_check()
ca47f63
{
ca47f63
	$SSHD -t
ca47f63
	RETVAL=$?
ca47f63
	if [ ! "$RETVAL" = 0 ]; then
ca47f63
		failure $"Configuration file or keys are invalid"
ca47f63
		echo
ca47f63
	fi
ca47f63
}
ca47f63
ca47f63
start()
ca47f63
{
ca47f63
	# Create keys if necessary
ca47f63
	if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then
ca47f63
		do_rsa1_keygen
ca47f63
		do_rsa_keygen
ca47f63
		do_dsa_keygen
ca47f63
	fi
ca47f63
ca47f63
	echo -n $"Starting $prog: "
ca47f63
	$SSHD $OPTIONS && success || failure
ca47f63
	RETVAL=$?
ca47f63
	[ "$RETVAL" = 0 ] && touch /var/lock/subsys/sshd
ca47f63
	echo
ca47f63
}
ca47f63
ca47f63
stop()
ca47f63
{
ca47f63
	echo -n $"Stopping $prog: "
ca47f63
	if [ -n "`pidfileofproc $SSHD`" ] ; then
ca47f63
	    killproc $SSHD
ca47f63
	else
ca47f63
	    failure $"Stopping $prog"
ca47f63
	fi
ca47f63
	RETVAL=$?
ca47f63
	# if we are in halt or reboot runlevel kill all running sessions
ca47f63
	# so the TCP connections are closed cleanly
ca47f63
	if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
ca47f63
	    trap '' TERM
ca47f63
	    killall $prog 2>/dev/null
ca47f63
	    trap TERM
ca47f63
	fi
ca47f63
	[ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/sshd
ca47f63
	echo
ca47f63
}
ca47f63
ca47f63
reload()
ca47f63
{
ca47f63
	echo -n $"Reloading $prog: "
ca47f63
	if [ -n "`pidfileofproc $SSHD`" ] ; then
ca47f63
	    killproc $SSHD -HUP
ca47f63
	else
ca47f63
	    failure $"Reloading $prog"
ca47f63
	fi
ca47f63
	RETVAL=$?
ca47f63
	echo
ca47f63
}
ca47f63
ca47f63
case "$1" in
ca47f63
	start)
ca47f63
		start
ca47f63
		;;
ca47f63
	stop)
ca47f63
		stop
ca47f63
		;;
ca47f63
	restart)
ca47f63
		stop
ca47f63
		start
ca47f63
		;;
ca47f63
	reload)
ca47f63
		reload
ca47f63
		;;
ca47f63
	condrestart)
ca47f63
		if [ -f /var/lock/subsys/sshd ] ; then
ca47f63
			do_restart_sanity_check
ca47f63
			if [ "$RETVAL" = 0 ] ; then
ca47f63
				stop
ca47f63
				# avoid race
ca47f63
				sleep 3
ca47f63
				start
ca47f63
			fi
ca47f63
		fi
ca47f63
		;;
ca47f63
	status)
ca47f63
		status -p $PID_FILE openssh-daemon
ca47f63
		RETVAL=$?
ca47f63
		;;
ca47f63
	*)
ca47f63
		echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
ca47f63
		RETVAL=1
ca47f63
esac
ca47f63
exit $RETVAL