#!/bin/sh
# Current identity is required before starting any services from an old snapshot.
# --copy is also used by the fixture test; it never mounts a host filesystem.
set -eu
copy_identity() {
    current=$1 target=$2 list=$3
    for required in etc/passwd etc/shadow etc/group etc/gshadow etc/sudoers; do
        [ -f "$current/$required" ] && [ ! -L "$current/$required" ] || return 1
    done
    # Never traverse an old snapshot's symlink while copying privileged files.
    for parent in etc var var/lib var/lib/starchaser usr usr/share usr/share/polkit-1; do
        [ ! -L "$target/$parent" ] || return 1
    done
    # This list contains identity files only. Authorization and automatic sign-in
    # also stay current for a snapshot session; bulk go-back exclusions stay out.
    # Read before the pipeline: a failed producer is hidden by a successful reader
    # in POSIX sh. An empty or incomplete list must also stop before any copy.
    entries=$(cat "$list") || return 1
    for required in /etc/passwd /etc/shadow /etc/group /etc/gshadow /var/lib/starchaser/recovery-codes; do
        case "
$entries
" in *"
$required
"*) ;; *) return 1 ;; esac
    done
    {
    printf '%s\n' "$entries" /etc/sudoers /etc/sudoers.d /etc/polkit-1 /usr/share/polkit-1/rules.d /etc/pam.d /etc/security /etc/greetd /etc/ssh /var/lib/starchaser/autologin.json
    if [ "${4:-}" != data ]; then printf '%s\n' /usr/local/lib/starchaser /usr/share/polkit-1/actions; fi
    } |
    while IFS= read -r entry; do
        case "$entry" in /*) ;; *) continue ;; esac
        case "$entry" in *..*) exit 1 ;; esac
        # Expand the filter's SSH host-key wildcard on both trees, so keys absent
        # today are removed too. Other entries include directories as a whole.
        # shellcheck disable=SC2086 # root-owned filter globs, including SSH host keys
        for file in "$current"$entry "$target"$entry; do
            case "$file" in "$current"/*) rel=${file#"$current"} ;; *) rel=${file#"$target"} ;; esac
            case "$rel" in *'*'*) continue ;; esac
            parent=$(dirname "$rel")
            while [ "$parent" != / ] && [ "$parent" != . ]; do
                [ ! -L "$target$parent" ] || exit 1
                parent=$(dirname "$parent")
            done
            rm -rf -- "$target$rel"
            if [ -e "$current$rel" ] || [ -L "$current$rel" ]; then
                mkdir -p -- "$(dirname "$target$rel")"
                cp -a -- "$current$rel" "$target$rel"
            fi
        done
    done
}
if [ "${1:-}" = --copy ] || [ "${1:-}" = --copy-data ]; then
    mode=all
    [ "$1" != --copy-data ] || mode=data
    copy_identity "$2" "$3" "$4" "$mode"
    exit
fi
target=$1
current=/run/starchaser-snapshot-current
mkdir -p "$current"
# The snapshot's Btrfs mount remains underneath the overlay, including with
# systemd-volatile-root. Strip findmnt's bracketed subvolume suffix.
device=$(findmnt -nr -t btrfs -o SOURCE | awk 'NR == 1 { sub(/\[.*/, ""); print; exit }')
[ -n "$device" ]
mount -t btrfs -o ro,subvol=@ "$device" "$current"
trap 'umount "$current"' EXIT
copy_identity "$current" "$target" /etc/starchaser-snapshot-identity-list
