2018-02-05 16:05:59 -05:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2018-01-12 20:57:50 -05:00
|
|
|
|
|
|
|
import (
|
2022-10-26 12:13:17 -04:00
|
|
|
"context"
|
2018-01-12 20:57:50 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/docker/docker/pkg/idtools"
|
2020-10-29 19:54:10 -04:00
|
|
|
"github.com/moby/sys/symlink"
|
2018-01-12 20:57:50 -05:00
|
|
|
lcUser "github.com/opencontainers/runc/libcontainer/user"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2022-10-26 12:13:17 -04:00
|
|
|
func parseChownFlag(ctx context.Context, builder *Builder, state *dispatchState, chown, ctrRootPath string, identityMapping idtools.IdentityMapping) (idtools.Identity, error) {
|
2018-01-12 20:57:50 -05:00
|
|
|
var userStr, grpStr string
|
|
|
|
parts := strings.Split(chown, ":")
|
|
|
|
if len(parts) > 2 {
|
2017-11-16 01:20:33 -05:00
|
|
|
return idtools.Identity{}, errors.New("invalid chown string format: " + chown)
|
2018-01-12 20:57:50 -05:00
|
|
|
}
|
|
|
|
if len(parts) == 1 {
|
|
|
|
// if no group specified, use the user spec as group as well
|
|
|
|
userStr, grpStr = parts[0], parts[0]
|
|
|
|
} else {
|
|
|
|
userStr, grpStr = parts[0], parts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
passwdPath, err := symlink.FollowSymlinkInScope(filepath.Join(ctrRootPath, "etc", "passwd"), ctrRootPath)
|
|
|
|
if err != nil {
|
2017-11-16 01:20:33 -05:00
|
|
|
return idtools.Identity{}, errors.Wrapf(err, "can't resolve /etc/passwd path in container rootfs")
|
2018-01-12 20:57:50 -05:00
|
|
|
}
|
|
|
|
groupPath, err := symlink.FollowSymlinkInScope(filepath.Join(ctrRootPath, "etc", "group"), ctrRootPath)
|
|
|
|
if err != nil {
|
2017-11-16 01:20:33 -05:00
|
|
|
return idtools.Identity{}, errors.Wrapf(err, "can't resolve /etc/group path in container rootfs")
|
2018-01-12 20:57:50 -05:00
|
|
|
}
|
|
|
|
uid, err := lookupUser(userStr, passwdPath)
|
|
|
|
if err != nil {
|
2017-11-16 01:20:33 -05:00
|
|
|
return idtools.Identity{}, errors.Wrapf(err, "can't find uid for user "+userStr)
|
2018-01-12 20:57:50 -05:00
|
|
|
}
|
|
|
|
gid, err := lookupGroup(grpStr, groupPath)
|
|
|
|
if err != nil {
|
2017-11-16 01:20:33 -05:00
|
|
|
return idtools.Identity{}, errors.Wrapf(err, "can't find gid for group "+grpStr)
|
2018-01-12 20:57:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert as necessary because of user namespaces
|
2017-11-16 01:20:33 -05:00
|
|
|
chownPair, err := identityMapping.ToHost(idtools.Identity{UID: uid, GID: gid})
|
2018-01-12 20:57:50 -05:00
|
|
|
if err != nil {
|
2017-11-16 01:20:33 -05:00
|
|
|
return idtools.Identity{}, errors.Wrapf(err, "unable to convert uid/gid to host mapping")
|
2018-01-12 20:57:50 -05:00
|
|
|
}
|
|
|
|
return chownPair, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupUser(userStr, filepath string) (int, error) {
|
|
|
|
// if the string is actually a uid integer, parse to int and return
|
|
|
|
// as we don't need to translate with the help of files
|
|
|
|
uid, err := strconv.Atoi(userStr)
|
|
|
|
if err == nil {
|
|
|
|
return uid, nil
|
|
|
|
}
|
|
|
|
users, err := lcUser.ParsePasswdFileFilter(filepath, func(u lcUser.User) bool {
|
|
|
|
return u.Name == userStr
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if len(users) == 0 {
|
|
|
|
return 0, errors.New("no such user: " + userStr)
|
|
|
|
}
|
|
|
|
return users[0].Uid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func lookupGroup(groupStr, filepath string) (int, error) {
|
|
|
|
// if the string is actually a gid integer, parse to int and return
|
|
|
|
// as we don't need to translate with the help of files
|
|
|
|
gid, err := strconv.Atoi(groupStr)
|
|
|
|
if err == nil {
|
|
|
|
return gid, nil
|
|
|
|
}
|
|
|
|
groups, err := lcUser.ParseGroupFileFilter(filepath, func(g lcUser.Group) bool {
|
|
|
|
return g.Name == groupStr
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if len(groups) == 0 {
|
|
|
|
return 0, errors.New("no such group: " + groupStr)
|
|
|
|
}
|
|
|
|
return groups[0].Gid, nil
|
|
|
|
}
|