Files
faumaray bdd69f0aad update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-25 23:44:48 +03:00

403 lines
14 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
PROGRAM=${0##*/}
SERVICE_NAME=echoion.service
UPDATER_SERVICE_NAME=echoion-updater.service
UPDATER_TIMER_NAME=echoion-updater.timer
SERVICE_USER=echoion
RECORD_NAME=.echoion-install
JSON_RECORD_NAME=install.json
MANAGED_MARKER=.echoion-managed
die() {
printf '%s: %s\n' "$PROGRAM" "$*" >&2
exit 1
}
warn() {
printf '%s: warning: %s\n' "$PROGRAM" "$*" >&2
}
usage() {
cat <<'EOF'
Usage: uninstall.sh [--purge] [--non-interactive]
Remove the Echoion runtime and its matching registered service. Configuration
and state are preserved unless --purge is explicitly supplied.
EOF
}
sha256_file() {
local result
result=$(sha256sum -- "$1")
printf '%s\n' "${result%% *}"
}
systemctl_for() {
local scope=$1
shift
if [[ $scope == user ]]; then
systemctl --user "$@"
else
systemctl "$@"
fi
}
acquire_prefix_lock() {
local lock_prefix=$1
command -v flock >/dev/null 2>&1 || die "flock is required for safe uninstallation"
exec {UNINSTALL_LOCK_FD}< "$lock_prefix"
flock -n "$UNINSTALL_LOCK_FD" || die "another Echoion install or uninstall is active for '$lock_prefix'"
}
safe_unit_path() {
local scope=$1
local unit=$2
[[ $unit == /* && $unit != *'/../'* && $unit != *'/./'* ]] || return 1
case $scope:$unit in
system:/etc/systemd/system/echoion.service) return 0 ;;
system:/etc/systemd/system/echoion-updater.service) return 0 ;;
system:/etc/systemd/system/echoion-updater.timer) return 0 ;;
user:/*/systemd/user/echoion.service) return 0 ;;
user:/*/systemd/user/echoion-updater.service) return 0 ;;
user:/*/systemd/user/echoion-updater.timer) return 0 ;;
*) return 1 ;;
esac
}
safe_purge_path() {
local path=$1
local prefix=$2
[[ $path == /* && $path != / ]] || return 1
local relative=${path#/}
[[ $relative == */* ]] || return 1
case $path in
/etc | /var | /home | /root | /usr | /usr/local | /opt | /tmp | /var/tmp | /run)
return 1
;;
esac
case $path in
"$prefix" | "$prefix/current" | "$prefix/current"/* | "$prefix/versions" | "$prefix/versions"/*)
return 1
;;
esac
return 0
}
validate_managed_purge_path() {
local kind=$1
local path=$2
local safety_prefix=$3
local marker_prefix=$4
local install_id=$5
local allowed=$6
local expected_device=$7
local expected_inode=$8
local expected_uid=$9
local expected_gid=${10}
local expected_hash=${11}
local marker=$path/$MANAGED_MARKER
local device inode uid gid
[[ $allowed == 1 ]] || die "$kind directory is not marked as installer-owned: '$path'"
safe_purge_path "$path" "$safety_prefix" || die "refusing unsafe purge path '$path'"
if [[ ! -e $path && ! -L $path ]]; then
return 0
fi
[[ -d $path && ! -L $path ]] || die "$kind purge path is not a real directory: '$path'"
[[ $expected_device =~ ^[0-9]+$ && $expected_inode =~ ^[0-9]+$ &&
$expected_uid =~ ^[0-9]+$ && $expected_gid =~ ^[0-9]+$ && -n $expected_hash ]] ||
die "$kind purge identity is incomplete"
device=$(stat -c '%d' -- "$path")
inode=$(stat -c '%i' -- "$path")
uid=$(stat -c '%u' -- "$path")
gid=$(stat -c '%g' -- "$path")
[[ $device == "$expected_device" && $inode == "$expected_inode" &&
$uid == "$expected_uid" && $gid == "$expected_gid" ]] ||
die "$kind directory identity changed; refusing purge: '$path'"
[[ -f $marker && ! -L $marker ]] || die "$kind ownership marker is missing: '$marker'"
[[ $(sha256_file "$marker") == "$expected_hash" ]] ||
die "$kind ownership marker changed: '$marker'"
grep -Fx -- "install_id=$install_id" "$marker" >/dev/null ||
die "$kind ownership marker has the wrong install identity"
grep -Fx -- "prefix=$marker_prefix" "$marker" >/dev/null ||
die "$kind ownership marker belongs to another prefix"
}
purge=0
while [[ $# -gt 0 ]]; do
case $1 in
--purge) purge=1 ;;
--non-interactive) ;;
-h | --help)
usage
exit 0
;;
*) die "unknown option: $1" ;;
esac
shift
done
SCRIPT_DIR=$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
prefix=$SCRIPT_DIR
[[ $prefix != / ]] || die "refusing to uninstall from the filesystem root"
acquire_prefix_lock "$prefix"
record=$prefix/$RECORD_NAME
[[ -f $record ]] || die "install record is missing: $record"
record_prefix=
relocatable=0
service_scope=none
service_unit=
service_unit_sha256=
updater_service_unit=
updater_service_unit_sha256=
updater_timer_unit=
updater_timer_unit_sha256=
updater_helper=
updater_helper_sha256=
config_dir=
state_dir=
cache_dir=
logs_dir=
incoming_dir=
mode=
data_layout=
launcher=
launcher_sha256=
created_system_user=0
service_uid=
service_gid=
install_id=
config_purge_allowed=0
config_dir_device=
config_dir_inode=
config_dir_uid=
config_dir_gid=
config_marker_sha256=
state_purge_allowed=0
state_dir_device=
state_dir_inode=
state_dir_uid=
state_dir_gid=
state_marker_sha256=
key=
value=
while IFS='=' read -r key value; do
case $key in
prefix) record_prefix=$value ;;
relocatable) relocatable=$value ;;
service_scope) service_scope=$value ;;
service_unit) service_unit=$value ;;
service_unit_sha256) service_unit_sha256=$value ;;
updater_service_unit) updater_service_unit=$value ;;
updater_service_unit_sha256) updater_service_unit_sha256=$value ;;
updater_timer_unit) updater_timer_unit=$value ;;
updater_timer_unit_sha256) updater_timer_unit_sha256=$value ;;
updater_helper) updater_helper=$value ;;
updater_helper_sha256) updater_helper_sha256=$value ;;
config_dir) config_dir=$value ;;
state_dir) state_dir=$value ;;
cache_dir) cache_dir=$value ;;
logs_dir) logs_dir=$value ;;
incoming_dir) incoming_dir=$value ;;
mode) mode=$value ;;
data_layout) data_layout=$value ;;
launcher) launcher=$value ;;
launcher_sha256) launcher_sha256=$value ;;
created_system_user) created_system_user=$value ;;
service_uid) service_uid=$value ;;
service_gid) service_gid=$value ;;
install_id) install_id=$value ;;
config_purge_allowed) config_purge_allowed=$value ;;
config_dir_device) config_dir_device=$value ;;
config_dir_inode) config_dir_inode=$value ;;
config_dir_uid) config_dir_uid=$value ;;
config_dir_gid) config_dir_gid=$value ;;
config_marker_sha256) config_marker_sha256=$value ;;
state_purge_allowed) state_purge_allowed=$value ;;
state_dir_device) state_dir_device=$value ;;
state_dir_inode) state_dir_inode=$value ;;
state_dir_uid) state_dir_uid=$value ;;
state_dir_gid) state_dir_gid=$value ;;
state_marker_sha256) state_marker_sha256=$value ;;
esac
done < "$record"
case $relocatable in
0)
[[ $record_prefix == "$prefix" ]] ||
die "install record belongs to '$record_prefix', not '$prefix'"
;;
1)
[[ $record_prefix == . && $mode == portable && $data_layout == self-contained ]] ||
die "relocatable install record is incomplete or unsafe"
[[ $config_dir == config && ( $state_dir == state || $state_dir == data ) ]] ||
die "relocatable install record has unsafe mutable paths"
config_dir=$prefix/config
state_dir=$prefix/$state_dir
if [[ -n $cache_dir || -n $logs_dir || -n $incoming_dir ]]; then
[[ $cache_dir == cache && $logs_dir == logs && $incoming_dir == incoming ]] ||
die "relocatable install record has unsafe auxiliary paths"
cache_dir=$prefix/cache
logs_dir=$prefix/logs
incoming_dir=$prefix/incoming
fi
;;
*) die "install record has an invalid relocatable value" ;;
esac
case $service_scope in
none | user | system) ;;
*) die "install record has an invalid service scope" ;;
esac
case $created_system_user in
0 | 1) ;;
*) die "install record has an invalid created_system_user value" ;;
esac
if [[ $service_scope == system || $mode == system || $created_system_user == 1 ]]; then
[[ ${EUID:-$(id -u)} -eq 0 ]] || die "uninstalling the system service requires root"
fi
# Complete every destructive preflight before stopping the service or removing
# any files, so a refused purge leaves the installation fully retryable.
if [[ $service_scope != none ]]; then
[[ -n $service_unit && -n $service_unit_sha256 ]] || die "service record is incomplete"
safe_unit_path "$service_scope" "$service_unit" || die "service record has an unsafe unit path"
[[ -f $service_unit ]] || die "registered service unit is missing: $service_unit"
[[ $(sha256_file "$service_unit") == "$service_unit_sha256" ]] ||
die "service unit was modified; refusing to remove it: $service_unit"
for updater_record in service timer; do
if [[ $updater_record == service ]]; then
updater_unit=$updater_service_unit
updater_hash=$updater_service_unit_sha256
else
updater_unit=$updater_timer_unit
updater_hash=$updater_timer_unit_sha256
fi
if [[ -n $updater_unit || -n $updater_hash ]]; then
[[ -n $updater_unit && -n $updater_hash ]] ||
die "updater $updater_record record is incomplete"
safe_unit_path "$service_scope" "$updater_unit" ||
die "updater $updater_record record has an unsafe unit path"
[[ -f $updater_unit ]] ||
die "registered updater unit is missing: $updater_unit"
[[ $(sha256_file "$updater_unit") == "$updater_hash" ]] ||
die "updater unit was modified; refusing to remove it: $updater_unit"
fi
done
fi
updater_helper_path=
updater_bundle_path=
if [[ -n $updater_helper ]]; then
[[ $updater_helper =~ ^libexec/updater/[A-Za-z0-9][A-Za-z0-9._-]{0,63}/bin/echoion-updater$ &&
$updater_helper_sha256 =~ ^[0-9a-f]{64}$ ]] ||
die "privileged updater helper record is incomplete or unsafe"
updater_helper_path=$prefix/$updater_helper
updater_bundle_path=$prefix/${updater_helper%/bin/echoion-updater}
[[ -d $updater_bundle_path && ! -L $updater_bundle_path ]] ||
die "recorded privileged updater bundle is missing or unsafe: $updater_bundle_path"
[[ -f $updater_helper_path && ! -L $updater_helper_path &&
-x $updater_helper_path ]] ||
die "recorded privileged updater helper is missing or unsafe: $updater_helper_path"
[[ $(sha256_file "$updater_helper_path") == "$updater_helper_sha256" ]] ||
die "privileged updater helper was modified; refusing to remove it: $updater_helper_path"
elif [[ -n $updater_helper_sha256 ]]; then
die "privileged updater helper record is incomplete"
fi
launcher_path=
if [[ -n $launcher ]]; then
[[ $launcher == echoion && $launcher_sha256 =~ ^[0-9a-f]{64}$ ]] ||
die "portable launcher record is incomplete or unsafe"
launcher_path=$prefix/$launcher
[[ -f $launcher_path && ! -L $launcher_path && -x $launcher_path ]] ||
die "recorded portable launcher is missing or unsafe: $launcher_path"
[[ $(sha256_file "$launcher_path") == "$launcher_sha256" ]] ||
die "portable launcher was modified; refusing to remove it: $launcher_path"
elif [[ -n $launcher_sha256 ]]; then
die "portable launcher record is incomplete"
fi
if [[ -e $prefix/current && ! -L $prefix/current ]]; then
die "current is not a symlink; refusing to remove runtime files"
fi
if [[ $purge -eq 1 ]]; then
[[ $install_id =~ ^[A-Za-z0-9-]{16,64}$ ]] || die "install record has no safe install identity"
[[ -n $config_dir && -n $state_dir ]] || die "install record has an empty purge path"
validate_managed_purge_path config "$config_dir" "$prefix" "$record_prefix" "$install_id" \
"$config_purge_allowed" "$config_dir_device" "$config_dir_inode" \
"$config_dir_uid" "$config_dir_gid" "$config_marker_sha256"
validate_managed_purge_path state "$state_dir" "$prefix" "$record_prefix" "$install_id" \
"$state_purge_allowed" "$state_dir_device" "$state_dir_inode" \
"$state_dir_uid" "$state_dir_gid" "$state_marker_sha256"
fi
service_account_present=0
if [[ $purge -eq 1 && $created_system_user == 1 ]]; then
[[ $service_uid =~ ^[0-9]+$ && $service_gid =~ ^[0-9]+$ ]] ||
die "installer-created service account record has no uid/gid identity"
if id -u "$SERVICE_USER" >/dev/null 2>&1; then
[[ $(id -u "$SERVICE_USER") -eq $service_uid &&
$(id -g "$SERVICE_USER") -eq $service_gid ]] ||
die "the recorded $SERVICE_USER account was deleted or replaced"
service_account_present=1
fi
fi
if [[ $service_scope != none ]]; then
service_names=("$SERVICE_NAME")
[[ -z $updater_timer_unit ]] || service_names+=("$UPDATER_TIMER_NAME")
if [[ -n $updater_service_unit ]] &&
! systemctl_for "$service_scope" stop "$UPDATER_SERVICE_NAME"; then
die "could not stop $UPDATER_SERVICE_NAME; runtime was not removed"
fi
if ! systemctl_for "$service_scope" disable --now "${service_names[@]}"; then
die "could not stop and disable $SERVICE_NAME; runtime was not removed"
fi
fi
rm -f -- "$prefix/current"
rm -rf -- "$prefix/versions"
[[ -z $updater_bundle_path ]] || rm -rf -- "$updater_bundle_path"
rmdir -- "$prefix/libexec/updater" "$prefix/libexec" 2>/dev/null || true
if [[ $purge -eq 1 ]]; then
if [[ $config_dir == "$state_dir" ]]; then
rm -rf -- "$config_dir"
else
rm -rf -- "$config_dir" "$state_dir"
fi
if [[ $data_layout == self-contained ]]; then
for auxiliary in "$cache_dir" "$logs_dir" "$incoming_dir"; do
[[ -z $auxiliary || $auxiliary == "$prefix/cache" ||
$auxiliary == "$prefix/logs" || $auxiliary == "$prefix/incoming" ]] ||
die "refusing unsafe self-contained auxiliary path: '$auxiliary'"
[[ -z $auxiliary ]] || rm -rf -- "$auxiliary"
done
fi
fi
if [[ $purge -eq 1 && $created_system_user == 1 && $service_account_present -eq 1 ]]; then
userdel "$SERVICE_USER"
fi
if [[ $service_scope != none ]]; then
rm -f -- "$service_unit"
[[ -z $updater_service_unit ]] || rm -f -- "$updater_service_unit"
[[ -z $updater_timer_unit ]] || rm -f -- "$updater_timer_unit"
systemctl_for "$service_scope" daemon-reload || warn "systemd daemon-reload failed"
fi
[[ -z $launcher_path ]] || rm -f -- "$launcher_path"
rm -f -- "$record" "$prefix/$JSON_RECORD_NAME" "$prefix/install.sh" "$prefix/uninstall.sh"
rmdir -- "$prefix" 2>/dev/null || true
printf 'Echoion runtime removed from %s.\n' "$prefix"
if [[ $purge -eq 0 ]]; then
printf 'Configuration and state were preserved:\n %s\n %s\n' "$config_dir" "$state_dir"
fi