| 1 | |
| 2 | |
| 3 | package maclogin |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "os/exec" |
| 8 | |
| 9 | "al.essio.dev/pkg/shellescape" |
| 10 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 11 | ) |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | func guestGUIProbeCommand(ctx context.Context, profile, administrator, user string, arguments ...string) (*exec.Cmd, error) { |
| 17 | args, err := guestos.MacOSSSHArgs(profile, administrator) |
| 18 | if err != nil { |
| 19 | return nil, err |
| 20 | } |
| 21 | rootScript := `set -eu |
| 22 | app='/Applications/Cage Guest Window.app' |
| 23 | for file in "$app" "$app/Contents" "$app/Contents/MacOS" "$app/Contents/MacOS/CageGuestWindow"; do |
| 24 | test ! -L "$file" && test "$(/usr/bin/stat -f %u "$file")" = 0 || exit 72 |
| 25 | mode=$(/usr/bin/stat -f %Lp "$file") |
| 26 | test "$((0$mode & 022))" = 0 || exit 72 |
| 27 | done |
| 28 | /usr/bin/codesign --verify --strict -R '=identifier "dev.cage.GuestClaudeWindow"' "$app" || exit 72 |
| 29 | uid=$(/usr/bin/id -u ` + shellescape.Quote(user) + `) |
| 30 | test "$(/usr/bin/stat -f %u /dev/console)" = "$uid" || exit 73 |
| 31 | exec /bin/launchctl asuser "$uid" /usr/bin/sudo -Hu ` + shellescape.Quote(user) + ` /usr/bin/env -i HOME=` + shellescape.Quote("/Users/"+user) + ` PATH=/usr/bin:/bin ` + |
| 32 | shellescape.QuoteCommand(append([]string{"/Applications/Cage Guest Window.app/Contents/MacOS/CageGuestWindow"}, arguments...)) |
| 33 | |
| 34 | |
| 35 | |
| 36 | loginScript := `set -eu |
| 37 | umask 077 |
| 38 | probe_dir=$(/usr/bin/mktemp -d ` + shellescape.Quote("/Users/"+administrator+"/.cage-probe.XXXXXXXX") + `) |
| 39 | trap '/bin/rm -f "$probe_dir/askpass"; /bin/rmdir "$probe_dir"' EXIT |
| 40 | trap 'exit 1' HUP INT TERM |
| 41 | /usr/bin/printf '%s\n' '#!/bin/sh' ` + shellescape.Quote("exec /bin/cat "+shellescape.Quote("/Users/"+administrator+"/password")) + ` > "$probe_dir/askpass" |
| 42 | /bin/chmod 700 "$probe_dir/askpass" |
| 43 | SUDO_ASKPASS="$probe_dir/askpass" /usr/bin/sudo -A -p '' /bin/sh -c ` + shellescape.Quote(rootScript) |
| 44 | command := "exec /bin/sh -c " + shellescape.Quote(loginScript) |
| 45 | cmd := exec.CommandContext(ctx, "/usr/bin/ssh", append(args, "-T", "cage-macos", "--", command)...) |
| 46 | cmd.Env = []string{"PATH=/usr/bin:/bin", "LC_ALL=C"} |
| 47 | return cmd, nil |
| 48 | } |