Bazaar/SysExec : fixed SysXXXAdmin() environment passing when app is already in elevated mode

git-svn-id: svn://ultimatepp.org/upp/trunk@3080 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
micio 2011-01-24 16:20:38 +00:00
parent f59da848b5
commit 6c1daadc05
3 changed files with 91 additions and 2 deletions

View file

@ -7,6 +7,86 @@
NAMESPACE_UPP
////////////////////////////////////////////////////////////////////////////////////
// utility functions to check whether an app is running in elevated mode
static bool IsVistaOrLater(void)
{
OSVERSIONINFO osvi;
BOOL bIsWindowsXPorLater;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
return (osvi.dwMajorVersion >= 6);
}
static BOOL IsGroupMember(DWORD dwRelativeID, BOOL bProcessRelative, BOOL* pIsMember)
{
HANDLE hToken, hDupToken;
PSID pSid = NULL;
SID_IDENTIFIER_AUTHORITY SidAuthority = SECURITY_NT_AUTHORITY;
if (!pIsMember)
{
SetLastError(ERROR_INVALID_USER_BUFFER);
return FALSE;
}
if (bProcessRelative || !OpenThreadToken(GetCurrentThread(), TOKEN_QUERY | TOKEN_DUPLICATE, TRUE, &hToken))
{
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY | TOKEN_DUPLICATE, &hToken))
return FALSE;
}
if (!DuplicateToken(hToken, SecurityIdentification, &hDupToken))
{
CloseHandle(hToken);
return FALSE;
}
CloseHandle(hToken);
hToken = hDupToken;
if (!AllocateAndInitializeSid(&SidAuthority, 2,
SECURITY_BUILTIN_DOMAIN_RID, dwRelativeID, 0, 0, 0, 0, 0, 0,
&pSid))
{
CloseHandle(hToken);
return FALSE;
}
if (!CheckTokenMembership(hToken, pSid, pIsMember))
{
CloseHandle(hToken);
FreeSid(pSid);
*pIsMember = FALSE;
return FALSE;
}
CloseHandle(hToken);
FreeSid(pSid);
return TRUE;
}
// check if user is running in admin mode
bool IsUserAdministrator(void)
{
BOOL isAdmin;
// always an admin for XP and previous versions
if(!IsVistaOrLater())
return true;
// check if running in admin mode for Vista or newers
IsGroupMember(DOMAIN_ALIAS_RID_ADMINS, FALSE, &isAdmin);
return isAdmin;
}
////////////////////////////////////////////////////////////////////////////////////
// as I found that ShellExecuteEx don't take environment from current process
// but gets it from registry, the only way I found to pass it to spawned process