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