131
131
# is given in BZR_SSH. See https://bugs.launchpad.net/bugs/414743
132
132
elif 'plink' in version and progname == 'plink':
133
133
# Checking if "plink" was the executed argument as Windows
134
# sometimes reports 'ssh -V' incorrectly with 'plink' in it's
134
# sometimes reports 'ssh -V' incorrectly with 'plink' in its
135
135
# version. See https://bugs.launchpad.net/bzr/+bug/107155
136
136
trace.mutter("ssh implementation is Putty's plink.")
137
137
vendor = PLinkSubprocessVendor()
239
239
def connect_ssh(self, username, password, host, port, command):
240
240
"""Make an SSH connection.
242
:returns: something with a `close` method, and a `get_filelike_channels`
243
method that returns a pair of (read, write) filelike objects.
242
:returns: an SSHConnection.
245
244
raise NotImplementedError(self.connect_ssh)
269
268
register_ssh_vendor('loopback', LoopbackVendor())
272
class _ParamikoSSHConnection(object):
273
def __init__(self, channel):
274
self.channel = channel
276
def get_filelike_channels(self):
277
return self.channel.makefile('rb'), self.channel.makefile('wb')
280
return self.channel.close()
283
271
class ParamikoVendor(SSHVendor):
284
272
"""Vendor that uses paramiko."""
348
336
self._raise_connection_error(host, port=port, orig_error=e,
349
337
msg='Unable to invoke remote bzr')
339
_ssh_connection_errors = (EOFError, OSError, IOError, socket.error)
351
340
if paramiko is not None:
352
341
vendor = ParamikoVendor()
353
342
register_ssh_vendor('paramiko', vendor)
354
343
register_ssh_vendor('none', vendor)
355
344
register_default_ssh_vendor(vendor)
356
_sftp_connection_errors = (EOFError, paramiko.SSHException)
345
_ssh_connection_errors += (paramiko.SSHException,)
359
_sftp_connection_errors = (EOFError,)
362
349
class SubprocessVendor(SSHVendor):
363
350
"""Abstract base class for vendors that use pipes to a subprocess."""
365
352
def _connect(self, argv):
366
proc = subprocess.Popen(argv,
367
stdin=subprocess.PIPE,
368
stdout=subprocess.PIPE,
353
# Attempt to make a socketpair to use as stdin/stdout for the SSH
354
# subprocess. We prefer sockets to pipes because they support
355
# non-blocking short reads, allowing us to optimistically read 64k (or
358
my_sock, subproc_sock = socket.socketpair()
359
except (AttributeError, socket.error):
360
# This platform doesn't support socketpair(), so just use ordinary
362
stdin = stdout = subprocess.PIPE
365
stdin = stdout = subproc_sock
367
proc = subprocess.Popen(argv, stdin=stdin, stdout=stdout,
369
368
**os_specific_subprocess_params())
370
return SSHSubprocess(proc)
369
return SSHSubprocessConnection(proc, sock=sock)
372
371
def connect_sftp(self, username, password, host, port):
375
374
subsystem='sftp')
376
375
sock = self._connect(argv)
377
376
return SFTPClient(SocketAsChannelAdapter(sock))
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,):
377
except _ssh_connection_errors, e:
386
378
self._raise_connection_error(host, port=port, orig_error=e)
388
380
def connect_ssh(self, username, password, host, port, command):
390
382
argv = self._get_vendor_specific_argv(username, host, port,
392
384
return self._connect(argv)
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,):
385
except _ssh_connection_errors, e:
401
386
self._raise_connection_error(host, port=port, orig_error=e)
403
388
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
645
630
_subproc_weakrefs = set()
647
632
def _close_ssh_proc(proc):
648
for func in [proc.stdin.close, proc.stdout.close, proc.wait]:
633
"""Carefully close stdin/stdout and reap the SSH process.
635
If the pipes are already closed and/or the process has already been
636
wait()ed on, that's ok, and no error is raised. The goal is to do our best
637
to clean up (whether or not a clean up was already tried).
639
dotted_names = ['stdin.close', 'stdout.close', 'wait']
640
for dotted_name in dotted_names:
641
attrs = dotted_name.split('.')
645
obj = getattr(obj, attr)
646
except AttributeError:
647
# It's ok for proc.stdin or proc.stdout to be None.
655
class SSHSubprocess(object):
656
"""A socket-like object that talks to an ssh subprocess via pipes."""
658
def __init__(self, proc):
652
# It's ok for the pipe to already be closed, or the process to
653
# already be finished.
657
class SSHConnection(object):
658
"""Abstract base class for SSH connections."""
660
def get_sock_or_pipes(self):
661
"""Returns a (kind, io_object) pair.
663
If kind == 'socket', then io_object is a socket.
665
If kind == 'pipes', then io_object is a pair of file-like objects
666
(read_from, write_to).
668
raise NotImplementedError(self.get_sock_or_pipes)
671
raise NotImplementedError(self.close)
674
class SSHSubprocessConnection(SSHConnection):
675
"""A connection to an ssh subprocess via pipes or a socket.
677
This class is also socket-like enough to be used with
678
SocketAsChannelAdapter (it has 'send' and 'recv' methods).
681
def __init__(self, proc, sock=None):
684
:param proc: a subprocess.Popen
685
:param sock: if proc.stdin/out is a socket from a socketpair, then sock
686
should bzrlib's half of that socketpair. If not passed, proc's
687
stdin/out is assumed to be ordinary pipes.
660
691
# Add a weakref to proc that will attempt to do the same as self.close
661
692
# to avoid leaving processes lingering indefinitely.
662
693
def terminate(ref):
665
696
_subproc_weakrefs.add(weakref.ref(self, terminate))
667
698
def send(self, data):
668
return os.write(self.proc.stdin.fileno(), data)
699
if self._sock is not None:
700
return self._sock.send(data)
702
return os.write(self.proc.stdin.fileno(), data)
670
704
def recv(self, count):
671
return os.read(self.proc.stdout.fileno(), count)
705
if self._sock is not None:
706
return self._sock.recv(count)
708
return os.read(self.proc.stdout.fileno(), count)
674
711
_close_ssh_proc(self.proc)
676
def get_filelike_channels(self):
677
return (self.proc.stdout, self.proc.stdin)
713
def get_sock_or_pipes(self):
714
if self._sock is not None:
715
return 'socket', self._sock
717
return 'pipes', (self.proc.stdout, self.proc.stdin)
720
class _ParamikoSSHConnection(SSHConnection):
721
"""An SSH connection via paramiko."""
723
def __init__(self, channel):
724
self.channel = channel
726
def get_sock_or_pipes(self):
727
return ('socket', self.channel)
730
return self.channel.close()