Skip to content

Commit

Permalink
util/osuser: run getent on non-Linux Unixes
Browse files Browse the repository at this point in the history
Remove the restriction that getent is skipped on non-Linux unixes.
Improve validation of the parsed output from getent, in case unknown
systems return unusable information.

Fixes tailscale#12730.

Signed-off-by: Ross Williams <ross@ross-williams.net>
  • Loading branch information
overhacked authored and bradfitz committed Jul 26, 2024
1 parent 6840f47 commit 1bf82dd
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions util/osuser/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ func LookupByUsername(username string) (*user.User, error) {
type lookupStd func(string) (*user.User, error)

func lookup(usernameOrUID string, std lookupStd, wantShell bool) (*user.User, string, error) {
// TODO(awly): we should use genet on more platforms, like FreeBSD.
if runtime.GOOS != "linux" {
// Skip getent entirely on Non-Unix platforms that won't ever have it.
// (Using HasPrefix for "wasip1", anticipating that WASI support will
// move beyond "preview 1" some day.)
if runtime.GOOS == "windows" || runtime.GOOS == "js" || strings.HasPrefix(runtime.GOOS, "wasi") {
u, err := std(usernameOrUID)
return u, "", err
}
Expand Down Expand Up @@ -129,6 +131,14 @@ func userLookupGetent(usernameOrUID string, std lookupStd) (*user.User, string,
for len(f) < 7 {
f = append(f, "")
}
var mandatoryFields = []int{0, 2, 3, 5}
for _, v := range mandatoryFields {
if f[v] == "" {
log.Printf("getent for user %q returned invalid output: %q", usernameOrUID, out)
u, err := std(usernameOrUID)
return u, "", err
}
}
return &user.User{
Username: f[0],
Uid: f[2],
Expand Down

0 comments on commit 1bf82dd

Please sign in to comment.