diff --git a/bazaar/SysExec/ShellLib.cpp b/bazaar/SysExec/ShellLib.cpp index 15ef0fe57..d58c5405f 100644 --- a/bazaar/SysExec/ShellLib.cpp +++ b/bazaar/SysExec/ShellLib.cpp @@ -7,85 +7,6 @@ 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 // but gets it from registry, the only way I found to pass it to spawned process diff --git a/bazaar/SysExec/ShellLib.h b/bazaar/SysExec/ShellLib.h index 490793b63..004428998 100644 --- a/bazaar/SysExec/ShellLib.h +++ b/bazaar/SysExec/ShellLib.h @@ -3,13 +3,12 @@ #include +#include "SysInfo.h" + NAMESPACE_UPP #ifdef PLATFORM_WIN32 -// check if user is running in admin mode -bool IsUserAdministrator(void); - // executes a command via shell "runas" as admin user; // if wait is true, will wait for command end, otherwise executes it in background bool ShellExec(String const &args, VectorMap const &env, bool wait); diff --git a/bazaar/SysExec/SysExec.h b/bazaar/SysExec/SysExec.h index 2245842f3..d02a615db 100644 --- a/bazaar/SysExec/SysExec.h +++ b/bazaar/SysExec/SysExec.h @@ -3,6 +3,10 @@ #include +#ifdef PLATFORM_WIN32 +#include "SysInfo.h" +#endif + NAMESPACE_UPP /////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/bazaar/SysExec/SysExec.upp b/bazaar/SysExec/SysExec.upp index fd7949671..bfca5fcc5 100644 --- a/bazaar/SysExec/SysExec.upp +++ b/bazaar/SysExec/SysExec.upp @@ -6,6 +6,8 @@ uses link(LINUX) -lutil; file + SysInfo.h, + SysInfo.cpp, ArgEnv.h, ArgEnv.cpp, SudoLib.h, diff --git a/bazaar/SysExec/SysInfo.cpp b/bazaar/SysExec/SysInfo.cpp new file mode 100644 index 000000000..123e670f2 --- /dev/null +++ b/bazaar/SysExec/SysInfo.cpp @@ -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 diff --git a/bazaar/SysExec/SysInfo.h b/bazaar/SysExec/SysInfo.h new file mode 100644 index 000000000..10f730986 --- /dev/null +++ b/bazaar/SysExec/SysInfo.h @@ -0,0 +1,22 @@ +#ifndef _SysExec_SysInfo_h_ +#define _SysExec_SysInfo_h_ + +#include + +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