FreeRDP
Loading...
Searching...
No Matches
client_cliprdr_file.c
1
24#include <freerdp/config.h>
25
26#include <stdlib.h>
27#include <errno.h>
28
29#ifdef WITH_FUSE
30#define FUSE_USE_VERSION 30
31#include <fuse_lowlevel.h>
32#endif
33
34#if defined(WITH_FUSE)
35#include <sys/mount.h>
36#include <sys/stat.h>
37#include <errno.h>
38#include <time.h>
39#endif
40
41#include <winpr/crt.h>
42#include <winpr/string.h>
43#include <winpr/assert.h>
44#include <winpr/image.h>
45#include <winpr/stream.h>
46#include <winpr/clipboard.h>
47#include <winpr/path.h>
48
49#include <freerdp/utils/signal.h>
50#include <freerdp/log.h>
51#include <freerdp/client/cliprdr.h>
52#include <freerdp/channels/channels.h>
53#include <freerdp/channels/cliprdr.h>
54
55#include <freerdp/client/client_cliprdr_file.h>
56
57#define NO_CLIP_DATA_ID (UINT64_C(1) << 32)
58#define WIN32_FILETIME_TO_UNIX_EPOCH INT64_C(11644473600)
59
60#ifdef WITH_DEBUG_CLIPRDR
61#define DEBUG_CLIPRDR(log, ...) WLog_Print(log, WLOG_DEBUG, __VA_ARGS__)
62#else
63#define DEBUG_CLIPRDR(log, ...) \
64 do \
65 { \
66 } while (0)
67#endif
68
69#if defined(WITH_FUSE)
70typedef enum eFuseLowlevelOperationType
71{
72 FUSE_LL_OPERATION_NONE,
73 FUSE_LL_OPERATION_LOOKUP,
74 FUSE_LL_OPERATION_GETATTR,
75 FUSE_LL_OPERATION_READ,
76} FuseLowlevelOperationType;
77
78typedef struct sCliprdrFuseFile CliprdrFuseFile;
79
80struct sCliprdrFuseFile
81{
82 CliprdrFuseFile* parent;
83 wArrayList* children;
84
85 size_t filename_len;
86 const char* filename;
87
88 size_t filename_with_root_len;
89 char* filename_with_root;
90 UINT32 list_idx;
91 fuse_ino_t ino;
92
93 BOOL is_directory;
94 BOOL is_readonly;
95
96 BOOL has_size;
97 UINT64 size;
98
99 BOOL has_last_write_time;
100 INT64 last_write_time_unix;
101
102 BOOL has_clip_data_id;
103 UINT32 clip_data_id;
104};
105
106typedef struct
107{
108 CliprdrFileContext* file_context;
109
110 CliprdrFuseFile* clip_data_dir;
111
112 BOOL has_clip_data_id;
113 UINT32 clip_data_id;
114} CliprdrFuseClipDataEntry;
115
116typedef struct
117{
118 CliprdrFileContext* file_context;
119
120 wArrayList* fuse_files;
121
122 BOOL all_files;
123 BOOL has_clip_data_id;
124 UINT32 clip_data_id;
125} FuseFileClearContext;
126
127typedef struct
128{
129 FuseLowlevelOperationType operation_type;
130 CliprdrFuseFile* fuse_file;
131 fuse_req_t fuse_req;
132 UINT32 stream_id;
133} CliprdrFuseRequest;
134
135typedef struct
136{
137 CliprdrFuseFile* parent;
138 char* parent_path;
139} CliprdrFuseFindParentContext;
140#endif
141
142typedef struct
143{
144 char* name;
145 FILE* fp;
146 INT64 size;
147 CliprdrFileContext* context;
148} CliprdrLocalFile;
149
150typedef struct
151{
152 UINT32 lockId;
153 BOOL locked;
154 size_t count;
155 CliprdrLocalFile* files;
156 CliprdrFileContext* context;
157} CliprdrLocalStream;
158
159struct cliprdr_file_context
160{
161#if defined(WITH_FUSE)
162 /* FUSE related**/
163 HANDLE fuse_start_sync;
164 HANDLE fuse_stop_sync;
165 HANDLE fuse_thread;
166 struct fuse_session* fuse_sess;
167#if FUSE_USE_VERSION < 30
168 struct fuse_chan* ch;
169#endif
170
171 wHashTable* inode_table;
172 wHashTable* clip_data_table;
173 wHashTable* request_table;
174
175 CliprdrFuseClipDataEntry* clip_data_entry_without_id;
176 UINT32 current_clip_data_id;
177
178 fuse_ino_t next_ino;
179 UINT32 next_clip_data_id;
180 UINT32 next_stream_id;
181#endif
182
183 /* File clipping */
184 BOOL file_formats_registered;
185 UINT32 file_capability_flags;
186
187 UINT32 local_lock_id;
188
189 wHashTable* local_streams;
190 wLog* log;
191 void* clipboard;
192 CliprdrClientContext* context;
193 char* path;
194 char* exposed_path;
195 BYTE server_data_hash[WINPR_SHA256_DIGEST_LENGTH];
196 BYTE client_data_hash[WINPR_SHA256_DIGEST_LENGTH];
197};
198
199#if defined(WITH_FUSE)
200static CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino);
201
202static void fuse_file_free(void* data)
203{
204 CliprdrFuseFile* fuse_file = data;
205
206 if (!fuse_file)
207 return;
208
209 ArrayList_Free(fuse_file->children);
210 free(fuse_file->filename_with_root);
211
212 free(fuse_file);
213}
214
215WINPR_ATTR_FORMAT_ARG(1, 2)
216WINPR_ATTR_MALLOC(fuse_file_free, 1)
217static CliprdrFuseFile* fuse_file_new(WINPR_FORMAT_ARG const char* fmt, ...)
218{
219 CliprdrFuseFile* file = calloc(1, sizeof(CliprdrFuseFile));
220 if (!file)
221 return NULL;
222
223 file->children = ArrayList_New(FALSE);
224 if (!file->children)
225 goto fail;
226
227 WINPR_ASSERT(fmt);
228
229 {
230 va_list ap;
231 va_start(ap, fmt);
232 const int rc =
233 winpr_vasprintf(&file->filename_with_root, &file->filename_with_root_len, fmt, ap);
234 va_end(ap);
235
236 if (rc < 0)
237 goto fail;
238 }
239
240 if (file->filename_with_root && (file->filename_with_root_len > 0))
241 {
242 file->filename_len = 0;
243 file->filename = strrchr(file->filename_with_root, '/') + 1;
244 if (file->filename)
245 file->filename_len = strnlen(file->filename, file->filename_with_root_len);
246 }
247 return file;
248fail:
249 fuse_file_free(file);
250 return NULL;
251}
252
253static void clip_data_entry_free(void* data)
254{
255 CliprdrFuseClipDataEntry* clip_data_entry = data;
256
257 if (!clip_data_entry)
258 return;
259
260 if (clip_data_entry->has_clip_data_id)
261 {
262 CliprdrFileContext* file_context = clip_data_entry->file_context;
263 CLIPRDR_UNLOCK_CLIPBOARD_DATA unlock_clipboard_data = { 0 };
264
265 WINPR_ASSERT(file_context);
266
267 unlock_clipboard_data.common.msgType = CB_UNLOCK_CLIPDATA;
268 unlock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
269
270 file_context->context->ClientUnlockClipboardData(file_context->context,
271 &unlock_clipboard_data);
272 clip_data_entry->has_clip_data_id = FALSE;
273
274 WLog_Print(file_context->log, WLOG_DEBUG, "Destroyed ClipDataEntry with id %u",
275 clip_data_entry->clip_data_id);
276 }
277
278 free(clip_data_entry);
279}
280
281static BOOL does_server_support_clipdata_locking(CliprdrFileContext* file_context)
282{
283 WINPR_ASSERT(file_context);
284
285 if (cliprdr_file_context_remote_get_flags(file_context) & CB_CAN_LOCK_CLIPDATA)
286 return TRUE;
287
288 return FALSE;
289}
290
291static UINT32 get_next_free_clip_data_id(CliprdrFileContext* file_context)
292{
293 UINT32 clip_data_id = 0;
294
295 WINPR_ASSERT(file_context);
296
297 HashTable_Lock(file_context->inode_table);
298 clip_data_id = file_context->next_clip_data_id;
299 while (clip_data_id == 0 ||
300 HashTable_GetItemValue(file_context->clip_data_table, (void*)(uintptr_t)clip_data_id))
301 ++clip_data_id;
302
303 file_context->next_clip_data_id = clip_data_id + 1;
304 HashTable_Unlock(file_context->inode_table);
305
306 return clip_data_id;
307}
308
309static CliprdrFuseClipDataEntry* clip_data_entry_new(CliprdrFileContext* file_context,
310 BOOL needs_clip_data_id)
311{
312 CliprdrFuseClipDataEntry* clip_data_entry = NULL;
313 CLIPRDR_LOCK_CLIPBOARD_DATA lock_clipboard_data = { 0 };
314
315 WINPR_ASSERT(file_context);
316
317 clip_data_entry = calloc(1, sizeof(CliprdrFuseClipDataEntry));
318 if (!clip_data_entry)
319 return NULL;
320
321 clip_data_entry->file_context = file_context;
322 clip_data_entry->clip_data_id = get_next_free_clip_data_id(file_context);
323
324 if (!needs_clip_data_id)
325 return clip_data_entry;
326
327 lock_clipboard_data.common.msgType = CB_LOCK_CLIPDATA;
328 lock_clipboard_data.clipDataId = clip_data_entry->clip_data_id;
329
330 if (file_context->context->ClientLockClipboardData(file_context->context, &lock_clipboard_data))
331 {
332 HashTable_Lock(file_context->inode_table);
333 clip_data_entry_free(clip_data_entry);
334 HashTable_Unlock(file_context->inode_table);
335 return NULL;
336 }
337 clip_data_entry->has_clip_data_id = TRUE;
338
339 WLog_Print(file_context->log, WLOG_DEBUG, "Created ClipDataEntry with id %u",
340 clip_data_entry->clip_data_id);
341
342 return clip_data_entry;
343}
344
345static BOOL should_remove_fuse_file(CliprdrFuseFile* fuse_file, BOOL all_files,
346 BOOL has_clip_data_id, UINT32 clip_data_id)
347{
348 if (all_files)
349 return TRUE;
350
351 if (fuse_file->ino == FUSE_ROOT_ID)
352 return FALSE;
353 if (!fuse_file->has_clip_data_id && !has_clip_data_id)
354 return TRUE;
355 if (fuse_file->has_clip_data_id && has_clip_data_id &&
356 (fuse_file->clip_data_id == clip_data_id))
357 return TRUE;
358
359 return FALSE;
360}
361
362static BOOL maybe_clear_fuse_request(const void* key, void* value, void* arg)
363{
364 CliprdrFuseRequest* fuse_request = value;
365 FuseFileClearContext* clear_context = arg;
366 CliprdrFileContext* file_context = clear_context->file_context;
367 CliprdrFuseFile* fuse_file = fuse_request->fuse_file;
368
369 WINPR_ASSERT(file_context);
370
371 if (!should_remove_fuse_file(fuse_file, clear_context->all_files,
372 clear_context->has_clip_data_id, clear_context->clip_data_id))
373 return TRUE;
374
375 DEBUG_CLIPRDR(file_context->log, "Clearing FileContentsRequest for file \"%s\"",
376 fuse_file->filename_with_root);
377
378 fuse_reply_err(fuse_request->fuse_req, EIO);
379 HashTable_Remove(file_context->request_table, key);
380
381 return TRUE;
382}
383
384static BOOL maybe_steal_inode(const void* key, void* value, void* arg)
385{
386 CliprdrFuseFile* fuse_file = value;
387 FuseFileClearContext* clear_context = arg;
388 CliprdrFileContext* file_context = clear_context->file_context;
389
390 WINPR_ASSERT(file_context);
391
392 if (should_remove_fuse_file(fuse_file, clear_context->all_files,
393 clear_context->has_clip_data_id, clear_context->clip_data_id))
394 {
395 if (!ArrayList_Append(clear_context->fuse_files, fuse_file))
396 WLog_Print(file_context->log, WLOG_ERROR,
397 "Failed to append FUSE file to list for deletion");
398
399 HashTable_Remove(file_context->inode_table, key);
400 }
401
402 return TRUE;
403}
404
405static BOOL notify_delete_child(void* data, WINPR_ATTR_UNUSED size_t index, va_list ap)
406{
407 CliprdrFuseFile* child = data;
408
409 WINPR_ASSERT(child);
410
411 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
412 CliprdrFuseFile* parent = va_arg(ap, CliprdrFuseFile*);
413
414 WINPR_ASSERT(file_context);
415 WINPR_ASSERT(parent);
416
417 WINPR_ASSERT(file_context->fuse_sess);
418 fuse_lowlevel_notify_delete(file_context->fuse_sess, parent->ino, child->ino, child->filename,
419 child->filename_len);
420
421 return TRUE;
422}
423
424static BOOL invalidate_inode(void* data, WINPR_ATTR_UNUSED size_t index, va_list ap)
425{
426 CliprdrFuseFile* fuse_file = data;
427
428 WINPR_ASSERT(fuse_file);
429
430 CliprdrFileContext* file_context = va_arg(ap, CliprdrFileContext*);
431 WINPR_ASSERT(file_context);
432 WINPR_ASSERT(file_context->fuse_sess);
433
434 ArrayList_ForEach(fuse_file->children, notify_delete_child, file_context, fuse_file);
435
436 DEBUG_CLIPRDR(file_context->log, "Invalidating inode %lu for file \"%s\"", fuse_file->ino,
437 fuse_file->filename);
438 fuse_lowlevel_notify_inval_inode(file_context->fuse_sess, fuse_file->ino, 0, 0);
439 WLog_Print(file_context->log, WLOG_DEBUG, "Inode %lu invalidated", fuse_file->ino);
440
441 return TRUE;
442}
443
444static void clear_selection(CliprdrFileContext* file_context, BOOL all_selections,
445 CliprdrFuseClipDataEntry* clip_data_entry)
446{
447 FuseFileClearContext clear_context = { 0 };
448 CliprdrFuseFile* clip_data_dir = NULL;
449
450 WINPR_ASSERT(file_context);
451
452 clear_context.file_context = file_context;
453 clear_context.fuse_files = ArrayList_New(FALSE);
454 WINPR_ASSERT(clear_context.fuse_files);
455
456 wObject* aobj = ArrayList_Object(clear_context.fuse_files);
457 WINPR_ASSERT(aobj);
458 aobj->fnObjectFree = fuse_file_free;
459
460 if (clip_data_entry)
461 {
462 clip_data_dir = clip_data_entry->clip_data_dir;
463 clip_data_entry->clip_data_dir = NULL;
464
465 WINPR_ASSERT(clip_data_dir);
466
467 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
468 if (root_dir)
469 ArrayList_Remove(root_dir->children, clip_data_dir);
470
471 clear_context.has_clip_data_id = clip_data_dir->has_clip_data_id;
472 clear_context.clip_data_id = clip_data_dir->clip_data_id;
473 }
474 clear_context.all_files = all_selections;
475
476 if (clip_data_entry && clip_data_entry->has_clip_data_id)
477 WLog_Print(file_context->log, WLOG_DEBUG, "Clearing selection for clipDataId %u",
478 clip_data_entry->clip_data_id);
479 else
480 WLog_Print(file_context->log, WLOG_DEBUG, "Clearing selection%s",
481 all_selections ? "s" : "");
482
483 HashTable_Foreach(file_context->request_table, maybe_clear_fuse_request, &clear_context);
484 HashTable_Foreach(file_context->inode_table, maybe_steal_inode, &clear_context);
485 HashTable_Unlock(file_context->inode_table);
486
487 if (file_context->fuse_sess)
488 {
489 /*
490 * fuse_lowlevel_notify_inval_inode() is a blocking operation. If we receive a
491 * FUSE request (e.g. read()), then FUSE would block in read(), since the
492 * mutex of the inode_table would still be locked, if we wouldn't unlock it
493 * here.
494 * So, to avoid a deadlock here, unlock the mutex and reply all incoming
495 * operations with -ENOENT until the invalidation process is complete.
496 */
497 ArrayList_ForEach(clear_context.fuse_files, invalidate_inode, file_context);
498 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
499 if (clip_data_dir && root_dir)
500 {
501 fuse_lowlevel_notify_delete(file_context->fuse_sess, root_dir->ino, clip_data_dir->ino,
502 clip_data_dir->filename, clip_data_dir->filename_len);
503 }
504 }
505 ArrayList_Free(clear_context.fuse_files);
506
507 HashTable_Lock(file_context->inode_table);
508 if (clip_data_entry && clip_data_entry->has_clip_data_id)
509 WLog_Print(file_context->log, WLOG_DEBUG, "Selection cleared for clipDataId %u",
510 clip_data_entry->clip_data_id);
511 else
512 WLog_Print(file_context->log, WLOG_DEBUG, "Selection%s cleared", all_selections ? "s" : "");
513}
514
515static void clear_entry_selection(CliprdrFuseClipDataEntry* clip_data_entry)
516{
517 WINPR_ASSERT(clip_data_entry);
518
519 if (!clip_data_entry->clip_data_dir)
520 return;
521
522 clear_selection(clip_data_entry->file_context, FALSE, clip_data_entry);
523}
524
525static void clear_no_cdi_entry(CliprdrFileContext* file_context)
526{
527 WINPR_ASSERT(file_context);
528
529 WINPR_ASSERT(file_context->inode_table);
530 HashTable_Lock(file_context->inode_table);
531 if (file_context->clip_data_entry_without_id)
532 {
533 clear_entry_selection(file_context->clip_data_entry_without_id);
534
535 clip_data_entry_free(file_context->clip_data_entry_without_id);
536 file_context->clip_data_entry_without_id = NULL;
537 }
538 HashTable_Unlock(file_context->inode_table);
539}
540
541static BOOL clear_clip_data_entries(WINPR_ATTR_UNUSED const void* key, void* value,
542 WINPR_ATTR_UNUSED void* arg)
543{
544 clear_entry_selection(value);
545
546 return TRUE;
547}
548
549static void clear_cdi_entries(CliprdrFileContext* file_context)
550{
551 WINPR_ASSERT(file_context);
552
553 HashTable_Lock(file_context->inode_table);
554 HashTable_Foreach(file_context->clip_data_table, clear_clip_data_entries, NULL);
555
556 HashTable_Clear(file_context->clip_data_table);
557 HashTable_Unlock(file_context->inode_table);
558}
559
560static UINT prepare_clip_data_entry_with_id(CliprdrFileContext* file_context)
561{
562 CliprdrFuseClipDataEntry* clip_data_entry = NULL;
563
564 WINPR_ASSERT(file_context);
565
566 clip_data_entry = clip_data_entry_new(file_context, TRUE);
567 if (!clip_data_entry)
568 {
569 WLog_Print(file_context->log, WLOG_ERROR, "Failed to create clipDataEntry");
570 return ERROR_INTERNAL_ERROR;
571 }
572
573 HashTable_Lock(file_context->inode_table);
574 if (!HashTable_Insert(file_context->clip_data_table,
575 (void*)(uintptr_t)clip_data_entry->clip_data_id, clip_data_entry))
576 {
577 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert clipDataEntry");
578 clip_data_entry_free(clip_data_entry);
579 HashTable_Unlock(file_context->inode_table);
580 return ERROR_INTERNAL_ERROR;
581 }
582 HashTable_Unlock(file_context->inode_table);
583
584 // HashTable_Insert owns clip_data_entry
585 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
586 file_context->current_clip_data_id = clip_data_entry->clip_data_id;
587
588 return CHANNEL_RC_OK;
589}
590
591static UINT prepare_clip_data_entry_without_id(CliprdrFileContext* file_context)
592{
593 WINPR_ASSERT(file_context);
594 WINPR_ASSERT(!file_context->clip_data_entry_without_id);
595
596 file_context->clip_data_entry_without_id = clip_data_entry_new(file_context, FALSE);
597 if (!file_context->clip_data_entry_without_id)
598 {
599 WLog_Print(file_context->log, WLOG_ERROR, "Failed to create clipDataEntry");
600 return ERROR_INTERNAL_ERROR;
601 }
602
603 return CHANNEL_RC_OK;
604}
605#endif
606
607UINT cliprdr_file_context_notify_new_server_format_list(CliprdrFileContext* file_context)
608{
609 UINT rc = CHANNEL_RC_OK;
610 WINPR_ASSERT(file_context);
611 WINPR_ASSERT(file_context->context);
612
613#if defined(WITH_FUSE)
614 clear_no_cdi_entry(file_context);
615 /* TODO: assign timeouts to old locks instead */
616 clear_cdi_entries(file_context);
617
618 if (does_server_support_clipdata_locking(file_context))
619 rc = prepare_clip_data_entry_with_id(file_context);
620 else
621 rc = prepare_clip_data_entry_without_id(file_context);
622#endif
623 return rc;
624}
625
626UINT cliprdr_file_context_notify_new_client_format_list(CliprdrFileContext* file_context)
627{
628 WINPR_ASSERT(file_context);
629 WINPR_ASSERT(file_context->context);
630
631#if defined(WITH_FUSE)
632 clear_no_cdi_entry(file_context);
633 /* TODO: assign timeouts to old locks instead */
634 clear_cdi_entries(file_context);
635#endif
636
637 return CHANNEL_RC_OK;
638}
639
640static CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
641 const char* data, size_t size);
642static void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread);
643static BOOL local_stream_discard(const void* key, void* value, void* arg);
644
645WINPR_ATTR_FORMAT_ARG(6, 7)
646static void writelog(wLog* log, DWORD level, const char* fname, const char* fkt, size_t line,
647 WINPR_FORMAT_ARG const char* fmt, ...)
648{
649 if (!WLog_IsLevelActive(log, level))
650 return;
651
652 va_list ap = { 0 };
653 va_start(ap, fmt);
654 WLog_PrintTextMessageVA(log, level, line, fname, fkt, fmt, ap);
655 va_end(ap);
656}
657
658#if defined(WITH_FUSE)
659static CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context);
660static void cliprdr_file_fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char* name);
661static void cliprdr_file_fuse_getattr(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi);
662static void cliprdr_file_fuse_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
663 struct fuse_file_info* fi);
664static void cliprdr_file_fuse_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
665 struct fuse_file_info* fi);
666static void cliprdr_file_fuse_open(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi);
667static void cliprdr_file_fuse_opendir(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi);
668
669static const struct fuse_lowlevel_ops cliprdr_file_fuse_oper = {
670 .lookup = cliprdr_file_fuse_lookup,
671 .getattr = cliprdr_file_fuse_getattr,
672 .readdir = cliprdr_file_fuse_readdir,
673 .open = cliprdr_file_fuse_open,
674 .read = cliprdr_file_fuse_read,
675 .opendir = cliprdr_file_fuse_opendir,
676};
677
678CliprdrFuseFile* get_fuse_file_by_ino(CliprdrFileContext* file_context, fuse_ino_t fuse_ino)
679{
680 WINPR_ASSERT(file_context);
681
682 return HashTable_GetItemValue(file_context->inode_table, (void*)(uintptr_t)fuse_ino);
683}
684
685static CliprdrFuseFile*
686get_fuse_file_by_name_from_parent(WINPR_ATTR_UNUSED CliprdrFileContext* file_context,
687 CliprdrFuseFile* parent, const char* name)
688{
689 WINPR_ASSERT(file_context);
690 WINPR_ASSERT(parent);
691
692 for (size_t i = 0; i < ArrayList_Count(parent->children); ++i)
693 {
694 CliprdrFuseFile* child = ArrayList_GetItem(parent->children, i);
695
696 WINPR_ASSERT(child);
697
698 if (strncmp(name, child->filename, child->filename_len + 1) == 0)
699 return child;
700 }
701
702 DEBUG_CLIPRDR(file_context->log, "Requested file \"%s\" in directory \"%s\" does not exist",
703 name, parent->filename);
704
705 return NULL;
706}
707
708static CliprdrFuseRequest* cliprdr_fuse_request_new(CliprdrFileContext* file_context,
709 CliprdrFuseFile* fuse_file, fuse_req_t fuse_req,
710 FuseLowlevelOperationType operation_type)
711{
712 CliprdrFuseRequest* fuse_request = NULL;
713 UINT32 stream_id = file_context->next_stream_id;
714
715 WINPR_ASSERT(file_context);
716 WINPR_ASSERT(fuse_file);
717
718 fuse_request = calloc(1, sizeof(CliprdrFuseRequest));
719 if (!fuse_request)
720 {
721 WLog_Print(file_context->log, WLOG_ERROR, "Failed to allocate FUSE request for file \"%s\"",
722 fuse_file->filename_with_root);
723 return NULL;
724 }
725
726 fuse_request->fuse_file = fuse_file;
727 fuse_request->fuse_req = fuse_req;
728 fuse_request->operation_type = operation_type;
729
730 while (stream_id == 0 ||
731 HashTable_GetItemValue(file_context->request_table, (void*)(uintptr_t)stream_id))
732 ++stream_id;
733 fuse_request->stream_id = stream_id;
734
735 file_context->next_stream_id = stream_id + 1;
736
737 if (!HashTable_Insert(file_context->request_table, (void*)(uintptr_t)fuse_request->stream_id,
738 fuse_request))
739 {
740 WLog_Print(file_context->log, WLOG_ERROR, "Failed to track FUSE request for file \"%s\"",
741 fuse_file->filename_with_root);
742 free(fuse_request);
743 return NULL;
744 }
745
746 return fuse_request;
747}
748
749static BOOL request_file_size_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
750 fuse_req_t fuse_req, FuseLowlevelOperationType operation_type)
751{
752 CliprdrFuseRequest* fuse_request = NULL;
753 CLIPRDR_FILE_CONTENTS_REQUEST file_contents_request = { 0 };
754
755 WINPR_ASSERT(file_context);
756 WINPR_ASSERT(fuse_file);
757
758 fuse_request = cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, operation_type);
759 if (!fuse_request)
760 return FALSE;
761
762 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
763 file_contents_request.streamId = fuse_request->stream_id;
764 file_contents_request.listIndex = fuse_file->list_idx;
765 file_contents_request.dwFlags = FILECONTENTS_SIZE;
766 file_contents_request.cbRequested = 0x8;
767 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
768 file_contents_request.clipDataId = fuse_file->clip_data_id;
769
770 if (file_context->context->ClientFileContentsRequest(file_context->context,
771 &file_contents_request))
772 {
773 WLog_Print(file_context->log, WLOG_ERROR,
774 "Failed to send FileContentsRequest for file \"%s\"",
775 fuse_file->filename_with_root);
776 HashTable_Remove(file_context->request_table, (void*)(uintptr_t)fuse_request->stream_id);
777 return FALSE;
778 }
779 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
780 DEBUG_CLIPRDR(file_context->log, "Requested file size for file \"%s\" with stream id %u",
781 fuse_file->filename, fuse_request->stream_id);
782
783 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
784 return TRUE;
785}
786
787static void write_file_attributes(CliprdrFuseFile* fuse_file, struct stat* attr)
788{
789 memset(attr, 0, sizeof(struct stat));
790
791 if (!fuse_file)
792 return;
793
794 attr->st_ino = fuse_file->ino;
795 if (fuse_file->is_directory)
796 {
797 attr->st_mode = S_IFDIR | (fuse_file->is_readonly ? 0555 : 0755);
798 attr->st_nlink = 2;
799 }
800 else
801 {
802 attr->st_mode = S_IFREG | (fuse_file->is_readonly ? 0444 : 0644);
803 attr->st_nlink = 1;
804 attr->st_size = WINPR_ASSERTING_INT_CAST(off_t, fuse_file->size);
805 }
806 attr->st_uid = getuid();
807 attr->st_gid = getgid();
808 attr->st_atime = attr->st_mtime = attr->st_ctime =
809 (fuse_file->has_last_write_time ? fuse_file->last_write_time_unix : time(NULL));
810}
811
812static void cliprdr_file_fuse_lookup(fuse_req_t fuse_req, fuse_ino_t parent_ino, const char* name)
813{
814 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
815 CliprdrFuseFile* parent = NULL;
816 CliprdrFuseFile* fuse_file = NULL;
817 struct fuse_entry_param entry = { 0 };
818
819 WINPR_ASSERT(file_context);
820
821 HashTable_Lock(file_context->inode_table);
822 if (!(parent = get_fuse_file_by_ino(file_context, parent_ino)) || !parent->is_directory)
823 {
824 HashTable_Unlock(file_context->inode_table);
825 fuse_reply_err(fuse_req, ENOENT);
826 return;
827 }
828 if (!(fuse_file = get_fuse_file_by_name_from_parent(file_context, parent, name)))
829 {
830 HashTable_Unlock(file_context->inode_table);
831 fuse_reply_err(fuse_req, ENOENT);
832 return;
833 }
834
835 DEBUG_CLIPRDR(file_context->log, "lookup() has been called for \"%s\"", name);
836 DEBUG_CLIPRDR(file_context->log, "Parent is \"%s\", child is \"%s\"",
837 parent->filename_with_root, fuse_file->filename_with_root);
838
839 if (!fuse_file->is_directory && !fuse_file->has_size)
840 {
841 BOOL result = 0;
842
843 result =
844 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_LOOKUP);
845 HashTable_Unlock(file_context->inode_table);
846
847 if (!result)
848 fuse_reply_err(fuse_req, EIO);
849
850 return;
851 }
852
853 entry.ino = fuse_file->ino;
854 write_file_attributes(fuse_file, &entry.attr);
855 entry.attr_timeout = 1.0;
856 entry.entry_timeout = 1.0;
857 HashTable_Unlock(file_context->inode_table);
858
859 fuse_reply_entry(fuse_req, &entry);
860}
861
862static void cliprdr_file_fuse_getattr(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
863 WINPR_ATTR_UNUSED struct fuse_file_info* file_info)
864{
865 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
866 CliprdrFuseFile* fuse_file = NULL;
867 struct stat attr = { 0 };
868
869 WINPR_ASSERT(file_context);
870
871 HashTable_Lock(file_context->inode_table);
872 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
873 {
874 HashTable_Unlock(file_context->inode_table);
875 fuse_reply_err(fuse_req, ENOENT);
876 return;
877 }
878
879 DEBUG_CLIPRDR(file_context->log, "getattr() has been called for file \"%s\"",
880 fuse_file->filename_with_root);
881
882 if (!fuse_file->is_directory && !fuse_file->has_size)
883 {
884 BOOL result = 0;
885
886 result =
887 request_file_size_async(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_GETATTR);
888 HashTable_Unlock(file_context->inode_table);
889
890 if (!result)
891 fuse_reply_err(fuse_req, EIO);
892
893 return;
894 }
895
896 write_file_attributes(fuse_file, &attr);
897 HashTable_Unlock(file_context->inode_table);
898
899 fuse_reply_attr(fuse_req, &attr, 1.0);
900}
901
902static void cliprdr_file_fuse_open(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
903 struct fuse_file_info* file_info)
904{
905 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
906 CliprdrFuseFile* fuse_file = NULL;
907
908 WINPR_ASSERT(file_context);
909
910 HashTable_Lock(file_context->inode_table);
911 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
912 {
913 HashTable_Unlock(file_context->inode_table);
914 fuse_reply_err(fuse_req, ENOENT);
915 return;
916 }
917 if (fuse_file->is_directory)
918 {
919 HashTable_Unlock(file_context->inode_table);
920 fuse_reply_err(fuse_req, EISDIR);
921 return;
922 }
923 HashTable_Unlock(file_context->inode_table);
924
925 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
926 {
927 fuse_reply_err(fuse_req, EACCES);
928 return;
929 }
930
931 /* Important for KDE to get file correctly */
932 file_info->direct_io = 1;
933
934 fuse_reply_open(fuse_req, file_info);
935}
936
937static BOOL request_file_range_async(CliprdrFileContext* file_context, CliprdrFuseFile* fuse_file,
938 fuse_req_t fuse_req, off_t offset, size_t requested_size)
939{
940 CLIPRDR_FILE_CONTENTS_REQUEST file_contents_request = { 0 };
941
942 WINPR_ASSERT(file_context);
943 WINPR_ASSERT(fuse_file);
944
945 if (requested_size > UINT32_MAX)
946 return FALSE;
947
948 CliprdrFuseRequest* fuse_request =
949 cliprdr_fuse_request_new(file_context, fuse_file, fuse_req, FUSE_LL_OPERATION_READ);
950 if (!fuse_request)
951 return FALSE;
952
953 file_contents_request.common.msgType = CB_FILECONTENTS_REQUEST;
954 file_contents_request.streamId = fuse_request->stream_id;
955 file_contents_request.listIndex = fuse_file->list_idx;
956 file_contents_request.dwFlags = FILECONTENTS_RANGE;
957 file_contents_request.nPositionLow = (UINT32)(offset & 0xFFFFFFFF);
958 file_contents_request.nPositionHigh = (UINT32)((offset >> 32) & 0xFFFFFFFF);
959 file_contents_request.cbRequested = (UINT32)requested_size;
960 file_contents_request.haveClipDataId = fuse_file->has_clip_data_id;
961 file_contents_request.clipDataId = fuse_file->clip_data_id;
962
963 if (file_context->context->ClientFileContentsRequest(file_context->context,
964 &file_contents_request))
965 {
966 WLog_Print(file_context->log, WLOG_ERROR,
967 "Failed to send FileContentsRequest for file \"%s\"",
968 fuse_file->filename_with_root);
969 HashTable_Remove(file_context->request_table, (void*)(uintptr_t)fuse_request->stream_id);
970 return FALSE;
971 }
972
973 // file_context->request_table owns fuse_request
974 // NOLINTBEGIN(clang-analyzer-unix.Malloc)
975 DEBUG_CLIPRDR(
976 file_context->log,
977 "Requested file range (%zu Bytes at offset %lu) for file \"%s\" with stream id %u",
978 requested_size, offset, fuse_file->filename, fuse_request->stream_id);
979
980 return TRUE;
981 // NOLINTEND(clang-analyzer-unix.Malloc)
982}
983
984static void cliprdr_file_fuse_read(fuse_req_t fuse_req, fuse_ino_t fuse_ino, size_t size,
985 off_t offset, WINPR_ATTR_UNUSED struct fuse_file_info* file_info)
986{
987 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
988 CliprdrFuseFile* fuse_file = NULL;
989 BOOL result = 0;
990
991 WINPR_ASSERT(file_context);
992
993 HashTable_Lock(file_context->inode_table);
994 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
995 {
996 HashTable_Unlock(file_context->inode_table);
997 fuse_reply_err(fuse_req, ENOENT);
998 return;
999 }
1000 if (fuse_file->is_directory)
1001 {
1002 HashTable_Unlock(file_context->inode_table);
1003 fuse_reply_err(fuse_req, EISDIR);
1004 return;
1005 }
1006 if (!fuse_file->has_size || (offset < 0) || ((size_t)offset > fuse_file->size))
1007 {
1008 HashTable_Unlock(file_context->inode_table);
1009 fuse_reply_err(fuse_req, EINVAL);
1010 return;
1011 }
1012
1013 size = MIN(size, 8ULL * 1024ULL * 1024ULL);
1014
1015 result = request_file_range_async(file_context, fuse_file, fuse_req, offset, size);
1016 HashTable_Unlock(file_context->inode_table);
1017
1018 if (!result)
1019 fuse_reply_err(fuse_req, EIO);
1020}
1021
1022static void cliprdr_file_fuse_opendir(fuse_req_t fuse_req, fuse_ino_t fuse_ino,
1023 struct fuse_file_info* file_info)
1024{
1025 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1026 CliprdrFuseFile* fuse_file = NULL;
1027
1028 WINPR_ASSERT(file_context);
1029
1030 HashTable_Lock(file_context->inode_table);
1031 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1032 {
1033 HashTable_Unlock(file_context->inode_table);
1034 fuse_reply_err(fuse_req, ENOENT);
1035 return;
1036 }
1037 if (!fuse_file->is_directory)
1038 {
1039 HashTable_Unlock(file_context->inode_table);
1040 fuse_reply_err(fuse_req, ENOTDIR);
1041 return;
1042 }
1043 HashTable_Unlock(file_context->inode_table);
1044
1045 if ((file_info->flags & O_ACCMODE) != O_RDONLY)
1046 {
1047 fuse_reply_err(fuse_req, EACCES);
1048 return;
1049 }
1050
1051 fuse_reply_open(fuse_req, file_info);
1052}
1053
1054static void cliprdr_file_fuse_readdir(fuse_req_t fuse_req, fuse_ino_t fuse_ino, size_t max_size,
1055 off_t offset,
1056 WINPR_ATTR_UNUSED struct fuse_file_info* file_info)
1057{
1058 CliprdrFileContext* file_context = fuse_req_userdata(fuse_req);
1059 CliprdrFuseFile* fuse_file = NULL;
1060 CliprdrFuseFile* child = NULL;
1061 struct stat attr = { 0 };
1062 size_t written_size = 0;
1063 size_t entry_size = 0;
1064 char* filename = NULL;
1065 char* buf = NULL;
1066
1067 WINPR_ASSERT(file_context);
1068
1069 HashTable_Lock(file_context->inode_table);
1070 if (!(fuse_file = get_fuse_file_by_ino(file_context, fuse_ino)))
1071 {
1072 HashTable_Unlock(file_context->inode_table);
1073 fuse_reply_err(fuse_req, ENOENT);
1074 return;
1075 }
1076 if (!fuse_file->is_directory)
1077 {
1078 HashTable_Unlock(file_context->inode_table);
1079 fuse_reply_err(fuse_req, ENOTDIR);
1080 return;
1081 }
1082
1083 DEBUG_CLIPRDR(file_context->log, "Reading directory \"%s\" at offset %lu",
1084 fuse_file->filename_with_root, offset);
1085
1086 if ((offset < 0) || ((size_t)offset >= ArrayList_Count(fuse_file->children)))
1087 {
1088 HashTable_Unlock(file_context->inode_table);
1089 fuse_reply_buf(fuse_req, NULL, 0);
1090 return;
1091 }
1092
1093 buf = calloc(max_size, sizeof(char));
1094 if (!buf)
1095 {
1096 HashTable_Unlock(file_context->inode_table);
1097 fuse_reply_err(fuse_req, ENOMEM);
1098 return;
1099 }
1100 written_size = 0;
1101
1102 for (off_t i = offset; i < 2; ++i)
1103 {
1104 if (i == 0)
1105 {
1106 write_file_attributes(fuse_file, &attr);
1107 filename = ".";
1108 }
1109 else if (i == 1)
1110 {
1111 write_file_attributes(fuse_file->parent, &attr);
1112 attr.st_ino = fuse_file->parent ? attr.st_ino : FUSE_ROOT_ID;
1113 attr.st_mode = fuse_file->parent ? attr.st_mode : 0555;
1114 filename = "..";
1115 }
1116 else
1117 {
1118 WINPR_ASSERT(FALSE);
1119 }
1120
1125 entry_size = fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1126 filename, &attr, i + 1);
1127 if (entry_size > max_size - written_size)
1128 break;
1129
1130 written_size += entry_size;
1131 }
1132
1133 for (size_t j = 0, i = 2; j < ArrayList_Count(fuse_file->children); ++j, ++i)
1134 {
1135 if (i < (size_t)offset)
1136 continue;
1137
1138 child = ArrayList_GetItem(fuse_file->children, j);
1139
1140 write_file_attributes(child, &attr);
1141 entry_size =
1142 fuse_add_direntry(fuse_req, buf + written_size, max_size - written_size,
1143 child->filename, &attr, WINPR_ASSERTING_INT_CAST(off_t, i + 1));
1144 if (entry_size > max_size - written_size)
1145 break;
1146
1147 written_size += entry_size;
1148 }
1149 HashTable_Unlock(file_context->inode_table);
1150
1151 fuse_reply_buf(fuse_req, buf, written_size);
1152 free(buf);
1153}
1154
1155static void fuse_abort(int sig, const char* signame, void* context)
1156{
1157 CliprdrFileContext* file = (CliprdrFileContext*)context;
1158
1159 if (file)
1160 {
1161 WLog_Print(file->log, WLOG_INFO, "signal %s [%d] aborting session", signame, sig);
1162 cliprdr_file_session_terminate(file, FALSE);
1163 }
1164}
1165
1166static DWORD WINAPI cliprdr_file_fuse_thread(LPVOID arg)
1167{
1168 CliprdrFileContext* file = (CliprdrFileContext*)arg;
1169
1170 WINPR_ASSERT(file);
1171
1172 DEBUG_CLIPRDR(file->log, "Starting fuse with mountpoint '%s'", file->path);
1173
1174 struct fuse_args args = FUSE_ARGS_INIT(0, NULL);
1175 fuse_opt_add_arg(&args, file->path);
1176 file->fuse_sess = fuse_session_new(&args, &cliprdr_file_fuse_oper,
1177 sizeof(cliprdr_file_fuse_oper), (void*)file);
1178 (void)SetEvent(file->fuse_start_sync);
1179
1180 if (file->fuse_sess != NULL)
1181 {
1182 freerdp_add_signal_cleanup_handler(file, fuse_abort);
1183 if (0 == fuse_session_mount(file->fuse_sess, file->path))
1184 {
1185 fuse_session_loop(file->fuse_sess);
1186 fuse_session_unmount(file->fuse_sess);
1187 }
1188 freerdp_del_signal_cleanup_handler(file, fuse_abort);
1189
1190 WLog_Print(file->log, WLOG_DEBUG, "Waiting for FUSE stop sync");
1191 if (WaitForSingleObject(file->fuse_stop_sync, INFINITE) == WAIT_FAILED)
1192 WLog_Print(file->log, WLOG_ERROR, "Failed to wait for stop sync");
1193 fuse_session_destroy(file->fuse_sess);
1194 }
1195 fuse_opt_free_args(&args);
1196
1197 DEBUG_CLIPRDR(file->log, "Quitting fuse with mountpoint '%s'", file->path);
1198
1199 ExitThread(0);
1200 return 0;
1201}
1202
1203static UINT cliprdr_file_context_server_file_contents_response(
1204 CliprdrClientContext* cliprdr_context,
1205 const CLIPRDR_FILE_CONTENTS_RESPONSE* file_contents_response)
1206{
1207 CliprdrFileContext* file_context = NULL;
1208 CliprdrFuseRequest* fuse_request = NULL;
1209 struct fuse_entry_param entry = { 0 };
1210
1211 WINPR_ASSERT(cliprdr_context);
1212 WINPR_ASSERT(file_contents_response);
1213
1214 file_context = cliprdr_context->custom;
1215 WINPR_ASSERT(file_context);
1216
1217 HashTable_Lock(file_context->inode_table);
1218 fuse_request = HashTable_GetItemValue(file_context->request_table,
1219 (void*)(uintptr_t)file_contents_response->streamId);
1220 if (!fuse_request)
1221 {
1222 HashTable_Unlock(file_context->inode_table);
1223 return CHANNEL_RC_OK;
1224 }
1225
1226 if (!(file_contents_response->common.msgFlags & CB_RESPONSE_OK))
1227 {
1228 WLog_Print(file_context->log, WLOG_WARN,
1229 "FileContentsRequests for file \"%s\" was unsuccessful",
1230 fuse_request->fuse_file->filename);
1231
1232 fuse_reply_err(fuse_request->fuse_req, EIO);
1233 HashTable_Remove(file_context->request_table,
1234 (void*)(uintptr_t)file_contents_response->streamId);
1235 HashTable_Unlock(file_context->inode_table);
1236 return CHANNEL_RC_OK;
1237 }
1238
1239 if ((fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1240 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR) &&
1241 file_contents_response->cbRequested != sizeof(UINT64))
1242 {
1243 WLog_Print(file_context->log, WLOG_WARN,
1244 "Received invalid file size for file \"%s\" from the client",
1245 fuse_request->fuse_file->filename);
1246 fuse_reply_err(fuse_request->fuse_req, EIO);
1247 HashTable_Remove(file_context->request_table,
1248 (void*)(uintptr_t)file_contents_response->streamId);
1249 HashTable_Unlock(file_context->inode_table);
1250 return CHANNEL_RC_OK;
1251 }
1252
1253 if (fuse_request->operation_type == FUSE_LL_OPERATION_LOOKUP ||
1254 fuse_request->operation_type == FUSE_LL_OPERATION_GETATTR)
1255 {
1256 DEBUG_CLIPRDR(file_context->log, "Received file size for file \"%s\" with stream id %u",
1257 fuse_request->fuse_file->filename, file_contents_response->streamId);
1258
1259 fuse_request->fuse_file->size = *((const UINT64*)file_contents_response->requestedData);
1260 fuse_request->fuse_file->has_size = TRUE;
1261
1262 entry.ino = fuse_request->fuse_file->ino;
1263 write_file_attributes(fuse_request->fuse_file, &entry.attr);
1264 entry.attr_timeout = 1.0;
1265 entry.entry_timeout = 1.0;
1266 }
1267 else if (fuse_request->operation_type == FUSE_LL_OPERATION_READ)
1268 {
1269 DEBUG_CLIPRDR(file_context->log, "Received file range for file \"%s\" with stream id %u",
1270 fuse_request->fuse_file->filename, file_contents_response->streamId);
1271 }
1272 HashTable_Unlock(file_context->inode_table);
1273
1274 switch (fuse_request->operation_type)
1275 {
1276 case FUSE_LL_OPERATION_NONE:
1277 break;
1278 case FUSE_LL_OPERATION_LOOKUP:
1279 fuse_reply_entry(fuse_request->fuse_req, &entry);
1280 break;
1281 case FUSE_LL_OPERATION_GETATTR:
1282 fuse_reply_attr(fuse_request->fuse_req, &entry.attr, entry.attr_timeout);
1283 break;
1284 case FUSE_LL_OPERATION_READ:
1285 fuse_reply_buf(fuse_request->fuse_req,
1286 (const char*)file_contents_response->requestedData,
1287 file_contents_response->cbRequested);
1288 break;
1289 default:
1290 break;
1291 }
1292
1293 HashTable_Remove(file_context->request_table,
1294 (void*)(uintptr_t)file_contents_response->streamId);
1295
1296 return CHANNEL_RC_OK;
1297}
1298#endif
1299
1300static UINT cliprdr_file_context_send_file_contents_failure(
1301 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1302{
1303 CLIPRDR_FILE_CONTENTS_RESPONSE response = { 0 };
1304
1305 WINPR_ASSERT(file);
1306 WINPR_ASSERT(fileContentsRequest);
1307
1308 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1309 ((UINT64)fileContentsRequest->nPositionLow);
1310 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1311 "server file contents request [lockID %" PRIu32 ", streamID %" PRIu32
1312 ", index %" PRIu32 "] offset %" PRIu64 ", size %" PRIu32 " failed",
1313 fileContentsRequest->clipDataId, fileContentsRequest->streamId,
1314 fileContentsRequest->listIndex, offset, fileContentsRequest->cbRequested);
1315
1316 response.common.msgFlags = CB_RESPONSE_FAIL;
1317 response.streamId = fileContentsRequest->streamId;
1318
1319 WINPR_ASSERT(file->context);
1320 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1321 return file->context->ClientFileContentsResponse(file->context, &response);
1322}
1323
1324static UINT
1325cliprdr_file_context_send_contents_response(CliprdrFileContext* file,
1326 const CLIPRDR_FILE_CONTENTS_REQUEST* request,
1327 const void* data, size_t size)
1328{
1329 if (size > UINT32_MAX)
1330 return ERROR_INVALID_PARAMETER;
1331
1332 CLIPRDR_FILE_CONTENTS_RESPONSE response = { .streamId = request->streamId,
1333 .requestedData = data,
1334 .cbRequested = (UINT32)size,
1335 .common.msgFlags = CB_RESPONSE_OK };
1336
1337 WINPR_ASSERT(request);
1338 WINPR_ASSERT(file);
1339
1340 WLog_Print(file->log, WLOG_DEBUG, "send contents response streamID=%" PRIu32 ", size=%" PRIu32,
1341 response.streamId, response.cbRequested);
1342 WINPR_ASSERT(file->context);
1343 WINPR_ASSERT(file->context->ClientFileContentsResponse);
1344 return file->context->ClientFileContentsResponse(file->context, &response);
1345}
1346
1347static BOOL dump_streams(const void* key, void* value, WINPR_ATTR_UNUSED void* arg)
1348{
1349 const UINT32* ukey = key;
1350 CliprdrLocalStream* cur = value;
1351
1352 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1353 "[key %" PRIu32 "] lockID %" PRIu32 ", count %" PRIuz ", locked %d", *ukey,
1354 cur->lockId, cur->count, cur->locked);
1355 for (size_t x = 0; x < cur->count; x++)
1356 {
1357 const CliprdrLocalFile* file = &cur->files[x];
1358 writelog(cur->context->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1359 "file [%" PRIuz "] %s: %" PRId64, x, file->name, file->size);
1360 }
1361 return TRUE;
1362}
1363
1364static CliprdrLocalFile* file_info_for_request(CliprdrFileContext* file, UINT32 lockId,
1365 UINT32 listIndex)
1366{
1367 WINPR_ASSERT(file);
1368
1369 CliprdrLocalStream* cur = HashTable_GetItemValue(file->local_streams, &lockId);
1370 if (cur)
1371 {
1372 if (listIndex < cur->count)
1373 {
1374 CliprdrLocalFile* f = &cur->files[listIndex];
1375 return f;
1376 }
1377 else
1378 {
1379 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1380 "invalid entry index for lockID %" PRIu32 ", index %" PRIu32 " [count %" PRIuz
1381 "] [locked %d]",
1382 lockId, listIndex, cur->count, cur->locked);
1383 }
1384 }
1385 else
1386 {
1387 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1388 "missing entry for lockID %" PRIu32 ", index %" PRIu32, lockId, listIndex);
1389 HashTable_Foreach(file->local_streams, dump_streams, file);
1390 }
1391
1392 return NULL;
1393}
1394
1395static CliprdrLocalFile* file_for_request(CliprdrFileContext* file, UINT32 lockId, UINT32 listIndex)
1396{
1397 CliprdrLocalFile* f = file_info_for_request(file, lockId, listIndex);
1398 if (f)
1399 {
1400 if (!f->fp)
1401 {
1402 const char* name = f->name;
1403 f->fp = winpr_fopen(name, "rb");
1404 }
1405 if (!f->fp)
1406 {
1407 char ebuffer[256] = { 0 };
1408 writelog(file->log, WLOG_WARN, __FILE__, __func__, __LINE__,
1409 "[lockID %" PRIu32 ", index %" PRIu32
1410 "] failed to open file '%s' [size %" PRId64 "] %s [%d]",
1411 lockId, listIndex, f->name, f->size,
1412 winpr_strerror(errno, ebuffer, sizeof(ebuffer)), errno);
1413 return NULL;
1414 }
1415 }
1416
1417 return f;
1418}
1419
1420static void cliprdr_local_file_try_close(CliprdrLocalFile* file, UINT res, UINT64 offset,
1421 UINT64 size)
1422{
1423 WINPR_ASSERT(file);
1424
1425 if (res != 0)
1426 {
1427 WINPR_ASSERT(file->context);
1428 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after error %" PRIu32,
1429 file->name, res);
1430 }
1431 else if (((file->size > 0) && (offset + size >= (UINT64)file->size)))
1432 {
1433 WINPR_ASSERT(file->context);
1434 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s after read", file->name);
1435 }
1436 else
1437 {
1438 // TODO: we need to keep track of open files to avoid running out of file descriptors
1439 // TODO: for the time being just close again.
1440 }
1441 if (file->fp)
1442 (void)fclose(file->fp);
1443 file->fp = NULL;
1444}
1445
1446static UINT cliprdr_file_context_server_file_size_request(
1447 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1448{
1449 WINPR_ASSERT(fileContentsRequest);
1450
1451 if (fileContentsRequest->cbRequested != sizeof(UINT64))
1452 {
1453 WLog_Print(file->log, WLOG_WARN, "unexpected FILECONTENTS_SIZE request: %" PRIu32 " bytes",
1454 fileContentsRequest->cbRequested);
1455 }
1456
1457 HashTable_Lock(file->local_streams);
1458
1459 UINT res = CHANNEL_RC_OK;
1460 CliprdrLocalFile* rfile =
1461 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1462 if (!rfile)
1463 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1464 else
1465 {
1466 if (_fseeki64(rfile->fp, 0, SEEK_END) < 0)
1467 res = cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1468 else
1469 {
1470 const INT64 size = _ftelli64(rfile->fp);
1471 rfile->size = size;
1472 cliprdr_local_file_try_close(rfile, res, 0, 0);
1473
1474 res = cliprdr_file_context_send_contents_response(file, fileContentsRequest, &size,
1475 sizeof(size));
1476 }
1477 }
1478
1479 HashTable_Unlock(file->local_streams);
1480 return res;
1481}
1482
1483static UINT cliprdr_file_context_server_file_range_request(
1484 CliprdrFileContext* file, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1485{
1486 BYTE* data = NULL;
1487
1488 WINPR_ASSERT(fileContentsRequest);
1489
1490 HashTable_Lock(file->local_streams);
1491 const UINT64 offset = (((UINT64)fileContentsRequest->nPositionHigh) << 32) |
1492 ((UINT64)fileContentsRequest->nPositionLow);
1493
1494 CliprdrLocalFile* rfile =
1495 file_for_request(file, fileContentsRequest->clipDataId, fileContentsRequest->listIndex);
1496 if (!rfile)
1497 goto fail;
1498
1499 if (_fseeki64(rfile->fp, WINPR_ASSERTING_INT_CAST(int64_t, offset), SEEK_SET) < 0)
1500 goto fail;
1501
1502 data = malloc(fileContentsRequest->cbRequested);
1503 if (!data)
1504 goto fail;
1505
1506 {
1507 const size_t r = fread(data, 1, fileContentsRequest->cbRequested, rfile->fp);
1508 const UINT rc =
1509 cliprdr_file_context_send_contents_response(file, fileContentsRequest, data, r);
1510 free(data);
1511
1512 cliprdr_local_file_try_close(rfile, rc, offset, fileContentsRequest->cbRequested);
1513 HashTable_Unlock(file->local_streams);
1514 return rc;
1515 }
1516fail:
1517 if (rfile)
1518 cliprdr_local_file_try_close(rfile, ERROR_INTERNAL_ERROR, offset,
1519 fileContentsRequest->cbRequested);
1520 free(data);
1521 HashTable_Unlock(file->local_streams);
1522 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1523}
1524
1525static void cliprdr_local_stream_free(void* obj);
1526
1527static UINT change_lock(CliprdrFileContext* file, UINT32 lockId, BOOL lock)
1528{
1529 UINT rc = CHANNEL_RC_OK;
1530
1531 WINPR_ASSERT(file);
1532
1533 HashTable_Lock(file->local_streams);
1534
1535 {
1536 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
1537 if (lock && !stream)
1538 {
1539 stream = cliprdr_local_stream_new(file, lockId, NULL, 0);
1540 if (!HashTable_Insert(file->local_streams, &lockId, stream))
1541 {
1542 rc = ERROR_INTERNAL_ERROR;
1543 cliprdr_local_stream_free(stream);
1544 stream = NULL;
1545 }
1546 file->local_lock_id = lockId;
1547 }
1548 if (stream)
1549 {
1550 stream->locked = lock;
1551 stream->lockId = lockId;
1552 }
1553 }
1554
1555 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc): HashTable_Insert ownership stream
1556 if (!lock)
1557 {
1558 if (!HashTable_Foreach(file->local_streams, local_stream_discard, file))
1559 rc = ERROR_INTERNAL_ERROR;
1560 }
1561
1562 HashTable_Unlock(file->local_streams);
1563 return rc;
1564}
1565
1566static UINT cliprdr_file_context_lock(CliprdrClientContext* context,
1567 const CLIPRDR_LOCK_CLIPBOARD_DATA* lockClipboardData)
1568{
1569 WINPR_ASSERT(context);
1570 WINPR_ASSERT(lockClipboardData);
1571 CliprdrFileContext* file = (context->custom);
1572 return change_lock(file, lockClipboardData->clipDataId, TRUE);
1573}
1574
1575static UINT cliprdr_file_context_unlock(CliprdrClientContext* context,
1576 const CLIPRDR_UNLOCK_CLIPBOARD_DATA* unlockClipboardData)
1577{
1578 WINPR_ASSERT(context);
1579 WINPR_ASSERT(unlockClipboardData);
1580 CliprdrFileContext* file = (context->custom);
1581 return change_lock(file, unlockClipboardData->clipDataId, FALSE);
1582}
1583
1584static UINT cliprdr_file_context_server_file_contents_request(
1585 CliprdrClientContext* context, const CLIPRDR_FILE_CONTENTS_REQUEST* fileContentsRequest)
1586{
1587 UINT error = NO_ERROR;
1588
1589 WINPR_ASSERT(context);
1590 WINPR_ASSERT(fileContentsRequest);
1591
1592 CliprdrFileContext* file = (context->custom);
1593 WINPR_ASSERT(file);
1594
1595 /*
1596 * MS-RDPECLIP 2.2.5.3 File Contents Request PDU (CLIPRDR_FILECONTENTS_REQUEST):
1597 * The FILECONTENTS_SIZE and FILECONTENTS_RANGE flags MUST NOT be set at the same time.
1598 */
1599 if ((fileContentsRequest->dwFlags & (FILECONTENTS_SIZE | FILECONTENTS_RANGE)) ==
1600 (FILECONTENTS_SIZE | FILECONTENTS_RANGE))
1601 {
1602 WLog_Print(file->log, WLOG_ERROR, "invalid CLIPRDR_FILECONTENTS_REQUEST.dwFlags");
1603 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1604 }
1605
1606 if (fileContentsRequest->dwFlags & FILECONTENTS_SIZE)
1607 error = cliprdr_file_context_server_file_size_request(file, fileContentsRequest);
1608
1609 if (fileContentsRequest->dwFlags & FILECONTENTS_RANGE)
1610 error = cliprdr_file_context_server_file_range_request(file, fileContentsRequest);
1611
1612 if (error)
1613 {
1614 WLog_Print(file->log, WLOG_ERROR, "failed to handle CLIPRDR_FILECONTENTS_REQUEST: 0x%08X",
1615 error);
1616 return cliprdr_file_context_send_file_contents_failure(file, fileContentsRequest);
1617 }
1618
1619 return CHANNEL_RC_OK;
1620}
1621
1622BOOL cliprdr_file_context_init(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1623{
1624 WINPR_ASSERT(file);
1625 WINPR_ASSERT(cliprdr);
1626
1627 cliprdr->custom = file;
1628 file->context = cliprdr;
1629
1630 cliprdr->ServerLockClipboardData = cliprdr_file_context_lock;
1631 cliprdr->ServerUnlockClipboardData = cliprdr_file_context_unlock;
1632 cliprdr->ServerFileContentsRequest = cliprdr_file_context_server_file_contents_request;
1633#if defined(WITH_FUSE)
1634 cliprdr->ServerFileContentsResponse = cliprdr_file_context_server_file_contents_response;
1635
1636 CliprdrFuseFile* root_dir = fuse_file_new_root(file);
1637 if (!root_dir)
1638 return FALSE;
1639#endif
1640
1641 return TRUE;
1642}
1643
1644#if defined(WITH_FUSE)
1645static void clear_all_selections(CliprdrFileContext* file_context)
1646{
1647 WINPR_ASSERT(file_context);
1648 WINPR_ASSERT(file_context->inode_table);
1649
1650 HashTable_Lock(file_context->inode_table);
1651 clear_selection(file_context, TRUE, NULL);
1652
1653 HashTable_Clear(file_context->clip_data_table);
1654 HashTable_Unlock(file_context->inode_table);
1655}
1656#endif
1657
1658BOOL cliprdr_file_context_uninit(CliprdrFileContext* file, CliprdrClientContext* cliprdr)
1659{
1660 WINPR_ASSERT(file);
1661 WINPR_ASSERT(cliprdr);
1662
1663 // Clear all data before the channel is closed
1664 // the cleanup handlers are dependent on a working channel.
1665#if defined(WITH_FUSE)
1666 if (file->inode_table)
1667 {
1668 clear_no_cdi_entry(file);
1669 clear_all_selections(file);
1670 }
1671#endif
1672
1673 HashTable_Clear(file->local_streams);
1674
1675 file->context = NULL;
1676
1677#if defined(WITH_FUSE)
1678 cliprdr->ServerFileContentsResponse = NULL;
1679#endif
1680
1681 return TRUE;
1682}
1683
1684static BOOL cliprdr_file_content_changed_and_update(void* ihash, size_t hsize, const void* data,
1685 size_t size)
1686{
1687
1688 BYTE hash[WINPR_SHA256_DIGEST_LENGTH] = { 0 };
1689
1690 if (hsize < sizeof(hash))
1691 return FALSE;
1692
1693 if (!winpr_Digest(WINPR_MD_SHA256, data, size, hash, sizeof(hash)))
1694 return FALSE;
1695
1696 const BOOL changed = memcmp(hash, ihash, sizeof(hash)) != 0;
1697 if (changed)
1698 memcpy(ihash, hash, sizeof(hash));
1699 return changed;
1700}
1701
1702static BOOL cliprdr_file_client_content_changed_and_update(CliprdrFileContext* file,
1703 const void* data, size_t size)
1704{
1705 WINPR_ASSERT(file);
1706 return cliprdr_file_content_changed_and_update(file->client_data_hash,
1707 sizeof(file->client_data_hash), data, size);
1708}
1709
1710#if defined(WITH_FUSE)
1711static fuse_ino_t get_next_free_inode(CliprdrFileContext* file_context)
1712{
1713 fuse_ino_t ino = 0;
1714
1715 WINPR_ASSERT(file_context);
1716
1717 ino = file_context->next_ino;
1718 while (ino == 0 || ino == FUSE_ROOT_ID ||
1719 HashTable_GetItemValue(file_context->inode_table, (void*)(uintptr_t)ino))
1720 ++ino;
1721
1722 file_context->next_ino = ino + 1;
1723
1724 return ino;
1725}
1726
1727static CliprdrFuseFile* clip_data_dir_new(CliprdrFileContext* file_context, BOOL has_clip_data_id,
1728 UINT32 clip_data_id)
1729{
1730 WINPR_ASSERT(file_context);
1731
1732 UINT64 data_id = clip_data_id;
1733 if (!has_clip_data_id)
1734 data_id = NO_CLIP_DATA_ID;
1735
1736 CliprdrFuseFile* clip_data_dir = fuse_file_new("/%" PRIu64, data_id);
1737 if (!clip_data_dir)
1738 return NULL;
1739
1740 clip_data_dir->ino = get_next_free_inode(file_context);
1741 clip_data_dir->is_directory = TRUE;
1742 clip_data_dir->is_readonly = TRUE;
1743 clip_data_dir->has_clip_data_id = has_clip_data_id;
1744 clip_data_dir->clip_data_id = clip_data_id;
1745
1746 CliprdrFuseFile* root_dir = get_fuse_file_by_ino(file_context, FUSE_ROOT_ID);
1747 if (!root_dir)
1748 {
1749 WLog_Print(file_context->log, WLOG_ERROR, "FUSE root directory missing");
1750 fuse_file_free(clip_data_dir);
1751 return NULL;
1752 }
1753 if (!ArrayList_Append(root_dir->children, clip_data_dir))
1754 {
1755 WLog_Print(file_context->log, WLOG_ERROR, "Failed to append FUSE file");
1756 fuse_file_free(clip_data_dir);
1757 return NULL;
1758 }
1759 clip_data_dir->parent = root_dir;
1760
1761 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)clip_data_dir->ino,
1762 clip_data_dir))
1763 {
1764 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into inode table");
1765 ArrayList_Remove(root_dir->children, clip_data_dir);
1766 fuse_file_free(clip_data_dir);
1767 return NULL;
1768 }
1769
1770 return clip_data_dir;
1771}
1772
1773static char* get_parent_path(const char* filepath)
1774{
1775 const char* base = strrchr(filepath, '/');
1776 WINPR_ASSERT(base);
1777
1778 while ((base > filepath) && (*base == '/'))
1779 --base;
1780
1781 WINPR_ASSERT(base >= filepath);
1782 const size_t parent_path_length = 1ULL + (size_t)(base - filepath);
1783 char* parent_path = calloc(parent_path_length + 1, sizeof(char));
1784 if (!parent_path)
1785 return NULL;
1786
1787 memcpy(parent_path, filepath, parent_path_length);
1788
1789 return parent_path;
1790}
1791
1792static BOOL is_fuse_file_not_parent(WINPR_ATTR_UNUSED const void* key, void* value, void* arg)
1793{
1794 CliprdrFuseFile* fuse_file = value;
1795 CliprdrFuseFindParentContext* find_context = arg;
1796
1797 if (!fuse_file->is_directory)
1798 return TRUE;
1799
1800 if (strncmp(find_context->parent_path, fuse_file->filename_with_root,
1801 fuse_file->filename_with_root_len + 1) == 0)
1802 {
1803 find_context->parent = fuse_file;
1804 return FALSE;
1805 }
1806
1807 return TRUE;
1808}
1809
1810static CliprdrFuseFile* get_parent_directory(CliprdrFileContext* file_context, const char* path)
1811{
1812 CliprdrFuseFindParentContext find_context = { 0 };
1813
1814 WINPR_ASSERT(file_context);
1815 WINPR_ASSERT(path);
1816
1817 find_context.parent_path = get_parent_path(path);
1818 if (!find_context.parent_path)
1819 return NULL;
1820
1821 WINPR_ASSERT(!find_context.parent);
1822
1823 if (HashTable_Foreach(file_context->inode_table, is_fuse_file_not_parent, &find_context))
1824 {
1825 free(find_context.parent_path);
1826 return NULL;
1827 }
1828 WINPR_ASSERT(find_context.parent);
1829
1830 free(find_context.parent_path);
1831
1832 return find_context.parent;
1833}
1834
1835// NOLINTBEGIN(clang-analyzer-unix.Malloc) HashTable_Insert owns fuse_file
1836static BOOL selection_handle_file(CliprdrFileContext* file_context,
1837 CliprdrFuseClipDataEntry* clip_data_entry, uint32_t index,
1838 const FILEDESCRIPTORW* file)
1839{
1840
1841 WINPR_ASSERT(file_context);
1842 WINPR_ASSERT(clip_data_entry);
1843 WINPR_ASSERT(file);
1844
1845 CliprdrFuseFile* clip_data_dir = clip_data_entry->clip_data_dir;
1846 WINPR_ASSERT(clip_data_dir);
1847
1848 char filename[ARRAYSIZE(file->cFileName) * 8] = { 0 };
1849 const SSIZE_T filenamelen = ConvertWCharNToUtf8(file->cFileName, ARRAYSIZE(file->cFileName),
1850 filename, ARRAYSIZE(filename) - 1);
1851 if (filenamelen < 0)
1852 {
1853 WLog_Print(file_context->log, WLOG_ERROR, "Failed to convert filename");
1854 return FALSE;
1855 }
1856
1857 for (size_t j = 0; filename[j]; ++j)
1858 {
1859 if (filename[j] == '\\')
1860 filename[j] = '/';
1861 }
1862
1863 BOOL crc = FALSE;
1864 CliprdrFuseFile* fuse_file = fuse_file_new(
1865 "%.*s/%s", WINPR_ASSERTING_INT_CAST(int, clip_data_dir->filename_with_root_len),
1866 clip_data_dir->filename_with_root, filename);
1867 if (!fuse_file)
1868 {
1869 WLog_Print(file_context->log, WLOG_ERROR, "Failed to create FUSE file");
1870 goto end;
1871 }
1872
1873 fuse_file->parent = get_parent_directory(file_context, fuse_file->filename_with_root);
1874 if (!fuse_file->parent)
1875 {
1876 WLog_Print(file_context->log, WLOG_ERROR, "Found no parent for FUSE file");
1877 goto end;
1878 }
1879
1880 if (!ArrayList_Append(fuse_file->parent->children, fuse_file))
1881 {
1882 WLog_Print(file_context->log, WLOG_ERROR, "Failed to append FUSE file");
1883 goto end;
1884 }
1885
1886 fuse_file->list_idx = index;
1887 fuse_file->ino = get_next_free_inode(file_context);
1888 fuse_file->has_clip_data_id = clip_data_entry->has_clip_data_id;
1889 fuse_file->clip_data_id = clip_data_entry->clip_data_id;
1890 if (file->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
1891 fuse_file->is_directory = TRUE;
1892 if (file->dwFileAttributes & FILE_ATTRIBUTE_READONLY)
1893 fuse_file->is_readonly = TRUE;
1894 if (file->dwFlags & FD_FILESIZE)
1895 {
1896 fuse_file->size = ((UINT64)file->nFileSizeHigh << 32) + file->nFileSizeLow;
1897 fuse_file->has_size = TRUE;
1898 }
1899 if (file->dwFlags & FD_WRITESTIME)
1900 {
1901 INT64 filetime = 0;
1902
1903 filetime = file->ftLastWriteTime.dwHighDateTime;
1904 filetime <<= 32;
1905 filetime += file->ftLastWriteTime.dwLowDateTime;
1906
1907 fuse_file->last_write_time_unix =
1908 1LL * filetime / (10LL * 1000LL * 1000LL) - WIN32_FILETIME_TO_UNIX_EPOCH;
1909 fuse_file->has_last_write_time = TRUE;
1910 }
1911
1912 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)fuse_file->ino, fuse_file))
1913 {
1914 WLog_Print(file_context->log, WLOG_ERROR, "Failed to insert inode into inode table");
1915 goto end;
1916 }
1917
1918 crc = TRUE;
1919
1920end:
1921 if (!crc)
1922 {
1923 fuse_file_free(fuse_file);
1924 clear_entry_selection(clip_data_entry);
1925 return FALSE;
1926 }
1927 return TRUE;
1928}
1929// NOLINTEND(clang-analyzer-unix.Malloc) HashTable_Insert owns fuse_file
1930
1931static BOOL set_selection_for_clip_data_entry(CliprdrFileContext* file_context,
1932 CliprdrFuseClipDataEntry* clip_data_entry,
1933 const FILEDESCRIPTORW* files, UINT32 n_files)
1934{
1935 WINPR_ASSERT(file_context);
1936 WINPR_ASSERT(clip_data_entry);
1937 WINPR_ASSERT(files);
1938
1939 if (clip_data_entry->has_clip_data_id)
1940 WLog_Print(file_context->log, WLOG_DEBUG, "Setting selection for clipDataId %u",
1941 clip_data_entry->clip_data_id);
1942 else
1943 WLog_Print(file_context->log, WLOG_DEBUG, "Setting selection");
1944
1945 for (UINT32 i = 0; i < n_files; ++i)
1946 {
1947 const FILEDESCRIPTORW* file = &files[i];
1948 if (!selection_handle_file(file_context, clip_data_entry, i, file))
1949 return FALSE;
1950 }
1951
1952 if (clip_data_entry->has_clip_data_id)
1953 WLog_Print(file_context->log, WLOG_DEBUG, "Selection set for clipDataId %u",
1954 clip_data_entry->clip_data_id);
1955 else
1956 WLog_Print(file_context->log, WLOG_DEBUG, "Selection set");
1957
1958 return TRUE;
1959}
1960
1961static BOOL update_exposed_path(CliprdrFileContext* file_context, wClipboard* clip,
1962 CliprdrFuseClipDataEntry* clip_data_entry)
1963{
1964 wClipboardDelegate* delegate = NULL;
1965 CliprdrFuseFile* clip_data_dir = NULL;
1966
1967 WINPR_ASSERT(file_context);
1968 WINPR_ASSERT(clip);
1969 WINPR_ASSERT(clip_data_entry);
1970
1971 delegate = ClipboardGetDelegate(clip);
1972 WINPR_ASSERT(delegate);
1973
1974 clip_data_dir = clip_data_entry->clip_data_dir;
1975 WINPR_ASSERT(clip_data_dir);
1976
1977 free(file_context->exposed_path);
1978 file_context->exposed_path = GetCombinedPath(file_context->path, clip_data_dir->filename);
1979 if (file_context->exposed_path)
1980 WLog_Print(file_context->log, WLOG_DEBUG, "Updated exposed path to \"%s\"",
1981 file_context->exposed_path);
1982
1983 delegate->basePath = file_context->exposed_path;
1984
1985 return delegate->basePath != NULL;
1986}
1987#endif
1988
1989BOOL cliprdr_file_context_update_server_data(CliprdrFileContext* file_context, wClipboard* clip,
1990 const void* data, size_t size)
1991{
1992#if defined(WITH_FUSE)
1993 CliprdrFuseClipDataEntry* clip_data_entry = NULL;
1994 FILEDESCRIPTORW* files = NULL;
1995 UINT32 n_files = 0;
1996 BOOL rc = FALSE;
1997
1998 WINPR_ASSERT(file_context);
1999 WINPR_ASSERT(clip);
2000 if (size > UINT32_MAX)
2001 return FALSE;
2002
2003 if (cliprdr_parse_file_list(data, (UINT32)size, &files, &n_files))
2004 {
2005 WLog_Print(file_context->log, WLOG_ERROR, "Failed to parse file list");
2006 return FALSE;
2007 }
2008
2009 HashTable_Lock(file_context->inode_table);
2010 HashTable_Lock(file_context->clip_data_table);
2011 if (does_server_support_clipdata_locking(file_context))
2012 clip_data_entry = HashTable_GetItemValue(
2013 file_context->clip_data_table, (void*)(uintptr_t)file_context->current_clip_data_id);
2014 else
2015 clip_data_entry = file_context->clip_data_entry_without_id;
2016
2017 WINPR_ASSERT(clip_data_entry);
2018
2019 clear_entry_selection(clip_data_entry);
2020 WINPR_ASSERT(!clip_data_entry->clip_data_dir);
2021
2022 clip_data_entry->clip_data_dir =
2023 clip_data_dir_new(file_context, does_server_support_clipdata_locking(file_context),
2024 file_context->current_clip_data_id);
2025 if (!clip_data_entry->clip_data_dir)
2026 goto fail;
2027 if (!update_exposed_path(file_context, clip, clip_data_entry))
2028 goto fail;
2029 if (!set_selection_for_clip_data_entry(file_context, clip_data_entry, files, n_files))
2030 goto fail;
2031
2032 rc = TRUE;
2033
2034fail:
2035 HashTable_Unlock(file_context->clip_data_table);
2036 HashTable_Unlock(file_context->inode_table);
2037 free(files);
2038 return rc;
2039#else
2040 return FALSE;
2041#endif
2042}
2043
2044void* cliprdr_file_context_get_context(CliprdrFileContext* file)
2045{
2046 WINPR_ASSERT(file);
2047 return file->clipboard;
2048}
2049
2050void cliprdr_file_session_terminate(CliprdrFileContext* file, BOOL stop_thread)
2051{
2052 if (!file)
2053 return;
2054
2055#if defined(WITH_FUSE)
2056 WINPR_ASSERT(file->fuse_stop_sync);
2057
2058 WLog_Print(file->log, WLOG_DEBUG, "Setting FUSE exit flag");
2059 if (file->fuse_sess)
2060 fuse_session_exit(file->fuse_sess);
2061
2062 if (stop_thread)
2063 {
2064 WLog_Print(file->log, WLOG_DEBUG, "Setting FUSE stop event");
2065 (void)SetEvent(file->fuse_stop_sync);
2066 }
2067#endif
2068 /* not elegant but works for umounting FUSE
2069 fuse_chan must receive an oper buf to unblock fuse_session_receive_buf function.
2070 */
2071#if defined(WITH_FUSE)
2072 WLog_Print(file->log, WLOG_DEBUG, "Forcing FUSE to check exit flag");
2073#endif
2074 (void)winpr_PathFileExists(file->path);
2075}
2076
2077void cliprdr_file_context_free(CliprdrFileContext* file)
2078{
2079 if (!file)
2080 return;
2081
2082#if defined(WITH_FUSE)
2083 if (file->fuse_thread)
2084 {
2085 WINPR_ASSERT(file->fuse_stop_sync);
2086
2087 WLog_Print(file->log, WLOG_DEBUG, "Stopping FUSE thread");
2088 cliprdr_file_session_terminate(file, TRUE);
2089
2090 WLog_Print(file->log, WLOG_DEBUG, "Waiting on FUSE thread");
2091 (void)WaitForSingleObject(file->fuse_thread, INFINITE);
2092 (void)CloseHandle(file->fuse_thread);
2093 }
2094 if (file->fuse_stop_sync)
2095 (void)CloseHandle(file->fuse_stop_sync);
2096 if (file->fuse_start_sync)
2097 (void)CloseHandle(file->fuse_start_sync);
2098
2099 HashTable_Free(file->request_table);
2100 HashTable_Free(file->clip_data_table);
2101 HashTable_Free(file->inode_table);
2102
2103#endif
2104 HashTable_Free(file->local_streams);
2105 winpr_RemoveDirectory(file->path);
2106 free(file->path);
2107 free(file->exposed_path);
2108 free(file);
2109}
2110
2111static BOOL create_base_path(CliprdrFileContext* file)
2112{
2113 WINPR_ASSERT(file);
2114
2115 char base[64] = { 0 };
2116 (void)_snprintf(base, sizeof(base), "com.freerdp.client.cliprdr.%" PRIu32,
2117 GetCurrentProcessId());
2118
2119 file->path = GetKnownSubPath(KNOWN_PATH_TEMP, base);
2120 if (!file->path)
2121 return FALSE;
2122
2123 if (!winpr_PathFileExists(file->path) && !winpr_PathMakePath(file->path, 0))
2124 {
2125 WLog_Print(file->log, WLOG_ERROR, "Failed to create directory '%s'", file->path);
2126 return FALSE;
2127 }
2128 return TRUE;
2129}
2130
2131static void cliprdr_local_file_free(CliprdrLocalFile* file)
2132{
2133 const CliprdrLocalFile empty = { 0 };
2134 if (!file)
2135 return;
2136 if (file->fp)
2137 {
2138 WLog_Print(file->context->log, WLOG_DEBUG, "closing file %s, discarding entry", file->name);
2139 (void)fclose(file->fp);
2140 }
2141 free(file->name);
2142 *file = empty;
2143}
2144
2145static BOOL cliprdr_local_file_new(CliprdrFileContext* context, CliprdrLocalFile* f,
2146 const char* path)
2147{
2148 const CliprdrLocalFile empty = { 0 };
2149 WINPR_ASSERT(f);
2150 WINPR_ASSERT(context);
2151 WINPR_ASSERT(path);
2152
2153 *f = empty;
2154 f->context = context;
2155 f->name = winpr_str_url_decode(path, strlen(path));
2156 if (!f->name)
2157 goto fail;
2158
2159 return TRUE;
2160fail:
2161 cliprdr_local_file_free(f);
2162 return FALSE;
2163}
2164
2165static void cliprdr_local_files_free(CliprdrLocalStream* stream)
2166{
2167 WINPR_ASSERT(stream);
2168
2169 for (size_t x = 0; x < stream->count; x++)
2170 cliprdr_local_file_free(&stream->files[x]);
2171 free(stream->files);
2172
2173 stream->files = NULL;
2174 stream->count = 0;
2175}
2176
2177static void cliprdr_local_stream_free(void* obj)
2178{
2179 CliprdrLocalStream* stream = (CliprdrLocalStream*)obj;
2180 if (stream)
2181 cliprdr_local_files_free(stream);
2182
2183 free(stream);
2184}
2185
2186static BOOL append_entry(CliprdrLocalStream* stream, const char* path)
2187{
2188 CliprdrLocalFile* tmp = realloc(stream->files, sizeof(CliprdrLocalFile) * (stream->count + 1));
2189 if (!tmp)
2190 return FALSE;
2191 stream->files = tmp;
2192 CliprdrLocalFile* f = &stream->files[stream->count++];
2193
2194 return cliprdr_local_file_new(stream->context, f, path);
2195}
2196
2197static BOOL is_directory(const char* path)
2198{
2199 WCHAR* wpath = ConvertUtf8ToWCharAlloc(path, NULL);
2200 if (!wpath)
2201 return FALSE;
2202
2203 HANDLE hFile =
2204 CreateFileW(wpath, 0, FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
2205 free(wpath);
2206
2207 if (hFile == INVALID_HANDLE_VALUE)
2208 return FALSE;
2209
2210 BY_HANDLE_FILE_INFORMATION fileInformation = { 0 };
2211 const BOOL status = GetFileInformationByHandle(hFile, &fileInformation);
2212 (void)CloseHandle(hFile);
2213 if (!status)
2214 return FALSE;
2215
2216 return (fileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? TRUE : FALSE;
2217}
2218
2219static BOOL add_directory(CliprdrLocalStream* stream, const char* path)
2220{
2221 char* wildcardpath = GetCombinedPath(path, "*");
2222 if (!wildcardpath)
2223 return FALSE;
2224 WCHAR* wpath = ConvertUtf8ToWCharAlloc(wildcardpath, NULL);
2225 free(wildcardpath);
2226 if (!wpath)
2227 return FALSE;
2228
2229 WIN32_FIND_DATAW FindFileData = { 0 };
2230 HANDLE hFind = FindFirstFileW(wpath, &FindFileData);
2231 free(wpath);
2232
2233 if (hFind == INVALID_HANDLE_VALUE)
2234 return FALSE;
2235
2236 BOOL rc = FALSE;
2237 char* next = NULL;
2238
2239 WCHAR dotbuffer[6] = { 0 };
2240 WCHAR dotdotbuffer[6] = { 0 };
2241 const WCHAR* dot = InitializeConstWCharFromUtf8(".", dotbuffer, ARRAYSIZE(dotbuffer));
2242 const WCHAR* dotdot = InitializeConstWCharFromUtf8("..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
2243 do
2244 {
2245 if (_wcscmp(FindFileData.cFileName, dot) == 0)
2246 continue;
2247 if (_wcscmp(FindFileData.cFileName, dotdot) == 0)
2248 continue;
2249
2250 char cFileName[MAX_PATH] = { 0 };
2251 (void)ConvertWCharNToUtf8(FindFileData.cFileName, ARRAYSIZE(FindFileData.cFileName),
2252 cFileName, ARRAYSIZE(cFileName));
2253
2254 free(next);
2255 next = GetCombinedPath(path, cFileName);
2256 if (!next)
2257 goto fail;
2258
2259 if (!append_entry(stream, next))
2260 goto fail;
2261 if (is_directory(next))
2262 {
2263 if (!add_directory(stream, next))
2264 goto fail;
2265 }
2266 } while (FindNextFileW(hFind, &FindFileData));
2267
2268 rc = TRUE;
2269fail:
2270 free(next);
2271 FindClose(hFind);
2272
2273 return rc;
2274}
2275
2276static BOOL cliprdr_local_stream_update(CliprdrLocalStream* stream, const char* data, size_t size)
2277{
2278 BOOL rc = FALSE;
2279 WINPR_ASSERT(stream);
2280 if (size == 0)
2281 return TRUE;
2282
2283 cliprdr_local_files_free(stream);
2284
2285 stream->files = calloc(size, sizeof(CliprdrLocalFile));
2286 if (!stream->files)
2287 return FALSE;
2288
2289 char* copy = strndup(data, size);
2290 if (!copy)
2291 return FALSE;
2292
2293 char* saveptr = NULL;
2294 char* ptr = strtok_s(copy, "\r\n", &saveptr);
2295 while (ptr)
2296 {
2297 const char* name = ptr;
2298 if (strncmp("file:///", ptr, 8) == 0)
2299 name = &ptr[7];
2300 else if (strncmp("file:/", ptr, 6) == 0)
2301 name = &ptr[5];
2302
2303 if (!append_entry(stream, name))
2304 goto fail;
2305
2306 if (is_directory(name))
2307 {
2308 const BOOL res = add_directory(stream, name);
2309 if (!res)
2310 goto fail;
2311 }
2312 ptr = strtok_s(NULL, "\r\n", &saveptr);
2313 }
2314
2315 rc = TRUE;
2316fail:
2317 free(copy);
2318 return rc;
2319}
2320
2321CliprdrLocalStream* cliprdr_local_stream_new(CliprdrFileContext* context, UINT32 streamID,
2322 const char* data, size_t size)
2323{
2324 WINPR_ASSERT(context);
2325 CliprdrLocalStream* stream = calloc(1, sizeof(CliprdrLocalStream));
2326 if (!stream)
2327 return NULL;
2328
2329 stream->context = context;
2330 if (!cliprdr_local_stream_update(stream, data, size))
2331 goto fail;
2332
2333 stream->lockId = streamID;
2334 return stream;
2335
2336fail:
2337 cliprdr_local_stream_free(stream);
2338 return NULL;
2339}
2340
2341static UINT32 UINTPointerHash(const void* id)
2342{
2343 WINPR_ASSERT(id);
2344 return *((const UINT32*)id);
2345}
2346
2347static BOOL UINTPointerCompare(const void* pointer1, const void* pointer2)
2348{
2349 if (!pointer1 || !pointer2)
2350 return pointer1 == pointer2;
2351
2352 const UINT32* a = pointer1;
2353 const UINT32* b = pointer2;
2354 return *a == *b;
2355}
2356
2357static void* UINTPointerClone(const void* other)
2358{
2359 const UINT32* src = other;
2360 if (!src)
2361 return NULL;
2362
2363 UINT32* copy = calloc(1, sizeof(UINT32));
2364 if (!copy)
2365 return NULL;
2366
2367 *copy = *src;
2368 return copy;
2369}
2370
2371#if defined(WITH_FUSE)
2372CliprdrFuseFile* fuse_file_new_root(CliprdrFileContext* file_context)
2373{
2374 CliprdrFuseFile* root_dir = fuse_file_new("/");
2375 if (!root_dir)
2376 return NULL;
2377
2378 root_dir->ino = FUSE_ROOT_ID;
2379 root_dir->is_directory = TRUE;
2380 root_dir->is_readonly = TRUE;
2381
2382 if (!HashTable_Insert(file_context->inode_table, (void*)(uintptr_t)root_dir->ino, root_dir))
2383 {
2384 fuse_file_free(root_dir);
2385 return NULL;
2386 }
2387
2388 return root_dir;
2389}
2390#endif
2391
2392CliprdrFileContext* cliprdr_file_context_new(void* context)
2393{
2394 CliprdrFileContext* file = calloc(1, sizeof(CliprdrFileContext));
2395 if (!file)
2396 return NULL;
2397
2398 file->log = WLog_Get(CLIENT_TAG("common.cliprdr.file"));
2399 file->clipboard = context;
2400
2401 file->local_streams = HashTable_New(FALSE);
2402 if (!file->local_streams)
2403 goto fail;
2404
2405 if (!HashTable_SetHashFunction(file->local_streams, UINTPointerHash))
2406 goto fail;
2407
2408 {
2409 wObject* hkobj = HashTable_KeyObject(file->local_streams);
2410 WINPR_ASSERT(hkobj);
2411 hkobj->fnObjectEquals = UINTPointerCompare;
2412 hkobj->fnObjectFree = free;
2413 hkobj->fnObjectNew = UINTPointerClone;
2414 }
2415
2416 {
2417 wObject* hobj = HashTable_ValueObject(file->local_streams);
2418 WINPR_ASSERT(hobj);
2419 hobj->fnObjectFree = cliprdr_local_stream_free;
2420 }
2421
2422#if defined(WITH_FUSE)
2423 file->inode_table = HashTable_New(FALSE);
2424 file->clip_data_table = HashTable_New(FALSE);
2425 file->request_table = HashTable_New(FALSE);
2426 if (!file->inode_table || !file->clip_data_table || !file->request_table)
2427 goto fail;
2428
2429 {
2430 wObject* ctobj = HashTable_ValueObject(file->request_table);
2431 WINPR_ASSERT(ctobj);
2432 ctobj->fnObjectFree = free;
2433 }
2434 {
2435 wObject* ctobj = HashTable_ValueObject(file->clip_data_table);
2436 WINPR_ASSERT(ctobj);
2437 ctobj->fnObjectFree = clip_data_entry_free;
2438 }
2439#endif
2440
2441 if (!create_base_path(file))
2442 goto fail;
2443
2444#if defined(WITH_FUSE)
2445 if (!(file->fuse_start_sync = CreateEvent(NULL, TRUE, FALSE, NULL)))
2446 goto fail;
2447 if (!(file->fuse_stop_sync = CreateEvent(NULL, TRUE, FALSE, NULL)))
2448 goto fail;
2449 if (!(file->fuse_thread = CreateThread(NULL, 0, cliprdr_file_fuse_thread, file, 0, NULL)))
2450 goto fail;
2451
2452 if (WaitForSingleObject(file->fuse_start_sync, INFINITE) == WAIT_FAILED)
2453 WLog_Print(file->log, WLOG_ERROR, "Failed to wait for start sync");
2454#endif
2455 return file;
2456
2457fail:
2458 WINPR_PRAGMA_DIAG_PUSH
2459 WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
2460 cliprdr_file_context_free(file);
2461 WINPR_PRAGMA_DIAG_POP
2462 return NULL;
2463}
2464
2465BOOL local_stream_discard(const void* key, void* value, void* arg)
2466{
2467 CliprdrFileContext* file = arg;
2468 CliprdrLocalStream* stream = value;
2469 WINPR_ASSERT(file);
2470 WINPR_ASSERT(stream);
2471
2472 if (!stream->locked)
2473 HashTable_Remove(file->local_streams, key);
2474 return TRUE;
2475}
2476
2477BOOL cliprdr_file_context_clear(CliprdrFileContext* file)
2478{
2479 WINPR_ASSERT(file);
2480
2481 WLog_Print(file->log, WLOG_DEBUG, "clear file clipboard...");
2482
2483 HashTable_Lock(file->local_streams);
2484 HashTable_Foreach(file->local_streams, local_stream_discard, file);
2485 HashTable_Unlock(file->local_streams);
2486
2487 memset(file->server_data_hash, 0, sizeof(file->server_data_hash));
2488 memset(file->client_data_hash, 0, sizeof(file->client_data_hash));
2489 return TRUE;
2490}
2491
2492BOOL cliprdr_file_context_update_client_data(CliprdrFileContext* file, const char* data,
2493 size_t size)
2494{
2495 BOOL rc = FALSE;
2496
2497 WINPR_ASSERT(file);
2498 if (!cliprdr_file_client_content_changed_and_update(file, data, size))
2499 return TRUE;
2500
2501 if (!cliprdr_file_context_clear(file))
2502 return FALSE;
2503
2504 UINT32 lockId = file->local_lock_id;
2505
2506 HashTable_Lock(file->local_streams);
2507 CliprdrLocalStream* stream = HashTable_GetItemValue(file->local_streams, &lockId);
2508
2509 WLog_Print(file->log, WLOG_DEBUG, "update client file list (stream=%p)...", (void*)stream);
2510 if (stream)
2511 rc = cliprdr_local_stream_update(stream, data, size);
2512 else
2513 {
2514 stream = cliprdr_local_stream_new(file, lockId, data, size);
2515 rc = HashTable_Insert(file->local_streams, &stream->lockId, stream);
2516 if (!rc)
2517 cliprdr_local_stream_free(stream);
2518 }
2519 // HashTable_Insert owns stream
2520 // NOLINTNEXTLINE(clang-analyzer-unix.Malloc)
2521 HashTable_Unlock(file->local_streams);
2522 return rc;
2523}
2524
2525UINT32 cliprdr_file_context_current_flags(CliprdrFileContext* file)
2526{
2527 WINPR_ASSERT(file);
2528
2529 if ((file->file_capability_flags & CB_STREAM_FILECLIP_ENABLED) == 0)
2530 return 0;
2531
2532 if (!file->file_formats_registered)
2533 return 0;
2534
2535 return CB_STREAM_FILECLIP_ENABLED | CB_FILECLIP_NO_FILE_PATHS |
2536 CB_HUGE_FILE_SUPPORT_ENABLED; // | CB_CAN_LOCK_CLIPDATA;
2537}
2538
2539BOOL cliprdr_file_context_set_locally_available(CliprdrFileContext* file, BOOL available)
2540{
2541 WINPR_ASSERT(file);
2542 file->file_formats_registered = available;
2543 return TRUE;
2544}
2545
2546BOOL cliprdr_file_context_remote_set_flags(CliprdrFileContext* file, UINT32 flags)
2547{
2548 WINPR_ASSERT(file);
2549 file->file_capability_flags = flags;
2550 return TRUE;
2551}
2552
2553UINT32 cliprdr_file_context_remote_get_flags(CliprdrFileContext* file)
2554{
2555 WINPR_ASSERT(file);
2556 return file->file_capability_flags;
2557}
2558
2559BOOL cliprdr_file_context_has_local_support(CliprdrFileContext* file)
2560{
2561 WINPR_UNUSED(file);
2562
2563#if defined(WITH_FUSE)
2564 return TRUE;
2565#else
2566 return FALSE;
2567#endif
2568}
This struct contains function pointer to initialize/free objects.
Definition collections.h:57