filesys_windows.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package system // import "github.com/docker/docker/pkg/system"
  2. import (
  3. "os"
  4. "regexp"
  5. "syscall"
  6. "unsafe"
  7. "golang.org/x/sys/windows"
  8. )
  9. // SddlAdministratorsLocalSystem is local administrators plus NT AUTHORITY\System.
  10. const SddlAdministratorsLocalSystem = "D:P(A;OICI;GA;;;BA)(A;OICI;GA;;;SY)"
  11. // volumePath is a regular expression to check if a path is a Windows
  12. // volume path (e.g., "\\?\Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}"
  13. // or "\\?\Volume{4c1b02c1-d990-11dc-99ae-806e6f6e6963}\").
  14. var volumePath = regexp.MustCompile(`^\\\\\?\\Volume{[a-z0-9-]+}\\?$`)
  15. // MkdirAllWithACL is a custom version of os.MkdirAll modified for use on Windows
  16. // so that it is both volume path aware, and can create a directory with
  17. // an appropriate SDDL defined ACL.
  18. func MkdirAllWithACL(path string, _ os.FileMode, sddl string) error {
  19. sa, err := makeSecurityAttributes(sddl)
  20. if err != nil {
  21. return &os.PathError{Op: "mkdirall", Path: path, Err: err}
  22. }
  23. return mkdirall(path, sa)
  24. }
  25. // MkdirAll is a custom version of os.MkdirAll that is volume path aware for
  26. // Windows. It can be used as a drop-in replacement for os.MkdirAll.
  27. func MkdirAll(path string, _ os.FileMode) error {
  28. return mkdirall(path, nil)
  29. }
  30. // mkdirall is a custom version of os.MkdirAll modified for use on Windows
  31. // so that it is both volume path aware, and can create a directory with
  32. // a DACL.
  33. func mkdirall(path string, perm *windows.SecurityAttributes) error {
  34. if volumePath.MatchString(path) {
  35. return nil
  36. }
  37. // The rest of this method is largely copied from os.MkdirAll and should be kept
  38. // as-is to ensure compatibility.
  39. // Fast path: if we can tell whether path is a directory or file, stop with success or error.
  40. dir, err := os.Stat(path)
  41. if err == nil {
  42. if dir.IsDir() {
  43. return nil
  44. }
  45. return &os.PathError{Op: "mkdir", Path: path, Err: syscall.ENOTDIR}
  46. }
  47. // Slow path: make sure parent exists and then call Mkdir for path.
  48. i := len(path)
  49. for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator.
  50. i--
  51. }
  52. j := i
  53. for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element.
  54. j--
  55. }
  56. if j > 1 {
  57. // Create parent.
  58. err = mkdirall(fixRootDirectory(path[:j-1]), perm)
  59. if err != nil {
  60. return err
  61. }
  62. }
  63. // Parent now exists; invoke Mkdir and use its result.
  64. err = mkdirWithACL(path, perm)
  65. if err != nil {
  66. // Handle arguments like "foo/." by
  67. // double-checking that directory doesn't exist.
  68. dir, err1 := os.Lstat(path)
  69. if err1 == nil && dir.IsDir() {
  70. return nil
  71. }
  72. return err
  73. }
  74. return nil
  75. }
  76. // mkdirWithACL creates a new directory. If there is an error, it will be of
  77. // type *PathError. .
  78. //
  79. // This is a modified and combined version of os.Mkdir and windows.Mkdir
  80. // in golang to cater for creating a directory am ACL permitting full
  81. // access, with inheritance, to any subfolder/file for Built-in Administrators
  82. // and Local System.
  83. func mkdirWithACL(name string, sa *windows.SecurityAttributes) error {
  84. if sa == nil {
  85. return os.Mkdir(name, 0)
  86. }
  87. namep, err := windows.UTF16PtrFromString(name)
  88. if err != nil {
  89. return &os.PathError{Op: "mkdir", Path: name, Err: err}
  90. }
  91. err = windows.CreateDirectory(namep, sa)
  92. if err != nil {
  93. return &os.PathError{Op: "mkdir", Path: name, Err: err}
  94. }
  95. return nil
  96. }
  97. // fixRootDirectory fixes a reference to a drive's root directory to
  98. // have the required trailing slash.
  99. func fixRootDirectory(p string) string {
  100. if len(p) == len(`\\?\c:`) {
  101. if os.IsPathSeparator(p[0]) && os.IsPathSeparator(p[1]) && p[2] == '?' && os.IsPathSeparator(p[3]) && p[5] == ':' {
  102. return p + `\`
  103. }
  104. }
  105. return p
  106. }
  107. func makeSecurityAttributes(sddl string) (*windows.SecurityAttributes, error) {
  108. var sa windows.SecurityAttributes
  109. sa.Length = uint32(unsafe.Sizeof(sa))
  110. sa.InheritHandle = 1
  111. var err error
  112. sa.SecurityDescriptor, err = windows.SecurityDescriptorFromString(sddl)
  113. if err != nil {
  114. return nil, err
  115. }
  116. return &sa, nil
  117. }