[python-win32] Changing Windows NT File Permissions

Justin Johnson justinjohnson at gmail.com
Fri Jul 16 17:24:35 CEST 2004


Hello,

I am trying to write a script that will add a user to a file's
permission list with full control to the file.  I have the following
code, but I'm not sure what the 2nd option to
win32security.SetFileSecurity is supposed to be.  The ActiveState docs
at http://aspn.activestate.com/ASPN/docs/ActivePython/2.3/PyWin32/win32security__SetFileSecurity_meth.html
 say the following:

info : int

The type of information to set.

I tried setting it to 0 but nothing seems to happen and I don't get
any error.  When I set it to 1 I get the following error:


C:\temp\perm>python sec.py ms\jjohn53
Traceback (most recent call last):
  File "sec.py", line 23, in ?
    win32security.SetFileSecurity(fileName, 1, sd)
pywintypes.error: (1338, 'SetFileSecurity', 'The security descriptor structure i
s invalid.')

Does anyone know what I need to do to accomplish my goal?  My script
is attached, with 1  as the 2nd option to SetFileSecurity.

Thanks much.
-Justin
-------------- next part --------------
import win32security
import pywintypes
import ntsecuritycon
import win32file
import sys

def createSD(userName):
    sd = pywintypes.SECURITY_DESCRIPTOR()
    sidUser = win32security.LookupAccountName(None,userName)[0]
    sidCreator = pywintypes.SID()
    sidCreator.Initialize(ntsecuritycon.SECURITY_CREATOR_SID_AUTHORITY,1)
    sidCreator.SetSubAuthority(0, ntsecuritycon.SECURITY_CREATOR_OWNER_RID)
    acl = pywintypes.ACL()
    acl.AddAccessAllowedAce(win32file.FILE_ALL_ACCESS, sidUser)
    acl.AddAccessAllowedAce(win32file.FILE_ALL_ACCESS, sidCreator)
    sd.SetSecurityDescriptorDacl(1, acl, 0)
    return sd

fileName = "file.txt"
userId = "test"
sd = createSD(userId)

win32security.SetFileSecurity(fileName, 1, sd)


More information about the Python-win32 mailing list