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