python-win32security通过跨域访问为NAS中的文件夹添加ACL
我正在处理一个创建文件夹和添加安全组的用例。我正在使用下面的代码。当我手动执行此操作以访问共享路径时,我们输入凭证并创建一个文件夹帖子,一旦我单击“安全”选项卡,它将再次提示输入凭证,并填充相同的和安全组。这是因为从不同的域访问共享位置是预期的。现在,当我尝试用下面的代码通过python来实现这一点时,我可以创建文件夹,但无法添加安全组,因为脚本是从不同域中的服务器运行的 错误(1332,LookupAccountName'未完成帐户名和安全ID之间的映射。) 因此,基本上我们可以在访问安全选项卡时设置权限,并将permmission设置为相同的权限 请帮忙python-win32security通过跨域访问为NAS中的文件夹添加ACL,python,winapi,acl,pywin32,pywin,Python,Winapi,Acl,Pywin32,Pywin,我正在处理一个创建文件夹和添加安全组的用例。我正在使用下面的代码。当我手动执行此操作以访问共享路径时,我们输入凭证并创建一个文件夹帖子,一旦我单击“安全”选项卡,它将再次提示输入凭证,并填充相同的和安全组。这是因为从不同的域访问共享位置是预期的。现在,当我尝试用下面的代码通过python来实现这一点时,我可以创建文件夹,但无法添加安全组,因为脚本是从不同域中的服务器运行的 错误(1332,LookupAccountName'未完成帐户名和安全ID之间的映射。) 因此,基本上我们可以在访问安全选项
class Create(Resource):
def post(self):
# Get JSON arguments from Payload shared NAS path, directorname groupname with read access and right access
parentdir = request.json.get("path")
dirname = request.json.get("name")
readGroup = request.json.get("readGroup")
# Access the NAS path through NAS credentails
class Impersonate:
def __init__(self,user,password):
#Update domain to access the shared NAS
self.domain_name = "domain"
self.user = user
self.password = password
logging.debug("Credentials Received: {} ".format(self.user))
def logon(self):
self.handle=win32security.LogonUser(self.user,self.domain_name,self.password,win32con.LOGON32_LOGON_INTERACTIVE,win32con.LOGON32_PROVIDER_DEFAULT)
win32security.ImpersonateLoggedOnUser(self.handle)
def logoff(self):
win32security.RevertToSelf() #terminates impersonation
self.handle.Close() #guarantees cleanup
if __name__ == "__main__":
#update username and password of the NAS path below within quotes
a=Impersonate('user','Password')
try:
a.logon() #Logon to NAS path with supplied credentails.
try:
logging.debug("Sucessfully connectd to NAS path {} ".format(parentdir))
# makedirs create directory recursively
os.makedirs(path)
try:
groupr, domain, type = win32security.LookupAccountName ("", readGroup)
sd = win32security.GetFileSecurity(path, win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl()
dacl.AddAccessAllowedAce(win32security.ACL_REVISION,win32con.GENERIC_READ, groupr)
#os.makedirs(path)
except OSError as e:
if e.errno == errno.EEXIST:
print(e)
resp = Response('{} fileshare creation created, adding security group {} with read permessions failed. Error:{}'.format(dirname, groupr, e))
print (resp)
resp.status_code = 201
return resp
except OSError as error:
print(error)
resp = Response('{} fileshare creation failed. Error is {} '.format(dirname, error))
print (resp)
resp.status_code = 300
return resp
#return ("Fileshare creation failed: {} ".format(dirname))
except Exception as error1:
print(error1)
logging.error("Failed to connect to NAS path{}, Error: {} ".format(parentdir, error1))
resp = Response('Could not connect to UNC Shared path. Error{}'.format(error1))
print (resp)
resp.status_code = 201
return resp
a.logoff()
您没有设置
lpSystemName
,并且根据:“如果此字符串为空,则帐户名转换在本地系统上开始。如果无法在本地系统上解析名称,则此函数将尝试使用本地系统信任的域控制器解析名称…”您的服务器要检索的域是否符合文档要求?@DrakeWu MSFT谢谢。服务器位于不同的域中,我正在另一个域中查找。我把文件看了一遍。我不确定如何指定lpSystemName。我该怎么做呢。任何指针请…谢谢为lpSystemName指定一个值仅当帐户位于不受信任的域中且该域中的计算机名已知时,您可以尝试指定该域中的计算机名。谢谢@DrakeWu MSFT。我将组名作为变量传递。我应该同时传递lpSystemName吗。此外,如果它是一个不受信任的多米安如何验证相同。如果它也可以是同一领域的,那么可能会有用例。那么我该如何验证呢。很抱歉,我是新来的,因此需要一些帮助。如果您不确定远程计算机名是否位于不受信任的域上,最好指定一个远程计算机名(其中包含组),并使用完全限定的帐户名(例如,域名\组名),而不是孤立的名称(例如,组名)。