~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/transport/sftp.py

  • Committer: Martin Pool
  • Date: 2006-03-09 03:28:52 UTC
  • mto: This revision was merged to the branch mainline in revision 1602.
  • Revision ID: mbp@sourcefrog.net-20060309032852-1097eb1947d9bceb
doc

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, Canonical Ltd
 
2
 
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
 
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
 
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
"""Implementation of Transport over SFTP, using paramiko."""
 
18
 
 
19
import errno
 
20
import getpass
 
21
import os
 
22
import random
 
23
import re
 
24
import stat
 
25
import subprocess
 
26
import sys
 
27
import time
 
28
import urllib
 
29
import urlparse
 
30
import weakref
 
31
 
 
32
from bzrlib.config import config_dir, ensure_config_dir_exists
 
33
from bzrlib.errors import (ConnectionError,
 
34
                           FileExists, 
 
35
                           TransportNotPossible, NoSuchFile, PathNotChild,
 
36
                           TransportError,
 
37
                           LockError, ParamikoNotPresent
 
38
                           )
 
39
from bzrlib.osutils import pathjoin, fancy_rename
 
40
from bzrlib.trace import mutter, warning, error
 
41
from bzrlib.transport import Transport, Server, urlescape
 
42
import bzrlib.ui
 
43
 
 
44
try:
 
45
    import paramiko
 
46
except ImportError, e:
 
47
    raise ParamikoNotPresent(e)
 
48
else:
 
49
    from paramiko.sftp import (SFTP_FLAG_WRITE, SFTP_FLAG_CREATE,
 
50
                               SFTP_FLAG_EXCL, SFTP_FLAG_TRUNC,
 
51
                               CMD_HANDLE, CMD_OPEN)
 
52
    from paramiko.sftp_attr import SFTPAttributes
 
53
    from paramiko.sftp_file import SFTPFile
 
54
    from paramiko.sftp_client import SFTPClient
 
55
 
 
56
if 'sftp' not in urlparse.uses_netloc:
 
57
    urlparse.uses_netloc.append('sftp')
 
58
 
 
59
# don't use prefetch unless paramiko version >= 1.5.2 (there were bugs earlier)
 
60
_default_do_prefetch = False
 
61
if getattr(paramiko, '__version_info__', (0, 0, 0)) >= (1, 5, 2):
 
62
    _default_do_prefetch = True
 
63
 
 
64
 
 
65
_close_fds = True
 
66
if sys.platform == 'win32':
 
67
    # close_fds not supported on win32
 
68
    _close_fds = False
 
69
 
 
70
_ssh_vendor = None
 
71
 
 
72
def _get_ssh_vendor():
 
73
    """Find out what version of SSH is on the system."""
 
74
    global _ssh_vendor
 
75
    if _ssh_vendor is not None:
 
76
        return _ssh_vendor
 
77
 
 
78
    _ssh_vendor = 'none'
 
79
 
 
80
    if 'BZR_SSH' in os.environ:
 
81
        _ssh_vendor = os.environ['BZR_SSH']
 
82
        if _ssh_vendor == 'paramiko':
 
83
            _ssh_vendor = 'none'
 
84
        return _ssh_vendor
 
85
 
 
86
    try:
 
87
        p = subprocess.Popen(['ssh', '-V'],
 
88
                             close_fds=_close_fds,
 
89
                             stdin=subprocess.PIPE,
 
90
                             stdout=subprocess.PIPE,
 
91
                             stderr=subprocess.PIPE)
 
92
        returncode = p.returncode
 
93
        stdout, stderr = p.communicate()
 
94
    except OSError:
 
95
        returncode = -1
 
96
        stdout = stderr = ''
 
97
    if 'OpenSSH' in stderr:
 
98
        mutter('ssh implementation is OpenSSH')
 
99
        _ssh_vendor = 'openssh'
 
100
    elif 'SSH Secure Shell' in stderr:
 
101
        mutter('ssh implementation is SSH Corp.')
 
102
        _ssh_vendor = 'ssh'
 
103
 
 
104
    if _ssh_vendor != 'none':
 
105
        return _ssh_vendor
 
106
 
 
107
    # XXX: 20051123 jamesh
 
108
    # A check for putty's plink or lsh would go here.
 
109
 
 
110
    mutter('falling back to paramiko implementation')
 
111
    return _ssh_vendor
 
112
 
 
113
 
 
114
class SFTPSubprocess:
 
115
    """A socket-like object that talks to an ssh subprocess via pipes."""
 
116
    def __init__(self, hostname, vendor, port=None, user=None):
 
117
        assert vendor in ['openssh', 'ssh']
 
118
        if vendor == 'openssh':
 
119
            args = ['ssh',
 
120
                    '-oForwardX11=no', '-oForwardAgent=no',
 
121
                    '-oClearAllForwardings=yes', '-oProtocol=2',
 
122
                    '-oNoHostAuthenticationForLocalhost=yes']
 
123
            if port is not None:
 
124
                args.extend(['-p', str(port)])
 
125
            if user is not None:
 
126
                args.extend(['-l', user])
 
127
            args.extend(['-s', hostname, 'sftp'])
 
128
        elif vendor == 'ssh':
 
129
            args = ['ssh', '-x']
 
130
            if port is not None:
 
131
                args.extend(['-p', str(port)])
 
132
            if user is not None:
 
133
                args.extend(['-l', user])
 
134
            args.extend(['-s', 'sftp', hostname])
 
135
 
 
136
        self.proc = subprocess.Popen(args, close_fds=_close_fds,
 
137
                                     stdin=subprocess.PIPE,
 
138
                                     stdout=subprocess.PIPE)
 
139
 
 
140
    def send(self, data):
 
141
        return os.write(self.proc.stdin.fileno(), data)
 
142
 
 
143
    def recv_ready(self):
 
144
        # TODO: jam 20051215 this function is necessary to support the
 
145
        # pipelined() function. In reality, it probably should use
 
146
        # poll() or select() to actually return if there is data
 
147
        # available, otherwise we probably don't get any benefit
 
148
        return True
 
149
 
 
150
    def recv(self, count):
 
151
        return os.read(self.proc.stdout.fileno(), count)
 
152
 
 
153
    def close(self):
 
154
        self.proc.stdin.close()
 
155
        self.proc.stdout.close()
 
156
        self.proc.wait()
 
157
 
 
158
 
 
159
class LoopbackSFTP(object):
 
160
    """Simple wrapper for a socket that pretends to be a paramiko Channel."""
 
161
 
 
162
    def __init__(self, sock):
 
163
        self.__socket = sock
 
164
 
 
165
    def send(self, data):
 
166
        return self.__socket.send(data)
 
167
 
 
168
    def recv(self, n):
 
169
        return self.__socket.recv(n)
 
170
 
 
171
    def recv_ready(self):
 
172
        return True
 
173
 
 
174
    def close(self):
 
175
        self.__socket.close()
 
176
 
 
177
 
 
178
SYSTEM_HOSTKEYS = {}
 
179
BZR_HOSTKEYS = {}
 
180
 
 
181
# This is a weakref dictionary, so that we can reuse connections
 
182
# that are still active. Long term, it might be nice to have some
 
183
# sort of expiration policy, such as disconnect if inactive for
 
184
# X seconds. But that requires a lot more fanciness.
 
185
_connected_hosts = weakref.WeakValueDictionary()
 
186
 
 
187
 
 
188
def load_host_keys():
 
189
    """
 
190
    Load system host keys (probably doesn't work on windows) and any
 
191
    "discovered" keys from previous sessions.
 
192
    """
 
193
    global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
 
194
    try:
 
195
        SYSTEM_HOSTKEYS = paramiko.util.load_host_keys(os.path.expanduser('~/.ssh/known_hosts'))
 
196
    except Exception, e:
 
197
        mutter('failed to load system host keys: ' + str(e))
 
198
    bzr_hostkey_path = pathjoin(config_dir(), 'ssh_host_keys')
 
199
    try:
 
200
        BZR_HOSTKEYS = paramiko.util.load_host_keys(bzr_hostkey_path)
 
201
    except Exception, e:
 
202
        mutter('failed to load bzr host keys: ' + str(e))
 
203
        save_host_keys()
 
204
 
 
205
 
 
206
def save_host_keys():
 
207
    """
 
208
    Save "discovered" host keys in $(config)/ssh_host_keys/.
 
209
    """
 
210
    global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
 
211
    bzr_hostkey_path = pathjoin(config_dir(), 'ssh_host_keys')
 
212
    ensure_config_dir_exists()
 
213
 
 
214
    try:
 
215
        f = open(bzr_hostkey_path, 'w')
 
216
        f.write('# SSH host keys collected by bzr\n')
 
217
        for hostname, keys in BZR_HOSTKEYS.iteritems():
 
218
            for keytype, key in keys.iteritems():
 
219
                f.write('%s %s %s\n' % (hostname, keytype, key.get_base64()))
 
220
        f.close()
 
221
    except IOError, e:
 
222
        mutter('failed to save bzr host keys: ' + str(e))
 
223
 
 
224
 
 
225
class SFTPLock(object):
 
226
    """This fakes a lock in a remote location."""
 
227
    __slots__ = ['path', 'lock_path', 'lock_file', 'transport']
 
228
    def __init__(self, path, transport):
 
229
        assert isinstance(transport, SFTPTransport)
 
230
 
 
231
        self.lock_file = None
 
232
        self.path = path
 
233
        self.lock_path = path + '.write-lock'
 
234
        self.transport = transport
 
235
        try:
 
236
            # RBC 20060103 FIXME should we be using private methods here ?
 
237
            abspath = transport._remote_path(self.lock_path)
 
238
            self.lock_file = transport._sftp_open_exclusive(abspath)
 
239
        except FileExists:
 
240
            raise LockError('File %r already locked' % (self.path,))
 
241
 
 
242
    def __del__(self):
 
243
        """Should this warn, or actually try to cleanup?"""
 
244
        if self.lock_file:
 
245
            warning("SFTPLock %r not explicitly unlocked" % (self.path,))
 
246
            self.unlock()
 
247
 
 
248
    def unlock(self):
 
249
        if not self.lock_file:
 
250
            return
 
251
        self.lock_file.close()
 
252
        self.lock_file = None
 
253
        try:
 
254
            self.transport.delete(self.lock_path)
 
255
        except (NoSuchFile,):
 
256
            # What specific errors should we catch here?
 
257
            pass
 
258
 
 
259
class SFTPTransport (Transport):
 
260
    """
 
261
    Transport implementation for SFTP access.
 
262
    """
 
263
    _do_prefetch = _default_do_prefetch
 
264
 
 
265
    def __init__(self, base, clone_from=None):
 
266
        assert base.startswith('sftp://')
 
267
        self._parse_url(base)
 
268
        base = self._unparse_url()
 
269
        if base[-1] != '/':
 
270
            base = base + '/'
 
271
        super(SFTPTransport, self).__init__(base)
 
272
        if clone_from is None:
 
273
            self._sftp_connect()
 
274
        else:
 
275
            # use the same ssh connection, etc
 
276
            self._sftp = clone_from._sftp
 
277
        # super saves 'self.base'
 
278
    
 
279
    def should_cache(self):
 
280
        """
 
281
        Return True if the data pulled across should be cached locally.
 
282
        """
 
283
        return True
 
284
 
 
285
    def clone(self, offset=None):
 
286
        """
 
287
        Return a new SFTPTransport with root at self.base + offset.
 
288
        We share the same SFTP session between such transports, because it's
 
289
        fairly expensive to set them up.
 
290
        """
 
291
        if offset is None:
 
292
            return SFTPTransport(self.base, self)
 
293
        else:
 
294
            return SFTPTransport(self.abspath(offset), self)
 
295
 
 
296
    def abspath(self, relpath):
 
297
        """
 
298
        Return the full url to the given relative path.
 
299
        
 
300
        @param relpath: the relative path or path components
 
301
        @type relpath: str or list
 
302
        """
 
303
        return self._unparse_url(self._remote_path(relpath))
 
304
    
 
305
    def _remote_path(self, relpath):
 
306
        """Return the path to be passed along the sftp protocol for relpath.
 
307
        
 
308
        relpath is a urlencoded string.
 
309
        """
 
310
        # FIXME: share the common code across transports
 
311
        assert isinstance(relpath, basestring)
 
312
        relpath = urllib.unquote(relpath).split('/')
 
313
        basepath = self._path.split('/')
 
314
        if len(basepath) > 0 and basepath[-1] == '':
 
315
            basepath = basepath[:-1]
 
316
 
 
317
        for p in relpath:
 
318
            if p == '..':
 
319
                if len(basepath) == 0:
 
320
                    # In most filesystems, a request for the parent
 
321
                    # of root, just returns root.
 
322
                    continue
 
323
                basepath.pop()
 
324
            elif p == '.':
 
325
                continue # No-op
 
326
            else:
 
327
                basepath.append(p)
 
328
 
 
329
        path = '/'.join(basepath)
 
330
        return path
 
331
 
 
332
    def relpath(self, abspath):
 
333
        username, password, host, port, path = self._split_url(abspath)
 
334
        error = []
 
335
        if (username != self._username):
 
336
            error.append('username mismatch')
 
337
        if (host != self._host):
 
338
            error.append('host mismatch')
 
339
        if (port != self._port):
 
340
            error.append('port mismatch')
 
341
        if (not path.startswith(self._path)):
 
342
            error.append('path mismatch')
 
343
        if error:
 
344
            extra = ': ' + ', '.join(error)
 
345
            raise PathNotChild(abspath, self.base, extra=extra)
 
346
        pl = len(self._path)
 
347
        return path[pl:].strip('/')
 
348
 
 
349
    def has(self, relpath):
 
350
        """
 
351
        Does the target location exist?
 
352
        """
 
353
        try:
 
354
            self._sftp.stat(self._remote_path(relpath))
 
355
            return True
 
356
        except IOError:
 
357
            return False
 
358
 
 
359
    def get(self, relpath, decode=False):
 
360
        """
 
361
        Get the file at the given relative path.
 
362
 
 
363
        :param relpath: The relative path to the file
 
364
        """
 
365
        try:
 
366
            path = self._remote_path(relpath)
 
367
            f = self._sftp.file(path, mode='rb')
 
368
            if self._do_prefetch and (getattr(f, 'prefetch', None) is not None):
 
369
                f.prefetch()
 
370
            return f
 
371
        except (IOError, paramiko.SSHException), e:
 
372
            self._translate_io_exception(e, path, ': error retrieving')
 
373
 
 
374
    def get_partial(self, relpath, start, length=None):
 
375
        """
 
376
        Get just part of a file.
 
377
 
 
378
        :param relpath: Path to the file, relative to base
 
379
        :param start: The starting position to read from
 
380
        :param length: The length to read. A length of None indicates
 
381
                       read to the end of the file.
 
382
        :return: A file-like object containing at least the specified bytes.
 
383
                 Some implementations may return objects which can be read
 
384
                 past this length, but this is not guaranteed.
 
385
        """
 
386
        # TODO: implement get_partial_multi to help with knit support
 
387
        f = self.get(relpath)
 
388
        f.seek(start)
 
389
        if self._do_prefetch and hasattr(f, 'prefetch'):
 
390
            f.prefetch()
 
391
        return f
 
392
 
 
393
    def put(self, relpath, f, mode=None):
 
394
        """
 
395
        Copy the file-like or string object into the location.
 
396
 
 
397
        :param relpath: Location to put the contents, relative to base.
 
398
        :param f:       File-like or string object.
 
399
        :param mode: The final mode for the file
 
400
        """
 
401
        final_path = self._remote_path(relpath)
 
402
        self._put(final_path, f, mode=mode)
 
403
 
 
404
    def _put(self, abspath, f, mode=None):
 
405
        """Helper function so both put() and copy_abspaths can reuse the code"""
 
406
        tmp_abspath = '%s.tmp.%.9f.%d.%d' % (abspath, time.time(),
 
407
                        os.getpid(), random.randint(0,0x7FFFFFFF))
 
408
        fout = self._sftp_open_exclusive(tmp_abspath, mode=mode)
 
409
        closed = False
 
410
        try:
 
411
            try:
 
412
                fout.set_pipelined(True)
 
413
                self._pump(f, fout)
 
414
            except (IOError, paramiko.SSHException), e:
 
415
                self._translate_io_exception(e, tmp_abspath)
 
416
            if mode is not None:
 
417
                self._sftp.chmod(tmp_abspath, mode)
 
418
            fout.close()
 
419
            closed = True
 
420
            self._rename_and_overwrite(tmp_abspath, abspath)
 
421
        except Exception, e:
 
422
            # If we fail, try to clean up the temporary file
 
423
            # before we throw the exception
 
424
            # but don't let another exception mess things up
 
425
            # Write out the traceback, because otherwise
 
426
            # the catch and throw destroys it
 
427
            import traceback
 
428
            mutter(traceback.format_exc())
 
429
            try:
 
430
                if not closed:
 
431
                    fout.close()
 
432
                self._sftp.remove(tmp_abspath)
 
433
            except:
 
434
                # raise the saved except
 
435
                raise e
 
436
            # raise the original with its traceback if we can.
 
437
            raise
 
438
 
 
439
    def iter_files_recursive(self):
 
440
        """Walk the relative paths of all files in this transport."""
 
441
        queue = list(self.list_dir('.'))
 
442
        while queue:
 
443
            relpath = urllib.quote(queue.pop(0))
 
444
            st = self.stat(relpath)
 
445
            if stat.S_ISDIR(st.st_mode):
 
446
                for i, basename in enumerate(self.list_dir(relpath)):
 
447
                    queue.insert(i, relpath+'/'+basename)
 
448
            else:
 
449
                yield relpath
 
450
 
 
451
    def mkdir(self, relpath, mode=None):
 
452
        """Create a directory at the given path."""
 
453
        try:
 
454
            path = self._remote_path(relpath)
 
455
            # In the paramiko documentation, it says that passing a mode flag 
 
456
            # will filtered against the server umask.
 
457
            # StubSFTPServer does not do this, which would be nice, because it is
 
458
            # what we really want :)
 
459
            # However, real servers do use umask, so we really should do it that way
 
460
            self._sftp.mkdir(path)
 
461
            if mode is not None:
 
462
                self._sftp.chmod(path, mode=mode)
 
463
        except (paramiko.SSHException, IOError), e:
 
464
            self._translate_io_exception(e, path, ': unable to mkdir',
 
465
                failure_exc=FileExists)
 
466
 
 
467
    def _translate_io_exception(self, e, path, more_info='', failure_exc=NoSuchFile):
 
468
        """Translate a paramiko or IOError into a friendlier exception.
 
469
 
 
470
        :param e: The original exception
 
471
        :param path: The path in question when the error is raised
 
472
        :param more_info: Extra information that can be included,
 
473
                          such as what was going on
 
474
        :param failure_exc: Paramiko has the super fun ability to raise completely
 
475
                           opaque errors that just set "e.args = ('Failure',)" with
 
476
                           no more information.
 
477
                           This sometimes means FileExists, but it also sometimes
 
478
                           means NoSuchFile
 
479
        """
 
480
        # paramiko seems to generate detailless errors.
 
481
        self._translate_error(e, path, raise_generic=False)
 
482
        if hasattr(e, 'args'):
 
483
            if (e.args == ('No such file or directory',) or
 
484
                e.args == ('No such file',)):
 
485
                raise NoSuchFile(path, str(e) + more_info)
 
486
            if (e.args == ('mkdir failed',)):
 
487
                raise FileExists(path, str(e) + more_info)
 
488
            # strange but true, for the paramiko server.
 
489
            if (e.args == ('Failure',)):
 
490
                raise failure_exc(path, str(e) + more_info)
 
491
            mutter('Raising exception with args %s', e.args)
 
492
        if hasattr(e, 'errno'):
 
493
            mutter('Raising exception with errno %s', e.errno)
 
494
        raise e
 
495
 
 
496
    def append(self, relpath, f):
 
497
        """
 
498
        Append the text in the file-like object into the final
 
499
        location.
 
500
        """
 
501
        try:
 
502
            path = self._remote_path(relpath)
 
503
            fout = self._sftp.file(path, 'ab')
 
504
            self._pump(f, fout)
 
505
        except (IOError, paramiko.SSHException), e:
 
506
            self._translate_io_exception(e, relpath, ': unable to append')
 
507
 
 
508
    def rename(self, rel_from, rel_to):
 
509
        """Rename without special overwriting"""
 
510
        try:
 
511
            self._sftp.rename(self._remote_path(rel_from),
 
512
                              self._remote_path(rel_to))
 
513
        except (IOError, paramiko.SSHException), e:
 
514
            self._translate_io_exception(e, rel_from,
 
515
                    ': unable to rename to %r' % (rel_to))
 
516
 
 
517
    def _rename_and_overwrite(self, abs_from, abs_to):
 
518
        """Do a fancy rename on the remote server.
 
519
        
 
520
        Using the implementation provided by osutils.
 
521
        """
 
522
        try:
 
523
            fancy_rename(abs_from, abs_to,
 
524
                    rename_func=self._sftp.rename,
 
525
                    unlink_func=self._sftp.remove)
 
526
        except (IOError, paramiko.SSHException), e:
 
527
            self._translate_io_exception(e, abs_from, ': unable to rename to %r' % (abs_to))
 
528
 
 
529
    def move(self, rel_from, rel_to):
 
530
        """Move the item at rel_from to the location at rel_to"""
 
531
        path_from = self._remote_path(rel_from)
 
532
        path_to = self._remote_path(rel_to)
 
533
        self._rename_and_overwrite(path_from, path_to)
 
534
 
 
535
    def delete(self, relpath):
 
536
        """Delete the item at relpath"""
 
537
        path = self._remote_path(relpath)
 
538
        try:
 
539
            self._sftp.remove(path)
 
540
        except (IOError, paramiko.SSHException), e:
 
541
            self._translate_io_exception(e, path, ': unable to delete')
 
542
            
 
543
    def listable(self):
 
544
        """Return True if this store supports listing."""
 
545
        return True
 
546
 
 
547
    def list_dir(self, relpath):
 
548
        """
 
549
        Return a list of all files at the given location.
 
550
        """
 
551
        # does anything actually use this?
 
552
        path = self._remote_path(relpath)
 
553
        try:
 
554
            return self._sftp.listdir(path)
 
555
        except (IOError, paramiko.SSHException), e:
 
556
            self._translate_io_exception(e, path, ': failed to list_dir')
 
557
 
 
558
    def rmdir(self, relpath):
 
559
        """See Transport.rmdir."""
 
560
        path = self._remote_path(relpath)
 
561
        try:
 
562
            return self._sftp.rmdir(path)
 
563
        except (IOError, paramiko.SSHException), e:
 
564
            self._translate_io_exception(e, path, ': failed to rmdir')
 
565
 
 
566
    def stat(self, relpath):
 
567
        """Return the stat information for a file."""
 
568
        path = self._remote_path(relpath)
 
569
        try:
 
570
            return self._sftp.stat(path)
 
571
        except (IOError, paramiko.SSHException), e:
 
572
            self._translate_io_exception(e, path, ': unable to stat')
 
573
 
 
574
    def lock_read(self, relpath):
 
575
        """
 
576
        Lock the given file for shared (read) access.
 
577
        :return: A lock object, which has an unlock() member function
 
578
        """
 
579
        # FIXME: there should be something clever i can do here...
 
580
        class BogusLock(object):
 
581
            def __init__(self, path):
 
582
                self.path = path
 
583
            def unlock(self):
 
584
                pass
 
585
        return BogusLock(relpath)
 
586
 
 
587
    def lock_write(self, relpath):
 
588
        """
 
589
        Lock the given file for exclusive (write) access.
 
590
        WARNING: many transports do not support this, so trying avoid using it
 
591
 
 
592
        :return: A lock object, which has an unlock() member function
 
593
        """
 
594
        # This is a little bit bogus, but basically, we create a file
 
595
        # which should not already exist, and if it does, we assume
 
596
        # that there is a lock, and if it doesn't, the we assume
 
597
        # that we have taken the lock.
 
598
        return SFTPLock(relpath, self)
 
599
 
 
600
    def _unparse_url(self, path=None):
 
601
        if path is None:
 
602
            path = self._path
 
603
        path = urllib.quote(path)
 
604
        # handle homedir paths
 
605
        if not path.startswith('/'):
 
606
            path = "/~/" + path
 
607
        netloc = urllib.quote(self._host)
 
608
        if self._username is not None:
 
609
            netloc = '%s@%s' % (urllib.quote(self._username), netloc)
 
610
        if self._port is not None:
 
611
            netloc = '%s:%d' % (netloc, self._port)
 
612
 
 
613
        return urlparse.urlunparse(('sftp', netloc, path, '', '', ''))
 
614
 
 
615
    def _split_url(self, url):
 
616
        if isinstance(url, unicode):
 
617
            url = url.encode('utf-8')
 
618
        (scheme, netloc, path, params,
 
619
         query, fragment) = urlparse.urlparse(url, allow_fragments=False)
 
620
        assert scheme == 'sftp'
 
621
        username = password = host = port = None
 
622
        if '@' in netloc:
 
623
            username, host = netloc.split('@', 1)
 
624
            if ':' in username:
 
625
                username, password = username.split(':', 1)
 
626
                password = urllib.unquote(password)
 
627
            username = urllib.unquote(username)
 
628
        else:
 
629
            host = netloc
 
630
 
 
631
        if ':' in host:
 
632
            host, port = host.rsplit(':', 1)
 
633
            try:
 
634
                port = int(port)
 
635
            except ValueError:
 
636
                # TODO: Should this be ConnectionError?
 
637
                raise TransportError('%s: invalid port number' % port)
 
638
        host = urllib.unquote(host)
 
639
 
 
640
        path = urllib.unquote(path)
 
641
 
 
642
        # the initial slash should be removed from the path, and treated
 
643
        # as a homedir relative path (the path begins with a double slash
 
644
        # if it is absolute).
 
645
        # see draft-ietf-secsh-scp-sftp-ssh-uri-03.txt
 
646
        # RBC 20060118 we are not using this as its too user hostile. instead
 
647
        # we are following lftp and using /~/foo to mean '~/foo'.
 
648
        # handle homedir paths
 
649
        if path.startswith('/~/'):
 
650
            path = path[3:]
 
651
        elif path == '/~':
 
652
            path = ''
 
653
        return (username, password, host, port, path)
 
654
 
 
655
    def _parse_url(self, url):
 
656
        (self._username, self._password,
 
657
         self._host, self._port, self._path) = self._split_url(url)
 
658
 
 
659
    def _sftp_connect(self):
 
660
        """Connect to the remote sftp server.
 
661
        After this, self._sftp should have a valid connection (or
 
662
        we raise an TransportError 'could not connect').
 
663
 
 
664
        TODO: Raise a more reasonable ConnectionFailed exception
 
665
        """
 
666
        global _connected_hosts
 
667
 
 
668
        idx = (self._host, self._port, self._username)
 
669
        try:
 
670
            self._sftp = _connected_hosts[idx]
 
671
            return
 
672
        except KeyError:
 
673
            pass
 
674
        
 
675
        vendor = _get_ssh_vendor()
 
676
        if vendor == 'loopback':
 
677
            sock = socket.socket()
 
678
            sock.connect((self._host, self._port))
 
679
            self._sftp = SFTPClient(LoopbackSFTP(sock))
 
680
        elif vendor != 'none':
 
681
            sock = SFTPSubprocess(self._host, vendor, self._port,
 
682
                                  self._username)
 
683
            self._sftp = SFTPClient(sock)
 
684
        else:
 
685
            self._paramiko_connect()
 
686
 
 
687
        _connected_hosts[idx] = self._sftp
 
688
 
 
689
    def _paramiko_connect(self):
 
690
        global SYSTEM_HOSTKEYS, BZR_HOSTKEYS
 
691
        
 
692
        load_host_keys()
 
693
 
 
694
        try:
 
695
            t = paramiko.Transport((self._host, self._port or 22))
 
696
            t.set_log_channel('bzr.paramiko')
 
697
            t.start_client()
 
698
        except paramiko.SSHException, e:
 
699
            raise ConnectionError('Unable to reach SSH host %s:%d' %
 
700
                                  (self._host, self._port), e)
 
701
            
 
702
        server_key = t.get_remote_server_key()
 
703
        server_key_hex = paramiko.util.hexify(server_key.get_fingerprint())
 
704
        keytype = server_key.get_name()
 
705
        if SYSTEM_HOSTKEYS.has_key(self._host) and SYSTEM_HOSTKEYS[self._host].has_key(keytype):
 
706
            our_server_key = SYSTEM_HOSTKEYS[self._host][keytype]
 
707
            our_server_key_hex = paramiko.util.hexify(our_server_key.get_fingerprint())
 
708
        elif BZR_HOSTKEYS.has_key(self._host) and BZR_HOSTKEYS[self._host].has_key(keytype):
 
709
            our_server_key = BZR_HOSTKEYS[self._host][keytype]
 
710
            our_server_key_hex = paramiko.util.hexify(our_server_key.get_fingerprint())
 
711
        else:
 
712
            warning('Adding %s host key for %s: %s' % (keytype, self._host, server_key_hex))
 
713
            if not BZR_HOSTKEYS.has_key(self._host):
 
714
                BZR_HOSTKEYS[self._host] = {}
 
715
            BZR_HOSTKEYS[self._host][keytype] = server_key
 
716
            our_server_key = server_key
 
717
            our_server_key_hex = paramiko.util.hexify(our_server_key.get_fingerprint())
 
718
            save_host_keys()
 
719
        if server_key != our_server_key:
 
720
            filename1 = os.path.expanduser('~/.ssh/known_hosts')
 
721
            filename2 = pathjoin(config_dir(), 'ssh_host_keys')
 
722
            raise TransportError('Host keys for %s do not match!  %s != %s' % \
 
723
                (self._host, our_server_key_hex, server_key_hex),
 
724
                ['Try editing %s or %s' % (filename1, filename2)])
 
725
 
 
726
        self._sftp_auth(t)
 
727
        
 
728
        try:
 
729
            self._sftp = t.open_sftp_client()
 
730
        except paramiko.SSHException, e:
 
731
            raise ConnectionError('Unable to start sftp client %s:%d' %
 
732
                                  (self._host, self._port), e)
 
733
 
 
734
    def _sftp_auth(self, transport):
 
735
        # paramiko requires a username, but it might be none if nothing was supplied
 
736
        # use the local username, just in case.
 
737
        # We don't override self._username, because if we aren't using paramiko,
 
738
        # the username might be specified in ~/.ssh/config and we don't want to
 
739
        # force it to something else
 
740
        # Also, it would mess up the self.relpath() functionality
 
741
        username = self._username or getpass.getuser()
 
742
 
 
743
        # Paramiko tries to open a socket.AF_UNIX in order to connect
 
744
        # to ssh-agent. That attribute doesn't exist on win32 (it does in cygwin)
 
745
        # so we get an AttributeError exception. For now, just don't try to
 
746
        # connect to an agent if we are on win32
 
747
        if sys.platform != 'win32':
 
748
            agent = paramiko.Agent()
 
749
            for key in agent.get_keys():
 
750
                mutter('Trying SSH agent key %s' % paramiko.util.hexify(key.get_fingerprint()))
 
751
                try:
 
752
                    transport.auth_publickey(username, key)
 
753
                    return
 
754
                except paramiko.SSHException, e:
 
755
                    pass
 
756
        
 
757
        # okay, try finding id_rsa or id_dss?  (posix only)
 
758
        if self._try_pkey_auth(transport, paramiko.RSAKey, username, 'id_rsa'):
 
759
            return
 
760
        if self._try_pkey_auth(transport, paramiko.DSSKey, username, 'id_dsa'):
 
761
            return
 
762
 
 
763
        if self._password:
 
764
            try:
 
765
                transport.auth_password(username, self._password)
 
766
                return
 
767
            except paramiko.SSHException, e:
 
768
                pass
 
769
 
 
770
            # FIXME: Don't keep a password held in memory if you can help it
 
771
            #self._password = None
 
772
 
 
773
        # give up and ask for a password
 
774
        password = bzrlib.ui.ui_factory.get_password(
 
775
                prompt='SSH %(user)s@%(host)s password',
 
776
                user=username, host=self._host)
 
777
        try:
 
778
            transport.auth_password(username, password)
 
779
        except paramiko.SSHException, e:
 
780
            raise ConnectionError('Unable to authenticate to SSH host as %s@%s' %
 
781
                                  (username, self._host), e)
 
782
 
 
783
    def _try_pkey_auth(self, transport, pkey_class, username, filename):
 
784
        filename = os.path.expanduser('~/.ssh/' + filename)
 
785
        try:
 
786
            key = pkey_class.from_private_key_file(filename)
 
787
            transport.auth_publickey(username, key)
 
788
            return True
 
789
        except paramiko.PasswordRequiredException:
 
790
            password = bzrlib.ui.ui_factory.get_password(
 
791
                    prompt='SSH %(filename)s password',
 
792
                    filename=filename)
 
793
            try:
 
794
                key = pkey_class.from_private_key_file(filename, password)
 
795
                transport.auth_publickey(username, key)
 
796
                return True
 
797
            except paramiko.SSHException:
 
798
                mutter('SSH authentication via %s key failed.' % (os.path.basename(filename),))
 
799
        except paramiko.SSHException:
 
800
            mutter('SSH authentication via %s key failed.' % (os.path.basename(filename),))
 
801
        except IOError:
 
802
            pass
 
803
        return False
 
804
 
 
805
    def _sftp_open_exclusive(self, abspath, mode=None):
 
806
        """Open a remote path exclusively.
 
807
 
 
808
        SFTP supports O_EXCL (SFTP_FLAG_EXCL), which fails if
 
809
        the file already exists. However it does not expose this
 
810
        at the higher level of SFTPClient.open(), so we have to
 
811
        sneak away with it.
 
812
 
 
813
        WARNING: This breaks the SFTPClient abstraction, so it
 
814
        could easily break against an updated version of paramiko.
 
815
 
 
816
        :param abspath: The remote absolute path where the file should be opened
 
817
        :param mode: The mode permissions bits for the new file
 
818
        """
 
819
        path = self._sftp._adjust_cwd(abspath)
 
820
        attr = SFTPAttributes()
 
821
        if mode is not None:
 
822
            attr.st_mode = mode
 
823
        omode = (SFTP_FLAG_WRITE | SFTP_FLAG_CREATE 
 
824
                | SFTP_FLAG_TRUNC | SFTP_FLAG_EXCL)
 
825
        try:
 
826
            t, msg = self._sftp._request(CMD_OPEN, path, omode, attr)
 
827
            if t != CMD_HANDLE:
 
828
                raise TransportError('Expected an SFTP handle')
 
829
            handle = msg.get_string()
 
830
            return SFTPFile(self._sftp, handle, 'wb', -1)
 
831
        except (paramiko.SSHException, IOError), e:
 
832
            self._translate_io_exception(e, abspath, ': unable to open',
 
833
                failure_exc=FileExists)
 
834
 
 
835
 
 
836
# ------------- server test implementation --------------
 
837
import socket
 
838
import threading
 
839
 
 
840
from bzrlib.tests.stub_sftp import StubServer, StubSFTPServer
 
841
 
 
842
STUB_SERVER_KEY = """
 
843
-----BEGIN RSA PRIVATE KEY-----
 
844
MIICWgIBAAKBgQDTj1bqB4WmayWNPB+8jVSYpZYk80Ujvj680pOTh2bORBjbIAyz
 
845
oWGW+GUjzKxTiiPvVmxFgx5wdsFvF03v34lEVVhMpouqPAYQ15N37K/ir5XY+9m/
 
846
d8ufMCkjeXsQkKqFbAlQcnWMCRnOoPHS3I4vi6hmnDDeeYTSRvfLbW0fhwIBIwKB
 
847
gBIiOqZYaoqbeD9OS9z2K9KR2atlTxGxOJPXiP4ESqP3NVScWNwyZ3NXHpyrJLa0
 
848
EbVtzsQhLn6rF+TzXnOlcipFvjsem3iYzCpuChfGQ6SovTcOjHV9z+hnpXvQ/fon
 
849
soVRZY65wKnF7IAoUwTmJS9opqgrN6kRgCd3DASAMd1bAkEA96SBVWFt/fJBNJ9H
 
850
tYnBKZGw0VeHOYmVYbvMSstssn8un+pQpUm9vlG/bp7Oxd/m+b9KWEh2xPfv6zqU
 
851
avNwHwJBANqzGZa/EpzF4J8pGti7oIAPUIDGMtfIcmqNXVMckrmzQ2vTfqtkEZsA
 
852
4rE1IERRyiJQx6EJsz21wJmGV9WJQ5kCQQDwkS0uXqVdFzgHO6S++tjmjYcxwr3g
 
853
H0CoFYSgbddOT6miqRskOQF3DZVkJT3kyuBgU2zKygz52ukQZMqxCb1fAkASvuTv
 
854
qfpH87Qq5kQhNKdbbwbmd2NxlNabazPijWuphGTdW0VfJdWfklyS2Kr+iqrs/5wV
 
855
HhathJt636Eg7oIjAkA8ht3MQ+XSl9yIJIS8gVpbPxSw5OMfw0PjVE7tBdQruiSc
 
856
nvuQES5C9BMHjF39LZiGH1iLQy7FgdHyoP+eodI7
 
857
-----END RSA PRIVATE KEY-----
 
858
"""
 
859
    
 
860
 
 
861
class SingleListener(threading.Thread):
 
862
 
 
863
    def __init__(self, callback):
 
864
        threading.Thread.__init__(self)
 
865
        self._callback = callback
 
866
        self._socket = socket.socket()
 
867
        self._socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 
868
        self._socket.bind(('localhost', 0))
 
869
        self._socket.listen(1)
 
870
        self.port = self._socket.getsockname()[1]
 
871
        self.stop_event = threading.Event()
 
872
 
 
873
    def run(self):
 
874
        s, _ = self._socket.accept()
 
875
        # now close the listen socket
 
876
        self._socket.close()
 
877
        try:
 
878
            self._callback(s, self.stop_event)
 
879
        except socket.error:
 
880
            pass #Ignore socket errors
 
881
        except Exception, x:
 
882
            # probably a failed test
 
883
            warning('Exception from within unit test server thread: %r' % x)
 
884
 
 
885
    def stop(self):
 
886
        self.stop_event.set()
 
887
        # use a timeout here, because if the test fails, the server thread may
 
888
        # never notice the stop_event.
 
889
        self.join(5.0)
 
890
 
 
891
 
 
892
class SFTPServer(Server):
 
893
    """Common code for SFTP server facilities."""
 
894
 
 
895
    def __init__(self):
 
896
        self._original_vendor = None
 
897
        self._homedir = None
 
898
        self._server_homedir = None
 
899
        self._listener = None
 
900
        self._root = None
 
901
        self._vendor = 'none'
 
902
        # sftp server logs
 
903
        self.logs = []
 
904
 
 
905
    def _get_sftp_url(self, path):
 
906
        """Calculate an sftp url to this server for path."""
 
907
        return 'sftp://foo:bar@localhost:%d/%s' % (self._listener.port, path)
 
908
 
 
909
    def log(self, message):
 
910
        """StubServer uses this to log when a new server is created."""
 
911
        self.logs.append(message)
 
912
 
 
913
    def _run_server(self, s, stop_event):
 
914
        ssh_server = paramiko.Transport(s)
 
915
        key_file = os.path.join(self._homedir, 'test_rsa.key')
 
916
        file(key_file, 'w').write(STUB_SERVER_KEY)
 
917
        host_key = paramiko.RSAKey.from_private_key_file(key_file)
 
918
        ssh_server.add_server_key(host_key)
 
919
        server = StubServer(self)
 
920
        ssh_server.set_subsystem_handler('sftp', paramiko.SFTPServer,
 
921
                                         StubSFTPServer, root=self._root,
 
922
                                         home=self._server_homedir)
 
923
        event = threading.Event()
 
924
        ssh_server.start_server(event, server)
 
925
        event.wait(5.0)
 
926
        stop_event.wait(30.0)
 
927
    
 
928
    def setUp(self):
 
929
        global _ssh_vendor
 
930
        self._original_vendor = _ssh_vendor
 
931
        _ssh_vendor = self._vendor
 
932
        self._homedir = os.getcwdu()
 
933
        if self._server_homedir is None:
 
934
            self._server_homedir = self._homedir
 
935
        self._root = '/'
 
936
        # FIXME WINDOWS: _root should be _server_homedir[0]:/
 
937
        self._listener = SingleListener(self._run_server)
 
938
        self._listener.setDaemon(True)
 
939
        self._listener.start()
 
940
 
 
941
    def tearDown(self):
 
942
        """See bzrlib.transport.Server.tearDown."""
 
943
        global _ssh_vendor
 
944
        self._listener.stop()
 
945
        _ssh_vendor = self._original_vendor
 
946
 
 
947
 
 
948
class SFTPFullAbsoluteServer(SFTPServer):
 
949
    """A test server for sftp transports, using absolute urls and ssh."""
 
950
 
 
951
    def get_url(self):
 
952
        """See bzrlib.transport.Server.get_url."""
 
953
        return self._get_sftp_url(urlescape(self._homedir[1:]))
 
954
 
 
955
 
 
956
class SFTPServerWithoutSSH(SFTPServer):
 
957
    """An SFTP server that uses a simple TCP socket pair rather than SSH."""
 
958
 
 
959
    def __init__(self):
 
960
        super(SFTPServerWithoutSSH, self).__init__()
 
961
        self._vendor = 'loopback'
 
962
 
 
963
    def _run_server(self, sock, stop_event):
 
964
        class FakeChannel(object):
 
965
            def get_transport(self):
 
966
                return self
 
967
            def get_log_channel(self):
 
968
                return 'paramiko'
 
969
            def get_name(self):
 
970
                return '1'
 
971
            def get_hexdump(self):
 
972
                return False
 
973
 
 
974
        server = paramiko.SFTPServer(FakeChannel(), 'sftp', StubServer(self), StubSFTPServer,
 
975
                                     root=self._root, home=self._server_homedir)
 
976
        server.start_subsystem('sftp', None, sock)
 
977
        server.finish_subsystem()
 
978
 
 
979
 
 
980
class SFTPAbsoluteServer(SFTPServerWithoutSSH):
 
981
    """A test server for sftp transports, using absolute urls."""
 
982
 
 
983
    def get_url(self):
 
984
        """See bzrlib.transport.Server.get_url."""
 
985
        return self._get_sftp_url(urlescape(self._homedir[1:]))
 
986
 
 
987
 
 
988
class SFTPHomeDirServer(SFTPServerWithoutSSH):
 
989
    """A test server for sftp transports, using homedir relative urls."""
 
990
 
 
991
    def get_url(self):
 
992
        """See bzrlib.transport.Server.get_url."""
 
993
        return self._get_sftp_url("~/")
 
994
 
 
995
 
 
996
class SFTPSiblingAbsoluteServer(SFTPAbsoluteServer):
 
997
    """A test servere for sftp transports, using absolute urls to non-home."""
 
998
 
 
999
    def setUp(self):
 
1000
        self._server_homedir = '/dev/noone/runs/tests/here'
 
1001
        super(SFTPSiblingAbsoluteServer, self).setUp()
 
1002
 
 
1003
 
 
1004
def get_test_permutations():
 
1005
    """Return the permutations to be used in testing."""
 
1006
    return [(SFTPTransport, SFTPAbsoluteServer),
 
1007
            (SFTPTransport, SFTPHomeDirServer),
 
1008
            (SFTPTransport, SFTPSiblingAbsoluteServer),
 
1009
            ]