Implement NTDLL's CSR routines in a compatible way. Fix prototypes, argument count, deprecated functions and new functions. Some functions will not work (or are designed not to work) fully until the new CSR is committed, but they keep the same functionality as the previous versions (no regressions found; tested with cmd, cmt, putty, telnet, ps). Also added NtSecureConnectPort.

svn path=/trunk/; revision=17727
This commit is contained in:
Alex Ionescu 2005-09-07 19:37:28 +00:00
parent 43692c4674
commit b0bac13ce1
16 changed files with 1083 additions and 559 deletions

View file

@ -8,29 +8,53 @@
#define _UMFUNCS_H #define _UMFUNCS_H
/* DEPENDENCIES **************************************************************/ /* DEPENDENCIES **************************************************************/
struct _CSR_API_MESSAGE;
struct _CSR_CAPTURE_BUFFER;
/* PROTOTYPES ****************************************************************/ /* PROTOTYPES ****************************************************************/
/* /*
* CSR Functions * CSR Functions
*/ */
PVOID
NTAPI
CsrAllocateCaptureBuffer(
ULONG ArgumentCount,
ULONG BufferSize
);
ULONG
NTAPI
CsrAllocateMessagePointer(
struct _CSR_CAPTURE_BUFFER *CaptureBuffer,
ULONG MessageLength,
PVOID *CaptureData
);
VOID
NTAPI
CsrCaptureMessageBuffer(
struct _CSR_CAPTURE_BUFFER *CaptureBuffer,
PVOID MessageString,
ULONG StringLength,
PVOID *CapturedData
);
NTSTATUS NTSTATUS
NTAPI NTAPI
CsrClientConnectToServer( CsrClientConnectToServer(
PWSTR ObjectDirectory, PWSTR ObjectDirectory,
ULONG ServerId, ULONG ServerId,
PVOID Unknown, PVOID ConnectionInfo,
PVOID Context, PULONG ConnectionInfoSize,
ULONG ContextLength,
PBOOLEAN ServerToServerCall PBOOLEAN ServerToServerCall
); );
struct _CSR_API_MESSAGE;
NTSTATUS NTSTATUS
NTAPI NTAPI
CsrClientCallServer( CsrClientCallServer(
struct _CSR_API_MESSAGE *Request, struct _CSR_API_MESSAGE *Request,
PVOID CapturedBuffer OPTIONAL, struct _CSR_CAPTURE_BUFFER *CaptureBuffer OPTIONAL,
ULONG ApiNumber, ULONG ApiNumber,
ULONG RequestLength ULONG RequestLength
); );
@ -39,6 +63,10 @@ NTSTATUS
NTAPI NTAPI
CsrIdentifyAlertableThread(VOID); CsrIdentifyAlertableThread(VOID);
VOID
NTAPI
CsrFreeCaptureBuffer(struct _CSR_CAPTURE_BUFFER *CaptureBuffer);
NTSTATUS NTSTATUS
NTAPI NTAPI
CsrNewThread(VOID); CsrNewThread(VOID);
@ -53,7 +81,7 @@ CsrSetPriorityClass(
VOID VOID
NTAPI NTAPI
CsrProbeForRead( CsrProbeForRead(
IN CONST PVOID Address, IN PVOID Address,
IN ULONG Length, IN ULONG Length,
IN ULONG Alignment IN ULONG Alignment
); );
@ -61,24 +89,11 @@ CsrProbeForRead(
VOID VOID
NTAPI NTAPI
CsrProbeForWrite( CsrProbeForWrite(
IN CONST PVOID Address, IN PVOID Address,
IN ULONG Length, IN ULONG Length,
IN ULONG Alignment IN ULONG Alignment
); );
NTSTATUS
NTAPI
CsrCaptureParameterBuffer(
PVOID ParameterBuffer,
ULONG ParameterBufferSize,
PVOID* ClientAddress,
PVOID* ServerAddress
);
NTSTATUS
NTAPI
CsrReleaseParameterBuffer(PVOID ClientAddress);
/* /*
* Debug Functions * Debug Functions
*/ */

View file

@ -3322,6 +3322,37 @@ ZwSaveKeyEx(
IN ULONG Flags IN ULONG Flags
); );
NTSYSCALLAPI
NTSTATUS
NTAPI
NtSecureConnectPort(
PHANDLE PortHandle,
PUNICODE_STRING PortName,
PSECURITY_QUALITY_OF_SERVICE SecurityQos,
PPORT_VIEW ClientView OPTIONAL,
PSID Sid OPTIONAL,
PREMOTE_PORT_VIEW ServerView OPTIONAL,
PULONG MaxMessageLength OPTIONAL,
PVOID ConnectionInformation OPTIONAL,
PULONG ConnectionInformationLength OPTIONAL
);
NTSYSAPI
NTSTATUS
NTAPI
ZwSecureConnectPort(
PHANDLE PortHandle,
PUNICODE_STRING PortName,
PSECURITY_QUALITY_OF_SERVICE SecurityQos,
PPORT_VIEW ClientView OPTIONAL,
PSID Sid OPTIONAL,
PREMOTE_PORT_VIEW ServerView OPTIONAL,
PULONG MaxMessageLength OPTIONAL,
PVOID ConnectionInformation OPTIONAL,
PULONG ConnectionInformationLength OPTIONAL
);
NTSTATUS NTSTATUS
NTAPI NTAPI
NtSetBootEntryOrder( NtSetBootEntryOrder(

View file

@ -484,6 +484,7 @@ typedef struct
typedef struct _CSR_API_MESSAGE typedef struct _CSR_API_MESSAGE
{ {
PORT_MESSAGE Header; PORT_MESSAGE Header;
PVOID CsrCaptureData;
ULONG Type; ULONG Type;
NTSTATUS Status; NTSTATUS Status;
union union
@ -544,4 +545,75 @@ typedef struct _CSR_API_MESSAGE
} Data; } Data;
} CSR_API_MESSAGE, *PCSR_API_MESSAGE; } CSR_API_MESSAGE, *PCSR_API_MESSAGE;
/* Types used in the new CSR. Temporarly here for proper compile of NTDLL */
#define CSR_SRV_SERVER 0
#define CsrSrvClientConnect 0
#define CsrSrvIdentifyAlertableThread 3
#define CsrSrvSetPriorityClass 4
#define CSR_MAKE_OPCODE(s,m) ((s) << 16) | (m)
typedef struct _CSR_CONNECTION_INFO
{
ULONG Version;
ULONG Unknown;
HANDLE ObjectDirectory;
PVOID SharedSectionBase;
PVOID SharedSectionHeap;
PVOID SharedSectionData;
ULONG DebugFlags;
ULONG Unknown2[3];
HANDLE ProcessId;
} CSR_CONNECTION_INFO, *PCSR_CONNECTION_INFO;
typedef struct _CSR_CLIENT_CONNECT
{
ULONG ServerId;
PVOID ConnectionInfo;
ULONG ConnectionInfoSize;
} CSR_CLIENT_CONNECT, *PCSR_CLIENT_CONNECT;
typedef struct _CSR_IDENTIFY_ALTERTABLE_THREAD
{
CLIENT_ID Cid;
} CSR_IDENTIFY_ALTERTABLE_THREAD, *PCSR_IDENTIFY_ALTERTABLE_THREAD;
typedef struct _CSR_SET_PRIORITY_CLASS
{
HANDLE hProcess;
ULONG PriorityClass;
} CSR_SET_PRIORITY_CLASS, *PCSR_SET_PRIORITY_CLASS;
typedef struct _CSR_API_MESSAGE2
{
PORT_MESSAGE Header;
union
{
CSR_CONNECTION_INFO ConnectionInfo;
struct
{
PVOID CsrCaptureData;
CSR_API_NUMBER Opcode;
ULONG Status;
ULONG Reserved;
union
{
CSR_CLIENT_CONNECT ClientConnect;
CSR_SET_PRIORITY_CLASS SetPriorityClass;
CSR_IDENTIFY_ALTERTABLE_THREAD IdentifyAlertableThread;
};
};
};
} CSR_API_MESSAGE2, *PCSR_API_MESSAGE2;
typedef struct _CSR_CAPTURE_BUFFER
{
ULONG Size;
struct _CSR_CAPTURE_BUFFER *PreviousCaptureBuffer;
ULONG PointerCount;
ULONG_PTR BufferEnd;
ULONG_PTR PointerArray[1];
} CSR_CAPTURE_BUFFER, *PCSR_CAPTURE_BUFFER;
#endif /* __INCLUDE_CSRSS_CSRSS_H */ #endif /* __INCLUDE_CSRSS_CSRSS_H */

View file

@ -1546,8 +1546,8 @@ IntPeekConsoleInput(HANDLE hConsoleInput,
BOOL bUnicode) BOOL bUnicode)
{ {
CSR_API_MESSAGE Request; ULONG CsrRequest; CSR_API_MESSAGE Request; ULONG CsrRequest;
PCSR_CAPTURE_BUFFER CaptureBuffer;
NTSTATUS Status; NTSTATUS Status;
PVOID BufferBase;
PVOID BufferTargetBase; PVOID BufferTargetBase;
ULONG Size; ULONG Size;
@ -1559,40 +1559,55 @@ IntPeekConsoleInput(HANDLE hConsoleInput,
Size = nLength * sizeof(INPUT_RECORD); Size = nLength * sizeof(INPUT_RECORD);
Status = CsrCaptureParameterBuffer(NULL, Size, &BufferBase, &BufferTargetBase); /* Allocate a Capture Buffer */
if(!NT_SUCCESS(Status)) DPRINT1("IntPeekConsoleInput: %lx %p\n", Size, lpNumberOfEventsRead);
{ CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
SetLastErrorByStatus(Status);
return FALSE;
}
/* Allocate space in the Buffer */
CsrCaptureMessageBuffer(CaptureBuffer, NULL, Size, &BufferTargetBase);
DPRINT1("Allocated message buffer: %p %p\n", CaptureBuffer, BufferTargetBase);
/* Set up the data to send to the Console Server */
CsrRequest = MAKE_CSR_API(PEEK_CONSOLE_INPUT, CSR_CONSOLE); CsrRequest = MAKE_CSR_API(PEEK_CONSOLE_INPUT, CSR_CONSOLE);
Request.Data.PeekConsoleInputRequest.ConsoleHandle = hConsoleInput; Request.Data.PeekConsoleInputRequest.ConsoleHandle = hConsoleInput;
Request.Data.PeekConsoleInputRequest.Unicode = bUnicode; Request.Data.PeekConsoleInputRequest.Unicode = bUnicode;
Request.Data.PeekConsoleInputRequest.Length = nLength; Request.Data.PeekConsoleInputRequest.Length = nLength;
Request.Data.PeekConsoleInputRequest.InputRecord = (INPUT_RECORD*)BufferTargetBase; Request.Data.PeekConsoleInputRequest.InputRecord = (INPUT_RECORD*)BufferTargetBase;
/* Call the server */
DPRINT1("Calling Server\n");
Status = CsrClientCallServer(&Request, Status = CsrClientCallServer(&Request,
NULL, CaptureBuffer,
CsrRequest, CsrRequest,
sizeof(CSR_API_MESSAGE)); sizeof(CSR_API_MESSAGE));
DPRINT1("Server returned: %x\n", Request.Status);
if(!NT_SUCCESS(Status) || !NT_SUCCESS(Status = Request.Status)) /* Check for success*/
{ if (NT_SUCCESS(Request.Status))
CsrReleaseParameterBuffer(BufferBase);
return FALSE;
}
memcpy(lpBuffer, BufferBase, sizeof(INPUT_RECORD) * Request.Data.PeekConsoleInputRequest.Length);
CsrReleaseParameterBuffer(BufferBase);
if(lpNumberOfEventsRead != NULL)
{ {
/* Return the number of events read */
DPRINT1("Events read: %lx\n", Request.Data.PeekConsoleInputRequest.Length);
*lpNumberOfEventsRead = Request.Data.PeekConsoleInputRequest.Length; *lpNumberOfEventsRead = Request.Data.PeekConsoleInputRequest.Length;
/* Copy into the buffer */
DPRINT1("Copying to buffer\n");
RtlCopyMemory(lpBuffer,
Request.Data.PeekConsoleInputRequest.InputRecord,
sizeof(INPUT_RECORD) * *lpNumberOfEventsRead);
}
else
{
/* Error out */
*lpNumberOfEventsRead = 0;
SetLastErrorByStatus(Request.Status);
} }
return TRUE; /* Release the capture buffer */
DPRINT1("Release buffer and return\n");
CsrFreeCaptureBuffer(CaptureBuffer);
/* Return TRUE or FALSE */
return NT_SUCCESS(Request.Status);
} }
/*-------------------------------------------------------------- /*--------------------------------------------------------------
@ -1751,8 +1766,8 @@ IntWriteConsoleInput(HANDLE hConsoleInput,
BOOL bUnicode) BOOL bUnicode)
{ {
CSR_API_MESSAGE Request; ULONG CsrRequest; CSR_API_MESSAGE Request; ULONG CsrRequest;
PCSR_CAPTURE_BUFFER CaptureBuffer;
PVOID BufferBase, BufferTargetBase; PVOID BufferTargetBase;
NTSTATUS Status; NTSTATUS Status;
DWORD Size; DWORD Size;
@ -1764,37 +1779,55 @@ IntWriteConsoleInput(HANDLE hConsoleInput,
Size = nLength * sizeof(INPUT_RECORD); Size = nLength * sizeof(INPUT_RECORD);
Status = CsrCaptureParameterBuffer((PVOID)lpBuffer, Size, &BufferBase, &BufferTargetBase); /* Allocate a Capture Buffer */
if(!NT_SUCCESS(Status)) DPRINT1("IntWriteConsoleInput: %lx %p\n", Size, lpNumberOfEventsWritten);
{ CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
SetLastErrorByStatus(Status);
return FALSE;
}
/* Allocate space in the Buffer */
CsrCaptureMessageBuffer(CaptureBuffer, NULL, Size, &BufferTargetBase);
DPRINT1("Allocated message buffer: %p %p\n", CaptureBuffer, BufferTargetBase);
/* Set up the data to send to the Console Server */
CsrRequest = MAKE_CSR_API(WRITE_CONSOLE_INPUT, CSR_CONSOLE); CsrRequest = MAKE_CSR_API(WRITE_CONSOLE_INPUT, CSR_CONSOLE);
Request.Data.WriteConsoleInputRequest.ConsoleHandle = hConsoleInput; Request.Data.WriteConsoleInputRequest.ConsoleHandle = hConsoleInput;
Request.Data.WriteConsoleInputRequest.Unicode = bUnicode; Request.Data.WriteConsoleInputRequest.Unicode = bUnicode;
Request.Data.WriteConsoleInputRequest.Length = nLength; Request.Data.WriteConsoleInputRequest.Length = nLength;
Request.Data.WriteConsoleInputRequest.InputRecord = (PINPUT_RECORD)BufferTargetBase; Request.Data.WriteConsoleInputRequest.InputRecord = (PINPUT_RECORD)BufferTargetBase;
Status = CsrClientCallServer(&Request, NULL, /* Call the server */
DPRINT1("Calling Server\n");
Status = CsrClientCallServer(&Request,
CaptureBuffer,
CsrRequest, CsrRequest,
sizeof(CSR_API_MESSAGE)); sizeof(CSR_API_MESSAGE));
DPRINT1("Server returned: %x\n", Request.Status);
CsrReleaseParameterBuffer(BufferBase); /* Check for success*/
if (NT_SUCCESS(Request.Status))
if(!NT_SUCCESS(Status) || !NT_SUCCESS(Status = Request.Status))
{
SetLastErrorByStatus(Status);
return FALSE;
}
if(lpNumberOfEventsWritten != NULL)
{ {
/* Return the number of events read */
DPRINT1("Events read: %lx\n", Request.Data.WriteConsoleInputRequest.Length);
*lpNumberOfEventsWritten = Request.Data.WriteConsoleInputRequest.Length; *lpNumberOfEventsWritten = Request.Data.WriteConsoleInputRequest.Length;
/* Copy into the buffer */
DPRINT1("Copying to buffer\n");
RtlCopyMemory(lpBuffer,
Request.Data.WriteConsoleInputRequest.InputRecord,
sizeof(INPUT_RECORD) * *lpNumberOfEventsWritten);
}
else
{
/* Error out */
*lpNumberOfEventsWritten = 0;
SetLastErrorByStatus(Request.Status);
} }
return TRUE; /* Release the capture buffer */
DPRINT1("Release buffer and return\n");
CsrFreeCaptureBuffer(CaptureBuffer);
/* Return TRUE or FALSE */
return NT_SUCCESS(Request.Status);
} }
@ -1851,8 +1884,7 @@ IntReadConsoleOutput(HANDLE hConsoleOutput,
BOOL bUnicode) BOOL bUnicode)
{ {
CSR_API_MESSAGE Request; ULONG CsrRequest; CSR_API_MESSAGE Request; ULONG CsrRequest;
PCSR_CAPTURE_BUFFER CaptureBuffer;
PVOID BufferBase;
PVOID BufferTargetBase; PVOID BufferTargetBase;
NTSTATUS Status; NTSTATUS Status;
DWORD Size, SizeX, SizeY; DWORD Size, SizeX, SizeY;
@ -1865,13 +1897,15 @@ IntReadConsoleOutput(HANDLE hConsoleOutput,
Size = dwBufferSize.X * dwBufferSize.Y * sizeof(CHAR_INFO); Size = dwBufferSize.X * dwBufferSize.Y * sizeof(CHAR_INFO);
Status = CsrCaptureParameterBuffer(NULL, Size, &BufferBase, &BufferTargetBase); /* Allocate a Capture Buffer */
if(!NT_SUCCESS(Status)) DPRINT1("IntReadConsoleOutput: %lx %p\n", Size, lpReadRegion);
{ CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
SetLastErrorByStatus(Status);
return FALSE;
}
/* Allocate space in the Buffer */
CsrCaptureMessageBuffer(CaptureBuffer, NULL, Size, &BufferTargetBase);
DPRINT1("Allocated message buffer: %p %p\n", CaptureBuffer, BufferTargetBase);
/* Set up the data to send to the Console Server */
CsrRequest = MAKE_CSR_API(READ_CONSOLE_OUTPUT, CSR_CONSOLE); CsrRequest = MAKE_CSR_API(READ_CONSOLE_OUTPUT, CSR_CONSOLE);
Request.Data.ReadConsoleOutputRequest.ConsoleHandle = hConsoleOutput; Request.Data.ReadConsoleOutputRequest.ConsoleHandle = hConsoleOutput;
Request.Data.ReadConsoleOutputRequest.Unicode = bUnicode; Request.Data.ReadConsoleOutputRequest.Unicode = bUnicode;
@ -1880,28 +1914,43 @@ IntReadConsoleOutput(HANDLE hConsoleOutput,
Request.Data.ReadConsoleOutputRequest.ReadRegion = *lpReadRegion; Request.Data.ReadConsoleOutputRequest.ReadRegion = *lpReadRegion;
Request.Data.ReadConsoleOutputRequest.CharInfo = (PCHAR_INFO)BufferTargetBase; Request.Data.ReadConsoleOutputRequest.CharInfo = (PCHAR_INFO)BufferTargetBase;
/* Call the server */
DPRINT1("Calling Server\n");
Status = CsrClientCallServer(&Request, Status = CsrClientCallServer(&Request,
NULL, CaptureBuffer,
CsrRequest, CsrRequest,
sizeof(CSR_API_MESSAGE)); sizeof(CSR_API_MESSAGE));
DPRINT1("Server returned: %x\n", Request.Status);
if(!NT_SUCCESS(Status) || !NT_SUCCESS(Status = Request.Status)) /* Check for success*/
if (NT_SUCCESS(Request.Status))
{ {
SetLastErrorByStatus(Status); /* Copy into the buffer */
CsrReleaseParameterBuffer(BufferBase); DPRINT1("Copying to buffer\n");
return FALSE; SizeX = Request.Data.ReadConsoleOutputRequest.ReadRegion.Right -
Request.Data.ReadConsoleOutputRequest.ReadRegion.Left + 1;
SizeY = Request.Data.ReadConsoleOutputRequest.ReadRegion.Bottom -
Request.Data.ReadConsoleOutputRequest.ReadRegion.Top + 1;
RtlCopyMemory(lpBuffer,
Request.Data.ReadConsoleOutputRequest.CharInfo,
sizeof(CHAR_INFO) * SizeX * SizeY);
}
else
{
/* Error out */
SetLastErrorByStatus(Request.Status);
} }
SizeX = Request.Data.ReadConsoleOutputRequest.ReadRegion.Right - Request.Data.ReadConsoleOutputRequest.ReadRegion.Left + 1; /* Return the read region */
SizeY = Request.Data.ReadConsoleOutputRequest.ReadRegion.Bottom - Request.Data.ReadConsoleOutputRequest.ReadRegion.Top + 1; DPRINT1("read region: %lx\n", Request.Data.ReadConsoleOutputRequest.ReadRegion);
memcpy(lpBuffer, BufferBase, sizeof(CHAR_INFO) * SizeX * SizeY);
CsrReleaseParameterBuffer(BufferBase);
*lpReadRegion = Request.Data.ReadConsoleOutputRequest.ReadRegion; *lpReadRegion = Request.Data.ReadConsoleOutputRequest.ReadRegion;
return TRUE; /* Release the capture buffer */
DPRINT1("Release buffer and return\n");
CsrFreeCaptureBuffer(CaptureBuffer);
/* Return TRUE or FALSE */
return NT_SUCCESS(Request.Status);
} }
/*-------------------------------------------------------------- /*--------------------------------------------------------------
@ -1953,49 +2002,59 @@ IntWriteConsoleOutput(HANDLE hConsoleOutput,
BOOL bUnicode) BOOL bUnicode)
{ {
CSR_API_MESSAGE Request; ULONG CsrRequest; CSR_API_MESSAGE Request; ULONG CsrRequest;
PCSR_CAPTURE_BUFFER CaptureBuffer;
NTSTATUS Status; NTSTATUS Status;
ULONG Size; ULONG Size;
PVOID BufferBase;
PVOID BufferTargetBase; PVOID BufferTargetBase;
Size = dwBufferSize.Y * dwBufferSize.X * sizeof(CHAR_INFO); Size = dwBufferSize.Y * dwBufferSize.X * sizeof(CHAR_INFO);
Status = CsrCaptureParameterBuffer((PVOID)lpBuffer, /* Allocate a Capture Buffer */
Size, DPRINT1("IntWriteConsoleOutput: %lx %p\n", Size, lpWriteRegion);
&BufferBase, CaptureBuffer = CsrAllocateCaptureBuffer(1, Size);
&BufferTargetBase);
if (!NT_SUCCESS(Status))
{
SetLastErrorByStatus(Status);
return(FALSE);
}
/* Allocate space in the Buffer */
CsrCaptureMessageBuffer(CaptureBuffer, NULL, Size, &BufferTargetBase);
DPRINT1("Allocated message buffer: %p %p\n", CaptureBuffer, BufferTargetBase);
/* Copy from the buffer */
DPRINT1("Copying into buffer\n");
RtlCopyMemory(BufferTargetBase, lpBuffer, Size);
/* Set up the data to send to the Console Server */
CsrRequest = MAKE_CSR_API(WRITE_CONSOLE_OUTPUT, CSR_CONSOLE); CsrRequest = MAKE_CSR_API(WRITE_CONSOLE_OUTPUT, CSR_CONSOLE);
Request.Data.WriteConsoleOutputRequest.ConsoleHandle = hConsoleOutput; Request.Data.WriteConsoleOutputRequest.ConsoleHandle = hConsoleOutput;
Request.Data.WriteConsoleOutputRequest.Unicode = bUnicode; Request.Data.WriteConsoleOutputRequest.Unicode = bUnicode;
Request.Data.WriteConsoleOutputRequest.BufferSize = dwBufferSize; Request.Data.WriteConsoleOutputRequest.BufferSize = dwBufferSize;
Request.Data.WriteConsoleOutputRequest.BufferCoord = dwBufferCoord; Request.Data.WriteConsoleOutputRequest.BufferCoord = dwBufferCoord;
Request.Data.WriteConsoleOutputRequest.WriteRegion = *lpWriteRegion; Request.Data.WriteConsoleOutputRequest.WriteRegion = *lpWriteRegion;
Request.Data.WriteConsoleOutputRequest.CharInfo = Request.Data.WriteConsoleOutputRequest.CharInfo = (CHAR_INFO*)BufferTargetBase;
(CHAR_INFO*)BufferTargetBase;
/* Call the server */
DPRINT1("Calling Server\n");
Status = CsrClientCallServer(&Request, Status = CsrClientCallServer(&Request,
NULL, CaptureBuffer,
CsrRequest, CsrRequest,
sizeof(CSR_API_MESSAGE)); sizeof(CSR_API_MESSAGE));
DPRINT1("Server returned: %x\n", Request.Status);
if (!NT_SUCCESS(Status) || !NT_SUCCESS(Status = Request.Status)) /* Check for success*/
{ if (!NT_SUCCESS(Request.Status))
CsrReleaseParameterBuffer(BufferBase); {
SetLastErrorByStatus(Status); /* Error out */
return FALSE; SetLastErrorByStatus(Request.Status);
} }
CsrReleaseParameterBuffer(BufferBase);
/* Return the read region */
DPRINT1("read region: %lx\n", Request.Data.WriteConsoleOutputRequest.WriteRegion);
*lpWriteRegion = Request.Data.WriteConsoleOutputRequest.WriteRegion; *lpWriteRegion = Request.Data.WriteConsoleOutputRequest.WriteRegion;
return(TRUE); /* Release the capture buffer */
DPRINT1("Release buffer and return\n");
CsrFreeCaptureBuffer(CaptureBuffer);
/* Return TRUE or FALSE */
return NT_SUCCESS(Request.Status);
} }
/*-------------------------------------------------------------- /*--------------------------------------------------------------

View file

@ -16,6 +16,8 @@
#define NDEBUG #define NDEBUG
#include "../include/debug.h" #include "../include/debug.h"
#define CSR_BASE_DLL 0 // <- This should be 1 when CSR gets committed
/* GLOBALS *******************************************************************/ /* GLOBALS *******************************************************************/
extern UNICODE_STRING SystemDirectory; extern UNICODE_STRING SystemDirectory;
@ -198,41 +200,46 @@ BasepInitConsole(VOID)
} }
BOOL STDCALL BOOL
STDCALL
DllMain(HANDLE hDll, DllMain(HANDLE hDll,
DWORD dwReason, DWORD dwReason,
LPVOID lpReserved) LPVOID lpReserved)
{ {
NTSTATUS Status; NTSTATUS Status;
BOOLEAN IsServer;
ULONG Dummy;
ULONG DummySize = sizeof(Dummy);
(void)lpReserved; DPRINT("DllMain(hInst %lx, dwReason %lu)\n",
hDll, dwReason);
DPRINT("DllMain(hInst %lx, dwReason %lu)\n", switch (dwReason)
hDll, dwReason);
switch (dwReason)
{ {
case DLL_PROCESS_ATTACH: case DLL_PROCESS_ATTACH:
DPRINT("DLL_PROCESS_ATTACH\n");
LdrDisableThreadCalloutsForDll ((PVOID)hDll); /* Don't bother us for each thread */
LdrDisableThreadCalloutsForDll((PVOID)hDll);
/* /* Connect to the base server */
* Connect to the csrss server Status = CsrClientConnectToServer(L"\\Windows", // <- FIXME: SessionDir
*/ CSR_BASE_DLL,
Status = CsrClientConnectToServer(L"\\Windows\\ApiPort", &Dummy,
0, &DummySize,
NULL, &IsServer);
NULL, if (!NT_SUCCESS(Status))
0, {
NULL); DPRINT1("Failed to connect to CSR (Status %lx)\n", Status);
if (!NT_SUCCESS(Status)) ZwTerminateProcess(NtCurrentProcess(), Status);
{ return FALSE;
DbgPrint("Failed to connect to csrss.exe (Status %lx)\n", }
Status);
ZwTerminateProcess(NtCurrentProcess(), Status); /* Check if we are running a CSR Server */
return FALSE; if (!IsServer)
} {
/* Set the termination port for the thread */
CsrNewThread();
}
hProcessHeap = RtlGetProcessHeap(); hProcessHeap = RtlGetProcessHeap();
hCurrentModule = hDll; hCurrentModule = hDll;

View file

@ -0,0 +1,89 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
* FILE: lib/ntdll/csr/api.c
* PURPOSE: CSR APIs exported through NTDLL
* PROGRAMMER: Alex Ionescu (alex@relsoft.net)
*/
/* INCLUDES *****************************************************************/
#include <ntdll.h>
#define NDEBUG
#include <debug.h>
/* GLOBALS *******************************************************************/
extern HANDLE CsrApiPort;
/* FUNCTIONS *****************************************************************/
/*
* @implemented
*/
NTSTATUS
NTAPI
CsrNewThread(VOID)
{
/* Register the termination port to CSR's */
return NtRegisterThreadTerminatePort(CsrApiPort);
}
/*
* @implemented
*/
NTSTATUS
NTAPI
CsrSetPriorityClass(HANDLE hProcess,
PULONG PriorityClass)
{
NTSTATUS Status;
CSR_API_MESSAGE2 ApiMessage; /* <- Remove the "2" when CSR is commited */
PCSR_SET_PRIORITY_CLASS SetPriorityClass = &ApiMessage.SetPriorityClass;
/* Set up the data for CSR */
DbgBreakPoint();
SetPriorityClass->hProcess = hProcess;
SetPriorityClass->PriorityClass = *PriorityClass;
/* Call it */
Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
NULL,
CSR_MAKE_OPCODE(CsrSrvSetPriorityClass,
CSR_SRV_SERVER),
sizeof(CSR_SET_PRIORITY_CLASS));
/* Return what we got, if requested */
if (*PriorityClass) *PriorityClass = SetPriorityClass->PriorityClass;
/* Return to caller */
return Status;
}
/*
* @implemented
*/
NTSTATUS
NTAPI
CsrIdentifyAlertableThread (VOID)
{
NTSTATUS Status;
CSR_API_MESSAGE2 ApiMessage; /* <- Remove the "2" when CSR is commited */
PCSR_IDENTIFY_ALTERTABLE_THREAD IdentifyAlertableThread;
/* Set up the data for CSR */
DbgBreakPoint();
IdentifyAlertableThread = &ApiMessage.IdentifyAlertableThread;
IdentifyAlertableThread->Cid = NtCurrentTeb()->Cid;
/* Call it */
Status = CsrClientCallServer((PCSR_API_MESSAGE)&ApiMessage,
NULL,
CSR_MAKE_OPCODE(CsrSrvIdentifyAlertableThread,
CSR_SRV_SERVER),
sizeof(CSR_SET_PRIORITY_CLASS));
/* Return to caller */
return Status;
}
/* EOF */

View file

@ -1,9 +1,9 @@
/* $Id$ /*
*
* COPYRIGHT: See COPYING in the top level directory * COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel * PROJECT: ReactOS kernel
* FILE: lib/ntdll/csr/capture.c * FILE: lib/ntdll/csr/capture.c
* PURPOSE: CSRSS Capture API * PURPOSE: routines for probing and capturing CSR API Messages
* PROGRAMMER: Alex Ionescu (alex@relsoft.net)
*/ */
/* INCLUDES *****************************************************************/ /* INCLUDES *****************************************************************/
@ -13,90 +13,255 @@
#include <debug.h> #include <debug.h>
/* GLOBALS *******************************************************************/ /* GLOBALS *******************************************************************/
extern HANDLE CsrPortHeap;
static HANDLE hCaptureHeap = INVALID_HANDLE_VALUE; /* FIXME: use the general NTDLL heap */
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
/* /*
* @unimplemented * @implemented
*/ */
PVOID VOID
STDCALL CsrAllocateCaptureBuffer ( NTAPI
DWORD Unknown0, CsrProbeForRead(IN PVOID Address,
DWORD Unknown1, IN ULONG Length,
DWORD Unknown2 IN ULONG Alignment)
)
{ {
/* FIXME: implement it! */ PUCHAR Pointer;
return NULL; UCHAR Data;
}
/* /* Validate length */
* @unimplemented if (Length == 0) return;
*/
VOID STDCALL
CsrCaptureMessageString (DWORD Unknown0,
DWORD Unknown1,
DWORD Unknown2,
DWORD Unknown3,
DWORD Unknown4)
{
}
/* /* Validate alignment */
* @unimplemented if ((ULONG_PTR)Address & (Alignment - 1))
*/ {
VOID STDCALL /* Raise exception if it doesn't match */
CsrAllocateCapturePointer(ULONG Unknown0, RtlRaiseStatus(STATUS_DATATYPE_MISALIGNMENT);
ULONG Unknown1, }
ULONG Unknown2)
{
} /* Do the probe */
Pointer = (PUCHAR)Address;
/* Data = *Pointer;
* @unimplemented Pointer = (PUCHAR)((ULONG)Address + Length -1);
*/ Data = *Pointer;
VOID STDCALL CsrAllocateMessagePointer (DWORD Unknown0,
DWORD Unknown1,
DWORD Unknown2)
{
}
/*
* @unimplemented
*/
VOID STDCALL
CsrCaptureMessageBuffer(ULONG Unknown0,
ULONG Unknown1,
ULONG Unknown2,
ULONG Unknown3)
{
}
/*
* @unimplemented
*/
BOOLEAN STDCALL CsrFreeCaptureBuffer (PVOID CaptureBuffer)
{
/* FIXME: use NTDLL own heap */
return RtlFreeHeap (hCaptureHeap, 0, CaptureBuffer);
} }
/* /*
* @implemented * @implemented
*/ */
PLARGE_INTEGER STDCALL VOID
CsrCaptureTimeout(LONG Milliseconds, NTAPI
PLARGE_INTEGER Timeout) CsrProbeForWrite(IN PVOID Address,
IN ULONG Length,
IN ULONG Alignment)
{ {
if (Milliseconds == -1) PUCHAR Pointer;
return NULL; UCHAR Data;
Timeout->QuadPart = Milliseconds * -100000; /* Validate length */
return Timeout; if (Length == 0) return;
/* Validate alignment */
if ((ULONG_PTR)Address & (Alignment - 1))
{
/* Raise exception if it doesn't match */
RtlRaiseStatus(STATUS_DATATYPE_MISALIGNMENT);
}
/* Do the probe */
Pointer = (PUCHAR)Address;
Data = *Pointer;
*Pointer = Data;
Pointer = (PUCHAR)((ULONG)Address + Length -1);
Data = *Pointer;
*Pointer = Data;
}
/*
* @implemented
*/
PVOID
NTAPI
CsrAllocateCaptureBuffer(ULONG ArgumentCount,
ULONG BufferSize)
{
PCSR_CAPTURE_BUFFER CaptureBuffer;
/* Validate size */
if (BufferSize >= MAXLONG) return NULL;
/* Add the size of the header and for each pointer to the pointers */
BufferSize += sizeof(CSR_CAPTURE_BUFFER) + (ArgumentCount * sizeof(PVOID));
/* Allocate memory from the port heap */
CaptureBuffer = RtlAllocateHeap(CsrPortHeap, 0, BufferSize);
/* Initialize the header */
CaptureBuffer->Size = BufferSize;
CaptureBuffer->PointerCount = 0;
/* Initialize all the pointers */
RtlZeroMemory(CaptureBuffer->PointerArray,
ArgumentCount * sizeof(ULONG_PTR));
/* Point the start of the free buffer */
CaptureBuffer->BufferEnd = (ULONG_PTR)CaptureBuffer->PointerArray +
ArgumentCount * sizeof(ULONG_PTR);
/* Return the address of the buffer */
return CaptureBuffer;
}
/*
* @implemented
*/
ULONG
NTAPI
CsrAllocateMessagePointer(PCSR_CAPTURE_BUFFER CaptureBuffer,
ULONG MessageLength,
PVOID *CaptureData)
{
/* If there's no data, our job is easy. */
if (MessageLength == 0)
{
*CaptureData = NULL;
CaptureData = NULL;
}
else
{
/* Set the capture data at our current available buffer */
*CaptureData = (PVOID)CaptureBuffer->BufferEnd;
/* Validate the size */
if (MessageLength >= MAXLONG) return 0;
/* Align it to a 4-byte boundary */
MessageLength = (MessageLength + 3) & ~3;
/* Move our available buffer beyond this space */
CaptureBuffer->BufferEnd += MessageLength;
}
/* Increase the pointer count */
CaptureBuffer->PointerCount++;
/* Write down this pointer in the array */
CaptureBuffer->PointerArray[CaptureBuffer->PointerCount] = (ULONG_PTR)CaptureData;
/* Return the aligned length */
return MessageLength;
}
/*
* @implemented
*/
VOID
NTAPI
CsrCaptureMessageBuffer(PCSR_CAPTURE_BUFFER CaptureBuffer,
PVOID MessageString,
ULONG StringLength,
PVOID *CapturedData)
{
/* Simply allocate a message pointer in the buffer */
CsrAllocateMessagePointer(CaptureBuffer, StringLength, CapturedData);
/* Check if there was any data */
if (!MessageString || !StringLength) return;
/* Copy the data into the buffer */
RtlMoveMemory(*CapturedData, MessageString, StringLength);
}
/*
* @implemented
*/
VOID
NTAPI
CsrFreeCaptureBuffer(PCSR_CAPTURE_BUFFER CaptureBuffer)
{
/* Free it from the heap */
RtlFreeHeap(CsrPortHeap, 0, CaptureBuffer);
}
/*
* @implemented
*/
NTSTATUS
NTAPI
CsrCaptureMessageMultiUnicodeStringsInPlace(IN PCSR_CAPTURE_BUFFER *CaptureBuffer,
IN ULONG MessageCount,
IN PVOID MessageStrings)
{
/* FIXME: allocate a buffer if we don't have one, and return it */
/* FIXME: call CsrCaptureMessageUnicodeStringInPlace for each string */
UNIMPLEMENTED;
return STATUS_NOT_IMPLEMENTED;
}
/*
* @implemented
*/
VOID
NTAPI
CsrCaptureMessageString(PCSR_CAPTURE_BUFFER CaptureBuffer,
LPSTR String,
IN ULONG StringLength,
IN ULONG MaximumLength,
OUT PANSI_STRING CapturedString)
{
ULONG ReturnedLength;
/* If we don't have a string, initialize an empty one */
if (!String)
{
CapturedString->Length = 0;
CapturedString->MaximumLength = MaximumLength;
/* Allocate a pointer for it */
CsrAllocateMessagePointer(CaptureBuffer,
MaximumLength,
(PVOID*)&CapturedString->Buffer);
return;
}
/* Initialize this string */
CapturedString->Length = StringLength;
/* Allocate a buffer and get its size */
ReturnedLength = CsrAllocateMessagePointer(CaptureBuffer,
MaximumLength,
(PVOID*)&CapturedString->Buffer);
CapturedString->MaximumLength = ReturnedLength;
/* If the string had data */
if (StringLength)
{
/* Copy it into the capture buffer */
RtlMoveMemory(CapturedString->Buffer, String, MaximumLength);
/* If we don't take up the whole space */
if (CapturedString->Length < CapturedString->MaximumLength)
{
/* Null-terminate it */
CapturedString->Buffer[CapturedString->Length] = '\0';
}
}
}
/*
* @implemented
*/
PLARGE_INTEGER
NTAPI
CsrCaptureTimeout(LONG Milliseconds,
PLARGE_INTEGER Timeout)
{
/* Validate the time */
if (Milliseconds == -1) return NULL;
/* Convert to relative ticks */
Timeout->QuadPart = Milliseconds * -100000;
return Timeout;
} }
/* EOF */ /* EOF */

View file

@ -0,0 +1,430 @@
/*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
* FILE: lib/ntdll/csr/connect.c
* PURPOSE: Routines for connecting and calling CSR
* PROGRAMMER: Alex Ionescu (alex@relsoft.net)
*/
/* INCLUDES *****************************************************************/
#include <ntdll.h>
#define NDEBUG
#include <debug.h>
/* GLOBALS *******************************************************************/
HANDLE CsrApiPort;
HANDLE CsrProcessId;
HANDLE CsrPortHeap;
ULONG_PTR CsrPortMemoryDelta;
BOOLEAN InsideCsrProcess = FALSE;
BOOLEAN UsingOldCsr = TRUE;
typedef NTSTATUS
(NTAPI *PCSR_SERVER_API_ROUTINE)(IN PPORT_MESSAGE Request,
IN PPORT_MESSAGE Reply);
PCSR_SERVER_API_ROUTINE CsrServerApiRoutine;
#define UNICODE_PATH_SEP L"\\"
#define CSR_PORT_NAME L"ApiPort"
/* FUNCTIONS *****************************************************************/
/*
* @implemented
*/
HANDLE
NTAPI
CsrGetProcessId(VOID)
{
return CsrProcessId;
}
/*
* @implemented
*/
NTSTATUS
NTAPI
CsrClientCallServer(PCSR_API_MESSAGE ApiMessage,
PCSR_CAPTURE_BUFFER CaptureBuffer OPTIONAL,
CSR_API_NUMBER ApiNumber,
ULONG RequestLength)
{
NTSTATUS Status;
ULONG PointerCount;
PULONG_PTR Pointers;
ULONG_PTR CurrentPointer;
DPRINT("CsrClientCallServer\n");
/* Fill out the Port Message Header */
ApiMessage->Header.u1.s1.DataLength = RequestLength - sizeof(PORT_MESSAGE);
ApiMessage->Header.u1.s1.TotalLength = RequestLength;
/* Fill out the CSR Header */
ApiMessage->Type = ApiNumber;
//ApiMessage->Opcode = ApiNumber; <- Activate with new CSR
ApiMessage->CsrCaptureData = NULL;
DPRINT("API: %x, u1.s1.DataLength: %x, u1.s1.TotalLength: %x\n",
ApiNumber,
ApiMessage->Header.u1.s1.DataLength,
ApiMessage->Header.u1.s1.TotalLength);
/* Check if we are already inside a CSR Server */
if (!InsideCsrProcess)
{
/* Check if we got a a Capture Buffer */
if (CaptureBuffer)
{
/* We have to convert from our local view to the remote view */
DPRINT1("Converting CaptureBuffer\n");
ApiMessage->CsrCaptureData = (PVOID)((ULONG_PTR)CaptureBuffer +
CsrPortMemoryDelta);
/* Lock the buffer */
CaptureBuffer->BufferEnd = 0;
/* Get the pointer information */
PointerCount = CaptureBuffer->PointerCount;
Pointers = CaptureBuffer->PointerArray;
/* Loop through every pointer and convert it */
while (PointerCount--)
{
/* Get this pointer and check if it's valid */
if ((CurrentPointer = *Pointers++))
{
/* Update it */
*(PULONG_PTR)CurrentPointer += CsrPortMemoryDelta;
Pointers[-1] = CurrentPointer - (ULONG_PTR)ApiMessage;
}
}
}
/* Send the LPC Message */
Status = NtRequestWaitReplyPort(CsrApiPort,
&ApiMessage->Header,
&ApiMessage->Header);
/* Check if we got a a Capture Buffer */
if (CaptureBuffer)
{
/* We have to convert from the remote view to our remote view */
DPRINT1("Reconverting CaptureBuffer\n");
ApiMessage->CsrCaptureData = (PVOID)((ULONG_PTR)
ApiMessage->CsrCaptureData -
CsrPortMemoryDelta);
/* Get the pointer information */
PointerCount = CaptureBuffer->PointerCount;
Pointers = CaptureBuffer->PointerArray;
/* Loop through every pointer and convert it */
while (PointerCount--)
{
/* Get this pointer and check if it's valid */
if ((CurrentPointer = *Pointers++))
{
/* Update it */
CurrentPointer += (ULONG_PTR)ApiMessage;
Pointers[-1] = CurrentPointer;
*(PULONG_PTR)CurrentPointer -= CsrPortMemoryDelta;
}
}
}
/* Check for success */
if (!NT_SUCCESS(Status))
{
/* We failed. Overwrite the return value with the failure */
DPRINT1("LPC Failed: %lx\n", Status);
ApiMessage->Status = Status;
}
}
else
{
/* This is a server-to-server call. Save our CID and do a direct call */
DbgBreakPoint();
ApiMessage->Header.ClientId = NtCurrentTeb()->Cid;
Status = CsrServerApiRoutine(&ApiMessage->Header,
&ApiMessage->Header);
/* Check for success */
if (!NT_SUCCESS(Status))
{
/* We failed. Overwrite the return value with the failure */
ApiMessage->Status = Status;
}
}
/* Return the CSR Result */
DPRINT("Got back: %x\n", ApiMessage->Status);
return ApiMessage->Status;
}
NTSTATUS
NTAPI
CsrConnectToServer(IN PWSTR ObjectDirectory)
{
ULONG PortNameLength;
UNICODE_STRING PortName;
LARGE_INTEGER CsrSectionViewSize;
NTSTATUS Status;
HANDLE CsrSectionHandle;
PORT_VIEW LpcWrite;
REMOTE_PORT_VIEW LpcRead;
SECURITY_QUALITY_OF_SERVICE SecurityQos;
SID_IDENTIFIER_AUTHORITY NtSidAuthority = {SECURITY_NT_AUTHORITY};
PSID SystemSid = NULL;
CSR_CONNECTION_INFO ConnectionInfo;
ULONG ConnectionInfoLength = sizeof(CSR_CONNECTION_INFO);
DPRINT("CsrConnectToServer\n");
/* Calculate the total port name size */
PortNameLength = ((wcslen(ObjectDirectory) + 1) * sizeof(WCHAR)) +
sizeof(CSR_PORT_NAME);
/* Set the port name */
PortName.Length = 0;
PortName.MaximumLength = PortNameLength;
/* Allocate a buffer for it */
PortName.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), 0, PortNameLength);
/* Create the name */
RtlAppendUnicodeToString(&PortName, ObjectDirectory );
RtlAppendUnicodeToString(&PortName, UNICODE_PATH_SEP);
RtlAppendUnicodeToString(&PortName, CSR_PORT_NAME);
/* Create a section for the port memory */
CsrSectionViewSize.QuadPart = CSR_CSRSS_SECTION_SIZE;
Status = NtCreateSection(&CsrSectionHandle,
SECTION_ALL_ACCESS,
NULL,
&CsrSectionViewSize,
PAGE_READWRITE,
SEC_COMMIT,
NULL);
if (!NT_SUCCESS(Status))
{
DPRINT1("Failure allocating CSR Section\n");
return Status;
}
/* Set up the port view structures to match them with the section */
LpcWrite.Length = sizeof(PORT_VIEW);
LpcWrite.SectionHandle = CsrSectionHandle;
LpcWrite.SectionOffset = 0;
LpcWrite.ViewSize = CsrSectionViewSize.u.LowPart;
LpcWrite.ViewBase = 0;
LpcWrite.ViewRemoteBase = 0;
LpcRead.Length = sizeof(REMOTE_PORT_VIEW);
LpcRead.ViewSize = 0;
LpcRead.ViewBase = 0;
/* Setup the QoS */
SecurityQos.ImpersonationLevel = SecurityImpersonation;
SecurityQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
SecurityQos.EffectiveOnly = TRUE;
/* Setup the connection info */
ConnectionInfo.Version = 0x10000;
/* Create a SID for us */
Status = RtlAllocateAndInitializeSid(&NtSidAuthority,
1,
SECURITY_LOCAL_SYSTEM_RID,
0,
0,
0,
0,
0,
0,
0,
&SystemSid);
/* Connect to the port */
Status = NtSecureConnectPort(&CsrApiPort,
&PortName,
&SecurityQos,
&LpcWrite,
SystemSid,
&LpcRead,
NULL,
&ConnectionInfo,
&ConnectionInfoLength);
NtClose(CsrSectionHandle);
if (!NT_SUCCESS(Status))
{
/* Failure */
DPRINT1("Couldn't connect to CSR port\n");
return Status;
}
/* Save the delta between the sections, for capture usage later */
CsrPortMemoryDelta = (ULONG_PTR)LpcWrite.ViewRemoteBase -
(ULONG_PTR)LpcWrite.ViewBase;
/* Save the Process */
CsrProcessId = ConnectionInfo.ProcessId;
/* Save CSR Section data */
NtCurrentPeb()->ReadOnlySharedMemoryBase = ConnectionInfo.SharedSectionBase;
NtCurrentPeb()->ReadOnlySharedMemoryHeap = ConnectionInfo.SharedSectionHeap;
NtCurrentPeb()->ReadOnlyStaticServerData = ConnectionInfo.SharedSectionData;
/* Create the port heap */
CsrPortHeap = RtlCreateHeap(0,
LpcWrite.ViewBase,
LpcWrite.ViewSize,
PAGE_SIZE,
0,
0);
/* Return success */
return STATUS_SUCCESS;
}
/*
* @implemented
*/
NTSTATUS
NTAPI
CsrClientConnectToServer(PWSTR ObjectDirectory,
ULONG ServerId,
PVOID ConnectionInfo,
PULONG ConnectionInfoSize,
PBOOLEAN ServerToServerCall)
{
NTSTATUS Status;
PIMAGE_NT_HEADERS NtHeader;
UNICODE_STRING CsrSrvName;
HANDLE hCsrSrv;
ANSI_STRING CsrServerRoutineName;
PCSR_CAPTURE_BUFFER CaptureBuffer;
CSR_API_MESSAGE RosApiMessage;
CSR_API_MESSAGE2 ApiMessage;
PCSR_CLIENT_CONNECT ClientConnect = &ApiMessage.ClientConnect;
/* Validate the Connection Info */
DPRINT("CsrClientConnectToServer: %lx %p\n", ServerId, ConnectionInfo);
if (ConnectionInfo && (!ConnectionInfoSize || !*ConnectionInfoSize))
{
DPRINT1("Connection info given, but no length\n");
return STATUS_INVALID_PARAMETER;
}
/* Check if we're inside a CSR Process */
if (InsideCsrProcess)
{
/* Tell the client that we're already inside CSR */
if (ServerToServerCall) *ServerToServerCall = TRUE;
return STATUS_SUCCESS;
}
/*
* We might be in a CSR Process but not know it, if this is the first call.
* So let's find out.
*/
if (!(NtHeader = RtlImageNtHeader(NtCurrentPeb()->ImageBaseAddress)))
{
/* The image isn't valid */
DPRINT1("Invalid image\n");
return STATUS_INVALID_IMAGE_FORMAT;
}
InsideCsrProcess = (NtHeader->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_NATIVE);
/* Now we can check if we are inside or not */
if (InsideCsrProcess && !UsingOldCsr)
{
/* We're inside, so let's find csrsrv */
DbgBreakPoint();
RtlInitUnicodeString(&CsrSrvName, L"csrsrv");
Status = LdrGetDllHandle(NULL,
NULL,
&CsrSrvName,
&hCsrSrv);
RtlFreeUnicodeString(&CsrSrvName);
/* Now get the Server to Server routine */
RtlInitAnsiString(&CsrServerRoutineName, "CsrCallServerFromServer");
Status = LdrGetProcedureAddress(hCsrSrv,
&CsrServerRoutineName,
0L,
(PVOID*)&CsrServerApiRoutine);
/* Use the local heap as port heap */
CsrPortHeap = RtlGetProcessHeap();
/* Tell the caller we're inside the server */
*ServerToServerCall = InsideCsrProcess;
return STATUS_SUCCESS;
}
/* Now check if connection info is given */
if (ConnectionInfo)
{
/* Well, we're defintely in a client now */
InsideCsrProcess = FALSE;
/* Do we have a connection to CSR yet? */
if (!CsrApiPort)
{
/* No, set it up now */
if (!NT_SUCCESS(Status = CsrConnectToServer(ObjectDirectory)))
{
/* Failed */
DPRINT1("Failure to connect to CSR\n");
return Status;
}
}
/* Setup the connect message header */
ClientConnect->ServerId = ServerId;
ClientConnect->ConnectionInfoSize = *ConnectionInfoSize;
/* Setup a buffer for the connection info */
CaptureBuffer = CsrAllocateCaptureBuffer(1,
ClientConnect->ConnectionInfoSize);
/* Allocate a pointer for the connection info*/
CsrAllocateMessagePointer(CaptureBuffer,
ClientConnect->ConnectionInfoSize,
&ClientConnect->ConnectionInfo);
/* Copy the data into the buffer */
RtlMoveMemory(ClientConnect->ConnectionInfo,
ConnectionInfo,
ClientConnect->ConnectionInfoSize);
/* Return the allocated length */
*ConnectionInfoSize = ClientConnect->ConnectionInfoSize;
/* Call CSR */
#if 0
Status = CsrClientCallServer(&ApiMessage,
CaptureBuffer,
CSR_MAKE_OPCODE(CsrSrvClientConnect,
CSR_SRV_DLL),
sizeof(CSR_CLIENT_CONNECT));
#endif
Status = CsrClientCallServer(&RosApiMessage,
NULL,
MAKE_CSR_API(CONNECT_PROCESS, CSR_NATIVE),
sizeof(CSR_API_MESSAGE));
}
else
{
/* No connection info, just return */
Status = STATUS_SUCCESS;
}
/* Let the caller know if this was server to server */
DPRINT("Status was: %lx. Are we in server: %lx\n", Status, InsideCsrProcess);
if (ServerToServerCall) *ServerToServerCall = InsideCsrProcess;
return Status;
}
/* EOF */

View file

@ -1,194 +0,0 @@
/* $Id$
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
* FILE: lib/ntdll/csr/lpc.c
* PURPOSE: CSRSS Client/Server LPC API
*
* REVISIONS:
* 2001-06-16 (ea)
* File api.c renamed lpc.c. Process/thread code moved
* in thread.c. Check added on the LPC port.
*/
/* INCLUDES *****************************************************************/
#include <ntdll.h>
#define NDEBUG
#include <debug.h>
/* GLOBALS *******************************************************************/
HANDLE WindowsApiPort = INVALID_HANDLE_VALUE;
static PVOID CsrSectionMapBase = NULL;
static PVOID CsrSectionMapServerBase = NULL;
static HANDLE CsrCommHeap = NULL;
#define CSR_CONTROL_HEAP_SIZE (65536)
/* FUNCTIONS *****************************************************************/
/* Possible CsrClientCallServer (the NT one): */
NTSTATUS STDCALL
CsrCaptureParameterBuffer(PVOID ParameterBuffer,
ULONG ParameterBufferSize,
PVOID* ClientAddress,
PVOID* ServerAddress)
{
PVOID Block;
Block = RtlAllocateHeap(CsrCommHeap, 0, ParameterBufferSize);
if (Block == NULL)
{
return(STATUS_NO_MEMORY);
}
if(ParameterBuffer != NULL)
{
memcpy(Block, ParameterBuffer, ParameterBufferSize);
}
*ClientAddress = Block;
*ServerAddress = (PVOID)((ULONG_PTR)Block - (ULONG_PTR)CsrSectionMapBase + (ULONG_PTR)CsrSectionMapServerBase);
return(STATUS_SUCCESS);
}
NTSTATUS STDCALL
CsrReleaseParameterBuffer(PVOID ClientAddress)
{
RtlFreeHeap(CsrCommHeap, 0, ClientAddress);
return(STATUS_SUCCESS);
}
/*
* @implemented
*/
NTSTATUS
STDCALL
CsrClientCallServer(PCSR_API_MESSAGE Request,
PVOID CapturedBuffer OPTIONAL,
CSR_API_NUMBER ApiNumber,
ULONG RequestLength)
{
NTSTATUS Status;
DPRINT("CSR: CsrClientCallServer!\n");
/* Make sure it's valid */
if (INVALID_HANDLE_VALUE == WindowsApiPort)
{
DPRINT1("NTDLL.%s: client not connected to CSRSS!\n", __FUNCTION__);
return (STATUS_UNSUCCESSFUL);
}
/* Fill out the header */
Request->Type = ApiNumber;
Request->Header.u1.s1.DataLength = RequestLength - sizeof(PORT_MESSAGE);
Request->Header.u1.s1.TotalLength = RequestLength;
DPRINT("CSR: API: %x, u1.s1.DataLength: %x, u1.s1.TotalLength: %x\n",
ApiNumber,
Request->Header.u1.s1.DataLength,
Request->Header.u1.s1.TotalLength);
/* Send the LPC Message */
Status = NtRequestWaitReplyPort(WindowsApiPort,
&Request->Header,
&Request->Header);
DPRINT("Got back: %x\n", Status);
return(Status);
}
/*
* @implemented
*/
NTSTATUS
STDCALL
CsrClientConnectToServer(PWSTR ObjectDirectory,
ULONG ServerId,
PVOID Unknown,
PVOID Context,
ULONG ContextLength,
PBOOLEAN ServerToServerCall)
{
NTSTATUS Status = STATUS_SUCCESS;
UNICODE_STRING PortName;
ULONG ConnectInfoLength;
CSR_API_MESSAGE Request;
PORT_VIEW LpcWrite;
HANDLE CsrSectionHandle;
LARGE_INTEGER CsrSectionViewSize;
if (WindowsApiPort != INVALID_HANDLE_VALUE)
{
return STATUS_SUCCESS;
}
if (NULL == ObjectDirectory)
{
return STATUS_INVALID_PARAMETER;
}
RtlInitUnicodeString (& PortName, ObjectDirectory);
CsrSectionViewSize.QuadPart = CSR_CSRSS_SECTION_SIZE;
Status = NtCreateSection(&CsrSectionHandle,
SECTION_ALL_ACCESS,
NULL,
&CsrSectionViewSize,
PAGE_READWRITE,
SEC_COMMIT,
NULL);
if (!NT_SUCCESS(Status))
{
return(Status);
}
ConnectInfoLength = 0;
LpcWrite.Length = sizeof(PORT_VIEW);
LpcWrite.SectionHandle = CsrSectionHandle;
LpcWrite.SectionOffset = 0;
LpcWrite.ViewSize = CsrSectionViewSize.u.LowPart;
Status = NtConnectPort(&WindowsApiPort,
&PortName,
NULL,
&LpcWrite,
NULL,
NULL,
NULL,
&ConnectInfoLength);
if (!NT_SUCCESS(Status))
{
WindowsApiPort = INVALID_HANDLE_VALUE;
return(Status);
}
NtClose(CsrSectionHandle);
CsrSectionMapBase = LpcWrite.ViewBase;
CsrSectionMapServerBase = LpcWrite.ViewRemoteBase;
/* Create the heap for communication for csrss. */
CsrCommHeap = RtlCreateHeap(0,
CsrSectionMapBase,
CsrSectionViewSize.u.LowPart,
CsrSectionViewSize.u.LowPart,
0,
NULL);
if (CsrCommHeap == NULL)
{
return(STATUS_NO_MEMORY);
}
Status = CsrClientCallServer(&Request,
NULL,
MAKE_CSR_API(CONNECT_PROCESS, CSR_NATIVE),
sizeof(CSR_API_MESSAGE));
if (!NT_SUCCESS(Status))
{
return(Status);
}
if (!NT_SUCCESS(Request.Status))
{
return(Request.Status);
}
return(STATUS_SUCCESS);
}
/* EOF */

View file

@ -1,66 +0,0 @@
/* $Id$
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
* FILE: lib/ntdll/csr/probe.c
* PURPOSE: CSRSS address range access probing API
* AUTHOR: Eric Kohl
* DATE: 2001-06-17
*/
#include <ntdll.h>
#define NDEBUG
#include <debug.h>
/*
* @implemented
*/
VOID STDCALL
CsrProbeForRead(IN CONST PVOID Address,
IN ULONG Length,
IN ULONG Alignment)
{
PUCHAR Pointer;
UCHAR Data;
if (Length == 0)
return;
if ((ULONG)Address & (Alignment - 1))
RtlRaiseStatus(STATUS_DATATYPE_MISALIGNMENT);
Pointer = (PUCHAR)Address;
Data = *Pointer;
Pointer = (PUCHAR)((ULONG)Address + Length -1);
Data = *Pointer;
}
/*
* @implemented
*/
VOID STDCALL
CsrProbeForWrite(IN CONST PVOID Address,
IN ULONG Length,
IN ULONG Alignment)
{
PUCHAR Pointer;
UCHAR Data;
if (Length == 0)
return;
if ((ULONG)Address & (Alignment - 1))
RtlRaiseStatus(STATUS_DATATYPE_MISALIGNMENT);
// if (Address >= MmUserProbeAddress)
// RtlRaiseStatus(STATUS_ACCESS_VIOLATION);
Pointer = (PUCHAR)Address;
Data = *Pointer;
*Pointer = Data;
Pointer = (PUCHAR)((ULONG)Address + Length -1);
Data = *Pointer;
*Pointer = Data;
}
/* EOF */

View file

@ -1,25 +0,0 @@
/* $Id$
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
* FILE: lib/ntdll/csr/srv.c
* PURPOSE: Get CSR.EXE PID
*
*/
/* INCLUDES *****************************************************************/
#include <ntdll.h>
#define NDEBUG
#include <debug.h>
/* GLOBALS *******************************************************************/
DWORD ProcessId = 0; // TODO: set it on startup
DWORD STDCALL CsrGetProcessId (VOID)
{
return ProcessId;
}
/* EOF */

View file

@ -1,60 +0,0 @@
/* $Id$
*
* COPYRIGHT: See COPYING in the top level directory
* PROJECT: ReactOS kernel
* FILE: lib/ntdll/csr/propvar.c
* PURPOSE: CSRSS threads API
*/
#include <ntdll.h>
#define NDEBUG
#include <debug.h>
/*
* @implemented
*/
NTSTATUS STDCALL
CsrNewThread(VOID)
{
return (NtRegisterThreadTerminatePort(WindowsApiPort));
}
/*
* @unimplemented
*/
NTSTATUS STDCALL
CsrSetPriorityClass(HANDLE Process,
PULONG PriorityClass)
{
/* FIXME: call csrss to get hProcess' priority */
*PriorityClass = PROCESS_PRIORITY_CLASS_NORMAL;
return (STATUS_NOT_IMPLEMENTED);
}
/*
* @unimplemented
*/
NTSTATUS
STDCALL
CsrIdentifyAlertableThread (VOID)
{
/* FIXME: notify csrss that current thread is alertable */
#if 0
CSRSS_IDENTIFY_ALERTABLE_THREAD_REPLY Reply;
CSRSS_IDENTIFY_ALERTABLE_THREAD_REQUEST Request = (PCSRSS_IDENTIFY_ALERTABLE_THREAD_REQUEST) & Reply;
PNT_TEB Teb;
Request->UniqueThread = NtCurrentTeb()->Cid.UniqueThread;
/* FIXME: this is written the NT way, NOT the ROS way! */
return CsrClientCallServer (
Request,
NULL, /* use Request storage for reply */
CSRSS_IDENTIFY_ALERTABLE_THREAD,
sizeof (CSRSS_IDENTIFY_ALERTABLE_THREAD_REPLY)
);
#endif
return (STATUS_NOT_IMPLEMENTED);
}
/* EOF */

View file

@ -5,16 +5,14 @@
LIBRARY ntdll.dll LIBRARY ntdll.dll
EXPORTS EXPORTS
CsrCaptureParameterBuffer@16 CsrAllocateCaptureBuffer@8
CsrReleaseParameterBuffer@4
CsrAllocateCaptureBuffer@12
CsrAllocateCapturePointer@12
CsrAllocateMessagePointer@12 CsrAllocateMessagePointer@12
CsrCaptureMessageBuffer@16 CsrCaptureMessageBuffer@16
CsrCaptureMessageMultiUnicodeStringsInPlace@12
CsrCaptureMessageString@20 CsrCaptureMessageString@20
CsrCaptureTimeout@8 CsrCaptureTimeout@8
CsrClientCallServer@16 CsrClientCallServer@16
CsrClientConnectToServer@24 CsrClientConnectToServer@20
CsrFreeCaptureBuffer@4 CsrFreeCaptureBuffer@4
CsrGetProcessId@0 CsrGetProcessId@0
CsrIdentifyAlertableThread@0 CsrIdentifyAlertableThread@0
@ -219,6 +217,7 @@ NtRestoreKey@12
NtResumeProcess@4 NtResumeProcess@4
NtResumeThread@8 NtResumeThread@8
NtSaveKey@8 NtSaveKey@8
NtSecureConnectPort@36
NtSetContextThread@8 NtSetContextThread@8
NtSetDefaultHardErrorPort@4 NtSetDefaultHardErrorPort@4
NtSetDefaultLocale@8 NtSetDefaultLocale@8
@ -852,6 +851,7 @@ ZwRestoreKey@12
ZwResumeProcess@4 ZwResumeProcess@4
ZwResumeThread@8 ZwResumeThread@8
ZwSaveKey@8 ZwSaveKey@8
ZwSecureConnectPort@36
ZwSetContextThread@8 ZwSetContextThread@8
ZwSetDefaultHardErrorPort@4 ZwSetDefaultHardErrorPort@4
ZwSetDefaultLocale@8 ZwSetDefaultLocale@8

View file

@ -14,11 +14,9 @@
<linkerflag>-lgcc</linkerflag> <linkerflag>-lgcc</linkerflag>
<linkerflag>-nostartfiles</linkerflag> <linkerflag>-nostartfiles</linkerflag>
<directory name="csr"> <directory name="csr">
<file>api.c</file>
<file>capture.c</file> <file>capture.c</file>
<file>lpc.c</file> <file>connect.c</file>
<file>probe.c</file>
<file>srv.c</file>
<file>thread.c</file>
</directory> </directory>
<directory name="dbg"> <directory name="dbg">
<file>debug.c</file> <file>debug.c</file>

View file

@ -458,8 +458,10 @@ CsrpApiRegisterDef (ULONG argc, PWSTR* argv)
static NTSTATUS static NTSTATUS
CsrpCCTS (ULONG argc, PWSTR* argv) CsrpCCTS (ULONG argc, PWSTR* argv)
{ {
return CsrClientConnectToServer(L"\\Windows\\ApiPort", ULONG Dummy;
0, NULL, NULL, 0, NULL); ULONG DummyLength = sizeof(Dummy);
return CsrClientConnectToServer(L"\\Windows",
0, &Dummy, &DummyLength, NULL);
} }
/********************************************************************** /**********************************************************************

View file

@ -170,6 +170,7 @@ NtResumeProcess 1
NtResumeThread 2 NtResumeThread 2
NtSaveKey 2 NtSaveKey 2
NtSaveKeyEx 3 NtSaveKeyEx 3
NtSecureConnectPort 9
NtSetBootEntryOrder 2 NtSetBootEntryOrder 2
NtSetBootOptions 2 NtSetBootOptions 2
NtSetIoCompletion 5 NtSetIoCompletion 5