diff --git a/scwx-qt/scwx-qt.cmake b/scwx-qt/scwx-qt.cmake index 92ca70a4..087c61e5 100644 --- a/scwx-qt/scwx-qt.cmake +++ b/scwx-qt/scwx-qt.cmake @@ -353,7 +353,8 @@ set(SRC_UI_SETUP source/scwx/qt/ui/setup/audio_codec_page.cpp source/scwx/qt/ui/setup/map_provider_page.cpp source/scwx/qt/ui/setup/setup_wizard.cpp source/scwx/qt/ui/setup/welcome_page.cpp) -set(HDR_UTIL source/scwx/qt/util/color.hpp +set(HDR_UTIL source/scwx/qt/util/check_privilege.hpp + source/scwx/qt/util/color.hpp source/scwx/qt/util/file.hpp source/scwx/qt/util/geographic_lib.hpp source/scwx/qt/util/imgui.hpp @@ -367,7 +368,8 @@ set(HDR_UTIL source/scwx/qt/util/color.hpp source/scwx/qt/util/q_file_input_stream.hpp source/scwx/qt/util/time.hpp source/scwx/qt/util/tooltip.hpp) -set(SRC_UTIL source/scwx/qt/util/color.cpp +set(SRC_UTIL source/scwx/qt/util/check_privilege.cpp + source/scwx/qt/util/color.cpp source/scwx/qt/util/file.cpp source/scwx/qt/util/geographic_lib.cpp source/scwx/qt/util/imgui.cpp diff --git a/scwx-qt/source/scwx/qt/main/main.cpp b/scwx-qt/source/scwx/qt/main/main.cpp index 7d66c80d..e8a833b4 100644 --- a/scwx-qt/source/scwx/qt/main/main.cpp +++ b/scwx-qt/source/scwx/qt/main/main.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,8 @@ #include #include +#include + #define QT6CT_LIBRARY #include #undef QT6CT_LIBRARY @@ -73,6 +76,20 @@ int main(int argc, char* argv[]) QCoreApplication::installTranslator(&translator); } + // Test to see if scwx was run with high privilege + if (scwx::qt::util::is_high_privilege()) + { + QMessageBox::StandardButton pressed = QMessageBox::warning( + nullptr, + "Warning: Running with High Privileges", + "Although Supercell-Wx can be run with high privileges, " + "it is not recommended", + QMessageBox::Ok | QMessageBox::Close); + if (pressed & QMessageBox::Ok) { + return 0; + } + } + if (!scwx::util::GetEnvironment("SCWX_TEST").empty()) { QStandardPaths::setTestModeEnabled(true); diff --git a/scwx-qt/source/scwx/qt/util/check_privilege.cpp b/scwx-qt/source/scwx/qt/util/check_privilege.cpp new file mode 100644 index 00000000..c33f87b4 --- /dev/null +++ b/scwx-qt/source/scwx/qt/util/check_privilege.cpp @@ -0,0 +1,43 @@ +#include "scwx/qt/util/check_privilege.hpp" +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +namespace scwx { +namespace qt { +namespace util { + +bool is_high_privilege() +{ +#if defined(_WIN32) + bool isAdmin = false; + HANDLE token = NULL; + TOKEN_ELEVATION elevation; + DWORD elevationSize = sizeof(TOKEN_ELEVATION); + + if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) + { + return false; + } + if (!GetTokenInformation( + token, TokenElevation, &elevation, elevationSize, &elevationSize)) + { + CloseHandle(token); + return false; + } + isAdmin = elevation.TokenIsElevated; + CloseHandle(token); + return isAdmin; +#elif defined(Q_OS_UNIX) + // On UNIX root is always uid 0. On Linux this is enforced by the kernal. + return geteuid() == 0; +#endif +} + +} // namespace util +} // namespace qt +} // namespace scwx diff --git a/scwx-qt/source/scwx/qt/util/check_privilege.hpp b/scwx-qt/source/scwx/qt/util/check_privilege.hpp new file mode 100644 index 00000000..1234f0ef --- /dev/null +++ b/scwx-qt/source/scwx/qt/util/check_privilege.hpp @@ -0,0 +1,11 @@ +#pragma once + +namespace scwx { +namespace qt { +namespace util { + +bool is_high_privilege(); + +} // namespace util +} // namespace qt +} // namespace scwx