2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
5 # opensecurity_client_restful_server
7 # the OpenSecurity client RESTful server
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 # ------------------------------------------------------------
44 from environment import Environment
45 from notification import Notification
46 import opensecurity_server
49 # ------------------------------------------------------------
56 """All the URLs we know mapping to class handler"""
58 '/application', 'os_application',
59 '/credentials', 'os_credentials',
60 '/notification', 'os_notification',
61 '/password', 'os_password',
66 # ------------------------------------------------------------
71 """OpenSecurity '/application' handler.
73 This is called on GET /application?vm=VM-ID&app=APP-ID
74 This tries to access the vm identified with the label VM-ID
75 and launched the application identified APP-ID
85 raise web.badrequest('no vm given')
89 raise web.badrequest('no app given')
91 apps = opensecurity_server.query_apps()
92 vms = opensecurity_server.query_vms()
94 # check if we do have valid vm
95 v = [v for v in vms if v['name'] == args.vm]
97 raise web.notfound('vm not found')
100 # check if we do have a valid app
101 a = [a for a in apps if a['name'] == args.app]
103 raise web.notfound('app not found')
107 res = "starting: launch " + v['user'] + " " + v['ip'] + " " + a['command']
109 launch_image = os.path.join(sys.path[0], 'launch.py')
110 process_command = [sys.executable, launch_image, v['user'], v['ip'], a['command']]
111 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
112 result = process.communicate()[0]
113 if process.returncode != 0:
114 return 'Launch of application aborted.'
119 class os_credentials:
120 """OpenSecurity '/credentials' handler.
122 This is called on GET /credentials?text=TEXT.
123 Ideally this should pop up a user dialog to insert his
124 credentials based the given TEXT.
133 if not "text" in args:
134 raise web.badrequest('no text given')
136 # invoke the user dialog as a subprocess
137 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
138 process_command = [sys.executable, dlg_image, 'credentials', args.text]
139 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
140 result = process.communicate()[0]
141 if process.returncode != 0:
142 return 'Credentials request has been aborted.'
147 class os_notification:
148 """OpenSecurity '/notification' handler.
150 This is called on GET /notification?msgtype=TYPE&text=TEXT.
151 This will pop up an OpenSecurity notifcation window
160 if not "msgtype" in args:
161 raise web.badrequest('no msgtype given')
163 if not args.msgtype in Notification.TYPES:
164 raise web.badrequest('Unknown value for msgtype')
167 if not "text" in args:
168 raise web.badrequest('no text given')
170 # invoke the user dialog as a subprocess
171 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
172 process_command = [sys.executable, dlg_image, 'notification-' + args.msgtype, args.text]
173 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
178 """OpenSecurity '/password' handler.
180 This is called on GET /password?text=TEXT.
181 Ideally this should pop up a user dialog to insert his
182 password based device name.
191 if not "text" in args:
192 raise web.badrequest('no text given')
195 remote_ip = web.ctx.environ['REMOTE_ADDR']
197 # invoke the user dialog as a subprocess
198 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
199 process_command = [sys.executable, dlg_image, 'password', args.text]
200 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
201 result = process.communicate()[0]
202 if process.returncode != 0:
203 return 'password request has been aborted.'
205 # all ok, tell send request back appropriate destination
207 # the returned value of the dialog is a jason object like
208 # "{ 'password': 'THE_PASSWORD' }"
209 # so we _could_ call eval(...) on this.
211 # However, anyone malicious enough _could_ encode a certain
212 # "password" making some nasty things within that eval code. :(
214 # So this is plain old-school string hacking then ...
216 password = result.split(':')[1].split("'")[1]
218 raise web.internalerror('error in password parsing')
220 url_addr = 'http://' + remote_ip + ':58080/password'
221 url_data = urllib.urlencode({ 'password': password})
222 req = urllib2.Request(url = url_addr + '?' + url_data)
224 res = urllib2.urlopen(req)
226 raise web.internalerror('failed to contact: ' + url_addr)
228 return 'password told'
232 """OpenSecurity '/' handler"""
236 res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % __version__
238 # add some sample links
244 (copy paste this into your browser's address field after the host:port)
246 /password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
248 (eg.: http://127.0.0.1:8090/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0))
249 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
252 Request a combination of user and password:
253 (copy paste this into your browser's address field after the host:port)
255 /credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
257 (eg.: http://127.0.0.1:8090/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.)
258 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
262 (copy paste this into your browser's address field after the host:port)
264 /application?vm=Debian+7&app=Browser
266 (e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
273 if __name__ == "__main__":
274 server = web.application(opensecurity_urls, globals())