]> wimlib.net Git - wimlib/blob - src/reparse.c
header cleanups
[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/compiler.h"
31 #include "wimlib/endianness.h"
32 #include "wimlib/encoding.h"
33 #include "wimlib/error.h"
34 #include "wimlib/inode.h"
35 #include "wimlib/lookup_table.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 lookup table
161  * entry resource length.  However, we reconstruct the first 8 bytes in the
162  * buffer returned by this function.
163  */
164 int
165 wim_inode_get_reparse_data(const struct wim_inode * restrict inode,
166                            u8 * restrict rpbuf,
167                            u16 * restrict rpbuflen_ret,
168                            struct wim_lookup_table_entry *lte_override)
169 {
170         struct wim_lookup_table_entry *lte;
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 (!lte_override) {
178                 lte = inode_unnamed_lte_resolved(inode);
179                 if (!lte) {
180                         ERROR("Reparse point has no reparse data!");
181                         return WIMLIB_ERR_INVALID_REPARSE_DATA;
182                 }
183         } else {
184                 lte = lte_override;
185         }
186
187         if (lte->size > REPARSE_POINT_MAX_SIZE - 8) {
188                 ERROR("Reparse data is too long!");
189                 return WIMLIB_ERR_INVALID_REPARSE_DATA;
190         }
191         rpdatalen = lte->size;
192
193         /* Read the data from the WIM file */
194         ret = read_full_stream_into_buf(lte, rpbuf + 8);
195         if (ret)
196                 return ret;
197
198         /* Reconstruct the first 8 bytes of the reparse point buffer */
199         rpbuf_disk = (struct reparse_buffer_disk*)rpbuf;
200
201         /* ReparseTag */
202         rpbuf_disk->rptag = cpu_to_le32(inode->i_reparse_tag);
203
204         /* ReparseDataLength */
205         rpbuf_disk->rpdatalen = cpu_to_le16(rpdatalen);
206
207         /* ReparseReserved
208          * XXX this could be one of the unknown fields in the WIM dentry. */
209         rpbuf_disk->rpreserved = cpu_to_le16(0);
210
211         *rpbuflen_ret = rpdatalen + 8;
212         return 0;
213 }
214
215 /* UNIX version of getting and setting the data in reparse points */
216 #ifndef __WIN32__
217
218 static const utf16lechar volume_junction_prefix[11] = {
219         cpu_to_le16('\\'),
220         cpu_to_le16('?'),
221         cpu_to_le16('?'),
222         cpu_to_le16('\\'),
223         cpu_to_le16('V'),
224         cpu_to_le16('o'),
225         cpu_to_le16('l'),
226         cpu_to_le16('u'),
227         cpu_to_le16('m'),
228         cpu_to_le16('e'),
229         cpu_to_le16('{'),
230 };
231
232 enum {
233         SUBST_NAME_IS_RELATIVE_LINK = -1,
234         SUBST_NAME_IS_VOLUME_JUNCTION = -2,
235         SUBST_NAME_IS_UNKNOWN = -3,
236 };
237
238 /* Parse the "substitute name" (link target) from a symbolic link or junction
239  * reparse point.
240  *
241  * Return value is:
242  *
243  * Non-negative integer:
244  *      The name is an absolute symbolic link in one of several formats,
245  *      and the return value is the number of UTF-16LE characters that need to
246  *      be advanced to reach a simple "absolute" path starting with a backslash
247  *      (i.e. skip over \??\ and/or drive letter)
248  * Negative integer:
249  *      SUBST_NAME_IS_VOLUME_JUNCTION:
250  *              The name is a volume junction.
251  *      SUBST_NAME_IS_RELATIVE_LINK:
252  *              The name is a relative symbolic link.
253  *      SUBST_NAME_IS_UNKNOWN:
254  *              The name does not appear to be a valid symbolic link, junction,
255  *              or mount point.
256  */
257 static int
258 parse_substitute_name(const utf16lechar *substitute_name,
259                       u16 substitute_name_nbytes, u32 rptag)
260 {
261         u16 substitute_name_nchars = substitute_name_nbytes / 2;
262
263         if (substitute_name_nchars >= 7 &&
264             substitute_name[0] == cpu_to_le16('\\') &&
265             substitute_name[1] == cpu_to_le16('?') &&
266             substitute_name[2] == cpu_to_le16('?') &&
267             substitute_name[3] == cpu_to_le16('\\') &&
268             substitute_name[4] != cpu_to_le16('\0') &&
269             substitute_name[5] == cpu_to_le16(':') &&
270             substitute_name[6] == cpu_to_le16('\\'))
271         {
272                 /* "Full" symlink or junction (\??\x:\ prefixed path) */
273                 return 6;
274         } else if (rptag == WIM_IO_REPARSE_TAG_MOUNT_POINT &&
275                    substitute_name_nchars >= 12 &&
276                    memcmp(substitute_name, volume_junction_prefix,
277                           sizeof(volume_junction_prefix)) == 0 &&
278                    substitute_name[substitute_name_nchars - 1] == cpu_to_le16('\\'))
279         {
280                 /* Volume junction.  Can't really do anything with it. */
281                 return SUBST_NAME_IS_VOLUME_JUNCTION;
282         } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
283                    substitute_name_nchars >= 3 &&
284                    substitute_name[0] != cpu_to_le16('\0') &&
285                    substitute_name[1] == cpu_to_le16(':') &&
286                    substitute_name[2] == cpu_to_le16('\\'))
287         {
288                 /* "Absolute" symlink, with drive letter */
289                 return 2;
290         } else if (rptag == WIM_IO_REPARSE_TAG_SYMLINK &&
291                    substitute_name_nchars >= 1)
292         {
293                 if (substitute_name[0] == cpu_to_le16('\\'))
294                         /* "Absolute" symlink, without drive letter */
295                         return 0;
296                 else
297                         /* "Relative" symlink, without drive letter */
298                         return SUBST_NAME_IS_RELATIVE_LINK;
299         } else {
300                 return SUBST_NAME_IS_UNKNOWN;
301         }
302 }
303
304 /*
305  * Get the UNIX-style symlink target from the WIM inode for a reparse point.
306  * Specifically, this translates the target from UTF-16 to the current multibyte
307  * encoding, strips the drive prefix if present, and replaces backslashes with
308  * forward slashes.
309  *
310  * @inode
311  *      The inode to read the symlink from.  It must be a reparse point with
312  *      tag WIM_IO_REPARSE_TAG_SYMLINK (a real symlink) or
313  *      WIM_IO_REPARSE_TAG_MOUNT_POINT (a mount point or junction point).
314  *
315  * @buf
316  *      Buffer into which to place the link target.
317  *
318  * @bufsize
319  *      Available space in @buf, in bytes.
320  *
321  * @lte_override
322  *      If not NULL, the stream from which to read the reparse data.  Otherwise,
323  *      the reparse data will be read from the unnamed stream of @inode.
324  *
325  * If the entire symbolic link target was placed in the buffer, returns the
326  * number of bytes written.  The resulting string is not null-terminated.  If
327  * the symbolic link target was too large to be placed in the buffer, the first
328  * @bufsize bytes of it are placed in the buffer and
329  * -ENAMETOOLONG is returned.  Otherwise, a negative errno value indicating
330  *  another error is returned.
331  */
332 ssize_t
333 wim_inode_readlink(const struct wim_inode * restrict inode,
334                    char * restrict buf, size_t bufsize,
335                    struct wim_lookup_table_entry *lte_override)
336 {
337         int ret;
338         struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
339         struct reparse_data rpdata;
340         char *link_target;
341         char *translated_target;
342         size_t link_target_len;
343         u16 rpbuflen;
344
345         wimlib_assert(inode_is_symlink(inode));
346
347         if (wim_inode_get_reparse_data(inode, (u8*)&rpbuf_disk, &rpbuflen,
348                                        lte_override))
349                 return -EIO;
350
351         if (parse_reparse_data((const u8*)&rpbuf_disk, rpbuflen, &rpdata))
352                 return -EINVAL;
353
354         ret = utf16le_to_tstr(rpdata.substitute_name,
355                               rpdata.substitute_name_nbytes,
356                               &link_target, &link_target_len);
357         if (ret)
358                 return -errno;
359
360         translated_target = link_target;
361         ret = parse_substitute_name(rpdata.substitute_name,
362                                     rpdata.substitute_name_nbytes,
363                                     rpdata.rptag);
364         switch (ret) {
365         case SUBST_NAME_IS_RELATIVE_LINK:
366                 goto out_translate_slashes;
367         case SUBST_NAME_IS_VOLUME_JUNCTION:
368                 goto out_have_link;
369         case SUBST_NAME_IS_UNKNOWN:
370                 ERROR("Can't understand reparse point "
371                       "substitute name \"%s\"", link_target);
372                 ret = -EIO;
373                 goto out_free_link_target;
374         default:
375                 translated_target += ret;
376                 link_target_len -= ret;
377                 break;
378         }
379
380 out_translate_slashes:
381         for (size_t i = 0; i < link_target_len; i++)
382                 if (translated_target[i] == '\\')
383                         translated_target[i] = '/';
384 out_have_link:
385         if (link_target_len > bufsize) {
386                 link_target_len = bufsize;
387                 ret = -ENAMETOOLONG;
388         } else {
389                 ret = link_target_len;
390         }
391         memcpy(buf, translated_target, link_target_len);
392 out_free_link_target:
393         FREE(link_target);
394         return ret;
395 }
396
397 int
398 wim_inode_set_symlink(struct wim_inode *inode,
399                       const char *target,
400                       struct wim_lookup_table *lookup_table)
401
402 {
403         struct reparse_buffer_disk rpbuf_disk _aligned_attribute(8);
404         struct reparse_data rpdata;
405         static const char abs_subst_name_prefix[12] = "\\\0?\0?\0\\\0C\0:\0";
406         static const char abs_print_name_prefix[4] = "C\0:\0";
407         utf16lechar *name_utf16le;
408         size_t name_utf16le_nbytes;
409         int ret;
410         u16 rpbuflen;
411
412         DEBUG("Creating reparse point data buffer for UNIX "
413               "symlink target \"%s\"", target);
414         memset(&rpdata, 0, sizeof(rpdata));
415         ret = tstr_to_utf16le(target, strlen(target),
416                               &name_utf16le, &name_utf16le_nbytes);
417         if (ret)
418                 return ret;
419
420         for (size_t i = 0; i < name_utf16le_nbytes / 2; i++)
421                 if (name_utf16le[i] == cpu_to_le16('/'))
422                         name_utf16le[i] = cpu_to_le16('\\');
423
424         /* Compatability notes:
425          *
426          * On UNIX, an absolute symbolic link begins with '/'; everything else
427          * is a relative symbolic link.  (Quite simple compared to the various
428          * ways to provide Windows paths.)
429          *
430          * To change a UNIX relative symbolic link to Windows format, we only
431          * need to translate it to UTF-16LE and replace forward slashes with
432          * backslashes.  We do not make any attempt to handle filename character
433          * problems, such as a link target that itself contains backslashes on
434          * UNIX.  Then, for these relative links, we set the reparse header
435          * @flags field to SYMBOLIC_LINK_RELATIVE.
436          *
437          * For UNIX absolute symbolic links, we must set the @flags field to 0.
438          * Then, there are multiple options as to actually represent the
439          * absolute link targets:
440          *
441          * (1) An absolute path beginning with one backslash character. similar
442          * to UNIX-style, just with a different path separator.  Print name same
443          * as substitute name.
444          *
445          * (2) Absolute path beginning with drive letter followed by a
446          * backslash.  Print name same as substitute name.
447          *
448          * (3) Absolute path beginning with drive letter followed by a
449          * backslash; substitute name prefixed with \??\, otherwise same as
450          * print name.
451          *
452          * We choose option (3) here, and we just assume C: for the drive
453          * letter.  The reasoning for this is:
454          *
455          * (1) Microsoft imagex.exe has a bug where it does not attempt to do
456          * reparse point fixups for these links, even though they are valid
457          * absolute links.  (Note: in this case prefixing the substitute name
458          * with \??\ does not work; it just makes the data unable to be restored
459          * at all.)
460          * (2) Microsoft imagex.exe will fail when doing reparse point fixups
461          * for these.  It apparently contains a bug that causes it to create an
462          * invalid reparse point, which then cannot be restored.
463          * (3) This is the only option I tested for which reparse point fixups
464          * worked properly in Microsoft imagex.exe.
465          *
466          * So option (3) it is.
467          */
468
469         rpdata.rptag = inode->i_reparse_tag;
470         if (target[0] == '/') {
471                 rpdata.substitute_name_nbytes = name_utf16le_nbytes +
472                                                 sizeof(abs_subst_name_prefix);
473                 rpdata.print_name_nbytes = name_utf16le_nbytes +
474                                            sizeof(abs_print_name_prefix);
475                 rpdata.substitute_name = alloca(rpdata.substitute_name_nbytes);
476                 rpdata.print_name = alloca(rpdata.print_name_nbytes);
477                 memcpy(rpdata.substitute_name, abs_subst_name_prefix,
478                        sizeof(abs_subst_name_prefix));
479                 memcpy(rpdata.print_name, abs_print_name_prefix,
480                        sizeof(abs_print_name_prefix));
481                 memcpy((void*)rpdata.substitute_name + sizeof(abs_subst_name_prefix),
482                        name_utf16le, name_utf16le_nbytes);
483                 memcpy((void*)rpdata.print_name + sizeof(abs_print_name_prefix),
484                        name_utf16le, name_utf16le_nbytes);
485         } else {
486                 rpdata.substitute_name_nbytes = name_utf16le_nbytes;
487                 rpdata.print_name_nbytes = name_utf16le_nbytes;
488                 rpdata.substitute_name = name_utf16le;
489                 rpdata.print_name = name_utf16le;
490                 rpdata.rpflags = SYMBOLIC_LINK_RELATIVE;
491         }
492
493         ret = make_reparse_buffer(&rpdata, (u8*)&rpbuf_disk, &rpbuflen);
494         if (ret == 0) {
495                 ret = inode_set_unnamed_stream(inode,
496                                                (u8*)&rpbuf_disk + 8,
497                                                rpbuflen - 8,
498                                                lookup_table);
499         }
500         FREE(name_utf16le);
501         return ret;
502 }
503
504 #endif /* !__WIN32__ */