util/osuser: run getent on non-Linux Unixes

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 #12730.

Signed-off-by: Ross Williams <ross@ross-williams.net>
This commit is contained in:
Ross Williams 2024-07-07 19:57:45 -04:00 committed by Brad Fitzpatrick
parent 6840f471c0
commit 1bf82ddf84

View File

@ -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
}
@ -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],