ReactOS 0.4.16-dev-258-g81860b4
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 Context->TrustedCaller = RequestMsg->ConnectInfo.TrustedCaller;
139
140 if (Context->TrustedCaller)
141 Context->TrustedCaller = LsapIsTrustedClient(ProcessHandle);
142
143 *LogonContext = Context;
144
145 return STATUS_SUCCESS;
146}
147
148
149static NTSTATUS
151{
152 PLSAP_LOGON_CONTEXT LogonContext = NULL;
154 BOOLEAN Accept;
155 REMOTE_PORT_VIEW RemotePortView;
157
158 TRACE("LsapHandlePortConnection(%p)\n", RequestMsg);
159
160 TRACE("Logon Process Name: %s\n", RequestMsg->ConnectInfo.LogonProcessNameBuffer);
161
162 if (RequestMsg->ConnectInfo.CreateContext != FALSE)
163 {
164 Status = LsapCheckLogonProcess(RequestMsg,
165 &LogonContext);
166
167 RequestMsg->ConnectInfo.OperationalMode = 0x43218765;
168
169 RequestMsg->ConnectInfo.Status = Status;
170 }
171
172 if (NT_SUCCESS(Status))
173 {
174 Accept = TRUE;
175 }
176 else
177 {
178 Accept = FALSE;
179 }
180
181 RemotePortView.Length = sizeof(REMOTE_PORT_VIEW);
183 (PVOID*)LogonContext,
184 &RequestMsg->h,
185 Accept,
186 NULL,
187 &RemotePortView);
188 if (!NT_SUCCESS(Status))
189 {
190 ERR("NtAcceptConnectPort failed (Status 0x%lx)\n", Status);
191 return Status;
192 }
193
194 if (Accept != FALSE)
195 {
196 if (LogonContext != NULL)
197 {
198 LogonContext->ConnectionHandle = ConnectionHandle;
199
201 &LogonContext->Entry);
202 }
203
205 if (!NT_SUCCESS(Status))
206 {
207 ERR("NtCompleteConnectPort failed (Status 0x%lx)\n", Status);
208 return Status;
209 }
210 }
211
212 return Status;
213}
214
215
218{
219 PLSAP_LOGON_CONTEXT LogonContext;
220 PLSA_API_MSG ReplyMsg = NULL;
221 LSA_API_MSG RequestMsg;
223
224 TRACE("AuthPortThreadRoutine() called\n");
225
227
228 for (;;)
229 {
230 TRACE("Reply: %p\n", ReplyMsg);
232 (PVOID*)&LogonContext,
233 (PPORT_MESSAGE)ReplyMsg,
234 (PPORT_MESSAGE)&RequestMsg);
235 if (!NT_SUCCESS(Status))
236 {
237 TRACE("NtReplyWaitReceivePort() failed (Status %lx)\n", Status);
238 break;
239 }
240
241 TRACE("Received message\n");
242
243 switch (RequestMsg.h.u2.s2.Type)
244 {
246 TRACE("Port connection request\n");
247 Status = LsapHandlePortConnection(&RequestMsg);
248 ReplyMsg = NULL;
249 break;
250
251 case LPC_PORT_CLOSED:
252 TRACE("Port closed\n");
253 ReplyMsg = NULL;
254 break;
255
256 case LPC_CLIENT_DIED:
257 TRACE("Client died\n");
258 ReplyMsg = NULL;
259 break;
260
261 default:
262 TRACE("Received request (ApiNumber: %lu)\n", RequestMsg.ApiNumber);
263
264 switch (RequestMsg.ApiNumber)
265 {
267 RequestMsg.Status = LsapCallAuthenticationPackage(&RequestMsg,
268 LogonContext);
269 ReplyMsg = &RequestMsg;
270 break;
271
273
274 ReplyMsg = &RequestMsg;
275 RequestMsg.Status = STATUS_SUCCESS;
277 &ReplyMsg->h);
278
279 LsapDeregisterLogonProcess(&RequestMsg,
280 LogonContext);
281
282 ReplyMsg = NULL;
283 break;
284
286 RequestMsg.Status = LsapLogonUser(&RequestMsg,
287 LogonContext);
288 ReplyMsg = &RequestMsg;
289 break;
290
292 RequestMsg.Status = LsapLookupAuthenticationPackage(&RequestMsg,
293 LogonContext);
294 ReplyMsg = &RequestMsg;
295 break;
296
298 RequestMsg.Status = LsapEnumLogonSessions(&RequestMsg);
299 ReplyMsg = &RequestMsg;
300 break;
301
303 RequestMsg.Status = LsapGetLogonSessionData(&RequestMsg);
304 ReplyMsg = &RequestMsg;
305 break;
306
308 RequestMsg.Status = LsapRegisterNotification(&RequestMsg);
309 ReplyMsg = &RequestMsg;
310 break;
311
312 default:
313 RequestMsg.Status = STATUS_INVALID_SYSTEM_SERVICE;
314 ReplyMsg = &RequestMsg;
315 break;
316 }
317
318 break;
319 }
320 }
321
322 return STATUS_SUCCESS;
323}
324
325
328{
331 DWORD ThreadId;
332 UNICODE_STRING EventName;
335
336 TRACE("StartAuthenticationPort()\n");
337
338 /* Initialize the logon context list */
340
342 L"\\LsaAuthenticationPort");
343
345 &PortName,
346 0,
347 NULL,
348 NULL);
349
352 sizeof(LSA_CONNECTION_INFO),
353 sizeof(LSA_API_MSG),
354 sizeof(LSA_API_MSG) * 32);
355 if (!NT_SUCCESS(Status))
356 {
357 WARN("NtCreatePort() failed (Status %lx)\n", Status);
358 return Status;
359 }
360
361 RtlInitUnicodeString(&EventName,
362 L"\\SECURITY\\LSA_AUTHENTICATION_INITIALIZED");
364 &EventName,
366 NULL,
367 NULL);
371 if (!NT_SUCCESS(Status))
372 {
373 TRACE("NtOpenEvent failed (Status 0x%08lx)\n", Status);
374
379 FALSE);
380 if (!NT_SUCCESS(Status))
381 {
382 WARN("NtCreateEvent failed (Status 0x%08lx)\n", Status);
383 return Status;
384 }
385 }
386
389 if (!NT_SUCCESS(Status))
390 {
391 WARN("NtSetEvent failed (Status 0x%08lx)\n", Status);
392 return Status;
393 }
394
396 0x1000,
398 NULL,
399 0,
400 &ThreadId);
401
402
403 return STATUS_SUCCESS;
404}
405
406/* EOF */
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:150
NTSTATUS WINAPI AuthPortThreadRoutine(PVOID Param)
Definition: authport.c:217
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:327
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:590
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:608
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
@ 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:177
LSA_CONNECTION_INFO ConnectInfo
Definition: lsass.h:180
BOOL CreateContext
Definition: lsass.h:37
BOOL TrustedCaller
Definition: lsass.h:38
LSA_OPERATIONAL_MODE OperationalMode
Definition: lsass.h:34
NTSTATUS Status
Definition: lsass.h:33
CHAR LogonProcessNameBuffer[LSASS_MAX_LOGON_PROCESS_NAME_LENGTH+1]
Definition: lsass.h:36
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