Core: ProcessHasAdminRights() function is added to utility functions. (#242)

* Core: ProcessHasAdminRights() function is added to utility functions.

* reference/DetectPrivegedProcess: Cosmetics.

* Core: ProcessHasAdminRights() renamed as IsAdmin()

* Core: IsAdmin() renamed as IsUserAdmin().
This commit is contained in:
İsmail Yılmaz 2025-04-18 13:46:36 +03:00 committed by GitHub
parent 707f3f7602
commit 1c48bd493c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 0 deletions

View file

@ -0,0 +1,11 @@
description "Detects and warns if the app is running in root/admin mode.\377";
uses
CtrlLib;
file
main.cpp;
mainconfig
"" = "GUI";

View file

@ -0,0 +1,12 @@
#include <CtrlLib/CtrlLib.h>
using namespace Upp;
GUI_APP_MAIN
{
// Run the example as administrator (Windows) or as root (POSIX) to get the warning.
if(IsUserAdmin())
Exclamation(t_("Warning: Application has administrator/root rights.&This might pose a security risk!"));
else
PromptOK(t_("Application has user rights."));
}

View file

@ -864,5 +864,29 @@ String GetProgramDataFolder() { return String("/var/opt"); }
#endif
bool IsUserAdmin()
{
#ifdef PLATFORM_POSIX
return geteuid() == 0;
#elif PLATFORM_WIN32
BOOL isAdmin = FALSE;
PSID pAdminGroup = nullptr;
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
if(AllocateAndInitializeSid(
&NtAuthority,
2,
SECURITY_BUILTIN_DOMAIN_RID,
DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&pAdminGroup)) {
CheckTokenMembership(nullptr, pAdminGroup, &isAdmin);
FreeSid(pAdminGroup);
}
return isAdmin;
#else
// Unsupported platform. (Assume no elevation.)
return false;
#endif
}
}

View file

@ -116,4 +116,6 @@ String GetTemplatesFolder();
String GetDownloadFolder();
String GetProgramDataFolder();
bool IsUserAdmin();
void InstallCrashHook(void (*h)());

View file

@ -224,6 +224,11 @@ tring]_[* GetDesktopManager]()&]
the value of `"DESKTOP`_SESSION`" environment variable is returned.&]
[s3; &]
[s4; &]
[s5;:Upp`:`:IsUserAdmin`(`): [@(0.0.255) bool] [* IsUserAdmin]()&]
[s2;%% Return true if the current process is running as administrator
(Windows) or root (POSIX).&]
[s3; &]
[s4; &]
[s5;:LaunchWebBrowser`(const String`&`): [@(0.0.255) void]_[* LaunchWebBrowser]([@(0.0.255) c
onst]_[_^topic`:`/`/Core`/src`/String`$en`-us`#String`:`:class^ String][@(0.0.255) `&
]_[*@3 url])&]