2
Windows API functions implemented as ctypes functions and classes as found
3
in jaraco.windows (3.4.1).
5
If you encounter issues with this module, please consider reporting the issues
6
in jaraco.windows and asking the author to port the fixes back here.
10
import ctypes.wintypes
12
from paramiko.py3compat import u, builtins
15
######################
16
# jaraco.windows.error
18
def format_system_message(errno):
20
Call FormatMessage with a system error number to retrieve
21
the descriptive error message.
23
# first some flags used by FormatMessageW
24
ALLOCATE_BUFFER = 0x100
27
# Let FormatMessageW allocate the buffer (we'll free it below)
28
# Also, let it know we want a system error message.
29
flags = ALLOCATE_BUFFER | FROM_SYSTEM
33
result_buffer = ctypes.wintypes.LPWSTR()
36
bytes = ctypes.windll.kernel32.FormatMessageW(
41
ctypes.byref(result_buffer),
45
# note the following will cause an infinite loop if GetLastError
46
# repeatedly returns an error that cannot be formatted, although
47
# this should not happen.
48
handle_nonzero_success(bytes)
49
message = result_buffer.value
50
ctypes.windll.kernel32.LocalFree(result_buffer)
54
class WindowsError(builtins.WindowsError):
55
"""more info about errors at
56
http://msdn.microsoft.com/en-us/library/ms681381(VS.85).aspx"""
58
def __init__(self, value=None):
60
value = ctypes.windll.kernel32.GetLastError()
61
strerror = format_system_message(value)
62
if sys.version_info > (3, 3):
63
args = 0, strerror, None, value
65
args = value, strerror
66
super(WindowsError, self).__init__(*args)
80
return '{self.__class__.__name__}({self.winerror})'.format(**vars())
83
def handle_nonzero_success(result):
88
###########################
89
# jaraco.windows.api.memory
93
GlobalAlloc = ctypes.windll.kernel32.GlobalAlloc
94
GlobalAlloc.argtypes = ctypes.wintypes.UINT, ctypes.c_size_t
95
GlobalAlloc.restype = ctypes.wintypes.HANDLE
97
GlobalLock = ctypes.windll.kernel32.GlobalLock
98
GlobalLock.argtypes = ctypes.wintypes.HGLOBAL,
99
GlobalLock.restype = ctypes.wintypes.LPVOID
101
GlobalUnlock = ctypes.windll.kernel32.GlobalUnlock
102
GlobalUnlock.argtypes = ctypes.wintypes.HGLOBAL,
103
GlobalUnlock.restype = ctypes.wintypes.BOOL
105
GlobalSize = ctypes.windll.kernel32.GlobalSize
106
GlobalSize.argtypes = ctypes.wintypes.HGLOBAL,
107
GlobalSize.restype = ctypes.c_size_t
109
CreateFileMapping = ctypes.windll.kernel32.CreateFileMappingW
110
CreateFileMapping.argtypes = [
111
ctypes.wintypes.HANDLE,
113
ctypes.wintypes.DWORD,
114
ctypes.wintypes.DWORD,
115
ctypes.wintypes.DWORD,
116
ctypes.wintypes.LPWSTR,
118
CreateFileMapping.restype = ctypes.wintypes.HANDLE
120
MapViewOfFile = ctypes.windll.kernel32.MapViewOfFile
121
MapViewOfFile.restype = ctypes.wintypes.HANDLE
123
UnmapViewOfFile = ctypes.windll.kernel32.UnmapViewOfFile
124
UnmapViewOfFile.argtypes = ctypes.wintypes.HANDLE,
126
RtlMoveMemory = ctypes.windll.kernel32.RtlMoveMemory
127
RtlMoveMemory.argtypes = (
133
ctypes.windll.kernel32.LocalFree.argtypes = ctypes.wintypes.HLOCAL,
135
#####################
136
# jaraco.windows.mmap
139
class MemoryMap(object):
141
A memory map object which can have security attributes overridden.
143
def __init__(self, name, length, security_attributes=None):
146
self.security_attributes = security_attributes
151
ctypes.byref(self.security_attributes)
152
if self.security_attributes else None
154
INVALID_HANDLE_VALUE = -1
157
filemap = ctypes.windll.kernel32.CreateFileMappingW(
158
INVALID_HANDLE_VALUE, p_SA, PAGE_READWRITE, 0, self.length,
160
handle_nonzero_success(filemap)
161
if filemap == INVALID_HANDLE_VALUE:
162
raise Exception("Failed to create file mapping")
163
self.filemap = filemap
164
self.view = MapViewOfFile(filemap, FILE_MAP_WRITE, 0, 0, 0)
170
def write(self, msg):
171
assert isinstance(msg, bytes)
173
if self.pos + n >= self.length: # A little safety.
174
raise ValueError("Refusing to write %d bytes" % n)
175
dest = self.view + self.pos
176
length = ctypes.c_size_t(n)
177
ctypes.windll.kernel32.RtlMoveMemory(dest, msg, length)
182
Read n bytes from mapped view.
184
out = ctypes.create_string_buffer(n)
185
source = self.view + self.pos
186
length = ctypes.c_size_t(n)
187
ctypes.windll.kernel32.RtlMoveMemory(out, source, length)
191
def __exit__(self, exc_type, exc_val, tb):
192
ctypes.windll.kernel32.UnmapViewOfFile(self.view)
193
ctypes.windll.kernel32.CloseHandle(self.filemap)
196
#############################
197
# jaraco.windows.api.security
200
READ_CONTROL = 0x00020000
201
STANDARD_RIGHTS_REQUIRED = 0x000F0000
202
STANDARD_RIGHTS_READ = READ_CONTROL
203
STANDARD_RIGHTS_WRITE = READ_CONTROL
204
STANDARD_RIGHTS_EXECUTE = READ_CONTROL
205
STANDARD_RIGHTS_ALL = 0x001F0000
208
POLICY_VIEW_LOCAL_INFORMATION = 0x00000001
209
POLICY_VIEW_AUDIT_INFORMATION = 0x00000002
210
POLICY_GET_PRIVATE_INFORMATION = 0x00000004
211
POLICY_TRUST_ADMIN = 0x00000008
212
POLICY_CREATE_ACCOUNT = 0x00000010
213
POLICY_CREATE_SECRET = 0x00000020
214
POLICY_CREATE_PRIVILEGE = 0x00000040
215
POLICY_SET_DEFAULT_QUOTA_LIMITS = 0x00000080
216
POLICY_SET_AUDIT_REQUIREMENTS = 0x00000100
217
POLICY_AUDIT_LOG_ADMIN = 0x00000200
218
POLICY_SERVER_ADMIN = 0x00000400
219
POLICY_LOOKUP_NAMES = 0x00000800
220
POLICY_NOTIFICATION = 0x00001000
222
POLICY_ALL_ACCESS = (
223
STANDARD_RIGHTS_REQUIRED |
224
POLICY_VIEW_LOCAL_INFORMATION |
225
POLICY_VIEW_AUDIT_INFORMATION |
226
POLICY_GET_PRIVATE_INFORMATION |
228
POLICY_CREATE_ACCOUNT |
229
POLICY_CREATE_SECRET |
230
POLICY_CREATE_PRIVILEGE |
231
POLICY_SET_DEFAULT_QUOTA_LIMITS |
232
POLICY_SET_AUDIT_REQUIREMENTS |
233
POLICY_AUDIT_LOG_ADMIN |
234
POLICY_SERVER_ADMIN |
239
STANDARD_RIGHTS_READ |
240
POLICY_VIEW_AUDIT_INFORMATION |
241
POLICY_GET_PRIVATE_INFORMATION)
244
STANDARD_RIGHTS_WRITE |
246
POLICY_CREATE_ACCOUNT |
247
POLICY_CREATE_SECRET |
248
POLICY_CREATE_PRIVILEGE |
249
POLICY_SET_DEFAULT_QUOTA_LIMITS |
250
POLICY_SET_AUDIT_REQUIREMENTS |
251
POLICY_AUDIT_LOG_ADMIN |
255
STANDARD_RIGHTS_EXECUTE |
256
POLICY_VIEW_LOCAL_INFORMATION |
264
class TokenInformationClass:
268
class TOKEN_USER(ctypes.Structure):
271
('SID', ctypes.c_void_p),
272
('ATTRIBUTES', ctypes.wintypes.DWORD),
276
class SECURITY_DESCRIPTOR(ctypes.Structure):
278
typedef struct _SECURITY_DESCRIPTOR
282
SECURITY_DESCRIPTOR_CONTROL Control;
287
} SECURITY_DESCRIPTOR;
289
SECURITY_DESCRIPTOR_CONTROL = ctypes.wintypes.USHORT
293
('Revision', ctypes.c_ubyte),
294
('Sbz1', ctypes.c_ubyte),
295
('Control', SECURITY_DESCRIPTOR_CONTROL),
296
('Owner', ctypes.c_void_p),
297
('Group', ctypes.c_void_p),
298
('Sacl', ctypes.c_void_p),
299
('Dacl', ctypes.c_void_p),
303
class SECURITY_ATTRIBUTES(ctypes.Structure):
305
typedef struct _SECURITY_ATTRIBUTES {
307
LPVOID lpSecurityDescriptor;
309
} SECURITY_ATTRIBUTES;
312
('nLength', ctypes.wintypes.DWORD),
313
('lpSecurityDescriptor', ctypes.c_void_p),
314
('bInheritHandle', ctypes.wintypes.BOOL),
317
def __init__(self, *args, **kwargs):
318
super(SECURITY_ATTRIBUTES, self).__init__(*args, **kwargs)
319
self.nLength = ctypes.sizeof(SECURITY_ATTRIBUTES)
322
def descriptor(self):
323
return self._descriptor
326
def descriptor(self, value):
327
self._descriptor = value
328
self.lpSecurityDescriptor = ctypes.addressof(value)
331
ctypes.windll.advapi32.SetSecurityDescriptorOwner.argtypes = (
332
ctypes.POINTER(SECURITY_DESCRIPTOR),
334
ctypes.wintypes.BOOL,
337
#########################
338
# jaraco.windows.security
341
def GetTokenInformation(token, information_class):
343
Given a token, get the token information for it.
345
data_size = ctypes.wintypes.DWORD()
346
ctypes.windll.advapi32.GetTokenInformation(token, information_class.num,
347
0, 0, ctypes.byref(data_size))
348
data = ctypes.create_string_buffer(data_size.value)
349
handle_nonzero_success(ctypes.windll.advapi32.GetTokenInformation(token,
350
information_class.num,
351
ctypes.byref(data), ctypes.sizeof(data),
352
ctypes.byref(data_size)))
353
return ctypes.cast(data, ctypes.POINTER(TOKEN_USER)).contents
356
def OpenProcessToken(proc_handle, access):
357
result = ctypes.wintypes.HANDLE()
358
proc_handle = ctypes.wintypes.HANDLE(proc_handle)
359
handle_nonzero_success(ctypes.windll.advapi32.OpenProcessToken(
360
proc_handle, access, ctypes.byref(result)))
364
def get_current_user():
366
Return a TOKEN_USER for the owner of this process.
368
process = OpenProcessToken(
369
ctypes.windll.kernel32.GetCurrentProcess(),
370
TokenAccess.TOKEN_QUERY,
372
return GetTokenInformation(process, TOKEN_USER)
375
def get_security_attributes_for_user(user=None):
377
Return a SECURITY_ATTRIBUTES structure with the SID set to the
378
specified user (uses current user if none is specified).
381
user = get_current_user()
383
assert isinstance(user, TOKEN_USER), "user must be TOKEN_USER instance"
385
SD = SECURITY_DESCRIPTOR()
386
SA = SECURITY_ATTRIBUTES()
387
# by attaching the actual security descriptor, it will be garbage-
388
# collected with the security attributes
390
SA.bInheritHandle = 1
392
ctypes.windll.advapi32.InitializeSecurityDescriptor(ctypes.byref(SD),
393
SECURITY_DESCRIPTOR.REVISION)
394
ctypes.windll.advapi32.SetSecurityDescriptorOwner(ctypes.byref(SD),