@@ -30,48 +30,147 @@ function Get-RepositoryRoot
30
30
31
31
<#
32
32
. Synopsis
33
- Sets the Secure File ACL.
34
- 1. Removed all user acl except Administrators group, system, and current user
35
- 2. whether or not take the owner
33
+ Set owner of the file to by LOCALSYSTEM account
34
+ Set private host key be fully controlled by LOCALSYSTEM and Administrators
35
+ Set public host key be fully controlled by LOCALSYSTEM and Administrators, read access by everyone
36
36
37
37
. Outputs
38
38
N/A
39
39
40
40
. Inputs
41
41
FilePath - The path to the file
42
- takeowner - if want to take the ownership
43
42
#>
44
- function Cleanup-SecureFileACL
43
+ function Adjust-HostKeyFileACL
45
44
{
46
- [CmdletBinding ()]
47
- param ([string ]$FilePath , [System.Security.Principal.NTAccount ] $Owner )
45
+ param (
46
+ [parameter (Mandatory = $true )]
47
+ [string ]$FilePath
48
+ )
48
49
49
- $myACL = Get-ACL $filePath
50
- $myACL.SetAccessRuleProtection ($True , $True )
51
- Set-Acl - Path $filePath - AclObject $myACL
50
+ $myACL = Get-ACL $FilePath
51
+ $myACL.SetAccessRuleProtection ($True , $FALSE )
52
+ Set-Acl - Path $FilePath - AclObject $myACL
52
53
53
- $myACL = Get-ACL $filePath
54
- if ($owner -ne $null )
54
+ $systemAccount = New-Object System.Security.Principal.NTAccount(" NT AUTHORITY" , " SYSTEM" )
55
+ $adminAccount = New-Object System.Security.Principal.NTAccount(" BUILTIN" , " Administrators" )
56
+ $everyoneAccount = New-Object System.Security.Principal.NTAccount(" EveryOne" )
57
+ $myACL = Get-ACL $FilePath
58
+
59
+ $myACL.SetOwner ($systemAccount )
60
+
61
+ if ($myACL.Access )
55
62
{
56
- $myACL.SetOwner ($owner )
63
+ $myACL.Access | % {
64
+ if (-not ($myACL.RemoveAccessRule ($_ )))
65
+ {
66
+ throw " failed to remove access of $ ( $_.IdentityReference.Value ) rule in setup "
67
+ }
68
+ }
69
+ }
70
+
71
+ $adminACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
72
+ ($adminAccount , " FullControl" , " None" , " None" , " Allow" )
73
+ $myACL.AddAccessRule ($adminACE )
74
+
75
+ $systemACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
76
+ ($systemAccount , " FullControl" , " None" , " None" , " Allow" )
77
+ $myACL.AddAccessRule ($systemACE )
78
+
79
+ if ($FilePath.EndsWith (" .pub" ))
80
+ {
81
+ $everyoneAce = New-Object System.Security.AccessControl.FileSystemAccessRule `
82
+ (" Everyone" , " Read" , " None" , " None" , " Allow" )
83
+ $myACL.AddAccessRule ($everyoneAce )
57
84
}
58
-
85
+ else
86
+ {
87
+ # this only is needed when the private host keys are not registered with agent
88
+ $sshdAce = New-Object System.Security.AccessControl.FileSystemAccessRule `
89
+ (" NT service\sshd" , " Read" , " None" , " None" , " Allow" )
90
+ $myACL.AddAccessRule ($sshdAce )
91
+ }
92
+ Set-Acl - Path $FilePath - AclObject $myACL
93
+ }
94
+
95
+ <#
96
+ . Synopsis
97
+ Set owner of the user key file
98
+ Set ACL to have private user key be fully controlled by LOCALSYSTEM and Administrators, Read, write access by owner
99
+ Set public user key be fully controlled by LOCALSYSTEM and Administrators, Read, write access by owner, read access by everyone
100
+
101
+ . Outputs
102
+ N/A
103
+
104
+ . Inputs
105
+ FilePath - The path to the file
106
+ Owner - owner of the file
107
+ OwnerPerms - the permissions grant to the owner
108
+ #>
109
+ function Adjust-UserKeyFileACL
110
+ {
111
+ param (
112
+ [parameter (Mandatory = $true )]
113
+ [string ]$FilePath ,
114
+ [System.Security.Principal.NTAccount ] $Owner = $null ,
115
+ [System.Security.AccessControl.FileSystemRights []] $OwnerPerms = $null
116
+ )
117
+
118
+ $myACL = Get-ACL $FilePath
119
+ $myACL.SetAccessRuleProtection ($True , $FALSE )
120
+ Set-Acl - Path $FilePath - AclObject $myACL
121
+
122
+ $systemAccount = New-Object System.Security.Principal.NTAccount(" NT AUTHORITY" , " SYSTEM" )
123
+ $adminAccount = New-Object System.Security.Principal.NTAccount(" BUILTIN" , " Administrators" )
124
+ $everyoneAccount = New-Object System.Security.Principal.NTAccount(" EveryOne" )
125
+ $myACL = Get-ACL $FilePath
126
+
127
+ $actualOwner = $null
128
+ if ($Owner -eq $null )
129
+ {
130
+ $actualOwner = New-Object System.Security.Principal.NTAccount($ ($env: USERDOMAIN ), $ ($env: USERNAME ))
131
+ }
132
+ else
133
+ {
134
+ $actualOwner = $Owner
135
+ }
136
+
137
+ $myACL.SetOwner ($actualOwner )
138
+
59
139
if ($myACL.Access )
60
140
{
61
141
$myACL.Access | % {
62
- if (($_ -ne $null ) -and ($_.IdentityReference.Value -ine " BUILTIN\Administrators" ) -and
63
- ($_.IdentityReference.Value -ine " NT AUTHORITY\SYSTEM" ) -and
64
- ($_.IdentityReference.Value -ine " $ ( whoami) " ))
142
+ if (-not ($myACL.RemoveAccessRule ($_ )))
65
143
{
66
- if (-not ($myACL.RemoveAccessRule ($_ )))
67
- {
68
- throw " failed to remove access of $ ( $_.IdentityReference.Value ) rule in setup "
69
- }
144
+ throw " failed to remove access of $ ( $_.IdentityReference.Value ) rule in setup "
70
145
}
71
146
}
147
+ }
148
+
149
+ $adminACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
150
+ ($adminAccount , " FullControl" , " None" , " None" , " Allow" )
151
+ $myACL.AddAccessRule ($adminACE )
152
+
153
+ $systemACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
154
+ ($systemAccount , " FullControl" , " None" , " None" , " Allow" )
155
+ $myACL.AddAccessRule ($systemACE )
156
+
157
+ if ($OwnerPerms )
158
+ {
159
+ $OwnerPerms | % {
160
+ $ownerACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
161
+ ($actualOwner , $_ , " None" , " None" , " Allow" )
162
+ $myACL.AddAccessRule ($ownerACE )
163
+ }
72
164
}
73
165
74
- Set-Acl - Path $filePath - AclObject $myACL
166
+ if ($FilePath.EndsWith (" .pub" ))
167
+ {
168
+ $everyoneAce = New-Object System.Security.AccessControl.FileSystemAccessRule `
169
+ (" Everyone" , " Read" , " None" , " None" , " Allow" )
170
+ $myACL.AddAccessRule ($everyoneAce )
171
+ }
172
+
173
+ Set-Acl - Path $FilePath - AclObject $myACL
75
174
}
76
175
77
176
<#
@@ -88,20 +187,27 @@ function Cleanup-SecureFileACL
88
187
#>
89
188
function Add-PermissionToFileACL
90
189
{
91
- [ CmdletBinding ()]
92
- param (
190
+ param (
191
+ [ parameter ( Mandatory = $true )]
93
192
[string ]$FilePath ,
193
+ [parameter (Mandatory = $true )]
94
194
[System.Security.Principal.NTAccount ] $User ,
95
- [System.Security.AccessControl.FileSystemRights ]$Perm
195
+ [parameter (Mandatory = $true )]
196
+ [System.Security.AccessControl.FileSystemRights []]$Perms
96
197
)
97
198
98
- $myACL = Get-ACL $filePath
199
+ $myACL = Get-ACL $FilePath
99
200
100
- $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
101
- ($User , $perm , " None" , " None" , " Allow" )
102
- $myACL.AddAccessRule ($objACE )
201
+ if ($Perms )
202
+ {
203
+ $Perms | % {
204
+ $userACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
205
+ ($User , $_ , " None" , " None" , " Allow" )
206
+ $myACL.AddAccessRule ($userACE )
207
+ }
208
+ }
103
209
104
- Set-Acl - Path $filePath - AclObject $myACL
210
+ Set-Acl - Path $FilePath - AclObject $myACL
105
211
}
106
212
107
- Export-ModuleMember - Function Get-RepositoryRoot , Add-PermissionToFileACL , Cleanup - SecureFileACL
213
+ Export-ModuleMember - Function Get-RepositoryRoot , Add-PermissionToFileACL , Adjust - HostKeyFileACL , Adjust - UserKeyFileACL
0 commit comments