| 1 | #!/bin/bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | export PATH=/usr/bin:/bin:/usr/sbin:/sbin |
| 5 | [[ $(sysctl -n hw.model) =~ ^VirtualMac[0-9]+,[0-9]+$ && $(cat /var/db/cage-guest-os) == macos ]] || exit 2 |
| 6 | [[ $# == 0 || ( $# == 1 && $1 == --prepare ) ]] || exit 2 |
| 7 | uid=$(id -u) |
| 8 | [[ $uid -ge 500 && ( ${1:-} == --prepare || $(stat -f %u /dev/console) == "$uid" ) ]] || { echo 'Log into the guest as the intended desktop user.' >&2; exit 4; } |
| 9 | app='/Applications/Cage Guest Clipboard.app' |
| 10 | codesign --verify --deep --strict -R '=identifier "dev.cage.GuestClipboard"' "$app" |
| 11 | label=dev.cage.GuestClipboard |
| 12 | cache="$HOME/Library/Caches/$label" |
| 13 | agent="$HOME/Library/LaunchAgents/$label.plist" |
| 14 | umask 077 |
| 15 | mkdir -p "$HOME/Library/LaunchAgents" "$cache" |
| 16 | [[ ! -L $cache && $(stat -f %u "$cache") == "$uid" ]] || exit 2 |
| 17 | chmod 700 "$cache" |
| 18 | temp=$(mktemp "$cache/plist.XXXXXXXX") |
| 19 | trap 'rm -f "$temp"' EXIT |
| 20 | plutil -create xml1 "$temp" |
| 21 | plutil -insert Label -string "$label" "$temp" |
| 22 | plutil -insert ProgramArguments -xml '<array/>' "$temp" |
| 23 | plutil -insert ProgramArguments.0 -string "$app/Contents/MacOS/CageGuestClipboard" "$temp" |
| 24 | plutil -insert LimitLoadToSessionType -string Aqua "$temp" |
| 25 | plutil -insert Sockets -xml '<dict><key>Clipboard</key><dict/></dict>' "$temp" |
| 26 | plutil -insert Sockets.Clipboard.SockPathName -string "$cache/clipboard.sock" "$temp" |
| 27 | plutil -insert Sockets.Clipboard.SockPathMode -integer 384 "$temp" |
| 28 | if [[ -f $agent ]] && ! cmp -s "$agent" "$temp"; then |
| 29 | echo 'An existing clipboard LaunchAgent has different settings.' >&2 |
| 30 | exit 2 |
| 31 | fi |
| 32 | mv -f "$temp" "$agent" |
| 33 | [[ ${1:-} == --prepare ]] && exit 0 |
| 34 | if ! launchctl print "gui/$uid/$label" >/dev/null 2>&1; then |
| 35 | launchctl bootstrap "gui/$uid" "$agent" |
| 36 | fi |