kicked superfluous file
authorOliver Maurhart <oliver.maurhart@ait.ac.at>
Wed, 02 Apr 2014 12:32:53 +0200
changeset 118e072452916fe
parent 117 cbd6164c870f
child 119 8f1bb21bd395
child 120 52c1face04d3
kicked superfluous file
OpenSecurity/bin/launch.pyw
     1.1 --- a/OpenSecurity/bin/launch.pyw	Wed Apr 02 12:28:15 2014 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,257 +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, 2014 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 urllib2
    1.43 -
    1.44 -from PyQt4 import QtCore
    1.45 -from PyQt4 import QtGui
    1.46 -
    1.47 -# local
    1.48 -from about import About
    1.49 -from cygwin import Cygwin
    1.50 -from environment import Environment
    1.51 -
    1.52 -
    1.53 -# ------------------------------------------------------------
    1.54 -# code
    1.55 -
    1.56 -
    1.57 -class Chooser(QtGui.QDialog, object):
    1.58 -    
    1.59 -    """Ask the user what to launch."""
    1.60 -    
    1.61 -    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    1.62 -    
    1.63 -        super(Chooser, self).__init__(parent, flags)
    1.64 -        self.setWindowTitle('OpenSecuirty Launch Application')
    1.65 -        self.setup_ui()
    1.66 -        
    1.67 -        # positionate ourself central
    1.68 -        screen = QtGui.QDesktopWidget().screenGeometry()
    1.69 -        self.resize(self.geometry().width() * 1.25, self.geometry().height())
    1.70 -        size = self.geometry()
    1.71 -        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    1.72 -        
    1.73 -        # TODO: THIS HERE IS HARD CODED
    1.74 -        self._vms = [ { 'name': 'SecurityDVM0' } ]
    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 -        return str(self._cbVM.currentText())
   1.193 -        
   1.194 -    vm = property(vm_get)
   1.195 -        
   1.196 -        
   1.197 -def ask_user():
   1.198 -    """ask the user for VM and app to start"""
   1.199 -    
   1.200 -    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
   1.201 -    image_path = os.path.join(Environment("OpenSecurity").data_path, 'gfx')
   1.202 -    for file in os.listdir(image_path):
   1.203 -        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
   1.204 -            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
   1.205 -            
   1.206 -    # we should have now our application icon
   1.207 -    app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
   1.208 -    
   1.209 -    # pop up the dialog
   1.210 -    dlg = Chooser()
   1.211 -    dlg.show()
   1.212 -    app.exec_()
   1.213 -    
   1.214 -    if dlg.result() == QtGui.QDialog.Accepted:
   1.215 -        return dlg.vm, dlg.app
   1.216 -
   1.217 -    return '', ''
   1.218 -    
   1.219 -
   1.220 -def main():
   1.221 -    """entry point"""
   1.222 -    
   1.223 -    # launch Qt
   1.224 -    global app
   1.225 -    app = QtGui.QApplication(sys.argv)
   1.226 -    
   1.227 -    # parse command line
   1.228 -    parser = argparse.ArgumentParser(description = 'OpenSecurity Launcher: run application in VM')
   1.229 -    parser.add_argument('vm', metavar='VM', help='Name of Virtual Machine', nargs='?', type=str, default='')
   1.230 -    parser.add_argument('command', metavar='COMMAND', help='Full path of command and arguments to start inside VM', nargs='?', type=str, default='')
   1.231 -    args = parser.parse_args()
   1.232 -    
   1.233 -    # we must have all set
   1.234 -    if args.vm == "" or args.command == '':
   1.235 -        print('VM and/or COMMAND missing - invoking user dialog')
   1.236 -        args.vm, args.command = ask_user()
   1.237 -        
   1.238 -    # still no VM? --> no chance, over and out!
   1.239 -    if args.vm == '':
   1.240 -        sys.exit(0)
   1.241 -        
   1.242 -    # ensure we have our X11 running
   1.243 -    Cygwin.start_X11()
   1.244 -
   1.245 -    # call the OpenSecurity Admin to launch our progie =)
   1.246 -    # TODO: hard coded PORT
   1.247 -    url = 'http://127.0.0.1:8080/sdvms/' + args.vm + '/application' + args.command
   1.248 -    print('Calling ' + url)
   1.249 -    try:
   1.250 -        result = urllib2.urlopen(url, None, 5)
   1.251 -    except urllib2.HTTPError as e:
   1.252 -        # Error, Fail, ... :(
   1.253 -        msg = 'Error received from OpenSecurity Subsystem\nError code: ' + str(e.code) + '\nReason: ' + e.reason
   1.254 -        QtGui.QMessageBox.critical(None, 'OpenSecurity Error', msg)
   1.255 -    
   1.256 -    
   1.257 -# start
   1.258 -if __name__ == "__main__":
   1.259 -    main()
   1.260 -