Mega Code Archive

 
Categories / Delphi / Files
 

Copy NTFS file security from a source to a destination

Title: Copy NTFS file security from a source to a destination Question: By default, copying a folder or file to a destination on an NTFS partition, the destination file takes on the security and access control settings of the destinations parent folder. This function provides an easy way to also copy the original security/ACL settings to the destination file, or copy security settings from another file/folder. Answer: Requires JEDI JCL - http://sourceforge.net/projects/jcl uses Windows, JwaAclApi, JwaWinNT, JwaAccCtrl, JwaWinBase; function CopyNTFSSecurity(const Source, Dest: string): boolean; var SidOwner: PSID; SidGroup: PSID; Dacl: PACL; Sacl: PACL; SecDescPtr: PSECURITY_DESCRIPTOR; begin Result := false; if GetNamedSecurityInfo(PChar(Source), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION or SACL_SECURITY_INFORMATION or OWNER_SECURITY_INFORMATION or GROUP_SECURITY_INFORMATION, @SidOwner, @SidGroup, @Dacl, @Sacl, SecDescPtr) = ERROR_SUCCESS then begin Result := SetNamedSecurityInfo(PChar(Dest), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION or SACL_SECURITY_INFORMATION or OWNER_SECURITY_INFORMATION or GROUP_SECURITY_INFORMATION, SidOwner, SidGroup, Dacl, Sacl) = ERROR_SUCCESS; LocalFree(HLOCAL(SecDescPtr)); end; end;