ait/os/bin/opensecurityd/launch.py
author om
Tue, 12 Nov 2013 11:31:34 +0100
branchom
changeset 2 c9bf2537109a
permissions -rwxr-xr-x
added C/C++ and Python sources
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # opensecurity-launcher
     6 # 
     7 # launches an application inside a VM
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    11 # Copyright (C) 2013 AIT Austrian Institute of Technology
    12 # AIT Austrian Institute of Technology GmbH
    13 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    14 # http://www.ait.ac.at
    15 #
    16 # This program is free software; you can redistribute it and/or
    17 # modify it under the terms of the GNU General Public License
    18 # as published by the Free Software Foundation version 2.
    19 # 
    20 # This program is distributed in the hope that it will be useful,
    21 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    22 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    23 # GNU General Public License for more details.
    24 # 
    25 # You should have received a copy of the GNU General Public License
    26 # along with this program; if not, write to the Free Software
    27 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    28 # Boston, MA  02110-1301, USA.
    29 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import argparse
    36 import os
    37 import subprocess
    38 import sys
    39 import urllib
    40 
    41 from PyQt4 import QtCore
    42 from PyQt4 import QtGui
    43 
    44 # local
    45 from about import About
    46 from environment import Environment
    47 
    48 
    49 # ------------------------------------------------------------
    50 # code
    51 
    52 
    53 class Chooser(QtGui.QDialog):
    54     
    55     """Ask the user what to launch."""
    56     
    57     def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
    58         
    59         super(Chooser, self).__init__(parent, flags)
    60         self.setWindowTitle('OpenSecuirty Launch Application')
    61         self.setup_ui()
    62         
    63         # positionate ourself central
    64         screen = QtGui.QDesktopWidget().screenGeometry()
    65         self.resize(self.geometry().width() * 1.25, self.geometry().height())
    66         size = self.geometry()
    67         self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
    68         
    69 
    70     def clicked_about(self):
    71         """clicked the about button"""
    72         dlgAbout = About()
    73         dlgAbout.exec_()
    74     
    75 
    76     def clicked_cancel(self):
    77         """clicked the cancel button"""
    78         self.reject()
    79     
    80 
    81     def clicked_ok(self):
    82         """clicked the ok button"""
    83         self.accept()
    84     
    85 
    86     def setup_ui(self):
    87         
    88         """Create the widgets."""
    89         
    90         lyMain = QtGui.QVBoxLayout(self)
    91         lyMain.setContentsMargins(8, 8, 8, 8)
    92         
    93         # content area: left pixmap, right text
    94         lyContent = QtGui.QHBoxLayout()
    95         lyMain.addLayout(lyContent)
    96         
    97         # pixmap
    98         lbPix = QtGui.QLabel()
    99         lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
   100         lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
   101         lyContent.addSpacing(16)
   102         
   103         # launch ...
   104         lyLaunch = QtGui.QGridLayout()
   105         lyContent.addLayout(lyLaunch)
   106         lbTitle = QtGui.QLabel('Specify details for application to launch.')
   107         lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
   108         
   109         lbVM = QtGui.QLabel('&VM-ID:')
   110         lyLaunch.addWidget(lbVM, 1, 0)
   111         self.edVM = QtGui.QLineEdit()
   112         lyLaunch.addWidget(self.edVM, 1, 1)
   113         lbVM.setBuddy(self.edVM)
   114         
   115         # TODO: HARD CODED!
   116         self.edVM.setText('Debian 7')
   117         
   118         lbApplication = QtGui.QLabel('&Application:')
   119         lyLaunch.addWidget(lbApplication, 2, 0)
   120         self.cbApplication = QtGui.QComboBox()
   121         self.cbApplication.setEditable(True)
   122         lyLaunch.addWidget(self.cbApplication, 2, 1)
   123         lbApplication.setBuddy(self.cbApplication)
   124         
   125         # TODO: HARD CODED!
   126         self.cbApplication.addItem('iceweasel')
   127         self.cbApplication.addItem('vlc')
   128         self.cbApplication.addItem('xfce4-terminal')
   129         
   130         lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
   131         lyLaunch.setColumnStretch(1, 1)
   132         lyLaunch.setRowStretch(3, 1)
   133         
   134         lyMain.addStretch(1)
   135         
   136         # buttons
   137         lyButton = QtGui.QHBoxLayout()
   138         lyMain.addLayout(lyButton)
   139         
   140         lyButton.addStretch(1)
   141         btnOk = QtGui.QPushButton('&Ok', self)
   142         btnOk.setDefault(True)
   143         btnOk.setMinimumWidth(100)
   144         lyButton.addWidget(btnOk)
   145         btnCancel = QtGui.QPushButton('&Cancel', self)
   146         btnCancel.setMinimumWidth(100)
   147         lyButton.addWidget(btnCancel)
   148         btnAbout = QtGui.QPushButton('&About', self)
   149         btnAbout.setMinimumWidth(100)
   150         lyButton.addWidget(btnAbout)
   151         
   152         button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
   153         btnOk.setMinimumWidth(button_width)
   154         btnCancel.setMinimumWidth(button_width)
   155         btnAbout.setMinimumWidth(button_width)
   156         
   157         # reduce to the max
   158         self.resize(lyMain.minimumSize())
   159         
   160         # connectors
   161         btnOk.clicked.connect(self.clicked_ok)
   162         btnCancel.clicked.connect(self.clicked_cancel)
   163         btnAbout.clicked.connect(self.clicked_about)
   164 
   165 
   166 def main():
   167     
   168     # parse command line
   169     app = QtGui.QApplication(sys.argv)
   170     
   171     # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
   172     data_path = Environment("opensecurity").image_path
   173     for file in os.listdir(data_path):
   174         if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
   175             QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(data_path, file)))
   176             
   177     # we should have now our application icon
   178     app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
   179     
   180     dlg = Chooser()
   181 
   182     # pop up the dialog
   183     dlg.show()
   184     app.exec_()
   185     
   186     if dlg.result() == QtGui.QDialog.Accepted:
   187         # encode an proper GET request to the opensecurity daemon
   188         get_vm = urllib.quote(str(dlg.edVM.text()))
   189         get_app = urllib.quote(str(dlg.cbApplication.currentText()))
   190         osd_request = 'http://127.0.0.1:8080/application?vm={0}&app={1}'.format(get_vm, get_app)
   191         urllib.urlopen(osd_request)
   192         res = 0
   193     else:
   194         res = 1
   195         
   196     sys.exit(res)
   197     
   198 # start
   199 if __name__ == "__main__":
   200     main()
   201