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 # ------------------------------------------------------------
40 from cygwin import Cygwin
42 from vmmanager import VMManager
45 from environment import Environment
46 from opensecurity_util import logger
49 # ------------------------------------------------------------
55 """All the URLs we know mapping to class handler"""
57 '/device_change', 'os_device_change', # http://localhost:8080/device_change GET
58 '/sdvm_started', 'os_sdvm_started', # http://localhost:8080/sdvm_started GET
59 '/browsing', 'os_browsing', # http://localhost:8080/browsing GET
60 '/sdvms', 'os_sdvms', # http://localhost:8080/sdvms GET, PUT
61 '/sdvms/(.*)/application/(.*)', 'os_sdvm_application', # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND] GET
62 '/sdvms/(.*)/ip', 'os_sdvm_ip', # http://localhost:8080/sdvms/[VMNAME]/ip GET
63 '/sdvms/(.*)/start', 'os_sdvm_start', # http://localhost:8080/sdvms/[VMNAME]/start GET
64 '/sdvms/(.*)/stop', 'os_sdvm_stop', # http://localhost:8080/sdvms/[VMNAME]/stop GET
65 '/sdvms/(.*)', 'os_sdvm', # http://localhost:8080/sdvms/[VMNAME] GET, DELETE
66 '/vms', 'os_vms', # http://localhost:8080/vms GET
67 '/vms/(.*)', 'os_vm', # http://localhost:8080/vms/[VMNAME] GET
68 '/', 'os_root', # http://localhost:8080/ GET
69 '/update_template', 'os_update_template' # http://localhost:8080/update_template GET
72 # ------------------------------------------------------------
75 # Global VMManager instance
76 gvm_mgr = VMManager.getInstance()
78 # ------------------------------------------------------------
82 class os_device_change:
83 """OpenSecurity '/device_change' handler"""
86 log_call(web.ctx.environ)
88 new_ip = gvm_mgr.handleDeviceChange()
91 raise web.internalerror()
95 """OpenSecurity '/browsing' handler
97 - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
101 log_call(web.ctx.environ)
103 browsingVM = gvm_mgr.handleBrowsingRequest()
106 raise web.internalerror()
108 class os_sdvm_started:
109 """OpenSecurity '/sdvm_started' handler"""
112 log_call(web.ctx.environ)
113 remote_ip = web.ctx.environ['REMOTE_ADDR']
114 gvm_mgr.putStartNotification(remote_ip)
115 return "os_sdvm_started"
118 """OpenSecurity '/sdvms/[VM]' handler
120 - GET: Information about a specific SecurityVM
121 - DELETE: Remove a specific
125 log_call(web.ctx.environ)
126 return gvm_mgr.getVMInfo(name)
128 def DELETE(self, name):
129 log_call(web.ctx.environ)
130 return gvm_mgr.removeVM(name)
133 class os_sdvm_application:
134 """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
136 - GET: start application with given command in the VM.
139 def GET(self, name, command):
140 log_call(web.ctx.environ)
141 command = '/' + command
142 result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key' )
143 self.poweroffVM(name)
144 return gvm_mgr.removeVM(name)
148 """OpenSecurity '/sdvms/[VM]/ip' handler
150 - GET: give IP of SecurityVM.
154 log_call(web.ctx.environ)
155 return gvm_mgr.getHostOnlyIP(name)
159 """OpenSecurity '/sdvms/[VM]/start' handler
161 - GET: Start specific SecuirtyVM.
165 log_call(web.ctx.environ)
166 return gvm_mgr.startVM(name)
170 """OpenSecurity '/sdvms/[VM]/stop' handler
172 - GET: stop specific Secuirty VM.
176 log_call(web.ctx.environ)
177 return gvm_mgr.stopVM(name)
181 """OpenSecurity '/sdvms' handler
183 - GET: list all available secuirty VMs.
184 - POST: create new security vm.
188 """get the list of SDVMs"""
189 log_call(web.ctx.environ)
190 return gvm_mgr.listSDVM()
193 """create a new SDVM"""
194 log_call(web.ctx.environ)
197 name = gvm_mgr.generateSDVMName()
199 gvm_mgr.createVM(name)
201 raise web.internalerror()
206 """OpenSecurity '/vms/[VM]' handler
208 - GET: list information of arbitrary VM.
212 log_call(web.ctx.environ)
213 return gvm_mgr.getVMInfo(name)
217 """OpenSecurity '/vms' handler
219 - GET: list all (also non Security) VMs.
223 log_call(web.ctx.environ)
224 return gvm_mgr.listVM()
228 """OpenSecurity '/' handler
230 - GET: give information about current installation.
234 log_call(web.ctx.environ)
235 res = "'os_server': { "
236 res += "'version': '" + __version__ + "', "
237 res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
241 class os_update_template:
242 """OpenSecurity '/update_template' handler
244 - GET: update template vm
248 #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
249 log_call(web.ctx.environ)
250 return gvm_mgr.updateTemplate()
253 def log_call(web_environ):
254 """log the incoming call to the REST api"""
256 call = 'REST ' + web_environ['REQUEST_METHOD'] + ' ' + web_environ['REQUEST_URI'] + ' from ' + web_environ['REMOTE_ADDR'] + ':' + web_environ['REMOTE_PORT']
262 if __name__ == "__main__":
263 server = web.application(opensecurity_urls, globals())