server/opensecurityd.py
changeset 7 903480cebdfb
child 12 11dc05750aea
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/server/opensecurityd.py	Tue Dec 03 18:04:46 2013 +0100
     1.3 @@ -0,0 +1,190 @@
     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 vmmanager.vmmanager import VMManager
    1.44 +
    1.45 +# local
    1.46 +from environment import Environment
    1.47 +
    1.48 +
    1.49 +# ------------------------------------------------------------
    1.50 +# const
    1.51 +
    1.52 +__version__ = "0.1"
    1.53 +
    1.54 +
    1.55 +"""All the URLs we know mapping to class handler"""
    1.56 +opensecurity_urls = (
    1.57 +    '/device_change',           'os_device_change',
    1.58 +    '/application',             'os_application',
    1.59 +    '/device',                  'os_device',
    1.60 +    '/device/credentials',      'os_device_credentials',
    1.61 +    '/device/password',         'os_device_password',
    1.62 +    '/',                        'os_root'
    1.63 +)
    1.64 +
    1.65 +
    1.66 +# ------------------------------------------------------------
    1.67 +# code
    1.68 +
    1.69 +gvm_mgr = VMManager()
    1.70 +
    1.71 +
    1.72 +class os_application:
    1.73 +    
    1.74 +    """OpenSecurity '/application' handler.
    1.75 +    
    1.76 +    This is called on GET /application?vm=VM-ID&app=APP-ID
    1.77 +    This tries to access the vm identified with the label VM-ID
    1.78 +    and launched the application identified APP-ID
    1.79 +    """
    1.80 +    
    1.81 +    def GET(self):
    1.82 +        
    1.83 +        # pick the arguments
    1.84 +        args = web.input()
    1.85 +        
    1.86 +        # we _need_ a vm
    1.87 +        if not "vm" in args:
    1.88 +            raise web.badrequest()
    1.89 +        
    1.90 +        # we _need_ a app
    1.91 +        if not "app" in args:
    1.92 +            raise web.badrequest()
    1.93 +        
    1.94 +        ## TODO: HARD CODED STUFF HERE! THIS SHOULD BE FLEXIBLE!
    1.95 +        ssh_private_key = os.path.join(Environment("opensecurity").data_path, 'share', '192.168.56.15.ppk')
    1.96 +        putty_session = '192.168.56.15'
    1.97 +        process_command = ['plink.exe', '-i', ssh_private_key, putty_session, args.app]
    1.98 +        si = subprocess.STARTUPINFO()
    1.99 +        si.dwFlags = subprocess.STARTF_USESHOWWINDOW
   1.100 +        si.wShowWindow = subprocess.SW_HIDE
   1.101 +        print('tyring to launch: ' + ' '.join(process_command))
   1.102 +        process = subprocess.Popen(process_command, shell = True)
   1.103 +        return 'launched: ' + ' '.join(process_command)
   1.104 +
   1.105 +class os_device:
   1.106 +    
   1.107 +    """OpenSecurity '/device' handler"""
   1.108 +    
   1.109 +    def GET(self):
   1.110 +        return "os_device"
   1.111 +
   1.112 +class os_device_change:
   1.113 +    
   1.114 +    """OpenSecurity '/device_change' handler"""
   1.115 +    
   1.116 +    def GET(self):
   1.117 +        gvm_mgr.cygwin_path = 'c:\\cygwin64\\bin\\'
   1.118 +        #gvm_mgr.configureHostNetworking()
   1.119 +        print 'received device_change'
   1.120 +        return "os_device_change"
   1.121 +
   1.122 +
   1.123 +class os_device_credentials:
   1.124 +    
   1.125 +    """OpenSecurity '/device/credentials' handler.
   1.126 +    
   1.127 +    This is called on GET /device/credentials?id=DEVICE-ID.
   1.128 +    Ideally this should pop up a user dialog to insert his
   1.129 +    credentials based the DEVICE-ID
   1.130 +    """
   1.131 +    
   1.132 +    def GET(self):
   1.133 +        
   1.134 +        # pick the arguments
   1.135 +        args = web.input()
   1.136 +        
   1.137 +        # we _need_ a device id
   1.138 +        if not "id" in args:
   1.139 +            raise web.badrequest()
   1.140 +        
   1.141 +        # invoke the user dialog as a subprocess
   1.142 +        dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   1.143 +        process_command = [sys.executable, dlg_credentials_image, 'credentials', 'Please provide credentials for accessing \ndevice: "{0}".'.format(args.id)]
   1.144 +        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   1.145 +        result = process.communicate()[0]
   1.146 +        if process.returncode != 0:
   1.147 +            return 'Credentials request has been aborted.'
   1.148 +        
   1.149 +        return result
   1.150 +
   1.151 +
   1.152 +class os_device_password:
   1.153 +    
   1.154 +    """OpenSecurity '/device/password' handler.
   1.155 +    
   1.156 +    This is called on GET /device/password?id=DEVICE-ID.
   1.157 +    Ideally this should pop up a user dialog to insert his
   1.158 +    password based the DEVICE-ID
   1.159 +    """
   1.160 +    
   1.161 +    def GET(self):
   1.162 +        
   1.163 +        # pick the arguments
   1.164 +        args = web.input()
   1.165 +        
   1.166 +        # we _need_ a device id
   1.167 +        if not "id" in args:
   1.168 +            raise web.badrequest()
   1.169 +            
   1.170 +        # invoke the user dialog as a subprocess
   1.171 +        dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   1.172 +        process_command = [sys.executable, dlg_credentials_image, 'password', 'Please provide a password for accessing \ndevice: "{0}".'.format(args.id)]
   1.173 +        process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   1.174 +        result = process.communicate()[0]
   1.175 +        if process.returncode != 0:
   1.176 +            return 'Credentials request has been aborted.'
   1.177 +        
   1.178 +        return result
   1.179 +
   1.180 +
   1.181 +class os_root:
   1.182 +    
   1.183 +    """OpenSecurity '/' handler"""
   1.184 +    
   1.185 +    def GET(self):
   1.186 +        return "OpenSecurity-Server { \"version\": \"%s\" }" % __version__
   1.187 +
   1.188 +
   1.189 +# start
   1.190 +if __name__ == "__main__":
   1.191 +    server = web.application(opensecurity_urls, globals())
   1.192 +    server.run()
   1.193 +