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 # ------------------------------------------------------------
41 from vmmanager import VMManager
44 from environment import Environment
47 # ------------------------------------------------------------
53 """All the URLs we know mapping to class handler"""
55 '/device_change', 'os_device_change', # http://localhost:8080/device_change GET
56 '/sdvm_started', 'os_sdvm_started', # http://localhost:8080/sdvm_started GET
57 '/browsing', 'os_browsing', # http://localhost:8080/browsing GET
58 '/sdvms', 'os_sdvms', # http://localhost:8080/sdvms GET, PUT
59 '/sdvms/(.*)/application/(.*)', 'os_sdvm_application', # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND] GET
60 '/sdvms/(.*)/ip', 'os_sdvm_ip', # http://localhost:8080/sdvms/[VMNAME]/ip GET
61 '/sdvms/(.*)/start', 'os_sdvm_start', # http://localhost:8080/sdvms/[VMNAME]/start GET
62 '/sdvms/(.*)/stop', 'os_sdvm_stop', # http://localhost:8080/sdvms/[VMNAME]/stop GET
63 '/sdvms/(.*)', 'os_sdvm', # http://localhost:8080/sdvms/[VMNAME] GET, DELETE
64 '/vms', 'os_vms', # http://localhost:8080/vms GET
65 '/vms/(.*)', 'os_vm', # http://localhost:8080/vms/[VMNAME] GET
66 '/', 'os_root', # http://localhost:8080/ GET
67 '/update_template', 'os_update_template' # http://localhost:8080/update_template GET
70 # ------------------------------------------------------------
73 # Global VMManager instance
74 gvm_mgr = VMManager.getInstance()
76 # ------------------------------------------------------------
80 class os_device_change:
81 """OpenSecurity '/device_change' handler"""
85 new_ip = gvm_mgr.handleDeviceChange()
88 raise web.internalerror()
92 """OpenSecurity '/browsing' handler
94 - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
99 browsingVM = gvm_mgr.handleBrowsingRequest()
102 raise web.internalerror()
104 class os_sdvm_started:
105 """OpenSecurity '/sdvm_started' handler"""
108 remote_ip = web.ctx.environ['REMOTE_ADDR']
109 gvm_mgr.putStartNotification(remote_ip)
110 return "os_sdvm_started"
113 """OpenSecurity '/sdvms/[VM]' handler
115 - GET: Information about a specific SecurityVM
116 - DELETE: Remove a specific
120 return gvm_mgr.getVMInfo(name)
122 def DELETE(self, name):
123 return gvm_mgr.removeVM(name)
126 class os_sdvm_application:
127 """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
129 - GET: start application with given command in the VM.
132 def GET(self, name, command):
133 command = '/' + command
134 print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
135 return gvm_mgr.sshGuestX11Execute(name, command)
139 """OpenSecurity '/sdvms/[VM]/ip' handler
141 - GET: give IP of SecurityVM.
145 return gvm_mgr.getHostOnlyIP(name)
149 """OpenSecurity '/sdvms/[VM]/start' handler
151 - GET: Start specific SecuirtyVM.
155 return gvm_mgr.startVM(name)
159 """OpenSecurity '/sdvms/[VM]/stop' handler
161 - GET: stop specific Secuirty VM.
165 return gvm_mgr.stopVM(name)
169 """OpenSecurity '/sdvms' handler
171 - GET: list all available secuirty VMs.
172 - POST: create new security vm.
176 """get the list of SDVMs"""
177 return gvm_mgr.listSDVM()
180 """create a new SDVM"""
183 name = gvm_mgr.generateSDVMName()
185 gvm_mgr.createVM(name)
187 raise web.internalerror()
192 """OpenSecurity '/vms/[VM]' handler
194 - GET: list information of arbitrary VM.
198 return gvm_mgr.getVMInfo(name)
202 """OpenSecurity '/vms' handler
204 - GET: list all (also non Security) VMs.
208 return gvm_mgr.listVM()
212 """OpenSecurity '/' handler
214 - GET: give information about current installation.
218 res = "'os_server': { "
219 res += "'version': '" + __version__ + "', "
220 res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
224 class os_update_template:
225 """OpenSecurity '/update_template' handler
227 - GET: update template vm
231 #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
232 return gvm_mgr.updateTemplate()
235 if __name__ == "__main__":
236 server = web.application(opensecurity_urls, globals())