|
| 1 | +# Copyright (C) 2003-2009 Robey Pointer <robeypointer@gmail.com> |
| 2 | +# |
| 3 | +# This file is part of paramiko. |
| 4 | +# |
| 5 | +# Paramiko is free software; you can redistribute it and/or modify it under the |
| 6 | +# terms of the GNU Lesser General Public License as published by the Free |
| 7 | +# Software Foundation; either version 2.1 of the License, or (at your option) |
| 8 | +# any later version. |
| 9 | +# |
| 10 | +# Paramiko is distrubuted in the hope that it will be useful, but WITHOUT ANY |
| 11 | +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
| 12 | +# A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
| 13 | +# details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public License |
| 16 | +# along with Paramiko; if not, write to the Free Software Foundation, Inc., |
| 17 | +# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 18 | + |
| 19 | +""" |
| 20 | +A stub SFTP server for loopback SFTP testing. |
| 21 | +""" |
| 22 | + |
| 23 | +import os |
| 24 | +from paramiko import ServerInterface, SFTPServerInterface, SFTPServer, SFTPAttributes, \ |
| 25 | + SFTPHandle, SFTP_OK, AUTH_SUCCESSFUL, OPEN_SUCCEEDED |
| 26 | + |
| 27 | + |
| 28 | +class StubServer (ServerInterface): |
| 29 | + def check_auth_password(self, username, password): |
| 30 | + # all are allowed |
| 31 | + return AUTH_SUCCESSFUL |
| 32 | + |
| 33 | + def check_channel_request(self, kind, chanid): |
| 34 | + return OPEN_SUCCEEDED |
| 35 | + |
| 36 | + |
| 37 | +class StubSFTPHandle (SFTPHandle): |
| 38 | + def stat(self): |
| 39 | + try: |
| 40 | + return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno())) |
| 41 | + except OSError as e: |
| 42 | + return SFTPServer.convert_errno(e.errno) |
| 43 | + |
| 44 | + def chattr(self, attr): |
| 45 | + # python doesn't have equivalents to fchown or fchmod, so we have to |
| 46 | + # use the stored filename |
| 47 | + try: |
| 48 | + SFTPServer.set_file_attr(self.filename, attr) |
| 49 | + return SFTP_OK |
| 50 | + except OSError as e: |
| 51 | + return SFTPServer.convert_errno(e.errno) |
| 52 | + |
| 53 | + |
| 54 | +class StubSFTPServer (SFTPServerInterface): |
| 55 | + # assume current folder is a fine root |
| 56 | + # (the tests always create and eventualy delete a subfolder, so there shouldn't be any mess) |
| 57 | + ROOT = os.getcwd() |
| 58 | + |
| 59 | + def _realpath(self, path): |
| 60 | + return self.ROOT + self.canonicalize(path) |
| 61 | + |
| 62 | + def list_folder(self, path): |
| 63 | + path = self._realpath(path) |
| 64 | + try: |
| 65 | + out = [ ] |
| 66 | + flist = os.listdir(path) |
| 67 | + for fname in flist: |
| 68 | + attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname))) |
| 69 | + attr.filename = fname |
| 70 | + out.append(attr) |
| 71 | + return out |
| 72 | + except OSError as e: |
| 73 | + return SFTPServer.convert_errno(e.errno) |
| 74 | + |
| 75 | + def stat(self, path): |
| 76 | + path = self._realpath(path) |
| 77 | + try: |
| 78 | + return SFTPAttributes.from_stat(os.stat(path)) |
| 79 | + except OSError as e: |
| 80 | + return SFTPServer.convert_errno(e.errno) |
| 81 | + |
| 82 | + def lstat(self, path): |
| 83 | + path = self._realpath(path) |
| 84 | + try: |
| 85 | + return SFTPAttributes.from_stat(os.lstat(path)) |
| 86 | + except OSError as e: |
| 87 | + return SFTPServer.convert_errno(e.errno) |
| 88 | + |
| 89 | + def open(self, path, flags, attr): |
| 90 | + path = self._realpath(path) |
| 91 | + try: |
| 92 | + binary_flag = getattr(os, 'O_BINARY', 0) |
| 93 | + flags |= binary_flag |
| 94 | + mode = getattr(attr, 'st_mode', None) |
| 95 | + if mode is not None: |
| 96 | + fd = os.open(path, flags, mode) |
| 97 | + else: |
| 98 | + # os.open() defaults to 0777 which is |
| 99 | + # an odd default mode for files |
| 100 | + fd = os.open(path, flags, 0o666) |
| 101 | + except OSError as e: |
| 102 | + return SFTPServer.convert_errno(e.errno) |
| 103 | + if (flags & os.O_CREAT) and (attr is not None): |
| 104 | + attr._flags &= ~attr.FLAG_PERMISSIONS |
| 105 | + SFTPServer.set_file_attr(path, attr) |
| 106 | + if flags & os.O_WRONLY: |
| 107 | + if flags & os.O_APPEND: |
| 108 | + fstr = 'ab' |
| 109 | + else: |
| 110 | + fstr = 'wb' |
| 111 | + elif flags & os.O_RDWR: |
| 112 | + if flags & os.O_APPEND: |
| 113 | + fstr = 'a+b' |
| 114 | + else: |
| 115 | + fstr = 'r+b' |
| 116 | + else: |
| 117 | + # O_RDONLY (== 0) |
| 118 | + fstr = 'rb' |
| 119 | + try: |
| 120 | + f = os.fdopen(fd, fstr) |
| 121 | + except OSError as e: |
| 122 | + return SFTPServer.convert_errno(e.errno) |
| 123 | + fobj = StubSFTPHandle(flags) |
| 124 | + fobj.filename = path |
| 125 | + fobj.readfile = f |
| 126 | + fobj.writefile = f |
| 127 | + return fobj |
| 128 | + |
| 129 | + def remove(self, path): |
| 130 | + path = self._realpath(path) |
| 131 | + try: |
| 132 | + os.remove(path) |
| 133 | + except OSError as e: |
| 134 | + return SFTPServer.convert_errno(e.errno) |
| 135 | + return SFTP_OK |
| 136 | + |
| 137 | + def rename(self, oldpath, newpath): |
| 138 | + oldpath = self._realpath(oldpath) |
| 139 | + newpath = self._realpath(newpath) |
| 140 | + try: |
| 141 | + os.rename(oldpath, newpath) |
| 142 | + except OSError as e: |
| 143 | + return SFTPServer.convert_errno(e.errno) |
| 144 | + return SFTP_OK |
| 145 | + |
| 146 | + def mkdir(self, path, attr): |
| 147 | + path = self._realpath(path) |
| 148 | + try: |
| 149 | + os.mkdir(path) |
| 150 | + if attr is not None: |
| 151 | + SFTPServer.set_file_attr(path, attr) |
| 152 | + except OSError as e: |
| 153 | + return SFTPServer.convert_errno(e.errno) |
| 154 | + return SFTP_OK |
| 155 | + |
| 156 | + def rmdir(self, path): |
| 157 | + path = self._realpath(path) |
| 158 | + try: |
| 159 | + os.rmdir(path) |
| 160 | + except OSError as e: |
| 161 | + return SFTPServer.convert_errno(e.errno) |
| 162 | + return SFTP_OK |
| 163 | + |
| 164 | + def chattr(self, path, attr): |
| 165 | + path = self._realpath(path) |
| 166 | + try: |
| 167 | + SFTPServer.set_file_attr(path, attr) |
| 168 | + except OSError as e: |
| 169 | + return SFTPServer.convert_errno(e.errno) |
| 170 | + return SFTP_OK |
| 171 | + |
| 172 | + def symlink(self, target_path, path): |
| 173 | + path = self._realpath(path) |
| 174 | + if (len(target_path) > 0) and (target_path[0] == '/'): |
| 175 | + # absolute symlink |
| 176 | + target_path = os.path.join(self.ROOT, target_path[1:]) |
| 177 | + if target_path[:2] == '//': |
| 178 | + # bug in os.path.join |
| 179 | + target_path = target_path[1:] |
| 180 | + else: |
| 181 | + # compute relative to path |
| 182 | + abspath = os.path.join(os.path.dirname(path), target_path) |
| 183 | + if abspath[:len(self.ROOT)] != self.ROOT: |
| 184 | + # this symlink isn't going to work anyway -- just break it immediately |
| 185 | + target_path = '<error>' |
| 186 | + try: |
| 187 | + os.symlink(target_path, path) |
| 188 | + except OSError as e: |
| 189 | + return SFTPServer.convert_errno(e.errno) |
| 190 | + return SFTP_OK |
| 191 | + |
| 192 | + def readlink(self, path): |
| 193 | + path = self._realpath(path) |
| 194 | + try: |
| 195 | + symlink = os.readlink(path) |
| 196 | + except OSError as e: |
| 197 | + return SFTPServer.convert_errno(e.errno) |
| 198 | + # if it's absolute, remove the root |
| 199 | + if os.path.isabs(symlink): |
| 200 | + if symlink[:len(self.ROOT)] == self.ROOT: |
| 201 | + symlink = symlink[len(self.ROOT):] |
| 202 | + if (len(symlink) == 0) or (symlink[0] != '/'): |
| 203 | + symlink = '/' + symlink |
| 204 | + else: |
| 205 | + symlink = '<error>' |
| 206 | + return symlink |
0 commit comments