# HG changeset patch # User mb # Date 1386760036 -3600 # Node ID b44a603b0b95d928d52d3c551faca57e3d8a98a9 # Parent 77509ad4f2e9bccee9d0c6c035f36776f0745e2a changes to handleDeviceChange mapNetworkDrive intead of netUse nad unmapNetworkDrive intead of netUnUse diff -r 77509ad4f2e9 -r b44a603b0b95 OpenSecurity/bin/opensecurityd.py --- a/OpenSecurity/bin/opensecurityd.py Wed Dec 11 10:39:14 2013 +0000 +++ b/OpenSecurity/bin/opensecurityd.py Wed Dec 11 12:07:16 2013 +0100 @@ -81,7 +81,9 @@ """OpenSecurity '/device_change' handler""" def GET(self): - gvm_mgr.handleDeviceChange() + new_ip = gvm_mgr.handleDeviceChange() + if new_ip != None: + gvm_mgr.mapNetworkDrive('h:', '\\\\' + new_ip + '\\USB', None, None) return "os_device_change" diff -r 77509ad4f2e9 -r b44a603b0b95 OpenSecurity/bin/vmmanager.py --- a/OpenSecurity/bin/vmmanager.py Wed Dec 11 10:39:14 2013 +0000 +++ b/OpenSecurity/bin/vmmanager.py Wed Dec 11 12:07:16 2013 +0100 @@ -12,9 +12,11 @@ import _winreg from cygwin import Cygwin import threading +import time +import string -DEBUG = True +DEBUG = False class USBFilter: vendorid = "" @@ -77,7 +79,7 @@ def execute(self, cmd): if DEBUG: print('trying to launch: ' + cmd) - process = Popen(cmd, stdout=PIPE, stderr=PIPE) + process = Popen(cmd, stdout=PIPE, stderr=PIPE) #shell = True if DEBUG: print('launched: ' + cmd) result = process.wait() @@ -222,8 +224,8 @@ # start VM def startVM(self, vm_name): print('starting ' + vm_name) - cmd = 'VBoxManage startvm ' + vm_name + ' --type headless' - print self.execute(cmd) + cmd = 'VBoxManage startvm ' + vm_name + ' --type headless' + print self.execute(cmd) #verify against (0, 'Waiting for VM "SecurityDVM0" to power on...\r\nVM "SecurityDVM0" has been successfully started.\r\n', '') # stop VM def stopVM(self, vm_name): @@ -239,6 +241,8 @@ if result=='': return None result = result[1] + if result.startswith('No value set!'): + return None return result[result.index(':')+1:].strip() # attach removable storage device to VM by provision of filter @@ -300,11 +304,12 @@ def handleDeviceChange(self): if VMManager.handleDeviceChangeLock.acquire(True): #destroy unused vms + new_ip = None attached_devices = self.getAttachedRSDs() connected_devices = self.listRSDS() for vm_name in attached_devices.keys(): if attached_devices[vm_name] not in connected_devices.values(): - # self.netUnUse(vm_name) + self.unmapNetworkDrive('h:') self.stopVM(vm_name) self.removeVM(vm_name) #create new vm for attached device if any @@ -314,10 +319,19 @@ new_sdvm = self.generateSDVMName() self.createVM(new_sdvm) self.attachRSD(new_sdvm, connected_device) + #sleep like method + self.listSDVM() self.startVM(new_sdvm) - - #self.netUse(new_sdvm) + + while new_ip == None: + time.sleep(1) + new_ip = self.getHostOnlyIP(new_sdvm) + while new_ip not in self.startNotifications: + time.sleep(1) + #TODO: cleanup notifications somwhere else (eg. machine shutdown) + self.startNotifications.remove(new_ip) VMManager.handleDeviceChangeLock.release() + return new_ip def handleBrowsingRequest(self): new_sdvm = self.generateSDVMName() @@ -344,16 +358,66 @@ # run command cmd = self.cygwin_path+'bash.exe --login -c \"DISPLAY=:0 ssh -Y -i \\\"' + machineFolder + '\\' + vm_name + '\\dvm_key\\\" ' + user_name + '@' + address + ' ' + prog + '\"' return self.execute(cmd) - - # executes NET USE and connects to samba share on guestos - def netUse(self, vm_name): - ip = self.getHostOnlyIP(vm_name) - cmd = 'net use H: \\' + ip + '\USB' - return self.execute(cmd) - -#if __name__ == '__main__': - #man = VMManager() + #Small function to check the availability of network resource. + def isAvailable(self, path): + cmd = 'IF EXIST ' + path + ' echo YES' + result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate() + return string.find(str(result), 'YES',) + + #Small function to check if the mention location is a directory + def isDirectory(self, path): + cmd = 'dir ' + path + ' | FIND ".."' + result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate() + return string.find(str(result), 'DIR',) + + def mapNetworkDrive(self, drive, networkPath, user, password): + self.unmapNetworkDrive('h:') + #Check for drive availability + if self.isAvailable(drive) > -1: + print "Drive letter is already in use: ", drive + return -1 + #Check for network resource availability + while self.isAvailable(networkPath) == -1: + time.sleep(1) + print "Path not accessible: ", networkPath, " retrying" + #return -1 + + #Prepare 'NET USE' commands + cmd = 'NET USE ' + drive + ' ' + networkPath + if user != None: + cmd = cmd + ' ' + password + ' /User' + user + + print "cmd = ", cmd + #Execute 'NET USE' command with authentication + result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate() + print "Executed: ", cmd + if string.find(str(result), 'successfully',) == -1: + print cmd, " FAILED" + return -1 + #Mapped with first try + return 1 + + def unmapNetworkDrive(self, drive): + #Check if the drive is in use + if self.isAvailable(drive) == -1: + #Drive is not in use + return -1 + #Prepare 'NET USE' command + cmd = 'net use ' + drive + ' /DELETE' + result = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).communicate() + if string.find(str(result), 'successfully',) == -1: + return -1 + return 1 + +if __name__ == '__main__': + + man = VMManager.getInstance() + #man.removeVM('SecurityDVM0') + #man.netUse('192.168.56.134', 'USB\\') + ip = '192.168.56.139' + man.mapNetworkDrive('h:', '\\\\' + ip + '\USB', None, None) + #man.cygwin_path = 'c:\\cygwin64\\bin\\' #man.handleDeviceChange() #print man.listSDVM()