Пример #1
0
 def setUp(self):
     os.chdir(t_path())
     self.home = 'home'
     if not os.path.isdir(self.home):
         os.mkdir(self.home)
     self.server = SFTPServer(SFTPServerStorage(self.home),
                              hook=UrlRequestHook('test_url'),
                              logfile=t_path('log'),
                              raise_on_error=True)
Пример #2
0
    def setUp(self):
        os.chdir(t_path())
        self.home = 'home'

        if not os.path.isdir(self.home):
            os.mkdir(self.home)

        self.server = SFTPServer(SFTPServerVirtualChroot(self.home),
                                 logfile=t_path('log'),
                                 raise_on_error=True)
Пример #3
0
 def setUp(self):
     os.chdir(t_path())
     self.home = 'home'
     if not os.path.isdir(self.home):
         os.mkdir(self.home)
     self.server = SFTPServer(
         SFTPServerStorage(self.home),
         hook=UrlRequestHook('test_url'),
         logfile=t_path('log'),
         raise_on_error=True
     )
Пример #4
0
    def setUp(self):
        os.chdir(t_path())
        self.home = 'home'

        if not os.path.isdir(self.home):
            os.mkdir(self.home)

        self.server = SFTPServer(
            SFTPServerVirtualChroot(self.home),
            logfile=t_path('log'),
            raise_on_error=True
        )
Пример #5
0
 def tearDownClass(cls):
     os.unlink(t_path('log'))  # comment me to see the log!
     rmtree(t_path('home'), ignore_errors=True)
Пример #6
0
 def tearDown(self):
     os.chdir(t_path())
     rmtree(self.home)
Пример #7
0
 def tearDownClass(cls):
     os.unlink(t_path('log'))  # comment me to see the log!
     rmtree(t_path('home'), ignore_errors=True)
Пример #8
0
 def tearDown(self):
     os.chdir(t_path())
     rmtree(self.home)
Пример #9
0
"""


import os

from paramiko import (AUTH_FAILED, AUTH_SUCCESSFUL, OPEN_SUCCEEDED, SFTP_OK,
                      RSAKey, ServerInterface, SFTPAttributes, SFTPHandle,
                      SFTPServer, SFTPServerInterface)
from paramiko.common import o666

from pysftpserver.tests.utils import t_path


USERNAME = "******"
PASSWORD = "******"
RSA_KEY = t_path("id_rsa")

SERVER_ROOT = "server_root"


class StubServer (ServerInterface):

    good_pub_key = RSAKey(filename=RSA_KEY)

    def check_auth_password(self, username, password):
        if username == USERNAME and password == PASSWORD:
            return AUTH_SUCCESSFUL
        return AUTH_FAILED

    def check_auth_publickey(self, username, key):
        if username == USERNAME and key == self.good_pub_key:
Пример #10
0
class StubSFTPServer(SFTPServerInterface):
    ROOT = t_path(SERVER_ROOT)

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

    def list_folder(self, path):
        path = self._realpath(path)
        try:
            out = []
            flist = os.listdir(path)
            for fname in flist:
                attr = SFTPAttributes.from_stat(
                    os.lstat(os.path.join(path, fname)))
                attr.filename = fname
                out.append(attr)
            return out
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)

    def stat(self, path):
        path = self._realpath(path)
        try:
            return SFTPAttributes.from_stat(os.stat(path))
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)

    def lstat(self, path):
        path = self._realpath(path)
        try:
            return SFTPAttributes.from_stat(os.lstat(path))
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)

    def open(self, path, flags, attr):
        path = self._realpath(path)
        try:
            binary_flag = getattr(os, 'O_BINARY', 0)
            flags |= binary_flag
            mode = getattr(attr, 'st_mode', None)
            if mode is not None:
                fd = os.open(path, flags, mode)
            else:
                # os.open() defaults to 0777 which is
                # an odd default mode for files
                fd = os.open(path, flags, o666)
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)
        if (flags & os.O_CREAT) and (attr is not None):
            attr._flags &= ~attr.FLAG_PERMISSIONS
            SFTPServer.set_file_attr(path, attr)
        if flags & os.O_WRONLY:
            if flags & os.O_APPEND:
                fstr = 'ab'
            else:
                fstr = 'wb'
        elif flags & os.O_RDWR:
            if flags & os.O_APPEND:
                fstr = 'a+b'
            else:
                fstr = 'r+b'
        else:
            # O_RDONLY (== 0)
            fstr = 'rb'
        try:
            f = os.fdopen(fd, fstr)
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)
        fobj = StubSFTPHandle(flags)
        fobj.filename = path
        fobj.readfile = f
        fobj.writefile = f
        return fobj

    def remove(self, path):
        path = self._realpath(path)
        try:
            os.remove(path)
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)
        return SFTP_OK

    def rename(self, oldpath, newpath):
        oldpath = self._realpath(oldpath)
        newpath = self._realpath(newpath)
        try:
            os.rename(oldpath, newpath)
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)
        return SFTP_OK

    def mkdir(self, path, attr):
        path = self._realpath(path)
        try:
            os.mkdir(path)
            if attr is not None:
                SFTPServer.set_file_attr(path, attr)
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)
        return SFTP_OK

    def rmdir(self, path):
        path = self._realpath(path)
        try:
            os.rmdir(path)
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)
        return SFTP_OK

    def chattr(self, path, attr):
        path = self._realpath(path)
        try:
            SFTPServer.set_file_attr(path, attr)
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)
        return SFTP_OK

    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:])

        try:
            os.symlink(target_path, path)
        except OSError as e:
            return SFTPServer.convert_errno(e.errno)
        return SFTP_OK

    def readlink(self, path):
        path = self._realpath(path)
        try:
            symlink = os.readlink(path)
        except OSError as e:
            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 (len(symlink) == 0) or (symlink[0] != '/'):
                    symlink = '/' + symlink
            else:
                symlink = '<error>'
        return symlink
Пример #11
0
"""
A stub SFTP server for loopback SFTP testing.
"""

import os

from paramiko import (AUTH_FAILED, AUTH_SUCCESSFUL, OPEN_SUCCEEDED, SFTP_OK,
                      RSAKey, ServerInterface, SFTPAttributes, SFTPHandle,
                      SFTPServer, SFTPServerInterface)
from paramiko.common import o666

from pysftpserver.tests.utils import t_path

USERNAME = "******"
PASSWORD = "******"
RSA_KEY = t_path("id_rsa")

SERVER_ROOT = "server_root"


class StubServer(ServerInterface):

    good_pub_key = RSAKey(filename=RSA_KEY)

    def check_auth_password(self, username, password):
        if username == USERNAME and password == PASSWORD:
            return AUTH_SUCCESSFUL
        return AUTH_FAILED

    def check_auth_publickey(self, username, key):
        if username == USERNAME and key == self.good_pub_key: