om@13
|
1 |
#!/bin/env python
|
om@13
|
2 |
# -*- coding: utf-8 -*-
|
om@13
|
3 |
|
om@13
|
4 |
# ------------------------------------------------------------
|
om@13
|
5 |
# opensecurity_client_restful_server
|
om@13
|
6 |
#
|
om@13
|
7 |
# the OpenSecurity client RESTful server
|
om@13
|
8 |
#
|
om@13
|
9 |
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
|
om@13
|
10 |
#
|
om@13
|
11 |
# Copyright (C) 2013 AIT Austrian Institute of Technology
|
om@13
|
12 |
# AIT Austrian Institute of Technology GmbH
|
om@13
|
13 |
# Donau-City-Strasse 1 | 1220 Vienna | Austria
|
om@13
|
14 |
# http://www.ait.ac.at
|
om@13
|
15 |
#
|
om@13
|
16 |
# This program is free software; you can redistribute it and/or
|
om@13
|
17 |
# modify it under the terms of the GNU General Public License
|
om@13
|
18 |
# as published by the Free Software Foundation version 2.
|
om@13
|
19 |
#
|
om@13
|
20 |
# This program is distributed in the hope that it will be useful,
|
om@13
|
21 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
om@13
|
22 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
om@13
|
23 |
# GNU General Public License for more details.
|
om@13
|
24 |
#
|
om@13
|
25 |
# You should have received a copy of the GNU General Public License
|
om@13
|
26 |
# along with this program; if not, write to the Free Software
|
om@13
|
27 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
om@13
|
28 |
# Boston, MA 02110-1301, USA.
|
om@13
|
29 |
# ------------------------------------------------------------
|
om@13
|
30 |
|
om@13
|
31 |
|
om@13
|
32 |
# ------------------------------------------------------------
|
om@13
|
33 |
# imports
|
om@13
|
34 |
|
oliver@142
|
35 |
import getpass
|
oliver@134
|
36 |
import json
|
om@13
|
37 |
import os
|
om@13
|
38 |
import os.path
|
oliver@142
|
39 |
import platform
|
oliver@136
|
40 |
import socket
|
om@13
|
41 |
import subprocess
|
om@13
|
42 |
import sys
|
om@29
|
43 |
import urllib
|
om@29
|
44 |
import urllib2
|
om@13
|
45 |
import web
|
mb@90
|
46 |
import threading
|
mb@90
|
47 |
import time
|
om@13
|
48 |
|
om@13
|
49 |
# local
|
oliver@144
|
50 |
import __init__ as opensecurity
|
om@13
|
51 |
|
om@13
|
52 |
|
om@13
|
53 |
# ------------------------------------------------------------
|
om@13
|
54 |
# const
|
om@13
|
55 |
|
om@13
|
56 |
|
om@13
|
57 |
"""All the URLs we know mapping to class handler"""
|
om@13
|
58 |
opensecurity_urls = (
|
om@13
|
59 |
'/credentials', 'os_credentials',
|
oliver@134
|
60 |
'/keyfile', 'os_keyfile',
|
oliver@142
|
61 |
'/log', 'os_log',
|
om@29
|
62 |
'/notification', 'os_notification',
|
om@13
|
63 |
'/password', 'os_password',
|
om@13
|
64 |
'/', 'os_root'
|
om@13
|
65 |
)
|
om@13
|
66 |
|
om@13
|
67 |
|
om@13
|
68 |
# ------------------------------------------------------------
|
oliver@136
|
69 |
# vars
|
oliver@136
|
70 |
|
oliver@136
|
71 |
|
oliver@136
|
72 |
"""The REST server object"""
|
oliver@136
|
73 |
server = None
|
oliver@136
|
74 |
|
oliver@136
|
75 |
|
oliver@136
|
76 |
# ------------------------------------------------------------
|
om@13
|
77 |
# code
|
om@13
|
78 |
|
om@13
|
79 |
|
oliver@134
|
80 |
class os_credentials:
|
om@31
|
81 |
|
om@13
|
82 |
"""OpenSecurity '/credentials' handler.
|
om@13
|
83 |
|
om@13
|
84 |
This is called on GET /credentials?text=TEXT.
|
om@13
|
85 |
Ideally this should pop up a user dialog to insert his
|
om@13
|
86 |
credentials based the given TEXT.
|
om@13
|
87 |
"""
|
om@13
|
88 |
|
om@13
|
89 |
def GET(self):
|
om@13
|
90 |
|
om@13
|
91 |
# pick the arguments
|
om@13
|
92 |
args = web.input()
|
om@13
|
93 |
|
om@29
|
94 |
# we _need_ a text
|
om@13
|
95 |
if not "text" in args:
|
om@29
|
96 |
raise web.badrequest('no text given')
|
om@13
|
97 |
|
oliver@134
|
98 |
# remember remote ip
|
oliver@134
|
99 |
remote_ip = web.ctx.environ['REMOTE_ADDR']
|
oliver@134
|
100 |
|
oliver@134
|
101 |
# create the process which queries the user
|
oliver@134
|
102 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
|
om@29
|
103 |
process_command = [sys.executable, dlg_image, 'credentials', args.text]
|
oliver@134
|
104 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
om@13
|
105 |
|
oliver@134
|
106 |
# run process result handling in seprate thread (not to block main one)
|
oliver@136
|
107 |
bouncer = ProcessResultBouncer(process, remote_ip, '/credentials')
|
oliver@134
|
108 |
bouncer.start()
|
oliver@134
|
109 |
|
oliver@134
|
110 |
return 'user queried for credentials'
|
oliver@134
|
111 |
|
oliver@134
|
112 |
|
oliver@134
|
113 |
class os_keyfile:
|
oliver@134
|
114 |
|
oliver@134
|
115 |
"""OpenSecurity '/keyfile' handler.
|
oliver@134
|
116 |
|
oliver@134
|
117 |
This is called on GET /keyfile?text=TEXT.
|
oliver@134
|
118 |
Ideally this should pop up a user dialog to insert his
|
oliver@134
|
119 |
password along with a keyfile.
|
oliver@134
|
120 |
"""
|
oliver@134
|
121 |
|
oliver@134
|
122 |
def GET(self):
|
oliver@134
|
123 |
|
oliver@134
|
124 |
# pick the arguments
|
oliver@134
|
125 |
args = web.input()
|
oliver@134
|
126 |
|
oliver@134
|
127 |
# we _need_ a text
|
oliver@134
|
128 |
if not "text" in args:
|
oliver@134
|
129 |
raise web.badrequest('no text given')
|
oliver@134
|
130 |
|
oliver@134
|
131 |
# remember remote ip
|
oliver@134
|
132 |
remote_ip = web.ctx.environ['REMOTE_ADDR']
|
oliver@134
|
133 |
|
oliver@134
|
134 |
# create the process which queries the user
|
oliver@134
|
135 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
|
oliver@134
|
136 |
process_command = [sys.executable, dlg_image, 'keyfile', args.text]
|
oliver@134
|
137 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
oliver@134
|
138 |
|
oliver@134
|
139 |
# run process result handling in seprate thread (not to block main one)
|
oliver@136
|
140 |
bouncer = ProcessResultBouncer(process, remote_ip, '/keyfile')
|
oliver@134
|
141 |
bouncer.start()
|
oliver@134
|
142 |
|
oliver@134
|
143 |
return 'user queried for password and keyfile'
|
om@13
|
144 |
|
om@13
|
145 |
|
oliver@142
|
146 |
class os_log:
|
oliver@142
|
147 |
|
oliver@142
|
148 |
"""OpenSecurity '/log' handler.
|
oliver@142
|
149 |
|
oliver@142
|
150 |
This is called on GET or POST on the log function /log
|
oliver@142
|
151 |
"""
|
oliver@142
|
152 |
|
oliver@142
|
153 |
def GET(self):
|
oliver@142
|
154 |
|
oliver@142
|
155 |
# pick the arguments
|
oliver@142
|
156 |
self.POST()
|
oliver@142
|
157 |
|
oliver@142
|
158 |
|
oliver@142
|
159 |
def POST(self):
|
oliver@142
|
160 |
|
oliver@142
|
161 |
# pick the arguments
|
oliver@142
|
162 |
args = web.input()
|
oliver@142
|
163 |
args['user'] = getpass.getuser()
|
oliver@142
|
164 |
args['system'] = platform.node() + " " + platform.system() + " " + platform.release()
|
oliver@142
|
165 |
|
oliver@142
|
166 |
# bounce log data
|
oliver@142
|
167 |
url_addr = 'http://GIMME-SERVER-TO-LOG-TO/log'
|
oliver@142
|
168 |
|
oliver@142
|
169 |
# by provided a 'data' we turn this into a POST statement
|
oliver@142
|
170 |
d = urllib.urlencode(args)
|
oliver@142
|
171 |
req = urllib2.Request(url_addr, d)
|
oliver@142
|
172 |
try:
|
oliver@142
|
173 |
res = urllib2.urlopen(req)
|
oliver@142
|
174 |
except:
|
oliver@142
|
175 |
print('failed to contact: ' + url_addr)
|
oliver@142
|
176 |
print('log data: ' + d)
|
oliver@142
|
177 |
return "Failed"
|
oliver@142
|
178 |
|
oliver@142
|
179 |
return "Ok"
|
oliver@142
|
180 |
|
oliver@142
|
181 |
|
om@29
|
182 |
class os_notification:
|
oliver@134
|
183 |
|
om@29
|
184 |
"""OpenSecurity '/notification' handler.
|
om@29
|
185 |
|
om@29
|
186 |
This is called on GET /notification?msgtype=TYPE&text=TEXT.
|
om@29
|
187 |
This will pop up an OpenSecurity notifcation window
|
om@29
|
188 |
"""
|
om@29
|
189 |
|
om@29
|
190 |
def GET(self):
|
om@29
|
191 |
|
om@29
|
192 |
# pick the arguments
|
om@29
|
193 |
args = web.input()
|
om@29
|
194 |
|
om@29
|
195 |
# we _need_ a type
|
om@29
|
196 |
if not "msgtype" in args:
|
om@29
|
197 |
raise web.badrequest('no msgtype given')
|
om@29
|
198 |
|
oliver@134
|
199 |
if not args.msgtype in ['information', 'warning', 'critical']:
|
om@29
|
200 |
raise web.badrequest('Unknown value for msgtype')
|
om@29
|
201 |
|
om@29
|
202 |
# we _need_ a text
|
om@29
|
203 |
if not "text" in args:
|
om@29
|
204 |
raise web.badrequest('no text given')
|
om@29
|
205 |
|
om@29
|
206 |
# invoke the user dialog as a subprocess
|
om@29
|
207 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
|
om@29
|
208 |
process_command = [sys.executable, dlg_image, 'notification-' + args.msgtype, args.text]
|
om@29
|
209 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
oliver@134
|
210 |
|
om@29
|
211 |
return "Ok"
|
om@29
|
212 |
|
om@29
|
213 |
|
om@13
|
214 |
class os_password:
|
oliver@134
|
215 |
|
om@13
|
216 |
"""OpenSecurity '/password' handler.
|
om@13
|
217 |
|
om@13
|
218 |
This is called on GET /password?text=TEXT.
|
om@13
|
219 |
Ideally this should pop up a user dialog to insert his
|
om@13
|
220 |
password based device name.
|
om@13
|
221 |
"""
|
om@13
|
222 |
|
om@13
|
223 |
def GET(self):
|
om@13
|
224 |
|
om@13
|
225 |
# pick the arguments
|
om@13
|
226 |
args = web.input()
|
om@13
|
227 |
|
om@29
|
228 |
# we _need_ a text
|
om@13
|
229 |
if not "text" in args:
|
om@29
|
230 |
raise web.badrequest('no text given')
|
om@13
|
231 |
|
om@29
|
232 |
# remember remote ip
|
om@29
|
233 |
remote_ip = web.ctx.environ['REMOTE_ADDR']
|
om@29
|
234 |
|
oliver@134
|
235 |
# create the process which queries the user
|
oliver@134
|
236 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
|
oliver@134
|
237 |
process_command = [sys.executable, dlg_image, 'password', args.text]
|
oliver@134
|
238 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
om@29
|
239 |
|
oliver@134
|
240 |
# run process result handling in seprate thread (not to block main one)
|
oliver@136
|
241 |
bouncer = ProcessResultBouncer(process, remote_ip, '/password')
|
oliver@134
|
242 |
bouncer.start()
|
oliver@134
|
243 |
|
oliver@134
|
244 |
return 'user queried for password'
|
om@13
|
245 |
|
om@13
|
246 |
|
om@13
|
247 |
class os_root:
|
oliver@134
|
248 |
|
om@13
|
249 |
"""OpenSecurity '/' handler"""
|
om@13
|
250 |
|
om@13
|
251 |
def GET(self):
|
om@13
|
252 |
|
oliver@144
|
253 |
res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % opensecurity.__version__
|
om@13
|
254 |
|
om@13
|
255 |
# add some sample links
|
om@13
|
256 |
res = res + """
|
om@13
|
257 |
|
om@13
|
258 |
USAGE EXAMPLES:
|
om@13
|
259 |
|
om@13
|
260 |
Request a password:
|
om@13
|
261 |
(copy paste this into your browser's address field after the host:port)
|
om@13
|
262 |
|
om@13
|
263 |
/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
|
om@13
|
264 |
|
om@13
|
265 |
(eg.: http://127.0.0.1:8090/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0))
|
om@13
|
266 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
om@13
|
267 |
|
om@13
|
268 |
|
om@13
|
269 |
Request a combination of user and password:
|
om@13
|
270 |
(copy paste this into your browser's address field after the host:port)
|
om@13
|
271 |
|
om@13
|
272 |
/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
|
om@13
|
273 |
|
om@13
|
274 |
(eg.: http://127.0.0.1:8090/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.)
|
om@13
|
275 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
om@13
|
276 |
|
om@13
|
277 |
|
oliver@134
|
278 |
Request a combination of password and keyfile:
|
oliver@134
|
279 |
(copy paste this into your browser's address field after the host:port)
|
oliver@134
|
280 |
|
oliver@134
|
281 |
/keyfile?text=Your%20private%20RSA%20Keyfile%3A
|
oliver@134
|
282 |
|
oliver@134
|
283 |
(eg.: http://127.0.0.1:8090//keyfile?text=Your%20private%20RSA%20Keyfile%3A)
|
oliver@134
|
284 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
oliver@134
|
285 |
|
oliver@134
|
286 |
|
om@13
|
287 |
Start a Browser:
|
om@13
|
288 |
(copy paste this into your browser's address field after the host:port)
|
om@13
|
289 |
|
om@13
|
290 |
/application?vm=Debian+7&app=Browser
|
om@13
|
291 |
|
om@13
|
292 |
(e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
|
om@13
|
293 |
"""
|
om@13
|
294 |
|
om@13
|
295 |
return res
|
om@13
|
296 |
|
om@13
|
297 |
|
oliver@136
|
298 |
class ProcessResultBouncer(threading.Thread):
|
oliver@134
|
299 |
|
oliver@136
|
300 |
"""A class to post the result of a given process - assuming it to be in JSON - to a REST Api."""
|
oliver@134
|
301 |
|
oliver@134
|
302 |
def __init__(self, process, remote_ip, resource):
|
oliver@134
|
303 |
|
oliver@134
|
304 |
"""ctor"""
|
oliver@134
|
305 |
|
oliver@134
|
306 |
threading.Thread.__init__(self)
|
oliver@134
|
307 |
self._process = process
|
oliver@134
|
308 |
self._remote_ip = remote_ip
|
oliver@134
|
309 |
self._resource = resource
|
oliver@134
|
310 |
|
oliver@134
|
311 |
|
oliver@134
|
312 |
def stop(self):
|
oliver@134
|
313 |
|
oliver@134
|
314 |
"""stop thread"""
|
oliver@134
|
315 |
self.running = False
|
oliver@134
|
316 |
|
oliver@134
|
317 |
|
oliver@134
|
318 |
def run(self):
|
oliver@134
|
319 |
|
oliver@134
|
320 |
"""run the thread"""
|
oliver@134
|
321 |
|
oliver@134
|
322 |
# invoke the user dialog as a subprocess
|
oliver@134
|
323 |
result = self._process.communicate()[0]
|
oliver@134
|
324 |
if self._process.returncode != 0:
|
oliver@134
|
325 |
print 'user request has been aborted.'
|
oliver@134
|
326 |
return
|
oliver@134
|
327 |
|
oliver@134
|
328 |
# all ok, tell send request back appropriate destination
|
oliver@134
|
329 |
try:
|
oliver@134
|
330 |
j = json.loads(result)
|
oliver@134
|
331 |
except:
|
oliver@134
|
332 |
print 'error in password parsing'
|
oliver@134
|
333 |
return
|
oliver@134
|
334 |
|
oliver@134
|
335 |
# TODO: it would be WAY easier and secure if we just
|
oliver@134
|
336 |
# add the result json to a HTTP-POST here.
|
oliver@134
|
337 |
url_addr = 'http://' + self._remote_ip + ':58080' + self._resource
|
oliver@142
|
338 |
|
oliver@142
|
339 |
# by provided a 'data' we turn this into a POST statement
|
oliver@142
|
340 |
req = urllib2.Request(url_addr, urllib.urlencode(j))
|
oliver@134
|
341 |
try:
|
oliver@134
|
342 |
res = urllib2.urlopen(req)
|
oliver@134
|
343 |
except:
|
oliver@134
|
344 |
print 'failed to contact: ' + url_addr
|
oliver@134
|
345 |
return
|
oliver@134
|
346 |
|
oliver@134
|
347 |
|
oliver@136
|
348 |
class RESTServerThread(threading.Thread):
|
oliver@136
|
349 |
|
oliver@136
|
350 |
"""Thread for serving the REST API."""
|
oliver@136
|
351 |
|
oliver@136
|
352 |
def __init__(self, port):
|
oliver@136
|
353 |
|
oliver@136
|
354 |
"""ctor"""
|
oliver@136
|
355 |
threading.Thread.__init__(self)
|
oliver@136
|
356 |
self._port = port
|
oliver@136
|
357 |
|
oliver@136
|
358 |
def stop(self):
|
oliver@136
|
359 |
|
oliver@136
|
360 |
"""stop thread"""
|
oliver@136
|
361 |
self.running = False
|
oliver@136
|
362 |
|
oliver@136
|
363 |
|
oliver@136
|
364 |
def run(self):
|
oliver@136
|
365 |
|
oliver@136
|
366 |
"""run the thread"""
|
oliver@136
|
367 |
_serve(self._port)
|
oliver@136
|
368 |
|
oliver@136
|
369 |
|
oliver@136
|
370 |
|
oliver@136
|
371 |
def is_already_running(port = 8090):
|
oliver@136
|
372 |
|
oliver@136
|
373 |
"""check if this is started twice"""
|
oliver@136
|
374 |
|
oliver@136
|
375 |
try:
|
oliver@136
|
376 |
s = socket.create_connection(('127.0.0.1', port), 0.5)
|
oliver@136
|
377 |
except:
|
oliver@136
|
378 |
return False
|
oliver@136
|
379 |
|
oliver@136
|
380 |
return True
|
oliver@136
|
381 |
|
oliver@136
|
382 |
|
oliver@136
|
383 |
def _serve(port):
|
oliver@136
|
384 |
|
oliver@136
|
385 |
"""Start the REST server"""
|
oliver@136
|
386 |
|
oliver@136
|
387 |
global server
|
oliver@136
|
388 |
|
oliver@136
|
389 |
# trick the web.py server
|
oliver@136
|
390 |
sys.argv = [__file__, str(port)]
|
om@13
|
391 |
server = web.application(opensecurity_urls, globals())
|
om@13
|
392 |
server.run()
|
oliver@134
|
393 |
|
oliver@136
|
394 |
|
oliver@136
|
395 |
def serve(port = 8090, background = False):
|
oliver@136
|
396 |
|
oliver@136
|
397 |
"""Start serving the REST Api
|
oliver@136
|
398 |
port ... port number to listen on
|
oliver@136
|
399 |
background ... cease into background (spawn thread) and return immediately"""
|
oliver@136
|
400 |
|
oliver@136
|
401 |
# start threaded or direct version
|
oliver@136
|
402 |
if background == True:
|
oliver@136
|
403 |
t = RESTServerThread(port)
|
oliver@136
|
404 |
t.start()
|
oliver@136
|
405 |
else:
|
oliver@136
|
406 |
_serve(port)
|
oliver@136
|
407 |
|
oliver@136
|
408 |
def stop():
|
oliver@136
|
409 |
|
oliver@136
|
410 |
"""Stop serving the REST Api"""
|
oliver@136
|
411 |
|
oliver@136
|
412 |
global server
|
oliver@136
|
413 |
if server is None:
|
oliver@136
|
414 |
return
|
oliver@136
|
415 |
|
oliver@136
|
416 |
server.stop()
|
oliver@136
|
417 |
|
oliver@136
|
418 |
|
oliver@136
|
419 |
# start
|
oliver@136
|
420 |
if __name__ == "__main__":
|
oliver@136
|
421 |
serve()
|
oliver@136
|
422 |
|