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