Fix permissions

This commit is contained in:
Bluzume 2021-01-19 01:33:58 +13:00 committed by GitHub
parent 0d4c30d64f
commit 46a86f53ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 14 additions and 9 deletions

View File

@ -42,17 +42,22 @@ namespace DontTouchMyFlash
{ {
FileSecurity fileS = File.GetAccessControl(filepath); FileSecurity fileS = File.GetAccessControl(filepath);
IdentityReference sid = fileS.GetOwner(typeof(SecurityIdentifier)); SecurityIdentifier cu = WindowsIdentity.GetCurrent().User;
string ntAccount = sid.Translate(typeof(NTAccount)).ToString(); SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
if(ntAccount == @"NT SERVICE\TrustedInstaller")
{
SecurityIdentifier cu = WindowsIdentity.GetCurrent().User;
fileS.SetOwner(cu);
fileS.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.FullControl, AccessControlType.Allow));
File.SetAccessControl(filepath, fileS); fileS.SetOwner(cu);
} fileS.SetAccessRuleProtection(false, false);
fileS.RemoveAccessRuleAll(new FileSystemAccessRule(everyone, FileSystemRights.FullControl, AccessControlType.Deny));
fileS.RemoveAccessRuleAll(new FileSystemAccessRule(cu, FileSystemRights.FullControl, AccessControlType.Deny));
fileS.SetAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.FullControl, AccessControlType.Allow));
fileS.SetAccessRule(new FileSystemAccessRule(cu, FileSystemRights.FullControl , AccessControlType.Allow));
File.SetAccessControl(filepath, fileS);
File.SetAttributes(filepath, FileAttributes.Normal);
} }
public bool CheckFileAndAdd(string filepath) public bool CheckFileAndAdd(string filepath)
{ {
try try