<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> import os <EOL> import logging <EOL> import salt . utils <EOL> from salt . exceptions import CommandExecutionError <EOL> from salt . ext . six import string_types <EOL> from salt . ext . six . moves import range <EOL> try : <EOL> import salt . ext . six . moves . winreg <EOL> import win32security <EOL> import ntsecuritycon <EOL> HAS_WINDOWS_MODULES = True <EOL> except ImportError : <EOL> HAS_WINDOWS_MODULES = False <EOL> log = logging . getLogger ( __name__ ) <EOL> __virtualname__ = '<STR_LIT>' <EOL> class daclConstants ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . FILE_ALL_ACCESS = ( ntsecuritycon . STANDARD_RIGHTS_REQUIRED | ntsecuritycon . SYNCHRONIZE | <NUM_LIT> ) <EOL> self . hkeys_security = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> self . rights = { <EOL> win32security . SE_REGISTRY_KEY : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : salt . ext . six . moves . winreg . KEY_READ , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : salt . ext . six . moves . winreg . KEY_ALL_ACCESS , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> } , <EOL> win32security . SE_FILE_OBJECT : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : ntsecuritycon . FILE_GENERIC_READ , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : ntsecuritycon . FILE_GENERIC_WRITE , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : ntsecuritycon . FILE_GENERIC_EXECUTE | <EOL> ntsecuritycon . FILE_GENERIC_READ , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : ntsecuritycon . FILE_GENERIC_WRITE | <EOL> ntsecuritycon . FILE_GENERIC_READ | <EOL> ntsecuritycon . FILE_GENERIC_EXECUTE | <EOL> ntsecuritycon . DELETE , <EOL> '<STR_LIT>' : '<STR_LIT>' } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : self . FILE_ALL_ACCESS , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> } <EOL> } <EOL> self . validAceTypes = { <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:0> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT:1> } } <EOL> self . validPropagations = { <EOL> win32security . SE_REGISTRY_KEY : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . NO_INHERITANCE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . CONTAINER_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . INHERIT_ONLY_ACE | <EOL> win32security . CONTAINER_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . NO_INHERITANCE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . CONTAINER_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . INHERIT_ONLY_ACE | <EOL> win32security . CONTAINER_INHERIT_ACE } <EOL> } , <EOL> win32security . SE_FILE_OBJECT : { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . NO_INHERITANCE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . NO_INHERITANCE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . CONTAINER_INHERIT_ACE | <EOL> win32security . OBJECT_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . CONTAINER_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . OBJECT_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . INHERIT_ONLY_ACE | <EOL> win32security . CONTAINER_INHERIT_ACE | <EOL> win32security . OBJECT_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . INHERIT_ONLY_ACE | <EOL> win32security . CONTAINER_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . INHERIT_ONLY_ACE | <EOL> win32security . OBJECT_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . NO_INHERITANCE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . NO_INHERITANCE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . CONTAINER_INHERIT_ACE | <EOL> win32security . OBJECT_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . CONTAINER_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . OBJECT_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . INHERIT_ONLY_ACE | <EOL> win32security . CONTAINER_INHERIT_ACE | <EOL> win32security . OBJECT_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . INHERIT_ONLY_ACE | <EOL> win32security . CONTAINER_INHERIT_ACE } , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : win32security . INHERIT_ONLY_ACE | <EOL> win32security . OBJECT_INHERIT_ACE } <EOL> } <EOL> } <EOL> self . reflection_mask = { <EOL> True : salt . ext . six . moves . winreg . KEY_ALL_ACCESS , <EOL> False : salt . ext . six . moves . winreg . KEY_ALL_ACCESS | salt . ext . six . moves . winreg . KEY_WOW64_64KEY , <EOL> } <EOL> self . objectType = { <EOL> '<STR_LIT>' : win32security . SE_FILE_OBJECT , <EOL> '<STR_LIT>' : win32security . SE_FILE_OBJECT , <EOL> '<STR_LIT>' : win32security . SE_REGISTRY_KEY } <EOL> def getObjectTypeBit ( self , t ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( t , string_types ) : <EOL> t = t . upper ( ) <EOL> try : <EOL> return self . objectType [ t ] <EOL> except KeyError : <EOL> raise CommandExecutionError ( ( <EOL> '<STR_LIT>' <EOL> ) . format ( t , '<STR_LIT:U+002CU+0020>' . join ( self . objectType ) ) ) <EOL> else : <EOL> return t <EOL> def getSecurityHkey ( self , s ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return self . hkeys_security [ s ] <EOL> except KeyError : <EOL> raise CommandExecutionError ( ( <EOL> '<STR_LIT>' <EOL> ) . format ( s , '<STR_LIT:U+002CU+0020>' . join ( self . hkeys_security ) ) ) <EOL> def getPermissionBit ( self , t , m ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> if isinstance ( m , string_types ) : <EOL> return self . rights [ t ] [ m ] [ '<STR_LIT>' ] <EOL> else : <EOL> return m <EOL> except KeyError : <EOL> raise CommandExecutionError ( ( <EOL> '<STR_LIT>' ) <EOL> . format ( m , '<STR_LIT:U+002CU+0020>' . join ( self . rights [ t ] ) ) ) <EOL> def getPermissionText ( self , t , m ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return self . rights [ t ] [ m ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise CommandExecutionError ( ( <EOL> '<STR_LIT>' ) <EOL> . format ( m , '<STR_LIT:U+002CU+0020>' . join ( self . rights [ t ] ) ) ) <EOL> def getAceTypeBit ( self , t ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return self . validAceTypes [ t ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise CommandExecutionError ( ( <EOL> '<STR_LIT>' <EOL> ) . format ( t , '<STR_LIT:U+002CU+0020>' . join ( self . validAceTypes ) ) ) <EOL> def getAceTypeText ( self , t ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return self . validAceTypes [ t ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise CommandExecutionError ( ( <EOL> '<STR_LIT>' <EOL> ) . format ( t , '<STR_LIT:U+002CU+0020>' . join ( self . validAceTypes ) ) ) <EOL> def getPropagationBit ( self , t , p ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return self . validPropagations [ t ] [ p ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise CommandExecutionError ( ( <EOL> '<STR_LIT>' <EOL> ) . format ( p , '<STR_LIT:U+002CU+0020>' . join ( self . validPropagations [ t ] ) ) ) <EOL> def getPropagationText ( self , t , p ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> return self . validPropagations [ t ] [ p ] [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise CommandExecutionError ( ( <EOL> '<STR_LIT>' <EOL> ) . format ( p , '<STR_LIT:U+002CU+0020>' . join ( self . validPropagations [ t ] ) ) ) <EOL> def processPath ( self , path , objectType ) : <EOL> '''<STR_LIT>''' <EOL> if objectType == win32security . SE_REGISTRY_KEY : <EOL> splt = path . split ( "<STR_LIT:\\>" ) <EOL> hive = self . getSecurityHkey ( splt . pop ( <NUM_LIT:0> ) . upper ( ) ) <EOL> splt . insert ( <NUM_LIT:0> , hive ) <EOL> path = r'<STR_LIT:\\>' . join ( splt ) <EOL> else : <EOL> path = os . path . expandvars ( path ) <EOL> return path <EOL> def _getUserSid ( user ) : <EOL> '''<STR_LIT>''' <EOL> ret = { } <EOL> try : <EOL> sid = win32security . LookupAccountName ( '<STR_LIT>' , user ) [ <NUM_LIT:0> ] if user else None <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> ret [ '<STR_LIT>' ] = sid <EOL> except Exception as e : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( <EOL> user , e ) <EOL> return ret <EOL> def __virtual__ ( ) : <EOL> '''<STR_LIT>''' <EOL> if salt . utils . is_windows ( ) and HAS_WINDOWS_MODULES : <EOL> return __virtualname__ <EOL> return ( False , "<STR_LIT>" ) <EOL> def _get_dacl ( path , objectType ) : <EOL> '''<STR_LIT>''' <EOL> try : <EOL> dacl = win32security . GetNamedSecurityInfo ( <EOL> path , objectType , win32security . DACL_SECURITY_INFORMATION <EOL> ) . GetSecurityDescriptorDacl ( ) <EOL> except Exception : <EOL> dacl = None <EOL> return dacl <EOL> def get ( path , objectType , user = None ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT>' : path , <EOL> '<STR_LIT>' : [ ] } <EOL> sidRet = _getUserSid ( user ) <EOL> if not sidRet [ '<STR_LIT:result>' ] : <EOL> return sidRet <EOL> if path and objectType : <EOL> dc = daclConstants ( ) <EOL> objectTypeBit = dc . getObjectTypeBit ( objectType ) <EOL> path = dc . processPath ( path , objectTypeBit ) <EOL> tdacl = _get_dacl ( path , objectTypeBit ) <EOL> if tdacl : <EOL> for counter in range ( <NUM_LIT:0> , tdacl . GetAceCount ( ) ) : <EOL> tAce = tdacl . GetAce ( counter ) <EOL> if not sidRet [ '<STR_LIT>' ] or ( tAce [ <NUM_LIT:2> ] == sidRet [ '<STR_LIT>' ] ) : <EOL> ret [ '<STR_LIT>' ] . append ( _ace_to_text ( tAce , objectTypeBit ) ) <EOL> return ret <EOL> def add_ace ( path , objectType , user , permission , acetype , propagation ) : <EOL> r'''<STR_LIT>''' <EOL> ret = { '<STR_LIT:result>' : None , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> if ( path and user and <EOL> permission and acetype <EOL> and propagation ) : <EOL> if objectType . upper ( ) == "<STR_LIT>" : <EOL> propagation = "<STR_LIT>" <EOL> dc = daclConstants ( ) <EOL> objectTypeBit = dc . getObjectTypeBit ( objectType ) <EOL> path = dc . processPath ( path , objectTypeBit ) <EOL> user = user . strip ( ) <EOL> permission = permission . strip ( ) . upper ( ) <EOL> acetype = acetype . strip ( ) . upper ( ) <EOL> propagation = propagation . strip ( ) . upper ( ) <EOL> sidRet = _getUserSid ( user ) <EOL> if not sidRet [ '<STR_LIT:result>' ] : <EOL> return sidRet <EOL> permissionbit = dc . getPermissionBit ( objectTypeBit , permission ) <EOL> acetypebit = dc . getAceTypeBit ( acetype ) <EOL> propagationbit = dc . getPropagationBit ( objectTypeBit , propagation ) <EOL> dacl = _get_dacl ( path , objectTypeBit ) <EOL> if dacl : <EOL> acesAdded = [ ] <EOL> try : <EOL> if acetypebit == <NUM_LIT:0> : <EOL> dacl . AddAccessAllowedAceEx ( win32security . ACL_REVISION , propagationbit , permissionbit , sidRet [ '<STR_LIT>' ] ) <EOL> elif acetypebit == <NUM_LIT:1> : <EOL> dacl . AddAccessDeniedAceEx ( win32security . ACL_REVISION , propagationbit , permissionbit , sidRet [ '<STR_LIT>' ] ) <EOL> win32security . SetNamedSecurityInfo ( <EOL> path , objectTypeBit , win32security . DACL_SECURITY_INFORMATION , <EOL> None , None , dacl , None ) <EOL> acesAdded . append ( ( <EOL> '<STR_LIT>' <EOL> ) . format ( <EOL> user , dc . getAceTypeText ( acetype ) , dc . getPermissionText ( objectTypeBit , permission ) , <EOL> dc . getPropagationText ( objectTypeBit , propagation ) ) ) <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> except Exception as e : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( e ) <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> if acesAdded : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = acesAdded <EOL> else : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( path ) <EOL> else : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> return ret <EOL> def rm_ace ( path , objectType , user , permission = None , acetype = None , propagation = None ) : <EOL> r'''<STR_LIT>''' <EOL> ret = { '<STR_LIT:result>' : None , <EOL> '<STR_LIT>' : { } , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> if path and user : <EOL> dc = daclConstants ( ) <EOL> if propagation and objectType . upper ( ) == "<STR_LIT>" : <EOL> propagation = "<STR_LIT>" <EOL> objectTypeBit = dc . getObjectTypeBit ( objectType ) <EOL> path = dc . processPath ( path , objectTypeBit ) <EOL> user = user . strip ( ) <EOL> permission = permission . strip ( ) . upper ( ) if permission else None <EOL> acetype = acetype . strip ( ) . upper ( ) if acetype else None <EOL> propagation = propagation . strip ( ) . upper ( ) if propagation else None <EOL> if check_ace ( path , objectType , user , permission , acetype , propagation , True ) [ '<STR_LIT>' ] : <EOL> sidRet = _getUserSid ( user ) <EOL> if not sidRet [ '<STR_LIT:result>' ] : <EOL> return sidRet <EOL> permissionbit = dc . getPermissionBit ( objectTypeBit , permission ) if permission else None <EOL> acetypebit = dc . getAceTypeBit ( acetype ) if acetype else None <EOL> propagationbit = dc . getPropagationBit ( objectTypeBit , propagation ) if propagation else None <EOL> dacl = _get_dacl ( path , objectTypeBit ) <EOL> counter = <NUM_LIT:0> <EOL> acesRemoved = [ ] <EOL> while counter < dacl . GetAceCount ( ) : <EOL> tAce = dacl . GetAce ( counter ) <EOL> if ( tAce [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] & win32security . INHERITED_ACE ) != win32security . INHERITED_ACE : <EOL> if tAce [ <NUM_LIT:2> ] == sidRet [ '<STR_LIT>' ] : <EOL> if not acetypebit or tAce [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == acetypebit : <EOL> if not propagationbit or ( ( tAce [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] & propagationbit ) == propagationbit ) : <EOL> if not permissionbit or tAce [ <NUM_LIT:1> ] == permissionbit : <EOL> dacl . DeleteAce ( counter ) <EOL> counter = counter - <NUM_LIT:1> <EOL> acesRemoved . append ( _ace_to_text ( tAce , objectTypeBit ) ) <EOL> counter = counter + <NUM_LIT:1> <EOL> if acesRemoved : <EOL> try : <EOL> win32security . SetNamedSecurityInfo ( <EOL> path , objectTypeBit , win32security . DACL_SECURITY_INFORMATION , <EOL> None , None , dacl , None ) <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = acesRemoved <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> except Exception as e : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( e ) <EOL> return ret <EOL> else : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret <EOL> def _ace_to_text ( ace , objectType ) : <EOL> '''<STR_LIT>''' <EOL> dc = daclConstants ( ) <EOL> objectType = dc . getObjectTypeBit ( objectType ) <EOL> try : <EOL> userSid = win32security . LookupAccountSid ( '<STR_LIT>' , ace [ <NUM_LIT:2> ] ) <EOL> if userSid [ <NUM_LIT:1> ] : <EOL> userSid = '<STR_LIT>' . format ( userSid [ <NUM_LIT:0> ] , userSid [ <NUM_LIT:1> ] ) <EOL> else : <EOL> userSid = '<STR_LIT>' . format ( userSid [ <NUM_LIT:0> ] ) <EOL> except Exception : <EOL> userSid = win32security . ConvertSidToStringSid ( ace [ <NUM_LIT:2> ] ) <EOL> tPerm = ace [ <NUM_LIT:1> ] <EOL> tAceType = ace [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> tProps = ace [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] <EOL> tInherited = '<STR_LIT>' <EOL> for x in dc . validAceTypes : <EOL> if dc . validAceTypes [ x ] [ '<STR_LIT>' ] == tAceType : <EOL> tAceType = dc . validAceTypes [ x ] [ '<STR_LIT>' ] <EOL> break <EOL> for x in dc . rights [ objectType ] : <EOL> if dc . rights [ objectType ] [ x ] [ '<STR_LIT>' ] == tPerm : <EOL> tPerm = dc . rights [ objectType ] [ x ] [ '<STR_LIT>' ] <EOL> break <EOL> if ( tProps & win32security . INHERITED_ACE ) == win32security . INHERITED_ACE : <EOL> tInherited = '<STR_LIT>' <EOL> tProps = ( tProps ^ win32security . INHERITED_ACE ) <EOL> for x in dc . validPropagations [ objectType ] : <EOL> if dc . validPropagations [ objectType ] [ x ] [ '<STR_LIT>' ] == tProps : <EOL> tProps = dc . validPropagations [ objectType ] [ x ] [ '<STR_LIT>' ] <EOL> break <EOL> return ( ( <EOL> '<STR_LIT>' <EOL> ) . format ( userSid , tAceType , tPerm , tProps , tInherited ) ) <EOL> def _set_dacl_inheritance ( path , objectType , inheritance = True , copy = True , clear = False ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:result>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { } } <EOL> if path : <EOL> try : <EOL> sd = win32security . GetNamedSecurityInfo ( path , objectType , win32security . DACL_SECURITY_INFORMATION ) <EOL> tdacl = sd . GetSecurityDescriptorDacl ( ) <EOL> if inheritance : <EOL> if clear : <EOL> counter = <NUM_LIT:0> <EOL> removedAces = [ ] <EOL> while counter < tdacl . GetAceCount ( ) : <EOL> tAce = tdacl . GetAce ( counter ) <EOL> if ( tAce [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] & win32security . INHERITED_ACE ) != win32security . INHERITED_ACE : <EOL> tdacl . DeleteAce ( counter ) <EOL> removedAces . append ( _ace_to_text ( tAce , objectType ) ) <EOL> else : <EOL> counter = counter + <NUM_LIT:1> <EOL> if removedAces : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = removedAces <EOL> else : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> win32security . SetNamedSecurityInfo ( <EOL> path , objectType , <EOL> win32security . DACL_SECURITY_INFORMATION | win32security . UNPROTECTED_DACL_SECURITY_INFORMATION , <EOL> None , None , tdacl , None ) <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> if not copy : <EOL> counter = <NUM_LIT:0> <EOL> inheritedAcesRemoved = [ ] <EOL> while counter < tdacl . GetAceCount ( ) : <EOL> tAce = tdacl . GetAce ( counter ) <EOL> if ( tAce [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] & win32security . INHERITED_ACE ) == win32security . INHERITED_ACE : <EOL> tdacl . DeleteAce ( counter ) <EOL> inheritedAcesRemoved . append ( _ace_to_text ( tAce , objectType ) ) <EOL> else : <EOL> counter = counter + <NUM_LIT:1> <EOL> if inheritedAcesRemoved : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = inheritedAcesRemoved <EOL> else : <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> win32security . SetNamedSecurityInfo ( <EOL> path , objectType , <EOL> win32security . DACL_SECURITY_INFORMATION | win32security . PROTECTED_DACL_SECURITY_INFORMATION , <EOL> None , None , tdacl , None ) <EOL> ret [ '<STR_LIT>' ] [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> except Exception as e : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( e ) <EOL> return ret <EOL> def enable_inheritance ( path , objectType , clear = False ) : <EOL> '''<STR_LIT>''' <EOL> dc = daclConstants ( ) <EOL> objectType = dc . getObjectTypeBit ( objectType ) <EOL> path = dc . processPath ( path , objectType ) <EOL> return _set_dacl_inheritance ( path , objectType , True , None , clear ) <EOL> def disable_inheritance ( path , objectType , copy = True ) : <EOL> '''<STR_LIT>''' <EOL> dc = daclConstants ( ) <EOL> objectType = dc . getObjectTypeBit ( objectType ) <EOL> path = dc . processPath ( path , objectType ) <EOL> return _set_dacl_inheritance ( path , objectType , False , copy , None ) <EOL> def check_inheritance ( path , objectType , user = None ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:result>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> sidRet = _getUserSid ( user ) <EOL> if not sidRet [ '<STR_LIT:result>' ] : <EOL> return sidRet <EOL> dc = daclConstants ( ) <EOL> objectType = dc . getObjectTypeBit ( objectType ) <EOL> path = dc . processPath ( path , objectType ) <EOL> try : <EOL> sd = win32security . GetNamedSecurityInfo ( path , objectType , win32security . DACL_SECURITY_INFORMATION ) <EOL> dacls = sd . GetSecurityDescriptorDacl ( ) <EOL> except Exception as e : <EOL> ret [ '<STR_LIT:result>' ] = False <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' . format ( e ) <EOL> return ret <EOL> for counter in range ( <NUM_LIT:0> , dacls . GetAceCount ( ) ) : <EOL> ace = dacls . GetAce ( counter ) <EOL> if ( ace [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] & win32security . INHERITED_ACE ) == win32security . INHERITED_ACE : <EOL> if not sidRet [ '<STR_LIT>' ] or ace [ <NUM_LIT:2> ] == sidRet [ '<STR_LIT>' ] : <EOL> ret [ '<STR_LIT>' ] = True <EOL> return ret <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> return ret <EOL> def check_ace ( path , objectType , user , permission = None , acetype = None , propagation = None , exactPermissionMatch = False ) : <EOL> '''<STR_LIT>''' <EOL> ret = { '<STR_LIT:result>' : False , <EOL> '<STR_LIT>' : False , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> dc = daclConstants ( ) <EOL> objectTypeBit = dc . getObjectTypeBit ( objectType ) <EOL> path = dc . processPath ( path , objectTypeBit ) <EOL> permission = permission . upper ( ) if permission else None <EOL> acetype = acetype . upper ( ) if permission else None <EOL> propagation = propagation . upper ( ) if propagation else None <EOL> permissionbit = dc . getPermissionBit ( objectTypeBit , permission ) if permission else None <EOL> acetypebit = dc . getAceTypeBit ( acetype ) if acetype else None <EOL> propagationbit = dc . getPropagationBit ( objectTypeBit , propagation ) if propagation else None <EOL> sidRet = _getUserSid ( user ) <EOL> if not sidRet [ '<STR_LIT:result>' ] : <EOL> return sidRet <EOL> dacls = _get_dacl ( path , objectTypeBit ) <EOL> ret [ '<STR_LIT:result>' ] = True <EOL> if dacls : <EOL> for counter in range ( <NUM_LIT:0> , dacls . GetAceCount ( ) ) : <EOL> ace = dacls . GetAce ( counter ) <EOL> if ace [ <NUM_LIT:2> ] == sidRet [ '<STR_LIT>' ] : <EOL> if not acetypebit or ace [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == acetypebit : <EOL> if not propagationbit or ( ace [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] & propagationbit ) == propagationbit : <EOL> if not permissionbit : <EOL> ret [ '<STR_LIT>' ] = True <EOL> return ret <EOL> if exactPermissionMatch : <EOL> if ace [ <NUM_LIT:1> ] == permissionbit : <EOL> ret [ '<STR_LIT>' ] = True <EOL> return ret <EOL> else : <EOL> if ( ace [ <NUM_LIT:1> ] & permissionbit ) == permissionbit : <EOL> ret [ '<STR_LIT>' ] = True <EOL> return ret <EOL> else : <EOL> ret [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return ret </s>
