# HG changeset patch # User BarthaM@N3SIM1218.D03.arc.local # Date 1409306186 -3600 # Node ID 9480e5ba1a82bfa9bec9e16a9bf0eb9ef5d42447 # Parent 327f282364b9aab85d869cad71441cfa0b6dd0b5 Improoved the update functionality: - Additional validation of template existance - Faster termination of worker threads - Forced template folder cleanup - etc. diff -r 327f282364b9 -r 9480e5ba1a82 OpenSecurity/bin/cygwin.py --- a/OpenSecurity/bin/cygwin.py Fri Aug 22 09:35:02 2014 +0100 +++ b/OpenSecurity/bin/cygwin.py Fri Aug 29 10:56:26 2014 +0100 @@ -72,6 +72,7 @@ theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe') #theClass.user_home = os.path.expanduser("~") theClass.user_home = os.environ['APPDATA']#os.path.expandvars("%APPDATA%") + theClass.allow_exec = True return theClass @@ -87,6 +88,7 @@ vbox_man = '' win_cmd = '' user_home = '' + allow_exec = True """Some nifty methods working with Cygwin""" def __call__(self, command, arguments, wait_return=True, window = False): @@ -140,10 +142,22 @@ def home(): return Cygwin.user_home + @staticmethod + def allowExec(): + Cygwin.allow_exec = True + + @staticmethod + def denyExec(): + Cygwin.allow_exec = False + executeLock = threading.Lock() #executes command on host system @staticmethod def execute(program, arguments, wait_return=True, window = False, stdin = PIPE, stdout = PIPE, stderr = PIPE): + if not Cygwin.allow_exec: + logger.error('Execution cancelled by system (shutting down).') + raise OpenSecurityException('Execution cancelled by system (shutting down).') + _startupinfo = STARTUPINFO() if not window: _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW @@ -254,13 +268,6 @@ cmd = 'cygpath -u \'' + path + '\'' return Cygwin.bashExecute(cmd)[1].rstrip('\n') - @staticmethod - def checkResult(result): - #if result[0] != 0: - # logger.error('Command failed:' + ''.join(result[2])) - # raise OpenSecurityException('Command failed:' + ''.join(result[2])) - return result - # start import os import win32api diff -r 327f282364b9 -r 9480e5ba1a82 OpenSecurity/bin/opensecurityd.pyw --- a/OpenSecurity/bin/opensecurityd.pyw Fri Aug 22 09:35:02 2014 +0100 +++ b/OpenSecurity/bin/opensecurityd.pyw Fri Aug 29 10:56:26 2014 +0100 @@ -155,12 +155,13 @@ tmplateUUID = gvm_mgr.getTemplateUUID() if tmplateUUID != None: logger.debug('found parent uuid ' + tmplateUUID) - gvm_mgr.storageDetach(gvm_mgr.vmRootName) + gvm_mgr.detachStorage(gvm_mgr.vmRootName) gvm_mgr.removeSnapshots(tmplateUUID) gvm_mgr.removeImage(tmplateUUID) else: logger.debug('parent uuid not found') gvm_mgr.removeVM(gvm_mgr.vmRootName) + gvm_mgr.removeVMFolder(gvm_mgr.vmRootName) trace_file_name = os.path.join(Environment('OpenSecurity').log_path, 'OpenSecurity_initial_import.log') trace_file = open(trace_file_name, 'w+') @@ -168,7 +169,7 @@ vm_image = Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/OsecVM.ova' initial_import_script = Cygwin.cygPath(os.path.abspath(os.path.join(os.path.split(__file__)[0], 'initial_vm.sh'))) Cygwin.bashExecute('\\"' + initial_import_script + '\\" \'' + vm_image + '\'', wait_return = False, stdout = trace_file, stderr = trace_file) - + gvm_mgr.start() res = '{ "init_log": "' + trace_file_name.replace('\\', '\\\\') + '" }' return res diff -r 327f282364b9 -r 9480e5ba1a82 OpenSecurity/bin/test_vmmanager.pyw --- a/OpenSecurity/bin/test_vmmanager.pyw Fri Aug 22 09:35:02 2014 +0100 +++ b/OpenSecurity/bin/test_vmmanager.pyw Fri Aug 29 10:56:26 2014 +0100 @@ -201,7 +201,7 @@ #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' ) #man.stopVM('SecurityDVM') - #man.storageDetach('SecurityDVM') + #man.detachStorage('SecurityDVM') #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable') #man.storageAttach('SecurityDVM') diff -r 327f282364b9 -r 9480e5ba1a82 OpenSecurity/bin/vmmanager.pyw --- a/OpenSecurity/bin/vmmanager.pyw Fri Aug 22 09:35:02 2014 +0100 +++ b/OpenSecurity/bin/vmmanager.pyw Fri Aug 29 10:56:26 2014 +0100 @@ -112,7 +112,6 @@ browsingManager = None blacklistedRSD = None status_message = 'Starting up...' - def __init__(self): # only proceed if we have a working background environment @@ -202,8 +201,15 @@ return True + def template_installed(self): + """ check if we do have our root VMs installed """ + vms = self.listVM() + if not self.vmRootName in vms: + self.status_message = 'Unable to locate root SecurityDVM. Please download and setup the initial image.' + return False + return True + def backend_ok(self): - """check if the backend (VirtualBox) is sufficient for our task""" # ensure we have our system props @@ -221,20 +227,16 @@ self.status_message = 'Unsure if suitable extension pack is installed. Please install the "Oracle VM VirtualBox Extension Pack" from https://www.virtualbox.org/wiki/Downloads.' return False - # check if we do have our root VMs installed - vms = self.listVM() - if not self.vmRootName in vms: - self.status_message = 'Unable to locate root SecurityDVM. Please download and setup the initial image.' + # check the existing hostOnly network settings and try to reconfigure if faulty + if not self.verifyHostOnlySettings(): return False - + # basically all seems nice and ready to rumble self.status_message = 'All is ok.' - - self.verifyHostOnlySettings() - return True def stop(self): + Cygwin.denyExec() if self.rsdHandler != None: self.rsdHandler.stop() self.rsdHandler.join() @@ -244,17 +246,21 @@ self.browsingManager.stop() self.browsingManager.join() self.browsingManager = None + Cygwin.allowExec() def start(self): self.stop() - self.browsingManager = BrowsingManager(self) - self.browsingManager.start() - self.rsdHandler = DeviceHandler(self) - self.rsdHandler.start() + Cygwin.allowExec() + if self.backend_ok() and self.template_installed(): + self.browsingManager = BrowsingManager(self) + self.browsingManager.start() + self.rsdHandler = DeviceHandler(self) + self.rsdHandler.start() def cleanup(self): self.stop() + Cygwin.allowExec() ip = self.getHostOnlyIP(None) try: result = urllib2.urlopen('http://127.0.0.1:8090/netcleanup?'+'hostonly_ip='+ip).readline() @@ -320,14 +326,25 @@ # check if the device is mass storage type @staticmethod def isMassStorageDevice(device): - keyname = 'SYSTEM\CurrentControlSet\Enum\USB' + '\VID_' + device.vendorid+'&'+'PID_'+ device.productid - key = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyname) - devinfokeyname = win32api.RegEnumKey(key, 0) - win32api.RegCloseKey(key) - - devinfokey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyname+'\\'+devinfokeyname) - value = win32api.RegQueryValueEx(devinfokey, 'SERVICE')[0] - win32api.RegCloseKey(devinfokey) + vidkey = None + devinfokey = None + value = "" + try: + keyname = 'SYSTEM\CurrentControlSet\Enum\USB' + '\VID_' + device.vendorid+'&'+'PID_'+ device.productid + vidkey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyname) + devinfokeyname = win32api.RegEnumKey(vidkey, 0) + win32api.RegCloseKey(vidkey) + + devinfokey = win32api.RegOpenKey(win32con.HKEY_LOCAL_MACHINE, keyname+'\\'+devinfokeyname) + value = win32api.RegQueryValueEx(devinfokey, 'SERVICE')[0] + win32api.RegCloseKey(devinfokey) + except Exception as ex: + logger.error('Error reading registry.Exception details: %s' %ex) + finally: + if vidkey is not None: + win32api.RegCloseKey(vidkey) + if devinfokey is not None: + win32api.RegCloseKey(devinfokey) return 'USBSTOR' in value @@ -498,8 +515,6 @@ self.changeStorageType(template_storage,'immutable') self.attachStorage(self.vmRootName) - #self.start() - #"SATA-0-0"="C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\Snapshots\{d0af827d-f13a-49be-8ac1-df20b13bda83}.vmdk" #"SATA-ImageUUID-0-0"="d0af827d-f13a-49be-8ac1-df20b13bda83" @staticmethod @@ -918,10 +933,8 @@ self.started.set() logger.info("Browsing SDVM running.") self.restart.wait() - except OpenSecurityException, e: - logger.error(''.join(e)) - except: - logger.error("Unexpected error: " + sys.exc_info()[0]) + except Exception as e: + logger.error("Unexpected error: ".join(e)) logger.error("BrowsingHandler failed. Cleaning up") #self.running= False @@ -938,7 +951,6 @@ self.running = False def run(self): - self.existingRSDs = dict() self.attachedRSDs = self.vmm.getAttachedRSDs()