|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | + Configure strict ACLs for a directory so only Administrators, Domain Admins (optional) |
| 4 | + and the specified Icinga service user(s) have FullControl. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | + This function validates the provided accounts, disables inheritance on the target |
| 8 | + directory, clears existing explicit ACL entries, sets the directory owner and |
| 9 | + grants FullControl recursively to the local Administrators group, an optional |
| 10 | + Domain Admins group and one or more Icinga service user accounts. |
| 11 | +
|
| 12 | +.PARAMETER Directory |
| 13 | + The path to the target directory to update ACLs for. Must be a directory path. |
| 14 | +
|
| 15 | +.PARAMETER Owner |
| 16 | + The account to set as owner of the directory. Default is 'Administrators'. |
| 17 | +
|
| 18 | +.PARAMETER IcingaUser |
| 19 | + One or more accounts (string or string[]) which should receive FullControl on |
| 20 | + the directory. Default is the value returned by `Get-IcingaServiceUser`. |
| 21 | +
|
| 22 | +.PARAMETER DomainName |
| 23 | + Optional domain name. When provided the function will also grant FullControl to |
| 24 | + '<DomainName>\Domain Admins'. |
| 25 | +
|
| 26 | +.OUTPUTS |
| 27 | + None. This function writes progress and errors to the Icinga console helpers. |
| 28 | +
|
| 29 | +.NOTES |
| 30 | + - Requires administrative privileges to set ownership and modify ACLs on system |
| 31 | + directories. |
| 32 | + - Intended for use on Windows hosts only. |
| 33 | +#> |
1 | 34 | function Set-IcingaAcl() |
2 | 35 | { |
3 | | - param( |
4 | | - [string]$Directory, |
5 | | - [string]$IcingaUser = (Get-IcingaServiceUser), |
6 | | - [switch]$Remove = $FALSE |
| 36 | + param ( |
| 37 | + [string]$Directory = $null, |
| 38 | + [string]$Owner = 'NT AUTHORITY\SYSTEM', |
| 39 | + [string[]]$IcingaUser = (Get-IcingaServiceUser), |
| 40 | + [string]$DomainName = ($env:USERDOMAIN).ToLower() |
7 | 41 | ); |
8 | 42 |
|
9 | | - if (-Not (Test-Path $Directory)) { |
10 | | - Write-IcingaConsoleWarning 'Unable to set ACL for directory "{0}". Directory does not exist' -Objects $Directory; |
| 43 | + # First check if the directory exists |
| 44 | + if (-not (Test-Path -Path $Directory -PathType Container)) { |
| 45 | + Write-IcingaConsoleWarning -Message 'The folder does not exist: {0}' -Objects $Directory; |
11 | 46 | return; |
12 | 47 | } |
13 | 48 |
|
14 | | - if ($IcingaUser.ToLower() -eq 'nt authority\system' -Or $IcingaUser.ToLower() -like '*localsystem') { |
| 49 | + # Create the owner account object and validate |
| 50 | + try { |
| 51 | + $ownerAccount = New-Object System.Security.Principal.NTAccount($Owner); |
| 52 | + $ownerAccount.Translate([System.Security.Principal.SecurityIdentifier]) | Out-Null; |
| 53 | + } catch { |
| 54 | + Write-IcingaConsoleError -Message 'The owner account does not exist or is invalid: {0}' -Objects $Owner; |
15 | 55 | return; |
16 | 56 | } |
17 | 57 |
|
18 | | - $DirectoryAcl = (Get-Item -Path $Directory).GetAccessControl('Access'); |
19 | | - $DirectoryAccessRule = New-Object System.Security.AccessControl.FileSystemAccessRule( |
20 | | - $IcingaUser, |
21 | | - 'Modify', |
22 | | - 'ContainerInherit,ObjectInherit', |
23 | | - 'None', |
24 | | - 'Allow' |
25 | | - ); |
| 58 | + # Create and validate IcingaUser accounts |
| 59 | + foreach ($user in $IcingaUser) { |
| 60 | + try { |
| 61 | + $userAccount = New-Object System.Security.Principal.NTAccount($user); |
| 62 | + $userAccount.Translate([System.Security.Principal.SecurityIdentifier]) | Out-Null; |
| 63 | + } catch { |
| 64 | + Write-IcingaConsoleError -Message 'The user account does not exist or is invalid: {0}' -Objects $user; |
| 65 | + return; |
| 66 | + } |
| 67 | + } |
26 | 68 |
|
27 | | - if ($Remove -eq $FALSE) { |
28 | | - $DirectoryAcl.SetAccessRule($DirectoryAccessRule); |
29 | | - } else { |
30 | | - foreach ($entry in $DirectoryAcl.Access) { |
31 | | - if (([string]($entry.IdentityReference)).ToLower() -like [string]::Format('*\{0}', $IcingaUser.ToLower())) { |
32 | | - $DirectoryAcl.RemoveAccessRuleSpecific($entry); |
33 | | - } |
| 69 | + # Validate if the local Administrators group exists (shouldn't happen anyway) |
| 70 | + try { |
| 71 | + $adminGroup = New-Object System.Security.Principal.NTAccount('Administrators'); |
| 72 | + $adminGroup.Translate([System.Security.Principal.SecurityIdentifier]) | Out-Null; |
| 73 | + } catch { |
| 74 | + Write-IcingaConsoleError -Message 'The local Administrators group does not exist or is invalid' -Objects $null; |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + [bool]$AddDomainAdmins = $TRUE; |
| 79 | + |
| 80 | + # Validate if the Domain Admins group exists (if DomainName is provided) |
| 81 | + if (-not [string]::IsNullOrEmpty($DomainName) ) { |
| 82 | + try { |
| 83 | + $domainAdminGroup = New-Object System.Security.Principal.NTAccount(([string]::Format('{0}\Domain Admins', $DomainName))); |
| 84 | + $domainAdminGroup.Translate([System.Security.Principal.SecurityIdentifier]) | Out-Null; |
| 85 | + } catch { |
| 86 | + # Continue in this case, just warn the user |
| 87 | + Write-IcingaConsoleWarning -Message 'The Domain Admins group does not exist or is invalid: {0}' -Objects ([string]::Format('{0}\Domain Admins', $DomainName)); |
| 88 | + $AddDomainAdmins = $FALSE; |
34 | 89 | } |
35 | 90 | } |
36 | 91 |
|
37 | | - Set-Acl -Path $Directory -AclObject $DirectoryAcl; |
| 92 | + try { |
| 93 | + # Get the ACL for the directory |
| 94 | + $acl = Get-Acl -Path $Directory; |
| 95 | + |
| 96 | + # Now disable inheritance for the parent folder |
| 97 | + $acl.SetAccessRuleProtection($true, $false) | Out-Null; |
| 98 | + |
| 99 | + # Update the owner of the folder to "Administrators" first, to ensure we don't |
| 100 | + # run into any exceptions |
| 101 | + $acl.SetOwner((New-Object System.Security.Principal.NTAccount('Administrators'))) | Out-Null; |
| 102 | + |
| 103 | + Write-IcingaConsoleNotice -Message 'Disabled inheritance for directory {0}' -Objects $Directory; |
| 104 | + |
| 105 | + # Clear all existing ACL entries to ensure we start fresh |
| 106 | + $acl.Access | ForEach-Object { |
| 107 | + $acl.RemoveAccessRule($_) | Out-Null; |
| 108 | + }; |
| 109 | + |
| 110 | + Write-IcingaConsoleNotice -Message 'Cleared existing ACL entries for directory {0}' -Objects $Directory; |
| 111 | + |
| 112 | + # Add the permission for each defined user with Full Control |
| 113 | + # Only add Icinga user permissions if we are not removing them |
| 114 | + foreach ($user in $IcingaUser) { |
| 115 | + $rule = New-Object System.Security.AccessControl.FileSystemAccessRule( |
| 116 | + $user, |
| 117 | + 'FullControl', |
| 118 | + 'ContainerInherit, ObjectInherit', |
| 119 | + 'None', |
| 120 | + 'Allow' |
| 121 | + ); |
| 122 | + $acl.AddAccessRule($rule) | Out-Null; |
| 123 | + } |
| 124 | + |
| 125 | + # Add local Administrators group (Full Control) |
| 126 | + $adminRule = New-Object System.Security.AccessControl.FileSystemAccessRule( |
| 127 | + 'Administrators', |
| 128 | + 'FullControl', |
| 129 | + 'ContainerInherit, ObjectInherit', |
| 130 | + 'None', |
| 131 | + 'Allow' |
| 132 | + ); |
| 133 | + |
| 134 | + $acl.AddAccessRule($adminRule) | Out-Null; |
| 135 | + |
| 136 | + # We need to ensure we add Domain Admins, as most likely those will require to have access as well |
| 137 | + # and allow them to configure Icinga for Windows |
| 138 | + if ($AddDomainAdmins) { |
| 139 | + $domainAdminRule = New-Object System.Security.AccessControl.FileSystemAccessRule( |
| 140 | + ([string]::Format('{0}\Domain Admins', $DomainName)), |
| 141 | + 'FullControl', |
| 142 | + 'ContainerInherit, ObjectInherit', |
| 143 | + 'None', |
| 144 | + 'Allow' |
| 145 | + ); |
| 146 | + $acl.AddAccessRule($domainAdminRule) | Out-Null; |
| 147 | + } |
| 148 | + |
| 149 | + Write-IcingaConsoleNotice -Message 'Configured new ACL entries for directory {0}' -Objects $Directory; |
| 150 | + |
| 151 | + # Update the ACL on the directory |
| 152 | + Set-Acl -Path $Directory -AclObject $acl | Out-Null; |
| 153 | + |
| 154 | + # Let's now enable inheritance for all subfolders and files, ensuring they inherit the correct permissions |
| 155 | + # from the parent folder and we only require to configure permissions there |
| 156 | + Get-ChildItem -Path $Directory -Recurse -Force | ForEach-Object { |
| 157 | + try { |
| 158 | + $childAcl = Get-Acl -Path $_.FullName; |
| 159 | + $childAcl.SetAccessRuleProtection($false, $true) | Out-Null; |
| 160 | + # As our parent or current Acl might be owned by SYSTEM, |
| 161 | + # we need to set the owner to Administrators here as well to fix exceptions |
| 162 | + # for SYSTEM user not being allowed to own this file |
| 163 | + $childAcl.SetOwner((New-Object System.Security.Principal.NTAccount('Administrators'))) | Out-Null; |
| 164 | + |
| 165 | + Set-Acl -Path $_.FullName -AclObject $childAcl | Out-Null; |
| 166 | + } catch { |
| 167 | + Write-IcingaConsoleWarning -Message 'Failed to enable inheritance for directory {0}: {1}' -Objects $_.FullName, $_.Exception.Message; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + # Ensure we set the owner of the directory and all sub-items correctly |
| 172 | + # Set-Acl cannot do this for the SYSTEM user and ProgramData folders |
| 173 | + # properly, so we need to use icacls for this task |
| 174 | + $Result = & icacls $Directory /setowner $Owner /T /C | Out-Null; |
| 175 | + |
| 176 | + if ($LASTEXITCODE -ne 0) { |
| 177 | + Write-IcingaConsoleError -Message 'Failed to set owner "{0}" for directory {1} and its sub-items using icacls. Output: {2}' -Objects $Owner, $Directory, $Result; |
| 178 | + } |
| 179 | + |
| 180 | + $StringBuilder = New-Object System.Text.StringBuilder; |
| 181 | + $StringBuilder.Append('Permissions for directory "').Append($Directory).Append('" successfully configured for owner "').Append($Owner).Append('"') | Out-Null; |
| 182 | + $StringBuilder.Append(' and full access users (').Append(($IcingaUser -join ', ')).Append(')') | Out-Null; |
| 183 | + |
| 184 | + $StringBuilder.Append(' and groups (Administrators') | Out-Null; |
| 185 | + |
| 186 | + if ($AddDomainAdmins) { |
| 187 | + $StringBuilder.Append(', ').Append(([string]::Format('{0}\Domain Admins)', $DomainName))) | Out-Null; |
| 188 | + } else { |
| 189 | + $StringBuilder.Append(')') | Out-Null; |
| 190 | + }; |
38 | 191 |
|
39 | | - if ($Remove -eq $FALSE) { |
40 | | - Test-IcingaAcl -Directory $Directory -WriteOutput | Out-Null; |
| 192 | + Write-IcingaConsoleNotice -Message $StringBuilder.ToString(); |
| 193 | + } catch { |
| 194 | + Write-IcingaConsoleError -Message 'Failed to Update ACL for directory {0}: {1}' -Objects $Directory, $_.Exception.Message; |
41 | 195 | } |
42 | 196 | } |
0 commit comments