I'm trying to get a function working in Lazarus that should add a File-ACL to the yet existing ACL of that object. Obviously I'm doing somethinh wrong: The program crashes with a SIGSEGV when it reaches BuildExplicitAccessWithName. Regarding the debugger, this function calls BuildTrusteeWithObjectsAndName iternally which then leads to the SIGSEGV. What am I doing wrong here? Here's my code:
program acltest;
uses JwaWindows;
function AddFileACL(Filename, TrusteeName: AnsiString; AccessMode: ACCESS_MODE; Inheritance: dWord): Boolean; stdcall;
var
pExplicitAccess : PEXPLICIT_ACCESS;
ExistingDacl : PACL;
pExistingDacl : PPACL;
NewAcl : PACL;
psd : PSECURITY_DESCRIPTOR;
begin
NewAcl := nil;
psd := nil;
pExistingDacl := nil;
Result := false;
try
if ERROR_SUCCESS = GetNamedSecurityInfo(pAnsiChar(Filename), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nil, nil, pExistingDacl, nil, psd) then
begin
try
BuildExplicitAccessWithName(pExplicitAccess, PAnsiChar(TrusteeName), GENERIC_ALL, AccessMode, Inheritance);
ExistingDacl := pExistingDacl^;
if ERROR_SUCCESS = SetEntriesInAcl(1, pExplicitAccess, ExistingDacl, NewAcl) then
begin
if ERROR_SUCCESS = SetNamedSecurityInfo(pAnsiChar(Filename), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, nil, nil, NewAcl, nil) then
begin
Result := true;
end;
end;
finally
end;
end;
finally
end;
end;
begin
if AddFileACL('C:\Users\keckc\Desktop\test.txt', 'Everyone', GRANT_ACCESS, SUB_CONTAINERS_AND_OBJECTS_INHERIT) = true then
begin
writeln('Yep, it works!');
end
else begin
writeln('Nope, try again!');
end;
end.
try...finally
looks quite pointless. Also, the lastif...then
could be converted to plain assignment statement.