How to change ACL from C ++?

How to change ACL from C ++?

Can anyone help me do the following from C ++ without any confirmation:

cacls c:\personal\file.txt /d everyone 
+2
source share
3 answers

Use the following code

 #include <Accctrl.h> #include <Aclapi.h> void SetFilePermission(LPCTSTR FileName) { PSID pEveryoneSID = NULL; PACL pACL = NULL; EXPLICIT_ACCESS ea[1]; SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY; // Create a well-known SID for the Everyone group. AllocateAndInitializeSid(&SIDAuthWorld, 1, SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, &pEveryoneSID); // Initialize an EXPLICIT_ACCESS structure for an ACE. ZeroMemory(&ea, 1 * sizeof(EXPLICIT_ACCESS)); ea[0].grfAccessPermissions = 0xFFFFFFFF; ea[0].grfAccessMode = DENY_ACCESS; ea[0].grfInheritance= NO_INHERITANCE; ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID; ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; ea[0].Trustee.ptstrName = (LPTSTR) pEveryoneSID; // Create a new ACL that contains the new ACEs. SetEntriesInAcl(1, ea, NULL, &pACL); // Initialize a security descriptor. PSECURITY_DESCRIPTOR pSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH); InitializeSecurityDescriptor(pSD,SECURITY_DESCRIPTOR_REVISION); // Add the ACL to the security descriptor. SetSecurityDescriptorDacl(pSD, TRUE, // bDaclPresent flag pACL, FALSE); // not a default DACL //Change the security attributes SetFileSecurity(FileName, DACL_SECURITY_INFORMATION, pSD); if (pEveryoneSID) FreeSid(pEveryoneSID); if (pACL) LocalFree(pACL); if (pSD) LocalFree(pSD); } 
+12
source

I assume you mean the Windows system? You need to use the NTFS part of the Win32 API that cacls uses. Browse the MSDN, it will be there somewhere. For example SetSecurityInfo

+1
source

If you do not want to interfere with the API (i.e. SetNamedSecurityInfo), you can bypass the request like this:

 echo y|cacls filename /d everyone 

Since echo is built-in, in order to call this command line from your program, you probably have to run:

 cmd.exe /c echo y|cacls filename /d everyone 
0
source

Source: https://habr.com/ru/post/1313053/


All Articles