From a44bde6f94fc6e303aa90795c85c0552de6fe965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 10 Feb 2026 20:25:50 +0800 Subject: [PATCH] Platform: adds UID / SID detection --- src/common/FFPlatform.h | 5 +++++ src/common/impl/FFPlatform_unix.c | 3 ++- src/common/impl/FFPlatform_windows.c | 25 ++++++++++++++++++++++-- src/detection/displayserver/linux/wmde.c | 2 +- src/detection/lm/lm_linux.c | 2 +- src/detection/users/users_linux.c | 2 +- src/modules/title/title.c | 6 ++++++ 7 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/common/FFPlatform.h b/src/common/FFPlatform.h index 2962cbd17..e174469ad 100644 --- a/src/common/FFPlatform.h +++ b/src/common/FFPlatform.h @@ -20,6 +20,11 @@ typedef struct FFPlatform FFlist dataDirs; // List of FFstrbuf, trailing slash included FFstrbuf exePath; // The real path of current exe (empty if unavailable) + #ifndef _WIN32 + uint32_t uid; + #else + FFstrbuf sid; + #endif FFstrbuf userName; FFstrbuf fullUserName; FFstrbuf hostName; diff --git a/src/common/impl/FFPlatform_unix.c b/src/common/impl/FFPlatform_unix.c index 6b25bd537..5d6ac07b3 100644 --- a/src/common/impl/FFPlatform_unix.c +++ b/src/common/impl/FFPlatform_unix.c @@ -227,7 +227,8 @@ static void getSysinfo(FFPlatformSysinfo* info, const struct utsname* uts) void ffPlatformInitImpl(FFPlatform* platform) { - struct passwd* pwd = getpwuid(getuid()); + platform->uid = getuid(); + struct passwd* pwd = getpwuid(platform->uid); struct utsname uts; if(uname(&uts) < 0) diff --git a/src/common/impl/FFPlatform_windows.c b/src/common/impl/FFPlatform_windows.c index 25062ba83..022a39e35 100644 --- a/src/common/impl/FFPlatform_windows.c +++ b/src/common/impl/FFPlatform_windows.c @@ -1,6 +1,7 @@ #include "FFPlatform_private.h" #include "common/io.h" #include "common/library.h" +#include "common/mallocHelper.h" #include "common/stringUtils.h" #include "common/windows/unicode.h" #include "common/windows/registry.h" @@ -8,6 +9,7 @@ #include #include +#include #define SECURITY_WIN32 1 // For secext.h #include @@ -148,9 +150,28 @@ static void getUserName(FFPlatform* platform) } wchar_t buffer[256]; - DWORD len = ARRAY_SIZE(buffer); - if (GetUserNameExW(NameDisplay, buffer, &len)) + DWORD size = ARRAY_SIZE(buffer); + if (GetUserNameExW(NameDisplay, buffer, &size)) ffStrbufSetWS(&platform->fullUserName, buffer); + + size = 0; + DWORD refDomainSize = 0; + SID_NAME_USE sidNameUse = SidTypeUnknown; + LookupAccountNameA(NULL, userName, NULL, &size, NULL, &refDomainSize, &sidNameUse); + if (size > 0) + { + FF_AUTO_FREE PSID sid = (PSID) malloc(size); + FF_AUTO_FREE char* refDomain = (char*) malloc(refDomainSize); + if (LookupAccountNameA(NULL, userName, sid, &size, refDomain, &refDomainSize, &sidNameUse)) + { + LPWSTR sidString; + if (ConvertSidToStringSidW(sid, &sidString)) + { + ffStrbufSetWS(&platform->sid, sidString); + LocalFree(sidString); + } + } + } } static void getHostName(FFPlatform* platform) diff --git a/src/detection/displayserver/linux/wmde.c b/src/detection/displayserver/linux/wmde.c index 58c66fba9..503721589 100644 --- a/src/detection/displayserver/linux/wmde.c +++ b/src/detection/displayserver/linux/wmde.c @@ -266,7 +266,7 @@ static void applyPrettyNameIfDE(FFDisplayServerResult* result, const char* name) static const char* getFromProcesses(FFDisplayServerResult* result) { - uint32_t userId = getuid(); + uint32_t userId = instance.state.platform.uid; #if __FreeBSD__ #ifdef __DragonFly__ diff --git a/src/detection/lm/lm_linux.c b/src/detection/lm/lm_linux.c index c336e5796..c73adf0cc 100644 --- a/src/detection/lm/lm_linux.c +++ b/src/detection/lm/lm_linux.c @@ -142,7 +142,7 @@ const char* ffDetectLM(FFLMResult* result) { // On some incorrectly configured systems, $XDG_SESSION_ID is not set. Try finding it ourself // WARNING: This is private data. Do not parse - ffStrbufSetF(&path, FF_SYSTEMD_USERS_PATH "%d", getuid()); + ffStrbufSetF(&path, FF_SYSTEMD_USERS_PATH "%d", instance.state.platform.uid); // This is actually buggy, and assumes current user is using DE // `sd_pid_get_session` can be a better option, but we need to find a pid to use diff --git a/src/detection/users/users_linux.c b/src/detection/users/users_linux.c index 46d8a02e5..357c1ab7d 100644 --- a/src/detection/users/users_linux.c +++ b/src/detection/users/users_linux.c @@ -100,7 +100,7 @@ const char* detectBySystemd(FFUsersOptions* options, FFlist* users) if (options->myselfOnly) { - ffStrbufAppendUInt(&pathUsers, getuid()); + ffStrbufAppendUInt(&pathUsers, instance.state.platform.uid); detectUserBySystemd(&pathUsers, users); } else diff --git a/src/modules/title/title.c b/src/modules/title/title.c index d846b2817..d75446e55 100644 --- a/src/modules/title/title.c +++ b/src/modules/title/title.c @@ -121,6 +121,11 @@ void ffGenerateTitleJsonConfig(FFTitleOptions* options, yyjson_mut_doc* doc, yyj bool ffGenerateTitleJsonResult(FF_MAYBE_UNUSED FFTitleOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) { yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result"); + #ifdef _WIN32 + yyjson_mut_obj_add_strbuf(doc, obj, "userId", &instance.state.platform.sid); + #else + yyjson_mut_obj_add_uint(doc, obj, "userId", instance.state.platform.uid); + #endif yyjson_mut_obj_add_strbuf(doc, obj, "userName", &instance.state.platform.userName); yyjson_mut_obj_add_strbuf(doc, obj, "fullUserName", &instance.state.platform.fullUserName); yyjson_mut_obj_add_strbuf(doc, obj, "hostName", &instance.state.platform.hostName); @@ -169,5 +174,6 @@ FFModuleBaseInfo ffTitleModuleInfo = { {"@ symbol (colored)", "at-symbol-colored"}, {"Host name (colored)", "host-name-colored"}, {"Full user name", "full-user-name"}, + {"UID / SID", "user-id"}, })) };