Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add server root directory support #12

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add API support for defining a root directory
Diego Argueta committed May 24, 2018
commit 6314ac1d86bdce68947cad54e67167a39da14ebd
20 changes: 11 additions & 9 deletions src/sftpserver/stub_sftp.py
Original file line number Diff line number Diff line change
@@ -29,11 +29,11 @@ class StubServer (ServerInterface):
def check_auth_password(self, username, password):
# all are allowed
return AUTH_SUCCESSFUL

def check_auth_publickey(self, username, key):
# all are allowed
return AUTH_SUCCESSFUL

def check_channel_request(self, kind, chanid):
return OPEN_SUCCEEDED

@@ -62,10 +62,12 @@ def chattr(self, attr):
class StubSFTPServer (SFTPServerInterface):
# assume current folder is a fine root
# (the tests always create and eventualy delete a subfolder, so there shouldn't be any mess)
ROOT = os.getcwd()

def __init__(self, server, root_directory=None):
super(StubSFTPServer, self).__init__(server)
self.root_directory = root_directory or os.getcwd()

def _realpath(self, path):
return self.ROOT + self.canonicalize(path)
return self.root_directory + self.canonicalize(path)

def list_folder(self, path):
path = self._realpath(path)
@@ -181,14 +183,14 @@ def symlink(self, target_path, path):
path = self._realpath(path)
if (len(target_path) > 0) and (target_path[0] == '/'):
# absolute symlink
target_path = os.path.join(self.ROOT, target_path[1:])
target_path = os.path.join(self.root_directory, target_path[1:])
if target_path[:2] == '//':
# bug in os.path.join
target_path = target_path[1:]
else:
# compute relative to path
abspath = os.path.join(os.path.dirname(path), target_path)
if abspath[:len(self.ROOT)] != self.ROOT:
if abspath[:len(self.root_directory)] != self.root_directory:
# this symlink isn't going to work anyway -- just break it immediately
target_path = '<error>'
try:
@@ -205,8 +207,8 @@ def readlink(self, path):
return SFTPServer.convert_errno(e.errno)
# if it's absolute, remove the root
if os.path.isabs(symlink):
if symlink[:len(self.ROOT)] == self.ROOT:
symlink = symlink[len(self.ROOT):]
if symlink[:len(self.root_directory)] == self.root_directory:
symlink = symlink[len(self.root_directory):]
if (len(symlink) == 0) or (symlink[0] != '/'):
symlink = '/' + symlink
else: