You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
83 lines
2.0 KiB
Python
83 lines
2.0 KiB
Python
7 years ago
|
#!/usr/bin/env python
|
||
|
|
||
|
import optparse
|
||
|
import os
|
||
|
import shlex
|
||
7 years ago
|
import subprocess
|
||
7 years ago
|
import sys
|
||
|
|
||
6 years ago
|
|
||
|
HOST_KEY_ASK_MSG = """
|
||
|
The authenticity of host '[91.121.165.123]:9122 ([91.121.165.123]:9122)' can't be established.
|
||
|
ECDSA key fingerprint is SHA256:JvfPvazZzQ9/CUdKN7tiYlNZtDRdEgDsYVIzOgPrsR4.
|
||
|
Are you sure you want to continue connecting (yes/no)?
|
||
|
""".strip('\n')
|
||
|
|
||
|
HOST_KEY_STRICT_MSG = """Host key verification failed.\n"""
|
||
|
|
||
6 years ago
|
PERMDENIED_CLASSIC_MSG = 'Permission denied (publickey,password)\n'
|
||
|
PERMDENIED_75_MSG = 'chicken@nandos.com: permission denied (publickey,password)\n'
|
||
|
|
||
|
|
||
6 years ago
|
def tty(msg):
|
||
6 years ago
|
fp = open('/dev/tty', 'wb', 0)
|
||
|
fp.write(msg.encode())
|
||
6 years ago
|
fp.close()
|
||
|
|
||
|
|
||
|
def stderr(msg):
|
||
6 years ago
|
fp = open('/dev/stderr', 'wb', 0)
|
||
|
fp.write(msg.encode())
|
||
6 years ago
|
fp.close()
|
||
|
|
||
|
|
||
|
def confirm(msg):
|
||
|
tty(msg)
|
||
6 years ago
|
fp = open('/dev/tty', 'rb', 0)
|
||
6 years ago
|
try:
|
||
6 years ago
|
return fp.readline().decode()
|
||
6 years ago
|
finally:
|
||
|
fp.close()
|
||
|
|
||
|
|
||
6 years ago
|
mode = os.getenv('STUBSSH_MODE')
|
||
6 years ago
|
|
||
|
if mode == 'ask':
|
||
6 years ago
|
assert 'yes\n' == confirm(HOST_KEY_ASK_MSG)
|
||
6 years ago
|
|
||
6 years ago
|
elif mode == 'strict':
|
||
6 years ago
|
stderr(HOST_KEY_STRICT_MSG)
|
||
|
sys.exit(255)
|
||
|
|
||
6 years ago
|
elif mode == 'permdenied_classic':
|
||
|
stderr(PERMDENIED_CLASSIC_MSG)
|
||
|
sys.exit(255)
|
||
|
|
||
|
elif mode == 'permdenied_75':
|
||
|
stderr(PERMDENIED_75_MSG)
|
||
|
sys.exit(255)
|
||
|
|
||
6 years ago
|
|
||
6 years ago
|
#
|
||
|
# Set an env var if stderr was a TTY to make ssh_test tests easier to write.
|
||
|
#
|
||
|
if os.isatty(2):
|
||
|
os.environ['STDERR_WAS_TTY'] = '1'
|
||
|
|
||
|
|
||
7 years ago
|
parser = optparse.OptionParser()
|
||
|
parser.add_option('--user', '-l', action='store')
|
||
7 years ago
|
parser.add_option('-o', dest='options', action='append')
|
||
7 years ago
|
parser.disable_interspersed_args()
|
||
|
|
||
|
opts, args = parser.parse_args(sys.argv[1:])
|
||
|
args.pop(0) # hostname
|
||
7 years ago
|
|
||
|
# On Linux the TTY layer appears to begin tearing down a PTY after the last FD
|
||
|
# for it is closed, causing SIGHUP to be sent to its foreground group. Since
|
||
|
# the bootstrap overwrites the last such fd (stderr), we can't just exec it
|
||
|
# directly, we must hold it open just like real SSH would. So use
|
||
|
# subprocess.call() rather than os.execve() here.
|
||
7 years ago
|
args = [''.join(shlex.split(s)) for s in args]
|
||
7 years ago
|
sys.exit(subprocess.call(args))
|