Coverage Report

Created: 2024-05-20 06:11

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