FreeRDP
Loading...
Searching...
No Matches
server/cliprdr_main.c
1
22#include <freerdp/config.h>
23
24#include <winpr/crt.h>
25#include <winpr/assert.h>
26#include <winpr/print.h>
27#include <winpr/stream.h>
28
29#include <freerdp/freerdp.h>
30#include <freerdp/channels/log.h>
31#include "cliprdr_main.h"
32#include "../cliprdr_common.h"
33
79static UINT cliprdr_server_packet_send(CliprdrServerPrivate* cliprdr, wStream* s)
80{
81 UINT rc = 0;
82 BOOL status = 0;
83 ULONG written = 0;
84
85 WINPR_ASSERT(cliprdr);
86
87 const size_t pos = Stream_GetPosition(s);
88 if ((pos < 8) || (pos > UINT32_MAX))
89 {
90 rc = ERROR_NO_DATA;
91 goto fail;
92 }
93
94 const UINT32 dataLen = (UINT32)(pos - 8);
95 if (!Stream_SetPosition(s, 4))
96 goto fail;
97 Stream_Write_UINT32(s, dataLen);
98
99 WINPR_ASSERT(pos <= UINT32_MAX);
100 status = WTSVirtualChannelWrite(cliprdr->ChannelHandle, Stream_BufferAs(s, char), (UINT32)pos,
101 &written);
102 rc = status ? CHANNEL_RC_OK : ERROR_INTERNAL_ERROR;
103fail:
104 Stream_Free(s, TRUE);
105 return rc;
106}
107
113static UINT cliprdr_server_capabilities(CliprdrServerContext* context,
114 const CLIPRDR_CAPABILITIES* capabilities)
115{
116 size_t offset = 0;
117
118 WINPR_ASSERT(context);
119 WINPR_ASSERT(capabilities);
120
121 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
122
123 if (capabilities->common.msgType != CB_CLIP_CAPS)
124 WLog_Print(cliprdr->log, WLOG_WARN, "called with invalid type %08" PRIx32,
125 capabilities->common.msgType);
126
127 if (capabilities->cCapabilitiesSets > UINT16_MAX)
128 {
129 WLog_Print(cliprdr->log, WLOG_ERROR, "Invalid number of capability sets in clipboard caps");
130 return ERROR_INVALID_PARAMETER;
131 }
132
133 wStream* s = cliprdr_packet_new(CB_CLIP_CAPS, 0, 4 + CB_CAPSTYPE_GENERAL_LEN);
134
135 if (!s)
136 {
137 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
138 return ERROR_INTERNAL_ERROR;
139 }
140
141 Stream_Write_UINT16(s,
142 (UINT16)capabilities->cCapabilitiesSets); /* cCapabilitiesSets (2 bytes) */
143 Stream_Write_UINT16(s, 0); /* pad1 (2 bytes) */
144 for (UINT32 x = 0; x < capabilities->cCapabilitiesSets; x++)
145 {
146 const CLIPRDR_CAPABILITY_SET* cap =
147 (const CLIPRDR_CAPABILITY_SET*)(((const BYTE*)capabilities->capabilitySets) + offset);
148 offset += cap->capabilitySetLength;
149
150 switch (cap->capabilitySetType)
151 {
152 case CB_CAPSTYPE_GENERAL:
153 {
154 const CLIPRDR_GENERAL_CAPABILITY_SET* generalCapabilitySet =
156 Stream_Write_UINT16(
157 s, generalCapabilitySet->capabilitySetType); /* capabilitySetType (2 bytes) */
158 Stream_Write_UINT16(
159 s, generalCapabilitySet->capabilitySetLength); /* lengthCapability (2 bytes) */
160 Stream_Write_UINT32(s, generalCapabilitySet->version); /* version (4 bytes) */
161 Stream_Write_UINT32(
162 s, generalCapabilitySet->generalFlags); /* generalFlags (4 bytes) */
163 }
164 break;
165
166 default:
167 WLog_Print(cliprdr->log, WLOG_WARN, "Unknown capability set type %08" PRIx16,
168 cap->capabilitySetType);
169 if (!Stream_SafeSeek(s, cap->capabilitySetLength))
170 {
171 WLog_Print(cliprdr->log, WLOG_ERROR, "short stream");
172 Stream_Free(s, TRUE);
173 return ERROR_NO_DATA;
174 }
175 break;
176 }
177 }
178 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerCapabilities");
179 return cliprdr_server_packet_send(cliprdr, s);
180}
181
187static UINT cliprdr_server_monitor_ready(CliprdrServerContext* context,
188 const CLIPRDR_MONITOR_READY* monitorReady)
189{
190 WINPR_ASSERT(context);
191 WINPR_ASSERT(monitorReady);
192
193 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
194
195 if (monitorReady->common.msgType != CB_MONITOR_READY)
196 WLog_Print(cliprdr->log, WLOG_WARN, "called with invalid type %08" PRIx32,
197 monitorReady->common.msgType);
198
199 wStream* s = cliprdr_packet_new(CB_MONITOR_READY, monitorReady->common.msgFlags,
200 monitorReady->common.dataLen);
201
202 if (!s)
203 {
204 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
205 return ERROR_INTERNAL_ERROR;
206 }
207
208 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerMonitorReady");
209 return cliprdr_server_packet_send(cliprdr, s);
210}
211
217static UINT cliprdr_server_format_list(CliprdrServerContext* context,
218 const CLIPRDR_FORMAT_LIST* formatList)
219{
220 WINPR_ASSERT(context);
221 WINPR_ASSERT(formatList);
222
223 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
224
225 wStream* s = cliprdr_packet_format_list_new(formatList, context->useLongFormatNames, FALSE);
226 if (!s)
227 {
228 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_format_list_new failed!");
229 return ERROR_INTERNAL_ERROR;
230 }
231
232 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFormatList: numFormats: %" PRIu32 "",
233 formatList->numFormats);
234 return cliprdr_server_packet_send(cliprdr, s);
235}
236
242static UINT
243cliprdr_server_format_list_response(CliprdrServerContext* context,
244 const CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse)
245{
246 WINPR_ASSERT(context);
247 WINPR_ASSERT(formatListResponse);
248
249 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
250 if (formatListResponse->common.msgType != CB_FORMAT_LIST_RESPONSE)
251 WLog_Print(cliprdr->log, WLOG_WARN, "called with invalid type %08" PRIx32,
252 formatListResponse->common.msgType);
253
254 wStream* s = cliprdr_packet_new(CB_FORMAT_LIST_RESPONSE, formatListResponse->common.msgFlags,
255 formatListResponse->common.dataLen);
256
257 if (!s)
258 {
259 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
260 return ERROR_INTERNAL_ERROR;
261 }
262
263 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFormatListResponse");
264 return cliprdr_server_packet_send(cliprdr, s);
265}
266
272static UINT cliprdr_server_lock_clipboard_data(CliprdrServerContext* context,
273 const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
274{
275 WINPR_ASSERT(context);
276 WINPR_ASSERT(lockClipboardData);
277
278 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
279 if (lockClipboardData->common.msgType != CB_LOCK_CLIPDATA)
280 WLog_Print(cliprdr->log, WLOG_WARN, "called with invalid type %08" PRIx32,
281 lockClipboardData->common.msgType);
282
283 wStream* s = cliprdr_packet_lock_clipdata_new(lockClipboardData);
284 if (!s)
285 {
286 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_lock_clipdata_new failed!");
287 return ERROR_INTERNAL_ERROR;
288 }
289
290 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerLockClipboardData: clipDataId: 0x%08" PRIX32 "",
291 lockClipboardData->clipDataId);
292 return cliprdr_server_packet_send(cliprdr, s);
293}
294
300static UINT
301cliprdr_server_unlock_clipboard_data(CliprdrServerContext* context,
302 const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
303{
304 WINPR_ASSERT(context);
305 WINPR_ASSERT(unlockClipboardData);
306
307 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
308 if (unlockClipboardData->common.msgType != CB_UNLOCK_CLIPDATA)
309 WLog_Print(cliprdr->log, WLOG_WARN, "called with invalid type %08" PRIx32,
310 unlockClipboardData->common.msgType);
311
312 wStream* s = cliprdr_packet_unlock_clipdata_new(unlockClipboardData);
313
314 if (!s)
315 {
316 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_unlock_clipdata_new failed!");
317 return ERROR_INTERNAL_ERROR;
318 }
319
320 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerUnlockClipboardData: clipDataId: 0x%08" PRIX32 "",
321 unlockClipboardData->clipDataId);
322 return cliprdr_server_packet_send(cliprdr, s);
323}
324
330static UINT cliprdr_server_format_data_request(CliprdrServerContext* context,
331 const CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
332{
333 WINPR_ASSERT(context);
334 WINPR_ASSERT(formatDataRequest);
335
336 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
337 if (formatDataRequest->common.msgType != CB_FORMAT_DATA_REQUEST)
338 WLog_Print(cliprdr->log, WLOG_WARN, "called with invalid type %08" PRIx32,
339 formatDataRequest->common.msgType);
340
341 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_REQUEST, formatDataRequest->common.msgFlags,
342 formatDataRequest->common.dataLen);
343
344 if (!s)
345 {
346 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
347 return ERROR_INTERNAL_ERROR;
348 }
349
350 Stream_Write_UINT32(s, formatDataRequest->requestedFormatId); /* requestedFormatId (4 bytes) */
351 WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFormatDataRequest");
352 return cliprdr_server_packet_send(cliprdr, s);
353}
354
360static UINT
361cliprdr_server_format_data_response(CliprdrServerContext* context,
362 const CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse)
363{
364 WINPR_ASSERT(context);
365 WINPR_ASSERT(formatDataResponse);
366
367 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
368
369 if (formatDataResponse->common.msgType != CB_FORMAT_DATA_RESPONSE)
370 WLog_Print(cliprdr->log, WLOG_WARN, "called with invalid type %08" PRIx32,
371 formatDataResponse->common.msgType);
372
373 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_RESPONSE, formatDataResponse->common.msgFlags,
374 formatDataResponse->common.dataLen);
375
376 if (!s)
377 {
378 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
379 return ERROR_INTERNAL_ERROR;
380 }
381
382 Stream_Write(s, formatDataResponse->requestedFormatData, formatDataResponse->common.dataLen);
383 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFormatDataResponse");
384 return cliprdr_server_packet_send(cliprdr, s);
385}
386
392static UINT
393cliprdr_server_file_contents_request(CliprdrServerContext* context,
394 const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
395{
396 WINPR_ASSERT(context);
397 WINPR_ASSERT(fileContentsRequest);
398
399 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
400
401 if (fileContentsRequest->common.msgType != CB_FILECONTENTS_REQUEST)
402 WLog_Print(cliprdr->log, WLOG_WARN, "called with invalid type %08" PRIx32,
403 fileContentsRequest->common.msgType);
404
405 wStream* s = cliprdr_packet_file_contents_request_new(fileContentsRequest);
406 if (!s)
407 {
408 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_file_contents_request_new failed!");
409 return ERROR_INTERNAL_ERROR;
410 }
411
412 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFileContentsRequest: streamId: 0x%08" PRIX32 "",
413 fileContentsRequest->streamId);
414 return cliprdr_server_packet_send(cliprdr, s);
415}
416
422static UINT
423cliprdr_server_file_contents_response(CliprdrServerContext* context,
424 const CLIPRDR_FILE_CONTENTS_RESPONSE* fileContentsResponse)
425{
426 WINPR_ASSERT(context);
427 WINPR_ASSERT(fileContentsResponse);
428
429 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
430 WINPR_ASSERT(cliprdr);
431
432 if (fileContentsResponse->common.msgType != CB_FILECONTENTS_RESPONSE)
433 WLog_Print(cliprdr->log, WLOG_WARN, "called with invalid type %08" PRIx32,
434 fileContentsResponse->common.msgType);
435
436 wStream* s = cliprdr_packet_file_contents_response_new(fileContentsResponse);
437 if (!s)
438 {
439 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_file_contents_response_new failed!");
440 return ERROR_INTERNAL_ERROR;
441 }
442
443 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerFileContentsResponse: streamId: 0x%08" PRIX32 "",
444 fileContentsResponse->streamId);
445 return cliprdr_server_packet_send(cliprdr, s);
446}
447
453static UINT cliprdr_server_receive_general_capability(CliprdrServerContext* context, wStream* s,
455{
456 WINPR_ASSERT(context);
457 WINPR_ASSERT(cap_set);
458
459 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
460 WINPR_ASSERT(cliprdr);
461
462 if (cap_set->capabilitySetLength != sizeof(CLIPRDR_GENERAL_CAPABILITY_SET))
463 {
464 WLog_Print(cliprdr->log, WLOG_ERROR,
465 "invalid capabilitySetLength %" PRIu16 " != %" PRIuz
466 " (capabilitySetType=CB_CAPSTYPE_GENERAL)",
467 cap_set->capabilitySetLength, sizeof(CLIPRDR_GENERAL_CAPABILITY_SET));
468 return ERROR_INVALID_DATA;
469 }
470
471 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
472 return ERROR_INVALID_DATA;
473
474 Stream_Read_UINT32(s, cap_set->version); /* version (4 bytes) */
475 Stream_Read_UINT32(s, cap_set->generalFlags); /* generalFlags (4 bytes) */
476
477 if (context->useLongFormatNames)
478 context->useLongFormatNames = (cap_set->generalFlags & CB_USE_LONG_FORMAT_NAMES) != 0;
479
480 if (context->streamFileClipEnabled)
481 context->streamFileClipEnabled = (cap_set->generalFlags & CB_STREAM_FILECLIP_ENABLED) != 0;
482
483 if (context->fileClipNoFilePaths)
484 context->fileClipNoFilePaths = (cap_set->generalFlags & CB_FILECLIP_NO_FILE_PATHS) != 0;
485
486 if (context->canLockClipData)
487 context->canLockClipData = (cap_set->generalFlags & CB_CAN_LOCK_CLIPDATA) != 0;
488
489 if (context->hasHugeFileSupport)
490 context->hasHugeFileSupport = (cap_set->generalFlags & CB_HUGE_FILE_SUPPORT_ENABLED) != 0;
491
492 return CHANNEL_RC_OK;
493}
494
495WINPR_ATTR_NODISCARD
496static BOOL cliprdr_capabilities_contain_capset(wLog* log, const CLIPRDR_CAPABILITIES* capabilities,
497 size_t capabilitiesSize,
498 const CLIPRDR_CAPABILITY_SET* capset)
499{
500 WINPR_ASSERT(capabilities);
501
502 const BYTE* offset = (const BYTE*)capabilities->capabilitySets;
503 size_t byteOffset = 0;
504 for (UINT32 x = 0; x < capabilities->cCapabilitiesSets; x++)
505 {
506 const CLIPRDR_CAPABILITY_SET* cur = (const CLIPRDR_CAPABILITY_SET*)offset;
507 byteOffset += cur->capabilitySetLength;
508 if (byteOffset > capabilitiesSize)
509 return FALSE;
510
511 offset += cur->capabilitySetLength;
512
513 if (cur->capabilitySetType == capset->capabilitySetType)
514 {
515 WLog_Print(log, WLOG_ERROR, "duplicate CLIPRDR_CAPABILITY_SET %" PRIu16,
516 cur->capabilitySetType);
517 return TRUE;
518 }
519 }
520 return FALSE;
521}
522
528static UINT cliprdr_server_receive_capabilities(CliprdrServerContext* context, wStream* s,
529 const CLIPRDR_HEADER* header)
530{
531 UINT error = ERROR_INVALID_DATA;
532 size_t cap_sets_size = 0;
533 CLIPRDR_CAPABILITIES capabilities = WINPR_C_ARRAY_INIT;
534
535 WINPR_ASSERT(context);
536 WINPR_UNUSED(header);
537
538 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
539 WINPR_ASSERT(cliprdr);
540
541 WLog_Print(cliprdr->log, WLOG_DEBUG, "CliprdrClientCapabilities");
542 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
543 return ERROR_INVALID_DATA;
544
545 const UINT16 cCapabilitiesSets = Stream_Get_UINT16(s); /* cCapabilitiesSets (2 bytes) */
546 Stream_Seek_UINT16(s); /* pad1 (2 bytes) */
547
548 for (size_t index = 0; index < cCapabilitiesSets; index++)
549 {
550 const size_t cap_set_offset = cap_sets_size;
551 if (!Stream_CheckAndLogRequiredLength(TAG, s, sizeof(CLIPRDR_CAPABILITY_SET)))
552 goto out;
553 const UINT16 capabilitySetType = Stream_Get_UINT16(s); /* capabilitySetType (2 bytes) */
554 const UINT16 capabilitySetLength = Stream_Get_UINT16(s); /* capabilitySetLength (2 bytes) */
555
556 if (capabilitySetLength < sizeof(CLIPRDR_CAPABILITY_SET))
557 {
558 WLog_Print(cliprdr->log, WLOG_ERROR,
559 "invalid capabilitySetLength %" PRIu16 " < %" PRIuz
560 " (capabilitySetType=%" PRIu16 ")",
561 capabilitySetLength, sizeof(CLIPRDR_CAPABILITY_SET), capabilitySetType);
562 goto out;
563 }
564
565 if (!Stream_CheckAndLogRequiredLength(
566 TAG, s, (size_t)capabilitySetLength - sizeof(CLIPRDR_CAPABILITY_SET)))
567 goto out;
568
569 cap_sets_size += capabilitySetLength;
570
571 CLIPRDR_CAPABILITY_SET* tmp = realloc(capabilities.capabilitySets, cap_sets_size);
572 if (tmp == nullptr)
573 {
574 WLog_Print(cliprdr->log, WLOG_ERROR, "capabilities.capabilitySets realloc failed!");
575 free(capabilities.capabilitySets);
576 return CHANNEL_RC_NO_MEMORY;
577 }
578
579 capabilities.capabilitySets = tmp;
580
581 CLIPRDR_CAPABILITY_SET* capSet =
582 (CLIPRDR_CAPABILITY_SET*)(((BYTE*)capabilities.capabilitySets) + cap_set_offset);
583
584 capSet->capabilitySetType = capabilitySetType;
585 capSet->capabilitySetLength = capabilitySetLength;
586
587 if (cliprdr_capabilities_contain_capset(cliprdr->log, &capabilities, cap_sets_size, capSet))
588 goto out;
589
590 capabilities.cCapabilitiesSets++;
591
592 switch (capSet->capabilitySetType)
593 {
594 case CB_CAPSTYPE_GENERAL:
595 error = cliprdr_server_receive_general_capability(
596 context, s, (CLIPRDR_GENERAL_CAPABILITY_SET*)capSet);
597 if (error)
598 {
599 WLog_Print(
600 cliprdr->log, WLOG_ERROR,
601 "cliprdr_server_receive_general_capability failed with error %" PRIu32 "",
602 error);
603 goto out;
604 }
605 break;
606
607 default:
608 WLog_Print(cliprdr->log, WLOG_ERROR, "unknown cliprdr capability set: %" PRIu16 "",
609 capSet->capabilitySetType);
610 goto out;
611 }
612 }
613
614 error = CHANNEL_RC_OK;
615 IFCALLRET(context->ClientCapabilities, error, context, &capabilities);
616out:
617 free(capabilities.capabilitySets);
618 return error;
619}
620
626static UINT cliprdr_server_receive_temporary_directory(CliprdrServerContext* context, wStream* s,
627 const CLIPRDR_HEADER* header)
628{
629 UINT error = CHANNEL_RC_OK;
630
631 WINPR_ASSERT(context);
632 WINPR_UNUSED(header);
633
634 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
635 WINPR_ASSERT(cliprdr);
636
637 if (!Stream_CheckAndLogRequiredLength(TAG, s,
638 ARRAYSIZE(cliprdr->temporaryDirectory) * sizeof(WCHAR)))
639 return CHANNEL_RC_NO_MEMORY;
640
641 const WCHAR* wszTempDir = Stream_ConstPointer(s);
642
643 if (wszTempDir[ARRAYSIZE(cliprdr->temporaryDirectory) - 1] != 0)
644 {
645 WLog_Print(cliprdr->log, WLOG_ERROR, "wszTempDir[259] was not 0");
646 return ERROR_INVALID_DATA;
647 }
648
649 if (ConvertWCharNToUtf8(wszTempDir, ARRAYSIZE(cliprdr->temporaryDirectory),
650 cliprdr->temporaryDirectory,
651 ARRAYSIZE(cliprdr->temporaryDirectory)) < 0)
652 {
653 WLog_Print(cliprdr->log, WLOG_ERROR, "failed to convert temporary directory name");
654 return ERROR_INVALID_DATA;
655 }
656
657 size_t length = strnlen(cliprdr->temporaryDirectory, ARRAYSIZE(cliprdr->temporaryDirectory));
658
659 if (length >= ARRAYSIZE(cliprdr->temporaryDirectory))
660 length = ARRAYSIZE(cliprdr->temporaryDirectory) - 1;
661
662 CLIPRDR_TEMP_DIRECTORY tempDirectory = WINPR_C_ARRAY_INIT;
663 CopyMemory(tempDirectory.szTempDir, cliprdr->temporaryDirectory, length);
664 tempDirectory.szTempDir[length] = '\0';
665 WLog_Print(cliprdr->log, WLOG_DEBUG, "CliprdrTemporaryDirectory: %s",
666 cliprdr->temporaryDirectory);
667 IFCALLRET(context->TempDirectory, error, context, &tempDirectory);
668
669 if (error)
670 WLog_Print(cliprdr->log, WLOG_ERROR, "TempDirectory failed with error %" PRIu32 "!", error);
671
672 return error;
673}
674
680static UINT cliprdr_server_receive_format_list(CliprdrServerContext* context, wStream* s,
681 const CLIPRDR_HEADER* header)
682{
683 UINT error = CHANNEL_RC_OK;
684
685 WINPR_ASSERT(context);
686 WINPR_ASSERT(header);
687
688 CLIPRDR_FORMAT_LIST formatList = { .common = { .msgType = CB_FORMAT_LIST,
689 .msgFlags = header->msgFlags,
690 .dataLen = header->dataLen },
691 .numFormats = 0,
692 .formats = nullptr };
693 wLog* log = WLog_Get(TAG);
694 if ((error = cliprdr_read_format_list(log, s, &formatList, context->useLongFormatNames)))
695 goto out;
696
697 WLog_Print(log, WLOG_DEBUG, "ClientFormatList: numFormats: %" PRIu32 "", formatList.numFormats);
698 IFCALLRET(context->ClientFormatList, error, context, &formatList);
699
700 if (error)
701 WLog_Print(log, WLOG_ERROR, "ClientFormatList failed with error %" PRIu32 "!", error);
702
703out:
704 cliprdr_free_format_list(&formatList);
705 return error;
706}
707
713static UINT cliprdr_server_receive_format_list_response(CliprdrServerContext* context, wStream* s,
714 const CLIPRDR_HEADER* header)
715{
716 UINT error = CHANNEL_RC_OK;
717
718 WINPR_ASSERT(context);
719 WINPR_ASSERT(header);
720
721 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
722 WINPR_ASSERT(cliprdr);
723
724 WINPR_UNUSED(s);
725 WLog_Print(cliprdr->log, WLOG_DEBUG, "CliprdrClientFormatListResponse");
726
727 CLIPRDR_FORMAT_LIST_RESPONSE formatListResponse = { .common = { .msgType =
728 CB_FORMAT_LIST_RESPONSE,
729 .msgFlags = header->msgFlags,
730 .dataLen = header->dataLen } };
731 IFCALLRET(context->ClientFormatListResponse, error, context, &formatListResponse);
732
733 if (error)
734 WLog_Print(cliprdr->log, WLOG_ERROR,
735 "ClientFormatListResponse failed with error %" PRIu32 "!", error);
736
737 return error;
738}
739
745static UINT cliprdr_server_receive_lock_clipdata(CliprdrServerContext* context, wStream* s,
746 const CLIPRDR_HEADER* header)
747{
748 UINT error = CHANNEL_RC_OK;
749
750 WINPR_ASSERT(context);
751 WINPR_ASSERT(header);
752
753 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
754 WINPR_ASSERT(cliprdr);
755
756 WLog_Print(cliprdr->log, WLOG_DEBUG, "CliprdrClientLockClipData");
757
758 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
759 return ERROR_INVALID_DATA;
760
761 CLIPRDR_LOCK_CLIPBOARD_DATA lockClipboardData = { .common = { .msgType = CB_LOCK_CLIPDATA,
762 .msgFlags = header->msgFlags,
763 .dataLen = header->dataLen },
764 .clipDataId = 0 };
765
766 Stream_Read_UINT32(s, lockClipboardData.clipDataId); /* clipDataId (4 bytes) */
767 IFCALLRET(context->ClientLockClipboardData, error, context, &lockClipboardData);
768
769 if (error)
770 WLog_Print(cliprdr->log, WLOG_ERROR,
771 "ClientLockClipboardData failed with error %" PRIu32 "!", error);
772
773 return error;
774}
775
781static UINT cliprdr_server_receive_unlock_clipdata(CliprdrServerContext* context, wStream* s,
782 const CLIPRDR_HEADER* header)
783{
784 WINPR_ASSERT(context);
785 WINPR_ASSERT(header);
786
787 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
788 WINPR_ASSERT(cliprdr);
789
790 WLog_Print(cliprdr->log, WLOG_DEBUG, "CliprdrClientUnlockClipData");
791
792 CLIPRDR_UNLOCK_CLIPBOARD_DATA unlockClipboardData = { .common = { .msgType = CB_UNLOCK_CLIPDATA,
793 .msgFlags = header->msgFlags,
794 .dataLen = header->dataLen },
795 .clipDataId = 0 };
796
797 UINT error = cliprdr_read_unlock_clipdata(s, &unlockClipboardData);
798 if (error)
799 return error;
800
801 IFCALLRET(context->ClientUnlockClipboardData, error, context, &unlockClipboardData);
802
803 if (error)
804 WLog_Print(cliprdr->log, WLOG_ERROR,
805 "ClientUnlockClipboardData failed with error %" PRIu32 "!", error);
806
807 return error;
808}
809
815static UINT cliprdr_server_receive_format_data_request(CliprdrServerContext* context, wStream* s,
816 const CLIPRDR_HEADER* header)
817{
818 WINPR_ASSERT(context);
819 WINPR_ASSERT(header);
820
821 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
822 WINPR_ASSERT(cliprdr);
823
824 WLog_Print(cliprdr->log, WLOG_DEBUG, "CliprdrClientFormatDataRequest");
825
826 CLIPRDR_FORMAT_DATA_REQUEST formatDataRequest = { .common = { .msgType = CB_FORMAT_DATA_REQUEST,
827 .msgFlags = header->msgFlags,
828 .dataLen = header->dataLen },
829 .requestedFormatId = 0 };
830
831 UINT error = cliprdr_read_format_data_request(s, &formatDataRequest);
832 if (error)
833 return error;
834
835 context->lastRequestedFormatId = formatDataRequest.requestedFormatId;
836 IFCALLRET(context->ClientFormatDataRequest, error, context, &formatDataRequest);
837
838 if (error)
839 WLog_Print(cliprdr->log, WLOG_ERROR,
840 "ClientFormatDataRequest failed with error %" PRIu32 "!", error);
841
842 return error;
843}
844
850static UINT cliprdr_server_receive_format_data_response(CliprdrServerContext* context, wStream* s,
851 const CLIPRDR_HEADER* header)
852{
853 WINPR_ASSERT(context);
854 WINPR_ASSERT(header);
855
856 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
857 WINPR_ASSERT(cliprdr);
858
859 WLog_Print(cliprdr->log, WLOG_DEBUG, "CliprdrClientFormatDataResponse");
860
861 CLIPRDR_FORMAT_DATA_RESPONSE formatDataResponse = { .common = { .msgType =
862 CB_FORMAT_DATA_RESPONSE,
863 .msgFlags = header->msgFlags,
864 .dataLen = header->dataLen },
865 .requestedFormatData = nullptr };
866
867 UINT error = cliprdr_read_format_data_response(s, &formatDataResponse);
868 if (error)
869 return error;
870
871 IFCALLRET(context->ClientFormatDataResponse, error, context, &formatDataResponse);
872
873 if (error)
874 WLog_Print(cliprdr->log, WLOG_ERROR,
875 "ClientFormatDataResponse failed with error %" PRIu32 "!", error);
876
877 return error;
878}
879
885static UINT cliprdr_server_receive_filecontents_request(CliprdrServerContext* context, wStream* s,
886 const CLIPRDR_HEADER* header)
887{
888 WINPR_ASSERT(context);
889 WINPR_ASSERT(header);
890
891 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
892 WINPR_ASSERT(cliprdr);
893
894 WLog_Print(cliprdr->log, WLOG_DEBUG, "CliprdrClientFileContentsRequest");
895
896 CLIPRDR_FILE_CONTENTS_REQUEST request = { .common = { .msgType = CB_FILECONTENTS_REQUEST,
897 .msgFlags = header->msgFlags,
898 .dataLen = header->dataLen },
899 .streamId = 0,
900 .listIndex = 0,
901 .dwFlags = 0,
902 .nPositionLow = 0,
903 .nPositionHigh = 0,
904 .cbRequested = 0,
905 .haveClipDataId = FALSE,
906 .clipDataId = 0 };
907
908 UINT error = cliprdr_read_file_contents_request(s, &request);
909 if (error)
910 return error;
911
912 if (!context->hasHugeFileSupport)
913 {
914 if (request.nPositionHigh > 0)
915 return ERROR_INVALID_DATA;
916 if ((UINT64)request.nPositionLow + request.cbRequested > UINT32_MAX)
917 return ERROR_INVALID_DATA;
918 }
919 IFCALLRET(context->ClientFileContentsRequest, error, context, &request);
920
921 if (error)
922 WLog_Print(cliprdr->log, WLOG_ERROR,
923 "ClientFileContentsRequest failed with error %" PRIu32 "!", error);
924
925 return error;
926}
927
933static UINT cliprdr_server_receive_filecontents_response(CliprdrServerContext* context, wStream* s,
934 const CLIPRDR_HEADER* header)
935{
936 UINT error = CHANNEL_RC_OK;
937
938 WINPR_ASSERT(context);
939 WINPR_ASSERT(header);
940
941 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
942 WINPR_ASSERT(cliprdr);
943
944 WLog_Print(cliprdr->log, WLOG_DEBUG, "CliprdrClientFileContentsResponse");
945
946 CLIPRDR_FILE_CONTENTS_RESPONSE response = { .common = { .msgType = CB_FILECONTENTS_RESPONSE,
947 .msgFlags = header->msgFlags,
948 .dataLen = header->dataLen },
949 .streamId = 0,
950 .cbRequested = 0,
951 .requestedData = nullptr };
952
953 if ((error = cliprdr_read_file_contents_response(s, &response)))
954 return error;
955
956 IFCALLRET(context->ClientFileContentsResponse, error, context, &response);
957
958 if (error)
959 WLog_Print(cliprdr->log, WLOG_ERROR,
960 "ClientFileContentsResponse failed with error %" PRIu32 "!", error);
961
962 return error;
963}
964
970static UINT cliprdr_server_receive_pdu(CliprdrServerContext* context, wStream* s,
971 const CLIPRDR_HEADER* header)
972{
973 UINT error = 0;
974
975 WINPR_ASSERT(context);
976 WINPR_ASSERT(header);
977
978 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
979 WINPR_ASSERT(cliprdr);
980
981 {
982 char buffer1[64] = WINPR_C_ARRAY_INIT;
983 char buffer2[64] = WINPR_C_ARRAY_INIT;
984 WLog_Print(cliprdr->log, WLOG_DEBUG,
985 "CliprdrServerReceivePdu: msgType: %s, msgFlags: %s dataLen: %" PRIu32 "",
986 CB_MSG_TYPE_STRING(header->msgType, buffer1, sizeof(buffer1)),
987 CB_MSG_FLAGS_STRING(header->msgFlags, buffer2, sizeof(buffer2)),
988 header->dataLen);
989 }
990
991 const size_t start = Stream_GetPosition(s);
992 switch (header->msgType)
993 {
994 case CB_CLIP_CAPS:
995 error = cliprdr_server_receive_capabilities(context, s, header);
996 break;
997
998 case CB_TEMP_DIRECTORY:
999 error = cliprdr_server_receive_temporary_directory(context, s, header);
1000 break;
1001
1002 case CB_FORMAT_LIST:
1003 error = cliprdr_server_receive_format_list(context, s, header);
1004 break;
1005
1006 case CB_FORMAT_LIST_RESPONSE:
1007 error = cliprdr_server_receive_format_list_response(context, s, header);
1008 break;
1009
1010 case CB_LOCK_CLIPDATA:
1011 error = cliprdr_server_receive_lock_clipdata(context, s, header);
1012 break;
1013
1014 case CB_UNLOCK_CLIPDATA:
1015 error = cliprdr_server_receive_unlock_clipdata(context, s, header);
1016 break;
1017
1018 case CB_FORMAT_DATA_REQUEST:
1019 error = cliprdr_server_receive_format_data_request(context, s, header);
1020 break;
1021
1022 case CB_FORMAT_DATA_RESPONSE:
1023 error = cliprdr_server_receive_format_data_response(context, s, header);
1024 break;
1025
1026 case CB_FILECONTENTS_REQUEST:
1027 error = cliprdr_server_receive_filecontents_request(context, s, header);
1028 break;
1029
1030 case CB_FILECONTENTS_RESPONSE:
1031 error = cliprdr_server_receive_filecontents_response(context, s, header);
1032 break;
1033
1034 default:
1035 error = ERROR_INVALID_DATA;
1036 WLog_Print(cliprdr->log, WLOG_ERROR, "Unexpected clipboard PDU type: %" PRIu16 "",
1037 header->msgType);
1038 break;
1039 }
1040
1041 const size_t end = Stream_GetPosition(s);
1042 const size_t diff = end - start;
1043 if (diff != header->dataLen)
1044 {
1045 char buffer1[64] = WINPR_C_ARRAY_INIT;
1046 WLog_WARN(
1047 TAG, "[%s] Mismatch: header told length is %" PRIu32 ", but actually read %" PRIuz,
1048 CB_MSG_TYPE_STRING(header->msgType, buffer1, sizeof(buffer1)), header->dataLen, diff);
1049 }
1050
1051 const size_t rem = Stream_GetRemainingLength(s);
1052 if (rem > 0)
1053 {
1054 char buffer1[64] = WINPR_C_ARRAY_INIT;
1055 char buffer2[64] = WINPR_C_ARRAY_INIT;
1056 WLog_WARN(
1057 TAG, "msgType: %s, msgFlags: %s dataLen: %" PRIu32 ": %" PRIuz " bytes not parsed",
1058 CB_MSG_TYPE_STRING(header->msgType, buffer1, sizeof(buffer1)),
1059 CB_MSG_FLAGS_STRING(header->msgFlags, buffer2, sizeof(buffer2)), header->dataLen, rem);
1060 }
1061 return error;
1062}
1063
1069static UINT cliprdr_server_init(CliprdrServerContext* context)
1070{
1071 UINT32 generalFlags = 0;
1072 UINT error = 0;
1073 CLIPRDR_MONITOR_READY monitorReady = WINPR_C_ARRAY_INIT;
1074
1075 WINPR_ASSERT(context);
1076
1077 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
1078 WINPR_ASSERT(cliprdr);
1079
1080 monitorReady.common.msgType = CB_MONITOR_READY;
1081
1082 if (context->useLongFormatNames)
1083 generalFlags |= CB_USE_LONG_FORMAT_NAMES;
1084
1085 if (context->streamFileClipEnabled)
1086 generalFlags |= CB_STREAM_FILECLIP_ENABLED;
1087
1088 if (context->fileClipNoFilePaths)
1089 generalFlags |= CB_FILECLIP_NO_FILE_PATHS;
1090
1091 if (context->canLockClipData)
1092 generalFlags |= CB_CAN_LOCK_CLIPDATA;
1093
1094 if (context->hasHugeFileSupport)
1095 generalFlags |= CB_HUGE_FILE_SUPPORT_ENABLED;
1096
1097 CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet = { .capabilitySetType =
1098 CB_CAPSTYPE_GENERAL,
1099 .capabilitySetLength =
1100 CB_CAPSTYPE_GENERAL_LEN,
1101 .version = CB_CAPS_VERSION_2,
1102 .generalFlags = generalFlags };
1103
1104 CLIPRDR_CAPABILITIES capabilities = { .common = { .msgType = CB_CLIP_CAPS,
1105 .msgFlags = 0,
1106 .dataLen = 4 + CB_CAPSTYPE_GENERAL_LEN },
1107 .cCapabilitiesSets = 1,
1108 .capabilitySets =
1109 (CLIPRDR_CAPABILITY_SET*)&generalCapabilitySet };
1110
1111 if ((error = context->ServerCapabilities(context, &capabilities)))
1112 {
1113 WLog_Print(cliprdr->log, WLOG_ERROR, "ServerCapabilities failed with error %" PRIu32 "!",
1114 error);
1115 return error;
1116 }
1117
1118 if ((error = context->MonitorReady(context, &monitorReady)))
1119 {
1120 WLog_Print(cliprdr->log, WLOG_ERROR, "MonitorReady failed with error %" PRIu32 "!", error);
1121 return error;
1122 }
1123
1124 return error;
1125}
1126
1132static UINT cliprdr_server_read(CliprdrServerContext* context)
1133{
1134 WINPR_ASSERT(context);
1135
1136 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
1137 WINPR_ASSERT(cliprdr);
1138
1139 wStream* s = cliprdr->s;
1140
1141 const size_t spos = Stream_GetPosition(s);
1142 WINPR_STATIC_ASSERT(CLIPRDR_HEADER_LENGTH == sizeof(CLIPRDR_HEADER));
1143
1144 /* Read data.
1145 * first the header, then the data expected from header supplied length */
1146 const BOOL noHeader = spos < sizeof(CLIPRDR_HEADER);
1147 if (noHeader || (spos < cliprdr->totalExpectedBytes))
1148 {
1149 DWORD BytesReturned = 0;
1150 const size_t BytesToRead =
1151 (noHeader ? sizeof(CLIPRDR_HEADER) : cliprdr->totalExpectedBytes) - spos;
1152 const DWORD status = WaitForSingleObject(cliprdr->ChannelEvent, 0);
1153
1154 if (status == WAIT_FAILED)
1155 {
1156 const UINT error = GetLastError();
1157 WLog_Print(cliprdr->log, WLOG_ERROR,
1158 "WaitForSingleObject failed with error %" PRIu32 "", error);
1159 return error;
1160 }
1161
1162 if (status == WAIT_TIMEOUT)
1163 return CHANNEL_RC_OK;
1164
1165 if (!WTSVirtualChannelRead(cliprdr->ChannelHandle, 0, Stream_Pointer(s),
1166 WINPR_ASSERTING_INT_CAST(DWORD, BytesToRead), &BytesReturned))
1167 {
1168 WLog_Print(cliprdr->log, WLOG_ERROR, "WTSVirtualChannelRead failed!");
1169 return ERROR_INTERNAL_ERROR;
1170 }
1171
1172 if (!Stream_SafeSeek(s, BytesReturned))
1173 return ERROR_INTERNAL_ERROR;
1174 }
1175
1176 /* Abort early if the packet is not fully available */
1177 const size_t epos = Stream_GetPosition(s);
1178 if ((epos < sizeof(CLIPRDR_HEADER)) || (epos < cliprdr->totalExpectedBytes))
1179 return CHANNEL_RC_OK;
1180
1181 // Got enough bytes to extract header, do so.
1182 if (cliprdr->totalExpectedBytes == 0)
1183 {
1184 const size_t position = Stream_GetPosition(s);
1185 Stream_ResetPosition(s);
1186 Stream_Read_UINT16(s, cliprdr->header.msgType); /* msgType (2 bytes) */
1187 Stream_Read_UINT16(s, cliprdr->header.msgFlags); /* msgFlags (2 bytes) */
1188 Stream_Read_UINT32(s, cliprdr->header.dataLen); /* dataLen (4 bytes) */
1189
1190 if (!Stream_EnsureRemainingCapacity(s, cliprdr->header.dataLen))
1191 {
1192 WLog_Print(cliprdr->log, WLOG_ERROR, "Stream_EnsureCapacity failed!");
1193 return CHANNEL_RC_NO_MEMORY;
1194 }
1195
1196 if (!Stream_SetPosition(s, position))
1197 return ERROR_INVALID_DATA;
1198
1199 cliprdr->totalExpectedBytes = cliprdr->header.dataLen + CLIPRDR_HEADER_LENGTH;
1200 }
1201 /* Read completed, process the data */
1202 else
1203 {
1204 if (epos > cliprdr->totalExpectedBytes)
1205 {
1206 char buffer1[128] = WINPR_C_ARRAY_INIT;
1207 char buffer2[128] = WINPR_C_ARRAY_INIT;
1208 WLog_Print(cliprdr->log, WLOG_WARN,
1209 "Received excess bytes for [%s|%s]: Expected %" PRIuz ", but got %" PRIuz,
1210 CB_MSG_TYPE_STRING(cliprdr->header.msgType, buffer1, sizeof(buffer1)),
1211 CB_MSG_FLAGS_STRING(cliprdr->header.msgFlags, buffer2, sizeof(buffer2)),
1212 cliprdr->totalExpectedBytes, epos);
1213 }
1214 Stream_SealLength(s);
1215 if (!Stream_SetPosition(s, CLIPRDR_HEADER_LENGTH))
1216 return ERROR_INVALID_DATA;
1217
1218 const UINT error = cliprdr_server_receive_pdu(context, s, &cliprdr->header);
1219 cliprdr->totalExpectedBytes = 0;
1220
1221 const CLIPRDR_HEADER empty = WINPR_C_ARRAY_INIT;
1222 cliprdr->header = empty;
1223 if (error)
1224 {
1225 char buffer1[128] = WINPR_C_ARRAY_INIT;
1226 char buffer2[128] = WINPR_C_ARRAY_INIT;
1227 WLog_Print(cliprdr->log, WLOG_ERROR,
1228 "cliprdr_server_receive_pdu [%s|%s] failed with error code %" PRIu32 "!",
1229 CB_MSG_TYPE_STRING(cliprdr->header.msgType, buffer1, sizeof(buffer1)),
1230 CB_MSG_FLAGS_STRING(cliprdr->header.msgFlags, buffer2, sizeof(buffer2)),
1231 error);
1232 return error;
1233 }
1234 }
1235
1236 return CHANNEL_RC_OK;
1237}
1238
1239static DWORD WINAPI cliprdr_server_thread(LPVOID arg)
1240{
1241 DWORD status = 0;
1242 DWORD nCount = 0;
1243 HANDLE events[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1244 CliprdrServerContext* context = (CliprdrServerContext*)arg;
1245 UINT error = CHANNEL_RC_OK;
1246
1247 WINPR_ASSERT(context);
1248
1249 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
1250 WINPR_ASSERT(cliprdr);
1251
1252 HANDLE ChannelEvent = context->GetEventHandle(context);
1253
1254 events[nCount++] = cliprdr->StopEvent;
1255 events[nCount++] = ChannelEvent;
1256
1257 if (context->autoInitializationSequence)
1258 {
1259 if ((error = cliprdr_server_init(context)))
1260 {
1261 WLog_Print(cliprdr->log, WLOG_ERROR,
1262 "cliprdr_server_init failed with error %" PRIu32 "!", error);
1263 goto out;
1264 }
1265 }
1266
1267 while (1)
1268 {
1269 status = WaitForMultipleObjects(nCount, events, FALSE, INFINITE);
1270
1271 if (status == WAIT_FAILED)
1272 {
1273 error = GetLastError();
1274 WLog_Print(cliprdr->log, WLOG_ERROR,
1275 "WaitForMultipleObjects failed with error %" PRIu32 "", error);
1276 goto out;
1277 }
1278
1279 status = WaitForSingleObject(cliprdr->StopEvent, 0);
1280
1281 if (status == WAIT_FAILED)
1282 {
1283 error = GetLastError();
1284 WLog_Print(cliprdr->log, WLOG_ERROR,
1285 "WaitForSingleObject failed with error %" PRIu32 "", error);
1286 goto out;
1287 }
1288
1289 if (status == WAIT_OBJECT_0)
1290 break;
1291
1292 status = WaitForSingleObject(ChannelEvent, 0);
1293
1294 if (status == WAIT_FAILED)
1295 {
1296 error = GetLastError();
1297 WLog_Print(cliprdr->log, WLOG_ERROR,
1298 "WaitForSingleObject failed with error %" PRIu32 "", error);
1299 goto out;
1300 }
1301
1302 if (status == WAIT_OBJECT_0)
1303 {
1304 if ((error = context->CheckEventHandle(context)))
1305 {
1306 WLog_Print(cliprdr->log, WLOG_ERROR,
1307 "CheckEventHandle failed with error %" PRIu32 "!", error);
1308 break;
1309 }
1310 }
1311 }
1312
1313out:
1314
1315 if (error && context->rdpcontext)
1316 setChannelError(context->rdpcontext, error, "cliprdr_server_thread reported an error");
1317
1318 ExitThread(error);
1319 return error;
1320}
1321
1327static UINT cliprdr_server_open(CliprdrServerContext* context)
1328{
1329 void* buffer = nullptr;
1330 DWORD BytesReturned = 0;
1331
1332 WINPR_ASSERT(context);
1333
1334 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
1335 WINPR_ASSERT(cliprdr);
1336
1337 cliprdr->ChannelHandle =
1338 WTSVirtualChannelOpen(cliprdr->vcm, WTS_CURRENT_SESSION, CLIPRDR_SVC_CHANNEL_NAME);
1339
1340 if (!cliprdr->ChannelHandle)
1341 {
1342 WLog_Print(cliprdr->log, WLOG_ERROR, "WTSVirtualChannelOpen for cliprdr failed!");
1343 return ERROR_INTERNAL_ERROR;
1344 }
1345
1346 cliprdr->ChannelEvent = nullptr;
1347
1348 if (WTSVirtualChannelQuery(cliprdr->ChannelHandle, WTSVirtualEventHandle, &buffer,
1349 &BytesReturned))
1350 {
1351 if (BytesReturned != sizeof(HANDLE))
1352 {
1353 WLog_Print(cliprdr->log, WLOG_ERROR, "BytesReturned has not size of HANDLE!");
1354 return ERROR_INTERNAL_ERROR;
1355 }
1356
1357 cliprdr->ChannelEvent = *(HANDLE*)buffer;
1358 WTSFreeMemory(buffer);
1359 }
1360
1361 if (!cliprdr->ChannelEvent)
1362 {
1363 WLog_Print(cliprdr->log, WLOG_ERROR, "WTSVirtualChannelQuery for cliprdr failed!");
1364 return ERROR_INTERNAL_ERROR;
1365 }
1366
1367 return CHANNEL_RC_OK;
1368}
1369
1375static UINT cliprdr_server_close(CliprdrServerContext* context)
1376{
1377 WINPR_ASSERT(context);
1378
1379 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
1380 WINPR_ASSERT(cliprdr);
1381
1382 if (cliprdr->ChannelHandle)
1383 {
1384 (void)WTSVirtualChannelClose(cliprdr->ChannelHandle);
1385 cliprdr->ChannelHandle = nullptr;
1386 }
1387
1388 return CHANNEL_RC_OK;
1389}
1390
1396static UINT cliprdr_server_start(CliprdrServerContext* context)
1397{
1398 UINT error = 0;
1399
1400 WINPR_ASSERT(context);
1401
1402 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
1403 WINPR_ASSERT(cliprdr);
1404
1405 if (!cliprdr->ChannelHandle)
1406 {
1407 error = context->Open(context);
1408 if (error)
1409 {
1410 WLog_Print(cliprdr->log, WLOG_ERROR, "Open failed!");
1411 return error;
1412 }
1413 }
1414
1415 cliprdr->StopEvent = CreateEvent(nullptr, TRUE, FALSE, nullptr);
1416 if (!cliprdr->StopEvent)
1417 {
1418 WLog_Print(cliprdr->log, WLOG_ERROR, "CreateEvent failed!");
1419 return ERROR_INTERNAL_ERROR;
1420 }
1421
1422 cliprdr->Thread = CreateThread(nullptr, 0, cliprdr_server_thread, (void*)context, 0, nullptr);
1423 if (!cliprdr->Thread)
1424 {
1425 WLog_Print(cliprdr->log, WLOG_ERROR, "CreateThread failed!");
1426 (void)CloseHandle(cliprdr->StopEvent);
1427 cliprdr->StopEvent = nullptr;
1428 return ERROR_INTERNAL_ERROR;
1429 }
1430
1431 return CHANNEL_RC_OK;
1432}
1433
1439static UINT cliprdr_server_stop(CliprdrServerContext* context)
1440{
1441 UINT error = CHANNEL_RC_OK;
1442
1443 WINPR_ASSERT(context);
1444
1445 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
1446 WINPR_ASSERT(cliprdr);
1447
1448 if (cliprdr->StopEvent)
1449 {
1450 (void)SetEvent(cliprdr->StopEvent);
1451
1452 if (WaitForSingleObject(cliprdr->Thread, INFINITE) == WAIT_FAILED)
1453 {
1454 error = GetLastError();
1455 WLog_Print(cliprdr->log, WLOG_ERROR,
1456 "WaitForSingleObject failed with error %" PRIu32 "", error);
1457 return error;
1458 }
1459
1460 (void)CloseHandle(cliprdr->Thread);
1461 (void)CloseHandle(cliprdr->StopEvent);
1462 }
1463
1464 if (cliprdr->ChannelHandle)
1465 return context->Close(context);
1466
1467 return error;
1468}
1469
1470static HANDLE cliprdr_server_get_event_handle(CliprdrServerContext* context)
1471{
1472 WINPR_ASSERT(context);
1473
1474 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
1475 WINPR_ASSERT(cliprdr);
1476 return cliprdr->ChannelEvent;
1477}
1478
1484static UINT cliprdr_server_check_event_handle(CliprdrServerContext* context)
1485{
1486 return cliprdr_server_read(context);
1487}
1488
1489CliprdrServerContext* cliprdr_server_context_new(HANDLE vcm)
1490{
1491 CliprdrServerContext* context = (CliprdrServerContext*)calloc(1, sizeof(CliprdrServerContext));
1492
1493 if (!context)
1494 goto fail;
1495
1496 context->autoInitializationSequence = TRUE;
1497 context->Open = cliprdr_server_open;
1498 context->Close = cliprdr_server_close;
1499 context->Start = cliprdr_server_start;
1500 context->Stop = cliprdr_server_stop;
1501 context->GetEventHandle = cliprdr_server_get_event_handle;
1502 context->CheckEventHandle = cliprdr_server_check_event_handle;
1503 context->ServerCapabilities = cliprdr_server_capabilities;
1504 context->MonitorReady = cliprdr_server_monitor_ready;
1505 context->ServerFormatList = cliprdr_server_format_list;
1506 context->ServerFormatListResponse = cliprdr_server_format_list_response;
1507 context->ServerLockClipboardData = cliprdr_server_lock_clipboard_data;
1508 context->ServerUnlockClipboardData = cliprdr_server_unlock_clipboard_data;
1509 context->ServerFormatDataRequest = cliprdr_server_format_data_request;
1510 context->ServerFormatDataResponse = cliprdr_server_format_data_response;
1511 context->ServerFileContentsRequest = cliprdr_server_file_contents_request;
1512 context->ServerFileContentsResponse = cliprdr_server_file_contents_response;
1513
1514 CliprdrServerPrivate* cliprdr = context->handle =
1515 (CliprdrServerPrivate*)calloc(1, sizeof(CliprdrServerPrivate));
1516 if (!cliprdr)
1517 goto fail;
1518
1519 cliprdr->log = WLog_Get(TAG);
1520 cliprdr->vcm = vcm;
1521 cliprdr->s = Stream_New(nullptr, 4096);
1522
1523 if (!cliprdr->s)
1524 goto fail;
1525
1526 return context;
1527
1528fail:
1529 cliprdr_server_context_free(context);
1530 return nullptr;
1531}
1532
1533void cliprdr_server_context_free(CliprdrServerContext* context)
1534{
1535 if (!context)
1536 return;
1537
1538 CliprdrServerPrivate* cliprdr = (CliprdrServerPrivate*)context->handle;
1539
1540 if (cliprdr)
1541 Stream_Free(cliprdr->s, TRUE);
1542
1543 free(context->handle);
1544 free(context);
1545}