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
# TODO: win32 incorrectly lists paths with non-ascii if path is not
97
# unicode. However on Linux the server should only deal with
98
# bytestreams and posix.listdir does the right thing
99
flist = os.listdir(path)
101
attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
102
attr.filename = fname
106
return SFTPServer.convert_errno(e.errno)
108
def stat(self, path):
109
path = self._realpath(path)
111
return SFTPAttributes.from_stat(os.stat(path))
113
return SFTPServer.convert_errno(e.errno)
115
def lstat(self, path):
116
path = self._realpath(path)
118
return SFTPAttributes.from_stat(os.lstat(path))
120
return SFTPServer.convert_errno(e.errno)
122
def open(self, path, flags, attr):
123
path = self._realpath(path)
125
if hasattr(os, 'O_BINARY'):
127
if getattr(attr, 'st_mode', None):
128
fd = os.open(path, flags, attr.st_mode)
130
fd = os.open(path, flags)
132
return SFTPServer.convert_errno(e.errno)
134
if (flags & os.O_CREAT) and (attr is not None):
135
attr._flags &= ~attr.FLAG_PERMISSIONS
136
SFTPServer.set_file_attr(path, attr)
137
if flags & os.O_WRONLY:
139
elif flags & os.O_RDWR:
145
f = os.fdopen(fd, fstr)
147
return SFTPServer.convert_errno(e.errno)
148
fobj = StubSFTPHandle()
154
def remove(self, path):
155
path = self._realpath(path)
159
return SFTPServer.convert_errno(e.errno)
162
def rename(self, oldpath, newpath):
163
oldpath = self._realpath(oldpath)
164
newpath = self._realpath(newpath)
166
os.rename(oldpath, newpath)
168
return SFTPServer.convert_errno(e.errno)
171
def mkdir(self, path, attr):
172
path = self._realpath(path)
174
# Using getattr() in case st_mode is None or 0
175
# both evaluate to False
176
if getattr(attr, 'st_mode', None):
177
os.mkdir(path, attr.st_mode)
181
attr._flags &= ~attr.FLAG_PERMISSIONS
182
SFTPServer.set_file_attr(path, attr)
184
return SFTPServer.convert_errno(e.errno)
187
def rmdir(self, path):
188
path = self._realpath(path)
192
return SFTPServer.convert_errno(e.errno)
195
# removed: chattr, symlink, readlink
196
# (nothing in bzr's sftp transport uses those)