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

integrate github.com/opencontainers/runc/libcontainer/user (alternative) #134

Merged
merged 51 commits into from Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
9331e29
Move UserLookup functionality into a separate pkg/user submodule that…
tianon Dec 27, 2013
5e76f74
Use type switch instead of reflection
crosbymichael Jan 17, 2014
b64c71e
Check uid ranges
LK4D4 May 17, 2014
e70ad14
Move "pkg/user" into libcontainer and add support for GetUserGroupSup…
tianon Jul 29, 2014
ef3397c
user: *: refactor and expand libcontainer/user API
cyphar Aug 14, 2014
03c82a3
user: lookup: added os/user-like lookup API
cyphar Aug 29, 2014
3b425cc
user: add unit tests for GetExecUser
cyphar Aug 14, 2014
52e6df1
Merge remote-tracking branch 'origin/master' into api
avagin Jan 27, 2015
6f2328c
Lookup additional groups in the container.
mrunalp Apr 30, 2015
c1a87e3
refactor GetAdditionalGroupsPath
dqminh May 25, 2015
f975a51
libcontainer: user: fix GetAdditionalGroupsPath to match API
cyphar Jun 28, 2015
78033bd
libcontainer: user: update tests for GetAdditionalGroups
cyphar Jun 28, 2015
652fbaa
Allow numeric groups for containers without /etc/group
Oct 4, 2015
23eb47f
Export user and group lookup errors as variables.
Mar 17, 2016
92e1773
libcontainer: user: always treat numeric ids numerically
cyphar Mar 30, 2016
a4b5eb7
libcontainer: user: add tests for numeric user specifications
cyphar Mar 30, 2016
496e469
libcontainer: user: general cleanups
cyphar Mar 30, 2016
91968cc
Fix TestGetAdditionalGroups on i686
hqhq Sep 27, 2016
c006cd2
Cleanup: remove redundant code
coolljt0725 Jan 9, 2017
6351002
user: fix the parameter error
datawolf Jan 18, 2017
b0f54ba
Move libcontainer to x/sys/unix
clnperez May 9, 2017
8cb7a3a
libcontainer/user: add supplementary groups only for non-numeric users
vrothberg May 16, 2017
b69bd1b
Move user pkg unix specific calls to unix file
mlaventure Aug 3, 2017
83ce763
Revert "Merge pull request #1450 from vrothberg/sgid-non-numeric"
mlaventure Aug 4, 2017
5123b0a
remove placeholder for non-linux platforms
dqminh Nov 16, 2017
3d7cc23
libcontainer/user: platform dependent calls
vbatts Feb 28, 2018
0eb4b05
libcontainer: add parser for /etc/sub{u,g}id and /proc/PID/{u,g}id_map
AkihiroSuda Jan 16, 2018
bd84305
libcontainer: fix compilation on GOARCH=arm GOARM=6 (32 bits)
Jun 14, 2018
c608377
libcontainer: CurrentGroupSubGIDs -> CurrentUserSubGIDs
AkihiroSuda Aug 28, 2018
afa0917
Nit: fix use of bufio.Scanner.Err
kolyshkin Mar 27, 2020
aeea88e
make sure pty.Close() will be called and fix comment
tjucoder Jul 5, 2020
b3750af
use string-concatenation instead of sprintf for simple cases
thaJeztah Sep 29, 2020
581a9d4
Fixing some lint issues
knabben Oct 3, 2020
efcad6e
Fix int overflow in test on 32 bit system
zhsj Jan 23, 2021
c49050c
Move fuzzers upstream
AdamKorcz Mar 8, 2021
7736478
libct/user: rm windows code
kolyshkin Nov 30, 2020
50e9abc
libcontainer/user: fix windows compile error
thaJeztah Mar 14, 2021
48fed46
libcontainer/user: remove outdated MAINTAINERS file
thaJeztah Mar 14, 2021
3e3208c
Use gofumpt to format code
kolyshkin Jun 1, 2021
da08f02
libcontainer/user: remove unused ErrUnsupported
thaJeztah May 27, 2021
edeacd3
libcontainer/user: fix capitalization (golint)
thaJeztah Jun 1, 2021
da5b9b2
Replace fmt.Errorf w/o %-style to errors.New
kolyshkin Jun 8, 2021
013c244
*: fmt.Errorf: use %w when appropriate
kolyshkin Jun 9, 2021
02cfd3c
libct/user: use []byte more, avoid allocations
kolyshkin Jul 2, 2021
a04d4c7
libct/user: ParseGroupFilter: use TrimSpace
kolyshkin Jul 2, 2021
330eb4c
libct/user: fix parsing long /etc/group lines
kolyshkin Jul 2, 2021
9f62d16
*: add go-1.17+ go:build tags
kolyshkin Aug 31, 2021
bad3316
libct: fixes for godoc 1.19
kolyshkin Aug 3, 2022
9820685
ci: bump golangci-lint, remove fixed exception
kolyshkin Jun 28, 2023
51d0ba7
integrate github.com/opencontainers/runc/libcontainer/user
thaJeztah Sep 17, 2023
0123f32
user: add go.mod and integrate in CI
thaJeztah Sep 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
@@ -1,4 +1,4 @@
PACKAGES ?= mountinfo mount sequential signal symlink
PACKAGES ?= mountinfo mount sequential signal symlink user
BINDIR ?= _build/bin
CROSS ?= linux/arm linux/arm64 linux/ppc64le linux/s390x \
freebsd/amd64 openbsd/amd64 darwin/amd64 darwin/arm64 windows/amd64
Expand Down
5 changes: 5 additions & 0 deletions user/go.mod
@@ -0,0 +1,5 @@
module github.com/moby/sys/user

go 1.17

require golang.org/x/sys v0.1.0
2 changes: 2 additions & 0 deletions user/go.sum
@@ -0,0 +1,2 @@
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
157 changes: 157 additions & 0 deletions user/lookup_unix.go
@@ -0,0 +1,157 @@
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris

package user

import (
"io"
"os"
"strconv"

"golang.org/x/sys/unix"
)

// Unix-specific path to the passwd and group formatted files.
const (
unixPasswdPath = "/etc/passwd"
unixGroupPath = "/etc/group"
)

// LookupUser looks up a user by their username in /etc/passwd. If the user
// cannot be found (or there is no /etc/passwd file on the filesystem), then
// LookupUser returns an error.
func LookupUser(username string) (User, error) {
return lookupUserFunc(func(u User) bool {
return u.Name == username
})
}

// LookupUid looks up a user by their user id in /etc/passwd. If the user cannot
// be found (or there is no /etc/passwd file on the filesystem), then LookupId
// returns an error.
func LookupUid(uid int) (User, error) {
return lookupUserFunc(func(u User) bool {
return u.Uid == uid
})
}

func lookupUserFunc(filter func(u User) bool) (User, error) {
// Get operating system-specific passwd reader-closer.
passwd, err := GetPasswd()
if err != nil {
return User{}, err
}
defer passwd.Close()

// Get the users.
users, err := ParsePasswdFilter(passwd, filter)
if err != nil {
return User{}, err
}

// No user entries found.
if len(users) == 0 {
return User{}, ErrNoPasswdEntries
}

// Assume the first entry is the "correct" one.
return users[0], nil
}

// LookupGroup looks up a group by its name in /etc/group. If the group cannot
// be found (or there is no /etc/group file on the filesystem), then LookupGroup
// returns an error.
func LookupGroup(groupname string) (Group, error) {
return lookupGroupFunc(func(g Group) bool {
return g.Name == groupname
})
}

// LookupGid looks up a group by its group id in /etc/group. If the group cannot
// be found (or there is no /etc/group file on the filesystem), then LookupGid
// returns an error.
func LookupGid(gid int) (Group, error) {
return lookupGroupFunc(func(g Group) bool {
return g.Gid == gid
})
}

func lookupGroupFunc(filter func(g Group) bool) (Group, error) {
// Get operating system-specific group reader-closer.
group, err := GetGroup()
if err != nil {
return Group{}, err
}
defer group.Close()

// Get the users.
groups, err := ParseGroupFilter(group, filter)
if err != nil {
return Group{}, err
}

// No user entries found.
if len(groups) == 0 {
return Group{}, ErrNoGroupEntries
}

// Assume the first entry is the "correct" one.
return groups[0], nil
}

func GetPasswdPath() (string, error) {
return unixPasswdPath, nil
}

func GetPasswd() (io.ReadCloser, error) {
return os.Open(unixPasswdPath)
}

func GetGroupPath() (string, error) {
return unixGroupPath, nil
}

func GetGroup() (io.ReadCloser, error) {
return os.Open(unixGroupPath)
}

// CurrentUser looks up the current user by their user id in /etc/passwd. If the
// user cannot be found (or there is no /etc/passwd file on the filesystem),
// then CurrentUser returns an error.
func CurrentUser() (User, error) {
return LookupUid(unix.Getuid())
}

// CurrentGroup looks up the current user's group by their primary group id's
// entry in /etc/passwd. If the group cannot be found (or there is no
// /etc/group file on the filesystem), then CurrentGroup returns an error.
func CurrentGroup() (Group, error) {
return LookupGid(unix.Getgid())
}

func currentUserSubIDs(fileName string) ([]SubID, error) {
u, err := CurrentUser()
if err != nil {
return nil, err
}
filter := func(entry SubID) bool {
return entry.Name == u.Name || entry.Name == strconv.Itoa(u.Uid)
}
return ParseSubIDFileFilter(fileName, filter)
}

func CurrentUserSubUIDs() ([]SubID, error) {
return currentUserSubIDs("/etc/subuid")
}

func CurrentUserSubGIDs() ([]SubID, error) {
return currentUserSubIDs("/etc/subgid")
}

func CurrentProcessUIDMap() ([]IDMap, error) {
return ParseIDMapFile("/proc/self/uid_map")
}

func CurrentProcessGIDMap() ([]IDMap, error) {
return ParseIDMapFile("/proc/self/gid_map")
}