Bazaar/SysExec : exported a couple of system info functions

git-svn-id: svn://ultimatepp.org/upp/trunk@3089 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
micio 2011-01-25 11:04:27 +00:00
parent d35920972b
commit 43b13d7bc4
6 changed files with 118 additions and 82 deletions

View file

@ -7,85 +7,6 @@
NAMESPACE_UPP NAMESPACE_UPP
////////////////////////////////////////////////////////////////////////////////////
// utility functions to check whether an app is running in elevated mode
static bool IsVistaOrLater(void)
{
OSVERSIONINFO osvi;
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 // 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 // but gets it from registry, the only way I found to pass it to spawned process

View file

@ -3,13 +3,12 @@
#include <Core/Core.h> #include <Core/Core.h>
#include "SysInfo.h"
NAMESPACE_UPP NAMESPACE_UPP
#ifdef PLATFORM_WIN32 #ifdef PLATFORM_WIN32
// check if user is running in admin mode
bool IsUserAdministrator(void);
// executes a command via shell "runas" as admin user; // executes a command via shell "runas" as admin user;
// if wait is true, will wait for command end, otherwise executes it in background // if wait is true, will wait for command end, otherwise executes it in background
bool ShellExec(String const &args, VectorMap<String, String> const &env, bool wait); bool ShellExec(String const &args, VectorMap<String, String> const &env, bool wait);

View file

@ -3,6 +3,10 @@
#include <Core/Core.h> #include <Core/Core.h>
#ifdef PLATFORM_WIN32
#include "SysInfo.h"
#endif
NAMESPACE_UPP NAMESPACE_UPP
/////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////

View file

@ -6,6 +6,8 @@ uses
link(LINUX) -lutil; link(LINUX) -lutil;
file file
SysInfo.h,
SysInfo.cpp,
ArgEnv.h, ArgEnv.h,
ArgEnv.cpp, ArgEnv.cpp,
SudoLib.h, SudoLib.h,

View file

@ -0,0 +1,88 @@
#include "SysInfo.h"
#ifdef PLATFORM_WIN32
NAMESPACE_UPP
////////////////////////////////////////////////////////////////////////////////////
// utility functions to if running Vista or newer OSs
bool IsVistaOrLater(void)
{
OSVERSIONINFO osvi;
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;
}
////////////////////////////////////////////////////////////////////////////////////
// utility functions to check whether an app is running in elevated 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;
}
END_UPP_NAMESPACE
#endif

22
bazaar/SysExec/SysInfo.h Normal file
View file

@ -0,0 +1,22 @@
#ifndef _SysExec_SysInfo_h_
#define _SysExec_SysInfo_h_
#include <Core/Core.h>
NAMESPACE_UPP
#ifdef PLATFORM_WIN32
////////////////////////////////////////////////////////////////////////////////////
// utility functions to if running Vista or newer OSs
bool IsVistaOrLater(void);
////////////////////////////////////////////////////////////////////////////////////
// check if user is running in admin mode
bool IsUserAdministrator(void);
#endif
END_UPP_NAMESPACE
#endif