mb@7
|
1 |
#!/bin/env python
|
mb@7
|
2 |
# -*- coding: utf-8 -*-
|
mb@7
|
3 |
|
mb@7
|
4 |
# ------------------------------------------------------------
|
mb@7
|
5 |
# opensecurityd
|
mb@7
|
6 |
#
|
mb@7
|
7 |
# the opensecurityd as RESTful server
|
mb@7
|
8 |
#
|
mb@7
|
9 |
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
|
mb@7
|
10 |
#
|
mb@7
|
11 |
# Copyright (C) 2013 AIT Austrian Institute of Technology
|
mb@7
|
12 |
# AIT Austrian Institute of Technology GmbH
|
mb@7
|
13 |
# Donau-City-Strasse 1 | 1220 Vienna | Austria
|
mb@7
|
14 |
# http://www.ait.ac.at
|
mb@7
|
15 |
#
|
mb@7
|
16 |
# This program is free software; you can redistribute it and/or
|
mb@7
|
17 |
# modify it under the terms of the GNU General Public License
|
mb@7
|
18 |
# as published by the Free Software Foundation version 2.
|
mb@7
|
19 |
#
|
mb@7
|
20 |
# This program is distributed in the hope that it will be useful,
|
mb@7
|
21 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
mb@7
|
22 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
mb@7
|
23 |
# GNU General Public License for more details.
|
mb@7
|
24 |
#
|
mb@7
|
25 |
# You should have received a copy of the GNU General Public License
|
mb@7
|
26 |
# along with this program; if not, write to the Free Software
|
mb@7
|
27 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
mb@7
|
28 |
# Boston, MA 02110-1301, USA.
|
mb@7
|
29 |
# ------------------------------------------------------------
|
mb@7
|
30 |
|
mb@7
|
31 |
|
mb@7
|
32 |
# ------------------------------------------------------------
|
mb@7
|
33 |
# imports
|
mb@7
|
34 |
|
mb@7
|
35 |
import os
|
mb@7
|
36 |
import os.path
|
mb@7
|
37 |
import subprocess
|
mb@7
|
38 |
import sys
|
mb@7
|
39 |
import web
|
mb@7
|
40 |
from vmmanager.vmmanager import VMManager
|
mb@7
|
41 |
|
mb@7
|
42 |
# local
|
mb@7
|
43 |
from environment import Environment
|
mb@7
|
44 |
|
mb@7
|
45 |
|
mb@7
|
46 |
# ------------------------------------------------------------
|
mb@7
|
47 |
# const
|
mb@7
|
48 |
|
mb@7
|
49 |
__version__ = "0.1"
|
mb@7
|
50 |
|
mb@7
|
51 |
|
mb@7
|
52 |
"""All the URLs we know mapping to class handler"""
|
mb@7
|
53 |
opensecurity_urls = (
|
mb@7
|
54 |
'/device_change', 'os_device_change',
|
mb@7
|
55 |
'/application', 'os_application',
|
mb@7
|
56 |
'/device', 'os_device',
|
mb@7
|
57 |
'/device/credentials', 'os_device_credentials',
|
mb@7
|
58 |
'/device/password', 'os_device_password',
|
mb@7
|
59 |
'/', 'os_root'
|
mb@7
|
60 |
)
|
mb@7
|
61 |
|
mb@7
|
62 |
|
mb@7
|
63 |
# ------------------------------------------------------------
|
mb@7
|
64 |
# code
|
mb@7
|
65 |
|
mb@7
|
66 |
gvm_mgr = VMManager()
|
mb@7
|
67 |
|
mb@7
|
68 |
|
mb@7
|
69 |
class os_application:
|
mb@7
|
70 |
|
mb@7
|
71 |
"""OpenSecurity '/application' handler.
|
mb@7
|
72 |
|
mb@7
|
73 |
This is called on GET /application?vm=VM-ID&app=APP-ID
|
mb@7
|
74 |
This tries to access the vm identified with the label VM-ID
|
mb@7
|
75 |
and launched the application identified APP-ID
|
mb@7
|
76 |
"""
|
mb@7
|
77 |
|
mb@7
|
78 |
def GET(self):
|
mb@7
|
79 |
|
mb@7
|
80 |
# pick the arguments
|
mb@7
|
81 |
args = web.input()
|
mb@7
|
82 |
|
mb@7
|
83 |
# we _need_ a vm
|
mb@7
|
84 |
if not "vm" in args:
|
mb@7
|
85 |
raise web.badrequest()
|
mb@7
|
86 |
|
mb@7
|
87 |
# we _need_ a app
|
mb@7
|
88 |
if not "app" in args:
|
mb@7
|
89 |
raise web.badrequest()
|
mb@7
|
90 |
|
mb@7
|
91 |
## TODO: HARD CODED STUFF HERE! THIS SHOULD BE FLEXIBLE!
|
mb@7
|
92 |
ssh_private_key = os.path.join(Environment("opensecurity").data_path, 'share', '192.168.56.15.ppk')
|
mb@7
|
93 |
putty_session = '192.168.56.15'
|
mb@7
|
94 |
process_command = ['plink.exe', '-i', ssh_private_key, putty_session, args.app]
|
mb@7
|
95 |
si = subprocess.STARTUPINFO()
|
mb@7
|
96 |
si.dwFlags = subprocess.STARTF_USESHOWWINDOW
|
mb@7
|
97 |
si.wShowWindow = subprocess.SW_HIDE
|
mb@7
|
98 |
print('tyring to launch: ' + ' '.join(process_command))
|
mb@7
|
99 |
process = subprocess.Popen(process_command, shell = True)
|
mb@7
|
100 |
return 'launched: ' + ' '.join(process_command)
|
mb@7
|
101 |
|
mb@7
|
102 |
class os_device:
|
mb@7
|
103 |
|
mb@7
|
104 |
"""OpenSecurity '/device' handler"""
|
mb@7
|
105 |
|
mb@7
|
106 |
def GET(self):
|
mb@7
|
107 |
return "os_device"
|
mb@7
|
108 |
|
mb@7
|
109 |
class os_device_change:
|
mb@7
|
110 |
|
mb@7
|
111 |
"""OpenSecurity '/device_change' handler"""
|
mb@7
|
112 |
|
mb@7
|
113 |
def GET(self):
|
mb@12
|
114 |
print 'received device_change'
|
mb@7
|
115 |
gvm_mgr.cygwin_path = 'c:\\cygwin64\\bin\\'
|
mb@12
|
116 |
gvm_mgr.handleDeviceChange()
|
mb@12
|
117 |
|
mb@7
|
118 |
#gvm_mgr.configureHostNetworking()
|
mb@7
|
119 |
return "os_device_change"
|
mb@7
|
120 |
|
mb@7
|
121 |
|
mb@7
|
122 |
class os_device_credentials:
|
mb@7
|
123 |
|
mb@7
|
124 |
"""OpenSecurity '/device/credentials' handler.
|
mb@7
|
125 |
|
mb@7
|
126 |
This is called on GET /device/credentials?id=DEVICE-ID.
|
mb@7
|
127 |
Ideally this should pop up a user dialog to insert his
|
mb@7
|
128 |
credentials based the DEVICE-ID
|
mb@7
|
129 |
"""
|
mb@7
|
130 |
|
mb@7
|
131 |
def GET(self):
|
mb@7
|
132 |
|
mb@7
|
133 |
# pick the arguments
|
mb@7
|
134 |
args = web.input()
|
mb@7
|
135 |
|
mb@7
|
136 |
# we _need_ a device id
|
mb@7
|
137 |
if not "id" in args:
|
mb@7
|
138 |
raise web.badrequest()
|
mb@7
|
139 |
|
mb@7
|
140 |
# invoke the user dialog as a subprocess
|
mb@7
|
141 |
dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
|
mb@7
|
142 |
process_command = [sys.executable, dlg_credentials_image, 'credentials', 'Please provide credentials for accessing \ndevice: "{0}".'.format(args.id)]
|
mb@7
|
143 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
mb@7
|
144 |
result = process.communicate()[0]
|
mb@7
|
145 |
if process.returncode != 0:
|
mb@7
|
146 |
return 'Credentials request has been aborted.'
|
mb@7
|
147 |
|
mb@7
|
148 |
return result
|
mb@7
|
149 |
|
mb@7
|
150 |
|
mb@7
|
151 |
class os_device_password:
|
mb@7
|
152 |
|
mb@7
|
153 |
"""OpenSecurity '/device/password' handler.
|
mb@7
|
154 |
|
mb@7
|
155 |
This is called on GET /device/password?id=DEVICE-ID.
|
mb@7
|
156 |
Ideally this should pop up a user dialog to insert his
|
mb@7
|
157 |
password based the DEVICE-ID
|
mb@7
|
158 |
"""
|
mb@7
|
159 |
|
mb@7
|
160 |
def GET(self):
|
mb@7
|
161 |
|
mb@7
|
162 |
# pick the arguments
|
mb@7
|
163 |
args = web.input()
|
mb@7
|
164 |
|
mb@7
|
165 |
# we _need_ a device id
|
mb@7
|
166 |
if not "id" in args:
|
mb@7
|
167 |
raise web.badrequest()
|
mb@7
|
168 |
|
mb@7
|
169 |
# invoke the user dialog as a subprocess
|
mb@7
|
170 |
dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
|
mb@7
|
171 |
process_command = [sys.executable, dlg_credentials_image, 'password', 'Please provide a password for accessing \ndevice: "{0}".'.format(args.id)]
|
mb@7
|
172 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
mb@7
|
173 |
result = process.communicate()[0]
|
mb@7
|
174 |
if process.returncode != 0:
|
mb@7
|
175 |
return 'Credentials request has been aborted.'
|
mb@7
|
176 |
|
mb@7
|
177 |
return result
|
mb@7
|
178 |
|
mb@7
|
179 |
|
mb@7
|
180 |
class os_root:
|
mb@7
|
181 |
|
mb@7
|
182 |
"""OpenSecurity '/' handler"""
|
mb@7
|
183 |
|
mb@7
|
184 |
def GET(self):
|
mb@7
|
185 |
return "OpenSecurity-Server { \"version\": \"%s\" }" % __version__
|
mb@7
|
186 |
|
mb@7
|
187 |
|
mb@7
|
188 |
# start
|
mb@7
|
189 |
if __name__ == "__main__":
|
mb@7
|
190 |
server = web.application(opensecurity_urls, globals())
|
mb@7
|
191 |
server.run()
|
mb@7
|
192 |
|