]> wimlib.net Git - wimlib/blob - programs/imagex-win32.c
Win32 fixes
[wimlib] / programs / imagex-win32.c
1
2 /* Replacements for functions needed specifically by the 'imagex' program in
3  * Windows native builds; also, Windows-specific code to acquire and release
4  * privileges needed to backup and restore files */
5
6 #ifndef __WIN32__
7 #  error "This file contains Windows code"
8 #endif
9
10 #include "imagex-win32.h"
11 #include <windows.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <assert.h>
15 #include <stdio.h>
16
17 /* Replacement for glob() in Windows native builds. */
18 int glob(const char *pattern, int flags,
19          int (*errfunc)(const char *epath, int eerrno),
20          glob_t *pglob)
21 {
22         WIN32_FIND_DATA dat;
23         DWORD err;
24         HANDLE hFind;
25         int ret;
26         size_t nspaces;
27
28         /* This function does not support all functionality of the POSIX glob(),
29          * so make sure the parameters are consistent with supported
30          * functionality. */
31         assert(errfunc == NULL);
32         assert((flags & GLOB_ERR) == GLOB_ERR);
33         assert((flags & ~(GLOB_NOSORT | GLOB_ERR)) == 0);
34
35         hFind = FindFirstFileA(pattern, &dat);
36         if (hFind == INVALID_HANDLE_VALUE) {
37                 err = GetLastError();
38                 if (err == ERROR_FILE_NOT_FOUND) {
39                         errno = 0;
40                         return GLOB_NOMATCH;
41                 } else {
42                         /* The other possible error codes for FindFirstFile()
43                          * are undocumented. */
44                         errno = EIO;
45                         return GLOB_ABORTED;
46                 }
47         }
48         pglob->gl_pathc = 0;
49         pglob->gl_pathv = NULL;
50         nspaces = 0;
51         do {
52                 char *filename;
53                 if (pglob->gl_pathc == nspaces) {
54                         size_t new_nspaces;
55                         char **pathv;
56
57                         new_nspaces = nspaces * 2 + 1;  
58                         pathv = realloc(pglob->gl_pathv,
59                                         new_nspaces * sizeof(pglob->gl_pathv[0]));
60                         if (!pathv)
61                                 goto oom;
62                         pglob->gl_pathv = pathv;
63                         nspaces = new_nspaces;
64                 }
65                 filename = strdup(dat.cFileName);
66                 if (!filename)
67                         goto oom;
68                 pglob->gl_pathv[pglob->gl_pathc++] = filename;
69         } while (FindNextFileA(hFind, &dat));
70         err = GetLastError();
71         CloseHandle(hFind);
72         if (err == ERROR_NO_MORE_FILES) {
73                 errno = 0;
74                 return 0;
75         } else {
76                 /* Other possible error codes for FindNextFile() are
77                  * undocumented */
78                 errno = EIO;
79                 ret = GLOB_ABORTED;
80                 goto fail_globfree;
81         }
82 oom:
83         CloseHandle(hFind);
84         errno = ENOMEM;
85         ret = GLOB_NOSPACE;
86 fail_globfree:
87         globfree(pglob);
88         return ret;
89 }
90
91 void globfree(glob_t *pglob)
92 {
93         size_t i;
94         for (i = 0; i < pglob->gl_pathc; i++)
95                 free(pglob->gl_pathv[i]);
96         free(pglob->gl_pathv[i]);
97 }
98
99 static bool
100 win32_modify_privilege(const char *privilege, bool enable)
101 {
102         HANDLE hToken;
103         LUID luid;
104         TOKEN_PRIVILEGES newState;
105         bool ret = false;
106
107         if (!OpenProcessToken(GetCurrentProcess(),
108                               TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
109                               &hToken))
110         {
111                 goto out;
112         }
113
114         if (!LookupPrivilegeValue(NULL, privilege, &luid)) {
115                 goto out;
116         }
117
118         newState.PrivilegeCount = 1;
119         newState.Privileges[0].Luid = luid;
120         newState.Privileges[0].Attributes = (enable ? SE_PRIVILEGE_ENABLED : 0);
121         ret = AdjustTokenPrivileges(hToken, FALSE, &newState, 0, NULL, NULL);
122         CloseHandle(hToken);
123 out:
124         if (!ret) {
125                 fprintf(stderr, "WARNING: Failed to %s privilege %s\n",
126                         enable ? "enable" : "disable", privilege);
127                 fprintf(stderr,
128                         "WARNING: The program will continue, "
129                         "but if permission issues are\n"
130                         "encountered, you may need to run "
131                         "this program as the administrator\n");
132         }
133         return ret;
134 }
135
136 static void
137 win32_modify_capture_privileges(bool enable)
138 {
139         win32_modify_privilege(SE_BACKUP_NAME, enable);
140         win32_modify_privilege(SE_SECURITY_NAME, enable);
141 }
142
143 static void
144 win32_modify_restore_privileges(bool enable)
145 {
146         win32_modify_privilege(SE_RESTORE_NAME, enable);
147         win32_modify_privilege(SE_SECURITY_NAME, enable);
148         win32_modify_privilege(SE_TAKE_OWNERSHIP_NAME, enable);
149 }
150
151 void
152 win32_acquire_capture_privileges()
153 {
154         win32_modify_capture_privileges(true);
155 }
156
157 void
158 win32_release_capture_privileges()
159 {
160         win32_modify_capture_privileges(false);
161 }
162
163 void
164 win32_acquire_restore_privileges()
165 {
166         win32_modify_restore_privileges(true);
167 }
168
169 void
170 win32_release_restore_privileges()
171 {
172         win32_modify_restore_privileges(false);
173 }