//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

TCHAR fileName[MAX_PATH];
PSECURITY_DESCRIPTOR pSD=0;
DWORD nLength;
TCHAR *ownerName=0;
DWORD ownerNameLength=0;
TCHAR *groupName=0;
DWORD groupNameLength=0;
TCHAR *domainName;
DWORD domainNameLength=0;
SID_NAME_USE peUse;
BOOL ownerAclDefaulted,groupAclDefaulted;
BOOL daclPresent,daclDefaulted;
PACL pAcl;
ACL_SIZE_INFORMATION pAclInfo;
PACCESS_ALLOWED_ACE pAce;


//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button1Click(TObject *Sender)
{
        strcpy(fileName,Edit1->Text.c_str());

        GetFileSecurity(fileName,
                OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
                NULL,
                0,
                &nLength);

        pSD = new PSECURITY_DESCRIPTOR[nLength];

        GetFileSecurity(fileName,
                OWNER_SECURITY_INFORMATION|GROUP_SECURITY_INFORMATION|DACL_SECURITY_INFORMATION,
                pSD,
                nLength,
                &nLength);

        if(CheckBox1->Checked) if(IsValidSecurityDescriptor(pSD)) Output->Lines->Add("//Security Descriptor valid");
        else Output->Lines->Add("//Security Descriptor invalid, error: "+IntToStr(GetLastError()));

        PSID pOwnerSid,pGroupSid;

        GetSecurityDescriptorOwner(pSD,&pOwnerSid,&ownerAclDefaulted);
        GetSecurityDescriptorGroup(pSD,&pGroupSid,&ownerAclDefaulted);

        if(CheckBox1->Checked) if(IsValidSid(pOwnerSid)) Output->Lines->Add("//ownerSid valid");
        else Output->Lines->Add("//ownerSidr invalid, error: "+IntToStr(GetLastError()));

        if(CheckBox1->Checked) if(IsValidSid(pGroupSid)) Output->Lines->Add("//groupSid valid");
        else Output->Lines->Add("//groupSid invalid, error: "+IntToStr(GetLastError()));


        LookupAccountSid(NULL,pOwnerSid,NULL,&ownerNameLength,NULL,&domainNameLength,&peUse);
        ownerName = new TCHAR[ownerNameLength];
        domainName = new TCHAR[domainNameLength];
        LookupAccountSid(NULL,pOwnerSid,ownerName,&ownerNameLength,domainName,&domainNameLength,&peUse);

        delete(domainName);
        domainNameLength=0;

        LookupAccountSid(NULL,pGroupSid,NULL,&groupNameLength,NULL,&domainNameLength,&peUse);
        groupName = new TCHAR[groupNameLength];
        domainName = new TCHAR[domainNameLength];
        LookupAccountSid(NULL,pGroupSid,groupName,&groupNameLength,domainName,&domainNameLength,&peUse);

        Output->Lines->Add("Owner: "+(AnsiString)ownerName+", Group: "+(AnsiString)groupName);

        GetSecurityDescriptorDacl(pSD,&daclPresent,&pAcl,&daclDefaulted);

        if(CheckBox1->Checked) if(IsValidAcl(pAcl)) Output->Lines->Add("//acl valid");
        else Output->Lines->Add("//acl invalid, error: "+IntToStr(GetLastError()));

        GetAclInformation(pAcl,&pAclInfo,sizeof(ACL_SIZE_INFORMATION),AclSizeInformation);


        for(int i=0;i<pAclInfo.AceCount;i++)
        {
                GetAce(pAcl,i,reinterpret_cast<void**>(&pAce));


                Output->Lines->Add("Mask "+IntToStr(pAce->Mask));
                if(pAce->Mask&(0x01ff))  Output->Lines->Add("lmao");
                if(EqualSid(&(pAce->SidStart),pOwnerSid) || EqualSid(&(pAce->SidStart),pGroupSid))
                if(pAce->Header.AceType==ACCESS_DENIED_ACE_TYPE)
                Output->Lines->Add("Deny");
                if(pAce->Header.AceType==ACCESS_ALLOWED_ACE_TYPE)
                Output->Lines->Add("Allow");
                if(pAce->Header.AceType==SYSTEM_AUDIT_ACE_TYPE)
                Output->Lines->Add("System");
                if(EqualSid(&(pAce->SidStart),pOwnerSid) || EqualSid(&(pAce->SidStart),pGroupSid))
                Output->Lines->Append(" of owner");

        }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if(ownerName) delete(ownerName);
if(groupName) delete(groupName);
if(pAce) delete[] (pAce);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
        Output->Lines->Clear();
}
//---------------------------------------------------------------------------