I'm trying to set permission on a file with WinApi. It works fine in 32 bits but not in 64 bits.
It seems the problem come from the EXPLICIT_ACCESS structure size (https://stackoverflow.com/questions/589 ... riesinacla).
Here is the code :
Code: Select all
; #include <Accctrl.h>
; #include <Aclapi.h>
#GRANT_ACCESS = 1
#NO_INHERITANCE = 0
#TRUSTEE_IS_SID = 0
#TRUSTEE_IS_WELL_KNOWN_GROUP = 5
#SECURITY_WORLD_SID_AUTHORITY = 1
#DACL_SECURITY_INFORMATION = 4
Structure NTAUTHORITY
NtAuthority.b[6]
EndStructure
Structure TRUSTEE
*pMultipleTrustee
MultipleTrusteeOperation.l
TrusteeForm.l
TrusteeType.l
*ptstrName
EndStructure
Structure EXPLICIT_ACCESS
grfAccessPermissions.l
grfAccessMode.l
grfInheritance.l
Trustee.TRUSTEE
EndStructure
Procedure SetFilePermission(FileName.s)
Protected Security_Nt_Authority.NTAUTHORITY
Security_Nt_Authority\NtAuthority[5] = #SECURITY_WORLD_SID_AUTHORITY
; Create a well-known SID For the Everyone group.
Protected *pEveryoneSID = #Null
If Not AllocateAndInitializeSid_(@Security_Nt_Authority, 1, #SECURITY_WORLD_RID, 0, 0, 0, 0, 0, 0, 0, @*pEveryoneSID)
Debug "erreur AllocateAndInitializeSid_ : " + GetLastError_()
EndIf
; Initialize an EXPLICIT_ACCESS Structure For an ACE.
Protected Dim ea.EXPLICIT_ACCESS(0)
ea(0)\grfAccessPermissions = $FFFFFFFF ; all access
ea(0)\grfAccessMode = #GRANT_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\MultipleTrusteeOperation = 0
ea(0)\Trustee\ptstrName = *pEveryoneSID
; Create a new ACL that contains the new ACEs.
Protected *pACL = #Null
ret = SetEntriesInAcl_(1, @ea(), #Null, @*pACL)
If ret <> #ERROR_SUCCESS
Debug "erreur SetEntriesInAcl_ : " + ret ; 87 invalid parameter
ProcedureReturn 0
EndIf
; Initialize a security descriptor.
Protected *pSD.SECURITY_DESCRIPTOR = AllocateMemory(#SECURITY_DESCRIPTOR_MIN_LENGTH)
If Not InitializeSecurityDescriptor_(*pSD, #SECURITY_DESCRIPTOR_REVISION)
Debug "erreur InitializeSecurityDescriptor_ : " + GetLastError_()
ProcedureReturn 0
EndIf
; Add the ACL To the security descriptor.
If Not SetSecurityDescriptorDacl_(*pSD,
#True, ; // bDaclPresent flag
*pACL,
#False) ; // Not a Default DACL
Debug "erreur SetSecurityDescriptorDacl_ : " + GetLastError_()
ProcedureReturn 0
EndIf
; Change the security attributes
If Not SetFileSecurity_(FileName, #DACL_SECURITY_INFORMATION, *pSD)
Debug "erreur SetFileSecurity_ : " + GetLastError_()
ProcedureReturn 0
EndIf
If *pEveryoneSID : FreeSid_(*pEveryoneSID) : EndIf
If *pACL : LocalFree_(*pACL) : EndIf
If *pSD : FreeMemory(*pSD) : EndIf
EndProcedure
If CreateFile(0, "c:\test.txt")
CloseFile(0)
SetFilePermission("c:\test.txt")
EndIf