#!/bin/sh
########################################################################
# Begin sshd
#
# Description : Start sshd daemon
#
# Author      : Bruce Dubbs - bdubbs@linuxfromscratch.org
#
# Version     : LFS 7.0
#
########################################################################

### BEGIN INIT INFO
# Provides:            sshd
# Required-Start:      $network
# Should-Start:
# Required-Stop:       sendsignals
# Should-Stop:
# Default-Start:       3 4 5
# Default-Stop:        0 1 2 6
# Short-Description:   Starts sshd daemon.
# Description:         Starts sshd daemon.
# X-LFS-Provided-By:   LFS
### END INIT INFO

. /lib/lsb/init-functions

#$LastChangedBy: dj $
#$Date: 2012-06-18 18:42:28 -0500 (Mon, 18 Jun 2012) $

case "$1" in
    start)

        log_info_msg "Starting SSH Server..."
	if [ ! -f /etc/ssh/ssh_host_rsa_key ]; then
        	/usr/bin/ssh-keygen -t rsa -N "" -f /etc/ssh/ssh_host_rsa_key
	fi
	if [ ! -f /etc/ssh/ssh_host_dsa_key ]; then
        	/usr/bin/ssh-keygen -t dsa -N "" -f /etc/ssh/ssh_host_dsa_key
	fi
	if [ ! -f /etc/ssh/ssh_host_ecdsa_key ]; then
        	/usr/bin/ssh-keygen -t ecdsa -N "" -f /etc/ssh/ssh_host_ecdsa_key
	fi
	if [ ! -f /etc/ssh/ssh_host_ed25519_key ]; then
        	/usr/bin/ssh-keygen -t ed25519 -N "" -f /etc/ssh/ssh_host_ed25519_key
	fi
        start_daemon -f /usr/sbin/sshd
        evaluate_retval
        # Also prevent ssh from being killed by out of memory conditions
        sleep 1
        pid=`cat /run/sshd.pid 2>/dev/null`
        echo "-16" >/proc/${pid}/oom_score_adj
        ;;

    stop)
        log_info_msg "Stopping SSH Server..."
        killproc -p "/run/sshd.pid" /usr/sbin/sshd
        evaluate_retval
        ;;

    reload)
        log_info_msg "Reloading SSH Server..."
        pid=`cat /run/sshd.pid 2>/dev/null`

        if [ -n "${pid}" ]; then
           kill -HUP "${pid}"
        else
           (exit 1)
        fi

        evaluate_retval
        ;;

    restart)
        $0 stop
        sleep 1
        $0 start
        ;;

    status)
        statusproc /usr/sbin/sshd
        ;;

    *)
        echo "Usage: $0 {start|stop|reload|restart|status}"
        exit 1
        ;;
esac

# End sshd bootscript
