Skip to content

Commit 60d343c

Browse files
committed
Platform (Windows): simplifies SID retrival
1 parent bdc844f commit 60d343c

2 files changed

Lines changed: 11 additions & 22 deletions

File tree

src/common/impl/FFPlatform_windows.c

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#include "FFPlatform_private.h"
22
#include "common/io.h"
33
#include "common/library.h"
4-
#include "common/mallocHelper.h"
54
#include "common/stringUtils.h"
65
#include "common/windows/unicode.h"
76
#include "common/windows/registry.h"
87
#include "common/windows/nt.h"
98

9+
#include <stdalign.h>
1010
#include <windows.h>
1111
#include <shlobj.h>
1212
#include <sddl.h>
@@ -145,30 +145,18 @@ static void getUserName(FFPlatform* platform)
145145

146146
size = ARRAY_SIZE(buffer);
147147
if (GetUserNameW(buffer, &size)) // GetUserNameExW(10002)?
148-
{
149148
ffStrbufSetWS(&platform->userName, buffer);
150-
151-
size = 0;
152-
DWORD refDomainSize = 0;
153-
SID_NAME_USE sidNameUse = SidTypeUnknown;
154-
LookupAccountNameW(NULL, buffer, NULL, &size, NULL, &refDomainSize, &sidNameUse);
155-
if (size > 0)
156-
{
157-
FF_AUTO_FREE PSID sid = (PSID) malloc(size);
158-
FF_AUTO_FREE LPWSTR refDomain = (LPWSTR) malloc(refDomainSize * sizeof(wchar_t));
159-
if (LookupAccountNameW(NULL, buffer, sid, &size, refDomain, &refDomainSize, &sidNameUse))
160-
{
161-
LPWSTR sidString;
162-
if (ConvertSidToStringSidW(sid, &sidString))
163-
{
164-
ffStrbufSetWS(&platform->sid, sidString);
165-
LocalFree(sidString);
166-
}
167-
}
168-
}
169-
}
170149
else
171150
ffStrbufSetS(&platform->userName, getenv("USERNAME"));
151+
152+
alignas(TOKEN_USER) char buf[SECURITY_MAX_SID_SIZE + sizeof(TOKEN_USER)];
153+
if (NT_SUCCESS(NtQueryInformationToken(NtCurrentProcessToken(), TokenUser, buf, sizeof(buf), &size)))
154+
{
155+
TOKEN_USER* tokenUser = (TOKEN_USER*) buf;
156+
UNICODE_STRING sidString = { .Buffer = buffer, .Length = 0, .MaximumLength = sizeof(buffer) };
157+
if (NT_SUCCESS(RtlConvertSidToUnicodeString(&sidString, tokenUser->User.Sid, FALSE)))
158+
ffStrbufSetNWS(&platform->sid, sidString.Length / sizeof(wchar_t), sidString.Buffer);
159+
}
172160
}
173161

174162
static void getHostName(FFPlatform* platform)

src/common/windows/nt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,3 +892,4 @@ NTSYSAPI NTSTATUS NTAPI NtQueryInformationToken(
892892
_In_ ULONG TokenInformationLength,
893893
_Out_ PULONG ReturnLength
894894
);
895+
#define NtCurrentProcessToken() ((HANDLE)(LONG_PTR)-4) // for NtQueryInformationToken only; Windows 8+

0 commit comments

Comments
 (0)