kentpeacock / rpms / openssh

Forked from rpms/openssh 2 years ago
Clone
b58e57
#!/bin/bash
b58e57
#
b58e57
# sshd		Start up the OpenSSH server daemon
b58e57
#
b58e57
# chkconfig: 2345 55 25
b58e57
# description: SSH is a protocol for secure remote shell access. \
b58e57
#              This service starts up the OpenSSH server daemon.
b58e57
#
b58e57
# processname: sshd
b58e57
# config: /etc/ssh/ssh_host_key
b58e57
# config: /etc/ssh/ssh_host_key.pub
b58e57
# config: /etc/ssh/ssh_random_seed
b58e57
# config: /etc/ssh/sshd_config
b58e57
# pidfile: /var/run/sshd.pid
b58e57
b58e57
### BEGIN INIT INFO
b58e57
# Provides: sshd
b58e57
# Required-Start: $local_fs $network $syslog
b58e57
# Required-Stop: $local_fs $syslog
b58e57
# Should-Start: $syslog
b58e57
# Should-Stop: $network $syslog
b58e57
# Default-Start: 2 3 4 5
b58e57
# Default-Stop: 0 1 6
b58e57
# Short-Description: Start up the OpenSSH server daemon
b58e57
# Description:       SSH is a protocol for secure remote shell access.
b58e57
#		     This service starts up the OpenSSH server daemon.
b58e57
### END INIT INFO
b58e57
b58e57
# source function library
b58e57
. /etc/rc.d/init.d/functions
b58e57
b58e57
# pull in sysconfig settings
b58e57
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
b58e57
b58e57
RETVAL=0
b58e57
prog="sshd"
b58e57
lockfile=/var/lock/subsys/$prog
b58e57
b58e57
# Some functions to make the below more readable
b58e57
SSHD=/usr/sbin/sshd
b58e57
XPID_FILE=/var/run/sshd.pid
b58e57
PID_FILE=/var/run/sshd-s.pid
b58e57
b58e57
runlevel=$(set -- $(runlevel); eval "echo \$$#" )
b58e57
b58e57
do_restart_sanity_check()
b58e57
{
b58e57
	$SSHD -t
b58e57
	RETVAL=$?
b58e57
	if [ $RETVAL -ne  0 ]; then
b58e57
		failure $"Configuration file or keys are invalid"
b58e57
		echo
b58e57
	fi
b58e57
}
b58e57
b58e57
start()
b58e57
{
b58e57
	[ -x $SSHD ] || exit 5
b58e57
	[ -f /etc/ssh/sshd_config ] || exit 6
b58e57
	# Create keys if necessary
b58e57
	/usr/sbin/sshd-keygen
b58e57
b58e57
	echo -n $"Starting $prog: "
b58e57
	$SSHD $OPTIONS && success || failure
b58e57
	RETVAL=$?
b58e57
	[ $RETVAL -eq 0 ] && touch $lockfile
b58e57
	[ $RETVAL -eq 0 ] && cp -f $XPID_FILE $PID_FILE
b58e57
	echo
b58e57
	return $RETVAL
b58e57
}
b58e57
b58e57
stop()
b58e57
{
b58e57
b58e57
	echo -n $"Stopping $prog: "
b58e57
	if [ ! -f "$PID_FILE" ]; then
b58e57
		# not running; per LSB standards this is "ok"
b58e57
		action $"Stopping $prog: " /bin/true
b58e57
		return 0
b58e57
	fi
b58e57
	PID=`cat "$PID_FILE"`
b58e57
	if [ -n "$PID" ]; then
b58e57
		/bin/kill "$PID" >/dev/null 2>&1
b58e57
		RETVAL=$?
b58e57
		if [ $RETVAL -eq 0 ]; then
b58e57
			RETVAL=1
b58e57
			action $"Stopping $prog: " /bin/false
b58e57
		else
b58e57
			action $"Stopping $prog: " /bin/true
b58e57
		fi
b58e57
	else
b58e57
		 # failed to read pidfile
b58e57
		action $"Stopping $prog: " /bin/false
b58e57
		RETVAL=4
b58e57
	fi
b58e57
	# if we are in halt or reboot runlevel kill all running sessions
b58e57
	# so the TCP connections are closed cleanly
b58e57
	if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
b58e57
	    trap '' TERM
b58e57
	    killall $prog 2>/dev/null
b58e57
	    trap TERM
b58e57
	fi
b58e57
	[ $RETVAL -eq 0 ] && rm -f $lockfile
b58e57
	rm -f "$PID_FILE"
b58e57
        return $RETVAL
b58e57
}
b58e57
b58e57
reload()
b58e57
{
b58e57
	echo -n $"Reloading $prog: "
b58e57
	if [ -n "`pidfileofproc $SSHD`" ] ; then
b58e57
	    killproc $SSHD -HUP
b58e57
	else
b58e57
	    failure $"Reloading $prog"
b58e57
	fi
b58e57
	RETVAL=$?
b58e57
	echo
b58e57
}
b58e57
b58e57
restart() {
b58e57
	stop
b58e57
	start
b58e57
}
b58e57
b58e57
force_reload() {
b58e57
	restart
b58e57
}
b58e57
b58e57
rh_status() {
b58e57
	status -p $PID_FILE openssh-daemon
b58e57
}
b58e57
b58e57
rh_status_q() {
b58e57
	rh_status >/dev/null 2>&1
b58e57
}
b58e57
b58e57
case "$1" in
b58e57
	start)
b58e57
		rh_status_q && exit 0
b58e57
		start
b58e57
		;;
b58e57
	stop)
b58e57
		if ! rh_status_q; then
b58e57
			rm -f $lockfile
b58e57
			exit 0
b58e57
		fi
b58e57
		stop
b58e57
		;;
b58e57
	restart)
b58e57
		restart
b58e57
		;;
b58e57
	reload)
b58e57
		rh_status_q || exit 7
b58e57
		reload
b58e57
		;;
b58e57
	force-reload)
b58e57
		force_reload
b58e57
		;;
b58e57
	condrestart|try-restart)
b58e57
		rh_status_q || exit 0
b58e57
		if [ -f $lockfile ] ; then
b58e57
			do_restart_sanity_check
b58e57
			if [ $RETVAL -eq 0 ] ; then
b58e57
				stop
b58e57
				# avoid race
b58e57
				sleep 3
b58e57
				start
b58e57
			else
b58e57
				RETVAL=6
b58e57
			fi
b58e57
		fi
b58e57
		;;
b58e57
	status)
b58e57
		rh_status
b58e57
		RETVAL=$?
b58e57
		if [ $RETVAL -eq 3 -a -f $lockfile ] ; then
b58e57
			RETVAL=2
b58e57
		fi
b58e57
		;;
b58e57
	*)
b58e57
		echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
b58e57
		RETVAL=2
b58e57
esac
b58e57
exit $RETVAL