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 chattr(self, attr): # python doesn't have equivalents to fchown or fchmod, so we have to # use the stored filename mutter('Changing permissions on %s to %s', self.filename, attr) try: SFTPServer.set_file_attr(self.filename, attr) except OSError, e: return SFTPServer.convert_errno(e.errno)
def chattr(self, attr): # python doesn't have equivalents to fchown or fchmod, so we have to # use the stored filename try: SFTPServer.set_file_attr(self.filename, attr) return SFTP_OK except OSError as e: return SFTPServer.convert_errno(e.errno)
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, e: return SFTPServer.convert_errno(e.errno)
def chattr(self, attr): if not self.user.is_sysop: return SFTP_PERMISSION_DENIED self.log.debug('chattr ({0!r})'.format(attr)) # python doesn't have equivalents to fchown or fchmod, so we have to # use the stored filename try: SFTPServer.set_file_attr(self.filename, attr) return SFTP_OK except OSError as err: return SFTPServer.convert_errno(err.errno)
def chattr(self, path, attr): """ This method changes the attributes of a given path path - the path attr - file attributes to be set return SFTP_OK or error """ path = self._realpath(path) try: SFTPServer.set_file_attr(path, attr) except OSError, e: return SFTPServer.convert_errno(e.errno)
def mkdir(self, path, attr): if not self.user.is_sysop or flagged_dirname in path: return SFTP_PERMISSION_DENIED self.log.debug('mkdir({0!r}, {1!r})'.format(path, attr)) path = self._realpath(path) try: os.mkdir(path) if attr is not None: SFTPServer.set_file_attr(path, attr) except OSError as err: return SFTPServer.convert_errno(err.errno) return SFTP_OK
def chattr(self, attr): """ This method performs the stat attributes for the given path path - file/folder path return path SFTPAttributes or error """ # python doesn't have equivalents to fchown or fchmod, so we have to # use the stored filename try: SFTPServer.set_file_attr(self.filename, attr) return SFTP_OK except OSError, e: return SFTPServer.convert_errno(e.errno)
def mkdir(self, path, attr): """ This method creates a dir path with passed attributes path - folder path to be created attr - attributes return SFTP_OK or error """ path = self._realpath(path) try: os.mkdir(path) if attr is not None: SFTPServer.set_file_attr(path, attr) except OSError, e: return SFTPServer.convert_errno(e.errno)
def mkdir(self, path, attr): path = self._realpath(path) try: # Using getattr() in case st_mode is None or 0 # both evaluate to False if getattr(attr, 'st_mode', None): os.mkdir(path, attr.st_mode) else: os.mkdir(path) if attr is not None: attr._flags &= ~attr.FLAG_PERMISSIONS SFTPServer.set_file_attr(path, attr) except OSError, e: return SFTPServer.convert_errno(e.errno)
def chattr(self, path, attr): if self._is_uploaddir(path): return SFTP_PERMISSION_DENIED elif not self.user.is_sysop or \ uploads_dirname in path or \ flagged_dirname in path: return SFTP_PERMISSION_DENIED self.log.debug('chattr({0!r})'.format(path)) path = self._realpath(path) try: SFTPServer.set_file_attr(path, attr) except OSError as err: return SFTPServer.convert_errno(err.errno) return SFTP_OK
def stat(self): """ Stat the file descriptor. """ self.log.debug('stat') try: return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno())) except OSError as err: return SFTPServer.convert_errno(err.errno)
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 lstat(self, path): path = self._realpath(path) try: return SFTPAttributes.from_stat(os.lstat(path)) except OSError: e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno)
def list_folder(self, path): """ List contents of a folder. """ self.log.debug('list_folder({0!r})'.format(path)) rpath = self._realpath(path) if not self.user.is_sysop and self._is_uploaddir(path): return [] try: out = [] if path == u'/': out.append(self._dummy_dir_stat()) elif flagged_dirname in path: self.flagged = self.user.get('flaggedfiles', set()) for fname in self.flagged: rname = fname attr = SFTPAttributes.from_stat(os.stat(rname)) attr.filename = fname[fname.rindex('/') + 1:] out.append(attr) return out flist = os.listdir(rpath) for fname in flist: attr = SFTPAttributes.from_stat( os.stat(os.path.join(rpath, fname))) attr.filename = fname out.append(attr) return out except OSError as err: return SFTPServer.convert_errno(err.errno)
def rename(self, oldpath, newpath): oldpath = self._realpath(oldpath) newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) except OSError, e: return SFTPServer.convert_errno(e.errno)
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 rmdir(self, path): path = self._realpath(path) try: os.rmdir(path) except OSError: e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK
def stat(self): """ This method performs the stat attributes return path SFTPAttributes or error """ try: return SFTPAttributes.from_stat(os.fstat(self.readfile.fileno())) except OSError, e: return SFTPServer.convert_errno(e.errno)
def chattr(self, attr): self.opt.o("sftpfile", 'chattr', self.file_name) try: self.remote_file.chattr(attr) except IOError as e: return SFTPServer.convert_errno(e.errno) else: return SFTP_OK
def rename(self, oldpath, newpath): oldpath = self._realpath(oldpath) newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) except OSError: e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) return SFTP_OK
def open(self, path, flags, attr): """ Up/download the given path. """ self.log.debug('lstat({0!r}, {1!r}, {2!r})' .format(path, flags, attr)) path = self._realpath(path) if (flags & os.O_CREAT and (uploads_dirname not in path and not self.user.is_sysop) or (uploads_dirname in path and os.path.exists(path))): return SFTP_PERMISSION_DENIED try: binary_flag = getattr(os, 'O_BINARY', 0) flags |= binary_flag filedesc = os.open(path, flags, self.mode) except OSError as err: return SFTPServer.convert_errno(err.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: openfile = os.fdopen(filedesc, fstr) except OSError as err: return SFTPServer.convert_errno(err.errno) fobj = X84SFTPHandle(flags, user=self.user) fobj.filename = path fobj.readfile = openfile fobj.writefile = openfile if path in self.flagged: self.flagged.remove(path) self.user['flaggedfiles'] = self.flagged return fobj
def rmdir(self, path): if not self.user.is_sysop or flagged_dirname in path: return SFTP_PERMISSION_DENIED self.log.debug('rmdir({0!r})'.format(path)) path = self._realpath(path) try: os.rmdir(path) except OSError as err: return SFTPServer.convert_errno(err.errno) return SFTP_OK
def open(self, path, flags, attr): """ Up/download the given path. """ self.log.debug('lstat({0!r}, {1!r}, {2!r})'.format(path, flags, attr)) path = self._realpath(path) if (flags & os.O_CREAT and (uploads_dirname not in path and not self.user.is_sysop) or (uploads_dirname in path and os.path.exists(path))): return SFTP_PERMISSION_DENIED try: binary_flag = getattr(os, 'O_BINARY', 0) flags |= binary_flag filedesc = os.open(path, flags, self.mode) except OSError as err: return SFTPServer.convert_errno(err.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: openfile = os.fdopen(filedesc, fstr) except OSError as err: return SFTPServer.convert_errno(err.errno) fobj = X84SFTPHandle(flags, user=self.user) fobj.filename = path fobj.readfile = openfile fobj.writefile = openfile if path in self.flagged: self.flagged.remove(path) self.user['flaggedfiles'] = self.flagged return fobj
def remove(self, path): # if not Share.PERMISSION_DELETE in self._get_bioshare_path_permissions(path): # return PermissionDenied() real_path = self._realpath(path) try: os.remove(real_path) self._path_modified(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) if os.path.exists(newpath): return SFTP_FAILURE try: os.rename(oldpath, newpath) except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK
def remove(self, path): """ Delete the given path. """ if not self.user.is_sysop or flagged_dirname in path: return SFTP_PERMISSION_DENIED self.log.debug('remove({0!r})'.format(path)) path = self._realpath(path) try: os.remove(path) except OSError as err: return SFTPServer.convert_errno(err.errno) return SFTP_OK
def rename(self, oldpath, newpath): if not self.user.is_sysop or flagged_dirname in path: return SFTP_PERMISSION_DENIED self.log.debug('rename({0!r}, {1!r})'.format(oldpath, newpath)) oldpath = self._realpath(oldpath) newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) except OSError as err: return SFTPServer.convert_errno(err.errno) return SFTP_OK
def lstat(self, path): """ This method performs the lstat attributes for the given path path - file/folder path return lstat SFTPAttributes or error """ path = self._realpath(path) try: return SFTPAttributes.from_stat(os.lstat(path)) except OSError, e: return SFTPServer.convert_errno(e.errno)
def remove(self, path): """ This method deletes the given path to file path - file path return SFTP_OK or error """ path = self._realpath(path) try: os.remove(path) except OSError, e: return SFTPServer.convert_errno(e.errno)
def rmdir(self, path): """ Remove a directory. """ if not self.user.is_sysop or flagged_dirname in path: return SFTP_PERMISSION_DENIED self.log.debug('rmdir({0!r})'.format(path)) path = self._realpath(path) try: os.rmdir(path) except OSError as err: return SFTPServer.convert_errno(err.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 rmdir(self, path): """ This method deletes the given folder path - folder path to be deleted return SFTP_OK or error """ path = self._realpath(path) try: os.rmdir(path) except OSError, e: return SFTPServer.convert_errno(e.errno)
def readlink(self, path): """ This method reads symbolic links. path - path to be read return SFTP_OK or error """ path = self._realpath(path) try: symlink = os.readlink(path) except OSError, e: return SFTPServer.convert_errno(e.errno)
def read(self, offset, length): self.opt.o("sftpfile", 'read', self.file_name) if not self.remote_file.readable(): return SFTP_OP_UNSUPPORTED try: self.remote_file.seek(offset) data = self.remote_file.read(length) return data except IOError as e: return SFTPServer.convert_errno(e.errno)
def wrapper(*args, **kwargs): try: value = fn(*args, **kwargs) except (OSError, IOError) as e: return SFTPServer.convert_errno(e.errno) except PermissionDenied: return paramiko.SFTP_PERMISSION_DENIED if value is None: return paramiko.SFTP_OK else: return value
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, 1638) # 0666 except OSError: e = sys.exc_info()[1] 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: e = sys.exc_info()[1] return SFTPServer.convert_errno(e.errno) fobj = StubSFTPHandle(flags) fobj.filename = path fobj.readfile = f fobj.writefile = f return fobj
def list_folder(self, path): path = self._realpath(path) try: out = [ ] flist = os.listdir(path) for fname in flist: attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname))) attr.filename = fname out.append(attr) return out except OSError, e: return SFTPServer.convert_errno(e.errno)
def open(self, path, flags, attr): path = self._realpath(path) try: flags |= getattr(os, 'O_BINARY', 0) if getattr(attr, 'st_mode', None): fd = os.open(path, flags, attr.st_mode) else: # os.open() defaults to 0777 which is # an odd default mode for files fd = os.open(path, flags, 0666) except OSError, e: return SFTPServer.convert_errno(e.errno)
def list_folder(self, path): path = self._realpath(path) try: out = [] flist = os.listdir(path) for fname in flist: attr = SFTPAttributes.from_stat(os.stat(os.path.join(path, fname))) attr.filename = fname out.append(attr) return out except OSError as e: return SFTPServer.convert_errno(e.errno)
def lstat(self, path): try: if path == "/": return self._dir_attr() else: parent = os.path.split(path)[0] if parent == '/': return self._dir_attr() else: path = self._realpath(path) return SFTPAttributes.from_stat(os.lstat(path)) except OSError as e: return SFTPServer.convert_errno(e.errno)
def rename(self, oldpath, newpath): """ This method renames oldpath into newpath oldpath - old file path newpath - new file path return SFTP_OK or error """ oldpath = self._realpath(oldpath) newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) except OSError, e: return SFTPServer.convert_errno(e.errno)
def rename(self, oldpath, newpath): """ Rename the ``oldpath`` to ``newpath``. """ if not self.user.is_sysop or flagged_dirname in oldpath or \ flagged_dirname in newpath: return SFTP_PERMISSION_DENIED self.log.debug('rename({0!r}, {1!r})'.format(oldpath, newpath)) oldpath = self._realpath(oldpath) newpath = self._realpath(newpath) try: os.rename(oldpath, newpath) except OSError as err: return SFTPServer.convert_errno(err.errno) return SFTP_OK
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, 0o666) 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 stat(self, path): try: # If the root path send a simple directory attribute object if path == "/": return self._dir_attr() else: parent = os.path.split(path)[0] if parent == '/': return self._dir_attr() else: path = self._realpath(path) return SFTPAttributes.from_stat(os.stat(path)) except OSError as e: return SFTPServer.convert_errno(e.errno)
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
def write(self, offset, data): self.opt.o("sftpfile", 'write', self.file_name) if not self.remote_file.writable(): return SFTP_OP_UNSUPPORTED try: self.remote_file.seek(offset) self.remote_file.write(data) self.remote_file.flush() self.save_file.seek(offset) self.save_file.write(data) self.save_file.flush() except IOError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK
def readlink(self, path): """ Read symbolic link. """ self.log.debug('readlink({0!r})'.format(path)) path = self._realpath(path) try: symlink = os.readlink(path) except OSError as err: return SFTPServer.convert_errno(err.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
def lstat(self, path): """ Lstat a given path. """ self.log.debug('lstat({0!r})'.format(path)) if path.endswith(flagged_dirname): return self._dummy_dir_stat() elif path.find(flagged_dirname) > -1: for fname in self.flagged: fstripped = fname[fname.rindex(os.path.sep) + 1:] pstripped = path[path.rindex('/') + 1:] if fstripped == pstripped: self.log.debug('file is actually {0}'.format(fname)) return SFTPAttributes.from_stat(fname) path = self._realpath(path) try: return SFTPAttributes.from_stat(os.lstat(path)) except OSError as err: return SFTPServer.convert_errno(err.errno)
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:]) 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: target_path = '<error>' try: os.symlink(target_path, path) except OSError, e: return SFTPServer.convert_errno(e.errno)
def list_folder(self, path): """ This method returns the list folder given a path path - path to folder return a list of folders """ path = self._realpath(path) try: out = [] flist = os.listdir(path) for fname in flist: attr = SFTPAttributes.from_stat( os.stat(os.path.join(path, fname))) attr.filename = fname out.append(attr) return out except OSError, e: return SFTPServer.convert_errno(e.errno)
def list_folder(self, path): path = self._realpath(path) try: out = [] # TODO: win32 incorrectly lists paths with non-ascii if path is not # unicode. However on Linux the server should only deal with # bytestreams and posix.listdir does the right thing if sys.platform == 'win32': flist = [f.encode('utf8') for f in os.listdir(path)] else: flist = os.listdir(path) for fname in flist: attr = SFTPAttributes.from_stat(os.stat(pathjoin(path, fname))) attr.filename = fname out.append(attr) return out except OSError, e: return SFTPServer.convert_errno(e.errno)
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:]) 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: # this symlink isn't going to work anyway -- just break it immediately target_path = '<error>' try: os.symlink(target_path, path) except OSError as e: return SFTPServer.convert_errno(e.errno) return SFTP_OK
def chattr(self, path, attr): self.opt.o("sftpserver", 'chattr', path) try: if attr._flags & attr.FLAG_PERMISSIONS: self.docker_client.chmod(path, attr.st_mode) if attr._flags & attr.FLAG_UIDGID: self.docker_client.chown(path, attr.st_uid, attr.st_gid) if attr._flags & attr.FLAG_AMTIME: self.docker_client.utime(path, (attr.st_atime, attr.st_mtime)) if attr._flags & attr.FLAG_SIZE: self.docker_client.truncate(path, attr.st_size) except IOError as e: return SFTPServer.convert_errno(e.errno) else: return SFTP_OK