server/opensecurityd.py
author mb
Tue, 03 Dec 2013 18:04:46 +0100
changeset 7 903480cebdfb
child 12 11dc05750aea
permissions -rw-r--r--
added vmmanager and USBEventSvc
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # opensecurityd
     6 # 
     7 # the opensecurityd as RESTful server
     8 #
     9 # Autor: Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    10 #
    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
    15 #
    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.
    19 # 
    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.
    24 # 
    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 # ------------------------------------------------------------
    30 
    31 
    32 # ------------------------------------------------------------
    33 # imports
    34 
    35 import os
    36 import os.path
    37 import subprocess
    38 import sys
    39 import web
    40 from vmmanager.vmmanager import VMManager
    41 
    42 # local
    43 from environment import Environment
    44 
    45 
    46 # ------------------------------------------------------------
    47 # const
    48 
    49 __version__ = "0.1"
    50 
    51 
    52 """All the URLs we know mapping to class handler"""
    53 opensecurity_urls = (
    54     '/device_change',           'os_device_change',
    55     '/application',             'os_application',
    56     '/device',                  'os_device',
    57     '/device/credentials',      'os_device_credentials',
    58     '/device/password',         'os_device_password',
    59     '/',                        'os_root'
    60 )
    61 
    62 
    63 # ------------------------------------------------------------
    64 # code
    65 
    66 gvm_mgr = VMManager()
    67 
    68 
    69 class os_application:
    70     
    71     """OpenSecurity '/application' handler.
    72     
    73     This is called on GET /application?vm=VM-ID&app=APP-ID
    74     This tries to access the vm identified with the label VM-ID
    75     and launched the application identified APP-ID
    76     """
    77     
    78     def GET(self):
    79         
    80         # pick the arguments
    81         args = web.input()
    82         
    83         # we _need_ a vm
    84         if not "vm" in args:
    85             raise web.badrequest()
    86         
    87         # we _need_ a app
    88         if not "app" in args:
    89             raise web.badrequest()
    90         
    91         ## TODO: HARD CODED STUFF HERE! THIS SHOULD BE FLEXIBLE!
    92         ssh_private_key = os.path.join(Environment("opensecurity").data_path, 'share', '192.168.56.15.ppk')
    93         putty_session = '192.168.56.15'
    94         process_command = ['plink.exe', '-i', ssh_private_key, putty_session, args.app]
    95         si = subprocess.STARTUPINFO()
    96         si.dwFlags = subprocess.STARTF_USESHOWWINDOW
    97         si.wShowWindow = subprocess.SW_HIDE
    98         print('tyring to launch: ' + ' '.join(process_command))
    99         process = subprocess.Popen(process_command, shell = True)
   100         return 'launched: ' + ' '.join(process_command)
   101 
   102 class os_device:
   103     
   104     """OpenSecurity '/device' handler"""
   105     
   106     def GET(self):
   107         return "os_device"
   108 
   109 class os_device_change:
   110     
   111     """OpenSecurity '/device_change' handler"""
   112     
   113     def GET(self):
   114         gvm_mgr.cygwin_path = 'c:\\cygwin64\\bin\\'
   115         #gvm_mgr.configureHostNetworking()
   116         print 'received device_change'
   117         return "os_device_change"
   118 
   119 
   120 class os_device_credentials:
   121     
   122     """OpenSecurity '/device/credentials' handler.
   123     
   124     This is called on GET /device/credentials?id=DEVICE-ID.
   125     Ideally this should pop up a user dialog to insert his
   126     credentials based the DEVICE-ID
   127     """
   128     
   129     def GET(self):
   130         
   131         # pick the arguments
   132         args = web.input()
   133         
   134         # we _need_ a device id
   135         if not "id" in args:
   136             raise web.badrequest()
   137         
   138         # invoke the user dialog as a subprocess
   139         dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   140         process_command = [sys.executable, dlg_credentials_image, 'credentials', 'Please provide credentials for accessing \ndevice: "{0}".'.format(args.id)]
   141         process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   142         result = process.communicate()[0]
   143         if process.returncode != 0:
   144             return 'Credentials request has been aborted.'
   145         
   146         return result
   147 
   148 
   149 class os_device_password:
   150     
   151     """OpenSecurity '/device/password' handler.
   152     
   153     This is called on GET /device/password?id=DEVICE-ID.
   154     Ideally this should pop up a user dialog to insert his
   155     password based the DEVICE-ID
   156     """
   157     
   158     def GET(self):
   159         
   160         # pick the arguments
   161         args = web.input()
   162         
   163         # we _need_ a device id
   164         if not "id" in args:
   165             raise web.badrequest()
   166             
   167         # invoke the user dialog as a subprocess
   168         dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py')
   169         process_command = [sys.executable, dlg_credentials_image, 'password', 'Please provide a password for accessing \ndevice: "{0}".'.format(args.id)]
   170         process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE)
   171         result = process.communicate()[0]
   172         if process.returncode != 0:
   173             return 'Credentials request has been aborted.'
   174         
   175         return result
   176 
   177 
   178 class os_root:
   179     
   180     """OpenSecurity '/' handler"""
   181     
   182     def GET(self):
   183         return "OpenSecurity-Server { \"version\": \"%s\" }" % __version__
   184 
   185 
   186 # start
   187 if __name__ == "__main__":
   188     server = web.application(opensecurity_urls, globals())
   189     server.run()
   190