4 * Capture a WIM image from a NTFS volume. We capture everything we can,
5 * including security data and alternate data streams. There should be no loss
10 * Copyright (C) 2012 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU Lesser General Public License as published by the Free
16 * Software Foundation; either version 2.1 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
29 #include "wimlib_internal.h"
34 #include "lookup_table.h"
36 #include <ntfs-3g/layout.h>
37 #include <ntfs-3g/acls.h>
38 #include <ntfs-3g/attrib.h>
39 #include <ntfs-3g/misc.h>
40 #include <ntfs-3g/reparse.h>
41 #include <ntfs-3g/security.h>
42 #include <ntfs-3g/volume.h>
46 extern int ntfs_inode_get_security(ntfs_inode *ni, u32 selection, char *buf,
47 u32 buflen, u32 *psize);
49 extern int ntfs_inode_get_attributes(ntfs_inode *ni);
51 /* Structure that allows searching the security descriptors by SHA1 message
54 struct wim_security_data *sd;
58 /* Binary tree node of security descriptors, indexed by the @hash field. */
61 u8 hash[SHA1_HASH_SIZE];
63 struct sd_node *right;
66 static void free_sd_tree(struct sd_node *root)
69 free_sd_tree(root->left);
70 free_sd_tree(root->right);
74 /* Frees a security descriptor index set. */
75 static void destroy_sd_set(struct sd_set *sd_set)
77 free_sd_tree(sd_set->root);
80 /* Inserts a a new node into the security descriptor index tree. */
81 static void insert_sd_node(struct sd_node *new, struct sd_node *root)
83 int cmp = hashes_cmp(new->hash, root->hash);
86 insert_sd_node(new, root->left);
91 insert_sd_node(new, root->right);
99 /* Returns the security ID of the security data having a SHA1 message digest of
100 * @hash in the security descriptor index tree rooted at @root.
102 * If not found, return -1. */
103 static int lookup_sd(const u8 hash[SHA1_HASH_SIZE], struct sd_node *root)
108 cmp = hashes_cmp(hash, root->hash);
110 return lookup_sd(hash, root->left);
112 return lookup_sd(hash, root->right);
114 return root->security_id;
118 * Adds a security descriptor to the indexed security descriptor set as well as
119 * the corresponding `struct wim_security_data', and returns the new security
120 * ID; or, if there is an existing security descriptor that is the same, return
121 * the security ID for it. If a new security descriptor cannot be allocated,
124 static int sd_set_add_sd(struct sd_set *sd_set, const char descriptor[],
127 u8 hash[SHA1_HASH_SIZE];
133 struct wim_security_data *sd;
135 sha1_buffer((const u8*)descriptor, size, hash);
136 security_id = lookup_sd(hash, sd_set->root);
137 if (security_id >= 0)
140 new = MALLOC(sizeof(*new));
143 descr_copy = MALLOC(size);
149 memcpy(descr_copy, descriptor, size);
150 new->security_id = sd->num_entries;
153 copy_hash(new->hash, hash);
156 descriptors = REALLOC(sd->descriptors,
157 (sd->num_entries + 1) * sizeof(sd->descriptors[0]));
160 sd->descriptors = descriptors;
161 sizes = REALLOC(sd->sizes,
162 (sd->num_entries + 1) * sizeof(sd->sizes[0]));
166 sd->descriptors[sd->num_entries] = descr_copy;
167 sd->sizes[sd->num_entries] = size;
169 sd->total_length += size + sizeof(sd->sizes[0]);
172 insert_sd_node(sd_set->root, new);
175 return new->security_id;
184 static inline ntfschar *attr_record_name(ATTR_RECORD *ar)
186 return (ntfschar*)((u8*)ar + le16_to_cpu(ar->name_offset));
189 /* Calculates the SHA1 message digest of a NTFS attribute.
191 * @ni: The NTFS inode containing the attribute.
192 * @ar: The ATTR_RECORD describing the attribute.
193 * @md: If successful, the returned SHA1 message digest.
195 * Return 0 on success or nonzero on error.
197 static int ntfs_attr_sha1sum(ntfs_inode *ni, ATTR_RECORD *ar,
198 u8 md[SHA1_HASH_SIZE])
206 na = ntfs_attr_open(ni, ar->type, attr_record_name(ar),
209 ERROR_WITH_ERRNO("Failed to open NTFS attribute");
210 return WIMLIB_ERR_NTFS_3G;
213 bytes_remaining = na->data_size;
216 DEBUG("Calculating SHA1 message digest (%"PRIu64" bytes)",
219 while (bytes_remaining) {
220 s64 to_read = min(bytes_remaining, sizeof(buf));
221 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
222 ERROR_WITH_ERRNO("Error reading NTFS attribute");
223 return WIMLIB_ERR_NTFS_3G;
225 sha1_update(&ctx, buf, to_read);
227 bytes_remaining -= to_read;
229 sha1_final(md, &ctx);
234 /* Load the streams from a WIM file or reparse point in the NTFS volume into the
235 * WIM lookup table */
236 static int capture_ntfs_streams(struct dentry *dentry, ntfs_inode *ni,
237 char path[], size_t path_len,
238 struct lookup_table *lookup_table,
239 ntfs_volume **ntfs_vol_p,
243 ntfs_attr_search_ctx *actx;
244 u8 attr_hash[SHA1_HASH_SIZE];
245 struct ntfs_location *ntfs_loc = NULL;
246 struct lookup_table_entry *lte;
249 DEBUG("Capturing NTFS data streams from `%s'", path);
251 /* Get context to search the streams of the NTFS file. */
252 actx = ntfs_attr_get_search_ctx(ni, NULL);
254 ERROR_WITH_ERRNO("Cannot get attribute search "
256 return WIMLIB_ERR_NTFS_3G;
259 /* Capture each data stream or reparse data stream. */
260 while (!ntfs_attr_lookup(type, NULL, 0,
261 CASE_SENSITIVE, 0, NULL, 0, actx))
263 char *stream_name_utf8;
264 size_t stream_name_utf16_len;
266 /* Checksum the stream. */
267 ret = ntfs_attr_sha1sum(ni, actx->attr, attr_hash);
271 /* Make a lookup table entry for the stream, or use an existing
272 * one if there's already an identical stream. */
273 lte = __lookup_resource(lookup_table, attr_hash);
274 ret = WIMLIB_ERR_NOMEM;
278 ntfs_loc = CALLOC(1, sizeof(*ntfs_loc));
281 ntfs_loc->ntfs_vol_p = ntfs_vol_p;
282 ntfs_loc->path_utf8 = MALLOC(path_len + 1);
283 if (!ntfs_loc->path_utf8)
284 goto out_free_ntfs_loc;
285 memcpy(ntfs_loc->path_utf8, path, path_len + 1);
286 ntfs_loc->stream_name_utf16 = MALLOC(actx->attr->name_length * 2);
287 if (!ntfs_loc->stream_name_utf16)
288 goto out_free_ntfs_loc;
289 memcpy(ntfs_loc->stream_name_utf16,
290 attr_record_name(actx->attr),
291 actx->attr->name_length * 2);
293 ntfs_loc->stream_name_utf16_num_chars = actx->attr->name_length;
294 ntfs_loc->is_reparse_point = (type == AT_REPARSE_POINT);
295 lte = new_lookup_table_entry();
297 goto out_free_ntfs_loc;
298 lte->ntfs_loc = ntfs_loc;
299 lte->resource_location = RESOURCE_IN_NTFS_VOLUME;
300 lte->resource_entry.original_size = actx->attr->data_size;
301 lte->resource_entry.size = actx->attr->data_size;
302 DEBUG("Add resource for `%s' (size = %zu)",
303 dentry->file_name_utf8,
304 lte->resource_entry.original_size);
305 copy_hash(lte->hash, attr_hash);
306 lookup_table_insert(lookup_table, lte);
308 if (actx->attr->name_length == 0) {
310 ERROR("Found two un-named data streams for "
312 ret = WIMLIB_ERR_NTFS_3G;
317 struct ads_entry *new_ads_entry;
318 stream_name_utf8 = utf16_to_utf8((const char*)attr_record_name(actx->attr),
319 actx->attr->name_length,
320 &stream_name_utf16_len);
321 if (!stream_name_utf8)
323 new_ads_entry = dentry_add_ads(dentry, stream_name_utf8);
324 FREE(stream_name_utf8);
328 new_ads_entry->lte = lte;
334 free_lookup_table_entry(lte);
337 FREE(ntfs_loc->path_utf8);
338 FREE(ntfs_loc->stream_name_utf16);
342 ntfs_attr_put_search_ctx(actx);
344 DEBUG("Successfully captured NTFS streams from `%s'", path);
346 DEBUG("Failed to capture NTFS streams from `%s", path);
351 struct dentry *parent;
355 struct lookup_table *lookup_table;
356 struct sd_set *sd_set;
357 const struct capture_config *config;
358 ntfs_volume **ntfs_vol_p;
362 build_dentry_tree_ntfs_recursive(struct dentry **root_p, ntfs_inode *ni,
363 char path[], size_t path_len,
364 struct lookup_table *lookup_table,
365 struct sd_set *sd_set,
366 const struct capture_config *config,
367 ntfs_volume **ntfs_vol_p);
369 static int wim_ntfs_capture_filldir(void *dirent, const ntfschar *name,
370 const int name_len, const int name_type,
371 const s64 pos, const MFT_REF mref,
372 const unsigned dt_type)
374 struct readdir_ctx *ctx;
375 size_t utf8_name_len;
377 struct dentry *child = NULL;
381 if (name_type == FILE_NAME_DOS)
386 utf8_name = utf16_to_utf8((const char*)name, name_len * 2,
391 if (utf8_name[0] == '.' &&
392 (utf8_name[1] == '\0' ||
393 (utf8_name[1] == '.' && utf8_name[2] == '\0'))) {
394 DEBUG("Skipping dentry `%s'", utf8_name);
396 goto out_free_utf8_name;
399 DEBUG("Opening inode for `%s'", utf8_name);
403 ntfs_inode *ni = ntfs_inode_open(ctx->dir_ni->vol, mref);
405 ERROR_WITH_ERRNO("Failed to open NTFS inode");
408 path_len = ctx->path_len;
410 ctx->path[path_len++] = '/';
411 memcpy(ctx->path + path_len, utf8_name, utf8_name_len + 1);
412 path_len += utf8_name_len;
413 ret = build_dentry_tree_ntfs_recursive(&child, ni, ctx->path, path_len,
414 ctx->lookup_table, ctx->sd_set,
415 ctx->config, ctx->ntfs_vol_p);
418 DEBUG("Linking dentry `%s' with parent `%s'",
419 child->file_name_utf8, ctx->parent->file_name_utf8);
420 link_dentry(child, ctx->parent);
422 ntfs_inode_close(ni);
429 /* Recursively build a WIM dentry tree corresponding to a NTFS volume.
430 * At the same time, update the WIM lookup table with lookup table entries for
431 * the NTFS streams, and build an array of security descriptors.
433 static int build_dentry_tree_ntfs_recursive(struct dentry **root_p,
435 char path[], size_t path_len,
436 struct lookup_table *lookup_table,
437 struct sd_set *sd_set,
438 const struct capture_config *config,
439 ntfs_volume **ntfs_vol_p)
447 if (exclude_path(path, config, false)) {
448 DEBUG("Excluding `%s' from capture", path);
452 DEBUG("Starting recursive capture at path = `%s'", path);
453 mrec_flags = ni->mrec->flags;
454 attributes = ntfs_inode_get_attributes(ni);
456 root = new_dentry(path_basename(path));
458 return WIMLIB_ERR_NOMEM;
461 root->creation_time = le64_to_cpu(ni->creation_time);
462 root->last_write_time = le64_to_cpu(ni->last_data_change_time);
463 root->last_access_time = le64_to_cpu(ni->last_access_time);
464 root->security_id = le32_to_cpu(ni->security_id);
465 root->attributes = le32_to_cpu(attributes);
466 root->link_group_id = ni->mft_no;
467 root->resolved = true;
469 if (attributes & FILE_ATTR_REPARSE_POINT) {
470 DEBUG("Reparse point `%s'", path);
471 /* Junction point, symbolic link, or other reparse point */
472 ret = capture_ntfs_streams(root, ni, path, path_len,
473 lookup_table, ntfs_vol_p,
475 } else if (mrec_flags & MFT_RECORD_IS_DIRECTORY) {
476 DEBUG("Directory `%s'", path);
478 /* Normal directory */
480 struct readdir_ctx ctx = {
484 .path_len = path_len,
485 .lookup_table = lookup_table,
488 .ntfs_vol_p = ntfs_vol_p,
490 ret = ntfs_readdir(ni, &pos, &ctx, wim_ntfs_capture_filldir);
492 ERROR_WITH_ERRNO("ntfs_readdir()");
493 ret = WIMLIB_ERR_NTFS_3G;
496 DEBUG("Normal file `%s'", path);
498 ret = capture_ntfs_streams(root, ni, path, path_len,
499 lookup_table, ntfs_vol_p,
505 ret = ntfs_inode_get_security(ni,
506 OWNER_SECURITY_INFORMATION |
507 GROUP_SECURITY_INFORMATION |
508 DACL_SECURITY_INFORMATION |
509 SACL_SECURITY_INFORMATION,
512 ret = ntfs_inode_get_security(ni,
513 OWNER_SECURITY_INFORMATION |
514 GROUP_SECURITY_INFORMATION |
515 DACL_SECURITY_INFORMATION |
516 SACL_SECURITY_INFORMATION,
517 sd, sd_size, &sd_size);
519 ERROR_WITH_ERRNO("Failed to get security information from "
521 ret = WIMLIB_ERR_NTFS_3G;
524 /*print_security_descriptor(sd, sd_size);*/
525 root->security_id = sd_set_add_sd(sd_set, sd, sd_size);
526 DEBUG("Added security ID = %u for `%s'",
527 root->security_id, path);
529 root->security_id = -1;
530 DEBUG("No security ID for `%s'", path);
537 static int build_dentry_tree_ntfs(struct dentry **root_p,
539 struct lookup_table *lookup_table,
540 struct wim_security_data *sd,
541 const struct capture_config *config,
548 struct sd_set sd_set;
551 ntfs_volume **ntfs_vol_p = extra_arg;
553 DEBUG("Mounting NTFS volume `%s' read-only", device);
555 vol = ntfs_mount(device, MS_RDONLY);
557 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s' read-only",
559 return WIMLIB_ERR_NTFS_3G;
562 NVolClearShowSysFiles(vol);
564 DEBUG("Opening root NTFS dentry");
565 root_ni = ntfs_inode_open(vol, FILE_root);
567 ERROR_WITH_ERRNO("Failed to open root inode of NTFS volume "
569 ret = WIMLIB_ERR_NTFS_3G;
572 char *path = MALLOC(32769);
574 ERROR("Could not allocate memory for NTFS pathname");
579 ret = build_dentry_tree_ntfs_recursive(root_p, root_ni, path, 1,
580 lookup_table, &sd_set,
584 ntfs_inode_close(root_ni);
585 destroy_sd_set(&sd_set);
589 if (ntfs_umount(vol, FALSE) != 0) {
590 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'",
593 ret = WIMLIB_ERR_NTFS_3G;
603 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
606 const char *config_str,
610 if (flags & (WIMLIB_ADD_IMAGE_FLAG_DEREFERENCE)) {
611 ERROR("Cannot dereference files when capturing directly from NTFS");
612 return WIMLIB_ERR_INVALID_PARAM;
614 return do_add_image(w, device, name, config_str, config_len, flags,
615 build_dentry_tree_ntfs, &w->ntfs_vol);
618 #else /* WITH_NTFS_3G */
619 WIMLIBAPI int wimlib_add_image_from_ntfs_volume(WIMStruct *w,
622 const char *description,
623 const char *flags_element,
625 const char *config_str,
628 ERROR("wimlib was compiled without support for NTFS-3g, so");
629 ERROR("we cannot capture a WIM image directly from a NTFS volume");
630 return WIMLIB_ERR_UNSUPPORTED;
632 #endif /* WITH_NTFS_3G */