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,
34
from bzrlib.osutils import pathjoin
35
from bzrlib.trace import mutter, warning
40
except ImportError, e:
41
raise ParamikoNotPresent(e)
43
from paramiko.sftp_client import SFTPClient
50
_paramiko_version = getattr(paramiko, '__version_info__', (0, 0, 0))
52
# Paramiko 1.5 tries to open a socket.AF_UNIX in order to connect
53
# to ssh-agent. That attribute doesn't exist on win32 (it does in cygwin)
54
# so we get an AttributeError exception. So we will not try to
55
# connect to an agent if we are on win32 and using Paramiko older than 1.6
56
_use_ssh_agent = (sys.platform != 'win32' or _paramiko_version >= (1, 6, 0))
60
def register_ssh_vendor(name, vendor):
61
"""Register SSH vendor."""
62
_ssh_vendors[name] = vendor
66
def _get_ssh_vendor():
67
"""Find out what version of SSH is on the system."""
69
if _ssh_vendor is not None:
72
if 'BZR_SSH' in os.environ:
73
vendor_name = os.environ['BZR_SSH']
75
_ssh_vendor = _ssh_vendors[vendor_name]
77
raise UnknownSSH(vendor_name)
81
p = subprocess.Popen(['ssh', '-V'],
82
stdin=subprocess.PIPE,
83
stdout=subprocess.PIPE,
84
stderr=subprocess.PIPE,
85
**os_specific_subprocess_params())
86
returncode = p.returncode
87
stdout, stderr = p.communicate()
91
if 'OpenSSH' in stderr:
92
mutter('ssh implementation is OpenSSH')
93
_ssh_vendor = OpenSSHSubprocessVendor()
94
elif 'SSH Secure Shell' in stderr:
95
mutter('ssh implementation is SSH Corp.')
96
_ssh_vendor = SSHCorpSubprocessVendor()
98
if _ssh_vendor is not None:
101
# XXX: 20051123 jamesh
102
# A check for putty's plink or lsh would go here.
104
mutter('falling back to paramiko implementation')
105
_ssh_vendor = ParamikoVendor()
110
def _ignore_sigint():
111
# TODO: This should possibly ignore SIGHUP as well, but bzr currently
112
# doesn't handle it itself.
113
# <https://launchpad.net/products/bzr/+bug/41433/+index>
115
signal.signal(signal.SIGINT, signal.SIG_IGN)
119
class LoopbackSFTP(object):
120
"""Simple wrapper for a socket that pretends to be a paramiko Channel."""
122
def __init__(self, sock):
125
def send(self, data):
126
return self.__socket.send(data)
129
return self.__socket.recv(n)
131
def recv_ready(self):
135
self.__socket.close()
138
class SSHVendor(object):
139
"""Abstract base class for SSH vendor implementations."""
141
def connect_sftp(self, username, password, host, port):
142
"""Make an SSH connection, and return an SFTPClient.
144
:param username: an ascii string
145
:param password: an ascii string
146
:param host: a host name as an ascii string
147
:param port: a port number
150
:raises: ConnectionError if it cannot connect.
152
:rtype: paramiko.sftp_client.SFTPClient
154
raise NotImplementedError(self.connect_sftp)
156
def connect_ssh(self, username, password, host, port, command):
157
"""Make an SSH connection, and return a pipe-like object.
159
(This is currently unused, it's just here to indicate future directions
162
raise NotImplementedError(self.connect_ssh)
165
class LoopbackVendor(SSHVendor):
166
"""SSH "vendor" that connects over a plain TCP socket, not SSH."""
168
def connect_sftp(self, username, password, host, port):
169
sock = socket.socket()
171
sock.connect((host, port))
172
except socket.error, e:
173
raise ConnectionError('Unable to connect to SSH host %s:%s: %s'
175
return SFTPClient(LoopbackSFTP(sock))
177
register_ssh_vendor('loopback', LoopbackVendor())
180
class ParamikoVendor(SSHVendor):
181
"""Vendor that uses paramiko."""
183
def connect_sftp(self, username, password, host, port):
184
global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
189
t = paramiko.Transport((host, port or 22))
190
t.set_log_channel('bzr.paramiko')
192
except (paramiko.SSHException, socket.error), e:
193
raise ConnectionError('Unable to reach SSH host %s:%s: %s'
196
server_key = t.get_remote_server_key()
197
server_key_hex = paramiko.util.hexify(server_key.get_fingerprint())
198
keytype = server_key.get_name()
199
if host in SYSTEM_HOSTKEYS and keytype in SYSTEM_HOSTKEYS[host]:
200
our_server_key = SYSTEM_HOSTKEYS[host][keytype]
201
our_server_key_hex = paramiko.util.hexify(our_server_key.get_fingerprint())
202
elif host in BZR_HOSTKEYS and keytype in BZR_HOSTKEYS[host]:
203
our_server_key = BZR_HOSTKEYS[host][keytype]
204
our_server_key_hex = paramiko.util.hexify(our_server_key.get_fingerprint())
206
warning('Adding %s host key for %s: %s' % (keytype, host, server_key_hex))
207
if host not in BZR_HOSTKEYS:
208
BZR_HOSTKEYS[host] = {}
209
BZR_HOSTKEYS[host][keytype] = server_key
210
our_server_key = server_key
211
our_server_key_hex = paramiko.util.hexify(our_server_key.get_fingerprint())
213
if server_key != our_server_key:
214
filename1 = os.path.expanduser('~/.ssh/known_hosts')
215
filename2 = pathjoin(config_dir(), 'ssh_host_keys')
216
raise TransportError('Host keys for %s do not match! %s != %s' % \
217
(host, our_server_key_hex, server_key_hex),
218
['Try editing %s or %s' % (filename1, filename2)])
220
_paramiko_auth(username, password, host, t)
223
sftp = t.open_sftp_client()
224
except paramiko.SSHException, e:
225
raise ConnectionError('Unable to start sftp client %s:%d' %
229
register_ssh_vendor('paramiko', ParamikoVendor())
232
class SubprocessVendor(SSHVendor):
233
"""Abstract base class for vendors that use pipes to a subprocess."""
235
def _connect(self, argv):
236
proc = subprocess.Popen(argv,
237
stdin=subprocess.PIPE,
238
stdout=subprocess.PIPE,
239
**os_specific_subprocess_params())
240
return SSHSubprocess(proc)
242
def connect_sftp(self, username, password, host, port):
244
argv = self._get_vendor_specific_argv(username, host, port,
246
sock = self._connect(argv)
247
return SFTPClient(sock)
248
except (EOFError, paramiko.SSHException), e:
249
raise ConnectionError('Unable to connect to SSH host %s:%s: %s'
251
except (OSError, IOError), e:
252
# If the machine is fast enough, ssh can actually exit
253
# before we try and send it the sftp request, which
254
# raises a Broken Pipe
255
if e.errno not in (errno.EPIPE,):
257
raise ConnectionError('Unable to connect to SSH host %s:%s: %s'
260
def connect_ssh(self, username, password, host, port, command):
262
argv = self._get_vendor_specific_argv(username, host, port,
264
return self._connect(argv)
265
except (EOFError), e:
266
raise ConnectionError('Unable to connect to SSH host %s:%s: %s'
268
except (OSError, IOError), e:
269
# If the machine is fast enough, ssh can actually exit
270
# before we try and send it the sftp request, which
271
# raises a Broken Pipe
272
if e.errno not in (errno.EPIPE,):
274
raise ConnectionError('Unable to connect to SSH host %s:%s: %s'
277
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
279
"""Returns the argument list to run the subprocess with.
281
Exactly one of 'subsystem' and 'command' must be specified.
283
raise NotImplementedError(self._get_vendor_specific_argv)
285
register_ssh_vendor('none', ParamikoVendor())
288
class OpenSSHSubprocessVendor(SubprocessVendor):
289
"""SSH vendor that uses the 'ssh' executable from OpenSSH."""
291
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
293
assert subsystem is not None or command is not None, (
294
'Must specify a command or subsystem')
295
if subsystem is not None:
296
assert command is None, (
297
'subsystem and command are mutually exclusive')
299
'-oForwardX11=no', '-oForwardAgent=no',
300
'-oClearAllForwardings=yes', '-oProtocol=2',
301
'-oNoHostAuthenticationForLocalhost=yes']
303
args.extend(['-p', str(port)])
304
if username is not None:
305
args.extend(['-l', username])
306
if subsystem is not None:
307
args.extend(['-s', host, subsystem])
309
args.extend([host] + command)
312
register_ssh_vendor('openssh', OpenSSHSubprocessVendor())
315
class SSHCorpSubprocessVendor(SubprocessVendor):
316
"""SSH vendor that uses the 'ssh' executable from SSH Corporation."""
318
def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
320
assert subsystem is not None or command is not None, (
321
'Must specify a command or subsystem')
322
if subsystem is not None:
323
assert command is None, (
324
'subsystem and command are mutually exclusive')
327
args.extend(['-p', str(port)])
328
if username is not None:
329
args.extend(['-l', username])
330
if subsystem is not None:
331
args.extend(['-s', subsystem, host])
333
args.extend([host] + command)
336
register_ssh_vendor('ssh', SSHCorpSubprocessVendor())
339
def _paramiko_auth(username, password, host, paramiko_transport):
340
# paramiko requires a username, but it might be none if nothing was supplied
341
# use the local username, just in case.
342
# We don't override username, because if we aren't using paramiko,
343
# the username might be specified in ~/.ssh/config and we don't want to
344
# force it to something else
345
# Also, it would mess up the self.relpath() functionality
346
username = username or getpass.getuser()
349
agent = paramiko.Agent()
350
for key in agent.get_keys():
351
mutter('Trying SSH agent key %s' % paramiko.util.hexify(key.get_fingerprint()))
353
paramiko_transport.auth_publickey(username, key)
355
except paramiko.SSHException, e:
358
# okay, try finding id_rsa or id_dss? (posix only)
359
if _try_pkey_auth(paramiko_transport, paramiko.RSAKey, username, 'id_rsa'):
361
if _try_pkey_auth(paramiko_transport, paramiko.DSSKey, username, 'id_dsa'):
366
paramiko_transport.auth_password(username, password)
368
except paramiko.SSHException, e:
371
# give up and ask for a password
372
password = bzrlib.ui.ui_factory.get_password(
373
prompt='SSH %(user)s@%(host)s password',
374
user=username, host=host)
376
paramiko_transport.auth_password(username, password)
377
except paramiko.SSHException, e:
378
raise ConnectionError('Unable to authenticate to SSH host as %s@%s' %
382
def _try_pkey_auth(paramiko_transport, pkey_class, username, filename):
383
filename = os.path.expanduser('~/.ssh/' + filename)
385
key = pkey_class.from_private_key_file(filename)
386
paramiko_transport.auth_publickey(username, key)
388
except paramiko.PasswordRequiredException:
389
password = bzrlib.ui.ui_factory.get_password(
390
prompt='SSH %(filename)s password',
393
key = pkey_class.from_private_key_file(filename, password)
394
paramiko_transport.auth_publickey(username, key)
396
except paramiko.SSHException:
397
mutter('SSH authentication via %s key failed.' % (os.path.basename(filename),))
398
except paramiko.SSHException:
399
mutter('SSH authentication via %s key failed.' % (os.path.basename(filename),))
405
def load_host_keys():
407
Load system host keys (probably doesn't work on windows) and any
408
"discovered" keys from previous sessions.
410
global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
412
SYSTEM_HOSTKEYS = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
414
mutter('failed to load system host keys: ' + str(e))
415
bzr_hostkey_path = pathjoin(config_dir(), 'ssh_host_keys')
417
BZR_HOSTKEYS = paramiko.util.load_host_keys(bzr_hostkey_path)
419
mutter('failed to load bzr host keys: ' + str(e))
423
def save_host_keys():
425
Save "discovered" host keys in $(config)/ssh_host_keys/.
427
global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
428
bzr_hostkey_path = pathjoin(config_dir(), 'ssh_host_keys')
429
ensure_config_dir_exists()
432
f = open(bzr_hostkey_path, 'w')
433
f.write('# SSH host keys collected by bzr\n')
434
for hostname, keys in BZR_HOSTKEYS.iteritems():
435
for keytype, key in keys.iteritems():
436
f.write('%s %s %s\n' % (hostname, keytype, key.get_base64()))
439
mutter('failed to save bzr host keys: ' + str(e))
442
def os_specific_subprocess_params():
443
"""Get O/S specific subprocess parameters."""
444
if sys.platform == 'win32':
445
# setting the process group and closing fds is not supported on
449
# We close fds other than the pipes as the child process does not need
452
# We also set the child process to ignore SIGINT. Normally the signal
453
# would be sent to every process in the foreground process group, but
454
# this causes it to be seen only by bzr and not by ssh. Python will
455
# generate a KeyboardInterrupt in bzr, and we will then have a chance
456
# to release locks or do other cleanup over ssh before the connection
458
# <https://launchpad.net/products/bzr/+bug/5987>
460
# Running it in a separate process group is not good because then it
461
# can't get non-echoed input of a password or passphrase.
462
# <https://launchpad.net/products/bzr/+bug/40508>
463
return {'preexec_fn': _ignore_sigint,
468
class SSHSubprocess(object):
469
"""A socket-like object that talks to an ssh subprocess via pipes."""
471
def __init__(self, proc):
474
def send(self, data):
475
return os.write(self.proc.stdin.fileno(), data)
477
def recv_ready(self):
478
# TODO: jam 20051215 this function is necessary to support the
479
# pipelined() function. In reality, it probably should use
480
# poll() or select() to actually return if there is data
481
# available, otherwise we probably don't get any benefit
484
def recv(self, count):
485
return os.read(self.proc.stdout.fileno(), count)
488
self.proc.stdin.close()
489
self.proc.stdout.close()
492
def get_filelike_channels(self):
493
return (self.proc.stdout, self.proc.stdin)