1.1 --- a/OpenSecurity/bin/opensecurity_client_restful_server.py Wed May 14 13:04:53 2014 +0100
1.2 +++ b/OpenSecurity/bin/opensecurity_client_restful_server.py Wed May 14 18:13:39 2014 +0100
1.3 @@ -45,6 +45,11 @@
1.4 import web
1.5 import threading
1.6 import time
1.7 +import string
1.8 +
1.9 +from opensecurity_util import logger, setupLogger, OpenSecurityException
1.10 +if sys.platform == 'win32' or sys.platform == 'cygwin':
1.11 + from cygwin import Cygwin
1.12
1.13 # local
1.14 import __init__ as opensecurity
1.15 @@ -61,6 +66,8 @@
1.16 '/log', 'os_log',
1.17 '/notification', 'os_notification',
1.18 '/password', 'os_password',
1.19 + '/netmount', 'os_netmount',
1.20 + '/netumount', 'os_netumount',
1.21 '/', 'os_root'
1.22 )
1.23
1.24 @@ -243,6 +250,107 @@
1.25
1.26 return 'user queried for password'
1.27
1.28 +# handles netumount request
1.29 +class MountNetworkDriveHandler(threading.Thread):
1.30 + drive = None
1.31 + resource = None
1.32 +
1.33 + def __init__(self, drv, net_path):
1.34 + threading.Thread.__init__(self)
1.35 + self.drive = drv
1.36 + self.networkPath = net_path
1.37 +
1.38 + def run(self):
1.39 + #Check for drive availability
1.40 + if os.path.exists(self.drive):
1.41 + logger.error("Drive letter is already in use: " + self.drive)
1.42 + return 1
1.43 +
1.44 + #Check for network resource availability
1.45 + retry = 5
1.46 + while not os.path.exists(self.networkPath):
1.47 + time.sleep(1)
1.48 + if retry == 0:
1.49 + return 1
1.50 + logger.info("Path not accessible: " + self.networkPath + " retrying")
1.51 + retry-=1
1.52 +
1.53 + command = 'USE ' + self.drive + ' ' + self.networkPath + ' /PERSISTENT:NO'
1.54 +
1.55 + result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\NET', command))
1.56 + if string.find(result[1], 'successfully',) == -1:
1.57 + logger.error("Failed: NET " + command)
1.58 + return 1
1.59 + return 0
1.60 +
1.61 +class os_netmount:
1.62 +
1.63 + """OpenSecurity '/netmount' handler"""
1.64 +
1.65 + def GET(self):
1.66 + # pick the arguments
1.67 + args = web.input()
1.68 +
1.69 + # we _need_ a net_resource
1.70 + if not "net_resource" in args:
1.71 + raise web.badrequest('no net_resource given')
1.72 +
1.73 + # we _need_ a drive_letter
1.74 + if not "drive_letter" in args:
1.75 + raise web.badrequest('no drive_letter given')
1.76 +
1.77 + driveHandler = MountNetworkDriveHandler(args['drive_letter'], args['net_resource'])
1.78 + driveHandler.start()
1.79 + return 'Ok'
1.80 +
1.81 +
1.82 +
1.83 +# handles netumount request
1.84 +class UmountNetworkDriveHandler(threading.Thread):
1.85 + drive = None
1.86 + running = True
1.87 +
1.88 + def __init__(self, drv):
1.89 + threading.Thread.__init__(self)
1.90 + self.drive = drv
1.91 +
1.92 + def run(self):
1.93 + while self.running:
1.94 + result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
1.95 + mappedDrives = list()
1.96 + for line in result[1].splitlines():
1.97 + if 'USB' in line or 'Download' in line:
1.98 + parts = line.split()
1.99 + mappedDrives.append(parts[1])
1.100 +
1.101 + logger.info(mappedDrives)
1.102 + logger.info(self.drive)
1.103 + if self.drive not in mappedDrives:
1.104 + self.running = False
1.105 + else:
1.106 + command = 'USE ' + self.drive + ' /DELETE /YES'
1.107 + result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command))
1.108 + if string.find(str(result[1]), 'successfully',) == -1:
1.109 + logger.error(result[2])
1.110 + continue
1.111 +
1.112 +
1.113 +class os_netumount:
1.114 +
1.115 + """OpenSecurity '/netumount' handler"""
1.116 +
1.117 + def GET(self):
1.118 + # pick the arguments
1.119 + args = web.input()
1.120 +
1.121 + # we _need_ a drive_letter
1.122 + if not "drive_letter" in args:
1.123 + raise web.badrequest('no drive_letter given')
1.124 +
1.125 + driveHandler = UmountNetworkDriveHandler(args['drive_letter'])
1.126 + driveHandler.start()
1.127 + return 'Ok'
1.128 +
1.129
1.130 class os_root:
1.131
2.1 --- a/OpenSecurity/bin/vmmanager.pyw Wed May 14 13:04:53 2014 +0100
2.2 +++ b/OpenSecurity/bin/vmmanager.pyw Wed May 14 18:13:39 2014 +0100
2.3 @@ -24,6 +24,8 @@
2.4 import win32api
2.5 import win32con
2.6 import win32security
2.7 +import urllib
2.8 +import urllib2
2.9 DEBUG = True
2.10
2.11 class VMManagerException(Exception):
2.12 @@ -125,15 +127,15 @@
2.13 self.rsdHandler.join()
2.14 drives = self.getNetworkDrives()
2.15 for drive in drives.keys():
2.16 - driveHandler = UnmapDriveHandler(self, drive)
2.17 - driveHandler.start()
2.18 + result = urllib2.urlopen('http://127.0.0.1:8090/netumount?'+'drive_letter='+drive).readline()
2.19 +
2.20 for vm in self.listSDVM():
2.21 self.poweroffVM(vm)
2.22 self.removeVM(vm)
2.23
2.24 # return hosty system properties
2.25 def getSystemProperties(self):
2.26 - result = checkResult(Cygwin.vboxExecute('list systemproperties'))
2.27 + result = Cygwin.checkResult(Cygwin.vboxExecute('list systemproperties'))
2.28 if result[1]=='':
2.29 return None
2.30 props = dict((k.strip(),v.strip().strip('"')) for k,v in (line.split(':', 1) for line in result[1].strip().splitlines()))
2.31 @@ -145,13 +147,13 @@
2.32
2.33 # list all existing VMs registered with VBox
2.34 def listVM(self):
2.35 - result = checkResult(Cygwin.vboxExecute('list vms'))[1]
2.36 + result = Cygwin.checkResult(Cygwin.vboxExecute('list vms'))[1]
2.37 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
2.38 return vms
2.39
2.40 # list running VMs
2.41 def listRunningVMS(self):
2.42 - result = checkResult(Cygwin.vboxExecute('list runningvms'))[1]
2.43 + result = Cygwin.checkResult(Cygwin.vboxExecute('list runningvms'))[1]
2.44 vms = list(k.strip().strip('"') for k,_ in (line.split(' ') for line in result.splitlines()))
2.45 return vms
2.46
2.47 @@ -192,7 +194,7 @@
2.48 # return the RSDs connected to the host
2.49 @staticmethod
2.50 def getConnectedRSDS():
2.51 - results = checkResult(Cygwin.vboxExecute('list usbhost'))[1]
2.52 + results = Cygwin.checkResult(Cygwin.vboxExecute('list usbhost'))[1]
2.53 results = results.split('Host USB Devices:')[1].strip()
2.54
2.55 items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
2.56 @@ -232,10 +234,10 @@
2.57 #Cygwin.vboxExecute(cmd)
2.58 #cmd = 'vboxmanage hostonlyif create'
2.59 #Cygwin.vboxExecute(cmd)
2.60 - checkResult(Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'))
2.61 + Cygwin.checkResult(Cygwin.vboxExecute('hostonlyif ipconfig \"VirtualBox Host-Only Ethernet Adapter\" --ip 192.168.56.1 --netmask 255.255.255.0'))
2.62 #cmd = 'vboxmanage dhcpserver add'
2.63 #Cygwin.vboxExecute(cmd)
2.64 - 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'))
2.65 + Cygwin.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'))
2.66
2.67 def isSDVMExisting(self, vm_name):
2.68 sdvms = self.listSDVM()
2.69 @@ -247,18 +249,18 @@
2.70 return
2.71 #remove eventually existing SDVM folder
2.72 machineFolder = Cygwin.cygPath(self.machineFolder)
2.73 - checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
2.74 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
2.75 hostonly_if = self.getHostOnlyIFs()
2.76 - checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register'))
2.77 - checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat'))
2.78 - checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'))
2.79 + Cygwin.checkResult(Cygwin.vboxExecute('createvm --name ' + vm_name + ' --ostype Debian --register'))
2.80 + Cygwin.checkResult(Cygwin.vboxExecute('modifyvm ' + vm_name + ' --memory 512 --vram 10 --cpus 1 --usb on --usbehci on --nic1 hostonly --hostonlyadapter1 \"' + hostonly_if['Name'] + '\" --nic2 nat'))
2.81 + Cygwin.checkResult(Cygwin.vboxExecute('storagectl ' + vm_name + ' --name SATA --add sata --portcount 2'))
2.82 return
2.83
2.84 # attach storage image to controller
2.85 def storageAttach(self, vm_name):
2.86 if self.isStorageAttached(vm_name):
2.87 self.storageDetach(vm_name)
2.88 - checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'))
2.89 + Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium \"'+ self.machineFolder + '\SecurityDVM\SecurityDVM.vmdk\"'))
2.90
2.91 # return true if storage is attached
2.92 def isStorageAttached(self, vm_name):
2.93 @@ -268,10 +270,10 @@
2.94 # detach storage from controller
2.95 def storageDetach(self, vm_name):
2.96 if self.isStorageAttached(vm_name):
2.97 - checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none'))
2.98 + Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 0 --device 0 --type hdd --medium none'))
2.99
2.100 def changeStorageType(self, filename, storage_type):
2.101 - checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type))
2.102 + Cygwin.checkResult(Cygwin.vboxExecute('modifyhd \"' + filename + '\" --type ' + storage_type))
2.103
2.104 # list storage snaphots for VM
2.105 def updateTemplate(self):
2.106 @@ -285,7 +287,7 @@
2.107 self.attachCertificateISO('SecurityDVM')
2.108
2.109 self.storageDetach('SecurityDVM')
2.110 - results = checkResult(Cygwin.vboxExecute('list hdds'))[1]
2.111 + results = Cygwin.checkResult(Cygwin.vboxExecute('list hdds'))[1]
2.112 results = results.replace('Parent UUID', 'Parent')
2.113 items = list( "UUID:"+result for result in results.split('UUID:') if result != '')
2.114
2.115 @@ -313,15 +315,15 @@
2.116 if hdd['Parent'] == template_uuid:
2.117 #template_uuid = hdd['UUID']
2.118 logger.debug('removing snapshot ' + hdd['UUID'])
2.119 - checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
2.120 + Cygwin.checkResult(Cygwin.vboxExecute('closemedium disk {' + hdd['UUID'] + '} --delete'))#[1]
2.121 # parse result 0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%
2.122
2.123 self.changeStorageType(template_storage,'normal')
2.124 self.storageAttach('SecurityDVM')
2.125 self.startVM('SecurityDVM')
2.126 self.waitStartup('SecurityDVM')
2.127 - checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
2.128 - checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
2.129 + Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y update"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
2.130 + Cygwin.checkResult(Cygwin.sshExecute('"sudo apt-get -y upgrade"', VMManager.getHostOnlyIP('SecurityDVM'), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + 'SecurityDVM' + '/dvm_key'))
2.131 #self.stopVM('SecurityDVM')
2.132 self.hibernateVM('SecurityDVM')
2.133 self.waitShutdown('SecurityDVM')
2.134 @@ -334,19 +336,19 @@
2.135 #remove VM from the system. should be used on VMs returned by listSDVMs
2.136 def removeVM(self, vm_name):
2.137 logger.info('Removing ' + vm_name)
2.138 - checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete'))
2.139 + Cygwin.checkResult(Cygwin.vboxExecute('unregistervm ' + vm_name + ' --delete'))
2.140 machineFolder = Cygwin.cygPath(self.machineFolder)
2.141 - checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
2.142 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '\\\"'))
2.143
2.144 # start VM
2.145 def startVM(self, vm_name):
2.146 logger.info('Starting ' + vm_name)
2.147 - result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' ))
2.148 + result = Cygwin.checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless' ))
2.149 while 'successfully started' not in result[1]:
2.150 logger.error("Failed to start SDVM: " + vm_name + " retrying")
2.151 logger.error("Command returned:\n" + result[2])
2.152 time.sleep(1)
2.153 - result = checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless'))
2.154 + result = Cygwin.checkResult(Cygwin.vboxExecute('startvm ' + vm_name + ' --type headless'))
2.155 return result[0]
2.156
2.157 # return wether VM is running or not
2.158 @@ -356,19 +358,19 @@
2.159 # stop VM
2.160 def stopVM(self, vm_name):
2.161 logger.info('Sending shutdown signal to ' + vm_name)
2.162 - checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' ))
2.163 + Cygwin.checkResult(Cygwin.sshExecute( '"sudo shutdown -h now"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key' ))
2.164
2.165 # stop VM
2.166 def hibernateVM(self, vm_name):
2.167 logger.info('Sending hibernate-disk signal to ' + vm_name)
2.168 - checkResult(Cygwin.sshExecute( '"sudo hibernate-disk&"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False))
2.169 + Cygwin.checkResult(Cygwin.sshExecute( '"sudo hibernate-disk&"', VMManager.getHostOnlyIP(vm_name), 'osecuser', Cygwin.cygPath(self.machineFolder) + '/' + vm_name + '/dvm_key', wait_return=False))
2.170
2.171 # poweroff VM
2.172 def poweroffVM(self, vm_name):
2.173 if not self.isVMRunning(vm_name):
2.174 return
2.175 logger.info('Powering off ' + vm_name)
2.176 - return checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff'))
2.177 + return Cygwin.checkResult(Cygwin.vboxExecute('controlvm ' + vm_name + ' poweroff'))
2.178
2.179 #list the hostonly IFs exposed by the VBox host
2.180 @staticmethod
2.181 @@ -387,7 +389,7 @@
2.182 return VMManager.getHostOnlyIFs()['IPAddress']
2.183 else:
2.184 logger.info('Getting hostOnly IP address ' + vm_name)
2.185 - result = checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
2.186 + result = Cygwin.checkResult(Cygwin.vboxExecute('guestproperty get ' + vm_name + ' /VirtualBox/GuestInfo/Net/0/V4/IP'))
2.187 if result=='':
2.188 return None
2.189 result = result[1]
2.190 @@ -397,15 +399,15 @@
2.191
2.192 # attach removable storage device to VM by provision of filter
2.193 def attachRSD(self, vm_name, rsd_filter):
2.194 - return checkResult(Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision))
2.195 + return Cygwin.checkResult(Cygwin.vboxExecute('usbfilter add 0 --target ' + vm_name + ' --name OpenSecurityRSD --vendorid ' + rsd_filter.vendorid + ' --productid ' + rsd_filter.productid + ' --revision ' + rsd_filter.revision))
2.196
2.197 # detach removable storage from VM by
2.198 def detachRSD(self, vm_name):
2.199 - return checkResult(Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name))
2.200 + return Cygwin.checkResult(Cygwin.vboxExecute('usbfilter remove 0 --target ' + vm_name))
2.201
2.202 # return the description set for an existing VM
2.203 def getVMInfo(self, vm_name):
2.204 - results = checkResult(Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable'))[1]
2.205 + results = Cygwin.checkResult(Cygwin.vboxExecute('showvminfo ' + vm_name + ' --machinereadable'))[1]
2.206 props = dict((k.strip().strip('"'),v.strip().strip('"')) for k,v in (line.split('=', 1) for line in results.splitlines()))
2.207 return props
2.208
2.209 @@ -423,32 +425,32 @@
2.210 def genCertificateISO(self, vm_name):
2.211 machineFolder = Cygwin.cygPath(self.machineFolder)
2.212 # remove .ssh folder if exists
2.213 - checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
2.214 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
2.215 # remove .ssh folder if exists
2.216 - checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"'))
2.217 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/rm -rf \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"'))
2.218 # create .ssh folder in vm_name
2.219 - checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
2.220 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
2.221 # generate dvm_key pair in vm_name / .ssh
2.222 - checkResult(Cygwin.bashExecute('/usr/bin/ssh-keygen -q -t rsa -N \\\"\\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"'))
2.223 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/ssh-keygen -q -t rsa -N \\\"\\\" -C \\\"' + vm_name + '\\\" -f \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\"'))
2.224 # move out private key
2.225 - checkResult(Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"'))
2.226 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key\\\" \\\"' + machineFolder + '/' + vm_name + '\\\"'))
2.227 # set permissions for private key
2.228 - checkResult(Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"'))
2.229 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/dvm_key\\\"'))
2.230 # rename public key to authorized_keys
2.231 - checkResult(Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
2.232 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mv \\\"' + machineFolder + '/' + vm_name + '/.ssh/dvm_key.pub\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
2.233 # set permissions for authorized_keys
2.234 - checkResult(Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
2.235 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/chmod 500 \\\"' + machineFolder + '/' + vm_name + '/.ssh/authorized_keys\\\"'))
2.236 # generate iso image with .ssh/authorized keys
2.237 - checkResult(Cygwin.bashExecute('/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
2.238 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/genisoimage -J -R -o \\\"' + machineFolder + '/' + vm_name + '/'+ vm_name + '.iso\\\" \\\"' + machineFolder + '/' + vm_name + '/.ssh\\\"'))
2.239
2.240 # attaches generated ssh public cert to guest vm
2.241 def attachCertificateISO(self, vm_name):
2.242 - 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\"'))
2.243 + result = Cygwin.checkResult(Cygwin.vboxExecute('storageattach ' + vm_name + ' --storagectl SATA --port 1 --device 0 --type dvddrive --mtype readonly --medium \"' + self.machineFolder + '\\' + vm_name + '\\'+ vm_name + '.iso\"'))
2.244 return result
2.245
2.246 # wait for machine to come up
2.247 def waitStartup(self, vm_name, timeout_ms = 30000):
2.248 - checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout'))
2.249 + Cygwin.checkResult(Cygwin.vboxExecute('guestproperty wait ' + vm_name + ' SDVMStarted --timeout ' + str(timeout_ms) + ' --fail-on-timeout'))
2.250 return VMManager.getHostOnlyIP(vm_name)
2.251
2.252 # wait for machine to shutdown
2.253 @@ -459,52 +461,14 @@
2.254
2.255 #Small function to check if the mentioned location is a directory
2.256 def isDirectory(self, path):
2.257 - result = checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."'))
2.258 + result = Cygwin.checkResult(Cygwin.cmdExecute('dir ' + path + ' | FIND ".."'))
2.259 return string.find(result[1], 'DIR',)
2.260 -
2.261 - def mapNetworkDrive(self, drive, networkPath, user, password):
2.262 - #self.unmapNetworkDrive(drive)
2.263 - #Check for drive availability
2.264 - if os.path.exists(drive):
2.265 - logger.error("Drive letter is already in use: " + drive)
2.266 - return -1
2.267 - #Check for network resource availability
2.268 - retry = 5
2.269 - while not os.path.exists(networkPath):
2.270 - time.sleep(1)
2.271 - if retry == 0:
2.272 - return -1
2.273 - logger.info("Path not accessible: " + networkPath + " retrying")
2.274 - retry-=1
2.275 -
2.276 - command = 'USE ' + drive + ' ' + networkPath + ' /PERSISTENT:NO'
2.277 - if user != None:
2.278 - command += ' ' + password + ' /User' + user
2.279 -
2.280 - result = checkResult(Cygwin.execute('C:\\Windows\\system32\\NET', command))
2.281 - #result = checkResult(Cygwin.cmdExecute('NET ' + command))
2.282 - if string.find(result[1], 'successfully',) == -1:
2.283 - logger.error("Failed: NET " + command)
2.284 - return -1
2.285 - return 1
2.286 -
2.287 - def unmapNetworkDrive(self, drive):
2.288 - #drives = self.getNetworkDrives()
2.289 - #if drive not in drives.keys():
2.290 - # return 1
2.291 - command = 'USE ' + drive + ' /DELETE /YES' #' ' + networkPath +
2.292 - result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', command))
2.293 - #result = checkResult(Cygwin.cmdExecute('NET ' + command))
2.294 - if string.find(str(result[1]), 'successfully',) == -1:
2.295 - logger.error(result[2])
2.296 - return -1
2.297 - return 1
2.298
2.299 def getNetworkDrives(self):
2.300 ip = VMManager.getHostOnlyIP(None)
2.301 ip = ip[:ip.rindex('.')]
2.302 drives = dict()
2.303 - result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
2.304 + result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
2.305 for line in result[1].splitlines():
2.306 if ip in line:
2.307 parts = line.split()
2.308 @@ -521,11 +485,12 @@
2.309
2.310 def getNetworkDrive(self, vm_name):
2.311 ip = self.getHostOnlyIP(vm_name)
2.312 - result = checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
2.313 + result = Cygwin.checkResult(Cygwin.execute('C:\\Windows\\system32\\net.exe', 'USE'))
2.314 for line in result[1].splitlines():
2.315 if line != None and ip in line:
2.316 parts = line.split()
2.317 return parts[1]
2.318 +
2.319 @staticmethod
2.320 def getLogicalDrives():
2.321 drive_bitmask = ctypes.cdll.kernel32.GetLogicalDrives()
2.322 @@ -595,34 +560,7 @@
2.323 #command = '-r -v -o StrictHostKeyChecking=no -i \"' + certificate + '\" \"' + src + '\" \"osecuser@' + self.browsingManager.ip_addr + ':' + dest + '\"'
2.324 command = '-r -o StrictHostKeyChecking=no -i "' + certificate + '" "' + src + '" "osecuser@' + self.browsingManager.ip_addr + ':' + dest + '"'
2.325 return Cygwin.execute(Cygwin.cygwin_scp, command, wait_return=True, window=False)
2.326 -
2.327 -
2.328
2.329 -def checkResult(result):
2.330 - if result[0] != 0:
2.331 - logger.error('Command failed:' + ''.join(result[2]))
2.332 - raise OpenSecurityException('Command failed:' + ''.join(result[2]))
2.333 - return result
2.334 -
2.335 -# handles browsing request
2.336 -class UnmapDriveHandler(threading.Thread):
2.337 - vmm = None
2.338 - drive = None
2.339 - running = True
2.340 - #Cygwin.start_X11()
2.341 - def __init__(self, vmmanager, drv):
2.342 - threading.Thread.__init__(self)
2.343 - self.vmm = vmmanager
2.344 - self.drive = drv
2.345 -
2.346 - def run(self):
2.347 - while self.running:
2.348 - self.vmm.unmapNetworkDrive(self.drive)
2.349 - mappedDrives = self.vmm.getNetworkDrives()
2.350 - #logger.info(mappedDrives)
2.351 - #logger.info(self.drive)
2.352 - if self.drive not in mappedDrives.keys():
2.353 - self.running = False
2.354
2.355 #handles browsing session creation
2.356 class BrowsingHandler(threading.Thread):
2.357 @@ -637,7 +575,7 @@
2.358 Cygwin.start_X11()
2.359 try:
2.360 self.vmm.browsingManager.started.wait()
2.361 - result = checkResult(Cygwin.sshExecuteX11(browser, self.vmm.browsingManager.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vmm.browsingManager.vm_name + '/dvm_key'))
2.362 + result = Cygwin.checkResult(Cygwin.sshExecuteX11(browser, self.vmm.browsingManager.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vmm.browsingManager.vm_name + '/dvm_key'))
2.363 #backup settings on vm
2.364 except:
2.365 logger.error("BrowsingHandler closing. Cleaning up")
2.366 @@ -667,9 +605,7 @@
2.367 self.restart.clear()
2.368 self.started.clear()
2.369 if self.drive != None:
2.370 - #driveHandler = UnmapDriveHandler(self.vmm, self.drive)
2.371 - #driveHandler.start()
2.372 - #driveHandler.join()
2.373 + browsing_vm = urllib2.urlopen('http://127.0.0.1:8090/netumount?'+'drive_letter='+self.drive).readline()
2.374 self.drive = None
2.375
2.376 self.ip_addr = None
2.377 @@ -691,8 +627,10 @@
2.378 self.drive = self.vmm.genNetworkDrive()
2.379 if self.drive == None:
2.380 continue
2.381 - networkPath = '\\\\' + self.ip_addr + '\\Download'
2.382 - #self.vmm.mapNetworkDrive(self.drive, networkPath, None, None)
2.383 +
2.384 + net_resource = '\\\\' + self.ip_addr + '\\Download'
2.385 + result = urllib2.urlopen('http://127.0.0.1:8090/netmount?'+'drive_letter='+self.drive+'&net_resource='+net_resource).readline()
2.386 +
2.387 self.started.set()
2.388
2.389 user = self.vmm.getActiveUserName()
2.390 @@ -700,11 +638,11 @@
2.391 path = self.vmm.getAppDataDir(sid)
2.392 self.appDataDir = Cygwin.cygPath(path)
2.393 # create OpenSecurity settings dir on local machine user home /AppData/Roaming
2.394 - checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + self.appDataDir + '/OpenSecurity\\\"'))
2.395 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + self.appDataDir + '/OpenSecurity\\\"'))
2.396 # create chromium settings dir on local machine if not existing
2.397 - checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + self.appDataDir + '/OpenSecurity/chromium\\\"'))
2.398 + Cygwin.checkResult(Cygwin.bashExecute('/usr/bin/mkdir -p \\\"' + self.appDataDir + '/OpenSecurity/chromium\\\"'))
2.399 # create chromium settings dir on remote machine if not existing
2.400 - checkResult(Cygwin.sshExecute('"mkdir -p \\\"/home/osecuser/.config\\\""', self.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vm_name + '/dvm_key'))
2.401 + Cygwin.checkResult(Cygwin.sshExecute('"mkdir -p \\\"/home/osecuser/.config\\\""', self.ip_addr, 'osecuser', Cygwin.cygPath(self.vmm.getMachineFolder()) + '/' + self.vm_name + '/dvm_key'))
2.402 #restore settings on vm
2.403 self.vmm.restoreFile(self.appDataDir + '/OpenSecurity/chromium', '/home/osecuser/.config/')
2.404 self.restart.wait()
2.405 @@ -738,9 +676,7 @@
2.406 self.vmm.poweroffVM(vm_name)
2.407 self.vmm.removeVM(vm_name)
2.408 if drive != None:
2.409 - #self.vmm.unmapNetworkDrive(drive)
2.410 - driveHandler = UnmapDriveHandler(self.vmm, drive)
2.411 - driveHandler.start()
2.412 + result = urllib2.urlopen('http://127.0.0.1:8090/netumount?'+'drive_letter='+drive).readline()
2.413 break
2.414
2.415
2.416 @@ -767,7 +703,9 @@
2.417 new_ip = self.vmm.waitStartup(new_sdvm)
2.418 drive = self.vmm.genNetworkDrive()
2.419 if new_ip != None:
2.420 - self.vmm.mapNetworkDrive(drive, '\\\\' + new_ip + '\\USB', None, None)
2.421 + net_resource = '\\\\' + new_ip + '\\USB'
2.422 + result = urllib2.urlopen('http://127.0.0.1:8090/netmount?'+'drive_letter='+drive+'&net_resource='+net_resource).readline()
2.423 +
2.424
2.425 if __name__ == '__main__':
2.426 #man = VMManager.getInstance()