|
@@ -9,7 +9,6 @@ import (
|
|
"os"
|
|
"os"
|
|
"path/filepath"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strconv"
|
|
- "strings"
|
|
|
|
"sync"
|
|
"sync"
|
|
"syscall"
|
|
"syscall"
|
|
|
|
|
|
@@ -107,14 +106,14 @@ func accessible(isOwner, isGroup bool, perms os.FileMode) bool {
|
|
|
|
|
|
// LookupUser uses traditional local system files lookup (from libcontainer/user) on a username,
|
|
// LookupUser uses traditional local system files lookup (from libcontainer/user) on a username,
|
|
// followed by a call to `getent` for supporting host configured non-files passwd and group dbs
|
|
// followed by a call to `getent` for supporting host configured non-files passwd and group dbs
|
|
-func LookupUser(username string) (user.User, error) {
|
|
|
|
|
|
+func LookupUser(name string) (user.User, error) {
|
|
// first try a local system files lookup using existing capabilities
|
|
// first try a local system files lookup using existing capabilities
|
|
- usr, err := user.LookupUser(username)
|
|
|
|
|
|
+ usr, err := user.LookupUser(name)
|
|
if err == nil {
|
|
if err == nil {
|
|
return usr, nil
|
|
return usr, nil
|
|
}
|
|
}
|
|
// local files lookup failed; attempt to call `getent` to query configured passwd dbs
|
|
// local files lookup failed; attempt to call `getent` to query configured passwd dbs
|
|
- usr, err = getentUser(fmt.Sprintf("%s %s", "passwd", username))
|
|
|
|
|
|
+ usr, err = getentUser(name)
|
|
if err != nil {
|
|
if err != nil {
|
|
return user.User{}, err
|
|
return user.User{}, err
|
|
}
|
|
}
|
|
@@ -130,11 +129,11 @@ func LookupUID(uid int) (user.User, error) {
|
|
return usr, nil
|
|
return usr, nil
|
|
}
|
|
}
|
|
// local files lookup failed; attempt to call `getent` to query configured passwd dbs
|
|
// local files lookup failed; attempt to call `getent` to query configured passwd dbs
|
|
- return getentUser(fmt.Sprintf("%s %d", "passwd", uid))
|
|
|
|
|
|
+ return getentUser(strconv.Itoa(uid))
|
|
}
|
|
}
|
|
|
|
|
|
-func getentUser(args string) (user.User, error) {
|
|
|
|
- reader, err := callGetent(args)
|
|
|
|
|
|
+func getentUser(name string) (user.User, error) {
|
|
|
|
+ reader, err := callGetent("passwd", name)
|
|
if err != nil {
|
|
if err != nil {
|
|
return user.User{}, err
|
|
return user.User{}, err
|
|
}
|
|
}
|
|
@@ -143,21 +142,21 @@ func getentUser(args string) (user.User, error) {
|
|
return user.User{}, err
|
|
return user.User{}, err
|
|
}
|
|
}
|
|
if len(users) == 0 {
|
|
if len(users) == 0 {
|
|
- return user.User{}, fmt.Errorf("getent failed to find passwd entry for %q", strings.Split(args, " ")[1])
|
|
|
|
|
|
+ return user.User{}, fmt.Errorf("getent failed to find passwd entry for %q", name)
|
|
}
|
|
}
|
|
return users[0], nil
|
|
return users[0], nil
|
|
}
|
|
}
|
|
|
|
|
|
// LookupGroup uses traditional local system files lookup (from libcontainer/user) on a group name,
|
|
// LookupGroup uses traditional local system files lookup (from libcontainer/user) on a group name,
|
|
// followed by a call to `getent` for supporting host configured non-files passwd and group dbs
|
|
// followed by a call to `getent` for supporting host configured non-files passwd and group dbs
|
|
-func LookupGroup(groupname string) (user.Group, error) {
|
|
|
|
|
|
+func LookupGroup(name string) (user.Group, error) {
|
|
// first try a local system files lookup using existing capabilities
|
|
// first try a local system files lookup using existing capabilities
|
|
- group, err := user.LookupGroup(groupname)
|
|
|
|
|
|
+ group, err := user.LookupGroup(name)
|
|
if err == nil {
|
|
if err == nil {
|
|
return group, nil
|
|
return group, nil
|
|
}
|
|
}
|
|
// local files lookup failed; attempt to call `getent` to query configured group dbs
|
|
// local files lookup failed; attempt to call `getent` to query configured group dbs
|
|
- return getentGroup(fmt.Sprintf("%s %s", "group", groupname))
|
|
|
|
|
|
+ return getentGroup(name)
|
|
}
|
|
}
|
|
|
|
|
|
// LookupGID uses traditional local system files lookup (from libcontainer/user) on a group ID,
|
|
// LookupGID uses traditional local system files lookup (from libcontainer/user) on a group ID,
|
|
@@ -169,11 +168,11 @@ func LookupGID(gid int) (user.Group, error) {
|
|
return group, nil
|
|
return group, nil
|
|
}
|
|
}
|
|
// local files lookup failed; attempt to call `getent` to query configured group dbs
|
|
// local files lookup failed; attempt to call `getent` to query configured group dbs
|
|
- return getentGroup(fmt.Sprintf("%s %d", "group", gid))
|
|
|
|
|
|
+ return getentGroup(strconv.Itoa(gid))
|
|
}
|
|
}
|
|
|
|
|
|
-func getentGroup(args string) (user.Group, error) {
|
|
|
|
- reader, err := callGetent(args)
|
|
|
|
|
|
+func getentGroup(name string) (user.Group, error) {
|
|
|
|
+ reader, err := callGetent("group", name)
|
|
if err != nil {
|
|
if err != nil {
|
|
return user.Group{}, err
|
|
return user.Group{}, err
|
|
}
|
|
}
|
|
@@ -182,18 +181,18 @@ func getentGroup(args string) (user.Group, error) {
|
|
return user.Group{}, err
|
|
return user.Group{}, err
|
|
}
|
|
}
|
|
if len(groups) == 0 {
|
|
if len(groups) == 0 {
|
|
- return user.Group{}, fmt.Errorf("getent failed to find groups entry for %q", strings.Split(args, " ")[1])
|
|
|
|
|
|
+ return user.Group{}, fmt.Errorf("getent failed to find groups entry for %q", name)
|
|
}
|
|
}
|
|
return groups[0], nil
|
|
return groups[0], nil
|
|
}
|
|
}
|
|
|
|
|
|
-func callGetent(args string) (io.Reader, error) {
|
|
|
|
|
|
+func callGetent(database, key string) (io.Reader, error) {
|
|
entOnce.Do(func() { getentCmd, _ = resolveBinary("getent") })
|
|
entOnce.Do(func() { getentCmd, _ = resolveBinary("getent") })
|
|
// if no `getent` command on host, can't do anything else
|
|
// if no `getent` command on host, can't do anything else
|
|
if getentCmd == "" {
|
|
if getentCmd == "" {
|
|
- return nil, fmt.Errorf("")
|
|
|
|
|
|
+ return nil, fmt.Errorf("unable to find getent command")
|
|
}
|
|
}
|
|
- out, err := execCmd(getentCmd, args)
|
|
|
|
|
|
+ out, err := execCmd(getentCmd, database, key)
|
|
if err != nil {
|
|
if err != nil {
|
|
exitCode, errC := system.GetExitCode(err)
|
|
exitCode, errC := system.GetExitCode(err)
|
|
if errC != nil {
|
|
if errC != nil {
|
|
@@ -203,8 +202,7 @@ func callGetent(args string) (io.Reader, error) {
|
|
case 1:
|
|
case 1:
|
|
return nil, fmt.Errorf("getent reported invalid parameters/database unknown")
|
|
return nil, fmt.Errorf("getent reported invalid parameters/database unknown")
|
|
case 2:
|
|
case 2:
|
|
- terms := strings.Split(args, " ")
|
|
|
|
- return nil, fmt.Errorf("getent unable to find entry %q in %s database", terms[1], terms[0])
|
|
|
|
|
|
+ return nil, fmt.Errorf("getent unable to find entry %q in %s database", key, database)
|
|
case 3:
|
|
case 3:
|
|
return nil, fmt.Errorf("getent database doesn't support enumeration")
|
|
return nil, fmt.Errorf("getent database doesn't support enumeration")
|
|
default:
|
|
default:
|
|
@@ -235,19 +233,19 @@ func lazyChown(p string, uid, gid int, stat *system.StatT) error {
|
|
// NewIdentityMapping takes a requested username and
|
|
// NewIdentityMapping takes a requested username and
|
|
// using the data from /etc/sub{uid,gid} ranges, creates the
|
|
// using the data from /etc/sub{uid,gid} ranges, creates the
|
|
// proper uid and gid remapping ranges for that user/group pair
|
|
// proper uid and gid remapping ranges for that user/group pair
|
|
-func NewIdentityMapping(username string) (*IdentityMapping, error) {
|
|
|
|
- usr, err := LookupUser(username)
|
|
|
|
|
|
+func NewIdentityMapping(name string) (*IdentityMapping, error) {
|
|
|
|
+ usr, err := LookupUser(name)
|
|
if err != nil {
|
|
if err != nil {
|
|
- return nil, fmt.Errorf("Could not get user for username %s: %v", username, err)
|
|
|
|
|
|
+ return nil, fmt.Errorf("Could not get user for username %s: %v", name, err)
|
|
}
|
|
}
|
|
|
|
|
|
uid := strconv.Itoa(usr.Uid)
|
|
uid := strconv.Itoa(usr.Uid)
|
|
|
|
|
|
- subuidRangesWithUserName, err := parseSubuid(username)
|
|
|
|
|
|
+ subuidRangesWithUserName, err := parseSubuid(name)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
- subgidRangesWithUserName, err := parseSubgid(username)
|
|
|
|
|
|
+ subgidRangesWithUserName, err := parseSubgid(name)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, err
|
|
return nil, err
|
|
}
|
|
}
|
|
@@ -265,10 +263,10 @@ func NewIdentityMapping(username string) (*IdentityMapping, error) {
|
|
subgidRanges := append(subgidRangesWithUserName, subgidRangesWithUID...)
|
|
subgidRanges := append(subgidRangesWithUserName, subgidRangesWithUID...)
|
|
|
|
|
|
if len(subuidRanges) == 0 {
|
|
if len(subuidRanges) == 0 {
|
|
- return nil, errors.Errorf("no subuid ranges found for user %q", username)
|
|
|
|
|
|
+ return nil, errors.Errorf("no subuid ranges found for user %q", name)
|
|
}
|
|
}
|
|
if len(subgidRanges) == 0 {
|
|
if len(subgidRanges) == 0 {
|
|
- return nil, errors.Errorf("no subgid ranges found for user %q", username)
|
|
|
|
|
|
+ return nil, errors.Errorf("no subgid ranges found for user %q", name)
|
|
}
|
|
}
|
|
|
|
|
|
return &IdentityMapping{
|
|
return &IdentityMapping{
|