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 size_t cchStringW = 0;
173 UINT32 msgLength = header->DataLength - 4;
174 const WCHAR* pStringW = Stream_ConstPointer(s);
175 const WCHAR* raConnectionStringW = pStringW;
177 while ((msgLength > 0) && pStringW[cchStringW])
183 if (pStringW[cchStringW] || !cchStringW)
184 return ERROR_INVALID_DATA;
187 const size_t cbRaConnectionStringW = cchStringW * 2;
188 pdu.raConnectionString =
189 ConvertWCharNToUtf8Alloc(raConnectionStringW, cbRaConnectionStringW /
sizeof(WCHAR), NULL);
190 if (!pdu.raConnectionString)
191 return ERROR_INTERNAL_ERROR;
193 WLog_INFO(TAG,
"RaConnectionString: %s", pdu.raConnectionString);
194 free(pdu.raConnectionString);
196 if ((error = remdesk_send_ctl_result_pdu(context, 0)))
197 WLog_ERR(TAG,
"remdesk_send_ctl_result_pdu failed with error %" PRIu32
"!", error);
207 static UINT remdesk_recv_ctl_authenticate_pdu(RemdeskServerContext* context,
wStream* s,
210 size_t cchTmpStringW = 0;
211 const WCHAR* expertBlobW = NULL;
213 UINT32 msgLength = header->DataLength - 4;
214 const WCHAR* pStringW = Stream_ConstPointer(s);
215 const WCHAR* raConnectionStringW = pStringW;
217 while ((msgLength > 0) && pStringW[cchTmpStringW])
223 if (pStringW[cchTmpStringW] || !cchTmpStringW)
224 return ERROR_INVALID_DATA;
227 const size_t cbRaConnectionStringW = cchTmpStringW *
sizeof(WCHAR);
228 pStringW += cchTmpStringW;
229 expertBlobW = pStringW;
231 size_t cchStringW = 0;
232 while ((msgLength > 0) && pStringW[cchStringW])
238 if (pStringW[cchStringW] || !cchStringW)
239 return ERROR_INVALID_DATA;
242 const size_t cbExpertBlobW = cchStringW * 2;
243 pdu.raConnectionString =
244 ConvertWCharNToUtf8Alloc(raConnectionStringW, cbRaConnectionStringW /
sizeof(WCHAR), NULL);
245 if (!pdu.raConnectionString)
246 return ERROR_INTERNAL_ERROR;
248 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW /
sizeof(WCHAR), NULL);
251 free(pdu.raConnectionString);
252 return ERROR_INTERNAL_ERROR;
255 WLog_INFO(TAG,
"RaConnectionString: %s ExpertBlob: %s", pdu.raConnectionString, pdu.expertBlob);
256 free(pdu.raConnectionString);
257 free(pdu.expertBlob);
258 return CHANNEL_RC_OK;
266 static 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);
287 if ((error = remdesk_send_ctl_result_pdu(context, 0)))
288 WLog_ERR(TAG,
"remdesk_send_ctl_result_pdu failed with error %" PRIu32
"!", error);
298 static 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;
386 static UINT remdesk_server_receive_pdu(RemdeskServerContext* context,
wStream* s)
388 UINT error = CHANNEL_RC_OK;
391 WLog_INFO(TAG,
"RemdeskReceive: %"PRIuz
"", Stream_GetRemainingLength(s));
392 winpr_HexDump(WCHAR* expertBlobW = NULL;(s), Stream_GetRemainingLength(s));
395 if ((error = remdesk_read_channel_header(s, &header)))
397 WLog_ERR(TAG,
"remdesk_read_channel_header failed with error %" PRIu32
"!", error);
401 if (strcmp(header.ChannelName,
"RC_CTL") == 0)
403 if ((error = remdesk_recv_ctl_pdu(context, s, &header)))
405 WLog_ERR(TAG,
"remdesk_recv_ctl_pdu failed with error %" PRIu32
"!", error);
409 else if (strcmp(header.ChannelName,
"70") == 0)
412 else if (strcmp(header.ChannelName,
"71") == 0)
415 else if (strcmp(header.ChannelName,
".") == 0)
418 else if (strcmp(header.ChannelName,
"1000.") == 0)
421 else if (strcmp(header.ChannelName,
"RA_FX") == 0)
431 static DWORD WINAPI remdesk_server_thread(LPVOID arg)
437 UINT32* pHeader = NULL;
438 UINT32 PduLength = 0;
440 HANDLE ChannelEvent = NULL;
441 DWORD BytesReturned = 0;
442 RemdeskServerContext* context = NULL;
444 context = (RemdeskServerContext*)arg;
448 s = Stream_New(NULL, 4096);
452 WLog_ERR(TAG,
"Stream_New failed!");
453 error = CHANNEL_RC_NO_MEMORY;
457 if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
458 &BytesReturned) == TRUE)
460 if (BytesReturned ==
sizeof(HANDLE))
461 ChannelEvent = *(HANDLE*)buffer;
463 WTSFreeMemory(buffer);
467 WLog_ERR(TAG,
"WTSVirtualChannelQuery failed!");
468 error = ERROR_INTERNAL_ERROR;
473 events[nCount++] = ChannelEvent;
474 events[nCount++] = context->priv->StopEvent;
476 if ((error = remdesk_send_ctl_version_info_pdu(context)))
478 WLog_ERR(TAG,
"remdesk_send_ctl_version_info_pdu failed with error %" PRIu32
"!", error);
484 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
486 if (status == WAIT_FAILED)
488 error = GetLastError();
489 WLog_ERR(TAG,
"WaitForMultipleObjects failed with error %" PRIu32
"", error);
493 status = WaitForSingleObject(context->priv->StopEvent, 0);
495 if (status == WAIT_FAILED)
497 error = GetLastError();
498 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"", error);
502 if (status == WAIT_OBJECT_0)
507 const size_t len = Stream_Capacity(s);
508 if (len > UINT32_MAX)
510 error = ERROR_INTERNAL_ERROR;
513 if (WTSVirtualChannelRead(context->priv->ChannelHandle, 0, Stream_BufferAs(s,
char),
514 (UINT32)len, &BytesReturned))
517 Stream_Seek(s, BytesReturned);
521 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
523 WLog_ERR(TAG,
"Stream_EnsureRemainingCapacity failed!");
524 error = CHANNEL_RC_NO_MEMORY;
529 if (Stream_GetPosition(s) >= 8)
531 pHeader = Stream_BufferAs(s, UINT32);
532 PduLength = pHeader[0] + pHeader[1] + 8;
534 if (PduLength >= Stream_GetPosition(s))
536 Stream_SealLength(s);
537 Stream_SetPosition(s, 0);
539 if ((error = remdesk_server_receive_pdu(context, s)))
541 WLog_ERR(TAG,
"remdesk_server_receive_pdu failed with error %" PRIu32
"!",
546 Stream_SetPosition(s, 0);
551 Stream_Free(s, TRUE);
554 if (error && context->rdpcontext)
555 setChannelError(context->rdpcontext, error,
"remdesk_server_thread reported an error");
566 static UINT remdesk_server_start(RemdeskServerContext* context)
568 context->priv->ChannelHandle =
569 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, REMDESK_SVC_CHANNEL_NAME);
571 if (!context->priv->ChannelHandle)
573 WLog_ERR(TAG,
"WTSVirtualChannelOpen failed!");
574 return ERROR_INTERNAL_ERROR;
577 if (!(context->priv->StopEvent = CreateEvent(NULL, TRUE, FALSE, NULL)))
579 WLog_ERR(TAG,
"CreateEvent failed!");
580 return ERROR_INTERNAL_ERROR;
583 if (!(context->priv->Thread =
584 CreateThread(NULL, 0, remdesk_server_thread, (
void*)context, 0, NULL)))
586 WLog_ERR(TAG,
"CreateThread failed!");
587 (void)CloseHandle(context->priv->StopEvent);
588 context->priv->StopEvent = NULL;
589 return ERROR_INTERNAL_ERROR;
592 return CHANNEL_RC_OK;
600 static UINT remdesk_server_stop(RemdeskServerContext* context)
603 (void)SetEvent(context->priv->StopEvent);
605 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
607 error = GetLastError();
608 WLog_ERR(TAG,
"WaitForSingleObject failed with error %" PRIu32
"!", error);
612 (void)CloseHandle(context->priv->Thread);
613 (void)CloseHandle(context->priv->StopEvent);
614 return CHANNEL_RC_OK;
617 RemdeskServerContext* remdesk_server_context_new(HANDLE vcm)
619 RemdeskServerContext* context = NULL;
620 context = (RemdeskServerContext*)calloc(1,
sizeof(RemdeskServerContext));
625 context->Start = remdesk_server_start;
626 context->Stop = remdesk_server_stop;
627 context->priv = (RemdeskServerPrivate*)calloc(1,
sizeof(RemdeskServerPrivate));
635 context->priv->Version = 1;
641 void remdesk_server_context_free(RemdeskServerContext* context)
645 if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
646 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);