Skip to content

Commit

Permalink
Merge pull request #1265 from giuseppe/idtools-lookup-by-uid
Browse files Browse the repository at this point in the history
idtools: add lookup by UID with libsubid
  • Loading branch information
vrothberg committed Jun 16, 2022
2 parents 587fa86 + 0b3607a commit 7df6428
Showing 1 changed file with 17 additions and 0 deletions.
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

0 comments on commit 7df6428

Please sign in to comment.