【问题标题】:Python & win32security - Appcrash on setting permissionsPython & win32security - Appcrash 设置权限
【发布时间】:2021-08-08 12:09:06
【问题描述】:

我创建了一个 python 工具来设置我们文件服务器的权限。 它正在工作 - 但不稳定。 如下所示,有功能“set_permission”和“delete_permission”。 “path_and_rights”参数是一个元组,包含路径为 str,权限为 int,继承为 int。这个函数在一个系列中被多次调用。

Python-Process 的 CPU 和 RAM 随机运行,直到 os (Win Server 2012R2) 杀死该进程。 关键的地方是 dacl.SetEntriesInAcl([newacl,]) - 我添加了一个“time.sleep(5)”,这有点帮助,所以我认为后台有一些非阻塞的东西,下一次访问会遇到麻烦 (?) 重新启动后,有问题的路径可以顺利处理,并且在函数的下一次调用之一中发生崩溃。 (1 到 10)。

附加信息: 代码在 Win10 下运行良好 - 可能是 Win Server 2012 下的错误?

关于解决这个问题的一些想法? 非常感谢!

import win32security
import time

def set_permission(path_and_rights,principal):
    usr=win32security.LookupAccountName(None,principal)[0]
    sd = win32security.GetNamedSecurityInfo(path_and_rights[0], win32security.SE_FILE_OBJECT,  win32security.DACL_SECURITY_INFORMATION)
    dacl=sd.GetSecurityDescriptorDacl()
    newacl={
        'AccessMode': win32security.GRANT_ACCESS,
        'AccessPermissions': path_and_rights[1],
        'Inheritance': path_and_rights[2],
        'Trustee':{
            'TrusteeType': win32security.TRUSTEE_IS_GROUP,
            'TrusteeForm': win32security.TRUSTEE_IS_SID,
            'Identifier': usr}}
    time.sleep(5)
    dacl.SetEntriesInAcl([newacl,])
    time.sleep(5)
    win32security.SetNamedSecurityInfo(path_and_rights[0], win32security.SE_FILE_OBJECT,
        win32security.DACL_SECURITY_INFORMATION |
        win32security.UNPROTECTED_DACL_SECURITY_INFORMATION,
        None, None, dacl, None)

def delete_permission(path_and_rights,principal):
    usr=win32security.LookupAccountName(None,principal)[0]
    sd = win32security.GetNamedSecurityInfo(path_and_rights[0], win32security.SE_FILE_OBJECT, win32security.DACL_SECURITY_INFORMATION)
    dacl=sd.GetSecurityDescriptorDacl()
    for aclnum in range(dacl.GetAceCount()):
        aacl=dacl.GetAce(aclnum)
        if aacl[2] == usr and aacl[1] == path_and_rights[1] and aacl[0][1] == path_and_rights[2]:
            dacl.DeleteAce(aclnum)
            win32security.SetNamedSecurityInfo(path_and_rights[0], win32security.SE_FILE_OBJECT,
            win32security.DACL_SECURITY_INFORMATION |
            win32security.UNPROTECTED_DACL_SECURITY_INFORMATION,
            None, None, dacl, None)
            return

这是 APPCRASH 上的消息:

Problemsignatur:
  Problemereignisname:  APPCRASH
  Anwendungsname:   python.exe
  Anwendungsversion:    3.8.6150.1013
  Anwendungszeitstempel:    5f6b7010
  Fehlermodulname:  StackHash_6776
  Fehlermodulversion:   6.3.9600.19994
  Fehlermodulzeitstempel:   60653cd2
  Ausnahmecode: c0000374
  Ausnahmeoffset:   PCH_63_FROM_ntdll+0x0000000000090B9A
  Betriebsystemversion: 6.3.9600.2.0.0.400.8

编辑: 我在另一台机器上测试 - 相同的操作系统,相同的 python 版本(3.8.6): 完美运行。

所以现在我必须找出还有什么原因? 有关如何调试的任何建议?

【问题讨论】:

  • 这似乎是 Windows Server 的问题:数据库维护实用程序损坏了包含非 ASCII 字符的 Windows 组名,从而导致 SetEntriesInAcl 错误。
  • 在任何 acl(现有 + 新)中都找不到非 ASCII 字符。这也不能解释为什么它在其他服务器上运行良好。我希望现在在此服务器上运行的任何其他程序都会引起麻烦。现在我在每一步之间增加了 40 秒的等待时间 - 不再有错误......

标签: python winapi file-permissions pywin32


【解决方案1】:

我完全改变了我的代码,现在可以毫无问题地使用“pythonnet”。 感谢阅读和帮助!

(也许有人知道win32-libary的解决方案,请随时在此处发布)

import clr
import System

from System.IO import Directory
from System.Security.AccessControl import (
    AccessControlType,
    FileSystemAccessRule,
    FileSystemRights,
    PropagationFlags,
)

def set_permission(path_and_rights,principal):
    accessControl = Directory.GetAccessControl(path_and_rights[0])
    accessRule = FileSystemAccessRule(principal,
                                      path_and_rights[1],
                                      path_and_rights[2],
                                      getattr(PropagationFlags, "None"),
                                      AccessControlType.Allow,
                                      )
    accessControl.AddAccessRule(accessRule)
    Directory.SetAccessControl(path_and_rights[0], accessControl)

def delete_permission(path_and_rights,principal):
    accessControl = Directory.GetAccessControl(path_and_rights[0])
    accessRule = FileSystemAccessRule(principal,
                                      path_and_rights[1],
                                      path_and_rights[2],
                                      getattr(PropagationFlags, "None"),
                                      AccessControlType.Allow,
                                      )
    accessControl.RemoveAccessRuleAll(accessRule)
    Directory.SetAccessControl(path_and_rights[0], accessControl)```

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-28
    • 2011-02-14
    • 2018-12-06
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 2015-08-01
    相关资源
    最近更新 更多