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