2019-07-20 10:26:52 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2020-06-07 21:30:18 +00:00
|
|
|
"errors"
|
2019-10-07 16:19:01 +00:00
|
|
|
"fmt"
|
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"
|
2019-10-07 16:19:01 +00:00
|
|
|
"strconv"
|
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
|
|
|
|
2020-11-04 18:11:40 +00:00
|
|
|
"golang.org/x/net/webdav"
|
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
"github.com/drakkan/sftpgo/kms"
|
2019-12-30 17:37:50 +00:00
|
|
|
"github.com/drakkan/sftpgo/logger"
|
2019-07-20 10:26:52 +00:00
|
|
|
"github.com/drakkan/sftpgo/utils"
|
2020-01-19 06:41:05 +00:00
|
|
|
"github.com/drakkan/sftpgo/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"
|
|
|
|
// rename files or directories is allowed
|
|
|
|
PermRename = "rename"
|
|
|
|
// 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"
|
2020-02-19 21:39:30 +00:00
|
|
|
)
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
var (
|
|
|
|
errNoMatchingVirtualFolder = errors.New("no matching virtual folder found")
|
|
|
|
)
|
|
|
|
|
2020-08-31 17:25:17 +00:00
|
|
|
// CachedUser adds fields useful for caching to a SFTPGo user
|
|
|
|
type CachedUser struct {
|
|
|
|
User User
|
|
|
|
Expiration time.Time
|
|
|
|
Password string
|
2020-11-04 18:11:40 +00:00
|
|
|
LockSystem webdav.LockSystem
|
2020-08-31 17:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsExpired returns true if the cached user is expired
|
2020-11-04 18:11:40 +00:00
|
|
|
func (c *CachedUser) IsExpired() bool {
|
2020-08-31 17:25:17 +00:00
|
|
|
if c.Expiration.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return c.Expiration.Before(time.Now())
|
|
|
|
}
|
|
|
|
|
2020-03-01 21:10:29 +00:00
|
|
|
// ExtensionsFilter defines filters based on file extensions.
|
|
|
|
// These restrictions do not apply to files listing for performance reasons, so
|
|
|
|
// a denied file cannot be downloaded/overwritten/renamed but will still be
|
2020-11-15 21:04:48 +00:00
|
|
|
// in the list of files.
|
2020-03-01 21:10:29 +00:00
|
|
|
// System commands such as Git and rsync interacts with the filesystem directly
|
2020-06-15 21:32:12 +00:00
|
|
|
// and they are not aware about these restrictions so they are not allowed
|
|
|
|
// inside paths with extensions filters
|
2020-03-01 21:10:29 +00:00
|
|
|
type ExtensionsFilter struct {
|
2020-11-15 21:04:48 +00:00
|
|
|
// Virtual path, if no other specific filter is defined, the filter apply for
|
2020-03-01 21:10:29 +00:00
|
|
|
// sub directories too.
|
|
|
|
// For example if filters are defined for the paths "/" and "/sub" then the
|
|
|
|
// filters for "/" are applied for any file outside the "/sub" directory
|
|
|
|
Path string `json:"path"`
|
|
|
|
// only files with these, case insensitive, extensions are allowed.
|
|
|
|
// Shell like expansion is not supported so you have to specify ".jpg" and
|
2020-11-15 21:04:48 +00:00
|
|
|
// not "*.jpg". If you want shell like patterns use pattern filters
|
2020-03-01 21:10:29 +00:00
|
|
|
AllowedExtensions []string `json:"allowed_extensions,omitempty"`
|
|
|
|
// files with these, case insensitive, extensions are not allowed.
|
|
|
|
// Denied file extensions are evaluated before the allowed ones
|
|
|
|
DeniedExtensions []string `json:"denied_extensions,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:04:48 +00:00
|
|
|
// PatternsFilter defines filters based on shell like patterns.
|
|
|
|
// These restrictions do not apply to files listing for performance reasons, so
|
|
|
|
// a denied file cannot be downloaded/overwritten/renamed but will still be
|
|
|
|
// in the list of files.
|
|
|
|
// System commands such as Git and rsync interacts with the filesystem directly
|
|
|
|
// and they are not aware about these restrictions so they are not allowed
|
|
|
|
// inside paths with extensions filters
|
|
|
|
type PatternsFilter struct {
|
|
|
|
// Virtual path, if no other specific filter is defined, the filter apply for
|
|
|
|
// sub directories too.
|
|
|
|
// For example if filters are defined for the paths "/" and "/sub" then the
|
|
|
|
// filters for "/" are applied for any file outside the "/sub" directory
|
|
|
|
Path string `json:"path"`
|
|
|
|
// files with these, case insensitive, patterns are allowed.
|
|
|
|
// Denied file patterns are evaluated before the allowed ones
|
|
|
|
AllowedPatterns []string `json:"allowed_patterns,omitempty"`
|
|
|
|
// files with these, case insensitive, patterns are not allowed.
|
|
|
|
// Denied file patterns are evaluated before the allowed ones
|
|
|
|
DeniedPatterns []string `json:"denied_patterns,omitempty"`
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
// UserFilters defines additional restrictions for a user
|
|
|
|
type UserFilters struct {
|
|
|
|
// only clients connecting from these IP/Mask are allowed.
|
|
|
|
// IP/Mask must be in CIDR notation as defined in RFC 4632 and RFC 4291
|
|
|
|
// for example "192.0.2.0/24" or "2001:db8::/32"
|
2020-02-19 21:39:30 +00:00
|
|
|
AllowedIP []string `json:"allowed_ip,omitempty"`
|
2019-12-30 17:37:50 +00:00
|
|
|
// clients connecting from these IP/Mask are not allowed.
|
|
|
|
// Denied rules will be evaluated before allowed ones
|
2020-02-19 21:39:30 +00:00
|
|
|
DeniedIP []string `json:"denied_ip,omitempty"`
|
|
|
|
// these login methods are not allowed.
|
|
|
|
// If null or empty any available login method is allowed
|
|
|
|
DeniedLoginMethods []string `json:"denied_login_methods,omitempty"`
|
2020-08-17 10:49:20 +00:00
|
|
|
// these protocols are not allowed.
|
|
|
|
// If null or empty any available protocol is allowed
|
|
|
|
DeniedProtocols []string `json:"denied_protocols,omitempty"`
|
2020-03-01 21:10:29 +00:00
|
|
|
// filters based on file extensions.
|
|
|
|
// Please note that these restrictions can be easily bypassed.
|
|
|
|
FileExtensions []ExtensionsFilter `json:"file_extensions,omitempty"`
|
2020-11-15 21:04:48 +00:00
|
|
|
// filter based on shell patterns
|
|
|
|
FilePatterns []PatternsFilter `json:"file_patterns,omitempty"`
|
2020-08-16 18:17:02 +00:00
|
|
|
// max size allowed for a single upload, 0 means unlimited
|
|
|
|
MaxUploadFileSize int64 `json:"max_upload_file_size,omitempty"`
|
2019-12-30 17:37:50 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 18:58:41 +00:00
|
|
|
// FilesystemProvider defines the supported storages
|
|
|
|
type FilesystemProvider int
|
|
|
|
|
2020-10-09 18:25:42 +00:00
|
|
|
// supported values for FilesystemProvider
|
2020-10-05 18:58:41 +00:00
|
|
|
const (
|
2020-10-25 07:18:48 +00:00
|
|
|
LocalFilesystemProvider FilesystemProvider = iota // Local
|
2020-10-26 18:01:17 +00:00
|
|
|
S3FilesystemProvider // AWS S3 compatible
|
2020-10-25 07:18:48 +00:00
|
|
|
GCSFilesystemProvider // Google Cloud Storage
|
|
|
|
AzureBlobFilesystemProvider // Azure Blob Storage
|
2020-12-05 12:48:13 +00:00
|
|
|
CryptedFilesystemProvider // Local encrypted
|
2020-12-12 09:31:09 +00:00
|
|
|
SFTPFilesystemProvider // SFTP
|
2020-10-05 18:58:41 +00:00
|
|
|
)
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// Filesystem defines cloud storage filesystem details
|
|
|
|
type Filesystem struct {
|
2020-10-25 07:18:48 +00:00
|
|
|
Provider FilesystemProvider `json:"provider"`
|
|
|
|
S3Config vfs.S3FsConfig `json:"s3config,omitempty"`
|
|
|
|
GCSConfig vfs.GCSFsConfig `json:"gcsconfig,omitempty"`
|
|
|
|
AzBlobConfig vfs.AzBlobFsConfig `json:"azblobconfig,omitempty"`
|
2020-12-05 12:48:13 +00:00
|
|
|
CryptConfig vfs.CryptFsConfig `json:"cryptconfig,omitempty"`
|
2020-12-12 09:31:09 +00:00
|
|
|
SFTPConfig vfs.SFTPFsConfig `json:"sftpconfig,omitempty"`
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-31 17:25:17 +00:00
|
|
|
// User defines a SFTPGo user
|
2019-07-20 10:26:52 +00:00
|
|
|
type User struct {
|
2019-07-30 18:51:29 +00:00
|
|
|
// Database unique identifier
|
|
|
|
ID int64 `json:"id"`
|
2019-11-13 10:36:21 +00:00
|
|
|
// 1 enabled, 0 disabled (login is not allowed)
|
|
|
|
Status int `json:"status"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// Username
|
|
|
|
Username string `json:"username"`
|
2019-11-13 10:36:21 +00:00
|
|
|
// Account expiration date as unix timestamp in milliseconds. An expired account cannot login.
|
|
|
|
// 0 means no expiration
|
|
|
|
ExpirationDate int64 `json:"expiration_date"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// Password used for password authentication.
|
|
|
|
// For users created using SFTPGo REST API the password is be stored using argon2id hashing algo.
|
2020-01-31 18:04:00 +00:00
|
|
|
// Checking passwords stored with bcrypt, pbkdf2, md5crypt and sha512crypt is supported too.
|
2019-07-30 18:51:29 +00:00
|
|
|
Password string `json:"password,omitempty"`
|
2019-08-07 21:41:10 +00:00
|
|
|
// PublicKeys used for public key authentication. At least one between password and a public key is mandatory
|
|
|
|
PublicKeys []string `json:"public_keys,omitempty"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// The user cannot upload or download files outside this directory. Must be an absolute path
|
|
|
|
HomeDir string `json:"home_dir"`
|
2020-06-07 21:30:18 +00:00
|
|
|
// Mapping between virtual paths and filesystem paths outside the home directory.
|
|
|
|
// Supported for local filesystem only
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualFolders []vfs.VirtualFolder `json:"virtual_folders,omitempty"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// If sftpgo runs as root system user then the created files and directories will be assigned to this system UID
|
|
|
|
UID int `json:"uid"`
|
|
|
|
// If sftpgo runs as root system user then the created files and directories will be assigned to this system GID
|
|
|
|
GID int `json:"gid"`
|
|
|
|
// Maximum concurrent sessions. 0 means unlimited
|
|
|
|
MaxSessions int `json:"max_sessions"`
|
|
|
|
// Maximum size allowed as bytes. 0 means unlimited
|
|
|
|
QuotaSize int64 `json:"quota_size"`
|
|
|
|
// Maximum number of files allowed. 0 means unlimited
|
|
|
|
QuotaFiles int `json:"quota_files"`
|
|
|
|
// List of the granted permissions
|
2019-12-25 17:20:19 +00:00
|
|
|
Permissions map[string][]string `json:"permissions"`
|
2019-07-30 18:51:29 +00:00
|
|
|
// Used quota as bytes
|
|
|
|
UsedQuotaSize int64 `json:"used_quota_size"`
|
|
|
|
// Used quota as number of files
|
|
|
|
UsedQuotaFiles int `json:"used_quota_files"`
|
|
|
|
// Last quota update as unix timestamp in milliseconds
|
|
|
|
LastQuotaUpdate int64 `json:"last_quota_update"`
|
|
|
|
// Maximum upload bandwidth as KB/s, 0 means unlimited
|
|
|
|
UploadBandwidth int64 `json:"upload_bandwidth"`
|
|
|
|
// Maximum download bandwidth as KB/s, 0 means unlimited
|
|
|
|
DownloadBandwidth int64 `json:"download_bandwidth"`
|
2019-11-13 10:36:21 +00:00
|
|
|
// Last login as unix timestamp in milliseconds
|
|
|
|
LastLogin int64 `json:"last_login"`
|
2019-12-30 17:37:50 +00:00
|
|
|
// Additional restrictions
|
|
|
|
Filters UserFilters `json:"filters"`
|
2020-01-19 06:41:05 +00:00
|
|
|
// Filesystem configuration details
|
|
|
|
FsConfig Filesystem `json:"filesystem"`
|
2020-11-25 21:26:34 +00:00
|
|
|
// free form text field for external systems
|
|
|
|
AdditionalInfo string `json:"additional_info,omitempty"`
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilesystem returns the filesystem for this user
|
|
|
|
func (u *User) GetFilesystem(connectionID string) (vfs.Fs, error) {
|
2020-12-05 12:48:13 +00:00
|
|
|
switch u.FsConfig.Provider {
|
|
|
|
case S3FilesystemProvider:
|
2020-01-19 06:41:05 +00:00
|
|
|
return vfs.NewS3Fs(connectionID, u.GetHomeDir(), u.FsConfig.S3Config)
|
2020-12-05 12:48:13 +00:00
|
|
|
case GCSFilesystemProvider:
|
2020-01-31 18:04:00 +00:00
|
|
|
config := u.FsConfig.GCSConfig
|
|
|
|
config.CredentialFile = u.getGCSCredentialsFilePath()
|
|
|
|
return vfs.NewGCSFs(connectionID, u.GetHomeDir(), config)
|
2020-12-05 12:48:13 +00:00
|
|
|
case AzureBlobFilesystemProvider:
|
2020-10-25 07:18:48 +00:00
|
|
|
return vfs.NewAzBlobFs(connectionID, u.GetHomeDir(), u.FsConfig.AzBlobConfig)
|
2020-12-05 12:48:13 +00:00
|
|
|
case CryptedFilesystemProvider:
|
|
|
|
return vfs.NewCryptFs(connectionID, u.GetHomeDir(), u.FsConfig.CryptConfig)
|
2020-12-12 09:31:09 +00:00
|
|
|
case SFTPFilesystemProvider:
|
|
|
|
return vfs.NewSFTPFs(connectionID, u.FsConfig.SFTPConfig)
|
2020-12-05 12:48:13 +00:00
|
|
|
default:
|
|
|
|
return vfs.NewOsFs(connectionID, u.GetHomeDir(), u.VirtualFolders), nil
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-11-22 20:53:04 +00:00
|
|
|
// HideConfidentialData hides user confidential data
|
|
|
|
func (u *User) HideConfidentialData() {
|
|
|
|
u.Password = ""
|
|
|
|
switch u.FsConfig.Provider {
|
|
|
|
case S3FilesystemProvider:
|
|
|
|
u.FsConfig.S3Config.AccessSecret.Hide()
|
|
|
|
case GCSFilesystemProvider:
|
|
|
|
u.FsConfig.GCSConfig.Credentials.Hide()
|
|
|
|
case AzureBlobFilesystemProvider:
|
|
|
|
u.FsConfig.AzBlobConfig.AccountKey.Hide()
|
2020-12-05 12:48:13 +00:00
|
|
|
case CryptedFilesystemProvider:
|
|
|
|
u.FsConfig.CryptConfig.Passphrase.Hide()
|
2020-12-12 09:31:09 +00:00
|
|
|
case SFTPFilesystemProvider:
|
|
|
|
u.FsConfig.SFTPConfig.Password.Hide()
|
|
|
|
u.FsConfig.SFTPConfig.PrivateKey.Hide()
|
2020-11-22 20:53:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 20:31:33 +00:00
|
|
|
// SetEmptySecrets sets to empty any user secret
|
|
|
|
func (u *User) SetEmptySecrets() {
|
|
|
|
u.FsConfig.S3Config.AccessSecret = kms.NewEmptySecret()
|
|
|
|
u.FsConfig.GCSConfig.Credentials = kms.NewEmptySecret()
|
|
|
|
u.FsConfig.AzBlobConfig.AccountKey = kms.NewEmptySecret()
|
|
|
|
u.FsConfig.CryptConfig.Passphrase = kms.NewEmptySecret()
|
|
|
|
u.FsConfig.SFTPConfig.Password = kms.NewEmptySecret()
|
|
|
|
u.FsConfig.SFTPConfig.PrivateKey = kms.NewEmptySecret()
|
|
|
|
}
|
|
|
|
|
2020-12-26 14:11:38 +00:00
|
|
|
// DecryptSecrets tries to decrypts kms secrets
|
|
|
|
func (u *User) DecryptSecrets() error {
|
|
|
|
switch u.FsConfig.Provider {
|
|
|
|
case S3FilesystemProvider:
|
|
|
|
if u.FsConfig.S3Config.AccessSecret.IsEncrypted() {
|
|
|
|
return u.FsConfig.S3Config.AccessSecret.Decrypt()
|
|
|
|
}
|
|
|
|
case GCSFilesystemProvider:
|
|
|
|
if u.FsConfig.GCSConfig.Credentials.IsEncrypted() {
|
|
|
|
return u.FsConfig.GCSConfig.Credentials.Decrypt()
|
|
|
|
}
|
|
|
|
case AzureBlobFilesystemProvider:
|
|
|
|
if u.FsConfig.AzBlobConfig.AccountKey.IsEncrypted() {
|
|
|
|
return u.FsConfig.AzBlobConfig.AccountKey.Decrypt()
|
|
|
|
}
|
|
|
|
case CryptedFilesystemProvider:
|
|
|
|
if u.FsConfig.CryptConfig.Passphrase.IsEncrypted() {
|
|
|
|
return u.FsConfig.CryptConfig.Passphrase.Decrypt()
|
|
|
|
}
|
|
|
|
case SFTPFilesystemProvider:
|
|
|
|
if u.FsConfig.SFTPConfig.Password.IsEncrypted() {
|
|
|
|
if err := u.FsConfig.SFTPConfig.Password.Decrypt(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if u.FsConfig.SFTPConfig.PrivateKey.IsEncrypted() {
|
|
|
|
if err := u.FsConfig.SFTPConfig.PrivateKey.Decrypt(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2020-02-23 10:30:26 +00:00
|
|
|
dirsForPath := utils.GetDirsForSFTPPath(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
|
|
|
|
for _, val := range dirsForPath {
|
|
|
|
if perms, ok := u.Permissions[val]; ok {
|
|
|
|
permissions = perms
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return permissions
|
|
|
|
}
|
|
|
|
|
2020-06-07 21:30:18 +00:00
|
|
|
// GetVirtualFolderForPath returns the virtual folder containing the specified sftp path.
|
|
|
|
// If the path is not inside a virtual folder an error is returned
|
|
|
|
func (u *User) GetVirtualFolderForPath(sftpPath string) (vfs.VirtualFolder, error) {
|
|
|
|
var folder vfs.VirtualFolder
|
2020-10-05 18:58:41 +00:00
|
|
|
if len(u.VirtualFolders) == 0 || u.FsConfig.Provider != LocalFilesystemProvider {
|
2020-06-07 21:30:18 +00:00
|
|
|
return folder, errNoMatchingVirtualFolder
|
2020-05-01 13:27:53 +00:00
|
|
|
}
|
2020-06-15 21:32:12 +00:00
|
|
|
dirsForPath := utils.GetDirsForSFTPPath(sftpPath)
|
2020-05-01 13:27:53 +00:00
|
|
|
for _, val := range dirsForPath {
|
|
|
|
for _, v := range u.VirtualFolders {
|
|
|
|
if v.VirtualPath == val {
|
2020-06-07 21:30:18 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-23 10:30:26 +00:00
|
|
|
// AddVirtualDirs adds virtual folders, if defined, to the given files list
|
|
|
|
func (u *User) AddVirtualDirs(list []os.FileInfo, sftpPath string) []os.FileInfo {
|
|
|
|
if len(u.VirtualFolders) == 0 {
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
for _, v := range u.VirtualFolders {
|
|
|
|
if path.Dir(v.VirtualPath) == sftpPath {
|
2020-08-22 12:52:17 +00:00
|
|
|
fi := vfs.NewFileInfo(v.VirtualPath, true, 0, time.Now(), false)
|
2020-02-23 10:30:26 +00:00
|
|
|
found := false
|
|
|
|
for index, f := range list {
|
|
|
|
if f.Name() == fi.Name() {
|
|
|
|
list[index] = fi
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
list = append(list, fi)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
for _, v := range u.VirtualFolders {
|
|
|
|
if fsPath == v.MappedPath {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-02-23 10:30:26 +00:00
|
|
|
// IsVirtualFolder returns true if the specified sftp path is a virtual folder
|
|
|
|
func (u *User) IsVirtualFolder(sftpPath string) bool {
|
|
|
|
for _, v := range u.VirtualFolders {
|
|
|
|
if sftpPath == v.VirtualPath {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:38:29 +00:00
|
|
|
// HasVirtualFoldersInside returns true if there are virtual folders inside the
|
2020-06-07 21:30:18 +00:00
|
|
|
// specified SFTP path. We assume that path are cleaned
|
|
|
|
func (u *User) HasVirtualFoldersInside(sftpPath string) bool {
|
2020-06-15 21:32:12 +00:00
|
|
|
if sftpPath == "/" && len(u.VirtualFolders) > 0 {
|
|
|
|
return true
|
|
|
|
}
|
2020-06-07 21:30:18 +00:00
|
|
|
for _, v := range u.VirtualFolders {
|
|
|
|
if len(v.VirtualPath) > len(sftpPath) {
|
|
|
|
if strings.HasPrefix(v.VirtualPath, sftpPath+"/") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-26 21:38:29 +00:00
|
|
|
// HasPermissionsInside returns true if the specified sftpPath has no permissions itself and
|
|
|
|
// no subdirs with defined permissions
|
|
|
|
func (u *User) HasPermissionsInside(sftpPath string) bool {
|
|
|
|
for dir := range u.Permissions {
|
|
|
|
if dir == sftpPath {
|
|
|
|
return true
|
|
|
|
} else if len(dir) > len(sftpPath) {
|
|
|
|
if strings.HasPrefix(dir, sftpPath+"/") {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-10 07:11:32 +00:00
|
|
|
// HasOverlappedMappedPaths returns true if this user has virtual folders with overlapped mapped paths
|
|
|
|
func (u *User) HasOverlappedMappedPaths() bool {
|
|
|
|
if len(u.VirtualFolders) <= 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for _, v1 := range u.VirtualFolders {
|
|
|
|
for _, v2 := range u.VirtualFolders {
|
|
|
|
if v1.VirtualPath == v2.VirtualPath {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if isMappedDirOverlapped(v1.MappedPath, v2.MappedPath) {
|
|
|
|
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)
|
|
|
|
if utils.IsStringInSlice(PermAny, perms) {
|
2019-07-20 10:26:52 +00:00
|
|
|
return true
|
|
|
|
}
|
2019-12-25 17:20:19 +00:00
|
|
|
return utils.IsStringInSlice(permission, perms)
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 21:26:42 +00:00
|
|
|
// HasPerms return 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)
|
|
|
|
if utils.IsStringInSlice(PermAny, perms) {
|
2019-11-26 21:26:42 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
for _, permission := range permissions {
|
2019-12-25 17:20:19 +00:00
|
|
|
if !utils.IsStringInSlice(permission, perms) {
|
2019-11-26 21:26:42 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if utils.IsStringInSlice(loginMethod, u.Filters.DeniedLoginMethods) {
|
|
|
|
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() {
|
|
|
|
if !utils.IsStringInSlice(method, SSHMultiStepsLoginMethods) {
|
|
|
|
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
|
|
|
|
for _, method := range ValidSSHLoginMethods {
|
|
|
|
if !utils.IsStringInSlice(method, u.Filters.DeniedLoginMethods) {
|
|
|
|
allowedMethods = append(allowedMethods, method)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allowedMethods
|
|
|
|
}
|
|
|
|
|
2020-03-01 21:10:29 +00:00
|
|
|
// IsFileAllowed returns true if the specified file is allowed by the file restrictions filters
|
2020-11-15 21:04:48 +00:00
|
|
|
func (u *User) IsFileAllowed(virtualPath string) bool {
|
|
|
|
return u.isFilePatternAllowed(virtualPath) && u.isFileExtensionAllowed(virtualPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) isFileExtensionAllowed(virtualPath string) bool {
|
2020-03-01 21:10:29 +00:00
|
|
|
if len(u.Filters.FileExtensions) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
dirsForPath := utils.GetDirsForSFTPPath(path.Dir(virtualPath))
|
2020-03-01 21:10:29 +00:00
|
|
|
var filter ExtensionsFilter
|
|
|
|
for _, dir := range dirsForPath {
|
|
|
|
for _, f := range u.Filters.FileExtensions {
|
|
|
|
if f.Path == dir {
|
|
|
|
filter = f
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
if filter.Path != "" {
|
2020-03-01 21:10:29 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-11-15 21:04:48 +00:00
|
|
|
if filter.Path != "" {
|
|
|
|
toMatch := strings.ToLower(virtualPath)
|
2020-03-01 21:10:29 +00:00
|
|
|
for _, denied := range filter.DeniedExtensions {
|
|
|
|
if strings.HasSuffix(toMatch, denied) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, allowed := range filter.AllowedExtensions {
|
|
|
|
if strings.HasSuffix(toMatch, allowed) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(filter.AllowedExtensions) == 0
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-15 21:04:48 +00:00
|
|
|
func (u *User) isFilePatternAllowed(virtualPath string) bool {
|
|
|
|
if len(u.Filters.FilePatterns) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
dirsForPath := utils.GetDirsForSFTPPath(path.Dir(virtualPath))
|
|
|
|
var filter PatternsFilter
|
|
|
|
for _, dir := range dirsForPath {
|
|
|
|
for _, f := range u.Filters.FilePatterns {
|
|
|
|
if f.Path == dir {
|
|
|
|
filter = f
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.Path != "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if filter.Path != "" {
|
|
|
|
toMatch := strings.ToLower(path.Base(virtualPath))
|
|
|
|
for _, denied := range filter.DeniedPatterns {
|
|
|
|
matched, err := path.Match(denied, toMatch)
|
|
|
|
if err != nil || matched {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, allowed := range filter.AllowedPatterns {
|
|
|
|
matched, err := path.Match(allowed, toMatch)
|
|
|
|
if err == nil && matched {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return len(filter.AllowedPatterns) == 0
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
remoteIP := net.ParseIP(utils.GetIPFromRemoteAddress(remoteAddr))
|
|
|
|
// 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 {
|
|
|
|
if u.UID <= 0 || u.UID > 65535 {
|
|
|
|
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 {
|
|
|
|
if u.GID <= 0 || u.GID > 65535 {
|
|
|
|
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
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// HasQuotaRestrictions returns true if there is a quota restriction on number of files or size or both
|
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
|
|
|
|
2019-10-07 16:19:01 +00:00
|
|
|
// GetQuotaSummary returns used quota and limits if defined
|
|
|
|
func (u *User) GetQuotaSummary() string {
|
|
|
|
var result string
|
|
|
|
result = "Files: " + strconv.Itoa(u.UsedQuotaFiles)
|
|
|
|
if u.QuotaFiles > 0 {
|
|
|
|
result += "/" + strconv.Itoa(u.QuotaFiles)
|
|
|
|
}
|
|
|
|
if u.UsedQuotaSize > 0 || u.QuotaSize > 0 {
|
2021-02-12 21:16:35 +00:00
|
|
|
result += ". Size: " + utils.ByteCountIEC(u.UsedQuotaSize)
|
2019-10-07 16:19:01 +00:00
|
|
|
if u.QuotaSize > 0 {
|
2021-02-12 21:16:35 +00:00
|
|
|
result += "/" + utils.ByteCountIEC(u.QuotaSize)
|
2019-10-07 16:19:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
result := "Download: "
|
|
|
|
if u.DownloadBandwidth > 0 {
|
2021-02-12 21:16:35 +00:00
|
|
|
result += utils.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
|
|
|
}
|
|
|
|
result += " Upload: "
|
|
|
|
if u.UploadBandwidth > 0 {
|
2021-02-12 21:16:35 +00:00
|
|
|
result += utils.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 {
|
|
|
|
var result string
|
2019-11-13 10:36:21 +00:00
|
|
|
if u.LastLogin > 0 {
|
|
|
|
t := utils.GetTimeFromMsecSinceEpoch(u.LastLogin)
|
|
|
|
result += fmt.Sprintf("Last login: %v ", t.Format("2006-01-02 15:04:05")) // YYYY-MM-DD HH:MM:SS
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
switch u.FsConfig.Provider {
|
|
|
|
case S3FilesystemProvider:
|
2020-04-30 13:06:15 +00:00
|
|
|
result += "Storage: S3 "
|
2020-12-12 09:31:09 +00:00
|
|
|
case GCSFilesystemProvider:
|
2020-04-30 13:06:15 +00:00
|
|
|
result += "Storage: GCS "
|
2020-12-12 09:31:09 +00:00
|
|
|
case AzureBlobFilesystemProvider:
|
2020-10-25 07:18:48 +00:00
|
|
|
result += "Storage: Azure "
|
2020-12-12 09:31:09 +00:00
|
|
|
case CryptedFilesystemProvider:
|
|
|
|
result += "Storage: Encrypted "
|
|
|
|
case SFTPFilesystemProvider:
|
|
|
|
result += "Storage: SFTP "
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
if len(u.PublicKeys) > 0 {
|
|
|
|
result += fmt.Sprintf("Public keys: %v ", len(u.PublicKeys))
|
|
|
|
}
|
|
|
|
if u.MaxSessions > 0 {
|
|
|
|
result += fmt.Sprintf("Max sessions: %v ", u.MaxSessions)
|
|
|
|
}
|
|
|
|
if u.UID > 0 {
|
|
|
|
result += fmt.Sprintf("UID: %v ", u.UID)
|
|
|
|
}
|
|
|
|
if u.GID > 0 {
|
|
|
|
result += fmt.Sprintf("GID: %v ", u.GID)
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
if len(u.Filters.DeniedIP) > 0 {
|
|
|
|
result += fmt.Sprintf("Denied IP/Mask: %v ", len(u.Filters.DeniedIP))
|
|
|
|
}
|
|
|
|
if len(u.Filters.AllowedIP) > 0 {
|
|
|
|
result += fmt.Sprintf("Allowed IP/Mask: %v ", len(u.Filters.AllowedIP))
|
|
|
|
}
|
2019-10-07 16:19:01 +00:00
|
|
|
return result
|
|
|
|
}
|
2019-10-25 16:37:12 +00:00
|
|
|
|
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 {
|
|
|
|
t := utils.GetTimeFromMsecSinceEpoch(u.ExpirationDate)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-30 20:46:34 +00:00
|
|
|
// SetEmptySecretsIfNil sets the secrets to empty if nil
|
|
|
|
func (u *User) SetEmptySecretsIfNil() {
|
|
|
|
if u.FsConfig.S3Config.AccessSecret == nil {
|
|
|
|
u.FsConfig.S3Config.AccessSecret = kms.NewEmptySecret()
|
|
|
|
}
|
|
|
|
if u.FsConfig.GCSConfig.Credentials == nil {
|
|
|
|
u.FsConfig.GCSConfig.Credentials = kms.NewEmptySecret()
|
|
|
|
}
|
|
|
|
if u.FsConfig.AzBlobConfig.AccountKey == nil {
|
|
|
|
u.FsConfig.AzBlobConfig.AccountKey = kms.NewEmptySecret()
|
|
|
|
}
|
2020-12-05 12:48:13 +00:00
|
|
|
if u.FsConfig.CryptConfig.Passphrase == nil {
|
|
|
|
u.FsConfig.CryptConfig.Passphrase = kms.NewEmptySecret()
|
|
|
|
}
|
2020-12-12 09:31:09 +00:00
|
|
|
if u.FsConfig.SFTPConfig.Password == nil {
|
|
|
|
u.FsConfig.SFTPConfig.Password = kms.NewEmptySecret()
|
|
|
|
}
|
|
|
|
if u.FsConfig.SFTPConfig.PrivateKey == nil {
|
|
|
|
u.FsConfig.SFTPConfig.PrivateKey = 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)
|
2020-02-23 10:30:26 +00:00
|
|
|
virtualFolders := make([]vfs.VirtualFolder, len(u.VirtualFolders))
|
|
|
|
copy(virtualFolders, u.VirtualFolders)
|
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
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
filters := UserFilters{}
|
2020-08-16 18:17:02 +00:00
|
|
|
filters.MaxUploadFileSize = u.Filters.MaxUploadFileSize
|
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)
|
2020-03-01 21:10:29 +00:00
|
|
|
filters.FileExtensions = make([]ExtensionsFilter, len(u.Filters.FileExtensions))
|
|
|
|
copy(filters.FileExtensions, u.Filters.FileExtensions)
|
2020-11-15 21:04:48 +00:00
|
|
|
filters.FilePatterns = make([]PatternsFilter, len(u.Filters.FilePatterns))
|
|
|
|
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)
|
2020-01-19 06:41:05 +00:00
|
|
|
fsConfig := Filesystem{
|
|
|
|
Provider: u.FsConfig.Provider,
|
|
|
|
S3Config: vfs.S3FsConfig{
|
2020-03-13 18:13:58 +00:00
|
|
|
Bucket: u.FsConfig.S3Config.Bucket,
|
|
|
|
Region: u.FsConfig.S3Config.Region,
|
|
|
|
AccessKey: u.FsConfig.S3Config.AccessKey,
|
2020-11-30 20:46:34 +00:00
|
|
|
AccessSecret: u.FsConfig.S3Config.AccessSecret.Clone(),
|
2020-03-13 18:13:58 +00:00
|
|
|
Endpoint: u.FsConfig.S3Config.Endpoint,
|
|
|
|
StorageClass: u.FsConfig.S3Config.StorageClass,
|
|
|
|
KeyPrefix: u.FsConfig.S3Config.KeyPrefix,
|
|
|
|
UploadPartSize: u.FsConfig.S3Config.UploadPartSize,
|
|
|
|
UploadConcurrency: u.FsConfig.S3Config.UploadConcurrency,
|
2020-01-19 06:41:05 +00:00
|
|
|
},
|
2020-01-31 18:04:00 +00:00
|
|
|
GCSConfig: vfs.GCSFsConfig{
|
2020-02-19 08:41:15 +00:00
|
|
|
Bucket: u.FsConfig.GCSConfig.Bucket,
|
|
|
|
CredentialFile: u.FsConfig.GCSConfig.CredentialFile,
|
2020-11-30 20:46:34 +00:00
|
|
|
Credentials: u.FsConfig.GCSConfig.Credentials.Clone(),
|
2020-02-19 08:41:15 +00:00
|
|
|
AutomaticCredentials: u.FsConfig.GCSConfig.AutomaticCredentials,
|
|
|
|
StorageClass: u.FsConfig.GCSConfig.StorageClass,
|
|
|
|
KeyPrefix: u.FsConfig.GCSConfig.KeyPrefix,
|
2020-01-31 18:04:00 +00:00
|
|
|
},
|
2020-10-25 07:18:48 +00:00
|
|
|
AzBlobConfig: vfs.AzBlobFsConfig{
|
|
|
|
Container: u.FsConfig.AzBlobConfig.Container,
|
|
|
|
AccountName: u.FsConfig.AzBlobConfig.AccountName,
|
2020-11-30 20:46:34 +00:00
|
|
|
AccountKey: u.FsConfig.AzBlobConfig.AccountKey.Clone(),
|
2020-10-25 07:18:48 +00:00
|
|
|
Endpoint: u.FsConfig.AzBlobConfig.Endpoint,
|
|
|
|
SASURL: u.FsConfig.AzBlobConfig.SASURL,
|
|
|
|
KeyPrefix: u.FsConfig.AzBlobConfig.KeyPrefix,
|
|
|
|
UploadPartSize: u.FsConfig.AzBlobConfig.UploadPartSize,
|
|
|
|
UploadConcurrency: u.FsConfig.AzBlobConfig.UploadConcurrency,
|
|
|
|
UseEmulator: u.FsConfig.AzBlobConfig.UseEmulator,
|
2020-11-22 20:53:04 +00:00
|
|
|
AccessTier: u.FsConfig.AzBlobConfig.AccessTier,
|
2020-10-25 07:18:48 +00:00
|
|
|
},
|
2020-12-05 12:48:13 +00:00
|
|
|
CryptConfig: vfs.CryptFsConfig{
|
|
|
|
Passphrase: u.FsConfig.CryptConfig.Passphrase.Clone(),
|
|
|
|
},
|
2020-12-12 09:31:09 +00:00
|
|
|
SFTPConfig: vfs.SFTPFsConfig{
|
|
|
|
Endpoint: u.FsConfig.SFTPConfig.Endpoint,
|
|
|
|
Username: u.FsConfig.SFTPConfig.Username,
|
|
|
|
Password: u.FsConfig.SFTPConfig.Password.Clone(),
|
|
|
|
PrivateKey: u.FsConfig.SFTPConfig.PrivateKey.Clone(),
|
|
|
|
Prefix: u.FsConfig.SFTPConfig.Prefix,
|
|
|
|
},
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-12-12 09:56:02 +00:00
|
|
|
if len(u.FsConfig.SFTPConfig.Fingerprints) > 0 {
|
|
|
|
fsConfig.SFTPConfig.Fingerprints = make([]string, len(u.FsConfig.SFTPConfig.Fingerprints))
|
|
|
|
copy(fsConfig.SFTPConfig.Fingerprints, u.FsConfig.SFTPConfig.Fingerprints)
|
|
|
|
}
|
2019-12-30 17:37:50 +00:00
|
|
|
|
2019-10-25 16:37:12 +00:00
|
|
|
return User{
|
|
|
|
ID: u.ID,
|
|
|
|
Username: u.Username,
|
|
|
|
Password: u.Password,
|
|
|
|
PublicKeys: pubKeys,
|
|
|
|
HomeDir: u.HomeDir,
|
2020-02-23 10:30:26 +00:00
|
|
|
VirtualFolders: virtualFolders,
|
2019-10-25 16:37:12 +00:00
|
|
|
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,
|
2019-11-13 10:36:21 +00:00
|
|
|
Status: u.Status,
|
|
|
|
ExpirationDate: u.ExpirationDate,
|
|
|
|
LastLogin: u.LastLogin,
|
2019-12-30 17:37:50 +00:00
|
|
|
Filters: filters,
|
2020-01-19 06:41:05 +00:00
|
|
|
FsConfig: fsConfig,
|
2020-11-25 21:26:34 +00:00
|
|
|
AdditionalInfo: u.AdditionalInfo,
|
2019-10-25 16:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-14 10:06:03 +00:00
|
|
|
|
2020-01-09 11:00:37 +00:00
|
|
|
func (u *User) getNotificationFieldsAsSlice(action string) []string {
|
|
|
|
return []string{action, u.Username,
|
2019-11-14 10:06:03 +00:00
|
|
|
strconv.FormatInt(u.ID, 10),
|
|
|
|
strconv.FormatInt(int64(u.Status), 10),
|
2020-04-30 12:23:55 +00:00
|
|
|
strconv.FormatInt(u.ExpirationDate, 10),
|
2019-11-14 10:06:03 +00:00
|
|
|
u.HomeDir,
|
|
|
|
strconv.FormatInt(int64(u.UID), 10),
|
|
|
|
strconv.FormatInt(int64(u.GID), 10),
|
|
|
|
}
|
|
|
|
}
|
2020-01-09 11:00:37 +00:00
|
|
|
|
2020-01-31 18:04:00 +00:00
|
|
|
func (u *User) getGCSCredentialsFilePath() string {
|
|
|
|
return filepath.Join(credentialsDirPath, fmt.Sprintf("%v_gcs_credentials.json", u.Username))
|
|
|
|
}
|