On Thu, Apr 05, 2012 at 10:31:49PM +0200, Frank Guthausen wrote:
> 
> An improved version of the init script is included.

An updated version of the suggested ssh init script is
attached. One bug and several minor typos are corrected.
#! /bin/sh

### BEGIN INIT INFO
# Provides:             sshd
# Required-Start:       $remote_fs $syslog
# Required-Stop:        $remote_fs $syslog
# Default-Start:        2 3 4 5
# Default-Stop:         
# Short-Description:    OpenBSD Secure Shell server
### END INIT INFO

set -e

# /etc/init.d/ssh: start and stop the OpenBSD "secure shell(tm)" daemon

# If used to start other instances of an OpenSSH daemon:
# Make sure it does not interfere with the regular ssh daemon
# Don't forget to change ``Provides:'' when starting another instance at boot 
time via update-rc.d

check_conflict() {
if [ ${SSH_ID} = "ssh" ] ; then
        echo "this instance conflicts with regular sshd instance"
        exit 1
fi
}

check_conflict_pid() {
if [ ${PID_FILE} = "sshd.pid" ] ; then
        echo "this instance conflicts with pidfile for the regular sshd 
instance"
        exit 1
fi
}

SSH_ID=${0##*/}                                         # service is identified 
by basename of this script
#SSH_ID="ssh"                                           # comment or delete 
this line for non-default ssh daemon
#check_conflict                                         # uncomment this line 
for non-default ssh daemons
SCRIPT_NAME="/etc/init.d/${SSH_ID}"                     # default: 
/etc/init.d/ssh
DAEMON_PATH="/usr/sbin"
DAEMON_FILE="${SSH_ID}d"                                # this is a patched 
binary with changed hardcoded pidfile value
DAEMON_NAME="${DAEMON_PATH}/sshd"                       # default: 
/usr/sbin/sshd
#DAEMON_NAME="${DAEMON_PATH}/${DAEMON_FILE}"            # uncomment to use a 
patched version for non-default ssh daemon
                                                        # or a symlink to 
original sshd
PID_DIR="/var/run/${DAEMON_FILE}" # not PATH but DIR    # default: /var/run/sshd
PID_FILE="${PID_DIR}.pid"                               # default: 
/var/run/sshd.pid
                                                        # if not set, pidfile 
will default to hardcoded deamon value
#check_conflict_pid                                     # uncomment this line 
for non-default ssh daemons
DEFAULT_PATH="/etc/default"
DEFAULT_FILE=${SSH_ID}                                  # default: ssh
DEFAULT_NAME="${DEFAULT_PATH}/${DEFAULT_FILE}"          # default: 
/etc/default/ssh
CONFIG_PATH="/etc/ssh"
CONFIG_FILE="${DAEMON_FILE}_config"
CONFIG_NAME="${CONFIG_PATH}/${CONFIG_FILE}"             # default: 
/etc/ssh/sshd_config
NOT_TO_RUN_CHECK="${CONFIG_PATH}/${DAEMON_FILE}_not_to_be_run"
                                                        # default: 
/etc/ssh/sshd_not_to_be_run
LOG_ACTION_MSG="OpenBSD Secure Shell server not in use 
(${CONFIG_PATH}/${DAEMON_FILE}_not_to_be_run)"
                                                        # default:
                                                        # "OpenBSD Secure Shell 
server not in use (/etc/ssh/sshd_not_to_be_run)"

DAEMON_NAME_EXISTS_EXECUTABLE=1
test -x ${DAEMON_NAME} || DAEMON_NAME_EXISTS_EXECUTABLE=0
if [ ${DAEMON_NAME_EXISTS_EXECUTABLE} = 0 ] ; then
        echo "No executable daemon file"
        exit 1
fi

DAEMON_IS_OPENSSH=1
#( ${DAEMON_NAME} -\? 2>&1 | grep -q OpenSSH ) 2>/dev/null || (echo "no OpenSSH 
daemon" && exit 0)
( ${DAEMON_NAME} -\? 2>&1 | grep -q OpenSSH ) 2>/dev/null || DAEMON_IS_OPENSSH=0
if [ ${DAEMON_IS_OPENSSH} = 0 ] ; then
        echo "Not an OpenSSH daemon"
        exit 1
fi

umask 022

if test -f ${DEFAULT_NAME}; then
    . ${DEFAULT_NAME}
fi

. /lib/lsb/init-functions

if [ -n "$2" ]; then
    SSHD_OPTS="$SSHD_OPTS $2"
fi

# Now force a pidfile
SSHD_OPTS="$SSHD_OPTS -o PidFile=${PID_FILE}"

# Are we running from init?
run_by_init() {
    ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
}

check_for_no_start() {
    # forget it if we're trying to start, and /etc/ssh/sshd_not_to_be_run exists
    if [ -e ${NOT_TO_RUN_CHECK} ]; then 
        if [ "$1" = log_end_msg ]; then
            log_end_msg 0
        fi
        if ! run_by_init; then
            log_action_msg "${LOG_ACTION_MSG}"
        fi
        exit 0
    fi
}

check_dev_null() {
    if [ ! -c /dev/null ]; then
        if [ "$1" = log_end_msg ]; then
            log_end_msg 1 || true
        fi
        if ! run_by_init; then
            log_action_msg "/dev/null is not a character device!"
        fi
        exit 1
    fi
}

check_privsep_dir() {
    # Create the PrivSep empty dir if necessary
    if [ ! -d ${PID_DIR} ]; then
        mkdir ${PID_DIR}
        chmod 0755 ${PID_DIR}
    fi
}

check_config() {
    if [ ! -e ${NOT_TO_RUN_CHECK} ]; then
        ${DAEMON_NAME} $SSHD_OPTS -t || exit 1
    fi
}

export PATH="${PATH:+$PATH:}/usr/sbin:/sbin:/usr/local/sbin"

case "$1" in
  start)
        check_privsep_dir
        check_for_no_start
        check_dev_null
        log_daemon_msg "Starting OpenBSD Secure Shell server" "${DAEMON_FILE}"
        if start-stop-daemon --start --quiet --oknodo --pidfile ${PID_FILE} 
--exec ${DAEMON_NAME} -- $SSHD_OPTS -f ${CONFIG_NAME}; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;
  stop)
        log_daemon_msg "Stopping OpenBSD Secure Shell server" "${DAEMON_FILE}"
        if start-stop-daemon --stop --quiet --oknodo --pidfile ${PID_FILE}; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;

  reload|force-reload)
        check_for_no_start
        check_config
        log_daemon_msg "Reloading OpenBSD Secure Shell server's configuration" 
"${DAEMON_FILE}"
        if start-stop-daemon --stop --signal 1 --quiet --oknodo --pidfile 
${PID_FILE} --exec ${DAEMON_NAME} -- -f ${CONFIG_NAME}; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;

  restart)
        check_privsep_dir
        check_config
        log_daemon_msg "Restarting OpenBSD Secure Shell server" "${DAEMON_FILE}"
        start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile 
${PID_FILE}
        check_for_no_start log_end_msg
        check_dev_null log_end_msg
        if start-stop-daemon --start --quiet --oknodo --pidfile ${PID_FILE} 
--exec ${DAEMON_NAME} -- $SSHD_OPTS -f ${CONFIG_NAME}; then
            log_end_msg 0
        else
            log_end_msg 1
        fi
        ;;

  try-restart)
        check_privsep_dir
        check_config
        log_daemon_msg "Restarting OpenBSD Secure Shell server" "${DAEMON_FILE}"
        set +e
        start-stop-daemon --stop --quiet --retry 30 --pidfile ${PID_FILE}
        RET="$?"
        set -e
        case $RET in
            0)
                # old daemon stopped
                check_for_no_start log_end_msg
                check_dev_null log_end_msg
                if start-stop-daemon --start --quiet --oknodo --pidfile 
${PID_FILE} --exec ${DAEMON_NAME} -- $SSHD_OPTS -f ${CONFIG_NAME}; then
                    log_end_msg 0
                else
                    log_end_msg 1
                fi
                ;;
            1)
                # daemon not running
                log_progress_msg "(not running)"
                log_end_msg 0
                ;;
            *)
                # failed to stop
                log_progress_msg "(failed to stop)"
                log_end_msg 1
                ;;
        esac
        ;;

  status)
        status_of_proc -p ${PID_FILE} ${DAEMON_NAME} ${SSH_ID} && exit 0 || 
exit $?
        ;;

  *)
        log_action_msg "Usage: ${SCRIPT_NAME} 
{start|stop|reload|force-reload|restart|try-restart|status}"
        exit 1
esac

exit 0

Reply via email to