ReactOS  0.4.13-dev-241-g63286c6
sid.c
Go to the documentation of this file.
1 /*
2  * ReactOS kernel
3  * Copyright (C) 2004 ReactOS Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 /*
20  * COPYRIGHT: See COPYING in the top level directory
21  * PROJECT: ReactOS system libraries
22  * FILE: dll/win32/userenv/sid.c
23  * PURPOSE: User profile code
24  * PROGRAMMER: Eric Kohl
25  */
26 
27 #include "precomp.h"
28 
29 #include <ndk/sefuncs.h>
30 
31 #define NDEBUG
32 #include <debug.h>
33 
34 PSID
35 WINAPI
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 }
100 
101 BOOL
103  PUNICODE_STRING SidString)
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 }
BOOL GetUserSidStringFromToken(HANDLE hToken, PUNICODE_STRING SidString)
Definition: sid.c:102
#define TRUE
Definition: types.h:120
NTSTATUS NTAPI RtlCopySid(IN ULONG BufferLength, IN PSID Dest, IN PSID Src)
Definition: sid.c:165
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
unsigned int BOOL
Definition: ntddk_ex.h:94
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 WINAPI
Definition: msvc.h:8
#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
static PSID pSid
Definition: security.c:74
PSID WINAPI GetUserSid(HANDLE hToken)
Definition: sid.c:36
unsigned int ULONG
Definition: retypes.h:1
#define LPTR
Definition: winbase.h:362
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1373