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

2137 lines
76 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: install.sh [OPTIONS]
Install an Echoion release without starting it by default.
Options:
--mode MODE system, user, or portable (default: user)
--prefix PATH installation root
--data-layout LAYOUT self-contained or standard (default: self-contained)
--config-dir PATH override the configuration directory
--state-dir PATH override the writable state directory
--register-service SCOPE none, user, or system
--start enable and start the registered service
--non-interactive accept defaults instead of prompting
-h, --help show this help
System and user installs register a matching service by default. Portable
installs do not register a service unless explicitly requested. Registration
alone leaves the service disabled and stopped; only --start activates it.
EOF
}
require_value() {
[[ $# -ge 2 && -n $2 ]] || die "$1 requires a value"
}
validate_choice() {
local value=$1
local label=$2
shift 2
local candidate
for candidate in "$@"; do
[[ $value == "$candidate" ]] && return 0
done
die "invalid $label '$value' (expected: $*)"
}
reject_control_characters() {
local label=$1
local value=$2
[[ ! $value =~ [[:cntrl:]] ]] ||
die "$label contains an unsupported control character"
}
absolute_path() {
local value=$1
realpath -m -- "$value"
}
prompt_value() {
local variable=$1
local label=$2
local default_value=$3
local answer
printf '%s [%s]: ' "$label" "$default_value" >&2
IFS= read -r answer || true
printf -v "$variable" '%s' "${answer:-$default_value}"
}
prompt_yes_no() {
local variable=$1
local label=$2
local answer
printf '%s [y/N]: ' "$label" >&2
IFS= read -r answer || true
case $answer in
y | Y | yes | YES | Yes) printf -v "$variable" '%s' 1 ;;
*) printf -v "$variable" '%s' 0 ;;
esac
}
manifest_version() {
local manifest=$1
local line
while IFS= read -r line; do
if [[ $line =~ \"version\"[[:space:]]*:[[:space:]]*\"([^\"]+)\" ]]; then
printf '%s\n' "${BASH_REMATCH[1]}"
return 0
fi
done < "$manifest"
return 1
}
manifest_is_v2_with_files() {
local manifest=$1
local line
local schema_ok=0
local files_ok=0
while IFS= read -r line; do
if [[ $line =~ \"schemaVersion\"[[:space:]]*:[[:space:]]*2([,[:space:]]|$) ]]; then
schema_ok=1
fi
if [[ $line =~ \"path\"[[:space:]]*:[[:space:]]*\"[^\"]+\" ]]; then
files_ok=1
fi
done < "$manifest"
[[ $schema_ok -eq 1 && $files_ok -eq 1 ]]
}
sha256_file() {
local result
result=$(sha256sum -- "$1")
printf '%s\n' "${result%% *}"
}
json_escape() {
local value=$1
value=${value//\\/\\\\}
value=${value//$'\n'/\\n}
value=${value//$'\r'/\\r}
value=${value//$'\t'/\\t}
value=${value//\"/\\\"}
printf '%s' "$value"
}
systemd_setting_escape() {
local value=$1
value=${value//\\/\\\\}
value=${value//$'\n'/\\n}
value=${value//$'\r'/\\r}
value=${value//$'\t'/\\t}
value=${value//\"/\\\"}
value=${value//%/%%}
printf '%s' "$value"
}
systemd_path_escape() {
local value=$1
value=${value//\\/\\x5c}
value=${value//$'\n'/\\x0a}
value=${value//$'\r'/\\x0d}
value=${value//$'\t'/\\x09}
value=${value// /\\x20}
value=${value//\"/\\x22}
value=${value//%/%%}
printf '%s' "$value"
}
systemd_exec_escape() {
local value
value=$(systemd_setting_escape "$1")
value=${value//\$/\$\$}
printf '%s' "$value"
}
render_config() {
local source=$1
local destination=$2
local web_root=$3
local escaped
local temporary
local line
local found=0
escaped=$(json_escape "$web_root")
temporary=$(mktemp --tmpdir="$(dirname -- "$destination")" '.config.json.XXXXXX')
while IFS= read -r line || [[ -n $line ]]; do
if [[ $line =~ ^([[:space:]]*)\"webRoot\"[[:space:]]*: ]]; then
printf '%s"webRoot": "%s",\n' "${BASH_REMATCH[1]}" "$escaped" >> "$temporary"
found=$((found + 1))
else
printf '%s\n' "$line" >> "$temporary"
fi
done < "$source"
if [[ $found -ne 1 ]]; then
rm -f -- "$temporary"
die "configuration template has no unique webRoot field"
fi
chmod 0600 "$temporary"
mv -f -- "$temporary" "$destination"
}
legacy_web_root_count() {
local source=$1
local line
local count=0
while IFS= read -r line || [[ -n $line ]]; do
if [[ $line =~ ^[[:space:]]*\"webRoot\"[[:space:]]*:[[:space:]]*\"\./web\"[[:space:]]*,?[[:space:]]*$ ]]; then
count=$((count + 1))
fi
done < "$source"
printf '%s\n' "$count"
}
migrate_legacy_web_root() {
local source=$1
local line
local matches=0
CONFIG_MIGRATION_TEMPORARY=$(mktemp \
--tmpdir="$(dirname -- "$source")" '.config-migration.XXXXXX')
while IFS= read -r line || [[ -n $line ]]; do
if [[ $line =~ ^([[:space:]]*)\"webRoot\"[[:space:]]*:[[:space:]]*\"\./web\"([[:space:]]*,?[[:space:]]*)$ ]]; then
printf '%s"webRoot": "./share/echoion/web"%s\n' \
"${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" \
>> "$CONFIG_MIGRATION_TEMPORARY"
matches=$((matches + 1))
else
printf '%s\n' "$line" >> "$CONFIG_MIGRATION_TEMPORARY"
fi
done < "$source"
[[ $matches -eq 1 ]] || die "legacy webRoot migration is no longer safe"
chmod --reference="$source" "$CONFIG_MIGRATION_TEMPORARY"
mv -f -- "$CONFIG_MIGRATION_TEMPORARY" "$source"
CONFIG_MIGRATION_TEMPORARY=
}
effective_uid() {
printf '%s\n' "${EUID:-$(id -u)}"
}
validate_registration_identity() {
local scope=$1
local uid=$2
if [[ $scope == user && $uid -eq 0 ]]; then
die "refusing to register a root user service; use system scope instead"
fi
}
validate_trusted_path_ancestors() {
local scope=$1
local path=$2
local service_uid=$3
local require_leaf_owner=${4:-0}
local current owner mode root_owner
root_owner=$(stat -c '%u' -- /)
current=$path
while [[ ! -e $current ]]; do
[[ $current != / ]] || break
current=$(dirname -- "$current")
done
while :; do
[[ -d $current && ! -L $current ]] ||
die "service path component is not a real directory: '$current'"
owner=$(stat -c '%u' -- "$current")
mode=$(stat -c '%a' -- "$current")
if (( (8#$mode & 0022) != 0 )); then
die "service path component is group/world-writable: '$current'"
fi
if [[ $scope == system ]]; then
[[ $owner -eq $root_owner ]] ||
die "system service path is not owned by root: '$current'"
elif [[ $owner -ne $root_owner && $owner -ne $service_uid ]]; then
die "user service path has an untrusted owner: '$current'"
fi
[[ $current != / ]] || break
current=$(dirname -- "$current")
done
if [[ $require_leaf_owner -eq 1 ]]; then
owner=$(stat -c '%u' -- "$path")
if [[ $scope == system ]]; then
[[ $owner -eq $root_owner ]] || die "system service prefix is not owned by root: '$path'"
else
[[ $owner -eq $service_uid ]] ||
die "user service prefix is not owned by uid $service_uid: '$path'"
fi
fi
}
acquire_prefix_lock() {
local lock_prefix=$1
command -v flock >/dev/null 2>&1 || die "flock is required for safe installation"
exec {INSTALL_LOCK_FD}< "$lock_prefix"
flock -n "$INSTALL_LOCK_FD" || die "another Echoion install or uninstall is active for '$lock_prefix'"
}
reject_temporary_prefix() {
local prefix=$1
local candidate
for candidate in /tmp /var/tmp /dev/shm /run "${TMPDIR:-}" "${XDG_RUNTIME_DIR:-}"; do
[[ -n $candidate ]] || continue
candidate=$(absolute_path "$candidate")
case $prefix in
"$candidate" | "$candidate"/*)
die "refusing to register a service from temporary path '$prefix'"
;;
esac
done
}
validate_mutable_path_ancestors() {
local scope=$1
local path=$2
local service_uid=$3
local existing=$path
if [[ -e $existing || -L $existing ]]; then
existing=$(dirname -- "$existing")
else
while [[ ! -e $existing ]]; do
[[ $existing != / ]] || break
existing=$(dirname -- "$existing")
done
fi
validate_trusted_path_ancestors "$scope" "$existing" "$service_uid" 0
}
validate_mutable_directory() {
local scope=$1
local path=$2
local expected_uid=$3
local service_uid=$4
local owner mode
[[ -d $path && ! -L $path ]] || die "mutable service path is not a real directory: '$path'"
owner=$(stat -c '%u' -- "$path")
mode=$(stat -c '%a' -- "$path")
[[ $owner -eq $expected_uid ]] || die "mutable service path has the wrong owner: '$path'"
(( (8#$mode & 0022) == 0 )) ||
die "mutable service path is group/world-writable: '$path'"
validate_trusted_path_ancestors "$scope" "$(dirname -- "$path")" "$service_uid" 0
}
systemctl_for() {
local scope=$1
shift
if [[ $scope == user ]]; then
systemctl --user "$@"
else
systemctl "$@"
fi
}
safe_recorded_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
}
render_unit() {
local template=$1
local destination=$2
local scope=$3
local home=$4
local core=$5
local state=$6
local executable=$7
local config=$8
local incoming=$9
local escaped_home escaped_core escaped_library_path escaped_state
local escaped_executable escaped_config
local escaped_working_directory
local escaped_exec_state escaped_incoming
local line
escaped_home=$(systemd_setting_escape "$home")
escaped_core=$(systemd_setting_escape "$core")
escaped_library_path=$(systemd_setting_escape "$home/lib")
escaped_state=$(systemd_setting_escape "$state")
escaped_executable=$(systemd_exec_escape "$executable")
escaped_config=$(systemd_exec_escape "$config")
escaped_exec_state=$(systemd_exec_escape "$state")
escaped_incoming=$(systemd_setting_escape "$incoming")
escaped_working_directory=$(systemd_path_escape "$home")
: > "$destination"
while IFS= read -r line || [[ -n $line ]]; do
case $line in
@SERVICE_IDENTITY@)
if [[ $scope == system ]]; then
printf 'User=%s\nGroup=%s\n' "$SERVICE_USER" "$SERVICE_USER" >> "$destination"
fi
;;
@WORKING_DIRECTORY@)
printf 'WorkingDirectory=%s\n' "$escaped_working_directory" >> "$destination"
;;
@ENVIRONMENT_HOME@)
printf 'Environment="ECHOION_HOME=%s"\n' "$escaped_home" >> "$destination"
;;
@ENVIRONMENT_CORE@)
printf 'Environment="ECHOION_CORE_PATH=%s"\n' "$escaped_core" >> "$destination"
;;
@ENVIRONMENT_LIBRARY_PATH@)
printf 'Environment="LD_LIBRARY_PATH=%s"\n' \
"$escaped_library_path" >> "$destination"
;;
@ENVIRONMENT_STATE@)
printf 'Environment="ECHOION_STATE_DIR=%s"\n' "$escaped_state" >> "$destination"
;;
@ENVIRONMENT_INCOMING@)
printf 'Environment="ECHOION_INCOMING_DIR=%s"\n' "$escaped_incoming" >> "$destination"
;;
@EXEC_START@)
printf 'ExecStart="%s" --config "%s" --state-dir "%s"\n' \
"$escaped_executable" "$escaped_config" "$escaped_exec_state" >> "$destination"
;;
@INSTALL_TARGET@)
if [[ $scope == system ]]; then
printf 'WantedBy=multi-user.target\n' >> "$destination"
else
printf 'WantedBy=default.target\n' >> "$destination"
fi
;;
*) printf '%s\n' "$line" >> "$destination" ;;
esac
done < "$template"
}
render_updater_service() {
local template=$1
local destination=$2
local scope=$3
local prefix=$4
local executable=$5
local state=$6
local escaped_prefix escaped_executable escaped_working_directory line
escaped_prefix=$(systemd_exec_escape "$prefix")
escaped_executable=$(systemd_exec_escape "$executable")
escaped_working_directory=$(systemd_path_escape "$prefix")
: > "$destination"
while IFS= read -r line || [[ -n $line ]]; do
case $line in
@SERVICE_IDENTITY@)
# System scope intentionally runs the fixed-operation helper as root.
;;
@UPDATER_WORKING_DIRECTORY@)
printf 'WorkingDirectory=%s\n' "$escaped_working_directory" >> "$destination"
;;
@UPDATER_EXEC_START@)
printf 'ExecStart="%s" --root "%s" consume\n' \
"$escaped_executable" "$escaped_prefix" >> "$destination"
;;
@UPDATER_HARDENING@)
if [[ $scope == system ]]; then
printf 'ProtectSystem=strict\nProtectHome=read-only\n' >> "$destination"
printf 'ReadWritePaths=%s %s\n' \
"$(systemd_path_escape "$prefix")" \
"$(systemd_path_escape "$state")" >> "$destination"
fi
;;
*) printf '%s\n' "$line" >> "$destination" ;;
esac
done < "$template"
}
render_updater_timer() {
local template=$1
local destination=$2
local scope=$3
local line
: > "$destination"
while IFS= read -r line || [[ -n $line ]]; do
case $line in
@INSTALL_TARGET@)
if [[ $scope == system ]]; then
printf 'WantedBy=timers.target\n' >> "$destination"
else
printf 'WantedBy=timers.target\n' >> "$destination"
fi
;;
*) printf '%s\n' "$line" >> "$destination" ;;
esac
done < "$template"
}
render_portable_launcher() {
local destination=$1
cat > "$destination" <<'EOF'
#!/usr/bin/env bash
# Echoion portable launcher
set -euo pipefail
die() {
printf 'echoion: %s\n' "$*" >&2
exit 1
}
launcher=$(realpath -e -- "${BASH_SOURCE[0]}") || die 'cannot resolve launcher path'
prefix=$(cd -P -- "$(dirname -- "$launcher")" && pwd -P) ||
die 'cannot resolve portable prefix'
[[ $prefix != / ]] || die 'refusing to run from the filesystem root'
current=$prefix/current
versions=$prefix/versions
[[ -d $versions && ! -L $versions ]] ||
die "versions directory is missing or unsafe: $versions"
[[ -L $current ]] || die "current release link is missing: $current"
target=$(readlink -- "$current")
[[ $target =~ ^versions/[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]] ||
die "unsafe current release target: $target"
daemon=$current/bin/echoiond
config=$prefix/config/config.json
state=$prefix/state
core=$current/lib/Echoion.ts
[[ -x $daemon ]] || die "daemon is missing or not executable: $daemon"
[[ -f $config && ! -L $config ]] || die "configuration is missing or unsafe: $config"
[[ -d $state && ! -L $state ]] || die "state directory is missing or unsafe: $state"
[[ -f $core ]] || die "Core library is missing: $core"
for argument in "$@"; do
case $argument in
--config | --config=* | --state-dir | --state-dir=*)
die "$argument is managed by the portable launcher"
;;
esac
done
cd -- "$current"
export ECHOION_HOME=$current
export ECHOION_CORE_PATH=$core
export ECHOION_STATE_DIR=$state
export ECHOION_INCOMING_DIR=$prefix/incoming
export LD_LIBRARY_PATH="$current/lib${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
exec "$daemon" --config "$config" --state-dir "$state" "$@"
EOF
}
valid_plugin_component() {
local value=$1
[[ $value =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]]
}
valid_plugin_version() {
local value=$1
local prerelease identifier
if [[ ! $value =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?$ ]]; then
return 1
fi
prerelease=${BASH_REMATCH[5]:-}
while [[ -n $prerelease ]]; do
if [[ $prerelease == *.* ]]; then
identifier=${prerelease%%.*}
prerelease=${prerelease#*.}
else
identifier=$prerelease
prerelease=
fi
[[ ! $identifier =~ ^0[0-9]+$ ]] || return 1
done
return 0
}
rollback_bootstrap_plugins() {
local index path
for ((index = ${#PLUGIN_BOOTSTRAP_CREATED_PATHS[@]} - 1;
index >= 0; --index)); do
path=${PLUGIN_BOOTSTRAP_CREATED_PATHS[index]}
case $path in
"$state_dir/plugins" | "$state_dir/plugins"/* | "$config_dir/plugins.d") ;;
*)
warn "refusing to roll back unexpected plugin path '$path'"
continue
;;
esac
if [[ -d $path && ! -L $path ]]; then
rmdir -- "$path" 2>/dev/null || true
else
rm -f -- "$path"
fi
done
PLUGIN_BOOTSTRAP_CREATED_PATHS=()
}
bootstrap_plugins() {
local source_root=$1
local managed_root=$state_dir/plugins/.managed
local plugin_config_root=$config_dir/plugins.d
local directory plugin_directory version_directory library destination
local versions_root staging_root
local plugin_id plugin_version current_target current_version
local -a plugin_directories version_directories entries
[[ -e $source_root || -L $source_root ]] || return 0
[[ -d $source_root && ! -L $source_root ]] ||
die "bootstrap plugin root is not a real directory: '$source_root'"
mapfile -d '' -t plugin_directories < <(
find "$source_root" -mindepth 1 -maxdepth 1 -print0 | LC_ALL=C sort -z
)
for plugin_directory in "${plugin_directories[@]}"; do
[[ -d $plugin_directory && ! -L $plugin_directory ]] ||
die "bootstrap plugin entry is not a real directory: '$plugin_directory'"
plugin_id=${plugin_directory##*/}
valid_plugin_component "$plugin_id" ||
die "bootstrap plugin has an unsafe identifier: '$plugin_id'"
mapfile -d '' -t version_directories < <(
find "$plugin_directory" -mindepth 1 -maxdepth 1 -print0 |
LC_ALL=C sort -z
)
[[ ${#version_directories[@]} -eq 1 ]] ||
die "bootstrap plugin '$plugin_id' must contain exactly one version"
version_directory=${version_directories[0]}
[[ -d $version_directory && ! -L $version_directory ]] ||
die "bootstrap plugin version is not a real directory: '$version_directory'"
plugin_version=${version_directory##*/}
if ! valid_plugin_component "$plugin_version" ||
! valid_plugin_version "$plugin_version"; then
die "bootstrap plugin '$plugin_id' has an unsafe version"
fi
library=$version_directory/$plugin_id.so
[[ -f $library && ! -L $library ]] ||
die "bootstrap plugin library is missing or unsafe: '$library'"
mapfile -d '' -t entries < <(
find "$version_directory" -mindepth 1 -maxdepth 1 -print0 |
LC_ALL=C sort -z
)
[[ ${#entries[@]} -eq 1 && ${entries[0]} == "$library" ]] ||
die "bootstrap plugin '$plugin_id' version '$plugin_version' has unexpected files"
done
for directory in "$state_dir/plugins" "$managed_root" "$plugin_config_root"; do
if [[ -e $directory || -L $directory ]]; then
[[ -d $directory && ! -L $directory ]] ||
die "plugin path is not a real directory: '$directory'"
else
install -d -m 0700 -- "$directory"
PLUGIN_BOOTSTRAP_CREATED_PATHS+=("$directory")
fi
done
for plugin_directory in "${plugin_directories[@]}"; do
plugin_id=${plugin_directory##*/}
mapfile -d '' -t version_directories < <(
find "$plugin_directory" -mindepth 1 -maxdepth 1 -print0 |
LC_ALL=C sort -z
)
version_directory=${version_directories[0]}
plugin_version=${version_directory##*/}
library=$version_directory/$plugin_id.so
destination=$managed_root/$plugin_id
if [[ -e $destination || -L $destination ]]; then
[[ -d $destination && ! -L $destination ]] ||
die "managed plugin path is not a real directory: '$destination'"
else
install -d -m 0700 -- "$destination"
PLUGIN_BOOTSTRAP_CREATED_PATHS+=("$destination")
fi
versions_root=$destination/versions
staging_root=$destination/staging
for directory in "$versions_root" "$staging_root"; do
if [[ -e $directory || -L $directory ]]; then
[[ -d $directory && ! -L $directory ]] ||
die "managed plugin store path is unsafe: '$directory'"
else
install -d -m 0700 -- "$directory"
PLUGIN_BOOTSTRAP_CREATED_PATHS+=("$directory")
fi
done
if [[ -e $versions_root/$plugin_version || -L $versions_root/$plugin_version ]]; then
[[ -d $versions_root/$plugin_version &&
! -L $versions_root/$plugin_version &&
-f $versions_root/$plugin_version/$plugin_id.so &&
! -L $versions_root/$plugin_version/$plugin_id.so ]] ||
die "managed plugin version is incomplete: '$versions_root/$plugin_version'"
cmp -s -- "$library" "$versions_root/$plugin_version/$plugin_id.so" ||
die "managed plugin '$plugin_id' version '$plugin_version' has different bytes"
else
PLUGIN_BOOTSTRAP_TEMPORARY=$(mktemp -d \
--tmpdir="$versions_root" ".bootstrap-$plugin_version.XXXXXX")
install -m 0644 -- "$library" \
"$PLUGIN_BOOTSTRAP_TEMPORARY/$plugin_id.so"
mv -- "$PLUGIN_BOOTSTRAP_TEMPORARY" "$versions_root/$plugin_version"
PLUGIN_BOOTSTRAP_TEMPORARY=
PLUGIN_BOOTSTRAP_CREATED_PATHS+=("$versions_root/$plugin_version")
PLUGIN_BOOTSTRAP_CREATED_PATHS+=(
"$versions_root/$plugin_version/$plugin_id.so")
fi
if [[ -L $destination/current ]]; then
current_target=$(readlink -- "$destination/current")
[[ $current_target =~ ^versions/([^/]+)$ ]] ||
die "managed plugin '$plugin_id' has an unsafe current target"
current_version=${BASH_REMATCH[1]}
if ! valid_plugin_component "$current_version" ||
! valid_plugin_version "$current_version"; then
die "managed plugin '$plugin_id' current version is invalid"
fi
[[ -f $destination/$current_target/$plugin_id.so &&
! -L $destination/$current_target/$plugin_id.so ]] ||
die "managed plugin '$plugin_id' current target is incomplete"
elif [[ -e $destination/current ]]; then
die "managed plugin '$plugin_id' current path is not a symlink"
else
PLUGIN_BOOTSTRAP_TEMPORARY=$destination/.current.$$.tmp
ln -s -- "versions/$plugin_version" "$PLUGIN_BOOTSTRAP_TEMPORARY"
mv -T -- "$PLUGIN_BOOTSTRAP_TEMPORARY" "$destination/current"
PLUGIN_BOOTSTRAP_TEMPORARY=
PLUGIN_BOOTSTRAP_CREATED_PATHS+=("$destination/current")
fi
done
if [[ $mode == system || $register_service == system ]]; then
for directory in "${PLUGIN_BOOTSTRAP_CREATED_PATHS[@]}"; do
if [[ $directory == "$plugin_config_root" ]]; then
chown "root:$SERVICE_USER" -- "$directory"
chmod 0750 "$directory"
else
chown -h "$SERVICE_USER:$SERVICE_USER" -- "$directory"
fi
done
fi
}
load_old_record() {
local record=$1
local key value
OLD_PREFIX=
OLD_SERVICE_SCOPE=none
OLD_SERVICE_UNIT=
OLD_SERVICE_SHA256=
OLD_UPDATER_SERVICE_UNIT=
OLD_UPDATER_SERVICE_SHA256=
OLD_UPDATER_TIMER_UNIT=
OLD_UPDATER_TIMER_SHA256=
OLD_UPDATER_HELPER=
OLD_UPDATER_HELPER_SHA256=
OLD_CREATED_SYSTEM_USER=0
OLD_SERVICE_UID=
OLD_SERVICE_GID=
OLD_INSTALL_ID=
OLD_CONFIG_PURGE_ALLOWED=0
OLD_CONFIG_DIR_DEVICE=
OLD_CONFIG_DIR_INODE=
OLD_CONFIG_DIR_UID=
OLD_CONFIG_DIR_GID=
OLD_CONFIG_MARKER_SHA256=
OLD_STATE_PURGE_ALLOWED=0
OLD_STATE_DIR_DEVICE=
OLD_STATE_DIR_INODE=
OLD_STATE_DIR_UID=
OLD_STATE_DIR_GID=
OLD_STATE_MARKER_SHA256=
OLD_RELOCATABLE=0
OLD_LAUNCHER=
OLD_LAUNCHER_SHA256=
[[ -f $record ]] || return 0
while IFS='=' read -r key value; do
case $key in
prefix) OLD_PREFIX=$value ;;
service_scope) OLD_SERVICE_SCOPE=$value ;;
service_unit) OLD_SERVICE_UNIT=$value ;;
service_unit_sha256) OLD_SERVICE_SHA256=$value ;;
updater_service_unit) OLD_UPDATER_SERVICE_UNIT=$value ;;
updater_service_unit_sha256) OLD_UPDATER_SERVICE_SHA256=$value ;;
updater_timer_unit) OLD_UPDATER_TIMER_UNIT=$value ;;
updater_timer_unit_sha256) OLD_UPDATER_TIMER_SHA256=$value ;;
updater_helper) OLD_UPDATER_HELPER=$value ;;
updater_helper_sha256) OLD_UPDATER_HELPER_SHA256=$value ;;
created_system_user) OLD_CREATED_SYSTEM_USER=$value ;;
service_uid) OLD_SERVICE_UID=$value ;;
service_gid) OLD_SERVICE_GID=$value ;;
install_id) OLD_INSTALL_ID=$value ;;
config_purge_allowed) OLD_CONFIG_PURGE_ALLOWED=$value ;;
config_dir_device) OLD_CONFIG_DIR_DEVICE=$value ;;
config_dir_inode) OLD_CONFIG_DIR_INODE=$value ;;
config_dir_uid) OLD_CONFIG_DIR_UID=$value ;;
config_dir_gid) OLD_CONFIG_DIR_GID=$value ;;
config_marker_sha256) OLD_CONFIG_MARKER_SHA256=$value ;;
state_purge_allowed) OLD_STATE_PURGE_ALLOWED=$value ;;
state_dir_device) OLD_STATE_DIR_DEVICE=$value ;;
state_dir_inode) OLD_STATE_DIR_INODE=$value ;;
state_dir_uid) OLD_STATE_DIR_UID=$value ;;
state_dir_gid) OLD_STATE_DIR_GID=$value ;;
state_marker_sha256) OLD_STATE_MARKER_SHA256=$value ;;
relocatable) OLD_RELOCATABLE=$value ;;
launcher) OLD_LAUNCHER=$value ;;
launcher_sha256) OLD_LAUNCHER_SHA256=$value ;;
esac
done < "$record"
case $OLD_RELOCATABLE in
0)
[[ $OLD_PREFIX == "$prefix" ]] ||
die "install record belongs to '$OLD_PREFIX', not '$prefix'"
;;
1)
[[ $OLD_PREFIX == . ]] || die "relocatable install record has an unsafe prefix"
;;
*) die "install record has an invalid relocatable value" ;;
esac
}
verify_recorded_unit() {
local scope=$1
local unit=$2
local expected_hash=$3
[[ -n $unit && -n $expected_hash ]] || die "existing service record is incomplete"
safe_recorded_unit_path "$scope" "$unit" || die "existing service record has an unsafe unit path"
[[ -f $unit ]] || die "recorded service unit is missing: $unit"
[[ $(sha256_file "$unit") == "$expected_hash" ]] ||
die "recorded service unit was modified; refusing to overwrite it: $unit"
}
verify_recorded_launcher() {
local launcher=$1
local expected_hash=$2
local path
[[ $launcher == echoion && $expected_hash =~ ^[0-9a-f]{64}$ ]] ||
die "existing portable launcher record is incomplete or unsafe"
path=$prefix/$launcher
[[ -f $path && ! -L $path && -x $path ]] ||
die "recorded portable launcher is missing or unsafe: $path"
[[ $(sha256_file "$path") == "$expected_hash" ]] ||
die "recorded portable launcher was modified; refusing to overwrite it: $path"
}
verify_recorded_updater_helper() {
local helper=$1
local expected_hash=$2
local path
[[ $helper =~ ^libexec/updater/[A-Za-z0-9][A-Za-z0-9._-]{0,63}/bin/echoion-updater$ &&
$expected_hash =~ ^[0-9a-f]{64}$ ]] ||
die "existing privileged updater helper record is incomplete or unsafe"
path=$prefix/$helper
[[ -f $path && ! -L $path && -x $path ]] ||
die "recorded privileged updater helper is missing or unsafe: $path"
[[ $(sha256_file "$path") == "$expected_hash" ]] ||
die "recorded privileged updater helper was modified; refusing to overwrite it: $path"
}
remove_recorded_service() {
local scope=$1
local unit=$2
local expected_hash=$3
local updater_service=${4:-}
local updater_service_hash=${5:-}
local updater_timer=${6:-}
local updater_timer_hash=${7:-}
local -a service_names=("$SERVICE_NAME")
verify_recorded_unit "$scope" "$unit" "$expected_hash"
if [[ -n $updater_service || -n $updater_service_hash ]]; then
verify_recorded_unit "$scope" "$updater_service" "$updater_service_hash"
fi
if [[ -n $updater_timer || -n $updater_timer_hash ]]; then
verify_recorded_unit "$scope" "$updater_timer" "$updater_timer_hash"
fi
if [[ -n $updater_service ]]; then
systemctl_for "$scope" stop "$UPDATER_SERVICE_NAME" || return 1
fi
[[ -z $updater_timer ]] || service_names+=("$UPDATER_TIMER_NAME")
systemctl_for "$scope" disable --now "${service_names[@]}" || return 1
rm -f -- "$unit" || return 1
[[ -z $updater_service ]] || rm -f -- "$updater_service" || return 1
[[ -z $updater_timer ]] || rm -f -- "$updater_timer" || return 1
systemctl_for "$scope" daemon-reload || return 1
}
ensure_system_user() {
local state=$1
local passwd_entry shell group_gid
if id -u "$SERVICE_USER" >/dev/null 2>&1; then
SERVICE_UID=$(id -u "$SERVICE_USER")
SERVICE_GID=$(id -g "$SERVICE_USER")
passwd_entry=$(getent passwd "$SERVICE_USER") ||
die "could not inspect existing $SERVICE_USER account"
IFS=: read -r _ _ _ _ _ _ shell <<< "$passwd_entry"
case ${shell##*/} in
nologin | false) ;;
*) die "existing $SERVICE_USER account does not use a non-login shell" ;;
esac
group_gid=$(getent group "$SERVICE_USER" | cut -d: -f3) || true
[[ -n $group_gid && $group_gid -eq $SERVICE_GID ]] ||
die "existing $SERVICE_USER account has no matching primary group"
if [[ ${OLD_CREATED_SYSTEM_USER:-0} == 1 ]]; then
[[ ${OLD_SERVICE_UID:-} =~ ^[0-9]+$ && ${OLD_SERVICE_GID:-} =~ ^[0-9]+$ ]] ||
die "installer-created service account record has no uid/gid identity"
[[ $SERVICE_UID -eq $OLD_SERVICE_UID && $SERVICE_GID -eq $OLD_SERVICE_GID ]] ||
die "the recorded $SERVICE_USER account was deleted or replaced"
fi
return 0
fi
local nologin=/usr/sbin/nologin
[[ -x $nologin ]] || nologin=/sbin/nologin
useradd --system --user-group --home-dir "$state" --shell "$nologin" \
--no-create-home "$SERVICE_USER"
CREATED_SYSTEM_USER=1
ACCOUNT_CREATED_THIS_RUN=1
SERVICE_UID=$(id -u "$SERVICE_USER")
SERVICE_GID=$(id -g "$SERVICE_USER")
ACCOUNT_IDENTITY_CAPTURED=1
}
remove_new_system_user() {
[[ ${ACCOUNT_CREATED_THIS_RUN:-0} -eq 1 ]] || return 0
if [[ ${ACCOUNT_IDENTITY_CAPTURED:-0} -eq 0 ]]; then
userdel "$SERVICE_USER" || warn "could not remove newly created $SERVICE_USER account"
elif id -u "$SERVICE_USER" >/dev/null 2>&1 &&
[[ $(id -u "$SERVICE_USER") -eq ${SERVICE_UID:-999999} ]] &&
[[ $(id -g "$SERVICE_USER") -eq ${SERVICE_GID:-999999} ]]; then
userdel "$SERVICE_USER" || warn "could not remove newly created $SERVICE_USER account"
else
warn "new $SERVICE_USER account identity changed; refusing automatic deletion"
fi
}
validate_service_access() {
local scope=$1
local executable=$2
local config=$3
local state=$4
if [[ $scope == user ]]; then
[[ -x $executable ]] || die "service user cannot execute '$executable'"
[[ -r $config ]] || die "service user cannot read '$config'"
[[ -w $state ]] || die "service user cannot write '$state'"
return 0
fi
command -v runuser >/dev/null 2>&1 ||
die "runuser is required to validate the system service account"
runuser -u "$SERVICE_USER" -- test -x "$executable" ||
die "$SERVICE_USER cannot traverse the prefix or execute echoiond"
runuser -u "$SERVICE_USER" -- test -r "$config" ||
die "$SERVICE_USER cannot read '$config'"
runuser -u "$SERVICE_USER" -- test -w "$state" ||
die "$SERVICE_USER cannot write '$state'"
}
validate_privileged_updater_helper() {
local scope=$1
local helper=$2
local service_uid=$3
local owner mode root_owner
[[ -f $helper && ! -L $helper && -x $helper ]] ||
die "privileged updater helper is missing or unsafe: '$helper'"
owner=$(stat -c '%u' -- "$helper")
mode=$(stat -c '%a' -- "$helper")
(( (8#$mode & 0022) == 0 )) ||
die "privileged updater helper is group/world-writable: '$helper'"
root_owner=$(stat -c '%u' -- /)
if [[ $scope == system ]]; then
[[ $owner -eq $root_owner ]] ||
die "privileged updater helper is not root-owned: '$helper'"
validate_trusted_path_ancestors system "$(dirname -- "$helper")" "$service_uid" 1
if runuser -u "$SERVICE_USER" -- test -w "$helper" ||
runuser -u "$SERVICE_USER" -- test -w "$(dirname -- "$helper")"; then
die "$SERVICE_USER can replace the privileged updater helper"
fi
elif [[ $scope == user ]]; then
[[ $owner -eq $service_uid ]] ||
die "user updater helper is not owned by uid $service_uid: '$helper'"
validate_trusted_path_ancestors user "$(dirname -- "$helper")" "$service_uid" 1
fi
}
normalize_and_validate_system_runtime() {
local root=$1
local foreign writable path
chown -R root:root -- "$root"
foreign=$(find "$root" \( ! -uid 0 -o ! -gid 0 \) -print -quit)
[[ -z $foreign ]] || die "immutable runtime entry is not root-owned: '$foreign'"
foreign=$(find "$root" ! -type l -perm /022 -print -quit)
[[ -z $foreign ]] || die "immutable runtime entry is group/world-writable: '$foreign'"
writable=$(runuser -u "$SERVICE_USER" -- find "$root" -writable -print -quit) ||
die "could not verify immutable runtime permissions as $SERVICE_USER"
[[ -z $writable ]] || die "$SERVICE_USER can modify immutable runtime entry: '$writable'"
for path in "$root" "$(dirname -- "$root")" "$(dirname -- "$(dirname -- "$root")")"; do
if runuser -u "$SERVICE_USER" -- test -w "$path"; then
die "$SERVICE_USER can replace immutable runtime path: '$path'"
fi
done
}
new_install_id() {
local value
if [[ -r /proc/sys/kernel/random/uuid ]]; then
IFS= read -r value < /proc/sys/kernel/random/uuid
else
value=$(printf '%s:%s:%s:%s' "$prefix" "$$" "$RANDOM" "$(date +%s%N)" |
sha256sum)
value=${value%% *}
fi
printf '%s\n' "$value"
}
write_managed_marker() {
local directory=$1
local marker=$directory/$MANAGED_MARKER
local temporary
temporary=$(mktemp --tmpdir="$directory" '.echoion-managed.XXXXXX')
{
printf 'format=1\n'
printf 'install_id=%s\n' "$INSTALL_ID"
printf 'relocatable=%s\n' "$RELOCATABLE"
printf 'prefix=%s\n' "$INSTALL_RECORD_PREFIX"
} > "$temporary"
chmod 0600 "$temporary"
mv -f -- "$temporary" "$marker"
}
marker_matches() {
local directory=$1
local expected_hash=$2
local marker=$directory/$MANAGED_MARKER
[[ -f $marker && ! -L $marker && -n $expected_hash ]] || return 1
[[ $(sha256_file "$marker") == "$expected_hash" ]] || return 1
grep -Fx -- "install_id=$INSTALL_ID" "$marker" >/dev/null || return 1
grep -Fx -- "prefix=$INSTALL_RECORD_PREFIX" "$marker" >/dev/null
}
capture_managed_directory() {
local kind=$1
local directory=$2
local old_allowed=$3
local old_device=$4
local old_inode=$5
local old_uid=$6
local old_gid=$7
local old_marker_hash=$8
local created=$9
local allowed=0 marker_hash='' device inode uid gid
device=$(stat -c '%d' -- "$directory")
inode=$(stat -c '%i' -- "$directory")
uid=$(stat -c '%u' -- "$directory")
gid=$(stat -c '%g' -- "$directory")
if [[ $old_allowed == 1 && $device == "$old_device" && $inode == "$old_inode" &&
$uid == "$old_uid" && $gid == "$old_gid" ]] && marker_matches "$directory" "$old_marker_hash"; then
allowed=1
marker_hash=$old_marker_hash
elif [[ $created -eq 1 ]]; then
write_managed_marker "$directory"
allowed=1
marker_hash=$(sha256_file "$directory/$MANAGED_MARKER")
elif [[ $old_allowed == 1 ]]; then
warn "$kind directory identity changed; disabling automatic purge for '$directory'"
fi
printf -v "${kind^^}_PURGE_ALLOWED" '%s' "$allowed"
printf -v "${kind^^}_DIR_DEVICE" '%s' "$device"
printf -v "${kind^^}_DIR_INODE" '%s' "$inode"
printf -v "${kind^^}_DIR_UID" '%s' "$uid"
printf -v "${kind^^}_DIR_GID" '%s' "$gid"
printf -v "${kind^^}_MARKER_SHA256" '%s' "$marker_hash"
}
snapshot_path_metadata() {
local path=$1
[[ -e $path && ! -L $path ]] || return 0
stat -c '%d:%i:%u:%g:%a' -- "$path"
}
restore_path_metadata() {
local path=$1
local snapshot=$2
local expected_device expected_inode uid gid mode current_device current_inode
[[ -n $snapshot ]] || return 0
IFS=: read -r expected_device expected_inode uid gid mode <<< "$snapshot"
[[ -e $path && ! -L $path ]] || return 1
current_device=$(stat -c '%d' -- "$path") || return 1
current_inode=$(stat -c '%i' -- "$path") || return 1
[[ $current_device == "$expected_device" && $current_inode == "$expected_inode" ]] || return 1
chown "$uid:$gid" -- "$path" || return 1
chmod "$mode" -- "$path"
}
write_record() {
local destination=$1
local temporary
local unit=$2
local unit_hash=$3
temporary=$(mktemp --tmpdir="$(dirname -- "$destination")" '.echoion-install.XXXXXX')
RECORD_TEMPORARY=$temporary
umask 077
{
printf 'format=1\n'
printf 'mode=%s\n' "$mode"
printf 'relocatable=%s\n' "$RELOCATABLE"
printf 'prefix=%s\n' "$INSTALL_RECORD_PREFIX"
printf 'version=%s\n' "$version"
printf 'data_layout=%s\n' "$data_layout"
printf 'config_dir=%s\n' "$INSTALL_RECORD_CONFIG_DIR"
printf 'state_dir=%s\n' "$INSTALL_RECORD_STATE_DIR"
printf 'cache_dir=%s\n' "$INSTALL_RECORD_CACHE_DIR"
printf 'logs_dir=%s\n' "$INSTALL_RECORD_LOGS_DIR"
printf 'incoming_dir=%s\n' "$INSTALL_RECORD_INCOMING_DIR"
printf 'config_created=%s\n' "$CONFIG_CREATED"
printf 'service_scope=%s\n' "$register_service"
printf 'service_name=%s\n' "$SERVICE_NAME"
printf 'service_unit=%s\n' "$unit"
printf 'service_unit_sha256=%s\n' "$unit_hash"
printf 'updater_service_unit=%s\n' "$updater_service_path"
printf 'updater_service_unit_sha256=%s\n' "$updater_service_hash"
printf 'updater_timer_unit=%s\n' "$updater_timer_path"
printf 'updater_timer_unit_sha256=%s\n' "$updater_timer_hash"
printf 'updater_helper=%s\n' "$UPDATER_HELPER_RECORD_PATH"
printf 'updater_helper_sha256=%s\n' "$UPDATER_HELPER_SHA256"
printf 'created_system_user=%s\n' "$CREATED_SYSTEM_USER"
printf 'service_uid=%s\n' "$SERVICE_UID"
printf 'service_gid=%s\n' "$SERVICE_GID"
printf 'install_id=%s\n' "$INSTALL_ID"
printf 'config_purge_allowed=%s\n' "$CONFIG_PURGE_ALLOWED"
printf 'config_dir_device=%s\n' "$CONFIG_DIR_DEVICE"
printf 'config_dir_inode=%s\n' "$CONFIG_DIR_INODE"
printf 'config_dir_uid=%s\n' "$CONFIG_DIR_UID"
printf 'config_dir_gid=%s\n' "$CONFIG_DIR_GID"
printf 'config_marker_sha256=%s\n' "$CONFIG_MARKER_SHA256"
printf 'state_purge_allowed=%s\n' "$STATE_PURGE_ALLOWED"
printf 'state_dir_device=%s\n' "$STATE_DIR_DEVICE"
printf 'state_dir_inode=%s\n' "$STATE_DIR_INODE"
printf 'state_dir_uid=%s\n' "$STATE_DIR_UID"
printf 'state_dir_gid=%s\n' "$STATE_DIR_GID"
printf 'state_marker_sha256=%s\n' "$STATE_MARKER_SHA256"
printf 'launcher=%s\n' "$LAUNCHER_RECORD_PATH"
printf 'launcher_sha256=%s\n' "$LAUNCHER_SHA256"
} > "$temporary"
chmod 0600 "$temporary"
mv -f -- "$temporary" "$destination"
RECORD_TEMPORARY=
}
write_json_record() {
local destination=$1
local temporary
temporary=$(mktemp --tmpdir="$(dirname -- "$destination")" '.install.json.XXXXXX')
JSON_RECORD_TEMPORARY=$temporary
umask 077
{
printf '{\n'
printf ' "schemaVersion": 1,\n'
printf ' "installId": "%s",\n' "$(json_escape "$INSTALL_ID")"
printf ' "version": "%s",\n' "$(json_escape "$version")"
printf ' "mode": "%s",\n' "$(json_escape "$mode")"
if [[ $RELOCATABLE == 1 ]]; then
printf ' "relocatable": true,\n'
else
printf ' "relocatable": false,\n'
fi
printf ' "prefix": "%s",\n' "$(json_escape "$INSTALL_RECORD_PREFIX")"
printf ' "current": "current",\n'
printf ' "versions": "versions",\n'
printf ' "configDir": "%s",\n' "$(json_escape "$INSTALL_RECORD_CONFIG_DIR")"
printf ' "stateDir": "%s",\n' "$(json_escape "$INSTALL_RECORD_STATE_DIR")"
printf ' "cacheDir": "%s",\n' "$(json_escape "$INSTALL_RECORD_CACHE_DIR")"
printf ' "logsDir": "%s",\n' "$(json_escape "$INSTALL_RECORD_LOGS_DIR")"
printf ' "incomingDir": "%s",\n' "$(json_escape "$INSTALL_RECORD_INCOMING_DIR")"
printf ' "service": {"scope": "%s", "name": "%s"},\n' \
"$(json_escape "$register_service")" "$(json_escape "$SERVICE_NAME")"
printf ' "updater": {"executable": "current/bin/echoion-updater", '
printf '"privilegedHelper": "%s", ' \
"$(json_escape "$UPDATER_HELPER_RECORD_PATH")"
printf '"timer": "echoion-updater.timer"}\n'
printf '}\n'
} > "$temporary"
chmod 0600 "$temporary"
mv -f -- "$temporary" "$destination"
JSON_RECORD_TEMPORARY=
}
if [[ ${BASH_SOURCE[0]} != "$0" ]]; then
return 0
fi
mode=user
mode_set=0
prefix=
prefix_set=0
data_layout=self-contained
data_layout_set=0
config_dir=
state_dir=
register_service=
register_service_set=0
start=0
start_set=0
non_interactive=0
while [[ $# -gt 0 ]]; do
case $1 in
--mode)
require_value "$@"
mode=$2
mode_set=1
shift 2
;;
--prefix)
require_value "$@"
prefix=$2
prefix_set=1
shift 2
;;
--data-layout)
require_value "$@"
data_layout=$2
data_layout_set=1
shift 2
;;
--config-dir)
require_value "$@"
config_dir=$2
shift 2
;;
--state-dir)
require_value "$@"
state_dir=$2
shift 2
;;
--register-service)
require_value "$@"
register_service=$2
register_service_set=1
shift 2
;;
--start)
start=1
start_set=1
shift
;;
--non-interactive)
non_interactive=1
shift
;;
-h | --help)
usage
exit 0
;;
--)
shift
[[ $# -eq 0 ]] || die "unexpected positional arguments: $*"
;;
*) die "unknown option: $1" ;;
esac
done
if [[ $non_interactive -eq 0 && -t 0 && -t 1 ]]; then
[[ $mode_set -eq 1 ]] || prompt_value mode 'Install mode (system/user/portable)' user
validate_choice "$mode" mode system user portable
case $mode in
system) default_prefix=/opt/echoion ;;
user) default_prefix=${HOME:?HOME is required}/.local/opt/echoion ;;
portable) default_prefix=$PWD/echoion ;;
esac
[[ $prefix_set -eq 1 ]] || prompt_value prefix 'Install prefix' "$default_prefix"
[[ $data_layout_set -eq 1 ]] ||
prompt_value data_layout 'Data layout (self-contained/standard)' self-contained
if [[ $register_service_set -eq 0 ]]; then
case $mode in
system) default_service=system ;;
user) default_service=user ;;
portable) default_service=none ;;
esac
prompt_value register_service 'Register service (none/user/system)' "$default_service"
fi
if [[ $register_service != none && $start_set -eq 0 ]]; then
prompt_yes_no start 'Enable and start the service now'
fi
fi
validate_choice "$mode" mode system user portable
validate_choice "$data_layout" data-layout standard self-contained
if [[ -z $register_service ]]; then
case $mode in
system) register_service=system ;;
user) register_service=user ;;
portable) register_service=none ;;
esac
fi
validate_choice "$register_service" service-scope none user system
[[ $start -eq 0 || $register_service != none ]] || die "--start requires a registered service"
INSTALL_UID=$(effective_uid)
validate_registration_identity "$register_service" "$INSTALL_UID"
case $mode in
system) default_prefix=/opt/echoion ;;
user) default_prefix=${HOME:?HOME is required}/.local/opt/echoion ;;
portable) default_prefix=$PWD/echoion ;;
esac
prefix=${prefix:-$default_prefix}
reject_control_characters prefix "$prefix"
prefix=$(absolute_path "$prefix")
[[ $prefix != / ]] || die "the filesystem root cannot be an install prefix"
if [[ $mode == system || $register_service == system ]]; then
[[ $INSTALL_UID -eq 0 ]] || die "system installation/service registration requires root"
fi
RUNTIME_TRUST_SCOPE=$register_service
if [[ $RUNTIME_TRUST_SCOPE == none && $mode == system ]]; then
RUNTIME_TRUST_SCOPE=system
fi
if [[ $RUNTIME_TRUST_SCOPE != none ]]; then
reject_temporary_prefix "$prefix"
validate_trusted_path_ancestors "$RUNTIME_TRUST_SCOPE" "$prefix" "$INSTALL_UID" 0
fi
if [[ $data_layout == self-contained ]]; then
default_config_dir=$prefix/config
default_state_dir=$prefix/state
elif [[ $mode == system || $register_service == system ]]; then
default_config_dir=/etc/echoion
default_state_dir=/var/lib/echoion
else
default_config_dir=${XDG_CONFIG_HOME:-${HOME:?HOME is required}/.config}/echoion
default_state_dir=${XDG_STATE_HOME:-${HOME:?HOME is required}/.local/state}/echoion
fi
config_dir=${config_dir:-$default_config_dir}
state_dir=${state_dir:-$default_state_dir}
reject_control_characters config-dir "$config_dir"
reject_control_characters state-dir "$state_dir"
config_dir=$(absolute_path "$config_dir")
state_dir=$(absolute_path "$state_dir")
[[ $config_dir != / && $state_dir != / ]] || die "config/state directory cannot be '/'"
for mutable_path in "$config_dir" "$state_dir"; do
case $mutable_path in
/etc | /var | /usr | /usr/local | /opt | /home | /root | /tmp | /var/tmp | /run)
die "config/state directory must be a dedicated Echoion directory: '$mutable_path'"
;;
esac
case $prefix in
"$mutable_path" | "$mutable_path"/*)
die "config/state directories cannot contain the installation prefix"
;;
esac
case $mutable_path in
"$prefix" | "$prefix/current" | "$prefix/current"/* | "$prefix/versions" | "$prefix/versions"/*)
die "config/state directories cannot be placed in the immutable runtime tree"
;;
esac
done
if [[ $RUNTIME_TRUST_SCOPE != none ]]; then
validate_mutable_path_ancestors "$RUNTIME_TRUST_SCOPE" "$config_dir" "$INSTALL_UID"
validate_mutable_path_ancestors "$RUNTIME_TRUST_SCOPE" "$state_dir" "$INSTALL_UID"
fi
config_file=$config_dir/config.json
if [[ $data_layout == self-contained ]]; then
cache_dir=$prefix/cache
logs_dir=$prefix/logs
incoming_dir=$prefix/incoming
elif [[ $mode == system || $register_service == system ]]; then
cache_dir=/var/cache/echoion
logs_dir=/var/log/echoion
incoming_dir=$state_dir/incoming
else
cache_dir=${XDG_CACHE_HOME:-${HOME:?HOME is required}/.cache}/echoion
logs_dir=$state_dir/logs
incoming_dir=$state_dir/incoming
fi
for mutable_name in cache logs incoming; do
case $mutable_name in
cache) mutable_value=$cache_dir ;;
logs) mutable_value=$logs_dir ;;
incoming) mutable_value=$incoming_dir ;;
esac
reject_control_characters "$mutable_name-dir" "$mutable_value"
mutable_value=$(absolute_path "$mutable_value")
case $mutable_name in
cache) cache_dir=$mutable_value ;;
logs) logs_dir=$mutable_value ;;
incoming) incoming_dir=$mutable_value ;;
esac
[[ $mutable_value != / ]] || die "$mutable_name directory cannot be '/'"
case $prefix in
"$mutable_value" | "$mutable_value"/*)
die "$mutable_name directory cannot contain the installation prefix"
;;
esac
case $mutable_value in
"$prefix/current" | "$prefix/current"/* | "$prefix/versions" | "$prefix/versions"/*)
die "$mutable_name directory cannot be placed in the immutable runtime tree"
;;
esac
if [[ $RUNTIME_TRUST_SCOPE != none ]]; then
validate_mutable_path_ancestors "$RUNTIME_TRUST_SCOPE" "$mutable_value" "$INSTALL_UID"
fi
done
RELOCATABLE=0
INSTALL_RECORD_PREFIX=$prefix
INSTALL_RECORD_CONFIG_DIR=$config_dir
INSTALL_RECORD_STATE_DIR=$state_dir
INSTALL_RECORD_CACHE_DIR=$cache_dir
INSTALL_RECORD_LOGS_DIR=$logs_dir
INSTALL_RECORD_INCOMING_DIR=$incoming_dir
if [[ $mode == portable && $data_layout == self-contained &&
$config_dir == "$prefix/config" && $state_dir == "$prefix/state" ]]; then
RELOCATABLE=1
INSTALL_RECORD_PREFIX=.
INSTALL_RECORD_CONFIG_DIR=config
INSTALL_RECORD_STATE_DIR=state
INSTALL_RECORD_CACHE_DIR=cache
INSTALL_RECORD_LOGS_DIR=logs
INSTALL_RECORD_INCOMING_DIR=incoming
fi
SCRIPT_DIR=$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)
PAYLOAD_ROOT=$SCRIPT_DIR
manifest=$PAYLOAD_ROOT/manifest.json
[[ -f $manifest ]] || die "payload manifest is missing: $manifest"
version=$(manifest_version "$manifest") || die "payload manifest has no version"
[[ $version =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]] || die "unsafe release version '$version'"
manifest_is_v2_with_files "$manifest" ||
die "payload manifest must use schemaVersion 2 and contain files"
[[ -x $PAYLOAD_ROOT/bin/echoiond ]] || die "payload executable is missing or not executable"
[[ -x $PAYLOAD_ROOT/bin/echoion-updater ]] ||
die "payload updater is missing or not executable"
[[ -f $PAYLOAD_ROOT/lib/Echoion.ts ]] || die "payload Core library is missing"
[[ -f $PAYLOAD_ROOT/share/echoion/config.example.json ]] || die "configuration template is missing"
[[ -d $PAYLOAD_ROOT/share/echoion/web ]] || die "web assets are missing"
[[ -f $PAYLOAD_ROOT/service/echoion.service.in ]] || die "service template is missing"
[[ -f $PAYLOAD_ROOT/service/echoion-updater.service.in ]] ||
die "updater service template is missing"
[[ -f $PAYLOAD_ROOT/service/echoion-updater.timer.in ]] ||
die "updater timer template is missing"
[[ -x $PAYLOAD_ROOT/uninstall.sh ]] || die "payload uninstaller is missing or not executable"
manifest_validator=$PAYLOAD_ROOT/share/echoion/tools/validate-release-manifest.py
[[ -f $manifest_validator && ! -L $manifest_validator ]] ||
die "payload manifest validator is missing or unsafe"
command -v python3 >/dev/null 2>&1 || die "python3 is required for payload validation"
if ! python3 "$manifest_validator" --root "$PAYLOAD_ROOT" --version "$version"; then
die "offline source payload validation failed for Echoion $version"
fi
CREATED_SYSTEM_USER=0
ACCOUNT_CREATED_THIS_RUN=0
ACCOUNT_IDENTITY_CAPTURED=0
SERVICE_UID=
SERVICE_GID=
CONFIG_CREATED=0
CONFIG_DIR_CREATED=0
STATE_DIR_CREATED=0
CACHE_DIR_CREATED=0
LOGS_DIR_CREATED=0
INCOMING_DIR_CREATED=0
CONFIG_DIR_METADATA_SNAPSHOT=
STATE_DIR_METADATA_SNAPSHOT=
CACHE_DIR_METADATA_SNAPSHOT=
LOGS_DIR_METADATA_SNAPSHOT=
INCOMING_DIR_METADATA_SNAPSHOT=
CONFIG_FILE_METADATA_SNAPSHOT=
CONFIG_MIGRATION_ARMED=0
CONFIG_MIGRATION_TEMPORARY=
CREATED_DESTINATION=
TRANSACTION_ACTIVE=0
INSTALL_COMMITTED=0
NEW_UNIT_MUTATION_ARMED=0
OLD_SERVICE_MUTATION_ARMED=0
HELPER_MUTATION_ARMED=0
LAUNCHER_MUTATION_ARMED=0
RECORD_TEMPORARY=
JSON_RECORD_TEMPORARY=
UNIT_TEMPORARY=
UPDATER_SERVICE_TEMPORARY=
UPDATER_TIMER_TEMPORARY=
INSTALL_HELPER_TEMPORARY=
UNINSTALL_HELPER_TEMPORARY=
LAUNCHER_TEMPORARY=
LAUNCHER_PATH=$prefix/echoion
LAUNCHER_RECORD_PATH=
LAUNCHER_SHA256=
UPDATER_HELPER_RECORD_PATH=libexec/updater/$version/bin/echoion-updater
UPDATER_HELPER_PATH=$prefix/$UPDATER_HELPER_RECORD_PATH
UPDATER_HELPER_SHA256=
UPDATER_BUNDLE_PATH=$prefix/libexec/updater/$version
UPDATER_BUNDLE_STAGING=
UPDATER_BUNDLE_CREATED=0
WANT_LAUNCHER=$RELOCATABLE
PLUGIN_BOOTSTRAP_CREATED_PATHS=()
PLUGIN_BOOTSTRAP_TEMPORARY=
STAGING=
TRANSACTION_BACKUP_DIR=
restore_current_link() {
local target=$1
local temporary_directory
if [[ -z $target ]]; then
rm -f -- "$prefix/current"
return
fi
temporary_directory=$(mktemp -d --tmpdir="$prefix" '.current-rollback.XXXXXX') || return
ln -s -- "$target" "$temporary_directory/current" || return
mv -Tf -- "$temporary_directory/current" "$prefix/current" || return
rmdir -- "$temporary_directory" || true
}
rollback_install() {
set +e
rollback_bootstrap_plugins
if [[ $CONFIG_MIGRATION_ARMED -eq 1 &&
-f $TRANSACTION_BACKUP_DIR/config-file ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/config-file" "$config_file"
CONFIG_FILE_METADATA_SNAPSHOT=
fi
if [[ $NEW_UNIT_MUTATION_ARMED -eq 1 ]]; then
systemctl_for "$register_service" stop "$UPDATER_SERVICE_NAME" || true
systemctl_for "$register_service" disable --now "$SERVICE_NAME" \
"$UPDATER_TIMER_NAME" || true
if [[ -f $TRANSACTION_BACKUP_DIR/new-scope-unit ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/new-scope-unit" "$unit_path"
else
rm -f -- "$unit_path"
fi
if [[ -f $TRANSACTION_BACKUP_DIR/new-updater-service ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/new-updater-service" "$updater_service_path"
else
rm -f -- "$updater_service_path"
fi
if [[ -f $TRANSACTION_BACKUP_DIR/new-updater-timer ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/new-updater-timer" "$updater_timer_path"
else
rm -f -- "$updater_timer_path"
fi
systemctl_for "$register_service" daemon-reload || true
fi
if [[ $OLD_SERVICE_MUTATION_ARMED -eq 1 && -f $TRANSACTION_BACKUP_DIR/old-scope-unit ]]; then
install -d -m 0755 -- "$(dirname -- "$OLD_SERVICE_UNIT")"
cp -a -- "$TRANSACTION_BACKUP_DIR/old-scope-unit" "$OLD_SERVICE_UNIT"
if [[ -f $TRANSACTION_BACKUP_DIR/old-updater-service ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/old-updater-service" \
"$OLD_UPDATER_SERVICE_UNIT"
fi
if [[ -f $TRANSACTION_BACKUP_DIR/old-updater-timer ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/old-updater-timer" "$OLD_UPDATER_TIMER_UNIT"
fi
systemctl_for "$OLD_SERVICE_SCOPE" daemon-reload || true
fi
if [[ $HELPER_MUTATION_ARMED -eq 1 ]]; then
if [[ -f $TRANSACTION_BACKUP_DIR/install-helper ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/install-helper" "$prefix/install.sh"
else
rm -f -- "$prefix/install.sh"
fi
if [[ -f $TRANSACTION_BACKUP_DIR/uninstall-helper ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/uninstall-helper" "$prefix/uninstall.sh"
else
rm -f -- "$prefix/uninstall.sh"
fi
fi
if [[ $LAUNCHER_MUTATION_ARMED -eq 1 ]]; then
if [[ -f $TRANSACTION_BACKUP_DIR/launcher ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/launcher" "$LAUNCHER_PATH"
else
rm -f -- "$LAUNCHER_PATH"
fi
fi
restore_current_link "$old_current"
if [[ -f $TRANSACTION_BACKUP_DIR/install-record ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/install-record" "$record"
else
rm -f -- "$record"
fi
if [[ -f $TRANSACTION_BACKUP_DIR/install-json ]]; then
cp -a -- "$TRANSACTION_BACKUP_DIR/install-json" "$json_record"
else
rm -f -- "$json_record"
fi
if [[ -n ${OLD_SERVICE_UNIT:-} && -f ${OLD_SERVICE_UNIT:-} ]]; then
if [[ $OLD_SERVICE_WAS_ENABLED -eq 1 ]]; then
systemctl_for "$OLD_SERVICE_SCOPE" enable "$SERVICE_NAME" || true
else
systemctl_for "$OLD_SERVICE_SCOPE" disable "$SERVICE_NAME" || true
fi
if [[ $OLD_SERVICE_WAS_ACTIVE -eq 1 ]]; then
systemctl_for "$OLD_SERVICE_SCOPE" restart "$SERVICE_NAME" || true
else
systemctl_for "$OLD_SERVICE_SCOPE" stop "$SERVICE_NAME" || true
fi
if [[ -n ${OLD_UPDATER_TIMER_UNIT:-} ]]; then
if [[ $OLD_UPDATER_TIMER_WAS_ENABLED -eq 1 ]]; then
systemctl_for "$OLD_SERVICE_SCOPE" enable "$UPDATER_TIMER_NAME" || true
else
systemctl_for "$OLD_SERVICE_SCOPE" disable "$UPDATER_TIMER_NAME" || true
fi
if [[ $OLD_UPDATER_TIMER_WAS_ACTIVE -eq 1 ]]; then
systemctl_for "$OLD_SERVICE_SCOPE" start "$UPDATER_TIMER_NAME" || true
else
systemctl_for "$OLD_SERVICE_SCOPE" stop "$UPDATER_TIMER_NAME" || true
fi
fi
fi
}
cleanup_install() {
local status=$?
trap - EXIT
if [[ $status -ne 0 && $TRANSACTION_ACTIVE -eq 1 && $INSTALL_COMMITTED -eq 0 ]]; then
rollback_install
fi
[[ -z $RECORD_TEMPORARY ]] || rm -f -- "$RECORD_TEMPORARY"
[[ -z $JSON_RECORD_TEMPORARY ]] || rm -f -- "$JSON_RECORD_TEMPORARY"
[[ -z $UNIT_TEMPORARY ]] || rm -f -- "$UNIT_TEMPORARY"
[[ -z $UPDATER_SERVICE_TEMPORARY ]] || rm -f -- "$UPDATER_SERVICE_TEMPORARY"
[[ -z $UPDATER_TIMER_TEMPORARY ]] || rm -f -- "$UPDATER_TIMER_TEMPORARY"
[[ -z $INSTALL_HELPER_TEMPORARY ]] || rm -f -- "$INSTALL_HELPER_TEMPORARY"
[[ -z $UNINSTALL_HELPER_TEMPORARY ]] || rm -f -- "$UNINSTALL_HELPER_TEMPORARY"
[[ -z $UPDATER_BUNDLE_STAGING ]] || rm -rf -- "$UPDATER_BUNDLE_STAGING"
[[ -z $LAUNCHER_TEMPORARY ]] || rm -f -- "$LAUNCHER_TEMPORARY"
[[ -z $CONFIG_MIGRATION_TEMPORARY ]] ||
rm -f -- "$CONFIG_MIGRATION_TEMPORARY"
[[ -z $PLUGIN_BOOTSTRAP_TEMPORARY ]] ||
rm -rf -- "$PLUGIN_BOOTSTRAP_TEMPORARY"
[[ -z $STAGING ]] || rm -rf -- "$STAGING"
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 && -n $CREATED_DESTINATION ]]; then
rm -rf -- "$CREATED_DESTINATION"
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 && $CONFIG_CREATED -eq 1 ]]; then
rm -f -- "$config_file"
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 && $CONFIG_DIR_CREATED -eq 1 ]]; then
rm -rf -- "$config_dir"
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 && $STATE_DIR_CREATED -eq 1 &&
$state_dir != "$config_dir" ]]; then
rm -rf -- "$state_dir"
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 ]]; then
[[ $CACHE_DIR_CREATED -eq 0 ]] || rm -rf -- "$cache_dir"
[[ $LOGS_DIR_CREATED -eq 0 ]] || rm -rf -- "$logs_dir"
[[ $INCOMING_DIR_CREATED -eq 0 ]] || rm -rf -- "$incoming_dir"
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 ]]; then
restore_path_metadata "$config_file" "$CONFIG_FILE_METADATA_SNAPSHOT" ||
warn "could not restore configuration file metadata for '$config_file'"
restore_path_metadata "$state_dir" "$STATE_DIR_METADATA_SNAPSHOT" ||
warn "could not restore state directory metadata for '$state_dir'"
restore_path_metadata "$cache_dir" "$CACHE_DIR_METADATA_SNAPSHOT" ||
warn "could not restore cache directory metadata for '$cache_dir'"
restore_path_metadata "$logs_dir" "$LOGS_DIR_METADATA_SNAPSHOT" ||
warn "could not restore logs directory metadata for '$logs_dir'"
restore_path_metadata "$incoming_dir" "$INCOMING_DIR_METADATA_SNAPSHOT" ||
warn "could not restore incoming directory metadata for '$incoming_dir'"
restore_path_metadata "$config_dir" "$CONFIG_DIR_METADATA_SNAPSHOT" ||
warn "could not restore configuration directory metadata for '$config_dir'"
fi
[[ -z $TRANSACTION_BACKUP_DIR ]] || rm -rf -- "$TRANSACTION_BACKUP_DIR"
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 &&
$UPDATER_BUNDLE_CREATED -eq 1 ]]; then
rm -rf -- "$UPDATER_BUNDLE_PATH"
rmdir -- "$prefix/libexec/updater" "$prefix/libexec" 2>/dev/null || true
fi
if [[ $status -ne 0 && $INSTALL_COMMITTED -eq 0 ]]; then
remove_new_system_user
fi
exit "$status"
}
trap cleanup_install EXIT
if [[ -e $prefix/versions || -L $prefix/versions ]]; then
[[ -d $prefix/versions && ! -L $prefix/versions ]] ||
die "versions path is not a real directory: $prefix/versions"
fi
install -d -m 0755 -- "$prefix" "$prefix/versions"
if [[ $RUNTIME_TRUST_SCOPE != none ]]; then
validate_trusted_path_ancestors "$RUNTIME_TRUST_SCOPE" "$prefix" "$INSTALL_UID" 1
validate_trusted_path_ancestors "$RUNTIME_TRUST_SCOPE" "$prefix/versions" "$INSTALL_UID" 1
fi
acquire_prefix_lock "$prefix"
record=$prefix/$RECORD_NAME
json_record=$prefix/$JSON_RECORD_NAME
load_old_record "$record"
if [[ -n ${OLD_UPDATER_HELPER:-} ]]; then
verify_recorded_updater_helper "$OLD_UPDATER_HELPER" \
"${OLD_UPDATER_HELPER_SHA256:-}"
elif [[ -n ${OLD_UPDATER_HELPER_SHA256:-} ]]; then
die "existing privileged updater helper record is incomplete"
fi
if [[ ( -e $UPDATER_HELPER_PATH || -L $UPDATER_HELPER_PATH ) &&
${OLD_UPDATER_HELPER:-} != "$UPDATER_HELPER_RECORD_PATH" ]]; then
die "refusing to overwrite an unrecognized privileged updater helper: $UPDATER_HELPER_PATH"
fi
if [[ -n ${OLD_LAUNCHER:-} ]]; then
verify_recorded_launcher "$OLD_LAUNCHER" "${OLD_LAUNCHER_SHA256:-}"
elif [[ -n ${OLD_LAUNCHER_SHA256:-} ]]; then
die "existing portable launcher record is incomplete"
fi
if [[ $WANT_LAUNCHER -eq 1 && ( -e $LAUNCHER_PATH || -L $LAUNCHER_PATH ) &&
-z ${OLD_LAUNCHER:-} ]]; then
die "refusing to overwrite an unrecognized portable launcher: $LAUNCHER_PATH"
fi
if [[ ${OLD_CREATED_SYSTEM_USER:-0} == 1 ]]; then
[[ ${OLD_SERVICE_UID:-} =~ ^[0-9]+$ && ${OLD_SERVICE_GID:-} =~ ^[0-9]+$ ]] ||
die "installer-created service account record has no uid/gid identity"
[[ $(id -u "$SERVICE_USER") -eq $OLD_SERVICE_UID &&
$(id -g "$SERVICE_USER") -eq $OLD_SERVICE_GID ]] ||
die "the recorded $SERVICE_USER account was deleted or replaced"
CREATED_SYSTEM_USER=1
SERVICE_UID=$OLD_SERVICE_UID
SERVICE_GID=$OLD_SERVICE_GID
fi
if [[ $mode == system || $register_service == system ]]; then
ensure_system_user "$state_dir"
fi
destination=$prefix/versions/$version
if [[ -e $destination || -L $destination ]]; then
[[ -d $destination && ! -L $destination && -f $destination/manifest.json ]] ||
die "release destination already exists but is incomplete: $destination"
cmp -s -- "$manifest" "$destination/manifest.json" ||
die "release '$version' is already installed with a different manifest"
else
STAGING=$(mktemp -d --tmpdir="$prefix/versions" ".staging-$version.XXXXXX")
cp -a -- "$PAYLOAD_ROOT/bin" "$PAYLOAD_ROOT/lib" "$PAYLOAD_ROOT/share" \
"$PAYLOAD_ROOT/service" "$PAYLOAD_ROOT/manifest.json" "$STAGING/"
[[ -x $STAGING/bin/echoiond && -x $STAGING/bin/echoion-updater &&
-f $STAGING/lib/Echoion.ts ]] ||
die "staged release failed layout validation"
mv -- "$STAGING" "$destination"
STAGING=
CREATED_DESTINATION=$destination
fi
if [[ $mode == system || $register_service == system ]]; then
normalize_and_validate_system_runtime "$destination"
fi
[[ -e $config_dir || -L $config_dir ]] || CONFIG_DIR_CREATED=1
[[ -e $state_dir || -L $state_dir ]] || STATE_DIR_CREATED=1
[[ -e $cache_dir || -L $cache_dir ]] || CACHE_DIR_CREATED=1
[[ -e $logs_dir || -L $logs_dir ]] || LOGS_DIR_CREATED=1
[[ -e $incoming_dir || -L $incoming_dir ]] || INCOMING_DIR_CREATED=1
CONFIG_DIR_METADATA_SNAPSHOT=$(snapshot_path_metadata "$config_dir")
STATE_DIR_METADATA_SNAPSHOT=$(snapshot_path_metadata "$state_dir")
CACHE_DIR_METADATA_SNAPSHOT=$(snapshot_path_metadata "$cache_dir")
LOGS_DIR_METADATA_SNAPSHOT=$(snapshot_path_metadata "$logs_dir")
INCOMING_DIR_METADATA_SNAPSHOT=$(snapshot_path_metadata "$incoming_dir")
CONFIG_FILE_METADATA_SNAPSHOT=$(snapshot_path_metadata "$config_file")
if [[ $CONFIG_DIR_CREATED -eq 0 && $RUNTIME_TRUST_SCOPE == system ]]; then
validate_mutable_directory system "$config_dir" "$(stat -c '%u' -- /)" "$INSTALL_UID"
elif [[ $CONFIG_DIR_CREATED -eq 0 && $RUNTIME_TRUST_SCOPE == user ]]; then
validate_mutable_directory user "$config_dir" "$INSTALL_UID" "$INSTALL_UID"
fi
if [[ $STATE_DIR_CREATED -eq 0 && $RUNTIME_TRUST_SCOPE == system ]]; then
validate_mutable_directory system "$state_dir" "$SERVICE_UID" "$INSTALL_UID"
elif [[ $STATE_DIR_CREATED -eq 0 && $RUNTIME_TRUST_SCOPE == user ]]; then
validate_mutable_directory user "$state_dir" "$INSTALL_UID" "$INSTALL_UID"
fi
for mutable_directory in "$cache_dir" "$logs_dir" "$incoming_dir"; do
if [[ -e $mutable_directory || -L $mutable_directory ]]; then
if [[ $RUNTIME_TRUST_SCOPE == system ]]; then
validate_mutable_directory system "$mutable_directory" "$SERVICE_UID" "$INSTALL_UID"
elif [[ $RUNTIME_TRUST_SCOPE == user ]]; then
validate_mutable_directory user "$mutable_directory" "$INSTALL_UID" "$INSTALL_UID"
elif [[ ! -d $mutable_directory || -L $mutable_directory ]]; then
die "mutable path is not a real directory: '$mutable_directory'"
fi
fi
done
for update_directory in "$state_dir/updates" \
"$state_dir/updates/privileged-requests" "$state_dir/updates/processed" \
"$state_dir/updates/rejected" "$state_dir/updates/staged"; do
if [[ -e $update_directory || -L $update_directory ]]; then
[[ -d $update_directory && ! -L $update_directory ]] ||
die "updater state path is not a real directory: '$update_directory'"
fi
done
install -d -m 0700 -- "$config_dir" "$state_dir" "$cache_dir" "$logs_dir" "$incoming_dir"
install -d -m 0700 -- "$state_dir/updates" \
"$state_dir/updates/privileged-requests" "$state_dir/updates/processed" \
"$state_dir/updates/rejected" "$state_dir/updates/staged"
if [[ ! -e $config_file && ! -L $config_file ]]; then
render_config "$destination/share/echoion/config.example.json" "$config_file" \
"./share/echoion/web"
CONFIG_CREATED=1
elif [[ -L $config_file || ! -f $config_file ]]; then
die "configuration path exists but is not a regular file: $config_file"
fi
if [[ $mode == system || $register_service == system ]]; then
[[ $config_dir != "$state_dir" ]] ||
die "system installations require separate configuration and state directories"
chown "$SERVICE_USER:$SERVICE_USER" "$state_dir"
chown "$SERVICE_USER:$SERVICE_USER" "$state_dir/updates" \
"$state_dir/updates/privileged-requests" \
"$state_dir/updates/processed" "$state_dir/updates/rejected" \
"$state_dir/updates/staged"
chown "$SERVICE_USER:$SERVICE_USER" "$cache_dir" "$logs_dir" "$incoming_dir"
chown "root:$SERVICE_USER" "$config_dir" "$config_file"
chmod 0750 "$config_dir" "$state_dir"
chmod 2770 "$state_dir/updates" \
"$state_dir/updates/privileged-requests" "$state_dir/updates/processed" \
"$state_dir/updates/rejected" "$state_dir/updates/staged"
chmod 0750 "$cache_dir" "$logs_dir" "$incoming_dir"
chmod 0640 "$config_file"
else
chmod 0700 "$config_dir" "$state_dir" "$cache_dir" "$logs_dir" "$incoming_dir"
chmod 0700 "$state_dir/updates" \
"$state_dir/updates/privileged-requests" "$state_dir/updates/processed" \
"$state_dir/updates/rejected" "$state_dir/updates/staged"
chmod 0600 "$config_file"
fi
if [[ $RUNTIME_TRUST_SCOPE == system ]]; then
validate_mutable_directory system "$config_dir" "$(stat -c '%u' -- /)" "$INSTALL_UID"
validate_mutable_directory system "$state_dir" "$SERVICE_UID" "$INSTALL_UID"
elif [[ $RUNTIME_TRUST_SCOPE == user ]]; then
validate_mutable_directory user "$config_dir" "$INSTALL_UID" "$INSTALL_UID"
validate_mutable_directory user "$state_dir" "$INSTALL_UID" "$INSTALL_UID"
fi
if [[ ${OLD_INSTALL_ID:-} =~ ^[A-Za-z0-9-]{16,64}$ ]]; then
INSTALL_ID=$OLD_INSTALL_ID
else
INSTALL_ID=$(new_install_id)
fi
capture_managed_directory config "$config_dir" "${OLD_CONFIG_PURGE_ALLOWED:-0}" \
"${OLD_CONFIG_DIR_DEVICE:-}" "${OLD_CONFIG_DIR_INODE:-}" "${OLD_CONFIG_DIR_UID:-}" \
"${OLD_CONFIG_DIR_GID:-}" "${OLD_CONFIG_MARKER_SHA256:-}" "$CONFIG_DIR_CREATED"
capture_managed_directory state "$state_dir" "${OLD_STATE_PURGE_ALLOWED:-0}" \
"${OLD_STATE_DIR_DEVICE:-}" "${OLD_STATE_DIR_INODE:-}" "${OLD_STATE_DIR_UID:-}" \
"${OLD_STATE_DIR_GID:-}" "${OLD_STATE_MARKER_SHA256:-}" "$STATE_DIR_CREATED"
candidate_executable=$destination/bin/echoiond
if ! python3 "$manifest_validator" --root "$destination" --version "$version"; then
die "offline payload validation failed for Echoion $version"
fi
if [[ $register_service != none ]]; then
validate_service_access "$register_service" "$candidate_executable" "$config_file" "$state_dir"
fi
old_current=
if [[ -L $prefix/current ]]; then
old_current=$(readlink -- "$prefix/current")
[[ $old_current =~ ^versions/[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]] ||
die "current symlink has an unsafe target: $old_current"
elif [[ -e $prefix/current ]]; then
die "current exists but is not a symlink: $prefix/current"
fi
unit_path=
unit_hash=
updater_service_path=
updater_service_hash=
updater_timer_path=
updater_timer_hash=
unit_root=
if [[ $register_service == user ]]; then
unit_root=${XDG_CONFIG_HOME:-${HOME:?HOME is required}/.config}
reject_control_characters service-unit-root "$unit_root"
unit_root=$(absolute_path "$unit_root")
validate_trusted_path_ancestors user "$unit_root" "$INSTALL_UID" 0
install -d -m 0755 -- "$unit_root/systemd/user"
validate_trusted_path_ancestors user "$unit_root" "$INSTALL_UID" 1
unit_path=$unit_root/systemd/user/$SERVICE_NAME
updater_service_path=$unit_root/systemd/user/$UPDATER_SERVICE_NAME
updater_timer_path=$unit_root/systemd/user/$UPDATER_TIMER_NAME
elif [[ $register_service == system ]]; then
unit_path=/etc/systemd/system/$SERVICE_NAME
updater_service_path=/etc/systemd/system/$UPDATER_SERVICE_NAME
updater_timer_path=/etc/systemd/system/$UPDATER_TIMER_NAME
fi
service_scope_changes=0
OLD_SERVICE_WAS_ENABLED=0
OLD_SERVICE_WAS_ACTIVE=0
OLD_UPDATER_TIMER_WAS_ENABLED=0
OLD_UPDATER_TIMER_WAS_ACTIVE=0
if [[ ${OLD_SERVICE_SCOPE:-none} != none ]]; then
if systemctl_for "$OLD_SERVICE_SCOPE" is-enabled --quiet "$SERVICE_NAME"; then
OLD_SERVICE_WAS_ENABLED=1
fi
if systemctl_for "$OLD_SERVICE_SCOPE" is-active --quiet "$SERVICE_NAME"; then
OLD_SERVICE_WAS_ACTIVE=1
fi
if [[ -n ${OLD_UPDATER_TIMER_UNIT:-} ]]; then
if systemctl_for "$OLD_SERVICE_SCOPE" is-enabled --quiet "$UPDATER_TIMER_NAME"; then
OLD_UPDATER_TIMER_WAS_ENABLED=1
fi
if systemctl_for "$OLD_SERVICE_SCOPE" is-active --quiet "$UPDATER_TIMER_NAME"; then
OLD_UPDATER_TIMER_WAS_ACTIVE=1
fi
fi
fi
if [[ ${OLD_SERVICE_SCOPE:-none} != none &&
( ${OLD_SERVICE_SCOPE:-none} != "$register_service" || ${OLD_SERVICE_UNIT:-} != "$unit_path" ) ]]; then
verify_recorded_unit "$OLD_SERVICE_SCOPE" "$OLD_SERVICE_UNIT" "$OLD_SERVICE_SHA256"
if [[ -n ${OLD_UPDATER_SERVICE_UNIT:-} || -n ${OLD_UPDATER_SERVICE_SHA256:-} ]]; then
verify_recorded_unit "$OLD_SERVICE_SCOPE" "$OLD_UPDATER_SERVICE_UNIT" \
"$OLD_UPDATER_SERVICE_SHA256"
fi
if [[ -n ${OLD_UPDATER_TIMER_UNIT:-} || -n ${OLD_UPDATER_TIMER_SHA256:-} ]]; then
verify_recorded_unit "$OLD_SERVICE_SCOPE" "$OLD_UPDATER_TIMER_UNIT" \
"$OLD_UPDATER_TIMER_SHA256"
fi
service_scope_changes=1
fi
if [[ $register_service != none && -e $unit_path ]]; then
if [[ ${OLD_SERVICE_UNIT:-} == "$unit_path" && ${OLD_SERVICE_SCOPE:-} == "$register_service" ]]; then
verify_recorded_unit "$OLD_SERVICE_SCOPE" "$OLD_SERVICE_UNIT" "$OLD_SERVICE_SHA256"
else
die "refusing to overwrite unrecognized service unit: $unit_path"
fi
fi
if [[ $register_service != none && -e $updater_service_path ]]; then
if [[ ${OLD_UPDATER_SERVICE_UNIT:-} == "$updater_service_path" &&
${OLD_SERVICE_SCOPE:-} == "$register_service" ]]; then
verify_recorded_unit "$OLD_SERVICE_SCOPE" "$OLD_UPDATER_SERVICE_UNIT" \
"$OLD_UPDATER_SERVICE_SHA256"
else
die "refusing to overwrite unrecognized updater service: $updater_service_path"
fi
fi
if [[ $register_service != none && -e $updater_timer_path ]]; then
if [[ ${OLD_UPDATER_TIMER_UNIT:-} == "$updater_timer_path" &&
${OLD_SERVICE_SCOPE:-} == "$register_service" ]]; then
verify_recorded_unit "$OLD_SERVICE_SCOPE" "$OLD_UPDATER_TIMER_UNIT" \
"$OLD_UPDATER_TIMER_SHA256"
else
die "refusing to overwrite unrecognized updater timer: $updater_timer_path"
fi
fi
if [[ $register_service != none ]]; then
install -d -m 0755 -- "$(dirname -- "$unit_path")"
UNIT_TEMPORARY=$(mktemp --tmpdir="$(dirname -- "$unit_path")" '.echoion.service.XXXXXX')
render_unit "$destination/service/echoion.service.in" "$UNIT_TEMPORARY" "$register_service" \
"$prefix/current" "$prefix/current/lib/Echoion.ts" "$state_dir" \
"$prefix/current/bin/echoiond" "$config_file" "$incoming_dir"
UPDATER_SERVICE_TEMPORARY=$(mktemp --tmpdir="$(dirname -- "$updater_service_path")" \
'.echoion-updater.service.XXXXXX')
UPDATER_TIMER_TEMPORARY=$(mktemp --tmpdir="$(dirname -- "$updater_timer_path")" \
'.echoion-updater.timer.XXXXXX')
render_updater_service "$destination/service/echoion-updater.service.in" \
"$UPDATER_SERVICE_TEMPORARY" "$register_service" "$prefix" \
"$UPDATER_HELPER_PATH" "$state_dir"
render_updater_timer "$destination/service/echoion-updater.timer.in" \
"$UPDATER_TIMER_TEMPORARY" "$register_service"
chmod 0644 "$UNIT_TEMPORARY"
chmod 0644 "$UPDATER_SERVICE_TEMPORARY" "$UPDATER_TIMER_TEMPORARY"
fi
for helper_directory in "$prefix/libexec" "$prefix/libexec/updater"; do
if [[ -e $helper_directory || -L $helper_directory ]]; then
[[ -d $helper_directory && ! -L $helper_directory ]] ||
die "privileged helper path is not a real directory: $helper_directory"
fi
done
install -d -m 0755 -- "$prefix/libexec/updater"
if [[ $RUNTIME_TRUST_SCOPE != none ]]; then
validate_trusted_path_ancestors "$RUNTIME_TRUST_SCOPE" "$prefix/libexec/updater" \
"$INSTALL_UID" 1
fi
if [[ -e $UPDATER_BUNDLE_PATH || -L $UPDATER_BUNDLE_PATH ]]; then
[[ ${OLD_UPDATER_HELPER:-} == "$UPDATER_HELPER_RECORD_PATH" &&
-d $UPDATER_BUNDLE_PATH && ! -L $UPDATER_BUNDLE_PATH ]] ||
die "privileged updater bundle already exists but is not recorded: $UPDATER_BUNDLE_PATH"
cmp -s -- "$destination/bin/echoion-updater" "$UPDATER_HELPER_PATH" ||
die "recorded privileged updater helper differs from the validated release"
else
UPDATER_BUNDLE_STAGING=$(mktemp -d --tmpdir="$prefix/libexec/updater" \
".staging-$version.XXXXXX")
install -d -m 0755 -- "$UPDATER_BUNDLE_STAGING/bin"
install -m 0755 -- "$destination/bin/echoion-updater" \
"$UPDATER_BUNDLE_STAGING/bin/echoion-updater"
cp -a -- "$destination/lib" "$UPDATER_BUNDLE_STAGING/lib"
install -m 0644 -- "$destination/manifest.json" \
"$UPDATER_BUNDLE_STAGING/source-manifest.json"
mv -- "$UPDATER_BUNDLE_STAGING" "$UPDATER_BUNDLE_PATH"
UPDATER_BUNDLE_STAGING=
UPDATER_BUNDLE_CREATED=1
fi
if [[ $RUNTIME_TRUST_SCOPE == system ]]; then
normalize_and_validate_system_runtime "$UPDATER_BUNDLE_PATH"
fi
validate_privileged_updater_helper "$RUNTIME_TRUST_SCOPE" \
"$UPDATER_HELPER_PATH" "$INSTALL_UID"
UPDATER_HELPER_SHA256=$(sha256_file "$UPDATER_HELPER_PATH")
INSTALL_HELPER_TEMPORARY=$(mktemp --tmpdir="$prefix" '.install-helper.XXXXXX')
UNINSTALL_HELPER_TEMPORARY=$(mktemp --tmpdir="$prefix" '.uninstall-helper.XXXXXX')
install -m 0755 -- "$PAYLOAD_ROOT/install.sh" "$INSTALL_HELPER_TEMPORARY"
install -m 0755 -- "$PAYLOAD_ROOT/uninstall.sh" "$UNINSTALL_HELPER_TEMPORARY"
if [[ $WANT_LAUNCHER -eq 1 ]]; then
LAUNCHER_TEMPORARY=$(mktemp --tmpdir="$prefix" '.echoion-launcher.XXXXXX')
render_portable_launcher "$LAUNCHER_TEMPORARY"
chmod 0755 "$LAUNCHER_TEMPORARY"
LAUNCHER_RECORD_PATH=echoion
LAUNCHER_SHA256=$(sha256_file "$LAUNCHER_TEMPORARY")
fi
TRANSACTION_BACKUP_DIR=$(mktemp -d --tmpdir="$prefix" '.install-rollback.XXXXXX')
if [[ -f $record ]]; then
cp -a -- "$record" "$TRANSACTION_BACKUP_DIR/install-record"
fi
if [[ -f $json_record ]]; then
cp -a -- "$json_record" "$TRANSACTION_BACKUP_DIR/install-json"
fi
if [[ $service_scope_changes -eq 1 ]]; then
cp -a -- "$OLD_SERVICE_UNIT" "$TRANSACTION_BACKUP_DIR/old-scope-unit"
if [[ -n ${OLD_UPDATER_SERVICE_UNIT:-} ]]; then
cp -a -- "$OLD_UPDATER_SERVICE_UNIT" "$TRANSACTION_BACKUP_DIR/old-updater-service"
fi
if [[ -n ${OLD_UPDATER_TIMER_UNIT:-} ]]; then
cp -a -- "$OLD_UPDATER_TIMER_UNIT" "$TRANSACTION_BACKUP_DIR/old-updater-timer"
fi
fi
if [[ $register_service != none && -f $unit_path ]]; then
cp -a -- "$unit_path" "$TRANSACTION_BACKUP_DIR/new-scope-unit"
fi
if [[ $register_service != none && -f $updater_service_path ]]; then
cp -a -- "$updater_service_path" "$TRANSACTION_BACKUP_DIR/new-updater-service"
fi
if [[ $register_service != none && -f $updater_timer_path ]]; then
cp -a -- "$updater_timer_path" "$TRANSACTION_BACKUP_DIR/new-updater-timer"
fi
if [[ -f $prefix/install.sh && $prefix/install.sh != "$INSTALL_HELPER_TEMPORARY" ]]; then
cp -a -- "$prefix/install.sh" "$TRANSACTION_BACKUP_DIR/install-helper"
fi
if [[ -f $prefix/uninstall.sh && $prefix/uninstall.sh != "$UNINSTALL_HELPER_TEMPORARY" ]]; then
cp -a -- "$prefix/uninstall.sh" "$TRANSACTION_BACKUP_DIR/uninstall-helper"
fi
if [[ -n ${OLD_LAUNCHER:-} ]]; then
cp -a -- "$LAUNCHER_PATH" "$TRANSACTION_BACKUP_DIR/launcher"
fi
if [[ $(legacy_web_root_count "$config_file") -eq 1 ]]; then
cp -a -- "$config_file" "$TRANSACTION_BACKUP_DIR/config-file"
CONFIG_MIGRATION_ARMED=1
fi
if [[ ${OLD_SERVICE_SCOPE:-none} != none &&
-n ${OLD_UPDATER_SERVICE_UNIT:-} ]]; then
systemctl_for "$OLD_SERVICE_SCOPE" stop "$UPDATER_SERVICE_NAME" ||
die "could not stop the updater before replacing the installed release"
# The helper may have switched current while this installer was staging the
# new payload. Capture the rollback target only after systemd confirms that
# the oneshot has stopped.
old_current=
if [[ -L $prefix/current ]]; then
old_current=$(readlink -- "$prefix/current")
[[ $old_current =~ ^versions/[A-Za-z0-9][A-Za-z0-9._-]{0,63}$ ]] ||
die "current symlink has an unsafe target: $old_current"
elif [[ -e $prefix/current ]]; then
die "current exists but is not a symlink: $prefix/current"
fi
fi
TRANSACTION_ACTIVE=1
if [[ $CONFIG_MIGRATION_ARMED -eq 1 ]]; then
migrate_legacy_web_root "$config_file"
fi
bootstrap_plugins "$destination/share/echoion/bootstrap-plugins"
HELPER_MUTATION_ARMED=1
mv -f -- "$INSTALL_HELPER_TEMPORARY" "$prefix/install.sh"
INSTALL_HELPER_TEMPORARY=
mv -f -- "$UNINSTALL_HELPER_TEMPORARY" "$prefix/uninstall.sh"
UNINSTALL_HELPER_TEMPORARY=
if [[ $WANT_LAUNCHER -eq 1 || -n ${OLD_LAUNCHER:-} ]]; then
LAUNCHER_MUTATION_ARMED=1
if [[ $WANT_LAUNCHER -eq 1 ]]; then
mv -f -- "$LAUNCHER_TEMPORARY" "$LAUNCHER_PATH"
LAUNCHER_TEMPORARY=
else
rm -f -- "$LAUNCHER_PATH"
fi
fi
if [[ $service_scope_changes -eq 1 ]]; then
OLD_SERVICE_MUTATION_ARMED=1
remove_recorded_service "$OLD_SERVICE_SCOPE" "$OLD_SERVICE_UNIT" "$OLD_SERVICE_SHA256" \
"${OLD_UPDATER_SERVICE_UNIT:-}" "${OLD_UPDATER_SERVICE_SHA256:-}" \
"${OLD_UPDATER_TIMER_UNIT:-}" "${OLD_UPDATER_TIMER_SHA256:-}" ||
die "could not remove the previously registered service"
fi
if [[ $register_service != none ]]; then
NEW_UNIT_MUTATION_ARMED=1
mv -f -- "$UNIT_TEMPORARY" "$unit_path"
UNIT_TEMPORARY=
mv -f -- "$UPDATER_SERVICE_TEMPORARY" "$updater_service_path"
UPDATER_SERVICE_TEMPORARY=
mv -f -- "$UPDATER_TIMER_TEMPORARY" "$updater_timer_path"
UPDATER_TIMER_TEMPORARY=
unit_hash=$(sha256_file "$unit_path")
updater_service_hash=$(sha256_file "$updater_service_path")
updater_timer_hash=$(sha256_file "$updater_timer_path")
fi
new_link_directory=$(mktemp -d --tmpdir="$prefix" '.current-link.XXXXXX')
ln -s -- "versions/$version" "$new_link_directory/current"
mv -Tf -- "$new_link_directory/current" "$prefix/current"
rmdir -- "$new_link_directory"
if [[ $register_service != none ]]; then
systemctl_for "$register_service" daemon-reload || die "systemd rejected the installed service unit"
systemctl_for "$register_service" stop "$UPDATER_SERVICE_NAME" ||
die "could not leave the updater service stopped"
if [[ $start -eq 1 ]]; then
if ! systemctl_for "$register_service" enable "$SERVICE_NAME" "$UPDATER_TIMER_NAME" ||
! systemctl_for "$register_service" restart "$SERVICE_NAME" ||
! systemctl_for "$register_service" start "$UPDATER_TIMER_NAME" ||
! systemctl_for "$register_service" is-active --quiet "$SERVICE_NAME"; then
warn "new service failed health validation; restoring previous release and unit"
die "service failed to start"
fi
else
systemctl_for "$register_service" disable --now "$SERVICE_NAME" \
"$UPDATER_TIMER_NAME" ||
die "could not leave the registered service disabled and stopped"
fi
fi
write_record "$record" "$unit_path" "$unit_hash"
write_json_record "$json_record"
INSTALL_COMMITTED=1
TRANSACTION_ACTIVE=0
if [[ -n ${OLD_UPDATER_HELPER:-} &&
${OLD_UPDATER_HELPER:-} != "$UPDATER_HELPER_RECORD_PATH" ]]; then
old_updater_bundle=$prefix/${OLD_UPDATER_HELPER%/bin/echoion-updater}
rm -rf -- "$old_updater_bundle"
rmdir -- "$prefix/libexec/updater" "$prefix/libexec" 2>/dev/null || true
fi
if [[ $PAYLOAD_ROOT == "$prefix" ]]; then
[[ -n $prefix && $prefix != / ]] || die "unsafe in-place install prefix"
rm -rf -- "${prefix:?}/bin" "${prefix:?}/lib" "${prefix:?}/share" \
"${prefix:?}/service"
rm -f -- "$prefix/manifest.json"
fi
printf 'Echoion %s installed in %s\n' "$version" "$prefix"
printf 'Configuration: %s\n' "$config_file"
printf 'State: %s\n' "$state_dir"
if [[ $register_service == none ]]; then
if [[ $WANT_LAUNCHER -eq 1 ]]; then
printf 'Run: %q\n' "$LAUNCHER_PATH"
else
printf 'Run: cd %q && LD_LIBRARY_PATH=%q ECHOION_INCOMING_DIR=%q %q --config %q --state-dir %q\n' \
"$prefix/current" "$prefix/current/lib" "$incoming_dir" \
"$prefix/current/bin/echoiond" "$config_file" "$state_dir"
fi
elif [[ $start -eq 1 ]]; then
printf '%s is enabled and running.\n' "$SERVICE_NAME"
else
printf '%s was registered but is disabled and stopped.\n' "$SERVICE_NAME"
fi