Coverage Report

Created: 2025-08-26 06:37

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