126
126
elif 'SSH Secure Shell' in version:
127
127
trace.mutter('ssh implementation is SSH Corp.')
128
128
vendor = SSHCorpSubprocessVendor()
129
elif 'lsh' in version:
130
trace.mutter('ssh implementation is GNU lsh.')
131
vendor = LSHSubprocessVendor()
132
129
# As plink user prompts are not handled currently, don't auto-detect
133
130
# it by inspection below, but keep this vendor detection for if a path
134
131
# is given in BZR_SSH. See https://bugs.launchpad.net/bugs/414743
135
132
elif 'plink' in version and progname == 'plink':
136
133
# Checking if "plink" was the executed argument as Windows
137
# sometimes reports 'ssh -V' incorrectly with 'plink' in its
134
# sometimes reports 'ssh -V' incorrectly with 'plink' in it's
138
135
# version. See https://bugs.launchpad.net/bzr/+bug/107155
139
136
trace.mutter("ssh implementation is Putty's plink.")
140
137
vendor = PLinkSubprocessVendor()
339
348
self._raise_connection_error(host, port=port, orig_error=e,
340
349
msg='Unable to invoke remote bzr')
342
_ssh_connection_errors = (EOFError, OSError, IOError, socket.error)
343
351
if paramiko is not None:
344
352
vendor = ParamikoVendor()
345
353
register_ssh_vendor('paramiko', vendor)
346
354
register_ssh_vendor('none', vendor)
347
355
register_default_ssh_vendor(vendor)
348
_ssh_connection_errors += (paramiko.SSHException,)
356
_sftp_connection_errors = (EOFError, paramiko.SSHException)
359
_sftp_connection_errors = (EOFError,)
352
362
class SubprocessVendor(SSHVendor):
353
363
"""Abstract base class for vendors that use pipes to a subprocess."""
355
365
def _connect(self, argv):
356
# Attempt to make a socketpair to use as stdin/stdout for the SSH
357
# subprocess. We prefer sockets to pipes because they support
358
# non-blocking short reads, allowing us to optimistically read 64k (or
361
my_sock, subproc_sock = socket.socketpair()
362
osutils.set_fd_cloexec(my_sock)
363
except (AttributeError, socket.error):
364
# This platform doesn't support socketpair(), so just use ordinary
366
stdin = stdout = subprocess.PIPE
367
my_sock, subproc_sock = None, None
369
stdin = stdout = subproc_sock
370
proc = subprocess.Popen(argv, stdin=stdin, stdout=stdout,
366
proc = subprocess.Popen(argv,
367
stdin=subprocess.PIPE,
368
stdout=subprocess.PIPE,
371
369
**os_specific_subprocess_params())
372
if subproc_sock is not None:
374
return SSHSubprocessConnection(proc, sock=my_sock)
370
return SSHSubprocess(proc)
376
372
def connect_sftp(self, username, password, host, port):
379
375
subsystem='sftp')
380
376
sock = self._connect(argv)
381
377
return SFTPClient(SocketAsChannelAdapter(sock))
382
except _ssh_connection_errors, e:
378
except _sftp_connection_errors, e:
379
self._raise_connection_error(host, port=port, orig_error=e)
380
except (OSError, IOError), e:
381
# If the machine is fast enough, ssh can actually exit
382
# before we try and send it the sftp request, which
383
# raises a Broken Pipe
384
if e.errno not in (errno.EPIPE,):
383
386
self._raise_connection_error(host, port=port, orig_error=e)
385
388
def connect_ssh(self, username, password, host, port, command):
387
390
argv = self._get_vendor_specific_argv(username, host, port,
389
392
return self._connect(argv)
390
except _ssh_connection_errors, e:
393
except (EOFError), e:
394
self._raise_connection_error(host, port=port, orig_error=e)
395
except (OSError, IOError), e:
396
# If the machine is fast enough, ssh can actually exit
397
# before we try and send it the sftp request, which
398
# raises a Broken Pipe
399
if e.errno not in (errno.EPIPE,):
391
401
self._raise_connection_error(host, port=port, orig_error=e)
393
403
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
444
454
register_ssh_vendor('sshcorp', SSHCorpSubprocessVendor())
447
class LSHSubprocessVendor(SubprocessVendor):
448
"""SSH vendor that uses the 'lsh' executable from GNU"""
450
executable_path = 'lsh'
452
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
454
args = [self.executable_path]
456
args.extend(['-p', str(port)])
457
if username is not None:
458
args.extend(['-l', username])
459
if subsystem is not None:
460
args.extend(['--subsystem', subsystem, host])
462
args.extend([host] + command)
465
register_ssh_vendor('lsh', LSHSubprocessVendor())
468
457
class PLinkSubprocessVendor(SubprocessVendor):
469
458
"""SSH vendor that uses the 'plink' executable from Putty."""
656
645
_subproc_weakrefs = set()
658
def _close_ssh_proc(proc, sock):
659
"""Carefully close stdin/stdout and reap the SSH process.
661
If the pipes are already closed and/or the process has already been
662
wait()ed on, that's ok, and no error is raised. The goal is to do our best
663
to clean up (whether or not a clean up was already tried).
666
for closeable in (proc.stdin, proc.stdout, sock):
667
# We expect that either proc (a subprocess.Popen) will have stdin and
668
# stdout streams to close, or that we will have been passed a socket to
669
# close, with the option not in use being None.
670
if closeable is not None:
671
funcs.append(closeable.close)
672
funcs.append(proc.wait)
647
def _close_ssh_proc(proc):
648
for func in [proc.stdin.close, proc.stdout.close, proc.wait]:
677
# It's ok for the pipe to already be closed, or the process to
678
# already be finished.
682
class SSHConnection(object):
683
"""Abstract base class for SSH connections."""
685
def get_sock_or_pipes(self):
686
"""Returns a (kind, io_object) pair.
688
If kind == 'socket', then io_object is a socket.
690
If kind == 'pipes', then io_object is a pair of file-like objects
691
(read_from, write_to).
693
raise NotImplementedError(self.get_sock_or_pipes)
696
raise NotImplementedError(self.close)
699
class SSHSubprocessConnection(SSHConnection):
700
"""A connection to an ssh subprocess via pipes or a socket.
702
This class is also socket-like enough to be used with
703
SocketAsChannelAdapter (it has 'send' and 'recv' methods).
706
def __init__(self, proc, sock=None):
709
:param proc: a subprocess.Popen
710
:param sock: if proc.stdin/out is a socket from a socketpair, then sock
711
should bzrlib's half of that socketpair. If not passed, proc's
712
stdin/out is assumed to be ordinary pipes.
655
class SSHSubprocess(object):
656
"""A socket-like object that talks to an ssh subprocess via pipes."""
658
def __init__(self, proc):
716
660
# Add a weakref to proc that will attempt to do the same as self.close
717
661
# to avoid leaving processes lingering indefinitely.
718
662
def terminate(ref):
719
663
_subproc_weakrefs.remove(ref)
720
_close_ssh_proc(proc, sock)
664
_close_ssh_proc(proc)
721
665
_subproc_weakrefs.add(weakref.ref(self, terminate))
723
667
def send(self, data):
724
if self._sock is not None:
725
return self._sock.send(data)
727
return os.write(self.proc.stdin.fileno(), data)
668
return os.write(self.proc.stdin.fileno(), data)
729
670
def recv(self, count):
730
if self._sock is not None:
731
return self._sock.recv(count)
733
return os.read(self.proc.stdout.fileno(), count)
736
_close_ssh_proc(self.proc, self._sock)
738
def get_sock_or_pipes(self):
739
if self._sock is not None:
740
return 'socket', self._sock
742
return 'pipes', (self.proc.stdout, self.proc.stdin)
745
class _ParamikoSSHConnection(SSHConnection):
746
"""An SSH connection via paramiko."""
748
def __init__(self, channel):
749
self.channel = channel
751
def get_sock_or_pipes(self):
752
return ('socket', self.channel)
755
return self.channel.close()
671
return os.read(self.proc.stdout.fileno(), count)
674
_close_ssh_proc(self.proc)
676
def get_filelike_channels(self):
677
return (self.proc.stdout, self.proc.stdin)