| 1 | #!/bin/bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | export PATH=/usr/bin:/bin:/usr/sbin:/sbin |
| 5 | |
| 6 | fail() { printf '%s\n' "$*" >&2; exit 1; } |
| 7 | |
| 8 | case "${1:-}" in |
| 9 | inspect|launch) [[ $# == 1 ]] || fail "Usage: $0 $1" ;; |
| 10 | install) [[ $# == 2 ]] || fail "Usage: $0 install /path/to/Claude.dmg" ;; |
| 11 | *) fail "Usage: $0 inspect | install /path/to/Claude.dmg | launch" ;; |
| 12 | esac |
| 13 | |
| 14 | [[ $(uname -s) == Darwin ]] || fail "This helper requires a Cage macOS guest." |
| 15 | model=$(sysctl -n hw.model) |
| 16 | [[ $model =~ ^VirtualMac[0-9]+,[0-9]+$ ]] || fail "This Mac is not a virtual Mac. Run this helper inside the Cage guest." |
| 17 | marker=/var/db/cage-guest-os |
| 18 | [[ -f $marker && ! -L $marker && $(stat -f '%u' "$marker") == 0 ]] || |
| 19 | fail "Missing root-owned Cage guest marker." |
| 20 | mode=$(stat -f '%OLp' "$marker") |
| 21 | (( (8#$mode & 022) == 0 )) && [[ $(cat "$marker") == macos ]] || |
| 22 | fail "Invalid Cage guest marker." |
| 23 | |
| 24 | app=/Applications/Claude.app |
| 25 | bundle_id=com.anthropic.claudefordesktop |
| 26 | team=Q6L2SF6YDW |
| 27 | requirement="anchor apple generic and identifier \"$bundle_id\" and certificate leaf[subject.OU] = \"$team\" and certificate 1[field.1.2.840.113635.100.6.2.6] exists and certificate leaf[field.1.2.840.113635.100.6.1.13] exists" |
| 28 | |
| 29 | info() { |
| 30 | plutil -extract "$2" raw -o - "$1/Contents/Info.plist" |
| 31 | } |
| 32 | |
| 33 | verify_app() { |
| 34 | [[ -d $1 && ! -L $1 ]] || fail "Expected a Claude.app directory at $1." |
| 35 | [[ $(info "$1" CFBundleIdentifier) == "$bundle_id" ]] || fail "Unexpected Claude bundle identifier." |
| 36 | codesign --verify --deep --strict "$1" |
| 37 | codesign --verify --strict -R "=$requirement" "$1" |
| 38 | } |
| 39 | |
| 40 | report() { |
| 41 | printf 'status=%s\nversion=%s\nbuild=%s\nbundle_id=%s\nteam=%s\n' \ |
| 42 | "$1" "$(info "$app" CFBundleShortVersionString)" "$(info "$app" CFBundleVersion)" "$bundle_id" "$team" |
| 43 | } |
| 44 | |
| 45 | case "$1" in |
| 46 | inspect) |
| 47 | [[ -e $app || -L $app ]] || { printf 'status=absent\n'; exit 3; } |
| 48 | verify_app "$app" |
| 49 | report installed |
| 50 | ;; |
| 51 | install) |
| 52 | [[ $EUID == 0 ]] || fail "Run install as root inside the guest." |
| 53 | dmg=$2 |
| 54 | [[ $dmg == /* && -f $dmg ]] || fail "Supply the absolute path of the downloaded Claude DMG." |
| 55 | exec 9>/var/run/cage-claude-install.lock |
| 56 | lockf -s -t 0 9 || fail "A Claude installation is in progress." |
| 57 | stage=$(mktemp -d /Applications/.cage-claude.XXXXXXXX) |
| 58 | mountdir="$stage/media" |
| 59 | mkdir "$mountdir" |
| 60 | cleanup() { |
| 61 | |
| 62 | if [[ $(stat -f '%d' "$mountdir") != "$(stat -f '%d' "$stage")" ]]; then |
| 63 | hdiutil detach "$mountdir" >/dev/null || { printf 'Unable to detach %s\n' "$mountdir" >&2; return; } |
| 64 | fi |
| 65 | rm -rf "$stage" |
| 66 | } |
| 67 | trap cleanup EXIT |
| 68 | trap 'exit 1' HUP INT TERM |
| 69 | hdiutil attach -readonly -nobrowse -noautoopen -mountpoint "$mountdir" "$dmg" >/dev/null |
| 70 | source_app="$mountdir/Claude.app" |
| 71 | verify_app "$source_app" |
| 72 | |
| 73 | |
| 74 | rename_flag=4 |
| 75 | if [[ -e $app || -L $app ]]; then |
| 76 | verify_app "$app" |
| 77 | if [[ $(info "$source_app" CFBundleVersion) == "$(info "$app" CFBundleVersion)" && |
| 78 | $(info "$source_app" CFBundleShortVersionString) == "$(info "$app" CFBundleShortVersionString)" ]]; then |
| 79 | report installed |
| 80 | exit 0 |
| 81 | fi |
| 82 | if pgrep -f '^/Applications/Claude\.app/Contents/MacOS/Claude([[:space:]]|$)' >/dev/null; then |
| 83 | fail "Quit the running guest Claude app before installing another version." |
| 84 | fi |
| 85 | rename_flag=2 |
| 86 | fi |
| 87 | staged_app="$stage/Claude.app" |
| 88 | ditto "$source_app" "$staged_app" |
| 89 | chown -R -P root:wheel "$staged_app" |
| 90 | verify_app "$staged_app" |
| 91 | /usr/bin/osascript -l JavaScript - "$staged_app" "$app" "$rename_flag" <<'JXA' |
| 92 | ObjC.bindFunction("renamex_np", ["int", ["char *", "char *", "uint32_t"]]); |
| 93 | function run(args) { |
| 94 | if ($.renamex_np(args[0], args[1], Number(args[2])) !== 0) { |
| 95 | throw Error("Could not publish Claude.app; previous installation preserved."); |
| 96 | } |
| 97 | } |
| 98 | JXA |
| 99 | report installed |
| 100 | ;; |
| 101 | launch) |
| 102 | verify_app "$app" |
| 103 | gui_uid=$(stat -f '%u' /dev/console) |
| 104 | if (( gui_uid < 500 )) || ! launchctl print "gui/$gui_uid" >/dev/null 2>&1; then |
| 105 | printf 'status=requires_gui_login\nLog in to the guest macOS desktop first.\n' >&2 |
| 106 | exit 4 |
| 107 | fi |
| 108 | if [[ $EUID == 0 ]]; then |
| 109 | launchctl asuser "$gui_uid" sudo -n -H -u "#$gui_uid" -- /usr/bin/open "$app" |
| 110 | elif [[ $EUID == "$gui_uid" ]]; then |
| 111 | /usr/bin/open "$app" |
| 112 | else |
| 113 | fail "Run launch as root or the guest console user." |
| 114 | fi |
| 115 | for ((attempt=0; attempt<20; attempt++)); do |
| 116 | if pid=$(pgrep -o -U "$gui_uid" -f '^/Applications/Claude\.app/Contents/MacOS/Claude([[:space:]]|$)'); then |
| 117 | report running |
| 118 | printf 'pid=%s\n' "$pid" |
| 119 | exit 0 |
| 120 | fi |
| 121 | sleep 1 |
| 122 | done |
| 123 | fail "The Claude application process did not start in the guest GUI session." |
| 124 | ;; |
| 125 | esac |