OpenSecurity/bin/vmmanager.py
changeset 40 b44a603b0b95
parent 36 cc6abcf9108b
child 41 76d9177ca509
child 42 e10a08095ccc
     1.1 --- a/OpenSecurity/bin/vmmanager.py	Tue Dec 10 16:02:24 2013 +0100
     1.2 +++ b/OpenSecurity/bin/vmmanager.py	Wed Dec 11 12:07:16 2013 +0100
     1.3 @@ -12,9 +12,11 @@
     1.4  import _winreg
     1.5  from cygwin import Cygwin
     1.6  import threading
     1.7 +import time
     1.8 +import string
     1.9  
    1.10  
    1.11 -DEBUG = True
    1.12 +DEBUG = False
    1.13  
    1.14  class USBFilter:
    1.15      vendorid = ""
    1.16 @@ -77,7 +79,7 @@
    1.17      def execute(self, cmd):
    1.18          if DEBUG:
    1.19              print('trying to launch: ' + cmd)
    1.20 -        process = Popen(cmd, stdout=PIPE, stderr=PIPE)
    1.21 +        process = Popen(cmd, stdout=PIPE, stderr=PIPE) #shell = True
    1.22          if DEBUG:
    1.23              print('launched: ' + cmd)
    1.24          result = process.wait()
    1.25 @@ -222,8 +224,8 @@
    1.26      # start VM
    1.27      def startVM(self, vm_name):
    1.28          print('starting ' +  vm_name)
    1.29 -        cmd = 'VBoxManage startvm ' + vm_name + ' --type headless'
    1.30 -        print self.execute(cmd)
    1.31 +        cmd = 'VBoxManage startvm ' + vm_name + ' --type headless' 
    1.32 +        print self.execute(cmd) #verify against (0, 'Waiting for VM "SecurityDVM0" to power on...\r\nVM "SecurityDVM0" has been successfully started.\r\n', '')
    1.33          
    1.34      # stop VM    
    1.35      def stopVM(self, vm_name):
    1.36 @@ -239,6 +241,8 @@
    1.37          if result=='':
    1.38              return None
    1.39          result = result[1]
    1.40 +        if result.startswith('No value set!'):
    1.41 +            return None
    1.42          return result[result.index(':')+1:].strip()
    1.43      
    1.44      # attach removable storage device to VM by provision of filter
    1.45 @@ -300,11 +304,12 @@
    1.46      def handleDeviceChange(self):
    1.47          if VMManager.handleDeviceChangeLock.acquire(True):
    1.48              #destroy unused vms
    1.49 +            new_ip = None
    1.50              attached_devices = self.getAttachedRSDs()
    1.51              connected_devices = self.listRSDS()
    1.52              for vm_name in attached_devices.keys():
    1.53                  if attached_devices[vm_name] not in connected_devices.values():
    1.54 -                    # self.netUnUse(vm_name)
    1.55 +                    self.unmapNetworkDrive('h:')
    1.56                      self.stopVM(vm_name)
    1.57                      self.removeVM(vm_name)
    1.58              #create new vm for attached device if any
    1.59 @@ -314,10 +319,19 @@
    1.60                      new_sdvm = self.generateSDVMName()
    1.61                      self.createVM(new_sdvm)
    1.62                      self.attachRSD(new_sdvm, connected_device)
    1.63 +                    #sleep like method
    1.64 +                    self.listSDVM()
    1.65                      self.startVM(new_sdvm)
    1.66 -                    
    1.67 -                    #self.netUse(new_sdvm)
    1.68 +                   
    1.69 +                    while new_ip == None:
    1.70 +                        time.sleep(1)
    1.71 +                        new_ip = self.getHostOnlyIP(new_sdvm)
    1.72 +                    while new_ip not in self.startNotifications:
    1.73 +                        time.sleep(1)
    1.74 +                    #TODO: cleanup notifications somwhere else (eg. machine shutdown)
    1.75 +                    self.startNotifications.remove(new_ip)
    1.76              VMManager.handleDeviceChangeLock.release()
    1.77 +            return new_ip
    1.78      
    1.79      def handleBrowsingRequest(self):
    1.80          new_sdvm = self.generateSDVMName()
    1.81 @@ -344,16 +358,66 @@
    1.82          # run command
    1.83          cmd = self.cygwin_path+'bash.exe --login -c \"DISPLAY=:0 ssh -Y -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\"  '  + user_name + '@' + address + ' ' + prog + '\"'
    1.84          return self.execute(cmd)    
    1.85 -        
    1.86 -    # executes NET USE and connects to samba share on guestos 
    1.87 -    def netUse(self, vm_name):
    1.88 -        ip = self.getHostOnlyIP(vm_name)
    1.89 -        cmd = 'net use H: \\' + ip + '\USB'
    1.90 -        return self.execute(cmd)
    1.91 -        
    1.92      
    1.93 -#if __name__ == '__main__':
    1.94 -    #man = VMManager()
    1.95 +    #Small function to check the availability of network resource.
    1.96 +    def isAvailable(self, path):
    1.97 +        cmd = 'IF EXIST ' + path + ' echo YES'
    1.98 +        result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
    1.99 +        return string.find(str(result), 'YES',)
   1.100 +    
   1.101 +    #Small function to check if the mention location is a directory
   1.102 +    def isDirectory(self, path):
   1.103 +        cmd = 'dir ' + path + ' | FIND ".."'
   1.104 +        result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
   1.105 +        return string.find(str(result), 'DIR',)
   1.106 +
   1.107 +    def mapNetworkDrive(self, drive, networkPath, user, password):
   1.108 +        self.unmapNetworkDrive('h:')
   1.109 +        #Check for drive availability
   1.110 +        if self.isAvailable(drive) > -1:
   1.111 +            print "Drive letter is already in use: ", drive
   1.112 +            return -1
   1.113 +        #Check for network resource availability
   1.114 +        while self.isAvailable(networkPath) == -1:
   1.115 +            time.sleep(1)
   1.116 +            print "Path not accessible: ", networkPath, " retrying"
   1.117 +            #return -1
   1.118 +    
   1.119 +        #Prepare 'NET USE' commands
   1.120 +        cmd = 'NET USE ' + drive + ' ' + networkPath
   1.121 +        if user != None:
   1.122 +            cmd = cmd + ' ' + password + ' /User' + user
   1.123 +    
   1.124 +        print "cmd = ", cmd
   1.125 +        #Execute 'NET USE' command with authentication
   1.126 +        result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
   1.127 +        print "Executed: ", cmd
   1.128 +        if string.find(str(result), 'successfully',) == -1:
   1.129 +            print cmd, " FAILED"
   1.130 +            return -1
   1.131 +        #Mapped with first try
   1.132 +        return 1
   1.133 +    
   1.134 +    def unmapNetworkDrive(self, drive):
   1.135 +        #Check if the drive is in use
   1.136 +        if self.isAvailable(drive) == -1:
   1.137 +            #Drive is not in use
   1.138 +            return -1
   1.139 +        #Prepare 'NET USE' command
   1.140 +        cmd = 'net use ' + drive + ' /DELETE'
   1.141 +        result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate()
   1.142 +        if string.find(str(result), 'successfully',) == -1:
   1.143 +            return -1
   1.144 +        return 1
   1.145 +
   1.146 +if __name__ == '__main__':
   1.147 +
   1.148 +    man = VMManager.getInstance()
   1.149 +    #man.removeVM('SecurityDVM0')
   1.150 +    #man.netUse('192.168.56.134', 'USB\\')
   1.151 +    ip = '192.168.56.139'
   1.152 +    man.mapNetworkDrive('h:', '\\\\' + ip + '\USB', None, None)
   1.153 +    
   1.154      #man.cygwin_path = 'c:\\cygwin64\\bin\\'
   1.155      #man.handleDeviceChange()
   1.156      #print man.listSDVM()