Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Commit

Permalink
Fix issues when writing files as the wrong group, whoops
Browse files Browse the repository at this point in the history
  • Loading branch information
DaneEveritt committed Nov 10, 2018
1 parent 8394fef commit aaadc5b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
31 changes: 26 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,27 @@ package main
import (
"errors"
"flag"
"fmt"
"github.com/buger/jsonparser"
"github.com/patrickmn/go-cache"
"github.com/pterodactyl/sftp-server/src/logger"
"github.com/pterodactyl/sftp-server/src/server"
"go.uber.org/zap"
"io/ioutil"
"os"
"os/user"
"path"
"runtime"
"strconv"
"time"
)

func main() {
if runtime.GOOS != "linux" {
fmt.Printf("This operating system (%s) is not supported.\n", runtime.GOOS)
os.Exit(1)
}

var (
configLocation string
bindPort int
Expand All @@ -41,18 +50,30 @@ func main() {
logger.Get().Fatalw("could not read configuration", zap.Error(err))
}

u, err := jsonparser.GetInt(config, "docker", "container", "user")
username, err := jsonparser.GetString(config, "docker", "container", "username")
if err != nil {
logger.Get().Fatalw("could not locate SFTP base user", zap.Error(err))
logger.Get().Debugw("could not find sftp user definition, falling back to \"pterodactyl\"", zap.Error(err))
username = "pterodactyl"
}

logger.Get().Infow("using system daemon user", zap.String("username", username))

u, err := user.Lookup(username)
if err != nil {
logger.Get().Fatalw("failed to lookup sftp user", zap.Error(err))
return
}

c := cache.New(5*time.Minute, 10*time.Minute)
uid, _ := strconv.Atoi(u.Uid)
gid, _ := strconv.Atoi(u.Gid)

var s = server.Configuration{
Data: config,
Cache: c,
User: int(u),
Cache: cache.New(5*time.Minute, 10*time.Minute),
User: server.SftpUser{
Uid: uid,
Gid: gid,
},
Settings: server.Settings{
BasePath: path.Dir(configLocation),
ReadOnly: readOnlyMode,
Expand Down
9 changes: 5 additions & 4 deletions src/server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type FileSystem struct {
Permissions []string
ReadOnly bool
DisableDiskCheck bool
User int
User SftpUser
Cache *cache.Cache
lock sync.Mutex
}
Expand Down Expand Up @@ -72,6 +72,7 @@ func (fs FileSystem) Filewrite(request *sftp.Request) (io.WriterAt, error) {
// If the user doesn't have enough space left on the server it should respond with an
// error since we won't be letting them write this file to the disk.
if !fs.hasSpace() {
logger.Get().Infow("denying file write due to space limit", zap.String("server", fs.UUID))
return nil, sftp.ErrSshFxFailure
}

Expand Down Expand Up @@ -106,7 +107,7 @@ func (fs FileSystem) Filewrite(request *sftp.Request) (io.WriterAt, error) {

// Not failing here is intentional. We still made the file, it is just owned incorrectly
// and will likely cause some issues.
if err := os.Chown(p, fs.User, fs.User); err != nil {
if err := os.Chown(p, fs.User.Uid, fs.User.Gid); err != nil {
logger.Get().Warnw("error chowning file", zap.String("file", p), zap.Error(err))
}

Expand Down Expand Up @@ -137,7 +138,7 @@ func (fs FileSystem) Filewrite(request *sftp.Request) (io.WriterAt, error) {

// Not failing here is intentional. We still made the file, it is just owned incorrectly
// and will likely cause some issues.
if err := os.Chown(p, fs.User, fs.User); err != nil {
if err := os.Chown(p, fs.User.Uid, fs.User.Gid); err != nil {
logger.Get().Warnw("error chowning file", zap.String("file", p), zap.Error(err))
}

Expand Down Expand Up @@ -248,7 +249,7 @@ func (fs FileSystem) Filecmd(request *sftp.Request) error {

// Not failing here is intentional. We still made the file, it is just owned incorrectly
// and will likely cause some issues.
if err := os.Chown(fileLocation, fs.User, fs.User); err != nil {
if err := os.Chown(fileLocation, fs.User.Uid, fs.User.Gid); err != nil {
logger.Get().Warnw("error chowning file", zap.String("file", fileLocation), zap.Error(err))
}

Expand Down
7 changes: 6 additions & 1 deletion src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ type Settings struct {
DisableDiskCheck bool
}

type SftpUser struct {
Uid int
Gid int
}

type Configuration struct {
Data []byte
Cache *cache.Cache
Settings Settings
User int
User SftpUser
}

type AuthenticationResponse struct {
Expand Down

0 comments on commit aaadc5b

Please sign in to comment.