22#include <freerdp/config.h>
25#include <winpr/print.h>
26#include <winpr/stream.h>
28#include <freerdp/freerdp.h>
30#include "remdesk_main.h"
31#include "remdesk_common.h"
39static UINT remdesk_virtual_channel_write(RemdeskServerContext* context,
wStream* s)
41 const size_t len = Stream_Length(s);
42 WINPR_ASSERT(len <= UINT32_MAX);
43 ULONG BytesWritten = 0;
44 BOOL status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
45 (UINT32)len, &BytesWritten);
46 return (status) ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
55static UINT remdesk_send_ctl_result_pdu(RemdeskServerContext* context, UINT32 result)
62 if ((error = remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_RESULT, 4)))
64 WLog_ERR(TAG,
"remdesk_prepare_ctl_header failed with error %" PRIu32
"!", error);
68 s = Stream_New(
nullptr, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.ch.DataLength);
72 WLog_ERR(TAG,
"Stream_New failed!");
73 return CHANNEL_RC_NO_MEMORY;
76 if ((error = remdesk_write_ctl_header(s, &(pdu.ctlHeader))))
78 WLog_ERR(TAG,
"remdesk_write_ctl_header failed with error %" PRIu32
"!", error);
82 Stream_Write_UINT32(s, pdu.result);
85 if ((error = remdesk_virtual_channel_write(context, s)))
86 WLog_ERR(TAG,
"remdesk_virtual_channel_write failed with error %" PRIu32
"!", error);
99static UINT remdesk_send_ctl_version_info_pdu(RemdeskServerContext* context)
105 if ((error = remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_VERSIONINFO, 8)))
107 WLog_ERR(TAG,
"remdesk_prepare_ctl_header failed with error %" PRIu32
"!", error);
111 pdu.versionMajor = 1;
112 pdu.versionMinor = 2;
113 s = Stream_New(
nullptr, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.ch.DataLength);
117 WLog_ERR(TAG,
"Stream_New failed!");
118 return CHANNEL_RC_NO_MEMORY;
121 if ((error = remdesk_write_ctl_header(s, &(pdu.ctlHeader))))
123 WLog_ERR(TAG,
"remdesk_write_ctl_header failed with error %" PRIu32
"!", error);
127 Stream_Write_UINT32(s, pdu.versionMajor);
128 Stream_Write_UINT32(s, pdu.versionMinor);
129 Stream_SealLength(s);
131 if ((error = remdesk_virtual_channel_write(context, s)))
132 WLog_ERR(TAG,
"remdesk_virtual_channel_write failed with error %" PRIu32
"!", error);
135 Stream_Free(s, TRUE);
146remdesk_recv_ctl_version_info_pdu(WINPR_ATTR_UNUSED RemdeskServerContext* context,
wStream* s,
149 UINT32 versionMajor = 0;
150 UINT32 versionMinor = 0;
152 if (header->DataLength < 12ull)
154 WLog_ERR(TAG,
"remdesk: HEADER::DataLength=%" PRIu32
", but expected >= 12",
156 return ERROR_INVALID_DATA;
159 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
160 return ERROR_INVALID_DATA;
162 Stream_Read_UINT32(s, versionMajor);
163 Stream_Read_UINT32(s, versionMinor);
164 if ((versionMajor != 1) || (versionMinor != 2))
166 WLog_ERR(TAG,
"REMOTEDESKTOP_CTL_VERSIONINFO_PACKET invalid version %" PRIu32
".%" PRIu32,
167 versionMajor, versionMinor);
168 return ERROR_INVALID_DATA;
171 return CHANNEL_RC_OK;
180static UINT remdesk_recv_ctl_remote_control_desktop_pdu(RemdeskServerContext* context,
wStream* s,
183 size_t cchStringW = 0;
187 size_t cchMax = header->DataLength - 4ull;
188 const size_t remaining = Stream_GetRemainingLength(s);
189 if (cchMax > remaining)
191 cchMax /=
sizeof(WCHAR);
193 const WCHAR* pStringW = Stream_ConstPointer(s);
194 const WCHAR* raConnectionStringW = pStringW;
196 while ((cchStringW < cchMax) && pStringW[cchStringW])
199 if ((cchStringW >= cchMax) || !cchStringW)
200 return ERROR_INVALID_DATA;
203 const size_t cbRaConnectionStringW = cchStringW *
sizeof(WCHAR);
204 pdu.raConnectionString = ConvertWCharNToUtf8Alloc(
205 raConnectionStringW, cbRaConnectionStringW /
sizeof(WCHAR),
nullptr);
206 if (!pdu.raConnectionString)
207 return ERROR_INTERNAL_ERROR;
209 WLog_INFO(TAG,
"RaConnectionString: %s", pdu.raConnectionString);
210 free(pdu.raConnectionString);
212 if ((error = remdesk_send_ctl_result_pdu(context, 0)))
213 WLog_ERR(TAG,
"remdesk_send_ctl_result_pdu failed with error %" PRIu32
"!", error);
224static UINT remdesk_recv_ctl_authenticate_pdu(WINPR_ATTR_UNUSED RemdeskServerContext* context,
227 size_t cchTmpStringW = 0;
228 const WCHAR* expertBlobW =
nullptr;
231 size_t cchRemaining = header->DataLength - 4ull;
232 const size_t remaining = Stream_GetRemainingLength(s);
233 if (cchRemaining > remaining)
234 cchRemaining = remaining;
235 cchRemaining /=
sizeof(WCHAR);
237 const WCHAR* pStringW = Stream_ConstPointer(s);
238 const WCHAR* raConnectionStringW = pStringW;
240 while ((cchTmpStringW < cchRemaining) && pStringW[cchTmpStringW])
243 if ((cchTmpStringW >= cchRemaining) || !cchTmpStringW)
244 return ERROR_INVALID_DATA;
247 const size_t cbRaConnectionStringW = cchTmpStringW *
sizeof(WCHAR);
248 pStringW += cchTmpStringW;
249 expertBlobW = pStringW;
250 cchRemaining -= cchTmpStringW;
252 size_t cchStringW = 0;
253 while ((cchStringW < cchRemaining) && pStringW[cchStringW])
256 if ((cchStringW >= cchRemaining) || !cchStringW)
257 return ERROR_INVALID_DATA;
260 const size_t cbExpertBlobW = cchStringW *
sizeof(WCHAR);
261 pdu.raConnectionString = ConvertWCharNToUtf8Alloc(
262 raConnectionStringW, cbRaConnectionStringW /
sizeof(WCHAR),
nullptr);
263 if (!pdu.raConnectionString)
264 return ERROR_INTERNAL_ERROR;
266 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW /
sizeof(WCHAR),
nullptr);
269 free(pdu.raConnectionString);
270 return ERROR_INTERNAL_ERROR;
273 WLog_INFO(TAG,
"RaConnectionString: %s ExpertBlob: %s", pdu.raConnectionString, pdu.expertBlob);
274 free(pdu.raConnectionString);
275 free(pdu.expertBlob);
276 return CHANNEL_RC_OK;
285static UINT remdesk_recv_ctl_verify_password_pdu(RemdeskServerContext* context,
wStream* s,
290 if (header->DataLength < 12ull)
292 WLog_ERR(TAG,
"remdesk: HEADER::DataLength=%" PRIu32
", but expected >= 12",
294 return ERROR_INVALID_DATA;
297 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
298 return ERROR_INVALID_DATA;
300 const WCHAR* expertBlobW = Stream_ConstPointer(s);
302 size_t cbExpertBlobW = header->DataLength - 4;
303 const size_t remaining = Stream_GetRemainingLength(s);
304 if (cbExpertBlobW > remaining)
305 cbExpertBlobW = remaining;
307 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW /
sizeof(WCHAR),
nullptr);
309 return ERROR_INTERNAL_ERROR;
311 WLog_INFO(TAG,
"ExpertBlob: %s", pdu.expertBlob);
315 free(pdu.expertBlob);
316 return remdesk_send_ctl_result_pdu(context, 0);
325static UINT remdesk_recv_ctl_pdu(RemdeskServerContext* context,
wStream* s,
328 UINT error = CHANNEL_RC_OK;
331 if (header->DataLength < 4)
333 WLog_ERR(TAG,
"remdesk: HEADER::DataLength=%" PRIu32
", but expected >= 4",
335 return ERROR_INVALID_DATA;
338 if (!Stream_CheckAndLogRequiredLength(TAG, s, header->DataLength))
339 return ERROR_INVALID_DATA;
341 Stream_Read_UINT32(s, msgType);
342 WLog_INFO(TAG,
"msgType: %" PRIu32
"", msgType);
346 case REMDESK_CTL_REMOTE_CONTROL_DESKTOP:
347 if ((error = remdesk_recv_ctl_remote_control_desktop_pdu(context, s, header)))
350 "remdesk_recv_ctl_remote_control_desktop_pdu failed with error %" PRIu32
358 case REMDESK_CTL_AUTHENTICATE:
359 if ((error = remdesk_recv_ctl_authenticate_pdu(context, s, header)))
361 WLog_ERR(TAG,
"remdesk_recv_ctl_authenticate_pdu failed with error %" PRIu32
"!",
368 case REMDESK_CTL_DISCONNECT:
371 case REMDESK_CTL_VERSIONINFO:
372 if ((error = remdesk_recv_ctl_version_info_pdu(context, s, header)))
374 WLog_ERR(TAG,
"remdesk_recv_ctl_version_info_pdu failed with error %" PRIu32
"!",
381 case REMDESK_CTL_ISCONNECTED:
384 case REMDESK_CTL_VERIFY_PASSWORD:
385 if ((error = remdesk_recv_ctl_verify_password_pdu(context, s, header)))
387 WLog_ERR(TAG,
"remdesk_recv_ctl_verify_password_pdu failed with error %" PRIu32
"!",
394 case REMDESK_CTL_EXPERT_ON_VISTA:
397 case REMDESK_CTL_RANOVICE_NAME:
400 case REMDESK_CTL_RAEXPERT_NAME:
403 case REMDESK_CTL_TOKEN:
407 WLog_ERR(TAG,
"remdesk_recv_control_pdu: unknown msgType: %" PRIu32
"", msgType);
408 error = ERROR_INVALID_DATA;
421static UINT remdesk_server_receive_pdu(RemdeskServerContext* context,
wStream* s)
425 UINT error = remdesk_read_channel_header(s, &header);
428 WLog_ERR(TAG,
"remdesk_read_channel_header failed with error %" PRIu32
"!", error);
432 if (strcmp(header.ChannelName,
"RC_CTL") == 0)
434 error = remdesk_recv_ctl_pdu(context, s, &header);
437 WLog_ERR(TAG,
"remdesk_recv_ctl_pdu failed with error %" PRIu32
"!", error);
441 else if (strcmp(header.ChannelName,
"70") == 0)
444 else if (strcmp(header.ChannelName,
"71") == 0)
447 else if (strcmp(header.ChannelName,
".") == 0)
450 else if (strcmp(header.ChannelName,
"1000.") == 0)
453 else if (strcmp(header.ChannelName,
"RA_FX") == 0)
464static DWORD WINAPI remdesk_server_thread(LPVOID arg)
466 void* buffer =
nullptr;
467 HANDLE events[8] = WINPR_C_ARRAY_INIT;
468 HANDLE ChannelEvent =
nullptr;
469 DWORD BytesReturned = 0;
471 RemdeskServerContext* context = (RemdeskServerContext*)arg;
472 WINPR_ASSERT(context);
473 wStream* s = Stream_New(
nullptr, 4096);
477 WLog_ERR(TAG,
"Stream_New failed!");
478 error = CHANNEL_RC_NO_MEMORY;
482 if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
483 &BytesReturned) == TRUE)
485 if (BytesReturned ==
sizeof(HANDLE))
486 ChannelEvent = *(HANDLE*)buffer;
488 WTSFreeMemory(buffer);
492 WLog_ERR(TAG,
"WTSVirtualChannelQuery failed!");
493 error = ERROR_INTERNAL_ERROR;
499 events[nCount++] = ChannelEvent;
500 events[nCount++] = context->priv->StopEvent;
502 if ((error = remdesk_send_ctl_version_info_pdu(context)))
504 WLog_ERR(TAG,
"remdesk_send_ctl_version_info_pdu failed with error %" PRIu32
"!",
511 DWORD status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
513 if (status == WAIT_FAILED)
515 error = GetLastError();
516 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"", error);
520 status = WaitForSingleObject(context->priv->StopEvent, 0);
522 if (status == WAIT_FAILED)
524 error = GetLastError();
525 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
529 if (status == WAIT_OBJECT_0)
534 const size_t len = Stream_Capacity(s);
535 if (len > UINT32_MAX)
537 error = ERROR_INTERNAL_ERROR;
540 if (WTSVirtualChannelRead(context->priv->ChannelHandle, 0, Stream_BufferAs(s,
char),
541 (UINT32)len, &BytesReturned))
544 Stream_Seek(s, BytesReturned);
548 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
550 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
551 error = CHANNEL_RC_NO_MEMORY;
556 const size_t pos = Stream_GetPosition(s);
559 Stream_ResetPosition(s);
560 const UINT32 LengthLow = Stream_Get_UINT32(s);
561 const UINT32 LengthHigh = Stream_Get_UINT32(s);
562 if (!Stream_SetPosition(s, pos))
564 error = ERROR_INTERNAL_ERROR;
567 const UINT64 PduLength = LengthLow + LengthHigh + 8ull;
569 if (PduLength >= pos)
571 Stream_SealLength(s);
572 Stream_ResetPosition(s);
574 error = remdesk_server_receive_pdu(context, s);
577 WLog_ERR(TAG,
"remdesk_server_receive_pdu failed with error %" PRIu32
"!",
582 Stream_ResetPosition(s);
588 Stream_Free(s, TRUE);
590 if (error && context->rdpcontext)
591 setChannelError(context->rdpcontext, error,
"remdesk_server_thread reported an error");
603static UINT remdesk_server_start(RemdeskServerContext* context)
605 context->priv->ChannelHandle =
606 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, REMDESK_SVC_CHANNEL_NAME);
608 if (!context->priv->ChannelHandle)
610 WLog_ERR(TAG,
"WTSVirtualChannelOpen failed!");
611 return ERROR_INTERNAL_ERROR;
614 if (!(context->priv->StopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
616 WLog_ERR(TAG,
"CreateEvent failed!");
617 return ERROR_INTERNAL_ERROR;
620 if (!(context->priv->Thread =
621 CreateThread(
nullptr, 0, remdesk_server_thread, (
void*)context, 0,
nullptr)))
623 WLog_ERR(TAG,
"CreateThread failed!");
624 (void)CloseHandle(context->priv->StopEvent);
625 context->priv->StopEvent =
nullptr;
626 return ERROR_INTERNAL_ERROR;
629 return CHANNEL_RC_OK;
638static UINT remdesk_server_stop(RemdeskServerContext* context)
641 (void)SetEvent(context->priv->StopEvent);
643 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
645 error = GetLastError();
646 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
650 (void)CloseHandle(context->priv->Thread);
651 (void)CloseHandle(context->priv->StopEvent);
652 return CHANNEL_RC_OK;
655RemdeskServerContext* remdesk_server_context_new(HANDLE vcm)
657 RemdeskServerContext* context =
nullptr;
658 context = (RemdeskServerContext*)calloc(1,
sizeof(RemdeskServerContext));
663 context->Start = remdesk_server_start;
664 context->Stop = remdesk_server_stop;
665 context->priv = (RemdeskServerPrivate*)calloc(1,
sizeof(RemdeskServerPrivate));
673 context->priv->Version = 1;
679void remdesk_server_context_free(RemdeskServerContext* context)
683 if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
684 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);