1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, Canonical Ltd
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.
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.
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
18
A stub SFTP server for loopback SFTP testing.
19
Adapted from the one in paramiko's unit tests.
23
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \
24
SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED
25
from bzrlib.osutils import pathjoin
26
from bzrlib.trace import mutter
29
class StubServer (ServerInterface):
30
def __init__(self, test_case):
31
ServerInterface.__init__(self)
32
self._test_case = test_case
34
def check_auth_password(self, username, password):
36
self._test_case.log('sftpserver - authorizing: %s' % (username,))
37
return AUTH_SUCCESSFUL
39
def check_channel_request(self, kind, chanid):
40
self._test_case.log('sftpserver - channel request: %s, %s' % (kind, chanid))
44
class StubSFTPHandle (SFTPHandle):
47
return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno()))
49
return SFTPServer.convert_errno(e.errno)
51
def chattr(self, attr):
52
# python doesn't have equivalents to fchown or fchmod, so we have to
53
# use the stored filename
54
mutter('Changing permissions on %s to %s', self.filename, attr)
56
SFTPServer.set_file_attr(self.filename, attr)
58
return SFTPServer.convert_errno(e.errno)
61
class StubSFTPServer (SFTPServerInterface):
62
def __init__(self, server, root, home=None):
63
SFTPServerInterface.__init__(self, server)
68
self.home = home[len(self.root):]
69
if (len(self.home) > 0) and (self.home[0] == '/'):
70
self.home = self.home[1:]
72
def _realpath(self, path):
73
return self.root + self.canonicalize(path)
75
def canonicalize(self, path):
76
if os.path.isabs(path):
77
return os.path.normpath(path)
79
return os.path.normpath('/' + os.path.join(self.home, path))
81
def chattr(self, path, attr):
83
SFTPServer.set_file_attr(path, attr)
85
return SFTPServer.convert_errno(e.errno)
88
def list_folder(self, path):
89
path = self._realpath(path)
92
flist = os.listdir(path)
94
attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
99
return SFTPServer.convert_errno(e.errno)
101
def stat(self, path):
102
path = self._realpath(path)
104
return SFTPAttributes.from_stat(os.stat(path))
106
return SFTPServer.convert_errno(e.errno)
108
def lstat(self, path):
109
path = self._realpath(path)
111
return SFTPAttributes.from_stat(os.lstat(path))
113
return SFTPServer.convert_errno(e.errno)
115
def open(self, path, flags, attr):
116
path = self._realpath(path)
118
if hasattr(os, 'O_BINARY'):
120
if getattr(attr, 'st_mode', None):
121
fd = os.open(path, flags, attr.st_mode)
123
fd = os.open(path, flags)
125
return SFTPServer.convert_errno(e.errno)
126
if (flags & os.O_CREAT) and (attr is not None):
127
attr._flags &= ~attr.FLAG_PERMISSIONS
128
SFTPServer.set_file_attr(path, attr)
129
if flags & os.O_WRONLY:
131
elif flags & os.O_RDWR:
137
f = os.fdopen(fd, fstr)
139
return SFTPServer.convert_errno(e.errno)
140
fobj = StubSFTPHandle()
146
def remove(self, path):
147
path = self._realpath(path)
151
return SFTPServer.convert_errno(e.errno)
154
def rename(self, oldpath, newpath):
155
oldpath = self._realpath(oldpath)
156
newpath = self._realpath(newpath)
158
os.rename(oldpath, newpath)
160
return SFTPServer.convert_errno(e.errno)
163
def mkdir(self, path, attr):
164
path = self._realpath(path)
166
# Using getattr() in case st_mode is None or 0
167
# both evaluate to False
168
if getattr(attr, 'st_mode', None):
169
os.mkdir(path, attr.st_mode)
173
attr._flags &= ~attr.FLAG_PERMISSIONS
174
SFTPServer.set_file_attr(path, attr)
176
return SFTPServer.convert_errno(e.errno)
179
def rmdir(self, path):
180
path = self._realpath(path)
184
return SFTPServer.convert_errno(e.errno)
187
# removed: chattr, symlink, readlink
188
# (nothing in bzr's sftp transport uses those)