fix(persistence): invoke the installed script literally (#31)
Invoke the installed script path literally from launchd
This commit is contained in:
@@ -9,9 +9,11 @@ cat > "$OUT" << HEADER
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
VERSION="$VERSION"
|
VERSION="$VERSION"
|
||||||
|
SCRIPT_DIR="\$(cd "\$(dirname "\$0")" && pwd)"
|
||||||
|
SCRIPT_PATH="\$SCRIPT_DIR/\$(basename "\$0")"
|
||||||
HEADER
|
HEADER
|
||||||
|
|
||||||
for lib in colors config detect validate dscl suppress backup status heal firewall harden whitelist check monitor history doctor selfupdate uninstall report ma_detect demo vpn; do
|
for lib in colors config detect validate dscl suppress backup status launchd heal firewall harden whitelist check monitor history doctor selfupdate uninstall report ma_detect demo vpn; do
|
||||||
tail -n +1 "$SCRIPT_DIR/lib/$lib.sh" >> "$OUT"
|
tail -n +1 "$SCRIPT_DIR/lib/$lib.sh" >> "$OUT"
|
||||||
echo "" >> "$OUT"
|
echo "" >> "$OUT"
|
||||||
done
|
done
|
||||||
|
|||||||
+3
-7
@@ -70,11 +70,7 @@ install_persist_launchdaemon() {
|
|||||||
root="$(_persist_mount_root "$data_mount")"
|
root="$(_persist_mount_root "$data_mount")"
|
||||||
|
|
||||||
local unleash_src
|
local unleash_src
|
||||||
if [ -n "$SCRIPT_DIR" ]; then
|
unleash_src=$(_launchd_script_path)
|
||||||
unleash_src="$SCRIPT_DIR/unleash"
|
|
||||||
else
|
|
||||||
unleash_src="$(cd "$(dirname "$0")" && pwd)/unleash"
|
|
||||||
fi
|
|
||||||
|
|
||||||
step "Installing LaunchDaemon for boot-time persistence..."
|
step "Installing LaunchDaemon for boot-time persistence..."
|
||||||
|
|
||||||
@@ -93,8 +89,8 @@ install_persist_launchdaemon() {
|
|||||||
<key>ProgramArguments</key>
|
<key>ProgramArguments</key>
|
||||||
<array>
|
<array>
|
||||||
<string>/bin/bash</string>
|
<string>/bin/bash</string>
|
||||||
<string>-c</string>
|
<string>${unleash_src}</string>
|
||||||
<string>${unleash_src} heal</string>
|
<string>heal</string>
|
||||||
</array>
|
</array>
|
||||||
<key>RunAtLoad</key>
|
<key>RunAtLoad</key>
|
||||||
<true/>
|
<true/>
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
_launchd_script_path() {
|
||||||
|
local script_path="${SCRIPT_PATH:-}"
|
||||||
|
if [ -z "$script_path" ]; then
|
||||||
|
script_path="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")"
|
||||||
|
fi
|
||||||
|
printf '%s' "$script_path" | sed 's/&/\&/g; s/</\</g; s/>/\>/g'
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
#!/usr/bin/env bats
|
||||||
|
|
||||||
|
@test "persistence invokes renamed scripts with spaces and XML text literally" {
|
||||||
|
local entrypoint
|
||||||
|
for entrypoint in unleash unleash-standalone.sh; do
|
||||||
|
local folder="$BATS_TEST_TMPDIR/$entrypoint/Tool Folder & <profile> \"quoted\""
|
||||||
|
local target="$BATS_TEST_TMPDIR/$entrypoint/Target"
|
||||||
|
local script="$folder/renamed standalone.sh"
|
||||||
|
local definitions="$BATS_TEST_TMPDIR/$entrypoint.functions"
|
||||||
|
mkdir -p "$folder" "$target/Library"
|
||||||
|
printf '#!/bin/bash\nprintf "ACTION=%%s\\n" "$1"\n' > "$script"
|
||||||
|
if [ "$entrypoint" = unleash ]; then
|
||||||
|
ln -s "$BATS_TEST_DIRNAME/../lib" "$folder/lib"
|
||||||
|
fi
|
||||||
|
sed '/^main "\$@"$/d' "$BATS_TEST_DIRNAME/../$entrypoint" > "$definitions"
|
||||||
|
run "${UNLEASH_TEST_SHELL:-bash}" -u -c '
|
||||||
|
unset SCRIPT_DIR SCRIPT_PATH
|
||||||
|
source "$1"
|
||||||
|
expected="$2/Library/LaunchDaemons"
|
||||||
|
mkdir() {
|
||||||
|
[ "$#" -eq 2 ] && [ "$1" = -p ] && [ "$2" = "$expected" ] || return 97
|
||||||
|
command mkdir "$@"
|
||||||
|
}
|
||||||
|
install_persist_launchdaemon "$2"
|
||||||
|
' "$script" "$definitions" "$target"
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
run python3 - "$target/Library/LaunchDaemons/com.unleash.heal.plist" "$script" <<'PY'
|
||||||
|
import plistlib,subprocess,sys
|
||||||
|
with open(sys.argv[1],'rb') as source:
|
||||||
|
data=plistlib.load(source)
|
||||||
|
expected=['/bin/bash',sys.argv[2],'heal']
|
||||||
|
assert data['ProgramArguments']==expected, data['ProgramArguments']
|
||||||
|
result=subprocess.run(expected,capture_output=True,text=True,timeout=5)
|
||||||
|
assert result.returncode==0, result.stderr
|
||||||
|
assert result.stdout=='ACTION=heal\n', result.stdout
|
||||||
|
PY
|
||||||
|
[ "$status" -eq 0 ]
|
||||||
|
done
|
||||||
|
}
|
||||||
@@ -4,9 +4,10 @@ set -euo pipefail
|
|||||||
VERSION="2.0.0"
|
VERSION="2.0.0"
|
||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
SCRIPT_PATH="$SCRIPT_DIR/$(basename "$0")"
|
||||||
LIB_DIR="$SCRIPT_DIR/lib"
|
LIB_DIR="$SCRIPT_DIR/lib"
|
||||||
|
|
||||||
for _lib in colors config detect validate dscl suppress backup status heal firewall harden whitelist check monitor history doctor selfupdate uninstall report ma_detect demo vpn init suggest remediate telemetry predict discord; do
|
for _lib in colors config detect validate dscl suppress backup status launchd heal firewall harden whitelist check monitor history doctor selfupdate uninstall report ma_detect demo vpn init suggest remediate telemetry predict discord; do
|
||||||
_lib_path="$LIB_DIR/$_lib.sh"
|
_lib_path="$LIB_DIR/$_lib.sh"
|
||||||
if [ -f "$_lib_path" ]; then
|
if [ -f "$_lib_path" ]; then
|
||||||
source "$_lib_path"
|
source "$_lib_path"
|
||||||
|
|||||||
+13
-7
@@ -1,6 +1,8 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
VERSION="2.0.0"
|
VERSION="2.0.0"
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
SCRIPT_PATH="$SCRIPT_DIR/$(basename "$0")"
|
||||||
RED='\033[1;31m'
|
RED='\033[1;31m'
|
||||||
GRN='\033[1;32m'
|
GRN='\033[1;32m'
|
||||||
BLU='\033[1;34m'
|
BLU='\033[1;34m'
|
||||||
@@ -940,6 +942,14 @@ deep_status_json() {
|
|||||||
echo -e "$json"
|
echo -e "$json"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_launchd_script_path() {
|
||||||
|
local script_path="${SCRIPT_PATH:-}"
|
||||||
|
if [ -z "$script_path" ]; then
|
||||||
|
script_path="$(cd "$(dirname "$0")" && pwd)/$(basename "$0")"
|
||||||
|
fi
|
||||||
|
printf '%s' "$script_path" | sed 's/&/\&/g; s/</\</g; s/>/\>/g'
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
heal_suppress() {
|
heal_suppress() {
|
||||||
local data_mount="$1"
|
local data_mount="$1"
|
||||||
@@ -1012,11 +1022,7 @@ install_persist_launchdaemon() {
|
|||||||
root="$(_persist_mount_root "$data_mount")"
|
root="$(_persist_mount_root "$data_mount")"
|
||||||
|
|
||||||
local unleash_src
|
local unleash_src
|
||||||
if [ -n "$SCRIPT_DIR" ]; then
|
unleash_src=$(_launchd_script_path)
|
||||||
unleash_src="$SCRIPT_DIR/unleash"
|
|
||||||
else
|
|
||||||
unleash_src="$(cd "$(dirname "$0")" && pwd)/unleash"
|
|
||||||
fi
|
|
||||||
|
|
||||||
step "Installing LaunchDaemon for boot-time persistence..."
|
step "Installing LaunchDaemon for boot-time persistence..."
|
||||||
|
|
||||||
@@ -1035,8 +1041,8 @@ install_persist_launchdaemon() {
|
|||||||
<key>ProgramArguments</key>
|
<key>ProgramArguments</key>
|
||||||
<array>
|
<array>
|
||||||
<string>/bin/bash</string>
|
<string>/bin/bash</string>
|
||||||
<string>-c</string>
|
<string>${unleash_src}</string>
|
||||||
<string>${unleash_src} heal</string>
|
<string>heal</string>
|
||||||
</array>
|
</array>
|
||||||
<key>RunAtLoad</key>
|
<key>RunAtLoad</key>
|
||||||
<true/>
|
<true/>
|
||||||
|
|||||||
Reference in New Issue
Block a user