1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/OpenSecurity/bin/opensecurityd.pyw Tue Feb 18 10:36:55 2014 +0100
1.3 @@ -0,0 +1,241 @@
1.4 +#!/bin/env python
1.5 +# -*- coding: utf-8 -*-
1.6 +
1.7 +# ------------------------------------------------------------
1.8 +# opensecurityd
1.9 +#
1.10 +# the opensecurityd as RESTful server
1.11 +#
1.12 +# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
1.13 +#
1.14 +# Copyright (C) 2013 AIT Austrian Institute of Technology
1.15 +# AIT Austrian Institute of Technology GmbH
1.16 +# Donau-City-Strasse 1 | 1220 Vienna | Austria
1.17 +# http://www.ait.ac.at
1.18 +#
1.19 +# This program is free software; you can redistribute it and/or
1.20 +# modify it under the terms of the GNU General Public License
1.21 +# as published by the Free Software Foundation version 2.
1.22 +#
1.23 +# This program is distributed in the hope that it will be useful,
1.24 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1.25 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.26 +# GNU General Public License for more details.
1.27 +#
1.28 +# You should have received a copy of the GNU General Public License
1.29 +# along with this program; if not, write to the Free Software
1.30 +# Foundation, Inc., 51 Franklin Street, Fifth Floor,
1.31 +# Boston, MA 02110-1301, USA.
1.32 +# ------------------------------------------------------------
1.33 +
1.34 +
1.35 +# ------------------------------------------------------------
1.36 +# imports
1.37 +
1.38 +import os
1.39 +import os.path
1.40 +import subprocess
1.41 +import sys
1.42 +import web
1.43 +from cygwin import Cygwin
1.44 +
1.45 +from vmmanager import VMManager
1.46 +
1.47 +# local
1.48 +from environment import Environment
1.49 +
1.50 +
1.51 +# ------------------------------------------------------------
1.52 +# const
1.53 +
1.54 +__version__ = "0.2"
1.55 +
1.56 +
1.57 +"""All the URLs we know mapping to class handler"""
1.58 +opensecurity_urls = (
1.59 + '/device_change', 'os_device_change', # http://localhost:8080/device_change GET
1.60 + '/sdvm_started', 'os_sdvm_started', # http://localhost:8080/sdvm_started GET
1.61 + '/browsing', 'os_browsing', # http://localhost:8080/browsing GET
1.62 + '/sdvms', 'os_sdvms', # http://localhost:8080/sdvms GET, PUT
1.63 + '/sdvms/(.*)/application/(.*)', 'os_sdvm_application', # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND] GET
1.64 + '/sdvms/(.*)/ip', 'os_sdvm_ip', # http://localhost:8080/sdvms/[VMNAME]/ip GET
1.65 + '/sdvms/(.*)/start', 'os_sdvm_start', # http://localhost:8080/sdvms/[VMNAME]/start GET
1.66 + '/sdvms/(.*)/stop', 'os_sdvm_stop', # http://localhost:8080/sdvms/[VMNAME]/stop GET
1.67 + '/sdvms/(.*)', 'os_sdvm', # http://localhost:8080/sdvms/[VMNAME] GET, DELETE
1.68 + '/vms', 'os_vms', # http://localhost:8080/vms GET
1.69 + '/vms/(.*)', 'os_vm', # http://localhost:8080/vms/[VMNAME] GET
1.70 + '/', 'os_root', # http://localhost:8080/ GET
1.71 + '/update_template', 'os_update_template' # http://localhost:8080/update_template GET
1.72 +)
1.73 +
1.74 + # ------------------------------------------------------------
1.75 +# vars
1.76 +
1.77 +# Global VMManager instance
1.78 +gvm_mgr = VMManager.getInstance()
1.79 +
1.80 +# ------------------------------------------------------------
1.81 +# code
1.82 +
1.83 +
1.84 +class os_device_change:
1.85 + """OpenSecurity '/device_change' handler"""
1.86 +
1.87 + def GET(self):
1.88 + try:
1.89 + new_ip = gvm_mgr.handleDeviceChange()
1.90 + return new_ip
1.91 + except:
1.92 + raise web.internalerror()
1.93 +
1.94 +
1.95 +class os_browsing:
1.96 + """OpenSecurity '/browsing' handler
1.97 +
1.98 + - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
1.99 + """
1.100 +
1.101 + def GET(self):
1.102 + try:
1.103 + browsingVM = gvm_mgr.handleBrowsingRequest()
1.104 + return browsingVM
1.105 + except:
1.106 + raise web.internalerror()
1.107 +
1.108 +class os_sdvm_started:
1.109 + """OpenSecurity '/sdvm_started' handler"""
1.110 +
1.111 + def GET(self):
1.112 + remote_ip = web.ctx.environ['REMOTE_ADDR']
1.113 + gvm_mgr.putStartNotification(remote_ip)
1.114 + return "os_sdvm_started"
1.115 +
1.116 +class os_sdvm:
1.117 + """OpenSecurity '/sdvms/[VM]' handler
1.118 +
1.119 + - GET: Information about a specific SecurityVM
1.120 + - DELETE: Remove a specific
1.121 + """
1.122 +
1.123 + def GET(self, name):
1.124 + return gvm_mgr.getVMInfo(name)
1.125 +
1.126 + def DELETE(self, name):
1.127 + return gvm_mgr.removeVM(name)
1.128 +
1.129 +
1.130 +class os_sdvm_application:
1.131 + """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
1.132 +
1.133 + - GET: start application with given command in the VM.
1.134 + """
1.135 +
1.136 + def GET(self, name, command):
1.137 + command = '/' + command
1.138 + print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
1.139 + result = Cygwin.sshExecuteX11(command, gvm_mgr.getHostOnlyIP(name), 'osecuser', Cygwin.cygPath(gvm_mgr.getMachineFolder()) + '/' + name + '/dvm_key' )
1.140 + self.poweroffVM(name)
1.141 + return gvm_mgr.removeVM(name)
1.142 +
1.143 +
1.144 +class os_sdvm_ip:
1.145 + """OpenSecurity '/sdvms/[VM]/ip' handler
1.146 +
1.147 + - GET: give IP of SecurityVM.
1.148 + """
1.149 +
1.150 + def GET(self, name):
1.151 + return gvm_mgr.getHostOnlyIP(name)
1.152 +
1.153 +
1.154 +class os_sdvm_start:
1.155 + """OpenSecurity '/sdvms/[VM]/start' handler
1.156 +
1.157 + - GET: Start specific SecuirtyVM.
1.158 + """
1.159 +
1.160 + def GET(self, name):
1.161 + return gvm_mgr.startVM(name)
1.162 +
1.163 +
1.164 +class os_sdvm_stop:
1.165 + """OpenSecurity '/sdvms/[VM]/stop' handler
1.166 +
1.167 + - GET: stop specific Secuirty VM.
1.168 + """
1.169 +
1.170 + def GET(self, name):
1.171 + return gvm_mgr.stopVM(name)
1.172 +
1.173 +
1.174 +class os_sdvms:
1.175 + """OpenSecurity '/sdvms' handler
1.176 +
1.177 + - GET: list all available secuirty VMs.
1.178 + - POST: create new security vm.
1.179 + """
1.180 +
1.181 + def GET(self):
1.182 + """get the list of SDVMs"""
1.183 + return gvm_mgr.listSDVM()
1.184 +
1.185 + def POST(self):
1.186 + """create a new SDVM"""
1.187 +
1.188 + # get a new vm-name
1.189 + name = gvm_mgr.generateSDVMName()
1.190 + try:
1.191 + gvm_mgr.createVM(name)
1.192 + except:
1.193 + raise web.internalerror()
1.194 +
1.195 + return name
1.196 +
1.197 +class os_vm:
1.198 + """OpenSecurity '/vms/[VM]' handler
1.199 +
1.200 + - GET: list information of arbitrary VM.
1.201 + """
1.202 +
1.203 + def GET(self, name):
1.204 + return gvm_mgr.getVMInfo(name)
1.205 +
1.206 +
1.207 +class os_vms:
1.208 + """OpenSecurity '/vms' handler
1.209 +
1.210 + - GET: list all (also non Security) VMs.
1.211 + """
1.212 +
1.213 + def GET(self):
1.214 + return gvm_mgr.listVM()
1.215 +
1.216 +
1.217 +class os_root:
1.218 + """OpenSecurity '/' handler
1.219 +
1.220 + - GET: give information about current installation.
1.221 + """
1.222 +
1.223 + def GET(self):
1.224 + res = "'os_server': { "
1.225 + res += "'version': '" + __version__ + "', "
1.226 + res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
1.227 + res += "}"
1.228 + return res
1.229 +
1.230 +class os_update_template:
1.231 + """OpenSecurity '/update_template' handler
1.232 +
1.233 + - GET: update template vm
1.234 + """
1.235 +
1.236 + def GET(self):
1.237 + #return gvm_mgr.guestExecute('SecurityDVM', 'sudo apt-get -y update')
1.238 + return gvm_mgr.updateTemplate()
1.239 +
1.240 +# start
1.241 +if __name__ == "__main__":
1.242 + server = web.application(opensecurity_urls, globals())
1.243 + server.run()
1.244 +