Revert 45697:
[reactos.git] / lib / lsalib / lsa.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS system libraries
4 * FILE: lib/lsalib/lsa.c
5 * PURPOSE: Client-side LSA functions
6 * UPDATE HISTORY:
7 * Created 05/08/00
8 */
9
10 /* INCLUDES ******************************************************************/
11
12 #include <ndk/ntndk.h>
13 #include <psdk/ntsecapi.h>
14 #include <lsass/lsass.h>
15
16 #define NDEBUG
17 #include <debug.h>
18
19 /* GLOBALS *******************************************************************/
20
21 extern HANDLE Secur32Heap;
22
23 /* FUNCTIONS *****************************************************************/
24
25 /*
26 * @implemented
27 */
28 NTSTATUS WINAPI
29 LsaDeregisterLogonProcess(HANDLE LsaHandle)
30 {
31 LSASS_REQUEST Request;
32 LSASS_REPLY Reply;
33 NTSTATUS Status;
34
35 Request.Header.u1.s1.DataLength = 0;
36 Request.Header.u1.s1.TotalLength = sizeof(LSASS_REQUEST);
37 Request.Type = LSASS_REQUEST_DEREGISTER_LOGON_PROCESS;
38 Status = ZwRequestWaitReplyPort(LsaHandle,
39 &Request.Header,
40 &Reply.Header);
41 if (!NT_SUCCESS(Status))
42 {
43 return Status;
44 }
45
46 if (!NT_SUCCESS(Reply.Status))
47 {
48 return Reply.Status;
49 }
50
51 return Status;
52 }
53
54
55 /*
56 * @unimplemented
57 */
58 NTSTATUS WINAPI
59 LsaConnectUntrusted(PHANDLE LsaHandle)
60 {
61 UNIMPLEMENTED;
62 return STATUS_NOT_IMPLEMENTED;
63 }
64
65
66 /*
67 * @implemented
68 */
69 NTSTATUS WINAPI
70 LsaCallAuthenticationPackage(HANDLE LsaHandle,
71 ULONG AuthenticationPackage,
72 PVOID ProtocolSubmitBuffer,
73 ULONG SubmitBufferLength,
74 PVOID *ProtocolReturnBuffer,
75 PULONG ReturnBufferLength,
76 PNTSTATUS ProtocolStatus)
77 {
78 PLSASS_REQUEST Request;
79 PLSASS_REPLY Reply;
80 LSASS_REQUEST RawRequest;
81 LSASS_REPLY RawReply;
82 NTSTATUS Status;
83 ULONG OutBufferSize;
84
85 Request = (PLSASS_REQUEST)&RawRequest;
86 Reply = (PLSASS_REPLY)&RawReply;
87
88 Request->Header.u1.s1.DataLength = sizeof(LSASS_REQUEST) + SubmitBufferLength -
89 sizeof(PORT_MESSAGE);
90 Request->Header.u1.s1.TotalLength =
91 Request->Header.u1.s1.DataLength + sizeof(PORT_MESSAGE);
92 Request->Type = LSASS_REQUEST_CALL_AUTHENTICATION_PACKAGE;
93 Request->d.CallAuthenticationPackageRequest.AuthenticationPackage =
94 AuthenticationPackage;
95 Request->d.CallAuthenticationPackageRequest.InBufferLength =
96 SubmitBufferLength;
97 memcpy(Request->d.CallAuthenticationPackageRequest.InBuffer,
98 ProtocolSubmitBuffer,
99 SubmitBufferLength);
100
101 Status = ZwRequestWaitReplyPort(LsaHandle,
102 &Request->Header,
103 &Reply->Header);
104 if (!NT_SUCCESS(Status))
105 {
106 return Status;
107 }
108
109 if (!NT_SUCCESS(Reply->Status))
110 {
111 return Reply->Status;
112 }
113
114 OutBufferSize = Reply->d.CallAuthenticationPackageReply.OutBufferLength;
115 *ProtocolReturnBuffer = RtlAllocateHeap(Secur32Heap,
116 0,
117 OutBufferSize);
118 *ReturnBufferLength = OutBufferSize;
119 memcpy(*ProtocolReturnBuffer,
120 Reply->d.CallAuthenticationPackageReply.OutBuffer,
121 *ReturnBufferLength);
122
123 return Status;
124 }
125
126
127 /*
128 * @implemented
129 */
130 NTSTATUS WINAPI
131 LsaFreeReturnBuffer(PVOID Buffer)
132 {
133 return RtlFreeHeap(Secur32Heap, 0, Buffer);
134 }
135
136
137 /*
138 * @implemented
139 */
140 NTSTATUS WINAPI
141 LsaLookupAuthenticationPackage(HANDLE LsaHandle,
142 PLSA_STRING PackageName,
143 PULONG AuthenticationPackage)
144 {
145 NTSTATUS Status;
146 PLSASS_REQUEST Request;
147 LSASS_REQUEST RawRequest;
148 LSASS_REPLY Reply;
149
150 Request = (PLSASS_REQUEST)&RawRequest;
151 Request->Header.u1.s1.DataLength = sizeof(LSASS_REQUEST) + PackageName->Length -
152 sizeof(PORT_MESSAGE);
153 Request->Header.u1.s1.TotalLength = Request->Header.u1.s1.DataLength +
154 sizeof(PORT_MESSAGE);
155 Request->Type = LSASS_REQUEST_LOOKUP_AUTHENTICATION_PACKAGE;
156
157 Status = ZwRequestWaitReplyPort(LsaHandle,
158 &Request->Header,
159 &Reply.Header);
160 if (!NT_SUCCESS(Status))
161 {
162 return Status;
163 }
164
165 if (!NT_SUCCESS(Reply.Status))
166 {
167 return Reply.Status;
168 }
169
170 *AuthenticationPackage = Reply.d.LookupAuthenticationPackageReply.Package;
171
172 return Reply.Status;
173 }
174
175
176 /*
177 * @implemented
178 */
179 NTSTATUS WINAPI
180 LsaLogonUser(HANDLE LsaHandle,
181 PLSA_STRING OriginName,
182 SECURITY_LOGON_TYPE LogonType,
183 ULONG AuthenticationPackage,
184 PVOID AuthenticationInformation,
185 ULONG AuthenticationInformationLength,
186 PTOKEN_GROUPS LocalGroups,
187 PTOKEN_SOURCE SourceContext,
188 PVOID *ProfileBuffer,
189 PULONG ProfileBufferLength,
190 PLUID LogonId,
191 PHANDLE Token,
192 PQUOTA_LIMITS Quotas,
193 PNTSTATUS SubStatus)
194 {
195 ULONG RequestLength;
196 ULONG CurrentLength;
197 PLSASS_REQUEST Request;
198 LSASS_REQUEST RawMessage;
199 PLSASS_REPLY Reply;
200 LSASS_REPLY RawReply;
201 NTSTATUS Status;
202
203 RequestLength = sizeof(LSASS_REQUEST) - sizeof(PORT_MESSAGE);
204 RequestLength = RequestLength + (OriginName->Length * sizeof(WCHAR));
205 RequestLength = RequestLength + AuthenticationInformationLength;
206 RequestLength = RequestLength +
207 (LocalGroups->GroupCount * sizeof(SID_AND_ATTRIBUTES));
208
209 CurrentLength = 0;
210 Request = (PLSASS_REQUEST)&RawMessage;
211
212 Request->d.LogonUserRequest.OriginNameLength = OriginName->Length;
213 Request->d.LogonUserRequest.OriginName = (PWSTR)&RawMessage + CurrentLength;
214 memcpy((PWSTR)&RawMessage + CurrentLength,
215 OriginName->Buffer,
216 OriginName->Length * sizeof(WCHAR));
217 CurrentLength = CurrentLength + (OriginName->Length * sizeof(WCHAR));
218
219 Request->d.LogonUserRequest.LogonType = LogonType;
220
221 Request->d.LogonUserRequest.AuthenticationPackage =
222 AuthenticationPackage;
223
224 Request->d.LogonUserRequest.AuthenticationInformation =
225 (PVOID)((ULONG_PTR)&RawMessage + CurrentLength);
226 Request->d.LogonUserRequest.AuthenticationInformationLength =
227 AuthenticationInformationLength;
228 memcpy((PVOID)((ULONG_PTR)&RawMessage + CurrentLength),
229 AuthenticationInformation,
230 AuthenticationInformationLength);
231 CurrentLength = CurrentLength + AuthenticationInformationLength;
232
233 Request->d.LogonUserRequest.LocalGroupsCount = LocalGroups->GroupCount;
234 Request->d.LogonUserRequest.LocalGroups =
235 (PSID_AND_ATTRIBUTES)&RawMessage + CurrentLength;
236 memcpy((PSID_AND_ATTRIBUTES)&RawMessage + CurrentLength,
237 LocalGroups->Groups,
238 LocalGroups->GroupCount * sizeof(SID_AND_ATTRIBUTES));
239
240 Request->d.LogonUserRequest.SourceContext = *SourceContext;
241
242 Request->Type = LSASS_REQUEST_LOGON_USER;
243 Request->Header.u1.s1.DataLength = RequestLength - sizeof(PORT_MESSAGE);
244 Request->Header.u1.s1.TotalLength = RequestLength + sizeof(PORT_MESSAGE);
245
246 Reply = (PLSASS_REPLY)&RawReply;
247
248 Status = ZwRequestWaitReplyPort(LsaHandle,
249 &Request->Header,
250 &Reply->Header);
251 if (!NT_SUCCESS(Status))
252 {
253 return Status;
254 }
255
256 *SubStatus = Reply->d.LogonUserReply.SubStatus;
257
258 if (!NT_SUCCESS(Reply->Status))
259 {
260 return Status;
261 }
262
263 *ProfileBuffer = RtlAllocateHeap(Secur32Heap,
264 0,
265 Reply->d.LogonUserReply.ProfileBufferLength);
266 memcpy(*ProfileBuffer,
267 (PVOID)((ULONG)Reply->d.LogonUserReply.Data +
268 (ULONG)Reply->d.LogonUserReply.ProfileBuffer),
269 Reply->d.LogonUserReply.ProfileBufferLength);
270 *LogonId = Reply->d.LogonUserReply.LogonId;
271 *Token = Reply->d.LogonUserReply.Token;
272 memcpy(Quotas,
273 &Reply->d.LogonUserReply.Quotas,
274 sizeof(Reply->d.LogonUserReply.Quotas));
275
276 return Status;
277 }
278
279
280 /*
281 * @implemented
282 */
283 NTSTATUS WINAPI
284 LsaRegisterLogonProcess(PLSA_STRING LsaLogonProcessName,
285 PHANDLE Handle,
286 PLSA_OPERATIONAL_MODE OperationalMode)
287 {
288 UNICODE_STRING PortName; // = RTL_CONSTANT_STRING(L"\\LsaAuthenticationPort");
289 SECURITY_QUALITY_OF_SERVICE SecurityQos;
290 ULONG ConnectInfoLength;
291 NTSTATUS Status;
292 LSASS_CONNECT_DATA ConnectInfo;
293 // LSASS_REQUEST Request;
294 // LSASS_REPLY Reply;
295
296 /* Check the logon process name length */
297 if (LsaLogonProcessName->Length > LSASS_MAX_LOGON_PROCESS_NAME_LENGTH)
298 return STATUS_NAME_TOO_LONG;
299
300 RtlInitUnicodeString(&PortName,
301 L"\\LsaAuthenticationPort");
302
303 SecurityQos.Length = sizeof (SecurityQos);
304 SecurityQos.ImpersonationLevel = SecurityIdentification;
305 SecurityQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
306 SecurityQos.EffectiveOnly = TRUE;
307
308 ConnectInfoLength = sizeof(LSASS_CONNECT_DATA);
309
310 strncpy(ConnectInfo.LogonProcessNameBuffer,
311 LsaLogonProcessName->Buffer,
312 LsaLogonProcessName->Length);
313 ConnectInfo.Length = LsaLogonProcessName->Length;
314 ConnectInfo.LogonProcessNameBuffer[ConnectInfo.Length] = '\0';
315
316 Status = ZwConnectPort(Handle,
317 &PortName,
318 &SecurityQos,
319 NULL,
320 NULL,
321 NULL,
322 &ConnectInfo,
323 &ConnectInfoLength);
324 if (!NT_SUCCESS(Status))
325 {
326 return Status;
327 }
328
329 return Status;
330 #if 0
331 Request.Type = LSASS_REQUEST_REGISTER_LOGON_PROCESS;
332 Request.Header.u1.s1.DataLength = sizeof(LSASS_REQUEST) -
333 sizeof(PORT_MESSAGE);
334 Request.Header.u1.s1.TotalLength = sizeof(LSASS_REQUEST);
335
336 Request.d.RegisterLogonProcessRequest.Length = LsaLogonProcessName->Length;
337 memcpy(Request.d.RegisterLogonProcessRequest.LogonProcessNameBuffer,
338 LsaLogonProcessName->Buffer,
339 Request.d.RegisterLogonProcessRequest.Length);
340
341 Status = ZwRequestWaitReplyPort(*Handle,
342 &Request.Header,
343 &Reply.Header);
344 if (!NT_SUCCESS(Status))
345 {
346 // NtClose(*Handle);
347 // *Handle = NULL;
348 return Status;
349 }
350
351 if (!NT_SUCCESS(Reply.Status))
352 {
353 // NtClose(*Handle);
354 // *Handle = NULL;
355 return Status;
356 }
357
358 *OperationalMode = Reply.d.RegisterLogonProcessReply.OperationalMode;
359
360 return Reply.Status;
361 #endif
362 }
363
364
365 /*
366 * @unimplemented
367 */
368 NTSTATUS
369 WINAPI
370 LsaEnumerateLogonSessions(PULONG LogonSessionCount,
371 PLUID *LogonSessionList)
372 {
373 UNIMPLEMENTED;
374 return STATUS_NOT_IMPLEMENTED;
375 }
376
377
378 /*
379 * @unimplemented
380 */
381 NTSTATUS
382 WINAPI
383 LsaGetLogonSessionData(PLUID LogonId,
384 PSECURITY_LOGON_SESSION_DATA *ppLogonSessionData)
385 {
386 UNIMPLEMENTED;
387 return STATUS_NOT_IMPLEMENTED;
388 }
389
390
391 /*
392 * @unimplemented
393 */
394 NTSTATUS
395 WINAPI
396 LsaRegisterPolicyChangeNotification(POLICY_NOTIFICATION_INFORMATION_CLASS InformationClass,
397 HANDLE NotificationEventHandle)
398 {
399 UNIMPLEMENTED;
400 return STATUS_NOT_IMPLEMENTED;
401 }
402
403
404 /*
405 * @unimplemented
406 */
407 NTSTATUS
408 WINAPI
409 LsaUnregisterPolicyChangeNotification(POLICY_NOTIFICATION_INFORMATION_CLASS InformationClass,
410 HANDLE NotificationEventHandle)
411 {
412 UNIMPLEMENTED;
413 return STATUS_NOT_IMPLEMENTED;
414 }