2 # -*- coding: utf-8 -*-
4 # ------------------------------------------------------------
7 # executes a cygwin command inside the opensecurity project
9 # Autor: Mihai Bartha, <mihai.bartha@ait.ac.at>
10 # Oliver Maurhart, <oliver.maurhart@ait.ac.at>
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
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.
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.
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 # ------------------------------------------------------------
33 # ------------------------------------------------------------
40 from subprocess import Popen, PIPE, call, STARTUPINFO, _subprocess
42 from environment import Environment
43 from opensecurity_util import logger, setupLogger, OpenSecurityException
44 # ------------------------------------------------------------
48 """get the path to our local cygwin installment"""
49 home_drive = os.path.expandvars("%HOMEDRIVE%") + os.sep
51 os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin')),
52 os.path.abspath(os.path.join(Environment('OpenSecurity').prefix_path, '..', 'cygwin64')),
53 os.path.abspath(os.path.join(home_drive, 'cygwin')),
54 os.path.abspath(os.path.join(home_drive, 'cygwin64'))
56 path_valid = [ p for p in path_hint if os.path.exists(p) ]
58 theClass.cygwin_root = path_valid[0]
59 theClass.cygwin_bin = os.path.join(theClass.cygwin_root, 'bin') + os.path.sep
60 theClass.cygwin_bash = os.path.join(theClass.cygwin_bin, 'bash.exe')
61 theClass.cygwin_ssh = os.path.join(theClass.cygwin_bin, 'ssh.exe')
62 theClass.win_cmd = os.environ.get("COMSPEC", "cmd.exe")
64 """get the path to the VirtualBox installation on this system"""
66 k = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\Oracle\VirtualBox')
67 theClass.vbox_root = _winreg.QueryValueEx(k, 'InstallDir')[0]
68 theClass.vbox_man = os.path.join(theClass.vbox_root, 'VBoxManage.exe')
83 """Some nifty methods working with Cygwin"""
85 def __call__(self, command, arguments, wait_return=True, window = False):
86 """make an instance of this object act as a function"""
87 return self.execute(command, arguments, wait_return, window)
92 return Cygwin.cygwin_root
96 return Cygwin.cygwin_bin
100 return Cygwin.cygwin_bash
104 return Cygwin.cygwin_ssh
108 return Cygwin.vbox_man
112 return Cygwin.win_cmd
114 #executes command on host system
116 def execute(program, arguments, wait_return=True, window = False):
117 _startupinfo = STARTUPINFO()
119 _startupinfo.dwFlags |= _subprocess.STARTF_USESHOWWINDOW
120 _startupinfo.wShowWindow = _subprocess.SW_HIDE
122 logger.debug('trying to launch: ' + program + ' ' + ''.join(arguments))
124 process = Popen(executable=program, args=' ' + arguments, startupinfo = _startupinfo, stdin=PIPE, stdout=PIPE, stderr=PIPE, shell = False)
125 logger.debug('Launched: ' + program + ' ' + ''.join(arguments))
127 return [0, 'working in background', '']
128 result = process.wait()
129 res_stdout = process.stdout.read();
130 res_stderr = process.stderr.read();
132 logger.debug(res_stdout)
134 logger.debug(res_stderr)
136 logger.error('Failed to execute cygwin command.\n\tcommand=' + program + ' ' + ''.join(arguments) + '\n')
137 #TODO: throw exception
138 return result, res_stdout, res_stderr
141 def vboxExecute(command, wait_return=True, window = False, bash_opts=''):
142 return Cygwin.execute(Cygwin.vbox_man, command, wait_return, window)
145 def bashExecute(command, wait_return=True, window = False, bash_opts=''):
146 command = bash_opts + ' --login -c ' + command
147 return Cygwin.execute(Cygwin.cygwin_bash, command, wait_return, window)
150 def cmdExecute(command, wait_return=True, window = False, bash_opts=''):
151 command = ' /c ' + command
152 return Cygwin.execute(Cygwin.win_cmd, command, wait_return, window)
154 # executes command over ssh on guest vm
156 def sshExecute(command, address, user_name, certificate, wait_return=True, window = False):
157 command = ' -v -i "' + certificate + '" ' + user_name + '@' + address + ' ' + command
158 return Cygwin.execute(Cygwin.cygwin_ssh, command, wait_return, window)
160 #machineFolder + '/' + vm_name + '/dvm_key
161 #address = self.getHostOnlyIP(vm_name)
162 #machineFolder = self.getDefaultMachineFolder()
163 #machineFolder = Cygwin.cygwinPath(machineFolder)
165 # executes command over ssh on guest vm with X forwarding
167 def sshExecuteX11(command, address, user_name, certificate, wait_return=True):
168 return Cygwin.bashExecute('"DISPLAY=:0.0 /usr/bin/ssh -v -Y -i \\"' + certificate +'\\" ' + user_name + '@' + address + ' ' + command + '\"')
171 def is_X11_running():
172 """check if we can connect to a X11 running instance"""
173 p = Cygwin.bashExecute('"DISPLAY=:0 /usr/bin/xset -q"')
179 """start X11 in the background (if not already running) on DISPLAY=:0"""
180 # do not start if already running
181 if Cygwin.is_X11_running():
183 # launch X11 (forget output and return immediately)
184 return Cygwin.bashExecute('"/usr/bin/X :0 -multiwindow"', bash_opts=' -i ', wait_return = False)
188 cmd = '"/usr/bin/cygpath -u \\"' + path + '\\""'
189 return Cygwin.bashExecute(cmd)[1].rstrip('\n')
192 if __name__ == "__main__":
193 logger = setupLogger('Cygwin')
195 logger.info(c.root())
197 logger.info(c.bash())