]> wimlib.net Git - wimlib/blob - src/win32_capture.c
win32_get_security_descriptor(): Check for STATUS_BUFFER_TOO_SMALL
[wimlib] / src / win32_capture.c
1 /*
2  * win32_capture.c - Windows-specific code for capturing files into a WIM image.
3  */
4
5 /*
6  * Copyright (C) 2013, 2014 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef __WIN32__
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "wimlib/win32_common.h"
31
32 #include "wimlib/capture.h"
33 #include "wimlib/dentry.h"
34 #include "wimlib/encoding.h"
35 #include "wimlib/endianness.h"
36 #include "wimlib/error.h"
37 #include "wimlib/lookup_table.h"
38 #include "wimlib/paths.h"
39 #include "wimlib/reparse.h"
40
41 #include <errno.h>
42
43 struct win32_capture_state {
44         unsigned long num_get_sd_access_denied;
45         unsigned long num_get_sacl_priv_notheld;
46         unsigned long num_long_path_warnings;
47 };
48
49 static NTSTATUS
50 winnt_openat(HANDLE cur_dir, const wchar_t *path, size_t path_nchars,
51              ACCESS_MASK perms, HANDLE *h_ret)
52 {
53         UNICODE_STRING name;
54         OBJECT_ATTRIBUTES attr;
55         IO_STATUS_BLOCK iosb;
56         NTSTATUS status;
57
58         name.Length = path_nchars * sizeof(wchar_t);
59         name.MaximumLength = name.Length + sizeof(wchar_t);
60         name.Buffer = (wchar_t *)path;
61
62         attr.Length = sizeof(attr);
63         attr.RootDirectory = cur_dir;
64         attr.ObjectName = &name;
65         attr.Attributes = 0;
66         attr.SecurityDescriptor = NULL;
67         attr.SecurityQualityOfService = NULL;
68
69 retry:
70         status = (*func_NtOpenFile)(h_ret, perms, &attr, &iosb,
71                                     FILE_SHARE_READ |
72                                             FILE_SHARE_WRITE |
73                                             FILE_SHARE_DELETE,
74                                     FILE_OPEN_REPARSE_POINT |
75                                             FILE_OPEN_FOR_BACKUP_INTENT |
76                                             FILE_SYNCHRONOUS_IO_NONALERT);
77         if (!NT_SUCCESS(status)) {
78                 if (status == STATUS_ACCESS_DENIED ||
79                     status == STATUS_PRIVILEGE_NOT_HELD) {
80                         if (perms & ACCESS_SYSTEM_SECURITY) {
81                                 perms &= ~ACCESS_SYSTEM_SECURITY;
82                                 goto retry;
83                         }
84                         if (perms & READ_CONTROL) {
85                                 perms &= ~READ_CONTROL;
86                                 goto retry;
87                         }
88                 }
89         }
90         return status;
91 }
92
93 int
94 read_win32_file_prefix(const struct wim_lookup_table_entry *lte,
95                        u64 size,
96                        consume_data_callback_t cb,
97                        void *cb_ctx)
98 {
99         const wchar_t *path;
100         HANDLE h;
101         NTSTATUS status;
102         u8 buf[BUFFER_SIZE];
103         u64 bytes_remaining;
104         int ret;
105
106         path = lte->file_on_disk;
107         status = winnt_openat(NULL, path, wcslen(path),
108                               FILE_READ_DATA | SYNCHRONIZE, &h);
109         if (!NT_SUCCESS(status)) {
110                 set_errno_from_nt_status(status);
111                 ERROR_WITH_ERRNO("\"%ls\": Can't open for reading", path);
112                 return WIMLIB_ERR_OPEN;
113         }
114
115         ret = 0;
116         bytes_remaining = size;
117         while (bytes_remaining) {
118                 IO_STATUS_BLOCK iosb;
119                 ULONG count;
120
121                 count = min(sizeof(buf), bytes_remaining);
122
123                 status = (*func_NtReadFile)(h, NULL, NULL, NULL,
124                                             &iosb, buf, count, NULL, NULL);
125                 if (!NT_SUCCESS(status) || iosb.Information != count) {
126                         set_errno_from_nt_status(status);
127                         ERROR_WITH_ERRNO("\"%ls\": Error reading data", path);
128                         ret = WIMLIB_ERR_READ;
129                         break;
130                 }
131
132                 bytes_remaining -= count;
133                 ret = (*cb)(buf, count, cb_ctx);
134                 if (ret)
135                         break;
136         }
137         (*func_NtClose)(h);
138         return ret;
139 }
140
141 struct win32_encrypted_read_ctx {
142         consume_data_callback_t read_prefix_cb;
143         void *read_prefix_ctx;
144         int wimlib_err_code;
145         u64 bytes_remaining;
146 };
147
148 static DWORD WINAPI
149 win32_encrypted_export_cb(unsigned char *data, void *_ctx, unsigned long len)
150 {
151         struct win32_encrypted_read_ctx *ctx = _ctx;
152         int ret;
153         size_t bytes_to_consume = min(len, ctx->bytes_remaining);
154
155         if (bytes_to_consume == 0)
156                 return ERROR_SUCCESS;
157
158         ret = (*ctx->read_prefix_cb)(data, bytes_to_consume, ctx->read_prefix_ctx);
159         if (ret) {
160                 ctx->wimlib_err_code = ret;
161                 /* Shouldn't matter what error code is returned here, as long as
162                  * it isn't ERROR_SUCCESS.  */
163                 return ERROR_READ_FAULT;
164         }
165         ctx->bytes_remaining -= bytes_to_consume;
166         return ERROR_SUCCESS;
167 }
168
169 int
170 read_win32_encrypted_file_prefix(const struct wim_lookup_table_entry *lte,
171                                  u64 size,
172                                  consume_data_callback_t cb,
173                                  void *cb_ctx)
174 {
175         struct win32_encrypted_read_ctx export_ctx;
176         DWORD err;
177         void *file_ctx;
178         int ret;
179
180         DEBUG("Reading %"PRIu64" bytes from encrypted file \"%ls\"",
181               size, lte->file_on_disk);
182
183         export_ctx.read_prefix_cb = cb;
184         export_ctx.read_prefix_ctx = cb_ctx;
185         export_ctx.wimlib_err_code = 0;
186         export_ctx.bytes_remaining = size;
187
188         err = OpenEncryptedFileRaw(lte->file_on_disk, 0, &file_ctx);
189         if (err != ERROR_SUCCESS) {
190                 set_errno_from_win32_error(err);
191                 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
192                                  "for raw read", lte->file_on_disk);
193                 return WIMLIB_ERR_OPEN;
194         }
195         err = ReadEncryptedFileRaw(win32_encrypted_export_cb,
196                                    &export_ctx, file_ctx);
197         if (err != ERROR_SUCCESS) {
198                 set_errno_from_win32_error(err);
199                 ERROR_WITH_ERRNO("Failed to read encrypted file \"%ls\"",
200                                  lte->file_on_disk);
201                 ret = export_ctx.wimlib_err_code;
202                 if (ret == 0)
203                         ret = WIMLIB_ERR_READ;
204         } else if (export_ctx.bytes_remaining != 0) {
205                 ERROR("Only could read %"PRIu64" of %"PRIu64" bytes from "
206                       "encryted file \"%ls\"",
207                       size - export_ctx.bytes_remaining, size,
208                       lte->file_on_disk);
209                 ret = WIMLIB_ERR_READ;
210         } else {
211                 ret = 0;
212         }
213         CloseEncryptedFileRaw(file_ctx);
214         return ret;
215 }
216
217 /* Load the short name of a file into a WIM dentry.
218  *
219  * If we can't read the short filename for some reason, we just ignore the error
220  * and assume the file has no short name.  This shouldn't be an issue, since the
221  * short names are essentially obsolete anyway.
222  */
223 static int
224 win32_get_short_name(HANDLE hFile, struct wim_dentry *dentry)
225 {
226
227         /* It's not any harder to just make the NtQueryInformationFile() system
228          * call ourselves, and it saves a dumb call to FindFirstFile() which of
229          * course has to create its own handle.  */
230         NTSTATUS status;
231         IO_STATUS_BLOCK io_status;
232         u8 buf[128] _aligned_attribute(8);
233         const FILE_NAME_INFORMATION *info;
234
235         status = (*func_NtQueryInformationFile)(hFile, &io_status, buf, sizeof(buf),
236                                                 FileAlternateNameInformation);
237         info = (const FILE_NAME_INFORMATION*)buf;
238         if (NT_SUCCESS(status) && info->FileNameLength != 0) {
239                 dentry->short_name = utf16le_dupz(info->FileName,
240                                                   info->FileNameLength);
241                 if (!dentry->short_name)
242                         return WIMLIB_ERR_NOMEM;
243                 dentry->short_name_nbytes = info->FileNameLength;
244         }
245         return 0;
246 }
247
248 static int
249 win32_get_security_descriptor(HANDLE h,
250                               struct wim_inode *inode,
251                               struct wim_sd_set *sd_set,
252                               struct win32_capture_state *state,
253                               int add_flags)
254 {
255         SECURITY_INFORMATION requestedInformation;
256         u8 _buf[4096];
257         u8 *buf;
258         size_t bufsize;
259         DWORD lenNeeded;
260         NTSTATUS status;
261         int ret;
262
263         requestedInformation = DACL_SECURITY_INFORMATION |
264                                SACL_SECURITY_INFORMATION |
265                                OWNER_SECURITY_INFORMATION |
266                                GROUP_SECURITY_INFORMATION;
267         buf = _buf;
268         bufsize = sizeof(_buf);
269
270         /*
271          * We need the file's security descriptor in SECURITY_DESCRIPTOR_RELATIVE
272          * format, and we currently have a handle opened with as many relevant
273          * permissions as possible.  At this point, on Windows there are a number of
274          * options for reading a file's security descriptor:
275          *
276          * GetFileSecurity():  This takes in a path and returns the
277          * SECURITY_DESCRIPTOR_RELATIVE.  Problem: this uses an internal handle, not
278          * ours, and the handle created internally doesn't specify
279          * FILE_FLAG_BACKUP_SEMANTICS.  Therefore there can be access denied errors on
280          * some files and directories, even when running as the Administrator.
281          *
282          * GetSecurityInfo():  This takes in a handle and returns the security
283          * descriptor split into a bunch of different parts.  This should work, but it's
284          * dumb because we have to put the security descriptor back together again.
285          *
286          * BackupRead():  This can read the security descriptor, but this is a
287          * difficult-to-use API, probably only works as the Administrator, and the
288          * format of the returned data is not well documented.
289          *
290          * NtQuerySecurityObject():  This is exactly what we need, as it takes in a
291          * handle and returns the security descriptor in SECURITY_DESCRIPTOR_RELATIVE
292          * format.  Only problem is that it's a ntdll function and therefore not
293          * officially part of the Win32 API.  Oh well.
294          */
295         while (!(NT_SUCCESS(status = (*func_NtQuerySecurityObject)(h,
296                                                                    requestedInformation,
297                                                                    (PSECURITY_DESCRIPTOR)buf,
298                                                                    bufsize,
299                                                                    &lenNeeded))))
300         {
301                 switch (status) {
302                 case STATUS_BUFFER_TOO_SMALL:
303                         wimlib_assert(buf == _buf);
304                         buf = MALLOC(lenNeeded);
305                         if (!buf)
306                                 return WIMLIB_ERR_NOMEM;
307                         bufsize = lenNeeded;
308                         break;
309                 case STATUS_PRIVILEGE_NOT_HELD:
310                 case STATUS_ACCESS_DENIED:
311                         if (add_flags & WIMLIB_ADD_FLAG_STRICT_ACLS) {
312                 default:
313                                 set_errno_from_nt_status(status);
314                                 ret = WIMLIB_ERR_READ;
315                                 goto out_free_buf;
316                         }
317                         if (requestedInformation & SACL_SECURITY_INFORMATION) {
318                                 state->num_get_sacl_priv_notheld++;
319                                 requestedInformation &= ~SACL_SECURITY_INFORMATION;
320                                 break;
321                         }
322                         state->num_get_sd_access_denied++;
323                         ret = 0;
324                         goto out_free_buf;
325                 }
326         }
327
328         inode->i_security_id = sd_set_add_sd(sd_set, buf, lenNeeded);
329         if (inode->i_security_id < 0)
330                 ret = WIMLIB_ERR_NOMEM;
331         else
332                 ret = 0;
333 out_free_buf:
334         if (buf != _buf)
335                 FREE(buf);
336         return ret;
337 }
338
339 static int
340 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
341                                   HANDLE cur_dir,
342                                   wchar_t *full_path,
343                                   size_t full_path_nchars,
344                                   const wchar_t *filename,
345                                   size_t filename_nchars,
346                                   struct add_image_params *params,
347                                   struct win32_capture_state *state,
348                                   DWORD vol_flags);
349
350 /* Reads the directory entries of directory and recursively calls
351  * win32_build_dentry_tree() on them.  */
352 static int
353 win32_recurse_directory(HANDLE h,
354                         wchar_t *full_path,
355                         size_t full_path_nchars,
356                         struct wim_dentry *root,
357                         struct add_image_params *params,
358                         struct win32_capture_state *state,
359                         DWORD vol_flags)
360 {
361         int ret;
362
363         /* Using NtQueryDirectoryFile() we can re-use the same open handle,
364          * which we opened with FILE_FLAG_BACKUP_SEMANTICS.  */
365
366         NTSTATUS status;
367         IO_STATUS_BLOCK io_status;
368         const size_t bufsize = 8192;
369         void *buf;
370
371         buf = MALLOC(bufsize);
372         if (!buf)
373                 return WIMLIB_ERR_NOMEM;
374
375         while (NT_SUCCESS(status = (*func_NtQueryDirectoryFile)(h, NULL, NULL, NULL,
376                                                                 &io_status, buf, bufsize,
377                                                                 FileNamesInformation,
378                                                                 FALSE, NULL, FALSE)))
379         {
380                 const FILE_NAMES_INFORMATION *info = buf;
381                 for (;;) {
382                         if (!(info->FileNameLength == 2 && info->FileName[0] == L'.') &&
383                             !(info->FileNameLength == 4 && info->FileName[0] == L'.' &&
384                                                            info->FileName[1] == L'.'))
385                         {
386                                 wchar_t *p;
387                                 struct wim_dentry *child;
388
389                                 p = full_path + full_path_nchars;
390                                 *p++ = L'\\';
391                                 p = wmempcpy(p, info->FileName,
392                                              info->FileNameLength / 2);
393                                 *p = '\0';
394
395                                 ret = win32_build_dentry_tree_recursive(
396                                                                 &child,
397                                                                 h,
398                                                                 full_path,
399                                                                 p - full_path,
400                                                                 full_path + full_path_nchars + 1,
401                                                                 info->FileNameLength / 2,
402                                                                 params,
403                                                                 state,
404                                                                 vol_flags);
405
406                                 full_path[full_path_nchars] = L'\0';
407
408                                 if (ret)
409                                         goto out_free_buf;
410                                 if (child)
411                                         dentry_add_child(root, child);
412                         }
413                         if (info->NextEntryOffset == 0)
414                                 break;
415                         info = (const FILE_NAMES_INFORMATION *)
416                                         ((const u8 *)info + info->NextEntryOffset);
417                 }
418         }
419
420         if (status != STATUS_NO_MORE_FILES) {
421                 set_errno_from_nt_status(status);
422                 ERROR_WITH_ERRNO("\"%ls\": Can't read directory", full_path);
423                 ret = WIMLIB_ERR_READ;
424         }
425 out_free_buf:
426         FREE(buf);
427         return ret;
428 }
429
430 /* Reparse point fixup status code */
431 enum rp_status {
432         /* Reparse point corresponded to an absolute symbolic link or junction
433          * point that pointed outside the directory tree being captured, and
434          * therefore was excluded. */
435         RP_EXCLUDED       = 0x0,
436
437         /* Reparse point was not fixed as it was either a relative symbolic
438          * link, a mount point, or something else we could not understand. */
439         RP_NOT_FIXED      = 0x1,
440
441         /* Reparse point corresponded to an absolute symbolic link or junction
442          * point that pointed inside the directory tree being captured, where
443          * the target was specified by a "full" \??\ prefixed path, and
444          * therefore was fixed to be relative to the root of the directory tree
445          * being captured. */
446         RP_FIXED_FULLPATH = 0x2,
447
448         /* Same as RP_FIXED_FULLPATH, except the absolute link target did not
449          * have the \??\ prefix.  It may have begun with a drive letter though.
450          * */
451         RP_FIXED_ABSPATH  = 0x4,
452
453         /* Either RP_FIXED_FULLPATH or RP_FIXED_ABSPATH. */
454         RP_FIXED          = RP_FIXED_FULLPATH | RP_FIXED_ABSPATH,
455 };
456
457 /* Given the "substitute name" target of a Windows reparse point, try doing a
458  * fixup where we change it to be absolute relative to the root of the directory
459  * tree being captured.
460  *
461  * Note that this is only executed when WIMLIB_ADD_FLAG_RPFIX has been
462  * set.
463  *
464  * @capture_root_ino and @capture_root_dev indicate the inode number and device
465  * of the root of the directory tree being captured.  They are meant to identify
466  * this directory (as an alternative to its actual path, which could potentially
467  * be reached via multiple destinations due to other symbolic links).  This may
468  * not work properly on FAT, which doesn't seem to supply proper inode numbers
469  * or file IDs.  However, FAT doesn't support reparse points so this function
470  * wouldn't even be called anyway.
471  */
472 static enum rp_status
473 win32_capture_maybe_rpfix_target(wchar_t *target, u16 *target_nbytes_p,
474                                  u64 capture_root_ino, u64 capture_root_dev,
475                                  u32 rptag)
476 {
477         u16 target_nchars = *target_nbytes_p / 2;
478         size_t stripped_chars;
479         wchar_t *orig_target;
480         int ret;
481
482         ret = parse_substitute_name(target, *target_nbytes_p, rptag);
483         if (ret < 0)
484                 return RP_NOT_FIXED;
485         stripped_chars = ret;
486         if (stripped_chars)
487                 stripped_chars -= 2;
488         target[target_nchars] = L'\0';
489         orig_target = target;
490         target = capture_fixup_absolute_symlink(target + stripped_chars,
491                                                 capture_root_ino, capture_root_dev);
492         if (!target)
493                 return RP_EXCLUDED;
494         target_nchars = wcslen(target);
495         wmemmove(orig_target + stripped_chars, target, target_nchars + 1);
496         *target_nbytes_p = (target_nchars + stripped_chars) * sizeof(wchar_t);
497         DEBUG("Fixed reparse point (new target: \"%ls\")", orig_target);
498         if (stripped_chars)
499                 return RP_FIXED_FULLPATH;
500         else
501                 return RP_FIXED_ABSPATH;
502 }
503
504 /* Returns: `enum rp_status' value on success; negative WIMLIB_ERR_* value on
505  * failure. */
506 static int
507 win32_capture_try_rpfix(u8 *rpbuf, u16 *rpbuflen_p,
508                         u64 capture_root_ino, u64 capture_root_dev,
509                         const wchar_t *path, struct add_image_params *params)
510 {
511         struct reparse_data rpdata;
512         int ret;
513         enum rp_status rp_status;
514
515         ret = parse_reparse_data(rpbuf, *rpbuflen_p, &rpdata);
516         if (ret)
517                 return -ret;
518
519         rp_status = win32_capture_maybe_rpfix_target(rpdata.substitute_name,
520                                                      &rpdata.substitute_name_nbytes,
521                                                      capture_root_ino,
522                                                      capture_root_dev,
523                                                      le32_to_cpu(*(le32*)rpbuf));
524         if (rp_status & RP_FIXED) {
525                 wimlib_assert(rpdata.substitute_name_nbytes % 2 == 0);
526                 utf16lechar substitute_name_copy[rpdata.substitute_name_nbytes / 2];
527                 wmemcpy(substitute_name_copy, rpdata.substitute_name,
528                         rpdata.substitute_name_nbytes / 2);
529                 rpdata.substitute_name = substitute_name_copy;
530                 rpdata.print_name = substitute_name_copy;
531                 rpdata.print_name_nbytes = rpdata.substitute_name_nbytes;
532                 if (rp_status == RP_FIXED_FULLPATH) {
533                         /* "full path", meaning \??\ prefixed.  We should not
534                          * include this prefix in the print name, as it is
535                          * apparently meant for the filesystem driver only. */
536                         rpdata.print_name += 4;
537                         rpdata.print_name_nbytes -= 8;
538                 }
539                 ret = make_reparse_buffer(&rpdata, rpbuf, rpbuflen_p);
540                 if (ret == 0)
541                         ret = rp_status;
542                 else
543                         ret = -ret;
544         } else {
545                 if (rp_status == RP_EXCLUDED) {
546                         /* Ignoring absolute symbolic link or junction point
547                          * that points out of the tree to be captured.  */
548                         size_t print_name_nchars = rpdata.print_name_nbytes / 2;
549                         wchar_t print_name0[print_name_nchars + 1];
550                         print_name0[print_name_nchars] = L'\0';
551                         wmemcpy(print_name0, rpdata.print_name, print_name_nchars);
552
553                         params->progress.scan.cur_path = path;
554                         params->progress.scan.symlink_target = print_name0;
555                         do_capture_progress(params,
556                                             WIMLIB_SCAN_DENTRY_EXCLUDED_SYMLINK,
557                                             NULL);
558                 }
559                 ret = rp_status;
560         }
561         return ret;
562 }
563
564 /*
565  * Loads the reparse point data from a reparse point into memory, optionally
566  * fixing the targets of absolute symbolic links and junction points to be
567  * relative to the root of capture.
568  *
569  * @h:      Open handle to the reparse point.
570  * @path:   Path to the reparse point file.
571  * @params: Additional parameters, including whether to do reparse point fixups
572  *          or not.
573  * @rpbuf:  Buffer of length at least REPARSE_POINT_MAX_SIZE bytes into which
574  *          the reparse point buffer will be loaded.
575  * @rpbuflen_ret:  On success, the length of the reparse point buffer in bytes
576  *                 is written to this location.
577  *
578  * Returns:
579  *      On success, returns an `enum rp_status' value that indicates if and/or
580  *      how the reparse point fixup was done.
581  *
582  *      On failure, returns a negative value that is a negated WIMLIB_ERR_*
583  *      code.
584  */
585 static int
586 win32_get_reparse_data(HANDLE h, const wchar_t *path,
587                        struct add_image_params *params,
588                        u8 *rpbuf, u16 *rpbuflen_ret)
589 {
590         DWORD bytesReturned;
591         u32 reparse_tag;
592         int ret;
593         u16 rpbuflen;
594
595         if (!DeviceIoControl(h, FSCTL_GET_REPARSE_POINT,
596                              NULL, /* "Not used with this operation; set to NULL" */
597                              0, /* "Not used with this operation; set to 0" */
598                              rpbuf, /* "A pointer to a buffer that
599                                                    receives the reparse point data */
600                              REPARSE_POINT_MAX_SIZE, /* "The size of the output
601                                                         buffer, in bytes */
602                              &bytesReturned,
603                              NULL))
604         {
605                 set_errno_from_GetLastError();
606                 return -WIMLIB_ERR_READ;
607         }
608
609         if (bytesReturned < 8 || bytesReturned > REPARSE_POINT_MAX_SIZE) {
610                 errno = EINVAL;
611                 return -WIMLIB_ERR_INVALID_REPARSE_DATA;
612         }
613
614         rpbuflen = bytesReturned;
615         reparse_tag = le32_to_cpu(*(le32*)rpbuf);
616         if (params->add_flags & WIMLIB_ADD_FLAG_RPFIX &&
617             (reparse_tag == WIM_IO_REPARSE_TAG_SYMLINK ||
618              reparse_tag == WIM_IO_REPARSE_TAG_MOUNT_POINT))
619         {
620                 /* Try doing reparse point fixup */
621                 ret = win32_capture_try_rpfix(rpbuf,
622                                               &rpbuflen,
623                                               params->capture_root_ino,
624                                               params->capture_root_dev,
625                                               path,
626                                               params);
627         } else {
628                 ret = RP_NOT_FIXED;
629         }
630         *rpbuflen_ret = rpbuflen;
631         return ret;
632 }
633
634 static DWORD WINAPI
635 win32_tally_encrypted_size_cb(unsigned char *_data, void *_size_ret,
636                               unsigned long len)
637 {
638         *(u64*)_size_ret += len;
639         return ERROR_SUCCESS;
640 }
641
642 static int
643 win32_get_encrypted_file_size(const wchar_t *path, u64 *size_ret)
644 {
645         DWORD err;
646         void *file_ctx;
647         int ret;
648
649         err = OpenEncryptedFileRaw(path, 0, &file_ctx);
650         if (err != ERROR_SUCCESS) {
651                 set_errno_from_win32_error(err);
652                 ERROR_WITH_ERRNO("Failed to open encrypted file \"%ls\" "
653                                  "for raw read", path);
654                 return WIMLIB_ERR_OPEN;
655         }
656         *size_ret = 0;
657         err = ReadEncryptedFileRaw(win32_tally_encrypted_size_cb,
658                                    size_ret, file_ctx);
659         if (err != ERROR_SUCCESS) {
660                 set_errno_from_win32_error(err);
661                 ERROR_WITH_ERRNO("Failed to read raw encrypted data from "
662                                  "\"%ls\"", path);
663                 ret = WIMLIB_ERR_READ;
664         } else {
665                 ret = 0;
666         }
667         CloseEncryptedFileRaw(file_ctx);
668         return ret;
669 }
670
671 static bool
672 get_data_stream_name(const wchar_t *raw_stream_name, size_t raw_stream_name_nchars,
673                      const wchar_t **stream_name_ret, size_t *stream_name_nchars_ret)
674 {
675         const wchar_t *sep, *type, *end;
676
677         /* The stream name should be returned as :NAME:TYPE  */
678         if (raw_stream_name_nchars < 1)
679                 return false;
680         if (raw_stream_name[0] != L':')
681                 return false;
682
683         raw_stream_name++;
684         raw_stream_name_nchars--;
685
686         end = raw_stream_name + raw_stream_name_nchars;
687
688         sep = wmemchr(raw_stream_name, L':', raw_stream_name_nchars);
689         if (!sep)
690                 return false;
691
692         type = sep + 1;
693         if (end - type != 5)
694                 return false;
695
696         if (wmemcmp(type, L"$DATA", 5))
697                 return false;
698
699         *stream_name_ret = raw_stream_name;
700         *stream_name_nchars_ret = sep - raw_stream_name;
701         return true;
702 }
703
704 static wchar_t *
705 build_stream_path(const wchar_t *path, size_t path_nchars,
706                   const wchar_t *stream_name, size_t stream_name_nchars)
707 {
708         size_t stream_path_nchars;
709         wchar_t *stream_path;
710         wchar_t *p;
711
712         stream_path_nchars = path_nchars;
713         if (stream_name_nchars)
714                 stream_path_nchars += 1 + stream_name_nchars;
715
716         stream_path = MALLOC((stream_path_nchars + 1) * sizeof(wchar_t));
717         if (stream_path) {
718                 p = wmempcpy(stream_path, path, path_nchars);
719                 if (stream_name_nchars) {
720                         *p++ = L':';
721                         p = wmempcpy(p, stream_name, stream_name_nchars);
722                 }
723                 *p++ = L'\0';
724         }
725         return stream_path;
726 }
727
728 static int
729 win32_capture_stream(const wchar_t *path, size_t path_nchars,
730                      const wchar_t *raw_stream_name, size_t raw_stream_name_nchars,
731                      u64 stream_size,
732                      struct wim_inode *inode,
733                      struct list_head *unhashed_streams)
734 {
735         const wchar_t *stream_name;
736         size_t stream_name_nchars;
737         struct wim_ads_entry *ads_entry;
738         wchar_t *stream_path;
739         struct wim_lookup_table_entry *lte;
740         u32 stream_id;
741
742         /* Given the raw stream name (which is something like
743          * L":streamname:$DATA", extract just the stream name part.  */
744         if (!get_data_stream_name(raw_stream_name, raw_stream_name_nchars,
745                                   &stream_name, &stream_name_nchars))
746                 return 0;
747
748         /* If this is a named stream, allocate an ADS entry.  */
749         if (stream_name_nchars) {
750                 ads_entry = inode_add_ads_utf16le(inode, stream_name,
751                                                   stream_name_nchars * sizeof(wchar_t));
752                 if (!ads_entry)
753                         return WIMLIB_ERR_NOMEM;
754         } else {
755                 ads_entry = NULL;
756         }
757
758         /* If the stream is empty, no lookup table entry is needed. */
759         if (stream_size == 0)
760                 return 0;
761
762         /* Build the path to the stream.  For unnamed streams, this is simply
763          * the path to the file.  For named streams, this is the path to the
764          * file, followed by a colon, followed by the stream name.  */
765         stream_path = build_stream_path(path, path_nchars,
766                                         stream_name, stream_name_nchars);
767         if (!stream_path)
768                 return WIMLIB_ERR_NOMEM;
769
770         /* Set up the lookup table entry for the stream.  */
771         lte = new_lookup_table_entry();
772         if (!lte) {
773                 FREE(stream_path);
774                 return WIMLIB_ERR_NOMEM;
775         }
776         lte->file_on_disk = stream_path;
777         lte->resource_location = RESOURCE_IN_FILE_ON_DISK;
778         lte->size = stream_size;
779         if ((inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) && !ads_entry) {
780                 /* Special case for encrypted file.  */
781
782                 /* OpenEncryptedFileRaw() expects Win32 name, not NT name.  */
783                 lte->file_on_disk[1] = L'\\';
784                 wimlib_assert(!wmemcmp(lte->file_on_disk, L"\\\\?\\", 4));
785
786                 u64 encrypted_size;
787                 int ret;
788
789                 ret = win32_get_encrypted_file_size(lte->file_on_disk,
790                                                     &encrypted_size);
791                 if (ret) {
792                         free_lookup_table_entry(lte);
793                         return ret;
794                 }
795                 lte->size = encrypted_size;
796                 lte->resource_location = RESOURCE_WIN32_ENCRYPTED;
797         }
798
799         if (ads_entry) {
800                 stream_id = ads_entry->stream_id;
801                 ads_entry->lte = lte;
802         } else {
803                 stream_id = 0;
804                 inode->i_lte = lte;
805         }
806         add_unhashed_stream(lte, inode, stream_id, unhashed_streams);
807         return 0;
808 }
809
810 /* Load information about the streams of an open file into a WIM inode.
811  *
812  * We use the NtQueryInformationFile() system call instead of FindFirstStream()
813  * and FindNextStream().  This is done for two reasons:
814  *
815  * - FindFirstStream() opens its own handle to the file or directory and
816  *   apparently does so without specifying FILE_FLAG_BACKUP_SEMANTICS, thereby
817  *   causing access denied errors on certain files (even when running as the
818  *   Administrator).
819  * - FindFirstStream() and FindNextStream() is only available on Windows Vista
820  *   and later, whereas the stream support in NtQueryInformationFile() was
821  *   already present in Windows XP.
822  */
823 static int
824 win32_capture_streams(HANDLE *hFile_p,
825                       const wchar_t *path,
826                       size_t path_nchars,
827                       struct wim_inode *inode,
828                       struct list_head *unhashed_streams,
829                       u64 file_size,
830                       DWORD vol_flags)
831 {
832         int ret;
833         u8 _buf[1024] _aligned_attribute(8);
834         u8 *buf;
835         size_t bufsize;
836         IO_STATUS_BLOCK io_status;
837         NTSTATUS status;
838         const FILE_STREAM_INFORMATION *info;
839
840         DEBUG("Capturing streams from \"%ls\"", path);
841
842         buf = _buf;
843         bufsize = sizeof(_buf);
844
845         if (!(vol_flags & FILE_NAMED_STREAMS))
846                 goto unnamed_only;
847
848         /* Get a buffer containing the stream information.  */
849         while (!NT_SUCCESS(status = (*func_NtQueryInformationFile)(*hFile_p,
850                                                                    &io_status,
851                                                                    buf,
852                                                                    bufsize,
853                                                                    FileStreamInformation)))
854         {
855
856                 switch (status) {
857                 case STATUS_BUFFER_OVERFLOW:
858                         {
859                                 u8 *newbuf;
860
861                                 bufsize *= 2;
862                                 if (buf == _buf)
863                                         newbuf = MALLOC(bufsize);
864                                 else
865                                         newbuf = REALLOC(buf, bufsize);
866                                 if (!newbuf) {
867                                         ret = WIMLIB_ERR_NOMEM;
868                                         goto out_free_buf;
869                                 }
870                                 buf = newbuf;
871                         }
872                         break;
873                 case STATUS_NOT_IMPLEMENTED:
874                 case STATUS_NOT_SUPPORTED:
875                 case STATUS_INVALID_INFO_CLASS:
876                         goto unnamed_only;
877                 default:
878                         set_errno_from_nt_status(status);
879                         ERROR_WITH_ERRNO("\"%ls\": Failed to query "
880                                          "stream information", path);
881                         ret = WIMLIB_ERR_READ;
882                         goto out_free_buf;
883                 }
884         }
885
886         if (io_status.Information == 0) {
887                 /* No stream information.  */
888                 ret = 0;
889                 goto out_free_buf;
890         }
891
892         if (inode->i_attributes & FILE_ATTRIBUTE_ENCRYPTED) {
893                 /* OpenEncryptedFileRaw() seems to fail with
894                  * ERROR_SHARING_VIOLATION if there are any handles opened to
895                  * the file.  */
896                 (*func_NtClose)(*hFile_p);
897                 *hFile_p = INVALID_HANDLE_VALUE;
898         }
899
900         /* Parse one or more stream information structures.  */
901         info = (const FILE_STREAM_INFORMATION *)buf;
902         for (;;) {
903                 /* Capture the stream.  */
904                 ret = win32_capture_stream(path, path_nchars,
905                                            info->StreamName,
906                                            info->StreamNameLength / 2,
907                                            info->StreamSize.QuadPart,
908                                            inode, unhashed_streams);
909                 if (ret)
910                         goto out_free_buf;
911
912                 if (info->NextEntryOffset == 0) {
913                         /* No more stream information.  */
914                         break;
915                 }
916                 /* Advance to next stream information.  */
917                 info = (const FILE_STREAM_INFORMATION *)
918                                 ((const u8 *)info + info->NextEntryOffset);
919         }
920         ret = 0;
921         goto out_free_buf;
922
923 unnamed_only:
924         /* The volume does not support named streams.  Only capture the unnamed
925          * data stream. */
926         DEBUG("Only capturing unnamed data stream");
927         if (inode->i_attributes & (FILE_ATTRIBUTE_DIRECTORY |
928                                    FILE_ATTRIBUTE_REPARSE_POINT))
929         {
930                 ret = 0;
931                 goto out_free_buf;
932         }
933
934         ret = win32_capture_stream(path, path_nchars,
935                                    L"::$DATA", 7, file_size,
936                                    inode, unhashed_streams);
937 out_free_buf:
938         /* Free buffer if allocated on heap.  */
939         if (buf != _buf)
940                 FREE(buf);
941         return ret;
942 }
943
944 static int
945 win32_build_dentry_tree_recursive(struct wim_dentry **root_ret,
946                                   HANDLE cur_dir,
947                                   wchar_t *full_path,
948                                   size_t full_path_nchars,
949                                   const wchar_t *filename,
950                                   size_t filename_nchars,
951                                   struct add_image_params *params,
952                                   struct win32_capture_state *state,
953                                   DWORD vol_flags)
954 {
955         struct wim_dentry *root = NULL;
956         struct wim_inode *inode = NULL;
957         HANDLE h = INVALID_HANDLE_VALUE;
958         int ret;
959         NTSTATUS status;
960         FILE_ALL_INFORMATION file_info;
961         u8 *rpbuf;
962         u16 rpbuflen;
963         u16 not_rpfixed;
964
965         if (should_exclude_path(full_path + params->capture_root_nchars,
966                                 full_path_nchars - params->capture_root_nchars,
967                                 params->config))
968         {
969                 ret = 0;
970                 goto out_progress;
971         }
972
973         status = winnt_openat(cur_dir,
974                               (cur_dir ? filename : full_path),
975                               (cur_dir ? filename_nchars : full_path_nchars),
976                               FILE_READ_DATA |
977                                         FILE_READ_ATTRIBUTES |
978                                         READ_CONTROL |
979                                         ACCESS_SYSTEM_SECURITY |
980                                         SYNCHRONIZE,
981                               &h);
982         if (!NT_SUCCESS(status)) {
983                 set_errno_from_nt_status(status);
984                 ERROR_WITH_ERRNO("\"%ls\": Can't open file", full_path);
985                 ret = WIMLIB_ERR_OPEN;
986                 goto out;
987         }
988
989         {
990                 IO_STATUS_BLOCK iosb;
991
992                 status = (*func_NtQueryInformationFile)(h, &iosb,
993                                                         &file_info, sizeof(file_info),
994                                                         FileAllInformation);
995                 if (!NT_SUCCESS(status) && status != STATUS_BUFFER_OVERFLOW) {
996                         set_errno_from_GetLastError();
997                         ERROR_WITH_ERRNO("\"%ls\": Can't get file information", full_path);
998                         ret = WIMLIB_ERR_STAT;
999                         goto out;
1000                 }
1001         }
1002
1003         if (!cur_dir) {
1004                 /* Root directory; get volume information.  */
1005                 FILE_FS_ATTRIBUTE_INFORMATION attr_info;
1006                 FILE_FS_VOLUME_INFORMATION vol_info;
1007                 IO_STATUS_BLOCK iosb;
1008
1009                 /* Get volume flags  */
1010                 status = (*func_NtQueryVolumeInformationFile)(h, &iosb,
1011                                                               &attr_info, sizeof(attr_info),
1012                                                               FileFsAttributeInformation);
1013                 if ((NT_SUCCESS(status) || (status == STATUS_BUFFER_OVERFLOW)) &&
1014                     iosb.Information >= offsetof(FILE_FS_ATTRIBUTE_INFORMATION,
1015                                                  FileSystemAttributes) +
1016                                         sizeof(attr_info.FileSystemAttributes))
1017                 {
1018                         vol_flags = attr_info.FileSystemAttributes;
1019                 } else {
1020                         set_errno_from_nt_status(status);
1021                         WARNING_WITH_ERRNO("\"%ls\": Can't get volume attributes",
1022                                            full_path);
1023                         vol_flags = 0;
1024                 }
1025
1026                 /* Set inode number of root directory  */
1027                 params->capture_root_ino = file_info.InternalInformation.IndexNumber.QuadPart;
1028
1029                 /* Get volume ID  */
1030                 status = (*func_NtQueryVolumeInformationFile)(h, &iosb,
1031                                                               &vol_info, sizeof(vol_info),
1032                                                               FileFsVolumeInformation);
1033                 if ((NT_SUCCESS(status) || (status == STATUS_BUFFER_OVERFLOW)) &&
1034                     iosb.Information >= offsetof(FILE_FS_VOLUME_INFORMATION,
1035                                                  VolumeSerialNumber) +
1036                                         sizeof(vol_info.VolumeSerialNumber))
1037                 {
1038                         params->capture_root_dev = vol_info.VolumeSerialNumber;
1039                 } else {
1040                         set_errno_from_nt_status(status);
1041                         WARNING_WITH_ERRNO("\"%ls\": Can't get volume ID",
1042                                            full_path);
1043                         params->capture_root_dev = 0;
1044                 }
1045         }
1046
1047         if (file_info.BasicInformation.FileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1048                 rpbuf = alloca(REPARSE_POINT_MAX_SIZE);
1049                 ret = win32_get_reparse_data(h, full_path,
1050                                              params, rpbuf, &rpbuflen);
1051                 if (ret < 0) {
1052                         /* WIMLIB_ERR_* (inverted) */
1053                         ret = -ret;
1054                         ERROR_WITH_ERRNO("\"%ls\": Can't get reparse data",
1055                                          full_path);
1056                         goto out;
1057                 } else if (ret & RP_FIXED) {
1058                         not_rpfixed = 0;
1059                 } else if (ret == RP_EXCLUDED) {
1060                         ret = 0;
1061                         goto out;
1062                 } else {
1063                         not_rpfixed = 1;
1064                 }
1065         }
1066
1067         /* Create a WIM dentry with an associated inode, which may be shared.
1068          *
1069          * However, we need to explicitly check for directories and files with
1070          * only 1 link and refuse to hard link them.  This is because Windows
1071          * has a bug where it can return duplicate File IDs for files and
1072          * directories on the FAT filesystem. */
1073         ret = inode_table_new_dentry(params->inode_table,
1074                                      filename,
1075                                      file_info.InternalInformation.IndexNumber.QuadPart,
1076                                      0, /* We don't follow mount points, so we
1077                                            currently don't need to get the
1078                                            volume ID / device number.  */
1079                                      (file_info.StandardInformation.NumberOfLinks <= 1 ||
1080                                         (file_info.BasicInformation.FileAttributes &
1081                                          FILE_ATTRIBUTE_DIRECTORY)),
1082                                      &root);
1083         if (ret)
1084                 goto out;
1085
1086         ret = win32_get_short_name(h, root);
1087         if (ret) {
1088                 ERROR_WITH_ERRNO("\"%ls\": Can't get short name", full_path);
1089                 goto out;
1090         }
1091
1092         inode = root->d_inode;
1093
1094         if (inode->i_nlink > 1) {
1095                 /* Shared inode; nothing more to do */
1096                 ret = 0;
1097                 goto out_progress;
1098         }
1099
1100         inode->i_attributes = file_info.BasicInformation.FileAttributes;
1101         inode->i_creation_time = file_info.BasicInformation.CreationTime.QuadPart;
1102         inode->i_last_write_time = file_info.BasicInformation.LastWriteTime.QuadPart;
1103         inode->i_last_access_time = file_info.BasicInformation.LastAccessTime.QuadPart;
1104         inode->i_resolved = 1;
1105
1106         params->add_flags &= ~WIMLIB_ADD_FLAG_ROOT;
1107
1108         if (!(params->add_flags & WIMLIB_ADD_FLAG_NO_ACLS)
1109             && (vol_flags & FILE_PERSISTENT_ACLS))
1110         {
1111                 ret = win32_get_security_descriptor(h, inode,
1112                                                     params->sd_set, state,
1113                                                     params->add_flags);
1114                 if (ret) {
1115                         ERROR_WITH_ERRNO("\"%ls\": Can't "
1116                                          "read security descriptor", full_path);
1117                         goto out;
1118                 }
1119         }
1120
1121         /* Capture the unnamed data stream (only should be present for regular
1122          * files) and any alternate data streams. */
1123         ret = win32_capture_streams(&h,
1124                                     full_path,
1125                                     full_path_nchars,
1126                                     inode,
1127                                     params->unhashed_streams,
1128                                     file_info.StandardInformation.EndOfFile.QuadPart,
1129                                     vol_flags);
1130         if (ret)
1131                 goto out;
1132
1133         if (inode->i_attributes & FILE_ATTRIBUTE_REPARSE_POINT) {
1134                 /* Reparse point: set the reparse data (which we read already)
1135                  * */
1136                 inode->i_not_rpfixed = not_rpfixed;
1137                 inode->i_reparse_tag = le32_to_cpu(*(le32*)rpbuf);
1138                 ret = inode_set_unnamed_stream(inode, rpbuf + 8, rpbuflen - 8,
1139                                                params->lookup_table);
1140         } else if (inode->i_attributes & FILE_ATTRIBUTE_DIRECTORY) {
1141                 /* Directory (not a reparse point) --- recurse to children */
1142
1143                 if (h == INVALID_HANDLE_VALUE) {
1144                         /* Re-open handle that was closed to read raw encrypted
1145                          * data.  */
1146                         status = winnt_openat(cur_dir,
1147                                               (cur_dir ?
1148                                                filename : full_path),
1149                                               (cur_dir ?
1150                                                filename_nchars : full_path_nchars),
1151                                               FILE_LIST_DIRECTORY | SYNCHRONIZE,
1152                                               &h);
1153                         if (!NT_SUCCESS(status)) {
1154                                 set_errno_from_nt_status(status);
1155                                 ERROR_WITH_ERRNO("\"%ls\": Can't open file",
1156                                                  full_path);
1157                                 ret = WIMLIB_ERR_OPEN;
1158                                 goto out;
1159                         }
1160                 }
1161                 ret = win32_recurse_directory(h,
1162                                               full_path,
1163                                               full_path_nchars,
1164                                               root,
1165                                               params,
1166                                               state,
1167                                               vol_flags);
1168         }
1169         if (ret)
1170                 goto out;
1171
1172 out_progress:
1173         params->progress.scan.cur_path = full_path;
1174         if (root)
1175                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_OK, inode);
1176         else
1177                 do_capture_progress(params, WIMLIB_SCAN_DENTRY_EXCLUDED, NULL);
1178 out:
1179         if (h != INVALID_HANDLE_VALUE)
1180                 (*func_NtClose)(h);
1181         if (ret == 0)
1182                 *root_ret = root;
1183         else
1184                 free_dentry_tree(root, params->lookup_table);
1185         return ret;
1186 }
1187
1188 static void
1189 win32_do_capture_warnings(const wchar_t *path,
1190                           const struct win32_capture_state *state,
1191                           int add_flags)
1192 {
1193         if (state->num_get_sacl_priv_notheld == 0 &&
1194             state->num_get_sd_access_denied == 0)
1195                 return;
1196
1197         WARNING("Scan of \"%ls\" complete, but with one or more warnings:", path);
1198         if (state->num_get_sacl_priv_notheld != 0) {
1199                 WARNING("- Could not capture SACL (System Access Control List)\n"
1200                         "            on %lu files or directories.",
1201                         state->num_get_sacl_priv_notheld);
1202         }
1203         if (state->num_get_sd_access_denied != 0) {
1204                 WARNING("- Could not capture security descriptor at all\n"
1205                         "            on %lu files or directories.",
1206                         state->num_get_sd_access_denied);
1207         }
1208         WARNING("To fully capture all security descriptors, run the program\n"
1209                 "          with Administrator rights.");
1210 }
1211
1212 #define WINDOWS_NT_MAX_PATH 32768
1213
1214 /* Win32 version of capturing a directory tree */
1215 int
1216 win32_build_dentry_tree(struct wim_dentry **root_ret,
1217                         const wchar_t *root_disk_path,
1218                         struct add_image_params *params)
1219 {
1220         wchar_t *path;
1221         DWORD dret;
1222         size_t path_nchars;
1223         int ret;
1224         struct win32_capture_state state;
1225
1226         /* WARNING: There is no check for overflow later when this buffer is
1227          * being used!  But it's as long as the maximum path length understood
1228          * by Windows NT (which is NOT the same as MAX_PATH).  */
1229         path = MALLOC((WINDOWS_NT_MAX_PATH + 1) * sizeof(wchar_t));
1230         if (!path)
1231                 return WIMLIB_ERR_NOMEM;
1232
1233         /* Translate into full path.  */
1234         dret = GetFullPathName(root_disk_path, WINDOWS_NT_MAX_PATH - 3,
1235                                &path[4], NULL);
1236
1237         if (dret == 0 || dret >= WINDOWS_NT_MAX_PATH - 3) {
1238                 ERROR("Can't get full path name for \"%ls\"", root_disk_path);
1239                 return WIMLIB_ERR_UNSUPPORTED;
1240         }
1241
1242         /* Add \??\ prefix to form the NT namespace path.  */
1243         wmemcpy(path, L"\\??\\", 4);
1244         path_nchars = dret + 4;
1245
1246        /* Strip trailing slashes.  If we don't do this, we may create a path
1247         * with multiple consecutive backslashes, which for some reason causes
1248         * Windows to report that the file cannot be found.  */
1249         while (path_nchars >= 2 &&
1250                path[path_nchars - 1] == L'\\' &&
1251                path[path_nchars - 2] != L':')
1252         {
1253                 path[--path_nchars] = L'\0';
1254         }
1255
1256         params->capture_root_nchars = path_nchars;
1257
1258         memset(&state, 0, sizeof(state));
1259         ret = win32_build_dentry_tree_recursive(root_ret, NULL,
1260                                                 path, path_nchars, L"", 0,
1261                                                 params, &state, 0);
1262         FREE(path);
1263         if (ret == 0)
1264                 win32_do_capture_warnings(root_disk_path, &state, params->add_flags);
1265         return ret;
1266 }
1267
1268 #endif /* __WIN32__ */