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 while (1)
510 {
511 DWORD status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
512
513 if (status == WAIT_FAILED)
514 {
515 error = GetLastError();
516 WLog_ERR(TAG, "WaitForMultipleObjects failed with error %" PRIu32 "", error);
517 break;
518 }
519
520 status = WaitForSingleObject(context->priv->StopEvent, 0);
521
522 if (status == WAIT_FAILED)
523 {
524 error = GetLastError();
525 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "", error);
526 break;
527 }
528
529 if (status == WAIT_OBJECT_0)
530 {
531 break;
532 }
533
534 const size_t len = Stream_Capacity(s);
535 if (len > UINT32_MAX)
536 {
537 error = ERROR_INTERNAL_ERROR;
538 break;
539 }
540 if (WTSVirtualChannelRead(context->priv->ChannelHandle, 0, Stream_BufferAs(s, char),
541 (UINT32)len, &BytesReturned))
542 {
543 if (BytesReturned)
544 Stream_Seek(s, BytesReturned);
545 }
546 else
547 {
548 if (!Stream_EnsureRemainingCapacity(s, BytesReturned))
549 {
550 WLog_ERR(TAG, "Stream_EnsureRemainingCapacity failed!");
551 error = CHANNEL_RC_NO_MEMORY;
552 break;
553 }
554 }
555
556 const size_t pos = Stream_GetPosition(s);
557 if (pos >= 8)
558 {
559 Stream_ResetPosition(s);
560 const UINT32 LengthLow = Stream_Get_UINT32(s);
561 const UINT32 LengthHigh = Stream_Get_UINT32(s);
562 if (!Stream_SetPosition(s, pos))
563 {
564 error = ERROR_INTERNAL_ERROR;
565 break;
566 }
567 const UINT64 PduLength = LengthLow + LengthHigh + 8ull;
568
569 if (PduLength >= pos)
570 {
571 Stream_SealLength(s);
572 Stream_ResetPosition(s);
573
574 error = remdesk_server_receive_pdu(context, s);
575 if (error)
576 {
577 WLog_ERR(TAG, "remdesk_server_receive_pdu failed with error %" PRIu32 "!",
578 error);
579 break;
580 }
581
582 Stream_ResetPosition(s);
583 }
584 }
585 }
586 }
587out:
588 Stream_Free(s, TRUE);
589
590 if (error && context->rdpcontext)
591 setChannelError(context->rdpcontext, error, "remdesk_server_thread reported an error");
592
593 ExitThread(error);
594 return error;
595}
596
602WINPR_ATTR_NODISCARD
603static UINT remdesk_server_start(RemdeskServerContext* context)
604{
605 context->priv->ChannelHandle =
606 WTSVirtualChannelOpen(context->vcm, WTS_CURRENT_SESSION, REMDESK_SVC_CHANNEL_NAME);
607
608 if (!context->priv->ChannelHandle)
609 {
610 WLog_ERR(TAG, "WTSVirtualChannelOpen failed!");
611 return ERROR_INTERNAL_ERROR;
612 }
613
614 if (!(context->priv->StopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr)))
615 {
616 WLog_ERR(TAG, "CreateEvent failed!");
617 return ERROR_INTERNAL_ERROR;
618 }
619
620 if (!(context->priv->Thread =
621 CreateThread(nullptr, 0, remdesk_server_thread, (void*)context, 0, nullptr)))
622 {
623 WLog_ERR(TAG, "CreateThread failed!");
624 (void)CloseHandle(context->priv->StopEvent);
625 context->priv->StopEvent = nullptr;
626 return ERROR_INTERNAL_ERROR;
627 }
628
629 return CHANNEL_RC_OK;
630}
631
637WINPR_ATTR_NODISCARD
638static UINT remdesk_server_stop(RemdeskServerContext* context)
639{
640 UINT error = 0;
641 (void)SetEvent(context->priv->StopEvent);
642
643 if (WaitForSingleObject(context->priv->Thread, INFINITE) == WAIT_FAILED)
644 {
645 error = GetLastError();
646 WLog_ERR(TAG, "WaitForSingleObject failed with error %" PRIu32 "!", error);
647 return error;
648 }
649
650 (void)CloseHandle(context->priv->Thread);
651 (void)CloseHandle(context->priv->StopEvent);
652 return CHANNEL_RC_OK;
653}
654
655RemdeskServerContext* remdesk_server_context_new(HANDLE vcm)
656{
657 RemdeskServerContext* context = nullptr;
658 context = (RemdeskServerContext*)calloc(1, sizeof(RemdeskServerContext));
659
660 if (context)
661 {
662 context->vcm = vcm;
663 context->Start = remdesk_server_start;
664 context->Stop = remdesk_server_stop;
665 context->priv = (RemdeskServerPrivate*)calloc(1, sizeof(RemdeskServerPrivate));
666
667 if (!context->priv)
668 {
669 free(context);
670 return nullptr;
671 }
672
673 context->priv->Version = 1;
674 }
675
676 return context;
677}
678
679void remdesk_server_context_free(RemdeskServerContext* context)
680{
681 if (context)
682 {
683 if (context->priv->ChannelHandle != INVALID_HANDLE_VALUE)
684 (void)WTSVirtualChannelClose(context->priv->ChannelHandle);
685
686 free(context->priv);
687 free(context);
688 }
689}