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