1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>
2
# Copyright (C) 2005, 2006 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Foundation SSH support for SFTP and smart server."""
27
from bzrlib.config import config_dir, ensure_config_dir_exists
28
from bzrlib.errors import (ConnectionError,
30
SocketConnectionError,
35
from bzrlib.osutils import pathjoin
36
from bzrlib.trace import mutter, warning
41
except ImportError, e:
42
# If we have an ssh subprocess, we don't strictly need paramiko for all ssh
46
from paramiko.sftp_client import SFTPClient
53
_paramiko_version = getattr(paramiko, '__version_info__', (0, 0, 0))
55
# Paramiko 1.5 tries to open a socket.AF_UNIX in order to connect
56
# to ssh-agent. That attribute doesn't exist on win32 (it does in cygwin)
57
# so we get an AttributeError exception. So we will not try to
58
# connect to an agent if we are on win32 and using Paramiko older than 1.6
59
_use_ssh_agent = (sys.platform != 'win32' or _paramiko_version >= (1, 6, 0))
63
def register_ssh_vendor(name, vendor):
64
"""Register SSH vendor."""
65
_ssh_vendors[name] = vendor
69
def _get_ssh_vendor():
70
"""Find out what version of SSH is on the system."""
72
if _ssh_vendor is not None:
75
if 'BZR_SSH' in os.environ:
76
vendor_name = os.environ['BZR_SSH']
78
_ssh_vendor = _ssh_vendors[vendor_name]
80
raise UnknownSSH(vendor_name)
84
p = subprocess.Popen(['ssh', '-V'],
85
stdin=subprocess.PIPE,
86
stdout=subprocess.PIPE,
87
stderr=subprocess.PIPE,
88
**os_specific_subprocess_params())
89
returncode = p.returncode
90
stdout, stderr = p.communicate()
94
if 'OpenSSH' in stderr:
95
mutter('ssh implementation is OpenSSH')
96
_ssh_vendor = OpenSSHSubprocessVendor()
97
elif 'SSH Secure Shell' in stderr:
98
mutter('ssh implementation is SSH Corp.')
99
_ssh_vendor = SSHCorpSubprocessVendor()
101
if _ssh_vendor is not None:
104
# XXX: 20051123 jamesh
105
# A check for putty's plink or lsh would go here.
107
mutter('falling back to paramiko implementation')
108
_ssh_vendor = ParamikoVendor()
112
def _ignore_sigint():
113
# TODO: This should possibly ignore SIGHUP as well, but bzr currently
114
# doesn't handle it itself.
115
# <https://launchpad.net/products/bzr/+bug/41433/+index>
117
signal.signal(signal.SIGINT, signal.SIG_IGN)
121
class LoopbackSFTP(object):
122
"""Simple wrapper for a socket that pretends to be a paramiko Channel."""
124
def __init__(self, sock):
127
def send(self, data):
128
return self.__socket.send(data)
131
return self.__socket.recv(n)
133
def recv_ready(self):
137
self.__socket.close()
140
class SSHVendor(object):
141
"""Abstract base class for SSH vendor implementations."""
143
def connect_sftp(self, username, password, host, port):
144
"""Make an SSH connection, and return an SFTPClient.
146
:param username: an ascii string
147
:param password: an ascii string
148
:param host: a host name as an ascii string
149
:param port: a port number
152
:raises: ConnectionError if it cannot connect.
154
:rtype: paramiko.sftp_client.SFTPClient
156
raise NotImplementedError(self.connect_sftp)
158
def connect_ssh(self, username, password, host, port, command):
159
"""Make an SSH connection.
161
:returns: something with a `close` method, and a `get_filelike_channels`
162
method that returns a pair of (read, write) filelike objects.
164
raise NotImplementedError(self.connect_ssh)
166
def _raise_connection_error(self, host, port=None, orig_error=None,
167
msg='Unable to connect to SSH host'):
168
"""Raise a SocketConnectionError with properly formatted host.
170
This just unifies all the locations that try to raise ConnectionError,
171
so that they format things properly.
173
raise SocketConnectionError(host=host, port=port, msg=msg,
174
orig_error=orig_error)
177
class LoopbackVendor(SSHVendor):
178
"""SSH "vendor" that connects over a plain TCP socket, not SSH."""
180
def connect_sftp(self, username, password, host, port):
181
sock = socket.socket()
183
sock.connect((host, port))
184
except socket.error, e:
185
self._raise_connection_error(host, port=port, orig_error=e)
186
return SFTPClient(LoopbackSFTP(sock))
188
register_ssh_vendor('loopback', LoopbackVendor())
191
class _ParamikoSSHConnection(object):
192
def __init__(self, channel):
193
self.channel = channel
195
def get_filelike_channels(self):
196
return self.channel.makefile('rb'), self.channel.makefile('wb')
199
return self.channel.close()
202
class ParamikoVendor(SSHVendor):
203
"""Vendor that uses paramiko."""
205
def _connect(self, username, password, host, port):
206
global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
211
t = paramiko.Transport((host, port or 22))
212
t.set_log_channel('bzr.paramiko')
214
except (paramiko.SSHException, socket.error), e:
215
self._raise_connection_error(host, port=port, orig_error=e)
217
server_key = t.get_remote_server_key()
218
server_key_hex = paramiko.util.hexify(server_key.get_fingerprint())
219
keytype = server_key.get_name()
220
if host in SYSTEM_HOSTKEYS and keytype in SYSTEM_HOSTKEYS[host]:
221
our_server_key = SYSTEM_HOSTKEYS[host][keytype]
222
our_server_key_hex = paramiko.util.hexify(our_server_key.get_fingerprint())
223
elif host in BZR_HOSTKEYS and keytype in BZR_HOSTKEYS[host]:
224
our_server_key = BZR_HOSTKEYS[host][keytype]
225
our_server_key_hex = paramiko.util.hexify(our_server_key.get_fingerprint())
227
warning('Adding %s host key for %s: %s' % (keytype, host, server_key_hex))
228
add = getattr(BZR_HOSTKEYS, 'add', None)
229
if add is not None: # paramiko >= 1.X.X
230
BZR_HOSTKEYS.add(host, keytype, server_key)
232
BZR_HOSTKEYS.setdefault(host, {})[keytype] = server_key
233
our_server_key = server_key
234
our_server_key_hex = paramiko.util.hexify(our_server_key.get_fingerprint())
236
if server_key != our_server_key:
237
filename1 = os.path.expanduser('~/.ssh/known_hosts')
238
filename2 = pathjoin(config_dir(), 'ssh_host_keys')
239
raise TransportError('Host keys for %s do not match! %s != %s' % \
240
(host, our_server_key_hex, server_key_hex),
241
['Try editing %s or %s' % (filename1, filename2)])
243
_paramiko_auth(username, password, host, t)
246
def connect_sftp(self, username, password, host, port):
247
t = self._connect(username, password, host, port)
249
return t.open_sftp_client()
250
except paramiko.SSHException, e:
251
self._raise_connection_error(host, port=port, orig_error=e,
252
msg='Unable to start sftp client')
254
def connect_ssh(self, username, password, host, port, command):
255
t = self._connect(username, password, host, port)
257
channel = t.open_session()
258
cmdline = ' '.join(command)
259
channel.exec_command(cmdline)
260
return _ParamikoSSHConnection(channel)
261
except paramiko.SSHException, e:
262
self._raise_connection_error(host, port=port, orig_error=e,
263
msg='Unable to invoke remote bzr')
265
if paramiko is not None:
266
register_ssh_vendor('paramiko', ParamikoVendor())
269
class SubprocessVendor(SSHVendor):
270
"""Abstract base class for vendors that use pipes to a subprocess."""
272
def _connect(self, argv):
273
proc = subprocess.Popen(argv,
274
stdin=subprocess.PIPE,
275
stdout=subprocess.PIPE,
276
**os_specific_subprocess_params())
277
return SSHSubprocess(proc)
279
def connect_sftp(self, username, password, host, port):
281
argv = self._get_vendor_specific_argv(username, host, port,
283
sock = self._connect(argv)
284
return SFTPClient(sock)
285
except (EOFError, paramiko.SSHException), e:
286
self._raise_connection_error(host, port=port, orig_error=e)
287
except (OSError, IOError), e:
288
# If the machine is fast enough, ssh can actually exit
289
# before we try and send it the sftp request, which
290
# raises a Broken Pipe
291
if e.errno not in (errno.EPIPE,):
293
self._raise_connection_error(host, port=port, orig_error=e)
295
def connect_ssh(self, username, password, host, port, command):
297
argv = self._get_vendor_specific_argv(username, host, port,
299
return self._connect(argv)
300
except (EOFError), e:
301
self._raise_connection_error(host, port=port, orig_error=e)
302
except (OSError, IOError), e:
303
# If the machine is fast enough, ssh can actually exit
304
# before we try and send it the sftp request, which
305
# raises a Broken Pipe
306
if e.errno not in (errno.EPIPE,):
308
self._raise_connection_error(host, port=port, orig_error=e)
310
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
312
"""Returns the argument list to run the subprocess with.
314
Exactly one of 'subsystem' and 'command' must be specified.
316
raise NotImplementedError(self._get_vendor_specific_argv)
318
register_ssh_vendor('none', ParamikoVendor())
321
class OpenSSHSubprocessVendor(SubprocessVendor):
322
"""SSH vendor that uses the 'ssh' executable from OpenSSH."""
324
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
326
assert subsystem is not None or command is not None, (
327
'Must specify a command or subsystem')
328
if subsystem is not None:
329
assert command is None, (
330
'subsystem and command are mutually exclusive')
332
'-oForwardX11=no', '-oForwardAgent=no',
333
'-oClearAllForwardings=yes', '-oProtocol=2',
334
'-oNoHostAuthenticationForLocalhost=yes']
336
args.extend(['-p', str(port)])
337
if username is not None:
338
args.extend(['-l', username])
339
if subsystem is not None:
340
args.extend(['-s', host, subsystem])
342
args.extend([host] + command)
345
register_ssh_vendor('openssh', OpenSSHSubprocessVendor())
348
class SSHCorpSubprocessVendor(SubprocessVendor):
349
"""SSH vendor that uses the 'ssh' executable from SSH Corporation."""
351
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
353
assert subsystem is not None or command is not None, (
354
'Must specify a command or subsystem')
355
if subsystem is not None:
356
assert command is None, (
357
'subsystem and command are mutually exclusive')
360
args.extend(['-p', str(port)])
361
if username is not None:
362
args.extend(['-l', username])
363
if subsystem is not None:
364
args.extend(['-s', subsystem, host])
366
args.extend([host] + command)
369
register_ssh_vendor('ssh', SSHCorpSubprocessVendor())
372
def _paramiko_auth(username, password, host, paramiko_transport):
373
# paramiko requires a username, but it might be none if nothing was supplied
374
# use the local username, just in case.
375
# We don't override username, because if we aren't using paramiko,
376
# the username might be specified in ~/.ssh/config and we don't want to
377
# force it to something else
378
# Also, it would mess up the self.relpath() functionality
379
username = username or getpass.getuser()
382
agent = paramiko.Agent()
383
for key in agent.get_keys():
384
mutter('Trying SSH agent key %s' % paramiko.util.hexify(key.get_fingerprint()))
386
paramiko_transport.auth_publickey(username, key)
388
except paramiko.SSHException, e:
391
# okay, try finding id_rsa or id_dss? (posix only)
392
if _try_pkey_auth(paramiko_transport, paramiko.RSAKey, username, 'id_rsa'):
394
if _try_pkey_auth(paramiko_transport, paramiko.DSSKey, username, 'id_dsa'):
399
paramiko_transport.auth_password(username, password)
401
except paramiko.SSHException, e:
404
# give up and ask for a password
405
password = bzrlib.ui.ui_factory.get_password(
406
prompt='SSH %(user)s@%(host)s password',
407
user=username, host=host)
409
paramiko_transport.auth_password(username, password)
410
except paramiko.SSHException, e:
411
raise ConnectionError('Unable to authenticate to SSH host as %s@%s' %
415
def _try_pkey_auth(paramiko_transport, pkey_class, username, filename):
416
filename = os.path.expanduser('~/.ssh/' + filename)
418
key = pkey_class.from_private_key_file(filename)
419
paramiko_transport.auth_publickey(username, key)
421
except paramiko.PasswordRequiredException:
422
password = bzrlib.ui.ui_factory.get_password(
423
prompt='SSH %(filename)s password',
426
key = pkey_class.from_private_key_file(filename, password)
427
paramiko_transport.auth_publickey(username, key)
429
except paramiko.SSHException:
430
mutter('SSH authentication via %s key failed.' % (os.path.basename(filename),))
431
except paramiko.SSHException:
432
mutter('SSH authentication via %s key failed.' % (os.path.basename(filename),))
438
def load_host_keys():
440
Load system host keys (probably doesn't work on windows) and any
441
"discovered" keys from previous sessions.
443
global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
445
SYSTEM_HOSTKEYS = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
447
mutter('failed to load system host keys: ' + str(e))
448
bzr_hostkey_path = pathjoin(config_dir(), 'ssh_host_keys')
450
BZR_HOSTKEYS = paramiko.util.load_host_keys(bzr_hostkey_path)
452
mutter('failed to load bzr host keys: ' + str(e))
456
def save_host_keys():
458
Save "discovered" host keys in $(config)/ssh_host_keys/.
460
global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
461
bzr_hostkey_path = pathjoin(config_dir(), 'ssh_host_keys')
462
ensure_config_dir_exists()
465
f = open(bzr_hostkey_path, 'w')
466
f.write('# SSH host keys collected by bzr\n')
467
for hostname, keys in BZR_HOSTKEYS.iteritems():
468
for keytype, key in keys.iteritems():
469
f.write('%s %s %s\n' % (hostname, keytype, key.get_base64()))
472
mutter('failed to save bzr host keys: ' + str(e))
475
def os_specific_subprocess_params():
476
"""Get O/S specific subprocess parameters."""
477
if sys.platform == 'win32':
478
# setting the process group and closing fds is not supported on
482
# We close fds other than the pipes as the child process does not need
485
# We also set the child process to ignore SIGINT. Normally the signal
486
# would be sent to every process in the foreground process group, but
487
# this causes it to be seen only by bzr and not by ssh. Python will
488
# generate a KeyboardInterrupt in bzr, and we will then have a chance
489
# to release locks or do other cleanup over ssh before the connection
491
# <https://launchpad.net/products/bzr/+bug/5987>
493
# Running it in a separate process group is not good because then it
494
# can't get non-echoed input of a password or passphrase.
495
# <https://launchpad.net/products/bzr/+bug/40508>
496
return {'preexec_fn': _ignore_sigint,
501
class SSHSubprocess(object):
502
"""A socket-like object that talks to an ssh subprocess via pipes."""
504
def __init__(self, proc):
507
def send(self, data):
508
return os.write(self.proc.stdin.fileno(), data)
510
def recv_ready(self):
511
# TODO: jam 20051215 this function is necessary to support the
512
# pipelined() function. In reality, it probably should use
513
# poll() or select() to actually return if there is data
514
# available, otherwise we probably don't get any benefit
517
def recv(self, count):
518
return os.read(self.proc.stdout.fileno(), count)
521
self.proc.stdin.close()
522
self.proc.stdout.close()
525
def get_filelike_channels(self):
526
return (self.proc.stdout, self.proc.stdin)