]> wimlib.net Git - wimlib/blob - src/ntfs.c
317f60d6904adf339323aedc727bc6a1703a2b63
[wimlib] / src / ntfs.c
1 /*
2  * Copyright (C) 2012 Eric Biggers
3  *
4  * This file is part of wimlib, a library for working with WIM files.
5  *
6  * wimlib is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU Lesser General Public License as published by the Free
8  * Software Foundation; either version 2.1 of the License, or (at your option)
9  * any later version.
10  *
11  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13  * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with wimlib; if not, see http://www.gnu.org/licenses/.
18  */
19
20 #include "config.h"
21 #include "wimlib_internal.h"
22
23
24 #ifdef WITH_NTFS_3G
25 #include "dentry.h"
26 #include "lookup_table.h"
27 #include <ntfs-3g/layout.h>
28 #include <ntfs-3g/acls.h>
29 #include <ntfs-3g/attrib.h>
30 #include <ntfs-3g/misc.h>
31 #include <ntfs-3g/reparse.h>
32 #include <ntfs-3g/security.h>
33 #include <ntfs-3g/volume.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36
37 struct ntfs_apply_args {
38         struct SECURITY_API *scapi;
39         int extract_flags;
40         WIMStruct *w;
41 };
42
43 extern int _ntfs_set_file_security(ntfs_volume *vol, ntfs_inode *ni,
44                                    u32 selection, const char *attr);
45 extern int _ntfs_set_file_attributes(ntfs_inode *ni, s32 attrib);
46
47 /*
48  *              Initializations before calling ntfs_get_file_security()
49  *      ntfs_set_file_security() and ntfs_read_directory()
50  *
51  *      Returns an (obscured) struct SECURITY_API* needed for further calls
52  *              NULL if device is mounted (EBUSY)
53  */
54
55 static struct SECURITY_API *_ntfs_initialize_file_security(const char *device,
56                                                            unsigned long flags)
57 {
58         ntfs_volume *vol;
59         unsigned long mntflag;
60         int mnt;
61         struct SECURITY_API *scapi;
62         struct SECURITY_CONTEXT *scx;
63
64         scapi = (struct SECURITY_API*)NULL;
65         mnt = ntfs_check_if_mounted(device, &mntflag);
66         if (!mnt && !(mntflag & NTFS_MF_MOUNTED)) {
67                 vol = ntfs_mount(device, flags);
68                 if (vol) {
69                         scapi = (struct SECURITY_API*)
70                                 ntfs_malloc(sizeof(struct SECURITY_API));
71                         if (!ntfs_volume_get_free_space(vol)
72                             && scapi) {
73                                 scapi->magic = MAGIC_API;
74                                 scapi->seccache = (struct PERMISSIONS_CACHE*)NULL;
75                                 scx = &scapi->security;
76                                 scx->vol = vol;
77                                 scx->uid = 0;
78                                 scx->gid = 0;
79                                 scx->pseccache = &scapi->seccache;
80                                 scx->vol->secure_flags = (1 << SECURITY_DEFAULT) |
81                                                         (1 << SECURITY_RAW);
82                                 ntfs_open_secure(vol);
83                                 ntfs_build_mapping(scx,(const char*)NULL,TRUE);
84                         } else {
85                                 if (scapi)
86                                         free(scapi);
87                                 else
88                                         errno = ENOMEM;
89                                 mnt = ntfs_umount(vol,FALSE);
90                                 scapi = (struct SECURITY_API*)NULL;
91                         }
92                 }
93         } else
94                 errno = EBUSY;
95         return (scapi);
96 }
97
98 /*
99  *      JPA NTFS constants or structs
100  *      should be moved to layout.h
101  */
102
103 #define ALIGN_SDS_BLOCK 0x40000 /* Alignment for a $SDS block */
104 #define ALIGN_SDS_ENTRY 16 /* Alignment for a $SDS entry */
105 #define STUFFSZ 0x4000 /* unitary stuffing size for $SDS */
106 #define FIRST_SECURITY_ID 0x100 /* Lowest security id */
107         /* Mask for attributes which can be forced */
108 #define FILE_ATTR_SETTABLE ( FILE_ATTR_READONLY         \
109                                 | FILE_ATTR_HIDDEN      \
110                                 | FILE_ATTR_SYSTEM      \
111                                 | FILE_ATTR_ARCHIVE     \
112                                 | FILE_ATTR_TEMPORARY   \
113                                 | FILE_ATTR_OFFLINE     \
114                                 | FILE_ATTR_NOT_CONTENT_INDEXED )
115
116
117
118 #if 0
119 #endif
120
121 static int extract_resource_to_ntfs_attr(WIMStruct *w, const struct resource_entry *entry, 
122                                          ntfs_attr *na)
123 {
124         u8 buf[min(entry->original_size, WIM_CHUNK_SIZE)];
125         u64 num_chunks = (entry->original_size + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
126         u64 n = WIM_CHUNK_SIZE;
127         int res_ctype = wim_resource_compression_type(w, entry);
128         u64 offset = 0;
129         for (u64 i = 0; i < num_chunks; i++) {
130                 DEBUG("Write chunk %u of %u", i + 1, num_chunks);
131                 int ret;
132                 if (i == num_chunks - 1) {
133                         n = entry->original_size % WIM_CHUNK_SIZE;
134                         if (n == 0) {
135                                 n = WIM_CHUNK_SIZE;
136                         }
137                 }
138
139                 ret = read_resource(w->fp, entry->size, entry->original_size,
140                                     entry->offset, res_ctype, n, offset, buf);
141                 if (ret != 0)
142                         return ret;
143
144                 if (ntfs_attr_pwrite(na, offset, n, buf) != n) {
145                         ERROR("Failed to write to NTFS data stream");
146                         return WIMLIB_ERR_WRITE;
147                 }
148                 offset += n;
149         }
150         return 0;
151 }
152
153 static int write_ntfs_data_streams(ntfs_inode *ni, const struct dentry *dentry,
154                                    WIMStruct *w)
155 {
156         ntfs_attr *na;
157         struct lookup_table_entry *lte;
158         int ret;
159
160
161         DEBUG("Writing NTFS data streams for `%s'", dentry->full_path_utf8);
162
163         wimlib_assert(dentry->num_ads == 0);
164
165         lte = dentry_stream_lte(dentry, 0, w->lookup_table);
166         if (lte && lte->resource_entry.original_size != 0) {
167
168                 na = ntfs_attr_open(ni, AT_DATA, AT_UNNAMED, 0);
169                 if (!na) {
170                         ERROR_WITH_ERRNO("Failed to open unnamed data stream of "
171                                          "extracted file `%s'",
172                                          dentry->full_path_utf8);
173                         return WIMLIB_ERR_NTFS_3G;
174                 }
175                 ret = extract_resource_to_ntfs_attr(w, &lte->resource_entry, na);
176                 if (ret != 0)
177                         return ret;
178                 ntfs_attr_close(na);
179         }
180
181         return 0;
182 }
183
184 static int __ntfs_apply_dentry(struct dentry *dentry, ntfs_inode *dir_ni,
185                                WIMStruct *w)
186 {
187         ntfs_inode *ni;
188         int ret;
189         mode_t type;
190
191         print_dentry(dentry, w->lookup_table);
192
193         if (dentry_is_directory(dentry))
194                 type = S_IFDIR;
195         else
196                 type = S_IFREG;
197
198         ni = ntfs_create(dir_ni, 0, (ntfschar*)dentry->file_name,
199                          dentry->file_name_len / 2, type);
200
201         if (!ni) {
202                 ERROR_WITH_ERRNO("Could not create NTFS object for `%s'",
203                                  dentry->full_path_utf8);
204                 ret = WIMLIB_ERR_NTFS_3G;
205                 goto out;
206         }
207
208         if (!dentry_is_directory(dentry) &&
209              !(dentry->attributes & FILE_ATTRIBUTE_REPARSE_POINT)) {
210                 ret = write_ntfs_data_streams(ni, dentry, w);
211                 if (ret != 0)
212                         goto out;
213         }
214
215         DEBUG("Setting file attributes 0x%x on `%s'",
216               dentry->attributes,
217               dentry->full_path_utf8);
218
219         if (!_ntfs_set_file_attributes(ni, dentry->attributes)) {
220                 ERROR("Failed to set NTFS file attributes on `%s'",
221                        dentry->full_path_utf8);
222                 ret = WIMLIB_ERR_NTFS_3G;
223                 goto out;
224         }
225
226         if (dentry->security_id != -1) {
227                 const struct wim_security_data *sd = wim_security_data(w);
228                 wimlib_assert(dentry->security_id < sd->num_entries);
229                 DEBUG("Applying security descriptor %d to `%s'",
230                       dentry->security_id, dentry->full_path_utf8);
231                 if (!_ntfs_set_file_security(ni->vol, ni, ~0,
232                                              sd->descriptors[dentry->security_id]))
233                 {
234                         ERROR_WITH_ERRNO("Failed to set security data on `%s'",
235                                         dentry->full_path_utf8);
236                         ret = WIMLIB_ERR_NTFS_3G;
237                         goto out;
238                 }
239         }
240
241         if (dentry->attributes & FILE_ATTR_REPARSE_POINT) {
242                 struct lookup_table_entry *lte;
243                 ntfs_inode *ni;
244                 lte = dentry_first_lte(dentry, w->lookup_table);
245                 if (!lte) {
246                         ERROR("Could not find reparse data for `%s'",
247                               dentry->full_path_utf8);
248                         ret = WIMLIB_ERR_INVALID_DENTRY;
249                         goto out;
250                 }
251                 
252                 ret = ntfs_set_ntfs_reparse_data(ni, lte->symlink_buf,
253                                                  lte->resource_entry.original_size,
254                                                  0);
255                 if (ret != 0) {
256                         ERROR_WITH_ERRNO("Failed to set NTFS reparse data on "
257                                          "`%s'", dentry->full_path_utf8);
258                         ret = WIMLIB_ERR_NTFS_3G;
259                         goto out;
260                 }
261         }
262
263         if (ntfs_inode_close_in_dir(ni, dir_ni) != 0) {
264                 ERROR_WITH_ERRNO("Failed to close new inode");
265                 ret = WIMLIB_ERR_NTFS_3G;
266                 goto out;
267         }
268 out:
269         return ret;
270 }
271
272 static int ntfs_apply_dentry(struct dentry *dentry, void *arg)
273 {
274         struct ntfs_apply_args *args = arg;
275         struct SECURITY_API *scapi   = args->scapi;
276         ntfs_volume *vol             = scapi->security.vol;
277         int extract_flags            = args->extract_flags;
278         WIMStruct *w                 = args->w;
279         ntfs_inode *dir_ni;
280         int ret;
281
282         DEBUG("Applying `%s'", dentry->full_path_utf8);
283
284         if (dentry_is_root(dentry))
285                 return 0;
286
287         if (extract_flags & WIMLIB_EXTRACT_FLAG_VERBOSE) {
288                 wimlib_assert(dentry->full_path_utf8);
289                 puts(dentry->full_path_utf8);
290         }
291
292         char *p = dentry->full_path_utf8 + dentry->full_path_utf8_len;
293         do {
294                 p--;
295         } while (*p != '/');
296
297         char orig = *p;
298         *p = '\0';
299         const char *dir_name = dentry->full_path_utf8;
300
301         dir_ni = ntfs_pathname_to_inode(vol, NULL, dir_name);
302         *p = orig;
303         if (!dir_ni) {
304                 ERROR_WITH_ERRNO("Could not find NTFS inode for `%s'",
305                                  dir_name);
306                 return WIMLIB_ERR_NTFS_3G;
307         }
308         DEBUG("Found NTFS inode for `%s'", dir_name);
309
310         ret = __ntfs_apply_dentry(dentry, dir_ni, w);
311
312         if (ntfs_inode_close(dir_ni) != 0) {
313                 if (ret == 0)
314                         ret = WIMLIB_ERR_NTFS_3G;
315                 ERROR_WITH_ERRNO("Failed to close directory inode");
316         }
317         return ret;
318
319 }
320
321 static int do_ntfs_apply(WIMStruct *w, const char *device, int extract_flags)
322 {
323         struct SECURITY_API *scapi;
324         int ret;
325         
326         scapi = _ntfs_initialize_file_security(device, 0);
327         if (!scapi) {
328                 ERROR_WITH_ERRNO("Failed to mount NTFS volume `%s'", device);
329                 return WIMLIB_ERR_NTFS_3G;
330         }
331         struct ntfs_apply_args args = {
332                 .scapi         = scapi,
333                 .extract_flags = extract_flags,
334                 .w             = w,
335         };
336         ret = for_dentry_in_tree(wim_root_dentry(w), ntfs_apply_dentry,
337                                  &args);
338         if (!ntfs_leave_file_security(scapi)) {
339                 ERROR_WITH_ERRNO("Failed to unmount NTFS volume");
340                 ret = WIMLIB_ERR_NTFS_3G;
341         }
342         return ret;
343 }
344
345 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
346                                                 const char *device, int flags)
347 {
348         int ret;
349
350         if (!device)
351                 return WIMLIB_ERR_INVALID_PARAM;
352         if (image == WIM_ALL_IMAGES) {
353                 ERROR("Can only apply a single image when applying "
354                       "directly to a NTFS volume");
355                 return WIMLIB_ERR_INVALID_PARAM;
356         }
357         if (flags & (WIMLIB_EXTRACT_FLAG_SYMLINK | WIMLIB_EXTRACT_FLAG_HARDLINK)) {
358                 ERROR("Cannot specifcy symlink or hardlink flags when applying ");
359                 ERROR("directly to a NTFS volume");
360                 return WIMLIB_ERR_INVALID_PARAM;
361         }
362         ret = wimlib_select_image(w, image);
363         if (ret != 0)
364                 return ret;
365
366         /*if (getuid() != 0) {*/
367                 /*ERROR("We are not root, but NTFS-3g requires root privileges to set arbitrary");*/
368                 /*ERROR("security data on the NTFS filesystem.  Please run this program as root");*/
369                 /*ERROR("if you want to extract a WIM image while preserving NTFS-specific");*/
370                 /*ERROR("information.");*/
371
372                 /*return WIMLIB_ERR_NOT_ROOT;*/
373         /*}*/
374         return do_ntfs_apply(w, device, flags);
375 }
376
377 #else /* WITH_NTFS_3G */
378 WIMLIBAPI int wimlib_apply_image_to_ntfs_volume(WIMStruct *w, int image,
379                                                 const char *device, int flags)
380 {
381         ERROR("wimlib was compiled without support for NTFS-3g, so");
382         ERROR("we cannot apply a WIM image directly to a NTFS volume");
383         return WIMLIB_ERR_UNSUPPORTED;
384 }
385 #endif /* WITH_NTFS_3G */