~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/ssh.py

  • Committer: John Arbash Meinel
  • Date: 2011-05-11 11:35:28 UTC
  • mto: This revision was merged to the branch mainline in revision 5851.
  • Revision ID: john@arbash-meinel.com-20110511113528-qepibuwxicjrbb2h
Break compatibility with python <2.6.

This includes auditing the code for places where we were doing
explicit 'sys.version' checks and removing them as appropriate.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006-2010 Robey Pointer <robey@lag.net>
 
1
# Copyright (C) 2006-2011 Robey Pointer <robey@lag.net>
2
2
# Copyright (C) 2005, 2006, 2007 Canonical Ltd
3
3
#
4
4
# This program is free software; you can redistribute it and/or modify
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()
129
132
        # As plink user prompts are not handled currently, don't auto-detect
130
133
        # it by inspection below, but keep this vendor detection for if a path
131
134
        # is given in BZR_SSH. See https://bugs.launchpad.net/bugs/414743
132
135
        elif 'plink' in version and progname == 'plink':
133
136
            # Checking if "plink" was the executed argument as Windows
134
 
            # sometimes reports 'ssh -V' incorrectly with 'plink' in it's
 
137
            # sometimes reports 'ssh -V' incorrectly with 'plink' in its
135
138
            # version.  See https://bugs.launchpad.net/bzr/+bug/107155
136
139
            trace.mutter("ssh implementation is Putty's plink.")
137
140
            vendor = PLinkSubprocessVendor()
336
339
            self._raise_connection_error(host, port=port, orig_error=e,
337
340
                                         msg='Unable to invoke remote bzr')
338
341
 
 
342
_ssh_connection_errors = (EOFError, OSError, IOError, socket.error)
339
343
if paramiko is not None:
340
344
    vendor = ParamikoVendor()
341
345
    register_ssh_vendor('paramiko', vendor)
342
346
    register_ssh_vendor('none', vendor)
343
347
    register_default_ssh_vendor(vendor)
344
 
    _sftp_connection_errors = (EOFError, paramiko.SSHException)
 
348
    _ssh_connection_errors += (paramiko.SSHException,)
345
349
    del vendor
346
 
else:
347
 
    _sftp_connection_errors = (EOFError,)
348
350
 
349
351
 
350
352
class SubprocessVendor(SSHVendor):
357
359
        # whatever) chunks.
358
360
        try:
359
361
            my_sock, subproc_sock = socket.socketpair()
 
362
            osutils.set_fd_cloexec(my_sock)
360
363
        except (AttributeError, socket.error):
361
364
            # This platform doesn't support socketpair(), so just use ordinary
362
365
            # pipes instead.
363
366
            stdin = stdout = subprocess.PIPE
364
 
            sock = None
 
367
            my_sock, subproc_sock = None, None
365
368
        else:
366
369
            stdin = stdout = subproc_sock
367
 
            sock = my_sock
368
370
        proc = subprocess.Popen(argv, stdin=stdin, stdout=stdout,
369
371
                                **os_specific_subprocess_params())
370
 
        return SSHSubprocessConnection(proc, sock=sock)
 
372
        if subproc_sock is not None:
 
373
            subproc_sock.close()
 
374
        return SSHSubprocessConnection(proc, sock=my_sock)
371
375
 
372
376
    def connect_sftp(self, username, password, host, port):
373
377
        try:
375
379
                                                  subsystem='sftp')
376
380
            sock = self._connect(argv)
377
381
            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,):
385
 
                raise
 
382
        except _ssh_connection_errors, e:
386
383
            self._raise_connection_error(host, port=port, orig_error=e)
387
384
 
388
385
    def connect_ssh(self, username, password, host, port, command):
390
387
            argv = self._get_vendor_specific_argv(username, host, port,
391
388
                                                  command=command)
392
389
            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,):
400
 
                raise
 
390
        except _ssh_connection_errors, e:
401
391
            self._raise_connection_error(host, port=port, orig_error=e)
402
392
 
403
393
    def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
418
408
                                  command=None):
419
409
        args = [self.executable_path,
420
410
                '-oForwardX11=no', '-oForwardAgent=no',
421
 
                '-oClearAllForwardings=yes', '-oProtocol=2',
 
411
                '-oClearAllForwardings=yes',
422
412
                '-oNoHostAuthenticationForLocalhost=yes']
423
413
        if port is not None:
424
414
            args.extend(['-p', str(port)])
454
444
register_ssh_vendor('sshcorp', SSHCorpSubprocessVendor())
455
445
 
456
446
 
 
447
class LSHSubprocessVendor(SubprocessVendor):
 
448
    """SSH vendor that uses the 'lsh' executable from GNU"""
 
449
 
 
450
    executable_path = 'lsh'
 
451
 
 
452
    def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
 
453
                                  command=None):
 
454
        args = [self.executable_path]
 
455
        if port is not None:
 
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])
 
461
        else:
 
462
            args.extend([host] + command)
 
463
        return args
 
464
 
 
465
register_ssh_vendor('lsh', LSHSubprocessVendor())
 
466
 
 
467
 
457
468
class PLinkSubprocessVendor(SubprocessVendor):
458
469
    """SSH vendor that uses the 'plink' executable from Putty."""
459
470
 
644
655
import weakref
645
656
_subproc_weakrefs = set()
646
657
 
647
 
def _close_ssh_proc(proc):
648
 
    for func in [proc.stdin.close, proc.stdout.close, proc.wait]:
 
658
def _close_ssh_proc(proc, sock):
 
659
    """Carefully close stdin/stdout and reap the SSH process.
 
660
 
 
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).
 
664
    """
 
665
    funcs = []
 
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)
 
673
    for func in funcs:
649
674
        try:
650
675
            func()
651
676
        except OSError:
652
 
            pass
 
677
            # It's ok for the pipe to already be closed, or the process to
 
678
            # already be finished.
 
679
            continue
653
680
 
654
681
 
655
682
class SSHConnection(object):
690
717
        # to avoid leaving processes lingering indefinitely.
691
718
        def terminate(ref):
692
719
            _subproc_weakrefs.remove(ref)
693
 
            _close_ssh_proc(proc)
 
720
            _close_ssh_proc(proc, sock)
694
721
        _subproc_weakrefs.add(weakref.ref(self, terminate))
695
722
 
696
723
    def send(self, data):
706
733
            return os.read(self.proc.stdout.fileno(), count)
707
734
 
708
735
    def close(self):
709
 
        _close_ssh_proc(self.proc)
 
736
        _close_ssh_proc(self.proc, self._sock)
710
737
 
711
738
    def get_sock_or_pipes(self):
712
739
        if self._sock is not None: