|
Tomáš Mráz |
ca47f6 |
#!/bin/bash
|
|
Tomáš Mráz |
ca47f6 |
#
|
|
Tomáš Mráz |
d93958 |
# sshd Start up the OpenSSH server daemon
|
|
Tomáš Mráz |
ca47f6 |
#
|
|
Tomáš Mráz |
ca47f6 |
# chkconfig: 2345 55 25
|
|
Tomáš Mráz |
d93958 |
# description: SSH is a protocol for secure remote shell access. \
|
|
Tomáš Mráz |
d93958 |
# This service starts up the OpenSSH server daemon.
|
|
Tomáš Mráz |
ca47f6 |
#
|
|
Tomáš Mráz |
ca47f6 |
# processname: sshd
|
|
Tomáš Mráz |
ca47f6 |
# config: /etc/ssh/ssh_host_key
|
|
Tomáš Mráz |
ca47f6 |
# config: /etc/ssh/ssh_host_key.pub
|
|
Tomáš Mráz |
ca47f6 |
# config: /etc/ssh/ssh_random_seed
|
|
Tomáš Mráz |
ca47f6 |
# config: /etc/ssh/sshd_config
|
|
Tomáš Mráz |
ca47f6 |
# pidfile: /var/run/sshd.pid
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
d93958 |
### BEGIN INIT INFO
|
|
Tomáš Mráz |
d93958 |
# Provides: sshd
|
|
Tomáš Mráz |
d93958 |
# Required-Start: $local_fs $network $syslog
|
|
Tomáš Mráz |
d93958 |
# Required-Stop: $local_fs $syslog
|
|
Tomáš Mráz |
d93958 |
# Should-Start: $syslog
|
|
Tomáš Mráz |
d93958 |
# Should-Stop: $network $syslog
|
|
Tomáš Mráz |
d93958 |
# Default-Start: 2 3 4 5
|
|
Tomáš Mráz |
d93958 |
# Default-Stop: 0 1 6
|
|
Tomáš Mráz |
d93958 |
# Short-Description: Start up the OpenSSH server daemon
|
|
Tomáš Mráz |
d93958 |
# Description: SSH is a protocol for secure remote shell access.
|
|
Tomáš Mráz |
d93958 |
# This service starts up the OpenSSH server daemon.
|
|
Tomáš Mráz |
d93958 |
### END INIT INFO
|
|
Tomáš Mráz |
d93958 |
|
|
Tomáš Mráz |
ca47f6 |
# source function library
|
|
Tomáš Mráz |
ca47f6 |
. /etc/rc.d/init.d/functions
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
# pull in sysconfig settings
|
|
Tomáš Mráz |
ca47f6 |
[ -f /etc/sysconfig/sshd ] && . /etc/sysconfig/sshd
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
RETVAL=0
|
|
Tomáš Mráz |
ca47f6 |
prog="sshd"
|
|
Tomáš Mráz |
d93958 |
lockfile=/var/lock/subsys/$prog
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
# Some functions to make the below more readable
|
|
Tomáš Mráz |
ca47f6 |
KEYGEN=/usr/bin/ssh-keygen
|
|
Tomáš Mráz |
ca47f6 |
SSHD=/usr/sbin/sshd
|
|
Tomáš Mráz |
ca47f6 |
RSA1_KEY=/etc/ssh/ssh_host_key
|
|
Tomáš Mráz |
ca47f6 |
RSA_KEY=/etc/ssh/ssh_host_rsa_key
|
|
Tomáš Mráz |
ca47f6 |
DSA_KEY=/etc/ssh/ssh_host_dsa_key
|
|
Tomáš Mráz |
ca47f6 |
PID_FILE=/var/run/sshd.pid
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
runlevel=$(set -- $(runlevel); eval "echo \$$#" )
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
do_rsa1_keygen() {
|
|
Tomáš Mráz |
ca47f6 |
if [ ! -s $RSA1_KEY ]; then
|
|
Tomáš Mráz |
ca47f6 |
echo -n $"Generating SSH1 RSA host key: "
|
|
Tomáš Mráz |
ca47f6 |
if $KEYGEN -q -t rsa1 -f $RSA1_KEY -C '' -N '' >&/dev/null; then
|
|
Tomáš Mráz |
ca47f6 |
chmod 600 $RSA1_KEY
|
|
Tomáš Mráz |
ca47f6 |
chmod 644 $RSA1_KEY.pub
|
|
Tomáš Mráz |
ca47f6 |
if [ -x /sbin/restorecon ]; then
|
|
Tomáš Mráz |
ca47f6 |
/sbin/restorecon $RSA1_KEY.pub
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
success $"RSA1 key generation"
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
ca47f6 |
else
|
|
Tomáš Mráz |
ca47f6 |
failure $"RSA1 key generation"
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
ca47f6 |
exit 1
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
}
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
do_rsa_keygen() {
|
|
Tomáš Mráz |
ca47f6 |
if [ ! -s $RSA_KEY ]; then
|
|
Tomáš Mráz |
ca47f6 |
echo -n $"Generating SSH2 RSA host key: "
|
|
Tomáš Mráz |
ca47f6 |
if $KEYGEN -q -t rsa -f $RSA_KEY -C '' -N '' >&/dev/null; then
|
|
Tomáš Mráz |
ca47f6 |
chmod 600 $RSA_KEY
|
|
Tomáš Mráz |
ca47f6 |
chmod 644 $RSA_KEY.pub
|
|
Tomáš Mráz |
ca47f6 |
if [ -x /sbin/restorecon ]; then
|
|
Tomáš Mráz |
ca47f6 |
/sbin/restorecon $RSA_KEY.pub
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
success $"RSA key generation"
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
ca47f6 |
else
|
|
Tomáš Mráz |
ca47f6 |
failure $"RSA key generation"
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
ca47f6 |
exit 1
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
}
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
do_dsa_keygen() {
|
|
Tomáš Mráz |
ca47f6 |
if [ ! -s $DSA_KEY ]; then
|
|
Tomáš Mráz |
ca47f6 |
echo -n $"Generating SSH2 DSA host key: "
|
|
Tomáš Mráz |
ca47f6 |
if $KEYGEN -q -t dsa -f $DSA_KEY -C '' -N '' >&/dev/null; then
|
|
Tomáš Mráz |
ca47f6 |
chmod 600 $DSA_KEY
|
|
Tomáš Mráz |
ca47f6 |
chmod 644 $DSA_KEY.pub
|
|
Tomáš Mráz |
ca47f6 |
if [ -x /sbin/restorecon ]; then
|
|
Tomáš Mráz |
ca47f6 |
/sbin/restorecon $DSA_KEY.pub
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
success $"DSA key generation"
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
ca47f6 |
else
|
|
Tomáš Mráz |
ca47f6 |
failure $"DSA key generation"
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
ca47f6 |
exit 1
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
}
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
do_restart_sanity_check()
|
|
Tomáš Mráz |
ca47f6 |
{
|
|
Tomáš Mráz |
ca47f6 |
$SSHD -t
|
|
Tomáš Mráz |
ca47f6 |
RETVAL=$?
|
|
Tomáš Mráz |
ca47f6 |
if [ ! "$RETVAL" = 0 ]; then
|
|
Tomáš Mráz |
ca47f6 |
failure $"Configuration file or keys are invalid"
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
}
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
start()
|
|
Tomáš Mráz |
ca47f6 |
{
|
|
Tomáš Mráz |
d93958 |
[ -x $SSHD ] || exit 5
|
|
Tomáš Mráz |
d93958 |
[ -f /etc/ssh/sshd_config ] || exit 6
|
|
Tomáš Mráz |
ca47f6 |
# Create keys if necessary
|
|
Tomáš Mráz |
ca47f6 |
if [ "x${AUTOCREATE_SERVER_KEYS}" != xNO ]; then
|
|
Tomáš Mráz |
ca47f6 |
do_rsa1_keygen
|
|
Tomáš Mráz |
ca47f6 |
do_rsa_keygen
|
|
Tomáš Mráz |
ca47f6 |
do_dsa_keygen
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
echo -n $"Starting $prog: "
|
|
Tomáš Mráz |
ca47f6 |
$SSHD $OPTIONS && success || failure
|
|
Tomáš Mráz |
ca47f6 |
RETVAL=$?
|
|
Tomáš Mráz |
d93958 |
[ "$RETVAL" = 0 ] && touch $lockfile
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
d93958 |
return $RETVAL
|
|
Tomáš Mráz |
ca47f6 |
}
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
stop()
|
|
Tomáš Mráz |
ca47f6 |
{
|
|
Tomáš Mráz |
ca47f6 |
echo -n $"Stopping $prog: "
|
|
Tomáš Mráz |
ca47f6 |
if [ -n "`pidfileofproc $SSHD`" ] ; then
|
|
Tomáš Mráz |
ca47f6 |
killproc $SSHD
|
|
Tomáš Mráz |
ca47f6 |
else
|
|
Tomáš Mráz |
ca47f6 |
failure $"Stopping $prog"
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
RETVAL=$?
|
|
Tomáš Mráz |
ca47f6 |
# if we are in halt or reboot runlevel kill all running sessions
|
|
Tomáš Mráz |
ca47f6 |
# so the TCP connections are closed cleanly
|
|
Tomáš Mráz |
ca47f6 |
if [ "x$runlevel" = x0 -o "x$runlevel" = x6 ] ; then
|
|
Tomáš Mráz |
ca47f6 |
trap '' TERM
|
|
Tomáš Mráz |
ca47f6 |
killall $prog 2>/dev/null
|
|
Tomáš Mráz |
ca47f6 |
trap TERM
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
d93958 |
[ "$RETVAL" = 0 ] && rm -f $lockfile
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
ca47f6 |
}
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
ca47f6 |
reload()
|
|
Tomáš Mráz |
ca47f6 |
{
|
|
Tomáš Mráz |
ca47f6 |
echo -n $"Reloading $prog: "
|
|
Tomáš Mráz |
ca47f6 |
if [ -n "`pidfileofproc $SSHD`" ] ; then
|
|
Tomáš Mráz |
ca47f6 |
killproc $SSHD -HUP
|
|
Tomáš Mráz |
ca47f6 |
else
|
|
Tomáš Mráz |
ca47f6 |
failure $"Reloading $prog"
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
RETVAL=$?
|
|
Tomáš Mráz |
ca47f6 |
echo
|
|
Tomáš Mráz |
ca47f6 |
}
|
|
Tomáš Mráz |
ca47f6 |
|
|
Tomáš Mráz |
d93958 |
restart() {
|
|
Tomáš Mráz |
d93958 |
stop
|
|
Tomáš Mráz |
d93958 |
start
|
|
Tomáš Mráz |
d93958 |
}
|
|
Tomáš Mráz |
d93958 |
|
|
Tomáš Mráz |
d93958 |
force_reload() {
|
|
Tomáš Mráz |
d93958 |
restart
|
|
Tomáš Mráz |
d93958 |
}
|
|
Tomáš Mráz |
d93958 |
|
|
Tomáš Mráz |
d93958 |
rh_status() {
|
|
Tomáš Mráz |
d93958 |
status -p $PID_FILE openssh-daemon
|
|
Tomáš Mráz |
d93958 |
}
|
|
Tomáš Mráz |
d93958 |
|
|
Tomáš Mráz |
d93958 |
rh_status_q() {
|
|
Tomáš Mráz |
d93958 |
rh_status >/dev/null 2>&1
|
|
Tomáš Mráz |
d93958 |
}
|
|
Tomáš Mráz |
d93958 |
|
|
Tomáš Mráz |
ca47f6 |
case "$1" in
|
|
Tomáš Mráz |
ca47f6 |
start)
|
|
Tomáš Mráz |
d93958 |
rh_status_q && exit 0
|
|
Tomáš Mráz |
ca47f6 |
start
|
|
Tomáš Mráz |
ca47f6 |
;;
|
|
Tomáš Mráz |
ca47f6 |
stop)
|
|
Tomáš Mráz |
d93958 |
rh_status_q || exit 0
|
|
Tomáš Mráz |
ca47f6 |
stop
|
|
Tomáš Mráz |
ca47f6 |
;;
|
|
Tomáš Mráz |
ca47f6 |
restart)
|
|
Tomáš Mráz |
d93958 |
restart
|
|
Tomáš Mráz |
ca47f6 |
;;
|
|
Tomáš Mráz |
ca47f6 |
reload)
|
|
Tomáš Mráz |
d93958 |
rh_status_q || exit 7
|
|
Tomáš Mráz |
ca47f6 |
reload
|
|
Tomáš Mráz |
ca47f6 |
;;
|
|
Tomáš Mráz |
d93958 |
force-reload)
|
|
Tomáš Mráz |
d93958 |
force_reload
|
|
Tomáš Mráz |
d93958 |
;;
|
|
Tomáš Mráz |
d93958 |
condrestart|try-restart)
|
|
Tomáš Mráz |
d93958 |
rh_status_q || exit 0
|
|
Tomáš Mráz |
d93958 |
if [ -f $lockfile ] ; then
|
|
Tomáš Mráz |
ca47f6 |
do_restart_sanity_check
|
|
Tomáš Mráz |
ca47f6 |
if [ "$RETVAL" = 0 ] ; then
|
|
Tomáš Mráz |
ca47f6 |
stop
|
|
Tomáš Mráz |
ca47f6 |
# avoid race
|
|
Tomáš Mráz |
ca47f6 |
sleep 3
|
|
Tomáš Mráz |
ca47f6 |
start
|
|
Tomáš Mráz |
d93958 |
else
|
|
Tomáš Mráz |
d93958 |
RETVAL=6
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
fi
|
|
Tomáš Mráz |
ca47f6 |
;;
|
|
Tomáš Mráz |
ca47f6 |
status)
|
|
Tomáš Mráz |
d93958 |
rh_status
|
|
Tomáš Mráz |
ca47f6 |
RETVAL=$?
|
|
Tomáš Mráz |
ca47f6 |
;;
|
|
Tomáš Mráz |
ca47f6 |
*)
|
|
Tomáš Mráz |
d93958 |
echo $"Usage: $0 {start|stop|restart|reload|force-reload|condrestart|try-restart|status}"
|
|
Tomáš Mráz |
d93958 |
RETVAL=2
|
|
Tomáš Mráz |
ca47f6 |
esac
|
|
Tomáš Mráz |
ca47f6 |
exit $RETVAL
|