#!/bin/sh
# Carry an SSH session from the initramfs into the booted system instead of
# ending it. Runs in the initramfs, from the session's forced command, once the
# disk is unlocked (or freshly encrypted). It registers the session's
# connection process (the dropbear child serving it) so the initramfs spares
# it, and the moment the real root is mounted (/root under initramfs-tools,
# /sysroot under dracut) it chroots into it, so the session keeps working after
# the initramfs is deleted. The rest (/scripts/servury-boot-follow) is read
# here and run by the real root's /bin/sh, so nothing has to be installed in
# the customer's root.
#
# initramfs-tools: init-bottom/dropbear (ours) spares registered sessions.
# dracut (systemd as PID 1): systemd ends every process at the switch to the
# real root except those whose name starts with "@", so dropbear runs as
# "@dropbear" and the handed-over session re-execs as "@servury-session";
# hook pre-pivot servury-handoff-wait holds the switch until it has.
#
# A session registers BEFORE it lets the boot go on (unlock.sh before the
# passphrase, fde-encrypt.sh before the job can release the boot): otherwise a
# fast boot reaches init-bottom first and the session is ended like any other.
#   servury-handoff register     register only, no output
#   servury-handoff unregister   the unlock failed; nothing to hand over
#   servury-handoff              register if needed, then hand over
H=/run/servury-handoff
L="$(cat /run/dropbear.pid 2>/dev/null)"
p=$$; conn=""
while [ "$p" -gt 1 ]; do
    read -r _ _ _ pp _ < "/proc/$p/stat" || break
    if [ -n "$L" ] && [ "$pp" = "$L" ]; then conn=$p; break; fi
    # No pidfile: the connection process is the dropbear whose parent is the
    # dropbear listener.
    if [ -z "$L" ] && [ "$(cat "/proc/$p/comm" 2>/dev/null)" = dropbear ] \
        && [ "$(cat "/proc/$pp/comm" 2>/dev/null)" = dropbear ]; then conn=$p; break; fi
    p=$pp
done
case "${1:-}" in
    unregister) [ -n "$conn" ] && rm -f "$H/$conn"; exit 0 ;;
esac
if [ -z "$conn" ]; then
    [ "${1:-}" = register ] && exit 0
    echo "  Booting. Connect again in a moment."
    exit 0
fi
mkdir -p "$H" && chmod 700 "$H"
# The keys this initramfs accepts, for boot-follow's check against the booted
# system's own sshd policy (init-bottom/dropbear deletes the original).
[ -e "$H/keys" ] || cp ~root/.ssh/authorized_keys "$H/keys" 2>/dev/null || : > "$H/keys"
: > "$H/$conn"
[ "${1:-}" = register ] && exit 0
echo "  Booting. This session continues into your server; no need to reconnect."
echo ""
i=0; R=""
while [ -z "$R" ]; do
    for r in /root /sysroot; do [ -e "$r/etc/os-release" ] && R=$r && break; done
    [ -n "$R" ] && break
    sleep 0.1; i=$((i + 1))
    if [ $i -gt 1800 ]; then
        rm -f "$H/$conn"
        echo "  The root filesystem did not come up in 3 minutes. Connect again once the server is up."
        exit 0
    fi
done
FOLLOW="$(cat /scripts/servury-boot-follow)"
if [ ! -x "$R/bin/sh" ] || [ -z "$FOLLOW" ]; then
    rm -f "$H/$conn"
    echo "  Connect again in a moment, once the server has booted."
    exit 0
fi
if [ "$(cat /proc/1/comm 2>/dev/null)" = systemd ]; then
    # systemd initramfs: rename to "@servury-session" inside the real root
    # (its bash; exec -a), or the switch to the real root ends this session.
    if [ -x "$R/bin/bash" ]; then
        : > "$H/$conn.ready"
        export FOLLOW
        exec chroot "$R" /bin/bash -c 'exec -a @servury-session /bin/sh -c "$FOLLOW" boot-follow'
    fi
    rm -f "$H/$conn"
    echo "  Connect again in a moment, once the server has booted."
    exit 0
fi
: > "$H/$conn.ready"
exec chroot "$R" /bin/sh -c "$FOLLOW" boot-follow
