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"
38static UINT remdesk_virtual_channel_write(RemdeskServerContext* context,
wStream* s)
40 const size_t len = Stream_Length(s);
41 WINPR_ASSERT(len <= UINT32_MAX);
42 ULONG BytesWritten = 0;
43 BOOL status = WTSVirtualChannelWrite(context->priv->ChannelHandle, Stream_BufferAs(s,
char),
44 (UINT32)len, &BytesWritten);
45 return (status) ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
53static UINT remdesk_send_ctl_result_pdu(RemdeskServerContext* context, UINT32 result)
60 if ((error = remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_RESULT, 4)))
62 WLog_ERR(TAG,
"remdesk_prepare_ctl_header failed with error %" PRIu32
"!", error);
66 s = Stream_New(
nullptr, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.ch.DataLength);
70 WLog_ERR(TAG,
"Stream_New failed!");
71 return CHANNEL_RC_NO_MEMORY;
74 if ((error = remdesk_write_ctl_header(s, &(pdu.ctlHeader))))
76 WLog_ERR(TAG,
"remdesk_write_ctl_header failed with error %" PRIu32
"!", error);
80 Stream_Write_UINT32(s, pdu.result);
83 if ((error = remdesk_virtual_channel_write(context, s)))
84 WLog_ERR(TAG,
"remdesk_virtual_channel_write failed with error %" PRIu32
"!", error);
96static UINT remdesk_send_ctl_version_info_pdu(RemdeskServerContext* context)
102 if ((error = remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_VERSIONINFO, 8)))
104 WLog_ERR(TAG,
"remdesk_prepare_ctl_header failed with error %" PRIu32
"!", error);
108 pdu.versionMajor = 1;
109 pdu.versionMinor = 2;
110 s = Stream_New(
nullptr, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.ch.DataLength);
114 WLog_ERR(TAG,
"Stream_New failed!");
115 return CHANNEL_RC_NO_MEMORY;
118 if ((error = remdesk_write_ctl_header(s, &(pdu.ctlHeader))))
120 WLog_ERR(TAG,
"remdesk_write_ctl_header failed with error %" PRIu32
"!", error);
124 Stream_Write_UINT32(s, pdu.versionMajor);
125 Stream_Write_UINT32(s, pdu.versionMinor);
126 Stream_SealLength(s);
128 if ((error = remdesk_virtual_channel_write(context, s)))
129 WLog_ERR(TAG,
"remdesk_virtual_channel_write failed with error %" PRIu32
"!", error);
132 Stream_Free(s, TRUE);
141static UINT remdesk_recv_ctl_version_info_pdu(WINPR_ATTR_UNUSED RemdeskServerContext* context,
145 UINT32 versionMajor = 0;
146 UINT32 versionMinor = 0;
148 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
149 return ERROR_INVALID_DATA;
151 Stream_Read_UINT32(s, versionMajor);
152 Stream_Read_UINT32(s, versionMinor);
153 if ((versionMajor != 1) || (versionMinor != 2))
155 WLog_ERR(TAG,
"REMOTEDESKTOP_CTL_VERSIONINFO_PACKET invalid version %" PRIu32
".%" PRIu32,
156 versionMajor, versionMinor);
157 return ERROR_INVALID_DATA;
160 return CHANNEL_RC_OK;
168static UINT remdesk_recv_ctl_remote_control_desktop_pdu(RemdeskServerContext* context,
wStream* s,
171 size_t cchStringW = 0;
175 size_t cchMax = header->DataLength - 4;
176 const size_t remaining = Stream_GetRemainingLength(s);
177 if (cchMax > remaining)
179 cchMax /=
sizeof(WCHAR);
181 const WCHAR* pStringW = Stream_ConstPointer(s);
182 const WCHAR* raConnectionStringW = pStringW;
184 while ((cchStringW < cchMax) && pStringW[cchStringW])
187 if ((cchStringW >= cchMax) || !cchStringW)
188 return ERROR_INVALID_DATA;
191 const size_t cbRaConnectionStringW = cchStringW *
sizeof(WCHAR);
192 pdu.raConnectionString = ConvertWCharNToUtf8Alloc(
193 raConnectionStringW, cbRaConnectionStringW /
sizeof(WCHAR),
nullptr);
194 if (!pdu.raConnectionString)
195 return ERROR_INTERNAL_ERROR;
197 WLog_INFO(TAG,
"RaConnectionString: %s", pdu.raConnectionString);
198 free(pdu.raConnectionString);
200 if ((error = remdesk_send_ctl_result_pdu(context, 0)))
201 WLog_ERR(TAG,
"remdesk_send_ctl_result_pdu failed with error %" PRIu32
"!", error);
211static UINT remdesk_recv_ctl_authenticate_pdu(WINPR_ATTR_UNUSED RemdeskServerContext* context,
214 size_t cchTmpStringW = 0;
215 const WCHAR* expertBlobW =
nullptr;
218 size_t cchRemaining = header->DataLength - 4;
219 const size_t remaining = Stream_GetRemainingLength(s);
220 if (cchRemaining > remaining)
221 cchRemaining = remaining;
222 cchRemaining /=
sizeof(WCHAR);
224 const WCHAR* pStringW = Stream_ConstPointer(s);
225 const WCHAR* raConnectionStringW = pStringW;
227 while ((cchTmpStringW < cchRemaining) && pStringW[cchTmpStringW])
230 if ((cchTmpStringW >= cchRemaining) || !cchTmpStringW)
231 return ERROR_INVALID_DATA;
234 const size_t cbRaConnectionStringW = cchTmpStringW *
sizeof(WCHAR);
235 pStringW += cchTmpStringW;
236 expertBlobW = pStringW;
237 cchRemaining -= cchTmpStringW;
239 size_t cchStringW = 0;
240 while ((cchStringW < cchRemaining) && pStringW[cchStringW])
243 if ((cchStringW >= cchRemaining) || !cchStringW)
244 return ERROR_INVALID_DATA;
247 const size_t cbExpertBlobW = cchStringW *
sizeof(WCHAR);
248 pdu.raConnectionString = ConvertWCharNToUtf8Alloc(
249 raConnectionStringW, cbRaConnectionStringW /
sizeof(WCHAR),
nullptr);
250 if (!pdu.raConnectionString)
251 return ERROR_INTERNAL_ERROR;
253 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW /
sizeof(WCHAR),
nullptr);
256 free(pdu.raConnectionString);
257 return ERROR_INTERNAL_ERROR;
260 WLog_INFO(TAG,
"RaConnectionString: %s ExpertBlob: %s", pdu.raConnectionString, pdu.expertBlob);
261 free(pdu.raConnectionString);
262 free(pdu.expertBlob);
263 return CHANNEL_RC_OK;
271static UINT remdesk_recv_ctl_verify_password_pdu(RemdeskServerContext* context,
wStream* s,
276 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
277 return ERROR_INVALID_DATA;
279 const WCHAR* expertBlobW = Stream_ConstPointer(s);
281 size_t cbExpertBlobW = header->DataLength - 4;
282 const size_t remaining = Stream_GetRemainingLength(s);
283 if (cbExpertBlobW > remaining)
284 cbExpertBlobW = remaining;
286 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW /
sizeof(WCHAR),
nullptr);
288 return ERROR_INTERNAL_ERROR;
290 WLog_INFO(TAG,
"ExpertBlob: %s", pdu.expertBlob);
294 free(pdu.expertBlob);
295 return remdesk_send_ctl_result_pdu(context, 0);
303static UINT remdesk_recv_ctl_pdu(RemdeskServerContext* context,
wStream* s,
306 UINT error = CHANNEL_RC_OK;
309 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
310 return ERROR_INVALID_DATA;
312 if (header->DataLength < 4)
313 return ERROR_INVALID_DATA;
315 Stream_Read_UINT32(s, msgType);
316 WLog_INFO(TAG,
"msgType: %" PRIu32
"", msgType);
320 case REMDESK_CTL_REMOTE_CONTROL_DESKTOP:
321 if ((error = remdesk_recv_ctl_remote_control_desktop_pdu(context, s, header)))
324 "remdesk_recv_ctl_remote_control_desktop_pdu failed with error %" PRIu32
332 case REMDESK_CTL_AUTHENTICATE:
333 if ((error = remdesk_recv_ctl_authenticate_pdu(context, s, header)))
335 WLog_ERR(TAG,
"remdesk_recv_ctl_authenticate_pdu failed with error %" PRIu32
"!",
342 case REMDESK_CTL_DISCONNECT:
345 case REMDESK_CTL_VERSIONINFO:
346 if ((error = remdesk_recv_ctl_version_info_pdu(context, s, header)))
348 WLog_ERR(TAG,
"remdesk_recv_ctl_version_info_pdu failed with error %" PRIu32
"!",
355 case REMDESK_CTL_ISCONNECTED:
358 case REMDESK_CTL_VERIFY_PASSWORD:
359 if ((error = remdesk_recv_ctl_verify_password_pdu(context, s, header)))
361 WLog_ERR(TAG,
"remdesk_recv_ctl_verify_password_pdu failed with error %" PRIu32
"!",
368 case REMDESK_CTL_EXPERT_ON_VISTA:
371 case REMDESK_CTL_RANOVICE_NAME:
374 case REMDESK_CTL_RAEXPERT_NAME:
377 case REMDESK_CTL_TOKEN:
381 WLog_ERR(TAG,
"remdesk_recv_control_pdu: unknown msgType: %" PRIu32
"", msgType);
382 error = ERROR_INVALID_DATA;
394static UINT remdesk_server_receive_pdu(RemdeskServerContext* context,
wStream* s)
396 UINT error = CHANNEL_RC_OK;
399 if ((error = remdesk_read_channel_header(s, &header)))
401 WLog_ERR(TAG,
"remdesk_read_channel_header failed with error %" PRIu32
"!", error);
405 if (strcmp(header.ChannelName,
"RC_CTL") == 0)
407 if ((error = remdesk_recv_ctl_pdu(context, s, &header)))
409 WLog_ERR(TAG,
"remdesk_recv_ctl_pdu failed with error %" PRIu32
"!", error);
413 else if (strcmp(header.ChannelName,
"70") == 0)
416 else if (strcmp(header.ChannelName,
"71") == 0)
419 else if (strcmp(header.ChannelName,
".") == 0)
422 else if (strcmp(header.ChannelName,
"1000.") == 0)
425 else if (strcmp(header.ChannelName,
"RA_FX") == 0)
435static DWORD WINAPI remdesk_server_thread(LPVOID arg)
437 void* buffer =
nullptr;
438 HANDLE events[8] = WINPR_C_ARRAY_INIT;
439 HANDLE ChannelEvent =
nullptr;
440 DWORD BytesReturned = 0;
442 RemdeskServerContext* context = (RemdeskServerContext*)arg;
443 WINPR_ASSERT(context);
444 wStream* s = Stream_New(
nullptr, 4096);
448 WLog_ERR(TAG,
"Stream_New failed!");
449 error = CHANNEL_RC_NO_MEMORY;
453 if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
454 &BytesReturned) == TRUE)
456 if (BytesReturned ==
sizeof(HANDLE))
457 ChannelEvent = *(HANDLE*)buffer;
459 WTSFreeMemory(buffer);
463 WLog_ERR(TAG,
"WTSVirtualChannelQuery failed!");
464 error = ERROR_INTERNAL_ERROR;
470 events[nCount++] = ChannelEvent;
471 events[nCount++] = context->priv->StopEvent;
473 if ((error = remdesk_send_ctl_version_info_pdu(context)))
475 WLog_ERR(TAG,
"remdesk_send_ctl_version_info_pdu failed with error %" PRIu32
"!",
482 DWORD status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
484 if (status == WAIT_FAILED)
486 error = GetLastError();
487 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"", error);
491 status = WaitForSingleObject(context->priv->StopEvent, 0);
493 if (status == WAIT_FAILED)
495 error = GetLastError();
496 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
500 if (status == WAIT_OBJECT_0)
505 const size_t len = Stream_Capacity(s);
506 if (len > UINT32_MAX)
508 error = ERROR_INTERNAL_ERROR;
511 if (WTSVirtualChannelRead(context->priv->ChannelHandle, 0, Stream_BufferAs(s,
char),
512 (UINT32)len, &BytesReturned))
515 Stream_Seek(s, BytesReturned);
519 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
521 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
522 error = CHANNEL_RC_NO_MEMORY;
527 if (Stream_GetPosition(s) >= 8)
529 const UINT32* pHeader = Stream_BufferAs(s, UINT32);
530 const UINT32 PduLength = pHeader[0] + pHeader[1] + 8;
532 if (PduLength >= Stream_GetPosition(s))
534 Stream_SealLength(s);
535 Stream_ResetPosition(s);
537 error = remdesk_server_receive_pdu(context, s);
540 WLog_ERR(TAG,
"remdesk_server_receive_pdu failed with error %" PRIu32
"!",
545 Stream_ResetPosition(s);
551 Stream_Free(s, TRUE);
553 if (error && context->rdpcontext)
554 setChannelError(context->rdpcontext, error,
"remdesk_server_thread reported an error");
565static UINT remdesk_server_start(RemdeskServerContext* context)
567 context->priv->ChannelHandle =
568 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, REMDESK_SVC_CHANNEL_NAME);
570 if (!context->priv->ChannelHandle)
572 WLog_ERR(TAG,
"WTSVirtualChannelOpen failed!");
573 return ERROR_INTERNAL_ERROR;
576 if (!(context->priv->StopEvent = CreateEvent(
nullptr, TRUE, FALSE,
nullptr)))
578 WLog_ERR(TAG,
"CreateEvent failed!");
579 return ERROR_INTERNAL_ERROR;
582 if (!(context->priv->Thread =
583 CreateThread(
nullptr, 0, remdesk_server_thread, (
void*)context, 0,
nullptr)))
585 WLog_ERR(TAG,
"CreateThread failed!");
586 (void)CloseHandle(context->priv->StopEvent);
587 context->priv->StopEvent =
nullptr;
588 return ERROR_INTERNAL_ERROR;
591 return CHANNEL_RC_OK;
599static UINT remdesk_server_stop(RemdeskServerContext* context)
602 (void)SetEvent(context->priv->StopEvent);
604 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
606 error = GetLastError();
607 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
611 (void)CloseHandle(context->priv->Thread);
612 (void)CloseHandle(context->priv->StopEvent);
613 return CHANNEL_RC_OK;
616RemdeskServerContext* remdesk_server_context_new(HANDLE vcm)
618 RemdeskServerContext* context =
nullptr;
619 context = (RemdeskServerContext*)calloc(1,
sizeof(RemdeskServerContext));
624 context->Start = remdesk_server_start;
625 context->Stop = remdesk_server_stop;
626 context->priv = (RemdeskServerPrivate*)calloc(1,
sizeof(RemdeskServerPrivate));
634 context->priv->Version = 1;
640void remdesk_server_context_free(RemdeskServerContext* context)
644 if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
645 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);