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