1 parent
818d391022
commit
639362fbf5
40 files changed
+6701
-429
No files matched your search
Executable
+314
@@ -0,0 +1,314 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
PROGRAM=${0##*/}
|
||||
SERVICE_NAME=echoion.service
|
||||
SERVICE_USER=echoion
|
||||
RECORD_NAME=.echoion-install
|
||||
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 ;;
|
||||
user:/*/systemd/user/echoion.service) 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=
|
||||
config_dir=
|
||||
state_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 ;;
|
||||
config_dir) config_dir=$value ;;
|
||||
state_dir) state_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 == data ]] ||
|
||||
die "relocatable install record has unsafe mutable paths"
|
||||
config_dir=$prefix/config
|
||||
state_dir=$prefix/data
|
||||
;;
|
||||
*) 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"
|
||||
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
|
||||
if ! systemctl_for "$service_scope" disable --now "$SERVICE_NAME"; then
|
||||
die "could not stop and disable $SERVICE_NAME; runtime was not removed"
|
||||
fi
|
||||
fi
|
||||
|
||||
rm -f -- "$prefix/current"
|
||||
rm -rf -- "$prefix/versions"
|
||||
|
||||
if [[ $purge -eq 1 ]]; then
|
||||
if [[ $config_dir == "$state_dir" ]]; then
|
||||
rm -rf -- "$config_dir"
|
||||
else
|
||||
rm -rf -- "$config_dir" "$state_dir"
|
||||
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"
|
||||
systemctl_for "$service_scope" daemon-reload || warn "systemd daemon-reload failed"
|
||||
fi
|
||||
|
||||
[[ -z $launcher_path ]] || rm -f -- "$launcher_path"
|
||||
rm -f -- "$record" "$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
|
||||
Reference in new issue
Block a user