Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Guys,

How to remove an ACE from a DACL of a directory/file. A sample will be of great help. I am writing a C++ program which removes (and stores somewhere) all the Deny ACE from DACL of a file/Directory for the group "Everyone". Also, During GetFileSecurity, what SecurityInformation should I query - OWNER_SECURITY_INFORMATION / DACL_SECURITY_INFORMATION or both.

PS:- Will vote generously for anybody that can help
--
Cheers

Varun
Posted

void RemoveEveryoneDeny()
{
SECURITY_DESCRIPTOR *psd;
DWORD needed;

GetFileSecurity(_T("test.txt"), DACL_SECURITY_INFORMATION, NULL, 0, &needed); //find out how big the security descriptor is
psd = (SECURITY_DESCRIPTOR*)malloc(needed); //allocate that memory
GetFileSecurity(_T("test.txt"), DACL_SECURITY_INFORMATION, psd, needed, &needed); //fill out the security descriptor

PACL dacl;
BOOL present, defaulted;
ULONG entries;
EXPLICIT_ACCESS *ea;

GetSecurityDescriptorDacl(psd, &present, &dacl, &defaulted);
GetExplicitEntriesFromAcl(dacl, &entries, &ea);
//do what you need to to save information from the explicit access
//array, the amount of entries that it contains is in entries, and
//the TRUSTEE structure that it contains will contain the SID of
//the user account, so you can compare it to the well known Everyone
//SID or use LookupAccountSid to get the name from it and compare that
//with the Everyone string

PACL newacl;
EXPLICIT_ACCESS ea2 = {0};
ea2.grfAccessMode = SET_ACCESS; //overrides any current acl permissions
ea2.grfAccessPermissions = STANDARD_RIGHTS_ALL; //this is filled in from the file permissions, see CreateFile
ea2.grfInheritance = NO_INHERITANCE;
ea2.Trustee.MultipleTrusteeOperation = NO_MULTIPLE_TRUSTEE; //must be set to this
ea2.Trustee.pMultipleTrustee = nullptr; //must be null
ea2.Trustee.TrusteeForm = TRUSTEE_IS_NAME; //this says that ptstrName points at a string
ea2.Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; //Everyone is the World SID, and is a well known group
ea2.Trustee.ptstrName = _T("Everyone"); //The world SID name

SetEntriesInAcl(1, &ea2, dacl, &newacl);

SECURITY_DESCRIPTOR sd2;
InitializeSecurityDescriptor(&sd2, SECURITY_DESCRIPTOR_REVISION);
SetSecurityDescriptorDacl(&sd2, TRUE, newacl, FALSE);

SetFileSecurity(_T("test.txt"), DACL_SECURITY_INFORMATION, &sd2);

}
 
Share this answer
 
I found a relatively very simple solution. Get the security descriptor by GetNamedSecurityInfo or GetFileSecurity. Extract the DACL and iterate. Now Check for the flags and other information, if matched, call the api DeleteAce and pass the index number.

Later on I started using api, AddAce, for adding entries in ACL. Surprisingly, using AddAce isn't that simple. Reason - Order of the ACEs in an ACL is specific. As far as what I could understand, user defined ACEs are to be placed first. Thus, I had to create a new ACL and copy new ACE and rest of the old ace every time I needed to add an ACE. I am still searching for a better method to do so. Or atleast to rearrange ACEs in an ACL. Any suggestions?

--
Varun
Thanks for the reply anyways, gxavi85. Please do comment if you think I can Improve this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900