2018-02-05 21:05:59 +00:00
|
|
|
package system // import "github.com/docker/docker/pkg/system"
|
2015-04-23 22:55:36 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"regexp"
|
|
|
|
"syscall"
|
2016-11-04 19:42:21 +00:00
|
|
|
"unsafe"
|
|
|
|
|
2017-05-23 14:22:32 +00:00
|
|
|
"golang.org/x/sys/windows"
|
2015-04-23 22:55:36 +00:00
|
|
|
)
|
|
|
|
|
2022-10-14 11:36:54 +00:00
|
|
|
// SddlAdministratorsLocalSystem is local administrators plus NT AUTHORITY\System.
|
|
|
|
const SddlAdministratorsLocalSystem = "D:P(A;OICI;GA;;;BA)(A;OICI;GA;;;SY)"
|
|
|
|
|
|
|
|
// volumePath is a regular expression to check if a path is a Windows
|
2022-10-14 14:38:08 +00:00
|
|
|
// volume path (e.g., "\\?\Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}"
|
|
|
|
// or "\\?\Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}\").
|
|
|
|
var volumePath = regexp.MustCompile(`^\\\\\?\\Volume{[a-z0-9-]+}\\?$`)
|
2017-06-02 01:59:11 +00:00
|
|
|
|
2022-10-14 11:36:54 +00:00
|
|
|
// MkdirAllWithACL is a custom version of os.MkdirAll modified for use on Windows
|
|
|
|
// so that it is both volume path aware, and can create a directory with
|
|
|
|
// an appropriate SDDL defined ACL.
|
|
|
|
func MkdirAllWithACL(path string, _ os.FileMode, sddl string) error {
|
2022-10-14 13:57:07 +00:00
|
|
|
sa, err := makeSecurityAttributes(sddl)
|
|
|
|
if err != nil {
|
|
|
|
return &os.PathError{Op: "mkdirall", Path: path, Err: err}
|
|
|
|
}
|
|
|
|
return mkdirall(path, sa)
|
2016-11-04 19:42:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 11:36:54 +00:00
|
|
|
// MkdirAll is a custom version of os.MkdirAll that is volume path aware for
|
|
|
|
// Windows. It can be used as a drop-in replacement for os.MkdirAll.
|
2019-08-08 09:51:00 +00:00
|
|
|
func MkdirAll(path string, _ os.FileMode) error {
|
2022-10-14 13:57:07 +00:00
|
|
|
return mkdirall(path, nil)
|
2016-11-04 19:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mkdirall is a custom version of os.MkdirAll modified for use on Windows
|
|
|
|
// so that it is both volume path aware, and can create a directory with
|
|
|
|
// a DACL.
|
2022-10-14 13:57:07 +00:00
|
|
|
func mkdirall(path string, perm *windows.SecurityAttributes) error {
|
2022-10-14 11:36:54 +00:00
|
|
|
if volumePath.MatchString(path) {
|
2015-04-23 22:55:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-04 19:42:21 +00:00
|
|
|
// The rest of this method is largely copied from os.MkdirAll and should be kept
|
2015-04-23 22:55:36 +00:00
|
|
|
// as-is to ensure compatibility.
|
|
|
|
|
|
|
|
// Fast path: if we can tell whether path is a directory or file, stop with success or error.
|
|
|
|
dir, err := os.Stat(path)
|
|
|
|
if err == nil {
|
|
|
|
if dir.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-14 14:11:38 +00:00
|
|
|
return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
|
2015-04-23 22:55:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Slow path: make sure parent exists and then call Mkdir for path.
|
|
|
|
i := len(path)
|
|
|
|
for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
|
|
|
|
i--
|
|
|
|
}
|
|
|
|
|
|
|
|
j := i
|
|
|
|
for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
|
|
|
|
if j > 1 {
|
2022-10-14 14:11:38 +00:00
|
|
|
// Create parent.
|
|
|
|
err = mkdirall(fixRootDirectory(path[:j-1]), perm)
|
2015-04-23 22:55:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:11:38 +00:00
|
|
|
// Parent now exists; invoke Mkdir and use its result.
|
2022-10-14 13:57:07 +00:00
|
|
|
err = mkdirWithACL(path, perm)
|
2015-04-23 22:55:36 +00:00
|
|
|
if err != nil {
|
|
|
|
// Handle arguments like "foo/." by
|
|
|
|
// double-checking that directory doesn't exist.
|
|
|
|
dir, err1 := os.Lstat(path)
|
|
|
|
if err1 == nil && dir.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-08-26 19:37:01 +00:00
|
|
|
|
2016-11-04 19:42:21 +00:00
|
|
|
// mkdirWithACL creates a new directory. If there is an error, it will be of
|
|
|
|
// type *PathError. .
|
|
|
|
//
|
2017-05-23 14:22:32 +00:00
|
|
|
// This is a modified and combined version of os.Mkdir and windows.Mkdir
|
2016-11-04 19:42:21 +00:00
|
|
|
// in golang to cater for creating a directory am ACL permitting full
|
|
|
|
// access, with inheritance, to any subfolder/file for Built-in Administrators
|
|
|
|
// and Local System.
|
2022-10-14 13:57:07 +00:00
|
|
|
func mkdirWithACL(name string, sa *windows.SecurityAttributes) error {
|
|
|
|
if sa == nil {
|
|
|
|
return os.Mkdir(name, 0)
|
2016-11-04 19:42:21 +00:00
|
|
|
}
|
|
|
|
|
2017-05-23 14:22:32 +00:00
|
|
|
namep, err := windows.UTF16PtrFromString(name)
|
2016-11-04 19:42:21 +00:00
|
|
|
if err != nil {
|
2016-11-19 00:06:50 +00:00
|
|
|
return &os.PathError{Op: "mkdir", Path: name, Err: err}
|
2016-11-04 19:42:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 13:57:07 +00:00
|
|
|
err = windows.CreateDirectory(namep, sa)
|
|
|
|
if err != nil {
|
|
|
|
return &os.PathError{Op: "mkdir", Path: name, Err: err}
|
2016-11-04 19:42:21 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-14 13:57:07 +00:00
|
|
|
|
2022-10-14 14:11:38 +00:00
|
|
|
// fixRootDirectory fixes a reference to a drive's root directory to
|
|
|
|
// have the required trailing slash.
|
|
|
|
func fixRootDirectory(p string) string {
|
|
|
|
if len(p) == len(`\\?\c:`) {
|
|
|
|
if os.IsPathSeparator(p[0]) && os.IsPathSeparator(p[1]) && p[2] == '?' && os.IsPathSeparator(p[3]) && p[5] == ':' {
|
|
|
|
return p + `\`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-10-14 13:57:07 +00:00
|
|
|
func makeSecurityAttributes(sddl string) (*windows.SecurityAttributes, error) {
|
|
|
|
var sa windows.SecurityAttributes
|
|
|
|
sa.Length = uint32(unsafe.Sizeof(sa))
|
|
|
|
sa.InheritHandle = 1
|
|
|
|
var err error
|
|
|
|
sa.SecurityDescriptor, err = windows.SecurityDescriptorFromString(sddl)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &sa, nil
|
|
|
|
}
|