go: os/user: LookupUser() doesn't find users on macOS when compiled with CGO_ENABLED=0
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (go version)?
go version go1.9.2 darwin/amd64
Does this issue reproduce with the latest release?
Yes.
What operating system and processor architecture are you using (go env)?
GOARCH=“amd64” GOBIN=“” GOCACHE=“/home/jeff_walter/.cache/go-build” GOEXE=“” GOHOSTARCH=“amd64” GOHOSTOS=“freebsd” GOOS=“darwin” GOPATH=“/home/jeff_walter/src/agent” GORACE=“” GOROOT=“/usr/local/go” GOTMPDIR=“” GOTOOLDIR=“/usr/local/go/pkg/tool/freebsd_amd64” GCCGO=“gccgo” CC=“clang” CXX=“clang++” CGO_ENABLED=“0” CGO_CFLAGS=“-g -O2” CGO_CPPFLAGS=“” CGO_CXXFLAGS=“-g -O2” CGO_FFLAGS=“-g -O2” CGO_LDFLAGS=“-g -O2” PKG_CONFIG=“pkg-config” GOGCCFLAGS=“-fPIC -m64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build053729832=/tmp/go-build -gno-record-gcc-switches -fno-common”
What did you do?
If I run the following program with sudo I get a different result than I get when I run it as ‘someuser’ (where ‘someuser’ is my current logged in user).
package main
import (
"fmt"
"os/user"
)
func main() {
u, err := user.Lookup("someuser")
if err != nil {
fmt.Printf("%s", err)
return
}
fmt.Printf("%V", u)
}
What did you expect to see?
$ ./user &{%!V(string=502) %!V(string=20) %!V(string=someuser) %!V(string=) %!V(string=/Users/someuser)}
What did you see instead?
$ sudo ./user user: unknown user someuser
About this issue
- Original URL
- State: open
- Created 6 years ago
- Comments: 24 (11 by maintainers)
Commits related to this issue
- feat: implement semantically correct handling of shell/pty sessions across platforms The `shell` module now correctly handles `scp` and similar commands. This commit resolves 3 inter-related matters:... — committed to kadeessh/kadeessh by mohammed90 2 years ago
- feat: implement semantically correct handling of shell/pty sessions across platforms The `shell` module now correctly handles `scp` and similar commands. This commit resolves 3 inter-related matters:... — committed to kadeessh/kadeessh by mohammed90 2 years ago
So, to recap. Compiling with GCO_ENABLED=0 and calling user.Lookup() on a user other than yourself or root fails.
The following commands were run as jeff_walter:
The following commands were run as root:
I came across the problem and had a workaround by shell out to macOS’s directory service tool
dscacheutil. The credit goes to @tweekmonster https://github.com/tweekmonster/luser. The snippet is attached at the end of the comment.The root cause is macOS queries directory service instead of relying on
/etc/passwd, you can’t even find your own name (cat /etc/passwd | grep $USER). So when cgo is disabled,os/user.Lookupsimply checks/etc/passwdand finds nothing.This didn’t cause a big problem because we take another path when looking for current user and then fallback to
lookupUser.However, I think we should address this problem because:
Some possible solutions:
os/user.Lookupand stating when not using cgo or cross compile it will have problem on macOS, and user should shell out or use a third party library.os/user/lookup_darwin.goto usedscacheutil(see snippet) or put it under a supplementary package, x/sys seems to be the closest one but shells out is not low level syscall…References
man getpwnam_r(used by cgo impl) containsThese functions obtain information from opendirectoryd(8)on macOSSnippet
To build and run it on a mac
The output should be like NOTE: only using std without cgo failed to lookup myself from root
Apparently, macOS doesn’t really use the /etc/passwd file anymore. Apparently, you can get the user info by parsing some plist files.
See https://apple.stackexchange.com/a/186899