]> wimlib.net Git - wimlib/blob - src/win32_capture.c
Windows: Use ntdll unconditionally
[wimlib] / src / win32_capture.c
1 /*
2  * win32_capture.c - Windows-specific code for capturing files into a WIM image.
3  */
4
5 /*
6  * Copyright (C) 2013, 2014 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef __WIN32__
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib/win32_common.h"
31
32 #include "wimlib/capture.h"
33 #include "wimlib/dentry.h"
34 #include "wimlib/encoding.h"
35 #include "wimlib/endianness.h"
36 #include "wimlib/error.h"
37 #include "wimlib/lookup_table.h"
38 #include "wimlib/paths.h"
39 #include "wimlib/reparse.h"
40
41 #define MAX_GET_SD_ACCESS_DENIED_WARNINGS 1
42 #define MAX_GET_SACL_PRIV_NOTHELD_WARNINGS 1
43 #define MAX_CAPTURE_LONG_PATH_WARNINGS 5
44
45 struct win32_capture_state {
46         unsigned long num_get_sd_access_denied;
47         unsigned long num_get_sacl_priv_notheld;
48         unsigned long num_long_path_warnings;
49 };
50
51
52 int
53 read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
54                        u64 size,
55                        consume_data_callback_t cb,
56                        void *cb_ctx)
57 {
58         int ret = 0;
59         u64 bytes_remaining;
60         u8 buf[BUFFER_SIZE];
61
62         HANDLE hFile = win32_open_existing_file(lte->file_on_disk,
63                                                 FILE_READ_DATA);
64         if (hFile == INVALID_HANDLE_VALUE) {
65                 set_errno_from_GetLastError();
66                 ERROR_WITH_ERRNO("Failed to open \"%ls\"", lte->file_on_disk);
67                 return WIMLIB_ERR_OPEN;
68         }
69
70         bytes_remaining = size;
71         while (bytes_remaining) {
72                 DWORD bytesToRead, bytesRead;
73
74                 bytesToRead = min(sizeof(buf), bytes_remaining);
75                 if (!ReadFile(hFile, buf, bytesToRead, &bytesRead, NULL) ||
76                     bytesRead != bytesToRead)
77                 {
78                         set_errno_from_GetLastError();
79                         ERROR_WITH_ERRNO("Failed to read data from \"%ls\"",
80                                          lte->file_on_disk);
81                         ret = WIMLIB_ERR_READ;
82                         break;
83                 }
84                 bytes_remaining -= bytesRead;
85                 ret = (*cb)(buf, bytesRead, cb_ctx);
86                 if (ret)
87                         break;
88         }
89         CloseHandle(hFile);
90         return ret;
91 }
92
93 struct win32_encrypted_read_ctx {
94         consume_data_callback_t read_prefix_cb;
95         void *read_prefix_ctx;
96         int wimlib_err_code;
97         u64 bytes_remaining;
98 };
99
100 static DWORD WINAPI
101 win32_encrypted_export_cb(unsigned char *data, void *_ctx, unsigned long len)
102 {
103         struct win32_encrypted_read_ctx *ctx = _ctx;
104         int ret;
105         size_t bytes_to_consume = min(len, ctx->bytes_remaining);
106
107         if (bytes_to_consume == 0)
108                 return ERROR_SUCCESS;
109
110         ret = (*ctx->read_prefix_cb)(data, bytes_to_consume, ctx->read_prefix_ctx);
111         if (ret) {
112                 ctx->wimlib_err_code = ret;
113                 /* Shouldn't matter what error code is returned here, as long as
114                  * it isn't ERROR_SUCCESS.  */
115                 return ERROR_READ_FAULT;
116         }
117         ctx->bytes_remaining -= bytes_to_consume;
118         return ERROR_SUCCESS;
119 }
120
121 int
122 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
123                                  u64 size,
124                                  consume_data_callback_t cb,
125                                  void *cb_ctx)
126 {
127         struct win32_encrypted_read_ctx export_ctx;
128         DWORD err;
129         void *file_ctx;
130         int ret;
131
132         DEBUG("Reading %"PRIu64" bytes from encrypted file \"%ls\"",
133               size, lte->file_on_disk);
134
135         export_ctx.read_prefix_cb = cb;
136         export_ctx.read_prefix_ctx = cb_ctx;
137         export_ctx.wimlib_err_code = 0;
138         export_ctx.bytes_remaining = size;
139
140         err = OpenEncryptedFileRaw(lte->file_on_disk, 0, &file_ctx);
141         if (err != ERROR_SUCCESS) {
142                 set_errno_from_win32_error(err);
143                 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
144                                  "for raw read", lte->file_on_disk);
145                 return WIMLIB_ERR_OPEN;
146         }
147         err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
148                                    &export_ctx, file_ctx);
149         if (err != ERROR_SUCCESS) {
150                 set_errno_from_win32_error(err);
151                 ERROR_WITH_ERRNO("Failed to read encrypted file \"%ls\"",
152                                  lte->file_on_disk);
153                 ret = export_ctx.wimlib_err_code;
154                 if (ret == 0)
155                         ret = WIMLIB_ERR_READ;
156         } else if (export_ctx.bytes_remaining != 0) {
157                 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
158                       "encryted file \"%ls\"",
159                       size - export_ctx.bytes_remaining, size,
160                       lte->file_on_disk);
161                 ret = WIMLIB_ERR_READ;
162         } else {
163                 ret = 0;
164         }
165         CloseEncryptedFileRaw(file_ctx);
166         return ret;
167 }
168
169
170 static u64
171 FILETIME_to_u64(const FILETIME *ft)
172 {
173         return ((u64)ft->dwHighDateTime << 32) | (u64)ft->dwLowDateTime;
174 }
175
176 /* Load the short name of a file into a WIM dentry.
177  *
178  * If we can't read the short filename for some reason, we just ignore the error
179  * and assume the file has no short name.  This shouldn't be an issue, since the
180  * short names are essentially obsolete anyway.
181  */
182 static int
183 win32_get_short_name(HANDLE hFile, struct wim_dentry *dentry)
184 {
185
186         /* It's not any harder to just make the NtQueryInformationFile() system
187          * call ourselves, and it saves a dumb call to FindFirstFile() which of
188          * course has to create its own handle.  */
189         NTSTATUS status;
190         IO_STATUS_BLOCK io_status;
191         u8 buf[128] _aligned_attribute(8);
192         const FILE_NAME_INFORMATION *info;
193
194         status = (*func_NtQueryInformationFile)(hFile, &io_status, buf, sizeof(buf),
195                                                 FileAlternateNameInformation);
196         info = (const FILE_NAME_INFORMATION*)buf;
197         if (NT_SUCCESS(status) && info->FileNameLength != 0) {
198                 dentry->short_name = utf16le_dupz(info->FileName,
199                                                   info->FileNameLength);
200                 if (!dentry->short_name)
201                         return WIMLIB_ERR_NOMEM;
202                 dentry->short_name_nbytes = info->FileNameLength;
203         }
204         return 0;
205 }
206
207 static int
208 win32_get_security_descriptor(HANDLE hFile,
209                               const wchar_t *path,
210                               struct wim_inode *inode,
211                               struct wim_sd_set *sd_set,
212                               struct win32_capture_state *state,
213                               int add_flags)
214 {
215         SECURITY_INFORMATION requestedInformation;
216         u8 _buf[4096];
217         u8 *buf;
218         size_t bufsize;
219         DWORD lenNeeded;
220         NTSTATUS status;
221         int ret;
222
223         requestedInformation = DACL_SECURITY_INFORMATION |
224                                SACL_SECURITY_INFORMATION |
225                                OWNER_SECURITY_INFORMATION |
226                                GROUP_SECURITY_INFORMATION;
227         buf = _buf;
228         bufsize = sizeof(_buf);
229
230         /*
231          * We need the file's security descriptor in SECURITY_DESCRIPTOR_RELATIVE
232          * format, and we currently have a handle opened with as many relevant
233          * permissions as possible.  At this point, on Windows there are a number of
234          * options for reading a file's security descriptor:
235          *
236          * GetFileSecurity():  This takes in a path and returns the
237          * SECURITY_DESCRIPTOR_RELATIVE.  Problem: this uses an internal handle, not
238          * ours, and the handle created internally doesn't specify
239          * FILE_FLAG_BACKUP_SEMANTICS.  Therefore there can be access denied errors on
240          * some files and directories, even when running as the Administrator.
241          *
242          * GetSecurityInfo():  This takes in a handle and returns the security
243          * descriptor split into a bunch of different parts.  This should work, but it's
244          * dumb because we have to put the security descriptor back together again.
245          *
246          * BackupRead():  This can read the security descriptor, but this is a
247          * difficult-to-use API, probably only works as the Administrator, and the
248          * format of the returned data is not well documented.
249          *
250          * NtQuerySecurityObject():  This is exactly what we need, as it takes in a
251          * handle and returns the security descriptor in SECURITY_DESCRIPTOR_RELATIVE
252          * format.  Only problem is that it's a ntdll function and therefore not
253          * officially part of the Win32 API.  Oh well.
254          */
255         while (!(NT_SUCCESS(status = (*func_NtQuerySecurityObject)(hFile,
256                                                                    requestedInformation,
257                                                                    (PSECURITY_DESCRIPTOR)buf,
258                                                                    bufsize,
259                                                                    &lenNeeded))))
260         {
261                 switch (status) {
262                 case STATUS_BUFFER_OVERFLOW:
263                         wimlib_assert(buf == _buf);
264                         buf = MALLOC(lenNeeded);
265                         if (!buf)
266                                 return WIMLIB_ERR_NOMEM;
267                         bufsize = lenNeeded;
268                         break;
269                 case STATUS_PRIVILEGE_NOT_HELD:
270                 case STATUS_ACCESS_DENIED:
271                         if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS) {
272                 default:
273                                 set_errno_from_nt_status(status);
274                                 ERROR_WITH_ERRNO("\"%ls\": Failed to "
275                                                  "read security descriptor", path);
276                                 ret = WIMLIB_ERR_READ;
277                                 goto out_free_buf;
278                         }
279                         if (requestedInformation & SACL_SECURITY_INFORMATION) {
280                                 state->num_get_sacl_priv_notheld++;
281                                 requestedInformation &= ~SACL_SECURITY_INFORMATION;
282                                 break;
283                         }
284                         state->num_get_sd_access_denied++;
285                         ret = 0;
286                         goto out_free_buf;
287                 }
288         }
289
290         inode->i_security_id = sd_set_add_sd(sd_set, buf, lenNeeded);
291         if (inode->i_security_id < 0)
292                 ret = WIMLIB_ERR_NOMEM;
293         else
294                 ret = 0;
295 out_free_buf:
296         if (buf != _buf)
297                 FREE(buf);
298         return ret;
299 }
300
301 static int
302 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
303                                   wchar_t *path,
304                                   size_t path_num_chars,
305                                   struct add_image_params *params,
306                                   struct win32_capture_state *state,
307                                   unsigned vol_flags);
308
309 /* Reads the directory entries of directory and recursively calls
310  * win32_build_dentry_tree() on them.  */
311 static int
312 win32_recurse_directory(HANDLE hDir,
313                         wchar_t *dir_path,
314                         size_t dir_path_num_chars,
315                         struct wim_dentry *root,
316                         struct add_image_params *params,
317                         struct win32_capture_state *state,
318                         unsigned vol_flags)
319 {
320         int ret;
321
322         DEBUG("Recurse to directory \"%ls\"", dir_path);
323
324         /* Using NtQueryDirectoryFile() we can re-use the same open handle,
325          * which we opened with FILE_FLAG_BACKUP_SEMANTICS.  */
326
327         NTSTATUS status;
328         IO_STATUS_BLOCK io_status;
329         const size_t bufsize = 8192;
330         void *buf;
331
332         buf = MALLOC(bufsize);
333         if (!buf)
334                 return WIMLIB_ERR_NOMEM;
335
336         while (NT_SUCCESS(status = (*func_NtQueryDirectoryFile)(hDir, NULL, NULL, NULL,
337                                                                 &io_status, buf, bufsize,
338                                                                 FileNamesInformation,
339                                                                 FALSE, NULL, FALSE)))
340         {
341                 const FILE_NAMES_INFORMATION *info = buf;
342                 for (;;) {
343                         if (!(info->FileNameLength == 2 && info->FileName[0] == L'.') &&
344                             !(info->FileNameLength == 4 && info->FileName[0] == L'.' &&
345                                                            info->FileName[1] == L'.'))
346                         {
347                                 wchar_t *p;
348                                 struct wim_dentry *child;
349
350                                 p = dir_path + dir_path_num_chars;
351                                 *p++ = L'\\';
352                                 p = wmempcpy(p, info->FileName,
353                                              info->FileNameLength / 2);
354                                 *p = '\0';
355
356                                 ret = win32_build_dentry_tree_recursive(
357                                                                 &child,
358                                                                 dir_path,
359                                                                 p - dir_path,
360                                                                 params,
361                                                                 state,
362                                                                 vol_flags);
363
364                                 dir_path[dir_path_num_chars] = L'\0';
365
366                                 if (ret)
367                                         goto out_free_buf;
368                                 if (child)
369                                         dentry_add_child(root, child);
370                         }
371                         if (info->NextEntryOffset == 0)
372                                 break;
373                         info = (const FILE_NAMES_INFORMATION *)
374                                         ((const u8 *)info + info->NextEntryOffset);
375                 }
376         }
377
378         if (status != STATUS_NO_MORE_FILES) {
379                 set_errno_from_nt_status(status);
380                 ERROR_WITH_ERRNO("Failed to read directory \"%ls\"", dir_path);
381                 ret = WIMLIB_ERR_READ;
382         }
383 out_free_buf:
384         FREE(buf);
385         return ret;
386 }
387
388 /* Reparse point fixup status code */
389 enum rp_status {
390         /* Reparse point corresponded to an absolute symbolic link or junction
391          * point that pointed outside the directory tree being captured, and
392          * therefore was excluded. */
393         RP_EXCLUDED       = 0x0,
394
395         /* Reparse point was not fixed as it was either a relative symbolic
396          * link, a mount point, or something else we could not understand. */
397         RP_NOT_FIXED      = 0x1,
398
399         /* Reparse point corresponded to an absolute symbolic link or junction
400          * point that pointed inside the directory tree being captured, where
401          * the target was specified by a "full" \??\ prefixed path, and
402          * therefore was fixed to be relative to the root of the directory tree
403          * being captured. */
404         RP_FIXED_FULLPATH = 0x2,
405
406         /* Same as RP_FIXED_FULLPATH, except the absolute link target did not
407          * have the \??\ prefix.  It may have begun with a drive letter though.
408          * */
409         RP_FIXED_ABSPATH  = 0x4,
410
411         /* Either RP_FIXED_FULLPATH or RP_FIXED_ABSPATH. */
412         RP_FIXED          = RP_FIXED_FULLPATH | RP_FIXED_ABSPATH,
413 };
414
415 /* Given the "substitute name" target of a Windows reparse point, try doing a
416  * fixup where we change it to be absolute relative to the root of the directory
417  * tree being captured.
418  *
419  * Note that this is only executed when WIMLIB_ADD_FLAG_RPFIX has been
420  * set.
421  *
422  * @capture_root_ino and @capture_root_dev indicate the inode number and device
423  * of the root of the directory tree being captured.  They are meant to identify
424  * this directory (as an alternative to its actual path, which could potentially
425  * be reached via multiple destinations due to other symbolic links).  This may
426  * not work properly on FAT, which doesn't seem to supply proper inode numbers
427  * or file IDs.  However, FAT doesn't support reparse points so this function
428  * wouldn't even be called anyway.
429  */
430 static enum rp_status
431 win32_capture_maybe_rpfix_target(wchar_t *target, u16 *target_nbytes_p,
432                                  u64 capture_root_ino, u64 capture_root_dev,
433                                  u32 rptag)
434 {
435         u16 target_nchars = *target_nbytes_p / 2;
436         size_t stripped_chars;
437         wchar_t *orig_target;
438         int ret;
439
440         ret = parse_substitute_name(target, *target_nbytes_p, rptag);
441         if (ret < 0)
442                 return RP_NOT_FIXED;
443         stripped_chars = ret;
444         if (stripped_chars)
445                 stripped_chars -= 2;
446         target[target_nchars] = L'\0';
447         orig_target = target;
448         target = capture_fixup_absolute_symlink(target + stripped_chars,
449                                                 capture_root_ino, capture_root_dev);
450         if (!target)
451                 return RP_EXCLUDED;
452         target_nchars = wcslen(target);
453         wmemmove(orig_target + stripped_chars, target, target_nchars + 1);
454         *target_nbytes_p = (target_nchars + stripped_chars) * sizeof(wchar_t);
455         DEBUG("Fixed reparse point (new target: \"%ls\")", orig_target);
456         if (stripped_chars)
457                 return RP_FIXED_FULLPATH;
458         else
459                 return RP_FIXED_ABSPATH;
460 }
461
462 /* Returns: `enum rp_status' value on success; negative WIMLIB_ERR_* value on
463  * failure. */
464 static int
465 win32_capture_try_rpfix(u8 *rpbuf, u16 *rpbuflen_p,
466                         u64 capture_root_ino, u64 capture_root_dev,
467                         const wchar_t *path, struct add_image_params *params)
468 {
469         struct reparse_data rpdata;
470         int ret;
471         enum rp_status rp_status;
472
473         ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
474         if (ret)
475                 return -ret;
476
477         rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
478                                                      &rpdata.substitute_name_nbytes,
479                                                      capture_root_ino,
480                                                      capture_root_dev,
481                                                      le32_to_cpu(*(le32*)rpbuf));
482         if (rp_status & RP_FIXED) {
483                 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
484                 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
485                 wmemcpy(substitute_name_copy, rpdata.substitute_name,
486                         rpdata.substitute_name_nbytes / 2);
487                 rpdata.substitute_name = substitute_name_copy;
488                 rpdata.print_name = substitute_name_copy;
489                 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
490                 if (rp_status == RP_FIXED_FULLPATH) {
491                         /* "full path", meaning \??\ prefixed.  We should not
492                          * include this prefix in the print name, as it is
493                          * apparently meant for the filesystem driver only. */
494                         rpdata.print_name += 4;
495                         rpdata.print_name_nbytes -= 8;
496                 }
497                 ret = make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
498                 if (ret == 0)
499                         ret = rp_status;
500                 else
501                         ret = -ret;
502         } else {
503                 if (rp_status == RP_EXCLUDED) {
504                         /* Ignoring absolute symbolic link or junction point
505                          * that points out of the tree to be captured.  */
506                         size_t print_name_nchars = rpdata.print_name_nbytes / 2;
507                         wchar_t print_name0[print_name_nchars + 1];
508                         print_name0[print_name_nchars] = L'\0';
509                         wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
510
511                         params->progress.scan.cur_path = path;
512                         params->progress.scan.symlink_target = print_name0;
513                         do_capture_progress(params,
514                                             WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK,
515                                             NULL);
516                 }
517                 ret = rp_status;
518         }
519         return ret;
520 }
521
522 /*
523  * Loads the reparse point data from a reparse point into memory, optionally
524  * fixing the targets of absolute symbolic links and junction points to be
525  * relative to the root of capture.
526  *
527  * @hFile:  Open handle to the reparse point.
528  * @path:   Path to the reparse point.  Used for error messages only.
529  * @params: Additional parameters, including whether to do reparse point fixups
530  *          or not.
531  * @rpbuf:  Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
532  *          the reparse point buffer will be loaded.
533  * @rpbuflen_ret:  On success, the length of the reparse point buffer in bytes
534  *                 is written to this location.
535  *
536  * Returns:
537  *      On success, returns an `enum rp_status' value that indicates if and/or
538  *      how the reparse point fixup was done.
539  *
540  *      On failure, returns a negative value that is a negated WIMLIB_ERR_*
541  *      code.
542  */
543 static int
544 win32_get_reparse_data(HANDLE hFile, const wchar_t *path,
545                        struct add_image_params *params,
546                        u8 *rpbuf, u16 *rpbuflen_ret)
547 {
548         DWORD bytesReturned;
549         u32 reparse_tag;
550         int ret;
551         u16 rpbuflen;
552
553         DEBUG("Loading reparse data from \"%ls\"", path);
554         if (!DeviceIoControl(hFile, FSCTL_GET_REPARSE_POINT,
555                              NULL, /* "Not used with this operation; set to NULL" */
556                              0, /* "Not used with this operation; set to 0" */
557                              rpbuf, /* "A pointer to a buffer that
558                                                    receives the reparse point data */
559                              REPARSE_POINT_MAX_SIZE, /* "The size of the output
560                                                         buffer, in bytes */
561                              &bytesReturned,
562                              NULL))
563         {
564                 set_errno_from_GetLastError();
565                 ERROR_WITH_ERRNO("Failed to get reparse data of \"%ls\"", path);
566                 return -WIMLIB_ERR_READ;
567         }
568         if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
569                 ERROR("Reparse data on \"%ls\" is invalid", path);
570                 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
571         }
572
573         rpbuflen = bytesReturned;
574         reparse_tag = le32_to_cpu(*(le32*)rpbuf);
575         if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
576             (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
577              reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
578         {
579                 /* Try doing reparse point fixup */
580                 ret = win32_capture_try_rpfix(rpbuf,
581                                               &rpbuflen,
582                                               params->capture_root_ino,
583                                               params->capture_root_dev,
584                                               path,
585                                               params);
586         } else {
587                 ret = RP_NOT_FIXED;
588         }
589         *rpbuflen_ret = rpbuflen;
590         return ret;
591 }
592
593 static DWORD WINAPI
594 win32_tally_encrypted_size_cb(unsigned char *_data, void *_size_ret,
595                               unsigned long len)
596 {
597         *(u64*)_size_ret += len;
598         return ERROR_SUCCESS;
599 }
600
601 static int
602 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
603 {
604         DWORD err;
605         void *file_ctx;
606         int ret;
607
608         err = OpenEncryptedFileRaw(path, 0, &file_ctx);
609         if (err != ERROR_SUCCESS) {
610                 set_errno_from_win32_error(err);
611                 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
612                                  "for raw read", path);
613                 return WIMLIB_ERR_OPEN;
614         }
615         *size_ret = 0;
616         err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
617                                    size_ret, file_ctx);
618         if (err != ERROR_SUCCESS) {
619                 set_errno_from_win32_error(err);
620                 ERROR_WITH_ERRNO("Failed to read raw encrypted data from "
621                                  "\"%ls\"", path);
622                 ret = WIMLIB_ERR_READ;
623         } else {
624                 ret = 0;
625         }
626         CloseEncryptedFileRaw(file_ctx);
627         return ret;
628 }
629
630 /* Scans an unnamed or named stream of a Win32 file (not a reparse point
631  * stream); calculates its SHA1 message digest and either creates a `struct
632  * wim_lookup_table_entry' in memory for it, or uses an existing 'struct
633  * wim_lookup_table_entry' for an identical stream.
634  *
635  * @path:               Path to the file (UTF-16LE).
636  *
637  * @path_num_chars:     Number of 2-byte characters in @path.
638  *
639  * @inode:              WIM inode to save the stream into.
640  *
641  * @unhashed_streams:   List of unhashed streams that have been added to the WIM
642  *                      image.
643  *
644  * @dat:                A `WIN32_FIND_STREAM_DATA' structure that specifies the
645  *                      stream name.
646  *
647  * Returns 0 on success; nonzero on failure.
648  */
649 static int
650 win32_capture_stream(const wchar_t *path,
651                      size_t path_num_chars,
652                      struct wim_inode *inode,
653                      struct list_head *unhashed_streams,
654                      WIN32_FIND_STREAM_DATA *dat)
655 {
656         struct wim_ads_entry *ads_entry;
657         struct wim_lookup_table_entry *lte;
658         int ret;
659         wchar_t *stream_name, *colon;
660         size_t stream_name_nchars;
661         bool is_named_stream;
662         wchar_t *spath;
663         size_t spath_nchars;
664         size_t spath_buf_nbytes;
665         const wchar_t *relpath_prefix;
666         const wchar_t *colonchar;
667
668         DEBUG("Capture \"%ls\" stream \"%ls\"", path, dat->cStreamName);
669
670         /* The stream name should be returned as :NAME:TYPE */
671         stream_name = dat->cStreamName;
672         if (*stream_name != L':')
673                 goto out_invalid_stream_name;
674         stream_name += 1;
675         colon = wcschr(stream_name, L':');
676         if (colon == NULL)
677                 goto out_invalid_stream_name;
678
679         if (wcscmp(colon + 1, L"$DATA")) {
680                 /* Not a DATA stream */
681                 ret = 0;
682                 goto out;
683         }
684
685         *colon = '\0';
686
687         stream_name_nchars = colon - stream_name;
688         is_named_stream = (stream_name_nchars != 0);
689
690         if (is_named_stream) {
691                 /* Allocate an ADS entry for the named stream. */
692                 ads_entry = inode_add_ads_utf16le(inode, stream_name,
693                                                   stream_name_nchars * sizeof(wchar_t));
694                 if (!ads_entry) {
695                         ret = WIMLIB_ERR_NOMEM;
696                         goto out;
697                 }
698         }
699
700         /* If zero length stream, no lookup table entry needed. */
701         if ((u64)dat->StreamSize.QuadPart == 0) {
702                 ret = 0;
703                 goto out;
704         }
705
706         /* Create a UTF-16LE string @spath that gives the filename, then a
707          * colon, then the stream name.  Or, if it's an unnamed stream, just the
708          * filename.  It is MALLOC()'ed so that it can be saved in the
709          * wim_lookup_table_entry if needed.
710          *
711          * As yet another special case, relative paths need to be changed to
712          * begin with an explicit "./" so that, for example, a file t:ads, where
713          * :ads is the part we added, is not interpreted as a file on the t:
714          * drive. */
715         spath_nchars = path_num_chars;
716         relpath_prefix = L"";
717         colonchar = L"";
718         if (is_named_stream) {
719                 spath_nchars += 1 + stream_name_nchars;
720                 colonchar = L":";
721                 if (path_num_chars == 1 && !is_any_path_separator(path[0])) {
722                         spath_nchars += 2;
723                         static const wchar_t _relpath_prefix[] =
724                                 {L'.', OS_PREFERRED_PATH_SEPARATOR, L'\0'};
725                         relpath_prefix = _relpath_prefix;
726                 }
727         }
728
729         spath_buf_nbytes = (spath_nchars + 1) * sizeof(wchar_t);
730         spath = MALLOC(spath_buf_nbytes);
731
732         tsprintf(spath, L"%ls%ls%ls%ls",
733                  relpath_prefix, path, colonchar, stream_name);
734
735         /* Make a new wim_lookup_table_entry */
736         lte = new_lookup_table_entry();
737         if (!lte) {
738                 ret = WIMLIB_ERR_NOMEM;
739                 goto out_free_spath;
740         }
741         lte->file_on_disk = spath;
742         spath = NULL;
743         if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED && !is_named_stream) {
744                 u64 encrypted_size;
745                 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
746                 ret = win32_get_encrypted_file_size(path, &encrypted_size);
747                 if (ret)
748                         goto out_free_spath;
749                 lte->size = encrypted_size;
750         } else {
751                 lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
752                 lte->size = (u64)dat->StreamSize.QuadPart;
753         }
754
755         u32 stream_id;
756         if (is_named_stream) {
757                 stream_id = ads_entry->stream_id;
758                 ads_entry->lte = lte;
759         } else {
760                 stream_id = 0;
761                 inode->i_lte = lte;
762         }
763         add_unhashed_stream(lte, inode, stream_id, unhashed_streams);
764         ret = 0;
765 out_free_spath:
766         FREE(spath);
767 out:
768         return ret;
769 out_invalid_stream_name:
770         ERROR("Invalid stream name: \"%ls:%ls\"", path, dat->cStreamName);
771         ret = WIMLIB_ERR_READ;
772         goto out;
773 }
774
775 /* Load information about the streams of an open file into a WIM inode.
776  *
777  * We use the NtQueryInformationFile() system call instead of FindFirstStream()
778  * and FindNextStream().  This is done for two reasons:
779  *
780  * - FindFirstStream() opens its own handle to the file or directory and
781  *   apparently does so without specifying FILE_FLAG_BACKUP_SEMANTICS, thereby
782  *   causing access denied errors on certain files (even when running as the
783  *   Administrator).
784  * - FindFirstStream() and FindNextStream() is only available on Windows Vista
785  *   and later, whereas the stream support in NtQueryInformationFile() was
786  *   already present in Windows XP.
787  */
788 static int
789 win32_capture_streams(HANDLE *hFile_p,
790                       const wchar_t *path,
791                       size_t path_num_chars,
792                       struct wim_inode *inode,
793                       struct list_head *unhashed_streams,
794                       u64 file_size,
795                       unsigned vol_flags)
796 {
797         int ret;
798         u8 _buf[8192] _aligned_attribute(8);
799         u8 *buf;
800         size_t bufsize;
801         IO_STATUS_BLOCK io_status;
802         NTSTATUS status;
803         const FILE_STREAM_INFORMATION *info;
804
805         DEBUG("Capturing streams from \"%ls\"", path);
806
807         buf = _buf;
808         bufsize = sizeof(_buf);
809
810         if (!(vol_flags & FILE_NAMED_STREAMS))
811                 goto unnamed_only;
812
813         /* Get a buffer containing the stream information.  */
814         while (!NT_SUCCESS(status = (*func_NtQueryInformationFile)(*hFile_p,
815                                                                    &io_status,
816                                                                    buf,
817                                                                    bufsize,
818                                                                    FileStreamInformation)))
819         {
820
821                 switch (status) {
822                 case STATUS_BUFFER_OVERFLOW:
823                         {
824                                 u8 *newbuf;
825
826                                 bufsize *= 2;
827                                 if (buf == _buf)
828                                         newbuf = MALLOC(bufsize);
829                                 else
830                                         newbuf = REALLOC(buf, bufsize);
831                                 if (!newbuf) {
832                                         ret = WIMLIB_ERR_NOMEM;
833                                         goto out_free_buf;
834                                 }
835                                 buf = newbuf;
836                         }
837                         break;
838                 case STATUS_NOT_IMPLEMENTED:
839                 case STATUS_NOT_SUPPORTED:
840                 case STATUS_INVALID_INFO_CLASS:
841                         goto unnamed_only;
842                 default:
843                         set_errno_from_nt_status(status);
844                         ERROR_WITH_ERRNO("\"%ls\": Failed to query "
845                                          "stream information", path);
846                         ret = WIMLIB_ERR_READ;
847                         goto out_free_buf;
848                 }
849         }
850
851         if (io_status.Information == 0) {
852                 /* No stream information.  */
853                 ret = 0;
854                 goto out_free_buf;
855         }
856
857         if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
858                 /* OpenEncryptedFileRaw() seems to fail with
859                  * ERROR_SHARING_VIOLATION if there are any handles opened to
860                  * the file.  */
861                 CloseHandle(*hFile_p);
862                 *hFile_p = INVALID_HANDLE_VALUE;
863         }
864
865         /* Parse one or more stream information structures.  */
866         info = (const FILE_STREAM_INFORMATION *)buf;
867         for (;;) {
868                 WIN32_FIND_STREAM_DATA dat;
869
870                 if (info->StreamNameLength <= sizeof(dat.cStreamName) - 2) {
871                         dat.StreamSize = info->StreamSize;
872                         memcpy(dat.cStreamName, info->StreamName, info->StreamNameLength);
873                         dat.cStreamName[info->StreamNameLength / 2] = L'\0';
874
875                         /* Capture the stream.  */
876                         ret = win32_capture_stream(path, path_num_chars, inode,
877                                                    unhashed_streams, &dat);
878                         if (ret)
879                                 goto out_free_buf;
880                 }
881                 if (info->NextEntryOffset == 0) {
882                         /* No more stream information.  */
883                         break;
884                 }
885                 /* Advance to next stream information.  */
886                 info = (const FILE_STREAM_INFORMATION *)
887                                 ((const u8 *)info + info->NextEntryOffset);
888         }
889         ret = 0;
890         goto out_free_buf;
891
892 unnamed_only:
893         /* The volume does not support named streams.  Only capture the unnamed
894          * data stream. */
895         DEBUG("Only capturing unnamed data stream");
896         if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
897                                    FILE_ATTRIBUTE_REPARSE_POINT))
898         {
899                 ret = 0;
900                 goto out_free_buf;
901         }
902
903         {
904                 WIN32_FIND_STREAM_DATA dat;
905
906                 wcscpy(dat.cStreamName, L"::$DATA");
907                 dat.StreamSize.QuadPart = file_size;
908                 ret = win32_capture_stream(path, path_num_chars,
909                                            inode, unhashed_streams, &dat);
910         }
911 out_free_buf:
912         /* Free buffer if allocated on heap.  */
913         if (buf != _buf)
914                 FREE(buf);
915         return ret;
916 }
917
918 static int
919 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
920                                   wchar_t *path,
921                                   size_t path_num_chars,
922                                   struct add_image_params *params,
923                                   struct win32_capture_state *state,
924                                   unsigned vol_flags)
925 {
926         struct wim_dentry *root = NULL;
927         struct wim_inode *inode = NULL;
928         DWORD err;
929         u64 file_size;
930         int ret;
931         u8 *rpbuf;
932         u16 rpbuflen;
933         u16 not_rpfixed;
934         HANDLE hFile = INVALID_HANDLE_VALUE;
935         DWORD desiredAccess;
936
937
938         if (should_exclude_path(path + params->capture_root_nchars,
939                                 path_num_chars - params->capture_root_nchars,
940                                 params->config))
941         {
942                 ret = 0;
943                 goto out_progress;
944         }
945
946 #if 0
947         if (path_num_chars >= 4 &&
948             !wmemcmp(path, L"\\\\?\\", 4) &&
949             path_num_chars + 1 - 4 > MAX_PATH &&
950             state->num_long_path_warnings < MAX_CAPTURE_LONG_PATH_WARNINGS)
951         {
952                 WARNING("Path \"%ls\" exceeds MAX_PATH", path);
953                 if (++state->num_long_path_warnings == MAX_CAPTURE_LONG_PATH_WARNINGS)
954                         WARNING("Suppressing further warnings about long paths.");
955         }
956 #endif
957
958         desiredAccess = FILE_READ_DATA | FILE_READ_ATTRIBUTES |
959                         READ_CONTROL | ACCESS_SYSTEM_SECURITY;
960 again:
961         hFile = win32_open_existing_file(path, desiredAccess);
962         if (hFile == INVALID_HANDLE_VALUE) {
963                 err = GetLastError();
964                 if (err == ERROR_ACCESS_DENIED || err == ERROR_PRIVILEGE_NOT_HELD) {
965                         if (desiredAccess & ACCESS_SYSTEM_SECURITY) {
966                                 desiredAccess &= ~ACCESS_SYSTEM_SECURITY;
967                                 goto again;
968                         }
969                         if (desiredAccess & READ_CONTROL) {
970                                 desiredAccess &= ~READ_CONTROL;
971                                 goto again;
972                         }
973                 }
974                 set_errno_from_GetLastError();
975                 ERROR_WITH_ERRNO("Failed to open \"%ls\" for reading", path);
976                 ret = WIMLIB_ERR_OPEN;
977                 goto out;
978         }
979
980         BY_HANDLE_FILE_INFORMATION file_info;
981         if (!GetFileInformationByHandle(hFile, &file_info)) {
982                 set_errno_from_GetLastError();
983                 ERROR_WITH_ERRNO("Failed to get file information for \"%ls\"",
984                                  path);
985                 ret = WIMLIB_ERR_STAT;
986                 goto out;
987         }
988
989         if (file_info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
990                 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
991                 ret = win32_get_reparse_data(hFile, path, params,
992                                              rpbuf, &rpbuflen);
993                 if (ret < 0) {
994                         /* WIMLIB_ERR_* (inverted) */
995                         ret = -ret;
996                         goto out;
997                 } else if (ret & RP_FIXED) {
998                         not_rpfixed = 0;
999                 } else if (ret == RP_EXCLUDED) {
1000                         ret = 0;
1001                         goto out;
1002                 } else {
1003                         not_rpfixed = 1;
1004                 }
1005         }
1006
1007         /* Create a WIM dentry with an associated inode, which may be shared.
1008          *
1009          * However, we need to explicitly check for directories and files with
1010          * only 1 link and refuse to hard link them.  This is because Windows
1011          * has a bug where it can return duplicate File IDs for files and
1012          * directories on the FAT filesystem. */
1013         ret = inode_table_new_dentry(params->inode_table,
1014                                      path_basename_with_len(path, path_num_chars),
1015                                      ((u64)file_info.nFileIndexHigh << 32) |
1016                                          (u64)file_info.nFileIndexLow,
1017                                      file_info.dwVolumeSerialNumber,
1018                                      (file_info.nNumberOfLinks <= 1 ||
1019                                         (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)),
1020                                      &root);
1021         if (ret)
1022                 goto out;
1023
1024         ret = win32_get_short_name(hFile, root);
1025         if (ret)
1026                 goto out;
1027
1028         inode = root->d_inode;
1029
1030         if (inode->i_nlink > 1) {
1031                 /* Shared inode; nothing more to do */
1032                 goto out_progress;
1033         }
1034
1035         inode->i_attributes = file_info.dwFileAttributes;
1036         inode->i_creation_time = FILETIME_to_u64(&file_info.ftCreationTime);
1037         inode->i_last_write_time = FILETIME_to_u64(&file_info.ftLastWriteTime);
1038         inode->i_last_access_time = FILETIME_to_u64(&file_info.ftLastAccessTime);
1039         inode->i_resolved = 1;
1040
1041         params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
1042
1043         if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)
1044             && (vol_flags & FILE_PERSISTENT_ACLS))
1045         {
1046                 ret = win32_get_security_descriptor(hFile, path, inode,
1047                                                     params->sd_set, state,
1048                                                     params->add_flags);
1049                 if (ret)
1050                         goto out;
1051         }
1052
1053         file_size = ((u64)file_info.nFileSizeHigh << 32) |
1054                      (u64)file_info.nFileSizeLow;
1055
1056
1057         /* Capture the unnamed data stream (only should be present for regular
1058          * files) and any alternate data streams. */
1059         ret = win32_capture_streams(&hFile,
1060                                     path,
1061                                     path_num_chars,
1062                                     inode,
1063                                     params->unhashed_streams,
1064                                     file_size,
1065                                     vol_flags);
1066         if (ret)
1067                 goto out;
1068
1069         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1070                 /* Reparse point: set the reparse data (which we read already)
1071                  * */
1072                 inode->i_not_rpfixed = not_rpfixed;
1073                 inode->i_reparse_tag = le32_to_cpu(*(le32*)rpbuf);
1074                 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1075                                                params->lookup_table);
1076         } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1077                 /* Directory (not a reparse point) --- recurse to children */
1078
1079                 if (hFile == INVALID_HANDLE_VALUE) {
1080                         /* Re-open handle that was closed to read raw encrypted
1081                          * data.  */
1082                         hFile = win32_open_existing_file(path, FILE_READ_DATA);
1083                         if (hFile == INVALID_HANDLE_VALUE) {
1084                                 set_errno_from_GetLastError();
1085                                 ERROR_WITH_ERRNO("Failed to reopen \"%ls\"",
1086                                                  path);
1087                                 ret = WIMLIB_ERR_OPEN;
1088                                 goto out;
1089                         }
1090                 }
1091                 ret = win32_recurse_directory(hFile,
1092                                               path,
1093                                               path_num_chars,
1094                                               root,
1095                                               params,
1096                                               state,
1097                                               vol_flags);
1098         }
1099         if (ret)
1100                 goto out;
1101
1102         path[path_num_chars] = '\0';
1103 out_progress:
1104         params->progress.scan.cur_path = path;
1105         if (root == NULL)
1106                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
1107         else
1108                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
1109 out:
1110         if (hFile != INVALID_HANDLE_VALUE)
1111                 CloseHandle(hFile);
1112         if (ret == 0)
1113                 *root_ret = root;
1114         else
1115                 free_dentry_tree(root, params->lookup_table);
1116         return ret;
1117 }
1118
1119 static void
1120 win32_do_capture_warnings(const wchar_t *path,
1121                           const struct win32_capture_state *state,
1122                           int add_flags)
1123 {
1124         if (state->num_get_sacl_priv_notheld == 0 &&
1125             state->num_get_sd_access_denied == 0)
1126                 return;
1127
1128         WARNING("Scan of \"%ls\" complete, but with one or more warnings:", path);
1129         if (state->num_get_sacl_priv_notheld != 0) {
1130                 WARNING("- Could not capture SACL (System Access Control List)\n"
1131                         "            on %lu files or directories.",
1132                         state->num_get_sacl_priv_notheld);
1133         }
1134         if (state->num_get_sd_access_denied != 0) {
1135                 WARNING("- Could not capture security descriptor at all\n"
1136                         "            on %lu files or directories.",
1137                         state->num_get_sd_access_denied);
1138         }
1139         WARNING("To fully capture all security descriptors, run the program\n"
1140                 "          with Administrator rights.");
1141 }
1142
1143 #define WINDOWS_NT_MAX_PATH 32768
1144
1145 /* Win32 version of capturing a directory tree */
1146 int
1147 win32_build_dentry_tree(struct wim_dentry **root_ret,
1148                         const wchar_t *root_disk_path,
1149                         struct add_image_params *params)
1150 {
1151         size_t path_nchars;
1152         wchar_t *path;
1153         int ret;
1154         struct win32_capture_state state;
1155         unsigned vol_flags;
1156         DWORD dret;
1157
1158         path_nchars = wcslen(root_disk_path);
1159         if (path_nchars > WINDOWS_NT_MAX_PATH)
1160                 return WIMLIB_ERR_INVALID_PARAM;
1161
1162         ret = win32_get_file_and_vol_ids(root_disk_path,
1163                                          &params->capture_root_ino,
1164                                          &params->capture_root_dev);
1165         if (ret) {
1166                 ERROR_WITH_ERRNO("Can't open %ls", root_disk_path);
1167                 return ret;
1168         }
1169
1170         win32_get_vol_flags(root_disk_path, &vol_flags, NULL);
1171
1172         /* WARNING: There is no check for overflow later when this buffer is
1173          * being used!  But it's as long as the maximum path length understood
1174          * by Windows NT (which is NOT the same as MAX_PATH). */
1175         path = MALLOC((WINDOWS_NT_MAX_PATH + 1) * sizeof(wchar_t));
1176         if (path == NULL)
1177                 return WIMLIB_ERR_NOMEM;
1178
1179         /* Work around defective behavior in Windows where paths longer than 260
1180          * characters are not supported by default; instead they need to be
1181          * turned into absolute paths and prefixed with "\\?\".  */
1182
1183         if (wcsncmp(root_disk_path, L"\\\\?\\", 4)) {
1184                 dret = GetFullPathName(root_disk_path, WINDOWS_NT_MAX_PATH - 3,
1185                                        &path[4], NULL);
1186
1187                 if (dret == 0 || dret >= WINDOWS_NT_MAX_PATH - 3) {
1188                         WARNING("Can't get full path name for \"%ls\"", root_disk_path);
1189                         wmemcpy(path, root_disk_path, path_nchars + 1);
1190                 } else {
1191                         wmemcpy(path, L"\\\\?\\", 4);
1192                         path_nchars = 4 + dret;
1193                 }
1194         } else {
1195                 wmemcpy(path, root_disk_path, path_nchars + 1);
1196         }
1197
1198        /* Strip trailing slashes.  If we don't do this, we may create a path
1199         * with multiple consecutive backslashes, which for some reason causes
1200         * Windows to report that the file cannot be found.  */
1201         while (path_nchars >= 2 &&
1202                path[path_nchars - 1] == L'\\' &&
1203                path[path_nchars - 2] != L':')
1204         {
1205                 path[--path_nchars] = L'\0';
1206         }
1207
1208         params->capture_root_nchars = path_nchars;
1209
1210         memset(&state, 0, sizeof(state));
1211         ret = win32_build_dentry_tree_recursive(root_ret, path,
1212                                                 path_nchars, params,
1213                                                 &state, vol_flags);
1214         FREE(path);
1215         if (ret == 0)
1216                 win32_do_capture_warnings(root_disk_path, &state, params->add_flags);
1217         return ret;
1218 }
1219
1220 #endif /* __WIN32__ */