#!/bin/bash
# /etc/rc.d/sshd — Dropbear SSH server on port 22.
#
# /sbin/sshd is a tiny launcher (userland/sshd/sshd.c) that execs /sbin/dropbear
# with the AnotherOS options. It binds 0.0.0.0 EXPLICITLY: we have no IPv6, and
# Dropbear treats the EAFNOSUPPORT from the passive wildcard's first (AF_INET6)
# result as fatal rather than falling through to the AF_INET one.
case "$1" in
    start)
        # A missing binary used to `exit 0` silently, which made a failed SSH
        # start indistinguishable from a disabled one — the reason this daemon
        # was dead at boot for so long without a word on the console.
        if [ ! -x /sbin/sshd ]; then
            echo "rc: sshd: /sbin/sshd missing or not executable" >&2
            exit 1
        fi
        # A release image carries NO host key (kernel.mk RELEASE_ISO): one
        # baked key would give every install the same identity, and anyone
        # holding the image holds its private half. Make this machine's own on
        # first start, as a distribution's first boot does.
        hk=/etc/dropbear/dropbear_ed25519_host_key
        if [ ! -s "$hk" ]; then
            mkdir -p /etc/dropbear
            /bin/dropbearkey -t ed25519 -f "$hk" >/dev/null 2>&1 \
                || { echo "rc: sshd: could not generate $hk" >&2; exit 1; }
            chmod 600 "$hk"
        fi
        /sbin/sshd &
        ;;
    stop)
        killall dropbear 2>/dev/null
        ;;
    *)
        echo "usage: $0 start|stop" >&2; exit 1 ;;
esac
exit 0
