OpenSecurity/bin/opensecurityd.py
author mb
Wed, 11 Dec 2013 12:07:16 +0100
changeset 40 b44a603b0b95
parent 35 ba1ca3e5870b
child 41 76d9177ca509
permissions -rw-r--r--
changes to handleDeviceChange
mapNetworkDrive intead of netUse nad unmapNetworkDrive intead of netUnUse
om@13
     1
#!/bin/env python
om@13
     2
# -*- coding: utf-8 -*-
om@13
     3
om@13
     4
# ------------------------------------------------------------
om@13
     5
# opensecurityd
om@13
     6
# 
om@13
     7
# the opensecurityd as RESTful server
om@13
     8
#
om@13
     9
# Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
om@13
    10
#
om@13
    11
# Copyright (C) 2013 AIT Austrian Institute of Technology
om@13
    12
# AIT Austrian Institute of Technology GmbH
om@13
    13
# Donau-City-Strasse 1 | 1220 Vienna | Austria
om@13
    14
# http://www.ait.ac.at
om@13
    15
#
om@13
    16
# This program is free software; you can redistribute it and/or
om@13
    17
# modify it under the terms of the GNU General Public License
om@13
    18
# as published by the Free Software Foundation version 2.
om@13
    19
# 
om@13
    20
# This program is distributed in the hope that it will be useful,
om@13
    21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
om@13
    22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
om@13
    23
# GNU General Public License for more details.
om@13
    24
# 
om@13
    25
# You should have received a copy of the GNU General Public License
om@13
    26
# along with this program; if not, write to the Free Software
om@13
    27
# Foundation, Inc., 51 Franklin Street, Fifth Floor, 
om@13
    28
# Boston, MA  02110-1301, USA.
om@13
    29
# ------------------------------------------------------------
om@13
    30
om@13
    31
om@13
    32
# ------------------------------------------------------------
om@13
    33
# imports
om@13
    34
om@13
    35
import os
om@13
    36
import os.path
om@13
    37
import subprocess
om@13
    38
import sys
om@13
    39
import web
om@22
    40
om@22
    41
from vmmanager import VMManager
om@13
    42
om@13
    43
# local
om@13
    44
from environment import Environment
om@13
    45
om@13
    46
om@13
    47
# ------------------------------------------------------------
om@13
    48
# const
om@13
    49
om@31
    50
__version__ = "0.2"
om@13
    51
om@13
    52
om@13
    53
"""All the URLs we know mapping to class handler"""
om@13
    54
opensecurity_urls = (
om@31
    55
    '/device_change',                   'os_device_change',     # http://localhost:8080/device_change                           GET
mb@34
    56
    '/sdvm_started',                    'os_sdvm_started',      # http://localhost:8080/sdvm_started                            GET
om@31
    57
    '/browsing',                        'os_browsing',          # http://localhost:8080/browsing                                GET
om@31
    58
    '/sdvms',                           'os_sdvms',             # http://localhost:8080/sdvms                                   GET, PUT
om@31
    59
    '/sdvms/(.*)/application/(.*)',     'os_sdvm_application',  # http://localhost:8080/sdvms/[VMNAME]/application/[COMMAND]    GET
om@31
    60
    '/sdvms/(.*)/ip',                   'os_sdvm_ip',           # http://localhost:8080/sdvms/[VMNAME]/ip                       GET
om@31
    61
    '/sdvms/(.*)/start',                'os_sdvm_start',        # http://localhost:8080/sdvms/[VMNAME]/start                    GET
om@31
    62
    '/sdvms/(.*)/stop',                 'os_sdvm_stop',         # http://localhost:8080/sdvms/[VMNAME]/stop                     GET
om@31
    63
    '/sdvms/(.*)',                      'os_sdvm',              # http://localhost:8080/sdvms/[VMNAME]                          GET, DELETE
om@31
    64
    '/vms',                             'os_vms',               # http://localhost:8080/vms                                     GET
om@31
    65
    '/vms/(.*)',                        'os_vm',                # http://localhost:8080/vms/[VMNAME]                            GET
om@31
    66
    '/',                                'os_root'               # http://localhost:8080/                                        GET
om@13
    67
)
om@13
    68
om@13
    69
om@13
    70
# ------------------------------------------------------------
om@13
    71
# vars
om@13
    72
om@13
    73
# Global VMManager instance
mb@33
    74
gvm_mgr = VMManager.getInstance()
om@13
    75
om@13
    76
# ------------------------------------------------------------
om@13
    77
# code
om@13
    78
om@13
    79
om@13
    80
class os_device_change:
om@13
    81
    """OpenSecurity '/device_change' handler"""
om@13
    82
    
om@13
    83
    def GET(self):
mb@40
    84
        new_ip = gvm_mgr.handleDeviceChange()
mb@40
    85
        if new_ip != None:
mb@40
    86
            gvm_mgr.mapNetworkDrive('h:', '\\\\' + new_ip + '\\USB', None, None)
om@13
    87
        return "os_device_change"
om@13
    88
om@31
    89
        
om@31
    90
class os_browsing:
om@31
    91
    """OpenSecurity '/browsing' handler
om@31
    92
    
om@31
    93
    - GET: Start and prepare a new SecurityVM for Internet Browsing. Return the name of the VM.
om@31
    94
    """
mb@27
    95
    
mb@27
    96
    def GET(self):
om@31
    97
        try:
om@31
    98
            browsingVM = gvm_mgr.handleBrowsingRequest()
om@31
    99
            gvm_mgr.startVM(browsingVM)
om@31
   100
            return browsingVM
om@31
   101
        except:
om@31
   102
            raise web.internalerror()
mb@27
   103
mb@27
   104
class os_sdvm_started:
mb@27
   105
    """OpenSecurity '/sdvm_started' handler"""
mb@27
   106
    
mb@27
   107
    def GET(self):
mb@34
   108
        remote_ip = web.ctx.environ['REMOTE_ADDR']
mb@34
   109
        gvm_mgr.putStartNotification(remote_ip)
mb@27
   110
        return "os_sdvm_started"
om@31
   111
        
om@22
   112
class os_sdvm:
om@31
   113
    """OpenSecurity '/sdvms/[VM]' handler
om@31
   114
    
om@31
   115
    - GET: Information about a specific SecurityVM
om@31
   116
    - DELETE: Remove a specific
om@31
   117
    """
om@22
   118
    
om@22
   119
    def GET(self, name):
om@22
   120
        return gvm_mgr.getVMInfo(name)
om@22
   121
om@22
   122
    def DELETE(self, name):
om@22
   123
        return gvm_mgr.removeVM(name)
om@22
   124
            
om@22
   125
om@31
   126
class os_sdvm_application:
om@31
   127
    """OpenSecurity '/sdvms/[VM]/application/[CMD]' handler
om@31
   128
    
om@31
   129
    - GET: start application with given command in the VM.
om@31
   130
    """
om@31
   131
    
om@31
   132
    def GET(self, name, command):
om@31
   133
        command = '/' + command
om@31
   134
        print('---> request to launch application in VM -- ' + name + ':' + command + ' <---')
om@31
   135
        return gvm_mgr.sshGuestX11Execute(name, command)
om@31
   136
            
om@31
   137
om@30
   138
class os_sdvm_ip:
om@31
   139
    """OpenSecurity '/sdvms/[VM]/ip' handler
om@31
   140
    
om@31
   141
    - GET: give IP of SecurityVM.
om@31
   142
    """
om@30
   143
    
om@30
   144
    def GET(self, name):
om@30
   145
        return gvm_mgr.getHostOnlyIP(name)
om@30
   146
            
om@30
   147
om@30
   148
class os_sdvm_start:
om@31
   149
    """OpenSecurity '/sdvms/[VM]/start' handler
om@31
   150
    
om@31
   151
    - GET: Start specific SecuirtyVM.
om@31
   152
    """
om@30
   153
    
om@30
   154
    def GET(self, name):
om@30
   155
        return gvm_mgr.startVM(name)
om@30
   156
            
om@30
   157
om@30
   158
class os_sdvm_stop:
om@31
   159
    """OpenSecurity '/sdvms/[VM]/stop' handler
om@31
   160
    
om@31
   161
    - GET: stop specific Secuirty VM.
om@31
   162
    """
om@30
   163
    
om@30
   164
    def GET(self, name):
om@30
   165
        return gvm_mgr.stopVM(name)
om@30
   166
            
om@30
   167
om@13
   168
class os_sdvms:
om@31
   169
    """OpenSecurity '/sdvms' handler
om@31
   170
    
om@31
   171
    - GET: list all available secuirty VMs.
om@31
   172
    - POST: create new security vm.
om@31
   173
    """
om@13
   174
    
om@13
   175
    def GET(self):
om@17
   176
        """get the list of SDVMs"""
om@13
   177
        return gvm_mgr.listSDVM() 
om@13
   178
            
om@31
   179
    def POST(self):
om@17
   180
        """create a new SDVM"""
om@22
   181
om@30
   182
        # get a new vm-name
om@30
   183
        name = gvm_mgr.generateSDVMName()
om@30
   184
        try:
om@30
   185
            gvm_mgr.createVM(name)
om@30
   186
        except:
om@30
   187
            raise web.internalerror()
om@17
   188
            
om@30
   189
        return name
om@22
   190
            
om@13
   191
class os_vm:
om@31
   192
    """OpenSecurity '/vms/[VM]' handler
om@31
   193
    
om@31
   194
    - GET: list information of arbitrary VM.
om@31
   195
    """
om@13
   196
    
om@13
   197
    def GET(self, name):
om@13
   198
        return gvm_mgr.getVMInfo(name)
om@13
   199
            
om@13
   200
om@13
   201
class os_vms:
om@31
   202
    """OpenSecurity '/vms' handler
om@31
   203
    
om@31
   204
    - GET: list all (also non Security) VMs.
om@31
   205
    """
om@13
   206
    
om@13
   207
    def GET(self):
om@13
   208
        return gvm_mgr.listVM() 
om@13
   209
            
om@13
   210
om@13
   211
class os_root:
om@31
   212
    """OpenSecurity '/' handler
om@31
   213
    
om@31
   214
    - GET: give information about current installation.
om@31
   215
    """
om@13
   216
    
om@13
   217
    def GET(self):
om@13
   218
        res = "'os_server': { "
om@13
   219
        res += "'version': '" + __version__ + "', "
om@13
   220
        res += "'machine_folder': '" + gvm_mgr.getDefaultMachineFolder() + "' "
om@13
   221
        res += "}"
om@13
   222
        return res
om@13
   223
om@13
   224
om@13
   225
# start
om@13
   226
if __name__ == "__main__":
om@13
   227
    server = web.application(opensecurity_urls, globals())
om@13
   228
    server.run()
om@13
   229