Coverage Report

Created: 2025-07-01 06:46

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