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