ReactOS  0.4.14-dev-98-gb0d4763
sid.c File Reference
#include "precomp.h"
#include <ndk/sefuncs.h>
#include <debug.h>
Include dependency graph for sid.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

PSID WINAPI GetUserSid (HANDLE hToken)
 
BOOL GetUserSidStringFromToken (HANDLE hToken, PUNICODE_STRING SidString)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 31 of file sid.c.

Function Documentation

◆ GetUserSid()

PSID WINAPI GetUserSid ( HANDLE  hToken)

Definition at line 36 of file sid.c.

37 {
38  PTOKEN_USER UserBuffer;
39  PTOKEN_USER TempBuffer;
41  ULONG Length;
42  PSID pSid;
43 
44  Length = 256;
45  UserBuffer = LocalAlloc(LPTR, Length);
46  if (UserBuffer == NULL)
47  {
48  return NULL;
49  }
50 
52  TokenUser,
53  (PVOID)UserBuffer,
54  Length,
55  &Length);
57  {
58  TempBuffer = LocalReAlloc(UserBuffer, Length, LMEM_MOVEABLE);
59  if (TempBuffer == NULL)
60  {
61  LocalFree(UserBuffer);
62  return NULL;
63  }
64 
65  UserBuffer = TempBuffer;
67  TokenUser,
68  (PVOID)UserBuffer,
69  Length,
70  &Length);
71  }
72 
73  if (!NT_SUCCESS(Status))
74  {
75  LocalFree(UserBuffer);
76  return NULL;
77  }
78 
79  Length = RtlLengthSid(UserBuffer->User.Sid);
80 
82  if (pSid == NULL)
83  {
84  LocalFree(UserBuffer);
85  return NULL;
86  }
87 
88  Status = RtlCopySid(Length, pSid, UserBuffer->User.Sid);
89 
90  LocalFree(UserBuffer);
91 
92  if (!NT_SUCCESS(Status))
93  {
94  LocalFree(pSid);
95  return NULL;
96  }
97 
98  return pSid;
99 }
NTSTATUS NTAPI RtlCopySid(IN ULONG BufferLength, IN PSID Dest, IN PSID Src)
Definition: sid.c:165
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:64
smooth NULL
Definition: ftsmooth.c:416
_Must_inspect_result_ __kernel_entry NTSTATUS NTAPI NtQueryInformationToken(_In_ HANDLE TokenHandle, _In_ TOKEN_INFORMATION_CLASS TokenInformationClass, _Out_writes_bytes_to_opt_(TokenInformationLength, *ReturnLength) PVOID TokenInformation, _In_ ULONG TokenInformationLength, _Out_ PULONG ReturnLength)
Definition: token.c:1839
ULONG NTAPI RtlLengthSid(IN PSID Sid_)
Definition: sid.c:150
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define LMEM_MOVEABLE
Definition: winbase.h:350
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
Status
Definition: gdiplustypes.h:24
HLOCAL NTAPI LocalReAlloc(HLOCAL hMem, SIZE_T dwBytes, UINT uFlags)
Definition: heapmem.c:1608
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
static PSID pSid
Definition: security.c:74
unsigned int ULONG
Definition: retypes.h:1
#define LPTR
Definition: winbase.h:362
SID_AND_ATTRIBUTES User
Definition: setypes.h:956
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373

Referenced by CopySystemProfile(), and GetUserAndDomainName().

◆ GetUserSidStringFromToken()

BOOL GetUserSidStringFromToken ( HANDLE  hToken,
PUNICODE_STRING  SidString 
)

Definition at line 102 of file sid.c.

104 {
105  PTOKEN_USER UserBuffer, nsb;
106  ULONG Length;
108 
109  Length = 256;
110  UserBuffer = LocalAlloc(LPTR, Length);
111  if (UserBuffer == NULL)
112  return FALSE;
113 
115  TokenUser,
116  (PVOID)UserBuffer,
117  Length,
118  &Length);
120  {
121  nsb = LocalReAlloc(UserBuffer,
122  Length,
123  LMEM_MOVEABLE);
124  if (nsb == NULL)
125  {
126  LocalFree(UserBuffer);
127  return FALSE;
128  }
129 
130  UserBuffer = nsb;
132  TokenUser,
133  (PVOID)UserBuffer,
134  Length,
135  &Length);
136  }
137 
138  if (!NT_SUCCESS (Status))
139  {
140  LocalFree(UserBuffer);
142  return FALSE;
143  }
144 
145  DPRINT("SidLength: %lu\n", RtlLengthSid (UserBuffer->User.Sid));
146 
148  UserBuffer->User.Sid,
149  TRUE);
150 
151  LocalFree(UserBuffer);
152 
153  if (!NT_SUCCESS(Status))
154  {
156  return FALSE;
157  }
158 
159  DPRINT("SidString.Length: %lu\n", SidString->Length);
160  DPRINT("SidString.MaximumLength: %lu\n", SidString->MaximumLength);
161  DPRINT("SidString: '%wZ'\n", SidString);
162 
163  return TRUE;
164 }
#define TRUE
Definition: types.h:120
USHORT MaximumLength
Definition: env_spec_w32.h:370
LONG NTSTATUS
Definition: precomp.h:26
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:64
NTSTATUS NTAPI RtlConvertSidToUnicodeString(IN PUNICODE_STRING String, IN PSID Sid_, IN BOOLEAN AllocateBuffer)
Definition: sid.c:342
smooth NULL
Definition: ftsmooth.c:416
_Must_inspect_result_ __kernel_entry NTSTATUS NTAPI NtQueryInformationToken(_In_ HANDLE TokenHandle, _In_ TOKEN_INFORMATION_CLASS TokenInformationClass, _Out_writes_bytes_to_opt_(TokenInformationLength, *ReturnLength) PVOID TokenInformation, _In_ ULONG TokenInformationLength, _Out_ PULONG ReturnLength)
Definition: token.c:1839
ULONG NTAPI RtlLengthSid(IN PSID Sid_)
Definition: sid.c:150
void DPRINT(...)
Definition: polytest.cpp:61
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define LMEM_MOVEABLE
Definition: winbase.h:350
#define SetLastError(x)
Definition: compat.h:409
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
Status
Definition: gdiplustypes.h:24
NTSYSAPI ULONG WINAPI RtlNtStatusToDosError(NTSTATUS)
HLOCAL NTAPI LocalReAlloc(HLOCAL hMem, SIZE_T dwBytes, UINT uFlags)
Definition: heapmem.c:1608
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
unsigned int ULONG
Definition: retypes.h:1
#define LPTR
Definition: winbase.h:362
SID_AND_ATTRIBUTES User
Definition: setypes.h:956
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373

Referenced by CheckForLoadedProfile(), CopySystemProfile(), GetCurrentUserKey(), GetProfileType(), GetUserProfileDirectoryW(), LoadUserProfileW(), and UnloadUserProfile().