win32_set_security_descriptor(): Do not request MAXIMUM_ALLOWED
[wimlib] / src / win32_apply.c
1 /*
2  * win32_apply.c - Windows-specific code for applying files from a WIM image.
3  */
4
5 /*
6  * Copyright (C) 2013 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/apply.h"
33 #include "wimlib/error.h"
34 #include "wimlib/lookup_table.h"
35
36 static int
37 win32_start_extract(const wchar_t *path, struct apply_ctx *ctx)
38 {
39         int ret;
40         unsigned vol_flags;
41         bool supports_SetFileShortName;
42
43         ret = win32_get_vol_flags(path, &vol_flags, &supports_SetFileShortName);
44         if (ret)
45                 return ret;
46
47         ctx->supported_features.archive_files = 1;
48         ctx->supported_features.hidden_files = 1;
49         ctx->supported_features.system_files = 1;
50
51         if (vol_flags & FILE_FILE_COMPRESSION)
52                 ctx->supported_features.compressed_files = 1;
53
54         if (vol_flags & FILE_SUPPORTS_ENCRYPTION) {
55                 ctx->supported_features.encrypted_files = 1;
56                 ctx->supported_features.encrypted_directories = 1;
57         }
58
59         ctx->supported_features.not_context_indexed_files = 1;
60
61         if (vol_flags & FILE_SUPPORTS_SPARSE_FILES)
62                 ctx->supported_features.sparse_files = 1;
63
64         if (vol_flags & FILE_NAMED_STREAMS)
65                 ctx->supported_features.named_data_streams = 1;
66
67         if (vol_flags & FILE_SUPPORTS_HARD_LINKS)
68                 ctx->supported_features.hard_links = 1;
69
70         if (vol_flags & FILE_SUPPORTS_REPARSE_POINTS) {
71                 ctx->supported_features.reparse_points = 1;
72                 if (win32func_CreateSymbolicLinkW)
73                         ctx->supported_features.symlink_reparse_points = 1;
74         }
75
76         if (vol_flags & FILE_PERSISTENT_ACLS)
77                 ctx->supported_features.security_descriptors = 1;
78
79         if (supports_SetFileShortName)
80                 ctx->supported_features.short_names = 1;
81         return 0;
82 }
83
84 /* Create a normal file, overwriting one already present.  */
85 static int
86 win32_create_file(const wchar_t *path, struct apply_ctx *ctx, u64 *cookie_ret)
87 {
88         HANDLE h;
89         unsigned retry_count = 0;
90         DWORD dwFlagsAndAttributes = FILE_FLAG_BACKUP_SEMANTICS;
91
92 retry:
93         /* WRITE_OWNER and WRITE_DAC privileges are required for some reason,
94          * even through we're creating a new file.  */
95         h = CreateFile(path, WRITE_OWNER | WRITE_DAC, 0, NULL,
96                        CREATE_ALWAYS, dwFlagsAndAttributes, NULL);
97         if (h == INVALID_HANDLE_VALUE) {
98                 /* File couldn't be created.  */
99                 DWORD err = GetLastError();
100                 if (err == ERROR_ACCESS_DENIED && retry_count == 0) {
101
102                         /* Access denied error for the first time.  Try
103                          * adjusting file attributes.  */
104
105                         /* Get attributes of the existing file.  */
106                         DWORD attribs = GetFileAttributes(path);
107                         if (attribs != INVALID_FILE_ATTRIBUTES &&
108                             (attribs & (FILE_ATTRIBUTE_HIDDEN |
109                                         FILE_ATTRIBUTE_SYSTEM |
110                                         FILE_ATTRIBUTE_READONLY)))
111                         {
112                                 /* If the existing file has
113                                  * FILE_ATTRIBUTE_HIDDEN and/or
114                                  * FILE_ATTRIBUTE_SYSTEM, they must be set in
115                                  * the call to CreateFile().  This is true even
116                                  * when FILE_ATTRIBUTE_NORMAL was not specified,
117                                  * contrary to the MS "documentation".  */
118                                 dwFlagsAndAttributes |= (attribs &
119                                                          (FILE_ATTRIBUTE_HIDDEN |
120                                                           FILE_ATTRIBUTE_SYSTEM));
121                                 /* If the existing file has
122                                  * FILE_ATTRIBUTE_READONLY, it must be cleared
123                                  * before attempting to create a new file over
124                                  * it.  This is true even when the process has
125                                  * the SE_RESTORE_NAME privilege and requested
126                                  * the FILE_FLAG_BACKUP_SEMANTICS flag to
127                                  * CreateFile().  */
128                                 if (attribs & FILE_ATTRIBUTE_READONLY) {
129                                         SetFileAttributes(path,
130                                                           attribs & ~FILE_ATTRIBUTE_READONLY);
131                                 }
132                                 retry_count++;
133                                 goto retry;
134                         }
135                 }
136                 set_errno_from_win32_error(err);
137                 return WIMLIB_ERR_OPEN;
138         }
139         CloseHandle(h);
140         return 0;
141 }
142
143 static int
144 win32_create_directory(const wchar_t *path, struct apply_ctx *ctx,
145                        u64 *cookie_ret)
146 {
147         if (!CreateDirectory(path, NULL))
148                 if (GetLastError() != ERROR_ALREADY_EXISTS)
149                         goto error;
150         return 0;
151
152 error:
153         set_errno_from_GetLastError();
154         return WIMLIB_ERR_MKDIR;
155 }
156
157 /* Delete a non-directory file, working around Windows quirks.  */
158 static BOOL
159 win32_delete_file_wrapper(const wchar_t *path)
160 {
161         DWORD attrib;
162         DWORD err;
163
164         if (DeleteFile(path))
165                 return TRUE;
166
167         err = GetLastError();
168         attrib = GetFileAttributes(path);
169         if (attrib & FILE_ATTRIBUTE_READONLY) {
170                 /* Try again with FILE_ATTRIBUTE_READONLY cleared.  */
171                 attrib &= ~FILE_ATTRIBUTE_READONLY;
172                 if (SetFileAttributes(path, attrib)) {
173                         if (DeleteFile(path))
174                                 return TRUE;
175                         else
176                                 err = GetLastError();
177                 }
178         }
179
180         SetLastError(err);
181         return FALSE;
182 }
183
184 static int
185 win32_create_hardlink(const wchar_t *oldpath, const wchar_t *newpath,
186                       struct apply_ctx *ctx)
187 {
188         if (!CreateHardLink(newpath, oldpath, NULL)) {
189                 if (GetLastError() != ERROR_ALREADY_EXISTS)
190                         goto error;
191                 if (!win32_delete_file_wrapper(newpath))
192                         goto error;
193                 if (!CreateHardLink(newpath, oldpath, NULL))
194                         goto error;
195         }
196         return 0;
197
198 error:
199         set_errno_from_GetLastError();
200         return WIMLIB_ERR_LINK;
201 }
202
203 static int
204 win32_create_symlink(const wchar_t *oldpath, const wchar_t *newpath,
205                      struct apply_ctx *ctx)
206 {
207         if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0)) {
208                 if (GetLastError() != ERROR_ALREADY_EXISTS)
209                         goto error;
210                 if (!win32_delete_file_wrapper(newpath))
211                         goto error;
212                 if (!(*win32func_CreateSymbolicLinkW)(newpath, oldpath, 0))
213                         goto error;
214         }
215         return 0;
216
217 error:
218         set_errno_from_GetLastError();
219         return WIMLIB_ERR_LINK;
220 }
221
222 static int
223 win32_extract_wim_chunk(const void *buf, size_t len, void *arg)
224 {
225         HANDLE h = (HANDLE)arg;
226         DWORD nbytes_written;
227
228         if (unlikely(!WriteFile(h, buf, len, &nbytes_written, NULL)))
229                 goto error;
230         if (unlikely(nbytes_written != len))
231                 goto error;
232         return 0;
233
234 error:
235         set_errno_from_GetLastError();
236         return WIMLIB_ERR_WRITE;
237 }
238
239 static int
240 win32_extract_stream(const wchar_t *path, const wchar_t *stream_name,
241                      size_t stream_name_nchars,
242                      struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
243 {
244         DWORD creationDisposition = OPEN_EXISTING;
245         wchar_t *stream_path = (wchar_t*)path;
246         HANDLE h;
247         int ret;
248
249         if (stream_name_nchars) {
250                 creationDisposition = CREATE_ALWAYS;
251                 stream_path = alloca(sizeof(wchar_t) *
252                                      (wcslen(path) + 1 +
253                                       wcslen(stream_name) + 1));
254                 tsprintf(stream_path, L"%ls:%ls", path, stream_name);
255         }
256
257         h = CreateFile(stream_path, FILE_WRITE_DATA, 0, NULL,
258                        creationDisposition, FILE_FLAG_BACKUP_SEMANTICS |
259                                             FILE_FLAG_OPEN_REPARSE_POINT,
260                        NULL);
261         if (h == INVALID_HANDLE_VALUE)
262                 goto error;
263
264         ret = 0;
265         if (!lte)
266                 goto out_close_handle;
267         ret = extract_stream(lte, lte->size, win32_extract_wim_chunk, h);
268 out_close_handle:
269         if (!CloseHandle(h))
270                 goto error;
271         if (ret && !errno)
272                 errno = -1;
273         return ret;
274
275 error:
276         set_errno_from_GetLastError();
277         return WIMLIB_ERR_WRITE;
278 }
279
280 static int
281 win32_extract_unnamed_stream(file_spec_t file,
282                              struct wim_lookup_table_entry *lte,
283                              struct apply_ctx *ctx)
284 {
285         return win32_extract_stream(file.path, NULL, 0, lte, ctx);
286 }
287
288 static int
289 win32_extract_named_stream(file_spec_t file, const wchar_t *stream_name,
290                            size_t stream_name_nchars,
291                            struct wim_lookup_table_entry *lte, struct apply_ctx *ctx)
292 {
293         return win32_extract_stream(file.path, stream_name,
294                                     stream_name_nchars, lte, ctx);
295 }
296
297 struct win32_encrypted_extract_ctx {
298         const struct wim_lookup_table_entry *lte;
299         u64 offset;
300 };
301
302 static DWORD WINAPI
303 win32_encrypted_import_cb(unsigned char *data, void *_import_ctx,
304                           unsigned long *len_p)
305 {
306         struct win32_encrypted_extract_ctx *import_ctx = _import_ctx;
307         unsigned long len = *len_p;
308         const struct wim_lookup_table_entry *lte = import_ctx->lte;
309
310         len = min(len, lte->size - import_ctx->offset);
311
312         if (read_partial_wim_stream_into_buf(lte, len, import_ctx->offset, data))
313                 return ERROR_READ_FAULT;
314
315         import_ctx->offset += len;
316         *len_p = len;
317         return ERROR_SUCCESS;
318 }
319
320 static int
321 win32_extract_encrypted_stream(const wchar_t *path,
322                                struct wim_lookup_table_entry *lte,
323                                struct apply_ctx *ctx)
324 {
325         void *file_ctx;
326         DWORD err;
327         int ret;
328         struct win32_encrypted_extract_ctx extract_ctx;
329
330         err = OpenEncryptedFileRaw(path, CREATE_FOR_IMPORT, &file_ctx);
331         if (err != ERROR_SUCCESS) {
332                 set_errno_from_win32_error(err);
333                 ret = WIMLIB_ERR_OPEN;
334                 goto out;
335         }
336
337         extract_ctx.lte = lte;
338         extract_ctx.offset = 0;
339         err = WriteEncryptedFileRaw(win32_encrypted_import_cb, &extract_ctx,
340                                     file_ctx);
341         if (err != ERROR_SUCCESS) {
342                 set_errno_from_win32_error(err);
343                 ret = WIMLIB_ERR_WRITE;
344                 goto out_close;
345         }
346
347         ret = 0;
348 out_close:
349         CloseEncryptedFileRaw(file_ctx);
350 out:
351         return ret;
352 }
353
354 static BOOL
355 win32_set_special_file_attributes(const wchar_t *path, u32 attributes)
356 {
357         HANDLE h;
358         DWORD err;
359         USHORT compression_format = COMPRESSION_FORMAT_DEFAULT;
360         DWORD bytes_returned;
361
362         h = win32_open_existing_file(path, GENERIC_READ | GENERIC_WRITE);
363         if (h == INVALID_HANDLE_VALUE)
364                 goto error;
365
366         if (attributes & FILE_ATTRIBUTE_SPARSE_FILE)
367                 if (!DeviceIoControl(h, FSCTL_SET_SPARSE,
368                                      NULL, 0,
369                                      NULL, 0,
370                                      &bytes_returned, NULL))
371                         goto error_close_handle;
372
373         if (attributes & FILE_ATTRIBUTE_COMPRESSED)
374                 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
375                                      &compression_format, sizeof(USHORT),
376                                      NULL, 0,
377                                      &bytes_returned, NULL))
378                         goto error_close_handle;
379
380         if (!CloseHandle(h))
381                 goto error;
382
383         if (attributes & FILE_ATTRIBUTE_ENCRYPTED)
384                 if (!EncryptFile(path))
385                         goto error;
386
387         return TRUE;
388
389 error_close_handle:
390         err = GetLastError();
391         CloseHandle(h);
392         SetLastError(err);
393 error:
394         return FALSE;
395 }
396
397 static int
398 win32_set_file_attributes(const wchar_t *path, u32 attributes,
399                           struct apply_ctx *ctx, unsigned pass)
400 {
401         u32 special_attributes =
402                 FILE_ATTRIBUTE_REPARSE_POINT |
403                 FILE_ATTRIBUTE_DIRECTORY |
404                 FILE_ATTRIBUTE_SPARSE_FILE |
405                 FILE_ATTRIBUTE_COMPRESSED |
406                 FILE_ATTRIBUTE_ENCRYPTED;
407         u32 actual_attributes;
408
409         /* Delay setting FILE_ATTRIBUTE_READONLY on the initial pass (when files
410          * are created, but data not extracted); otherwise the system will
411          * refuse access to the file even if the process has SeRestorePrivilege.
412          */
413         if (pass == 0)
414                 attributes &= ~FILE_ATTRIBUTE_READONLY;
415
416         if (!SetFileAttributes(path, attributes & ~special_attributes))
417                 goto error;
418
419         if (pass != 0)
420                 return 0;
421
422         if (attributes & (FILE_ATTRIBUTE_SPARSE_FILE |
423                           FILE_ATTRIBUTE_ENCRYPTED |
424                           FILE_ATTRIBUTE_COMPRESSED))
425                 if (!win32_set_special_file_attributes(path, attributes))
426                         goto error;
427
428         /* If file is not supposed to be encrypted or compressed, remove
429          * defaulted encrypted or compressed attributes (from creating file in
430          * encrypted or compressed directory).  */
431         actual_attributes = GetFileAttributes(path);
432         if (actual_attributes == INVALID_FILE_ATTRIBUTES)
433                 goto error;
434
435         if ((actual_attributes & FILE_ATTRIBUTE_ENCRYPTED) &&
436             !(attributes & FILE_ATTRIBUTE_ENCRYPTED))
437                 if (!DecryptFile(path, 0))
438                         goto error;
439         if ((actual_attributes & FILE_ATTRIBUTE_COMPRESSED) &&
440             !(attributes & FILE_ATTRIBUTE_COMPRESSED))
441         {
442                 HANDLE h;
443                 DWORD bytes_returned;
444                 USHORT compression_format = COMPRESSION_FORMAT_NONE;
445
446                 h = win32_open_existing_file(path, GENERIC_READ | GENERIC_WRITE);
447                 if (h == INVALID_HANDLE_VALUE)
448                         goto error;
449
450                 if (!DeviceIoControl(h, FSCTL_SET_COMPRESSION,
451                                      &compression_format, sizeof(USHORT),
452                                      NULL, 0,
453                                      &bytes_returned, NULL))
454                 {
455                         DWORD err = GetLastError();
456                         CloseHandle(h);
457                         SetLastError(err);
458                         goto error;
459                 }
460
461                 if (!CloseHandle(h))
462                         goto error;
463         }
464
465         return 0;
466
467 error:
468         set_errno_from_GetLastError();
469         return WIMLIB_ERR_SET_ATTRIBUTES;
470 }
471
472 static int
473 win32_set_reparse_data(const wchar_t *path, const u8 *rpbuf, u16 rpbuflen,
474                        struct apply_ctx *ctx)
475 {
476         HANDLE h;
477         DWORD err;
478         DWORD bytes_returned;
479
480         h = win32_open_existing_file(path, GENERIC_WRITE);
481         if (h == INVALID_HANDLE_VALUE)
482                 goto error;
483
484         if (!DeviceIoControl(h, FSCTL_SET_REPARSE_POINT,
485                              (void*)rpbuf, rpbuflen,
486                              NULL, 0, &bytes_returned, NULL))
487                 goto error_close_handle;
488
489         if (!CloseHandle(h))
490                 goto error;
491
492         return 0;
493
494 error_close_handle:
495         err = GetLastError();
496         CloseHandle(h);
497         SetLastError(err);
498 error:
499         set_errno_from_GetLastError();
500         return WIMLIB_ERR_WRITE; /* XXX: need better error code */
501 }
502
503 static int
504 win32_set_short_name(const wchar_t *path, const wchar_t *short_name,
505                      size_t short_name_nchars, struct apply_ctx *ctx)
506 {
507         HANDLE h;
508         DWORD err;
509
510         h = win32_open_existing_file(path, GENERIC_WRITE | DELETE);
511         if (h == INVALID_HANDLE_VALUE)
512                 goto error;
513
514         if (short_name_nchars) {
515                 if (!SetFileShortName(h, short_name))
516                         goto error_close_handle;
517         } else if (running_on_windows_7_or_later()) {
518                 if (!SetFileShortName(h, L""))
519                         goto error_close_handle;
520         }
521
522         if (!CloseHandle(h))
523                 goto error;
524
525         return 0;
526
527 error_close_handle:
528         err = GetLastError();
529         CloseHandle(h);
530         SetLastError(err);
531 error:
532         set_errno_from_GetLastError();
533         return WIMLIB_ERR_WRITE; /* XXX: need better error code */
534 }
535
536 static DWORD
537 do_win32_set_security_descriptor(HANDLE h, const wchar_t *path,
538                                  SECURITY_INFORMATION info,
539                                  PSECURITY_DESCRIPTOR desc)
540 {
541 #ifdef WITH_NTDLL
542         if (func_NtSetSecurityObject) {
543                 return (*func_RtlNtStatusToDosError)(
544                                 (*func_NtSetSecurityObject)(h, info, desc));
545         }
546 #endif
547         if (SetFileSecurity(path, info, desc))
548                 return ERROR_SUCCESS;
549         else
550                 return GetLastError();
551 }
552
553 /*
554  * Set an arbitrary security descriptor on an arbitrary file (or directory),
555  * working around bugs and design flaws in the Windows operating system.
556  *
557  * On success, return 0.  On failure, return WIMLIB_ERR_SET_SECURITY and set
558  * errno.  Note: if WIMLIB_EXTRACT_FLAG_STRICT_ACLS is not set in
559  * ctx->extract_flags, this function succeeds iff any part of the security
560  * descriptor was successfully set.
561  */
562 static int
563 win32_set_security_descriptor(const wchar_t *path, const u8 *desc,
564                               size_t desc_size, struct apply_ctx *ctx)
565 {
566         SECURITY_INFORMATION info;
567         HANDLE h;
568         int ret;
569
570         /* We really just want to set entire the security descriptor as-is, but
571          * all available APIs require specifying the specific parts of the
572          * descriptor being set.  Start out by requesting all parts be set.  If
573          * permissions problems are encountered, fall back to omitting some
574          * parts (first the SACL, then the DACL, then the owner), unless the
575          * WIMLIB_EXTRACT_FLAG_STRICT_ACLS flag has been enabled.  */
576         info = OWNER_SECURITY_INFORMATION | GROUP_SECURITY_INFORMATION |
577                DACL_SECURITY_INFORMATION  | SACL_SECURITY_INFORMATION;
578
579         h = INVALID_HANDLE_VALUE;
580
581         /* Prefer NtSetSecurityObject() to SetFileSecurity().  SetFileSecurity()
582          * itself necessarily uses NtSetSecurityObject() as the latter is the
583          * underlying system call for setting security information, but
584          * SetFileSecurity() opens the handle with NtCreateFile() without
585          * FILE_OPEN_FILE_BACKUP_INTENT.  Hence, access checks are done and due
586          * to the Windows security model, even a process running as the
587          * Administrator can have access denied.  (Of course, this not mentioned
588          * in the MS "documentation".)  */
589
590 #ifdef WITH_NTDLL
591         if (func_NtSetSecurityObject) {
592                 DWORD dwDesiredAccess;
593
594                 /* Open a handle for NtSetSecurityObject() with as many relevant
595                  * access rights as possible.
596                  *
597                  * We don't know which rights will be actually granted.  It
598                  * could be less than what is needed to actually assign the full
599                  * security descriptor, especially if the process is running as
600                  * a non-Administrator.  However, by default we just do the best
601                  * we can, unless WIMLIB_EXTRACT_FLAG_STRICT_ACLS has been
602                  * enabled.  The MAXIMUM_ALLOWED access right is seemingly
603                  * designed for this use case; however, it does not work
604                  * properly in all cases: it can cause CreateFile() to fail with
605                  * ERROR_ACCESS_DENIED, even though by definition
606                  * MAXIMUM_ALLOWED access only requests access rights that are
607                  * *not* denied.  (Needless to say, MS does not document this
608                  * bug.)  */
609
610                 dwDesiredAccess = WRITE_DAC |
611                                   WRITE_OWNER |
612                                   ACCESS_SYSTEM_SECURITY;
613                 for (;;) {
614                         DWORD err;
615
616                         h = win32_open_existing_file(path, dwDesiredAccess);
617                         if (h != INVALID_HANDLE_VALUE)
618                                 break;
619                         err = GetLastError();
620                         if (err == ERROR_ACCESS_DENIED ||
621                             err == ERROR_PRIVILEGE_NOT_HELD)
622                         {
623                                 /* Don't increment partial_security_descriptors
624                                  * here or check WIMLIB_EXTRACT_FLAG_STRICT_ACLS
625                                  * here.  It will be done later if needed; here
626                                  * we are just trying to get as many relevant
627                                  * access rights as possible.  */
628                                 if (dwDesiredAccess & ACCESS_SYSTEM_SECURITY) {
629                                         dwDesiredAccess &= ~ACCESS_SYSTEM_SECURITY;
630                                         continue;
631                                 }
632                                 if (dwDesiredAccess & WRITE_DAC) {
633                                         dwDesiredAccess &= ~WRITE_DAC;
634                                         continue;
635                                 }
636                                 if (dwDesiredAccess & WRITE_OWNER) {
637                                         dwDesiredAccess &= ~WRITE_OWNER;
638                                         continue;
639                                 }
640                         }
641                         /* Other error, or couldn't open the file even with no
642                          * access rights specified.  Something else must be
643                          * wrong.  */
644                         set_errno_from_win32_error(err);
645                         return WIMLIB_ERR_SET_SECURITY;
646                 }
647         }
648 #endif
649
650         /* Try setting the security descriptor.  */
651         for (;;) {
652                 DWORD err;
653
654                 err = do_win32_set_security_descriptor(h, path, info,
655                                                        (PSECURITY_DESCRIPTOR)desc);
656                 if (err == ERROR_SUCCESS) {
657                         ret = 0;
658                         break;
659                 }
660
661                 /* Failed to set the requested parts of the security descriptor.
662                  * If the error was permissions-related, try to set fewer parts
663                  * of the security descriptor, unless
664                  * WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled.  */
665                 if ((err == ERROR_PRIVILEGE_NOT_HELD ||
666                      err == ERROR_ACCESS_DENIED) &&
667                     !(ctx->extract_flags & WIMLIB_EXTRACT_FLAG_STRICT_ACLS))
668                 {
669                         if (info & SACL_SECURITY_INFORMATION) {
670                                 info &= ~SACL_SECURITY_INFORMATION;
671                                 ctx->partial_security_descriptors++;
672                                 continue;
673                         }
674                         if (info & DACL_SECURITY_INFORMATION) {
675                                 info &= ~DACL_SECURITY_INFORMATION;
676                                 continue;
677                         }
678                         if (info & OWNER_SECURITY_INFORMATION) {
679                                 info &= ~OWNER_SECURITY_INFORMATION;
680                                 continue;
681                         }
682                         /* Nothing left except GROUP, and if we removed it we
683                          * wouldn't have anything at all.  */
684                 }
685                 /* No part of the security descriptor could be set, or
686                  * WIMLIB_EXTRACT_FLAG_STRICT_ACLS is enabled and the full
687                  * security descriptor could not be set.  */
688                 if (!(info & SACL_SECURITY_INFORMATION))
689                         ctx->partial_security_descriptors--;
690                 set_errno_from_win32_error(err);
691                 ret = WIMLIB_ERR_SET_SECURITY;
692                 break;
693         }
694
695         /* Close handle opened for NtSetSecurityObject().  */
696 #ifdef WITH_NTDLL
697         if (func_NtSetSecurityObject)
698                 CloseHandle(h);
699 #endif
700         return ret;
701 }
702
703 static int
704 win32_set_timestamps(const wchar_t *path, u64 creation_time,
705                      u64 last_write_time, u64 last_access_time,
706                      struct apply_ctx *ctx)
707 {
708         HANDLE h;
709         DWORD err;
710         FILETIME creationTime = {.dwLowDateTime = creation_time & 0xffffffff,
711                                  .dwHighDateTime = creation_time >> 32};
712         FILETIME lastAccessTime = {.dwLowDateTime = last_access_time & 0xffffffff,
713                                   .dwHighDateTime = last_access_time >> 32};
714         FILETIME lastWriteTime = {.dwLowDateTime = last_write_time & 0xffffffff,
715                                   .dwHighDateTime = last_write_time >> 32};
716
717         h = win32_open_existing_file(path, FILE_WRITE_ATTRIBUTES);
718         if (h == INVALID_HANDLE_VALUE)
719                 goto error;
720
721         if (!SetFileTime(h, &creationTime, &lastAccessTime, &lastWriteTime))
722                 goto error_close_handle;
723
724         if (!CloseHandle(h))
725                 goto error;
726
727         return 0;
728
729 error_close_handle:
730         err = GetLastError();
731         CloseHandle(h);
732         SetLastError(err);
733 error:
734         set_errno_from_GetLastError();
735         return WIMLIB_ERR_SET_TIMESTAMPS;
736 }
737
738 const struct apply_operations win32_apply_ops = {
739         .name = L"Win32",
740
741         .target_is_root           = win32_path_is_root_of_drive,
742         .start_extract            = win32_start_extract,
743         .create_file              = win32_create_file,
744         .create_directory         = win32_create_directory,
745         .create_hardlink          = win32_create_hardlink,
746         .create_symlink           = win32_create_symlink,
747         .extract_unnamed_stream   = win32_extract_unnamed_stream,
748         .extract_named_stream     = win32_extract_named_stream,
749         .extract_encrypted_stream = win32_extract_encrypted_stream,
750         .set_file_attributes      = win32_set_file_attributes,
751         .set_reparse_data         = win32_set_reparse_data,
752         .set_short_name           = win32_set_short_name,
753         .set_security_descriptor  = win32_set_security_descriptor,
754         .set_timestamps           = win32_set_timestamps,
755
756         .path_prefix = L"\\\\?\\",
757         .path_prefix_nchars = 4,
758         .path_separator = L'\\',
759         .path_max = 32768,
760
761         .requires_realtarget_in_paths = 1,
762         .realpath_works_on_nonexisting_files = 1,
763         .root_directory_is_special = 1,
764         .requires_final_set_attributes_pass = 1,
765         .extract_encrypted_stream_creates_file = 1,
766         .requires_short_name_reordering = 1, /* TODO: check if this is really needed  */
767 };
768
769 #endif /* __WIN32__ */