mirror of
https://github.com/ultimatepp/ultimatepp.git
synced 2026-05-15 06:05:58 -06:00
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:
parent
707f3f7602
commit
1c48bd493c
5 changed files with 54 additions and 0 deletions
|
|
@ -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";
|
||||
|
||||
12
reference/DetectPrivilegedProcess/main.cpp
Normal file
12
reference/DetectPrivilegedProcess/main.cpp
Normal 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."));
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -116,4 +116,6 @@ String GetTemplatesFolder();
|
|||
String GetDownloadFolder();
|
||||
String GetProgramDataFolder();
|
||||
|
||||
bool IsUserAdmin();
|
||||
|
||||
void InstallCrashHook(void (*h)());
|
||||
|
|
@ -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])&]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue