| 1 | #!/bin/bash |
| 2 | |
| 3 | |
| 4 | set -euo pipefail |
| 5 | export PATH=/usr/bin:/bin:/usr/sbin:/sbin |
| 6 | fail() { printf '%s\n' "$*" >&2; exit 1; } |
| 7 | [[ $# == 1 && $1 == /* ]] || fail 'Usage: install-macos-project-tools.sh /path/to/root-owned-guest-project-kit' |
| 8 | [[ $(uname -sm) == 'Darwin arm64' && $(sysctl -n hw.model) =~ ^VirtualMac[0-9]+,[0-9]+$ && $(id -u) == 0 ]] || |
| 9 | fail 'Run this installer in the target arm64 Cage guest as administrator.' |
| 10 | marker=/var/db/cage-guest-os |
| 11 | [[ -f $marker && ! -L $marker && $(stat -f %u "$marker") == 0 && $(cat "$marker") == macos ]] || |
| 12 | fail 'Missing the root-owned Cage guest marker.' |
| 13 | mode=$(stat -f %OLp "$marker") |
| 14 | (( (8#$mode & 022) == 0 )) || fail 'The guest marker is writable by another account.' |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | kit=$(cd -P "$1" && pwd) |
| 20 | [[ $(stat -f %u "$kit") == 0 && -z $(find "$kit" \( -type l -o ! -user root -o -perm -0020 -o -perm -0002 \) -print -quit) ]] || |
| 21 | fail 'Use a root-owned kit with no symlinks or group/other-writable members.' |
| 22 | parent=$(dirname "$kit") |
| 23 | while [[ $parent != / ]]; do |
| 24 | mode=$(stat -f %Op "$parent") |
| 25 | [[ $(stat -f %u "$parent") == 0 ]] && (( (8#$mode & 022) == 0 || (8#$mode & 01000) != 0 )) || |
| 26 | fail 'The kit can be replaced through an untrusted parent directory.' |
| 27 | parent=$(dirname "$parent") |
| 28 | done |
| 29 | for file in macfuse.pkg cage-sshfs cage-macos-terminal; do [[ -f $kit/$file ]] || fail "Missing $file"; done |
| 30 | [[ -d $kit/CageProjectAccess.app ]] || fail 'Missing CageProjectAccess.app.' |
| 31 | umask 077 |
| 32 | work=$(mktemp -d /private/var/tmp/cage-project-kit.XXXXXXXX) |
| 33 | trap 'rm -rf "$work"' EXIT |
| 34 | for file in macfuse.pkg cage-sshfs cage-macos-terminal; do cp "$kit/$file" "$work/$file"; done |
| 35 | ditto "$kit/CageProjectAccess.app" "$work/CageProjectAccess.app" |
| 36 | pkg="$work/macfuse.pkg" |
| 37 | [[ $(shasum -a 256 "$pkg" | cut -d' ' -f1) == 5acf9e30bab227707de8d15eb28472a0cc9ed45709846b3e067d283294dc7f9e ]] || |
| 38 | fail 'The macFUSE installer SHA-256 does not match the pinned release.' |
| 39 | pkgutil --check-signature "$pkg" | grep -F 'Developer ID Installer: Benjamin Fleischer (3T5GSNBU6W)' >/dev/null || |
| 40 | fail 'Unexpected macFUSE installer signer.' |
| 41 | for file in cage-sshfs cage-macos-terminal; do |
| 42 | codesign --verify --strict --architecture arm64 "$work/$file" |
| 43 | done |
| 44 | access_id=dev.cage.guest.project-access |
| 45 | codesign --verify --strict -R "=identifier \"$access_id\"" "$work/CageProjectAccess.app" |
| 46 | macfuse_requirement='anchor apple generic and identifier "io.macfuse.app" and certificate leaf[subject.OU] = "3T5GSNBU6W"' |
| 47 | driver=/Library/Filesystems/macfuse.fs |
| 48 | vendor_app="$driver/Contents/Resources/macfuse.app" |
| 49 | installed='' |
| 50 | if [[ -e $driver ]]; then |
| 51 | codesign --verify --strict -R "=$macfuse_requirement" "$vendor_app" |
| 52 | installed=$(plutil -extract CFBundleShortVersionString raw -o - "$driver/Contents/Info.plist") |
| 53 | fi |
| 54 | up_to_date=no |
| 55 | if [[ -n $installed ]]; then |
| 56 | [[ $installed =~ ^([0-9]+)\.([0-9]+)(\.[0-9]+)?$ ]] || fail 'Unrecognized installed macFUSE version.' |
| 57 | if (( 10#${BASH_REMATCH[1]} > 5 || (10#${BASH_REMATCH[1]} == 5 && 10#${BASH_REMATCH[2]} >= 5) )); then up_to_date=yes; fi |
| 58 | fi |
| 59 | if [[ $up_to_date == no ]]; then |
| 60 | installer -pkg "$pkg" -target / |
| 61 | fi |
| 62 | codesign --verify --strict -R "=$macfuse_requirement" "$vendor_app" |
| 63 | |
| 64 | install_app() { |
| 65 | local source=$1 target=$2 requirement=$3 stage |
| 66 | if [[ -e $target || -L $target ]]; then |
| 67 | [[ ! -L $target && -d $target ]] || fail "Unexpected application at $target" |
| 68 | codesign --verify --strict -R "=$requirement" "$target" |
| 69 | if diff -qr "$source" "$target" >/dev/null; then return; fi |
| 70 | fail "A different verified build is already installed at $target. Stop and update that application before retrying." |
| 71 | fi |
| 72 | stage=$(mktemp -d /Applications/.cage-project-install.XXXXXXXX) |
| 73 | if ditto "$source" "$stage/app" && chmod -R a+rX,go-w "$stage/app" && |
| 74 | codesign --verify --strict -R "=$requirement" "$stage/app" && mv "$stage/app" "$target"; then |
| 75 | rmdir "$stage" |
| 76 | else |
| 77 | rm -rf "$stage" |
| 78 | fail "Could not install $target" |
| 79 | fi |
| 80 | } |
| 81 | install_app "$vendor_app" /Applications/macFUSE.app "$macfuse_requirement" |
| 82 | install_app "$work/CageProjectAccess.app" /Applications/CageProjectAccess.app "identifier \"$access_id\"" |
| 83 | libexec=/usr/local/libexec |
| 84 | mkdir -p "$libexec" |
| 85 | for file in cage-sshfs cage-macos-terminal; do |
| 86 | [[ ! -L $libexec/$file ]] || fail "Refusing to replace a linked executable: $file" |
| 87 | if [[ -f $libexec/$file ]] && cmp -s "$work/$file" "$libexec/$file"; then continue; fi |
| 88 | stage=$(mktemp "$libexec/.cage-tool.XXXXXXXX") |
| 89 | if install -o root -g wheel -m 755 "$work/$file" "$stage" && mv -f "$stage" "$libexec/$file"; then :; else |
| 90 | rm -f "$stage" |
| 91 | fail "Could not install $file" |
| 92 | fi |
| 93 | done |
| 94 | printf '%s\n' 'Project tools are ready. In the guest user session, open macFUSE.app once, then enable both macFUSE and macFUSE (local) under By Category > File System Extensions.' |