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@13
|
39 |
|
om@13
|
40 |
from PyQt4 import QtCore
|
om@13
|
41 |
from PyQt4 import QtGui
|
om@13
|
42 |
|
om@13
|
43 |
# local
|
om@13
|
44 |
from about import About
|
om@13
|
45 |
from cygwin import Cygwin
|
om@13
|
46 |
from environment import Environment
|
om@13
|
47 |
import opensecurity_server
|
om@13
|
48 |
|
om@13
|
49 |
|
om@13
|
50 |
# ------------------------------------------------------------
|
om@13
|
51 |
# code
|
om@13
|
52 |
|
om@13
|
53 |
|
om@13
|
54 |
class Chooser(QtGui.QDialog, object):
|
om@13
|
55 |
|
om@13
|
56 |
"""Ask the user what to launch."""
|
om@13
|
57 |
|
om@13
|
58 |
def __init__(self, parent = None, flags = QtCore.Qt.WindowFlags(0)):
|
om@13
|
59 |
|
om@13
|
60 |
super(Chooser, self).__init__(parent, flags)
|
om@13
|
61 |
self.setWindowTitle('OpenSecuirty Launch Application')
|
om@13
|
62 |
self.setup_ui()
|
om@13
|
63 |
|
om@13
|
64 |
# known vms and applications
|
om@13
|
65 |
self._apps, self_vms = [], []
|
om@13
|
66 |
|
om@13
|
67 |
# positionate ourself central
|
om@13
|
68 |
screen = QtGui.QDesktopWidget().screenGeometry()
|
om@13
|
69 |
self.resize(self.geometry().width() * 1.25, self.geometry().height())
|
om@13
|
70 |
size = self.geometry()
|
om@13
|
71 |
self.move((screen.width() - size.width()) / 2, (screen.height() - size.height()) / 2)
|
om@13
|
72 |
|
om@13
|
73 |
# refresh vm and command input
|
om@13
|
74 |
self.refresh()
|
om@13
|
75 |
|
om@13
|
76 |
|
om@13
|
77 |
def app_get(self):
|
om@13
|
78 |
"""The application of the user."""
|
om@13
|
79 |
a = str(self._cbApplication.currentText())
|
om@13
|
80 |
for app in self._apps:
|
om@13
|
81 |
if a == app['name']:
|
om@13
|
82 |
return app['command']
|
om@13
|
83 |
return a
|
om@13
|
84 |
|
om@13
|
85 |
app = property(app_get)
|
om@13
|
86 |
|
om@13
|
87 |
|
om@13
|
88 |
def clicked_about(self):
|
om@13
|
89 |
"""clicked the about button"""
|
om@13
|
90 |
dlgAbout = About()
|
om@13
|
91 |
dlgAbout.exec_()
|
om@13
|
92 |
|
om@13
|
93 |
|
om@13
|
94 |
def clicked_cancel(self):
|
om@13
|
95 |
"""clicked the cancel button"""
|
om@13
|
96 |
self.reject()
|
om@13
|
97 |
|
om@13
|
98 |
|
om@13
|
99 |
def clicked_ok(self):
|
om@13
|
100 |
"""clicked the ok button"""
|
om@13
|
101 |
self.accept()
|
om@13
|
102 |
|
om@13
|
103 |
|
om@13
|
104 |
def refresh(self):
|
om@13
|
105 |
"""load the known vms and commands and adjust input fields"""
|
om@13
|
106 |
|
om@13
|
107 |
self._apps = opensecurity_server.query_apps()
|
om@13
|
108 |
self._vms = opensecurity_server.query_vms()
|
om@13
|
109 |
|
om@13
|
110 |
# add the VMs we know
|
om@13
|
111 |
self._cbApplication.clear()
|
om@13
|
112 |
for app in self._apps:
|
om@13
|
113 |
self._cbApplication.addItem(app['name'])
|
om@13
|
114 |
|
om@13
|
115 |
# add the commands we know
|
om@13
|
116 |
self._cbVM.clear()
|
om@13
|
117 |
for vm in self._vms:
|
om@13
|
118 |
self._cbVM.addItem(vm['name'])
|
om@13
|
119 |
|
om@13
|
120 |
|
om@13
|
121 |
def setup_ui(self):
|
om@13
|
122 |
"""Create the widgets."""
|
om@13
|
123 |
|
om@13
|
124 |
lyMain = QtGui.QVBoxLayout(self)
|
om@13
|
125 |
lyMain.setContentsMargins(8, 8, 8, 8)
|
om@13
|
126 |
|
om@13
|
127 |
# content area: left pixmap, right text
|
om@13
|
128 |
lyContent = QtGui.QHBoxLayout()
|
om@13
|
129 |
lyMain.addLayout(lyContent)
|
om@13
|
130 |
|
om@13
|
131 |
# pixmap
|
om@13
|
132 |
lbPix = QtGui.QLabel()
|
om@13
|
133 |
lbPix.setPixmap(QtGui.QPixmapCache.find('opensecurity_icon_64'))
|
om@13
|
134 |
lyContent.addWidget(lbPix, 0, QtCore.Qt.Alignment(QtCore.Qt.AlignTop + QtCore.Qt.AlignHCenter))
|
om@13
|
135 |
lyContent.addSpacing(16)
|
om@13
|
136 |
|
om@13
|
137 |
# launch ...
|
om@13
|
138 |
lyLaunch = QtGui.QGridLayout()
|
om@13
|
139 |
lyContent.addLayout(lyLaunch)
|
om@13
|
140 |
lbTitle = QtGui.QLabel('Specify details for application to launch.')
|
om@13
|
141 |
lyLaunch.addWidget(lbTitle, 0, 0, 1, 2)
|
om@13
|
142 |
|
om@13
|
143 |
lbVM = QtGui.QLabel('&VM-ID:')
|
om@13
|
144 |
lyLaunch.addWidget(lbVM, 1, 0)
|
om@13
|
145 |
self._cbVM = QtGui.QComboBox()
|
om@13
|
146 |
self._cbVM.setEditable(True)
|
om@13
|
147 |
self._cbVM.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
|
om@13
|
148 |
lyLaunch.addWidget(self._cbVM, 1, 1)
|
om@13
|
149 |
lbVM.setBuddy(self._cbVM)
|
om@13
|
150 |
|
om@13
|
151 |
lbApplication = QtGui.QLabel('&Application:')
|
om@13
|
152 |
lyLaunch.addWidget(lbApplication, 2, 0)
|
om@13
|
153 |
self._cbApplication = QtGui.QComboBox()
|
om@13
|
154 |
self._cbApplication.setEditable(True)
|
om@13
|
155 |
self._cbApplication.setInsertPolicy(QtGui.QComboBox.InsertAlphabetically)
|
om@13
|
156 |
lyLaunch.addWidget(self._cbApplication, 2, 1)
|
om@13
|
157 |
lbApplication.setBuddy(self._cbApplication)
|
om@13
|
158 |
|
om@13
|
159 |
lyLaunch.addWidget(QtGui.QWidget(), 3, 0, 1, 2)
|
om@13
|
160 |
lyLaunch.setColumnStretch(1, 1)
|
om@13
|
161 |
lyLaunch.setRowStretch(3, 1)
|
om@13
|
162 |
|
om@13
|
163 |
lyMain.addStretch(1)
|
om@13
|
164 |
|
om@13
|
165 |
# buttons
|
om@13
|
166 |
lyButton = QtGui.QHBoxLayout()
|
om@13
|
167 |
lyMain.addLayout(lyButton)
|
om@13
|
168 |
|
om@13
|
169 |
lyButton.addStretch(1)
|
om@13
|
170 |
btnOk = QtGui.QPushButton('&Ok', self)
|
om@13
|
171 |
btnOk.setDefault(True)
|
om@13
|
172 |
btnOk.setMinimumWidth(100)
|
om@13
|
173 |
lyButton.addWidget(btnOk)
|
om@13
|
174 |
btnCancel = QtGui.QPushButton('&Cancel', self)
|
om@13
|
175 |
btnCancel.setMinimumWidth(100)
|
om@13
|
176 |
lyButton.addWidget(btnCancel)
|
om@13
|
177 |
btnAbout = QtGui.QPushButton('&About', self)
|
om@13
|
178 |
btnAbout.setMinimumWidth(100)
|
om@13
|
179 |
lyButton.addWidget(btnAbout)
|
om@13
|
180 |
|
om@13
|
181 |
button_width = max(btnOk.width(), btnCancel.width(), btnAbout.width())
|
om@13
|
182 |
btnOk.setMinimumWidth(button_width)
|
om@13
|
183 |
btnCancel.setMinimumWidth(button_width)
|
om@13
|
184 |
btnAbout.setMinimumWidth(button_width)
|
om@13
|
185 |
|
om@13
|
186 |
# reduce to the max
|
om@13
|
187 |
self.resize(lyMain.minimumSize())
|
om@13
|
188 |
|
om@13
|
189 |
# connectors
|
om@13
|
190 |
btnOk.clicked.connect(self.clicked_ok)
|
om@13
|
191 |
btnCancel.clicked.connect(self.clicked_cancel)
|
om@13
|
192 |
btnAbout.clicked.connect(self.clicked_about)
|
om@13
|
193 |
|
om@13
|
194 |
|
om@13
|
195 |
def user_get(self):
|
om@13
|
196 |
"""The user of the vm of choice."""
|
om@13
|
197 |
v = str(self._cbVM.currentText())
|
om@13
|
198 |
for vm in self._vms:
|
om@13
|
199 |
if v == vm['name']:
|
om@13
|
200 |
return vm['user']
|
om@13
|
201 |
return v
|
om@13
|
202 |
|
om@13
|
203 |
user = property(user_get)
|
om@13
|
204 |
|
om@13
|
205 |
|
om@13
|
206 |
def vm_get(self):
|
om@13
|
207 |
"""The vm of choice."""
|
om@13
|
208 |
v = str(self._cbVM.currentText())
|
om@13
|
209 |
for vm in self._vms:
|
om@13
|
210 |
if v == vm['name']:
|
om@13
|
211 |
return vm['ip']
|
om@13
|
212 |
return v
|
om@13
|
213 |
|
om@13
|
214 |
vm = property(vm_get)
|
om@13
|
215 |
|
om@13
|
216 |
|
om@13
|
217 |
def ask_user():
|
om@13
|
218 |
"""ask the user for VM and app to start"""
|
om@13
|
219 |
|
om@13
|
220 |
# launch Qt
|
om@13
|
221 |
app = QtGui.QApplication(sys.argv)
|
om@13
|
222 |
|
om@13
|
223 |
# prebuild the pixmap cache: fetch all jpg, png and jpeg images and load them
|
om@13
|
224 |
image_path = os.path.join(Environment("OpenSecurity").data_path, '..', 'gfx')
|
om@13
|
225 |
for file in os.listdir(image_path):
|
om@13
|
226 |
if file.lower().rpartition('.')[2] in ('jpg', 'png', 'jpeg'):
|
om@13
|
227 |
QtGui.QPixmapCache.insert(file.lower().rpartition('.')[0], QtGui.QPixmap(os.path.join(image_path, file)))
|
om@13
|
228 |
|
om@13
|
229 |
# we should have now our application icon
|
om@13
|
230 |
app.setWindowIcon(QtGui.QIcon(QtGui.QPixmapCache.find('opensecurity_icon_64')))
|
om@13
|
231 |
|
om@13
|
232 |
# pop up the dialog
|
om@13
|
233 |
dlg = Chooser()
|
om@13
|
234 |
dlg.show()
|
om@13
|
235 |
app.exec_()
|
om@13
|
236 |
|
om@13
|
237 |
if dlg.result() == QtGui.QDialog.Accepted:
|
om@13
|
238 |
return dlg.user, dlg.vm, dlg.app
|
om@13
|
239 |
|
om@13
|
240 |
return '', '', ''
|
om@13
|
241 |
|
om@13
|
242 |
|
om@13
|
243 |
def main():
|
om@13
|
244 |
"""entry point"""
|
om@13
|
245 |
|
om@13
|
246 |
# parse command line
|
om@13
|
247 |
parser = argparse.ArgumentParser(description = 'OpenSecurity Launcher: run application in VM')
|
om@13
|
248 |
parser.add_argument('user', metavar='USER', help='USER on Virtual Machine', nargs='?', type=str, default='')
|
om@13
|
249 |
parser.add_argument('ip', metavar='IP', help='IP of Virtual Machine', nargs='?', type=str, default='')
|
om@13
|
250 |
parser.add_argument('command', metavar='COMMAND', help='Full path of command and arguments to start inside VM', nargs='?', type=str, default='')
|
om@13
|
251 |
args = parser.parse_args()
|
om@13
|
252 |
|
om@13
|
253 |
# we must have at least all or none set
|
om@13
|
254 |
set_user = args.user != ''
|
om@13
|
255 |
set_ip = args.ip != ''
|
om@13
|
256 |
set_command = args.command != ''
|
om@13
|
257 |
set_ALL = set_user and set_ip and set_command
|
om@13
|
258 |
set_NONE = (not set_user) and (not set_ip) and (not set_command)
|
om@13
|
259 |
if (not set_ALL) and (not set_NONE):
|
om@13
|
260 |
sys.stderr.write("Please specify user, ip and command or none.\n")
|
om@13
|
261 |
sys.stderr.write("Type '--help' for help.\n")
|
om@13
|
262 |
sys.exit(1)
|
om@13
|
263 |
|
om@13
|
264 |
# check if we need to ask the user
|
om@13
|
265 |
if set_NONE:
|
om@13
|
266 |
args.user, args.ip, args.command = ask_user()
|
om@13
|
267 |
|
om@13
|
268 |
# still no IP? --> no chance, over and out!
|
om@13
|
269 |
if args.ip == '':
|
om@13
|
270 |
sys.exit(0)
|
om@13
|
271 |
|
om@13
|
272 |
# ensure we have our X11 running
|
om@13
|
273 |
Cygwin.start_X11()
|
om@13
|
274 |
|
om@13
|
275 |
# the SSH command
|
om@13
|
276 |
user_at_guest = args.user + '@' + args.ip
|
om@13
|
277 |
ssh = 'DISPLAY=:0 /usr/bin/ssh -Y ' + user_at_guest + ' ' + args.command
|
om@13
|
278 |
print(ssh)
|
om@13
|
279 |
|
om@13
|
280 |
# off we go!
|
om@13
|
281 |
Cygwin()(['/bin/bash', '--login', '-i', '-c', ssh], None, None, None)
|
om@13
|
282 |
|
om@13
|
283 |
|
om@13
|
284 |
# start
|
om@13
|
285 |
if __name__ == "__main__":
|
om@13
|
286 |
main()
|
om@13
|
287 |
|