filePermissions.ps1 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2019 The Kubernetes Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. Param(
  15. [string]$FileName = $(throw "-FileName is required.")
  16. )
  17. # read = read data | read attributes
  18. $READ_PERMISSIONS = 0x0001 -bor 0x0080
  19. # write = write data | append data | write attributes | write EA
  20. $WRITE_PERMISSIONS = 0x0002 -bor 0x0004 -bor 0x0100 -bor 0x0010
  21. # execute = read data | file execute
  22. $EXECUTE_PERMISSIONS = 0x0001 -bor 0x0020
  23. function GetFilePermissions($path) {
  24. $objPath = "Win32_LogicalFileSecuritySetting='$path'"
  25. $output = Invoke-WmiMethod -Namespace root/cimv2 -Path $objPath -Name GetSecurityDescriptor
  26. if ($output.ReturnValue -ne 0) {
  27. $retVal = $output.ReturnValue
  28. Write-Error "GetSecurityDescriptor invocation failed with code: $retVal"
  29. exit 1
  30. }
  31. $fileSD = $output.Descriptor
  32. $fileOwnerGroup = $fileSD.Group
  33. $fileOwner = $fileSD.Owner
  34. if ($fileOwnerGroup.Name -eq $null -and $fileOwnerGroup.Domain -eq $null) {
  35. # the file owner's group is not recognized. Check if the Owner itself is
  36. # a group, and if so, default the group to it.
  37. net user $fileOwner.Name > $null 2> $null
  38. if (-not $?) {
  39. $fileOwnerGroup = $fileOwner
  40. }
  41. }
  42. $userMask = 0
  43. $groupMask = 0
  44. $otherMask = 0
  45. foreach ($ace in $fileSD.DACL) {
  46. $mask = 0
  47. if ($ace.AceType -ne 0) {
  48. # not an Allow ACE, skip.
  49. continue
  50. }
  51. # convert mask.
  52. if ( ($ace.AccessMask -band $READ_PERMISSIONS) -eq $READ_PERMISSIONS ) {
  53. $mask = $mask -bor 4
  54. }
  55. if ( ($ace.AccessMask -band $WRITE_PERMISSIONS) -eq $WRITE_PERMISSIONS ) {
  56. $mask = $mask -bor 2
  57. }
  58. if ( ($ace.AccessMask -band $EXECUTE_PERMISSIONS) -eq $EXECUTE_PERMISSIONS ) {
  59. $mask = $mask -bor 1
  60. }
  61. # detect mask type.
  62. if ($ace.Trustee.Equals($fileOwner)) {
  63. $userMask = $mask
  64. }
  65. if ($ace.Trustee.Equals($fileOwnerGroup)) {
  66. $groupMask = $mask
  67. }
  68. if ($ace.Trustee.Name.ToLower() -eq "users") {
  69. $otherMask = $mask
  70. }
  71. }
  72. return "$userMask$groupMask$otherMask"
  73. }
  74. $mask = GetFilePermissions($FileName)
  75. if (-not $?) {
  76. exit 1
  77. }
  78. # print the permission mask Linux-style.
  79. echo "0$mask"