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 const 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=%" PRIu32
" duration=%" PRIu64, size, ret, written,
72 GetTickCount64() - start);
76 WLog_VRB(TAG,
"closed on write");
80 WINPR_ASSERT(written <= INT32_MAX);
84static BOOL treatReadResult(WINPR_BIO_NAMED* ptr, DWORD readBytes)
86 WLog_VRB(TAG,
"treatReadResult(readBytes=%" PRIu32
")", readBytes);
87 ptr->opInProgress = FALSE;
90 WLog_VRB(TAG,
"readBytes == 0");
94 if (!ringbuffer_write(&ptr->readBuffer, ptr->tmpReadBuffer, readBytes))
96 WLog_VRB(TAG,
"ringbuffer_write()");
100 return SetEvent(ptr->readEvent);
103static BOOL doReadOp(WINPR_BIO_NAMED* ptr)
107 if (!ResetEvent(ptr->readEvent))
110 ptr->opInProgress = TRUE;
111 if (!ReadFile(ptr->hFile, ptr->tmpReadBuffer,
sizeof(ptr->tmpReadBuffer), &readBytes,
112 &ptr->readOverlapped))
114 DWORD error = GetLastError();
118 WLog_VRB(TAG,
"No Data, unexpected");
120 case ERROR_IO_PENDING:
121 WLog_VRB(TAG,
"ERROR_IO_PENDING");
123 case ERROR_BROKEN_PIPE:
124 WLog_VRB(TAG,
"broken pipe");
125 ptr->lastOpClosed = TRUE;
132 return treatReadResult(ptr, readBytes);
135static int transport_bio_named_read(BIO* bio,
char* buf,
int size)
140 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
144 BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ);
148 while (!ringbuffer_used(&ptr->readBuffer))
150 if (ptr->lastOpClosed)
153 if (ptr->opInProgress)
155 DWORD status = WaitForSingleObjectEx(ptr->readEvent, 500, TRUE);
159 case WAIT_IO_COMPLETION:
168 if (!GetOverlappedResult(ptr->hFile, &ptr->readOverlapped, &readBytes, FALSE))
170 WLog_ERR(TAG,
"GetOverlappedResult blocking(lastError=%" PRIu32
")",
175 if (!treatReadResult(ptr, readBytes))
177 WLog_ERR(TAG,
"treatReadResult blocking");
185 if (ptr->opInProgress)
187 DWORD status = WaitForSingleObject(ptr->readEvent, 0);
193 BIO_set_flags(bio, (BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ));
196 WLog_ERR(TAG,
"error WaitForSingleObject(readEvent)=0x%" PRIx32
"", status);
201 if (!GetOverlappedResult(ptr->hFile, &ptr->readOverlapped, &readBytes, FALSE))
203 WLog_ERR(TAG,
"GetOverlappedResult non blocking(lastError=%" PRIu32
")",
208 if (!treatReadResult(ptr, readBytes))
210 WLog_ERR(TAG,
"error treatReadResult non blocking");
219 size_t rsize = ringbuffer_used(&ptr->readBuffer);
220 if (rsize <= SSIZE_MAX)
221 ret = MIN(size, (SSIZE_T)rsize);
223 if ((size >= 0) && ret)
227 ringbuffer_peek(&ptr->readBuffer, chunks, WINPR_ASSERTING_INT_CAST(
size_t, ret));
228 for (
int i = 0; i < nchunks; i++)
230 memcpy(buf, chunks[i].data, chunks[i].size);
231 buf += chunks[i].size;
234 ringbuffer_commit_read_bytes(&ptr->readBuffer, WINPR_ASSERTING_INT_CAST(
size_t, ret));
236 WLog_VRB(TAG,
"(%d)=%" PRIdz
" nchunks=%d", size, ret, nchunks);
239 if (!ringbuffer_used(&ptr->readBuffer))
241 if (!ptr->opInProgress && !doReadOp(ptr))
243 WLog_ERR(TAG,
"error rearming read");
249 BIO_set_flags(bio, (BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ));
251 WINPR_ASSERT(ret <= INT32_MAX);
255static int transport_bio_named_puts(BIO* bio,
const char* str)
260 const size_t max = (INT_MAX > SIZE_MAX) ? SIZE_MAX : INT_MAX;
261 const size_t len = strnlen(str, max);
264 return transport_bio_named_write(bio, str, WINPR_ASSERTING_INT_CAST(
int, len));
267static int transport_bio_named_gets(BIO* bio,
char* str,
int size)
272 return transport_bio_named_read(bio, str, size);
275static long transport_bio_named_ctrl(BIO* bio,
int cmd,
long arg1,
void* arg2)
280 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
284 case BIO_C_SET_SOCKET:
285 case BIO_C_GET_SOCKET:
287 case BIO_C_GET_EVENT:
288 if (!BIO_get_init(bio) || !arg2)
291 *((HANDLE*)arg2) = ptr->readEvent;
293 case BIO_C_SET_HANDLE:
294 BIO_set_init(bio, 1);
295 if (!BIO_get_init(bio) || !arg2)
298 ptr->hFile = (HANDLE)arg2;
299 ptr->blocking = TRUE;
303 case BIO_C_SET_NONBLOCK:
305 WLog_DBG(TAG,
"BIO_C_SET_NONBLOCK");
306 ptr->blocking = FALSE;
309 case BIO_C_WAIT_READ:
311 WLog_DBG(TAG,
"BIO_C_WAIT_READ");
315 case BIO_C_WAIT_WRITE:
317 WLog_DBG(TAG,
"BIO_C_WAIT_WRITE");
327 case BIO_CTRL_GET_CLOSE:
328 status = BIO_get_shutdown(bio);
331 case BIO_CTRL_SET_CLOSE:
332 BIO_set_shutdown(bio, (
int)arg1);
352static void BIO_NAMED_free(WINPR_BIO_NAMED* ptr)
359 (void)CloseHandle(ptr->hFile);
365 (void)CloseHandle(ptr->readEvent);
366 ptr->readEvent = NULL;
369 ringbuffer_destroy(&ptr->readBuffer);
373static int transport_bio_named_uninit(BIO* bio)
376 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
380 BIO_set_init(bio, 0);
381 BIO_set_flags(bio, 0);
385static int transport_bio_named_new(BIO* bio)
389 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)calloc(1,
sizeof(WINPR_BIO_NAMED));
393 if (!ringbuffer_init(&ptr->readBuffer, 0xfffff))
396 ptr->readEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
397 if (!ptr->readEvent || ptr->readEvent == INVALID_HANDLE_VALUE)
400 ptr->readOverlapped.hEvent = ptr->readEvent;
402 BIO_set_data(bio, ptr);
403 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
411static int transport_bio_named_free(BIO* bio)
413 WINPR_BIO_NAMED* ptr = NULL;
418 transport_bio_named_uninit(bio);
420 ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
422 BIO_set_data(bio, NULL);
427static BIO_METHOD* BIO_s_namedpipe(
void)
429 static BIO_METHOD* bio_methods = NULL;
431 if (bio_methods == NULL)
433 if (!(bio_methods = BIO_meth_new(BIO_TYPE_NAMEDPIPE,
"NamedPipe")))
436 BIO_meth_set_write(bio_methods, transport_bio_named_write);
437 BIO_meth_set_read(bio_methods, transport_bio_named_read);
438 BIO_meth_set_puts(bio_methods, transport_bio_named_puts);
439 BIO_meth_set_gets(bio_methods, transport_bio_named_gets);
440 BIO_meth_set_ctrl(bio_methods, transport_bio_named_ctrl);
441 BIO_meth_set_create(bio_methods, transport_bio_named_new);
442 BIO_meth_set_destroy(bio_methods, transport_bio_named_free);
448typedef NTSTATUS (*WinStationCreateChildSessionTransportFn)(WCHAR* path, DWORD len);
449static BOOL createChildSessionTransport(HANDLE* pFile)
453 HANDLE hModule = NULL;
455 *pFile = INVALID_HANDLE_VALUE;
457 BOOL childEnabled = 0;
458 if (!WTSIsChildSessionsEnabled(&childEnabled))
460 WLog_ERR(TAG,
"error when calling WTSIsChildSessionsEnabled");
466 WLog_INFO(TAG,
"child sessions aren't enabled");
467 if (!WTSEnableChildSessions(TRUE))
469 WLog_ERR(TAG,
"error when calling WTSEnableChildSessions");
472 WLog_INFO(TAG,
"successfully enabled child sessions");
475 hModule = LoadLibraryA(
"winsta.dll");
480 WCHAR pipePath[0x80] = { 0 };
481 char pipePathA[0x80] = { 0 };
484 WinStationCreateChildSessionTransportFn createChildSessionFn =
485 GetProcAddressAs(hModule,
"WinStationCreateChildSessionTransport",
486 WinStationCreateChildSessionTransportFn);
487 if (!createChildSessionFn)
489 WLog_ERR(TAG,
"unable to retrieve WinStationCreateChildSessionTransport function");
494 HRESULT hStatus = createChildSessionFn(pipePath, 0x80);
495 if (!SUCCEEDED(hStatus))
497 WLog_ERR(TAG,
"error 0x%08x when creating childSessionTransport",
498 WINPR_CXX_COMPAT_CAST(
unsigned, hStatus));
505 const BYTE startOfPath[] = {
'\\', 0,
'\\', 0,
'.', 0,
'\\', 0 };
506 if (_wcsncmp(pipePath, (
const WCHAR*)startOfPath, 4))
511 size_t len = _wcslen(pipePath);
512 if (len > 0x80 - (4 + 1))
514 WLog_ERR(TAG,
"pipePath is too long to be adjusted");
518 memmove(pipePath + 4, pipePath, (len + 1) *
sizeof(WCHAR));
519 memcpy(pipePath, startOfPath, 8);
523 (void)ConvertWCharNToUtf8(pipePath, 0x80, pipePathA,
sizeof(pipePathA));
524 WLog_DBG(TAG,
"child session is at '%s'", pipePathA);
527 HANDLE f = CreateFileW(pipePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
528 FILE_FLAG_OVERLAPPED, NULL);
529 if (f == INVALID_HANDLE_VALUE)
531 WLog_ERR(TAG,
"error when connecting to local named pipe");
542 FreeLibrary(hModule);
546BIO* createChildSessionBio(
void)
548 HANDLE f = INVALID_HANDLE_VALUE;
549 if (!createChildSessionTransport(&f))
552 BIO* lowLevelBio = BIO_new(BIO_s_namedpipe());
555 (void)CloseHandle(f);
559 BIO_set_handle(lowLevelBio, f);
560 BIO* bufferedBio = BIO_new(BIO_s_buffered_socket());
564 BIO_free_all(lowLevelBio);
568 bufferedBio = BIO_push(bufferedBio, lowLevelBio);
a piece of data in the ring buffer, exactly like a glibc iovec