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

idtools: add lookup by UID with libsubid #1265

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Changes from all commits
Commits
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
17 changes: 17 additions & 0 deletions pkg/idtools/idtools_supported.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//go:build linux && cgo && libsubid
// +build linux,cgo,libsubid

package idtools

import (
"os/user"
"unsafe"

"github.com/pkg/errors"
Expand Down Expand Up @@ -32,19 +34,34 @@ import "C"

func readSubid(username string, isUser bool) (ranges, error) {
var ret ranges
uidstr := ""

if username == "ALL" {
return nil, errors.New("username ALL not supported")
}

if u, err := user.Lookup(username); err == nil {
uidstr = u.Uid
}

cUsername := C.CString(username)
defer C.free(unsafe.Pointer(cUsername))

cuidstr := C.CString(uidstr)
defer C.free(unsafe.Pointer(cuidstr))

var nRanges C.int
var cRanges *C.struct_subid_range
if isUser {
nRanges = C.subid_get_uid_ranges(cUsername, &cRanges)
if nRanges <= 0 {
nRanges = C.subid_get_uid_ranges(cuidstr, &cRanges)
}
} else {
nRanges = C.subid_get_gid_ranges(cUsername, &cRanges)
if nRanges <= 0 {
nRanges = C.subid_get_gid_ranges(cuidstr, &cRanges)
}
}
if nRanges < 0 {
return nil, errors.New("cannot read subids")
Expand Down