user.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package dataprovider
  2. import (
  3. "encoding/json"
  4. "path/filepath"
  5. "github.com/drakkan/sftpgo/utils"
  6. )
  7. // Available permissions for SFTP users
  8. const (
  9. // All permissions are granted
  10. PermAny = "*"
  11. // List items such as files and directories is allowed
  12. PermListItems = "list"
  13. // download files is allowed
  14. PermDownload = "download"
  15. // upload files is allowed
  16. PermUpload = "upload"
  17. // delete files or directories is allowed
  18. PermDelete = "delete"
  19. // rename files or directories is allowed
  20. PermRename = "rename"
  21. // create directories is allowed
  22. PermCreateDirs = "create_dirs"
  23. // create symbolic links is allowed
  24. PermCreateSymlinks = "create_symlinks"
  25. )
  26. // User defines an SFTP user
  27. type User struct {
  28. // Database unique identifier
  29. ID int64 `json:"id"`
  30. // Username
  31. Username string `json:"username"`
  32. // Password used for password authentication.
  33. // For users created using SFTPGo REST API the password is be stored using argon2id hashing algo.
  34. // Checking passwords stored with bcrypt is supported too.
  35. // Currently, as fallback, there is a clear text password checking but you should not store passwords
  36. // as clear text and this support could be removed at any time, so please don't depend on it.
  37. Password string `json:"password,omitempty"`
  38. // PublicKey used for public key authentication. At least one between password and public key is mandatory
  39. PublicKey string `json:"public_key,omitempty"`
  40. // The user cannot upload or download files outside this directory. Must be an absolute path
  41. HomeDir string `json:"home_dir"`
  42. // If sftpgo runs as root system user then the created files and directories will be assigned to this system UID
  43. UID int `json:"uid"`
  44. // If sftpgo runs as root system user then the created files and directories will be assigned to this system GID
  45. GID int `json:"gid"`
  46. // Maximum concurrent sessions. 0 means unlimited
  47. MaxSessions int `json:"max_sessions"`
  48. // Maximum size allowed as bytes. 0 means unlimited
  49. QuotaSize int64 `json:"quota_size"`
  50. // Maximum number of files allowed. 0 means unlimited
  51. QuotaFiles int `json:"quota_files"`
  52. // List of the granted permissions
  53. Permissions []string `json:"permissions"`
  54. // Used quota as bytes
  55. UsedQuotaSize int64 `json:"used_quota_size"`
  56. // Used quota as number of files
  57. UsedQuotaFiles int `json:"used_quota_files"`
  58. // Last quota update as unix timestamp in milliseconds
  59. LastQuotaUpdate int64 `json:"last_quota_update"`
  60. // Maximum upload bandwidth as KB/s, 0 means unlimited
  61. UploadBandwidth int64 `json:"upload_bandwidth"`
  62. // Maximum download bandwidth as KB/s, 0 means unlimited
  63. DownloadBandwidth int64 `json:"download_bandwidth"`
  64. }
  65. // HasPerm returns true if the user has the given permission or any permission
  66. func (u *User) HasPerm(permission string) bool {
  67. if utils.IsStringInSlice(PermAny, u.Permissions) {
  68. return true
  69. }
  70. return utils.IsStringInSlice(permission, u.Permissions)
  71. }
  72. // GetPermissionsAsJSON returns the permissions as json byte array
  73. func (u *User) GetPermissionsAsJSON() ([]byte, error) {
  74. return json.Marshal(u.Permissions)
  75. }
  76. // GetUID returns a validate uid, suitable for use with os.Chown
  77. func (u *User) GetUID() int {
  78. if u.UID <= 0 || u.UID > 65535 {
  79. return -1
  80. }
  81. return u.UID
  82. }
  83. // GetGID returns a validate gid, suitable for use with os.Chown
  84. func (u *User) GetGID() int {
  85. if u.GID <= 0 || u.GID > 65535 {
  86. return -1
  87. }
  88. return u.GID
  89. }
  90. // GetHomeDir returns the shortest path name equivalent to the user's home directory
  91. func (u *User) GetHomeDir() string {
  92. return filepath.Clean(u.HomeDir)
  93. }
  94. // HasQuotaRestrictions returns true if there is a quota restriction on number of files or size or both
  95. func (u *User) HasQuotaRestrictions() bool {
  96. return u.QuotaFiles > 0 || u.QuotaSize > 0
  97. }