xref: /reactos/dll/win32/lsasrv/srm.c (revision c2c66aff)
1*c2c66affSColin Finck /*
2*c2c66affSColin Finck  * COPYRIGHT:   See COPYING in the top level directory
3*c2c66affSColin Finck  * PROJECT:     Local Security Authority Server DLL
4*c2c66affSColin Finck  * FILE:        dll/win32/lsasrv/srm.c
5*c2c66affSColin Finck  * PURPOSE:     Security Reference Monitor Server
6*c2c66affSColin Finck  *
7*c2c66affSColin Finck  * PROGRAMMERS: Timo Kreuzer (timo.kreuzer@reactos.org)
8*c2c66affSColin Finck  */
9*c2c66affSColin Finck 
10*c2c66affSColin Finck /* INCLUDES ****************************************************************/
11*c2c66affSColin Finck 
12*c2c66affSColin Finck #include "lsasrv.h"
13*c2c66affSColin Finck #include <ndk/ntndk.h>
14*c2c66affSColin Finck 
15*c2c66affSColin Finck /* GLOBALS *****************************************************************/
16*c2c66affSColin Finck 
17*c2c66affSColin Finck HANDLE SeLsaCommandPort;
18*c2c66affSColin Finck HANDLE SeRmCommandPort;
19*c2c66affSColin Finck 
20*c2c66affSColin Finck /* FUNCTIONS ***************************************************************/
21*c2c66affSColin Finck 
22*c2c66affSColin Finck static
23*c2c66affSColin Finck VOID
24*c2c66affSColin Finck LsapComponentTest(
25*c2c66affSColin Finck     PLSAP_RM_API_MESSAGE Message)
26*c2c66affSColin Finck {
27*c2c66affSColin Finck     ERR("Security: LSA Component Test Command Received\n");
28*c2c66affSColin Finck }
29*c2c66affSColin Finck 
30*c2c66affSColin Finck static
31*c2c66affSColin Finck VOID
32*c2c66affSColin Finck LsapAdtWriteLog(
33*c2c66affSColin Finck     PLSAP_RM_API_MESSAGE Message)
34*c2c66affSColin Finck {
35*c2c66affSColin Finck     ERR("LsapAdtWriteLog\n");
36*c2c66affSColin Finck }
37*c2c66affSColin Finck 
38*c2c66affSColin Finck static
39*c2c66affSColin Finck VOID
40*c2c66affSColin Finck LsapAsync(
41*c2c66affSColin Finck     PLSAP_RM_API_MESSAGE Message)
42*c2c66affSColin Finck {
43*c2c66affSColin Finck     ERR("LsapAsync\n");
44*c2c66affSColin Finck }
45*c2c66affSColin Finck 
46*c2c66affSColin Finck static
47*c2c66affSColin Finck DWORD
48*c2c66affSColin Finck WINAPI
49*c2c66affSColin Finck LsapRmServerThread(
50*c2c66affSColin Finck     PVOID StartContext)
51*c2c66affSColin Finck {
52*c2c66affSColin Finck     LSAP_RM_API_MESSAGE Message;
53*c2c66affSColin Finck     PPORT_MESSAGE ReplyMessage;
54*c2c66affSColin Finck     REMOTE_PORT_VIEW RemotePortView;
55*c2c66affSColin Finck     HANDLE MessagePort, DummyPortHandle;
56*c2c66affSColin Finck     NTSTATUS Status;
57*c2c66affSColin Finck 
58*c2c66affSColin Finck     /* Initialize the port message */
59*c2c66affSColin Finck     Message.Header.u1.s1.TotalLength = sizeof(Message);
60*c2c66affSColin Finck     Message.Header.u1.s1.DataLength = 0;
61*c2c66affSColin Finck 
62*c2c66affSColin Finck     /* Listen on the LSA command port */
63*c2c66affSColin Finck     Status = NtListenPort(SeLsaCommandPort, &Message.Header);
64*c2c66affSColin Finck     if (!NT_SUCCESS(Status))
65*c2c66affSColin Finck     {
66*c2c66affSColin Finck         ERR("LsapRmServerThread - Port Listen failed 0x%lx\n", Status);
67*c2c66affSColin Finck         return Status;
68*c2c66affSColin Finck     }
69*c2c66affSColin Finck 
70*c2c66affSColin Finck     /* Setup the Port View Structure */
71*c2c66affSColin Finck     RemotePortView.Length = sizeof(REMOTE_PORT_VIEW);
72*c2c66affSColin Finck     RemotePortView.ViewSize = 0;
73*c2c66affSColin Finck     RemotePortView.ViewBase = NULL;
74*c2c66affSColin Finck 
75*c2c66affSColin Finck     /* Accept the connection */
76*c2c66affSColin Finck     Status = NtAcceptConnectPort(&MessagePort,
77*c2c66affSColin Finck                                  0,
78*c2c66affSColin Finck                                  &Message.Header,
79*c2c66affSColin Finck                                  TRUE,
80*c2c66affSColin Finck                                  NULL,
81*c2c66affSColin Finck                                  &RemotePortView);
82*c2c66affSColin Finck     if (!NT_SUCCESS(Status))
83*c2c66affSColin Finck     {
84*c2c66affSColin Finck         ERR("LsapRmServerThread - Port Accept Connect failed 0x%lx\n", Status);
85*c2c66affSColin Finck         return Status;
86*c2c66affSColin Finck     }
87*c2c66affSColin Finck 
88*c2c66affSColin Finck     /* Complete the connection */
89*c2c66affSColin Finck     Status = NtCompleteConnectPort(MessagePort);
90*c2c66affSColin Finck     if (!NT_SUCCESS(Status))
91*c2c66affSColin Finck     {
92*c2c66affSColin Finck         ERR("LsapRmServerThread - Port Complete Connect failed 0x%lx\n", Status);
93*c2c66affSColin Finck         return Status;
94*c2c66affSColin Finck     }
95*c2c66affSColin Finck 
96*c2c66affSColin Finck     /* No reply yet */
97*c2c66affSColin Finck     ReplyMessage = NULL;
98*c2c66affSColin Finck 
99*c2c66affSColin Finck     /* Start looping */
100*c2c66affSColin Finck     while (TRUE)
101*c2c66affSColin Finck     {
102*c2c66affSColin Finck         /* Wait for a message */
103*c2c66affSColin Finck         Status = NtReplyWaitReceivePort(MessagePort,
104*c2c66affSColin Finck                                         NULL,
105*c2c66affSColin Finck                                         ReplyMessage,
106*c2c66affSColin Finck                                         &Message.Header);
107*c2c66affSColin Finck         if (!NT_SUCCESS(Status))
108*c2c66affSColin Finck         {
109*c2c66affSColin Finck             ERR("LsapRmServerThread - Failed to get message: 0x%lx", Status);
110*c2c66affSColin Finck             ReplyMessage = NULL;
111*c2c66affSColin Finck             continue;
112*c2c66affSColin Finck         }
113*c2c66affSColin Finck 
114*c2c66affSColin Finck         /* Check if this is a connection request */
115*c2c66affSColin Finck         if (Message.Header.u2.s2.Type == LPC_CONNECTION_REQUEST)
116*c2c66affSColin Finck         {
117*c2c66affSColin Finck             /* Reject connection request */
118*c2c66affSColin Finck             NtAcceptConnectPort(&DummyPortHandle,
119*c2c66affSColin Finck                                 NULL,
120*c2c66affSColin Finck                                 &Message.Header,
121*c2c66affSColin Finck                                 FALSE,
122*c2c66affSColin Finck                                 NULL,
123*c2c66affSColin Finck                                 NULL);
124*c2c66affSColin Finck 
125*c2c66affSColin Finck             /* Start over */
126*c2c66affSColin Finck             ReplyMessage = NULL;
127*c2c66affSColin Finck             continue;
128*c2c66affSColin Finck         }
129*c2c66affSColin Finck 
130*c2c66affSColin Finck         /* Check if this is an actual request */
131*c2c66affSColin Finck         if (Message.Header.u2.s2.Type == LPC_REQUEST)
132*c2c66affSColin Finck         {
133*c2c66affSColin Finck             ReplyMessage = &Message.Header;
134*c2c66affSColin Finck 
135*c2c66affSColin Finck             switch (Message.ApiNumber)
136*c2c66affSColin Finck             {
137*c2c66affSColin Finck                 case LsapAdtWriteLogApi:
138*c2c66affSColin Finck                     LsapAdtWriteLog(&Message);
139*c2c66affSColin Finck                     break;
140*c2c66affSColin Finck 
141*c2c66affSColin Finck                 case LsapAsyncApi:
142*c2c66affSColin Finck                     LsapAsync(&Message);
143*c2c66affSColin Finck                     break;
144*c2c66affSColin Finck 
145*c2c66affSColin Finck                 case LsapComponentTestApi:
146*c2c66affSColin Finck                     LsapComponentTest(&Message);
147*c2c66affSColin Finck                     break;
148*c2c66affSColin Finck 
149*c2c66affSColin Finck                 default:
150*c2c66affSColin Finck                     ERR("LsapRmServerThread - invalid API number: 0x%lx\n",
151*c2c66affSColin Finck                         Message.ApiNumber);
152*c2c66affSColin Finck                     ReplyMessage = NULL;
153*c2c66affSColin Finck             }
154*c2c66affSColin Finck 
155*c2c66affSColin Finck             continue;
156*c2c66affSColin Finck         }
157*c2c66affSColin Finck 
158*c2c66affSColin Finck         ERR("LsapRmServerThread - unexpected message type: 0x%lx\n",
159*c2c66affSColin Finck             Message.Header.u2.s2.Type);
160*c2c66affSColin Finck 
161*c2c66affSColin Finck         /* Start over */
162*c2c66affSColin Finck         ReplyMessage = NULL;
163*c2c66affSColin Finck     }
164*c2c66affSColin Finck }
165*c2c66affSColin Finck 
166*c2c66affSColin Finck NTSTATUS
167*c2c66affSColin Finck LsapRmInitializeServer(VOID)
168*c2c66affSColin Finck {
169*c2c66affSColin Finck     UNICODE_STRING Name;
170*c2c66affSColin Finck     OBJECT_ATTRIBUTES ObjectAttributes;
171*c2c66affSColin Finck     SECURITY_QUALITY_OF_SERVICE SecurityQos;
172*c2c66affSColin Finck     HANDLE InitEvent;
173*c2c66affSColin Finck     HANDLE ThreadHandle;
174*c2c66affSColin Finck     DWORD ThreadId;
175*c2c66affSColin Finck     NTSTATUS Status;
176*c2c66affSColin Finck 
177*c2c66affSColin Finck     /* Create the LSA command port */
178*c2c66affSColin Finck     RtlInitUnicodeString(&Name, L"\\SeLsaCommandPort");
179*c2c66affSColin Finck     InitializeObjectAttributes(&ObjectAttributes, &Name, 0, NULL, NULL);
180*c2c66affSColin Finck     Status = NtCreatePort(&SeLsaCommandPort,
181*c2c66affSColin Finck                           &ObjectAttributes,
182*c2c66affSColin Finck                           0,
183*c2c66affSColin Finck                           PORT_MAXIMUM_MESSAGE_LENGTH,
184*c2c66affSColin Finck                           2 * PAGE_SIZE);
185*c2c66affSColin Finck     if (!NT_SUCCESS(Status))
186*c2c66affSColin Finck     {
187*c2c66affSColin Finck         ERR("LsapRmInitializeServer - Port Create failed 0x%lx\n", Status);
188*c2c66affSColin Finck         return Status;
189*c2c66affSColin Finck     }
190*c2c66affSColin Finck 
191*c2c66affSColin Finck     /* Open the LSA init event */
192*c2c66affSColin Finck     RtlInitUnicodeString(&Name, L"\\SeLsaInitEvent");
193*c2c66affSColin Finck     InitializeObjectAttributes(&ObjectAttributes, &Name, 0, NULL, NULL);
194*c2c66affSColin Finck     Status = NtOpenEvent(&InitEvent, 2, &ObjectAttributes);
195*c2c66affSColin Finck     if (!NT_SUCCESS(Status))
196*c2c66affSColin Finck     {
197*c2c66affSColin Finck         ERR("LsapRmInitializeServer - Lsa Init Event Open failed 0x%lx\n", Status);
198*c2c66affSColin Finck         return Status;
199*c2c66affSColin Finck     }
200*c2c66affSColin Finck 
201*c2c66affSColin Finck     /* Signal the kernel, that we are ready */
202*c2c66affSColin Finck     Status = NtSetEvent(InitEvent, 0);
203*c2c66affSColin Finck     if (!NT_SUCCESS(Status))
204*c2c66affSColin Finck     {
205*c2c66affSColin Finck         ERR("LsapRmInitializeServer - Set Init Event failed 0x%lx\n", Status);
206*c2c66affSColin Finck         return Status;
207*c2c66affSColin Finck     }
208*c2c66affSColin Finck 
209*c2c66affSColin Finck     /* Setup the QoS structure */
210*c2c66affSColin Finck     SecurityQos.ImpersonationLevel = SecurityIdentification;
211*c2c66affSColin Finck     SecurityQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
212*c2c66affSColin Finck     SecurityQos.EffectiveOnly = TRUE;
213*c2c66affSColin Finck 
214*c2c66affSColin Finck     /* Connect to the kernel server */
215*c2c66affSColin Finck     RtlInitUnicodeString(&Name, L"\\SeRmCommandPort");
216*c2c66affSColin Finck     Status = NtConnectPort(&SeRmCommandPort,
217*c2c66affSColin Finck                            &Name,
218*c2c66affSColin Finck                            &SecurityQos,
219*c2c66affSColin Finck                            NULL,
220*c2c66affSColin Finck                            NULL,
221*c2c66affSColin Finck                            NULL,
222*c2c66affSColin Finck                            NULL,
223*c2c66affSColin Finck                            NULL);
224*c2c66affSColin Finck     if (!NT_SUCCESS(Status))
225*c2c66affSColin Finck     {
226*c2c66affSColin Finck         ERR("LsapRmInitializeServer - Connect to Rm Command Port failed 0x%lx\n", Status);
227*c2c66affSColin Finck         return Status;
228*c2c66affSColin Finck     }
229*c2c66affSColin Finck 
230*c2c66affSColin Finck     /* Create the server thread */
231*c2c66affSColin Finck     ThreadHandle = CreateThread(NULL, 0, LsapRmServerThread, NULL, 0, &ThreadId);
232*c2c66affSColin Finck     if (ThreadHandle == NULL)
233*c2c66affSColin Finck     {
234*c2c66affSColin Finck         ERR("LsapRmInitializeServer - Create Thread  failed 0x%lx\n", Status);
235*c2c66affSColin Finck         return STATUS_INSUFFICIENT_RESOURCES;
236*c2c66affSColin Finck     }
237*c2c66affSColin Finck 
238*c2c66affSColin Finck     /* Close the server thread handle */
239*c2c66affSColin Finck     CloseHandle(ThreadHandle);
240*c2c66affSColin Finck 
241*c2c66affSColin Finck     return STATUS_SUCCESS;
242*c2c66affSColin Finck }
243*c2c66affSColin Finck 
244*c2c66affSColin Finck NTSTATUS
245*c2c66affSColin Finck LsapRmCreateLogonSession(
246*c2c66affSColin Finck     PLUID LogonId)
247*c2c66affSColin Finck {
248*c2c66affSColin Finck     SEP_RM_API_MESSAGE RequestMessage;
249*c2c66affSColin Finck     SEP_RM_API_MESSAGE ReplyMessage;
250*c2c66affSColin Finck     NTSTATUS Status;
251*c2c66affSColin Finck 
252*c2c66affSColin Finck     TRACE("LsapRmCreateLogonSession(%p)\n", LogonId);
253*c2c66affSColin Finck 
254*c2c66affSColin Finck     RequestMessage.Header.u2.ZeroInit = 0;
255*c2c66affSColin Finck     RequestMessage.Header.u1.s1.TotalLength =
256*c2c66affSColin Finck         (CSHORT)(sizeof(PORT_MESSAGE) + sizeof(ULONG) + sizeof(LUID));
257*c2c66affSColin Finck     RequestMessage.Header.u1.s1.DataLength =
258*c2c66affSColin Finck         RequestMessage.Header.u1.s1.TotalLength -
259*c2c66affSColin Finck         (CSHORT)sizeof(PORT_MESSAGE);
260*c2c66affSColin Finck 
261*c2c66affSColin Finck     RequestMessage.ApiNumber = (ULONG)RmCreateLogonSession;
262*c2c66affSColin Finck     RtlCopyLuid(&RequestMessage.u.LogonLuid, LogonId);
263*c2c66affSColin Finck 
264*c2c66affSColin Finck     ReplyMessage.Header.u2.ZeroInit = 0;
265*c2c66affSColin Finck     ReplyMessage.Header.u1.s1.TotalLength =
266*c2c66affSColin Finck         (CSHORT)(sizeof(PORT_MESSAGE) + sizeof(ULONG) + sizeof(NTSTATUS));
267*c2c66affSColin Finck     ReplyMessage.Header.u1.s1.DataLength =
268*c2c66affSColin Finck         ReplyMessage.Header.u1.s1.TotalLength -
269*c2c66affSColin Finck         (CSHORT)sizeof(PORT_MESSAGE);
270*c2c66affSColin Finck 
271*c2c66affSColin Finck     ReplyMessage.u.ResultStatus = STATUS_SUCCESS;
272*c2c66affSColin Finck 
273*c2c66affSColin Finck     Status = NtRequestWaitReplyPort(SeRmCommandPort,
274*c2c66affSColin Finck                                     (PPORT_MESSAGE)&RequestMessage,
275*c2c66affSColin Finck                                     (PPORT_MESSAGE)&ReplyMessage);
276*c2c66affSColin Finck     if (NT_SUCCESS(Status))
277*c2c66affSColin Finck     {
278*c2c66affSColin Finck         Status = ReplyMessage.u.ResultStatus;
279*c2c66affSColin Finck     }
280*c2c66affSColin Finck 
281*c2c66affSColin Finck     return Status;
282*c2c66affSColin Finck }
283*c2c66affSColin Finck 
284*c2c66affSColin Finck NTSTATUS
285*c2c66affSColin Finck LsapRmDeleteLogonSession(
286*c2c66affSColin Finck     PLUID LogonId)
287*c2c66affSColin Finck {
288*c2c66affSColin Finck     SEP_RM_API_MESSAGE RequestMessage;
289*c2c66affSColin Finck     SEP_RM_API_MESSAGE ReplyMessage;
290*c2c66affSColin Finck     NTSTATUS Status;
291*c2c66affSColin Finck 
292*c2c66affSColin Finck     TRACE("LsapRmDeleteLogonSession(%p)\n", LogonId);
293*c2c66affSColin Finck 
294*c2c66affSColin Finck     RequestMessage.Header.u2.ZeroInit = 0;
295*c2c66affSColin Finck     RequestMessage.Header.u1.s1.TotalLength =
296*c2c66affSColin Finck         (CSHORT)(sizeof(PORT_MESSAGE) + sizeof(ULONG) + sizeof(LUID));
297*c2c66affSColin Finck     RequestMessage.Header.u1.s1.DataLength =
298*c2c66affSColin Finck         RequestMessage.Header.u1.s1.TotalLength -
299*c2c66affSColin Finck         (CSHORT)sizeof(PORT_MESSAGE);
300*c2c66affSColin Finck 
301*c2c66affSColin Finck     RequestMessage.ApiNumber = (ULONG)RmDeleteLogonSession;
302*c2c66affSColin Finck     RtlCopyLuid(&RequestMessage.u.LogonLuid, LogonId);
303*c2c66affSColin Finck 
304*c2c66affSColin Finck     ReplyMessage.Header.u2.ZeroInit = 0;
305*c2c66affSColin Finck     ReplyMessage.Header.u1.s1.TotalLength =
306*c2c66affSColin Finck         (CSHORT)(sizeof(PORT_MESSAGE) + sizeof(ULONG) + sizeof(NTSTATUS));
307*c2c66affSColin Finck     ReplyMessage.Header.u1.s1.DataLength =
308*c2c66affSColin Finck         ReplyMessage.Header.u1.s1.TotalLength -
309*c2c66affSColin Finck         (CSHORT)sizeof(PORT_MESSAGE);
310*c2c66affSColin Finck 
311*c2c66affSColin Finck     ReplyMessage.u.ResultStatus = STATUS_SUCCESS;
312*c2c66affSColin Finck 
313*c2c66affSColin Finck     Status = NtRequestWaitReplyPort(SeRmCommandPort,
314*c2c66affSColin Finck                                     (PPORT_MESSAGE)&RequestMessage,
315*c2c66affSColin Finck                                     (PPORT_MESSAGE)&ReplyMessage);
316*c2c66affSColin Finck     if (NT_SUCCESS(Status))
317*c2c66affSColin Finck     {
318*c2c66affSColin Finck         Status = ReplyMessage.u.ResultStatus;
319*c2c66affSColin Finck     }
320*c2c66affSColin Finck 
321*c2c66affSColin Finck     return Status;
322*c2c66affSColin Finck }
323