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
26
from bzrlib.osutils import pathjoin
27
from bzrlib.trace import mutter
30
class StubServer (ServerInterface):
32
def __init__(self, test_case):
33
ServerInterface.__init__(self)
34
self._test_case = test_case
36
def check_auth_password(self, username, password):
38
self._test_case.log('sftpserver - authorizing: %s' % (username,))
39
return AUTH_SUCCESSFUL
41
def check_channel_request(self, kind, chanid):
42
self._test_case.log('sftpserver - channel request: %s, %s' % (kind, chanid))
46
class StubSFTPHandle (SFTPHandle):
49
return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno()))
51
return SFTPServer.convert_errno(e.errno)
53
def chattr(self, attr):
54
# python doesn't have equivalents to fchown or fchmod, so we have to
55
# use the stored filename
56
mutter('Changing permissions on %s to %s', self.filename, attr)
58
SFTPServer.set_file_attr(self.filename, attr)
60
return SFTPServer.convert_errno(e.errno)
63
class StubSFTPServer (SFTPServerInterface):
65
def __init__(self, server, root, home=None):
66
SFTPServerInterface.__init__(self, server)
71
self.home = home[len(self.root):]
72
if (len(self.home) > 0) and (self.home[0] == '/'):
73
self.home = self.home[1:]
74
server._test_case.log('sftpserver - new connection')
76
def _realpath(self, path):
77
return self.root + self.canonicalize(path)
79
def canonicalize(self, path):
80
if os.path.isabs(path):
81
return os.path.normpath(path)
83
return os.path.normpath('/' + os.path.join(self.home, path))
85
def chattr(self, path, attr):
87
SFTPServer.set_file_attr(path, attr)
89
return SFTPServer.convert_errno(e.errno)
92
def list_folder(self, path):
93
path = self._realpath(path)
96
flist = os.listdir(path)
98
attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
103
return SFTPServer.convert_errno(e.errno)
105
def stat(self, path):
106
path = self._realpath(path)
108
return SFTPAttributes.from_stat(os.stat(path))
110
return SFTPServer.convert_errno(e.errno)
112
def lstat(self, path):
113
path = self._realpath(path)
115
return SFTPAttributes.from_stat(os.lstat(path))
117
return SFTPServer.convert_errno(e.errno)
119
def open(self, path, flags, attr):
120
path = self._realpath(path)
122
if hasattr(os, 'O_BINARY'):
124
if getattr(attr, 'st_mode', None):
125
fd = os.open(path, flags, attr.st_mode)
127
fd = os.open(path, flags)
129
return SFTPServer.convert_errno(e.errno)
130
if (flags & os.O_CREAT) and (attr is not None):
131
attr._flags &= ~attr.FLAG_PERMISSIONS
132
SFTPServer.set_file_attr(path, attr)
133
if flags & os.O_WRONLY:
135
elif flags & os.O_RDWR:
141
f = os.fdopen(fd, fstr)
143
return SFTPServer.convert_errno(e.errno)
144
fobj = StubSFTPHandle()
150
def remove(self, path):
151
path = self._realpath(path)
155
return SFTPServer.convert_errno(e.errno)
158
def rename(self, oldpath, newpath):
159
oldpath = self._realpath(oldpath)
160
newpath = self._realpath(newpath)
162
os.rename(oldpath, newpath)
164
return SFTPServer.convert_errno(e.errno)
167
def mkdir(self, path, attr):
168
path = self._realpath(path)
170
# Using getattr() in case st_mode is None or 0
171
# both evaluate to False
172
if getattr(attr, 'st_mode', None):
173
os.mkdir(path, attr.st_mode)
177
attr._flags &= ~attr.FLAG_PERMISSIONS
178
SFTPServer.set_file_attr(path, attr)
180
return SFTPServer.convert_errno(e.errno)
183
def rmdir(self, path):
184
path = self._realpath(path)
188
return SFTPServer.convert_errno(e.errno)
191
# removed: chattr, symlink, readlink
192
# (nothing in bzr's sftp transport uses those)