Moved drive generation and modified net mount/unmount
authorBarthaM@N3SIM1218.D03.arc.local
Wed, 04 Jun 2014 16:42:37 +0100
changeset 17632c895509a2a
parent 175 448479c75cc1
child 177 269e73c34b20
child 178 e0c11d73a10c
Moved drive generation and modified net mount/unmount
TODO: blacklisting cardreaders is still open
OpenSecurity/bin/opensecurity_client_restful_server.py
OpenSecurity/bin/vmmanager.pyw
     1.1 --- a/OpenSecurity/bin/opensecurity_client_restful_server.py	Tue Jun 03 15:06:47 2014 +0200
     1.2 +++ b/OpenSecurity/bin/opensecurity_client_restful_server.py	Wed Jun 04 16:42:37 2014 +0100
     1.3 @@ -52,6 +52,9 @@
     1.4  import string
     1.5  import win32api
     1.6  import win32con
     1.7 +import win32wnet
     1.8 +import itertools
     1.9 +import ctypes
    1.10  
    1.11  from opensecurity_util import logger, setupLogger, OpenSecurityException
    1.12  if sys.platform == 'win32' or sys.platform == 'cygwin':
    1.13 @@ -75,6 +78,7 @@
    1.14      '/password',                'os_password',
    1.15      '/netmount',                'os_netmount',
    1.16      '/netumount',               'os_netumount',
    1.17 +    '/netcleanup',              'os_netcleanup',
    1.18      '/',                        'os_root'
    1.19  )
    1.20  
    1.21 @@ -258,20 +262,54 @@
    1.22          
    1.23          return 'user queried for password'
    1.24  
    1.25 +def genNetworkDrive():
    1.26 +    logical_drives = getLogicalDrives()
    1.27 +    logger.info("Used logical drive letters: "+ str(logical_drives).strip('[]') )
    1.28 +    drives = list(map(chr, range(68, 91)))  
    1.29 +    for drive in drives:
    1.30 +        if drive not in logical_drives:
    1.31 +            return drive
    1.32 +    return None
    1.33 +            
    1.34 +def getLogicalDrives():
    1.35 +    drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
    1.36 +    drives = list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
    1.37 +    return drives
    1.38 +
    1.39 +def getNetworkPath(drive):
    1.40 +    return win32wnet.WNetGetConnection(drive+':')
    1.41 +
    1.42 +def getDriveType(drive):
    1.43 +    return ctypes.cdll.kernel32.GetDriveTypeW(u"%s:\\"%drive)
    1.44 +        
    1.45 +def getNetworkDrive(path):
    1.46 +    for drive in getLogicalDrives():
    1.47 +        #if is a network drive
    1.48 +        if getDriveType(drive) == 4:
    1.49 +            network_path = getNetworkPath(drive)
    1.50 +            if path in network_path:
    1.51 +                return drive
    1.52 +    return None
    1.53 +
    1.54  # handles netumount request                    
    1.55  class MountNetworkDriveHandler(threading.Thread): 
    1.56 -    drive = None
    1.57 -    resource = None
    1.58 -    
    1.59 -    def __init__(self, drv, net_path):
    1.60 +    networkPath = None
    1.61 +    def __init__(self, net_path):
    1.62          threading.Thread.__init__(self)
    1.63 -        self.drive = drv
    1.64          self.networkPath = net_path
    1.65      
    1.66      def run(self):
    1.67 +        drive = genNetworkDrive()
    1.68 +        if not drive:
    1.69 +            logger.error("Failed to assign drive letter for: " + self.networkPath)
    1.70 +            return 1
    1.71 +        else:
    1.72 +            logger.info("Assigned drive " + drive + " to " + self.networkPath)
    1.73 +        
    1.74          #Check for drive availability
    1.75 -        if os.path.exists(self.drive):
    1.76 -            logger.error("Drive letter is already in use: " + self.drive)
    1.77 +        drive = drive+':'
    1.78 +        if os.path.exists(drive):
    1.79 +            logger.error("Drive letter is already in use: " + drive)
    1.80              return 1
    1.81          
    1.82          #Check for network resource availability
    1.83 @@ -283,7 +321,7 @@
    1.84              logger.info("Path not accessible: " + self.networkPath + " retrying")
    1.85              retry-=1
    1.86      
    1.87 -        command = 'USE ' + self.drive + ' ' + self.networkPath + ' /PERSISTENT:NO'
    1.88 +        command = 'USE ' + drive + ' ' + self.networkPath + ' /PERSISTENT:NO'
    1.89      
    1.90          result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\NET', command))
    1.91          if string.find(result[1], 'successfully',) == -1:
    1.92 @@ -303,41 +341,30 @@
    1.93          if not "net_resource" in args:
    1.94              raise web.badrequest('no net_resource given')
    1.95          
    1.96 -        # we _need_ a drive_letter
    1.97 -        if not "drive_letter" in args:
    1.98 -            raise web.badrequest('no drive_letter given')
    1.99 -
   1.100 -        driveHandler = MountNetworkDriveHandler(args['drive_letter'], args['net_resource'])
   1.101 +        driveHandler = MountNetworkDriveHandler(args['net_resource'])
   1.102          driveHandler.start()
   1.103          driveHandler.join(None)
   1.104          return 'Ok'
   1.105 -
   1.106 -         
   1.107          
   1.108  # handles netumount request                    
   1.109  class UmountNetworkDriveHandler(threading.Thread): 
   1.110 -    drive = None
   1.111 +    networkPath = None
   1.112      running = True
   1.113      
   1.114 -    def __init__(self, drv):
   1.115 +    def __init__(self, path):
   1.116          threading.Thread.__init__(self)
   1.117 -        self.drive = drv
   1.118 +        self.networkPath = path
   1.119  
   1.120      def run(self):
   1.121          while self.running:
   1.122 -            result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
   1.123 -            mappedDrives = list()
   1.124 -            for line in result[1].splitlines():
   1.125 -                if 'USB' in line or 'Download' in line:
   1.126 -                    parts = line.split()
   1.127 -                    mappedDrives.append(parts[1])
   1.128 -            
   1.129 -            logger.info(mappedDrives)
   1.130 -            logger.info(self.drive)
   1.131 -            if self.drive not in mappedDrives:
   1.132 +            drive = getNetworkDrive(self.networkPath)
   1.133 +            if not drive:
   1.134 +                logger.info("Failed to retrieve drive letter for: " + self.networkPath + ". Successfully deleted or missing.")
   1.135                  self.running = False
   1.136              else:
   1.137 -                command = 'USE ' + self.drive + ' /DELETE /YES' 
   1.138 +                drive = drive+':'
   1.139 +                logger.info("Unmounting drive " + drive + " for " + self.networkPath)
   1.140 +                command = 'USE ' + drive + ' /DELETE /YES' 
   1.141                  result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command)) 
   1.142                  if string.find(str(result[1]), 'successfully',) == -1:
   1.143                      logger.error(result[2])
   1.144 @@ -352,15 +379,38 @@
   1.145          # pick the arguments
   1.146          args = web.input()
   1.147          
   1.148 -        # we _need_ a drive_letter
   1.149 -        if not "drive_letter" in args:
   1.150 -            raise web.badrequest('no drive_letter given')
   1.151 +        # we _need_ a net_resource
   1.152 +        if not "net_resource" in args:
   1.153 +            raise web.badrequest('no net_resource given')
   1.154          
   1.155 -        driveHandler = UmountNetworkDriveHandler(args['drive_letter'])
   1.156 +        driveHandler = UmountNetworkDriveHandler(args['net_resource'])
   1.157          driveHandler.start()
   1.158          driveHandler.join(None)
   1.159          return 'Ok'
   1.160 +
   1.161 +class os_netcleanup:
   1.162      
   1.163 +    """OpenSecurity '/netcleanup' handler"""
   1.164 +    
   1.165 +    def GET(self):
   1.166 +        # pick the arguments
   1.167 +        args = web.input()
   1.168 +        
   1.169 +        # we _need_ a net_resource
   1.170 +        if not "hostonly_ip" in args:
   1.171 +            raise web.badrequest('no net_resource given')
   1.172 +        
   1.173 +        ip = args['hostonly_ip']
   1.174 +        ip = ip[:ip.rindex('.')]
   1.175 +        drives = getLogicalDrives()
   1.176 +        for drive in drives:
   1.177 +            # found network drive
   1.178 +            if getDriveType(drive) == 4:
   1.179 +                path = getNetworkPath(drive)
   1.180 +                if ip in path:
   1.181 +                    driveHandler = UmountNetworkDriveHandler(path)
   1.182 +                    driveHandler.start()
   1.183 +                    driveHandler.join(None)
   1.184  
   1.185  class os_root:
   1.186  
     2.1 --- a/OpenSecurity/bin/vmmanager.pyw	Tue Jun 03 15:06:47 2014 +0200
     2.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Wed Jun 04 16:42:37 2014 +0100
     2.3 @@ -24,6 +24,7 @@
     2.4  import win32api
     2.5  import win32con
     2.6  import win32security
     2.7 +import win32wnet
     2.8  import urllib
     2.9  import urllib2
    2.10  DEBUG = True
    2.11 @@ -70,6 +71,7 @@
    2.12      _instance = None
    2.13      machineFolder = ''
    2.14      rsdHandler = None
    2.15 +    hostonlyIFs = None
    2.16      browsingManager = None
    2.17      status_message = 'Starting up...'
    2.18  
    2.19 @@ -78,7 +80,7 @@
    2.20  
    2.21          self.systemProperties = self.getSystemProperties()
    2.22          self.machineFolder = self.systemProperties["Default machine folder"]
    2.23 -
    2.24 +        self.hostonlyIFs = self.getHostOnlyIFs()
    2.25          # only proceed if we have a working background environment
    2.26          if self.backend_ok():
    2.27              self.cleanup()
    2.28 @@ -93,6 +95,27 @@
    2.29              VMManager._instance = VMManager()
    2.30          return VMManager._instance
    2.31      
    2.32 +    #list the hostonly IFs exposed by the VBox host
    2.33 +    @staticmethod    
    2.34 +    def getHostOnlyIFs():
    2.35 +        result = Cygwin.vboxExecute('list hostonlyifs')[1]
    2.36 +        if result=='':
    2.37 +            return None
    2.38 +        props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
    2.39 +        return props    
    2.40 +        
    2.41 +    # return hosty system properties
    2.42 +    @staticmethod
    2.43 +    def getSystemProperties():
    2.44 +        result = Cygwin.checkResult(Cygwin.vboxExecute('list systemproperties'))
    2.45 +        if result[1]=='':
    2.46 +            return None
    2.47 +        props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
    2.48 +        return props
    2.49 +    
    2.50 +    # return the folder containing the guest VMs     
    2.51 +    def getMachineFolder(self):
    2.52 +        return self.machineFolder
    2.53  
    2.54      def backend_ok(self):
    2.55  
    2.56 @@ -145,33 +168,15 @@
    2.57  
    2.58      def cleanup(self):
    2.59          self.stop()
    2.60 -        drives = self.getNetworkDrives()
    2.61 -        for drive in drives.keys():
    2.62 -            try:
    2.63 -                result = urllib2.urlopen('http://127.0.0.1:8090/netumount?'+'drive_letter='+drive).readline()
    2.64 -            except urllib2.URLError:
    2.65 -                logger.info("Network drive disconnect skipped. OpenSecurity Tray client not started yet.")
    2.66 +        ip = self.getHostOnlyIP(None)
    2.67 +        try:
    2.68 +            result = urllib2.urlopen('http://127.0.0.1:8090/netcleanup?'+'hostonly_ip='+ip).readline()
    2.69 +        except urllib2.URLError:
    2.70 +            logger.info("Network drive cleanup all skipped. OpenSecurity Tray client not started yet.")
    2.71              
    2.72 -        #TODO://continue here
    2.73 -        #self.removeSnapshots('SecurityDVM')
    2.74 -        
    2.75          for vm in self.listSDVM():
    2.76              self.poweroffVM(vm)
    2.77              self.removeVM(vm)
    2.78 -        
    2.79 -        
    2.80 -        
    2.81 -    # return hosty system properties
    2.82 -    def getSystemProperties(self):
    2.83 -        result = Cygwin.checkResult(Cygwin.vboxExecute('list systemproperties'))
    2.84 -        if result[1]=='':
    2.85 -            return None
    2.86 -        props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
    2.87 -        return props
    2.88 -    
    2.89 -    # return the folder containing the guest VMs     
    2.90 -    def getMachineFolder(self):
    2.91 -        return self.machineFolder
    2.92  
    2.93      # list all existing VMs registered with VBox
    2.94      def listVM(self):
    2.95 @@ -474,21 +479,12 @@
    2.96          logger.info('Powering off ' + vm_name)
    2.97          return Cygwin.checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff'))
    2.98      
    2.99 -    #list the hostonly IFs exposed by the VBox host
   2.100 -    @staticmethod    
   2.101 -    def getHostOnlyIFs():
   2.102 -        result = Cygwin.vboxExecute('list hostonlyifs')[1]
   2.103 -        if result=='':
   2.104 -            return None
   2.105 -        props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
   2.106 -        return props
   2.107 -    
   2.108 -    # return the hostOnly IP for a running guest or the host
   2.109 -    @staticmethod    
   2.110 -    def getHostOnlyIP(vm_name):
   2.111 +    # return the hostOnly IP for a running guest or the host    
   2.112 +    def getHostOnlyIP(self, vm_name):
   2.113          if vm_name == None:
   2.114              logger.info('Getting hostOnly IP address for Host')
   2.115 -            return VMManager.getHostOnlyIFs()['IPAddress']
   2.116 +            #TODO:// optimise to store on init local variable and return that value (avoid calling list hostonlyifs)
   2.117 +            return self.hostonlyIFs['IPAddress']
   2.118          else:
   2.119              logger.info('Getting hostOnly IP address ' + vm_name)
   2.120              result = Cygwin.checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
   2.121 @@ -535,7 +531,7 @@
   2.122      # wait for machine to come up
   2.123      def waitStartup(self, vm_name, timeout_ms = 30000):
   2.124          Cygwin.checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout'))
   2.125 -        return VMManager.getHostOnlyIP(vm_name)
   2.126 +        return self.getHostOnlyIP(vm_name)
   2.127      
   2.128      # wait for machine to shutdown
   2.129      def waitShutdown(self, vm_name):
   2.130 @@ -548,50 +544,29 @@
   2.131          result = Cygwin.checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."'))
   2.132          return string.find(result[1], 'DIR',)
   2.133      
   2.134 -    def getNetworkDrives(self):
   2.135 -        ip = VMManager.getHostOnlyIP(None)
   2.136 -        ip = ip[:ip.rindex('.')]
   2.137 -        drives = dict()    
   2.138 -        result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
   2.139 -
   2.140 -        #logger.info( result[1] )
   2.141 -        for line in result[1].splitlines():
   2.142 -            if ip in line:
   2.143 -                parts = line.split()
   2.144 -                drives[parts[1]] = parts[2]
   2.145 -
   2.146 -        return drives
   2.147 -
   2.148 -    def getNetworkDrive(self, vm_name):
   2.149 -        ip = self.getHostOnlyIP(vm_name)
   2.150 -        if ip == None:
   2.151 -            return None
   2.152 -        result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
   2.153 -        for line in result[1].splitlines():
   2.154 -            if line != None and ip in line:
   2.155 -                parts = line.split()
   2.156 -                return parts[1]
   2.157 -    
   2.158      def genNetworkDrive(self):
   2.159 -        network_drives = self.getNetworkDrives()
   2.160 -        logger.info("Used network drive letters: "+ str(network_drives.keys()).strip('[]') ) 
   2.161          logical_drives = VMManager.getLogicalDrives()
   2.162          logger.info("Used logical drive letters: "+ str(logical_drives).strip('[]') )
   2.163          drives = list(map(chr, range(68, 91)))  
   2.164          for drive in drives:
   2.165 -            if drive+':' not in network_drives and drive not in logical_drives:
   2.166 -                return drive+':'
   2.167 +            if drive not in logical_drives:
   2.168 +                return drive
   2.169              
   2.170      @staticmethod
   2.171      def getLogicalDrives():
   2.172          drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
   2.173 -        return list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
   2.174 +        drives = list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
   2.175 +        return drives
   2.176      
   2.177      @staticmethod
   2.178      def getDriveType(drive):
   2.179          return ctypes.cdll.kernel32.GetDriveTypeW(u"%s:\\"%drive)
   2.180      
   2.181      @staticmethod
   2.182 +    def getNetworkPath(drive):
   2.183 +        return win32wnet.WNetGetConnection(drive+':')
   2.184 +    
   2.185 +    @staticmethod
   2.186      def getVolumeInfo(drive):
   2.187          volumeNameBuffer = ctypes.create_unicode_buffer(1024)
   2.188          fileSystemNameBuffer = ctypes.create_unicode_buffer(1024)
   2.189 @@ -600,7 +575,6 @@
   2.190          file_system_flags = None
   2.191          
   2.192          rc = ctypes.cdll.kernel32.GetVolumeInformationW(
   2.193 -            #ctypes.c_wchar_p("F:\\"),
   2.194              u"%s:\\"%drive,
   2.195              volumeNameBuffer,
   2.196              ctypes.sizeof(volumeNameBuffer),
   2.197 @@ -610,9 +584,38 @@
   2.198              fileSystemNameBuffer,
   2.199              ctypes.sizeof(fileSystemNameBuffer)
   2.200          )
   2.201 -        
   2.202          return volumeNameBuffer.value, fileSystemNameBuffer.value
   2.203      
   2.204 +    def getNetworkDrive(self, vm_name):
   2.205 +        ip = self.getHostOnlyIP(vm_name)
   2.206 +        if ip == None:
   2.207 +            logger.error("Failed getting hostonly IP for " + vm_name)
   2.208 +            return None
   2.209 +        logger.info("Got IP address for " + vm_name + ': ' + ip)
   2.210 +        for drive in VMManager.getLogicalDrives():
   2.211 +            #if is a network drive
   2.212 +            if VMManager.getDriveType(drive) == 4:
   2.213 +                network_path = VMManager.getNetworkPath(drive)
   2.214 +                if ip in network_path:
   2.215 +                    return drive
   2.216 +        return None
   2.217 +    
   2.218 +    def getNetworkDrives(self):
   2.219 +        ip = self.getHostOnlyIP(None)
   2.220 +        if ip == None:
   2.221 +            logger.error("Failed getting hostonly IP for system")
   2.222 +            return None
   2.223 +        logger.info("Got IP address for system: " + ip)
   2.224 +        ip = ip[:ip.rindex('.')]
   2.225 +        network_drives = dict()
   2.226 +        for drive in VMManager.getLogicalDrives():
   2.227 +            #if is a network drive
   2.228 +            if VMManager.getDriveType(drive) == 4:
   2.229 +                network_path = VMManager.getNetworkPath(drive)
   2.230 +                if ip in network_path:
   2.231 +                    network_drives[drive] = network_path  
   2.232 +        return network_drives
   2.233 +    
   2.234      # handles browsing request    
   2.235      def handleBrowsingRequest(self):
   2.236          handler = BrowsingHandler(self)
   2.237 @@ -670,10 +673,6 @@
   2.238      def run(self):
   2.239          #browser = '\\\"/usr/bin/chromium; pidof dbus-launch | xargs kill\\\"'
   2.240          browser = '\\\"/usr/bin/chromium\\\"'
   2.241 -        #if not Cygwin.is_X11_running():
   2.242 -        #    self.vmm.browsingManager.restart.set()
   2.243 -        #    return
   2.244 -        
   2.245          try:
   2.246              self.vmm.browsingManager.started.wait() 
   2.247              result = Cygwin.checkResult(Cygwin.sshExecuteX11(browser, self.vmm.browsingManager.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vmm.browsingManager.vm_name + '/dvm_key'))
   2.248 @@ -691,7 +690,7 @@
   2.249      restart = None
   2.250      ip_addr = None
   2.251      vm_name = None
   2.252 -    drive = None
   2.253 +    net_resource = None
   2.254      appDataDir = None
   2.255      
   2.256      def __init__(self, vmmanager):
   2.257 @@ -709,14 +708,14 @@
   2.258              self.restart.clear()
   2.259              self.started.clear()
   2.260              
   2.261 -            if self.drive == None:
   2.262 -                logger.info("Missing browsing SDVM's network drive letter. Skipping disconnect")
   2.263 +            if self.net_resource == None:
   2.264 +                logger.info("Missing browsing SDVM's network share. Skipping disconnect")
   2.265              else:
   2.266                  try:
   2.267 -                    browsing_vm = urllib2.urlopen('http://127.0.0.1:8090/netumount?'+'drive_letter='+self.drive).readline()
   2.268 -                    self.drive = None
   2.269 +                    browsing_vm = urllib2.urlopen('http://127.0.0.1:8090/netumount?'+'net_resource='+self.net_resource).readline()
   2.270 +                    self.net_resource = None
   2.271                  except urllib2.URLError:
   2.272 -                    logger.error("Network drive disconnect failed. OpenSecurity Tray client not running.")
   2.273 +                    logger.error("Network share disconnect failed. OpenSecurity Tray client not running.")
   2.274                      continue
   2.275              
   2.276              self.ip_addr = None
   2.277 @@ -738,19 +737,12 @@
   2.278                  else:
   2.279                      logger.info("Got IP address for " + self.vm_name + ' ' + self.ip_addr)
   2.280                  
   2.281 -                self.drive = self.vmm.genNetworkDrive()
   2.282 -                if self.drive == None:
   2.283 -                    logger.error("Failed to assign Network drive letter")
   2.284 -                    continue
   2.285 -                else:
   2.286 -                    logger.info("Assigned drive " + self.drive + " to " + self.vm_name)
   2.287 -                
   2.288                  try:
   2.289 -                    net_resource = '\\\\' + self.ip_addr + '\\Download'
   2.290 -                    result = urllib2.urlopen('http://127.0.0.1:8090/netmount?'+'drive_letter='+self.drive+'&net_resource='+net_resource).readline()
   2.291 +                    self.net_resource = '\\\\' + self.ip_addr + '\\Download'
   2.292 +                    result = urllib2.urlopen('http://127.0.0.1:8090/netmount?'+'net_resource='+self.net_resource).readline()
   2.293                  except urllib2.URLError:
   2.294                      logger.error("Network drive connect failed. OpenSecurity Tray client not running.")
   2.295 -                    self.drive = None
   2.296 +                    self.net_resource = None
   2.297                      continue
   2.298                  
   2.299                  user = self.vmm.getActiveUserName()
   2.300 @@ -802,19 +794,27 @@
   2.301          self.running = False
   2.302          
   2.303      def run(self):
   2.304 -        self.existingRSDs = self.vmm.getExistingRSDs()
   2.305 +        
   2.306 +        self.existingRSDs = dict()
   2.307          self.attachedRSDs = self.vmm.getAttachedRSDs()
   2.308          
   2.309          while self.running:
   2.310              tmp_rsds = self.vmm.getExistingRSDs()
   2.311 +            if tmp_rsds.keys() == self.existingRSDs.keys():
   2.312 +                logger.debug("Nothing's changed. sleep(3)")
   2.313 +                time.sleep(3)
   2.314 +                continue
   2.315 +            
   2.316 +            logger.info("Something's changed")     
   2.317              for vm_name in self.attachedRSDs.keys():
   2.318                  if self.attachedRSDs[vm_name] not in tmp_rsds.values():
   2.319 -                    drive = self.vmm.getNetworkDrive(vm_name)
   2.320 +                    ip = self.vmm.getHostOnlyIP(vm_name)
   2.321 +                    if ip == None:
   2.322 +                        logger.error("Failed getting hostonly IP for " + vm_name)
   2.323 +                        continue
   2.324                      try:
   2.325 -                        if drive != None:
   2.326 -                            result = urllib2.urlopen('http://127.0.0.1:8090/netumount?'+'drive_letter='+drive).readline()
   2.327 -                        else:
   2.328 -                            logger.error("Error getting SDVM's network drive letter.")
   2.329 +                        net_resource = '\\\\' + ip + '\\USB'
   2.330 +                        result = urllib2.urlopen('http://127.0.0.1:8090/netumount?'+'net_resource='+net_resource).readline()
   2.331                      except urllib2.URLError:
   2.332                          logger.error("Network drive disconnect failed. OpenSecurity Tray client not running.")
   2.333                          continue
   2.334 @@ -824,22 +824,11 @@
   2.335                      del self.attachedRSDs[vm_name]
   2.336                      self.vmm.poweroffVM(vm_name)
   2.337                      self.vmm.removeVM(vm_name)
   2.338 -                    break
   2.339 +                    #break
   2.340                      
   2.341 -            if tmp_rsds.keys() == self.existingRSDs.keys():
   2.342 -                logger.debug("Nothing's changed. sleep(3)")
   2.343 -                time.sleep(3)
   2.344 -                continue
   2.345 -            
   2.346 -            logger.info("Something's changed")          
   2.347 -            self.existingRSDs = tmp_rsds
   2.348 -           
   2.349 -            #create new vm for attached device if any
   2.350 -            #self.attachedRSDs = self.vmm.getAttachedRSDs()
   2.351 -            #self.connectedRSDs = self.vmm.getExistingRSDs()
   2.352 -            
   2.353 +            #create new vms for new devices if any
   2.354              new_ip = None
   2.355 -            for new_device in self.existingRSDs.values():
   2.356 +            for new_device in tmp_rsds.values():
   2.357                  if (self.attachedRSDs and False) or (new_device not in self.attachedRSDs.values()):
   2.358                      new_sdvm = self.vmm.newSDVM()
   2.359                      self.vmm.storageAttach(new_sdvm)
   2.360 @@ -860,18 +849,16 @@
   2.361                          self.vmm.poweroffVM(new_sdvm)
   2.362                          self.vmm.removeVM(new_sdvm)
   2.363                          continue
   2.364 -                    drive = self.vmm.genNetworkDrive()
   2.365 -                    if drive == None:
   2.366 -                        logger.error("Error getting drive letter for network drive.")
   2.367 -                        continue
   2.368                      try:
   2.369                          net_resource = '\\\\' + new_ip + '\\USB'
   2.370 -                        result = urllib2.urlopen('http://127.0.0.1:8090/netmount?'+'drive_letter='+drive+'&net_resource='+net_resource).readline()
   2.371 +                        result = urllib2.urlopen('http://127.0.0.1:8090/netmount?'+'net_resource='+net_resource).readline()
   2.372                      except urllib2.URLError:
   2.373                          logger.error("Network drive connect failed (tray client not accessible). Cleaning up.")
   2.374                          self.vmm.poweroffVM(new_sdvm)
   2.375                          self.vmm.removeVM(new_sdvm)
   2.376                          continue
   2.377 +                    
   2.378 +            self.existingRSDs = tmp_rsds
   2.379                          
   2.380  
   2.381  if __name__ == '__main__':
   2.382 @@ -891,6 +878,7 @@
   2.383      print drives
   2.384      print VMManager.getDriveType("E")
   2.385      print VMManager.getVolumeInfo("E")
   2.386 +    print VMManager.getNetworkPath("E")
   2.387      
   2.388      #vmm.backupFile()
   2.389      #for device in devices.values():