om@2: #!/bin/env python om@2: # -*- coding: utf-8 -*- om@2: om@2: # ------------------------------------------------------------ om@2: # opensecurity-launcher om@2: # om@2: # launches an application inside a VM om@2: # om@2: # Autor: Oliver Maurhart, om@2: # om@2: # Copyright (C) 2013 AIT Austrian Institute of Technology om@2: # AIT Austrian Institute of Technology GmbH om@2: # Donau-City-Strasse 1 | 1220 Vienna | Austria om@2: # http://www.ait.ac.at om@2: # om@2: # This program is free software; you can redistribute it and/or om@2: # modify it under the terms of the GNU General Public License om@2: # as published by the Free Software Foundation version 2. om@2: # om@2: # This program is distributed in the hope that it will be useful, om@2: # but WITHOUT ANY WARRANTY; without even the implied warranty of om@2: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the om@2: # GNU General Public License for more details. om@2: # om@2: # You should have received a copy of the GNU General Public License om@2: # along with this program; if not, write to the Free Software om@2: # Foundation, Inc., 51 Franklin Street, Fifth Floor, om@2: # Boston, MA 02110-1301, USA. om@2: # ------------------------------------------------------------ om@2: om@2: om@2: # ------------------------------------------------------------ om@2: # imports om@2: om@2: import argparse om@2: import os om@2: import subprocess om@2: import sys om@2: import urllib om@2: om@2: from PyQt4 import QtCore om@2: from PyQt4 import QtGui om@2: om@2: # local om@2: from about import About om@2: from environment import Environment om@2: om@2: om@2: # ------------------------------------------------------------ om@2: # code om@2: om@2: om@2: class Chooser(QtGui.QDialog): om@2: om@2: """Ask the user what to launch.""" om@2: om@2: def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)): om@2: om@2: super(Chooser, self).__init__(parent, flags) om@2: self.setWindowTitle('OpenSecuirty Launch Application') om@2: self.setup_ui() om@2: om@2: # positionate ourself central om@2: screen = QtGui.QDesktopWidget().screenGeometry() om@2: self.resize(self.geometry().width() * 1.25, self.geometry().height()) om@2: size = self.geometry() om@2: self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2) om@2: om@2: om@2: def clicked_about(self): om@2: """clicked the about button""" om@2: dlgAbout = About() om@2: dlgAbout.exec_() om@2: om@2: om@2: def clicked_cancel(self): om@2: """clicked the cancel button""" om@2: self.reject() om@2: om@2: om@2: def clicked_ok(self): om@2: """clicked the ok button""" om@2: self.accept() om@2: om@2: om@2: def setup_ui(self): om@2: om@2: """Create the widgets.""" om@2: om@2: lyMain = QtGui.QVBoxLayout(self) om@2: lyMain.setContentsMargins(8, 8, 8, 8) om@2: om@2: # content area: left pixmap, right text om@2: lyContent = QtGui.QHBoxLayout() om@2: lyMain.addLayout(lyContent) om@2: om@2: # pixmap om@2: lbPix = QtGui.QLabel() om@2: lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64')) om@2: lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter)) om@2: lyContent.addSpacing(16) om@2: om@2: # launch ... om@2: lyLaunch = QtGui.QGridLayout() om@2: lyContent.addLayout(lyLaunch) om@2: lbTitle = QtGui.QLabel('Specify details for application to launch.') om@2: lyLaunch.addWidget(lbTitle, 0, 0, 1, 2) om@2: om@2: lbVM = QtGui.QLabel('&VM-ID:') om@2: lyLaunch.addWidget(lbVM, 1, 0) om@2: self.edVM = QtGui.QLineEdit() om@2: lyLaunch.addWidget(self.edVM, 1, 1) om@2: lbVM.setBuddy(self.edVM) om@2: om@2: # TODO: HARD CODED! om@2: self.edVM.setText('Debian 7') om@2: om@2: lbApplication = QtGui.QLabel('&Application:') om@2: lyLaunch.addWidget(lbApplication, 2, 0) om@2: self.cbApplication = QtGui.QComboBox() om@2: self.cbApplication.setEditable(True) om@2: lyLaunch.addWidget(self.cbApplication, 2, 1) om@2: lbApplication.setBuddy(self.cbApplication) om@2: om@2: # TODO: HARD CODED! om@2: self.cbApplication.addItem('iceweasel') om@2: self.cbApplication.addItem('vlc') om@2: self.cbApplication.addItem('xfce4-terminal') om@2: om@2: lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2) om@2: lyLaunch.setColumnStretch(1, 1) om@2: lyLaunch.setRowStretch(3, 1) om@2: om@2: lyMain.addStretch(1) om@2: om@2: # buttons om@2: lyButton = QtGui.QHBoxLayout() om@2: lyMain.addLayout(lyButton) om@2: om@2: lyButton.addStretch(1) om@2: btnOk = QtGui.QPushButton('&Ok', self) om@2: btnOk.setDefault(True) om@2: btnOk.setMinimumWidth(100) om@2: lyButton.addWidget(btnOk) om@2: btnCancel = QtGui.QPushButton('&Cancel', self) om@2: btnCancel.setMinimumWidth(100) om@2: lyButton.addWidget(btnCancel) om@2: btnAbout = QtGui.QPushButton('&About', self) om@2: btnAbout.setMinimumWidth(100) om@2: lyButton.addWidget(btnAbout) om@2: om@2: button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width()) om@2: btnOk.setMinimumWidth(button_width) om@2: btnCancel.setMinimumWidth(button_width) om@2: btnAbout.setMinimumWidth(button_width) om@2: om@2: # reduce to the max om@2: self.resize(lyMain.minimumSize()) om@2: om@2: # connectors om@2: btnOk.clicked.connect(self.clicked_ok) om@2: btnCancel.clicked.connect(self.clicked_cancel) om@2: btnAbout.clicked.connect(self.clicked_about) om@2: om@2: om@2: def main(): om@2: om@2: # parse command line om@2: app = QtGui.QApplication(sys.argv) om@2: om@2: # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them om@2: data_path = Environment("opensecurity").image_path om@2: for file in os.listdir(data_path): om@2: if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'): om@2: QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(data_path, file))) om@2: om@2: # we should have now our application icon om@2: app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64'))) om@2: om@2: dlg = Chooser() om@2: om@2: # pop up the dialog om@2: dlg.show() om@2: app.exec_() om@2: om@2: if dlg.result() == QtGui.QDialog.Accepted: om@2: # encode an proper GET request to the opensecurity daemon om@2: get_vm = urllib.quote(str(dlg.edVM.text())) om@2: get_app = urllib.quote(str(dlg.cbApplication.currentText())) om@2: osd_request = 'http://127.0.0.1:8080/application?vm={0}&app={1}'.format(get_vm, get_app) om@2: urllib.urlopen(osd_request) om@2: res = 0 om@2: else: om@2: res = 1 om@2: om@2: sys.exit(res) om@2: om@2: # start om@2: if __name__ == "__main__": om@2: main() om@2: