Search code examples
pythonwindowsaclpywin32

creating permissions on shared windows folder with python


I'm using this code in python 3.3 with the pywin32 library to share a folder. How can I now add pemissions to the folder? The following code sets no permissions to the shared folder. I want to add a specific user as read/write

import win32net
import win32netcon

shinfo={}

shinfo['netname']='python test'
shinfo['type']=win32netcon.STYPE_DISKTREE
shinfo['remark']='data files'
shinfo['permissions']=0
shinfo['max_uses']=-1
shinfo['current_uses']=0
shinfo['path']='C:\\sharedfolder'
shinfo['passwd']=''
server='192.168.1.100'

win32net.NetShareAdd(server,2,shinfo)

Solution

  • An alternative to the win32security module is to wimp out and use the cacls program, which is rather easier to use, see at http://support.microsoft.com/kb/162786/en-us for example :

    from subprocess import *
    
    proc = Popen("echo y|cacls filename /E /G BUILTIN\\Users:R", shell=True) 
    
    proc.wait()
    
    print "Child exited with",proc.returncode
    

    The echo y is because this stupid program asks an "are you sure?" question. On windows 7, cacls is deprecated (but still works), use icacls instead (or xcalcs from the resource kit).

    Of course creating child processes to do this will not be as efficient as calling the Win32 API.