Coverage Report

Created: 2026-03-04 06:17

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