|
|
@@ -47,6 +47,53 @@ json_string() {
|
|
|
sed -n "s/^[[:space:]]*\"${key}\"[[:space:]]*:[[:space:]]*\"\([^\"]*\)\".*/\1/p" "$file" | head -n 1
|
|
|
}
|
|
|
|
|
|
+safe_product_path() {
|
|
|
+ local kind="$1" path="$2" resolved
|
|
|
+ [[ "$path" == /* ]] || fail "non-absolute product path: $path"
|
|
|
+ resolved="$(realpath -m -s -- "$path")"
|
|
|
+ [[ "$resolved" == "$path" ]] || fail "unresolved product path rejected: $path -> $resolved"
|
|
|
+ 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|owned_dir:/etc/hth/profiles/prod2u-door-temp) return 0 ;;
|
|
|
+ file:/usr/local/share/hth/sdk/hth_sdk-*.whl)
|
|
|
+ [[ "${resolved#/usr/local/share/hth/sdk/}" != */* ]] ||
|
|
|
+ fail "nested SDK wheel path rejected: $resolved"
|
|
|
+ return 0
|
|
|
+ ;;
|
|
|
+ *) fail "product path outside upgrade allowlist: $kind $resolved" ;;
|
|
|
+ esac
|
|
|
+}
|
|
|
+
|
|
|
+remove_product_path() {
|
|
|
+ local kind="$1" path="$2"
|
|
|
+ safe_product_path "$kind" "$path"
|
|
|
+ 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) rmdir -- "$path" 2>/dev/null || true ;;
|
|
|
+ *) fail "unsupported product removal kind: $kind" ;;
|
|
|
+ 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 ;;
|
|
|
+ *) fail "identity outside product allowlist: $1 $2" ;;
|
|
|
+ esac
|
|
|
+}
|
|
|
+
|
|
|
[[ "$(uname -m)" == x86_64 ]] || fail "this release targets x86_64 Linux (found $(uname -m))"
|
|
|
[[ "$(id -u)" -eq 0 ]] || fail "run as root, for example: sudo ./install.sh"
|
|
|
[[ -f "$ROOT/RELEASE-METADATA.json" ]] || fail "package is missing RELEASE-METADATA.json"
|
|
|
@@ -58,15 +105,20 @@ case "$BACKEND" in mock|serial) ;; *) fail "--backend must be mock or serial" ;;
|
|
|
if [[ "$BACKEND" == serial && ! -e "$SERIAL_PATH" ]]; then
|
|
|
fail "serial device does not exist: $SERIAL_PATH"
|
|
|
fi
|
|
|
-for command_name in realpath readlink sha256sum systemctl systemd-sysusers systemd-tmpfiles base64 find; do
|
|
|
+for command_name in realpath readlink sha256sum systemctl systemd-sysusers systemd-tmpfiles base64 find getent groupadd; do
|
|
|
command -v "$command_name" >/dev/null 2>&1 || fail "required command is missing: $command_name"
|
|
|
done
|
|
|
+shopt -s nullglob
|
|
|
+PACKAGE_WHEELS=("$ROOT"/sdk/*.whl)
|
|
|
+shopt -u nullglob
|
|
|
+[[ "${#PACKAGE_WHEELS[@]}" -eq 1 ]] || fail "package must contain exactly one Python SDK wheel"
|
|
|
+PACKAGE_WHEEL="${PACKAGE_WHEELS[0]}"
|
|
|
for required in \
|
|
|
"$ROOT/bin/hthd" "$ROOT/bin/hthctl" "$ROOT/bin/hth-capture" \
|
|
|
"$ROOT/config/hthd.6400-mock.yaml" "$ROOT/config/hthd.6400-serial.yaml" \
|
|
|
"$ROOT/service/hthd.service" "$ROOT/service/hth.conf" \
|
|
|
"$ROOT/service/hthd-tmpfiles.conf" "$ROOT/profiles/6400-control/profile.json" \
|
|
|
- "$ROOT/sdk"/*.whl "$ROOT/verify-install.sh"; do
|
|
|
+ "$PACKAGE_WHEEL" "$ROOT/verify-install.sh"; do
|
|
|
[[ -f "$required" ]] || fail "package is incomplete: $required"
|
|
|
done
|
|
|
if [[ -f "$ROOT/SHA256SUMS" ]]; then
|
|
|
@@ -79,6 +131,7 @@ for absolute_dir in "$BINDIR" "$SHAREDIR" "$ETCDIR" "$UNITDIR" "$STATEDIR" "$RUN
|
|
|
done
|
|
|
|
|
|
INSTALLED_META="$ETCDIR/release-metadata.json"
|
|
|
+OLD_MANIFEST="$ETCDIR/install-manifest.txt"
|
|
|
INSTALLED_VERSION=""
|
|
|
INSTALLED_COMMIT=""
|
|
|
if [[ -f "$INSTALLED_META" ]]; then
|
|
|
@@ -90,6 +143,51 @@ for path in "$BINDIR/hthd" "$UNITDIR/hthd.service" "$ETCDIR/hthd.yaml"; do
|
|
|
[[ -e "$path" || -L "$path" ]] && FOOTPRINT=1
|
|
|
done
|
|
|
|
|
|
+IDENTITY_NAMES=(hth hth-observe hth-control)
|
|
|
+declare -A PREEXISTING_ACCOUNTS=()
|
|
|
+declare -A PREEXISTING_GROUPS=()
|
|
|
+declare -A OWNED_ACCOUNTS=()
|
|
|
+declare -A OWNED_GROUPS=()
|
|
|
+declare -a OWNED_WHEELS=()
|
|
|
+for identity_name in "${IDENTITY_NAMES[@]}"; do
|
|
|
+ if getent passwd "$identity_name" >/dev/null 2>&1; then
|
|
|
+ PREEXISTING_ACCOUNTS["$identity_name"]=1
|
|
|
+ else
|
|
|
+ PREEXISTING_ACCOUNTS["$identity_name"]=0
|
|
|
+ fi
|
|
|
+ if getent group "$identity_name" >/dev/null 2>&1; then
|
|
|
+ PREEXISTING_GROUPS["$identity_name"]=1
|
|
|
+ else
|
|
|
+ PREEXISTING_GROUPS["$identity_name"]=0
|
|
|
+ fi
|
|
|
+done
|
|
|
+if [[ -f "$OLD_MANIFEST" ]]; then
|
|
|
+ while read -r old_kind old_value old_extra; do
|
|
|
+ [[ -z "$old_kind" || "$old_kind" == \#* ]] && continue
|
|
|
+ case "$old_kind" in
|
|
|
+ created_account)
|
|
|
+ [[ -n "$old_value" && -z "${old_extra:-}" ]] || fail "malformed created_account in old manifest"
|
|
|
+ identity_allowed "$old_kind" "$old_value"
|
|
|
+ OWNED_ACCOUNTS["$old_value"]=1
|
|
|
+ ;;
|
|
|
+ created_group)
|
|
|
+ [[ -n "$old_value" && -z "${old_extra:-}" ]] || fail "malformed created_group in old manifest"
|
|
|
+ identity_allowed "$old_kind" "$old_value"
|
|
|
+ OWNED_GROUPS["$old_value"]=1
|
|
|
+ ;;
|
|
|
+ file)
|
|
|
+ case "$old_value" in
|
|
|
+ "$SHAREDIR"/sdk/hth_sdk-*.whl)
|
|
|
+ [[ -z "${old_extra:-}" ]] || fail "malformed SDK wheel in old manifest"
|
|
|
+ safe_product_path file "$old_value"
|
|
|
+ OWNED_WHEELS+=("$old_value")
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+ ;;
|
|
|
+ esac
|
|
|
+ done <"$OLD_MANIFEST"
|
|
|
+fi
|
|
|
+
|
|
|
ACTION=install
|
|
|
FROM_VERSION=none
|
|
|
PRESERVE_CONFIG=0
|
|
|
@@ -121,22 +219,20 @@ if systemctl is-active --quiet "$SERVICE"; then
|
|
|
fi
|
|
|
|
|
|
if [[ "$ACTION" == upgrade ]]; then
|
|
|
- rm -f -- \
|
|
|
- "$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"
|
|
|
- rm -f -- \
|
|
|
- "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
|
|
|
- "$ETCDIR/install-manifest.txt" \
|
|
|
- "$ETCDIR/profiles/prod2u-door-temp/profile.json" \
|
|
|
- "$ETCDIR/profiles/6400-control/profile.json"
|
|
|
- rmdir "$ETCDIR/profiles/prod2u-door-temp" 2>/dev/null || true
|
|
|
- if [[ -d "$RUNDIR" ]]; then
|
|
|
- resolved="$(realpath -m -s -- "$RUNDIR")"
|
|
|
- [[ "$resolved" == /run/hth ]] || fail "unsafe runtime path during upgrade: $resolved"
|
|
|
- rm -rf --one-file-system -- "$resolved"
|
|
|
- fi
|
|
|
+ for path in "$BINDIR/hthd" "$BINDIR/hthctl" "$BINDIR/hth-capture" \
|
|
|
+ "$UNITDIR/hthd.service" "$UNITDIR/hthd@.service" \
|
|
|
+ "$LIBDIR/sysusers.d/hth.conf" "$LIBDIR/tmpfiles.d/hth.conf"; do
|
|
|
+ remove_product_path file "$path"
|
|
|
+ done
|
|
|
+ remove_product_path symlink "$UNITDIR/multi-user.target.wants/hthd.service"
|
|
|
+ for path in "$ETCDIR/hthd.yaml" "$ETCDIR/release-metadata.json" \
|
|
|
+ "$ETCDIR/install-manifest.txt" \
|
|
|
+ "$ETCDIR/profiles/prod2u-door-temp/profile.json" \
|
|
|
+ "$ETCDIR/profiles/6400-control/profile.json"; do
|
|
|
+ remove_product_path config "$path"
|
|
|
+ done
|
|
|
+ remove_product_path owned_dir "$ETCDIR/profiles/prod2u-door-temp"
|
|
|
+ remove_product_path managed_tree "$RUNDIR"
|
|
|
systemctl daemon-reload
|
|
|
fi
|
|
|
|
|
|
@@ -151,8 +247,8 @@ install -m 0644 "$ROOT/service/hth.conf" "$LIBDIR/sysusers.d/hth.conf"
|
|
|
install -m 0644 "$ROOT/service/hthd-tmpfiles.conf" "$LIBDIR/tmpfiles.d/hth.conf"
|
|
|
install -m 0644 "$ROOT/RELEASE-METADATA.json" "$ETCDIR/release-metadata.json"
|
|
|
install -m 0644 "$ROOT/profiles/6400-control/profile.json" "$ETCDIR/profiles/6400-control/profile.json"
|
|
|
-rm -f -- "$SHAREDIR/sdk"/*.whl
|
|
|
-install -m 0644 "$ROOT"/sdk/*.whl "$SHAREDIR/sdk/"
|
|
|
+for path in "${OWNED_WHEELS[@]}"; do remove_product_path file "$path"; done
|
|
|
+install -m 0644 "$PACKAGE_WHEEL" "$SHAREDIR/sdk/"
|
|
|
|
|
|
if [[ "$PRESERVE_CONFIG" -eq 0 || ! -f "$ETCDIR/hthd.yaml" ]]; then
|
|
|
template="$ROOT/config/hthd.6400-${BACKEND}.yaml"
|
|
|
@@ -166,6 +262,18 @@ if [[ "$PRESERVE_CONFIG" -eq 0 || ! -f "$ETCDIR/hthd.yaml" ]]; then
|
|
|
install -m 0640 "$temp_config" "$ETCDIR/hthd.yaml"
|
|
|
fi
|
|
|
|
|
|
+getent group dialout >/dev/null 2>&1 || groupadd --system dialout
|
|
|
+systemd-sysusers "$LIBDIR/sysusers.d/hth.conf"
|
|
|
+for identity_name in "${IDENTITY_NAMES[@]}"; do
|
|
|
+ if [[ "${PREEXISTING_ACCOUNTS[$identity_name]}" -eq 0 ]] && getent passwd "$identity_name" >/dev/null 2>&1; then
|
|
|
+ OWNED_ACCOUNTS["$identity_name"]=1
|
|
|
+ fi
|
|
|
+ if [[ "${PREEXISTING_GROUPS[$identity_name]}" -eq 0 ]] && getent group "$identity_name" >/dev/null 2>&1; then
|
|
|
+ OWNED_GROUPS["$identity_name"]=1
|
|
|
+ fi
|
|
|
+done
|
|
|
+systemd-tmpfiles --create "$LIBDIR/tmpfiles.d/hth.conf"
|
|
|
+
|
|
|
cat >"$ETCDIR/install-manifest.txt" <<EOF
|
|
|
# HTH owned paths; generated by ${VERSION} (${SOURCE_COMMIT})
|
|
|
file $BINDIR/hthd
|
|
|
@@ -175,7 +283,7 @@ file $UNITDIR/hthd.service
|
|
|
symlink $UNITDIR/multi-user.target.wants/hthd.service
|
|
|
file $LIBDIR/sysusers.d/hth.conf
|
|
|
file $LIBDIR/tmpfiles.d/hth.conf
|
|
|
-file $SHAREDIR/sdk/$(basename "$ROOT"/sdk/*.whl)
|
|
|
+file $SHAREDIR/sdk/$(basename "$PACKAGE_WHEEL")
|
|
|
config $ETCDIR/hthd.yaml
|
|
|
config $ETCDIR/release-metadata.json
|
|
|
config $ETCDIR/install-manifest.txt
|
|
|
@@ -185,18 +293,24 @@ managed_tree $RUNDIR
|
|
|
managed_tree $STATEDIR/captures
|
|
|
managed_tree $STATEDIR/replay
|
|
|
managed_tree $STATEDIR/receipts
|
|
|
-account hth
|
|
|
-account hth-observe
|
|
|
-account hth-control
|
|
|
-group hth
|
|
|
-group hth-observe
|
|
|
-group hth-control
|
|
|
+owned_dir $SHAREDIR/sdk
|
|
|
+owned_dir $SHAREDIR
|
|
|
+owned_dir $ETCDIR/profiles/6400-control
|
|
|
+owned_dir $ETCDIR/profiles/prod2u-door-temp
|
|
|
+owned_dir $ETCDIR/profiles
|
|
|
+owned_dir $ETCDIR
|
|
|
+owned_dir $STATEDIR
|
|
|
EOF
|
|
|
+for identity_name in "${IDENTITY_NAMES[@]}"; do
|
|
|
+ if [[ "${OWNED_ACCOUNTS[$identity_name]:-0}" -eq 1 ]]; then
|
|
|
+ echo "created_account $identity_name" >>"$ETCDIR/install-manifest.txt"
|
|
|
+ fi
|
|
|
+ if [[ "${OWNED_GROUPS[$identity_name]:-0}" -eq 1 ]]; then
|
|
|
+ echo "created_group $identity_name" >>"$ETCDIR/install-manifest.txt"
|
|
|
+ fi
|
|
|
+done
|
|
|
chmod 0644 "$ETCDIR/install-manifest.txt"
|
|
|
|
|
|
-getent group dialout >/dev/null 2>&1 || groupadd --system dialout
|
|
|
-systemd-sysusers "$LIBDIR/sysusers.d/hth.conf"
|
|
|
-systemd-tmpfiles --create "$LIBDIR/tmpfiles.d/hth.conf"
|
|
|
chown root:hth "$ETCDIR" "$ETCDIR/hthd.yaml" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control" 2>/dev/null || true
|
|
|
chown hth:hth "$STATEDIR" "$STATEDIR/captures" "$STATEDIR/replay" "$STATEDIR/receipts" 2>/dev/null || true
|
|
|
chmod 0750 "$ETCDIR" "$ETCDIR/profiles" "$ETCDIR/profiles/6400-control"
|