-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #882 from giuseppe/support-libsubid
idtools: add support for libsubid
- Loading branch information
Showing
6 changed files
with
100 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env bash | ||
if test $(${GO:-go} env GOOS) != "linux" ; then | ||
echo no_libsubid | ||
exit 0 | ||
fi | ||
tmpdir="$PWD/tmp.$RANDOM" | ||
mkdir -p "$tmpdir" | ||
trap 'rm -fr "$tmpdir"' EXIT | ||
cc -o "$tmpdir"/libsubid_tag -l subid -x c - > /dev/null 2> /dev/null << EOF | ||
#include <shadow/subid.h> | ||
int main() { | ||
struct subid_range *ranges = NULL; | ||
get_subuid_ranges("root", &ranges); | ||
free(ranges); | ||
return 0; | ||
} | ||
EOF | ||
if test $? -ne 0 ; then | ||
echo no_libsubid | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// +build linux,cgo,!no_libsubid | ||
|
||
package idtools | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
/* | ||
#cgo LDFLAGS: -l subid | ||
#include <shadow/subid.h> | ||
#include <stdlib.h> | ||
const char *Prog = "storage"; | ||
struct subid_range get_range(struct subid_range *ranges, int i) | ||
{ | ||
return ranges[i]; | ||
} | ||
*/ | ||
import "C" | ||
|
||
func readSubid(username string, isUser bool) (ranges, error) { | ||
var ret ranges | ||
if username == "ALL" { | ||
return nil, errors.New("username ALL not supported") | ||
} | ||
|
||
cUsername := C.CString(username) | ||
defer C.free(unsafe.Pointer(cUsername)) | ||
|
||
var nRanges C.int | ||
var cRanges *C.struct_subid_range | ||
if isUser { | ||
nRanges = C.get_subuid_ranges(cUsername, &cRanges) | ||
} else { | ||
nRanges = C.get_subgid_ranges(cUsername, &cRanges) | ||
} | ||
if nRanges < 0 { | ||
return nil, errors.New("cannot read subids") | ||
} | ||
defer C.free(unsafe.Pointer(cRanges)) | ||
|
||
for i := 0; i < int(nRanges); i++ { | ||
r := C.get_range(cRanges, C.int(i)) | ||
newRange := subIDRange{ | ||
Start: int(r.start), | ||
Length: int(r.count), | ||
} | ||
ret = append(ret, newRange) | ||
} | ||
return ret, nil | ||
} | ||
|
||
func readSubuid(username string) (ranges, error) { | ||
return readSubid(username, true) | ||
} | ||
|
||
func readSubgid(username string) (ranges, error) { | ||
return readSubid(username, false) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// +build !linux no_libsubid !cgo | ||
|
||
package idtools | ||
|
||
func readSubuid(username string) (ranges, error) { | ||
return parseSubidFile(subuidFileName, username) | ||
} | ||
|
||
func readSubgid(username string) (ranges, error) { | ||
return parseSubidFile(subgidFileName, username) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters