]> wimlib.net Git - wimlib/blob - src/reparse.c
Remove unnecessary argument to hlist iteration macros
[wimlib] / src / reparse.c
1 /*
2  * reparse.c - Handle reparse data.
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU Lesser General Public License as published by the Free
10  * Software Foundation; either version 3 of the License, or (at your option) any
11  * later version.
12  *
13  * This file is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this file; if not, see http://www.gnu.org/licenses/.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25
26 #include <errno.h>
27
28 #include "wimlib/alloca.h"
29 #include "wimlib/assert.h"
30 #include "wimlib/blob_table.h"
31 #include "wimlib/compiler.h"
32 #include "wimlib/endianness.h"
33 #include "wimlib/encoding.h"
34 #include "wimlib/error.h"
35 #include "wimlib/inode.h"
36 #include "wimlib/reparse.h"
37 #include "wimlib/resource.h"
38
39 /*
40  * Read the data from a symbolic link, junction, or mount point reparse point
41  * buffer into a `struct reparse_data'.
42  *
43  * See http://msdn.microsoft.com/en-us/library/cc232006(v=prot.10).aspx for a
44  * description of the format of the reparse point buffers.
45  */
46 int
47 parse_reparse_data(const u8 * restrict rpbuf, u16 rpbuflen,
48                    struct reparse_data * restrict rpdata)
49 {
50         u16 substitute_name_offset;
51         u16 print_name_offset;
52         const struct reparse_buffer_disk *rpbuf_disk =
53                 (const struct reparse_buffer_disk*)rpbuf;
54         const u8 *data;
55
56         memset(rpdata, 0, sizeof(*rpdata));
57         if (rpbuflen < 16)
58                 goto out_invalid;
59         rpdata->rptag = le32_to_cpu(rpbuf_disk->rptag);
60         wimlib_assert(rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK ||
61                       rpdata->rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT);
62         rpdata->rpdatalen = le16_to_cpu(rpbuf_disk->rpdatalen);
63         rpdata->rpreserved = le16_to_cpu(rpbuf_disk->rpreserved);
64         substitute_name_offset = le16_to_cpu(rpbuf_disk->symlink.substitute_name_offset);
65         rpdata->substitute_name_nbytes = le16_to_cpu(rpbuf_disk->symlink.substitute_name_nbytes);
66         print_name_offset = le16_to_cpu(rpbuf_disk->symlink.print_name_offset);
67         rpdata->print_name_nbytes = le16_to_cpu(rpbuf_disk->symlink.print_name_nbytes);
68
69         if ((substitute_name_offset & 1) | (print_name_offset & 1) |
70             (rpdata->substitute_name_nbytes & 1) | (rpdata->print_name_nbytes & 1))
71         {
72                 /* Names would be unaligned... */
73                 goto out_invalid;
74         }
75
76         if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK) {
77                 if (rpbuflen < 20)
78                         goto out_invalid;
79                 rpdata->rpflags = le32_to_cpu(rpbuf_disk->symlink.rpflags);
80                 data = rpbuf_disk->symlink.data;
81         } else {
82                 data = rpbuf_disk->junction.data;
83         }
84         if ((size_t)substitute_name_offset + rpdata->substitute_name_nbytes +
85             (data - rpbuf) > rpbuflen)
86                 goto out_invalid;
87         if ((size_t)print_name_offset + rpdata->print_name_nbytes +
88             (data - rpbuf) > rpbuflen)
89                 goto out_invalid;
90         rpdata->substitute_name = (utf16lechar*)&data[substitute_name_offset];
91         rpdata->print_name = (utf16lechar*)&data[print_name_offset];
92         return 0;
93 out_invalid:
94         ERROR("Invalid reparse data");
95         return WIMLIB_ERR_INVALID_REPARSE_DATA;
96 }
97
98 /*
99  * Create a reparse point data buffer.
100  *
101  * @rpdata:  Structure that contains the data we need.
102  *
103  * @rpbuf:     Buffer into which to write the reparse point data buffer.  Must be
104  *              at least REPARSE_POINT_MAX_SIZE bytes long.
105  */
106 int
107 make_reparse_buffer(const struct reparse_data * restrict rpdata,
108                     u8 * restrict rpbuf,
109                     u16 * restrict rpbuflen_ret)
110 {
111         struct reparse_buffer_disk *rpbuf_disk =
112                 (struct reparse_buffer_disk*)rpbuf;
113         u8 *data;
114
115         if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK)
116                 data = rpbuf_disk->symlink.data;
117         else
118                 data = rpbuf_disk->junction.data;
119
120         if ((data - rpbuf) + rpdata->substitute_name_nbytes +
121             rpdata->print_name_nbytes +
122             2 * sizeof(utf16lechar) > REPARSE_POINT_MAX_SIZE)
123         {
124                 ERROR("Reparse data is too long!");
125                 return WIMLIB_ERR_INVALID_REPARSE_DATA;
126         }
127
128         rpbuf_disk->rptag = cpu_to_le32(rpdata->rptag);
129         rpbuf_disk->rpreserved = cpu_to_le16(rpdata->rpreserved);
130         rpbuf_disk->symlink.substitute_name_offset = cpu_to_le16(0);
131         rpbuf_disk->symlink.substitute_name_nbytes = cpu_to_le16(rpdata->substitute_name_nbytes);
132         rpbuf_disk->symlink.print_name_offset = cpu_to_le16(rpdata->substitute_name_nbytes + 2);
133         rpbuf_disk->symlink.print_name_nbytes = cpu_to_le16(rpdata->print_name_nbytes);
134
135         if (rpdata->rptag == WIM_IO_REPARSE_TAG_SYMLINK)
136                 rpbuf_disk->symlink.rpflags = cpu_to_le32(rpdata->rpflags);
137
138         /* We null-terminate the substitute and print names, although this may
139          * not be strictly necessary.  Note that the byte counts should not
140          * include the null terminators. */
141         data = mempcpy(data, rpdata->substitute_name, rpdata->substitute_name_nbytes);
142         *(utf16lechar*)data = cpu_to_le16(0);
143         data += 2;
144         data = mempcpy(data, rpdata->print_name, rpdata->print_name_nbytes);
145         *(utf16lechar*)data = cpu_to_le16(0);
146         data += 2;
147         rpbuf_disk->rpdatalen = cpu_to_le16(data - rpbuf - 8);
148         *rpbuflen_ret = data - rpbuf;
149         return 0;
150 }
151
152 /*
153  * Read the reparse data from a WIM inode that is a reparse point.
154  *
155  * @rpbuf points to a buffer at least REPARSE_POINT_MAX_SIZE bytes into which
156  * the reparse point data buffer will be reconstructed.
157  *
158  * Note: in the WIM format, the first 8 bytes of the reparse point data buffer
159  * are omitted, presumably because we already know the reparse tag from the
160  * dentry, and we already know the reparse tag length from the blob length.
161  * However, we reconstruct the first 8 bytes in the buffer returned by this
162  * function.
163  */
164 static int
165 wim_inode_get_reparse_data(const struct wim_inode * restrict inode,
166                            u8 * restrict rpbuf,
167                            u16 * restrict rpbuflen_ret,
168                            struct blob_descriptor *blob_override)
169 {
170         struct blob_descriptor *blob;
171         int ret;
172         struct reparse_buffer_disk *rpbuf_disk;
173         u16 rpdatalen;
174
175         wimlib_assert(inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT);
176
177         if (blob_override) {
178                 blob = blob_override;
179         } else {
180                 struct wim_inode_stream *strm;
181
182                 strm = inode_get_stream(inode, STREAM_TYPE_REPARSE_POINT,
183                                         NO_STREAM_NAME);
184                 if (strm)
185                         blob = stream_blob_resolved(strm);
186                 else
187                         blob = NULL;
188                 if (!blob) {
189                         ERROR("Reparse point has no reparse data!");
190                         return WIMLIB_ERR_INVALID_REPARSE_DATA;
191                 }
192         }
193
194         if (blob->size > REPARSE_DATA_MAX_SIZE) {
195                 ERROR("Reparse data is too long!");
196                 return WIMLIB_ERR_INVALID_REPARSE_DATA;
197         }
198         rpdatalen = blob->size;
199
200         /* Read the reparse data from blob  */
201         ret = read_full_blob_into_buf(blob, rpbuf + REPARSE_DATA_OFFSET);
202         if (ret)
203                 return ret;
204
205         /* Reconstruct the first 8 bytes of the reparse point buffer */
206         rpbuf_disk = (struct reparse_buffer_disk*)rpbuf;
207
208         /* ReparseTag */
209         rpbuf_disk->rptag = cpu_to_le32(inode->i_reparse_tag);
210
211         /* ReparseDataLength */
212         rpbuf_disk->rpdatalen = cpu_to_le16(rpdatalen);
213
214         /* ReparseReserved
215          * XXX this could be one of the unknown fields in the WIM dentry. */
216         rpbuf_disk->rpreserved = cpu_to_le16(0);
217
218         *rpbuflen_ret = rpdatalen + 8;
219         return 0;
220 }
221
222 /* UNIX version of getting and setting the data in reparse points */
223 #ifndef __WIN32__
224
225 static const utf16lechar volume_junction_prefix[11] = {
226         cpu_to_le16('\\'),
227         cpu_to_le16('?'),
228         cpu_to_le16('?'),
229         cpu_to_le16('\\'),
230         cpu_to_le16('V'),
231         cpu_to_le16('o'),
232         cpu_to_le16('l'),
233         cpu_to_le16('u'),
234         cpu_to_le16('m'),
235         cpu_to_le16('e'),
236         cpu_to_le16('{'),
237 };
238
239 enum {
240         SUBST_NAME_IS_RELATIVE_LINK = -1,
241         SUBST_NAME_IS_VOLUME_JUNCTION = -2,
242         SUBST_NAME_IS_UNKNOWN = -3,
243 };
244
245 /* Parse the "substitute name" (link target) from a symbolic link or junction
246  * reparse point.
247  *
248  * Return value is:
249  *
250  * Non-negative integer:
251  *      The name is an absolute symbolic link in one of several formats,
252  *      and the return value is the number of UTF-16LE characters that need to
253  *      be advanced to reach a simple "absolute" path starting with a backslash
254  *      (i.e. skip over \??\ and/or drive letter)
255  * Negative integer:
256  *      SUBST_NAME_IS_VOLUME_JUNCTION:
257  *              The name is a volume junction.
258  *      SUBST_NAME_IS_RELATIVE_LINK:
259  *              The name is a relative symbolic link.
260  *      SUBST_NAME_IS_UNKNOWN:
261  *              The name does not appear to be a valid symbolic link, junction,
262  *              or mount point.
263  */
264 static int
265 parse_substitute_name(const utf16lechar *substitute_name,
266                       u16 substitute_name_nbytes, u32 rptag)
267 {
268         u16 substitute_name_nchars = substitute_name_nbytes / 2;
269
270         if (substitute_name_nchars >= 7 &&
271             substitute_name[0] == cpu_to_le16('\\') &&
272             substitute_name[1] == cpu_to_le16('?') &&
273             substitute_name[2] == cpu_to_le16('?') &&
274             substitute_name[3] == cpu_to_le16('\\') &&
275             substitute_name[4] != cpu_to_le16('\0') &&
276             substitute_name[5] == cpu_to_le16(':') &&
277             substitute_name[6] == cpu_to_le16('\\'))
278         {
279                 /* "Full" symlink or junction (\??\x:\ prefixed path) */
280                 return 6;
281         } else if (rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT &&
282                    substitute_name_nchars >= 12 &&
283                    memcmp(substitute_name, volume_junction_prefix,
284                           sizeof(volume_junction_prefix)) == 0 &&
285                    substitute_name[substitute_name_nchars - 1] == cpu_to_le16('\\'))
286         {
287                 /* Volume junction.  Can't really do anything with it. */
288                 return SUBST_NAME_IS_VOLUME_JUNCTION;
289         } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
290                    substitute_name_nchars >= 3 &&
291                    substitute_name[0] != cpu_to_le16('\0') &&
292                    substitute_name[1] == cpu_to_le16(':') &&
293                    substitute_name[2] == cpu_to_le16('\\'))
294         {
295                 /* "Absolute" symlink, with drive letter */
296                 return 2;
297         } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
298                    substitute_name_nchars >= 1)
299         {
300                 if (substitute_name[0] == cpu_to_le16('\\'))
301                         /* "Absolute" symlink, without drive letter */
302                         return 0;
303                 else
304                         /* "Relative" symlink, without drive letter */
305                         return SUBST_NAME_IS_RELATIVE_LINK;
306         } else {
307                 return SUBST_NAME_IS_UNKNOWN;
308         }
309 }
310
311 /*
312  * Get the UNIX-style symlink target from the WIM inode for a reparse point.
313  * Specifically, this translates the target from UTF-16 to the current multibyte
314  * encoding, strips the drive prefix if present, and replaces backslashes with
315  * forward slashes.
316  *
317  * @inode
318  *      The inode to read the symlink from.  It must be a reparse point with
319  *      tag WIM_IO_REPARSE_TAG_SYMLINK (a real symlink) or
320  *      WIM_IO_REPARSE_TAG_MOUNT_POINT (a mount point or junction point).
321  *
322  * @buf
323  *      Buffer into which to place the link target.
324  *
325  * @bufsize
326  *      Available space in @buf, in bytes.
327  *
328  * @blob_override
329  *      If not NULL, the blob from which to read the reparse data.  Otherwise,
330  *      the reparse data will be read from the reparse point stream of @inode.
331  *
332  * If the entire symbolic link target was placed in the buffer, returns the
333  * number of bytes written.  The resulting string is not null-terminated.  If
334  * the symbolic link target was too large to be placed in the buffer, the first
335  * @bufsize bytes of it are placed in the buffer and
336  * -ENAMETOOLONG is returned.  Otherwise, a negative errno value indicating
337  *  another error is returned.
338  */
339 ssize_t
340 wim_inode_readlink(const struct wim_inode * restrict inode,
341                    char * restrict buf, size_t bufsize,
342                    struct blob_descriptor *blob_override)
343 {
344         int ret;
345         struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
346         struct reparse_data rpdata;
347         char *link_target;
348         char *translated_target;
349         size_t link_target_len;
350         u16 rpbuflen;
351
352         wimlib_assert(inode_is_symlink(inode));
353
354         if (wim_inode_get_reparse_data(inode, (u8*)&rpbuf_disk, &rpbuflen,
355                                        blob_override))
356                 return -EIO;
357
358         if (parse_reparse_data((const u8*)&rpbuf_disk, rpbuflen, &rpdata))
359                 return -EINVAL;
360
361         ret = utf16le_to_tstr(rpdata.substitute_name,
362                               rpdata.substitute_name_nbytes,
363                               &link_target, &link_target_len);
364         if (ret)
365                 return -errno;
366
367         translated_target = link_target;
368         ret = parse_substitute_name(rpdata.substitute_name,
369                                     rpdata.substitute_name_nbytes,
370                                     rpdata.rptag);
371         switch (ret) {
372         case SUBST_NAME_IS_RELATIVE_LINK:
373                 goto out_translate_slashes;
374         case SUBST_NAME_IS_VOLUME_JUNCTION:
375                 goto out_have_link;
376         case SUBST_NAME_IS_UNKNOWN:
377                 ERROR("Can't understand reparse point "
378                       "substitute name \"%s\"", link_target);
379                 ret = -EIO;
380                 goto out_free_link_target;
381         default:
382                 translated_target += ret;
383                 link_target_len -= ret;
384                 break;
385         }
386
387 out_translate_slashes:
388         for (size_t i = 0; i < link_target_len; i++)
389                 if (translated_target[i] == '\\')
390                         translated_target[i] = '/';
391 out_have_link:
392         if (link_target_len > bufsize) {
393                 link_target_len = bufsize;
394                 ret = -ENAMETOOLONG;
395         } else {
396                 ret = link_target_len;
397         }
398         memcpy(buf, translated_target, link_target_len);
399 out_free_link_target:
400         FREE(link_target);
401         return ret;
402 }
403
404 /* Given a UNIX-style symbolic link target, create a Windows-style reparse point
405  * buffer and assign it to the specified inode.  */
406 int
407 wim_inode_set_symlink(struct wim_inode *inode, const char *target,
408                       struct blob_table *blob_table)
409
410 {
411         struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
412         struct reparse_data rpdata;
413         static const char abs_subst_name_prefix[12] = "\\\0?\0?\0\\\0C\0:\0";
414         static const char abs_print_name_prefix[4] = "C\0:\0";
415         utf16lechar *name_utf16le;
416         size_t name_utf16le_nbytes;
417         int ret;
418         u16 rpbuflen;
419
420         DEBUG("Creating reparse point data buffer for UNIX "
421               "symlink target \"%s\"", target);
422         memset(&rpdata, 0, sizeof(rpdata));
423         ret = tstr_to_utf16le(target, strlen(target),
424                               &name_utf16le, &name_utf16le_nbytes);
425         if (ret)
426                 return ret;
427
428         for (size_t i = 0; i < name_utf16le_nbytes / 2; i++)
429                 if (name_utf16le[i] == cpu_to_le16('/'))
430                         name_utf16le[i] = cpu_to_le16('\\');
431
432         /* Compatability notes:
433          *
434          * On UNIX, an absolute symbolic link begins with '/'; everything else
435          * is a relative symbolic link.  (Quite simple compared to the various
436          * ways to provide Windows paths.)
437          *
438          * To change a UNIX relative symbolic link to Windows format, we only
439          * need to translate it to UTF-16LE and replace forward slashes with
440          * backslashes.  We do not make any attempt to handle filename character
441          * problems, such as a link target that itself contains backslashes on
442          * UNIX.  Then, for these relative links, we set the reparse header
443          * @flags field to SYMBOLIC_LINK_RELATIVE.
444          *
445          * For UNIX absolute symbolic links, we must set the @flags field to 0.
446          * Then, there are multiple options as to actually represent the
447          * absolute link targets:
448          *
449          * (1) An absolute path beginning with one backslash character. similar
450          * to UNIX-style, just with a different path separator.  Print name same
451          * as substitute name.
452          *
453          * (2) Absolute path beginning with drive letter followed by a
454          * backslash.  Print name same as substitute name.
455          *
456          * (3) Absolute path beginning with drive letter followed by a
457          * backslash; substitute name prefixed with \??\, otherwise same as
458          * print name.
459          *
460          * We choose option (3) here, and we just assume C: for the drive
461          * letter.  The reasoning for this is:
462          *
463          * (1) Microsoft imagex.exe has a bug where it does not attempt to do
464          * reparse point fixups for these links, even though they are valid
465          * absolute links.  (Note: in this case prefixing the substitute name
466          * with \??\ does not work; it just makes the data unable to be restored
467          * at all.)
468          * (2) Microsoft imagex.exe will fail when doing reparse point fixups
469          * for these.  It apparently contains a bug that causes it to create an
470          * invalid reparse point, which then cannot be restored.
471          * (3) This is the only option I tested for which reparse point fixups
472          * worked properly in Microsoft imagex.exe.
473          *
474          * So option (3) it is.
475          */
476
477         rpdata.rptag = inode->i_reparse_tag;
478         if (target[0] == '/') {
479                 rpdata.substitute_name_nbytes = name_utf16le_nbytes +
480                                                 sizeof(abs_subst_name_prefix);
481                 rpdata.print_name_nbytes = name_utf16le_nbytes +
482                                            sizeof(abs_print_name_prefix);
483                 rpdata.substitute_name = alloca(rpdata.substitute_name_nbytes);
484                 rpdata.print_name = alloca(rpdata.print_name_nbytes);
485                 memcpy(rpdata.substitute_name, abs_subst_name_prefix,
486                        sizeof(abs_subst_name_prefix));
487                 memcpy(rpdata.print_name, abs_print_name_prefix,
488                        sizeof(abs_print_name_prefix));
489                 memcpy((void*)rpdata.substitute_name + sizeof(abs_subst_name_prefix),
490                        name_utf16le, name_utf16le_nbytes);
491                 memcpy((void*)rpdata.print_name + sizeof(abs_print_name_prefix),
492                        name_utf16le, name_utf16le_nbytes);
493         } else {
494                 rpdata.substitute_name_nbytes = name_utf16le_nbytes;
495                 rpdata.print_name_nbytes = name_utf16le_nbytes;
496                 rpdata.substitute_name = name_utf16le;
497                 rpdata.print_name = name_utf16le;
498                 rpdata.rpflags = SYMBOLIC_LINK_RELATIVE;
499         }
500
501         ret = make_reparse_buffer(&rpdata, (u8*)&rpbuf_disk, &rpbuflen);
502         if (ret == 0) {
503                 if (!inode_add_stream_with_data(inode,
504                                                 STREAM_TYPE_REPARSE_POINT,
505                                                 NO_STREAM_NAME,
506                                                 (u8*)&rpbuf_disk + 8,
507                                                 rpbuflen - 8,
508                                                 blob_table))
509                         ret = WIMLIB_ERR_NOMEM;
510         }
511         FREE(name_utf16le);
512         return ret;
513 }
514
515 #endif /* !__WIN32__ */