| 1 | #!/bin/bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | export PATH=/usr/bin:/bin:/usr/sbin:/sbin |
| 5 | [[ ( $# == 1 || ( $# == 2 && $2 == --update-tools ) ) && $EUID == 0 && $(sysctl -n hw.model) =~ ^VirtualMac[0-9]+,[0-9]+$ ]] |
| 6 | [[ -f /var/db/cage-guest-os && ! -L /var/db/cage-guest-os && $(stat -f %u /var/db/cage-guest-os) == 0 && $(cat /var/db/cage-guest-os) == macos ]] |
| 7 | media=$(cd "$1" && pwd) |
| 8 | base=/usr/local/libexec |
| 9 | tools="$base/cage-workbench-browser-4.145.0-pw1.65.0" |
| 10 | mkdir -p "$base" |
| 11 | stage=$(mktemp -d "$base/.cage-browser.XXXXXXXX") |
| 12 | volume="$stage/volume" |
| 13 | trap 'if mount | grep -Fq " on $volume ("; then hdiutil detach "$volume" >/dev/null; fi; rm -rf "$stage"' EXIT |
| 14 | umask 022 |
| 15 | verify() { |
| 16 | [[ $(shasum -a 256 "$1" | cut -d' ' -f1) == "$2" ]] || { echo "Unexpected package digest: $1" >&2; exit 1; } |
| 17 | } |
| 18 | if [[ ! -x $tools/editor/lib/node || ! -f $tools/playwright/package.json ]]; then |
| 19 | mkdir -p "$stage/tools/editor" "$stage/tools/playwright" |
| 20 | cp "$media/editor.tgz" "$media/playwright.tgz" "$stage/" |
| 21 | verify "$stage/editor.tgz" a79774d8d34eed785c1f831bc0125f7534bc21a0039bef8e8a2aa32463f9421a |
| 22 | verify "$stage/playwright.tgz" 315247c55a793ca760b2f05bdcee0e6a53fd65b1f22d1d3de8323c8c9395add3 |
| 23 | tar -xzf "$stage/editor.tgz" -C "$stage/tools/editor" --no-same-owner --strip-components 1 |
| 24 | tar -xzf "$stage/playwright.tgz" -C "$stage/tools/playwright" --no-same-owner --strip-components 1 |
| 25 | "$stage/tools/editor/lib/node" --version |
| 26 | mkdir -p "$tools" |
| 27 | for component in editor playwright; do |
| 28 | [[ ! -L $tools/$component ]] |
| 29 | if [[ -e $tools/$component ]]; then mv "$tools/$component" "$stage/previous-$component"; fi |
| 30 | if ! mv "$stage/tools/$component" "$tools/$component"; then |
| 31 | if [[ -d $stage/previous-$component ]]; then mv "$stage/previous-$component" "$tools/$component"; fi |
| 32 | exit 1 |
| 33 | fi |
| 34 | done |
| 35 | fi |
| 36 | app='/Applications/Google Chrome.app' |
| 37 | requirement='anchor apple generic and identifier "com.google.Chrome" and certificate leaf[subject.OU] = "EQHXZ8M8AV"' |
| 38 | if [[ ! -e $app || ${2:-} == --update-tools ]]; then |
| 39 | cp "$media/chrome.dmg" "$stage/chrome.dmg" |
| 40 | mkdir "$volume" |
| 41 | hdiutil attach -readonly -nobrowse -noautoopen -mountpoint "$volume" "$stage/chrome.dmg" >/dev/null |
| 42 | spctl --assess --type execute "$volume/Google Chrome.app" |
| 43 | ditto --norsrc "$volume/Google Chrome.app" "$stage/Google Chrome.app" |
| 44 | codesign --verify --strict --deep -R="$requirement" "$stage/Google Chrome.app" |
| 45 | spctl --assess --type execute "$stage/Google Chrome.app" |
| 46 | install_chrome=yes |
| 47 | if [[ -e $app ]]; then |
| 48 | [[ -d $app && ! -L $app ]] |
| 49 | codesign --verify --strict -R="$requirement" "$app" |
| 50 | current=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$app/Contents/Info.plist") |
| 51 | incoming=$(/usr/libexec/PlistBuddy -c 'Print :CFBundleShortVersionString' "$stage/Google Chrome.app/Contents/Info.plist") |
| 52 | [[ $current =~ ^[0-9]+(\.[0-9]+)*$ && $incoming =~ ^[0-9]+(\.[0-9]+)*$ ]] |
| 53 | if ! /usr/bin/perl -e '@a=split(/\./,$ARGV[0]);@b=split(/\./,$ARGV[1]);for($i=0;$i<@a||$i<@b;$i++){exit(($a[$i]//0)>($b[$i]//0)?0:1) if ($a[$i]//0)!=($b[$i]//0)}exit 1' "$incoming" "$current"; then install_chrome=no; fi |
| 54 | fi |
| 55 | if [[ $install_chrome == yes ]]; then |
| 56 | if [[ -e $app ]]; then mv "$app" "$stage/previous-chrome.app"; fi |
| 57 | if ! mv "$stage/Google Chrome.app" "$app"; then |
| 58 | if [[ -d $stage/previous-chrome.app ]]; then mv "$stage/previous-chrome.app" "$app"; fi |
| 59 | exit 1 |
| 60 | fi |
| 61 | fi |
| 62 | fi |
| 63 | codesign --verify --strict -R="$requirement" "$app" |
| 64 | sources=$(cd "$(dirname "$0")" && pwd) |
| 65 | install -m 644 "$sources/macos-browser.mjs" "$base/cage-macos-browser.mjs" |
| 66 | install -m 755 "$sources/cage-macos-editor" "$base/cage-macos-editor" |
| 67 | if [[ -f "$media/native-browser/macos-native-browser.mjs" ]]; then |
| 68 | cp -R "$media/native-browser" "$stage/controller" |
| 69 | else |
| 70 | "$sources/package-macos-browser.sh" "$stage/controller" |
| 71 | fi |
| 72 | install -d "$tools/controller" "$tools/controller/modules" "$tools/controller/split-extension" |
| 73 | install -m 644 "$stage/controller/"*.mjs "$tools/controller/" |
| 74 | install -m 755 "$stage/controller/cage-tailnet" "$tools/controller/cage-tailnet" |
| 75 | install -m 644 "$stage/controller/modules/"*.mjs "$tools/controller/modules/" |
| 76 | install -m 644 "$stage/controller/split-extension/"* "$tools/controller/split-extension/" |