2018-02-05 21:05:59 +00:00
|
|
|
package dockerfile // import "github.com/docker/docker/builder/dockerfile"
|
2018-01-13 01:57:50 +00:00
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
2022-10-26 16:13:17 +00:00
|
|
|
"context"
|
2017-11-16 06:20:33 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2018-01-13 01:57:50 +00:00
|
|
|
|
2017-11-16 06:20:33 +00:00
|
|
|
"github.com/containerd/containerd/platforms"
|
|
|
|
"github.com/docker/docker/api/types/container"
|
|
|
|
"github.com/docker/docker/api/types/mount"
|
|
|
|
"github.com/docker/docker/pkg/idtools"
|
|
|
|
"github.com/docker/docker/pkg/jsonmessage"
|
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
)
|
|
|
|
|
2022-10-26 16:13:17 +00:00
|
|
|
func parseChownFlag(ctx context.Context, builder *Builder, state *dispatchState, chown, ctrRootPath string, identityMapping idtools.IdentityMapping) (idtools.Identity, error) {
|
2017-11-16 06:20:33 +00:00
|
|
|
if builder.options.Platform == "windows" {
|
2022-10-26 16:13:17 +00:00
|
|
|
return getAccountIdentity(ctx, builder, chown, ctrRootPath, state)
|
2017-11-16 06:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return identityMapping.RootPair(), nil
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:13:17 +00:00
|
|
|
func getAccountIdentity(ctx context.Context, builder *Builder, accountName string, ctrRootPath string, state *dispatchState) (idtools.Identity, error) {
|
2017-11-16 06:20:33 +00:00
|
|
|
// If this is potentially a string SID then attempt to convert it to verify
|
|
|
|
// this, otherwise continue looking for the account.
|
|
|
|
if strings.HasPrefix(accountName, "S-") || strings.HasPrefix(accountName, "s-") {
|
|
|
|
sid, err := windows.StringToSid(accountName)
|
|
|
|
|
|
|
|
if err == nil {
|
2019-09-30 15:07:47 +00:00
|
|
|
return idtools.Identity{SID: sid.String()}, nil
|
2017-11-16 06:20:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to obtain the SID using the name.
|
|
|
|
sid, _, accType, err := windows.LookupSID("", accountName)
|
|
|
|
|
|
|
|
// If this is a SID that is built-in and hence the same across all systems then use that.
|
|
|
|
if err == nil && (accType == windows.SidTypeAlias || accType == windows.SidTypeWellKnownGroup) {
|
2019-09-30 15:07:47 +00:00
|
|
|
return idtools.Identity{SID: sid.String()}, nil
|
2017-11-16 06:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the account name is one unique to containers.
|
|
|
|
if strings.EqualFold(accountName, "ContainerAdministrator") {
|
2021-06-18 08:37:04 +00:00
|
|
|
return idtools.Identity{SID: idtools.ContainerAdministratorSidString}, nil
|
2017-11-16 06:20:33 +00:00
|
|
|
} else if strings.EqualFold(accountName, "ContainerUser") {
|
2021-06-18 08:37:04 +00:00
|
|
|
return idtools.Identity{SID: idtools.ContainerUserSidString}, nil
|
2017-11-16 06:20:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// All other lookups failed, so therefore determine if the account in
|
|
|
|
// question exists in the container and if so, obtain its SID.
|
2022-10-26 16:13:17 +00:00
|
|
|
return lookupNTAccount(ctx, builder, accountName, state)
|
2017-11-16 06:20:33 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 16:13:17 +00:00
|
|
|
func lookupNTAccount(ctx context.Context, builder *Builder, accountName string, state *dispatchState) (idtools.Identity, error) {
|
2017-11-16 06:20:33 +00:00
|
|
|
source, _ := filepath.Split(os.Args[0])
|
|
|
|
|
|
|
|
target := "C:\\Docker"
|
|
|
|
targetExecutable := target + "\\containerutility.exe"
|
|
|
|
|
|
|
|
optionsPlatform, err := platforms.Parse(builder.options.Platform)
|
|
|
|
if err != nil {
|
|
|
|
return idtools.Identity{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
runConfig := copyRunConfig(state.runConfig,
|
|
|
|
withCmdCommentString("internal run to obtain NT account information.", optionsPlatform.OS))
|
|
|
|
|
|
|
|
runConfig.Cmd = []string{targetExecutable, "getaccountsid", accountName}
|
|
|
|
|
2022-01-20 12:48:03 +00:00
|
|
|
hostConfig := &container.HostConfig{
|
|
|
|
Mounts: []mount.Mount{
|
|
|
|
{
|
|
|
|
Type: mount.TypeBind,
|
|
|
|
Source: source,
|
|
|
|
Target: target,
|
|
|
|
ReadOnly: true,
|
|
|
|
},
|
2017-11-16 06:20:33 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-10-26 16:13:17 +00:00
|
|
|
container, err := builder.containerManager.Create(ctx, runConfig, hostConfig)
|
2017-11-16 06:20:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return idtools.Identity{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stdout := new(bytes.Buffer)
|
|
|
|
stderr := new(bytes.Buffer)
|
|
|
|
|
2022-10-28 11:40:06 +00:00
|
|
|
if err := builder.containerManager.Run(ctx, container.ID, stdout, stderr); err != nil {
|
2017-11-16 06:20:33 +00:00
|
|
|
if err, ok := err.(*statusCodeError); ok {
|
|
|
|
return idtools.Identity{}, &jsonmessage.JSONError{
|
|
|
|
Message: stderr.String(),
|
|
|
|
Code: err.StatusCode(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return idtools.Identity{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
accountSid := stdout.String()
|
|
|
|
|
|
|
|
return idtools.Identity{SID: accountSid}, nil
|
2018-01-13 01:57:50 +00:00
|
|
|
}
|