| 1 | package workbench |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "github.com/google/uuid" |
| 9 | "os" |
| 10 | "os/exec" |
| 11 | "path/filepath" |
| 12 | "regexp" |
| 13 | "strings" |
| 14 | "time" |
| 15 | |
| 16 | "al.essio.dev/pkg/shellescape" |
| 17 | "github.com/sylvinus/agent-vm/internal/guestos" |
| 18 | "github.com/sylvinus/agent-vm/internal/host" |
| 19 | ) |
| 20 | |
| 21 | func (s *Server) syncMacGuestPassword(ctx context.Context) error { |
| 22 | if s.macosGuest == nil || s.account == nil { |
| 23 | return nil |
| 24 | } |
| 25 | install, err := guestos.LoadInstallation(s.profile) |
| 26 | if err != nil { |
| 27 | return err |
| 28 | } |
| 29 | if install == nil { |
| 30 | return nil |
| 31 | } |
| 32 | return s.account.syncMacPassword(ctx, install.ID, s.macosGuest.User, func(ctx context.Context, oldPassword, newPassword string) error { |
| 33 | return s.applyMacGuestPassword(ctx, install.ID, oldPassword, newPassword) |
| 34 | }) |
| 35 | } |
| 36 | |
| 37 | func (s *Server) applyMacGuestPassword(ctx context.Context, id uuid.UUID, oldPassword, newPassword string) error { |
| 38 | ctx, cancel := context.WithTimeout(ctx, 45*time.Second) |
| 39 | defer cancel() |
| 40 | provision, err := guestos.MacOSProvisionUser(s.profile) |
| 41 | if err != nil { |
| 42 | return err |
| 43 | } |
| 44 | if provision != "cageprovision" { |
| 45 | return errors.New("客机置备身份不匹配") |
| 46 | } |
| 47 | args, err := guestos.MacOSSSHArgs(s.profile, provision) |
| 48 | if err != nil { |
| 49 | return err |
| 50 | } |
| 51 | command := func(ctx context.Context, script string) *exec.Cmd { |
| 52 | cmd := exec.CommandContext(ctx, sshExecutable(), append(append([]string{}, args...), "-T", "cage-macos", "--", "exec /bin/sh -ec "+shellescape.Quote(script))...) |
| 53 | cmd.Env = sshEnvironment() |
| 54 | host.HideCommandWindow(cmd) |
| 55 | return cmd |
| 56 | } |
| 57 | |
| 58 | |
| 59 | helper, err := os.ReadFile(filepath.Join(filepath.Dir(s.exe), "guest-os", "cage-finish-initial-setup")) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | out, err := command(ctx, `umask 077; mktemp -d /private/var/tmp/cage-account.XXXXXXXX`).Output() |
| 64 | stage := strings.TrimSpace(string(out)) |
| 65 | if err != nil || !regexp.MustCompile(`^/private/var/tmp/cage-account\.[A-Za-z0-9]{8}$`).MatchString(stage) { |
| 66 | return errors.New("无法准备客机改密组件") |
| 67 | } |
| 68 | admin := " < /Users/" + provision + "/password" |
| 69 | defer func() { |
| 70 | cleanup, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 71 | defer cancel() |
| 72 | _ = command(cleanup, `sudo -S -p '' /bin/rm -rf `+stage+admin).Run() |
| 73 | }() |
| 74 | upload := command(ctx, `umask 077; cat > `+stage+`/helper`) |
| 75 | upload.Stdin = bytes.NewReader(helper) |
| 76 | if err = upload.Run(); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | script := `chown -R root:wheel ` + stage + `; chmod 700 ` + stage + ` ` + stage + `/helper; chmod 600 ` + stage + `/input; exec ` + stage + `/helper password ` + id.String() + ` ` + s.macosGuest.User + ` < ` + stage + `/input` |
| 80 | apply := command(ctx, `umask 077; cat > `+stage+`/input; sudo -S -p '' /bin/sh -ec `+shellescape.Quote(script)+admin) |
| 81 | apply.Stdin = strings.NewReader(oldPassword + "\n" + newPassword + "\n") |
| 82 | if out, err := apply.CombinedOutput(); err != nil { |
| 83 | return fmt.Errorf("guest password update: %w: %.2048s", err, out) |
| 84 | } |
| 85 | return nil |
| 86 | } |