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>
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,
253 ntfs_attr_search_ctx *actx;
254 u8 attr_hash[SHA1_HASH_SIZE];
255 struct ntfs_location *ntfs_loc = NULL;
257 struct lookup_table_entry *lte;
259 DEBUG2("Capturing NTFS data streams from `%s'", path);
261 /* Get context to search the streams of the NTFS file. */
262 actx = ntfs_attr_get_search_ctx(ni, NULL);
264 ERROR_WITH_ERRNO("Cannot get NTFS attribute search "
266 return WIMLIB_ERR_NTFS_3G;
269 /* Capture each data stream or reparse data stream. */
270 while (!ntfs_attr_lookup(type, NULL, 0,
271 CASE_SENSITIVE, 0, NULL, 0, actx))
273 char *stream_name_utf8;
274 size_t stream_name_utf16_len;
276 u64 data_size = ntfs_get_attribute_value_length(actx->attr);
277 u64 name_length = actx->attr->name_length;
279 if (data_size == 0) {
281 ERROR_WITH_ERRNO("Failed to get size of attribute of "
283 ret = WIMLIB_ERR_NTFS_3G;
286 /* Empty stream. No lookup table entry is needed. */
289 if (type == AT_REPARSE_POINT && data_size < 8) {
290 ERROR("`%s': reparse point buffer too small",
292 ret = WIMLIB_ERR_NTFS_3G;
295 /* Checksum the stream. */
296 ret = ntfs_attr_sha1sum(ni, actx->attr, attr_hash, &reparse_tag);
300 /* Make a lookup table entry for the stream, or use an existing
301 * one if there's already an identical stream. */
302 lte = __lookup_resource(lookup_table, attr_hash);
303 ret = WIMLIB_ERR_NOMEM;
307 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
310 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
311 ntfs_loc->path_utf8 = MALLOC(path_len + 1);
312 if (!ntfs_loc->path_utf8)
313 goto out_free_ntfs_loc;
314 memcpy(ntfs_loc->path_utf8, path, path_len + 1);
316 ntfs_loc->stream_name_utf16 = MALLOC(name_length * 2);
317 if (!ntfs_loc->stream_name_utf16)
318 goto out_free_ntfs_loc;
319 memcpy(ntfs_loc->stream_name_utf16,
320 attr_record_name(actx->attr),
321 actx->attr->name_length * 2);
322 ntfs_loc->stream_name_utf16_num_chars = name_length;
325 lte = new_lookup_table_entry();
327 goto out_free_ntfs_loc;
328 lte->ntfs_loc = ntfs_loc;
329 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
330 if (type == AT_REPARSE_POINT) {
331 dentry->d_inode->reparse_tag = reparse_tag;
332 ntfs_loc->is_reparse_point = true;
333 lte->resource_entry.original_size = data_size - 8;
334 lte->resource_entry.size = data_size - 8;
336 ntfs_loc->is_reparse_point = false;
337 lte->resource_entry.original_size = data_size;
338 lte->resource_entry.size = data_size;
341 DEBUG("Add resource for `%s' (size = %zu)",
342 dentry->file_name_utf8,
343 lte->resource_entry.original_size);
344 copy_hash(lte->hash, attr_hash);
345 lookup_table_insert(lookup_table, lte);
348 if (name_length == 0) {
349 /* Unnamed data stream. Put the reference to it in the
351 if (dentry->d_inode->lte) {
352 ERROR("Found two un-named data streams for "
354 ret = WIMLIB_ERR_NTFS_3G;
357 dentry->d_inode->lte = lte;
359 /* Named data stream. Put the reference to it in the
360 * alternate data stream entries */
361 struct ads_entry *new_ads_entry;
362 size_t stream_name_utf8_len;
363 stream_name_utf8 = utf16_to_utf8((const char*)attr_record_name(actx->attr),
365 &stream_name_utf8_len);
366 if (!stream_name_utf8)
368 new_ads_entry = inode_add_ads(dentry->d_inode, stream_name_utf8);
369 FREE(stream_name_utf8);
373 wimlib_assert(new_ads_entry->stream_name_len == name_length * 2);
375 new_ads_entry->lte = lte;
381 free_lookup_table_entry(lte);
384 FREE(ntfs_loc->path_utf8);
385 FREE(ntfs_loc->stream_name_utf16);
389 ntfs_attr_put_search_ctx(actx);
391 DEBUG2("Successfully captured NTFS streams from `%s'", path);
393 ERROR("Failed to capture NTFS streams from `%s", path);
398 struct dentry *parent;
402 struct lookup_table *lookup_table;
403 struct sd_set *sd_set;
404 const struct capture_config *config;
405 ntfs_volume **ntfs_vol_p;
410 build_dentry_tree_ntfs_recursive(struct dentry **root_p, ntfs_inode *dir_ni,
411 ntfs_inode *ni, char path[], size_t path_len,
413 struct lookup_table *lookup_table,
414 struct sd_set *sd_set,
415 const struct capture_config *config,
416 ntfs_volume **ntfs_vol_p,
419 static int wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
420 const int name_len, const int name_type,
421 const s64 pos, const MFT_REF mref,
422 const unsigned dt_type)
424 struct readdir_ctx *ctx;
425 size_t utf8_name_len;
427 struct dentry *child = NULL;
431 if (name_type == FILE_NAME_DOS)
436 utf8_name = utf16_to_utf8((const char*)name, name_len * 2,
441 if (utf8_name[0] == '.' &&
442 (utf8_name[1] == '\0' ||
443 (utf8_name[1] == '.' && utf8_name[2] == '\0'))) {
445 goto out_free_utf8_name;
450 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
452 ERROR_WITH_ERRNO("Failed to open NTFS inode");
453 goto out_free_utf8_name;
455 path_len = ctx->path_len;
457 ctx->path[path_len++] = '/';
458 memcpy(ctx->path + path_len, utf8_name, utf8_name_len + 1);
459 path_len += utf8_name_len;
460 ret = build_dentry_tree_ntfs_recursive(&child, ctx->dir_ni,
461 ni, ctx->path, path_len, name_type,
462 ctx->lookup_table, ctx->sd_set,
463 ctx->config, ctx->ntfs_vol_p,
467 link_dentry(child, ctx->parent);
469 ntfs_inode_close(ni);
476 static int change_dentry_short_name(struct dentry *dentry,
477 const char short_name_utf8[],
478 int short_name_utf8_len)
480 size_t short_name_utf16_len;
481 char *short_name_utf16;
482 short_name_utf16 = utf8_to_utf16(short_name_utf8, short_name_utf8_len,
483 &short_name_utf16_len);
484 if (!short_name_utf16) {
485 ERROR_WITH_ERRNO("Failed to convert short name to UTF-16");
486 return WIMLIB_ERR_NOMEM;
488 dentry->short_name = short_name_utf16;
489 dentry->short_name_len = short_name_utf16_len;
493 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
494 * At the same time, update the WIM lookup table with lookup table entries for
495 * the NTFS streams, and build an array of security descriptors.
497 static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
503 struct lookup_table *lookup_table,
504 struct sd_set *sd_set,
505 const struct capture_config *config,
506 ntfs_volume **ntfs_vol_p,
513 char dos_name_utf8[64];
516 mrec_flags = ni->mrec->flags;
517 struct SECURITY_CONTEXT ctx;
518 memset(&ctx, 0, sizeof(ctx));
520 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ATTRIB,
521 ni, dir_ni, (char *)&attributes,
524 ERROR_WITH_ERRNO("Failed to get NTFS attributes from `%s'",
526 return WIMLIB_ERR_NTFS_3G;
529 if (exclude_path(path, config, false)) {
530 if (flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE) {
531 const char *file_type;
532 if (attributes & MFT_RECORD_IS_DIRECTORY)
533 file_type = "directory";
536 printf("Excluding %s `%s' from capture\n",
543 if (flags & WIMLIB_ADD_IMAGE_FLAG_VERBOSE)
544 printf("Scanning `%s'\n", path);
546 root = new_dentry_with_timeless_inode(path_basename(path));
548 return WIMLIB_ERR_NOMEM;
551 if (dir_ni && (name_type == FILE_NAME_WIN32_AND_DOS
552 || name_type == FILE_NAME_WIN32))
554 ret = ntfs_get_ntfs_dos_name(ni, dir_ni, dos_name_utf8,
555 sizeof(dos_name_utf8));
557 DEBUG("Changing short name of `%s'", path);
558 ret = change_dentry_short_name(root, dos_name_utf8,
564 if (errno != ENODATA) {
565 ERROR_WITH_ERRNO("Error getting DOS name "
567 return WIMLIB_ERR_NTFS_3G;
573 root->d_inode->creation_time = le64_to_cpu(ni->creation_time);
574 root->d_inode->last_write_time = le64_to_cpu(ni->last_data_change_time);
575 root->d_inode->last_access_time = le64_to_cpu(ni->last_access_time);
576 root->d_inode->attributes = le32_to_cpu(attributes);
577 root->d_inode->ino = ni->mft_no;
578 root->d_inode->resolved = true;
580 if (attributes & FILE_ATTR_REPARSE_POINT) {
581 /* Junction point, symbolic link, or other reparse point */
582 ret = capture_ntfs_streams(root, ni, path, path_len,
583 lookup_table, ntfs_vol_p,
585 } else if (mrec_flags & MFT_RECORD_IS_DIRECTORY) {
587 /* Normal directory */
589 struct readdir_ctx ctx = {
593 .path_len = path_len,
594 .lookup_table = lookup_table,
597 .ntfs_vol_p = ntfs_vol_p,
600 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
602 ERROR_WITH_ERRNO("ntfs_readdir()");
603 ret = WIMLIB_ERR_NTFS_3G;
607 ret = capture_ntfs_streams(root, ni, path, path_len,
608 lookup_table, ntfs_vol_p,
617 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
620 if (ret > sizeof(sd)) {
622 ret = ntfs_xattr_system_getxattr(&ctx, XATTR_NTFS_ACL,
623 ni, dir_ni, sd, ret);
626 root->d_inode->security_id = sd_set_add_sd(sd_set, sd, ret);
627 if (root->d_inode->security_id == -1) {
628 ERROR("Out of memory");
629 return WIMLIB_ERR_NOMEM;
631 DEBUG("Added security ID = %u for `%s'",
632 root->d_inode->security_id, path);
634 } else if (ret < 0) {
635 ERROR_WITH_ERRNO("Failed to get security information from "
637 ret = WIMLIB_ERR_NTFS_3G;
639 root->d_inode->security_id = -1;
640 DEBUG("No security ID for `%s'", path);
645 static int build_dentry_tree_ntfs(struct dentry **root_p,
647 struct lookup_table *lookup_table,
648 struct wim_security_data *sd,
649 const struct capture_config *config,
656 struct sd_set sd_set = {
660 ntfs_volume **ntfs_vol_p = extra_arg;
662 DEBUG("Mounting NTFS volume `%s' read-only", device);
664 vol = ntfs_mount(device, MS_RDONLY);
666 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
668 return WIMLIB_ERR_NTFS_3G;
670 ntfs_open_secure(vol);
672 /* We don't want to capture the special NTFS files such as $Bitmap. Not
673 * to be confused with "hidden" or "system" files which are real files
674 * that we do need to capture. */
675 NVolClearShowSysFiles(vol);
677 DEBUG("Opening root NTFS dentry");
678 root_ni = ntfs_inode_open(vol, FILE_root);
680 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
682 ret = WIMLIB_ERR_NTFS_3G;
686 /* Currently we assume that all the UTF-8 paths fit into this length and
687 * there is no check for overflow. */
688 char *path = MALLOC(32768);
690 ERROR("Could not allocate memory for NTFS pathname");
696 ret = build_dentry_tree_ntfs_recursive(root_p, NULL, root_ni, path, 1,
697 FILE_NAME_POSIX, lookup_table,
698 &sd_set, config, ntfs_vol_p,
702 ntfs_inode_close(root_ni);
703 destroy_sd_set(&sd_set);
707 if (ntfs_umount(vol, FALSE) != 0) {
708 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
711 ret = WIMLIB_ERR_NTFS_3G;
714 /* We need to leave the NTFS volume mounted so that we can read
715 * the NTFS files again when we are actually writing the WIM */
723 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
726 const char *config_str,
730 if (flags & (WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)) {
731 ERROR("Cannot dereference files when capturing directly from NTFS");
732 return WIMLIB_ERR_INVALID_PARAM;
734 return do_add_image(w, device, name, config_str, config_len, flags,
735 build_dentry_tree_ntfs, &w->ntfs_vol);
738 #else /* WITH_NTFS_3G */
739 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
742 const char *config_str,
746 ERROR("wimlib was compiled without support for NTFS-3g, so");
747 ERROR("we cannot capture a WIM image directly from a NTFS volume");
748 return WIMLIB_ERR_UNSUPPORTED;
750 #endif /* WITH_NTFS_3G */