<s> """<STR_LIT>""" <EOL> import abc <EOL> import os <EOL> from dfvfs . credentials import manager as credentials_manager <EOL> from dfvfs . lib import definitions <EOL> from dfvfs . lib import errors <EOL> from dfvfs . helpers import source_scanner <EOL> from dfvfs . helpers import windows_path_resolver <EOL> from dfvfs . path import factory as path_spec_factory <EOL> from dfvfs . resolver import resolver <EOL> from dfvfs . volume import tsk_volume_system <EOL> from dfvfs . volume import vshadow_volume_system <EOL> class VolumeScannerMediator ( object ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractmethod <EOL> def GetPartitionIdentifiers ( self , volume_system , volume_identifiers ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractmethod <EOL> def GetVSSStoreIdentifiers ( self , volume_system , volume_identifiers ) : <EOL> """<STR_LIT>""" <EOL> @ abc . abstractmethod <EOL> def UnlockEncryptedVolume ( <EOL> self , source_scanner_object , scan_context , locked_scan_node , credentials ) : <EOL> """<STR_LIT>""" <EOL> class VolumeScanner ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , mediator = None ) : <EOL> """<STR_LIT>""" <EOL> super ( VolumeScanner , self ) . __init__ ( ) <EOL> self . _mediator = mediator <EOL> self . _source_path = None <EOL> self . _source_scanner = source_scanner . SourceScanner ( ) <EOL> self . _source_type = None <EOL> self . _vss_stores = None <EOL> def _GetTSKPartitionIdentifiers ( self , scan_node ) : <EOL> """<STR_LIT>""" <EOL> if not scan_node or not scan_node . path_spec : <EOL> raise errors . ScannerError ( u'<STR_LIT>' ) <EOL> volume_system = tsk_volume_system . TSKVolumeSystem ( ) <EOL> volume_system . Open ( scan_node . path_spec ) <EOL> volume_identifiers = self . _source_scanner . GetVolumeIdentifiers ( <EOL> volume_system ) <EOL> if not volume_identifiers : <EOL> raise errors . ScannerError ( u'<STR_LIT>' ) <EOL> if not self . _mediator or len ( volume_identifiers ) == <NUM_LIT:1> : <EOL> return volume_identifiers <EOL> try : <EOL> return self . _mediator . GetPartitionIdentifiers ( <EOL> volume_system , volume_identifiers ) <EOL> except KeyboardInterrupt : <EOL> raise errors . ScannerError ( u'<STR_LIT>' ) <EOL> def _GetVSSStoreIdentifiers ( self , scan_node ) : <EOL> """<STR_LIT>""" <EOL> if not scan_node or not scan_node . path_spec : <EOL> raise errors . ScannerError ( u'<STR_LIT>' ) <EOL> volume_system = vshadow_volume_system . VShadowVolumeSystem ( ) <EOL> volume_system . Open ( scan_node . path_spec ) <EOL> volume_identifiers = self . _source_scanner . GetVolumeIdentifiers ( <EOL> volume_system ) <EOL> if not self . _mediator and not volume_identifiers : <EOL> return [ ] <EOL> try : <EOL> return self . _mediator . GetVSSStoreIdentifiers ( <EOL> volume_system , volume_identifiers ) <EOL> except KeyboardInterrupt : <EOL> raise errors . UserAbort ( u'<STR_LIT>' ) <EOL> def _ScanFileSystem ( self , file_system_scan_node , base_path_specs ) : <EOL> """<STR_LIT>""" <EOL> if not file_system_scan_node or not file_system_scan_node . path_spec : <EOL> raise errors . ScannerError ( u'<STR_LIT>' ) <EOL> base_path_specs . append ( file_system_scan_node . path_spec ) <EOL> def _ScanVolume ( self , scan_context , volume_scan_node , base_path_specs ) : <EOL> """<STR_LIT>""" <EOL> if not volume_scan_node or not volume_scan_node . path_spec : <EOL> raise errors . ScannerError ( u'<STR_LIT>' ) <EOL> if len ( volume_scan_node . sub_nodes ) == <NUM_LIT:0> : <EOL> self . _ScanVolumeScanNode ( scan_context , volume_scan_node , base_path_specs ) <EOL> else : <EOL> for sub_scan_node in volume_scan_node . sub_nodes : <EOL> self . _ScanVolumeScanNode ( scan_context , sub_scan_node , base_path_specs ) <EOL> def _ScanVolumeScanNode ( <EOL> self , scan_context , volume_scan_node , base_path_specs ) : <EOL> """<STR_LIT>""" <EOL> if not volume_scan_node or not volume_scan_node . path_spec : <EOL> raise errors . ScannerError ( u'<STR_LIT>' ) <EOL> scan_node = volume_scan_node <EOL> while len ( scan_node . sub_nodes ) == <NUM_LIT:1> : <EOL> if scan_node . type_indicator == definitions . TYPE_INDICATOR_VSHADOW : <EOL> location = getattr ( scan_node . path_spec , u'<STR_LIT:location>' , None ) <EOL> if location == u'<STR_LIT:/>' : <EOL> break <EOL> scan_node = scan_node . sub_nodes [ <NUM_LIT:0> ] <EOL> if scan_node . type_indicator in definitions . ENCRYPTED_VOLUME_TYPE_INDICATORS : <EOL> self . _ScanVolumeScanNodeEncrypted ( <EOL> scan_context , scan_node , base_path_specs ) <EOL> elif scan_node . type_indicator == definitions . TYPE_INDICATOR_VSHADOW : <EOL> self . _ScanVolumeScanNodeVSS ( scan_node , base_path_specs ) <EOL> elif scan_node . type_indicator in definitions . FILE_SYSTEM_TYPE_INDICATORS : <EOL> self . _ScanFileSystem ( scan_node , base_path_specs ) <EOL> def _ScanVolumeScanNodeEncrypted ( <EOL> self , scan_context , volume_scan_node , base_path_specs ) : <EOL> """<STR_LIT>""" <EOL> result = not scan_context . IsLockedScanNode ( volume_scan_node . path_spec ) <EOL> if not result : <EOL> credentials = credentials_manager . CredentialsManager . GetCredentials ( <EOL> volume_scan_node . path_spec ) <EOL> result = self . _mediator . GetEncryptedVolumeCredential ( <EOL> self . _source_scanner , scan_context , volume_scan_node , credentials ) <EOL> if result : <EOL> self . _source_scanner . Scan ( <EOL> scan_context , scan_path_spec = volume_scan_node . path_spec ) <EOL> self . _ScanVolume ( scan_context , volume_scan_node , base_path_specs ) <EOL> def _ScanVolumeScanNodeVSS ( self , volume_scan_node , base_path_specs ) : <EOL> """<STR_LIT>""" <EOL> location = getattr ( volume_scan_node . path_spec , u'<STR_LIT:location>' , None ) <EOL> if location != u'<STR_LIT:/>' : <EOL> return <EOL> vss_store_identifiers = self . _GetVSSStoreIdentifiers ( volume_scan_node ) <EOL> self . _vss_stores = list ( vss_store_identifiers ) <EOL> vss_store_identifiers . reverse ( ) <EOL> for vss_store_identifier in vss_store_identifiers : <EOL> location = u'<STR_LIT>' . format ( vss_store_identifier ) <EOL> sub_scan_node = volume_scan_node . GetSubNodeByLocation ( location ) <EOL> if not sub_scan_node : <EOL> raise errors . ScannerError ( <EOL> u'<STR_LIT>' . format ( <EOL> vss_store_identifier ) ) <EOL> path_spec = path_spec_factory . Factory . NewPathSpec ( <EOL> definitions . TYPE_INDICATOR_TSK , location = u'<STR_LIT:/>' , <EOL> parent = sub_scan_node . path_spec ) <EOL> base_path_specs . append ( path_spec ) <EOL> def GetBasePathSpecs ( self , source_path ) : <EOL> """<STR_LIT>""" <EOL> if ( not source_path . startswith ( u'<STR_LIT>' ) and <EOL> not os . path . exists ( source_path ) ) : <EOL> raise errors . ScannerError ( <EOL> u'<STR_LIT>' . format ( source_path ) ) <EOL> scan_context = source_scanner . SourceScannerContext ( ) <EOL> scan_context . OpenSourcePath ( source_path ) <EOL> try : <EOL> self . _source_scanner . Scan ( scan_context ) <EOL> except ( errors . BackEndError , ValueError ) as exception : <EOL> raise errors . ScannerError ( <EOL> u'<STR_LIT>' . format ( exception ) ) <EOL> self . _source_path = source_path <EOL> self . _source_type = scan_context . source_type <EOL> if self . _source_type not in [ <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_DEVICE , <EOL> definitions . SOURCE_TYPE_STORAGE_MEDIA_IMAGE ] : <EOL> scan_node = scan_context . GetRootScanNode ( ) <EOL> return [ scan_node . path_spec ] <EOL> scan_node = scan_context . GetRootScanNode ( ) <EOL> while len ( scan_node . sub_nodes ) == <NUM_LIT:1> : <EOL> scan_node = scan_node . sub_nodes [ <NUM_LIT:0> ] <EOL> if scan_node . type_indicator != definitions . TYPE_INDICATOR_TSK_PARTITION : <EOL> partition_identifiers = None <EOL> else : <EOL> partition_identifiers = self . _GetTSKPartitionIdentifiers ( scan_node ) <EOL> base_path_specs = [ ] <EOL> if not partition_identifiers : <EOL> self . _ScanVolume ( scan_context , scan_node , base_path_specs ) <EOL> else : <EOL> for partition_identifier in partition_identifiers : <EOL> location = u'<STR_LIT>' . format ( partition_identifier ) <EOL> sub_scan_node = scan_node . GetSubNodeByLocation ( location ) <EOL> self . _ScanVolume ( scan_context , sub_scan_node , base_path_specs ) <EOL> return base_path_specs <EOL> class WindowsVolumeScanner ( VolumeScanner ) : <EOL> """<STR_LIT>""" <EOL> _WINDOWS_DIRECTORIES = frozenset ( [ <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> u'<STR_LIT>' , <EOL> ] ) <EOL> def __init__ ( self , mediator = None ) : <EOL> """<STR_LIT>""" <EOL> super ( WindowsVolumeScanner , self ) . __init__ ( mediator = mediator ) <EOL> self . _file_system = None <EOL> self . _path_resolver = None <EOL> self . _windows_directory = None <EOL> def _ScanFileSystem ( self , file_system_scan_node , base_path_specs ) : <EOL> """<STR_LIT>""" <EOL> if not file_system_scan_node or not file_system_scan_node . path_spec : <EOL> raise errors . ScannerError ( u'<STR_LIT>' ) <EOL> file_system = resolver . Resolver . OpenFileSystem ( <EOL> file_system_scan_node . path_spec ) <EOL> if not file_system : <EOL> return <EOL> try : <EOL> path_resolver = windows_path_resolver . WindowsPathResolver ( <EOL> file_system , file_system_scan_node . path_spec . parent ) <EOL> if self . _ScanFileSystemForWindowsDirectory ( path_resolver ) : <EOL> base_path_specs . append ( file_system_scan_node . path_spec ) <EOL> finally : <EOL> file_system . Close ( ) <EOL> def _ScanFileSystemForWindowsDirectory ( self , path_resolver ) : <EOL> """<STR_LIT>""" <EOL> result = False <EOL> for windows_path in self . _WINDOWS_DIRECTORIES : <EOL> windows_path_spec = path_resolver . ResolvePath ( windows_path ) <EOL> result = windows_path_spec is not None <EOL> if result : <EOL> self . _windows_directory = windows_path <EOL> break <EOL> return result <EOL> def OpenFile ( self , windows_path ) : <EOL> """<STR_LIT>""" <EOL> path_spec = self . _path_resolver . ResolvePath ( windows_path ) <EOL> if path_spec is None : <EOL> return <EOL> return self . _file_system . GetFileObjectByPathSpec ( path_spec ) <EOL> def ScanForWindowsVolume ( self , source_path ) : <EOL> """<STR_LIT>""" <EOL> windows_path_specs = self . GetBasePathSpecs ( source_path ) <EOL> if ( not windows_path_specs or <EOL> self . _source_type == definitions . SOURCE_TYPE_FILE ) : <EOL> return False <EOL> file_system_path_spec = windows_path_specs [ <NUM_LIT:0> ] <EOL> self . _file_system = resolver . Resolver . OpenFileSystem ( file_system_path_spec ) <EOL> if file_system_path_spec . type_indicator == definitions . TYPE_INDICATOR_OS : <EOL> mount_point = file_system_path_spec <EOL> else : <EOL> mount_point = file_system_path_spec . parent <EOL> self . _path_resolver = windows_path_resolver . WindowsPathResolver ( <EOL> self . _file_system , mount_point ) <EOL> if not self . _windows_directory : <EOL> self . _ScanFileSystemForWindowsDirectory ( self . _path_resolver ) <EOL> if not self . _windows_directory : <EOL> return False <EOL> self . _path_resolver . SetEnvironmentVariable ( <EOL> u'<STR_LIT>' , self . _windows_directory ) <EOL> self . _path_resolver . SetEnvironmentVariable ( <EOL> u'<STR_LIT>' , self . _windows_directory ) <EOL> return True </s>
