设置权限时Python和win32security应用程序崩溃

2024-03-29 14:56:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建了一个python工具来设置文件服务器的权限。 它在工作,但不稳定。 下面是“设置权限”和“删除权限”功能。 “path_和_rights”参数是一个元组,包含path作为str、permissions作为int和inheritation作为int。这个函数在一个系列中被多次调用

Python进程的CPU和RAM随机运行,直到os(winserver2012r2)杀死进程。 关键的地方是dacl.setEntriesAcl([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): 运转良好

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


Tags: andpathnoneprincipalinformationtimeusrsd
1条回答
网友
1楼 · 发布于 2024-03-29 14:56:07

我完全修改了我的代码,现在使用“pythonnet”没有任何问题。 谢谢你的阅读和评论;救命啊

(可能有人知道使用win32库的解决方案,请随意在此处发布)

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)```

相关问题 更多 >