ReactOS  0.4.14-dev-41-g31d7680
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 
21 static 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 
38 static
39 BOOL
42 {
43  LUID TcbPrivilege = {SE_TCB_PRIVILEGE, 0};
46  ULONG Size, i;
47  BOOL Trusted = FALSE;
49 
52  &TokenHandle);
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 
70  Privileges,
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 
85 done:
86  if (Privileges != NULL)
87  RtlFreeHeap(RtlGetProcessHeap(), 0, Privileges);
88 
89  if (TokenHandle != NULL)
91 
92  return Trusted;
93 }
94 
95 
96 static 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->Untrusted = RequestMsg->ConnectInfo.Untrusted;
139 
140  if (Context->Untrusted == FALSE)
142 
143  *LogonContext = Context;
144 
145  return STATUS_SUCCESS;
146 }
147 
148 
149 static 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 
326 NTSTATUS
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 */
NTSTATUS LsapLogonUser(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authpackage.c:1375
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:35
DWORD(WINAPI * LPTHREAD_START_ROUTINE)(LPVOID)
Definition: winbase.h:707
#define TRUE
Definition: types.h:120
#define STATUS_INSUFFICIENT_RESOURCES
Definition: udferr_usr.h:158
static NTSTATUS LsapHandlePortConnection(PLSA_API_MSG RequestMsg)
Definition: authport.c:150
#define PROCESS_QUERY_INFORMATION
Definition: pstypes.h:158
NTSTATUS StartAuthenticationPort(VOID)
Definition: authport.c:327
FORCEINLINE VOID InsertHeadList(_Inout_ PLIST_ENTRY ListHead, _Inout_ __drv_aliasesMem PLIST_ENTRY Entry)
Definition: rtlfuncs.h:201
static LIST_ENTRY LsapLogonContextList
Definition: authport.c:13
#define WARN(fmt,...)
Definition: debug.h:111
static BOOL LsapIsTrustedClient(_In_ HANDLE ProcessHandle)
Definition: authport.c:40
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:606
static HANDLE AuthPortHandle
Definition: authport.c:16
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:100
#define OBJ_PERMANENT
Definition: winternl.h:226
HANDLE ClientProcessHandle
Definition: lsasrv.h:80
NTSTATUS LsapCallAuthenticationPackage(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authpackage.c:545
NTSTATUS NTAPI NtOpenProcessToken(IN HANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess, OUT PHANDLE TokenHandle)
Definition: security.c:350
PORT_MESSAGE h
Definition: lsass.h:177
HANDLE UniqueProcess
Definition: compat.h:474
CHAR LogonProcessNameBuffer[LSASS_MAX_LOGON_PROCESS_NAME_LENGTH+1]
Definition: lsass.h:36
NTSTATUS NTAPI NtOpenProcess(OUT PHANDLE ProcessHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN PCLIENT_ID ClientId)
Definition: process.c:1440
#define STATUS_BUFFER_TOO_SMALL
Definition: shellext.h:64
NTSTATUS NTAPI NtReplyPort(IN HANDLE PortHandle, IN PPORT_MESSAGE ReplyMessage)
Definition: reply.c:190
BOOL CreateContext
Definition: lsass.h:37
NTSTATUS Status
Definition: lsass.h:33
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 SE_TCB_PRIVILEGE
Definition: security.c:661
unsigned int BOOL
Definition: ntddk_ex.h:94
#define RtlEqualLuid(Luid1, Luid2)
Definition: rtlfuncs.h:301
#define PROCESS_DUP_HANDLE
unsigned char BOOLEAN
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:136
smooth NULL
Definition: ftsmooth.c:416
_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)
Definition: token.c:1839
struct _REMOTE_PORT_VIEW REMOTE_PORT_VIEW
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
_In_ ACCESS_MASK _In_ ULONG _Out_ PHANDLE TokenHandle
Definition: psfuncs.h:715
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:588
#define TRACE(s)
Definition: solgame.cpp:4
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
#define PROCESS_VM_WRITE
Definition: pstypes.h:154
NTSTATUS NTAPI NtCreatePort(OUT PHANDLE PortHandle, IN POBJECT_ATTRIBUTES ObjectAttributes, IN ULONG MaxConnectInfoLength, IN ULONG MaxDataLength, IN ULONG MaxPoolUsage)
Definition: create.c:212
NTSTATUS NTAPI NtSetEvent(IN HANDLE EventHandle, OUT PLONG PreviousState OPTIONAL)
Definition: event.c:458
#define TOKEN_QUERY
Definition: setypes.h:874
static HANDLE PortThreadHandle
Definition: authport.c:15
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
NTSTATUS LsapEnumLogonSessions(IN OUT PLSA_API_MSG RequestMsg)
Definition: session.c:254
NTSTATUS LsapGetLogonSessionData(IN OUT PLSA_API_MSG RequestMsg)
Definition: session.c:350
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
_In_ PSECURITY_SUBJECT_CONTEXT _In_ BOOLEAN _In_ ACCESS_MASK _In_ ACCESS_MASK _Outptr_opt_ PPRIVILEGE_SET * Privileges
Definition: sefuncs.h:13
NTSTATUS NTAPI NtClose(IN HANDLE Handle)
Definition: obhandle.c:3399
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
HANDLE UniqueThread
Definition: compat.h:475
NTSTATUS NTAPI NtReplyWaitReceivePort(IN HANDLE PortHandle, OUT PVOID *PortContext OPTIONAL, IN PPORT_MESSAGE ReplyMessage OPTIONAL, OUT PPORT_MESSAGE ReceiveMessage)
Definition: reply.c:743
static const WCHAR L[]
Definition: oid.c:1250
Definition: typedefs.h:117
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
LSA_OPERATIONAL_MODE OperationalMode
Definition: lsass.h:34
NTSTATUS NTAPI NtOpenEvent(OUT PHANDLE EventHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes)
Definition: event.c:185
Status
Definition: gdiplustypes.h:24
LIST_ENTRY Entry
Definition: lsasrv.h:79
#define ERR(fmt,...)
Definition: debug.h:109
#define _In_
Definition: no_sal2.h:204
NTSTATUS LsapLookupAuthenticationPackage(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authpackage.c:510
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
NTSTATUS WINAPI AuthPortThreadRoutine(PVOID Param)
Definition: authport.c:217
static NTSTATUS LsapCheckLogonProcess(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT *LogonContext)
Definition: authport.c:97
static NTSTATUS LsapDeregisterLogonProcess(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authport.c:22
CLIENT_ID ClientId
Definition: winternl.h:1751
NTSTATUS LsapRegisterNotification(PLSA_API_MSG RequestMsg)
Definition: notify.c:59
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
LSA_CONNECTION_INFO ConnectInfo
Definition: lsass.h:180
#define PROCESS_VM_READ
Definition: pstypes.h:153
HANDLE ConnectionHandle
Definition: lsasrv.h:81
struct tagContext Context
Definition: acpixf.h:1024
NTSTATUS NTAPI NtCompleteConnectPort(IN HANDLE PortHandle)
Definition: complete.c:421
unsigned int ULONG
Definition: retypes.h:1
#define EVENT_MODIFY_STATE
Definition: winbase.h:163
NTSYSAPI VOID NTAPI RtlInitUnicodeString(PUNICODE_STRING DestinationString, PCWSTR SourceString)
#define STATUS_INVALID_SYSTEM_SERVICE
Definition: ntstatus.h:251
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define PROCESS_VM_OPERATION
Definition: pstypes.h:152
_In_ PKSPIN_CONNECT _In_ ACCESS_MASK _Out_ PHANDLE ConnectionHandle
Definition: ks.h:4536
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:403
return STATUS_SUCCESS
Definition: btrfs.c:2966
IN PUNICODE_STRING PortName
Definition: conport.c:35
_Out_ PHANDLE EventHandle
Definition: iofuncs.h:855