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

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
PSID pSid;
TCHAR accountName[64];
DWORD tehMask=0;
bool ok;

//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
        : TForm(Owner)
{

}
//---------------------------------------------------------------------------

DWORD TForm2::GetMask()
{
        DWORD tempMask=0;
        if(CheckBox1->Checked) tempMask|=WRITE_DAC|WRITE_OWNER|0x40;
        if(CheckBox2->Checked) tempMask|=DELETE;
        if(CheckBox3->Checked) tempMask|=FILE_EXECUTE;
        if(CheckBox4->Checked) tempMask|=FILE_READ_DATA|FILE_READ_ATTRIBUTES|FILE_READ_EA|READ_CONTROL|SYNCHRONIZE;
        if(CheckBox5->Checked) tempMask|=FILE_WRITE_DATA|FILE_APPEND_DATA|FILE_WRITE_ATTRIBUTES|FILE_WRITE_EA|SYNCHRONIZE;
        Edit2->Text="0x"+(AnsiString)IntToHex((int)tempMask,8);
        return tempMask;
}
void __fastcall TForm2::Button1Click(TObject *Sender)
{
        strcpy(accountName,Edit1->Text.c_str());
        if(GetSidFromName(&pSid,accountName)&&(strlen(accountName))!=0)
        {
                pAcl->AclSize+=sizeof(ACCESS_DENIED_ACE)+GetLengthSid(pSid);

                if(RadioButton1->Checked) ok=AddAccessAllowedAce(pAcl,ACL_REVISION,tehMask,pSid);
                else ok=AddAccessDeniedAce(pAcl,ACL_REVISION,tehMask,pSid);

                if(ok)
                {
                        ok=SetFileSecurity(fileName,DACL_SECURITY_INFORMATION,pSD);
                        if(!ok)
                        MessageBox(this->Handle,"Could not write to Security Descriptor","Error",MB_OK|MB_ICONERROR);
                        Form2->Visible=false;
                }
                //Could not add teh ACE
                else MessageBox(this->Handle,"Could not add teh ACE","Error",MB_OK|MB_ICONERROR);
        }
        else MessageBox(this->Handle,"Could not get Sid from desired account name","Error",MB_OK|MB_ICONERROR);

}
//---------------------------------------------------------------------------

void __fastcall TForm2::CheckBox1Click(TObject *Sender)
{
        if(CheckBox1->Checked)
        {
                CheckBox2->Checked=true;
                CheckBox3->Checked=true;
                CheckBox4->Checked=true;
                CheckBox5->Checked=true;
        }
        tehMask=GetMask();
}
//---------------------------------------------------------------------------

void __fastcall TForm2::Button2Click(TObject *Sender)
{
Form2->Visible=false;
}
//---------------------------------------------------------------------------

void __fastcall TForm2::CheckBox2Click(TObject *Sender)
{

        if(CheckBox2->Checked)
        {
                CheckBox3->Checked=true;
                CheckBox4->Checked=true;
                CheckBox5->Checked=true;
        }
        else
        {
                CheckBox1->Checked=false;
        }
        tehMask=GetMask();
}
//---------------------------------------------------------------------------

void __fastcall TForm2::CheckBox3Click(TObject *Sender)
{
        if(CheckBox3->Checked)
        {
                CheckBox4->Checked=true;
        }
        else
        {
                CheckBox1->Checked=false;
                CheckBox2->Checked=false;
        }
        tehMask=GetMask();
}
//---------------------------------------------------------------------------

void __fastcall TForm2::CheckBox4Click(TObject *Sender)
{
        if(CheckBox4->Checked)
        {
        }
        else
        {
                CheckBox1->Checked=false;
                CheckBox2->Checked=false;
                CheckBox3->Checked=false;
        }
        tehMask=GetMask();
}
//---------------------------------------------------------------------------

void __fastcall TForm2::CheckBox5Click(TObject *Sender)
{
        if(CheckBox4->Checked)
        {
        }
        else
        {
                CheckBox1->Checked=false;
                CheckBox2->Checked=false;
        }
        tehMask=GetMask();
}
//---------------------------------------------------------------------------

