xref: /reactos/dll/win32/userenv/sid.c (revision d6eebaa4)
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
36 GetUserSid(HANDLE hToken)
37 {
38     PTOKEN_USER UserBuffer;
39     PTOKEN_USER TempBuffer;
40     NTSTATUS Status;
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 
51     Status = NtQueryInformationToken(hToken,
52                                      TokenUser,
53                                      (PVOID)UserBuffer,
54                                      Length,
55                                      &Length);
56     if (Status == STATUS_BUFFER_TOO_SMALL)
57     {
58         TempBuffer = LocalReAlloc(UserBuffer, Length, LMEM_MOVEABLE);
59         if (TempBuffer == NULL)
60         {
61             LocalFree(UserBuffer);
62             return NULL;
63         }
64 
65         UserBuffer = TempBuffer;
66         Status = NtQueryInformationToken(hToken,
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 
81     pSid = LocalAlloc(LPTR, Length);
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
102 GetUserSidStringFromToken(HANDLE hToken,
103                           PUNICODE_STRING SidString)
104 {
105     PTOKEN_USER UserBuffer, nsb;
106     ULONG Length;
107     NTSTATUS Status;
108 
109     Length = 256;
110     UserBuffer = LocalAlloc(LPTR, Length);
111     if (UserBuffer == NULL)
112         return FALSE;
113 
114     Status = NtQueryInformationToken(hToken,
115                                      TokenUser,
116                                      (PVOID)UserBuffer,
117                                      Length,
118                                      &Length);
119     if (Status == STATUS_BUFFER_TOO_SMALL)
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;
131         Status = NtQueryInformationToken(hToken,
132                                          TokenUser,
133                                          (PVOID)UserBuffer,
134                                          Length,
135                                          &Length);
136     }
137 
138     if (!NT_SUCCESS (Status))
139     {
140         LocalFree(UserBuffer);
141         SetLastError(RtlNtStatusToDosError(Status));
142         return FALSE;
143     }
144 
145     DPRINT("SidLength: %lu\n", RtlLengthSid (UserBuffer->User.Sid));
146 
147     Status = RtlConvertSidToUnicodeString(SidString,
148                                           UserBuffer->User.Sid,
149                                           TRUE);
150 
151     LocalFree(UserBuffer);
152 
153     if (!NT_SUCCESS(Status))
154     {
155         SetLastError(RtlNtStatusToDosError(Status));
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 }
165