unmount thread, stricthostkeychecking disable
authorBarthaM@N3SIM1218.D03.arc.local
Tue, 29 Apr 2014 15:40:48 +0100
changeset 135c9499f5166c7
parent 134 f1c1c06c947d
child 136 ac117cd7bab1
unmount thread, stricthostkeychecking disable
OpenSecurity/bin/cygwin.py
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/cygwin.py	Tue Apr 29 13:00:46 2014 +0200
     1.2 +++ b/OpenSecurity/bin/cygwin.py	Tue Apr 29 15:40:48 2014 +0100
     1.3 @@ -199,14 +199,14 @@
     1.4          return Cygwin.execute(Cygwin.cygwin_bash, command, wait_return, window, stdin = stdin, stdout = stdout, stderr = stderr)
     1.5      
     1.6      @staticmethod
     1.7 -    def cmdExecute(command, wait_return=True, window = False, bash_opts=''):
     1.8 +    def cmdExecute(command, wait_return=True, window = False):
     1.9          command = ' /c ' + command 
    1.10          return Cygwin.execute(Cygwin.win_cmd, command, wait_return, window)
    1.11  
    1.12      # executes command over ssh on guest vm
    1.13      @staticmethod
    1.14      def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
    1.15 -        command = ' -v -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
    1.16 +        command = ' -v -o StrictHostKeyChecking=no -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
    1.17          return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)     
    1.18      
    1.19      #machineFolder + '/' + vm_name + '/dvm_key
    1.20 @@ -218,7 +218,7 @@
    1.21      @staticmethod
    1.22      def sshExecuteX11(command, address, user_name, certificate, wait_return=True):
    1.23          #return call('"'+ Cygwin.cygwin_bash +'" -l -c "' + 'DISPLAY=:0.0 ssh -Y -i \\\"' + certificate +'\\\" ' + user_name + '@' + address + ' ' + command + '"', shell=True)
    1.24 -        return Cygwin.bashExecute('DISPLAY=:0.0 ssh -Y -i \\\"' + certificate +'\\\" ' + user_name + '@' + address + ' ' + command + '')
    1.25 +        return Cygwin.bashExecute('DISPLAY=:0.0 ssh -Y -o StrictHostKeyChecking=no -i \\\"' + certificate +'\\\" ' + user_name + '@' + address + ' ' + command + '')
    1.26  
    1.27      @staticmethod
    1.28      def is_X11_running():
    1.29 @@ -279,4 +279,4 @@
    1.30      #print('C:\\Program Files\\OpenSecurity: ' + c.cygPath('C:\\Program Files\\OpenSecurity'))
    1.31      
    1.32      sys.exit(0)
    1.33 -    
    1.34 +    
     2.1 --- a/OpenSecurity/bin/vmmanager.pyw	Tue Apr 29 13:00:46 2014 +0200
     2.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Tue Apr 29 15:40:48 2014 +0100
     2.3 @@ -54,7 +54,6 @@
     2.4      #    return self.coords[item]
     2.5   
     2.6  class VMManager(object):
     2.7 -
     2.8      vmRootName = "SecurityDVM"
     2.9      systemProperties = None
    2.10      _instance = None
    2.11 @@ -121,7 +120,8 @@
    2.12              self.rsdHandler.join()
    2.13          drives = self.getNetworkDrives()
    2.14          for drive in drives.keys():
    2.15 -            self.unmapNetworkDrive(drive)
    2.16 +            driveHandler = UnmapDriveHandler(self, drive)
    2.17 +            driveHandler.start()
    2.18          for vm in self.listSDVM():
    2.19              self.poweroffVM(vm)
    2.20              self.removeVM(vm)
    2.21 @@ -458,19 +458,13 @@
    2.22          handler.start()
    2.23          return 'ok'
    2.24      
    2.25 -    #Small function to check the availability of network resource.
    2.26 -    #def isAvailable(self, path):
    2.27 -        #return os.path.exists(path)
    2.28 -        #result = Cygwin.cmdExecute('IF EXIST "' + path + '" echo YES')
    2.29 -        #return string.find(result[1], 'YES',)
    2.30 -    
    2.31 -    #Small function to check if the mention location is a directory
    2.32 +    #Small function to check if the mentioned location is a directory
    2.33      def isDirectory(self, path):
    2.34          result = checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."'))
    2.35          return string.find(result[1], 'DIR',)
    2.36  
    2.37      def mapNetworkDrive(self, drive, networkPath, user, password):
    2.38 -        self.unmapNetworkDrive(drive)
    2.39 +        #self.unmapNetworkDrive(drive)
    2.40          #Check for drive availability
    2.41          if os.path.exists(drive):
    2.42              logger.error("Drive letter is already in use: " + drive)
    2.43 @@ -483,24 +477,25 @@
    2.44                  return -1
    2.45              logger.info("Path not accessible: " + networkPath + " retrying")
    2.46              retry-=1
    2.47 -            #return -1
    2.48      
    2.49          command = 'USE ' + drive + ' ' + networkPath + ' /PERSISTENT:NO'
    2.50          if user != None:
    2.51              command += ' ' + password + ' /User' + user
    2.52      
    2.53 -        #TODO: Execute 'NET USE' command with authentication
    2.54 -        result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command))
    2.55 +        result = checkResult(Cygwin.execute('C:\\Windows\\system32\\NET ', command))
    2.56 +        #result = checkResult(Cygwin.cmdExecute('NET ' + command))
    2.57          if string.find(result[1], 'successfully',) == -1:
    2.58              logger.error("Failed: NET " + command)
    2.59              return -1
    2.60          return 1
    2.61      
    2.62      def unmapNetworkDrive(self, drive):
    2.63 -        drives = self.getNetworkDrives()
    2.64 -        if drive not in drives.keys():
    2.65 -            return 1 
    2.66 -        result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE ' + drive + ' /DELETE /YES'))
    2.67 +        #drives = self.getNetworkDrives()
    2.68 +        #if drive not in drives.keys():
    2.69 +        #    return 1 
    2.70 +        command = 'USE ' + drive + ' /DELETE /YES' #' ' + networkPath +
    2.71 +        result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command)) 
    2.72 +        #result = checkResult(Cygwin.cmdExecute('NET ' + command)) 
    2.73          if string.find(str(result[1]), 'successfully',) == -1:
    2.74              logger.error(result[2])
    2.75              return -1
    2.76 @@ -531,7 +526,7 @@
    2.77          for line in result[1].splitlines():
    2.78              if line != None and ip in line:
    2.79                  parts = line.split()
    2.80 -                return parts[0]
    2.81 +                return parts[1]
    2.82      @staticmethod
    2.83      def getLogicalDrives():
    2.84          drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
    2.85 @@ -565,12 +560,30 @@
    2.86  
    2.87  def checkResult(result):
    2.88      if result[0] != 0:
    2.89 -        logger.error('Command failed:\n' + result[2])
    2.90 +        logger.error('Command failed:' + ''.join(result[2]))
    2.91          raise OpenSecurityException('Command failed:' + ''.join(result[2]))
    2.92      return result
    2.93  
    2.94 -
    2.95 -            
    2.96 +# handles browsing request                    
    2.97 +class UnmapDriveHandler(threading.Thread): 
    2.98 +    vmm = None
    2.99 +    drive = None
   2.100 +    running = True
   2.101 +    #Cygwin.start_X11()
   2.102 +    def __init__(self, vmmanager, drv):
   2.103 +        threading.Thread.__init__(self)
   2.104 +        self.vmm = vmmanager            
   2.105 +        self.drive = drv
   2.106 +        
   2.107 +    def run(self):
   2.108 +        while self.running:
   2.109 +             self.vmm.unmapNetworkDrive(self.drive)
   2.110 +             mappedDrives = self.vmm.getNetworkDrives()
   2.111 +             #logger.info(mappedDrives)
   2.112 +             #logger.info(self.drive)
   2.113 +             if self.drive not in mappedDrives.keys():
   2.114 +                 self.running = False
   2.115 +    
   2.116  # handles browsing request                    
   2.117  class BrowsingHandler(threading.Thread):   
   2.118      vmm = None
   2.119 @@ -581,6 +594,7 @@
   2.120       
   2.121      def run(self):
   2.122          drive = None
   2.123 +        networkPath = None
   2.124          try:
   2.125              new_sdvm = self.vmm.generateSDVMName()
   2.126              self.vmm.createVM(new_sdvm)
   2.127 @@ -591,7 +605,8 @@
   2.128              new_ip = self.vmm.waitStartup(new_sdvm)
   2.129              drive = self.vmm.genNetworkDrive()
   2.130              if new_ip != None:
   2.131 -                self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\Download', None, None)
   2.132 +                networkPath = '\\\\' + new_ip + '\\Download'
   2.133 +                self.vmm.mapNetworkDrive(drive, networkPath, None, None)
   2.134              #browser = '/usr/bin/iceweasel'
   2.135              #browser = '/usr/bin/midori'
   2.136              #browser = '/usr/bin/chromium '
   2.137 @@ -603,8 +618,13 @@
   2.138              result = checkResult(Cygwin.sshExecuteX11(browser, new_ip, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + new_sdvm + '/dvm_key'))
   2.139          except:
   2.140              logger.error("BrowsingHandler failed. Cleaning up")
   2.141 -        if drive != None:    
   2.142 -            self.vmm.unmapNetworkDrive(drive)
   2.143 +        
   2.144 +        if drive != None: # and networkPath != None:    
   2.145 +            #self.vmm.unmapNetworkDrive(drive) #, networkPath)
   2.146 +            driveHandler = UnmapDriveHandler(self.vmm, drive)
   2.147 +            driveHandler.start()
   2.148 +
   2.149 +            
   2.150          self.vmm.poweroffVM(new_sdvm)
   2.151          self.vmm.removeVM(new_sdvm)
   2.152                  
   2.153 @@ -622,6 +642,7 @@
   2.154          
   2.155      def run(self):
   2.156          self.connectedRSDs = dict()
   2.157 +        
   2.158          while self.running:
   2.159              tmp_rsds = self.vmm.getConnectedRSDS()
   2.160              
   2.161 @@ -629,11 +650,14 @@
   2.162              for vm_name in self.attachedRSDs.keys():
   2.163                  if self.attachedRSDs[vm_name] not in tmp_rsds.values():
   2.164                      drive = self.vmm.getNetworkDrive(vm_name)
   2.165 -                    self.vmm.unmapNetworkDrive(drive)
   2.166                      #self.stopVM(vm_name)
   2.167                      self.vmm.detachRSD(vm_name)
   2.168                      self.vmm.poweroffVM(vm_name)
   2.169                      self.vmm.removeVM(vm_name)
   2.170 +                    if drive != None:
   2.171 +                        #self.vmm.unmapNetworkDrive(drive)
   2.172 +                        driveHandler = UnmapDriveHandler(self.vmm, drive)
   2.173 +                        driveHandler.start()
   2.174                      break
   2.175                      
   2.176                      
   2.177 @@ -644,9 +668,7 @@
   2.178              
   2.179              logger.info("Something's changed")          
   2.180              self.connectedRSDs = tmp_rsds
   2.181 -            
   2.182 -            
   2.183 -            
   2.184 +           
   2.185              #create new vm for attached device if any
   2.186              self.attachedRSDs = self.vmm.getAttachedRSDs()
   2.187              self.connectedRSDs = self.vmm.getConnectedRSDS()