4 * Capture a WIM image from a NTFS volume. We capture everything we can,
5 * including security data and alternate data streams.
9 * Copyright (C) 2012 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib_internal.h"
33 #include "lookup_table.h"
35 #include <ntfs-3g/layout.h>
36 #include <ntfs-3g/acls.h>
37 #include <ntfs-3g/attrib.h>
38 #include <ntfs-3g/misc.h>
39 #include <ntfs-3g/reparse.h>
40 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
41 #include <ntfs-3g/xattrs.h>
42 #include <ntfs-3g/volume.h>
48 extern int ntfs_get_inode_security(ntfs_inode *ni, u32 selection, char *buf,
49 u32 buflen, u32 *psize);
51 extern u32 ntfs_get_inode_attributes(ntfs_inode *ni);
54 /* Structure that allows searching the security descriptors by SHA1 message
57 struct wim_security_data *sd;
61 /* Binary tree node of security descriptors, indexed by the @hash field. */
64 u8 hash[SHA1_HASH_SIZE];
66 struct sd_node *right;
69 static void free_sd_tree(struct sd_node *root)
72 free_sd_tree(root->left);
73 free_sd_tree(root->right);
77 /* Frees a security descriptor index set. */
78 static void destroy_sd_set(struct sd_set *sd_set)
80 free_sd_tree(sd_set->root);
83 /* Inserts a a new node into the security descriptor index tree. */
84 static void insert_sd_node(struct sd_node *new, struct sd_node *root)
86 int cmp = hashes_cmp(new->hash, root->hash);
89 insert_sd_node(new, root->left);
94 insert_sd_node(new, root->right);
102 /* Returns the security ID of the security data having a SHA1 message digest of
103 * @hash in the security descriptor index tree rooted at @root.
105 * If not found, return -1. */
106 static int lookup_sd(const u8 hash[SHA1_HASH_SIZE], struct sd_node *root)
111 cmp = hashes_cmp(hash, root->hash);
113 return lookup_sd(hash, root->left);
115 return lookup_sd(hash, root->right);
117 return root->security_id;
121 * Adds a security descriptor to the indexed security descriptor set as well as
122 * the corresponding `struct wim_security_data', and returns the new security
123 * ID; or, if there is an existing security descriptor that is the same, return
124 * the security ID for it. If a new security descriptor cannot be allocated,
127 static int sd_set_add_sd(struct sd_set *sd_set, const char descriptor[],
130 u8 hash[SHA1_HASH_SIZE];
136 struct wim_security_data *sd;
138 sha1_buffer((const u8*)descriptor, size, hash);
140 security_id = lookup_sd(hash, sd_set->root);
141 if (security_id >= 0)
144 new = MALLOC(sizeof(*new));
147 descr_copy = MALLOC(size);
153 memcpy(descr_copy, descriptor, size);
154 new->security_id = sd->num_entries;
157 copy_hash(new->hash, hash);
160 descriptors = REALLOC(sd->descriptors,
161 (sd->num_entries + 1) * sizeof(sd->descriptors[0]));
164 sd->descriptors = descriptors;
165 sizes = REALLOC(sd->sizes,
166 (sd->num_entries + 1) * sizeof(sd->sizes[0]));
170 sd->descriptors[sd->num_entries] = descr_copy;
171 sd->sizes[sd->num_entries] = size;
173 DEBUG("There are now %d security descriptors", sd->num_entries);
174 sd->total_length += size + sizeof(sd->sizes[0]);
177 insert_sd_node(new, sd_set->root);
180 return new->security_id;
189 static inline ntfschar *attr_record_name(ATTR_RECORD *ar)
191 return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
194 /* Calculates the SHA1 message digest of a NTFS attribute.
196 * @ni: The NTFS inode containing the attribute.
197 * @ar: The ATTR_RECORD describing the attribute.
198 * @md: If successful, the returned SHA1 message digest.
199 * @reparse_tag_ret: Optional pointer into which the first 4 bytes of the
200 * attribute will be written (to get the reparse
203 * Return 0 on success or nonzero on error.
205 static int ntfs_attr_sha1sum(ntfs_inode *ni, ATTR_RECORD *ar,
206 u8 md[SHA1_HASH_SIZE],
207 u32 *reparse_tag_ret)
215 na = ntfs_attr_open(ni, ar->type, attr_record_name(ar),
218 ERROR_WITH_ERRNO("Failed to open NTFS attribute");
219 return WIMLIB_ERR_NTFS_3G;
222 bytes_remaining = na->data_size;
225 DEBUG2("Calculating SHA1 message digest (%"PRIu64" bytes)",
228 while (bytes_remaining) {
229 s64 to_read = min(bytes_remaining, sizeof(buf));
230 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
231 ERROR_WITH_ERRNO("Error reading NTFS attribute");
232 return WIMLIB_ERR_NTFS_3G;
234 if (bytes_remaining == na->data_size && reparse_tag_ret)
235 *reparse_tag_ret = le32_to_cpu(*(u32*)buf);
236 sha1_update(&ctx, buf, to_read);
238 bytes_remaining -= to_read;
240 sha1_final(md, &ctx);
245 /* Load the streams from a WIM file or reparse point in the NTFS volume into the
246 * WIM lookup table */
247 static int capture_ntfs_streams(struct dentry *dentry, ntfs_inode *ni,
248 char path[], size_t path_len,
249 struct lookup_table *lookup_table,
250 ntfs_volume **ntfs_vol_p,
254 ntfs_attr_search_ctx *actx;
255 u8 attr_hash[SHA1_HASH_SIZE];
256 struct ntfs_location *ntfs_loc = NULL;
258 struct lookup_table_entry *lte;
260 DEBUG2("Capturing NTFS data streams from `%s'", path);
262 /* Get context to search the streams of the NTFS file. */
263 actx = ntfs_attr_get_search_ctx(ni, NULL);
265 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
267 return WIMLIB_ERR_NTFS_3G;
270 /* Capture each data stream or reparse data stream. */
271 while (!ntfs_attr_lookup(type, NULL, 0,
272 CASE_SENSITIVE, 0, NULL, 0, actx))
274 char *stream_name_utf8;
275 size_t stream_name_utf16_len;
277 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
278 u64 name_length = actx->attr->name_length;
280 if (data_size == 0) {
282 ERROR_WITH_ERRNO("Failed to get size of attribute of "
284 ret = WIMLIB_ERR_NTFS_3G;
287 /* Empty stream. No lookup table entry is needed. */
290 if (type == AT_REPARSE_POINT && data_size < 8) {
291 ERROR("`%s': reparse point buffer too small",
293 ret = WIMLIB_ERR_NTFS_3G;
296 /* Checksum the stream. */
297 ret = ntfs_attr_sha1sum(ni, actx->attr, attr_hash, &reparse_tag);
301 /* Make a lookup table entry for the stream, or use an existing
302 * one if there's already an identical stream. */
303 lte = __lookup_resource(lookup_table, attr_hash);
304 ret = WIMLIB_ERR_NOMEM;
308 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
311 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
312 ntfs_loc->path_utf8 = MALLOC(path_len + 1);
313 if (!ntfs_loc->path_utf8)
314 goto out_free_ntfs_loc;
315 memcpy(ntfs_loc->path_utf8, path, path_len + 1);
317 ntfs_loc->stream_name_utf16 = MALLOC(name_length * 2);
318 if (!ntfs_loc->stream_name_utf16)
319 goto out_free_ntfs_loc;
320 memcpy(ntfs_loc->stream_name_utf16,
321 attr_record_name(actx->attr),
322 actx->attr->name_length * 2);
323 ntfs_loc->stream_name_utf16_num_chars = name_length;
326 lte = new_lookup_table_entry();
328 goto out_free_ntfs_loc;
329 lte->ntfs_loc = ntfs_loc;
330 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
331 if (type == AT_REPARSE_POINT) {
332 dentry->reparse_tag = reparse_tag;
333 ntfs_loc->is_reparse_point = true;
334 lte->resource_entry.original_size = data_size - 8;
335 lte->resource_entry.size = data_size - 8;
337 ntfs_loc->is_reparse_point = false;
338 lte->resource_entry.original_size = data_size;
339 lte->resource_entry.size = data_size;
342 DEBUG("Add resource for `%s' (size = %zu)",
343 dentry->file_name_utf8,
344 lte->resource_entry.original_size);
345 copy_hash(lte->hash, attr_hash);
346 lookup_table_insert(lookup_table, lte);
349 if (name_length == 0) {
350 /* Unnamed data stream. Put the reference to it in the
353 ERROR("Found two un-named data streams for "
355 ret = WIMLIB_ERR_NTFS_3G;
360 /* Named data stream. Put the reference to it in the
361 * alternate data stream entries */
362 struct ads_entry *new_ads_entry;
363 size_t stream_name_utf8_len;
364 stream_name_utf8 = utf16_to_utf8((const char*)attr_record_name(actx->attr),
366 &stream_name_utf8_len);
367 if (!stream_name_utf8)
369 new_ads_entry = dentry_add_ads(dentry, stream_name_utf8);
370 FREE(stream_name_utf8);
374 wimlib_assert(new_ads_entry->stream_name_len == name_length * 2);
376 new_ads_entry->lte = lte;
382 free_lookup_table_entry(lte);
385 FREE(ntfs_loc->path_utf8);
386 FREE(ntfs_loc->stream_name_utf16);
390 ntfs_attr_put_search_ctx(actx);
392 DEBUG2("Successfully captured NTFS streams from `%s'", path);
394 ERROR("Failed to capture NTFS streams from `%s", path);
399 struct dentry *parent;
403 struct lookup_table *lookup_table;
404 struct sd_set *sd_set;
405 const struct capture_config *config;
406 ntfs_volume **ntfs_vol_p;
411 build_dentry_tree_ntfs_recursive(struct dentry **root_p, ntfs_inode *dir_ni,
412 ntfs_inode *ni, char path[], size_t path_len,
414 struct lookup_table *lookup_table,
415 struct sd_set *sd_set,
416 const struct capture_config *config,
417 ntfs_volume **ntfs_vol_p,
420 static int wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
421 const int name_len, const int name_type,
422 const s64 pos, const MFT_REF mref,
423 const unsigned dt_type)
425 struct readdir_ctx *ctx;
426 size_t utf8_name_len;
428 struct dentry *child = NULL;
432 if (name_type == FILE_NAME_DOS)
437 utf8_name = utf16_to_utf8((const char*)name, name_len * 2,
442 if (utf8_name[0] == '.' &&
443 (utf8_name[1] == '\0' ||
444 (utf8_name[1] == '.' && utf8_name[2] == '\0'))) {
446 goto out_free_utf8_name;
451 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
453 ERROR_WITH_ERRNO("Failed to open NTFS inode");
456 path_len = ctx->path_len;
458 ctx->path[path_len++] = '/';
459 memcpy(ctx->path + path_len, utf8_name, utf8_name_len + 1);
460 path_len += utf8_name_len;
461 ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
462 ni, ctx->path, path_len, name_type,
463 ctx->lookup_table, ctx->sd_set,
464 ctx->config, ctx->ntfs_vol_p,
468 link_dentry(child, ctx->parent);
470 ntfs_inode_close(ni);
477 static int change_dentry_short_name(struct dentry *dentry,
478 const char short_name_utf8[],
479 int short_name_utf8_len)
481 size_t short_name_utf16_len;
482 char *short_name_utf16;
483 short_name_utf16 = utf8_to_utf16(short_name_utf8, short_name_utf8_len,
484 &short_name_utf16_len);
485 if (!short_name_utf16) {
486 ERROR_WITH_ERRNO("Failed to convert short name to UTF-16");
487 return WIMLIB_ERR_NOMEM;
489 dentry->short_name = short_name_utf16;
490 dentry->short_name_len = short_name_utf16_len;
494 /*#define HAVE_NTFS_INODE_FUNCTIONS*/
496 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
497 * At the same time, update the WIM lookup table with lookup table entries for
498 * the NTFS streams, and build an array of security descriptors.
500 static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
506 struct lookup_table *lookup_table,
507 struct sd_set *sd_set,
508 const struct capture_config *config,
509 ntfs_volume **ntfs_vol_p,
516 char dos_name_utf8[64];
519 mrec_flags = ni->mrec->flags;
520 #ifdef HAVE_NTFS_INODE_FUNCTIONS
521 attributes = ntfs_get_inode_attributes(ni);
523 struct SECURITY_CONTEXT ctx;
524 memset(&ctx, 0, sizeof(ctx));
526 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
527 ni, dir_ni, (char *)&attributes,
530 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
532 return WIMLIB_ERR_NTFS_3G;
536 if (exclude_path(path, config, false)) {
537 if (flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE) {
538 const char *file_type;
539 if (attributes & MFT_RECORD_IS_DIRECTORY)
540 file_type = "directory";
543 printf("Excluding %s `%s' from capture\n",
550 if (flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
551 printf("Scanning `%s'\n", path);
553 root = new_dentry(path_basename(path));
555 return WIMLIB_ERR_NOMEM;
558 if (dir_ni && (name_type == FILE_NAME_WIN32_AND_DOS
559 || name_type == FILE_NAME_WIN32))
561 ret = ntfs_get_ntfs_dos_name(ni, dir_ni, dos_name_utf8,
562 sizeof(dos_name_utf8));
564 DEBUG("Changing short name of `%s'", path);
565 ret = change_dentry_short_name(root, dos_name_utf8,
570 if (errno != ENODATA) {
571 ERROR_WITH_ERRNO("Error getting DOS name "
573 return WIMLIB_ERR_NTFS_3G;
578 root->creation_time = le64_to_cpu(ni->creation_time);
579 root->last_write_time = le64_to_cpu(ni->last_data_change_time);
580 root->last_access_time = le64_to_cpu(ni->last_access_time);
581 root->attributes = le32_to_cpu(attributes);
582 root->link_group_id = ni->mft_no;
583 root->resolved = true;
585 if (attributes & FILE_ATTR_REPARSE_POINT) {
586 /* Junction point, symbolic link, or other reparse point */
587 ret = capture_ntfs_streams(root, ni, path, path_len,
588 lookup_table, ntfs_vol_p,
590 } else if (mrec_flags & MFT_RECORD_IS_DIRECTORY) {
592 /* Normal directory */
594 struct readdir_ctx ctx = {
598 .path_len = path_len,
599 .lookup_table = lookup_table,
602 .ntfs_vol_p = ntfs_vol_p,
605 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
607 ERROR_WITH_ERRNO("ntfs_readdir()");
608 ret = WIMLIB_ERR_NTFS_3G;
612 ret = capture_ntfs_streams(root, ni, path, path_len,
613 lookup_table, ntfs_vol_p,
619 #ifdef HAVE_NTFS_INODE_FUNCTIONS
620 ret = ntfs_get_inode_security(ni,
621 OWNER_SECURITY_INFORMATION |
622 GROUP_SECURITY_INFORMATION |
623 DACL_SECURITY_INFORMATION |
624 SACL_SECURITY_INFORMATION,
627 ret = ntfs_get_inode_security(ni,
628 OWNER_SECURITY_INFORMATION |
629 GROUP_SECURITY_INFORMATION |
630 DACL_SECURITY_INFORMATION |
631 SACL_SECURITY_INFORMATION,
632 sd, sd_size, &sd_size);
634 ERROR_WITH_ERRNO("Failed to get security information from "
636 ret = WIMLIB_ERR_NTFS_3G;
639 /*print_security_descriptor(sd, sd_size);*/
640 root->security_id = sd_set_add_sd(sd_set, sd, ret);
641 if (root->security_id == -1) {
642 ERROR("Out of memory");
643 return WIMLIB_ERR_NOMEM;
645 DEBUG("Added security ID = %u for `%s'",
646 root->security_id, path);
648 root->security_id = -1;
649 DEBUG("No security ID for `%s'", path);
657 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
660 if (ret > sizeof(sd)) {
662 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
663 ni, dir_ni, sd, ret);
666 root->security_id = sd_set_add_sd(sd_set, sd, ret);
667 if (root->security_id == -1) {
668 ERROR("Out of memory");
669 return WIMLIB_ERR_NOMEM;
671 DEBUG("Added security ID = %u for `%s'",
672 root->security_id, path);
674 } else if (ret < 0) {
675 ERROR_WITH_ERRNO("Failed to get security information from "
677 ret = WIMLIB_ERR_NTFS_3G;
679 root->security_id = -1;
680 DEBUG("No security ID for `%s'", path);
686 static int build_dentry_tree_ntfs(struct dentry **root_p,
688 struct lookup_table *lookup_table,
689 struct wim_security_data *sd,
690 const struct capture_config *config,
697 struct sd_set sd_set = {
701 ntfs_volume **ntfs_vol_p = extra_arg;
703 DEBUG("Mounting NTFS volume `%s' read-only", device);
705 vol = ntfs_mount(device, MS_RDONLY);
707 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
709 return WIMLIB_ERR_NTFS_3G;
711 ntfs_open_secure(vol);
713 /* We don't want to capture the special NTFS files such as $Bitmap. Not
714 * to be confused with "hidden" or "system" files which are real files
715 * that we do need to capture. */
716 NVolClearShowSysFiles(vol);
718 DEBUG("Opening root NTFS dentry");
719 root_ni = ntfs_inode_open(vol, FILE_root);
721 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
723 ret = WIMLIB_ERR_NTFS_3G;
727 /* Currently we assume that all the UTF-8 paths fit into this length and
728 * there is no check for overflow. */
729 char *path = MALLOC(32768);
731 ERROR("Could not allocate memory for NTFS pathname");
737 ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
738 FILE_NAME_POSIX, lookup_table,
739 &sd_set, config, ntfs_vol_p,
743 ntfs_inode_close(root_ni);
744 destroy_sd_set(&sd_set);
748 if (ntfs_umount(vol, FALSE) != 0) {
749 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
752 ret = WIMLIB_ERR_NTFS_3G;
755 /* We need to leave the NTFS volume mounted so that we can read
756 * the NTFS files again when we are actually writing the WIM */
764 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
767 const char *config_str,
771 if (flags & (WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)) {
772 ERROR("Cannot dereference files when capturing directly from NTFS");
773 return WIMLIB_ERR_INVALID_PARAM;
775 return do_add_image(w, device, name, config_str, config_len, flags,
776 build_dentry_tree_ntfs, &w->ntfs_vol);
779 #else /* WITH_NTFS_3G */
780 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
783 const char *config_str,
787 ERROR("wimlib was compiled without support for NTFS-3g, so");
788 ERROR("we cannot capture a WIM image directly from a NTFS volume");
789 return WIMLIB_ERR_UNSUPPORTED;
791 #endif /* WITH_NTFS_3G */