2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # the opensecurityd as 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 # ------------------------------------------------------------
45 from cygwin import Cygwin
46 from environment import Environment
47 from opensecurity_util import logger
50 # ------------------------------------------------------------
56 """All the URLs we know mapping to class handler"""
58 '/browsing', 'os_browsing', # http://localhost:8080/browsing GET
59 '/fetch_initial_image', 'os_fetch_image', # http://localhost:8080/fetch_initial_image GET
60 '/init', 'os_init', # http://localhost:8080/init GET
61 '/initial_image', 'os_initial_image', # http://localhost:8080/initial_image GET
62 '/sdvms', 'os_sdvms', # http://localhost:8080/sdvms GET, PUT
63 '/sdvms/(.*)/application/(.*)', 'os_sdvm_application', # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND] GET
64 '/sdvms/(.*)/ip', 'os_sdvm_ip', # http://localhost:8080/sdvms/[VMNAME]/ip GET
65 '/sdvms/(.*)/start', 'os_sdvm_start', # http://localhost:8080/sdvms/[VMNAME]/start GET
66 '/sdvms/(.*)/stop', 'os_sdvm_stop', # http://localhost:8080/sdvms/[VMNAME]/stop GET
67 '/sdvms/(.*)', 'os_sdvm', # http://localhost:8080/sdvms/[VMNAME] GET, DELETE
68 '/setup', 'os_setup', # http://localhost:8080/setup GET
69 '/vms', 'os_vms', # http://localhost:8080/vms GET
70 '/vms/(.*)', 'os_vm', # http://localhost:8080/vms/[VMNAME] GET
71 '/update_template', 'os_update_template', # http://localhost:8080/update_template GET
72 '/terminate', 'os_terminate', # http://localhost:8080/terminate GET
73 '/', 'os_root' # http://localhost:8080/ GET
77 # ------------------------------------------------------------
80 # Global VMManager instance
84 # ------------------------------------------------------------
89 """OpenSecurity '/browsing' handler
91 - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
95 log_call(web.ctx.environ)
98 result = gvm_mgr.handleBrowsingRequest()
101 raise web.internalerror()
104 class os_fetch_image:
105 """OpenSecurity '/fetch_initial_image' handler
107 - GET: fetch the initial image from the X-Net Servers
108 The initial image is stored in the
109 Virtual Box default machine path.
110 The result to this call is a temprary file
111 which shows the progress (or error state)
117 log_call(web.ctx.environ)
120 trace_file_name = os.path.join(tempfile.gettempdir(), 'OpenSecurity_fetch_image.log')
121 trace_file = open(trace_file_name, 'w+')
123 machine_folder = Cygwin.cygPath(gvm_mgr.getMachineFolder())
124 download_initial_image_script = '/OpenSecurity/bin/download_initial_image.sh \'' + machine_folder + '\''
125 Cygwin.bashExecute(download_initial_image_script, wait_return = False, stdout = trace_file, stderr = trace_file)
131 Fetch OpenSecurity image log ({0}):<br/>
132 <iframe src="file://{0}" width="100%" height="100%"></iframe>
136 return page.format(trace_file_name)
140 """OpenSecurity '/init' handler
142 - GET: Do initial import of OsecVM.ova
146 log_call(web.ctx.environ)
149 trace_file_name = os.path.join(tempfile.gettempdir(), 'OpenSecurity_initial_import.log')
150 trace_file = open(trace_file_name, 'w+')
152 vm_image = Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/OsecVM.ova'
153 initial_import_script = '/OpenSecurity/bin/initial_vm.sh \'' + vm_image + '\''
154 Cygwin.bashExecute(initial_import_script, wait_return = False, stdout = trace_file, stderr = trace_file)
160 Import initial OpenSecurity image log ({0}):<br/>
161 <iframe src="file://{0}" width="100%" height="100%"></iframe>
165 return page.format(trace_file_name)
168 class os_initial_image:
169 """OpenSecurity '/initial_image' handler
171 - GET: Return what we have as initial image.
175 log_call(web.ctx.environ)
177 t = os.path.join(gvm_mgr.systemProperties['Default machine folder'], 'OsecVM.ova')
179 if os.path.isfile(t):
180 res = '{"initial_template": { '
181 res += '"name": "OsecVM.ova", '
182 res += '"path": "' + t.replace('\\', '\\\\') + '", '
183 res += '"size": ' + str(os.path.getsize(t)) + ', '
184 res += '"date": ' + str(os.path.getmtime(t)) + ''
190 """OpenSecurity '/' handler
192 - GET: give information about current installation.
196 log_call(web.ctx.environ)
198 res = '{"os_server": { '
199 res += '"version": "' + __version__ + '", '
200 res += '"virtual box systemproperties": ' + str(gvm_mgr.systemProperties).replace("'", '"') + ', '
201 res += '"current temporary folder": "' + tempfile.gettempdir().replace('\\', '\\\\') + '" '
207 """OpenSecurity '/sdvms/[VM]' handler
209 - GET: Information about a specific SecurityVM
210 - DELETE: Remove a specific
214 log_call(web.ctx.environ)
216 return gvm_mgr.getVMInfo(name)
218 def DELETE(self, name):
219 log_call(web.ctx.environ)
221 return gvm_mgr.removeVM(name)
224 class os_sdvm_application:
225 """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
227 - GET: start application with given command in the VM.
230 def GET(self, name, command):
231 log_call(web.ctx.environ)
233 command = '/' + command
234 result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key' )
235 self.poweroffVM(name)
236 return gvm_mgr.removeVM(name)
240 """OpenSecurity '/sdvms/[VM]/ip' handler
242 - GET: give IP of SecurityVM.
246 log_call(web.ctx.environ)
248 return gvm_mgr.getHostOnlyIP(name)
252 """OpenSecurity '/sdvms/[VM]/start' handler
254 - GET: Start specific SecuirtyVM.
258 log_call(web.ctx.environ)
260 return gvm_mgr.startVM(name)
264 """OpenSecurity '/sdvms/[VM]/stop' handler
266 - GET: stop specific Secuirty VM.
270 log_call(web.ctx.environ)
272 return gvm_mgr.stopVM(name)
276 """OpenSecurity '/sdvms' handler
278 - GET: list all available secuirty VMs.
279 - POST: create new security vm.
283 """get the list of SDVMs"""
284 log_call(web.ctx.environ)
286 return gvm_mgr.listSDVM()
289 """create a new SDVM"""
290 log_call(web.ctx.environ)
294 name = gvm_mgr.generateSDVMName()
296 gvm_mgr.createVM(name)
298 raise web.internalerror()
304 """OpenSecurity '/setup' handler
306 - GET: Give user some info how to setup the OpenSecurity envvironment
311 log_call(web.ctx.environ)
316 <h1>Setup OpenSecurity</h1>
317 In order to setup OpenSecurity an inital VM image has to be downloaded and imported:<br/>
319 <li>Download initial VM image: <a href="/fetch_initial_image">fetch_initial_image</a>
320 <li>Import initial VM: <a href="/init">init</a>
329 """OpenSecurity '/terminate' handler
331 - GET: terminate the opensecurityd.
333 YES: this here is bonkers. But the web.py http
334 server runs infinite until a SystemExit exception
335 or KeyboardInterrupt exception is raised.
337 see: site-packages/web/httpserver.py - line 157ff
338 see: site-packages/web/wsgiserver/__init__.py - line 1682ff
340 So, we invoke a sys.exit(0) here to trigger server.stop().
342 TODO: need to find a better way doing this, and not via the
343 REST api. Maybe hack web.py server code?
347 log_call(web.ctx.environ)
355 class os_update_template:
356 """OpenSecurity '/update_template' handler
358 - GET: update template vm
362 #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
364 log_call(web.ctx.environ)
365 return gvm_mgr.updateTemplate()
369 """OpenSecurity '/vms/[VM]' handler
371 - GET: list information of arbitrary VM.
375 log_call(web.ctx.environ)
377 return gvm_mgr.getVMInfo(name)
381 """OpenSecurity '/vms' handler
383 - GET: list all (also non Security) VMs.
387 log_call(web.ctx.environ)
389 return gvm_mgr.listVM()
392 def log_call(web_environ):
393 """log the incoming call to the REST api"""
395 call = 'REST ' + web_environ['REQUEST_METHOD'] + ' ' + web_environ['REQUEST_URI'] + ' from ' + web_environ['REMOTE_ADDR'] + ':' + web_environ['REMOTE_PORT']
402 """main startup for the opensecuirityd"""
404 logger.debug('Starting OpenSecurity REST server')
406 # ensure a VMManger is yet loaded
408 gvm_mgr = vmmanager.VMManager.getInstance()
410 # tweak sys.argv to control wep.py server start behavior
411 sys.argv = [__file__, "8080"]
412 server = web.application(opensecurity_urls, globals(), autoreload = False)
415 logger.debug('Stopped OpenSecurity REST server')
419 """stop the opensecuirityd"""
421 # calling sys.exit() raises a SystemExit exception
422 # of the WSGI Server to let it wind down
429 if __name__ == "__main__":