1.1 --- a/OpenSecurity/bin/launch.py Tue Dec 10 16:02:24 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,271 +0,0 @@
1.4 -#!/bin/env python
1.5 -# -*- coding: utf-8 -*-
1.6 -
1.7 -# ------------------------------------------------------------
1.8 -# opensecurity-launcher
1.9 -#
1.10 -# launches an application inside a VM
1.11 -#
1.12 -# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
1.13 -#
1.14 -# Copyright (C) 2013 AIT Austrian Institute of Technology
1.15 -# AIT Austrian Institute of Technology GmbH
1.16 -# Donau-City-Strasse 1 | 1220 Vienna | Austria
1.17 -# http://www.ait.ac.at
1.18 -#
1.19 -# This program is free software; you can redistribute it and/or
1.20 -# modify it under the terms of the GNU General Public License
1.21 -# as published by the Free Software Foundation version 2.
1.22 -#
1.23 -# This program is distributed in the hope that it will be useful,
1.24 -# but WITHOUT ANY WARRANTY; without even the implied warranty of
1.25 -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.26 -# GNU General Public License for more details.
1.27 -#
1.28 -# You should have received a copy of the GNU General Public License
1.29 -# along with this program; if not, write to the Free Software
1.30 -# Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.31 -# Boston, MA 02110-1301, USA.
1.32 -# ------------------------------------------------------------
1.33 -
1.34 -
1.35 -# ------------------------------------------------------------
1.36 -# imports
1.37 -
1.38 -import argparse
1.39 -import os
1.40 -import subprocess
1.41 -import sys
1.42 -import urllib
1.43 -import urllib2
1.44 -
1.45 -from PyQt4 import QtCore
1.46 -from PyQt4 import QtGui
1.47 -
1.48 -# local
1.49 -from about import About
1.50 -from cygwin import Cygwin
1.51 -from environment import Environment
1.52 -
1.53 -
1.54 -# ------------------------------------------------------------
1.55 -# code
1.56 -
1.57 -
1.58 -class Chooser(QtGui.QDialog, object):
1.59 -
1.60 - """Ask the user what to launch."""
1.61 -
1.62 - def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
1.63 -
1.64 - super(Chooser, self).__init__(parent, flags)
1.65 - self.setWindowTitle('OpenSecuirty Launch Application')
1.66 - self.setup_ui()
1.67 -
1.68 - # positionate ourself central
1.69 - screen = QtGui.QDesktopWidget().screenGeometry()
1.70 - self.resize(self.geometry().width() * 1.25, self.geometry().height())
1.71 - size = self.geometry()
1.72 - self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
1.73 -
1.74 - self._vms = [ { 'name': 'SecurityDVM0', 'ip': '192.168.56.101' } ]
1.75 - self._apps = [ { 'name': 'Browser', 'command': '/usr/bin/iceweasel' } ]
1.76 -
1.77 - # add the VMs we know
1.78 - self._cbVM.clear()
1.79 - for vm in self._vms:
1.80 - self._cbVM.addItem(vm['name'])
1.81 -
1.82 - # add the commands we know
1.83 - self._cbApplication.clear()
1.84 - for app in self._apps:
1.85 - self._cbApplication.addItem(app['name'])
1.86 -
1.87 -
1.88 -
1.89 - def app_get(self):
1.90 - """The application of the user."""
1.91 - a = str(self._cbApplication.currentText())
1.92 - for app in self._apps:
1.93 - if a == app['name']:
1.94 - return app['command']
1.95 - return a
1.96 -
1.97 - app = property(app_get)
1.98 -
1.99 -
1.100 - def clicked_about(self):
1.101 - """clicked the about button"""
1.102 - dlgAbout = About()
1.103 - dlgAbout.exec_()
1.104 -
1.105 -
1.106 - def clicked_cancel(self):
1.107 - """clicked the cancel button"""
1.108 - self.reject()
1.109 -
1.110 -
1.111 - def clicked_ok(self):
1.112 - """clicked the ok button"""
1.113 - self.accept()
1.114 -
1.115 -
1.116 - def setup_ui(self):
1.117 - """Create the widgets."""
1.118 -
1.119 - lyMain = QtGui.QVBoxLayout(self)
1.120 - lyMain.setContentsMargins(8, 8, 8, 8)
1.121 -
1.122 - # content area: left pixmap, right text
1.123 - lyContent = QtGui.QHBoxLayout()
1.124 - lyMain.addLayout(lyContent)
1.125 -
1.126 - # pixmap
1.127 - lbPix = QtGui.QLabel()
1.128 - lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
1.129 - lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
1.130 - lyContent.addSpacing(16)
1.131 -
1.132 - # launch ...
1.133 - lyLaunch = QtGui.QGridLayout()
1.134 - lyContent.addLayout(lyLaunch)
1.135 - lbTitle = QtGui.QLabel('Specify details for application to launch.')
1.136 - lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
1.137 -
1.138 - lbVM = QtGui.QLabel('&VM-ID:')
1.139 - lyLaunch.addWidget(lbVM, 1, 0)
1.140 - self._cbVM = QtGui.QComboBox()
1.141 - self._cbVM.setEditable(True)
1.142 - self._cbVM.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
1.143 - lyLaunch.addWidget(self._cbVM, 1, 1)
1.144 - lbVM.setBuddy(self._cbVM)
1.145 -
1.146 - lbApplication = QtGui.QLabel('&Application:')
1.147 - lyLaunch.addWidget(lbApplication, 2, 0)
1.148 - self._cbApplication = QtGui.QComboBox()
1.149 - self._cbApplication.setEditable(True)
1.150 - self._cbApplication.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
1.151 - lyLaunch.addWidget(self._cbApplication, 2, 1)
1.152 - lbApplication.setBuddy(self._cbApplication)
1.153 -
1.154 - lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
1.155 - lyLaunch.setColumnStretch(1, 1)
1.156 - lyLaunch.setRowStretch(3, 1)
1.157 -
1.158 - lyMain.addStretch(1)
1.159 -
1.160 - # buttons
1.161 - lyButton = QtGui.QHBoxLayout()
1.162 - lyMain.addLayout(lyButton)
1.163 -
1.164 - lyButton.addStretch(1)
1.165 - btnOk = QtGui.QPushButton('&Ok', self)
1.166 - btnOk.setDefault(True)
1.167 - btnOk.setMinimumWidth(100)
1.168 - lyButton.addWidget(btnOk)
1.169 - btnCancel = QtGui.QPushButton('&Cancel', self)
1.170 - btnCancel.setMinimumWidth(100)
1.171 - lyButton.addWidget(btnCancel)
1.172 - btnAbout = QtGui.QPushButton('&About', self)
1.173 - btnAbout.setMinimumWidth(100)
1.174 - lyButton.addWidget(btnAbout)
1.175 -
1.176 - button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
1.177 - btnOk.setMinimumWidth(button_width)
1.178 - btnCancel.setMinimumWidth(button_width)
1.179 - btnAbout.setMinimumWidth(button_width)
1.180 -
1.181 - # reduce to the max
1.182 - self.resize(lyMain.minimumSize())
1.183 -
1.184 - # connectors
1.185 - btnOk.clicked.connect(self.clicked_ok)
1.186 - btnCancel.clicked.connect(self.clicked_cancel)
1.187 - btnAbout.clicked.connect(self.clicked_about)
1.188 -
1.189 -
1.190 - def vm_get(self):
1.191 - """The vm of choice."""
1.192 - v = str(self._cbVM.currentText())
1.193 - for vm in self._vms:
1.194 - if v == vm['name']:
1.195 - return vm['ip']
1.196 - return v
1.197 -
1.198 - vm = property(vm_get)
1.199 -
1.200 -
1.201 -def ask_user():
1.202 - """ask the user for VM and app to start"""
1.203 -
1.204 - # launch Qt
1.205 - app = QtGui.QApplication(sys.argv)
1.206 -
1.207 - # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
1.208 - image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
1.209 - for file in os.listdir(image_path):
1.210 - if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
1.211 - QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
1.212 -
1.213 - # we should have now our application icon
1.214 - app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
1.215 -
1.216 - # pop up the dialog
1.217 - dlg = Chooser()
1.218 - dlg.show()
1.219 - app.exec_()
1.220 -
1.221 - if dlg.result() == QtGui.QDialog.Accepted:
1.222 - return dlg.vm, dlg.app
1.223 -
1.224 - return '', ''
1.225 -
1.226 -
1.227 -def main():
1.228 - """entry point"""
1.229 -
1.230 - # parse command line
1.231 - parser = argparse.ArgumentParser(description = 'OpenSecurity Launcher: run application in VM')
1.232 - parser.add_argument('ip', metavar='IP', help='IP of Virtual Machine', nargs='?', type=str, default='')
1.233 - parser.add_argument('command', metavar='COMMAND', help='Full path of command and arguments to start inside VM', nargs='?', type=str, default='')
1.234 - args = parser.parse_args()
1.235 -
1.236 - # we must have at least all or none set
1.237 - set_ip = args.ip != ''
1.238 - set_command = args.command != ''
1.239 - set_ALL = set_ip and set_command
1.240 - set_NONE = (not set_ip) and (not set_command)
1.241 - if (not set_ALL) and (not set_NONE):
1.242 - sys.stderr.write("Please specify ip and command or none.\n")
1.243 - sys.stderr.write("Type '--help' for help.\n")
1.244 - sys.exit(1)
1.245 -
1.246 - # check if we need to ask the user
1.247 - if set_NONE:
1.248 - args.ip, args.command = ask_user()
1.249 -
1.250 - # still no IP? --> no chance, over and out!
1.251 - if args.ip == '':
1.252 - sys.exit(0)
1.253 -
1.254 - # ensure we have our X11 running
1.255 - #Cygwin.start_X11()
1.256 -
1.257 - # call the OpenSecurity Admin to launch our progie =)
1.258 - url_vm = urllib.quote(args.ip)
1.259 - url_command = urllib.quote(args.command)
1.260 - print(url_vm)
1.261 - print(url_command)
1.262 -
1.263 - # user_at_guest = args.user + '@' + args.ip
1.264 - # ssh = 'DISPLAY=:0 /usr/bin/ssh -Y ' + user_at_guest + ' ' + args.command
1.265 - # print(ssh)
1.266 -
1.267 - # # off we go!
1.268 - # Cygwin()(['/bin/bash', '--login', '-i', '-c', ssh], None, None, None)
1.269 -
1.270 -
1.271 -# start
1.272 -if __name__ == "__main__":
1.273 - main()
1.274 -