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
26
27
from bzrlib.osutils import pathjoin
27
28
from bzrlib.trace import mutter
65
66
def __init__(self, server, root, home=None):
66
67
SFTPServerInterface.__init__(self, server)
68
# All paths are actually relative to 'root'.
69
# this is like implementing chroot().
74
if not home.startswith(self.root):
76
"home must be a subdirectory of root (%s vs %s)"
71
78
self.home = home[len(self.root):]
72
if (len(self.home) > 0) and (self.home[0] == '/'):
79
if self.home.startswith('/'):
73
80
self.home = self.home[1:]
74
81
server._test_case.log('sftpserver - new connection')
76
83
def _realpath(self, path):
84
# paths returned from self.canonicalize() always start with
85
# a path separator. So if 'root' is just '/', this would cause
86
# a double slash at the beginning '//home/dir'.
88
return self.canonicalize(path)
77
89
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))
91
if sys.platform == 'win32':
92
def canonicalize(self, path):
93
# Win32 sftp paths end up looking like
94
# sftp://host@foo/h:/foo/bar
95
# which means absolute paths look like:
97
# and relative paths stay the same:
99
# win32 needs to use the Unicode APIs. so we require the
100
# paths to be utf8 (Linux just uses bytestreams)
101
thispath = path.decode('utf8')
102
if path.startswith('/'):
104
return os.path.normpath(thispath[1:])
106
return os.path.normpath(os.path.join(self.home, thispath))
108
def canonicalize(self, path):
109
if os.path.isabs(path):
110
return os.path.normpath(path)
112
return os.path.normpath('/' + os.path.join(self.home, path))
85
114
def chattr(self, path, attr):
96
125
# TODO: win32 incorrectly lists paths with non-ascii if path is not
97
126
# unicode. However on Linux the server should only deal with
98
127
# bytestreams and posix.listdir does the right thing
99
flist = os.listdir(path)
128
if sys.platform == 'win32':
129
flist = [f.encode('utf8') for f in os.listdir(path)]
131
flist = os.listdir(path)
100
132
for fname in flist:
101
133
attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname)))
102
134
attr.filename = fname
122
154
def open(self, path, flags, attr):
123
155
path = self._realpath(path)
125
if hasattr(os, 'O_BINARY'):
157
flags |= getattr(os, 'O_BINARY', 0)
127
158
if getattr(attr, 'st_mode', None):
128
159
fd = os.open(path, flags, attr.st_mode)
130
fd = os.open(path, flags)
161
# os.open() defaults to 0777 which is
162
# an odd default mode for files
163
fd = os.open(path, flags, 0666)
131
164
except OSError, e:
132
165
return SFTPServer.convert_errno(e.errno)