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 --- include/m_utils.h | 3 +++ libs/win32/mir_app.lib | Bin 223622 -> 223922 bytes libs/win64/mir_app.lib | Bin 219346 -> 219646 bytes plugins/PluginUpdater/src/DlgListNew.cpp | 17 ++------------ plugins/PluginUpdater/src/DlgUpdate.cpp | 18 ++------------- 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 ++++++++++++++++++++----------- 9 files changed, 42 insertions(+), 44 deletions(-) diff --git a/include/m_utils.h b/include/m_utils.h index 421f52a3a3..026666bbce 100644 --- a/include/m_utils.h +++ b/include/m_utils.h @@ -412,6 +412,9 @@ namespace PU // Checks if we're working via pu_stub or not MIR_APP_DLL(bool) IsDirect(); + // Checks if Miranda's folder is writeable + MIR_APP_DLL(bool) IsMirandaFolderWritable(); + // Checks if a process has enough rights to write into Miranda's folder MIR_APP_DLL(bool) IsProcessElevated(); diff --git a/libs/win32/mir_app.lib b/libs/win32/mir_app.lib index b9a50b309c..600c648a6e 100644 Binary files a/libs/win32/mir_app.lib and b/libs/win32/mir_app.lib differ diff --git a/libs/win64/mir_app.lib b/libs/win64/mir_app.lib index 3ffdb4de90..d414d547c8 100644 Binary files a/libs/win64/mir_app.lib and b/libs/win64/mir_app.lib differ diff --git a/plugins/PluginUpdater/src/DlgListNew.cpp b/plugins/PluginUpdater/src/DlgListNew.cpp index 8dcbc35592..bcc050163f 100644 --- a/plugins/PluginUpdater/src/DlgListNew.cpp +++ b/plugins/PluginUpdater/src/DlgListNew.cpp @@ -129,21 +129,8 @@ public: ImageList_AddIconFromIconLib(hIml, IDI_INFO); m_list.SetImageList(hIml, LVSIL_SMALL); - if (IsWinVer7Plus()) { - TFileName szPath; - GetModuleFileNameW(nullptr, szPath, _countof(szPath)); - wchar_t *ext = wcsrchr(szPath, '.'); - if (ext != nullptr) - *ext = '\0'; - wcscat(szPath, L".test"); - HANDLE hFile = CreateFileW(szPath, GENERIC_WRITE, FILE_SHARE_READ, nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); - if (hFile == INVALID_HANDLE_VALUE) - Button_SetElevationRequiredState(GetDlgItem(m_hwnd, IDOK), !PU::IsProcessElevated()); - else { - CloseHandle(hFile); - DeleteFile(szPath); - } - } + if (!PU::IsMirandaFolderWritable()) + Button_SetElevationRequiredState(GetDlgItem(m_hwnd, IDOK), !PU::IsProcessElevated()); ////////////////////////////////////////////////////////////////////////////////////// LVCOLUMN lvc = { 0 }; diff --git a/plugins/PluginUpdater/src/DlgUpdate.cpp b/plugins/PluginUpdater/src/DlgUpdate.cpp index baa2d0e90d..90a5a3be48 100644 --- a/plugins/PluginUpdater/src/DlgUpdate.cpp +++ b/plugins/PluginUpdater/src/DlgUpdate.cpp @@ -203,22 +203,8 @@ public: Window_SetIcon_IcoLib(m_hwnd, g_plugin.getIconHandle(IDI_MENU)); - if (IsWinVerVistaPlus()) { - TFileName wszPath; - GetModuleFileName(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) - // Running Windows Vista or later (major version >= 6). - Button_SetElevationRequiredState(btnOk.GetHwnd(), !PU::IsProcessElevated()); - else { - CloseHandle(hFile); - DeleteFileW(wszPath); - } - } + if (!PU::IsMirandaFolderWritable()) + Button_SetElevationRequiredState(btnOk.GetHwnd(), !PU::IsProcessElevated()); // Initialize the LVCOLUMN structure. // The mask specifies that the format, width, text, and 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