| 1 | package mac |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "io" |
| 7 | "io/fs" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "slices" |
| 11 | "sort" |
| 12 | "strconv" |
| 13 | "strings" |
| 14 | ) |
| 15 | |
| 16 | // Account is the macOS user the installer runs as. |
| 17 | type Account struct { |
| 18 | Name string |
| 19 | UID int |
| 20 | Groups []string |
| 21 | } |
| 22 | |
| 23 | // ReadAccount asks id(1): os/user without cgo cannot see the admin |
| 24 | // group's members, which Directory Services keeps. |
| 25 | func ReadAccount(ctx context.Context, x Exec) (Account, error) { |
| 26 | name, err1 := x(ctx, "/usr/bin/id", "-un") |
| 27 | uid, err2 := x(ctx, "/usr/bin/id", "-u") |
| 28 | groups, err3 := x(ctx, "/usr/bin/id", "-Gn") |
| 29 | if err := errors.Join(err1, err2, err3); err != nil { |
| 30 | return Account{}, err |
| 31 | } |
| 32 | n, err := strconv.Atoi(strings.TrimSpace(uid)) |
| 33 | if err != nil { |
| 34 | return Account{}, err |
| 35 | } |
| 36 | return Account{Name: strings.TrimSpace(name), UID: n, Groups: strings.Fields(groups)}, nil |
| 37 | } |
| 38 | |
| 39 | // ReadHome is name's home folder as the user directory records it. The |
| 40 | // environment's HOME is not used: under su or sudo it can be the |
| 41 | // caller's. |
| 42 | func ReadHome(ctx context.Context, x Exec, name string) (string, error) { |
| 43 | out, err := x(ctx, "/usr/bin/dscl", ".", "-read", "/Users/"+name, "NFSHomeDirectory") |
| 44 | if err != nil { |
| 45 | return "", err |
| 46 | } |
| 47 | for _, line := range strings.Split(out, "\n") { |
| 48 | if home, ok := strings.CutPrefix(line, "NFSHomeDirectory: "); ok && filepath.IsAbs(strings.TrimSpace(home)) { |
| 49 | return filepath.Clean(strings.TrimSpace(home)), nil |
| 50 | } |
| 51 | } |
| 52 | return "", errors.New("no home folder recorded for " + name) |
| 53 | } |
| 54 | |
| 55 | // runnerUser is a user made for Equipo: its short name has "equipo" in |
| 56 | // it, in any case (equipo-paul, or paulequipo as macOS's Add User dialog |
| 57 | // makes it from "Paul Equipo"). Paul, 2026-11-10. |
| 58 | func runnerUser(name string) bool { return strings.Contains(strings.ToLower(name), "equipo") } |
| 59 | |
| 60 | // RefuseAccount is the copy key that refuses this account, or "": not |
| 61 | // root, not an administrator, and with equipo in its name (own-Mac mode |
| 62 | // is not in scope). |
| 63 | func RefuseAccount(a Account) string { |
| 64 | switch { |
| 65 | case a.UID == 0 || a.Name == "root": |
| 66 | return "runner.mac.root" |
| 67 | case slices.Contains(a.Groups, "admin") || slices.Contains(a.Groups, "wheel"): |
| 68 | return "runner.mac.admin" |
| 69 | case !runnerUser(a.Name): |
| 70 | return "runner.mac.user" |
| 71 | } |
| 72 | return "" |
| 73 | } |
| 74 | |
| 75 | // OtherHomes are the homes under users that are not home: every other |
| 76 | // person's (and /Users/Shared), which every profile denies. A home that |
| 77 | // is a symbolic link is listed as the link and as where it leads. An |
| 78 | // error reading users is returned: homes that cannot be listed are not |
| 79 | // known to be isolated. |
| 80 | func OtherHomes(users, home string) ([]string, error) { |
| 81 | entries, err := os.ReadDir(users) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | var out []string |
| 86 | for _, e := range entries { |
| 87 | p := filepath.Join(users, e.Name()) |
| 88 | if strings.HasPrefix(e.Name(), ".") || p == home { |
| 89 | continue |
| 90 | } |
| 91 | switch { |
| 92 | case e.IsDir(): |
| 93 | out = append(out, p) |
| 94 | case e.Type()&fs.ModeSymlink != 0: |
| 95 | out = append(out, p) |
| 96 | if target, err := filepath.EvalSymlinks(p); err == nil && target != home { |
| 97 | out = append(out, target) |
| 98 | } else if err == nil { |
| 99 | // A link to this user's own home is not another's. |
| 100 | out = out[:len(out)-1] |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | sort.Strings(out) |
| 105 | return slices.Compact(out), nil |
| 106 | } |
| 107 | |
| 108 | // ExposedHomes are the people's homes this user can get into: the |
| 109 | // installer refuses to enrol while there is one (spec §3.5), and P4 |
| 110 | // fails. |
| 111 | func ExposedHomes(homes []string, exposed func(string) bool) []string { |
| 112 | var out []string |
| 113 | for _, h := range homes { |
| 114 | if filepath.Base(h) != "Shared" && exposed(h) { |
| 115 | out = append(out, h) |
| 116 | } |
| 117 | } |
| 118 | return out |
| 119 | } |
| 120 | |
| 121 | // homeSentinels are files most homes have; reading any is reading the |
| 122 | // home. They stand in for the sentinels P4 cannot place in another |
| 123 | // person's home. |
| 124 | var homeSentinels = []string{".zshrc", ".zprofile", ".bash_profile", ".profile", ".gitconfig", "Library/Preferences/.GlobalPreferences.plist"} |
| 125 | |
| 126 | // Exposed reports whether this user can list home, pass through it, or |
| 127 | // read a file in it. Only a refusal (permission denied) of each counts |
| 128 | // as closed; any other answer, an error included, is exposed. |
| 129 | func Exposed(home string) bool { |
| 130 | closed := func(err error) bool { return errors.Is(err, fs.ErrPermission) } |
| 131 | if f, err := os.Open(home); err == nil { |
| 132 | _, rerr := f.Readdirnames(1) |
| 133 | f.Close() |
| 134 | if rerr == nil || errors.Is(rerr, io.EOF) || !closed(rerr) { |
| 135 | return true |
| 136 | } |
| 137 | } else if !closed(err) { |
| 138 | return true |
| 139 | } |
| 140 | // Passing through: a name that does not exist answers "not found" |
| 141 | // to someone who may search the folder, "permission denied" otherwise. |
| 142 | if _, err := os.Lstat(filepath.Join(home, ".equipo-traversal-probe")); !closed(err) { |
| 143 | return true |
| 144 | } |
| 145 | for _, s := range homeSentinels { |
| 146 | if f, err := os.Open(filepath.Join(home, s)); err == nil { |
| 147 | f.Close() |
| 148 | return true |
| 149 | } else if !closed(err) && !errors.Is(err, fs.ErrNotExist) { |
| 150 | return true |
| 151 | } |
| 152 | } |
| 153 | return false |
| 154 | } |
| 155 | |