Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os/user: add pure Go LookupGroup when cgo isn't available #18102

Closed
kevinburke opened this issue Nov 29, 2016 · 9 comments
Closed

os/user: add pure Go LookupGroup when cgo isn't available #18102

kevinburke opened this issue Nov 29, 2016 · 9 comments

Comments

@kevinburke
Copy link
Contributor

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?

tip

Currently when you cross compile Go for a Unix target, calls to user.LookupGroup fail with user: LookupGroup requires cgo. Under the hood, LookupGroup calls getgrnam_r from C.

Is there any interest in replacing this with a pure Go function? We'd probably need a clean rewrite, but here's some Go code that parses /etc/group, so it's at least possible. https://github.com/opencontainers/runc/blob/master/libcontainer/user/user.go#L141

The other consideration (reading through libc) is that apparently getgrnam checks other sources besides /etc/group:

The getgrnam() function returns a pointer to a structure containing the broken-out fields of the record in the group database (e.g., the local group file /etc/group, NIS, and LDAP) that matches the group name name.

It might be trickier to replicate that other behavior.

@bradfitz
Copy link
Contributor

Feel free to add /etc/group parsing code for Unix in the no-cgo case, but the default shouldn't change: when cgo is available, use cgo.

There's no way to write a pure Go function, since the configuration of libc's user/groups allows arbitrary shared libraries to be loaded to do the work.

@bradfitz bradfitz changed the title proposal: os/user: don't rely on cgo for LookupGroup os/user: add pure Go LookupGroup when cgo isn't available Nov 29, 2016
@bradfitz bradfitz added this to the Unplanned milestone Nov 29, 2016
@minux
Copy link
Member

minux commented Nov 30, 2016 via email

@bradfitz
Copy link
Contributor

But DNS is common. LookupUser and LookGroup aren't, I imagine.

@minux
Copy link
Member

minux commented Nov 30, 2016 via email

@kevinburke
Copy link
Contributor Author

I've started working on this. Related to @minux's comment, should we respect the group setting in /etc/nsswitch.conf, if it exists? There's code to parse nsswitch.conf in net, but it's not currently exported.

@bradfitz
Copy link
Contributor

The parsing should probably move to a new internal package if it's to be shared.

But I'm not sure how much it matters. What's an interesting case that parsing nsswitch.conf solves? It seems that it should either be simple (no cgo, only parsing /etc/group) or complete (cgo). Things in-between seem "meh" unless there's a common use case or common configuration that you're looking to address. What do you have in mind?

@kevinburke
Copy link
Contributor Author

To be honest I just want to cross compile and find the GID for a groupname. But I figured if I was going to write a patch I could try to match libc, to a degree. I'll ignore it for now.

@gopherbot
Copy link

CL https://golang.org/cl/33713 mentions this issue.

@bradfitz bradfitz modified the milestones: Go1.9Maybe, Unplanned Feb 22, 2017
@gopherbot
Copy link

CL https://golang.org/cl/37497 mentions this issue.

gopherbot pushed a commit that referenced this issue Feb 27, 2017
In another CL, I'll add a pure Go implementation of lookupGroup and
lookupGroupId in lookup_unix.go, but attempting that in one CL makes
the diff too difficult to read.

Updates #18102.

Change-Id: If8e26cee5efd30385763430f34304c70165aef32
Reviewed-on: https://go-review.googlesource.com/37497
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Feb 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants