2019-07-20 10:26:52 +00:00
|
|
|
package dataprovider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-10-07 16:19:01 +00:00
|
|
|
"fmt"
|
2019-12-30 17:37:50 +00:00
|
|
|
"net"
|
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"
|
2019-07-20 10:26:52 +00:00
|
|
|
|
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
|
|
|
)
|
|
|
|
|
2019-07-30 18:51:29 +00:00
|
|
|
// Available permissions for SFTP 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
|
|
|
)
|
|
|
|
|
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"
|
|
|
|
AllowedIP []string `json:"allowed_ip"`
|
|
|
|
// clients connecting from these IP/Mask are not allowed.
|
|
|
|
// Denied rules will be evaluated before allowed ones
|
|
|
|
DeniedIP []string `json:"denied_ip"`
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:41:05 +00:00
|
|
|
// Filesystem defines cloud storage filesystem details
|
|
|
|
type Filesystem struct {
|
2020-01-31 18:04:00 +00:00
|
|
|
// 0 local filesystem, 1 Amazon S3 compatible, 2 Google Cloud Storage
|
|
|
|
Provider int `json:"provider"`
|
|
|
|
S3Config vfs.S3FsConfig `json:"s3config,omitempty"`
|
|
|
|
GCSConfig vfs.GCSFsConfig `json:"gcsconfig,omitempty"`
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 10:26:52 +00:00
|
|
|
// User defines an SFTP user
|
|
|
|
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"`
|
|
|
|
// 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"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFilesystem returns the filesystem for this user
|
|
|
|
func (u *User) GetFilesystem(connectionID string) (vfs.Fs, error) {
|
|
|
|
if u.FsConfig.Provider == 1 {
|
|
|
|
return vfs.NewS3Fs(connectionID, u.GetHomeDir(), u.FsConfig.S3Config)
|
2020-01-31 18:04:00 +00:00
|
|
|
} else if u.FsConfig.Provider == 2 {
|
|
|
|
config := u.FsConfig.GCSConfig
|
|
|
|
config.CredentialFile = u.getGCSCredentialsFilePath()
|
|
|
|
return vfs.NewGCSFs(connectionID, u.GetHomeDir(), config)
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
2020-01-19 12:58:55 +00:00
|
|
|
return vfs.NewOsFs(connectionID, u.GetHomeDir()), nil
|
2019-07-20 10:26:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-05 10:41:25 +00:00
|
|
|
// GetPermissionsForPath returns the permissions for the given path.
|
|
|
|
// The path must be an SFTP 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-01-05 10:41:25 +00:00
|
|
|
sftpPath := filepath.ToSlash(p)
|
|
|
|
if !path.IsAbs(p) {
|
|
|
|
sftpPath = "/" + sftpPath
|
2019-12-25 17:20:19 +00:00
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
sftpPath = path.Clean(sftpPath)
|
|
|
|
dirsForPath := []string{sftpPath}
|
2019-12-25 17:20:19 +00:00
|
|
|
for {
|
2020-01-05 10:41:25 +00:00
|
|
|
if sftpPath == "/" {
|
2019-12-25 17:20:19 +00:00
|
|
|
break
|
|
|
|
}
|
2020-01-05 10:41:25 +00:00
|
|
|
sftpPath = path.Dir(sftpPath)
|
|
|
|
dirsForPath = append(dirsForPath, sftpPath)
|
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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-12-30 17:37:50 +00:00
|
|
|
// IsLoginAllowed return true if the login is allowed from the specified remoteAddr.
|
|
|
|
// 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
|
|
|
|
func (u *User) IsLoginAllowed(remoteAddr string) bool {
|
|
|
|
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 {
|
|
|
|
result += ". Size: " + utils.ByteCountSI(u.UsedQuotaSize)
|
|
|
|
if u.QuotaSize > 0 {
|
|
|
|
result += "/" + utils.ByteCountSI(u.QuotaSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
var dirPerms string
|
|
|
|
for _, p := range perms {
|
|
|
|
if len(dirPerms) > 0 {
|
|
|
|
dirPerms += ", "
|
|
|
|
}
|
|
|
|
dirPerms += p
|
|
|
|
}
|
|
|
|
dp := fmt.Sprintf("%#v: %#v", dir, dirPerms)
|
|
|
|
if dir == "/" {
|
|
|
|
if len(result) > 0 {
|
|
|
|
result = dp + ", " + result
|
|
|
|
} else {
|
|
|
|
result = dp
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if len(result) > 0 {
|
|
|
|
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 {
|
|
|
|
result += utils.ByteCountSI(u.DownloadBandwidth*1000) + "/s."
|
|
|
|
} else {
|
|
|
|
result += "ulimited."
|
|
|
|
}
|
|
|
|
result += " Upload: "
|
|
|
|
if u.UploadBandwidth > 0 {
|
|
|
|
result += utils.ByteCountSI(u.UploadBandwidth*1000) + "/s."
|
|
|
|
} else {
|
|
|
|
result += "ulimited."
|
|
|
|
}
|
|
|
|
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-01-19 06:41:05 +00:00
|
|
|
if u.FsConfig.Provider == 1 {
|
2020-01-31 18:04:00 +00:00
|
|
|
result += fmt.Sprintf("Storage: S3 ")
|
|
|
|
} else if u.FsConfig.Provider == 2 {
|
|
|
|
result += fmt.Sprintf("Storage: GCS ")
|
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
|
|
|
|
func (u User) GetAllowedIPAsString() string {
|
|
|
|
result := ""
|
|
|
|
for _, IPMask := range u.Filters.AllowedIP {
|
|
|
|
if len(result) > 0 {
|
|
|
|
result += ","
|
|
|
|
}
|
|
|
|
result += IPMask
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeniedIPAsString returns the denied IP as comma separated string
|
|
|
|
func (u User) GetDeniedIPAsString() string {
|
|
|
|
result := ""
|
|
|
|
for _, IPMask := range u.Filters.DeniedIP {
|
|
|
|
if len(result) > 0 {
|
|
|
|
result += ","
|
|
|
|
}
|
|
|
|
result += IPMask
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-10-25 16:37:12 +00:00
|
|
|
func (u *User) getACopy() User {
|
|
|
|
pubKeys := make([]string, len(u.PublicKeys))
|
|
|
|
copy(pubKeys, u.PublicKeys)
|
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{}
|
|
|
|
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-01-19 06:41:05 +00:00
|
|
|
fsConfig := Filesystem{
|
|
|
|
Provider: u.FsConfig.Provider,
|
|
|
|
S3Config: vfs.S3FsConfig{
|
|
|
|
Bucket: u.FsConfig.S3Config.Bucket,
|
|
|
|
Region: u.FsConfig.S3Config.Region,
|
|
|
|
AccessKey: u.FsConfig.S3Config.AccessKey,
|
|
|
|
AccessSecret: u.FsConfig.S3Config.AccessSecret,
|
|
|
|
Endpoint: u.FsConfig.S3Config.Endpoint,
|
|
|
|
StorageClass: u.FsConfig.S3Config.StorageClass,
|
2020-01-19 22:23:09 +00:00
|
|
|
KeyPrefix: u.FsConfig.S3Config.KeyPrefix,
|
2020-01-19 06:41:05 +00:00
|
|
|
},
|
2020-01-31 18:04:00 +00:00
|
|
|
GCSConfig: vfs.GCSFsConfig{
|
|
|
|
Bucket: u.FsConfig.GCSConfig.Bucket,
|
|
|
|
CredentialFile: u.FsConfig.GCSConfig.CredentialFile,
|
|
|
|
StorageClass: u.FsConfig.GCSConfig.StorageClass,
|
|
|
|
KeyPrefix: u.FsConfig.GCSConfig.KeyPrefix,
|
|
|
|
},
|
2020-01-19 06:41:05 +00:00
|
|
|
}
|
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,
|
|
|
|
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,
|
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),
|
|
|
|
strconv.FormatInt(int64(u.ExpirationDate), 10),
|
|
|
|
u.HomeDir,
|
|
|
|
strconv.FormatInt(int64(u.UID), 10),
|
|
|
|
strconv.FormatInt(int64(u.GID), 10),
|
|
|
|
}
|
|
|
|
}
|
2020-01-09 11:00:37 +00:00
|
|
|
|
|
|
|
func (u *User) getNotificationFieldsAsEnvVars(action string) []string {
|
|
|
|
return []string{fmt.Sprintf("SFTPGO_USER_ACTION=%v", action),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_USERNAME=%v", u.Username),
|
2020-02-05 21:17:03 +00:00
|
|
|
fmt.Sprintf("SFTPGO_USER_PASSWORD=%v", u.Password),
|
2020-01-09 11:00:37 +00:00
|
|
|
fmt.Sprintf("SFTPGO_USER_ID=%v", u.ID),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_STATUS=%v", u.Status),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_EXPIRATION_DATE=%v", u.ExpirationDate),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_HOME_DIR=%v", u.HomeDir),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_UID=%v", u.UID),
|
2020-02-05 21:17:03 +00:00
|
|
|
fmt.Sprintf("SFTPGO_USER_GID=%v", u.GID),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_QUOTA_FILES=%v", u.QuotaFiles),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_QUOTA_SIZE=%v", u.QuotaSize),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_UPLOAD_BANDWIDTH=%v", u.UploadBandwidth),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_DOWNLOAD_BANDWIDTH=%v", u.DownloadBandwidth),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_MAX_SESSIONS=%v", u.MaxSessions),
|
|
|
|
fmt.Sprintf("SFTPGO_USER_FS_PROVIDER=%v", u.FsConfig.Provider)}
|
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))
|
|
|
|
}
|