OpenSecurity/bin/cygwin.py
author mb
Wed, 26 Feb 2014 17:27:07 +0100
changeset 79 617009c32da0
parent 78 23551f635ca9
child 87 d5b04809faca
permissions -rwxr-xr-x
hibernate, wait_startup, and other changes
     1 #!/bin/env python
     2 # -*- coding: utf-8 -*-
     3 
     4 # ------------------------------------------------------------
     5 # cygwin command
     6 # 
     7 # executes a cygwin command inside the opensecurity project
     8 #
     9 # Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
    10 #        Oliver Maurhart, <oliver.maurhart@ait.ac.at>
    11 #
    12 # Copyright (C) 2013 AIT Austrian Institute of Technology
    13 # AIT Austrian Institute of Technology GmbH
    14 # Donau-City-Strasse 1 | 1220 Vienna | Austria
    15 # http://www.ait.ac.at
    16 #
    17 # This program is free software; you can redistribute it and/or
    18 # modify it under the terms of the GNU General Public License
    19 # as published by the Free Software Foundation version 2.
    20 # 
    21 # This program is distributed in the hope that it will be useful,
    22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
    23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    24 # GNU General Public License for more details.
    25 # 
    26 # You should have received a copy of the GNU General Public License
    27 # along with this program; if not, write to the Free Software
    28 # Foundation, Inc., 51 Franklin Street, Fifth Floor, 
    29 # Boston, MA  02110-1301, USA.
    30 # ------------------------------------------------------------
    31 
    32 
    33 # ------------------------------------------------------------
    34 # imports
    35 
    36 import os
    37 import subprocess
    38 import sys
    39 import _winreg
    40 from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
    41 import threading
    42 # local
    43 from environment import Environment
    44 from opensecurity_util import logger, setupLogger, OpenSecurityException
    45 
    46 # ------------------------------------------------------------
    47 # code
    48 
    49 def once(theClass):
    50     """get the path to our local cygwin installment"""
    51     home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
    52     path_hint = [ 
    53         os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')), 
    54         os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin64')), 
    55         os.path.abspath(os.path.join(home_drive, 'cygwin')),
    56         os.path.abspath(os.path.join(home_drive, 'cygwin64'))
    57     ]
    58     path_valid = [ p for p in path_hint if os.path.exists(p) ]
    59         
    60     theClass.cygwin_root = path_valid[0]
    61     theClass.cygwin_bin = os.path.join(theClass.cygwin_root, 'bin') + os.path.sep
    62     theClass.cygwin_bash = os.path.join(theClass.cygwin_bin, 'bash.exe')
    63     theClass.cygwin_ssh = os.path.join(theClass.cygwin_bin, 'ssh.exe')
    64     theClass.cygwin_x11 = os.path.join(theClass.cygwin_bin, 'XWin.exe')
    65     theClass.win_cmd = os.environ.get("COMSPEC", "cmd.exe") 
    66     
    67     """get the path to the VirtualBox installation on this system"""
    68     try:
    69         k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\Oracle\VirtualBox')
    70         theClass.vbox_root = _winreg.QueryValueEx(k, 'InstallDir')[0]
    71         theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe')
    72         _winreg.CloseKey(k)
    73     except:
    74         pass
    75     return theClass
    76 
    77 @once
    78 class Cygwin(object):
    79     cygwin_root = ''
    80     cygwin_bin = ''
    81     cygwin_bash = ''
    82     cygwin_ssh = ''
    83     cygwin_x11 = ''
    84     vbox_root = ''
    85     vbox_man = ''
    86     win_cmd = ''
    87     """Some nifty methods working with Cygwin"""
    88     
    89     def __call__(self, command, arguments, wait_return=True, window = False):
    90         """make an instance of this object act as a function"""
    91         return self.execute(command, arguments, wait_return, window)
    92 
    93         
    94     @staticmethod
    95     def root():
    96         return Cygwin.cygwin_root
    97 
    98     @staticmethod
    99     def bin():
   100         return Cygwin.cygwin_bin
   101     
   102     @staticmethod
   103     def bash():
   104         return Cygwin.cygwin_bash
   105     
   106     @staticmethod    
   107     def ssh():
   108         return Cygwin.cygwin_ssh
   109 
   110     @staticmethod    
   111     def x11():
   112         return Cygwin.cygwin_x11
   113     
   114     @staticmethod
   115     def vboxman():
   116         return Cygwin.vbox_man
   117     
   118     @staticmethod
   119     def cmd():
   120         return Cygwin.win_cmd
   121     
   122     executeLock = threading.Lock()
   123     #executes command on host system
   124     @staticmethod
   125     def execute(program, arguments, wait_return=True, window = False):
   126         _startupinfo = STARTUPINFO()
   127         if not window:
   128             _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
   129             _startupinfo.wShowWindow = _subprocess.SW_HIDE
   130 
   131             #logger.debug('trying to launch: ' + program + ' ' + ''.join(arguments))
   132         res_stderr = None
   133         try:
   134             process = Popen(executable=program, args=' ' + arguments, startupinfo = _startupinfo, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell = False)
   135             logger.debug('Launched: ' + program + ' ' + ''.join(arguments))
   136             if not wait_return:
   137                 return [0, 'working in background', '']
   138             result = process.wait()
   139             res_stdout = process.stdout.read();
   140             res_stderr = process.stderr.read();
   141 
   142         except Exception as ex:
   143             res_stderr = ''.join(ex.args)
   144             result = -1 
   145         
   146         if result != 0:
   147             logger.error('Failed to execute cygwin command.\n\tcommand=' + program + ' ' + ''.join(arguments) + '\n' + res_stderr)
   148             raise OpenSecurityException('Failed to execute cygwin command.\n\tcommand=' + program + ' ' + ''.join(arguments) + '\n' + res_stderr)
   149             
   150         return result, res_stdout, res_stderr
   151     
   152     @staticmethod
   153     def vboxExecute(command, wait_return=True, window = False, bash_opts=''):
   154         if Cygwin.executeLock.acquire(True):
   155             retry = 0
   156             while retry < 3:
   157                 try:
   158                     result = Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
   159                     Cygwin.executeLock.release()
   160                     return result
   161                 except OpenSecurityException as inst:
   162                     retry+=1
   163             Cygwin.executeLock.release()
   164             raise inst
   165         return result
   166         
   167     
   168     @staticmethod
   169     def bashExecute(command, wait_return=True, window = False, bash_opts=''):
   170         command = bash_opts + ' -l -c '  + command
   171         return Cygwin.execute(Cygwin.cygwin_bash, command, wait_return, window)
   172     
   173     @staticmethod
   174     def cmdExecute(command, wait_return=True, window = False, bash_opts=''):
   175         command = ' /c ' + command 
   176         return Cygwin.execute(Cygwin.win_cmd, command, wait_return, window)
   177 
   178     # executes command over ssh on guest vm
   179     @staticmethod
   180     def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
   181         command = ' -v -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command        
   182         return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)     
   183     
   184     #machineFolder + '/' + vm_name + '/dvm_key
   185     #address = self.getHostOnlyIP(vm_name)
   186     #machineFolder = self.getDefaultMachineFolder()
   187     #machineFolder = Cygwin.cygwinPath(machineFolder)
   188     
   189     # executes command over ssh on guest vm with X forwarding
   190     @staticmethod
   191     def sshExecuteX11(command, address, user_name, certificate, wait_return=True):
   192         return Cygwin.bashExecute('"DISPLAY=:0.0 /usr/bin/ssh -v -Y -i \\"' + certificate +'\\" ' + user_name + '@' + address + ' ' + command + '\"')
   193 
   194     @staticmethod
   195     def is_X11_running():
   196         """check if we can connect to a X11 running instance"""
   197         p = Cygwin.bashExecute('"DISPLAY=:0 /usr/bin/xset -q"')
   198         return p[0] == 0
   199         
   200         
   201     @staticmethod
   202     def start_X11():
   203         """start X11 in the background (if not already running) on DISPLAY=:0"""
   204         # do not start if already running
   205         if Cygwin.is_X11_running():
   206             return           
   207         # launch X11 (forget output and return immediately)
   208         return Cygwin.execute(Cygwin.cygwin_x11, ':0 -multiwindow', wait_return = False, window = False)
   209     
   210     @staticmethod    
   211     def cygPath(path):
   212         cmd = '"/usr/bin/cygpath -u \\"' + path + '\\""'
   213         return Cygwin.bashExecute(cmd)[1].rstrip('\n')
   214     
   215 # start
   216 if __name__ == "__main__":
   217     logger = setupLogger('Cygwin')
   218     c = Cygwin()
   219     logger.info(c.root())
   220     logger.info(c.bin())
   221     logger.info(c.bash())
   222     logger.info(c.ssh())
   223     
   224     c.cygPath('C:')
   225     c.start_X11()
   226 
   227         
   228     
   229