///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Winnov L.P., 1996.  All rights reserved
// security.cpp: CSecurity class implementation.
///////////////////////////////////////////////////////////////////////////////

#include <windows.h>
#include <malloc.h>
#include "wnverr.h"
#include "debug.h"
#include "security.h"
#include "log.h"

///////////////////////////////////////////////////////////////////////////////

CSecurity::CSecurity ()
{
    pSD = NULL;
}

CSecurity::~CSecurity () {} // destructor stub

///////////////////////////////////////////////////////////////////////////////

PSECURITY_ATTRIBUTES CSecurity::GetSecurityAttributes (void)
{
    return &sa;
}

///////////////////////////////////////////////////////////////////////////////

void CSecurity::Close (void)
{
    if (pSD)
    {
	free ((PVOID)pSD);
	pSD = NULL;
    }
}

///////////////////////////////////////////////////////////////////////////////

DWORD CSecurity::Open (void)
{
    DWORD dwResult;

    // create a security descriptor that allows anyone to write
    pSD = (PSECURITY_DESCRIPTOR) malloc (SECURITY_DESCRIPTOR_MIN_LENGTH);
    if (!pSD)
    {
	LogEvent (WNVERR_WNVIRQ_NOT_ENOUGH_MEMORY, L"\n********** CSecurity::Open: malloc failed.");
	Close ();
	SetLastError (WNVERR_WNVIRQ_NOT_ENOUGH_MEMORY);
	return WNVERR_WNVIRQ_NOT_ENOUGH_MEMORY;
    }

    if (!InitializeSecurityDescriptor(pSD, SECURITY_DESCRIPTOR_REVISION))
    {
	dwResult = GetLastError ();
	LogEvent (dwResult, L"\n********** CSecurity::Open: InitializeSecurityDescriptor failed");
	Close ();
	return dwResult;
    }

    // add a NULL disc. ACL to the security descriptor.
    //
    if (!SetSecurityDescriptorDacl(pSD, TRUE, (PACL) NULL, FALSE))
    {
	dwResult = GetLastError ();
	LogEvent (dwResult, L"\n********** CSecurity::Open: SetSecurityDescriptorDacl failed");
	Close ();
	return dwResult;
    }

    sa.nLength = sizeof(sa);
    sa.lpSecurityDescriptor = pSD;
    sa.bInheritHandle = TRUE;

    return 0;	// pass
}
