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
79 # ------------------------------------------------------------
83 #class os_device_change:
84 # """OpenSecurity '/device_change' handler"""
87 # log_call(web.ctx.environ)
89 # new_ip = gvm_mgr.handleDeviceChange()
92 # raise web.internalerror()
96 """OpenSecurity '/browsing' handler
98 - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
102 log_call(web.ctx.environ)
104 browsingVM = vmmanager().handleBrowsingRequest()
107 raise web.internalerror()
109 #class os_sdvm_started:
110 # """OpenSecurity '/sdvm_started' handler"""
113 # log_call(web.ctx.environ)
114 # remote_ip = web.ctx.environ['REMOTE_ADDR']
115 # gvm_mgr.putStartNotification(remote_ip)
116 # return "os_sdvm_started"
119 """OpenSecurity '/sdvms/[VM]' handler
121 - GET: Information about a specific SecurityVM
122 - DELETE: Remove a specific
126 log_call(web.ctx.environ)
127 return vmmanager().getVMInfo(name)
129 def DELETE(self, name):
130 log_call(web.ctx.environ)
131 return vmmanager().removeVM(name)
134 class os_sdvm_application:
135 """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
137 - GET: start application with given command in the VM.
140 def GET(self, name, command):
141 log_call(web.ctx.environ)
142 command = '/' + command
143 result = Cygwin.sshExecuteX11(command, vmmanager().getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key' )
144 self.poweroffVM(name)
145 return vmmanager().removeVM(name)
149 """OpenSecurity '/sdvms/[VM]/ip' handler
151 - GET: give IP of SecurityVM.
155 log_call(web.ctx.environ)
156 return vmmanager().getHostOnlyIP(name)
160 """OpenSecurity '/sdvms/[VM]/start' handler
162 - GET: Start specific SecuirtyVM.
166 log_call(web.ctx.environ)
167 return vmmanager().startVM(name)
171 """OpenSecurity '/sdvms/[VM]/stop' handler
173 - GET: stop specific Secuirty VM.
177 log_call(web.ctx.environ)
178 return vmmanager().stopVM(name)
182 """OpenSecurity '/sdvms' handler
184 - GET: list all available secuirty VMs.
185 - POST: create new security vm.
189 """get the list of SDVMs"""
190 log_call(web.ctx.environ)
191 return vmmanager().listSDVM()
194 """create a new SDVM"""
195 log_call(web.ctx.environ)
198 name = vmmanager().generateSDVMName()
200 vmmanager().createVM(name)
202 raise web.internalerror()
207 """OpenSecurity '/vms/[VM]' handler
209 - GET: list information of arbitrary VM.
213 log_call(web.ctx.environ)
214 return vmmanager().getVMInfo(name)
218 """OpenSecurity '/vms' handler
220 - GET: list all (also non Security) VMs.
224 log_call(web.ctx.environ)
225 return vmmanager().listVM()
229 """OpenSecurity '/' handler
231 - GET: give information about current installation.
235 log_call(web.ctx.environ)
236 res = "'os_server': { "
237 res += "'version': '" + __version__ + "', "
238 res += "'machine_folder': '" + vmmanager().getDefaultMachineFolder() + "' "
242 class os_update_template:
243 """OpenSecurity '/update_template' handler
245 - GET: update template vm
249 #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
250 log_call(web.ctx.environ)
251 return vmmanager().updateTemplate()
254 def log_call(web_environ):
255 """log the incoming call to the REST api"""
257 call = 'REST ' + web_environ['REQUEST_METHOD'] + ' ' + web_environ['REQUEST_URI'] + ' from ' + web_environ['REMOTE_ADDR'] + ':' + web_environ['REMOTE_PORT']
264 """main startup for the opensecuirityd"""
265 server = web.application(opensecurity_urls, globals())
271 """helper method to make lazy init of VMManager"""
273 gvm_mgr = VMManager.getInstance()
278 if __name__ == "__main__":