Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

8.5 KB
Raw Blame
#!/bin/sh
# One request on http://10.42.0.1 while the disk is locked (`run`), on stdin and stdout (tcpsvd).
#
#   GET  /setup             {protocol, hotspot, locked: true, wait}: the phone asks for the password
#   GET  /setup/networks    the Wi-Fi around ({networks: [...]}, `networks`), from the startup's scan,
#                           or with ?rescan=1 scanned now on the hotspot's card where it allows that
#   POST /setup/check       the computer's password, as the body, only checked against the disk
#                           (cryptsetup --test-passphrase): 200 {state: "right"}, 401 WRONG_PASSWORD.
#                           Nothing opens; the phone asks it first, then the Wi-Fi, then unlocks. As
#                           many tries as the person likes (protocol 3).
#   POST /setup/unlock      the computer's password, as the body: it answers the disk's question
#                           (systemd's ask-password, the one the screen answers), then says whether the
#                           disk opened: 200, 401 WRONG_PASSWORD, 429 WAIT after many wrong ones, 409 BUSY
#
# Protocol 2: the unlock may carry the rest of the setup too, in one stay on the hotspot — the Wi-Fi
# chosen, its password, the sign-in ticket and the phone's account, as setup.py's /setup/connect
# takes them. The body is then the password followed by that JSON, its length in the header
# X-Harness-Setup-Length. Kept, once the disk opened, in SETUP (/run, which outlives the switch to the
# system proper; root's alone), where setup.py joins and signs in with it: no second hotspot.
#
# The password goes only to systemd-reply-password, on its stdin; it is never written or logged.
set -u
STATE=${HARNESS_UNLOCK_STATE:-/run/harness-unlock}
ASK=${HARNESS_UNLOCK_ASK:-/run/systemd/ask-password}
REPLY=${HARNESS_UNLOCK_REPLY:-/usr/lib/systemd/systemd-reply-password}
MAPPER=${HARNESS_UNLOCK_MAPPER:-/dev/mapper}
# How long a password may take to open the disk (argon2 takes a few seconds on purpose).
OPENS=${HARNESS_UNLOCK_OPENS:-40}
BY_PHONE=${HARNESS_UNLOCK_BY_PHONE:-/run/harness-os/unlocked-by-phone}
SETUP=${HARNESS_UNLOCK_SETUP:-/run/harness-os/phone-setup.json}
CRYPTSETUP=${HARNESS_UNLOCK_CRYPTSETUP:-/usr/bin/cryptsetup}
HERE=$(dirname "$0")
# Wrong ones before the page makes the phone wait, and the first wait; it doubles after each.
FREE_TRIES=5
FIRST_WAIT=30
CR=$(printf '\r')

reply() {
    printf 'HTTP/1.1 %s\r\nContent-Type: application/json\r\nContent-Length: %s\r\nCache-Control: no-store\r\nConnection: close\r\n\r\n%s' \
        "$1" "${#2}" "$2"
    exit 0
}

now() { cut -d. -f1 /proc/uptime 2>/dev/null || date +%s; }

# Wrong passwords so far, and when the phone may try again: "count until".
tries() { cat "$STATE/tries" 2>/dev/null || echo '0 0'; }

wait_left() {
    set -- $(tries)
    left=$(( $2 - $(now) ))
    [ "$left" -gt 0 ] && echo "$left" || echo 0
}

# The disk's question: the ask-password file systemd-cryptsetup wrote, and its socket.
question() {
    for file in "$ASK"/ask.*; do
        [ -f "$file" ] || continue
        grep -q '^Id=cryptsetup:' "$file" && { echo "$file"; return 0; }
    done
    return 1
}

opened() {
    for device in "$MAPPER"/*; do
        [ "${device##*/}" = control ] && continue
        [ -e "$device" ] && return 0
    done
    return 1
}

read -r method target _ || exit 0
query=
case $target in *\?*) query=${target#*\?} ;; esac
target=${target%%\?*}
length=0
extra=0
while read -r line; do
    line=${line%"$CR"}
    [ -z "$line" ] && break
    case $line in
        [Cc]ontent-[Ll]ength:*) length=${line#*:}; length=${length# } ;;
        [Xx]-[Hh]arness-[Ss]etup-[Ll]ength:*) extra=${line#*:}; extra=${extra# } ;;
    esac
done
case $length in ''|*[!0-9]*) length=0 ;; esac
case $extra in ''|*[!0-9]*) extra=0 ;; esac
name=$(cat "$STATE/name" 2>/dev/null || echo Harness-Setup)

case "$method $target" in
    'GET /setup')
        reply '200 OK' "{\"protocol\":3,\"hotspot\":\"$name\",\"locked\":true,\"wait\":$(wait_left)}"
        ;;
    'GET /setup/networks')
        # Asked again (the phone's Rescan): scanned now, beside the hotspot, where the card allows it.
        if [ "$query" = rescan=1 ] && [ -f "$STATE/device" ]; then
            fresh=$("$HERE/networks" "$(cat "$STATE/device")" "$name" ap-force) && [ -n "$fresh" ] &&
                printf '%s\n' "$fresh" > "$STATE/networks.json"
        fi
        reply '200 OK' "$(cat "$STATE/networks.json" 2>/dev/null || echo '{"networks":[]}')"
        ;;
    'POST /setup/check')
        [ "$length" -ge 1 ] && [ "$length" -le 512 ] || reply '400 Bad Request' '{"error":"BAD_REQUEST","message":"Enter the computer'"'"'s password."}'
        password=$(dd bs=1 count="$length" 2>/dev/null)
        opened && reply '200 OK' '{"state":"right"}'
        asked=$(question) || reply '503 Service Unavailable' '{"error":"NOT_ASKING","message":"The computer is not asking for its password yet. Try again in a moment."}'
        # The disk the question is about: "Id=cryptsetup:/dev/nvme0n1p3".
        disk=$(sed -n 's/^Id=cryptsetup://p' "$asked")
        [ -n "$disk" ] || reply '503 Service Unavailable' '{"error":"NOT_ASKING","message":"The computer is not asking for its password yet. Try again in a moment."}'
        mkdir "$STATE/busy" 2>/dev/null || reply '409 Conflict' '{"error":"BUSY","message":"Already trying a password."}'
        trap 'rmdir "$STATE/busy" 2>/dev/null' EXIT
        # Checked only: the key slot is tried, nothing is opened. The password on stdin, as it is.
        if printf '%s' "$password" | "$CRYPTSETUP" open --test-passphrase --key-file=- "$disk" >/dev/null 2>&1; then
            password=
            reply '200 OK' '{"state":"right"}'
        fi
        password=
        reply '401 Unauthorized' '{"error":"WRONG_PASSWORD","message":"That is not this computer'"'"'s password. It is the one set when it was installed."}'
        ;;
    'POST /setup/unlock')
        ;;
    *)
        reply '404 Not Found' '{"error":"NOT_FOUND"}'
        ;;
esac

[ "$extra" -le 8192 ] && [ "$extra" -lt "$length" ] || extra=0
secret=$(( length - extra ))
[ "$secret" -ge 1 ] && [ "$secret" -le 512 ] || reply '400 Bad Request' '{"error":"BAD_REQUEST","message":"Enter the computer'"'"'s password."}'
# A byte at a time: head reads ahead on a pipe, and would take the setup that follows with it.
password=$(dd bs=1 count="$secret" 2>/dev/null)
setup=
[ "$extra" -gt 0 ] && setup=$(head -c "$extra")
left=$(wait_left)
[ "$left" -gt 0 ] && reply '429 Too Many Requests' "{\"error\":\"WAIT\",\"wait\":$left,\"message\":\"Too many wrong passwords. Try again in $left seconds.\"}"
mkdir "$STATE/busy" 2>/dev/null || reply '409 Conflict' '{"error":"BUSY","message":"Already trying a password."}'
trap 'rmdir "$STATE/busy" 2>/dev/null' EXIT

opened && reply '200 OK' '{"state":"unlocked"}'
asked=$(question) || reply '503 Service Unavailable' '{"error":"NOT_ASKING","message":"The computer is not asking for its password yet. Try again in a moment."}'
socket=$(sed -n 's/^Socket=//p' "$asked")
printf '%s\n' "$password" | "$REPLY" 1 "$socket" 2>/dev/null
password=

# Opened, or asked again (a wrong one: systemd-cryptsetup asks anew, in a new file), within OPENS
# seconds.
result=
for _ in $(seq "$OPENS"); do
    opened && { result=opened; break; }
    current=$(question) || current=
    [ -n "$current" ] && [ "$current" != "$asked" ] && { result=wrong; break; }
    sleep 1
done
if [ "$result" = opened ]; then
    echo '0 0' > "$STATE/tries"
    # Unlocked from a phone, not at the screen: /run outlives the switch to the system proper, where
    # the screen's first Wi-Fi page reads this and leaves the radio to setup.py's hotspot, for the
    # phone to choose the Wi-Fi (network.py UNLOCKED_BY_PHONE). Nobody at the screen typed anything.
    mkdir -p "$(dirname "$BY_PHONE")" && : > "$BY_PHONE"
    # The rest of the setup, for setup.py as the system starts: root's alone, and only in /run.
    if [ -n "$setup" ]; then
        (umask 077 && printf '%s' "$setup" > "$SETUP")
        setup=
    fi
    reply '200 OK' '{"state":"unlocked"}'
fi
[ -z "$result" ] && reply '504 Gateway Timeout' '{"error":"TIMEOUT","message":"The computer did not say whether that opened it. Try again."}'
set -- $(tries)
count=$(( $1 + 1 ))
until=0
if [ "$count" -ge "$FREE_TRIES" ]; then
    wait=$FIRST_WAIT
    extra=$(( count - FREE_TRIES ))
    while [ "$extra" -gt 0 ] && [ "$wait" -lt 600 ]; do wait=$(( wait * 2 )); extra=$(( extra - 1 )); done
    until=$(( $(now) + wait ))
fi
echo "$count $until" > "$STATE/tries"
reply '401 Unauthorized' '{"error":"WRONG_PASSWORD","message":"That is not this computer'"'"'s password. It is the one set when it was installed."}'