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 = _wcsdup(fullpath);
200 if (!file->fullpath)
201 return FALSE;
202
203 const WCHAR sep[] = { PathGetSeparatorW(PATH_STYLE_NATIVE), '\0' };
204 WCHAR* filename = _wcsrchr(file->fullpath, *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, BYTE* buffer, UINT32* Length)
437{
438 DWORD read = 0;
439
440 if (!file || !buffer || !Length)
441 return FALSE;
442
443 DEBUG_WSTR("Read file %s", file->fullpath);
444
445 if (ReadFile(file->file_handle, buffer, *Length, &read, nullptr))
446 {
447 *Length = read;
448 return TRUE;
449 }
450
451 return FALSE;
452}
453
454BOOL drive_file_write(DRIVE_FILE* file, const BYTE* buffer, UINT32 Length)
455{
456 DWORD written = 0;
457
458 if (!file || !buffer)
459 return FALSE;
460
461 DEBUG_WSTR("Write file %s", file->fullpath);
462
463 while (Length > 0)
464 {
465 if (!WriteFile(file->file_handle, buffer, Length, &written, nullptr))
466 return FALSE;
467
468 Length -= written;
469 buffer += written;
470 }
471
472 return TRUE;
473}
474
475WINPR_ATTR_NODISCARD
476static BOOL drive_file_query_from_handle_information(const DRIVE_FILE* file,
477 const BY_HANDLE_FILE_INFORMATION* info,
478 UINT32 FsInformationClass, wStream* output)
479{
480 switch (FsInformationClass)
481 {
482 case FileBasicInformation:
483
484 /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
485 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
486 return FALSE;
487
488 Stream_Write_UINT32(output, 36); /* Length */
489 Stream_Write_UINT32(output, info->ftCreationTime.dwLowDateTime); /* CreationTime */
490 Stream_Write_UINT32(output, info->ftCreationTime.dwHighDateTime); /* CreationTime */
491 Stream_Write_UINT32(output, info->ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
492 Stream_Write_UINT32(output, info->ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
493 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
494 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
495 Stream_Write_UINT32(output, info->ftLastWriteTime.dwLowDateTime); /* ChangeTime */
496 Stream_Write_UINT32(output, info->ftLastWriteTime.dwHighDateTime); /* ChangeTime */
497 Stream_Write_UINT32(output, info->dwFileAttributes); /* FileAttributes */
498 /* Reserved(4), MUST NOT be added! */
499 break;
500
501 case FileStandardInformation:
502
503 /* http://msdn.microsoft.com/en-us/library/cc232088.aspx */
504 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
505 return FALSE;
506
507 Stream_Write_UINT32(output, 22); /* Length */
508 Stream_Write_UINT32(output, info->nFileSizeLow); /* AllocationSize */
509 Stream_Write_UINT32(output, info->nFileSizeHigh); /* AllocationSize */
510 Stream_Write_UINT32(output, info->nFileSizeLow); /* EndOfFile */
511 Stream_Write_UINT32(output, info->nFileSizeHigh); /* EndOfFile */
512 Stream_Write_UINT32(output, info->nNumberOfLinks); /* NumberOfLinks */
513 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0); /* DeletePending */
514 Stream_Write_UINT8(output, (info->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
515 0); /* Directory */
516 /* Reserved(2), MUST NOT be added! */
517 break;
518
519 case FileAttributeTagInformation:
520
521 /* http://msdn.microsoft.com/en-us/library/cc232093.aspx */
522 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
523 return FALSE;
524
525 Stream_Write_UINT32(output, 8); /* Length */
526 Stream_Write_UINT32(output, info->dwFileAttributes); /* FileAttributes */
527 Stream_Write_UINT32(output, 0); /* ReparseTag */
528 break;
529
530 default:
531 /* Unhandled FsInformationClass */
532 WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
533 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
534 return FALSE;
535 }
536
537 return TRUE;
538}
539
540WINPR_ATTR_NODISCARD
541static BOOL drive_file_query_from_attributes(const DRIVE_FILE* file,
542 const WIN32_FILE_ATTRIBUTE_DATA* attrib,
543 UINT32 FsInformationClass, wStream* output)
544{
545 switch (FsInformationClass)
546 {
547 case FileBasicInformation:
548
549 /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
550 if (!Stream_EnsureRemainingCapacity(output, 4 + 36))
551 return FALSE;
552
553 Stream_Write_UINT32(output, 36); /* Length */
554 Stream_Write_UINT32(output, attrib->ftCreationTime.dwLowDateTime); /* CreationTime */
555 Stream_Write_UINT32(output, attrib->ftCreationTime.dwHighDateTime); /* CreationTime */
556 Stream_Write_UINT32(output,
557 attrib->ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
558 Stream_Write_UINT32(output,
559 attrib->ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
560 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
561 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
562 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwLowDateTime); /* ChangeTime */
563 Stream_Write_UINT32(output, attrib->ftLastWriteTime.dwHighDateTime); /* ChangeTime */
564 Stream_Write_UINT32(output, attrib->dwFileAttributes); /* FileAttributes */
565 /* Reserved(4), MUST NOT be added! */
566 break;
567
568 case FileStandardInformation:
569
570 /* http://msdn.microsoft.com/en-us/library/cc232088.aspx */
571 if (!Stream_EnsureRemainingCapacity(output, 4 + 22))
572 return FALSE;
573
574 Stream_Write_UINT32(output, 22); /* Length */
575 Stream_Write_UINT32(output, attrib->nFileSizeLow); /* AllocationSize */
576 Stream_Write_UINT32(output, attrib->nFileSizeHigh); /* AllocationSize */
577 Stream_Write_UINT32(output, attrib->nFileSizeLow); /* EndOfFile */
578 Stream_Write_UINT32(output, attrib->nFileSizeHigh); /* EndOfFile */
579 Stream_Write_UINT32(output, 0); /* NumberOfLinks */
580 Stream_Write_UINT8(output, file->delete_pending ? 1 : 0); /* DeletePending */
581 Stream_Write_UINT8(output, (attrib->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=
582 0); /* Directory */
583 /* Reserved(2), MUST NOT be added! */
584 break;
585
586 case FileAttributeTagInformation:
587
588 /* http://msdn.microsoft.com/en-us/library/cc232093.aspx */
589 if (!Stream_EnsureRemainingCapacity(output, 4 + 8))
590 return FALSE;
591
592 Stream_Write_UINT32(output, 8); /* Length */
593 Stream_Write_UINT32(output, attrib->dwFileAttributes); /* FileAttributes */
594 Stream_Write_UINT32(output, 0); /* ReparseTag */
595 break;
596
597 default:
598 /* Unhandled FsInformationClass */
599 WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
600 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
601 return FALSE;
602 }
603
604 return TRUE;
605}
606
607BOOL drive_file_query_information(DRIVE_FILE* file, UINT32 FsInformationClass, wStream* output)
608{
609 BY_HANDLE_FILE_INFORMATION fileInformation = WINPR_C_ARRAY_INIT;
610 BOOL status = 0;
611
612 if (!file || !output)
613 return FALSE;
614
615 if ((file->file_handle != INVALID_HANDLE_VALUE) &&
616 GetFileInformationByHandle(file->file_handle, &fileInformation))
617 return drive_file_query_from_handle_information(file, &fileInformation, FsInformationClass,
618 output);
619
620 if (!file->is_dir)
621 {
622 HANDLE hFile = CreateFileW(file->fullpath, 0, FILE_SHARE_DELETE, nullptr, OPEN_EXISTING,
623 FILE_ATTRIBUTE_NORMAL, nullptr);
624 if (hFile != INVALID_HANDLE_VALUE)
625 {
626 status = GetFileInformationByHandle(hFile, &fileInformation);
627 (void)CloseHandle(hFile);
628 if (!status)
629 goto out_fail;
630
631 if (!drive_file_query_from_handle_information(file, &fileInformation,
632 FsInformationClass, output))
633 goto out_fail;
634
635 return TRUE;
636 }
637 }
638
639 /* If we failed before (i.e. if information for a drive is queried) fall back to
640 * GetFileAttributesExW */
641 {
642 WIN32_FILE_ATTRIBUTE_DATA fileAttributes = WINPR_C_ARRAY_INIT;
643 if (!GetFileAttributesExW(file->fullpath, GetFileExInfoStandard, &fileAttributes))
644 goto out_fail;
645
646 if (!drive_file_query_from_attributes(file, &fileAttributes, FsInformationClass, output))
647 goto out_fail;
648 }
649
650 return TRUE;
651out_fail:
652 Stream_Write_UINT32(output, 0); /* Length */
653 return FALSE;
654}
655
656WINPR_ATTR_NODISCARD
657static BOOL drive_file_set_basic_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
658{
659 WINPR_ASSERT(file);
660
661 const uint32_t expect = 36;
662 if (Length != expect)
663 {
664 WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect);
665 return FALSE;
666 }
667
668 /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */
669 const ULARGE_INTEGER liCreationTime = { .QuadPart = Stream_Get_UINT64(input) };
670 const ULARGE_INTEGER liLastAccessTime = { .QuadPart = Stream_Get_UINT64(input) };
671 const ULARGE_INTEGER liLastWriteTime = { .QuadPart = Stream_Get_UINT64(input) };
672 const ULARGE_INTEGER liChangeTime = { .QuadPart = Stream_Get_UINT64(input) };
673 const uint32_t FileAttributes = Stream_Get_UINT32(input);
674
675 if (!PathFileExistsW(file->fullpath))
676 return FALSE;
677
678 if (file->file_handle == INVALID_HANDLE_VALUE)
679 {
680 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
681 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath) - 1);
682
683 WLog_ERR(TAG, "Unable to set file time %s (%" PRIu32 ")", fullpath, GetLastError());
684 return FALSE;
685 }
686
687 FILETIME ftCreationTime = WINPR_C_ARRAY_INIT;
688 FILETIME ftLastAccessTime = WINPR_C_ARRAY_INIT;
689 FILETIME ftLastWriteTime = WINPR_C_ARRAY_INIT;
690 FILETIME* pftCreationTime = nullptr;
691 FILETIME* pftLastAccessTime = nullptr;
692 FILETIME* pftLastWriteTime = nullptr;
693 if (liCreationTime.QuadPart != 0)
694 {
695 ftCreationTime.dwHighDateTime = liCreationTime.u.HighPart;
696 ftCreationTime.dwLowDateTime = liCreationTime.u.LowPart;
697 pftCreationTime = &ftCreationTime;
698 }
699
700 if (liLastAccessTime.QuadPart != 0)
701 {
702 ftLastAccessTime.dwHighDateTime = liLastAccessTime.u.HighPart;
703 ftLastAccessTime.dwLowDateTime = liLastAccessTime.u.LowPart;
704 pftLastAccessTime = &ftLastAccessTime;
705 }
706
707 if (liLastWriteTime.QuadPart != 0)
708 {
709 ftLastWriteTime.dwHighDateTime = liLastWriteTime.u.HighPart;
710 ftLastWriteTime.dwLowDateTime = liLastWriteTime.u.LowPart;
711 pftLastWriteTime = &ftLastWriteTime;
712 }
713
714 if (liChangeTime.QuadPart != 0 && liChangeTime.QuadPart > liLastWriteTime.QuadPart)
715 {
716 ftLastWriteTime.dwHighDateTime = liChangeTime.u.HighPart;
717 ftLastWriteTime.dwLowDateTime = liChangeTime.u.LowPart;
718 pftLastWriteTime = &ftLastWriteTime;
719 }
720
721 DEBUG_WSTR("SetFileTime %s", file->fullpath);
722
723 if (!SetFileAttributesW(file->fullpath, FileAttributes))
724 {
725 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
726 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
727 WLog_ERR(TAG, "Unable to set file attributes for %s", fullpath);
728 return FALSE;
729 }
730
731 if (!SetFileTime(file->file_handle, pftCreationTime, pftLastAccessTime, pftLastWriteTime))
732 {
733 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
734 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
735 WLog_ERR(TAG, "Unable to set file time for %s", fullpath);
736 return FALSE;
737 }
738 return TRUE;
739}
740
741WINPR_ATTR_NODISCARD
742static BOOL drive_file_set_alloc_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
743{
744 WINPR_ASSERT(file);
745 const uint32_t expect = 8;
746 if (Length != expect)
747 {
748 WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect);
749 return FALSE;
750 }
751
752 /* http://msdn.microsoft.com/en-us/library/cc232076.aspx */
753 const int64_t size = Stream_Get_INT64(input);
754
755 if (file->file_handle == INVALID_HANDLE_VALUE)
756 {
757 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
758 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
759 WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size,
760 GetLastError());
761 return FALSE;
762 }
763
764 LARGE_INTEGER liSize = { .QuadPart = size };
765
766 if (!SetFilePointerEx(file->file_handle, liSize, nullptr, FILE_BEGIN))
767 {
768 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
769 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
770 WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size,
771 GetLastError());
772 return FALSE;
773 }
774
775 DEBUG_WSTR("Truncate %s", file->fullpath);
776
777 if (SetEndOfFile(file->file_handle) == 0)
778 {
779 char fullpath[MAX_PATH] = WINPR_C_ARRAY_INIT;
780 (void)ConvertWCharToUtf8(file->fullpath, fullpath, sizeof(fullpath));
781 WLog_ERR(TAG, "Unable to truncate %s to %" PRId64 " (%" PRIu32 ")", fullpath, size,
782 GetLastError());
783 return FALSE;
784 }
785
786 return TRUE;
787}
788
789WINPR_ATTR_NODISCARD
790static BOOL drive_file_set_disposition_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
791{
792 WINPR_ASSERT(file);
793 uint8_t delete_pending = 0;
794 /* http://msdn.microsoft.com/en-us/library/cc232098.aspx */
795 /* http://msdn.microsoft.com/en-us/library/cc241371.aspx */
796 if (file->is_dir && !PathIsDirectoryEmptyW(file->fullpath))
797 {
798 SetLastError(ERROR_DIR_NOT_EMPTY);
799 return FALSE;
800 }
801
802 if (Length)
803 {
804 const uint32_t expect = 1;
805 if (Length != expect)
806 WLog_DBG(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu32, Length, expect);
807
808 delete_pending = Stream_Get_UINT8(input);
809 }
810 else
811 delete_pending = 1;
812
813 if (delete_pending)
814 {
815 DEBUG_WSTR("SetDeletePending %s", file->fullpath);
816 const uint32_t attr = GetFileAttributesW(file->fullpath);
817
818 if (attr & FILE_ATTRIBUTE_READONLY)
819 {
820 SetLastError(ERROR_ACCESS_DENIED);
821 return FALSE;
822 }
823 }
824
825 file->delete_pending = delete_pending;
826 return TRUE;
827}
828
829WINPR_ATTR_NODISCARD
830static BOOL drive_file_set_rename_information(DRIVE_FILE* file, UINT32 Length, wStream* input)
831{
832 WINPR_ASSERT(file);
833
834 const uint32_t expect = 6;
835 if (Length < expect)
836 {
837 WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected at least %" PRIu32, Length, expect);
838 return FALSE;
839 }
840
841 /* http://msdn.microsoft.com/en-us/library/cc232085.aspx */
842 const uint8_t ReplaceIfExists = Stream_Get_UINT8(input);
843 Stream_Seek_UINT8(input); /* RootDirectory */
844 const uint64_t FileNameLength = Stream_Get_UINT32(input);
845
846 if (Length != expect + FileNameLength)
847 {
848 WLog_WARN(TAG, "Unexpected Length=%" PRIu32 ", expected %" PRIu64, Length,
849 expect + FileNameLength);
850 return FALSE;
851 }
852
853 WCHAR* fullpath = drive_file_combine_fullpath(file->basepath, Stream_ConstPointer(input),
854 FileNameLength / sizeof(WCHAR));
855
856 if (!fullpath)
857 return FALSE;
858
859#ifdef _WIN32
860
861 if (file->file_handle != INVALID_HANDLE_VALUE)
862 {
863 (void)CloseHandle(file->file_handle);
864 file->file_handle = INVALID_HANDLE_VALUE;
865 }
866
867#endif
868 DEBUG_WSTR("MoveFileExW %s", file->fullpath);
869
870 if (MoveFileExW(file->fullpath, fullpath,
871 MOVEFILE_COPY_ALLOWED | (ReplaceIfExists ? MOVEFILE_REPLACE_EXISTING : 0)))
872 {
873 const BOOL rc = drive_file_set_fullpath(file, fullpath);
874 free(fullpath);
875 if (!rc)
876 return FALSE;
877 }
878 else
879 {
880 free(fullpath);
881 return FALSE;
882 }
883
884#ifdef _WIN32
885 drive_file_init(file);
886#endif
887 return TRUE;
888}
889
890BOOL drive_file_set_information(DRIVE_FILE* file, UINT32 FsInformationClass, UINT32 Length,
891 wStream* input)
892{
893 if (!file || !input)
894 return FALSE;
895
896 if (!Stream_CheckAndLogRequiredLength(TAG, input, Length))
897 return FALSE;
898
899 switch (FsInformationClass)
900 {
901 case FileBasicInformation:
902 return drive_file_set_basic_information(file, Length, input);
903
904 case FileEndOfFileInformation:
905 /* http://msdn.microsoft.com/en-us/library/cc232067.aspx */
906 case FileAllocationInformation:
907 return drive_file_set_alloc_information(file, Length, input);
908
909 case FileDispositionInformation:
910 return drive_file_set_disposition_information(file, Length, input);
911
912 case FileRenameInformation:
913 return drive_file_set_rename_information(file, Length, input);
914
915 default:
916 WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
917 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
918 return FALSE;
919 }
920
921 return TRUE;
922}
923
924WINPR_ATTR_NODISCARD
925static BOOL drive_file_query_dir_info(DRIVE_FILE* file, wStream* output, size_t length)
926{
927 WINPR_ASSERT(file);
928 WINPR_ASSERT(output);
929
930 /* http://msdn.microsoft.com/en-us/library/cc232097.aspx */
931 if (!Stream_EnsureRemainingCapacity(output, 4 + 64 + length))
932 return FALSE;
933
934 if (length > UINT32_MAX - 64)
935 return FALSE;
936
937 Stream_Write_UINT32(output, (UINT32)(64 + length)); /* Length */
938 Stream_Write_UINT32(output, 0); /* NextEntryOffset */
939 Stream_Write_UINT32(output, 0); /* FileIndex */
940 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */
941 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */
942 Stream_Write_UINT32(output,
943 file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
944 Stream_Write_UINT32(output,
945 file->find_data.ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
946 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
947 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
948 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* ChangeTime */
949 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */
950 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* EndOfFile */
951 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* EndOfFile */
952 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* AllocationSize */
953 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* AllocationSize */
954 Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */
955 Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */
956 Stream_Write(output, file->find_data.cFileName, length);
957 return TRUE;
958}
959
960WINPR_ATTR_NODISCARD
961static BOOL drive_file_query_full_dir_info(DRIVE_FILE* file, wStream* output, size_t length)
962{
963 WINPR_ASSERT(file);
964 WINPR_ASSERT(output);
965 /* http://msdn.microsoft.com/en-us/library/cc232068.aspx */
966 if (!Stream_EnsureRemainingCapacity(output, 4 + 68 + length))
967 return FALSE;
968
969 if (length > UINT32_MAX - 68)
970 return FALSE;
971
972 Stream_Write_UINT32(output, (UINT32)(68 + length)); /* Length */
973 Stream_Write_UINT32(output, 0); /* NextEntryOffset */
974 Stream_Write_UINT32(output, 0); /* FileIndex */
975 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */
976 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */
977 Stream_Write_UINT32(output,
978 file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
979 Stream_Write_UINT32(output,
980 file->find_data.ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
981 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
982 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
983 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* ChangeTime */
984 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */
985 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* EndOfFile */
986 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* EndOfFile */
987 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* AllocationSize */
988 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* AllocationSize */
989 Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */
990 Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */
991 Stream_Write_UINT32(output, 0); /* EaSize */
992 Stream_Write(output, file->find_data.cFileName, length);
993 return TRUE;
994}
995
996WINPR_ATTR_NODISCARD
997static BOOL drive_file_query_both_dir_info(DRIVE_FILE* file, wStream* output, size_t length)
998{
999 WINPR_ASSERT(file);
1000 WINPR_ASSERT(output);
1001 /* http://msdn.microsoft.com/en-us/library/cc232095.aspx */
1002 if (!Stream_EnsureRemainingCapacity(output, 4 + 93 + length))
1003 return FALSE;
1004
1005 if (length > UINT32_MAX - 93)
1006 return FALSE;
1007
1008 Stream_Write_UINT32(output, (UINT32)(93 + length)); /* Length */
1009 Stream_Write_UINT32(output, 0); /* NextEntryOffset */
1010 Stream_Write_UINT32(output, 0); /* FileIndex */
1011 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwLowDateTime); /* CreationTime */
1012 Stream_Write_UINT32(output, file->find_data.ftCreationTime.dwHighDateTime); /* CreationTime */
1013 Stream_Write_UINT32(output,
1014 file->find_data.ftLastAccessTime.dwLowDateTime); /* LastAccessTime */
1015 Stream_Write_UINT32(output,
1016 file->find_data.ftLastAccessTime.dwHighDateTime); /* LastAccessTime */
1017 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* LastWriteTime */
1018 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* LastWriteTime */
1019 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwLowDateTime); /* ChangeTime */
1020 Stream_Write_UINT32(output, file->find_data.ftLastWriteTime.dwHighDateTime); /* ChangeTime */
1021 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* EndOfFile */
1022 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* EndOfFile */
1023 Stream_Write_UINT32(output, file->find_data.nFileSizeLow); /* AllocationSize */
1024 Stream_Write_UINT32(output, file->find_data.nFileSizeHigh); /* AllocationSize */
1025 Stream_Write_UINT32(output, file->find_data.dwFileAttributes); /* FileAttributes */
1026 Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */
1027 Stream_Write_UINT32(output, 0); /* EaSize */
1028 Stream_Write_UINT8(output, 0); /* ShortNameLength */
1029 /* Reserved(1), MUST NOT be added! */
1030 Stream_Zero(output, 24); /* ShortName */
1031 Stream_Write(output, file->find_data.cFileName, length);
1032 return TRUE;
1033}
1034
1035WINPR_ATTR_NODISCARD
1036static BOOL drive_file_query_names_info(DRIVE_FILE* file, wStream* output, size_t length)
1037{
1038 WINPR_ASSERT(file);
1039 WINPR_ASSERT(output);
1040 /* http://msdn.microsoft.com/en-us/library/cc232077.aspx */
1041 if (!Stream_EnsureRemainingCapacity(output, 4 + 12 + length))
1042 return FALSE;
1043
1044 if (length > UINT32_MAX - 12)
1045 return FALSE;
1046
1047 Stream_Write_UINT32(output, (UINT32)(12 + length)); /* Length */
1048 Stream_Write_UINT32(output, 0); /* NextEntryOffset */
1049 Stream_Write_UINT32(output, 0); /* FileIndex */
1050 Stream_Write_UINT32(output, (UINT32)length); /* FileNameLength */
1051 Stream_Write(output, file->find_data.cFileName, length);
1052 return TRUE;
1053}
1054
1055BOOL drive_file_query_directory(DRIVE_FILE* file, UINT32 FsInformationClass, BYTE InitialQuery,
1056 const WCHAR* path, UINT32 PathWCharLength, wStream* output)
1057{
1058 BOOL rc = FALSE;
1059 size_t length = 0;
1060 WCHAR* ent_path = nullptr;
1061
1062 if (!file || !path || !output)
1063 return FALSE;
1064
1065 if (InitialQuery != 0)
1066 {
1067 /* release search handle */
1068 if (file->find_handle != INVALID_HANDLE_VALUE)
1069 FindClose(file->find_handle);
1070
1071 ent_path = drive_file_combine_fullpath(file->basepath, path, PathWCharLength);
1072 /* open new search handle and retrieve the first entry */
1073 file->find_handle = FindFirstFileW(ent_path, &file->find_data);
1074 free(ent_path);
1075
1076 if (file->find_handle == INVALID_HANDLE_VALUE)
1077 goto out_fail;
1078 }
1079 else if (!FindNextFileW(file->find_handle, &file->find_data))
1080 goto out_fail;
1081
1082 length = _wcslen(file->find_data.cFileName) * sizeof(WCHAR);
1083
1084 switch (FsInformationClass)
1085 {
1086 case FileDirectoryInformation:
1087 rc = drive_file_query_dir_info(file, output, length);
1088 break;
1089
1090 case FileFullDirectoryInformation:
1091 rc = drive_file_query_full_dir_info(file, output, length);
1092 break;
1093
1094 case FileBothDirectoryInformation:
1095 rc = drive_file_query_both_dir_info(file, output, length);
1096 break;
1097
1098 case FileNamesInformation:
1099 rc = drive_file_query_names_info(file, output, length);
1100 break;
1101
1102 default:
1103 WLog_WARN(TAG, "Unhandled FSInformationClass %s [0x%08" PRIx32 "]",
1104 FSInformationClass2Tag(FsInformationClass), FsInformationClass);
1105 /* Unhandled FsInformationClass */
1106 goto out_fail;
1107 }
1108
1109out_fail:
1110 if (!rc)
1111 {
1112 Stream_Write_UINT32(output, 0); /* Length */
1113 Stream_Write_UINT8(output, 0); /* Padding */
1114 }
1115 return rc;
1116}