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 # ------------------------------------------------------------
42 from PyQt4 import QtCore
43 from PyQt4 import QtGui
46 from about import About
47 from cygwin import Cygwin
48 from environment import Environment
51 # ------------------------------------------------------------
55 class Chooser(QtGui.QDialog, object):
57 """Ask the user what to launch."""
59 def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
61 super(Chooser, self).__init__(parent, flags)
62 self.setWindowTitle('OpenSecuirty Launch Application')
65 # positionate ourself central
66 screen = QtGui.QDesktopWidget().screenGeometry()
67 self.resize(self.geometry().width() * 1.25, self.geometry().height())
68 size = self.geometry()
69 self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
71 self._vms = [ { 'name': 'SecurityDVM0', 'ip': '192.168.56.101' } ]
72 self._apps = [ { 'name': 'Browser', 'command': '/usr/bin/iceweasel' } ]
77 self._cbVM.addItem(vm['name'])
79 # add the commands we know
80 self._cbApplication.clear()
81 for app in self._apps:
82 self._cbApplication.addItem(app['name'])
87 """The application of the user."""
88 a = str(self._cbApplication.currentText())
89 for app in self._apps:
94 app = property(app_get)
97 def clicked_about(self):
98 """clicked the about button"""
103 def clicked_cancel(self):
104 """clicked the cancel button"""
108 def clicked_ok(self):
109 """clicked the ok button"""
114 """Create the widgets."""
116 lyMain = QtGui.QVBoxLayout(self)
117 lyMain.setContentsMargins(8, 8, 8, 8)
119 # content area: left pixmap, right text
120 lyContent = QtGui.QHBoxLayout()
121 lyMain.addLayout(lyContent)
124 lbPix = QtGui.QLabel()
125 lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
126 lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
127 lyContent.addSpacing(16)
130 lyLaunch = QtGui.QGridLayout()
131 lyContent.addLayout(lyLaunch)
132 lbTitle = QtGui.QLabel('Specify details for application to launch.')
133 lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
135 lbVM = QtGui.QLabel('&VM-ID:')
136 lyLaunch.addWidget(lbVM, 1, 0)
137 self._cbVM = QtGui.QComboBox()
138 self._cbVM.setEditable(True)
139 self._cbVM.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
140 lyLaunch.addWidget(self._cbVM, 1, 1)
141 lbVM.setBuddy(self._cbVM)
143 lbApplication = QtGui.QLabel('&Application:')
144 lyLaunch.addWidget(lbApplication, 2, 0)
145 self._cbApplication = QtGui.QComboBox()
146 self._cbApplication.setEditable(True)
147 self._cbApplication.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
148 lyLaunch.addWidget(self._cbApplication, 2, 1)
149 lbApplication.setBuddy(self._cbApplication)
151 lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
152 lyLaunch.setColumnStretch(1, 1)
153 lyLaunch.setRowStretch(3, 1)
158 lyButton = QtGui.QHBoxLayout()
159 lyMain.addLayout(lyButton)
161 lyButton.addStretch(1)
162 btnOk = QtGui.QPushButton('&Ok', self)
163 btnOk.setDefault(True)
164 btnOk.setMinimumWidth(100)
165 lyButton.addWidget(btnOk)
166 btnCancel = QtGui.QPushButton('&Cancel', self)
167 btnCancel.setMinimumWidth(100)
168 lyButton.addWidget(btnCancel)
169 btnAbout = QtGui.QPushButton('&About', self)
170 btnAbout.setMinimumWidth(100)
171 lyButton.addWidget(btnAbout)
173 button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
174 btnOk.setMinimumWidth(button_width)
175 btnCancel.setMinimumWidth(button_width)
176 btnAbout.setMinimumWidth(button_width)
179 self.resize(lyMain.minimumSize())
182 btnOk.clicked.connect(self.clicked_ok)
183 btnCancel.clicked.connect(self.clicked_cancel)
184 btnAbout.clicked.connect(self.clicked_about)
188 """The vm of choice."""
189 v = str(self._cbVM.currentText())
195 vm = property(vm_get)
199 """ask the user for VM and app to start"""
201 # prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
202 image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
203 for file in os.listdir(image_path):
204 if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
205 QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
207 # we should have now our application icon
208 app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
215 if dlg.result() == QtGui.QDialog.Accepted:
216 return dlg.vm, dlg.app
226 app = QtGui.QApplication(sys.argv)
229 parser = argparse.ArgumentParser(description = 'OpenSecurity Launcher: run application in VM')
230 parser.add_argument('ip', metavar='IP', help='IP of Virtual Machine', nargs='?', type=str, default='')
231 parser.add_argument('command', metavar='COMMAND', help='Full path of command and arguments to start inside VM', nargs='?', type=str, default='')
232 args = parser.parse_args()
234 # we must have at least all or none set
235 set_ip = args.ip != ''
236 set_command = args.command != ''
237 set_ALL = set_ip and set_command
238 set_NONE = (not set_ip) and (not set_command)
239 if (not set_ALL) and (not set_NONE):
240 sys.stderr.write("Please specify ip and command or none.\n")
241 sys.stderr.write("Type '--help' for help.\n")
244 # check if we need to ask the user
246 args.ip, args.command = ask_user()
248 # still no IP? --> no chance, over and out!
252 # ensure we have our X11 running
255 # call the OpenSecurity Admin to launch our progie =)
256 url_vm = urllib.quote(args.ip)
257 url_command = urllib.quote(args.command)
258 QtGui.QMessageBox.information(None, 'OpenSecurity Launche', 'About to launch <br/><b>' + url_command + '</b><br/>at VM <br/><b>' + url_vm + '</b>')
260 # user_at_guest = args.user + '@' + args.ip
261 # ssh = 'DISPLAY=:0 /usr/bin/ssh -Y ' + user_at_guest + ' ' + args.command
265 # Cygwin()(['/bin/bash', '--login', '-i', '-c', ssh], None, None, None)
269 if __name__ == "__main__":