~lifeless/ubuntu/lucid/bzr/2.1.2-sru

« back to all changes in this revision

Viewing changes to bzrlib/transport/ssh.py

  • Committer: Bazaar Package Importer
  • Author(s): Jelmer Vernooij
  • Date: 2010-02-17 17:47:40 UTC
  • mfrom: (1.4.5 upstream) (9.2.2 experimental)
  • Revision ID: james.westby@ubuntu.com-20100217174740-35yh6t5rjnztg9z6
Tags: 2.1.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
94
94
            try:
95
95
                vendor = self._ssh_vendors[vendor_name]
96
96
            except KeyError:
97
 
                raise errors.UnknownSSH(vendor_name)
 
97
                vendor = self._get_vendor_from_path(vendor_name)
 
98
                if vendor is None:
 
99
                    raise errors.UnknownSSH(vendor_name)
 
100
                vendor.executable_path = vendor_name
98
101
            return vendor
99
102
        return None
100
103
 
110
113
            stdout = stderr = ''
111
114
        return stdout + stderr
112
115
 
113
 
    def _get_vendor_by_version_string(self, version, args):
 
116
    def _get_vendor_by_version_string(self, version, progname):
114
117
        """Return the vendor or None based on output from the subprocess.
115
118
 
116
119
        :param version: The output of 'ssh -V' like command.
123
126
        elif 'SSH Secure Shell' in version:
124
127
            trace.mutter('ssh implementation is SSH Corp.')
125
128
            vendor = SSHCorpSubprocessVendor()
126
 
        # Auto-detect of plink vendor disabled, on Windows recommended
127
 
        # default ssh-client is paramiko
128
 
        # see https://bugs.launchpad.net/bugs/414743
129
 
        #~elif 'plink' in version and args[0] == 'plink':
130
 
        #~    # Checking if "plink" was the executed argument as Windows
131
 
        #~    # sometimes reports 'ssh -V' incorrectly with 'plink' in it's
132
 
        #~    # version.  See https://bugs.launchpad.net/bzr/+bug/107155
133
 
        #~    trace.mutter("ssh implementation is Putty's plink.")
134
 
        #~    vendor = PLinkSubprocessVendor()
 
129
        # As plink user prompts are not handled currently, don't auto-detect
 
130
        # it by inspection below, but keep this vendor detection for if a path
 
131
        # is given in BZR_SSH. See https://bugs.launchpad.net/bugs/414743
 
132
        elif 'plink' in version and progname == 'plink':
 
133
            # Checking if "plink" was the executed argument as Windows
 
134
            # sometimes reports 'ssh -V' incorrectly with 'plink' in it's
 
135
            # version.  See https://bugs.launchpad.net/bzr/+bug/107155
 
136
            trace.mutter("ssh implementation is Putty's plink.")
 
137
            vendor = PLinkSubprocessVendor()
135
138
        return vendor
136
139
 
137
140
    def _get_vendor_by_inspection(self):
138
141
        """Return the vendor or None by checking for known SSH implementations."""
139
 
        for args in (['ssh', '-V'], ['plink', '-V']):
140
 
            version = self._get_ssh_version_string(args)
141
 
            vendor = self._get_vendor_by_version_string(version, args)
142
 
            if vendor is not None:
143
 
                return vendor
144
 
        return None
 
142
        version = self._get_ssh_version_string(['ssh', '-V'])
 
143
        return self._get_vendor_by_version_string(version, "ssh")
 
144
 
 
145
    def _get_vendor_from_path(self, path):
 
146
        """Return the vendor or None using the program at the given path"""
 
147
        version = self._get_ssh_version_string([path, '-V'])
 
148
        return self._get_vendor_by_version_string(version, 
 
149
            os.path.splitext(os.path.basename(path))[0])
145
150
 
146
151
    def get_vendor(self, environment=None):
147
152
        """Find out what version of SSH is on the system.
404
409
class OpenSSHSubprocessVendor(SubprocessVendor):
405
410
    """SSH vendor that uses the 'ssh' executable from OpenSSH."""
406
411
 
 
412
    executable_path = 'ssh'
 
413
 
407
414
    def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
408
415
                                  command=None):
409
 
        args = ['ssh',
 
416
        args = [self.executable_path,
410
417
                '-oForwardX11=no', '-oForwardAgent=no',
411
418
                '-oClearAllForwardings=yes', '-oProtocol=2',
412
419
                '-oNoHostAuthenticationForLocalhost=yes']
426
433
class SSHCorpSubprocessVendor(SubprocessVendor):
427
434
    """SSH vendor that uses the 'ssh' executable from SSH Corporation."""
428
435
 
 
436
    executable_path = 'ssh'
 
437
 
429
438
    def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
430
439
                                  command=None):
431
 
        args = ['ssh', '-x']
 
440
        args = [self.executable_path, '-x']
432
441
        if port is not None:
433
442
            args.extend(['-p', str(port)])
434
443
        if username is not None:
439
448
            args.extend([host] + command)
440
449
        return args
441
450
 
442
 
register_ssh_vendor('ssh', SSHCorpSubprocessVendor())
 
451
register_ssh_vendor('sshcorp', SSHCorpSubprocessVendor())
443
452
 
444
453
 
445
454
class PLinkSubprocessVendor(SubprocessVendor):
446
455
    """SSH vendor that uses the 'plink' executable from Putty."""
447
456
 
 
457
    executable_path = 'plink'
 
458
 
448
459
    def _get_vendor_specific_argv(self, username, host, port, subsystem=None,
449
460
                                  command=None):
450
 
        args = ['plink', '-x', '-a', '-ssh', '-2', '-batch']
 
461
        args = [self.executable_path, '-x', '-a', '-ssh', '-2', '-batch']
451
462
        if port is not None:
452
463
            args.extend(['-P', str(port)])
453
464
        if username is not None:
627
638
                'close_fds': True,
628
639
                }
629
640
 
 
641
import weakref
 
642
_subproc_weakrefs = set()
 
643
 
 
644
def _close_ssh_proc(proc):
 
645
    for func in [proc.stdin.close, proc.stdout.close, proc.wait]:
 
646
        try:
 
647
            func()
 
648
        except OSError:
 
649
            pass
 
650
 
630
651
 
631
652
class SSHSubprocess(object):
632
653
    """A socket-like object that talks to an ssh subprocess via pipes."""
633
654
 
634
655
    def __init__(self, proc):
635
656
        self.proc = proc
 
657
        # Add a weakref to proc that will attempt to do the same as self.close
 
658
        # to avoid leaving processes lingering indefinitely.
 
659
        def terminate(ref):
 
660
            _subproc_weakrefs.remove(ref)
 
661
            _close_ssh_proc(proc)
 
662
        _subproc_weakrefs.add(weakref.ref(self, terminate))
636
663
 
637
664
    def send(self, data):
638
665
        return os.write(self.proc.stdin.fileno(), data)
641
668
        return os.read(self.proc.stdout.fileno(), count)
642
669
 
643
670
    def close(self):
644
 
        self.proc.stdin.close()
645
 
        self.proc.stdout.close()
646
 
        self.proc.wait()
 
671
        _close_ssh_proc(self.proc)
647
672
 
648
673
    def get_filelike_channels(self):
649
674
        return (self.proc.stdout, self.proc.stdin)