4 * Capture a WIM image directly from an NTFS volume using libntfs-3g. We capture
5 * everything we can, including security data and alternate data streams.
9 * Copyright (C) 2012-2016 Eric Biggers
11 * This file is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 3 of the License, or (at your option) any
16 * This file is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this file; if not, see http://www.gnu.org/licenses/.
33 #include <ntfs-3g/attrib.h>
34 #include <ntfs-3g/object_id.h>
35 #include <ntfs-3g/reparse.h>
36 #include <ntfs-3g/security.h>
37 #include <ntfs-3g/volume.h>
39 #include "wimlib/alloca.h"
40 #include "wimlib/assert.h"
41 #include "wimlib/blob_table.h"
42 #include "wimlib/dentry.h"
43 #include "wimlib/encoding.h"
44 #include "wimlib/endianness.h"
45 #include "wimlib/error.h"
46 #include "wimlib/ntfs_3g.h"
47 #include "wimlib/object_id.h"
48 #include "wimlib/paths.h"
49 #include "wimlib/reparse.h"
50 #include "wimlib/scan.h"
51 #include "wimlib/security.h"
53 /* NTFS-3G 2013 renamed MS_RDONLY to NTFS_MNT_RDONLY. We can't check for the
54 * existence of NTFS_MNT_RDONLY at compilation time because it's an enum. We
55 * also can't check for MS_RDONLY being missing because it's also a system
56 * constant. So check if the NTFS-3G specific MS_IGNORE_HIBERFILE is defined;
57 * if yes, then we need to use the old MS_RDONLY. */
58 #ifdef MS_IGNORE_HIBERFILE
59 # define NTFS_MNT_RDONLY MS_RDONLY
62 /* A reference-counted NTFS volume than is automatically unmounted when the
63 * reference count reaches 0 */
64 struct ntfs_volume_wrapper {
69 /* Description of where data is located in an NTFS volume */
70 struct ntfs_location {
71 struct ntfs_volume_wrapper *volume;
79 static struct ntfs_volume_wrapper *
80 get_ntfs_volume(struct ntfs_volume_wrapper *volume)
87 put_ntfs_volume(struct ntfs_volume_wrapper *volume)
89 if (--volume->refcnt == 0) {
90 ntfs_umount(volume->vol, FALSE);
95 static inline const ntfschar *
96 attr_record_name(const ATTR_RECORD *record)
98 return (const ntfschar *)
99 ((const u8 *)record + le16_to_cpu(record->name_offset));
103 open_ntfs_attr(ntfs_inode *ni, const struct ntfs_location *loc)
107 na = ntfs_attr_open(ni, loc->attr_type, loc->attr_name,
108 loc->attr_name_nchars);
110 ERROR_WITH_ERRNO("Failed to open attribute of NTFS inode %"PRIu64,
117 read_ntfs_attribute_prefix(const struct blob_descriptor *blob, u64 size,
118 const struct read_blob_callbacks *cbs)
120 const struct ntfs_location *loc = blob->ntfs_loc;
121 ntfs_volume *vol = loc->volume->vol;
129 ni = ntfs_inode_open(vol, loc->mft_no);
131 ERROR_WITH_ERRNO("Failed to open NTFS inode %"PRIu64,
133 ret = WIMLIB_ERR_NTFS_3G;
137 na = open_ntfs_attr(ni, loc);
139 ret = WIMLIB_ERR_NTFS_3G;
140 goto out_close_ntfs_inode;
143 pos = (loc->attr_type == AT_REPARSE_POINT) ? REPARSE_DATA_OFFSET : 0;
144 bytes_remaining = size;
145 while (bytes_remaining) {
146 s64 to_read = min(bytes_remaining, sizeof(buf));
147 if (ntfs_attr_pread(na, pos, to_read, buf) != to_read) {
148 ERROR_WITH_ERRNO("Error reading data from NTFS inode "
149 "%"PRIu64, loc->mft_no);
150 ret = WIMLIB_ERR_NTFS_3G;
151 goto out_close_ntfs_attr;
154 bytes_remaining -= to_read;
155 ret = call_consume_chunk(buf, to_read, cbs);
157 goto out_close_ntfs_attr;
162 out_close_ntfs_inode:
163 ntfs_inode_close(ni);
169 free_ntfs_location(struct ntfs_location *loc)
171 put_ntfs_volume(loc->volume);
172 if (loc->attr_name != AT_UNNAMED)
173 FREE(loc->attr_name);
177 struct ntfs_location *
178 clone_ntfs_location(const struct ntfs_location *loc)
180 struct ntfs_location *new = memdup(loc, sizeof(*loc));
183 if (loc->attr_name != AT_UNNAMED) {
184 new->attr_name = utf16le_dup(loc->attr_name);
188 new->volume = get_ntfs_volume(loc->volume);
198 cmp_ntfs_locations(const struct ntfs_location *loc1,
199 const struct ntfs_location *loc2)
201 return cmp_u64(loc1->sort_key, loc2->sort_key);
204 /* Read rptag and rpreserved from the NTFS inode and save them in the WIM inode.
207 read_reparse_header(ntfs_inode *ni, struct wim_inode *inode)
217 na = ntfs_attr_open(ni, AT_REPARSE_POINT, AT_UNNAMED, 0);
219 return WIMLIB_ERR_NTFS_3G;
221 res = ntfs_attr_pread(na, 0, sizeof(hdr), &hdr);
225 if (res != sizeof(hdr))
226 return WIMLIB_ERR_NTFS_3G;
228 inode->i_reparse_tag = le32_to_cpu(hdr.rptag);
229 inode->i_rp_reserved = le16_to_cpu(hdr.rpreserved);
234 attr_type_to_wimlib_stream_type(ATTR_TYPES type)
238 return STREAM_TYPE_DATA;
239 case AT_REPARSE_POINT:
240 return STREAM_TYPE_REPARSE_POINT;
243 return STREAM_TYPE_UNKNOWN;
247 /* When sorting blobs located in NTFS volumes for sequential reading, we sort
248 * first by starting LCN of the attribute if available, otherwise no sort order
249 * is defined. This usually results in better sequential access to the volume.
252 set_attr_sort_key(ntfs_inode *ni, struct ntfs_location *loc)
257 na = open_ntfs_attr(ni, loc);
259 return WIMLIB_ERR_NTFS_3G;
261 rl = ntfs_attr_find_vcn(na, 0);
262 if (rl && rl->lcn != LCN_HOLE)
263 loc->sort_key = rl->lcn;
272 * Add a new stream to the specified inode, with duplicate checking.
274 * This works around a problem where NTFS-3G can list multiple unnamed data
275 * streams for a single file. In this case we can only keep one. We'll prefer
276 * one that is nonempty.
279 add_stream(struct wim_inode *inode, const char *path, int stream_type,
280 const utf16lechar *stream_name, struct blob_descriptor **blob_p,
281 struct list_head *unhashed_blobs)
283 struct blob_descriptor *blob = *blob_p;
284 struct wim_inode_stream *strm;
286 strm = inode_get_stream(inode, stream_type, stream_name);
287 if (unlikely(strm)) {
288 /* Stream already existed. */
291 if (stream_blob_resolved(strm)) {
292 WARNING("\"%s\" has multiple nonempty streams "
293 "with the same type and name! Only the first "
294 "will be saved.", path);
297 inode_replace_stream_blob(inode, strm, blob, NULL);
299 strm = inode_add_stream(inode, stream_type, stream_name, blob);
301 return WIMLIB_ERR_NOMEM;
303 prepare_unhashed_blob(blob, inode, strm->stream_id, unhashed_blobs);
309 /* Save information about an NTFS attribute (stream) to a WIM inode. */
311 scan_ntfs_attr(struct wim_inode *inode,
314 struct list_head *unhashed_blobs,
315 struct ntfs_volume_wrapper *volume,
317 const ATTR_RECORD *record)
319 u64 data_size = ntfs_get_attribute_value_length(record);
320 const u32 name_nchars = record->name_length;
321 struct blob_descriptor *blob = NULL;
322 utf16lechar *stream_name = (utf16lechar *)NO_STREAM_NAME;
325 if (unlikely(name_nchars)) {
327 stream_name = utf16le_dupz(attr_record_name(record),
328 name_nchars * sizeof(ntfschar));
330 ret = WIMLIB_ERR_NOMEM;
335 if (unlikely(type == AT_REPARSE_POINT)) {
336 if (data_size < REPARSE_DATA_OFFSET) {
337 ERROR("Reparse point attribute of \"%s\" "
338 "is too short!", path);
339 ret = WIMLIB_ERR_INVALID_REPARSE_DATA;
342 data_size -= REPARSE_DATA_OFFSET;
344 ret = read_reparse_header(ni, inode);
346 ERROR_WITH_ERRNO("Error reading reparse point header "
352 /* If the stream is non-empty, set up a blob descriptor for it. */
353 if (data_size != 0) {
354 blob = new_blob_descriptor();
355 if (unlikely(!blob)) {
356 ret = WIMLIB_ERR_NOMEM;
360 blob->ntfs_loc = MALLOC(sizeof(struct ntfs_location));
361 if (unlikely(!blob->ntfs_loc)) {
362 ret = WIMLIB_ERR_NOMEM;
366 blob->blob_location = BLOB_IN_NTFS_VOLUME;
367 blob->size = data_size;
368 blob->ntfs_loc->volume = get_ntfs_volume(volume);
369 blob->ntfs_loc->mft_no = ni->mft_no;
370 blob->ntfs_loc->attr_type = type;
372 if (unlikely(name_nchars)) {
373 blob->ntfs_loc->attr_name_nchars = name_nchars;
374 blob->ntfs_loc->attr_name = utf16le_dup(stream_name);
375 if (!blob->ntfs_loc->attr_name) {
376 ret = WIMLIB_ERR_NOMEM;
380 blob->ntfs_loc->attr_name_nchars = 0;
381 blob->ntfs_loc->attr_name = AT_UNNAMED;
384 ret = set_attr_sort_key(ni, blob->ntfs_loc);
389 ret = add_stream(inode, path, attr_type_to_wimlib_stream_type(type),
390 stream_name, &blob, unhashed_blobs);
392 free_blob_descriptor(blob);
393 if (stream_name != NO_STREAM_NAME)
398 /* Scan attributes of the specified type from a file in the NTFS volume */
400 scan_ntfs_attrs_with_type(struct wim_inode *inode,
403 struct list_head *unhashed_blobs,
404 struct ntfs_volume_wrapper *volume,
407 ntfs_attr_search_ctx *actx;
410 actx = ntfs_attr_get_search_ctx(ni, NULL);
412 ERROR_WITH_ERRNO("Failed to get NTFS attribute search "
413 "context for \"%s\"", path);
414 return WIMLIB_ERR_NTFS_3G;
417 while (!ntfs_attr_lookup(type, NULL, 0,
418 CASE_SENSITIVE, 0, NULL, 0, actx))
420 ret = scan_ntfs_attr(inode,
430 if (errno != ENOENT) {
431 ERROR_WITH_ERRNO("Error listing NTFS attributes of \"%s\"", path);
432 ret = WIMLIB_ERR_NTFS_3G;
437 ntfs_attr_put_search_ctx(actx);
441 static noinline_for_stack int
442 load_object_id(ntfs_inode *ni, struct wim_inode *inode)
447 len = ntfs_get_ntfs_object_id(ni, (char *)&attr, sizeof(attr));
448 if (likely(len == -ENODATA || len == 0))
451 return WIMLIB_ERR_NTFS_3G;
452 if (!inode_set_object_id(inode, &attr, len))
453 return WIMLIB_ERR_NOMEM;
457 /* Load the security descriptor of an NTFS inode into the corresponding WIM
458 * inode and the WIM image's security descriptor set. */
459 static noinline_for_stack int
460 get_security_descriptor(ntfs_inode *ni, struct wim_inode *inode,
461 ntfs_volume *vol, struct wim_sd_set *sd_set)
463 struct SECURITY_CONTEXT scx = {.vol = vol};
466 size_t avail_size = sizeof(_buf);
470 ret = ntfs_get_ntfs_acl(&scx, ni, buf, avail_size);
471 if (unlikely(ret < 0)) {
472 ret = WIMLIB_ERR_NTFS_3G;
476 if (unlikely(ret > avail_size)) {
477 if (unlikely(buf != _buf))
481 ret = WIMLIB_ERR_NOMEM;
488 if (likely(ret > 0)) {
489 inode->i_security_id = sd_set_add_sd(sd_set, buf, ret);
490 if (unlikely(inode->i_security_id < 0)) {
491 ret = WIMLIB_ERR_NOMEM;
498 if (unlikely(buf != _buf))
503 /* Binary tree that maps NTFS inode numbers to DOS names */
504 struct dos_name_map {
505 struct avl_tree_node *root;
508 struct dos_name_node {
509 struct avl_tree_node index_node;
515 #define DOS_NAME_NODE(avl_node) \
516 avl_tree_entry(avl_node, struct dos_name_node, index_node)
519 _avl_cmp_by_ntfs_ino(const struct avl_tree_node *n1,
520 const struct avl_tree_node *n2)
522 return cmp_u64(DOS_NAME_NODE(n1)->ntfs_ino,
523 DOS_NAME_NODE(n2)->ntfs_ino);
526 /* Inserts a new DOS name into the map */
528 insert_dos_name(struct dos_name_map *map, const ntfschar *dos_name,
529 size_t name_nbytes, u64 ntfs_ino)
531 struct dos_name_node *new_node;
533 new_node = MALLOC(sizeof(struct dos_name_node));
535 return WIMLIB_ERR_NOMEM;
537 /* DOS names are supposed to be 12 characters max (that's 24 bytes,
538 * assuming 2-byte ntfs characters) */
539 wimlib_assert(name_nbytes <= sizeof(new_node->dos_name));
541 /* Initialize the DOS name, DOS name length, and NTFS inode number of
542 * the search tree node */
543 memcpy(new_node->dos_name, dos_name, name_nbytes);
544 new_node->name_nbytes = name_nbytes;
545 new_node->ntfs_ino = ntfs_ino;
547 /* Insert the search tree node */
548 if (avl_tree_insert(&map->root, &new_node->index_node,
549 _avl_cmp_by_ntfs_ino))
551 /* This should be impossible since an NTFS inode cannot have
552 * multiple DOS names, and we only should get each DOS name
553 * entry once from the ntfs_readdir() calls. */
554 WARNING("NTFS inode %"PRIu64" has multiple DOS names", ntfs_ino);
560 /* Returns a structure that contains the DOS name and its length for an NTFS
561 * inode, or NULL if the inode has no DOS name. */
562 static struct dos_name_node *
563 lookup_dos_name(const struct dos_name_map *map, u64 ntfs_ino)
565 struct dos_name_node dummy;
566 struct avl_tree_node *res;
568 dummy.ntfs_ino = ntfs_ino;
570 res = avl_tree_lookup_node(map->root, &dummy.index_node,
571 _avl_cmp_by_ntfs_ino);
574 return DOS_NAME_NODE(res);
578 set_dentry_dos_name(struct wim_dentry *dentry, const struct dos_name_map *map)
580 const struct dos_name_node *node;
582 if (dentry->d_is_win32_name) {
583 node = lookup_dos_name(map, dentry->d_inode->i_ino);
585 dentry->d_short_name = utf16le_dupz(node->dos_name,
587 if (!dentry->d_short_name)
588 return WIMLIB_ERR_NOMEM;
589 dentry->d_short_name_nbytes = node->name_nbytes;
591 WARNING("NTFS inode %"PRIu64" has Win32 name with no "
592 "corresponding DOS name",
593 dentry->d_inode->i_ino);
600 destroy_dos_name_map(struct dos_name_map *map)
602 struct dos_name_node *node;
603 avl_tree_for_each_in_postorder(node, map->root,
604 struct dos_name_node, index_node)
609 struct wim_dentry *parent;
612 struct dos_name_map dos_name_map;
613 struct ntfs_volume_wrapper *volume;
614 struct scan_params *params;
619 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_p,
624 struct ntfs_volume_wrapper *volume,
625 struct scan_params *params);
628 filldir(void *_ctx, const ntfschar *name, const int name_nchars,
629 const int name_type, const s64 pos, const MFT_REF mref,
630 const unsigned dt_type)
632 struct readdir_ctx *ctx = _ctx;
633 const size_t name_nbytes = name_nchars * sizeof(ntfschar);
635 size_t mbs_name_nbytes;
637 struct wim_dentry *child;
640 if (name_type & FILE_NAME_DOS) {
641 /* If this is the entry for a DOS name, store it for later. */
642 ret = insert_dos_name(&ctx->dos_name_map, name,
643 name_nbytes, MREF(mref));
645 /* Return now if an error occurred or if this is just a DOS name
646 * and not a Win32+DOS name. */
647 if (ret != 0 || name_type == FILE_NAME_DOS)
651 ret = utf16le_to_tstr(name, name_nbytes, &mbs_name, &mbs_name_nbytes);
655 if (should_ignore_filename(mbs_name, mbs_name_nbytes))
656 goto out_free_mbs_name;
658 path_len = ctx->path_len;
660 ctx->path[path_len++] = '/';
661 memcpy(ctx->path + path_len, mbs_name, mbs_name_nbytes + 1);
662 path_len += mbs_name_nbytes;
664 ret = ntfs_3g_build_dentry_tree_recursive(&child, mref, ctx->path,
666 ctx->volume, ctx->params);
667 attach_scanned_tree(ctx->parent, child, ctx->params->blob_table);
676 ntfs_3g_recurse_directory(ntfs_inode *ni, char *path, size_t path_len,
677 struct wim_dentry *parent,
678 struct ntfs_volume_wrapper *volume,
679 struct scan_params *params)
683 struct readdir_ctx ctx = {
686 .path_len = path_len,
687 .dos_name_map = { .root = NULL },
692 ret = ntfs_readdir(ni, &pos, &ctx, filldir);
693 path[path_len] = '\0';
699 /* error from ntfs_readdir() itself */
700 ERROR_WITH_ERRNO("Error reading directory \"%s\"", path);
701 ret = WIMLIB_ERR_NTFS_3G;
704 struct wim_dentry *child;
707 for_dentry_child(child, parent) {
708 ret = set_dentry_dos_name(child, &ctx.dos_name_map);
713 destroy_dos_name_map(&ctx.dos_name_map);
718 ntfs_3g_build_dentry_tree_recursive(struct wim_dentry **root_ret,
723 struct ntfs_volume_wrapper *volume,
724 struct scan_params *params)
728 struct wim_dentry *root = NULL;
729 struct wim_inode *inode = NULL;
730 ntfs_inode *ni = NULL;
732 ret = try_exclude(path, params);
733 if (unlikely(ret < 0)) /* Excluded? */
735 if (unlikely(ret > 0)) /* Error? */
738 ni = ntfs_inode_open(volume->vol, mref);
740 ERROR_WITH_ERRNO("Failed to open NTFS file \"%s\"", path);
741 ret = WIMLIB_ERR_NTFS_3G;
745 /* Get file attributes */
746 ret = ntfs_get_ntfs_attrib(ni, (char*)&attributes, sizeof(attributes));
747 if (ret != sizeof(attributes)) {
748 ERROR_WITH_ERRNO("Failed to get NTFS attributes from \"%s\"", path);
749 ret = WIMLIB_ERR_NTFS_3G;
753 if (unlikely(attributes & FILE_ATTRIBUTE_ENCRYPTED)) {
754 if (params->add_flags & WIMLIB_ADD_FLAG_NO_UNSUPPORTED_EXCLUDE)
756 ERROR("Can't archive \"%s\" because NTFS-3G capture mode "
757 "does not support encrypted files and directories", path);
758 ret = WIMLIB_ERR_UNSUPPORTED_FILE;
761 params->progress.scan.cur_path = path;
762 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_UNSUPPORTED, NULL);
766 /* Create a WIM dentry with an associated inode, which may be shared */
767 ret = inode_table_new_dentry(params->inode_table,
768 path_basename_with_len(path, path_len),
769 ni->mft_no, 0, false, &root);
773 if (name_type & FILE_NAME_WIN32) /* Win32 or Win32+DOS name (rather than POSIX) */
774 root->d_is_win32_name = 1;
776 inode = root->d_inode;
778 if (inode->i_nlink > 1) {
779 /* Shared inode; nothing more to do */
783 inode->i_creation_time = le64_to_cpu(ni->creation_time);
784 inode->i_last_write_time = le64_to_cpu(ni->last_data_change_time);
785 inode->i_last_access_time = le64_to_cpu(ni->last_access_time);
786 inode->i_attributes = attributes;
788 if (attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
789 /* Scan the reparse point stream. */
790 ret = scan_ntfs_attrs_with_type(inode, ni, path,
791 params->unhashed_blobs,
792 volume, AT_REPARSE_POINT);
797 /* Load the object ID. */
798 ret = load_object_id(ni, inode);
800 ERROR_WITH_ERRNO("Error reading object ID of \"%s\"", path);
804 /* Scan the data streams.
806 * Note: directories should not have an unnamed data stream, but they
807 * may have named data streams. Nondirectories (including reparse
808 * points) can have an unnamed data stream as well as named data
810 ret = scan_ntfs_attrs_with_type(inode, ni, path, params->unhashed_blobs,
815 /* Reparse-point fixups are a no-op because in NTFS-3G capture mode we
816 * only allow capturing an entire volume. */
817 if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
818 inode_is_symlink(inode))
819 inode->i_rp_flags &= ~WIM_RP_FLAG_NOT_FIXED;
821 if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)) {
822 ret = get_security_descriptor(ni, inode, volume->vol,
825 ERROR_WITH_ERRNO("Error reading security descriptor "
831 if (inode_is_directory(inode)) {
832 ret = ntfs_3g_recurse_directory(ni, path, path_len, root,
839 params->progress.scan.cur_path = path;
841 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
843 ret = do_scan_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
846 ntfs_inode_close(ni);
848 free_dentry_tree(root, params->blob_table);
850 ret = report_scan_error(params, ret, path);
857 ntfs_3g_build_dentry_tree(struct wim_dentry **root_ret,
858 const char *device, struct scan_params *params)
860 struct ntfs_volume_wrapper *volume;
865 volume = MALLOC(sizeof(struct ntfs_volume_wrapper));
867 return WIMLIB_ERR_NOMEM;
869 vol = ntfs_mount(device, NTFS_MNT_RDONLY);
871 ERROR_WITH_ERRNO("Failed to mount NTFS volume \"%s\" read-only",
874 return WIMLIB_ERR_NTFS_3G;
880 ntfs_open_secure(vol);
882 /* We don't want to capture the special NTFS files such as $Bitmap. Not
883 * to be confused with "hidden" or "system" files which are real files
884 * that we do need to capture. */
885 NVolClearShowSysFiles(vol);
887 /* Currently we assume that all the paths fit into this length and there
888 * is no check for overflow. */
889 path = MALLOC(32768);
891 ret = WIMLIB_ERR_NOMEM;
892 goto out_put_ntfs_volume;
897 ret = ntfs_3g_build_dentry_tree_recursive(root_ret, FILE_root, path, 1,
898 FILE_NAME_POSIX, volume,
902 ntfs_index_ctx_put(vol->secure_xsii);
903 ntfs_index_ctx_put(vol->secure_xsdh);
904 ntfs_inode_close(vol->secure_ni);
905 put_ntfs_volume(volume);
908 #endif /* WITH_NTFS_3G */