Windows agent wizard: apply permissions recursively

refs #7998
This commit is contained in:
Alexander A. Klimov 2020-05-14 13:30:01 +02:00
parent 5bb929c2e8
commit 4580aaeafa

View File

@ -242,8 +242,8 @@ namespace Icinga
FileSystemRights.Modify, FileSystemRights.Modify,
InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow); InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
try { try {
AddAccessRuleToDir(rule, Program.Icinga2InstallDir); AddAccessRuleToFSTree(rule, Program.Icinga2InstallDir);
AddAccessRuleToDir(rule, Program.Icinga2DataDir); AddAccessRuleToFSTree(rule, Program.Icinga2DataDir);
} catch (System.Security.Principal.IdentityNotMappedException) { } catch (System.Security.Principal.IdentityNotMappedException) {
ShowErrorText("Could not set ACLs for user \"" + serviceUser + "\". Identitiy is not mapped.\n"); ShowErrorText("Could not set ACLs for user \"" + serviceUser + "\". Identitiy is not mapped.\n");
return; return;
@ -283,6 +283,14 @@ namespace Icinga
FinishConfigure(); FinishConfigure();
} }
private void AddAccessRuleToFile(FileSystemAccessRule rule, string file)
{
FileInfo fi = new FileInfo(file);
FileSecurity fs = fi.GetAccessControl();
fs.AddAccessRule(rule);
fi.SetAccessControl(fs);
}
private void AddAccessRuleToDir(FileSystemAccessRule rule, string dir) private void AddAccessRuleToDir(FileSystemAccessRule rule, string dir)
{ {
DirectoryInfo di = new DirectoryInfo(dir); DirectoryInfo di = new DirectoryInfo(dir);
@ -291,6 +299,19 @@ namespace Icinga
di.SetAccessControl(ds); di.SetAccessControl(ds);
} }
private void AddAccessRuleToFSTree(FileSystemAccessRule rule, string root)
{
AddAccessRuleToDir(rule, root);
foreach (string path in Directory.EnumerateDirectories(root, "*", SearchOption.AllDirectories)) {
AddAccessRuleToDir(rule, path);
}
foreach (string path in Directory.EnumerateFiles(root, "*", SearchOption.AllDirectories)) {
AddAccessRuleToFile(rule, path);
}
}
private void FinishConfigure() private void FinishConfigure()
{ {
if (InvokeRequired) { if (InvokeRequired) {