2024-01-01 10:31:45 +00:00
|
|
|
// Copyright (C) 2019 Nicola Murino
|
2022-07-17 18:16:00 +00:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
2023-01-03 09:18:30 +00:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2022-07-17 18:16:00 +00:00
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
package ftpd_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2020-12-24 17:48:06 +00:00
|
|
|
"crypto/sha256"
|
2020-07-29 19:56:56 +00:00
|
|
|
"crypto/tls"
|
2020-12-24 17:48:06 +00:00
|
|
|
"encoding/hex"
|
2020-07-29 19:56:56 +00:00
|
|
|
"encoding/json"
|
2022-05-09 17:09:43 +00:00
|
|
|
"errors"
|
2020-07-29 19:56:56 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-05-09 17:09:43 +00:00
|
|
|
"io/fs"
|
2020-07-29 19:56:56 +00:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
2021-02-11 18:45:52 +00:00
|
|
|
"strconv"
|
2020-07-29 19:56:56 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-28 17:40:04 +00:00
|
|
|
ftpserver "github.com/fclairamb/ftpserverlib"
|
2020-07-29 19:56:56 +00:00
|
|
|
"github.com/jlaffaye/ftp"
|
2021-09-04 10:11:04 +00:00
|
|
|
"github.com/pquerna/otp"
|
|
|
|
"github.com/pquerna/otp/totp"
|
2020-07-29 19:56:56 +00:00
|
|
|
"github.com/rs/zerolog"
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
|
|
|
sdkkms "github.com/sftpgo/sdk/kms"
|
2020-07-29 19:56:56 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2020-12-23 15:12:30 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2022-07-24 14:18:54 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/internal/common"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/config"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/dataprovider"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/ftpd"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/httpdtest"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/kms"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/logger"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/mfa"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/sftpd"
|
|
|
|
"github.com/drakkan/sftpgo/v2/internal/vfs"
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-08-20 14:00:36 +00:00
|
|
|
logSender = "ftpdTesting"
|
|
|
|
ftpServerAddr = "127.0.0.1:2121"
|
|
|
|
sftpServerAddr = "127.0.0.1:2122"
|
|
|
|
ftpSrvAddrTLS = "127.0.0.1:2124" // ftp server with implicit tls
|
|
|
|
ftpSrvAddrTLSResumption = "127.0.0.1:2126" // ftp server with implicit tls
|
|
|
|
defaultUsername = "test_user_ftp"
|
|
|
|
defaultPassword = "test_password"
|
|
|
|
osWindows = "windows"
|
|
|
|
ftpsCert = `-----BEGIN CERTIFICATE-----
|
2020-07-29 19:56:56 +00:00
|
|
|
MIICHTCCAaKgAwIBAgIUHnqw7QnB1Bj9oUsNpdb+ZkFPOxMwCgYIKoZIzj0EAwIw
|
|
|
|
RTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGElu
|
|
|
|
dGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMDAyMDQwOTUzMDRaFw0zMDAyMDEw
|
|
|
|
OTUzMDRaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYD
|
|
|
|
VQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwdjAQBgcqhkjOPQIBBgUrgQQA
|
|
|
|
IgNiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVqWvrJ51t5OxV0v25NsOgR82CA
|
|
|
|
NXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIVCzgWkxiz7XE4lgUwX44FCXZM
|
|
|
|
3+JeUbKjUzBRMB0GA1UdDgQWBBRhLw+/o3+Z02MI/d4tmaMui9W16jAfBgNVHSME
|
|
|
|
GDAWgBRhLw+/o3+Z02MI/d4tmaMui9W16jAPBgNVHRMBAf8EBTADAQH/MAoGCCqG
|
|
|
|
SM49BAMCA2kAMGYCMQDqLt2lm8mE+tGgtjDmtFgdOcI72HSbRQ74D5rYTzgST1rY
|
|
|
|
/8wTi5xl8TiFUyLMUsICMQC5ViVxdXbhuG7gX6yEqSkMKZICHpO8hqFwOD/uaFVI
|
|
|
|
dV4vKmHUzwK/eIx+8Ay3neE=
|
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
ftpsKey = `-----BEGIN EC PARAMETERS-----
|
|
|
|
BgUrgQQAIg==
|
|
|
|
-----END EC PARAMETERS-----
|
|
|
|
-----BEGIN EC PRIVATE KEY-----
|
|
|
|
MIGkAgEBBDCfMNsN6miEE3rVyUPwElfiJSWaR5huPCzUenZOfJT04GAcQdWvEju3
|
|
|
|
UM2lmBLIXpGgBwYFK4EEACKhZANiAARCjRMqJ85rzMC998X5z761nJ+xL3bkmGVq
|
|
|
|
WvrJ51t5OxV0v25NsOgR82CANXUgvhVYs7vNFN+jxtb2aj6Xg+/2G/BNxkaFspIV
|
|
|
|
CzgWkxiz7XE4lgUwX44FCXZM3+JeUbI=
|
|
|
|
-----END EC PRIVATE KEY-----`
|
2021-02-28 11:10:40 +00:00
|
|
|
caCRT = `-----BEGIN CERTIFICATE-----
|
|
|
|
MIIE5jCCAs6gAwIBAgIBATANBgkqhkiG9w0BAQsFADATMREwDwYDVQQDEwhDZXJ0
|
2024-01-10 18:53:48 +00:00
|
|
|
QXV0aDAeFw0yNDAxMTAxODEyMDRaFw0zNDAxMTAxODIxNTRaMBMxETAPBgNVBAMT
|
|
|
|
CENlcnRBdXRoMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA7WHW216m
|
|
|
|
fi4uF8cx6HWf8wvAxaEWgCHTOi2MwFIzOrOtuT7xb64rkpdzx1aWetSiCrEyc3D1
|
|
|
|
v03k0Akvlz1gtnDtO64+MA8bqlTnCydZJY4cCTvDOBUYZgtMqHZzpE6xRrqQ84zh
|
|
|
|
yzjKQ5bR0st+XGfIkuhjSuf2n/ZPS37fge9j6AKzn/2uEVt33qmO85WtN3RzbSqL
|
|
|
|
CdOJ6cQ216j3la1C5+NWvzIKC7t6NE1bBGI4+tRj7B5P5MeamkkogwbExUjdHp3U
|
|
|
|
4yasvoGcCHUQDoa4Dej1faywz6JlwB6rTV4ys4aZDe67V/Q8iB2May1k7zBz1Ztb
|
|
|
|
KF5Em3xewP1LqPEowF1uc4KtPGcP4bxdaIpSpmObcn8AIfH6smLQrn0C3cs7CYfo
|
|
|
|
NlFuTbwzENUhjz0X6EsoM4w4c87lO+dRNR7YpHLqR/BJTbbyXUB0imne1u00fuzb
|
|
|
|
S7OtweiA9w7DRCkr2gU4lmHe7l0T+SA9pxIeVLb78x7ivdyXSF5LVQJ1JvhhWu6i
|
|
|
|
M6GQdLHat/0fpRFUbEe34RQSDJ2eOBifMJqvsvpBP8d2jcRZVUVrSXGc2mAGuGOY
|
|
|
|
/tmnCJGW8Fd+sgpCVAqM0pxCM+apqrvJYUqqQZ2ZxugCXULtRWJ9p4C9zUl40HEy
|
|
|
|
OQ+AaiiwFll/doXELglcJdNg8AZPGhugfxMCAwEAAaNFMEMwDgYDVR0PAQH/BAQD
|
|
|
|
AgEGMBIGA1UdEwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFNoJhIvDZQrEf/VQbWuu
|
|
|
|
XgNnt2m5MA0GCSqGSIb3DQEBCwUAA4ICAQCYhT5SRqk19hGrQ09hVSZOzynXAa5F
|
|
|
|
sYkEWJzFyLg9azhnTPE1bFM18FScnkd+dal6mt+bQiJvdh24NaVkDghVB7GkmXki
|
|
|
|
pAiZwEDHMqtbhiPxY8LtSeCBAz5JqXVU2Q0TpAgNSH4W7FbGWNThhxcJVOoIrXKE
|
|
|
|
jbzhwl1Etcaf0DBKWliUbdlxQQs65DLy+rNBYtOeK0pzhzn1vpehUlJ4eTFzP9KX
|
|
|
|
y2Mksuq9AspPbqnqpWW645MdTxMb5T57MCrY3GDKw63z5z3kz88LWJF3nOxZmgQy
|
|
|
|
WFUhbLmZm7x6N5eiu6Wk8/B4yJ/n5UArD4cEP1i7nqu+mbbM/SZlq1wnGpg/sbRV
|
|
|
|
oUF+a7pRcSbfxEttle4pLFhS+ErKatjGcNEab2OlU3bX5UoBs+TYodnCWGKOuBKV
|
|
|
|
L/CYc65QyeYZ+JiwYn9wC8YkzOnnVIQjiCEkLgSL30h9dxpnTZDLrdAA8ItelDn5
|
|
|
|
DvjuQq58CGDsaVqpSobiSC1DMXYWot4Ets1wwovUNEq1l0MERB+2olE+JU/8E23E
|
|
|
|
eL1/aA7Kw/JibkWz1IyzClpFDKXf6kR2onJyxerdwUL+is7tqYFLysiHxZDL1bli
|
|
|
|
SXbW8hMa5gvo0IilFP9Rznn8PplIfCsvBDVv6xsRr5nTAFtwKaMBVgznE2ghs69w
|
|
|
|
kK8u1YiiVenmoQ==
|
2021-02-28 11:10:40 +00:00
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
caCRL = `-----BEGIN X509 CRL-----
|
|
|
|
MIICpzCBkAIBATANBgkqhkiG9w0BAQsFADATMREwDwYDVQQDEwhDZXJ0QXV0aBcN
|
2024-01-10 18:53:48 +00:00
|
|
|
MjQwMTEwMTgyMjU4WhcNMjYwMTA5MTgyMjU4WjAkMCICEQDOaeHbjY4pEj8WBmqg
|
|
|
|
ZuRRFw0yNDAxMTAxODIyNThaoCMwITAfBgNVHSMEGDAWgBTaCYSLw2UKxH/1UG1r
|
|
|
|
rl4DZ7dpuTANBgkqhkiG9w0BAQsFAAOCAgEAZzZ4aBqCcAJigR9e/mqKpJa4B6FV
|
|
|
|
+jZmnWXolGeUuVkjdiG9w614x7mB2S768iioJyALejjCZjqsp6ydxtn0epQw4199
|
|
|
|
XSfPIxA9lxc7w79GLe0v3ztojvxDPh5V1+lwPzGf9i8AsGqb2BrcBqgxDeatndnE
|
|
|
|
jF+18bY1saXOBpukNLjtRScUXzy5YcSuO6mwz4548v+1ebpF7W4Yh+yh0zldJKcF
|
|
|
|
DouuirZWujJwTwxxfJ+2+yP7GAuefXUOhYs/1y9ylvUgvKFqSyokv6OaVgTooKYD
|
|
|
|
MSADzmNcbRvwyAC5oL2yJTVVoTFeP6fXl/BdFH3sO/hlKXGy4Wh1AjcVE6T0CSJ4
|
|
|
|
iYFX3gLFh6dbP9IQWMlIM5DKtAKSjmgOywEaWii3e4M0NFSf/Cy17p2E5/jXSLlE
|
|
|
|
ypDileK0aALkx2twGWwogh6sY1dQ6R3GpKSRPD2muQxVOG6wXvuJce0E9WLx1Ud4
|
|
|
|
hVUdUEMlKUvm77/15U5awarH2cCJQxzS/GMeIintQiG7hUlgRzRdmWVe3vOOvt94
|
|
|
|
cp8+ZUH/QSDOo41ATTHpFeC/XqF5E2G/ahXqra+O5my52V/FP0bSJnkorJ8apy67
|
|
|
|
sn6DFbkqX9khTXGtacczh2PcqVjcQjBniYl2sPO3qIrrrY3tic96tMnM/u3JRdcn
|
|
|
|
w7bXJGfJcIMrrKs=
|
2021-02-28 11:10:40 +00:00
|
|
|
-----END X509 CRL-----`
|
|
|
|
client1Crt = `-----BEGIN CERTIFICATE-----
|
2024-01-10 18:53:48 +00:00
|
|
|
MIIEITCCAgmgAwIBAgIRAJr32nHRlhyPiS7IfZ/ZWYowDQYJKoZIhvcNAQELBQAw
|
|
|
|
EzERMA8GA1UEAxMIQ2VydEF1dGgwHhcNMjQwMTEwMTgxMjM3WhcNMzQwMTEwMTgy
|
|
|
|
MTUzWjASMRAwDgYDVQQDEwdjbGllbnQxMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
|
|
|
|
MIIBCgKCAQEAtuQFiqvdjd8WLxP0FgPDyDEJ1/uJ+Aoj6QllNV7svWxwW+kiJ3X6
|
|
|
|
HUVNWhhCsNfly4pGW4erF4fZzmesElGx1PoWgQCWZKsa/N08bznelWgdmkyi85xE
|
|
|
|
OkTj6e/cTWHFSOBURNJaXkGHZ0ROSh7qu0Ld+eqNo3k9W+NqZaqYvs2K7MLWeYl7
|
|
|
|
Qie8Ctuq5Qaz/jm0XwR2PFBROVQSaCPCukancPQ21ftqHPhAbjxoxvvN5QP4ZdRf
|
|
|
|
XlH/LDLhlFnJzPZdHnVy9xisSPPRfFApJiwyfjRYdtslpJOcNgP6oPlpX/dybbhO
|
|
|
|
c9FEUgj/Q90Je8EfioBYFYsqVD6/dFv9SwIDAQABo3EwbzAOBgNVHQ8BAf8EBAMC
|
|
|
|
A7gwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMB0GA1UdDgQWBBRUh5Xo
|
|
|
|
Gzjh6iReaPSOgGatqOw9bDAfBgNVHSMEGDAWgBTaCYSLw2UKxH/1UG1rrl4DZ7dp
|
|
|
|
uTANBgkqhkiG9w0BAQsFAAOCAgEAyAK7cOTWqjyLgFM0kyyx1fNPvm2GwKep3MuU
|
|
|
|
OrSnLuWjoxzb7WcbKNVMlnvnmSUAWuErxsY0PUJNfcuqWiGmEp4d/SWfWPigG6DC
|
|
|
|
sDej35BlSfX8FCufYrfC74VNk4yBS2LVYmIqcpqUrfay0I2oZA8+ToLEpdUvEv2I
|
|
|
|
l59eOhJO2jsC3JbOyZZmK2Kv7d94fR+1tg2Rq1Wbnmc9AZKq7KDReAlIJh4u2KHb
|
|
|
|
BbtF79idusMwZyP777tqSQ4THBMa+VAEc2UrzdZqTIAwqlKQOvO2fRz2P+ARR+Tz
|
|
|
|
MYJMdCdmPZ9qAc8U1OcFBG6qDDltO8wf/Nu/PsSI5LGCIhIuPPIuKfm0rRfTqCG7
|
|
|
|
QPQPWjRoXtGGhwjdIuWbX9fIB+c+NpAEKHgLtV+Rxj8s5IVxqG9a5TtU9VkfVXJz
|
|
|
|
J20naoz/G+vDsVINpd3kH0ziNvdrKfGRM5UgtnUOPCXB22fVmkIsMH2knI10CKK+
|
|
|
|
offI56NTkLRu00xvg98/wdukhkwIAxg6PQI/BHY5mdvoacEHHHdOhMq+GSAh7DDX
|
|
|
|
G8+HdbABM1ExkPnZLat15q706ztiuUpQv1C2DI8YviUVkMqCslj4cD4F8EFPo4kr
|
|
|
|
kvme0Cuc9Qlf7N5rjdV3cjwavhFx44dyXj9aesft2Q1okPiIqbGNpcjHcIRlj4Au
|
|
|
|
MU3Bo0A=
|
2021-02-28 11:10:40 +00:00
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
client1Key = `-----BEGIN RSA PRIVATE KEY-----
|
2024-01-10 18:53:48 +00:00
|
|
|
MIIEpAIBAAKCAQEAtuQFiqvdjd8WLxP0FgPDyDEJ1/uJ+Aoj6QllNV7svWxwW+ki
|
|
|
|
J3X6HUVNWhhCsNfly4pGW4erF4fZzmesElGx1PoWgQCWZKsa/N08bznelWgdmkyi
|
|
|
|
85xEOkTj6e/cTWHFSOBURNJaXkGHZ0ROSh7qu0Ld+eqNo3k9W+NqZaqYvs2K7MLW
|
|
|
|
eYl7Qie8Ctuq5Qaz/jm0XwR2PFBROVQSaCPCukancPQ21ftqHPhAbjxoxvvN5QP4
|
|
|
|
ZdRfXlH/LDLhlFnJzPZdHnVy9xisSPPRfFApJiwyfjRYdtslpJOcNgP6oPlpX/dy
|
|
|
|
bbhOc9FEUgj/Q90Je8EfioBYFYsqVD6/dFv9SwIDAQABAoIBAFjSHK7gENVZxphO
|
|
|
|
hHg8k9ShnDo8eyDvK8l9Op3U3/yOsXKxolivvyx//7UFmz3vXDahjNHe7YScAXdw
|
|
|
|
eezbqBXa7xrvghqZzp2HhFYwMJ0210mcdncBKVFzK4ztZHxgQ0PFTqet0R19jZjl
|
|
|
|
X3A325/eNZeuBeOied4qb/24AD6JGc6A0J55f5/QUQtdwYwrL15iC/KZXDL90PPJ
|
|
|
|
CFJyrSzcXvOMEvOfXIFxhDVKRCppyIYXG7c80gtNC37I6rxxMNQ4mxjwUI2IVhxL
|
|
|
|
j+nZDu0JgRZ4NaGjOq2e79QxUVm/GG3z25XgmBFBrXkEVV+sCZE1VDyj6kQfv9FU
|
|
|
|
NhOrwGECgYEAzq47r/HwXifuGYBV/mvInFw3BNLrKry+iUZrJ4ms4g+LfOi0BAgf
|
|
|
|
sXsWXulpBo2YgYjFdO8G66f69GlB4B7iLscpABXbRtpDZEnchQpaF36/+4g3i8gB
|
|
|
|
Z29XHNDB8+7t4wbXvlSnLv1tZWey2fS4hPosc2YlvS87DMmnJMJqhs8CgYEA4oiB
|
|
|
|
LGQP6VNdX0Uigmh5fL1g1k95eC8GP1ylczCcIwsb2OkAq0MT7SHRXOlg3leEq4+g
|
|
|
|
mCHk1NdjkSYxDL2ZeTKTS/gy4p1jlcDa6Ilwi4pVvatNvu4o80EYWxRNNb1mAn67
|
|
|
|
T8TN9lzc6mEi+LepQM3nYJ3F+ZWTKgxH8uoJwMUCgYEArpumE1vbjUBAuEyi2eGn
|
|
|
|
RunlFW83fBCfDAxw5KM8anNlja5uvuU6GU/6s06QCxg+2lh5MPPrLdXpfukZ3UVa
|
|
|
|
Itjg+5B7gx1MSALaiY8YU7cibFdFThM3lHIM72wyH2ogkWcrh0GvSFSUQlJcWCSW
|
|
|
|
asmMGiYXBgBL697FFZomMyMCgYEAkAnp0JcDQwHd4gDsk2zoqnckBsDb5J5J46n+
|
|
|
|
DYNAFEww9bgZ08u/9MzG+cPu8xFE621U2MbcYLVfuuBE2ewIlPaij/COMmeO9Z59
|
|
|
|
0tPpOuDH6eTtd1SptxqR6P+8pEn8feOlKHBj4Z1kXqdK/EiTlwAVeep4Al2oCFls
|
|
|
|
ujkz4F0CgYAe8vHnVFHlWi16zAqZx4ZZZhNuqPtgFkvPg9LfyNTA4dz7F9xgtUaY
|
|
|
|
nXBPyCe/8NtgBfT79HkPiG3TM0xRZY9UZgsJKFtqAu5u4ManuWDnsZI9RK2QTLHe
|
|
|
|
yEbH5r3Dg3n9k/3GbjXFIWdU9UaYsdnSKHHtMw9ZODc14LaAogEQug==
|
2021-02-28 11:10:40 +00:00
|
|
|
-----END RSA PRIVATE KEY-----`
|
|
|
|
// client 2 crt is revoked
|
|
|
|
client2Crt = `-----BEGIN CERTIFICATE-----
|
2024-01-10 18:53:48 +00:00
|
|
|
MIIEITCCAgmgAwIBAgIRAM5p4duNjikSPxYGaqBm5FEwDQYJKoZIhvcNAQELBQAw
|
|
|
|
EzERMA8GA1UEAxMIQ2VydEF1dGgwHhcNMjQwMTEwMTgxMjUyWhcNMzQwMTEwMTgy
|
|
|
|
MTUzWjASMRAwDgYDVQQDEwdjbGllbnQyMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8A
|
|
|
|
MIIBCgKCAQEApNYpNZVmXZtAObpRRIuP2o/7z04H2E161vKZvJ3LSLlUTImVjm/b
|
|
|
|
Qe6DTNCUVLnzQuanmUlu2rUnN3lDSfYoBcJWbvC3y1OCPRkCjDV6KiYMA9TPkZua
|
|
|
|
eq6y3+bFFfEmyumsVEe0bSuzNHXCOIBT7PqYMdovECcwBh/RZCA5mqO5omEKh4LQ
|
|
|
|
cr6+sVVkvD3nsyx0Alz/kTLFqc0mVflmpJq+0BpdetHRg4n5vy/I/08jZ81PQAmT
|
|
|
|
A0kyl0Jh132JBGFdA8eyugPPP8n5edU4f3HXV/nR7XLwBrpSt8KgEg8cwfAu4Ic0
|
|
|
|
6tGzB0CH8lSGtU0tH2/cOlDuguDD7VvokQIDAQABo3EwbzAOBgNVHQ8BAf8EBAMC
|
|
|
|
A7gwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMB0GA1UdDgQWBBR5mf0f
|
|
|
|
Zjf8ZCGXqU2+45th7VkkLDAfBgNVHSMEGDAWgBTaCYSLw2UKxH/1UG1rrl4DZ7dp
|
|
|
|
uTANBgkqhkiG9w0BAQsFAAOCAgEARhFxNAouwbpEfN1M90+ao5rwyxEewerSoCCz
|
|
|
|
PQzeUZ66MA/FkS/tFUGgGGG+wERN+WLbe1cN6q/XFr0FSMLuUxLXDNV02oUL/FnY
|
|
|
|
xcyNLaZUZ0pP7sA+Hmx2AdTA6baIwQbyIY9RLAaz6hzo1YbI8yeis645F1bxgL2D
|
|
|
|
EP5kXa3Obv0tqWByMZtrmJPv3p0W5GJKXVDn51GR/E5KI7pliZX2e0LmMX9mxfPB
|
|
|
|
4sXFUggMHXxWMMSAmXPVsxC2KX6gMnajO7JUraTwuGm+6V371FzEX+UKXHI+xSvO
|
|
|
|
78TseTIYsBGLjeiA8UjkKlD3T9qsQm2mb2PlKyqjvIm4i2ilM0E2w4JZmd45b925
|
|
|
|
7q/QLV3NZ/zZMi6AMyULu28DWKfAx3RLKwnHWSFcR4lVkxQrbDhEUMhAhLAX+2+e
|
|
|
|
qc7qZm3dTabi7ZJiiOvYK/yNgFHa/XtZp5uKPB5tigPIa+34hbZF7s2/ty5X3O1N
|
|
|
|
f5Ardz7KNsxJjZIt6HvB28E/PPOvBqCKJc1Y08J9JbZi8p6QS1uarGoR7l7rT1Hv
|
|
|
|
/ZXkNTw2bw1VpcWdzDBLLVHYNnJmS14189LVk11PcJJpSmubwCqg+ZZULdgtVr3S
|
|
|
|
ANas2dgMPVwXhnAalgkcc+lb2QqaEz06axfbRGBsgnyqR5/koKCg1Hr0+vThHSsR
|
|
|
|
E0+r2+4=
|
2021-02-28 11:10:40 +00:00
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
client2Key = `-----BEGIN RSA PRIVATE KEY-----
|
2024-01-10 18:53:48 +00:00
|
|
|
MIIEowIBAAKCAQEApNYpNZVmXZtAObpRRIuP2o/7z04H2E161vKZvJ3LSLlUTImV
|
|
|
|
jm/bQe6DTNCUVLnzQuanmUlu2rUnN3lDSfYoBcJWbvC3y1OCPRkCjDV6KiYMA9TP
|
|
|
|
kZuaeq6y3+bFFfEmyumsVEe0bSuzNHXCOIBT7PqYMdovECcwBh/RZCA5mqO5omEK
|
|
|
|
h4LQcr6+sVVkvD3nsyx0Alz/kTLFqc0mVflmpJq+0BpdetHRg4n5vy/I/08jZ81P
|
|
|
|
QAmTA0kyl0Jh132JBGFdA8eyugPPP8n5edU4f3HXV/nR7XLwBrpSt8KgEg8cwfAu
|
|
|
|
4Ic06tGzB0CH8lSGtU0tH2/cOlDuguDD7VvokQIDAQABAoIBAQCMnEeg9uXQmdvq
|
|
|
|
op4qi6bV+ZcDWvvkLwvHikFMnYpIaheYBpF2ZMKzdmO4xgCSWeFCQ4Hah8KxfHCM
|
|
|
|
qLuWvw2bBBE5J8yQ/JaPyeLbec7RX41GQ2YhPoxDdP0PdErREdpWo4imiFhH/Ewt
|
|
|
|
Rvq7ufRdpdLoS8dzzwnvX3r+H2MkHoC/QANW2AOuVoZK5qyCH5N8yEAAbWKaQaeL
|
|
|
|
VBhAYEVKbAkWEtXw7bYXzxRR7WIM3f45v3ncRusDIG+Hf75ZjatoH0lF1gHQNofO
|
|
|
|
qkCVZVzjkLFuzDic2KZqsNORglNs4J6t5Dahb9v3hnoK963YMnVSUjFvqQ+/RZZy
|
|
|
|
VILFShilAoGBANucwZU61eJ0tLKBYEwmRY/K7Gu1MvvcYJIOoX8/BL3zNmNO0CLl
|
|
|
|
NiABtNt9WOVwZxDsxJXdo1zvMtAegNqS6W11R1VAZbL6mQ/krScbLDE6JKA5DmA7
|
|
|
|
4nNi1gJOW1ziAfdBAfhe4cLbQOb94xkOK5xM1YpO0xgDJLwrZbehDMmPAoGBAMAl
|
|
|
|
/owPDAvcXz7JFynT0ieYVc64MSFiwGYJcsmxSAnbEgQ+TR5FtkHYe91OSqauZcCd
|
|
|
|
aoKXQNyrYKIhyounRPFTdYQrlx6KtEs7LU9wOxuphhpJtGjRnhmA7IqvX703wNvu
|
|
|
|
khrEavn86G5boH8R80371SrN0Rh9UeAlQGuNBdvfAoGAEAmokW9Ug08miwqrr6Pz
|
|
|
|
3IZjMZJwALidTM1IufQuMnj6ddIhnQrEIx48yPKkdUz6GeBQkuk2rujA+zXfDxc/
|
|
|
|
eMDhzrX/N0zZtLFse7ieR5IJbrH7/MciyG5lVpHGVkgjAJ18uVikgAhm+vd7iC7i
|
|
|
|
vG1YAtuyysQgAKXircBTIL0CgYAHeTLWVbt9NpwJwB6DhPaWjalAug9HIiUjktiB
|
|
|
|
GcEYiQnBWn77X3DATOA8clAa/Yt9m2HKJIHkU1IV3ESZe+8Fh955PozJJlHu3yVb
|
|
|
|
Ap157PUHTriSnxyMF2Sb3EhX/rQkmbnbCqqygHC14iBy8MrKzLG00X6BelZV5n0D
|
|
|
|
8d85dwKBgGWY2nsaemPH/TiTVF6kW1IKSQoIyJChkngc+Xj/2aCCkkmAEn8eqncl
|
|
|
|
RKjnkiEZeG4+G91Xu7+HmcBLwV86k5I+tXK9O1Okomr6Zry8oqVcxU5TB6VRS+rA
|
|
|
|
ubwF00Drdvk2+kDZfxIM137nBiy7wgCJi2Ksm5ihN3dUF6Q0oNPl
|
2021-02-28 11:10:40 +00:00
|
|
|
-----END RSA PRIVATE KEY-----`
|
2022-06-11 08:41:34 +00:00
|
|
|
testFileName = "test_file_ftp.dat"
|
|
|
|
testDLFileName = "test_download_ftp.dat"
|
|
|
|
tlsClient1Username = "client1"
|
|
|
|
tlsClient2Username = "client2"
|
|
|
|
httpFsPort = 23456
|
|
|
|
defaultHTTPFsUsername = "httpfs_ftp_user"
|
2022-07-27 16:54:25 +00:00
|
|
|
emptyPwdPlaceholder = "empty"
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-07-24 14:18:54 +00:00
|
|
|
configDir = filepath.Join(".", "..", "..")
|
2020-07-30 20:33:49 +00:00
|
|
|
allPerms = []string{dataprovider.PermAny}
|
|
|
|
homeBasePath string
|
|
|
|
hookCmdPath string
|
|
|
|
extAuthPath string
|
|
|
|
preLoginPath string
|
|
|
|
postConnectPath string
|
2021-05-26 05:48:37 +00:00
|
|
|
preDownloadPath string
|
|
|
|
preUploadPath string
|
2020-07-30 20:33:49 +00:00
|
|
|
logFilePath string
|
2021-02-28 11:10:40 +00:00
|
|
|
caCrtPath string
|
|
|
|
caCRLPath string
|
2020-07-29 19:56:56 +00:00
|
|
|
)
|
|
|
|
|
2023-08-20 14:00:36 +00:00
|
|
|
func TestMain(m *testing.M) { //nolint:gocyclo
|
2020-07-29 19:56:56 +00:00
|
|
|
logFilePath = filepath.Join(configDir, "sftpgo_ftpd_test.log")
|
|
|
|
bannerFileName := "banner_file"
|
|
|
|
bannerFile := filepath.Join(configDir, bannerFileName)
|
2021-11-06 14:18:16 +00:00
|
|
|
logger.InitLogger(logFilePath, 5, 1, 28, false, false, zerolog.DebugLevel)
|
2021-02-25 20:53:04 +00:00
|
|
|
err := os.WriteFile(bannerFile, []byte("SFTPGo test ready\nsimple banner line\n"), os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error creating banner file: %v", err)
|
2023-08-20 14:00:36 +00:00
|
|
|
os.Exit(1)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
// we run the test cases with UploadMode atomic and resume support. The non atomic code path
|
|
|
|
// simply does not execute some code so if it works in atomic mode will
|
|
|
|
// work in non atomic mode too
|
|
|
|
os.Setenv("SFTPGO_COMMON__UPLOAD_MODE", "2")
|
2021-05-14 17:21:15 +00:00
|
|
|
os.Setenv("SFTPGO_DATA_PROVIDER__CREATE_DEFAULT_ADMIN", "1")
|
2022-10-12 16:12:12 +00:00
|
|
|
os.Setenv("SFTPGO_COMMON__ALLOW_SELF_CONNECTIONS", "1")
|
2021-08-20 08:37:51 +00:00
|
|
|
os.Setenv("SFTPGO_DEFAULT_ADMIN_USERNAME", "admin")
|
|
|
|
os.Setenv("SFTPGO_DEFAULT_ADMIN_PASSWORD", "password")
|
2020-07-29 19:56:56 +00:00
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error loading configuration: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
logger.InfoToConsole("Starting FTPD tests, provider: %v", providerConf.Driver)
|
|
|
|
|
|
|
|
commonConf := config.GetCommonConfig()
|
|
|
|
homeBasePath = os.TempDir()
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
commonConf.Actions.ExecuteOn = []string{"download", "upload", "rename", "delete"}
|
|
|
|
commonConf.Actions.Hook = hookCmdPath
|
|
|
|
hookCmdPath, err = exec.LookPath("true")
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, "", "unable to get hook command: %v", err)
|
|
|
|
logger.WarnToConsole("unable to get hook command: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
certPath := filepath.Join(os.TempDir(), "test_ftpd.crt")
|
|
|
|
keyPath := filepath.Join(os.TempDir(), "test_ftpd.key")
|
2021-02-28 11:10:40 +00:00
|
|
|
caCrtPath = filepath.Join(os.TempDir(), "test_ftpd_ca.crt")
|
|
|
|
caCRLPath = filepath.Join(os.TempDir(), "test_ftpd_crl.crt")
|
|
|
|
err = writeCerts(certPath, keyPath, caCrtPath, caCRLPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2023-02-09 08:33:33 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2021-01-02 13:05:09 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
logger.ErrorToConsole("error initializing data provider: %v", err)
|
2021-01-02 13:05:09 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-02-09 08:33:33 +00:00
|
|
|
err = common.Initialize(commonConf, 0)
|
2020-07-29 19:56:56 +00:00
|
|
|
if err != nil {
|
2023-02-09 08:33:33 +00:00
|
|
|
logger.WarnToConsole("error initializing common: %v", err)
|
2020-07-29 19:56:56 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
httpConfig := config.GetHTTPConfig()
|
2021-02-13 13:41:37 +00:00
|
|
|
httpConfig.Initialize(configDir) //nolint:errcheck
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
kmsConfig := config.GetKMSConfig()
|
|
|
|
err = kmsConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error initializing kms: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
mfaConfig := config.GetMFAConfig()
|
|
|
|
err = mfaConfig.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error initializing MFA: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
httpdConf := config.GetHTTPDConfig()
|
2021-01-19 17:59:41 +00:00
|
|
|
httpdConf.Bindings[0].Port = 8079
|
2021-01-17 21:29:08 +00:00
|
|
|
httpdtest.SetBaseURL("http://127.0.0.1:8079")
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
ftpdConf := config.GetFTPDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
ftpdConf.Bindings = []ftpd.Binding{
|
|
|
|
{
|
2022-05-21 14:34:47 +00:00
|
|
|
Port: 2121,
|
|
|
|
ClientAuthType: 2,
|
|
|
|
CertificateFile: certPath,
|
|
|
|
CertificateKeyFile: keyPath,
|
2020-12-23 15:12:30 +00:00
|
|
|
},
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
ftpdConf.PassivePortRange.Start = 0
|
|
|
|
ftpdConf.PassivePortRange.End = 0
|
|
|
|
ftpdConf.BannerFile = bannerFileName
|
2021-02-28 11:10:40 +00:00
|
|
|
ftpdConf.CACertificates = []string{caCrtPath}
|
|
|
|
ftpdConf.CARevocationLists = []string{caCRLPath}
|
2020-12-24 17:48:06 +00:00
|
|
|
ftpdConf.EnableSite = true
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
// required to test sftpfs
|
|
|
|
sftpdConf := config.GetSFTPDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
sftpdConf.Bindings = []sftpd.Binding{
|
|
|
|
{
|
|
|
|
Port: 2122,
|
|
|
|
},
|
|
|
|
}
|
2020-12-24 17:48:06 +00:00
|
|
|
hostKeyPath := filepath.Join(os.TempDir(), "id_ed25519")
|
|
|
|
sftpdConf.HostKeys = []string{hostKeyPath}
|
2020-12-12 09:31:09 +00:00
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
extAuthPath = filepath.Join(homeBasePath, "extauth.sh")
|
|
|
|
preLoginPath = filepath.Join(homeBasePath, "prelogin.sh")
|
2020-07-30 20:33:49 +00:00
|
|
|
postConnectPath = filepath.Join(homeBasePath, "postconnect.sh")
|
2021-05-26 05:48:37 +00:00
|
|
|
preDownloadPath = filepath.Join(homeBasePath, "predownload.sh")
|
|
|
|
preUploadPath = filepath.Join(homeBasePath, "preupload.sh")
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-12-08 10:18:34 +00:00
|
|
|
status := ftpd.GetStatus()
|
|
|
|
if status.IsActive {
|
|
|
|
logger.ErrorToConsole("ftpd is already active")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
go func() {
|
|
|
|
logger.Debug(logSender, "", "initializing FTP server with config %+v", ftpdConf)
|
|
|
|
if err := ftpdConf.Initialize(configDir); err != nil {
|
|
|
|
logger.ErrorToConsole("could not start FTP server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
go func() {
|
|
|
|
logger.Debug(logSender, "", "initializing SFTP server with config %+v", sftpdConf)
|
|
|
|
if err := sftpdConf.Initialize(configDir); err != nil {
|
|
|
|
logger.ErrorToConsole("could not start SFTP server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
go func() {
|
2022-05-19 17:49:51 +00:00
|
|
|
if err := httpdConf.Initialize(configDir, 0); err != nil {
|
2020-07-29 19:56:56 +00:00
|
|
|
logger.ErrorToConsole("could not start HTTP server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
waitTCPListening(ftpdConf.Bindings[0].GetAddress())
|
2021-01-19 17:59:41 +00:00
|
|
|
waitTCPListening(httpdConf.Bindings[0].GetAddress())
|
2020-12-23 15:12:30 +00:00
|
|
|
waitTCPListening(sftpdConf.Bindings[0].GetAddress())
|
2021-01-03 16:03:04 +00:00
|
|
|
ftpd.ReloadCertificateMgr() //nolint:errcheck
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-12-24 17:48:06 +00:00
|
|
|
ftpdConf = config.GetFTPDConfig()
|
|
|
|
ftpdConf.Bindings = []ftpd.Binding{
|
|
|
|
{
|
|
|
|
Port: 2124,
|
|
|
|
TLSMode: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ftpdConf.CertificateFile = certPath
|
|
|
|
ftpdConf.CertificateKeyFile = keyPath
|
2021-02-28 11:10:40 +00:00
|
|
|
ftpdConf.CACertificates = []string{caCrtPath}
|
|
|
|
ftpdConf.CARevocationLists = []string{caCRLPath}
|
2020-12-24 17:48:06 +00:00
|
|
|
ftpdConf.EnableSite = false
|
|
|
|
ftpdConf.DisableActiveMode = true
|
|
|
|
ftpdConf.CombineSupport = 1
|
|
|
|
ftpdConf.HASHSupport = 1
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
logger.Debug(logSender, "", "initializing FTP server with config %+v", ftpdConf)
|
|
|
|
if err := ftpdConf.Initialize(configDir); err != nil {
|
|
|
|
logger.ErrorToConsole("could not start FTP server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
waitTCPListening(ftpdConf.Bindings[0].GetAddress())
|
2023-08-20 14:00:36 +00:00
|
|
|
|
|
|
|
ftpdConf = config.GetFTPDConfig()
|
|
|
|
ftpdConf.Bindings = []ftpd.Binding{
|
|
|
|
{
|
|
|
|
Port: 2126,
|
|
|
|
CertificateFile: certPath,
|
|
|
|
CertificateKeyFile: keyPath,
|
|
|
|
TLSMode: 1,
|
|
|
|
TLSSessionReuse: 1,
|
|
|
|
ClientAuthType: 2,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ftpdConf.CACertificates = []string{caCrtPath}
|
|
|
|
ftpdConf.CARevocationLists = []string{caCRLPath}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
logger.Debug(logSender, "", "initializing FTP server with config %+v", ftpdConf)
|
|
|
|
if err := ftpdConf.Initialize(configDir); err != nil {
|
|
|
|
logger.ErrorToConsole("could not start FTP server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
waitTCPListening(ftpdConf.Bindings[0].GetAddress())
|
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
waitNoConnections()
|
2022-06-11 08:41:34 +00:00
|
|
|
startHTTPFs()
|
2020-12-24 17:48:06 +00:00
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
exitCode := m.Run()
|
|
|
|
os.Remove(logFilePath)
|
|
|
|
os.Remove(bannerFile)
|
|
|
|
os.Remove(extAuthPath)
|
|
|
|
os.Remove(preLoginPath)
|
2020-07-30 20:33:49 +00:00
|
|
|
os.Remove(postConnectPath)
|
2021-05-26 05:48:37 +00:00
|
|
|
os.Remove(preDownloadPath)
|
|
|
|
os.Remove(preUploadPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
os.Remove(certPath)
|
|
|
|
os.Remove(keyPath)
|
2021-02-28 11:10:40 +00:00
|
|
|
os.Remove(caCrtPath)
|
|
|
|
os.Remove(caCRLPath)
|
2020-12-24 17:48:06 +00:00
|
|
|
os.Remove(hostKeyPath)
|
|
|
|
os.Remove(hostKeyPath + ".pub")
|
2020-07-29 19:56:56 +00:00
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2020-12-23 15:12:30 +00:00
|
|
|
func TestInitializationFailure(t *testing.T) {
|
2020-12-08 10:18:34 +00:00
|
|
|
ftpdConf := config.GetFTPDConfig()
|
2020-12-23 15:12:30 +00:00
|
|
|
ftpdConf.Bindings = []ftpd.Binding{}
|
2020-12-08 10:18:34 +00:00
|
|
|
ftpdConf.CertificateFile = filepath.Join(os.TempDir(), "test_ftpd.crt")
|
|
|
|
ftpdConf.CertificateKeyFile = filepath.Join(os.TempDir(), "test_ftpd.key")
|
|
|
|
err := ftpdConf.Initialize(configDir)
|
2020-12-23 15:12:30 +00:00
|
|
|
require.EqualError(t, err, common.ErrNoBinding.Error())
|
|
|
|
ftpdConf.Bindings = []ftpd.Binding{
|
|
|
|
{
|
|
|
|
Port: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port: 2121,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ftpdConf.BannerFile = "a-missing-file"
|
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
ftpdConf.BannerFile = ""
|
|
|
|
ftpdConf.Bindings[1].TLSMode = 10
|
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
ftpdConf.CertificateFile = ""
|
|
|
|
ftpdConf.CertificateKeyFile = ""
|
|
|
|
ftpdConf.Bindings[1].TLSMode = 1
|
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
require.Error(t, err)
|
2020-12-29 08:20:09 +00:00
|
|
|
|
|
|
|
certPath := filepath.Join(os.TempDir(), "test_ftpd.crt")
|
|
|
|
keyPath := filepath.Join(os.TempDir(), "test_ftpd.key")
|
|
|
|
ftpdConf.CertificateFile = certPath
|
|
|
|
ftpdConf.CertificateKeyFile = keyPath
|
|
|
|
ftpdConf.CACertificates = []string{"invalid ca cert"}
|
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
require.Error(t, err)
|
2021-01-03 16:03:04 +00:00
|
|
|
|
|
|
|
ftpdConf.CACertificates = nil
|
|
|
|
ftpdConf.CARevocationLists = []string{""}
|
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
require.Error(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
|
|
|
|
ftpdConf.CACertificates = []string{caCrtPath}
|
|
|
|
ftpdConf.CARevocationLists = []string{caCRLPath}
|
2021-04-16 13:08:10 +00:00
|
|
|
ftpdConf.Bindings[1].ForcePassiveIP = "127001"
|
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "the provided passive IP \"127001\" is not valid")
|
|
|
|
ftpdConf.Bindings[1].ForcePassiveIP = ""
|
2023-08-20 14:00:36 +00:00
|
|
|
ftpdConf.Bindings[1].TLSMode = 2
|
|
|
|
ftpdConf.Bindings[1].TLSSessionReuse = 1
|
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
require.Error(t, err, "TLS session resumption should not be supported with implicit FTPS")
|
|
|
|
ftpdConf.Bindings[1].TLSMode = 0
|
|
|
|
ftpdConf.Bindings[1].TLSSessionReuse = 100
|
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
require.Error(t, err)
|
|
|
|
assert.Contains(t, err.Error(), "unsupported TLS reuse mode")
|
|
|
|
ftpdConf.Bindings[1].TLSSessionReuse = 0
|
2021-02-28 11:10:40 +00:00
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
require.Error(t, err)
|
2023-02-23 18:25:20 +00:00
|
|
|
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpdConf.Initialize(configDir)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "unable to load config from provider")
|
|
|
|
}
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
2020-12-08 10:18:34 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestBasicFTPHandling(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.QuotaSize = 6553600
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u = getTestSFTPUser()
|
|
|
|
u.QuotaSize = 6553600
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
2020-12-12 09:31:09 +00:00
|
|
|
if user.Username == defaultUsername {
|
2022-11-16 18:04:50 +00:00
|
|
|
assert.Len(t, common.Connections.GetStats(""), 1)
|
2020-12-12 09:31:09 +00:00
|
|
|
} else {
|
2022-11-16 18:04:50 +00:00
|
|
|
assert.Len(t, common.Connections.GetStats(""), 2)
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
expectedQuotaSize := testFileSize
|
|
|
|
expectedQuotaFiles := 1
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, path.Join("/missing_dir", testFileName), testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
2022-08-21 17:01:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), user.FirstUpload)
|
|
|
|
assert.Equal(t, int64(0), user.FirstDownload)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
2022-08-21 17:01:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Greater(t, user.FirstUpload, int64(0))
|
|
|
|
assert.Equal(t, int64(0), user.FirstDownload)
|
2020-12-12 09:31:09 +00:00
|
|
|
// overwrite an existing file
|
|
|
|
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)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, expectedQuotaSize, user.UsedQuotaSize)
|
2022-08-21 17:01:08 +00:00
|
|
|
assert.Greater(t, user.FirstUpload, int64(0))
|
|
|
|
assert.Greater(t, user.FirstDownload, int64(0))
|
2020-12-12 09:31:09 +00:00
|
|
|
err = client.Rename(testFileName, testFileName+"1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Delete(testFileName)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Delete(testFileName + "1")
|
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.GetUserByUsername(user.Username, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedQuotaFiles-1, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, expectedQuotaSize-testFileSize, user.UsedQuotaSize)
|
|
|
|
curDir, err := client.CurrentDir()
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "/", curDir)
|
|
|
|
}
|
|
|
|
testDir := "testDir"
|
|
|
|
err = client.MakeDir(testDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.ChangeDir(testDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
curDir, err = client.CurrentDir()
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, path.Join("/", testDir), curDir)
|
|
|
|
}
|
2021-07-31 07:42:23 +00:00
|
|
|
res, err := client.List(path.Join("/", testDir))
|
|
|
|
assert.NoError(t, err)
|
2022-05-04 17:32:02 +00:00
|
|
|
assert.Len(t, res, 0)
|
2021-07-31 07:42:23 +00:00
|
|
|
res, err = client.List(path.Join("/"))
|
|
|
|
assert.NoError(t, err)
|
2022-05-04 17:32:02 +00:00
|
|
|
if assert.Len(t, res, 1) {
|
|
|
|
assert.Equal(t, testDir, res[0].Name)
|
2021-07-31 07:42:23 +00:00
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
size, err := client.FileSize(path.Join("/", testDir, testFileName))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testFileSize, size)
|
|
|
|
err = client.ChangeDirToParent()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
curDir, err = client.CurrentDir()
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, "/", curDir)
|
|
|
|
}
|
|
|
|
err = client.Delete(path.Join("/", testDir, testFileName))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Delete(testDir)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.RemoveDir(testDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2022-11-16 18:04:50 +00:00
|
|
|
assert.Eventually(t, func() bool { return len(common.Connections.GetStats("")) == 0 }, 1*time.Second, 50*time.Millisecond)
|
2021-05-11 06:04:57 +00:00
|
|
|
assert.Eventually(t, func() bool { return common.Connections.GetClientConnections() == 0 }, 1000*time.Millisecond,
|
|
|
|
50*time.Millisecond)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2022-06-11 08:41:34 +00:00
|
|
|
func TestHTTPFs(t *testing.T) {
|
|
|
|
u := getTestUserWithHTTPFs()
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
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)
|
|
|
|
// test a download resume
|
|
|
|
data := []byte("test data")
|
|
|
|
err = os.WriteFile(testFilePath, data, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, int64(len(data)), client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, int64(len(data)-5), client, 5)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
readed, err := os.ReadFile(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("data"), readed, "readed data mismatch: %q", string(readed))
|
|
|
|
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(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-11-16 18:04:50 +00:00
|
|
|
assert.Eventually(t, func() bool { return len(common.Connections.GetStats("")) == 0 }, 1*time.Second, 50*time.Millisecond)
|
2022-06-11 08:41:34 +00:00
|
|
|
assert.Eventually(t, func() bool { return common.Connections.GetClientConnections() == 0 }, 1000*time.Millisecond,
|
|
|
|
50*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2022-05-04 17:32:02 +00:00
|
|
|
func TestListDirWithWildcards(t *testing.T) {
|
|
|
|
localUser, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
2022-09-18 11:08:48 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
_, 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)
|
|
|
|
}()
|
|
|
|
|
2022-05-04 17:32:02 +00:00
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
2022-09-18 11:08:48 +00:00
|
|
|
client, err := getFTPClient(user, true, nil, ftp.DialWithDisabledMLSD(true))
|
2022-05-04 17:32:02 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
dir1 := "test.dir"
|
|
|
|
dir2 := "test.dir1"
|
|
|
|
err = client.MakeDir(dir1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.MakeDir(dir2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
2022-05-05 09:27:19 +00:00
|
|
|
fileName := "file[a-z]e.dat"
|
2022-05-04 17:32:02 +00:00
|
|
|
err = ftpUploadFile(testFilePath, fileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
|
|
|
err = ftpDownloadFile(fileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
2022-09-18 11:08:48 +00:00
|
|
|
entries, err := client.List(fileName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Equal(t, fileName, entries[0].Name)
|
|
|
|
nListEntries, err := client.NameList(fileName)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Contains(t, nListEntries, fileName)
|
|
|
|
entries, err = client.List(".")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 3)
|
|
|
|
nListEntries, err = client.NameList(".")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, nListEntries, 3)
|
|
|
|
entries, err = client.List("/test.*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 2)
|
|
|
|
found := 0
|
|
|
|
for _, e := range entries {
|
|
|
|
switch e.Name {
|
|
|
|
case dir1, dir2:
|
|
|
|
found++
|
|
|
|
}
|
2022-05-04 17:32:02 +00:00
|
|
|
}
|
2022-09-18 11:08:48 +00:00
|
|
|
assert.Equal(t, 2, found)
|
|
|
|
nListEntries, err = client.NameList("/test.*")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 2)
|
|
|
|
assert.Contains(t, nListEntries, dir1)
|
|
|
|
assert.Contains(t, nListEntries, dir2)
|
|
|
|
entries, err = client.List("/*.dir?")
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Len(t, entries, 1)
|
|
|
|
assert.Equal(t, dir2, entries[0].Name)
|
|
|
|
nListEntries, err = client.NameList("/*.dir?")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Contains(t, nListEntries, dir2)
|
|
|
|
entries, err = client.List("/test.???")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Equal(t, dir1, entries[0].Name)
|
|
|
|
nListEntries, err = client.NameList("/test.???")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Contains(t, nListEntries, dir1)
|
2022-05-04 17:32:02 +00:00
|
|
|
_, err = client.NameList("/missingdir/test.*")
|
|
|
|
assert.Error(t, err)
|
2022-09-18 11:08:48 +00:00
|
|
|
_, err = client.List("/missingdir/test.*")
|
|
|
|
assert.Error(t, err)
|
2022-05-04 17:32:02 +00:00
|
|
|
_, err = client.NameList("test[-]")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), path.ErrBadPattern.Error())
|
|
|
|
}
|
2022-09-18 11:08:48 +00:00
|
|
|
_, err = client.List("test[-]")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), path.ErrBadPattern.Error())
|
|
|
|
}
|
2022-05-04 17:32:02 +00:00
|
|
|
subDir := path.Join(dir1, "sub.d")
|
|
|
|
err = client.MakeDir(subDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.ChangeDir(path.Dir(subDir))
|
|
|
|
assert.NoError(t, err)
|
2022-09-18 11:08:48 +00:00
|
|
|
entries, err = client.List("sub.?")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Contains(t, path.Base(subDir), entries[0].Name)
|
|
|
|
nListEntries, err = client.NameList("sub.?")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Contains(t, nListEntries, path.Base(subDir))
|
|
|
|
entries, err = client.List("../*.dir?")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Equal(t, path.Join("../", dir2), entries[0].Name)
|
|
|
|
nListEntries, err = client.NameList("../*.dir?")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Contains(t, nListEntries, path.Join("../", dir2))
|
|
|
|
|
2022-05-04 17:32:02 +00:00
|
|
|
err = client.ChangeDir("/")
|
|
|
|
assert.NoError(t, err)
|
2022-09-18 11:08:48 +00:00
|
|
|
entries, err = client.List(path.Join(dir1, "sub.*"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Equal(t, path.Join(dir1, "sub.d"), entries[0].Name)
|
|
|
|
nListEntries, err = client.NameList(path.Join(dir1, "sub.*"))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, entries, 1)
|
|
|
|
assert.Contains(t, nListEntries, path.Join(dir1, "sub.d"))
|
2022-05-04 17:32:02 +00:00
|
|
|
err = client.RemoveDir(subDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.RemoveDir(dir1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.RemoveDir(dir2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 11:44:56 +00:00
|
|
|
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)
|
2022-05-04 17:32:02 +00:00
|
|
|
if assert.Len(t, entries, 1) {
|
|
|
|
assert.Equal(t, testFileName, entries[0].Name)
|
|
|
|
}
|
2022-03-03 11:44:56 +00:00
|
|
|
entries, err = client.List("/")
|
|
|
|
assert.NoError(t, err)
|
2022-05-04 17:32:02 +00:00
|
|
|
if assert.Len(t, entries, 1) {
|
|
|
|
assert.Equal(t, "start", entries[0].Name)
|
|
|
|
}
|
2022-03-03 11:44:56 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:54:25 +00:00
|
|
|
func TestLoginEmptyPassword(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Password = ""
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = emptyPwdPlaceholder
|
|
|
|
|
|
|
|
_, err = getFTPClient(user, true, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnonymousUser(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Password = ""
|
|
|
|
u.Filters.IsAnonymous = true
|
|
|
|
_, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.Error(t, err)
|
|
|
|
user, _, err := httpdtest.GetUserByUsername(u.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, user.Filters.IsAnonymous)
|
|
|
|
assert.Equal(t, []string{dataprovider.PermListItems, dataprovider.PermDownload}, user.Permissions["/"])
|
|
|
|
assert.Equal(t, []string{common.ProtocolSSH, common.ProtocolHTTP}, user.Filters.DeniedProtocols)
|
|
|
|
assert.Equal(t, []string{dataprovider.SSHLoginMethodPublicKey, dataprovider.SSHLoginMethodPassword,
|
|
|
|
dataprovider.SSHLoginMethodKeyboardInteractive, dataprovider.SSHLoginMethodKeyAndPassword,
|
|
|
|
dataprovider.SSHLoginMethodKeyAndKeyboardInt, dataprovider.LoginMethodTLSCertificate,
|
|
|
|
dataprovider.LoginMethodTLSCertificateAndPwd}, user.Filters.DeniedLoginMethods)
|
|
|
|
|
|
|
|
user.Password = emptyPwdPlaceholder
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "permission")
|
|
|
|
}
|
|
|
|
err = os.Rename(testFilePath, filepath.Join(user.GetHomeDir(), testFileName))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.MakeDir("adir")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "permission")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAnonymousGroupInheritance(t *testing.T) {
|
|
|
|
g := getTestGroup()
|
|
|
|
g.UserSettings.Filters.IsAnonymous = true
|
|
|
|
g.UserSettings.Permissions = make(map[string][]string)
|
|
|
|
g.UserSettings.Permissions["/"] = allPerms
|
|
|
|
g.UserSettings.Permissions["/testsub"] = allPerms
|
|
|
|
group, _, err := httpdtest.AddGroup(g, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u := getTestUser()
|
|
|
|
u.Groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: group.Name,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
user.Password = emptyPwdPlaceholder
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "permission")
|
|
|
|
}
|
|
|
|
err = client.MakeDir("adir")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "permission")
|
|
|
|
}
|
|
|
|
err = client.MakeDir("/testsub/adir")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "permission")
|
|
|
|
}
|
|
|
|
err = os.Rename(testFilePath, filepath.Join(user.GetHomeDir(), testFileName))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
user.Password = defaultPassword
|
|
|
|
client, err = getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveGroup(group, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
func TestMultiFactorAuth(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2023-11-18 19:06:31 +00:00
|
|
|
configName, key, _, err := mfa.GenerateTOTPSecret(mfa.GetAvailableTOTPConfigNames()[0], user.Username)
|
2021-09-04 10:11:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
2022-01-06 09:11:47 +00:00
|
|
|
user.Filters.TOTPConfig = dataprovider.UserTOTPConfig{
|
2021-09-04 10:11:04 +00:00
|
|
|
Enabled: true,
|
|
|
|
ConfigName: configName,
|
2023-11-18 19:06:31 +00:00
|
|
|
Secret: kms.NewPlainSecret(key.Secret()),
|
2021-09-04 10:11:04 +00:00
|
|
|
Protocols: []string{common.ProtocolFTP},
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateUser(&user, "", "", "")
|
2021-09-04 10:11:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
user.Password = defaultPassword
|
|
|
|
_, err = getFTPClient(user, true, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), dataprovider.ErrInvalidCredentials.Error())
|
|
|
|
}
|
2023-11-18 19:06:31 +00:00
|
|
|
passcode, err := generateTOTPPasscode(key.Secret(), otp.AlgorithmSHA1)
|
2021-09-04 10:11:04 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword + passcode
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
// reusing the same passcode should not work
|
|
|
|
_, err = getFTPClient(user, true, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), dataprovider.ErrInvalidCredentials.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-12-11 16:15:34 +00:00
|
|
|
func TestMustChangePasswordRequirement(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Filters.RequirePasswordChange = true
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFTPClient(user, true, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = dataprovider.UpdateUserPassword(user.Username, defaultPassword, "", "", "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-03-06 15:57:13 +00:00
|
|
|
func TestSecondFactorRequirement(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Filters.TwoFactorAuthProtocols = []string{common.ProtocolFTP}
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFTPClient(user, true, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "second factor authentication is not set")
|
|
|
|
}
|
|
|
|
|
2023-11-18 19:06:31 +00:00
|
|
|
configName, key, _, err := mfa.GenerateTOTPSecret(mfa.GetAvailableTOTPConfigNames()[0], user.Username)
|
2022-03-06 15:57:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.Filters.TOTPConfig = dataprovider.UserTOTPConfig{
|
|
|
|
Enabled: true,
|
|
|
|
ConfigName: configName,
|
2023-11-18 19:06:31 +00:00
|
|
|
Secret: kms.NewPlainSecret(key.Secret()),
|
2022-03-06 15:57:13 +00:00
|
|
|
Protocols: []string{common.ProtocolFTP},
|
|
|
|
}
|
2022-12-03 10:45:27 +00:00
|
|
|
err = dataprovider.UpdateUser(&user, "", "", "")
|
2022-03-06 15:57:13 +00:00
|
|
|
assert.NoError(t, err)
|
2023-11-18 19:06:31 +00:00
|
|
|
passcode, err := generateTOTPPasscode(key.Secret(), otp.AlgorithmSHA1)
|
2022-03-06 15:57:13 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword + passcode
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-04-26 19:53:09 +00:00
|
|
|
func TestLoginInvalidCredentials(t *testing.T) {
|
2020-07-29 19:56:56 +00:00
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-04-26 17:48:21 +00:00
|
|
|
user.Username = "wrong username"
|
2021-02-28 11:10:40 +00:00
|
|
|
_, err = getFTPClient(user, false, nil)
|
2021-04-26 17:48:21 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), dataprovider.ErrInvalidCredentials.Error())
|
|
|
|
}
|
|
|
|
user.Username = u.Username
|
|
|
|
user.Password = "wrong pwd"
|
|
|
|
_, err = getFTPClient(user, false, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), dataprovider.ErrInvalidCredentials.Error())
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
func TestLoginNonExistentUser(t *testing.T) {
|
|
|
|
user := getTestUser()
|
2021-02-28 11:10:40 +00:00
|
|
|
_, err := getFTPClient(user, false, nil)
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2022-07-26 16:51:39 +00:00
|
|
|
func TestFTPSecurity(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Filters.FTPSecurity = 1
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
_, err = getFTPClient(user, false, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "TLS is required")
|
|
|
|
}
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGroupFTPSecurity(t *testing.T) {
|
|
|
|
g := getTestGroup()
|
|
|
|
g.UserSettings.Filters.FTPSecurity = 1
|
|
|
|
group, _, err := httpdtest.AddGroup(g, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u := getTestUser()
|
|
|
|
u.Groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: group.Name,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
_, err = getFTPClient(user, false, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "TLS is required")
|
|
|
|
}
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveGroup(group, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestLoginExternalAuth(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
u := getTestUser()
|
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf := config.GetProviderConf()
|
2022-07-19 20:25:00 +00:00
|
|
|
err = os.WriteFile(extAuthPath, getExtAuthScriptContent(u), os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf.ExternalAuthHook = extAuthPath
|
|
|
|
providerConf.ExternalAuthScope = 0
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2022-04-25 13:49:11 +00:00
|
|
|
g := getTestGroup()
|
|
|
|
g.UserSettings.Filters.DeniedProtocols = []string{common.ProtocolFTP}
|
|
|
|
group, _, err := httpdtest.AddGroup(g, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(u, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2022-04-25 13:49:11 +00:00
|
|
|
u.Groups = []sdk.GroupMapping{
|
|
|
|
{
|
|
|
|
Name: group.Name,
|
|
|
|
Type: sdk.GroupTypePrimary,
|
|
|
|
},
|
|
|
|
}
|
2022-07-19 20:25:00 +00:00
|
|
|
err = os.WriteFile(extAuthPath, getExtAuthScriptContent(u), os.ModePerm)
|
2022-04-25 13:49:11 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFTPClient(u, true, nil)
|
|
|
|
if !assert.Error(t, err) {
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
assert.Contains(t, err.Error(), "protocol FTP is not allowed")
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Groups = nil
|
2022-07-19 20:25:00 +00:00
|
|
|
err = os.WriteFile(extAuthPath, getExtAuthScriptContent(u), os.ModePerm)
|
2022-04-25 13:49:11 +00:00
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
u.Username = defaultUsername + "1"
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(u, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if !assert.Error(t, err) {
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
2022-04-25 13:49:11 +00:00
|
|
|
} else {
|
|
|
|
assert.Contains(t, err.Error(), "invalid credentials")
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.GetUserByUsername(defaultUsername, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
assert.Equal(t, defaultUsername, user.Username)
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-04-25 13:49:11 +00:00
|
|
|
_, err = httpdtest.RemoveGroup(group, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf = config.GetProviderConf()
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(extAuthPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreLoginHook(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
u := getTestUser()
|
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf := config.GetProviderConf()
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(preLoginPath, getPreLoginScriptContent(u, false), os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf.PreLoginHook = preLoginPath
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, _, err = httpdtest.GetUserByUsername(defaultUsername, http.StatusNotFound)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(u, false, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.GetUserByUsername(defaultUsername, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// test login with an existing user
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(preLoginPath, getPreLoginScriptContent(user, true), os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(u, false, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if !assert.Error(t, err) {
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
user.Status = 0
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(preLoginPath, getPreLoginScriptContent(user, false), os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(u, false, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if !assert.Error(t, err, "pre-login script returned a disabled user, login must fail") {
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2022-07-26 16:51:39 +00:00
|
|
|
user.Status = 0
|
|
|
|
user.Filters.FTPSecurity = 1
|
|
|
|
err = os.WriteFile(preLoginPath, getPreLoginScriptContent(user, false), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err = getFTPClient(u, true, nil)
|
|
|
|
if !assert.Error(t, err) {
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
_, err = getFTPClient(user, false, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "TLS is required")
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf = config.GetProviderConf()
|
2021-01-17 21:29:08 +00:00
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(preLoginPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-07-27 16:54:25 +00:00
|
|
|
func TestPreLoginHookReturningAnonymousUser(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
u := getTestUser()
|
|
|
|
u.Filters.IsAnonymous = true
|
|
|
|
u.Filters.DeniedProtocols = []string{common.ProtocolSSH}
|
|
|
|
u.Password = ""
|
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
err = os.WriteFile(preLoginPath, getPreLoginScriptContent(u, false), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf.PreLoginHook = preLoginPath
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// the pre-login hook create the anonymous user
|
|
|
|
client, err := getFTPClient(u, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = client.MakeDir("tdiranonymous")
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "permission")
|
|
|
|
}
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "permission")
|
|
|
|
}
|
|
|
|
err = os.Rename(testFilePath, filepath.Join(u.GetHomeDir(), testFileName))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, _, err := httpdtest.GetUserByUsername(defaultUsername, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, user.Filters.IsAnonymous)
|
|
|
|
assert.Equal(t, []string{dataprovider.PermListItems, dataprovider.PermDownload}, user.Permissions["/"])
|
|
|
|
assert.Equal(t, []string{common.ProtocolSSH, common.ProtocolHTTP}, user.Filters.DeniedProtocols)
|
|
|
|
assert.Equal(t, []string{dataprovider.SSHLoginMethodPublicKey, dataprovider.SSHLoginMethodPassword,
|
|
|
|
dataprovider.SSHLoginMethodKeyboardInteractive, dataprovider.SSHLoginMethodKeyAndPassword,
|
|
|
|
dataprovider.SSHLoginMethodKeyAndKeyboardInt, dataprovider.LoginMethodTLSCertificate,
|
|
|
|
dataprovider.LoginMethodTLSCertificateAndPwd}, user.Filters.DeniedLoginMethods)
|
|
|
|
// now the same with an existing user
|
|
|
|
client, err = getFTPClient(u, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "permission")
|
|
|
|
}
|
|
|
|
err = os.Rename(testFilePath, filepath.Join(u.GetHomeDir(), testFileName))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(preLoginPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-05-26 05:48:37 +00:00
|
|
|
func TestPreDownloadHook(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
oldExecuteOn := common.Config.Actions.ExecuteOn
|
|
|
|
oldHook := common.Config.Actions.Hook
|
|
|
|
|
|
|
|
common.Config.Actions.ExecuteOn = []string{common.OperationPreDownload}
|
|
|
|
common.Config.Actions.Hook = preDownloadPath
|
|
|
|
|
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.WriteFile(preDownloadPath, getExitCodeScriptContent(0), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
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)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
// now return an error from the pre-download hook
|
|
|
|
err = os.WriteFile(preDownloadPath, getExitCodeScriptContent(1), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err = getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
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)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "permission denied")
|
|
|
|
}
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
common.Config.Actions.ExecuteOn = oldExecuteOn
|
|
|
|
common.Config.Actions.Hook = oldHook
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreUploadHook(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
oldExecuteOn := common.Config.Actions.ExecuteOn
|
|
|
|
oldHook := common.Config.Actions.Hook
|
|
|
|
|
|
|
|
common.Config.Actions.ExecuteOn = []string{common.OperationPreUpload}
|
|
|
|
common.Config.Actions.Hook = preUploadPath
|
|
|
|
|
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.WriteFile(preUploadPath, getExitCodeScriptContent(0), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
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)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
// now return an error from the pre-upload hook
|
|
|
|
err = os.WriteFile(preUploadPath, getExitCodeScriptContent(1), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err = getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
if assert.Error(t, err) {
|
2021-06-28 17:40:04 +00:00
|
|
|
assert.Contains(t, err.Error(), ftpserver.ErrFileNameNotAllowed.Error())
|
2021-05-26 05:48:37 +00:00
|
|
|
}
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName+"1", testFileSize, client, 0)
|
|
|
|
if assert.Error(t, err) {
|
2021-06-28 17:40:04 +00:00
|
|
|
assert.Contains(t, err.Error(), ftpserver.ErrFileNameNotAllowed.Error())
|
2021-05-26 05:48:37 +00:00
|
|
|
}
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
common.Config.Actions.ExecuteOn = oldExecuteOn
|
|
|
|
common.Config.Actions.Hook = oldHook
|
|
|
|
}
|
|
|
|
|
2020-07-30 20:33:49 +00:00
|
|
|
func TestPostConnectHook(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
common.Config.PostConnectHook = postConnectPath
|
|
|
|
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-30 20:33:49 +00:00
|
|
|
assert.NoError(t, err)
|
2021-05-26 05:48:37 +00:00
|
|
|
err = os.WriteFile(postConnectPath, getExitCodeScriptContent(0), os.ModePerm)
|
2020-07-30 20:33:49 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-07-30 20:33:49 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-05-26 05:48:37 +00:00
|
|
|
err = os.WriteFile(postConnectPath, getExitCodeScriptContent(1), os.ModePerm)
|
2020-07-30 20:33:49 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, true, nil)
|
2020-07-30 20:33:49 +00:00
|
|
|
if !assert.Error(t, err) {
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
common.Config.PostConnectHook = "http://127.0.0.1:8079/healthz"
|
2020-07-30 20:33:49 +00:00
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, false, nil)
|
2020-07-30 20:33:49 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
common.Config.PostConnectHook = "http://127.0.0.1:8079/notfound"
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, true, nil)
|
2020-07-30 20:33:49 +00:00
|
|
|
if !assert.Error(t, err) {
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-30 20:33:49 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
common.Config.PostConnectHook = ""
|
|
|
|
}
|
|
|
|
|
2021-05-11 06:04:57 +00:00
|
|
|
//nolint:dupl
|
2020-12-15 18:29:30 +00:00
|
|
|
func TestMaxConnections(t *testing.T) {
|
|
|
|
oldValue := common.Config.MaxTotalConnections
|
|
|
|
common.Config.MaxTotalConnections = 1
|
|
|
|
|
2021-05-11 06:04:57 +00:00
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
return common.Connections.GetClientConnections() == 0
|
|
|
|
}, 1000*time.Millisecond, 50*time.Millisecond)
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
user := getTestUser()
|
2022-12-03 10:45:27 +00:00
|
|
|
err := dataprovider.AddUser(&user, "", "", "")
|
2020-12-15 18:29:30 +00:00
|
|
|
assert.NoError(t, err)
|
2021-05-11 04:54:06 +00:00
|
|
|
user.Password = ""
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-12-15 18:29:30 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
_, err = getFTPClient(user, false, nil)
|
2020-12-15 18:29:30 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(user.Username, "", "", "")
|
2020-12-15 18:29:30 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
common.Config.MaxTotalConnections = oldValue
|
|
|
|
}
|
|
|
|
|
2021-05-11 06:04:57 +00:00
|
|
|
//nolint:dupl
|
2021-05-08 17:45:21 +00:00
|
|
|
func TestMaxPerHostConnections(t *testing.T) {
|
|
|
|
oldValue := common.Config.MaxPerHostConnections
|
|
|
|
common.Config.MaxPerHostConnections = 1
|
|
|
|
|
2021-05-11 06:04:57 +00:00
|
|
|
assert.Eventually(t, func() bool {
|
|
|
|
return common.Connections.GetClientConnections() == 0
|
|
|
|
}, 1000*time.Millisecond, 50*time.Millisecond)
|
|
|
|
|
2021-05-11 04:54:06 +00:00
|
|
|
user := getTestUser()
|
2022-12-03 10:45:27 +00:00
|
|
|
err := dataprovider.AddUser(&user, "", "", "")
|
2021-05-08 17:45:21 +00:00
|
|
|
assert.NoError(t, err)
|
2021-05-11 04:54:06 +00:00
|
|
|
user.Password = ""
|
2021-05-08 17:45:21 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFTPClient(user, false, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(user.Username, "", "", "")
|
2021-05-08 17:45:21 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
common.Config.MaxPerHostConnections = oldValue
|
|
|
|
}
|
|
|
|
|
2021-04-18 10:31:06 +00:00
|
|
|
func TestRateLimiter(t *testing.T) {
|
|
|
|
oldConfig := config.GetCommonConfig()
|
|
|
|
|
|
|
|
cfg := config.GetCommonConfig()
|
|
|
|
cfg.DefenderConfig.Enabled = true
|
|
|
|
cfg.DefenderConfig.Threshold = 5
|
2021-05-08 17:45:21 +00:00
|
|
|
cfg.DefenderConfig.ScoreLimitExceeded = 3
|
2021-04-18 10:31:06 +00:00
|
|
|
cfg.RateLimitersConfig = []common.RateLimiterConfig{
|
|
|
|
{
|
|
|
|
Average: 1,
|
|
|
|
Period: 1000,
|
|
|
|
Burst: 1,
|
|
|
|
Type: 2,
|
|
|
|
Protocols: []string{common.ProtocolFTP},
|
|
|
|
GenerateDefenderEvents: true,
|
|
|
|
EntriesSoftLimit: 100,
|
|
|
|
EntriesHardLimit: 150,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
err := common.Initialize(cfg, 0)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
client, err := getFTPClient(user, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = getFTPClient(user, true, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "rate limit exceed")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = getFTPClient(user, false, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "rate limit exceed")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = getFTPClient(user, true, nil)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "banned client IP")
|
|
|
|
}
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(user.Username, "", "", "")
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
err = common.Initialize(oldConfig, 0)
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
func TestDefender(t *testing.T) {
|
|
|
|
oldConfig := config.GetCommonConfig()
|
|
|
|
|
|
|
|
cfg := config.GetCommonConfig()
|
|
|
|
cfg.DefenderConfig.Enabled = true
|
2023-01-25 20:57:27 +00:00
|
|
|
cfg.DefenderConfig.Threshold = 4
|
2021-05-08 17:45:21 +00:00
|
|
|
cfg.DefenderConfig.ScoreLimitExceeded = 2
|
2023-01-25 20:57:27 +00:00
|
|
|
cfg.DefenderConfig.ScoreNoAuth = 1
|
|
|
|
cfg.DefenderConfig.ScoreValid = 1
|
2021-01-02 13:05:09 +00:00
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
err := common.Initialize(cfg, 0)
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2021-01-02 13:05:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2023-01-25 20:57:27 +00:00
|
|
|
// just dial without login
|
|
|
|
ftpOptions := []ftp.DialOption{ftp.DialWithTimeout(5 * time.Second)}
|
|
|
|
client, err = ftp.Dial(ftpServerAddr, ftpOptions...)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
hosts, _, err := httpdtest.GetDefenderHosts(http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, hosts, 1) {
|
|
|
|
host := hosts[0]
|
|
|
|
assert.Empty(t, host.GetBanTime())
|
|
|
|
assert.Equal(t, 1, host.Score)
|
|
|
|
}
|
|
|
|
user.Password = "wrong_pwd"
|
|
|
|
_, err = getFTPClient(user, false, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
hosts, _, err = httpdtest.GetDefenderHosts(http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, hosts, 1) {
|
|
|
|
host := hosts[0]
|
|
|
|
assert.Empty(t, host.GetBanTime())
|
|
|
|
assert.Equal(t, 2, host.Score)
|
|
|
|
}
|
2021-01-02 13:05:09 +00:00
|
|
|
|
2023-01-25 20:57:27 +00:00
|
|
|
for i := 0; i < 2; i++ {
|
2021-02-28 11:10:40 +00:00
|
|
|
_, err = getFTPClient(user, false, nil)
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = defaultPassword
|
2021-02-28 11:10:40 +00:00
|
|
|
_, err = getFTPClient(user, false, nil)
|
2021-01-02 13:05:09 +00:00
|
|
|
if assert.Error(t, err) {
|
2021-04-18 10:31:06 +00:00
|
|
|
assert.Contains(t, err.Error(), "banned client IP")
|
2021-01-02 13:05:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 18:04:50 +00:00
|
|
|
err = dataprovider.DeleteUser(user.Username, "", "", "")
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
err = common.Initialize(oldConfig, 0)
|
2021-01-02 13:05:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestMaxSessions(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.MaxSessions = 1
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
_, err = getFTPClient(user, false, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.Error(t, err)
|
2020-09-01 14:10:26 +00:00
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestZeroBytesTransfers(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, useTLS := range []bool{true, false} {
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, useTLS, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFileName := "testfilename"
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, "empty_download")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(localDownloadPath, []byte(""), os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(localDownloadPath, testFileName, 0, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
size, err := client.FileSize(testFileName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(0), size)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoFileExists(t, localDownloadPath)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, 0, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.FileExists(t, localDownloadPath)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-08-11 21:56:10 +00:00
|
|
|
func TestDownloadErrors(t *testing.T) {
|
2020-07-29 19:56:56 +00:00
|
|
|
u := getTestUser()
|
|
|
|
u.QuotaFiles = 1
|
|
|
|
subDir1 := "sub1"
|
|
|
|
subDir2 := "sub2"
|
|
|
|
u.Permissions[path.Join("/", subDir1)] = []string{dataprovider.PermListItems}
|
|
|
|
u.Permissions[path.Join("/", subDir2)] = []string{dataprovider.PermListItems, dataprovider.PermUpload,
|
|
|
|
dataprovider.PermDelete, dataprovider.PermDownload}
|
2021-07-11 13:26:51 +00:00
|
|
|
u.Filters.FilePatterns = []sdk.PatternsFilter{
|
2020-11-15 21:04:48 +00:00
|
|
|
{
|
|
|
|
Path: "/sub2",
|
|
|
|
AllowedPatterns: []string{},
|
2021-05-22 10:28:05 +00:00
|
|
|
DeniedPatterns: []string{"*.jpg", "*.zip"},
|
2020-11-15 21:04:48 +00:00
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath1 := filepath.Join(user.HomeDir, subDir1, "file.zip")
|
|
|
|
testFilePath2 := filepath.Join(user.HomeDir, subDir2, "file.zip")
|
2020-11-15 21:04:48 +00:00
|
|
|
testFilePath3 := filepath.Join(user.HomeDir, subDir2, "file.jpg")
|
2020-07-29 19:56:56 +00:00
|
|
|
err = os.MkdirAll(filepath.Dir(testFilePath1), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.MkdirAll(filepath.Dir(testFilePath2), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testFilePath1, []byte("file1"), os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testFilePath2, []byte("file2"), os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testFilePath3, []byte("file3"), os.ModePerm)
|
2020-11-15 21:04:48 +00:00
|
|
|
assert.NoError(t, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ftpDownloadFile(path.Join("/", subDir1, "file.zip"), localDownloadPath, 5, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = ftpDownloadFile(path.Join("/", subDir2, "file.zip"), localDownloadPath, 5, client, 0)
|
|
|
|
assert.Error(t, err)
|
2020-11-15 21:04:48 +00:00
|
|
|
err = ftpDownloadFile(path.Join("/", subDir2, "file.jpg"), localDownloadPath, 5, client, 0)
|
|
|
|
assert.Error(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ftpDownloadFile("/missing.zip", localDownloadPath, 5, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUploadErrors(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.QuotaSize = 65535
|
|
|
|
subDir1 := "sub1"
|
|
|
|
subDir2 := "sub2"
|
|
|
|
u.Permissions[path.Join("/", subDir1)] = []string{dataprovider.PermListItems}
|
|
|
|
u.Permissions[path.Join("/", subDir2)] = []string{dataprovider.PermListItems, dataprovider.PermUpload,
|
|
|
|
dataprovider.PermDelete}
|
2021-07-11 13:26:51 +00:00
|
|
|
u.Filters.FilePatterns = []sdk.PatternsFilter{
|
2020-07-29 19:56:56 +00:00
|
|
|
{
|
2021-05-22 10:28:05 +00:00
|
|
|
Path: "/sub2",
|
|
|
|
AllowedPatterns: []string{},
|
|
|
|
DeniedPatterns: []string{"*.zip"},
|
2020-07-29 19:56:56 +00:00
|
|
|
},
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := user.QuotaSize
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.MakeDir(subDir1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.MakeDir(subDir2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.ChangeDir(subDir1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.ChangeDirToParent()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.ChangeDir(subDir2)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName+".zip", testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.ChangeDir("/")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, subDir1, testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
// overquota
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName+"1", testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Delete(path.Join("/", subDir2, testFileName))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
2020-08-11 21:56:10 +00:00
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-04-03 14:00:55 +00:00
|
|
|
func TestSFTPBuffered(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u = getTestSFTPUser()
|
|
|
|
u.QuotaFiles = 100
|
|
|
|
u.FsConfig.SFTPConfig.BufferSize = 2
|
|
|
|
u.HomeDir = filepath.Join(os.TempDir(), u.Username)
|
|
|
|
sftpUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClient(sftpUser, true, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
expectedQuotaSize := testFileSize
|
|
|
|
expectedQuotaFiles := 1
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// overwrite an existing file
|
|
|
|
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)
|
|
|
|
user, _, err := httpdtest.GetUserByUsername(sftpUser.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedQuotaFiles, user.UsedQuotaFiles)
|
|
|
|
assert.Equal(t, expectedQuotaSize, user.UsedQuotaSize)
|
|
|
|
|
|
|
|
data := []byte("test data")
|
|
|
|
err = os.WriteFile(testFilePath, data, os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, int64(len(data)), client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, int64(len(data)+5), client, 5)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "operation unsupported")
|
|
|
|
}
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, int64(4), client, 5)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
readed, err := os.ReadFile(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, []byte("data"), readed)
|
|
|
|
// try to append to a file, it should fail
|
|
|
|
// now append to a file
|
|
|
|
srcFile, err := os.Open(testFilePath)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = client.Append(testFileName, srcFile)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "operation unsupported")
|
|
|
|
}
|
|
|
|
err = srcFile.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
size, err := client.FileSize(testFileName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(len(data)), size)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, int64(len(data)), client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
err = os.RemoveAll(sftpUser.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestResume(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-16 16:08:14 +00:00
|
|
|
u = getTestUser()
|
|
|
|
u.FsConfig.OSConfig = sdk.OSFsConfig{
|
|
|
|
ReadBufferSize: 1,
|
|
|
|
WriteBufferSize: 1,
|
|
|
|
}
|
|
|
|
u.Username += "_buf"
|
|
|
|
u.HomeDir += "_buf"
|
|
|
|
bufferedUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser, bufferedUser} {
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
2020-12-12 09:31:09 +00:00
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
data := []byte("test data")
|
2021-02-25 20:53:04 +00:00
|
|
|
err = os.WriteFile(testFilePath, data, os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = ftpUploadFile(testFilePath, testFileName, int64(len(data)), client, 0)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = ftpUploadFile(testFilePath, testFileName, int64(len(data)+5), client, 5)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
readed, err := os.ReadFile(filepath.Join(user.GetHomeDir(), testFileName))
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "test test data", string(readed))
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, int64(len(data)), client, 5)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
readed, err = os.ReadFile(localDownloadPath)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.Equal(t, data, readed)
|
|
|
|
err = client.Delete(testFileName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, int64(len(data)), client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// now append to a file
|
|
|
|
srcFile, err := os.Open(testFilePath)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = client.Append(testFileName, srcFile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = srcFile.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
size, err := client.FileSize(testFileName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(2*len(data)), size)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, int64(2*len(data)), client, 0)
|
|
|
|
assert.NoError(t, err)
|
2021-02-25 20:53:04 +00:00
|
|
|
readed, err = os.ReadFile(localDownloadPath)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
expected := append(data, data...)
|
|
|
|
assert.Equal(t, expected, readed)
|
|
|
|
}
|
2022-10-12 09:37:31 +00:00
|
|
|
// append to a new file
|
|
|
|
srcFile, err = os.Open(testFilePath)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
newFileName := testFileName + "_new"
|
|
|
|
err = client.Append(newFileName, srcFile)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = srcFile.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
size, err := client.FileSize(newFileName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, int64(len(data)), size)
|
|
|
|
err = ftpDownloadFile(newFileName, localDownloadPath, int64(len(data)), client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
readed, err = os.ReadFile(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, data, readed)
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2023-05-16 16:08:14 +00:00
|
|
|
_, err = httpdtest.RemoveUser(bufferedUser, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(bufferedUser.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2020-08-19 17:36:12 +00:00
|
|
|
//nolint:dupl
|
|
|
|
func TestDeniedLoginMethod(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Filters.DeniedLoginMethods = []string{dataprovider.LoginMethodPassword}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-08-19 17:36:12 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
_, err = getFTPClient(user, false, nil)
|
2020-08-19 17:36:12 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
user.Filters.DeniedLoginMethods = []string{dataprovider.SSHLoginMethodPublicKey, dataprovider.SSHLoginMethodKeyAndPassword}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-08-19 17:36:12 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-08-19 17:36:12 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.NoError(t, checkBasicFTP(client))
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-08-19 17:36:12 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:dupl
|
2020-08-17 10:49:20 +00:00
|
|
|
func TestDeniedProtocols(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Filters.DeniedProtocols = []string{common.ProtocolFTP}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-08-17 10:49:20 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
_, err = getFTPClient(user, false, nil)
|
2020-08-17 10:49:20 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
user.Filters.DeniedProtocols = []string{common.ProtocolSSH, common.ProtocolWebDAV}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-08-17 10:49:20 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-08-17 10:49:20 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.NoError(t, checkBasicFTP(client))
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-08-17 10:49:20 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestQuotaLimits(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.QuotaFiles = 1
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
u = getTestSFTPUser()
|
|
|
|
u.QuotaFiles = 1
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
testFileSize1 := int64(131072)
|
|
|
|
testFileName1 := "test_file1.dat"
|
|
|
|
testFilePath1 := filepath.Join(homeBasePath, testFileName1)
|
|
|
|
err = createTestFile(testFilePath1, testFileSize1)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
testFileSize2 := int64(32768)
|
|
|
|
testFileName2 := "test_file2.dat"
|
|
|
|
testFilePath2 := filepath.Join(homeBasePath, testFileName2)
|
|
|
|
err = createTestFile(testFilePath2, testFileSize2)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
// test quota files
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
2023-10-20 18:31:17 +00:00
|
|
|
err = ftpUploadFile(testFilePath, testFileName+".quota", testFileSize, client, 0) //nolint:goconst
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName+".quota1", testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Rename(testFileName+".quota", testFileName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
// test quota size
|
|
|
|
user.QuotaSize = testFileSize - 1
|
|
|
|
user.QuotaFiles = 0
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, true, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName+".quota", testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Rename(testFileName, testFileName+".quota")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
// now test quota limits while uploading the current file, we have 1 bytes remaining
|
|
|
|
user.QuotaSize = testFileSize + 1
|
|
|
|
user.QuotaFiles = 0
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, false, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = ftpUploadFile(testFilePath1, testFileName1, testFileSize1, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = client.FileSize(testFileName1)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Rename(testFileName+".quota", testFileName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// overwriting an existing file will work if the resulting size is lesser or equal than the current one
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath2, testFileName, testFileSize2, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath1, testFileName, testFileSize1, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath1, testFileName, testFileSize1, client, 10)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath2, testFileName, testFileSize2, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(testFilePath)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = os.Remove(testFilePath1)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = os.Remove(testFilePath2)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.QuotaFiles = 0
|
|
|
|
user.QuotaSize = 0
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
user.Password = defaultPassword
|
|
|
|
user.QuotaSize = 0
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-08-16 18:17:02 +00:00
|
|
|
func TestUploadMaxSize(t *testing.T) {
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
u := getTestUser()
|
|
|
|
u.Filters.MaxUploadFileSize = testFileSize + 1
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
u = getTestSFTPUser()
|
|
|
|
u.Filters.MaxUploadFileSize = testFileSize + 1
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
testFileSize1 := int64(131072)
|
|
|
|
testFileName1 := "test_file1.dat"
|
|
|
|
testFilePath1 := filepath.Join(homeBasePath, testFileName1)
|
|
|
|
err = createTestFile(testFilePath1, testFileSize1)
|
2020-10-25 07:52:31 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = ftpUploadFile(testFilePath1, testFileName1, testFileSize1, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
// now test overwrite an existing file with a size bigger than the allowed one
|
|
|
|
err = createTestFile(filepath.Join(user.GetHomeDir(), testFileName1), testFileSize1)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath1, testFileName1, testFileSize1, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testFilePath1)
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
user.Password = defaultPassword
|
|
|
|
user.Filters.MaxUploadFileSize = 65536000
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
2020-08-16 18:17:02 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestLoginWithIPilters(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Filters.DeniedIP = []string{"192.167.0.0/24", "172.18.0.0/16"}
|
|
|
|
u.Filters.AllowedIP = []string{"172.19.0.0/16"}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if !assert.Error(t, err) {
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-10-22 08:42:40 +00:00
|
|
|
func TestLoginWithDatabaseCredentials(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-07-11 13:26:51 +00:00
|
|
|
u.FsConfig.Provider = sdk.GCSFilesystemProvider
|
2020-10-22 08:42:40 +00:00
|
|
|
u.FsConfig.GCSConfig.Bucket = "test"
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.GCSConfig.Credentials = kms.NewPlainSecret(`{ "type": "service_account" }`)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-10-22 08:42:40 +00:00
|
|
|
assert.NoError(t, err)
|
2022-01-06 09:11:47 +00:00
|
|
|
assert.Equal(t, sdkkms.SecretStatusSecretBox, user.FsConfig.GCSConfig.Credentials.GetStatus())
|
2020-11-30 20:46:34 +00:00
|
|
|
assert.NotEmpty(t, user.FsConfig.GCSConfig.Credentials.GetPayload())
|
|
|
|
assert.Empty(t, user.FsConfig.GCSConfig.Credentials.GetAdditionalData())
|
|
|
|
assert.Empty(t, user.FsConfig.GCSConfig.Credentials.GetKey())
|
2020-10-22 08:42:40 +00:00
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-10-22 08:42:40 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-10-22 08:42:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestLoginInvalidFs(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-07-11 13:26:51 +00:00
|
|
|
u.FsConfig.Provider = sdk.GCSFilesystemProvider
|
2020-07-29 19:56:56 +00:00
|
|
|
u.FsConfig.GCSConfig.Bucket = "test"
|
2020-11-30 20:46:34 +00:00
|
|
|
u.FsConfig.GCSConfig.Credentials = kms.NewPlainSecret("invalid JSON for credentials")
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-10-22 08:42:40 +00:00
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if !assert.Error(t, err) {
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientClose(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
2022-11-16 18:04:50 +00:00
|
|
|
stats := common.Connections.GetStats("")
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.Len(t, stats, 1) {
|
2022-11-16 18:04:50 +00:00
|
|
|
common.Connections.Close(stats[0].ConnectionID, "")
|
|
|
|
assert.Eventually(t, func() bool { return len(common.Connections.GetStats("")) == 0 },
|
2020-07-29 19:56:56 +00:00
|
|
|
1*time.Second, 50*time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRename(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
|
|
|
testDir := "adir"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = client.MakeDir(testDir)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = client.Rename(testFileName, path.Join("missing", testFileName))
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.Error(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = client.Rename(testFileName, path.Join(testDir, testFileName))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
size, err := client.FileSize(path.Join(testDir, testFileName))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testFileSize, size)
|
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
otherDir := "dir"
|
|
|
|
err = client.MakeDir(otherDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.MakeDir(path.Join(otherDir, testDir))
|
|
|
|
assert.NoError(t, err)
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand(fmt.Sprintf("SITE CHMOD 0001 %v", otherDir))
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusCommandOK, code)
|
|
|
|
assert.Equal(t, "SITE CHMOD command successful", response)
|
|
|
|
err = client.Rename(testDir, path.Join(otherDir, testDir))
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err = client.SendCommand(fmt.Sprintf("SITE CHMOD 755 %v", otherDir))
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusCommandOK, code)
|
|
|
|
assert.Equal(t, "SITE CHMOD command successful", response)
|
|
|
|
}
|
|
|
|
err = client.Quit()
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
user.Permissions[path.Join("/", testDir)] = []string{dataprovider.PermListItems}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, false, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = client.Rename(path.Join(testDir, testFileName), testFileName)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(testFilePath)
|
2020-09-01 14:10:26 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = allPerms
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-08-15 11:02:25 +00:00
|
|
|
func TestSymlink(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
2020-12-12 09:31:09 +00:00
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
2024-02-15 19:53:56 +00:00
|
|
|
code, _, err := client.SendCommand(fmt.Sprintf("SITE SYMLINK %v %v", testFileName, testFileName+".link"))
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusCommandOK, code)
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
if runtime.GOOS != osWindows {
|
|
|
|
testDir := "adir"
|
|
|
|
otherDir := "dir"
|
|
|
|
err = client.MakeDir(otherDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.MakeDir(path.Join(otherDir, testDir))
|
|
|
|
assert.NoError(t, err)
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand(fmt.Sprintf("SITE CHMOD 0001 %v", otherDir))
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusCommandOK, code)
|
|
|
|
assert.Equal(t, "SITE CHMOD command successful", response)
|
2024-02-15 19:53:56 +00:00
|
|
|
code, _, err = client.SendCommand(fmt.Sprintf("SITE SYMLINK %v %v", testDir, path.Join(otherDir, testDir)))
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFileUnavailable, code)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err = client.SendCommand(fmt.Sprintf("SITE CHMOD 755 %v", otherDir))
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusCommandOK, code)
|
|
|
|
assert.Equal(t, "SITE CHMOD command successful", response)
|
|
|
|
}
|
|
|
|
err = client.Quit()
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
2020-08-15 11:02:25 +00:00
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
err = os.Remove(testFilePath)
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
2020-12-12 09:31:09 +00:00
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
2020-08-15 11:02:25 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestStat(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Permissions["/subdir"] = []string{dataprovider.PermUpload}
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
subDir := "subdir"
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.MakeDir(subDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, path.Join("/", subDir, testFileName), testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
size, err := client.FileSize(testFileName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, testFileSize, size)
|
|
|
|
_, err = client.FileSize(path.Join("/", subDir, testFileName))
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, err = client.FileSize("missing file")
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUploadOverwriteVfolder(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
vdir := "/vdir"
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vdir")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := filepath.Base(mappedPath)
|
2023-07-23 16:48:49 +00:00
|
|
|
f := vfs.BaseVirtualFolder{
|
|
|
|
Name: folderName,
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
}
|
|
|
|
_, _, err := httpdtest.AddFolder(f, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2023-07-23 16:48:49 +00:00
|
|
|
Name: folderName,
|
2020-07-29 19:56:56 +00:00
|
|
|
},
|
|
|
|
VirtualPath: vdir,
|
|
|
|
QuotaSize: -1,
|
|
|
|
QuotaFiles: -1,
|
|
|
|
})
|
2023-07-23 16:48:49 +00:00
|
|
|
err = os.MkdirAll(mappedPath, os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, path.Join(vdir, testFileName), testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err := httpdtest.GetFolderByName(folderName, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, testFileSize, folder.UsedQuotaSize)
|
|
|
|
assert.Equal(t, 1, folder.UsedQuotaFiles)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = ftpUploadFile(testFilePath, path.Join(vdir, testFileName), testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
folder, _, err = httpdtest.GetFolderByName(folderName, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
assert.Equal(t, testFileSize, folder.UsedQuotaSize)
|
|
|
|
assert.Equal(t, 1, folder.UsedQuotaFiles)
|
2020-07-29 19:56:56 +00:00
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{Name: folderName}, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(mappedPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
func TestTransferQuotaLimits(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.DownloadDataTransfer = 1
|
|
|
|
u.UploadDataTransfer = 1
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(524288)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClient(user, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), ftpserver.ErrStorageExceeded.Error())
|
|
|
|
}
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), common.ErrReadQuotaExceeded.Error())
|
|
|
|
}
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testFileSize = int64(600000)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.DownloadDataTransfer = 2
|
|
|
|
user.UploadDataTransfer = 2
|
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err = getFTPClient(user, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-12-25 10:14:08 +00:00
|
|
|
func TestAllocateAvailable(t *testing.T) {
|
2020-07-29 19:56:56 +00:00
|
|
|
u := getTestUser()
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "vdir")
|
2021-02-01 18:04:15 +00:00
|
|
|
folderName := filepath.Base(mappedPath)
|
2023-07-23 16:48:49 +00:00
|
|
|
f := vfs.BaseVirtualFolder{
|
|
|
|
Name: folderName,
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
}
|
|
|
|
_, _, err := httpdtest.AddFolder(f, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2023-07-23 16:48:49 +00:00
|
|
|
Name: folderName,
|
2020-07-29 19:56:56 +00:00
|
|
|
},
|
|
|
|
VirtualPath: "/vdir",
|
|
|
|
QuotaSize: 110,
|
|
|
|
})
|
2023-07-23 16:48:49 +00:00
|
|
|
err = os.MkdirAll(mappedPath, os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("allo 2000000")
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusCommandOK, code)
|
|
|
|
assert.Equal(t, "Done !", response)
|
2020-12-25 10:14:08 +00:00
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err = client.SendCommand("AVBL /vdir")
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Equal(t, "110", response)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, _, err = client.SendCommand("AVBL")
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
user.QuotaSize = 100
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, false, nil)
|
2020-07-29 19:56:56 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := user.QuotaSize - 1
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("allo 1000")
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusCommandOK, code)
|
|
|
|
assert.Equal(t, "Done !", response)
|
|
|
|
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
2020-12-25 10:14:08 +00:00
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err = client.SendCommand("AVBL")
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Equal(t, "1", response)
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
err = client.Quit()
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2022-01-30 10:42:36 +00:00
|
|
|
err = os.Remove(testFilePath)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2022-01-30 10:42:36 +00:00
|
|
|
}
|
|
|
|
user.TotalDataTransfer = 1
|
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err = getFTPClient(user, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("AVBL")
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2022-01-30 10:42:36 +00:00
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Equal(t, "1", response)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
2022-01-30 10:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user.TotalDataTransfer = 0
|
|
|
|
user.UploadDataTransfer = 5
|
|
|
|
user.QuotaSize = 6 * 1024 * 1024
|
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err = getFTPClient(user, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("AVBL")
|
2022-01-30 10:42:36 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Equal(t, "5242880", response)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user.TotalDataTransfer = 0
|
|
|
|
user.UploadDataTransfer = 5
|
|
|
|
user.QuotaSize = 0
|
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err = getFTPClient(user, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("AVBL")
|
2022-01-30 10:42:36 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Equal(t, "5242880", response)
|
|
|
|
|
|
|
|
err = client.Quit()
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-08-16 18:17:02 +00:00
|
|
|
user.Filters.MaxUploadFileSize = 100
|
|
|
|
user.QuotaSize = 0
|
2022-01-30 10:42:36 +00:00
|
|
|
user.TotalDataTransfer = 0
|
|
|
|
user.UploadDataTransfer = 0
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, false, nil)
|
2020-08-16 18:17:02 +00:00
|
|
|
if assert.NoError(t, err) {
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("allo 10000")
|
2020-08-16 18:17:02 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusCommandOK, code)
|
|
|
|
assert.Equal(t, "Done !", response)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err = client.SendCommand("AVBL")
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Equal(t, "100", response)
|
|
|
|
|
2020-08-16 18:17:02 +00:00
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-12-25 10:14:08 +00:00
|
|
|
user.QuotaSize = 50
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, false, nil)
|
2020-12-25 10:14:08 +00:00
|
|
|
if assert.NoError(t, err) {
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("AVBL")
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Equal(t, "0", response)
|
|
|
|
}
|
|
|
|
|
|
|
|
user.QuotaSize = 1000
|
|
|
|
user.Filters.MaxUploadFileSize = 1
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, false, nil)
|
2020-12-25 10:14:08 +00:00
|
|
|
if assert.NoError(t, err) {
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("AVBL")
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Equal(t, "1", response)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-01 18:04:15 +00:00
|
|
|
_, err = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{Name: folderName}, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(mappedPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-02-11 18:45:52 +00:00
|
|
|
func TestAvailableSFTPFs(t *testing.T) {
|
2020-12-25 10:14:08 +00:00
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(sftpUser, false, nil)
|
2020-12-25 10:14:08 +00:00
|
|
|
if assert.NoError(t, err) {
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("AVBL /")
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-11 18:45:52 +00:00
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
avblSize, err := strconv.ParseInt(response, 10, 64)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Greater(t, avblSize, int64(0))
|
2020-12-25 10:14:08 +00:00
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-12-25 10:14:08 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestChtimes(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, false, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
mtime := time.Now().Format("20060102150405")
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand(fmt.Sprintf("MFMT %v %v", mtime, testFileName))
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Equal(t, fmt.Sprintf("Modify=%v; %v", mtime, testFileName), response)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2024-03-16 09:30:46 +00:00
|
|
|
func TestSTAT(t *testing.T) {
|
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClient(user, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(131072)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
testDir := "testdir"
|
|
|
|
err = client.MakeDir(testDir)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, path.Join(testDir, testFileName), testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, path.Join(testDir, testFileName+"_1"), testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
code, response, err := client.SendCommand(fmt.Sprintf("STAT %s", testDir))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusDirectory, code)
|
|
|
|
assert.Contains(t, response, fmt.Sprintf("STAT %s", testDir))
|
|
|
|
assert.Contains(t, response, testFileName)
|
|
|
|
assert.Contains(t, response, testFileName+"_1")
|
|
|
|
assert.Contains(t, response, "End")
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-12-17 12:40:36 +00:00
|
|
|
func TestChown(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("chown is not supported on Windows")
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(getTestUser(), http.StatusCreated)
|
2020-12-17 12:40:36 +00:00
|
|
|
assert.NoError(t, err)
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-12-17 12:40:36 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(131072)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand(fmt.Sprintf("SITE CHOWN 1000:1000 %v", testFileName))
|
2020-12-17 12:40:36 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFileUnavailable, code)
|
|
|
|
assert.Equal(t, "Couldn't chown: operation unsupported", response)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-17 12:40:36 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func TestChmod(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("chmod is partially supported on Windows")
|
|
|
|
}
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-12-12 09:31:09 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(131072)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand(fmt.Sprintf("SITE CHMOD 600 %v", testFileName))
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusCommandOK, code)
|
|
|
|
assert.Equal(t, "SITE CHMOD command successful", response)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
fi, err := os.Stat(filepath.Join(user.HomeDir, testFileName))
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
assert.Equal(t, os.FileMode(0600), fi.Mode().Perm())
|
|
|
|
}
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-12-12 09:31:09 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
2020-07-29 19:56:56 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2020-12-24 17:48:06 +00:00
|
|
|
func TestCombineDisabled(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err := getFTPClient(user, true, nil)
|
2020-12-24 17:48:06 +00:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("COMB file file.1 file.2")
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusNotImplemented, code)
|
|
|
|
assert.Equal(t, "COMB support is disabled", response)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestActiveModeDisabled(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClientImplicitTLS(user)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("PORT 10,2,0,2,4,31")
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusNotAvailable, code)
|
|
|
|
assert.Equal(t, "PORT command is disabled", response)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err = client.SendCommand("EPRT |1|132.235.1.2|6275|")
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusNotAvailable, code)
|
|
|
|
assert.Equal(t, "EPRT command is disabled", response)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
client, err = getFTPClient(user, false, nil)
|
2020-12-24 17:48:06 +00:00
|
|
|
if assert.NoError(t, err) {
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("PORT 10,2,0,2,4,31")
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-08-05 16:38:15 +00:00
|
|
|
assert.Equal(t, ftp.StatusBadArguments, code)
|
|
|
|
assert.Equal(t, "Your request does not meet the configured security requirements", response)
|
2020-12-24 17:48:06 +00:00
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err = client.SendCommand("EPRT |1|132.235.1.2|6275|")
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-08-05 16:38:15 +00:00
|
|
|
assert.Equal(t, ftp.StatusBadArguments, code)
|
|
|
|
assert.Equal(t, "Your request does not meet the configured security requirements", response)
|
2020-12-24 17:48:06 +00:00
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSITEDisabled(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClientImplicitTLS(user)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand("SITE CHMOD 600 afile.txt")
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusBadCommand, code)
|
|
|
|
assert.Equal(t, "SITE support is disabled", response)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHASH(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
u = getTestUserWithCryptFs()
|
|
|
|
u.Username += "_crypt"
|
2021-01-17 21:29:08 +00:00
|
|
|
cryptUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser, cryptUser} {
|
|
|
|
client, err := getFTPClientImplicitTLS(user)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(131072)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
h := sha256.New()
|
|
|
|
f, err := os.Open(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = io.Copy(h, f)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
hash := hex.EncodeToString(h.Sum(nil))
|
|
|
|
err = f.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand(fmt.Sprintf("XSHA256 %v", testFileName))
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusRequestedFileActionOK, code)
|
|
|
|
assert.Contains(t, response, hash)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err = client.SendCommand(fmt.Sprintf("HASH %v", testFileName))
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, ftp.StatusFile, code)
|
|
|
|
assert.Contains(t, response, hash)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-24 17:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(cryptUser, http.StatusOK)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(cryptUser.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCombine(t *testing.T) {
|
|
|
|
u := getTestUser()
|
2021-01-17 21:29:08 +00:00
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(getTestSFTPUser(), http.StatusCreated)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
for _, user := range []dataprovider.User{localUser, sftpUser} {
|
|
|
|
client, err := getFTPClientImplicitTLS(user)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(131072)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName+".1", testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName+".2", testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2024-02-15 19:53:56 +00:00
|
|
|
code, response, err := client.SendCommand(fmt.Sprintf("COMB %v %v %v", testFileName, testFileName+".1", testFileName+".2"))
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if user.Username == defaultUsername {
|
|
|
|
assert.Equal(t, ftp.StatusRequestedFileActionOK, code)
|
|
|
|
assert.Equal(t, "COMB succeeded!", response)
|
|
|
|
} else {
|
|
|
|
assert.Equal(t, ftp.StatusFileUnavailable, code)
|
|
|
|
assert.Contains(t, response, "COMB is not supported for this filesystem")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if user.Username == defaultUsername {
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-02-24 15:11:35 +00:00
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
user.Password = defaultPassword
|
|
|
|
user.ID = 0
|
|
|
|
user.CreatedAt = 0
|
|
|
|
_, resp, err := httpdtest.AddUser(user, http.StatusCreated)
|
|
|
|
assert.NoError(t, err, string(resp))
|
2020-12-24 17:48:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(sftpUser, http.StatusOK)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
2021-01-17 21:29:08 +00:00
|
|
|
_, err = httpdtest.RemoveUser(localUser, http.StatusOK)
|
2020-12-24 17:48:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2023-08-20 14:00:36 +00:00
|
|
|
func TestTLSSessionReuse(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
client, err := getFTPClientWithSessionReuse(user, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
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, 1)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// this TLS config does not support session resumption
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "localhost",
|
|
|
|
InsecureSkipVerify: true, // use this for tests only
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
client, err = getFTPClientWithSessionReuse(user, tlsConfig)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
func TestClientCertificateAuthRevokedCert(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Username = tlsClient2Username
|
2021-07-11 13:26:51 +00:00
|
|
|
u.Filters.TLSUsername = sdk.TLSUsernameCN
|
2021-02-28 11:10:40 +00:00
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "localhost",
|
|
|
|
InsecureSkipVerify: true, // use this for tests only
|
|
|
|
MinVersion: tls.VersionTLS12,
|
2023-08-20 14:00:36 +00:00
|
|
|
ClientSessionCache: tls.NewLRUClientSessionCache(0),
|
2021-02-28 11:10:40 +00:00
|
|
|
}
|
|
|
|
tlsCert, err := tls.X509KeyPair([]byte(client2Crt), []byte(client2Key))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, tlsCert)
|
2023-08-20 14:00:36 +00:00
|
|
|
_, err = getFTPClientWithSessionReuse(user, tlsConfig)
|
2021-02-28 11:10:40 +00:00
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "bad certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientCertificateAuth(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Username = tlsClient1Username
|
|
|
|
u.Filters.DeniedLoginMethods = []string{dataprovider.LoginMethodPassword}
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "localhost",
|
|
|
|
InsecureSkipVerify: true, // use this for tests only
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
tlsCert, err := tls.X509KeyPair([]byte(client1Crt), []byte(client1Key))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, tlsCert)
|
|
|
|
// TLS username is not enabled, mutual TLS should fail
|
|
|
|
_, err = getFTPClient(user, true, tlsConfig)
|
|
|
|
if assert.Error(t, err) {
|
2021-03-21 18:15:47 +00:00
|
|
|
assert.Contains(t, err.Error(), "login method password is not allowed")
|
2021-02-28 11:10:40 +00:00
|
|
|
}
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
user.Filters.TLSUsername = sdk.TLSUsernameCN
|
2021-02-28 11:10:40 +00:00
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClient(user, true, tlsConfig)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// now use a valid certificate with a CN different from username
|
|
|
|
u = getTestUser()
|
|
|
|
u.Username = tlsClient2Username
|
2021-07-11 13:26:51 +00:00
|
|
|
u.Filters.TLSUsername = sdk.TLSUsernameCN
|
2021-02-28 11:10:40 +00:00
|
|
|
u.Filters.DeniedLoginMethods = []string{dataprovider.LoginMethodPassword}
|
|
|
|
user2, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFTPClient(user2, true, tlsConfig)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "does not match username")
|
|
|
|
}
|
2024-01-14 20:36:23 +00:00
|
|
|
// add the certs to the user
|
|
|
|
user2.Filters.TLSUsername = sdk.TLSUsernameNone
|
|
|
|
user2.Filters.TLSCerts = []string{client2Crt, client1Crt}
|
|
|
|
user2, _, err = httpdtest.UpdateUser(user2, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err = getFTPClient(user2, true, tlsConfig)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
user2.Filters.TLSCerts = []string{client2Crt}
|
|
|
|
user2, _, err = httpdtest.UpdateUser(user2, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFTPClient(user2, true, tlsConfig)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "TLS certificate is not valid")
|
|
|
|
}
|
2021-02-28 11:10:40 +00:00
|
|
|
|
|
|
|
// now disable certificate authentication
|
|
|
|
user.Filters.DeniedLoginMethods = append(user.Filters.DeniedLoginMethods, dataprovider.LoginMethodTLSCertificate,
|
|
|
|
dataprovider.LoginMethodTLSCertificateAndPwd)
|
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFTPClient(user, true, tlsConfig)
|
|
|
|
if assert.Error(t, err) {
|
2021-03-21 18:15:47 +00:00
|
|
|
assert.Contains(t, err.Error(), "login method TLSCertificate+password is not allowed")
|
2021-02-28 11:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// disable FTP protocol
|
|
|
|
user.Filters.DeniedLoginMethods = []string{dataprovider.LoginMethodPassword}
|
|
|
|
user.Filters.DeniedProtocols = append(user.Filters.DeniedProtocols, common.ProtocolFTP)
|
|
|
|
user, _, err = httpdtest.UpdateUser(user, http.StatusOK, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFTPClient(user, true, tlsConfig)
|
|
|
|
if assert.Error(t, err) {
|
2021-03-21 18:15:47 +00:00
|
|
|
assert.Contains(t, err.Error(), "protocol FTP is not allowed")
|
2021-02-28 11:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user2.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = getFTPClient(user, true, tlsConfig)
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClientCertificateAndPwdAuth(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Username = tlsClient1Username
|
2021-07-11 13:26:51 +00:00
|
|
|
u.Filters.TLSUsername = sdk.TLSUsernameCN
|
2021-02-28 11:10:40 +00:00
|
|
|
u.Filters.DeniedLoginMethods = []string{dataprovider.LoginMethodPassword, dataprovider.LoginMethodTLSCertificate}
|
|
|
|
user, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "localhost",
|
|
|
|
InsecureSkipVerify: true, // use this for tests only
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
tlsCert, err := tls.X509KeyPair([]byte(client1Crt), []byte(client1Key))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, tlsCert)
|
|
|
|
client, err := getFTPClient(user, true, tlsConfig)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = getFTPClient(user, true, nil)
|
|
|
|
if assert.Error(t, err) {
|
2021-03-21 18:15:47 +00:00
|
|
|
assert.Contains(t, err.Error(), "login method password is not allowed")
|
2021-02-28 11:10:40 +00:00
|
|
|
}
|
|
|
|
user.Password = defaultPassword + "1"
|
|
|
|
_, err = getFTPClient(user, true, tlsConfig)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "invalid credentials")
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsCert, err = tls.X509KeyPair([]byte(client2Crt), []byte(client2Key))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig.Certificates = []tls.Certificate{tlsCert}
|
|
|
|
_, err = getFTPClient(user, true, tlsConfig)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "bad certificate")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func TestExternalAuthWithClientCert(t *testing.T) {
|
2021-02-28 11:10:40 +00:00
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
u := getTestUser()
|
|
|
|
u.Username = tlsClient1Username
|
|
|
|
u.Filters.DeniedLoginMethods = append(u.Filters.DeniedLoginMethods, dataprovider.LoginMethodPassword)
|
2021-07-11 13:26:51 +00:00
|
|
|
u.Filters.TLSUsername = sdk.TLSUsernameCN
|
2021-02-28 11:10:40 +00:00
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf := config.GetProviderConf()
|
2022-07-19 20:25:00 +00:00
|
|
|
err = os.WriteFile(extAuthPath, getExtAuthScriptContent(u), os.ModePerm)
|
2021-02-28 11:10:40 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf.ExternalAuthHook = extAuthPath
|
|
|
|
providerConf.ExternalAuthScope = 8
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// external auth not called, auth scope is 8
|
|
|
|
_, err = getFTPClient(u, true, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
_, _, err = httpdtest.GetUserByUsername(u.Username, http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "localhost",
|
|
|
|
InsecureSkipVerify: true, // use this for tests only
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
tlsCert, err := tls.X509KeyPair([]byte(client1Crt), []byte(client1Key))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, tlsCert)
|
|
|
|
client, err := getFTPClient(u, true, tlsConfig)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, _, err := httpdtest.GetUserByUsername(u.Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, u.Username, user.Username)
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
u.Username = tlsClient2Username
|
|
|
|
_, err = getFTPClient(u, true, tlsConfig)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "invalid credentials")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(extAuthPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPreLoginHookWithClientCert(t *testing.T) {
|
|
|
|
if runtime.GOOS == osWindows {
|
|
|
|
t.Skip("this test is not available on Windows")
|
|
|
|
}
|
|
|
|
u := getTestUser()
|
|
|
|
u.Username = tlsClient1Username
|
|
|
|
u.Filters.DeniedLoginMethods = append(u.Filters.DeniedLoginMethods, dataprovider.LoginMethodPassword)
|
2021-07-11 13:26:51 +00:00
|
|
|
u.Filters.TLSUsername = sdk.TLSUsernameCN
|
2021-02-28 11:10:40 +00:00
|
|
|
err := dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf := config.GetProviderConf()
|
|
|
|
err = os.WriteFile(preLoginPath, getPreLoginScriptContent(u, false), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf.PreLoginHook = preLoginPath
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, _, err = httpdtest.GetUserByUsername(tlsClient1Username, http.StatusNotFound)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "localhost",
|
|
|
|
InsecureSkipVerify: true, // use this for tests only
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
tlsCert, err := tls.X509KeyPair([]byte(client1Crt), []byte(client1Key))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
tlsConfig.Certificates = append(tlsConfig.Certificates, tlsCert)
|
|
|
|
client, err := getFTPClient(u, true, tlsConfig)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, _, err := httpdtest.GetUserByUsername(tlsClient1Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// test login with an existing user
|
|
|
|
client, err = getFTPClient(user, true, tlsConfig)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err := client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
u.Username = tlsClient2Username
|
|
|
|
err = os.WriteFile(preLoginPath, getPreLoginScriptContent(u, false), os.ModePerm)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = getFTPClient(u, true, tlsConfig)
|
|
|
|
if assert.Error(t, err) {
|
|
|
|
assert.Contains(t, err.Error(), "does not match username")
|
|
|
|
}
|
|
|
|
|
|
|
|
user2, _, err := httpdtest.GetUserByUsername(tlsClient2Username, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveUser(user2, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user2.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = httpdtest.RemoveUser(user, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(user.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = dataprovider.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = config.LoadConfig(configDir, "")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
providerConf = config.GetProviderConf()
|
|
|
|
err = dataprovider.Initialize(providerConf, configDir, true)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(preLoginPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func TestNestedVirtualFolders(t *testing.T) {
|
|
|
|
u := getTestUser()
|
|
|
|
localUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
u = getTestSFTPUser()
|
|
|
|
mappedPathCrypt := filepath.Join(os.TempDir(), "crypt")
|
|
|
|
folderNameCrypt := filepath.Base(mappedPathCrypt)
|
|
|
|
vdirCryptPath := "/vdir/crypt"
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
|
|
|
Name: folderNameCrypt,
|
|
|
|
},
|
|
|
|
VirtualPath: vdirCryptPath,
|
|
|
|
})
|
|
|
|
mappedPath := filepath.Join(os.TempDir(), "local")
|
|
|
|
folderName := filepath.Base(mappedPath)
|
|
|
|
vdirPath := "/vdir/local"
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2023-07-23 16:48:49 +00:00
|
|
|
Name: folderName,
|
2021-03-21 18:15:47 +00:00
|
|
|
},
|
|
|
|
VirtualPath: vdirPath,
|
|
|
|
})
|
|
|
|
mappedPathNested := filepath.Join(os.TempDir(), "nested")
|
|
|
|
folderNameNested := filepath.Base(mappedPathNested)
|
|
|
|
vdirNestedPath := "/vdir/crypt/nested"
|
|
|
|
u.VirtualFolders = append(u.VirtualFolders, vfs.VirtualFolder{
|
|
|
|
BaseVirtualFolder: vfs.BaseVirtualFolder{
|
2023-07-23 16:48:49 +00:00
|
|
|
Name: folderNameNested,
|
2021-03-21 18:15:47 +00:00
|
|
|
},
|
|
|
|
VirtualPath: vdirNestedPath,
|
|
|
|
QuotaFiles: -1,
|
|
|
|
QuotaSize: -1,
|
|
|
|
})
|
2023-07-23 16:48:49 +00:00
|
|
|
f1 := vfs.BaseVirtualFolder{
|
|
|
|
Name: folderNameCrypt,
|
|
|
|
FsConfig: vfs.Filesystem{
|
|
|
|
Provider: sdk.CryptedFilesystemProvider,
|
|
|
|
CryptConfig: vfs.CryptFsConfig{
|
|
|
|
Passphrase: kms.NewPlainSecret(defaultPassword),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MappedPath: mappedPathCrypt,
|
|
|
|
}
|
|
|
|
_, _, err = httpdtest.AddFolder(f1, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
f2 := vfs.BaseVirtualFolder{
|
|
|
|
Name: folderName,
|
|
|
|
MappedPath: mappedPath,
|
|
|
|
}
|
|
|
|
_, _, err = httpdtest.AddFolder(f2, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
f3 := vfs.BaseVirtualFolder{
|
|
|
|
Name: folderNameNested,
|
|
|
|
MappedPath: mappedPathNested,
|
|
|
|
}
|
|
|
|
_, _, err = httpdtest.AddFolder(f3, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
sftpUser, _, err := httpdtest.AddUser(u, http.StatusCreated)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
client, err := getFTPClient(sftpUser, false, nil)
|
|
|
|
if assert.NoError(t, err) {
|
|
|
|
err = checkBasicFTP(client)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
testFilePath := filepath.Join(homeBasePath, testFileName)
|
|
|
|
testFileSize := int64(65535)
|
|
|
|
err = createTestFile(testFilePath, testFileSize)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
localDownloadPath := filepath.Join(homeBasePath, testDLFileName)
|
|
|
|
|
|
|
|
err = ftpUploadFile(testFilePath, testFileName, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(testFileName, localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, path.Join("/vdir", testFileName), testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(path.Join("/vdir", testFileName), localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, path.Join(vdirPath, testFileName), testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(path.Join(vdirPath, testFileName), localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, path.Join(vdirCryptPath, testFileName), testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(path.Join(vdirCryptPath, testFileName), localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpUploadFile(testFilePath, path.Join(vdirNestedPath, testFileName), testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = ftpDownloadFile(path.Join(vdirNestedPath, testFileName), localDownloadPath, testFileSize, client, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
err = client.Quit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(testFilePath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.Remove(localDownloadPath)
|
|
|
|
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 = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{Name: folderNameCrypt}, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{Name: folderName}, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = httpdtest.RemoveFolder(vfs.BaseVirtualFolder{Name: folderNameNested}, http.StatusOK)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(mappedPathCrypt)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(mappedPath)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(mappedPathNested)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = os.RemoveAll(localUser.GetHomeDir())
|
|
|
|
assert.NoError(t, err)
|
2022-11-16 18:04:50 +00:00
|
|
|
assert.Eventually(t, func() bool { return len(common.Connections.GetStats("")) == 0 }, 1*time.Second, 50*time.Millisecond)
|
2021-05-11 06:04:57 +00:00
|
|
|
assert.Eventually(t, func() bool { return common.Connections.GetClientConnections() == 0 }, 1000*time.Millisecond,
|
|
|
|
50*time.Millisecond)
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func checkBasicFTP(client *ftp.ServerConn) error {
|
|
|
|
_, err := client.CurrentDir()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = client.NoOp()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = client.List(".")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ftpUploadFile(localSourcePath string, remoteDestPath string, expectedSize int64, client *ftp.ServerConn, offset uint64) error {
|
|
|
|
srcFile, err := os.Open(localSourcePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
if offset > 0 {
|
|
|
|
err = client.StorFrom(remoteDestPath, srcFile, offset)
|
|
|
|
} else {
|
|
|
|
err = client.Stor(remoteDestPath, srcFile)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if expectedSize > 0 {
|
|
|
|
size, err := client.FileSize(remoteDestPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if size != expectedSize {
|
|
|
|
return fmt.Errorf("uploaded file size does not match, actual: %v, expected: %v", size, expectedSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ftpDownloadFile(remoteSourcePath string, localDestPath string, expectedSize int64, client *ftp.ServerConn, offset uint64) error {
|
|
|
|
downloadDest, err := os.Create(localDestPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer downloadDest.Close()
|
|
|
|
var r *ftp.Response
|
|
|
|
if offset > 0 {
|
|
|
|
r, err = client.RetrFrom(remoteSourcePath, offset)
|
|
|
|
} else {
|
|
|
|
r, err = client.Retr(remoteSourcePath)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer r.Close()
|
|
|
|
|
|
|
|
written, err := io.Copy(downloadDest, r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if written != expectedSize {
|
|
|
|
return fmt.Errorf("downloaded file size does not match, actual: %v, expected: %v", written, expectedSize)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-24 17:48:06 +00:00
|
|
|
func getFTPClientImplicitTLS(user dataprovider.User) (*ftp.ServerConn, error) {
|
|
|
|
ftpOptions := []ftp.DialOption{ftp.DialWithTimeout(5 * time.Second)}
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
ServerName: "localhost",
|
|
|
|
InsecureSkipVerify: true, // use this for tests only
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
|
|
|
ftpOptions = append(ftpOptions, ftp.DialWithTLS(tlsConfig))
|
|
|
|
ftpOptions = append(ftpOptions, ftp.DialWithDisabledEPSV(true))
|
|
|
|
client, err := ftp.Dial(ftpSrvAddrTLS, ftpOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pwd := defaultPassword
|
|
|
|
if user.Password != "" {
|
|
|
|
pwd = user.Password
|
|
|
|
}
|
|
|
|
err = client.Login(user.Username, pwd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
|
2023-08-20 14:00:36 +00:00
|
|
|
func getFTPClientWithSessionReuse(user dataprovider.User, tlsConfig *tls.Config, dialOptions ...ftp.DialOption,
|
|
|
|
) (*ftp.ServerConn, error) {
|
|
|
|
ftpOptions := []ftp.DialOption{ftp.DialWithTimeout(5 * time.Second)}
|
|
|
|
ftpOptions = append(ftpOptions, dialOptions...)
|
|
|
|
if tlsConfig == nil {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
ServerName: "localhost",
|
|
|
|
InsecureSkipVerify: true, // use this for tests only
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
ClientSessionCache: tls.NewLRUClientSessionCache(0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ftpOptions = append(ftpOptions, ftp.DialWithExplicitTLS(tlsConfig))
|
|
|
|
client, err := ftp.Dial(ftpSrvAddrTLSResumption, ftpOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pwd := defaultPassword
|
|
|
|
if user.Password != "" {
|
|
|
|
if user.Password == emptyPwdPlaceholder {
|
|
|
|
pwd = ""
|
|
|
|
} else {
|
|
|
|
pwd = user.Password
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err = client.Login(user.Username, pwd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
|
2022-09-18 11:08:48 +00:00
|
|
|
func getFTPClient(user dataprovider.User, useTLS bool, tlsConfig *tls.Config, dialOptions ...ftp.DialOption,
|
|
|
|
) (*ftp.ServerConn, error) {
|
2020-07-29 19:56:56 +00:00
|
|
|
ftpOptions := []ftp.DialOption{ftp.DialWithTimeout(5 * time.Second)}
|
2022-09-18 11:08:48 +00:00
|
|
|
ftpOptions = append(ftpOptions, dialOptions...)
|
2020-07-29 19:56:56 +00:00
|
|
|
if useTLS {
|
2021-02-28 11:10:40 +00:00
|
|
|
if tlsConfig == nil {
|
|
|
|
tlsConfig = &tls.Config{
|
|
|
|
ServerName: "localhost",
|
|
|
|
InsecureSkipVerify: true, // use this for tests only
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
ftpOptions = append(ftpOptions, ftp.DialWithExplicitTLS(tlsConfig))
|
|
|
|
}
|
|
|
|
client, err := ftp.Dial(ftpServerAddr, ftpOptions...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pwd := defaultPassword
|
2020-12-24 17:48:06 +00:00
|
|
|
if user.Password != "" {
|
2022-07-27 16:54:25 +00:00
|
|
|
if user.Password == emptyPwdPlaceholder {
|
|
|
|
pwd = ""
|
|
|
|
} else {
|
|
|
|
pwd = user.Password
|
|
|
|
}
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
err = client.Login(user.Username, pwd)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return client, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func waitTCPListening(address string) {
|
|
|
|
for {
|
|
|
|
conn, err := net.Dial("tcp", address)
|
|
|
|
if err != nil {
|
2021-02-14 21:08:08 +00:00
|
|
|
logger.WarnToConsole("tcp server %v not listening: %v", address, err)
|
2020-07-29 19:56:56 +00:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-14 21:08:08 +00:00
|
|
|
logger.InfoToConsole("tcp server %v now listening", address)
|
2020-07-29 19:56:56 +00:00
|
|
|
conn.Close()
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-02 13:05:09 +00:00
|
|
|
func waitNoConnections() {
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
2022-11-16 18:04:50 +00:00
|
|
|
for len(common.Connections.GetStats("")) > 0 {
|
2021-01-02 13:05:09 +00:00
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:49:11 +00:00
|
|
|
func getTestGroup() dataprovider.Group {
|
|
|
|
return dataprovider.Group{
|
|
|
|
BaseGroup: sdk.BaseGroup{
|
|
|
|
Name: "test_group",
|
|
|
|
Description: "test group description",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func getTestUser() dataprovider.User {
|
|
|
|
user := dataprovider.User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
|
|
|
Username: defaultUsername,
|
|
|
|
Password: defaultPassword,
|
|
|
|
HomeDir: filepath.Join(homeBasePath, defaultUsername),
|
|
|
|
Status: 1,
|
|
|
|
ExpirationDate: 0,
|
|
|
|
},
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
|
|
|
user.Permissions = make(map[string][]string)
|
|
|
|
user.Permissions["/"] = allPerms
|
|
|
|
return user
|
|
|
|
}
|
|
|
|
|
2020-12-12 09:31:09 +00:00
|
|
|
func getTestSFTPUser() dataprovider.User {
|
|
|
|
u := getTestUser()
|
|
|
|
u.Username = u.Username + "_sftp"
|
2021-07-11 13:26:51 +00:00
|
|
|
u.FsConfig.Provider = sdk.SFTPFilesystemProvider
|
2020-12-12 09:31:09 +00:00
|
|
|
u.FsConfig.SFTPConfig.Endpoint = sftpServerAddr
|
|
|
|
u.FsConfig.SFTPConfig.Username = defaultUsername
|
|
|
|
u.FsConfig.SFTPConfig.Password = kms.NewPlainSecret(defaultPassword)
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2022-06-11 08:41:34 +00:00
|
|
|
func getTestUserWithHTTPFs() dataprovider.User {
|
|
|
|
u := getTestUser()
|
|
|
|
u.FsConfig.Provider = sdk.HTTPFilesystemProvider
|
|
|
|
u.FsConfig.HTTPConfig = vfs.HTTPFsConfig{
|
|
|
|
BaseHTTPFsConfig: sdk.BaseHTTPFsConfig{
|
|
|
|
Endpoint: fmt.Sprintf("http://127.0.0.1:%d/api/v1", httpFsPort),
|
|
|
|
Username: defaultHTTPFsUsername,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return u
|
|
|
|
}
|
|
|
|
|
2022-07-19 20:25:00 +00:00
|
|
|
func getExtAuthScriptContent(user dataprovider.User) []byte {
|
2020-07-29 19:56:56 +00:00
|
|
|
extAuthContent := []byte("#!/bin/sh\n\n")
|
|
|
|
extAuthContent = append(extAuthContent, []byte(fmt.Sprintf("if test \"$SFTPGO_AUTHD_USERNAME\" = \"%v\"; then\n", user.Username))...)
|
|
|
|
u, _ := json.Marshal(user)
|
2022-07-19 20:25:00 +00:00
|
|
|
extAuthContent = append(extAuthContent, []byte(fmt.Sprintf("echo '%v'\n", string(u)))...)
|
2020-07-29 19:56:56 +00:00
|
|
|
extAuthContent = append(extAuthContent, []byte("else\n")...)
|
2022-07-19 20:25:00 +00:00
|
|
|
extAuthContent = append(extAuthContent, []byte("echo '{\"username\":\"\"}'\n")...)
|
2020-07-29 19:56:56 +00:00
|
|
|
extAuthContent = append(extAuthContent, []byte("fi\n")...)
|
|
|
|
return extAuthContent
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPreLoginScriptContent(user dataprovider.User, nonJSONResponse bool) []byte {
|
|
|
|
content := []byte("#!/bin/sh\n\n")
|
|
|
|
if nonJSONResponse {
|
|
|
|
content = append(content, []byte("echo 'text response'\n")...)
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
if len(user.Username) > 0 {
|
|
|
|
u, _ := json.Marshal(user)
|
|
|
|
content = append(content, []byte(fmt.Sprintf("echo '%v'\n", string(u)))...)
|
|
|
|
}
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2021-05-26 05:48:37 +00:00
|
|
|
func getExitCodeScriptContent(exitCode int) []byte {
|
2020-07-30 20:33:49 +00:00
|
|
|
content := []byte("#!/bin/sh\n\n")
|
|
|
|
content = append(content, []byte(fmt.Sprintf("exit %v", exitCode))...)
|
|
|
|
return content
|
|
|
|
}
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
func createTestFile(path string, size int64) error {
|
|
|
|
baseDir := filepath.Dir(path)
|
2022-05-09 17:09:43 +00:00
|
|
|
if _, err := os.Stat(baseDir); errors.Is(err, fs.ErrNotExist) {
|
2020-07-29 19:56:56 +00:00
|
|
|
err = os.MkdirAll(baseDir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
content := make([]byte, size)
|
|
|
|
_, err := rand.Read(content)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-02-25 20:53:04 +00:00
|
|
|
return os.WriteFile(path, content, os.ModePerm)
|
2020-07-29 19:56:56 +00:00
|
|
|
}
|
2021-02-28 11:10:40 +00:00
|
|
|
|
|
|
|
func writeCerts(certPath, keyPath, caCrtPath, caCRLPath string) error {
|
|
|
|
err := os.WriteFile(certPath, []byte(ftpsCert), os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error writing FTPS certificate: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.WriteFile(keyPath, []byte(ftpsKey), os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error writing FTPS private key: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.WriteFile(caCrtPath, []byte(caCRT), os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error writing FTPS CA crt: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = os.WriteFile(caCRLPath, []byte(caCRL), os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
logger.ErrorToConsole("error writing FTPS CRL: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
|
|
|
|
func generateTOTPPasscode(secret string, algo otp.Algorithm) (string, error) {
|
|
|
|
return totp.GenerateCodeCustom(secret, time.Now(), totp.ValidateOpts{
|
|
|
|
Period: 30,
|
|
|
|
Skew: 1,
|
|
|
|
Digits: otp.DigitsSix,
|
|
|
|
Algorithm: algo,
|
|
|
|
})
|
|
|
|
}
|
2022-06-11 08:41:34 +00:00
|
|
|
|
|
|
|
func startHTTPFs() {
|
|
|
|
go func() {
|
2023-04-08 08:01:48 +00:00
|
|
|
if err := httpdtest.StartTestHTTPFs(httpFsPort, nil); err != nil {
|
2022-06-11 08:41:34 +00:00
|
|
|
logger.ErrorToConsole("could not start HTTPfs test server: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
waitTCPListening(fmt.Sprintf(":%d", httpFsPort))
|
|
|
|
}
|