#!/usr/bin/env bash # Install/update-time preparation, BEFORE the hardened backend is started. # Always run as the service account, never as root or an ExecStartPre helper. # No ownership/mode repair, relocation, recursive creation, or symlink following. set -euo pipefail fail() { printf 'Terminal workspace preparation refused: %s\n' "$1" >&2; exit 1; } [[ $# == 1 ]] || fail 'expected the service account home' home=$1 [[ $(id -u) != 0 ]] || fail 'run as the service account, not root' [[ $home == /* && -d $home && ! -L $home && -O $home ]] || fail 'home must be an existing account-owned directory' [[ $(cd -- "$home" && pwd -P) == "$home" ]] || fail 'home must not traverse symlinks' # Existing home-local metadata remains authoritative; never migrate or chown it. # Reject aliased paths before making the narrowly named paths writable in a unit. legacy=$home for component in .local state archipelago sessions; do legacy=$legacy/$component [[ ! -L $legacy ]] || fail 'legacy session path must not traverse symlinks' if [[ -e $legacy ]]; then [[ -d $legacy && -O $legacy ]] || fail 'legacy session path must be account-owned directories' # Check search access before descending: inaccessible descendants must # never be mistaken for an absent legacy suffix. [[ -x $legacy ]] || fail 'legacy session path is not searchable by the service account' fi done if [[ -d $legacy ]]; then [[ -w $legacy && -x $legacy ]] || fail 'legacy sessions are not accessible to the service account' fi work=$home/Work if [[ -e $work || -L $work ]]; then [[ -d $work && ! -L $work && -O $work && -w $work && -x $work ]] || fail 'existing Work must be an accessible account-owned directory (no symlink)' else # A single mkdir fails closed on a concurrent creator. Never follow an # existing symlink or use install/chown/chmod to repair someone else's data. (umask 077; mkdir -m 700 -- "$work") fi printf 'Terminal workspace ready (existing files and permissions preserved).\n'