ReactOS 0.4.16-dev-736-g28b802b
authport.c
Go to the documentation of this file.
1/*
2 * PROJECT: Local Security Authority Server DLL
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/win32/lsasrv/authport.c
5 * PURPOSE: LsaAuthenticationPort server routines
6 * COPYRIGHT: Copyright 2009 Eric Kohl
7 */
8
9#include "lsasrv.h"
10
11#include <ndk/lpcfuncs.h>
12
14
17
18
19/* FUNCTIONS ***************************************************************/
20
21static NTSTATUS
23 PLSAP_LOGON_CONTEXT LogonContext)
24{
25 TRACE("LsapDeregisterLogonProcess(%p %p)\n", RequestMsg, LogonContext);
26
27 RemoveHeadList(&LogonContext->Entry);
28
29 NtClose(LogonContext->ClientProcessHandle);
30 NtClose(LogonContext->ConnectionHandle);
31
32 RtlFreeHeap(RtlGetProcessHeap(), 0, LogonContext);
33
34 return STATUS_SUCCESS;
35}
36
37
38static
39BOOL
42{
43 LUID TcbPrivilege = {SE_TCB_PRIVILEGE, 0};
46 ULONG Size, i;
47 BOOL Trusted = FALSE;
49
53 if (!NT_SUCCESS(Status))
54 goto done;
55
58 NULL,
59 0,
60 &Size);
62 goto done;
63
64 Privileges = RtlAllocateHeap(RtlGetProcessHeap(), 0, Size);
65 if (Privileges == NULL)
66 goto done;
67
71 Size,
72 &Size);
73 if (!NT_SUCCESS(Status))
74 goto done;
75
76 for (i = 0; i < Privileges->PrivilegeCount; i++)
77 {
78 if (RtlEqualLuid(&Privileges->Privileges[i].Luid, &TcbPrivilege))
79 {
80 Trusted = TRUE;
81 break;
82 }
83 }
84
85done:
86 if (Privileges != NULL)
87 RtlFreeHeap(RtlGetProcessHeap(), 0, Privileges);
88
89 if (TokenHandle != NULL)
91
92 return Trusted;
93}
94
95
96static NTSTATUS
98 PLSAP_LOGON_CONTEXT *LogonContext)
99{
104
105 TRACE("LsapCheckLogonProcess(%p)\n", RequestMsg);
106
107 TRACE("Client ID: %p %p\n", RequestMsg->h.ClientId.UniqueProcess, RequestMsg->h.ClientId.UniqueThread);
108
110 NULL,
111 0,
112 NULL,
113 NULL);
114
118 &RequestMsg->h.ClientId);
119 if (!NT_SUCCESS(Status))
120 {
121 TRACE("NtOpenProcess() failed (Status %lx)\n", Status);
122 return Status;
123 }
124
125 /* Allocate the logon context */
126 Context = RtlAllocateHeap(RtlGetProcessHeap(),
128 sizeof(LSAP_LOGON_CONTEXT));
129 if (Context == NULL)
130 {
133 }
134
135 TRACE("New LogonContext: %p\n", Context);
136
137 Context->ClientProcessHandle = ProcessHandle;
138
139 switch (RequestMsg->ConnectInfo.TrustedCaller)
140 {
141 case NO:
142 Context->TrustedCaller = FALSE;
143 break;
144
145 case YES:
146 Context->TrustedCaller = TRUE;
147 break;
148
149 case CHECK:
150 default:
151 Context->TrustedCaller = LsapIsTrustedClient(ProcessHandle);
152 break;
153 }
154
155 TRACE("TrustedCaller: %u\n", Context->TrustedCaller);
156
157 *LogonContext = Context;
158
159 return STATUS_SUCCESS;
160}
161
162
163static NTSTATUS
165{
166 PLSAP_LOGON_CONTEXT LogonContext = NULL;
168 BOOLEAN Accept;
169 REMOTE_PORT_VIEW RemotePortView;
171
172 TRACE("LsapHandlePortConnection(%p)\n", RequestMsg);
173
174 TRACE("Logon Process Name: %s\n", RequestMsg->ConnectInfo.LogonProcessNameBuffer);
175
176 if (RequestMsg->ConnectInfo.CreateContext != FALSE)
177 {
178 Status = LsapCheckLogonProcess(RequestMsg,
179 &LogonContext);
180
181 RequestMsg->ConnectInfo.OperationalMode = 0x43218765;
182
183 RequestMsg->ConnectInfo.Status = Status;
184 }
185
186 if (NT_SUCCESS(Status))
187 {
188 Accept = TRUE;
189 }
190 else
191 {
192 Accept = FALSE;
193 }
194
195 RemotePortView.Length = sizeof(REMOTE_PORT_VIEW);
197 (PVOID*)LogonContext,
198 &RequestMsg->h,
199 Accept,
200 NULL,
201 &RemotePortView);
202 if (!NT_SUCCESS(Status))
203 {
204 ERR("NtAcceptConnectPort failed (Status 0x%lx)\n", Status);
205 return Status;
206 }
207
208 if (Accept != FALSE)
209 {
210 if (LogonContext != NULL)
211 {
212 LogonContext->ConnectionHandle = ConnectionHandle;
213
215 &LogonContext->Entry);
216 }
217
219 if (!NT_SUCCESS(Status))
220 {
221 ERR("NtCompleteConnectPort failed (Status 0x%lx)\n", Status);
222 return Status;
223 }
224 }
225
226 return Status;
227}
228
229
232{
233 PLSAP_LOGON_CONTEXT LogonContext;
234 PLSA_API_MSG ReplyMsg = NULL;
235 LSA_API_MSG RequestMsg;
237
238 TRACE("AuthPortThreadRoutine() called\n");
239
241
242 for (;;)
243 {
244 TRACE("Reply: %p\n", ReplyMsg);
246 (PVOID*)&LogonContext,
247 (PPORT_MESSAGE)ReplyMsg,
248 (PPORT_MESSAGE)&RequestMsg);
249 if (!NT_SUCCESS(Status))
250 {
251 TRACE("NtReplyWaitReceivePort() failed (Status %lx)\n", Status);
252 break;
253 }
254
255 TRACE("Received message\n");
256
257 switch (RequestMsg.h.u2.s2.Type)
258 {
260 TRACE("Port connection request\n");
261 Status = LsapHandlePortConnection(&RequestMsg);
262 ReplyMsg = NULL;
263 break;
264
265 case LPC_PORT_CLOSED:
266 TRACE("Port closed\n");
267 ReplyMsg = NULL;
268 break;
269
270 case LPC_CLIENT_DIED:
271 TRACE("Client died\n");
272 ReplyMsg = NULL;
273 break;
274
275 default:
276 TRACE("Received request (ApiNumber: %lu)\n", RequestMsg.ApiNumber);
277
278 switch (RequestMsg.ApiNumber)
279 {
281 RequestMsg.Status = LsapCallAuthenticationPackage(&RequestMsg,
282 LogonContext);
283 ReplyMsg = &RequestMsg;
284 break;
285
287
288 ReplyMsg = &RequestMsg;
289 RequestMsg.Status = STATUS_SUCCESS;
291 &ReplyMsg->h);
292
293 LsapDeregisterLogonProcess(&RequestMsg,
294 LogonContext);
295
296 ReplyMsg = NULL;
297 break;
298
300 RequestMsg.Status = LsapLogonUser(&RequestMsg,
301 LogonContext);
302 ReplyMsg = &RequestMsg;
303 break;
304
306 RequestMsg.Status = LsapLookupAuthenticationPackage(&RequestMsg,
307 LogonContext);
308 ReplyMsg = &RequestMsg;
309 break;
310
312 RequestMsg.Status = LsapEnumLogonSessions(&RequestMsg);
313 ReplyMsg = &RequestMsg;
314 break;
315
317 RequestMsg.Status = LsapGetLogonSessionData(&RequestMsg);
318 ReplyMsg = &RequestMsg;
319 break;
320
322 RequestMsg.Status = LsapRegisterNotification(&RequestMsg);
323 ReplyMsg = &RequestMsg;
324 break;
325
326 default:
327 RequestMsg.Status = STATUS_INVALID_SYSTEM_SERVICE;
328 ReplyMsg = &RequestMsg;
329 break;
330 }
331
332 break;
333 }
334 }
335
336 return STATUS_SUCCESS;
337}
338
339
342{
345 DWORD ThreadId;
346 UNICODE_STRING EventName;
349
350 TRACE("StartAuthenticationPort()\n");
351
352 /* Initialize the logon context list */
354
356 L"\\LsaAuthenticationPort");
357
359 &PortName,
360 0,
361 NULL,
362 NULL);
363
366 sizeof(LSA_CONNECTION_INFO),
367 sizeof(LSA_API_MSG),
368 sizeof(LSA_API_MSG) * 32);
369 if (!NT_SUCCESS(Status))
370 {
371 WARN("NtCreatePort() failed (Status %lx)\n", Status);
372 return Status;
373 }
374
375 RtlInitUnicodeString(&EventName,
376 L"\\SECURITY\\LSA_AUTHENTICATION_INITIALIZED");
378 &EventName,
380 NULL,
381 NULL);
385 if (!NT_SUCCESS(Status))
386 {
387 TRACE("NtOpenEvent failed (Status 0x%08lx)\n", Status);
388
393 FALSE);
394 if (!NT_SUCCESS(Status))
395 {
396 WARN("NtCreateEvent failed (Status 0x%08lx)\n", Status);
397 return Status;
398 }
399 }
400
403 if (!NT_SUCCESS(Status))
404 {
405 WARN("NtSetEvent failed (Status 0x%08lx)\n", Status);
406 return Status;
407 }
408
410 0x1000,
412 NULL,
413 0,
414 &ThreadId);
415
416
417 return STATUS_SUCCESS;
418}
419
420/* EOF */
#define CHECK(hwndTarget)
static UNICODE_STRING PortName
unsigned char BOOLEAN
NTSTATUS LsapCallAuthenticationPackage(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authpackage.c:564
NTSTATUS LsapLogonUser(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authpackage.c:1394
NTSTATUS LsapLookupAuthenticationPackage(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authpackage.c:510
static HANDLE PortThreadHandle
Definition: authport.c:15
static NTSTATUS LsapCheckLogonProcess(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT *LogonContext)
Definition: authport.c:97
static LIST_ENTRY LsapLogonContextList
Definition: authport.c:13
static NTSTATUS LsapHandlePortConnection(PLSA_API_MSG RequestMsg)
Definition: authport.c:164
NTSTATUS WINAPI AuthPortThreadRoutine(PVOID Param)
Definition: authport.c:231
static NTSTATUS LsapDeregisterLogonProcess(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authport.c:22
static HANDLE AuthPortHandle
Definition: authport.c:16
NTSTATUS StartAuthenticationPort(VOID)
Definition: authport.c:341
static BOOL LsapIsTrustedClient(_In_ HANDLE ProcessHandle)
Definition: authport.c:40
LONG NTSTATUS
Definition: precomp.h:26
#define WARN(fmt,...)
Definition: precomp.h:61
#define ERR(fmt,...)
Definition: precomp.h:57
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:616
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:634
NTSTATUS NTAPI NtAcceptConnectPort(OUT PHANDLE PortHandle, IN PVOID PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage, IN BOOLEAN AcceptConnection, IN OUT PPORT_VIEW ServerView OPTIONAL, OUT PREMOTE_PORT_VIEW ClientView OPTIONAL)
Definition: complete.c:40
NTSTATUS NTAPI NtCompleteConnectPort(IN HANDLE PortHandle)
Definition: complete.c:423
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
HANDLE WINAPI DECLSPEC_HOTPATCH CreateThread(IN LPSECURITY_ATTRIBUTES lpThreadAttributes, IN DWORD dwStackSize, IN LPTHREAD_START_ROUTINE lpStartAddress, IN LPVOID lpParameter, IN DWORD dwCreationFlags, OUT LPDWORD lpThreadId)
Definition: thread.c:137
NTSTATUS LsapRegisterNotification(PLSA_API_MSG RequestMsg)
Definition: notify.c:159
NTSTATUS LsapEnumLogonSessions(IN OUT PLSA_API_MSG RequestMsg)
Definition: session.c:299
NTSTATUS LsapGetLogonSessionData(IN OUT PLSA_API_MSG RequestMsg)
Definition: session.c:395
#define InsertHeadList(ListHead, Entry)
#define RemoveHeadList(ListHead)
Definition: env_spec_w32.h:964
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
Status
Definition: gdiplustypes.h:25
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define PROCESS_VM_READ
Definition: pstypes.h:162
#define PROCESS_QUERY_INFORMATION
Definition: pstypes.h:167
#define PROCESS_VM_WRITE
Definition: pstypes.h:163
#define PROCESS_VM_OPERATION
Definition: pstypes.h:161
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define OBJ_PERMANENT
Definition: winternl.h:226
#define PROCESS_DUP_HANDLE
_In_ PKSPIN_CONNECT _In_ ACCESS_MASK _Out_ PHANDLE ConnectionHandle
Definition: ks.h:4538
@ NO
Definition: lsass.h:32
@ YES
Definition: lsass.h:33
@ LSASS_REQUEST_ENUM_LOGON_SESSIONS
Definition: lsass.h:24
@ LSASS_REQUEST_LOGON_USER
Definition: lsass.h:22
@ LSASS_REQUEST_POLICY_CHANGE_NOTIFY
Definition: lsass.h:26
@ LSASS_REQUEST_CALL_AUTHENTICATION_PACKAGE
Definition: lsass.h:20
@ LSASS_REQUEST_GET_LOGON_SESSION_DATA
Definition: lsass.h:25
@ LSASS_REQUEST_LOOKUP_AUTHENTICATION_PACKAGE
Definition: lsass.h:23
@ LSASS_REQUEST_DEREGISTER_LOGON_PROCESS
Definition: lsass.h:21
#define SE_TCB_PRIVILEGE
Definition: security.c:661
#define LPC_CLIENT_DIED
Definition: port.c:98
#define LPC_CONNECTION_REQUEST
Definition: port.c:102
#define LPC_PORT_CLOSED
Definition: port.c:97
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:403
_In_ ACCESS_MASK _In_ ULONG _Out_ PHANDLE TokenHandle
Definition: psfuncs.h:726
#define _In_
Definition: no_sal2.h:158
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3402
@ NotificationEvent
struct _REMOTE_PORT_VIEW REMOTE_PORT_VIEW
NTSTATUS NTAPI NtSetEvent(IN HANDLE EventHandle, OUT PLONG PreviousState OPTIONAL)
Definition: event.c:455
NTSTATUS NTAPI NtOpenEvent(OUT PHANDLE EventHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
Definition: event.c:181
NTSTATUS NTAPI NtCreateEvent(OUT PHANDLE EventHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes OPTIONAL, IN EVENT_TYPE EventType, IN BOOLEAN InitialState)
Definition: event.c:96
NTSTATUS NTAPI NtCreatePort(OUT PHANDLE PortHandle, IN POBJECT_ATTRIBUTES ObjectAttributes, IN ULONG MaxConnectInfoLength, IN ULONG MaxDataLength, IN ULONG MaxPoolUsage)
Definition: create.c:222
NTSTATUS NTAPI NtReplyPort(IN HANDLE PortHandle, IN PPORT_MESSAGE ReplyMessage)
Definition: reply.c:190
NTSTATUS NTAPI NtReplyWaitReceivePort(IN HANDLE PortHandle, OUT PVOID *PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage OPTIONAL, OUT PPORT_MESSAGE ReceiveMessage)
Definition: reply.c:743
NTSTATUS NTAPI NtOpenProcess(OUT PHANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN PCLIENT_ID ClientId)
Definition: process.c:1440
NTSTATUS NTAPI NtOpenProcessToken(IN HANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess, OUT PHANDLE TokenHandle)
Definition: security.c:350
#define STATUS_INVALID_SYSTEM_SERVICE
Definition: ntstatus.h:265
#define L(x)
Definition: ntvdm.h:50
#define STATUS_SUCCESS
Definition: shellext.h:65
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:69
#define TRACE(s)
Definition: solgame.cpp:4
HANDLE UniqueThread
Definition: compat.h:826
HANDLE UniqueProcess
Definition: compat.h:825
Definition: typedefs.h:120
LIST_ENTRY Entry
Definition: lsasrv.h:79
HANDLE ClientProcessHandle
Definition: lsasrv.h:80
HANDLE ConnectionHandle
Definition: lsasrv.h:81
PORT_MESSAGE h
Definition: lsass.h:183
LSA_CONNECTION_INFO ConnectInfo
Definition: lsass.h:186
BOOL CreateContext
Definition: lsass.h:43
LSA_OPERATIONAL_MODE OperationalMode
Definition: lsass.h:40
LSA_TRUSTED_CALLER TrustedCaller
Definition: lsass.h:44
NTSTATUS Status
Definition: lsass.h:39
CHAR LogonProcessNameBuffer[LSASS_MAX_LOGON_PROCESS_NAME_LENGTH+1]
Definition: lsass.h:42
CLIENT_ID ClientId
Definition: winternl.h:1751
_Must_inspect_result_ __kernel_entry NTSTATUS NTAPI NtQueryInformationToken(_In_ HANDLE TokenHandle, _In_ TOKEN_INFORMATION_CLASS TokenInformationClass, _Out_writes_bytes_to_opt_(TokenInformationLength, *ReturnLength) PVOID TokenInformation, _In_ ULONG TokenInformationLength, _Out_ PULONG ReturnLength)
Queries a specific type of information in regard of an access token based upon the information class....
Definition: tokencls.c:473
uint32_t ULONG
Definition: typedefs.h:59
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
#define EVENT_MODIFY_STATE
Definition: winbase.h:164
DWORD(WINAPI * LPTHREAD_START_ROUTINE)(LPVOID)
Definition: winbase.h:754
#define WINAPI
Definition: msvc.h:6
_Out_ PHANDLE EventHandle
Definition: iofuncs.h:857
#define RtlEqualLuid(Luid1, Luid2)
Definition: rtlfuncs.h:304
_In_ PSECURITY_SUBJECT_CONTEXT _In_ BOOLEAN _In_ ACCESS_MASK _In_ ACCESS_MASK _Outptr_opt_ PPRIVILEGE_SET * Privileges
Definition: sefuncs.h:17
#define TOKEN_QUERY
Definition: setypes.h:928
@ TokenPrivileges
Definition: setypes.h:968