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