I've been working on a way to access and modify privileges to a file on Windows via Python 3, more precisely with the win32security library.
From those 2 answers How to authorize/deny write access to a directory on Windows using Python? and Setting folder permissions in Windows using Python, I've came up with this:
sd = win32security.GetFileSecurity(TESTFILE,win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()
dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION, 3, 2032127, win32security.LookupAccountName("", "theUser")[0])
sd.SetSecurityDescriptorDacl(1, dacl, 0)
win32security.SetFileSecurity(TESTFILE,win32security.DACL_SECURITY_INFORMATION,sd)
This works like a charm with files that dosen't require administrator prompt, but when accessing protected files this error shows :
pywintypes.error: (5, 'SetFileSecurity', 'Access is denied.')
Consequently, I'm looking for a way to impersonate the current user with win32security.ImpersonateLoggedOnUser(...)
. However, I can't find the right arguments to put as a parameter. Python 2.6 had a win32Security.LogonUser
but not the 3.4 version.
Can somebody point my the right arguments or the appropriate documentation for this?