Commit 3a18f0ec authored by Kevin Burke's avatar Kevin Burke

os/user: retrieve Current username from /etc/passwd, not $USER

Per golang/go#27524 there are situations where the username for the
uid does not match the value in the $USER environment variable and it
seems sensible to choose the value in /etc/passwd when they disagree.

This may make the Current() call slightly more expensive, since we
read /etc/passwd with cgo disabled instead of just checking the
environment. However, we cache the result of Current() calls, so we
only invoke this cost once in the lifetime of the process.

Fixes #14626.
Fixes #27524.
Updates #24884.

Change-Id: I0dcd224cf7f61dd5292f3fcc363aa2e9656a2cb1
Reviewed-on: https://go-review.googlesource.com/134218Reviewed-by: default avatarIan Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 1705962c
...@@ -19,8 +19,15 @@ func init() { ...@@ -19,8 +19,15 @@ func init() {
} }
func current() (*User, error) { func current() (*User, error) {
u := &User{ uid := currentUID()
Uid: currentUID(), // $USER and /etc/passwd may disagree; prefer the latter if we can get it.
// See issue 27524 for more information.
u, err := lookupUserId(uid)
if err == nil {
return u, nil
}
u = &User{
Uid: uid,
Gid: currentGID(), Gid: currentGID(),
Username: os.Getenv("USER"), Username: os.Getenv("USER"),
Name: "", // ignored Name: "", // ignored
......
...@@ -5,33 +5,18 @@ ...@@ -5,33 +5,18 @@
package user package user
import ( import (
"internal/testenv"
"os"
"runtime" "runtime"
"testing" "testing"
) )
func checkUser(t *testing.T) { func checkUser(t *testing.T) {
t.Helper()
if !userImplemented { if !userImplemented {
t.Skip("user: not implemented; skipping tests") t.Skip("user: not implemented; skipping tests")
} }
} }
func TestCurrent(t *testing.T) { func TestCurrent(t *testing.T) {
// The Go builders (in particular the ones using containers)
// often have minimal environments without $HOME or $USER set,
// which breaks Current which relies on those working as a
// fallback.
// TODO: we should fix that (Issue 24884) and remove these
// workarounds.
if testenv.Builder() != "" && runtime.GOOS != "windows" && runtime.GOOS != "plan9" {
if os.Getenv("HOME") == "" {
os.Setenv("HOME", "/tmp")
}
if os.Getenv("USER") == "" {
os.Setenv("USER", "gobuilder")
}
}
u, err := Current() u, err := Current()
if err != nil { if err != nil {
t.Fatalf("Current: %v (got %#v)", err, u) t.Fatalf("Current: %v (got %#v)", err, u)
...@@ -108,6 +93,7 @@ func TestLookupId(t *testing.T) { ...@@ -108,6 +93,7 @@ func TestLookupId(t *testing.T) {
} }
func checkGroup(t *testing.T) { func checkGroup(t *testing.T) {
t.Helper()
if !groupImplemented { if !groupImplemented {
t.Skip("user: group not implemented; skipping test") t.Skip("user: group not implemented; skipping test")
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment