icinga-powershell-framework/lib/core/icingaagent/tests/Test-IcingaAcl.psm1

55 lines
2.3 KiB
PowerShell
Raw Normal View History

2019-09-29 12:25:40 -04:00
function Test-IcingaAcl()
{
param(
[string]$Directory,
2021-08-06 12:12:27 -04:00
[switch]$WriteOutput,
[string]$ServiceUser = (Get-IcingaServiceUser)
2019-09-29 12:25:40 -04:00
);
if ([string]::IsNullOrEmpty($Directory) -Or -Not (Test-Path $Directory)) {
2021-08-06 12:12:27 -04:00
Write-IcingaConsoleWarning 'The specified directory "{0}" was not found' -Objects $Directory;
return $FALSE;
2019-09-29 12:25:40 -04:00
}
$FolderACL = Get-Acl $Directory;
$UserFound = $FALSE;
$HasAccess = $FALSE;
$ServiceUserSID = Get-IcingaUserSID $ServiceUser;
2019-09-29 12:25:40 -04:00
foreach ($user in $FolderACL.Access) {
# Not only check here for the exact name but also for included strings like NT AU or NT-AU or even further later on
# As the Get-Acl Cmdlet will translate usernames into the own language, resultng in 'NT AUTHORITY\NetworkService' being translated
# to 'NT-AUTORITÄT\Netzwerkdienst' for example
$UserSID = $null;
try {
$UserSID = Get-IcingaUserSID $user.IdentityReference;
} catch {
$UserSID = $null;
}
if ($ServiceUserSID -eq $UserSID) {
2019-09-29 12:25:40 -04:00
$UserFound = $TRUE;
if (($user.FileSystemRights -Like '*Modify*' -And $user.FileSystemRights -Like '*Synchronize*') -Or $user.FileSystemRights -like '*FullControl*') {
2019-09-29 12:25:40 -04:00
$HasAccess = $TRUE;
}
}
}
if ($WriteOutput) {
[string]$messageFormat = 'Directory "{0}" {1} by the Icinga Service User "{2}"';
if ($UserFound) {
if ($HasAccess) {
Write-IcingaTestOutput -Severity 'Passed' -Message ([string]::Format($messageFormat, $Directory, 'is accessible and writable', $ServiceUser));
2019-09-29 12:25:40 -04:00
} else {
Write-IcingaTestOutput -Severity 'Failed' -Message ([string]::Format($messageFormat, $Directory, 'is accessible but NOT writable', $ServiceUser));
Write-IcingaConsolePlain "\_ Please run the following command to fix this issue: Set-IcingaAcl -Directory '$Directory'";
2019-09-29 12:25:40 -04:00
}
} else {
Write-IcingaTestOutput -Severity 'Failed' -Message ([string]::Format($messageFormat, $Directory, 'is not accessible', $ServiceUser));
Write-IcingaConsolePlain "\_ Please run the following command to fix this issue: Set-IcingaAcl -Directory '$Directory'";
2019-09-29 12:25:40 -04:00
}
}
return $UserFound;
}