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(NULL, 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(NULL, 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;
174 UINT32 msgLength = header->DataLength - 4;
175 const WCHAR* pStringW = Stream_ConstPointer(s);
176 const WCHAR* raConnectionStringW = pStringW;
178 while ((msgLength > 0) && pStringW[cchStringW])
184 if (pStringW[cchStringW] || !cchStringW)
185 return ERROR_INVALID_DATA;
188 const size_t cbRaConnectionStringW = cchStringW * 2;
189 pdu.raConnectionString =
190 ConvertWCharNToUtf8Alloc(raConnectionStringW, cbRaConnectionStringW /
sizeof(WCHAR), NULL);
191 if (!pdu.raConnectionString)
192 return ERROR_INTERNAL_ERROR;
194 WLog_INFO(TAG,
"RaConnectionString: %s", pdu.raConnectionString);
195 free(pdu.raConnectionString);
197 if ((error = remdesk_send_ctl_result_pdu(context, 0)))
198 WLog_ERR(TAG,
"remdesk_send_ctl_result_pdu failed with error %" PRIu32
"!", error);
208static UINT remdesk_recv_ctl_authenticate_pdu(WINPR_ATTR_UNUSED RemdeskServerContext* context,
211 size_t cchTmpStringW = 0;
212 const WCHAR* expertBlobW = NULL;
214 UINT32 msgLength = header->DataLength - 4;
215 const WCHAR* pStringW = Stream_ConstPointer(s);
216 const WCHAR* raConnectionStringW = pStringW;
218 while ((msgLength > 0) && pStringW[cchTmpStringW])
224 if (pStringW[cchTmpStringW] || !cchTmpStringW)
225 return ERROR_INVALID_DATA;
228 const size_t cbRaConnectionStringW = cchTmpStringW *
sizeof(WCHAR);
229 pStringW += cchTmpStringW;
230 expertBlobW = pStringW;
232 size_t cchStringW = 0;
233 while ((msgLength > 0) && pStringW[cchStringW])
239 if (pStringW[cchStringW] || !cchStringW)
240 return ERROR_INVALID_DATA;
243 const size_t cbExpertBlobW = cchStringW * 2;
244 pdu.raConnectionString =
245 ConvertWCharNToUtf8Alloc(raConnectionStringW, cbRaConnectionStringW /
sizeof(WCHAR), NULL);
246 if (!pdu.raConnectionString)
247 return ERROR_INTERNAL_ERROR;
249 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW /
sizeof(WCHAR), NULL);
252 free(pdu.raConnectionString);
253 return ERROR_INTERNAL_ERROR;
256 WLog_INFO(TAG,
"RaConnectionString: %s ExpertBlob: %s", pdu.raConnectionString, pdu.expertBlob);
257 free(pdu.raConnectionString);
258 free(pdu.expertBlob);
259 return CHANNEL_RC_OK;
267static UINT remdesk_recv_ctl_verify_password_pdu(RemdeskServerContext* context,
wStream* s,
272 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
273 return ERROR_INVALID_DATA;
275 const WCHAR* expertBlobW = Stream_ConstPointer(s);
276 if (header->DataLength < 4)
277 return ERROR_INVALID_PARAMETER;
279 const size_t cbExpertBlobW = header->DataLength - 4;
281 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW /
sizeof(WCHAR), NULL);
283 return ERROR_INTERNAL_ERROR;
285 WLog_INFO(TAG,
"ExpertBlob: %s", pdu.expertBlob);
289 free(pdu.expertBlob);
290 return remdesk_send_ctl_result_pdu(context, 0);
298static UINT remdesk_recv_ctl_pdu(RemdeskServerContext* context,
wStream* s,
301 UINT error = CHANNEL_RC_OK;
304 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
305 return ERROR_INVALID_DATA;
307 Stream_Read_UINT32(s, msgType);
308 WLog_INFO(TAG,
"msgType: %" PRIu32
"", msgType);
312 case REMDESK_CTL_REMOTE_CONTROL_DESKTOP:
313 if ((error = remdesk_recv_ctl_remote_control_desktop_pdu(context, s, header)))
316 "remdesk_recv_ctl_remote_control_desktop_pdu failed with error %" PRIu32
324 case REMDESK_CTL_AUTHENTICATE:
325 if ((error = remdesk_recv_ctl_authenticate_pdu(context, s, header)))
327 WLog_ERR(TAG,
"remdesk_recv_ctl_authenticate_pdu failed with error %" PRIu32
"!",
334 case REMDESK_CTL_DISCONNECT:
337 case REMDESK_CTL_VERSIONINFO:
338 if ((error = remdesk_recv_ctl_version_info_pdu(context, s, header)))
340 WLog_ERR(TAG,
"remdesk_recv_ctl_version_info_pdu failed with error %" PRIu32
"!",
347 case REMDESK_CTL_ISCONNECTED:
350 case REMDESK_CTL_VERIFY_PASSWORD:
351 if ((error = remdesk_recv_ctl_verify_password_pdu(context, s, header)))
353 WLog_ERR(TAG,
"remdesk_recv_ctl_verify_password_pdu failed with error %" PRIu32
"!",
360 case REMDESK_CTL_EXPERT_ON_VISTA:
363 case REMDESK_CTL_RANOVICE_NAME:
366 case REMDESK_CTL_RAEXPERT_NAME:
369 case REMDESK_CTL_TOKEN:
373 WLog_ERR(TAG,
"remdesk_recv_control_pdu: unknown msgType: %" PRIu32
"", msgType);
374 error = ERROR_INVALID_DATA;
386static UINT remdesk_server_receive_pdu(RemdeskServerContext* context,
wStream* s)
388 UINT error = CHANNEL_RC_OK;
391 if ((error = remdesk_read_channel_header(s, &header)))
393 WLog_ERR(TAG,
"remdesk_read_channel_header failed with error %" PRIu32
"!", error);
397 if (strcmp(header.ChannelName,
"RC_CTL") == 0)
399 if ((error = remdesk_recv_ctl_pdu(context, s, &header)))
401 WLog_ERR(TAG,
"remdesk_recv_ctl_pdu failed with error %" PRIu32
"!", error);
405 else if (strcmp(header.ChannelName,
"70") == 0)
408 else if (strcmp(header.ChannelName,
"71") == 0)
411 else if (strcmp(header.ChannelName,
".") == 0)
414 else if (strcmp(header.ChannelName,
"1000.") == 0)
417 else if (strcmp(header.ChannelName,
"RA_FX") == 0)
427static DWORD WINAPI remdesk_server_thread(LPVOID arg)
433 UINT32* pHeader = NULL;
434 UINT32 PduLength = 0;
436 HANDLE ChannelEvent = NULL;
437 DWORD BytesReturned = 0;
438 RemdeskServerContext* context = NULL;
440 context = (RemdeskServerContext*)arg;
444 s = Stream_New(NULL, 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;
469 events[nCount++] = ChannelEvent;
470 events[nCount++] = context->priv->StopEvent;
472 if ((error = remdesk_send_ctl_version_info_pdu(context)))
474 WLog_ERR(TAG,
"remdesk_send_ctl_version_info_pdu failed with error %" PRIu32
"!", error);
480 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
482 if (status == WAIT_FAILED)
484 error = GetLastError();
485 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"", error);
489 status = WaitForSingleObject(context->priv->StopEvent, 0);
491 if (status == WAIT_FAILED)
493 error = GetLastError();
494 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
498 if (status == WAIT_OBJECT_0)
503 const size_t len = Stream_Capacity(s);
504 if (len > UINT32_MAX)
506 error = ERROR_INTERNAL_ERROR;
509 if (WTSVirtualChannelRead(context->priv->ChannelHandle, 0, Stream_BufferAs(s,
char),
510 (UINT32)len, &BytesReturned))
513 Stream_Seek(s, BytesReturned);
517 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
519 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
520 error = CHANNEL_RC_NO_MEMORY;
525 if (Stream_GetPosition(s) >= 8)
527 pHeader = Stream_BufferAs(s, UINT32);
528 PduLength = pHeader[0] + pHeader[1] + 8;
530 if (PduLength >= Stream_GetPosition(s))
532 Stream_SealLength(s);
533 Stream_SetPosition(s, 0);
535 if ((error = remdesk_server_receive_pdu(context, s)))
537 WLog_ERR(TAG,
"remdesk_server_receive_pdu failed with error %" PRIu32
"!",
542 Stream_SetPosition(s, 0);
547 Stream_Free(s, TRUE);
550 if (error && context->rdpcontext)
551 setChannelError(context->rdpcontext, error,
"remdesk_server_thread reported an error");
562static UINT remdesk_server_start(RemdeskServerContext* context)
564 context->priv->ChannelHandle =
565 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, REMDESK_SVC_CHANNEL_NAME);
567 if (!context->priv->ChannelHandle)
569 WLog_ERR(TAG,
"WTSVirtualChannelOpen failed!");
570 return ERROR_INTERNAL_ERROR;
573 if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
575 WLog_ERR(TAG,
"CreateEvent failed!");
576 return ERROR_INTERNAL_ERROR;
579 if (!(context->priv->Thread =
580 CreateThread(NULL, 0, remdesk_server_thread, (
void*)context, 0, NULL)))
582 WLog_ERR(TAG,
"CreateThread failed!");
583 (void)CloseHandle(context->priv->StopEvent);
584 context->priv->StopEvent = NULL;
585 return ERROR_INTERNAL_ERROR;
588 return CHANNEL_RC_OK;
596static UINT remdesk_server_stop(RemdeskServerContext* context)
599 (void)SetEvent(context->priv->StopEvent);
601 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
603 error = GetLastError();
604 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
608 (void)CloseHandle(context->priv->Thread);
609 (void)CloseHandle(context->priv->StopEvent);
610 return CHANNEL_RC_OK;
613RemdeskServerContext* remdesk_server_context_new(HANDLE vcm)
615 RemdeskServerContext* context = NULL;
616 context = (RemdeskServerContext*)calloc(1,
sizeof(RemdeskServerContext));
621 context->Start = remdesk_server_start;
622 context->Stop = remdesk_server_stop;
623 context->priv = (RemdeskServerPrivate*)calloc(1,
sizeof(RemdeskServerPrivate));
631 context->priv->Version = 1;
637void remdesk_server_context_free(RemdeskServerContext* context)
641 if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
642 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);