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 # ------------------------------------------------------------
50 from environment import Environment
53 # ------------------------------------------------------------
60 """All the URLs we know mapping to class handler"""
62 '/credentials', 'os_credentials',
63 '/keyfile', 'os_keyfile',
65 '/notification', 'os_notification',
66 '/password', 'os_password',
71 # ------------------------------------------------------------
75 """The REST server object"""
79 # ------------------------------------------------------------
85 """OpenSecurity '/credentials' handler.
87 This is called on GET /credentials?text=TEXT.
88 Ideally this should pop up a user dialog to insert his
89 credentials based the given TEXT.
98 if not "text" in args:
99 raise web.badrequest('no text given')
102 remote_ip = web.ctx.environ['REMOTE_ADDR']
104 # create the process which queries the user
105 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
106 process_command = [sys.executable, dlg_image, 'credentials', args.text]
107 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
109 # run process result handling in seprate thread (not to block main one)
110 bouncer = ProcessResultBouncer(process, remote_ip, '/credentials')
113 return 'user queried for credentials'
118 """OpenSecurity '/keyfile' handler.
120 This is called on GET /keyfile?text=TEXT.
121 Ideally this should pop up a user dialog to insert his
122 password along with a keyfile.
131 if not "text" in args:
132 raise web.badrequest('no text given')
135 remote_ip = web.ctx.environ['REMOTE_ADDR']
137 # create the process which queries the user
138 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
139 process_command = [sys.executable, dlg_image, 'keyfile', args.text]
140 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
142 # run process result handling in seprate thread (not to block main one)
143 bouncer = ProcessResultBouncer(process, remote_ip, '/keyfile')
146 return 'user queried for password and keyfile'
151 """OpenSecurity '/log' handler.
153 This is called on GET or POST on the log function /log
166 args['user'] = getpass.getuser()
167 args['system'] = platform.node() + " " + platform.system() + " " + platform.release()
170 url_addr = 'http://GIMME-SERVER-TO-LOG-TO/log'
172 # by provided a 'data' we turn this into a POST statement
173 d = urllib.urlencode(args)
174 req = urllib2.Request(url_addr, d)
176 res = urllib2.urlopen(req)
178 print('failed to contact: ' + url_addr)
179 print('log data: ' + d)
185 class os_notification:
187 """OpenSecurity '/notification' handler.
189 This is called on GET /notification?msgtype=TYPE&text=TEXT.
190 This will pop up an OpenSecurity notifcation window
199 if not "msgtype" in args:
200 raise web.badrequest('no msgtype given')
202 if not args.msgtype in ['information', 'warning', 'critical']:
203 raise web.badrequest('Unknown value for msgtype')
206 if not "text" in args:
207 raise web.badrequest('no text given')
209 # invoke the user dialog as a subprocess
210 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
211 process_command = [sys.executable, dlg_image, 'notification-' + args.msgtype, args.text]
212 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
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 # create the process which queries the user
239 dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
240 process_command = [sys.executable, dlg_image, 'password', args.text]
241 process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
243 # run process result handling in seprate thread (not to block main one)
244 bouncer = ProcessResultBouncer(process, remote_ip, '/password')
247 return 'user queried for password'
252 """OpenSecurity '/' handler"""
256 res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % __version__
258 # add some sample links
264 (copy paste this into your browser's address field after the host:port)
266 /password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
268 (eg.: http://127.0.0.1:8090/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0))
269 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
272 Request a combination of user and password:
273 (copy paste this into your browser's address field after the host:port)
275 /credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
277 (eg.: http://127.0.0.1:8090/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.)
278 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
281 Request a combination of password and keyfile:
282 (copy paste this into your browser's address field after the host:port)
284 /keyfile?text=Your%20private%20RSA%20Keyfile%3A
286 (eg.: http://127.0.0.1:8090//keyfile?text=Your%20private%20RSA%20Keyfile%3A)
287 NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
291 (copy paste this into your browser's address field after the host:port)
293 /application?vm=Debian+7&app=Browser
295 (e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
301 class ProcessResultBouncer(threading.Thread):
303 """A class to post the result of a given process - assuming it to be in JSON - to a REST Api."""
305 def __init__(self, process, remote_ip, resource):
309 threading.Thread.__init__(self)
310 self._process = process
311 self._remote_ip = remote_ip
312 self._resource = resource
325 # invoke the user dialog as a subprocess
326 result = self._process.communicate()[0]
327 if self._process.returncode != 0:
328 print 'user request has been aborted.'
331 # all ok, tell send request back appropriate destination
333 j = json.loads(result)
335 print 'error in password parsing'
338 # TODO: it would be WAY easier and secure if we just
339 # add the result json to a HTTP-POST here.
340 url_addr = 'http://' + self._remote_ip + ':58080' + self._resource
342 # by provided a 'data' we turn this into a POST statement
343 req = urllib2.Request(url_addr, urllib.urlencode(j))
345 res = urllib2.urlopen(req)
347 print 'failed to contact: ' + url_addr
351 class RESTServerThread(threading.Thread):
353 """Thread for serving the REST API."""
355 def __init__(self, port):
358 threading.Thread.__init__(self)
374 def is_already_running(port = 8090):
376 """check if this is started twice"""
379 s = socket.create_connection(('127.0.0.1', port), 0.5)
388 """Start the REST server"""
392 # trick the web.py server
393 sys.argv = [__file__, str(port)]
394 server = web.application(opensecurity_urls, globals())
398 def serve(port = 8090, background = False):
400 """Start serving the REST Api
401 port ... port number to listen on
402 background ... cease into background (spawn thread) and return immediately"""
404 # start threaded or direct version
405 if background == True:
406 t = RESTServerThread(port)
413 """Stop serving the REST Api"""
423 if __name__ == "__main__":