uninstall.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  4. BINDIR=/usr/local/bin
  5. SHAREDIR=/usr/local/share/hth
  6. ETCDIR=/etc/hth
  7. UNITDIR=/etc/systemd/system
  8. LIBDIR=/usr/lib
  9. STATEDIR=/var/lib/hth
  10. RUNDIR=/run/hth
  11. SERVICE=hthd.service
  12. MANIFEST="$ETCDIR/install-manifest.txt"
  13. PURGE=0
  14. YES=0
  15. usage() { echo "Usage: sudo ./uninstall.sh [--purge --yes]" >&2; }
  16. while [[ $# -gt 0 ]]; do
  17. case "$1" in
  18. --purge) PURGE=1; shift ;;
  19. --yes) YES=1; shift ;;
  20. -h|--help) usage; exit 0 ;;
  21. *) echo "uninstall: unknown argument: $1" >&2; usage; exit 2 ;;
  22. esac
  23. done
  24. [[ "$(id -u)" -eq 0 ]] || { echo "uninstall: run as root" >&2; exit 2; }
  25. if [[ "$PURGE" -eq 1 && "$YES" -ne 1 ]]; then
  26. echo "uninstall: --purge requires --yes because product config and state will be removed" >&2
  27. exit 2
  28. fi
  29. if [[ "$YES" -eq 1 && "$PURGE" -ne 1 ]]; then
  30. echo "uninstall: --yes is only valid with --purge" >&2
  31. exit 2
  32. fi
  33. fail() { echo "uninstall: $*" >&2; exit 1; }
  34. json_string() {
  35. local key="$1" file="$2"
  36. sed -n "s/^[[:space:]]*\"${key}\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" "$file" | head -n 1
  37. }
  38. verify_current_immutable_file() {
  39. local installed="$1" packaged="$2" label="$3" installed_sha package_sha
  40. [[ -f "$packaged" && ! -L "$packaged" ]] ||
  41. fail "uninstall package is missing immutable file: $label"
  42. [[ -f "$installed" && ! -L "$installed" ]] ||
  43. fail "required immutable installed file is missing or not regular: $installed"
  44. installed_sha="$(sha256sum "$installed" | awk '{print $1}')"
  45. package_sha="$(sha256sum "$packaged" | awk '{print $1}')"
  46. [[ "$installed_sha" == "$package_sha" ]] ||
  47. fail "immutable installed file does not match uninstall package: $installed"
  48. }
  49. no_symlink_parents() {
  50. local path="$1" parent
  51. parent="${path%/*}"
  52. [[ -n "$parent" ]] || parent=/
  53. while [[ "$parent" != / ]]; do
  54. if [[ -L "$parent" ]]; then
  55. echo "uninstall: symlink parent rejected: $parent (target $path)" >&2
  56. return 1
  57. fi
  58. parent="${parent%/*}"
  59. [[ -n "$parent" ]] || parent=/
  60. done
  61. }
  62. safe_path() {
  63. local kind="$1" path="$2" resolved
  64. [[ "$path" == /* ]] || { echo "uninstall: non-absolute manifest path: $path" >&2; return 1; }
  65. resolved="$(realpath -m -s -- "$path")"
  66. [[ "$resolved" == "$path" ]] || { echo "uninstall: unresolved path rejected: $path -> $resolved" >&2; return 1; }
  67. no_symlink_parents "$resolved" || return 1
  68. case "$kind:$resolved" in
  69. file:/usr/local/bin/hthd|file:/usr/local/bin/hthctl|file:/usr/local/bin/hth-capture|\
  70. file:/etc/systemd/system/hthd.service|file:/etc/systemd/system/hthd@.service|\
  71. file:/usr/lib/sysusers.d/hth.conf|\
  72. file:/usr/lib/tmpfiles.d/hth.conf|\
  73. config:/etc/hth/hthd.yaml|config:/etc/hth/release-metadata.json|\
  74. config:/etc/hth/install-manifest.txt|config:/etc/hth/profiles/6400-control/profile.json|\
  75. config:/etc/hth/profiles/prod2u-door-temp/profile.json|\
  76. symlink:/etc/systemd/system/multi-user.target.wants/hthd.service|\
  77. managed_tree:/run/hth|managed_tree:/var/lib/hth/captures|\
  78. managed_tree:/var/lib/hth/replay|managed_tree:/var/lib/hth/receipts|\
  79. owned_dir:/usr/local/share/hth/sdk|owned_dir:/usr/local/share/hth|\
  80. owned_dir:/etc/hth/profiles/6400-control|\
  81. owned_dir:/etc/hth/profiles/prod2u-door-temp|owned_dir:/etc/hth/profiles|\
  82. owned_dir:/etc/hth|owned_dir:/var/lib/hth) return 0 ;;
  83. file:/usr/local/share/hth/sdk/hth_sdk-*.whl)
  84. [[ "${resolved#/usr/local/share/hth/sdk/}" != */* ]] || {
  85. echo "uninstall: nested SDK wheel path rejected: $resolved" >&2
  86. return 1
  87. }
  88. return 0
  89. ;;
  90. *) echo "uninstall: manifest path outside product allowlist: $kind $resolved" >&2; return 1 ;;
  91. esac
  92. }
  93. remove_owned_path() {
  94. local kind="$1" path="$2"
  95. safe_path "$kind" "$path" || return 1
  96. case "$kind" in
  97. file|config|symlink) rm -f -- "$path" ;;
  98. managed_tree)
  99. if [[ -e "$path" || -L "$path" ]]; then
  100. rm -rf --one-file-system -- "$path"
  101. fi
  102. ;;
  103. owned_dir)
  104. # Product-owned roots are never recursively removed. Unknown content keeps
  105. # the directory in place and cannot be mistaken for an HTH-owned tree.
  106. rmdir -- "$path" 2>/dev/null || true
  107. ;;
  108. *) echo "uninstall: unsupported removal kind: $kind" >&2; return 1 ;;
  109. esac
  110. }
  111. manifest_identity_rejected() {
  112. echo "uninstall: current manifest cannot claim service identity ownership: $1 $2" >&2
  113. return 1
  114. }
  115. declare -a FILES=()
  116. declare -a CONFIGS=()
  117. declare -a TREES=()
  118. declare -a OWNED_DIRS=()
  119. declare -a PURGE_OWNED_DIRS=()
  120. declare -A MANIFEST_ENTRIES=()
  121. if [[ ! -f "$MANIFEST" ]]; then
  122. footprint=0
  123. for path in "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
  124. "$UNITDIR/hthd.service" "$UNITDIR/hthd@.service" \
  125. "$UNITDIR/multi-user.target.wants/hthd.service" \
  126. "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf" \
  127. "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
  128. "$ETCDIR/profiles/6400-control/profile.json" \
  129. "$RUNDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"; do
  130. [[ -e "$path" || -L "$path" ]] && footprint=1
  131. done
  132. if [[ "$footprint" -eq 1 ]]; then
  133. echo "uninstall: HTH-like files exist without a trusted install manifest; refusing deletion" >&2
  134. echo "uninstall: use the new install.sh --upgrade path for verified v0.1.1 migration" >&2
  135. exit 1
  136. fi
  137. echo "HTH_UNINSTALL_PASS purge=$PURGE residual_conflicts=0 already_absent=1"
  138. exit 0
  139. fi
  140. [[ -f "$ROOT/RELEASE-METADATA.json" ]] || fail "package is missing RELEASE-METADATA.json"
  141. [[ -f "$ETCDIR/release-metadata.json" ]] ||
  142. fail "trusted install manifest requires installed release metadata"
  143. PACKAGE_VERSION="$(json_string version "$ROOT/RELEASE-METADATA.json")"
  144. PACKAGE_COMMIT="$(json_string source_commit "$ROOT/RELEASE-METADATA.json")"
  145. INSTALLED_VERSION="$(json_string version "$ETCDIR/release-metadata.json")"
  146. INSTALLED_COMMIT="$(json_string source_commit "$ETCDIR/release-metadata.json")"
  147. [[ "$PACKAGE_VERSION" =~ ^v[0-9][0-9A-Za-z.+-]*$ ]] ||
  148. fail "package release metadata has an invalid version"
  149. [[ "$PACKAGE_COMMIT" =~ ^[0-9a-f]{40}([0-9a-f]{24})?$ ]] ||
  150. fail "package release metadata has an invalid source_commit"
  151. [[ "$INSTALLED_VERSION" =~ ^v[0-9][0-9A-Za-z.+-]*$ ]] ||
  152. fail "installed release metadata has an invalid version"
  153. [[ "$INSTALLED_COMMIT" =~ ^[0-9a-f]{40}([0-9a-f]{24})?$ ]] ||
  154. fail "installed release metadata has an invalid source_commit"
  155. [[ "$INSTALLED_VERSION" == "$PACKAGE_VERSION" && "$INSTALLED_COMMIT" == "$PACKAGE_COMMIT" ]] ||
  156. fail "installed release metadata does not match this uninstall package"
  157. EXPECTED_HEADER="# HTH owned paths; generated by ${PACKAGE_VERSION} (${PACKAGE_COMMIT})"
  158. [[ "$(head -n 1 "$MANIFEST")" == "$EXPECTED_HEADER" ]] ||
  159. fail "install manifest header does not match installed release metadata"
  160. shopt -s nullglob
  161. PACKAGE_WHEELS=("$ROOT"/sdk/*.whl)
  162. shopt -u nullglob
  163. [[ "${#PACKAGE_WHEELS[@]}" -eq 1 ]] ||
  164. fail "uninstall package must contain exactly one Python SDK wheel"
  165. EXPECTED_WHEEL="$SHAREDIR/sdk/$(basename "${PACKAGE_WHEELS[0]}")"
  166. verify_current_immutable_file "$ETCDIR/release-metadata.json" \
  167. "$ROOT/RELEASE-METADATA.json" release-metadata
  168. verify_current_immutable_file "$BINDIR/hthd" "$ROOT/bin/hthd" hthd
  169. verify_current_immutable_file "$BINDIR/hthctl" "$ROOT/bin/hthctl" hthctl
  170. verify_current_immutable_file "$BINDIR/hth-capture" "$ROOT/bin/hth-capture" hth-capture
  171. verify_current_immutable_file "$UNITDIR/hthd.service" "$ROOT/service/hthd.service" hthd.service
  172. verify_current_immutable_file "$LIBDIR/sysusers.d/hth.conf" "$ROOT/service/hth.conf" hth.conf
  173. verify_current_immutable_file "$LIBDIR/tmpfiles.d/hth.conf" \
  174. "$ROOT/service/hthd-tmpfiles.conf" hthd-tmpfiles.conf
  175. verify_current_immutable_file "$ETCDIR/profiles/6400-control/profile.json" \
  176. "$ROOT/profiles/6400-control/profile.json" 6400-control-profile
  177. verify_current_immutable_file "$EXPECTED_WHEEL" "${PACKAGE_WHEELS[0]}" python-sdk-wheel
  178. MANIFEST_WHEEL_COUNT=0
  179. while read -r kind path extra; do
  180. [[ -z "$kind" || "$kind" == \#* ]] && continue
  181. [[ -n "$path" && -z "${extra:-}" ]] ||
  182. fail "malformed manifest line: $kind $path ${extra:-}"
  183. case "$kind" in
  184. file|config|symlink|managed_tree|owned_dir) safe_path "$kind" "$path" || exit 1 ;;
  185. created_account|created_group) manifest_identity_rejected "$kind" "$path" || exit 1 ;;
  186. *) fail "unknown current manifest kind: $kind" ;;
  187. esac
  188. manifest_key="$kind $path"
  189. [[ -z "${MANIFEST_ENTRIES[$manifest_key]:-}" ]] ||
  190. fail "duplicate current manifest entry: $manifest_key"
  191. MANIFEST_ENTRIES["$manifest_key"]=1
  192. case "$kind:$path" in
  193. file:$BINDIR/hthd|file:$BINDIR/hthctl|file:$BINDIR/hth-capture|\
  194. file:$UNITDIR/hthd.service|file:$LIBDIR/sysusers.d/hth.conf|\
  195. file:$LIBDIR/tmpfiles.d/hth.conf|file:$EXPECTED_WHEEL|\
  196. config:$ETCDIR/hthd.yaml|config:$ETCDIR/release-metadata.json|\
  197. config:$ETCDIR/install-manifest.txt|\
  198. config:$ETCDIR/profiles/6400-control/profile.json|\
  199. symlink:$UNITDIR/multi-user.target.wants/hthd.service|\
  200. managed_tree:$RUNDIR|managed_tree:$STATEDIR/captures|\
  201. managed_tree:$STATEDIR/replay|managed_tree:$STATEDIR/receipts|\
  202. owned_dir:$SHAREDIR/sdk|owned_dir:$SHAREDIR|\
  203. owned_dir:$ETCDIR/profiles/6400-control|owned_dir:$ETCDIR/profiles|\
  204. owned_dir:$ETCDIR|owned_dir:$STATEDIR) ;;
  205. *) fail "entry is not valid for a current HTH manifest: $manifest_key" ;;
  206. esac
  207. if [[ "$kind" == file && "$path" == "$SHAREDIR"/sdk/hth_sdk-*.whl ]]; then
  208. ((MANIFEST_WHEEL_COUNT += 1))
  209. fi
  210. case "$kind" in
  211. file|symlink) FILES+=("$path") ;;
  212. config) CONFIGS+=("$path") ;;
  213. managed_tree) TREES+=("$path") ;;
  214. owned_dir)
  215. case "$path" in
  216. "$SHAREDIR"|"$SHAREDIR/sdk") OWNED_DIRS+=("$path") ;;
  217. *) PURGE_OWNED_DIRS+=("$path") ;;
  218. esac
  219. ;;
  220. esac
  221. done <"$MANIFEST"
  222. while read -r required_kind required_path; do
  223. required_key="$required_kind $required_path"
  224. [[ "${MANIFEST_ENTRIES[$required_key]:-0}" -eq 1 ]] ||
  225. fail "current manifest is incomplete: $required_key"
  226. done <<EOF
  227. file $BINDIR/hthd
  228. file $BINDIR/hthctl
  229. file $BINDIR/hth-capture
  230. file $UNITDIR/hthd.service
  231. file $LIBDIR/sysusers.d/hth.conf
  232. file $LIBDIR/tmpfiles.d/hth.conf
  233. file $EXPECTED_WHEEL
  234. config $ETCDIR/hthd.yaml
  235. config $ETCDIR/release-metadata.json
  236. config $ETCDIR/install-manifest.txt
  237. config $ETCDIR/profiles/6400-control/profile.json
  238. managed_tree $RUNDIR
  239. managed_tree $STATEDIR/captures
  240. managed_tree $STATEDIR/replay
  241. managed_tree $STATEDIR/receipts
  242. owned_dir $SHAREDIR/sdk
  243. owned_dir $SHAREDIR
  244. owned_dir $ETCDIR/profiles/6400-control
  245. owned_dir $ETCDIR/profiles
  246. owned_dir $ETCDIR
  247. owned_dir $STATEDIR
  248. EOF
  249. [[ "$MANIFEST_WHEEL_COUNT" -eq 1 ]] ||
  250. fail "current manifest must contain exactly one owned SDK wheel"
  251. systemctl disable --now "$SERVICE" >/dev/null 2>&1 || true
  252. systemctl disable --now 'hthd@*.service' >/dev/null 2>&1 || true
  253. if systemctl is-active --quiet "$SERVICE"; then
  254. echo "uninstall: $SERVICE is still active" >&2
  255. exit 1
  256. fi
  257. for path in "${FILES[@]}"; do
  258. case "$path" in
  259. "$UNITDIR"/multi-user.target.wants/*) remove_owned_path symlink "$path" ;;
  260. *) remove_owned_path file "$path" ;;
  261. esac
  262. done
  263. systemctl daemon-reload >/dev/null 2>&1 || true
  264. if [[ "$PURGE" -eq 1 ]]; then
  265. for path in "${CONFIGS[@]}"; do remove_owned_path config "$path"; done
  266. for path in "${TREES[@]}"; do remove_owned_path managed_tree "$path"; done
  267. for path in "${PURGE_OWNED_DIRS[@]}"; do remove_owned_path owned_dir "$path"; done
  268. fi
  269. for path in "${OWNED_DIRS[@]}"; do remove_owned_path owned_dir "$path"; done
  270. failures=0
  271. for path in "${FILES[@]}"; do
  272. if [[ -e "$path" || -L "$path" ]]; then echo "uninstall residual: $path" >&2; failures=1; fi
  273. done
  274. for proc_dir in /proc/[0-9]*; do
  275. [[ -r "$proc_dir/comm" ]] || continue
  276. if [[ "$(<"$proc_dir/comm")" == hthd ]]; then
  277. echo "uninstall residual: hthd process ${proc_dir##*/}" >&2
  278. failures=1
  279. fi
  280. done
  281. if [[ "$PURGE" -eq 1 ]]; then
  282. for path in "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" "$ETCDIR/install-manifest.txt" \
  283. "$ETCDIR/profiles/6400-control/profile.json" "$RUNDIR" \
  284. "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts"; do
  285. if [[ -e "$path" || -L "$path" ]]; then echo "purge residual: $path" >&2; failures=1; fi
  286. done
  287. fi
  288. [[ "$failures" -eq 0 ]] || exit 1
  289. echo "HTH_UNINSTALL_PASS purge=$PURGE residual_conflicts=0"