public/Set-XFileMode.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255


function Set-XFileMode() {
<#
.SYNOPSIS
    Short description
.DESCRIPTION
    Long description
.EXAMPLE
    PS C:\> <example usage>
    Explanation of what the example does
 
.PARAMETER Path
    The path to the file or directory that must be changed
 
.PARAMETER Umask
    The unix permission code such as 755 or 0644
 
.PARAMETER Clean
    Clean is a dangerous flag that will remove all other access control
    list data and only retain the user, group, and other permissions.
     
    On windows, "Everyone" is used for other, the owner is the user, and
    the primary group is the group.
 
.PARAMETER Force
    Force is used avoid the prompt when the -Clean flag is used.
 
.INPUTS
    Inputs (if any)
.OUTPUTS
    Output (if any)
.NOTES
    General notes
#>

    [CmdletBinding(SupportsShouldProcess = $true)]
    Param(
        [ValidateNotNullOrEmpty()]
        [Parameter(ValueFromRemainingArguments = $true)]
        [String] $Path,

        [ValidateNotNullOrEmpty()]
        [Parameter(Position = 0)]
        [String] $Mode, 

        [Alias("r")]
        [Switch] $Recurse,

        [Alias("c")]
        [Switch] $Clean,

        [Alias("f")]
        [Switch] $Force
    )

    PROCESS {
        if($Clean.ToBool())
        {
            if(!$Force.ToBool()) {
                $d = Read-Host -Prompt "Are you sure you want to remove other permissions? "
                if(!($d -eq "y" -or $d[0] -eq "y")) {
                    Write-Host "$d was chosen. Action Cancelled."
                    return 
                }
            }
        }
        if(Test-OsPlatform "Linux", "Mac") {
            $cmd = "chmod"
            $splat += "$Path"
            if($Recurse.ToBool()) {
                $splat += "-R"
            }
            $splat += $Code.ToString()

            if($PSCmdlet.ShouldProcess("$cmd $([string]::IsNullOrWhiteSpace($splat))")) {
                & $cmd @splat 
            }

            

            if($Clean.ToBool() -and $Force.ToBool()) {
                $noAcl = $null -eq (Get-Command setfacl -EA SilentlyContinue)
                if($noAcl)
                {
                    if($PSCmdlet.ShouldProcess("Write-Warning `"setfacl is not installed`""))
                    {
                        Write-Warning "setfacl is not installed"
                        return;
                    }
                }

                if($PSCmdlet.ShouldProcess("setfacl -D e $Path")) {
                    setfacl -D e $Path 
                }
                
            }
            
        } else {

            $parts =  $Mode
            if(![char]::IsDigit($Mode[0])) {
                throw "Set-XFileMode on windows currently only supports digit umasks"
            }

            if($Mode.Length -lt 3) {
                throw "Set-XFileMode on windows requires at least 3 digits"
            }

            if($Mode.Length -gt 4) {
                throw "Set-XFileMode on windows umask number cannot be greater than 4 digits"
            }

            if($parts.Length -gt 3) {
                $parts = $parts.Substring(1)
            }

            $u = $parts[0]
            $g = $parts[1]
            $e = $parts[2]

          
    
            $objects = @();

          
            $data = (Get-ACL $Path)
            foreach($d in $data.Access) {
                $objects += $d.IdentityReference.Value
            }

            $splat = @($Path)
    
            function Get-ChmodSplat() {
                Param(
                    [String] $Name,
    
                    [String] $Code 
                )
    
                $splat = @();
    
                switch($Code) {
                    "0" { 
                        $splat += "/remove:g"
                        $splat += $Name  
                    }
                    "1" {
                        $splat += "/grant"
                        $splat += "${Name}:(ge)"
                    }
                    "2" {
                        $splat += "/grant"
                        $splat += "${Name}:(gw)"
                    }
                    "3" {
                        $splat += "/grant"
                        $splat += "${NAME}:(gw,ge)"
                    }
                    "4" {
                        $splat += "/grant"
                        $splat += "${Name}:(r)" 
                    }
                    "5" {
                        $splat += "/grant"
                        $splat += "${Name}:(rx)" 
                    }
                    "6" {
                        $splat += "/grant"
                        $splat += "${Name}:(m)" 
                    }
                    "7" {
                        $splat += "/grant"
                        $splat += "${Name}:(f)" 
                    }
                }

                return $splat 
            }
    
            if($Clean.ToBool() -and $Force.ToBool()) {
                $filter = @($data.Owner, $data.Group, "BUILTIN\Users", "Everyone")
                
                $splat += "/inheritance:r"
               

                foreach($o in $objects) {
                  
                    if($filter.Contains($o)) {
                        continue;
                    }
    
                    $splat += "/remove:g"
                    $splat += $o
                }
            }
    
            # NOTES: Group is the Posix compatible primary group
            # Its not really used by windows

            # Also Groups on windows tend not to be named the same
            # as a user. So if the group is the same as the owner
            # we should take the highest permission
            
            if($data.Owner -eq $data.Group) {
                $o = $u;
                if($g -gt $u) {
                    $o = $g;
                }
    
                $next = Get-ChmodSplat -Name $data.Owner -Code $o 
                foreach($n in $next) {
                    $splat += $n
                }
                
            } else {
                $next = Get-ChmodSplat -Name $data.Owner -Code $u 
                foreach($n in $next) {
                    $splat += $n
                }
                
                $next = Get-ChmodSplat -Name $data.Group -Code $g
                foreach($n in $next) {
                    $splat += $n
                }
            }
    
    
            
            if($objects.Contains($users) -and $data.Group -ne $users -and $data.Owner -ne $users) {
                $next = Get-ChmodSplat -Name $users -Code $g
                foreach($n in $next) {
                    $splat += $n
                }
            }
            
            $removeEveryone = $objects.Contains("Everyone") -and $e -eq 0
            if($removeEveryone -or ($Clean.ToBool() -and $Force.ToBool())) {
                $next = Get-ChmodSplat -Name "Everyone" -Code $e
                foreach($n in $next) {
                    $splat += $n
                }
            }

            if($Recurse.ToBool()) {
                $splat += "/t"
            }

            $cmd = "icacls.exe"
    
            if($PSCmdlet.ShouldProcess("$cmd $([string]::Join(" ", $splat))")) {
                & $cmd @splat 
            }
        }
    }
}