| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- #!/usr/bin/env bash
- set -euo pipefail
- ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- BINDIR=/usr/local/bin
- SHAREDIR=/usr/local/share/hth
- ETCDIR=/etc/hth
- UNITDIR=/etc/systemd/system
- LIBDIR=/usr/lib
- STATEDIR=/var/lib/hth
- RUNDIR=/run/hth
- SERVICE=hthd.service
- MANIFEST="$ETCDIR/install-manifest.txt"
- PURGE=0
- YES=0
- usage() { echo "Usage: sudo ./uninstall.sh [--purge --yes]" >&2; }
- while [[ $# -gt 0 ]]; do
- case "$1" in
- --purge) PURGE=1; shift ;;
- --yes) YES=1; shift ;;
- -h|--help) usage; exit 0 ;;
- *) echo "uninstall: unknown argument: $1" >&2; usage; exit 2 ;;
- esac
- done
- [[ "$(id -u)" -eq 0 ]] || { echo "uninstall: run as root" >&2; exit 2; }
- if [[ "$PURGE" -eq 1 && "$YES" -ne 1 ]]; then
- echo "uninstall: --purge requires --yes because product config and state will be removed" >&2
- exit 2
- fi
- if [[ "$YES" -eq 1 && "$PURGE" -ne 1 ]]; then
- echo "uninstall: --yes is only valid with --purge" >&2
- exit 2
- fi
- fail() { echo "uninstall: $*" >&2; exit 1; }
- json_string() {
- local key="$1" file="$2"
- sed -n "s/^[[:space:]]*\"${key}\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" "$file" | head -n 1
- }
- verify_current_immutable_file() {
- local installed="$1" packaged="$2" label="$3" installed_sha package_sha
- [[ -f "$packaged" && ! -L "$packaged" ]] ||
- fail "uninstall package is missing immutable file: $label"
- [[ -f "$installed" && ! -L "$installed" ]] ||
- fail "required immutable installed file is missing or not regular: $installed"
- installed_sha="$(sha256sum "$installed" | awk '{print $1}')"
- package_sha="$(sha256sum "$packaged" | awk '{print $1}')"
- [[ "$installed_sha" == "$package_sha" ]] ||
- fail "immutable installed file does not match uninstall package: $installed"
- }
- no_symlink_parents() {
- local path="$1" parent
- parent="${path%/*}"
- [[ -n "$parent" ]] || parent=/
- while [[ "$parent" != / ]]; do
- if [[ -L "$parent" ]]; then
- echo "uninstall: symlink parent rejected: $parent (target $path)" >&2
- return 1
- fi
- parent="${parent%/*}"
- [[ -n "$parent" ]] || parent=/
- done
- }
- safe_path() {
- local kind="$1" path="$2" resolved
- [[ "$path" == /* ]] || { echo "uninstall: non-absolute manifest path: $path" >&2; return 1; }
- resolved="$(realpath -m -s -- "$path")"
- [[ "$resolved" == "$path" ]] || { echo "uninstall: unresolved path rejected: $path -> $resolved" >&2; return 1; }
- no_symlink_parents "$resolved" || return 1
- case "$kind:$resolved" in
- file:/usr/local/bin/hthd|file:/usr/local/bin/hthctl|file:/usr/local/bin/hth-capture|\
- file:/etc/systemd/system/hthd.service|file:/etc/systemd/system/hthd@.service|\
- file:/usr/lib/sysusers.d/hth.conf|\
- file:/usr/lib/tmpfiles.d/hth.conf|\
- config:/etc/hth/hthd.yaml|config:/etc/hth/release-metadata.json|\
- config:/etc/hth/install-manifest.txt|config:/etc/hth/profiles/6400-control/profile.json|\
- config:/etc/hth/profiles/prod2u-door-temp/profile.json|\
- symlink:/etc/systemd/system/multi-user.target.wants/hthd.service|\
- managed_tree:/run/hth|managed_tree:/var/lib/hth/captures|\
- managed_tree:/var/lib/hth/replay|managed_tree:/var/lib/hth/receipts|\
- owned_dir:/usr/local/share/hth/sdk|owned_dir:/usr/local/share/hth|\
- owned_dir:/etc/hth/profiles/6400-control|\
- owned_dir:/etc/hth/profiles/prod2u-door-temp|owned_dir:/etc/hth/profiles|\
- owned_dir:/etc/hth|owned_dir:/var/lib/hth) return 0 ;;
- file:/usr/local/share/hth/sdk/hth_sdk-*.whl)
- [[ "${resolved#/usr/local/share/hth/sdk/}" != */* ]] || {
- echo "uninstall: nested SDK wheel path rejected: $resolved" >&2
- return 1
- }
- return 0
- ;;
- *) echo "uninstall: manifest path outside product allowlist: $kind $resolved" >&2; return 1 ;;
- esac
- }
- remove_owned_path() {
- local kind="$1" path="$2"
- safe_path "$kind" "$path" || return 1
- case "$kind" in
- file|config|symlink) rm -f -- "$path" ;;
- managed_tree)
- if [[ -e "$path" || -L "$path" ]]; then
- rm -rf --one-file-system -- "$path"
- fi
- ;;
- owned_dir)
- # Product-owned roots are never recursively removed. Unknown content keeps
- # the directory in place and cannot be mistaken for an HTH-owned tree.
- rmdir -- "$path" 2>/dev/null || true
- ;;
- *) echo "uninstall: unsupported removal kind: $kind" >&2; return 1 ;;
- esac
- }
- manifest_identity_rejected() {
- echo "uninstall: current manifest cannot claim service identity ownership: $1 $2" >&2
- return 1
- }
- declare -a FILES=()
- declare -a CONFIGS=()
- declare -a TREES=()
- declare -a OWNED_DIRS=()
- declare -a PURGE_OWNED_DIRS=()
- declare -A MANIFEST_ENTRIES=()
- if [[ ! -f "$MANIFEST" ]]; then
- footprint=0
- for path in "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
- "$UNITDIR/hthd.service" "$UNITDIR/hthd@.service" \
- "$UNITDIR/multi-user.target.wants/hthd.service" \
- "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf" \
- "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
- "$ETCDIR/profiles/6400-control/profile.json" \
- "$RUNDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"; do
- [[ -e "$path" || -L "$path" ]] && footprint=1
- done
- if [[ "$footprint" -eq 1 ]]; then
- echo "uninstall: HTH-like files exist without a trusted install manifest; refusing deletion" >&2
- echo "uninstall: use the new install.sh --upgrade path for verified v0.1.1 migration" >&2
- exit 1
- fi
- echo "HTH_UNINSTALL_PASS purge=$PURGE residual_conflicts=0 already_absent=1"
- exit 0
- fi
- [[ -f "$ROOT/RELEASE-METADATA.json" ]] || fail "package is missing RELEASE-METADATA.json"
- [[ -f "$ETCDIR/release-metadata.json" ]] ||
- fail "trusted install manifest requires installed release metadata"
- PACKAGE_VERSION="$(json_string version "$ROOT/RELEASE-METADATA.json")"
- PACKAGE_COMMIT="$(json_string source_commit "$ROOT/RELEASE-METADATA.json")"
- INSTALLED_VERSION="$(json_string version "$ETCDIR/release-metadata.json")"
- INSTALLED_COMMIT="$(json_string source_commit "$ETCDIR/release-metadata.json")"
- [[ "$PACKAGE_VERSION" =~ ^v[0-9][0-9A-Za-z.+-]*$ ]] ||
- fail "package release metadata has an invalid version"
- [[ "$PACKAGE_COMMIT" =~ ^[0-9a-f]{40}([0-9a-f]{24})?$ ]] ||
- fail "package release metadata has an invalid source_commit"
- [[ "$INSTALLED_VERSION" =~ ^v[0-9][0-9A-Za-z.+-]*$ ]] ||
- fail "installed release metadata has an invalid version"
- [[ "$INSTALLED_COMMIT" =~ ^[0-9a-f]{40}([0-9a-f]{24})?$ ]] ||
- fail "installed release metadata has an invalid source_commit"
- [[ "$INSTALLED_VERSION" == "$PACKAGE_VERSION" && "$INSTALLED_COMMIT" == "$PACKAGE_COMMIT" ]] ||
- fail "installed release metadata does not match this uninstall package"
- EXPECTED_HEADER="# HTH owned paths; generated by ${PACKAGE_VERSION} (${PACKAGE_COMMIT})"
- [[ "$(head -n 1 "$MANIFEST")" == "$EXPECTED_HEADER" ]] ||
- fail "install manifest header does not match installed release metadata"
- shopt -s nullglob
- PACKAGE_WHEELS=("$ROOT"/sdk/*.whl)
- shopt -u nullglob
- [[ "${#PACKAGE_WHEELS[@]}" -eq 1 ]] ||
- fail "uninstall package must contain exactly one Python SDK wheel"
- EXPECTED_WHEEL="$SHAREDIR/sdk/$(basename "${PACKAGE_WHEELS[0]}")"
- verify_current_immutable_file "$ETCDIR/release-metadata.json" \
- "$ROOT/RELEASE-METADATA.json" release-metadata
- verify_current_immutable_file "$BINDIR/hthd" "$ROOT/bin/hthd" hthd
- verify_current_immutable_file "$BINDIR/hthctl" "$ROOT/bin/hthctl" hthctl
- verify_current_immutable_file "$BINDIR/hth-capture" "$ROOT/bin/hth-capture" hth-capture
- verify_current_immutable_file "$UNITDIR/hthd.service" "$ROOT/service/hthd.service" hthd.service
- verify_current_immutable_file "$LIBDIR/sysusers.d/hth.conf" "$ROOT/service/hth.conf" hth.conf
- verify_current_immutable_file "$LIBDIR/tmpfiles.d/hth.conf" \
- "$ROOT/service/hthd-tmpfiles.conf" hthd-tmpfiles.conf
- verify_current_immutable_file "$ETCDIR/profiles/6400-control/profile.json" \
- "$ROOT/profiles/6400-control/profile.json" 6400-control-profile
- verify_current_immutable_file "$EXPECTED_WHEEL" "${PACKAGE_WHEELS[0]}" python-sdk-wheel
- MANIFEST_WHEEL_COUNT=0
- while read -r kind path extra; do
- [[ -z "$kind" || "$kind" == \#* ]] && continue
- [[ -n "$path" && -z "${extra:-}" ]] ||
- fail "malformed manifest line: $kind $path ${extra:-}"
- case "$kind" in
- file|config|symlink|managed_tree|owned_dir) safe_path "$kind" "$path" || exit 1 ;;
- created_account|created_group) manifest_identity_rejected "$kind" "$path" || exit 1 ;;
- *) fail "unknown current manifest kind: $kind" ;;
- esac
- manifest_key="$kind $path"
- [[ -z "${MANIFEST_ENTRIES[$manifest_key]:-}" ]] ||
- fail "duplicate current manifest entry: $manifest_key"
- MANIFEST_ENTRIES["$manifest_key"]=1
- case "$kind:$path" in
- file:$BINDIR/hthd|file:$BINDIR/hthctl|file:$BINDIR/hth-capture|\
- file:$UNITDIR/hthd.service|file:$LIBDIR/sysusers.d/hth.conf|\
- file:$LIBDIR/tmpfiles.d/hth.conf|file:$EXPECTED_WHEEL|\
- config:$ETCDIR/hthd.yaml|config:$ETCDIR/release-metadata.json|\
- config:$ETCDIR/install-manifest.txt|\
- config:$ETCDIR/profiles/6400-control/profile.json|\
- symlink:$UNITDIR/multi-user.target.wants/hthd.service|\
- managed_tree:$RUNDIR|managed_tree:$STATEDIR/captures|\
- managed_tree:$STATEDIR/replay|managed_tree:$STATEDIR/receipts|\
- owned_dir:$SHAREDIR/sdk|owned_dir:$SHAREDIR|\
- owned_dir:$ETCDIR/profiles/6400-control|owned_dir:$ETCDIR/profiles|\
- owned_dir:$ETCDIR|owned_dir:$STATEDIR) ;;
- *) fail "entry is not valid for a current HTH manifest: $manifest_key" ;;
- esac
- if [[ "$kind" == file && "$path" == "$SHAREDIR"/sdk/hth_sdk-*.whl ]]; then
- ((MANIFEST_WHEEL_COUNT += 1))
- fi
- case "$kind" in
- file|symlink) FILES+=("$path") ;;
- config) CONFIGS+=("$path") ;;
- managed_tree) TREES+=("$path") ;;
- owned_dir)
- case "$path" in
- "$SHAREDIR"|"$SHAREDIR/sdk") OWNED_DIRS+=("$path") ;;
- *) PURGE_OWNED_DIRS+=("$path") ;;
- esac
- ;;
- esac
- done <"$MANIFEST"
- while read -r required_kind required_path; do
- required_key="$required_kind $required_path"
- [[ "${MANIFEST_ENTRIES[$required_key]:-0}" -eq 1 ]] ||
- fail "current manifest is incomplete: $required_key"
- done <<EOF
- file $BINDIR/hthd
- file $BINDIR/hthctl
- file $BINDIR/hth-capture
- file $UNITDIR/hthd.service
- file $LIBDIR/sysusers.d/hth.conf
- file $LIBDIR/tmpfiles.d/hth.conf
- file $EXPECTED_WHEEL
- config $ETCDIR/hthd.yaml
- config $ETCDIR/release-metadata.json
- config $ETCDIR/install-manifest.txt
- config $ETCDIR/profiles/6400-control/profile.json
- managed_tree $RUNDIR
- managed_tree $STATEDIR/captures
- managed_tree $STATEDIR/replay
- managed_tree $STATEDIR/receipts
- owned_dir $SHAREDIR/sdk
- owned_dir $SHAREDIR
- owned_dir $ETCDIR/profiles/6400-control
- owned_dir $ETCDIR/profiles
- owned_dir $ETCDIR
- owned_dir $STATEDIR
- EOF
- [[ "$MANIFEST_WHEEL_COUNT" -eq 1 ]] ||
- fail "current manifest must contain exactly one owned SDK wheel"
- systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
- systemctl disable --now 'hthd@*.service' >/dev/null 2>&1 || true
- if systemctl is-active --quiet "$SERVICE"; then
- echo "uninstall: $SERVICE is still active" >&2
- exit 1
- fi
- for path in "${FILES[@]}"; do
- case "$path" in
- "$UNITDIR"/multi-user.target.wants/*) remove_owned_path symlink "$path" ;;
- *) remove_owned_path file "$path" ;;
- esac
- done
- systemctl daemon-reload >/dev/null 2>&1 || true
- if [[ "$PURGE" -eq 1 ]]; then
- for path in "${CONFIGS[@]}"; do remove_owned_path config "$path"; done
- for path in "${TREES[@]}"; do remove_owned_path managed_tree "$path"; done
- for path in "${PURGE_OWNED_DIRS[@]}"; do remove_owned_path owned_dir "$path"; done
- fi
- for path in "${OWNED_DIRS[@]}"; do remove_owned_path owned_dir "$path"; done
- failures=0
- for path in "${FILES[@]}"; do
- if [[ -e "$path" || -L "$path" ]]; then echo "uninstall residual: $path" >&2; failures=1; fi
- done
- for proc_dir in /proc/[0-9]*; do
- [[ -r "$proc_dir/comm" ]] || continue
- if [[ "$(<"$proc_dir/comm")" == hthd ]]; then
- echo "uninstall residual: hthd process ${proc_dir##*/}" >&2
- failures=1
- fi
- done
- if [[ "$PURGE" -eq 1 ]]; then
- for path in "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" "$ETCDIR/install-manifest.txt" \
- "$ETCDIR/profiles/6400-control/profile.json" "$RUNDIR" \
- "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"; do
- if [[ -e "$path" || -L "$path" ]]; then echo "purge residual: $path" >&2; failures=1; fi
- done
- fi
- [[ "$failures" -eq 0 ]] || exit 1
- echo "HTH_UNINSTALL_PASS purge=$PURGE residual_conflicts=0"
|