From df5aaee5813d76923feb102765a00db40bb4e144 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Tue, 17 Dec 2013 22:36:16 +0100 Subject: [PATCH] Some code clean-up. Also removed some obsolete compatibility code. --- etc/Translation/Blank.ts | 8 +-- etc/Translation/LameXP_PL.ts | 8 +-- etc/Translation/LameXP_SV.ts | 8 +-- src/Config.h | 2 +- src/Global_Win32.cpp | 96 +++++++++++++++++++++++++++--------- 5 files changed, 85 insertions(+), 37 deletions(-) diff --git a/etc/Translation/Blank.ts b/etc/Translation/Blank.ts index 4a3744a5..5c2e8482 100644 --- a/etc/Translation/Blank.ts +++ b/etc/Translation/Blank.ts @@ -3310,22 +3310,22 @@ QApplication - + Executable '%1' doesn't support Windows compatibility mode. - + Executable '%1' requires Qt v%2, but found Qt v%3. - + Executable '%1' was built for Qt '%2', but found Qt '%3'. - + Executable '%1' requires Windows XP or later. diff --git a/etc/Translation/LameXP_PL.ts b/etc/Translation/LameXP_PL.ts index 8aab31d4..10a969b0 100644 --- a/etc/Translation/LameXP_PL.ts +++ b/etc/Translation/LameXP_PL.ts @@ -3347,22 +3347,22 @@ QApplication - + Executable '%1' doesn't support Windows compatibility mode. Plik wykonywalny '%1' nie działa w trybie kompatybilności z Windows. - + Executable '%1' requires Qt v%2, but found Qt v%3. Plik wykonywalny '%1' wymaga Qt v%2, znaleziono jednak Qt v%3. - + Executable '%1' was built for Qt '%2', but found Qt '%3'. Plik wykonywalny "%1" został skompilowany dla Qt "%2", znaleziono "%3". - + Executable '%1' requires Windows XP or later. Plik wykonywalny "%1" wymaga Windows XP lub nowszego. diff --git a/etc/Translation/LameXP_SV.ts b/etc/Translation/LameXP_SV.ts index 1615e78e..dcf2720a 100644 --- a/etc/Translation/LameXP_SV.ts +++ b/etc/Translation/LameXP_SV.ts @@ -3330,22 +3330,22 @@ QApplication - + Executable '%1' doesn't support Windows compatibility mode. EXE-filen '%1' stöder inte Windows kompatibilitetsläge. - + Executable '%1' requires Qt v%2, but found Qt v%3. EXE-filen '%1' kräver Qt v%2, du har Qt v%3. - + Executable '%1' was built for Qt '%2', but found Qt '%3'. EXE-filen '%1' är byggd för Qt '%2', du har Qt '%3'. - + Executable '%1' requires Windows XP or later. diff --git a/src/Config.h b/src/Config.h index f816c33c..67fb913e 100644 --- a/src/Config.h +++ b/src/Config.h @@ -35,7 +35,7 @@ #define VER_LAMEXP_MINOR_LO 9 #define VER_LAMEXP_TYPE Alpha #define VER_LAMEXP_PATCH 10 -#define VER_LAMEXP_BUILD 1511 +#define VER_LAMEXP_BUILD 1512 #define VER_LAMEXP_CONFG 1348 /////////////////////////////////////////////////////////////////////////////// diff --git a/src/Global_Win32.cpp b/src/Global_Win32.cpp index d5f771ab..0d78c46d 100644 --- a/src/Global_Win32.cpp +++ b/src/Global_Win32.cpp @@ -894,32 +894,29 @@ static bool lamexp_event_filter(void *message, long *result) */ static bool lamexp_process_is_elevated(bool *bIsUacEnabled = NULL) { - typedef enum { lamexp_token_elevationType_class = 18, lamexp_token_elevation_class = 20 } LAMEXP_TOKEN_INFORMATION_CLASS; - typedef enum { lamexp_elevationType_default = 1, lamexp_elevationType_full, lamexp_elevationType_limited } LAMEXP_TOKEN_ELEVATION_TYPE; - bool bIsProcessElevated = false; if(bIsUacEnabled) *bIsUacEnabled = false; HANDLE hToken = NULL; if(OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) { - LAMEXP_TOKEN_ELEVATION_TYPE tokenElevationType; + TOKEN_ELEVATION_TYPE tokenElevationType; DWORD returnLength; - if(GetTokenInformation(hToken, (TOKEN_INFORMATION_CLASS) lamexp_token_elevationType_class, &tokenElevationType, sizeof(LAMEXP_TOKEN_ELEVATION_TYPE), &returnLength)) + if(GetTokenInformation(hToken, TokenElevationType, &tokenElevationType, sizeof(TOKEN_ELEVATION_TYPE), &returnLength)) { - if(returnLength == sizeof(LAMEXP_TOKEN_ELEVATION_TYPE)) + if(returnLength == sizeof(TOKEN_ELEVATION_TYPE)) { switch(tokenElevationType) { - case lamexp_elevationType_default: + case TokenElevationTypeDefault: qDebug("Process token elevation type: Default -> UAC is disabled.\n"); break; - case lamexp_elevationType_full: + case TokenElevationTypeFull: qWarning("Process token elevation type: Full -> potential security risk!\n"); bIsProcessElevated = true; if(bIsUacEnabled) *bIsUacEnabled = true; break; - case lamexp_elevationType_limited: + case TokenElevationTypeLimited: qDebug("Process token elevation type: Limited -> not elevated.\n"); if(bIsUacEnabled) *bIsUacEnabled = true; break; @@ -928,6 +925,10 @@ static bool lamexp_process_is_elevated(bool *bIsUacEnabled = NULL) break; } } + else + { + qWarning("GetTokenInformation() return an unexpected size!"); + } } CloseHandle(hToken); } @@ -2170,13 +2171,72 @@ QColor lamexp_system_color(const int color_id) } /* - * Check if the current user is in the "admin" group (up to and including Windows XP only) + * Check if the current user is an administartor (helper function) + */ +static bool lamexp_user_is_admin_helper(void) +{ + HANDLE hToken = NULL; + if(!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) + { + return false; + } + + DWORD dwSize = 0; + if(!GetTokenInformation(hToken, TokenGroups, NULL, 0, &dwSize)) + { + if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) + { + CloseHandle(hToken); + return false; + } + } + + PTOKEN_GROUPS lpGroups = (PTOKEN_GROUPS) malloc(dwSize); + if(!lpGroups) + { + CloseHandle(hToken); + return false; + } + + if(!GetTokenInformation(hToken, TokenGroups, lpGroups, dwSize, &dwSize)) + { + free(lpGroups); + CloseHandle(hToken); + return false; + } + + PSID lpSid = NULL; SID_IDENTIFIER_AUTHORITY Authority = {SECURITY_NT_AUTHORITY}; + if(!AllocateAndInitializeSid(&Authority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &lpSid)) + { + free(lpGroups); + CloseHandle(hToken); + return false; + } + + bool bResult = false; + for(DWORD i = 0; i < lpGroups->GroupCount; i++) + { + if(EqualSid(lpSid, lpGroups->Groups[i].Sid)) + { + bResult = true; + break; + } + } + + FreeSid(lpSid); + free(lpGroups); + CloseHandle(hToken); + return bResult; +} + +/* + * Check if the current user is an administartor */ bool lamexp_user_is_admin(void) { bool isAdmin = false; - //Check for process elevation and UAC first! + //Check for process elevation and UAC support first! if(lamexp_process_is_elevated(&isAdmin)) { qWarning("Process is elevated -> user is admin!"); @@ -2187,19 +2247,7 @@ bool lamexp_user_is_admin(void) if(!isAdmin) { qDebug("UAC is disabled/unavailable -> checking for Administrators group"); - - SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; - PSID AdministratorsGroup; - - if(AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup)) - { - BOOL b = FALSE; - if(CheckTokenMembership(NULL, AdministratorsGroup, &b)) - { - if(b) isAdmin = true; - } - FreeSid(AdministratorsGroup); - } + isAdmin = lamexp_user_is_admin_helper(); } return isAdmin;