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@134
|
35 |
import json
|
om@13
|
36 |
import os
|
om@13
|
37 |
import os.path
|
oliver@136
|
38 |
import socket
|
om@13
|
39 |
import subprocess
|
om@13
|
40 |
import sys
|
om@29
|
41 |
import urllib
|
om@29
|
42 |
import urllib2
|
om@13
|
43 |
import web
|
mb@90
|
44 |
import threading
|
mb@90
|
45 |
import time
|
om@13
|
46 |
|
om@13
|
47 |
# local
|
om@13
|
48 |
from environment import Environment
|
om@13
|
49 |
|
om@13
|
50 |
|
om@13
|
51 |
# ------------------------------------------------------------
|
om@13
|
52 |
# const
|
om@13
|
53 |
|
om@13
|
54 |
|
oliver@130
|
55 |
__version__ = "0.2.5"
|
om@13
|
56 |
|
om@13
|
57 |
|
om@13
|
58 |
"""All the URLs we know mapping to class handler"""
|
om@13
|
59 |
opensecurity_urls = (
|
om@13
|
60 |
'/credentials', 'os_credentials',
|
oliver@134
|
61 |
'/keyfile', 'os_keyfile',
|
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 |
|
om@29
|
146 |
class os_notification:
|
oliver@134
|
147 |
|
om@29
|
148 |
"""OpenSecurity '/notification' handler.
|
om@29
|
149 |
|
om@29
|
150 |
This is called on GET /notification?msgtype=TYPE&text=TEXT.
|
om@29
|
151 |
This will pop up an OpenSecurity notifcation window
|
om@29
|
152 |
"""
|
om@29
|
153 |
|
om@29
|
154 |
def GET(self):
|
om@29
|
155 |
|
om@29
|
156 |
# pick the arguments
|
om@29
|
157 |
args = web.input()
|
om@29
|
158 |
|
om@29
|
159 |
# we _need_ a type
|
om@29
|
160 |
if not "msgtype" in args:
|
om@29
|
161 |
raise web.badrequest('no msgtype given')
|
om@29
|
162 |
|
oliver@134
|
163 |
if not args.msgtype in ['information', 'warning', 'critical']:
|
om@29
|
164 |
raise web.badrequest('Unknown value for msgtype')
|
om@29
|
165 |
|
om@29
|
166 |
# we _need_ a text
|
om@29
|
167 |
if not "text" in args:
|
om@29
|
168 |
raise web.badrequest('no text given')
|
om@29
|
169 |
|
om@29
|
170 |
# invoke the user dialog as a subprocess
|
om@29
|
171 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.py')
|
om@29
|
172 |
process_command = [sys.executable, dlg_image, 'notification-' + args.msgtype, args.text]
|
om@29
|
173 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
oliver@134
|
174 |
|
om@29
|
175 |
return "Ok"
|
om@29
|
176 |
|
om@29
|
177 |
|
om@13
|
178 |
class os_password:
|
oliver@134
|
179 |
|
om@13
|
180 |
"""OpenSecurity '/password' handler.
|
om@13
|
181 |
|
om@13
|
182 |
This is called on GET /password?text=TEXT.
|
om@13
|
183 |
Ideally this should pop up a user dialog to insert his
|
om@13
|
184 |
password based device name.
|
om@13
|
185 |
"""
|
om@13
|
186 |
|
om@13
|
187 |
def GET(self):
|
om@13
|
188 |
|
om@13
|
189 |
# pick the arguments
|
om@13
|
190 |
args = web.input()
|
om@13
|
191 |
|
om@29
|
192 |
# we _need_ a text
|
om@13
|
193 |
if not "text" in args:
|
om@29
|
194 |
raise web.badrequest('no text given')
|
om@13
|
195 |
|
om@29
|
196 |
# remember remote ip
|
om@29
|
197 |
remote_ip = web.ctx.environ['REMOTE_ADDR']
|
om@29
|
198 |
|
oliver@134
|
199 |
# create the process which queries the user
|
oliver@134
|
200 |
dlg_image = os.path.join(sys.path[0], 'opensecurity_dialog.pyw')
|
oliver@134
|
201 |
process_command = [sys.executable, dlg_image, 'password', args.text]
|
oliver@134
|
202 |
process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
|
om@29
|
203 |
|
oliver@134
|
204 |
# run process result handling in seprate thread (not to block main one)
|
oliver@136
|
205 |
bouncer = ProcessResultBouncer(process, remote_ip, '/password')
|
oliver@134
|
206 |
bouncer.start()
|
oliver@134
|
207 |
|
oliver@134
|
208 |
return 'user queried for password'
|
om@13
|
209 |
|
om@13
|
210 |
|
om@13
|
211 |
class os_root:
|
oliver@134
|
212 |
|
om@13
|
213 |
"""OpenSecurity '/' handler"""
|
om@13
|
214 |
|
om@13
|
215 |
def GET(self):
|
om@13
|
216 |
|
om@13
|
217 |
res = "OpenSecurity-Client RESTFul Server { \"version\": \"%s\" }" % __version__
|
om@13
|
218 |
|
om@13
|
219 |
# add some sample links
|
om@13
|
220 |
res = res + """
|
om@13
|
221 |
|
om@13
|
222 |
USAGE EXAMPLES:
|
om@13
|
223 |
|
om@13
|
224 |
Request a password:
|
om@13
|
225 |
(copy paste this into your browser's address field after the host:port)
|
om@13
|
226 |
|
om@13
|
227 |
/password?text=Give+me+a+password+for+device+%22My+USB+Drive%22+(ID%3A+32090-AAA-X0)
|
om@13
|
228 |
|
om@13
|
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))
|
om@13
|
230 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
om@13
|
231 |
|
om@13
|
232 |
|
om@13
|
233 |
Request a combination of user and password:
|
om@13
|
234 |
(copy paste this into your browser's address field after the host:port)
|
om@13
|
235 |
|
om@13
|
236 |
/credentials?text=Tell+the+NSA+which+credentials+to+use+in+order+to+avoid+hacking+noise+on+wire.
|
om@13
|
237 |
|
om@13
|
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.)
|
om@13
|
239 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
om@13
|
240 |
|
om@13
|
241 |
|
oliver@134
|
242 |
Request a combination of password and keyfile:
|
oliver@134
|
243 |
(copy paste this into your browser's address field after the host:port)
|
oliver@134
|
244 |
|
oliver@134
|
245 |
/keyfile?text=Your%20private%20RSA%20Keyfile%3A
|
oliver@134
|
246 |
|
oliver@134
|
247 |
(eg.: http://127.0.0.1:8090//keyfile?text=Your%20private%20RSA%20Keyfile%3A)
|
oliver@134
|
248 |
NOTE: check yout taskbar, the dialog window may not pop up in front of your browser window.
|
oliver@134
|
249 |
|
oliver@134
|
250 |
|
om@13
|
251 |
Start a Browser:
|
om@13
|
252 |
(copy paste this into your browser's address field after the host:port)
|
om@13
|
253 |
|
om@13
|
254 |
/application?vm=Debian+7&app=Browser
|
om@13
|
255 |
|
om@13
|
256 |
(e.g. http://127.0.0.1:8090/application?vm=Debian+7&app=Browser)
|
om@13
|
257 |
"""
|
om@13
|
258 |
|
om@13
|
259 |
return res
|
om@13
|
260 |
|
om@13
|
261 |
|
oliver@136
|
262 |
class ProcessResultBouncer(threading.Thread):
|
oliver@134
|
263 |
|
oliver@136
|
264 |
"""A class to post the result of a given process - assuming it to be in JSON - to a REST Api."""
|
oliver@134
|
265 |
|
oliver@134
|
266 |
def __init__(self, process, remote_ip, resource):
|
oliver@134
|
267 |
|
oliver@134
|
268 |
"""ctor"""
|
oliver@134
|
269 |
|
oliver@134
|
270 |
threading.Thread.__init__(self)
|
oliver@134
|
271 |
self._process = process
|
oliver@134
|
272 |
self._remote_ip = remote_ip
|
oliver@134
|
273 |
self._resource = resource
|
oliver@134
|
274 |
|
oliver@134
|
275 |
|
oliver@134
|
276 |
def stop(self):
|
oliver@134
|
277 |
|
oliver@134
|
278 |
"""stop thread"""
|
oliver@134
|
279 |
self.running = False
|
oliver@134
|
280 |
|
oliver@134
|
281 |
|
oliver@134
|
282 |
def run(self):
|
oliver@134
|
283 |
|
oliver@134
|
284 |
"""run the thread"""
|
oliver@134
|
285 |
|
oliver@134
|
286 |
# invoke the user dialog as a subprocess
|
oliver@134
|
287 |
result = self._process.communicate()[0]
|
oliver@134
|
288 |
if self._process.returncode != 0:
|
oliver@134
|
289 |
print 'user request has been aborted.'
|
oliver@134
|
290 |
return
|
oliver@134
|
291 |
|
oliver@134
|
292 |
# all ok, tell send request back appropriate destination
|
oliver@134
|
293 |
try:
|
oliver@134
|
294 |
j = json.loads(result)
|
oliver@134
|
295 |
except:
|
oliver@134
|
296 |
print 'error in password parsing'
|
oliver@134
|
297 |
return
|
oliver@134
|
298 |
|
oliver@134
|
299 |
# TODO: it would be WAY easier and secure if we just
|
oliver@134
|
300 |
# add the result json to a HTTP-POST here.
|
oliver@134
|
301 |
url_addr = 'http://' + self._remote_ip + ':58080' + self._resource
|
oliver@134
|
302 |
url = url_addr + '?' + urllib.urlencode(j)
|
oliver@134
|
303 |
req = urllib2.Request(url)
|
oliver@134
|
304 |
try:
|
oliver@134
|
305 |
res = urllib2.urlopen(req)
|
oliver@134
|
306 |
except:
|
oliver@134
|
307 |
print 'failed to contact: ' + url_addr
|
oliver@134
|
308 |
return
|
oliver@134
|
309 |
|
oliver@134
|
310 |
|
oliver@136
|
311 |
class RESTServerThread(threading.Thread):
|
oliver@136
|
312 |
|
oliver@136
|
313 |
"""Thread for serving the REST API."""
|
oliver@136
|
314 |
|
oliver@136
|
315 |
def __init__(self, port):
|
oliver@136
|
316 |
|
oliver@136
|
317 |
"""ctor"""
|
oliver@136
|
318 |
threading.Thread.__init__(self)
|
oliver@136
|
319 |
self._port = port
|
oliver@136
|
320 |
|
oliver@136
|
321 |
def stop(self):
|
oliver@136
|
322 |
|
oliver@136
|
323 |
"""stop thread"""
|
oliver@136
|
324 |
self.running = False
|
oliver@136
|
325 |
|
oliver@136
|
326 |
|
oliver@136
|
327 |
def run(self):
|
oliver@136
|
328 |
|
oliver@136
|
329 |
"""run the thread"""
|
oliver@136
|
330 |
_serve(self._port)
|
oliver@136
|
331 |
|
oliver@136
|
332 |
|
oliver@136
|
333 |
|
oliver@136
|
334 |
def is_already_running(port = 8090):
|
oliver@136
|
335 |
|
oliver@136
|
336 |
"""check if this is started twice"""
|
oliver@136
|
337 |
|
oliver@136
|
338 |
try:
|
oliver@136
|
339 |
s = socket.create_connection(('127.0.0.1', port), 0.5)
|
oliver@136
|
340 |
except:
|
oliver@136
|
341 |
return False
|
oliver@136
|
342 |
|
oliver@136
|
343 |
return True
|
oliver@136
|
344 |
|
oliver@136
|
345 |
|
oliver@136
|
346 |
def _serve(port):
|
oliver@136
|
347 |
|
oliver@136
|
348 |
"""Start the REST server"""
|
oliver@136
|
349 |
|
oliver@136
|
350 |
global server
|
oliver@136
|
351 |
|
oliver@136
|
352 |
# trick the web.py server
|
oliver@136
|
353 |
sys.argv = [__file__, str(port)]
|
om@13
|
354 |
server = web.application(opensecurity_urls, globals())
|
om@13
|
355 |
server.run()
|
oliver@134
|
356 |
|
oliver@136
|
357 |
|
oliver@136
|
358 |
def serve(port = 8090, background = False):
|
oliver@136
|
359 |
|
oliver@136
|
360 |
"""Start serving the REST Api
|
oliver@136
|
361 |
port ... port number to listen on
|
oliver@136
|
362 |
background ... cease into background (spawn thread) and return immediately"""
|
oliver@136
|
363 |
|
oliver@136
|
364 |
# start threaded or direct version
|
oliver@136
|
365 |
if background == True:
|
oliver@136
|
366 |
t = RESTServerThread(port)
|
oliver@136
|
367 |
t.start()
|
oliver@136
|
368 |
else:
|
oliver@136
|
369 |
_serve(port)
|
oliver@136
|
370 |
|
oliver@136
|
371 |
def stop():
|
oliver@136
|
372 |
|
oliver@136
|
373 |
"""Stop serving the REST Api"""
|
oliver@136
|
374 |
|
oliver@136
|
375 |
global server
|
oliver@136
|
376 |
if server is None:
|
oliver@136
|
377 |
return
|
oliver@136
|
378 |
|
oliver@136
|
379 |
server.stop()
|
oliver@136
|
380 |
|
oliver@136
|
381 |
|
oliver@136
|
382 |
# start
|
oliver@136
|
383 |
if __name__ == "__main__":
|
oliver@136
|
384 |
serve()
|
oliver@136
|
385 |
|