From dcaae5dea7a22183576ee6ce3045cacab085a56c Mon Sep 17 00:00:00 2001 From: George Hazan Date: Mon, 10 Jan 2022 18:12:15 +0300 Subject: PU::IsMirandaFolderWritable - core function to detect if need to draw a shield on buttons that require escalation --- src/mir_app/src/db_ini.cpp | 8 ++++++++ src/mir_app/src/mir_app.def | 1 + src/mir_app/src/mir_app64.def | 1 + src/mir_app/src/pu_utils.cpp | 38 +++++++++++++++++++++++++------------- 4 files changed, 35 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/mir_app/src/db_ini.cpp b/src/mir_app/src/db_ini.cpp index e4eb35c047..978c802679 100644 --- a/src/mir_app/src/db_ini.cpp +++ b/src/mir_app/src/db_ini.cpp @@ -227,6 +227,14 @@ public: { m_iniPath.SetText(m_path); m_newPath.SetText(m_path); + + if (!PU::IsMirandaFolderWritable()) { + bool bIsElevated = PU::IsProcessElevated(); + Button_SetElevationRequiredState(btnMove.GetHwnd(), !bIsElevated); + Button_SetElevationRequiredState(btnDelete.GetHwnd(), !bIsElevated); + Button_SetElevationRequiredState(btnRecycle.GetHwnd(), !bIsElevated); + } + return true; } diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index 575fe053e1..174144ffcc 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -790,3 +790,4 @@ _Netlib_SslWrite@12 @877 NONAME ?AllowTyping@CSrmmBaseDialog@@IBE_NXZ @878 NONAME ?setTyping@CSrmmBaseDialog@@QAEXHPBUUSERINFO@@@Z @879 NONAME ?SafeRecycleBin@PU@@YGHPB_W@Z @880 NONAME +?IsMirandaFolderWritable@PU@@YG_NXZ @881 NONAME diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index f867e78aa7..52a1e3b701 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -790,3 +790,4 @@ Netlib_SslWrite @877 NONAME ?AllowTyping@CSrmmBaseDialog@@IEBA_NXZ @878 NONAME ?setTyping@CSrmmBaseDialog@@QEAAXHPEBUUSERINFO@@@Z @879 NONAME ?SafeRecycleBin@PU@@YAHPEB_W@Z @880 NONAME +?IsMirandaFolderWritable@PU@@YA_NXZ @881 NONAME diff --git a/src/mir_app/src/pu_utils.cpp b/src/mir_app/src/pu_utils.cpp index 3851b4d949..87a78ac374 100644 --- a/src/mir_app/src/pu_utils.cpp +++ b/src/mir_app/src/pu_utils.cpp @@ -74,20 +74,40 @@ MIR_APP_DLL(bool) PU::IsDirect() return g_hPipe == nullptr; } +///////////////////////////////////////////////////////////////////////////////////////// +// Checks if Miranda's folder is writeable + +MIR_APP_DLL(bool) PU::IsMirandaFolderWritable() +{ + if (!IsWinVerVistaPlus()) + return true; + + wchar_t wszPath[MAX_PATH]; + GetModuleFileNameW(nullptr, wszPath, _countof(wszPath)); + wchar_t *ext = wcsrchr(wszPath, '.'); + if (ext != nullptr) + *ext = '\0'; + wcscat(wszPath, L".test"); + HANDLE hFile = CreateFileW(wszPath, GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); + if (hFile == INVALID_HANDLE_VALUE) + return false; + + CloseHandle(hFile); + DeleteFileW(wszPath); + return true; +} + ///////////////////////////////////////////////////////////////////////////////////////// // Checks if a process has enough rights to write into Miranda's folder MIR_APP_DLL(bool) PU::IsProcessElevated() { bool bIsElevated = false; - uint32_t dwError = ERROR_SUCCESS; HANDLE hToken = nullptr; // Open the primary access token of the process with TOKEN_QUERY. - if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) { - dwError = GetLastError(); + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) goto Cleanup; - } // Retrieve token elevation information. TOKEN_ELEVATION elevation; @@ -97,7 +117,6 @@ MIR_APP_DLL(bool) PU::IsProcessElevated() // Vista, GetTokenInformation returns FALSE with the // ERROR_INVALID_PARAMETER error code because TokenElevation is // not supported on those operating systems. - dwError = GetLastError(); goto Cleanup; } @@ -105,15 +124,8 @@ MIR_APP_DLL(bool) PU::IsProcessElevated() Cleanup: // Centralized cleanup for all allocated resources. - if (hToken) { + if (hToken) CloseHandle(hToken); - hToken = nullptr; - } - - // Throw the error if something failed in the function. - if (ERROR_SUCCESS != dwError) { - throw dwError; - } return bIsElevated; } -- cgit v1.2.3