2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
5 # opensecurity-launcher
7 # launches an application inside a VM
9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
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
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.
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.
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 # ------------------------------------------------------------
32 # ------------------------------------------------------------
40 from PyQt4 import QtCore
41 from PyQt4 import QtGui
44 from about import About
45 from cygwin import Cygwin
46 from environment import Environment
47 import opensecurity_server
50 # ------------------------------------------------------------
54 class Chooser(QtGui.QDialog, object):
56 """Ask the user what to launch."""
58 def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
60 super(Chooser, self).__init__(parent, flags)
61 self.setWindowTitle('OpenSecuirty Launch Application')
64 # known vms and applications
65 self._apps, self_vms = [], []
67 # positionate ourself central
68 screen = QtGui.QDesktopWidget().screenGeometry()
69 self.resize(self.geometry().width() * 1.25, self.geometry().height())
70 size = self.geometry()
71 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
73 # refresh vm and command input
78 """The application of the user."""
79 a = str(self._cbApplication.currentText())
80 for app in self._apps:
85 app = property(app_get)
88 def clicked_about(self):
89 """clicked the about button"""
94 def clicked_cancel(self):
95 """clicked the cancel button"""
100 """clicked the ok button"""
105 """load the known vms and commands and adjust input fields"""
107 self._apps = opensecurity_server.query_apps()
108 self._vms = opensecurity_server.query_vms()
110 # add the VMs we know
111 self._cbApplication.clear()
112 for app in self._apps:
113 self._cbApplication.addItem(app['name'])
115 # add the commands we know
118 self._cbVM.addItem(vm['name'])
122 """Create the widgets."""
124 lyMain = QtGui.QVBoxLayout(self)
125 lyMain.setContentsMargins(8, 8, 8, 8)
127 # content area: left pixmap, right text
128 lyContent = QtGui.QHBoxLayout()
129 lyMain.addLayout(lyContent)
132 lbPix = QtGui.QLabel()
133 lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
134 lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
135 lyContent.addSpacing(16)
138 lyLaunch = QtGui.QGridLayout()
139 lyContent.addLayout(lyLaunch)
140 lbTitle = QtGui.QLabel('Specify details for application to launch.')
141 lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
143 lbVM = QtGui.QLabel('&VM-ID:')
144 lyLaunch.addWidget(lbVM, 1, 0)
145 self._cbVM = QtGui.QComboBox()
146 self._cbVM.setEditable(True)
147 self._cbVM.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
148 lyLaunch.addWidget(self._cbVM, 1, 1)
149 lbVM.setBuddy(self._cbVM)
151 lbApplication = QtGui.QLabel('&Application:')
152 lyLaunch.addWidget(lbApplication, 2, 0)
153 self._cbApplication = QtGui.QComboBox()
154 self._cbApplication.setEditable(True)
155 self._cbApplication.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
156 lyLaunch.addWidget(self._cbApplication, 2, 1)
157 lbApplication.setBuddy(self._cbApplication)
159 lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
160 lyLaunch.setColumnStretch(1, 1)
161 lyLaunch.setRowStretch(3, 1)
166 lyButton = QtGui.QHBoxLayout()
167 lyMain.addLayout(lyButton)
169 lyButton.addStretch(1)
170 btnOk = QtGui.QPushButton('&Ok', self)
171 btnOk.setDefault(True)
172 btnOk.setMinimumWidth(100)
173 lyButton.addWidget(btnOk)
174 btnCancel = QtGui.QPushButton('&Cancel', self)
175 btnCancel.setMinimumWidth(100)
176 lyButton.addWidget(btnCancel)
177 btnAbout = QtGui.QPushButton('&About', self)
178 btnAbout.setMinimumWidth(100)
179 lyButton.addWidget(btnAbout)
181 button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
182 btnOk.setMinimumWidth(button_width)
183 btnCancel.setMinimumWidth(button_width)
184 btnAbout.setMinimumWidth(button_width)
187 self.resize(lyMain.minimumSize())
190 btnOk.clicked.connect(self.clicked_ok)
191 btnCancel.clicked.connect(self.clicked_cancel)
192 btnAbout.clicked.connect(self.clicked_about)
196 """The user of the vm of choice."""
197 v = str(self._cbVM.currentText())
203 user = property(user_get)
207 """The vm of choice."""
208 v = str(self._cbVM.currentText())
214 vm = property(vm_get)
218 """ask the user for VM and app to start"""
221 app = QtGui.QApplication(sys.argv)
223 # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
224 image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
225 for file in os.listdir(image_path):
226 if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
227 QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
229 # we should have now our application icon
230 app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
237 if dlg.result() == QtGui.QDialog.Accepted:
238 return dlg.user, dlg.vm, dlg.app
247 parser = argparse.ArgumentParser(description = 'OpenSecurity Launcher: run application in VM')
248 parser.add_argument('user', metavar='USER', help='USER on Virtual Machine', nargs='?', type=str, default='')
249 parser.add_argument('ip', metavar='IP', help='IP of Virtual Machine', nargs='?', type=str, default='')
250 parser.add_argument('command', metavar='COMMAND', help='Full path of command and arguments to start inside VM', nargs='?', type=str, default='')
251 args = parser.parse_args()
253 # we must have at least all or none set
254 set_user = args.user != ''
255 set_ip = args.ip != ''
256 set_command = args.command != ''
257 set_ALL = set_user and set_ip and set_command
258 set_NONE = (not set_user) and (not set_ip) and (not set_command)
259 if (not set_ALL) and (not set_NONE):
260 sys.stderr.write("Please specify user, ip and command or none.\n")
261 sys.stderr.write("Type '--help' for help.\n")
264 # check if we need to ask the user
266 args.user, args.ip, args.command = ask_user()
268 # still no IP? --> no chance, over and out!
272 # ensure we have our X11 running
276 user_at_guest = args.user + '@' + args.ip
277 ssh = 'DISPLAY=:0 /usr/bin/ssh -Y ' + user_at_guest + ' ' + args.command
281 Cygwin()(['/bin/bash', '--login', '-i', '-c', ssh], None, None, None)
285 if __name__ == "__main__":