2019-07-20 10:26:52 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
2021-05-06 19:35:43 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/base64"
|
2019-07-20 10:26:52 +00:00
|
|
|
"encoding/json"
|
2020-06-07 21:30:18 +00:00
|
|
|
"errors"
|
2019-10-07 16:19:01 +00:00
|
|
|
"fmt"
|
2021-03-25 16:11:42 +00:00
|
|
|
"math"
|
2019-12-30 17:37:50 +00:00
|
|
|
"net"
|
2020-02-23 10:30:26 +00:00
|
|
|
"os"
|
2019-12-25 17:20:19 +00:00
|
|
|
"path"
|
2019-07-20 10:26:52 +00:00
|
|
|
"path/filepath"
|
2020-03-01 21:10:29 +00:00
|
|
|
"strings"
|
2020-02-23 10:30:26 +00:00
|
|
|
"time"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
2022-01-06 10:54:43 +00:00
|
|
|
"github.com/sftpgo/sdk"
|
|
|
|
|
2022-01-06 09:11:47 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/kms"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/logger"
|
2021-09-04 10:11:04 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/mfa"
|
2022-02-25 10:51:10 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/plugin"
|
2021-07-11 13:26:51 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/util"
|
2021-06-26 05:31:41 +00:00
|
|
|
"github.com/drakkan/sftpgo/v2/vfs"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2021-01-17 21:29:08 +00:00
|
|
|
// Available permissions for SFTPGo users
|
2019-07-20 10:26:52 +00:00
|
|
|
const (
|
2019-07-30 18:51:29 +00:00
|
|
|
// All permissions are granted
|
|
|
|
PermAny = "*"
|
|
|
|
// List items such as files and directories is allowed
|
|
|
|
PermListItems = "list"
|
|
|
|
// download files is allowed
|
|
|
|
PermDownload = "download"
|
|
|
|
// upload files is allowed
|
|
|
|
PermUpload = "upload"
|
2019-09-17 06:53:45 +00:00
|
|
|
// overwrite an existing file, while uploading, is allowed
|
|
|
|
// upload permission is required to allow file overwrite
|
|
|
|
PermOverwrite = "overwrite"
|
2019-07-30 18:51:29 +00:00
|
|
|
// delete files or directories is allowed
|
|
|
|
PermDelete = "delete"
|
2021-11-14 15:23:33 +00:00
|
|
|
// delete files is allowed
|
|
|
|
PermDeleteFiles = "delete_files"
|
|
|
|
// delete directories is allowed
|
|
|
|
PermDeleteDirs = "delete_dirs"
|
2019-07-30 18:51:29 +00:00
|
|
|
// rename files or directories is allowed
|
|
|
|
PermRename = "rename"
|
2021-11-14 15:23:33 +00:00
|
|
|
// rename files is allowed
|
|
|
|
PermRenameFiles = "rename_files"
|
|
|
|
// rename directories is allowed
|
|
|
|
PermRenameDirs = "rename_dirs"
|
2019-07-30 18:51:29 +00:00
|
|
|
// create directories is allowed
|
|
|
|
PermCreateDirs = "create_dirs"
|
|
|
|
// create symbolic links is allowed
|
2019-07-20 10:26:52 +00:00
|
|
|
PermCreateSymlinks = "create_symlinks"
|
2019-11-15 11:15:07 +00:00
|
|
|
// changing file or directory permissions is allowed
|
|
|
|
PermChmod = "chmod"
|
2019-11-15 11:26:52 +00:00
|
|
|
// changing file or directory owner and group is allowed
|
2019-11-15 11:15:07 +00:00
|
|
|
PermChown = "chown"
|
2019-11-16 09:23:41 +00:00
|
|
|
// changing file or directory access and modification time is allowed
|
|
|
|
PermChtimes = "chtimes"
|
2019-07-20 10:26:52 +00:00
|
|
|
)
|
|
|
|
|
2020-07-29 19:56:56 +00:00
|
|
|
// Available login methods
|
2020-02-19 21:39:30 +00:00
|
|
|
const (
|
2020-08-12 14:15:12 +00:00
|
|
|
LoginMethodNoAuthTryed = "no_auth_tryed"
|
|
|
|
LoginMethodPassword = "password"
|
2020-02-19 21:39:30 +00:00
|
|
|
SSHLoginMethodPublicKey = "publickey"
|
|
|
|
SSHLoginMethodKeyboardInteractive = "keyboard-interactive"
|
2020-04-09 21:32:42 +00:00
|
|
|
SSHLoginMethodKeyAndPassword = "publickey+password"
|
|
|
|
SSHLoginMethodKeyAndKeyboardInt = "publickey+keyboard-interactive"
|
2021-02-28 11:10:40 +00:00
|
|
|
LoginMethodTLSCertificate = "TLSCertificate"
|
|
|
|
LoginMethodTLSCertificateAndPwd = "TLSCertificate+password"
|
2022-02-19 09:53:35 +00:00
|
|
|
LoginMethodIDP = "IDP"
|
2021-02-28 11:10:40 +00:00
|
|
|
)
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
var (
|
|
|
|
errNoMatchingVirtualFolder = errors.New("no matching virtual folder found")
|
2021-11-14 15:23:33 +00:00
|
|
|
permsRenameAny = []string{PermRename, PermRenameDirs, PermRenameFiles}
|
|
|
|
permsDeleteAny = []string{PermDelete, PermDeleteDirs, PermDeleteFiles}
|
|
|
|
permsCreateAny = []string{PermUpload, PermCreateDirs}
|
2020-06-07 21:30:18 +00:00
|
|
|
)
|
|
|
|
|
2022-01-06 09:11:47 +00:00
|
|
|
// RecoveryCode defines a 2FA recovery code
|
|
|
|
type RecoveryCode struct {
|
|
|
|
Secret *kms.Secret `json:"secret"`
|
|
|
|
Used bool `json:"used,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserTOTPConfig defines the time-based one time password configuration
|
|
|
|
type UserTOTPConfig struct {
|
|
|
|
Enabled bool `json:"enabled,omitempty"`
|
|
|
|
ConfigName string `json:"config_name,omitempty"`
|
|
|
|
Secret *kms.Secret `json:"secret,omitempty"`
|
|
|
|
// TOTP will be required for the specified protocols.
|
|
|
|
// SSH protocol (SFTP/SCP/SSH commands) will ask for the TOTP passcode if the client uses keyboard interactive
|
|
|
|
// authentication.
|
|
|
|
// FTP have no standard way to support two factor authentication, if you
|
|
|
|
// enable the support for this protocol you have to add the TOTP passcode after the password.
|
|
|
|
// For example if your password is "password" and your one time passcode is
|
|
|
|
// "123456" you have to use "password123456" as password.
|
|
|
|
Protocols []string `json:"protocols,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// UserFilters defines additional restrictions for a user
|
|
|
|
// TODO: rename to UserOptions in v3
|
|
|
|
type UserFilters struct {
|
|
|
|
sdk.BaseUserFilters
|
|
|
|
// Time-based one time passwords configuration
|
|
|
|
TOTPConfig UserTOTPConfig `json:"totp_config,omitempty"`
|
|
|
|
// Recovery codes to use if the user loses access to their second factor auth device.
|
|
|
|
// Each code can only be used once, you should use these codes to login and disable or
|
|
|
|
// reset 2FA for your account
|
|
|
|
RecoveryCodes []RecoveryCode `json:"recovery_codes,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:25:17 +00:00
|
|
|
// User defines a SFTPGo user
|
2019-07-20 10:26:52 +00:00
|
|
|
type User struct {
|
2021-07-11 13:26:51 +00:00
|
|
|
sdk.BaseUser
|
2022-01-06 09:11:47 +00:00
|
|
|
// Additional restrictions
|
|
|
|
Filters UserFilters `json:"filters"`
|
2021-03-21 18:15:47 +00:00
|
|
|
// Mapping between virtual paths and virtual folders
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualFolders []vfs.VirtualFolder `json:"virtual_folders,omitempty"`
|
2020-01-19 06:41:05 +00:00
|
|
|
// Filesystem configuration details
|
2021-03-21 18:15:47 +00:00
|
|
|
FsConfig vfs.Filesystem `json:"filesystem"`
|
|
|
|
// we store the filesystem here using the base path as key.
|
|
|
|
fsCache map[string]vfs.Fs `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilesystem returns the base filesystem for this user
|
|
|
|
func (u *User) GetFilesystem(connectionID string) (fs vfs.Fs, err error) {
|
|
|
|
fs, err = u.getRootFs(connectionID)
|
|
|
|
if err != nil {
|
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
u.fsCache = make(map[string]vfs.Fs)
|
|
|
|
u.fsCache["/"] = fs
|
|
|
|
return fs, err
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
func (u *User) getRootFs(connectionID string) (fs vfs.Fs, err error) {
|
2020-12-05 12:48:13 +00:00
|
|
|
switch u.FsConfig.Provider {
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.S3FilesystemProvider:
|
2021-03-21 18:15:47 +00:00
|
|
|
return vfs.NewS3Fs(connectionID, u.GetHomeDir(), "", u.FsConfig.S3Config)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.GCSFilesystemProvider:
|
2020-01-31 18:04:00 +00:00
|
|
|
config := u.FsConfig.GCSConfig
|
2021-03-21 18:15:47 +00:00
|
|
|
config.CredentialFile = u.GetGCSCredentialsFilePath()
|
|
|
|
return vfs.NewGCSFs(connectionID, u.GetHomeDir(), "", config)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.AzureBlobFilesystemProvider:
|
2021-03-21 18:15:47 +00:00
|
|
|
return vfs.NewAzBlobFs(connectionID, u.GetHomeDir(), "", u.FsConfig.AzBlobConfig)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.CryptedFilesystemProvider:
|
2021-03-21 18:15:47 +00:00
|
|
|
return vfs.NewCryptFs(connectionID, u.GetHomeDir(), "", u.FsConfig.CryptConfig)
|
2021-07-11 13:26:51 +00:00
|
|
|
case sdk.SFTPFilesystemProvider:
|
2021-04-01 16:53:48 +00:00
|
|
|
forbiddenSelfUsers, err := u.getForbiddenSFTPSelfUsers(u.FsConfig.SFTPConfig.Username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
forbiddenSelfUsers = append(forbiddenSelfUsers, u.Username)
|
2021-04-03 14:00:55 +00:00
|
|
|
return vfs.NewSFTPFs(connectionID, "", u.GetHomeDir(), forbiddenSelfUsers, u.FsConfig.SFTPConfig)
|
2020-12-05 12:48:13 +00:00
|
|
|
default:
|
2021-03-21 18:15:47 +00:00
|
|
|
return vfs.NewOsFs(connectionID, u.GetHomeDir(), ""), nil
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2022-02-24 15:11:35 +00:00
|
|
|
func (u *User) checkDirWithParents(virtualDirPath, connectionID string) error {
|
|
|
|
dirs := util.GetDirsForVirtualPath(virtualDirPath)
|
|
|
|
for idx := len(dirs) - 1; idx >= 0; idx-- {
|
|
|
|
vPath := dirs[idx]
|
|
|
|
if vPath == "/" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fs, err := u.GetFilesystemForPath(vPath, connectionID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to get fs for path %#v: %w", vPath, err)
|
|
|
|
}
|
|
|
|
if fs.HasVirtualFolders() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fsPath, err := fs.ResolvePath(vPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to resolve path %#v: %w", vPath, err)
|
|
|
|
}
|
|
|
|
_, err = fs.Stat(fsPath)
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fs.IsNotExist(err) {
|
|
|
|
err = fs.Mkdir(fsPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
vfs.SetPathPermissions(fs, fsPath, u.GetUID(), u.GetGID())
|
|
|
|
} else {
|
|
|
|
return fmt.Errorf("unable to stat path %#v: %w", vPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
// CheckFsRoot check the root directory for the main fs and the virtual folders.
|
|
|
|
// It returns an error if the main filesystem cannot be created
|
|
|
|
func (u *User) CheckFsRoot(connectionID string) error {
|
2021-04-05 15:57:30 +00:00
|
|
|
if u.Filters.DisableFsChecks {
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-25 10:51:10 +00:00
|
|
|
delay := lastLoginMinDelay
|
|
|
|
if u.Filters.ExternalAuthCacheTime > 0 {
|
|
|
|
cacheTime := time.Duration(u.Filters.ExternalAuthCacheTime) * time.Second
|
|
|
|
if cacheTime > delay {
|
|
|
|
delay = cacheTime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isLastActivityRecent(u.LastLogin, delay) {
|
2022-02-24 15:11:35 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
fs, err := u.GetFilesystemForPath("/", connectionID)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, connectionID, "could not create main filesystem for user %#v err: %v", u.Username, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fs.CheckRootPath(u.Username, u.GetUID(), u.GetGID())
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
v := &u.VirtualFolders[idx]
|
|
|
|
fs, err = u.GetFilesystemForPath(v.VirtualPath, connectionID)
|
|
|
|
if err == nil {
|
|
|
|
fs.CheckRootPath(u.Username, u.GetUID(), u.GetGID())
|
|
|
|
}
|
|
|
|
// now check intermediary folders
|
2022-02-24 15:11:35 +00:00
|
|
|
err = u.checkDirWithParents(path.Dir(v.VirtualPath), connectionID)
|
|
|
|
if err != nil {
|
|
|
|
logger.Warn(logSender, connectionID, "could not create intermediary dir to %#v, err: %v", v.VirtualPath, err)
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-27 18:10:27 +00:00
|
|
|
// isFsEqual returns true if the fs has the same configuration
|
|
|
|
func (u *User) isFsEqual(other *User) bool {
|
2021-07-11 13:26:51 +00:00
|
|
|
if u.FsConfig.Provider == sdk.LocalFilesystemProvider && u.GetHomeDir() != other.GetHomeDir() {
|
2021-03-27 18:10:27 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !u.FsConfig.IsEqual(&other.FsConfig) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if len(u.VirtualFolders) != len(other.VirtualFolders) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
f := &u.VirtualFolders[idx]
|
|
|
|
found := false
|
|
|
|
for idx1 := range other.VirtualFolders {
|
|
|
|
f1 := &other.VirtualFolders[idx1]
|
|
|
|
if f.VirtualPath == f1.VirtualPath {
|
|
|
|
found = true
|
2021-07-11 13:26:51 +00:00
|
|
|
if f.FsConfig.Provider == sdk.LocalFilesystemProvider && f.MappedPath != f1.MappedPath {
|
2021-03-27 18:10:27 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
if !f.FsConfig.IsEqual(&f1.FsConfig) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-08-17 16:08:32 +00:00
|
|
|
// CheckLoginConditions checks if the user is active and not expired
|
|
|
|
func (u *User) CheckLoginConditions() error {
|
|
|
|
if u.Status < 1 {
|
|
|
|
return fmt.Errorf("user %#v is disabled", u.Username)
|
|
|
|
}
|
|
|
|
if u.ExpirationDate > 0 && u.ExpirationDate < util.GetTimeAsMsSinceEpoch(time.Now()) {
|
|
|
|
return fmt.Errorf("user %#v is expired, expiration timestamp: %v current timestamp: %v", u.Username,
|
|
|
|
u.ExpirationDate, util.GetTimeAsMsSinceEpoch(time.Now()))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-22 18:03:25 +00:00
|
|
|
// hideConfidentialData hides user confidential data
|
|
|
|
func (u *User) hideConfidentialData() {
|
2020-11-22 20:53:04 +00:00
|
|
|
u.Password = ""
|
2021-06-19 10:24:43 +00:00
|
|
|
u.FsConfig.HideConfidentialData()
|
2021-09-11 12:19:17 +00:00
|
|
|
if u.Filters.TOTPConfig.Secret != nil {
|
|
|
|
u.Filters.TOTPConfig.Secret.Hide()
|
|
|
|
}
|
|
|
|
for _, code := range u.Filters.RecoveryCodes {
|
|
|
|
if code.Secret != nil {
|
|
|
|
code.Secret.Hide()
|
|
|
|
}
|
|
|
|
}
|
2021-03-22 18:03:25 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 20:02:01 +00:00
|
|
|
// GetSubDirPermissions returns permissions for sub directories
|
2021-07-11 13:26:51 +00:00
|
|
|
func (u *User) GetSubDirPermissions() []sdk.DirectoryPermissions {
|
|
|
|
var result []sdk.DirectoryPermissions
|
2021-05-23 20:02:01 +00:00
|
|
|
for k, v := range u.Permissions {
|
|
|
|
if k == "/" {
|
|
|
|
continue
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
dirPerms := sdk.DirectoryPermissions{
|
2021-05-23 20:02:01 +00:00
|
|
|
Path: k,
|
|
|
|
Permissions: v,
|
|
|
|
}
|
|
|
|
result = append(result, dirPerms)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
// RenderAsJSON implements the renderer interface used within plugins
|
|
|
|
func (u *User) RenderAsJSON(reload bool) ([]byte, error) {
|
|
|
|
if reload {
|
|
|
|
user, err := provider.userExists(u.Username)
|
|
|
|
if err != nil {
|
2021-12-16 18:53:00 +00:00
|
|
|
providerLog(logger.LevelError, "unable to reload user before rendering as json: %v", err)
|
2021-07-11 13:26:51 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
user.PrepareForRendering()
|
|
|
|
return json.Marshal(user)
|
|
|
|
}
|
|
|
|
u.PrepareForRendering()
|
|
|
|
return json.Marshal(u)
|
|
|
|
}
|
|
|
|
|
2021-03-22 18:03:25 +00:00
|
|
|
// PrepareForRendering prepares a user for rendering.
|
|
|
|
// It hides confidential data and set to nil the empty secrets
|
|
|
|
// so they are not serialized
|
|
|
|
func (u *User) PrepareForRendering() {
|
|
|
|
u.hideConfidentialData()
|
|
|
|
u.FsConfig.SetNilSecretsIfEmpty()
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
folder := &u.VirtualFolders[idx]
|
2021-03-22 18:03:25 +00:00
|
|
|
folder.PrepareForRendering()
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-10 11:08:05 +00:00
|
|
|
// HasRedactedSecret returns true if the user has a redacted secret
|
2021-03-21 18:15:47 +00:00
|
|
|
func (u *User) hasRedactedSecret() bool {
|
2021-06-19 10:24:43 +00:00
|
|
|
if u.FsConfig.HasRedactedSecret() {
|
|
|
|
return true
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
folder := &u.VirtualFolders[idx]
|
|
|
|
if folder.HasRedactedSecret() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
return u.Filters.TOTPConfig.Secret.IsRedacted()
|
2021-03-21 18:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CloseFs closes the underlying filesystems
|
|
|
|
func (u *User) CloseFs() error {
|
|
|
|
if u.fsCache == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, fs := range u.fsCache {
|
|
|
|
errClose := fs.Close()
|
|
|
|
if err == nil {
|
|
|
|
err = errClose
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
|
|
|
|
2021-02-15 18:38:53 +00:00
|
|
|
// IsPasswordHashed returns true if the password is hashed
|
|
|
|
func (u *User) IsPasswordHashed() bool {
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.IsStringPrefixInSlice(u.Password, hashPwdPrefixes)
|
2021-02-15 18:38:53 +00:00
|
|
|
}
|
|
|
|
|
2021-02-28 11:10:40 +00:00
|
|
|
// IsTLSUsernameVerificationEnabled returns true if we need to extract the username
|
|
|
|
// from the client TLS certificate
|
|
|
|
func (u *User) IsTLSUsernameVerificationEnabled() bool {
|
|
|
|
if u.Filters.TLSUsername != "" {
|
2021-07-11 13:26:51 +00:00
|
|
|
return u.Filters.TLSUsername != sdk.TLSUsernameNone
|
2021-02-28 11:10:40 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
// SetEmptySecrets sets to empty any user secret
|
|
|
|
func (u *User) SetEmptySecrets() {
|
2022-01-12 18:01:19 +00:00
|
|
|
u.FsConfig.SetEmptySecrets()
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
folder := &u.VirtualFolders[idx]
|
2022-01-12 18:01:19 +00:00
|
|
|
folder.FsConfig.SetEmptySecrets()
|
2020-12-26 14:11:38 +00:00
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
u.Filters.TOTPConfig.Secret = kms.NewEmptySecret()
|
2020-12-26 14:11:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
// GetPermissionsForPath returns the permissions for the given path.
|
2021-02-14 21:08:08 +00:00
|
|
|
// The path must be a SFTPGo exposed path
|
2019-12-25 17:20:19 +00:00
|
|
|
func (u *User) GetPermissionsForPath(p string) []string {
|
|
|
|
permissions := []string{}
|
|
|
|
if perms, ok := u.Permissions["/"]; ok {
|
|
|
|
// if only root permissions are defined returns them unconditionally
|
|
|
|
if len(u.Permissions) == 1 {
|
|
|
|
return perms
|
|
|
|
}
|
|
|
|
// fallback permissions
|
|
|
|
permissions = perms
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
dirsForPath := util.GetDirsForVirtualPath(p)
|
2019-12-25 17:20:19 +00:00
|
|
|
// dirsForPath contains all the dirs for a given path in reverse order
|
|
|
|
// for example if the path is: /1/2/3/4 it contains:
|
|
|
|
// [ "/1/2/3/4", "/1/2/3", "/1/2", "/1", "/" ]
|
|
|
|
// so the first match is the one we are interested to
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range dirsForPath {
|
|
|
|
if perms, ok := u.Permissions[dirsForPath[idx]]; ok {
|
2019-12-25 17:20:19 +00:00
|
|
|
permissions = perms
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return permissions
|
|
|
|
}
|
|
|
|
|
2021-12-08 18:25:22 +00:00
|
|
|
// HasBufferedSFTP returns true if the user has a SFTP filesystem with buffering enabled
|
|
|
|
func (u *User) HasBufferedSFTP(name string) bool {
|
|
|
|
fs := u.GetFsConfigForPath(name)
|
|
|
|
if fs.Provider == sdk.SFTPFilesystemProvider {
|
|
|
|
return fs.SFTPConfig.BufferSize > 0
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-04-01 16:53:48 +00:00
|
|
|
func (u *User) getForbiddenSFTPSelfUsers(username string) ([]string, error) {
|
|
|
|
sftpUser, err := UserExists(username)
|
|
|
|
if err == nil {
|
|
|
|
// we don't allow local nested SFTP folders
|
|
|
|
var forbiddens []string
|
2021-07-11 13:26:51 +00:00
|
|
|
if sftpUser.FsConfig.Provider == sdk.SFTPFilesystemProvider {
|
2021-04-01 16:53:48 +00:00
|
|
|
forbiddens = append(forbiddens, sftpUser.Username)
|
|
|
|
return forbiddens, nil
|
|
|
|
}
|
|
|
|
for idx := range sftpUser.VirtualFolders {
|
|
|
|
v := &sftpUser.VirtualFolders[idx]
|
2021-07-11 13:26:51 +00:00
|
|
|
if v.FsConfig.Provider == sdk.SFTPFilesystemProvider {
|
2021-04-01 16:53:48 +00:00
|
|
|
forbiddens = append(forbiddens, sftpUser.Username)
|
|
|
|
return forbiddens, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return forbiddens, nil
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if _, ok := err.(*util.RecordNotFoundError); !ok {
|
2021-04-01 16:53:48 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 05:48:37 +00:00
|
|
|
// GetFsConfigForPath returns the file system configuration for the specified virtual path
|
|
|
|
func (u *User) GetFsConfigForPath(virtualPath string) vfs.Filesystem {
|
|
|
|
if virtualPath != "" && virtualPath != "/" && len(u.VirtualFolders) > 0 {
|
|
|
|
folder, err := u.GetVirtualFolderForPath(virtualPath)
|
|
|
|
if err == nil {
|
|
|
|
return folder.FsConfig
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.FsConfig
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
// GetFilesystemForPath returns the filesystem for the given path
|
|
|
|
func (u *User) GetFilesystemForPath(virtualPath, connectionID string) (vfs.Fs, error) {
|
|
|
|
if u.fsCache == nil {
|
|
|
|
u.fsCache = make(map[string]vfs.Fs)
|
|
|
|
}
|
|
|
|
if virtualPath != "" && virtualPath != "/" && len(u.VirtualFolders) > 0 {
|
|
|
|
folder, err := u.GetVirtualFolderForPath(virtualPath)
|
|
|
|
if err == nil {
|
|
|
|
if fs, ok := u.fsCache[folder.VirtualPath]; ok {
|
|
|
|
return fs, nil
|
|
|
|
}
|
2021-04-01 16:53:48 +00:00
|
|
|
forbiddenSelfUsers := []string{u.Username}
|
2021-07-11 13:26:51 +00:00
|
|
|
if folder.FsConfig.Provider == sdk.SFTPFilesystemProvider {
|
2021-04-01 16:53:48 +00:00
|
|
|
forbiddens, err := u.getForbiddenSFTPSelfUsers(folder.FsConfig.SFTPConfig.Username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
forbiddenSelfUsers = append(forbiddenSelfUsers, forbiddens...)
|
|
|
|
}
|
|
|
|
fs, err := folder.GetFilesystem(connectionID, forbiddenSelfUsers)
|
2021-03-21 18:15:47 +00:00
|
|
|
if err == nil {
|
|
|
|
u.fsCache[folder.VirtualPath] = fs
|
|
|
|
}
|
|
|
|
return fs, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := u.fsCache["/"]; ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.GetFilesystem(connectionID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVirtualFolderForPath returns the virtual folder containing the specified virtual path.
|
2020-06-07 21:30:18 +00:00
|
|
|
// If the path is not inside a virtual folder an error is returned
|
2021-03-21 18:15:47 +00:00
|
|
|
func (u *User) GetVirtualFolderForPath(virtualPath string) (vfs.VirtualFolder, error) {
|
2020-06-07 21:30:18 +00:00
|
|
|
var folder vfs.VirtualFolder
|
2022-01-30 10:42:36 +00:00
|
|
|
if virtualPath == "/" || len(u.VirtualFolders) == 0 {
|
2020-06-07 21:30:18 +00:00
|
|
|
return folder, errNoMatchingVirtualFolder
|
2020-05-01 13:27:53 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
dirsForPath := util.GetDirsForVirtualPath(virtualPath)
|
2021-03-21 18:15:47 +00:00
|
|
|
for index := range dirsForPath {
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
v := &u.VirtualFolders[idx]
|
|
|
|
if v.VirtualPath == dirsForPath[index] {
|
|
|
|
return *v, nil
|
2020-05-01 13:27:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
return folder, errNoMatchingVirtualFolder
|
2020-05-01 13:27:53 +00:00
|
|
|
}
|
|
|
|
|
2021-12-16 17:18:36 +00:00
|
|
|
// CheckMetadataConsistency checks the consistency between the metadata stored
|
|
|
|
// in the configured metadata plugin and the filesystem
|
|
|
|
func (u *User) CheckMetadataConsistency() error {
|
|
|
|
fs, err := u.getRootFs("")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fs.Close()
|
|
|
|
|
|
|
|
if err = fs.CheckMetadata(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
v := &u.VirtualFolders[idx]
|
|
|
|
if err = v.CheckMetadataConsistency(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
// ScanQuota scans the user home dir and virtual folders, included in its quota,
|
|
|
|
// and returns the number of files and their size
|
|
|
|
func (u *User) ScanQuota() (int, int64, error) {
|
|
|
|
fs, err := u.getRootFs("")
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
defer fs.Close()
|
2021-12-16 17:18:36 +00:00
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
numFiles, size, err := fs.ScanRootDirContents()
|
|
|
|
if err != nil {
|
|
|
|
return numFiles, size, err
|
|
|
|
}
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
v := &u.VirtualFolders[idx]
|
|
|
|
if !v.IsIncludedInUserQuota() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
num, s, err := v.ScanQuota()
|
|
|
|
if err != nil {
|
|
|
|
return numFiles, size, err
|
|
|
|
}
|
|
|
|
numFiles += num
|
|
|
|
size += s
|
|
|
|
}
|
|
|
|
|
|
|
|
return numFiles, size, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVirtualFoldersInPath returns the virtual folders inside virtualPath including
|
|
|
|
// any parents
|
|
|
|
func (u *User) GetVirtualFoldersInPath(virtualPath string) map[string]bool {
|
|
|
|
result := make(map[string]bool)
|
|
|
|
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
v := &u.VirtualFolders[idx]
|
2021-07-11 13:26:51 +00:00
|
|
|
dirsForPath := util.GetDirsForVirtualPath(v.VirtualPath)
|
2021-03-21 18:15:47 +00:00
|
|
|
for index := range dirsForPath {
|
|
|
|
d := dirsForPath[index]
|
|
|
|
if d == "/" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if path.Dir(d) == virtualPath {
|
|
|
|
result[d] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
// FilterListDir adds virtual folders and remove hidden items from the given files list
|
|
|
|
func (u *User) FilterListDir(dirContents []os.FileInfo, virtualPath string) []os.FileInfo {
|
|
|
|
filter := u.getPatternsFilterForPath(virtualPath)
|
|
|
|
if len(u.VirtualFolders) == 0 && filter.DenyPolicy != sdk.DenyPolicyHide {
|
|
|
|
return dirContents
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
|
2022-01-10 18:44:16 +00:00
|
|
|
vdirs := make(map[string]bool)
|
2021-03-21 18:15:47 +00:00
|
|
|
for dir := range u.GetVirtualFoldersInPath(virtualPath) {
|
2022-01-15 16:16:49 +00:00
|
|
|
dirName := path.Base(dir)
|
|
|
|
if filter.DenyPolicy == sdk.DenyPolicyHide {
|
|
|
|
if !filter.CheckAllowed(dirName) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vdirs[dirName] = true
|
2022-01-10 18:44:16 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
validIdx := 0
|
|
|
|
for index, fi := range dirContents {
|
2022-01-10 18:44:16 +00:00
|
|
|
for dir := range vdirs {
|
2022-01-15 16:16:49 +00:00
|
|
|
if fi.Name() == dir {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
fi = vfs.NewFileInfo(dir, true, 0, time.Now(), false)
|
|
|
|
dirContents[index] = fi
|
2022-01-12 18:01:19 +00:00
|
|
|
}
|
2022-01-10 18:44:16 +00:00
|
|
|
delete(vdirs, dir)
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
if filter.DenyPolicy == sdk.DenyPolicyHide {
|
|
|
|
if filter.CheckAllowed(fi.Name()) {
|
|
|
|
dirContents[validIdx] = fi
|
|
|
|
validIdx++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if filter.DenyPolicy == sdk.DenyPolicyHide {
|
|
|
|
for idx := validIdx; idx < len(dirContents); idx++ {
|
|
|
|
dirContents[idx] = nil
|
|
|
|
}
|
|
|
|
dirContents = dirContents[:validIdx]
|
2022-01-10 18:44:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for dir := range vdirs {
|
|
|
|
fi := vfs.NewFileInfo(dir, true, 0, time.Now(), false)
|
2022-01-15 16:16:49 +00:00
|
|
|
dirContents = append(dirContents, fi)
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
return dirContents
|
2020-02-23 10:30:26 +00:00
|
|
|
}
|
|
|
|
|
2020-06-10 07:11:32 +00:00
|
|
|
// IsMappedPath returns true if the specified filesystem path has a virtual folder mapping.
|
|
|
|
// The filesystem path must be cleaned before calling this method
|
|
|
|
func (u *User) IsMappedPath(fsPath string) bool {
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
v := &u.VirtualFolders[idx]
|
2020-06-10 07:11:32 +00:00
|
|
|
if fsPath == v.MappedPath {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
// IsVirtualFolder returns true if the specified virtual path is a virtual folder
|
|
|
|
func (u *User) IsVirtualFolder(virtualPath string) bool {
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
v := &u.VirtualFolders[idx]
|
|
|
|
if virtualPath == v.VirtualPath {
|
2020-02-23 10:30:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:38:29 +00:00
|
|
|
// HasVirtualFoldersInside returns true if there are virtual folders inside the
|
2021-03-21 18:15:47 +00:00
|
|
|
// specified virtual path. We assume that path are cleaned
|
|
|
|
func (u *User) HasVirtualFoldersInside(virtualPath string) bool {
|
|
|
|
if virtualPath == "/" && len(u.VirtualFolders) > 0 {
|
2020-06-15 21:32:12 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-03-21 18:15:47 +00:00
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
v := &u.VirtualFolders[idx]
|
|
|
|
if len(v.VirtualPath) > len(virtualPath) {
|
|
|
|
if strings.HasPrefix(v.VirtualPath, virtualPath+"/") {
|
2020-06-07 21:30:18 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:15:47 +00:00
|
|
|
// HasPermissionsInside returns true if the specified virtualPath has no permissions itself and
|
2020-06-26 21:38:29 +00:00
|
|
|
// no subdirs with defined permissions
|
2021-03-21 18:15:47 +00:00
|
|
|
func (u *User) HasPermissionsInside(virtualPath string) bool {
|
2020-06-26 21:38:29 +00:00
|
|
|
for dir := range u.Permissions {
|
2021-03-21 18:15:47 +00:00
|
|
|
if dir == virtualPath {
|
2020-06-26 21:38:29 +00:00
|
|
|
return true
|
2021-03-21 18:15:47 +00:00
|
|
|
} else if len(dir) > len(virtualPath) {
|
|
|
|
if strings.HasPrefix(dir, virtualPath+"/") {
|
2020-06-10 07:11:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
// HasPerm returns true if the user has the given permission or any permission
|
2019-12-25 17:20:19 +00:00
|
|
|
func (u *User) HasPerm(permission, path string) bool {
|
|
|
|
perms := u.GetPermissionsForPath(path)
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.IsStringInSlice(PermAny, perms) {
|
2019-07-20 10:26:52 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
return util.IsStringInSlice(permission, perms)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2021-11-14 15:23:33 +00:00
|
|
|
// HasAnyPerm returns true if the user has at least one of the given permissions
|
|
|
|
func (u *User) HasAnyPerm(permissions []string, path string) bool {
|
|
|
|
perms := u.GetPermissionsForPath(path)
|
|
|
|
if util.IsStringInSlice(PermAny, perms) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, permission := range permissions {
|
|
|
|
if util.IsStringInSlice(permission, perms) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasPerms returns true if the user has all the given permissions
|
2019-12-25 17:20:19 +00:00
|
|
|
func (u *User) HasPerms(permissions []string, path string) bool {
|
|
|
|
perms := u.GetPermissionsForPath(path)
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.IsStringInSlice(PermAny, perms) {
|
2019-11-26 21:26:42 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, permission := range permissions {
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsStringInSlice(permission, perms) {
|
2019-11-26 21:26:42 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-11-14 15:23:33 +00:00
|
|
|
// HasPermsDeleteAll returns true if the user can delete both files and directories
|
|
|
|
// for the given path
|
|
|
|
func (u *User) HasPermsDeleteAll(path string) bool {
|
|
|
|
perms := u.GetPermissionsForPath(path)
|
|
|
|
canDeleteFiles := false
|
|
|
|
canDeleteDirs := false
|
|
|
|
for _, permission := range perms {
|
|
|
|
if permission == PermAny || permission == PermDelete {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if permission == PermDeleteFiles {
|
|
|
|
canDeleteFiles = true
|
|
|
|
}
|
|
|
|
if permission == PermDeleteDirs {
|
|
|
|
canDeleteDirs = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return canDeleteFiles && canDeleteDirs
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasPermsRenameAll returns true if the user can rename both files and directories
|
|
|
|
// for the given path
|
|
|
|
func (u *User) HasPermsRenameAll(path string) bool {
|
|
|
|
perms := u.GetPermissionsForPath(path)
|
|
|
|
canRenameFiles := false
|
|
|
|
canRenameDirs := false
|
|
|
|
for _, permission := range perms {
|
|
|
|
if permission == PermAny || permission == PermRename {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if permission == PermRenameFiles {
|
|
|
|
canRenameFiles = true
|
|
|
|
}
|
|
|
|
if permission == PermRenameDirs {
|
|
|
|
canRenameDirs = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return canRenameFiles && canRenameDirs
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// HasNoQuotaRestrictions returns true if no quota restrictions need to be applyed
|
|
|
|
func (u *User) HasNoQuotaRestrictions(checkFiles bool) bool {
|
|
|
|
if u.QuotaSize == 0 && (!checkFiles || u.QuotaFiles == 0) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-04-09 21:32:42 +00:00
|
|
|
// IsLoginMethodAllowed returns true if the specified login method is allowed
|
|
|
|
func (u *User) IsLoginMethodAllowed(loginMethod string, partialSuccessMethods []string) bool {
|
2020-02-19 21:39:30 +00:00
|
|
|
if len(u.Filters.DeniedLoginMethods) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2020-04-09 21:32:42 +00:00
|
|
|
if len(partialSuccessMethods) == 1 {
|
2020-09-01 17:34:40 +00:00
|
|
|
for _, method := range u.GetNextAuthMethods(partialSuccessMethods, true) {
|
2020-04-09 21:32:42 +00:00
|
|
|
if method == loginMethod {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if util.IsStringInSlice(loginMethod, u.Filters.DeniedLoginMethods) {
|
2020-04-09 21:32:42 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNextAuthMethods returns the list of authentications methods that
|
|
|
|
// can continue for multi-step authentication
|
2020-09-01 17:34:40 +00:00
|
|
|
func (u *User) GetNextAuthMethods(partialSuccessMethods []string, isPasswordAuthEnabled bool) []string {
|
2020-04-09 21:32:42 +00:00
|
|
|
var methods []string
|
|
|
|
if len(partialSuccessMethods) != 1 {
|
|
|
|
return methods
|
|
|
|
}
|
|
|
|
if partialSuccessMethods[0] != SSHLoginMethodPublicKey {
|
|
|
|
return methods
|
|
|
|
}
|
|
|
|
for _, method := range u.GetAllowedLoginMethods() {
|
2020-09-01 17:34:40 +00:00
|
|
|
if method == SSHLoginMethodKeyAndPassword && isPasswordAuthEnabled {
|
2020-08-12 14:15:12 +00:00
|
|
|
methods = append(methods, LoginMethodPassword)
|
2020-04-09 21:32:42 +00:00
|
|
|
}
|
|
|
|
if method == SSHLoginMethodKeyAndKeyboardInt {
|
|
|
|
methods = append(methods, SSHLoginMethodKeyboardInteractive)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return methods
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsPartialAuth returns true if the specified login method is a step for
|
|
|
|
// a multi-step Authentication.
|
|
|
|
// We support publickey+password and publickey+keyboard-interactive, so
|
|
|
|
// only publickey can returns partial success.
|
|
|
|
// We can have partial success if only multi-step Auth methods are enabled
|
|
|
|
func (u *User) IsPartialAuth(loginMethod string) bool {
|
|
|
|
if loginMethod != SSHLoginMethodPublicKey {
|
2020-02-19 21:39:30 +00:00
|
|
|
return false
|
|
|
|
}
|
2020-04-09 21:32:42 +00:00
|
|
|
for _, method := range u.GetAllowedLoginMethods() {
|
2021-02-28 11:10:40 +00:00
|
|
|
if method == LoginMethodTLSCertificate || method == LoginMethodTLSCertificateAndPwd {
|
|
|
|
continue
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsStringInSlice(method, SSHMultiStepsLoginMethods) {
|
2020-04-09 21:32:42 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2020-02-19 21:39:30 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-04-09 21:32:42 +00:00
|
|
|
// GetAllowedLoginMethods returns the allowed login methods
|
|
|
|
func (u *User) GetAllowedLoginMethods() []string {
|
|
|
|
var allowedMethods []string
|
2021-02-28 11:10:40 +00:00
|
|
|
for _, method := range ValidLoginMethods {
|
2021-07-11 13:26:51 +00:00
|
|
|
if !util.IsStringInSlice(method, u.Filters.DeniedLoginMethods) {
|
2020-04-09 21:32:42 +00:00
|
|
|
allowedMethods = append(allowedMethods, method)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allowedMethods
|
|
|
|
}
|
|
|
|
|
2021-05-23 20:02:01 +00:00
|
|
|
// GetFlatFilePatterns returns file patterns as flat list
|
|
|
|
// duplicating a path if it has both allowed and denied patterns
|
2021-07-11 13:26:51 +00:00
|
|
|
func (u *User) GetFlatFilePatterns() []sdk.PatternsFilter {
|
|
|
|
var result []sdk.PatternsFilter
|
2021-05-23 20:02:01 +00:00
|
|
|
|
|
|
|
for _, pattern := range u.Filters.FilePatterns {
|
|
|
|
if len(pattern.AllowedPatterns) > 0 {
|
2021-07-11 13:26:51 +00:00
|
|
|
result = append(result, sdk.PatternsFilter{
|
2021-05-23 20:02:01 +00:00
|
|
|
Path: pattern.Path,
|
|
|
|
AllowedPatterns: pattern.AllowedPatterns,
|
2022-01-15 16:16:49 +00:00
|
|
|
DenyPolicy: pattern.DenyPolicy,
|
2021-05-23 20:02:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if len(pattern.DeniedPatterns) > 0 {
|
2021-07-11 13:26:51 +00:00
|
|
|
result = append(result, sdk.PatternsFilter{
|
2021-05-23 20:02:01 +00:00
|
|
|
Path: pattern.Path,
|
|
|
|
DeniedPatterns: pattern.DeniedPatterns,
|
2022-01-15 16:16:49 +00:00
|
|
|
DenyPolicy: pattern.DenyPolicy,
|
2021-05-23 20:02:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
func (u *User) getPatternsFilterForPath(virtualPath string) sdk.PatternsFilter {
|
|
|
|
var filter sdk.PatternsFilter
|
2020-11-15 21:04:48 +00:00
|
|
|
if len(u.Filters.FilePatterns) == 0 {
|
2022-01-15 16:16:49 +00:00
|
|
|
return filter
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
dirsForPath := util.GetDirsForVirtualPath(virtualPath)
|
2020-11-15 21:04:48 +00:00
|
|
|
for _, dir := range dirsForPath {
|
|
|
|
for _, f := range u.Filters.FilePatterns {
|
|
|
|
if f.Path == dir {
|
|
|
|
filter = f
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.Path != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2022-01-15 16:16:49 +00:00
|
|
|
return filter
|
|
|
|
}
|
|
|
|
|
2022-01-15 18:06:02 +00:00
|
|
|
func (u *User) isDirHidden(virtualPath string) bool {
|
|
|
|
if len(u.Filters.FilePatterns) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, dirPath := range util.GetDirsForVirtualPath(virtualPath) {
|
|
|
|
if dirPath == "/" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
filter := u.getPatternsFilterForPath(dirPath)
|
|
|
|
if filter.DenyPolicy == sdk.DenyPolicyHide {
|
|
|
|
if !filter.CheckAllowed(path.Base(dirPath)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-01-15 16:16:49 +00:00
|
|
|
// IsFileAllowed returns true if the specified file is allowed by the file restrictions filters.
|
|
|
|
// The second parameter returned is the deny policy
|
|
|
|
func (u *User) IsFileAllowed(virtualPath string) (bool, int) {
|
2022-01-15 18:06:02 +00:00
|
|
|
dirPath := path.Dir(virtualPath)
|
|
|
|
if u.isDirHidden(dirPath) {
|
|
|
|
return false, sdk.DenyPolicyHide
|
|
|
|
}
|
|
|
|
filter := u.getPatternsFilterForPath(dirPath)
|
2022-01-15 16:16:49 +00:00
|
|
|
return filter.CheckAllowed(path.Base(virtualPath)), filter.DenyPolicy
|
2020-11-15 21:04:48 +00:00
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
// CanManageMFA returns true if the user can add a multi-factor authentication configuration
|
|
|
|
func (u *User) CanManageMFA() bool {
|
|
|
|
if util.IsStringInSlice(sdk.WebClientMFADisabled, u.Filters.WebClient) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return len(mfa.GetAvailableTOTPConfigs()) > 0
|
|
|
|
}
|
|
|
|
|
2022-02-25 10:51:10 +00:00
|
|
|
func (u *User) isExternalAuthCached() bool {
|
|
|
|
if u.ID <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if u.Filters.ExternalAuthCacheTime <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return isLastActivityRecent(u.LastLogin, time.Duration(u.Filters.ExternalAuthCacheTime)*time.Second)
|
|
|
|
}
|
|
|
|
|
2021-11-06 13:13:20 +00:00
|
|
|
// CanManageShares returns true if the user can add, update and list shares
|
|
|
|
func (u *User) CanManageShares() bool {
|
|
|
|
return !util.IsStringInSlice(sdk.WebClientSharesDisabled, u.Filters.WebClient)
|
|
|
|
}
|
|
|
|
|
2021-11-13 12:25:43 +00:00
|
|
|
// CanResetPassword returns true if this user is allowed to reset its password
|
|
|
|
func (u *User) CanResetPassword() bool {
|
|
|
|
return !util.IsStringInSlice(sdk.WebClientPasswordResetDisabled, u.Filters.WebClient)
|
|
|
|
}
|
|
|
|
|
2021-09-05 16:49:13 +00:00
|
|
|
// CanChangePassword returns true if this user is allowed to change its password
|
|
|
|
func (u *User) CanChangePassword() bool {
|
|
|
|
return !util.IsStringInSlice(sdk.WebClientPasswordChangeDisabled, u.Filters.WebClient)
|
|
|
|
}
|
|
|
|
|
2021-09-06 16:46:35 +00:00
|
|
|
// CanChangeAPIKeyAuth returns true if this user is allowed to enable/disable API key authentication
|
|
|
|
func (u *User) CanChangeAPIKeyAuth() bool {
|
|
|
|
return !util.IsStringInSlice(sdk.WebClientAPIKeyAuthChangeDisabled, u.Filters.WebClient)
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:46:15 +00:00
|
|
|
// CanChangeInfo returns true if this user is allowed to change its info such as email and description
|
|
|
|
func (u *User) CanChangeInfo() bool {
|
|
|
|
return !util.IsStringInSlice(sdk.WebClientInfoChangeDisabled, u.Filters.WebClient)
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
// CanManagePublicKeys returns true if this user is allowed to manage public keys
|
2021-07-26 18:55:49 +00:00
|
|
|
// from the web client. Used in web client UI
|
2021-05-07 18:41:20 +00:00
|
|
|
func (u *User) CanManagePublicKeys() bool {
|
2021-07-11 13:26:51 +00:00
|
|
|
return !util.IsStringInSlice(sdk.WebClientPubKeyChangeDisabled, u.Filters.WebClient)
|
2021-05-06 19:35:43 +00:00
|
|
|
}
|
|
|
|
|
2021-07-26 18:55:49 +00:00
|
|
|
// CanAddFilesFromWeb returns true if the client can add files from the web UI.
|
|
|
|
// The specified target is the directory where the files must be uploaded
|
|
|
|
func (u *User) CanAddFilesFromWeb(target string) bool {
|
|
|
|
if util.IsStringInSlice(sdk.WebClientWriteDisabled, u.Filters.WebClient) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return u.HasPerm(PermUpload, target) || u.HasPerm(PermOverwrite, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanAddDirsFromWeb returns true if the client can add directories from the web UI.
|
|
|
|
// The specified target is the directory where the new directory must be created
|
|
|
|
func (u *User) CanAddDirsFromWeb(target string) bool {
|
|
|
|
if util.IsStringInSlice(sdk.WebClientWriteDisabled, u.Filters.WebClient) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return u.HasPerm(PermCreateDirs, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanRenameFromWeb returns true if the client can rename objects from the web UI.
|
|
|
|
// The specified src and dest are the source and target directories for the rename.
|
|
|
|
func (u *User) CanRenameFromWeb(src, dest string) bool {
|
|
|
|
if util.IsStringInSlice(sdk.WebClientWriteDisabled, u.Filters.WebClient) {
|
|
|
|
return false
|
|
|
|
}
|
2021-11-14 15:23:33 +00:00
|
|
|
if u.HasAnyPerm(permsRenameAny, src) && u.HasAnyPerm(permsRenameAny, dest) {
|
2021-07-26 18:55:49 +00:00
|
|
|
return true
|
|
|
|
}
|
2021-11-14 15:23:33 +00:00
|
|
|
if !u.HasAnyPerm(permsDeleteAny, src) {
|
2021-07-26 18:55:49 +00:00
|
|
|
return false
|
|
|
|
}
|
2021-11-14 15:23:33 +00:00
|
|
|
return u.HasAnyPerm(permsCreateAny, dest)
|
2021-07-26 18:55:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CanDeleteFromWeb returns true if the client can delete objects from the web UI.
|
|
|
|
// The specified target is the parent directory for the object to delete
|
|
|
|
func (u *User) CanDeleteFromWeb(target string) bool {
|
|
|
|
if util.IsStringInSlice(sdk.WebClientWriteDisabled, u.Filters.WebClient) {
|
|
|
|
return false
|
|
|
|
}
|
2021-11-14 15:23:33 +00:00
|
|
|
return u.HasAnyPerm(permsDeleteAny, target)
|
2021-07-26 18:55:49 +00:00
|
|
|
}
|
|
|
|
|
2021-05-06 19:35:43 +00:00
|
|
|
// GetSignature returns a signature for this admin.
|
|
|
|
// It could change after an update
|
|
|
|
func (u *User) GetSignature() string {
|
|
|
|
data := []byte(fmt.Sprintf("%v_%v_%v", u.Username, u.Status, u.ExpirationDate))
|
|
|
|
data = append(data, []byte(u.Password)...)
|
|
|
|
signature := sha256.Sum256(data)
|
|
|
|
return base64.StdEncoding.EncodeToString(signature[:])
|
|
|
|
}
|
|
|
|
|
2021-12-10 17:43:26 +00:00
|
|
|
// GetBandwidthForIP returns the upload and download bandwidth for the specified IP
|
|
|
|
func (u *User) GetBandwidthForIP(clientIP, connectionID string) (int64, int64) {
|
|
|
|
if len(u.Filters.BandwidthLimits) > 0 {
|
|
|
|
ip := net.ParseIP(clientIP)
|
|
|
|
if ip != nil {
|
|
|
|
for _, bwLimit := range u.Filters.BandwidthLimits {
|
|
|
|
for _, source := range bwLimit.Sources {
|
|
|
|
_, ipNet, err := net.ParseCIDR(source)
|
|
|
|
if err == nil {
|
|
|
|
if ipNet.Contains(ip) {
|
|
|
|
logger.Debug(logSender, connectionID, "override bandwidth limit for ip %#v, upload limit: %v KB/s, download limit: %v KB/s",
|
|
|
|
clientIP, bwLimit.UploadBandwidth, bwLimit.DownloadBandwidth)
|
|
|
|
return bwLimit.UploadBandwidth, bwLimit.DownloadBandwidth
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return u.UploadBandwidth, u.DownloadBandwidth
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:39:30 +00:00
|
|
|
// IsLoginFromAddrAllowed returns true if the login is allowed from the specified remoteAddr.
|
2019-12-30 17:37:50 +00:00
|
|
|
// If AllowedIP is defined only the specified IP/Mask can login.
|
|
|
|
// If DeniedIP is defined the specified IP/Mask cannot login.
|
|
|
|
// If an IP is both allowed and denied then login will be denied
|
2020-02-19 21:39:30 +00:00
|
|
|
func (u *User) IsLoginFromAddrAllowed(remoteAddr string) bool {
|
2019-12-30 17:37:50 +00:00
|
|
|
if len(u.Filters.AllowedIP) == 0 && len(u.Filters.DeniedIP) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
remoteIP := net.ParseIP(util.GetIPFromRemoteAddress(remoteAddr))
|
2019-12-30 17:37:50 +00:00
|
|
|
// if remoteIP is invalid we allow login, this should never happen
|
|
|
|
if remoteIP == nil {
|
|
|
|
logger.Warn(logSender, "", "login allowed for invalid IP. remote address: %#v", remoteAddr)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, IPMask := range u.Filters.DeniedIP {
|
|
|
|
_, IPNet, err := net.ParseCIDR(IPMask)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if IPNet.Contains(remoteIP) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, IPMask := range u.Filters.AllowedIP {
|
|
|
|
_, IPNet, err := net.ParseCIDR(IPMask)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if IPNet.Contains(remoteIP) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(u.Filters.AllowedIP) == 0
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetPermissionsAsJSON returns the permissions as json byte array
|
2019-07-20 10:26:52 +00:00
|
|
|
func (u *User) GetPermissionsAsJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(u.Permissions)
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:42:46 +00:00
|
|
|
// GetPublicKeysAsJSON returns the public keys as json byte array
|
|
|
|
func (u *User) GetPublicKeysAsJSON() ([]byte, error) {
|
2019-08-07 21:41:10 +00:00
|
|
|
return json.Marshal(u.PublicKeys)
|
2019-08-01 20:42:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
// GetFiltersAsJSON returns the filters as json byte array
|
|
|
|
func (u *User) GetFiltersAsJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(u.Filters)
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// GetFsConfigAsJSON returns the filesystem config as json byte array
|
|
|
|
func (u *User) GetFsConfigAsJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(u.FsConfig)
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetUID returns a validate uid, suitable for use with os.Chown
|
2019-07-20 10:26:52 +00:00
|
|
|
func (u *User) GetUID() int {
|
2021-03-25 16:30:39 +00:00
|
|
|
if u.UID <= 0 || u.UID > math.MaxInt32 {
|
2019-07-20 10:26:52 +00:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return u.UID
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetGID returns a validate gid, suitable for use with os.Chown
|
2019-07-20 10:26:52 +00:00
|
|
|
func (u *User) GetGID() int {
|
2021-03-25 16:30:39 +00:00
|
|
|
if u.GID <= 0 || u.GID > math.MaxInt32 {
|
2019-07-20 10:26:52 +00:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return u.GID
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// GetHomeDir returns the shortest path name equivalent to the user's home directory
|
2019-07-20 10:26:52 +00:00
|
|
|
func (u *User) GetHomeDir() string {
|
|
|
|
return filepath.Clean(u.HomeDir)
|
|
|
|
}
|
2019-07-28 20:04:50 +00:00
|
|
|
|
2022-02-24 15:11:35 +00:00
|
|
|
// HasRecentActivity returns true if the last user login is recent and so we can skip some expensive checks
|
|
|
|
func (u *User) HasRecentActivity() bool {
|
2022-02-25 10:51:10 +00:00
|
|
|
return isLastActivityRecent(u.LastLogin, lastLoginMinDelay)
|
2022-02-24 15:11:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
// HasQuotaRestrictions returns true if there are any disk quota restrictions
|
2019-07-28 20:04:50 +00:00
|
|
|
func (u *User) HasQuotaRestrictions() bool {
|
|
|
|
return u.QuotaFiles > 0 || u.QuotaSize > 0
|
|
|
|
}
|
2019-08-08 17:33:16 +00:00
|
|
|
|
2022-01-30 10:42:36 +00:00
|
|
|
// HasTransferQuotaRestrictions returns true if there are any data transfer restrictions
|
|
|
|
func (u *User) HasTransferQuotaRestrictions() bool {
|
|
|
|
if len(u.Filters.DataTransferLimits) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return u.UploadDataTransfer > 0 || u.TotalDataTransfer > 0 || u.DownloadDataTransfer > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDataTransferLimits returns upload, download and total data transfer limits
|
|
|
|
func (u *User) GetDataTransferLimits(clientIP string) (int64, int64, int64) {
|
|
|
|
var total, ul, dl int64
|
|
|
|
if len(u.Filters.DataTransferLimits) > 0 {
|
|
|
|
ip := net.ParseIP(clientIP)
|
|
|
|
if ip != nil {
|
|
|
|
for _, limit := range u.Filters.DataTransferLimits {
|
|
|
|
for _, source := range limit.Sources {
|
|
|
|
_, ipNet, err := net.ParseCIDR(source)
|
|
|
|
if err == nil {
|
|
|
|
if ipNet.Contains(ip) {
|
|
|
|
if limit.TotalDataTransfer > 0 {
|
|
|
|
total = limit.TotalDataTransfer * 1048576
|
|
|
|
}
|
|
|
|
if limit.DownloadDataTransfer > 0 {
|
|
|
|
dl = limit.DownloadDataTransfer * 1048576
|
|
|
|
}
|
|
|
|
if limit.UploadDataTransfer > 0 {
|
|
|
|
ul = limit.UploadDataTransfer * 1048576
|
|
|
|
}
|
|
|
|
return ul, dl, total
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if u.TotalDataTransfer > 0 {
|
|
|
|
total = u.TotalDataTransfer * 1048576
|
|
|
|
}
|
|
|
|
if u.DownloadDataTransfer > 0 {
|
|
|
|
dl = u.DownloadDataTransfer * 1048576
|
|
|
|
}
|
|
|
|
if u.UploadDataTransfer > 0 {
|
|
|
|
ul = u.UploadDataTransfer * 1048576
|
|
|
|
}
|
|
|
|
return ul, dl, total
|
|
|
|
}
|
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
// GetQuotaSummary returns used quota and limits if defined
|
|
|
|
func (u *User) GetQuotaSummary() string {
|
2022-02-08 11:43:08 +00:00
|
|
|
var sb strings.Builder
|
|
|
|
|
|
|
|
addSection := func() {
|
|
|
|
if sb.Len() > 0 {
|
|
|
|
sb.WriteString(". ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if u.UsedQuotaFiles > 0 || u.QuotaFiles > 0 {
|
|
|
|
sb.WriteString(fmt.Sprintf("Files: %v", u.UsedQuotaFiles))
|
|
|
|
if u.QuotaFiles > 0 {
|
|
|
|
sb.WriteString(fmt.Sprintf("/%v", u.QuotaFiles))
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
if u.UsedQuotaSize > 0 || u.QuotaSize > 0 {
|
2022-02-08 11:43:08 +00:00
|
|
|
addSection()
|
|
|
|
sb.WriteString(fmt.Sprintf("Size: %v", util.ByteCountIEC(u.UsedQuotaSize)))
|
2019-10-07 16:19:01 +00:00
|
|
|
if u.QuotaSize > 0 {
|
2022-02-08 11:43:08 +00:00
|
|
|
sb.WriteString(fmt.Sprintf("/%v", util.ByteCountIEC(u.QuotaSize)))
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-08 11:43:08 +00:00
|
|
|
if u.TotalDataTransfer > 0 {
|
|
|
|
addSection()
|
|
|
|
total := u.UsedDownloadDataTransfer + u.UsedUploadDataTransfer
|
|
|
|
sb.WriteString(fmt.Sprintf("Transfer: %v/%v", util.ByteCountIEC(total),
|
|
|
|
util.ByteCountIEC(u.TotalDataTransfer*1048576)))
|
|
|
|
}
|
|
|
|
if u.UploadDataTransfer > 0 {
|
|
|
|
addSection()
|
|
|
|
sb.WriteString(fmt.Sprintf("UL: %v/%v", util.ByteCountIEC(u.UsedUploadDataTransfer),
|
|
|
|
util.ByteCountIEC(u.UploadDataTransfer*1048576)))
|
|
|
|
}
|
|
|
|
if u.DownloadDataTransfer > 0 {
|
|
|
|
addSection()
|
|
|
|
sb.WriteString(fmt.Sprintf("DL: %v/%v", util.ByteCountIEC(u.UsedDownloadDataTransfer),
|
|
|
|
util.ByteCountIEC(u.DownloadDataTransfer*1048576)))
|
|
|
|
}
|
2021-03-28 09:02:11 +00:00
|
|
|
if u.LastQuotaUpdate > 0 {
|
2022-02-08 11:43:08 +00:00
|
|
|
addSection()
|
2021-07-11 13:26:51 +00:00
|
|
|
t := util.GetTimeFromMsecSinceEpoch(u.LastQuotaUpdate)
|
2022-02-08 11:43:08 +00:00
|
|
|
sb.WriteString(fmt.Sprintf("Last update: %v", t.Format("2006-01-02 15:04"))) // YYYY-MM-DD HH:MM
|
2021-03-28 09:02:11 +00:00
|
|
|
}
|
2022-02-08 11:43:08 +00:00
|
|
|
return sb.String()
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPermissionsAsString returns the user's permissions as comma separated string
|
|
|
|
func (u *User) GetPermissionsAsString() string {
|
2019-12-25 17:20:19 +00:00
|
|
|
result := ""
|
|
|
|
for dir, perms := range u.Permissions {
|
2021-02-11 18:45:52 +00:00
|
|
|
dirPerms := strings.Join(perms, ", ")
|
2019-12-25 17:20:19 +00:00
|
|
|
dp := fmt.Sprintf("%#v: %#v", dir, dirPerms)
|
|
|
|
if dir == "/" {
|
2021-02-11 18:45:52 +00:00
|
|
|
if result != "" {
|
2019-12-25 17:20:19 +00:00
|
|
|
result = dp + ", " + result
|
|
|
|
} else {
|
|
|
|
result = dp
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-11 18:45:52 +00:00
|
|
|
if result != "" {
|
2019-12-25 17:20:19 +00:00
|
|
|
result += ", "
|
|
|
|
}
|
|
|
|
result += dp
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBandwidthAsString returns bandwidth limits if defines
|
|
|
|
func (u *User) GetBandwidthAsString() string {
|
2021-03-28 09:02:11 +00:00
|
|
|
result := "DL: "
|
2019-10-07 16:19:01 +00:00
|
|
|
if u.DownloadBandwidth > 0 {
|
2021-07-11 13:26:51 +00:00
|
|
|
result += util.ByteCountIEC(u.DownloadBandwidth*1000) + "/s."
|
2019-10-07 16:19:01 +00:00
|
|
|
} else {
|
2020-03-04 04:43:15 +00:00
|
|
|
result += "unlimited."
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2021-03-28 09:02:11 +00:00
|
|
|
result += " UL: "
|
2019-10-07 16:19:01 +00:00
|
|
|
if u.UploadBandwidth > 0 {
|
2021-07-11 13:26:51 +00:00
|
|
|
result += util.ByteCountIEC(u.UploadBandwidth*1000) + "/s."
|
2019-10-07 16:19:01 +00:00
|
|
|
} else {
|
2020-03-04 04:43:15 +00:00
|
|
|
result += "unlimited."
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetInfoString returns user's info as string.
|
2020-01-31 18:04:00 +00:00
|
|
|
// Storage provider, number of public keys, max sessions, uid,
|
|
|
|
// gid, denied and allowed IP/Mask are returned
|
2019-10-07 16:19:01 +00:00
|
|
|
func (u *User) GetInfoString() string {
|
2021-11-06 13:13:20 +00:00
|
|
|
var result strings.Builder
|
2019-11-13 10:36:21 +00:00
|
|
|
if u.LastLogin > 0 {
|
2021-07-11 13:26:51 +00:00
|
|
|
t := util.GetTimeFromMsecSinceEpoch(u.LastLogin)
|
2021-11-06 13:13:20 +00:00
|
|
|
result.WriteString(fmt.Sprintf("Last login: %v. ", t.Format("2006-01-02 15:04"))) // YYYY-MM-DD HH:MM
|
2019-11-13 10:36:21 +00:00
|
|
|
}
|
2021-07-11 13:26:51 +00:00
|
|
|
if u.FsConfig.Provider != sdk.LocalFilesystemProvider {
|
2021-11-06 13:13:20 +00:00
|
|
|
result.WriteString(fmt.Sprintf("Storage: %s. ", u.FsConfig.Provider.ShortInfo()))
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
if len(u.PublicKeys) > 0 {
|
2021-11-06 13:13:20 +00:00
|
|
|
result.WriteString(fmt.Sprintf("Public keys: %v. ", len(u.PublicKeys)))
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
if u.MaxSessions > 0 {
|
2021-11-06 13:13:20 +00:00
|
|
|
result.WriteString(fmt.Sprintf("Max sessions: %v. ", u.MaxSessions))
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
if u.UID > 0 {
|
2021-11-06 13:13:20 +00:00
|
|
|
result.WriteString(fmt.Sprintf("UID: %v. ", u.UID))
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
if u.GID > 0 {
|
2021-11-06 13:13:20 +00:00
|
|
|
result.WriteString(fmt.Sprintf("GID: %v. ", u.GID))
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
if len(u.Filters.DeniedIP) > 0 {
|
2021-11-06 13:13:20 +00:00
|
|
|
result.WriteString(fmt.Sprintf("Denied IP/Mask: %v. ", len(u.Filters.DeniedIP)))
|
2019-12-30 17:37:50 +00:00
|
|
|
}
|
|
|
|
if len(u.Filters.AllowedIP) > 0 {
|
2021-11-06 13:13:20 +00:00
|
|
|
result.WriteString(fmt.Sprintf("Allowed IP/Mask: %v", len(u.Filters.AllowedIP)))
|
2019-12-30 17:37:50 +00:00
|
|
|
}
|
2021-11-06 13:13:20 +00:00
|
|
|
return result.String()
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
2019-10-25 16:37:12 +00:00
|
|
|
|
2021-03-28 09:02:11 +00:00
|
|
|
// GetStatusAsString returns the user status as a string
|
|
|
|
func (u *User) GetStatusAsString() string {
|
2021-07-11 13:26:51 +00:00
|
|
|
if u.ExpirationDate > 0 && u.ExpirationDate < util.GetTimeAsMsSinceEpoch(time.Now()) {
|
2021-03-28 09:02:11 +00:00
|
|
|
return "Expired"
|
|
|
|
}
|
|
|
|
if u.Status == 1 {
|
|
|
|
return "Active"
|
|
|
|
}
|
|
|
|
return "Inactive"
|
|
|
|
}
|
|
|
|
|
2019-11-13 10:36:21 +00:00
|
|
|
// GetExpirationDateAsString returns expiration date formatted as YYYY-MM-DD
|
|
|
|
func (u *User) GetExpirationDateAsString() string {
|
|
|
|
if u.ExpirationDate > 0 {
|
2021-07-11 13:26:51 +00:00
|
|
|
t := util.GetTimeFromMsecSinceEpoch(u.ExpirationDate)
|
2019-11-13 10:36:21 +00:00
|
|
|
return t.Format("2006-01-02")
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
// GetAllowedIPAsString returns the allowed IP as comma separated string
|
2021-01-25 20:31:33 +00:00
|
|
|
func (u *User) GetAllowedIPAsString() string {
|
2021-01-17 21:29:08 +00:00
|
|
|
return strings.Join(u.Filters.AllowedIP, ",")
|
2019-12-30 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeniedIPAsString returns the denied IP as comma separated string
|
2021-01-25 20:31:33 +00:00
|
|
|
func (u *User) GetDeniedIPAsString() string {
|
2021-01-17 21:29:08 +00:00
|
|
|
return strings.Join(u.Filters.DeniedIP, ",")
|
2019-12-30 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
2022-02-25 10:51:10 +00:00
|
|
|
// HasExternalAuth returns true if the external authentication is globally enabled
|
|
|
|
// and it is not disabled for this user
|
|
|
|
func (u *User) HasExternalAuth() bool {
|
|
|
|
if u.Filters.Hooks.ExternalAuthDisabled {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if config.ExternalAuthHook != "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return plugin.Handler.HasAuthenticators()
|
|
|
|
}
|
|
|
|
|
2021-09-04 10:11:04 +00:00
|
|
|
// CountUnusedRecoveryCodes returns the number of unused recovery codes
|
|
|
|
func (u *User) CountUnusedRecoveryCodes() int {
|
|
|
|
unused := 0
|
|
|
|
for _, code := range u.Filters.RecoveryCodes {
|
|
|
|
if !code.Used {
|
|
|
|
unused++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return unused
|
|
|
|
}
|
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
// SetEmptySecretsIfNil sets the secrets to empty if nil
|
|
|
|
func (u *User) SetEmptySecretsIfNil() {
|
2021-03-21 18:15:47 +00:00
|
|
|
u.FsConfig.SetEmptySecretsIfNil()
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
vfolder := &u.VirtualFolders[idx]
|
|
|
|
vfolder.FsConfig.SetEmptySecretsIfNil()
|
2020-12-12 09:31:09 +00:00
|
|
|
}
|
2021-09-04 10:11:04 +00:00
|
|
|
if u.Filters.TOTPConfig.Secret == nil {
|
|
|
|
u.Filters.TOTPConfig.Secret = kms.NewEmptySecret()
|
|
|
|
}
|
2020-11-30 20:46:34 +00:00
|
|
|
}
|
|
|
|
|
2019-10-25 16:37:12 +00:00
|
|
|
func (u *User) getACopy() User {
|
2020-11-30 20:46:34 +00:00
|
|
|
u.SetEmptySecretsIfNil()
|
2019-10-25 16:37:12 +00:00
|
|
|
pubKeys := make([]string, len(u.PublicKeys))
|
|
|
|
copy(pubKeys, u.PublicKeys)
|
2021-03-21 18:15:47 +00:00
|
|
|
virtualFolders := make([]vfs.VirtualFolder, 0, len(u.VirtualFolders))
|
|
|
|
for idx := range u.VirtualFolders {
|
|
|
|
vfolder := u.VirtualFolders[idx].GetACopy()
|
|
|
|
virtualFolders = append(virtualFolders, vfolder)
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
permissions := make(map[string][]string)
|
|
|
|
for k, v := range u.Permissions {
|
|
|
|
perms := make([]string, len(v))
|
|
|
|
copy(perms, v)
|
|
|
|
permissions[k] = perms
|
|
|
|
}
|
2022-01-06 09:11:47 +00:00
|
|
|
filters := UserFilters{}
|
2020-08-16 18:17:02 +00:00
|
|
|
filters.MaxUploadFileSize = u.Filters.MaxUploadFileSize
|
2021-02-28 11:10:40 +00:00
|
|
|
filters.TLSUsername = u.Filters.TLSUsername
|
2021-09-04 10:11:04 +00:00
|
|
|
filters.UserType = u.Filters.UserType
|
|
|
|
filters.TOTPConfig.Enabled = u.Filters.TOTPConfig.Enabled
|
|
|
|
filters.TOTPConfig.ConfigName = u.Filters.TOTPConfig.ConfigName
|
|
|
|
filters.TOTPConfig.Secret = u.Filters.TOTPConfig.Secret.Clone()
|
|
|
|
filters.TOTPConfig.Protocols = make([]string, len(u.Filters.TOTPConfig.Protocols))
|
|
|
|
copy(filters.TOTPConfig.Protocols, u.Filters.TOTPConfig.Protocols)
|
2019-12-30 17:37:50 +00:00
|
|
|
filters.AllowedIP = make([]string, len(u.Filters.AllowedIP))
|
|
|
|
copy(filters.AllowedIP, u.Filters.AllowedIP)
|
|
|
|
filters.DeniedIP = make([]string, len(u.Filters.DeniedIP))
|
|
|
|
copy(filters.DeniedIP, u.Filters.DeniedIP)
|
2020-02-19 21:39:30 +00:00
|
|
|
filters.DeniedLoginMethods = make([]string, len(u.Filters.DeniedLoginMethods))
|
|
|
|
copy(filters.DeniedLoginMethods, u.Filters.DeniedLoginMethods)
|
2021-07-11 13:26:51 +00:00
|
|
|
filters.FilePatterns = make([]sdk.PatternsFilter, len(u.Filters.FilePatterns))
|
2020-11-15 21:04:48 +00:00
|
|
|
copy(filters.FilePatterns, u.Filters.FilePatterns)
|
2020-08-17 10:49:20 +00:00
|
|
|
filters.DeniedProtocols = make([]string, len(u.Filters.DeniedProtocols))
|
|
|
|
copy(filters.DeniedProtocols, u.Filters.DeniedProtocols)
|
2021-04-04 20:32:25 +00:00
|
|
|
filters.Hooks.ExternalAuthDisabled = u.Filters.Hooks.ExternalAuthDisabled
|
|
|
|
filters.Hooks.PreLoginDisabled = u.Filters.Hooks.PreLoginDisabled
|
|
|
|
filters.Hooks.CheckPasswordDisabled = u.Filters.Hooks.CheckPasswordDisabled
|
2021-04-05 15:57:30 +00:00
|
|
|
filters.DisableFsChecks = u.Filters.DisableFsChecks
|
2021-08-17 16:08:32 +00:00
|
|
|
filters.AllowAPIKeyAuth = u.Filters.AllowAPIKeyAuth
|
2022-02-25 10:51:10 +00:00
|
|
|
filters.ExternalAuthCacheTime = u.Filters.ExternalAuthCacheTime
|
2021-05-06 19:35:43 +00:00
|
|
|
filters.WebClient = make([]string, len(u.Filters.WebClient))
|
|
|
|
copy(filters.WebClient, u.Filters.WebClient)
|
2022-01-06 09:11:47 +00:00
|
|
|
filters.RecoveryCodes = make([]RecoveryCode, 0, len(u.Filters.RecoveryCodes))
|
2021-09-04 10:11:04 +00:00
|
|
|
for _, code := range u.Filters.RecoveryCodes {
|
|
|
|
if code.Secret == nil {
|
|
|
|
code.Secret = kms.NewEmptySecret()
|
|
|
|
}
|
2022-01-06 09:11:47 +00:00
|
|
|
filters.RecoveryCodes = append(filters.RecoveryCodes, RecoveryCode{
|
2021-09-04 10:11:04 +00:00
|
|
|
Secret: code.Secret.Clone(),
|
|
|
|
Used: code.Used,
|
|
|
|
})
|
|
|
|
}
|
2021-12-10 17:43:26 +00:00
|
|
|
filters.BandwidthLimits = make([]sdk.BandwidthLimit, 0, len(u.Filters.BandwidthLimits))
|
|
|
|
for _, limit := range u.Filters.BandwidthLimits {
|
|
|
|
bwLimit := sdk.BandwidthLimit{
|
|
|
|
UploadBandwidth: limit.UploadBandwidth,
|
|
|
|
DownloadBandwidth: limit.DownloadBandwidth,
|
|
|
|
Sources: make([]string, 0, len(limit.Sources)),
|
|
|
|
}
|
|
|
|
bwLimit.Sources = make([]string, len(limit.Sources))
|
|
|
|
copy(bwLimit.Sources, limit.Sources)
|
|
|
|
filters.BandwidthLimits = append(filters.BandwidthLimits, bwLimit)
|
|
|
|
}
|
2022-01-30 10:42:36 +00:00
|
|
|
filters.DataTransferLimits = make([]sdk.DataTransferLimit, 0, len(u.Filters.DataTransferLimits))
|
|
|
|
for _, limit := range u.Filters.DataTransferLimits {
|
|
|
|
dtLimit := sdk.DataTransferLimit{
|
|
|
|
UploadDataTransfer: limit.UploadDataTransfer,
|
|
|
|
DownloadDataTransfer: limit.DownloadDataTransfer,
|
|
|
|
TotalDataTransfer: limit.TotalDataTransfer,
|
|
|
|
Sources: make([]string, 0, len(limit.Sources)),
|
|
|
|
}
|
|
|
|
dtLimit.Sources = make([]string, len(limit.Sources))
|
|
|
|
copy(dtLimit.Sources, limit.Sources)
|
|
|
|
filters.DataTransferLimits = append(filters.DataTransferLimits, dtLimit)
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
|
2019-10-25 16:37:12 +00:00
|
|
|
return User{
|
2021-07-11 13:26:51 +00:00
|
|
|
BaseUser: sdk.BaseUser{
|
2022-01-30 10:42:36 +00:00
|
|
|
ID: u.ID,
|
|
|
|
Username: u.Username,
|
|
|
|
Email: u.Email,
|
|
|
|
Password: u.Password,
|
|
|
|
PublicKeys: pubKeys,
|
|
|
|
HomeDir: u.HomeDir,
|
|
|
|
UID: u.UID,
|
|
|
|
GID: u.GID,
|
|
|
|
MaxSessions: u.MaxSessions,
|
|
|
|
QuotaSize: u.QuotaSize,
|
|
|
|
QuotaFiles: u.QuotaFiles,
|
|
|
|
Permissions: permissions,
|
|
|
|
UsedQuotaSize: u.UsedQuotaSize,
|
|
|
|
UsedQuotaFiles: u.UsedQuotaFiles,
|
|
|
|
LastQuotaUpdate: u.LastQuotaUpdate,
|
|
|
|
UploadBandwidth: u.UploadBandwidth,
|
|
|
|
DownloadBandwidth: u.DownloadBandwidth,
|
|
|
|
UploadDataTransfer: u.UploadDataTransfer,
|
|
|
|
DownloadDataTransfer: u.DownloadDataTransfer,
|
|
|
|
TotalDataTransfer: u.TotalDataTransfer,
|
|
|
|
UsedUploadDataTransfer: u.UsedUploadDataTransfer,
|
|
|
|
UsedDownloadDataTransfer: u.UsedDownloadDataTransfer,
|
|
|
|
Status: u.Status,
|
|
|
|
ExpirationDate: u.ExpirationDate,
|
|
|
|
LastLogin: u.LastLogin,
|
|
|
|
AdditionalInfo: u.AdditionalInfo,
|
|
|
|
Description: u.Description,
|
|
|
|
CreatedAt: u.CreatedAt,
|
|
|
|
UpdatedAt: u.UpdatedAt,
|
2021-07-11 13:26:51 +00:00
|
|
|
},
|
2022-01-06 09:11:47 +00:00
|
|
|
Filters: filters,
|
2021-07-11 13:26:51 +00:00
|
|
|
VirtualFolders: virtualFolders,
|
|
|
|
FsConfig: u.FsConfig.GetACopy(),
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 10:06:03 +00:00
|
|
|
|
2021-06-19 10:24:43 +00:00
|
|
|
// GetEncryptionAdditionalData returns the additional data to use for AEAD
|
|
|
|
func (u *User) GetEncryptionAdditionalData() string {
|
2021-03-21 18:15:47 +00:00
|
|
|
return u.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGCSCredentialsFilePath returns the path for GCS credentials
|
|
|
|
func (u *User) GetGCSCredentialsFilePath() string {
|
2020-01-31 18:04:00 +00:00
|
|
|
return filepath.Join(credentialsDirPath, fmt.Sprintf("%v_gcs_credentials.json", u.Username))
|
|
|
|
}
|