sftpd: port to non deprecated pkg/sftp error types

see https://github.com/pkg/sftp/pull/304
This commit is contained in:
Nicola Murino
2019-10-14 22:44:57 +02:00
parent 2932dba5cc
commit f98a29a1e0
2 changed files with 43 additions and 43 deletions

View File

@@ -51,25 +51,25 @@ func (c Connection) Fileread(request *sftp.Request) (io.ReaderAt, error) {
updateConnectionActivity(c.ID) updateConnectionActivity(c.ID)
if !c.User.HasPerm(dataprovider.PermDownload) { if !c.User.HasPerm(dataprovider.PermDownload) {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSSHFxPermissionDenied
} }
p, err := c.buildPath(request.Filepath) p, err := c.buildPath(request.Filepath)
if err != nil { if err != nil {
return nil, sftp.ErrSshFxNoSuchFile return nil, sftp.ErrSSHFxNoSuchFile
} }
c.lock.Lock() c.lock.Lock()
defer c.lock.Unlock() defer c.lock.Unlock()
if _, err := os.Stat(p); os.IsNotExist(err) { if _, err := os.Stat(p); os.IsNotExist(err) {
return nil, sftp.ErrSshFxNoSuchFile return nil, sftp.ErrSSHFxNoSuchFile
} }
file, err := os.Open(p) file, err := os.Open(p)
if err != nil { if err != nil {
c.Log(logger.LevelError, logSender, "could not open file %#v for reading: %v", p, err) c.Log(logger.LevelError, logSender, "could not open file %#v for reading: %v", p, err)
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
c.Log(logger.LevelDebug, logSender, "fileread requested for path: %#v", p) c.Log(logger.LevelDebug, logSender, "fileread requested for path: %#v", p)
@@ -98,12 +98,12 @@ func (c Connection) Fileread(request *sftp.Request) (io.ReaderAt, error) {
func (c Connection) Filewrite(request *sftp.Request) (io.WriterAt, error) { func (c Connection) Filewrite(request *sftp.Request) (io.WriterAt, error) {
updateConnectionActivity(c.ID) updateConnectionActivity(c.ID)
if !c.User.HasPerm(dataprovider.PermUpload) { if !c.User.HasPerm(dataprovider.PermUpload) {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSSHFxPermissionDenied
} }
p, err := c.buildPath(request.Filepath) p, err := c.buildPath(request.Filepath)
if err != nil { if err != nil {
return nil, sftp.ErrSshFxNoSuchFile return nil, sftp.ErrSSHFxNoSuchFile
} }
filePath := p filePath := p
@@ -123,17 +123,17 @@ func (c Connection) Filewrite(request *sftp.Request) (io.WriterAt, error) {
if statErr != nil { if statErr != nil {
c.Log(logger.LevelError, logSender, "error performing file stat %#v: %v", p, statErr) c.Log(logger.LevelError, logSender, "error performing file stat %#v: %v", p, statErr)
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
// This happen if we upload a file that has the same name of an existing directory // This happen if we upload a file that has the same name of an existing directory
if stat.IsDir() { if stat.IsDir() {
c.Log(logger.LevelWarn, logSender, "attempted to open a directory for writing to: %#v", p) c.Log(logger.LevelWarn, logSender, "attempted to open a directory for writing to: %#v", p)
return nil, sftp.ErrSshFxOpUnsupported return nil, sftp.ErrSSHFxOpUnsupported
} }
if !c.User.HasPerm(dataprovider.PermOverwrite) { if !c.User.HasPerm(dataprovider.PermOverwrite) {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSSHFxPermissionDenied
} }
return c.handleSFTPUploadToExistingFile(request.Pflags(), p, filePath, stat.Size()) return c.handleSFTPUploadToExistingFile(request.Pflags(), p, filePath, stat.Size())
@@ -146,12 +146,12 @@ func (c Connection) Filecmd(request *sftp.Request) error {
p, err := c.buildPath(request.Filepath) p, err := c.buildPath(request.Filepath)
if err != nil { if err != nil {
return sftp.ErrSshFxNoSuchFile return sftp.ErrSSHFxNoSuchFile
} }
target, err := c.getSFTPCmdTargetPath(request.Target) target, err := c.getSFTPCmdTargetPath(request.Target)
if err != nil { if err != nil {
return sftp.ErrSshFxOpUnsupported return sftp.ErrSSHFxOpUnsupported
} }
c.Log(logger.LevelDebug, logSender, "new cmd, method: %v, sourcePath: %#v, targetPath: %#v", request.Method, c.Log(logger.LevelDebug, logSender, "new cmd, method: %v, sourcePath: %#v, targetPath: %#v", request.Method,
@@ -188,7 +188,7 @@ func (c Connection) Filecmd(request *sftp.Request) error {
return c.handleSFTPRemove(p) return c.handleSFTPRemove(p)
default: default:
return sftp.ErrSshFxOpUnsupported return sftp.ErrSSHFxOpUnsupported
} }
var fileLocation = p var fileLocation = p
@@ -199,7 +199,7 @@ func (c Connection) Filecmd(request *sftp.Request) error {
// we return if we remove a file or a dir so source path or target path always exists here // we return if we remove a file or a dir so source path or target path always exists here
utils.SetPathPermissions(fileLocation, c.User.GetUID(), c.User.GetGID()) utils.SetPathPermissions(fileLocation, c.User.GetUID(), c.User.GetGID())
return sftp.ErrSshFxOk return sftp.ErrSSHFxOk
} }
// Filelist is the handler for SFTP filesystem list calls. This will handle calls to list the contents of // Filelist is the handler for SFTP filesystem list calls. This will handle calls to list the contents of
@@ -208,13 +208,13 @@ func (c Connection) Filelist(request *sftp.Request) (sftp.ListerAt, error) {
updateConnectionActivity(c.ID) updateConnectionActivity(c.ID)
p, err := c.buildPath(request.Filepath) p, err := c.buildPath(request.Filepath)
if err != nil { if err != nil {
return nil, sftp.ErrSshFxNoSuchFile return nil, sftp.ErrSSHFxNoSuchFile
} }
switch request.Method { switch request.Method {
case "List": case "List":
if !c.User.HasPerm(dataprovider.PermListItems) { if !c.User.HasPerm(dataprovider.PermListItems) {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSSHFxPermissionDenied
} }
c.Log(logger.LevelDebug, logSender, "requested list file for dir: %#v", p) c.Log(logger.LevelDebug, logSender, "requested list file for dir: %#v", p)
@@ -222,27 +222,27 @@ func (c Connection) Filelist(request *sftp.Request) (sftp.ListerAt, error) {
files, err := ioutil.ReadDir(p) files, err := ioutil.ReadDir(p)
if err != nil { if err != nil {
c.Log(logger.LevelError, logSender, "error listing directory: %#v", err) c.Log(logger.LevelError, logSender, "error listing directory: %#v", err)
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
return listerAt(files), nil return listerAt(files), nil
case "Stat": case "Stat":
if !c.User.HasPerm(dataprovider.PermListItems) { if !c.User.HasPerm(dataprovider.PermListItems) {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSSHFxPermissionDenied
} }
c.Log(logger.LevelDebug, logSender, "requested stat for file: %#v", p) c.Log(logger.LevelDebug, logSender, "requested stat for file: %#v", p)
s, err := os.Stat(p) s, err := os.Stat(p)
if os.IsNotExist(err) { if os.IsNotExist(err) {
return nil, sftp.ErrSshFxNoSuchFile return nil, sftp.ErrSSHFxNoSuchFile
} else if err != nil { } else if err != nil {
c.Log(logger.LevelError, logSender, "error running STAT on file: %#v", err) c.Log(logger.LevelError, logSender, "error running STAT on file: %#v", err)
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
return listerAt([]os.FileInfo{s}), nil return listerAt([]os.FileInfo{s}), nil
default: default:
return nil, sftp.ErrSshFxOpUnsupported return nil, sftp.ErrSSHFxOpUnsupported
} }
} }
@@ -255,7 +255,7 @@ func (c Connection) getSFTPCmdTargetPath(requestTarget string) (string, error) {
var err error var err error
target, err = c.buildPath(requestTarget) target, err = c.buildPath(requestTarget)
if err != nil { if err != nil {
return target, sftp.ErrSshFxOpUnsupported return target, sftp.ErrSSHFxOpUnsupported
} }
} }
return target, nil return target, nil
@@ -263,11 +263,11 @@ func (c Connection) getSFTPCmdTargetPath(requestTarget string) (string, error) {
func (c Connection) handleSFTPRename(sourcePath string, targetPath string) error { func (c Connection) handleSFTPRename(sourcePath string, targetPath string) error {
if !c.User.HasPerm(dataprovider.PermRename) { if !c.User.HasPerm(dataprovider.PermRename) {
return sftp.ErrSshFxPermissionDenied return sftp.ErrSSHFxPermissionDenied
} }
if err := os.Rename(sourcePath, targetPath); err != nil { if err := os.Rename(sourcePath, targetPath); err != nil {
c.Log(logger.LevelError, logSender, "failed to rename file, source: %#v target: %#v: %v", sourcePath, targetPath, err) c.Log(logger.LevelError, logSender, "failed to rename file, source: %#v target: %#v: %v", sourcePath, targetPath, err)
return sftp.ErrSshFxFailure return sftp.ErrSSHFxFailure
} }
logger.CommandLog(renameLogSender, sourcePath, targetPath, c.User.Username, c.ID, c.protocol) logger.CommandLog(renameLogSender, sourcePath, targetPath, c.User.Username, c.ID, c.protocol)
executeAction(operationRename, c.User.Username, sourcePath, targetPath) executeAction(operationRename, c.User.Username, sourcePath, targetPath)
@@ -276,17 +276,17 @@ func (c Connection) handleSFTPRename(sourcePath string, targetPath string) error
func (c Connection) handleSFTPRmdir(path string) error { func (c Connection) handleSFTPRmdir(path string) error {
if !c.User.HasPerm(dataprovider.PermDelete) { if !c.User.HasPerm(dataprovider.PermDelete) {
return sftp.ErrSshFxPermissionDenied return sftp.ErrSSHFxPermissionDenied
} }
numFiles, size, fileList, err := utils.ScanDirContents(path) numFiles, size, fileList, err := utils.ScanDirContents(path)
if err != nil { if err != nil {
c.Log(logger.LevelError, logSender, "failed to remove directory %#v, scanning error: %v", path, err) c.Log(logger.LevelError, logSender, "failed to remove directory %#v, scanning error: %v", path, err)
return sftp.ErrSshFxFailure return sftp.ErrSSHFxFailure
} }
if err := os.RemoveAll(path); err != nil { if err := os.RemoveAll(path); err != nil {
c.Log(logger.LevelError, logSender, "failed to remove directory %#v: %v", path, err) c.Log(logger.LevelError, logSender, "failed to remove directory %#v: %v", path, err)
return sftp.ErrSshFxFailure return sftp.ErrSSHFxFailure
} }
logger.CommandLog(rmdirLogSender, path, "", c.User.Username, c.ID, c.protocol) logger.CommandLog(rmdirLogSender, path, "", c.User.Username, c.ID, c.protocol)
@@ -294,16 +294,16 @@ func (c Connection) handleSFTPRmdir(path string) error {
for _, p := range fileList { for _, p := range fileList {
executeAction(operationDelete, c.User.Username, p, "") executeAction(operationDelete, c.User.Username, p, "")
} }
return sftp.ErrSshFxOk return sftp.ErrSSHFxOk
} }
func (c Connection) handleSFTPSymlink(sourcePath string, targetPath string) error { func (c Connection) handleSFTPSymlink(sourcePath string, targetPath string) error {
if !c.User.HasPerm(dataprovider.PermCreateSymlinks) { if !c.User.HasPerm(dataprovider.PermCreateSymlinks) {
return sftp.ErrSshFxPermissionDenied return sftp.ErrSSHFxPermissionDenied
} }
if err := os.Symlink(sourcePath, targetPath); err != nil { if err := os.Symlink(sourcePath, targetPath); err != nil {
c.Log(logger.LevelWarn, logSender, "failed to create symlink %#v -> %#v: %v", sourcePath, targetPath, err) c.Log(logger.LevelWarn, logSender, "failed to create symlink %#v -> %#v: %v", sourcePath, targetPath, err)
return sftp.ErrSshFxFailure return sftp.ErrSSHFxFailure
} }
logger.CommandLog(symlinkLogSender, sourcePath, targetPath, c.User.Username, c.ID, c.protocol) logger.CommandLog(symlinkLogSender, sourcePath, targetPath, c.User.Username, c.ID, c.protocol)
@@ -312,12 +312,12 @@ func (c Connection) handleSFTPSymlink(sourcePath string, targetPath string) erro
func (c Connection) handleSFTPMkdir(path string) error { func (c Connection) handleSFTPMkdir(path string) error {
if !c.User.HasPerm(dataprovider.PermCreateDirs) { if !c.User.HasPerm(dataprovider.PermCreateDirs) {
return sftp.ErrSshFxPermissionDenied return sftp.ErrSSHFxPermissionDenied
} }
if err := c.createMissingDirs(filepath.Join(path, "testfile")); err != nil { if err := c.createMissingDirs(filepath.Join(path, "testfile")); err != nil {
c.Log(logger.LevelError, logSender, "error making missing dir for path %#v: %v", path, err) c.Log(logger.LevelError, logSender, "error making missing dir for path %#v: %v", path, err)
return sftp.ErrSshFxFailure return sftp.ErrSSHFxFailure
} }
logger.CommandLog(mkdirLogSender, path, "", c.User.Username, c.ID, c.protocol) logger.CommandLog(mkdirLogSender, path, "", c.User.Username, c.ID, c.protocol)
return nil return nil
@@ -325,7 +325,7 @@ func (c Connection) handleSFTPMkdir(path string) error {
func (c Connection) handleSFTPRemove(path string) error { func (c Connection) handleSFTPRemove(path string) error {
if !c.User.HasPerm(dataprovider.PermDelete) { if !c.User.HasPerm(dataprovider.PermDelete) {
return sftp.ErrSshFxPermissionDenied return sftp.ErrSSHFxPermissionDenied
} }
var size int64 var size int64
@@ -333,12 +333,12 @@ func (c Connection) handleSFTPRemove(path string) error {
var err error var err error
if fi, err = os.Lstat(path); err != nil { if fi, err = os.Lstat(path); err != nil {
c.Log(logger.LevelError, logSender, "failed to remove a file %#v: stat error: %v", path, err) c.Log(logger.LevelError, logSender, "failed to remove a file %#v: stat error: %v", path, err)
return sftp.ErrSshFxFailure return sftp.ErrSSHFxFailure
} }
size = fi.Size() size = fi.Size()
if err := os.Remove(path); err != nil { if err := os.Remove(path); err != nil {
c.Log(logger.LevelError, logSender, "failed to remove a file/symlink %#v: %v", path, err) c.Log(logger.LevelError, logSender, "failed to remove a file/symlink %#v: %v", path, err)
return sftp.ErrSshFxFailure return sftp.ErrSSHFxFailure
} }
logger.CommandLog(removeLogSender, path, "", c.User.Username, c.ID, c.protocol) logger.CommandLog(removeLogSender, path, "", c.User.Username, c.ID, c.protocol)
@@ -347,31 +347,31 @@ func (c Connection) handleSFTPRemove(path string) error {
} }
executeAction(operationDelete, c.User.Username, path, "") executeAction(operationDelete, c.User.Username, path, "")
return sftp.ErrSshFxOk return sftp.ErrSSHFxOk
} }
func (c Connection) handleSFTPUploadToNewFile(requestPath, filePath string) (io.WriterAt, error) { func (c Connection) handleSFTPUploadToNewFile(requestPath, filePath string) (io.WriterAt, error) {
if !c.hasSpace(true) { if !c.hasSpace(true) {
c.Log(logger.LevelInfo, logSender, "denying file write due to space limit") c.Log(logger.LevelInfo, logSender, "denying file write due to space limit")
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
if _, err := os.Stat(filepath.Dir(requestPath)); os.IsNotExist(err) { if _, err := os.Stat(filepath.Dir(requestPath)); os.IsNotExist(err) {
if !c.User.HasPerm(dataprovider.PermCreateDirs) { if !c.User.HasPerm(dataprovider.PermCreateDirs) {
return nil, sftp.ErrSshFxPermissionDenied return nil, sftp.ErrSSHFxPermissionDenied
} }
} }
err := c.createMissingDirs(requestPath) err := c.createMissingDirs(requestPath)
if err != nil { if err != nil {
c.Log(logger.LevelError, logSender, "error making missing dir for path %#v: %v", requestPath, err) c.Log(logger.LevelError, logSender, "error making missing dir for path %#v: %v", requestPath, err)
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
file, err := os.Create(filePath) file, err := os.Create(filePath)
if err != nil { if err != nil {
c.Log(logger.LevelError, logSender, "error creating file %#v: %v", requestPath, err) c.Log(logger.LevelError, logSender, "error creating file %#v: %v", requestPath, err)
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
utils.SetPathPermissions(filePath, c.User.GetUID(), c.User.GetGID()) utils.SetPathPermissions(filePath, c.User.GetUID(), c.User.GetGID())
@@ -401,7 +401,7 @@ func (c Connection) handleSFTPUploadToExistingFile(pflags sftp.FileOpenFlags, re
var err error var err error
if !c.hasSpace(false) { if !c.hasSpace(false) {
c.Log(logger.LevelInfo, logSender, "denying file write due to space limit") c.Log(logger.LevelInfo, logSender, "denying file write due to space limit")
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
minWriteOffset := int64(0) minWriteOffset := int64(0)
@@ -412,14 +412,14 @@ func (c Connection) handleSFTPUploadToExistingFile(pflags sftp.FileOpenFlags, re
if err != nil { if err != nil {
c.Log(logger.LevelError, logSender, "error renaming existing file for atomic upload, source: %#v, dest: %#v, err: %v", c.Log(logger.LevelError, logSender, "error renaming existing file for atomic upload, source: %#v, dest: %#v, err: %v",
requestPath, filePath, err) requestPath, filePath, err)
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
} }
// we use 0666 so the umask is applied // we use 0666 so the umask is applied
file, err := os.OpenFile(filePath, osFlags, 0666) file, err := os.OpenFile(filePath, osFlags, 0666)
if err != nil { if err != nil {
c.Log(logger.LevelError, logSender, "error opening existing file, flags: %v, source: %#v, err: %v", pflags, filePath, err) c.Log(logger.LevelError, logSender, "error opening existing file, flags: %v, source: %#v, err: %v", pflags, filePath, err)
return nil, sftp.ErrSshFxFailure return nil, sftp.ErrSSHFxFailure
} }
if pflags.Append && osFlags&os.O_TRUNC == 0 { if pflags.Append && osFlags&os.O_TRUNC == 0 {

View File

@@ -191,7 +191,7 @@ func TestSFTPCmdTargetPath(t *testing.T) {
User: u, User: u,
} }
_, err := connection.getSFTPCmdTargetPath("invalid_path") _, err := connection.getSFTPCmdTargetPath("invalid_path")
if err != sftp.ErrSshFxOpUnsupported { if err != sftp.ErrSSHFxOpUnsupported {
t.Errorf("getSFTPCmdTargetPath must fal with the expected error: %v", err) t.Errorf("getSFTPCmdTargetPath must fal with the expected error: %v", err)
} }
} }