|
# coding: utf-8 |
|
|
|
import win32net |
|
import win32netcon |
|
import win32security |
|
import win32file |
|
|
|
def create_share(directory, share_name): |
|
"""Create a windows share |
|
""" |
|
|
|
# Everyone account |
|
everyone_sid = win32security.ConvertStringSidToSid('S-1-1-0') |
|
|
|
# Set folder permissions to everyone |
|
sd = win32security.GetFileSecurity(directory, win32security.DACL_SECURITY_INFORMATION) |
|
dacl = sd.GetSecurityDescriptorDacl() |
|
dacl.AddAccessAllowedAceEx(win32security.ACL_REVISION_DS, |
|
win32security.OBJECT_INHERIT_ACE | win32security.CONTAINER_INHERIT_ACE, |
|
win32file.FILE_ALL_ACCESS, everyone_sid) |
|
sd.SetSecurityDescriptorDacl(1, dacl, 0) |
|
win32security.SetFileSecurity(directory, win32security.DACL_SECURITY_INFORMATION, sd) |
|
|
|
# create share |
|
try: |
|
shinfo = { |
|
'netname': share_name, |
|
'type': win32netcon.STYPE_DISKTREE, |
|
'remark': 'data files', |
|
'permissions': 0, |
|
'max_uses': -1, |
|
'current_uses': 0, |
|
'path': directory, |
|
'passwd': ''} |
|
server = 'localhost' |
|
win32net.NetShareAdd(server, 2, shinfo) |
|
except Exception: |
|
pass |
|
|
|
# Set share permissions to everyone |
|
sd = win32security.GetNamedSecurityInfo(share_name, win32security.SE_LMSHARE, |
|
win32security.DACL_SECURITY_INFORMATION) |
|
if not sd: |
|
sd = win32security.SECURITY_DESCRIPTOR() |
|
dacl = sd.GetSecurityDescriptorDacl() |
|
if not dacl: |
|
dacl = win32security.ACL() |
|
dacl.AddAccessAllowedAce(win32file.FILE_ALL_ACCESS, everyone_sid) |
|
sd.SetSecurityDescriptorDacl(1, dacl, 0) |
|
win32security.SetNamedSecurityInfo(share_name, win32security.SE_LMSHARE, win32security.DACL_SECURITY_INFORMATION, |
|
None, None, dacl) |
|
|
|
|
|
def remove_share(share_name): |
|
"""Removes a windows share |
|
""" |
|
win32net.NetShareDel('localhost', share_name) |
|
|
|
|
|
def get_shares(): |
|
"""Gets all shares on local machine. |
|
""" |
|
shares = [] |
|
lst = list(win32net.NetShareEnum('localhost', 2)) |
|
for item in lst: |
|
if isinstance(item, list): |
|
for share in item: |
|
name = share['netname'] |
|
my_type = share['type'] |
|
if my_type == 0: |
|
shares.append(name) |
|
|
|
return shares |
|
|
|
def is_shared(share_name): |
|
"""Checks if windows share exists |
|
""" |
|
return share_name in get_shares() |