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