LibrePortal/scripts/peer/peer_shell.sh
librelad 3fe2c0660a feat(peers): direct peer SSH — pairing + peer-shell + pull (Phase 3)
End-to-end direct-ssh-direct: two LibrePortal instances exchange pairing
tokens, each authorizes the other to call a locked-down peer-shell dispatcher
via SSH forced-command, then either side can pull live app data from the
other without needing a shared backup repo.

Push and Connect-via-relay are deferred — push is symmetric to pull (same
forced-command, opposite verb), and the relay variant waits for Connect to
actually exist (config_json + kind enum already future-proofed in Phase 2).

Key generation (peer_key.sh):
  One ed25519 keypair per install at ~<manager>/.ssh/libreportal-peer{,.pub}.
  Generated lazily on the first peer-related call. Used as our outbound
  SSH identity AND as the pubkey other instances authorize.

Forced-command dispatcher (peer_shell.sh):
  Standalone script, deployed by peerInstallShell() to
  ~<manager>/.local/bin/peer-shell. authorized_keys entries look like:
    command="~/.local/bin/peer-shell <peer-name>",no-pty,no-port-forwarding,
    no-X11-forwarding,no-agent-forwarding,no-user-rc ssh-ed25519 AAAA… peer:<name>
  sshd hands us $SSH_ORIGINAL_COMMAND; we parse, whitelist the verb, and
  refuse anything else. Verbs:
    ping        Liveness probe (JSON ok:true).
    list-apps   JSON {peer, apps:[{slug, size_kb}]}.
    stream-app  tar of containers_dir/<slug> to stdout (slug strictly
                validated — lowercase alnum+dash; rejects path traversal).
  Audit log appended to ~/.local/state/libreportal/peer-shell.log. Excluded
  from the generated source arrays (would crash any sourcing shell on empty
  SSH_ORIGINAL_COMMAND); generate_arrays.sh skip-list extended.

Pairing token (peer_pairing.sh):
  Format: lp-peer|v1|<name>|<user>|<host>|<port>|<base64-pubkey>|<fingerprint>
  Pipe-delimited because the SHA256 fingerprint and base64 pubkey both
  contain ':'. peerPairingParse decodes + re-derives the fingerprint from
  the actual key, refusing tokens with mismatched fingerprints (catches
  truncation / tampering). peerPairingAccept:
    1. Installs peer-shell (peerInstallShell).
    2. Appends to authorized_keys with the lockdown options above.
    3. Inserts a peers row (kind=direct-ssh-direct, config carries host,
       port, user, fingerprint).
  Symmetric — user runs accept on BOTH sides with the other's token to
  enable bidirectional calls.

Outbound SSH (peer_remote.sh):
  peerExec <name> <verb> [args] — looks up the peer's connection config and
  ssh's in with the right key, BatchMode + ConnectTimeout + accept-new for
  the host key. peerPing wraps it and updates peers.status + last_seen.

Pull-an-app (peer_pull.sh):
  peerPullApp <peer> <app> [--no-pre-backup] [--keep-urls]
    1. peerPing (refuse if unreachable).
    2. migratePreBackupDestination (reuses the Phase 0 safety wrapper —
       same restic-tagged pre-migrate snapshot as the backup-channel flow).
    3. Stop + wipe destination's app folder.
    4. peerExec stream-app | tar -x (pipefail; bails on partial transfers).
    5. migrateApplyUrlRewrite + dockerComposeUpdateAndStartApp install
       (URL repointing, idempotent install path).
    6. dockerComposeUp + post-restore hooks.
  Identical Stage-2..6 to migrateApplyApp; only the data source differs
  (tar-over-SSH instead of restic-restore).

CLI (cli_peer_commands.sh + header):
  libreportal peer token                — emit this host's pairing token
  libreportal peer pair <token> [name]  — accept a token (override name)
  libreportal peer apps <peer>          — live peer-shell list-apps
  libreportal peer pull <peer> <app> [--no-pre-backup] [--keep-urls]

WebUI (/peers):
  Header gains 'Show my token' and 'Pair with token' buttons (both open
  modals around the matching CLI verbs). Token modal warns the user that
  the token is credentials. Pair modal accepts a free-form override name.
  Direct-SSH peer cards gain a 'List apps' button that opens an inline
  drawer showing the peer's live app inventory (via peer apps) with per-
  app 'Pull' buttons. Pull modal has the same two safety toggles as the
  Migrate tab (pre-backup ON, URL rewrite ON by default).
  Backup-channel manual-add modal kept; direct-SSH must use the token flow.

Smoke-tested:
  - All 16 peer-subsystem functions register without crashing the shell.
  - peer-shell ping ⇒ {ok:true}; unknown-verb refused; path-traversal slug
    refused; valid-slug streams.
  - Token emit→parse round-trip preserves every field; garbage rejected
    with not-a-token; v99 rejected with unsupported-version.
Signed-off-by: librelad <librelad@digitalangels.vip>
2026-05-26 17:56:57 +01:00

107 lines
3.5 KiB
Bash

#!/bin/bash
# peer-shell — the forced-command dispatcher invoked by an incoming SSH peer.
#
# Deployed by peerInstallShell() to ~<manager>/.local/bin/peer-shell, then
# every incoming peer key in authorized_keys has:
#
# command="~/.local/bin/peer-shell <peer-name>",no-pty,no-port-forwarding,...
#
# sshd sets $SSH_ORIGINAL_COMMAND to whatever the caller asked for. We parse
# it, whitelist the verb, and refuse anything else. The peer name passed as
# $1 (by the forced-command itself) lets us scope and audit calls per peer.
#
# Trust boundary: this script runs as the manager user, the same identity the
# rest of LibrePortal runs as on a rootless install. It deliberately has no
# write paths into root-owned territory — everything it can touch is what
# the manager could already touch through the WebUI.
set -u
LP_PEER_NAME="${1:-unknown}"
# Audit log — appended; rotated by logrotate if present, otherwise harmless.
LP_PEER_LOG="${HOME}/.local/state/libreportal/peer-shell.log"
mkdir -p "$(dirname "$LP_PEER_LOG")" 2>/dev/null || true
_log() {
printf '%s peer=%s verb=%s detail=%s\n' \
"$(date -Iseconds)" "$LP_PEER_NAME" "$1" "${2:-}" >> "$LP_PEER_LOG" 2>/dev/null || true
}
_die() {
_log error "$1"
printf '{"error":"%s"}\n' "$1" >&2
exit 1
}
# Bootstrap shared LibrePortal config (containers_dir, manager helpers).
# Source the standard env if we can find it; otherwise infer paths.
if [[ -r "${HOME}/.libreportal-env" ]]; then
# shellcheck disable=SC1090
source "${HOME}/.libreportal-env" >/dev/null 2>&1 || true
fi
: "${containers_dir:=/libreportal-containers/}"
CMD="${SSH_ORIGINAL_COMMAND:-}"
if [[ -z "$CMD" ]]; then
_die "no-command"
fi
# Parse: first token = verb, rest = args. No shell expansion of CMD's content.
read -r VERB ARGS <<< "$CMD"
# App-slug validator. Slugs are lowercase alnum + dash; anything else means
# someone's trying path traversal or shell injection through the args.
_valid_slug() {
[[ "$1" =~ ^[a-z0-9][a-z0-9-]{0,62}$ ]]
}
verb_ping() {
_log ping ""
printf '{"ok":true,"peer":"%s","time":"%s"}\n' \
"$LP_PEER_NAME" "$(date -Iseconds)"
}
verb_list_apps() {
_log list-apps ""
local first=1
printf '{"peer":"%s","apps":[' "$LP_PEER_NAME"
local d slug size_kb
for d in "$containers_dir"*/; do
[[ -d "$d" ]] || continue
slug=$(basename "$d")
[[ -f "${d}docker-compose.yml" || -f "${d}compose.yml" ]] || continue
_valid_slug "$slug" || continue
size_kb=$(du -sk "$d" 2>/dev/null | awk '{print $1}')
[[ -z "$size_kb" ]] && size_kb=0
(( first )) || printf ','
first=0
printf '{"slug":"%s","size_kb":%s}' "$slug" "$size_kb"
done
printf ']}\n'
}
verb_stream_app() {
local slug
read -r slug <<< "$ARGS"
if [[ -z "$slug" ]] || ! _valid_slug "$slug"; then
_die "invalid-slug"
fi
if [[ ! -d "${containers_dir}${slug}" ]]; then
_die "no-such-app"
fi
_log stream-app "$slug"
# Stream a tar of the app dir to stdout. --warning=no-file-changed because
# live data dirs change during read; we accept eventual consistency. The
# caller is the receiver's peer_pull.sh, which untars into a staging dir
# and then runs the migrate-flow.
tar --warning=no-file-changed --warning=no-file-removed \
-C "$containers_dir" -cf - "$slug"
}
case "$VERB" in
ping) verb_ping ;;
list-apps) verb_list_apps ;;
stream-app) verb_stream_app ;;
*) _die "unknown-verb" ;;
esac