Skip to content

Commit a44bde6

Browse files
committed
Platform: adds UID / SID detection
1 parent 5825bce commit a44bde6

7 files changed

Lines changed: 39 additions & 6 deletions

File tree

src/common/FFPlatform.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ typedef struct FFPlatform
2020
FFlist dataDirs; // List of FFstrbuf, trailing slash included
2121
FFstrbuf exePath; // The real path of current exe (empty if unavailable)
2222

23+
#ifndef _WIN32
24+
uint32_t uid;
25+
#else
26+
FFstrbuf sid;
27+
#endif
2328
FFstrbuf userName;
2429
FFstrbuf fullUserName;
2530
FFstrbuf hostName;

src/common/impl/FFPlatform_unix.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ static void getSysinfo(FFPlatformSysinfo* info, const struct utsname* uts)
227227

228228
void ffPlatformInitImpl(FFPlatform* platform)
229229
{
230-
struct passwd* pwd = getpwuid(getuid());
230+
platform->uid = getuid();
231+
struct passwd* pwd = getpwuid(platform->uid);
231232

232233
struct utsname uts;
233234
if(uname(&uts) < 0)

src/common/impl/FFPlatform_windows.c

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
#include "FFPlatform_private.h"
22
#include "common/io.h"
33
#include "common/library.h"
4+
#include "common/mallocHelper.h"
45
#include "common/stringUtils.h"
56
#include "common/windows/unicode.h"
67
#include "common/windows/registry.h"
78
#include "common/windows/nt.h"
89

910
#include <windows.h>
1011
#include <shlobj.h>
12+
#include <sddl.h>
1113

1214
#define SECURITY_WIN32 1 // For secext.h
1315
#include <secext.h>
@@ -148,9 +150,28 @@ static void getUserName(FFPlatform* platform)
148150
}
149151

150152
wchar_t buffer[256];
151-
DWORD len = ARRAY_SIZE(buffer);
152-
if (GetUserNameExW(NameDisplay, buffer, &len))
153+
DWORD size = ARRAY_SIZE(buffer);
154+
if (GetUserNameExW(NameDisplay, buffer, &size))
153155
ffStrbufSetWS(&platform->fullUserName, buffer);
156+
157+
size = 0;
158+
DWORD refDomainSize = 0;
159+
SID_NAME_USE sidNameUse = SidTypeUnknown;
160+
LookupAccountNameA(NULL, userName, NULL, &size, NULL, &refDomainSize, &sidNameUse);
161+
if (size > 0)
162+
{
163+
FF_AUTO_FREE PSID sid = (PSID) malloc(size);
164+
FF_AUTO_FREE char* refDomain = (char*) malloc(refDomainSize);
165+
if (LookupAccountNameA(NULL, userName, sid, &size, refDomain, &refDomainSize, &sidNameUse))
166+
{
167+
LPWSTR sidString;
168+
if (ConvertSidToStringSidW(sid, &sidString))
169+
{
170+
ffStrbufSetWS(&platform->sid, sidString);
171+
LocalFree(sidString);
172+
}
173+
}
174+
}
154175
}
155176

156177
static void getHostName(FFPlatform* platform)

src/detection/displayserver/linux/wmde.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ static void applyPrettyNameIfDE(FFDisplayServerResult* result, const char* name)
266266

267267
static const char* getFromProcesses(FFDisplayServerResult* result)
268268
{
269-
uint32_t userId = getuid();
269+
uint32_t userId = instance.state.platform.uid;
270270

271271
#if __FreeBSD__
272272
#ifdef __DragonFly__

src/detection/lm/lm_linux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ const char* ffDetectLM(FFLMResult* result)
142142
{
143143
// On some incorrectly configured systems, $XDG_SESSION_ID is not set. Try finding it ourself
144144
// WARNING: This is private data. Do not parse
145-
ffStrbufSetF(&path, FF_SYSTEMD_USERS_PATH "%d", getuid());
145+
ffStrbufSetF(&path, FF_SYSTEMD_USERS_PATH "%d", instance.state.platform.uid);
146146

147147
// This is actually buggy, and assumes current user is using DE
148148
// `sd_pid_get_session` can be a better option, but we need to find a pid to use

src/detection/users/users_linux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ const char* detectBySystemd(FFUsersOptions* options, FFlist* users)
100100

101101
if (options->myselfOnly)
102102
{
103-
ffStrbufAppendUInt(&pathUsers, getuid());
103+
ffStrbufAppendUInt(&pathUsers, instance.state.platform.uid);
104104
detectUserBySystemd(&pathUsers, users);
105105
}
106106
else

src/modules/title/title.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ void ffGenerateTitleJsonConfig(FFTitleOptions* options, yyjson_mut_doc* doc, yyj
121121
bool ffGenerateTitleJsonResult(FF_MAYBE_UNUSED FFTitleOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
122122
{
123123
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
124+
#ifdef _WIN32
125+
yyjson_mut_obj_add_strbuf(doc, obj, "userId", &instance.state.platform.sid);
126+
#else
127+
yyjson_mut_obj_add_uint(doc, obj, "userId", instance.state.platform.uid);
128+
#endif
124129
yyjson_mut_obj_add_strbuf(doc, obj, "userName", &instance.state.platform.userName);
125130
yyjson_mut_obj_add_strbuf(doc, obj, "fullUserName", &instance.state.platform.fullUserName);
126131
yyjson_mut_obj_add_strbuf(doc, obj, "hostName", &instance.state.platform.hostName);
@@ -169,5 +174,6 @@ FFModuleBaseInfo ffTitleModuleInfo = {
169174
{"@ symbol (colored)", "at-symbol-colored"},
170175
{"Host name (colored)", "host-name-colored"},
171176
{"Full user name", "full-user-name"},
177+
{"UID / SID", "user-id"},
172178
}))
173179
};

0 commit comments

Comments
 (0)