1
1
# Copyright (C) 2005 Robey Pointer <robey@lag.net>, Canonical Ltd
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
5
5
# the Free Software Foundation; either version 2 of the License, or
6
6
# (at your option) any later version.
8
8
# This program is distributed in the hope that it will be useful,
9
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
11
# GNU General Public License for more details.
13
13
# You should have received a copy of the GNU General Public License
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
23
from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \
24
24
SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED
27
from bzrlib.osutils import pathjoin
28
from bzrlib.trace import mutter
27
31
class StubServer (ServerInterface):
28
33
def __init__(self, test_case):
29
34
ServerInterface.__init__(self)
30
35
self._test_case = test_case
58
64
class StubSFTPServer (SFTPServerInterface):
59
def __init__(self, server, root):
66
def __init__(self, server, root, home=None):
60
67
SFTPServerInterface.__init__(self, server)
68
# All paths are actually relative to 'root'.
69
# this is like implementing chroot().
74
assert home.startswith(self.root), \
75
"home must be a subdirectory of root (%s vs %s)" \
77
self.home = home[len(self.root):]
78
if self.home.startswith('/'):
79
self.home = self.home[1:]
80
server._test_case.log('sftpserver - new connection')
63
82
def _realpath(self, path):
83
# paths returned from self.canonicalize() always start with
84
# a path separator. So if 'root' is just '/', this would cause
85
# a double slash at the beginning '//home/dir'.
87
return self.canonicalize(path)
64
88
return self.root + self.canonicalize(path)
90
if sys.platform == 'win32':
91
def canonicalize(self, path):
92
# Win32 sftp paths end up looking like
93
# sftp://host@foo/h:/foo/bar
94
# which means absolute paths look like:
96
# and relative paths stay the same:
98
# win32 needs to use the Unicode APIs. so we require the
99
# paths to be utf8 (Linux just uses bytestreams)
100
thispath = path.decode('utf8')
101
if path.startswith('/'):
103
return os.path.normpath(thispath[1:])
105
return os.path.normpath(os.path.join(self.home, thispath))
107
def canonicalize(self, path):
108
if os.path.isabs(path):
109
return os.path.normpath(path)
111
return os.path.normpath('/' + os.path.join(self.home, path))
113
def chattr(self, path, attr):
115
SFTPServer.set_file_attr(path, attr)
117
return SFTPServer.convert_errno(e.errno)
66
120
def list_folder(self, path):
67
121
path = self._realpath(path)
70
flist = os.listdir(path)
124
# TODO: win32 incorrectly lists paths with non-ascii if path is not
125
# unicode. However on Linux the server should only deal with
126
# bytestreams and posix.listdir does the right thing
127
if sys.platform == 'win32':
128
flist = [f.encode('utf8') for f in os.listdir(path)]
130
flist = os.listdir(path)
71
131
for fname in flist:
72
attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname)))
132
attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
73
133
attr.filename = fname
93
153
def open(self, path, flags, attr):
94
154
path = self._realpath(path)
96
fd = os.open(path, flags)
156
flags |= getattr(os, 'O_BINARY', 0)
157
if getattr(attr, 'st_mode', None):
158
fd = os.open(path, flags, attr.st_mode)
160
fd = os.open(path, flags)
97
161
except OSError, e:
98
162
return SFTPServer.convert_errno(e.errno)
99
164
if (flags & os.O_CREAT) and (attr is not None):
165
attr._flags &= ~attr.FLAG_PERMISSIONS
100
166
SFTPServer.set_file_attr(path, attr)
101
167
if flags & os.O_WRONLY:
103
169
elif flags & os.O_RDWR:
106
172
# O_RDONLY (== 0)
109
175
f = os.fdopen(fd, fstr)
176
except (IOError, OSError), e:
111
177
return SFTPServer.convert_errno(e.errno)
112
178
fobj = StubSFTPHandle()
113
179
fobj.filename = path
135
201
def mkdir(self, path, attr):
136
202
path = self._realpath(path)
204
# Using getattr() in case st_mode is None or 0
205
# both evaluate to False
206
if getattr(attr, 'st_mode', None):
207
os.mkdir(path, attr.st_mode)
139
210
if attr is not None:
211
attr._flags &= ~attr.FLAG_PERMISSIONS
140
212
SFTPServer.set_file_attr(path, attr)
141
213
except OSError, e:
142
214
return SFTPServer.convert_errno(e.errno)