FreeRDP
Loading...
Searching...
No Matches
drive_file.c
1
28#include <freerdp/config.h>
29
30#include <errno.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35
36#include <winpr/wtypes.h>
37#include <winpr/crt.h>
38#include <winpr/string.h>
39#include <winpr/path.h>
40#include <winpr/file.h>
41#include <winpr/stream.h>
42
43#include <freerdp/channels/rdpdr.h>
44
45#include "drive_file.h"
46
47#ifdef WITH_DEBUG_RDPDR
48#define DEBUG_WSTR(msg, wstr) \
49 do \
50 { \
51 char lpstr[1024] = WINPR_C_ARRAY_INIT; \
52 (void)ConvertWCharToUtf8(wstr, lpstr, ARRAYSIZE(lpstr)); \
53 WLog_DBG(TAG, msg, lpstr); \
54 } while (0)
55#else
56#define DEBUG_WSTR(msg, wstr) \
57 do \
58 { \
59 } while (0)
60#endif
61
62WINPR_ATTR_NODISCARD
63static BOOL drive_file_fix_path(WCHAR* path, size_t length)
64{
65 if ((length == 0) || (length > UINT32_MAX))
66 return FALSE;
67
68 WINPR_ASSERT(path);
69
70 for (size_t i = 0; i < length; i++)
71 {
72 if (path[i] == L'\\')
73 path[i] = L'/';
74 }
75
76#ifdef WIN32
77
78 if ((length == 3) && (path[1] == L':') && (path[2] == L'/'))
79 return FALSE;
80
81#else
82
83 if ((length == 1) && (path[0] == L'/'))
84 return FALSE;
85
86#endif
87
88 if ((length > 0) && (path[length - 1] == L'/'))
89 path[length - 1] = L'\0';
90
91 return TRUE;
92}
93
94WINPR_ATTR_NODISCARD
95static BOOL contains_dotdot(const WCHAR* path, size_t base_length, size_t path_length)
96{
97 WCHAR dotdotbuffer[6] = WINPR_C_ARRAY_INIT;
98 const WCHAR* dotdot = InitializeConstWCharFromUtf8("..", dotdotbuffer, ARRAYSIZE(dotdotbuffer));
99 const WCHAR* tst = path;
100
101 if (path_length < 2)
102 return FALSE;
103
104 do
105 {
106 tst = _wcsstr(tst, dotdot);
107 if (!tst)
108 return FALSE;
109
110 /* Filter .. sequences in file or directory names */
111 if ((base_length == 0) || (*(tst - 1) == L'/') || (*(tst - 1) == L'\\'))
112 {
113 if (tst + 2 < path + path_length)
114 {
115 if ((tst[2] == '/') || (tst[2] == '\\') || (tst[2] == '\0'))
116 return TRUE;
117 }
118 else
119 return TRUE;
120 }
121 tst += 2;
122 } while (TRUE);
123
124 return FALSE;
125}
126
127WINPR_ATTR_MALLOC(free, 1)
128static WCHAR* drive_file_combine_fullpath(const WCHAR* base_path, const WCHAR* path,
129 size_t PathWCharLength)
130{
131 BOOL ok = FALSE;
132 WCHAR* fullpath = nullptr;
133
134 if (!base_path || (!path && (PathWCharLength > 0)))
135 goto fail;
136
137 {
138 size_t base_path_length = _wcsnlen(base_path, MAX_PATH);
139 if (base_path_length < 1)
140 goto fail;
141
142 const size_t length = base_path_length + PathWCharLength + 2;
143 fullpath = (WCHAR*)calloc(length, sizeof(WCHAR));
144
145 if (!fullpath)
146 goto fail;
147
148 CopyMemory(fullpath, base_path, base_path_length * sizeof(WCHAR));
149
150 const WCHAR last = base_path[base_path_length - 1];
151 const WCHAR sepu = PathGetSeparatorW(PATH_STYLE_UNIX);
152 const WCHAR sepw = PathGetSeparatorW(PATH_STYLE_WINDOWS);
153 if ((last != sepu) && (last != sepw))
154 {
155 if (path && (PathWCharLength > 0) && (path[0] != sepu) && (path[0] != sepw))
156 fullpath[base_path_length++] = sepu;
157 }
158
159 if (path)
160 CopyMemory(&fullpath[base_path_length], path, PathWCharLength * sizeof(WCHAR));
161
162 if (!drive_file_fix_path(fullpath, length))
163 goto fail;
164
165 /* Ensure the path does not contain sequences like '..' */
166 if (contains_dotdot(&fullpath[base_path_length], base_path_length, PathWCharLength))
167 {
168 char* abuffer = ConvertWCharToUtf8Alloc(&fullpath[base_path_length], nullptr);
169 WLog_WARN(TAG, "[rdpdr] received invalid file path '%s' from server, aborting!",
170 abuffer);
171 free(abuffer);
172 goto fail;
173 }
174 }
175
176 ok = TRUE;
177fail:
178 if (!ok)
179 {
180 free(fullpath);
181 fullpath = nullptr;
182 }
183 return fullpath;
184}
185
186WINPR_ATTR_NODISCARD
187static BOOL drive_file_set_fullpath(DRIVE_FILE* file, const WCHAR* fullpath)
188{
189 if (!file || !fullpath)
190 return FALSE;
191
192 const size_t len = _wcslen(fullpath);
193 free(file->fullpath);
194 file->fullpath = nullptr;
195
196 if (len == 0)
197 return TRUE;
198
199 file->fullpath = wcsndup(fullpath, len);
200 if (!file->fullpath)
201 return FALSE;
202
203 const WCHAR sep[] = { PathGetSeparatorW(PATH_STYLE_NATIVE), '\0' };
204 WCHAR* filename = winpr_wcsnrchr(file->fullpath, len, *sep);
205 if (filename && _wcsncmp(filename, sep, ARRAYSIZE(sep)) == 0)
206 *filename = '\0';
207
208 return TRUE;
209}
210
211WINPR_ATTR_NODISCARD
212static BOOL drive_file_init(DRIVE_FILE* file)
213{
214 UINT CreateDisposition = 0;
215 DWORD dwAttr = GetFileAttributesW(file->fullpath);
216
217 if (dwAttr != INVALID_FILE_ATTRIBUTES)
218 {
219 /* The file exists */
220 file->is_dir = (dwAttr & FILE_ATTRIBUTE_DIRECTORY) != 0;
221
222 if (file->is_dir)
223 {
224 if (file->CreateDisposition == FILE_CREATE)
225 {
226 SetLastError(ERROR_ALREADY_EXISTS);
227 return FALSE;
228 }
229
230 if (file->CreateOptions & FILE_NON_DIRECTORY_FILE)
231 {
232 SetLastError(ERROR_ACCESS_DENIED);
233 return FALSE;
234 }
235
236 return TRUE;
237 }
238 else
239 {
240 if (file->CreateOptions & FILE_DIRECTORY_FILE)
241 {
242 SetLastError(ERROR_DIRECTORY);
243 return FALSE;
244 }
245 }
246 }
247 else
248 {
249 file->is_dir = ((file->CreateOptions & FILE_DIRECTORY_FILE) != 0);
250
251 if (file->is_dir)
252 {
253 /* Should only create the directory if the disposition allows for it */
254 if ((file->CreateDisposition == FILE_OPEN_IF) ||
255 (file->CreateDisposition == FILE_CREATE))
256 {
257 if (CreateDirectoryW(file->fullpath, nullptr) != 0)
258 {
259 return TRUE;
260 }
261 }
262
263 SetLastError(ERROR_FILE_NOT_FOUND);
264 return FALSE;
265 }
266 }
267
268 if (file->file_handle == INVALID_HANDLE_VALUE)
269 {
270 switch (file->CreateDisposition)
271 {
272 case FILE_SUPERSEDE: /* If the file already exists, replace it with the given file. If
273 it does not, create the given file. */
274 CreateDisposition = CREATE_ALWAYS;
275 break;
276
277 case FILE_OPEN: /* If the file already exists, open it instead of creating a new file.
278 If it does not, fail the request and do not create a new file. */
279 CreateDisposition = OPEN_EXISTING;
280 break;
281
282 case FILE_CREATE: /* If the file already exists, fail the request and do not create or
283 open the given file. If it does not, create the given file. */
284 CreateDisposition = CREATE_NEW;
285 break;
286
287 case FILE_OPEN_IF: /* If the file already exists, open it. If it does not, create the
288 given file. */
289 CreateDisposition = OPEN_ALWAYS;
290 break;
291
292 case FILE_OVERWRITE: /* If the file already exists, open it and overwrite it. If it does
293 not, fail the request. */
294 CreateDisposition = TRUNCATE_EXISTING;
295 break;
296
297 case FILE_OVERWRITE_IF: /* If the file already exists, open it and overwrite it. If it
298 does not, create the given file. */
299 CreateDisposition = CREATE_ALWAYS;
300 break;
301
302 default:
303 break;
304 }
305
306#ifndef WIN32
307 file->SharedAccess = 0;
308#endif
309 file->file_handle = CreateFileW(file->fullpath, file->DesiredAccess, file->SharedAccess,
310 nullptr, CreateDisposition, file->FileAttributes, nullptr);
311 }
312
313#ifdef WIN32
314 if (file->file_handle == INVALID_HANDLE_VALUE)
315 {
316 /* Get the error message, if any. */
317 DWORD errorMessageID = GetLastError();
318
319 if (errorMessageID != 0)
320 {
321 LPSTR messageBuffer = nullptr;
322 size_t size =
323 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
324 FORMAT_MESSAGE_IGNORE_INSERTS,
325 nullptr, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
326 (LPSTR)&messageBuffer, 0, nullptr);
327 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
328 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
329 WLog_ERR(TAG, "Error in drive_file_init: %s %s", messageBuffer, fullpath);
330 /* Free the buffer. */
331 LocalFree(messageBuffer);
332 /* restore original error code */
333 SetLastError(errorMessageID);
334 }
335 }
336#endif
337
338 return file->file_handle != INVALID_HANDLE_VALUE;
339}
340
341DRIVE_FILE* drive_file_new(const WCHAR* base_path, const WCHAR* path, UINT32 PathWCharLength,
342 UINT32 id, UINT32 DesiredAccess, UINT32 CreateDisposition,
343 UINT32 CreateOptions, UINT32 FileAttributes, UINT32 SharedAccess)
344{
345 if (!base_path || (!path && (PathWCharLength > 0)))
346 return nullptr;
347
348 DRIVE_FILE* file = (DRIVE_FILE*)calloc(1, sizeof(DRIVE_FILE));
349
350 if (!file)
351 {
352 WLog_ERR(TAG, "calloc failed!");
353 return nullptr;
354 }
355
356 file->file_handle = INVALID_HANDLE_VALUE;
357 file->find_handle = INVALID_HANDLE_VALUE;
358 file->id = id;
359 file->basepath = base_path;
360 file->FileAttributes = FileAttributes;
361 file->DesiredAccess = DesiredAccess;
362 file->CreateDisposition = CreateDisposition;
363 file->CreateOptions = CreateOptions;
364 file->SharedAccess = SharedAccess;
365
366 WCHAR* p = drive_file_combine_fullpath(base_path, path, PathWCharLength);
367 (void)drive_file_set_fullpath(file, p);
368 free(p);
369
370 if (!drive_file_init(file))
371 {
372 DWORD lastError = GetLastError();
373 drive_file_free(file);
374 SetLastError(lastError);
375 return nullptr;
376 }
377
378 return file;
379}
380
381BOOL drive_file_free(DRIVE_FILE* file)
382{
383 BOOL rc = FALSE;
384
385 if (!file)
386 return FALSE;
387
388 if (file->file_handle != INVALID_HANDLE_VALUE)
389 {
390 (void)CloseHandle(file->file_handle);
391 file->file_handle = INVALID_HANDLE_VALUE;
392 }
393
394 if (file->find_handle != INVALID_HANDLE_VALUE)
395 {
396 FindClose(file->find_handle);
397 file->find_handle = INVALID_HANDLE_VALUE;
398 }
399
400 if (file->CreateOptions & FILE_DELETE_ON_CLOSE)
401 file->delete_pending = TRUE;
402
403 if (file->delete_pending)
404 {
405 if (file->is_dir)
406 {
407 if (!winpr_RemoveDirectory_RecursiveW(file->fullpath))
408 goto fail;
409 }
410 else if (!DeleteFileW(file->fullpath))
411 goto fail;
412 }
413
414 rc = TRUE;
415fail:
416 DEBUG_WSTR("Free %s", file->fullpath);
417 free(file->fullpath);
418 free(file);
419 return rc;
420}
421
422BOOL drive_file_seek(DRIVE_FILE* file, UINT64 Offset)
423{
424 LARGE_INTEGER loffset = WINPR_C_ARRAY_INIT;
425
426 if (!file)
427 return FALSE;
428
429 if (Offset > INT64_MAX)
430 return FALSE;
431
432 loffset.QuadPart = (LONGLONG)Offset;
433 return SetFilePointerEx(file->file_handle, loffset, nullptr, FILE_BEGIN);
434}
435
436BOOL drive_file_read(DRIVE_FILE* file, wStream* s, UINT64 Offset, UINT32* Length)
437{
438 DWORD read = 0;
439
440 if (!file || !s || !Length)
441 {
442 SetLastError(ERROR_INVALID_PARAMETER);
443 return FALSE;
444 }
445
446 UINT32 size = *Length;
447 *Length = 0;
448
449 if (!drive_file_seek(file, Offset))
450 return FALSE;
451
452 DEBUG_WSTR("Read file %s", file->fullpath);
453
454 DWORD sizeHigh = 0;
455 const DWORD sizeLow = GetFileSize(file, &sizeHigh);
456 const UINT64 size64 = 1ull * sizeLow + ((1ull * sizeHigh) << 32);
457 if (Offset > size64)
458 {
459 SetLastError(ERROR_INVALID_PARAMETER);
460 return FALSE;
461 }
462
463 const UINT64 remain = size64 - Offset;
464 if (remain < size)
465 size = WINPR_ASSERTING_INT_CAST(UINT32, remain);
466
467 if (!Stream_EnsureRemainingCapacity(s, size))
468 {
469 SetLastError(ERROR_NOT_ENOUGH_MEMORY);
470 return FALSE;
471 }
472
473 void* buffer = Stream_Pointer(s);
474 if (!ReadFile(file->file_handle, buffer, size, &read, nullptr))
475 return FALSE;
476
477 *Length = read;
478 return TRUE;
479}
480
481BOOL drive_file_write(DRIVE_FILE* file, const BYTE* buffer, UINT32 Length)
482{
483 DWORD written = 0;
484
485 if (!file || !buffer)
486 return FALSE;
487
488 DEBUG_WSTR("Write file %s", file->fullpath);
489
490 while (Length > 0)
491 {
492 if (!WriteFile(file->file_handle, buffer, Length, &written, nullptr))
493 return FALSE;
494
495 Length -= written;
496 buffer += written;
497 }
498
499 return TRUE;
500}
501
502WINPR_ATTR_NODISCARD
503static BOOL drive_file_query_from_handle_information(const DRIVE_FILE* file,
504 const BY_HANDLE_FILE_INFORMATION* info,
505 UINT32 FsInformationClass, wStream* output)
506{
507 switch (FsInformationClass)
508 {
509 case FileBasicInformation:
510
511 /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
512 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
513 return FALSE;
514
515 Stream_Write_UINT32(output, 36); /* Length */
516 Stream_Write_UINT32(output, info->ftCreationTime.dwLowDateTime); /* CreationTime */
517 Stream_Write_UINT32(output, info->ftCreationTime.dwHighDateTime); /* CreationTime */
518 Stream_Write_UINT32(output, info->ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
519 Stream_Write_UINT32(output, info->ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
520 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
521 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
522 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime); /* ChangeTime */
523 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime); /* ChangeTime */
524 Stream_Write_UINT32(output, info->dwFileAttributes); /* FileAttributes */
525 /* Reserved(4), MUST NOT be added! */
526 break;
527
528 case FileStandardInformation:
529
530 /* http://msdn.microsoft.com/en-us/library/cc232088.aspx */
531 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
532 return FALSE;
533
534 Stream_Write_UINT32(output, 22); /* Length */
535 Stream_Write_UINT32(output, info->nFileSizeLow); /* AllocationSize */
536 Stream_Write_UINT32(output, info->nFileSizeHigh); /* AllocationSize */
537 Stream_Write_UINT32(output, info->nFileSizeLow); /* EndOfFile */
538 Stream_Write_UINT32(output, info->nFileSizeHigh); /* EndOfFile */
539 Stream_Write_UINT32(output, info->nNumberOfLinks); /* NumberOfLinks */
540 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0); /* DeletePending */
541 Stream_Write_UINT8(output, (info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
542 0); /* Directory */
543 /* Reserved(2), MUST NOT be added! */
544 break;
545
546 case FileAttributeTagInformation:
547
548 /* http://msdn.microsoft.com/en-us/library/cc232093.aspx */
549 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
550 return FALSE;
551
552 Stream_Write_UINT32(output, 8); /* Length */
553 Stream_Write_UINT32(output, info->dwFileAttributes); /* FileAttributes */
554 Stream_Write_UINT32(output, 0); /* ReparseTag */
555 break;
556
557 default:
558 /* Unhandled FsInformationClass */
559 WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
560 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
561 return FALSE;
562 }
563
564 return TRUE;
565}
566
567WINPR_ATTR_NODISCARD
568static BOOL drive_file_query_from_attributes(const DRIVE_FILE* file,
569 const WIN32_FILE_ATTRIBUTE_DATA* attrib,
570 UINT32 FsInformationClass, wStream* output)
571{
572 switch (FsInformationClass)
573 {
574 case FileBasicInformation:
575
576 /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
577 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
578 return FALSE;
579
580 Stream_Write_UINT32(output, 36); /* Length */
581 Stream_Write_UINT32(output, attrib->ftCreationTime.dwLowDateTime); /* CreationTime */
582 Stream_Write_UINT32(output, attrib->ftCreationTime.dwHighDateTime); /* CreationTime */
583 Stream_Write_UINT32(output,
584 attrib->ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
585 Stream_Write_UINT32(output,
586 attrib->ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
587 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
588 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
589 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime); /* ChangeTime */
590 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime); /* ChangeTime */
591 Stream_Write_UINT32(output, attrib->dwFileAttributes); /* FileAttributes */
592 /* Reserved(4), MUST NOT be added! */
593 break;
594
595 case FileStandardInformation:
596
597 /* http://msdn.microsoft.com/en-us/library/cc232088.aspx */
598 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
599 return FALSE;
600
601 Stream_Write_UINT32(output, 22); /* Length */
602 Stream_Write_UINT32(output, attrib->nFileSizeLow); /* AllocationSize */
603 Stream_Write_UINT32(output, attrib->nFileSizeHigh); /* AllocationSize */
604 Stream_Write_UINT32(output, attrib->nFileSizeLow); /* EndOfFile */
605 Stream_Write_UINT32(output, attrib->nFileSizeHigh); /* EndOfFile */
606 Stream_Write_UINT32(output, 0); /* NumberOfLinks */
607 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0); /* DeletePending */
608 Stream_Write_UINT8(output, (attrib->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
609 0); /* Directory */
610 /* Reserved(2), MUST NOT be added! */
611 break;
612
613 case FileAttributeTagInformation:
614
615 /* http://msdn.microsoft.com/en-us/library/cc232093.aspx */
616 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
617 return FALSE;
618
619 Stream_Write_UINT32(output, 8); /* Length */
620 Stream_Write_UINT32(output, attrib->dwFileAttributes); /* FileAttributes */
621 Stream_Write_UINT32(output, 0); /* ReparseTag */
622 break;
623
624 default:
625 /* Unhandled FsInformationClass */
626 WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
627 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
628 return FALSE;
629 }
630
631 return TRUE;
632}
633
634BOOL drive_file_query_information(DRIVE_FILE* file, UINT32 FsInformationClass, wStream* output)
635{
636 BY_HANDLE_FILE_INFORMATION fileInformation = WINPR_C_ARRAY_INIT;
637 BOOL status = 0;
638
639 if (!file || !output)
640 return FALSE;
641
642 if ((file->file_handle != INVALID_HANDLE_VALUE) &&
643 GetFileInformationByHandle(file->file_handle, &fileInformation))
644 return drive_file_query_from_handle_information(file, &fileInformation, FsInformationClass,
645 output);
646
647 if (!file->is_dir)
648 {
649 HANDLE hFile = CreateFileW(file->fullpath, 0, FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
650 FILE_ATTRIBUTE_NORMAL, nullptr);
651 if (hFile != INVALID_HANDLE_VALUE)
652 {
653 status = GetFileInformationByHandle(hFile, &fileInformation);
654 (void)CloseHandle(hFile);
655 if (!status)
656 goto out_fail;
657
658 if (!drive_file_query_from_handle_information(file, &fileInformation,
659 FsInformationClass, output))
660 goto out_fail;
661
662 return TRUE;
663 }
664 }
665
666 /* If we failed before (i.e. if information for a drive is queried) fall back to
667 * GetFileAttributesExW */
668 {
669 WIN32_FILE_ATTRIBUTE_DATA fileAttributes = WINPR_C_ARRAY_INIT;
670 if (!GetFileAttributesExW(file->fullpath, GetFileExInfoStandard, &fileAttributes))
671 goto out_fail;
672
673 if (!drive_file_query_from_attributes(file, &fileAttributes, FsInformationClass, output))
674 goto out_fail;
675 }
676
677 return TRUE;
678out_fail:
679 Stream_Write_UINT32(output, 0); /* Length */
680 return FALSE;
681}
682
683WINPR_ATTR_NODISCARD
684static BOOL drive_file_set_basic_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
685{
686 WINPR_ASSERT(file);
687
688 const uint32_t expect = 36;
689 if (Length != expect)
690 {
691 WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect);
692 return FALSE;
693 }
694
695 /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
696 const ULARGE_INTEGER liCreationTime = { .QuadPart = Stream_Get_UINT64(input) };
697 const ULARGE_INTEGER liLastAccessTime = { .QuadPart = Stream_Get_UINT64(input) };
698 const ULARGE_INTEGER liLastWriteTime = { .QuadPart = Stream_Get_UINT64(input) };
699 const ULARGE_INTEGER liChangeTime = { .QuadPart = Stream_Get_UINT64(input) };
700 const uint32_t FileAttributes = Stream_Get_UINT32(input);
701
702 if (!PathFileExistsW(file->fullpath))
703 return FALSE;
704
705 if (file->file_handle == INVALID_HANDLE_VALUE)
706 {
707 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
708 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath) - 1);
709
710 WLog_ERR(TAG, "Unable to set file time %s (%" PRIu32 ")", fullpath, GetLastError());
711 return FALSE;
712 }
713
714 FILETIME ftCreationTime = WINPR_C_ARRAY_INIT;
715 FILETIME ftLastAccessTime = WINPR_C_ARRAY_INIT;
716 FILETIME ftLastWriteTime = WINPR_C_ARRAY_INIT;
717 FILETIME* pftCreationTime = nullptr;
718 FILETIME* pftLastAccessTime = nullptr;
719 FILETIME* pftLastWriteTime = nullptr;
720 if (liCreationTime.QuadPart != 0)
721 {
722 ftCreationTime.dwHighDateTime = liCreationTime.u.HighPart;
723 ftCreationTime.dwLowDateTime = liCreationTime.u.LowPart;
724 pftCreationTime = &ftCreationTime;
725 }
726
727 if (liLastAccessTime.QuadPart != 0)
728 {
729 ftLastAccessTime.dwHighDateTime = liLastAccessTime.u.HighPart;
730 ftLastAccessTime.dwLowDateTime = liLastAccessTime.u.LowPart;
731 pftLastAccessTime = &ftLastAccessTime;
732 }
733
734 if (liLastWriteTime.QuadPart != 0)
735 {
736 ftLastWriteTime.dwHighDateTime = liLastWriteTime.u.HighPart;
737 ftLastWriteTime.dwLowDateTime = liLastWriteTime.u.LowPart;
738 pftLastWriteTime = &ftLastWriteTime;
739 }
740
741 if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart)
742 {
743 ftLastWriteTime.dwHighDateTime = liChangeTime.u.HighPart;
744 ftLastWriteTime.dwLowDateTime = liChangeTime.u.LowPart;
745 pftLastWriteTime = &ftLastWriteTime;
746 }
747
748 DEBUG_WSTR("SetFileTime %s", file->fullpath);
749
750 if (!SetFileAttributesW(file->fullpath, FileAttributes))
751 {
752 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
753 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
754 WLog_ERR(TAG, "Unable to set file attributes for %s", fullpath);
755 return FALSE;
756 }
757
758 if (!SetFileTime(file->file_handle, pftCreationTime, pftLastAccessTime, pftLastWriteTime))
759 {
760 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
761 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
762 WLog_ERR(TAG, "Unable to set file time for %s", fullpath);
763 return FALSE;
764 }
765 return TRUE;
766}
767
768WINPR_ATTR_NODISCARD
769static BOOL drive_file_set_alloc_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
770{
771 WINPR_ASSERT(file);
772 const uint32_t expect = 8;
773 if (Length != expect)
774 {
775 WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect);
776 return FALSE;
777 }
778
779 /* http://msdn.microsoft.com/en-us/library/cc232076.aspx */
780 const int64_t size = Stream_Get_INT64(input);
781
782 if (file->file_handle == INVALID_HANDLE_VALUE)
783 {
784 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
785 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
786 WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size,
787 GetLastError());
788 return FALSE;
789 }
790
791 LARGE_INTEGER liSize = { .QuadPart = size };
792
793 if (!SetFilePointerEx(file->file_handle, liSize, nullptr, FILE_BEGIN))
794 {
795 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
796 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
797 WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size,
798 GetLastError());
799 return FALSE;
800 }
801
802 DEBUG_WSTR("Truncate %s", file->fullpath);
803
804 if (SetEndOfFile(file->file_handle) == 0)
805 {
806 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
807 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
808 WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size,
809 GetLastError());
810 return FALSE;
811 }
812
813 return TRUE;
814}
815
816WINPR_ATTR_NODISCARD
817static BOOL drive_file_set_disposition_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
818{
819 WINPR_ASSERT(file);
820 uint8_t delete_pending = 0;
821 /* http://msdn.microsoft.com/en-us/library/cc232098.aspx */
822 /* http://msdn.microsoft.com/en-us/library/cc241371.aspx */
823 if (file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
824 {
825 SetLastError(ERROR_DIR_NOT_EMPTY);
826 return FALSE;
827 }
828
829 if (Length)
830 {
831 const uint32_t expect = 1;
832 if (Length != expect)
833 WLog_DBG(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect);
834
835 delete_pending = Stream_Get_UINT8(input);
836 }
837 else
838 delete_pending = 1;
839
840 if (delete_pending)
841 {
842 DEBUG_WSTR("SetDeletePending %s", file->fullpath);
843 const uint32_t attr = GetFileAttributesW(file->fullpath);
844
845 if (attr & FILE_ATTRIBUTE_READONLY)
846 {
847 SetLastError(ERROR_ACCESS_DENIED);
848 return FALSE;
849 }
850 }
851
852 file->delete_pending = delete_pending;
853 return TRUE;
854}
855
856WINPR_ATTR_NODISCARD
857static BOOL drive_file_set_rename_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
858{
859 WINPR_ASSERT(file);
860
861 const uint32_t expect = 6;
862 if (Length < expect)
863 {
864 WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected at least %" PRIu32, Length, expect);
865 return FALSE;
866 }
867
868 /* http://msdn.microsoft.com/en-us/library/cc232085.aspx */
869 const uint8_t ReplaceIfExists = Stream_Get_UINT8(input);
870 Stream_Seek_UINT8(input); /* RootDirectory */
871 const uint64_t FileNameLength = Stream_Get_UINT32(input);
872
873 if (Length != expect + FileNameLength)
874 {
875 WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu64, Length,
876 expect + FileNameLength);
877 return FALSE;
878 }
879
880 WCHAR* fullpath = drive_file_combine_fullpath(file->basepath, Stream_ConstPointer(input),
881 FileNameLength / sizeof(WCHAR));
882
883 if (!fullpath)
884 return FALSE;
885
886#ifdef _WIN32
887
888 if (file->file_handle != INVALID_HANDLE_VALUE)
889 {
890 (void)CloseHandle(file->file_handle);
891 file->file_handle = INVALID_HANDLE_VALUE;
892 }
893
894#endif
895 DEBUG_WSTR("MoveFileExW %s", file->fullpath);
896
897 if (MoveFileExW(file->fullpath, fullpath,
898 MOVEFILE_COPY_ALLOWED | (ReplaceIfExists ? MOVEFILE_REPLACE_EXISTING : 0)))
899 {
900 const BOOL rc = drive_file_set_fullpath(file, fullpath);
901 free(fullpath);
902 if (!rc)
903 return FALSE;
904 }
905 else
906 {
907 free(fullpath);
908 return FALSE;
909 }
910
911#ifdef _WIN32
912 drive_file_init(file);
913#endif
914 return TRUE;
915}
916
917BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
918 wStream* input)
919{
920 if (!file || !input)
921 return FALSE;
922
923 if (!Stream_CheckAndLogRequiredLength(TAG, input, Length))
924 return FALSE;
925
926 switch (FsInformationClass)
927 {
928 case FileBasicInformation:
929 return drive_file_set_basic_information(file, Length, input);
930
931 case FileEndOfFileInformation:
932 /* http://msdn.microsoft.com/en-us/library/cc232067.aspx */
933 case FileAllocationInformation:
934 return drive_file_set_alloc_information(file, Length, input);
935
936 case FileDispositionInformation:
937 return drive_file_set_disposition_information(file, Length, input);
938
939 case FileRenameInformation:
940 return drive_file_set_rename_information(file, Length, input);
941
942 default:
943 WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
944 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
945 return FALSE;
946 }
947
948 return TRUE;
949}
950
951WINPR_ATTR_NODISCARD
952static BOOL drive_file_query_dir_info(DRIVE_FILE* file, wStream* output, size_t length)
953{
954 WINPR_ASSERT(file);
955 WINPR_ASSERT(output);
956
957 /* http://msdn.microsoft.com/en-us/library/cc232097.aspx */
958 if (!Stream_EnsureRemainingCapacity(output, 4 + 64 + length))
959 return FALSE;
960
961 if (length > UINT32_MAX - 64)
962 return FALSE;
963
964 Stream_Write_UINT32(output, (UINT32)(64 + length)); /* Length */
965 Stream_Write_UINT32(output, 0); /* NextEntryOffset */
966 Stream_Write_UINT32(output, 0); /* FileIndex */
967 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */
968 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */
969 Stream_Write_UINT32(output,
970 file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
971 Stream_Write_UINT32(output,
972 file->find_data.ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
973 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
974 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
975 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* ChangeTime */
976 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */
977 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* EndOfFile */
978 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* EndOfFile */
979 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* AllocationSize */
980 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* AllocationSize */
981 Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */
982 Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */
983 Stream_Write(output, file->find_data.cFileName, length);
984 return TRUE;
985}
986
987WINPR_ATTR_NODISCARD
988static BOOL drive_file_query_full_dir_info(DRIVE_FILE* file, wStream* output, size_t length)
989{
990 WINPR_ASSERT(file);
991 WINPR_ASSERT(output);
992 /* http://msdn.microsoft.com/en-us/library/cc232068.aspx */
993 if (!Stream_EnsureRemainingCapacity(output, 4 + 68 + length))
994 return FALSE;
995
996 if (length > UINT32_MAX - 68)
997 return FALSE;
998
999 Stream_Write_UINT32(output, (UINT32)(68 + length)); /* Length */
1000 Stream_Write_UINT32(output, 0); /* NextEntryOffset */
1001 Stream_Write_UINT32(output, 0); /* FileIndex */
1002 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */
1003 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */
1004 Stream_Write_UINT32(output,
1005 file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
1006 Stream_Write_UINT32(output,
1007 file->find_data.ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
1008 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
1009 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
1010 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* ChangeTime */
1011 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */
1012 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* EndOfFile */
1013 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* EndOfFile */
1014 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* AllocationSize */
1015 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* AllocationSize */
1016 Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */
1017 Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */
1018 Stream_Write_UINT32(output, 0); /* EaSize */
1019 Stream_Write(output, file->find_data.cFileName, length);
1020 return TRUE;
1021}
1022
1023WINPR_ATTR_NODISCARD
1024static BOOL drive_file_query_both_dir_info(DRIVE_FILE* file, wStream* output, size_t length)
1025{
1026 WINPR_ASSERT(file);
1027 WINPR_ASSERT(output);
1028 /* http://msdn.microsoft.com/en-us/library/cc232095.aspx */
1029 if (!Stream_EnsureRemainingCapacity(output, 4 + 93 + length))
1030 return FALSE;
1031
1032 if (length > UINT32_MAX - 93)
1033 return FALSE;
1034
1035 Stream_Write_UINT32(output, (UINT32)(93 + length)); /* Length */
1036 Stream_Write_UINT32(output, 0); /* NextEntryOffset */
1037 Stream_Write_UINT32(output, 0); /* FileIndex */
1038 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */
1039 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */
1040 Stream_Write_UINT32(output,
1041 file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
1042 Stream_Write_UINT32(output,
1043 file->find_data.ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
1044 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
1045 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
1046 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* ChangeTime */
1047 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */
1048 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* EndOfFile */
1049 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* EndOfFile */
1050 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* AllocationSize */
1051 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* AllocationSize */
1052 Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */
1053 Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */
1054 Stream_Write_UINT32(output, 0); /* EaSize */
1055 Stream_Write_UINT8(output, 0); /* ShortNameLength */
1056 /* Reserved(1), MUST NOT be added! */
1057 Stream_Zero(output, 24); /* ShortName */
1058 Stream_Write(output, file->find_data.cFileName, length);
1059 return TRUE;
1060}
1061
1062WINPR_ATTR_NODISCARD
1063static BOOL drive_file_query_names_info(DRIVE_FILE* file, wStream* output, size_t length)
1064{
1065 WINPR_ASSERT(file);
1066 WINPR_ASSERT(output);
1067 /* http://msdn.microsoft.com/en-us/library/cc232077.aspx */
1068 if (!Stream_EnsureRemainingCapacity(output, 4 + 12 + length))
1069 return FALSE;
1070
1071 if (length > UINT32_MAX - 12)
1072 return FALSE;
1073
1074 Stream_Write_UINT32(output, (UINT32)(12 + length)); /* Length */
1075 Stream_Write_UINT32(output, 0); /* NextEntryOffset */
1076 Stream_Write_UINT32(output, 0); /* FileIndex */
1077 Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */
1078 Stream_Write(output, file->find_data.cFileName, length);
1079 return TRUE;
1080}
1081
1082BOOL drive_file_query_directory(DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
1083 const WCHAR* path, UINT32 PathWCharLength, wStream* output)
1084{
1085 BOOL rc = FALSE;
1086 size_t length = 0;
1087 WCHAR* ent_path = nullptr;
1088
1089 if (!file || !path || !output)
1090 return FALSE;
1091
1092 if (InitialQuery != 0)
1093 {
1094 /* release search handle */
1095 if (file->find_handle != INVALID_HANDLE_VALUE)
1096 FindClose(file->find_handle);
1097
1098 ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength);
1099 /* open new search handle and retrieve the first entry */
1100 file->find_handle = FindFirstFileW(ent_path, &file->find_data);
1101 free(ent_path);
1102
1103 if (file->find_handle == INVALID_HANDLE_VALUE)
1104 goto out_fail;
1105 }
1106 else if (!FindNextFileW(file->find_handle, &file->find_data))
1107 goto out_fail;
1108
1109 length = _wcslen(file->find_data.cFileName) * sizeof(WCHAR);
1110
1111 switch (FsInformationClass)
1112 {
1113 case FileDirectoryInformation:
1114 rc = drive_file_query_dir_info(file, output, length);
1115 break;
1116
1117 case FileFullDirectoryInformation:
1118 rc = drive_file_query_full_dir_info(file, output, length);
1119 break;
1120
1121 case FileBothDirectoryInformation:
1122 rc = drive_file_query_both_dir_info(file, output, length);
1123 break;
1124
1125 case FileNamesInformation:
1126 rc = drive_file_query_names_info(file, output, length);
1127 break;
1128
1129 default:
1130 WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
1131 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
1132 /* Unhandled FsInformationClass */
1133 goto out_fail;
1134 }
1135
1136out_fail:
1137 if (!rc)
1138 {
1139 Stream_Write_UINT32(output, 0); /* Length */
1140 Stream_Write_UINT8(output, 0); /* Padding */
1141 }
1142 return rc;
1143}