internals_linux.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "context"
  4. "path/filepath"
  5. "strconv"
  6. "strings"
  7. "github.com/docker/docker/pkg/idtools"
  8. "github.com/moby/sys/symlink"
  9. "github.com/moby/sys/user"
  10. "github.com/pkg/errors"
  11. )
  12. func parseChownFlag(ctx context.Context, builder *Builder, state *dispatchState, chown, ctrRootPath string, identityMapping idtools.IdentityMapping) (idtools.Identity, error) {
  13. var userStr, grpStr string
  14. parts := strings.Split(chown, ":")
  15. if len(parts) > 2 {
  16. return idtools.Identity{}, errors.New("invalid chown string format: " + chown)
  17. }
  18. if len(parts) == 1 {
  19. // if no group specified, use the user spec as group as well
  20. userStr, grpStr = parts[0], parts[0]
  21. } else {
  22. userStr, grpStr = parts[0], parts[1]
  23. }
  24. passwdPath, err := symlink.FollowSymlinkInScope(filepath.Join(ctrRootPath, "etc", "passwd"), ctrRootPath)
  25. if err != nil {
  26. return idtools.Identity{}, errors.Wrapf(err, "can't resolve /etc/passwd path in container rootfs")
  27. }
  28. groupPath, err := symlink.FollowSymlinkInScope(filepath.Join(ctrRootPath, "etc", "group"), ctrRootPath)
  29. if err != nil {
  30. return idtools.Identity{}, errors.Wrapf(err, "can't resolve /etc/group path in container rootfs")
  31. }
  32. uid, err := lookupUser(userStr, passwdPath)
  33. if err != nil {
  34. return idtools.Identity{}, errors.Wrapf(err, "can't find uid for user "+userStr)
  35. }
  36. gid, err := lookupGroup(grpStr, groupPath)
  37. if err != nil {
  38. return idtools.Identity{}, errors.Wrapf(err, "can't find gid for group "+grpStr)
  39. }
  40. // convert as necessary because of user namespaces
  41. chownPair, err := identityMapping.ToHost(idtools.Identity{UID: uid, GID: gid})
  42. if err != nil {
  43. return idtools.Identity{}, errors.Wrapf(err, "unable to convert uid/gid to host mapping")
  44. }
  45. return chownPair, nil
  46. }
  47. func lookupUser(userStr, filepath string) (int, error) {
  48. // if the string is actually a uid integer, parse to int and return
  49. // as we don't need to translate with the help of files
  50. uid, err := strconv.Atoi(userStr)
  51. if err == nil {
  52. return uid, nil
  53. }
  54. users, err := user.ParsePasswdFileFilter(filepath, func(u user.User) bool {
  55. return u.Name == userStr
  56. })
  57. if err != nil {
  58. return 0, err
  59. }
  60. if len(users) == 0 {
  61. return 0, errors.New("no such user: " + userStr)
  62. }
  63. return users[0].Uid, nil
  64. }
  65. func lookupGroup(groupStr, filepath string) (int, error) {
  66. // if the string is actually a gid integer, parse to int and return
  67. // as we don't need to translate with the help of files
  68. gid, err := strconv.Atoi(groupStr)
  69. if err == nil {
  70. return gid, nil
  71. }
  72. groups, err := user.ParseGroupFileFilter(filepath, func(g user.Group) bool {
  73. return g.Name == groupStr
  74. })
  75. if err != nil {
  76. return 0, err
  77. }
  78. if len(groups) == 0 {
  79. return 0, errors.New("no such group: " + groupStr)
  80. }
  81. return groups[0].Gid, nil
  82. }