You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.3 KiB
111 lines
2.3 KiB
//
|
|
// Copyright © Microsoft Corporation. All rights reserved.
|
|
//
|
|
// CImpersonate class implementation
|
|
// Created: 6/3/2000
|
|
// Author: khughes
|
|
|
|
#include <precomp.h>
|
|
#include <chstring.h>
|
|
#include "AutoImpRevert.h"
|
|
|
|
|
|
|
|
CAutoImpRevert::CAutoImpRevert(
|
|
BOOL fOpenAsSelf)
|
|
: m_hOriginalUser(INVALID_HANDLE_VALUE),
|
|
m_dwLastError(ERROR_SUCCESS)
|
|
{
|
|
GetCurrentImpersonation(fOpenAsSelf);
|
|
}
|
|
|
|
|
|
CAutoImpRevert::~CAutoImpRevert()
|
|
{
|
|
if(m_hOriginalUser != INVALID_HANDLE_VALUE)
|
|
{
|
|
Revert();
|
|
}
|
|
}
|
|
|
|
|
|
bool CAutoImpRevert::GetCurrentImpersonation(
|
|
BOOL fOpenAsSelf)
|
|
{
|
|
bool fRet = false;
|
|
::SetLastError(ERROR_SUCCESS);
|
|
m_dwLastError = ERROR_SUCCESS;
|
|
|
|
// Store the current user's handle...
|
|
if(::OpenThreadToken(
|
|
::GetCurrentThread(),
|
|
TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE,
|
|
fOpenAsSelf,
|
|
&m_hOriginalUser))
|
|
{
|
|
fRet = true;
|
|
}
|
|
else
|
|
{
|
|
m_dwLastError = ::GetLastError();
|
|
if(m_dwLastError == ERROR_NO_TOKEN)
|
|
{
|
|
::SetLastError(ERROR_SUCCESS);
|
|
if(::ImpersonateSelf(SecurityImpersonation))
|
|
{
|
|
if(::OpenThreadToken(
|
|
::GetCurrentThread(),
|
|
TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE,
|
|
fOpenAsSelf,
|
|
&m_hOriginalUser))
|
|
{
|
|
fRet = true;
|
|
m_dwLastError = ERROR_SUCCESS;
|
|
}
|
|
else
|
|
{
|
|
m_dwLastError = ::GetLastError();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
m_dwLastError = ::GetLastError();
|
|
}
|
|
}
|
|
}
|
|
|
|
return fRet;
|
|
}
|
|
|
|
|
|
|
|
bool CAutoImpRevert::Revert()
|
|
{
|
|
bool fRet = false;
|
|
|
|
if(m_hOriginalUser != INVALID_HANDLE_VALUE)
|
|
{
|
|
if(::ImpersonateLoggedOnUser(m_hOriginalUser))
|
|
{
|
|
CloseHandle(m_hOriginalUser);
|
|
m_hOriginalUser = INVALID_HANDLE_VALUE;
|
|
fRet = true;
|
|
}
|
|
else
|
|
{
|
|
m_dwLastError = ::GetLastError();
|
|
}
|
|
}
|
|
|
|
return fRet;
|
|
}
|
|
|
|
DWORD CAutoImpRevert::LastError() const
|
|
{
|
|
return m_dwLastError;
|
|
}
|
|
|
|
|
|
|
|
|
|
|