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 # ------------------------------------------------------------
48 from environment import Environment
51 # ------------------------------------------------------------
58 """All the URLs we know mapping to class handler"""
60 '/credentials', 'os_credentials',
61 '/keyfile', 'os_keyfile',
62 '/notification', 'os_notification',
63 '/password', 'os_password',
68 # ------------------------------------------------------------
72 """The REST server object"""
76 # ------------------------------------------------------------
82 """OpenSecurity '/credentials' handler.
84 This is called on GET /credentials?text=TEXT.
85 Ideally this should pop up a user dialog to insert his
86 credentials based the given TEXT.
95 if not "text" in args:
96 raise web.badrequest('no text given')
99 remote_ip = web.ctx.environ['REMOTE_ADDR']
101 # create the process which queries the user
102 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
103 process_command = [sys.executable, dlg_image, 'credentials', args.text]
104 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
106 # run process result handling in seprate thread (not to block main one)
107 bouncer = ProcessResultBouncer(process, remote_ip, '/credentials')
110 return 'user queried for credentials'
115 """OpenSecurity '/keyfile' handler.
117 This is called on GET /keyfile?text=TEXT.
118 Ideally this should pop up a user dialog to insert his
119 password along with a keyfile.
128 if not "text" in args:
129 raise web.badrequest('no text given')
132 remote_ip = web.ctx.environ['REMOTE_ADDR']
134 # create the process which queries the user
135 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
136 process_command = [sys.executable, dlg_image, 'keyfile', args.text]
137 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
139 # run process result handling in seprate thread (not to block main one)
140 bouncer = ProcessResultBouncer(process, remote_ip, '/keyfile')
143 return 'user queried for password and keyfile'
146 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 ['information', 'warning', 'critical']:
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)
180 """OpenSecurity '/password' handler.
182 This is called on GET /password?text=TEXT.
183 Ideally this should pop up a user dialog to insert his
184 password based device name.
193 if not "text" in args:
194 raise web.badrequest('no text given')
197 remote_ip = web.ctx.environ['REMOTE_ADDR']
199 # create the process which queries the user
200 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
201 process_command = [sys.executable, dlg_image, 'password', args.text]
202 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
204 # run process result handling in seprate thread (not to block main one)
205 bouncer = ProcessResultBouncer(process, remote_ip, '/password')
208 return 'user queried for password'
213 """OpenSecurity '/' handler"""
217 res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % __version__
219 # add some sample links
225 (copy paste this into your browser's address field after the host:port)
227 /password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
229 (eg.: http://127.0.0.1:8090/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0))
230 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
233 Request a combination of user and password:
234 (copy paste this into your browser's address field after the host:port)
236 /credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
238 (eg.: http://127.0.0.1:8090/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.)
239 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
242 Request a combination of password and keyfile:
243 (copy paste this into your browser's address field after the host:port)
245 /keyfile?text=Your%20private%20RSA%20Keyfile%3A
247 (eg.: http://127.0.0.1:8090//keyfile?text=Your%20private%20RSA%20Keyfile%3A)
248 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
252 (copy paste this into your browser's address field after the host:port)
254 /application?vm=Debian+7&app=Browser
256 (e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
262 class ProcessResultBouncer(threading.Thread):
264 """A class to post the result of a given process - assuming it to be in JSON - to a REST Api."""
266 def __init__(self, process, remote_ip, resource):
270 threading.Thread.__init__(self)
271 self._process = process
272 self._remote_ip = remote_ip
273 self._resource = resource
286 # invoke the user dialog as a subprocess
287 result = self._process.communicate()[0]
288 if self._process.returncode != 0:
289 print 'user request has been aborted.'
292 # all ok, tell send request back appropriate destination
294 j = json.loads(result)
296 print 'error in password parsing'
299 # TODO: it would be WAY easier and secure if we just
300 # add the result json to a HTTP-POST here.
301 url_addr = 'http://' + self._remote_ip + ':58080' + self._resource
302 url = url_addr + '?' + urllib.urlencode(j)
303 req = urllib2.Request(url)
305 res = urllib2.urlopen(req)
307 print 'failed to contact: ' + url_addr
311 class RESTServerThread(threading.Thread):
313 """Thread for serving the REST API."""
315 def __init__(self, port):
318 threading.Thread.__init__(self)
334 def is_already_running(port = 8090):
336 """check if this is started twice"""
339 s = socket.create_connection(('127.0.0.1', port), 0.5)
348 """Start the REST server"""
352 # trick the web.py server
353 sys.argv = [__file__, str(port)]
354 server = web.application(opensecurity_urls, globals())
358 def serve(port = 8090, background = False):
360 """Start serving the REST Api
361 port ... port number to listen on
362 background ... cease into background (spawn thread) and return immediately"""
364 # start threaded or direct version
365 if background == True:
366 t = RESTServerThread(port)
373 """Stop serving the REST Api"""
383 if __name__ == "__main__":