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
48 # ------------------------------------------------------------
54 """All the URLs we know mapping to class handler"""
56 '/device_change', 'os_device_change', # http://localhost:8080/device_change GET
57 '/sdvm_started', 'os_sdvm_started', # http://localhost:8080/sdvm_started GET
58 '/browsing', 'os_browsing', # http://localhost:8080/browsing GET
59 '/sdvms', 'os_sdvms', # http://localhost:8080/sdvms GET, PUT
60 '/sdvms/(.*)/application/(.*)', 'os_sdvm_application', # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND] GET
61 '/sdvms/(.*)/ip', 'os_sdvm_ip', # http://localhost:8080/sdvms/[VMNAME]/ip GET
62 '/sdvms/(.*)/start', 'os_sdvm_start', # http://localhost:8080/sdvms/[VMNAME]/start GET
63 '/sdvms/(.*)/stop', 'os_sdvm_stop', # http://localhost:8080/sdvms/[VMNAME]/stop GET
64 '/sdvms/(.*)', 'os_sdvm', # http://localhost:8080/sdvms/[VMNAME] GET, DELETE
65 '/vms', 'os_vms', # http://localhost:8080/vms GET
66 '/vms/(.*)', 'os_vm', # http://localhost:8080/vms/[VMNAME] GET
67 '/', 'os_root', # http://localhost:8080/ GET
68 '/update_template', 'os_update_template' # http://localhost:8080/update_template GET
71 # ------------------------------------------------------------
74 # Global VMManager instance
75 gvm_mgr = VMManager.getInstance()
77 # ------------------------------------------------------------
81 class os_device_change:
82 """OpenSecurity '/device_change' handler"""
86 new_ip = gvm_mgr.handleDeviceChange()
89 raise web.internalerror()
93 """OpenSecurity '/browsing' handler
95 - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
100 browsingVM = gvm_mgr.handleBrowsingRequest()
103 raise web.internalerror()
105 class os_sdvm_started:
106 """OpenSecurity '/sdvm_started' handler"""
109 remote_ip = web.ctx.environ['REMOTE_ADDR']
110 gvm_mgr.putStartNotification(remote_ip)
111 return "os_sdvm_started"
114 """OpenSecurity '/sdvms/[VM]' handler
116 - GET: Information about a specific SecurityVM
117 - DELETE: Remove a specific
121 return gvm_mgr.getVMInfo(name)
123 def DELETE(self, name):
124 return gvm_mgr.removeVM(name)
127 class os_sdvm_application:
128 """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
130 - GET: start application with given command in the VM.
133 def GET(self, name, command):
134 command = '/' + command
135 print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
136 result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key' )
137 self.poweroffVM(name)
138 return gvm_mgr.removeVM(name)
142 """OpenSecurity '/sdvms/[VM]/ip' handler
144 - GET: give IP of SecurityVM.
148 return gvm_mgr.getHostOnlyIP(name)
152 """OpenSecurity '/sdvms/[VM]/start' handler
154 - GET: Start specific SecuirtyVM.
158 return gvm_mgr.startVM(name)
162 """OpenSecurity '/sdvms/[VM]/stop' handler
164 - GET: stop specific Secuirty VM.
168 return gvm_mgr.stopVM(name)
172 """OpenSecurity '/sdvms' handler
174 - GET: list all available secuirty VMs.
175 - POST: create new security vm.
179 """get the list of SDVMs"""
180 return gvm_mgr.listSDVM()
183 """create a new SDVM"""
186 name = gvm_mgr.generateSDVMName()
188 gvm_mgr.createVM(name)
190 raise web.internalerror()
195 """OpenSecurity '/vms/[VM]' handler
197 - GET: list information of arbitrary VM.
201 return gvm_mgr.getVMInfo(name)
205 """OpenSecurity '/vms' handler
207 - GET: list all (also non Security) VMs.
211 return gvm_mgr.listVM()
215 """OpenSecurity '/' handler
217 - GET: give information about current installation.
221 res = "'os_server': { "
222 res += "'version': '" + __version__ + "', "
223 res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
227 class os_update_template:
228 """OpenSecurity '/update_template' handler
230 - GET: update template vm
234 #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
235 return gvm_mgr.updateTemplate()
238 if __name__ == "__main__":
239 server = web.application(opensecurity_urls, globals())