rcolebaugh / rpms / openssh

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