internals_windows.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  2. import (
  3. "bytes"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/containerd/containerd/platforms"
  8. "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/api/types/mount"
  10. "github.com/docker/docker/pkg/idtools"
  11. "github.com/docker/docker/pkg/jsonmessage"
  12. "golang.org/x/sys/windows"
  13. )
  14. func parseChownFlag(builder *Builder, state *dispatchState, chown, ctrRootPath string, identityMapping *idtools.IdentityMapping) (idtools.Identity, error) {
  15. if builder.options.Platform == "windows" {
  16. return getAccountIdentity(builder, chown, ctrRootPath, state)
  17. }
  18. return identityMapping.RootPair(), nil
  19. }
  20. func getAccountIdentity(builder *Builder, accountName string, ctrRootPath string, state *dispatchState) (idtools.Identity, error) {
  21. // If this is potentially a string SID then attempt to convert it to verify
  22. // this, otherwise continue looking for the account.
  23. if strings.HasPrefix(accountName, "S-") || strings.HasPrefix(accountName, "s-") {
  24. sid, err := windows.StringToSid(accountName)
  25. if err == nil {
  26. return idtools.Identity{SID: sid.String()}, nil
  27. }
  28. }
  29. // Attempt to obtain the SID using the name.
  30. sid, _, accType, err := windows.LookupSID("", accountName)
  31. // If this is a SID that is built-in and hence the same across all systems then use that.
  32. if err == nil && (accType == windows.SidTypeAlias || accType == windows.SidTypeWellKnownGroup) {
  33. return idtools.Identity{SID: sid.String()}, nil
  34. }
  35. // Check if the account name is one unique to containers.
  36. if strings.EqualFold(accountName, "ContainerAdministrator") {
  37. return idtools.Identity{SID: idtools.ContainerAdministratorSidString}, nil
  38. } else if strings.EqualFold(accountName, "ContainerUser") {
  39. return idtools.Identity{SID: idtools.ContainerUserSidString}, nil
  40. }
  41. // All other lookups failed, so therefore determine if the account in
  42. // question exists in the container and if so, obtain its SID.
  43. return lookupNTAccount(builder, accountName, state)
  44. }
  45. func lookupNTAccount(builder *Builder, accountName string, state *dispatchState) (idtools.Identity, error) {
  46. source, _ := filepath.Split(os.Args[0])
  47. target := "C:\\Docker"
  48. targetExecutable := target + "\\containerutility.exe"
  49. optionsPlatform, err := platforms.Parse(builder.options.Platform)
  50. if err != nil {
  51. return idtools.Identity{}, err
  52. }
  53. runConfig := copyRunConfig(state.runConfig,
  54. withCmdCommentString("internal run to obtain NT account information.", optionsPlatform.OS))
  55. runConfig.Cmd = []string{targetExecutable, "getaccountsid", accountName}
  56. hostConfig := &container.HostConfig{Mounts: []mount.Mount{
  57. {
  58. Type: mount.TypeBind,
  59. Source: source,
  60. Target: target,
  61. ReadOnly: true,
  62. },
  63. },
  64. }
  65. container, err := builder.containerManager.Create(runConfig, hostConfig)
  66. if err != nil {
  67. return idtools.Identity{}, err
  68. }
  69. stdout := new(bytes.Buffer)
  70. stderr := new(bytes.Buffer)
  71. if err := builder.containerManager.Run(builder.clientCtx, container.ID, stdout, stderr); err != nil {
  72. if err, ok := err.(*statusCodeError); ok {
  73. return idtools.Identity{}, &jsonmessage.JSONError{
  74. Message: stderr.String(),
  75. Code: err.StatusCode(),
  76. }
  77. }
  78. return idtools.Identity{}, err
  79. }
  80. accountSid := stdout.String()
  81. return idtools.Identity{SID: accountSid}, nil
  82. }