OpenSecurity/bin/vmmanager.pyw
changeset 217 4162648fb167
parent 213 2e0b94e12bfc
child 218 327f282364b9
     1.1 --- a/OpenSecurity/bin/vmmanager.pyw	Fri Jul 18 13:45:09 2014 +0100
     1.2 +++ b/OpenSecurity/bin/vmmanager.pyw	Thu Aug 14 09:51:11 2014 +0100
     1.3 @@ -97,7 +97,7 @@
     1.4  def once(theClass):
     1.5      theClass.systemProperties = theClass.getSystemProperties()
     1.6      theClass.machineFolder =    theClass.systemProperties["Default machine folder"]
     1.7 -    theClass.hostonlyIFs =      theClass.getHostOnlyIFs()
     1.8 +    #theClass.hostonlyIF =       theClass.getHostOnlyIFs()["VirtualBox Host-Only Ethernet Adapter"]
     1.9      theClass.blacklistedRSD =   theClass.loadRSDBlacklist()
    1.10      return theClass
    1.11      
    1.12 @@ -108,7 +108,7 @@
    1.13      _instance = None
    1.14      machineFolder = ''
    1.15      rsdHandler = None
    1.16 -    hostonlyIFs = None
    1.17 +    hostonlyIF = None
    1.18      browsingManager = None
    1.19      blacklistedRSD = None
    1.20      status_message = 'Starting up...'
    1.21 @@ -117,6 +117,7 @@
    1.22      def __init__(self):
    1.23          # only proceed if we have a working background environment
    1.24          if self.backend_ok():
    1.25 +            VMManager.hostonlyIF = self.getHostOnlyIFs()["VirtualBox Host-Only Ethernet Adapter"]
    1.26              self.cleanup()
    1.27          else:
    1.28              logger.critical(self.status_message)
    1.29 @@ -131,11 +132,33 @@
    1.30      #list the hostonly IFs exposed by the VBox host
    1.31      @staticmethod    
    1.32      def getHostOnlyIFs():
    1.33 -        result = Cygwin.vboxExecute('list hostonlyifs')[1]
    1.34 -        if result=='':
    1.35 -            return None
    1.36 -        props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
    1.37 -        return props    
    1.38 +        results = Cygwin.vboxExecute('list hostonlyifs')[1]
    1.39 +        ifs = dict()
    1.40 +        if results=='':
    1.41 +            return ifs
    1.42 +        items = list( "Name:    " + result for result in results.split('Name:    ') if result != '')
    1.43 +        for item in items:
    1.44 +            if item != "":
    1.45 +                props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in item.strip().splitlines()))
    1.46 +                ifs[props["Name"]] = props
    1.47 +        return ifs
    1.48 +                    
    1.49 +        #props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
    1.50 +        #return props
    1.51 +    
    1.52 +    #list the hostonly IFs exposed by the VBox host
    1.53 +    @staticmethod    
    1.54 +    def getDHCPServers():
    1.55 +        results = Cygwin.vboxExecute('list dhcpservers')[1]
    1.56 +        if results=='':
    1.57 +            return dict()
    1.58 +        items = list( "NetworkName:    " + result for result in results.split('NetworkName:    ') if result != '')
    1.59 +        dhcps = dict()   
    1.60 +        for item in items:
    1.61 +            if item != "":
    1.62 +                props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in item.strip().splitlines()))
    1.63 +                dhcps[props["NetworkName"]] = props
    1.64 +        return dhcps 
    1.65          
    1.66      # return hosty system properties
    1.67      @staticmethod
    1.68 @@ -149,6 +172,35 @@
    1.69      # return the folder containing the guest VMs     
    1.70      def getMachineFolder(self):
    1.71          return VMManager.machineFolder
    1.72 +    
    1.73 +    # verifies the hostonly interface and DHCP server settings
    1.74 +    def verifyHostOnlySettings(self):
    1.75 +        interfaceName = "VirtualBox Host-Only Ethernet Adapter"
    1.76 +        networkName = "HostInterfaceNetworking-VirtualBox Host-Only Ethernet Adapter"
    1.77 +        
    1.78 +        hostonlyifs = self.getHostOnlyIFs()
    1.79 +        if not interfaceName in hostonlyifs.keys():
    1.80 +            Cygwin.vboxExecute('hostonlyif create')
    1.81 +            hostonlyifs = self.getHostOnlyIFs()
    1.82 +            if not interfaceName in hostonlyifs.keys():
    1.83 +                return False
    1.84 +        
    1.85 +        interface = hostonlyifs[interfaceName]
    1.86 +        if interface['VBoxNetworkName'] != networkName or interface['DHCP'] != 'Disabled' or interface['IPAddress'] != '192.168.56.1':
    1.87 +            Cygwin.vboxExecute('hostonlyif ipconfig "' + interfaceName + '" --ip 192.168.56.1 --netmask 255.255.255.0')
    1.88 +            
    1.89 +        dhcpservers = self.getDHCPServers()
    1.90 +        if not networkName in dhcpservers.keys():
    1.91 +            Cygwin.vboxExecute('dhcpserver add --ifname "' + interfaceName + '" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.254 --enable')
    1.92 +            dhcpservers = self.getDHCPServers()
    1.93 +            if not networkName in dhcpservers.keys():
    1.94 +                return False
    1.95 +            
    1.96 +        server = dhcpservers[networkName]
    1.97 +        if server['IP'] != '192.168.56.100' or server['NetworkMask'] != '255.255.255.0' or server['lowerIPAddress'] != '192.168.56.101' or server['upperIPAddress'] != '192.168.56.254' or server['Enabled'] != 'Yes':
    1.98 +            Cygwin.vboxExecute('VBoxManage dhcpserver modify --netname "' + networkName + '" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.254 --enable')
    1.99 +        
   1.100 +        return True
   1.101  
   1.102      def backend_ok(self):
   1.103  
   1.104 @@ -178,6 +230,8 @@
   1.105          # basically all seems nice and ready to rumble
   1.106          self.status_message = 'All is ok.'
   1.107  
   1.108 +        self.verifyHostOnlySettings()
   1.109 +        
   1.110          return True
   1.111      
   1.112      def stop(self):
   1.113 @@ -370,9 +424,8 @@
   1.114          #remove eventually existing SDVM folder
   1.115          machineFolder = Cygwin.cygPath(VMManager.machineFolder)
   1.116          Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
   1.117 -        hostonly_if = self.getHostOnlyIFs()
   1.118          Cygwin.checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register'))
   1.119 -        Cygwin.checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 768 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat'))
   1.120 +        Cygwin.checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 768 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + self.hostonlyIF['Name'] + '\" --nic2 nat'))
   1.121          Cygwin.checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'))
   1.122  
   1.123      #create new SecurityDVM with automatically generated name from template (thread safe)        
   1.124 @@ -540,8 +593,7 @@
   1.125      def getHostOnlyIP(self, vm_name):
   1.126          if vm_name == None:
   1.127              logger.info('Getting hostOnly IP address for Host')
   1.128 -            #TODO:// optimise to store on init local variable and return that value (avoid calling list hostonlyifs)
   1.129 -            return VMManager.hostonlyIFs['IPAddress']
   1.130 +            return VMManager.hostonlyIF['IPAddress']
   1.131          else:
   1.132              logger.info('Getting hostOnly IP address ' + vm_name)
   1.133              result = Cygwin.checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
   1.134 @@ -587,7 +639,14 @@
   1.135      
   1.136      # wait for machine to come up
   1.137      def waitStartup(self, vm_name, timeout_ms = 1000):
   1.138 -        Cygwin.checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout', try_count = 60))
   1.139 +        #Cygwin.checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout', try_count = 60))
   1.140 +        started = False
   1.141 +        while not started:
   1.142 +            result = Cygwin.checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' SDVMStarted'))[1]
   1.143 +            if "Value: True" in result:
   1.144 +                started = True
   1.145 +            else:
   1.146 +                time.sleep(3) 
   1.147          return self.getHostOnlyIP(vm_name)
   1.148      
   1.149      # wait for machine to shutdown