FreeRDP
Loading...
Searching...
No Matches
client/cliprdr_main.c
1
23#include <freerdp/config.h>
24
25#include <winpr/wtypes.h>
26#include <winpr/assert.h>
27#include <winpr/crt.h>
28#include <winpr/print.h>
29#include <winpr/clipboard.h>
30
31#include <freerdp/types.h>
32#include <freerdp/constants.h>
33#include <freerdp/freerdp.h>
34#include <freerdp/client/cliprdr.h>
35
36#include "../../../channels/client/addin.h"
37
38#include "cliprdr_main.h"
39#include "cliprdr_format.h"
40#include "../cliprdr_common.h"
41
42const char type_FileGroupDescriptorW[] = "FileGroupDescriptorW";
43const char type_FileContents[] = "FileContents";
44
45CliprdrClientContext* cliprdr_get_client_interface(cliprdrPlugin* cliprdr)
46{
47 CliprdrClientContext* pInterface = NULL;
48
49 if (!cliprdr)
50 return NULL;
51
52 pInterface = (CliprdrClientContext*)cliprdr->channelEntryPoints.pInterface;
53 return pInterface;
54}
55
61static UINT cliprdr_packet_send(cliprdrPlugin* cliprdr, wStream* s)
62{
63 UINT status = CHANNEL_RC_OK;
64
65 WINPR_ASSERT(cliprdr);
66 WINPR_ASSERT(s);
67
68 const size_t pos = Stream_GetPosition(s);
69 WINPR_ASSERT(pos >= 8ULL);
70 WINPR_ASSERT(pos <= UINT32_MAX - 8);
71
72 const uint32_t dataLen = WINPR_ASSERTING_INT_CAST(uint32_t, pos - 8UL);
73
74 Stream_SetPosition(s, 4);
75 Stream_Write_UINT32(s, dataLen);
76 Stream_SetPosition(s, pos);
77
78 WLog_Print(cliprdr->log, WLOG_DEBUG, "Cliprdr Sending (%" PRIuz " bytes)", pos);
79
80 if (!cliprdr)
81 {
82 status = CHANNEL_RC_BAD_INIT_HANDLE;
83 }
84 else
85 {
86 WINPR_ASSERT(cliprdr->channelEntryPoints.pVirtualChannelWriteEx);
87 status = cliprdr->channelEntryPoints.pVirtualChannelWriteEx(
88 cliprdr->InitHandle, cliprdr->OpenHandle, Stream_Buffer(s),
89 (UINT32)Stream_GetPosition(s), s);
90 }
91
92 if (status != CHANNEL_RC_OK)
93 {
94 Stream_Free(s, TRUE);
95 WLog_Print(cliprdr->log, WLOG_ERROR, "VirtualChannelWrite failed with %s [%08" PRIX32 "]",
96 WTSErrorToString(status), status);
97 }
98
99 return status;
100}
101
102UINT cliprdr_send_error_response(cliprdrPlugin* cliprdr, UINT16 type)
103{
104 wStream* s = cliprdr_packet_new(type, CB_RESPONSE_FAIL, 0);
105 if (!s)
106 {
107 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
108 return ERROR_OUTOFMEMORY;
109 }
110
111 return cliprdr_packet_send(cliprdr, s);
112}
113
114static void cliprdr_print_general_capability_flags(wLog* log, UINT32 flags)
115{
116 WLog_Print(log, WLOG_DEBUG, "generalFlags (0x%08" PRIX32 ") {", flags);
117
118 if (flags & CB_USE_LONG_FORMAT_NAMES)
119 WLog_Print(log, WLOG_DEBUG, "\tCB_USE_LONG_FORMAT_NAMES");
120
121 if (flags & CB_STREAM_FILECLIP_ENABLED)
122 WLog_Print(log, WLOG_DEBUG, "\tCB_STREAM_FILECLIP_ENABLED");
123
124 if (flags & CB_FILECLIP_NO_FILE_PATHS)
125 WLog_Print(log, WLOG_DEBUG, "\tCB_FILECLIP_NO_FILE_PATHS");
126
127 if (flags & CB_CAN_LOCK_CLIPDATA)
128 WLog_Print(log, WLOG_DEBUG, "\tCB_CAN_LOCK_CLIPDATA");
129
130 if (flags & CB_HUGE_FILE_SUPPORT_ENABLED)
131 WLog_Print(log, WLOG_DEBUG, "\tCB_HUGE_FILE_SUPPORT_ENABLED");
132
133 WLog_Print(log, WLOG_DEBUG, "}");
134}
135
141static UINT cliprdr_process_general_capability(cliprdrPlugin* cliprdr, wStream* s)
142{
143 UINT32 version = 0;
144 UINT32 generalFlags = 0;
145 CLIPRDR_CAPABILITIES capabilities = { 0 };
146 CLIPRDR_GENERAL_CAPABILITY_SET generalCapabilitySet = { 0 };
147 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
148 UINT error = CHANNEL_RC_OK;
149
150 WINPR_ASSERT(cliprdr);
151 WINPR_ASSERT(s);
152
153 if (!context)
154 {
155 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_get_client_interface failed!");
156 return ERROR_INTERNAL_ERROR;
157 }
158
159 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 8))
160 return ERROR_INVALID_DATA;
161
162 Stream_Read_UINT32(s, version); /* version (4 bytes) */
163 Stream_Read_UINT32(s, generalFlags); /* generalFlags (4 bytes) */
164 WLog_Print(cliprdr->log, WLOG_DEBUG, "Version: %" PRIu32 "", version);
165
166 cliprdr_print_general_capability_flags(cliprdr->log, generalFlags);
167
168 cliprdr->useLongFormatNames = (generalFlags & CB_USE_LONG_FORMAT_NAMES) ? TRUE : FALSE;
169 cliprdr->streamFileClipEnabled = (generalFlags & CB_STREAM_FILECLIP_ENABLED) ? TRUE : FALSE;
170 cliprdr->fileClipNoFilePaths = (generalFlags & CB_FILECLIP_NO_FILE_PATHS) ? TRUE : FALSE;
171 cliprdr->canLockClipData = (generalFlags & CB_CAN_LOCK_CLIPDATA) ? TRUE : FALSE;
172 cliprdr->hasHugeFileSupport = (generalFlags & CB_HUGE_FILE_SUPPORT_ENABLED) ? TRUE : FALSE;
173 cliprdr->capabilitiesReceived = TRUE;
174
175 capabilities.common.msgType = CB_CLIP_CAPS;
176 capabilities.cCapabilitiesSets = 1;
177 capabilities.capabilitySets = (CLIPRDR_CAPABILITY_SET*)&(generalCapabilitySet);
178 generalCapabilitySet.capabilitySetType = CB_CAPSTYPE_GENERAL;
179 generalCapabilitySet.capabilitySetLength = 12;
180 generalCapabilitySet.version = version;
181 generalCapabilitySet.generalFlags = generalFlags;
182 IFCALLRET(context->ServerCapabilities, error, context, &capabilities);
183
184 if (error)
185 WLog_Print(cliprdr->log, WLOG_ERROR, "ServerCapabilities failed with error %" PRIu32 "!",
186 error);
187
188 return error;
189}
190
196static UINT cliprdr_process_clip_caps(cliprdrPlugin* cliprdr, wStream* s,
197 WINPR_ATTR_UNUSED UINT32 length,
198 WINPR_ATTR_UNUSED UINT16 flags)
199{
200 UINT16 lengthCapability = 0;
201 UINT16 cCapabilitiesSets = 0;
202 UINT16 capabilitySetType = 0;
203 UINT error = CHANNEL_RC_OK;
204
205 WINPR_ASSERT(cliprdr);
206 WINPR_ASSERT(s);
207
208 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 4))
209 return ERROR_INVALID_DATA;
210
211 Stream_Read_UINT16(s, cCapabilitiesSets); /* cCapabilitiesSets (2 bytes) */
212 Stream_Seek_UINT16(s); /* pad1 (2 bytes) */
213 WLog_Print(cliprdr->log, WLOG_DEBUG, "ServerCapabilities");
214
215 for (UINT16 index = 0; index < cCapabilitiesSets; index++)
216 {
217 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 4))
218 return ERROR_INVALID_DATA;
219
220 Stream_Read_UINT16(s, capabilitySetType); /* capabilitySetType (2 bytes) */
221 Stream_Read_UINT16(s, lengthCapability); /* lengthCapability (2 bytes) */
222
223 if ((lengthCapability < 4) ||
224 (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, lengthCapability - 4U)))
225 return ERROR_INVALID_DATA;
226
227 switch (capabilitySetType)
228 {
229 case CB_CAPSTYPE_GENERAL:
230 if ((error = cliprdr_process_general_capability(cliprdr, s)))
231 {
232 WLog_Print(cliprdr->log, WLOG_ERROR,
233 "cliprdr_process_general_capability failed with error %" PRIu32 "!",
234 error);
235 return error;
236 }
237
238 break;
239
240 default:
241 WLog_Print(cliprdr->log, WLOG_ERROR, "unknown cliprdr capability set: %" PRIu16 "",
242 capabilitySetType);
243 return CHANNEL_RC_BAD_PROC;
244 }
245 }
246
247 return error;
248}
249
255static UINT cliprdr_process_monitor_ready(cliprdrPlugin* cliprdr, WINPR_ATTR_UNUSED wStream* s,
256 UINT32 length, UINT16 flags)
257{
258 CLIPRDR_MONITOR_READY monitorReady = { 0 };
259 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
260 UINT error = CHANNEL_RC_OK;
261
262 WINPR_ASSERT(cliprdr);
263 WINPR_ASSERT(s);
264
265 WLog_Print(cliprdr->log, WLOG_DEBUG, "MonitorReady");
266
267 if (!cliprdr->capabilitiesReceived)
268 {
275 cliprdr->useLongFormatNames = FALSE;
276 cliprdr->streamFileClipEnabled = FALSE;
277 cliprdr->fileClipNoFilePaths = TRUE;
278 cliprdr->canLockClipData = FALSE;
279 }
280
281 monitorReady.common.msgType = CB_MONITOR_READY;
282 monitorReady.common.msgFlags = flags;
283 monitorReady.common.dataLen = length;
284 IFCALLRET(context->MonitorReady, error, context, &monitorReady);
285
286 if (error)
287 WLog_Print(cliprdr->log, WLOG_ERROR, "MonitorReady failed with error %" PRIu32 "!", error);
288
289 return error;
290}
291
297static UINT cliprdr_process_filecontents_request(cliprdrPlugin* cliprdr, wStream* s, UINT32 length,
298 UINT16 flags)
299{
300 CLIPRDR_FILE_CONTENTS_REQUEST request = { 0 };
301 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
302 UINT error = CHANNEL_RC_OK;
303
304 WINPR_ASSERT(cliprdr);
305 WINPR_ASSERT(s);
306
307 WLog_Print(cliprdr->log, WLOG_DEBUG, "FileContentsRequest");
308
309 request.common.msgType = CB_FILECONTENTS_REQUEST;
310 request.common.msgFlags = flags;
311 request.common.dataLen = length;
312
313 if ((error = cliprdr_read_file_contents_request(s, &request)))
314 return error;
315
316 const UINT32 mask =
317 freerdp_settings_get_uint32(context->rdpcontext->settings, FreeRDP_ClipboardFeatureMask);
318 if ((mask & (CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES)) == 0)
319 {
320 WLog_Print(cliprdr->log, WLOG_WARN, "local -> remote file copy disabled, ignoring request");
321 return cliprdr_send_error_response(cliprdr, CB_FILECONTENTS_RESPONSE);
322 }
323 IFCALLRET(context->ServerFileContentsRequest, error, context, &request);
324
325 if (error)
326 WLog_Print(cliprdr->log, WLOG_ERROR,
327 "ServerFileContentsRequest failed with error %" PRIu32 "!", error);
328
329 return error;
330}
331
337static UINT cliprdr_process_filecontents_response(cliprdrPlugin* cliprdr, wStream* s, UINT32 length,
338 UINT16 flags)
339{
340 CLIPRDR_FILE_CONTENTS_RESPONSE response = { 0 };
341 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
342 UINT error = CHANNEL_RC_OK;
343
344 WINPR_ASSERT(cliprdr);
345 WINPR_ASSERT(s);
346
347 WLog_Print(cliprdr->log, WLOG_DEBUG, "FileContentsResponse");
348
349 response.common.msgType = CB_FILECONTENTS_RESPONSE;
350 response.common.msgFlags = flags;
351 response.common.dataLen = length;
352
353 if ((error = cliprdr_read_file_contents_response(s, &response)))
354 return error;
355
356 IFCALLRET(context->ServerFileContentsResponse, error, context, &response);
357
358 if (error)
359 WLog_Print(cliprdr->log, WLOG_ERROR,
360 "ServerFileContentsResponse failed with error %" PRIu32 "!", error);
361
362 return error;
363}
364
370static UINT cliprdr_process_lock_clipdata(cliprdrPlugin* cliprdr, wStream* s, UINT32 length,
371 UINT16 flags)
372{
373 CLIPRDR_LOCK_CLIPBOARD_DATA lockClipboardData = { 0 };
374 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
375 UINT error = CHANNEL_RC_OK;
376
377 WINPR_ASSERT(cliprdr);
378 WINPR_ASSERT(s);
379
380 WLog_Print(cliprdr->log, WLOG_DEBUG, "LockClipData");
381
382 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 4))
383 return ERROR_INVALID_DATA;
384
385 lockClipboardData.common.msgType = CB_LOCK_CLIPDATA;
386 lockClipboardData.common.msgFlags = flags;
387 lockClipboardData.common.dataLen = length;
388 Stream_Read_UINT32(s, lockClipboardData.clipDataId); /* clipDataId (4 bytes) */
389 IFCALLRET(context->ServerLockClipboardData, error, context, &lockClipboardData);
390
391 if (error)
392 WLog_Print(cliprdr->log, WLOG_ERROR,
393 "ServerLockClipboardData failed with error %" PRIu32 "!", error);
394
395 return error;
396}
397
403static UINT cliprdr_process_unlock_clipdata(cliprdrPlugin* cliprdr, wStream* s, UINT32 length,
404 UINT16 flags)
405{
406 CLIPRDR_UNLOCK_CLIPBOARD_DATA unlockClipboardData = { 0 };
407 CliprdrClientContext* context = cliprdr_get_client_interface(cliprdr);
408 UINT error = CHANNEL_RC_OK;
409
410 WINPR_ASSERT(cliprdr);
411 WINPR_ASSERT(s);
412
413 WLog_Print(cliprdr->log, WLOG_DEBUG, "UnlockClipData");
414
415 if ((error = cliprdr_read_unlock_clipdata(s, &unlockClipboardData)))
416 return error;
417
418 unlockClipboardData.common.msgType = CB_UNLOCK_CLIPDATA;
419 unlockClipboardData.common.msgFlags = flags;
420 unlockClipboardData.common.dataLen = length;
421
422 IFCALLRET(context->ServerUnlockClipboardData, error, context, &unlockClipboardData);
423
424 if (error)
425 WLog_Print(cliprdr->log, WLOG_ERROR,
426 "ServerUnlockClipboardData failed with error %" PRIu32 "!", error);
427
428 return error;
429}
430
436static UINT cliprdr_order_recv(LPVOID userdata, wStream* s)
437{
438 cliprdrPlugin* cliprdr = userdata;
439 UINT16 msgType = 0;
440 UINT16 msgFlags = 0;
441 UINT32 dataLen = 0;
442 UINT error = 0;
443
444 WINPR_ASSERT(cliprdr);
445 WINPR_ASSERT(s);
446
447 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, 8))
448 return ERROR_INVALID_DATA;
449
450 Stream_Read_UINT16(s, msgType); /* msgType (2 bytes) */
451 Stream_Read_UINT16(s, msgFlags); /* msgFlags (2 bytes) */
452 Stream_Read_UINT32(s, dataLen); /* dataLen (4 bytes) */
453
454 if (!Stream_CheckAndLogRequiredLengthWLog(cliprdr->log, s, dataLen))
455 return ERROR_INVALID_DATA;
456
457 char buffer1[64] = { 0 };
458 char buffer2[64] = { 0 };
459 WLog_Print(cliprdr->log, WLOG_DEBUG,
460 "msgType: %s (%" PRIu16 "), msgFlags: %s dataLen: %" PRIu32 "",
461 CB_MSG_TYPE_STRING(msgType, buffer1, sizeof(buffer1)), msgType,
462 CB_MSG_FLAGS_STRING(msgFlags, buffer2, sizeof(buffer2)), dataLen);
463
464 switch (msgType)
465 {
466 case CB_CLIP_CAPS:
467 if ((error = cliprdr_process_clip_caps(cliprdr, s, dataLen, msgFlags)))
468 WLog_Print(cliprdr->log, WLOG_ERROR,
469 "cliprdr_process_clip_caps failed with error %" PRIu32 "!", error);
470
471 break;
472
473 case CB_MONITOR_READY:
474 if ((error = cliprdr_process_monitor_ready(cliprdr, s, dataLen, msgFlags)))
475 WLog_Print(cliprdr->log, WLOG_ERROR,
476 "cliprdr_process_monitor_ready failed with error %" PRIu32 "!", error);
477
478 break;
479
480 case CB_FORMAT_LIST:
481 if ((error = cliprdr_process_format_list(cliprdr, s, dataLen, msgFlags)))
482 WLog_Print(cliprdr->log, WLOG_ERROR,
483 "cliprdr_process_format_list failed with error %" PRIu32 "!", error);
484
485 break;
486
487 case CB_FORMAT_LIST_RESPONSE:
488 if ((error = cliprdr_process_format_list_response(cliprdr, s, dataLen, msgFlags)))
489 WLog_Print(cliprdr->log, WLOG_ERROR,
490 "cliprdr_process_format_list_response failed with error %" PRIu32 "!",
491 error);
492
493 break;
494
495 case CB_FORMAT_DATA_REQUEST:
496 if ((error = cliprdr_process_format_data_request(cliprdr, s, dataLen, msgFlags)))
497 WLog_Print(cliprdr->log, WLOG_ERROR,
498 "cliprdr_process_format_data_request failed with error %" PRIu32 "!",
499 error);
500
501 break;
502
503 case CB_FORMAT_DATA_RESPONSE:
504 if ((error = cliprdr_process_format_data_response(cliprdr, s, dataLen, msgFlags)))
505 WLog_Print(cliprdr->log, WLOG_ERROR,
506 "cliprdr_process_format_data_response failed with error %" PRIu32 "!",
507 error);
508
509 break;
510
511 case CB_FILECONTENTS_REQUEST:
512 if ((error = cliprdr_process_filecontents_request(cliprdr, s, dataLen, msgFlags)))
513 WLog_Print(cliprdr->log, WLOG_ERROR,
514 "cliprdr_process_filecontents_request failed with error %" PRIu32 "!",
515 error);
516
517 break;
518
519 case CB_FILECONTENTS_RESPONSE:
520 if ((error = cliprdr_process_filecontents_response(cliprdr, s, dataLen, msgFlags)))
521 WLog_Print(cliprdr->log, WLOG_ERROR,
522 "cliprdr_process_filecontents_response failed with error %" PRIu32 "!",
523 error);
524
525 break;
526
527 case CB_LOCK_CLIPDATA:
528 if ((error = cliprdr_process_lock_clipdata(cliprdr, s, dataLen, msgFlags)))
529 WLog_Print(cliprdr->log, WLOG_ERROR,
530 "cliprdr_process_lock_clipdata failed with error %" PRIu32 "!", error);
531
532 break;
533
534 case CB_UNLOCK_CLIPDATA:
535 if ((error = cliprdr_process_unlock_clipdata(cliprdr, s, dataLen, msgFlags)))
536 WLog_Print(cliprdr->log, WLOG_ERROR,
537 "cliprdr_process_unlock_clipdata failed with error %" PRIu32 "!", error);
538
539 break;
540
541 default:
542 error = CHANNEL_RC_BAD_PROC;
543 WLog_Print(cliprdr->log, WLOG_ERROR, "unknown msgType %" PRIu16 "", msgType);
544 break;
545 }
546
547 Stream_Free(s, TRUE);
548 return error;
549}
550
560static UINT cliprdr_client_capabilities(CliprdrClientContext* context,
561 const CLIPRDR_CAPABILITIES* capabilities)
562{
563 wStream* s = NULL;
564 UINT32 flags = 0;
565 const CLIPRDR_GENERAL_CAPABILITY_SET* generalCapabilitySet = NULL;
566 cliprdrPlugin* cliprdr = NULL;
567
568 WINPR_ASSERT(context);
569
570 cliprdr = (cliprdrPlugin*)context->handle;
571 WINPR_ASSERT(cliprdr);
572
573 s = cliprdr_packet_new(CB_CLIP_CAPS, 0, 4 + CB_CAPSTYPE_GENERAL_LEN);
574
575 if (!s)
576 {
577 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
578 return ERROR_INTERNAL_ERROR;
579 }
580
581 Stream_Write_UINT16(s, 1); /* cCapabilitiesSets */
582 Stream_Write_UINT16(s, 0); /* pad1 */
583 generalCapabilitySet = (const CLIPRDR_GENERAL_CAPABILITY_SET*)capabilities->capabilitySets;
584 Stream_Write_UINT16(s, generalCapabilitySet->capabilitySetType); /* capabilitySetType */
585 Stream_Write_UINT16(s, generalCapabilitySet->capabilitySetLength); /* lengthCapability */
586 Stream_Write_UINT32(s, generalCapabilitySet->version); /* version */
587 flags = generalCapabilitySet->generalFlags;
588
589 /* Client capabilities are sent in response to server capabilities.
590 * -> Do not request features the server does not support.
591 * -> Update clipboard context feature state to what was agreed upon.
592 */
593 if (!cliprdr->useLongFormatNames)
594 flags &= (uint32_t)~CB_USE_LONG_FORMAT_NAMES;
595 if (!cliprdr->streamFileClipEnabled)
596 flags &= (uint32_t)~CB_STREAM_FILECLIP_ENABLED;
597 if (!cliprdr->fileClipNoFilePaths)
598 flags &= (uint32_t)~CB_FILECLIP_NO_FILE_PATHS;
599 if (!cliprdr->canLockClipData)
600 flags &= (uint32_t)~CB_CAN_LOCK_CLIPDATA;
601 if (!cliprdr->hasHugeFileSupport)
602 flags &= (uint32_t)~CB_HUGE_FILE_SUPPORT_ENABLED;
603
604 cliprdr->useLongFormatNames = (flags & CB_USE_LONG_FORMAT_NAMES) ? TRUE : FALSE;
605 cliprdr->streamFileClipEnabled = (flags & CB_STREAM_FILECLIP_ENABLED) ? TRUE : FALSE;
606 cliprdr->fileClipNoFilePaths = (flags & CB_FILECLIP_NO_FILE_PATHS) ? TRUE : FALSE;
607 cliprdr->canLockClipData = (flags & CB_CAN_LOCK_CLIPDATA) ? TRUE : FALSE;
608 cliprdr->hasHugeFileSupport = (flags & CB_HUGE_FILE_SUPPORT_ENABLED) ? TRUE : FALSE;
609
610 Stream_Write_UINT32(s, flags); /* generalFlags */
611 WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientCapabilities");
612
613 cliprdr->initialFormatListSent = FALSE;
614
615 return cliprdr_packet_send(cliprdr, s);
616}
617
623static UINT cliprdr_temp_directory(CliprdrClientContext* context,
624 const CLIPRDR_TEMP_DIRECTORY* tempDirectory)
625{
626 wStream* s = NULL;
627 cliprdrPlugin* cliprdr = NULL;
628
629 WINPR_ASSERT(context);
630 WINPR_ASSERT(tempDirectory);
631
632 cliprdr = (cliprdrPlugin*)context->handle;
633 WINPR_ASSERT(cliprdr);
634
635 const size_t tmpDirCharLen = sizeof(tempDirectory->szTempDir) / sizeof(WCHAR);
636 s = cliprdr_packet_new(CB_TEMP_DIRECTORY, 0, tmpDirCharLen * sizeof(WCHAR));
637
638 if (!s)
639 {
640 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
641 return ERROR_INTERNAL_ERROR;
642 }
643
644 if (Stream_Write_UTF16_String_From_UTF8(s, tmpDirCharLen - 1, tempDirectory->szTempDir,
645 ARRAYSIZE(tempDirectory->szTempDir), TRUE) < 0)
646 {
647 Stream_Free(s, TRUE);
648 return ERROR_INTERNAL_ERROR;
649 }
650 /* Path must be 260 UTF16 characters with '\0' termination.
651 * ensure this here */
652 Stream_Write_UINT16(s, 0);
653
654 WLog_Print(cliprdr->log, WLOG_DEBUG, "TempDirectory: %s", tempDirectory->szTempDir);
655 return cliprdr_packet_send(cliprdr, s);
656}
657
663static UINT cliprdr_client_format_list(CliprdrClientContext* context,
664 const CLIPRDR_FORMAT_LIST* formatList)
665{
666 wStream* s = NULL;
667 cliprdrPlugin* cliprdr = NULL;
668
669 WINPR_ASSERT(context);
670 WINPR_ASSERT(formatList);
671
672 cliprdr = (cliprdrPlugin*)context->handle;
673 WINPR_ASSERT(cliprdr);
674
675 {
676 const UINT32 mask = CB_RESPONSE_OK | CB_RESPONSE_FAIL;
677 if ((formatList->common.msgFlags & mask) != 0)
678 WLog_Print(cliprdr->log, WLOG_WARN,
679 "Sending clipboard request with invalid flags msgFlags = 0x%08" PRIx32
680 ". Correct in your client!",
681 formatList->common.msgFlags & mask);
682 }
683
684 const UINT32 mask =
685 freerdp_settings_get_uint32(context->rdpcontext->settings, FreeRDP_ClipboardFeatureMask);
686 CLIPRDR_FORMAT_LIST filterList = cliprdr_filter_format_list(
687 formatList, mask, CLIPRDR_FLAG_LOCAL_TO_REMOTE | CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES);
688
689 /* Allow initial format list from monitor ready, but ignore later attempts */
690 if ((filterList.numFormats == 0) && cliprdr->initialFormatListSent)
691 {
692 cliprdr_free_format_list(&filterList);
693 return CHANNEL_RC_OK;
694 }
695 cliprdr->initialFormatListSent = TRUE;
696
697 const uint32_t level = WLOG_DEBUG;
698 if (WLog_IsLevelActive(cliprdr->log, level))
699 {
700 WLog_Print(cliprdr->log, level, "ClientFormatList: numFormats: %" PRIu32 "",
701 formatList->numFormats);
702 for (size_t x = 0; x < filterList.numFormats; x++)
703 {
704 const CLIPRDR_FORMAT* format = &filterList.formats[x];
705 WLog_Print(cliprdr->log, level, "[%" PRIuz "]: id=0x%08" PRIx32 " [%s|%s]", x,
706 format->formatId, ClipboardGetFormatIdString(format->formatId),
707 format->formatName);
708 }
709 }
710
711 s = cliprdr_packet_format_list_new(&filterList, cliprdr->useLongFormatNames, FALSE);
712 cliprdr_free_format_list(&filterList);
713
714 if (!s)
715 {
716 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_format_list_new failed!");
717 return ERROR_INTERNAL_ERROR;
718 }
719
720 return cliprdr_packet_send(cliprdr, s);
721}
722
728static UINT
729cliprdr_client_format_list_response(CliprdrClientContext* context,
730 const CLIPRDR_FORMAT_LIST_RESPONSE* formatListResponse)
731{
732 wStream* s = NULL;
733 cliprdrPlugin* cliprdr = NULL;
734
735 WINPR_ASSERT(context);
736 WINPR_ASSERT(formatListResponse);
737
738 cliprdr = (cliprdrPlugin*)context->handle;
739 WINPR_ASSERT(cliprdr);
740
741 s = cliprdr_packet_new(CB_FORMAT_LIST_RESPONSE, formatListResponse->common.msgFlags, 0);
742
743 if (!s)
744 {
745 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
746 return ERROR_INTERNAL_ERROR;
747 }
748
749 WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFormatListResponse");
750 return cliprdr_packet_send(cliprdr, s);
751}
752
758static UINT cliprdr_client_lock_clipboard_data(CliprdrClientContext* context,
759 const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
760{
761 wStream* s = NULL;
762 cliprdrPlugin* cliprdr = NULL;
763
764 WINPR_ASSERT(context);
765 WINPR_ASSERT(lockClipboardData);
766
767 cliprdr = (cliprdrPlugin*)context->handle;
768 WINPR_ASSERT(cliprdr);
769
770 s = cliprdr_packet_lock_clipdata_new(lockClipboardData);
771
772 if (!s)
773 {
774 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_lock_clipdata_new failed!");
775 return ERROR_INTERNAL_ERROR;
776 }
777
778 WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientLockClipboardData: clipDataId: 0x%08" PRIX32 "",
779 lockClipboardData->clipDataId);
780 return cliprdr_packet_send(cliprdr, s);
781}
782
788static UINT
789cliprdr_client_unlock_clipboard_data(CliprdrClientContext* context,
790 const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
791{
792 wStream* s = NULL;
793 cliprdrPlugin* cliprdr = NULL;
794
795 WINPR_ASSERT(context);
796 WINPR_ASSERT(unlockClipboardData);
797
798 cliprdr = (cliprdrPlugin*)context->handle;
799 WINPR_ASSERT(cliprdr);
800
801 s = cliprdr_packet_unlock_clipdata_new(unlockClipboardData);
802
803 if (!s)
804 {
805 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_unlock_clipdata_new failed!");
806 return ERROR_INTERNAL_ERROR;
807 }
808
809 WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientUnlockClipboardData: clipDataId: 0x%08" PRIX32 "",
810 unlockClipboardData->clipDataId);
811 return cliprdr_packet_send(cliprdr, s);
812}
813
819static UINT cliprdr_client_format_data_request(CliprdrClientContext* context,
820 const CLIPRDR_FORMAT_DATA_REQUEST* formatDataRequest)
821{
822 WINPR_ASSERT(context);
823 WINPR_ASSERT(formatDataRequest);
824
825 cliprdrPlugin* cliprdr = (cliprdrPlugin*)context->handle;
826 WINPR_ASSERT(cliprdr);
827
828 const UINT32 mask =
829 freerdp_settings_get_uint32(context->rdpcontext->settings, FreeRDP_ClipboardFeatureMask);
830 if ((mask & (CLIPRDR_FLAG_REMOTE_TO_LOCAL | CLIPRDR_FLAG_REMOTE_TO_LOCAL_FILES)) == 0)
831 {
832 WLog_Print(cliprdr->log, WLOG_WARN, "remote -> local copy disabled, ignoring request");
833 return CHANNEL_RC_OK;
834 }
835
836 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_REQUEST, 0, 4);
837 if (!s)
838 {
839 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
840 return ERROR_INTERNAL_ERROR;
841 }
842
843 Stream_Write_UINT32(s, formatDataRequest->requestedFormatId); /* requestedFormatId (4 bytes) */
844 WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFormatDataRequest(0x%08" PRIx32 " [%s])",
845 formatDataRequest->requestedFormatId,
846 ClipboardGetFormatIdString(formatDataRequest->requestedFormatId));
847 return cliprdr_packet_send(cliprdr, s);
848}
849
855static UINT
856cliprdr_client_format_data_response(CliprdrClientContext* context,
857 const CLIPRDR_FORMAT_DATA_RESPONSE* formatDataResponse)
858{
859 WINPR_ASSERT(context);
860 WINPR_ASSERT(formatDataResponse);
861
862 cliprdrPlugin* cliprdr = (cliprdrPlugin*)context->handle;
863 WINPR_ASSERT(cliprdr);
864
865 WINPR_ASSERT(
866 (freerdp_settings_get_uint32(context->rdpcontext->settings, FreeRDP_ClipboardFeatureMask) &
867 (CLIPRDR_FLAG_LOCAL_TO_REMOTE | CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES)) != 0);
868
869 wStream* s = cliprdr_packet_new(CB_FORMAT_DATA_RESPONSE, formatDataResponse->common.msgFlags,
870 formatDataResponse->common.dataLen);
871
872 if (!s)
873 {
874 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_new failed!");
875 return ERROR_INTERNAL_ERROR;
876 }
877
878 Stream_Write(s, formatDataResponse->requestedFormatData, formatDataResponse->common.dataLen);
879 WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFormatDataResponse");
880 return cliprdr_packet_send(cliprdr, s);
881}
882
888static UINT
889cliprdr_client_file_contents_request(CliprdrClientContext* context,
890 const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
891{
892 wStream* s = NULL;
893
894 WINPR_ASSERT(context);
895 WINPR_ASSERT(fileContentsRequest);
896
897 cliprdrPlugin* cliprdr = (cliprdrPlugin*)context->handle;
898 if (!cliprdr)
899 return ERROR_INTERNAL_ERROR;
900
901 const UINT32 mask =
902 freerdp_settings_get_uint32(context->rdpcontext->settings, FreeRDP_ClipboardFeatureMask);
903 if ((mask & CLIPRDR_FLAG_REMOTE_TO_LOCAL_FILES) == 0)
904 {
905 WLog_Print(cliprdr->log, WLOG_WARN, "remote -> local file copy disabled, ignoring request");
906 return CHANNEL_RC_OK;
907 }
908
909 if (!cliprdr->hasHugeFileSupport)
910 {
911 if (((UINT64)fileContentsRequest->cbRequested + fileContentsRequest->nPositionLow) >
912 UINT32_MAX)
913 return ERROR_INVALID_PARAMETER;
914 if (fileContentsRequest->nPositionHigh != 0)
915 return ERROR_INVALID_PARAMETER;
916 }
917
918 s = cliprdr_packet_file_contents_request_new(fileContentsRequest);
919
920 if (!s)
921 {
922 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_file_contents_request_new failed!");
923 return ERROR_INTERNAL_ERROR;
924 }
925
926 WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFileContentsRequest: streamId: 0x%08" PRIX32 "",
927 fileContentsRequest->streamId);
928 return cliprdr_packet_send(cliprdr, s);
929}
930
936static UINT
937cliprdr_client_file_contents_response(CliprdrClientContext* context,
938 const CLIPRDR_FILE_CONTENTS_RESPONSE* fileContentsResponse)
939{
940 wStream* s = NULL;
941 cliprdrPlugin* cliprdr = NULL;
942
943 WINPR_ASSERT(context);
944 WINPR_ASSERT(fileContentsResponse);
945
946 cliprdr = (cliprdrPlugin*)context->handle;
947 WINPR_ASSERT(cliprdr);
948
949 const UINT32 mask =
950 freerdp_settings_get_uint32(context->rdpcontext->settings, FreeRDP_ClipboardFeatureMask);
951 if ((mask & CLIPRDR_FLAG_LOCAL_TO_REMOTE_FILES) == 0)
952 return cliprdr_send_error_response(cliprdr, CB_FILECONTENTS_RESPONSE);
953
954 s = cliprdr_packet_file_contents_response_new(fileContentsResponse);
955
956 if (!s)
957 {
958 WLog_Print(cliprdr->log, WLOG_ERROR, "cliprdr_packet_file_contents_response_new failed!");
959 return ERROR_INTERNAL_ERROR;
960 }
961
962 WLog_Print(cliprdr->log, WLOG_DEBUG, "ClientFileContentsResponse: streamId: 0x%08" PRIX32 "",
963 fileContentsResponse->streamId);
964 return cliprdr_packet_send(cliprdr, s);
965}
966
967static VOID VCAPITYPE cliprdr_virtual_channel_open_event_ex(LPVOID lpUserParam, DWORD openHandle,
968 UINT event, LPVOID pData,
969 UINT32 dataLength, UINT32 totalLength,
970 UINT32 dataFlags)
971{
972 UINT error = CHANNEL_RC_OK;
973 cliprdrPlugin* cliprdr = (cliprdrPlugin*)lpUserParam;
974 WINPR_ASSERT(cliprdr);
975
976 switch (event)
977 {
978 case CHANNEL_EVENT_DATA_RECEIVED:
979 if (cliprdr->OpenHandle != openHandle)
980 {
981 WLog_Print(cliprdr->log, WLOG_ERROR, "error no match");
982 return;
983 }
984 if ((error = channel_client_post_message(cliprdr->MsgsHandle, pData, dataLength,
985 totalLength, dataFlags)))
986 WLog_Print(cliprdr->log, WLOG_ERROR, "failed with error %" PRIu32 "", error);
987
988 break;
989
990 case CHANNEL_EVENT_WRITE_CANCELLED:
991 case CHANNEL_EVENT_WRITE_COMPLETE:
992 {
993 wStream* s = (wStream*)pData;
994 Stream_Free(s, TRUE);
995 }
996 break;
997
998 case CHANNEL_EVENT_USER:
999 break;
1000 default:
1001 break;
1002 }
1003
1004 if (error && cliprdr->context->rdpcontext)
1005 setChannelError(cliprdr->context->rdpcontext, error,
1006 "cliprdr_virtual_channel_open_event_ex reported an error");
1007}
1008
1014static UINT cliprdr_virtual_channel_event_connected(cliprdrPlugin* cliprdr,
1015 WINPR_ATTR_UNUSED LPVOID pData,
1016 WINPR_ATTR_UNUSED UINT32 dataLength)
1017{
1018 DWORD status = 0;
1019 WINPR_ASSERT(cliprdr);
1020 WINPR_ASSERT(cliprdr->context);
1021
1022 WINPR_ASSERT(cliprdr->channelEntryPoints.pVirtualChannelOpenEx);
1023 status = cliprdr->channelEntryPoints.pVirtualChannelOpenEx(
1024 cliprdr->InitHandle, &cliprdr->OpenHandle, cliprdr->channelDef.name,
1025 cliprdr_virtual_channel_open_event_ex);
1026 if (status != CHANNEL_RC_OK)
1027 return status;
1028
1029 cliprdr->MsgsHandle = channel_client_create_handler(
1030 cliprdr->context->rdpcontext, cliprdr, cliprdr_order_recv, CLIPRDR_SVC_CHANNEL_NAME);
1031 if (!cliprdr->MsgsHandle)
1032 return ERROR_INTERNAL_ERROR;
1033
1034 return status;
1035}
1036
1042static UINT cliprdr_virtual_channel_event_disconnected(cliprdrPlugin* cliprdr)
1043{
1044 UINT rc = 0;
1045
1046 WINPR_ASSERT(cliprdr);
1047
1048 channel_client_quit_handler(cliprdr->MsgsHandle);
1049 cliprdr->MsgsHandle = NULL;
1050
1051 if (cliprdr->OpenHandle == 0)
1052 return CHANNEL_RC_OK;
1053
1054 WINPR_ASSERT(cliprdr->channelEntryPoints.pVirtualChannelCloseEx);
1055 rc = cliprdr->channelEntryPoints.pVirtualChannelCloseEx(cliprdr->InitHandle,
1056 cliprdr->OpenHandle);
1057
1058 if (CHANNEL_RC_OK != rc)
1059 {
1060 WLog_Print(cliprdr->log, WLOG_ERROR, "pVirtualChannelClose failed with %s [%08" PRIX32 "]",
1061 WTSErrorToString(rc), rc);
1062 return rc;
1063 }
1064
1065 cliprdr->OpenHandle = 0;
1066
1067 return CHANNEL_RC_OK;
1068}
1069
1075static UINT cliprdr_virtual_channel_event_terminated(cliprdrPlugin* cliprdr)
1076{
1077 WINPR_ASSERT(cliprdr);
1078
1079 cliprdr->InitHandle = 0;
1080 free(cliprdr->context);
1081 free(cliprdr);
1082 return CHANNEL_RC_OK;
1083}
1084
1085static VOID VCAPITYPE cliprdr_virtual_channel_init_event_ex(LPVOID lpUserParam, LPVOID pInitHandle,
1086 UINT event, LPVOID pData,
1087 UINT dataLength)
1088{
1089 UINT error = CHANNEL_RC_OK;
1090 cliprdrPlugin* cliprdr = (cliprdrPlugin*)lpUserParam;
1091 WINPR_ASSERT(cliprdr);
1092
1093 if (cliprdr->InitHandle != pInitHandle)
1094 {
1095 WLog_Print(cliprdr->log, WLOG_ERROR, "error no match");
1096 return;
1097 }
1098
1099 switch (event)
1100 {
1101 case CHANNEL_EVENT_CONNECTED:
1102 if ((error = cliprdr_virtual_channel_event_connected(cliprdr, pData, dataLength)))
1103 WLog_Print(cliprdr->log, WLOG_ERROR,
1104 "cliprdr_virtual_channel_event_connected failed with error %" PRIu32 "!",
1105 error);
1106
1107 break;
1108
1109 case CHANNEL_EVENT_DISCONNECTED:
1110 if ((error = cliprdr_virtual_channel_event_disconnected(cliprdr)))
1111 WLog_Print(cliprdr->log, WLOG_ERROR,
1112 "cliprdr_virtual_channel_event_disconnected failed with error %" PRIu32
1113 "!",
1114 error);
1115
1116 break;
1117
1118 case CHANNEL_EVENT_TERMINATED:
1119 if ((error = cliprdr_virtual_channel_event_terminated(cliprdr)))
1120 WLog_Print(cliprdr->log, WLOG_ERROR,
1121 "cliprdr_virtual_channel_event_terminated failed with error %" PRIu32
1122 "!",
1123 error);
1124 break;
1125 default:
1126 break;
1127 }
1128
1129 if (error && cliprdr->context->rdpcontext)
1130 setChannelError(cliprdr->context->rdpcontext, error,
1131 "cliprdr_virtual_channel_init_event reported an error");
1132}
1133
1134/* cliprdr is always built-in */
1135#define VirtualChannelEntryEx cliprdr_VirtualChannelEntryEx
1136
1137FREERDP_ENTRY_POINT(BOOL VCAPITYPE VirtualChannelEntryEx(PCHANNEL_ENTRY_POINTS pEntryPoints,
1138 PVOID pInitHandle))
1139{
1140 UINT rc = 0;
1141 CHANNEL_ENTRY_POINTS_FREERDP_EX* pEntryPointsEx = NULL;
1142 cliprdrPlugin* cliprdr = (cliprdrPlugin*)calloc(1, sizeof(cliprdrPlugin));
1143
1144 wLog* log = WLog_Get(CHANNELS_TAG("cliprdr.client"));
1145 WINPR_ASSERT(log);
1146
1147 if (!cliprdr)
1148 {
1149 WLog_Print(log, WLOG_ERROR, "calloc failed!");
1150 return FALSE;
1151 }
1152
1153 cliprdr->log = log;
1154 cliprdr->channelDef.options = CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP |
1155 CHANNEL_OPTION_COMPRESS_RDP | CHANNEL_OPTION_SHOW_PROTOCOL;
1156 (void)sprintf_s(cliprdr->channelDef.name, ARRAYSIZE(cliprdr->channelDef.name),
1157 CLIPRDR_SVC_CHANNEL_NAME);
1158 pEntryPointsEx = (CHANNEL_ENTRY_POINTS_FREERDP_EX*)pEntryPoints;
1159 WINPR_ASSERT(pEntryPointsEx);
1160
1161 if ((pEntryPointsEx->cbSize >= sizeof(CHANNEL_ENTRY_POINTS_FREERDP_EX)) &&
1162 (pEntryPointsEx->MagicNumber == FREERDP_CHANNEL_MAGIC_NUMBER))
1163 {
1164 CliprdrClientContext* context =
1165 (CliprdrClientContext*)calloc(1, sizeof(CliprdrClientContext));
1166
1167 if (!context)
1168 {
1169 WLog_Print(cliprdr->log, WLOG_ERROR, "calloc failed!");
1170 free(cliprdr);
1171 return FALSE;
1172 }
1173
1174 context->handle = (void*)cliprdr;
1175 context->custom = NULL;
1176 context->ClientCapabilities = cliprdr_client_capabilities;
1177 context->TempDirectory = cliprdr_temp_directory;
1178 context->ClientFormatList = cliprdr_client_format_list;
1179 context->ClientFormatListResponse = cliprdr_client_format_list_response;
1180 context->ClientLockClipboardData = cliprdr_client_lock_clipboard_data;
1181 context->ClientUnlockClipboardData = cliprdr_client_unlock_clipboard_data;
1182 context->ClientFormatDataRequest = cliprdr_client_format_data_request;
1183 context->ClientFormatDataResponse = cliprdr_client_format_data_response;
1184 context->ClientFileContentsRequest = cliprdr_client_file_contents_request;
1185 context->ClientFileContentsResponse = cliprdr_client_file_contents_response;
1186 cliprdr->context = context;
1187 context->rdpcontext = pEntryPointsEx->context;
1188 }
1189
1190 WLog_Print(cliprdr->log, WLOG_DEBUG, "VirtualChannelEntryEx");
1191 CopyMemory(&(cliprdr->channelEntryPoints), pEntryPoints,
1193 cliprdr->InitHandle = pInitHandle;
1194 rc = cliprdr->channelEntryPoints.pVirtualChannelInitEx(
1195 cliprdr, cliprdr->context, pInitHandle, &cliprdr->channelDef, 1,
1196 VIRTUAL_CHANNEL_VERSION_WIN2000, cliprdr_virtual_channel_init_event_ex);
1197
1198 if (CHANNEL_RC_OK != rc)
1199 {
1200 WLog_Print(cliprdr->log, WLOG_ERROR, "pVirtualChannelInit failed with %s [%08" PRIX32 "]",
1201 WTSErrorToString(rc), rc);
1202 free(cliprdr->context);
1203 free(cliprdr);
1204 return FALSE;
1205 }
1206
1207 cliprdr->channelEntryPoints.pInterface = cliprdr->context;
1208 return TRUE;
1209}
FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
Definition svc.h:60