OpenSecurity/bin/launch.pyw
author Oliver Maurhart <oliver.maurhart@ait.ac.at>
Tue, 04 Feb 2014 13:36:06 +0100
changeset 58 1e1d8ca35988
parent 52 1238895dc6b6
child 70 e1db93adbebf
permissions -rwxr-xr-x
removed superfluous sys.exit(0) in launch.pyw
om@13
     1
#!/bin/env python
om@13
     2
# -*- coding: utf-8 -*-
om@13
     3
om@13
     4
# ------------------------------------------------------------
om@13
     5
# opensecurity-launcher
om@13
     6
# 
om@13
     7
# launches an application inside a VM
om@13
     8
#
om@13
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@13
    10
#
om@13
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@13
    12
# AIT Austrian Institute of Technology GmbH
om@13
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@13
    14
# http://www.ait.ac.at
om@13
    15
#
om@13
    16
# This program is free software; you can redistribute it and/or
om@13
    17
# modify it under the terms of the GNU General Public License
om@13
    18
# as published by the Free Software Foundation version 2.
om@13
    19
# 
om@13
    20
# This program is distributed in the hope that it will be useful,
om@13
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@13
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@13
    23
# GNU General Public License for more details.
om@13
    24
# 
om@13
    25
# You should have received a copy of the GNU General Public License
om@13
    26
# along with this program; if not, write to the Free Software
om@13
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@13
    28
# Boston, MA  02110-1301, USA.
om@13
    29
# ------------------------------------------------------------
om@13
    30
om@13
    31
om@13
    32
# ------------------------------------------------------------
om@13
    33
# imports
om@13
    34
om@13
    35
import argparse
om@13
    36
import os
om@13
    37
import subprocess
om@13
    38
import sys
om@31
    39
import urllib2
om@13
    40
om@13
    41
from PyQt4 import QtCore
om@13
    42
from PyQt4 import QtGui
om@13
    43
om@13
    44
# local
om@13
    45
from about import About
om@13
    46
from cygwin import Cygwin
om@13
    47
from environment import Environment
om@13
    48
om@13
    49
om@13
    50
# ------------------------------------------------------------
om@13
    51
# code
om@13
    52
om@13
    53
om@13
    54
class Chooser(QtGui.QDialog, object):
om@13
    55
    
om@13
    56
    """Ask the user what to launch."""
om@13
    57
    
om@13
    58
    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
om@13
    59
    
om@13
    60
        super(Chooser, self).__init__(parent, flags)
om@13
    61
        self.setWindowTitle('OpenSecuirty Launch Application')
om@13
    62
        self.setup_ui()
om@13
    63
        
om@13
    64
        # positionate ourself central
om@13
    65
        screen = QtGui.QDesktopWidget().screenGeometry()
om@13
    66
        self.resize(self.geometry().width() * 1.25, self.geometry().height())
om@13
    67
        size = self.geometry()
om@13
    68
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
om@13
    69
        
om@42
    70
        # TODO: THIS HERE IS HARD CODED
om@42
    71
        self._vms = [ { 'name': 'SecurityDVM0' } ]
om@31
    72
        self._apps = [ { 'name': 'Browser', 'command': '/usr/bin/iceweasel' } ]
om@31
    73
        
om@31
    74
        # add the VMs we know
om@31
    75
        self._cbVM.clear()
om@31
    76
        for vm in self._vms:
om@31
    77
            self._cbVM.addItem(vm['name'])
om@31
    78
            
om@31
    79
        # add the commands we know
om@31
    80
        self._cbApplication.clear()
om@31
    81
        for app in self._apps:
om@31
    82
            self._cbApplication.addItem(app['name'])
om@31
    83
        
om@13
    84
        
om@13
    85
        
om@13
    86
    def app_get(self):
om@13
    87
        """The application of the user."""
om@13
    88
        a = str(self._cbApplication.currentText())
om@13
    89
        for app in self._apps:
om@13
    90
            if a == app['name']:
om@13
    91
                return app['command']
om@13
    92
        return a
om@13
    93
        
om@13
    94
    app = property(app_get)
om@13
    95
        
om@13
    96
om@13
    97
    def clicked_about(self):
om@13
    98
        """clicked the about button"""
om@13
    99
        dlgAbout = About()
om@13
   100
        dlgAbout.exec_()
om@13
   101
om@13
   102
om@13
   103
    def clicked_cancel(self):
om@13
   104
        """clicked the cancel button"""
om@13
   105
        self.reject()
om@13
   106
    
om@13
   107
om@13
   108
    def clicked_ok(self):
om@13
   109
        """clicked the ok button"""
om@13
   110
        self.accept()
om@13
   111
    
om@13
   112
    
om@13
   113
    def setup_ui(self):
om@13
   114
        """Create the widgets."""
om@13
   115
        
om@13
   116
        lyMain = QtGui.QVBoxLayout(self)
om@13
   117
        lyMain.setContentsMargins(8, 8, 8, 8)
om@13
   118
        
om@13
   119
        # content area: left pixmap, right text
om@13
   120
        lyContent = QtGui.QHBoxLayout()
om@13
   121
        lyMain.addLayout(lyContent)
om@13
   122
        
om@13
   123
        # pixmap
om@13
   124
        lbPix = QtGui.QLabel()
om@13
   125
        lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
om@13
   126
        lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
om@13
   127
        lyContent.addSpacing(16)
om@13
   128
        
om@13
   129
        # launch ...
om@13
   130
        lyLaunch = QtGui.QGridLayout()
om@13
   131
        lyContent.addLayout(lyLaunch)
om@13
   132
        lbTitle = QtGui.QLabel('Specify details for application to launch.')
om@13
   133
        lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
om@13
   134
        
om@13
   135
        lbVM = QtGui.QLabel('&VM-ID:')
om@13
   136
        lyLaunch.addWidget(lbVM, 1, 0)
om@13
   137
        self._cbVM = QtGui.QComboBox()
om@13
   138
        self._cbVM.setEditable(True)
om@13
   139
        self._cbVM.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
om@13
   140
        lyLaunch.addWidget(self._cbVM, 1, 1)
om@13
   141
        lbVM.setBuddy(self._cbVM)
om@13
   142
        
om@13
   143
        lbApplication = QtGui.QLabel('&Application:')
om@13
   144
        lyLaunch.addWidget(lbApplication, 2, 0)
om@13
   145
        self._cbApplication = QtGui.QComboBox()
om@13
   146
        self._cbApplication.setEditable(True)
om@13
   147
        self._cbApplication.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
om@13
   148
        lyLaunch.addWidget(self._cbApplication, 2, 1)
om@13
   149
        lbApplication.setBuddy(self._cbApplication)
om@13
   150
        
om@13
   151
        lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
om@13
   152
        lyLaunch.setColumnStretch(1, 1)
om@13
   153
        lyLaunch.setRowStretch(3, 1)
om@13
   154
        
om@13
   155
        lyMain.addStretch(1)
om@13
   156
        
om@13
   157
        # buttons
om@13
   158
        lyButton = QtGui.QHBoxLayout()
om@13
   159
        lyMain.addLayout(lyButton)
om@13
   160
        
om@13
   161
        lyButton.addStretch(1)
om@13
   162
        btnOk = QtGui.QPushButton('&Ok', self)
om@13
   163
        btnOk.setDefault(True)
om@13
   164
        btnOk.setMinimumWidth(100)
om@13
   165
        lyButton.addWidget(btnOk)
om@13
   166
        btnCancel = QtGui.QPushButton('&Cancel', self)
om@13
   167
        btnCancel.setMinimumWidth(100)
om@13
   168
        lyButton.addWidget(btnCancel)
om@13
   169
        btnAbout = QtGui.QPushButton('&About', self)
om@13
   170
        btnAbout.setMinimumWidth(100)
om@13
   171
        lyButton.addWidget(btnAbout)
om@13
   172
        
om@13
   173
        button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
om@13
   174
        btnOk.setMinimumWidth(button_width)
om@13
   175
        btnCancel.setMinimumWidth(button_width)
om@13
   176
        btnAbout.setMinimumWidth(button_width)
om@13
   177
        
om@13
   178
        # reduce to the max
om@13
   179
        self.resize(lyMain.minimumSize())
om@13
   180
        
om@13
   181
        # connectors
om@13
   182
        btnOk.clicked.connect(self.clicked_ok)
om@13
   183
        btnCancel.clicked.connect(self.clicked_cancel)
om@13
   184
        btnAbout.clicked.connect(self.clicked_about)
om@13
   185
om@13
   186
        
om@13
   187
    def vm_get(self):
om@13
   188
        """The vm of choice."""
om@42
   189
        return str(self._cbVM.currentText())
om@13
   190
        
om@13
   191
    vm = property(vm_get)
om@13
   192
        
om@13
   193
        
om@13
   194
def ask_user():
om@13
   195
    """ask the user for VM and app to start"""
om@13
   196
    
om@13
   197
    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
om@13
   198
    image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
om@13
   199
    for file in os.listdir(image_path):
om@13
   200
        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
om@13
   201
            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
om@13
   202
            
om@13
   203
    # we should have now our application icon
om@13
   204
    app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
om@13
   205
    
om@13
   206
    # pop up the dialog
om@13
   207
    dlg = Chooser()
om@13
   208
    dlg.show()
om@13
   209
    app.exec_()
om@13
   210
    
om@13
   211
    if dlg.result() == QtGui.QDialog.Accepted:
om@31
   212
        return dlg.vm, dlg.app
om@13
   213
om@31
   214
    return '', ''
om@13
   215
    
om@13
   216
om@13
   217
def main():
om@13
   218
    """entry point"""
om@13
   219
    
om@37
   220
    # launch Qt
om@37
   221
    global app
om@37
   222
    app = QtGui.QApplication(sys.argv)
om@37
   223
    
om@13
   224
    # parse command line
om@13
   225
    parser = argparse.ArgumentParser(description = 'OpenSecurity Launcher: run application in VM')
om@42
   226
    parser.add_argument('vm', metavar='VM', help='Name of Virtual Machine', nargs='?', type=str, default='')
om@13
   227
    parser.add_argument('command', metavar='COMMAND', help='Full path of command and arguments to start inside VM', nargs='?', type=str, default='')
om@13
   228
    args = parser.parse_args()
om@13
   229
    
om@42
   230
    # we must have all set
om@42
   231
    if args.vm == "" or args.command == '':
om@42
   232
        print('VM and/or COMMAND missing - invoking user dialog')
om@42
   233
        args.vm, args.command = ask_user()
om@13
   234
        
om@42
   235
    # still no VM? --> no chance, over and out!
om@42
   236
    if args.vm == '':
om@13
   237
        sys.exit(0)
om@13
   238
        
om@13
   239
    # ensure we have our X11 running
om@37
   240
    Cygwin.start_X11()
dyle@52
   241
   
om@31
   242
    # call the OpenSecurity Admin to launch our progie =)
om@42
   243
    # TODO: hard coded PORT
om@42
   244
    url = 'http://127.0.0.1:8080/sdvms/' + args.vm + '/application' + args.command
om@42
   245
    print('Calling ' + url)
om@42
   246
    try:
om@42
   247
        result = urllib2.urlopen(url, None, 5)
om@42
   248
    except urllib2.HTTPError as e:
om@42
   249
        # Error, Fail, ... :(
om@42
   250
        msg = 'Error received from OpenSecurity Subsystem\nError code: ' + str(e.code) + '\nReason: ' + e.reason
om@42
   251
        QtGui.QMessageBox.critical(None, 'OpenSecurity Error', msg)
om@13
   252
    
om@13
   253
    
om@13
   254
# start
om@13
   255
if __name__ == "__main__":
om@13
   256
    main()
om@13
   257