22#include <winpr/library.h>
23#include <winpr/assert.h>
24#include <winpr/print.h>
25#include <winpr/sysinfo.h>
27#include <freerdp/utils/ringbuffer.h>
29#include "childsession.h"
31#define TAG FREERDP_TAG("childsession")
41 BYTE tmpReadBuffer[4096];
46static int transport_bio_named_uninit(BIO* bio);
48static int transport_bio_named_write(BIO* bio,
const char* buf,
int size)
53 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
58 BIO_clear_flags(bio, BIO_FLAGS_WRITE);
61 UINT64 start = GetTickCount64();
62 BOOL ret = WriteFile(ptr->hFile, buf, WINPR_ASSERTING_INT_CAST(uint32_t, size), &written, NULL);
67 WLog_VRB(TAG,
"error or deferred");
71 WLog_VRB(TAG,
"(%d)=%d written=%d duration=%d", size, ret, written, GetTickCount64() - start);
75 WLog_VRB(TAG,
"closed on write");
79 WINPR_ASSERT(written <= INT32_MAX);
83static BOOL treatReadResult(WINPR_BIO_NAMED* ptr, DWORD readBytes)
85 WLog_VRB(TAG,
"treatReadResult(readBytes=%" PRIu32
")", readBytes);
86 ptr->opInProgress = FALSE;
89 WLog_VRB(TAG,
"readBytes == 0");
93 if (!ringbuffer_write(&ptr->readBuffer, ptr->tmpReadBuffer, readBytes))
95 WLog_VRB(TAG,
"ringbuffer_write()");
99 return SetEvent(ptr->readEvent);
102static BOOL doReadOp(WINPR_BIO_NAMED* ptr)
106 if (!ResetEvent(ptr->readEvent))
109 ptr->opInProgress = TRUE;
110 if (!ReadFile(ptr->hFile, ptr->tmpReadBuffer,
sizeof(ptr->tmpReadBuffer), &readBytes,
111 &ptr->readOverlapped))
113 DWORD error = GetLastError();
117 WLog_VRB(TAG,
"No Data, unexpected");
119 case ERROR_IO_PENDING:
120 WLog_VRB(TAG,
"ERROR_IO_PENDING");
122 case ERROR_BROKEN_PIPE:
123 WLog_VRB(TAG,
"broken pipe");
124 ptr->lastOpClosed = TRUE;
131 return treatReadResult(ptr, readBytes);
134static int transport_bio_named_read(BIO* bio,
char* buf,
int size)
139 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
143 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ);
147 while (!ringbuffer_used(&ptr->readBuffer))
149 if (ptr->lastOpClosed)
152 if (ptr->opInProgress)
154 DWORD status = WaitForSingleObjectEx(ptr->readEvent, 500, TRUE);
158 case WAIT_IO_COMPLETION:
167 if (!GetOverlappedResult(ptr->hFile, &ptr->readOverlapped, &readBytes, FALSE))
169 WLog_ERR(TAG,
"GetOverlappedResult blocking(lastError=%" PRIu32
")",
174 if (!treatReadResult(ptr, readBytes))
176 WLog_ERR(TAG,
"treatReadResult blocking");
184 if (ptr->opInProgress)
186 DWORD status = WaitForSingleObject(ptr->readEvent, 0);
192 BIO_set_flags(bio, (BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ));
195 WLog_ERR(TAG,
"error WaitForSingleObject(readEvent)=0x%" PRIx32
"", status);
200 if (!GetOverlappedResult(ptr->hFile, &ptr->readOverlapped, &readBytes, FALSE))
202 WLog_ERR(TAG,
"GetOverlappedResult non blocking(lastError=%" PRIu32
")",
207 if (!treatReadResult(ptr, readBytes))
209 WLog_ERR(TAG,
"error treatReadResult non blocking");
218 size_t rsize = ringbuffer_used(&ptr->readBuffer);
219 if (rsize <= SSIZE_MAX)
220 ret = MIN(size, (SSIZE_T)rsize);
222 if ((size >= 0) && ret)
226 ringbuffer_peek(&ptr->readBuffer, chunks, WINPR_ASSERTING_INT_CAST(
size_t, ret));
227 for (
int i = 0; i < nchunks; i++)
229 memcpy(buf, chunks[i].data, chunks[i].size);
230 buf += chunks[i].size;
233 ringbuffer_commit_read_bytes(&ptr->readBuffer, WINPR_ASSERTING_INT_CAST(
size_t, ret));
235 WLog_VRB(TAG,
"(%d)=%" PRIdz
" nchunks=%d", size, ret, nchunks);
238 if (!ringbuffer_used(&ptr->readBuffer))
240 if (!ptr->opInProgress && !doReadOp(ptr))
242 WLog_ERR(TAG,
"error rearming read");
248 BIO_set_flags(bio, (BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ));
250 WINPR_ASSERT(ret <= INT32_MAX);
254static int transport_bio_named_puts(BIO* bio,
const char* str)
259 const int max = (INT_MAX > SIZE_MAX) ? SIZE_MAX : INT_MAX;
260 const size_t len = strnlen(str, max);
263 return transport_bio_named_write(bio, str, WINPR_ASSERTING_INT_CAST(
int, len));
266static int transport_bio_named_gets(BIO* bio,
char* str,
int size)
271 return transport_bio_named_read(bio, str, size);
274static long transport_bio_named_ctrl(BIO* bio,
int cmd,
long arg1,
void* arg2)
279 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
283 case BIO_C_SET_SOCKET:
284 case BIO_C_GET_SOCKET:
286 case BIO_C_GET_EVENT:
287 if (!BIO_get_init(bio) || !arg2)
290 *((HANDLE*)arg2) = ptr->readEvent;
292 case BIO_C_SET_HANDLE:
293 BIO_set_init(bio, 1);
294 if (!BIO_get_init(bio) || !arg2)
297 ptr->hFile = (HANDLE)arg2;
298 ptr->blocking = TRUE;
302 case BIO_C_SET_NONBLOCK:
304 WLog_DBG(TAG,
"BIO_C_SET_NONBLOCK");
305 ptr->blocking = FALSE;
308 case BIO_C_WAIT_READ:
310 WLog_DBG(TAG,
"BIO_C_WAIT_READ");
314 case BIO_C_WAIT_WRITE:
316 WLog_DBG(TAG,
"BIO_C_WAIT_WRITE");
326 case BIO_CTRL_GET_CLOSE:
327 status = BIO_get_shutdown(bio);
330 case BIO_CTRL_SET_CLOSE:
331 BIO_set_shutdown(bio, (
int)arg1);
351static void BIO_NAMED_free(WINPR_BIO_NAMED* ptr)
358 (void)CloseHandle(ptr->hFile);
364 (void)CloseHandle(ptr->readEvent);
365 ptr->readEvent = NULL;
368 ringbuffer_destroy(&ptr->readBuffer);
372static int transport_bio_named_uninit(BIO* bio)
375 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
379 BIO_set_init(bio, 0);
380 BIO_set_flags(bio, 0);
384static int transport_bio_named_new(BIO* bio)
388 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)calloc(1,
sizeof(WINPR_BIO_NAMED));
392 if (!ringbuffer_init(&ptr->readBuffer, 0xfffff))
395 ptr->readEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
396 if (!ptr->readEvent || ptr->readEvent == INVALID_HANDLE_VALUE)
399 ptr->readOverlapped.hEvent = ptr->readEvent;
401 BIO_set_data(bio, ptr);
402 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
410static int transport_bio_named_free(BIO* bio)
412 WINPR_BIO_NAMED* ptr = NULL;
417 transport_bio_named_uninit(bio);
419 ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
421 BIO_set_data(bio, NULL);
426static BIO_METHOD* BIO_s_namedpipe(
void)
428 static BIO_METHOD* bio_methods = NULL;
430 if (bio_methods == NULL)
432 if (!(bio_methods = BIO_meth_new(BIO_TYPE_NAMEDPIPE,
"NamedPipe")))
435 BIO_meth_set_write(bio_methods, transport_bio_named_write);
436 BIO_meth_set_read(bio_methods, transport_bio_named_read);
437 BIO_meth_set_puts(bio_methods, transport_bio_named_puts);
438 BIO_meth_set_gets(bio_methods, transport_bio_named_gets);
439 BIO_meth_set_ctrl(bio_methods, transport_bio_named_ctrl);
440 BIO_meth_set_create(bio_methods, transport_bio_named_new);
441 BIO_meth_set_destroy(bio_methods, transport_bio_named_free);
447typedef NTSTATUS (*WinStationCreateChildSessionTransportFn)(WCHAR* path, DWORD len);
448static BOOL createChildSessionTransport(HANDLE* pFile)
452 HANDLE hModule = NULL;
454 *pFile = INVALID_HANDLE_VALUE;
456 BOOL childEnabled = 0;
457 if (!WTSIsChildSessionsEnabled(&childEnabled))
459 WLog_ERR(TAG,
"error when calling WTSIsChildSessionsEnabled");
465 WLog_INFO(TAG,
"child sessions aren't enabled");
466 if (!WTSEnableChildSessions(TRUE))
468 WLog_ERR(TAG,
"error when calling WTSEnableChildSessions");
471 WLog_INFO(TAG,
"successfully enabled child sessions");
474 hModule = LoadLibraryA(
"winsta.dll");
477 WCHAR pipePath[0x80] = { 0 };
478 char pipePathA[0x80] = { 0 };
480 WinStationCreateChildSessionTransportFn createChildSessionFn = GetProcAddressAs(
481 hModule,
"WinStationCreateChildSessionTransport", WinStationCreateChildSessionTransportFn);
482 if (!createChildSessionFn)
484 WLog_ERR(TAG,
"unable to retrieve WinStationCreateChildSessionTransport function");
488 HRESULT hStatus = createChildSessionFn(pipePath, 0x80);
489 if (!SUCCEEDED(hStatus))
491 WLog_ERR(TAG,
"error 0x%x when creating childSessionTransport", hStatus);
495 const BYTE startOfPath[] = {
'\\', 0,
'\\', 0,
'.', 0,
'\\', 0 };
496 if (_wcsncmp(pipePath, (
const WCHAR*)startOfPath, 4))
501 size_t len = _wcslen(pipePath);
502 if (len > 0x80 - (4 + 1))
504 WLog_ERR(TAG,
"pipePath is too long to be adjusted");
508 memmove(pipePath + 4, pipePath, (len + 1) *
sizeof(WCHAR));
509 memcpy(pipePath, startOfPath, 8);
512 (void)ConvertWCharNToUtf8(pipePath, 0x80, pipePathA,
sizeof(pipePathA));
513 WLog_DBG(TAG,
"child session is at '%s'", pipePathA);
515 HANDLE f = CreateFileW(pipePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
516 FILE_FLAG_OVERLAPPED, NULL);
517 if (f == INVALID_HANDLE_VALUE)
519 WLog_ERR(TAG,
"error when connecting to local named pipe");
527 FreeLibrary(hModule);
531BIO* createChildSessionBio(
void)
533 HANDLE f = INVALID_HANDLE_VALUE;
534 if (!createChildSessionTransport(&f))
537 BIO* lowLevelBio = BIO_new(BIO_s_namedpipe());
540 (void)CloseHandle(f);
544 BIO_set_handle(lowLevelBio, f);
545 BIO* bufferedBio = BIO_new(BIO_s_buffered_socket());
549 BIO_free_all(lowLevelBio);
553 bufferedBio = BIO_push(bufferedBio, lowLevelBio);
a piece of data in the ring buffer, exactly like a glibc iovec