om@2: #!/bin/env python om@2: # -*- coding: utf-8 -*- om@2: om@2: # ------------------------------------------------------------ om@2: # opensecurityd om@2: # om@2: # the opensecurityd as RESTful server om@2: # om@2: # Autor: Oliver Maurhart, om@2: # om@2: # Copyright (C) 2013 AIT Austrian Institute of Technology om@2: # AIT Austrian Institute of Technology GmbH om@2: # Donau-City-Strasse 1 | 1220 Vienna | Austria om@2: # http://www.ait.ac.at om@2: # om@2: # This program is free software; you can redistribute it and/or om@2: # modify it under the terms of the GNU General Public License om@2: # as published by the Free Software Foundation version 2. om@2: # om@2: # This program is distributed in the hope that it will be useful, om@2: # but WITHOUT ANY WARRANTY; without even the implied warranty of om@2: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the om@2: # GNU General Public License for more details. om@2: # om@2: # You should have received a copy of the GNU General Public License om@2: # along with this program; if not, write to the Free Software om@2: # Foundation, Inc., 51 Franklin Street, Fifth Floor, om@2: # Boston, MA 02110-1301, USA. om@2: # ------------------------------------------------------------ om@2: om@2: om@2: # ------------------------------------------------------------ om@2: # imports om@2: om@2: import os om@2: import os.path om@2: import subprocess om@2: import sys om@2: import web om@2: om@2: # local om@2: from environment import Environment om@2: om@2: om@2: # ------------------------------------------------------------ om@2: # const om@2: om@2: om@2: __version__ = "0.1" om@2: om@2: om@2: """All the URLs we know mapping to class handler""" om@2: opensecurity_urls = ( om@2: '/application', 'os_application', om@2: '/device', 'os_device', om@2: '/device/credentials', 'os_device_credentials', om@2: '/device/password', 'os_device_password', om@2: '/', 'os_root' om@2: ) om@2: om@2: om@2: # ------------------------------------------------------------ om@2: # code om@2: om@2: om@2: class os_application: om@2: om@2: """OpenSecurity '/application' handler. om@2: om@2: This is called on GET /application?vm=VM-ID&app=APP-ID om@2: This tries to access the vm identified with the label VM-ID om@2: and launched the application identified APP-ID om@2: """ om@2: om@2: def GET(self): om@2: om@2: # pick the arguments om@2: args = web.input() om@2: om@2: # we _need_ a vm om@2: if not "vm" in args: om@2: raise web.badrequest() om@2: om@2: # we _need_ a app om@2: if not "app" in args: om@2: raise web.badrequest() om@2: om@2: ## TODO: HARD CODED STUFF HERE! THIS SHOULD BE FLEXIBLE! om@2: ssh_private_key = os.path.join(Environment("opensecurity").data_path, 'share', '192.168.56.15.ppk') om@2: putty_session = '192.168.56.15' om@2: process_command = ['plink.exe', '-i', ssh_private_key, putty_session, args.app] om@2: si = subprocess.STARTUPINFO() om@2: si.dwFlags = subprocess.STARTF_USESHOWWINDOW om@2: si.wShowWindow = subprocess.SW_HIDE om@2: print('tyring to launch: ' + ' '.join(process_command)) om@2: process = subprocess.Popen(process_command, shell = True) om@2: return 'launched: ' + ' '.join(process_command) om@2: om@2: om@2: class os_device: om@2: om@2: """OpenSecurity '/device' handler""" om@2: om@2: def GET(self): om@2: return "os_device" om@2: om@2: om@2: class os_device_credentials: om@2: om@2: """OpenSecurity '/device/credentials' handler. om@2: om@2: This is called on GET /device/credentials?id=DEVICE-ID. om@2: Ideally this should pop up a user dialog to insert his om@2: credentials based the DEVICE-ID om@2: """ om@2: om@2: def GET(self): om@2: om@2: # pick the arguments om@2: args = web.input() om@2: om@2: # we _need_ a device id om@2: if not "id" in args: om@2: raise web.badrequest() om@2: om@2: # invoke the user dialog as a subprocess om@2: dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py') om@2: process_command = [sys.executable, dlg_credentials_image, 'credentials', 'Please provide credentials for accessing \ndevice: "{0}".'.format(args.id)] om@2: process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE) om@2: result = process.communicate()[0] om@2: if process.returncode != 0: om@2: return 'Credentials request has been aborted.' om@2: om@2: return result om@2: om@2: om@2: class os_device_password: om@2: om@2: """OpenSecurity '/device/password' handler. om@2: om@2: This is called on GET /device/password?id=DEVICE-ID. om@2: Ideally this should pop up a user dialog to insert his om@2: password based the DEVICE-ID om@2: """ om@2: om@2: def GET(self): om@2: om@2: # pick the arguments om@2: args = web.input() om@2: om@2: # we _need_ a device id om@2: if not "id" in args: om@2: raise web.badrequest() om@2: om@2: # invoke the user dialog as a subprocess om@2: dlg_credentials_image = os.path.join(sys.path[0], 'opensecurity-dialog.py') om@2: process_command = [sys.executable, dlg_credentials_image, 'password', 'Please provide a password for accessing \ndevice: "{0}".'.format(args.id)] om@2: process = subprocess.Popen(process_command, shell = False, stdout = subprocess.PIPE) om@2: result = process.communicate()[0] om@2: if process.returncode != 0: om@2: return 'Credentials request has been aborted.' om@2: om@2: return result om@2: om@2: om@2: class os_root: om@2: om@2: """OpenSecurity '/' handler""" om@2: om@2: def GET(self): om@2: return "OpenSecurity-Server { \"version\": \"%s\" }" % __version__ om@2: om@2: om@2: # start om@2: if __name__ == "__main__": om@2: server = web.application(opensecurity_urls, globals()) om@2: server.run() om@2: