#!/sbin/sh
#
# SMF method script for svc:/ozone/sshd:default
# OpenSSH daemon from OZSWopenssh (/opt/ozsw)
#

SSHDIR=/etc/opt/ozsw/openssh
KEYGEN="/opt/ozsw/bin/ssh-keygen"
SSHD="/opt/ozsw/sbin/sshd"
PIDFILE=/var/run/ozone-sshd.pid

create_key() {
    keypath=$1
    keytype=$2
    if [ ! -f "$keypath" ]; then
        echo "Generating $keytype host key: $keypath"
        "$KEYGEN" -f "$keypath" -t "$keytype" -N ''
    fi
}

case "$1" in
start)
    create_key "$SSHDIR/ssh_host_rsa_key"     rsa
    create_key "$SSHDIR/ssh_host_ed25519_key" ed25519
    exec "$SSHD" -f "$SSHDIR/sshd_config" \
        -h "$SSHDIR/ssh_host_rsa_key" \
        -h "$SSHDIR/ssh_host_ed25519_key"
    ;;
stop)
    if [ -f "$PIDFILE" ]; then
        /usr/bin/kill "$(/usr/bin/cat "$PIDFILE")"
    fi
    ;;
refresh)
    if [ -f "$PIDFILE" ]; then
        /usr/bin/kill -HUP "$(/usr/bin/cat "$PIDFILE")"
    fi
    ;;
*)
    echo "Usage: $0 { start | stop | refresh }"
    exit 1
    ;;
esac

exit $?
