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