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
path = path.decode('utf-8')
81
if os.path.isabs(path):
82
return os.path.normpath(path)
84
return os.path.normpath('/' + os.path.join(self.home, path))
86
def chattr(self, path, attr):
88
SFTPServer.set_file_attr(path, attr)
90
return SFTPServer.convert_errno(e.errno)
93
def list_folder(self, path):
94
path = self._realpath(path)
97
flist = os.listdir(path)
99
attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
100
attr.filename = fname
104
return SFTPServer.convert_errno(e.errno)
106
def stat(self, path):
107
path = self._realpath(path)
109
return SFTPAttributes.from_stat(os.stat(path))
111
return SFTPServer.convert_errno(e.errno)
113
def lstat(self, path):
114
path = self._realpath(path)
116
return SFTPAttributes.from_stat(os.lstat(path))
118
return SFTPServer.convert_errno(e.errno)
120
def open(self, path, flags, attr):
121
path = self._realpath(path)
123
if hasattr(os, 'O_BINARY'):
125
if getattr(attr, 'st_mode', None):
126
fd = os.open(path, flags, attr.st_mode)
128
fd = os.open(path, flags)
130
return SFTPServer.convert_errno(e.errno)
131
if (flags & os.O_CREAT) and (attr is not None):
132
attr._flags &= ~attr.FLAG_PERMISSIONS
133
SFTPServer.set_file_attr(path, attr)
134
if flags & os.O_WRONLY:
136
elif flags & os.O_RDWR:
142
f = os.fdopen(fd, fstr)
144
return SFTPServer.convert_errno(e.errno)
145
fobj = StubSFTPHandle()
151
def remove(self, path):
152
path = self._realpath(path)
156
return SFTPServer.convert_errno(e.errno)
159
def rename(self, oldpath, newpath):
160
oldpath = self._realpath(oldpath)
161
newpath = self._realpath(newpath)
163
os.rename(oldpath, newpath)
165
return SFTPServer.convert_errno(e.errno)
168
def mkdir(self, path, attr):
169
path = self._realpath(path)
171
# Using getattr() in case st_mode is None or 0
172
# both evaluate to False
173
if getattr(attr, 'st_mode', None):
174
os.mkdir(path, attr.st_mode)
178
attr._flags &= ~attr.FLAG_PERMISSIONS
179
SFTPServer.set_file_attr(path, attr)
181
return SFTPServer.convert_errno(e.errno)
184
def rmdir(self, path):
185
path = self._realpath(path)
189
return SFTPServer.convert_errno(e.errno)
192
# removed: chattr, symlink, readlink
193
# (nothing in bzr's sftp transport uses those)