Debian Codegraph @

rpython/rlib/src/winhelpers.c

raw symbols SCIP
1 // #define WIN32_LEAN_AND_MEAN // FSCTL_GET_REPARSE_POINT is not defined in LEAN_AND_MEAN
2 #include <windows.h>
3 #include <winhelpers.h>
4 #include <stdio.h>
5 #include <aclapi.h> // SetEntriesInAcl
6 #include <sddl.h> // SDDL_REVISION_1
7
8 #ifndef RPY_EXPORTED
9 #ifdef __GNUC__
10 # define RPY_EXPORTED extern __attribute__((visibility("default")))
11 #else
12 # define RPY_EXPORTED extern __declspec(dllexport)
13 #endif
14 #endif
15
16 static void __cdecl _silent_invalid_parameter_handler(
17 wchar_t const* expression,
18 wchar_t const* function,
19 wchar_t const* file,
20 unsigned int line,
21 uintptr_t pReserved) { }
22
23 #define _BEGIN_SUPPRESS_IPH { _invalid_parameter_handler _old_handler = \
24 _set_thread_local_invalid_parameter_handler(_silent_invalid_parameter_handler);
25 #define _END_SUPPRESS_IPH _set_thread_local_invalid_parameter_handler(_old_handler); }
26
27
28
29 void* enter_suppress_iph();
30 void exit_suppress_iph(void* handle);
31
32 RPY_EXPORTED int
33 os_readlink_impl(wchar_t *path_wide, void *target_buffer, wchar_t **result) {
34 DWORD n_bytes_returned;
35 DWORD io_result = 0;
36 HANDLE reparse_point_handle;
37 _Py_REPARSE_DATA_BUFFER *rdb = (_Py_REPARSE_DATA_BUFFER *)target_buffer;
38
39 /* First get a handle to the reparse point */
40 reparse_point_handle = CreateFileW(
41 path_wide,
42 0,
43 0,
44 0,
45 OPEN_EXISTING,
46 FILE_FLAG_OPEN_REPARSE_POINT|FILE_FLAG_BACKUP_SEMANTICS,
47 0);
48 if (reparse_point_handle != INVALID_HANDLE_VALUE) {
49 /* New call DeviceIoControl to read the reparse point */
50 io_result = DeviceIoControl(
51 reparse_point_handle,
52 FSCTL_GET_REPARSE_POINT,
53 0, 0, /* in buffer */
54 target_buffer, _Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE,
55 &n_bytes_returned,
56 0 /* we're not using OVERLAPPED_IO */
57 );
58 CloseHandle(reparse_point_handle);
59 }
60
61 if (io_result == 0) {
62 return -1;
63 }
64
65 long nameLen = 0;
66 if (rdb->ReparseTag == IO_REPARSE_TAG_SYMLINK)
67 {
68 result[0] = (wchar_t *)((char*)rdb->SymbolicLinkReparseBuffer.PathBuffer +
69 rdb->SymbolicLinkReparseBuffer.SubstituteNameOffset);
70 nameLen = rdb->SymbolicLinkReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
71 }
72 else if (rdb->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT)
73 {
74 result[0] = (wchar_t*)((char*)rdb->MountPointReparseBuffer.PathBuffer +
75 rdb->MountPointReparseBuffer.SubstituteNameOffset);
76 nameLen = rdb->MountPointReparseBuffer.SubstituteNameLength / sizeof(wchar_t);
77 }
78 else
79 {
80 result[0] = NULL;
81 return -2;
82 }
83 if (nameLen > 4 && wcsncmp(result[0], L"\\??\\", 4) == 0) {
84 result[0][1] = L'\\';
85 }
86 return (int)nameLen;
87 }
88
89 /* Remove the last portion of the path - return 0 on success */
90 static int
91 _dirnameW(WCHAR *path)
92 {
93 WCHAR *ptr;
94 size_t length = wcsnlen_s(path, MAX_PATH);
95 if (length == MAX_PATH) {
96 return -1;
97 }
98
99 /* walk the path from the end until a backslash is encountered */
100 for(ptr = path + length; ptr != path; ptr--) {
101 if (*ptr == L'\\' || *ptr == L'/') {
102 break;
103 }
104 }
105 *ptr = 0;
106 return 0;
107 }
108
109 /* Is this path absolute? */
110 static int
111 _is_absW(const WCHAR *path)
112 {
113 return path[0] == L'\\' || path[0] == L'/' ||
114 (path[0] && path[1] == L':');
115 }
116
117 /* join root and rest with a backslash - return 0 on success */
118 static int
119 _joinW(WCHAR *dest_path, const WCHAR *root, const WCHAR *rest)
120 {
121 if (_is_absW(rest)) {
122 return wcscpy_s(dest_path, MAX_PATH, rest);
123 }
124
125 if (wcscpy_s(dest_path, MAX_PATH, root)) {
126 return -1;
127 }
128
129 if (dest_path[0] && wcscat_s(dest_path, MAX_PATH, L"\\")) {
130 return -1;
131 }
132
133 return wcscat_s(dest_path, MAX_PATH, rest);
134 }
135
136 /* Return True if the path at src relative to dest is a directory */
137 static int
138 _check_dirW(LPCWSTR src, LPCWSTR dest)
139 {
140 WIN32_FILE_ATTRIBUTE_DATA src_info;
141 WCHAR dest_parent[MAX_PATH];
142 WCHAR src_resolved[MAX_PATH] = L"";
143
144 /* dest_parent = os.path.dirname(dest) */
145 if (wcscpy_s(dest_parent, MAX_PATH, dest) ||
146 _dirnameW(dest_parent)) {
147 return 0;
148 }
149 /* src_resolved = os.path.join(dest_parent, src) */
150 if (_joinW(src_resolved, dest_parent, src)) {
151 return 0;
152 }
153 return (
154 GetFileAttributesExW(src_resolved, GetFileExInfoStandard, &src_info)
155 && src_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY
156 );
157 }
158
159 RPY_EXPORTED int
160 os_symlink_impl(wchar_t *src, wchar_t *dst, int target_is_directory)
161 {
162 DWORD result;
163 DWORD flags = 0;
164
165 /* Assumed true, set to false if detected to not be available. */
166 static int windows_has_symlink_unprivileged_flag = TRUE;
167 if (windows_has_symlink_unprivileged_flag) {
168 /* Allow non-admin symlinks if system allows it. */
169 flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
170 }
171
172 _BEGIN_SUPPRESS_IPH
173 /* if src is a directory, ensure flags==1 (target_is_directory bit) */
174 if (target_is_directory || _check_dirW(src, dst)) {
175 flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
176 }
177
178 result = CreateSymbolicLinkW(dst, src, flags);
179 _END_SUPPRESS_IPH
180
181 if (windows_has_symlink_unprivileged_flag && !result &&
182 ERROR_INVALID_PARAMETER == GetLastError()) {
183
184 _BEGIN_SUPPRESS_IPH
185 /* This error might be caused by
186 SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE not being supported.
187 Try again, and update windows_has_symlink_unprivileged_flag if we
188 are successful this time.
189
190 NOTE: There is a risk of a race condition here if there are other
191 conditions than the flag causing ERROR_INVALID_PARAMETER, and
192 another process (or thread) changes that condition in between our
193 calls to CreateSymbolicLink.
194 */
195 flags &= ~(SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE);
196 result = CreateSymbolicLinkW(dst, src, flags);
197 _END_SUPPRESS_IPH
198
199 if (result || ERROR_INVALID_PARAMETER != GetLastError()) {
200 windows_has_symlink_unprivileged_flag = FALSE;
201 }
202 }
203 return (int)result;
204 }
205
206 RPY_EXPORTED int
207 os_unlink_impl(wchar_t *lpFileName)
208 {
209 /* Taken from Py_DeleteFileW in posixmodule.c */
210 WIN32_FILE_ATTRIBUTE_DATA info;
211 WIN32_FIND_DATAW find_data;
212 HANDLE find_data_handle;
213 int is_directory = 0;
214 int is_link = 0;
215
216 if (GetFileAttributesExW(lpFileName, GetFileExInfoStandard, &info)) {
217 is_directory = info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
218
219 /* Get WIN32_FIND_DATA structure for the path to determine if
220 it is a symlink */
221 if(is_directory &&
222 info.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) {
223 find_data_handle = FindFirstFileW(lpFileName, &find_data);
224
225 if(find_data_handle != INVALID_HANDLE_VALUE) {
226 /* IO_REPARSE_TAG_SYMLINK if it is a symlink and
227 IO_REPARSE_TAG_MOUNT_POINT if it is a junction point. */
228 is_link = find_data.dwReserved0 == IO_REPARSE_TAG_SYMLINK ||
229 find_data.dwReserved0 == IO_REPARSE_TAG_MOUNT_POINT;
230 FindClose(find_data_handle);
231 }
232 }
233 }
234
235 if (is_directory && is_link)
236 return (int)RemoveDirectoryW(lpFileName);
237
238 return (int)DeleteFileW(lpFileName);
239 }
240
241 int
242 os_createdirectory_impl(wchar_t *path, int mode){
243 int result = 0;
244 int error = 0;
245 SECURITY_ATTRIBUTES secAttr = { sizeof(secAttr) };
246 SECURITY_ATTRIBUTES *pSecAttr = NULL;
247 if (mode == 0700 /* 0o700 */) {
248 ULONG sdSize;
249 pSecAttr = &secAttr;
250 // Set a discretionary ACL (D) that is protected (P) and includes
251 // inheritable (OICI) entries that allow (A) full control (FA) to
252 // SYSTEM (SY), Administrators (BA), and the owner (OW).
253 if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(
254 L"D:P(A;OICI;FA;;;SY)(A;OICI;FA;;;BA)(A;OICI;FA;;;OW)",
255 SDDL_REVISION_1,
256 &secAttr.lpSecurityDescriptor,
257 &sdSize
258 )) {
259 error = GetLastError();
260 }
261 }
262 if (!error) {
263 result = CreateDirectoryW(path, pSecAttr);
264 if (secAttr.lpSecurityDescriptor &&
265 // uncommonly, LocalFree returns non-zero on error, but still uses
266 // GetLastError() to see what the error code is
267 LocalFree(secAttr.lpSecurityDescriptor)) {
268 error = GetLastError();
269 }
270 }
271 if (error) {
272 return -1;
273 }
274 if (!result) {
275 return -2;
276 }
277 return 0;
278 }
279