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