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/.
31 #include <ntfs-3g/endians.h>
32 #include <ntfs-3g/types.h>
35 #include "wimlib_internal.h"
40 #include "lookup_table.h"
42 #include <ntfs-3g/layout.h>
43 #include <ntfs-3g/acls.h>
44 #include <ntfs-3g/attrib.h>
45 #include <ntfs-3g/misc.h>
46 #include <ntfs-3g/reparse.h>
47 #include <ntfs-3g/security.h> /* security.h before xattrs.h */
48 #include <ntfs-3g/xattrs.h>
49 #include <ntfs-3g/volume.h>
55 extern int ntfs_get_inode_security(ntfs_inode *ni, u32 selection, char *buf,
56 u32 buflen, u32 *psize);
58 extern u32 ntfs_get_inode_attributes(ntfs_inode *ni);
61 /* Structure that allows searching the security descriptors by SHA1 message
64 struct wim_security_data *sd;
68 /* Binary tree node of security descriptors, indexed by the @hash field. */
71 u8 hash[SHA1_HASH_SIZE];
73 struct sd_node *right;
76 static void free_sd_tree(struct sd_node *root)
79 free_sd_tree(root->left);
80 free_sd_tree(root->right);
84 /* Frees a security descriptor index set. */
85 static void destroy_sd_set(struct sd_set *sd_set)
87 free_sd_tree(sd_set->root);
90 /* Inserts a a new node into the security descriptor index tree. */
91 static void insert_sd_node(struct sd_node *new, struct sd_node *root)
93 int cmp = hashes_cmp(new->hash, root->hash);
96 insert_sd_node(new, root->left);
101 insert_sd_node(new, root->right);
109 /* Returns the security ID of the security data having a SHA1 message digest of
110 * @hash in the security descriptor index tree rooted at @root.
112 * If not found, return -1. */
113 static int lookup_sd(const u8 hash[SHA1_HASH_SIZE], struct sd_node *root)
118 cmp = hashes_cmp(hash, root->hash);
120 return lookup_sd(hash, root->left);
122 return lookup_sd(hash, root->right);
124 return root->security_id;
128 * Adds a security descriptor to the indexed security descriptor set as well as
129 * the corresponding `struct wim_security_data', and returns the new security
130 * ID; or, if there is an existing security descriptor that is the same, return
131 * the security ID for it. If a new security descriptor cannot be allocated,
134 static int sd_set_add_sd(struct sd_set *sd_set, const char descriptor[],
137 u8 hash[SHA1_HASH_SIZE];
143 struct wim_security_data *sd;
145 sha1_buffer((const u8*)descriptor, size, hash);
147 security_id = lookup_sd(hash, sd_set->root);
148 if (security_id >= 0)
151 new = MALLOC(sizeof(*new));
154 descr_copy = MALLOC(size);
160 memcpy(descr_copy, descriptor, size);
161 new->security_id = sd->num_entries;
164 copy_hash(new->hash, hash);
167 descriptors = REALLOC(sd->descriptors,
168 (sd->num_entries + 1) * sizeof(sd->descriptors[0]));
171 sd->descriptors = descriptors;
172 sizes = REALLOC(sd->sizes,
173 (sd->num_entries + 1) * sizeof(sd->sizes[0]));
177 sd->descriptors[sd->num_entries] = descr_copy;
178 sd->sizes[sd->num_entries] = size;
180 DEBUG("There are now %d security descriptors", sd->num_entries);
181 sd->total_length += size + sizeof(sd->sizes[0]);
184 insert_sd_node(new, sd_set->root);
187 return new->security_id;
196 static inline ntfschar *attr_record_name(ATTR_RECORD *ar)
198 return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
201 /* Calculates the SHA1 message digest of a NTFS attribute.
203 * @ni: The NTFS inode containing the attribute.
204 * @ar: The ATTR_RECORD describing the attribute.
205 * @md: If successful, the returned SHA1 message digest.
206 * @reparse_tag_ret: Optional pointer into which the first 4 bytes of the
207 * attribute will be written (to get the reparse
210 * Return 0 on success or nonzero on error.
212 static int ntfs_attr_sha1sum(ntfs_inode *ni, ATTR_RECORD *ar,
213 u8 md[SHA1_HASH_SIZE],
214 u32 *reparse_tag_ret)
222 na = ntfs_attr_open(ni, ar->type, attr_record_name(ar),
225 ERROR_WITH_ERRNO("Failed to open NTFS attribute");
226 return WIMLIB_ERR_NTFS_3G;
229 bytes_remaining = na->data_size;
232 DEBUG2("Calculating SHA1 message digest (%"PRIu64" bytes)",
235 while (bytes_remaining) {
236 s64 to_read = min(bytes_remaining, sizeof(buf));
237 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
238 ERROR_WITH_ERRNO("Error reading NTFS attribute");
239 return WIMLIB_ERR_NTFS_3G;
241 if (bytes_remaining == na->data_size && reparse_tag_ret)
242 *reparse_tag_ret = le32_to_cpu(*(u32*)buf);
243 sha1_update(&ctx, buf, to_read);
245 bytes_remaining -= to_read;
247 sha1_final(md, &ctx);
252 /* Load the streams from a WIM file or reparse point in the NTFS volume into the
253 * WIM lookup table */
254 static int capture_ntfs_streams(struct dentry *dentry, ntfs_inode *ni,
255 char path[], size_t path_len,
256 struct lookup_table *lookup_table,
257 ntfs_volume **ntfs_vol_p,
261 ntfs_attr_search_ctx *actx;
262 u8 attr_hash[SHA1_HASH_SIZE];
263 struct ntfs_location *ntfs_loc = NULL;
265 struct lookup_table_entry *lte;
267 DEBUG2("Capturing NTFS data streams from `%s'", path);
269 /* Get context to search the streams of the NTFS file. */
270 actx = ntfs_attr_get_search_ctx(ni, NULL);
272 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
274 return WIMLIB_ERR_NTFS_3G;
277 /* Capture each data stream or reparse data stream. */
278 while (!ntfs_attr_lookup(type, NULL, 0,
279 CASE_SENSITIVE, 0, NULL, 0, actx))
281 char *stream_name_utf8;
282 size_t stream_name_utf16_len;
284 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
285 u64 name_length = actx->attr->name_length;
287 if (data_size == 0) {
289 ERROR_WITH_ERRNO("Failed to get size of attribute of "
291 ret = WIMLIB_ERR_NTFS_3G;
294 /* Empty stream. No lookup table entry is needed. */
297 if (type == AT_REPARSE_POINT && data_size < 8) {
298 ERROR("`%s': reparse point buffer too small",
300 ret = WIMLIB_ERR_NTFS_3G;
303 /* Checksum the stream. */
304 ret = ntfs_attr_sha1sum(ni, actx->attr, attr_hash, &reparse_tag);
308 /* Make a lookup table entry for the stream, or use an existing
309 * one if there's already an identical stream. */
310 lte = __lookup_resource(lookup_table, attr_hash);
311 ret = WIMLIB_ERR_NOMEM;
315 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
318 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
319 ntfs_loc->path_utf8 = MALLOC(path_len + 1);
320 if (!ntfs_loc->path_utf8)
321 goto out_free_ntfs_loc;
322 memcpy(ntfs_loc->path_utf8, path, path_len + 1);
324 ntfs_loc->stream_name_utf16 = MALLOC(name_length * 2);
325 if (!ntfs_loc->stream_name_utf16)
326 goto out_free_ntfs_loc;
327 memcpy(ntfs_loc->stream_name_utf16,
328 attr_record_name(actx->attr),
329 actx->attr->name_length * 2);
330 ntfs_loc->stream_name_utf16_num_chars = name_length;
333 lte = new_lookup_table_entry();
335 goto out_free_ntfs_loc;
336 lte->ntfs_loc = ntfs_loc;
337 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
338 if (type == AT_REPARSE_POINT) {
339 dentry->inode->reparse_tag = reparse_tag;
340 ntfs_loc->is_reparse_point = true;
341 lte->resource_entry.original_size = data_size - 8;
342 lte->resource_entry.size = data_size - 8;
344 ntfs_loc->is_reparse_point = false;
345 lte->resource_entry.original_size = data_size;
346 lte->resource_entry.size = data_size;
349 DEBUG("Add resource for `%s' (size = %zu)",
350 dentry->file_name_utf8,
351 lte->resource_entry.original_size);
352 copy_hash(lte->hash, attr_hash);
353 lookup_table_insert(lookup_table, lte);
356 if (name_length == 0) {
357 /* Unnamed data stream. Put the reference to it in the
359 if (dentry->inode->lte) {
360 ERROR("Found two un-named data streams for "
362 ret = WIMLIB_ERR_NTFS_3G;
365 dentry->inode->lte = lte;
367 /* Named data stream. Put the reference to it in the
368 * alternate data stream entries */
369 struct ads_entry *new_ads_entry;
370 size_t stream_name_utf8_len;
371 stream_name_utf8 = utf16_to_utf8((const char*)attr_record_name(actx->attr),
373 &stream_name_utf8_len);
374 if (!stream_name_utf8)
376 new_ads_entry = inode_add_ads(dentry->inode, stream_name_utf8);
377 FREE(stream_name_utf8);
381 wimlib_assert(new_ads_entry->stream_name_len == name_length * 2);
383 new_ads_entry->lte = lte;
389 free_lookup_table_entry(lte);
392 FREE(ntfs_loc->path_utf8);
393 FREE(ntfs_loc->stream_name_utf16);
397 ntfs_attr_put_search_ctx(actx);
399 DEBUG2("Successfully captured NTFS streams from `%s'", path);
401 ERROR("Failed to capture NTFS streams from `%s", path);
406 struct dentry *parent;
410 struct lookup_table *lookup_table;
411 struct sd_set *sd_set;
412 const struct capture_config *config;
413 ntfs_volume **ntfs_vol_p;
418 build_dentry_tree_ntfs_recursive(struct dentry **root_p, ntfs_inode *dir_ni,
419 ntfs_inode *ni, char path[], size_t path_len,
421 struct lookup_table *lookup_table,
422 struct sd_set *sd_set,
423 const struct capture_config *config,
424 ntfs_volume **ntfs_vol_p,
427 static int wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
428 const int name_len, const int name_type,
429 const s64 pos, const MFT_REF mref,
430 const unsigned dt_type)
432 struct readdir_ctx *ctx;
433 size_t utf8_name_len;
435 struct dentry *child = NULL;
439 if (name_type == FILE_NAME_DOS)
444 utf8_name = utf16_to_utf8((const char*)name, name_len * 2,
449 if (utf8_name[0] == '.' &&
450 (utf8_name[1] == '\0' ||
451 (utf8_name[1] == '.' && utf8_name[2] == '\0'))) {
453 goto out_free_utf8_name;
458 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
460 ERROR_WITH_ERRNO("Failed to open NTFS inode");
463 path_len = ctx->path_len;
465 ctx->path[path_len++] = '/';
466 memcpy(ctx->path + path_len, utf8_name, utf8_name_len + 1);
467 path_len += utf8_name_len;
468 ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
469 ni, ctx->path, path_len, name_type,
470 ctx->lookup_table, ctx->sd_set,
471 ctx->config, ctx->ntfs_vol_p,
475 link_dentry(child, ctx->parent);
477 ntfs_inode_close(ni);
484 static int change_dentry_short_name(struct dentry *dentry,
485 const char short_name_utf8[],
486 int short_name_utf8_len)
488 size_t short_name_utf16_len;
489 char *short_name_utf16;
490 short_name_utf16 = utf8_to_utf16(short_name_utf8, short_name_utf8_len,
491 &short_name_utf16_len);
492 if (!short_name_utf16) {
493 ERROR_WITH_ERRNO("Failed to convert short name to UTF-16");
494 return WIMLIB_ERR_NOMEM;
496 dentry->short_name = short_name_utf16;
497 dentry->short_name_len = short_name_utf16_len;
501 /*#define HAVE_NTFS_INODE_FUNCTIONS*/
503 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
504 * At the same time, update the WIM lookup table with lookup table entries for
505 * the NTFS streams, and build an array of security descriptors.
507 static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
513 struct lookup_table *lookup_table,
514 struct sd_set *sd_set,
515 const struct capture_config *config,
516 ntfs_volume **ntfs_vol_p,
523 char dos_name_utf8[64];
526 mrec_flags = ni->mrec->flags;
527 #ifdef HAVE_NTFS_INODE_FUNCTIONS
528 attributes = ntfs_get_inode_attributes(ni);
530 struct SECURITY_CONTEXT ctx;
531 memset(&ctx, 0, sizeof(ctx));
533 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
534 ni, dir_ni, (char *)&attributes,
537 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
539 return WIMLIB_ERR_NTFS_3G;
543 if (exclude_path(path, config, false)) {
544 if (flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE) {
545 const char *file_type;
546 if (attributes & MFT_RECORD_IS_DIRECTORY)
547 file_type = "directory";
550 printf("Excluding %s `%s' from capture\n",
557 if (flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
558 printf("Scanning `%s'\n", path);
560 root = new_dentry_with_inode(path_basename(path));
562 return WIMLIB_ERR_NOMEM;
565 if (dir_ni && (name_type == FILE_NAME_WIN32_AND_DOS
566 || name_type == FILE_NAME_WIN32))
568 ret = ntfs_get_ntfs_dos_name(ni, dir_ni, dos_name_utf8,
569 sizeof(dos_name_utf8));
571 DEBUG("Changing short name of `%s'", path);
572 ret = change_dentry_short_name(root, dos_name_utf8,
578 if (errno != ENODATA) {
579 ERROR_WITH_ERRNO("Error getting DOS name "
581 return WIMLIB_ERR_NTFS_3G;
587 root->inode->creation_time = le64_to_cpu(ni->creation_time);
588 root->inode->last_write_time = le64_to_cpu(ni->last_data_change_time);
589 root->inode->last_access_time = le64_to_cpu(ni->last_access_time);
590 root->inode->attributes = le32_to_cpu(attributes);
591 root->inode->ino = ni->mft_no;
592 root->inode->resolved = true;
594 if (attributes & FILE_ATTR_REPARSE_POINT) {
595 /* Junction point, symbolic link, or other reparse point */
596 ret = capture_ntfs_streams(root, ni, path, path_len,
597 lookup_table, ntfs_vol_p,
599 } else if (mrec_flags & MFT_RECORD_IS_DIRECTORY) {
601 /* Normal directory */
603 struct readdir_ctx ctx = {
607 .path_len = path_len,
608 .lookup_table = lookup_table,
611 .ntfs_vol_p = ntfs_vol_p,
614 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
616 ERROR_WITH_ERRNO("ntfs_readdir()");
617 ret = WIMLIB_ERR_NTFS_3G;
621 ret = capture_ntfs_streams(root, ni, path, path_len,
622 lookup_table, ntfs_vol_p,
628 #ifdef HAVE_NTFS_INODE_FUNCTIONS
629 ret = ntfs_get_inode_security(ni,
630 OWNER_SECURITY_INFORMATION |
631 GROUP_SECURITY_INFORMATION |
632 DACL_SECURITY_INFORMATION |
633 SACL_SECURITY_INFORMATION,
636 ret = ntfs_get_inode_security(ni,
637 OWNER_SECURITY_INFORMATION |
638 GROUP_SECURITY_INFORMATION |
639 DACL_SECURITY_INFORMATION |
640 SACL_SECURITY_INFORMATION,
641 sd, sd_size, &sd_size);
643 ERROR_WITH_ERRNO("Failed to get security information from "
645 ret = WIMLIB_ERR_NTFS_3G;
648 /*print_security_descriptor(sd, sd_size);*/
649 root->security_id = sd_set_add_sd(sd_set, sd, ret);
650 if (root->security_id == -1) {
651 ERROR("Out of memory");
652 return WIMLIB_ERR_NOMEM;
654 DEBUG("Added security ID = %u for `%s'",
655 root->security_id, path);
657 root->security_id = -1;
658 DEBUG("No security ID for `%s'", path);
666 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
669 if (ret > sizeof(sd)) {
671 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
672 ni, dir_ni, sd, ret);
675 root->inode->security_id = sd_set_add_sd(sd_set, sd, ret);
676 if (root->inode->security_id == -1) {
677 ERROR("Out of memory");
678 return WIMLIB_ERR_NOMEM;
680 DEBUG("Added security ID = %u for `%s'",
681 root->inode->security_id, path);
683 } else if (ret < 0) {
684 ERROR_WITH_ERRNO("Failed to get security information from "
686 ret = WIMLIB_ERR_NTFS_3G;
688 root->inode->security_id = -1;
689 DEBUG("No security ID for `%s'", path);
695 static int build_dentry_tree_ntfs(struct dentry **root_p,
697 struct lookup_table *lookup_table,
698 struct wim_security_data *sd,
699 const struct capture_config *config,
706 struct sd_set sd_set = {
710 ntfs_volume **ntfs_vol_p = extra_arg;
712 DEBUG("Mounting NTFS volume `%s' read-only", device);
714 vol = ntfs_mount(device, MS_RDONLY);
716 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
718 return WIMLIB_ERR_NTFS_3G;
720 ntfs_open_secure(vol);
722 /* We don't want to capture the special NTFS files such as $Bitmap. Not
723 * to be confused with "hidden" or "system" files which are real files
724 * that we do need to capture. */
725 NVolClearShowSysFiles(vol);
727 DEBUG("Opening root NTFS dentry");
728 root_ni = ntfs_inode_open(vol, FILE_root);
730 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
732 ret = WIMLIB_ERR_NTFS_3G;
736 /* Currently we assume that all the UTF-8 paths fit into this length and
737 * there is no check for overflow. */
738 char *path = MALLOC(32768);
740 ERROR("Could not allocate memory for NTFS pathname");
746 ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
747 FILE_NAME_POSIX, lookup_table,
748 &sd_set, config, ntfs_vol_p,
752 ntfs_inode_close(root_ni);
753 destroy_sd_set(&sd_set);
757 if (ntfs_umount(vol, FALSE) != 0) {
758 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
761 ret = WIMLIB_ERR_NTFS_3G;
764 /* We need to leave the NTFS volume mounted so that we can read
765 * the NTFS files again when we are actually writing the WIM */
773 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
776 const char *config_str,
780 if (flags & (WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)) {
781 ERROR("Cannot dereference files when capturing directly from NTFS");
782 return WIMLIB_ERR_INVALID_PARAM;
784 return do_add_image(w, device, name, config_str, config_len, flags,
785 build_dentry_tree_ntfs, &w->ntfs_vol);
788 #else /* WITH_NTFS_3G */
789 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
792 const char *config_str,
796 ERROR("wimlib was compiled without support for NTFS-3g, so");
797 ERROR("we cannot capture a WIM image directly from a NTFS volume");
798 return WIMLIB_ERR_UNSUPPORTED;
800 #endif /* WITH_NTFS_3G */