#!/bin/sh
# Servury override of dropbear-initramfs' scripts/init-bottom/dropbear (same
# name in /etc/initramfs-tools/scripts wins). Same job as the packaged script:
# forbid new logins, end the SSH sessions, stop the listener, take the network
# down. One difference: a session that unlocked the disk and registered itself
# in /run/servury-handoff (see servury-handoff) is NOT ended. It has already
# entered the real root and carries on into the booted system, so its
# connection process is spared and, while such a session is alive on a root
# that uses netplan, the interface keeps its address (systemd-networkd takes
# it over).
PREREQ=""
prereqs() { echo "$PREREQ"; }
case "$1" in prereqs) prereqs; exit 0 ;; esac

. /scripts/functions
EXE="$(readlink -f /sbin/dropbear)" && [ -f "$EXE" ] || exit 1
PIDFILE="/run/dropbear.pid"
HANDOFF="/run/servury-handoff"

rm -f ~root/.ssh/authorized_keys

IFDOWN="*"
DROPBEAR_SHUTDOWN_TIMEOUT=60
if [ -e /etc/dropbear/dropbear.conf ]; then
    . /etc/dropbear/dropbear.conf
fi

wait_for_dropbear() {
    local pid exe timer="$DROPBEAR_SHUTDOWN_TIMEOUT"
    pid="$(cat "$PIDFILE" 2>/dev/null)" || return 1
    while [ $timer -gt 0 ] && exe="$(readlink -f "/proc/$pid/exe" 2>/dev/null)"; do
        if [ "$exe" = "$EXE" ]; then
            echo "$pid"
            return 0
        fi
        sleep 1
        timer=$(( timer - 1 ))
    done
    return 1
}

# A registered session still on its way into the real root gets up to 15 s.
handoff_alive() { [ -n "$1" ] && [ -e "$HANDOFF/$1" ] && [ -d "/proc/$1" ]; }
if [ -d "$HANDOFF" ]; then
    t=0
    for m in "$HANDOFF"/[0-9]*; do
        c="${m##*/}"; case "$c" in *.*|"") continue ;; esac
        while handoff_alive "$c" && [ ! -e "$HANDOFF/$c.ready" ] && [ $t -lt 150 ]; do
            sleep 0.1; t=$((t + 1))
        done
    done
fi

KEEP=0
if PID="$(wait_for_dropbear)"; then
    log_begin_msg "Stopping dropbear"
    sed -nr "s/^([0-9]+) \\(.*\\) \\S $PID \\1 .*/\\1/p" \
        /proc/[0-9]*/stat 2>/dev/null | \
        while read pgid; do
            if handoff_alive "$pgid" && [ -e "$HANDOFF/$pgid.ready" ]; then continue; fi
            kill -TERM -"$pgid"
        done
    sed -nr "s/^([0-9]+) \\(.*\\) \\S $PID [0-9]+ .*/\\1/p" \
        /proc/[0-9]*/stat 2>/dev/null | \
        while read pid; do
            if handoff_alive "$pid" && [ -e "$HANDOFF/$pid.ready" ]; then continue; fi
            kill -TERM "$pid"
        done
    kill -TERM "$PID"
    log_end_msg
fi
for m in "$HANDOFF"/[0-9]*.ready; do
    c="${m##*/}"; c="${c%.ready}"
    handoff_alive "$c" && KEEP=1
done
# Only a root that uses netplan (systemd-networkd underneath) takes over an
# address that is already on the interface. ifupdown and NetworkManager stop
# with "file exists" and never add the IPv6 address or the routes, so for those
# the interface is taken down as usual and the handed-over session simply
# waits a few seconds for the booted system to bring it back.
[ "$KEEP" = 1 ] && [ ! -e "${rootmnt}/usr/sbin/netplan" ] && KEEP=0

if [ "$BOOT" != nfs ] && [ "$IFDOWN" != none ] && [ "$KEEP" = 0 ]; then
    for IFACE in /sys/class/net/$IFDOWN; do
        [ -e "$IFACE" ] || continue
        IFACE="${IFACE#/sys/class/net/}"
        log_begin_msg "Bringing down $IFACE"
        ip link    set   dev "$IFACE" down
        ip address flush dev "$IFACE"
        ip route   flush dev "$IFACE"
        log_end_msg
    done
fi
