mirror of
https://github.com/drakkan/sftpgo.git
synced 2025-12-07 23:00:55 +03:00
add support for a start directory
Fixes #705 Signed-off-by: Nicola Murino <nicola.murino@gmail.com>
This commit is contained in:
@@ -597,6 +597,68 @@ func TestBasicFTPHandling(t *testing.T) {
|
||||
50*time.Millisecond)
|
||||
}
|
||||
|
||||
func TestStartDirectory(t *testing.T) {
|
||||
startDir := "/start/dir"
|
||||
u := getTestUser()
|
||||
u.Filters.StartDirectory = startDir
|
||||
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
||||
assert.NoError(t, err)
|
||||
u = getTestSFTPUser()
|
||||
u.Filters.StartDirectory = startDir
|
||||
sftpUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
||||
assert.NoError(t, err)
|
||||
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
||||
client, err := getFTPClient(user, true, nil)
|
||||
if assert.NoError(t, err) {
|
||||
currentDir, err := client.CurrentDir()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, startDir, currentDir)
|
||||
|
||||
testFilePath := filepath.Join(homeBasePath, testFileName)
|
||||
testFileSize := int64(65535)
|
||||
err = createTestFile(testFilePath, testFileSize)
|
||||
assert.NoError(t, err)
|
||||
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
||||
assert.NoError(t, err)
|
||||
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
||||
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
||||
assert.NoError(t, err)
|
||||
entries, err := client.List(".")
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, entries, 3)
|
||||
|
||||
entries, err = client.List("/")
|
||||
assert.NoError(t, err)
|
||||
assert.Len(t, entries, 2)
|
||||
|
||||
err = client.ChangeDirToParent()
|
||||
assert.NoError(t, err)
|
||||
currentDir, err = client.CurrentDir()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, path.Dir(startDir), currentDir)
|
||||
err = client.ChangeDirToParent()
|
||||
assert.NoError(t, err)
|
||||
currentDir, err = client.CurrentDir()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "/", currentDir)
|
||||
|
||||
err = os.Remove(testFilePath)
|
||||
assert.NoError(t, err)
|
||||
err = os.Remove(localDownloadPath)
|
||||
assert.NoError(t, err)
|
||||
err = client.Quit()
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
||||
assert.NoError(t, err)
|
||||
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
||||
assert.NoError(t, err)
|
||||
err = os.RemoveAll(localUser.GetHomeDir())
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func TestMultiFactorAuth(t *testing.T) {
|
||||
u := getTestUser()
|
||||
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
||||
|
||||
@@ -262,6 +262,8 @@ func (cc mockFTPClientContext) Path() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (cc mockFTPClientContext) SetPath(name string) {}
|
||||
|
||||
func (cc mockFTPClientContext) SetDebug(debug bool) {}
|
||||
|
||||
func (cc mockFTPClientContext) Debug() bool {
|
||||
|
||||
@@ -201,6 +201,7 @@ func (s *Server) AuthUser(cc ftpserver.ClientContext, username, password string)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setStartDirectory(user.Filters.StartDirectory, cc)
|
||||
connection.Log(logger.LevelInfo, "User %#v logged in with %#v from ip %#v", user.Username, loginMethod, ipAddr)
|
||||
dataprovider.UpdateLastLogin(&user)
|
||||
return connection, nil
|
||||
@@ -246,6 +247,7 @@ func (s *Server) VerifyConnection(cc ftpserver.ClientContext, user string, tlsCo
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
setStartDirectory(dbUser.Filters.StartDirectory, cc)
|
||||
connection.Log(logger.LevelInfo, "User id: %d, logged in with FTP using a TLS certificate, username: %#v, home_dir: %#v remote addr: %#v",
|
||||
dbUser.ID, dbUser.Username, dbUser.HomeDir, ipAddr)
|
||||
dataprovider.UpdateLastLogin(&dbUser)
|
||||
@@ -367,6 +369,13 @@ func (s *Server) validateUser(user dataprovider.User, cc ftpserver.ClientContext
|
||||
return connection, nil
|
||||
}
|
||||
|
||||
func setStartDirectory(startDirectory string, cc ftpserver.ClientContext) {
|
||||
if startDirectory == "" {
|
||||
return
|
||||
}
|
||||
cc.SetPath(startDirectory)
|
||||
}
|
||||
|
||||
func updateLoginMetrics(user *dataprovider.User, ip, loginMethod string, err error) {
|
||||
metric.AddLoginAttempt(loginMethod)
|
||||
if err != nil && err != common.ErrInternalFailure {
|
||||
|
||||
Reference in New Issue
Block a user