]> wimlib.net Git - wimlib/blob - src/ntfs-apply.c
bf29014393dbaeec89d74621c4271f3e57ca8a8b
[wimlib] / src / ntfs-apply.c
1 /*
2  * ntfs-apply.c
3  *
4  * Apply a WIM image to a NTFS volume, restoring everything we can, including
5  * security data and alternate data streams.  There should be no loss of
6  * information.
7  */
8
9 /*
10  * Copyright (C) 2012 Eric Biggers
11  *
12  * This file is part of wimlib, a library for working with WIM files.
13  *
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)
17  * any later version.
18  *
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
22  * details.
23  *
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/.
26  */
27
28 #include "config.h"
29 #include "wimlib_internal.h"
30
31
32 #ifdef WITH_NTFS_3G
33 #include "dentry.h"
34 #include "lookup_table.h"
35 #include "io.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>
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 struct ntfs_apply_args {
47         ntfs_volume *vol;
48         int extract_flags;
49         WIMStruct *w;
50 };
51
52 extern int _ntfs_set_file_security(ntfs_inode *ni, u32 selection,
53                                    const char *attr);
54 extern int _ntfs_set_file_attributes(ntfs_inode *ni, s32 attrib);
55
56 /* 
57  * Extracts a WIM resource to a NTFS attribute.
58  */
59 static int
60 extract_wim_resource_to_ntfs_attr(const struct lookup_table_entry *lte,
61                                   ntfs_attr *na)
62 {
63         u64 bytes_remaining = wim_resource_size(lte);
64         char buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
65         u64 offset = 0;
66         int ret = 0;
67         u8 hash[SHA1_HASH_SIZE];
68
69         SHA_CTX ctx;
70         sha1_init(&ctx);
71
72         while (bytes_remaining) {
73                 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
74                 ret = read_wim_resource(lte, buf, to_read, offset, false);
75                 if (ret != 0)
76                         break;
77                 sha1_update(&ctx, buf, to_read);
78                 if (ntfs_attr_pwrite(na, offset, to_read, buf) != to_read) {
79                         ERROR_WITH_ERRNO("Error extracting WIM resource");
80                         return WIMLIB_ERR_WRITE;
81                 }
82                 bytes_remaining -= to_read;
83                 offset += to_read;
84         }
85         sha1_final(hash, &ctx);
86         if (!hashes_equal(hash, lte->hash)) {
87                 ERROR("Invalid checksum on a WIM resource "
88                       "(detected when extracting to NTFS stream file)");
89                 ERROR("The following WIM resource is invalid:");
90                 print_lookup_table_entry(lte);
91                 return WIMLIB_ERR_INVALID_RESOURCE_HASH;
92         }
93         return 0;
94 }
95
96 /* Writes the data streams to a NTFS file
97  *
98  * @ni:      The NTFS inode for the file.
99  * @dentry:  The directory entry in the WIM file.
100  * @w:       The WIMStruct for the WIM containing the image we are applying.
101  *
102  * Returns 0 on success, nonzero on failure.
103  */
104 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
105                                    WIMStruct *w)
106 {
107         int ret = 0;
108         unsigned stream_idx = 0;
109         ntfschar *stream_name = AT_UNNAMED;
110         u32 stream_name_len = 0;
111
112         DEBUG("Writing %u NTFS data stream%s for `%s'",
113               dentry->num_ads + 1,
114               (dentry->num_ads == 0 ? "" : "s"),
115               dentry->full_path_utf8);
116
117         while (1) {
118                 struct lookup_table_entry *lte;
119                 ntfs_attr *na;
120
121                 lte = dentry_stream_lte(dentry, stream_idx, w->lookup_table);
122                 na = ntfs_attr_open(ni, AT_DATA, stream_name, stream_name_len);
123                 if (!na) {
124                         ERROR_WITH_ERRNO("Failed to open a data stream of "
125                                          "extracted file `%s'",
126                                          dentry->full_path_utf8);
127                         ret = WIMLIB_ERR_NTFS_3G;
128                         break;
129                 }
130                 if (lte)
131                         ret = extract_wim_resource_to_ntfs_attr(lte, na);
132                 ntfs_attr_close(na);
133                 if (ret != 0)
134                         break;
135                 if (stream_idx == dentry->num_ads)
136                         break;
137                 stream_name = (ntfschar*)dentry->ads_entries[stream_idx].stream_name;
138                 stream_name_len = dentry->ads_entries[stream_idx].stream_name_len / 2;
139                 stream_idx++;
140         }
141         return ret;
142 }
143
144 /*
145  * Makes a NTFS hard link
146  *
147  * It is named @from_dentry->file_name and is located under the directory
148  * specified by @dir_ni, and it is made to point to the previously extracted
149  * file located at @to_dentry->extracted_file.
150  *
151  * Return 0 on success, nonzero on failure.
152  */
153 static int wim_apply_hardlink_ntfs(const struct dentry *from_dentry,
154                                    const struct dentry *to_dentry,
155                                    ntfs_inode *dir_ni)
156 {
157         int ret;
158         ntfs_inode *to_ni;
159
160         DEBUG("Extracting NTFS hard link `%s' => `%s'",
161               from_dentry->full_path_utf8, to_dentry->extracted_file);
162
163         to_ni = ntfs_pathname_to_inode(dir_ni->vol, NULL,
164                                        to_dentry->extracted_file);
165         if (!to_ni) {
166                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
167                                  to_dentry->extracted_file);
168                 return WIMLIB_ERR_NTFS_3G;
169         }
170         ret = ntfs_link(to_ni, dir_ni,
171                         (ntfschar*)from_dentry->file_name,
172                         from_dentry->file_name_len / 2);
173         if (ret != 0) {
174                 ERROR_WITH_ERRNO("Could not create hard link `%s' => `%s'",
175                                  from_dentry->full_path_utf8,
176                                  to_dentry->extracted_file);
177                 ret = WIMLIB_ERR_NTFS_3G;
178         }
179         if (ntfs_inode_close(to_ni) != 0) {
180                 ERROR_WITH_ERRNO("Failed to close NTFS inode for `%s'",
181                                  to_dentry->extracted_file);
182                 ret = WIMLIB_ERR_NTFS_3G;
183         }
184         return ret;
185 }
186
187 static int
188 apply_file_attributes_and_security_data(ntfs_inode *ni,
189                                         const struct dentry *dentry,
190                                         const WIMStruct *w)
191 {
192         DEBUG("Setting NTFS file attributes on `%s' to %#"PRIx32,
193               dentry->full_path_utf8, dentry->attributes);
194         if (!_ntfs_set_file_attributes(ni, dentry->attributes)) {
195                 ERROR("Failed to set NTFS file attributes on `%s'",
196                        dentry->full_path_utf8);
197                 return WIMLIB_ERR_NTFS_3G;
198         }
199
200         if (dentry->security_id != -1) {
201                 const struct wim_security_data *sd;
202                 
203                 sd = wim_const_security_data(w);
204                 wimlib_assert(dentry->security_id < sd->num_entries);
205                 DEBUG("Applying security descriptor %d to `%s'",
206                       dentry->security_id, dentry->full_path_utf8);
207                 u32 selection = OWNER_SECURITY_INFORMATION |
208                                 GROUP_SECURITY_INFORMATION |
209                                 DACL_SECURITY_INFORMATION  |
210                                 SACL_SECURITY_INFORMATION;
211                                 
212                 if (!_ntfs_set_file_security(ni, selection,
213                                              sd->descriptors[dentry->security_id]))
214                 {
215                         ERROR_WITH_ERRNO("Failed to set security data on `%s'",
216                                         dentry->full_path_utf8);
217                         return WIMLIB_ERR_NTFS_3G;
218                 }
219         }
220         return 0;
221 }
222
223 static int apply_reparse_data(ntfs_inode *ni, const struct dentry *dentry,
224                               const WIMStruct *w)
225 {
226         struct lookup_table_entry *lte;
227         int ret = 0;
228
229         wimlib_assert(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT);
230
231         lte = dentry_first_lte(dentry, w->lookup_table);
232
233         if (!lte) {
234                 ERROR("Could not find reparse data for `%s'",
235                       dentry->full_path_utf8);
236                 return WIMLIB_ERR_INVALID_DENTRY;
237         }
238
239         if (wim_resource_size(lte) >= 0xffff) {
240                 ERROR("Reparse data of `%s' is too long (%lu bytes)",
241                       dentry->full_path_utf8, wim_resource_size(lte));
242                 return WIMLIB_ERR_INVALID_DENTRY;
243         }
244
245         char reparse_data_buf[8 + wim_resource_size(lte)];
246         char *p = reparse_data_buf;
247         p = put_u32(p, dentry->reparse_tag); /* ReparseTag */
248         p = put_u16(p, wim_resource_size(lte)); /* ReparseDataLength */
249         p = put_u16(p, 0); /* Reserved */
250
251         ret = read_full_wim_resource(lte, p);
252         if (ret != 0)
253                 return ret;
254
255         ret = ntfs_set_ntfs_reparse_data(ni, reparse_data_buf,
256                                          wim_resource_size(lte) + 8, 0);
257         if (ret != 0) {
258                 ERROR_WITH_ERRNO("Failed to set NTFS reparse data on `%s'",
259                                  dentry->full_path_utf8);
260                 return WIMLIB_ERR_NTFS_3G;
261         }
262         return 0;
263 }
264
265 /* 
266  * Applies a WIM dentry to a NTFS filesystem.
267  *
268  * @dentry:  The WIM dentry to apply
269  * @dir_ni:  The NTFS inode for the parent directory
270  * @w:       The WIMStruct for the WIM containing the image we are applying.
271  *
272  * @return:  0 on success; nonzero on failure.
273  */
274 static int do_wim_apply_dentry_ntfs(struct dentry *dentry, ntfs_inode *dir_ni,
275                                     WIMStruct *w)
276 {
277         ntfs_inode *ni;
278         int ret = 0;
279         mode_t type;
280
281         if (dentry->attributes & FILE_ATTRIBUTE_DIRECTORY) {
282                 type = S_IFDIR;
283         } else {
284                 type = S_IFREG;
285 #if 0
286                 const struct list_head *head = &dentry->link_group_list;
287                 if (head->next != head) {
288                         /* This dentry is one of a hard link set of at least 2
289                          * dentries.  If one of the other dentries has already
290                          * been extracted, make a hard link to the file
291                          * corresponding to this already-extracted directory.
292                          * Otherwise, extract the file, and set the
293                          * dentry->extracted_file field so that other dentries
294                          * in the hard link group can link to it. */
295                         struct dentry *other;
296                         list_for_each_entry(other, head, link_group_list) {
297                                 if (other->extracted_file) {
298                                         return wim_apply_hardlink_ntfs(
299                                                         dentry, other, dir_ni);
300                                 }
301                         }
302                 }
303                 FREE(dentry->extracted_file);
304                 dentry->extracted_file = STRDUP(dentry->full_path_utf8);
305                 if (!dentry->extracted_file) {
306                         ERROR("Failed to allocate memory for filename");
307                         return WIMLIB_ERR_NOMEM;
308                 }
309 #endif
310         }
311
312         /* 
313          * Create a directory or file.
314          *
315          * Note: For symbolic links that are not directory junctions, pass
316          * S_IFREG here, since we manually set the reparse data later.
317          */
318         ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
319                          dentry->file_name_len / 2, type);
320
321         if (!ni) {
322                 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
323                                  dentry->full_path_utf8);
324                 ret = WIMLIB_ERR_NTFS_3G;
325                 goto out;
326         }
327
328         /* Write the data streams, unless this is a directory or reparse point
329          * */
330         if (!dentry_is_directory(dentry) &&
331              !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
332                 ret = write_ntfs_data_streams(ni, dentry, w);
333                 if (ret != 0)
334                         goto out;
335         }
336
337
338         ret = apply_file_attributes_and_security_data(ni, dentry, w);
339         if (ret != 0)
340                 goto out;
341
342         if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
343                 ret = apply_reparse_data(ni, dentry, w);
344                 if (ret != 0)
345                         goto out;
346         }
347
348         if (ntfs_inode_close_in_dir(ni, dir_ni) != 0) {
349                 ERROR_WITH_ERRNO("Failed to close new inode");
350                 ret = WIMLIB_ERR_NTFS_3G;
351                 goto out;
352         }
353 out:
354         return ret;
355 }
356
357 static int wim_apply_root_dentry_ntfs(const struct dentry *dentry,
358                                       ntfs_volume *vol,
359                                       const WIMStruct *w)
360 {
361         ntfs_inode *ni;
362         int ret = 0;
363
364         wimlib_assert(dentry_is_directory(dentry));
365         ni = ntfs_pathname_to_inode(vol, NULL, "/");
366         if (!ni) {
367                 ERROR_WITH_ERRNO("Could not find root NTFS inode");
368                 return WIMLIB_ERR_NTFS_3G;
369         }
370         ret = apply_file_attributes_and_security_data(ni, dentry, w);
371         if (ntfs_inode_close(ni) != 0) {
372                 ERROR_WITH_ERRNO("Failed to close NTFS inode for root "
373                                  "directory");
374                 ret = WIMLIB_ERR_NTFS_3G;
375         }
376         return ret;
377 }
378
379 /* Applies a WIM dentry to the NTFS volume */
380 static int wim_apply_dentry_ntfs(struct dentry *dentry, void *arg)
381 {
382         struct ntfs_apply_args *args = arg;
383         ntfs_volume *vol             = args->vol;
384         int extract_flags            = args->extract_flags;
385         WIMStruct *w                 = args->w;
386         ntfs_inode *dir_ni;
387         int ret;
388         char *p;
389         char orig;
390         const char *dir_name;
391
392         wimlib_assert(dentry->full_path_utf8);
393
394         DEBUG("Applying dentry `%s' to NTFS", dentry->full_path_utf8);
395
396         if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE)
397                 puts(dentry->full_path_utf8);
398
399         if (dentry_is_root(dentry))
400                 return wim_apply_root_dentry_ntfs(dentry, vol, w);
401
402         p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
403         do {
404                 p--;
405         } while (*p != '/');
406
407         orig = *p;
408         *p = '\0';
409         dir_name = dentry->full_path_utf8;
410
411         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
412         *p = orig;
413         if (!dir_ni) {
414                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
415                                  dir_name);
416                 return WIMLIB_ERR_NTFS_3G;
417         }
418         DEBUG("Found NTFS inode for `%s'", dir_name);
419
420         ret = do_wim_apply_dentry_ntfs(dentry, dir_ni, w);
421
422         if (ntfs_inode_close(dir_ni) != 0) {
423                 if (ret == 0)
424                         ret = WIMLIB_ERR_NTFS_3G;
425                 ERROR_WITH_ERRNO("Failed to close directory inode");
426         }
427         return ret;
428
429 }
430
431 static int do_wim_apply_image_ntfs(WIMStruct *w, const char *device, int extract_flags)
432 {
433         ntfs_volume *vol;
434         int ret;
435         
436         vol = ntfs_mount(device, 0);
437         if (!vol) {
438                 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
439                 return WIMLIB_ERR_NTFS_3G;
440         }
441         struct ntfs_apply_args args = {
442                 .vol           = vol,
443                 .extract_flags = extract_flags,
444                 .w             = w,
445         };
446         ret = for_dentry_in_tree(wim_root_dentry(w), wim_apply_dentry_ntfs,
447                                  &args);
448         if (ntfs_umount(vol, FALSE) != 0) {
449                 ERROR_WITH_ERRNO("Failed to unmount NTFS volume `%s'", device);
450                 if (ret == 0)
451                         ret = WIMLIB_ERR_NTFS_3G;
452         }
453         return ret;
454 }
455
456
457 /* 
458  * API entry point for applying a WIM image to a NTFS volume.
459  *
460  * Please note that this is a NTFS *volume* and not a directory.  The intention
461  * is that the volume contain an empty filesystem, and the WIM image contain a
462  * full filesystem to be applied to the volume.
463  */
464 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
465                                                 const char *device, int flags)
466 {
467         int ret;
468
469         if (!device)
470                 return WIMLIB_ERR_INVALID_PARAM;
471         if (image == WIM_ALL_IMAGES) {
472                 ERROR("Can only apply a single image when applying "
473                       "directly to a NTFS volume");
474                 return WIMLIB_ERR_INVALID_PARAM;
475         }
476         if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
477                 ERROR("Cannot specify symlink or hardlink flags when applying ");
478                 ERROR("directly to a NTFS volume");
479                 return WIMLIB_ERR_INVALID_PARAM;
480         }
481         ret = wimlib_select_image(w, image);
482         if (ret != 0)
483                 return ret;
484
485 #if 0
486         if (getuid() != 0) {
487                 ERROR("We are not root, but NTFS-3g requires root privileges to set arbitrary");
488                 ERROR("security data on the NTFS filesystem.  Please run this program as root");
489                 ERROR("if you want to extract a WIM image while preserving NTFS-specific");
490                 ERROR("information.");
491
492                 return WIMLIB_ERR_NOT_ROOT;
493         }
494 #endif
495         return do_wim_apply_image_ntfs(w, device, flags);
496 }
497
498 #else /* WITH_NTFS_3G */
499 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
500                                                 const char *device, int flags)
501 {
502         ERROR("wimlib was compiled without support for NTFS-3g, so");
503         ERROR("we cannot apply a WIM image directly to a NTFS volume");
504         return WIMLIB_ERR_UNSUPPORTED;
505 }
506 #endif /* WITH_NTFS_3G */