1
# Copyright (C) 2006-2011 Robey Pointer <robey@lag.net>
2
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Foundation SSH support for SFTP and smart server."""
27
from binascii import hexlify
40
except ImportError as e:
41
# If we have an ssh subprocess, we don't strictly need paramiko for all ssh
45
from paramiko.sftp_client import SFTPClient
48
class StrangeHostname(errors.BzrError):
49
_fmt = "Refusing to connect to strange SSH hostname %(hostname)s"
56
class SSHVendorManager(object):
57
"""Manager for manage SSH vendors."""
59
# Note, although at first sign the class interface seems similar to
60
# breezy.registry.Registry it is not possible/convenient to directly use
61
# the Registry because the class just has "get()" interface instead of the
62
# Registry's "get(key)".
65
self._ssh_vendors = {}
66
self._cached_ssh_vendor = None
67
self._default_ssh_vendor = None
69
def register_default_vendor(self, vendor):
70
"""Register default SSH vendor."""
71
self._default_ssh_vendor = vendor
73
def register_vendor(self, name, vendor):
74
"""Register new SSH vendor by name."""
75
self._ssh_vendors[name] = vendor
77
def clear_cache(self):
78
"""Clear previously cached lookup result."""
79
self._cached_ssh_vendor = None
81
def _get_vendor_by_config(self):
82
vendor_name = config.GlobalStack().get('ssh')
83
if vendor_name is not None:
85
vendor = self._ssh_vendors[vendor_name]
87
vendor = self._get_vendor_from_path(vendor_name)
89
raise errors.UnknownSSH(vendor_name)
90
vendor.executable_path = vendor_name
94
def _get_ssh_version_string(self, args):
95
"""Return SSH version string from the subprocess."""
97
p = subprocess.Popen(args,
98
stdout=subprocess.PIPE,
99
stderr=subprocess.PIPE,
101
**os_specific_subprocess_params())
102
stdout, stderr = p.communicate()
104
stdout = stderr = b''
105
return (stdout + stderr).decode(osutils.get_terminal_encoding())
107
def _get_vendor_by_version_string(self, version, progname):
108
"""Return the vendor or None based on output from the subprocess.
110
:param version: The output of 'ssh -V' like command.
111
:param args: Command line that was run.
114
if 'OpenSSH' in version:
115
trace.mutter('ssh implementation is OpenSSH')
116
vendor = OpenSSHSubprocessVendor()
117
elif 'SSH Secure Shell' in version:
118
trace.mutter('ssh implementation is SSH Corp.')
119
vendor = SSHCorpSubprocessVendor()
120
elif 'lsh' in version:
121
trace.mutter('ssh implementation is GNU lsh.')
122
vendor = LSHSubprocessVendor()
123
# As plink user prompts are not handled currently, don't auto-detect
124
# it by inspection below, but keep this vendor detection for if a path
125
# is given in BRZ_SSH. See https://bugs.launchpad.net/bugs/414743
126
elif 'plink' in version and progname == 'plink':
127
# Checking if "plink" was the executed argument as Windows
128
# sometimes reports 'ssh -V' incorrectly with 'plink' in its
129
# version. See https://bugs.launchpad.net/bzr/+bug/107155
130
trace.mutter("ssh implementation is Putty's plink.")
131
vendor = PLinkSubprocessVendor()
134
def _get_vendor_by_inspection(self):
135
"""Return the vendor or None by checking for known SSH implementations."""
136
version = self._get_ssh_version_string(['ssh', '-V'])
137
return self._get_vendor_by_version_string(version, "ssh")
139
def _get_vendor_from_path(self, path):
140
"""Return the vendor or None using the program at the given path"""
141
version = self._get_ssh_version_string([path, '-V'])
142
return self._get_vendor_by_version_string(version,
143
os.path.splitext(os.path.basename(path))[0])
145
def get_vendor(self):
146
"""Find out what version of SSH is on the system.
148
:raises SSHVendorNotFound: if no any SSH vendor is found
149
:raises UnknownSSH: if the BRZ_SSH environment variable contains
152
if self._cached_ssh_vendor is None:
153
vendor = self._get_vendor_by_config()
155
vendor = self._get_vendor_by_inspection()
157
trace.mutter('falling back to default implementation')
158
vendor = self._default_ssh_vendor
160
raise errors.SSHVendorNotFound()
161
self._cached_ssh_vendor = vendor
162
return self._cached_ssh_vendor
165
_ssh_vendor_manager = SSHVendorManager()
166
_get_ssh_vendor = _ssh_vendor_manager.get_vendor
167
register_default_ssh_vendor = _ssh_vendor_manager.register_default_vendor
168
register_ssh_vendor = _ssh_vendor_manager.register_vendor
171
def _ignore_signals():
172
# TODO: This should possibly ignore SIGHUP as well, but bzr currently
173
# doesn't handle it itself.
174
# <https://launchpad.net/products/bzr/+bug/41433/+index>
176
signal.signal(signal.SIGINT, signal.SIG_IGN)
177
# GZ 2010-02-19: Perhaps make this check if breakin is installed instead
178
if signal.getsignal(signal.SIGQUIT) != signal.SIG_DFL:
179
signal.signal(signal.SIGQUIT, signal.SIG_IGN)
182
class SocketAsChannelAdapter(object):
183
"""Simple wrapper for a socket that pretends to be a paramiko Channel."""
185
def __init__(self, sock):
189
return "bzr SocketAsChannelAdapter"
191
def send(self, data):
192
return self.__socket.send(data)
196
return self.__socket.recv(n)
197
except socket.error as e:
198
if e.args[0] in (errno.EPIPE, errno.ECONNRESET, errno.ECONNABORTED,
200
# Connection has closed. Paramiko expects an empty string in
201
# this case, not an exception.
205
def recv_ready(self):
206
# TODO: jam 20051215 this function is necessary to support the
207
# pipelined() function. In reality, it probably should use
208
# poll() or select() to actually return if there is data
209
# available, otherwise we probably don't get any benefit
213
self.__socket.close()
216
class SSHVendor(object):
217
"""Abstract base class for SSH vendor implementations."""
219
def connect_sftp(self, username, password, host, port):
220
"""Make an SSH connection, and return an SFTPClient.
222
:param username: an ascii string
223
:param password: an ascii string
224
:param host: a host name as an ascii string
225
:param port: a port number
228
:raises: ConnectionError if it cannot connect.
230
:rtype: paramiko.sftp_client.SFTPClient
232
raise NotImplementedError(self.connect_sftp)
234
def connect_ssh(self, username, password, host, port, command):
235
"""Make an SSH connection.
237
:returns: an SSHConnection.
239
raise NotImplementedError(self.connect_ssh)
241
def _raise_connection_error(self, host, port=None, orig_error=None,
242
msg='Unable to connect to SSH host'):
243
"""Raise a SocketConnectionError with properly formatted host.
245
This just unifies all the locations that try to raise ConnectionError,
246
so that they format things properly.
248
raise errors.SocketConnectionError(host=host, port=port, msg=msg,
249
orig_error=orig_error)
252
class LoopbackVendor(SSHVendor):
253
"""SSH "vendor" that connects over a plain TCP socket, not SSH."""
255
def connect_sftp(self, username, password, host, port):
256
sock = socket.socket()
258
sock.connect((host, port))
259
except socket.error as e:
260
self._raise_connection_error(host, port=port, orig_error=e)
261
return SFTPClient(SocketAsChannelAdapter(sock))
264
register_ssh_vendor('loopback', LoopbackVendor())
267
class ParamikoVendor(SSHVendor):
268
"""Vendor that uses paramiko."""
270
def _hexify(self, s):
271
return hexlify(s).upper()
273
def _connect(self, username, password, host, port):
274
global SYSTEM_HOSTKEYS, BRZ_HOSTKEYS
279
t = paramiko.Transport((host, port or 22))
280
t.set_log_channel('bzr.paramiko')
282
except (paramiko.SSHException, socket.error) as e:
283
self._raise_connection_error(host, port=port, orig_error=e)
285
server_key = t.get_remote_server_key()
286
server_key_hex = self._hexify(server_key.get_fingerprint())
287
keytype = server_key.get_name()
288
if host in SYSTEM_HOSTKEYS and keytype in SYSTEM_HOSTKEYS[host]:
289
our_server_key = SYSTEM_HOSTKEYS[host][keytype]
290
our_server_key_hex = self._hexify(our_server_key.get_fingerprint())
291
elif host in BRZ_HOSTKEYS and keytype in BRZ_HOSTKEYS[host]:
292
our_server_key = BRZ_HOSTKEYS[host][keytype]
293
our_server_key_hex = self._hexify(our_server_key.get_fingerprint())
295
trace.warning('Adding %s host key for %s: %s'
296
% (keytype, host, server_key_hex))
297
add = getattr(BRZ_HOSTKEYS, 'add', None)
298
if add is not None: # paramiko >= 1.X.X
299
BRZ_HOSTKEYS.add(host, keytype, server_key)
301
BRZ_HOSTKEYS.setdefault(host, {})[keytype] = server_key
302
our_server_key = server_key
303
our_server_key_hex = self._hexify(our_server_key.get_fingerprint())
305
if server_key != our_server_key:
306
filename1 = os.path.expanduser('~/.ssh/known_hosts')
307
filename2 = _ssh_host_keys_config_dir()
308
raise errors.TransportError(
309
'Host keys for %s do not match! %s != %s' %
310
(host, our_server_key_hex, server_key_hex),
311
['Try editing %s or %s' % (filename1, filename2)])
313
_paramiko_auth(username, password, host, port, t)
316
def connect_sftp(self, username, password, host, port):
317
t = self._connect(username, password, host, port)
319
return t.open_sftp_client()
320
except paramiko.SSHException as e:
321
self._raise_connection_error(host, port=port, orig_error=e,
322
msg='Unable to start sftp client')
324
def connect_ssh(self, username, password, host, port, command):
325
t = self._connect(username, password, host, port)
327
channel = t.open_session()
328
cmdline = ' '.join(command)
329
channel.exec_command(cmdline)
330
return _ParamikoSSHConnection(channel)
331
except paramiko.SSHException as e:
332
self._raise_connection_error(host, port=port, orig_error=e,
333
msg='Unable to invoke remote bzr')
336
_ssh_connection_errors = (EOFError, OSError, IOError, socket.error)
337
if paramiko is not None:
338
vendor = ParamikoVendor()
339
register_ssh_vendor('paramiko', vendor)
340
register_ssh_vendor('none', vendor)
341
register_default_ssh_vendor(vendor)
342
_ssh_connection_errors += (paramiko.SSHException,)
346
class SubprocessVendor(SSHVendor):
347
"""Abstract base class for vendors that use pipes to a subprocess."""
349
# In general stderr should be inherited from the parent process so prompts
350
# are visible on the terminal. This can be overriden to another file for
351
# tests, but beware of using PIPE which may hang due to not being read.
352
_stderr_target = None
355
def _check_hostname(arg):
356
if arg.startswith('-'):
357
raise StrangeHostname(hostname=arg)
359
def _connect(self, argv):
360
# Attempt to make a socketpair to use as stdin/stdout for the SSH
361
# subprocess. We prefer sockets to pipes because they support
362
# non-blocking short reads, allowing us to optimistically read 64k (or
365
my_sock, subproc_sock = socket.socketpair()
366
osutils.set_fd_cloexec(my_sock)
367
except (AttributeError, socket.error):
368
# This platform doesn't support socketpair(), so just use ordinary
370
stdin = stdout = subprocess.PIPE
371
my_sock, subproc_sock = None, None
373
stdin = stdout = subproc_sock
374
proc = subprocess.Popen(argv, stdin=stdin, stdout=stdout,
375
stderr=self._stderr_target,
377
**os_specific_subprocess_params())
378
if subproc_sock is not None:
380
return SSHSubprocessConnection(proc, sock=my_sock)
382
def connect_sftp(self, username, password, host, port):
384
argv = self._get_vendor_specific_argv(username, host, port,
386
sock = self._connect(argv)
387
return SFTPClient(SocketAsChannelAdapter(sock))
388
except _ssh_connection_errors as e:
389
self._raise_connection_error(host, port=port, orig_error=e)
391
def connect_ssh(self, username, password, host, port, command):
393
argv = self._get_vendor_specific_argv(username, host, port,
395
return self._connect(argv)
396
except _ssh_connection_errors as e:
397
self._raise_connection_error(host, port=port, orig_error=e)
399
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
401
"""Returns the argument list to run the subprocess with.
403
Exactly one of 'subsystem' and 'command' must be specified.
405
raise NotImplementedError(self._get_vendor_specific_argv)
408
class OpenSSHSubprocessVendor(SubprocessVendor):
409
"""SSH vendor that uses the 'ssh' executable from OpenSSH."""
411
executable_path = 'ssh'
413
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
415
args = [self.executable_path,
416
'-oForwardX11=no', '-oForwardAgent=no',
417
'-oClearAllForwardings=yes',
418
'-oNoHostAuthenticationForLocalhost=yes']
420
args.extend(['-p', str(port)])
421
if username is not None:
422
args.extend(['-l', username])
423
if subsystem is not None:
424
args.extend(['-s', '--', host, subsystem])
426
args.extend(['--', host] + command)
430
register_ssh_vendor('openssh', OpenSSHSubprocessVendor())
433
class SSHCorpSubprocessVendor(SubprocessVendor):
434
"""SSH vendor that uses the 'ssh' executable from SSH Corporation."""
436
executable_path = 'ssh'
438
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
440
self._check_hostname(host)
441
args = [self.executable_path, '-x']
443
args.extend(['-p', str(port)])
444
if username is not None:
445
args.extend(['-l', username])
446
if subsystem is not None:
447
args.extend(['-s', subsystem, host])
449
args.extend([host] + command)
453
register_ssh_vendor('sshcorp', SSHCorpSubprocessVendor())
456
class LSHSubprocessVendor(SubprocessVendor):
457
"""SSH vendor that uses the 'lsh' executable from GNU"""
459
executable_path = 'lsh'
461
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
463
self._check_hostname(host)
464
args = [self.executable_path]
466
args.extend(['-p', str(port)])
467
if username is not None:
468
args.extend(['-l', username])
469
if subsystem is not None:
470
args.extend(['--subsystem', subsystem, host])
472
args.extend([host] + command)
476
register_ssh_vendor('lsh', LSHSubprocessVendor())
479
class PLinkSubprocessVendor(SubprocessVendor):
480
"""SSH vendor that uses the 'plink' executable from Putty."""
482
executable_path = 'plink'
484
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
486
self._check_hostname(host)
487
args = [self.executable_path, '-x', '-a', '-ssh', '-2', '-batch']
489
args.extend(['-P', str(port)])
490
if username is not None:
491
args.extend(['-l', username])
492
if subsystem is not None:
493
args.extend(['-s', host, subsystem])
495
args.extend([host] + command)
499
register_ssh_vendor('plink', PLinkSubprocessVendor())
502
def _paramiko_auth(username, password, host, port, paramiko_transport):
503
auth = config.AuthenticationConfig()
504
# paramiko requires a username, but it might be none if nothing was
505
# supplied. If so, use the local username.
507
username = auth.get_user('ssh', host, port=port,
508
default=getpass.getuser())
509
agent = paramiko.Agent()
510
for key in agent.get_keys():
511
trace.mutter('Trying SSH agent key %s'
512
% hexlify(key.get_fingerprint()).upper())
514
paramiko_transport.auth_publickey(username, key)
516
except paramiko.SSHException as e:
519
# okay, try finding id_rsa or id_dss? (posix only)
520
if _try_pkey_auth(paramiko_transport, paramiko.RSAKey, username, 'id_rsa'):
522
if _try_pkey_auth(paramiko_transport, paramiko.DSSKey, username, 'id_dsa'):
525
# If we have gotten this far, we are about to try for passwords, do an
526
# auth_none check to see if it is even supported.
527
supported_auth_types = []
529
# Note that with paramiko <1.7.5 this logs an INFO message:
530
# Authentication type (none) not permitted.
531
# So we explicitly disable the logging level for this action
532
old_level = paramiko_transport.logger.level
533
paramiko_transport.logger.setLevel(logging.WARNING)
535
paramiko_transport.auth_none(username)
537
paramiko_transport.logger.setLevel(old_level)
538
except paramiko.BadAuthenticationType as e:
539
# Supported methods are in the exception
540
supported_auth_types = e.allowed_types
541
except paramiko.SSHException as e:
542
# Don't know what happened, but just ignore it
544
# We treat 'keyboard-interactive' and 'password' auth methods identically,
545
# because Paramiko's auth_password method will automatically try
546
# 'keyboard-interactive' auth (using the password as the response) if
547
# 'password' auth is not available. Apparently some Debian and Gentoo
548
# OpenSSH servers require this.
549
# XXX: It's possible for a server to require keyboard-interactive auth that
550
# requires something other than a single password, but we currently don't
552
if ('password' not in supported_auth_types and
553
'keyboard-interactive' not in supported_auth_types):
554
raise errors.ConnectionError('Unable to authenticate to SSH host as'
555
'\n %s@%s\nsupported auth types: %s'
556
% (username, host, supported_auth_types))
560
paramiko_transport.auth_password(username, password)
562
except paramiko.SSHException as e:
565
# give up and ask for a password
566
password = auth.get_password('ssh', host, username, port=port)
567
# get_password can still return None, which means we should not prompt
568
if password is not None:
570
paramiko_transport.auth_password(username, password)
571
except paramiko.SSHException as e:
572
raise errors.ConnectionError(
573
'Unable to authenticate to SSH host as'
574
'\n %s@%s\n' % (username, host), e)
576
raise errors.ConnectionError('Unable to authenticate to SSH host as'
577
' %s@%s' % (username, host))
580
def _try_pkey_auth(paramiko_transport, pkey_class, username, filename):
581
filename = os.path.expanduser('~/.ssh/' + filename)
583
key = pkey_class.from_private_key_file(filename)
584
paramiko_transport.auth_publickey(username, key)
586
except paramiko.PasswordRequiredException:
587
password = ui.ui_factory.get_password(
588
prompt=u'SSH %(filename)s password',
589
filename=filename.decode(osutils._fs_enc))
591
key = pkey_class.from_private_key_file(filename, password)
592
paramiko_transport.auth_publickey(username, key)
594
except paramiko.SSHException:
595
trace.mutter('SSH authentication via %s key failed.'
596
% (os.path.basename(filename),))
597
except paramiko.SSHException:
598
trace.mutter('SSH authentication via %s key failed.'
599
% (os.path.basename(filename),))
605
def _ssh_host_keys_config_dir():
606
return osutils.pathjoin(bedding.config_dir(), 'ssh_host_keys')
609
def load_host_keys():
611
Load system host keys (probably doesn't work on windows) and any
612
"discovered" keys from previous sessions.
614
global SYSTEM_HOSTKEYS, BRZ_HOSTKEYS
616
SYSTEM_HOSTKEYS = paramiko.util.load_host_keys(
617
os.path.expanduser('~/.ssh/known_hosts'))
619
trace.mutter('failed to load system host keys: ' + str(e))
620
brz_hostkey_path = _ssh_host_keys_config_dir()
622
BRZ_HOSTKEYS = paramiko.util.load_host_keys(brz_hostkey_path)
624
trace.mutter('failed to load brz host keys: ' + str(e))
628
def save_host_keys():
630
Save "discovered" host keys in $(config)/ssh_host_keys/.
632
global SYSTEM_HOSTKEYS, BRZ_HOSTKEYS
633
bzr_hostkey_path = _ssh_host_keys_config_dir()
634
bedding.ensure_config_dir_exists()
637
with open(bzr_hostkey_path, 'w') as f:
638
f.write('# SSH host keys collected by bzr\n')
639
for hostname, keys in BRZ_HOSTKEYS.items():
640
for keytype, key in keys.items():
641
f.write('%s %s %s\n' %
642
(hostname, keytype, key.get_base64()))
644
trace.mutter('failed to save bzr host keys: ' + str(e))
647
def os_specific_subprocess_params():
648
"""Get O/S specific subprocess parameters."""
649
if sys.platform == 'win32':
650
# setting the process group and closing fds is not supported on
654
# We close fds other than the pipes as the child process does not need
657
# We also set the child process to ignore SIGINT. Normally the signal
658
# would be sent to every process in the foreground process group, but
659
# this causes it to be seen only by bzr and not by ssh. Python will
660
# generate a KeyboardInterrupt in bzr, and we will then have a chance
661
# to release locks or do other cleanup over ssh before the connection
663
# <https://launchpad.net/products/bzr/+bug/5987>
665
# Running it in a separate process group is not good because then it
666
# can't get non-echoed input of a password or passphrase.
667
# <https://launchpad.net/products/bzr/+bug/40508>
668
return {'preexec_fn': _ignore_signals,
674
_subproc_weakrefs = set()
677
def _close_ssh_proc(proc, sock):
678
"""Carefully close stdin/stdout and reap the SSH process.
680
If the pipes are already closed and/or the process has already been
681
wait()ed on, that's ok, and no error is raised. The goal is to do our best
682
to clean up (whether or not a clean up was already tried).
685
for closeable in (proc.stdin, proc.stdout, sock):
686
# We expect that either proc (a subprocess.Popen) will have stdin and
687
# stdout streams to close, or that we will have been passed a socket to
688
# close, with the option not in use being None.
689
if closeable is not None:
690
funcs.append(closeable.close)
691
funcs.append(proc.wait)
696
# It's ok for the pipe to already be closed, or the process to
697
# already be finished.
701
class SSHConnection(object):
702
"""Abstract base class for SSH connections."""
704
def get_sock_or_pipes(self):
705
"""Returns a (kind, io_object) pair.
707
If kind == 'socket', then io_object is a socket.
709
If kind == 'pipes', then io_object is a pair of file-like objects
710
(read_from, write_to).
712
raise NotImplementedError(self.get_sock_or_pipes)
715
raise NotImplementedError(self.close)
718
class SSHSubprocessConnection(SSHConnection):
719
"""A connection to an ssh subprocess via pipes or a socket.
721
This class is also socket-like enough to be used with
722
SocketAsChannelAdapter (it has 'send' and 'recv' methods).
725
def __init__(self, proc, sock=None):
728
:param proc: a subprocess.Popen
729
:param sock: if proc.stdin/out is a socket from a socketpair, then sock
730
should breezy's half of that socketpair. If not passed, proc's
731
stdin/out is assumed to be ordinary pipes.
735
# Add a weakref to proc that will attempt to do the same as self.close
736
# to avoid leaving processes lingering indefinitely.
739
_subproc_weakrefs.remove(ref)
740
_close_ssh_proc(proc, sock)
741
_subproc_weakrefs.add(weakref.ref(self, terminate))
743
def send(self, data):
744
if self._sock is not None:
745
return self._sock.send(data)
747
return os.write(self.proc.stdin.fileno(), data)
749
def recv(self, count):
750
if self._sock is not None:
751
return self._sock.recv(count)
753
return os.read(self.proc.stdout.fileno(), count)
756
_close_ssh_proc(self.proc, self._sock)
758
def get_sock_or_pipes(self):
759
if self._sock is not None:
760
return 'socket', self._sock
762
return 'pipes', (self.proc.stdout, self.proc.stdin)
765
class _ParamikoSSHConnection(SSHConnection):
766
"""An SSH connection via paramiko."""
768
def __init__(self, channel):
769
self.channel = channel
771
def get_sock_or_pipes(self):
772
return ('socket', self.channel)
775
return self.channel.close()