Back

// PSLPrivilege.cpp : Implementation of CPSLPrivilege

#include "stdafx.h"
#include "PSLPrivilege.h"

#define LUID_TO_QWORD(luid) (((__int64)luid.HighPart) << 32) + luid.LowPart

CPSLPrivilege::CPSLPrivilege()
{
   m_sName = _T("");

   m_bEnabled = false;
   m_bDefault = false;
   m_bRemoved = false;
   m_bUsedForAccess = false;

   m_Luid.HighPart = 0;
   m_Luid.LowPart = 0;
}

CPSLPrivilege::~CPSLPrivilege()
{

}

HRESULT CPSLPrivilege::FinalConstruct()
{
   return S_OK;
}

void CPSLPrivilege::FinalRelease()
{
}

void CPSLPrivilege::Initialize(LUID_AND_ATTRIBUTES & Attributes, LPCTSTR sPrivilegeName)
{
   m_sName = sPrivilegeName;
   m_Luid = Attributes.Luid;
   if(Attributes.Attributes)
   {
      m_bEnabled = (Attributes.Attributes & SE_PRIVILEGE_ENABLED) == SE_PRIVILEGE_ENABLED;
      m_bDefault = (Attributes.Attributes & SE_PRIVILEGE_ENABLED_BY_DEFAULT) == SE_PRIVILEGE_ENABLED_BY_DEFAULT;
      m_bRemoved = (Attributes.Attributes & SE_PRIVILEGE_REMOVED) == SE_PRIVILEGE_REMOVED;
      m_bUsedForAccess = (Attributes.Attributes & SE_PRIVILEGE_USED_FOR_ACCESS) == SE_PRIVILEGE_USED_FOR_ACCESS;
   }
}

bool CPSLPrivilege::EnablePrivilege(bool bEnable)
{
   TOKEN_PRIVILEGES tp;
   tp.PrivilegeCount = 1;
   tp.Privileges[0].Luid = m_Luid;
   if(bEnable)
      tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
   else
      tp.Privileges[0].Attributes = 0;

   bool bSuccess = false;
   HANDLE hToken = NULL;
   if(::OpenProcessToken(::GetCurrentProcess(), TOKEN_WRITE, &hToken))
   {
      bSuccess = (::AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES), NULL, NULL) == TRUE);
      ::CloseHandle(hToken);
   }
   return bSuccess;
}

////////////////////////////////////////////////////////////////////////
// Interface Implementation;
////////////////////////////////////////////////////////////////////////

STDMETHODIMP CPSLPrivilege::get_Name(BSTR * pValue)
{
   PSL_BEGIN

   *pValue = m_sName.copy();

   PSL_END
}

STDMETHODIMP CPSLPrivilege::get_Enabled(VARIANT_BOOL * pValue)
{
   PSL_BEGIN

   *pValue = m_bEnabled?VARIANT_TRUE:VARIANT_FALSE;

   PSL_END
}

STDMETHODIMP CPSLPrivilege::put_Enabled(VARIANT_BOOL newValue)
{
   PSL_BEGIN

   bool bEnabling = newValue?true:false;
   if(m_bEnabled != bEnabling)
   {
      if(EnablePrivilege(bEnabling))
         m_bEnabled = bEnabling;
   }

   PSL_END
}

STDMETHODIMP CPSLPrivilege::get_Default(VARIANT_BOOL * pValue)
{
   PSL_BEGIN

   *pValue = m_bDefault?VARIANT_TRUE:VARIANT_FALSE;

   PSL_END
}

STDMETHODIMP CPSLPrivilege::get_Removed(VARIANT_BOOL * pValue)
{
   PSL_BEGIN

   *pValue = m_bRemoved?VARIANT_TRUE:VARIANT_FALSE;

   PSL_END
}

STDMETHODIMP CPSLPrivilege::get_UsedForAccess(VARIANT_BOOL * pValue)
{
   PSL_BEGIN

   *pValue = m_bUsedForAccess?VARIANT_TRUE:VARIANT_FALSE;

   PSL_END
}

STDMETHODIMP CPSLPrivilege::get_LUID(VARIANT * pValue)
{
   PSL_BEGIN

   CPSLUtilities::SetVariant64Bit(pValue, LUID_TO_QWORD(m_Luid));

   PSL_END
}

Top