22 #include <freerdp/config.h>
24 #include <winpr/crt.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"
38 static 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;
53 static 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);
96 static 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);
141 static UINT remdesk_recv_ctl_version_info_pdu(RemdeskServerContext* context,
wStream* s,
144 UINT32 versionMajor = 0;
145 UINT32 versionMinor = 0;
147 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
148 return ERROR_INVALID_DATA;
150 Stream_Read_UINT32(s, versionMajor);
151 Stream_Read_UINT32(s, versionMinor);
152 if ((versionMajor != 1) || (versionMinor != 2))
154 WLog_ERR(TAG,
"REMOTEDESKTOP_CTL_VERSIONINFO_PACKET invalid version %" PRIu32
".%" PRIu32,
155 versionMajor, versionMinor);
156 return ERROR_INVALID_DATA;
159 return CHANNEL_RC_OK;
167 static UINT remdesk_recv_ctl_remote_control_desktop_pdu(RemdeskServerContext* context,
wStream* s,
170 SSIZE_T cchStringW = 0;
171 SSIZE_T cbRaConnectionStringW = 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 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);
208 static UINT remdesk_recv_ctl_authenticate_pdu(RemdeskServerContext* context,
wStream* s,
212 UINT32 msgLength = 0;
213 int cbExpertBlobW = 0;
214 const WCHAR* expertBlobW = NULL;
215 int cbRaConnectionStringW = 0;
217 msgLength = header->DataLength - 4;
218 const WCHAR* pStringW = Stream_ConstPointer(s);
219 const WCHAR* raConnectionStringW = pStringW;
221 while ((msgLength > 0) && pStringW[cchStringW])
227 if (pStringW[cchStringW] || !cchStringW)
228 return ERROR_INVALID_DATA;
231 cbRaConnectionStringW = cchStringW * 2;
232 pStringW += cchStringW;
233 expertBlobW = pStringW;
236 while ((msgLength > 0) && pStringW[cchStringW])
242 if (pStringW[cchStringW] || !cchStringW)
243 return ERROR_INVALID_DATA;
246 cbExpertBlobW = cchStringW * 2;
247 pdu.raConnectionString =
248 ConvertWCharNToUtf8Alloc(raConnectionStringW, cbRaConnectionStringW /
sizeof(WCHAR), NULL);
249 if (!pdu.raConnectionString)
250 return ERROR_INTERNAL_ERROR;
252 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW /
sizeof(WCHAR), NULL);
255 free(pdu.raConnectionString);
256 return ERROR_INTERNAL_ERROR;
259 WLog_INFO(TAG,
"RaConnectionString: %s ExpertBlob: %s", pdu.raConnectionString, pdu.expertBlob);
260 free(pdu.raConnectionString);
261 free(pdu.expertBlob);
262 return CHANNEL_RC_OK;
270 static UINT remdesk_recv_ctl_verify_password_pdu(RemdeskServerContext* context,
wStream* s,
273 SSIZE_T cbExpertBlobW = 0;
277 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
278 return ERROR_INVALID_DATA;
280 const WCHAR* expertBlobW = Stream_ConstPointer(s);
281 cbExpertBlobW = header->DataLength - 4;
283 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW /
sizeof(WCHAR), NULL);
285 return ERROR_INTERNAL_ERROR;
287 WLog_INFO(TAG,
"ExpertBlob: %s", pdu.expertBlob);
289 if ((error = remdesk_send_ctl_result_pdu(context, 0)))
290 WLog_ERR(TAG,
"remdesk_send_ctl_result_pdu failed with error %" PRIu32
"!", error);
300 static UINT remdesk_recv_ctl_pdu(RemdeskServerContext* context,
wStream* s,
303 UINT error = CHANNEL_RC_OK;
306 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
307 return ERROR_INVALID_DATA;
309 Stream_Read_UINT32(s, msgType);
310 WLog_INFO(TAG,
"msgType: %" PRIu32
"", msgType);
314 case REMDESK_CTL_REMOTE_CONTROL_DESKTOP:
315 if ((error = remdesk_recv_ctl_remote_control_desktop_pdu(context, s, header)))
318 "remdesk_recv_ctl_remote_control_desktop_pdu failed with error %" PRIu32
326 case REMDESK_CTL_AUTHENTICATE:
327 if ((error = remdesk_recv_ctl_authenticate_pdu(context, s, header)))
329 WLog_ERR(TAG,
"remdesk_recv_ctl_authenticate_pdu failed with error %" PRIu32
"!",
336 case REMDESK_CTL_DISCONNECT:
339 case REMDESK_CTL_VERSIONINFO:
340 if ((error = remdesk_recv_ctl_version_info_pdu(context, s, header)))
342 WLog_ERR(TAG,
"remdesk_recv_ctl_version_info_pdu failed with error %" PRIu32
"!",
349 case REMDESK_CTL_ISCONNECTED:
352 case REMDESK_CTL_VERIFY_PASSWORD:
353 if ((error = remdesk_recv_ctl_verify_password_pdu(context, s, header)))
355 WLog_ERR(TAG,
"remdesk_recv_ctl_verify_password_pdu failed with error %" PRIu32
"!",
362 case REMDESK_CTL_EXPERT_ON_VISTA:
365 case REMDESK_CTL_RANOVICE_NAME:
368 case REMDESK_CTL_RAEXPERT_NAME:
371 case REMDESK_CTL_TOKEN:
375 WLog_ERR(TAG,
"remdesk_recv_control_pdu: unknown msgType: %" PRIu32
"", msgType);
376 error = ERROR_INVALID_DATA;
388 static UINT remdesk_server_receive_pdu(RemdeskServerContext* context,
wStream* s)
390 UINT error = CHANNEL_RC_OK;
393 WLog_INFO(TAG,
"RemdeskReceive: %"PRIuz
"", Stream_GetRemainingLength(s));
394 winpr_HexDump(WCHAR* expertBlobW = NULL;(s), Stream_GetRemainingLength(s));
397 if ((error = remdesk_read_channel_header(s, &header)))
399 WLog_ERR(TAG,
"remdesk_read_channel_header failed with error %" PRIu32
"!", error);
403 if (strcmp(header.ChannelName,
"RC_CTL") == 0)
405 if ((error = remdesk_recv_ctl_pdu(context, s, &header)))
407 WLog_ERR(TAG,
"remdesk_recv_ctl_pdu failed with error %" PRIu32
"!", error);
411 else if (strcmp(header.ChannelName,
"70") == 0)
414 else if (strcmp(header.ChannelName,
"71") == 0)
417 else if (strcmp(header.ChannelName,
".") == 0)
420 else if (strcmp(header.ChannelName,
"1000.") == 0)
423 else if (strcmp(header.ChannelName,
"RA_FX") == 0)
433 static DWORD WINAPI remdesk_server_thread(LPVOID arg)
439 UINT32* pHeader = NULL;
440 UINT32 PduLength = 0;
442 HANDLE ChannelEvent = NULL;
443 DWORD BytesReturned = 0;
444 RemdeskServerContext* context = NULL;
446 context = (RemdeskServerContext*)arg;
450 s = Stream_New(NULL, 4096);
454 WLog_ERR(TAG,
"Stream_New failed!");
455 error = CHANNEL_RC_NO_MEMORY;
459 if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
460 &BytesReturned) == TRUE)
462 if (BytesReturned ==
sizeof(HANDLE))
463 CopyMemory(&ChannelEvent, buffer,
sizeof(HANDLE));
465 WTSFreeMemory(buffer);
469 WLog_ERR(TAG,
"WTSVirtualChannelQuery failed!");
470 error = ERROR_INTERNAL_ERROR;
475 events[nCount++] = ChannelEvent;
476 events[nCount++] = context->priv->StopEvent;
478 if ((error = remdesk_send_ctl_version_info_pdu(context)))
480 WLog_ERR(TAG,
"remdesk_send_ctl_version_info_pdu failed with error %" PRIu32
"!", error);
486 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
488 if (status == WAIT_FAILED)
490 error = GetLastError();
491 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"", error);
495 status = WaitForSingleObject(context->priv->StopEvent, 0);
497 if (status == WAIT_FAILED)
499 error = GetLastError();
500 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
504 if (status == WAIT_OBJECT_0)
509 const size_t len = Stream_Capacity(s);
510 if (len > UINT32_MAX)
512 error = ERROR_INTERNAL_ERROR;
515 if (WTSVirtualChannelRead(context->priv->ChannelHandle, 0, Stream_BufferAs(s,
char),
516 (UINT32)len, &BytesReturned))
519 Stream_Seek(s, BytesReturned);
523 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
525 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
526 error = CHANNEL_RC_NO_MEMORY;
531 if (Stream_GetPosition(s) >= 8)
533 pHeader = Stream_BufferAs(s, UINT32);
534 PduLength = pHeader[0] + pHeader[1] + 8;
536 if (PduLength >= Stream_GetPosition(s))
538 Stream_SealLength(s);
539 Stream_SetPosition(s, 0);
541 if ((error = remdesk_server_receive_pdu(context, s)))
543 WLog_ERR(TAG,
"remdesk_server_receive_pdu failed with error %" PRIu32
"!",
548 Stream_SetPosition(s, 0);
553 Stream_Free(s, TRUE);
556 if (error && context->rdpcontext)
557 setChannelError(context->rdpcontext, error,
"remdesk_server_thread reported an error");
568 static UINT remdesk_server_start(RemdeskServerContext* context)
570 context->priv->ChannelHandle =
571 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, REMDESK_SVC_CHANNEL_NAME);
573 if (!context->priv->ChannelHandle)
575 WLog_ERR(TAG,
"WTSVirtualChannelOpen failed!");
576 return ERROR_INTERNAL_ERROR;
579 if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
581 WLog_ERR(TAG,
"CreateEvent failed!");
582 return ERROR_INTERNAL_ERROR;
585 if (!(context->priv->Thread =
586 CreateThread(NULL, 0, remdesk_server_thread, (
void*)context, 0, NULL)))
588 WLog_ERR(TAG,
"CreateThread failed!");
589 (void)CloseHandle(context->priv->StopEvent);
590 context->priv->StopEvent = NULL;
591 return ERROR_INTERNAL_ERROR;
594 return CHANNEL_RC_OK;
602 static UINT remdesk_server_stop(RemdeskServerContext* context)
605 (void)SetEvent(context->priv->StopEvent);
607 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
609 error = GetLastError();
610 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
614 (void)CloseHandle(context->priv->Thread);
615 (void)CloseHandle(context->priv->StopEvent);
616 return CHANNEL_RC_OK;
619 RemdeskServerContext* remdesk_server_context_new(HANDLE vcm)
621 RemdeskServerContext* context = NULL;
622 context = (RemdeskServerContext*)calloc(1,
sizeof(RemdeskServerContext));
627 context->Start = remdesk_server_start;
628 context->Stop = remdesk_server_stop;
629 context->priv = (RemdeskServerPrivate*)calloc(1,
sizeof(RemdeskServerPrivate));
637 context->priv->Version = 1;
643 void remdesk_server_context_free(RemdeskServerContext* context)
647 if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
648 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);