OpenSecurity/bin/vmmanager.pyw
author BarthaM@N3SIM1218.D03.arc.local
Tue, 29 Apr 2014 15:40:48 +0100
changeset 135 c9499f5166c7
parent 133 6649faffb63c
child 141 ca6622112caa
permissions -rwxr-xr-x
unmount thread, stricthostkeychecking disable
mb@90
     1
'''
mb@90
     2
Created on Nov 19, 2013
mb@90
     3
mb@90
     4
@author: BarthaM
mb@90
     5
'''
mb@90
     6
import os
mb@90
     7
import os.path
mb@90
     8
from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
mb@90
     9
import sys
mb@90
    10
import re
mb@90
    11
mb@90
    12
from cygwin import Cygwin
mb@90
    13
from environment import Environment
mb@90
    14
import threading
mb@90
    15
import time
mb@90
    16
import string
mb@90
    17
mb@90
    18
import shutil
mb@90
    19
import stat
mb@90
    20
import tempfile
mb@90
    21
from opensecurity_util import logger, setupLogger, OpenSecurityException
mb@90
    22
import ctypes
mb@90
    23
import itertools
mb@90
    24
import _winreg
mb@90
    25
DEBUG = True
mb@90
    26
mb@90
    27
class VMManagerException(Exception):
mb@90
    28
    def __init__(self, value):
mb@90
    29
        self.value = value
mb@90
    30
    def __str__(self):
mb@90
    31
        return repr(self.value)
mb@90
    32
mb@90
    33
class USBFilter:
mb@90
    34
    vendorid = ""
mb@90
    35
    productid = ""
mb@90
    36
    revision = ""
mb@90
    37
    
mb@90
    38
    def __init__(self, vendorid, productid, revision):
mb@90
    39
        self.vendorid = vendorid.lower()
mb@90
    40
        self.productid = productid.lower()
mb@90
    41
        self.revision = revision.lower()
mb@90
    42
        return
mb@90
    43
    
mb@90
    44
    def __eq__(self, other):
mb@90
    45
        return self.vendorid == other.vendorid and self.productid == other.productid and self.revision == other.revision
mb@90
    46
    
mb@90
    47
    def __hash__(self):
mb@90
    48
        return hash(self.vendorid) ^ hash(self.productid) ^ hash(self.revision)
mb@90
    49
    
mb@90
    50
    def __repr__(self):
mb@90
    51
        return "VendorId = \'" + str(self.vendorid) + "\' ProductId = \'" + str(self.productid) + "\' Revision = \'" + str(self.revision) + "\'"
mb@90
    52
    
mb@90
    53
    #def __getitem__(self, item):
mb@90
    54
    #    return self.coords[item]
mb@90
    55
 
mb@90
    56
class VMManager(object):
mb@90
    57
    vmRootName = "SecurityDVM"
mb@90
    58
    systemProperties = None
mb@90
    59
    _instance = None
mb@90
    60
    machineFolder = ''
mb@90
    61
    rsdHandler = None
oliver@131
    62
    status_message = 'Starting up...'
oliver@131
    63
oliver@131
    64
 
oliver@131
    65
    def __init__(self):
oliver@131
    66
oliver@131
    67
        self.systemProperties = self.getSystemProperties()
oliver@133
    68
        self.machineFolder = self.systemProperties["Default machine folder"]
oliver@131
    69
oliver@131
    70
        # only proceed if we have a working background environment
oliver@131
    71
        if self.backend_ok():
oliver@131
    72
            self.cleanup()
oliver@131
    73
            self.rsdHandler = DeviceHandler(self)
oliver@131
    74
            self.rsdHandler.start()
oliver@131
    75
        else:
oliver@131
    76
            logger.critical(self.status_message)
mb@90
    77
    
oliver@131
    78
mb@90
    79
    @staticmethod
mb@90
    80
    def getInstance():
mb@90
    81
        if VMManager._instance == None:
mb@90
    82
            VMManager._instance = VMManager()
mb@90
    83
        return VMManager._instance
mb@90
    84
    
oliver@131
    85
oliver@131
    86
    def backend_ok(self):
oliver@131
    87
oliver@131
    88
        """check if the backend (VirtualBox) is sufficient for our task"""
oliver@131
    89
oliver@131
    90
        # ensure we have our system props
oliver@131
    91
        if self.systemProperties == None:
oliver@131
    92
            self.systemProperties = self.getSystemProperties()
oliver@131
    93
        if self.systemProperties == None:
oliver@131
    94
            self.status_message = 'Failed to get backend system properties. Is Backend (VirtualBox?) installed?'
oliver@131
    95
            return False
oliver@131
    96
oliver@131
    97
        # check for existing Extension pack
oliver@131
    98
        if not 'Remote desktop ExtPack' in self.systemProperties:
oliver@131
    99
            self.status_message = 'No remote desktop extension pack found. Please install the "Oracle VM VirtualBox Extension Pack" from https://www.virtualbox.org/wiki/Downloads.'
oliver@131
   100
            return False
oliver@131
   101
        if self.systemProperties['Remote desktop ExtPack'] == 'Oracle VM VirtualBox Extension Pack ':
oliver@131
   102
            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.'
oliver@131
   103
            return False
oliver@131
   104
oliver@131
   105
        # check if we do have our root VMs installed
oliver@131
   106
        vms = self.listVM()
oliver@131
   107
        if not self.vmRootName in vms:
oliver@131
   108
            self.status_message = 'Unable to locate root SecurityDVM. Please download and setup the initial image.'
oliver@131
   109
            return False
oliver@131
   110
oliver@131
   111
        # basically all seems nice and ready to rumble
oliver@131
   112
        self.status_message = 'All is ok.'
oliver@131
   113
oliver@131
   114
        return True
oliver@131
   115
oliver@131
   116
mb@90
   117
    def cleanup(self):
mb@90
   118
        if self.rsdHandler != None:
mb@90
   119
            self.rsdHandler.stop()
mb@90
   120
            self.rsdHandler.join()
mb@90
   121
        drives = self.getNetworkDrives()
mb@90
   122
        for drive in drives.keys():
BarthaM@135
   123
            driveHandler = UnmapDriveHandler(self, drive)
BarthaM@135
   124
            driveHandler.start()
mb@90
   125
        for vm in self.listSDVM():
mb@90
   126
            self.poweroffVM(vm)
mb@90
   127
            self.removeVM(vm)
mb@90
   128
        
mb@90
   129
    # return hosty system properties
mb@90
   130
    def getSystemProperties(self):
mb@90
   131
        result = checkResult(Cygwin.vboxExecute('list systemproperties'))
mb@90
   132
        if result[1]=='':
mb@90
   133
            return None
mb@90
   134
        props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
mb@90
   135
        return props
mb@90
   136
    
mb@90
   137
    # return the folder containing the guest VMs     
mb@90
   138
    def getMachineFolder(self):
mb@90
   139
        return self.machineFolder
mb@90
   140
mb@90
   141
    # list all existing VMs registered with VBox
mb@90
   142
    def listVM(self):
mb@90
   143
        result = checkResult(Cygwin.vboxExecute('list vms'))[1]
mb@90
   144
        vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
mb@90
   145
        return vms
mb@90
   146
    
mb@90
   147
    # list running VMs
mb@90
   148
    def listRunningVMS(self):
mb@90
   149
        result = checkResult(Cygwin.vboxExecute('list runningvms'))[1]
mb@90
   150
        vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
mb@90
   151
        return vms
mb@90
   152
    
mb@90
   153
    # list existing SDVMs
mb@90
   154
    def listSDVM(self):
mb@90
   155
        vms = self.listVM()
mb@90
   156
        svdms = []
mb@90
   157
        for vm in vms:
mb@90
   158
            if vm.startswith(self.vmRootName) and vm != self.vmRootName:
mb@90
   159
                svdms.append(vm)
mb@90
   160
        return svdms
mb@90
   161
    
mb@90
   162
    # generate valid (not already existing SDVM name). necessary for creating a new VM
mb@90
   163
    def generateSDVMName(self):
mb@90
   164
        vms = self.listVM()
mb@90
   165
        for i in range(0,999):
mb@90
   166
            if(not self.vmRootName+str(i) in vms):
mb@90
   167
                return self.vmRootName+str(i)
mb@90
   168
        return ''
mb@90
   169
    
mb@90
   170
    # check if the device is mass storage type
mb@90
   171
    @staticmethod
mb@90
   172
    def isMassStorageDevice(device):
mb@90
   173
        keyname = 'SYSTEM\CurrentControlSet\Enum\USB' + '\VID_' + device.vendorid+'&'+'PID_'+ device.productid
mb@90
   174
        key = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname)
mb@90
   175
        #subkeys = _winreg.QueryInfoKey(key)[0]
mb@90
   176
        #for i in range(0, subkeys):
mb@90
   177
        #    print _winreg.EnumKey(key, i)     
mb@90
   178
        devinfokeyname = _winreg.EnumKey(key, 0)
mb@90
   179
        _winreg.CloseKey(key)
mb@90
   180
mb@90
   181
        devinfokey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, keyname+'\\'+devinfokeyname)
mb@90
   182
        value = _winreg.QueryValueEx(devinfokey, 'SERVICE')[0]
mb@90
   183
        _winreg.CloseKey(devinfokey)
mb@90
   184
        
mb@90
   185
        return 'USBSTOR' in value
mb@90
   186
    
mb@90
   187
    # return the RSDs connected to the host
mb@90
   188
    @staticmethod
mb@90
   189
    def getConnectedRSDS():
mb@90
   190
        results = checkResult(Cygwin.vboxExecute('list usbhost'))[1]
mb@90
   191
        results = results.split('Host USB Devices:')[1].strip()
mb@90
   192
        
mb@90
   193
        items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
mb@90
   194
        rsds = dict()   
mb@90
   195
        for item in items:
mb@90
   196
            props = dict()
mb@90
   197
            for line in item.splitlines():
mb@90
   198
                if line != "":         
mb@90
   199
                    k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
mb@90
   200
                    props[k] = v
mb@90
   201
            
mb@90
   202
            #if 'Product' in props.keys() and props['Product'] == 'Mass Storage':
mb@90
   203
            
mb@90
   204
            usb_filter = USBFilter( re.search(r"\((?P<vid>[0-9A-Fa-f]+)\)", props['VendorId']).groupdict()['vid'], 
mb@90
   205
                                    re.search(r"\((?P<pid>[0-9A-Fa-f]+)\)", props['ProductId']).groupdict()['pid'],
mb@90
   206
                                    re.search(r"\((?P<rev>[0-9A-Fa-f]+)\)", props['Revision']).groupdict()['rev'] )
mb@90
   207
            if VMManager.isMassStorageDevice(usb_filter):
mb@90
   208
                rsds[props['UUID']] = usb_filter;
mb@90
   209
                logger.debug(usb_filter)
mb@90
   210
        return rsds
mb@90
   211
    
mb@90
   212
    # return the RSDs attached to all existing SDVMs
mb@90
   213
    def getAttachedRSDs(self):
mb@90
   214
        vms = self.listSDVM()
mb@90
   215
        attached_devices = dict()
mb@90
   216
        for vm in vms:
mb@90
   217
            rsd_filter = self.getUSBFilter(vm)
mb@90
   218
            if rsd_filter != None:
mb@90
   219
                attached_devices[vm] = rsd_filter
mb@90
   220
        return attached_devices
mb@90
   221
    
mb@90
   222
    # configures hostonly networking and DHCP server. requires admin rights
mb@90
   223
    def configureHostNetworking(self):
mb@90
   224
        #cmd = 'vboxmanage list hostonlyifs'
mb@90
   225
        #Cygwin.vboxExecute(cmd)
mb@90
   226
        #cmd = 'vboxmanage hostonlyif remove \"VirtualBox Host-Only Ethernet Adapter\"'
mb@90
   227
        #Cygwin.vboxExecute(cmd)
mb@90
   228
        #cmd = 'vboxmanage hostonlyif create'
mb@90
   229
        #Cygwin.vboxExecute(cmd)
mb@90
   230
        checkResult(Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'))
mb@90
   231
        #cmd = 'vboxmanage dhcpserver add'
mb@90
   232
        #Cygwin.vboxExecute(cmd)
mb@90
   233
        checkResult(Cygwin.vboxExecute('dhcpserver modify --ifname \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.100 --netmask 255.255.255.0 --lowerip 192.168.56.101 --upperip 192.168.56.200'))
mb@90
   234
    
BarthaM@125
   235
    def isSDVMExisting(self, vm_name):
BarthaM@125
   236
        sdvms = self.listSDVM()
BarthaM@125
   237
        return vm_name in sdvms
BarthaM@125
   238
        
mb@90
   239
    #create new virtual machine instance based on template vm named SecurityDVM (\SecurityDVM\SecurityDVM.vmdk)
mb@90
   240
    def createVM(self, vm_name):
BarthaM@125
   241
        if self.isSDVMExisting(vm_name):
BarthaM@125
   242
            return
BarthaM@125
   243
        #remove eventually existing SDVM folder
BarthaM@125
   244
        machineFolder = Cygwin.cygPath(self.machineFolder)
BarthaM@125
   245
        checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
mb@90
   246
        hostonly_if = self.getHostOnlyIFs()
mb@90
   247
        checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register'))
mb@90
   248
        checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat'))
mb@90
   249
        checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'))
mb@90
   250
        return
mb@90
   251
    
mb@90
   252
    # attach storage image to controller
mb@90
   253
    def storageAttach(self, vm_name):
mb@90
   254
        if self.isStorageAttached(vm_name):
mb@90
   255
            self.storageDetach(vm_name)
mb@90
   256
        checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'))
mb@90
   257
    
mb@90
   258
    # return true if storage is attached 
mb@90
   259
    def isStorageAttached(self, vm_name):
mb@90
   260
        info = self.getVMInfo(vm_name)
mb@90
   261
        return (info['SATA-0-0']!='none')
mb@90
   262
    
mb@90
   263
    # detach storage from controller
mb@90
   264
    def storageDetach(self, vm_name):
mb@90
   265
        if self.isStorageAttached(vm_name):
mb@90
   266
            checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none'))
mb@90
   267
    
mb@90
   268
    def changeStorageType(self, filename, storage_type):
mb@90
   269
        checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type))
mb@90
   270
    
mb@90
   271
    # list storage snaphots for VM
mb@90
   272
    def updateTemplate(self):
mb@95
   273
mb@90
   274
        self.cleanup()
mb@90
   275
        self.poweroffVM('SecurityDVM')
mb@90
   276
        self.waitShutdown('SecurityDVM')
mb@90
   277
        
mb@90
   278
        # check for updates
mb@90
   279
        self.genCertificateISO('SecurityDVM')
mb@90
   280
        self.attachCertificateISO('SecurityDVM')
mb@90
   281
        
mb@90
   282
        self.storageDetach('SecurityDVM')
mb@90
   283
        results = checkResult(Cygwin.vboxExecute('list hdds'))[1]
mb@90
   284
        results = results.replace('Parent UUID', 'Parent')
mb@90
   285
        items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
mb@90
   286
        
mb@90
   287
        snaps = dict()   
mb@90
   288
        for item in items:
mb@90
   289
            props = dict()
mb@90
   290
            for line in item.splitlines():
mb@90
   291
                if line != "":         
mb@90
   292
                    k,v = line[:line.index(':')].strip(), line[line.index(':')+1:].strip()
mb@90
   293
                    props[k] = v;
mb@90
   294
            snaps[props['UUID']] = props
mb@90
   295
        
mb@90
   296
        
mb@90
   297
        template_storage = self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk'
mb@90
   298
        
mb@90
   299
        # find template uuid
mb@90
   300
        template_uuid = ''
mb@90
   301
        for hdd in snaps.values():
mb@90
   302
            if hdd['Location'] == template_storage:
mb@90
   303
                template_uuid = hdd['UUID']
mb@90
   304
        logger.debug('found parent uuid ' + template_uuid)
mb@90
   305
        
mb@90
   306
        # remove snapshots 
mb@90
   307
        for hdd in snaps.values():
mb@90
   308
            if hdd['Parent'] == template_uuid:
mb@90
   309
                #template_uuid = hdd['UUID']
mb@90
   310
                logger.debug('removing snapshot ' + hdd['UUID'])
mb@90
   311
                checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
mb@90
   312
                # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
mb@90
   313
        
mb@90
   314
        self.changeStorageType(template_storage,'normal')
mb@90
   315
        self.storageAttach('SecurityDVM')
mb@90
   316
        self.startVM('SecurityDVM')
mb@90
   317
        self.waitStartup('SecurityDVM')
mb@90
   318
        checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
mb@90
   319
        checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
mb@90
   320
        #self.stopVM('SecurityDVM')
mb@90
   321
        self.hibernateVM('SecurityDVM')
mb@90
   322
        self.waitShutdown('SecurityDVM')
mb@90
   323
        self.storageDetach('SecurityDVM')
mb@90
   324
        self.changeStorageType(template_storage,'immutable')
mb@90
   325
        self.storageAttach('SecurityDVM')
mb@90
   326
        self.rsdHandler = DeviceHandler(self)
mb@90
   327
        self.rsdHandler.start()
mb@90
   328
    
mb@90
   329
    #remove VM from the system. should be used on VMs returned by listSDVMs    
mb@90
   330
    def removeVM(self, vm_name):
mb@90
   331
        logger.info('Removing ' + vm_name)
mb@90
   332
        checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete'))
mb@90
   333
        machineFolder = Cygwin.cygPath(self.machineFolder)
mb@97
   334
        checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
mb@90
   335
    
mb@90
   336
    # start VM
mb@90
   337
    def startVM(self, vm_name):
mb@90
   338
        logger.info('Starting ' +  vm_name)
mb@90
   339
        result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' ))
mb@90
   340
        while 'successfully started' not in result[1]:
mb@90
   341
            logger.error("Failed to start SDVM: " + vm_name + " retrying")
oliver@129
   342
            logger.error("Command returned:\n" + result[2])
mb@90
   343
            time.sleep(1)
mb@90
   344
            result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless'))
mb@90
   345
        return result[0]
mb@90
   346
    
mb@90
   347
    # return wether VM is running or not
mb@90
   348
    def isVMRunning(self, vm_name):
mb@90
   349
        return vm_name in self.listRunningVMS()    
mb@90
   350
    
mb@90
   351
    # stop VM
mb@90
   352
    def stopVM(self, vm_name):
mb@90
   353
        logger.info('Sending shutdown signal to ' + vm_name)
mb@90
   354
        checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' ))
mb@90
   355
    
mb@90
   356
    # stop VM
mb@90
   357
    def hibernateVM(self, vm_name):
mb@95
   358
        logger.info('Sending hibernate-disk signal to ' + vm_name)
mb@90
   359
        checkResult(Cygwin.sshExecute( '"sudo hibernate-disk&"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False))
mb@90
   360
            
mb@90
   361
    # poweroff VM
mb@90
   362
    def poweroffVM(self, vm_name):
mb@90
   363
        if not self.isVMRunning(vm_name):
mb@90
   364
            return
mb@90
   365
        logger.info('Powering off ' + vm_name)
mb@90
   366
        return checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff'))
mb@90
   367
    
mb@90
   368
    #list the hostonly IFs exposed by the VBox host
mb@90
   369
    @staticmethod    
mb@90
   370
    def getHostOnlyIFs():
mb@90
   371
        result = Cygwin.vboxExecute('list hostonlyifs')[1]
mb@90
   372
        if result=='':
mb@90
   373
            return None
mb@90
   374
        props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result.strip().splitlines()))
mb@90
   375
        return props
mb@90
   376
    
mb@90
   377
    # return the hostOnly IP for a running guest or the host
mb@90
   378
    @staticmethod    
mb@90
   379
    def getHostOnlyIP(vm_name):
mb@90
   380
        if vm_name == None:
oliver@129
   381
            logger.info('Getting hostOnly IP address for Host')
mb@90
   382
            return VMManager.getHostOnlyIFs()['IPAddress']
mb@90
   383
        else:
oliver@129
   384
            logger.info('Getting hostOnly IP address ' + vm_name)
mb@90
   385
            result = checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
mb@90
   386
            if result=='':
mb@90
   387
                return None
mb@90
   388
            result = result[1]
mb@90
   389
            if result.startswith('No value set!'):
mb@90
   390
                return None
mb@90
   391
            return result[result.index(':')+1:].strip()
mb@90
   392
            
mb@90
   393
    # attach removable storage device to VM by provision of filter
mb@90
   394
    def attachRSD(self, vm_name, rsd_filter):
mb@90
   395
        return checkResult(Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision))
mb@90
   396
    
mb@90
   397
    # detach removable storage from VM by 
mb@90
   398
    def detachRSD(self, vm_name):
mb@90
   399
        return checkResult(Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name))
mb@90
   400
        
mb@90
   401
    # return the description set for an existing VM
mb@90
   402
    def getVMInfo(self, vm_name):
mb@90
   403
        results = checkResult(Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable'))[1]
mb@90
   404
        props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
mb@90
   405
        return props
mb@90
   406
    
mb@90
   407
    # return the configured USB filter for an existing VM 
mb@90
   408
    def getUSBFilter(self, vm_name):
mb@90
   409
        props = self.getVMInfo(vm_name)
mb@90
   410
        keys = set(['USBFilterVendorId1', 'USBFilterProductId1', 'USBFilterRevision1'])
mb@90
   411
        keyset = set(props.keys())
mb@90
   412
        usb_filter = None
mb@90
   413
        if keyset.issuperset(keys):
mb@90
   414
            usb_filter = USBFilter(props['USBFilterVendorId1'], props['USBFilterProductId1'], props['USBFilterRevision1'])
mb@90
   415
        return usb_filter
mb@90
   416
    
mb@90
   417
    #generates ISO containing authorized_keys for use with guest VM
mb@90
   418
    def genCertificateISO(self, vm_name):
mb@90
   419
        machineFolder = Cygwin.cygPath(self.machineFolder)
mb@90
   420
        # remove .ssh folder if exists
mb@97
   421
        checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
mb@90
   422
        # remove .ssh folder if exists
mb@97
   423
        checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"'))
mb@90
   424
        # create .ssh folder in vm_name
mb@97
   425
        checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
mb@90
   426
        # generate dvm_key pair in vm_name / .ssh     
mb@97
   427
        checkResult(Cygwin.bashExecute('/usr/bin/ssh-keygen -q -t rsa -N \\\"\\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"'))
mb@90
   428
        # move out private key
mb@97
   429
        checkResult(Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"'))
mb@90
   430
        # set permissions for private key
mb@97
   431
        checkResult(Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"'))
mb@90
   432
        # rename public key to authorized_keys
mb@97
   433
        checkResult(Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
mb@90
   434
        # set permissions for authorized_keys
mb@97
   435
        checkResult(Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
mb@90
   436
        # generate iso image with .ssh/authorized keys
mb@97
   437
        checkResult(Cygwin.bashExecute('/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
mb@90
   438
    
mb@90
   439
    # attaches generated ssh public cert to guest vm
mb@90
   440
    def attachCertificateISO(self, vm_name):
mb@90
   441
        result = checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + self.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'))
mb@90
   442
        return result
mb@90
   443
    
mb@90
   444
    # wait for machine to come up
mb@90
   445
    def waitStartup(self, vm_name, timeout_ms = 30000):
mb@90
   446
        checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout'))
mb@90
   447
        return VMManager.getHostOnlyIP(vm_name)
mb@90
   448
    
mb@90
   449
    # wait for machine to shutdown
mb@90
   450
    def waitShutdown(self, vm_name):
mb@90
   451
        while vm_name in self.listRunningVMS():
mb@90
   452
            time.sleep(1)
mb@90
   453
        return
mb@90
   454
        
mb@90
   455
    # handles browsing request    
mb@90
   456
    def handleBrowsingRequest(self):
mb@90
   457
        handler = BrowsingHandler(self)
mb@90
   458
        handler.start()
mb@90
   459
        return 'ok'
mb@90
   460
    
BarthaM@135
   461
    #Small function to check if the mentioned location is a directory
mb@90
   462
    def isDirectory(self, path):
mb@90
   463
        result = checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."'))
mb@90
   464
        return string.find(result[1], 'DIR',)
mb@90
   465
mb@90
   466
    def mapNetworkDrive(self, drive, networkPath, user, password):
BarthaM@135
   467
        #self.unmapNetworkDrive(drive)
mb@90
   468
        #Check for drive availability
mb@90
   469
        if os.path.exists(drive):
mb@90
   470
            logger.error("Drive letter is already in use: " + drive)
mb@90
   471
            return -1
mb@90
   472
        #Check for network resource availability
mb@90
   473
        retry = 5
mb@90
   474
        while not os.path.exists(networkPath):
mb@90
   475
            time.sleep(1)
mb@90
   476
            if retry == 0:
mb@90
   477
                return -1
mb@90
   478
            logger.info("Path not accessible: " + networkPath + " retrying")
mb@90
   479
            retry-=1
mb@90
   480
    
mb@90
   481
        command = 'USE ' + drive + ' ' + networkPath + ' /PERSISTENT:NO'
mb@90
   482
        if user != None:
mb@90
   483
            command += ' ' + password + ' /User' + user
mb@90
   484
    
BarthaM@135
   485
        result = checkResult(Cygwin.execute('C:\\Windows\\system32\\NET ', command))
BarthaM@135
   486
        #result = checkResult(Cygwin.cmdExecute('NET ' + command))
mb@90
   487
        if string.find(result[1], 'successfully',) == -1:
mb@90
   488
            logger.error("Failed: NET " + command)
mb@90
   489
            return -1
mb@90
   490
        return 1
mb@90
   491
    
mb@90
   492
    def unmapNetworkDrive(self, drive):
BarthaM@135
   493
        #drives = self.getNetworkDrives()
BarthaM@135
   494
        #if drive not in drives.keys():
BarthaM@135
   495
        #    return 1 
BarthaM@135
   496
        command = 'USE ' + drive + ' /DELETE /YES' #' ' + networkPath +
BarthaM@135
   497
        result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command)) 
BarthaM@135
   498
        #result = checkResult(Cygwin.cmdExecute('NET ' + command)) 
mb@90
   499
        if string.find(str(result[1]), 'successfully',) == -1:
mb@90
   500
            logger.error(result[2])
mb@90
   501
            return -1
mb@90
   502
        return 1
mb@90
   503
    
mb@90
   504
    def getNetworkDrives(self):
mb@90
   505
        ip = VMManager.getHostOnlyIP(None)
mb@90
   506
        ip = ip[:ip.rindex('.')]
mb@90
   507
        drives = dict()    
mb@90
   508
        result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
mb@90
   509
        for line in result[1].splitlines():
mb@90
   510
            if ip in line:
mb@90
   511
                parts = line.split()
mb@90
   512
                drives[parts[1]] = parts[2]
mb@90
   513
        return drives
mb@90
   514
            
mb@90
   515
    def genNetworkDrive(self):
mb@90
   516
        network_drives = self.getNetworkDrives()
mb@90
   517
        logical_drives = VMManager.getLogicalDrives()
mb@90
   518
        drives = list(map(chr, range(68, 91)))  
mb@90
   519
        for drive in drives:
mb@90
   520
            if drive+':' not in network_drives and drive not in logical_drives:
mb@90
   521
                return drive+':'
mb@90
   522
mb@90
   523
    def getNetworkDrive(self, vm_name):
mb@90
   524
        ip = self.getHostOnlyIP(vm_name)
mb@90
   525
        result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
mb@90
   526
        for line in result[1].splitlines():
mb@90
   527
            if line != None and ip in line:
mb@90
   528
                parts = line.split()
BarthaM@135
   529
                return parts[1]
mb@90
   530
    @staticmethod
mb@90
   531
    def getLogicalDrives():
mb@90
   532
        drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
mb@90
   533
        return list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
mb@90
   534
    
mb@90
   535
    @staticmethod
mb@90
   536
    def getDriveType(drive):
mb@90
   537
        return ctypes.cdll.kernel32.GetDriveTypeW(u"%s:\\"%drive)
mb@90
   538
    
mb@90
   539
    @staticmethod
mb@90
   540
    def getVolumeInfo(drive):
mb@90
   541
        volumeNameBuffer = ctypes.create_unicode_buffer(1024)
mb@90
   542
        fileSystemNameBuffer = ctypes.create_unicode_buffer(1024)
mb@90
   543
        serial_number = None
mb@90
   544
        max_component_length = None
mb@90
   545
        file_system_flags = None
mb@90
   546
        
mb@90
   547
        rc = ctypes.cdll.kernel32.GetVolumeInformationW(
mb@90
   548
            #ctypes.c_wchar_p("F:\\"),
mb@90
   549
            u"%s:\\"%drive,
mb@90
   550
            volumeNameBuffer,
mb@90
   551
            ctypes.sizeof(volumeNameBuffer),
mb@90
   552
            serial_number,
mb@90
   553
            max_component_length,
mb@90
   554
            file_system_flags,
mb@90
   555
            fileSystemNameBuffer,
mb@90
   556
            ctypes.sizeof(fileSystemNameBuffer)
mb@90
   557
        )
mb@90
   558
        
mb@90
   559
        return volumeNameBuffer.value, fileSystemNameBuffer.value
mb@90
   560
mb@90
   561
def checkResult(result):
mb@90
   562
    if result[0] != 0:
BarthaM@135
   563
        logger.error('Command failed:' + ''.join(result[2]))
mb@90
   564
        raise OpenSecurityException('Command failed:' + ''.join(result[2]))
mb@90
   565
    return result
mb@90
   566
BarthaM@135
   567
# handles browsing request                    
BarthaM@135
   568
class UnmapDriveHandler(threading.Thread): 
BarthaM@135
   569
    vmm = None
BarthaM@135
   570
    drive = None
BarthaM@135
   571
    running = True
BarthaM@135
   572
    #Cygwin.start_X11()
BarthaM@135
   573
    def __init__(self, vmmanager, drv):
BarthaM@135
   574
        threading.Thread.__init__(self)
BarthaM@135
   575
        self.vmm = vmmanager            
BarthaM@135
   576
        self.drive = drv
BarthaM@135
   577
        
BarthaM@135
   578
    def run(self):
BarthaM@135
   579
        while self.running:
BarthaM@135
   580
             self.vmm.unmapNetworkDrive(self.drive)
BarthaM@135
   581
             mappedDrives = self.vmm.getNetworkDrives()
BarthaM@135
   582
             #logger.info(mappedDrives)
BarthaM@135
   583
             #logger.info(self.drive)
BarthaM@135
   584
             if self.drive not in mappedDrives.keys():
BarthaM@135
   585
                 self.running = False
BarthaM@135
   586
    
mb@90
   587
# handles browsing request                    
mb@95
   588
class BrowsingHandler(threading.Thread):   
mb@90
   589
    vmm = None
mb@110
   590
    #Cygwin.start_X11()
mb@90
   591
    def __init__(self, vmmanager):
mb@90
   592
        threading.Thread.__init__(self)
mb@90
   593
        self.vmm = vmmanager
mb@90
   594
     
mb@90
   595
    def run(self):
mb@97
   596
        drive = None
BarthaM@135
   597
        networkPath = None
mb@90
   598
        try:
mb@90
   599
            new_sdvm = self.vmm.generateSDVMName()
mb@90
   600
            self.vmm.createVM(new_sdvm)
mb@90
   601
            self.vmm.storageAttach(new_sdvm)
mb@90
   602
            self.vmm.genCertificateISO(new_sdvm)
mb@90
   603
            self.vmm.attachCertificateISO(new_sdvm)
mb@90
   604
            self.vmm.startVM(new_sdvm)
mb@90
   605
            new_ip = self.vmm.waitStartup(new_sdvm)
BarthaM@125
   606
            drive = self.vmm.genNetworkDrive()
BarthaM@125
   607
            if new_ip != None:
BarthaM@135
   608
                networkPath = '\\\\' + new_ip + '\\Download'
BarthaM@135
   609
                self.vmm.mapNetworkDrive(drive, networkPath, None, None)
mb@95
   610
            #browser = '/usr/bin/iceweasel'
mb@95
   611
            #browser = '/usr/bin/midori'
mb@110
   612
            #browser = '/usr/bin/chromium '
mb@110
   613
            browser = '\\\"/usr/bin/chromium; pidof dbus-launch | xargs kill\\\"'
mb@110
   614
            Cygwin.start_X11()
mb@110
   615
           
mb@110
   616
            #if Cygwin.is_X11_running()==True:
mb@110
   617
            #result = checkResult(Cygwin.bashExecute('DISPLAY=:0 xhost '+new_ip))
mb@95
   618
            result = checkResult(Cygwin.sshExecuteX11(browser, new_ip, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + new_sdvm + '/dvm_key'))
mb@90
   619
        except:
mb@90
   620
            logger.error("BrowsingHandler failed. Cleaning up")
BarthaM@135
   621
        
BarthaM@135
   622
        if drive != None: # and networkPath != None:    
BarthaM@135
   623
            #self.vmm.unmapNetworkDrive(drive) #, networkPath)
BarthaM@135
   624
            driveHandler = UnmapDriveHandler(self.vmm, drive)
BarthaM@135
   625
            driveHandler.start()
BarthaM@135
   626
BarthaM@135
   627
            
mb@90
   628
        self.vmm.poweroffVM(new_sdvm)
mb@90
   629
        self.vmm.removeVM(new_sdvm)
mb@90
   630
                
mb@90
   631
class DeviceHandler(threading.Thread): 
mb@90
   632
    vmm = None
mb@90
   633
    attachedRSDs = None  
mb@90
   634
    connectedRSDs = None
mb@90
   635
    running = True
mb@90
   636
    def __init__(self, vmmanger): 
mb@90
   637
        threading.Thread.__init__(self)
mb@90
   638
        self.vmm = vmmanger
mb@90
   639
 
mb@90
   640
    def stop(self):
mb@90
   641
        self.running = False
mb@90
   642
        
mb@90
   643
    def run(self):
mb@90
   644
        self.connectedRSDs = dict()
BarthaM@135
   645
        
mb@90
   646
        while self.running:
mb@90
   647
            tmp_rsds = self.vmm.getConnectedRSDS()
mb@95
   648
            
mb@95
   649
            self.attachedRSDs = self.vmm.getAttachedRSDs()
mb@95
   650
            for vm_name in self.attachedRSDs.keys():
mb@95
   651
                if self.attachedRSDs[vm_name] not in tmp_rsds.values():
mb@95
   652
                    drive = self.vmm.getNetworkDrive(vm_name)
mb@95
   653
                    #self.stopVM(vm_name)
mb@95
   654
                    self.vmm.detachRSD(vm_name)
mb@95
   655
                    self.vmm.poweroffVM(vm_name)
mb@95
   656
                    self.vmm.removeVM(vm_name)
BarthaM@135
   657
                    if drive != None:
BarthaM@135
   658
                        #self.vmm.unmapNetworkDrive(drive)
BarthaM@135
   659
                        driveHandler = UnmapDriveHandler(self.vmm, drive)
BarthaM@135
   660
                        driveHandler.start()
mb@95
   661
                    break
mb@95
   662
                    
mb@95
   663
                    
mb@90
   664
            if tmp_rsds.keys() == self.connectedRSDs.keys():
mb@90
   665
                logger.debug("Nothing's changed. sleep(3)")
mb@90
   666
                time.sleep(3)
mb@90
   667
                continue
mb@90
   668
            
mb@90
   669
            logger.info("Something's changed")          
mb@90
   670
            self.connectedRSDs = tmp_rsds
BarthaM@135
   671
           
mb@90
   672
            #create new vm for attached device if any
mb@90
   673
            self.attachedRSDs = self.vmm.getAttachedRSDs()
mb@90
   674
            self.connectedRSDs = self.vmm.getConnectedRSDS()
mb@90
   675
            
mb@90
   676
            new_ip = None
mb@90
   677
            for connected_device in self.connectedRSDs.values():
mb@90
   678
                if (self.attachedRSDs and False) or (connected_device not in self.attachedRSDs.values()):
mb@90
   679
                    new_sdvm = self.vmm.generateSDVMName()
mb@90
   680
                    self.vmm.createVM(new_sdvm)
mb@90
   681
                    self.vmm.storageAttach(new_sdvm)
mb@90
   682
                    self.vmm.attachRSD(new_sdvm, connected_device)
mb@90
   683
                    self.vmm.startVM(new_sdvm)
mb@90
   684
                    new_ip = self.vmm.waitStartup(new_sdvm)
mb@90
   685
                    drive = self.vmm.genNetworkDrive()
mb@90
   686
                    if new_ip != None:
mb@90
   687
                        self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\USB', None, None)
mb@90
   688
mb@90
   689
if __name__ == '__main__':
mb@90
   690
    #man = VMManager.getInstance()
mb@90
   691
    #man.listVM()
mb@90
   692
    #print man.getConnectedRSDs()
mb@90
   693
    #print man.getNetworkDrives()
mb@90
   694
    #man.genNetworkDrive()
mb@90
   695
    #drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
mb@90
   696
    #print list(itertools.compress(string.ascii_uppercase,  map(lambda x:ord(x) - ord('0'), bin(drive_bitmask)[:1:-1])))
mb@90
   697
    #print list(map(chr, range(68, 91))) 
mb@90
   698
    #print Cygwin.getRegEntry('SYSTEM\CurrentControlSet\Enum\USB', 'VID_1058&PID_0704')[0]
mb@90
   699
    #devices = VMManager.getConnectedRSDS()
mb@90
   700
    #print devices
mb@90
   701
    
mb@90
   702
    drives = VMManager.getLogicalDrives()
mb@90
   703
    print drives
mb@90
   704
    print VMManager.getDriveType("E")
mb@90
   705
    print VMManager.getVolumeInfo("E")
mb@90
   706
    #for device in devices.values():
mb@90
   707
    #    #print device
mb@90
   708
    #    if VMManager.isMassStorageDevice(device):
mb@90
   709
    #        print device
mb@90
   710
        
mb@90
   711
    
mb@90
   712
    
mb@90
   713
    #time.sleep(-1)
mb@90
   714
    #man.listVM()
mb@90
   715
    #man.listVM()
mb@90
   716
    #man.listVM()
mb@90
   717
    #man.listVM()
mb@90
   718
    #man.genCertificateISO('SecurityDVM0')
mb@90
   719
    #man.guestExecute('SecurityDVM0', '/bin/ls -la')
mb@90
   720
    #logger = setupLogger('VMManager')
mb@90
   721
    #c = Cygwin()
mb@90
   722
    
mb@90
   723
    #man.sshExecute('/bin/ls -la', 'SecurityDVM0')
mb@90
   724
    #man.sshExecuteX11('/usr/bin/iceweasel', 'SecurityDVM0')
mb@90
   725
    #man.removeVM('SecurityDVM0')
mb@90
   726
    #man.netUse('192.168.56.134', 'USB\\')
mb@90
   727
    #ip = '192.168.56.139'
mb@90
   728
    
mb@90
   729
    #man.cygwin_path = 'c:\\cygwin64\\bin\\'
mb@90
   730
    #man.handleDeviceChange()
mb@90
   731
    #print man.listSDVM()
mb@90
   732
    #man.configureHostNetworking()
mb@90
   733
    #new_vm = man.generateSDVMName()
mb@90
   734
    #man.createVM(new_vm)
mb@90
   735
    
mb@90
   736
    #print Cygwin.cmd()
mb@90
   737
    #man.isAvailable('c:')
mb@90
   738
    #ip = man.getHostOnlyIP('SecurityDVM0')
mb@90
   739
    #man.mapNetworkDrive('h:', '\\\\' + ip + '\Download', None, None)
mb@90
   740
    
mb@90
   741
    #man.genCertificateISO(new_vm)
mb@90
   742
    #man.attachCertificateISO(new_vm)
mb@90
   743
    
mb@90
   744
    #man.attachCertificateISO(vm_name)
mb@90
   745
    #man.guestExecute(vm_name, "ls")
mb@90
   746
    #man.sshGuestX11Execute('SecurityDVM1', '/usr/bin/iceweasel')
mb@90
   747
    #time.sleep(60)
mb@90
   748
    #print man.cygwinPath("C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\.ssh\*")
mb@90
   749
    #man.genCertificateISO('SecurityDVM')
mb@90
   750
    #man.attachCertificateISO('SecurityDVM')
mb@90
   751
    #man.isStorageAttached('SecurityDVM')
mb@90
   752
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y update')
mb@90
   753
    #man.guestExecute('SecurityDVM', 'sudo apt-get -y upgrade' )
mb@90
   754
    
mb@90
   755
    #man.stopVM('SecurityDVM')
mb@90
   756
    #man.storageDetach('SecurityDVM')
mb@90
   757
    #man.changeStorageType('C:\Users\BarthaM\VirtualBox VMs\SecurityDVM\SecurityDVM.vmdk','immutable')
mb@90
   758
    #man.storageAttach('SecurityDVM')
mb@90
   759
    
mb@90
   760
    
mb@90
   761
    #cmd = "c:\\cygwin64\\bin\\bash.exe --login -c \"/bin/ls\""
mb@90
   762
    #man.execute(cmd)