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];
46 static int transport_bio_named_uninit(BIO* bio);
48 static 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, 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);
83 static 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);
102 static 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);
134 static 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)
225 const int nchunks = ringbuffer_peek(&ptr->readBuffer, chunks, ret);
226 for (
int i = 0; i < nchunks; i++)
228 memcpy(buf, chunks[i].data, chunks[i].size);
229 buf += chunks[i].size;
232 ringbuffer_commit_read_bytes(&ptr->readBuffer, ret);
234 WLog_VRB(TAG,
"(%d)=%" PRIdz
" nchunks=%d", size, ret, nchunks);
237 if (!ringbuffer_used(&ptr->readBuffer))
239 if (!ptr->opInProgress && !doReadOp(ptr))
241 WLog_ERR(TAG,
"error rearming read");
247 BIO_set_flags(bio, (BIO_FLAGS_SHOULD_RETRY | BIO_FLAGS_READ));
249 WINPR_ASSERT(ret <= INT32_MAX);
253 static int transport_bio_named_puts(BIO* bio,
const char* str)
258 return transport_bio_named_write(bio, str, (
int)strnlen(str, INT32_MAX));
261 static int transport_bio_named_gets(BIO* bio,
char* str,
int size)
266 return transport_bio_named_read(bio, str, size);
269 static long transport_bio_named_ctrl(BIO* bio,
int cmd,
long arg1,
void* arg2)
274 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
278 case BIO_C_SET_SOCKET:
279 case BIO_C_GET_SOCKET:
281 case BIO_C_GET_EVENT:
282 if (!BIO_get_init(bio) || !arg2)
285 *((HANDLE*)arg2) = ptr->readEvent;
287 case BIO_C_SET_HANDLE:
288 BIO_set_init(bio, 1);
289 if (!BIO_get_init(bio) || !arg2)
292 ptr->hFile = (HANDLE)arg2;
293 ptr->blocking = TRUE;
297 case BIO_C_SET_NONBLOCK:
299 WLog_DBG(TAG,
"BIO_C_SET_NONBLOCK");
300 ptr->blocking = FALSE;
303 case BIO_C_WAIT_READ:
305 WLog_DBG(TAG,
"BIO_C_WAIT_READ");
309 case BIO_C_WAIT_WRITE:
311 WLog_DBG(TAG,
"BIO_C_WAIT_WRITE");
321 case BIO_CTRL_GET_CLOSE:
322 status = BIO_get_shutdown(bio);
325 case BIO_CTRL_SET_CLOSE:
326 BIO_set_shutdown(bio, (
int)arg1);
346 static void BIO_NAMED_free(WINPR_BIO_NAMED* ptr)
353 (void)CloseHandle(ptr->hFile);
359 (void)CloseHandle(ptr->readEvent);
360 ptr->readEvent = NULL;
363 ringbuffer_destroy(&ptr->readBuffer);
367 static int transport_bio_named_uninit(BIO* bio)
370 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
374 BIO_set_init(bio, 0);
375 BIO_set_flags(bio, 0);
379 static int transport_bio_named_new(BIO* bio)
383 WINPR_BIO_NAMED* ptr = (WINPR_BIO_NAMED*)calloc(1,
sizeof(WINPR_BIO_NAMED));
387 if (!ringbuffer_init(&ptr->readBuffer, 0xfffff))
390 ptr->readEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
391 if (!ptr->readEvent || ptr->readEvent == INVALID_HANDLE_VALUE)
394 ptr->readOverlapped.hEvent = ptr->readEvent;
396 BIO_set_data(bio, ptr);
397 BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
405 static int transport_bio_named_free(BIO* bio)
407 WINPR_BIO_NAMED* ptr = NULL;
412 transport_bio_named_uninit(bio);
414 ptr = (WINPR_BIO_NAMED*)BIO_get_data(bio);
416 BIO_set_data(bio, NULL);
421 static BIO_METHOD* BIO_s_namedpipe(
void)
423 static BIO_METHOD* bio_methods = NULL;
425 if (bio_methods == NULL)
427 if (!(bio_methods = BIO_meth_new(BIO_TYPE_NAMEDPIPE,
"NamedPipe")))
430 BIO_meth_set_write(bio_methods, transport_bio_named_write);
431 BIO_meth_set_read(bio_methods, transport_bio_named_read);
432 BIO_meth_set_puts(bio_methods, transport_bio_named_puts);
433 BIO_meth_set_gets(bio_methods, transport_bio_named_gets);
434 BIO_meth_set_ctrl(bio_methods, transport_bio_named_ctrl);
435 BIO_meth_set_create(bio_methods, transport_bio_named_new);
436 BIO_meth_set_destroy(bio_methods, transport_bio_named_free);
442 typedef NTSTATUS (*WinStationCreateChildSessionTransportFn)(WCHAR* path, DWORD len);
443 static BOOL createChildSessionTransport(HANDLE* pFile)
447 HANDLE hModule = NULL;
449 *pFile = INVALID_HANDLE_VALUE;
451 BOOL childEnabled = 0;
452 if (!WTSIsChildSessionsEnabled(&childEnabled))
454 WLog_ERR(TAG,
"error when calling WTSIsChildSessionsEnabled");
460 WLog_INFO(TAG,
"child sessions aren't enabled");
461 if (!WTSEnableChildSessions(TRUE))
463 WLog_ERR(TAG,
"error when calling WTSEnableChildSessions");
466 WLog_INFO(TAG,
"successfully enabled child sessions");
469 hModule = LoadLibraryA(
"winsta.dll");
472 WCHAR pipePath[0x80] = { 0 };
473 char pipePathA[0x80] = { 0 };
475 WinStationCreateChildSessionTransportFn createChildSessionFn = GetProcAddressAs(
476 hModule,
"WinStationCreateChildSessionTransport", WinStationCreateChildSessionTransportFn);
477 if (!createChildSessionFn)
479 WLog_ERR(TAG,
"unable to retrieve WinStationCreateChildSessionTransport function");
483 HRESULT hStatus = createChildSessionFn(pipePath, 0x80);
484 if (!SUCCEEDED(hStatus))
486 WLog_ERR(TAG,
"error 0x%x when creating childSessionTransport", hStatus);
490 const BYTE startOfPath[] = {
'\\', 0,
'\\', 0,
'.', 0,
'\\', 0 };
491 if (_wcsncmp(pipePath, (
const WCHAR*)startOfPath, 4))
496 size_t len = _wcslen(pipePath);
497 if (len > 0x80 - (4 + 1))
499 WLog_ERR(TAG,
"pipePath is too long to be adjusted");
503 memmove(pipePath + 4, pipePath, (len + 1) *
sizeof(WCHAR));
504 memcpy(pipePath, startOfPath, 8);
507 (void)ConvertWCharNToUtf8(pipePath, 0x80, pipePathA,
sizeof(pipePathA));
508 WLog_DBG(TAG,
"child session is at '%s'", pipePathA);
510 HANDLE f = CreateFileW(pipePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
511 FILE_FLAG_OVERLAPPED, NULL);
512 if (f == INVALID_HANDLE_VALUE)
514 WLog_ERR(TAG,
"error when connecting to local named pipe");
522 FreeLibrary(hModule);
526 BIO* createChildSessionBio(
void)
528 HANDLE f = INVALID_HANDLE_VALUE;
529 if (!createChildSessionTransport(&f))
532 BIO* lowLevelBio = BIO_new(BIO_s_namedpipe());
535 (void)CloseHandle(f);
539 BIO_set_handle(lowLevelBio, f);
540 BIO* bufferedBio = BIO_new(BIO_s_buffered_socket());
544 BIO_free_all(lowLevelBio);
548 bufferedBio = BIO_push(bufferedBio, lowLevelBio);
a piece of data in the ring buffer, exactly like a glibc iovec