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