aboutsummaryrefslogtreecommitdiff
path: root/weed/sftpd/sftp_server.go
diff options
context:
space:
mode:
Diffstat (limited to 'weed/sftpd/sftp_server.go')
-rw-r--r--weed/sftpd/sftp_server.go63
1 files changed, 55 insertions, 8 deletions
diff --git a/weed/sftpd/sftp_server.go b/weed/sftpd/sftp_server.go
index be3af6fd1..1399b64c7 100644
--- a/weed/sftpd/sftp_server.go
+++ b/weed/sftpd/sftp_server.go
@@ -2,12 +2,18 @@
package sftpd
import (
+ "context"
+ "fmt"
"io"
+ "os"
+ "time"
"github.com/pkg/sftp"
+ "github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb"
- "github.com/seaweedfs/seaweedfs/weed/sftpd/auth"
+ filer_pb "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
"github.com/seaweedfs/seaweedfs/weed/sftpd/user"
+ "github.com/seaweedfs/seaweedfs/weed/util"
"google.golang.org/grpc"
)
@@ -17,16 +23,10 @@ type SftpServer struct {
dataCenter string
filerGroup string
user *user.User
- authManager *auth.Manager
}
// NewSftpServer constructs the server.
func NewSftpServer(filerAddr pb.ServerAddress, grpcDialOption grpc.DialOption, dataCenter, filerGroup string, user *user.User) SftpServer {
- // Create a file system helper for the auth manager
- fsHelper := NewFileSystemHelper(filerAddr, grpcDialOption, dataCenter, filerGroup)
-
- // Create an auth manager for permission checking
- authManager := auth.NewManager(nil, fsHelper, []string{})
return SftpServer{
filerAddr: filerAddr,
@@ -34,7 +34,6 @@ func NewSftpServer(filerAddr pb.ServerAddress, grpcDialOption grpc.DialOption, d
dataCenter: dataCenter,
filerGroup: filerGroup,
user: user,
- authManager: authManager,
}
}
@@ -57,3 +56,51 @@ func (fs *SftpServer) Filecmd(req *sftp.Request) error {
func (fs *SftpServer) Filelist(req *sftp.Request) (sftp.ListerAt, error) {
return fs.listDir(req)
}
+
+// EnsureHomeDirectory creates the user's home directory if it doesn't exist
+func (fs *SftpServer) EnsureHomeDirectory() error {
+ if fs.user.HomeDir == "" {
+ return fmt.Errorf("user has no home directory configured")
+ }
+
+ glog.V(0).Infof("Ensuring home directory exists for user %s: %s", fs.user.Username, fs.user.HomeDir)
+
+ // Check if home directory already exists
+ entry, err := fs.getEntry(fs.user.HomeDir)
+ if err == nil && entry != nil {
+ // Directory exists, just ensure proper ownership
+ if entry.Attributes.Uid != fs.user.Uid || entry.Attributes.Gid != fs.user.Gid {
+ dir, _ := util.FullPath(fs.user.HomeDir).DirAndName()
+ entry.Attributes.Uid = fs.user.Uid
+ entry.Attributes.Gid = fs.user.Gid
+ return fs.updateEntry(dir, entry)
+ }
+ return nil
+ }
+
+ // Skip permission check for home directory creation
+ // This is a special case where we want to create the directory regardless
+ dir, name := util.FullPath(fs.user.HomeDir).DirAndName()
+
+ // Create the directory with proper permissions using filer_pb.Mkdir
+ err = filer_pb.Mkdir(context.Background(), fs, dir, name, func(entry *filer_pb.Entry) {
+ mode := uint32(0700 | os.ModeDir) // Default to private permissions for home dirs
+ entry.Attributes.FileMode = mode
+ entry.Attributes.Uid = fs.user.Uid
+ entry.Attributes.Gid = fs.user.Gid
+ now := time.Now().Unix()
+ entry.Attributes.Crtime = now
+ entry.Attributes.Mtime = now
+ if entry.Extended == nil {
+ entry.Extended = make(map[string][]byte)
+ }
+ entry.Extended["creator"] = []byte(fs.user.Username)
+ })
+
+ if err != nil {
+ return fmt.Errorf("failed to create home directory: %v", err)
+ }
+
+ glog.V(0).Infof("Successfully created home directory for user %s: %s", fs.user.Username, fs.user.HomeDir)
+ return nil
+}