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