| 1 |
|
| 2 |
#include <windows.h> |
| 3 |
#include <winhelpers.h> |
| 4 |
#include <stdio.h> |
| 5 |
#include <aclapi.h> |
| 6 |
#include <sddl.h> |
| 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 |
|
| 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 |
|
| 50 |
io_result = DeviceIoControl( |
| 51 |
reparse_point_handle, |
| 52 |
FSCTL_GET_REPARSE_POINT, |
| 53 |
0, 0, |
| 54 |
target_buffer, _Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE, |
| 55 |
&n_bytes_returned, |
| 56 |
0 |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 145 |
if (wcscpy_s(dest_parent, MAX_PATH, dest) || |
| 146 |
_dirnameW(dest_parent)) { |
| 147 |
return 0; |
| 148 |
} |
| 149 |
|
| 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 |
|
| 166 |
static int windows_has_symlink_unprivileged_flag = TRUE; |
| 167 |
if (windows_has_symlink_unprivileged_flag) { |
| 168 |
|
| 169 |
flags |= SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE; |
| 170 |
} |
| 171 |
|
| 172 |
_BEGIN_SUPPRESS_IPH |
| 173 |
|
| 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 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 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 |
|
| 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 |
|
| 220 |
|
| 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 |
|
| 227 |
|
| 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 ) { |
| 248 |
ULONG sdSize; |
| 249 |
pSecAttr = &secAttr; |
| 250 |
|
| 251 |
|
| 252 |
|
| 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 |
|
| 266 |
|
| 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 |
|