xref: /reactos/dll/win32/msgina/lsa.c (revision 5100859e)
1 /*
2  * PROJECT:         ReactOS msgina.dll
3  * FILE:            dll/win32/msgina/gui.c
4  * PURPOSE:         ReactOS Logon GINA DLL
5  * PROGRAMMER:      Eric Kohl
6  */
7 
8 #include "msgina.h"
9 
10 NTSTATUS
11 ConnectToLsa(
12     PGINA_CONTEXT pgContext)
13 {
14     LSA_STRING LogonProcessName;
15     LSA_STRING PackageName;
16     LSA_OPERATIONAL_MODE SecurityMode = 0;
17     NTSTATUS Status;
18 
19     /* We are already connected to the LSA */
20     if (pgContext->LsaHandle != NULL)
21         return STATUS_SUCCESS;
22 
23     /* Connect to the LSA server */
24     RtlInitAnsiString((PANSI_STRING)&LogonProcessName,
25                       "MSGINA");
26 
27     Status = LsaRegisterLogonProcess(&LogonProcessName,
28                                      &pgContext->LsaHandle,
29                                      &SecurityMode);
30     if (!NT_SUCCESS(Status))
31     {
32         ERR("LsaRegisterLogonProcess failed (Status 0x%08lx)\n", Status);
33         return Status;
34     }
35 
36     /* Get the authentication package */
37     RtlInitAnsiString((PANSI_STRING)&PackageName,
38                       MSV1_0_PACKAGE_NAME);
39 
40     Status = LsaLookupAuthenticationPackage(pgContext->LsaHandle,
41                                             &PackageName,
42                                             &pgContext->AuthenticationPackage);
43     if (!NT_SUCCESS(Status))
44     {
45         ERR("LsaLookupAuthenticationPackage failed (Status 0x%08lx)\n", Status);
46     }
47 
48     return Status;
49 }
50 
51 
52 NTSTATUS
53 MyLogonUser(
54     HANDLE LsaHandle,
55     ULONG AuthenticationPackage,
56     LPWSTR lpszUsername,
57     LPWSTR lpszDomain,
58     LPWSTR lpszPassword,
59     PHANDLE phToken,
60     PNTSTATUS SubStatus)
61 {
62     SID_IDENTIFIER_AUTHORITY LocalAuthority = {SECURITY_LOCAL_SID_AUTHORITY};
63     SID_IDENTIFIER_AUTHORITY SystemAuthority = {SECURITY_NT_AUTHORITY};
64     PSID LogonSid = NULL;
65     PSID LocalSid = NULL;
66     LSA_STRING OriginName;
67     UNICODE_STRING DomainName;
68     UNICODE_STRING UserName;
69     UNICODE_STRING Password;
70     PMSV1_0_INTERACTIVE_LOGON AuthInfo = NULL;
71     ULONG AuthInfoLength;
72     ULONG_PTR Ptr;
73     TOKEN_SOURCE TokenSource;
74     PTOKEN_GROUPS TokenGroups = NULL;
75     PMSV1_0_INTERACTIVE_PROFILE ProfileBuffer = NULL;
76     ULONG ProfileBufferLength = 0;
77     LUID Luid = {0, 0};
78     LUID LogonId = {0, 0};
79     HANDLE TokenHandle = NULL;
80     QUOTA_LIMITS QuotaLimits;
81     NTSTATUS Status;
82 
83     *phToken = NULL;
84 
85     RtlInitAnsiString((PANSI_STRING)&OriginName,
86                       "MSGINA Logon");
87 
88     RtlInitUnicodeString(&DomainName,
89                          lpszDomain);
90 
91     RtlInitUnicodeString(&UserName,
92                          lpszUsername);
93 
94     RtlInitUnicodeString(&Password,
95                          lpszPassword);
96 
97     AuthInfoLength = sizeof(MSV1_0_INTERACTIVE_LOGON)+
98                      DomainName.MaximumLength +
99                      UserName.MaximumLength +
100                      Password.MaximumLength;
101 
102     AuthInfo = RtlAllocateHeap(RtlGetProcessHeap(),
103                                HEAP_ZERO_MEMORY,
104                                AuthInfoLength);
105     if (AuthInfo == NULL)
106     {
107         Status = STATUS_INSUFFICIENT_RESOURCES;
108         goto done;
109     }
110 
111     AuthInfo->MessageType = MsV1_0InteractiveLogon;
112 
113     Ptr = (ULONG_PTR)AuthInfo + sizeof(MSV1_0_INTERACTIVE_LOGON);
114 
115     AuthInfo->LogonDomainName.Length = DomainName.Length;
116     AuthInfo->LogonDomainName.MaximumLength = DomainName.MaximumLength;
117     AuthInfo->LogonDomainName.Buffer = (DomainName.Buffer == NULL) ? NULL : (PWCHAR)Ptr;
118     if (DomainName.MaximumLength > 0)
119     {
120         RtlCopyMemory(AuthInfo->LogonDomainName.Buffer,
121                       DomainName.Buffer,
122                       DomainName.MaximumLength);
123 
124         Ptr += DomainName.MaximumLength;
125     }
126 
127     AuthInfo->UserName.Length = UserName.Length;
128     AuthInfo->UserName.MaximumLength = UserName.MaximumLength;
129     AuthInfo->UserName.Buffer = (PWCHAR)Ptr;
130     if (UserName.MaximumLength > 0)
131         RtlCopyMemory(AuthInfo->UserName.Buffer,
132                       UserName.Buffer,
133                       UserName.MaximumLength);
134 
135     Ptr += UserName.MaximumLength;
136 
137     AuthInfo->Password.Length = Password.Length;
138     AuthInfo->Password.MaximumLength = Password.MaximumLength;
139     AuthInfo->Password.Buffer = (PWCHAR)Ptr;
140     if (Password.MaximumLength > 0)
141         RtlCopyMemory(AuthInfo->Password.Buffer,
142                       Password.Buffer,
143                       Password.MaximumLength);
144 
145     /* Create the Logon SID*/
146     AllocateLocallyUniqueId(&LogonId);
147     Status = RtlAllocateAndInitializeSid(&SystemAuthority,
148                                          SECURITY_LOGON_IDS_RID_COUNT,
149                                          SECURITY_LOGON_IDS_RID,
150                                          LogonId.HighPart,
151                                          LogonId.LowPart,
152                                          SECURITY_NULL_RID,
153                                          SECURITY_NULL_RID,
154                                          SECURITY_NULL_RID,
155                                          SECURITY_NULL_RID,
156                                          SECURITY_NULL_RID,
157                                          &LogonSid);
158     if (!NT_SUCCESS(Status))
159         goto done;
160 
161     /* Create the Local SID*/
162     Status = RtlAllocateAndInitializeSid(&LocalAuthority,
163                                          1,
164                                          SECURITY_LOCAL_RID,
165                                          SECURITY_NULL_RID,
166                                          SECURITY_NULL_RID,
167                                          SECURITY_NULL_RID,
168                                          SECURITY_NULL_RID,
169                                          SECURITY_NULL_RID,
170                                          SECURITY_NULL_RID,
171                                          SECURITY_NULL_RID,
172                                          &LocalSid);
173     if (!NT_SUCCESS(Status))
174         goto done;
175 
176     /* Allocate and set the token groups */
177     TokenGroups = RtlAllocateHeap(RtlGetProcessHeap(),
178                                   HEAP_ZERO_MEMORY,
179                                   sizeof(TOKEN_GROUPS) + ((2 - ANYSIZE_ARRAY) * sizeof(SID_AND_ATTRIBUTES)));
180     if (TokenGroups == NULL)
181     {
182         Status = STATUS_INSUFFICIENT_RESOURCES;
183         goto done;
184     }
185 
186     TokenGroups->GroupCount = 2;
187     TokenGroups->Groups[0].Sid = LogonSid;
188     TokenGroups->Groups[0].Attributes = SE_GROUP_MANDATORY | SE_GROUP_ENABLED |
189                                         SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_LOGON_ID;
190     TokenGroups->Groups[1].Sid = LocalSid;
191     TokenGroups->Groups[1].Attributes = SE_GROUP_MANDATORY | SE_GROUP_ENABLED |
192                                         SE_GROUP_ENABLED_BY_DEFAULT;
193 
194     /* Set the token source */
195     strncpy(TokenSource.SourceName, "User32  ", sizeof(TokenSource.SourceName));
196     AllocateLocallyUniqueId(&TokenSource.SourceIdentifier);
197 
198     Status = LsaLogonUser(LsaHandle,
199                           &OriginName,
200                           Interactive,
201                           AuthenticationPackage,
202                           (PVOID)AuthInfo,
203                           AuthInfoLength,
204                           TokenGroups,
205                           &TokenSource,
206                           (PVOID*)&ProfileBuffer,
207                           &ProfileBufferLength,
208                           &Luid,
209                           &TokenHandle,
210                           &QuotaLimits,
211                           SubStatus);
212     if (!NT_SUCCESS(Status))
213     {
214         ERR("LsaLogonUser failed (Status 0x%08lx)\n", Status);
215         goto done;
216     }
217 
218     if (ProfileBuffer != NULL)
219     {
220         TRACE("ProfileBuffer: %p\n", ProfileBuffer);
221         TRACE("MessageType: %u\n", ProfileBuffer->MessageType);
222 
223         TRACE("FullName: %p\n", ProfileBuffer->FullName.Buffer);
224         TRACE("FullName: %S\n", ProfileBuffer->FullName.Buffer);
225 
226         TRACE("LogonServer: %p\n", ProfileBuffer->LogonServer.Buffer);
227         TRACE("LogonServer: %S\n", ProfileBuffer->LogonServer.Buffer);
228     }
229 
230     TRACE("Luid: 0x%08lx%08lx\n", Luid.HighPart, Luid.LowPart);
231 
232     if (TokenHandle != NULL)
233     {
234         TRACE("TokenHandle: %p\n", TokenHandle);
235     }
236 
237     *phToken = TokenHandle;
238 
239 done:
240     if (ProfileBuffer != NULL)
241         LsaFreeReturnBuffer(ProfileBuffer);
242 
243     if (!NT_SUCCESS(Status))
244     {
245         if (TokenHandle != NULL)
246             CloseHandle(TokenHandle);
247     }
248 
249     if (TokenGroups != NULL)
250         RtlFreeHeap(RtlGetProcessHeap(), 0, TokenGroups);
251 
252     if (LocalSid != NULL)
253         RtlFreeSid(LocalSid);
254 
255     if (LogonSid != NULL)
256         RtlFreeSid(LogonSid);
257 
258     if (AuthInfo != NULL)
259         RtlFreeHeap(RtlGetProcessHeap(), 0, AuthInfo);
260 
261     return Status;
262 }
263 
264 /* EOF */
265