|
|
@@ -1,6 +1,7 @@
|
|
|
#!/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
|
|
|
@@ -32,6 +33,22 @@ if [[ "$YES" -eq 1 && "$PURGE" -ne 1 ]]; then
|
|
|
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%/*}"
|
|
|
@@ -97,12 +114,9 @@ remove_owned_path() {
|
|
|
esac
|
|
|
}
|
|
|
|
|
|
-identity_allowed() {
|
|
|
- case "$1:$2" in
|
|
|
- created_account:hth|created_account:hth-observe|created_account:hth-control|\
|
|
|
- created_group:hth|created_group:hth-observe|created_group:hth-control) return 0 ;;
|
|
|
- *) echo "uninstall: identity outside product allowlist: $1 $2" >&2; return 1 ;;
|
|
|
- esac
|
|
|
+manifest_identity_rejected() {
|
|
|
+ echo "uninstall: current manifest cannot claim service identity ownership: $1 $2" >&2
|
|
|
+ return 1
|
|
|
}
|
|
|
|
|
|
declare -a FILES=()
|
|
|
@@ -110,8 +124,7 @@ declare -a CONFIGS=()
|
|
|
declare -a TREES=()
|
|
|
declare -a OWNED_DIRS=()
|
|
|
declare -a PURGE_OWNED_DIRS=()
|
|
|
-declare -a CREATED_ACCOUNTS=()
|
|
|
-declare -a CREATED_GROUPS=()
|
|
|
+declare -A MANIFEST_ENTRIES=()
|
|
|
|
|
|
if [[ ! -f "$MANIFEST" ]]; then
|
|
|
footprint=0
|
|
|
@@ -121,7 +134,6 @@ if [[ ! -f "$MANIFEST" ]]; then
|
|
|
"$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf" \
|
|
|
"$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
|
|
|
"$ETCDIR/profiles/6400-control/profile.json" \
|
|
|
- "$ETCDIR/profiles/prod2u-door-temp/profile.json" \
|
|
|
"$RUNDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"; do
|
|
|
[[ -e "$path" || -L "$path" ]] && footprint=1
|
|
|
done
|
|
|
@@ -134,42 +146,119 @@ if [[ ! -f "$MANIFEST" ]]; then
|
|
|
exit 0
|
|
|
fi
|
|
|
|
|
|
-if [[ -f "$MANIFEST" ]]; then
|
|
|
- while read -r kind path extra; do
|
|
|
- [[ -z "$kind" || "$kind" == \#* ]] && continue
|
|
|
- [[ -n "$path" && -z "${extra:-}" ]] || { echo "uninstall: malformed manifest line: $kind $path ${extra:-}" >&2; exit 1; }
|
|
|
- if [[ "$kind:$path" == "config:$ETCDIR/profiles/prod2u-door-temp/profile.json" ||
|
|
|
- "$kind:$path" == "owned_dir:$ETCDIR/profiles/prod2u-door-temp" ]]; then
|
|
|
- # The formal v0.1.1 package never had a manifest, and current 6400 does
|
|
|
- # not create this legacy path. Historical claims cannot prove ownership.
|
|
|
- continue
|
|
|
- fi
|
|
|
- case "$kind" in
|
|
|
- file|config|symlink|managed_tree|owned_dir) safe_path "$kind" "$path" || exit 1 ;;
|
|
|
- created_account|created_group) identity_allowed "$kind" "$path" || exit 1 ;;
|
|
|
- account|group)
|
|
|
- # Legacy manifests did not record whether an identity predated HTH.
|
|
|
- # Keep it rather than guessing ownership.
|
|
|
- [[ "$path" == hth || "$path" == hth-observe || "$path" == hth-control ]] || exit 1
|
|
|
- continue
|
|
|
- ;;
|
|
|
- *) echo "uninstall: unknown manifest kind: $kind" >&2; exit 1 ;;
|
|
|
- esac
|
|
|
- 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
|
|
|
- ;;
|
|
|
- created_account) CREATED_ACCOUNTS+=("$path") ;;
|
|
|
- created_group) CREATED_GROUPS+=("$path") ;;
|
|
|
- esac
|
|
|
- done <"$MANIFEST"
|
|
|
-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
|
|
|
@@ -190,12 +279,6 @@ 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
|
|
|
- for account_name in "${CREATED_ACCOUNTS[@]}"; do
|
|
|
- if getent passwd "$account_name" >/dev/null 2>&1; then userdel "$account_name"; fi
|
|
|
- done
|
|
|
- for group_name in "${CREATED_GROUPS[@]}"; do
|
|
|
- if getent group "$group_name" >/dev/null 2>&1; then groupdel "$group_name"; fi
|
|
|
- done
|
|
|
fi
|
|
|
for path in "${OWNED_DIRS[@]}"; do remove_owned_path owned_dir "$path"; done
|
|
|
|
|
|
@@ -212,23 +295,10 @@ for proc_dir in /proc/[0-9]*; do
|
|
|
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" \
|
|
|
- "$ETCDIR/profiles/prod2u-door-temp/profile.json" "$RUNDIR" \
|
|
|
+ "$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
|
|
|
- for account_name in "${CREATED_ACCOUNTS[@]}"; do
|
|
|
- if getent passwd "$account_name" >/dev/null 2>&1; then
|
|
|
- echo "purge residual: created account $account_name" >&2
|
|
|
- failures=1
|
|
|
- fi
|
|
|
- done
|
|
|
- for group_name in "${CREATED_GROUPS[@]}"; do
|
|
|
- if getent group "$group_name" >/dev/null 2>&1; then
|
|
|
- echo "purge residual: created group $group_name" >&2
|
|
|
- failures=1
|
|
|
- fi
|
|
|
- done
|
|
|
fi
|
|
|
[[ "$failures" -eq 0 ]] || exit 1
|
|
|
echo "HTH_UNINSTALL_PASS purge=$PURGE residual_conflicts=0"
|