changes to handleDeviceChange
authormb
Wed, 11 Dec 2013 12:07:16 +0100
changeset 40b44a603b0b95
parent 39 77509ad4f2e9
child 41 76d9177ca509
child 42 e10a08095ccc
child 57 2144ea8c7fc6
changes to handleDeviceChange
mapNetworkDrive intead of netUse nad unmapNetworkDrive intead of netUnUse
OpenSecurity/bin/opensecurityd.py
OpenSecurity/bin/vmmanager.py
     1.1 --- a/OpenSecurity/bin/opensecurityd.py	Wed Dec 11 10:39:14 2013 +0000
     1.2 +++ b/OpenSecurity/bin/opensecurityd.py	Wed Dec 11 12:07:16 2013 +0100
     1.3 @@ -81,7 +81,9 @@
     1.4      """OpenSecurity '/device_change' handler"""
     1.5      
     1.6      def GET(self):
     1.7 -        gvm_mgr.handleDeviceChange()
     1.8 +        new_ip = gvm_mgr.handleDeviceChange()
     1.9 +        if new_ip != None:
    1.10 +            gvm_mgr.mapNetworkDrive('h:', '\\\\' + new_ip + '\\USB', None, None)
    1.11          return "os_device_change"
    1.12  
    1.13          
     2.1 --- a/OpenSecurity/bin/vmmanager.py	Wed Dec 11 10:39:14 2013 +0000
     2.2 +++ b/OpenSecurity/bin/vmmanager.py	Wed Dec 11 12:07:16 2013 +0100
     2.3 @@ -12,9 +12,11 @@
     2.4  import _winreg
     2.5  from cygwin import Cygwin
     2.6  import threading
     2.7 +import time
     2.8 +import string
     2.9  
    2.10  
    2.11 -DEBUG = True
    2.12 +DEBUG = False
    2.13  
    2.14  class USBFilter:
    2.15      vendorid = ""
    2.16 @@ -77,7 +79,7 @@
    2.17      def execute(self, cmd):
    2.18          if DEBUG:
    2.19              print('trying to launch: ' + cmd)
    2.20 -        process = Popen(cmd, stdout=PIPE, stderr=PIPE)
    2.21 +        process = Popen(cmd, stdout=PIPE, stderr=PIPE) #shell = True
    2.22          if DEBUG:
    2.23              print('launched: ' + cmd)
    2.24          result = process.wait()
    2.25 @@ -222,8 +224,8 @@
    2.26      # start VM
    2.27      def startVM(self, vm_name):
    2.28          print('starting ' +  vm_name)
    2.29 -        cmd = 'VBoxManage startvm ' + vm_name + ' --type headless'
    2.30 -        print self.execute(cmd)
    2.31 +        cmd = 'VBoxManage startvm ' + vm_name + ' --type headless' 
    2.32 +        print self.execute(cmd) #verify against (0, 'Waiting for VM "SecurityDVM0" to power on...\r\nVM "SecurityDVM0" has been successfully started.\r\n', '')
    2.33          
    2.34      # stop VM    
    2.35      def stopVM(self, vm_name):
    2.36 @@ -239,6 +241,8 @@
    2.37          if result=='':
    2.38              return None
    2.39          result = result[1]
    2.40 +        if result.startswith('No value set!'):
    2.41 +            return None
    2.42          return result[result.index(':')+1:].strip()
    2.43      
    2.44      # attach removable storage device to VM by provision of filter
    2.45 @@ -300,11 +304,12 @@
    2.46      def handleDeviceChange(self):
    2.47          if VMManager.handleDeviceChangeLock.acquire(True):
    2.48              #destroy unused vms
    2.49 +            new_ip = None
    2.50              attached_devices = self.getAttachedRSDs()
    2.51              connected_devices = self.listRSDS()
    2.52              for vm_name in attached_devices.keys():
    2.53                  if attached_devices[vm_name] not in connected_devices.values():
    2.54 -                    # self.netUnUse(vm_name)
    2.55 +                    self.unmapNetworkDrive('h:')
    2.56                      self.stopVM(vm_name)
    2.57                      self.removeVM(vm_name)
    2.58              #create new vm for attached device if any
    2.59 @@ -314,10 +319,19 @@
    2.60                      new_sdvm = self.generateSDVMName()
    2.61                      self.createVM(new_sdvm)
    2.62                      self.attachRSD(new_sdvm, connected_device)
    2.63 +                    #sleep like method
    2.64 +                    self.listSDVM()
    2.65                      self.startVM(new_sdvm)
    2.66 -                    
    2.67 -                    #self.netUse(new_sdvm)
    2.68 +                   
    2.69 +                    while new_ip == None:
    2.70 +                        time.sleep(1)
    2.71 +                        new_ip = self.getHostOnlyIP(new_sdvm)
    2.72 +                    while new_ip not in self.startNotifications:
    2.73 +                        time.sleep(1)
    2.74 +                    #TODO: cleanup notifications somwhere else (eg. machine shutdown)
    2.75 +                    self.startNotifications.remove(new_ip)
    2.76              VMManager.handleDeviceChangeLock.release()
    2.77 +            return new_ip
    2.78      
    2.79      def handleBrowsingRequest(self):
    2.80          new_sdvm = self.generateSDVMName()
    2.81 @@ -344,16 +358,66 @@
    2.82          # run command
    2.83          cmd = self.cygwin_path+'bash.exe --login -c \"DISPLAY=:0 ssh -Y -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\"  '  + user_name + '@' + address + ' ' + prog + '\"'
    2.84          return self.execute(cmd)    
    2.85 -        
    2.86 -    # executes NET USE and connects to samba share on guestos 
    2.87 -    def netUse(self, vm_name):
    2.88 -        ip = self.getHostOnlyIP(vm_name)
    2.89 -        cmd = 'net use H: \\' + ip + '\USB'
    2.90 -        return self.execute(cmd)
    2.91 -        
    2.92      
    2.93 -#if __name__ == '__main__':
    2.94 -    #man = VMManager()
    2.95 +    #Small function to check the availability of network resource.
    2.96 +    def isAvailable(self, path):
    2.97 +        cmd = 'IF EXIST ' + path + ' echo YES'
    2.98 +        result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
    2.99 +        return string.find(str(result), 'YES',)
   2.100 +    
   2.101 +    #Small function to check if the mention location is a directory
   2.102 +    def isDirectory(self, path):
   2.103 +        cmd = 'dir ' + path + ' | FIND ".."'
   2.104 +        result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
   2.105 +        return string.find(str(result), 'DIR',)
   2.106 +
   2.107 +    def mapNetworkDrive(self, drive, networkPath, user, password):
   2.108 +        self.unmapNetworkDrive('h:')
   2.109 +        #Check for drive availability
   2.110 +        if self.isAvailable(drive) > -1:
   2.111 +            print "Drive letter is already in use: ", drive
   2.112 +            return -1
   2.113 +        #Check for network resource availability
   2.114 +        while self.isAvailable(networkPath) == -1:
   2.115 +            time.sleep(1)
   2.116 +            print "Path not accessible: ", networkPath, " retrying"
   2.117 +            #return -1
   2.118 +    
   2.119 +        #Prepare 'NET USE' commands
   2.120 +        cmd = 'NET USE ' + drive + ' ' + networkPath
   2.121 +        if user != None:
   2.122 +            cmd = cmd + ' ' + password + ' /User' + user
   2.123 +    
   2.124 +        print "cmd = ", cmd
   2.125 +        #Execute 'NET USE' command with authentication
   2.126 +        result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
   2.127 +        print "Executed: ", cmd
   2.128 +        if string.find(str(result), 'successfully',) == -1:
   2.129 +            print cmd, " FAILED"
   2.130 +            return -1
   2.131 +        #Mapped with first try
   2.132 +        return 1
   2.133 +    
   2.134 +    def unmapNetworkDrive(self, drive):
   2.135 +        #Check if the drive is in use
   2.136 +        if self.isAvailable(drive) == -1:
   2.137 +            #Drive is not in use
   2.138 +            return -1
   2.139 +        #Prepare 'NET USE' command
   2.140 +        cmd = 'net use ' + drive + ' /DELETE'
   2.141 +        result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
   2.142 +        if string.find(str(result), 'successfully',) == -1:
   2.143 +            return -1
   2.144 +        return 1
   2.145 +
   2.146 +if __name__ == '__main__':
   2.147 +
   2.148 +    man = VMManager.getInstance()
   2.149 +    #man.removeVM('SecurityDVM0')
   2.150 +    #man.netUse('192.168.56.134', 'USB\\')
   2.151 +    ip = '192.168.56.139'
   2.152 +    man.mapNetworkDrive('h:', '\\\\' + ip + '\USB', None, None)
   2.153 +    
   2.154      #man.cygwin_path = 'c:\\cygwin64\\bin\\'
   2.155      #man.handleDeviceChange()
   2.156      #print man.listSDVM()