OpenSecurity/bin/launch.py
author om
Tue, 10 Dec 2013 14:04:11 +0100
changeset 31 d95fe93d7a83
parent 16 e16d64b5e008
permissions -rwxr-xr-x
opensecurityd can now invoke applications on vm
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 urllib
om@31
    40
import urllib2
om@13
    41
om@13
    42
from PyQt4 import QtCore
om@13
    43
from PyQt4 import QtGui
om@13
    44
om@13
    45
# local
om@13
    46
from about import About
om@13
    47
from cygwin import Cygwin
om@13
    48
from environment import Environment
om@13
    49
om@13
    50
om@13
    51
# ------------------------------------------------------------
om@13
    52
# code
om@13
    53
om@13
    54
om@13
    55
class Chooser(QtGui.QDialog, object):
om@13
    56
    
om@13
    57
    """Ask the user what to launch."""
om@13
    58
    
om@13
    59
    def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
om@13
    60
    
om@13
    61
        super(Chooser, self).__init__(parent, flags)
om@13
    62
        self.setWindowTitle('OpenSecuirty Launch Application')
om@13
    63
        self.setup_ui()
om@13
    64
        
om@13
    65
        # positionate ourself central
om@13
    66
        screen = QtGui.QDesktopWidget().screenGeometry()
om@13
    67
        self.resize(self.geometry().width() * 1.25, self.geometry().height())
om@13
    68
        size = self.geometry()
om@13
    69
        self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
om@13
    70
        
om@31
    71
        self._vms = [ { 'name': 'SecurityDVM0', 'ip': '192.168.56.101' } ]
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@13
   189
        v = str(self._cbVM.currentText())
om@13
   190
        for vm in self._vms:
om@13
   191
            if v == vm['name']:
om@13
   192
                return vm['ip']
om@13
   193
        return v
om@13
   194
        
om@13
   195
    vm = property(vm_get)
om@13
   196
        
om@13
   197
        
om@13
   198
def ask_user():
om@13
   199
    """ask the user for VM and app to start"""
om@13
   200
    
om@13
   201
    # launch Qt
om@13
   202
    app = QtGui.QApplication(sys.argv)
om@13
   203
    
om@13
   204
    # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
om@13
   205
    image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
om@13
   206
    for file in os.listdir(image_path):
om@13
   207
        if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
om@13
   208
            QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
om@13
   209
            
om@13
   210
    # we should have now our application icon
om@13
   211
    app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
om@13
   212
    
om@13
   213
    # pop up the dialog
om@13
   214
    dlg = Chooser()
om@13
   215
    dlg.show()
om@13
   216
    app.exec_()
om@13
   217
    
om@13
   218
    if dlg.result() == QtGui.QDialog.Accepted:
om@31
   219
        return dlg.vm, dlg.app
om@13
   220
om@31
   221
    return '', ''
om@13
   222
    
om@13
   223
om@13
   224
def main():
om@13
   225
    """entry point"""
om@13
   226
    
om@13
   227
    # parse command line
om@13
   228
    parser = argparse.ArgumentParser(description = 'OpenSecurity Launcher: run application in VM')
om@13
   229
    parser.add_argument('ip', metavar='IP', help='IP of Virtual Machine', nargs='?', type=str, default='')
om@13
   230
    parser.add_argument('command', metavar='COMMAND', help='Full path of command and arguments to start inside VM', nargs='?', type=str, default='')
om@13
   231
    args = parser.parse_args()
om@13
   232
    
om@13
   233
    # we must have at least all or none set
om@13
   234
    set_ip = args.ip != ''
om@13
   235
    set_command = args.command != ''
om@31
   236
    set_ALL = set_ip and set_command
om@31
   237
    set_NONE = (not set_ip) and (not set_command)
om@13
   238
    if (not set_ALL) and (not set_NONE):
om@31
   239
        sys.stderr.write("Please specify ip and command or none.\n")
om@13
   240
        sys.stderr.write("Type '--help' for help.\n")
om@13
   241
        sys.exit(1)
om@13
   242
        
om@13
   243
    # check if we need to ask the user
om@13
   244
    if set_NONE:
om@31
   245
        args.ip, args.command = ask_user()
om@13
   246
        
om@13
   247
    # still no IP? --> no chance, over and out!
om@13
   248
    if args.ip == '':
om@13
   249
        sys.exit(0)
om@13
   250
        
om@13
   251
    # ensure we have our X11 running
om@31
   252
    #Cygwin.start_X11()
om@13
   253
    
om@31
   254
    # call the OpenSecurity Admin to launch our progie =)
om@31
   255
    url_vm = urllib.quote(args.ip)
om@31
   256
    url_command = urllib.quote(args.command)
om@31
   257
    print(url_vm)
om@31
   258
    print(url_command)
om@13
   259
    
om@31
   260
    # user_at_guest = args.user + '@' + args.ip
om@31
   261
    # ssh = 'DISPLAY=:0 /usr/bin/ssh -Y ' + user_at_guest + ' ' + args.command
om@31
   262
    # print(ssh)
om@31
   263
    
om@31
   264
    # # off we go!
om@31
   265
    # Cygwin()(['/bin/bash', '--login', '-i', '-c', ssh], None, None, None)
om@13
   266
om@13
   267
    
om@13
   268
# start
om@13
   269
if __name__ == "__main__":
om@13
   270
    main()
om@13
   271