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)
127 res = '{ "fetch.log": " ' + trace_file_name.replace('\\', '\\\\') + '" }'
131 """OpenSecurity '/init' handler
133 - GET: Do initial import of OsecVM.ova
137 log_call(web.ctx.environ)
140 trace_file_name = os.path.join(tempfile.gettempdir(), 'OpenSecurity_initial_import.log')
141 trace_file = open(trace_file_name, 'w+')
143 vm_image = Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/OsecVM.ova'
144 initial_import_script = '/OpenSecurity/bin/initial_vm.sh \'' + vm_image + '\''
145 Cygwin.bashExecute(initial_import_script, wait_return = False, stdout = trace_file, stderr = trace_file)
147 res = '{ "init.log": " ' + trace_file_name.replace('\\', '\\\\') + '" }'
151 class os_initial_image:
152 """OpenSecurity '/initial_image' handler
154 - GET: Return what we have as initial image.
158 log_call(web.ctx.environ)
160 t = os.path.join(gvm_mgr.systemProperties['Default machine folder'], 'OsecVM.ova')
162 if os.path.isfile(t):
163 res = '{"initial_template": { '
164 res += '"name": "OsecVM.ova", '
165 res += '"path": "' + t.replace('\\', '\\\\') + '", '
166 res += '"size": ' + str(os.path.getsize(t)) + ', '
167 res += '"date": ' + str(os.path.getmtime(t)) + ''
173 """OpenSecurity '/' handler
175 - GET: give information about current installation.
179 log_call(web.ctx.environ)
181 res = '{"os_server": { '
182 res += '"version": "' + __version__ + '", '
183 res += '"virtual box systemproperties": ' + str(gvm_mgr.systemProperties).replace("'", '"') + ', '
184 res += '"current temporary folder": "' + tempfile.gettempdir().replace('\\', '\\\\') + '" '
190 """OpenSecurity '/sdvms/[VM]' handler
192 - GET: Information about a specific SecurityVM
193 - DELETE: Remove a specific
197 log_call(web.ctx.environ)
199 return gvm_mgr.getVMInfo(name)
201 def DELETE(self, name):
202 log_call(web.ctx.environ)
204 return gvm_mgr.removeVM(name)
207 class os_sdvm_application:
208 """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
210 - GET: start application with given command in the VM.
213 def GET(self, name, command):
214 log_call(web.ctx.environ)
216 command = '/' + command
217 result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key' )
218 self.poweroffVM(name)
219 return gvm_mgr.removeVM(name)
223 """OpenSecurity '/sdvms/[VM]/ip' handler
225 - GET: give IP of SecurityVM.
229 log_call(web.ctx.environ)
231 return gvm_mgr.getHostOnlyIP(name)
235 """OpenSecurity '/sdvms/[VM]/start' handler
237 - GET: Start specific SecuirtyVM.
241 log_call(web.ctx.environ)
243 return gvm_mgr.startVM(name)
247 """OpenSecurity '/sdvms/[VM]/stop' handler
249 - GET: stop specific Secuirty VM.
253 log_call(web.ctx.environ)
255 return gvm_mgr.stopVM(name)
259 """OpenSecurity '/sdvms' handler
261 - GET: list all available secuirty VMs.
262 - POST: create new security vm.
266 """get the list of SDVMs"""
267 log_call(web.ctx.environ)
269 return gvm_mgr.listSDVM()
272 """create a new SDVM"""
273 log_call(web.ctx.environ)
277 name = gvm_mgr.generateSDVMName()
279 gvm_mgr.createVM(name)
281 raise web.internalerror()
287 """OpenSecurity '/setup' handler
289 - GET: Give user some info how to setup the OpenSecurity envvironment
294 log_call(web.ctx.environ)
299 <h1>Setup OpenSecurity</h1>
300 In order to setup OpenSecurity an inital VM image has to be downloaded and imported:<br/>
302 <li>Download initial VM image: <a href="/fetch_initial_image">fetch_initial_image</a>
303 <li>Import initial VM: <a href="/init">init</a>
312 """OpenSecurity '/terminate' handler
314 - GET: terminate the opensecurityd.
316 YES: this here is bonkers. But the web.py http
317 server runs infinite until a SystemExit exception
318 or KeyboardInterrupt exception is raised.
320 see: site-packages/web/httpserver.py - line 157ff
321 see: site-packages/web/wsgiserver/__init__.py - line 1682ff
323 So, we invoke a sys.exit(0) here to trigger server.stop().
325 TODO: need to find a better way doing this, and not via the
326 REST api. Maybe hack web.py server code?
330 log_call(web.ctx.environ)
338 class os_update_template:
339 """OpenSecurity '/update_template' handler
341 - GET: update template vm
345 #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
347 log_call(web.ctx.environ)
348 return gvm_mgr.updateTemplate()
352 """OpenSecurity '/vms/[VM]' handler
354 - GET: list information of arbitrary VM.
358 log_call(web.ctx.environ)
360 return gvm_mgr.getVMInfo(name)
364 """OpenSecurity '/vms' handler
366 - GET: list all (also non Security) VMs.
370 log_call(web.ctx.environ)
372 return str(gvm_mgr.listVM()).replace("'",'"')
375 def log_call(web_environ):
376 """log the incoming call to the REST api"""
378 call = 'REST ' + web_environ['REQUEST_METHOD'] + ' ' + web_environ['REQUEST_URI'] + ' from ' + web_environ['REMOTE_ADDR'] + ':' + web_environ['REMOTE_PORT']
385 """main startup for the opensecuirityd"""
387 logger.debug('Starting OpenSecurity REST server')
389 # ensure a VMManger is yet loaded
391 gvm_mgr = vmmanager.VMManager.getInstance()
393 # tweak sys.argv to control wep.py server start behavior
394 sys.argv = [__file__, "8080"]
395 server = web.application(opensecurity_urls, globals(), autoreload = False)
398 logger.debug('Stopped OpenSecurity REST server')
402 """stop the opensecuirityd"""
404 # calling sys.exit() raises a SystemExit exception
405 # of the WSGI Server to let it wind down
412 if __name__ == "__main__":