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 # ------------------------------------------------------------
46 from environment import Environment
47 from notification import Notification
50 # ------------------------------------------------------------
57 """All the URLs we know mapping to class handler"""
59 #'/application', 'os_application',
60 '/credentials', 'os_credentials',
61 '/notification', 'os_notification',
62 '/password', 'os_password',
67 # ------------------------------------------------------------
71 # class os_application:
75 # """OpenSecurity '/application' handler.
77 # This is called on GET /application?vm=VM-ID&app=APP-ID
78 # This tries to access the vm identified with the label VM-ID
79 # and launched the application identified APP-ID
84 # # pick the arguments
88 # if not "vm" in args:
89 # raise web.badrequest('no vm given')
92 # if not "command" in args:
93 # raise web.badrequest('no app given')
95 # # check if we do have valid vm
96 # v = [v for v in vms if v['name'] == args.vm]
98 # raise web.notfound('vm not found')
101 # # check if we do have a valid app
102 # a = [a for a in apps if a['name'] == args.app]
104 # raise web.notfound('app not found')
107 # # invoke launch with
108 # res = "starting: launch " + v['user'] + " " + v['ip'] + " " + a['command']
110 # launch_image = os.path.join(sys.path[0], 'launch.py')
111 # process_command = [sys.executable, launch_image, v['user'], v['ip'], a['command']]
112 # process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
113 # result = process.communicate()[0]
114 # if process.returncode != 0:
115 # return 'Launch of application aborted.'
120 class os_credentials:
121 """OpenSecurity '/credentials' handler.
123 This is called on GET /credentials?text=TEXT.
124 Ideally this should pop up a user dialog to insert his
125 credentials based the given TEXT.
134 if not "text" in args:
135 raise web.badrequest('no text given')
137 # invoke the user dialog as a subprocess
138 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
139 process_command = [sys.executable, dlg_image, 'credentials', args.text]
140 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
141 result = process.communicate()[0]
142 if process.returncode != 0:
143 return 'Credentials request has been aborted.'
148 class os_notification:
149 """OpenSecurity '/notification' handler.
151 This is called on GET /notification?msgtype=TYPE&text=TEXT.
152 This will pop up an OpenSecurity notifcation window
160 if "message" in args:
164 if not "msgtype" in args:
165 raise web.badrequest('no msgtype given')
167 if not args.msgtype in Notification.TYPES:
168 raise web.badrequest('Unknown value for msgtype')
171 if not "text" in args:
172 raise web.badrequest('no text given')
174 # invoke the user dialog as a subprocess
175 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
176 process_command = [sys.executable, dlg_image, 'notification-' + args.msgtype, args.text]
177 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
180 class PasswordSender(threading.Thread):
183 def __init__(self, remote_ip, args):
184 threading.Thread.__init__(self)
186 self.remote_ip = remote_ip
192 # invoke the user dialog as a subprocess
193 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
194 process_command = [sys.executable, dlg_image, 'password', self.args.text]
195 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
196 result = process.communicate()[0]
197 if process.returncode != 0:
198 print 'password request has been aborted.'
201 # all ok, tell send request back appropriate destination
203 password = result.split(':')[1].split("'")[1]
205 print 'error in password parsing'
208 url_addr = 'http://' + self.remote_ip + ':58080/password'
209 url_data = urllib.urlencode({ 'password': password})
210 url = url_addr + '?' + url_data
211 req = urllib2.Request(url)
213 res = urllib2.urlopen(req)
215 print 'failed to contact: ' + url_addr
219 """OpenSecurity '/password' handler.
221 This is called on GET /password?text=TEXT.
222 Ideally this should pop up a user dialog to insert his
223 password based device name.
232 if not "text" in args:
233 raise web.badrequest('no text given')
236 remote_ip = web.ctx.environ['REMOTE_ADDR']
238 sender = PasswordSender(remote_ip, args)
241 return 'password told'
245 """OpenSecurity '/' handler"""
249 res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % __version__
251 # add some sample links
257 (copy paste this into your browser's address field after the host:port)
259 /password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
261 (eg.: http://127.0.0.1:8090/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0))
262 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
265 Request a combination of user and password:
266 (copy paste this into your browser's address field after the host:port)
268 /credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
270 (eg.: http://127.0.0.1:8090/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.)
271 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
275 (copy paste this into your browser's address field after the host:port)
277 /application?vm=Debian+7&app=Browser
279 (e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
286 if __name__ == "__main__":
287 server = web.application(opensecurity_urls, globals())