ReactOS  0.4.13-dev-39-g8b6696f
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 NTSTATUS
40  PLSAP_LOGON_CONTEXT *LogonContext)
41 {
46 
47  TRACE("LsapCheckLogonProcess(%p)\n", RequestMsg);
48 
49  TRACE("Client ID: %p %p\n", RequestMsg->h.ClientId.UniqueProcess, RequestMsg->h.ClientId.UniqueThread);
50 
52  NULL,
53  0,
54  NULL,
55  NULL);
56 
60  &RequestMsg->h.ClientId);
61  if (!NT_SUCCESS(Status))
62  {
63  TRACE("NtOpenProcess() failed (Status %lx)\n", Status);
64  return Status;
65  }
66 
67  /* Allocate the logon context */
68  Context = RtlAllocateHeap(RtlGetProcessHeap(),
70  sizeof(LSAP_LOGON_CONTEXT));
71  if (Context == NULL)
72  {
75  }
76 
77  TRACE("New LogonContext: %p\n", Context);
78 
79  Context->ClientProcessHandle = ProcessHandle;
80 
81  *LogonContext = Context;
82 
83  return STATUS_SUCCESS;
84 }
85 
86 
87 static NTSTATUS
89 {
90  PLSAP_LOGON_CONTEXT LogonContext = NULL;
92  BOOLEAN Accept;
93  REMOTE_PORT_VIEW RemotePortView;
95 
96  TRACE("LsapHandlePortConnection(%p)\n", RequestMsg);
97 
98  TRACE("Logon Process Name: %s\n", RequestMsg->ConnectInfo.LogonProcessNameBuffer);
99 
100  if (RequestMsg->ConnectInfo.CreateContext != FALSE)
101  {
102  Status = LsapCheckLogonProcess(RequestMsg,
103  &LogonContext);
104 
105  RequestMsg->ConnectInfo.OperationalMode = 0x43218765;
106 
107  RequestMsg->ConnectInfo.Status = Status;
108  }
109 
110  if (NT_SUCCESS(Status))
111  {
112  Accept = TRUE;
113  }
114  else
115  {
116  Accept = FALSE;
117  }
118 
119  RemotePortView.Length = sizeof(REMOTE_PORT_VIEW);
121  (PVOID*)LogonContext,
122  &RequestMsg->h,
123  Accept,
124  NULL,
125  &RemotePortView);
126  if (!NT_SUCCESS(Status))
127  {
128  ERR("NtAcceptConnectPort failed (Status 0x%lx)\n", Status);
129  return Status;
130  }
131 
132  if (Accept != FALSE)
133  {
134  if (LogonContext != NULL)
135  {
136  LogonContext->ConnectionHandle = ConnectionHandle;
137 
139  &LogonContext->Entry);
140  }
141 
143  if (!NT_SUCCESS(Status))
144  {
145  ERR("NtCompleteConnectPort failed (Status 0x%lx)\n", Status);
146  return Status;
147  }
148  }
149 
150  return Status;
151 }
152 
153 
156 {
157  PLSAP_LOGON_CONTEXT LogonContext;
158  PLSA_API_MSG ReplyMsg = NULL;
159  LSA_API_MSG RequestMsg;
161 
162  TRACE("AuthPortThreadRoutine() called\n");
163 
165 
166  for (;;)
167  {
168  TRACE("Reply: %p\n", ReplyMsg);
170  (PVOID*)&LogonContext,
171  (PPORT_MESSAGE)ReplyMsg,
172  (PPORT_MESSAGE)&RequestMsg);
173  if (!NT_SUCCESS(Status))
174  {
175  TRACE("NtReplyWaitReceivePort() failed (Status %lx)\n", Status);
176  break;
177  }
178 
179  TRACE("Received message\n");
180 
181  switch (RequestMsg.h.u2.s2.Type)
182  {
184  TRACE("Port connection request\n");
185  Status = LsapHandlePortConnection(&RequestMsg);
186  ReplyMsg = NULL;
187  break;
188 
189  case LPC_PORT_CLOSED:
190  TRACE("Port closed\n");
191  ReplyMsg = NULL;
192  break;
193 
194  case LPC_CLIENT_DIED:
195  TRACE("Client died\n");
196  ReplyMsg = NULL;
197  break;
198 
199  default:
200  TRACE("Received request (ApiNumber: %lu)\n", RequestMsg.ApiNumber);
201 
202  switch (RequestMsg.ApiNumber)
203  {
205  RequestMsg.Status = LsapCallAuthenticationPackage(&RequestMsg,
206  LogonContext);
207  ReplyMsg = &RequestMsg;
208  break;
209 
211 
212  ReplyMsg = &RequestMsg;
213  RequestMsg.Status = STATUS_SUCCESS;
215  &ReplyMsg->h);
216 
217  LsapDeregisterLogonProcess(&RequestMsg,
218  LogonContext);
219 
220  ReplyMsg = NULL;
221  break;
222 
224  RequestMsg.Status = LsapLogonUser(&RequestMsg,
225  LogonContext);
226  ReplyMsg = &RequestMsg;
227  break;
228 
230  RequestMsg.Status = LsapLookupAuthenticationPackage(&RequestMsg,
231  LogonContext);
232  ReplyMsg = &RequestMsg;
233  break;
234 
236  RequestMsg.Status = LsapEnumLogonSessions(&RequestMsg);
237  ReplyMsg = &RequestMsg;
238  break;
239 
241  RequestMsg.Status = LsapGetLogonSessionData(&RequestMsg);
242  ReplyMsg = &RequestMsg;
243  break;
244 
246  RequestMsg.Status = LsapRegisterNotification(&RequestMsg);
247  ReplyMsg = &RequestMsg;
248  break;
249 
250  default:
251  RequestMsg.Status = STATUS_INVALID_SYSTEM_SERVICE;
252  ReplyMsg = &RequestMsg;
253  break;
254  }
255 
256  break;
257  }
258  }
259 
260  return STATUS_SUCCESS;
261 }
262 
263 
264 NTSTATUS
266 {
269  DWORD ThreadId;
270  UNICODE_STRING EventName;
273 
274  TRACE("StartAuthenticationPort()\n");
275 
276  /* Initialize the logon context list */
278 
280  L"\\LsaAuthenticationPort");
281 
283  &PortName,
284  0,
285  NULL,
286  NULL);
287 
290  sizeof(LSA_CONNECTION_INFO),
291  sizeof(LSA_API_MSG),
292  sizeof(LSA_API_MSG) * 32);
293  if (!NT_SUCCESS(Status))
294  {
295  WARN("NtCreatePort() failed (Status %lx)\n", Status);
296  return Status;
297  }
298 
299  RtlInitUnicodeString(&EventName,
300  L"\\SECURITY\\LSA_AUTHENTICATION_INITIALIZED");
302  &EventName,
304  NULL,
305  NULL);
309  if (!NT_SUCCESS(Status))
310  {
311  TRACE("NtOpenEvent failed (Status 0x%08lx)\n", Status);
312 
317  FALSE);
318  if (!NT_SUCCESS(Status))
319  {
320  WARN("NtCreateEvent failed (Status 0x%08lx)\n", Status);
321  return Status;
322  }
323  }
324 
327  if (!NT_SUCCESS(Status))
328  {
329  WARN("NtSetEvent failed (Status 0x%08lx)\n", Status);
330  return Status;
331  }
332 
334  0x1000,
336  NULL,
337  0,
338  &ThreadId);
339 
340 
341  return STATUS_SUCCESS;
342 }
343 
344 /* EOF */
NTSTATUS LsapLogonUser(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authpackage.c:1366
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:88
NTSTATUS StartAuthenticationPort(VOID)
Definition: authport.c:265
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
LONG NTSTATUS
Definition: precomp.h:26
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:603
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:79
NTSTATUS LsapCallAuthenticationPackage(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT LogonContext)
Definition: authpackage.c:545
PORT_MESSAGE h
Definition: lsass.h:176
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
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
#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:112
smooth NULL
Definition: ftsmooth.c:416
struct _REMOTE_PORT_VIEW REMOTE_PORT_VIEW
FORCEINLINE PLIST_ENTRY RemoveHeadList(_Inout_ PLIST_ENTRY ListHead)
Definition: rtlfuncs.h:128
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:585
#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
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
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
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:78
#define ERR(fmt,...)
Definition: debug.h:109
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:155
static NTSTATUS LsapCheckLogonProcess(PLSA_API_MSG RequestMsg, PLSAP_LOGON_CONTEXT *LogonContext)
Definition: authport.c:39
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:179
#define PROCESS_VM_READ
Definition: pstypes.h:153
HANDLE ConnectionHandle
Definition: lsasrv.h:80
struct tagContext Context
Definition: acpixf.h:1012
NTSTATUS NTAPI NtCompleteConnectPort(IN HANDLE PortHandle)
Definition: complete.c:421
#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:4565
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:403
return STATUS_SUCCESS
Definition: btrfs.c:2725
IN PUNICODE_STRING PortName
Definition: conport.c:35
_Out_ PHANDLE EventHandle
Definition: iofuncs.h:855