FreeRDP
Loading...
Searching...
No Matches
server/remdesk_main.c
1
22#include <freerdp/config.h>
23
24#include <winpr/crt.h>
25#include <winpr/print.h>
26#include <winpr/stream.h>
27
28#include <freerdp/freerdp.h>
29
30#include "remdesk_main.h"
31#include "remdesk_common.h"
32
38WINPR_ATTR_NODISCARD
39static UINT remdesk_virtual_channel_write(RemdeskServerContext* context, wStream* s)
40{
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;
47}
48
54WINPR_ATTR_NODISCARD
55static UINT remdesk_send_ctl_result_pdu(RemdeskServerContext* context, UINT32 result)
56{
57 wStream* s = nullptr;
59 UINT error = 0;
60 pdu.result = result;
61
62 if ((error = remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_RESULT, 4)))
63 {
64 WLog_ERR(TAG, "remdesk_prepare_ctl_header failed with error %" PRIu32 "!", error);
65 return error;
66 }
67
68 s = Stream_New(nullptr, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.ch.DataLength);
69
70 if (!s)
71 {
72 WLog_ERR(TAG, "Stream_New failed!");
73 return CHANNEL_RC_NO_MEMORY;
74 }
75
76 if ((error = remdesk_write_ctl_header(s, &(pdu.ctlHeader))))
77 {
78 WLog_ERR(TAG, "remdesk_write_ctl_header failed with error %" PRIu32 "!", error);
79 goto out;
80 }
81
82 Stream_Write_UINT32(s, pdu.result); /* result (4 bytes) */
83 Stream_SealLength(s);
84
85 if ((error = remdesk_virtual_channel_write(context, s)))
86 WLog_ERR(TAG, "remdesk_virtual_channel_write failed with error %" PRIu32 "!", error);
87
88out:
89 Stream_Free(s, TRUE);
90 return error;
91}
92
98WINPR_ATTR_NODISCARD
99static UINT remdesk_send_ctl_version_info_pdu(RemdeskServerContext* context)
100{
101 wStream* s = nullptr;
103 UINT error = 0;
104
105 if ((error = remdesk_prepare_ctl_header(&(pdu.ctlHeader), REMDESK_CTL_VERSIONINFO, 8)))
106 {
107 WLog_ERR(TAG, "remdesk_prepare_ctl_header failed with error %" PRIu32 "!", error);
108 return error;
109 }
110
111 pdu.versionMajor = 1;
112 pdu.versionMinor = 2;
113 s = Stream_New(nullptr, REMDESK_CHANNEL_CTL_SIZE + pdu.ctlHeader.ch.DataLength);
114
115 if (!s)
116 {
117 WLog_ERR(TAG, "Stream_New failed!");
118 return CHANNEL_RC_NO_MEMORY;
119 }
120
121 if ((error = remdesk_write_ctl_header(s, &(pdu.ctlHeader))))
122 {
123 WLog_ERR(TAG, "remdesk_write_ctl_header failed with error %" PRIu32 "!", error);
124 goto out;
125 }
126
127 Stream_Write_UINT32(s, pdu.versionMajor); /* versionMajor (4 bytes) */
128 Stream_Write_UINT32(s, pdu.versionMinor); /* versionMinor (4 bytes) */
129 Stream_SealLength(s);
130
131 if ((error = remdesk_virtual_channel_write(context, s)))
132 WLog_ERR(TAG, "remdesk_virtual_channel_write failed with error %" PRIu32 "!", error);
133
134out:
135 Stream_Free(s, TRUE);
136 return error;
137}
138
144WINPR_ATTR_NODISCARD
145static UINT
146remdesk_recv_ctl_version_info_pdu(WINPR_ATTR_UNUSED RemdeskServerContext* context, wStream* s,
147 WINPR_ATTR_UNUSED const REMDESK_CHANNEL_HEADER* header)
148{
149 UINT32 versionMajor = 0;
150 UINT32 versionMinor = 0;
151
152 if (header->DataLength < 12ull)
153 {
154 WLog_ERR(TAG, "remdesk: HEADER::DataLength=%" PRIu32 ", but expected >= 12",
155 header->DataLength);
156 return ERROR_INVALID_DATA;
157 }
158
159 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
160 return ERROR_INVALID_DATA;
161
162 Stream_Read_UINT32(s, versionMajor); /* versionMajor (4 bytes) */
163 Stream_Read_UINT32(s, versionMinor); /* versionMinor (4 bytes) */
164 if ((versionMajor != 1) || (versionMinor != 2))
165 {
166 WLog_ERR(TAG, "REMOTEDESKTOP_CTL_VERSIONINFO_PACKET invalid version %" PRIu32 ".%" PRIu32,
167 versionMajor, versionMinor);
168 return ERROR_INVALID_DATA;
169 }
170
171 return CHANNEL_RC_OK;
172}
173
179WINPR_ATTR_NODISCARD
180static UINT remdesk_recv_ctl_remote_control_desktop_pdu(RemdeskServerContext* context, wStream* s,
181 const REMDESK_CHANNEL_HEADER* header)
182{
183 size_t cchStringW = 0;
184 REMDESK_CTL_REMOTE_CONTROL_DESKTOP_PDU pdu = WINPR_C_ARRAY_INIT;
185 UINT error = 0;
186
187 size_t cchMax = header->DataLength - 4ull;
188 const size_t remaining = Stream_GetRemainingLength(s);
189 if (cchMax > remaining)
190 cchMax = remaining;
191 cchMax /= sizeof(WCHAR);
192
193 const WCHAR* pStringW = Stream_ConstPointer(s);
194 const WCHAR* raConnectionStringW = pStringW;
195
196 while ((cchStringW < cchMax) && pStringW[cchStringW])
197 cchStringW++;
198
199 if ((cchStringW >= cchMax) || !cchStringW)
200 return ERROR_INVALID_DATA;
201
202 cchStringW++;
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;
208
209 WLog_INFO(TAG, "RaConnectionString: %s", pdu.raConnectionString);
210 free(pdu.raConnectionString);
211
212 if ((error = remdesk_send_ctl_result_pdu(context, 0)))
213 WLog_ERR(TAG, "remdesk_send_ctl_result_pdu failed with error %" PRIu32 "!", error);
214
215 return error;
216}
217
223WINPR_ATTR_NODISCARD
224static UINT remdesk_recv_ctl_authenticate_pdu(WINPR_ATTR_UNUSED RemdeskServerContext* context,
225 wStream* s, const REMDESK_CHANNEL_HEADER* header)
226{
227 size_t cchTmpStringW = 0;
228 const WCHAR* expertBlobW = nullptr;
229 REMDESK_CTL_AUTHENTICATE_PDU pdu = WINPR_C_ARRAY_INIT;
230
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);
236
237 const WCHAR* pStringW = Stream_ConstPointer(s);
238 const WCHAR* raConnectionStringW = pStringW;
239
240 while ((cchTmpStringW < cchRemaining) && pStringW[cchTmpStringW])
241 cchTmpStringW++;
242
243 if ((cchTmpStringW >= cchRemaining) || !cchTmpStringW)
244 return ERROR_INVALID_DATA;
245
246 cchTmpStringW++;
247 const size_t cbRaConnectionStringW = cchTmpStringW * sizeof(WCHAR);
248 pStringW += cchTmpStringW;
249 expertBlobW = pStringW;
250 cchRemaining -= cchTmpStringW;
251
252 size_t cchStringW = 0;
253 while ((cchStringW < cchRemaining) && pStringW[cchStringW])
254 cchStringW++;
255
256 if ((cchStringW >= cchRemaining) || !cchStringW)
257 return ERROR_INVALID_DATA;
258
259 cchStringW++;
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;
265
266 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW / sizeof(WCHAR), nullptr);
267 if (!pdu.expertBlob)
268 {
269 free(pdu.raConnectionString);
270 return ERROR_INTERNAL_ERROR;
271 }
272
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;
277}
278
284WINPR_ATTR_NODISCARD
285static UINT remdesk_recv_ctl_verify_password_pdu(RemdeskServerContext* context, wStream* s,
286 const REMDESK_CHANNEL_HEADER* header)
287{
288 REMDESK_CTL_VERIFY_PASSWORD_PDU pdu = WINPR_C_ARRAY_INIT;
289
290 if (header->DataLength < 12ull)
291 {
292 WLog_ERR(TAG, "remdesk: HEADER::DataLength=%" PRIu32 ", but expected >= 12",
293 header->DataLength);
294 return ERROR_INVALID_DATA;
295 }
296
297 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
298 return ERROR_INVALID_DATA;
299
300 const WCHAR* expertBlobW = Stream_ConstPointer(s);
301
302 size_t cbExpertBlobW = header->DataLength - 4;
303 const size_t remaining = Stream_GetRemainingLength(s);
304 if (cbExpertBlobW > remaining)
305 cbExpertBlobW = remaining;
306
307 pdu.expertBlob = ConvertWCharNToUtf8Alloc(expertBlobW, cbExpertBlobW / sizeof(WCHAR), nullptr);
308 if (!pdu.expertBlob)
309 return ERROR_INTERNAL_ERROR;
310
311 WLog_INFO(TAG, "ExpertBlob: %s", pdu.expertBlob);
312
313 // TODO: Callback?
314
315 free(pdu.expertBlob);
316 return remdesk_send_ctl_result_pdu(context, 0);
317}
318
324WINPR_ATTR_NODISCARD
325static UINT remdesk_recv_ctl_pdu(RemdeskServerContext* context, wStream* s,
326 const REMDESK_CHANNEL_HEADER* header)
327{
328 UINT error = CHANNEL_RC_OK;
329 UINT32 msgType = 0;
330
331 if (header->DataLength < 4)
332 {
333 WLog_ERR(TAG, "remdesk: HEADER::DataLength=%" PRIu32 ", but expected >= 4",
334 header->DataLength);
335 return ERROR_INVALID_DATA;
336 }
337
338 if (!Stream_CheckAndLogRequiredLength(TAG, s, header->DataLength))
339 return ERROR_INVALID_DATA;
340
341 Stream_Read_UINT32(s, msgType); /* msgType (4 bytes) */
342 WLog_INFO(TAG, "msgType: %" PRIu32 "", msgType);
343
344 switch (msgType)
345 {
346 case REMDESK_CTL_REMOTE_CONTROL_DESKTOP:
347 if ((error = remdesk_recv_ctl_remote_control_desktop_pdu(context, s, header)))
348 {
349 WLog_ERR(TAG,
350 "remdesk_recv_ctl_remote_control_desktop_pdu failed with error %" PRIu32
351 "!",
352 error);
353 return error;
354 }
355
356 break;
357
358 case REMDESK_CTL_AUTHENTICATE:
359 if ((error = remdesk_recv_ctl_authenticate_pdu(context, s, header)))
360 {
361 WLog_ERR(TAG, "remdesk_recv_ctl_authenticate_pdu failed with error %" PRIu32 "!",
362 error);
363 return error;
364 }
365
366 break;
367
368 case REMDESK_CTL_DISCONNECT:
369 break;
370
371 case REMDESK_CTL_VERSIONINFO:
372 if ((error = remdesk_recv_ctl_version_info_pdu(context, s, header)))
373 {
374 WLog_ERR(TAG, "remdesk_recv_ctl_version_info_pdu failed with error %" PRIu32 "!",
375 error);
376 return error;
377 }
378
379 break;
380
381 case REMDESK_CTL_ISCONNECTED:
382 break;
383
384 case REMDESK_CTL_VERIFY_PASSWORD:
385 if ((error = remdesk_recv_ctl_verify_password_pdu(context, s, header)))
386 {
387 WLog_ERR(TAG, "remdesk_recv_ctl_verify_password_pdu failed with error %" PRIu32 "!",
388 error);
389 return error;
390 }
391
392 break;
393
394 case REMDESK_CTL_EXPERT_ON_VISTA:
395 break;
396
397 case REMDESK_CTL_RANOVICE_NAME:
398 break;
399
400 case REMDESK_CTL_RAEXPERT_NAME:
401 break;
402
403 case REMDESK_CTL_TOKEN:
404 break;
405
406 default:
407 WLog_ERR(TAG, "remdesk_recv_control_pdu: unknown msgType: %" PRIu32 "", msgType);
408 error = ERROR_INVALID_DATA;
409 break;
410 }
411
412 return error;
413}
414
420WINPR_ATTR_NODISCARD
421static UINT remdesk_server_receive_pdu(RemdeskServerContext* context, wStream* s)
422{
423 REMDESK_CHANNEL_HEADER header = WINPR_C_ARRAY_INIT;
424
425 UINT error = remdesk_read_channel_header(s, &header);
426 if (error)
427 {
428 WLog_ERR(TAG, "remdesk_read_channel_header failed with error %" PRIu32 "!", error);
429 return error;
430 }
431
432 if (strcmp(header.ChannelName, "RC_CTL") == 0)
433 {
434 error = remdesk_recv_ctl_pdu(context, s, &header);
435 if (error)
436 {
437 WLog_ERR(TAG, "remdesk_recv_ctl_pdu failed with error %" PRIu32 "!", error);
438 return error;
439 }
440 }
441 else if (strcmp(header.ChannelName, "70") == 0)
442 {
443 }
444 else if (strcmp(header.ChannelName, "71") == 0)
445 {
446 }
447 else if (strcmp(header.ChannelName, ".") == 0)
448 {
449 }
450 else if (strcmp(header.ChannelName, "1000.") == 0)
451 {
452 }
453 else if (strcmp(header.ChannelName, "RA_FX") == 0)
454 {
455 }
456 else
457 {
458 }
459
460 return error;
461}
462
463WINPR_ATTR_NODISCARD
464static DWORD WINAPI remdesk_server_thread(LPVOID arg)
465{
466 void* buffer = nullptr;
467 HANDLE events[8] = WINPR_C_ARRAY_INIT;
468 HANDLE ChannelEvent = nullptr;
469 DWORD BytesReturned = 0;
470 UINT error = 0;
471 RemdeskServerContext* context = (RemdeskServerContext*)arg;
472 WINPR_ASSERT(context);
473 wStream* s = Stream_New(nullptr, 4096);
474
475 if (!s)
476 {
477 WLog_ERR(TAG, "Stream_New failed!");
478 error = CHANNEL_RC_NO_MEMORY;
479 goto out;
480 }
481
482 if (WTSVirtualChannelQuery(context->priv->ChannelHandle, WTSVirtualEventHandle, &buffer,
483 &BytesReturned) == TRUE)
484 {
485 if (BytesReturned == sizeof(HANDLE))
486 ChannelEvent = *(HANDLE*)buffer;
487
488 WTSFreeMemory(buffer);
489 }
490 else
491 {
492 WLog_ERR(TAG, "WTSVirtualChannelQuery failed!");
493 error = ERROR_INTERNAL_ERROR;
494 goto out;
495 }
496
497 {
498 DWORD nCount = 0;
499 events[nCount++] = ChannelEvent;
500 events[nCount++] = context->priv->StopEvent;
501
502 if ((error = remdesk_send_ctl_version_info_pdu(context)))
503 {
504 WLog_ERR(TAG, "remdesk_send_ctl_version_info_pdu failed with error %" PRIu32 "!",
505 error);
506 goto out;
507 }
508
509 UINT64 PduLength = 0;
510 while (1)
511 {
512 DWORD status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
513
514 if (status == WAIT_FAILED)
515 {
516 error = GetLastError();
517 WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
518 break;
519 }
520
521 status = WaitForSingleObject(context->priv->StopEvent, 0);
522
523 if (status == WAIT_FAILED)
524 {
525 error = GetLastError();
526 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
527 break;
528 }
529
530 if (status == WAIT_OBJECT_0)
531 {
532 break;
533 }
534
535 const size_t len = Stream_GetRemainingCapacity(s);
536 if (len > UINT32_MAX)
537 {
538 error = ERROR_INTERNAL_ERROR;
539 break;
540 }
541 if (!WTSVirtualChannelRead(context->priv->ChannelHandle, 0, Stream_PointerAs(s, char),
542 (UINT32)len, &BytesReturned))
543 {
544 error = ERROR_INTERNAL_ERROR;
545 break;
546 }
547 Stream_Seek(s, BytesReturned);
548
549 const size_t pos = Stream_GetPosition(s);
550 if (pos >= 8)
551 {
552 /* First run. Extract PDU length and ensure capacity */
553 if (PduLength == 0)
554 {
555 Stream_ResetPosition(s);
556 const UINT32 LengthName = Stream_Get_UINT32(s);
557 if (LengthName > 64)
558 {
559 WLog_ERR(TAG,
560 "[MS-RA] 2.2.1.2 "
561 "REMOTEDESKTOP_CTL_PACKETHEADER::ChannelNameLen(%" PRIu32
562 ")) > 64",
563 LengthName);
564 Stream_ResetPosition(s);
565 PduLength = 0;
566 continue;
567 }
568 const UINT32 LengthData = Stream_Get_UINT32(s);
569
570 /* Arbitrarily chosen length limit.
571 *
572 * Since the packets usually do not exceed a certain size we limit it to
573 * 128MB - which is far beyond anything that should ever be sent by the server.
574 */
575 const size_t DataLengthLimit = UINT32_MAX / 32;
576 if (LengthName > DataLengthLimit)
577 {
578 WLog_ERR(TAG,
579 "[MS-RA] 2.2.1.2 REMOTEDESKTOP_CTL_PACKETHEADER::DataLen(%" PRIu32
580 ")) > %" PRIuz,
581 LengthData, DataLengthLimit);
582 Stream_ResetPosition(s);
583 PduLength = 0;
584 continue;
585 }
586 if (!Stream_SetPosition(s, pos))
587 {
588 error = ERROR_INTERNAL_ERROR;
589 break;
590 }
591 PduLength = LengthName + LengthData + 8ull;
592 if (pos < PduLength)
593 {
594 if (!Stream_EnsureRemainingCapacity(s, PduLength - pos))
595 {
596 WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
597 error = CHANNEL_RC_NO_MEMORY;
598 break;
599 }
600 }
601 }
602
603 /* Check for completion.
604 * If data read, process and reset stream
605 */
606 if (pos >= PduLength)
607 {
608 Stream_SealLength(s);
609 Stream_ResetPosition(s);
610
611 error = remdesk_server_receive_pdu(context, s);
612 if (error)
613 {
614 WLog_ERR(TAG, "remdesk_server_receive_pdu failed with error %" PRIu32 "!",
615 error);
616 break;
617 }
618
619 Stream_ResetPosition(s);
620 PduLength = 0;
621 }
622 }
623 }
624 }
625out:
626 Stream_Free(s, TRUE);
627
628 if (error && context->rdpcontext)
629 setChannelError(context->rdpcontext, error, "remdesk_server_thread reported an error");
630
631 ExitThread(error);
632 return error;
633}
634
640WINPR_ATTR_NODISCARD
641static UINT remdesk_server_start(RemdeskServerContext* context)
642{
643 context->priv->ChannelHandle =
644 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, REMDESK_SVC_CHANNEL_NAME);
645
646 if (!context->priv->ChannelHandle)
647 {
648 WLog_ERR(TAG, "WTSVirtualChannelOpen failed!");
649 return ERROR_INTERNAL_ERROR;
650 }
651
652 if (!(context->priv->StopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
653 {
654 WLog_ERR(TAG, "CreateEvent failed!");
655 return ERROR_INTERNAL_ERROR;
656 }
657
658 if (!(context->priv->Thread =
659 CreateThread(nullptr, 0, remdesk_server_thread, (void*)context, 0, nullptr)))
660 {
661 WLog_ERR(TAG, "CreateThread failed!");
662 (void)CloseHandle(context->priv->StopEvent);
663 context->priv->StopEvent = nullptr;
664 return ERROR_INTERNAL_ERROR;
665 }
666
667 return CHANNEL_RC_OK;
668}
669
675WINPR_ATTR_NODISCARD
676static UINT remdesk_server_stop(RemdeskServerContext* context)
677{
678 UINT error = 0;
679 (void)SetEvent(context->priv->StopEvent);
680
681 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
682 {
683 error = GetLastError();
684 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
685 return error;
686 }
687
688 (void)CloseHandle(context->priv->Thread);
689 (void)CloseHandle(context->priv->StopEvent);
690 return CHANNEL_RC_OK;
691}
692
693RemdeskServerContext* remdesk_server_context_new(HANDLE vcm)
694{
695 RemdeskServerContext* context = nullptr;
696 context = (RemdeskServerContext*)calloc(1, sizeof(RemdeskServerContext));
697
698 if (context)
699 {
700 context->vcm = vcm;
701 context->Start = remdesk_server_start;
702 context->Stop = remdesk_server_stop;
703 context->priv = (RemdeskServerPrivate*)calloc(1, sizeof(RemdeskServerPrivate));
704
705 if (!context->priv)
706 {
707 free(context);
708 return nullptr;
709 }
710
711 context->priv->Version = 1;
712 }
713
714 return context;
715}
716
717void remdesk_server_context_free(RemdeskServerContext* context)
718{
719 if (context)
720 {
721 if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
722 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
723
724 free(context->priv);
725 free(context);
726 }
727}