package mac import ( "context" "errors" "io" "io/fs" "os" "path/filepath" "slices" "sort" "strconv" "strings" ) // Account is the macOS user the installer runs as. type Account struct { Name string UID int Groups []string } // ReadAccount asks id(1): os/user without cgo cannot see the admin // group's members, which Directory Services keeps. func ReadAccount(ctx context.Context, x Exec) (Account, error) { name, err1 := x(ctx, "/usr/bin/id", "-un") uid, err2 := x(ctx, "/usr/bin/id", "-u") groups, err3 := x(ctx, "/usr/bin/id", "-Gn") if err := errors.Join(err1, err2, err3); err != nil { return Account{}, err } n, err := strconv.Atoi(strings.TrimSpace(uid)) if err != nil { return Account{}, err } return Account{Name: strings.TrimSpace(name), UID: n, Groups: strings.Fields(groups)}, nil } // ReadHome is name's home folder as the user directory records it. The // environment's HOME is not used: under su or sudo it can be the // caller's. func ReadHome(ctx context.Context, x Exec, name string) (string, error) { out, err := x(ctx, "/usr/bin/dscl", ".", "-read", "/Users/"+name, "NFSHomeDirectory") if err != nil { return "", err } for _, line := range strings.Split(out, "\n") { if home, ok := strings.CutPrefix(line, "NFSHomeDirectory: "); ok && filepath.IsAbs(strings.TrimSpace(home)) { return filepath.Clean(strings.TrimSpace(home)), nil } } return "", errors.New("no home folder recorded for " + name) } // runnerUser is a user made for Equipo: its short name has "equipo" in // it, in any case (equipo-paul, or paulequipo as macOS's Add User dialog // makes it from "Paul Equipo"). Paul, 2026-11-10. func runnerUser(name string) bool { return strings.Contains(strings.ToLower(name), "equipo") } // RefuseAccount is the copy key that refuses this account, or "": not // root, not an administrator, and with equipo in its name (own-Mac mode // is not in scope). func RefuseAccount(a Account) string { switch { case a.UID == 0 || a.Name == "root": return "runner.mac.root" case slices.Contains(a.Groups, "admin") || slices.Contains(a.Groups, "wheel"): return "runner.mac.admin" case !runnerUser(a.Name): return "runner.mac.user" } return "" } // OtherHomes are the homes under users that are not home: every other // person's (and /Users/Shared), which every profile denies. A home that // is a symbolic link is listed as the link and as where it leads. An // error reading users is returned: homes that cannot be listed are not // known to be isolated. func OtherHomes(users, home string) ([]string, error) { entries, err := os.ReadDir(users) if err != nil { return nil, err } var out []string for _, e := range entries { p := filepath.Join(users, e.Name()) if strings.HasPrefix(e.Name(), ".") || p == home { continue } switch { case e.IsDir(): out = append(out, p) case e.Type()&fs.ModeSymlink != 0: out = append(out, p) if target, err := filepath.EvalSymlinks(p); err == nil && target != home { out = append(out, target) } else if err == nil { // A link to this user's own home is not another's. out = out[:len(out)-1] } } } sort.Strings(out) return slices.Compact(out), nil } // ExposedHomes are the people's homes this user can get into: the // installer refuses to enrol while there is one (spec ยง3.5), and P4 // fails. func ExposedHomes(homes []string, exposed func(string) bool) []string { var out []string for _, h := range homes { if filepath.Base(h) != "Shared" && exposed(h) { out = append(out, h) } } return out } // homeSentinels are files most homes have; reading any is reading the // home. They stand in for the sentinels P4 cannot place in another // person's home. var homeSentinels = []string{".zshrc", ".zprofile", ".bash_profile", ".profile", ".gitconfig", "Library/Preferences/.GlobalPreferences.plist"} // Exposed reports whether this user can list home, pass through it, or // read a file in it. Only a refusal (permission denied) of each counts // as closed; any other answer, an error included, is exposed. func Exposed(home string) bool { closed := func(err error) bool { return errors.Is(err, fs.ErrPermission) } if f, err := os.Open(home); err == nil { _, rerr := f.Readdirnames(1) f.Close() if rerr == nil || errors.Is(rerr, io.EOF) || !closed(rerr) { return true } } else if !closed(err) { return true } // Passing through: a name that does not exist answers "not found" // to someone who may search the folder, "permission denied" otherwise. if _, err := os.Lstat(filepath.Join(home, ".equipo-traversal-probe")); !closed(err) { return true } for _, s := range homeSentinels { if f, err := os.Open(filepath.Join(home, s)); err == nil { f.Close() return true } else if !closed(err) && !errors.Is(err, fs.ErrNotExist) { return true } } return false }